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