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: |
b312d799 | 414 | goto kfree; |
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) { | |
446 | dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb); | |
447 | goto kfree; | |
448 | } | |
449 | return; | |
450 | kfree: | |
451 | kfree(urb->transfer_buffer); | |
452 | } | |
453 | ||
454 | static inline int int_urb_interval(struct usb_device *udev) | |
455 | { | |
456 | switch (udev->speed) { | |
457 | case USB_SPEED_HIGH: | |
458 | return 4; | |
459 | case USB_SPEED_LOW: | |
460 | return 10; | |
461 | case USB_SPEED_FULL: | |
462 | default: | |
463 | return 1; | |
464 | } | |
465 | } | |
466 | ||
467 | static inline int usb_int_enabled(struct zd_usb *usb) | |
468 | { | |
469 | unsigned long flags; | |
470 | struct zd_usb_interrupt *intr = &usb->intr; | |
471 | struct urb *urb; | |
472 | ||
473 | spin_lock_irqsave(&intr->lock, flags); | |
474 | urb = intr->urb; | |
475 | spin_unlock_irqrestore(&intr->lock, flags); | |
476 | return urb != NULL; | |
477 | } | |
478 | ||
479 | int zd_usb_enable_int(struct zd_usb *usb) | |
480 | { | |
481 | int r; | |
482 | struct usb_device *udev; | |
483 | struct zd_usb_interrupt *intr = &usb->intr; | |
484 | void *transfer_buffer = NULL; | |
485 | struct urb *urb; | |
486 | ||
487 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
488 | ||
35c3404e | 489 | urb = usb_alloc_urb(0, GFP_KERNEL); |
e85d0918 DD |
490 | if (!urb) { |
491 | r = -ENOMEM; | |
492 | goto out; | |
493 | } | |
494 | ||
495 | ZD_ASSERT(!irqs_disabled()); | |
496 | spin_lock_irq(&intr->lock); | |
497 | if (intr->urb) { | |
498 | spin_unlock_irq(&intr->lock); | |
499 | r = 0; | |
500 | goto error_free_urb; | |
501 | } | |
502 | intr->urb = urb; | |
503 | spin_unlock_irq(&intr->lock); | |
504 | ||
505 | /* TODO: make it a DMA buffer */ | |
506 | r = -ENOMEM; | |
35c3404e | 507 | transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL); |
e85d0918 DD |
508 | if (!transfer_buffer) { |
509 | dev_dbg_f(zd_usb_dev(usb), | |
510 | "couldn't allocate transfer_buffer\n"); | |
511 | goto error_set_urb_null; | |
512 | } | |
513 | ||
514 | udev = zd_usb_to_usbdev(usb); | |
515 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), | |
516 | transfer_buffer, USB_MAX_EP_INT_BUFFER, | |
517 | int_urb_complete, usb, | |
518 | intr->interval); | |
519 | ||
520 | dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb); | |
35c3404e | 521 | r = usb_submit_urb(urb, GFP_KERNEL); |
e85d0918 DD |
522 | if (r) { |
523 | dev_dbg_f(zd_usb_dev(usb), | |
524 | "Couldn't submit urb. Error number %d\n", r); | |
525 | goto error; | |
526 | } | |
527 | ||
528 | return 0; | |
529 | error: | |
530 | kfree(transfer_buffer); | |
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; | |
544 | struct zd_usb_interrupt *intr = &usb->intr; | |
545 | struct urb *urb; | |
546 | ||
547 | spin_lock_irqsave(&intr->lock, flags); | |
548 | urb = intr->urb; | |
549 | if (!urb) { | |
550 | spin_unlock_irqrestore(&intr->lock, flags); | |
551 | return; | |
552 | } | |
553 | intr->urb = NULL; | |
554 | spin_unlock_irqrestore(&intr->lock, flags); | |
555 | ||
556 | usb_kill_urb(urb); | |
557 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); | |
558 | usb_free_urb(urb); | |
559 | } | |
560 | ||
561 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, | |
562 | unsigned int length) | |
563 | { | |
564 | int i; | |
e85d0918 DD |
565 | const struct rx_length_info *length_info; |
566 | ||
567 | if (length < sizeof(struct rx_length_info)) { | |
568 | /* It's not a complete packet anyhow. */ | |
7f4013f0 BP |
569 | printk("%s: invalid, small RX packet : %d\n", |
570 | __func__, length); | |
e85d0918 DD |
571 | return; |
572 | } | |
573 | length_info = (struct rx_length_info *) | |
574 | (buffer + length - sizeof(struct rx_length_info)); | |
575 | ||
576 | /* It might be that three frames are merged into a single URB | |
577 | * transaction. We have to check for the length info tag. | |
578 | * | |
579 | * While testing we discovered that length_info might be unaligned, | |
580 | * because if USB transactions are merged, the last packet will not | |
581 | * be padded. Unaligned access might also happen if the length_info | |
582 | * structure is not present. | |
583 | */ | |
533dd1b0 | 584 | if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG) |
b269825b | 585 | { |
e85d0918 DD |
586 | unsigned int l, k, n; |
587 | for (i = 0, l = 0;; i++) { | |
533dd1b0 | 588 | k = get_unaligned_le16(&length_info->length[i]); |
850c211c UK |
589 | if (k == 0) |
590 | return; | |
e85d0918 DD |
591 | n = l+k; |
592 | if (n > length) | |
593 | return; | |
459c51ad | 594 | zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k); |
e85d0918 DD |
595 | if (i >= 2) |
596 | return; | |
597 | l = (n+3) & ~3; | |
598 | } | |
599 | } else { | |
459c51ad | 600 | zd_mac_rx(zd_usb_to_hw(usb), buffer, length); |
e85d0918 DD |
601 | } |
602 | } | |
603 | ||
7d12e780 | 604 | static void rx_urb_complete(struct urb *urb) |
e85d0918 DD |
605 | { |
606 | struct zd_usb *usb; | |
607 | struct zd_usb_rx *rx; | |
608 | const u8 *buffer; | |
609 | unsigned int length; | |
610 | ||
611 | switch (urb->status) { | |
612 | case 0: | |
613 | break; | |
614 | case -ESHUTDOWN: | |
615 | case -EINVAL: | |
616 | case -ENODEV: | |
617 | case -ENOENT: | |
618 | case -ECONNRESET: | |
e85d0918 | 619 | case -EPIPE: |
b312d799 | 620 | return; |
e85d0918 DD |
621 | default: |
622 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
623 | goto resubmit; | |
624 | } | |
625 | ||
626 | buffer = urb->transfer_buffer; | |
627 | length = urb->actual_length; | |
628 | usb = urb->context; | |
629 | rx = &usb->rx; | |
630 | ||
631 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { | |
632 | /* If there is an old first fragment, we don't care. */ | |
633 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); | |
634 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); | |
635 | spin_lock(&rx->lock); | |
636 | memcpy(rx->fragment, buffer, length); | |
637 | rx->fragment_length = length; | |
638 | spin_unlock(&rx->lock); | |
639 | goto resubmit; | |
640 | } | |
641 | ||
642 | spin_lock(&rx->lock); | |
643 | if (rx->fragment_length > 0) { | |
644 | /* We are on a second fragment, we believe */ | |
645 | ZD_ASSERT(length + rx->fragment_length <= | |
646 | ARRAY_SIZE(rx->fragment)); | |
647 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); | |
648 | memcpy(rx->fragment+rx->fragment_length, buffer, length); | |
649 | handle_rx_packet(usb, rx->fragment, | |
650 | rx->fragment_length + length); | |
651 | rx->fragment_length = 0; | |
652 | spin_unlock(&rx->lock); | |
653 | } else { | |
654 | spin_unlock(&rx->lock); | |
655 | handle_rx_packet(usb, buffer, length); | |
656 | } | |
657 | ||
658 | resubmit: | |
659 | usb_submit_urb(urb, GFP_ATOMIC); | |
660 | } | |
661 | ||
459c51ad | 662 | static struct urb *alloc_rx_urb(struct zd_usb *usb) |
e85d0918 DD |
663 | { |
664 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
665 | struct urb *urb; | |
666 | void *buffer; | |
667 | ||
35c3404e | 668 | urb = usb_alloc_urb(0, GFP_KERNEL); |
e85d0918 DD |
669 | if (!urb) |
670 | return NULL; | |
997ea58e DM |
671 | buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL, |
672 | &urb->transfer_dma); | |
e85d0918 DD |
673 | if (!buffer) { |
674 | usb_free_urb(urb); | |
675 | return NULL; | |
676 | } | |
677 | ||
678 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), | |
997ea58e | 679 | buffer, USB_MAX_RX_SIZE, |
e85d0918 DD |
680 | rx_urb_complete, usb); |
681 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
682 | ||
683 | return urb; | |
684 | } | |
685 | ||
459c51ad | 686 | static void free_rx_urb(struct urb *urb) |
e85d0918 DD |
687 | { |
688 | if (!urb) | |
689 | return; | |
997ea58e DM |
690 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
691 | urb->transfer_buffer, urb->transfer_dma); | |
e85d0918 DD |
692 | usb_free_urb(urb); |
693 | } | |
694 | ||
695 | int zd_usb_enable_rx(struct zd_usb *usb) | |
696 | { | |
697 | int i, r; | |
698 | struct zd_usb_rx *rx = &usb->rx; | |
699 | struct urb **urbs; | |
700 | ||
701 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
702 | ||
703 | r = -ENOMEM; | |
459c51ad | 704 | urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL); |
e85d0918 DD |
705 | if (!urbs) |
706 | goto error; | |
459c51ad DD |
707 | for (i = 0; i < RX_URBS_COUNT; i++) { |
708 | urbs[i] = alloc_rx_urb(usb); | |
e85d0918 DD |
709 | if (!urbs[i]) |
710 | goto error; | |
711 | } | |
712 | ||
713 | ZD_ASSERT(!irqs_disabled()); | |
714 | spin_lock_irq(&rx->lock); | |
715 | if (rx->urbs) { | |
716 | spin_unlock_irq(&rx->lock); | |
717 | r = 0; | |
718 | goto error; | |
719 | } | |
720 | rx->urbs = urbs; | |
459c51ad | 721 | rx->urbs_count = RX_URBS_COUNT; |
e85d0918 DD |
722 | spin_unlock_irq(&rx->lock); |
723 | ||
459c51ad | 724 | for (i = 0; i < RX_URBS_COUNT; i++) { |
35c3404e | 725 | r = usb_submit_urb(urbs[i], GFP_KERNEL); |
e85d0918 DD |
726 | if (r) |
727 | goto error_submit; | |
728 | } | |
729 | ||
730 | return 0; | |
731 | error_submit: | |
459c51ad | 732 | for (i = 0; i < RX_URBS_COUNT; i++) { |
e85d0918 DD |
733 | usb_kill_urb(urbs[i]); |
734 | } | |
735 | spin_lock_irq(&rx->lock); | |
736 | rx->urbs = NULL; | |
737 | rx->urbs_count = 0; | |
738 | spin_unlock_irq(&rx->lock); | |
739 | error: | |
740 | if (urbs) { | |
459c51ad DD |
741 | for (i = 0; i < RX_URBS_COUNT; i++) |
742 | free_rx_urb(urbs[i]); | |
e85d0918 DD |
743 | } |
744 | return r; | |
745 | } | |
746 | ||
747 | void zd_usb_disable_rx(struct zd_usb *usb) | |
748 | { | |
749 | int i; | |
750 | unsigned long flags; | |
751 | struct urb **urbs; | |
752 | unsigned int count; | |
753 | struct zd_usb_rx *rx = &usb->rx; | |
754 | ||
755 | spin_lock_irqsave(&rx->lock, flags); | |
756 | urbs = rx->urbs; | |
757 | count = rx->urbs_count; | |
758 | spin_unlock_irqrestore(&rx->lock, flags); | |
759 | if (!urbs) | |
760 | return; | |
761 | ||
762 | for (i = 0; i < count; i++) { | |
763 | usb_kill_urb(urbs[i]); | |
459c51ad | 764 | free_rx_urb(urbs[i]); |
e85d0918 DD |
765 | } |
766 | kfree(urbs); | |
767 | ||
768 | spin_lock_irqsave(&rx->lock, flags); | |
769 | rx->urbs = NULL; | |
770 | rx->urbs_count = 0; | |
771 | spin_unlock_irqrestore(&rx->lock, flags); | |
772 | } | |
773 | ||
459c51ad DD |
774 | /** |
775 | * zd_usb_disable_tx - disable transmission | |
776 | * @usb: the zd1211rw-private USB structure | |
777 | * | |
778 | * Frees all URBs in the free list and marks the transmission as disabled. | |
779 | */ | |
780 | void zd_usb_disable_tx(struct zd_usb *usb) | |
781 | { | |
782 | struct zd_usb_tx *tx = &usb->tx; | |
783 | unsigned long flags; | |
78fc800f JK |
784 | |
785 | atomic_set(&tx->enabled, 0); | |
786 | ||
787 | /* kill all submitted tx-urbs */ | |
788 | usb_kill_anchored_urbs(&tx->submitted); | |
459c51ad DD |
789 | |
790 | spin_lock_irqsave(&tx->lock, flags); | |
78fc800f | 791 | WARN_ON(tx->submitted_urbs != 0); |
459c51ad | 792 | tx->submitted_urbs = 0; |
78fc800f JK |
793 | spin_unlock_irqrestore(&tx->lock, flags); |
794 | ||
459c51ad DD |
795 | /* The stopped state is ignored, relying on ieee80211_wake_queues() |
796 | * in a potentionally following zd_usb_enable_tx(). | |
797 | */ | |
459c51ad DD |
798 | } |
799 | ||
800 | /** | |
801 | * zd_usb_enable_tx - enables transmission | |
802 | * @usb: a &struct zd_usb pointer | |
803 | * | |
804 | * This function enables transmission and prepares the &zd_usb_tx data | |
805 | * structure. | |
806 | */ | |
807 | void zd_usb_enable_tx(struct zd_usb *usb) | |
808 | { | |
809 | unsigned long flags; | |
810 | struct zd_usb_tx *tx = &usb->tx; | |
811 | ||
812 | spin_lock_irqsave(&tx->lock, flags); | |
78fc800f | 813 | atomic_set(&tx->enabled, 1); |
459c51ad DD |
814 | tx->submitted_urbs = 0; |
815 | ieee80211_wake_queues(zd_usb_to_hw(usb)); | |
816 | tx->stopped = 0; | |
817 | spin_unlock_irqrestore(&tx->lock, flags); | |
818 | } | |
819 | ||
459c51ad DD |
820 | static void tx_dec_submitted_urbs(struct zd_usb *usb) |
821 | { | |
822 | struct zd_usb_tx *tx = &usb->tx; | |
823 | unsigned long flags; | |
824 | ||
825 | spin_lock_irqsave(&tx->lock, flags); | |
826 | --tx->submitted_urbs; | |
827 | if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) { | |
828 | ieee80211_wake_queues(zd_usb_to_hw(usb)); | |
829 | tx->stopped = 0; | |
830 | } | |
831 | spin_unlock_irqrestore(&tx->lock, flags); | |
832 | } | |
833 | ||
834 | static void tx_inc_submitted_urbs(struct zd_usb *usb) | |
835 | { | |
836 | struct zd_usb_tx *tx = &usb->tx; | |
837 | unsigned long flags; | |
838 | ||
839 | spin_lock_irqsave(&tx->lock, flags); | |
840 | ++tx->submitted_urbs; | |
841 | if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) { | |
842 | ieee80211_stop_queues(zd_usb_to_hw(usb)); | |
843 | tx->stopped = 1; | |
844 | } | |
845 | spin_unlock_irqrestore(&tx->lock, flags); | |
846 | } | |
847 | ||
848 | /** | |
849 | * tx_urb_complete - completes the execution of an URB | |
850 | * @urb: a URB | |
851 | * | |
852 | * This function is called if the URB has been transferred to a device or an | |
853 | * error has happened. | |
854 | */ | |
7d12e780 | 855 | static void tx_urb_complete(struct urb *urb) |
e85d0918 DD |
856 | { |
857 | int r; | |
459c51ad | 858 | struct sk_buff *skb; |
e039fa4a | 859 | struct ieee80211_tx_info *info; |
459c51ad | 860 | struct zd_usb *usb; |
78fc800f JK |
861 | struct zd_usb_tx *tx; |
862 | ||
863 | skb = (struct sk_buff *)urb->context; | |
864 | info = IEEE80211_SKB_CB(skb); | |
865 | /* | |
866 | * grab 'usb' pointer before handing off the skb (since | |
867 | * it might be freed by zd_mac_tx_to_dev or mac80211) | |
868 | */ | |
869 | usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb; | |
870 | tx = &usb->tx; | |
e85d0918 DD |
871 | |
872 | switch (urb->status) { | |
873 | case 0: | |
874 | break; | |
875 | case -ESHUTDOWN: | |
876 | case -EINVAL: | |
877 | case -ENODEV: | |
878 | case -ENOENT: | |
879 | case -ECONNRESET: | |
b312d799 | 880 | case -EPIPE: |
e85d0918 DD |
881 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
882 | break; | |
e85d0918 DD |
883 | default: |
884 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
885 | goto resubmit; | |
886 | } | |
887 | free_urb: | |
dbabad0c | 888 | zd_mac_tx_to_dev(skb, urb->status); |
78fc800f | 889 | usb_free_urb(urb); |
459c51ad | 890 | tx_dec_submitted_urbs(usb); |
e85d0918 DD |
891 | return; |
892 | resubmit: | |
78fc800f | 893 | usb_anchor_urb(urb, &tx->submitted); |
e85d0918 DD |
894 | r = usb_submit_urb(urb, GFP_ATOMIC); |
895 | if (r) { | |
78fc800f | 896 | usb_unanchor_urb(urb); |
e85d0918 DD |
897 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); |
898 | goto free_urb; | |
899 | } | |
900 | } | |
901 | ||
459c51ad DD |
902 | /** |
903 | * zd_usb_tx: initiates transfer of a frame of the device | |
904 | * | |
905 | * @usb: the zd1211rw-private USB structure | |
906 | * @skb: a &struct sk_buff pointer | |
907 | * | |
908 | * This function tranmits a frame to the device. It doesn't wait for | |
909 | * completion. The frame must contain the control set and have all the | |
910 | * control set information available. | |
911 | * | |
912 | * The function returns 0 if the transfer has been successfully initiated. | |
e85d0918 | 913 | */ |
459c51ad | 914 | int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb) |
e85d0918 DD |
915 | { |
916 | int r; | |
917 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
918 | struct urb *urb; | |
78fc800f | 919 | struct zd_usb_tx *tx = &usb->tx; |
e85d0918 | 920 | |
78fc800f JK |
921 | if (!atomic_read(&tx->enabled)) { |
922 | r = -ENOENT; | |
923 | goto out; | |
924 | } | |
925 | ||
926 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
e85d0918 DD |
927 | if (!urb) { |
928 | r = -ENOMEM; | |
929 | goto out; | |
930 | } | |
931 | ||
e85d0918 | 932 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
459c51ad | 933 | skb->data, skb->len, tx_urb_complete, skb); |
e85d0918 | 934 | |
78fc800f | 935 | usb_anchor_urb(urb, &tx->submitted); |
e85d0918 | 936 | r = usb_submit_urb(urb, GFP_ATOMIC); |
78fc800f JK |
937 | if (r) { |
938 | usb_unanchor_urb(urb); | |
e85d0918 | 939 | goto error; |
78fc800f | 940 | } |
459c51ad | 941 | tx_inc_submitted_urbs(usb); |
e85d0918 DD |
942 | return 0; |
943 | error: | |
78fc800f | 944 | usb_free_urb(urb); |
e85d0918 DD |
945 | out: |
946 | return r; | |
947 | } | |
948 | ||
949 | static inline void init_usb_interrupt(struct zd_usb *usb) | |
950 | { | |
951 | struct zd_usb_interrupt *intr = &usb->intr; | |
952 | ||
953 | spin_lock_init(&intr->lock); | |
954 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); | |
955 | init_completion(&intr->read_regs.completion); | |
0ce34bc8 | 956 | intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT); |
e85d0918 DD |
957 | } |
958 | ||
959 | static inline void init_usb_rx(struct zd_usb *usb) | |
960 | { | |
961 | struct zd_usb_rx *rx = &usb->rx; | |
962 | spin_lock_init(&rx->lock); | |
963 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { | |
964 | rx->usb_packet_size = 512; | |
965 | } else { | |
966 | rx->usb_packet_size = 64; | |
967 | } | |
968 | ZD_ASSERT(rx->fragment_length == 0); | |
969 | } | |
970 | ||
971 | static inline void init_usb_tx(struct zd_usb *usb) | |
972 | { | |
459c51ad DD |
973 | struct zd_usb_tx *tx = &usb->tx; |
974 | spin_lock_init(&tx->lock); | |
78fc800f | 975 | atomic_set(&tx->enabled, 0); |
459c51ad | 976 | tx->stopped = 0; |
78fc800f | 977 | init_usb_anchor(&tx->submitted); |
459c51ad | 978 | tx->submitted_urbs = 0; |
e85d0918 DD |
979 | } |
980 | ||
459c51ad | 981 | void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw, |
e85d0918 DD |
982 | struct usb_interface *intf) |
983 | { | |
984 | memset(usb, 0, sizeof(*usb)); | |
985 | usb->intf = usb_get_intf(intf); | |
459c51ad | 986 | usb_set_intfdata(usb->intf, hw); |
e85d0918 DD |
987 | init_usb_interrupt(usb); |
988 | init_usb_tx(usb); | |
989 | init_usb_rx(usb); | |
990 | } | |
991 | ||
e85d0918 DD |
992 | void zd_usb_clear(struct zd_usb *usb) |
993 | { | |
994 | usb_set_intfdata(usb->intf, NULL); | |
995 | usb_put_intf(usb->intf); | |
c48cf125 | 996 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
e85d0918 DD |
997 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
998 | } | |
999 | ||
1000 | static const char *speed(enum usb_device_speed speed) | |
1001 | { | |
1002 | switch (speed) { | |
1003 | case USB_SPEED_LOW: | |
1004 | return "low"; | |
1005 | case USB_SPEED_FULL: | |
1006 | return "full"; | |
1007 | case USB_SPEED_HIGH: | |
1008 | return "high"; | |
1009 | default: | |
1010 | return "unknown speed"; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) | |
1015 | { | |
1016 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", | |
1017 | le16_to_cpu(udev->descriptor.idVendor), | |
1018 | le16_to_cpu(udev->descriptor.idProduct), | |
1019 | get_bcdDevice(udev), | |
1020 | speed(udev->speed)); | |
1021 | } | |
1022 | ||
1023 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) | |
1024 | { | |
1025 | struct usb_device *udev = interface_to_usbdev(usb->intf); | |
1026 | return scnprint_id(udev, buffer, size); | |
1027 | } | |
1028 | ||
1029 | #ifdef DEBUG | |
1030 | static void print_id(struct usb_device *udev) | |
1031 | { | |
1032 | char buffer[40]; | |
1033 | ||
1034 | scnprint_id(udev, buffer, sizeof(buffer)); | |
1035 | buffer[sizeof(buffer)-1] = 0; | |
1036 | dev_dbg_f(&udev->dev, "%s\n", buffer); | |
1037 | } | |
1038 | #else | |
1039 | #define print_id(udev) do { } while (0) | |
1040 | #endif | |
1041 | ||
a1030e92 DD |
1042 | static int eject_installer(struct usb_interface *intf) |
1043 | { | |
1044 | struct usb_device *udev = interface_to_usbdev(intf); | |
1045 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; | |
1046 | struct usb_endpoint_descriptor *endpoint; | |
1047 | unsigned char *cmd; | |
1048 | u8 bulk_out_ep; | |
1049 | int r; | |
1050 | ||
1051 | /* Find bulk out endpoint */ | |
11466f13 SS |
1052 | for (r = 1; r >= 0; r--) { |
1053 | endpoint = &iface_desc->endpoint[r].desc; | |
1054 | if (usb_endpoint_dir_out(endpoint) && | |
1055 | usb_endpoint_xfer_bulk(endpoint)) { | |
1056 | bulk_out_ep = endpoint->bEndpointAddress; | |
1057 | break; | |
1058 | } | |
1059 | } | |
1060 | if (r == -1) { | |
a1030e92 DD |
1061 | dev_err(&udev->dev, |
1062 | "zd1211rw: Could not find bulk out endpoint\n"); | |
1063 | return -ENODEV; | |
1064 | } | |
1065 | ||
1066 | cmd = kzalloc(31, GFP_KERNEL); | |
1067 | if (cmd == NULL) | |
1068 | return -ENODEV; | |
1069 | ||
1070 | /* USB bulk command block */ | |
1071 | cmd[0] = 0x55; /* bulk command signature */ | |
1072 | cmd[1] = 0x53; /* bulk command signature */ | |
1073 | cmd[2] = 0x42; /* bulk command signature */ | |
1074 | cmd[3] = 0x43; /* bulk command signature */ | |
1075 | cmd[14] = 6; /* command length */ | |
1076 | ||
1077 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ | |
1078 | cmd[19] = 0x2; /* eject disc */ | |
1079 | ||
1080 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); | |
1081 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), | |
1082 | cmd, 31, NULL, 2000); | |
1083 | kfree(cmd); | |
1084 | if (r) | |
1085 | return r; | |
1086 | ||
1087 | /* At this point, the device disconnects and reconnects with the real | |
1088 | * ID numbers. */ | |
1089 | ||
1090 | usb_set_intfdata(intf, NULL); | |
1091 | return 0; | |
1092 | } | |
1093 | ||
74553aed DD |
1094 | int zd_usb_init_hw(struct zd_usb *usb) |
1095 | { | |
1096 | int r; | |
1097 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1098 | ||
1099 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1100 | ||
1101 | r = upload_firmware(usb); | |
1102 | if (r) { | |
1103 | dev_err(zd_usb_dev(usb), | |
1104 | "couldn't load firmware. Error number %d\n", r); | |
1105 | return r; | |
1106 | } | |
1107 | ||
1108 | r = usb_reset_configuration(zd_usb_to_usbdev(usb)); | |
1109 | if (r) { | |
1110 | dev_dbg_f(zd_usb_dev(usb), | |
1111 | "couldn't reset configuration. Error number %d\n", r); | |
1112 | return r; | |
1113 | } | |
1114 | ||
459c51ad | 1115 | r = zd_mac_init_hw(mac->hw); |
74553aed DD |
1116 | if (r) { |
1117 | dev_dbg_f(zd_usb_dev(usb), | |
1118 | "couldn't initialize mac. Error number %d\n", r); | |
1119 | return r; | |
1120 | } | |
1121 | ||
1122 | usb->initialized = 1; | |
1123 | return 0; | |
1124 | } | |
1125 | ||
e85d0918 DD |
1126 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
1127 | { | |
1128 | int r; | |
1129 | struct usb_device *udev = interface_to_usbdev(intf); | |
459c51ad DD |
1130 | struct zd_usb *usb; |
1131 | struct ieee80211_hw *hw = NULL; | |
e85d0918 DD |
1132 | |
1133 | print_id(udev); | |
1134 | ||
a1030e92 DD |
1135 | if (id->driver_info & DEVICE_INSTALLER) |
1136 | return eject_installer(intf); | |
1137 | ||
e85d0918 DD |
1138 | switch (udev->speed) { |
1139 | case USB_SPEED_LOW: | |
1140 | case USB_SPEED_FULL: | |
1141 | case USB_SPEED_HIGH: | |
1142 | break; | |
1143 | default: | |
1144 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); | |
1145 | r = -ENODEV; | |
1146 | goto error; | |
1147 | } | |
1148 | ||
459c51ad DD |
1149 | r = usb_reset_device(udev); |
1150 | if (r) { | |
1151 | dev_err(&intf->dev, | |
1152 | "couldn't reset usb device. Error number %d\n", r); | |
1153 | goto error; | |
1154 | } | |
6e3632f6 | 1155 | |
459c51ad DD |
1156 | hw = zd_mac_alloc_hw(intf); |
1157 | if (hw == NULL) { | |
e85d0918 DD |
1158 | r = -ENOMEM; |
1159 | goto error; | |
1160 | } | |
1161 | ||
459c51ad | 1162 | usb = &zd_hw_mac(hw)->chip.usb; |
74553aed | 1163 | usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0; |
e85d0918 | 1164 | |
459c51ad | 1165 | r = zd_mac_preinit_hw(hw); |
e85d0918 DD |
1166 | if (r) { |
1167 | dev_dbg_f(&intf->dev, | |
1168 | "couldn't initialize mac. Error number %d\n", r); | |
1169 | goto error; | |
1170 | } | |
1171 | ||
459c51ad | 1172 | r = ieee80211_register_hw(hw); |
e85d0918 DD |
1173 | if (r) { |
1174 | dev_dbg_f(&intf->dev, | |
459c51ad | 1175 | "couldn't register device. Error number %d\n", r); |
e85d0918 DD |
1176 | goto error; |
1177 | } | |
1178 | ||
1179 | dev_dbg_f(&intf->dev, "successful\n"); | |
459c51ad | 1180 | dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy)); |
e85d0918 DD |
1181 | return 0; |
1182 | error: | |
1183 | usb_reset_device(interface_to_usbdev(intf)); | |
459c51ad DD |
1184 | if (hw) { |
1185 | zd_mac_clear(zd_hw_mac(hw)); | |
1186 | ieee80211_free_hw(hw); | |
1187 | } | |
e85d0918 DD |
1188 | return r; |
1189 | } | |
1190 | ||
1191 | static void disconnect(struct usb_interface *intf) | |
1192 | { | |
459c51ad | 1193 | struct ieee80211_hw *hw = zd_intf_to_hw(intf); |
e0579d57 MP |
1194 | struct zd_mac *mac; |
1195 | struct zd_usb *usb; | |
e85d0918 | 1196 | |
a1030e92 DD |
1197 | /* Either something really bad happened, or we're just dealing with |
1198 | * a DEVICE_INSTALLER. */ | |
459c51ad | 1199 | if (hw == NULL) |
a1030e92 DD |
1200 | return; |
1201 | ||
459c51ad | 1202 | mac = zd_hw_mac(hw); |
e0579d57 MP |
1203 | usb = &mac->chip.usb; |
1204 | ||
e85d0918 DD |
1205 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
1206 | ||
459c51ad | 1207 | ieee80211_unregister_hw(hw); |
e85d0918 DD |
1208 | |
1209 | /* Just in case something has gone wrong! */ | |
78fc800f | 1210 | zd_usb_disable_tx(usb); |
e85d0918 DD |
1211 | zd_usb_disable_rx(usb); |
1212 | zd_usb_disable_int(usb); | |
1213 | ||
1214 | /* If the disconnect has been caused by a removal of the | |
1215 | * driver module, the reset allows reloading of the driver. If the | |
1216 | * reset will not be executed here, the upload of the firmware in the | |
1217 | * probe function caused by the reloading of the driver will fail. | |
1218 | */ | |
1219 | usb_reset_device(interface_to_usbdev(intf)); | |
1220 | ||
459c51ad DD |
1221 | zd_mac_clear(mac); |
1222 | ieee80211_free_hw(hw); | |
e85d0918 DD |
1223 | dev_dbg(&intf->dev, "disconnected\n"); |
1224 | } | |
1225 | ||
1226 | static struct usb_driver driver = { | |
459c51ad | 1227 | .name = KBUILD_MODNAME, |
e85d0918 DD |
1228 | .id_table = usb_ids, |
1229 | .probe = probe, | |
1230 | .disconnect = disconnect, | |
1231 | }; | |
1232 | ||
bc5f06a8 UK |
1233 | struct workqueue_struct *zd_workqueue; |
1234 | ||
e85d0918 DD |
1235 | static int __init usb_init(void) |
1236 | { | |
1237 | int r; | |
1238 | ||
741fec53 | 1239 | pr_debug("%s usb_init()\n", driver.name); |
e85d0918 | 1240 | |
bc5f06a8 UK |
1241 | zd_workqueue = create_singlethread_workqueue(driver.name); |
1242 | if (zd_workqueue == NULL) { | |
741fec53 | 1243 | printk(KERN_ERR "%s couldn't create workqueue\n", driver.name); |
bc5f06a8 UK |
1244 | return -ENOMEM; |
1245 | } | |
1246 | ||
e85d0918 DD |
1247 | r = usb_register(&driver); |
1248 | if (r) { | |
192b775c | 1249 | destroy_workqueue(zd_workqueue); |
741fec53 UK |
1250 | printk(KERN_ERR "%s usb_register() failed. Error number %d\n", |
1251 | driver.name, r); | |
e85d0918 DD |
1252 | return r; |
1253 | } | |
1254 | ||
741fec53 | 1255 | pr_debug("%s initialized\n", driver.name); |
e85d0918 DD |
1256 | return 0; |
1257 | } | |
1258 | ||
1259 | static void __exit usb_exit(void) | |
1260 | { | |
741fec53 | 1261 | pr_debug("%s usb_exit()\n", driver.name); |
e85d0918 | 1262 | usb_deregister(&driver); |
bc5f06a8 | 1263 | destroy_workqueue(zd_workqueue); |
e85d0918 DD |
1264 | } |
1265 | ||
1266 | module_init(usb_init); | |
1267 | module_exit(usb_exit); | |
1268 | ||
1269 | static int usb_int_regs_length(unsigned int count) | |
1270 | { | |
1271 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); | |
1272 | } | |
1273 | ||
1274 | static void prepare_read_regs_int(struct zd_usb *usb) | |
1275 | { | |
1276 | struct zd_usb_interrupt *intr = &usb->intr; | |
1277 | ||
a68077de | 1278 | spin_lock_irq(&intr->lock); |
e85d0918 DD |
1279 | intr->read_regs_enabled = 1; |
1280 | INIT_COMPLETION(intr->read_regs.completion); | |
a68077de UK |
1281 | spin_unlock_irq(&intr->lock); |
1282 | } | |
1283 | ||
1284 | static void disable_read_regs_int(struct zd_usb *usb) | |
1285 | { | |
1286 | struct zd_usb_interrupt *intr = &usb->intr; | |
1287 | ||
1288 | spin_lock_irq(&intr->lock); | |
1289 | intr->read_regs_enabled = 0; | |
1290 | spin_unlock_irq(&intr->lock); | |
e85d0918 DD |
1291 | } |
1292 | ||
1293 | static int get_results(struct zd_usb *usb, u16 *values, | |
1294 | struct usb_req_read_regs *req, unsigned int count) | |
1295 | { | |
1296 | int r; | |
1297 | int i; | |
1298 | struct zd_usb_interrupt *intr = &usb->intr; | |
1299 | struct read_regs_int *rr = &intr->read_regs; | |
1300 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; | |
1301 | ||
a68077de | 1302 | spin_lock_irq(&intr->lock); |
e85d0918 DD |
1303 | |
1304 | r = -EIO; | |
1305 | /* The created block size seems to be larger than expected. | |
1306 | * However results appear to be correct. | |
1307 | */ | |
1308 | if (rr->length < usb_int_regs_length(count)) { | |
1309 | dev_dbg_f(zd_usb_dev(usb), | |
1310 | "error: actual length %d less than expected %d\n", | |
1311 | rr->length, usb_int_regs_length(count)); | |
1312 | goto error_unlock; | |
1313 | } | |
1314 | if (rr->length > sizeof(rr->buffer)) { | |
1315 | dev_dbg_f(zd_usb_dev(usb), | |
1316 | "error: actual length %d exceeds buffer size %zu\n", | |
1317 | rr->length, sizeof(rr->buffer)); | |
1318 | goto error_unlock; | |
1319 | } | |
1320 | ||
1321 | for (i = 0; i < count; i++) { | |
1322 | struct reg_data *rd = ®s->regs[i]; | |
1323 | if (rd->addr != req->addr[i]) { | |
1324 | dev_dbg_f(zd_usb_dev(usb), | |
1325 | "rd[%d] addr %#06hx expected %#06hx\n", i, | |
1326 | le16_to_cpu(rd->addr), | |
1327 | le16_to_cpu(req->addr[i])); | |
1328 | goto error_unlock; | |
1329 | } | |
1330 | values[i] = le16_to_cpu(rd->value); | |
1331 | } | |
1332 | ||
1333 | r = 0; | |
1334 | error_unlock: | |
a68077de | 1335 | spin_unlock_irq(&intr->lock); |
e85d0918 DD |
1336 | return r; |
1337 | } | |
1338 | ||
1339 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, | |
1340 | const zd_addr_t *addresses, unsigned int count) | |
1341 | { | |
1342 | int r; | |
1343 | int i, req_len, actual_req_len; | |
1344 | struct usb_device *udev; | |
1345 | struct usb_req_read_regs *req = NULL; | |
1346 | unsigned long timeout; | |
1347 | ||
1348 | if (count < 1) { | |
1349 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); | |
1350 | return -EINVAL; | |
1351 | } | |
1352 | if (count > USB_MAX_IOREAD16_COUNT) { | |
1353 | dev_dbg_f(zd_usb_dev(usb), | |
1354 | "error: count %u exceeds possible max %u\n", | |
1355 | count, USB_MAX_IOREAD16_COUNT); | |
1356 | return -EINVAL; | |
1357 | } | |
1358 | if (in_atomic()) { | |
1359 | dev_dbg_f(zd_usb_dev(usb), | |
1360 | "error: io in atomic context not supported\n"); | |
1361 | return -EWOULDBLOCK; | |
1362 | } | |
1363 | if (!usb_int_enabled(usb)) { | |
1364 | dev_dbg_f(zd_usb_dev(usb), | |
1365 | "error: usb interrupt not enabled\n"); | |
1366 | return -EWOULDBLOCK; | |
1367 | } | |
1368 | ||
1369 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); | |
35c3404e | 1370 | req = kmalloc(req_len, GFP_KERNEL); |
e85d0918 DD |
1371 | if (!req) |
1372 | return -ENOMEM; | |
1373 | req->id = cpu_to_le16(USB_REQ_READ_REGS); | |
1374 | for (i = 0; i < count; i++) | |
0ce34bc8 | 1375 | req->addr[i] = cpu_to_le16((u16)addresses[i]); |
e85d0918 DD |
1376 | |
1377 | udev = zd_usb_to_usbdev(usb); | |
1378 | prepare_read_regs_int(usb); | |
1379 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1380 | req, req_len, &actual_req_len, 1000 /* ms */); | |
1381 | if (r) { | |
1382 | dev_dbg_f(zd_usb_dev(usb), | |
1383 | "error in usb_bulk_msg(). Error number %d\n", r); | |
1384 | goto error; | |
1385 | } | |
1386 | if (req_len != actual_req_len) { | |
1387 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n" | |
1388 | " req_len %d != actual_req_len %d\n", | |
1389 | req_len, actual_req_len); | |
1390 | r = -EIO; | |
1391 | goto error; | |
1392 | } | |
1393 | ||
1394 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, | |
1395 | msecs_to_jiffies(1000)); | |
1396 | if (!timeout) { | |
1397 | disable_read_regs_int(usb); | |
1398 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); | |
1399 | r = -ETIMEDOUT; | |
1400 | goto error; | |
1401 | } | |
1402 | ||
1403 | r = get_results(usb, values, req, count); | |
1404 | error: | |
1405 | kfree(req); | |
1406 | return r; | |
1407 | } | |
1408 | ||
1409 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, | |
1410 | unsigned int count) | |
1411 | { | |
1412 | int r; | |
1413 | struct usb_device *udev; | |
1414 | struct usb_req_write_regs *req = NULL; | |
1415 | int i, req_len, actual_req_len; | |
1416 | ||
1417 | if (count == 0) | |
1418 | return 0; | |
1419 | if (count > USB_MAX_IOWRITE16_COUNT) { | |
1420 | dev_dbg_f(zd_usb_dev(usb), | |
1421 | "error: count %u exceeds possible max %u\n", | |
1422 | count, USB_MAX_IOWRITE16_COUNT); | |
1423 | return -EINVAL; | |
1424 | } | |
1425 | if (in_atomic()) { | |
1426 | dev_dbg_f(zd_usb_dev(usb), | |
1427 | "error: io in atomic context not supported\n"); | |
1428 | return -EWOULDBLOCK; | |
1429 | } | |
1430 | ||
1431 | req_len = sizeof(struct usb_req_write_regs) + | |
1432 | count * sizeof(struct reg_data); | |
35c3404e | 1433 | req = kmalloc(req_len, GFP_KERNEL); |
e85d0918 DD |
1434 | if (!req) |
1435 | return -ENOMEM; | |
1436 | ||
1437 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); | |
1438 | for (i = 0; i < count; i++) { | |
1439 | struct reg_data *rw = &req->reg_writes[i]; | |
0ce34bc8 | 1440 | rw->addr = cpu_to_le16((u16)ioreqs[i].addr); |
e85d0918 DD |
1441 | rw->value = cpu_to_le16(ioreqs[i].value); |
1442 | } | |
1443 | ||
1444 | udev = zd_usb_to_usbdev(usb); | |
1445 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1446 | req, req_len, &actual_req_len, 1000 /* ms */); | |
1447 | if (r) { | |
1448 | dev_dbg_f(zd_usb_dev(usb), | |
1449 | "error in usb_bulk_msg(). Error number %d\n", r); | |
1450 | goto error; | |
1451 | } | |
1452 | if (req_len != actual_req_len) { | |
1453 | dev_dbg_f(zd_usb_dev(usb), | |
1454 | "error in usb_bulk_msg()" | |
1455 | " req_len %d != actual_req_len %d\n", | |
1456 | req_len, actual_req_len); | |
1457 | r = -EIO; | |
1458 | goto error; | |
1459 | } | |
1460 | ||
1461 | /* FALL-THROUGH with r == 0 */ | |
1462 | error: | |
1463 | kfree(req); | |
1464 | return r; | |
1465 | } | |
1466 | ||
1467 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) | |
1468 | { | |
1469 | int r; | |
1470 | struct usb_device *udev; | |
1471 | struct usb_req_rfwrite *req = NULL; | |
1472 | int i, req_len, actual_req_len; | |
1473 | u16 bit_value_template; | |
1474 | ||
1475 | if (in_atomic()) { | |
1476 | dev_dbg_f(zd_usb_dev(usb), | |
1477 | "error: io in atomic context not supported\n"); | |
1478 | return -EWOULDBLOCK; | |
1479 | } | |
1480 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { | |
1481 | dev_dbg_f(zd_usb_dev(usb), | |
1482 | "error: bits %d are smaller than" | |
1483 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", | |
1484 | bits, USB_MIN_RFWRITE_BIT_COUNT); | |
1485 | return -EINVAL; | |
1486 | } | |
1487 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { | |
1488 | dev_dbg_f(zd_usb_dev(usb), | |
1489 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", | |
1490 | bits, USB_MAX_RFWRITE_BIT_COUNT); | |
1491 | return -EINVAL; | |
1492 | } | |
1493 | #ifdef DEBUG | |
1494 | if (value & (~0UL << bits)) { | |
1495 | dev_dbg_f(zd_usb_dev(usb), | |
1496 | "error: value %#09x has bits >= %d set\n", | |
1497 | value, bits); | |
1498 | return -EINVAL; | |
1499 | } | |
1500 | #endif /* DEBUG */ | |
1501 | ||
1502 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); | |
1503 | ||
1504 | r = zd_usb_ioread16(usb, &bit_value_template, CR203); | |
1505 | if (r) { | |
1506 | dev_dbg_f(zd_usb_dev(usb), | |
1507 | "error %d: Couldn't read CR203\n", r); | |
1508 | goto out; | |
1509 | } | |
1510 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); | |
1511 | ||
1512 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); | |
35c3404e | 1513 | req = kmalloc(req_len, GFP_KERNEL); |
e85d0918 DD |
1514 | if (!req) |
1515 | return -ENOMEM; | |
1516 | ||
1517 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); | |
1518 | /* 1: 3683a, but not used in ZYDAS driver */ | |
1519 | req->value = cpu_to_le16(2); | |
1520 | req->bits = cpu_to_le16(bits); | |
1521 | ||
1522 | for (i = 0; i < bits; i++) { | |
1523 | u16 bv = bit_value_template; | |
1524 | if (value & (1 << (bits-1-i))) | |
1525 | bv |= RF_DATA; | |
1526 | req->bit_values[i] = cpu_to_le16(bv); | |
1527 | } | |
1528 | ||
1529 | udev = zd_usb_to_usbdev(usb); | |
1530 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1531 | req, req_len, &actual_req_len, 1000 /* ms */); | |
1532 | if (r) { | |
1533 | dev_dbg_f(zd_usb_dev(usb), | |
1534 | "error in usb_bulk_msg(). Error number %d\n", r); | |
1535 | goto out; | |
1536 | } | |
1537 | if (req_len != actual_req_len) { | |
1538 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()" | |
1539 | " req_len %d != actual_req_len %d\n", | |
1540 | req_len, actual_req_len); | |
1541 | r = -EIO; | |
1542 | goto out; | |
1543 | } | |
1544 | ||
1545 | /* FALL-THROUGH with r == 0 */ | |
1546 | out: | |
1547 | kfree(req); | |
1548 | return r; | |
1549 | } |