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