irda-usb.c 59 KB

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