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