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