Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1 | /* zd_usb.c |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include <asm/unaligned.h> |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/firmware.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/usb.h> |
| 27 | #include <net/ieee80211.h> |
| 28 | |
| 29 | #include "zd_def.h" |
| 30 | #include "zd_netdev.h" |
| 31 | #include "zd_mac.h" |
| 32 | #include "zd_usb.h" |
| 33 | #include "zd_util.h" |
| 34 | |
| 35 | static struct usb_device_id usb_ids[] = { |
| 36 | /* ZD1211 */ |
| 37 | { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 }, |
| 38 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 }, |
| 39 | { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 }, |
| 40 | { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 }, |
| 41 | { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 }, |
| 42 | { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | dd2f553 | 2006-07-05 15:57:31 +0100 | [diff] [blame] | 43 | { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 1b86549 | 2006-08-12 17:59:50 +0100 | [diff] [blame] | 44 | { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 12f3930 | 2006-08-12 17:59:51 +0100 | [diff] [blame] | 45 | { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 4ceb7e9 | 2006-08-12 18:00:03 +0100 | [diff] [blame] | 46 | { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | fca2714 | 2006-08-29 23:49:32 +0100 | [diff] [blame^] | 47 | { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 48 | /* ZD1211B */ |
| 49 | { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B }, |
| 50 | { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | dd2f553 | 2006-07-05 15:57:31 +0100 | [diff] [blame] | 51 | { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B }, |
Ulrich Kunitz | 943599e | 2006-08-12 18:00:05 +0100 | [diff] [blame] | 52 | { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 53 | /* "Driverless" devices that need ejecting */ |
| 54 | { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 55 | {} |
| 56 | }; |
| 57 | |
| 58 | MODULE_LICENSE("GPL"); |
| 59 | MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip."); |
| 60 | MODULE_AUTHOR("Ulrich Kunitz"); |
| 61 | MODULE_AUTHOR("Daniel Drake"); |
| 62 | MODULE_VERSION("1.0"); |
| 63 | MODULE_DEVICE_TABLE(usb, usb_ids); |
| 64 | |
| 65 | #define FW_ZD1211_PREFIX "zd1211/zd1211_" |
| 66 | #define FW_ZD1211B_PREFIX "zd1211/zd1211b_" |
| 67 | |
| 68 | /* register address handling */ |
| 69 | |
| 70 | #ifdef DEBUG |
| 71 | static int check_addr(struct zd_usb *usb, zd_addr_t addr) |
| 72 | { |
| 73 | u32 base = ZD_ADDR_BASE(addr); |
| 74 | u32 offset = ZD_OFFSET(addr); |
| 75 | |
| 76 | if ((u32)addr & ADDR_ZERO_MASK) |
| 77 | goto invalid_address; |
| 78 | switch (base) { |
| 79 | case USB_BASE: |
| 80 | break; |
| 81 | case CR_BASE: |
| 82 | if (offset > CR_MAX_OFFSET) { |
| 83 | dev_dbg(zd_usb_dev(usb), |
| 84 | "CR offset %#010x larger than" |
| 85 | " CR_MAX_OFFSET %#10x\n", |
| 86 | offset, CR_MAX_OFFSET); |
| 87 | goto invalid_address; |
| 88 | } |
| 89 | if (offset & 1) { |
| 90 | dev_dbg(zd_usb_dev(usb), |
| 91 | "CR offset %#010x is not a multiple of 2\n", |
| 92 | offset); |
| 93 | goto invalid_address; |
| 94 | } |
| 95 | break; |
| 96 | case E2P_BASE: |
| 97 | if (offset > E2P_MAX_OFFSET) { |
| 98 | dev_dbg(zd_usb_dev(usb), |
| 99 | "E2P offset %#010x larger than" |
| 100 | " E2P_MAX_OFFSET %#010x\n", |
| 101 | offset, E2P_MAX_OFFSET); |
| 102 | goto invalid_address; |
| 103 | } |
| 104 | break; |
| 105 | case FW_BASE: |
| 106 | if (!usb->fw_base_offset) { |
| 107 | dev_dbg(zd_usb_dev(usb), |
| 108 | "ERROR: fw base offset has not been set\n"); |
| 109 | return -EAGAIN; |
| 110 | } |
| 111 | if (offset > FW_MAX_OFFSET) { |
| 112 | dev_dbg(zd_usb_dev(usb), |
| 113 | "FW offset %#10x is larger than" |
| 114 | " FW_MAX_OFFSET %#010x\n", |
| 115 | offset, FW_MAX_OFFSET); |
| 116 | goto invalid_address; |
| 117 | } |
| 118 | break; |
| 119 | default: |
| 120 | dev_dbg(zd_usb_dev(usb), |
| 121 | "address has unsupported base %#010x\n", addr); |
| 122 | goto invalid_address; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | invalid_address: |
| 127 | dev_dbg(zd_usb_dev(usb), |
| 128 | "ERROR: invalid address: %#010x\n", addr); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | #endif /* DEBUG */ |
| 132 | |
| 133 | static u16 usb_addr(struct zd_usb *usb, zd_addr_t addr) |
| 134 | { |
| 135 | u32 base; |
| 136 | u16 offset; |
| 137 | |
| 138 | base = ZD_ADDR_BASE(addr); |
| 139 | offset = ZD_OFFSET(addr); |
| 140 | |
| 141 | ZD_ASSERT(check_addr(usb, addr) == 0); |
| 142 | |
| 143 | switch (base) { |
| 144 | case CR_BASE: |
| 145 | offset += CR_BASE_OFFSET; |
| 146 | break; |
| 147 | case E2P_BASE: |
| 148 | offset += E2P_BASE_OFFSET; |
| 149 | break; |
| 150 | case FW_BASE: |
| 151 | offset += usb->fw_base_offset; |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | return offset; |
| 156 | } |
| 157 | |
| 158 | /* USB device initialization */ |
| 159 | |
| 160 | static int request_fw_file( |
| 161 | const struct firmware **fw, const char *name, struct device *device) |
| 162 | { |
| 163 | int r; |
| 164 | |
| 165 | dev_dbg_f(device, "fw name %s\n", name); |
| 166 | |
| 167 | r = request_firmware(fw, name, device); |
| 168 | if (r) |
| 169 | dev_err(device, |
| 170 | "Could not load firmware file %s. Error number %d\n", |
| 171 | name, r); |
| 172 | return r; |
| 173 | } |
| 174 | |
| 175 | static inline u16 get_bcdDevice(const struct usb_device *udev) |
| 176 | { |
| 177 | return le16_to_cpu(udev->descriptor.bcdDevice); |
| 178 | } |
| 179 | |
| 180 | enum upload_code_flags { |
| 181 | REBOOT = 1, |
| 182 | }; |
| 183 | |
| 184 | /* Ensures that MAX_TRANSFER_SIZE is even. */ |
| 185 | #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1) |
| 186 | |
| 187 | static int upload_code(struct usb_device *udev, |
| 188 | const u8 *data, size_t size, u16 code_offset, int flags) |
| 189 | { |
| 190 | u8 *p; |
| 191 | int r; |
| 192 | |
| 193 | /* USB request blocks need "kmalloced" buffers. |
| 194 | */ |
| 195 | p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL); |
| 196 | if (!p) { |
| 197 | dev_err(&udev->dev, "out of memory\n"); |
| 198 | r = -ENOMEM; |
| 199 | goto error; |
| 200 | } |
| 201 | |
| 202 | size &= ~1; |
| 203 | while (size > 0) { |
| 204 | size_t transfer_size = size <= MAX_TRANSFER_SIZE ? |
| 205 | size : MAX_TRANSFER_SIZE; |
| 206 | |
| 207 | dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size); |
| 208 | |
| 209 | memcpy(p, data, transfer_size); |
| 210 | r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 211 | USB_REQ_FIRMWARE_DOWNLOAD, |
| 212 | USB_DIR_OUT | USB_TYPE_VENDOR, |
| 213 | code_offset, 0, p, transfer_size, 1000 /* ms */); |
| 214 | if (r < 0) { |
| 215 | dev_err(&udev->dev, |
| 216 | "USB control request for firmware upload" |
| 217 | " failed. Error number %d\n", r); |
| 218 | goto error; |
| 219 | } |
| 220 | transfer_size = r & ~1; |
| 221 | |
| 222 | size -= transfer_size; |
| 223 | data += transfer_size; |
| 224 | code_offset += transfer_size/sizeof(u16); |
| 225 | } |
| 226 | |
| 227 | if (flags & REBOOT) { |
| 228 | u8 ret; |
| 229 | |
| 230 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 231 | USB_REQ_FIRMWARE_CONFIRM, |
| 232 | USB_DIR_IN | USB_TYPE_VENDOR, |
| 233 | 0, 0, &ret, sizeof(ret), 5000 /* ms */); |
| 234 | if (r != sizeof(ret)) { |
| 235 | dev_err(&udev->dev, |
| 236 | "control request firmeware confirmation failed." |
| 237 | " Return value %d\n", r); |
| 238 | if (r >= 0) |
| 239 | r = -ENODEV; |
| 240 | goto error; |
| 241 | } |
| 242 | if (ret & 0x80) { |
| 243 | dev_err(&udev->dev, |
| 244 | "Internal error while downloading." |
| 245 | " Firmware confirm return value %#04x\n", |
| 246 | (unsigned int)ret); |
| 247 | r = -ENODEV; |
| 248 | goto error; |
| 249 | } |
| 250 | dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n", |
| 251 | (unsigned int)ret); |
| 252 | } |
| 253 | |
| 254 | r = 0; |
| 255 | error: |
| 256 | kfree(p); |
| 257 | return r; |
| 258 | } |
| 259 | |
| 260 | static u16 get_word(const void *data, u16 offset) |
| 261 | { |
| 262 | const __le16 *p = data; |
| 263 | return le16_to_cpu(p[offset]); |
| 264 | } |
| 265 | |
| 266 | static char *get_fw_name(char *buffer, size_t size, u8 device_type, |
| 267 | const char* postfix) |
| 268 | { |
| 269 | scnprintf(buffer, size, "%s%s", |
| 270 | device_type == DEVICE_ZD1211B ? |
| 271 | FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX, |
| 272 | postfix); |
| 273 | return buffer; |
| 274 | } |
| 275 | |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 276 | static int handle_version_mismatch(struct usb_device *udev, u8 device_type, |
| 277 | const struct firmware *ub_fw) |
| 278 | { |
| 279 | const struct firmware *ur_fw = NULL; |
| 280 | int offset; |
| 281 | int r = 0; |
| 282 | char fw_name[128]; |
| 283 | |
| 284 | r = request_fw_file(&ur_fw, |
| 285 | get_fw_name(fw_name, sizeof(fw_name), device_type, "ur"), |
| 286 | &udev->dev); |
| 287 | if (r) |
| 288 | goto error; |
| 289 | |
| 290 | r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START_OFFSET, |
| 291 | REBOOT); |
| 292 | if (r) |
| 293 | goto error; |
| 294 | |
| 295 | offset = ((EEPROM_REGS_OFFSET + EEPROM_REGS_SIZE) * sizeof(u16)); |
| 296 | r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset, |
| 297 | E2P_BASE_OFFSET + EEPROM_REGS_SIZE, REBOOT); |
| 298 | |
| 299 | /* At this point, the vendor driver downloads the whole firmware |
| 300 | * image, hacks around with version IDs, and uploads it again, |
| 301 | * completely overwriting the boot code. We do not do this here as |
| 302 | * it is not required on any tested devices, and it is suspected to |
| 303 | * cause problems. */ |
| 304 | error: |
| 305 | release_firmware(ur_fw); |
| 306 | return r; |
| 307 | } |
| 308 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 309 | static int upload_firmware(struct usb_device *udev, u8 device_type) |
| 310 | { |
| 311 | int r; |
| 312 | u16 fw_bcdDevice; |
| 313 | u16 bcdDevice; |
| 314 | const struct firmware *ub_fw = NULL; |
| 315 | const struct firmware *uph_fw = NULL; |
| 316 | char fw_name[128]; |
| 317 | |
| 318 | bcdDevice = get_bcdDevice(udev); |
| 319 | |
| 320 | r = request_fw_file(&ub_fw, |
| 321 | get_fw_name(fw_name, sizeof(fw_name), device_type, "ub"), |
| 322 | &udev->dev); |
| 323 | if (r) |
| 324 | goto error; |
| 325 | |
| 326 | fw_bcdDevice = get_word(ub_fw->data, EEPROM_REGS_OFFSET); |
| 327 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 328 | if (fw_bcdDevice != bcdDevice) { |
| 329 | dev_info(&udev->dev, |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 330 | "firmware version %#06x and device bootcode version " |
| 331 | "%#06x differ\n", fw_bcdDevice, bcdDevice); |
| 332 | if (bcdDevice <= 0x4313) |
| 333 | dev_warn(&udev->dev, "device has old bootcode, please " |
| 334 | "report success or failure\n"); |
| 335 | |
| 336 | r = handle_version_mismatch(udev, device_type, ub_fw); |
| 337 | if (r) |
| 338 | goto error; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 339 | } else { |
| 340 | dev_dbg_f(&udev->dev, |
| 341 | "firmware device id %#06x is equal to the " |
| 342 | "actual device id\n", fw_bcdDevice); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | r = request_fw_file(&uph_fw, |
| 347 | get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"), |
| 348 | &udev->dev); |
| 349 | if (r) |
| 350 | goto error; |
| 351 | |
| 352 | r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START_OFFSET, |
| 353 | REBOOT); |
| 354 | if (r) { |
| 355 | dev_err(&udev->dev, |
| 356 | "Could not upload firmware code uph. Error number %d\n", |
| 357 | r); |
| 358 | } |
| 359 | |
| 360 | /* FALL-THROUGH */ |
| 361 | error: |
| 362 | release_firmware(ub_fw); |
| 363 | release_firmware(uph_fw); |
| 364 | return r; |
| 365 | } |
| 366 | |
| 367 | static void disable_read_regs_int(struct zd_usb *usb) |
| 368 | { |
| 369 | struct zd_usb_interrupt *intr = &usb->intr; |
| 370 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 371 | spin_lock(&intr->lock); |
| 372 | intr->read_regs_enabled = 0; |
| 373 | spin_unlock(&intr->lock); |
| 374 | } |
| 375 | |
| 376 | #define urb_dev(urb) (&(urb)->dev->dev) |
| 377 | |
| 378 | static inline void handle_regs_int(struct urb *urb) |
| 379 | { |
| 380 | struct zd_usb *usb = urb->context; |
| 381 | struct zd_usb_interrupt *intr = &usb->intr; |
| 382 | int len; |
| 383 | |
| 384 | ZD_ASSERT(in_interrupt()); |
| 385 | spin_lock(&intr->lock); |
| 386 | |
| 387 | if (intr->read_regs_enabled) { |
| 388 | intr->read_regs.length = len = urb->actual_length; |
| 389 | |
| 390 | if (len > sizeof(intr->read_regs.buffer)) |
| 391 | len = sizeof(intr->read_regs.buffer); |
| 392 | memcpy(intr->read_regs.buffer, urb->transfer_buffer, len); |
| 393 | intr->read_regs_enabled = 0; |
| 394 | complete(&intr->read_regs.completion); |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n"); |
| 399 | out: |
| 400 | spin_unlock(&intr->lock); |
| 401 | } |
| 402 | |
| 403 | static inline void handle_retry_failed_int(struct urb *urb) |
| 404 | { |
| 405 | dev_dbg_f(urb_dev(urb), "retry failed interrupt\n"); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | static void int_urb_complete(struct urb *urb, struct pt_regs *pt_regs) |
| 410 | { |
| 411 | int r; |
| 412 | struct usb_int_header *hdr; |
| 413 | |
| 414 | switch (urb->status) { |
| 415 | case 0: |
| 416 | break; |
| 417 | case -ESHUTDOWN: |
| 418 | case -EINVAL: |
| 419 | case -ENODEV: |
| 420 | case -ENOENT: |
| 421 | case -ECONNRESET: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 422 | case -EPIPE: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 423 | goto kfree; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 424 | default: |
| 425 | goto resubmit; |
| 426 | } |
| 427 | |
| 428 | if (urb->actual_length < sizeof(hdr)) { |
| 429 | dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb); |
| 430 | goto resubmit; |
| 431 | } |
| 432 | |
| 433 | hdr = urb->transfer_buffer; |
| 434 | if (hdr->type != USB_INT_TYPE) { |
| 435 | dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb); |
| 436 | goto resubmit; |
| 437 | } |
| 438 | |
| 439 | switch (hdr->id) { |
| 440 | case USB_INT_ID_REGS: |
| 441 | handle_regs_int(urb); |
| 442 | break; |
| 443 | case USB_INT_ID_RETRY_FAILED: |
| 444 | handle_retry_failed_int(urb); |
| 445 | break; |
| 446 | default: |
| 447 | dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb, |
| 448 | (unsigned int)hdr->id); |
| 449 | goto resubmit; |
| 450 | } |
| 451 | |
| 452 | resubmit: |
| 453 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 454 | if (r) { |
| 455 | dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb); |
| 456 | goto kfree; |
| 457 | } |
| 458 | return; |
| 459 | kfree: |
| 460 | kfree(urb->transfer_buffer); |
| 461 | } |
| 462 | |
| 463 | static inline int int_urb_interval(struct usb_device *udev) |
| 464 | { |
| 465 | switch (udev->speed) { |
| 466 | case USB_SPEED_HIGH: |
| 467 | return 4; |
| 468 | case USB_SPEED_LOW: |
| 469 | return 10; |
| 470 | case USB_SPEED_FULL: |
| 471 | default: |
| 472 | return 1; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static inline int usb_int_enabled(struct zd_usb *usb) |
| 477 | { |
| 478 | unsigned long flags; |
| 479 | struct zd_usb_interrupt *intr = &usb->intr; |
| 480 | struct urb *urb; |
| 481 | |
| 482 | spin_lock_irqsave(&intr->lock, flags); |
| 483 | urb = intr->urb; |
| 484 | spin_unlock_irqrestore(&intr->lock, flags); |
| 485 | return urb != NULL; |
| 486 | } |
| 487 | |
| 488 | int zd_usb_enable_int(struct zd_usb *usb) |
| 489 | { |
| 490 | int r; |
| 491 | struct usb_device *udev; |
| 492 | struct zd_usb_interrupt *intr = &usb->intr; |
| 493 | void *transfer_buffer = NULL; |
| 494 | struct urb *urb; |
| 495 | |
| 496 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 497 | |
| 498 | urb = usb_alloc_urb(0, GFP_NOFS); |
| 499 | if (!urb) { |
| 500 | r = -ENOMEM; |
| 501 | goto out; |
| 502 | } |
| 503 | |
| 504 | ZD_ASSERT(!irqs_disabled()); |
| 505 | spin_lock_irq(&intr->lock); |
| 506 | if (intr->urb) { |
| 507 | spin_unlock_irq(&intr->lock); |
| 508 | r = 0; |
| 509 | goto error_free_urb; |
| 510 | } |
| 511 | intr->urb = urb; |
| 512 | spin_unlock_irq(&intr->lock); |
| 513 | |
| 514 | /* TODO: make it a DMA buffer */ |
| 515 | r = -ENOMEM; |
| 516 | transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS); |
| 517 | if (!transfer_buffer) { |
| 518 | dev_dbg_f(zd_usb_dev(usb), |
| 519 | "couldn't allocate transfer_buffer\n"); |
| 520 | goto error_set_urb_null; |
| 521 | } |
| 522 | |
| 523 | udev = zd_usb_to_usbdev(usb); |
| 524 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), |
| 525 | transfer_buffer, USB_MAX_EP_INT_BUFFER, |
| 526 | int_urb_complete, usb, |
| 527 | intr->interval); |
| 528 | |
| 529 | dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb); |
| 530 | r = usb_submit_urb(urb, GFP_NOFS); |
| 531 | if (r) { |
| 532 | dev_dbg_f(zd_usb_dev(usb), |
| 533 | "Couldn't submit urb. Error number %d\n", r); |
| 534 | goto error; |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | error: |
| 539 | kfree(transfer_buffer); |
| 540 | error_set_urb_null: |
| 541 | spin_lock_irq(&intr->lock); |
| 542 | intr->urb = NULL; |
| 543 | spin_unlock_irq(&intr->lock); |
| 544 | error_free_urb: |
| 545 | usb_free_urb(urb); |
| 546 | out: |
| 547 | return r; |
| 548 | } |
| 549 | |
| 550 | void zd_usb_disable_int(struct zd_usb *usb) |
| 551 | { |
| 552 | unsigned long flags; |
| 553 | struct zd_usb_interrupt *intr = &usb->intr; |
| 554 | struct urb *urb; |
| 555 | |
| 556 | spin_lock_irqsave(&intr->lock, flags); |
| 557 | urb = intr->urb; |
| 558 | if (!urb) { |
| 559 | spin_unlock_irqrestore(&intr->lock, flags); |
| 560 | return; |
| 561 | } |
| 562 | intr->urb = NULL; |
| 563 | spin_unlock_irqrestore(&intr->lock, flags); |
| 564 | |
| 565 | usb_kill_urb(urb); |
| 566 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); |
| 567 | usb_free_urb(urb); |
| 568 | } |
| 569 | |
| 570 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, |
| 571 | unsigned int length) |
| 572 | { |
| 573 | int i; |
| 574 | struct zd_mac *mac = zd_usb_to_mac(usb); |
| 575 | const struct rx_length_info *length_info; |
| 576 | |
| 577 | if (length < sizeof(struct rx_length_info)) { |
| 578 | /* It's not a complete packet anyhow. */ |
| 579 | return; |
| 580 | } |
| 581 | length_info = (struct rx_length_info *) |
| 582 | (buffer + length - sizeof(struct rx_length_info)); |
| 583 | |
| 584 | /* It might be that three frames are merged into a single URB |
| 585 | * transaction. We have to check for the length info tag. |
| 586 | * |
| 587 | * While testing we discovered that length_info might be unaligned, |
| 588 | * because if USB transactions are merged, the last packet will not |
| 589 | * be padded. Unaligned access might also happen if the length_info |
| 590 | * structure is not present. |
| 591 | */ |
Ulrich Kunitz | b269825 | 2006-08-01 23:43:34 +0200 | [diff] [blame] | 592 | if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG)) |
| 593 | { |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 594 | unsigned int l, k, n; |
| 595 | for (i = 0, l = 0;; i++) { |
Ulrich Kunitz | b269825 | 2006-08-01 23:43:34 +0200 | [diff] [blame] | 596 | k = le16_to_cpu(get_unaligned(&length_info->length[i])); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 597 | n = l+k; |
| 598 | if (n > length) |
| 599 | return; |
| 600 | zd_mac_rx(mac, buffer+l, k); |
| 601 | if (i >= 2) |
| 602 | return; |
| 603 | l = (n+3) & ~3; |
| 604 | } |
| 605 | } else { |
| 606 | zd_mac_rx(mac, buffer, length); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static void rx_urb_complete(struct urb *urb, struct pt_regs *pt_regs) |
| 611 | { |
| 612 | struct zd_usb *usb; |
| 613 | struct zd_usb_rx *rx; |
| 614 | const u8 *buffer; |
| 615 | unsigned int length; |
| 616 | |
| 617 | switch (urb->status) { |
| 618 | case 0: |
| 619 | break; |
| 620 | case -ESHUTDOWN: |
| 621 | case -EINVAL: |
| 622 | case -ENODEV: |
| 623 | case -ENOENT: |
| 624 | case -ECONNRESET: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 625 | case -EPIPE: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 626 | return; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 627 | default: |
| 628 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 629 | goto resubmit; |
| 630 | } |
| 631 | |
| 632 | buffer = urb->transfer_buffer; |
| 633 | length = urb->actual_length; |
| 634 | usb = urb->context; |
| 635 | rx = &usb->rx; |
| 636 | |
| 637 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { |
| 638 | /* If there is an old first fragment, we don't care. */ |
| 639 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); |
| 640 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); |
| 641 | spin_lock(&rx->lock); |
| 642 | memcpy(rx->fragment, buffer, length); |
| 643 | rx->fragment_length = length; |
| 644 | spin_unlock(&rx->lock); |
| 645 | goto resubmit; |
| 646 | } |
| 647 | |
| 648 | spin_lock(&rx->lock); |
| 649 | if (rx->fragment_length > 0) { |
| 650 | /* We are on a second fragment, we believe */ |
| 651 | ZD_ASSERT(length + rx->fragment_length <= |
| 652 | ARRAY_SIZE(rx->fragment)); |
| 653 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); |
| 654 | memcpy(rx->fragment+rx->fragment_length, buffer, length); |
| 655 | handle_rx_packet(usb, rx->fragment, |
| 656 | rx->fragment_length + length); |
| 657 | rx->fragment_length = 0; |
| 658 | spin_unlock(&rx->lock); |
| 659 | } else { |
| 660 | spin_unlock(&rx->lock); |
| 661 | handle_rx_packet(usb, buffer, length); |
| 662 | } |
| 663 | |
| 664 | resubmit: |
| 665 | usb_submit_urb(urb, GFP_ATOMIC); |
| 666 | } |
| 667 | |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 668 | static struct urb *alloc_urb(struct zd_usb *usb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 669 | { |
| 670 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
| 671 | struct urb *urb; |
| 672 | void *buffer; |
| 673 | |
| 674 | urb = usb_alloc_urb(0, GFP_NOFS); |
| 675 | if (!urb) |
| 676 | return NULL; |
| 677 | buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS, |
| 678 | &urb->transfer_dma); |
| 679 | if (!buffer) { |
| 680 | usb_free_urb(urb); |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), |
| 685 | buffer, USB_MAX_RX_SIZE, |
| 686 | rx_urb_complete, usb); |
| 687 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 688 | |
| 689 | return urb; |
| 690 | } |
| 691 | |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 692 | static void free_urb(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 693 | { |
| 694 | if (!urb) |
| 695 | return; |
| 696 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
| 697 | urb->transfer_buffer, urb->transfer_dma); |
| 698 | usb_free_urb(urb); |
| 699 | } |
| 700 | |
| 701 | int zd_usb_enable_rx(struct zd_usb *usb) |
| 702 | { |
| 703 | int i, r; |
| 704 | struct zd_usb_rx *rx = &usb->rx; |
| 705 | struct urb **urbs; |
| 706 | |
| 707 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 708 | |
| 709 | r = -ENOMEM; |
| 710 | urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS); |
| 711 | if (!urbs) |
| 712 | goto error; |
| 713 | for (i = 0; i < URBS_COUNT; i++) { |
| 714 | urbs[i] = alloc_urb(usb); |
| 715 | if (!urbs[i]) |
| 716 | goto error; |
| 717 | } |
| 718 | |
| 719 | ZD_ASSERT(!irqs_disabled()); |
| 720 | spin_lock_irq(&rx->lock); |
| 721 | if (rx->urbs) { |
| 722 | spin_unlock_irq(&rx->lock); |
| 723 | r = 0; |
| 724 | goto error; |
| 725 | } |
| 726 | rx->urbs = urbs; |
| 727 | rx->urbs_count = URBS_COUNT; |
| 728 | spin_unlock_irq(&rx->lock); |
| 729 | |
| 730 | for (i = 0; i < URBS_COUNT; i++) { |
| 731 | r = usb_submit_urb(urbs[i], GFP_NOFS); |
| 732 | if (r) |
| 733 | goto error_submit; |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | error_submit: |
| 738 | for (i = 0; i < URBS_COUNT; i++) { |
| 739 | usb_kill_urb(urbs[i]); |
| 740 | } |
| 741 | spin_lock_irq(&rx->lock); |
| 742 | rx->urbs = NULL; |
| 743 | rx->urbs_count = 0; |
| 744 | spin_unlock_irq(&rx->lock); |
| 745 | error: |
| 746 | if (urbs) { |
| 747 | for (i = 0; i < URBS_COUNT; i++) |
| 748 | free_urb(urbs[i]); |
| 749 | } |
| 750 | return r; |
| 751 | } |
| 752 | |
| 753 | void zd_usb_disable_rx(struct zd_usb *usb) |
| 754 | { |
| 755 | int i; |
| 756 | unsigned long flags; |
| 757 | struct urb **urbs; |
| 758 | unsigned int count; |
| 759 | struct zd_usb_rx *rx = &usb->rx; |
| 760 | |
| 761 | spin_lock_irqsave(&rx->lock, flags); |
| 762 | urbs = rx->urbs; |
| 763 | count = rx->urbs_count; |
| 764 | spin_unlock_irqrestore(&rx->lock, flags); |
| 765 | if (!urbs) |
| 766 | return; |
| 767 | |
| 768 | for (i = 0; i < count; i++) { |
| 769 | usb_kill_urb(urbs[i]); |
| 770 | free_urb(urbs[i]); |
| 771 | } |
| 772 | kfree(urbs); |
| 773 | |
| 774 | spin_lock_irqsave(&rx->lock, flags); |
| 775 | rx->urbs = NULL; |
| 776 | rx->urbs_count = 0; |
| 777 | spin_unlock_irqrestore(&rx->lock, flags); |
| 778 | } |
| 779 | |
| 780 | static void tx_urb_complete(struct urb *urb, struct pt_regs *pt_regs) |
| 781 | { |
| 782 | int r; |
| 783 | |
| 784 | switch (urb->status) { |
| 785 | case 0: |
| 786 | break; |
| 787 | case -ESHUTDOWN: |
| 788 | case -EINVAL: |
| 789 | case -ENODEV: |
| 790 | case -ENOENT: |
| 791 | case -ECONNRESET: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 792 | case -EPIPE: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 793 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 794 | break; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 795 | default: |
| 796 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 797 | goto resubmit; |
| 798 | } |
| 799 | free_urb: |
| 800 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
| 801 | urb->transfer_buffer, urb->transfer_dma); |
| 802 | usb_free_urb(urb); |
| 803 | return; |
| 804 | resubmit: |
| 805 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 806 | if (r) { |
| 807 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); |
| 808 | goto free_urb; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* Puts the frame on the USB endpoint. It doesn't wait for |
| 813 | * completion. The frame must contain the control set. |
| 814 | */ |
| 815 | int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length) |
| 816 | { |
| 817 | int r; |
| 818 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
| 819 | struct urb *urb; |
| 820 | void *buffer; |
| 821 | |
| 822 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 823 | if (!urb) { |
| 824 | r = -ENOMEM; |
| 825 | goto out; |
| 826 | } |
| 827 | |
| 828 | buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC, |
| 829 | &urb->transfer_dma); |
| 830 | if (!buffer) { |
| 831 | r = -ENOMEM; |
| 832 | goto error_free_urb; |
| 833 | } |
| 834 | memcpy(buffer, frame, length); |
| 835 | |
| 836 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
| 837 | buffer, length, tx_urb_complete, NULL); |
| 838 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 839 | |
| 840 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 841 | if (r) |
| 842 | goto error; |
| 843 | return 0; |
| 844 | error: |
| 845 | usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer, |
| 846 | urb->transfer_dma); |
| 847 | error_free_urb: |
| 848 | usb_free_urb(urb); |
| 849 | out: |
| 850 | return r; |
| 851 | } |
| 852 | |
| 853 | static inline void init_usb_interrupt(struct zd_usb *usb) |
| 854 | { |
| 855 | struct zd_usb_interrupt *intr = &usb->intr; |
| 856 | |
| 857 | spin_lock_init(&intr->lock); |
| 858 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); |
| 859 | init_completion(&intr->read_regs.completion); |
| 860 | intr->read_regs.cr_int_addr = cpu_to_le16(usb_addr(usb, CR_INTERRUPT)); |
| 861 | } |
| 862 | |
| 863 | static inline void init_usb_rx(struct zd_usb *usb) |
| 864 | { |
| 865 | struct zd_usb_rx *rx = &usb->rx; |
| 866 | spin_lock_init(&rx->lock); |
| 867 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { |
| 868 | rx->usb_packet_size = 512; |
| 869 | } else { |
| 870 | rx->usb_packet_size = 64; |
| 871 | } |
| 872 | ZD_ASSERT(rx->fragment_length == 0); |
| 873 | } |
| 874 | |
| 875 | static inline void init_usb_tx(struct zd_usb *usb) |
| 876 | { |
| 877 | /* FIXME: at this point we will allocate a fixed number of urb's for |
| 878 | * use in a cyclic scheme */ |
| 879 | } |
| 880 | |
| 881 | void zd_usb_init(struct zd_usb *usb, struct net_device *netdev, |
| 882 | struct usb_interface *intf) |
| 883 | { |
| 884 | memset(usb, 0, sizeof(*usb)); |
| 885 | usb->intf = usb_get_intf(intf); |
| 886 | usb_set_intfdata(usb->intf, netdev); |
| 887 | init_usb_interrupt(usb); |
| 888 | init_usb_tx(usb); |
| 889 | init_usb_rx(usb); |
| 890 | } |
| 891 | |
| 892 | int zd_usb_init_hw(struct zd_usb *usb) |
| 893 | { |
| 894 | int r; |
| 895 | struct zd_chip *chip = zd_usb_to_chip(usb); |
| 896 | |
| 897 | ZD_ASSERT(mutex_is_locked(&chip->mutex)); |
| 898 | r = zd_ioread16_locked(chip, &usb->fw_base_offset, |
| 899 | USB_REG((u16)FW_BASE_ADDR_OFFSET)); |
| 900 | if (r) |
| 901 | return r; |
| 902 | dev_dbg_f(zd_usb_dev(usb), "fw_base_offset: %#06hx\n", |
| 903 | usb->fw_base_offset); |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | void zd_usb_clear(struct zd_usb *usb) |
| 909 | { |
| 910 | usb_set_intfdata(usb->intf, NULL); |
| 911 | usb_put_intf(usb->intf); |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 912 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 913 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
| 914 | } |
| 915 | |
| 916 | static const char *speed(enum usb_device_speed speed) |
| 917 | { |
| 918 | switch (speed) { |
| 919 | case USB_SPEED_LOW: |
| 920 | return "low"; |
| 921 | case USB_SPEED_FULL: |
| 922 | return "full"; |
| 923 | case USB_SPEED_HIGH: |
| 924 | return "high"; |
| 925 | default: |
| 926 | return "unknown speed"; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) |
| 931 | { |
| 932 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", |
| 933 | le16_to_cpu(udev->descriptor.idVendor), |
| 934 | le16_to_cpu(udev->descriptor.idProduct), |
| 935 | get_bcdDevice(udev), |
| 936 | speed(udev->speed)); |
| 937 | } |
| 938 | |
| 939 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) |
| 940 | { |
| 941 | struct usb_device *udev = interface_to_usbdev(usb->intf); |
| 942 | return scnprint_id(udev, buffer, size); |
| 943 | } |
| 944 | |
| 945 | #ifdef DEBUG |
| 946 | static void print_id(struct usb_device *udev) |
| 947 | { |
| 948 | char buffer[40]; |
| 949 | |
| 950 | scnprint_id(udev, buffer, sizeof(buffer)); |
| 951 | buffer[sizeof(buffer)-1] = 0; |
| 952 | dev_dbg_f(&udev->dev, "%s\n", buffer); |
| 953 | } |
| 954 | #else |
| 955 | #define print_id(udev) do { } while (0) |
| 956 | #endif |
| 957 | |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 958 | static int eject_installer(struct usb_interface *intf) |
| 959 | { |
| 960 | struct usb_device *udev = interface_to_usbdev(intf); |
| 961 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; |
| 962 | struct usb_endpoint_descriptor *endpoint; |
| 963 | unsigned char *cmd; |
| 964 | u8 bulk_out_ep; |
| 965 | int r; |
| 966 | |
| 967 | /* Find bulk out endpoint */ |
| 968 | endpoint = &iface_desc->endpoint[1].desc; |
| 969 | if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT && |
| 970 | (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| 971 | USB_ENDPOINT_XFER_BULK) { |
| 972 | bulk_out_ep = endpoint->bEndpointAddress; |
| 973 | } else { |
| 974 | dev_err(&udev->dev, |
| 975 | "zd1211rw: Could not find bulk out endpoint\n"); |
| 976 | return -ENODEV; |
| 977 | } |
| 978 | |
| 979 | cmd = kzalloc(31, GFP_KERNEL); |
| 980 | if (cmd == NULL) |
| 981 | return -ENODEV; |
| 982 | |
| 983 | /* USB bulk command block */ |
| 984 | cmd[0] = 0x55; /* bulk command signature */ |
| 985 | cmd[1] = 0x53; /* bulk command signature */ |
| 986 | cmd[2] = 0x42; /* bulk command signature */ |
| 987 | cmd[3] = 0x43; /* bulk command signature */ |
| 988 | cmd[14] = 6; /* command length */ |
| 989 | |
| 990 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ |
| 991 | cmd[19] = 0x2; /* eject disc */ |
| 992 | |
| 993 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); |
| 994 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), |
| 995 | cmd, 31, NULL, 2000); |
| 996 | kfree(cmd); |
| 997 | if (r) |
| 998 | return r; |
| 999 | |
| 1000 | /* At this point, the device disconnects and reconnects with the real |
| 1001 | * ID numbers. */ |
| 1002 | |
| 1003 | usb_set_intfdata(intf, NULL); |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1007 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 1008 | { |
| 1009 | int r; |
| 1010 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1011 | struct net_device *netdev = NULL; |
| 1012 | |
| 1013 | print_id(udev); |
| 1014 | |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1015 | if (id->driver_info & DEVICE_INSTALLER) |
| 1016 | return eject_installer(intf); |
| 1017 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1018 | switch (udev->speed) { |
| 1019 | case USB_SPEED_LOW: |
| 1020 | case USB_SPEED_FULL: |
| 1021 | case USB_SPEED_HIGH: |
| 1022 | break; |
| 1023 | default: |
| 1024 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); |
| 1025 | r = -ENODEV; |
| 1026 | goto error; |
| 1027 | } |
| 1028 | |
| 1029 | netdev = zd_netdev_alloc(intf); |
| 1030 | if (netdev == NULL) { |
| 1031 | r = -ENOMEM; |
| 1032 | goto error; |
| 1033 | } |
| 1034 | |
| 1035 | r = upload_firmware(udev, id->driver_info); |
| 1036 | if (r) { |
| 1037 | dev_err(&intf->dev, |
| 1038 | "couldn't load firmware. Error number %d\n", r); |
| 1039 | goto error; |
| 1040 | } |
| 1041 | |
| 1042 | r = usb_reset_configuration(udev); |
| 1043 | if (r) { |
| 1044 | dev_dbg_f(&intf->dev, |
| 1045 | "couldn't reset configuration. Error number %d\n", r); |
| 1046 | goto error; |
| 1047 | } |
| 1048 | |
| 1049 | /* At this point the interrupt endpoint is not generally enabled. We |
| 1050 | * save the USB bandwidth until the network device is opened. But |
| 1051 | * notify that the initialization of the MAC will require the |
| 1052 | * interrupts to be temporary enabled. |
| 1053 | */ |
| 1054 | r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info); |
| 1055 | if (r) { |
| 1056 | dev_dbg_f(&intf->dev, |
| 1057 | "couldn't initialize mac. Error number %d\n", r); |
| 1058 | goto error; |
| 1059 | } |
| 1060 | |
| 1061 | r = register_netdev(netdev); |
| 1062 | if (r) { |
| 1063 | dev_dbg_f(&intf->dev, |
| 1064 | "couldn't register netdev. Error number %d\n", r); |
| 1065 | goto error; |
| 1066 | } |
| 1067 | |
| 1068 | dev_dbg_f(&intf->dev, "successful\n"); |
| 1069 | dev_info(&intf->dev,"%s\n", netdev->name); |
| 1070 | return 0; |
| 1071 | error: |
| 1072 | usb_reset_device(interface_to_usbdev(intf)); |
| 1073 | zd_netdev_free(netdev); |
| 1074 | return r; |
| 1075 | } |
| 1076 | |
| 1077 | static void disconnect(struct usb_interface *intf) |
| 1078 | { |
| 1079 | struct net_device *netdev = zd_intf_to_netdev(intf); |
| 1080 | struct zd_mac *mac = zd_netdev_mac(netdev); |
| 1081 | struct zd_usb *usb = &mac->chip.usb; |
| 1082 | |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1083 | /* Either something really bad happened, or we're just dealing with |
| 1084 | * a DEVICE_INSTALLER. */ |
| 1085 | if (netdev == NULL) |
| 1086 | return; |
| 1087 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1088 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 1089 | |
| 1090 | zd_netdev_disconnect(netdev); |
| 1091 | |
| 1092 | /* Just in case something has gone wrong! */ |
| 1093 | zd_usb_disable_rx(usb); |
| 1094 | zd_usb_disable_int(usb); |
| 1095 | |
| 1096 | /* If the disconnect has been caused by a removal of the |
| 1097 | * driver module, the reset allows reloading of the driver. If the |
| 1098 | * reset will not be executed here, the upload of the firmware in the |
| 1099 | * probe function caused by the reloading of the driver will fail. |
| 1100 | */ |
| 1101 | usb_reset_device(interface_to_usbdev(intf)); |
| 1102 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1103 | zd_netdev_free(netdev); |
| 1104 | dev_dbg(&intf->dev, "disconnected\n"); |
| 1105 | } |
| 1106 | |
| 1107 | static struct usb_driver driver = { |
| 1108 | .name = "zd1211rw", |
| 1109 | .id_table = usb_ids, |
| 1110 | .probe = probe, |
| 1111 | .disconnect = disconnect, |
| 1112 | }; |
| 1113 | |
| 1114 | static int __init usb_init(void) |
| 1115 | { |
| 1116 | int r; |
| 1117 | |
| 1118 | pr_debug("usb_init()\n"); |
| 1119 | |
| 1120 | r = usb_register(&driver); |
| 1121 | if (r) { |
| 1122 | printk(KERN_ERR "usb_register() failed. Error number %d\n", r); |
| 1123 | return r; |
| 1124 | } |
| 1125 | |
| 1126 | pr_debug("zd1211rw initialized\n"); |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | static void __exit usb_exit(void) |
| 1131 | { |
| 1132 | pr_debug("usb_exit()\n"); |
| 1133 | usb_deregister(&driver); |
| 1134 | } |
| 1135 | |
| 1136 | module_init(usb_init); |
| 1137 | module_exit(usb_exit); |
| 1138 | |
| 1139 | static int usb_int_regs_length(unsigned int count) |
| 1140 | { |
| 1141 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); |
| 1142 | } |
| 1143 | |
| 1144 | static void prepare_read_regs_int(struct zd_usb *usb) |
| 1145 | { |
| 1146 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1147 | |
| 1148 | spin_lock(&intr->lock); |
| 1149 | intr->read_regs_enabled = 1; |
| 1150 | INIT_COMPLETION(intr->read_regs.completion); |
| 1151 | spin_unlock(&intr->lock); |
| 1152 | } |
| 1153 | |
| 1154 | static int get_results(struct zd_usb *usb, u16 *values, |
| 1155 | struct usb_req_read_regs *req, unsigned int count) |
| 1156 | { |
| 1157 | int r; |
| 1158 | int i; |
| 1159 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1160 | struct read_regs_int *rr = &intr->read_regs; |
| 1161 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; |
| 1162 | |
| 1163 | spin_lock(&intr->lock); |
| 1164 | |
| 1165 | r = -EIO; |
| 1166 | /* The created block size seems to be larger than expected. |
| 1167 | * However results appear to be correct. |
| 1168 | */ |
| 1169 | if (rr->length < usb_int_regs_length(count)) { |
| 1170 | dev_dbg_f(zd_usb_dev(usb), |
| 1171 | "error: actual length %d less than expected %d\n", |
| 1172 | rr->length, usb_int_regs_length(count)); |
| 1173 | goto error_unlock; |
| 1174 | } |
| 1175 | if (rr->length > sizeof(rr->buffer)) { |
| 1176 | dev_dbg_f(zd_usb_dev(usb), |
| 1177 | "error: actual length %d exceeds buffer size %zu\n", |
| 1178 | rr->length, sizeof(rr->buffer)); |
| 1179 | goto error_unlock; |
| 1180 | } |
| 1181 | |
| 1182 | for (i = 0; i < count; i++) { |
| 1183 | struct reg_data *rd = ®s->regs[i]; |
| 1184 | if (rd->addr != req->addr[i]) { |
| 1185 | dev_dbg_f(zd_usb_dev(usb), |
| 1186 | "rd[%d] addr %#06hx expected %#06hx\n", i, |
| 1187 | le16_to_cpu(rd->addr), |
| 1188 | le16_to_cpu(req->addr[i])); |
| 1189 | goto error_unlock; |
| 1190 | } |
| 1191 | values[i] = le16_to_cpu(rd->value); |
| 1192 | } |
| 1193 | |
| 1194 | r = 0; |
| 1195 | error_unlock: |
| 1196 | spin_unlock(&intr->lock); |
| 1197 | return r; |
| 1198 | } |
| 1199 | |
| 1200 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, |
| 1201 | const zd_addr_t *addresses, unsigned int count) |
| 1202 | { |
| 1203 | int r; |
| 1204 | int i, req_len, actual_req_len; |
| 1205 | struct usb_device *udev; |
| 1206 | struct usb_req_read_regs *req = NULL; |
| 1207 | unsigned long timeout; |
| 1208 | |
| 1209 | if (count < 1) { |
| 1210 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); |
| 1211 | return -EINVAL; |
| 1212 | } |
| 1213 | if (count > USB_MAX_IOREAD16_COUNT) { |
| 1214 | dev_dbg_f(zd_usb_dev(usb), |
| 1215 | "error: count %u exceeds possible max %u\n", |
| 1216 | count, USB_MAX_IOREAD16_COUNT); |
| 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | if (in_atomic()) { |
| 1220 | dev_dbg_f(zd_usb_dev(usb), |
| 1221 | "error: io in atomic context not supported\n"); |
| 1222 | return -EWOULDBLOCK; |
| 1223 | } |
| 1224 | if (!usb_int_enabled(usb)) { |
| 1225 | dev_dbg_f(zd_usb_dev(usb), |
| 1226 | "error: usb interrupt not enabled\n"); |
| 1227 | return -EWOULDBLOCK; |
| 1228 | } |
| 1229 | |
| 1230 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); |
| 1231 | req = kmalloc(req_len, GFP_NOFS); |
| 1232 | if (!req) |
| 1233 | return -ENOMEM; |
| 1234 | req->id = cpu_to_le16(USB_REQ_READ_REGS); |
| 1235 | for (i = 0; i < count; i++) |
| 1236 | req->addr[i] = cpu_to_le16(usb_addr(usb, addresses[i])); |
| 1237 | |
| 1238 | udev = zd_usb_to_usbdev(usb); |
| 1239 | prepare_read_regs_int(usb); |
| 1240 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1241 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1242 | if (r) { |
| 1243 | dev_dbg_f(zd_usb_dev(usb), |
| 1244 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1245 | goto error; |
| 1246 | } |
| 1247 | if (req_len != actual_req_len) { |
| 1248 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n" |
| 1249 | " req_len %d != actual_req_len %d\n", |
| 1250 | req_len, actual_req_len); |
| 1251 | r = -EIO; |
| 1252 | goto error; |
| 1253 | } |
| 1254 | |
| 1255 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, |
| 1256 | msecs_to_jiffies(1000)); |
| 1257 | if (!timeout) { |
| 1258 | disable_read_regs_int(usb); |
| 1259 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); |
| 1260 | r = -ETIMEDOUT; |
| 1261 | goto error; |
| 1262 | } |
| 1263 | |
| 1264 | r = get_results(usb, values, req, count); |
| 1265 | error: |
| 1266 | kfree(req); |
| 1267 | return r; |
| 1268 | } |
| 1269 | |
| 1270 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
| 1271 | unsigned int count) |
| 1272 | { |
| 1273 | int r; |
| 1274 | struct usb_device *udev; |
| 1275 | struct usb_req_write_regs *req = NULL; |
| 1276 | int i, req_len, actual_req_len; |
| 1277 | |
| 1278 | if (count == 0) |
| 1279 | return 0; |
| 1280 | if (count > USB_MAX_IOWRITE16_COUNT) { |
| 1281 | dev_dbg_f(zd_usb_dev(usb), |
| 1282 | "error: count %u exceeds possible max %u\n", |
| 1283 | count, USB_MAX_IOWRITE16_COUNT); |
| 1284 | return -EINVAL; |
| 1285 | } |
| 1286 | if (in_atomic()) { |
| 1287 | dev_dbg_f(zd_usb_dev(usb), |
| 1288 | "error: io in atomic context not supported\n"); |
| 1289 | return -EWOULDBLOCK; |
| 1290 | } |
| 1291 | |
| 1292 | req_len = sizeof(struct usb_req_write_regs) + |
| 1293 | count * sizeof(struct reg_data); |
| 1294 | req = kmalloc(req_len, GFP_NOFS); |
| 1295 | if (!req) |
| 1296 | return -ENOMEM; |
| 1297 | |
| 1298 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); |
| 1299 | for (i = 0; i < count; i++) { |
| 1300 | struct reg_data *rw = &req->reg_writes[i]; |
| 1301 | rw->addr = cpu_to_le16(usb_addr(usb, ioreqs[i].addr)); |
| 1302 | rw->value = cpu_to_le16(ioreqs[i].value); |
| 1303 | } |
| 1304 | |
| 1305 | udev = zd_usb_to_usbdev(usb); |
| 1306 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1307 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1308 | if (r) { |
| 1309 | dev_dbg_f(zd_usb_dev(usb), |
| 1310 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1311 | goto error; |
| 1312 | } |
| 1313 | if (req_len != actual_req_len) { |
| 1314 | dev_dbg_f(zd_usb_dev(usb), |
| 1315 | "error in usb_bulk_msg()" |
| 1316 | " req_len %d != actual_req_len %d\n", |
| 1317 | req_len, actual_req_len); |
| 1318 | r = -EIO; |
| 1319 | goto error; |
| 1320 | } |
| 1321 | |
| 1322 | /* FALL-THROUGH with r == 0 */ |
| 1323 | error: |
| 1324 | kfree(req); |
| 1325 | return r; |
| 1326 | } |
| 1327 | |
| 1328 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) |
| 1329 | { |
| 1330 | int r; |
| 1331 | struct usb_device *udev; |
| 1332 | struct usb_req_rfwrite *req = NULL; |
| 1333 | int i, req_len, actual_req_len; |
| 1334 | u16 bit_value_template; |
| 1335 | |
| 1336 | if (in_atomic()) { |
| 1337 | dev_dbg_f(zd_usb_dev(usb), |
| 1338 | "error: io in atomic context not supported\n"); |
| 1339 | return -EWOULDBLOCK; |
| 1340 | } |
| 1341 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { |
| 1342 | dev_dbg_f(zd_usb_dev(usb), |
| 1343 | "error: bits %d are smaller than" |
| 1344 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", |
| 1345 | bits, USB_MIN_RFWRITE_BIT_COUNT); |
| 1346 | return -EINVAL; |
| 1347 | } |
| 1348 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { |
| 1349 | dev_dbg_f(zd_usb_dev(usb), |
| 1350 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", |
| 1351 | bits, USB_MAX_RFWRITE_BIT_COUNT); |
| 1352 | return -EINVAL; |
| 1353 | } |
| 1354 | #ifdef DEBUG |
| 1355 | if (value & (~0UL << bits)) { |
| 1356 | dev_dbg_f(zd_usb_dev(usb), |
| 1357 | "error: value %#09x has bits >= %d set\n", |
| 1358 | value, bits); |
| 1359 | return -EINVAL; |
| 1360 | } |
| 1361 | #endif /* DEBUG */ |
| 1362 | |
| 1363 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); |
| 1364 | |
| 1365 | r = zd_usb_ioread16(usb, &bit_value_template, CR203); |
| 1366 | if (r) { |
| 1367 | dev_dbg_f(zd_usb_dev(usb), |
| 1368 | "error %d: Couldn't read CR203\n", r); |
| 1369 | goto out; |
| 1370 | } |
| 1371 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); |
| 1372 | |
| 1373 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); |
| 1374 | req = kmalloc(req_len, GFP_NOFS); |
| 1375 | if (!req) |
| 1376 | return -ENOMEM; |
| 1377 | |
| 1378 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); |
| 1379 | /* 1: 3683a, but not used in ZYDAS driver */ |
| 1380 | req->value = cpu_to_le16(2); |
| 1381 | req->bits = cpu_to_le16(bits); |
| 1382 | |
| 1383 | for (i = 0; i < bits; i++) { |
| 1384 | u16 bv = bit_value_template; |
| 1385 | if (value & (1 << (bits-1-i))) |
| 1386 | bv |= RF_DATA; |
| 1387 | req->bit_values[i] = cpu_to_le16(bv); |
| 1388 | } |
| 1389 | |
| 1390 | udev = zd_usb_to_usbdev(usb); |
| 1391 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1392 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1393 | if (r) { |
| 1394 | dev_dbg_f(zd_usb_dev(usb), |
| 1395 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1396 | goto out; |
| 1397 | } |
| 1398 | if (req_len != actual_req_len) { |
| 1399 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()" |
| 1400 | " req_len %d != actual_req_len %d\n", |
| 1401 | req_len, actual_req_len); |
| 1402 | r = -EIO; |
| 1403 | goto out; |
| 1404 | } |
| 1405 | |
| 1406 | /* FALL-THROUGH with r == 0 */ |
| 1407 | out: |
| 1408 | kfree(req); |
| 1409 | return r; |
| 1410 | } |