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