blob: a1a54748ccbd4046ab3fbb6207a9435bc7bdbd4f [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
18#include <asm/unaligned.h>
Daniel Draked066c212006-08-12 17:59:59 +010019#include <linux/kernel.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010020#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/firmware.h>
23#include <linux/device.h>
24#include <linux/errno.h>
25#include <linux/skbuff.h>
26#include <linux/usb.h>
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +010027#include <linux/workqueue.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010028#include <net/ieee80211.h>
29
30#include "zd_def.h"
31#include "zd_netdev.h"
32#include "zd_mac.h"
33#include "zd_usb.h"
34#include "zd_util.h"
35
36static struct usb_device_id usb_ids[] = {
37 /* ZD1211 */
38 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
39 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010043 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010044 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
Daniel Drakedd2f5532006-07-05 15:57:31 +010045 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
Daniel Drake1b865492006-08-12 17:59:50 +010046 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
Daniel Drake12f39302006-08-12 17:59:51 +010047 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
Daniel Drake4ceb7e92006-08-12 18:00:03 +010048 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
Daniel Drakefca27142006-08-29 23:49:32 +010049 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
Daniel Drakefc3e39b2006-08-29 23:49:53 +010050 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
Daniel Drakef2423722006-09-26 04:04:38 +010051 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
Daniel Drakea362bf52006-10-18 00:17:02 +010054 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
Daniel Drake229782a2006-11-17 00:13:48 +000055 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
Daniel Drakea3fa3ab2007-03-26 01:00:12 +010056 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
Daniel Drakea9eea9a2007-05-24 01:06:29 +010057 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010058 /* ZD1211B */
59 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
60 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
Daniel Drakedd2f5532006-07-05 15:57:31 +010061 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz943599e2006-08-12 18:00:05 +010062 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e872952006-11-17 00:26:40 +000063 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
Daniel Drakea2bdcc62006-12-12 01:25:37 +000064 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
65 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
Daniel Drake33218ba2006-12-30 22:38:23 +000066 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
Daniel Drake2e9b2462007-01-04 03:33:54 +000067 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
Daniel Drakeaec91022007-03-11 19:54:39 +000068 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
S.Çağlar Onurc445a312007-04-22 00:52:48 +030070 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
Daniel Drake5d082fe2007-04-07 16:00:43 +010071 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
Daniel Drake187bb412007-05-01 17:14:51 +010072 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz019a6752007-05-01 17:13:51 +010073 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
Daniel Drake93f510b2007-07-01 18:22:21 +010074 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
Daniel Drakea1030e922006-08-13 12:15:29 +010075 /* "Driverless" devices that need ejecting */
76 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010077 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
Daniel Drakee85d0912006-06-02 17:11:32 +010078 {}
79};
80
81MODULE_LICENSE("GPL");
82MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
83MODULE_AUTHOR("Ulrich Kunitz");
84MODULE_AUTHOR("Daniel Drake");
85MODULE_VERSION("1.0");
86MODULE_DEVICE_TABLE(usb, usb_ids);
87
88#define FW_ZD1211_PREFIX "zd1211/zd1211_"
89#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
90
Daniel Drakee85d0912006-06-02 17:11:32 +010091/* USB device initialization */
92
93static int request_fw_file(
94 const struct firmware **fw, const char *name, struct device *device)
95{
96 int r;
97
98 dev_dbg_f(device, "fw name %s\n", name);
99
100 r = request_firmware(fw, name, device);
101 if (r)
102 dev_err(device,
103 "Could not load firmware file %s. Error number %d\n",
104 name, r);
105 return r;
106}
107
108static inline u16 get_bcdDevice(const struct usb_device *udev)
109{
110 return le16_to_cpu(udev->descriptor.bcdDevice);
111}
112
113enum upload_code_flags {
114 REBOOT = 1,
115};
116
117/* Ensures that MAX_TRANSFER_SIZE is even. */
118#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
119
120static int upload_code(struct usb_device *udev,
121 const u8 *data, size_t size, u16 code_offset, int flags)
122{
123 u8 *p;
124 int r;
125
126 /* USB request blocks need "kmalloced" buffers.
127 */
128 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
129 if (!p) {
130 dev_err(&udev->dev, "out of memory\n");
131 r = -ENOMEM;
132 goto error;
133 }
134
135 size &= ~1;
136 while (size > 0) {
137 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
138 size : MAX_TRANSFER_SIZE;
139
140 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
141
142 memcpy(p, data, transfer_size);
143 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
144 USB_REQ_FIRMWARE_DOWNLOAD,
145 USB_DIR_OUT | USB_TYPE_VENDOR,
146 code_offset, 0, p, transfer_size, 1000 /* ms */);
147 if (r < 0) {
148 dev_err(&udev->dev,
149 "USB control request for firmware upload"
150 " failed. Error number %d\n", r);
151 goto error;
152 }
153 transfer_size = r & ~1;
154
155 size -= transfer_size;
156 data += transfer_size;
157 code_offset += transfer_size/sizeof(u16);
158 }
159
160 if (flags & REBOOT) {
161 u8 ret;
162
163 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
164 USB_REQ_FIRMWARE_CONFIRM,
165 USB_DIR_IN | USB_TYPE_VENDOR,
166 0, 0, &ret, sizeof(ret), 5000 /* ms */);
167 if (r != sizeof(ret)) {
168 dev_err(&udev->dev,
169 "control request firmeware confirmation failed."
170 " Return value %d\n", r);
171 if (r >= 0)
172 r = -ENODEV;
173 goto error;
174 }
175 if (ret & 0x80) {
176 dev_err(&udev->dev,
177 "Internal error while downloading."
178 " Firmware confirm return value %#04x\n",
179 (unsigned int)ret);
180 r = -ENODEV;
181 goto error;
182 }
183 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
184 (unsigned int)ret);
185 }
186
187 r = 0;
188error:
189 kfree(p);
190 return r;
191}
192
193static u16 get_word(const void *data, u16 offset)
194{
195 const __le16 *p = data;
196 return le16_to_cpu(p[offset]);
197}
198
Daniel Drake74553ae2007-07-01 18:22:32 +0100199static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
Daniel Drakee85d0912006-06-02 17:11:32 +0100200 const char* postfix)
201{
202 scnprintf(buffer, size, "%s%s",
Daniel Drake74553ae2007-07-01 18:22:32 +0100203 usb->is_zd1211b ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100204 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
205 postfix);
206 return buffer;
207}
208
Daniel Drake74553ae2007-07-01 18:22:32 +0100209static int handle_version_mismatch(struct zd_usb *usb,
Daniel Draked066c212006-08-12 17:59:59 +0100210 const struct firmware *ub_fw)
211{
Daniel Drake74553ae2007-07-01 18:22:32 +0100212 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Draked066c212006-08-12 17:59:59 +0100213 const struct firmware *ur_fw = NULL;
214 int offset;
215 int r = 0;
216 char fw_name[128];
217
218 r = request_fw_file(&ur_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100219 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
Daniel Draked066c212006-08-12 17:59:59 +0100220 &udev->dev);
221 if (r)
222 goto error;
223
Daniel Drakeee302762006-12-12 01:25:52 +0000224 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100225 if (r)
226 goto error;
227
Daniel Drakeee302762006-12-12 01:25:52 +0000228 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
Daniel Draked066c212006-08-12 17:59:59 +0100229 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
Daniel Drakeee302762006-12-12 01:25:52 +0000230 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100231
232 /* At this point, the vendor driver downloads the whole firmware
233 * image, hacks around with version IDs, and uploads it again,
234 * completely overwriting the boot code. We do not do this here as
235 * it is not required on any tested devices, and it is suspected to
236 * cause problems. */
237error:
238 release_firmware(ur_fw);
239 return r;
240}
241
Daniel Drake74553ae2007-07-01 18:22:32 +0100242static int upload_firmware(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100243{
244 int r;
245 u16 fw_bcdDevice;
246 u16 bcdDevice;
Daniel Drake74553ae2007-07-01 18:22:32 +0100247 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100248 const struct firmware *ub_fw = NULL;
249 const struct firmware *uph_fw = NULL;
250 char fw_name[128];
251
252 bcdDevice = get_bcdDevice(udev);
253
254 r = request_fw_file(&ub_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100255 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100256 &udev->dev);
257 if (r)
258 goto error;
259
Daniel Drakeee302762006-12-12 01:25:52 +0000260 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
Daniel Drakee85d0912006-06-02 17:11:32 +0100261
Daniel Drakee85d0912006-06-02 17:11:32 +0100262 if (fw_bcdDevice != bcdDevice) {
263 dev_info(&udev->dev,
Daniel Draked066c212006-08-12 17:59:59 +0100264 "firmware version %#06x and device bootcode version "
265 "%#06x differ\n", fw_bcdDevice, bcdDevice);
266 if (bcdDevice <= 0x4313)
267 dev_warn(&udev->dev, "device has old bootcode, please "
268 "report success or failure\n");
269
Daniel Drake74553ae2007-07-01 18:22:32 +0100270 r = handle_version_mismatch(usb, ub_fw);
Daniel Draked066c212006-08-12 17:59:59 +0100271 if (r)
272 goto error;
Daniel Drakee85d0912006-06-02 17:11:32 +0100273 } else {
274 dev_dbg_f(&udev->dev,
275 "firmware device id %#06x is equal to the "
276 "actual device id\n", fw_bcdDevice);
277 }
278
279
280 r = request_fw_file(&uph_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100281 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100282 &udev->dev);
283 if (r)
284 goto error;
285
Daniel Drakeee302762006-12-12 01:25:52 +0000286 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100287 if (r) {
288 dev_err(&udev->dev,
289 "Could not upload firmware code uph. Error number %d\n",
290 r);
291 }
292
293 /* FALL-THROUGH */
294error:
295 release_firmware(ub_fw);
296 release_firmware(uph_fw);
297 return r;
298}
299
Daniel Drake74553ae2007-07-01 18:22:32 +0100300/* Read data from device address space using "firmware interface" which does
301 * not require firmware to be loaded. */
302int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
303{
304 int r;
305 struct usb_device *udev = zd_usb_to_usbdev(usb);
306
307 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
308 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
309 data, len, 5000);
310 if (r < 0) {
311 dev_err(&udev->dev,
312 "read over firmware interface failed: %d\n", r);
313 return r;
314 } else if (r != len) {
315 dev_err(&udev->dev,
316 "incomplete read over firmware interface: %d/%d\n",
317 r, len);
318 return -EIO;
319 }
320
321 return 0;
322}
323
Daniel Drakee85d0912006-06-02 17:11:32 +0100324#define urb_dev(urb) (&(urb)->dev->dev)
325
326static inline void handle_regs_int(struct urb *urb)
327{
328 struct zd_usb *usb = urb->context;
329 struct zd_usb_interrupt *intr = &usb->intr;
330 int len;
331
332 ZD_ASSERT(in_interrupt());
333 spin_lock(&intr->lock);
334
335 if (intr->read_regs_enabled) {
336 intr->read_regs.length = len = urb->actual_length;
337
338 if (len > sizeof(intr->read_regs.buffer))
339 len = sizeof(intr->read_regs.buffer);
340 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
341 intr->read_regs_enabled = 0;
342 complete(&intr->read_regs.completion);
343 goto out;
344 }
345
346 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
347out:
348 spin_unlock(&intr->lock);
349}
350
351static inline void handle_retry_failed_int(struct urb *urb)
352{
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000353 struct zd_usb *usb = urb->context;
354 struct zd_mac *mac = zd_usb_to_mac(usb);
355 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
356
357 ieee->stats.tx_errors++;
358 ieee->ieee_stats.tx_retry_limit_exceeded++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100359 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
360}
361
362
David Howells7d12e782006-10-05 14:55:46 +0100363static void int_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100364{
365 int r;
366 struct usb_int_header *hdr;
367
368 switch (urb->status) {
369 case 0:
370 break;
371 case -ESHUTDOWN:
372 case -EINVAL:
373 case -ENODEV:
374 case -ENOENT:
375 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100376 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100377 goto kfree;
Daniel Drakee85d0912006-06-02 17:11:32 +0100378 default:
379 goto resubmit;
380 }
381
382 if (urb->actual_length < sizeof(hdr)) {
383 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
384 goto resubmit;
385 }
386
387 hdr = urb->transfer_buffer;
388 if (hdr->type != USB_INT_TYPE) {
389 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
390 goto resubmit;
391 }
392
393 switch (hdr->id) {
394 case USB_INT_ID_REGS:
395 handle_regs_int(urb);
396 break;
397 case USB_INT_ID_RETRY_FAILED:
398 handle_retry_failed_int(urb);
399 break;
400 default:
401 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
402 (unsigned int)hdr->id);
403 goto resubmit;
404 }
405
406resubmit:
407 r = usb_submit_urb(urb, GFP_ATOMIC);
408 if (r) {
409 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
410 goto kfree;
411 }
412 return;
413kfree:
414 kfree(urb->transfer_buffer);
415}
416
417static inline int int_urb_interval(struct usb_device *udev)
418{
419 switch (udev->speed) {
420 case USB_SPEED_HIGH:
421 return 4;
422 case USB_SPEED_LOW:
423 return 10;
424 case USB_SPEED_FULL:
425 default:
426 return 1;
427 }
428}
429
430static inline int usb_int_enabled(struct zd_usb *usb)
431{
432 unsigned long flags;
433 struct zd_usb_interrupt *intr = &usb->intr;
434 struct urb *urb;
435
436 spin_lock_irqsave(&intr->lock, flags);
437 urb = intr->urb;
438 spin_unlock_irqrestore(&intr->lock, flags);
439 return urb != NULL;
440}
441
442int zd_usb_enable_int(struct zd_usb *usb)
443{
444 int r;
445 struct usb_device *udev;
446 struct zd_usb_interrupt *intr = &usb->intr;
447 void *transfer_buffer = NULL;
448 struct urb *urb;
449
450 dev_dbg_f(zd_usb_dev(usb), "\n");
451
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000452 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100453 if (!urb) {
454 r = -ENOMEM;
455 goto out;
456 }
457
458 ZD_ASSERT(!irqs_disabled());
459 spin_lock_irq(&intr->lock);
460 if (intr->urb) {
461 spin_unlock_irq(&intr->lock);
462 r = 0;
463 goto error_free_urb;
464 }
465 intr->urb = urb;
466 spin_unlock_irq(&intr->lock);
467
468 /* TODO: make it a DMA buffer */
469 r = -ENOMEM;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000470 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100471 if (!transfer_buffer) {
472 dev_dbg_f(zd_usb_dev(usb),
473 "couldn't allocate transfer_buffer\n");
474 goto error_set_urb_null;
475 }
476
477 udev = zd_usb_to_usbdev(usb);
478 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
479 transfer_buffer, USB_MAX_EP_INT_BUFFER,
480 int_urb_complete, usb,
481 intr->interval);
482
483 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000484 r = usb_submit_urb(urb, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100485 if (r) {
486 dev_dbg_f(zd_usb_dev(usb),
487 "Couldn't submit urb. Error number %d\n", r);
488 goto error;
489 }
490
491 return 0;
492error:
493 kfree(transfer_buffer);
494error_set_urb_null:
495 spin_lock_irq(&intr->lock);
496 intr->urb = NULL;
497 spin_unlock_irq(&intr->lock);
498error_free_urb:
499 usb_free_urb(urb);
500out:
501 return r;
502}
503
504void zd_usb_disable_int(struct zd_usb *usb)
505{
506 unsigned long flags;
507 struct zd_usb_interrupt *intr = &usb->intr;
508 struct urb *urb;
509
510 spin_lock_irqsave(&intr->lock, flags);
511 urb = intr->urb;
512 if (!urb) {
513 spin_unlock_irqrestore(&intr->lock, flags);
514 return;
515 }
516 intr->urb = NULL;
517 spin_unlock_irqrestore(&intr->lock, flags);
518
519 usb_kill_urb(urb);
520 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
521 usb_free_urb(urb);
522}
523
524static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
525 unsigned int length)
526{
527 int i;
528 struct zd_mac *mac = zd_usb_to_mac(usb);
529 const struct rx_length_info *length_info;
530
531 if (length < sizeof(struct rx_length_info)) {
532 /* It's not a complete packet anyhow. */
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000533 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
534 ieee->stats.rx_errors++;
535 ieee->stats.rx_length_errors++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100536 return;
537 }
538 length_info = (struct rx_length_info *)
539 (buffer + length - sizeof(struct rx_length_info));
540
541 /* It might be that three frames are merged into a single URB
542 * transaction. We have to check for the length info tag.
543 *
544 * While testing we discovered that length_info might be unaligned,
545 * because if USB transactions are merged, the last packet will not
546 * be padded. Unaligned access might also happen if the length_info
547 * structure is not present.
548 */
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200549 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
550 {
Daniel Drakee85d0912006-06-02 17:11:32 +0100551 unsigned int l, k, n;
552 for (i = 0, l = 0;; i++) {
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200553 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
Ulrich Kunitz850c2112006-11-22 00:06:19 +0000554 if (k == 0)
555 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100556 n = l+k;
557 if (n > length)
558 return;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800559 zd_mac_rx_irq(mac, buffer+l, k);
Daniel Drakee85d0912006-06-02 17:11:32 +0100560 if (i >= 2)
561 return;
562 l = (n+3) & ~3;
563 }
564 } else {
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800565 zd_mac_rx_irq(mac, buffer, length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100566 }
567}
568
David Howells7d12e782006-10-05 14:55:46 +0100569static void rx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100570{
571 struct zd_usb *usb;
572 struct zd_usb_rx *rx;
573 const u8 *buffer;
574 unsigned int length;
575
576 switch (urb->status) {
577 case 0:
578 break;
579 case -ESHUTDOWN:
580 case -EINVAL:
581 case -ENODEV:
582 case -ENOENT:
583 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100584 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100585 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100586 default:
587 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
588 goto resubmit;
589 }
590
591 buffer = urb->transfer_buffer;
592 length = urb->actual_length;
593 usb = urb->context;
594 rx = &usb->rx;
595
596 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
597 /* If there is an old first fragment, we don't care. */
598 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
599 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
600 spin_lock(&rx->lock);
601 memcpy(rx->fragment, buffer, length);
602 rx->fragment_length = length;
603 spin_unlock(&rx->lock);
604 goto resubmit;
605 }
606
607 spin_lock(&rx->lock);
608 if (rx->fragment_length > 0) {
609 /* We are on a second fragment, we believe */
610 ZD_ASSERT(length + rx->fragment_length <=
611 ARRAY_SIZE(rx->fragment));
612 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
613 memcpy(rx->fragment+rx->fragment_length, buffer, length);
614 handle_rx_packet(usb, rx->fragment,
615 rx->fragment_length + length);
616 rx->fragment_length = 0;
617 spin_unlock(&rx->lock);
618 } else {
619 spin_unlock(&rx->lock);
620 handle_rx_packet(usb, buffer, length);
621 }
622
623resubmit:
624 usb_submit_urb(urb, GFP_ATOMIC);
625}
626
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100627static struct urb *alloc_urb(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100628{
629 struct usb_device *udev = zd_usb_to_usbdev(usb);
630 struct urb *urb;
631 void *buffer;
632
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000633 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100634 if (!urb)
635 return NULL;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000636 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
Daniel Drakee85d0912006-06-02 17:11:32 +0100637 &urb->transfer_dma);
638 if (!buffer) {
639 usb_free_urb(urb);
640 return NULL;
641 }
642
643 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
644 buffer, USB_MAX_RX_SIZE,
645 rx_urb_complete, usb);
646 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
647
648 return urb;
649}
650
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100651static void free_urb(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100652{
653 if (!urb)
654 return;
655 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
656 urb->transfer_buffer, urb->transfer_dma);
657 usb_free_urb(urb);
658}
659
660int zd_usb_enable_rx(struct zd_usb *usb)
661{
662 int i, r;
663 struct zd_usb_rx *rx = &usb->rx;
664 struct urb **urbs;
665
666 dev_dbg_f(zd_usb_dev(usb), "\n");
667
668 r = -ENOMEM;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000669 urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100670 if (!urbs)
671 goto error;
672 for (i = 0; i < URBS_COUNT; i++) {
673 urbs[i] = alloc_urb(usb);
674 if (!urbs[i])
675 goto error;
676 }
677
678 ZD_ASSERT(!irqs_disabled());
679 spin_lock_irq(&rx->lock);
680 if (rx->urbs) {
681 spin_unlock_irq(&rx->lock);
682 r = 0;
683 goto error;
684 }
685 rx->urbs = urbs;
686 rx->urbs_count = URBS_COUNT;
687 spin_unlock_irq(&rx->lock);
688
689 for (i = 0; i < URBS_COUNT; i++) {
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000690 r = usb_submit_urb(urbs[i], GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100691 if (r)
692 goto error_submit;
693 }
694
695 return 0;
696error_submit:
697 for (i = 0; i < URBS_COUNT; i++) {
698 usb_kill_urb(urbs[i]);
699 }
700 spin_lock_irq(&rx->lock);
701 rx->urbs = NULL;
702 rx->urbs_count = 0;
703 spin_unlock_irq(&rx->lock);
704error:
705 if (urbs) {
706 for (i = 0; i < URBS_COUNT; i++)
707 free_urb(urbs[i]);
708 }
709 return r;
710}
711
712void zd_usb_disable_rx(struct zd_usb *usb)
713{
714 int i;
715 unsigned long flags;
716 struct urb **urbs;
717 unsigned int count;
718 struct zd_usb_rx *rx = &usb->rx;
719
720 spin_lock_irqsave(&rx->lock, flags);
721 urbs = rx->urbs;
722 count = rx->urbs_count;
723 spin_unlock_irqrestore(&rx->lock, flags);
724 if (!urbs)
725 return;
726
727 for (i = 0; i < count; i++) {
728 usb_kill_urb(urbs[i]);
729 free_urb(urbs[i]);
730 }
731 kfree(urbs);
732
733 spin_lock_irqsave(&rx->lock, flags);
734 rx->urbs = NULL;
735 rx->urbs_count = 0;
736 spin_unlock_irqrestore(&rx->lock, flags);
737}
738
David Howells7d12e782006-10-05 14:55:46 +0100739static void tx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100740{
741 int r;
742
743 switch (urb->status) {
744 case 0:
745 break;
746 case -ESHUTDOWN:
747 case -EINVAL:
748 case -ENODEV:
749 case -ENOENT:
750 case -ECONNRESET:
Daniel Drakeb312d792006-07-05 15:57:39 +0100751 case -EPIPE:
Daniel Drakee85d0912006-06-02 17:11:32 +0100752 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
753 break;
Daniel Drakee85d0912006-06-02 17:11:32 +0100754 default:
755 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
756 goto resubmit;
757 }
758free_urb:
759 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
760 urb->transfer_buffer, urb->transfer_dma);
761 usb_free_urb(urb);
762 return;
763resubmit:
764 r = usb_submit_urb(urb, GFP_ATOMIC);
765 if (r) {
766 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
767 goto free_urb;
768 }
769}
770
771/* Puts the frame on the USB endpoint. It doesn't wait for
772 * completion. The frame must contain the control set.
773 */
774int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
775{
776 int r;
777 struct usb_device *udev = zd_usb_to_usbdev(usb);
778 struct urb *urb;
779 void *buffer;
780
781 urb = usb_alloc_urb(0, GFP_ATOMIC);
782 if (!urb) {
783 r = -ENOMEM;
784 goto out;
785 }
786
787 buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
788 &urb->transfer_dma);
789 if (!buffer) {
790 r = -ENOMEM;
791 goto error_free_urb;
792 }
793 memcpy(buffer, frame, length);
794
795 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
796 buffer, length, tx_urb_complete, NULL);
797 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
798
799 r = usb_submit_urb(urb, GFP_ATOMIC);
800 if (r)
801 goto error;
802 return 0;
803error:
804 usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
805 urb->transfer_dma);
806error_free_urb:
807 usb_free_urb(urb);
808out:
809 return r;
810}
811
812static inline void init_usb_interrupt(struct zd_usb *usb)
813{
814 struct zd_usb_interrupt *intr = &usb->intr;
815
816 spin_lock_init(&intr->lock);
817 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
818 init_completion(&intr->read_regs.completion);
Daniel Drake0ce34bc2006-12-12 01:26:11 +0000819 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100820}
821
822static inline void init_usb_rx(struct zd_usb *usb)
823{
824 struct zd_usb_rx *rx = &usb->rx;
825 spin_lock_init(&rx->lock);
826 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
827 rx->usb_packet_size = 512;
828 } else {
829 rx->usb_packet_size = 64;
830 }
831 ZD_ASSERT(rx->fragment_length == 0);
832}
833
834static inline void init_usb_tx(struct zd_usb *usb)
835{
836 /* FIXME: at this point we will allocate a fixed number of urb's for
837 * use in a cyclic scheme */
838}
839
840void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
841 struct usb_interface *intf)
842{
843 memset(usb, 0, sizeof(*usb));
844 usb->intf = usb_get_intf(intf);
845 usb_set_intfdata(usb->intf, netdev);
846 init_usb_interrupt(usb);
847 init_usb_tx(usb);
848 init_usb_rx(usb);
849}
850
Daniel Drakee85d0912006-06-02 17:11:32 +0100851void zd_usb_clear(struct zd_usb *usb)
852{
853 usb_set_intfdata(usb->intf, NULL);
854 usb_put_intf(usb->intf);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100855 ZD_MEMCLEAR(usb, sizeof(*usb));
Daniel Drakee85d0912006-06-02 17:11:32 +0100856 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
857}
858
859static const char *speed(enum usb_device_speed speed)
860{
861 switch (speed) {
862 case USB_SPEED_LOW:
863 return "low";
864 case USB_SPEED_FULL:
865 return "full";
866 case USB_SPEED_HIGH:
867 return "high";
868 default:
869 return "unknown speed";
870 }
871}
872
873static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
874{
875 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
876 le16_to_cpu(udev->descriptor.idVendor),
877 le16_to_cpu(udev->descriptor.idProduct),
878 get_bcdDevice(udev),
879 speed(udev->speed));
880}
881
882int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
883{
884 struct usb_device *udev = interface_to_usbdev(usb->intf);
885 return scnprint_id(udev, buffer, size);
886}
887
888#ifdef DEBUG
889static void print_id(struct usb_device *udev)
890{
891 char buffer[40];
892
893 scnprint_id(udev, buffer, sizeof(buffer));
894 buffer[sizeof(buffer)-1] = 0;
895 dev_dbg_f(&udev->dev, "%s\n", buffer);
896}
897#else
898#define print_id(udev) do { } while (0)
899#endif
900
Daniel Drakea1030e922006-08-13 12:15:29 +0100901static int eject_installer(struct usb_interface *intf)
902{
903 struct usb_device *udev = interface_to_usbdev(intf);
904 struct usb_host_interface *iface_desc = &intf->altsetting[0];
905 struct usb_endpoint_descriptor *endpoint;
906 unsigned char *cmd;
907 u8 bulk_out_ep;
908 int r;
909
910 /* Find bulk out endpoint */
911 endpoint = &iface_desc->endpoint[1].desc;
912 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
913 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
914 USB_ENDPOINT_XFER_BULK) {
915 bulk_out_ep = endpoint->bEndpointAddress;
916 } else {
917 dev_err(&udev->dev,
918 "zd1211rw: Could not find bulk out endpoint\n");
919 return -ENODEV;
920 }
921
922 cmd = kzalloc(31, GFP_KERNEL);
923 if (cmd == NULL)
924 return -ENODEV;
925
926 /* USB bulk command block */
927 cmd[0] = 0x55; /* bulk command signature */
928 cmd[1] = 0x53; /* bulk command signature */
929 cmd[2] = 0x42; /* bulk command signature */
930 cmd[3] = 0x43; /* bulk command signature */
931 cmd[14] = 6; /* command length */
932
933 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
934 cmd[19] = 0x2; /* eject disc */
935
936 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
937 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
938 cmd, 31, NULL, 2000);
939 kfree(cmd);
940 if (r)
941 return r;
942
943 /* At this point, the device disconnects and reconnects with the real
944 * ID numbers. */
945
946 usb_set_intfdata(intf, NULL);
947 return 0;
948}
949
Daniel Drake74553ae2007-07-01 18:22:32 +0100950int zd_usb_init_hw(struct zd_usb *usb)
951{
952 int r;
953 struct zd_mac *mac = zd_usb_to_mac(usb);
954
955 dev_dbg_f(zd_usb_dev(usb), "\n");
956
957 r = upload_firmware(usb);
958 if (r) {
959 dev_err(zd_usb_dev(usb),
960 "couldn't load firmware. Error number %d\n", r);
961 return r;
962 }
963
964 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
965 if (r) {
966 dev_dbg_f(zd_usb_dev(usb),
967 "couldn't reset configuration. Error number %d\n", r);
968 return r;
969 }
970
971 r = zd_mac_init_hw(mac);
972 if (r) {
973 dev_dbg_f(zd_usb_dev(usb),
974 "couldn't initialize mac. Error number %d\n", r);
975 return r;
976 }
977
978 usb->initialized = 1;
979 return 0;
980}
981
Daniel Drakee85d0912006-06-02 17:11:32 +0100982static int probe(struct usb_interface *intf, const struct usb_device_id *id)
983{
984 int r;
Daniel Drake74553ae2007-07-01 18:22:32 +0100985 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100986 struct usb_device *udev = interface_to_usbdev(intf);
987 struct net_device *netdev = NULL;
988
989 print_id(udev);
990
Daniel Drakea1030e922006-08-13 12:15:29 +0100991 if (id->driver_info & DEVICE_INSTALLER)
992 return eject_installer(intf);
993
Daniel Drakee85d0912006-06-02 17:11:32 +0100994 switch (udev->speed) {
995 case USB_SPEED_LOW:
996 case USB_SPEED_FULL:
997 case USB_SPEED_HIGH:
998 break;
999 default:
1000 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1001 r = -ENODEV;
1002 goto error;
1003 }
1004
Ulrich Kunitz6e3632f2007-01-29 00:59:28 +00001005 usb_reset_device(interface_to_usbdev(intf));
1006
Daniel Drakee85d0912006-06-02 17:11:32 +01001007 netdev = zd_netdev_alloc(intf);
1008 if (netdev == NULL) {
1009 r = -ENOMEM;
1010 goto error;
1011 }
1012
Daniel Drake74553ae2007-07-01 18:22:32 +01001013 usb = &zd_netdev_mac(netdev)->chip.usb;
1014 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001015
Daniel Drake74553ae2007-07-01 18:22:32 +01001016 r = zd_mac_preinit_hw(zd_netdev_mac(netdev));
Daniel Drakee85d0912006-06-02 17:11:32 +01001017 if (r) {
1018 dev_dbg_f(&intf->dev,
1019 "couldn't initialize mac. Error number %d\n", r);
1020 goto error;
1021 }
1022
1023 r = register_netdev(netdev);
1024 if (r) {
1025 dev_dbg_f(&intf->dev,
1026 "couldn't register netdev. Error number %d\n", r);
1027 goto error;
1028 }
1029
1030 dev_dbg_f(&intf->dev, "successful\n");
1031 dev_info(&intf->dev,"%s\n", netdev->name);
1032 return 0;
1033error:
1034 usb_reset_device(interface_to_usbdev(intf));
1035 zd_netdev_free(netdev);
1036 return r;
1037}
1038
1039static void disconnect(struct usb_interface *intf)
1040{
1041 struct net_device *netdev = zd_intf_to_netdev(intf);
1042 struct zd_mac *mac = zd_netdev_mac(netdev);
1043 struct zd_usb *usb = &mac->chip.usb;
1044
Daniel Drakea1030e922006-08-13 12:15:29 +01001045 /* Either something really bad happened, or we're just dealing with
1046 * a DEVICE_INSTALLER. */
1047 if (netdev == NULL)
1048 return;
1049
Daniel Drakee85d0912006-06-02 17:11:32 +01001050 dev_dbg_f(zd_usb_dev(usb), "\n");
1051
1052 zd_netdev_disconnect(netdev);
1053
1054 /* Just in case something has gone wrong! */
1055 zd_usb_disable_rx(usb);
1056 zd_usb_disable_int(usb);
1057
1058 /* If the disconnect has been caused by a removal of the
1059 * driver module, the reset allows reloading of the driver. If the
1060 * reset will not be executed here, the upload of the firmware in the
1061 * probe function caused by the reloading of the driver will fail.
1062 */
1063 usb_reset_device(interface_to_usbdev(intf));
1064
Daniel Drakee85d0912006-06-02 17:11:32 +01001065 zd_netdev_free(netdev);
1066 dev_dbg(&intf->dev, "disconnected\n");
1067}
1068
1069static struct usb_driver driver = {
1070 .name = "zd1211rw",
1071 .id_table = usb_ids,
1072 .probe = probe,
1073 .disconnect = disconnect,
1074};
1075
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001076struct workqueue_struct *zd_workqueue;
1077
Daniel Drakee85d0912006-06-02 17:11:32 +01001078static int __init usb_init(void)
1079{
1080 int r;
1081
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001082 pr_debug("%s usb_init()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001083
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001084 zd_workqueue = create_singlethread_workqueue(driver.name);
1085 if (zd_workqueue == NULL) {
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001086 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001087 return -ENOMEM;
1088 }
1089
Daniel Drakee85d0912006-06-02 17:11:32 +01001090 r = usb_register(&driver);
1091 if (r) {
Maxime Austruy192b7752007-01-29 00:59:51 +00001092 destroy_workqueue(zd_workqueue);
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001093 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1094 driver.name, r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001095 return r;
1096 }
1097
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001098 pr_debug("%s initialized\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001099 return 0;
1100}
1101
1102static void __exit usb_exit(void)
1103{
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001104 pr_debug("%s usb_exit()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001105 usb_deregister(&driver);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001106 destroy_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +01001107}
1108
1109module_init(usb_init);
1110module_exit(usb_exit);
1111
1112static int usb_int_regs_length(unsigned int count)
1113{
1114 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1115}
1116
1117static void prepare_read_regs_int(struct zd_usb *usb)
1118{
1119 struct zd_usb_interrupt *intr = &usb->intr;
1120
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001121 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001122 intr->read_regs_enabled = 1;
1123 INIT_COMPLETION(intr->read_regs.completion);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001124 spin_unlock_irq(&intr->lock);
1125}
1126
1127static void disable_read_regs_int(struct zd_usb *usb)
1128{
1129 struct zd_usb_interrupt *intr = &usb->intr;
1130
1131 spin_lock_irq(&intr->lock);
1132 intr->read_regs_enabled = 0;
1133 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001134}
1135
1136static int get_results(struct zd_usb *usb, u16 *values,
1137 struct usb_req_read_regs *req, unsigned int count)
1138{
1139 int r;
1140 int i;
1141 struct zd_usb_interrupt *intr = &usb->intr;
1142 struct read_regs_int *rr = &intr->read_regs;
1143 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1144
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001145 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001146
1147 r = -EIO;
1148 /* The created block size seems to be larger than expected.
1149 * However results appear to be correct.
1150 */
1151 if (rr->length < usb_int_regs_length(count)) {
1152 dev_dbg_f(zd_usb_dev(usb),
1153 "error: actual length %d less than expected %d\n",
1154 rr->length, usb_int_regs_length(count));
1155 goto error_unlock;
1156 }
1157 if (rr->length > sizeof(rr->buffer)) {
1158 dev_dbg_f(zd_usb_dev(usb),
1159 "error: actual length %d exceeds buffer size %zu\n",
1160 rr->length, sizeof(rr->buffer));
1161 goto error_unlock;
1162 }
1163
1164 for (i = 0; i < count; i++) {
1165 struct reg_data *rd = &regs->regs[i];
1166 if (rd->addr != req->addr[i]) {
1167 dev_dbg_f(zd_usb_dev(usb),
1168 "rd[%d] addr %#06hx expected %#06hx\n", i,
1169 le16_to_cpu(rd->addr),
1170 le16_to_cpu(req->addr[i]));
1171 goto error_unlock;
1172 }
1173 values[i] = le16_to_cpu(rd->value);
1174 }
1175
1176 r = 0;
1177error_unlock:
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001178 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001179 return r;
1180}
1181
1182int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1183 const zd_addr_t *addresses, unsigned int count)
1184{
1185 int r;
1186 int i, req_len, actual_req_len;
1187 struct usb_device *udev;
1188 struct usb_req_read_regs *req = NULL;
1189 unsigned long timeout;
1190
1191 if (count < 1) {
1192 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1193 return -EINVAL;
1194 }
1195 if (count > USB_MAX_IOREAD16_COUNT) {
1196 dev_dbg_f(zd_usb_dev(usb),
1197 "error: count %u exceeds possible max %u\n",
1198 count, USB_MAX_IOREAD16_COUNT);
1199 return -EINVAL;
1200 }
1201 if (in_atomic()) {
1202 dev_dbg_f(zd_usb_dev(usb),
1203 "error: io in atomic context not supported\n");
1204 return -EWOULDBLOCK;
1205 }
1206 if (!usb_int_enabled(usb)) {
1207 dev_dbg_f(zd_usb_dev(usb),
1208 "error: usb interrupt not enabled\n");
1209 return -EWOULDBLOCK;
1210 }
1211
1212 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001213 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001214 if (!req)
1215 return -ENOMEM;
1216 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1217 for (i = 0; i < count; i++)
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001218 req->addr[i] = cpu_to_le16((u16)addresses[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +01001219
1220 udev = zd_usb_to_usbdev(usb);
1221 prepare_read_regs_int(usb);
1222 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1223 req, req_len, &actual_req_len, 1000 /* ms */);
1224 if (r) {
1225 dev_dbg_f(zd_usb_dev(usb),
1226 "error in usb_bulk_msg(). Error number %d\n", r);
1227 goto error;
1228 }
1229 if (req_len != actual_req_len) {
1230 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1231 " req_len %d != actual_req_len %d\n",
1232 req_len, actual_req_len);
1233 r = -EIO;
1234 goto error;
1235 }
1236
1237 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1238 msecs_to_jiffies(1000));
1239 if (!timeout) {
1240 disable_read_regs_int(usb);
1241 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1242 r = -ETIMEDOUT;
1243 goto error;
1244 }
1245
1246 r = get_results(usb, values, req, count);
1247error:
1248 kfree(req);
1249 return r;
1250}
1251
1252int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1253 unsigned int count)
1254{
1255 int r;
1256 struct usb_device *udev;
1257 struct usb_req_write_regs *req = NULL;
1258 int i, req_len, actual_req_len;
1259
1260 if (count == 0)
1261 return 0;
1262 if (count > USB_MAX_IOWRITE16_COUNT) {
1263 dev_dbg_f(zd_usb_dev(usb),
1264 "error: count %u exceeds possible max %u\n",
1265 count, USB_MAX_IOWRITE16_COUNT);
1266 return -EINVAL;
1267 }
1268 if (in_atomic()) {
1269 dev_dbg_f(zd_usb_dev(usb),
1270 "error: io in atomic context not supported\n");
1271 return -EWOULDBLOCK;
1272 }
1273
1274 req_len = sizeof(struct usb_req_write_regs) +
1275 count * sizeof(struct reg_data);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001276 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001277 if (!req)
1278 return -ENOMEM;
1279
1280 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1281 for (i = 0; i < count; i++) {
1282 struct reg_data *rw = &req->reg_writes[i];
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001283 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
Daniel Drakee85d0912006-06-02 17:11:32 +01001284 rw->value = cpu_to_le16(ioreqs[i].value);
1285 }
1286
1287 udev = zd_usb_to_usbdev(usb);
1288 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1289 req, req_len, &actual_req_len, 1000 /* ms */);
1290 if (r) {
1291 dev_dbg_f(zd_usb_dev(usb),
1292 "error in usb_bulk_msg(). Error number %d\n", r);
1293 goto error;
1294 }
1295 if (req_len != actual_req_len) {
1296 dev_dbg_f(zd_usb_dev(usb),
1297 "error in usb_bulk_msg()"
1298 " req_len %d != actual_req_len %d\n",
1299 req_len, actual_req_len);
1300 r = -EIO;
1301 goto error;
1302 }
1303
1304 /* FALL-THROUGH with r == 0 */
1305error:
1306 kfree(req);
1307 return r;
1308}
1309
1310int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1311{
1312 int r;
1313 struct usb_device *udev;
1314 struct usb_req_rfwrite *req = NULL;
1315 int i, req_len, actual_req_len;
1316 u16 bit_value_template;
1317
1318 if (in_atomic()) {
1319 dev_dbg_f(zd_usb_dev(usb),
1320 "error: io in atomic context not supported\n");
1321 return -EWOULDBLOCK;
1322 }
1323 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1324 dev_dbg_f(zd_usb_dev(usb),
1325 "error: bits %d are smaller than"
1326 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1327 bits, USB_MIN_RFWRITE_BIT_COUNT);
1328 return -EINVAL;
1329 }
1330 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1331 dev_dbg_f(zd_usb_dev(usb),
1332 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1333 bits, USB_MAX_RFWRITE_BIT_COUNT);
1334 return -EINVAL;
1335 }
1336#ifdef DEBUG
1337 if (value & (~0UL << bits)) {
1338 dev_dbg_f(zd_usb_dev(usb),
1339 "error: value %#09x has bits >= %d set\n",
1340 value, bits);
1341 return -EINVAL;
1342 }
1343#endif /* DEBUG */
1344
1345 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1346
1347 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1348 if (r) {
1349 dev_dbg_f(zd_usb_dev(usb),
1350 "error %d: Couldn't read CR203\n", r);
1351 goto out;
1352 }
1353 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1354
1355 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001356 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001357 if (!req)
1358 return -ENOMEM;
1359
1360 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1361 /* 1: 3683a, but not used in ZYDAS driver */
1362 req->value = cpu_to_le16(2);
1363 req->bits = cpu_to_le16(bits);
1364
1365 for (i = 0; i < bits; i++) {
1366 u16 bv = bit_value_template;
1367 if (value & (1 << (bits-1-i)))
1368 bv |= RF_DATA;
1369 req->bit_values[i] = cpu_to_le16(bv);
1370 }
1371
1372 udev = zd_usb_to_usbdev(usb);
1373 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1374 req, req_len, &actual_req_len, 1000 /* ms */);
1375 if (r) {
1376 dev_dbg_f(zd_usb_dev(usb),
1377 "error in usb_bulk_msg(). Error number %d\n", r);
1378 goto out;
1379 }
1380 if (req_len != actual_req_len) {
1381 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1382 " req_len %d != actual_req_len %d\n",
1383 req_len, actual_req_len);
1384 r = -EIO;
1385 goto out;
1386 }
1387
1388 /* FALL-THROUGH with r == 0 */
1389out:
1390 kfree(req);
1391 return r;
1392}