irda-usb.c 49 KB

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