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