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