irda-usb.c 51 KB

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