Commit | Line | Data |
---|---|---|
e85d0918 DD |
1 | /* zd_usb.c |
2 | * | |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2 of the License, or | |
6 | * (at your option) any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
17 | ||
18 | #include <asm/unaligned.h> | |
d066c219 | 19 | #include <linux/kernel.h> |
e85d0918 DD |
20 | #include <linux/init.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/firmware.h> | |
23 | #include <linux/device.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/skbuff.h> | |
26 | #include <linux/usb.h> | |
27 | #include <net/ieee80211.h> | |
28 | ||
29 | #include "zd_def.h" | |
30 | #include "zd_netdev.h" | |
31 | #include "zd_mac.h" | |
32 | #include "zd_usb.h" | |
33 | #include "zd_util.h" | |
34 | ||
35 | static struct usb_device_id usb_ids[] = { | |
36 | /* ZD1211 */ | |
37 | { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 }, | |
38 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 }, | |
39 | { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 }, | |
40 | { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 }, | |
41 | { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 }, | |
42 | { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 }, | |
dd2f5538 | 43 | { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 }, |
1b865491 | 44 | { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 }, |
12f39308 | 45 | { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 }, |
4ceb7e99 | 46 | { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 }, |
fca2714f | 47 | { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 }, |
fc3e39be | 48 | { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 }, |
e85d0918 DD |
49 | /* ZD1211B */ |
50 | { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B }, | |
51 | { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B }, | |
dd2f5538 | 52 | { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B }, |
943599ee | 53 | { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B }, |
a1030e92 DD |
54 | /* "Driverless" devices that need ejecting */ |
55 | { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER }, | |
e85d0918 DD |
56 | {} |
57 | }; | |
58 | ||
59 | MODULE_LICENSE("GPL"); | |
60 | MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip."); | |
61 | MODULE_AUTHOR("Ulrich Kunitz"); | |
62 | MODULE_AUTHOR("Daniel Drake"); | |
63 | MODULE_VERSION("1.0"); | |
64 | MODULE_DEVICE_TABLE(usb, usb_ids); | |
65 | ||
66 | #define FW_ZD1211_PREFIX "zd1211/zd1211_" | |
67 | #define FW_ZD1211B_PREFIX "zd1211/zd1211b_" | |
68 | ||
69 | /* register address handling */ | |
70 | ||
71 | #ifdef DEBUG | |
72 | static int check_addr(struct zd_usb *usb, zd_addr_t addr) | |
73 | { | |
74 | u32 base = ZD_ADDR_BASE(addr); | |
75 | u32 offset = ZD_OFFSET(addr); | |
76 | ||
77 | if ((u32)addr & ADDR_ZERO_MASK) | |
78 | goto invalid_address; | |
79 | switch (base) { | |
80 | case USB_BASE: | |
81 | break; | |
82 | case CR_BASE: | |
83 | if (offset > CR_MAX_OFFSET) { | |
84 | dev_dbg(zd_usb_dev(usb), | |
85 | "CR offset %#010x larger than" | |
86 | " CR_MAX_OFFSET %#10x\n", | |
87 | offset, CR_MAX_OFFSET); | |
88 | goto invalid_address; | |
89 | } | |
90 | if (offset & 1) { | |
91 | dev_dbg(zd_usb_dev(usb), | |
92 | "CR offset %#010x is not a multiple of 2\n", | |
93 | offset); | |
94 | goto invalid_address; | |
95 | } | |
96 | break; | |
97 | case E2P_BASE: | |
98 | if (offset > E2P_MAX_OFFSET) { | |
99 | dev_dbg(zd_usb_dev(usb), | |
100 | "E2P offset %#010x larger than" | |
101 | " E2P_MAX_OFFSET %#010x\n", | |
102 | offset, E2P_MAX_OFFSET); | |
103 | goto invalid_address; | |
104 | } | |
105 | break; | |
106 | case FW_BASE: | |
107 | if (!usb->fw_base_offset) { | |
108 | dev_dbg(zd_usb_dev(usb), | |
109 | "ERROR: fw base offset has not been set\n"); | |
110 | return -EAGAIN; | |
111 | } | |
112 | if (offset > FW_MAX_OFFSET) { | |
113 | dev_dbg(zd_usb_dev(usb), | |
114 | "FW offset %#10x is larger than" | |
115 | " FW_MAX_OFFSET %#010x\n", | |
116 | offset, FW_MAX_OFFSET); | |
117 | goto invalid_address; | |
118 | } | |
119 | break; | |
120 | default: | |
121 | dev_dbg(zd_usb_dev(usb), | |
122 | "address has unsupported base %#010x\n", addr); | |
123 | goto invalid_address; | |
124 | } | |
125 | ||
126 | return 0; | |
127 | invalid_address: | |
128 | dev_dbg(zd_usb_dev(usb), | |
129 | "ERROR: invalid address: %#010x\n", addr); | |
130 | return -EINVAL; | |
131 | } | |
132 | #endif /* DEBUG */ | |
133 | ||
134 | static u16 usb_addr(struct zd_usb *usb, zd_addr_t addr) | |
135 | { | |
136 | u32 base; | |
137 | u16 offset; | |
138 | ||
139 | base = ZD_ADDR_BASE(addr); | |
140 | offset = ZD_OFFSET(addr); | |
141 | ||
142 | ZD_ASSERT(check_addr(usb, addr) == 0); | |
143 | ||
144 | switch (base) { | |
145 | case CR_BASE: | |
146 | offset += CR_BASE_OFFSET; | |
147 | break; | |
148 | case E2P_BASE: | |
149 | offset += E2P_BASE_OFFSET; | |
150 | break; | |
151 | case FW_BASE: | |
152 | offset += usb->fw_base_offset; | |
153 | break; | |
154 | } | |
155 | ||
156 | return offset; | |
157 | } | |
158 | ||
159 | /* USB device initialization */ | |
160 | ||
161 | static int request_fw_file( | |
162 | const struct firmware **fw, const char *name, struct device *device) | |
163 | { | |
164 | int r; | |
165 | ||
166 | dev_dbg_f(device, "fw name %s\n", name); | |
167 | ||
168 | r = request_firmware(fw, name, device); | |
169 | if (r) | |
170 | dev_err(device, | |
171 | "Could not load firmware file %s. Error number %d\n", | |
172 | name, r); | |
173 | return r; | |
174 | } | |
175 | ||
176 | static inline u16 get_bcdDevice(const struct usb_device *udev) | |
177 | { | |
178 | return le16_to_cpu(udev->descriptor.bcdDevice); | |
179 | } | |
180 | ||
181 | enum upload_code_flags { | |
182 | REBOOT = 1, | |
183 | }; | |
184 | ||
185 | /* Ensures that MAX_TRANSFER_SIZE is even. */ | |
186 | #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1) | |
187 | ||
188 | static int upload_code(struct usb_device *udev, | |
189 | const u8 *data, size_t size, u16 code_offset, int flags) | |
190 | { | |
191 | u8 *p; | |
192 | int r; | |
193 | ||
194 | /* USB request blocks need "kmalloced" buffers. | |
195 | */ | |
196 | p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL); | |
197 | if (!p) { | |
198 | dev_err(&udev->dev, "out of memory\n"); | |
199 | r = -ENOMEM; | |
200 | goto error; | |
201 | } | |
202 | ||
203 | size &= ~1; | |
204 | while (size > 0) { | |
205 | size_t transfer_size = size <= MAX_TRANSFER_SIZE ? | |
206 | size : MAX_TRANSFER_SIZE; | |
207 | ||
208 | dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size); | |
209 | ||
210 | memcpy(p, data, transfer_size); | |
211 | r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
212 | USB_REQ_FIRMWARE_DOWNLOAD, | |
213 | USB_DIR_OUT | USB_TYPE_VENDOR, | |
214 | code_offset, 0, p, transfer_size, 1000 /* ms */); | |
215 | if (r < 0) { | |
216 | dev_err(&udev->dev, | |
217 | "USB control request for firmware upload" | |
218 | " failed. Error number %d\n", r); | |
219 | goto error; | |
220 | } | |
221 | transfer_size = r & ~1; | |
222 | ||
223 | size -= transfer_size; | |
224 | data += transfer_size; | |
225 | code_offset += transfer_size/sizeof(u16); | |
226 | } | |
227 | ||
228 | if (flags & REBOOT) { | |
229 | u8 ret; | |
230 | ||
231 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
232 | USB_REQ_FIRMWARE_CONFIRM, | |
233 | USB_DIR_IN | USB_TYPE_VENDOR, | |
234 | 0, 0, &ret, sizeof(ret), 5000 /* ms */); | |
235 | if (r != sizeof(ret)) { | |
236 | dev_err(&udev->dev, | |
237 | "control request firmeware confirmation failed." | |
238 | " Return value %d\n", r); | |
239 | if (r >= 0) | |
240 | r = -ENODEV; | |
241 | goto error; | |
242 | } | |
243 | if (ret & 0x80) { | |
244 | dev_err(&udev->dev, | |
245 | "Internal error while downloading." | |
246 | " Firmware confirm return value %#04x\n", | |
247 | (unsigned int)ret); | |
248 | r = -ENODEV; | |
249 | goto error; | |
250 | } | |
251 | dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n", | |
252 | (unsigned int)ret); | |
253 | } | |
254 | ||
255 | r = 0; | |
256 | error: | |
257 | kfree(p); | |
258 | return r; | |
259 | } | |
260 | ||
261 | static u16 get_word(const void *data, u16 offset) | |
262 | { | |
263 | const __le16 *p = data; | |
264 | return le16_to_cpu(p[offset]); | |
265 | } | |
266 | ||
267 | static char *get_fw_name(char *buffer, size_t size, u8 device_type, | |
268 | const char* postfix) | |
269 | { | |
270 | scnprintf(buffer, size, "%s%s", | |
271 | device_type == DEVICE_ZD1211B ? | |
272 | FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX, | |
273 | postfix); | |
274 | return buffer; | |
275 | } | |
276 | ||
d066c219 DD |
277 | static int handle_version_mismatch(struct usb_device *udev, u8 device_type, |
278 | const struct firmware *ub_fw) | |
279 | { | |
280 | const struct firmware *ur_fw = NULL; | |
281 | int offset; | |
282 | int r = 0; | |
283 | char fw_name[128]; | |
284 | ||
285 | r = request_fw_file(&ur_fw, | |
286 | get_fw_name(fw_name, sizeof(fw_name), device_type, "ur"), | |
287 | &udev->dev); | |
288 | if (r) | |
289 | goto error; | |
290 | ||
291 | r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START_OFFSET, | |
292 | REBOOT); | |
293 | if (r) | |
294 | goto error; | |
295 | ||
296 | offset = ((EEPROM_REGS_OFFSET + EEPROM_REGS_SIZE) * sizeof(u16)); | |
297 | r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset, | |
298 | E2P_BASE_OFFSET + EEPROM_REGS_SIZE, REBOOT); | |
299 | ||
300 | /* At this point, the vendor driver downloads the whole firmware | |
301 | * image, hacks around with version IDs, and uploads it again, | |
302 | * completely overwriting the boot code. We do not do this here as | |
303 | * it is not required on any tested devices, and it is suspected to | |
304 | * cause problems. */ | |
305 | error: | |
306 | release_firmware(ur_fw); | |
307 | return r; | |
308 | } | |
309 | ||
e85d0918 DD |
310 | static int upload_firmware(struct usb_device *udev, u8 device_type) |
311 | { | |
312 | int r; | |
313 | u16 fw_bcdDevice; | |
314 | u16 bcdDevice; | |
315 | const struct firmware *ub_fw = NULL; | |
316 | const struct firmware *uph_fw = NULL; | |
317 | char fw_name[128]; | |
318 | ||
319 | bcdDevice = get_bcdDevice(udev); | |
320 | ||
321 | r = request_fw_file(&ub_fw, | |
322 | get_fw_name(fw_name, sizeof(fw_name), device_type, "ub"), | |
323 | &udev->dev); | |
324 | if (r) | |
325 | goto error; | |
326 | ||
327 | fw_bcdDevice = get_word(ub_fw->data, EEPROM_REGS_OFFSET); | |
328 | ||
e85d0918 DD |
329 | if (fw_bcdDevice != bcdDevice) { |
330 | dev_info(&udev->dev, | |
d066c219 DD |
331 | "firmware version %#06x and device bootcode version " |
332 | "%#06x differ\n", fw_bcdDevice, bcdDevice); | |
333 | if (bcdDevice <= 0x4313) | |
334 | dev_warn(&udev->dev, "device has old bootcode, please " | |
335 | "report success or failure\n"); | |
336 | ||
337 | r = handle_version_mismatch(udev, device_type, ub_fw); | |
338 | if (r) | |
339 | goto error; | |
e85d0918 DD |
340 | } else { |
341 | dev_dbg_f(&udev->dev, | |
342 | "firmware device id %#06x is equal to the " | |
343 | "actual device id\n", fw_bcdDevice); | |
344 | } | |
345 | ||
346 | ||
347 | r = request_fw_file(&uph_fw, | |
348 | get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"), | |
349 | &udev->dev); | |
350 | if (r) | |
351 | goto error; | |
352 | ||
353 | r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START_OFFSET, | |
354 | REBOOT); | |
355 | if (r) { | |
356 | dev_err(&udev->dev, | |
357 | "Could not upload firmware code uph. Error number %d\n", | |
358 | r); | |
359 | } | |
360 | ||
361 | /* FALL-THROUGH */ | |
362 | error: | |
363 | release_firmware(ub_fw); | |
364 | release_firmware(uph_fw); | |
365 | return r; | |
366 | } | |
367 | ||
368 | static void disable_read_regs_int(struct zd_usb *usb) | |
369 | { | |
370 | struct zd_usb_interrupt *intr = &usb->intr; | |
371 | ||
e85d0918 DD |
372 | spin_lock(&intr->lock); |
373 | intr->read_regs_enabled = 0; | |
374 | spin_unlock(&intr->lock); | |
375 | } | |
376 | ||
377 | #define urb_dev(urb) (&(urb)->dev->dev) | |
378 | ||
379 | static inline void handle_regs_int(struct urb *urb) | |
380 | { | |
381 | struct zd_usb *usb = urb->context; | |
382 | struct zd_usb_interrupt *intr = &usb->intr; | |
383 | int len; | |
384 | ||
385 | ZD_ASSERT(in_interrupt()); | |
386 | spin_lock(&intr->lock); | |
387 | ||
388 | if (intr->read_regs_enabled) { | |
389 | intr->read_regs.length = len = urb->actual_length; | |
390 | ||
391 | if (len > sizeof(intr->read_regs.buffer)) | |
392 | len = sizeof(intr->read_regs.buffer); | |
393 | memcpy(intr->read_regs.buffer, urb->transfer_buffer, len); | |
394 | intr->read_regs_enabled = 0; | |
395 | complete(&intr->read_regs.completion); | |
396 | goto out; | |
397 | } | |
398 | ||
399 | dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n"); | |
400 | out: | |
401 | spin_unlock(&intr->lock); | |
402 | } | |
403 | ||
404 | static inline void handle_retry_failed_int(struct urb *urb) | |
405 | { | |
406 | dev_dbg_f(urb_dev(urb), "retry failed interrupt\n"); | |
407 | } | |
408 | ||
409 | ||
410 | static void int_urb_complete(struct urb *urb, struct pt_regs *pt_regs) | |
411 | { | |
412 | int r; | |
413 | struct usb_int_header *hdr; | |
414 | ||
415 | switch (urb->status) { | |
416 | case 0: | |
417 | break; | |
418 | case -ESHUTDOWN: | |
419 | case -EINVAL: | |
420 | case -ENODEV: | |
421 | case -ENOENT: | |
422 | case -ECONNRESET: | |
e85d0918 | 423 | case -EPIPE: |
b312d799 | 424 | goto kfree; |
e85d0918 DD |
425 | default: |
426 | goto resubmit; | |
427 | } | |
428 | ||
429 | if (urb->actual_length < sizeof(hdr)) { | |
430 | dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb); | |
431 | goto resubmit; | |
432 | } | |
433 | ||
434 | hdr = urb->transfer_buffer; | |
435 | if (hdr->type != USB_INT_TYPE) { | |
436 | dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb); | |
437 | goto resubmit; | |
438 | } | |
439 | ||
440 | switch (hdr->id) { | |
441 | case USB_INT_ID_REGS: | |
442 | handle_regs_int(urb); | |
443 | break; | |
444 | case USB_INT_ID_RETRY_FAILED: | |
445 | handle_retry_failed_int(urb); | |
446 | break; | |
447 | default: | |
448 | dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb, | |
449 | (unsigned int)hdr->id); | |
450 | goto resubmit; | |
451 | } | |
452 | ||
453 | resubmit: | |
454 | r = usb_submit_urb(urb, GFP_ATOMIC); | |
455 | if (r) { | |
456 | dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb); | |
457 | goto kfree; | |
458 | } | |
459 | return; | |
460 | kfree: | |
461 | kfree(urb->transfer_buffer); | |
462 | } | |
463 | ||
464 | static inline int int_urb_interval(struct usb_device *udev) | |
465 | { | |
466 | switch (udev->speed) { | |
467 | case USB_SPEED_HIGH: | |
468 | return 4; | |
469 | case USB_SPEED_LOW: | |
470 | return 10; | |
471 | case USB_SPEED_FULL: | |
472 | default: | |
473 | return 1; | |
474 | } | |
475 | } | |
476 | ||
477 | static inline int usb_int_enabled(struct zd_usb *usb) | |
478 | { | |
479 | unsigned long flags; | |
480 | struct zd_usb_interrupt *intr = &usb->intr; | |
481 | struct urb *urb; | |
482 | ||
483 | spin_lock_irqsave(&intr->lock, flags); | |
484 | urb = intr->urb; | |
485 | spin_unlock_irqrestore(&intr->lock, flags); | |
486 | return urb != NULL; | |
487 | } | |
488 | ||
489 | int zd_usb_enable_int(struct zd_usb *usb) | |
490 | { | |
491 | int r; | |
492 | struct usb_device *udev; | |
493 | struct zd_usb_interrupt *intr = &usb->intr; | |
494 | void *transfer_buffer = NULL; | |
495 | struct urb *urb; | |
496 | ||
497 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
498 | ||
499 | urb = usb_alloc_urb(0, GFP_NOFS); | |
500 | if (!urb) { | |
501 | r = -ENOMEM; | |
502 | goto out; | |
503 | } | |
504 | ||
505 | ZD_ASSERT(!irqs_disabled()); | |
506 | spin_lock_irq(&intr->lock); | |
507 | if (intr->urb) { | |
508 | spin_unlock_irq(&intr->lock); | |
509 | r = 0; | |
510 | goto error_free_urb; | |
511 | } | |
512 | intr->urb = urb; | |
513 | spin_unlock_irq(&intr->lock); | |
514 | ||
515 | /* TODO: make it a DMA buffer */ | |
516 | r = -ENOMEM; | |
517 | transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS); | |
518 | if (!transfer_buffer) { | |
519 | dev_dbg_f(zd_usb_dev(usb), | |
520 | "couldn't allocate transfer_buffer\n"); | |
521 | goto error_set_urb_null; | |
522 | } | |
523 | ||
524 | udev = zd_usb_to_usbdev(usb); | |
525 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), | |
526 | transfer_buffer, USB_MAX_EP_INT_BUFFER, | |
527 | int_urb_complete, usb, | |
528 | intr->interval); | |
529 | ||
530 | dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb); | |
531 | r = usb_submit_urb(urb, GFP_NOFS); | |
532 | if (r) { | |
533 | dev_dbg_f(zd_usb_dev(usb), | |
534 | "Couldn't submit urb. Error number %d\n", r); | |
535 | goto error; | |
536 | } | |
537 | ||
538 | return 0; | |
539 | error: | |
540 | kfree(transfer_buffer); | |
541 | error_set_urb_null: | |
542 | spin_lock_irq(&intr->lock); | |
543 | intr->urb = NULL; | |
544 | spin_unlock_irq(&intr->lock); | |
545 | error_free_urb: | |
546 | usb_free_urb(urb); | |
547 | out: | |
548 | return r; | |
549 | } | |
550 | ||
551 | void zd_usb_disable_int(struct zd_usb *usb) | |
552 | { | |
553 | unsigned long flags; | |
554 | struct zd_usb_interrupt *intr = &usb->intr; | |
555 | struct urb *urb; | |
556 | ||
557 | spin_lock_irqsave(&intr->lock, flags); | |
558 | urb = intr->urb; | |
559 | if (!urb) { | |
560 | spin_unlock_irqrestore(&intr->lock, flags); | |
561 | return; | |
562 | } | |
563 | intr->urb = NULL; | |
564 | spin_unlock_irqrestore(&intr->lock, flags); | |
565 | ||
566 | usb_kill_urb(urb); | |
567 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); | |
568 | usb_free_urb(urb); | |
569 | } | |
570 | ||
571 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, | |
572 | unsigned int length) | |
573 | { | |
574 | int i; | |
575 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
576 | const struct rx_length_info *length_info; | |
577 | ||
578 | if (length < sizeof(struct rx_length_info)) { | |
579 | /* It's not a complete packet anyhow. */ | |
580 | return; | |
581 | } | |
582 | length_info = (struct rx_length_info *) | |
583 | (buffer + length - sizeof(struct rx_length_info)); | |
584 | ||
585 | /* It might be that three frames are merged into a single URB | |
586 | * transaction. We have to check for the length info tag. | |
587 | * | |
588 | * While testing we discovered that length_info might be unaligned, | |
589 | * because if USB transactions are merged, the last packet will not | |
590 | * be padded. Unaligned access might also happen if the length_info | |
591 | * structure is not present. | |
592 | */ | |
b269825b UK |
593 | if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG)) |
594 | { | |
e85d0918 DD |
595 | unsigned int l, k, n; |
596 | for (i = 0, l = 0;; i++) { | |
b269825b | 597 | k = le16_to_cpu(get_unaligned(&length_info->length[i])); |
e85d0918 DD |
598 | n = l+k; |
599 | if (n > length) | |
600 | return; | |
601 | zd_mac_rx(mac, buffer+l, k); | |
602 | if (i >= 2) | |
603 | return; | |
604 | l = (n+3) & ~3; | |
605 | } | |
606 | } else { | |
607 | zd_mac_rx(mac, buffer, length); | |
608 | } | |
609 | } | |
610 | ||
611 | static void rx_urb_complete(struct urb *urb, struct pt_regs *pt_regs) | |
612 | { | |
613 | struct zd_usb *usb; | |
614 | struct zd_usb_rx *rx; | |
615 | const u8 *buffer; | |
616 | unsigned int length; | |
617 | ||
618 | switch (urb->status) { | |
619 | case 0: | |
620 | break; | |
621 | case -ESHUTDOWN: | |
622 | case -EINVAL: | |
623 | case -ENODEV: | |
624 | case -ENOENT: | |
625 | case -ECONNRESET: | |
e85d0918 | 626 | case -EPIPE: |
b312d799 | 627 | return; |
e85d0918 DD |
628 | default: |
629 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
630 | goto resubmit; | |
631 | } | |
632 | ||
633 | buffer = urb->transfer_buffer; | |
634 | length = urb->actual_length; | |
635 | usb = urb->context; | |
636 | rx = &usb->rx; | |
637 | ||
638 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { | |
639 | /* If there is an old first fragment, we don't care. */ | |
640 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); | |
641 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); | |
642 | spin_lock(&rx->lock); | |
643 | memcpy(rx->fragment, buffer, length); | |
644 | rx->fragment_length = length; | |
645 | spin_unlock(&rx->lock); | |
646 | goto resubmit; | |
647 | } | |
648 | ||
649 | spin_lock(&rx->lock); | |
650 | if (rx->fragment_length > 0) { | |
651 | /* We are on a second fragment, we believe */ | |
652 | ZD_ASSERT(length + rx->fragment_length <= | |
653 | ARRAY_SIZE(rx->fragment)); | |
654 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); | |
655 | memcpy(rx->fragment+rx->fragment_length, buffer, length); | |
656 | handle_rx_packet(usb, rx->fragment, | |
657 | rx->fragment_length + length); | |
658 | rx->fragment_length = 0; | |
659 | spin_unlock(&rx->lock); | |
660 | } else { | |
661 | spin_unlock(&rx->lock); | |
662 | handle_rx_packet(usb, buffer, length); | |
663 | } | |
664 | ||
665 | resubmit: | |
666 | usb_submit_urb(urb, GFP_ATOMIC); | |
667 | } | |
668 | ||
c48cf125 | 669 | static struct urb *alloc_urb(struct zd_usb *usb) |
e85d0918 DD |
670 | { |
671 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
672 | struct urb *urb; | |
673 | void *buffer; | |
674 | ||
675 | urb = usb_alloc_urb(0, GFP_NOFS); | |
676 | if (!urb) | |
677 | return NULL; | |
678 | buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS, | |
679 | &urb->transfer_dma); | |
680 | if (!buffer) { | |
681 | usb_free_urb(urb); | |
682 | return NULL; | |
683 | } | |
684 | ||
685 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), | |
686 | buffer, USB_MAX_RX_SIZE, | |
687 | rx_urb_complete, usb); | |
688 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
689 | ||
690 | return urb; | |
691 | } | |
692 | ||
c48cf125 | 693 | static void free_urb(struct urb *urb) |
e85d0918 DD |
694 | { |
695 | if (!urb) | |
696 | return; | |
697 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, | |
698 | urb->transfer_buffer, urb->transfer_dma); | |
699 | usb_free_urb(urb); | |
700 | } | |
701 | ||
702 | int zd_usb_enable_rx(struct zd_usb *usb) | |
703 | { | |
704 | int i, r; | |
705 | struct zd_usb_rx *rx = &usb->rx; | |
706 | struct urb **urbs; | |
707 | ||
708 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
709 | ||
710 | r = -ENOMEM; | |
711 | urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS); | |
712 | if (!urbs) | |
713 | goto error; | |
714 | for (i = 0; i < URBS_COUNT; i++) { | |
715 | urbs[i] = alloc_urb(usb); | |
716 | if (!urbs[i]) | |
717 | goto error; | |
718 | } | |
719 | ||
720 | ZD_ASSERT(!irqs_disabled()); | |
721 | spin_lock_irq(&rx->lock); | |
722 | if (rx->urbs) { | |
723 | spin_unlock_irq(&rx->lock); | |
724 | r = 0; | |
725 | goto error; | |
726 | } | |
727 | rx->urbs = urbs; | |
728 | rx->urbs_count = URBS_COUNT; | |
729 | spin_unlock_irq(&rx->lock); | |
730 | ||
731 | for (i = 0; i < URBS_COUNT; i++) { | |
732 | r = usb_submit_urb(urbs[i], GFP_NOFS); | |
733 | if (r) | |
734 | goto error_submit; | |
735 | } | |
736 | ||
737 | return 0; | |
738 | error_submit: | |
739 | for (i = 0; i < URBS_COUNT; i++) { | |
740 | usb_kill_urb(urbs[i]); | |
741 | } | |
742 | spin_lock_irq(&rx->lock); | |
743 | rx->urbs = NULL; | |
744 | rx->urbs_count = 0; | |
745 | spin_unlock_irq(&rx->lock); | |
746 | error: | |
747 | if (urbs) { | |
748 | for (i = 0; i < URBS_COUNT; i++) | |
749 | free_urb(urbs[i]); | |
750 | } | |
751 | return r; | |
752 | } | |
753 | ||
754 | void zd_usb_disable_rx(struct zd_usb *usb) | |
755 | { | |
756 | int i; | |
757 | unsigned long flags; | |
758 | struct urb **urbs; | |
759 | unsigned int count; | |
760 | struct zd_usb_rx *rx = &usb->rx; | |
761 | ||
762 | spin_lock_irqsave(&rx->lock, flags); | |
763 | urbs = rx->urbs; | |
764 | count = rx->urbs_count; | |
765 | spin_unlock_irqrestore(&rx->lock, flags); | |
766 | if (!urbs) | |
767 | return; | |
768 | ||
769 | for (i = 0; i < count; i++) { | |
770 | usb_kill_urb(urbs[i]); | |
771 | free_urb(urbs[i]); | |
772 | } | |
773 | kfree(urbs); | |
774 | ||
775 | spin_lock_irqsave(&rx->lock, flags); | |
776 | rx->urbs = NULL; | |
777 | rx->urbs_count = 0; | |
778 | spin_unlock_irqrestore(&rx->lock, flags); | |
779 | } | |
780 | ||
781 | static void tx_urb_complete(struct urb *urb, struct pt_regs *pt_regs) | |
782 | { | |
783 | int r; | |
784 | ||
785 | switch (urb->status) { | |
786 | case 0: | |
787 | break; | |
788 | case -ESHUTDOWN: | |
789 | case -EINVAL: | |
790 | case -ENODEV: | |
791 | case -ENOENT: | |
792 | case -ECONNRESET: | |
b312d799 | 793 | case -EPIPE: |
e85d0918 DD |
794 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
795 | break; | |
e85d0918 DD |
796 | default: |
797 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
798 | goto resubmit; | |
799 | } | |
800 | free_urb: | |
801 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, | |
802 | urb->transfer_buffer, urb->transfer_dma); | |
803 | usb_free_urb(urb); | |
804 | return; | |
805 | resubmit: | |
806 | r = usb_submit_urb(urb, GFP_ATOMIC); | |
807 | if (r) { | |
808 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); | |
809 | goto free_urb; | |
810 | } | |
811 | } | |
812 | ||
813 | /* Puts the frame on the USB endpoint. It doesn't wait for | |
814 | * completion. The frame must contain the control set. | |
815 | */ | |
816 | int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length) | |
817 | { | |
818 | int r; | |
819 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
820 | struct urb *urb; | |
821 | void *buffer; | |
822 | ||
823 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
824 | if (!urb) { | |
825 | r = -ENOMEM; | |
826 | goto out; | |
827 | } | |
828 | ||
829 | buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC, | |
830 | &urb->transfer_dma); | |
831 | if (!buffer) { | |
832 | r = -ENOMEM; | |
833 | goto error_free_urb; | |
834 | } | |
835 | memcpy(buffer, frame, length); | |
836 | ||
837 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), | |
838 | buffer, length, tx_urb_complete, NULL); | |
839 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
840 | ||
841 | r = usb_submit_urb(urb, GFP_ATOMIC); | |
842 | if (r) | |
843 | goto error; | |
844 | return 0; | |
845 | error: | |
846 | usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer, | |
847 | urb->transfer_dma); | |
848 | error_free_urb: | |
849 | usb_free_urb(urb); | |
850 | out: | |
851 | return r; | |
852 | } | |
853 | ||
854 | static inline void init_usb_interrupt(struct zd_usb *usb) | |
855 | { | |
856 | struct zd_usb_interrupt *intr = &usb->intr; | |
857 | ||
858 | spin_lock_init(&intr->lock); | |
859 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); | |
860 | init_completion(&intr->read_regs.completion); | |
861 | intr->read_regs.cr_int_addr = cpu_to_le16(usb_addr(usb, CR_INTERRUPT)); | |
862 | } | |
863 | ||
864 | static inline void init_usb_rx(struct zd_usb *usb) | |
865 | { | |
866 | struct zd_usb_rx *rx = &usb->rx; | |
867 | spin_lock_init(&rx->lock); | |
868 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { | |
869 | rx->usb_packet_size = 512; | |
870 | } else { | |
871 | rx->usb_packet_size = 64; | |
872 | } | |
873 | ZD_ASSERT(rx->fragment_length == 0); | |
874 | } | |
875 | ||
876 | static inline void init_usb_tx(struct zd_usb *usb) | |
877 | { | |
878 | /* FIXME: at this point we will allocate a fixed number of urb's for | |
879 | * use in a cyclic scheme */ | |
880 | } | |
881 | ||
882 | void zd_usb_init(struct zd_usb *usb, struct net_device *netdev, | |
883 | struct usb_interface *intf) | |
884 | { | |
885 | memset(usb, 0, sizeof(*usb)); | |
886 | usb->intf = usb_get_intf(intf); | |
887 | usb_set_intfdata(usb->intf, netdev); | |
888 | init_usb_interrupt(usb); | |
889 | init_usb_tx(usb); | |
890 | init_usb_rx(usb); | |
891 | } | |
892 | ||
893 | int zd_usb_init_hw(struct zd_usb *usb) | |
894 | { | |
895 | int r; | |
896 | struct zd_chip *chip = zd_usb_to_chip(usb); | |
897 | ||
898 | ZD_ASSERT(mutex_is_locked(&chip->mutex)); | |
899 | r = zd_ioread16_locked(chip, &usb->fw_base_offset, | |
900 | USB_REG((u16)FW_BASE_ADDR_OFFSET)); | |
901 | if (r) | |
902 | return r; | |
903 | dev_dbg_f(zd_usb_dev(usb), "fw_base_offset: %#06hx\n", | |
904 | usb->fw_base_offset); | |
905 | ||
906 | return 0; | |
907 | } | |
908 | ||
909 | void zd_usb_clear(struct zd_usb *usb) | |
910 | { | |
911 | usb_set_intfdata(usb->intf, NULL); | |
912 | usb_put_intf(usb->intf); | |
c48cf125 | 913 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
e85d0918 DD |
914 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
915 | } | |
916 | ||
917 | static const char *speed(enum usb_device_speed speed) | |
918 | { | |
919 | switch (speed) { | |
920 | case USB_SPEED_LOW: | |
921 | return "low"; | |
922 | case USB_SPEED_FULL: | |
923 | return "full"; | |
924 | case USB_SPEED_HIGH: | |
925 | return "high"; | |
926 | default: | |
927 | return "unknown speed"; | |
928 | } | |
929 | } | |
930 | ||
931 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) | |
932 | { | |
933 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", | |
934 | le16_to_cpu(udev->descriptor.idVendor), | |
935 | le16_to_cpu(udev->descriptor.idProduct), | |
936 | get_bcdDevice(udev), | |
937 | speed(udev->speed)); | |
938 | } | |
939 | ||
940 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) | |
941 | { | |
942 | struct usb_device *udev = interface_to_usbdev(usb->intf); | |
943 | return scnprint_id(udev, buffer, size); | |
944 | } | |
945 | ||
946 | #ifdef DEBUG | |
947 | static void print_id(struct usb_device *udev) | |
948 | { | |
949 | char buffer[40]; | |
950 | ||
951 | scnprint_id(udev, buffer, sizeof(buffer)); | |
952 | buffer[sizeof(buffer)-1] = 0; | |
953 | dev_dbg_f(&udev->dev, "%s\n", buffer); | |
954 | } | |
955 | #else | |
956 | #define print_id(udev) do { } while (0) | |
957 | #endif | |
958 | ||
a1030e92 DD |
959 | static int eject_installer(struct usb_interface *intf) |
960 | { | |
961 | struct usb_device *udev = interface_to_usbdev(intf); | |
962 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; | |
963 | struct usb_endpoint_descriptor *endpoint; | |
964 | unsigned char *cmd; | |
965 | u8 bulk_out_ep; | |
966 | int r; | |
967 | ||
968 | /* Find bulk out endpoint */ | |
969 | endpoint = &iface_desc->endpoint[1].desc; | |
970 | if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT && | |
971 | (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == | |
972 | USB_ENDPOINT_XFER_BULK) { | |
973 | bulk_out_ep = endpoint->bEndpointAddress; | |
974 | } else { | |
975 | dev_err(&udev->dev, | |
976 | "zd1211rw: Could not find bulk out endpoint\n"); | |
977 | return -ENODEV; | |
978 | } | |
979 | ||
980 | cmd = kzalloc(31, GFP_KERNEL); | |
981 | if (cmd == NULL) | |
982 | return -ENODEV; | |
983 | ||
984 | /* USB bulk command block */ | |
985 | cmd[0] = 0x55; /* bulk command signature */ | |
986 | cmd[1] = 0x53; /* bulk command signature */ | |
987 | cmd[2] = 0x42; /* bulk command signature */ | |
988 | cmd[3] = 0x43; /* bulk command signature */ | |
989 | cmd[14] = 6; /* command length */ | |
990 | ||
991 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ | |
992 | cmd[19] = 0x2; /* eject disc */ | |
993 | ||
994 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); | |
995 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), | |
996 | cmd, 31, NULL, 2000); | |
997 | kfree(cmd); | |
998 | if (r) | |
999 | return r; | |
1000 | ||
1001 | /* At this point, the device disconnects and reconnects with the real | |
1002 | * ID numbers. */ | |
1003 | ||
1004 | usb_set_intfdata(intf, NULL); | |
1005 | return 0; | |
1006 | } | |
1007 | ||
e85d0918 DD |
1008 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
1009 | { | |
1010 | int r; | |
1011 | struct usb_device *udev = interface_to_usbdev(intf); | |
1012 | struct net_device *netdev = NULL; | |
1013 | ||
1014 | print_id(udev); | |
1015 | ||
a1030e92 DD |
1016 | if (id->driver_info & DEVICE_INSTALLER) |
1017 | return eject_installer(intf); | |
1018 | ||
e85d0918 DD |
1019 | switch (udev->speed) { |
1020 | case USB_SPEED_LOW: | |
1021 | case USB_SPEED_FULL: | |
1022 | case USB_SPEED_HIGH: | |
1023 | break; | |
1024 | default: | |
1025 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); | |
1026 | r = -ENODEV; | |
1027 | goto error; | |
1028 | } | |
1029 | ||
1030 | netdev = zd_netdev_alloc(intf); | |
1031 | if (netdev == NULL) { | |
1032 | r = -ENOMEM; | |
1033 | goto error; | |
1034 | } | |
1035 | ||
1036 | r = upload_firmware(udev, id->driver_info); | |
1037 | if (r) { | |
1038 | dev_err(&intf->dev, | |
1039 | "couldn't load firmware. Error number %d\n", r); | |
1040 | goto error; | |
1041 | } | |
1042 | ||
1043 | r = usb_reset_configuration(udev); | |
1044 | if (r) { | |
1045 | dev_dbg_f(&intf->dev, | |
1046 | "couldn't reset configuration. Error number %d\n", r); | |
1047 | goto error; | |
1048 | } | |
1049 | ||
1050 | /* At this point the interrupt endpoint is not generally enabled. We | |
1051 | * save the USB bandwidth until the network device is opened. But | |
1052 | * notify that the initialization of the MAC will require the | |
1053 | * interrupts to be temporary enabled. | |
1054 | */ | |
1055 | r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info); | |
1056 | if (r) { | |
1057 | dev_dbg_f(&intf->dev, | |
1058 | "couldn't initialize mac. Error number %d\n", r); | |
1059 | goto error; | |
1060 | } | |
1061 | ||
1062 | r = register_netdev(netdev); | |
1063 | if (r) { | |
1064 | dev_dbg_f(&intf->dev, | |
1065 | "couldn't register netdev. Error number %d\n", r); | |
1066 | goto error; | |
1067 | } | |
1068 | ||
1069 | dev_dbg_f(&intf->dev, "successful\n"); | |
1070 | dev_info(&intf->dev,"%s\n", netdev->name); | |
1071 | return 0; | |
1072 | error: | |
1073 | usb_reset_device(interface_to_usbdev(intf)); | |
1074 | zd_netdev_free(netdev); | |
1075 | return r; | |
1076 | } | |
1077 | ||
1078 | static void disconnect(struct usb_interface *intf) | |
1079 | { | |
1080 | struct net_device *netdev = zd_intf_to_netdev(intf); | |
1081 | struct zd_mac *mac = zd_netdev_mac(netdev); | |
1082 | struct zd_usb *usb = &mac->chip.usb; | |
1083 | ||
a1030e92 DD |
1084 | /* Either something really bad happened, or we're just dealing with |
1085 | * a DEVICE_INSTALLER. */ | |
1086 | if (netdev == NULL) | |
1087 | return; | |
1088 | ||
e85d0918 DD |
1089 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
1090 | ||
1091 | zd_netdev_disconnect(netdev); | |
1092 | ||
1093 | /* Just in case something has gone wrong! */ | |
1094 | zd_usb_disable_rx(usb); | |
1095 | zd_usb_disable_int(usb); | |
1096 | ||
1097 | /* If the disconnect has been caused by a removal of the | |
1098 | * driver module, the reset allows reloading of the driver. If the | |
1099 | * reset will not be executed here, the upload of the firmware in the | |
1100 | * probe function caused by the reloading of the driver will fail. | |
1101 | */ | |
1102 | usb_reset_device(interface_to_usbdev(intf)); | |
1103 | ||
e85d0918 DD |
1104 | zd_netdev_free(netdev); |
1105 | dev_dbg(&intf->dev, "disconnected\n"); | |
1106 | } | |
1107 | ||
1108 | static struct usb_driver driver = { | |
1109 | .name = "zd1211rw", | |
1110 | .id_table = usb_ids, | |
1111 | .probe = probe, | |
1112 | .disconnect = disconnect, | |
1113 | }; | |
1114 | ||
1115 | static int __init usb_init(void) | |
1116 | { | |
1117 | int r; | |
1118 | ||
1119 | pr_debug("usb_init()\n"); | |
1120 | ||
1121 | r = usb_register(&driver); | |
1122 | if (r) { | |
1123 | printk(KERN_ERR "usb_register() failed. Error number %d\n", r); | |
1124 | return r; | |
1125 | } | |
1126 | ||
1127 | pr_debug("zd1211rw initialized\n"); | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | static void __exit usb_exit(void) | |
1132 | { | |
1133 | pr_debug("usb_exit()\n"); | |
1134 | usb_deregister(&driver); | |
1135 | } | |
1136 | ||
1137 | module_init(usb_init); | |
1138 | module_exit(usb_exit); | |
1139 | ||
1140 | static int usb_int_regs_length(unsigned int count) | |
1141 | { | |
1142 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); | |
1143 | } | |
1144 | ||
1145 | static void prepare_read_regs_int(struct zd_usb *usb) | |
1146 | { | |
1147 | struct zd_usb_interrupt *intr = &usb->intr; | |
1148 | ||
1149 | spin_lock(&intr->lock); | |
1150 | intr->read_regs_enabled = 1; | |
1151 | INIT_COMPLETION(intr->read_regs.completion); | |
1152 | spin_unlock(&intr->lock); | |
1153 | } | |
1154 | ||
1155 | static int get_results(struct zd_usb *usb, u16 *values, | |
1156 | struct usb_req_read_regs *req, unsigned int count) | |
1157 | { | |
1158 | int r; | |
1159 | int i; | |
1160 | struct zd_usb_interrupt *intr = &usb->intr; | |
1161 | struct read_regs_int *rr = &intr->read_regs; | |
1162 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; | |
1163 | ||
1164 | spin_lock(&intr->lock); | |
1165 | ||
1166 | r = -EIO; | |
1167 | /* The created block size seems to be larger than expected. | |
1168 | * However results appear to be correct. | |
1169 | */ | |
1170 | if (rr->length < usb_int_regs_length(count)) { | |
1171 | dev_dbg_f(zd_usb_dev(usb), | |
1172 | "error: actual length %d less than expected %d\n", | |
1173 | rr->length, usb_int_regs_length(count)); | |
1174 | goto error_unlock; | |
1175 | } | |
1176 | if (rr->length > sizeof(rr->buffer)) { | |
1177 | dev_dbg_f(zd_usb_dev(usb), | |
1178 | "error: actual length %d exceeds buffer size %zu\n", | |
1179 | rr->length, sizeof(rr->buffer)); | |
1180 | goto error_unlock; | |
1181 | } | |
1182 | ||
1183 | for (i = 0; i < count; i++) { | |
1184 | struct reg_data *rd = ®s->regs[i]; | |
1185 | if (rd->addr != req->addr[i]) { | |
1186 | dev_dbg_f(zd_usb_dev(usb), | |
1187 | "rd[%d] addr %#06hx expected %#06hx\n", i, | |
1188 | le16_to_cpu(rd->addr), | |
1189 | le16_to_cpu(req->addr[i])); | |
1190 | goto error_unlock; | |
1191 | } | |
1192 | values[i] = le16_to_cpu(rd->value); | |
1193 | } | |
1194 | ||
1195 | r = 0; | |
1196 | error_unlock: | |
1197 | spin_unlock(&intr->lock); | |
1198 | return r; | |
1199 | } | |
1200 | ||
1201 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, | |
1202 | const zd_addr_t *addresses, unsigned int count) | |
1203 | { | |
1204 | int r; | |
1205 | int i, req_len, actual_req_len; | |
1206 | struct usb_device *udev; | |
1207 | struct usb_req_read_regs *req = NULL; | |
1208 | unsigned long timeout; | |
1209 | ||
1210 | if (count < 1) { | |
1211 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); | |
1212 | return -EINVAL; | |
1213 | } | |
1214 | if (count > USB_MAX_IOREAD16_COUNT) { | |
1215 | dev_dbg_f(zd_usb_dev(usb), | |
1216 | "error: count %u exceeds possible max %u\n", | |
1217 | count, USB_MAX_IOREAD16_COUNT); | |
1218 | return -EINVAL; | |
1219 | } | |
1220 | if (in_atomic()) { | |
1221 | dev_dbg_f(zd_usb_dev(usb), | |
1222 | "error: io in atomic context not supported\n"); | |
1223 | return -EWOULDBLOCK; | |
1224 | } | |
1225 | if (!usb_int_enabled(usb)) { | |
1226 | dev_dbg_f(zd_usb_dev(usb), | |
1227 | "error: usb interrupt not enabled\n"); | |
1228 | return -EWOULDBLOCK; | |
1229 | } | |
1230 | ||
1231 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); | |
1232 | req = kmalloc(req_len, GFP_NOFS); | |
1233 | if (!req) | |
1234 | return -ENOMEM; | |
1235 | req->id = cpu_to_le16(USB_REQ_READ_REGS); | |
1236 | for (i = 0; i < count; i++) | |
1237 | req->addr[i] = cpu_to_le16(usb_addr(usb, addresses[i])); | |
1238 | ||
1239 | udev = zd_usb_to_usbdev(usb); | |
1240 | prepare_read_regs_int(usb); | |
1241 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1242 | req, req_len, &actual_req_len, 1000 /* ms */); | |
1243 | if (r) { | |
1244 | dev_dbg_f(zd_usb_dev(usb), | |
1245 | "error in usb_bulk_msg(). Error number %d\n", r); | |
1246 | goto error; | |
1247 | } | |
1248 | if (req_len != actual_req_len) { | |
1249 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n" | |
1250 | " req_len %d != actual_req_len %d\n", | |
1251 | req_len, actual_req_len); | |
1252 | r = -EIO; | |
1253 | goto error; | |
1254 | } | |
1255 | ||
1256 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, | |
1257 | msecs_to_jiffies(1000)); | |
1258 | if (!timeout) { | |
1259 | disable_read_regs_int(usb); | |
1260 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); | |
1261 | r = -ETIMEDOUT; | |
1262 | goto error; | |
1263 | } | |
1264 | ||
1265 | r = get_results(usb, values, req, count); | |
1266 | error: | |
1267 | kfree(req); | |
1268 | return r; | |
1269 | } | |
1270 | ||
1271 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, | |
1272 | unsigned int count) | |
1273 | { | |
1274 | int r; | |
1275 | struct usb_device *udev; | |
1276 | struct usb_req_write_regs *req = NULL; | |
1277 | int i, req_len, actual_req_len; | |
1278 | ||
1279 | if (count == 0) | |
1280 | return 0; | |
1281 | if (count > USB_MAX_IOWRITE16_COUNT) { | |
1282 | dev_dbg_f(zd_usb_dev(usb), | |
1283 | "error: count %u exceeds possible max %u\n", | |
1284 | count, USB_MAX_IOWRITE16_COUNT); | |
1285 | return -EINVAL; | |
1286 | } | |
1287 | if (in_atomic()) { | |
1288 | dev_dbg_f(zd_usb_dev(usb), | |
1289 | "error: io in atomic context not supported\n"); | |
1290 | return -EWOULDBLOCK; | |
1291 | } | |
1292 | ||
1293 | req_len = sizeof(struct usb_req_write_regs) + | |
1294 | count * sizeof(struct reg_data); | |
1295 | req = kmalloc(req_len, GFP_NOFS); | |
1296 | if (!req) | |
1297 | return -ENOMEM; | |
1298 | ||
1299 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); | |
1300 | for (i = 0; i < count; i++) { | |
1301 | struct reg_data *rw = &req->reg_writes[i]; | |
1302 | rw->addr = cpu_to_le16(usb_addr(usb, ioreqs[i].addr)); | |
1303 | rw->value = cpu_to_le16(ioreqs[i].value); | |
1304 | } | |
1305 | ||
1306 | udev = zd_usb_to_usbdev(usb); | |
1307 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1308 | req, req_len, &actual_req_len, 1000 /* ms */); | |
1309 | if (r) { | |
1310 | dev_dbg_f(zd_usb_dev(usb), | |
1311 | "error in usb_bulk_msg(). Error number %d\n", r); | |
1312 | goto error; | |
1313 | } | |
1314 | if (req_len != actual_req_len) { | |
1315 | dev_dbg_f(zd_usb_dev(usb), | |
1316 | "error in usb_bulk_msg()" | |
1317 | " req_len %d != actual_req_len %d\n", | |
1318 | req_len, actual_req_len); | |
1319 | r = -EIO; | |
1320 | goto error; | |
1321 | } | |
1322 | ||
1323 | /* FALL-THROUGH with r == 0 */ | |
1324 | error: | |
1325 | kfree(req); | |
1326 | return r; | |
1327 | } | |
1328 | ||
1329 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) | |
1330 | { | |
1331 | int r; | |
1332 | struct usb_device *udev; | |
1333 | struct usb_req_rfwrite *req = NULL; | |
1334 | int i, req_len, actual_req_len; | |
1335 | u16 bit_value_template; | |
1336 | ||
1337 | if (in_atomic()) { | |
1338 | dev_dbg_f(zd_usb_dev(usb), | |
1339 | "error: io in atomic context not supported\n"); | |
1340 | return -EWOULDBLOCK; | |
1341 | } | |
1342 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { | |
1343 | dev_dbg_f(zd_usb_dev(usb), | |
1344 | "error: bits %d are smaller than" | |
1345 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", | |
1346 | bits, USB_MIN_RFWRITE_BIT_COUNT); | |
1347 | return -EINVAL; | |
1348 | } | |
1349 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { | |
1350 | dev_dbg_f(zd_usb_dev(usb), | |
1351 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", | |
1352 | bits, USB_MAX_RFWRITE_BIT_COUNT); | |
1353 | return -EINVAL; | |
1354 | } | |
1355 | #ifdef DEBUG | |
1356 | if (value & (~0UL << bits)) { | |
1357 | dev_dbg_f(zd_usb_dev(usb), | |
1358 | "error: value %#09x has bits >= %d set\n", | |
1359 | value, bits); | |
1360 | return -EINVAL; | |
1361 | } | |
1362 | #endif /* DEBUG */ | |
1363 | ||
1364 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); | |
1365 | ||
1366 | r = zd_usb_ioread16(usb, &bit_value_template, CR203); | |
1367 | if (r) { | |
1368 | dev_dbg_f(zd_usb_dev(usb), | |
1369 | "error %d: Couldn't read CR203\n", r); | |
1370 | goto out; | |
1371 | } | |
1372 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); | |
1373 | ||
1374 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); | |
1375 | req = kmalloc(req_len, GFP_NOFS); | |
1376 | if (!req) | |
1377 | return -ENOMEM; | |
1378 | ||
1379 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); | |
1380 | /* 1: 3683a, but not used in ZYDAS driver */ | |
1381 | req->value = cpu_to_le16(2); | |
1382 | req->bits = cpu_to_le16(bits); | |
1383 | ||
1384 | for (i = 0; i < bits; i++) { | |
1385 | u16 bv = bit_value_template; | |
1386 | if (value & (1 << (bits-1-i))) | |
1387 | bv |= RF_DATA; | |
1388 | req->bit_values[i] = cpu_to_le16(bv); | |
1389 | } | |
1390 | ||
1391 | udev = zd_usb_to_usbdev(usb); | |
1392 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1393 | req, req_len, &actual_req_len, 1000 /* ms */); | |
1394 | if (r) { | |
1395 | dev_dbg_f(zd_usb_dev(usb), | |
1396 | "error in usb_bulk_msg(). Error number %d\n", r); | |
1397 | goto out; | |
1398 | } | |
1399 | if (req_len != actual_req_len) { | |
1400 | dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()" | |
1401 | " req_len %d != actual_req_len %d\n", | |
1402 | req_len, actual_req_len); | |
1403 | r = -EIO; | |
1404 | goto out; | |
1405 | } | |
1406 | ||
1407 | /* FALL-THROUGH with r == 0 */ | |
1408 | out: | |
1409 | kfree(req); | |
1410 | return r; | |
1411 | } |