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