PageRenderTime 73ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/irda/irda-usb.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 1959 lines | 1059 code | 249 blank | 651 comment | 215 complexity | 798cdad6ac60723d59a77450f8efe422 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*****************************************************************************
  2. *
  3. * Filename: irda-usb.c
  4. * Version: 0.10
  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. * Copyright (C) 2004, SigmaTel, Inc. <irquality@sigmatel.com>
  13. * Copyright (C) 2005, Milan Beno <beno@pobox.sk>
  14. * Copyright (C) 2006, Nick Fedchik <nick@fedchik.org.ua>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. *****************************************************************************/
  31. /*
  32. * IMPORTANT NOTE
  33. * --------------
  34. *
  35. * As of kernel 2.5.20, this is the state of compliance and testing of
  36. * this driver (irda-usb) with regards to the USB low level drivers...
  37. *
  38. * This driver has been tested SUCCESSFULLY with the following drivers :
  39. * o usb-uhci-hcd (For Intel/Via USB controllers)
  40. * o uhci-hcd (Alternate/JE driver for Intel/Via USB controllers)
  41. * o ohci-hcd (For other USB controllers)
  42. *
  43. * This driver has NOT been tested with the following drivers :
  44. * o ehci-hcd (USB 2.0 controllers)
  45. *
  46. * Note that all HCD drivers do URB_ZERO_PACKET and timeout properly,
  47. * so we don't have to worry about that anymore.
  48. * One common problem is the failure to set the address on the dongle,
  49. * but this happens before the driver gets loaded...
  50. *
  51. * Jean II
  52. */
  53. /*------------------------------------------------------------------*/
  54. #include <linux/module.h>
  55. #include <linux/moduleparam.h>
  56. #include <linux/kernel.h>
  57. #include <linux/types.h>
  58. #include <linux/init.h>
  59. #include <linux/skbuff.h>
  60. #include <linux/netdevice.h>
  61. #include <linux/slab.h>
  62. #include <linux/rtnetlink.h>
  63. #include <linux/usb.h>
  64. #include <linux/firmware.h>
  65. #include "irda-usb.h"
  66. /*------------------------------------------------------------------*/
  67. static int qos_mtt_bits = 0;
  68. /* These are the currently known IrDA USB dongles. Add new dongles here */
  69. static struct usb_device_id dongles[] = {
  70. /* ACTiSYS Corp., ACT-IR2000U FIR-USB Adapter */
  71. { USB_DEVICE(0x9c4, 0x011), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
  72. /* Look like ACTiSYS, Report : IBM Corp., IBM UltraPort IrDA */
  73. { USB_DEVICE(0x4428, 0x012), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
  74. /* KC Technology Inc., KC-180 USB IrDA Device */
  75. { USB_DEVICE(0x50f, 0x180), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
  76. /* Extended Systems, Inc., XTNDAccess IrDA USB (ESI-9685) */
  77. { USB_DEVICE(0x8e9, 0x100), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
  78. /* SigmaTel STIR4210/4220/4116 USB IrDA (VFIR) Bridge */
  79. { USB_DEVICE(0x66f, 0x4210), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
  80. { USB_DEVICE(0x66f, 0x4220), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
  81. { USB_DEVICE(0x66f, 0x4116), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
  82. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  83. USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  84. .bInterfaceClass = USB_CLASS_APP_SPEC,
  85. .bInterfaceSubClass = USB_CLASS_IRDA,
  86. .driver_info = IUC_DEFAULT, },
  87. { }, /* The end */
  88. };
  89. /*
  90. * Important note :
  91. * Devices based on the SigmaTel chipset (0x66f, 0x4200) are not designed
  92. * using the "USB-IrDA specification" (yes, there exist such a thing), and
  93. * therefore not supported by this driver (don't add them above).
  94. * There is a Linux driver, stir4200, that support those USB devices.
  95. * Jean II
  96. */
  97. MODULE_DEVICE_TABLE(usb, dongles);
  98. /*------------------------------------------------------------------*/
  99. static void irda_usb_init_qos(struct irda_usb_cb *self) ;
  100. static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
  101. static void irda_usb_disconnect(struct usb_interface *intf);
  102. static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
  103. static netdev_tx_t irda_usb_hard_xmit(struct sk_buff *skb,
  104. struct net_device *dev);
  105. static int irda_usb_open(struct irda_usb_cb *self);
  106. static void irda_usb_close(struct irda_usb_cb *self);
  107. static void speed_bulk_callback(struct urb *urb);
  108. static void write_bulk_callback(struct urb *urb);
  109. static void irda_usb_receive(struct urb *urb);
  110. static void irda_usb_rx_defer_expired(unsigned long data);
  111. static int irda_usb_net_open(struct net_device *dev);
  112. static int irda_usb_net_close(struct net_device *dev);
  113. static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  114. static void irda_usb_net_timeout(struct net_device *dev);
  115. /************************ TRANSMIT ROUTINES ************************/
  116. /*
  117. * Receive packets from the IrDA stack and send them on the USB pipe.
  118. * Handle speed change, timeout and lot's of ugliness...
  119. */
  120. /*------------------------------------------------------------------*/
  121. /*
  122. * Function irda_usb_build_header(self, skb, header)
  123. *
  124. * Builds USB-IrDA outbound header
  125. *
  126. * When we send an IrDA frame over an USB pipe, we add to it a 1 byte
  127. * header. This function create this header with the proper values.
  128. *
  129. * Important note : the USB-IrDA spec 1.0 say very clearly in chapter 5.4.2.2
  130. * that the setting of the link speed and xbof number in this outbound header
  131. * should be applied *AFTER* the frame has been sent.
  132. * Unfortunately, some devices are not compliant with that... It seems that
  133. * reading the spec is far too difficult...
  134. * Jean II
  135. */
  136. static void irda_usb_build_header(struct irda_usb_cb *self,
  137. __u8 *header,
  138. int force)
  139. {
  140. /* Here we check if we have an STIR421x chip,
  141. * and if either speed or xbofs (or both) needs
  142. * to be changed.
  143. */
  144. if (self->capability & IUC_STIR421X &&
  145. ((self->new_speed != -1) || (self->new_xbofs != -1))) {
  146. /* With STIR421x, speed and xBOFs must be set at the same
  147. * time, even if only one of them changes.
  148. */
  149. if (self->new_speed == -1)
  150. self->new_speed = self->speed ;
  151. if (self->new_xbofs == -1)
  152. self->new_xbofs = self->xbofs ;
  153. }
  154. /* Set the link speed */
  155. if (self->new_speed != -1) {
  156. /* Hum... Ugly hack :-(
  157. * Some device are not compliant with the spec and change
  158. * parameters *before* sending the frame. - Jean II
  159. */
  160. if ((self->capability & IUC_SPEED_BUG) &&
  161. (!force) && (self->speed != -1)) {
  162. /* No speed and xbofs change here
  163. * (we'll do it later in the write callback) */
  164. IRDA_DEBUG(2, "%s(), not changing speed yet\n", __func__);
  165. *header = 0;
  166. return;
  167. }
  168. IRDA_DEBUG(2, "%s(), changing speed to %d\n", __func__, self->new_speed);
  169. self->speed = self->new_speed;
  170. /* We will do ` self->new_speed = -1; ' in the completion
  171. * handler just in case the current URB fail - Jean II */
  172. switch (self->speed) {
  173. case 2400:
  174. *header = SPEED_2400;
  175. break;
  176. default:
  177. case 9600:
  178. *header = SPEED_9600;
  179. break;
  180. case 19200:
  181. *header = SPEED_19200;
  182. break;
  183. case 38400:
  184. *header = SPEED_38400;
  185. break;
  186. case 57600:
  187. *header = SPEED_57600;
  188. break;
  189. case 115200:
  190. *header = SPEED_115200;
  191. break;
  192. case 576000:
  193. *header = SPEED_576000;
  194. break;
  195. case 1152000:
  196. *header = SPEED_1152000;
  197. break;
  198. case 4000000:
  199. *header = SPEED_4000000;
  200. self->new_xbofs = 0;
  201. break;
  202. case 16000000:
  203. *header = SPEED_16000000;
  204. self->new_xbofs = 0;
  205. break;
  206. }
  207. } else
  208. /* No change */
  209. *header = 0;
  210. /* Set the negotiated additional XBOFS */
  211. if (self->new_xbofs != -1) {
  212. IRDA_DEBUG(2, "%s(), changing xbofs to %d\n", __func__, self->new_xbofs);
  213. self->xbofs = self->new_xbofs;
  214. /* We will do ` self->new_xbofs = -1; ' in the completion
  215. * handler just in case the current URB fail - Jean II */
  216. switch (self->xbofs) {
  217. case 48:
  218. *header |= 0x10;
  219. break;
  220. case 28:
  221. case 24: /* USB spec 1.0 says 24 */
  222. *header |= 0x20;
  223. break;
  224. default:
  225. case 12:
  226. *header |= 0x30;
  227. break;
  228. case 5: /* Bug in IrLAP spec? (should be 6) */
  229. case 6:
  230. *header |= 0x40;
  231. break;
  232. case 3:
  233. *header |= 0x50;
  234. break;
  235. case 2:
  236. *header |= 0x60;
  237. break;
  238. case 1:
  239. *header |= 0x70;
  240. break;
  241. case 0:
  242. *header |= 0x80;
  243. break;
  244. }
  245. }
  246. }
  247. /*
  248. * calculate turnaround time for SigmaTel header
  249. */
  250. static __u8 get_turnaround_time(struct sk_buff *skb)
  251. {
  252. int turnaround_time = irda_get_mtt(skb);
  253. if ( turnaround_time == 0 )
  254. return 0;
  255. else if ( turnaround_time <= 10 )
  256. return 1;
  257. else if ( turnaround_time <= 50 )
  258. return 2;
  259. else if ( turnaround_time <= 100 )
  260. return 3;
  261. else if ( turnaround_time <= 500 )
  262. return 4;
  263. else if ( turnaround_time <= 1000 )
  264. return 5;
  265. else if ( turnaround_time <= 5000 )
  266. return 6;
  267. else
  268. return 7;
  269. }
  270. /*------------------------------------------------------------------*/
  271. /*
  272. * Send a command to change the speed of the dongle
  273. * Need to be called with spinlock on.
  274. */
  275. static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
  276. {
  277. __u8 *frame;
  278. struct urb *urb;
  279. int ret;
  280. IRDA_DEBUG(2, "%s(), speed=%d, xbofs=%d\n", __func__,
  281. self->new_speed, self->new_xbofs);
  282. /* Grab the speed URB */
  283. urb = self->speed_urb;
  284. if (urb->status != 0) {
  285. IRDA_WARNING("%s(), URB still in use!\n", __func__);
  286. return;
  287. }
  288. /* Allocate the fake frame */
  289. frame = self->speed_buff;
  290. /* Set the new speed and xbofs in this fake frame */
  291. irda_usb_build_header(self, frame, 1);
  292. if (self->capability & IUC_STIR421X) {
  293. if (frame[0] == 0) return ; // do nothing if no change
  294. frame[1] = 0; // other parameters don't change here
  295. frame[2] = 0;
  296. }
  297. /* Submit the 0 length IrDA frame to trigger new speed settings */
  298. usb_fill_bulk_urb(urb, self->usbdev,
  299. usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
  300. frame, IRDA_USB_SPEED_MTU,
  301. speed_bulk_callback, self);
  302. urb->transfer_buffer_length = self->header_length;
  303. urb->transfer_flags = 0;
  304. /* Irq disabled -> GFP_ATOMIC */
  305. if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
  306. IRDA_WARNING("%s(), failed Speed URB\n", __func__);
  307. }
  308. }
  309. /*------------------------------------------------------------------*/
  310. /*
  311. * Speed URB callback
  312. * Now, we can only get called for the speed URB.
  313. */
  314. static void speed_bulk_callback(struct urb *urb)
  315. {
  316. struct irda_usb_cb *self = urb->context;
  317. IRDA_DEBUG(2, "%s()\n", __func__);
  318. /* We should always have a context */
  319. IRDA_ASSERT(self != NULL, return;);
  320. /* We should always be called for the speed URB */
  321. IRDA_ASSERT(urb == self->speed_urb, return;);
  322. /* Check for timeout and other USB nasties */
  323. if (urb->status != 0) {
  324. /* I get a lot of -ECONNABORTED = -103 here - Jean II */
  325. IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __func__, urb->status, urb->transfer_flags);
  326. /* Don't do anything here, that might confuse the USB layer.
  327. * Instead, we will wait for irda_usb_net_timeout(), the
  328. * network layer watchdog, to fix the situation.
  329. * Jean II */
  330. /* A reset of the dongle might be welcomed here - Jean II */
  331. return;
  332. }
  333. /* urb is now available */
  334. //urb->status = 0; -> tested above
  335. /* New speed and xbof is now committed in hardware */
  336. self->new_speed = -1;
  337. self->new_xbofs = -1;
  338. /* Allow the stack to send more packets */
  339. netif_wake_queue(self->netdev);
  340. }
  341. /*------------------------------------------------------------------*/
  342. /*
  343. * Send an IrDA frame to the USB dongle (for transmission)
  344. */
  345. static netdev_tx_t irda_usb_hard_xmit(struct sk_buff *skb,
  346. struct net_device *netdev)
  347. {
  348. struct irda_usb_cb *self = netdev_priv(netdev);
  349. struct urb *urb = self->tx_urb;
  350. unsigned long flags;
  351. s32 speed;
  352. s16 xbofs;
  353. int res, mtt;
  354. IRDA_DEBUG(4, "%s() on %s\n", __func__, netdev->name);
  355. netif_stop_queue(netdev);
  356. /* Protect us from USB callbacks, net watchdog and else. */
  357. spin_lock_irqsave(&self->lock, flags);
  358. /* Check if the device is still there.
  359. * We need to check self->present under the spinlock because
  360. * of irda_usb_disconnect() is synchronous - Jean II */
  361. if (!self->present) {
  362. IRDA_DEBUG(0, "%s(), Device is gone...\n", __func__);
  363. goto drop;
  364. }
  365. /* Check if we need to change the number of xbofs */
  366. xbofs = irda_get_next_xbofs(skb);
  367. if ((xbofs != self->xbofs) && (xbofs != -1)) {
  368. self->new_xbofs = xbofs;
  369. }
  370. /* Check if we need to change the speed */
  371. speed = irda_get_next_speed(skb);
  372. if ((speed != self->speed) && (speed != -1)) {
  373. /* Set the desired speed */
  374. self->new_speed = speed;
  375. /* Check for empty frame */
  376. if (!skb->len) {
  377. /* IrLAP send us an empty frame to make us change the
  378. * speed. Changing speed with the USB adapter is in
  379. * fact sending an empty frame to the adapter, so we
  380. * could just let the present function do its job.
  381. * However, we would wait for min turn time,
  382. * do an extra memcpy and increment packet counters...
  383. * Jean II */
  384. irda_usb_change_speed_xbofs(self);
  385. netdev->trans_start = jiffies;
  386. /* Will netif_wake_queue() in callback */
  387. goto drop;
  388. }
  389. }
  390. if (urb->status != 0) {
  391. IRDA_WARNING("%s(), URB still in use!\n", __func__);
  392. goto drop;
  393. }
  394. skb_copy_from_linear_data(skb, self->tx_buff + self->header_length, skb->len);
  395. /* Change setting for next frame */
  396. if (self->capability & IUC_STIR421X) {
  397. __u8 turnaround_time;
  398. __u8* frame = self->tx_buff;
  399. turnaround_time = get_turnaround_time( skb );
  400. irda_usb_build_header(self, frame, 0);
  401. frame[2] = turnaround_time;
  402. if ((skb->len != 0) &&
  403. ((skb->len % 128) == 0) &&
  404. ((skb->len % 512) != 0)) {
  405. /* add extra byte for special SigmaTel feature */
  406. frame[1] = 1;
  407. skb_put(skb, 1);
  408. } else {
  409. frame[1] = 0;
  410. }
  411. } else {
  412. irda_usb_build_header(self, self->tx_buff, 0);
  413. }
  414. /* FIXME: Make macro out of this one */
  415. ((struct irda_skb_cb *)skb->cb)->context = self;
  416. usb_fill_bulk_urb(urb, self->usbdev,
  417. usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
  418. self->tx_buff, skb->len + self->header_length,
  419. write_bulk_callback, skb);
  420. /* This flag (URB_ZERO_PACKET) indicates that what we send is not
  421. * a continuous stream of data but separate packets.
  422. * In this case, the USB layer will insert an empty USB frame (TD)
  423. * after each of our packets that is exact multiple of the frame size.
  424. * This is how the dongle will detect the end of packet - Jean II */
  425. urb->transfer_flags = URB_ZERO_PACKET;
  426. /* Generate min turn time. FIXME: can we do better than this? */
  427. /* Trying to a turnaround time at this level is trying to measure
  428. * processor clock cycle with a wrist-watch, approximate at best...
  429. *
  430. * What we know is the last time we received a frame over USB.
  431. * Due to latency over USB that depend on the USB load, we don't
  432. * know when this frame was received over IrDA (a few ms before ?)
  433. * Then, same story for our outgoing frame...
  434. *
  435. * In theory, the USB dongle is supposed to handle the turnaround
  436. * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
  437. * why this code is enabled only for dongles that doesn't meet
  438. * the spec.
  439. * Jean II */
  440. if (self->capability & IUC_NO_TURN) {
  441. mtt = irda_get_mtt(skb);
  442. if (mtt) {
  443. int diff;
  444. do_gettimeofday(&self->now);
  445. diff = self->now.tv_usec - self->stamp.tv_usec;
  446. #ifdef IU_USB_MIN_RTT
  447. /* Factor in USB delays -> Get rid of udelay() that
  448. * would be lost in the noise - Jean II */
  449. diff += IU_USB_MIN_RTT;
  450. #endif /* IU_USB_MIN_RTT */
  451. /* If the usec counter did wraparound, the diff will
  452. * go negative (tv_usec is a long), so we need to
  453. * correct it by one second. Jean II */
  454. if (diff < 0)
  455. diff += 1000000;
  456. /* Check if the mtt is larger than the time we have
  457. * already used by all the protocol processing
  458. */
  459. if (mtt > diff) {
  460. mtt -= diff;
  461. if (mtt > 1000)
  462. mdelay(mtt/1000);
  463. else
  464. udelay(mtt);
  465. }
  466. }
  467. }
  468. /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
  469. if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
  470. IRDA_WARNING("%s(), failed Tx URB\n", __func__);
  471. netdev->stats.tx_errors++;
  472. /* Let USB recover : We will catch that in the watchdog */
  473. /*netif_start_queue(netdev);*/
  474. } else {
  475. /* Increment packet stats */
  476. netdev->stats.tx_packets++;
  477. netdev->stats.tx_bytes += skb->len;
  478. netdev->trans_start = jiffies;
  479. }
  480. spin_unlock_irqrestore(&self->lock, flags);
  481. return NETDEV_TX_OK;
  482. drop:
  483. /* Drop silently the skb and exit */
  484. dev_kfree_skb(skb);
  485. spin_unlock_irqrestore(&self->lock, flags);
  486. return NETDEV_TX_OK;
  487. }
  488. /*------------------------------------------------------------------*/
  489. /*
  490. * Note : this function will be called only for tx_urb...
  491. */
  492. static void write_bulk_callback(struct urb *urb)
  493. {
  494. unsigned long flags;
  495. struct sk_buff *skb = urb->context;
  496. struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
  497. IRDA_DEBUG(2, "%s()\n", __func__);
  498. /* We should always have a context */
  499. IRDA_ASSERT(self != NULL, return;);
  500. /* We should always be called for the speed URB */
  501. IRDA_ASSERT(urb == self->tx_urb, return;);
  502. /* Free up the skb */
  503. dev_kfree_skb_any(skb);
  504. urb->context = NULL;
  505. /* Check for timeout and other USB nasties */
  506. if (urb->status != 0) {
  507. /* I get a lot of -ECONNABORTED = -103 here - Jean II */
  508. IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __func__, urb->status, urb->transfer_flags);
  509. /* Don't do anything here, that might confuse the USB layer,
  510. * and we could go in recursion and blow the kernel stack...
  511. * Instead, we will wait for irda_usb_net_timeout(), the
  512. * network layer watchdog, to fix the situation.
  513. * Jean II */
  514. /* A reset of the dongle might be welcomed here - Jean II */
  515. return;
  516. }
  517. /* urb is now available */
  518. //urb->status = 0; -> tested above
  519. /* Make sure we read self->present properly */
  520. spin_lock_irqsave(&self->lock, flags);
  521. /* If the network is closed, stop everything */
  522. if ((!self->netopen) || (!self->present)) {
  523. IRDA_DEBUG(0, "%s(), Network is gone...\n", __func__);
  524. spin_unlock_irqrestore(&self->lock, flags);
  525. return;
  526. }
  527. /* If changes to speed or xbofs is pending... */
  528. if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
  529. if ((self->new_speed != self->speed) ||
  530. (self->new_xbofs != self->xbofs)) {
  531. /* We haven't changed speed yet (because of
  532. * IUC_SPEED_BUG), so do it now - Jean II */
  533. IRDA_DEBUG(1, "%s(), Changing speed now...\n", __func__);
  534. irda_usb_change_speed_xbofs(self);
  535. } else {
  536. /* New speed and xbof is now committed in hardware */
  537. self->new_speed = -1;
  538. self->new_xbofs = -1;
  539. /* Done, waiting for next packet */
  540. netif_wake_queue(self->netdev);
  541. }
  542. } else {
  543. /* Otherwise, allow the stack to send more packets */
  544. netif_wake_queue(self->netdev);
  545. }
  546. spin_unlock_irqrestore(&self->lock, flags);
  547. }
  548. /*------------------------------------------------------------------*/
  549. /*
  550. * Watchdog timer from the network layer.
  551. * After a predetermined timeout, if we don't give confirmation that
  552. * the packet has been sent (i.e. no call to netif_wake_queue()),
  553. * the network layer will call this function.
  554. * Note that URB that we submit have also a timeout. When the URB timeout
  555. * expire, the normal URB callback is called (write_bulk_callback()).
  556. */
  557. static void irda_usb_net_timeout(struct net_device *netdev)
  558. {
  559. unsigned long flags;
  560. struct irda_usb_cb *self = netdev_priv(netdev);
  561. struct urb *urb;
  562. int done = 0; /* If we have made any progress */
  563. IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __func__);
  564. IRDA_ASSERT(self != NULL, return;);
  565. /* Protect us from USB callbacks, net Tx and else. */
  566. spin_lock_irqsave(&self->lock, flags);
  567. /* self->present *MUST* be read under spinlock */
  568. if (!self->present) {
  569. IRDA_WARNING("%s(), device not present!\n", __func__);
  570. netif_stop_queue(netdev);
  571. spin_unlock_irqrestore(&self->lock, flags);
  572. return;
  573. }
  574. /* Check speed URB */
  575. urb = self->speed_urb;
  576. if (urb->status != 0) {
  577. IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
  578. switch (urb->status) {
  579. case -EINPROGRESS:
  580. usb_unlink_urb(urb);
  581. /* Note : above will *NOT* call netif_wake_queue()
  582. * in completion handler, we will come back here.
  583. * Jean II */
  584. done = 1;
  585. break;
  586. case -ECONNRESET:
  587. case -ENOENT: /* urb unlinked by us */
  588. default: /* ??? - Play safe */
  589. urb->status = 0;
  590. netif_wake_queue(self->netdev);
  591. done = 1;
  592. break;
  593. }
  594. }
  595. /* Check Tx URB */
  596. urb = self->tx_urb;
  597. if (urb->status != 0) {
  598. struct sk_buff *skb = urb->context;
  599. IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
  600. /* Increase error count */
  601. netdev->stats.tx_errors++;
  602. #ifdef IU_BUG_KICK_TIMEOUT
  603. /* Can't be a bad idea to reset the speed ;-) - Jean II */
  604. if(self->new_speed == -1)
  605. self->new_speed = self->speed;
  606. if(self->new_xbofs == -1)
  607. self->new_xbofs = self->xbofs;
  608. irda_usb_change_speed_xbofs(self);
  609. #endif /* IU_BUG_KICK_TIMEOUT */
  610. switch (urb->status) {
  611. case -EINPROGRESS:
  612. usb_unlink_urb(urb);
  613. /* Note : above will *NOT* call netif_wake_queue()
  614. * in completion handler, because urb->status will
  615. * be -ENOENT. We will fix that at the next watchdog,
  616. * leaving more time to USB to recover...
  617. * Jean II */
  618. done = 1;
  619. break;
  620. case -ECONNRESET:
  621. case -ENOENT: /* urb unlinked by us */
  622. default: /* ??? - Play safe */
  623. if(skb != NULL) {
  624. dev_kfree_skb_any(skb);
  625. urb->context = NULL;
  626. }
  627. urb->status = 0;
  628. netif_wake_queue(self->netdev);
  629. done = 1;
  630. break;
  631. }
  632. }
  633. spin_unlock_irqrestore(&self->lock, flags);
  634. /* Maybe we need a reset */
  635. /* Note : Some drivers seem to use a usb_set_interface() when they
  636. * need to reset the hardware. Hum...
  637. */
  638. /* if(done == 0) */
  639. }
  640. /************************* RECEIVE ROUTINES *************************/
  641. /*
  642. * Receive packets from the USB layer stack and pass them to the IrDA stack.
  643. * Try to work around USB failures...
  644. */
  645. /*
  646. * Note :
  647. * Some of you may have noticed that most dongle have an interrupt in pipe
  648. * that we don't use. Here is the little secret...
  649. * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
  650. * in every USB frame. This is unnecessary overhead.
  651. * The interrupt in pipe will generate an event every time a packet is
  652. * received. Reading an interrupt pipe adds minimal overhead, but has some
  653. * latency (~1ms).
  654. * If we are connected (speed != 9600), we want to minimise latency, so
  655. * we just always hang the Rx URB and ignore the interrupt.
  656. * If we are not connected (speed == 9600), there is usually no Rx traffic,
  657. * and we want to minimise the USB overhead. In this case we should wait
  658. * on the interrupt pipe and hang the Rx URB only when an interrupt is
  659. * received.
  660. * Jean II
  661. *
  662. * Note : don't read the above as what we are currently doing, but as
  663. * something we could do with KC dongle. Also don't forget that the
  664. * interrupt pipe is not part of the original standard, so this would
  665. * need to be optional...
  666. * Jean II
  667. */
  668. /*------------------------------------------------------------------*/
  669. /*
  670. * Submit a Rx URB to the USB layer to handle reception of a frame
  671. * Mostly called by the completion callback of the previous URB.
  672. *
  673. * Jean II
  674. */
  675. static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
  676. {
  677. struct irda_skb_cb *cb;
  678. int ret;
  679. IRDA_DEBUG(2, "%s()\n", __func__);
  680. /* This should never happen */
  681. IRDA_ASSERT(skb != NULL, return;);
  682. IRDA_ASSERT(urb != NULL, return;);
  683. /* Save ourselves in the skb */
  684. cb = (struct irda_skb_cb *) skb->cb;
  685. cb->context = self;
  686. /* Reinitialize URB */
  687. usb_fill_bulk_urb(urb, self->usbdev,
  688. usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
  689. skb->data, IRDA_SKB_MAX_MTU,
  690. irda_usb_receive, skb);
  691. urb->status = 0;
  692. /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
  693. ret = usb_submit_urb(urb, GFP_ATOMIC);
  694. if (ret) {
  695. /* If this ever happen, we are in deep s***.
  696. * Basically, the Rx path will stop... */
  697. IRDA_WARNING("%s(), Failed to submit Rx URB %d\n",
  698. __func__, ret);
  699. }
  700. }
  701. /*------------------------------------------------------------------*/
  702. /*
  703. * Function irda_usb_receive(urb)
  704. *
  705. * Called by the USB subsystem when a frame has been received
  706. *
  707. */
  708. static void irda_usb_receive(struct urb *urb)
  709. {
  710. struct sk_buff *skb = (struct sk_buff *) urb->context;
  711. struct irda_usb_cb *self;
  712. struct irda_skb_cb *cb;
  713. struct sk_buff *newskb;
  714. struct sk_buff *dataskb;
  715. struct urb *next_urb;
  716. unsigned int len, docopy;
  717. IRDA_DEBUG(2, "%s(), len=%d\n", __func__, urb->actual_length);
  718. /* Find ourselves */
  719. cb = (struct irda_skb_cb *) skb->cb;
  720. IRDA_ASSERT(cb != NULL, return;);
  721. self = (struct irda_usb_cb *) cb->context;
  722. IRDA_ASSERT(self != NULL, return;);
  723. /* If the network is closed or the device gone, stop everything */
  724. if ((!self->netopen) || (!self->present)) {
  725. IRDA_DEBUG(0, "%s(), Network is gone!\n", __func__);
  726. /* Don't re-submit the URB : will stall the Rx path */
  727. return;
  728. }
  729. /* Check the status */
  730. if (urb->status != 0) {
  731. switch (urb->status) {
  732. case -EILSEQ:
  733. self->netdev->stats.rx_crc_errors++;
  734. /* Also precursor to a hot-unplug on UHCI. */
  735. /* Fallthrough... */
  736. case -ECONNRESET:
  737. /* Random error, if I remember correctly */
  738. /* uhci_cleanup_unlink() is going to kill the Rx
  739. * URB just after we return. No problem, at this
  740. * point the URB will be idle ;-) - Jean II */
  741. case -ESHUTDOWN:
  742. /* That's usually a hot-unplug. Submit will fail... */
  743. case -ETIME:
  744. /* Usually precursor to a hot-unplug on OHCI. */
  745. default:
  746. self->netdev->stats.rx_errors++;
  747. IRDA_DEBUG(0, "%s(), RX status %d, transfer_flags 0x%04X\n", __func__, urb->status, urb->transfer_flags);
  748. break;
  749. }
  750. /* If we received an error, we don't want to resubmit the
  751. * Rx URB straight away but to give the USB layer a little
  752. * bit of breathing room.
  753. * We are in the USB thread context, therefore there is a
  754. * danger of recursion (new URB we submit fails, we come
  755. * back here).
  756. * With recent USB stack (2.6.15+), I'm seeing that on
  757. * hot unplug of the dongle...
  758. * Lowest effective timer is 10ms...
  759. * Jean II */
  760. self->rx_defer_timer.function = irda_usb_rx_defer_expired;
  761. self->rx_defer_timer.data = (unsigned long) urb;
  762. mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
  763. return;
  764. }
  765. /* Check for empty frames */
  766. if (urb->actual_length <= self->header_length) {
  767. IRDA_WARNING("%s(), empty frame!\n", __func__);
  768. goto done;
  769. }
  770. /*
  771. * Remember the time we received this frame, so we can
  772. * reduce the min turn time a bit since we will know
  773. * how much time we have used for protocol processing
  774. */
  775. do_gettimeofday(&self->stamp);
  776. /* Check if we need to copy the data to a new skb or not.
  777. * For most frames, we use ZeroCopy and pass the already
  778. * allocated skb up the stack.
  779. * If the frame is small, it is more efficient to copy it
  780. * to save memory (copy will be fast anyway - that's
  781. * called Rx-copy-break). Jean II */
  782. docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
  783. /* Allocate a new skb */
  784. if (self->capability & IUC_STIR421X)
  785. newskb = dev_alloc_skb(docopy ? urb->actual_length :
  786. IRDA_SKB_MAX_MTU +
  787. USB_IRDA_STIR421X_HEADER);
  788. else
  789. newskb = dev_alloc_skb(docopy ? urb->actual_length :
  790. IRDA_SKB_MAX_MTU);
  791. if (!newskb) {
  792. self->netdev->stats.rx_dropped++;
  793. /* We could deliver the current skb, but this would stall
  794. * the Rx path. Better drop the packet... Jean II */
  795. goto done;
  796. }
  797. /* Make sure IP header get aligned (IrDA header is 5 bytes) */
  798. /* But IrDA-USB header is 1 byte. Jean II */
  799. //skb_reserve(newskb, USB_IRDA_HEADER - 1);
  800. if(docopy) {
  801. /* Copy packet, so we can recycle the original */
  802. skb_copy_from_linear_data(skb, newskb->data, urb->actual_length);
  803. /* Deliver this new skb */
  804. dataskb = newskb;
  805. /* And hook the old skb to the URB
  806. * Note : we don't need to "clean up" the old skb,
  807. * as we never touched it. Jean II */
  808. } else {
  809. /* We are using ZeroCopy. Deliver old skb */
  810. dataskb = skb;
  811. /* And hook the new skb to the URB */
  812. skb = newskb;
  813. }
  814. /* Set proper length on skb & remove USB-IrDA header */
  815. skb_put(dataskb, urb->actual_length);
  816. skb_pull(dataskb, self->header_length);
  817. /* Ask the networking layer to queue the packet for the IrDA stack */
  818. dataskb->dev = self->netdev;
  819. skb_reset_mac_header(dataskb);
  820. dataskb->protocol = htons(ETH_P_IRDA);
  821. len = dataskb->len;
  822. netif_rx(dataskb);
  823. /* Keep stats up to date */
  824. self->netdev->stats.rx_bytes += len;
  825. self->netdev->stats.rx_packets++;
  826. done:
  827. /* Note : at this point, the URB we've just received (urb)
  828. * is still referenced by the USB layer. For example, if we
  829. * have received a -ECONNRESET, uhci_cleanup_unlink() will
  830. * continue to process it (in fact, cleaning it up).
  831. * If we were to submit this URB, disaster would ensue.
  832. * Therefore, we submit our idle URB, and put this URB in our
  833. * idle slot....
  834. * Jean II */
  835. /* Note : with this scheme, we could submit the idle URB before
  836. * processing the Rx URB. I don't think it would buy us anything as
  837. * we are running in the USB thread context. Jean II */
  838. next_urb = self->idle_rx_urb;
  839. /* Recycle Rx URB : Now, the idle URB is the present one */
  840. urb->context = NULL;
  841. self->idle_rx_urb = urb;
  842. /* Submit the idle URB to replace the URB we've just received.
  843. * Do it last to avoid race conditions... Jean II */
  844. irda_usb_submit(self, skb, next_urb);
  845. }
  846. /*------------------------------------------------------------------*/
  847. /*
  848. * In case of errors, we want the USB layer to have time to recover.
  849. * Now, it is time to resubmit ouur Rx URB...
  850. */
  851. static void irda_usb_rx_defer_expired(unsigned long data)
  852. {
  853. struct urb *urb = (struct urb *) data;
  854. struct sk_buff *skb = (struct sk_buff *) urb->context;
  855. struct irda_usb_cb *self;
  856. struct irda_skb_cb *cb;
  857. struct urb *next_urb;
  858. IRDA_DEBUG(2, "%s()\n", __func__);
  859. /* Find ourselves */
  860. cb = (struct irda_skb_cb *) skb->cb;
  861. IRDA_ASSERT(cb != NULL, return;);
  862. self = (struct irda_usb_cb *) cb->context;
  863. IRDA_ASSERT(self != NULL, return;);
  864. /* Same stuff as when Rx is done, see above... */
  865. next_urb = self->idle_rx_urb;
  866. urb->context = NULL;
  867. self->idle_rx_urb = urb;
  868. irda_usb_submit(self, skb, next_urb);
  869. }
  870. /*------------------------------------------------------------------*/
  871. /*
  872. * Callbak from IrDA layer. IrDA wants to know if we have
  873. * started receiving anything.
  874. */
  875. static int irda_usb_is_receiving(struct irda_usb_cb *self)
  876. {
  877. /* Note : because of the way UHCI works, it's almost impossible
  878. * to get this info. The Controller DMA directly to memory and
  879. * signal only when the whole frame is finished. To know if the
  880. * first TD of the URB has been filled or not seems hard work...
  881. *
  882. * The other solution would be to use the "receiving" command
  883. * on the default decriptor with a usb_control_msg(), but that
  884. * would add USB traffic and would return result only in the
  885. * next USB frame (~1ms).
  886. *
  887. * I've been told that current dongles send status info on their
  888. * interrupt endpoint, and that's what the Windows driver uses
  889. * to know this info. Unfortunately, this is not yet in the spec...
  890. *
  891. * Jean II
  892. */
  893. return 0; /* For now */
  894. }
  895. #define STIR421X_PATCH_PRODUCT_VER "Product Version: "
  896. #define STIR421X_PATCH_STMP_TAG "STMP"
  897. #define STIR421X_PATCH_CODE_OFFSET 512 /* patch image starts before here */
  898. /* marks end of patch file header (PC DOS text file EOF character) */
  899. #define STIR421X_PATCH_END_OF_HDR_TAG 0x1A
  900. #define STIR421X_PATCH_BLOCK_SIZE 1023
  901. /*
  902. * Function stir421x_fwupload (struct irda_usb_cb *self,
  903. * unsigned char *patch,
  904. * const unsigned int patch_len)
  905. *
  906. * Upload firmware code to SigmaTel 421X IRDA-USB dongle
  907. */
  908. static int stir421x_fw_upload(struct irda_usb_cb *self,
  909. const unsigned char *patch,
  910. const unsigned int patch_len)
  911. {
  912. int ret = -ENOMEM;
  913. int actual_len = 0;
  914. unsigned int i;
  915. unsigned int block_size = 0;
  916. unsigned char *patch_block;
  917. patch_block = kzalloc(STIR421X_PATCH_BLOCK_SIZE, GFP_KERNEL);
  918. if (patch_block == NULL)
  919. return -ENOMEM;
  920. /* break up patch into 1023-byte sections */
  921. for (i = 0; i < patch_len; i += block_size) {
  922. block_size = patch_len - i;
  923. if (block_size > STIR421X_PATCH_BLOCK_SIZE)
  924. block_size = STIR421X_PATCH_BLOCK_SIZE;
  925. /* upload the patch section */
  926. memcpy(patch_block, patch + i, block_size);
  927. ret = usb_bulk_msg(self->usbdev,
  928. usb_sndbulkpipe(self->usbdev,
  929. self->bulk_out_ep),
  930. patch_block, block_size,
  931. &actual_len, msecs_to_jiffies(500));
  932. IRDA_DEBUG(3,"%s(): Bulk send %u bytes, ret=%d\n",
  933. __func__, actual_len, ret);
  934. if (ret < 0)
  935. break;
  936. mdelay(10);
  937. }
  938. kfree(patch_block);
  939. return ret;
  940. }
  941. /*
  942. * Function stir421x_patch_device(struct irda_usb_cb *self)
  943. *
  944. * Get a firmware code from userspase using hotplug request_firmware() call
  945. */
  946. static int stir421x_patch_device(struct irda_usb_cb *self)
  947. {
  948. unsigned int i;
  949. int ret;
  950. char stir421x_fw_name[12];
  951. const struct firmware *fw;
  952. const unsigned char *fw_version_ptr; /* pointer to version string */
  953. unsigned long fw_version = 0;
  954. /*
  955. * Known firmware patch file names for STIR421x dongles
  956. * are "42101001.sb" or "42101002.sb"
  957. */
  958. sprintf(stir421x_fw_name, "4210%4X.sb",
  959. self->usbdev->descriptor.bcdDevice);
  960. ret = request_firmware(&fw, stir421x_fw_name, &self->usbdev->dev);
  961. if (ret < 0)
  962. return ret;
  963. /* We get a patch from userspace */
  964. IRDA_MESSAGE("%s(): Received firmware %s (%zu bytes)\n",
  965. __func__, stir421x_fw_name, fw->size);
  966. ret = -EINVAL;
  967. /* Get the bcd product version */
  968. if (!memcmp(fw->data, STIR421X_PATCH_PRODUCT_VER,
  969. sizeof(STIR421X_PATCH_PRODUCT_VER) - 1)) {
  970. fw_version_ptr = fw->data +
  971. sizeof(STIR421X_PATCH_PRODUCT_VER) - 1;
  972. /* Let's check if the product version is dotted */
  973. if (fw_version_ptr[3] == '.' &&
  974. fw_version_ptr[7] == '.') {
  975. unsigned long major, minor, build;
  976. major = simple_strtoul(fw_version_ptr, NULL, 10);
  977. minor = simple_strtoul(fw_version_ptr + 4, NULL, 10);
  978. build = simple_strtoul(fw_version_ptr + 8, NULL, 10);
  979. fw_version = (major << 12)
  980. + (minor << 8)
  981. + ((build / 10) << 4)
  982. + (build % 10);
  983. IRDA_DEBUG(3, "%s(): Firmware Product version %ld\n",
  984. __func__, fw_version);
  985. }
  986. }
  987. if (self->usbdev->descriptor.bcdDevice == cpu_to_le16(fw_version)) {
  988. /*
  989. * If we're here, we've found a correct patch
  990. * The actual image starts after the "STMP" keyword
  991. * so forward to the firmware header tag
  992. */
  993. for (i = 0; i < fw->size && fw->data[i] !=
  994. STIR421X_PATCH_END_OF_HDR_TAG; i++) ;
  995. /* here we check for the out of buffer case */
  996. if (i < STIR421X_PATCH_CODE_OFFSET && i < fw->size &&
  997. STIR421X_PATCH_END_OF_HDR_TAG == fw->data[i]) {
  998. if (!memcmp(fw->data + i + 1, STIR421X_PATCH_STMP_TAG,
  999. sizeof(STIR421X_PATCH_STMP_TAG) - 1)) {
  1000. /* We can upload the patch to the target */
  1001. i += sizeof(STIR421X_PATCH_STMP_TAG);
  1002. ret = stir421x_fw_upload(self, &fw->data[i],
  1003. fw->size - i);
  1004. }
  1005. }
  1006. }
  1007. release_firmware(fw);
  1008. return ret;
  1009. }
  1010. /********************** IRDA DEVICE CALLBACKS **********************/
  1011. /*
  1012. * Main calls from the IrDA/Network subsystem.
  1013. * Mostly registering a new irda-usb device and removing it....
  1014. * We only deal with the IrDA side of the business, the USB side will
  1015. * be dealt with below...
  1016. */
  1017. /*------------------------------------------------------------------*/
  1018. /*
  1019. * Function irda_usb_net_open (dev)
  1020. *
  1021. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  1022. *
  1023. * Note : don't mess with self->netopen - Jean II
  1024. */
  1025. static int irda_usb_net_open(struct net_device *netdev)
  1026. {
  1027. struct irda_usb_cb *self;
  1028. unsigned long flags;
  1029. char hwname[16];
  1030. int i;
  1031. IRDA_DEBUG(1, "%s()\n", __func__);
  1032. IRDA_ASSERT(netdev != NULL, return -1;);
  1033. self = netdev_priv(netdev);
  1034. IRDA_ASSERT(self != NULL, return -1;);
  1035. spin_lock_irqsave(&self->lock, flags);
  1036. /* Can only open the device if it's there */
  1037. if(!self->present) {
  1038. spin_unlock_irqrestore(&self->lock, flags);
  1039. IRDA_WARNING("%s(), device not present!\n", __func__);
  1040. return -1;
  1041. }
  1042. if(self->needspatch) {
  1043. spin_unlock_irqrestore(&self->lock, flags);
  1044. IRDA_WARNING("%s(), device needs patch\n", __func__) ;
  1045. return -EIO ;
  1046. }
  1047. /* Initialise default speed and xbofs value
  1048. * (IrLAP will change that soon) */
  1049. self->speed = -1;
  1050. self->xbofs = -1;
  1051. self->new_speed = -1;
  1052. self->new_xbofs = -1;
  1053. /* To do *before* submitting Rx urbs and starting net Tx queue
  1054. * Jean II */
  1055. self->netopen = 1;
  1056. spin_unlock_irqrestore(&self->lock, flags);
  1057. /*
  1058. * Now that everything should be initialized properly,
  1059. * Open new IrLAP layer instance to take care of us...
  1060. * Note : will send immediately a speed change...
  1061. */
  1062. sprintf(hwname, "usb#%d", self->usbdev->devnum);
  1063. self->irlap = irlap_open(netdev, &self->qos, hwname);
  1064. IRDA_ASSERT(self->irlap != NULL, return -1;);
  1065. /* Allow IrLAP to send data to us */
  1066. netif_start_queue(netdev);
  1067. /* We submit all the Rx URB except for one that we keep idle.
  1068. * Need to be initialised before submitting other USBs, because
  1069. * in some cases as soon as we submit the URBs the USB layer
  1070. * will trigger a dummy receive - Jean II */
  1071. self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
  1072. self->idle_rx_urb->context = NULL;
  1073. /* Now that we can pass data to IrLAP, allow the USB layer
  1074. * to send us some data... */
  1075. for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
  1076. struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  1077. if (!skb) {
  1078. /* If this ever happen, we are in deep s***.
  1079. * Basically, we can't start the Rx path... */
  1080. IRDA_WARNING("%s(), Failed to allocate Rx skb\n",
  1081. __func__);
  1082. return -1;
  1083. }
  1084. //skb_reserve(newskb, USB_IRDA_HEADER - 1);
  1085. irda_usb_submit(self, skb, self->rx_urb[i]);
  1086. }
  1087. /* Ready to play !!! */
  1088. return 0;
  1089. }
  1090. /*------------------------------------------------------------------*/
  1091. /*
  1092. * Function irda_usb_net_close (self)
  1093. *
  1094. * Network device is taken down. Usually this is done by
  1095. * "ifconfig irda0 down"
  1096. */
  1097. static int irda_usb_net_close(struct net_device *netdev)
  1098. {
  1099. struct irda_usb_cb *self;
  1100. int i;
  1101. IRDA_DEBUG(1, "%s()\n", __func__);
  1102. IRDA_ASSERT(netdev != NULL, return -1;);
  1103. self = netdev_priv(netdev);
  1104. IRDA_ASSERT(self != NULL, return -1;);
  1105. /* Clear this flag *before* unlinking the urbs and *before*
  1106. * stopping the network Tx queue - Jean II */
  1107. self->netopen = 0;
  1108. /* Stop network Tx queue */
  1109. netif_stop_queue(netdev);
  1110. /* Kill defered Rx URB */
  1111. del_timer(&self->rx_defer_timer);
  1112. /* Deallocate all the Rx path buffers (URBs and skb) */
  1113. for (i = 0; i < self->max_rx_urb; i++) {
  1114. struct urb *urb = self->rx_urb[i];
  1115. struct sk_buff *skb = (struct sk_buff *) urb->context;
  1116. /* Cancel the receive command */
  1117. usb_kill_urb(urb);
  1118. /* The skb is ours, free it */
  1119. if(skb) {
  1120. dev_kfree_skb(skb);
  1121. urb->context = NULL;
  1122. }
  1123. }
  1124. /* Cancel Tx and speed URB - need to be synchronous to avoid races */
  1125. usb_kill_urb(self->tx_urb);
  1126. usb_kill_urb(self->speed_urb);
  1127. /* Stop and remove instance of IrLAP */
  1128. if (self->irlap)
  1129. irlap_close(self->irlap);
  1130. self->irlap = NULL;
  1131. return 0;
  1132. }
  1133. /*------------------------------------------------------------------*/
  1134. /*
  1135. * IOCTLs : Extra out-of-band network commands...
  1136. */
  1137. static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1138. {
  1139. unsigned long flags;
  1140. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1141. struct irda_usb_cb *self;
  1142. int ret = 0;
  1143. IRDA_ASSERT(dev != NULL, return -1;);
  1144. self = netdev_priv(dev);
  1145. IRDA_ASSERT(self != NULL, return -1;);
  1146. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
  1147. switch (cmd) {
  1148. case SIOCSBANDWIDTH: /* Set bandwidth */
  1149. if (!capable(CAP_NET_ADMIN))
  1150. return -EPERM;
  1151. /* Protect us from USB callbacks, net watchdog and else. */
  1152. spin_lock_irqsave(&self->lock, flags);
  1153. /* Check if the device is still there */
  1154. if(self->present) {
  1155. /* Set the desired speed */
  1156. self->new_speed = irq->ifr_baudrate;
  1157. irda_usb_change_speed_xbofs(self);
  1158. }
  1159. spin_unlock_irqrestore(&self->lock, flags);
  1160. break;
  1161. case SIOCSMEDIABUSY: /* Set media busy */
  1162. if (!capable(CAP_NET_ADMIN))
  1163. return -EPERM;
  1164. /* Check if the IrDA stack is still there */
  1165. if(self->netopen)
  1166. irda_device_set_media_busy(self->netdev, TRUE);
  1167. break;
  1168. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1169. irq->ifr_receiving = irda_usb_is_receiving(self);
  1170. break;
  1171. default:
  1172. ret = -EOPNOTSUPP;
  1173. }
  1174. return ret;
  1175. }
  1176. /*------------------------------------------------------------------*/
  1177. /********************* IRDA CONFIG SUBROUTINES *********************/
  1178. /*
  1179. * Various subroutines dealing with IrDA and network stuff we use to
  1180. * configure and initialise each irda-usb instance.
  1181. * These functions are used below in the main calls of the driver...
  1182. */
  1183. /*------------------------------------------------------------------*/
  1184. /*
  1185. * Set proper values in the IrDA QOS structure
  1186. */
  1187. static inline void irda_usb_init_qos(struct irda_usb_cb *self)
  1188. {
  1189. struct irda_class_desc *desc;
  1190. IRDA_DEBUG(3, "%s()\n", __func__);
  1191. desc = self->irda_desc;
  1192. /* Initialize QoS for this device */
  1193. irda_init_max_qos_capabilies(&self->qos);
  1194. /* See spec section 7.2 for meaning.
  1195. * Values are little endian (as most USB stuff), the IrDA stack
  1196. * use it in native order (see parameters.c). - Jean II */
  1197. self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
  1198. self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
  1199. self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
  1200. self->qos.window_size.bits = desc->bmWindowSize;
  1201. self->qos.data_size.bits = desc->bmDataSize;
  1202. IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
  1203. __func__, 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);
  1204. /* Don't always trust what the dongle tell us */
  1205. if(self->capability & IUC_SIR_ONLY)
  1206. self->qos.baud_rate.bits &= 0x00ff;
  1207. if(self->capability & IUC_SMALL_PKT)
  1208. self->qos.data_size.bits = 0x07;
  1209. if(self->capability & IUC_NO_WINDOW)
  1210. self->qos.window_size.bits = 0x01;
  1211. if(self->capability & IUC_MAX_WINDOW)
  1212. self->qos.window_size.bits = 0x7f;
  1213. if(self->capability & IUC_MAX_XBOFS)
  1214. self->qos.additional_bofs.bits = 0x01;
  1215. #if 1
  1216. /* Module parameter can override the rx window size */
  1217. if (qos_mtt_bits)
  1218. self->qos.min_turn_time.bits = qos_mtt_bits;
  1219. #endif
  1220. /*
  1221. * Note : most of those values apply only for the receive path,
  1222. * the transmit path will be set differently - Jean II
  1223. */
  1224. irda_qos_bits_to_value(&self->qos);
  1225. }
  1226. /*------------------------------------------------------------------*/
  1227. static const struct net_device_ops irda_usb_netdev_ops = {
  1228. .ndo_open = irda_usb_net_open,
  1229. .ndo_stop = irda_usb_net_close,
  1230. .ndo_do_ioctl = irda_usb_net_ioctl,
  1231. .ndo_start_xmit = irda_usb_hard_xmit,
  1232. .ndo_tx_timeout = irda_usb_net_timeout,
  1233. };
  1234. /*
  1235. * Initialise the network side of the irda-usb instance
  1236. * Called when a new USB instance is registered in irda_usb_probe()
  1237. */
  1238. static inline int irda_usb_open(struct irda_usb_cb *self)
  1239. {
  1240. struct net_device *netdev = self->netdev;
  1241. IRDA_DEBUG(1, "%s()\n", __func__);
  1242. netdev->netdev_ops = &irda_usb_netdev_ops;
  1243. irda_usb_init_qos(self);
  1244. return register_netdev(netdev);
  1245. }
  1246. /*------------------------------------------------------------------*/
  1247. /*
  1248. * Cleanup the network side of the irda-usb instance
  1249. * Called when a USB instance is removed in irda_usb_disconnect()
  1250. */
  1251. static inline void irda_usb_close(struct irda_usb_cb *self)
  1252. {
  1253. IRDA_DEBUG(1, "%s()\n", __func__);
  1254. /* Remove netdevice */
  1255. unregister_netdev(self->netdev);
  1256. /* Remove the speed buffer */
  1257. kfree(self->speed_buff);
  1258. self->speed_buff = NULL;
  1259. kfree(self->tx_buff);
  1260. self->tx_buff = NULL;
  1261. }
  1262. /********************** USB CONFIG SUBROUTINES **********************/
  1263. /*
  1264. * Various subroutines dealing with USB stuff we use to configure and
  1265. * initialise each irda-usb instance.
  1266. * These functions are used below in the main calls of the driver...
  1267. */
  1268. /*------------------------------------------------------------------*/
  1269. /*
  1270. * Function irda_usb_parse_endpoints(dev, ifnum)
  1271. *
  1272. * Parse the various endpoints and find the one we need.
  1273. *
  1274. * The endpoint are the pipes used to communicate with the USB device.
  1275. * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
  1276. * These are used to pass frames back and forth with the dongle.
  1277. * Most dongle have also an interrupt endpoint, that will be probably
  1278. * documented in the next spec...
  1279. */
  1280. static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
  1281. {
  1282. int i; /* Endpoint index in table */
  1283. /* Init : no endpoints */
  1284. self->bulk_in_ep = 0;
  1285. self->bulk_out_ep = 0;
  1286. self->bulk_int_ep = 0;
  1287. /* Let's look at all those endpoints */
  1288. for(i = 0; i < ennum; i++) {
  1289. /* All those variables will get optimised by the compiler,
  1290. * so let's aim for clarity... - Jean II */
  1291. __u8 ep; /* Endpoint address */
  1292. __u8 dir; /* Endpoint direction */
  1293. __u8 attr; /* Endpoint attribute */
  1294. __u16 psize; /* Endpoint max packet size in bytes */
  1295. /* Get endpoint address, direction and attribute */
  1296. ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1297. dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
  1298. attr = endpoint[i].desc.bmAttributes;
  1299. psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
  1300. /* Is it a bulk endpoint ??? */
  1301. if(attr == USB_ENDPOINT_XFER_BULK) {
  1302. /* We need to find an IN and an OUT */
  1303. if(dir == USB_DIR_IN) {
  1304. /* This is our Rx endpoint */
  1305. self->bulk_in_ep = ep;
  1306. } else {
  1307. /* This is our Tx endpoint */
  1308. self->bulk_out_ep = ep;
  1309. self->bulk_out_mtu = psize;
  1310. }
  1311. } else {
  1312. if((attr == USB_ENDPOINT_XFER_INT) &&
  1313. (dir == USB_DIR_IN)) {
  1314. /* This is our interrupt endpoint */
  1315. self->bulk_int_ep = ep;
  1316. } else {
  1317. IRDA_ERROR("%s(), Unrecognised endpoint %02X.\n", __func__, ep);
  1318. }
  1319. }
  1320. }
  1321. IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
  1322. __func__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
  1323. return (self->bulk_in_ep != 0) && (self->bulk_out_ep != 0);
  1324. }
  1325. #ifdef IU_DUMP_CLASS_DESC
  1326. /*------------------------------------------------------------------*/
  1327. /*
  1328. * Function usb_irda_dump_class_desc(desc)
  1329. *
  1330. * Prints out the contents of the IrDA class descriptor
  1331. *
  1332. */
  1333. static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
  1334. {
  1335. /* Values are little endian */
  1336. printk("bLength=%x\n", desc->bLength);
  1337. printk("bDescriptorType=%x\n", desc->bDescriptorType);
  1338. printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
  1339. printk("bmDataSize=%x\n", desc->bmDataSize);
  1340. printk("bmWindowSize=%x\n", desc->bmWindowSize);
  1341. printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
  1342. printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
  1343. printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
  1344. printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
  1345. printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
  1346. }
  1347. #endif /* IU_DUMP_CLASS_DESC */
  1348. /*------------------------------------------------------------------*/
  1349. /*
  1350. * Function irda_usb_find_class_desc(intf)
  1351. *
  1352. * Returns instance of IrDA class descriptor, or NULL if not found
  1353. *
  1354. * The class descriptor is some extra info that IrDA USB devices will
  1355. * offer to us, describing their IrDA characteristics. We will use that in
  1356. * irda_usb_init_qos()
  1357. */
  1358. static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
  1359. {
  1360. struct usb_device *dev = interface_to_usbdev (intf);
  1361. struct irda_class_desc *desc;
  1362. int ret;
  1363. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  1364. if (!desc)
  1365. return NULL;
  1366. /* USB-IrDA class spec 1.0:
  1367. * 6.1.3: Standard "Get Descriptor" Device Request is not
  1368. * appropriate to retrieve class-specific descriptor
  1369. * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
  1370. * is mandatory and returns the USB-IrDA class descriptor
  1371. */
  1372. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
  1373. IU_REQ_GET_CLASS_DESC,
  1374. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1375. 0, intf->altsetting->desc.bInterfaceNumber, desc,
  1376. sizeof(*desc), 500);
  1377. IRDA_DEBUG(1, "%s(), ret=%d\n", __func__, ret);
  1378. if (ret < sizeof(*desc)) {
  1379. IRDA_WARNING("usb-irda: class_descriptor read %s (%d)\n",
  1380. (ret<0) ? "failed" : "too short", ret);
  1381. }
  1382. else if (desc->bDescriptorType != USB_DT_IRDA) {
  1383. IRDA_WARNING("usb-irda: bad class_descriptor type\n");
  1384. }
  1385. else {
  1386. #ifdef IU_DUMP_CLASS_DESC
  1387. irda_usb_dump_class_desc(desc);
  1388. #endif /* IU_DUMP_CLASS_DESC */
  1389. return desc;
  1390. }
  1391. kfree(desc);
  1392. return NULL;
  1393. }
  1394. /*********************** USB DEVICE CALLBACKS ***********************/
  1395. /*
  1396. * Main calls from the USB subsystem.
  1397. * Mostly registering a new irda-usb device and removing it....
  1398. */
  1399. /*------------------------------------------------------------------*/
  1400. /*
  1401. * This routine is called by the USB subsystem for each new device
  1402. * in the system. We need to check if the device is ours, and in
  1403. * this case start handling it.
  1404. * The USB layer protect us from reentrancy (via BKL), so we don't need
  1405. * to spinlock in there... Jean II
  1406. */
  1407. static int irda_usb_probe(struct usb_interface *intf,
  1408. const struct usb_device_id *id)
  1409. {
  1410. struct net_device *net;
  1411. struct usb_device *dev = interface_to_usbdev(intf);
  1412. struct irda_usb_cb *self;
  1413. struct usb_host_interface *interface;
  1414. struct irda_class_desc *irda_desc;
  1415. int ret = -ENOMEM;
  1416. int i; /* Driver instance index / Rx URB index */
  1417. /* Note : the probe make sure to call us only for devices that
  1418. * matches the list of dongle (top of the file). So, we
  1419. * don't need to check if the dongle is really ours.
  1420. * Jean II */
  1421. IRDA_MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
  1422. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  1423. le16_to_cpu(dev->descriptor.idProduct));
  1424. net = alloc_irdadev(sizeof(*self));
  1425. if (!net)
  1426. goto err_out;
  1427. SET_NETDEV_DEV(net, &intf->dev);
  1428. self = netdev_priv(net);
  1429. self->netdev = net;
  1430. spin_lock_init(&self->lock);
  1431. init_timer(&self->rx_defer_timer);
  1432. self->capability = id->driver_info;
  1433. self->needspatch = ((self->capability & IUC_STIR421X) != 0);
  1434. /* Create all of the needed urbs */
  1435. if (self->capability & IUC_STIR421X) {
  1436. self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
  1437. self->header_length = USB_IRDA_STIR421X_HEADER;
  1438. } else {
  1439. self->max_rx_urb = IU_MAX_RX_URBS;
  1440. self->header_length = USB_IRDA_HEADER;
  1441. }
  1442. self->rx_urb = kcalloc(self->max_rx_urb, sizeof(struct urb *),
  1443. GFP_KERNEL);
  1444. if (!self->rx_urb)
  1445. goto err_free_net;
  1446. for (i = 0; i < self->max_rx_urb; i++) {
  1447. self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1448. if (!self->rx_urb[i]) {
  1449. goto err_out_1;
  1450. }
  1451. }
  1452. self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1453. if (!self->tx_urb) {
  1454. goto err_out_1;
  1455. }
  1456. self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
  1457. if (!self->speed_urb) {
  1458. goto err_out_2;
  1459. }
  1460. /* Is this really necessary? (no, except maybe for broken devices) */
  1461. if (usb_reset_configuration (dev) < 0) {
  1462. err("reset_configuration failed");
  1463. ret = -EIO;
  1464. goto err_out_3;
  1465. }
  1466. /* Is this really necessary? */
  1467. /* Note : some driver do hardcode the interface number, some others
  1468. * specify an alternate, but very few driver do like this.
  1469. * Jean II */
  1470. ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
  1471. IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
  1472. switch (ret) {
  1473. case 0:
  1474. break;
  1475. case -EPIPE: /* -EPIPE = -32 */
  1476. /* Martin Diehl says if we get a -EPIPE we should
  1477. * be fine and we don't need to do a usb_clear_halt().
  1478. * - Jean II */
  1479. IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __func__);
  1480. break;
  1481. default:
  1482. IRDA_DEBUG(0, "%s(), Unknown error %d\n", __func__, ret);
  1483. ret = -EIO;
  1484. goto err_out_3;
  1485. }
  1486. /* Find our endpoints */
  1487. interface = intf->cur_altsetting;
  1488. if(!irda_usb_parse_endpoints(self, interface->endpoint,
  1489. interface->desc.bNumEndpoints)) {
  1490. IRDA_ERROR("%s(), Bogus endpoints...\n", __func__);
  1491. ret = -EIO;
  1492. goto err_out_3;
  1493. }
  1494. self->usbdev = dev;
  1495. /* Find IrDA class descriptor */
  1496. irda_desc = irda_usb_find_class_desc(intf);
  1497. ret = -ENODEV;
  1498. if (!irda_desc)
  1499. goto err_out_3;
  1500. if (self->needspatch) {
  1501. ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
  1502. 0x02, 0x40, 0, 0, NULL, 0, 500);
  1503. if (ret < 0) {
  1504. IRDA_DEBUG (0, "usb_control_msg failed %d\n", ret);
  1505. goto err_out_3;
  1506. } else {
  1507. mdelay(10);
  1508. }
  1509. }
  1510. self->irda_desc = irda_desc;
  1511. self->present = 1;
  1512. self->netopen = 0;
  1513. self->usbintf = intf;
  1514. /* Allocate the buffer for speed changes */
  1515. /* Don't change this buffer size and allocation without doing
  1516. * some heavy and complete testing. Don't ask why :-(
  1517. * Jean II */
  1518. self->speed_buff = kzalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
  1519. if (!self->speed_buff)
  1520. goto err_out_3;
  1521. self->tx_buff = kzalloc(IRDA_SKB_MAX_MTU + self->header_length,
  1522. GFP_KERNEL);
  1523. if (!self->tx_buff)
  1524. goto err_out_4;
  1525. ret = irda_usb_open(self);
  1526. if (ret)
  1527. goto err_out_5;
  1528. IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
  1529. usb_set_intfdata(intf, self);
  1530. if (self->needspatch) {
  1531. /* Now we fetch and upload the firmware patch */
  1532. ret = stir421x_patch_device(self);
  1533. self->needspatch = (ret < 0);
  1534. if (self->needspatch) {
  1535. IRDA_ERROR("STIR421X: Couldn't upload patch\n");
  1536. goto err_out_6;
  1537. }
  1538. /* replace IrDA class descriptor with what patched device is now reporting */
  1539. irda_desc = irda_usb_find_class_desc (self->usbintf);
  1540. if (!irda_desc) {
  1541. ret = -ENODEV;
  1542. goto err_out_6;
  1543. }
  1544. kfree(self->irda_desc);
  1545. self->irda_desc = irda_desc;
  1546. irda_usb_init_qos(self);
  1547. }
  1548. return 0;
  1549. err_out_6:
  1550. unregister_netdev(self->netdev);
  1551. err_out_5:
  1552. kfree(self->tx_buff);
  1553. err_out_4:
  1554. kfree(self->speed_buff);
  1555. err_out_3:
  1556. /* Free all urbs that we may have created */
  1557. usb_free_urb(self->speed_urb);
  1558. err_out_2:
  1559. usb_free_urb(self->tx_urb);
  1560. err_out_1:
  1561. for (i = 0; i < self->max_rx_urb; i++)
  1562. usb_free_urb(self->rx_urb[i]);
  1563. kfree(self->rx_urb);
  1564. err_free_net:
  1565. free_netdev(net);
  1566. err_out:
  1567. return ret;
  1568. }
  1569. /*------------------------------------------------------------------*/
  1570. /*
  1571. * The current irda-usb device is removed, the USB layer tell us
  1572. * to shut it down...
  1573. * One of the constraints is that when we exit this function,
  1574. * we cannot use the usb_device no more. Gone. Destroyed. kfree().
  1575. * Most other subsystem allow you to destroy the instance at a time
  1576. * when it's convenient to you, to postpone it to a later date, but
  1577. * not the USB subsystem.
  1578. * So, we must make bloody sure that everything gets deactivated.
  1579. * Jean II
  1580. */
  1581. static void irda_usb_disconnect(struct usb_interface *intf)
  1582. {
  1583. unsigned long flags;
  1584. struct irda_usb_cb *self = usb_get_intfdata(intf);
  1585. int i;
  1586. IRDA_DEBUG(1, "%s()\n", __func__);
  1587. usb_set_intfdata(intf, NULL);
  1588. if (!self)
  1589. return;
  1590. /* Make sure that the Tx path is not executing. - Jean II */
  1591. spin_lock_irqsave(&self->lock, flags);
  1592. /* Oups ! We are not there any more.
  1593. * This will stop/desactivate the Tx path. - Jean II */
  1594. self->present = 0;
  1595. /* Kill defered Rx URB */
  1596. del_timer(&self->rx_defer_timer);
  1597. /* We need to have irq enabled to unlink the URBs. That's OK,
  1598. * at this point the Tx path is gone - Jean II */
  1599. spin_unlock_irqrestore(&self->lock, flags);
  1600. /* Hum... Check if networking is still active (avoid races) */
  1601. if((self->netopen) || (self->irlap)) {
  1602. /* Accept no more transmissions */
  1603. /*netif_device_detach(self->netdev);*/
  1604. netif_stop_queue(self->netdev);
  1605. /* Stop all the receive URBs. Must be synchronous. */
  1606. for (i = 0; i < self->max_rx_urb; i++)
  1607. usb_kill_urb(self->rx_urb[i]);
  1608. /* Cancel Tx and speed URB.
  1609. * Make sure it's synchronous to avoid races. */
  1610. usb_kill_urb(self->tx_urb);
  1611. usb_kill_urb(self->speed_urb);
  1612. }
  1613. /* Cleanup the device stuff */
  1614. irda_usb_close(self);
  1615. /* No longer attached to USB bus */
  1616. self->usbdev = NULL;
  1617. self->usbintf = NULL;
  1618. /* Clean up our urbs */
  1619. for (i = 0; i < self->max_rx_urb; i++)
  1620. usb_free_urb(self->rx_urb[i]);
  1621. kfree(self->rx_urb);
  1622. /* Clean up Tx and speed URB */
  1623. usb_free_urb(self->tx_urb);
  1624. usb_free_urb(self->speed_urb);
  1625. /* Free self and network device */
  1626. free_netdev(self->netdev);
  1627. IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __func__);
  1628. }
  1629. #ifdef CONFIG_PM
  1630. /* USB suspend, so power off the transmitter/receiver */
  1631. static int irda_usb_suspend(struct usb_interface *intf, pm_message_t message)
  1632. {
  1633. struct irda_usb_cb *self = usb_get_intfdata(intf);
  1634. int i;
  1635. netif_device_detach(self->netdev);
  1636. if (self->tx_urb != NULL)
  1637. usb_kill_urb(self->tx_urb);
  1638. if (self->speed_urb != NULL)
  1639. usb_kill_urb(self->speed_urb);
  1640. for (i = 0; i < self->max_rx_urb; i++) {
  1641. if (self->rx_urb[i] != NULL)
  1642. usb_kill_urb(self->rx_urb[i]);
  1643. }
  1644. return 0;
  1645. }
  1646. /* Coming out of suspend, so reset hardware */
  1647. static int irda_usb_resume(struct usb_interface *intf)
  1648. {
  1649. struct irda_usb_cb *self = usb_get_intfdata(intf);
  1650. int i;
  1651. for (i = 0; i < self->max_rx_urb; i++) {
  1652. if (self->rx_urb[i] != NULL)
  1653. usb_submit_urb(self->rx_urb[i], GFP_KERNEL);
  1654. }
  1655. netif_device_attach(self->netdev);
  1656. return 0;
  1657. }
  1658. #endif
  1659. /*------------------------------------------------------------------*/
  1660. /*
  1661. * USB device callbacks
  1662. */
  1663. static struct usb_driver irda_driver = {
  1664. .name = "irda-usb",
  1665. .probe = irda_usb_probe,
  1666. .disconnect = irda_usb_disconnect,
  1667. .id_table = dongles,
  1668. #ifdef CONFIG_PM
  1669. .suspend = irda_usb_suspend,
  1670. .resume = irda_usb_resume,
  1671. #endif
  1672. };
  1673. /************************* MODULE CALLBACKS *************************/
  1674. /*
  1675. * Deal with module insertion/removal
  1676. * Mostly tell USB about our existence
  1677. */
  1678. /*------------------------------------------------------------------*/
  1679. /*
  1680. * Module insertion
  1681. */
  1682. static int __init usb_irda_init(void)
  1683. {
  1684. int ret;
  1685. ret = usb_register(&irda_driver);
  1686. if (ret < 0)
  1687. return ret;
  1688. IRDA_MESSAGE("USB IrDA support registered\n");
  1689. return 0;
  1690. }
  1691. module_init(usb_irda_init);
  1692. /*------------------------------------------------------------------*/
  1693. /*
  1694. * Module removal
  1695. */
  1696. static void __exit usb_irda_cleanup(void)
  1697. {
  1698. /* Deregister the driver and remove all pending instances */
  1699. usb_deregister(&irda_driver);
  1700. }
  1701. module_exit(usb_irda_cleanup);
  1702. /*------------------------------------------------------------------*/
  1703. /*
  1704. * Module parameters
  1705. */
  1706. module_param(qos_mtt_bits, int, 0);
  1707. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  1708. MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net>, Jean Tourrilhes <jt@hpl.hp.com> and Nick Fedchik <nick@fedchik.org.ua>");
  1709. MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
  1710. MODULE_LICENSE("GPL");