irda-usb.c 58 KB

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