irda-usb.c 60 KB

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