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