irda-usb.c 51 KB

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