irda-usb.c 58 KB

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