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