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