Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[deliverable/linux.git] / drivers / usb / misc / ftdi-elan.c
1 /*
2 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
3 *
4 * Copyright(C) 2006 Elan Digital Systems Limited
5 * http://www.elandigitalsystems.com
6 *
7 * Author and Maintainer - Tony Olech - Elan Digital Systems
8 * tony.olech@elandigitalsystems.com
9 *
10 * This program is free software;you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
13 *
14 *
15 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
16 * based on various USB client drivers in the 2.6.15 linux kernel
17 * with constant reference to the 3rd Edition of Linux Device Drivers
18 * published by O'Reilly
19 *
20 * The U132 adapter is a USB to CardBus adapter specifically designed
21 * for PC cards that contain an OHCI host controller. Typical PC cards
22 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
23 *
24 * The U132 adapter will *NOT *work with PC cards that do not contain
25 * an OHCI controller. A simple way to test whether a PC card has an
26 * OHCI controller as an interface is to insert the PC card directly
27 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
28 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
29 * then there is a good chance that the U132 adapter will support the
30 * PC card.(you also need the specific client driver for the PC card)
31 *
32 * Please inform the Author and Maintainer about any PC cards that
33 * contain OHCI Host Controller and work when directly connected to
34 * an embedded CardBus slot but do not work when they are connected
35 * via an ELAN U132 adapter.
36 *
37 */
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <linux/ioctl.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <asm/uaccess.h>
47 #include <linux/usb.h>
48 #include <linux/workqueue.h>
49 #include <linux/platform_device.h>
50 MODULE_AUTHOR("Tony Olech");
51 MODULE_DESCRIPTION("FTDI ELAN driver");
52 MODULE_LICENSE("GPL");
53 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
54 extern struct platform_driver u132_platform_driver;
55 static struct workqueue_struct *status_queue;
56 static struct workqueue_struct *command_queue;
57 static struct workqueue_struct *respond_queue;
58 /*
59 * ftdi_module_lock exists to protect access to global variables
60 *
61 */
62 static struct semaphore ftdi_module_lock;
63 static int ftdi_instances = 0;
64 static struct list_head ftdi_static_list;
65 /*
66 * end of the global variables protected by ftdi_module_lock
67 */
68 #include "usb_u132.h"
69 #define TD_DEVNOTRESP 5
70 /* Define these values to match your devices*/
71 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
72 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
73 /* table of devices that work with this driver*/
74 static struct usb_device_id ftdi_elan_table[] = {
75 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
76 { /* Terminating entry */ }
77 };
78
79 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
80 /* only the jtag(firmware upgrade device) interface requires
81 * a device file and corresponding minor number, but the
82 * interface is created unconditionally - I suppose it could
83 * be configured or not according to a module parameter.
84 * But since we(now) require one interface per device,
85 * and since it unlikely that a normal installation would
86 * require more than a couple of elan-ftdi devices, 8 seems
87 * like a reasonable limit to have here, and if someone
88 * really requires more than 8 devices, then they can frig the
89 * code and recompile
90 */
91 #define USB_FTDI_ELAN_MINOR_BASE 192
92 #define COMMAND_BITS 5
93 #define COMMAND_SIZE (1<<COMMAND_BITS)
94 #define COMMAND_MASK (COMMAND_SIZE-1)
95 struct u132_command {
96 u8 header;
97 u16 length;
98 u8 address;
99 u8 width;
100 u32 value;
101 int follows;
102 void *buffer;
103 };
104 #define RESPOND_BITS 5
105 #define RESPOND_SIZE (1<<RESPOND_BITS)
106 #define RESPOND_MASK (RESPOND_SIZE-1)
107 struct u132_respond {
108 u8 header;
109 u8 address;
110 u32 *value;
111 int *result;
112 struct completion wait_completion;
113 };
114 struct u132_target {
115 void *endp;
116 struct urb *urb;
117 int toggle_bits;
118 int error_count;
119 int condition_code;
120 int repeat_number;
121 int halted;
122 int skipped;
123 int actual;
124 int non_null;
125 int active;
126 int abandoning;
127 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
128 int toggle_bits, int error_count, int condition_code,
129 int repeat_number, int halted, int skipped, int actual,
130 int non_null);
131 };
132 /* Structure to hold all of our device specific stuff*/
133 struct usb_ftdi {
134 struct list_head ftdi_list;
135 struct semaphore u132_lock;
136 int command_next;
137 int command_head;
138 struct u132_command command[COMMAND_SIZE];
139 int respond_next;
140 int respond_head;
141 struct u132_respond respond[RESPOND_SIZE];
142 struct u132_target target[4];
143 char device_name[16];
144 unsigned synchronized:1;
145 unsigned enumerated:1;
146 unsigned registered:1;
147 unsigned initialized:1;
148 unsigned card_ejected:1;
149 int function;
150 int sequence_num;
151 int disconnected;
152 int gone_away;
153 int stuck_status;
154 int status_queue_delay;
155 struct semaphore sw_lock;
156 struct usb_device *udev;
157 struct usb_interface *interface;
158 struct usb_class_driver *class;
159 struct delayed_work status_work;
160 struct delayed_work command_work;
161 struct delayed_work respond_work;
162 struct u132_platform_data platform_data;
163 struct resource resources[0];
164 struct platform_device platform_dev;
165 unsigned char *bulk_in_buffer;
166 size_t bulk_in_size;
167 size_t bulk_in_last;
168 size_t bulk_in_left;
169 __u8 bulk_in_endpointAddr;
170 __u8 bulk_out_endpointAddr;
171 struct kref kref;
172 u32 controlreg;
173 u8 response[4 + 1024];
174 int expected;
175 int recieved;
176 int ed_found;
177 };
178 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
179 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
180 platform_dev)
181 static struct usb_driver ftdi_elan_driver;
182 static void ftdi_elan_delete(struct kref *kref)
183 {
184 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
185 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
186 usb_put_dev(ftdi->udev);
187 ftdi->disconnected += 1;
188 down(&ftdi_module_lock);
189 list_del_init(&ftdi->ftdi_list);
190 ftdi_instances -= 1;
191 up(&ftdi_module_lock);
192 kfree(ftdi->bulk_in_buffer);
193 ftdi->bulk_in_buffer = NULL;
194 }
195
196 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
197 {
198 kref_put(&ftdi->kref, ftdi_elan_delete);
199 }
200
201 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
202 {
203 kref_get(&ftdi->kref);
204 }
205
206 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
207 {
208 kref_init(&ftdi->kref);
209 }
210
211 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
212 {
213 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
214 kref_put(&ftdi->kref, ftdi_elan_delete);
215 }
216
217 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
218 {
219 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
220 kref_get(&ftdi->kref);
221 }
222
223 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
224 {
225 if (cancel_delayed_work(&ftdi->status_work))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
227 }
228
229 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
230 {
231 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
232 kref_put(&ftdi->kref, ftdi_elan_delete);
233 }
234
235 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
236 {
237 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
238 kref_get(&ftdi->kref);
239 }
240
241 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
242 {
243 if (cancel_delayed_work(&ftdi->command_work))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
245 }
246
247 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
248 unsigned int delta)
249 {
250 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
251 kref_put(&ftdi->kref, ftdi_elan_delete);
252 }
253
254 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
255 {
256 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
257 kref_get(&ftdi->kref);
258 }
259
260 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
261 {
262 if (cancel_delayed_work(&ftdi->respond_work))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
264 }
265
266 void ftdi_elan_gone_away(struct platform_device *pdev)
267 {
268 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
269 ftdi->gone_away += 1;
270 ftdi_elan_put_kref(ftdi);
271 }
272
273
274 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
275 static void ftdi_release_platform_dev(struct device *dev)
276 {
277 dev->parent = NULL;
278 }
279
280 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
281 struct u132_target *target, u8 *buffer, int length);
282 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
283 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
284 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
285 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
286 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
287 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
288 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
289 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
290 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
291 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
292 {
293 int result;
294 if (ftdi->platform_dev.dev.parent)
295 return -EBUSY;
296 ftdi_elan_get_kref(ftdi);
297 ftdi->platform_data.potpg = 100;
298 ftdi->platform_data.reset = NULL;
299 ftdi->platform_dev.id = ftdi->sequence_num;
300 ftdi->platform_dev.resource = ftdi->resources;
301 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
302 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
303 ftdi->platform_dev.dev.parent = NULL;
304 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
305 ftdi->platform_dev.dev.dma_mask = NULL;
306 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
307 ftdi->platform_dev.name = ftdi->device_name;
308 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
309 request_module("u132_hcd");
310 dev_info(&ftdi->udev->dev, "registering '%s'\n",
311 ftdi->platform_dev.name);
312 result = platform_device_register(&ftdi->platform_dev);
313 return result;
314 }
315
316 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
317 {
318 down(&ftdi->u132_lock);
319 while (ftdi->respond_next > ftdi->respond_head) {
320 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
321 ftdi->respond_head++];
322 *respond->result = -ESHUTDOWN;
323 *respond->value = 0;
324 complete(&respond->wait_completion);
325 } up(&ftdi->u132_lock);
326 }
327
328 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
329 {
330 int ed_number = 4;
331 down(&ftdi->u132_lock);
332 while (ed_number-- > 0) {
333 struct u132_target *target = &ftdi->target[ed_number];
334 if (target->active == 1) {
335 target->condition_code = TD_DEVNOTRESP;
336 up(&ftdi->u132_lock);
337 ftdi_elan_do_callback(ftdi, target, NULL, 0);
338 down(&ftdi->u132_lock);
339 }
340 }
341 ftdi->recieved = 0;
342 ftdi->expected = 4;
343 ftdi->ed_found = 0;
344 up(&ftdi->u132_lock);
345 }
346
347 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
348 {
349 int ed_number = 4;
350 down(&ftdi->u132_lock);
351 while (ed_number-- > 0) {
352 struct u132_target *target = &ftdi->target[ed_number];
353 target->abandoning = 1;
354 wait_1:if (target->active == 1) {
355 int command_size = ftdi->command_next -
356 ftdi->command_head;
357 if (command_size < COMMAND_SIZE) {
358 struct u132_command *command = &ftdi->command[
359 COMMAND_MASK & ftdi->command_next];
360 command->header = 0x80 | (ed_number << 5) | 0x4;
361 command->length = 0x00;
362 command->address = 0x00;
363 command->width = 0x00;
364 command->follows = 0;
365 command->value = 0;
366 command->buffer = &command->value;
367 ftdi->command_next += 1;
368 ftdi_elan_kick_command_queue(ftdi);
369 } else {
370 up(&ftdi->u132_lock);
371 msleep(100);
372 down(&ftdi->u132_lock);
373 goto wait_1;
374 }
375 }
376 wait_2:if (target->active == 1) {
377 int command_size = ftdi->command_next -
378 ftdi->command_head;
379 if (command_size < COMMAND_SIZE) {
380 struct u132_command *command = &ftdi->command[
381 COMMAND_MASK & ftdi->command_next];
382 command->header = 0x90 | (ed_number << 5);
383 command->length = 0x00;
384 command->address = 0x00;
385 command->width = 0x00;
386 command->follows = 0;
387 command->value = 0;
388 command->buffer = &command->value;
389 ftdi->command_next += 1;
390 ftdi_elan_kick_command_queue(ftdi);
391 } else {
392 up(&ftdi->u132_lock);
393 msleep(100);
394 down(&ftdi->u132_lock);
395 goto wait_2;
396 }
397 }
398 }
399 ftdi->recieved = 0;
400 ftdi->expected = 4;
401 ftdi->ed_found = 0;
402 up(&ftdi->u132_lock);
403 }
404
405 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
406 {
407 int ed_number = 4;
408 down(&ftdi->u132_lock);
409 while (ed_number-- > 0) {
410 struct u132_target *target = &ftdi->target[ed_number];
411 target->abandoning = 1;
412 wait:if (target->active == 1) {
413 int command_size = ftdi->command_next -
414 ftdi->command_head;
415 if (command_size < COMMAND_SIZE) {
416 struct u132_command *command = &ftdi->command[
417 COMMAND_MASK & ftdi->command_next];
418 command->header = 0x80 | (ed_number << 5) | 0x4;
419 command->length = 0x00;
420 command->address = 0x00;
421 command->width = 0x00;
422 command->follows = 0;
423 command->value = 0;
424 command->buffer = &command->value;
425 ftdi->command_next += 1;
426 ftdi_elan_kick_command_queue(ftdi);
427 } else {
428 up(&ftdi->u132_lock);
429 msleep(100);
430 down(&ftdi->u132_lock);
431 goto wait;
432 }
433 }
434 }
435 ftdi->recieved = 0;
436 ftdi->expected = 4;
437 ftdi->ed_found = 0;
438 up(&ftdi->u132_lock);
439 }
440
441 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
442 {
443 ftdi_command_queue_work(ftdi, 0);
444 return;
445 }
446
447 static void ftdi_elan_command_work(struct work_struct *work)
448 {
449 struct usb_ftdi *ftdi =
450 container_of(work, struct usb_ftdi, command_work.work);
451
452 if (ftdi->disconnected > 0) {
453 ftdi_elan_put_kref(ftdi);
454 return;
455 } else {
456 int retval = ftdi_elan_command_engine(ftdi);
457 if (retval == -ESHUTDOWN) {
458 ftdi->disconnected += 1;
459 } else if (retval == -ENODEV) {
460 ftdi->disconnected += 1;
461 } else if (retval)
462 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
463 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
464 return;
465 }
466 }
467
468 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
469 {
470 ftdi_respond_queue_work(ftdi, 0);
471 return;
472 }
473
474 static void ftdi_elan_respond_work(struct work_struct *work)
475 {
476 struct usb_ftdi *ftdi =
477 container_of(work, struct usb_ftdi, respond_work.work);
478 if (ftdi->disconnected > 0) {
479 ftdi_elan_put_kref(ftdi);
480 return;
481 } else {
482 int retval = ftdi_elan_respond_engine(ftdi);
483 if (retval == 0) {
484 } else if (retval == -ESHUTDOWN) {
485 ftdi->disconnected += 1;
486 } else if (retval == -ENODEV) {
487 ftdi->disconnected += 1;
488 } else if (retval == -EILSEQ) {
489 ftdi->disconnected += 1;
490 } else {
491 ftdi->disconnected += 1;
492 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
493 }
494 if (ftdi->disconnected > 0) {
495 ftdi_elan_abandon_completions(ftdi);
496 ftdi_elan_abandon_targets(ftdi);
497 }
498 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
499 return;
500 }
501 }
502
503
504 /*
505 * the sw_lock is initially held and will be freed
506 * after the FTDI has been synchronized
507 *
508 */
509 static void ftdi_elan_status_work(struct work_struct *work)
510 {
511 struct usb_ftdi *ftdi =
512 container_of(work, struct usb_ftdi, status_work.work);
513 int work_delay_in_msec = 0;
514 if (ftdi->disconnected > 0) {
515 ftdi_elan_put_kref(ftdi);
516 return;
517 } else if (ftdi->synchronized == 0) {
518 down(&ftdi->sw_lock);
519 if (ftdi_elan_synchronize(ftdi) == 0) {
520 ftdi->synchronized = 1;
521 ftdi_command_queue_work(ftdi, 1);
522 ftdi_respond_queue_work(ftdi, 1);
523 up(&ftdi->sw_lock);
524 work_delay_in_msec = 100;
525 } else {
526 dev_err(&ftdi->udev->dev, "synchronize failed\n");
527 up(&ftdi->sw_lock);
528 work_delay_in_msec = 10 *1000;
529 }
530 } else if (ftdi->stuck_status > 0) {
531 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
532 ftdi->stuck_status = 0;
533 ftdi->synchronized = 0;
534 } else if ((ftdi->stuck_status++ % 60) == 1) {
535 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
536 "- please remove\n");
537 } else
538 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
539 "- checked %d times\n", ftdi->stuck_status);
540 work_delay_in_msec = 100;
541 } else if (ftdi->enumerated == 0) {
542 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
543 ftdi->enumerated = 1;
544 work_delay_in_msec = 250;
545 } else
546 work_delay_in_msec = 1000;
547 } else if (ftdi->initialized == 0) {
548 if (ftdi_elan_setupOHCI(ftdi) == 0) {
549 ftdi->initialized = 1;
550 work_delay_in_msec = 500;
551 } else {
552 dev_err(&ftdi->udev->dev, "initialized failed - trying "
553 "again in 10 seconds\n");
554 work_delay_in_msec = 10 *1000;
555 }
556 } else if (ftdi->registered == 0) {
557 work_delay_in_msec = 10;
558 if (ftdi_elan_hcd_init(ftdi) == 0) {
559 ftdi->registered = 1;
560 } else
561 dev_err(&ftdi->udev->dev, "register failed\n");
562 work_delay_in_msec = 250;
563 } else {
564 if (ftdi_elan_checkingPCI(ftdi) == 0) {
565 work_delay_in_msec = 250;
566 } else if (ftdi->controlreg & 0x00400000) {
567 if (ftdi->gone_away > 0) {
568 dev_err(&ftdi->udev->dev, "PCI device eject con"
569 "firmed platform_dev.dev.parent=%p plat"
570 "form_dev.dev=%p\n",
571 ftdi->platform_dev.dev.parent,
572 &ftdi->platform_dev.dev);
573 platform_device_unregister(&ftdi->platform_dev);
574 ftdi->platform_dev.dev.parent = NULL;
575 ftdi->registered = 0;
576 ftdi->enumerated = 0;
577 ftdi->card_ejected = 0;
578 ftdi->initialized = 0;
579 ftdi->gone_away = 0;
580 } else
581 ftdi_elan_flush_targets(ftdi);
582 work_delay_in_msec = 250;
583 } else {
584 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
585 );
586 ftdi_elan_cancel_targets(ftdi);
587 work_delay_in_msec = 500;
588 ftdi->enumerated = 0;
589 ftdi->initialized = 0;
590 }
591 }
592 if (ftdi->disconnected > 0) {
593 ftdi_elan_put_kref(ftdi);
594 return;
595 } else {
596 ftdi_status_requeue_work(ftdi,
597 msecs_to_jiffies(work_delay_in_msec));
598 return;
599 }
600 }
601
602
603 /*
604 * file_operations for the jtag interface
605 *
606 * the usage count for the device is incremented on open()
607 * and decremented on release()
608 */
609 static int ftdi_elan_open(struct inode *inode, struct file *file)
610 {
611 int subminor = iminor(inode);
612 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
613 subminor);
614 if (!interface) {
615 printk(KERN_ERR "can't find device for minor %d\n", subminor);
616 return -ENODEV;
617 } else {
618 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
619 if (!ftdi) {
620 return -ENODEV;
621 } else {
622 if (down_interruptible(&ftdi->sw_lock)) {
623 return -EINTR;
624 } else {
625 ftdi_elan_get_kref(ftdi);
626 file->private_data = ftdi;
627 return 0;
628 }
629 }
630 }
631 }
632
633 static int ftdi_elan_release(struct inode *inode, struct file *file)
634 {
635 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
636 if (ftdi == NULL)
637 return -ENODEV;
638 up(&ftdi->sw_lock); /* decrement the count on our device */
639 ftdi_elan_put_kref(ftdi);
640 return 0;
641 }
642
643
644 #define FTDI_ELAN_IOC_MAGIC 0xA1
645 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
646 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
647 unsigned int cmd, unsigned long arg)
648 {
649 switch (cmd) {
650 case FTDI_ELAN_IOCDEBUG:{
651 char line[132];
652 int size = strncpy_from_user(line,
653 (const char __user *)arg, sizeof(line));
654 if (size < 0) {
655 return -EINVAL;
656 } else {
657 printk(KERN_ERR "TODO: ioctl %s\n", line);
658 return 0;
659 }
660 }
661 default:
662 return -EFAULT;
663 }
664 }
665
666
667 /*
668 *
669 * blocking bulk reads are used to get data from the device
670 *
671 */
672 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
673 size_t count, loff_t *ppos)
674 {
675 char data[30 *3 + 4];
676 char *d = data;
677 int m = (sizeof(data) - 1) / 3;
678 int bytes_read = 0;
679 int retry_on_empty = 10;
680 int retry_on_timeout = 5;
681 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
682 if (ftdi->disconnected > 0) {
683 return -ENODEV;
684 }
685 data[0] = 0;
686 have:if (ftdi->bulk_in_left > 0) {
687 if (count-- > 0) {
688 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
689 ftdi->bulk_in_left -= 1;
690 if (bytes_read < m) {
691 d += sprintf(d, " %02X", 0x000000FF & *p);
692 } else if (bytes_read > m) {
693 } else
694 d += sprintf(d, " ..");
695 if (copy_to_user(buffer++, p, 1)) {
696 return -EFAULT;
697 } else {
698 bytes_read += 1;
699 goto have;
700 }
701 } else
702 return bytes_read;
703 }
704 more:if (count > 0) {
705 int packet_bytes = 0;
706 int retval = usb_bulk_msg(ftdi->udev,
707 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
708 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
709 &packet_bytes, msecs_to_jiffies(50));
710 if (packet_bytes > 2) {
711 ftdi->bulk_in_left = packet_bytes - 2;
712 ftdi->bulk_in_last = 1;
713 goto have;
714 } else if (retval == -ETIMEDOUT) {
715 if (retry_on_timeout-- > 0) {
716 goto more;
717 } else if (bytes_read > 0) {
718 return bytes_read;
719 } else
720 return retval;
721 } else if (retval == 0) {
722 if (retry_on_empty-- > 0) {
723 goto more;
724 } else
725 return bytes_read;
726 } else
727 return retval;
728 } else
729 return bytes_read;
730 }
731
732 static void ftdi_elan_write_bulk_callback(struct urb *urb)
733 {
734 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
735 if (urb->status && !(urb->status == -ENOENT || urb->status ==
736 -ECONNRESET || urb->status == -ESHUTDOWN)) {
737 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
738 "d\n", urb, urb->status);
739 }
740 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
741 urb->transfer_buffer, urb->transfer_dma);
742 }
743
744 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
745 char *buf, int command_size, int total_size)
746 {
747 int ed_commands = 0;
748 int b = 0;
749 int I = command_size;
750 int i = ftdi->command_head;
751 while (I-- > 0) {
752 struct u132_command *command = &ftdi->command[COMMAND_MASK &
753 i++];
754 int F = command->follows;
755 u8 *f = command->buffer;
756 if (command->header & 0x80) {
757 ed_commands |= 1 << (0x3 & (command->header >> 5));
758 }
759 buf[b++] = command->header;
760 buf[b++] = (command->length >> 0) & 0x00FF;
761 buf[b++] = (command->length >> 8) & 0x00FF;
762 buf[b++] = command->address;
763 buf[b++] = command->width;
764 while (F-- > 0) {
765 buf[b++] = *f++;
766 }
767 }
768 return ed_commands;
769 }
770
771 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
772 {
773 int total_size = 0;
774 int I = command_size;
775 int i = ftdi->command_head;
776 while (I-- > 0) {
777 struct u132_command *command = &ftdi->command[COMMAND_MASK &
778 i++];
779 total_size += 5 + command->follows;
780 } return total_size;
781 }
782
783 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
784 {
785 int retval;
786 char *buf;
787 int ed_commands;
788 int total_size;
789 struct urb *urb;
790 int command_size = ftdi->command_next - ftdi->command_head;
791 if (command_size == 0)
792 return 0;
793 total_size = ftdi_elan_total_command_size(ftdi, command_size);
794 urb = usb_alloc_urb(0, GFP_KERNEL);
795 if (!urb) {
796 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
797 "ands totaling %d bytes to the Uxxx\n", command_size,
798 total_size);
799 return -ENOMEM;
800 }
801 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
802 &urb->transfer_dma);
803 if (!buf) {
804 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
805 "ommands totaling %d bytes to the Uxxx\n", command_size,
806 total_size);
807 usb_free_urb(urb);
808 return -ENOMEM;
809 }
810 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
811 command_size, total_size);
812 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
813 ftdi->bulk_out_endpointAddr), buf, total_size,
814 ftdi_elan_write_bulk_callback, ftdi);
815 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
816 if (ed_commands) {
817 char diag[40 *3 + 4];
818 char *d = diag;
819 int m = total_size;
820 u8 *c = buf;
821 int s = (sizeof(diag) - 1) / 3;
822 diag[0] = 0;
823 while (s-- > 0 && m-- > 0) {
824 if (s > 0 || m == 0) {
825 d += sprintf(d, " %02X", *c++);
826 } else
827 d += sprintf(d, " ..");
828 }
829 }
830 retval = usb_submit_urb(urb, GFP_KERNEL);
831 if (retval) {
832 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
833 "%d commands totaling %d bytes to the Uxxx\n", retval,
834 urb, command_size, total_size);
835 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
836 usb_free_urb(urb);
837 return retval;
838 }
839 usb_free_urb(urb); /* release our reference to this urb,
840 the USB core will eventually free it entirely */
841 ftdi->command_head += command_size;
842 ftdi_elan_kick_respond_queue(ftdi);
843 return 0;
844 }
845
846 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
847 struct u132_target *target, u8 *buffer, int length)
848 {
849 struct urb *urb = target->urb;
850 int halted = target->halted;
851 int skipped = target->skipped;
852 int actual = target->actual;
853 int non_null = target->non_null;
854 int toggle_bits = target->toggle_bits;
855 int error_count = target->error_count;
856 int condition_code = target->condition_code;
857 int repeat_number = target->repeat_number;
858 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
859 int, int, int, int) = target->callback;
860 target->active -= 1;
861 target->callback = NULL;
862 (*callback) (target->endp, urb, buffer, length, toggle_bits,
863 error_count, condition_code, repeat_number, halted, skipped,
864 actual, non_null);
865 }
866
867 static char *have_ed_set_response(struct usb_ftdi *ftdi,
868 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
869 char *b)
870 {
871 int payload = (ed_length >> 0) & 0x07FF;
872 down(&ftdi->u132_lock);
873 target->actual = 0;
874 target->non_null = (ed_length >> 15) & 0x0001;
875 target->repeat_number = (ed_length >> 11) & 0x000F;
876 if (ed_type == 0x02) {
877 if (payload == 0 || target->abandoning > 0) {
878 target->abandoning = 0;
879 up(&ftdi->u132_lock);
880 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
881 payload);
882 ftdi->recieved = 0;
883 ftdi->expected = 4;
884 ftdi->ed_found = 0;
885 return ftdi->response;
886 } else {
887 ftdi->expected = 4 + payload;
888 ftdi->ed_found = 1;
889 up(&ftdi->u132_lock);
890 return b;
891 }
892 } else if (ed_type == 0x03) {
893 if (payload == 0 || target->abandoning > 0) {
894 target->abandoning = 0;
895 up(&ftdi->u132_lock);
896 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
897 payload);
898 ftdi->recieved = 0;
899 ftdi->expected = 4;
900 ftdi->ed_found = 0;
901 return ftdi->response;
902 } else {
903 ftdi->expected = 4 + payload;
904 ftdi->ed_found = 1;
905 up(&ftdi->u132_lock);
906 return b;
907 }
908 } else if (ed_type == 0x01) {
909 target->abandoning = 0;
910 up(&ftdi->u132_lock);
911 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
912 payload);
913 ftdi->recieved = 0;
914 ftdi->expected = 4;
915 ftdi->ed_found = 0;
916 return ftdi->response;
917 } else {
918 target->abandoning = 0;
919 up(&ftdi->u132_lock);
920 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
921 payload);
922 ftdi->recieved = 0;
923 ftdi->expected = 4;
924 ftdi->ed_found = 0;
925 return ftdi->response;
926 }
927 }
928
929 static char *have_ed_get_response(struct usb_ftdi *ftdi,
930 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
931 char *b)
932 {
933 down(&ftdi->u132_lock);
934 target->condition_code = TD_DEVNOTRESP;
935 target->actual = (ed_length >> 0) & 0x01FF;
936 target->non_null = (ed_length >> 15) & 0x0001;
937 target->repeat_number = (ed_length >> 11) & 0x000F;
938 up(&ftdi->u132_lock);
939 if (target->active)
940 ftdi_elan_do_callback(ftdi, target, NULL, 0);
941 target->abandoning = 0;
942 ftdi->recieved = 0;
943 ftdi->expected = 4;
944 ftdi->ed_found = 0;
945 return ftdi->response;
946 }
947
948
949 /*
950 * The engine tries to empty the FTDI fifo
951 *
952 * all responses found in the fifo data are dispatched thus
953 * the response buffer can only ever hold a maximum sized
954 * response from the Uxxx.
955 *
956 */
957 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
958 {
959 u8 *b = ftdi->response + ftdi->recieved;
960 int bytes_read = 0;
961 int retry_on_empty = 1;
962 int retry_on_timeout = 3;
963 int empty_packets = 0;
964 read:{
965 int packet_bytes = 0;
966 int retval = usb_bulk_msg(ftdi->udev,
967 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
968 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
969 &packet_bytes, msecs_to_jiffies(500));
970 char diag[30 *3 + 4];
971 char *d = diag;
972 int m = packet_bytes;
973 u8 *c = ftdi->bulk_in_buffer;
974 int s = (sizeof(diag) - 1) / 3;
975 diag[0] = 0;
976 while (s-- > 0 && m-- > 0) {
977 if (s > 0 || m == 0) {
978 d += sprintf(d, " %02X", *c++);
979 } else
980 d += sprintf(d, " ..");
981 }
982 if (packet_bytes > 2) {
983 ftdi->bulk_in_left = packet_bytes - 2;
984 ftdi->bulk_in_last = 1;
985 goto have;
986 } else if (retval == -ETIMEDOUT) {
987 if (retry_on_timeout-- > 0) {
988 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
989 "t_bytes = %d with total %d bytes%s\n",
990 packet_bytes, bytes_read, diag);
991 goto more;
992 } else if (bytes_read > 0) {
993 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
994 bytes_read, diag);
995 return -ENOMEM;
996 } else {
997 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
998 "t_bytes = %d with total %d bytes%s\n",
999 packet_bytes, bytes_read, diag);
1000 return -ENOMEM;
1001 }
1002 } else if (retval == -EILSEQ) {
1003 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1004 " = %d with total %d bytes%s\n", retval,
1005 packet_bytes, bytes_read, diag);
1006 return retval;
1007 } else if (retval) {
1008 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1009 " = %d with total %d bytes%s\n", retval,
1010 packet_bytes, bytes_read, diag);
1011 return retval;
1012 } else if (packet_bytes == 2) {
1013 unsigned char s0 = ftdi->bulk_in_buffer[0];
1014 unsigned char s1 = ftdi->bulk_in_buffer[1];
1015 empty_packets += 1;
1016 if (s0 == 0x31 && s1 == 0x60) {
1017 if (retry_on_empty-- > 0) {
1018 goto more;
1019 } else
1020 return 0;
1021 } else if (s0 == 0x31 && s1 == 0x00) {
1022 if (retry_on_empty-- > 0) {
1023 goto more;
1024 } else
1025 return 0;
1026 } else {
1027 if (retry_on_empty-- > 0) {
1028 goto more;
1029 } else
1030 return 0;
1031 }
1032 } else if (packet_bytes == 1) {
1033 if (retry_on_empty-- > 0) {
1034 goto more;
1035 } else
1036 return 0;
1037 } else {
1038 if (retry_on_empty-- > 0) {
1039 goto more;
1040 } else
1041 return 0;
1042 }
1043 }
1044 more:{
1045 goto read;
1046 }
1047 have:if (ftdi->bulk_in_left > 0) {
1048 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1049 bytes_read += 1;
1050 ftdi->bulk_in_left -= 1;
1051 if (ftdi->recieved == 0 && c == 0xFF) {
1052 goto have;
1053 } else
1054 *b++ = c;
1055 if (++ftdi->recieved < ftdi->expected) {
1056 goto have;
1057 } else if (ftdi->ed_found) {
1058 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1059 u16 ed_length = (ftdi->response[2] << 8) |
1060 ftdi->response[1];
1061 struct u132_target *target = &ftdi->target[ed_number];
1062 int payload = (ed_length >> 0) & 0x07FF;
1063 char diag[30 *3 + 4];
1064 char *d = diag;
1065 int m = payload;
1066 u8 *c = 4 + ftdi->response;
1067 int s = (sizeof(diag) - 1) / 3;
1068 diag[0] = 0;
1069 while (s-- > 0 && m-- > 0) {
1070 if (s > 0 || m == 0) {
1071 d += sprintf(d, " %02X", *c++);
1072 } else
1073 d += sprintf(d, " ..");
1074 }
1075 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1076 payload);
1077 ftdi->recieved = 0;
1078 ftdi->expected = 4;
1079 ftdi->ed_found = 0;
1080 b = ftdi->response;
1081 goto have;
1082 } else if (ftdi->expected == 8) {
1083 u8 buscmd;
1084 int respond_head = ftdi->respond_head++;
1085 struct u132_respond *respond = &ftdi->respond[
1086 RESPOND_MASK & respond_head];
1087 u32 data = ftdi->response[7];
1088 data <<= 8;
1089 data |= ftdi->response[6];
1090 data <<= 8;
1091 data |= ftdi->response[5];
1092 data <<= 8;
1093 data |= ftdi->response[4];
1094 *respond->value = data;
1095 *respond->result = 0;
1096 complete(&respond->wait_completion);
1097 ftdi->recieved = 0;
1098 ftdi->expected = 4;
1099 ftdi->ed_found = 0;
1100 b = ftdi->response;
1101 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1102 if (buscmd == 0x00) {
1103 } else if (buscmd == 0x02) {
1104 } else if (buscmd == 0x06) {
1105 } else if (buscmd == 0x0A) {
1106 } else
1107 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1108 "lue = %08X\n", buscmd, data);
1109 goto have;
1110 } else {
1111 if ((ftdi->response[0] & 0x80) == 0x00) {
1112 ftdi->expected = 8;
1113 goto have;
1114 } else {
1115 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1116 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1117 u16 ed_length = (ftdi->response[2] << 8) |
1118 ftdi->response[1];
1119 struct u132_target *target = &ftdi->target[
1120 ed_number];
1121 target->halted = (ftdi->response[0] >> 3) &
1122 0x01;
1123 target->skipped = (ftdi->response[0] >> 2) &
1124 0x01;
1125 target->toggle_bits = (ftdi->response[3] >> 6)
1126 & 0x03;
1127 target->error_count = (ftdi->response[3] >> 4)
1128 & 0x03;
1129 target->condition_code = (ftdi->response[
1130 3] >> 0) & 0x0F;
1131 if ((ftdi->response[0] & 0x10) == 0x00) {
1132 b = have_ed_set_response(ftdi, target,
1133 ed_length, ed_number, ed_type,
1134 b);
1135 goto have;
1136 } else {
1137 b = have_ed_get_response(ftdi, target,
1138 ed_length, ed_number, ed_type,
1139 b);
1140 goto have;
1141 }
1142 }
1143 }
1144 } else
1145 goto more;
1146 }
1147
1148
1149 /*
1150 * create a urb, and a buffer for it, and copy the data to the urb
1151 *
1152 */
1153 static ssize_t ftdi_elan_write(struct file *file,
1154 const char __user *user_buffer, size_t count,
1155 loff_t *ppos)
1156 {
1157 int retval = 0;
1158 struct urb *urb;
1159 char *buf;
1160 struct usb_ftdi *ftdi = file->private_data;
1161
1162 if (ftdi->disconnected > 0) {
1163 return -ENODEV;
1164 }
1165 if (count == 0) {
1166 goto exit;
1167 }
1168 urb = usb_alloc_urb(0, GFP_KERNEL);
1169 if (!urb) {
1170 retval = -ENOMEM;
1171 goto error_1;
1172 }
1173 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1174 &urb->transfer_dma);
1175 if (!buf) {
1176 retval = -ENOMEM;
1177 goto error_2;
1178 }
1179 if (copy_from_user(buf, user_buffer, count)) {
1180 retval = -EFAULT;
1181 goto error_3;
1182 }
1183 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1184 ftdi->bulk_out_endpointAddr), buf, count,
1185 ftdi_elan_write_bulk_callback, ftdi);
1186 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1187 retval = usb_submit_urb(urb, GFP_KERNEL);
1188 if (retval) {
1189 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1190 "d\n", retval);
1191 goto error_3;
1192 }
1193 usb_free_urb(urb);
1194
1195 exit:
1196 return count;
1197 error_3:
1198 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1199 error_2:
1200 usb_free_urb(urb);
1201 error_1:
1202 return retval;
1203 }
1204
1205 static struct file_operations ftdi_elan_fops = {
1206 .owner = THIS_MODULE,
1207 .llseek = no_llseek,
1208 .ioctl = ftdi_elan_ioctl,
1209 .read = ftdi_elan_read,
1210 .write = ftdi_elan_write,
1211 .open = ftdi_elan_open,
1212 .release = ftdi_elan_release,
1213 };
1214
1215 /*
1216 * usb class driver info in order to get a minor number from the usb core,
1217 * and to have the device registered with the driver core
1218 */
1219 static struct usb_class_driver ftdi_elan_jtag_class = {
1220 .name = "ftdi-%d-jtag",
1221 .fops = &ftdi_elan_fops,
1222 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1223 };
1224
1225 /*
1226 * the following definitions are for the
1227 * ELAN FPGA state machgine processor that
1228 * lies on the other side of the FTDI chip
1229 */
1230 #define cPCIu132rd 0x0
1231 #define cPCIu132wr 0x1
1232 #define cPCIiord 0x2
1233 #define cPCIiowr 0x3
1234 #define cPCImemrd 0x6
1235 #define cPCImemwr 0x7
1236 #define cPCIcfgrd 0xA
1237 #define cPCIcfgwr 0xB
1238 #define cPCInull 0xF
1239 #define cU132cmd_status 0x0
1240 #define cU132flash 0x1
1241 #define cPIDsetup 0x0
1242 #define cPIDout 0x1
1243 #define cPIDin 0x2
1244 #define cPIDinonce 0x3
1245 #define cCCnoerror 0x0
1246 #define cCCcrc 0x1
1247 #define cCCbitstuff 0x2
1248 #define cCCtoggle 0x3
1249 #define cCCstall 0x4
1250 #define cCCnoresp 0x5
1251 #define cCCbadpid1 0x6
1252 #define cCCbadpid2 0x7
1253 #define cCCdataoverrun 0x8
1254 #define cCCdataunderrun 0x9
1255 #define cCCbuffoverrun 0xC
1256 #define cCCbuffunderrun 0xD
1257 #define cCCnotaccessed 0xF
1258 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1259 {
1260 wait:if (ftdi->disconnected > 0) {
1261 return -ENODEV;
1262 } else {
1263 int command_size;
1264 down(&ftdi->u132_lock);
1265 command_size = ftdi->command_next - ftdi->command_head;
1266 if (command_size < COMMAND_SIZE) {
1267 struct u132_command *command = &ftdi->command[
1268 COMMAND_MASK & ftdi->command_next];
1269 command->header = 0x00 | cPCIu132wr;
1270 command->length = 0x04;
1271 command->address = 0x00;
1272 command->width = 0x00;
1273 command->follows = 4;
1274 command->value = data;
1275 command->buffer = &command->value;
1276 ftdi->command_next += 1;
1277 ftdi_elan_kick_command_queue(ftdi);
1278 up(&ftdi->u132_lock);
1279 return 0;
1280 } else {
1281 up(&ftdi->u132_lock);
1282 msleep(100);
1283 goto wait;
1284 }
1285 }
1286 }
1287
1288 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1289 u8 width, u32 data)
1290 {
1291 u8 addressofs = config_offset / 4;
1292 wait:if (ftdi->disconnected > 0) {
1293 return -ENODEV;
1294 } else {
1295 int command_size;
1296 down(&ftdi->u132_lock);
1297 command_size = ftdi->command_next - ftdi->command_head;
1298 if (command_size < COMMAND_SIZE) {
1299 struct u132_command *command = &ftdi->command[
1300 COMMAND_MASK & ftdi->command_next];
1301 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1302 command->length = 0x04;
1303 command->address = addressofs;
1304 command->width = 0x00 | (width & 0x0F);
1305 command->follows = 4;
1306 command->value = data;
1307 command->buffer = &command->value;
1308 ftdi->command_next += 1;
1309 ftdi_elan_kick_command_queue(ftdi);
1310 up(&ftdi->u132_lock);
1311 return 0;
1312 } else {
1313 up(&ftdi->u132_lock);
1314 msleep(100);
1315 goto wait;
1316 }
1317 }
1318 }
1319
1320 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1321 u8 width, u32 data)
1322 {
1323 u8 addressofs = mem_offset / 4;
1324 wait:if (ftdi->disconnected > 0) {
1325 return -ENODEV;
1326 } else {
1327 int command_size;
1328 down(&ftdi->u132_lock);
1329 command_size = ftdi->command_next - ftdi->command_head;
1330 if (command_size < COMMAND_SIZE) {
1331 struct u132_command *command = &ftdi->command[
1332 COMMAND_MASK & ftdi->command_next];
1333 command->header = 0x00 | (cPCImemwr & 0x0F);
1334 command->length = 0x04;
1335 command->address = addressofs;
1336 command->width = 0x00 | (width & 0x0F);
1337 command->follows = 4;
1338 command->value = data;
1339 command->buffer = &command->value;
1340 ftdi->command_next += 1;
1341 ftdi_elan_kick_command_queue(ftdi);
1342 up(&ftdi->u132_lock);
1343 return 0;
1344 } else {
1345 up(&ftdi->u132_lock);
1346 msleep(100);
1347 goto wait;
1348 }
1349 }
1350 }
1351
1352 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1353 u8 width, u32 data)
1354 {
1355 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1356 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1357 }
1358
1359
1360 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1361 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1362 {
1363 wait:if (ftdi->disconnected > 0) {
1364 return -ENODEV;
1365 } else {
1366 int command_size;
1367 int respond_size;
1368 down(&ftdi->u132_lock);
1369 command_size = ftdi->command_next - ftdi->command_head;
1370 respond_size = ftdi->respond_next - ftdi->respond_head;
1371 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1372 {
1373 struct u132_command *command = &ftdi->command[
1374 COMMAND_MASK & ftdi->command_next];
1375 struct u132_respond *respond = &ftdi->respond[
1376 RESPOND_MASK & ftdi->respond_next];
1377 int result = -ENODEV;
1378 respond->result = &result;
1379 respond->header = command->header = 0x00 | cPCIu132rd;
1380 command->length = 0x04;
1381 respond->address = command->address = cU132cmd_status;
1382 command->width = 0x00;
1383 command->follows = 0;
1384 command->value = 0;
1385 command->buffer = NULL;
1386 respond->value = data;
1387 init_completion(&respond->wait_completion);
1388 ftdi->command_next += 1;
1389 ftdi->respond_next += 1;
1390 ftdi_elan_kick_command_queue(ftdi);
1391 up(&ftdi->u132_lock);
1392 wait_for_completion(&respond->wait_completion);
1393 return result;
1394 } else {
1395 up(&ftdi->u132_lock);
1396 msleep(100);
1397 goto wait;
1398 }
1399 }
1400 }
1401
1402 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1403 u8 width, u32 *data)
1404 {
1405 u8 addressofs = config_offset / 4;
1406 wait:if (ftdi->disconnected > 0) {
1407 return -ENODEV;
1408 } else {
1409 int command_size;
1410 int respond_size;
1411 down(&ftdi->u132_lock);
1412 command_size = ftdi->command_next - ftdi->command_head;
1413 respond_size = ftdi->respond_next - ftdi->respond_head;
1414 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1415 {
1416 struct u132_command *command = &ftdi->command[
1417 COMMAND_MASK & ftdi->command_next];
1418 struct u132_respond *respond = &ftdi->respond[
1419 RESPOND_MASK & ftdi->respond_next];
1420 int result = -ENODEV;
1421 respond->result = &result;
1422 respond->header = command->header = 0x00 | (cPCIcfgrd &
1423 0x0F);
1424 command->length = 0x04;
1425 respond->address = command->address = addressofs;
1426 command->width = 0x00 | (width & 0x0F);
1427 command->follows = 0;
1428 command->value = 0;
1429 command->buffer = NULL;
1430 respond->value = data;
1431 init_completion(&respond->wait_completion);
1432 ftdi->command_next += 1;
1433 ftdi->respond_next += 1;
1434 ftdi_elan_kick_command_queue(ftdi);
1435 up(&ftdi->u132_lock);
1436 wait_for_completion(&respond->wait_completion);
1437 return result;
1438 } else {
1439 up(&ftdi->u132_lock);
1440 msleep(100);
1441 goto wait;
1442 }
1443 }
1444 }
1445
1446 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1447 u8 width, u32 *data)
1448 {
1449 u8 addressofs = mem_offset / 4;
1450 wait:if (ftdi->disconnected > 0) {
1451 return -ENODEV;
1452 } else {
1453 int command_size;
1454 int respond_size;
1455 down(&ftdi->u132_lock);
1456 command_size = ftdi->command_next - ftdi->command_head;
1457 respond_size = ftdi->respond_next - ftdi->respond_head;
1458 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1459 {
1460 struct u132_command *command = &ftdi->command[
1461 COMMAND_MASK & ftdi->command_next];
1462 struct u132_respond *respond = &ftdi->respond[
1463 RESPOND_MASK & ftdi->respond_next];
1464 int result = -ENODEV;
1465 respond->result = &result;
1466 respond->header = command->header = 0x00 | (cPCImemrd &
1467 0x0F);
1468 command->length = 0x04;
1469 respond->address = command->address = addressofs;
1470 command->width = 0x00 | (width & 0x0F);
1471 command->follows = 0;
1472 command->value = 0;
1473 command->buffer = NULL;
1474 respond->value = data;
1475 init_completion(&respond->wait_completion);
1476 ftdi->command_next += 1;
1477 ftdi->respond_next += 1;
1478 ftdi_elan_kick_command_queue(ftdi);
1479 up(&ftdi->u132_lock);
1480 wait_for_completion(&respond->wait_completion);
1481 return result;
1482 } else {
1483 up(&ftdi->u132_lock);
1484 msleep(100);
1485 goto wait;
1486 }
1487 }
1488 }
1489
1490 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1491 u8 width, u32 *data)
1492 {
1493 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1494 if (ftdi->initialized == 0) {
1495 return -ENODEV;
1496 } else
1497 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1498 }
1499
1500
1501 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1502 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1503 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1504 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1505 int toggle_bits, int error_count, int condition_code, int repeat_number,
1506 int halted, int skipped, int actual, int non_null))
1507 {
1508 u8 ed = ed_number - 1;
1509 wait:if (ftdi->disconnected > 0) {
1510 return -ENODEV;
1511 } else if (ftdi->initialized == 0) {
1512 return -ENODEV;
1513 } else {
1514 int command_size;
1515 down(&ftdi->u132_lock);
1516 command_size = ftdi->command_next - ftdi->command_head;
1517 if (command_size < COMMAND_SIZE) {
1518 struct u132_target *target = &ftdi->target[ed];
1519 struct u132_command *command = &ftdi->command[
1520 COMMAND_MASK & ftdi->command_next];
1521 command->header = 0x80 | (ed << 5);
1522 command->length = 0x8007;
1523 command->address = (toggle_bits << 6) | (ep_number << 2)
1524 | (address << 0);
1525 command->width = usb_maxpacket(urb->dev, urb->pipe,
1526 usb_pipeout(urb->pipe));
1527 command->follows = 8;
1528 command->value = 0;
1529 command->buffer = urb->setup_packet;
1530 target->callback = callback;
1531 target->endp = endp;
1532 target->urb = urb;
1533 target->active = 1;
1534 ftdi->command_next += 1;
1535 ftdi_elan_kick_command_queue(ftdi);
1536 up(&ftdi->u132_lock);
1537 return 0;
1538 } else {
1539 up(&ftdi->u132_lock);
1540 msleep(100);
1541 goto wait;
1542 }
1543 }
1544 }
1545
1546 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1547 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1548 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1549 int toggle_bits, int error_count, int condition_code, int repeat_number,
1550 int halted, int skipped, int actual, int non_null))
1551 {
1552 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1553 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1554 ep_number, toggle_bits, callback);
1555 }
1556
1557
1558 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1559 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1560 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1561 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1562 int toggle_bits, int error_count, int condition_code, int repeat_number,
1563 int halted, int skipped, int actual, int non_null))
1564 {
1565 u8 ed = ed_number - 1;
1566 wait:if (ftdi->disconnected > 0) {
1567 return -ENODEV;
1568 } else if (ftdi->initialized == 0) {
1569 return -ENODEV;
1570 } else {
1571 int command_size;
1572 down(&ftdi->u132_lock);
1573 command_size = ftdi->command_next - ftdi->command_head;
1574 if (command_size < COMMAND_SIZE) {
1575 struct u132_target *target = &ftdi->target[ed];
1576 struct u132_command *command = &ftdi->command[
1577 COMMAND_MASK & ftdi->command_next];
1578 int remaining_length = urb->transfer_buffer_length -
1579 urb->actual_length;
1580 command->header = 0x82 | (ed << 5);
1581 if (remaining_length == 0) {
1582 command->length = 0x0000;
1583 } else if (remaining_length > 1024) {
1584 command->length = 0x8000 | 1023;
1585 } else
1586 command->length = 0x8000 | (remaining_length -
1587 1);
1588 command->address = (toggle_bits << 6) | (ep_number << 2)
1589 | (address << 0);
1590 command->width = usb_maxpacket(urb->dev, urb->pipe,
1591 usb_pipeout(urb->pipe));
1592 command->follows = 0;
1593 command->value = 0;
1594 command->buffer = NULL;
1595 target->callback = callback;
1596 target->endp = endp;
1597 target->urb = urb;
1598 target->active = 1;
1599 ftdi->command_next += 1;
1600 ftdi_elan_kick_command_queue(ftdi);
1601 up(&ftdi->u132_lock);
1602 return 0;
1603 } else {
1604 up(&ftdi->u132_lock);
1605 msleep(100);
1606 goto wait;
1607 }
1608 }
1609 }
1610
1611 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1612 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1613 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1614 int toggle_bits, int error_count, int condition_code, int repeat_number,
1615 int halted, int skipped, int actual, int non_null))
1616 {
1617 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1618 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1619 ep_number, toggle_bits, callback);
1620 }
1621
1622
1623 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1624 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1625 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1626 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1627 int toggle_bits, int error_count, int condition_code, int repeat_number,
1628 int halted, int skipped, int actual, int non_null))
1629 {
1630 u8 ed = ed_number - 1;
1631 wait:if (ftdi->disconnected > 0) {
1632 return -ENODEV;
1633 } else if (ftdi->initialized == 0) {
1634 return -ENODEV;
1635 } else {
1636 int command_size;
1637 down(&ftdi->u132_lock);
1638 command_size = ftdi->command_next - ftdi->command_head;
1639 if (command_size < COMMAND_SIZE) {
1640 struct u132_target *target = &ftdi->target[ed];
1641 struct u132_command *command = &ftdi->command[
1642 COMMAND_MASK & ftdi->command_next];
1643 command->header = 0x81 | (ed << 5);
1644 command->length = 0x0000;
1645 command->address = (toggle_bits << 6) | (ep_number << 2)
1646 | (address << 0);
1647 command->width = usb_maxpacket(urb->dev, urb->pipe,
1648 usb_pipeout(urb->pipe));
1649 command->follows = 0;
1650 command->value = 0;
1651 command->buffer = NULL;
1652 target->callback = callback;
1653 target->endp = endp;
1654 target->urb = urb;
1655 target->active = 1;
1656 ftdi->command_next += 1;
1657 ftdi_elan_kick_command_queue(ftdi);
1658 up(&ftdi->u132_lock);
1659 return 0;
1660 } else {
1661 up(&ftdi->u132_lock);
1662 msleep(100);
1663 goto wait;
1664 }
1665 }
1666 }
1667
1668 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1669 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1670 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1671 int toggle_bits, int error_count, int condition_code, int repeat_number,
1672 int halted, int skipped, int actual, int non_null))
1673 {
1674 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1675 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1676 ep_number, toggle_bits, callback);
1677 }
1678
1679
1680 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1681 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1682 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1683 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1684 int toggle_bits, int error_count, int condition_code, int repeat_number,
1685 int halted, int skipped, int actual, int non_null))
1686 {
1687 u8 ed = ed_number - 1;
1688 wait:if (ftdi->disconnected > 0) {
1689 return -ENODEV;
1690 } else if (ftdi->initialized == 0) {
1691 return -ENODEV;
1692 } else {
1693 int command_size;
1694 down(&ftdi->u132_lock);
1695 command_size = ftdi->command_next - ftdi->command_head;
1696 if (command_size < COMMAND_SIZE) {
1697 u8 *b;
1698 u16 urb_size;
1699 int i = 0;
1700 char data[30 *3 + 4];
1701 char *d = data;
1702 int m = (sizeof(data) - 1) / 3;
1703 int l = 0;
1704 struct u132_target *target = &ftdi->target[ed];
1705 struct u132_command *command = &ftdi->command[
1706 COMMAND_MASK & ftdi->command_next];
1707 command->header = 0x81 | (ed << 5);
1708 command->address = (toggle_bits << 6) | (ep_number << 2)
1709 | (address << 0);
1710 command->width = usb_maxpacket(urb->dev, urb->pipe,
1711 usb_pipeout(urb->pipe));
1712 command->follows = min(1024,
1713 urb->transfer_buffer_length -
1714 urb->actual_length);
1715 command->value = 0;
1716 command->buffer = urb->transfer_buffer +
1717 urb->actual_length;
1718 command->length = 0x8000 | (command->follows - 1);
1719 b = command->buffer;
1720 urb_size = command->follows;
1721 data[0] = 0;
1722 while (urb_size-- > 0) {
1723 if (i > m) {
1724 } else if (i++ < m) {
1725 int w = sprintf(d, " %02X", *b++);
1726 d += w;
1727 l += w;
1728 } else
1729 d += sprintf(d, " ..");
1730 }
1731 target->callback = callback;
1732 target->endp = endp;
1733 target->urb = urb;
1734 target->active = 1;
1735 ftdi->command_next += 1;
1736 ftdi_elan_kick_command_queue(ftdi);
1737 up(&ftdi->u132_lock);
1738 return 0;
1739 } else {
1740 up(&ftdi->u132_lock);
1741 msleep(100);
1742 goto wait;
1743 }
1744 }
1745 }
1746
1747 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1748 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1749 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1750 int toggle_bits, int error_count, int condition_code, int repeat_number,
1751 int halted, int skipped, int actual, int non_null))
1752 {
1753 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1754 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1755 ep_number, toggle_bits, callback);
1756 }
1757
1758
1759 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1760 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1761 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1762 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1763 int toggle_bits, int error_count, int condition_code, int repeat_number,
1764 int halted, int skipped, int actual, int non_null))
1765 {
1766 u8 ed = ed_number - 1;
1767 wait:if (ftdi->disconnected > 0) {
1768 return -ENODEV;
1769 } else if (ftdi->initialized == 0) {
1770 return -ENODEV;
1771 } else {
1772 int command_size;
1773 down(&ftdi->u132_lock);
1774 command_size = ftdi->command_next - ftdi->command_head;
1775 if (command_size < COMMAND_SIZE) {
1776 int remaining_length = urb->transfer_buffer_length -
1777 urb->actual_length;
1778 struct u132_target *target = &ftdi->target[ed];
1779 struct u132_command *command = &ftdi->command[
1780 COMMAND_MASK & ftdi->command_next];
1781 command->header = 0x83 | (ed << 5);
1782 if (remaining_length == 0) {
1783 command->length = 0x0000;
1784 } else if (remaining_length > 1024) {
1785 command->length = 0x8000 | 1023;
1786 } else
1787 command->length = 0x8000 | (remaining_length -
1788 1);
1789 command->address = (toggle_bits << 6) | (ep_number << 2)
1790 | (address << 0);
1791 command->width = usb_maxpacket(urb->dev, urb->pipe,
1792 usb_pipeout(urb->pipe));
1793 command->follows = 0;
1794 command->value = 0;
1795 command->buffer = NULL;
1796 target->callback = callback;
1797 target->endp = endp;
1798 target->urb = urb;
1799 target->active = 1;
1800 ftdi->command_next += 1;
1801 ftdi_elan_kick_command_queue(ftdi);
1802 up(&ftdi->u132_lock);
1803 return 0;
1804 } else {
1805 up(&ftdi->u132_lock);
1806 msleep(100);
1807 goto wait;
1808 }
1809 }
1810 }
1811
1812 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1813 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1814 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1815 int toggle_bits, int error_count, int condition_code, int repeat_number,
1816 int halted, int skipped, int actual, int non_null))
1817 {
1818 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1819 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1820 ep_number, toggle_bits, callback);
1821 }
1822
1823
1824 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1825 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1826 void *endp)
1827 {
1828 u8 ed = ed_number - 1;
1829 if (ftdi->disconnected > 0) {
1830 return -ENODEV;
1831 } else if (ftdi->initialized == 0) {
1832 return -ENODEV;
1833 } else {
1834 struct u132_target *target = &ftdi->target[ed];
1835 down(&ftdi->u132_lock);
1836 if (target->abandoning > 0) {
1837 up(&ftdi->u132_lock);
1838 return 0;
1839 } else {
1840 target->abandoning = 1;
1841 wait_1:if (target->active == 1) {
1842 int command_size = ftdi->command_next -
1843 ftdi->command_head;
1844 if (command_size < COMMAND_SIZE) {
1845 struct u132_command *command =
1846 &ftdi->command[COMMAND_MASK &
1847 ftdi->command_next];
1848 command->header = 0x80 | (ed << 5) |
1849 0x4;
1850 command->length = 0x00;
1851 command->address = 0x00;
1852 command->width = 0x00;
1853 command->follows = 0;
1854 command->value = 0;
1855 command->buffer = &command->value;
1856 ftdi->command_next += 1;
1857 ftdi_elan_kick_command_queue(ftdi);
1858 } else {
1859 up(&ftdi->u132_lock);
1860 msleep(100);
1861 down(&ftdi->u132_lock);
1862 goto wait_1;
1863 }
1864 }
1865 up(&ftdi->u132_lock);
1866 return 0;
1867 }
1868 }
1869 }
1870
1871 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1872 void *endp)
1873 {
1874 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1875 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1876 }
1877
1878
1879 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1880 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1881 {
1882 int retry_on_empty = 10;
1883 int retry_on_timeout = 5;
1884 int retry_on_status = 20;
1885 more:{
1886 int packet_bytes = 0;
1887 int retval = usb_bulk_msg(ftdi->udev,
1888 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1889 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1890 &packet_bytes, msecs_to_jiffies(100));
1891 if (packet_bytes > 2) {
1892 char diag[30 *3 + 4];
1893 char *d = diag;
1894 int m = (sizeof(diag) - 1) / 3;
1895 char *b = ftdi->bulk_in_buffer;
1896 int bytes_read = 0;
1897 diag[0] = 0;
1898 while (packet_bytes-- > 0) {
1899 char c = *b++;
1900 if (bytes_read < m) {
1901 d += sprintf(d, " %02X",
1902 0x000000FF & c);
1903 } else if (bytes_read > m) {
1904 } else
1905 d += sprintf(d, " ..");
1906 bytes_read += 1;
1907 continue;
1908 }
1909 goto more;
1910 } else if (packet_bytes > 1) {
1911 char s1 = ftdi->bulk_in_buffer[0];
1912 char s2 = ftdi->bulk_in_buffer[1];
1913 if (s1 == 0x31 && s2 == 0x60) {
1914 return 0;
1915 } else if (retry_on_status-- > 0) {
1916 goto more;
1917 } else {
1918 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1919 "imit reached\n");
1920 return -EFAULT;
1921 }
1922 } else if (packet_bytes > 0) {
1923 char b1 = ftdi->bulk_in_buffer[0];
1924 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1925 "TDI = %02X\n", b1);
1926 if (retry_on_status-- > 0) {
1927 goto more;
1928 } else {
1929 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1930 "imit reached\n");
1931 return -EFAULT;
1932 }
1933 } else if (retval == -ETIMEDOUT) {
1934 if (retry_on_timeout-- > 0) {
1935 goto more;
1936 } else {
1937 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1938 "t reached\n");
1939 return -ENOMEM;
1940 }
1941 } else if (retval == 0) {
1942 if (retry_on_empty-- > 0) {
1943 goto more;
1944 } else {
1945 dev_err(&ftdi->udev->dev, "empty packet retry l"
1946 "imit reached\n");
1947 return -ENOMEM;
1948 }
1949 } else {
1950 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1951 return retval;
1952 }
1953 }
1954 return -1;
1955 }
1956
1957
1958 /*
1959 * send the long flush sequence
1960 *
1961 */
1962 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1963 {
1964 int retval;
1965 struct urb *urb;
1966 char *buf;
1967 int I = 257;
1968 int i = 0;
1969 urb = usb_alloc_urb(0, GFP_KERNEL);
1970 if (!urb) {
1971 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1972 "ence\n");
1973 return -ENOMEM;
1974 }
1975 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1976 if (!buf) {
1977 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
1978 "uence\n");
1979 usb_free_urb(urb);
1980 return -ENOMEM;
1981 }
1982 while (I-- > 0)
1983 buf[i++] = 0x55;
1984 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1985 ftdi->bulk_out_endpointAddr), buf, i,
1986 ftdi_elan_write_bulk_callback, ftdi);
1987 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1988 retval = usb_submit_urb(urb, GFP_KERNEL);
1989 if (retval) {
1990 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
1991 "flush sequence\n");
1992 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
1993 usb_free_urb(urb);
1994 return -ENOMEM;
1995 }
1996 usb_free_urb(urb);
1997 return 0;
1998 }
1999
2000
2001 /*
2002 * send the reset sequence
2003 *
2004 */
2005 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2006 {
2007 int retval;
2008 struct urb *urb;
2009 char *buf;
2010 int I = 4;
2011 int i = 0;
2012 urb = usb_alloc_urb(0, GFP_KERNEL);
2013 if (!urb) {
2014 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2015 "quence\n");
2016 return -ENOMEM;
2017 }
2018 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2019 if (!buf) {
2020 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2021 " sequence\n");
2022 usb_free_urb(urb);
2023 return -ENOMEM;
2024 }
2025 buf[i++] = 0x55;
2026 buf[i++] = 0xAA;
2027 buf[i++] = 0x5A;
2028 buf[i++] = 0xA5;
2029 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2030 ftdi->bulk_out_endpointAddr), buf, i,
2031 ftdi_elan_write_bulk_callback, ftdi);
2032 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2033 retval = usb_submit_urb(urb, GFP_KERNEL);
2034 if (retval) {
2035 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2036 "reset sequence\n");
2037 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2038 usb_free_urb(urb);
2039 return -ENOMEM;
2040 }
2041 usb_free_urb(urb);
2042 return 0;
2043 }
2044
2045 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2046 {
2047 int retval;
2048 int long_stop = 10;
2049 int retry_on_timeout = 5;
2050 int retry_on_empty = 10;
2051 int err_count = 0;
2052 retval = ftdi_elan_flush_input_fifo(ftdi);
2053 if (retval)
2054 return retval;
2055 ftdi->bulk_in_left = 0;
2056 ftdi->bulk_in_last = -1;
2057 while (long_stop-- > 0) {
2058 int read_stop;
2059 int read_stuck;
2060 retval = ftdi_elan_synchronize_flush(ftdi);
2061 if (retval)
2062 return retval;
2063 retval = ftdi_elan_flush_input_fifo(ftdi);
2064 if (retval)
2065 return retval;
2066 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2067 if (retval)
2068 return retval;
2069 read_stop = 100;
2070 read_stuck = 10;
2071 read:{
2072 int packet_bytes = 0;
2073 retval = usb_bulk_msg(ftdi->udev,
2074 usb_rcvbulkpipe(ftdi->udev,
2075 ftdi->bulk_in_endpointAddr),
2076 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2077 &packet_bytes, msecs_to_jiffies(500));
2078 if (packet_bytes > 2) {
2079 char diag[30 *3 + 4];
2080 char *d = diag;
2081 int m = (sizeof(diag) - 1) / 3;
2082 char *b = ftdi->bulk_in_buffer;
2083 int bytes_read = 0;
2084 unsigned char c = 0;
2085 diag[0] = 0;
2086 while (packet_bytes-- > 0) {
2087 c = *b++;
2088 if (bytes_read < m) {
2089 d += sprintf(d, " %02X", c);
2090 } else if (bytes_read > m) {
2091 } else
2092 d += sprintf(d, " ..");
2093 bytes_read += 1;
2094 continue;
2095 }
2096 if (c == 0x7E) {
2097 return 0;
2098 } else {
2099 if (c == 0x55) {
2100 goto read;
2101 } else if (read_stop-- > 0) {
2102 goto read;
2103 } else {
2104 dev_err(&ftdi->udev->dev, "retr"
2105 "y limit reached\n");
2106 continue;
2107 }
2108 }
2109 } else if (packet_bytes > 1) {
2110 unsigned char s1 = ftdi->bulk_in_buffer[0];
2111 unsigned char s2 = ftdi->bulk_in_buffer[1];
2112 if (s1 == 0x31 && s2 == 0x00) {
2113 if (read_stuck-- > 0) {
2114 goto read;
2115 } else
2116 goto reset;
2117 } else if (s1 == 0x31 && s2 == 0x60) {
2118 if (read_stop-- > 0) {
2119 goto read;
2120 } else {
2121 dev_err(&ftdi->udev->dev, "retr"
2122 "y limit reached\n");
2123 continue;
2124 }
2125 } else {
2126 if (read_stop-- > 0) {
2127 goto read;
2128 } else {
2129 dev_err(&ftdi->udev->dev, "retr"
2130 "y limit reached\n");
2131 continue;
2132 }
2133 }
2134 } else if (packet_bytes > 0) {
2135 if (read_stop-- > 0) {
2136 goto read;
2137 } else {
2138 dev_err(&ftdi->udev->dev, "retry limit "
2139 "reached\n");
2140 continue;
2141 }
2142 } else if (retval == -ETIMEDOUT) {
2143 if (retry_on_timeout-- > 0) {
2144 goto read;
2145 } else {
2146 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2147 "try limit reached\n");
2148 continue;
2149 }
2150 } else if (retval == 0) {
2151 if (retry_on_empty-- > 0) {
2152 goto read;
2153 } else {
2154 dev_err(&ftdi->udev->dev, "empty packet"
2155 " retry limit reached\n");
2156 continue;
2157 }
2158 } else {
2159 err_count += 1;
2160 dev_err(&ftdi->udev->dev, "error = %d\n",
2161 retval);
2162 if (read_stop-- > 0) {
2163 goto read;
2164 } else {
2165 dev_err(&ftdi->udev->dev, "retry limit "
2166 "reached\n");
2167 continue;
2168 }
2169 }
2170 }
2171 }
2172 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2173 return -EFAULT;
2174 }
2175
2176 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2177 {
2178 int retry_on_empty = 10;
2179 int retry_on_timeout = 5;
2180 int retry_on_status = 50;
2181 more:{
2182 int packet_bytes = 0;
2183 int retval = usb_bulk_msg(ftdi->udev,
2184 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2185 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2186 &packet_bytes, msecs_to_jiffies(1000));
2187 if (packet_bytes > 2) {
2188 char diag[30 *3 + 4];
2189 char *d = diag;
2190 int m = (sizeof(diag) - 1) / 3;
2191 char *b = ftdi->bulk_in_buffer;
2192 int bytes_read = 0;
2193 diag[0] = 0;
2194 while (packet_bytes-- > 0) {
2195 char c = *b++;
2196 if (bytes_read < m) {
2197 d += sprintf(d, " %02X",
2198 0x000000FF & c);
2199 } else if (bytes_read > m) {
2200 } else
2201 d += sprintf(d, " ..");
2202 bytes_read += 1;
2203 continue;
2204 }
2205 goto more;
2206 } else if (packet_bytes > 1) {
2207 char s1 = ftdi->bulk_in_buffer[0];
2208 char s2 = ftdi->bulk_in_buffer[1];
2209 if (s1 == 0x31 && s2 == 0x60) {
2210 return 0;
2211 } else if (retry_on_status-- > 0) {
2212 msleep(5);
2213 goto more;
2214 } else
2215 return -EFAULT;
2216 } else if (packet_bytes > 0) {
2217 char b1 = ftdi->bulk_in_buffer[0];
2218 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2219 "TDI = %02X\n", b1);
2220 if (retry_on_status-- > 0) {
2221 msleep(5);
2222 goto more;
2223 } else {
2224 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2225 "imit reached\n");
2226 return -EFAULT;
2227 }
2228 } else if (retval == -ETIMEDOUT) {
2229 if (retry_on_timeout-- > 0) {
2230 goto more;
2231 } else {
2232 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2233 "t reached\n");
2234 return -ENOMEM;
2235 }
2236 } else if (retval == 0) {
2237 if (retry_on_empty-- > 0) {
2238 goto more;
2239 } else {
2240 dev_err(&ftdi->udev->dev, "empty packet retry l"
2241 "imit reached\n");
2242 return -ENOMEM;
2243 }
2244 } else {
2245 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2246 return -ENOMEM;
2247 }
2248 }
2249 return -1;
2250 }
2251
2252 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2253 {
2254 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2255 if (UxxxStatus)
2256 return UxxxStatus;
2257 if (ftdi->controlreg & 0x00400000) {
2258 if (ftdi->card_ejected) {
2259 } else {
2260 ftdi->card_ejected = 1;
2261 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2262 "%08X\n", ftdi->controlreg);
2263 }
2264 return -ENODEV;
2265 } else {
2266 u8 fn = ftdi->function - 1;
2267 int activePCIfn = fn << 8;
2268 u32 pcidata;
2269 u32 pciVID;
2270 u32 pciPID;
2271 int reg = 0;
2272 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2273 &pcidata);
2274 if (UxxxStatus)
2275 return UxxxStatus;
2276 pciVID = pcidata & 0xFFFF;
2277 pciPID = (pcidata >> 16) & 0xFFFF;
2278 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2279 ftdi->platform_data.device) {
2280 return 0;
2281 } else {
2282 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2283 "ce=%04X pciPID=%04X\n",
2284 ftdi->platform_data.vendor, pciVID,
2285 ftdi->platform_data.device, pciPID);
2286 return -ENODEV;
2287 }
2288 }
2289 }
2290
2291 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2292 {
2293 u32 latence_timer;
2294 u32 controlreg;
2295 int UxxxStatus;
2296 u32 pcidata;
2297 int reg = 0;
2298 int foundOHCI = 0;
2299 u8 fn;
2300 int activePCIfn = 0;
2301 u32 pciVID = 0;
2302 u32 pciPID = 0;
2303 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2304 if (UxxxStatus)
2305 return UxxxStatus;
2306 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2307 if (UxxxStatus)
2308 return UxxxStatus;
2309 msleep(750);
2310 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2311 if (UxxxStatus)
2312 return UxxxStatus;
2313 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2314 if (UxxxStatus)
2315 return UxxxStatus;
2316 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2317 if (UxxxStatus)
2318 return UxxxStatus;
2319 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2320 if (UxxxStatus)
2321 return UxxxStatus;
2322 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2323 if (UxxxStatus)
2324 return UxxxStatus;
2325 msleep(250);
2326 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2327 if (UxxxStatus)
2328 return UxxxStatus;
2329 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2330 if (UxxxStatus)
2331 return UxxxStatus;
2332 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2333 if (UxxxStatus)
2334 return UxxxStatus;
2335 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2336 if (UxxxStatus)
2337 return UxxxStatus;
2338 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2339 if (UxxxStatus)
2340 return UxxxStatus;
2341 msleep(1000);
2342 for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
2343 activePCIfn = fn << 8;
2344 ftdi->function = fn + 1;
2345 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2346 &pcidata);
2347 if (UxxxStatus)
2348 return UxxxStatus;
2349 pciVID = pcidata & 0xFFFF;
2350 pciPID = (pcidata >> 16) & 0xFFFF;
2351 if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
2352 foundOHCI = 1;
2353 } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
2354 foundOHCI = 1;
2355 } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
2356 foundOHCI = 1;
2357 } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
2358 foundOHCI = 1;
2359 } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
2360 }
2361 }
2362 if (foundOHCI == 0) {
2363 return -ENXIO;
2364 }
2365 ftdi->platform_data.vendor = pciVID;
2366 ftdi->platform_data.device = pciPID;
2367 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2368 if (UxxxStatus)
2369 return UxxxStatus;
2370 reg = 16;
2371 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2372 0xFFFFFFFF);
2373 if (UxxxStatus)
2374 return UxxxStatus;
2375 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2376 &pcidata);
2377 if (UxxxStatus)
2378 return UxxxStatus;
2379 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2380 0xF0000000);
2381 if (UxxxStatus)
2382 return UxxxStatus;
2383 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2384 &pcidata);
2385 if (UxxxStatus)
2386 return UxxxStatus;
2387 reg = 12;
2388 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2389 &latence_timer);
2390 if (UxxxStatus)
2391 return UxxxStatus;
2392 latence_timer &= 0xFFFF00FF;
2393 latence_timer |= 0x00001600;
2394 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2395 latence_timer);
2396 if (UxxxStatus)
2397 return UxxxStatus;
2398 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2399 &pcidata);
2400 if (UxxxStatus)
2401 return UxxxStatus;
2402 reg = 4;
2403 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2404 0x06);
2405 if (UxxxStatus)
2406 return UxxxStatus;
2407 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2408 &pcidata);
2409 if (UxxxStatus)
2410 return UxxxStatus;
2411 return 0;
2412 }
2413
2414 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2415 {
2416 u32 pcidata;
2417 int U132Status;
2418 int reg;
2419 int reset_repeat = 0;
2420 do_reset:reg = 8;
2421 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
2422 if (U132Status)
2423 return U132Status;
2424 reset_check:{
2425 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2426 if (U132Status)
2427 return U132Status;
2428 if (pcidata & 1) {
2429 msleep(500);
2430 if (reset_repeat++ > 100) {
2431 reset_repeat = 0;
2432 goto do_reset;
2433 } else
2434 goto reset_check;
2435 }
2436 }
2437 goto dump_regs;
2438 msleep(500);
2439 reg = 0x28;
2440 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
2441 if (U132Status)
2442 return U132Status;
2443 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2444 if (U132Status)
2445 return U132Status;
2446 reg = 0x40;
2447 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
2448 if (U132Status)
2449 return U132Status;
2450 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2451 if (U132Status)
2452 return U132Status;
2453 reg = 0x34;
2454 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
2455 if (U132Status)
2456 return U132Status;
2457 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2458 if (U132Status)
2459 return U132Status;
2460 reg = 4;
2461 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
2462 if (U132Status)
2463 return U132Status;
2464 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2465 if (U132Status)
2466 return U132Status;
2467 msleep(250);
2468 reg = 8;
2469 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
2470 if (U132Status)
2471 return U132Status;
2472 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2473 if (U132Status)
2474 return U132Status;
2475 reg = 0x28;
2476 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2477 if (U132Status)
2478 return U132Status;
2479 reg = 8;
2480 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2481 if (U132Status)
2482 return U132Status;
2483 reg = 0x48;
2484 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
2485 if (U132Status)
2486 return U132Status;
2487 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2488 if (U132Status)
2489 return U132Status;
2490 reg = 0x54;
2491 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2492 if (U132Status)
2493 return U132Status;
2494 reg = 0x58;
2495 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2496 if (U132Status)
2497 return U132Status;
2498 reg = 0x34;
2499 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
2500 if (U132Status)
2501 return U132Status;
2502 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2503 if (U132Status)
2504 return U132Status;
2505 msleep(100);
2506 reg = 0x50;
2507 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
2508 if (U132Status)
2509 return U132Status;
2510 reg = 0x54;
2511 power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2512 if (U132Status)
2513 return U132Status;
2514 if (!(pcidata & 1)) {
2515 msleep(500);
2516 goto power_check;
2517 }
2518 msleep(3000);
2519 reg = 0x54;
2520 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2521 if (U132Status)
2522 return U132Status;
2523 reg = 0x58;
2524 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2525 if (U132Status)
2526 return U132Status;
2527 reg = 0x54;
2528 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2529 if (U132Status)
2530 return U132Status;
2531 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2532 if (U132Status)
2533 return U132Status;
2534 reg = 0x54;
2535 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
2536 if (U132Status)
2537 return U132Status;
2538 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2539 if (U132Status)
2540 return U132Status;
2541 msleep(750);
2542 reg = 0x54;
2543 if (0) {
2544 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2545 if (U132Status)
2546 return U132Status;
2547 }
2548 if (0) {
2549 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2550 if (U132Status)
2551 return U132Status;
2552 }
2553 reg = 0x54;
2554 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2555 if (U132Status)
2556 return U132Status;
2557 reg = 0x58;
2558 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2559 if (U132Status)
2560 return U132Status;
2561 dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
2562 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2563 if (U132Status)
2564 return U132Status;
2565 }
2566 return 0;
2567 }
2568
2569
2570 /*
2571 * we use only the first bulk-in and bulk-out endpoints
2572 */
2573 static int ftdi_elan_probe(struct usb_interface *interface,
2574 const struct usb_device_id *id)
2575 {
2576 struct usb_host_interface *iface_desc;
2577 struct usb_endpoint_descriptor *endpoint;
2578 size_t buffer_size;
2579 int i;
2580 int retval = -ENOMEM;
2581 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2582 if (ftdi == NULL) {
2583 printk(KERN_ERR "Out of memory\n");
2584 return -ENOMEM;
2585 }
2586 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2587 down(&ftdi_module_lock);
2588 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2589 ftdi->sequence_num = ++ftdi_instances;
2590 up(&ftdi_module_lock);
2591 ftdi_elan_init_kref(ftdi);
2592 init_MUTEX(&ftdi->sw_lock);
2593 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2594 ftdi->interface = interface;
2595 init_MUTEX(&ftdi->u132_lock);
2596 ftdi->expected = 4;
2597 iface_desc = interface->cur_altsetting;
2598 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2599 endpoint = &iface_desc->endpoint[i].desc;
2600 if (!ftdi->bulk_in_endpointAddr &&
2601 usb_endpoint_is_bulk_in(endpoint)) {
2602 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2603 ftdi->bulk_in_size = buffer_size;
2604 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2605 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2606 if (!ftdi->bulk_in_buffer) {
2607 dev_err(&ftdi->udev->dev, "Could not allocate b"
2608 "ulk_in_buffer\n");
2609 retval = -ENOMEM;
2610 goto error;
2611 }
2612 }
2613 if (!ftdi->bulk_out_endpointAddr &&
2614 usb_endpoint_is_bulk_out(endpoint)) {
2615 ftdi->bulk_out_endpointAddr =
2616 endpoint->bEndpointAddress;
2617 }
2618 }
2619 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2620 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2621 "-out endpoints\n");
2622 retval = -ENODEV;
2623 goto error;
2624 }
2625 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2626 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2627 ftdi->bulk_out_endpointAddr);
2628 usb_set_intfdata(interface, ftdi);
2629 if (iface_desc->desc.bInterfaceNumber == 0 &&
2630 ftdi->bulk_in_endpointAddr == 0x81 &&
2631 ftdi->bulk_out_endpointAddr == 0x02) {
2632 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2633 if (retval) {
2634 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2635 "this device.\n");
2636 usb_set_intfdata(interface, NULL);
2637 retval = -ENOMEM;
2638 goto error;
2639 } else {
2640 ftdi->class = &ftdi_elan_jtag_class;
2641 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2642 "%d now attached to ftdi%d\n", ftdi,
2643 iface_desc->desc.bInterfaceNumber,
2644 interface->minor);
2645 return 0;
2646 }
2647 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2648 ftdi->bulk_in_endpointAddr == 0x83 &&
2649 ftdi->bulk_out_endpointAddr == 0x04) {
2650 ftdi->class = NULL;
2651 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2652 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2653 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2654 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2655 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2656 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2657 return 0;
2658 } else {
2659 dev_err(&ftdi->udev->dev,
2660 "Could not find ELAN's U132 device\n");
2661 retval = -ENODEV;
2662 goto error;
2663 }
2664 error:if (ftdi) {
2665 ftdi_elan_put_kref(ftdi);
2666 }
2667 return retval;
2668 }
2669
2670 static void ftdi_elan_disconnect(struct usb_interface *interface)
2671 {
2672 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2673 ftdi->disconnected += 1;
2674 if (ftdi->class) {
2675 int minor = interface->minor;
2676 struct usb_class_driver *class = ftdi->class;
2677 usb_set_intfdata(interface, NULL);
2678 usb_deregister_dev(interface, class);
2679 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2680 "or %d now disconnected\n", minor);
2681 } else {
2682 ftdi_status_cancel_work(ftdi);
2683 ftdi_command_cancel_work(ftdi);
2684 ftdi_response_cancel_work(ftdi);
2685 ftdi_elan_abandon_completions(ftdi);
2686 ftdi_elan_abandon_targets(ftdi);
2687 if (ftdi->registered) {
2688 platform_device_unregister(&ftdi->platform_dev);
2689 ftdi->synchronized = 0;
2690 ftdi->enumerated = 0;
2691 ftdi->registered = 0;
2692 }
2693 flush_workqueue(status_queue);
2694 flush_workqueue(command_queue);
2695 flush_workqueue(respond_queue);
2696 ftdi->disconnected += 1;
2697 usb_set_intfdata(interface, NULL);
2698 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2699 "face now disconnected\n");
2700 }
2701 ftdi_elan_put_kref(ftdi);
2702 }
2703
2704 static struct usb_driver ftdi_elan_driver = {
2705 .name = "ftdi-elan",
2706 .probe = ftdi_elan_probe,
2707 .disconnect = ftdi_elan_disconnect,
2708 .id_table = ftdi_elan_table,
2709 };
2710 static int __init ftdi_elan_init(void)
2711 {
2712 int result;
2713 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2714 __TIME__, __DATE__);
2715 init_MUTEX(&ftdi_module_lock);
2716 INIT_LIST_HEAD(&ftdi_static_list);
2717 status_queue = create_singlethread_workqueue("ftdi-status-control");
2718 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2719 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2720 result = usb_register(&ftdi_elan_driver);
2721 if (result)
2722 printk(KERN_ERR "usb_register failed. Error number %d\n",
2723 result);
2724 return result;
2725 }
2726
2727 static void __exit ftdi_elan_exit(void)
2728 {
2729 struct usb_ftdi *ftdi;
2730 struct usb_ftdi *temp;
2731 usb_deregister(&ftdi_elan_driver);
2732 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2733 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2734 ftdi_status_cancel_work(ftdi);
2735 ftdi_command_cancel_work(ftdi);
2736 ftdi_response_cancel_work(ftdi);
2737 } flush_workqueue(status_queue);
2738 destroy_workqueue(status_queue);
2739 status_queue = NULL;
2740 flush_workqueue(command_queue);
2741 destroy_workqueue(command_queue);
2742 command_queue = NULL;
2743 flush_workqueue(respond_queue);
2744 destroy_workqueue(respond_queue);
2745 respond_queue = NULL;
2746 }
2747
2748
2749 module_init(ftdi_elan_init);
2750 module_exit(ftdi_elan_exit);
This page took 0.099379 seconds and 5 git commands to generate.