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