]>
Commit | Line | Data |
---|---|---|
66bb42fd DD |
1 | /* 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> | |
e85d0918 DD |
6 | * |
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 | ||
d066c219 | 22 | #include <linux/kernel.h> |
e85d0918 | 23 | #include <linux/init.h> |
e85d0918 DD |
24 | #include <linux/firmware.h> |
25 | #include <linux/device.h> | |
26 | #include <linux/errno.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
e85d0918 DD |
28 | #include <linux/skbuff.h> |
29 | #include <linux/usb.h> | |
bc5f06a8 | 30 | #include <linux/workqueue.h> |
459c51ad | 31 | #include <net/mac80211.h> |
c248725b | 32 | #include <asm/unaligned.h> |
e85d0918 DD |
33 | |
34 | #include "zd_def.h" | |
e85d0918 DD |
35 | #include "zd_mac.h" |
36 | #include "zd_usb.h" | |
e85d0918 DD |
37 | |
38 | static struct usb_device_id usb_ids[] = { | |
39 | /* ZD1211 */ | |
3bfbe80e HTL |
40 | { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 }, |
41 | { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 }, | |
42 | { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 }, | |
43 | { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 }, | |
44 | { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 }, | |
45 | { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 }, | |
46 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 }, | |
e85d0918 | 47 | { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 }, |
14990c69 | 48 | { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
49 | { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 }, |
50 | { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 }, | |
51 | { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 }, | |
e85d0918 | 52 | { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 }, |
aa1d3a18 | 53 | { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
54 | { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 }, |
55 | { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 }, | |
56 | { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 }, | |
f2423723 | 57 | { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 }, |
8cecc90e | 58 | { USB_DEVICE(0x14ea, 0xab10), .driver_info = DEVICE_ZD1211 }, |
229782a3 | 59 | { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 }, |
269fca0e | 60 | { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
61 | { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 }, |
62 | { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 }, | |
9011cd25 | 63 | { USB_DEVICE(0x157e, 0x3207), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
64 | { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 }, |
65 | { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 }, | |
e85d0918 | 66 | /* ZD1211B */ |
3bfbe80e | 67 | { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B }, |
ad580db5 | 68 | { USB_DEVICE(0x0409, 0x0248), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
69 | { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B }, |
70 | { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B }, | |
71 | { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B }, | |
72 | { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B }, | |
8b339d05 | 73 | { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
74 | { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B }, |
75 | { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B }, | |
76 | { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B }, | |
77 | { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B }, | |
78 | { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B }, | |
dd2f5538 | 79 | { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
80 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B }, |
81 | { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B }, | |
82 | { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B }, | |
8f75e07a | 83 | { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B }, |
f72151fb | 84 | { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B }, |
9dfd5500 | 85 | { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
86 | { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B }, |
87 | { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B }, | |
aec91028 | 88 | { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B }, |
c445a31c | 89 | { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B }, |
8e97afe5 | 90 | { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B }, |
de262496 | 91 | { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
92 | { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B }, |
93 | { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B }, | |
94 | { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B }, | |
95 | { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B }, | |
96 | { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B }, | |
8cecc90e | 97 | { USB_DEVICE(0x2019, 0xed01), .driver_info = DEVICE_ZD1211B }, |
a1030e92 DD |
98 | /* "Driverless" devices that need ejecting */ |
99 | { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER }, | |
aa1d3a18 | 100 | { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER }, |
e85d0918 DD |
101 | {} |
102 | }; | |
103 | ||
104 | MODULE_LICENSE("GPL"); | |
105 | MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip."); | |
106 | MODULE_AUTHOR("Ulrich Kunitz"); | |
107 | MODULE_AUTHOR("Daniel Drake"); | |
108 | MODULE_VERSION("1.0"); | |
109 | MODULE_DEVICE_TABLE(usb, usb_ids); | |
110 | ||
111 | #define FW_ZD1211_PREFIX "zd1211/zd1211_" | |
112 | #define FW_ZD1211B_PREFIX "zd1211/zd1211b_" | |
113 | ||
e85d0918 | 114 | /* USB device initialization */ |
72e77a8a | 115 | static void int_urb_complete(struct urb *urb); |
e85d0918 DD |
116 | |
117 | static int request_fw_file( | |
118 | const struct firmware **fw, const char *name, struct device *device) | |
119 | { | |
120 | int r; | |
121 | ||
122 | dev_dbg_f(device, "fw name %s\n", name); | |
123 | ||
124 | r = request_firmware(fw, name, device); | |
125 | if (r) | |
126 | dev_err(device, | |
127 | "Could not load firmware file %s. Error number %d\n", | |
128 | name, r); | |
129 | return r; | |
130 | } | |
131 | ||
132 | static inline u16 get_bcdDevice(const struct usb_device *udev) | |
133 | { | |
134 | return le16_to_cpu(udev->descriptor.bcdDevice); | |
135 | } | |
136 | ||
137 | enum upload_code_flags { | |
138 | REBOOT = 1, | |
139 | }; | |
140 | ||
141 | /* Ensures that MAX_TRANSFER_SIZE is even. */ | |
142 | #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1) | |
143 | ||
144 | static int upload_code(struct usb_device *udev, | |
145 | const u8 *data, size_t size, u16 code_offset, int flags) | |
146 | { | |
147 | u8 *p; | |
148 | int r; | |
149 | ||
150 | /* USB request blocks need "kmalloced" buffers. | |
151 | */ | |
152 | p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL); | |
153 | if (!p) { | |
154 | dev_err(&udev->dev, "out of memory\n"); | |
155 | r = -ENOMEM; | |
156 | goto error; | |
157 | } | |
158 | ||
159 | size &= ~1; | |
160 | while (size > 0) { | |
161 | size_t transfer_size = size <= MAX_TRANSFER_SIZE ? | |
162 | size : MAX_TRANSFER_SIZE; | |
163 | ||
164 | dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size); | |
165 | ||
166 | memcpy(p, data, transfer_size); | |
167 | r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
168 | USB_REQ_FIRMWARE_DOWNLOAD, | |
169 | USB_DIR_OUT | USB_TYPE_VENDOR, | |
170 | code_offset, 0, p, transfer_size, 1000 /* ms */); | |
171 | if (r < 0) { | |
172 | dev_err(&udev->dev, | |
173 | "USB control request for firmware upload" | |
174 | " failed. Error number %d\n", r); | |
175 | goto error; | |
176 | } | |
177 | transfer_size = r & ~1; | |
178 | ||
179 | size -= transfer_size; | |
180 | data += transfer_size; | |
181 | code_offset += transfer_size/sizeof(u16); | |
182 | } | |
183 | ||
184 | if (flags & REBOOT) { | |
185 | u8 ret; | |
186 | ||
a8c4ea7a | 187 | /* Use "DMA-aware" buffer. */ |
e85d0918 DD |
188 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
189 | USB_REQ_FIRMWARE_CONFIRM, | |
190 | USB_DIR_IN | USB_TYPE_VENDOR, | |
a8c4ea7a | 191 | 0, 0, p, sizeof(ret), 5000 /* ms */); |
e85d0918 DD |
192 | if (r != sizeof(ret)) { |
193 | dev_err(&udev->dev, | |
194 | "control request firmeware confirmation failed." | |
195 | " Return value %d\n", r); | |
196 | if (r >= 0) | |
197 | r = -ENODEV; | |
198 | goto error; | |
199 | } | |
a8c4ea7a | 200 | ret = p[0]; |
e85d0918 DD |
201 | if (ret & 0x80) { |
202 | dev_err(&udev->dev, | |
203 | "Internal error while downloading." | |
204 | " Firmware confirm return value %#04x\n", | |
205 | (unsigned int)ret); | |
206 | r = -ENODEV; | |
207 | goto error; | |
208 | } | |
209 | dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n", | |
210 | (unsigned int)ret); | |
211 | } | |
212 | ||
213 | r = 0; | |
214 | error: | |
215 | kfree(p); | |
216 | return r; | |
217 | } | |
218 | ||
219 | static u16 get_word(const void *data, u16 offset) | |
220 | { | |
221 | const __le16 *p = data; | |
222 | return le16_to_cpu(p[offset]); | |
223 | } | |
224 | ||
74553aed | 225 | static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size, |
e85d0918 DD |
226 | const char* postfix) |
227 | { | |
228 | scnprintf(buffer, size, "%s%s", | |
74553aed | 229 | usb->is_zd1211b ? |
e85d0918 DD |
230 | FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX, |
231 | postfix); | |
232 | return buffer; | |
233 | } | |
234 | ||
74553aed | 235 | static int handle_version_mismatch(struct zd_usb *usb, |
d066c219 DD |
236 | const struct firmware *ub_fw) |
237 | { | |
74553aed | 238 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
d066c219 DD |
239 | const struct firmware *ur_fw = NULL; |
240 | int offset; | |
241 | int r = 0; | |
242 | char fw_name[128]; | |
243 | ||
244 | r = request_fw_file(&ur_fw, | |
74553aed | 245 | get_fw_name(usb, fw_name, sizeof(fw_name), "ur"), |
d066c219 DD |
246 | &udev->dev); |
247 | if (r) | |
248 | goto error; | |
249 | ||
ee302767 | 250 | r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT); |
d066c219 DD |
251 | if (r) |
252 | goto error; | |
253 | ||
ee302767 | 254 | offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16)); |
d066c219 | 255 | r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset, |
ee302767 | 256 | E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT); |
d066c219 DD |
257 | |
258 | /* At this point, the vendor driver downloads the whole firmware | |
259 | * image, hacks around with version IDs, and uploads it again, | |
260 | * completely overwriting the boot code. We do not do this here as | |
261 | * it is not required on any tested devices, and it is suspected to | |
262 | * cause problems. */ | |
263 | error: | |
264 | release_firmware(ur_fw); | |
265 | return r; | |
266 | } | |
267 | ||
74553aed | 268 | static int upload_firmware(struct zd_usb *usb) |
e85d0918 DD |
269 | { |
270 | int r; | |
271 | u16 fw_bcdDevice; | |
272 | u16 bcdDevice; | |
74553aed | 273 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
e85d0918 DD |
274 | const struct firmware *ub_fw = NULL; |
275 | const struct firmware *uph_fw = NULL; | |
276 | char fw_name[128]; | |
277 | ||
278 | bcdDevice = get_bcdDevice(udev); | |
279 | ||
280 | r = request_fw_file(&ub_fw, | |
74553aed | 281 | get_fw_name(usb, fw_name, sizeof(fw_name), "ub"), |
e85d0918 DD |
282 | &udev->dev); |
283 | if (r) | |
284 | goto error; | |
285 | ||
ee302767 | 286 | fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET); |
e85d0918 | 287 | |
e85d0918 DD |
288 | if (fw_bcdDevice != bcdDevice) { |
289 | dev_info(&udev->dev, | |
d066c219 DD |
290 | "firmware version %#06x and device bootcode version " |
291 | "%#06x differ\n", fw_bcdDevice, bcdDevice); | |
292 | if (bcdDevice <= 0x4313) | |
293 | dev_warn(&udev->dev, "device has old bootcode, please " | |
294 | "report success or failure\n"); | |
295 | ||
74553aed | 296 | r = handle_version_mismatch(usb, ub_fw); |
d066c219 DD |
297 | if (r) |
298 | goto error; | |
e85d0918 DD |
299 | } else { |
300 | dev_dbg_f(&udev->dev, | |
301 | "firmware device id %#06x is equal to the " | |
302 | "actual device id\n", fw_bcdDevice); | |
303 | } | |
304 | ||
305 | ||
306 | r = request_fw_file(&uph_fw, | |
74553aed | 307 | get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"), |
e85d0918 DD |
308 | &udev->dev); |
309 | if (r) | |
310 | goto error; | |
311 | ||
ee302767 | 312 | r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT); |
e85d0918 DD |
313 | if (r) { |
314 | dev_err(&udev->dev, | |
315 | "Could not upload firmware code uph. Error number %d\n", | |
316 | r); | |
317 | } | |
318 | ||
319 | /* FALL-THROUGH */ | |
320 | error: | |
321 | release_firmware(ub_fw); | |
322 | release_firmware(uph_fw); | |
323 | return r; | |
324 | } | |
325 | ||
3e8b4d00 BH |
326 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ur"); |
327 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "ur"); | |
328 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ub"); | |
329 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "ub"); | |
330 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "uphr"); | |
331 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "uphr"); | |
332 | ||
74553aed DD |
333 | /* Read data from device address space using "firmware interface" which does |
334 | * not require firmware to be loaded. */ | |
335 | int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len) | |
336 | { | |
337 | int r; | |
338 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
a8c4ea7a | 339 | u8 *buf; |
74553aed | 340 | |
a8c4ea7a AN |
341 | /* Use "DMA-aware" buffer. */ |
342 | buf = kmalloc(len, GFP_KERNEL); | |
343 | if (!buf) | |
344 | return -ENOMEM; | |
74553aed DD |
345 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
346 | USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0, | |
a8c4ea7a | 347 | buf, len, 5000); |
74553aed DD |
348 | if (r < 0) { |
349 | dev_err(&udev->dev, | |
350 | "read over firmware interface failed: %d\n", r); | |
a8c4ea7a | 351 | goto exit; |
74553aed DD |
352 | } else if (r != len) { |
353 | dev_err(&udev->dev, | |
354 | "incomplete read over firmware interface: %d/%d\n", | |
355 | r, len); | |
a8c4ea7a AN |
356 | r = -EIO; |
357 | goto exit; | |
74553aed | 358 | } |
a8c4ea7a AN |
359 | r = 0; |
360 | memcpy(data, buf, len); | |
361 | exit: | |
362 | kfree(buf); | |
363 | return r; | |
74553aed DD |
364 | } |
365 | ||
e85d0918 DD |
366 | #define urb_dev(urb) (&(urb)->dev->dev) |
367 | ||
368 | static inline void handle_regs_int(struct urb *urb) | |
369 | { | |
370 | struct zd_usb *usb = urb->context; | |
371 | struct zd_usb_interrupt *intr = &usb->intr; | |
372 | int len; | |
72e77a8a | 373 | u16 int_num; |
e85d0918 DD |
374 | |
375 | ZD_ASSERT(in_interrupt()); | |
376 | spin_lock(&intr->lock); | |
377 | ||
d63ddcec | 378 | int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2)); |
72e77a8a LCC |
379 | if (int_num == CR_INTERRUPT) { |
380 | struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context)); | |
8b17f75c | 381 | spin_lock(&mac->lock); |
72e77a8a LCC |
382 | memcpy(&mac->intr_buffer, urb->transfer_buffer, |
383 | USB_MAX_EP_INT_BUFFER); | |
8b17f75c | 384 | spin_unlock(&mac->lock); |
72e77a8a LCC |
385 | schedule_work(&mac->process_intr); |
386 | } else if (intr->read_regs_enabled) { | |
e85d0918 DD |
387 | intr->read_regs.length = len = urb->actual_length; |
388 | ||
389 | if (len > sizeof(intr->read_regs.buffer)) | |
390 | len = sizeof(intr->read_regs.buffer); | |
391 | memcpy(intr->read_regs.buffer, urb->transfer_buffer, len); | |
392 | intr->read_regs_enabled = 0; | |
393 | complete(&intr->read_regs.completion); | |
394 | goto out; | |
395 | } | |
396 | ||
e85d0918 DD |
397 | out: |
398 | spin_unlock(&intr->lock); | |
399 | } | |
400 | ||
7d12e780 | 401 | static void int_urb_complete(struct urb *urb) |
e85d0918 DD |
402 | { |
403 | int r; | |
404 | struct usb_int_header *hdr; | |
405 | ||
406 | switch (urb->status) { | |
407 | case 0: | |
408 | break; | |
409 | case -ESHUTDOWN: | |
410 | case -EINVAL: | |
411 | case -ENODEV: | |
412 | case -ENOENT: | |
413 | case -ECONNRESET: | |
e85d0918 | 414 | case -EPIPE: |
24d24c62 | 415 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
4a3b0874 | 416 | return; |
e85d0918 | 417 | default: |
24d24c62 | 418 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
e85d0918 DD |
419 | goto resubmit; |
420 | } | |
421 | ||
422 | if (urb->actual_length < sizeof(hdr)) { | |
423 | dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb); | |
424 | goto resubmit; | |
425 | } | |
426 | ||
427 | hdr = urb->transfer_buffer; | |
428 | if (hdr->type != USB_INT_TYPE) { | |
429 | dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb); | |
430 | goto resubmit; | |
431 | } | |
432 | ||
433 | switch (hdr->id) { | |
434 | case USB_INT_ID_REGS: | |
435 | handle_regs_int(urb); | |
436 | break; | |
437 | case USB_INT_ID_RETRY_FAILED: | |
7f4013f0 | 438 | zd_mac_tx_failed(urb); |
e85d0918 DD |
439 | break; |
440 | default: | |
441 | dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb, | |
442 | (unsigned int)hdr->id); | |
443 | goto resubmit; | |
444 | } | |
445 | ||
446 | resubmit: | |
447 | r = usb_submit_urb(urb, GFP_ATOMIC); | |
448 | if (r) { | |
4a3b0874 JK |
449 | dev_dbg_f(urb_dev(urb), "error: resubmit urb %p err code %d\n", |
450 | urb, r); | |
451 | /* TODO: add worker to reset intr->urb */ | |
e85d0918 DD |
452 | } |
453 | return; | |
e85d0918 DD |
454 | } |
455 | ||
456 | static inline int int_urb_interval(struct usb_device *udev) | |
457 | { | |
458 | switch (udev->speed) { | |
459 | case USB_SPEED_HIGH: | |
460 | return 4; | |
461 | case USB_SPEED_LOW: | |
462 | return 10; | |
463 | case USB_SPEED_FULL: | |
464 | default: | |
465 | return 1; | |
466 | } | |
467 | } | |
468 | ||
469 | static inline int usb_int_enabled(struct zd_usb *usb) | |
470 | { | |
471 | unsigned long flags; | |
472 | struct zd_usb_interrupt *intr = &usb->intr; | |
473 | struct urb *urb; | |
474 | ||
475 | spin_lock_irqsave(&intr->lock, flags); | |
476 | urb = intr->urb; | |
477 | spin_unlock_irqrestore(&intr->lock, flags); | |
478 | return urb != NULL; | |
479 | } | |
480 | ||
481 | int zd_usb_enable_int(struct zd_usb *usb) | |
482 | { | |
483 | int r; | |
4a3b0874 | 484 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
e85d0918 | 485 | struct zd_usb_interrupt *intr = &usb->intr; |
e85d0918 DD |
486 | struct urb *urb; |
487 | ||
488 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
489 | ||
35c3404e | 490 | urb = usb_alloc_urb(0, GFP_KERNEL); |
e85d0918 DD |
491 | if (!urb) { |
492 | r = -ENOMEM; | |
493 | goto out; | |
494 | } | |
495 | ||
496 | ZD_ASSERT(!irqs_disabled()); | |
497 | spin_lock_irq(&intr->lock); | |
498 | if (intr->urb) { | |
499 | spin_unlock_irq(&intr->lock); | |
500 | r = 0; | |
501 | goto error_free_urb; | |
502 | } | |
503 | intr->urb = urb; | |
504 | spin_unlock_irq(&intr->lock); | |
505 | ||
e85d0918 | 506 | r = -ENOMEM; |
4a3b0874 JK |
507 | intr->buffer = usb_alloc_coherent(udev, USB_MAX_EP_INT_BUFFER, |
508 | GFP_KERNEL, &intr->buffer_dma); | |
509 | if (!intr->buffer) { | |
e85d0918 DD |
510 | dev_dbg_f(zd_usb_dev(usb), |
511 | "couldn't allocate transfer_buffer\n"); | |
512 | goto error_set_urb_null; | |
513 | } | |
514 | ||
e85d0918 | 515 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), |
4a3b0874 | 516 | intr->buffer, USB_MAX_EP_INT_BUFFER, |
e85d0918 DD |
517 | int_urb_complete, usb, |
518 | intr->interval); | |
4a3b0874 JK |
519 | urb->transfer_dma = intr->buffer_dma; |
520 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
e85d0918 DD |
521 | |
522 | dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb); | |
35c3404e | 523 | r = usb_submit_urb(urb, GFP_KERNEL); |
e85d0918 DD |
524 | if (r) { |
525 | dev_dbg_f(zd_usb_dev(usb), | |
526 | "Couldn't submit urb. Error number %d\n", r); | |
527 | goto error; | |
528 | } | |
529 | ||
530 | return 0; | |
531 | error: | |
4a3b0874 JK |
532 | usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER, |
533 | intr->buffer, intr->buffer_dma); | |
e85d0918 DD |
534 | error_set_urb_null: |
535 | spin_lock_irq(&intr->lock); | |
536 | intr->urb = NULL; | |
537 | spin_unlock_irq(&intr->lock); | |
538 | error_free_urb: | |
539 | usb_free_urb(urb); | |
540 | out: | |
541 | return r; | |
542 | } | |
543 | ||
544 | void zd_usb_disable_int(struct zd_usb *usb) | |
545 | { | |
546 | unsigned long flags; | |
4a3b0874 | 547 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
e85d0918 DD |
548 | struct zd_usb_interrupt *intr = &usb->intr; |
549 | struct urb *urb; | |
4a3b0874 JK |
550 | void *buffer; |
551 | dma_addr_t buffer_dma; | |
e85d0918 DD |
552 | |
553 | spin_lock_irqsave(&intr->lock, flags); | |
554 | urb = intr->urb; | |
555 | if (!urb) { | |
556 | spin_unlock_irqrestore(&intr->lock, flags); | |
557 | return; | |
558 | } | |
559 | intr->urb = NULL; | |
4a3b0874 JK |
560 | buffer = intr->buffer; |
561 | buffer_dma = intr->buffer_dma; | |
562 | intr->buffer = NULL; | |
e85d0918 DD |
563 | spin_unlock_irqrestore(&intr->lock, flags); |
564 | ||
565 | usb_kill_urb(urb); | |
566 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); | |
567 | usb_free_urb(urb); | |
4a3b0874 JK |
568 | |
569 | if (buffer) | |
570 | usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER, | |
571 | buffer, buffer_dma); | |
e85d0918 DD |
572 | } |
573 | ||
574 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, | |
575 | unsigned int length) | |
576 | { | |
577 | int i; | |
e85d0918 DD |
578 | const struct rx_length_info *length_info; |
579 | ||
580 | if (length < sizeof(struct rx_length_info)) { | |
581 | /* It's not a complete packet anyhow. */ | |
7f4013f0 BP |
582 | printk("%s: invalid, small RX packet : %d\n", |
583 | __func__, length); | |
e85d0918 DD |
584 | return; |
585 | } | |
586 | length_info = (struct rx_length_info *) | |
587 | (buffer + length - sizeof(struct rx_length_info)); | |
588 | ||
589 | /* It might be that three frames are merged into a single URB | |
590 | * transaction. We have to check for the length info tag. | |
591 | * | |
592 | * While testing we discovered that length_info might be unaligned, | |
593 | * because if USB transactions are merged, the last packet will not | |
594 | * be padded. Unaligned access might also happen if the length_info | |
595 | * structure is not present. | |
596 | */ | |
533dd1b0 | 597 | if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG) |
b269825b | 598 | { |
e85d0918 DD |
599 | unsigned int l, k, n; |
600 | for (i = 0, l = 0;; i++) { | |
533dd1b0 | 601 | k = get_unaligned_le16(&length_info->length[i]); |
850c211c UK |
602 | if (k == 0) |
603 | return; | |
e85d0918 DD |
604 | n = l+k; |
605 | if (n > length) | |
606 | return; | |
459c51ad | 607 | zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k); |
e85d0918 DD |
608 | if (i >= 2) |
609 | return; | |
610 | l = (n+3) & ~3; | |
611 | } | |
612 | } else { | |
459c51ad | 613 | zd_mac_rx(zd_usb_to_hw(usb), buffer, length); |
e85d0918 DD |
614 | } |
615 | } | |
616 | ||
7d12e780 | 617 | static void rx_urb_complete(struct urb *urb) |
e85d0918 | 618 | { |
24d24c62 | 619 | int r; |
e85d0918 DD |
620 | struct zd_usb *usb; |
621 | struct zd_usb_rx *rx; | |
622 | const u8 *buffer; | |
623 | unsigned int length; | |
624 | ||
625 | switch (urb->status) { | |
626 | case 0: | |
627 | break; | |
628 | case -ESHUTDOWN: | |
629 | case -EINVAL: | |
630 | case -ENODEV: | |
631 | case -ENOENT: | |
632 | case -ECONNRESET: | |
e85d0918 | 633 | case -EPIPE: |
24d24c62 | 634 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
b312d799 | 635 | return; |
e85d0918 DD |
636 | default: |
637 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
638 | goto resubmit; | |
639 | } | |
640 | ||
641 | buffer = urb->transfer_buffer; | |
642 | length = urb->actual_length; | |
643 | usb = urb->context; | |
644 | rx = &usb->rx; | |
645 | ||
02353573 | 646 | tasklet_schedule(&rx->reset_timer_tasklet); |
1f6ccccc | 647 | |
e85d0918 DD |
648 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { |
649 | /* If there is an old first fragment, we don't care. */ | |
650 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); | |
651 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); | |
652 | spin_lock(&rx->lock); | |
653 | memcpy(rx->fragment, buffer, length); | |
654 | rx->fragment_length = length; | |
655 | spin_unlock(&rx->lock); | |
656 | goto resubmit; | |
657 | } | |
658 | ||
659 | spin_lock(&rx->lock); | |
660 | if (rx->fragment_length > 0) { | |
661 | /* We are on a second fragment, we believe */ | |
662 | ZD_ASSERT(length + rx->fragment_length <= | |
663 | ARRAY_SIZE(rx->fragment)); | |
664 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); | |
665 | memcpy(rx->fragment+rx->fragment_length, buffer, length); | |
666 | handle_rx_packet(usb, rx->fragment, | |
667 | rx->fragment_length + length); | |
668 | rx->fragment_length = 0; | |
669 | spin_unlock(&rx->lock); | |
670 | } else { | |
671 | spin_unlock(&rx->lock); | |
672 | handle_rx_packet(usb, buffer, length); | |
673 | } | |
674 | ||
675 | resubmit: | |
24d24c62 JK |
676 | r = usb_submit_urb(urb, GFP_ATOMIC); |
677 | if (r) | |
678 | dev_dbg_f(urb_dev(urb), "urb %p resubmit error %d\n", urb, r); | |
e85d0918 DD |
679 | } |
680 | ||
459c51ad | 681 | static struct urb *alloc_rx_urb(struct zd_usb *usb) |
e85d0918 DD |
682 | { |
683 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
684 | struct urb *urb; | |
685 | void *buffer; | |
686 | ||
35c3404e | 687 | urb = usb_alloc_urb(0, GFP_KERNEL); |
e85d0918 DD |
688 | if (!urb) |
689 | return NULL; | |
997ea58e DM |
690 | buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL, |
691 | &urb->transfer_dma); | |
e85d0918 DD |
692 | if (!buffer) { |
693 | usb_free_urb(urb); | |
694 | return NULL; | |
695 | } | |
696 | ||
697 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), | |
997ea58e | 698 | buffer, USB_MAX_RX_SIZE, |
e85d0918 DD |
699 | rx_urb_complete, usb); |
700 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
701 | ||
702 | return urb; | |
703 | } | |
704 | ||
459c51ad | 705 | static void free_rx_urb(struct urb *urb) |
e85d0918 DD |
706 | { |
707 | if (!urb) | |
708 | return; | |
997ea58e DM |
709 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
710 | urb->transfer_buffer, urb->transfer_dma); | |
e85d0918 DD |
711 | usb_free_urb(urb); |
712 | } | |
713 | ||
1f6ccccc | 714 | static int __zd_usb_enable_rx(struct zd_usb *usb) |
e85d0918 DD |
715 | { |
716 | int i, r; | |
717 | struct zd_usb_rx *rx = &usb->rx; | |
718 | struct urb **urbs; | |
719 | ||
720 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
721 | ||
722 | r = -ENOMEM; | |
459c51ad | 723 | urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL); |
e85d0918 DD |
724 | if (!urbs) |
725 | goto error; | |
459c51ad DD |
726 | for (i = 0; i < RX_URBS_COUNT; i++) { |
727 | urbs[i] = alloc_rx_urb(usb); | |
e85d0918 DD |
728 | if (!urbs[i]) |
729 | goto error; | |
730 | } | |
731 | ||
732 | ZD_ASSERT(!irqs_disabled()); | |
733 | spin_lock_irq(&rx->lock); | |
734 | if (rx->urbs) { | |
735 | spin_unlock_irq(&rx->lock); | |
736 | r = 0; | |
737 | goto error; | |
738 | } | |
739 | rx->urbs = urbs; | |
459c51ad | 740 | rx->urbs_count = RX_URBS_COUNT; |
e85d0918 DD |
741 | spin_unlock_irq(&rx->lock); |
742 | ||
459c51ad | 743 | for (i = 0; i < RX_URBS_COUNT; i++) { |
35c3404e | 744 | r = usb_submit_urb(urbs[i], GFP_KERNEL); |
e85d0918 DD |
745 | if (r) |
746 | goto error_submit; | |
747 | } | |
748 | ||
749 | return 0; | |
750 | error_submit: | |
459c51ad | 751 | for (i = 0; i < RX_URBS_COUNT; i++) { |
e85d0918 DD |
752 | usb_kill_urb(urbs[i]); |
753 | } | |
754 | spin_lock_irq(&rx->lock); | |
755 | rx->urbs = NULL; | |
756 | rx->urbs_count = 0; | |
757 | spin_unlock_irq(&rx->lock); | |
758 | error: | |
759 | if (urbs) { | |
459c51ad DD |
760 | for (i = 0; i < RX_URBS_COUNT; i++) |
761 | free_rx_urb(urbs[i]); | |
e85d0918 DD |
762 | } |
763 | return r; | |
764 | } | |
765 | ||
1f6ccccc JK |
766 | int zd_usb_enable_rx(struct zd_usb *usb) |
767 | { | |
768 | int r; | |
769 | struct zd_usb_rx *rx = &usb->rx; | |
770 | ||
771 | mutex_lock(&rx->setup_mutex); | |
772 | r = __zd_usb_enable_rx(usb); | |
773 | mutex_unlock(&rx->setup_mutex); | |
774 | ||
775 | zd_usb_reset_rx_idle_timer(usb); | |
776 | ||
777 | return r; | |
778 | } | |
779 | ||
780 | static void __zd_usb_disable_rx(struct zd_usb *usb) | |
e85d0918 DD |
781 | { |
782 | int i; | |
783 | unsigned long flags; | |
784 | struct urb **urbs; | |
785 | unsigned int count; | |
786 | struct zd_usb_rx *rx = &usb->rx; | |
787 | ||
788 | spin_lock_irqsave(&rx->lock, flags); | |
789 | urbs = rx->urbs; | |
790 | count = rx->urbs_count; | |
791 | spin_unlock_irqrestore(&rx->lock, flags); | |
792 | if (!urbs) | |
793 | return; | |
794 | ||
795 | for (i = 0; i < count; i++) { | |
796 | usb_kill_urb(urbs[i]); | |
459c51ad | 797 | free_rx_urb(urbs[i]); |
e85d0918 DD |
798 | } |
799 | kfree(urbs); | |
800 | ||
801 | spin_lock_irqsave(&rx->lock, flags); | |
802 | rx->urbs = NULL; | |
803 | rx->urbs_count = 0; | |
804 | spin_unlock_irqrestore(&rx->lock, flags); | |
805 | } | |
806 | ||
1f6ccccc JK |
807 | void zd_usb_disable_rx(struct zd_usb *usb) |
808 | { | |
809 | struct zd_usb_rx *rx = &usb->rx; | |
810 | ||
811 | mutex_lock(&rx->setup_mutex); | |
812 | __zd_usb_disable_rx(usb); | |
813 | mutex_unlock(&rx->setup_mutex); | |
814 | ||
02353573 | 815 | tasklet_kill(&rx->reset_timer_tasklet); |
1f6ccccc JK |
816 | cancel_delayed_work_sync(&rx->idle_work); |
817 | } | |
818 | ||
819 | static void zd_usb_reset_rx(struct zd_usb *usb) | |
820 | { | |
821 | bool do_reset; | |
822 | struct zd_usb_rx *rx = &usb->rx; | |
823 | unsigned long flags; | |
824 | ||
825 | mutex_lock(&rx->setup_mutex); | |
826 | ||
827 | spin_lock_irqsave(&rx->lock, flags); | |
828 | do_reset = rx->urbs != NULL; | |
829 | spin_unlock_irqrestore(&rx->lock, flags); | |
830 | ||
831 | if (do_reset) { | |
832 | __zd_usb_disable_rx(usb); | |
833 | __zd_usb_enable_rx(usb); | |
834 | } | |
835 | ||
836 | mutex_unlock(&rx->setup_mutex); | |
837 | ||
838 | if (do_reset) | |
839 | zd_usb_reset_rx_idle_timer(usb); | |
840 | } | |
841 | ||
459c51ad DD |
842 | /** |
843 | * zd_usb_disable_tx - disable transmission | |
844 | * @usb: the zd1211rw-private USB structure | |
845 | * | |
846 | * Frees all URBs in the free list and marks the transmission as disabled. | |
847 | */ | |
848 | void zd_usb_disable_tx(struct zd_usb *usb) | |
849 | { | |
850 | struct zd_usb_tx *tx = &usb->tx; | |
851 | unsigned long flags; | |
78fc800f JK |
852 | |
853 | atomic_set(&tx->enabled, 0); | |
854 | ||
855 | /* kill all submitted tx-urbs */ | |
856 | usb_kill_anchored_urbs(&tx->submitted); | |
459c51ad DD |
857 | |
858 | spin_lock_irqsave(&tx->lock, flags); | |
a0fd751f | 859 | WARN_ON(!skb_queue_empty(&tx->submitted_skbs)); |
78fc800f | 860 | WARN_ON(tx->submitted_urbs != 0); |
459c51ad | 861 | tx->submitted_urbs = 0; |
78fc800f JK |
862 | spin_unlock_irqrestore(&tx->lock, flags); |
863 | ||
459c51ad DD |
864 | /* The stopped state is ignored, relying on ieee80211_wake_queues() |
865 | * in a potentionally following zd_usb_enable_tx(). | |
866 | */ | |
459c51ad DD |
867 | } |
868 | ||
869 | /** | |
870 | * zd_usb_enable_tx - enables transmission | |
871 | * @usb: a &struct zd_usb pointer | |
872 | * | |
873 | * This function enables transmission and prepares the &zd_usb_tx data | |
874 | * structure. | |
875 | */ | |
876 | void zd_usb_enable_tx(struct zd_usb *usb) | |
877 | { | |
878 | unsigned long flags; | |
879 | struct zd_usb_tx *tx = &usb->tx; | |
880 | ||
881 | spin_lock_irqsave(&tx->lock, flags); | |
78fc800f | 882 | atomic_set(&tx->enabled, 1); |
459c51ad DD |
883 | tx->submitted_urbs = 0; |
884 | ieee80211_wake_queues(zd_usb_to_hw(usb)); | |
885 | tx->stopped = 0; | |
886 | spin_unlock_irqrestore(&tx->lock, flags); | |
887 | } | |
888 | ||
459c51ad DD |
889 | static void tx_dec_submitted_urbs(struct zd_usb *usb) |
890 | { | |
891 | struct zd_usb_tx *tx = &usb->tx; | |
892 | unsigned long flags; | |
893 | ||
894 | spin_lock_irqsave(&tx->lock, flags); | |
895 | --tx->submitted_urbs; | |
896 | if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) { | |
897 | ieee80211_wake_queues(zd_usb_to_hw(usb)); | |
898 | tx->stopped = 0; | |
899 | } | |
900 | spin_unlock_irqrestore(&tx->lock, flags); | |
901 | } | |
902 | ||
903 | static void tx_inc_submitted_urbs(struct zd_usb *usb) | |
904 | { | |
905 | struct zd_usb_tx *tx = &usb->tx; | |
906 | unsigned long flags; | |
907 | ||
908 | spin_lock_irqsave(&tx->lock, flags); | |
909 | ++tx->submitted_urbs; | |
910 | if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) { | |
911 | ieee80211_stop_queues(zd_usb_to_hw(usb)); | |
912 | tx->stopped = 1; | |
913 | } | |
914 | spin_unlock_irqrestore(&tx->lock, flags); | |
915 | } | |
916 | ||
917 | /** | |
918 | * tx_urb_complete - completes the execution of an URB | |
919 | * @urb: a URB | |
920 | * | |
921 | * This function is called if the URB has been transferred to a device or an | |
922 | * error has happened. | |
923 | */ | |
7d12e780 | 924 | static void tx_urb_complete(struct urb *urb) |
e85d0918 DD |
925 | { |
926 | int r; | |
459c51ad | 927 | struct sk_buff *skb; |
e039fa4a | 928 | struct ieee80211_tx_info *info; |
459c51ad | 929 | struct zd_usb *usb; |
78fc800f JK |
930 | struct zd_usb_tx *tx; |
931 | ||
932 | skb = (struct sk_buff *)urb->context; | |
933 | info = IEEE80211_SKB_CB(skb); | |
934 | /* | |
935 | * grab 'usb' pointer before handing off the skb (since | |
936 | * it might be freed by zd_mac_tx_to_dev or mac80211) | |
937 | */ | |
938 | usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb; | |
939 | tx = &usb->tx; | |
e85d0918 DD |
940 | |
941 | switch (urb->status) { | |
942 | case 0: | |
943 | break; | |
944 | case -ESHUTDOWN: | |
945 | case -EINVAL: | |
946 | case -ENODEV: | |
947 | case -ENOENT: | |
948 | case -ECONNRESET: | |
b312d799 | 949 | case -EPIPE: |
e85d0918 DD |
950 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
951 | break; | |
e85d0918 DD |
952 | default: |
953 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
954 | goto resubmit; | |
955 | } | |
956 | free_urb: | |
a0fd751f | 957 | skb_unlink(skb, &usb->tx.submitted_skbs); |
dbabad0c | 958 | zd_mac_tx_to_dev(skb, urb->status); |
78fc800f | 959 | usb_free_urb(urb); |
459c51ad | 960 | tx_dec_submitted_urbs(usb); |
e85d0918 DD |
961 | return; |
962 | resubmit: | |
78fc800f | 963 | usb_anchor_urb(urb, &tx->submitted); |
e85d0918 DD |
964 | r = usb_submit_urb(urb, GFP_ATOMIC); |
965 | if (r) { | |
78fc800f | 966 | usb_unanchor_urb(urb); |
e85d0918 DD |
967 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); |
968 | goto free_urb; | |
969 | } | |
970 | } | |
971 | ||
459c51ad DD |
972 | /** |
973 | * zd_usb_tx: initiates transfer of a frame of the device | |
974 | * | |
975 | * @usb: the zd1211rw-private USB structure | |
976 | * @skb: a &struct sk_buff pointer | |
977 | * | |
978 | * This function tranmits a frame to the device. It doesn't wait for | |
979 | * completion. The frame must contain the control set and have all the | |
980 | * control set information available. | |
981 | * | |
982 | * The function returns 0 if the transfer has been successfully initiated. | |
e85d0918 | 983 | */ |
459c51ad | 984 | int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb) |
e85d0918 DD |
985 | { |
986 | int r; | |
a0fd751f | 987 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
e85d0918 DD |
988 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
989 | struct urb *urb; | |
78fc800f | 990 | struct zd_usb_tx *tx = &usb->tx; |
e85d0918 | 991 | |
78fc800f JK |
992 | if (!atomic_read(&tx->enabled)) { |
993 | r = -ENOENT; | |
994 | goto out; | |
995 | } | |
996 | ||
997 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
e85d0918 DD |
998 | if (!urb) { |
999 | r = -ENOMEM; | |
1000 | goto out; | |
1001 | } | |
1002 | ||
e85d0918 | 1003 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
459c51ad | 1004 | skb->data, skb->len, tx_urb_complete, skb); |
e85d0918 | 1005 | |
a0fd751f JK |
1006 | info->rate_driver_data[1] = (void *)jiffies; |
1007 | skb_queue_tail(&tx->submitted_skbs, skb); | |
78fc800f | 1008 | usb_anchor_urb(urb, &tx->submitted); |
a0fd751f | 1009 | |
e85d0918 | 1010 | r = usb_submit_urb(urb, GFP_ATOMIC); |
78fc800f | 1011 | if (r) { |
24d24c62 | 1012 | dev_dbg_f(zd_usb_dev(usb), "error submit urb %p %d\n", urb, r); |
78fc800f | 1013 | usb_unanchor_urb(urb); |
a0fd751f | 1014 | skb_unlink(skb, &tx->submitted_skbs); |
e85d0918 | 1015 | goto error; |
78fc800f | 1016 | } |
459c51ad | 1017 | tx_inc_submitted_urbs(usb); |
e85d0918 DD |
1018 | return 0; |
1019 | error: | |
78fc800f | 1020 | usb_free_urb(urb); |
e85d0918 DD |
1021 | out: |
1022 | return r; | |
1023 | } | |
1024 | ||
a0fd751f JK |
1025 | static bool zd_tx_timeout(struct zd_usb *usb) |
1026 | { | |
1027 | struct zd_usb_tx *tx = &usb->tx; | |
1028 | struct sk_buff_head *q = &tx->submitted_skbs; | |
1029 | struct sk_buff *skb, *skbnext; | |
1030 | struct ieee80211_tx_info *info; | |
1031 | unsigned long flags, trans_start; | |
1032 | bool have_timedout = false; | |
1033 | ||
1034 | spin_lock_irqsave(&q->lock, flags); | |
1035 | skb_queue_walk_safe(q, skb, skbnext) { | |
1036 | info = IEEE80211_SKB_CB(skb); | |
1037 | trans_start = (unsigned long)info->rate_driver_data[1]; | |
1038 | ||
1039 | if (time_is_before_jiffies(trans_start + ZD_TX_TIMEOUT)) { | |
1040 | have_timedout = true; | |
1041 | break; | |
1042 | } | |
1043 | } | |
1044 | spin_unlock_irqrestore(&q->lock, flags); | |
1045 | ||
1046 | return have_timedout; | |
1047 | } | |
1048 | ||
1049 | static void zd_tx_watchdog_handler(struct work_struct *work) | |
1050 | { | |
1051 | struct zd_usb *usb = | |
1052 | container_of(work, struct zd_usb, tx.watchdog_work.work); | |
1053 | struct zd_usb_tx *tx = &usb->tx; | |
1054 | ||
1055 | if (!atomic_read(&tx->enabled) || !tx->watchdog_enabled) | |
1056 | goto out; | |
1057 | if (!zd_tx_timeout(usb)) | |
1058 | goto out; | |
1059 | ||
1060 | /* TX halted, try reset */ | |
1061 | dev_warn(zd_usb_dev(usb), "TX-stall detected, reseting device..."); | |
1062 | ||
1063 | usb_queue_reset_device(usb->intf); | |
1064 | ||
1065 | /* reset will stop this worker, don't rearm */ | |
1066 | return; | |
1067 | out: | |
1068 | queue_delayed_work(zd_workqueue, &tx->watchdog_work, | |
1069 | ZD_TX_WATCHDOG_INTERVAL); | |
1070 | } | |
1071 | ||
1072 | void zd_tx_watchdog_enable(struct zd_usb *usb) | |
1073 | { | |
1074 | struct zd_usb_tx *tx = &usb->tx; | |
1075 | ||
1076 | if (!tx->watchdog_enabled) { | |
1077 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1078 | queue_delayed_work(zd_workqueue, &tx->watchdog_work, | |
1079 | ZD_TX_WATCHDOG_INTERVAL); | |
1080 | tx->watchdog_enabled = 1; | |
1081 | } | |
1082 | } | |
1083 | ||
1084 | void zd_tx_watchdog_disable(struct zd_usb *usb) | |
1085 | { | |
1086 | struct zd_usb_tx *tx = &usb->tx; | |
1087 | ||
1088 | if (tx->watchdog_enabled) { | |
1089 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1090 | tx->watchdog_enabled = 0; | |
1091 | cancel_delayed_work_sync(&tx->watchdog_work); | |
1092 | } | |
1093 | } | |
1094 | ||
1f6ccccc JK |
1095 | static void zd_rx_idle_timer_handler(struct work_struct *work) |
1096 | { | |
1097 | struct zd_usb *usb = | |
1098 | container_of(work, struct zd_usb, rx.idle_work.work); | |
1099 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1100 | ||
1101 | if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags)) | |
1102 | return; | |
1103 | ||
1104 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1105 | ||
1106 | /* 30 seconds since last rx, reset rx */ | |
1107 | zd_usb_reset_rx(usb); | |
1108 | } | |
1109 | ||
02353573 JK |
1110 | static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param) |
1111 | { | |
1112 | struct zd_usb *usb = (struct zd_usb *)param; | |
1113 | ||
1114 | zd_usb_reset_rx_idle_timer(usb); | |
1115 | } | |
1116 | ||
1f6ccccc JK |
1117 | void zd_usb_reset_rx_idle_timer(struct zd_usb *usb) |
1118 | { | |
1119 | struct zd_usb_rx *rx = &usb->rx; | |
1120 | ||
1121 | cancel_delayed_work(&rx->idle_work); | |
1122 | queue_delayed_work(zd_workqueue, &rx->idle_work, ZD_RX_IDLE_INTERVAL); | |
1123 | } | |
1124 | ||
e85d0918 DD |
1125 | static inline void init_usb_interrupt(struct zd_usb *usb) |
1126 | { | |
1127 | struct zd_usb_interrupt *intr = &usb->intr; | |
1128 | ||
1129 | spin_lock_init(&intr->lock); | |
1130 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); | |
1131 | init_completion(&intr->read_regs.completion); | |
0ce34bc8 | 1132 | intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT); |
e85d0918 DD |
1133 | } |
1134 | ||
1135 | static inline void init_usb_rx(struct zd_usb *usb) | |
1136 | { | |
1137 | struct zd_usb_rx *rx = &usb->rx; | |
02353573 | 1138 | |
e85d0918 | 1139 | spin_lock_init(&rx->lock); |
1f6ccccc | 1140 | mutex_init(&rx->setup_mutex); |
e85d0918 DD |
1141 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { |
1142 | rx->usb_packet_size = 512; | |
1143 | } else { | |
1144 | rx->usb_packet_size = 64; | |
1145 | } | |
1146 | ZD_ASSERT(rx->fragment_length == 0); | |
1f6ccccc | 1147 | INIT_DELAYED_WORK(&rx->idle_work, zd_rx_idle_timer_handler); |
02353573 JK |
1148 | rx->reset_timer_tasklet.func = zd_usb_reset_rx_idle_timer_tasklet; |
1149 | rx->reset_timer_tasklet.data = (unsigned long)usb; | |
e85d0918 DD |
1150 | } |
1151 | ||
1152 | static inline void init_usb_tx(struct zd_usb *usb) | |
1153 | { | |
459c51ad | 1154 | struct zd_usb_tx *tx = &usb->tx; |
02353573 | 1155 | |
459c51ad | 1156 | spin_lock_init(&tx->lock); |
78fc800f | 1157 | atomic_set(&tx->enabled, 0); |
459c51ad | 1158 | tx->stopped = 0; |
a0fd751f | 1159 | skb_queue_head_init(&tx->submitted_skbs); |
78fc800f | 1160 | init_usb_anchor(&tx->submitted); |
459c51ad | 1161 | tx->submitted_urbs = 0; |
a0fd751f JK |
1162 | tx->watchdog_enabled = 0; |
1163 | INIT_DELAYED_WORK(&tx->watchdog_work, zd_tx_watchdog_handler); | |
e85d0918 DD |
1164 | } |
1165 | ||
459c51ad | 1166 | void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw, |
e85d0918 DD |
1167 | struct usb_interface *intf) |
1168 | { | |
1169 | memset(usb, 0, sizeof(*usb)); | |
1170 | usb->intf = usb_get_intf(intf); | |
459c51ad | 1171 | usb_set_intfdata(usb->intf, hw); |
eefdbec1 | 1172 | init_usb_anchor(&usb->submitted_cmds); |
e85d0918 DD |
1173 | init_usb_interrupt(usb); |
1174 | init_usb_tx(usb); | |
1175 | init_usb_rx(usb); | |
1176 | } | |
1177 | ||
e85d0918 DD |
1178 | void zd_usb_clear(struct zd_usb *usb) |
1179 | { | |
1180 | usb_set_intfdata(usb->intf, NULL); | |
1181 | usb_put_intf(usb->intf); | |
c48cf125 | 1182 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
e85d0918 DD |
1183 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
1184 | } | |
1185 | ||
1186 | static const char *speed(enum usb_device_speed speed) | |
1187 | { | |
1188 | switch (speed) { | |
1189 | case USB_SPEED_LOW: | |
1190 | return "low"; | |
1191 | case USB_SPEED_FULL: | |
1192 | return "full"; | |
1193 | case USB_SPEED_HIGH: | |
1194 | return "high"; | |
1195 | default: | |
1196 | return "unknown speed"; | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) | |
1201 | { | |
1202 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", | |
1203 | le16_to_cpu(udev->descriptor.idVendor), | |
1204 | le16_to_cpu(udev->descriptor.idProduct), | |
1205 | get_bcdDevice(udev), | |
1206 | speed(udev->speed)); | |
1207 | } | |
1208 | ||
1209 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) | |
1210 | { | |
1211 | struct usb_device *udev = interface_to_usbdev(usb->intf); | |
1212 | return scnprint_id(udev, buffer, size); | |
1213 | } | |
1214 | ||
1215 | #ifdef DEBUG | |
1216 | static void print_id(struct usb_device *udev) | |
1217 | { | |
1218 | char buffer[40]; | |
1219 | ||
1220 | scnprint_id(udev, buffer, sizeof(buffer)); | |
1221 | buffer[sizeof(buffer)-1] = 0; | |
1222 | dev_dbg_f(&udev->dev, "%s\n", buffer); | |
1223 | } | |
1224 | #else | |
1225 | #define print_id(udev) do { } while (0) | |
1226 | #endif | |
1227 | ||
a1030e92 DD |
1228 | static int eject_installer(struct usb_interface *intf) |
1229 | { | |
1230 | struct usb_device *udev = interface_to_usbdev(intf); | |
1231 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; | |
1232 | struct usb_endpoint_descriptor *endpoint; | |
1233 | unsigned char *cmd; | |
1234 | u8 bulk_out_ep; | |
1235 | int r; | |
1236 | ||
1237 | /* Find bulk out endpoint */ | |
11466f13 SS |
1238 | for (r = 1; r >= 0; r--) { |
1239 | endpoint = &iface_desc->endpoint[r].desc; | |
1240 | if (usb_endpoint_dir_out(endpoint) && | |
1241 | usb_endpoint_xfer_bulk(endpoint)) { | |
1242 | bulk_out_ep = endpoint->bEndpointAddress; | |
1243 | break; | |
1244 | } | |
1245 | } | |
1246 | if (r == -1) { | |
a1030e92 DD |
1247 | dev_err(&udev->dev, |
1248 | "zd1211rw: Could not find bulk out endpoint\n"); | |
1249 | return -ENODEV; | |
1250 | } | |
1251 | ||
1252 | cmd = kzalloc(31, GFP_KERNEL); | |
1253 | if (cmd == NULL) | |
1254 | return -ENODEV; | |
1255 | ||
1256 | /* USB bulk command block */ | |
1257 | cmd[0] = 0x55; /* bulk command signature */ | |
1258 | cmd[1] = 0x53; /* bulk command signature */ | |
1259 | cmd[2] = 0x42; /* bulk command signature */ | |
1260 | cmd[3] = 0x43; /* bulk command signature */ | |
1261 | cmd[14] = 6; /* command length */ | |
1262 | ||
1263 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ | |
1264 | cmd[19] = 0x2; /* eject disc */ | |
1265 | ||
1266 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); | |
1267 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), | |
1268 | cmd, 31, NULL, 2000); | |
1269 | kfree(cmd); | |
1270 | if (r) | |
1271 | return r; | |
1272 | ||
1273 | /* At this point, the device disconnects and reconnects with the real | |
1274 | * ID numbers. */ | |
1275 | ||
1276 | usb_set_intfdata(intf, NULL); | |
1277 | return 0; | |
1278 | } | |
1279 | ||
74553aed DD |
1280 | int zd_usb_init_hw(struct zd_usb *usb) |
1281 | { | |
1282 | int r; | |
1283 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1284 | ||
1285 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1286 | ||
1287 | r = upload_firmware(usb); | |
1288 | if (r) { | |
1289 | dev_err(zd_usb_dev(usb), | |
1290 | "couldn't load firmware. Error number %d\n", r); | |
1291 | return r; | |
1292 | } | |
1293 | ||
1294 | r = usb_reset_configuration(zd_usb_to_usbdev(usb)); | |
1295 | if (r) { | |
1296 | dev_dbg_f(zd_usb_dev(usb), | |
1297 | "couldn't reset configuration. Error number %d\n", r); | |
1298 | return r; | |
1299 | } | |
1300 | ||
459c51ad | 1301 | r = zd_mac_init_hw(mac->hw); |
74553aed DD |
1302 | if (r) { |
1303 | dev_dbg_f(zd_usb_dev(usb), | |
1304 | "couldn't initialize mac. Error number %d\n", r); | |
1305 | return r; | |
1306 | } | |
1307 | ||
1308 | usb->initialized = 1; | |
1309 | return 0; | |
1310 | } | |
1311 | ||
e85d0918 DD |
1312 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
1313 | { | |
1314 | int r; | |
1315 | struct usb_device *udev = interface_to_usbdev(intf); | |
459c51ad DD |
1316 | struct zd_usb *usb; |
1317 | struct ieee80211_hw *hw = NULL; | |
e85d0918 DD |
1318 | |
1319 | print_id(udev); | |
1320 | ||
a1030e92 DD |
1321 | if (id->driver_info & DEVICE_INSTALLER) |
1322 | return eject_installer(intf); | |
1323 | ||
e85d0918 DD |
1324 | switch (udev->speed) { |
1325 | case USB_SPEED_LOW: | |
1326 | case USB_SPEED_FULL: | |
1327 | case USB_SPEED_HIGH: | |
1328 | break; | |
1329 | default: | |
1330 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); | |
1331 | r = -ENODEV; | |
1332 | goto error; | |
1333 | } | |
1334 | ||
459c51ad DD |
1335 | r = usb_reset_device(udev); |
1336 | if (r) { | |
1337 | dev_err(&intf->dev, | |
1338 | "couldn't reset usb device. Error number %d\n", r); | |
1339 | goto error; | |
1340 | } | |
6e3632f6 | 1341 | |
459c51ad DD |
1342 | hw = zd_mac_alloc_hw(intf); |
1343 | if (hw == NULL) { | |
e85d0918 DD |
1344 | r = -ENOMEM; |
1345 | goto error; | |
1346 | } | |
1347 | ||
459c51ad | 1348 | usb = &zd_hw_mac(hw)->chip.usb; |
74553aed | 1349 | usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0; |
e85d0918 | 1350 | |
459c51ad | 1351 | r = zd_mac_preinit_hw(hw); |
e85d0918 DD |
1352 | if (r) { |
1353 | dev_dbg_f(&intf->dev, | |
1354 | "couldn't initialize mac. Error number %d\n", r); | |
1355 | goto error; | |
1356 | } | |
1357 | ||
459c51ad | 1358 | r = ieee80211_register_hw(hw); |
e85d0918 DD |
1359 | if (r) { |
1360 | dev_dbg_f(&intf->dev, | |
459c51ad | 1361 | "couldn't register device. Error number %d\n", r); |
e85d0918 DD |
1362 | goto error; |
1363 | } | |
1364 | ||
1365 | dev_dbg_f(&intf->dev, "successful\n"); | |
459c51ad | 1366 | dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy)); |
e85d0918 DD |
1367 | return 0; |
1368 | error: | |
1369 | usb_reset_device(interface_to_usbdev(intf)); | |
459c51ad DD |
1370 | if (hw) { |
1371 | zd_mac_clear(zd_hw_mac(hw)); | |
1372 | ieee80211_free_hw(hw); | |
1373 | } | |
e85d0918 DD |
1374 | return r; |
1375 | } | |
1376 | ||
1377 | static void disconnect(struct usb_interface *intf) | |
1378 | { | |
459c51ad | 1379 | struct ieee80211_hw *hw = zd_intf_to_hw(intf); |
e0579d57 MP |
1380 | struct zd_mac *mac; |
1381 | struct zd_usb *usb; | |
e85d0918 | 1382 | |
a1030e92 DD |
1383 | /* Either something really bad happened, or we're just dealing with |
1384 | * a DEVICE_INSTALLER. */ | |
459c51ad | 1385 | if (hw == NULL) |
a1030e92 DD |
1386 | return; |
1387 | ||
459c51ad | 1388 | mac = zd_hw_mac(hw); |
e0579d57 MP |
1389 | usb = &mac->chip.usb; |
1390 | ||
e85d0918 DD |
1391 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
1392 | ||
459c51ad | 1393 | ieee80211_unregister_hw(hw); |
e85d0918 DD |
1394 | |
1395 | /* Just in case something has gone wrong! */ | |
78fc800f | 1396 | zd_usb_disable_tx(usb); |
e85d0918 DD |
1397 | zd_usb_disable_rx(usb); |
1398 | zd_usb_disable_int(usb); | |
1399 | ||
1400 | /* If the disconnect has been caused by a removal of the | |
1401 | * driver module, the reset allows reloading of the driver. If the | |
1402 | * reset will not be executed here, the upload of the firmware in the | |
1403 | * probe function caused by the reloading of the driver will fail. | |
1404 | */ | |
1405 | usb_reset_device(interface_to_usbdev(intf)); | |
1406 | ||
459c51ad DD |
1407 | zd_mac_clear(mac); |
1408 | ieee80211_free_hw(hw); | |
e85d0918 DD |
1409 | dev_dbg(&intf->dev, "disconnected\n"); |
1410 | } | |
1411 | ||
a0fd751f JK |
1412 | static void zd_usb_resume(struct zd_usb *usb) |
1413 | { | |
1414 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1415 | int r; | |
1416 | ||
1417 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1418 | ||
1419 | r = zd_op_start(zd_usb_to_hw(usb)); | |
1420 | if (r < 0) { | |
1421 | dev_warn(zd_usb_dev(usb), "Device resume failed " | |
1422 | "with error code %d. Retrying...\n", r); | |
1423 | if (usb->was_running) | |
1424 | set_bit(ZD_DEVICE_RUNNING, &mac->flags); | |
1425 | usb_queue_reset_device(usb->intf); | |
1426 | return; | |
1427 | } | |
1428 | ||
1429 | if (mac->type != NL80211_IFTYPE_UNSPECIFIED) { | |
1430 | r = zd_restore_settings(mac); | |
1431 | if (r < 0) { | |
1432 | dev_dbg(zd_usb_dev(usb), | |
1433 | "failed to restore settings, %d\n", r); | |
1434 | return; | |
1435 | } | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | static void zd_usb_stop(struct zd_usb *usb) | |
1440 | { | |
1441 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1442 | ||
1443 | zd_op_stop(zd_usb_to_hw(usb)); | |
1444 | ||
1445 | zd_usb_disable_tx(usb); | |
1446 | zd_usb_disable_rx(usb); | |
1447 | zd_usb_disable_int(usb); | |
1448 | ||
1449 | usb->initialized = 0; | |
1450 | } | |
1451 | ||
1452 | static int pre_reset(struct usb_interface *intf) | |
1453 | { | |
1454 | struct ieee80211_hw *hw = usb_get_intfdata(intf); | |
1455 | struct zd_mac *mac; | |
1456 | struct zd_usb *usb; | |
1457 | ||
1458 | if (!hw || intf->condition != USB_INTERFACE_BOUND) | |
1459 | return 0; | |
1460 | ||
1461 | mac = zd_hw_mac(hw); | |
1462 | usb = &mac->chip.usb; | |
1463 | ||
1464 | usb->was_running = test_bit(ZD_DEVICE_RUNNING, &mac->flags); | |
1465 | ||
1466 | zd_usb_stop(usb); | |
1467 | ||
1468 | mutex_lock(&mac->chip.mutex); | |
1469 | return 0; | |
1470 | } | |
1471 | ||
1472 | static int post_reset(struct usb_interface *intf) | |
1473 | { | |
1474 | struct ieee80211_hw *hw = usb_get_intfdata(intf); | |
1475 | struct zd_mac *mac; | |
1476 | struct zd_usb *usb; | |
1477 | ||
1478 | if (!hw || intf->condition != USB_INTERFACE_BOUND) | |
1479 | return 0; | |
1480 | ||
1481 | mac = zd_hw_mac(hw); | |
1482 | usb = &mac->chip.usb; | |
1483 | ||
1484 | mutex_unlock(&mac->chip.mutex); | |
1485 | ||
1486 | if (usb->was_running) | |
1487 | zd_usb_resume(usb); | |
1488 | return 0; | |
1489 | } | |
1490 | ||
e85d0918 | 1491 | static struct usb_driver driver = { |
459c51ad | 1492 | .name = KBUILD_MODNAME, |
e85d0918 DD |
1493 | .id_table = usb_ids, |
1494 | .probe = probe, | |
1495 | .disconnect = disconnect, | |
a0fd751f JK |
1496 | .pre_reset = pre_reset, |
1497 | .post_reset = post_reset, | |
e85d0918 DD |
1498 | }; |
1499 | ||
bc5f06a8 UK |
1500 | struct workqueue_struct *zd_workqueue; |
1501 | ||
e85d0918 DD |
1502 | static int __init usb_init(void) |
1503 | { | |
1504 | int r; | |
1505 | ||
741fec53 | 1506 | pr_debug("%s usb_init()\n", driver.name); |
e85d0918 | 1507 | |
bc5f06a8 UK |
1508 | zd_workqueue = create_singlethread_workqueue(driver.name); |
1509 | if (zd_workqueue == NULL) { | |
741fec53 | 1510 | printk(KERN_ERR "%s couldn't create workqueue\n", driver.name); |
bc5f06a8 UK |
1511 | return -ENOMEM; |
1512 | } | |
1513 | ||
e85d0918 DD |
1514 | r = usb_register(&driver); |
1515 | if (r) { | |
192b775c | 1516 | destroy_workqueue(zd_workqueue); |
741fec53 UK |
1517 | printk(KERN_ERR "%s usb_register() failed. Error number %d\n", |
1518 | driver.name, r); | |
e85d0918 DD |
1519 | return r; |
1520 | } | |
1521 | ||
741fec53 | 1522 | pr_debug("%s initialized\n", driver.name); |
e85d0918 DD |
1523 | return 0; |
1524 | } | |
1525 | ||
1526 | static void __exit usb_exit(void) | |
1527 | { | |
741fec53 | 1528 | pr_debug("%s usb_exit()\n", driver.name); |
e85d0918 | 1529 | usb_deregister(&driver); |
bc5f06a8 | 1530 | destroy_workqueue(zd_workqueue); |
e85d0918 DD |
1531 | } |
1532 | ||
1533 | module_init(usb_init); | |
1534 | module_exit(usb_exit); | |
1535 | ||
1536 | static int usb_int_regs_length(unsigned int count) | |
1537 | { | |
1538 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); | |
1539 | } | |
1540 | ||
1541 | static void prepare_read_regs_int(struct zd_usb *usb) | |
1542 | { | |
1543 | struct zd_usb_interrupt *intr = &usb->intr; | |
1544 | ||
a68077de | 1545 | spin_lock_irq(&intr->lock); |
e85d0918 DD |
1546 | intr->read_regs_enabled = 1; |
1547 | INIT_COMPLETION(intr->read_regs.completion); | |
a68077de UK |
1548 | spin_unlock_irq(&intr->lock); |
1549 | } | |
1550 | ||
1551 | static void disable_read_regs_int(struct zd_usb *usb) | |
1552 | { | |
1553 | struct zd_usb_interrupt *intr = &usb->intr; | |
1554 | ||
1555 | spin_lock_irq(&intr->lock); | |
1556 | intr->read_regs_enabled = 0; | |
1557 | spin_unlock_irq(&intr->lock); | |
e85d0918 DD |
1558 | } |
1559 | ||
1560 | static int get_results(struct zd_usb *usb, u16 *values, | |
1561 | struct usb_req_read_regs *req, unsigned int count) | |
1562 | { | |
1563 | int r; | |
1564 | int i; | |
1565 | struct zd_usb_interrupt *intr = &usb->intr; | |
1566 | struct read_regs_int *rr = &intr->read_regs; | |
1567 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; | |
1568 | ||
a68077de | 1569 | spin_lock_irq(&intr->lock); |
e85d0918 DD |
1570 | |
1571 | r = -EIO; | |
1572 | /* The created block size seems to be larger than expected. | |
1573 | * However results appear to be correct. | |
1574 | */ | |
1575 | if (rr->length < usb_int_regs_length(count)) { | |
1576 | dev_dbg_f(zd_usb_dev(usb), | |
1577 | "error: actual length %d less than expected %d\n", | |
1578 | rr->length, usb_int_regs_length(count)); | |
1579 | goto error_unlock; | |
1580 | } | |
1581 | if (rr->length > sizeof(rr->buffer)) { | |
1582 | dev_dbg_f(zd_usb_dev(usb), | |
1583 | "error: actual length %d exceeds buffer size %zu\n", | |
1584 | rr->length, sizeof(rr->buffer)); | |
1585 | goto error_unlock; | |
1586 | } | |
1587 | ||
1588 | for (i = 0; i < count; i++) { | |
1589 | struct reg_data *rd = ®s->regs[i]; | |
1590 | if (rd->addr != req->addr[i]) { | |
1591 | dev_dbg_f(zd_usb_dev(usb), | |
1592 | "rd[%d] addr %#06hx expected %#06hx\n", i, | |
1593 | le16_to_cpu(rd->addr), | |
1594 | le16_to_cpu(req->addr[i])); | |
1595 | goto error_unlock; | |
1596 | } | |
1597 | values[i] = le16_to_cpu(rd->value); | |
1598 | } | |
1599 | ||
1600 | r = 0; | |
1601 | error_unlock: | |
a68077de | 1602 | spin_unlock_irq(&intr->lock); |
e85d0918 DD |
1603 | return r; |
1604 | } | |
1605 | ||
1606 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, | |
1607 | const zd_addr_t *addresses, unsigned int count) | |
1608 | { | |
1609 | int r; | |
1610 | int i, req_len, actual_req_len; | |
1611 | struct usb_device *udev; | |
1612 | struct usb_req_read_regs *req = NULL; | |
1613 | unsigned long timeout; | |
1614 | ||
1615 | if (count < 1) { | |
1616 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); | |
1617 | return -EINVAL; | |
1618 | } | |
1619 | if (count > USB_MAX_IOREAD16_COUNT) { | |
1620 | dev_dbg_f(zd_usb_dev(usb), | |
1621 | "error: count %u exceeds possible max %u\n", | |
1622 | count, USB_MAX_IOREAD16_COUNT); | |
1623 | return -EINVAL; | |
1624 | } | |
1625 | if (in_atomic()) { | |
1626 | dev_dbg_f(zd_usb_dev(usb), | |
1627 | "error: io in atomic context not supported\n"); | |
1628 | return -EWOULDBLOCK; | |
1629 | } | |
1630 | if (!usb_int_enabled(usb)) { | |
9bca0c3b | 1631 | dev_dbg_f(zd_usb_dev(usb), |
e85d0918 DD |
1632 | "error: usb interrupt not enabled\n"); |
1633 | return -EWOULDBLOCK; | |
1634 | } | |
1635 | ||
9bca0c3b JK |
1636 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); |
1637 | BUILD_BUG_ON(sizeof(struct usb_req_read_regs) + USB_MAX_IOREAD16_COUNT * | |
1638 | sizeof(__le16) > sizeof(usb->req_buf)); | |
1639 | BUG_ON(sizeof(struct usb_req_read_regs) + count * sizeof(__le16) > | |
1640 | sizeof(usb->req_buf)); | |
1641 | ||
e85d0918 | 1642 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); |
9bca0c3b JK |
1643 | req = (void *)usb->req_buf; |
1644 | ||
e85d0918 DD |
1645 | req->id = cpu_to_le16(USB_REQ_READ_REGS); |
1646 | for (i = 0; i < count; i++) | |
0ce34bc8 | 1647 | req->addr[i] = cpu_to_le16((u16)addresses[i]); |
e85d0918 DD |
1648 | |
1649 | udev = zd_usb_to_usbdev(usb); | |
1650 | prepare_read_regs_int(usb); | |
37939810 JK |
1651 | r = usb_interrupt_msg(udev, usb_sndintpipe(udev, EP_REGS_OUT), |
1652 | req, req_len, &actual_req_len, 50 /* ms */); | |
e85d0918 DD |
1653 | if (r) { |
1654 | dev_dbg_f(zd_usb_dev(usb), | |
37939810 | 1655 | "error in usb_interrupt_msg(). Error number %d\n", r); |
e85d0918 DD |
1656 | goto error; |
1657 | } | |
1658 | if (req_len != actual_req_len) { | |
37939810 | 1659 | dev_dbg_f(zd_usb_dev(usb), "error in usb_interrupt_msg()\n" |
e85d0918 DD |
1660 | " req_len %d != actual_req_len %d\n", |
1661 | req_len, actual_req_len); | |
1662 | r = -EIO; | |
1663 | goto error; | |
1664 | } | |
1665 | ||
1666 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, | |
8f2d8f86 | 1667 | msecs_to_jiffies(50)); |
e85d0918 DD |
1668 | if (!timeout) { |
1669 | disable_read_regs_int(usb); | |
1670 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); | |
1671 | r = -ETIMEDOUT; | |
1672 | goto error; | |
1673 | } | |
1674 | ||
1675 | r = get_results(usb, values, req, count); | |
1676 | error: | |
e85d0918 DD |
1677 | return r; |
1678 | } | |
1679 | ||
eefdbec1 JK |
1680 | static void iowrite16v_urb_complete(struct urb *urb) |
1681 | { | |
1682 | struct zd_usb *usb = urb->context; | |
1683 | ||
1684 | if (urb->status && !usb->cmd_error) | |
1685 | usb->cmd_error = urb->status; | |
2fc713b2 JK |
1686 | |
1687 | if (!usb->cmd_error && | |
1688 | urb->actual_length != urb->transfer_buffer_length) | |
1689 | usb->cmd_error = -EIO; | |
eefdbec1 JK |
1690 | } |
1691 | ||
1692 | static int zd_submit_waiting_urb(struct zd_usb *usb, bool last) | |
1693 | { | |
8662b251 | 1694 | int r = 0; |
eefdbec1 JK |
1695 | struct urb *urb = usb->urb_async_waiting; |
1696 | ||
1697 | if (!urb) | |
1698 | return 0; | |
1699 | ||
1700 | usb->urb_async_waiting = NULL; | |
1701 | ||
1702 | if (!last) | |
1703 | urb->transfer_flags |= URB_NO_INTERRUPT; | |
1704 | ||
1705 | usb_anchor_urb(urb, &usb->submitted_cmds); | |
1706 | r = usb_submit_urb(urb, GFP_KERNEL); | |
1707 | if (r) { | |
1708 | usb_unanchor_urb(urb); | |
1709 | dev_dbg_f(zd_usb_dev(usb), | |
1710 | "error in usb_submit_urb(). Error number %d\n", r); | |
1711 | goto error; | |
1712 | } | |
1713 | ||
1714 | /* fall-through with r == 0 */ | |
1715 | error: | |
1716 | usb_free_urb(urb); | |
1717 | return r; | |
1718 | } | |
1719 | ||
8662b251 | 1720 | void zd_usb_iowrite16v_async_start(struct zd_usb *usb) |
eefdbec1 JK |
1721 | { |
1722 | ZD_ASSERT(usb_anchor_empty(&usb->submitted_cmds)); | |
1723 | ZD_ASSERT(usb->urb_async_waiting == NULL); | |
1724 | ZD_ASSERT(!usb->in_async); | |
1725 | ||
1726 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); | |
1727 | ||
1728 | usb->in_async = 1; | |
1729 | usb->cmd_error = 0; | |
1730 | usb->urb_async_waiting = NULL; | |
1731 | } | |
1732 | ||
8662b251 | 1733 | int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout) |
eefdbec1 JK |
1734 | { |
1735 | int r; | |
1736 | ||
1737 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); | |
1738 | ZD_ASSERT(usb->in_async); | |
1739 | ||
1740 | /* Submit last iowrite16v URB */ | |
1741 | r = zd_submit_waiting_urb(usb, true); | |
1742 | if (r) { | |
1743 | dev_dbg_f(zd_usb_dev(usb), | |
1744 | "error in zd_submit_waiting_usb(). " | |
1745 | "Error number %d\n", r); | |
1746 | ||
1747 | usb_kill_anchored_urbs(&usb->submitted_cmds); | |
1748 | goto error; | |
1749 | } | |
1750 | ||
1751 | if (timeout) | |
1752 | timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds, | |
1753 | timeout); | |
1754 | if (!timeout) { | |
1755 | usb_kill_anchored_urbs(&usb->submitted_cmds); | |
1756 | if (usb->cmd_error == -ENOENT) { | |
1757 | dev_dbg_f(zd_usb_dev(usb), "timed out"); | |
1758 | r = -ETIMEDOUT; | |
1759 | goto error; | |
1760 | } | |
1761 | } | |
1762 | ||
1763 | r = usb->cmd_error; | |
1764 | error: | |
1765 | usb->in_async = 0; | |
1766 | return r; | |
1767 | } | |
1768 | ||
8662b251 JK |
1769 | int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
1770 | unsigned int count) | |
e85d0918 DD |
1771 | { |
1772 | int r; | |
1773 | struct usb_device *udev; | |
1774 | struct usb_req_write_regs *req = NULL; | |
eefdbec1 JK |
1775 | int i, req_len; |
1776 | struct urb *urb; | |
1777 | struct usb_host_endpoint *ep; | |
1778 | ||
1779 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); | |
1780 | ZD_ASSERT(usb->in_async); | |
e85d0918 DD |
1781 | |
1782 | if (count == 0) | |
1783 | return 0; | |
1784 | if (count > USB_MAX_IOWRITE16_COUNT) { | |
1785 | dev_dbg_f(zd_usb_dev(usb), | |
1786 | "error: count %u exceeds possible max %u\n", | |
1787 | count, USB_MAX_IOWRITE16_COUNT); | |
1788 | return -EINVAL; | |
1789 | } | |
1790 | if (in_atomic()) { | |
1791 | dev_dbg_f(zd_usb_dev(usb), | |
1792 | "error: io in atomic context not supported\n"); | |
1793 | return -EWOULDBLOCK; | |
1794 | } | |
1795 | ||
eefdbec1 JK |
1796 | udev = zd_usb_to_usbdev(usb); |
1797 | ||
1798 | ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev, EP_REGS_OUT)); | |
1799 | if (!ep) | |
1800 | return -ENOENT; | |
1801 | ||
1802 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
1803 | if (!urb) | |
1804 | return -ENOMEM; | |
9bca0c3b | 1805 | |
e85d0918 DD |
1806 | req_len = sizeof(struct usb_req_write_regs) + |
1807 | count * sizeof(struct reg_data); | |
eefdbec1 JK |
1808 | req = kmalloc(req_len, GFP_KERNEL); |
1809 | if (!req) { | |
1810 | r = -ENOMEM; | |
1811 | goto error; | |
1812 | } | |
e85d0918 DD |
1813 | |
1814 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); | |
1815 | for (i = 0; i < count; i++) { | |
1816 | struct reg_data *rw = &req->reg_writes[i]; | |
0ce34bc8 | 1817 | rw->addr = cpu_to_le16((u16)ioreqs[i].addr); |
e85d0918 DD |
1818 | rw->value = cpu_to_le16(ioreqs[i].value); |
1819 | } | |
1820 | ||
eefdbec1 JK |
1821 | usb_fill_int_urb(urb, udev, usb_sndintpipe(udev, EP_REGS_OUT), |
1822 | req, req_len, iowrite16v_urb_complete, usb, | |
1823 | ep->desc.bInterval); | |
2fc713b2 | 1824 | urb->transfer_flags |= URB_FREE_BUFFER; |
eefdbec1 JK |
1825 | |
1826 | /* Submit previous URB */ | |
1827 | r = zd_submit_waiting_urb(usb, false); | |
e85d0918 DD |
1828 | if (r) { |
1829 | dev_dbg_f(zd_usb_dev(usb), | |
eefdbec1 JK |
1830 | "error in zd_submit_waiting_usb(). " |
1831 | "Error number %d\n", r); | |
e85d0918 DD |
1832 | goto error; |
1833 | } | |
1834 | ||
eefdbec1 JK |
1835 | /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs |
1836 | * of currect batch except for very last. | |
1837 | */ | |
1838 | usb->urb_async_waiting = urb; | |
1839 | return 0; | |
e85d0918 | 1840 | error: |
eefdbec1 | 1841 | usb_free_urb(urb); |
e85d0918 DD |
1842 | return r; |
1843 | } | |
1844 | ||
eefdbec1 JK |
1845 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
1846 | unsigned int count) | |
1847 | { | |
1848 | int r; | |
1849 | ||
1850 | zd_usb_iowrite16v_async_start(usb); | |
1851 | r = zd_usb_iowrite16v_async(usb, ioreqs, count); | |
1852 | if (r) { | |
1853 | zd_usb_iowrite16v_async_end(usb, 0); | |
1854 | return r; | |
1855 | } | |
1856 | return zd_usb_iowrite16v_async_end(usb, 50 /* ms */); | |
1857 | } | |
1858 | ||
e85d0918 DD |
1859 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) |
1860 | { | |
1861 | int r; | |
1862 | struct usb_device *udev; | |
1863 | struct usb_req_rfwrite *req = NULL; | |
1864 | int i, req_len, actual_req_len; | |
1865 | u16 bit_value_template; | |
1866 | ||
1867 | if (in_atomic()) { | |
1868 | dev_dbg_f(zd_usb_dev(usb), | |
1869 | "error: io in atomic context not supported\n"); | |
1870 | return -EWOULDBLOCK; | |
1871 | } | |
1872 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { | |
1873 | dev_dbg_f(zd_usb_dev(usb), | |
1874 | "error: bits %d are smaller than" | |
1875 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", | |
1876 | bits, USB_MIN_RFWRITE_BIT_COUNT); | |
1877 | return -EINVAL; | |
1878 | } | |
1879 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { | |
1880 | dev_dbg_f(zd_usb_dev(usb), | |
1881 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", | |
1882 | bits, USB_MAX_RFWRITE_BIT_COUNT); | |
1883 | return -EINVAL; | |
1884 | } | |
1885 | #ifdef DEBUG | |
1886 | if (value & (~0UL << bits)) { | |
1887 | dev_dbg_f(zd_usb_dev(usb), | |
1888 | "error: value %#09x has bits >= %d set\n", | |
1889 | value, bits); | |
1890 | return -EINVAL; | |
1891 | } | |
1892 | #endif /* DEBUG */ | |
1893 | ||
1894 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); | |
1895 | ||
fbd5d17b | 1896 | r = zd_usb_ioread16(usb, &bit_value_template, ZD_CR203); |
e85d0918 DD |
1897 | if (r) { |
1898 | dev_dbg_f(zd_usb_dev(usb), | |
fbd5d17b | 1899 | "error %d: Couldn't read ZD_CR203\n", r); |
9bca0c3b | 1900 | return r; |
e85d0918 DD |
1901 | } |
1902 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); | |
1903 | ||
9bca0c3b JK |
1904 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); |
1905 | BUILD_BUG_ON(sizeof(struct usb_req_rfwrite) + | |
1906 | USB_MAX_RFWRITE_BIT_COUNT * sizeof(__le16) > | |
1907 | sizeof(usb->req_buf)); | |
1908 | BUG_ON(sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16) > | |
1909 | sizeof(usb->req_buf)); | |
1910 | ||
e85d0918 | 1911 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); |
9bca0c3b | 1912 | req = (void *)usb->req_buf; |
e85d0918 DD |
1913 | |
1914 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); | |
1915 | /* 1: 3683a, but not used in ZYDAS driver */ | |
1916 | req->value = cpu_to_le16(2); | |
1917 | req->bits = cpu_to_le16(bits); | |
1918 | ||
1919 | for (i = 0; i < bits; i++) { | |
1920 | u16 bv = bit_value_template; | |
1921 | if (value & (1 << (bits-1-i))) | |
1922 | bv |= RF_DATA; | |
1923 | req->bit_values[i] = cpu_to_le16(bv); | |
1924 | } | |
1925 | ||
1926 | udev = zd_usb_to_usbdev(usb); | |
37939810 JK |
1927 | r = usb_interrupt_msg(udev, usb_sndintpipe(udev, EP_REGS_OUT), |
1928 | req, req_len, &actual_req_len, 50 /* ms */); | |
e85d0918 DD |
1929 | if (r) { |
1930 | dev_dbg_f(zd_usb_dev(usb), | |
37939810 | 1931 | "error in usb_interrupt_msg(). Error number %d\n", r); |
e85d0918 DD |
1932 | goto out; |
1933 | } | |
1934 | if (req_len != actual_req_len) { | |
37939810 | 1935 | dev_dbg_f(zd_usb_dev(usb), "error in usb_interrupt_msg()" |
e85d0918 DD |
1936 | " req_len %d != actual_req_len %d\n", |
1937 | req_len, actual_req_len); | |
1938 | r = -EIO; | |
1939 | goto out; | |
1940 | } | |
1941 | ||
1942 | /* FALL-THROUGH with r == 0 */ | |
1943 | out: | |
e85d0918 DD |
1944 | return r; |
1945 | } |