blob: b0684f965761fec054bfc2ffeab014c1e329d810 [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
Daniel Draked066c212006-08-12 17:59:59 +010018#include <linux/kernel.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010019#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/firmware.h>
22#include <linux/device.h>
23#include <linux/errno.h>
24#include <linux/skbuff.h>
25#include <linux/usb.h>
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +010026#include <linux/workqueue.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010027#include <net/ieee80211.h>
Al Viroc2487252007-07-15 21:00:21 +010028#include <asm/unaligned.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010029
30#include "zd_def.h"
31#include "zd_netdev.h"
32#include "zd_mac.h"
33#include "zd_usb.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010034
35static struct usb_device_id usb_ids[] = {
36 /* ZD1211 */
37 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
38 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
39 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010042 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010043 { 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 Drakea9eea9a2007-05-24 01:06:29 +010056 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
Ulrich Kunitzababda02007-09-01 22:40:32 +010057 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010058 /* ZD1211B */
59 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
60 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
Daniel Drakedd2f5532006-07-05 15:57:31 +010061 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz943599e2006-08-12 18:00:05 +010062 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e872952006-11-17 00:26:40 +000063 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
Daniel Drakea2bdcc62006-12-12 01:25:37 +000064 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
65 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
Daniel Drake33218ba2006-12-30 22:38:23 +000066 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
Daniel Drake2e9b2462007-01-04 03:33:54 +000067 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
Daniel Drakeaec91022007-03-11 19:54:39 +000068 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
S.Çağlar Onurc445a312007-04-22 00:52:48 +030070 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
Daniel Drake5d082fe2007-04-07 16:00:43 +010071 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
Daniel Drake187bb412007-05-01 17:14:51 +010072 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz019a6752007-05-01 17:13:51 +010073 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
Daniel Drake93f510b2007-07-01 18:22:21 +010074 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
Masakazu Mokuno6eb07252007-07-14 01:40:11 +010075 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
Daniel Drake25343462007-07-14 01:40:54 +010076 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e97afe2007-08-06 01:24:19 +010077 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
Daniel Drake475fed12007-08-06 02:25:18 +010078 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
Daniel Drake61ef6062007-10-07 16:24:26 +010079 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
Daniel Drakea1030e922006-08-13 12:15:29 +010080 /* "Driverless" devices that need ejecting */
81 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010082 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
Daniel Drakee85d0912006-06-02 17:11:32 +010083 {}
84};
85
86MODULE_LICENSE("GPL");
87MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
88MODULE_AUTHOR("Ulrich Kunitz");
89MODULE_AUTHOR("Daniel Drake");
90MODULE_VERSION("1.0");
91MODULE_DEVICE_TABLE(usb, usb_ids);
92
93#define FW_ZD1211_PREFIX "zd1211/zd1211_"
94#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
95
Daniel Drakee85d0912006-06-02 17:11:32 +010096/* USB device initialization */
97
98static int request_fw_file(
99 const struct firmware **fw, const char *name, struct device *device)
100{
101 int r;
102
103 dev_dbg_f(device, "fw name %s\n", name);
104
105 r = request_firmware(fw, name, device);
106 if (r)
107 dev_err(device,
108 "Could not load firmware file %s. Error number %d\n",
109 name, r);
110 return r;
111}
112
113static inline u16 get_bcdDevice(const struct usb_device *udev)
114{
115 return le16_to_cpu(udev->descriptor.bcdDevice);
116}
117
118enum upload_code_flags {
119 REBOOT = 1,
120};
121
122/* Ensures that MAX_TRANSFER_SIZE is even. */
123#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
124
125static int upload_code(struct usb_device *udev,
126 const u8 *data, size_t size, u16 code_offset, int flags)
127{
128 u8 *p;
129 int r;
130
131 /* USB request blocks need "kmalloced" buffers.
132 */
133 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
134 if (!p) {
135 dev_err(&udev->dev, "out of memory\n");
136 r = -ENOMEM;
137 goto error;
138 }
139
140 size &= ~1;
141 while (size > 0) {
142 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
143 size : MAX_TRANSFER_SIZE;
144
145 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
146
147 memcpy(p, data, transfer_size);
148 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
149 USB_REQ_FIRMWARE_DOWNLOAD,
150 USB_DIR_OUT | USB_TYPE_VENDOR,
151 code_offset, 0, p, transfer_size, 1000 /* ms */);
152 if (r < 0) {
153 dev_err(&udev->dev,
154 "USB control request for firmware upload"
155 " failed. Error number %d\n", r);
156 goto error;
157 }
158 transfer_size = r & ~1;
159
160 size -= transfer_size;
161 data += transfer_size;
162 code_offset += transfer_size/sizeof(u16);
163 }
164
165 if (flags & REBOOT) {
166 u8 ret;
167
168 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
169 USB_REQ_FIRMWARE_CONFIRM,
170 USB_DIR_IN | USB_TYPE_VENDOR,
171 0, 0, &ret, sizeof(ret), 5000 /* ms */);
172 if (r != sizeof(ret)) {
173 dev_err(&udev->dev,
174 "control request firmeware confirmation failed."
175 " Return value %d\n", r);
176 if (r >= 0)
177 r = -ENODEV;
178 goto error;
179 }
180 if (ret & 0x80) {
181 dev_err(&udev->dev,
182 "Internal error while downloading."
183 " Firmware confirm return value %#04x\n",
184 (unsigned int)ret);
185 r = -ENODEV;
186 goto error;
187 }
188 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
189 (unsigned int)ret);
190 }
191
192 r = 0;
193error:
194 kfree(p);
195 return r;
196}
197
198static u16 get_word(const void *data, u16 offset)
199{
200 const __le16 *p = data;
201 return le16_to_cpu(p[offset]);
202}
203
Daniel Drake74553ae2007-07-01 18:22:32 +0100204static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
Daniel Drakee85d0912006-06-02 17:11:32 +0100205 const char* postfix)
206{
207 scnprintf(buffer, size, "%s%s",
Daniel Drake74553ae2007-07-01 18:22:32 +0100208 usb->is_zd1211b ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100209 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
210 postfix);
211 return buffer;
212}
213
Daniel Drake74553ae2007-07-01 18:22:32 +0100214static int handle_version_mismatch(struct zd_usb *usb,
Daniel Draked066c212006-08-12 17:59:59 +0100215 const struct firmware *ub_fw)
216{
Daniel Drake74553ae2007-07-01 18:22:32 +0100217 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Draked066c212006-08-12 17:59:59 +0100218 const struct firmware *ur_fw = NULL;
219 int offset;
220 int r = 0;
221 char fw_name[128];
222
223 r = request_fw_file(&ur_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100224 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
Daniel Draked066c212006-08-12 17:59:59 +0100225 &udev->dev);
226 if (r)
227 goto error;
228
Daniel Drakeee302762006-12-12 01:25:52 +0000229 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100230 if (r)
231 goto error;
232
Daniel Drakeee302762006-12-12 01:25:52 +0000233 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
Daniel Draked066c212006-08-12 17:59:59 +0100234 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
Daniel Drakeee302762006-12-12 01:25:52 +0000235 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100236
237 /* At this point, the vendor driver downloads the whole firmware
238 * image, hacks around with version IDs, and uploads it again,
239 * completely overwriting the boot code. We do not do this here as
240 * it is not required on any tested devices, and it is suspected to
241 * cause problems. */
242error:
243 release_firmware(ur_fw);
244 return r;
245}
246
Daniel Drake74553ae2007-07-01 18:22:32 +0100247static int upload_firmware(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100248{
249 int r;
250 u16 fw_bcdDevice;
251 u16 bcdDevice;
Daniel Drake74553ae2007-07-01 18:22:32 +0100252 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100253 const struct firmware *ub_fw = NULL;
254 const struct firmware *uph_fw = NULL;
255 char fw_name[128];
256
257 bcdDevice = get_bcdDevice(udev);
258
259 r = request_fw_file(&ub_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100260 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100261 &udev->dev);
262 if (r)
263 goto error;
264
Daniel Drakeee302762006-12-12 01:25:52 +0000265 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
Daniel Drakee85d0912006-06-02 17:11:32 +0100266
Daniel Drakee85d0912006-06-02 17:11:32 +0100267 if (fw_bcdDevice != bcdDevice) {
268 dev_info(&udev->dev,
Daniel Draked066c212006-08-12 17:59:59 +0100269 "firmware version %#06x and device bootcode version "
270 "%#06x differ\n", fw_bcdDevice, bcdDevice);
271 if (bcdDevice <= 0x4313)
272 dev_warn(&udev->dev, "device has old bootcode, please "
273 "report success or failure\n");
274
Daniel Drake74553ae2007-07-01 18:22:32 +0100275 r = handle_version_mismatch(usb, ub_fw);
Daniel Draked066c212006-08-12 17:59:59 +0100276 if (r)
277 goto error;
Daniel Drakee85d0912006-06-02 17:11:32 +0100278 } else {
279 dev_dbg_f(&udev->dev,
280 "firmware device id %#06x is equal to the "
281 "actual device id\n", fw_bcdDevice);
282 }
283
284
285 r = request_fw_file(&uph_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100286 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100287 &udev->dev);
288 if (r)
289 goto error;
290
Daniel Drakeee302762006-12-12 01:25:52 +0000291 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100292 if (r) {
293 dev_err(&udev->dev,
294 "Could not upload firmware code uph. Error number %d\n",
295 r);
296 }
297
298 /* FALL-THROUGH */
299error:
300 release_firmware(ub_fw);
301 release_firmware(uph_fw);
302 return r;
303}
304
Daniel Drake74553ae2007-07-01 18:22:32 +0100305/* Read data from device address space using "firmware interface" which does
306 * not require firmware to be loaded. */
307int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
308{
309 int r;
310 struct usb_device *udev = zd_usb_to_usbdev(usb);
311
312 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
313 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
314 data, len, 5000);
315 if (r < 0) {
316 dev_err(&udev->dev,
317 "read over firmware interface failed: %d\n", r);
318 return r;
319 } else if (r != len) {
320 dev_err(&udev->dev,
321 "incomplete read over firmware interface: %d/%d\n",
322 r, len);
323 return -EIO;
324 }
325
326 return 0;
327}
328
Daniel Drakee85d0912006-06-02 17:11:32 +0100329#define urb_dev(urb) (&(urb)->dev->dev)
330
331static inline void handle_regs_int(struct urb *urb)
332{
333 struct zd_usb *usb = urb->context;
334 struct zd_usb_interrupt *intr = &usb->intr;
335 int len;
336
337 ZD_ASSERT(in_interrupt());
338 spin_lock(&intr->lock);
339
340 if (intr->read_regs_enabled) {
341 intr->read_regs.length = len = urb->actual_length;
342
343 if (len > sizeof(intr->read_regs.buffer))
344 len = sizeof(intr->read_regs.buffer);
345 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
346 intr->read_regs_enabled = 0;
347 complete(&intr->read_regs.completion);
348 goto out;
349 }
350
351 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
352out:
353 spin_unlock(&intr->lock);
354}
355
356static inline void handle_retry_failed_int(struct urb *urb)
357{
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000358 struct zd_usb *usb = urb->context;
359 struct zd_mac *mac = zd_usb_to_mac(usb);
360 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
361
362 ieee->stats.tx_errors++;
363 ieee->ieee_stats.tx_retry_limit_exceeded++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100364 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
365}
366
367
David Howells7d12e782006-10-05 14:55:46 +0100368static void int_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100369{
370 int r;
371 struct usb_int_header *hdr;
372
373 switch (urb->status) {
374 case 0:
375 break;
376 case -ESHUTDOWN:
377 case -EINVAL:
378 case -ENODEV:
379 case -ENOENT:
380 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100381 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100382 goto kfree;
Daniel Drakee85d0912006-06-02 17:11:32 +0100383 default:
384 goto resubmit;
385 }
386
387 if (urb->actual_length < sizeof(hdr)) {
388 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
389 goto resubmit;
390 }
391
392 hdr = urb->transfer_buffer;
393 if (hdr->type != USB_INT_TYPE) {
394 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
395 goto resubmit;
396 }
397
398 switch (hdr->id) {
399 case USB_INT_ID_REGS:
400 handle_regs_int(urb);
401 break;
402 case USB_INT_ID_RETRY_FAILED:
403 handle_retry_failed_int(urb);
404 break;
405 default:
406 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
407 (unsigned int)hdr->id);
408 goto resubmit;
409 }
410
411resubmit:
412 r = usb_submit_urb(urb, GFP_ATOMIC);
413 if (r) {
414 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
415 goto kfree;
416 }
417 return;
418kfree:
419 kfree(urb->transfer_buffer);
420}
421
422static inline int int_urb_interval(struct usb_device *udev)
423{
424 switch (udev->speed) {
425 case USB_SPEED_HIGH:
426 return 4;
427 case USB_SPEED_LOW:
428 return 10;
429 case USB_SPEED_FULL:
430 default:
431 return 1;
432 }
433}
434
435static inline int usb_int_enabled(struct zd_usb *usb)
436{
437 unsigned long flags;
438 struct zd_usb_interrupt *intr = &usb->intr;
439 struct urb *urb;
440
441 spin_lock_irqsave(&intr->lock, flags);
442 urb = intr->urb;
443 spin_unlock_irqrestore(&intr->lock, flags);
444 return urb != NULL;
445}
446
447int zd_usb_enable_int(struct zd_usb *usb)
448{
449 int r;
450 struct usb_device *udev;
451 struct zd_usb_interrupt *intr = &usb->intr;
452 void *transfer_buffer = NULL;
453 struct urb *urb;
454
455 dev_dbg_f(zd_usb_dev(usb), "\n");
456
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000457 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100458 if (!urb) {
459 r = -ENOMEM;
460 goto out;
461 }
462
463 ZD_ASSERT(!irqs_disabled());
464 spin_lock_irq(&intr->lock);
465 if (intr->urb) {
466 spin_unlock_irq(&intr->lock);
467 r = 0;
468 goto error_free_urb;
469 }
470 intr->urb = urb;
471 spin_unlock_irq(&intr->lock);
472
473 /* TODO: make it a DMA buffer */
474 r = -ENOMEM;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000475 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100476 if (!transfer_buffer) {
477 dev_dbg_f(zd_usb_dev(usb),
478 "couldn't allocate transfer_buffer\n");
479 goto error_set_urb_null;
480 }
481
482 udev = zd_usb_to_usbdev(usb);
483 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
484 transfer_buffer, USB_MAX_EP_INT_BUFFER,
485 int_urb_complete, usb,
486 intr->interval);
487
488 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000489 r = usb_submit_urb(urb, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100490 if (r) {
491 dev_dbg_f(zd_usb_dev(usb),
492 "Couldn't submit urb. Error number %d\n", r);
493 goto error;
494 }
495
496 return 0;
497error:
498 kfree(transfer_buffer);
499error_set_urb_null:
500 spin_lock_irq(&intr->lock);
501 intr->urb = NULL;
502 spin_unlock_irq(&intr->lock);
503error_free_urb:
504 usb_free_urb(urb);
505out:
506 return r;
507}
508
509void zd_usb_disable_int(struct zd_usb *usb)
510{
511 unsigned long flags;
512 struct zd_usb_interrupt *intr = &usb->intr;
513 struct urb *urb;
514
515 spin_lock_irqsave(&intr->lock, flags);
516 urb = intr->urb;
517 if (!urb) {
518 spin_unlock_irqrestore(&intr->lock, flags);
519 return;
520 }
521 intr->urb = NULL;
522 spin_unlock_irqrestore(&intr->lock, flags);
523
524 usb_kill_urb(urb);
525 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
526 usb_free_urb(urb);
527}
528
529static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
530 unsigned int length)
531{
532 int i;
533 struct zd_mac *mac = zd_usb_to_mac(usb);
534 const struct rx_length_info *length_info;
535
536 if (length < sizeof(struct rx_length_info)) {
537 /* It's not a complete packet anyhow. */
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000538 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
539 ieee->stats.rx_errors++;
540 ieee->stats.rx_length_errors++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100541 return;
542 }
543 length_info = (struct rx_length_info *)
544 (buffer + length - sizeof(struct rx_length_info));
545
546 /* It might be that three frames are merged into a single URB
547 * transaction. We have to check for the length info tag.
548 *
549 * While testing we discovered that length_info might be unaligned,
550 * because if USB transactions are merged, the last packet will not
551 * be padded. Unaligned access might also happen if the length_info
552 * structure is not present.
553 */
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200554 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
555 {
Daniel Drakee85d0912006-06-02 17:11:32 +0100556 unsigned int l, k, n;
557 for (i = 0, l = 0;; i++) {
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200558 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
Ulrich Kunitz850c2112006-11-22 00:06:19 +0000559 if (k == 0)
560 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100561 n = l+k;
562 if (n > length)
563 return;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800564 zd_mac_rx_irq(mac, buffer+l, k);
Daniel Drakee85d0912006-06-02 17:11:32 +0100565 if (i >= 2)
566 return;
567 l = (n+3) & ~3;
568 }
569 } else {
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800570 zd_mac_rx_irq(mac, buffer, length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100571 }
572}
573
David Howells7d12e782006-10-05 14:55:46 +0100574static void rx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100575{
576 struct zd_usb *usb;
577 struct zd_usb_rx *rx;
578 const u8 *buffer;
579 unsigned int length;
580
581 switch (urb->status) {
582 case 0:
583 break;
584 case -ESHUTDOWN:
585 case -EINVAL:
586 case -ENODEV:
587 case -ENOENT:
588 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100589 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100590 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100591 default:
592 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
593 goto resubmit;
594 }
595
596 buffer = urb->transfer_buffer;
597 length = urb->actual_length;
598 usb = urb->context;
599 rx = &usb->rx;
600
601 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
602 /* If there is an old first fragment, we don't care. */
603 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
604 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
605 spin_lock(&rx->lock);
606 memcpy(rx->fragment, buffer, length);
607 rx->fragment_length = length;
608 spin_unlock(&rx->lock);
609 goto resubmit;
610 }
611
612 spin_lock(&rx->lock);
613 if (rx->fragment_length > 0) {
614 /* We are on a second fragment, we believe */
615 ZD_ASSERT(length + rx->fragment_length <=
616 ARRAY_SIZE(rx->fragment));
617 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
618 memcpy(rx->fragment+rx->fragment_length, buffer, length);
619 handle_rx_packet(usb, rx->fragment,
620 rx->fragment_length + length);
621 rx->fragment_length = 0;
622 spin_unlock(&rx->lock);
623 } else {
624 spin_unlock(&rx->lock);
625 handle_rx_packet(usb, buffer, length);
626 }
627
628resubmit:
629 usb_submit_urb(urb, GFP_ATOMIC);
630}
631
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100632static struct urb *alloc_urb(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100633{
634 struct usb_device *udev = zd_usb_to_usbdev(usb);
635 struct urb *urb;
636 void *buffer;
637
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000638 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100639 if (!urb)
640 return NULL;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000641 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
Daniel Drakee85d0912006-06-02 17:11:32 +0100642 &urb->transfer_dma);
643 if (!buffer) {
644 usb_free_urb(urb);
645 return NULL;
646 }
647
648 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
649 buffer, USB_MAX_RX_SIZE,
650 rx_urb_complete, usb);
651 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
652
653 return urb;
654}
655
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100656static void free_urb(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100657{
658 if (!urb)
659 return;
660 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
661 urb->transfer_buffer, urb->transfer_dma);
662 usb_free_urb(urb);
663}
664
665int zd_usb_enable_rx(struct zd_usb *usb)
666{
667 int i, r;
668 struct zd_usb_rx *rx = &usb->rx;
669 struct urb **urbs;
670
671 dev_dbg_f(zd_usb_dev(usb), "\n");
672
673 r = -ENOMEM;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000674 urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100675 if (!urbs)
676 goto error;
677 for (i = 0; i < URBS_COUNT; i++) {
678 urbs[i] = alloc_urb(usb);
679 if (!urbs[i])
680 goto error;
681 }
682
683 ZD_ASSERT(!irqs_disabled());
684 spin_lock_irq(&rx->lock);
685 if (rx->urbs) {
686 spin_unlock_irq(&rx->lock);
687 r = 0;
688 goto error;
689 }
690 rx->urbs = urbs;
691 rx->urbs_count = URBS_COUNT;
692 spin_unlock_irq(&rx->lock);
693
694 for (i = 0; i < URBS_COUNT; i++) {
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000695 r = usb_submit_urb(urbs[i], GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100696 if (r)
697 goto error_submit;
698 }
699
700 return 0;
701error_submit:
702 for (i = 0; i < URBS_COUNT; i++) {
703 usb_kill_urb(urbs[i]);
704 }
705 spin_lock_irq(&rx->lock);
706 rx->urbs = NULL;
707 rx->urbs_count = 0;
708 spin_unlock_irq(&rx->lock);
709error:
710 if (urbs) {
711 for (i = 0; i < URBS_COUNT; i++)
712 free_urb(urbs[i]);
713 }
714 return r;
715}
716
717void zd_usb_disable_rx(struct zd_usb *usb)
718{
719 int i;
720 unsigned long flags;
721 struct urb **urbs;
722 unsigned int count;
723 struct zd_usb_rx *rx = &usb->rx;
724
725 spin_lock_irqsave(&rx->lock, flags);
726 urbs = rx->urbs;
727 count = rx->urbs_count;
728 spin_unlock_irqrestore(&rx->lock, flags);
729 if (!urbs)
730 return;
731
732 for (i = 0; i < count; i++) {
733 usb_kill_urb(urbs[i]);
734 free_urb(urbs[i]);
735 }
736 kfree(urbs);
737
738 spin_lock_irqsave(&rx->lock, flags);
739 rx->urbs = NULL;
740 rx->urbs_count = 0;
741 spin_unlock_irqrestore(&rx->lock, flags);
742}
743
David Howells7d12e782006-10-05 14:55:46 +0100744static void tx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100745{
746 int r;
747
748 switch (urb->status) {
749 case 0:
750 break;
751 case -ESHUTDOWN:
752 case -EINVAL:
753 case -ENODEV:
754 case -ENOENT:
755 case -ECONNRESET:
Daniel Drakeb312d792006-07-05 15:57:39 +0100756 case -EPIPE:
Daniel Drakee85d0912006-06-02 17:11:32 +0100757 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
758 break;
Daniel Drakee85d0912006-06-02 17:11:32 +0100759 default:
760 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
761 goto resubmit;
762 }
763free_urb:
764 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
765 urb->transfer_buffer, urb->transfer_dma);
766 usb_free_urb(urb);
767 return;
768resubmit:
769 r = usb_submit_urb(urb, GFP_ATOMIC);
770 if (r) {
771 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
772 goto free_urb;
773 }
774}
775
776/* Puts the frame on the USB endpoint. It doesn't wait for
777 * completion. The frame must contain the control set.
778 */
779int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
780{
781 int r;
782 struct usb_device *udev = zd_usb_to_usbdev(usb);
783 struct urb *urb;
784 void *buffer;
785
786 urb = usb_alloc_urb(0, GFP_ATOMIC);
787 if (!urb) {
788 r = -ENOMEM;
789 goto out;
790 }
791
792 buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
793 &urb->transfer_dma);
794 if (!buffer) {
795 r = -ENOMEM;
796 goto error_free_urb;
797 }
798 memcpy(buffer, frame, length);
799
800 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
801 buffer, length, tx_urb_complete, NULL);
802 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
803
804 r = usb_submit_urb(urb, GFP_ATOMIC);
805 if (r)
806 goto error;
807 return 0;
808error:
809 usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
810 urb->transfer_dma);
811error_free_urb:
812 usb_free_urb(urb);
813out:
814 return r;
815}
816
817static inline void init_usb_interrupt(struct zd_usb *usb)
818{
819 struct zd_usb_interrupt *intr = &usb->intr;
820
821 spin_lock_init(&intr->lock);
822 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
823 init_completion(&intr->read_regs.completion);
Daniel Drake0ce34bc2006-12-12 01:26:11 +0000824 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100825}
826
827static inline void init_usb_rx(struct zd_usb *usb)
828{
829 struct zd_usb_rx *rx = &usb->rx;
830 spin_lock_init(&rx->lock);
831 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
832 rx->usb_packet_size = 512;
833 } else {
834 rx->usb_packet_size = 64;
835 }
836 ZD_ASSERT(rx->fragment_length == 0);
837}
838
839static inline void init_usb_tx(struct zd_usb *usb)
840{
841 /* FIXME: at this point we will allocate a fixed number of urb's for
842 * use in a cyclic scheme */
843}
844
845void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
846 struct usb_interface *intf)
847{
848 memset(usb, 0, sizeof(*usb));
849 usb->intf = usb_get_intf(intf);
850 usb_set_intfdata(usb->intf, netdev);
851 init_usb_interrupt(usb);
852 init_usb_tx(usb);
853 init_usb_rx(usb);
854}
855
Daniel Drakee85d0912006-06-02 17:11:32 +0100856void zd_usb_clear(struct zd_usb *usb)
857{
858 usb_set_intfdata(usb->intf, NULL);
859 usb_put_intf(usb->intf);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100860 ZD_MEMCLEAR(usb, sizeof(*usb));
Daniel Drakee85d0912006-06-02 17:11:32 +0100861 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
862}
863
864static const char *speed(enum usb_device_speed speed)
865{
866 switch (speed) {
867 case USB_SPEED_LOW:
868 return "low";
869 case USB_SPEED_FULL:
870 return "full";
871 case USB_SPEED_HIGH:
872 return "high";
873 default:
874 return "unknown speed";
875 }
876}
877
878static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
879{
880 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
881 le16_to_cpu(udev->descriptor.idVendor),
882 le16_to_cpu(udev->descriptor.idProduct),
883 get_bcdDevice(udev),
884 speed(udev->speed));
885}
886
887int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
888{
889 struct usb_device *udev = interface_to_usbdev(usb->intf);
890 return scnprint_id(udev, buffer, size);
891}
892
893#ifdef DEBUG
894static void print_id(struct usb_device *udev)
895{
896 char buffer[40];
897
898 scnprint_id(udev, buffer, sizeof(buffer));
899 buffer[sizeof(buffer)-1] = 0;
900 dev_dbg_f(&udev->dev, "%s\n", buffer);
901}
902#else
903#define print_id(udev) do { } while (0)
904#endif
905
Daniel Drakea1030e922006-08-13 12:15:29 +0100906static int eject_installer(struct usb_interface *intf)
907{
908 struct usb_device *udev = interface_to_usbdev(intf);
909 struct usb_host_interface *iface_desc = &intf->altsetting[0];
910 struct usb_endpoint_descriptor *endpoint;
911 unsigned char *cmd;
912 u8 bulk_out_ep;
913 int r;
914
915 /* Find bulk out endpoint */
916 endpoint = &iface_desc->endpoint[1].desc;
917 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
918 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
919 USB_ENDPOINT_XFER_BULK) {
920 bulk_out_ep = endpoint->bEndpointAddress;
921 } else {
922 dev_err(&udev->dev,
923 "zd1211rw: Could not find bulk out endpoint\n");
924 return -ENODEV;
925 }
926
927 cmd = kzalloc(31, GFP_KERNEL);
928 if (cmd == NULL)
929 return -ENODEV;
930
931 /* USB bulk command block */
932 cmd[0] = 0x55; /* bulk command signature */
933 cmd[1] = 0x53; /* bulk command signature */
934 cmd[2] = 0x42; /* bulk command signature */
935 cmd[3] = 0x43; /* bulk command signature */
936 cmd[14] = 6; /* command length */
937
938 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
939 cmd[19] = 0x2; /* eject disc */
940
941 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
942 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
943 cmd, 31, NULL, 2000);
944 kfree(cmd);
945 if (r)
946 return r;
947
948 /* At this point, the device disconnects and reconnects with the real
949 * ID numbers. */
950
951 usb_set_intfdata(intf, NULL);
952 return 0;
953}
954
Daniel Drake74553ae2007-07-01 18:22:32 +0100955int zd_usb_init_hw(struct zd_usb *usb)
956{
957 int r;
958 struct zd_mac *mac = zd_usb_to_mac(usb);
959
960 dev_dbg_f(zd_usb_dev(usb), "\n");
961
962 r = upload_firmware(usb);
963 if (r) {
964 dev_err(zd_usb_dev(usb),
965 "couldn't load firmware. Error number %d\n", r);
966 return r;
967 }
968
969 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
970 if (r) {
971 dev_dbg_f(zd_usb_dev(usb),
972 "couldn't reset configuration. Error number %d\n", r);
973 return r;
974 }
975
976 r = zd_mac_init_hw(mac);
977 if (r) {
978 dev_dbg_f(zd_usb_dev(usb),
979 "couldn't initialize mac. Error number %d\n", r);
980 return r;
981 }
982
983 usb->initialized = 1;
984 return 0;
985}
986
Daniel Drakee85d0912006-06-02 17:11:32 +0100987static int probe(struct usb_interface *intf, const struct usb_device_id *id)
988{
989 int r;
Daniel Drake74553ae2007-07-01 18:22:32 +0100990 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100991 struct usb_device *udev = interface_to_usbdev(intf);
992 struct net_device *netdev = NULL;
993
994 print_id(udev);
995
Daniel Drakea1030e922006-08-13 12:15:29 +0100996 if (id->driver_info & DEVICE_INSTALLER)
997 return eject_installer(intf);
998
Daniel Drakee85d0912006-06-02 17:11:32 +0100999 switch (udev->speed) {
1000 case USB_SPEED_LOW:
1001 case USB_SPEED_FULL:
1002 case USB_SPEED_HIGH:
1003 break;
1004 default:
1005 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1006 r = -ENODEV;
1007 goto error;
1008 }
1009
Ulrich Kunitz6e3632f2007-01-29 00:59:28 +00001010 usb_reset_device(interface_to_usbdev(intf));
1011
Daniel Drakee85d0912006-06-02 17:11:32 +01001012 netdev = zd_netdev_alloc(intf);
1013 if (netdev == NULL) {
1014 r = -ENOMEM;
1015 goto error;
1016 }
1017
Daniel Drake74553ae2007-07-01 18:22:32 +01001018 usb = &zd_netdev_mac(netdev)->chip.usb;
1019 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001020
Daniel Drake74553ae2007-07-01 18:22:32 +01001021 r = zd_mac_preinit_hw(zd_netdev_mac(netdev));
Daniel Drakee85d0912006-06-02 17:11:32 +01001022 if (r) {
1023 dev_dbg_f(&intf->dev,
1024 "couldn't initialize mac. Error number %d\n", r);
1025 goto error;
1026 }
1027
1028 r = register_netdev(netdev);
1029 if (r) {
1030 dev_dbg_f(&intf->dev,
1031 "couldn't register netdev. Error number %d\n", r);
1032 goto error;
1033 }
1034
1035 dev_dbg_f(&intf->dev, "successful\n");
1036 dev_info(&intf->dev,"%s\n", netdev->name);
1037 return 0;
1038error:
1039 usb_reset_device(interface_to_usbdev(intf));
1040 zd_netdev_free(netdev);
1041 return r;
1042}
1043
1044static void disconnect(struct usb_interface *intf)
1045{
1046 struct net_device *netdev = zd_intf_to_netdev(intf);
1047 struct zd_mac *mac = zd_netdev_mac(netdev);
1048 struct zd_usb *usb = &mac->chip.usb;
1049
Daniel Drakea1030e922006-08-13 12:15:29 +01001050 /* Either something really bad happened, or we're just dealing with
1051 * a DEVICE_INSTALLER. */
1052 if (netdev == NULL)
1053 return;
1054
Daniel Drakee85d0912006-06-02 17:11:32 +01001055 dev_dbg_f(zd_usb_dev(usb), "\n");
1056
1057 zd_netdev_disconnect(netdev);
1058
1059 /* Just in case something has gone wrong! */
1060 zd_usb_disable_rx(usb);
1061 zd_usb_disable_int(usb);
1062
1063 /* If the disconnect has been caused by a removal of the
1064 * driver module, the reset allows reloading of the driver. If the
1065 * reset will not be executed here, the upload of the firmware in the
1066 * probe function caused by the reloading of the driver will fail.
1067 */
1068 usb_reset_device(interface_to_usbdev(intf));
1069
Daniel Drakee85d0912006-06-02 17:11:32 +01001070 zd_netdev_free(netdev);
1071 dev_dbg(&intf->dev, "disconnected\n");
1072}
1073
1074static struct usb_driver driver = {
1075 .name = "zd1211rw",
1076 .id_table = usb_ids,
1077 .probe = probe,
1078 .disconnect = disconnect,
1079};
1080
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001081struct workqueue_struct *zd_workqueue;
1082
Daniel Drakee85d0912006-06-02 17:11:32 +01001083static int __init usb_init(void)
1084{
1085 int r;
1086
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001087 pr_debug("%s usb_init()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001088
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001089 zd_workqueue = create_singlethread_workqueue(driver.name);
1090 if (zd_workqueue == NULL) {
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001091 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001092 return -ENOMEM;
1093 }
1094
Daniel Drakee85d0912006-06-02 17:11:32 +01001095 r = usb_register(&driver);
1096 if (r) {
Maxime Austruy192b7752007-01-29 00:59:51 +00001097 destroy_workqueue(zd_workqueue);
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001098 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1099 driver.name, r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001100 return r;
1101 }
1102
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001103 pr_debug("%s initialized\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001104 return 0;
1105}
1106
1107static void __exit usb_exit(void)
1108{
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001109 pr_debug("%s usb_exit()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001110 usb_deregister(&driver);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001111 destroy_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +01001112}
1113
1114module_init(usb_init);
1115module_exit(usb_exit);
1116
1117static int usb_int_regs_length(unsigned int count)
1118{
1119 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1120}
1121
1122static void prepare_read_regs_int(struct zd_usb *usb)
1123{
1124 struct zd_usb_interrupt *intr = &usb->intr;
1125
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001126 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001127 intr->read_regs_enabled = 1;
1128 INIT_COMPLETION(intr->read_regs.completion);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001129 spin_unlock_irq(&intr->lock);
1130}
1131
1132static void disable_read_regs_int(struct zd_usb *usb)
1133{
1134 struct zd_usb_interrupt *intr = &usb->intr;
1135
1136 spin_lock_irq(&intr->lock);
1137 intr->read_regs_enabled = 0;
1138 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001139}
1140
1141static int get_results(struct zd_usb *usb, u16 *values,
1142 struct usb_req_read_regs *req, unsigned int count)
1143{
1144 int r;
1145 int i;
1146 struct zd_usb_interrupt *intr = &usb->intr;
1147 struct read_regs_int *rr = &intr->read_regs;
1148 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1149
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001150 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001151
1152 r = -EIO;
1153 /* The created block size seems to be larger than expected.
1154 * However results appear to be correct.
1155 */
1156 if (rr->length < usb_int_regs_length(count)) {
1157 dev_dbg_f(zd_usb_dev(usb),
1158 "error: actual length %d less than expected %d\n",
1159 rr->length, usb_int_regs_length(count));
1160 goto error_unlock;
1161 }
1162 if (rr->length > sizeof(rr->buffer)) {
1163 dev_dbg_f(zd_usb_dev(usb),
1164 "error: actual length %d exceeds buffer size %zu\n",
1165 rr->length, sizeof(rr->buffer));
1166 goto error_unlock;
1167 }
1168
1169 for (i = 0; i < count; i++) {
1170 struct reg_data *rd = &regs->regs[i];
1171 if (rd->addr != req->addr[i]) {
1172 dev_dbg_f(zd_usb_dev(usb),
1173 "rd[%d] addr %#06hx expected %#06hx\n", i,
1174 le16_to_cpu(rd->addr),
1175 le16_to_cpu(req->addr[i]));
1176 goto error_unlock;
1177 }
1178 values[i] = le16_to_cpu(rd->value);
1179 }
1180
1181 r = 0;
1182error_unlock:
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001183 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001184 return r;
1185}
1186
1187int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1188 const zd_addr_t *addresses, unsigned int count)
1189{
1190 int r;
1191 int i, req_len, actual_req_len;
1192 struct usb_device *udev;
1193 struct usb_req_read_regs *req = NULL;
1194 unsigned long timeout;
1195
1196 if (count < 1) {
1197 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1198 return -EINVAL;
1199 }
1200 if (count > USB_MAX_IOREAD16_COUNT) {
1201 dev_dbg_f(zd_usb_dev(usb),
1202 "error: count %u exceeds possible max %u\n",
1203 count, USB_MAX_IOREAD16_COUNT);
1204 return -EINVAL;
1205 }
1206 if (in_atomic()) {
1207 dev_dbg_f(zd_usb_dev(usb),
1208 "error: io in atomic context not supported\n");
1209 return -EWOULDBLOCK;
1210 }
1211 if (!usb_int_enabled(usb)) {
1212 dev_dbg_f(zd_usb_dev(usb),
1213 "error: usb interrupt not enabled\n");
1214 return -EWOULDBLOCK;
1215 }
1216
1217 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001218 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001219 if (!req)
1220 return -ENOMEM;
1221 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1222 for (i = 0; i < count; i++)
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001223 req->addr[i] = cpu_to_le16((u16)addresses[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +01001224
1225 udev = zd_usb_to_usbdev(usb);
1226 prepare_read_regs_int(usb);
1227 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1228 req, req_len, &actual_req_len, 1000 /* ms */);
1229 if (r) {
1230 dev_dbg_f(zd_usb_dev(usb),
1231 "error in usb_bulk_msg(). Error number %d\n", r);
1232 goto error;
1233 }
1234 if (req_len != actual_req_len) {
1235 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1236 " req_len %d != actual_req_len %d\n",
1237 req_len, actual_req_len);
1238 r = -EIO;
1239 goto error;
1240 }
1241
1242 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1243 msecs_to_jiffies(1000));
1244 if (!timeout) {
1245 disable_read_regs_int(usb);
1246 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1247 r = -ETIMEDOUT;
1248 goto error;
1249 }
1250
1251 r = get_results(usb, values, req, count);
1252error:
1253 kfree(req);
1254 return r;
1255}
1256
1257int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1258 unsigned int count)
1259{
1260 int r;
1261 struct usb_device *udev;
1262 struct usb_req_write_regs *req = NULL;
1263 int i, req_len, actual_req_len;
1264
1265 if (count == 0)
1266 return 0;
1267 if (count > USB_MAX_IOWRITE16_COUNT) {
1268 dev_dbg_f(zd_usb_dev(usb),
1269 "error: count %u exceeds possible max %u\n",
1270 count, USB_MAX_IOWRITE16_COUNT);
1271 return -EINVAL;
1272 }
1273 if (in_atomic()) {
1274 dev_dbg_f(zd_usb_dev(usb),
1275 "error: io in atomic context not supported\n");
1276 return -EWOULDBLOCK;
1277 }
1278
1279 req_len = sizeof(struct usb_req_write_regs) +
1280 count * sizeof(struct reg_data);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001281 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001282 if (!req)
1283 return -ENOMEM;
1284
1285 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1286 for (i = 0; i < count; i++) {
1287 struct reg_data *rw = &req->reg_writes[i];
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001288 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
Daniel Drakee85d0912006-06-02 17:11:32 +01001289 rw->value = cpu_to_le16(ioreqs[i].value);
1290 }
1291
1292 udev = zd_usb_to_usbdev(usb);
1293 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1294 req, req_len, &actual_req_len, 1000 /* ms */);
1295 if (r) {
1296 dev_dbg_f(zd_usb_dev(usb),
1297 "error in usb_bulk_msg(). Error number %d\n", r);
1298 goto error;
1299 }
1300 if (req_len != actual_req_len) {
1301 dev_dbg_f(zd_usb_dev(usb),
1302 "error in usb_bulk_msg()"
1303 " req_len %d != actual_req_len %d\n",
1304 req_len, actual_req_len);
1305 r = -EIO;
1306 goto error;
1307 }
1308
1309 /* FALL-THROUGH with r == 0 */
1310error:
1311 kfree(req);
1312 return r;
1313}
1314
1315int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1316{
1317 int r;
1318 struct usb_device *udev;
1319 struct usb_req_rfwrite *req = NULL;
1320 int i, req_len, actual_req_len;
1321 u16 bit_value_template;
1322
1323 if (in_atomic()) {
1324 dev_dbg_f(zd_usb_dev(usb),
1325 "error: io in atomic context not supported\n");
1326 return -EWOULDBLOCK;
1327 }
1328 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1329 dev_dbg_f(zd_usb_dev(usb),
1330 "error: bits %d are smaller than"
1331 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1332 bits, USB_MIN_RFWRITE_BIT_COUNT);
1333 return -EINVAL;
1334 }
1335 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1336 dev_dbg_f(zd_usb_dev(usb),
1337 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1338 bits, USB_MAX_RFWRITE_BIT_COUNT);
1339 return -EINVAL;
1340 }
1341#ifdef DEBUG
1342 if (value & (~0UL << bits)) {
1343 dev_dbg_f(zd_usb_dev(usb),
1344 "error: value %#09x has bits >= %d set\n",
1345 value, bits);
1346 return -EINVAL;
1347 }
1348#endif /* DEBUG */
1349
1350 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1351
1352 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1353 if (r) {
1354 dev_dbg_f(zd_usb_dev(usb),
1355 "error %d: Couldn't read CR203\n", r);
1356 goto out;
1357 }
1358 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1359
1360 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001361 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001362 if (!req)
1363 return -ENOMEM;
1364
1365 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1366 /* 1: 3683a, but not used in ZYDAS driver */
1367 req->value = cpu_to_le16(2);
1368 req->bits = cpu_to_le16(bits);
1369
1370 for (i = 0; i < bits; i++) {
1371 u16 bv = bit_value_template;
1372 if (value & (1 << (bits-1-i)))
1373 bv |= RF_DATA;
1374 req->bit_values[i] = cpu_to_le16(bv);
1375 }
1376
1377 udev = zd_usb_to_usbdev(usb);
1378 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1379 req, req_len, &actual_req_len, 1000 /* ms */);
1380 if (r) {
1381 dev_dbg_f(zd_usb_dev(usb),
1382 "error in usb_bulk_msg(). Error number %d\n", r);
1383 goto out;
1384 }
1385 if (req_len != actual_req_len) {
1386 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1387 " req_len %d != actual_req_len %d\n",
1388 req_len, actual_req_len);
1389 r = -EIO;
1390 goto out;
1391 }
1392
1393 /* FALL-THROUGH with r == 0 */
1394out:
1395 kfree(req);
1396 return r;
1397}