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