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