blob: 6fbc4ad16cfc091b6fd2afd6df484bf84558fc7b [file] [log] [blame]
Daniel Drakee85d0912006-06-02 17:11:32 +01001/* zd_usb.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
Daniel Draked066c212006-08-12 17:59:59 +010018#include <linux/kernel.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010019#include <linux/init.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010020#include <linux/firmware.h>
21#include <linux/device.h>
22#include <linux/errno.h>
23#include <linux/skbuff.h>
24#include <linux/usb.h>
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +010025#include <linux/workqueue.h>
Daniel Drake459c51a2007-11-19 15:00:29 +000026#include <net/mac80211.h>
Al Viroc2487252007-07-15 21:00:21 +010027#include <asm/unaligned.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010028
29#include "zd_def.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010030#include "zd_mac.h"
31#include "zd_usb.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010032
33static struct usb_device_id usb_ids[] = {
34 /* ZD1211 */
35 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
36 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
37 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
38 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
39 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010040 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010041 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
Daniel Drakedd2f5532006-07-05 15:57:31 +010042 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
Daniel Drake1b865492006-08-12 17:59:50 +010043 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
Daniel Drake12f39302006-08-12 17:59:51 +010044 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
Daniel Drake4ceb7e92006-08-12 18:00:03 +010045 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
Daniel Drakefca27142006-08-29 23:49:32 +010046 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
Daniel Drakefc3e39b2006-08-29 23:49:53 +010047 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
Daniel Drakef2423722006-09-26 04:04:38 +010048 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
49 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
Daniel Drakea362bf52006-10-18 00:17:02 +010051 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
Daniel Drake229782a2006-11-17 00:13:48 +000052 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
Daniel Drakea3fa3ab2007-03-26 01:00:12 +010053 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
Daniel Drakea9eea9a2007-05-24 01:06:29 +010054 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
Ulrich Kunitzababda02007-09-01 22:40:32 +010055 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
Daniel Drake269fca02007-11-19 15:29:24 +000056 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010057 /* ZD1211B */
58 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
59 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
Daniel Drakedd2f5532006-07-05 15:57:31 +010060 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz943599e2006-08-12 18:00:05 +010061 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e872952006-11-17 00:26:40 +000062 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
Daniel Drakea2bdcc62006-12-12 01:25:37 +000063 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
64 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
Daniel Drake33218ba2006-12-30 22:38:23 +000065 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
Daniel Drake2e9b2462007-01-04 03:33:54 +000066 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
Daniel Drakeaec91022007-03-11 19:54:39 +000067 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
68 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
S.Çağlar Onurc445a312007-04-22 00:52:48 +030069 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
Daniel Drake5d082fe2007-04-07 16:00:43 +010070 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
Daniel Drake187bb412007-05-01 17:14:51 +010071 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz019a6752007-05-01 17:13:51 +010072 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
Daniel Drake93f510b2007-07-01 18:22:21 +010073 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
Masakazu Mokuno6eb07252007-07-14 01:40:11 +010074 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
Daniel Drake25343462007-07-14 01:40:54 +010075 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e97afe2007-08-06 01:24:19 +010076 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
Daniel Drake475fed12007-08-06 02:25:18 +010077 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
Daniel Drake61ef6062007-10-07 16:24:26 +010078 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
Daniel Drakea1030e92006-08-13 12:15:29 +010079 /* "Driverless" devices that need ejecting */
80 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010081 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
Daniel Drakee85d0912006-06-02 17:11:32 +010082 {}
83};
84
85MODULE_LICENSE("GPL");
86MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
87MODULE_AUTHOR("Ulrich Kunitz");
88MODULE_AUTHOR("Daniel Drake");
89MODULE_VERSION("1.0");
90MODULE_DEVICE_TABLE(usb, usb_ids);
91
92#define FW_ZD1211_PREFIX "zd1211/zd1211_"
93#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
94
Daniel Drakee85d0912006-06-02 17:11:32 +010095/* USB device initialization */
96
97static int request_fw_file(
98 const struct firmware **fw, const char *name, struct device *device)
99{
100 int r;
101
102 dev_dbg_f(device, "fw name %s\n", name);
103
104 r = request_firmware(fw, name, device);
105 if (r)
106 dev_err(device,
107 "Could not load firmware file %s. Error number %d\n",
108 name, r);
109 return r;
110}
111
112static inline u16 get_bcdDevice(const struct usb_device *udev)
113{
114 return le16_to_cpu(udev->descriptor.bcdDevice);
115}
116
117enum upload_code_flags {
118 REBOOT = 1,
119};
120
121/* Ensures that MAX_TRANSFER_SIZE is even. */
122#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
123
124static int upload_code(struct usb_device *udev,
125 const u8 *data, size_t size, u16 code_offset, int flags)
126{
127 u8 *p;
128 int r;
129
130 /* USB request blocks need "kmalloced" buffers.
131 */
132 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
133 if (!p) {
134 dev_err(&udev->dev, "out of memory\n");
135 r = -ENOMEM;
136 goto error;
137 }
138
139 size &= ~1;
140 while (size > 0) {
141 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
142 size : MAX_TRANSFER_SIZE;
143
144 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
145
146 memcpy(p, data, transfer_size);
147 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
148 USB_REQ_FIRMWARE_DOWNLOAD,
149 USB_DIR_OUT | USB_TYPE_VENDOR,
150 code_offset, 0, p, transfer_size, 1000 /* ms */);
151 if (r < 0) {
152 dev_err(&udev->dev,
153 "USB control request for firmware upload"
154 " failed. Error number %d\n", r);
155 goto error;
156 }
157 transfer_size = r & ~1;
158
159 size -= transfer_size;
160 data += transfer_size;
161 code_offset += transfer_size/sizeof(u16);
162 }
163
164 if (flags & REBOOT) {
165 u8 ret;
166
167 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
168 USB_REQ_FIRMWARE_CONFIRM,
169 USB_DIR_IN | USB_TYPE_VENDOR,
170 0, 0, &ret, sizeof(ret), 5000 /* ms */);
171 if (r != sizeof(ret)) {
172 dev_err(&udev->dev,
173 "control request firmeware confirmation failed."
174 " Return value %d\n", r);
175 if (r >= 0)
176 r = -ENODEV;
177 goto error;
178 }
179 if (ret & 0x80) {
180 dev_err(&udev->dev,
181 "Internal error while downloading."
182 " Firmware confirm return value %#04x\n",
183 (unsigned int)ret);
184 r = -ENODEV;
185 goto error;
186 }
187 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
188 (unsigned int)ret);
189 }
190
191 r = 0;
192error:
193 kfree(p);
194 return r;
195}
196
197static u16 get_word(const void *data, u16 offset)
198{
199 const __le16 *p = data;
200 return le16_to_cpu(p[offset]);
201}
202
Daniel Drake74553ae2007-07-01 18:22:32 +0100203static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
Daniel Drakee85d0912006-06-02 17:11:32 +0100204 const char* postfix)
205{
206 scnprintf(buffer, size, "%s%s",
Daniel Drake74553ae2007-07-01 18:22:32 +0100207 usb->is_zd1211b ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100208 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
209 postfix);
210 return buffer;
211}
212
Daniel Drake74553ae2007-07-01 18:22:32 +0100213static int handle_version_mismatch(struct zd_usb *usb,
Daniel Draked066c212006-08-12 17:59:59 +0100214 const struct firmware *ub_fw)
215{
Daniel Drake74553ae2007-07-01 18:22:32 +0100216 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Draked066c212006-08-12 17:59:59 +0100217 const struct firmware *ur_fw = NULL;
218 int offset;
219 int r = 0;
220 char fw_name[128];
221
222 r = request_fw_file(&ur_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100223 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
Daniel Draked066c212006-08-12 17:59:59 +0100224 &udev->dev);
225 if (r)
226 goto error;
227
Daniel Drakeee302762006-12-12 01:25:52 +0000228 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100229 if (r)
230 goto error;
231
Daniel Drakeee302762006-12-12 01:25:52 +0000232 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
Daniel Draked066c212006-08-12 17:59:59 +0100233 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
Daniel Drakeee302762006-12-12 01:25:52 +0000234 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100235
236 /* At this point, the vendor driver downloads the whole firmware
237 * image, hacks around with version IDs, and uploads it again,
238 * completely overwriting the boot code. We do not do this here as
239 * it is not required on any tested devices, and it is suspected to
240 * cause problems. */
241error:
242 release_firmware(ur_fw);
243 return r;
244}
245
Daniel Drake74553ae2007-07-01 18:22:32 +0100246static int upload_firmware(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100247{
248 int r;
249 u16 fw_bcdDevice;
250 u16 bcdDevice;
Daniel Drake74553ae2007-07-01 18:22:32 +0100251 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100252 const struct firmware *ub_fw = NULL;
253 const struct firmware *uph_fw = NULL;
254 char fw_name[128];
255
256 bcdDevice = get_bcdDevice(udev);
257
258 r = request_fw_file(&ub_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100259 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100260 &udev->dev);
261 if (r)
262 goto error;
263
Daniel Drakeee302762006-12-12 01:25:52 +0000264 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
Daniel Drakee85d0912006-06-02 17:11:32 +0100265
Daniel Drakee85d0912006-06-02 17:11:32 +0100266 if (fw_bcdDevice != bcdDevice) {
267 dev_info(&udev->dev,
Daniel Draked066c212006-08-12 17:59:59 +0100268 "firmware version %#06x and device bootcode version "
269 "%#06x differ\n", fw_bcdDevice, bcdDevice);
270 if (bcdDevice <= 0x4313)
271 dev_warn(&udev->dev, "device has old bootcode, please "
272 "report success or failure\n");
273
Daniel Drake74553ae2007-07-01 18:22:32 +0100274 r = handle_version_mismatch(usb, ub_fw);
Daniel Draked066c212006-08-12 17:59:59 +0100275 if (r)
276 goto error;
Daniel Drakee85d0912006-06-02 17:11:32 +0100277 } else {
278 dev_dbg_f(&udev->dev,
279 "firmware device id %#06x is equal to the "
280 "actual device id\n", fw_bcdDevice);
281 }
282
283
284 r = request_fw_file(&uph_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100285 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
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 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100291 if (r) {
292 dev_err(&udev->dev,
293 "Could not upload firmware code uph. Error number %d\n",
294 r);
295 }
296
297 /* FALL-THROUGH */
298error:
299 release_firmware(ub_fw);
300 release_firmware(uph_fw);
301 return r;
302}
303
Daniel Drake74553ae2007-07-01 18:22:32 +0100304/* Read data from device address space using "firmware interface" which does
305 * not require firmware to be loaded. */
306int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
307{
308 int r;
309 struct usb_device *udev = zd_usb_to_usbdev(usb);
310
311 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
312 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
313 data, len, 5000);
314 if (r < 0) {
315 dev_err(&udev->dev,
316 "read over firmware interface failed: %d\n", r);
317 return r;
318 } else if (r != len) {
319 dev_err(&udev->dev,
320 "incomplete read over firmware interface: %d/%d\n",
321 r, len);
322 return -EIO;
323 }
324
325 return 0;
326}
327
Daniel Drakee85d0912006-06-02 17:11:32 +0100328#define urb_dev(urb) (&(urb)->dev->dev)
329
330static inline void handle_regs_int(struct urb *urb)
331{
332 struct zd_usb *usb = urb->context;
333 struct zd_usb_interrupt *intr = &usb->intr;
334 int len;
335
336 ZD_ASSERT(in_interrupt());
337 spin_lock(&intr->lock);
338
339 if (intr->read_regs_enabled) {
340 intr->read_regs.length = len = urb->actual_length;
341
342 if (len > sizeof(intr->read_regs.buffer))
343 len = sizeof(intr->read_regs.buffer);
344 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
345 intr->read_regs_enabled = 0;
346 complete(&intr->read_regs.completion);
347 goto out;
348 }
349
350 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
351out:
352 spin_unlock(&intr->lock);
353}
354
David Howells7d12e782006-10-05 14:55:46 +0100355static void int_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100356{
357 int r;
358 struct usb_int_header *hdr;
359
360 switch (urb->status) {
361 case 0:
362 break;
363 case -ESHUTDOWN:
364 case -EINVAL:
365 case -ENODEV:
366 case -ENOENT:
367 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100368 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100369 goto kfree;
Daniel Drakee85d0912006-06-02 17:11:32 +0100370 default:
371 goto resubmit;
372 }
373
374 if (urb->actual_length < sizeof(hdr)) {
375 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
376 goto resubmit;
377 }
378
379 hdr = urb->transfer_buffer;
380 if (hdr->type != USB_INT_TYPE) {
381 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
382 goto resubmit;
383 }
384
385 switch (hdr->id) {
386 case USB_INT_ID_REGS:
387 handle_regs_int(urb);
388 break;
389 case USB_INT_ID_RETRY_FAILED:
Daniel Drake459c51a2007-11-19 15:00:29 +0000390 zd_mac_tx_failed(zd_usb_to_hw(urb->context));
Daniel Drakee85d0912006-06-02 17:11:32 +0100391 break;
392 default:
393 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
394 (unsigned int)hdr->id);
395 goto resubmit;
396 }
397
398resubmit:
399 r = usb_submit_urb(urb, GFP_ATOMIC);
400 if (r) {
401 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
402 goto kfree;
403 }
404 return;
405kfree:
406 kfree(urb->transfer_buffer);
407}
408
409static inline int int_urb_interval(struct usb_device *udev)
410{
411 switch (udev->speed) {
412 case USB_SPEED_HIGH:
413 return 4;
414 case USB_SPEED_LOW:
415 return 10;
416 case USB_SPEED_FULL:
417 default:
418 return 1;
419 }
420}
421
422static inline int usb_int_enabled(struct zd_usb *usb)
423{
424 unsigned long flags;
425 struct zd_usb_interrupt *intr = &usb->intr;
426 struct urb *urb;
427
428 spin_lock_irqsave(&intr->lock, flags);
429 urb = intr->urb;
430 spin_unlock_irqrestore(&intr->lock, flags);
431 return urb != NULL;
432}
433
434int zd_usb_enable_int(struct zd_usb *usb)
435{
436 int r;
437 struct usb_device *udev;
438 struct zd_usb_interrupt *intr = &usb->intr;
439 void *transfer_buffer = NULL;
440 struct urb *urb;
441
442 dev_dbg_f(zd_usb_dev(usb), "\n");
443
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000444 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100445 if (!urb) {
446 r = -ENOMEM;
447 goto out;
448 }
449
450 ZD_ASSERT(!irqs_disabled());
451 spin_lock_irq(&intr->lock);
452 if (intr->urb) {
453 spin_unlock_irq(&intr->lock);
454 r = 0;
455 goto error_free_urb;
456 }
457 intr->urb = urb;
458 spin_unlock_irq(&intr->lock);
459
460 /* TODO: make it a DMA buffer */
461 r = -ENOMEM;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000462 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100463 if (!transfer_buffer) {
464 dev_dbg_f(zd_usb_dev(usb),
465 "couldn't allocate transfer_buffer\n");
466 goto error_set_urb_null;
467 }
468
469 udev = zd_usb_to_usbdev(usb);
470 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
471 transfer_buffer, USB_MAX_EP_INT_BUFFER,
472 int_urb_complete, usb,
473 intr->interval);
474
475 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000476 r = usb_submit_urb(urb, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100477 if (r) {
478 dev_dbg_f(zd_usb_dev(usb),
479 "Couldn't submit urb. Error number %d\n", r);
480 goto error;
481 }
482
483 return 0;
484error:
485 kfree(transfer_buffer);
486error_set_urb_null:
487 spin_lock_irq(&intr->lock);
488 intr->urb = NULL;
489 spin_unlock_irq(&intr->lock);
490error_free_urb:
491 usb_free_urb(urb);
492out:
493 return r;
494}
495
496void zd_usb_disable_int(struct zd_usb *usb)
497{
498 unsigned long flags;
499 struct zd_usb_interrupt *intr = &usb->intr;
500 struct urb *urb;
501
502 spin_lock_irqsave(&intr->lock, flags);
503 urb = intr->urb;
504 if (!urb) {
505 spin_unlock_irqrestore(&intr->lock, flags);
506 return;
507 }
508 intr->urb = NULL;
509 spin_unlock_irqrestore(&intr->lock, flags);
510
511 usb_kill_urb(urb);
512 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
513 usb_free_urb(urb);
514}
515
516static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
517 unsigned int length)
518{
519 int i;
Daniel Drakee85d0912006-06-02 17:11:32 +0100520 const struct rx_length_info *length_info;
521
522 if (length < sizeof(struct rx_length_info)) {
523 /* It's not a complete packet anyhow. */
524 return;
525 }
526 length_info = (struct rx_length_info *)
527 (buffer + length - sizeof(struct rx_length_info));
528
529 /* It might be that three frames are merged into a single URB
530 * transaction. We have to check for the length info tag.
531 *
532 * While testing we discovered that length_info might be unaligned,
533 * because if USB transactions are merged, the last packet will not
534 * be padded. Unaligned access might also happen if the length_info
535 * structure is not present.
536 */
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200537 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
538 {
Daniel Drakee85d0912006-06-02 17:11:32 +0100539 unsigned int l, k, n;
540 for (i = 0, l = 0;; i++) {
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200541 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
Ulrich Kunitz850c2112006-11-22 00:06:19 +0000542 if (k == 0)
543 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100544 n = l+k;
545 if (n > length)
546 return;
Daniel Drake459c51a2007-11-19 15:00:29 +0000547 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
Daniel Drakee85d0912006-06-02 17:11:32 +0100548 if (i >= 2)
549 return;
550 l = (n+3) & ~3;
551 }
552 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000553 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100554 }
555}
556
David Howells7d12e782006-10-05 14:55:46 +0100557static void rx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100558{
559 struct zd_usb *usb;
560 struct zd_usb_rx *rx;
561 const u8 *buffer;
562 unsigned int length;
563
564 switch (urb->status) {
565 case 0:
566 break;
567 case -ESHUTDOWN:
568 case -EINVAL:
569 case -ENODEV:
570 case -ENOENT:
571 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100572 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100573 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100574 default:
575 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
576 goto resubmit;
577 }
578
579 buffer = urb->transfer_buffer;
580 length = urb->actual_length;
581 usb = urb->context;
582 rx = &usb->rx;
583
584 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
585 /* If there is an old first fragment, we don't care. */
586 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
587 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
588 spin_lock(&rx->lock);
589 memcpy(rx->fragment, buffer, length);
590 rx->fragment_length = length;
591 spin_unlock(&rx->lock);
592 goto resubmit;
593 }
594
595 spin_lock(&rx->lock);
596 if (rx->fragment_length > 0) {
597 /* We are on a second fragment, we believe */
598 ZD_ASSERT(length + rx->fragment_length <=
599 ARRAY_SIZE(rx->fragment));
600 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
601 memcpy(rx->fragment+rx->fragment_length, buffer, length);
602 handle_rx_packet(usb, rx->fragment,
603 rx->fragment_length + length);
604 rx->fragment_length = 0;
605 spin_unlock(&rx->lock);
606 } else {
607 spin_unlock(&rx->lock);
608 handle_rx_packet(usb, buffer, length);
609 }
610
611resubmit:
612 usb_submit_urb(urb, GFP_ATOMIC);
613}
614
Daniel Drake459c51a2007-11-19 15:00:29 +0000615static struct urb *alloc_rx_urb(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100616{
617 struct usb_device *udev = zd_usb_to_usbdev(usb);
618 struct urb *urb;
619 void *buffer;
620
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000621 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100622 if (!urb)
623 return NULL;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000624 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
Daniel Drakee85d0912006-06-02 17:11:32 +0100625 &urb->transfer_dma);
626 if (!buffer) {
627 usb_free_urb(urb);
628 return NULL;
629 }
630
631 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
632 buffer, USB_MAX_RX_SIZE,
633 rx_urb_complete, usb);
634 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
635
636 return urb;
637}
638
Daniel Drake459c51a2007-11-19 15:00:29 +0000639static void free_rx_urb(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100640{
641 if (!urb)
642 return;
643 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
644 urb->transfer_buffer, urb->transfer_dma);
645 usb_free_urb(urb);
646}
647
648int zd_usb_enable_rx(struct zd_usb *usb)
649{
650 int i, r;
651 struct zd_usb_rx *rx = &usb->rx;
652 struct urb **urbs;
653
654 dev_dbg_f(zd_usb_dev(usb), "\n");
655
656 r = -ENOMEM;
Daniel Drake459c51a2007-11-19 15:00:29 +0000657 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100658 if (!urbs)
659 goto error;
Daniel Drake459c51a2007-11-19 15:00:29 +0000660 for (i = 0; i < RX_URBS_COUNT; i++) {
661 urbs[i] = alloc_rx_urb(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100662 if (!urbs[i])
663 goto error;
664 }
665
666 ZD_ASSERT(!irqs_disabled());
667 spin_lock_irq(&rx->lock);
668 if (rx->urbs) {
669 spin_unlock_irq(&rx->lock);
670 r = 0;
671 goto error;
672 }
673 rx->urbs = urbs;
Daniel Drake459c51a2007-11-19 15:00:29 +0000674 rx->urbs_count = RX_URBS_COUNT;
Daniel Drakee85d0912006-06-02 17:11:32 +0100675 spin_unlock_irq(&rx->lock);
676
Daniel Drake459c51a2007-11-19 15:00:29 +0000677 for (i = 0; i < RX_URBS_COUNT; i++) {
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000678 r = usb_submit_urb(urbs[i], GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100679 if (r)
680 goto error_submit;
681 }
682
683 return 0;
684error_submit:
Daniel Drake459c51a2007-11-19 15:00:29 +0000685 for (i = 0; i < RX_URBS_COUNT; i++) {
Daniel Drakee85d0912006-06-02 17:11:32 +0100686 usb_kill_urb(urbs[i]);
687 }
688 spin_lock_irq(&rx->lock);
689 rx->urbs = NULL;
690 rx->urbs_count = 0;
691 spin_unlock_irq(&rx->lock);
692error:
693 if (urbs) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000694 for (i = 0; i < RX_URBS_COUNT; i++)
695 free_rx_urb(urbs[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +0100696 }
697 return r;
698}
699
700void zd_usb_disable_rx(struct zd_usb *usb)
701{
702 int i;
703 unsigned long flags;
704 struct urb **urbs;
705 unsigned int count;
706 struct zd_usb_rx *rx = &usb->rx;
707
708 spin_lock_irqsave(&rx->lock, flags);
709 urbs = rx->urbs;
710 count = rx->urbs_count;
711 spin_unlock_irqrestore(&rx->lock, flags);
712 if (!urbs)
713 return;
714
715 for (i = 0; i < count; i++) {
716 usb_kill_urb(urbs[i]);
Daniel Drake459c51a2007-11-19 15:00:29 +0000717 free_rx_urb(urbs[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +0100718 }
719 kfree(urbs);
720
721 spin_lock_irqsave(&rx->lock, flags);
722 rx->urbs = NULL;
723 rx->urbs_count = 0;
724 spin_unlock_irqrestore(&rx->lock, flags);
725}
726
Daniel Drake459c51a2007-11-19 15:00:29 +0000727/**
728 * zd_usb_disable_tx - disable transmission
729 * @usb: the zd1211rw-private USB structure
730 *
731 * Frees all URBs in the free list and marks the transmission as disabled.
732 */
733void zd_usb_disable_tx(struct zd_usb *usb)
734{
735 struct zd_usb_tx *tx = &usb->tx;
736 unsigned long flags;
737 struct list_head *pos, *n;
738
739 spin_lock_irqsave(&tx->lock, flags);
740 list_for_each_safe(pos, n, &tx->free_urb_list) {
741 list_del(pos);
742 usb_free_urb(list_entry(pos, struct urb, urb_list));
743 }
744 tx->enabled = 0;
745 tx->submitted_urbs = 0;
746 /* The stopped state is ignored, relying on ieee80211_wake_queues()
747 * in a potentionally following zd_usb_enable_tx().
748 */
749 spin_unlock_irqrestore(&tx->lock, flags);
750}
751
752/**
753 * zd_usb_enable_tx - enables transmission
754 * @usb: a &struct zd_usb pointer
755 *
756 * This function enables transmission and prepares the &zd_usb_tx data
757 * structure.
758 */
759void zd_usb_enable_tx(struct zd_usb *usb)
760{
761 unsigned long flags;
762 struct zd_usb_tx *tx = &usb->tx;
763
764 spin_lock_irqsave(&tx->lock, flags);
765 tx->enabled = 1;
766 tx->submitted_urbs = 0;
767 ieee80211_wake_queues(zd_usb_to_hw(usb));
768 tx->stopped = 0;
769 spin_unlock_irqrestore(&tx->lock, flags);
770}
771
772/**
773 * alloc_tx_urb - provides an tx URB
774 * @usb: a &struct zd_usb pointer
775 *
776 * Allocates a new URB. If possible takes the urb from the free list in
777 * usb->tx.
778 */
779static struct urb *alloc_tx_urb(struct zd_usb *usb)
780{
781 struct zd_usb_tx *tx = &usb->tx;
782 unsigned long flags;
783 struct list_head *entry;
784 struct urb *urb;
785
786 spin_lock_irqsave(&tx->lock, flags);
787 if (list_empty(&tx->free_urb_list)) {
788 urb = usb_alloc_urb(0, GFP_ATOMIC);
789 goto out;
790 }
791 entry = tx->free_urb_list.next;
792 list_del(entry);
793 urb = list_entry(entry, struct urb, urb_list);
794out:
795 spin_unlock_irqrestore(&tx->lock, flags);
796 return urb;
797}
798
799/**
800 * free_tx_urb - frees a used tx URB
801 * @usb: a &struct zd_usb pointer
802 * @urb: URB to be freed
803 *
804 * Frees the the transmission URB, which means to put it on the free URB
805 * list.
806 */
807static void free_tx_urb(struct zd_usb *usb, struct urb *urb)
808{
809 struct zd_usb_tx *tx = &usb->tx;
810 unsigned long flags;
811
812 spin_lock_irqsave(&tx->lock, flags);
813 if (!tx->enabled) {
814 usb_free_urb(urb);
815 goto out;
816 }
817 list_add(&urb->urb_list, &tx->free_urb_list);
818out:
819 spin_unlock_irqrestore(&tx->lock, flags);
820}
821
822static void tx_dec_submitted_urbs(struct zd_usb *usb)
823{
824 struct zd_usb_tx *tx = &usb->tx;
825 unsigned long flags;
826
827 spin_lock_irqsave(&tx->lock, flags);
828 --tx->submitted_urbs;
829 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
830 ieee80211_wake_queues(zd_usb_to_hw(usb));
831 tx->stopped = 0;
832 }
833 spin_unlock_irqrestore(&tx->lock, flags);
834}
835
836static void tx_inc_submitted_urbs(struct zd_usb *usb)
837{
838 struct zd_usb_tx *tx = &usb->tx;
839 unsigned long flags;
840
841 spin_lock_irqsave(&tx->lock, flags);
842 ++tx->submitted_urbs;
843 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
844 ieee80211_stop_queues(zd_usb_to_hw(usb));
845 tx->stopped = 1;
846 }
847 spin_unlock_irqrestore(&tx->lock, flags);
848}
849
850/**
851 * tx_urb_complete - completes the execution of an URB
852 * @urb: a URB
853 *
854 * This function is called if the URB has been transferred to a device or an
855 * error has happened.
856 */
David Howells7d12e782006-10-05 14:55:46 +0100857static void tx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100858{
859 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000860 struct sk_buff *skb;
861 struct zd_tx_skb_control_block *cb;
862 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100863
864 switch (urb->status) {
865 case 0:
866 break;
867 case -ESHUTDOWN:
868 case -EINVAL:
869 case -ENODEV:
870 case -ENOENT:
871 case -ECONNRESET:
Daniel Drakeb312d792006-07-05 15:57:39 +0100872 case -EPIPE:
Daniel Drakee85d0912006-06-02 17:11:32 +0100873 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
874 break;
Daniel Drakee85d0912006-06-02 17:11:32 +0100875 default:
876 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
877 goto resubmit;
878 }
879free_urb:
Daniel Drake459c51a2007-11-19 15:00:29 +0000880 skb = (struct sk_buff *)urb->context;
881 zd_mac_tx_to_dev(skb, urb->status);
882 cb = (struct zd_tx_skb_control_block *)skb->cb;
883 usb = &zd_hw_mac(cb->hw)->chip.usb;
884 free_tx_urb(usb, urb);
885 tx_dec_submitted_urbs(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100886 return;
887resubmit:
888 r = usb_submit_urb(urb, GFP_ATOMIC);
889 if (r) {
890 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
891 goto free_urb;
892 }
893}
894
Daniel Drake459c51a2007-11-19 15:00:29 +0000895/**
896 * zd_usb_tx: initiates transfer of a frame of the device
897 *
898 * @usb: the zd1211rw-private USB structure
899 * @skb: a &struct sk_buff pointer
900 *
901 * This function tranmits a frame to the device. It doesn't wait for
902 * completion. The frame must contain the control set and have all the
903 * control set information available.
904 *
905 * The function returns 0 if the transfer has been successfully initiated.
Daniel Drakee85d0912006-06-02 17:11:32 +0100906 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000907int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100908{
909 int r;
910 struct usb_device *udev = zd_usb_to_usbdev(usb);
911 struct urb *urb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100912
Daniel Drake459c51a2007-11-19 15:00:29 +0000913 urb = alloc_tx_urb(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100914 if (!urb) {
915 r = -ENOMEM;
916 goto out;
917 }
918
Daniel Drakee85d0912006-06-02 17:11:32 +0100919 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
Daniel Drake459c51a2007-11-19 15:00:29 +0000920 skb->data, skb->len, tx_urb_complete, skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100921
922 r = usb_submit_urb(urb, GFP_ATOMIC);
923 if (r)
924 goto error;
Daniel Drake459c51a2007-11-19 15:00:29 +0000925 tx_inc_submitted_urbs(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100926 return 0;
927error:
Daniel Drake459c51a2007-11-19 15:00:29 +0000928 free_tx_urb(usb, urb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100929out:
930 return r;
931}
932
933static inline void init_usb_interrupt(struct zd_usb *usb)
934{
935 struct zd_usb_interrupt *intr = &usb->intr;
936
937 spin_lock_init(&intr->lock);
938 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
939 init_completion(&intr->read_regs.completion);
Daniel Drake0ce34bc2006-12-12 01:26:11 +0000940 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100941}
942
943static inline void init_usb_rx(struct zd_usb *usb)
944{
945 struct zd_usb_rx *rx = &usb->rx;
946 spin_lock_init(&rx->lock);
947 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
948 rx->usb_packet_size = 512;
949 } else {
950 rx->usb_packet_size = 64;
951 }
952 ZD_ASSERT(rx->fragment_length == 0);
953}
954
955static inline void init_usb_tx(struct zd_usb *usb)
956{
Daniel Drake459c51a2007-11-19 15:00:29 +0000957 struct zd_usb_tx *tx = &usb->tx;
958 spin_lock_init(&tx->lock);
959 tx->enabled = 0;
960 tx->stopped = 0;
961 INIT_LIST_HEAD(&tx->free_urb_list);
962 tx->submitted_urbs = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100963}
964
Daniel Drake459c51a2007-11-19 15:00:29 +0000965void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
Daniel Drakee85d0912006-06-02 17:11:32 +0100966 struct usb_interface *intf)
967{
968 memset(usb, 0, sizeof(*usb));
969 usb->intf = usb_get_intf(intf);
Daniel Drake459c51a2007-11-19 15:00:29 +0000970 usb_set_intfdata(usb->intf, hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100971 init_usb_interrupt(usb);
972 init_usb_tx(usb);
973 init_usb_rx(usb);
974}
975
Daniel Drakee85d0912006-06-02 17:11:32 +0100976void zd_usb_clear(struct zd_usb *usb)
977{
978 usb_set_intfdata(usb->intf, NULL);
979 usb_put_intf(usb->intf);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100980 ZD_MEMCLEAR(usb, sizeof(*usb));
Daniel Drakee85d0912006-06-02 17:11:32 +0100981 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
982}
983
984static const char *speed(enum usb_device_speed speed)
985{
986 switch (speed) {
987 case USB_SPEED_LOW:
988 return "low";
989 case USB_SPEED_FULL:
990 return "full";
991 case USB_SPEED_HIGH:
992 return "high";
993 default:
994 return "unknown speed";
995 }
996}
997
998static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
999{
1000 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1001 le16_to_cpu(udev->descriptor.idVendor),
1002 le16_to_cpu(udev->descriptor.idProduct),
1003 get_bcdDevice(udev),
1004 speed(udev->speed));
1005}
1006
1007int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1008{
1009 struct usb_device *udev = interface_to_usbdev(usb->intf);
1010 return scnprint_id(udev, buffer, size);
1011}
1012
1013#ifdef DEBUG
1014static void print_id(struct usb_device *udev)
1015{
1016 char buffer[40];
1017
1018 scnprint_id(udev, buffer, sizeof(buffer));
1019 buffer[sizeof(buffer)-1] = 0;
1020 dev_dbg_f(&udev->dev, "%s\n", buffer);
1021}
1022#else
1023#define print_id(udev) do { } while (0)
1024#endif
1025
Daniel Drakea1030e92006-08-13 12:15:29 +01001026static int eject_installer(struct usb_interface *intf)
1027{
1028 struct usb_device *udev = interface_to_usbdev(intf);
1029 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1030 struct usb_endpoint_descriptor *endpoint;
1031 unsigned char *cmd;
1032 u8 bulk_out_ep;
1033 int r;
1034
1035 /* Find bulk out endpoint */
1036 endpoint = &iface_desc->endpoint[1].desc;
1037 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
1038 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1039 USB_ENDPOINT_XFER_BULK) {
1040 bulk_out_ep = endpoint->bEndpointAddress;
1041 } else {
1042 dev_err(&udev->dev,
1043 "zd1211rw: Could not find bulk out endpoint\n");
1044 return -ENODEV;
1045 }
1046
1047 cmd = kzalloc(31, GFP_KERNEL);
1048 if (cmd == NULL)
1049 return -ENODEV;
1050
1051 /* USB bulk command block */
1052 cmd[0] = 0x55; /* bulk command signature */
1053 cmd[1] = 0x53; /* bulk command signature */
1054 cmd[2] = 0x42; /* bulk command signature */
1055 cmd[3] = 0x43; /* bulk command signature */
1056 cmd[14] = 6; /* command length */
1057
1058 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1059 cmd[19] = 0x2; /* eject disc */
1060
1061 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1062 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1063 cmd, 31, NULL, 2000);
1064 kfree(cmd);
1065 if (r)
1066 return r;
1067
1068 /* At this point, the device disconnects and reconnects with the real
1069 * ID numbers. */
1070
1071 usb_set_intfdata(intf, NULL);
1072 return 0;
1073}
1074
Daniel Drake74553ae2007-07-01 18:22:32 +01001075int zd_usb_init_hw(struct zd_usb *usb)
1076{
1077 int r;
1078 struct zd_mac *mac = zd_usb_to_mac(usb);
1079
1080 dev_dbg_f(zd_usb_dev(usb), "\n");
1081
1082 r = upload_firmware(usb);
1083 if (r) {
1084 dev_err(zd_usb_dev(usb),
1085 "couldn't load firmware. Error number %d\n", r);
1086 return r;
1087 }
1088
1089 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1090 if (r) {
1091 dev_dbg_f(zd_usb_dev(usb),
1092 "couldn't reset configuration. Error number %d\n", r);
1093 return r;
1094 }
1095
Daniel Drake459c51a2007-11-19 15:00:29 +00001096 r = zd_mac_init_hw(mac->hw);
Daniel Drake74553ae2007-07-01 18:22:32 +01001097 if (r) {
1098 dev_dbg_f(zd_usb_dev(usb),
1099 "couldn't initialize mac. Error number %d\n", r);
1100 return r;
1101 }
1102
1103 usb->initialized = 1;
1104 return 0;
1105}
1106
Daniel Drakee85d0912006-06-02 17:11:32 +01001107static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1108{
1109 int r;
1110 struct usb_device *udev = interface_to_usbdev(intf);
Daniel Drake459c51a2007-11-19 15:00:29 +00001111 struct zd_usb *usb;
1112 struct ieee80211_hw *hw = NULL;
Daniel Drakee85d0912006-06-02 17:11:32 +01001113
1114 print_id(udev);
1115
Daniel Drakea1030e92006-08-13 12:15:29 +01001116 if (id->driver_info & DEVICE_INSTALLER)
1117 return eject_installer(intf);
1118
Daniel Drakee85d0912006-06-02 17:11:32 +01001119 switch (udev->speed) {
1120 case USB_SPEED_LOW:
1121 case USB_SPEED_FULL:
1122 case USB_SPEED_HIGH:
1123 break;
1124 default:
1125 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1126 r = -ENODEV;
1127 goto error;
1128 }
1129
Daniel Drake459c51a2007-11-19 15:00:29 +00001130 r = usb_reset_device(udev);
1131 if (r) {
1132 dev_err(&intf->dev,
1133 "couldn't reset usb device. Error number %d\n", r);
1134 goto error;
1135 }
Ulrich Kunitz6e3632f2007-01-29 00:59:28 +00001136
Daniel Drake459c51a2007-11-19 15:00:29 +00001137 hw = zd_mac_alloc_hw(intf);
1138 if (hw == NULL) {
Daniel Drakee85d0912006-06-02 17:11:32 +01001139 r = -ENOMEM;
1140 goto error;
1141 }
1142
Daniel Drake459c51a2007-11-19 15:00:29 +00001143 usb = &zd_hw_mac(hw)->chip.usb;
Daniel Drake74553ae2007-07-01 18:22:32 +01001144 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001145
Daniel Drake459c51a2007-11-19 15:00:29 +00001146 r = zd_mac_preinit_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001147 if (r) {
1148 dev_dbg_f(&intf->dev,
1149 "couldn't initialize mac. Error number %d\n", r);
1150 goto error;
1151 }
1152
Daniel Drake459c51a2007-11-19 15:00:29 +00001153 r = ieee80211_register_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001154 if (r) {
1155 dev_dbg_f(&intf->dev,
Daniel Drake459c51a2007-11-19 15:00:29 +00001156 "couldn't register device. Error number %d\n", r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001157 goto error;
1158 }
1159
1160 dev_dbg_f(&intf->dev, "successful\n");
Daniel Drake459c51a2007-11-19 15:00:29 +00001161 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
Daniel Drakee85d0912006-06-02 17:11:32 +01001162 return 0;
1163error:
1164 usb_reset_device(interface_to_usbdev(intf));
Daniel Drake459c51a2007-11-19 15:00:29 +00001165 if (hw) {
1166 zd_mac_clear(zd_hw_mac(hw));
1167 ieee80211_free_hw(hw);
1168 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001169 return r;
1170}
1171
1172static void disconnect(struct usb_interface *intf)
1173{
Daniel Drake459c51a2007-11-19 15:00:29 +00001174 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
Marc Pignate0579d52007-10-15 08:51:52 +02001175 struct zd_mac *mac;
1176 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +01001177
Daniel Drakea1030e92006-08-13 12:15:29 +01001178 /* Either something really bad happened, or we're just dealing with
1179 * a DEVICE_INSTALLER. */
Daniel Drake459c51a2007-11-19 15:00:29 +00001180 if (hw == NULL)
Daniel Drakea1030e92006-08-13 12:15:29 +01001181 return;
1182
Daniel Drake459c51a2007-11-19 15:00:29 +00001183 mac = zd_hw_mac(hw);
Marc Pignate0579d52007-10-15 08:51:52 +02001184 usb = &mac->chip.usb;
1185
Daniel Drakee85d0912006-06-02 17:11:32 +01001186 dev_dbg_f(zd_usb_dev(usb), "\n");
1187
Daniel Drake459c51a2007-11-19 15:00:29 +00001188 ieee80211_unregister_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001189
1190 /* Just in case something has gone wrong! */
1191 zd_usb_disable_rx(usb);
1192 zd_usb_disable_int(usb);
1193
1194 /* If the disconnect has been caused by a removal of the
1195 * driver module, the reset allows reloading of the driver. If the
1196 * reset will not be executed here, the upload of the firmware in the
1197 * probe function caused by the reloading of the driver will fail.
1198 */
1199 usb_reset_device(interface_to_usbdev(intf));
1200
Daniel Drake459c51a2007-11-19 15:00:29 +00001201 zd_mac_clear(mac);
1202 ieee80211_free_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001203 dev_dbg(&intf->dev, "disconnected\n");
1204}
1205
1206static struct usb_driver driver = {
Daniel Drake459c51a2007-11-19 15:00:29 +00001207 .name = KBUILD_MODNAME,
Daniel Drakee85d0912006-06-02 17:11:32 +01001208 .id_table = usb_ids,
1209 .probe = probe,
1210 .disconnect = disconnect,
1211};
1212
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001213struct workqueue_struct *zd_workqueue;
1214
Daniel Drakee85d0912006-06-02 17:11:32 +01001215static int __init usb_init(void)
1216{
1217 int r;
1218
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001219 pr_debug("%s usb_init()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001220
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001221 zd_workqueue = create_singlethread_workqueue(driver.name);
1222 if (zd_workqueue == NULL) {
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001223 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001224 return -ENOMEM;
1225 }
1226
Daniel Drakee85d0912006-06-02 17:11:32 +01001227 r = usb_register(&driver);
1228 if (r) {
Maxime Austruy192b7752007-01-29 00:59:51 +00001229 destroy_workqueue(zd_workqueue);
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001230 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1231 driver.name, r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001232 return r;
1233 }
1234
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001235 pr_debug("%s initialized\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001236 return 0;
1237}
1238
1239static void __exit usb_exit(void)
1240{
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001241 pr_debug("%s usb_exit()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001242 usb_deregister(&driver);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001243 destroy_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +01001244}
1245
1246module_init(usb_init);
1247module_exit(usb_exit);
1248
1249static int usb_int_regs_length(unsigned int count)
1250{
1251 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1252}
1253
1254static void prepare_read_regs_int(struct zd_usb *usb)
1255{
1256 struct zd_usb_interrupt *intr = &usb->intr;
1257
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001258 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001259 intr->read_regs_enabled = 1;
1260 INIT_COMPLETION(intr->read_regs.completion);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001261 spin_unlock_irq(&intr->lock);
1262}
1263
1264static void disable_read_regs_int(struct zd_usb *usb)
1265{
1266 struct zd_usb_interrupt *intr = &usb->intr;
1267
1268 spin_lock_irq(&intr->lock);
1269 intr->read_regs_enabled = 0;
1270 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001271}
1272
1273static int get_results(struct zd_usb *usb, u16 *values,
1274 struct usb_req_read_regs *req, unsigned int count)
1275{
1276 int r;
1277 int i;
1278 struct zd_usb_interrupt *intr = &usb->intr;
1279 struct read_regs_int *rr = &intr->read_regs;
1280 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1281
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001282 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001283
1284 r = -EIO;
1285 /* The created block size seems to be larger than expected.
1286 * However results appear to be correct.
1287 */
1288 if (rr->length < usb_int_regs_length(count)) {
1289 dev_dbg_f(zd_usb_dev(usb),
1290 "error: actual length %d less than expected %d\n",
1291 rr->length, usb_int_regs_length(count));
1292 goto error_unlock;
1293 }
1294 if (rr->length > sizeof(rr->buffer)) {
1295 dev_dbg_f(zd_usb_dev(usb),
1296 "error: actual length %d exceeds buffer size %zu\n",
1297 rr->length, sizeof(rr->buffer));
1298 goto error_unlock;
1299 }
1300
1301 for (i = 0; i < count; i++) {
1302 struct reg_data *rd = &regs->regs[i];
1303 if (rd->addr != req->addr[i]) {
1304 dev_dbg_f(zd_usb_dev(usb),
1305 "rd[%d] addr %#06hx expected %#06hx\n", i,
1306 le16_to_cpu(rd->addr),
1307 le16_to_cpu(req->addr[i]));
1308 goto error_unlock;
1309 }
1310 values[i] = le16_to_cpu(rd->value);
1311 }
1312
1313 r = 0;
1314error_unlock:
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001315 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001316 return r;
1317}
1318
1319int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1320 const zd_addr_t *addresses, unsigned int count)
1321{
1322 int r;
1323 int i, req_len, actual_req_len;
1324 struct usb_device *udev;
1325 struct usb_req_read_regs *req = NULL;
1326 unsigned long timeout;
1327
1328 if (count < 1) {
1329 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1330 return -EINVAL;
1331 }
1332 if (count > USB_MAX_IOREAD16_COUNT) {
1333 dev_dbg_f(zd_usb_dev(usb),
1334 "error: count %u exceeds possible max %u\n",
1335 count, USB_MAX_IOREAD16_COUNT);
1336 return -EINVAL;
1337 }
1338 if (in_atomic()) {
1339 dev_dbg_f(zd_usb_dev(usb),
1340 "error: io in atomic context not supported\n");
1341 return -EWOULDBLOCK;
1342 }
1343 if (!usb_int_enabled(usb)) {
1344 dev_dbg_f(zd_usb_dev(usb),
1345 "error: usb interrupt not enabled\n");
1346 return -EWOULDBLOCK;
1347 }
1348
1349 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001350 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001351 if (!req)
1352 return -ENOMEM;
1353 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1354 for (i = 0; i < count; i++)
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001355 req->addr[i] = cpu_to_le16((u16)addresses[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +01001356
1357 udev = zd_usb_to_usbdev(usb);
1358 prepare_read_regs_int(usb);
1359 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1360 req, req_len, &actual_req_len, 1000 /* ms */);
1361 if (r) {
1362 dev_dbg_f(zd_usb_dev(usb),
1363 "error in usb_bulk_msg(). Error number %d\n", r);
1364 goto error;
1365 }
1366 if (req_len != actual_req_len) {
1367 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1368 " req_len %d != actual_req_len %d\n",
1369 req_len, actual_req_len);
1370 r = -EIO;
1371 goto error;
1372 }
1373
1374 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1375 msecs_to_jiffies(1000));
1376 if (!timeout) {
1377 disable_read_regs_int(usb);
1378 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1379 r = -ETIMEDOUT;
1380 goto error;
1381 }
1382
1383 r = get_results(usb, values, req, count);
1384error:
1385 kfree(req);
1386 return r;
1387}
1388
1389int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1390 unsigned int count)
1391{
1392 int r;
1393 struct usb_device *udev;
1394 struct usb_req_write_regs *req = NULL;
1395 int i, req_len, actual_req_len;
1396
1397 if (count == 0)
1398 return 0;
1399 if (count > USB_MAX_IOWRITE16_COUNT) {
1400 dev_dbg_f(zd_usb_dev(usb),
1401 "error: count %u exceeds possible max %u\n",
1402 count, USB_MAX_IOWRITE16_COUNT);
1403 return -EINVAL;
1404 }
1405 if (in_atomic()) {
1406 dev_dbg_f(zd_usb_dev(usb),
1407 "error: io in atomic context not supported\n");
1408 return -EWOULDBLOCK;
1409 }
1410
1411 req_len = sizeof(struct usb_req_write_regs) +
1412 count * sizeof(struct reg_data);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001413 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001414 if (!req)
1415 return -ENOMEM;
1416
1417 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1418 for (i = 0; i < count; i++) {
1419 struct reg_data *rw = &req->reg_writes[i];
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001420 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
Daniel Drakee85d0912006-06-02 17:11:32 +01001421 rw->value = cpu_to_le16(ioreqs[i].value);
1422 }
1423
1424 udev = zd_usb_to_usbdev(usb);
1425 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1426 req, req_len, &actual_req_len, 1000 /* ms */);
1427 if (r) {
1428 dev_dbg_f(zd_usb_dev(usb),
1429 "error in usb_bulk_msg(). Error number %d\n", r);
1430 goto error;
1431 }
1432 if (req_len != actual_req_len) {
1433 dev_dbg_f(zd_usb_dev(usb),
1434 "error in usb_bulk_msg()"
1435 " req_len %d != actual_req_len %d\n",
1436 req_len, actual_req_len);
1437 r = -EIO;
1438 goto error;
1439 }
1440
1441 /* FALL-THROUGH with r == 0 */
1442error:
1443 kfree(req);
1444 return r;
1445}
1446
1447int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1448{
1449 int r;
1450 struct usb_device *udev;
1451 struct usb_req_rfwrite *req = NULL;
1452 int i, req_len, actual_req_len;
1453 u16 bit_value_template;
1454
1455 if (in_atomic()) {
1456 dev_dbg_f(zd_usb_dev(usb),
1457 "error: io in atomic context not supported\n");
1458 return -EWOULDBLOCK;
1459 }
1460 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1461 dev_dbg_f(zd_usb_dev(usb),
1462 "error: bits %d are smaller than"
1463 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1464 bits, USB_MIN_RFWRITE_BIT_COUNT);
1465 return -EINVAL;
1466 }
1467 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1468 dev_dbg_f(zd_usb_dev(usb),
1469 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1470 bits, USB_MAX_RFWRITE_BIT_COUNT);
1471 return -EINVAL;
1472 }
1473#ifdef DEBUG
1474 if (value & (~0UL << bits)) {
1475 dev_dbg_f(zd_usb_dev(usb),
1476 "error: value %#09x has bits >= %d set\n",
1477 value, bits);
1478 return -EINVAL;
1479 }
1480#endif /* DEBUG */
1481
1482 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1483
1484 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1485 if (r) {
1486 dev_dbg_f(zd_usb_dev(usb),
1487 "error %d: Couldn't read CR203\n", r);
1488 goto out;
1489 }
1490 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1491
1492 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001493 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001494 if (!req)
1495 return -ENOMEM;
1496
1497 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1498 /* 1: 3683a, but not used in ZYDAS driver */
1499 req->value = cpu_to_le16(2);
1500 req->bits = cpu_to_le16(bits);
1501
1502 for (i = 0; i < bits; i++) {
1503 u16 bv = bit_value_template;
1504 if (value & (1 << (bits-1-i)))
1505 bv |= RF_DATA;
1506 req->bit_values[i] = cpu_to_le16(bv);
1507 }
1508
1509 udev = zd_usb_to_usbdev(usb);
1510 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1511 req, req_len, &actual_req_len, 1000 /* ms */);
1512 if (r) {
1513 dev_dbg_f(zd_usb_dev(usb),
1514 "error in usb_bulk_msg(). Error number %d\n", r);
1515 goto out;
1516 }
1517 if (req_len != actual_req_len) {
1518 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1519 " req_len %d != actual_req_len %d\n",
1520 req_len, actual_req_len);
1521 r = -EIO;
1522 goto out;
1523 }
1524
1525 /* FALL-THROUGH with r == 0 */
1526out:
1527 kfree(req);
1528 return r;
1529}