irda-usb.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:50k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  *
  3.  * Filename:      irda-usb.c
  4.  * Version:       0.9b
  5.  * Description:   IrDA-USB Driver
  6.  * Status:        Experimental 
  7.  * Author:        Dag Brattli <dag@brattli.net>
  8.  *
  9.  * Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
  10.  *      Copyright (C) 2001, Dag Brattli <dag@brattli.net>
  11.  *      Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
  12.  *          
  13.  * This program is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 2 of the License, or
  16.  * (at your option) any later version.
  17.  *
  18.  * This program is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with this program; if not, write to the Free Software
  25.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  *
  27.  *****************************************************************************/
  28. /*
  29.  *     IMPORTANT NOTE
  30.  *     --------------
  31.  *
  32.  * As of kernel 2.4.10, this is the state of compliance and testing of
  33.  * this driver (irda-usb) with regards to the USB low level drivers...
  34.  *
  35.  * This driver has been tested SUCCESSFULLY with the following drivers :
  36.  * o usb-uhci (For Intel/Via USB controllers)
  37.  * o usb-ohci (For other USB controllers)
  38.  *
  39.  * This driver has NOT been tested with the following drivers :
  40.  * o usb-ehci (USB 2.0 controllers)
  41.  *
  42.  * This driver WON'T WORK with the following drivers :
  43.  * o uhci (Alternate/JE driver for Intel/Via USB controllers)
  44.  * Amongst the reasons :
  45.  * o uhci doesn't implement USB_ZERO_PACKET
  46.  * o uhci non-compliant use of urb->timeout
  47.  * The final fix for USB_ZERO_PACKET in uhci is likely to be in 2.4.19 and
  48.  * 2.5.8. With this fix, the driver will work properly. More on that later.
  49.  *
  50.  * Jean II
  51.  */
  52. /*------------------------------------------------------------------*/
  53. #include <linux/module.h>
  54. #include <linux/kernel.h>
  55. #include <linux/types.h>
  56. #include <linux/init.h>
  57. #include <linux/skbuff.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/slab.h>
  60. #include <linux/rtnetlink.h>
  61. #include <linux/usb.h>
  62. #include <net/irda/irda.h>
  63. #include <net/irda/irlap.h>
  64. #include <net/irda/irda_device.h>
  65. #include <net/irda/wrapper.h>
  66. #include <net/irda/irda-usb.h>
  67. /*------------------------------------------------------------------*/
  68. static int qos_mtt_bits = 0;
  69. /* Master instance for each hardware found */
  70. #define NIRUSB 4 /* Max number of USB-IrDA dongles */
  71. static struct irda_usb_cb irda_instance[NIRUSB];
  72. /* These are the currently known IrDA USB dongles. Add new dongles here */
  73. static struct usb_device_id dongles[] = {
  74. /* ACTiSYS Corp,  ACT-IR2000U FIR-USB Adapter */
  75. { USB_DEVICE(0x9c4, 0x011), driver_info: IUC_SPEED_BUG | IUC_NO_WINDOW },
  76. /* KC Technology Inc.,  KC-180 USB IrDA Device */
  77. { USB_DEVICE(0x50f, 0x180), driver_info: IUC_SPEED_BUG | IUC_NO_WINDOW },
  78. /* Extended Systems, Inc.,  XTNDAccess IrDA USB (ESI-9685) */
  79. { USB_DEVICE(0x8e9, 0x100), driver_info: IUC_SPEED_BUG | IUC_NO_WINDOW },
  80. { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS |
  81.                USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  82.   bInterfaceClass: USB_CLASS_APP_SPEC,
  83.   bInterfaceSubClass: USB_CLASS_IRDA,
  84.   driver_info: IUC_DEFAULT, },
  85. { }, /* The end */
  86. };
  87. /*
  88.  * Important note :
  89.  * Devices based on the SigmaTel chipset (0x66f, 0x4200) are not compliant
  90.  * with the USB-IrDA specification (and actually very very different), and
  91.  * there is no way this driver can support those devices, apart from
  92.  * a complete rewrite...
  93.  * Jean II
  94.  */
  95. MODULE_DEVICE_TABLE(usb, dongles);
  96. /*------------------------------------------------------------------*/
  97. static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum);
  98. static void irda_usb_disconnect(struct usb_device *dev, void *ptr);
  99. static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
  100. static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *dev);
  101. static int irda_usb_open(struct irda_usb_cb *self);
  102. static int irda_usb_close(struct irda_usb_cb *self);
  103. static void speed_bulk_callback(struct urb *urb);
  104. static void write_bulk_callback(struct urb *urb);
  105. static void irda_usb_receive(struct urb *urb);
  106. static int irda_usb_net_init(struct net_device *dev);
  107. static int irda_usb_net_open(struct net_device *dev);
  108. static int irda_usb_net_close(struct net_device *dev);
  109. static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  110. static void irda_usb_net_timeout(struct net_device *dev);
  111. static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev);
  112. /************************ TRANSMIT ROUTINES ************************/
  113. /*
  114.  * Receive packets from the IrDA stack and send them on the USB pipe.
  115.  * Handle speed change, timeout and lot's of uglyness...
  116.  */
  117. /*------------------------------------------------------------------*/
  118. /*
  119.  * Function irda_usb_build_header(self, skb, header)
  120.  *
  121.  *   Builds USB-IrDA outbound header
  122.  *
  123.  * When we send an IrDA frame over an USB pipe, we add to it a 1 byte
  124.  * header. This function create this header with the proper values.
  125.  *
  126.  * Important note : the USB-IrDA spec 1.0 say very clearly in chapter 5.4.2.2
  127.  * that the setting of the link speed and xbof number in this outbound header
  128.  * should be applied *AFTER* the frame has been sent.
  129.  * Unfortunately, some devices are not compliant with that... It seems that
  130.  * reading the spec is far too difficult...
  131.  * Jean II
  132.  */
  133. static void irda_usb_build_header(struct irda_usb_cb *self,
  134.   __u8 *header,
  135.   int force)
  136. {
  137. /* Set the negotiated link speed */
  138. if (self->new_speed != -1) {
  139. /* Hum... Ugly hack :-(
  140.  * Some device are not compliant with the spec and change
  141.  * parameters *before* sending the frame. - Jean II
  142.  */
  143. if ((self->capability & IUC_SPEED_BUG) &&
  144.     (!force) && (self->speed != -1)) {
  145. /* No speed and xbofs change here
  146.  * (we'll do it later in the write callback) */
  147. IRDA_DEBUG(2, "%s(), not changing speed yetn", __FUNCTION__);
  148. *header = 0;
  149. return;
  150. }
  151. IRDA_DEBUG(2, "%s(), changing speed to %dn", __FUNCTION__, self->new_speed);
  152. self->speed = self->new_speed;
  153. self->new_speed = -1;
  154. switch (self->speed) {
  155. case 2400:
  156.         *header = SPEED_2400;
  157. break;
  158. default:
  159. case 9600:
  160. *header = SPEED_9600;
  161. break;
  162. case 19200:
  163. *header = SPEED_19200;
  164. break;
  165. case 38400:
  166. *header = SPEED_38400;
  167. break;
  168. case 57600:
  169.         *header = SPEED_57600;
  170. break;
  171. case 115200:
  172.         *header = SPEED_115200;
  173. break;
  174. case 576000:
  175.         *header = SPEED_576000;
  176. break;
  177. case 1152000:
  178.         *header = SPEED_1152000;
  179. break;
  180. case 4000000:
  181.         *header = SPEED_4000000;
  182. self->new_xbofs = 0;
  183. break;
  184. }
  185. } else
  186. /* No change */
  187. *header = 0;
  188. /* Set the negotiated additional XBOFS */
  189. if (self->new_xbofs != -1) {
  190. IRDA_DEBUG(2, "%s(), changing xbofs to %dn", __FUNCTION__, self->new_xbofs);
  191. self->xbofs = self->new_xbofs;
  192. self->new_xbofs = -1;
  193. switch (self->xbofs) {
  194. case 48:
  195. *header |= 0x10;
  196. break;
  197. case 28:
  198. case 24: /* USB spec 1.0 says 24 */
  199. *header |= 0x20;
  200. break;
  201. default:
  202. case 12:
  203. *header |= 0x30;
  204. break;
  205. case 5: /* Bug in IrLAP spec? (should be 6) */
  206. case 6:
  207. *header |= 0x40;
  208. break;
  209. case 3:
  210. *header |= 0x50;
  211. break;
  212. case 2:
  213. *header |= 0x60;
  214. break;
  215. case 1:
  216. *header |= 0x70;
  217. break;
  218. case 0:
  219. *header |= 0x80;
  220. break;
  221. }
  222. }
  223. }
  224. /*------------------------------------------------------------------*/
  225. /*
  226.  * Send a command to change the speed of the dongle
  227.  * Need to be called with spinlock on.
  228.  */
  229. static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
  230. {
  231. __u8 *frame;
  232. int ret;
  233. struct urb *purb;
  234. IRDA_DEBUG(2, "%s(), speed=%d, xbofs=%dn",
  235. __FUNCTION__, self->new_speed, self->new_xbofs);
  236. /* Grab the speed URB */
  237. purb = &self->speed_urb;
  238. if (purb->status != USB_ST_NOERROR) {
  239. WARNING("%s(), URB still in use!n", __FUNCTION__);
  240. return;
  241. }
  242. /* Allocate the fake frame */
  243. frame = self->speed_buff;
  244. /* Set the new speed and xbofs in this fake frame */
  245. irda_usb_build_header(self, frame, 1);
  246. /* Submit the 0 length IrDA frame to trigger new speed settings */
  247.         FILL_BULK_URB(purb, self->usbdev,
  248.       usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
  249.                       frame, IRDA_USB_SPEED_MTU,
  250.                       speed_bulk_callback, self);
  251. purb->transfer_buffer_length = USB_IRDA_HEADER;
  252. purb->transfer_flags = USB_QUEUE_BULK | USB_ASYNC_UNLINK;
  253. purb->timeout = MSECS_TO_JIFFIES(100);
  254. if ((ret = usb_submit_urb(purb))) {
  255. WARNING("%s(), failed Speed URBn", __FUNCTION__);
  256. }
  257. }
  258. /*------------------------------------------------------------------*/
  259. /*
  260.  * Note : this function will be called with both speed_urb and empty_urb...
  261.  */
  262. static void speed_bulk_callback(struct urb *purb)
  263. {
  264. struct irda_usb_cb *self = purb->context;
  265. IRDA_DEBUG(2, "%s()n", __FUNCTION__);
  266. /* We should always have a context */
  267. if (self == NULL) {
  268. WARNING("%s(), Bug : self == NULLn", __FUNCTION__);
  269. return;
  270. }
  271. /* Check for timeout and other USB nasties */
  272. if(purb->status != USB_ST_NOERROR) {
  273. /* I get a lot of -ECONNABORTED = -103 here - Jean II */
  274. IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04Xn", __FUNCTION__, purb->status, purb->transfer_flags);
  275. /* Don't do anything here, that might confuse the USB layer.
  276.  * Instead, we will wait for irda_usb_net_timeout(), the
  277.  * network layer watchdog, to fix the situation.
  278.  * Jean II */
  279. /* A reset of the dongle might be welcomed here - Jean II */
  280. return;
  281. }
  282. /* urb is now available */
  283. purb->status = USB_ST_NOERROR;
  284. /* If it was the speed URB, allow the stack to send more packets */
  285. if(purb == &self->speed_urb) {
  286. netif_wake_queue(self->netdev);
  287. }
  288. }
  289. /*------------------------------------------------------------------*/
  290. /*
  291.  * Send an IrDA frame to the USB dongle (for transmission)
  292.  */
  293. static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  294. {
  295. struct irda_usb_cb *self = netdev->priv;
  296. struct urb *purb = &self->tx_urb;
  297. unsigned long flags;
  298. s32 speed;
  299. s16 xbofs;
  300. int res, mtt;
  301. netif_stop_queue(netdev);
  302. /* Protect us from USB callbacks, net watchdog and else. */
  303. spin_lock_irqsave(&self->lock, flags);
  304. /* Check if the device is still there.
  305.  * We need to check self->present under the spinlock because
  306.  * of irda_usb_disconnect() is synchronous - Jean II */
  307. if ((!self) || (!self->present)) {
  308. IRDA_DEBUG(0, "%s(), Device is gone...n", __FUNCTION__);
  309. spin_unlock_irqrestore(&self->lock, flags);
  310. return 1; /* Failed */
  311. }
  312. /* Check if we need to change the number of xbofs */
  313.         xbofs = irda_get_next_xbofs(skb);
  314.         if ((xbofs != self->xbofs) && (xbofs != -1)) {
  315. self->new_xbofs = xbofs;
  316. }
  317.         /* Check if we need to change the speed */
  318. speed = irda_get_next_speed(skb);
  319. if ((speed != self->speed) && (speed != -1)) {
  320. /* Set the desired speed */
  321. self->new_speed = speed;
  322. /* Check for empty frame */
  323. if (!skb->len) {
  324. /* IrLAP send us an empty frame to make us change the
  325.  * speed. Changing speed with the USB adapter is in
  326.  * fact sending an empty frame to the adapter, so we
  327.  * could just let the present function do its job.
  328.  * However, we would wait for min turn time,
  329.  * do an extra memcpy and increment packet counters...
  330.  * Jean II */
  331. irda_usb_change_speed_xbofs(self);
  332. netdev->trans_start = jiffies;
  333. /* Will netif_wake_queue() in callback */
  334. goto drop;
  335. }
  336. }
  337. if (purb->status != USB_ST_NOERROR) {
  338. WARNING("%s(), URB still in use!n", __FUNCTION__);
  339. goto drop;
  340. }
  341. /* Make sure there is room for IrDA-USB header. The actual
  342.  * allocation will be done lower in skb_push().
  343.  * Also, we don't use directly skb_cow(), because it require
  344.  * headroom >= 16, which force unnecessary copies - Jean II */
  345. if (skb_headroom(skb) < USB_IRDA_HEADER) {
  346. IRDA_DEBUG(0, "%s(), Insuficient skb headroom.n", __FUNCTION__);
  347. if (skb_cow(skb, USB_IRDA_HEADER)) {
  348. WARNING("%s(), failed skb_cow() !!!n", __FUNCTION__);
  349. goto drop;
  350. }
  351. }
  352. /* Change setting for next frame */
  353. irda_usb_build_header(self, skb_push(skb, USB_IRDA_HEADER), 0);
  354. /* FIXME: Make macro out of this one */
  355. ((struct irda_skb_cb *)skb->cb)->context = self;
  356.         FILL_BULK_URB(purb, self->usbdev, 
  357.       usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
  358.                       skb->data, IRDA_USB_MAX_MTU,
  359.                       write_bulk_callback, skb);
  360. purb->transfer_buffer_length = skb->len;
  361. /* Note : unlink *must* be Asynchronous because of the code in 
  362.  * irda_usb_net_timeout() -> call in irq - Jean II */
  363. purb->transfer_flags = USB_QUEUE_BULK | USB_ASYNC_UNLINK;
  364. /* This flag (USB_ZERO_PACKET) indicates that what we send is not
  365.  * a continuous stream of data but separate packets.
  366.  * In this case, the USB layer will insert an empty USB frame (TD)
  367.  * after each of our packets that is exact multiple of the frame size.
  368.  * This is how the dongle will detect the end of packet - Jean II */
  369. purb->transfer_flags |= USB_ZERO_PACKET;
  370. /* Timeout need to be shorter than NET watchdog timer */
  371. purb->timeout = MSECS_TO_JIFFIES(200);
  372. /* Generate min turn time. FIXME: can we do better than this? */
  373. /* Trying to a turnaround time at this level is trying to measure
  374.  * processor clock cycle with a wrist-watch, approximate at best...
  375.  *
  376.  * What we know is the last time we received a frame over USB.
  377.  * Due to latency over USB that depend on the USB load, we don't
  378.  * know when this frame was received over IrDA (a few ms before ?)
  379.  * Then, same story for our outgoing frame...
  380.  *
  381.  * In theory, the USB dongle is supposed to handle the turnaround
  382.  * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
  383.  * why this code is enabled only for dongles that doesn't meet
  384.  * the spec.
  385.  * Jean II */
  386. if (self->capability & IUC_NO_TURN) {
  387. mtt = irda_get_mtt(skb);
  388. if (mtt) {
  389. int diff;
  390. do_gettimeofday(&self->now);
  391. diff = self->now.tv_usec - self->stamp.tv_usec;
  392. #ifdef IU_USB_MIN_RTT
  393. /* Factor in USB delays -> Get rid of udelay() that
  394.  * would be lost in the noise - Jean II */
  395. diff += IU_USB_MIN_RTT;
  396. #endif /* IU_USB_MIN_RTT */
  397. if (diff < 0)
  398. diff += 1000000;
  399.         /* Check if the mtt is larger than the time we have
  400.  * already used by all the protocol processing
  401.  */
  402. if (mtt > diff) {
  403. mtt -= diff;
  404. if (mtt > 1000)
  405. mdelay(mtt/1000);
  406. else
  407. udelay(mtt);
  408. }
  409. }
  410. }
  411. /* Ask USB to send the packet */
  412. if ((res = usb_submit_urb(purb))) {
  413. WARNING("%s(), failed Tx URBn", __FUNCTION__);
  414. self->stats.tx_errors++;
  415. /* Let USB recover : We will catch that in the watchdog */
  416. /*netif_start_queue(netdev);*/
  417. } else {
  418. /* Increment packet stats */
  419. self->stats.tx_packets++;
  420.                 self->stats.tx_bytes += skb->len;
  421. netdev->trans_start = jiffies;
  422. }
  423. spin_unlock_irqrestore(&self->lock, flags);
  424. return 0;
  425. drop:
  426. /* Drop silently the skb and exit */
  427. dev_kfree_skb(skb);
  428. spin_unlock_irqrestore(&self->lock, flags);
  429. return 0;
  430. }
  431. /*------------------------------------------------------------------*/
  432. /*
  433.  * Note : this function will be called only for tx_urb...
  434.  */
  435. static void write_bulk_callback(struct urb *purb)
  436. {
  437. unsigned long flags;
  438. struct sk_buff *skb = purb->context;
  439. struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
  440. IRDA_DEBUG(2, "%s()n", __FUNCTION__);
  441. /* We should always have a context */
  442. if (self == NULL) {
  443. WARNING("%s(), Bug : self == NULLn", __FUNCTION__);
  444. return;
  445. }
  446. /* Free up the skb */
  447. dev_kfree_skb_any(skb);
  448. purb->context = NULL;
  449. /* Check for timeout and other USB nasties */
  450. if(purb->status != USB_ST_NOERROR) {
  451. /* I get a lot of -ECONNABORTED = -103 here - Jean II */
  452. IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04Xn", __FUNCTION__, purb->status, purb->transfer_flags);
  453. /* Don't do anything here, that might confuse the USB layer,
  454.  * and we could go in recursion and blow the kernel stack...
  455.  * Instead, we will wait for irda_usb_net_timeout(), the
  456.  * network layer watchdog, to fix the situation.
  457.  * Jean II */
  458. /* A reset of the dongle might be welcomed here - Jean II */
  459. return;
  460. }
  461. /* urb is now available */
  462. //purb->status = USB_ST_NOERROR; -> tested above
  463. /* Make sure we read self->present properly */
  464. spin_lock_irqsave(&self->lock, flags);
  465. /* If the network is closed, stop everything */
  466. if ((!self->netopen) || (!self->present)) {
  467. IRDA_DEBUG(0, "%s(), Network is gone...n", __FUNCTION__);
  468. spin_unlock_irqrestore(&self->lock, flags);
  469. return;
  470. }
  471. /* If we need to change the speed or xbofs, do it now */
  472. if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
  473. IRDA_DEBUG(1, "%s(), Changing speed now...n", __FUNCTION__);
  474. irda_usb_change_speed_xbofs(self);
  475. } else {
  476. /* Otherwise, allow the stack to send more packets */
  477. netif_wake_queue(self->netdev);
  478. }
  479. spin_unlock_irqrestore(&self->lock, flags);
  480. }
  481. /*------------------------------------------------------------------*/
  482. /*
  483.  * Watchdog timer from the network layer.
  484.  * After a predetermined timeout, if we don't give confirmation that
  485.  * the packet has been sent (i.e. no call to netif_wake_queue()),
  486.  * the network layer will call this function.
  487.  * Note that URB that we submit have also a timeout. When the URB timeout
  488.  * expire, the normal URB callback is called (write_bulk_callback()).
  489.  */
  490. static void irda_usb_net_timeout(struct net_device *netdev)
  491. {
  492. unsigned long flags;
  493. struct irda_usb_cb *self = netdev->priv;
  494. struct urb *purb;
  495. int done = 0; /* If we have made any progress */
  496. IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!n", __FUNCTION__);
  497. /* Protect us from USB callbacks, net Tx and else. */
  498. spin_lock_irqsave(&self->lock, flags);
  499. if ((!self) || (!self->present)) {
  500. WARNING("%s(), device not present!n", __FUNCTION__);
  501. netif_stop_queue(netdev);
  502. spin_unlock_irqrestore(&self->lock, flags);
  503. return;
  504. }
  505. /* Check speed URB */
  506. purb = &(self->speed_urb);
  507. if (purb->status != USB_ST_NOERROR) {
  508. IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04Xn", netdev->name, purb->status, purb->transfer_flags);
  509. switch (purb->status) {
  510. case USB_ST_URB_PENDING: /* -EINPROGRESS == -115 */
  511. usb_unlink_urb(purb);
  512. /* Note : above will  *NOT* call netif_wake_queue()
  513.  * in completion handler, we will come back here.
  514.  * Jean II */
  515. done = 1;
  516. break;
  517. case -ECONNABORTED: /* -103 */
  518. case -ECONNRESET: /* -104 */
  519. case -ETIMEDOUT: /* -110 */
  520. case -ENOENT: /* -2 (urb unlinked by us)  */
  521. default: /* ??? - Play safe */
  522. purb->status = USB_ST_NOERROR;
  523. netif_wake_queue(self->netdev);
  524. done = 1;
  525. break;
  526. }
  527. }
  528. /* Check Tx URB */
  529. purb = &(self->tx_urb);
  530. if (purb->status != USB_ST_NOERROR) {
  531. struct sk_buff *skb = purb->context;
  532. IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04Xn", netdev->name, purb->status, purb->transfer_flags);
  533. /* Increase error count */
  534. self->stats.tx_errors++;
  535. #ifdef IU_BUG_KICK_TIMEOUT
  536. /* Can't be a bad idea to reset the speed ;-) - Jean II */
  537. if(self->new_speed == -1)
  538. self->new_speed = self->speed;
  539. if(self->new_xbofs == -1)
  540. self->new_xbofs = self->xbofs;
  541. irda_usb_change_speed_xbofs(self);
  542. #endif /* IU_BUG_KICK_TIMEOUT */
  543. switch (purb->status) {
  544. case USB_ST_URB_PENDING: /* -EINPROGRESS == -115 */
  545. usb_unlink_urb(purb);
  546. /* Note : above will  *NOT* call netif_wake_queue()
  547.  * in completion handler, because purb->status will
  548.  * be -ENOENT. We will fix that at the next watchdog,
  549.  * leaving more time to USB to recover...
  550.  * Also, we are in interrupt, so we need to have
  551.  * USB_ASYNC_UNLINK to work properly...
  552.  * Jean II */
  553. done = 1;
  554. break;
  555. case -ECONNABORTED: /* -103 */
  556. case -ECONNRESET: /* -104 */
  557. case -ETIMEDOUT: /* -110 */
  558. case -ENOENT: /* -2 (urb unlinked by us)  */
  559. default: /* ??? - Play safe */
  560. if(skb != NULL) {
  561. dev_kfree_skb_any(skb);
  562. purb->context = NULL;
  563. }
  564. purb->status = USB_ST_NOERROR;
  565. netif_wake_queue(self->netdev);
  566. done = 1;
  567. break;
  568. }
  569. }
  570. spin_unlock_irqrestore(&self->lock, flags);
  571. /* Maybe we need a reset */
  572. /* Note : Some drivers seem to use a usb_set_interface() when they
  573.  * need to reset the hardware. Hum...
  574.  */
  575. /* if(done == 0) */
  576. }
  577. /************************* RECEIVE ROUTINES *************************/
  578. /*
  579.  * Receive packets from the USB layer stack and pass them to the IrDA stack.
  580.  * Try to work around USB failures...
  581.  */
  582. /*
  583.  * Note :
  584.  * Some of you may have noticed that most dongle have an interrupt in pipe
  585.  * that we don't use. Here is the little secret...
  586.  * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
  587.  * in every USB frame. This is unnecessary overhead.
  588.  * The interrupt in pipe will generate an event every time a packet is
  589.  * received. Reading an interrupt pipe adds minimal overhead, but has some
  590.  * latency (~1ms).
  591.  * If we are connected (speed != 9600), we want to minimise latency, so
  592.  * we just always hang the Rx URB and ignore the interrupt.
  593.  * If we are not connected (speed == 9600), there is usually no Rx traffic,
  594.  * and we want to minimise the USB overhead. In this case we should wait
  595.  * on the interrupt pipe and hang the Rx URB only when an interrupt is
  596.  * received.
  597.  * Jean II
  598.  */
  599. /*------------------------------------------------------------------*/
  600. /*
  601.  * Submit a Rx URB to the USB layer to handle reception of a frame
  602.  *
  603.  * Important note :
  604.  * The function process_urb() in usb-uhci.c contains the following code :
  605.  * > urb->complete ((struct urb *) urb);
  606.  * > // Re-submit the URB if ring-linked
  607.  * > if (is_ring && (urb->status != -ENOENT) && !contains_killed) {
  608.  * > urb->dev=usb_dev;
  609.  * > uhci_submit_urb (urb);
  610.  * > }
  611.  * The way I see it is that if we submit more than one Rx URB at a
  612.  * time, the Rx URB can be automatically re-submitted after the
  613.  * completion handler is called.
  614.  * We make sure to disable this feature by setting urb->next to NULL
  615.  *
  616.  * My take is that it's a questionable feature, and quite difficult
  617.  * to control and to make work effectively.
  618.  * The outcome (re-submited or not) depend on various complex
  619.  * test ('is_ring' and 'contains_killed'), and the completion handler
  620.  * don't have this information, so basically the driver has no way
  621.  * to know if URB are resubmitted or not. Yuck !
  622.  * If everything is perfect, it's cool, but the problem is when
  623.  * an URB is killed (timeout, call to unlink_urb(), ...), things get
  624.  * messy...
  625.  * The other problem is that this scheme deal only with the URB
  626.  * and ignore everything about the associated buffer. So, it would
  627.  * resubmit URB even if the buffer is still in use or non-existent.
  628.  * On the other hand, submitting ourself in the completion callback
  629.  * is quite trivial and work well (this function).
  630.  * Moreover, this scheme doesn't allow to have an idle URB, which is
  631.  * necessary to overcome some URB failures.
  632.  *
  633.  * Jean II
  634.  */
  635. static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *purb)
  636. {
  637. struct irda_skb_cb *cb;
  638. int ret;
  639. IRDA_DEBUG(2, "%s()n", __FUNCTION__);
  640. /* Check that we have an urb */
  641. if (!purb) {
  642. WARNING("%s(), Bug : purb == NULLn", __FUNCTION__);
  643. return;
  644. }
  645. /* Allocate new skb if it has not been recycled */
  646. if (!skb) {
  647. skb = dev_alloc_skb(IRDA_USB_MAX_MTU + 1);
  648. if (!skb) {
  649. /* If this ever happen, we are in deep s***.
  650.  * Basically, the Rx path will stop... */
  651. WARNING("%s(), Failed to allocate Rx skbn", __FUNCTION__);
  652. return;
  653. }
  654. } else  {
  655. /* Reset recycled skb */
  656. skb->data = skb->tail = skb->head;
  657. skb->len = 0;
  658. }
  659. /* Make sure IP header get aligned (IrDA header is 5 bytes ) */
  660. skb_reserve(skb, 1);
  661. /* Save ourselves */
  662. cb = (struct irda_skb_cb *) skb->cb;
  663. cb->context = self;
  664. /* Reinitialize URB */
  665. FILL_BULK_URB(purb, self->usbdev, 
  666.       usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep), 
  667.       skb->data, skb->truesize,
  668.                       irda_usb_receive, skb);
  669. purb->transfer_flags = USB_QUEUE_BULK;
  670. /* Note : unlink *must* be synchronous because of the code in 
  671.  * irda_usb_net_close() -> free the skb - Jean II */
  672. purb->status = USB_ST_NOERROR;
  673. purb->next = NULL; /* Don't auto resubmit URBs */
  674. ret = usb_submit_urb(purb);
  675. if (ret) {
  676. /* If this ever happen, we are in deep s***.
  677.  * Basically, the Rx path will stop... */
  678. WARNING("%s(), Failed to submit Rx URB %dn", __FUNCTION__, ret);
  679. }
  680. }
  681. /*------------------------------------------------------------------*/
  682. /*
  683.  * Function irda_usb_receive(purb)
  684.  *
  685.  *     Called by the USB subsystem when a frame has been received
  686.  *
  687.  */
  688. static void irda_usb_receive(struct urb *purb) 
  689. {
  690. struct sk_buff *skb = (struct sk_buff *) purb->context;
  691. struct irda_usb_cb *self; 
  692. struct irda_skb_cb *cb;
  693. struct sk_buff *new;
  694. IRDA_DEBUG(2, "%s(), len=%dn", __FUNCTION__, purb->actual_length);
  695. /* Find ourselves */
  696. cb = (struct irda_skb_cb *) skb->cb;
  697. ASSERT(cb != NULL, return;);
  698. self = (struct irda_usb_cb *) cb->context;
  699. ASSERT(self != NULL, return;);
  700. /* If the network is closed or the device gone, stop everything */
  701. if ((!self->netopen) || (!self->present)) {
  702. IRDA_DEBUG(0, "%s(), Network is gone!n", __FUNCTION__);
  703. /* Don't re-submit the URB : will stall the Rx path */
  704. return;
  705. }
  706. /* Check the status */
  707. if(purb->status != USB_ST_NOERROR) {
  708. switch (purb->status) {
  709. case USB_ST_CRC: /* -EILSEQ */
  710. self->stats.rx_errors++;
  711. self->stats.rx_crc_errors++;
  712. break;
  713. case -ECONNRESET: /* -104 */
  714. IRDA_DEBUG(0, "%s(), Connection Reset (-104), transfer_flags 0x%04X n", __FUNCTION__, purb->transfer_flags);
  715. /* uhci_cleanup_unlink() is going to kill the Rx
  716.  * URB just after we return. No problem, at this
  717.  * point the URB will be idle ;-) - Jean II */
  718. break;
  719. default:
  720. IRDA_DEBUG(0, "%s(), RX status %d,transfer_flags 0x%04X n", __FUNCTION__, purb->status, purb->transfer_flags);
  721. break;
  722. }
  723. goto done;
  724. }
  725. /* Check for empty frames */
  726. if (purb->actual_length <= USB_IRDA_HEADER) {
  727. WARNING("%s(), empty frame!n", __FUNCTION__);
  728. goto done;
  729. }
  730. /*  
  731.  * Remember the time we received this frame, so we can
  732.  * reduce the min turn time a bit since we will know
  733.  * how much time we have used for protocol processing
  734.  */
  735.         do_gettimeofday(&self->stamp);
  736. /* Fix skb, and remove USB-IrDA header */
  737. skb_put(skb, purb->actual_length);
  738. skb_pull(skb, USB_IRDA_HEADER);
  739. /* Don't waste a lot of memory on small IrDA frames */
  740. if (skb->len < RX_COPY_THRESHOLD) {
  741. new = dev_alloc_skb(skb->len+1);
  742. if (!new) {
  743. self->stats.rx_dropped++;
  744. goto done;  
  745. }
  746. /* Make sure IP header get aligned (IrDA header is 5 bytes) */
  747. skb_reserve(new, 1);
  748. /* Copy packet, so we can recycle the original */
  749. memcpy(skb_put(new, skb->len), skb->data, skb->len);
  750. /* We will cleanup the skb in irda_usb_submit() */
  751. } else {
  752. /* Deliver the original skb */
  753. new = skb;
  754. skb = NULL;
  755. }
  756. self->stats.rx_bytes += new->len;
  757. self->stats.rx_packets++;
  758. /* Ask the networking layer to queue the packet for the IrDA stack */
  759.         new->dev = self->netdev;
  760.         new->mac.raw  = new->data;
  761.         new->protocol = htons(ETH_P_IRDA);
  762.         netif_rx(new);
  763. done:
  764. /* Note : at this point, the URB we've just received (purb)
  765.  * is still referenced by the USB layer. For example, if we
  766.  * have received a -ECONNRESET, uhci_cleanup_unlink() will
  767.  * continue to process it (in fact, cleaning it up).
  768.  * If we were to submit this URB, disaster would ensue.
  769.  * Therefore, we submit our idle URB, and put this URB in our
  770.  * idle slot....
  771.  * Jean II */
  772. /* Note : with this scheme, we could submit the idle URB before
  773.  * processing the Rx URB. Another time... Jean II */
  774. /* Submit the idle URB to replace the URB we've just received */
  775. irda_usb_submit(self, skb, self->idle_rx_urb);
  776. /* Recycle Rx URB : Now, the idle URB is the present one */
  777. purb->context = NULL;
  778. self->idle_rx_urb = purb;
  779. }
  780. /*------------------------------------------------------------------*/
  781. /*
  782.  * Callbak from IrDA layer. IrDA wants to know if we have
  783.  * started receiving anything.
  784.  */
  785. static int irda_usb_is_receiving(struct irda_usb_cb *self)
  786. {
  787. /* Note : because of the way UHCI works, it's almost impossible
  788.  * to get this info. The Controller DMA directly to memory and
  789.  * signal only when the whole frame is finished. To know if the
  790.  * first TD of the URB has been filled or not seems hard work...
  791.  *
  792.  * The other solution would be to use the "receiving" command
  793.  * on the default decriptor with a usb_control_msg(), but that
  794.  * would add USB traffic and would return result only in the
  795.  * next USB frame (~1ms).
  796.  *
  797.  * I've been told that current dongles send status info on their
  798.  * interrupt endpoint, and that's what the Windows driver uses
  799.  * to know this info. Unfortunately, this is not yet in the spec...
  800.  *
  801.  * Jean II
  802.  */
  803. return 0; /* For now */
  804. }
  805. /********************** IRDA DEVICE CALLBACKS **********************/
  806. /*
  807.  * Main calls from the IrDA/Network subsystem.
  808.  * Mostly registering a new irda-usb device and removing it....
  809.  * We only deal with the IrDA side of the business, the USB side will
  810.  * be dealt with below...
  811.  */
  812. /*------------------------------------------------------------------*/
  813. /*
  814.  * Callback when a new IrDA device is created.
  815.  */
  816. static int irda_usb_net_init(struct net_device *dev)
  817. {
  818. IRDA_DEBUG(1, "%s()n", __FUNCTION__);
  819. /* Set up to be a normal IrDA network device driver */
  820. irda_device_setup(dev);
  821. /* Insert overrides below this line! */
  822. return 0;
  823. }
  824. /*------------------------------------------------------------------*/
  825. /*
  826.  * Function irda_usb_net_open (dev)
  827.  *
  828.  *    Network device is taken up. Usually this is done by "ifconfig irda0 up" 
  829.  *   
  830.  * Note : don't mess with self->netopen - Jean II
  831.  */
  832. static int irda_usb_net_open(struct net_device *netdev)
  833. {
  834. struct irda_usb_cb *self;
  835. char hwname[16];
  836. int i;
  837. IRDA_DEBUG(1, "%s()n", __FUNCTION__);
  838. ASSERT(netdev != NULL, return -1;);
  839. self = (struct irda_usb_cb *) netdev->priv;
  840. ASSERT(self != NULL, return -1;);
  841. /* Can only open the device if it's there */
  842. if(!self->present) {
  843. WARNING("%s(), device not present!n", __FUNCTION__);
  844. return -1;
  845. }
  846. /* Initialise default speed and xbofs value
  847.  * (IrLAP will change that soon) */
  848. self->speed = -1;
  849. self->xbofs = -1;
  850. self->new_speed = -1;
  851. self->new_xbofs = -1;
  852. /* To do *before* submitting Rx urbs and starting net Tx queue
  853.  * Jean II */
  854. self->netopen = 1;
  855. /* 
  856.  * Now that everything should be initialized properly,
  857.  * Open new IrLAP layer instance to take care of us...
  858.  * Note : will send immediately a speed change...
  859.  */
  860. sprintf(hwname, "usb#%d", self->usbdev->devnum);
  861. self->irlap = irlap_open(netdev, &self->qos, hwname);
  862. ASSERT(self->irlap != NULL, return -1;);
  863. /* Allow IrLAP to send data to us */
  864. netif_start_queue(netdev);
  865. /* We submit all the Rx URB except for one that we keep idle.
  866.  * Need to be initialised before submitting other USBs, because
  867.  * in some cases as soon as we submit the URBs the USB layer
  868.  * will trigger a dummy receive - Jean II */
  869. self->idle_rx_urb = &(self->rx_urb[IU_MAX_ACTIVE_RX_URBS]);
  870. self->idle_rx_urb->context = NULL;
  871. /* Now that we can pass data to IrLAP, allow the USB layer
  872.  * to send us some data... */
  873. for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++)
  874. irda_usb_submit(self, NULL, &(self->rx_urb[i]));
  875. /* Ready to play !!! */
  876. MOD_INC_USE_COUNT;
  877. return 0;
  878. }
  879. /*------------------------------------------------------------------*/
  880. /*
  881.  * Function irda_usb_net_close (self)
  882.  *
  883.  *    Network device is taken down. Usually this is done by 
  884.  *    "ifconfig irda0 down" 
  885.  */
  886. static int irda_usb_net_close(struct net_device *netdev)
  887. {
  888. struct irda_usb_cb *self;
  889. int i;
  890. IRDA_DEBUG(1, "%s()n", __FUNCTION__);
  891. ASSERT(netdev != NULL, return -1;);
  892. self = (struct irda_usb_cb *) netdev->priv;
  893. ASSERT(self != NULL, return -1;);
  894. /* Clear this flag *before* unlinking the urbs and *before*
  895.  * stopping the network Tx queue - Jean II */
  896. self->netopen = 0;
  897. /* Stop network Tx queue */
  898. netif_stop_queue(netdev);
  899. /* Deallocate all the Rx path buffers (URBs and skb) */
  900. for (i = 0; i < IU_MAX_RX_URBS; i++) {
  901. struct urb *purb = &(self->rx_urb[i]);
  902. struct sk_buff *skb = (struct sk_buff *) purb->context;
  903. /* Cancel the receive command */
  904. usb_unlink_urb(purb);
  905. /* The skb is ours, free it */
  906. if(skb) {
  907. dev_kfree_skb(skb);
  908. purb->context = NULL;
  909. }
  910. }
  911. /* Cancel Tx and speed URB - need to be synchronous to avoid races */
  912. self->tx_urb.transfer_flags &= ~USB_ASYNC_UNLINK;
  913. usb_unlink_urb(&(self->tx_urb));
  914. self->speed_urb.transfer_flags &= ~USB_ASYNC_UNLINK;
  915. usb_unlink_urb(&(self->speed_urb));
  916. /* Stop and remove instance of IrLAP */
  917. if (self->irlap)
  918. irlap_close(self->irlap);
  919. self->irlap = NULL;
  920. MOD_DEC_USE_COUNT;
  921. return 0;
  922. }
  923. /*------------------------------------------------------------------*/
  924. /*
  925.  * IOCTLs : Extra out-of-band network commands...
  926.  */
  927. static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  928. {
  929. unsigned long flags;
  930. struct if_irda_req *irq = (struct if_irda_req *) rq;
  931. struct irda_usb_cb *self;
  932. int ret = 0;
  933. ASSERT(dev != NULL, return -1;);
  934. self = dev->priv;
  935. ASSERT(self != NULL, return -1;);
  936. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)n", __FUNCTION__, dev->name, cmd);
  937. switch (cmd) {
  938. case SIOCSBANDWIDTH: /* Set bandwidth */
  939. if (!capable(CAP_NET_ADMIN))
  940. return -EPERM;
  941. /* Protect us from USB callbacks, net watchdog and else. */
  942. spin_lock_irqsave(&self->lock, flags);
  943. /* Check if the device is still there */
  944. if(self->present) {
  945. /* Set the desired speed */
  946. self->new_speed = irq->ifr_baudrate;
  947. irda_usb_change_speed_xbofs(self);
  948. }
  949. spin_unlock_irqrestore(&self->lock, flags);
  950. break;
  951. case SIOCSMEDIABUSY: /* Set media busy */
  952. if (!capable(CAP_NET_ADMIN))
  953. return -EPERM;
  954. /* Check if the IrDA stack is still there */
  955. if(self->netopen)
  956. irda_device_set_media_busy(self->netdev, TRUE);
  957. break;
  958. case SIOCGRECEIVING: /* Check if we are receiving right now */
  959. irq->ifr_receiving = irda_usb_is_receiving(self);
  960. break;
  961. default:
  962. ret = -EOPNOTSUPP;
  963. }
  964. return ret;
  965. }
  966. /*------------------------------------------------------------------*/
  967. /*
  968.  * Get device stats (for /proc/net/dev and ifconfig)
  969.  */
  970. static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
  971. {
  972. struct irda_usb_cb *self = dev->priv;
  973. return &self->stats;
  974. }
  975. /********************* IRDA CONFIG SUBROUTINES *********************/
  976. /*
  977.  * Various subroutines dealing with IrDA and network stuff we use to
  978.  * configure and initialise each irda-usb instance.
  979.  * These functions are used below in the main calls of the driver...
  980.  */
  981. /*------------------------------------------------------------------*/
  982. /*
  983.  * Set proper values in the IrDA QOS structure
  984.  */
  985. static inline void irda_usb_init_qos(struct irda_usb_cb *self)
  986. {
  987. struct irda_class_desc *desc;
  988. IRDA_DEBUG(3, "%s()n", __FUNCTION__);
  989. desc = self->irda_desc;
  990. /* Initialize QoS for this device */
  991. irda_init_max_qos_capabilies(&self->qos);
  992. self->qos.baud_rate.bits       = desc->wBaudRate;
  993. self->qos.min_turn_time.bits   = desc->bmMinTurnaroundTime;
  994. self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
  995. self->qos.window_size.bits     = desc->bmWindowSize;
  996. self->qos.data_size.bits       = desc->bmDataSize;
  997. IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%Xn", 
  998. __FUNCTION__, self->qos.baud_rate.bits, self->qos.data_size.bits, self->qos.window_size.bits, self->qos.additional_bofs.bits, self->qos.min_turn_time.bits);
  999. /* Don't always trust what the dongle tell us */
  1000. if(self->capability & IUC_SIR_ONLY)
  1001. self->qos.baud_rate.bits &= 0xff;
  1002. if(self->capability & IUC_SMALL_PKT)
  1003. self->qos.data_size.bits  = 0x07;
  1004. if(self->capability & IUC_NO_WINDOW)
  1005. self->qos.window_size.bits  = 0x01;
  1006. if(self->capability & IUC_MAX_WINDOW)
  1007. self->qos.window_size.bits  = 0x7f;
  1008. if(self->capability & IUC_MAX_XBOFS)
  1009. self->qos.additional_bofs.bits  = 0x01;
  1010. #if 1
  1011. /* Module parameter can override the rx window size */
  1012. if (qos_mtt_bits)
  1013. self->qos.min_turn_time.bits = qos_mtt_bits;
  1014. #endif     
  1015. /* 
  1016.  * Note : most of those values apply only for the receive path,
  1017.  * the transmit path will be set differently - Jean II 
  1018.  */
  1019. irda_qos_bits_to_value(&self->qos);
  1020. self->flags |= IFF_SIR;
  1021. if (self->qos.baud_rate.value > 115200)
  1022. self->flags |= IFF_MIR;
  1023. if (self->qos.baud_rate.value > 1152000)
  1024. self->flags |= IFF_FIR;
  1025. if (self->qos.baud_rate.value > 4000000)
  1026. self->flags |= IFF_VFIR;
  1027. }
  1028. /*------------------------------------------------------------------*/
  1029. /*
  1030.  * Initialise the network side of the irda-usb instance
  1031.  * Called when a new USB instance is registered in irda_usb_probe()
  1032.  */
  1033. static inline int irda_usb_open(struct irda_usb_cb *self)
  1034. {
  1035. struct net_device *netdev;
  1036. int err;
  1037. IRDA_DEBUG(1, "%s()n", __FUNCTION__);
  1038. spin_lock_init(&self->lock);
  1039. irda_usb_init_qos(self);
  1040. /* Initialise list of skb beeing curently transmitted */
  1041. self->tx_list = hashbin_new(HB_GLOBAL);
  1042. /* Allocate the buffer for speed changes */
  1043. /* Don't change this buffer size and allocation without doing
  1044.  * some heavy and complete testing. Don't ask why :-(
  1045.  * Jean II */
  1046. self->speed_buff = (char *) kmalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
  1047. if (self->speed_buff == NULL) 
  1048. return -1;
  1049. memset(self->speed_buff, 0, IRDA_USB_SPEED_MTU);
  1050. /* Create a network device for us */
  1051. if (!(netdev = dev_alloc("irda%d", &err))) {
  1052. ERROR("%s(), dev_alloc() failed!n", __FUNCTION__);
  1053. return -1;
  1054. }
  1055. self->netdev = netdev;
  1056.   netdev->priv = (void *) self;
  1057. /* Override the network functions we need to use */
  1058. netdev->init            = irda_usb_net_init;
  1059. netdev->hard_start_xmit = irda_usb_hard_xmit;
  1060. netdev->tx_timeout = irda_usb_net_timeout;
  1061. netdev->watchdog_timeo  = 250*HZ/1000; /* 250 ms > USB timeout */
  1062. netdev->open            = irda_usb_net_open;
  1063. netdev->stop            = irda_usb_net_close;
  1064. netdev->get_stats = irda_usb_net_get_stats;
  1065. netdev->do_ioctl        = irda_usb_net_ioctl;
  1066. rtnl_lock();
  1067. err = register_netdevice(netdev);
  1068. rtnl_unlock();
  1069. if (err) {
  1070. ERROR("%s(), register_netdev() failed!n", __FUNCTION__);
  1071. return -1;
  1072. }
  1073. MESSAGE("IrDA: Registered device %sn", netdev->name);
  1074. return 0;
  1075. }
  1076. /*------------------------------------------------------------------*/
  1077. /*
  1078.  * Cleanup the network side of the irda-usb instance
  1079.  * Called when a USB instance is removed in irda_usb_disconnect()
  1080.  */
  1081. static inline int irda_usb_close(struct irda_usb_cb *self)
  1082. {
  1083. IRDA_DEBUG(1, "%s()n", __FUNCTION__);
  1084. ASSERT(self != NULL, return -1;);
  1085. /* Remove netdevice */
  1086. if (self->netdev) {
  1087. rtnl_lock();
  1088. unregister_netdevice(self->netdev);
  1089. self->netdev = NULL;
  1090. rtnl_unlock();
  1091. }
  1092. /* Delete all pending skbs */
  1093. hashbin_delete(self->tx_list, (FREE_FUNC) &dev_kfree_skb_any);
  1094. /* Remove the speed buffer */
  1095. if (self->speed_buff != NULL) {
  1096. kfree(self->speed_buff);
  1097. self->speed_buff = NULL;
  1098. }
  1099. return 0;
  1100. }
  1101. /********************** USB CONFIG SUBROUTINES **********************/
  1102. /*
  1103.  * Various subroutines dealing with USB stuff we use to configure and
  1104.  * initialise each irda-usb instance.
  1105.  * These functions are used below in the main calls of the driver...
  1106.  */
  1107. /*------------------------------------------------------------------*/
  1108. /*
  1109.  * Function irda_usb_parse_endpoints(dev, ifnum)
  1110.  *
  1111.  *    Parse the various endpoints and find the one we need.
  1112.  *
  1113.  * The endpoint are the pipes used to communicate with the USB device.
  1114.  * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
  1115.  * These are used to pass frames back and forth with the dongle.
  1116.  * Most dongle have also an interrupt endpoint, that will be probably
  1117.  * documented in the next spec...
  1118.  */
  1119. static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_endpoint_descriptor *endpoint, int ennum)
  1120. {
  1121. int i; /* Endpoint index in table */
  1122. /* Init : no endpoints */
  1123. self->bulk_in_ep = 0;
  1124. self->bulk_out_ep = 0;
  1125. self->bulk_int_ep = 0;
  1126. /* Let's look at all those endpoints */
  1127. for(i = 0; i < ennum; i++) {
  1128. /* All those variables will get optimised by the compiler,
  1129.  * so let's aim for clarity... - Jean II */
  1130. __u8 ep; /* Endpoint address */
  1131. __u8 dir; /* Endpoint direction */
  1132. __u8 attr; /* Endpoint attribute */
  1133. __u16 psize; /* Endpoint max packet size in bytes */
  1134. /* Get endpoint address, direction and attribute */
  1135. ep = endpoint[i].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1136. dir = endpoint[i].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
  1137. attr = endpoint[i].bmAttributes;
  1138. psize = endpoint[i].wMaxPacketSize;
  1139. /* Is it a bulk endpoint ??? */
  1140. if(attr == USB_ENDPOINT_XFER_BULK) {
  1141. /* We need to find an IN and an OUT */
  1142. if(dir == USB_DIR_IN) {
  1143. /* This is our Rx endpoint */
  1144. self->bulk_in_ep = ep;
  1145. } else {
  1146. /* This is our Tx endpoint */
  1147. self->bulk_out_ep = ep;
  1148. self->bulk_out_mtu = psize;
  1149. }
  1150. } else {
  1151. if((attr == USB_ENDPOINT_XFER_INT) &&
  1152.    (dir == USB_DIR_IN)) {
  1153. /* This is our interrupt endpoint */
  1154. self->bulk_int_ep = ep;
  1155. } else {
  1156. ERROR("%s(), Unrecognised endpoint %02X.n", __FUNCTION__, ep);
  1157. }
  1158. }
  1159. }
  1160. IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02Xn",
  1161. __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
  1162. /* Should be 8, 16, 32 or 64 bytes */
  1163. ASSERT(self->bulk_out_mtu == 64, ;);
  1164. return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
  1165. }
  1166. #ifdef IU_DUMP_CLASS_DESC
  1167. /*------------------------------------------------------------------*/
  1168. /*
  1169.  * Function usb_irda_dump_class_desc(desc)
  1170.  *
  1171.  *    Prints out the contents of the IrDA class descriptor
  1172.  *
  1173.  */
  1174. static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
  1175. {
  1176. printk("bLength=%xn", desc->bLength);
  1177. printk("bDescriptorType=%xn", desc->bDescriptorType);
  1178. printk("bcdSpecRevision=%xn", desc->bcdSpecRevision); 
  1179. printk("bmDataSize=%xn", desc->bmDataSize);
  1180. printk("bmWindowSize=%xn", desc->bmWindowSize);
  1181. printk("bmMinTurnaroundTime=%dn", desc->bmMinTurnaroundTime);
  1182. printk("wBaudRate=%xn", desc->wBaudRate);
  1183. printk("bmAdditionalBOFs=%xn", desc->bmAdditionalBOFs);
  1184. printk("bIrdaRateSniff=%xn", desc->bIrdaRateSniff);
  1185. printk("bMaxUnicastList=%xn", desc->bMaxUnicastList);
  1186. }
  1187. #endif /* IU_DUMP_CLASS_DESC */
  1188. /*------------------------------------------------------------------*/
  1189. /*
  1190.  * Function irda_usb_find_class_desc(dev, ifnum)
  1191.  *
  1192.  *    Returns instance of IrDA class descriptor, or NULL if not found
  1193.  *
  1194.  * The class descriptor is some extra info that IrDA USB devices will
  1195.  * offer to us, describing their IrDA characteristics. We will use that in
  1196.  * irda_usb_init_qos()
  1197.  */
  1198. static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  1199. {
  1200. struct irda_class_desc *desc;
  1201. int ret;
  1202. desc = kmalloc(sizeof (*desc), GFP_KERNEL);
  1203. if (desc == NULL) 
  1204. return NULL;
  1205. memset(desc, 0, sizeof(*desc));
  1206. /* USB-IrDA class spec 1.0:
  1207.  * 6.1.3: Standard "Get Descriptor" Device Request is not
  1208.  *        appropriate to retrieve class-specific descriptor
  1209.  * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
  1210.  *        is mandatory and returns the USB-IrDA class descriptor
  1211.  */
  1212. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
  1213. IU_REQ_GET_CLASS_DESC,
  1214. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1215. 0, ifnum, desc, sizeof(*desc), MSECS_TO_JIFFIES(500));
  1216. IRDA_DEBUG(1, "%s(), ret=%dn", __FUNCTION__, ret);
  1217. if (ret < sizeof(*desc)) {
  1218. WARNING("usb-irda: class_descriptor read %s (%d)n",
  1219. (ret<0) ? "failed" : "too short", ret);
  1220. }
  1221. else if (desc->bDescriptorType != USB_DT_IRDA) {
  1222. WARNING("usb-irda: bad class_descriptor typen");
  1223. }
  1224. else {
  1225. #ifdef IU_DUMP_CLASS_DESC
  1226. irda_usb_dump_class_desc(desc);
  1227. #endif /* IU_DUMP_CLASS_DESC */
  1228. return desc;
  1229. }
  1230. kfree(desc);
  1231. return NULL;
  1232. }
  1233. /*********************** USB DEVICE CALLBACKS ***********************/
  1234. /*
  1235.  * Main calls from the USB subsystem.
  1236.  * Mostly registering a new irda-usb device and removing it....
  1237.  */
  1238. /*------------------------------------------------------------------*/
  1239. /*
  1240.  * This routine is called by the USB subsystem for each new device
  1241.  * in the system. We need to check if the device is ours, and in
  1242.  * this case start handling it.
  1243.  * Note : it might be worth protecting this function by a global
  1244.  * spinlock... Or not, because maybe USB already deal with that...
  1245.  */
  1246. static void *irda_usb_probe(struct usb_device *dev, unsigned int ifnum,
  1247.     const struct usb_device_id *id)
  1248. {
  1249. struct irda_usb_cb *self = NULL;
  1250. struct usb_interface_descriptor *interface;
  1251. struct irda_class_desc *irda_desc;
  1252. int ret;
  1253. int i;
  1254. /* Note : the probe make sure to call us only for devices that
  1255.  * matches the list of dongle (top of the file). So, we
  1256.  * don't need to check if the dongle is really ours.
  1257.  * Jean II */
  1258. MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %xn",
  1259. dev->devnum, dev->descriptor.idVendor,
  1260. dev->descriptor.idProduct);
  1261. /* Try to cleanup all instance that have a pending disconnect
  1262.  * In theory, it can't happen any longer.
  1263.  * Jean II */
  1264. for (i = 0; i < NIRUSB; i++) {
  1265. struct irda_usb_cb *irda = &irda_instance[i];
  1266. if((irda->usbdev != NULL) &&
  1267.    (irda->present == 0) &&
  1268.    (irda->netopen == 0)) {
  1269. IRDA_DEBUG(0, "%s(), found a zombie instance !!!n", __FUNCTION__);
  1270. irda_usb_disconnect(irda->usbdev, (void *) irda);
  1271. }
  1272. }
  1273. /* Find an free instance to handle this new device... */
  1274. self = NULL;
  1275. for (i = 0; i < NIRUSB; i++) {
  1276. if(irda_instance[i].usbdev == NULL) {
  1277. self = &irda_instance[i];
  1278. break;
  1279. }
  1280. }
  1281. if(self == NULL) {
  1282. WARNING("Too many USB IrDA devices !!! (max = %d)n",
  1283.    NIRUSB);
  1284. return NULL;
  1285. }
  1286. /* Reset the instance */
  1287. self->present = 0;
  1288. self->netopen = 0;
  1289.        /* Is this really necessary? */
  1290. if (usb_set_configuration (dev, dev->config[0].bConfigurationValue) < 0) {
  1291. err("set_configuration failed");
  1292. return NULL;
  1293. }
  1294. /* Is this really necessary? */
  1295. /* Note : some driver do hardcode the interface number, some others
  1296.  * specify an alternate, but very few driver do like this.
  1297.  * Jean II */
  1298. ret = usb_set_interface(dev, ifnum, 0);
  1299. IRDA_DEBUG(1, "usb-irda: set interface %d result %dn", ifnum, ret);
  1300. switch (ret) {
  1301. case USB_ST_NOERROR: /* 0 */
  1302. break;
  1303. case USB_ST_STALL: /* -EPIPE = -32 */
  1304. usb_clear_halt(dev, usb_sndctrlpipe(dev, 0));
  1305. IRDA_DEBUG(0, "%s(), Clearing stall on control interfacen", __FUNCTION__);
  1306. break;
  1307. default:
  1308. IRDA_DEBUG(0, "%s(), Unknown error %dn", __FUNCTION__, ret);
  1309. return NULL;
  1310. break;
  1311. }
  1312. /* Find our endpoints */
  1313. interface = &dev->actconfig->interface[ifnum].altsetting[0];
  1314. if(!irda_usb_parse_endpoints(self, interface->endpoint,
  1315.      interface->bNumEndpoints)) {
  1316. ERROR("%s(), Bogus endpoints...n", __FUNCTION__);
  1317. return NULL;
  1318. }
  1319. /* Find IrDA class descriptor */
  1320. irda_desc = irda_usb_find_class_desc(dev, ifnum);
  1321. if (irda_desc == NULL)
  1322. return NULL;
  1323. self->irda_desc =  irda_desc;
  1324. self->present = 1;
  1325. self->netopen = 0;
  1326. self->capability = id->driver_info;
  1327. self->usbdev = dev;
  1328. ret = irda_usb_open(self);
  1329. if (ret)
  1330. return NULL;
  1331. return self;
  1332. }
  1333. /*------------------------------------------------------------------*/
  1334. /*
  1335.  * The current irda-usb device is removed, the USB layer tell us
  1336.  * to shut it down...
  1337.  * One of the constraints is that when we exit this function,
  1338.  * we cannot use the usb_device no more. Gone. Destroyed. kfree().
  1339.  * Most other subsystem allow you to destroy the instance at a time
  1340.  * when it's convenient to you, to postpone it to a later date, but
  1341.  * not the USB subsystem.
  1342.  * So, we must make bloody sure that everything gets deactivated.
  1343.  * Jean II
  1344.  */
  1345. static void irda_usb_disconnect(struct usb_device *dev, void *ptr)
  1346. {
  1347. unsigned long flags;
  1348. struct irda_usb_cb *self = (struct irda_usb_cb *) ptr;
  1349. int i;
  1350. IRDA_DEBUG(1, "%s()n", __FUNCTION__);
  1351. /* Make sure that the Tx path is not executing. - Jean II */
  1352. spin_lock_irqsave(&self->lock, flags);
  1353. /* Oups ! We are not there any more.
  1354.  * This will stop/desactivate the Tx path. - Jean II */
  1355. self->present = 0;
  1356. /* We need to have irq enabled to unlink the URBs. That's OK,
  1357.  * at this point the Tx path is gone - Jean II */
  1358. spin_unlock_irqrestore(&self->lock, flags);
  1359. /* Hum... Check if networking is still active (avoid races) */
  1360. if((self->netopen) || (self->irlap)) {
  1361. /* Accept no more transmissions */
  1362. /*netif_device_detach(self->netdev);*/
  1363. netif_stop_queue(self->netdev);
  1364. /* Stop all the receive URBs */
  1365. for (i = 0; i < IU_MAX_RX_URBS; i++)
  1366. usb_unlink_urb(&(self->rx_urb[i]));
  1367. /* Cancel Tx and speed URB.
  1368.  * Toggle flags to make sure it's synchronous. */
  1369. self->tx_urb.transfer_flags &= ~USB_ASYNC_UNLINK;
  1370. usb_unlink_urb(&(self->tx_urb));
  1371. self->speed_urb.transfer_flags &= ~USB_ASYNC_UNLINK;
  1372. usb_unlink_urb(&(self->speed_urb));
  1373. }
  1374. /* Cleanup the device stuff */
  1375. irda_usb_close(self);
  1376. /* No longer attached to USB bus */
  1377. self->usbdev = NULL;
  1378. IRDA_DEBUG(0, "%s(), USB IrDA Disconnectedn", __FUNCTION__);
  1379. }
  1380. /*------------------------------------------------------------------*/
  1381. /*
  1382.  * USB device callbacks
  1383.  */
  1384. static struct usb_driver irda_driver = {
  1385. name: "irda-usb",
  1386. probe: irda_usb_probe,
  1387. disconnect: irda_usb_disconnect,
  1388. id_table: dongles,
  1389. };
  1390. /************************* MODULE CALLBACKS *************************/
  1391. /*
  1392.  * Deal with module insertion/removal
  1393.  * Mostly tell USB about our existence
  1394.  */
  1395. /*------------------------------------------------------------------*/
  1396. /*
  1397.  * Module insertion
  1398.  */
  1399. int __init usb_irda_init(void)
  1400. {
  1401. if (usb_register(&irda_driver) < 0)
  1402. return -1;
  1403. MESSAGE("USB IrDA support registeredn");
  1404. return 0;
  1405. }
  1406. module_init(usb_irda_init);
  1407. /*------------------------------------------------------------------*/
  1408. /*
  1409.  * Module removal
  1410.  */
  1411. void __exit usb_irda_cleanup(void)
  1412. {
  1413. struct irda_usb_cb *irda = NULL;
  1414. int i;
  1415. /* Find zombie instances and kill them...
  1416.  * In theory, it can't happen any longer. Jean II */
  1417. for (i = 0; i < NIRUSB; i++) {
  1418. irda = &irda_instance[i];
  1419. /* If the Device is zombie */
  1420. if((irda->usbdev != NULL) && (irda->present == 0)) {
  1421. IRDA_DEBUG(0, "%s(), disconnect zombie now !n", __FUNCTION__);
  1422. irda_usb_disconnect(irda->usbdev, (void *) irda);
  1423. }
  1424. }
  1425. /* Deregister the driver and remove all pending instances */
  1426. usb_deregister(&irda_driver);
  1427. }
  1428. module_exit(usb_irda_cleanup);
  1429. /*------------------------------------------------------------------*/
  1430. /*
  1431.  * Module parameters
  1432.  */
  1433. MODULE_PARM(qos_mtt_bits, "i");
  1434. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  1435. MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net> and Jean Tourrilhes <jt@hpl.hp.com>");
  1436. MODULE_DESCRIPTION("IrDA-USB Dongle Driver"); 
  1437. MODULE_LICENSE("GPL");