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