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