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