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