blob: 8941f5eb96c2774cb6a6e2004d237c66eaab7bc7 [file] [log] [blame]
Daniel Drake66bb42f2007-11-19 16:20:12 +00001/* ZD1211 USB-WLAN driver for Linux
2 *
3 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
4 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
Daniel Drakee85d0912006-06-02 17:11:32 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Daniel Draked066c212006-08-12 17:59:59 +010022#include <linux/kernel.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010023#include <linux/init.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010024#include <linux/firmware.h>
25#include <linux/device.h>
26#include <linux/errno.h>
27#include <linux/skbuff.h>
28#include <linux/usb.h>
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +010029#include <linux/workqueue.h>
Daniel Drake459c51a2007-11-19 15:00:29 +000030#include <net/mac80211.h>
Al Viroc2487252007-07-15 21:00:21 +010031#include <asm/unaligned.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010032
33#include "zd_def.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010034#include "zd_mac.h"
35#include "zd_usb.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010036
37static struct usb_device_id usb_ids[] = {
38 /* ZD1211 */
39 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010044 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010045 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
Daniel Drakedd2f5532006-07-05 15:57:31 +010046 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
Daniel Drake1b865492006-08-12 17:59:50 +010047 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
Daniel Drake12f39302006-08-12 17:59:51 +010048 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
Daniel Drake4ceb7e92006-08-12 18:00:03 +010049 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
Daniel Drakefca27142006-08-29 23:49:32 +010050 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
Daniel Drakefc3e39b2006-08-29 23:49:53 +010051 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
Daniel Drakef2423722006-09-26 04:04:38 +010052 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
54 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
Daniel Drakea362bf52006-10-18 00:17:02 +010055 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
Daniel Drake229782a2006-11-17 00:13:48 +000056 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
Daniel Drakea3fa3ab2007-03-26 01:00:12 +010057 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
Daniel Drakea9eea9a2007-05-24 01:06:29 +010058 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
Ulrich Kunitzababda02007-09-01 22:40:32 +010059 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
Daniel Drake269fca02007-11-19 15:29:24 +000060 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
Daniel Drakee85d0912006-06-02 17:11:32 +010061 /* ZD1211B */
62 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
63 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
Daniel Drakedd2f5532006-07-05 15:57:31 +010064 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz943599e2006-08-12 18:00:05 +010065 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e872952006-11-17 00:26:40 +000066 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
Daniel Drakea2bdcc62006-12-12 01:25:37 +000067 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
68 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
Daniel Drake33218ba2006-12-30 22:38:23 +000069 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
Daniel Drake2e9b2462007-01-04 03:33:54 +000070 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
Daniel Drakeaec91022007-03-11 19:54:39 +000071 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
72 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
S.Çağlar Onurc445a312007-04-22 00:52:48 +030073 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
Daniel Drake5d082fe2007-04-07 16:00:43 +010074 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
Daniel Drake187bb412007-05-01 17:14:51 +010075 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
Ulrich Kunitz019a6752007-05-01 17:13:51 +010076 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
Daniel Drake93f510b2007-07-01 18:22:21 +010077 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
Masakazu Mokuno6eb07252007-07-14 01:40:11 +010078 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
Daniel Drake25343462007-07-14 01:40:54 +010079 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
Daniel Drake8e97afe2007-08-06 01:24:19 +010080 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
Daniel Drake475fed12007-08-06 02:25:18 +010081 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
Daniel Drake61ef6062007-10-07 16:24:26 +010082 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
Daniel Drakea1030e92006-08-13 12:15:29 +010083 /* "Driverless" devices that need ejecting */
84 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
Matthew Davidsonaa1d3a12007-05-01 17:14:30 +010085 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
Daniel Drakee85d0912006-06-02 17:11:32 +010086 {}
87};
88
89MODULE_LICENSE("GPL");
90MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
91MODULE_AUTHOR("Ulrich Kunitz");
92MODULE_AUTHOR("Daniel Drake");
93MODULE_VERSION("1.0");
94MODULE_DEVICE_TABLE(usb, usb_ids);
95
96#define FW_ZD1211_PREFIX "zd1211/zd1211_"
97#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
98
Daniel Drakee85d0912006-06-02 17:11:32 +010099/* USB device initialization */
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800100static void int_urb_complete(struct urb *urb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100101
102static int request_fw_file(
103 const struct firmware **fw, const char *name, struct device *device)
104{
105 int r;
106
107 dev_dbg_f(device, "fw name %s\n", name);
108
109 r = request_firmware(fw, name, device);
110 if (r)
111 dev_err(device,
112 "Could not load firmware file %s. Error number %d\n",
113 name, r);
114 return r;
115}
116
117static inline u16 get_bcdDevice(const struct usb_device *udev)
118{
119 return le16_to_cpu(udev->descriptor.bcdDevice);
120}
121
122enum upload_code_flags {
123 REBOOT = 1,
124};
125
126/* Ensures that MAX_TRANSFER_SIZE is even. */
127#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
128
129static int upload_code(struct usb_device *udev,
130 const u8 *data, size_t size, u16 code_offset, int flags)
131{
132 u8 *p;
133 int r;
134
135 /* USB request blocks need "kmalloced" buffers.
136 */
137 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
138 if (!p) {
139 dev_err(&udev->dev, "out of memory\n");
140 r = -ENOMEM;
141 goto error;
142 }
143
144 size &= ~1;
145 while (size > 0) {
146 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
147 size : MAX_TRANSFER_SIZE;
148
149 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
150
151 memcpy(p, data, transfer_size);
152 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
153 USB_REQ_FIRMWARE_DOWNLOAD,
154 USB_DIR_OUT | USB_TYPE_VENDOR,
155 code_offset, 0, p, transfer_size, 1000 /* ms */);
156 if (r < 0) {
157 dev_err(&udev->dev,
158 "USB control request for firmware upload"
159 " failed. Error number %d\n", r);
160 goto error;
161 }
162 transfer_size = r & ~1;
163
164 size -= transfer_size;
165 data += transfer_size;
166 code_offset += transfer_size/sizeof(u16);
167 }
168
169 if (flags & REBOOT) {
170 u8 ret;
171
172 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
173 USB_REQ_FIRMWARE_CONFIRM,
174 USB_DIR_IN | USB_TYPE_VENDOR,
175 0, 0, &ret, sizeof(ret), 5000 /* ms */);
176 if (r != sizeof(ret)) {
177 dev_err(&udev->dev,
178 "control request firmeware confirmation failed."
179 " Return value %d\n", r);
180 if (r >= 0)
181 r = -ENODEV;
182 goto error;
183 }
184 if (ret & 0x80) {
185 dev_err(&udev->dev,
186 "Internal error while downloading."
187 " Firmware confirm return value %#04x\n",
188 (unsigned int)ret);
189 r = -ENODEV;
190 goto error;
191 }
192 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
193 (unsigned int)ret);
194 }
195
196 r = 0;
197error:
198 kfree(p);
199 return r;
200}
201
202static u16 get_word(const void *data, u16 offset)
203{
204 const __le16 *p = data;
205 return le16_to_cpu(p[offset]);
206}
207
Daniel Drake74553ae2007-07-01 18:22:32 +0100208static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
Daniel Drakee85d0912006-06-02 17:11:32 +0100209 const char* postfix)
210{
211 scnprintf(buffer, size, "%s%s",
Daniel Drake74553ae2007-07-01 18:22:32 +0100212 usb->is_zd1211b ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100213 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
214 postfix);
215 return buffer;
216}
217
Daniel Drake74553ae2007-07-01 18:22:32 +0100218static int handle_version_mismatch(struct zd_usb *usb,
Daniel Draked066c212006-08-12 17:59:59 +0100219 const struct firmware *ub_fw)
220{
Daniel Drake74553ae2007-07-01 18:22:32 +0100221 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Draked066c212006-08-12 17:59:59 +0100222 const struct firmware *ur_fw = NULL;
223 int offset;
224 int r = 0;
225 char fw_name[128];
226
227 r = request_fw_file(&ur_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100228 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
Daniel Draked066c212006-08-12 17:59:59 +0100229 &udev->dev);
230 if (r)
231 goto error;
232
Daniel Drakeee302762006-12-12 01:25:52 +0000233 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100234 if (r)
235 goto error;
236
Daniel Drakeee302762006-12-12 01:25:52 +0000237 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
Daniel Draked066c212006-08-12 17:59:59 +0100238 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
Daniel Drakeee302762006-12-12 01:25:52 +0000239 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
Daniel Draked066c212006-08-12 17:59:59 +0100240
241 /* At this point, the vendor driver downloads the whole firmware
242 * image, hacks around with version IDs, and uploads it again,
243 * completely overwriting the boot code. We do not do this here as
244 * it is not required on any tested devices, and it is suspected to
245 * cause problems. */
246error:
247 release_firmware(ur_fw);
248 return r;
249}
250
Daniel Drake74553ae2007-07-01 18:22:32 +0100251static int upload_firmware(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100252{
253 int r;
254 u16 fw_bcdDevice;
255 u16 bcdDevice;
Daniel Drake74553ae2007-07-01 18:22:32 +0100256 struct usb_device *udev = zd_usb_to_usbdev(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100257 const struct firmware *ub_fw = NULL;
258 const struct firmware *uph_fw = NULL;
259 char fw_name[128];
260
261 bcdDevice = get_bcdDevice(udev);
262
263 r = request_fw_file(&ub_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100264 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100265 &udev->dev);
266 if (r)
267 goto error;
268
Daniel Drakeee302762006-12-12 01:25:52 +0000269 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
Daniel Drakee85d0912006-06-02 17:11:32 +0100270
Daniel Drakee85d0912006-06-02 17:11:32 +0100271 if (fw_bcdDevice != bcdDevice) {
272 dev_info(&udev->dev,
Daniel Draked066c212006-08-12 17:59:59 +0100273 "firmware version %#06x and device bootcode version "
274 "%#06x differ\n", fw_bcdDevice, bcdDevice);
275 if (bcdDevice <= 0x4313)
276 dev_warn(&udev->dev, "device has old bootcode, please "
277 "report success or failure\n");
278
Daniel Drake74553ae2007-07-01 18:22:32 +0100279 r = handle_version_mismatch(usb, ub_fw);
Daniel Draked066c212006-08-12 17:59:59 +0100280 if (r)
281 goto error;
Daniel Drakee85d0912006-06-02 17:11:32 +0100282 } else {
283 dev_dbg_f(&udev->dev,
284 "firmware device id %#06x is equal to the "
285 "actual device id\n", fw_bcdDevice);
286 }
287
288
289 r = request_fw_file(&uph_fw,
Daniel Drake74553ae2007-07-01 18:22:32 +0100290 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
Daniel Drakee85d0912006-06-02 17:11:32 +0100291 &udev->dev);
292 if (r)
293 goto error;
294
Daniel Drakeee302762006-12-12 01:25:52 +0000295 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100296 if (r) {
297 dev_err(&udev->dev,
298 "Could not upload firmware code uph. Error number %d\n",
299 r);
300 }
301
302 /* FALL-THROUGH */
303error:
304 release_firmware(ub_fw);
305 release_firmware(uph_fw);
306 return r;
307}
308
Daniel Drake74553ae2007-07-01 18:22:32 +0100309/* Read data from device address space using "firmware interface" which does
310 * not require firmware to be loaded. */
311int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
312{
313 int r;
314 struct usb_device *udev = zd_usb_to_usbdev(usb);
315
316 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
317 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
318 data, len, 5000);
319 if (r < 0) {
320 dev_err(&udev->dev,
321 "read over firmware interface failed: %d\n", r);
322 return r;
323 } else if (r != len) {
324 dev_err(&udev->dev,
325 "incomplete read over firmware interface: %d/%d\n",
326 r, len);
327 return -EIO;
328 }
329
330 return 0;
331}
332
Daniel Drakee85d0912006-06-02 17:11:32 +0100333#define urb_dev(urb) (&(urb)->dev->dev)
334
335static inline void handle_regs_int(struct urb *urb)
336{
337 struct zd_usb *usb = urb->context;
338 struct zd_usb_interrupt *intr = &usb->intr;
339 int len;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800340 u16 int_num;
Daniel Drakee85d0912006-06-02 17:11:32 +0100341
342 ZD_ASSERT(in_interrupt());
343 spin_lock(&intr->lock);
344
Al Viro4ec7ffa2008-05-21 06:32:11 +0100345 int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800346 if (int_num == CR_INTERRUPT) {
347 struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
348 memcpy(&mac->intr_buffer, urb->transfer_buffer,
349 USB_MAX_EP_INT_BUFFER);
350 schedule_work(&mac->process_intr);
351 } else if (intr->read_regs_enabled) {
Daniel Drakee85d0912006-06-02 17:11:32 +0100352 intr->read_regs.length = len = urb->actual_length;
353
354 if (len > sizeof(intr->read_regs.buffer))
355 len = sizeof(intr->read_regs.buffer);
356 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
357 intr->read_regs_enabled = 0;
358 complete(&intr->read_regs.completion);
359 goto out;
360 }
361
Daniel Drakee85d0912006-06-02 17:11:32 +0100362out:
363 spin_unlock(&intr->lock);
364}
365
David Howells7d12e782006-10-05 14:55:46 +0100366static void int_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100367{
368 int r;
369 struct usb_int_header *hdr;
370
371 switch (urb->status) {
372 case 0:
373 break;
374 case -ESHUTDOWN:
375 case -EINVAL:
376 case -ENODEV:
377 case -ENOENT:
378 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100379 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100380 goto kfree;
Daniel Drakee85d0912006-06-02 17:11:32 +0100381 default:
382 goto resubmit;
383 }
384
385 if (urb->actual_length < sizeof(hdr)) {
386 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
387 goto resubmit;
388 }
389
390 hdr = urb->transfer_buffer;
391 if (hdr->type != USB_INT_TYPE) {
392 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
393 goto resubmit;
394 }
395
396 switch (hdr->id) {
397 case USB_INT_ID_REGS:
398 handle_regs_int(urb);
399 break;
400 case USB_INT_ID_RETRY_FAILED:
Daniel Drake459c51a2007-11-19 15:00:29 +0000401 zd_mac_tx_failed(zd_usb_to_hw(urb->context));
Daniel Drakee85d0912006-06-02 17:11:32 +0100402 break;
403 default:
404 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
405 (unsigned int)hdr->id);
406 goto resubmit;
407 }
408
409resubmit:
410 r = usb_submit_urb(urb, GFP_ATOMIC);
411 if (r) {
412 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
413 goto kfree;
414 }
415 return;
416kfree:
417 kfree(urb->transfer_buffer);
418}
419
420static inline int int_urb_interval(struct usb_device *udev)
421{
422 switch (udev->speed) {
423 case USB_SPEED_HIGH:
424 return 4;
425 case USB_SPEED_LOW:
426 return 10;
427 case USB_SPEED_FULL:
428 default:
429 return 1;
430 }
431}
432
433static inline int usb_int_enabled(struct zd_usb *usb)
434{
435 unsigned long flags;
436 struct zd_usb_interrupt *intr = &usb->intr;
437 struct urb *urb;
438
439 spin_lock_irqsave(&intr->lock, flags);
440 urb = intr->urb;
441 spin_unlock_irqrestore(&intr->lock, flags);
442 return urb != NULL;
443}
444
445int zd_usb_enable_int(struct zd_usb *usb)
446{
447 int r;
448 struct usb_device *udev;
449 struct zd_usb_interrupt *intr = &usb->intr;
450 void *transfer_buffer = NULL;
451 struct urb *urb;
452
453 dev_dbg_f(zd_usb_dev(usb), "\n");
454
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000455 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100456 if (!urb) {
457 r = -ENOMEM;
458 goto out;
459 }
460
461 ZD_ASSERT(!irqs_disabled());
462 spin_lock_irq(&intr->lock);
463 if (intr->urb) {
464 spin_unlock_irq(&intr->lock);
465 r = 0;
466 goto error_free_urb;
467 }
468 intr->urb = urb;
469 spin_unlock_irq(&intr->lock);
470
471 /* TODO: make it a DMA buffer */
472 r = -ENOMEM;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000473 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100474 if (!transfer_buffer) {
475 dev_dbg_f(zd_usb_dev(usb),
476 "couldn't allocate transfer_buffer\n");
477 goto error_set_urb_null;
478 }
479
480 udev = zd_usb_to_usbdev(usb);
481 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
482 transfer_buffer, USB_MAX_EP_INT_BUFFER,
483 int_urb_complete, usb,
484 intr->interval);
485
486 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000487 r = usb_submit_urb(urb, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100488 if (r) {
489 dev_dbg_f(zd_usb_dev(usb),
490 "Couldn't submit urb. Error number %d\n", r);
491 goto error;
492 }
493
494 return 0;
495error:
496 kfree(transfer_buffer);
497error_set_urb_null:
498 spin_lock_irq(&intr->lock);
499 intr->urb = NULL;
500 spin_unlock_irq(&intr->lock);
501error_free_urb:
502 usb_free_urb(urb);
503out:
504 return r;
505}
506
507void zd_usb_disable_int(struct zd_usb *usb)
508{
509 unsigned long flags;
510 struct zd_usb_interrupt *intr = &usb->intr;
511 struct urb *urb;
512
513 spin_lock_irqsave(&intr->lock, flags);
514 urb = intr->urb;
515 if (!urb) {
516 spin_unlock_irqrestore(&intr->lock, flags);
517 return;
518 }
519 intr->urb = NULL;
520 spin_unlock_irqrestore(&intr->lock, flags);
521
522 usb_kill_urb(urb);
523 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
524 usb_free_urb(urb);
525}
526
527static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
528 unsigned int length)
529{
530 int i;
Daniel Drakee85d0912006-06-02 17:11:32 +0100531 const struct rx_length_info *length_info;
532
533 if (length < sizeof(struct rx_length_info)) {
534 /* It's not a complete packet anyhow. */
535 return;
536 }
537 length_info = (struct rx_length_info *)
538 (buffer + length - sizeof(struct rx_length_info));
539
540 /* It might be that three frames are merged into a single URB
541 * transaction. We have to check for the length info tag.
542 *
543 * While testing we discovered that length_info might be unaligned,
544 * because if USB transactions are merged, the last packet will not
545 * be padded. Unaligned access might also happen if the length_info
546 * structure is not present.
547 */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700548 if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
Ulrich Kunitzb2698252006-08-01 23:43:34 +0200549 {
Daniel Drakee85d0912006-06-02 17:11:32 +0100550 unsigned int l, k, n;
551 for (i = 0, l = 0;; i++) {
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700552 k = get_unaligned_le16(&length_info->length[i]);
Ulrich Kunitz850c2112006-11-22 00:06:19 +0000553 if (k == 0)
554 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100555 n = l+k;
556 if (n > length)
557 return;
Daniel Drake459c51a2007-11-19 15:00:29 +0000558 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
Daniel Drakee85d0912006-06-02 17:11:32 +0100559 if (i >= 2)
560 return;
561 l = (n+3) & ~3;
562 }
563 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000564 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
Daniel Drakee85d0912006-06-02 17:11:32 +0100565 }
566}
567
David Howells7d12e782006-10-05 14:55:46 +0100568static void rx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100569{
570 struct zd_usb *usb;
571 struct zd_usb_rx *rx;
572 const u8 *buffer;
573 unsigned int length;
574
575 switch (urb->status) {
576 case 0:
577 break;
578 case -ESHUTDOWN:
579 case -EINVAL:
580 case -ENODEV:
581 case -ENOENT:
582 case -ECONNRESET:
Daniel Drakee85d0912006-06-02 17:11:32 +0100583 case -EPIPE:
Daniel Drakeb312d792006-07-05 15:57:39 +0100584 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100585 default:
586 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
587 goto resubmit;
588 }
589
590 buffer = urb->transfer_buffer;
591 length = urb->actual_length;
592 usb = urb->context;
593 rx = &usb->rx;
594
595 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
596 /* If there is an old first fragment, we don't care. */
597 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
598 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
599 spin_lock(&rx->lock);
600 memcpy(rx->fragment, buffer, length);
601 rx->fragment_length = length;
602 spin_unlock(&rx->lock);
603 goto resubmit;
604 }
605
606 spin_lock(&rx->lock);
607 if (rx->fragment_length > 0) {
608 /* We are on a second fragment, we believe */
609 ZD_ASSERT(length + rx->fragment_length <=
610 ARRAY_SIZE(rx->fragment));
611 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
612 memcpy(rx->fragment+rx->fragment_length, buffer, length);
613 handle_rx_packet(usb, rx->fragment,
614 rx->fragment_length + length);
615 rx->fragment_length = 0;
616 spin_unlock(&rx->lock);
617 } else {
618 spin_unlock(&rx->lock);
619 handle_rx_packet(usb, buffer, length);
620 }
621
622resubmit:
623 usb_submit_urb(urb, GFP_ATOMIC);
624}
625
Daniel Drake459c51a2007-11-19 15:00:29 +0000626static struct urb *alloc_rx_urb(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100627{
628 struct usb_device *udev = zd_usb_to_usbdev(usb);
629 struct urb *urb;
630 void *buffer;
631
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000632 urb = usb_alloc_urb(0, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100633 if (!urb)
634 return NULL;
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000635 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
Daniel Drakee85d0912006-06-02 17:11:32 +0100636 &urb->transfer_dma);
637 if (!buffer) {
638 usb_free_urb(urb);
639 return NULL;
640 }
641
642 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
643 buffer, USB_MAX_RX_SIZE,
644 rx_urb_complete, usb);
645 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
646
647 return urb;
648}
649
Daniel Drake459c51a2007-11-19 15:00:29 +0000650static void free_rx_urb(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100651{
652 if (!urb)
653 return;
654 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
655 urb->transfer_buffer, urb->transfer_dma);
656 usb_free_urb(urb);
657}
658
659int zd_usb_enable_rx(struct zd_usb *usb)
660{
661 int i, r;
662 struct zd_usb_rx *rx = &usb->rx;
663 struct urb **urbs;
664
665 dev_dbg_f(zd_usb_dev(usb), "\n");
666
667 r = -ENOMEM;
Daniel Drake459c51a2007-11-19 15:00:29 +0000668 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100669 if (!urbs)
670 goto error;
Daniel Drake459c51a2007-11-19 15:00:29 +0000671 for (i = 0; i < RX_URBS_COUNT; i++) {
672 urbs[i] = alloc_rx_urb(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100673 if (!urbs[i])
674 goto error;
675 }
676
677 ZD_ASSERT(!irqs_disabled());
678 spin_lock_irq(&rx->lock);
679 if (rx->urbs) {
680 spin_unlock_irq(&rx->lock);
681 r = 0;
682 goto error;
683 }
684 rx->urbs = urbs;
Daniel Drake459c51a2007-11-19 15:00:29 +0000685 rx->urbs_count = RX_URBS_COUNT;
Daniel Drakee85d0912006-06-02 17:11:32 +0100686 spin_unlock_irq(&rx->lock);
687
Daniel Drake459c51a2007-11-19 15:00:29 +0000688 for (i = 0; i < RX_URBS_COUNT; i++) {
Ulrich Kunitz35c34042007-02-18 20:28:23 +0000689 r = usb_submit_urb(urbs[i], GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100690 if (r)
691 goto error_submit;
692 }
693
694 return 0;
695error_submit:
Daniel Drake459c51a2007-11-19 15:00:29 +0000696 for (i = 0; i < RX_URBS_COUNT; i++) {
Daniel Drakee85d0912006-06-02 17:11:32 +0100697 usb_kill_urb(urbs[i]);
698 }
699 spin_lock_irq(&rx->lock);
700 rx->urbs = NULL;
701 rx->urbs_count = 0;
702 spin_unlock_irq(&rx->lock);
703error:
704 if (urbs) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000705 for (i = 0; i < RX_URBS_COUNT; i++)
706 free_rx_urb(urbs[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +0100707 }
708 return r;
709}
710
711void zd_usb_disable_rx(struct zd_usb *usb)
712{
713 int i;
714 unsigned long flags;
715 struct urb **urbs;
716 unsigned int count;
717 struct zd_usb_rx *rx = &usb->rx;
718
719 spin_lock_irqsave(&rx->lock, flags);
720 urbs = rx->urbs;
721 count = rx->urbs_count;
722 spin_unlock_irqrestore(&rx->lock, flags);
723 if (!urbs)
724 return;
725
726 for (i = 0; i < count; i++) {
727 usb_kill_urb(urbs[i]);
Daniel Drake459c51a2007-11-19 15:00:29 +0000728 free_rx_urb(urbs[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +0100729 }
730 kfree(urbs);
731
732 spin_lock_irqsave(&rx->lock, flags);
733 rx->urbs = NULL;
734 rx->urbs_count = 0;
735 spin_unlock_irqrestore(&rx->lock, flags);
736}
737
Daniel Drake459c51a2007-11-19 15:00:29 +0000738/**
739 * zd_usb_disable_tx - disable transmission
740 * @usb: the zd1211rw-private USB structure
741 *
742 * Frees all URBs in the free list and marks the transmission as disabled.
743 */
744void zd_usb_disable_tx(struct zd_usb *usb)
745{
746 struct zd_usb_tx *tx = &usb->tx;
747 unsigned long flags;
748 struct list_head *pos, *n;
749
750 spin_lock_irqsave(&tx->lock, flags);
751 list_for_each_safe(pos, n, &tx->free_urb_list) {
752 list_del(pos);
753 usb_free_urb(list_entry(pos, struct urb, urb_list));
754 }
755 tx->enabled = 0;
756 tx->submitted_urbs = 0;
757 /* The stopped state is ignored, relying on ieee80211_wake_queues()
758 * in a potentionally following zd_usb_enable_tx().
759 */
760 spin_unlock_irqrestore(&tx->lock, flags);
761}
762
763/**
764 * zd_usb_enable_tx - enables transmission
765 * @usb: a &struct zd_usb pointer
766 *
767 * This function enables transmission and prepares the &zd_usb_tx data
768 * structure.
769 */
770void zd_usb_enable_tx(struct zd_usb *usb)
771{
772 unsigned long flags;
773 struct zd_usb_tx *tx = &usb->tx;
774
775 spin_lock_irqsave(&tx->lock, flags);
776 tx->enabled = 1;
777 tx->submitted_urbs = 0;
778 ieee80211_wake_queues(zd_usb_to_hw(usb));
779 tx->stopped = 0;
780 spin_unlock_irqrestore(&tx->lock, flags);
781}
782
783/**
784 * alloc_tx_urb - provides an tx URB
785 * @usb: a &struct zd_usb pointer
786 *
787 * Allocates a new URB. If possible takes the urb from the free list in
788 * usb->tx.
789 */
790static struct urb *alloc_tx_urb(struct zd_usb *usb)
791{
792 struct zd_usb_tx *tx = &usb->tx;
793 unsigned long flags;
794 struct list_head *entry;
795 struct urb *urb;
796
797 spin_lock_irqsave(&tx->lock, flags);
798 if (list_empty(&tx->free_urb_list)) {
799 urb = usb_alloc_urb(0, GFP_ATOMIC);
800 goto out;
801 }
802 entry = tx->free_urb_list.next;
803 list_del(entry);
804 urb = list_entry(entry, struct urb, urb_list);
805out:
806 spin_unlock_irqrestore(&tx->lock, flags);
807 return urb;
808}
809
810/**
811 * free_tx_urb - frees a used tx URB
812 * @usb: a &struct zd_usb pointer
813 * @urb: URB to be freed
814 *
815 * Frees the the transmission URB, which means to put it on the free URB
816 * list.
817 */
818static void free_tx_urb(struct zd_usb *usb, struct urb *urb)
819{
820 struct zd_usb_tx *tx = &usb->tx;
821 unsigned long flags;
822
823 spin_lock_irqsave(&tx->lock, flags);
824 if (!tx->enabled) {
825 usb_free_urb(urb);
826 goto out;
827 }
828 list_add(&urb->urb_list, &tx->free_urb_list);
829out:
830 spin_unlock_irqrestore(&tx->lock, flags);
831}
832
833static void tx_dec_submitted_urbs(struct zd_usb *usb)
834{
835 struct zd_usb_tx *tx = &usb->tx;
836 unsigned long flags;
837
838 spin_lock_irqsave(&tx->lock, flags);
839 --tx->submitted_urbs;
840 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
841 ieee80211_wake_queues(zd_usb_to_hw(usb));
842 tx->stopped = 0;
843 }
844 spin_unlock_irqrestore(&tx->lock, flags);
845}
846
847static void tx_inc_submitted_urbs(struct zd_usb *usb)
848{
849 struct zd_usb_tx *tx = &usb->tx;
850 unsigned long flags;
851
852 spin_lock_irqsave(&tx->lock, flags);
853 ++tx->submitted_urbs;
854 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
855 ieee80211_stop_queues(zd_usb_to_hw(usb));
856 tx->stopped = 1;
857 }
858 spin_unlock_irqrestore(&tx->lock, flags);
859}
860
861/**
862 * tx_urb_complete - completes the execution of an URB
863 * @urb: a URB
864 *
865 * This function is called if the URB has been transferred to a device or an
866 * error has happened.
867 */
David Howells7d12e782006-10-05 14:55:46 +0100868static void tx_urb_complete(struct urb *urb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100869{
870 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000871 struct sk_buff *skb;
872 struct zd_tx_skb_control_block *cb;
873 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100874
875 switch (urb->status) {
876 case 0:
877 break;
878 case -ESHUTDOWN:
879 case -EINVAL:
880 case -ENODEV:
881 case -ENOENT:
882 case -ECONNRESET:
Daniel Drakeb312d792006-07-05 15:57:39 +0100883 case -EPIPE:
Daniel Drakee85d0912006-06-02 17:11:32 +0100884 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
885 break;
Daniel Drakee85d0912006-06-02 17:11:32 +0100886 default:
887 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
888 goto resubmit;
889 }
890free_urb:
Daniel Drake459c51a2007-11-19 15:00:29 +0000891 skb = (struct sk_buff *)urb->context;
Johannes Bergdbabad02008-05-08 01:43:59 +0200892 /*
893 * grab 'usb' pointer before handing off the skb (since
894 * it might be freed by zd_mac_tx_to_dev or mac80211)
895 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000896 cb = (struct zd_tx_skb_control_block *)skb->cb;
897 usb = &zd_hw_mac(cb->hw)->chip.usb;
Johannes Bergdbabad02008-05-08 01:43:59 +0200898 zd_mac_tx_to_dev(skb, urb->status);
Daniel Drake459c51a2007-11-19 15:00:29 +0000899 free_tx_urb(usb, urb);
900 tx_dec_submitted_urbs(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100901 return;
902resubmit:
903 r = usb_submit_urb(urb, GFP_ATOMIC);
904 if (r) {
905 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
906 goto free_urb;
907 }
908}
909
Daniel Drake459c51a2007-11-19 15:00:29 +0000910/**
911 * zd_usb_tx: initiates transfer of a frame of the device
912 *
913 * @usb: the zd1211rw-private USB structure
914 * @skb: a &struct sk_buff pointer
915 *
916 * This function tranmits a frame to the device. It doesn't wait for
917 * completion. The frame must contain the control set and have all the
918 * control set information available.
919 *
920 * The function returns 0 if the transfer has been successfully initiated.
Daniel Drakee85d0912006-06-02 17:11:32 +0100921 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000922int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100923{
924 int r;
925 struct usb_device *udev = zd_usb_to_usbdev(usb);
926 struct urb *urb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100927
Daniel Drake459c51a2007-11-19 15:00:29 +0000928 urb = alloc_tx_urb(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100929 if (!urb) {
930 r = -ENOMEM;
931 goto out;
932 }
933
Daniel Drakee85d0912006-06-02 17:11:32 +0100934 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
Daniel Drake459c51a2007-11-19 15:00:29 +0000935 skb->data, skb->len, tx_urb_complete, skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100936
937 r = usb_submit_urb(urb, GFP_ATOMIC);
938 if (r)
939 goto error;
Daniel Drake459c51a2007-11-19 15:00:29 +0000940 tx_inc_submitted_urbs(usb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100941 return 0;
942error:
Daniel Drake459c51a2007-11-19 15:00:29 +0000943 free_tx_urb(usb, urb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100944out:
945 return r;
946}
947
948static inline void init_usb_interrupt(struct zd_usb *usb)
949{
950 struct zd_usb_interrupt *intr = &usb->intr;
951
952 spin_lock_init(&intr->lock);
953 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
954 init_completion(&intr->read_regs.completion);
Daniel Drake0ce34bc2006-12-12 01:26:11 +0000955 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
Daniel Drakee85d0912006-06-02 17:11:32 +0100956}
957
958static inline void init_usb_rx(struct zd_usb *usb)
959{
960 struct zd_usb_rx *rx = &usb->rx;
961 spin_lock_init(&rx->lock);
962 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
963 rx->usb_packet_size = 512;
964 } else {
965 rx->usb_packet_size = 64;
966 }
967 ZD_ASSERT(rx->fragment_length == 0);
968}
969
970static inline void init_usb_tx(struct zd_usb *usb)
971{
Daniel Drake459c51a2007-11-19 15:00:29 +0000972 struct zd_usb_tx *tx = &usb->tx;
973 spin_lock_init(&tx->lock);
974 tx->enabled = 0;
975 tx->stopped = 0;
976 INIT_LIST_HEAD(&tx->free_urb_list);
977 tx->submitted_urbs = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100978}
979
Daniel Drake459c51a2007-11-19 15:00:29 +0000980void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
Daniel Drakee85d0912006-06-02 17:11:32 +0100981 struct usb_interface *intf)
982{
983 memset(usb, 0, sizeof(*usb));
984 usb->intf = usb_get_intf(intf);
Daniel Drake459c51a2007-11-19 15:00:29 +0000985 usb_set_intfdata(usb->intf, hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100986 init_usb_interrupt(usb);
987 init_usb_tx(usb);
988 init_usb_rx(usb);
989}
990
Daniel Drakee85d0912006-06-02 17:11:32 +0100991void zd_usb_clear(struct zd_usb *usb)
992{
993 usb_set_intfdata(usb->intf, NULL);
994 usb_put_intf(usb->intf);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100995 ZD_MEMCLEAR(usb, sizeof(*usb));
Daniel Drakee85d0912006-06-02 17:11:32 +0100996 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
997}
998
999static const char *speed(enum usb_device_speed speed)
1000{
1001 switch (speed) {
1002 case USB_SPEED_LOW:
1003 return "low";
1004 case USB_SPEED_FULL:
1005 return "full";
1006 case USB_SPEED_HIGH:
1007 return "high";
1008 default:
1009 return "unknown speed";
1010 }
1011}
1012
1013static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
1014{
1015 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1016 le16_to_cpu(udev->descriptor.idVendor),
1017 le16_to_cpu(udev->descriptor.idProduct),
1018 get_bcdDevice(udev),
1019 speed(udev->speed));
1020}
1021
1022int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1023{
1024 struct usb_device *udev = interface_to_usbdev(usb->intf);
1025 return scnprint_id(udev, buffer, size);
1026}
1027
1028#ifdef DEBUG
1029static void print_id(struct usb_device *udev)
1030{
1031 char buffer[40];
1032
1033 scnprint_id(udev, buffer, sizeof(buffer));
1034 buffer[sizeof(buffer)-1] = 0;
1035 dev_dbg_f(&udev->dev, "%s\n", buffer);
1036}
1037#else
1038#define print_id(udev) do { } while (0)
1039#endif
1040
Daniel Drakea1030e92006-08-13 12:15:29 +01001041static int eject_installer(struct usb_interface *intf)
1042{
1043 struct usb_device *udev = interface_to_usbdev(intf);
1044 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1045 struct usb_endpoint_descriptor *endpoint;
1046 unsigned char *cmd;
1047 u8 bulk_out_ep;
1048 int r;
1049
1050 /* Find bulk out endpoint */
1051 endpoint = &iface_desc->endpoint[1].desc;
1052 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
1053 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1054 USB_ENDPOINT_XFER_BULK) {
1055 bulk_out_ep = endpoint->bEndpointAddress;
1056 } else {
1057 dev_err(&udev->dev,
1058 "zd1211rw: Could not find bulk out endpoint\n");
1059 return -ENODEV;
1060 }
1061
1062 cmd = kzalloc(31, GFP_KERNEL);
1063 if (cmd == NULL)
1064 return -ENODEV;
1065
1066 /* USB bulk command block */
1067 cmd[0] = 0x55; /* bulk command signature */
1068 cmd[1] = 0x53; /* bulk command signature */
1069 cmd[2] = 0x42; /* bulk command signature */
1070 cmd[3] = 0x43; /* bulk command signature */
1071 cmd[14] = 6; /* command length */
1072
1073 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1074 cmd[19] = 0x2; /* eject disc */
1075
1076 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1077 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1078 cmd, 31, NULL, 2000);
1079 kfree(cmd);
1080 if (r)
1081 return r;
1082
1083 /* At this point, the device disconnects and reconnects with the real
1084 * ID numbers. */
1085
1086 usb_set_intfdata(intf, NULL);
1087 return 0;
1088}
1089
Daniel Drake74553ae2007-07-01 18:22:32 +01001090int zd_usb_init_hw(struct zd_usb *usb)
1091{
1092 int r;
1093 struct zd_mac *mac = zd_usb_to_mac(usb);
1094
1095 dev_dbg_f(zd_usb_dev(usb), "\n");
1096
1097 r = upload_firmware(usb);
1098 if (r) {
1099 dev_err(zd_usb_dev(usb),
1100 "couldn't load firmware. Error number %d\n", r);
1101 return r;
1102 }
1103
1104 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1105 if (r) {
1106 dev_dbg_f(zd_usb_dev(usb),
1107 "couldn't reset configuration. Error number %d\n", r);
1108 return r;
1109 }
1110
Daniel Drake459c51a2007-11-19 15:00:29 +00001111 r = zd_mac_init_hw(mac->hw);
Daniel Drake74553ae2007-07-01 18:22:32 +01001112 if (r) {
1113 dev_dbg_f(zd_usb_dev(usb),
1114 "couldn't initialize mac. Error number %d\n", r);
1115 return r;
1116 }
1117
1118 usb->initialized = 1;
1119 return 0;
1120}
1121
Daniel Drakee85d0912006-06-02 17:11:32 +01001122static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1123{
1124 int r;
1125 struct usb_device *udev = interface_to_usbdev(intf);
Daniel Drake459c51a2007-11-19 15:00:29 +00001126 struct zd_usb *usb;
1127 struct ieee80211_hw *hw = NULL;
Daniel Drakee85d0912006-06-02 17:11:32 +01001128
1129 print_id(udev);
1130
Daniel Drakea1030e92006-08-13 12:15:29 +01001131 if (id->driver_info & DEVICE_INSTALLER)
1132 return eject_installer(intf);
1133
Daniel Drakee85d0912006-06-02 17:11:32 +01001134 switch (udev->speed) {
1135 case USB_SPEED_LOW:
1136 case USB_SPEED_FULL:
1137 case USB_SPEED_HIGH:
1138 break;
1139 default:
1140 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1141 r = -ENODEV;
1142 goto error;
1143 }
1144
Daniel Drake459c51a2007-11-19 15:00:29 +00001145 r = usb_reset_device(udev);
1146 if (r) {
1147 dev_err(&intf->dev,
1148 "couldn't reset usb device. Error number %d\n", r);
1149 goto error;
1150 }
Ulrich Kunitz6e3632f2007-01-29 00:59:28 +00001151
Daniel Drake459c51a2007-11-19 15:00:29 +00001152 hw = zd_mac_alloc_hw(intf);
1153 if (hw == NULL) {
Daniel Drakee85d0912006-06-02 17:11:32 +01001154 r = -ENOMEM;
1155 goto error;
1156 }
1157
Daniel Drake459c51a2007-11-19 15:00:29 +00001158 usb = &zd_hw_mac(hw)->chip.usb;
Daniel Drake74553ae2007-07-01 18:22:32 +01001159 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001160
Daniel Drake459c51a2007-11-19 15:00:29 +00001161 r = zd_mac_preinit_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001162 if (r) {
1163 dev_dbg_f(&intf->dev,
1164 "couldn't initialize mac. Error number %d\n", r);
1165 goto error;
1166 }
1167
Daniel Drake459c51a2007-11-19 15:00:29 +00001168 r = ieee80211_register_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001169 if (r) {
1170 dev_dbg_f(&intf->dev,
Daniel Drake459c51a2007-11-19 15:00:29 +00001171 "couldn't register device. Error number %d\n", r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001172 goto error;
1173 }
1174
1175 dev_dbg_f(&intf->dev, "successful\n");
Daniel Drake459c51a2007-11-19 15:00:29 +00001176 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
Daniel Drakee85d0912006-06-02 17:11:32 +01001177 return 0;
1178error:
1179 usb_reset_device(interface_to_usbdev(intf));
Daniel Drake459c51a2007-11-19 15:00:29 +00001180 if (hw) {
1181 zd_mac_clear(zd_hw_mac(hw));
1182 ieee80211_free_hw(hw);
1183 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001184 return r;
1185}
1186
1187static void disconnect(struct usb_interface *intf)
1188{
Daniel Drake459c51a2007-11-19 15:00:29 +00001189 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
Marc Pignate0579d52007-10-15 08:51:52 +02001190 struct zd_mac *mac;
1191 struct zd_usb *usb;
Daniel Drakee85d0912006-06-02 17:11:32 +01001192
Daniel Drakea1030e92006-08-13 12:15:29 +01001193 /* Either something really bad happened, or we're just dealing with
1194 * a DEVICE_INSTALLER. */
Daniel Drake459c51a2007-11-19 15:00:29 +00001195 if (hw == NULL)
Daniel Drakea1030e92006-08-13 12:15:29 +01001196 return;
1197
Daniel Drake459c51a2007-11-19 15:00:29 +00001198 mac = zd_hw_mac(hw);
Marc Pignate0579d52007-10-15 08:51:52 +02001199 usb = &mac->chip.usb;
1200
Daniel Drakee85d0912006-06-02 17:11:32 +01001201 dev_dbg_f(zd_usb_dev(usb), "\n");
1202
Daniel Drake459c51a2007-11-19 15:00:29 +00001203 ieee80211_unregister_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001204
1205 /* Just in case something has gone wrong! */
1206 zd_usb_disable_rx(usb);
1207 zd_usb_disable_int(usb);
1208
1209 /* If the disconnect has been caused by a removal of the
1210 * driver module, the reset allows reloading of the driver. If the
1211 * reset will not be executed here, the upload of the firmware in the
1212 * probe function caused by the reloading of the driver will fail.
1213 */
1214 usb_reset_device(interface_to_usbdev(intf));
1215
Daniel Drake459c51a2007-11-19 15:00:29 +00001216 zd_mac_clear(mac);
1217 ieee80211_free_hw(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +01001218 dev_dbg(&intf->dev, "disconnected\n");
1219}
1220
1221static struct usb_driver driver = {
Daniel Drake459c51a2007-11-19 15:00:29 +00001222 .name = KBUILD_MODNAME,
Daniel Drakee85d0912006-06-02 17:11:32 +01001223 .id_table = usb_ids,
1224 .probe = probe,
1225 .disconnect = disconnect,
1226};
1227
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001228struct workqueue_struct *zd_workqueue;
1229
Daniel Drakee85d0912006-06-02 17:11:32 +01001230static int __init usb_init(void)
1231{
1232 int r;
1233
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001234 pr_debug("%s usb_init()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001235
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001236 zd_workqueue = create_singlethread_workqueue(driver.name);
1237 if (zd_workqueue == NULL) {
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001238 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001239 return -ENOMEM;
1240 }
1241
Daniel Drakee85d0912006-06-02 17:11:32 +01001242 r = usb_register(&driver);
1243 if (r) {
Maxime Austruy192b7752007-01-29 00:59:51 +00001244 destroy_workqueue(zd_workqueue);
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001245 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1246 driver.name, r);
Daniel Drakee85d0912006-06-02 17:11:32 +01001247 return r;
1248 }
1249
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001250 pr_debug("%s initialized\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001251 return 0;
1252}
1253
1254static void __exit usb_exit(void)
1255{
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001256 pr_debug("%s usb_exit()\n", driver.name);
Daniel Drakee85d0912006-06-02 17:11:32 +01001257 usb_deregister(&driver);
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +01001258 destroy_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +01001259}
1260
1261module_init(usb_init);
1262module_exit(usb_exit);
1263
1264static int usb_int_regs_length(unsigned int count)
1265{
1266 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1267}
1268
1269static void prepare_read_regs_int(struct zd_usb *usb)
1270{
1271 struct zd_usb_interrupt *intr = &usb->intr;
1272
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001273 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001274 intr->read_regs_enabled = 1;
1275 INIT_COMPLETION(intr->read_regs.completion);
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001276 spin_unlock_irq(&intr->lock);
1277}
1278
1279static void disable_read_regs_int(struct zd_usb *usb)
1280{
1281 struct zd_usb_interrupt *intr = &usb->intr;
1282
1283 spin_lock_irq(&intr->lock);
1284 intr->read_regs_enabled = 0;
1285 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001286}
1287
1288static int get_results(struct zd_usb *usb, u16 *values,
1289 struct usb_req_read_regs *req, unsigned int count)
1290{
1291 int r;
1292 int i;
1293 struct zd_usb_interrupt *intr = &usb->intr;
1294 struct read_regs_int *rr = &intr->read_regs;
1295 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1296
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001297 spin_lock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001298
1299 r = -EIO;
1300 /* The created block size seems to be larger than expected.
1301 * However results appear to be correct.
1302 */
1303 if (rr->length < usb_int_regs_length(count)) {
1304 dev_dbg_f(zd_usb_dev(usb),
1305 "error: actual length %d less than expected %d\n",
1306 rr->length, usb_int_regs_length(count));
1307 goto error_unlock;
1308 }
1309 if (rr->length > sizeof(rr->buffer)) {
1310 dev_dbg_f(zd_usb_dev(usb),
1311 "error: actual length %d exceeds buffer size %zu\n",
1312 rr->length, sizeof(rr->buffer));
1313 goto error_unlock;
1314 }
1315
1316 for (i = 0; i < count; i++) {
1317 struct reg_data *rd = &regs->regs[i];
1318 if (rd->addr != req->addr[i]) {
1319 dev_dbg_f(zd_usb_dev(usb),
1320 "rd[%d] addr %#06hx expected %#06hx\n", i,
1321 le16_to_cpu(rd->addr),
1322 le16_to_cpu(req->addr[i]));
1323 goto error_unlock;
1324 }
1325 values[i] = le16_to_cpu(rd->value);
1326 }
1327
1328 r = 0;
1329error_unlock:
Ulrich Kunitza68077d2006-11-22 00:06:06 +00001330 spin_unlock_irq(&intr->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +01001331 return r;
1332}
1333
1334int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1335 const zd_addr_t *addresses, unsigned int count)
1336{
1337 int r;
1338 int i, req_len, actual_req_len;
1339 struct usb_device *udev;
1340 struct usb_req_read_regs *req = NULL;
1341 unsigned long timeout;
1342
1343 if (count < 1) {
1344 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1345 return -EINVAL;
1346 }
1347 if (count > USB_MAX_IOREAD16_COUNT) {
1348 dev_dbg_f(zd_usb_dev(usb),
1349 "error: count %u exceeds possible max %u\n",
1350 count, USB_MAX_IOREAD16_COUNT);
1351 return -EINVAL;
1352 }
1353 if (in_atomic()) {
1354 dev_dbg_f(zd_usb_dev(usb),
1355 "error: io in atomic context not supported\n");
1356 return -EWOULDBLOCK;
1357 }
1358 if (!usb_int_enabled(usb)) {
1359 dev_dbg_f(zd_usb_dev(usb),
1360 "error: usb interrupt not enabled\n");
1361 return -EWOULDBLOCK;
1362 }
1363
1364 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001365 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001366 if (!req)
1367 return -ENOMEM;
1368 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1369 for (i = 0; i < count; i++)
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001370 req->addr[i] = cpu_to_le16((u16)addresses[i]);
Daniel Drakee85d0912006-06-02 17:11:32 +01001371
1372 udev = zd_usb_to_usbdev(usb);
1373 prepare_read_regs_int(usb);
1374 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1375 req, req_len, &actual_req_len, 1000 /* ms */);
1376 if (r) {
1377 dev_dbg_f(zd_usb_dev(usb),
1378 "error in usb_bulk_msg(). Error number %d\n", r);
1379 goto error;
1380 }
1381 if (req_len != actual_req_len) {
1382 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1383 " req_len %d != actual_req_len %d\n",
1384 req_len, actual_req_len);
1385 r = -EIO;
1386 goto error;
1387 }
1388
1389 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1390 msecs_to_jiffies(1000));
1391 if (!timeout) {
1392 disable_read_regs_int(usb);
1393 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1394 r = -ETIMEDOUT;
1395 goto error;
1396 }
1397
1398 r = get_results(usb, values, req, count);
1399error:
1400 kfree(req);
1401 return r;
1402}
1403
1404int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1405 unsigned int count)
1406{
1407 int r;
1408 struct usb_device *udev;
1409 struct usb_req_write_regs *req = NULL;
1410 int i, req_len, actual_req_len;
1411
1412 if (count == 0)
1413 return 0;
1414 if (count > USB_MAX_IOWRITE16_COUNT) {
1415 dev_dbg_f(zd_usb_dev(usb),
1416 "error: count %u exceeds possible max %u\n",
1417 count, USB_MAX_IOWRITE16_COUNT);
1418 return -EINVAL;
1419 }
1420 if (in_atomic()) {
1421 dev_dbg_f(zd_usb_dev(usb),
1422 "error: io in atomic context not supported\n");
1423 return -EWOULDBLOCK;
1424 }
1425
1426 req_len = sizeof(struct usb_req_write_regs) +
1427 count * sizeof(struct reg_data);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001428 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001429 if (!req)
1430 return -ENOMEM;
1431
1432 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1433 for (i = 0; i < count; i++) {
1434 struct reg_data *rw = &req->reg_writes[i];
Daniel Drake0ce34bc2006-12-12 01:26:11 +00001435 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
Daniel Drakee85d0912006-06-02 17:11:32 +01001436 rw->value = cpu_to_le16(ioreqs[i].value);
1437 }
1438
1439 udev = zd_usb_to_usbdev(usb);
1440 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1441 req, req_len, &actual_req_len, 1000 /* ms */);
1442 if (r) {
1443 dev_dbg_f(zd_usb_dev(usb),
1444 "error in usb_bulk_msg(). Error number %d\n", r);
1445 goto error;
1446 }
1447 if (req_len != actual_req_len) {
1448 dev_dbg_f(zd_usb_dev(usb),
1449 "error in usb_bulk_msg()"
1450 " req_len %d != actual_req_len %d\n",
1451 req_len, actual_req_len);
1452 r = -EIO;
1453 goto error;
1454 }
1455
1456 /* FALL-THROUGH with r == 0 */
1457error:
1458 kfree(req);
1459 return r;
1460}
1461
1462int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1463{
1464 int r;
1465 struct usb_device *udev;
1466 struct usb_req_rfwrite *req = NULL;
1467 int i, req_len, actual_req_len;
1468 u16 bit_value_template;
1469
1470 if (in_atomic()) {
1471 dev_dbg_f(zd_usb_dev(usb),
1472 "error: io in atomic context not supported\n");
1473 return -EWOULDBLOCK;
1474 }
1475 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1476 dev_dbg_f(zd_usb_dev(usb),
1477 "error: bits %d are smaller than"
1478 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1479 bits, USB_MIN_RFWRITE_BIT_COUNT);
1480 return -EINVAL;
1481 }
1482 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1483 dev_dbg_f(zd_usb_dev(usb),
1484 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1485 bits, USB_MAX_RFWRITE_BIT_COUNT);
1486 return -EINVAL;
1487 }
1488#ifdef DEBUG
1489 if (value & (~0UL << bits)) {
1490 dev_dbg_f(zd_usb_dev(usb),
1491 "error: value %#09x has bits >= %d set\n",
1492 value, bits);
1493 return -EINVAL;
1494 }
1495#endif /* DEBUG */
1496
1497 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1498
1499 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1500 if (r) {
1501 dev_dbg_f(zd_usb_dev(usb),
1502 "error %d: Couldn't read CR203\n", r);
1503 goto out;
1504 }
1505 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1506
1507 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
Ulrich Kunitz35c34042007-02-18 20:28:23 +00001508 req = kmalloc(req_len, GFP_KERNEL);
Daniel Drakee85d0912006-06-02 17:11:32 +01001509 if (!req)
1510 return -ENOMEM;
1511
1512 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1513 /* 1: 3683a, but not used in ZYDAS driver */
1514 req->value = cpu_to_le16(2);
1515 req->bits = cpu_to_le16(bits);
1516
1517 for (i = 0; i < bits; i++) {
1518 u16 bv = bit_value_template;
1519 if (value & (1 << (bits-1-i)))
1520 bv |= RF_DATA;
1521 req->bit_values[i] = cpu_to_le16(bv);
1522 }
1523
1524 udev = zd_usb_to_usbdev(usb);
1525 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1526 req, req_len, &actual_req_len, 1000 /* ms */);
1527 if (r) {
1528 dev_dbg_f(zd_usb_dev(usb),
1529 "error in usb_bulk_msg(). Error number %d\n", r);
1530 goto out;
1531 }
1532 if (req_len != actual_req_len) {
1533 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1534 " req_len %d != actual_req_len %d\n",
1535 req_len, actual_req_len);
1536 r = -EIO;
1537 goto out;
1538 }
1539
1540 /* FALL-THROUGH with r == 0 */
1541out:
1542 kfree(req);
1543 return r;
1544}