Daniel Drake | 66bb42f | 2007-11-19 16:20:12 +0000 | [diff] [blame] | 1 | /* ZD1211 USB-WLAN driver for Linux |
| 2 | * |
| 3 | * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de> |
| 4 | * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org> |
| 5 | * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 23 | #include <linux/init.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 24 | #include <linux/firmware.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/skbuff.h> |
| 28 | #include <linux/usb.h> |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 29 | #include <linux/workqueue.h> |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 30 | #include <net/mac80211.h> |
Al Viro | c248725 | 2007-07-15 21:00:21 +0100 | [diff] [blame] | 31 | #include <asm/unaligned.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 32 | |
| 33 | #include "zd_def.h" |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 34 | #include "zd_mac.h" |
| 35 | #include "zd_usb.h" |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 36 | |
| 37 | static struct usb_device_id usb_ids[] = { |
| 38 | /* ZD1211 */ |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 39 | { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 }, |
| 40 | { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 }, |
| 41 | { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 }, |
| 42 | { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 }, |
| 43 | { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 }, |
| 44 | { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 }, |
| 45 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 46 | { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 }, |
Hin-Tak Leung | 14990c6 | 2009-02-08 02:13:56 +0000 | [diff] [blame] | 47 | { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 48 | { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 }, |
| 49 | { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 }, |
| 50 | { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 51 | { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 }, |
Matthew Davidson | aa1d3a1 | 2007-05-01 17:14:30 +0100 | [diff] [blame] | 52 | { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 53 | { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 }, |
Ulrich Kunitz | ababda0 | 2007-09-01 22:40:32 +0100 | [diff] [blame] | 54 | { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 55 | { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 }, |
| 56 | { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 }, |
| 57 | { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 269fca0 | 2007-11-19 15:29:24 +0000 | [diff] [blame] | 58 | { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 59 | { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 }, |
| 60 | { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 }, |
| 61 | { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 }, |
| 62 | { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 63 | /* ZD1211B */ |
Ulrich Kunitz | 019a675 | 2007-05-01 17:13:51 +0100 | [diff] [blame] | 64 | { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 93f510b | 2007-07-01 18:22:21 +0100 | [diff] [blame] | 65 | { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 66 | { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 61ef606 | 2007-10-07 16:24:26 +0100 | [diff] [blame] | 67 | { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 68 | { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B }, |
| 69 | { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B }, |
| 70 | { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B }, |
| 71 | { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B }, |
| 72 | { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B }, |
| 73 | { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B }, |
| 74 | { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B }, |
| 75 | { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B }, |
Pascal Terjan | b4b223c | 2009-06-18 17:54:03 +0200 | [diff] [blame] | 76 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 77 | { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B }, |
| 78 | { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B }, |
Hin-Tak Leung | 8f75e07 | 2009-07-13 23:20:37 +0100 | [diff] [blame] | 79 | { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B }, |
Hin-Tak Leung | 3bfbe80 | 2009-06-18 03:53:26 +0100 | [diff] [blame] | 80 | { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B }, |
| 81 | { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B }, |
| 82 | { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B }, |
| 83 | { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B }, |
| 84 | { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B }, |
| 85 | { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B }, |
| 86 | { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B }, |
| 87 | { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B }, |
| 88 | { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B }, |
| 89 | { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B }, |
| 90 | { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B }, |
| 91 | { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B }, |
| 92 | { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 93 | /* "Driverless" devices that need ejecting */ |
| 94 | { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER }, |
Matthew Davidson | aa1d3a1 | 2007-05-01 17:14:30 +0100 | [diff] [blame] | 95 | { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 96 | {} |
| 97 | }; |
| 98 | |
| 99 | MODULE_LICENSE("GPL"); |
| 100 | MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip."); |
| 101 | MODULE_AUTHOR("Ulrich Kunitz"); |
| 102 | MODULE_AUTHOR("Daniel Drake"); |
| 103 | MODULE_VERSION("1.0"); |
| 104 | MODULE_DEVICE_TABLE(usb, usb_ids); |
| 105 | |
| 106 | #define FW_ZD1211_PREFIX "zd1211/zd1211_" |
| 107 | #define FW_ZD1211B_PREFIX "zd1211/zd1211b_" |
| 108 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 109 | /* USB device initialization */ |
Luis Carlos Cobo | 72e77a8 | 2008-03-03 12:32:15 -0800 | [diff] [blame] | 110 | static void int_urb_complete(struct urb *urb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 111 | |
| 112 | static int request_fw_file( |
| 113 | const struct firmware **fw, const char *name, struct device *device) |
| 114 | { |
| 115 | int r; |
| 116 | |
| 117 | dev_dbg_f(device, "fw name %s\n", name); |
| 118 | |
| 119 | r = request_firmware(fw, name, device); |
| 120 | if (r) |
| 121 | dev_err(device, |
| 122 | "Could not load firmware file %s. Error number %d\n", |
| 123 | name, r); |
| 124 | return r; |
| 125 | } |
| 126 | |
| 127 | static inline u16 get_bcdDevice(const struct usb_device *udev) |
| 128 | { |
| 129 | return le16_to_cpu(udev->descriptor.bcdDevice); |
| 130 | } |
| 131 | |
| 132 | enum upload_code_flags { |
| 133 | REBOOT = 1, |
| 134 | }; |
| 135 | |
| 136 | /* Ensures that MAX_TRANSFER_SIZE is even. */ |
| 137 | #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1) |
| 138 | |
| 139 | static int upload_code(struct usb_device *udev, |
| 140 | const u8 *data, size_t size, u16 code_offset, int flags) |
| 141 | { |
| 142 | u8 *p; |
| 143 | int r; |
| 144 | |
| 145 | /* USB request blocks need "kmalloced" buffers. |
| 146 | */ |
| 147 | p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL); |
| 148 | if (!p) { |
| 149 | dev_err(&udev->dev, "out of memory\n"); |
| 150 | r = -ENOMEM; |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | size &= ~1; |
| 155 | while (size > 0) { |
| 156 | size_t transfer_size = size <= MAX_TRANSFER_SIZE ? |
| 157 | size : MAX_TRANSFER_SIZE; |
| 158 | |
| 159 | dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size); |
| 160 | |
| 161 | memcpy(p, data, transfer_size); |
| 162 | r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 163 | USB_REQ_FIRMWARE_DOWNLOAD, |
| 164 | USB_DIR_OUT | USB_TYPE_VENDOR, |
| 165 | code_offset, 0, p, transfer_size, 1000 /* ms */); |
| 166 | if (r < 0) { |
| 167 | dev_err(&udev->dev, |
| 168 | "USB control request for firmware upload" |
| 169 | " failed. Error number %d\n", r); |
| 170 | goto error; |
| 171 | } |
| 172 | transfer_size = r & ~1; |
| 173 | |
| 174 | size -= transfer_size; |
| 175 | data += transfer_size; |
| 176 | code_offset += transfer_size/sizeof(u16); |
| 177 | } |
| 178 | |
| 179 | if (flags & REBOOT) { |
| 180 | u8 ret; |
| 181 | |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 182 | /* Use "DMA-aware" buffer. */ |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 183 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 184 | USB_REQ_FIRMWARE_CONFIRM, |
| 185 | USB_DIR_IN | USB_TYPE_VENDOR, |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 186 | 0, 0, p, sizeof(ret), 5000 /* ms */); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 187 | if (r != sizeof(ret)) { |
| 188 | dev_err(&udev->dev, |
| 189 | "control request firmeware confirmation failed." |
| 190 | " Return value %d\n", r); |
| 191 | if (r >= 0) |
| 192 | r = -ENODEV; |
| 193 | goto error; |
| 194 | } |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 195 | ret = p[0]; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 196 | if (ret & 0x80) { |
| 197 | dev_err(&udev->dev, |
| 198 | "Internal error while downloading." |
| 199 | " Firmware confirm return value %#04x\n", |
| 200 | (unsigned int)ret); |
| 201 | r = -ENODEV; |
| 202 | goto error; |
| 203 | } |
| 204 | dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n", |
| 205 | (unsigned int)ret); |
| 206 | } |
| 207 | |
| 208 | r = 0; |
| 209 | error: |
| 210 | kfree(p); |
| 211 | return r; |
| 212 | } |
| 213 | |
| 214 | static u16 get_word(const void *data, u16 offset) |
| 215 | { |
| 216 | const __le16 *p = data; |
| 217 | return le16_to_cpu(p[offset]); |
| 218 | } |
| 219 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 220 | static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 221 | const char* postfix) |
| 222 | { |
| 223 | scnprintf(buffer, size, "%s%s", |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 224 | usb->is_zd1211b ? |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 225 | FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX, |
| 226 | postfix); |
| 227 | return buffer; |
| 228 | } |
| 229 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 230 | static int handle_version_mismatch(struct zd_usb *usb, |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 231 | const struct firmware *ub_fw) |
| 232 | { |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 233 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 234 | const struct firmware *ur_fw = NULL; |
| 235 | int offset; |
| 236 | int r = 0; |
| 237 | char fw_name[128]; |
| 238 | |
| 239 | r = request_fw_file(&ur_fw, |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 240 | get_fw_name(usb, fw_name, sizeof(fw_name), "ur"), |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 241 | &udev->dev); |
| 242 | if (r) |
| 243 | goto error; |
| 244 | |
Daniel Drake | ee30276 | 2006-12-12 01:25:52 +0000 | [diff] [blame] | 245 | 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] | 246 | if (r) |
| 247 | goto error; |
| 248 | |
Daniel Drake | ee30276 | 2006-12-12 01:25:52 +0000 | [diff] [blame] | 249 | offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16)); |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 250 | r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset, |
Daniel Drake | ee30276 | 2006-12-12 01:25:52 +0000 | [diff] [blame] | 251 | E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT); |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 252 | |
| 253 | /* At this point, the vendor driver downloads the whole firmware |
| 254 | * image, hacks around with version IDs, and uploads it again, |
| 255 | * completely overwriting the boot code. We do not do this here as |
| 256 | * it is not required on any tested devices, and it is suspected to |
| 257 | * cause problems. */ |
| 258 | error: |
| 259 | release_firmware(ur_fw); |
| 260 | return r; |
| 261 | } |
| 262 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 263 | static int upload_firmware(struct zd_usb *usb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 264 | { |
| 265 | int r; |
| 266 | u16 fw_bcdDevice; |
| 267 | u16 bcdDevice; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 268 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 269 | const struct firmware *ub_fw = NULL; |
| 270 | const struct firmware *uph_fw = NULL; |
| 271 | char fw_name[128]; |
| 272 | |
| 273 | bcdDevice = get_bcdDevice(udev); |
| 274 | |
| 275 | r = request_fw_file(&ub_fw, |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 276 | get_fw_name(usb, fw_name, sizeof(fw_name), "ub"), |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 277 | &udev->dev); |
| 278 | if (r) |
| 279 | goto error; |
| 280 | |
Daniel Drake | ee30276 | 2006-12-12 01:25:52 +0000 | [diff] [blame] | 281 | fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 282 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 283 | if (fw_bcdDevice != bcdDevice) { |
| 284 | dev_info(&udev->dev, |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 285 | "firmware version %#06x and device bootcode version " |
| 286 | "%#06x differ\n", fw_bcdDevice, bcdDevice); |
| 287 | if (bcdDevice <= 0x4313) |
| 288 | dev_warn(&udev->dev, "device has old bootcode, please " |
| 289 | "report success or failure\n"); |
| 290 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 291 | r = handle_version_mismatch(usb, ub_fw); |
Daniel Drake | d066c21 | 2006-08-12 17:59:59 +0100 | [diff] [blame] | 292 | if (r) |
| 293 | goto error; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 294 | } else { |
| 295 | dev_dbg_f(&udev->dev, |
| 296 | "firmware device id %#06x is equal to the " |
| 297 | "actual device id\n", fw_bcdDevice); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | r = request_fw_file(&uph_fw, |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 302 | get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"), |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 303 | &udev->dev); |
| 304 | if (r) |
| 305 | goto error; |
| 306 | |
Daniel Drake | ee30276 | 2006-12-12 01:25:52 +0000 | [diff] [blame] | 307 | 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] | 308 | if (r) { |
| 309 | dev_err(&udev->dev, |
| 310 | "Could not upload firmware code uph. Error number %d\n", |
| 311 | r); |
| 312 | } |
| 313 | |
| 314 | /* FALL-THROUGH */ |
| 315 | error: |
| 316 | release_firmware(ub_fw); |
| 317 | release_firmware(uph_fw); |
| 318 | return r; |
| 319 | } |
| 320 | |
Ben Hutchings | 3e8b4d0 | 2009-11-07 22:03:22 +0000 | [diff] [blame] | 321 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ur"); |
| 322 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "ur"); |
| 323 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ub"); |
| 324 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "ub"); |
| 325 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "uphr"); |
| 326 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "uphr"); |
| 327 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 328 | /* Read data from device address space using "firmware interface" which does |
| 329 | * not require firmware to be loaded. */ |
| 330 | int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len) |
| 331 | { |
| 332 | int r; |
| 333 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 334 | u8 *buf; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 335 | |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 336 | /* Use "DMA-aware" buffer. */ |
| 337 | buf = kmalloc(len, GFP_KERNEL); |
| 338 | if (!buf) |
| 339 | return -ENOMEM; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 340 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 341 | USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0, |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 342 | buf, len, 5000); |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 343 | if (r < 0) { |
| 344 | dev_err(&udev->dev, |
| 345 | "read over firmware interface failed: %d\n", r); |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 346 | goto exit; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 347 | } else if (r != len) { |
| 348 | dev_err(&udev->dev, |
| 349 | "incomplete read over firmware interface: %d/%d\n", |
| 350 | r, len); |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 351 | r = -EIO; |
| 352 | goto exit; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 353 | } |
Atsushi Nemoto | a8c4ea7 | 2008-05-16 17:27:05 +0900 | [diff] [blame] | 354 | r = 0; |
| 355 | memcpy(data, buf, len); |
| 356 | exit: |
| 357 | kfree(buf); |
| 358 | return r; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 361 | #define urb_dev(urb) (&(urb)->dev->dev) |
| 362 | |
| 363 | static inline void handle_regs_int(struct urb *urb) |
| 364 | { |
| 365 | struct zd_usb *usb = urb->context; |
| 366 | struct zd_usb_interrupt *intr = &usb->intr; |
| 367 | int len; |
Luis Carlos Cobo | 72e77a8 | 2008-03-03 12:32:15 -0800 | [diff] [blame] | 368 | u16 int_num; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 369 | |
| 370 | ZD_ASSERT(in_interrupt()); |
| 371 | spin_lock(&intr->lock); |
| 372 | |
Al Viro | d63ddce | 2008-05-21 01:34:30 +0100 | [diff] [blame] | 373 | int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2)); |
Luis Carlos Cobo | 72e77a8 | 2008-03-03 12:32:15 -0800 | [diff] [blame] | 374 | if (int_num == CR_INTERRUPT) { |
| 375 | struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context)); |
| 376 | memcpy(&mac->intr_buffer, urb->transfer_buffer, |
| 377 | USB_MAX_EP_INT_BUFFER); |
| 378 | schedule_work(&mac->process_intr); |
| 379 | } else if (intr->read_regs_enabled) { |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 380 | intr->read_regs.length = len = urb->actual_length; |
| 381 | |
| 382 | if (len > sizeof(intr->read_regs.buffer)) |
| 383 | len = sizeof(intr->read_regs.buffer); |
| 384 | memcpy(intr->read_regs.buffer, urb->transfer_buffer, len); |
| 385 | intr->read_regs_enabled = 0; |
| 386 | complete(&intr->read_regs.completion); |
| 387 | goto out; |
| 388 | } |
| 389 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 390 | out: |
| 391 | spin_unlock(&intr->lock); |
| 392 | } |
| 393 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 394 | static void int_urb_complete(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 395 | { |
| 396 | int r; |
| 397 | struct usb_int_header *hdr; |
| 398 | |
| 399 | switch (urb->status) { |
| 400 | case 0: |
| 401 | break; |
| 402 | case -ESHUTDOWN: |
| 403 | case -EINVAL: |
| 404 | case -ENODEV: |
| 405 | case -ENOENT: |
| 406 | case -ECONNRESET: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 407 | case -EPIPE: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 408 | goto kfree; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 409 | default: |
| 410 | goto resubmit; |
| 411 | } |
| 412 | |
| 413 | if (urb->actual_length < sizeof(hdr)) { |
| 414 | dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb); |
| 415 | goto resubmit; |
| 416 | } |
| 417 | |
| 418 | hdr = urb->transfer_buffer; |
| 419 | if (hdr->type != USB_INT_TYPE) { |
| 420 | dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb); |
| 421 | goto resubmit; |
| 422 | } |
| 423 | |
| 424 | switch (hdr->id) { |
| 425 | case USB_INT_ID_REGS: |
| 426 | handle_regs_int(urb); |
| 427 | break; |
| 428 | case USB_INT_ID_RETRY_FAILED: |
Benoit PAPILLAULT | 7f4013f | 2009-10-22 12:04:52 +0200 | [diff] [blame] | 429 | zd_mac_tx_failed(urb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb, |
| 433 | (unsigned int)hdr->id); |
| 434 | goto resubmit; |
| 435 | } |
| 436 | |
| 437 | resubmit: |
| 438 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 439 | if (r) { |
| 440 | dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb); |
| 441 | goto kfree; |
| 442 | } |
| 443 | return; |
| 444 | kfree: |
| 445 | kfree(urb->transfer_buffer); |
| 446 | } |
| 447 | |
| 448 | static inline int int_urb_interval(struct usb_device *udev) |
| 449 | { |
| 450 | switch (udev->speed) { |
| 451 | case USB_SPEED_HIGH: |
| 452 | return 4; |
| 453 | case USB_SPEED_LOW: |
| 454 | return 10; |
| 455 | case USB_SPEED_FULL: |
| 456 | default: |
| 457 | return 1; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static inline int usb_int_enabled(struct zd_usb *usb) |
| 462 | { |
| 463 | unsigned long flags; |
| 464 | struct zd_usb_interrupt *intr = &usb->intr; |
| 465 | struct urb *urb; |
| 466 | |
| 467 | spin_lock_irqsave(&intr->lock, flags); |
| 468 | urb = intr->urb; |
| 469 | spin_unlock_irqrestore(&intr->lock, flags); |
| 470 | return urb != NULL; |
| 471 | } |
| 472 | |
| 473 | int zd_usb_enable_int(struct zd_usb *usb) |
| 474 | { |
| 475 | int r; |
| 476 | struct usb_device *udev; |
| 477 | struct zd_usb_interrupt *intr = &usb->intr; |
| 478 | void *transfer_buffer = NULL; |
| 479 | struct urb *urb; |
| 480 | |
| 481 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 482 | |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 483 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 484 | if (!urb) { |
| 485 | r = -ENOMEM; |
| 486 | goto out; |
| 487 | } |
| 488 | |
| 489 | ZD_ASSERT(!irqs_disabled()); |
| 490 | spin_lock_irq(&intr->lock); |
| 491 | if (intr->urb) { |
| 492 | spin_unlock_irq(&intr->lock); |
| 493 | r = 0; |
| 494 | goto error_free_urb; |
| 495 | } |
| 496 | intr->urb = urb; |
| 497 | spin_unlock_irq(&intr->lock); |
| 498 | |
| 499 | /* TODO: make it a DMA buffer */ |
| 500 | r = -ENOMEM; |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 501 | transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 502 | if (!transfer_buffer) { |
| 503 | dev_dbg_f(zd_usb_dev(usb), |
| 504 | "couldn't allocate transfer_buffer\n"); |
| 505 | goto error_set_urb_null; |
| 506 | } |
| 507 | |
| 508 | udev = zd_usb_to_usbdev(usb); |
| 509 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), |
| 510 | transfer_buffer, USB_MAX_EP_INT_BUFFER, |
| 511 | int_urb_complete, usb, |
| 512 | intr->interval); |
| 513 | |
| 514 | 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] | 515 | r = usb_submit_urb(urb, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 516 | if (r) { |
| 517 | dev_dbg_f(zd_usb_dev(usb), |
| 518 | "Couldn't submit urb. Error number %d\n", r); |
| 519 | goto error; |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | error: |
| 524 | kfree(transfer_buffer); |
| 525 | error_set_urb_null: |
| 526 | spin_lock_irq(&intr->lock); |
| 527 | intr->urb = NULL; |
| 528 | spin_unlock_irq(&intr->lock); |
| 529 | error_free_urb: |
| 530 | usb_free_urb(urb); |
| 531 | out: |
| 532 | return r; |
| 533 | } |
| 534 | |
| 535 | void zd_usb_disable_int(struct zd_usb *usb) |
| 536 | { |
| 537 | unsigned long flags; |
| 538 | struct zd_usb_interrupt *intr = &usb->intr; |
| 539 | struct urb *urb; |
| 540 | |
| 541 | spin_lock_irqsave(&intr->lock, flags); |
| 542 | urb = intr->urb; |
| 543 | if (!urb) { |
| 544 | spin_unlock_irqrestore(&intr->lock, flags); |
| 545 | return; |
| 546 | } |
| 547 | intr->urb = NULL; |
| 548 | spin_unlock_irqrestore(&intr->lock, flags); |
| 549 | |
| 550 | usb_kill_urb(urb); |
| 551 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); |
| 552 | usb_free_urb(urb); |
| 553 | } |
| 554 | |
| 555 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, |
| 556 | unsigned int length) |
| 557 | { |
| 558 | int i; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 559 | const struct rx_length_info *length_info; |
| 560 | |
| 561 | if (length < sizeof(struct rx_length_info)) { |
| 562 | /* It's not a complete packet anyhow. */ |
Benoit PAPILLAULT | 7f4013f | 2009-10-22 12:04:52 +0200 | [diff] [blame] | 563 | printk("%s: invalid, small RX packet : %d\n", |
| 564 | __func__, length); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | length_info = (struct rx_length_info *) |
| 568 | (buffer + length - sizeof(struct rx_length_info)); |
| 569 | |
| 570 | /* It might be that three frames are merged into a single URB |
| 571 | * transaction. We have to check for the length info tag. |
| 572 | * |
| 573 | * While testing we discovered that length_info might be unaligned, |
| 574 | * because if USB transactions are merged, the last packet will not |
| 575 | * be padded. Unaligned access might also happen if the length_info |
| 576 | * structure is not present. |
| 577 | */ |
Harvey Harrison | 533dd1b | 2008-04-29 01:03:36 -0700 | [diff] [blame] | 578 | if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG) |
Ulrich Kunitz | b269825 | 2006-08-01 23:43:34 +0200 | [diff] [blame] | 579 | { |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 580 | unsigned int l, k, n; |
| 581 | for (i = 0, l = 0;; i++) { |
Harvey Harrison | 533dd1b | 2008-04-29 01:03:36 -0700 | [diff] [blame] | 582 | k = get_unaligned_le16(&length_info->length[i]); |
Ulrich Kunitz | 850c211 | 2006-11-22 00:06:19 +0000 | [diff] [blame] | 583 | if (k == 0) |
| 584 | return; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 585 | n = l+k; |
| 586 | if (n > length) |
| 587 | return; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 588 | zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 589 | if (i >= 2) |
| 590 | return; |
| 591 | l = (n+3) & ~3; |
| 592 | } |
| 593 | } else { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 594 | zd_mac_rx(zd_usb_to_hw(usb), buffer, length); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 598 | static void rx_urb_complete(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 599 | { |
| 600 | struct zd_usb *usb; |
| 601 | struct zd_usb_rx *rx; |
| 602 | const u8 *buffer; |
| 603 | unsigned int length; |
| 604 | |
| 605 | switch (urb->status) { |
| 606 | case 0: |
| 607 | break; |
| 608 | case -ESHUTDOWN: |
| 609 | case -EINVAL: |
| 610 | case -ENODEV: |
| 611 | case -ENOENT: |
| 612 | case -ECONNRESET: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 613 | case -EPIPE: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 614 | return; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 615 | default: |
| 616 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 617 | goto resubmit; |
| 618 | } |
| 619 | |
| 620 | buffer = urb->transfer_buffer; |
| 621 | length = urb->actual_length; |
| 622 | usb = urb->context; |
| 623 | rx = &usb->rx; |
| 624 | |
| 625 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { |
| 626 | /* If there is an old first fragment, we don't care. */ |
| 627 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); |
| 628 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); |
| 629 | spin_lock(&rx->lock); |
| 630 | memcpy(rx->fragment, buffer, length); |
| 631 | rx->fragment_length = length; |
| 632 | spin_unlock(&rx->lock); |
| 633 | goto resubmit; |
| 634 | } |
| 635 | |
| 636 | spin_lock(&rx->lock); |
| 637 | if (rx->fragment_length > 0) { |
| 638 | /* We are on a second fragment, we believe */ |
| 639 | ZD_ASSERT(length + rx->fragment_length <= |
| 640 | ARRAY_SIZE(rx->fragment)); |
| 641 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); |
| 642 | memcpy(rx->fragment+rx->fragment_length, buffer, length); |
| 643 | handle_rx_packet(usb, rx->fragment, |
| 644 | rx->fragment_length + length); |
| 645 | rx->fragment_length = 0; |
| 646 | spin_unlock(&rx->lock); |
| 647 | } else { |
| 648 | spin_unlock(&rx->lock); |
| 649 | handle_rx_packet(usb, buffer, length); |
| 650 | } |
| 651 | |
| 652 | resubmit: |
| 653 | usb_submit_urb(urb, GFP_ATOMIC); |
| 654 | } |
| 655 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 656 | static struct urb *alloc_rx_urb(struct zd_usb *usb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 657 | { |
| 658 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
| 659 | struct urb *urb; |
| 660 | void *buffer; |
| 661 | |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 662 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 663 | if (!urb) |
| 664 | return NULL; |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 665 | buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 666 | &urb->transfer_dma); |
| 667 | if (!buffer) { |
| 668 | usb_free_urb(urb); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), |
| 673 | buffer, USB_MAX_RX_SIZE, |
| 674 | rx_urb_complete, usb); |
| 675 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 676 | |
| 677 | return urb; |
| 678 | } |
| 679 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 680 | static void free_rx_urb(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 681 | { |
| 682 | if (!urb) |
| 683 | return; |
| 684 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
| 685 | urb->transfer_buffer, urb->transfer_dma); |
| 686 | usb_free_urb(urb); |
| 687 | } |
| 688 | |
| 689 | int zd_usb_enable_rx(struct zd_usb *usb) |
| 690 | { |
| 691 | int i, r; |
| 692 | struct zd_usb_rx *rx = &usb->rx; |
| 693 | struct urb **urbs; |
| 694 | |
| 695 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 696 | |
| 697 | r = -ENOMEM; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 698 | urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 699 | if (!urbs) |
| 700 | goto error; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 701 | for (i = 0; i < RX_URBS_COUNT; i++) { |
| 702 | urbs[i] = alloc_rx_urb(usb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 703 | if (!urbs[i]) |
| 704 | goto error; |
| 705 | } |
| 706 | |
| 707 | ZD_ASSERT(!irqs_disabled()); |
| 708 | spin_lock_irq(&rx->lock); |
| 709 | if (rx->urbs) { |
| 710 | spin_unlock_irq(&rx->lock); |
| 711 | r = 0; |
| 712 | goto error; |
| 713 | } |
| 714 | rx->urbs = urbs; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 715 | rx->urbs_count = RX_URBS_COUNT; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 716 | spin_unlock_irq(&rx->lock); |
| 717 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 718 | for (i = 0; i < RX_URBS_COUNT; i++) { |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 719 | r = usb_submit_urb(urbs[i], GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 720 | if (r) |
| 721 | goto error_submit; |
| 722 | } |
| 723 | |
| 724 | return 0; |
| 725 | error_submit: |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 726 | for (i = 0; i < RX_URBS_COUNT; i++) { |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 727 | usb_kill_urb(urbs[i]); |
| 728 | } |
| 729 | spin_lock_irq(&rx->lock); |
| 730 | rx->urbs = NULL; |
| 731 | rx->urbs_count = 0; |
| 732 | spin_unlock_irq(&rx->lock); |
| 733 | error: |
| 734 | if (urbs) { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 735 | for (i = 0; i < RX_URBS_COUNT; i++) |
| 736 | free_rx_urb(urbs[i]); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 737 | } |
| 738 | return r; |
| 739 | } |
| 740 | |
| 741 | void zd_usb_disable_rx(struct zd_usb *usb) |
| 742 | { |
| 743 | int i; |
| 744 | unsigned long flags; |
| 745 | struct urb **urbs; |
| 746 | unsigned int count; |
| 747 | struct zd_usb_rx *rx = &usb->rx; |
| 748 | |
| 749 | spin_lock_irqsave(&rx->lock, flags); |
| 750 | urbs = rx->urbs; |
| 751 | count = rx->urbs_count; |
| 752 | spin_unlock_irqrestore(&rx->lock, flags); |
| 753 | if (!urbs) |
| 754 | return; |
| 755 | |
| 756 | for (i = 0; i < count; i++) { |
| 757 | usb_kill_urb(urbs[i]); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 758 | free_rx_urb(urbs[i]); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 759 | } |
| 760 | kfree(urbs); |
| 761 | |
| 762 | spin_lock_irqsave(&rx->lock, flags); |
| 763 | rx->urbs = NULL; |
| 764 | rx->urbs_count = 0; |
| 765 | spin_unlock_irqrestore(&rx->lock, flags); |
| 766 | } |
| 767 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 768 | /** |
| 769 | * zd_usb_disable_tx - disable transmission |
| 770 | * @usb: the zd1211rw-private USB structure |
| 771 | * |
| 772 | * Frees all URBs in the free list and marks the transmission as disabled. |
| 773 | */ |
| 774 | void zd_usb_disable_tx(struct zd_usb *usb) |
| 775 | { |
| 776 | struct zd_usb_tx *tx = &usb->tx; |
| 777 | unsigned long flags; |
| 778 | struct list_head *pos, *n; |
| 779 | |
| 780 | spin_lock_irqsave(&tx->lock, flags); |
| 781 | list_for_each_safe(pos, n, &tx->free_urb_list) { |
| 782 | list_del(pos); |
| 783 | usb_free_urb(list_entry(pos, struct urb, urb_list)); |
| 784 | } |
| 785 | tx->enabled = 0; |
| 786 | tx->submitted_urbs = 0; |
| 787 | /* The stopped state is ignored, relying on ieee80211_wake_queues() |
| 788 | * in a potentionally following zd_usb_enable_tx(). |
| 789 | */ |
| 790 | spin_unlock_irqrestore(&tx->lock, flags); |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * zd_usb_enable_tx - enables transmission |
| 795 | * @usb: a &struct zd_usb pointer |
| 796 | * |
| 797 | * This function enables transmission and prepares the &zd_usb_tx data |
| 798 | * structure. |
| 799 | */ |
| 800 | void zd_usb_enable_tx(struct zd_usb *usb) |
| 801 | { |
| 802 | unsigned long flags; |
| 803 | struct zd_usb_tx *tx = &usb->tx; |
| 804 | |
| 805 | spin_lock_irqsave(&tx->lock, flags); |
| 806 | tx->enabled = 1; |
| 807 | tx->submitted_urbs = 0; |
| 808 | ieee80211_wake_queues(zd_usb_to_hw(usb)); |
| 809 | tx->stopped = 0; |
| 810 | spin_unlock_irqrestore(&tx->lock, flags); |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * alloc_tx_urb - provides an tx URB |
| 815 | * @usb: a &struct zd_usb pointer |
| 816 | * |
| 817 | * Allocates a new URB. If possible takes the urb from the free list in |
| 818 | * usb->tx. |
| 819 | */ |
| 820 | static struct urb *alloc_tx_urb(struct zd_usb *usb) |
| 821 | { |
| 822 | struct zd_usb_tx *tx = &usb->tx; |
| 823 | unsigned long flags; |
| 824 | struct list_head *entry; |
| 825 | struct urb *urb; |
| 826 | |
| 827 | spin_lock_irqsave(&tx->lock, flags); |
| 828 | if (list_empty(&tx->free_urb_list)) { |
| 829 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 830 | goto out; |
| 831 | } |
| 832 | entry = tx->free_urb_list.next; |
| 833 | list_del(entry); |
| 834 | urb = list_entry(entry, struct urb, urb_list); |
| 835 | out: |
| 836 | spin_unlock_irqrestore(&tx->lock, flags); |
| 837 | return urb; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * free_tx_urb - frees a used tx URB |
| 842 | * @usb: a &struct zd_usb pointer |
| 843 | * @urb: URB to be freed |
| 844 | * |
| 845 | * Frees the the transmission URB, which means to put it on the free URB |
| 846 | * list. |
| 847 | */ |
| 848 | static void free_tx_urb(struct zd_usb *usb, struct urb *urb) |
| 849 | { |
| 850 | struct zd_usb_tx *tx = &usb->tx; |
| 851 | unsigned long flags; |
| 852 | |
| 853 | spin_lock_irqsave(&tx->lock, flags); |
| 854 | if (!tx->enabled) { |
| 855 | usb_free_urb(urb); |
| 856 | goto out; |
| 857 | } |
| 858 | list_add(&urb->urb_list, &tx->free_urb_list); |
| 859 | out: |
| 860 | spin_unlock_irqrestore(&tx->lock, flags); |
| 861 | } |
| 862 | |
| 863 | static void tx_dec_submitted_urbs(struct zd_usb *usb) |
| 864 | { |
| 865 | struct zd_usb_tx *tx = &usb->tx; |
| 866 | unsigned long flags; |
| 867 | |
| 868 | spin_lock_irqsave(&tx->lock, flags); |
| 869 | --tx->submitted_urbs; |
| 870 | if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) { |
| 871 | ieee80211_wake_queues(zd_usb_to_hw(usb)); |
| 872 | tx->stopped = 0; |
| 873 | } |
| 874 | spin_unlock_irqrestore(&tx->lock, flags); |
| 875 | } |
| 876 | |
| 877 | static void tx_inc_submitted_urbs(struct zd_usb *usb) |
| 878 | { |
| 879 | struct zd_usb_tx *tx = &usb->tx; |
| 880 | unsigned long flags; |
| 881 | |
| 882 | spin_lock_irqsave(&tx->lock, flags); |
| 883 | ++tx->submitted_urbs; |
| 884 | if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) { |
| 885 | ieee80211_stop_queues(zd_usb_to_hw(usb)); |
| 886 | tx->stopped = 1; |
| 887 | } |
| 888 | spin_unlock_irqrestore(&tx->lock, flags); |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * tx_urb_complete - completes the execution of an URB |
| 893 | * @urb: a URB |
| 894 | * |
| 895 | * This function is called if the URB has been transferred to a device or an |
| 896 | * error has happened. |
| 897 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 898 | static void tx_urb_complete(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 899 | { |
| 900 | int r; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 901 | struct sk_buff *skb; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 902 | struct ieee80211_tx_info *info; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 903 | struct zd_usb *usb; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 904 | |
| 905 | switch (urb->status) { |
| 906 | case 0: |
| 907 | break; |
| 908 | case -ESHUTDOWN: |
| 909 | case -EINVAL: |
| 910 | case -ENODEV: |
| 911 | case -ENOENT: |
| 912 | case -ECONNRESET: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 913 | case -EPIPE: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 914 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 915 | break; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 916 | default: |
| 917 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 918 | goto resubmit; |
| 919 | } |
| 920 | free_urb: |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 921 | skb = (struct sk_buff *)urb->context; |
Johannes Berg | dbabad0 | 2008-05-08 01:43:59 +0200 | [diff] [blame] | 922 | /* |
| 923 | * grab 'usb' pointer before handing off the skb (since |
| 924 | * it might be freed by zd_mac_tx_to_dev or mac80211) |
| 925 | */ |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 926 | info = IEEE80211_SKB_CB(skb); |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 927 | usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb; |
Johannes Berg | dbabad0 | 2008-05-08 01:43:59 +0200 | [diff] [blame] | 928 | zd_mac_tx_to_dev(skb, urb->status); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 929 | free_tx_urb(usb, urb); |
| 930 | tx_dec_submitted_urbs(usb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 931 | return; |
| 932 | resubmit: |
| 933 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 934 | if (r) { |
| 935 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); |
| 936 | goto free_urb; |
| 937 | } |
| 938 | } |
| 939 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 940 | /** |
| 941 | * zd_usb_tx: initiates transfer of a frame of the device |
| 942 | * |
| 943 | * @usb: the zd1211rw-private USB structure |
| 944 | * @skb: a &struct sk_buff pointer |
| 945 | * |
| 946 | * This function tranmits a frame to the device. It doesn't wait for |
| 947 | * completion. The frame must contain the control set and have all the |
| 948 | * control set information available. |
| 949 | * |
| 950 | * The function returns 0 if the transfer has been successfully initiated. |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 951 | */ |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 952 | int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 953 | { |
| 954 | int r; |
| 955 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
| 956 | struct urb *urb; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 957 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 958 | urb = alloc_tx_urb(usb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 959 | if (!urb) { |
| 960 | r = -ENOMEM; |
| 961 | goto out; |
| 962 | } |
| 963 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 964 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 965 | skb->data, skb->len, tx_urb_complete, skb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 966 | |
| 967 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 968 | if (r) |
| 969 | goto error; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 970 | tx_inc_submitted_urbs(usb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 971 | return 0; |
| 972 | error: |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 973 | free_tx_urb(usb, urb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 974 | out: |
| 975 | return r; |
| 976 | } |
| 977 | |
| 978 | static inline void init_usb_interrupt(struct zd_usb *usb) |
| 979 | { |
| 980 | struct zd_usb_interrupt *intr = &usb->intr; |
| 981 | |
| 982 | spin_lock_init(&intr->lock); |
| 983 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); |
| 984 | init_completion(&intr->read_regs.completion); |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 985 | intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | static inline void init_usb_rx(struct zd_usb *usb) |
| 989 | { |
| 990 | struct zd_usb_rx *rx = &usb->rx; |
| 991 | spin_lock_init(&rx->lock); |
| 992 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { |
| 993 | rx->usb_packet_size = 512; |
| 994 | } else { |
| 995 | rx->usb_packet_size = 64; |
| 996 | } |
| 997 | ZD_ASSERT(rx->fragment_length == 0); |
| 998 | } |
| 999 | |
| 1000 | static inline void init_usb_tx(struct zd_usb *usb) |
| 1001 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1002 | struct zd_usb_tx *tx = &usb->tx; |
| 1003 | spin_lock_init(&tx->lock); |
| 1004 | tx->enabled = 0; |
| 1005 | tx->stopped = 0; |
| 1006 | INIT_LIST_HEAD(&tx->free_urb_list); |
| 1007 | tx->submitted_urbs = 0; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1010 | void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1011 | struct usb_interface *intf) |
| 1012 | { |
| 1013 | memset(usb, 0, sizeof(*usb)); |
| 1014 | usb->intf = usb_get_intf(intf); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1015 | usb_set_intfdata(usb->intf, hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1016 | init_usb_interrupt(usb); |
| 1017 | init_usb_tx(usb); |
| 1018 | init_usb_rx(usb); |
| 1019 | } |
| 1020 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1021 | void zd_usb_clear(struct zd_usb *usb) |
| 1022 | { |
| 1023 | usb_set_intfdata(usb->intf, NULL); |
| 1024 | usb_put_intf(usb->intf); |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 1025 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1026 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
| 1027 | } |
| 1028 | |
| 1029 | static const char *speed(enum usb_device_speed speed) |
| 1030 | { |
| 1031 | switch (speed) { |
| 1032 | case USB_SPEED_LOW: |
| 1033 | return "low"; |
| 1034 | case USB_SPEED_FULL: |
| 1035 | return "full"; |
| 1036 | case USB_SPEED_HIGH: |
| 1037 | return "high"; |
| 1038 | default: |
| 1039 | return "unknown speed"; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) |
| 1044 | { |
| 1045 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", |
| 1046 | le16_to_cpu(udev->descriptor.idVendor), |
| 1047 | le16_to_cpu(udev->descriptor.idProduct), |
| 1048 | get_bcdDevice(udev), |
| 1049 | speed(udev->speed)); |
| 1050 | } |
| 1051 | |
| 1052 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) |
| 1053 | { |
| 1054 | struct usb_device *udev = interface_to_usbdev(usb->intf); |
| 1055 | return scnprint_id(udev, buffer, size); |
| 1056 | } |
| 1057 | |
| 1058 | #ifdef DEBUG |
| 1059 | static void print_id(struct usb_device *udev) |
| 1060 | { |
| 1061 | char buffer[40]; |
| 1062 | |
| 1063 | scnprint_id(udev, buffer, sizeof(buffer)); |
| 1064 | buffer[sizeof(buffer)-1] = 0; |
| 1065 | dev_dbg_f(&udev->dev, "%s\n", buffer); |
| 1066 | } |
| 1067 | #else |
| 1068 | #define print_id(udev) do { } while (0) |
| 1069 | #endif |
| 1070 | |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1071 | static int eject_installer(struct usb_interface *intf) |
| 1072 | { |
| 1073 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1074 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; |
| 1075 | struct usb_endpoint_descriptor *endpoint; |
| 1076 | unsigned char *cmd; |
| 1077 | u8 bulk_out_ep; |
| 1078 | int r; |
| 1079 | |
| 1080 | /* Find bulk out endpoint */ |
Stefan Seyfried | 11466f1 | 2009-12-08 15:21:35 +0100 | [diff] [blame^] | 1081 | for (r = 1; r >= 0; r--) { |
| 1082 | endpoint = &iface_desc->endpoint[r].desc; |
| 1083 | if (usb_endpoint_dir_out(endpoint) && |
| 1084 | usb_endpoint_xfer_bulk(endpoint)) { |
| 1085 | bulk_out_ep = endpoint->bEndpointAddress; |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | if (r == -1) { |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1090 | dev_err(&udev->dev, |
| 1091 | "zd1211rw: Could not find bulk out endpoint\n"); |
| 1092 | return -ENODEV; |
| 1093 | } |
| 1094 | |
| 1095 | cmd = kzalloc(31, GFP_KERNEL); |
| 1096 | if (cmd == NULL) |
| 1097 | return -ENODEV; |
| 1098 | |
| 1099 | /* USB bulk command block */ |
| 1100 | cmd[0] = 0x55; /* bulk command signature */ |
| 1101 | cmd[1] = 0x53; /* bulk command signature */ |
| 1102 | cmd[2] = 0x42; /* bulk command signature */ |
| 1103 | cmd[3] = 0x43; /* bulk command signature */ |
| 1104 | cmd[14] = 6; /* command length */ |
| 1105 | |
| 1106 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ |
| 1107 | cmd[19] = 0x2; /* eject disc */ |
| 1108 | |
| 1109 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); |
| 1110 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), |
| 1111 | cmd, 31, NULL, 2000); |
| 1112 | kfree(cmd); |
| 1113 | if (r) |
| 1114 | return r; |
| 1115 | |
| 1116 | /* At this point, the device disconnects and reconnects with the real |
| 1117 | * ID numbers. */ |
| 1118 | |
| 1119 | usb_set_intfdata(intf, NULL); |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 1123 | int zd_usb_init_hw(struct zd_usb *usb) |
| 1124 | { |
| 1125 | int r; |
| 1126 | struct zd_mac *mac = zd_usb_to_mac(usb); |
| 1127 | |
| 1128 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 1129 | |
| 1130 | r = upload_firmware(usb); |
| 1131 | if (r) { |
| 1132 | dev_err(zd_usb_dev(usb), |
| 1133 | "couldn't load firmware. Error number %d\n", r); |
| 1134 | return r; |
| 1135 | } |
| 1136 | |
| 1137 | r = usb_reset_configuration(zd_usb_to_usbdev(usb)); |
| 1138 | if (r) { |
| 1139 | dev_dbg_f(zd_usb_dev(usb), |
| 1140 | "couldn't reset configuration. Error number %d\n", r); |
| 1141 | return r; |
| 1142 | } |
| 1143 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1144 | r = zd_mac_init_hw(mac->hw); |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 1145 | if (r) { |
| 1146 | dev_dbg_f(zd_usb_dev(usb), |
| 1147 | "couldn't initialize mac. Error number %d\n", r); |
| 1148 | return r; |
| 1149 | } |
| 1150 | |
| 1151 | usb->initialized = 1; |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1155 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 1156 | { |
| 1157 | int r; |
| 1158 | struct usb_device *udev = interface_to_usbdev(intf); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1159 | struct zd_usb *usb; |
| 1160 | struct ieee80211_hw *hw = NULL; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1161 | |
| 1162 | print_id(udev); |
| 1163 | |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1164 | if (id->driver_info & DEVICE_INSTALLER) |
| 1165 | return eject_installer(intf); |
| 1166 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1167 | switch (udev->speed) { |
| 1168 | case USB_SPEED_LOW: |
| 1169 | case USB_SPEED_FULL: |
| 1170 | case USB_SPEED_HIGH: |
| 1171 | break; |
| 1172 | default: |
| 1173 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); |
| 1174 | r = -ENODEV; |
| 1175 | goto error; |
| 1176 | } |
| 1177 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1178 | r = usb_reset_device(udev); |
| 1179 | if (r) { |
| 1180 | dev_err(&intf->dev, |
| 1181 | "couldn't reset usb device. Error number %d\n", r); |
| 1182 | goto error; |
| 1183 | } |
Ulrich Kunitz | 6e3632f | 2007-01-29 00:59:28 +0000 | [diff] [blame] | 1184 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1185 | hw = zd_mac_alloc_hw(intf); |
| 1186 | if (hw == NULL) { |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1187 | r = -ENOMEM; |
| 1188 | goto error; |
| 1189 | } |
| 1190 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1191 | usb = &zd_hw_mac(hw)->chip.usb; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 1192 | usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1193 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1194 | r = zd_mac_preinit_hw(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1195 | if (r) { |
| 1196 | dev_dbg_f(&intf->dev, |
| 1197 | "couldn't initialize mac. Error number %d\n", r); |
| 1198 | goto error; |
| 1199 | } |
| 1200 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1201 | r = ieee80211_register_hw(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1202 | if (r) { |
| 1203 | dev_dbg_f(&intf->dev, |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1204 | "couldn't register device. Error number %d\n", r); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1205 | goto error; |
| 1206 | } |
| 1207 | |
| 1208 | dev_dbg_f(&intf->dev, "successful\n"); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1209 | dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1210 | return 0; |
| 1211 | error: |
| 1212 | usb_reset_device(interface_to_usbdev(intf)); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1213 | if (hw) { |
| 1214 | zd_mac_clear(zd_hw_mac(hw)); |
| 1215 | ieee80211_free_hw(hw); |
| 1216 | } |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1217 | return r; |
| 1218 | } |
| 1219 | |
| 1220 | static void disconnect(struct usb_interface *intf) |
| 1221 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1222 | struct ieee80211_hw *hw = zd_intf_to_hw(intf); |
Marc Pignat | e0579d5 | 2007-10-15 08:51:52 +0200 | [diff] [blame] | 1223 | struct zd_mac *mac; |
| 1224 | struct zd_usb *usb; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1225 | |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1226 | /* Either something really bad happened, or we're just dealing with |
| 1227 | * a DEVICE_INSTALLER. */ |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1228 | if (hw == NULL) |
Daniel Drake | a1030e92 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1229 | return; |
| 1230 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1231 | mac = zd_hw_mac(hw); |
Marc Pignat | e0579d5 | 2007-10-15 08:51:52 +0200 | [diff] [blame] | 1232 | usb = &mac->chip.usb; |
| 1233 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1234 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 1235 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1236 | ieee80211_unregister_hw(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1237 | |
| 1238 | /* Just in case something has gone wrong! */ |
| 1239 | zd_usb_disable_rx(usb); |
| 1240 | zd_usb_disable_int(usb); |
| 1241 | |
| 1242 | /* If the disconnect has been caused by a removal of the |
| 1243 | * driver module, the reset allows reloading of the driver. If the |
| 1244 | * reset will not be executed here, the upload of the firmware in the |
| 1245 | * probe function caused by the reloading of the driver will fail. |
| 1246 | */ |
| 1247 | usb_reset_device(interface_to_usbdev(intf)); |
| 1248 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1249 | zd_mac_clear(mac); |
| 1250 | ieee80211_free_hw(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1251 | dev_dbg(&intf->dev, "disconnected\n"); |
| 1252 | } |
| 1253 | |
| 1254 | static struct usb_driver driver = { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 1255 | .name = KBUILD_MODNAME, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1256 | .id_table = usb_ids, |
| 1257 | .probe = probe, |
| 1258 | .disconnect = disconnect, |
| 1259 | }; |
| 1260 | |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1261 | struct workqueue_struct *zd_workqueue; |
| 1262 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1263 | static int __init usb_init(void) |
| 1264 | { |
| 1265 | int r; |
| 1266 | |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1267 | pr_debug("%s usb_init()\n", driver.name); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1268 | |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1269 | zd_workqueue = create_singlethread_workqueue(driver.name); |
| 1270 | if (zd_workqueue == NULL) { |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1271 | printk(KERN_ERR "%s couldn't create workqueue\n", driver.name); |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1272 | return -ENOMEM; |
| 1273 | } |
| 1274 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1275 | r = usb_register(&driver); |
| 1276 | if (r) { |
Maxime Austruy | 192b775 | 2007-01-29 00:59:51 +0000 | [diff] [blame] | 1277 | destroy_workqueue(zd_workqueue); |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1278 | printk(KERN_ERR "%s usb_register() failed. Error number %d\n", |
| 1279 | driver.name, r); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1280 | return r; |
| 1281 | } |
| 1282 | |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1283 | pr_debug("%s initialized\n", driver.name); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | static void __exit usb_exit(void) |
| 1288 | { |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1289 | pr_debug("%s usb_exit()\n", driver.name); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1290 | usb_deregister(&driver); |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1291 | destroy_workqueue(zd_workqueue); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | module_init(usb_init); |
| 1295 | module_exit(usb_exit); |
| 1296 | |
| 1297 | static int usb_int_regs_length(unsigned int count) |
| 1298 | { |
| 1299 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); |
| 1300 | } |
| 1301 | |
| 1302 | static void prepare_read_regs_int(struct zd_usb *usb) |
| 1303 | { |
| 1304 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1305 | |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1306 | spin_lock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1307 | intr->read_regs_enabled = 1; |
| 1308 | INIT_COMPLETION(intr->read_regs.completion); |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1309 | spin_unlock_irq(&intr->lock); |
| 1310 | } |
| 1311 | |
| 1312 | static void disable_read_regs_int(struct zd_usb *usb) |
| 1313 | { |
| 1314 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1315 | |
| 1316 | spin_lock_irq(&intr->lock); |
| 1317 | intr->read_regs_enabled = 0; |
| 1318 | spin_unlock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | static int get_results(struct zd_usb *usb, u16 *values, |
| 1322 | struct usb_req_read_regs *req, unsigned int count) |
| 1323 | { |
| 1324 | int r; |
| 1325 | int i; |
| 1326 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1327 | struct read_regs_int *rr = &intr->read_regs; |
| 1328 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; |
| 1329 | |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1330 | spin_lock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1331 | |
| 1332 | r = -EIO; |
| 1333 | /* The created block size seems to be larger than expected. |
| 1334 | * However results appear to be correct. |
| 1335 | */ |
| 1336 | if (rr->length < usb_int_regs_length(count)) { |
| 1337 | dev_dbg_f(zd_usb_dev(usb), |
| 1338 | "error: actual length %d less than expected %d\n", |
| 1339 | rr->length, usb_int_regs_length(count)); |
| 1340 | goto error_unlock; |
| 1341 | } |
| 1342 | if (rr->length > sizeof(rr->buffer)) { |
| 1343 | dev_dbg_f(zd_usb_dev(usb), |
| 1344 | "error: actual length %d exceeds buffer size %zu\n", |
| 1345 | rr->length, sizeof(rr->buffer)); |
| 1346 | goto error_unlock; |
| 1347 | } |
| 1348 | |
| 1349 | for (i = 0; i < count; i++) { |
| 1350 | struct reg_data *rd = ®s->regs[i]; |
| 1351 | if (rd->addr != req->addr[i]) { |
| 1352 | dev_dbg_f(zd_usb_dev(usb), |
| 1353 | "rd[%d] addr %#06hx expected %#06hx\n", i, |
| 1354 | le16_to_cpu(rd->addr), |
| 1355 | le16_to_cpu(req->addr[i])); |
| 1356 | goto error_unlock; |
| 1357 | } |
| 1358 | values[i] = le16_to_cpu(rd->value); |
| 1359 | } |
| 1360 | |
| 1361 | r = 0; |
| 1362 | error_unlock: |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1363 | spin_unlock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1364 | return r; |
| 1365 | } |
| 1366 | |
| 1367 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, |
| 1368 | const zd_addr_t *addresses, unsigned int count) |
| 1369 | { |
| 1370 | int r; |
| 1371 | int i, req_len, actual_req_len; |
| 1372 | struct usb_device *udev; |
| 1373 | struct usb_req_read_regs *req = NULL; |
| 1374 | unsigned long timeout; |
| 1375 | |
| 1376 | if (count < 1) { |
| 1377 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); |
| 1378 | return -EINVAL; |
| 1379 | } |
| 1380 | if (count > USB_MAX_IOREAD16_COUNT) { |
| 1381 | dev_dbg_f(zd_usb_dev(usb), |
| 1382 | "error: count %u exceeds possible max %u\n", |
| 1383 | count, USB_MAX_IOREAD16_COUNT); |
| 1384 | return -EINVAL; |
| 1385 | } |
| 1386 | if (in_atomic()) { |
| 1387 | dev_dbg_f(zd_usb_dev(usb), |
| 1388 | "error: io in atomic context not supported\n"); |
| 1389 | return -EWOULDBLOCK; |
| 1390 | } |
| 1391 | if (!usb_int_enabled(usb)) { |
| 1392 | dev_dbg_f(zd_usb_dev(usb), |
| 1393 | "error: usb interrupt not enabled\n"); |
| 1394 | return -EWOULDBLOCK; |
| 1395 | } |
| 1396 | |
| 1397 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 1398 | req = kmalloc(req_len, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1399 | if (!req) |
| 1400 | return -ENOMEM; |
| 1401 | req->id = cpu_to_le16(USB_REQ_READ_REGS); |
| 1402 | for (i = 0; i < count; i++) |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 1403 | req->addr[i] = cpu_to_le16((u16)addresses[i]); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1404 | |
| 1405 | udev = zd_usb_to_usbdev(usb); |
| 1406 | prepare_read_regs_int(usb); |
| 1407 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1408 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1409 | if (r) { |
| 1410 | dev_dbg_f(zd_usb_dev(usb), |
| 1411 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1412 | goto error; |
| 1413 | } |
| 1414 | if (req_len != actual_req_len) { |
| 1415 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n" |
| 1416 | " req_len %d != actual_req_len %d\n", |
| 1417 | req_len, actual_req_len); |
| 1418 | r = -EIO; |
| 1419 | goto error; |
| 1420 | } |
| 1421 | |
| 1422 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, |
| 1423 | msecs_to_jiffies(1000)); |
| 1424 | if (!timeout) { |
| 1425 | disable_read_regs_int(usb); |
| 1426 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); |
| 1427 | r = -ETIMEDOUT; |
| 1428 | goto error; |
| 1429 | } |
| 1430 | |
| 1431 | r = get_results(usb, values, req, count); |
| 1432 | error: |
| 1433 | kfree(req); |
| 1434 | return r; |
| 1435 | } |
| 1436 | |
| 1437 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
| 1438 | unsigned int count) |
| 1439 | { |
| 1440 | int r; |
| 1441 | struct usb_device *udev; |
| 1442 | struct usb_req_write_regs *req = NULL; |
| 1443 | int i, req_len, actual_req_len; |
| 1444 | |
| 1445 | if (count == 0) |
| 1446 | return 0; |
| 1447 | if (count > USB_MAX_IOWRITE16_COUNT) { |
| 1448 | dev_dbg_f(zd_usb_dev(usb), |
| 1449 | "error: count %u exceeds possible max %u\n", |
| 1450 | count, USB_MAX_IOWRITE16_COUNT); |
| 1451 | return -EINVAL; |
| 1452 | } |
| 1453 | if (in_atomic()) { |
| 1454 | dev_dbg_f(zd_usb_dev(usb), |
| 1455 | "error: io in atomic context not supported\n"); |
| 1456 | return -EWOULDBLOCK; |
| 1457 | } |
| 1458 | |
| 1459 | req_len = sizeof(struct usb_req_write_regs) + |
| 1460 | count * sizeof(struct reg_data); |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 1461 | req = kmalloc(req_len, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1462 | if (!req) |
| 1463 | return -ENOMEM; |
| 1464 | |
| 1465 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); |
| 1466 | for (i = 0; i < count; i++) { |
| 1467 | struct reg_data *rw = &req->reg_writes[i]; |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 1468 | rw->addr = cpu_to_le16((u16)ioreqs[i].addr); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1469 | rw->value = cpu_to_le16(ioreqs[i].value); |
| 1470 | } |
| 1471 | |
| 1472 | udev = zd_usb_to_usbdev(usb); |
| 1473 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1474 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1475 | if (r) { |
| 1476 | dev_dbg_f(zd_usb_dev(usb), |
| 1477 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1478 | goto error; |
| 1479 | } |
| 1480 | if (req_len != actual_req_len) { |
| 1481 | dev_dbg_f(zd_usb_dev(usb), |
| 1482 | "error in usb_bulk_msg()" |
| 1483 | " req_len %d != actual_req_len %d\n", |
| 1484 | req_len, actual_req_len); |
| 1485 | r = -EIO; |
| 1486 | goto error; |
| 1487 | } |
| 1488 | |
| 1489 | /* FALL-THROUGH with r == 0 */ |
| 1490 | error: |
| 1491 | kfree(req); |
| 1492 | return r; |
| 1493 | } |
| 1494 | |
| 1495 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) |
| 1496 | { |
| 1497 | int r; |
| 1498 | struct usb_device *udev; |
| 1499 | struct usb_req_rfwrite *req = NULL; |
| 1500 | int i, req_len, actual_req_len; |
| 1501 | u16 bit_value_template; |
| 1502 | |
| 1503 | if (in_atomic()) { |
| 1504 | dev_dbg_f(zd_usb_dev(usb), |
| 1505 | "error: io in atomic context not supported\n"); |
| 1506 | return -EWOULDBLOCK; |
| 1507 | } |
| 1508 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { |
| 1509 | dev_dbg_f(zd_usb_dev(usb), |
| 1510 | "error: bits %d are smaller than" |
| 1511 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", |
| 1512 | bits, USB_MIN_RFWRITE_BIT_COUNT); |
| 1513 | return -EINVAL; |
| 1514 | } |
| 1515 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { |
| 1516 | dev_dbg_f(zd_usb_dev(usb), |
| 1517 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", |
| 1518 | bits, USB_MAX_RFWRITE_BIT_COUNT); |
| 1519 | return -EINVAL; |
| 1520 | } |
| 1521 | #ifdef DEBUG |
| 1522 | if (value & (~0UL << bits)) { |
| 1523 | dev_dbg_f(zd_usb_dev(usb), |
| 1524 | "error: value %#09x has bits >= %d set\n", |
| 1525 | value, bits); |
| 1526 | return -EINVAL; |
| 1527 | } |
| 1528 | #endif /* DEBUG */ |
| 1529 | |
| 1530 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); |
| 1531 | |
| 1532 | r = zd_usb_ioread16(usb, &bit_value_template, CR203); |
| 1533 | if (r) { |
| 1534 | dev_dbg_f(zd_usb_dev(usb), |
| 1535 | "error %d: Couldn't read CR203\n", r); |
| 1536 | goto out; |
| 1537 | } |
| 1538 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); |
| 1539 | |
| 1540 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 1541 | req = kmalloc(req_len, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1542 | if (!req) |
| 1543 | return -ENOMEM; |
| 1544 | |
| 1545 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); |
| 1546 | /* 1: 3683a, but not used in ZYDAS driver */ |
| 1547 | req->value = cpu_to_le16(2); |
| 1548 | req->bits = cpu_to_le16(bits); |
| 1549 | |
| 1550 | for (i = 0; i < bits; i++) { |
| 1551 | u16 bv = bit_value_template; |
| 1552 | if (value & (1 << (bits-1-i))) |
| 1553 | bv |= RF_DATA; |
| 1554 | req->bit_values[i] = cpu_to_le16(bv); |
| 1555 | } |
| 1556 | |
| 1557 | udev = zd_usb_to_usbdev(usb); |
| 1558 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1559 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1560 | if (r) { |
| 1561 | dev_dbg_f(zd_usb_dev(usb), |
| 1562 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1563 | goto out; |
| 1564 | } |
| 1565 | if (req_len != actual_req_len) { |
| 1566 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()" |
| 1567 | " req_len %d != actual_req_len %d\n", |
| 1568 | req_len, actual_req_len); |
| 1569 | r = -EIO; |
| 1570 | goto out; |
| 1571 | } |
| 1572 | |
| 1573 | /* FALL-THROUGH with r == 0 */ |
| 1574 | out: |
| 1575 | kfree(req); |
| 1576 | return r; |
| 1577 | } |