blob: edaaad2f648b627493abc6954d0d76558e24eec3 [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 },
43 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
Daniel Drakedd2f5532006-07-05 15:57:31 +010044 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
Daniel Drake1b865492006-08-12 17:59:50 +010045 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
Daniel Drake12f39302006-08-12 17:59:51 +010046 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
Daniel Drake4ceb7e92006-08-12 18:00:03 +010047 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
Daniel Drakefca27142006-08-29 23:49:32 +010048 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
Daniel Drakefc3e39b2006-08-29 23:49:53 +010049 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
Daniel Drakef2423722006-09-26 04:04:38 +010050 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
Daniel Drakea362bf52006-10-18 00:17:02 +010053 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
Daniel Drake229782a2006-11-17 00:13:48 +000054 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010055 /* ZD1211B */
56 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
57 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
Daniel Drakedd2f5532006-07-05 15:57:31 +010058 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz943599e2006-08-12 18:00:05 +010059 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e872952006-11-17 00:26:40 +000060 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
Daniel Drakea2bdcc62006-12-12 01:25:37 +000061 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
62 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
Daniel Drake33218ba2006-12-30 22:38:23 +000063 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
Daniel Drake2e9b2462007-01-04 03:33:54 +000064 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
S.Çağlar Onurc445a312007-04-22 00:52:48 +030065 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
Daniel Drakea1030e922006-08-13 12:15:29 +010066 /* "Driverless" devices that need ejecting */
67 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
Daniel Drakee85d0912006-06-02 17:11:32 +010068 {}
69};
70
71MODULE_LICENSE("GPL");
72MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
73MODULE_AUTHOR("Ulrich Kunitz");
74MODULE_AUTHOR("Daniel Drake");
75MODULE_VERSION("1.0");
76MODULE_DEVICE_TABLE(usb, usb_ids);
77
78#define FW_ZD1211_PREFIX "zd1211/zd1211_"
79#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
80
Daniel Drakee85d0912006-06-02 17:11:32 +010081/* USB device initialization */
82
83static int request_fw_file(
84 const struct firmware **fw, const char *name, struct device *device)
85{
86 int r;
87
88 dev_dbg_f(device, "fw name %s\n", name);
89
90 r = request_firmware(fw, name, device);
91 if (r)
92 dev_err(device,
93 "Could not load firmware file %s. Error number %d\n",
94 name, r);
95 return r;
96}
97
98static inline u16 get_bcdDevice(const struct usb_device *udev)
99{
100 return le16_to_cpu(udev->descriptor.bcdDevice);
101}
102
103enum upload_code_flags {
104 REBOOT = 1,
105};
106
107/* Ensures that MAX_TRANSFER_SIZE is even. */
108#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
109
110static int upload_code(struct usb_device *udev,
111 const u8 *data, size_t size, u16 code_offset, int flags)
112{
113 u8 *p;
114 int r;
115
116 /* USB request blocks need "kmalloced" buffers.
117 */
118 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
119 if (!p) {
120 dev_err(&udev->dev, "out of memory\n");
121 r = -ENOMEM;
122 goto error;
123 }
124
125 size &= ~1;
126 while (size > 0) {
127 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
128 size : MAX_TRANSFER_SIZE;
129
130 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
131
132 memcpy(p, data, transfer_size);
133 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
134 USB_REQ_FIRMWARE_DOWNLOAD,
135 USB_DIR_OUT | USB_TYPE_VENDOR,
136 code_offset, 0, p, transfer_size, 1000 /* ms */);
137 if (r < 0) {
138 dev_err(&udev->dev,
139 "USB control request for firmware upload"
140 " failed. Error number %d\n", r);
141 goto error;
142 }
143 transfer_size = r & ~1;
144
145 size -= transfer_size;
146 data += transfer_size;
147 code_offset += transfer_size/sizeof(u16);
148 }
149
150 if (flags & REBOOT) {
151 u8 ret;
152
153 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
154 USB_REQ_FIRMWARE_CONFIRM,
155 USB_DIR_IN | USB_TYPE_VENDOR,
156 0, 0, &ret, sizeof(ret), 5000 /* ms */);
157 if (r != sizeof(ret)) {
158 dev_err(&udev->dev,
159 "control request firmeware confirmation failed."
160 " Return value %d\n", r);
161 if (r >= 0)
162 r = -ENODEV;
163 goto error;
164 }
165 if (ret & 0x80) {
166 dev_err(&udev->dev,
167 "Internal error while downloading."
168 " Firmware confirm return value %#04x\n",
169 (unsigned int)ret);
170 r = -ENODEV;
171 goto error;
172 }
173 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
174 (unsigned int)ret);
175 }
176
177 r = 0;
178error:
179 kfree(p);
180 return r;
181}
182
183static u16 get_word(const void *data, u16 offset)
184{
185 const __le16 *p = data;
186 return le16_to_cpu(p[offset]);
187}
188
189static char *get_fw_name(char *buffer, size_t size, u8 device_type,
190 const char* postfix)
191{
192 scnprintf(buffer, size, "%s%s",
193 device_type == DEVICE_ZD1211B ?
194 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
195 postfix);
196 return buffer;
197}
198
Daniel Draked066c212006-08-12 17:59:59 +0100199static int handle_version_mismatch(struct usb_device *udev, u8 device_type,
200 const struct firmware *ub_fw)
201{
202 const struct firmware *ur_fw = NULL;
203 int offset;
204 int r = 0;
205 char fw_name[128];
206
207 r = request_fw_file(&ur_fw,
208 get_fw_name(fw_name, sizeof(fw_name), device_type, "ur"),
209 &udev->dev);
210 if (r)
211 goto error;
212
Daniel Drakeee302762006-12-12 01:25:52 +0000213 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100214 if (r)
215 goto error;
216
Daniel Drakeee302762006-12-12 01:25:52 +0000217 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
Daniel Draked066c212006-08-12 17:59:59 +0100218 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
Daniel Drakeee302762006-12-12 01:25:52 +0000219 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100220
221 /* At this point, the vendor driver downloads the whole firmware
222 * image, hacks around with version IDs, and uploads it again,
223 * completely overwriting the boot code. We do not do this here as
224 * it is not required on any tested devices, and it is suspected to
225 * cause problems. */
226error:
227 release_firmware(ur_fw);
228 return r;
229}
230
Daniel Drakee85d0912006-06-02 17:11:32 +0100231static int upload_firmware(struct usb_device *udev, u8 device_type)
232{
233 int r;
234 u16 fw_bcdDevice;
235 u16 bcdDevice;
236 const struct firmware *ub_fw = NULL;
237 const struct firmware *uph_fw = NULL;
238 char fw_name[128];
239
240 bcdDevice = get_bcdDevice(udev);
241
242 r = request_fw_file(&ub_fw,
243 get_fw_name(fw_name, sizeof(fw_name), device_type, "ub"),
244 &udev->dev);
245 if (r)
246 goto error;
247
Daniel Drakeee302762006-12-12 01:25:52 +0000248 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
Daniel Drakee85d0912006-06-02 17:11:32 +0100249
Daniel Drakee85d0912006-06-02 17:11:32 +0100250 if (fw_bcdDevice != bcdDevice) {
251 dev_info(&udev->dev,
Daniel Draked066c212006-08-12 17:59:59 +0100252 "firmware version %#06x and device bootcode version "
253 "%#06x differ\n", fw_bcdDevice, bcdDevice);
254 if (bcdDevice <= 0x4313)
255 dev_warn(&udev->dev, "device has old bootcode, please "
256 "report success or failure\n");
257
258 r = handle_version_mismatch(udev, device_type, ub_fw);
259 if (r)
260 goto error;
Daniel Drakee85d0912006-06-02 17:11:32 +0100261 } else {
262 dev_dbg_f(&udev->dev,
263 "firmware device id %#06x is equal to the "
264 "actual device id\n", fw_bcdDevice);
265 }
266
267
268 r = request_fw_file(&uph_fw,
269 get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"),
270 &udev->dev);
271 if (r)
272 goto error;
273
Daniel Drakeee302762006-12-12 01:25:52 +0000274 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100275 if (r) {
276 dev_err(&udev->dev,
277 "Could not upload firmware code uph. Error number %d\n",
278 r);
279 }
280
281 /* FALL-THROUGH */
282error:
283 release_firmware(ub_fw);
284 release_firmware(uph_fw);
285 return r;
286}
287
Daniel Drakee85d0912006-06-02 17:11:32 +0100288#define urb_dev(urb) (&(urb)->dev->dev)
289
290static inline void handle_regs_int(struct urb *urb)
291{
292 struct zd_usb *usb = urb->context;
293 struct zd_usb_interrupt *intr = &usb->intr;
294 int len;
295
296 ZD_ASSERT(in_interrupt());
297 spin_lock(&intr->lock);
298
299 if (intr->read_regs_enabled) {
300 intr->read_regs.length = len = urb->actual_length;
301
302 if (len > sizeof(intr->read_regs.buffer))
303 len = sizeof(intr->read_regs.buffer);
304 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
305 intr->read_regs_enabled = 0;
306 complete(&intr->read_regs.completion);
307 goto out;
308 }
309
310 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
311out:
312 spin_unlock(&intr->lock);
313}
314
315static inline void handle_retry_failed_int(struct urb *urb)
316{
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000317 struct zd_usb *usb = urb->context;
318 struct zd_mac *mac = zd_usb_to_mac(usb);
319 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
320
321 ieee->stats.tx_errors++;
322 ieee->ieee_stats.tx_retry_limit_exceeded++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100323 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
324}
325
326
David Howells7d12e782006-10-05 14:55:46 +0100327static void int_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100328{
329 int r;
330 struct usb_int_header *hdr;
331
332 switch (urb->status) {
333 case 0:
334 break;
335 case -ESHUTDOWN:
336 case -EINVAL:
337 case -ENODEV:
338 case -ENOENT:
339 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100340 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100341 goto kfree;
Daniel Drakee85d0912006-06-02 17:11:32 +0100342 default:
343 goto resubmit;
344 }
345
346 if (urb->actual_length < sizeof(hdr)) {
347 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
348 goto resubmit;
349 }
350
351 hdr = urb->transfer_buffer;
352 if (hdr->type != USB_INT_TYPE) {
353 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
354 goto resubmit;
355 }
356
357 switch (hdr->id) {
358 case USB_INT_ID_REGS:
359 handle_regs_int(urb);
360 break;
361 case USB_INT_ID_RETRY_FAILED:
362 handle_retry_failed_int(urb);
363 break;
364 default:
365 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
366 (unsigned int)hdr->id);
367 goto resubmit;
368 }
369
370resubmit:
371 r = usb_submit_urb(urb, GFP_ATOMIC);
372 if (r) {
373 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
374 goto kfree;
375 }
376 return;
377kfree:
378 kfree(urb->transfer_buffer);
379}
380
381static inline int int_urb_interval(struct usb_device *udev)
382{
383 switch (udev->speed) {
384 case USB_SPEED_HIGH:
385 return 4;
386 case USB_SPEED_LOW:
387 return 10;
388 case USB_SPEED_FULL:
389 default:
390 return 1;
391 }
392}
393
394static inline int usb_int_enabled(struct zd_usb *usb)
395{
396 unsigned long flags;
397 struct zd_usb_interrupt *intr = &usb->intr;
398 struct urb *urb;
399
400 spin_lock_irqsave(&intr->lock, flags);
401 urb = intr->urb;
402 spin_unlock_irqrestore(&intr->lock, flags);
403 return urb != NULL;
404}
405
406int zd_usb_enable_int(struct zd_usb *usb)
407{
408 int r;
409 struct usb_device *udev;
410 struct zd_usb_interrupt *intr = &usb->intr;
411 void *transfer_buffer = NULL;
412 struct urb *urb;
413
414 dev_dbg_f(zd_usb_dev(usb), "\n");
415
416 urb = usb_alloc_urb(0, GFP_NOFS);
417 if (!urb) {
418 r = -ENOMEM;
419 goto out;
420 }
421
422 ZD_ASSERT(!irqs_disabled());
423 spin_lock_irq(&intr->lock);
424 if (intr->urb) {
425 spin_unlock_irq(&intr->lock);
426 r = 0;
427 goto error_free_urb;
428 }
429 intr->urb = urb;
430 spin_unlock_irq(&intr->lock);
431
432 /* TODO: make it a DMA buffer */
433 r = -ENOMEM;
434 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS);
435 if (!transfer_buffer) {
436 dev_dbg_f(zd_usb_dev(usb),
437 "couldn't allocate transfer_buffer\n");
438 goto error_set_urb_null;
439 }
440
441 udev = zd_usb_to_usbdev(usb);
442 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
443 transfer_buffer, USB_MAX_EP_INT_BUFFER,
444 int_urb_complete, usb,
445 intr->interval);
446
447 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
448 r = usb_submit_urb(urb, GFP_NOFS);
449 if (r) {
450 dev_dbg_f(zd_usb_dev(usb),
451 "Couldn't submit urb. Error number %d\n", r);
452 goto error;
453 }
454
455 return 0;
456error:
457 kfree(transfer_buffer);
458error_set_urb_null:
459 spin_lock_irq(&intr->lock);
460 intr->urb = NULL;
461 spin_unlock_irq(&intr->lock);
462error_free_urb:
463 usb_free_urb(urb);
464out:
465 return r;
466}
467
468void zd_usb_disable_int(struct zd_usb *usb)
469{
470 unsigned long flags;
471 struct zd_usb_interrupt *intr = &usb->intr;
472 struct urb *urb;
473
474 spin_lock_irqsave(&intr->lock, flags);
475 urb = intr->urb;
476 if (!urb) {
477 spin_unlock_irqrestore(&intr->lock, flags);
478 return;
479 }
480 intr->urb = NULL;
481 spin_unlock_irqrestore(&intr->lock, flags);
482
483 usb_kill_urb(urb);
484 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
485 usb_free_urb(urb);
486}
487
488static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
489 unsigned int length)
490{
491 int i;
492 struct zd_mac *mac = zd_usb_to_mac(usb);
493 const struct rx_length_info *length_info;
494
495 if (length < sizeof(struct rx_length_info)) {
496 /* It's not a complete packet anyhow. */
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000497 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
498 ieee->stats.rx_errors++;
499 ieee->stats.rx_length_errors++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100500 return;
501 }
502 length_info = (struct rx_length_info *)
503 (buffer + length - sizeof(struct rx_length_info));
504
505 /* It might be that three frames are merged into a single URB
506 * transaction. We have to check for the length info tag.
507 *
508 * While testing we discovered that length_info might be unaligned,
509 * because if USB transactions are merged, the last packet will not
510 * be padded. Unaligned access might also happen if the length_info
511 * structure is not present.
512 */
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200513 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
514 {
Daniel Drakee85d0912006-06-02 17:11:32 +0100515 unsigned int l, k, n;
516 for (i = 0, l = 0;; i++) {
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200517 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
Ulrich Kunitz850c2112006-11-22 00:06:19 +0000518 if (k == 0)
519 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100520 n = l+k;
521 if (n > length)
522 return;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800523 zd_mac_rx_irq(mac, buffer+l, k);
Daniel Drakee85d0912006-06-02 17:11:32 +0100524 if (i >= 2)
525 return;
526 l = (n+3) & ~3;
527 }
528 } else {
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800529 zd_mac_rx_irq(mac, buffer, length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100530 }
531}
532
David Howells7d12e782006-10-05 14:55:46 +0100533static void rx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100534{
535 struct zd_usb *usb;
536 struct zd_usb_rx *rx;
537 const u8 *buffer;
538 unsigned int length;
539
540 switch (urb->status) {
541 case 0:
542 break;
543 case -ESHUTDOWN:
544 case -EINVAL:
545 case -ENODEV:
546 case -ENOENT:
547 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100548 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100549 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100550 default:
551 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
552 goto resubmit;
553 }
554
555 buffer = urb->transfer_buffer;
556 length = urb->actual_length;
557 usb = urb->context;
558 rx = &usb->rx;
559
560 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
561 /* If there is an old first fragment, we don't care. */
562 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
563 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
564 spin_lock(&rx->lock);
565 memcpy(rx->fragment, buffer, length);
566 rx->fragment_length = length;
567 spin_unlock(&rx->lock);
568 goto resubmit;
569 }
570
571 spin_lock(&rx->lock);
572 if (rx->fragment_length > 0) {
573 /* We are on a second fragment, we believe */
574 ZD_ASSERT(length + rx->fragment_length <=
575 ARRAY_SIZE(rx->fragment));
576 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
577 memcpy(rx->fragment+rx->fragment_length, buffer, length);
578 handle_rx_packet(usb, rx->fragment,
579 rx->fragment_length + length);
580 rx->fragment_length = 0;
581 spin_unlock(&rx->lock);
582 } else {
583 spin_unlock(&rx->lock);
584 handle_rx_packet(usb, buffer, length);
585 }
586
587resubmit:
588 usb_submit_urb(urb, GFP_ATOMIC);
589}
590
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100591static struct urb *alloc_urb(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100592{
593 struct usb_device *udev = zd_usb_to_usbdev(usb);
594 struct urb *urb;
595 void *buffer;
596
597 urb = usb_alloc_urb(0, GFP_NOFS);
598 if (!urb)
599 return NULL;
600 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS,
601 &urb->transfer_dma);
602 if (!buffer) {
603 usb_free_urb(urb);
604 return NULL;
605 }
606
607 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
608 buffer, USB_MAX_RX_SIZE,
609 rx_urb_complete, usb);
610 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
611
612 return urb;
613}
614
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100615static void free_urb(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100616{
617 if (!urb)
618 return;
619 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
620 urb->transfer_buffer, urb->transfer_dma);
621 usb_free_urb(urb);
622}
623
624int zd_usb_enable_rx(struct zd_usb *usb)
625{
626 int i, r;
627 struct zd_usb_rx *rx = &usb->rx;
628 struct urb **urbs;
629
630 dev_dbg_f(zd_usb_dev(usb), "\n");
631
632 r = -ENOMEM;
633 urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS);
634 if (!urbs)
635 goto error;
636 for (i = 0; i < URBS_COUNT; i++) {
637 urbs[i] = alloc_urb(usb);
638 if (!urbs[i])
639 goto error;
640 }
641
642 ZD_ASSERT(!irqs_disabled());
643 spin_lock_irq(&rx->lock);
644 if (rx->urbs) {
645 spin_unlock_irq(&rx->lock);
646 r = 0;
647 goto error;
648 }
649 rx->urbs = urbs;
650 rx->urbs_count = URBS_COUNT;
651 spin_unlock_irq(&rx->lock);
652
653 for (i = 0; i < URBS_COUNT; i++) {
654 r = usb_submit_urb(urbs[i], GFP_NOFS);
655 if (r)
656 goto error_submit;
657 }
658
659 return 0;
660error_submit:
661 for (i = 0; i < URBS_COUNT; i++) {
662 usb_kill_urb(urbs[i]);
663 }
664 spin_lock_irq(&rx->lock);
665 rx->urbs = NULL;
666 rx->urbs_count = 0;
667 spin_unlock_irq(&rx->lock);
668error:
669 if (urbs) {
670 for (i = 0; i < URBS_COUNT; i++)
671 free_urb(urbs[i]);
672 }
673 return r;
674}
675
676void zd_usb_disable_rx(struct zd_usb *usb)
677{
678 int i;
679 unsigned long flags;
680 struct urb **urbs;
681 unsigned int count;
682 struct zd_usb_rx *rx = &usb->rx;
683
684 spin_lock_irqsave(&rx->lock, flags);
685 urbs = rx->urbs;
686 count = rx->urbs_count;
687 spin_unlock_irqrestore(&rx->lock, flags);
688 if (!urbs)
689 return;
690
691 for (i = 0; i < count; i++) {
692 usb_kill_urb(urbs[i]);
693 free_urb(urbs[i]);
694 }
695 kfree(urbs);
696
697 spin_lock_irqsave(&rx->lock, flags);
698 rx->urbs = NULL;
699 rx->urbs_count = 0;
700 spin_unlock_irqrestore(&rx->lock, flags);
701}
702
David Howells7d12e782006-10-05 14:55:46 +0100703static void tx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100704{
705 int r;
706
707 switch (urb->status) {
708 case 0:
709 break;
710 case -ESHUTDOWN:
711 case -EINVAL:
712 case -ENODEV:
713 case -ENOENT:
714 case -ECONNRESET:
Daniel Drakeb312d792006-07-05 15:57:39 +0100715 case -EPIPE:
Daniel Drakee85d0912006-06-02 17:11:32 +0100716 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
717 break;
Daniel Drakee85d0912006-06-02 17:11:32 +0100718 default:
719 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
720 goto resubmit;
721 }
722free_urb:
723 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
724 urb->transfer_buffer, urb->transfer_dma);
725 usb_free_urb(urb);
726 return;
727resubmit:
728 r = usb_submit_urb(urb, GFP_ATOMIC);
729 if (r) {
730 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
731 goto free_urb;
732 }
733}
734
735/* Puts the frame on the USB endpoint. It doesn't wait for
736 * completion. The frame must contain the control set.
737 */
738int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
739{
740 int r;
741 struct usb_device *udev = zd_usb_to_usbdev(usb);
742 struct urb *urb;
743 void *buffer;
744
745 urb = usb_alloc_urb(0, GFP_ATOMIC);
746 if (!urb) {
747 r = -ENOMEM;
748 goto out;
749 }
750
751 buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
752 &urb->transfer_dma);
753 if (!buffer) {
754 r = -ENOMEM;
755 goto error_free_urb;
756 }
757 memcpy(buffer, frame, length);
758
759 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
760 buffer, length, tx_urb_complete, NULL);
761 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
762
763 r = usb_submit_urb(urb, GFP_ATOMIC);
764 if (r)
765 goto error;
766 return 0;
767error:
768 usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
769 urb->transfer_dma);
770error_free_urb:
771 usb_free_urb(urb);
772out:
773 return r;
774}
775
776static inline void init_usb_interrupt(struct zd_usb *usb)
777{
778 struct zd_usb_interrupt *intr = &usb->intr;
779
780 spin_lock_init(&intr->lock);
781 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
782 init_completion(&intr->read_regs.completion);
Daniel Drake0ce34bc2006-12-12 01:26:11 +0000783 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100784}
785
786static inline void init_usb_rx(struct zd_usb *usb)
787{
788 struct zd_usb_rx *rx = &usb->rx;
789 spin_lock_init(&rx->lock);
790 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
791 rx->usb_packet_size = 512;
792 } else {
793 rx->usb_packet_size = 64;
794 }
795 ZD_ASSERT(rx->fragment_length == 0);
796}
797
798static inline void init_usb_tx(struct zd_usb *usb)
799{
800 /* FIXME: at this point we will allocate a fixed number of urb's for
801 * use in a cyclic scheme */
802}
803
804void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
805 struct usb_interface *intf)
806{
807 memset(usb, 0, sizeof(*usb));
808 usb->intf = usb_get_intf(intf);
809 usb_set_intfdata(usb->intf, netdev);
810 init_usb_interrupt(usb);
811 init_usb_tx(usb);
812 init_usb_rx(usb);
813}
814
Daniel Drakee85d0912006-06-02 17:11:32 +0100815void zd_usb_clear(struct zd_usb *usb)
816{
817 usb_set_intfdata(usb->intf, NULL);
818 usb_put_intf(usb->intf);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100819 ZD_MEMCLEAR(usb, sizeof(*usb));
Daniel Drakee85d0912006-06-02 17:11:32 +0100820 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
821}
822
823static const char *speed(enum usb_device_speed speed)
824{
825 switch (speed) {
826 case USB_SPEED_LOW:
827 return "low";
828 case USB_SPEED_FULL:
829 return "full";
830 case USB_SPEED_HIGH:
831 return "high";
832 default:
833 return "unknown speed";
834 }
835}
836
837static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
838{
839 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
840 le16_to_cpu(udev->descriptor.idVendor),
841 le16_to_cpu(udev->descriptor.idProduct),
842 get_bcdDevice(udev),
843 speed(udev->speed));
844}
845
846int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
847{
848 struct usb_device *udev = interface_to_usbdev(usb->intf);
849 return scnprint_id(udev, buffer, size);
850}
851
852#ifdef DEBUG
853static void print_id(struct usb_device *udev)
854{
855 char buffer[40];
856
857 scnprint_id(udev, buffer, sizeof(buffer));
858 buffer[sizeof(buffer)-1] = 0;
859 dev_dbg_f(&udev->dev, "%s\n", buffer);
860}
861#else
862#define print_id(udev) do { } while (0)
863#endif
864
Daniel Drakea1030e922006-08-13 12:15:29 +0100865static int eject_installer(struct usb_interface *intf)
866{
867 struct usb_device *udev = interface_to_usbdev(intf);
868 struct usb_host_interface *iface_desc = &intf->altsetting[0];
869 struct usb_endpoint_descriptor *endpoint;
870 unsigned char *cmd;
871 u8 bulk_out_ep;
872 int r;
873
874 /* Find bulk out endpoint */
875 endpoint = &iface_desc->endpoint[1].desc;
876 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
877 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
878 USB_ENDPOINT_XFER_BULK) {
879 bulk_out_ep = endpoint->bEndpointAddress;
880 } else {
881 dev_err(&udev->dev,
882 "zd1211rw: Could not find bulk out endpoint\n");
883 return -ENODEV;
884 }
885
886 cmd = kzalloc(31, GFP_KERNEL);
887 if (cmd == NULL)
888 return -ENODEV;
889
890 /* USB bulk command block */
891 cmd[0] = 0x55; /* bulk command signature */
892 cmd[1] = 0x53; /* bulk command signature */
893 cmd[2] = 0x42; /* bulk command signature */
894 cmd[3] = 0x43; /* bulk command signature */
895 cmd[14] = 6; /* command length */
896
897 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
898 cmd[19] = 0x2; /* eject disc */
899
900 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
901 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
902 cmd, 31, NULL, 2000);
903 kfree(cmd);
904 if (r)
905 return r;
906
907 /* At this point, the device disconnects and reconnects with the real
908 * ID numbers. */
909
910 usb_set_intfdata(intf, NULL);
911 return 0;
912}
913
Daniel Drakee85d0912006-06-02 17:11:32 +0100914static int probe(struct usb_interface *intf, const struct usb_device_id *id)
915{
916 int r;
917 struct usb_device *udev = interface_to_usbdev(intf);
918 struct net_device *netdev = NULL;
919
920 print_id(udev);
921
Daniel Drakea1030e922006-08-13 12:15:29 +0100922 if (id->driver_info & DEVICE_INSTALLER)
923 return eject_installer(intf);
924
Daniel Drakee85d0912006-06-02 17:11:32 +0100925 switch (udev->speed) {
926 case USB_SPEED_LOW:
927 case USB_SPEED_FULL:
928 case USB_SPEED_HIGH:
929 break;
930 default:
931 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
932 r = -ENODEV;
933 goto error;
934 }
935
Ulrich Kunitz6e3632f2007-01-29 00:59:28 +0000936 usb_reset_device(interface_to_usbdev(intf));
937
Daniel Drakee85d0912006-06-02 17:11:32 +0100938 netdev = zd_netdev_alloc(intf);
939 if (netdev == NULL) {
940 r = -ENOMEM;
941 goto error;
942 }
943
944 r = upload_firmware(udev, id->driver_info);
945 if (r) {
946 dev_err(&intf->dev,
947 "couldn't load firmware. Error number %d\n", r);
948 goto error;
949 }
950
951 r = usb_reset_configuration(udev);
952 if (r) {
953 dev_dbg_f(&intf->dev,
954 "couldn't reset configuration. Error number %d\n", r);
955 goto error;
956 }
957
958 /* At this point the interrupt endpoint is not generally enabled. We
959 * save the USB bandwidth until the network device is opened. But
960 * notify that the initialization of the MAC will require the
961 * interrupts to be temporary enabled.
962 */
963 r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info);
964 if (r) {
965 dev_dbg_f(&intf->dev,
966 "couldn't initialize mac. Error number %d\n", r);
967 goto error;
968 }
969
970 r = register_netdev(netdev);
971 if (r) {
972 dev_dbg_f(&intf->dev,
973 "couldn't register netdev. Error number %d\n", r);
974 goto error;
975 }
976
977 dev_dbg_f(&intf->dev, "successful\n");
978 dev_info(&intf->dev,"%s\n", netdev->name);
979 return 0;
980error:
981 usb_reset_device(interface_to_usbdev(intf));
982 zd_netdev_free(netdev);
983 return r;
984}
985
986static void disconnect(struct usb_interface *intf)
987{
988 struct net_device *netdev = zd_intf_to_netdev(intf);
989 struct zd_mac *mac = zd_netdev_mac(netdev);
990 struct zd_usb *usb = &mac->chip.usb;
991
Daniel Drakea1030e922006-08-13 12:15:29 +0100992 /* Either something really bad happened, or we're just dealing with
993 * a DEVICE_INSTALLER. */
994 if (netdev == NULL)
995 return;
996
Daniel Drakee85d0912006-06-02 17:11:32 +0100997 dev_dbg_f(zd_usb_dev(usb), "\n");
998
999 zd_netdev_disconnect(netdev);
1000
1001 /* Just in case something has gone wrong! */
1002 zd_usb_disable_rx(usb);
1003 zd_usb_disable_int(usb);
1004
1005 /* If the disconnect has been caused by a removal of the
1006 * driver module, the reset allows reloading of the driver. If the
1007 * reset will not be executed here, the upload of the firmware in the
1008 * probe function caused by the reloading of the driver will fail.
1009 */
1010 usb_reset_device(interface_to_usbdev(intf));
1011
Daniel Drakee85d0912006-06-02 17:11:32 +01001012 zd_netdev_free(netdev);
1013 dev_dbg(&intf->dev, "disconnected\n");
1014}
1015
1016static struct usb_driver driver = {
1017 .name = "zd1211rw",
1018 .id_table = usb_ids,
1019 .probe = probe,
1020 .disconnect = disconnect,
1021};
1022
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001023struct workqueue_struct *zd_workqueue;
1024
Daniel Drakee85d0912006-06-02 17:11:32 +01001025static int __init usb_init(void)
1026{
1027 int r;
1028
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001029 pr_debug("%s usb_init()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001030
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001031 zd_workqueue = create_singlethread_workqueue(driver.name);
1032 if (zd_workqueue == NULL) {
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001033 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001034 return -ENOMEM;
1035 }
1036
Daniel Drakee85d0912006-06-02 17:11:32 +01001037 r = usb_register(&driver);
1038 if (r) {
Maxime Austruy192b7752007-01-29 00:59:51 +00001039 destroy_workqueue(zd_workqueue);
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001040 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1041 driver.name, r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001042 return r;
1043 }
1044
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001045 pr_debug("%s initialized\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001046 return 0;
1047}
1048
1049static void __exit usb_exit(void)
1050{
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001051 pr_debug("%s usb_exit()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001052 usb_deregister(&driver);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001053 destroy_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +01001054}
1055
1056module_init(usb_init);
1057module_exit(usb_exit);
1058
1059static int usb_int_regs_length(unsigned int count)
1060{
1061 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1062}
1063
1064static void prepare_read_regs_int(struct zd_usb *usb)
1065{
1066 struct zd_usb_interrupt *intr = &usb->intr;
1067
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001068 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001069 intr->read_regs_enabled = 1;
1070 INIT_COMPLETION(intr->read_regs.completion);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001071 spin_unlock_irq(&intr->lock);
1072}
1073
1074static void disable_read_regs_int(struct zd_usb *usb)
1075{
1076 struct zd_usb_interrupt *intr = &usb->intr;
1077
1078 spin_lock_irq(&intr->lock);
1079 intr->read_regs_enabled = 0;
1080 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001081}
1082
1083static int get_results(struct zd_usb *usb, u16 *values,
1084 struct usb_req_read_regs *req, unsigned int count)
1085{
1086 int r;
1087 int i;
1088 struct zd_usb_interrupt *intr = &usb->intr;
1089 struct read_regs_int *rr = &intr->read_regs;
1090 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1091
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001092 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001093
1094 r = -EIO;
1095 /* The created block size seems to be larger than expected.
1096 * However results appear to be correct.
1097 */
1098 if (rr->length < usb_int_regs_length(count)) {
1099 dev_dbg_f(zd_usb_dev(usb),
1100 "error: actual length %d less than expected %d\n",
1101 rr->length, usb_int_regs_length(count));
1102 goto error_unlock;
1103 }
1104 if (rr->length > sizeof(rr->buffer)) {
1105 dev_dbg_f(zd_usb_dev(usb),
1106 "error: actual length %d exceeds buffer size %zu\n",
1107 rr->length, sizeof(rr->buffer));
1108 goto error_unlock;
1109 }
1110
1111 for (i = 0; i < count; i++) {
1112 struct reg_data *rd = &regs->regs[i];
1113 if (rd->addr != req->addr[i]) {
1114 dev_dbg_f(zd_usb_dev(usb),
1115 "rd[%d] addr %#06hx expected %#06hx\n", i,
1116 le16_to_cpu(rd->addr),
1117 le16_to_cpu(req->addr[i]));
1118 goto error_unlock;
1119 }
1120 values[i] = le16_to_cpu(rd->value);
1121 }
1122
1123 r = 0;
1124error_unlock:
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001125 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001126 return r;
1127}
1128
1129int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1130 const zd_addr_t *addresses, unsigned int count)
1131{
1132 int r;
1133 int i, req_len, actual_req_len;
1134 struct usb_device *udev;
1135 struct usb_req_read_regs *req = NULL;
1136 unsigned long timeout;
1137
1138 if (count < 1) {
1139 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1140 return -EINVAL;
1141 }
1142 if (count > USB_MAX_IOREAD16_COUNT) {
1143 dev_dbg_f(zd_usb_dev(usb),
1144 "error: count %u exceeds possible max %u\n",
1145 count, USB_MAX_IOREAD16_COUNT);
1146 return -EINVAL;
1147 }
1148 if (in_atomic()) {
1149 dev_dbg_f(zd_usb_dev(usb),
1150 "error: io in atomic context not supported\n");
1151 return -EWOULDBLOCK;
1152 }
1153 if (!usb_int_enabled(usb)) {
1154 dev_dbg_f(zd_usb_dev(usb),
1155 "error: usb interrupt not enabled\n");
1156 return -EWOULDBLOCK;
1157 }
1158
1159 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1160 req = kmalloc(req_len, GFP_NOFS);
1161 if (!req)
1162 return -ENOMEM;
1163 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1164 for (i = 0; i < count; i++)
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001165 req->addr[i] = cpu_to_le16((u16)addresses[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +01001166
1167 udev = zd_usb_to_usbdev(usb);
1168 prepare_read_regs_int(usb);
1169 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1170 req, req_len, &actual_req_len, 1000 /* ms */);
1171 if (r) {
1172 dev_dbg_f(zd_usb_dev(usb),
1173 "error in usb_bulk_msg(). Error number %d\n", r);
1174 goto error;
1175 }
1176 if (req_len != actual_req_len) {
1177 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1178 " req_len %d != actual_req_len %d\n",
1179 req_len, actual_req_len);
1180 r = -EIO;
1181 goto error;
1182 }
1183
1184 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1185 msecs_to_jiffies(1000));
1186 if (!timeout) {
1187 disable_read_regs_int(usb);
1188 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1189 r = -ETIMEDOUT;
1190 goto error;
1191 }
1192
1193 r = get_results(usb, values, req, count);
1194error:
1195 kfree(req);
1196 return r;
1197}
1198
1199int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1200 unsigned int count)
1201{
1202 int r;
1203 struct usb_device *udev;
1204 struct usb_req_write_regs *req = NULL;
1205 int i, req_len, actual_req_len;
1206
1207 if (count == 0)
1208 return 0;
1209 if (count > USB_MAX_IOWRITE16_COUNT) {
1210 dev_dbg_f(zd_usb_dev(usb),
1211 "error: count %u exceeds possible max %u\n",
1212 count, USB_MAX_IOWRITE16_COUNT);
1213 return -EINVAL;
1214 }
1215 if (in_atomic()) {
1216 dev_dbg_f(zd_usb_dev(usb),
1217 "error: io in atomic context not supported\n");
1218 return -EWOULDBLOCK;
1219 }
1220
1221 req_len = sizeof(struct usb_req_write_regs) +
1222 count * sizeof(struct reg_data);
1223 req = kmalloc(req_len, GFP_NOFS);
1224 if (!req)
1225 return -ENOMEM;
1226
1227 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1228 for (i = 0; i < count; i++) {
1229 struct reg_data *rw = &req->reg_writes[i];
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001230 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
Daniel Drakee85d0912006-06-02 17:11:32 +01001231 rw->value = cpu_to_le16(ioreqs[i].value);
1232 }
1233
1234 udev = zd_usb_to_usbdev(usb);
1235 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1236 req, req_len, &actual_req_len, 1000 /* ms */);
1237 if (r) {
1238 dev_dbg_f(zd_usb_dev(usb),
1239 "error in usb_bulk_msg(). Error number %d\n", r);
1240 goto error;
1241 }
1242 if (req_len != actual_req_len) {
1243 dev_dbg_f(zd_usb_dev(usb),
1244 "error in usb_bulk_msg()"
1245 " req_len %d != actual_req_len %d\n",
1246 req_len, actual_req_len);
1247 r = -EIO;
1248 goto error;
1249 }
1250
1251 /* FALL-THROUGH with r == 0 */
1252error:
1253 kfree(req);
1254 return r;
1255}
1256
1257int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1258{
1259 int r;
1260 struct usb_device *udev;
1261 struct usb_req_rfwrite *req = NULL;
1262 int i, req_len, actual_req_len;
1263 u16 bit_value_template;
1264
1265 if (in_atomic()) {
1266 dev_dbg_f(zd_usb_dev(usb),
1267 "error: io in atomic context not supported\n");
1268 return -EWOULDBLOCK;
1269 }
1270 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1271 dev_dbg_f(zd_usb_dev(usb),
1272 "error: bits %d are smaller than"
1273 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1274 bits, USB_MIN_RFWRITE_BIT_COUNT);
1275 return -EINVAL;
1276 }
1277 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1278 dev_dbg_f(zd_usb_dev(usb),
1279 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1280 bits, USB_MAX_RFWRITE_BIT_COUNT);
1281 return -EINVAL;
1282 }
1283#ifdef DEBUG
1284 if (value & (~0UL << bits)) {
1285 dev_dbg_f(zd_usb_dev(usb),
1286 "error: value %#09x has bits >= %d set\n",
1287 value, bits);
1288 return -EINVAL;
1289 }
1290#endif /* DEBUG */
1291
1292 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1293
1294 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1295 if (r) {
1296 dev_dbg_f(zd_usb_dev(usb),
1297 "error %d: Couldn't read CR203\n", r);
1298 goto out;
1299 }
1300 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1301
1302 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1303 req = kmalloc(req_len, GFP_NOFS);
1304 if (!req)
1305 return -ENOMEM;
1306
1307 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1308 /* 1: 3683a, but not used in ZYDAS driver */
1309 req->value = cpu_to_le16(2);
1310 req->bits = cpu_to_le16(bits);
1311
1312 for (i = 0; i < bits; i++) {
1313 u16 bv = bit_value_template;
1314 if (value & (1 << (bits-1-i)))
1315 bv |= RF_DATA;
1316 req->bit_values[i] = cpu_to_le16(bv);
1317 }
1318
1319 udev = zd_usb_to_usbdev(usb);
1320 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1321 req, req_len, &actual_req_len, 1000 /* ms */);
1322 if (r) {
1323 dev_dbg_f(zd_usb_dev(usb),
1324 "error in usb_bulk_msg(). Error number %d\n", r);
1325 goto out;
1326 }
1327 if (req_len != actual_req_len) {
1328 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1329 " req_len %d != actual_req_len %d\n",
1330 req_len, actual_req_len);
1331 r = -EIO;
1332 goto out;
1333 }
1334
1335 /* FALL-THROUGH with r == 0 */
1336out:
1337 kfree(req);
1338 return r;
1339}