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