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> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/firmware.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/skbuff.h> |
| 25 | #include <linux/usb.h> |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 27 | #include <net/ieee80211.h> |
Al Viro | c248725 | 2007-07-15 21:00:21 +0100 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 29 | |
| 30 | #include "zd_def.h" |
| 31 | #include "zd_netdev.h" |
| 32 | #include "zd_mac.h" |
| 33 | #include "zd_usb.h" |
| 34 | #include "zd_util.h" |
| 35 | |
| 36 | static struct usb_device_id usb_ids[] = { |
| 37 | /* ZD1211 */ |
| 38 | { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 }, |
| 39 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 }, |
| 40 | { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 }, |
| 41 | { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 }, |
| 42 | { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 }, |
Matthew Davidson | aa1d3a1 | 2007-05-01 17:14:30 +0100 | [diff] [blame] | 43 | { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 44 | { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | dd2f553 | 2006-07-05 15:57:31 +0100 | [diff] [blame] | 45 | { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 1b86549 | 2006-08-12 17:59:50 +0100 | [diff] [blame] | 46 | { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 12f3930 | 2006-08-12 17:59:51 +0100 | [diff] [blame] | 47 | { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 4ceb7e9 | 2006-08-12 18:00:03 +0100 | [diff] [blame] | 48 | { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | fca2714 | 2006-08-29 23:49:32 +0100 | [diff] [blame] | 49 | { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | fc3e39b | 2006-08-29 23:49:53 +0100 | [diff] [blame] | 50 | { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | f242372 | 2006-09-26 04:04:38 +0100 | [diff] [blame] | 51 | { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 }, |
| 52 | { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 }, |
| 53 | { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | a362bf5 | 2006-10-18 00:17:02 +0100 | [diff] [blame] | 54 | { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | 229782a | 2006-11-17 00:13:48 +0000 | [diff] [blame] | 55 | { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | a3fa3ab | 2007-03-26 01:00:12 +0100 | [diff] [blame] | 56 | { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | a9eea9a | 2007-05-24 01:06:29 +0100 | [diff] [blame] | 57 | { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 }, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 58 | /* ZD1211B */ |
| 59 | { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B }, |
| 60 | { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | dd2f553 | 2006-07-05 15:57:31 +0100 | [diff] [blame] | 61 | { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B }, |
Ulrich Kunitz | 943599e | 2006-08-12 18:00:05 +0100 | [diff] [blame] | 62 | { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 8e87295 | 2006-11-17 00:26:40 +0000 | [diff] [blame] | 63 | { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | a2bdcc6 | 2006-12-12 01:25:37 +0000 | [diff] [blame] | 64 | { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B }, |
| 65 | { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 33218ba | 2006-12-30 22:38:23 +0000 | [diff] [blame] | 66 | { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 2e9b246 | 2007-01-04 03:33:54 +0000 | [diff] [blame] | 67 | { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | aec9102 | 2007-03-11 19:54:39 +0000 | [diff] [blame] | 68 | { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B }, |
| 69 | { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B }, |
S.Çağlar Onur | c445a31 | 2007-04-22 00:52:48 +0300 | [diff] [blame] | 70 | { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 5d082fe | 2007-04-07 16:00:43 +0100 | [diff] [blame] | 71 | { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 187bb41 | 2007-05-01 17:14:51 +0100 | [diff] [blame] | 72 | { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B }, |
Ulrich Kunitz | 019a675 | 2007-05-01 17:13:51 +0100 | [diff] [blame] | 73 | { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 93f510b | 2007-07-01 18:22:21 +0100 | [diff] [blame] | 74 | { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B }, |
Masakazu Mokuno | 6eb0725 | 2007-07-14 01:40:11 +0100 | [diff] [blame] | 75 | { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 2534346 | 2007-07-14 01:40:54 +0100 | [diff] [blame] | 76 | { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 8e97afe | 2007-08-06 01:24:19 +0100 | [diff] [blame] | 77 | { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B }, |
Daniel Drake | 475fed1 | 2007-08-06 02:25:18 +0100 | [diff] [blame^] | 78 | { USB_DEVICE(0x0586, 0x340a), .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 | |
| 355 | static inline void handle_retry_failed_int(struct urb *urb) |
| 356 | { |
Ulrich Kunitz | 22d3405 | 2007-01-29 01:00:03 +0000 | [diff] [blame] | 357 | struct zd_usb *usb = urb->context; |
| 358 | struct zd_mac *mac = zd_usb_to_mac(usb); |
| 359 | struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac); |
| 360 | |
| 361 | ieee->stats.tx_errors++; |
| 362 | ieee->ieee_stats.tx_retry_limit_exceeded++; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 363 | dev_dbg_f(urb_dev(urb), "retry failed interrupt\n"); |
| 364 | } |
| 365 | |
| 366 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 367 | static void int_urb_complete(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 368 | { |
| 369 | int r; |
| 370 | struct usb_int_header *hdr; |
| 371 | |
| 372 | switch (urb->status) { |
| 373 | case 0: |
| 374 | break; |
| 375 | case -ESHUTDOWN: |
| 376 | case -EINVAL: |
| 377 | case -ENODEV: |
| 378 | case -ENOENT: |
| 379 | case -ECONNRESET: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 380 | case -EPIPE: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 381 | goto kfree; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 382 | default: |
| 383 | goto resubmit; |
| 384 | } |
| 385 | |
| 386 | if (urb->actual_length < sizeof(hdr)) { |
| 387 | dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb); |
| 388 | goto resubmit; |
| 389 | } |
| 390 | |
| 391 | hdr = urb->transfer_buffer; |
| 392 | if (hdr->type != USB_INT_TYPE) { |
| 393 | dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb); |
| 394 | goto resubmit; |
| 395 | } |
| 396 | |
| 397 | switch (hdr->id) { |
| 398 | case USB_INT_ID_REGS: |
| 399 | handle_regs_int(urb); |
| 400 | break; |
| 401 | case USB_INT_ID_RETRY_FAILED: |
| 402 | handle_retry_failed_int(urb); |
| 403 | break; |
| 404 | default: |
| 405 | dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb, |
| 406 | (unsigned int)hdr->id); |
| 407 | goto resubmit; |
| 408 | } |
| 409 | |
| 410 | resubmit: |
| 411 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 412 | if (r) { |
| 413 | dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb); |
| 414 | goto kfree; |
| 415 | } |
| 416 | return; |
| 417 | kfree: |
| 418 | kfree(urb->transfer_buffer); |
| 419 | } |
| 420 | |
| 421 | static inline int int_urb_interval(struct usb_device *udev) |
| 422 | { |
| 423 | switch (udev->speed) { |
| 424 | case USB_SPEED_HIGH: |
| 425 | return 4; |
| 426 | case USB_SPEED_LOW: |
| 427 | return 10; |
| 428 | case USB_SPEED_FULL: |
| 429 | default: |
| 430 | return 1; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | static inline int usb_int_enabled(struct zd_usb *usb) |
| 435 | { |
| 436 | unsigned long flags; |
| 437 | struct zd_usb_interrupt *intr = &usb->intr; |
| 438 | struct urb *urb; |
| 439 | |
| 440 | spin_lock_irqsave(&intr->lock, flags); |
| 441 | urb = intr->urb; |
| 442 | spin_unlock_irqrestore(&intr->lock, flags); |
| 443 | return urb != NULL; |
| 444 | } |
| 445 | |
| 446 | int zd_usb_enable_int(struct zd_usb *usb) |
| 447 | { |
| 448 | int r; |
| 449 | struct usb_device *udev; |
| 450 | struct zd_usb_interrupt *intr = &usb->intr; |
| 451 | void *transfer_buffer = NULL; |
| 452 | struct urb *urb; |
| 453 | |
| 454 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 455 | |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 456 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 457 | if (!urb) { |
| 458 | r = -ENOMEM; |
| 459 | goto out; |
| 460 | } |
| 461 | |
| 462 | ZD_ASSERT(!irqs_disabled()); |
| 463 | spin_lock_irq(&intr->lock); |
| 464 | if (intr->urb) { |
| 465 | spin_unlock_irq(&intr->lock); |
| 466 | r = 0; |
| 467 | goto error_free_urb; |
| 468 | } |
| 469 | intr->urb = urb; |
| 470 | spin_unlock_irq(&intr->lock); |
| 471 | |
| 472 | /* TODO: make it a DMA buffer */ |
| 473 | r = -ENOMEM; |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 474 | transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 475 | if (!transfer_buffer) { |
| 476 | dev_dbg_f(zd_usb_dev(usb), |
| 477 | "couldn't allocate transfer_buffer\n"); |
| 478 | goto error_set_urb_null; |
| 479 | } |
| 480 | |
| 481 | udev = zd_usb_to_usbdev(usb); |
| 482 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), |
| 483 | transfer_buffer, USB_MAX_EP_INT_BUFFER, |
| 484 | int_urb_complete, usb, |
| 485 | intr->interval); |
| 486 | |
| 487 | 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] | 488 | r = usb_submit_urb(urb, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 489 | if (r) { |
| 490 | dev_dbg_f(zd_usb_dev(usb), |
| 491 | "Couldn't submit urb. Error number %d\n", r); |
| 492 | goto error; |
| 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | error: |
| 497 | kfree(transfer_buffer); |
| 498 | error_set_urb_null: |
| 499 | spin_lock_irq(&intr->lock); |
| 500 | intr->urb = NULL; |
| 501 | spin_unlock_irq(&intr->lock); |
| 502 | error_free_urb: |
| 503 | usb_free_urb(urb); |
| 504 | out: |
| 505 | return r; |
| 506 | } |
| 507 | |
| 508 | void zd_usb_disable_int(struct zd_usb *usb) |
| 509 | { |
| 510 | unsigned long flags; |
| 511 | struct zd_usb_interrupt *intr = &usb->intr; |
| 512 | struct urb *urb; |
| 513 | |
| 514 | spin_lock_irqsave(&intr->lock, flags); |
| 515 | urb = intr->urb; |
| 516 | if (!urb) { |
| 517 | spin_unlock_irqrestore(&intr->lock, flags); |
| 518 | return; |
| 519 | } |
| 520 | intr->urb = NULL; |
| 521 | spin_unlock_irqrestore(&intr->lock, flags); |
| 522 | |
| 523 | usb_kill_urb(urb); |
| 524 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); |
| 525 | usb_free_urb(urb); |
| 526 | } |
| 527 | |
| 528 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, |
| 529 | unsigned int length) |
| 530 | { |
| 531 | int i; |
| 532 | struct zd_mac *mac = zd_usb_to_mac(usb); |
| 533 | const struct rx_length_info *length_info; |
| 534 | |
| 535 | if (length < sizeof(struct rx_length_info)) { |
| 536 | /* It's not a complete packet anyhow. */ |
Ulrich Kunitz | 22d3405 | 2007-01-29 01:00:03 +0000 | [diff] [blame] | 537 | struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac); |
| 538 | ieee->stats.rx_errors++; |
| 539 | ieee->stats.rx_length_errors++; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | length_info = (struct rx_length_info *) |
| 543 | (buffer + length - sizeof(struct rx_length_info)); |
| 544 | |
| 545 | /* It might be that three frames are merged into a single URB |
| 546 | * transaction. We have to check for the length info tag. |
| 547 | * |
| 548 | * While testing we discovered that length_info might be unaligned, |
| 549 | * because if USB transactions are merged, the last packet will not |
| 550 | * be padded. Unaligned access might also happen if the length_info |
| 551 | * structure is not present. |
| 552 | */ |
Ulrich Kunitz | b269825 | 2006-08-01 23:43:34 +0200 | [diff] [blame] | 553 | if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG)) |
| 554 | { |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 555 | unsigned int l, k, n; |
| 556 | for (i = 0, l = 0;; i++) { |
Ulrich Kunitz | b269825 | 2006-08-01 23:43:34 +0200 | [diff] [blame] | 557 | k = le16_to_cpu(get_unaligned(&length_info->length[i])); |
Ulrich Kunitz | 850c211 | 2006-11-22 00:06:19 +0000 | [diff] [blame] | 558 | if (k == 0) |
| 559 | return; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 560 | n = l+k; |
| 561 | if (n > length) |
| 562 | return; |
Ulrich Kunitz | 4d1feab | 2006-12-10 11:13:12 -0800 | [diff] [blame] | 563 | zd_mac_rx_irq(mac, buffer+l, k); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 564 | if (i >= 2) |
| 565 | return; |
| 566 | l = (n+3) & ~3; |
| 567 | } |
| 568 | } else { |
Ulrich Kunitz | 4d1feab | 2006-12-10 11:13:12 -0800 | [diff] [blame] | 569 | zd_mac_rx_irq(mac, buffer, length); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 573 | static void rx_urb_complete(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 574 | { |
| 575 | struct zd_usb *usb; |
| 576 | struct zd_usb_rx *rx; |
| 577 | const u8 *buffer; |
| 578 | unsigned int length; |
| 579 | |
| 580 | switch (urb->status) { |
| 581 | case 0: |
| 582 | break; |
| 583 | case -ESHUTDOWN: |
| 584 | case -EINVAL: |
| 585 | case -ENODEV: |
| 586 | case -ENOENT: |
| 587 | case -ECONNRESET: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 588 | case -EPIPE: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 589 | return; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 590 | default: |
| 591 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 592 | goto resubmit; |
| 593 | } |
| 594 | |
| 595 | buffer = urb->transfer_buffer; |
| 596 | length = urb->actual_length; |
| 597 | usb = urb->context; |
| 598 | rx = &usb->rx; |
| 599 | |
| 600 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { |
| 601 | /* If there is an old first fragment, we don't care. */ |
| 602 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); |
| 603 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); |
| 604 | spin_lock(&rx->lock); |
| 605 | memcpy(rx->fragment, buffer, length); |
| 606 | rx->fragment_length = length; |
| 607 | spin_unlock(&rx->lock); |
| 608 | goto resubmit; |
| 609 | } |
| 610 | |
| 611 | spin_lock(&rx->lock); |
| 612 | if (rx->fragment_length > 0) { |
| 613 | /* We are on a second fragment, we believe */ |
| 614 | ZD_ASSERT(length + rx->fragment_length <= |
| 615 | ARRAY_SIZE(rx->fragment)); |
| 616 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); |
| 617 | memcpy(rx->fragment+rx->fragment_length, buffer, length); |
| 618 | handle_rx_packet(usb, rx->fragment, |
| 619 | rx->fragment_length + length); |
| 620 | rx->fragment_length = 0; |
| 621 | spin_unlock(&rx->lock); |
| 622 | } else { |
| 623 | spin_unlock(&rx->lock); |
| 624 | handle_rx_packet(usb, buffer, length); |
| 625 | } |
| 626 | |
| 627 | resubmit: |
| 628 | usb_submit_urb(urb, GFP_ATOMIC); |
| 629 | } |
| 630 | |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 631 | static struct urb *alloc_urb(struct zd_usb *usb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 632 | { |
| 633 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
| 634 | struct urb *urb; |
| 635 | void *buffer; |
| 636 | |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 637 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 638 | if (!urb) |
| 639 | return NULL; |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 640 | buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 641 | &urb->transfer_dma); |
| 642 | if (!buffer) { |
| 643 | usb_free_urb(urb); |
| 644 | return NULL; |
| 645 | } |
| 646 | |
| 647 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), |
| 648 | buffer, USB_MAX_RX_SIZE, |
| 649 | rx_urb_complete, usb); |
| 650 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 651 | |
| 652 | return urb; |
| 653 | } |
| 654 | |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 655 | static void free_urb(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 656 | { |
| 657 | if (!urb) |
| 658 | return; |
| 659 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
| 660 | urb->transfer_buffer, urb->transfer_dma); |
| 661 | usb_free_urb(urb); |
| 662 | } |
| 663 | |
| 664 | int zd_usb_enable_rx(struct zd_usb *usb) |
| 665 | { |
| 666 | int i, r; |
| 667 | struct zd_usb_rx *rx = &usb->rx; |
| 668 | struct urb **urbs; |
| 669 | |
| 670 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 671 | |
| 672 | r = -ENOMEM; |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 673 | urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 674 | if (!urbs) |
| 675 | goto error; |
| 676 | for (i = 0; i < URBS_COUNT; i++) { |
| 677 | urbs[i] = alloc_urb(usb); |
| 678 | if (!urbs[i]) |
| 679 | goto error; |
| 680 | } |
| 681 | |
| 682 | ZD_ASSERT(!irqs_disabled()); |
| 683 | spin_lock_irq(&rx->lock); |
| 684 | if (rx->urbs) { |
| 685 | spin_unlock_irq(&rx->lock); |
| 686 | r = 0; |
| 687 | goto error; |
| 688 | } |
| 689 | rx->urbs = urbs; |
| 690 | rx->urbs_count = URBS_COUNT; |
| 691 | spin_unlock_irq(&rx->lock); |
| 692 | |
| 693 | for (i = 0; i < URBS_COUNT; i++) { |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 694 | r = usb_submit_urb(urbs[i], GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 695 | if (r) |
| 696 | goto error_submit; |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | error_submit: |
| 701 | for (i = 0; i < URBS_COUNT; i++) { |
| 702 | usb_kill_urb(urbs[i]); |
| 703 | } |
| 704 | spin_lock_irq(&rx->lock); |
| 705 | rx->urbs = NULL; |
| 706 | rx->urbs_count = 0; |
| 707 | spin_unlock_irq(&rx->lock); |
| 708 | error: |
| 709 | if (urbs) { |
| 710 | for (i = 0; i < URBS_COUNT; i++) |
| 711 | free_urb(urbs[i]); |
| 712 | } |
| 713 | return r; |
| 714 | } |
| 715 | |
| 716 | void zd_usb_disable_rx(struct zd_usb *usb) |
| 717 | { |
| 718 | int i; |
| 719 | unsigned long flags; |
| 720 | struct urb **urbs; |
| 721 | unsigned int count; |
| 722 | struct zd_usb_rx *rx = &usb->rx; |
| 723 | |
| 724 | spin_lock_irqsave(&rx->lock, flags); |
| 725 | urbs = rx->urbs; |
| 726 | count = rx->urbs_count; |
| 727 | spin_unlock_irqrestore(&rx->lock, flags); |
| 728 | if (!urbs) |
| 729 | return; |
| 730 | |
| 731 | for (i = 0; i < count; i++) { |
| 732 | usb_kill_urb(urbs[i]); |
| 733 | free_urb(urbs[i]); |
| 734 | } |
| 735 | kfree(urbs); |
| 736 | |
| 737 | spin_lock_irqsave(&rx->lock, flags); |
| 738 | rx->urbs = NULL; |
| 739 | rx->urbs_count = 0; |
| 740 | spin_unlock_irqrestore(&rx->lock, flags); |
| 741 | } |
| 742 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 743 | static void tx_urb_complete(struct urb *urb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 744 | { |
| 745 | int r; |
| 746 | |
| 747 | switch (urb->status) { |
| 748 | case 0: |
| 749 | break; |
| 750 | case -ESHUTDOWN: |
| 751 | case -EINVAL: |
| 752 | case -ENODEV: |
| 753 | case -ENOENT: |
| 754 | case -ECONNRESET: |
Daniel Drake | b312d79 | 2006-07-05 15:57:39 +0100 | [diff] [blame] | 755 | case -EPIPE: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 756 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 757 | break; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 758 | default: |
| 759 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 760 | goto resubmit; |
| 761 | } |
| 762 | free_urb: |
| 763 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
| 764 | urb->transfer_buffer, urb->transfer_dma); |
| 765 | usb_free_urb(urb); |
| 766 | return; |
| 767 | resubmit: |
| 768 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 769 | if (r) { |
| 770 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); |
| 771 | goto free_urb; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* Puts the frame on the USB endpoint. It doesn't wait for |
| 776 | * completion. The frame must contain the control set. |
| 777 | */ |
| 778 | int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length) |
| 779 | { |
| 780 | int r; |
| 781 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
| 782 | struct urb *urb; |
| 783 | void *buffer; |
| 784 | |
| 785 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 786 | if (!urb) { |
| 787 | r = -ENOMEM; |
| 788 | goto out; |
| 789 | } |
| 790 | |
| 791 | buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC, |
| 792 | &urb->transfer_dma); |
| 793 | if (!buffer) { |
| 794 | r = -ENOMEM; |
| 795 | goto error_free_urb; |
| 796 | } |
| 797 | memcpy(buffer, frame, length); |
| 798 | |
| 799 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
| 800 | buffer, length, tx_urb_complete, NULL); |
| 801 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 802 | |
| 803 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 804 | if (r) |
| 805 | goto error; |
| 806 | return 0; |
| 807 | error: |
| 808 | usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer, |
| 809 | urb->transfer_dma); |
| 810 | error_free_urb: |
| 811 | usb_free_urb(urb); |
| 812 | out: |
| 813 | return r; |
| 814 | } |
| 815 | |
| 816 | static inline void init_usb_interrupt(struct zd_usb *usb) |
| 817 | { |
| 818 | struct zd_usb_interrupt *intr = &usb->intr; |
| 819 | |
| 820 | spin_lock_init(&intr->lock); |
| 821 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); |
| 822 | init_completion(&intr->read_regs.completion); |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 823 | intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static inline void init_usb_rx(struct zd_usb *usb) |
| 827 | { |
| 828 | struct zd_usb_rx *rx = &usb->rx; |
| 829 | spin_lock_init(&rx->lock); |
| 830 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { |
| 831 | rx->usb_packet_size = 512; |
| 832 | } else { |
| 833 | rx->usb_packet_size = 64; |
| 834 | } |
| 835 | ZD_ASSERT(rx->fragment_length == 0); |
| 836 | } |
| 837 | |
| 838 | static inline void init_usb_tx(struct zd_usb *usb) |
| 839 | { |
| 840 | /* FIXME: at this point we will allocate a fixed number of urb's for |
| 841 | * use in a cyclic scheme */ |
| 842 | } |
| 843 | |
| 844 | void zd_usb_init(struct zd_usb *usb, struct net_device *netdev, |
| 845 | struct usb_interface *intf) |
| 846 | { |
| 847 | memset(usb, 0, sizeof(*usb)); |
| 848 | usb->intf = usb_get_intf(intf); |
| 849 | usb_set_intfdata(usb->intf, netdev); |
| 850 | init_usb_interrupt(usb); |
| 851 | init_usb_tx(usb); |
| 852 | init_usb_rx(usb); |
| 853 | } |
| 854 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 855 | void zd_usb_clear(struct zd_usb *usb) |
| 856 | { |
| 857 | usb_set_intfdata(usb->intf, NULL); |
| 858 | usb_put_intf(usb->intf); |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 859 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 860 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
| 861 | } |
| 862 | |
| 863 | static const char *speed(enum usb_device_speed speed) |
| 864 | { |
| 865 | switch (speed) { |
| 866 | case USB_SPEED_LOW: |
| 867 | return "low"; |
| 868 | case USB_SPEED_FULL: |
| 869 | return "full"; |
| 870 | case USB_SPEED_HIGH: |
| 871 | return "high"; |
| 872 | default: |
| 873 | return "unknown speed"; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) |
| 878 | { |
| 879 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", |
| 880 | le16_to_cpu(udev->descriptor.idVendor), |
| 881 | le16_to_cpu(udev->descriptor.idProduct), |
| 882 | get_bcdDevice(udev), |
| 883 | speed(udev->speed)); |
| 884 | } |
| 885 | |
| 886 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) |
| 887 | { |
| 888 | struct usb_device *udev = interface_to_usbdev(usb->intf); |
| 889 | return scnprint_id(udev, buffer, size); |
| 890 | } |
| 891 | |
| 892 | #ifdef DEBUG |
| 893 | static void print_id(struct usb_device *udev) |
| 894 | { |
| 895 | char buffer[40]; |
| 896 | |
| 897 | scnprint_id(udev, buffer, sizeof(buffer)); |
| 898 | buffer[sizeof(buffer)-1] = 0; |
| 899 | dev_dbg_f(&udev->dev, "%s\n", buffer); |
| 900 | } |
| 901 | #else |
| 902 | #define print_id(udev) do { } while (0) |
| 903 | #endif |
| 904 | |
Daniel Drake | a1030e9 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 905 | static int eject_installer(struct usb_interface *intf) |
| 906 | { |
| 907 | struct usb_device *udev = interface_to_usbdev(intf); |
| 908 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; |
| 909 | struct usb_endpoint_descriptor *endpoint; |
| 910 | unsigned char *cmd; |
| 911 | u8 bulk_out_ep; |
| 912 | int r; |
| 913 | |
| 914 | /* Find bulk out endpoint */ |
| 915 | endpoint = &iface_desc->endpoint[1].desc; |
| 916 | if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT && |
| 917 | (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| 918 | USB_ENDPOINT_XFER_BULK) { |
| 919 | bulk_out_ep = endpoint->bEndpointAddress; |
| 920 | } else { |
| 921 | dev_err(&udev->dev, |
| 922 | "zd1211rw: Could not find bulk out endpoint\n"); |
| 923 | return -ENODEV; |
| 924 | } |
| 925 | |
| 926 | cmd = kzalloc(31, GFP_KERNEL); |
| 927 | if (cmd == NULL) |
| 928 | return -ENODEV; |
| 929 | |
| 930 | /* USB bulk command block */ |
| 931 | cmd[0] = 0x55; /* bulk command signature */ |
| 932 | cmd[1] = 0x53; /* bulk command signature */ |
| 933 | cmd[2] = 0x42; /* bulk command signature */ |
| 934 | cmd[3] = 0x43; /* bulk command signature */ |
| 935 | cmd[14] = 6; /* command length */ |
| 936 | |
| 937 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ |
| 938 | cmd[19] = 0x2; /* eject disc */ |
| 939 | |
| 940 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); |
| 941 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), |
| 942 | cmd, 31, NULL, 2000); |
| 943 | kfree(cmd); |
| 944 | if (r) |
| 945 | return r; |
| 946 | |
| 947 | /* At this point, the device disconnects and reconnects with the real |
| 948 | * ID numbers. */ |
| 949 | |
| 950 | usb_set_intfdata(intf, NULL); |
| 951 | return 0; |
| 952 | } |
| 953 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 954 | int zd_usb_init_hw(struct zd_usb *usb) |
| 955 | { |
| 956 | int r; |
| 957 | struct zd_mac *mac = zd_usb_to_mac(usb); |
| 958 | |
| 959 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 960 | |
| 961 | r = upload_firmware(usb); |
| 962 | if (r) { |
| 963 | dev_err(zd_usb_dev(usb), |
| 964 | "couldn't load firmware. Error number %d\n", r); |
| 965 | return r; |
| 966 | } |
| 967 | |
| 968 | r = usb_reset_configuration(zd_usb_to_usbdev(usb)); |
| 969 | if (r) { |
| 970 | dev_dbg_f(zd_usb_dev(usb), |
| 971 | "couldn't reset configuration. Error number %d\n", r); |
| 972 | return r; |
| 973 | } |
| 974 | |
| 975 | r = zd_mac_init_hw(mac); |
| 976 | if (r) { |
| 977 | dev_dbg_f(zd_usb_dev(usb), |
| 978 | "couldn't initialize mac. Error number %d\n", r); |
| 979 | return r; |
| 980 | } |
| 981 | |
| 982 | usb->initialized = 1; |
| 983 | return 0; |
| 984 | } |
| 985 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 986 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 987 | { |
| 988 | int r; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 989 | struct zd_usb *usb; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 990 | struct usb_device *udev = interface_to_usbdev(intf); |
| 991 | struct net_device *netdev = NULL; |
| 992 | |
| 993 | print_id(udev); |
| 994 | |
Daniel Drake | a1030e9 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 995 | if (id->driver_info & DEVICE_INSTALLER) |
| 996 | return eject_installer(intf); |
| 997 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 998 | switch (udev->speed) { |
| 999 | case USB_SPEED_LOW: |
| 1000 | case USB_SPEED_FULL: |
| 1001 | case USB_SPEED_HIGH: |
| 1002 | break; |
| 1003 | default: |
| 1004 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); |
| 1005 | r = -ENODEV; |
| 1006 | goto error; |
| 1007 | } |
| 1008 | |
Ulrich Kunitz | 6e3632f | 2007-01-29 00:59:28 +0000 | [diff] [blame] | 1009 | usb_reset_device(interface_to_usbdev(intf)); |
| 1010 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1011 | netdev = zd_netdev_alloc(intf); |
| 1012 | if (netdev == NULL) { |
| 1013 | r = -ENOMEM; |
| 1014 | goto error; |
| 1015 | } |
| 1016 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 1017 | usb = &zd_netdev_mac(netdev)->chip.usb; |
| 1018 | usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1019 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 1020 | r = zd_mac_preinit_hw(zd_netdev_mac(netdev)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1021 | if (r) { |
| 1022 | dev_dbg_f(&intf->dev, |
| 1023 | "couldn't initialize mac. Error number %d\n", r); |
| 1024 | goto error; |
| 1025 | } |
| 1026 | |
| 1027 | r = register_netdev(netdev); |
| 1028 | if (r) { |
| 1029 | dev_dbg_f(&intf->dev, |
| 1030 | "couldn't register netdev. Error number %d\n", r); |
| 1031 | goto error; |
| 1032 | } |
| 1033 | |
| 1034 | dev_dbg_f(&intf->dev, "successful\n"); |
| 1035 | dev_info(&intf->dev,"%s\n", netdev->name); |
| 1036 | return 0; |
| 1037 | error: |
| 1038 | usb_reset_device(interface_to_usbdev(intf)); |
| 1039 | zd_netdev_free(netdev); |
| 1040 | return r; |
| 1041 | } |
| 1042 | |
| 1043 | static void disconnect(struct usb_interface *intf) |
| 1044 | { |
| 1045 | struct net_device *netdev = zd_intf_to_netdev(intf); |
| 1046 | struct zd_mac *mac = zd_netdev_mac(netdev); |
| 1047 | struct zd_usb *usb = &mac->chip.usb; |
| 1048 | |
Daniel Drake | a1030e9 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 1049 | /* Either something really bad happened, or we're just dealing with |
| 1050 | * a DEVICE_INSTALLER. */ |
| 1051 | if (netdev == NULL) |
| 1052 | return; |
| 1053 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1054 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
| 1055 | |
| 1056 | zd_netdev_disconnect(netdev); |
| 1057 | |
| 1058 | /* Just in case something has gone wrong! */ |
| 1059 | zd_usb_disable_rx(usb); |
| 1060 | zd_usb_disable_int(usb); |
| 1061 | |
| 1062 | /* If the disconnect has been caused by a removal of the |
| 1063 | * driver module, the reset allows reloading of the driver. If the |
| 1064 | * reset will not be executed here, the upload of the firmware in the |
| 1065 | * probe function caused by the reloading of the driver will fail. |
| 1066 | */ |
| 1067 | usb_reset_device(interface_to_usbdev(intf)); |
| 1068 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1069 | zd_netdev_free(netdev); |
| 1070 | dev_dbg(&intf->dev, "disconnected\n"); |
| 1071 | } |
| 1072 | |
| 1073 | static struct usb_driver driver = { |
| 1074 | .name = "zd1211rw", |
| 1075 | .id_table = usb_ids, |
| 1076 | .probe = probe, |
| 1077 | .disconnect = disconnect, |
| 1078 | }; |
| 1079 | |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1080 | struct workqueue_struct *zd_workqueue; |
| 1081 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1082 | static int __init usb_init(void) |
| 1083 | { |
| 1084 | int r; |
| 1085 | |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1086 | pr_debug("%s usb_init()\n", driver.name); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1087 | |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1088 | zd_workqueue = create_singlethread_workqueue(driver.name); |
| 1089 | if (zd_workqueue == NULL) { |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1090 | printk(KERN_ERR "%s couldn't create workqueue\n", driver.name); |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1091 | return -ENOMEM; |
| 1092 | } |
| 1093 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1094 | r = usb_register(&driver); |
| 1095 | if (r) { |
Maxime Austruy | 192b775 | 2007-01-29 00:59:51 +0000 | [diff] [blame] | 1096 | destroy_workqueue(zd_workqueue); |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1097 | printk(KERN_ERR "%s usb_register() failed. Error number %d\n", |
| 1098 | driver.name, r); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1099 | return r; |
| 1100 | } |
| 1101 | |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1102 | pr_debug("%s initialized\n", driver.name); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static void __exit usb_exit(void) |
| 1107 | { |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 1108 | pr_debug("%s usb_exit()\n", driver.name); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1109 | usb_deregister(&driver); |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 1110 | destroy_workqueue(zd_workqueue); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | module_init(usb_init); |
| 1114 | module_exit(usb_exit); |
| 1115 | |
| 1116 | static int usb_int_regs_length(unsigned int count) |
| 1117 | { |
| 1118 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); |
| 1119 | } |
| 1120 | |
| 1121 | static void prepare_read_regs_int(struct zd_usb *usb) |
| 1122 | { |
| 1123 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1124 | |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1125 | spin_lock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1126 | intr->read_regs_enabled = 1; |
| 1127 | INIT_COMPLETION(intr->read_regs.completion); |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1128 | spin_unlock_irq(&intr->lock); |
| 1129 | } |
| 1130 | |
| 1131 | static void disable_read_regs_int(struct zd_usb *usb) |
| 1132 | { |
| 1133 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1134 | |
| 1135 | spin_lock_irq(&intr->lock); |
| 1136 | intr->read_regs_enabled = 0; |
| 1137 | spin_unlock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static int get_results(struct zd_usb *usb, u16 *values, |
| 1141 | struct usb_req_read_regs *req, unsigned int count) |
| 1142 | { |
| 1143 | int r; |
| 1144 | int i; |
| 1145 | struct zd_usb_interrupt *intr = &usb->intr; |
| 1146 | struct read_regs_int *rr = &intr->read_regs; |
| 1147 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; |
| 1148 | |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1149 | spin_lock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1150 | |
| 1151 | r = -EIO; |
| 1152 | /* The created block size seems to be larger than expected. |
| 1153 | * However results appear to be correct. |
| 1154 | */ |
| 1155 | if (rr->length < usb_int_regs_length(count)) { |
| 1156 | dev_dbg_f(zd_usb_dev(usb), |
| 1157 | "error: actual length %d less than expected %d\n", |
| 1158 | rr->length, usb_int_regs_length(count)); |
| 1159 | goto error_unlock; |
| 1160 | } |
| 1161 | if (rr->length > sizeof(rr->buffer)) { |
| 1162 | dev_dbg_f(zd_usb_dev(usb), |
| 1163 | "error: actual length %d exceeds buffer size %zu\n", |
| 1164 | rr->length, sizeof(rr->buffer)); |
| 1165 | goto error_unlock; |
| 1166 | } |
| 1167 | |
| 1168 | for (i = 0; i < count; i++) { |
| 1169 | struct reg_data *rd = ®s->regs[i]; |
| 1170 | if (rd->addr != req->addr[i]) { |
| 1171 | dev_dbg_f(zd_usb_dev(usb), |
| 1172 | "rd[%d] addr %#06hx expected %#06hx\n", i, |
| 1173 | le16_to_cpu(rd->addr), |
| 1174 | le16_to_cpu(req->addr[i])); |
| 1175 | goto error_unlock; |
| 1176 | } |
| 1177 | values[i] = le16_to_cpu(rd->value); |
| 1178 | } |
| 1179 | |
| 1180 | r = 0; |
| 1181 | error_unlock: |
Ulrich Kunitz | a68077d | 2006-11-22 00:06:06 +0000 | [diff] [blame] | 1182 | spin_unlock_irq(&intr->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1183 | return r; |
| 1184 | } |
| 1185 | |
| 1186 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, |
| 1187 | const zd_addr_t *addresses, unsigned int count) |
| 1188 | { |
| 1189 | int r; |
| 1190 | int i, req_len, actual_req_len; |
| 1191 | struct usb_device *udev; |
| 1192 | struct usb_req_read_regs *req = NULL; |
| 1193 | unsigned long timeout; |
| 1194 | |
| 1195 | if (count < 1) { |
| 1196 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); |
| 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | if (count > USB_MAX_IOREAD16_COUNT) { |
| 1200 | dev_dbg_f(zd_usb_dev(usb), |
| 1201 | "error: count %u exceeds possible max %u\n", |
| 1202 | count, USB_MAX_IOREAD16_COUNT); |
| 1203 | return -EINVAL; |
| 1204 | } |
| 1205 | if (in_atomic()) { |
| 1206 | dev_dbg_f(zd_usb_dev(usb), |
| 1207 | "error: io in atomic context not supported\n"); |
| 1208 | return -EWOULDBLOCK; |
| 1209 | } |
| 1210 | if (!usb_int_enabled(usb)) { |
| 1211 | dev_dbg_f(zd_usb_dev(usb), |
| 1212 | "error: usb interrupt not enabled\n"); |
| 1213 | return -EWOULDBLOCK; |
| 1214 | } |
| 1215 | |
| 1216 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 1217 | req = kmalloc(req_len, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1218 | if (!req) |
| 1219 | return -ENOMEM; |
| 1220 | req->id = cpu_to_le16(USB_REQ_READ_REGS); |
| 1221 | for (i = 0; i < count; i++) |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 1222 | req->addr[i] = cpu_to_le16((u16)addresses[i]); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1223 | |
| 1224 | udev = zd_usb_to_usbdev(usb); |
| 1225 | prepare_read_regs_int(usb); |
| 1226 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1227 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1228 | if (r) { |
| 1229 | dev_dbg_f(zd_usb_dev(usb), |
| 1230 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1231 | goto error; |
| 1232 | } |
| 1233 | if (req_len != actual_req_len) { |
| 1234 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n" |
| 1235 | " req_len %d != actual_req_len %d\n", |
| 1236 | req_len, actual_req_len); |
| 1237 | r = -EIO; |
| 1238 | goto error; |
| 1239 | } |
| 1240 | |
| 1241 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, |
| 1242 | msecs_to_jiffies(1000)); |
| 1243 | if (!timeout) { |
| 1244 | disable_read_regs_int(usb); |
| 1245 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); |
| 1246 | r = -ETIMEDOUT; |
| 1247 | goto error; |
| 1248 | } |
| 1249 | |
| 1250 | r = get_results(usb, values, req, count); |
| 1251 | error: |
| 1252 | kfree(req); |
| 1253 | return r; |
| 1254 | } |
| 1255 | |
| 1256 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
| 1257 | unsigned int count) |
| 1258 | { |
| 1259 | int r; |
| 1260 | struct usb_device *udev; |
| 1261 | struct usb_req_write_regs *req = NULL; |
| 1262 | int i, req_len, actual_req_len; |
| 1263 | |
| 1264 | if (count == 0) |
| 1265 | return 0; |
| 1266 | if (count > USB_MAX_IOWRITE16_COUNT) { |
| 1267 | dev_dbg_f(zd_usb_dev(usb), |
| 1268 | "error: count %u exceeds possible max %u\n", |
| 1269 | count, USB_MAX_IOWRITE16_COUNT); |
| 1270 | return -EINVAL; |
| 1271 | } |
| 1272 | if (in_atomic()) { |
| 1273 | dev_dbg_f(zd_usb_dev(usb), |
| 1274 | "error: io in atomic context not supported\n"); |
| 1275 | return -EWOULDBLOCK; |
| 1276 | } |
| 1277 | |
| 1278 | req_len = sizeof(struct usb_req_write_regs) + |
| 1279 | count * sizeof(struct reg_data); |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 1280 | req = kmalloc(req_len, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1281 | if (!req) |
| 1282 | return -ENOMEM; |
| 1283 | |
| 1284 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); |
| 1285 | for (i = 0; i < count; i++) { |
| 1286 | struct reg_data *rw = &req->reg_writes[i]; |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 1287 | rw->addr = cpu_to_le16((u16)ioreqs[i].addr); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1288 | rw->value = cpu_to_le16(ioreqs[i].value); |
| 1289 | } |
| 1290 | |
| 1291 | udev = zd_usb_to_usbdev(usb); |
| 1292 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1293 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1294 | if (r) { |
| 1295 | dev_dbg_f(zd_usb_dev(usb), |
| 1296 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1297 | goto error; |
| 1298 | } |
| 1299 | if (req_len != actual_req_len) { |
| 1300 | dev_dbg_f(zd_usb_dev(usb), |
| 1301 | "error in usb_bulk_msg()" |
| 1302 | " req_len %d != actual_req_len %d\n", |
| 1303 | req_len, actual_req_len); |
| 1304 | r = -EIO; |
| 1305 | goto error; |
| 1306 | } |
| 1307 | |
| 1308 | /* FALL-THROUGH with r == 0 */ |
| 1309 | error: |
| 1310 | kfree(req); |
| 1311 | return r; |
| 1312 | } |
| 1313 | |
| 1314 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) |
| 1315 | { |
| 1316 | int r; |
| 1317 | struct usb_device *udev; |
| 1318 | struct usb_req_rfwrite *req = NULL; |
| 1319 | int i, req_len, actual_req_len; |
| 1320 | u16 bit_value_template; |
| 1321 | |
| 1322 | if (in_atomic()) { |
| 1323 | dev_dbg_f(zd_usb_dev(usb), |
| 1324 | "error: io in atomic context not supported\n"); |
| 1325 | return -EWOULDBLOCK; |
| 1326 | } |
| 1327 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { |
| 1328 | dev_dbg_f(zd_usb_dev(usb), |
| 1329 | "error: bits %d are smaller than" |
| 1330 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", |
| 1331 | bits, USB_MIN_RFWRITE_BIT_COUNT); |
| 1332 | return -EINVAL; |
| 1333 | } |
| 1334 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { |
| 1335 | dev_dbg_f(zd_usb_dev(usb), |
| 1336 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", |
| 1337 | bits, USB_MAX_RFWRITE_BIT_COUNT); |
| 1338 | return -EINVAL; |
| 1339 | } |
| 1340 | #ifdef DEBUG |
| 1341 | if (value & (~0UL << bits)) { |
| 1342 | dev_dbg_f(zd_usb_dev(usb), |
| 1343 | "error: value %#09x has bits >= %d set\n", |
| 1344 | value, bits); |
| 1345 | return -EINVAL; |
| 1346 | } |
| 1347 | #endif /* DEBUG */ |
| 1348 | |
| 1349 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); |
| 1350 | |
| 1351 | r = zd_usb_ioread16(usb, &bit_value_template, CR203); |
| 1352 | if (r) { |
| 1353 | dev_dbg_f(zd_usb_dev(usb), |
| 1354 | "error %d: Couldn't read CR203\n", r); |
| 1355 | goto out; |
| 1356 | } |
| 1357 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); |
| 1358 | |
| 1359 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); |
Ulrich Kunitz | 35c3404 | 2007-02-18 20:28:23 +0000 | [diff] [blame] | 1360 | req = kmalloc(req_len, GFP_KERNEL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 1361 | if (!req) |
| 1362 | return -ENOMEM; |
| 1363 | |
| 1364 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); |
| 1365 | /* 1: 3683a, but not used in ZYDAS driver */ |
| 1366 | req->value = cpu_to_le16(2); |
| 1367 | req->bits = cpu_to_le16(bits); |
| 1368 | |
| 1369 | for (i = 0; i < bits; i++) { |
| 1370 | u16 bv = bit_value_template; |
| 1371 | if (value & (1 << (bits-1-i))) |
| 1372 | bv |= RF_DATA; |
| 1373 | req->bit_values[i] = cpu_to_le16(bv); |
| 1374 | } |
| 1375 | |
| 1376 | udev = zd_usb_to_usbdev(usb); |
| 1377 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), |
| 1378 | req, req_len, &actual_req_len, 1000 /* ms */); |
| 1379 | if (r) { |
| 1380 | dev_dbg_f(zd_usb_dev(usb), |
| 1381 | "error in usb_bulk_msg(). Error number %d\n", r); |
| 1382 | goto out; |
| 1383 | } |
| 1384 | if (req_len != actual_req_len) { |
| 1385 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()" |
| 1386 | " req_len %d != actual_req_len %d\n", |
| 1387 | req_len, actual_req_len); |
| 1388 | r = -EIO; |
| 1389 | goto out; |
| 1390 | } |
| 1391 | |
| 1392 | /* FALL-THROUGH with r == 0 */ |
| 1393 | out: |
| 1394 | kfree(req); |
| 1395 | return r; |
| 1396 | } |