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