irda-usb.c 49 KB

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