irda-usb.c 59 KB

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