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