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