irda-usb.c 58 KB

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