📄 irda-usb.c
字号:
} return 0;}/********************** USB CONFIG SUBROUTINES **********************//* * Various subroutines dealing with USB stuff we use to configure and * initialise each irda-usb instance. * These functions are used below in the main calls of the driver... *//*------------------------------------------------------------------*//* * Function irda_usb_parse_endpoints(dev, ifnum) * * Parse the various endpoints and find the one we need. * * The endpoint are the pipes used to communicate with the USB device. * The spec defines 2 endpoints of type bulk transfer, one in, and one out. * These are used to pass frames back and forth with the dongle. * Most dongle have also an interrupt endpoint, that will be probably * documented in the next spec... */static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_endpoint_descriptor *endpoint, int ennum){ int i; /* Endpoint index in table */ /* Init : no endpoints */ self->bulk_in_ep = 0; self->bulk_out_ep = 0; self->bulk_int_ep = 0; /* Let's look at all those endpoints */ for(i = 0; i < ennum; i++) { /* All those variables will get optimised by the compiler, * so let's aim for clarity... - Jean II */ __u8 ep; /* Endpoint address */ __u8 dir; /* Endpoint direction */ __u8 attr; /* Endpoint attribute */ __u16 psize; /* Endpoint max packet size in bytes */ /* Get endpoint address, direction and attribute */ ep = endpoint[i].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; dir = endpoint[i].bEndpointAddress & USB_ENDPOINT_DIR_MASK; attr = endpoint[i].bmAttributes; psize = endpoint[i].wMaxPacketSize; /* Is it a bulk endpoint ??? */ if(attr == USB_ENDPOINT_XFER_BULK) { /* We need to find an IN and an OUT */ if(dir == USB_DIR_IN) { /* This is our Rx endpoint */ self->bulk_in_ep = ep; } else { /* This is our Tx endpoint */ self->bulk_out_ep = ep; self->bulk_out_mtu = psize; } } else { if((attr == USB_ENDPOINT_XFER_INT) && (dir == USB_DIR_IN)) { /* This is our interrupt endpoint */ self->bulk_int_ep = ep; } else { ERROR(__FUNCTION__ "(), Unrecognised endpoint %02X.\n", ep); } } } IRDA_DEBUG(0, __FUNCTION__ "(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n", self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep); /* Should be 8, 16, 32 or 64 bytes */ ASSERT(self->bulk_out_mtu == 64, ;); return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));}#ifdef IU_DUMP_CLASS_DESC/*------------------------------------------------------------------*//* * Function usb_irda_dump_class_desc(desc) * * Prints out the contents of the IrDA class descriptor * */static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc){ printk("bLength=%x\n", desc->bLength); printk("bDescriptorType=%x\n", desc->bDescriptorType); printk("bcdSpecRevision=%x\n", desc->bcdSpecRevision); printk("bmDataSize=%x\n", desc->bmDataSize); printk("bmWindowSize=%x\n", desc->bmWindowSize); printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime); printk("wBaudRate=%x\n", desc->wBaudRate); printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs); printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff); printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);}#endif /* IU_DUMP_CLASS_DESC *//*------------------------------------------------------------------*//* * Function irda_usb_find_class_desc(dev, ifnum) * * Returns instance of IrDA class descriptor, or NULL if not found * * The class descriptor is some extra info that IrDA USB devices will * offer to us, describing their IrDA characteristics. We will use that in * irda_usb_init_qos() */static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum){ struct irda_class_desc *desc; int ret; desc = kmalloc(sizeof (*desc), GFP_KERNEL); if (desc == NULL) return NULL; memset(desc, 0, sizeof(*desc)); /* USB-IrDA class spec 1.0: * 6.1.3: Standard "Get Descriptor" Device Request is not * appropriate to retrieve class-specific descriptor * 6.2.5: Class Specific "Get Class Descriptor" Interface Request * is mandatory and returns the USB-IrDA class descriptor */ ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0), IU_REQ_GET_CLASS_DESC, USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, ifnum, desc, sizeof(*desc), MSECS_TO_JIFFIES(500)); IRDA_DEBUG(1, __FUNCTION__ "(), ret=%d\n", ret); if (ret < sizeof(*desc)) { WARNING("usb-irda: class_descriptor read %s (%d)\n", (ret<0) ? "failed" : "too short", ret); } else if (desc->bDescriptorType != USB_DT_IRDA) { WARNING("usb-irda: bad class_descriptor type\n"); } else {#ifdef IU_DUMP_CLASS_DESC irda_usb_dump_class_desc(desc);#endif /* IU_DUMP_CLASS_DESC */ return desc; } kfree(desc); return NULL;}/*********************** USB DEVICE CALLBACKS ***********************//* * Main calls from the USB subsystem. * Mostly registering a new irda-usb device and removing it.... *//*------------------------------------------------------------------*//* * This routine is called by the USB subsystem for each new device * in the system. We need to check if the device is ours, and in * this case start handling it. * Note : it might be worth protecting this function by a global * spinlock... Or not, because maybe USB already deal with that... */static void *irda_usb_probe(struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id){ struct irda_usb_cb *self = NULL; struct usb_interface_descriptor *interface; struct irda_class_desc *irda_desc; int ret; int i; /* Note : the probe make sure to call us only for devices that * matches the list of dongle (top of the file). So, we * don't need to check if the dongle is really ours. * Jean II */ MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n", dev->devnum, dev->descriptor.idVendor, dev->descriptor.idProduct); /* Try to cleanup all instance that have a pending disconnect * In theory, it can't happen any longer. * Jean II */ for (i = 0; i < NIRUSB; i++) { struct irda_usb_cb *irda = &irda_instance[i]; if((irda->usbdev != NULL) && (irda->present == 0) && (irda->netopen == 0)) { IRDA_DEBUG(0, __FUNCTION__ "(), found a zombie instance !!!\n"); irda_usb_disconnect(irda->usbdev, (void *) irda); } } /* Find an free instance to handle this new device... */ self = NULL; for (i = 0; i < NIRUSB; i++) { if(irda_instance[i].usbdev == NULL) { self = &irda_instance[i]; break; } } if(self == NULL) { WARNING("Too many USB IrDA devices !!! (max = %d)\n", NIRUSB); return NULL; } /* Reset the instance */ self->present = 0; self->netopen = 0; /* Create all of the needed urbs */ for (i = 0; i < IU_MAX_RX_URBS; i++) { self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL); if (!self->rx_urb[i]) { int j; for (j = 0; j < i; j++) usb_free_urb(self->rx_urb[j]); return NULL; } } self->tx_urb = usb_alloc_urb(0, GFP_KERNEL); if (!self->tx_urb) { for (i = 0; i < IU_MAX_RX_URBS; i++) usb_free_urb(self->rx_urb[i]); return NULL; } self->speed_urb = usb_alloc_urb(0, GFP_KERNEL); if (!self->speed_urb) { for (i = 0; i < IU_MAX_RX_URBS; i++) usb_free_urb(self->rx_urb[i]); usb_free_urb(self->tx_urb); return NULL; } /* Is this really necessary? */ if (usb_set_configuration (dev, dev->config[0].bConfigurationValue) < 0) { err("set_configuration failed"); return NULL; } /* Is this really necessary? */ /* Note : some driver do hardcode the interface number, some others * specify an alternate, but very few driver do like this. * Jean II */ ret = usb_set_interface(dev, ifnum, 0); IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", ifnum, ret); switch (ret) { case 0: break; case -EPIPE: /* -EPIPE = -32 */ usb_clear_halt(dev, usb_sndctrlpipe(dev, 0)); IRDA_DEBUG(0, __FUNCTION__ "(), Clearing stall on control interface\n" ); break; default: IRDA_DEBUG(0, __FUNCTION__ "(), Unknown error %d\n", ret); return NULL; break; } /* Find our endpoints */ interface = &dev->actconfig->interface[ifnum].altsetting[0]; if(!irda_usb_parse_endpoints(self, interface->endpoint, interface->bNumEndpoints)) { ERROR(__FUNCTION__ "(), Bogus endpoints...\n"); return NULL; } /* Find IrDA class descriptor */ irda_desc = irda_usb_find_class_desc(dev, ifnum); if (irda_desc == NULL) return NULL; self->irda_desc = irda_desc; self->present = 1; self->netopen = 0; self->capability = id->driver_info; self->usbdev = dev; ret = irda_usb_open(self); if (ret) return NULL; return self;}/*------------------------------------------------------------------*//* * The current irda-usb device is removed, the USB layer tell us * to shut it down... * One of the constraints is that when we exit this function, * we cannot use the usb_device no more. Gone. Destroyed. kfree(). * Most other subsystem allow you to destroy the instance at a time * when it's convenient to you, to postpone it to a later date, but * not the USB subsystem. * So, we must make bloody sure that everything gets deactivated. * Jean II */static void irda_usb_disconnect(struct usb_device *dev, void *ptr){ unsigned long flags; struct irda_usb_cb *self = (struct irda_usb_cb *) ptr; int i; IRDA_DEBUG(1, __FUNCTION__ "()\n"); /* Make sure that the Tx path is not executing. - Jean II */ spin_lock_irqsave(&self->lock, flags); /* Oups ! We are not there any more. * This will stop/desactivate the Tx path. - Jean II */ self->present = 0; /* We need to have irq enabled to unlink the URBs. That's OK, * at this point the Tx path is gone - Jean II */ spin_unlock_irqrestore(&self->lock, flags); /* Hum... Check if networking is still active (avoid races) */ if((self->netopen) || (self->irlap)) { /* Accept no more transmissions */ /*netif_device_detach(self->netdev);*/ netif_stop_queue(self->netdev); /* Stop all the receive URBs */ for (i = 0; i < IU_MAX_RX_URBS; i++) usb_unlink_urb(self->rx_urb[i]); /* Cancel Tx and speed URB. * Toggle flags to make sure it's synchronous. */ self->tx_urb->transfer_flags &= ~USB_ASYNC_UNLINK; usb_unlink_urb(self->tx_urb); self->speed_urb->transfer_flags &= ~USB_ASYNC_UNLINK; usb_unlink_urb(self->speed_urb); } /* Cleanup the device stuff */ irda_usb_close(self); /* No longer attached to USB bus */ self->usbdev = NULL; /* Clean up our urbs */ for (i = 0; i < IU_MAX_RX_URBS; i++) usb_free_urb(self->rx_urb[i]); /* Clean up Tx and speed URB */ usb_free_urb(self->tx_urb); usb_free_urb(self->speed_urb); IRDA_DEBUG(0, __FUNCTION__ "(), USB IrDA Disconnected\n");}/*------------------------------------------------------------------*//* * USB device callbacks */static struct usb_driver irda_driver = { name: "irda-usb", probe: irda_usb_probe, disconnect: irda_usb_disconnect, id_table: dongles,};/************************* MODULE CALLBACKS *************************//* * Deal with module insertion/removal * Mostly tell USB about our existence *//*------------------------------------------------------------------*//* * Module insertion */int __init usb_irda_init(void){ if (usb_register(&irda_driver) < 0) return -1; MESSAGE("USB IrDA support registered\n"); return 0;}module_init(usb_irda_init);/*------------------------------------------------------------------*//* * Module removal */void __exit usb_irda_cleanup(void){ struct irda_usb_cb *irda = NULL; int i; /* Find zombie instances and kill them... * In theory, it can't happen any longer. Jean II */ for (i = 0; i < NIRUSB; i++) { irda = &irda_instance[i]; /* If the Device is zombie */ if((irda->usbdev != NULL) && (irda->present == 0)) { IRDA_DEBUG(0, __FUNCTION__ "(), disconnect zombie now !\n"); irda_usb_disconnect(irda->usbdev, (void *) irda); } } /* Deregister the driver and remove all pending instances */ usb_deregister(&irda_driver);}module_exit(usb_irda_cleanup);/*------------------------------------------------------------------*//* * Module parameters */MODULE_PARM(qos_mtt_bits, "i");MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net> and Jean Tourrilhes <jt@hpl.hp.com>");MODULE_DESCRIPTION("IrDA-USB Dongle Driver"); MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -