blob: 785bdbe38f2a0cb6f67d6ad56a3c0ad2d5980490 [file] [log] [blame]
Daniel Drake66bb42f2007-11-19 16:20:12 +00001/* 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 Drakee85d0912006-06-02 17:11:32 +01006 *
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 Draked066c212006-08-12 17:59:59 +010022#include <linux/kernel.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010023#include <linux/init.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010024#include <linux/firmware.h>
25#include <linux/device.h>
26#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010028#include <linux/skbuff.h>
29#include <linux/usb.h>
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +010030#include <linux/workqueue.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040031#include <linux/module.h>
Daniel Drake459c51a2007-11-19 15:00:29 +000032#include <net/mac80211.h>
Al Viroc2487252007-07-15 21:00:21 +010033#include <asm/unaligned.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010034
35#include "zd_def.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010036#include "zd_mac.h"
37#include "zd_usb.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010038
39static struct usb_device_id usb_ids[] = {
40 /* ZD1211 */
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010041 { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010048 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung14990c62009-02-08 02:13:56 +000049 { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010050 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010053 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010054 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010055 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
Ulrich Kunitzababda02007-09-01 22:40:32 +010056 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010057 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
58 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
maximilian attems8cecc902010-11-02 23:10:12 +010059 { USB_DEVICE(0x14ea, 0xab10), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010060 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
Daniel Drake269fca02007-11-19 15:29:24 +000061 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010062 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
63 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
Tõnu Samuel9011cd22011-03-12 11:29:25 +020064 { USB_DEVICE(0x157e, 0x3207), .driver_info = DEVICE_ZD1211 },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010065 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
66 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010067 /* ZD1211B */
Ulrich Kunitz019a6752007-05-01 17:13:51 +010068 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
Hin-Tak Leungad580db2010-01-18 01:24:11 +000069 { USB_DEVICE(0x0409, 0x0248), .driver_info = DEVICE_ZD1211B },
Daniel Drake93f510b2007-07-01 18:22:21 +010070 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010071 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
Daniel Drake61ef6062007-10-07 16:24:26 +010072 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010073 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
74 { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B },
75 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
76 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
77 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
78 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
79 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
80 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
Pascal Terjanb4b223c2009-06-18 17:54:03 +020081 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010082 { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B },
83 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
Hin-Tak Leung8f75e072009-07-13 23:20:37 +010084 { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B },
Hin-Tak Leung3bfbe802009-06-18 03:53:26 +010085 { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B },
86 { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
87 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
88 { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B },
89 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
90 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
91 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
92 { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B },
93 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
94 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
95 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
96 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
97 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
maximilian attems8cecc902010-11-02 23:10:12 +010098 { USB_DEVICE(0x2019, 0xed01), .driver_info = DEVICE_ZD1211B },
Daniel Drakea1030e92006-08-13 12:15:29 +010099 /* "Driverless" devices that need ejecting */
100 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +0100101 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100102 {}
103};
104
105MODULE_LICENSE("GPL");
106MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
107MODULE_AUTHOR("Ulrich Kunitz");
108MODULE_AUTHOR("Daniel Drake");
109MODULE_VERSION("1.0");
110MODULE_DEVICE_TABLE(usb, usb_ids);
111
112#define FW_ZD1211_PREFIX "zd1211/zd1211_"
113#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
114
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300115static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
116 unsigned int count);
117
Daniel Drakee85d0912006-06-02 17:11:32 +0100118/* USB device initialization */
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800119static void int_urb_complete(struct urb *urb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100120
121static int request_fw_file(
122 const struct firmware **fw, const char *name, struct device *device)
123{
124 int r;
125
126 dev_dbg_f(device, "fw name %s\n", name);
127
128 r = request_firmware(fw, name, device);
129 if (r)
130 dev_err(device,
131 "Could not load firmware file %s. Error number %d\n",
132 name, r);
133 return r;
134}
135
136static inline u16 get_bcdDevice(const struct usb_device *udev)
137{
138 return le16_to_cpu(udev->descriptor.bcdDevice);
139}
140
141enum upload_code_flags {
142 REBOOT = 1,
143};
144
145/* Ensures that MAX_TRANSFER_SIZE is even. */
146#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
147
148static int upload_code(struct usb_device *udev,
149 const u8 *data, size_t size, u16 code_offset, int flags)
150{
151 u8 *p;
152 int r;
153
154 /* USB request blocks need "kmalloced" buffers.
155 */
156 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
157 if (!p) {
158 dev_err(&udev->dev, "out of memory\n");
159 r = -ENOMEM;
160 goto error;
161 }
162
163 size &= ~1;
164 while (size > 0) {
165 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
166 size : MAX_TRANSFER_SIZE;
167
168 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
169
170 memcpy(p, data, transfer_size);
171 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
172 USB_REQ_FIRMWARE_DOWNLOAD,
173 USB_DIR_OUT | USB_TYPE_VENDOR,
174 code_offset, 0, p, transfer_size, 1000 /* ms */);
175 if (r < 0) {
176 dev_err(&udev->dev,
177 "USB control request for firmware upload"
178 " failed. Error number %d\n", r);
179 goto error;
180 }
181 transfer_size = r & ~1;
182
183 size -= transfer_size;
184 data += transfer_size;
185 code_offset += transfer_size/sizeof(u16);
186 }
187
188 if (flags & REBOOT) {
189 u8 ret;
190
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900191 /* Use "DMA-aware" buffer. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100192 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
193 USB_REQ_FIRMWARE_CONFIRM,
194 USB_DIR_IN | USB_TYPE_VENDOR,
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900195 0, 0, p, sizeof(ret), 5000 /* ms */);
Daniel Drakee85d0912006-06-02 17:11:32 +0100196 if (r != sizeof(ret)) {
197 dev_err(&udev->dev,
198 "control request firmeware confirmation failed."
199 " Return value %d\n", r);
200 if (r >= 0)
201 r = -ENODEV;
202 goto error;
203 }
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900204 ret = p[0];
Daniel Drakee85d0912006-06-02 17:11:32 +0100205 if (ret & 0x80) {
206 dev_err(&udev->dev,
207 "Internal error while downloading."
208 " Firmware confirm return value %#04x\n",
209 (unsigned int)ret);
210 r = -ENODEV;
211 goto error;
212 }
213 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
214 (unsigned int)ret);
215 }
216
217 r = 0;
218error:
219 kfree(p);
220 return r;
221}
222
223static u16 get_word(const void *data, u16 offset)
224{
225 const __le16 *p = data;
226 return le16_to_cpu(p[offset]);
227}
228
Daniel Drake74553ae2007-07-01 18:22:32 +0100229static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
Daniel Drakee85d0912006-06-02 17:11:32 +0100230 const char* postfix)
231{
232 scnprintf(buffer, size, "%s%s",
Daniel Drake74553ae2007-07-01 18:22:32 +0100233 usb->is_zd1211b ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100234 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
235 postfix);
236 return buffer;
237}
238
Daniel Drake74553ae2007-07-01 18:22:32 +0100239static int handle_version_mismatch(struct zd_usb *usb,
Daniel Draked066c212006-08-12 17:59:59 +0100240 const struct firmware *ub_fw)
241{
Daniel Drake74553ae2007-07-01 18:22:32 +0100242 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Draked066c212006-08-12 17:59:59 +0100243 const struct firmware *ur_fw = NULL;
244 int offset;
245 int r = 0;
246 char fw_name[128];
247
248 r = request_fw_file(&ur_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100249 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
Daniel Draked066c212006-08-12 17:59:59 +0100250 &udev->dev);
251 if (r)
252 goto error;
253
Daniel Drakeee302762006-12-12 01:25:52 +0000254 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100255 if (r)
256 goto error;
257
Daniel Drakeee302762006-12-12 01:25:52 +0000258 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
Daniel Draked066c212006-08-12 17:59:59 +0100259 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
Daniel Drakeee302762006-12-12 01:25:52 +0000260 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100261
262 /* At this point, the vendor driver downloads the whole firmware
263 * image, hacks around with version IDs, and uploads it again,
264 * completely overwriting the boot code. We do not do this here as
265 * it is not required on any tested devices, and it is suspected to
266 * cause problems. */
267error:
268 release_firmware(ur_fw);
269 return r;
270}
271
Daniel Drake74553ae2007-07-01 18:22:32 +0100272static int upload_firmware(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100273{
274 int r;
275 u16 fw_bcdDevice;
276 u16 bcdDevice;
Daniel Drake74553ae2007-07-01 18:22:32 +0100277 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100278 const struct firmware *ub_fw = NULL;
279 const struct firmware *uph_fw = NULL;
280 char fw_name[128];
281
282 bcdDevice = get_bcdDevice(udev);
283
284 r = request_fw_file(&ub_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100285 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100286 &udev->dev);
287 if (r)
288 goto error;
289
Daniel Drakeee302762006-12-12 01:25:52 +0000290 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
Daniel Drakee85d0912006-06-02 17:11:32 +0100291
Daniel Drakee85d0912006-06-02 17:11:32 +0100292 if (fw_bcdDevice != bcdDevice) {
293 dev_info(&udev->dev,
Daniel Draked066c212006-08-12 17:59:59 +0100294 "firmware version %#06x and device bootcode version "
295 "%#06x differ\n", fw_bcdDevice, bcdDevice);
296 if (bcdDevice <= 0x4313)
297 dev_warn(&udev->dev, "device has old bootcode, please "
298 "report success or failure\n");
299
Daniel Drake74553ae2007-07-01 18:22:32 +0100300 r = handle_version_mismatch(usb, ub_fw);
Daniel Draked066c212006-08-12 17:59:59 +0100301 if (r)
302 goto error;
Daniel Drakee85d0912006-06-02 17:11:32 +0100303 } else {
304 dev_dbg_f(&udev->dev,
305 "firmware device id %#06x is equal to the "
306 "actual device id\n", fw_bcdDevice);
307 }
308
309
310 r = request_fw_file(&uph_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100311 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100312 &udev->dev);
313 if (r)
314 goto error;
315
Daniel Drakeee302762006-12-12 01:25:52 +0000316 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100317 if (r) {
318 dev_err(&udev->dev,
319 "Could not upload firmware code uph. Error number %d\n",
320 r);
321 }
322
323 /* FALL-THROUGH */
324error:
325 release_firmware(ub_fw);
326 release_firmware(uph_fw);
327 return r;
328}
329
Ben Hutchings3e8b4d02009-11-07 22:03:22 +0000330MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ur");
331MODULE_FIRMWARE(FW_ZD1211_PREFIX "ur");
332MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ub");
333MODULE_FIRMWARE(FW_ZD1211_PREFIX "ub");
334MODULE_FIRMWARE(FW_ZD1211B_PREFIX "uphr");
335MODULE_FIRMWARE(FW_ZD1211_PREFIX "uphr");
336
Daniel Drake74553ae2007-07-01 18:22:32 +0100337/* Read data from device address space using "firmware interface" which does
338 * not require firmware to be loaded. */
339int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
340{
341 int r;
342 struct usb_device *udev = zd_usb_to_usbdev(usb);
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900343 u8 *buf;
Daniel Drake74553ae2007-07-01 18:22:32 +0100344
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900345 /* Use "DMA-aware" buffer. */
346 buf = kmalloc(len, GFP_KERNEL);
347 if (!buf)
348 return -ENOMEM;
Daniel Drake74553ae2007-07-01 18:22:32 +0100349 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
350 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900351 buf, len, 5000);
Daniel Drake74553ae2007-07-01 18:22:32 +0100352 if (r < 0) {
353 dev_err(&udev->dev,
354 "read over firmware interface failed: %d\n", r);
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900355 goto exit;
Daniel Drake74553ae2007-07-01 18:22:32 +0100356 } else if (r != len) {
357 dev_err(&udev->dev,
358 "incomplete read over firmware interface: %d/%d\n",
359 r, len);
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900360 r = -EIO;
361 goto exit;
Daniel Drake74553ae2007-07-01 18:22:32 +0100362 }
Atsushi Nemotoa8c4ea72008-05-16 17:27:05 +0900363 r = 0;
364 memcpy(data, buf, len);
365exit:
366 kfree(buf);
367 return r;
Daniel Drake74553ae2007-07-01 18:22:32 +0100368}
369
Daniel Drakee85d0912006-06-02 17:11:32 +0100370#define urb_dev(urb) (&(urb)->dev->dev)
371
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300372static inline void handle_regs_int_override(struct urb *urb)
373{
374 struct zd_usb *usb = urb->context;
375 struct zd_usb_interrupt *intr = &usb->intr;
376
377 spin_lock(&intr->lock);
378 if (atomic_read(&intr->read_regs_enabled)) {
379 atomic_set(&intr->read_regs_enabled, 0);
380 intr->read_regs_int_overridden = 1;
381 complete(&intr->read_regs.completion);
382 }
383 spin_unlock(&intr->lock);
384}
385
Daniel Drakee85d0912006-06-02 17:11:32 +0100386static inline void handle_regs_int(struct urb *urb)
387{
388 struct zd_usb *usb = urb->context;
389 struct zd_usb_interrupt *intr = &usb->intr;
390 int len;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800391 u16 int_num;
Daniel Drakee85d0912006-06-02 17:11:32 +0100392
393 ZD_ASSERT(in_interrupt());
394 spin_lock(&intr->lock);
395
Al Virod63ddce2008-05-21 01:34:30 +0100396 int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800397 if (int_num == CR_INTERRUPT) {
398 struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
Jussi Kivilinna8b17f752011-01-31 20:47:27 +0200399 spin_lock(&mac->lock);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800400 memcpy(&mac->intr_buffer, urb->transfer_buffer,
401 USB_MAX_EP_INT_BUFFER);
Jussi Kivilinna8b17f752011-01-31 20:47:27 +0200402 spin_unlock(&mac->lock);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800403 schedule_work(&mac->process_intr);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300404 } else if (atomic_read(&intr->read_regs_enabled)) {
405 len = urb->actual_length;
406 intr->read_regs.length = urb->actual_length;
Daniel Drakee85d0912006-06-02 17:11:32 +0100407 if (len > sizeof(intr->read_regs.buffer))
408 len = sizeof(intr->read_regs.buffer);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300409
Daniel Drakee85d0912006-06-02 17:11:32 +0100410 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300411
412 /* Sometimes USB_INT_ID_REGS is not overridden, but comes after
413 * USB_INT_ID_RETRY_FAILED. Read-reg retry then gets this
414 * delayed USB_INT_ID_REGS, but leaves USB_INT_ID_REGS of
415 * retry unhandled. Next read-reg command then might catch
416 * this wrong USB_INT_ID_REGS. Fix by ignoring wrong reads.
417 */
418 if (!check_read_regs(usb, intr->read_regs.req,
419 intr->read_regs.req_count))
420 goto out;
421
422 atomic_set(&intr->read_regs_enabled, 0);
423 intr->read_regs_int_overridden = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100424 complete(&intr->read_regs.completion);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300425
Daniel Drakee85d0912006-06-02 17:11:32 +0100426 goto out;
427 }
428
Daniel Drakee85d0912006-06-02 17:11:32 +0100429out:
430 spin_unlock(&intr->lock);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300431
432 /* CR_INTERRUPT might override read_reg too. */
433 if (int_num == CR_INTERRUPT && atomic_read(&intr->read_regs_enabled))
434 handle_regs_int_override(urb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100435}
436
David Howells7d12e782006-10-05 14:55:46 +0100437static void int_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100438{
439 int r;
440 struct usb_int_header *hdr;
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300441 struct zd_usb *usb;
442 struct zd_usb_interrupt *intr;
Daniel Drakee85d0912006-06-02 17:11:32 +0100443
444 switch (urb->status) {
445 case 0:
446 break;
447 case -ESHUTDOWN:
448 case -EINVAL:
449 case -ENODEV:
450 case -ENOENT:
451 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100452 case -EPIPE:
Jussi Kivilinna24d24c62011-01-31 20:50:31 +0200453 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200454 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100455 default:
Jussi Kivilinna24d24c62011-01-31 20:50:31 +0200456 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
Daniel Drakee85d0912006-06-02 17:11:32 +0100457 goto resubmit;
458 }
459
460 if (urb->actual_length < sizeof(hdr)) {
461 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
462 goto resubmit;
463 }
464
465 hdr = urb->transfer_buffer;
466 if (hdr->type != USB_INT_TYPE) {
467 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
468 goto resubmit;
469 }
470
Jussi Kivilinnac900eff2011-06-20 14:42:44 +0300471 /* USB_INT_ID_RETRY_FAILED triggered by tx-urb submit can override
472 * pending USB_INT_ID_REGS causing read command timeout.
473 */
474 usb = urb->context;
475 intr = &usb->intr;
476 if (hdr->id != USB_INT_ID_REGS && atomic_read(&intr->read_regs_enabled))
477 handle_regs_int_override(urb);
478
Daniel Drakee85d0912006-06-02 17:11:32 +0100479 switch (hdr->id) {
480 case USB_INT_ID_REGS:
481 handle_regs_int(urb);
482 break;
483 case USB_INT_ID_RETRY_FAILED:
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200484 zd_mac_tx_failed(urb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100485 break;
486 default:
487 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
488 (unsigned int)hdr->id);
489 goto resubmit;
490 }
491
492resubmit:
493 r = usb_submit_urb(urb, GFP_ATOMIC);
494 if (r) {
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200495 dev_dbg_f(urb_dev(urb), "error: resubmit urb %p err code %d\n",
496 urb, r);
497 /* TODO: add worker to reset intr->urb */
Daniel Drakee85d0912006-06-02 17:11:32 +0100498 }
499 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100500}
501
502static inline int int_urb_interval(struct usb_device *udev)
503{
504 switch (udev->speed) {
505 case USB_SPEED_HIGH:
506 return 4;
507 case USB_SPEED_LOW:
508 return 10;
509 case USB_SPEED_FULL:
510 default:
511 return 1;
512 }
513}
514
515static inline int usb_int_enabled(struct zd_usb *usb)
516{
517 unsigned long flags;
518 struct zd_usb_interrupt *intr = &usb->intr;
519 struct urb *urb;
520
521 spin_lock_irqsave(&intr->lock, flags);
522 urb = intr->urb;
523 spin_unlock_irqrestore(&intr->lock, flags);
524 return urb != NULL;
525}
526
527int zd_usb_enable_int(struct zd_usb *usb)
528{
529 int r;
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200530 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100531 struct zd_usb_interrupt *intr = &usb->intr;
Daniel Drakee85d0912006-06-02 17:11:32 +0100532 struct urb *urb;
533
534 dev_dbg_f(zd_usb_dev(usb), "\n");
535
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000536 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100537 if (!urb) {
538 r = -ENOMEM;
539 goto out;
540 }
541
542 ZD_ASSERT(!irqs_disabled());
543 spin_lock_irq(&intr->lock);
544 if (intr->urb) {
545 spin_unlock_irq(&intr->lock);
546 r = 0;
547 goto error_free_urb;
548 }
549 intr->urb = urb;
550 spin_unlock_irq(&intr->lock);
551
Daniel Drakee85d0912006-06-02 17:11:32 +0100552 r = -ENOMEM;
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200553 intr->buffer = usb_alloc_coherent(udev, USB_MAX_EP_INT_BUFFER,
554 GFP_KERNEL, &intr->buffer_dma);
555 if (!intr->buffer) {
Daniel Drakee85d0912006-06-02 17:11:32 +0100556 dev_dbg_f(zd_usb_dev(usb),
557 "couldn't allocate transfer_buffer\n");
558 goto error_set_urb_null;
559 }
560
Daniel Drakee85d0912006-06-02 17:11:32 +0100561 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200562 intr->buffer, USB_MAX_EP_INT_BUFFER,
Daniel Drakee85d0912006-06-02 17:11:32 +0100563 int_urb_complete, usb,
564 intr->interval);
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200565 urb->transfer_dma = intr->buffer_dma;
566 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Daniel Drakee85d0912006-06-02 17:11:32 +0100567
568 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000569 r = usb_submit_urb(urb, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100570 if (r) {
571 dev_dbg_f(zd_usb_dev(usb),
572 "Couldn't submit urb. Error number %d\n", r);
573 goto error;
574 }
575
576 return 0;
577error:
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200578 usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
579 intr->buffer, intr->buffer_dma);
Daniel Drakee85d0912006-06-02 17:11:32 +0100580error_set_urb_null:
581 spin_lock_irq(&intr->lock);
582 intr->urb = NULL;
583 spin_unlock_irq(&intr->lock);
584error_free_urb:
585 usb_free_urb(urb);
586out:
587 return r;
588}
589
590void zd_usb_disable_int(struct zd_usb *usb)
591{
592 unsigned long flags;
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200593 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100594 struct zd_usb_interrupt *intr = &usb->intr;
595 struct urb *urb;
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200596 void *buffer;
597 dma_addr_t buffer_dma;
Daniel Drakee85d0912006-06-02 17:11:32 +0100598
599 spin_lock_irqsave(&intr->lock, flags);
600 urb = intr->urb;
601 if (!urb) {
602 spin_unlock_irqrestore(&intr->lock, flags);
603 return;
604 }
605 intr->urb = NULL;
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200606 buffer = intr->buffer;
607 buffer_dma = intr->buffer_dma;
608 intr->buffer = NULL;
Daniel Drakee85d0912006-06-02 17:11:32 +0100609 spin_unlock_irqrestore(&intr->lock, flags);
610
611 usb_kill_urb(urb);
612 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
613 usb_free_urb(urb);
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200614
615 if (buffer)
616 usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
617 buffer, buffer_dma);
Daniel Drakee85d0912006-06-02 17:11:32 +0100618}
619
620static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
621 unsigned int length)
622{
623 int i;
Daniel Drakee85d0912006-06-02 17:11:32 +0100624 const struct rx_length_info *length_info;
625
626 if (length < sizeof(struct rx_length_info)) {
627 /* It's not a complete packet anyhow. */
Jussi Kivilinnab405e1b2011-06-20 14:42:33 +0300628 dev_dbg_f(zd_usb_dev(usb), "invalid, small RX packet : %d\n",
629 length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100630 return;
631 }
632 length_info = (struct rx_length_info *)
633 (buffer + length - sizeof(struct rx_length_info));
634
635 /* It might be that three frames are merged into a single URB
636 * transaction. We have to check for the length info tag.
637 *
638 * While testing we discovered that length_info might be unaligned,
639 * because if USB transactions are merged, the last packet will not
640 * be padded. Unaligned access might also happen if the length_info
641 * structure is not present.
642 */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700643 if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200644 {
Daniel Drakee85d0912006-06-02 17:11:32 +0100645 unsigned int l, k, n;
646 for (i = 0, l = 0;; i++) {
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700647 k = get_unaligned_le16(&length_info->length[i]);
Ulrich Kunitz850c2112006-11-22 00:06:19 +0000648 if (k == 0)
649 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100650 n = l+k;
651 if (n > length)
652 return;
Daniel Drake459c51a2007-11-19 15:00:29 +0000653 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
Daniel Drakee85d0912006-06-02 17:11:32 +0100654 if (i >= 2)
655 return;
656 l = (n+3) & ~3;
657 }
658 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000659 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100660 }
661}
662
David Howells7d12e782006-10-05 14:55:46 +0100663static void rx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100664{
Jussi Kivilinna24d24c62011-01-31 20:50:31 +0200665 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100666 struct zd_usb *usb;
667 struct zd_usb_rx *rx;
668 const u8 *buffer;
669 unsigned int length;
670
671 switch (urb->status) {
672 case 0:
673 break;
674 case -ESHUTDOWN:
675 case -EINVAL:
676 case -ENODEV:
677 case -ENOENT:
678 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100679 case -EPIPE:
Jussi Kivilinna24d24c62011-01-31 20:50:31 +0200680 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
Daniel Drakeb312d792006-07-05 15:57:39 +0100681 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100682 default:
683 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
684 goto resubmit;
685 }
686
687 buffer = urb->transfer_buffer;
688 length = urb->actual_length;
689 usb = urb->context;
690 rx = &usb->rx;
691
Jussi Kivilinna02353572011-04-01 19:34:16 +0300692 tasklet_schedule(&rx->reset_timer_tasklet);
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200693
Daniel Drakee85d0912006-06-02 17:11:32 +0100694 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
695 /* If there is an old first fragment, we don't care. */
696 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
697 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
698 spin_lock(&rx->lock);
699 memcpy(rx->fragment, buffer, length);
700 rx->fragment_length = length;
701 spin_unlock(&rx->lock);
702 goto resubmit;
703 }
704
705 spin_lock(&rx->lock);
706 if (rx->fragment_length > 0) {
707 /* We are on a second fragment, we believe */
708 ZD_ASSERT(length + rx->fragment_length <=
709 ARRAY_SIZE(rx->fragment));
710 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
711 memcpy(rx->fragment+rx->fragment_length, buffer, length);
712 handle_rx_packet(usb, rx->fragment,
713 rx->fragment_length + length);
714 rx->fragment_length = 0;
715 spin_unlock(&rx->lock);
716 } else {
717 spin_unlock(&rx->lock);
718 handle_rx_packet(usb, buffer, length);
719 }
720
721resubmit:
Jussi Kivilinna24d24c62011-01-31 20:50:31 +0200722 r = usb_submit_urb(urb, GFP_ATOMIC);
723 if (r)
724 dev_dbg_f(urb_dev(urb), "urb %p resubmit error %d\n", urb, r);
Daniel Drakee85d0912006-06-02 17:11:32 +0100725}
726
Daniel Drake459c51a2007-11-19 15:00:29 +0000727static struct urb *alloc_rx_urb(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100728{
729 struct usb_device *udev = zd_usb_to_usbdev(usb);
730 struct urb *urb;
731 void *buffer;
732
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000733 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100734 if (!urb)
735 return NULL;
Daniel Mack997ea582010-04-12 13:17:25 +0200736 buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
737 &urb->transfer_dma);
Daniel Drakee85d0912006-06-02 17:11:32 +0100738 if (!buffer) {
739 usb_free_urb(urb);
740 return NULL;
741 }
742
743 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
Daniel Mack997ea582010-04-12 13:17:25 +0200744 buffer, USB_MAX_RX_SIZE,
Daniel Drakee85d0912006-06-02 17:11:32 +0100745 rx_urb_complete, usb);
746 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
747
748 return urb;
749}
750
Daniel Drake459c51a2007-11-19 15:00:29 +0000751static void free_rx_urb(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100752{
753 if (!urb)
754 return;
Daniel Mack997ea582010-04-12 13:17:25 +0200755 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
756 urb->transfer_buffer, urb->transfer_dma);
Daniel Drakee85d0912006-06-02 17:11:32 +0100757 usb_free_urb(urb);
758}
759
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200760static int __zd_usb_enable_rx(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100761{
762 int i, r;
763 struct zd_usb_rx *rx = &usb->rx;
764 struct urb **urbs;
765
766 dev_dbg_f(zd_usb_dev(usb), "\n");
767
768 r = -ENOMEM;
Daniel Drake459c51a2007-11-19 15:00:29 +0000769 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100770 if (!urbs)
771 goto error;
Daniel Drake459c51a2007-11-19 15:00:29 +0000772 for (i = 0; i < RX_URBS_COUNT; i++) {
773 urbs[i] = alloc_rx_urb(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100774 if (!urbs[i])
775 goto error;
776 }
777
778 ZD_ASSERT(!irqs_disabled());
779 spin_lock_irq(&rx->lock);
780 if (rx->urbs) {
781 spin_unlock_irq(&rx->lock);
782 r = 0;
783 goto error;
784 }
785 rx->urbs = urbs;
Daniel Drake459c51a2007-11-19 15:00:29 +0000786 rx->urbs_count = RX_URBS_COUNT;
Daniel Drakee85d0912006-06-02 17:11:32 +0100787 spin_unlock_irq(&rx->lock);
788
Daniel Drake459c51a2007-11-19 15:00:29 +0000789 for (i = 0; i < RX_URBS_COUNT; i++) {
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000790 r = usb_submit_urb(urbs[i], GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100791 if (r)
792 goto error_submit;
793 }
794
795 return 0;
796error_submit:
Daniel Drake459c51a2007-11-19 15:00:29 +0000797 for (i = 0; i < RX_URBS_COUNT; i++) {
Daniel Drakee85d0912006-06-02 17:11:32 +0100798 usb_kill_urb(urbs[i]);
799 }
800 spin_lock_irq(&rx->lock);
801 rx->urbs = NULL;
802 rx->urbs_count = 0;
803 spin_unlock_irq(&rx->lock);
804error:
805 if (urbs) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000806 for (i = 0; i < RX_URBS_COUNT; i++)
807 free_rx_urb(urbs[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +0100808 }
809 return r;
810}
811
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200812int zd_usb_enable_rx(struct zd_usb *usb)
813{
814 int r;
815 struct zd_usb_rx *rx = &usb->rx;
816
817 mutex_lock(&rx->setup_mutex);
818 r = __zd_usb_enable_rx(usb);
819 mutex_unlock(&rx->setup_mutex);
820
821 zd_usb_reset_rx_idle_timer(usb);
822
823 return r;
824}
825
826static void __zd_usb_disable_rx(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100827{
828 int i;
829 unsigned long flags;
830 struct urb **urbs;
831 unsigned int count;
832 struct zd_usb_rx *rx = &usb->rx;
833
834 spin_lock_irqsave(&rx->lock, flags);
835 urbs = rx->urbs;
836 count = rx->urbs_count;
837 spin_unlock_irqrestore(&rx->lock, flags);
838 if (!urbs)
839 return;
840
841 for (i = 0; i < count; i++) {
842 usb_kill_urb(urbs[i]);
Daniel Drake459c51a2007-11-19 15:00:29 +0000843 free_rx_urb(urbs[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +0100844 }
845 kfree(urbs);
846
847 spin_lock_irqsave(&rx->lock, flags);
848 rx->urbs = NULL;
849 rx->urbs_count = 0;
850 spin_unlock_irqrestore(&rx->lock, flags);
851}
852
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200853void zd_usb_disable_rx(struct zd_usb *usb)
854{
855 struct zd_usb_rx *rx = &usb->rx;
856
857 mutex_lock(&rx->setup_mutex);
858 __zd_usb_disable_rx(usb);
859 mutex_unlock(&rx->setup_mutex);
860
Jussi Kivilinna02353572011-04-01 19:34:16 +0300861 tasklet_kill(&rx->reset_timer_tasklet);
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200862 cancel_delayed_work_sync(&rx->idle_work);
863}
864
865static void zd_usb_reset_rx(struct zd_usb *usb)
866{
867 bool do_reset;
868 struct zd_usb_rx *rx = &usb->rx;
869 unsigned long flags;
870
871 mutex_lock(&rx->setup_mutex);
872
873 spin_lock_irqsave(&rx->lock, flags);
874 do_reset = rx->urbs != NULL;
875 spin_unlock_irqrestore(&rx->lock, flags);
876
877 if (do_reset) {
878 __zd_usb_disable_rx(usb);
879 __zd_usb_enable_rx(usb);
880 }
881
882 mutex_unlock(&rx->setup_mutex);
883
884 if (do_reset)
885 zd_usb_reset_rx_idle_timer(usb);
886}
887
Daniel Drake459c51a2007-11-19 15:00:29 +0000888/**
889 * zd_usb_disable_tx - disable transmission
890 * @usb: the zd1211rw-private USB structure
891 *
892 * Frees all URBs in the free list and marks the transmission as disabled.
893 */
894void zd_usb_disable_tx(struct zd_usb *usb)
895{
896 struct zd_usb_tx *tx = &usb->tx;
897 unsigned long flags;
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200898
899 atomic_set(&tx->enabled, 0);
900
901 /* kill all submitted tx-urbs */
902 usb_kill_anchored_urbs(&tx->submitted);
Daniel Drake459c51a2007-11-19 15:00:29 +0000903
904 spin_lock_irqsave(&tx->lock, flags);
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200905 WARN_ON(!skb_queue_empty(&tx->submitted_skbs));
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200906 WARN_ON(tx->submitted_urbs != 0);
Daniel Drake459c51a2007-11-19 15:00:29 +0000907 tx->submitted_urbs = 0;
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200908 spin_unlock_irqrestore(&tx->lock, flags);
909
Daniel Drake459c51a2007-11-19 15:00:29 +0000910 /* The stopped state is ignored, relying on ieee80211_wake_queues()
911 * in a potentionally following zd_usb_enable_tx().
912 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000913}
914
915/**
916 * zd_usb_enable_tx - enables transmission
917 * @usb: a &struct zd_usb pointer
918 *
919 * This function enables transmission and prepares the &zd_usb_tx data
920 * structure.
921 */
922void zd_usb_enable_tx(struct zd_usb *usb)
923{
924 unsigned long flags;
925 struct zd_usb_tx *tx = &usb->tx;
926
927 spin_lock_irqsave(&tx->lock, flags);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200928 atomic_set(&tx->enabled, 1);
Daniel Drake459c51a2007-11-19 15:00:29 +0000929 tx->submitted_urbs = 0;
930 ieee80211_wake_queues(zd_usb_to_hw(usb));
931 tx->stopped = 0;
932 spin_unlock_irqrestore(&tx->lock, flags);
933}
934
Daniel Drake459c51a2007-11-19 15:00:29 +0000935static void tx_dec_submitted_urbs(struct zd_usb *usb)
936{
937 struct zd_usb_tx *tx = &usb->tx;
938 unsigned long flags;
939
940 spin_lock_irqsave(&tx->lock, flags);
941 --tx->submitted_urbs;
942 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
943 ieee80211_wake_queues(zd_usb_to_hw(usb));
944 tx->stopped = 0;
945 }
946 spin_unlock_irqrestore(&tx->lock, flags);
947}
948
949static void tx_inc_submitted_urbs(struct zd_usb *usb)
950{
951 struct zd_usb_tx *tx = &usb->tx;
952 unsigned long flags;
953
954 spin_lock_irqsave(&tx->lock, flags);
955 ++tx->submitted_urbs;
956 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
957 ieee80211_stop_queues(zd_usb_to_hw(usb));
958 tx->stopped = 1;
959 }
960 spin_unlock_irqrestore(&tx->lock, flags);
961}
962
963/**
964 * tx_urb_complete - completes the execution of an URB
965 * @urb: a URB
966 *
967 * This function is called if the URB has been transferred to a device or an
968 * error has happened.
969 */
David Howells7d12e782006-10-05 14:55:46 +0100970static void tx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100971{
972 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000973 struct sk_buff *skb;
Johannes Berge039fa42008-05-15 12:55:29 +0200974 struct ieee80211_tx_info *info;
Daniel Drake459c51a2007-11-19 15:00:29 +0000975 struct zd_usb *usb;
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200976 struct zd_usb_tx *tx;
977
978 skb = (struct sk_buff *)urb->context;
979 info = IEEE80211_SKB_CB(skb);
980 /*
981 * grab 'usb' pointer before handing off the skb (since
982 * it might be freed by zd_mac_tx_to_dev or mac80211)
983 */
984 usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb;
985 tx = &usb->tx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100986
987 switch (urb->status) {
988 case 0:
989 break;
990 case -ESHUTDOWN:
991 case -EINVAL:
992 case -ENODEV:
993 case -ENOENT:
994 case -ECONNRESET:
Daniel Drakeb312d792006-07-05 15:57:39 +0100995 case -EPIPE:
Daniel Drakee85d0912006-06-02 17:11:32 +0100996 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
997 break;
Daniel Drakee85d0912006-06-02 17:11:32 +0100998 default:
999 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
1000 goto resubmit;
1001 }
1002free_urb:
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001003 skb_unlink(skb, &usb->tx.submitted_skbs);
Johannes Bergdbabad02008-05-08 01:43:59 +02001004 zd_mac_tx_to_dev(skb, urb->status);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001005 usb_free_urb(urb);
Daniel Drake459c51a2007-11-19 15:00:29 +00001006 tx_dec_submitted_urbs(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001007 return;
1008resubmit:
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001009 usb_anchor_urb(urb, &tx->submitted);
Daniel Drakee85d0912006-06-02 17:11:32 +01001010 r = usb_submit_urb(urb, GFP_ATOMIC);
1011 if (r) {
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001012 usb_unanchor_urb(urb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001013 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
1014 goto free_urb;
1015 }
1016}
1017
Daniel Drake459c51a2007-11-19 15:00:29 +00001018/**
1019 * zd_usb_tx: initiates transfer of a frame of the device
1020 *
1021 * @usb: the zd1211rw-private USB structure
1022 * @skb: a &struct sk_buff pointer
1023 *
1024 * This function tranmits a frame to the device. It doesn't wait for
1025 * completion. The frame must contain the control set and have all the
1026 * control set information available.
1027 *
1028 * The function returns 0 if the transfer has been successfully initiated.
Daniel Drakee85d0912006-06-02 17:11:32 +01001029 */
Daniel Drake459c51a2007-11-19 15:00:29 +00001030int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +01001031{
1032 int r;
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001033 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001034 struct usb_device *udev = zd_usb_to_usbdev(usb);
1035 struct urb *urb;
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001036 struct zd_usb_tx *tx = &usb->tx;
Daniel Drakee85d0912006-06-02 17:11:32 +01001037
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001038 if (!atomic_read(&tx->enabled)) {
1039 r = -ENOENT;
1040 goto out;
1041 }
1042
1043 urb = usb_alloc_urb(0, GFP_ATOMIC);
Daniel Drakee85d0912006-06-02 17:11:32 +01001044 if (!urb) {
1045 r = -ENOMEM;
1046 goto out;
1047 }
1048
Daniel Drakee85d0912006-06-02 17:11:32 +01001049 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
Daniel Drake459c51a2007-11-19 15:00:29 +00001050 skb->data, skb->len, tx_urb_complete, skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001051
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001052 info->rate_driver_data[1] = (void *)jiffies;
1053 skb_queue_tail(&tx->submitted_skbs, skb);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001054 usb_anchor_urb(urb, &tx->submitted);
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001055
Daniel Drakee85d0912006-06-02 17:11:32 +01001056 r = usb_submit_urb(urb, GFP_ATOMIC);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001057 if (r) {
Jussi Kivilinna24d24c62011-01-31 20:50:31 +02001058 dev_dbg_f(zd_usb_dev(usb), "error submit urb %p %d\n", urb, r);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001059 usb_unanchor_urb(urb);
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001060 skb_unlink(skb, &tx->submitted_skbs);
Daniel Drakee85d0912006-06-02 17:11:32 +01001061 goto error;
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001062 }
Daniel Drake459c51a2007-11-19 15:00:29 +00001063 tx_inc_submitted_urbs(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001064 return 0;
1065error:
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001066 usb_free_urb(urb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001067out:
1068 return r;
1069}
1070
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001071static bool zd_tx_timeout(struct zd_usb *usb)
1072{
1073 struct zd_usb_tx *tx = &usb->tx;
1074 struct sk_buff_head *q = &tx->submitted_skbs;
1075 struct sk_buff *skb, *skbnext;
1076 struct ieee80211_tx_info *info;
1077 unsigned long flags, trans_start;
1078 bool have_timedout = false;
1079
1080 spin_lock_irqsave(&q->lock, flags);
1081 skb_queue_walk_safe(q, skb, skbnext) {
1082 info = IEEE80211_SKB_CB(skb);
1083 trans_start = (unsigned long)info->rate_driver_data[1];
1084
1085 if (time_is_before_jiffies(trans_start + ZD_TX_TIMEOUT)) {
1086 have_timedout = true;
1087 break;
1088 }
1089 }
1090 spin_unlock_irqrestore(&q->lock, flags);
1091
1092 return have_timedout;
1093}
1094
1095static void zd_tx_watchdog_handler(struct work_struct *work)
1096{
1097 struct zd_usb *usb =
1098 container_of(work, struct zd_usb, tx.watchdog_work.work);
1099 struct zd_usb_tx *tx = &usb->tx;
1100
1101 if (!atomic_read(&tx->enabled) || !tx->watchdog_enabled)
1102 goto out;
1103 if (!zd_tx_timeout(usb))
1104 goto out;
1105
1106 /* TX halted, try reset */
1107 dev_warn(zd_usb_dev(usb), "TX-stall detected, reseting device...");
1108
1109 usb_queue_reset_device(usb->intf);
1110
1111 /* reset will stop this worker, don't rearm */
1112 return;
1113out:
1114 queue_delayed_work(zd_workqueue, &tx->watchdog_work,
1115 ZD_TX_WATCHDOG_INTERVAL);
1116}
1117
1118void zd_tx_watchdog_enable(struct zd_usb *usb)
1119{
1120 struct zd_usb_tx *tx = &usb->tx;
1121
1122 if (!tx->watchdog_enabled) {
1123 dev_dbg_f(zd_usb_dev(usb), "\n");
1124 queue_delayed_work(zd_workqueue, &tx->watchdog_work,
1125 ZD_TX_WATCHDOG_INTERVAL);
1126 tx->watchdog_enabled = 1;
1127 }
1128}
1129
1130void zd_tx_watchdog_disable(struct zd_usb *usb)
1131{
1132 struct zd_usb_tx *tx = &usb->tx;
1133
1134 if (tx->watchdog_enabled) {
1135 dev_dbg_f(zd_usb_dev(usb), "\n");
1136 tx->watchdog_enabled = 0;
1137 cancel_delayed_work_sync(&tx->watchdog_work);
1138 }
1139}
1140
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +02001141static void zd_rx_idle_timer_handler(struct work_struct *work)
1142{
1143 struct zd_usb *usb =
1144 container_of(work, struct zd_usb, rx.idle_work.work);
1145 struct zd_mac *mac = zd_usb_to_mac(usb);
1146
1147 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1148 return;
1149
1150 dev_dbg_f(zd_usb_dev(usb), "\n");
1151
1152 /* 30 seconds since last rx, reset rx */
1153 zd_usb_reset_rx(usb);
1154}
1155
Jussi Kivilinna02353572011-04-01 19:34:16 +03001156static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param)
1157{
1158 struct zd_usb *usb = (struct zd_usb *)param;
1159
1160 zd_usb_reset_rx_idle_timer(usb);
1161}
1162
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +02001163void zd_usb_reset_rx_idle_timer(struct zd_usb *usb)
1164{
1165 struct zd_usb_rx *rx = &usb->rx;
1166
1167 cancel_delayed_work(&rx->idle_work);
1168 queue_delayed_work(zd_workqueue, &rx->idle_work, ZD_RX_IDLE_INTERVAL);
1169}
1170
Daniel Drakee85d0912006-06-02 17:11:32 +01001171static inline void init_usb_interrupt(struct zd_usb *usb)
1172{
1173 struct zd_usb_interrupt *intr = &usb->intr;
1174
1175 spin_lock_init(&intr->lock);
1176 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
1177 init_completion(&intr->read_regs.completion);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001178 atomic_set(&intr->read_regs_enabled, 0);
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001179 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
Daniel Drakee85d0912006-06-02 17:11:32 +01001180}
1181
1182static inline void init_usb_rx(struct zd_usb *usb)
1183{
1184 struct zd_usb_rx *rx = &usb->rx;
Jussi Kivilinna02353572011-04-01 19:34:16 +03001185
Daniel Drakee85d0912006-06-02 17:11:32 +01001186 spin_lock_init(&rx->lock);
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +02001187 mutex_init(&rx->setup_mutex);
Daniel Drakee85d0912006-06-02 17:11:32 +01001188 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
1189 rx->usb_packet_size = 512;
1190 } else {
1191 rx->usb_packet_size = 64;
1192 }
1193 ZD_ASSERT(rx->fragment_length == 0);
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +02001194 INIT_DELAYED_WORK(&rx->idle_work, zd_rx_idle_timer_handler);
Jussi Kivilinna02353572011-04-01 19:34:16 +03001195 rx->reset_timer_tasklet.func = zd_usb_reset_rx_idle_timer_tasklet;
1196 rx->reset_timer_tasklet.data = (unsigned long)usb;
Daniel Drakee85d0912006-06-02 17:11:32 +01001197}
1198
1199static inline void init_usb_tx(struct zd_usb *usb)
1200{
Daniel Drake459c51a2007-11-19 15:00:29 +00001201 struct zd_usb_tx *tx = &usb->tx;
Jussi Kivilinna02353572011-04-01 19:34:16 +03001202
Daniel Drake459c51a2007-11-19 15:00:29 +00001203 spin_lock_init(&tx->lock);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001204 atomic_set(&tx->enabled, 0);
Daniel Drake459c51a2007-11-19 15:00:29 +00001205 tx->stopped = 0;
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001206 skb_queue_head_init(&tx->submitted_skbs);
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001207 init_usb_anchor(&tx->submitted);
Daniel Drake459c51a2007-11-19 15:00:29 +00001208 tx->submitted_urbs = 0;
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001209 tx->watchdog_enabled = 0;
1210 INIT_DELAYED_WORK(&tx->watchdog_work, zd_tx_watchdog_handler);
Daniel Drakee85d0912006-06-02 17:11:32 +01001211}
1212
Daniel Drake459c51a2007-11-19 15:00:29 +00001213void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
Daniel Drakee85d0912006-06-02 17:11:32 +01001214 struct usb_interface *intf)
1215{
1216 memset(usb, 0, sizeof(*usb));
1217 usb->intf = usb_get_intf(intf);
Daniel Drake459c51a2007-11-19 15:00:29 +00001218 usb_set_intfdata(usb->intf, hw);
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001219 init_usb_anchor(&usb->submitted_cmds);
Daniel Drakee85d0912006-06-02 17:11:32 +01001220 init_usb_interrupt(usb);
1221 init_usb_tx(usb);
1222 init_usb_rx(usb);
1223}
1224
Daniel Drakee85d0912006-06-02 17:11:32 +01001225void zd_usb_clear(struct zd_usb *usb)
1226{
1227 usb_set_intfdata(usb->intf, NULL);
1228 usb_put_intf(usb->intf);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +01001229 ZD_MEMCLEAR(usb, sizeof(*usb));
Daniel Drakee85d0912006-06-02 17:11:32 +01001230 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1231}
1232
1233static const char *speed(enum usb_device_speed speed)
1234{
1235 switch (speed) {
1236 case USB_SPEED_LOW:
1237 return "low";
1238 case USB_SPEED_FULL:
1239 return "full";
1240 case USB_SPEED_HIGH:
1241 return "high";
1242 default:
1243 return "unknown speed";
1244 }
1245}
1246
1247static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
1248{
1249 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1250 le16_to_cpu(udev->descriptor.idVendor),
1251 le16_to_cpu(udev->descriptor.idProduct),
1252 get_bcdDevice(udev),
1253 speed(udev->speed));
1254}
1255
1256int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1257{
1258 struct usb_device *udev = interface_to_usbdev(usb->intf);
1259 return scnprint_id(udev, buffer, size);
1260}
1261
1262#ifdef DEBUG
1263static void print_id(struct usb_device *udev)
1264{
1265 char buffer[40];
1266
1267 scnprint_id(udev, buffer, sizeof(buffer));
1268 buffer[sizeof(buffer)-1] = 0;
1269 dev_dbg_f(&udev->dev, "%s\n", buffer);
1270}
1271#else
1272#define print_id(udev) do { } while (0)
1273#endif
1274
Daniel Drakea1030e92006-08-13 12:15:29 +01001275static int eject_installer(struct usb_interface *intf)
1276{
1277 struct usb_device *udev = interface_to_usbdev(intf);
1278 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1279 struct usb_endpoint_descriptor *endpoint;
1280 unsigned char *cmd;
1281 u8 bulk_out_ep;
1282 int r;
1283
1284 /* Find bulk out endpoint */
Stefan Seyfried11466f12009-12-08 15:21:35 +01001285 for (r = 1; r >= 0; r--) {
1286 endpoint = &iface_desc->endpoint[r].desc;
1287 if (usb_endpoint_dir_out(endpoint) &&
1288 usb_endpoint_xfer_bulk(endpoint)) {
1289 bulk_out_ep = endpoint->bEndpointAddress;
1290 break;
1291 }
1292 }
1293 if (r == -1) {
Daniel Drakea1030e92006-08-13 12:15:29 +01001294 dev_err(&udev->dev,
1295 "zd1211rw: Could not find bulk out endpoint\n");
1296 return -ENODEV;
1297 }
1298
1299 cmd = kzalloc(31, GFP_KERNEL);
1300 if (cmd == NULL)
1301 return -ENODEV;
1302
1303 /* USB bulk command block */
1304 cmd[0] = 0x55; /* bulk command signature */
1305 cmd[1] = 0x53; /* bulk command signature */
1306 cmd[2] = 0x42; /* bulk command signature */
1307 cmd[3] = 0x43; /* bulk command signature */
1308 cmd[14] = 6; /* command length */
1309
1310 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1311 cmd[19] = 0x2; /* eject disc */
1312
1313 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1314 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1315 cmd, 31, NULL, 2000);
1316 kfree(cmd);
1317 if (r)
1318 return r;
1319
1320 /* At this point, the device disconnects and reconnects with the real
1321 * ID numbers. */
1322
1323 usb_set_intfdata(intf, NULL);
1324 return 0;
1325}
1326
Daniel Drake74553ae2007-07-01 18:22:32 +01001327int zd_usb_init_hw(struct zd_usb *usb)
1328{
1329 int r;
1330 struct zd_mac *mac = zd_usb_to_mac(usb);
1331
1332 dev_dbg_f(zd_usb_dev(usb), "\n");
1333
1334 r = upload_firmware(usb);
1335 if (r) {
1336 dev_err(zd_usb_dev(usb),
1337 "couldn't load firmware. Error number %d\n", r);
1338 return r;
1339 }
1340
1341 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1342 if (r) {
1343 dev_dbg_f(zd_usb_dev(usb),
1344 "couldn't reset configuration. Error number %d\n", r);
1345 return r;
1346 }
1347
Daniel Drake459c51a2007-11-19 15:00:29 +00001348 r = zd_mac_init_hw(mac->hw);
Daniel Drake74553ae2007-07-01 18:22:32 +01001349 if (r) {
1350 dev_dbg_f(zd_usb_dev(usb),
1351 "couldn't initialize mac. Error number %d\n", r);
1352 return r;
1353 }
1354
1355 usb->initialized = 1;
1356 return 0;
1357}
1358
Daniel Drakee85d0912006-06-02 17:11:32 +01001359static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1360{
1361 int r;
1362 struct usb_device *udev = interface_to_usbdev(intf);
Daniel Drake459c51a2007-11-19 15:00:29 +00001363 struct zd_usb *usb;
1364 struct ieee80211_hw *hw = NULL;
Daniel Drakee85d0912006-06-02 17:11:32 +01001365
1366 print_id(udev);
1367
Daniel Drakea1030e92006-08-13 12:15:29 +01001368 if (id->driver_info & DEVICE_INSTALLER)
1369 return eject_installer(intf);
1370
Daniel Drakee85d0912006-06-02 17:11:32 +01001371 switch (udev->speed) {
1372 case USB_SPEED_LOW:
1373 case USB_SPEED_FULL:
1374 case USB_SPEED_HIGH:
1375 break;
1376 default:
1377 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1378 r = -ENODEV;
1379 goto error;
1380 }
1381
Daniel Drake459c51a2007-11-19 15:00:29 +00001382 r = usb_reset_device(udev);
1383 if (r) {
1384 dev_err(&intf->dev,
1385 "couldn't reset usb device. Error number %d\n", r);
1386 goto error;
1387 }
Ulrich Kunitz6e3632f2007-01-29 00:59:28 +00001388
Daniel Drake459c51a2007-11-19 15:00:29 +00001389 hw = zd_mac_alloc_hw(intf);
1390 if (hw == NULL) {
Daniel Drakee85d0912006-06-02 17:11:32 +01001391 r = -ENOMEM;
1392 goto error;
1393 }
1394
Daniel Drake459c51a2007-11-19 15:00:29 +00001395 usb = &zd_hw_mac(hw)->chip.usb;
Daniel Drake74553ae2007-07-01 18:22:32 +01001396 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001397
Daniel Drake459c51a2007-11-19 15:00:29 +00001398 r = zd_mac_preinit_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001399 if (r) {
1400 dev_dbg_f(&intf->dev,
1401 "couldn't initialize mac. Error number %d\n", r);
1402 goto error;
1403 }
1404
Daniel Drake459c51a2007-11-19 15:00:29 +00001405 r = ieee80211_register_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001406 if (r) {
1407 dev_dbg_f(&intf->dev,
Daniel Drake459c51a2007-11-19 15:00:29 +00001408 "couldn't register device. Error number %d\n", r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001409 goto error;
1410 }
1411
1412 dev_dbg_f(&intf->dev, "successful\n");
Daniel Drake459c51a2007-11-19 15:00:29 +00001413 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
Daniel Drakee85d0912006-06-02 17:11:32 +01001414 return 0;
1415error:
1416 usb_reset_device(interface_to_usbdev(intf));
Daniel Drake459c51a2007-11-19 15:00:29 +00001417 if (hw) {
1418 zd_mac_clear(zd_hw_mac(hw));
1419 ieee80211_free_hw(hw);
1420 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001421 return r;
1422}
1423
1424static void disconnect(struct usb_interface *intf)
1425{
Daniel Drake459c51a2007-11-19 15:00:29 +00001426 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
Marc Pignate0579d52007-10-15 08:51:52 +02001427 struct zd_mac *mac;
1428 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +01001429
Daniel Drakea1030e92006-08-13 12:15:29 +01001430 /* Either something really bad happened, or we're just dealing with
1431 * a DEVICE_INSTALLER. */
Daniel Drake459c51a2007-11-19 15:00:29 +00001432 if (hw == NULL)
Daniel Drakea1030e92006-08-13 12:15:29 +01001433 return;
1434
Daniel Drake459c51a2007-11-19 15:00:29 +00001435 mac = zd_hw_mac(hw);
Marc Pignate0579d52007-10-15 08:51:52 +02001436 usb = &mac->chip.usb;
1437
Daniel Drakee85d0912006-06-02 17:11:32 +01001438 dev_dbg_f(zd_usb_dev(usb), "\n");
1439
Daniel Drake459c51a2007-11-19 15:00:29 +00001440 ieee80211_unregister_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001441
1442 /* Just in case something has gone wrong! */
Jussi Kivilinna78fc8002011-01-31 20:47:08 +02001443 zd_usb_disable_tx(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001444 zd_usb_disable_rx(usb);
1445 zd_usb_disable_int(usb);
1446
1447 /* If the disconnect has been caused by a removal of the
1448 * driver module, the reset allows reloading of the driver. If the
1449 * reset will not be executed here, the upload of the firmware in the
1450 * probe function caused by the reloading of the driver will fail.
1451 */
1452 usb_reset_device(interface_to_usbdev(intf));
1453
Daniel Drake459c51a2007-11-19 15:00:29 +00001454 zd_mac_clear(mac);
1455 ieee80211_free_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001456 dev_dbg(&intf->dev, "disconnected\n");
1457}
1458
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001459static void zd_usb_resume(struct zd_usb *usb)
1460{
1461 struct zd_mac *mac = zd_usb_to_mac(usb);
1462 int r;
1463
1464 dev_dbg_f(zd_usb_dev(usb), "\n");
1465
1466 r = zd_op_start(zd_usb_to_hw(usb));
1467 if (r < 0) {
1468 dev_warn(zd_usb_dev(usb), "Device resume failed "
1469 "with error code %d. Retrying...\n", r);
1470 if (usb->was_running)
1471 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
1472 usb_queue_reset_device(usb->intf);
1473 return;
1474 }
1475
1476 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) {
1477 r = zd_restore_settings(mac);
1478 if (r < 0) {
1479 dev_dbg(zd_usb_dev(usb),
1480 "failed to restore settings, %d\n", r);
1481 return;
1482 }
1483 }
1484}
1485
1486static void zd_usb_stop(struct zd_usb *usb)
1487{
1488 dev_dbg_f(zd_usb_dev(usb), "\n");
1489
1490 zd_op_stop(zd_usb_to_hw(usb));
1491
1492 zd_usb_disable_tx(usb);
1493 zd_usb_disable_rx(usb);
1494 zd_usb_disable_int(usb);
1495
1496 usb->initialized = 0;
1497}
1498
1499static int pre_reset(struct usb_interface *intf)
1500{
1501 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1502 struct zd_mac *mac;
1503 struct zd_usb *usb;
1504
1505 if (!hw || intf->condition != USB_INTERFACE_BOUND)
1506 return 0;
1507
1508 mac = zd_hw_mac(hw);
1509 usb = &mac->chip.usb;
1510
1511 usb->was_running = test_bit(ZD_DEVICE_RUNNING, &mac->flags);
1512
1513 zd_usb_stop(usb);
1514
1515 mutex_lock(&mac->chip.mutex);
1516 return 0;
1517}
1518
1519static int post_reset(struct usb_interface *intf)
1520{
1521 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1522 struct zd_mac *mac;
1523 struct zd_usb *usb;
1524
1525 if (!hw || intf->condition != USB_INTERFACE_BOUND)
1526 return 0;
1527
1528 mac = zd_hw_mac(hw);
1529 usb = &mac->chip.usb;
1530
1531 mutex_unlock(&mac->chip.mutex);
1532
1533 if (usb->was_running)
1534 zd_usb_resume(usb);
1535 return 0;
1536}
1537
Daniel Drakee85d0912006-06-02 17:11:32 +01001538static struct usb_driver driver = {
Daniel Drake459c51a2007-11-19 15:00:29 +00001539 .name = KBUILD_MODNAME,
Daniel Drakee85d0912006-06-02 17:11:32 +01001540 .id_table = usb_ids,
1541 .probe = probe,
1542 .disconnect = disconnect,
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001543 .pre_reset = pre_reset,
1544 .post_reset = post_reset,
Daniel Drakee85d0912006-06-02 17:11:32 +01001545};
1546
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001547struct workqueue_struct *zd_workqueue;
1548
Daniel Drakee85d0912006-06-02 17:11:32 +01001549static int __init usb_init(void)
1550{
1551 int r;
1552
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001553 pr_debug("%s usb_init()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001554
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001555 zd_workqueue = create_singlethread_workqueue(driver.name);
1556 if (zd_workqueue == NULL) {
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001557 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001558 return -ENOMEM;
1559 }
1560
Daniel Drakee85d0912006-06-02 17:11:32 +01001561 r = usb_register(&driver);
1562 if (r) {
Maxime Austruy192b7752007-01-29 00:59:51 +00001563 destroy_workqueue(zd_workqueue);
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001564 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1565 driver.name, r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001566 return r;
1567 }
1568
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001569 pr_debug("%s initialized\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001570 return 0;
1571}
1572
1573static void __exit usb_exit(void)
1574{
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001575 pr_debug("%s usb_exit()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001576 usb_deregister(&driver);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001577 destroy_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +01001578}
1579
1580module_init(usb_init);
1581module_exit(usb_exit);
1582
Jussi Kivilinna59342f62011-05-30 10:15:47 +03001583static int zd_ep_regs_out_msg(struct usb_device *udev, void *data, int len,
1584 int *actual_length, int timeout)
1585{
1586 /* In USB 2.0 mode EP_REGS_OUT endpoint is interrupt type. However in
1587 * USB 1.1 mode endpoint is bulk. Select correct type URB by endpoint
1588 * descriptor.
1589 */
1590 struct usb_host_endpoint *ep;
1591 unsigned int pipe;
1592
1593 pipe = usb_sndintpipe(udev, EP_REGS_OUT);
1594 ep = usb_pipe_endpoint(udev, pipe);
1595 if (!ep)
1596 return -EINVAL;
1597
1598 if (usb_endpoint_xfer_int(&ep->desc)) {
1599 return usb_interrupt_msg(udev, pipe, data, len,
1600 actual_length, timeout);
1601 } else {
1602 pipe = usb_sndbulkpipe(udev, EP_REGS_OUT);
1603 return usb_bulk_msg(udev, pipe, data, len, actual_length,
1604 timeout);
1605 }
1606}
1607
Daniel Drakee85d0912006-06-02 17:11:32 +01001608static int usb_int_regs_length(unsigned int count)
1609{
1610 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1611}
1612
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001613static void prepare_read_regs_int(struct zd_usb *usb,
1614 struct usb_req_read_regs *req,
1615 unsigned int count)
Daniel Drakee85d0912006-06-02 17:11:32 +01001616{
1617 struct zd_usb_interrupt *intr = &usb->intr;
1618
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001619 spin_lock_irq(&intr->lock);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001620 atomic_set(&intr->read_regs_enabled, 1);
1621 intr->read_regs.req = req;
1622 intr->read_regs.req_count = count;
Daniel Drakee85d0912006-06-02 17:11:32 +01001623 INIT_COMPLETION(intr->read_regs.completion);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001624 spin_unlock_irq(&intr->lock);
1625}
1626
1627static void disable_read_regs_int(struct zd_usb *usb)
1628{
1629 struct zd_usb_interrupt *intr = &usb->intr;
1630
1631 spin_lock_irq(&intr->lock);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001632 atomic_set(&intr->read_regs_enabled, 0);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001633 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001634}
1635
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001636static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
1637 unsigned int count)
1638{
1639 int i;
1640 struct zd_usb_interrupt *intr = &usb->intr;
1641 struct read_regs_int *rr = &intr->read_regs;
1642 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1643
1644 /* The created block size seems to be larger than expected.
1645 * However results appear to be correct.
1646 */
1647 if (rr->length < usb_int_regs_length(count)) {
1648 dev_dbg_f(zd_usb_dev(usb),
1649 "error: actual length %d less than expected %d\n",
1650 rr->length, usb_int_regs_length(count));
1651 return false;
1652 }
1653
1654 if (rr->length > sizeof(rr->buffer)) {
1655 dev_dbg_f(zd_usb_dev(usb),
1656 "error: actual length %d exceeds buffer size %zu\n",
1657 rr->length, sizeof(rr->buffer));
1658 return false;
1659 }
1660
1661 for (i = 0; i < count; i++) {
1662 struct reg_data *rd = &regs->regs[i];
1663 if (rd->addr != req->addr[i]) {
1664 dev_dbg_f(zd_usb_dev(usb),
1665 "rd[%d] addr %#06hx expected %#06hx\n", i,
1666 le16_to_cpu(rd->addr),
1667 le16_to_cpu(req->addr[i]));
1668 return false;
1669 }
1670 }
1671
1672 return true;
1673}
1674
Daniel Drakee85d0912006-06-02 17:11:32 +01001675static int get_results(struct zd_usb *usb, u16 *values,
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001676 struct usb_req_read_regs *req, unsigned int count,
1677 bool *retry)
Daniel Drakee85d0912006-06-02 17:11:32 +01001678{
1679 int r;
1680 int i;
1681 struct zd_usb_interrupt *intr = &usb->intr;
1682 struct read_regs_int *rr = &intr->read_regs;
1683 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1684
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001685 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001686
1687 r = -EIO;
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001688
1689 /* Read failed because firmware bug? */
1690 *retry = !!intr->read_regs_int_overridden;
1691 if (*retry)
Daniel Drakee85d0912006-06-02 17:11:32 +01001692 goto error_unlock;
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001693
1694 if (!check_read_regs(usb, req, count)) {
1695 dev_dbg_f(zd_usb_dev(usb), "error: invalid read regs\n");
Daniel Drakee85d0912006-06-02 17:11:32 +01001696 goto error_unlock;
1697 }
1698
1699 for (i = 0; i < count; i++) {
1700 struct reg_data *rd = &regs->regs[i];
Daniel Drakee85d0912006-06-02 17:11:32 +01001701 values[i] = le16_to_cpu(rd->value);
1702 }
1703
1704 r = 0;
1705error_unlock:
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001706 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001707 return r;
1708}
1709
1710int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1711 const zd_addr_t *addresses, unsigned int count)
1712{
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001713 int r, i, req_len, actual_req_len, try_count = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001714 struct usb_device *udev;
1715 struct usb_req_read_regs *req = NULL;
1716 unsigned long timeout;
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001717 bool retry = false;
Daniel Drakee85d0912006-06-02 17:11:32 +01001718
1719 if (count < 1) {
1720 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1721 return -EINVAL;
1722 }
1723 if (count > USB_MAX_IOREAD16_COUNT) {
1724 dev_dbg_f(zd_usb_dev(usb),
1725 "error: count %u exceeds possible max %u\n",
1726 count, USB_MAX_IOREAD16_COUNT);
1727 return -EINVAL;
1728 }
1729 if (in_atomic()) {
1730 dev_dbg_f(zd_usb_dev(usb),
1731 "error: io in atomic context not supported\n");
1732 return -EWOULDBLOCK;
1733 }
1734 if (!usb_int_enabled(usb)) {
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02001735 dev_dbg_f(zd_usb_dev(usb),
Daniel Drakee85d0912006-06-02 17:11:32 +01001736 "error: usb interrupt not enabled\n");
1737 return -EWOULDBLOCK;
1738 }
1739
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02001740 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1741 BUILD_BUG_ON(sizeof(struct usb_req_read_regs) + USB_MAX_IOREAD16_COUNT *
1742 sizeof(__le16) > sizeof(usb->req_buf));
1743 BUG_ON(sizeof(struct usb_req_read_regs) + count * sizeof(__le16) >
1744 sizeof(usb->req_buf));
1745
Daniel Drakee85d0912006-06-02 17:11:32 +01001746 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02001747 req = (void *)usb->req_buf;
1748
Daniel Drakee85d0912006-06-02 17:11:32 +01001749 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1750 for (i = 0; i < count; i++)
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001751 req->addr[i] = cpu_to_le16((u16)addresses[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +01001752
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001753retry_read:
1754 try_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001755 udev = zd_usb_to_usbdev(usb);
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001756 prepare_read_regs_int(usb, req, count);
Jussi Kivilinna59342f62011-05-30 10:15:47 +03001757 r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
Daniel Drakee85d0912006-06-02 17:11:32 +01001758 if (r) {
1759 dev_dbg_f(zd_usb_dev(usb),
Jussi Kivilinna59342f62011-05-30 10:15:47 +03001760 "error in zd_ep_regs_out_msg(). Error number %d\n", r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001761 goto error;
1762 }
1763 if (req_len != actual_req_len) {
Jussi Kivilinna59342f62011-05-30 10:15:47 +03001764 dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()\n"
Daniel Drakee85d0912006-06-02 17:11:32 +01001765 " req_len %d != actual_req_len %d\n",
1766 req_len, actual_req_len);
1767 r = -EIO;
1768 goto error;
1769 }
1770
1771 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
Jussi Kivilinna8f2d8f82011-01-31 20:49:33 +02001772 msecs_to_jiffies(50));
Daniel Drakee85d0912006-06-02 17:11:32 +01001773 if (!timeout) {
1774 disable_read_regs_int(usb);
1775 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1776 r = -ETIMEDOUT;
1777 goto error;
1778 }
1779
Jussi Kivilinnac900eff2011-06-20 14:42:44 +03001780 r = get_results(usb, values, req, count, &retry);
1781 if (retry && try_count < 20) {
1782 dev_dbg_f(zd_usb_dev(usb), "read retry, tries so far: %d\n",
1783 try_count);
1784 goto retry_read;
1785 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001786error:
Daniel Drakee85d0912006-06-02 17:11:32 +01001787 return r;
1788}
1789
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001790static void iowrite16v_urb_complete(struct urb *urb)
1791{
1792 struct zd_usb *usb = urb->context;
1793
1794 if (urb->status && !usb->cmd_error)
1795 usb->cmd_error = urb->status;
Jussi Kivilinna2fc713b2011-04-01 19:34:08 +03001796
1797 if (!usb->cmd_error &&
1798 urb->actual_length != urb->transfer_buffer_length)
1799 usb->cmd_error = -EIO;
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001800}
1801
1802static int zd_submit_waiting_urb(struct zd_usb *usb, bool last)
1803{
Jussi Kivilinna8662b252011-02-12 20:43:42 +02001804 int r = 0;
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001805 struct urb *urb = usb->urb_async_waiting;
1806
1807 if (!urb)
1808 return 0;
1809
1810 usb->urb_async_waiting = NULL;
1811
1812 if (!last)
1813 urb->transfer_flags |= URB_NO_INTERRUPT;
1814
1815 usb_anchor_urb(urb, &usb->submitted_cmds);
1816 r = usb_submit_urb(urb, GFP_KERNEL);
1817 if (r) {
1818 usb_unanchor_urb(urb);
1819 dev_dbg_f(zd_usb_dev(usb),
1820 "error in usb_submit_urb(). Error number %d\n", r);
1821 goto error;
1822 }
1823
1824 /* fall-through with r == 0 */
1825error:
1826 usb_free_urb(urb);
1827 return r;
1828}
1829
Jussi Kivilinna8662b252011-02-12 20:43:42 +02001830void zd_usb_iowrite16v_async_start(struct zd_usb *usb)
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001831{
1832 ZD_ASSERT(usb_anchor_empty(&usb->submitted_cmds));
1833 ZD_ASSERT(usb->urb_async_waiting == NULL);
1834 ZD_ASSERT(!usb->in_async);
1835
1836 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1837
1838 usb->in_async = 1;
1839 usb->cmd_error = 0;
1840 usb->urb_async_waiting = NULL;
1841}
1842
Jussi Kivilinna8662b252011-02-12 20:43:42 +02001843int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout)
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001844{
1845 int r;
1846
1847 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1848 ZD_ASSERT(usb->in_async);
1849
1850 /* Submit last iowrite16v URB */
1851 r = zd_submit_waiting_urb(usb, true);
1852 if (r) {
1853 dev_dbg_f(zd_usb_dev(usb),
1854 "error in zd_submit_waiting_usb(). "
1855 "Error number %d\n", r);
1856
1857 usb_kill_anchored_urbs(&usb->submitted_cmds);
1858 goto error;
1859 }
1860
1861 if (timeout)
1862 timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds,
1863 timeout);
1864 if (!timeout) {
1865 usb_kill_anchored_urbs(&usb->submitted_cmds);
1866 if (usb->cmd_error == -ENOENT) {
1867 dev_dbg_f(zd_usb_dev(usb), "timed out");
1868 r = -ETIMEDOUT;
1869 goto error;
1870 }
1871 }
1872
1873 r = usb->cmd_error;
1874error:
1875 usb->in_async = 0;
1876 return r;
1877}
1878
Jussi Kivilinna8662b252011-02-12 20:43:42 +02001879int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1880 unsigned int count)
Daniel Drakee85d0912006-06-02 17:11:32 +01001881{
1882 int r;
1883 struct usb_device *udev;
1884 struct usb_req_write_regs *req = NULL;
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001885 int i, req_len;
1886 struct urb *urb;
1887 struct usb_host_endpoint *ep;
1888
1889 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1890 ZD_ASSERT(usb->in_async);
Daniel Drakee85d0912006-06-02 17:11:32 +01001891
1892 if (count == 0)
1893 return 0;
1894 if (count > USB_MAX_IOWRITE16_COUNT) {
1895 dev_dbg_f(zd_usb_dev(usb),
1896 "error: count %u exceeds possible max %u\n",
1897 count, USB_MAX_IOWRITE16_COUNT);
1898 return -EINVAL;
1899 }
1900 if (in_atomic()) {
1901 dev_dbg_f(zd_usb_dev(usb),
1902 "error: io in atomic context not supported\n");
1903 return -EWOULDBLOCK;
1904 }
1905
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001906 udev = zd_usb_to_usbdev(usb);
1907
1908 ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev, EP_REGS_OUT));
1909 if (!ep)
1910 return -ENOENT;
1911
1912 urb = usb_alloc_urb(0, GFP_KERNEL);
1913 if (!urb)
1914 return -ENOMEM;
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02001915
Daniel Drakee85d0912006-06-02 17:11:32 +01001916 req_len = sizeof(struct usb_req_write_regs) +
1917 count * sizeof(struct reg_data);
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001918 req = kmalloc(req_len, GFP_KERNEL);
1919 if (!req) {
1920 r = -ENOMEM;
1921 goto error;
1922 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001923
1924 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1925 for (i = 0; i < count; i++) {
1926 struct reg_data *rw = &req->reg_writes[i];
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001927 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
Daniel Drakee85d0912006-06-02 17:11:32 +01001928 rw->value = cpu_to_le16(ioreqs[i].value);
1929 }
1930
Jussi Kivilinna59342f62011-05-30 10:15:47 +03001931 /* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode
1932 * endpoint is bulk. Select correct type URB by endpoint descriptor.
1933 */
1934 if (usb_endpoint_xfer_int(&ep->desc))
1935 usb_fill_int_urb(urb, udev, usb_sndintpipe(udev, EP_REGS_OUT),
1936 req, req_len, iowrite16v_urb_complete, usb,
1937 ep->desc.bInterval);
1938 else
1939 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1940 req, req_len, iowrite16v_urb_complete, usb);
1941
Jussi Kivilinna2fc713b2011-04-01 19:34:08 +03001942 urb->transfer_flags |= URB_FREE_BUFFER;
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001943
1944 /* Submit previous URB */
1945 r = zd_submit_waiting_urb(usb, false);
Daniel Drakee85d0912006-06-02 17:11:32 +01001946 if (r) {
1947 dev_dbg_f(zd_usb_dev(usb),
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001948 "error in zd_submit_waiting_usb(). "
1949 "Error number %d\n", r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001950 goto error;
1951 }
1952
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001953 /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
1954 * of currect batch except for very last.
1955 */
1956 usb->urb_async_waiting = urb;
1957 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001958error:
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001959 usb_free_urb(urb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001960 return r;
1961}
1962
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +02001963int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1964 unsigned int count)
1965{
1966 int r;
1967
1968 zd_usb_iowrite16v_async_start(usb);
1969 r = zd_usb_iowrite16v_async(usb, ioreqs, count);
1970 if (r) {
1971 zd_usb_iowrite16v_async_end(usb, 0);
1972 return r;
1973 }
1974 return zd_usb_iowrite16v_async_end(usb, 50 /* ms */);
1975}
1976
Daniel Drakee85d0912006-06-02 17:11:32 +01001977int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1978{
1979 int r;
1980 struct usb_device *udev;
1981 struct usb_req_rfwrite *req = NULL;
1982 int i, req_len, actual_req_len;
1983 u16 bit_value_template;
1984
1985 if (in_atomic()) {
1986 dev_dbg_f(zd_usb_dev(usb),
1987 "error: io in atomic context not supported\n");
1988 return -EWOULDBLOCK;
1989 }
1990 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1991 dev_dbg_f(zd_usb_dev(usb),
1992 "error: bits %d are smaller than"
1993 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1994 bits, USB_MIN_RFWRITE_BIT_COUNT);
1995 return -EINVAL;
1996 }
1997 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1998 dev_dbg_f(zd_usb_dev(usb),
1999 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
2000 bits, USB_MAX_RFWRITE_BIT_COUNT);
2001 return -EINVAL;
2002 }
2003#ifdef DEBUG
2004 if (value & (~0UL << bits)) {
2005 dev_dbg_f(zd_usb_dev(usb),
2006 "error: value %#09x has bits >= %d set\n",
2007 value, bits);
2008 return -EINVAL;
2009 }
2010#endif /* DEBUG */
2011
2012 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
2013
Jussi Kivilinnafbd5d172011-04-02 11:25:54 +03002014 r = zd_usb_ioread16(usb, &bit_value_template, ZD_CR203);
Daniel Drakee85d0912006-06-02 17:11:32 +01002015 if (r) {
2016 dev_dbg_f(zd_usb_dev(usb),
Jussi Kivilinnafbd5d172011-04-02 11:25:54 +03002017 "error %d: Couldn't read ZD_CR203\n", r);
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02002018 return r;
Daniel Drakee85d0912006-06-02 17:11:32 +01002019 }
2020 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
2021
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02002022 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
2023 BUILD_BUG_ON(sizeof(struct usb_req_rfwrite) +
2024 USB_MAX_RFWRITE_BIT_COUNT * sizeof(__le16) >
2025 sizeof(usb->req_buf));
2026 BUG_ON(sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16) >
2027 sizeof(usb->req_buf));
2028
Daniel Drakee85d0912006-06-02 17:11:32 +01002029 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +02002030 req = (void *)usb->req_buf;
Daniel Drakee85d0912006-06-02 17:11:32 +01002031
2032 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
2033 /* 1: 3683a, but not used in ZYDAS driver */
2034 req->value = cpu_to_le16(2);
2035 req->bits = cpu_to_le16(bits);
2036
2037 for (i = 0; i < bits; i++) {
2038 u16 bv = bit_value_template;
2039 if (value & (1 << (bits-1-i)))
2040 bv |= RF_DATA;
2041 req->bit_values[i] = cpu_to_le16(bv);
2042 }
2043
2044 udev = zd_usb_to_usbdev(usb);
Jussi Kivilinna59342f62011-05-30 10:15:47 +03002045 r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
Daniel Drakee85d0912006-06-02 17:11:32 +01002046 if (r) {
2047 dev_dbg_f(zd_usb_dev(usb),
Jussi Kivilinna59342f62011-05-30 10:15:47 +03002048 "error in zd_ep_regs_out_msg(). Error number %d\n", r);
Daniel Drakee85d0912006-06-02 17:11:32 +01002049 goto out;
2050 }
2051 if (req_len != actual_req_len) {
Jussi Kivilinna59342f62011-05-30 10:15:47 +03002052 dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()"
Daniel Drakee85d0912006-06-02 17:11:32 +01002053 " req_len %d != actual_req_len %d\n",
2054 req_len, actual_req_len);
2055 r = -EIO;
2056 goto out;
2057 }
2058
2059 /* FALL-THROUGH with r == 0 */
2060out:
Daniel Drakee85d0912006-06-02 17:11:32 +01002061 return r;
2062}