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