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