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"
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
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
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 {
111 #define RESPOND_BITS 5
112 #define RESPOND_SIZE (1<<RESPOND_BITS)
113 #define RESPOND_MASK (RESPOND_SIZE-1)
114 struct u132_respond {
119 struct completion wait_completion;
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,
139 /* Structure to hold all of our device specific stuff*/
141 struct list_head ftdi_list;
142 struct semaphore u132_lock;
145 struct u132_command command[COMMAND_SIZE];
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;
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;
176 __u8 bulk_in_endpointAddr;
177 __u8 bulk_out_endpointAddr;
180 u8 response[4 + 1024];
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, \
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);
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,
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)
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)
301 if (ftdi->platform_dev.dev.parent)
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);
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;
331 complete(&respond->wait_completion);
332 } up(&ftdi->u132_lock);
335 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
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);
351 up(&ftdi->u132_lock);
354 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
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 -
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;
373 command->buffer = &command->value;
374 ftdi->command_next += 1;
375 ftdi_elan_kick_command_queue(ftdi);
377 up(&ftdi->u132_lock);
379 down(&ftdi->u132_lock);
383 wait_2:if (target->active == 1) {
384 int command_size = ftdi->command_next -
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;
395 command->buffer = &command->value;
396 ftdi->command_next += 1;
397 ftdi_elan_kick_command_queue(ftdi);
399 up(&ftdi->u132_lock);
401 down(&ftdi->u132_lock);
409 up(&ftdi->u132_lock);
412 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
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 -
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;
431 command->buffer = &command->value;
432 ftdi->command_next += 1;
433 ftdi_elan_kick_command_queue(ftdi);
435 up(&ftdi->u132_lock);
437 down(&ftdi->u132_lock);
445 up(&ftdi->u132_lock);
448 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
450 ftdi_command_queue_work(ftdi, 0);
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);
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;
469 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
470 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
475 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
477 ftdi_respond_queue_work(ftdi, 0);
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);
489 int retval = ftdi_elan_respond_engine(ftdi);
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;
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));
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);
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);
531 work_delay_in_msec = 100;
533 dev_err(&ftdi->udev->dev, "synchronize failed\n");
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");
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;
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;
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;
568 dev_err(&ftdi->udev->dev, "register failed\n");
569 work_delay_in_msec = 250;
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"
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;
588 ftdi_elan_flush_targets(ftdi);
589 work_delay_in_msec = 250;
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);
603 ftdi_status_requeue_work(ftdi,
604 msecs_to_jiffies(work_delay_in_msec));
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,
622 printk(KERN_ERR "can't find device for minor %d\n", subminor);
625 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
629 if (down_interruptible(&ftdi->sw_lock)) {
632 ftdi_elan_get_kref(ftdi);
633 file->private_data = ftdi;
640 static int ftdi_elan_release(struct inode *inode, struct file *file)
642 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
645 up(&ftdi->sw_lock); /* decrement the count on our device */
646 ftdi_elan_put_kref(ftdi);
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)
657 case FTDI_ELAN_IOCDEBUG:{
659 int size = strncpy_from_user(line,
660 (const char __user *)arg, sizeof(line));
664 printk(KERN_ERR "TODO: ioctl %s\n", line);
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];
684 int m = (sizeof(data) - 1) / 3;
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) {
693 have:if (ftdi->bulk_in_left > 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) {
701 d += sprintf(d, " ..");
702 if (copy_to_user(buffer++, p, 1)) {
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;
721 } else if (retval == -ETIMEDOUT) {
722 if (retry_on_timeout-- > 0) {
724 } else if (bytes_read > 0) {
728 } else if (retval == 0) {
729 if (retry_on_empty-- > 0) {
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)
756 int I = command_size;
757 int i = ftdi->command_head;
759 struct u132_command *command = &ftdi->command[COMMAND_MASK &
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;
778 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
781 int I = command_size;
782 int i = ftdi->command_head;
784 struct u132_command *command = &ftdi->command[COMMAND_MASK &
786 total_size += 5 + command->follows;
790 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
797 int command_size = ftdi->command_next - ftdi->command_head;
798 if (command_size == 0)
800 total_size = ftdi_elan_total_command_size(ftdi, command_size);
801 urb = usb_alloc_urb(0, GFP_KERNEL);
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,
808 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
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,
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;
824 char diag[40 *3 + 4];
828 int s = (sizeof(diag) - 1) / 3;
830 while (s-- > 0 && m-- > 0) {
831 if (s > 0 || m == 0) {
832 d += sprintf(d, " %02X", *c++);
834 d += sprintf(d, " ..");
837 retval = usb_submit_urb(urb, GFP_KERNEL);
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);
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);
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;
868 target->callback = NULL;
869 (*callback) (target->endp, urb, buffer, length, toggle_bits,
870 error_count, condition_code, repeat_number, halted, skipped,
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,
878 int payload = (ed_length >> 0) & 0x07FF;
879 down(&ftdi->u132_lock);
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,
892 return ftdi->response;
894 ftdi->expected = 4 + payload;
896 up(&ftdi->u132_lock);
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,
908 return ftdi->response;
910 ftdi->expected = 4 + payload;
912 up(&ftdi->u132_lock);
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,
923 return ftdi->response;
925 target->abandoning = 0;
926 up(&ftdi->u132_lock);
927 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
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,
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);
947 ftdi_elan_do_callback(ftdi, target, NULL, 0);
948 target->abandoning = 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;
968 int retry_on_empty = 1;
969 int retry_on_timeout = 3;
970 int empty_packets = 0;
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];
979 int m = packet_bytes;
980 u8 *c = ftdi->bulk_in_buffer;
981 int s = (sizeof(diag) - 1) / 3;
983 while (s-- > 0 && m-- > 0) {
984 if (s > 0 || m == 0) {
985 d += sprintf(d, " %02X", *c++);
987 d += sprintf(d, " ..");
989 if (packet_bytes > 2) {
990 ftdi->bulk_in_left = packet_bytes - 2;
991 ftdi->bulk_in_last = 1;
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);
999 } else if (bytes_read > 0) {
1000 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
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);
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);
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);
1019 } else if (packet_bytes == 2) {
1020 unsigned char s0 = ftdi->bulk_in_buffer[0];
1021 unsigned char s1 = ftdi->bulk_in_buffer[1];
1023 if (s0 == 0x31 && s1 == 0x60) {
1024 if (retry_on_empty-- > 0) {
1028 } else if (s0 == 0x31 && s1 == 0x00) {
1029 if (retry_on_empty-- > 0) {
1034 if (retry_on_empty-- > 0) {
1039 } else if (packet_bytes == 1) {
1040 if (retry_on_empty-- > 0) {
1045 if (retry_on_empty-- > 0) {
1054 have:if (ftdi->bulk_in_left > 0) {
1055 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1057 ftdi->bulk_in_left -= 1;
1058 if (ftdi->recieved == 0 && c == 0xFF) {
1062 if (++ftdi->recieved < ftdi->expected) {
1064 } else if (ftdi->ed_found) {
1065 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1066 u16 ed_length = (ftdi->response[2] << 8) |
1068 struct u132_target *target = &ftdi->target[ed_number];
1069 int payload = (ed_length >> 0) & 0x07FF;
1070 char diag[30 *3 + 4];
1073 u8 *c = 4 + ftdi->response;
1074 int s = (sizeof(diag) - 1) / 3;
1076 while (s-- > 0 && m-- > 0) {
1077 if (s > 0 || m == 0) {
1078 d += sprintf(d, " %02X", *c++);
1080 d += sprintf(d, " ..");
1082 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1089 } else if (ftdi->expected == 8) {
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];
1096 data |= ftdi->response[6];
1098 data |= ftdi->response[5];
1100 data |= ftdi->response[4];
1101 *respond->value = data;
1102 *respond->result = 0;
1103 complete(&respond->wait_completion);
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) {
1114 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1115 "lue = %08X\n", buscmd, data);
1118 if ((ftdi->response[0] & 0x80) == 0x00) {
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) |
1126 struct u132_target *target = &ftdi->target[
1128 target->halted = (ftdi->response[0] >> 3) &
1130 target->skipped = (ftdi->response[0] >> 2) &
1132 target->toggle_bits = (ftdi->response[3] >> 6)
1134 target->error_count = (ftdi->response[3] >> 4)
1136 target->condition_code = (ftdi->response[
1138 if ((ftdi->response[0] & 0x10) == 0x00) {
1139 b = have_ed_set_response(ftdi, target,
1140 ed_length, ed_number, ed_type,
1144 b = have_ed_get_response(ftdi, target,
1145 ed_length, ed_number, ed_type,
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,
1167 struct usb_ftdi *ftdi = file->private_data;
1169 if (ftdi->disconnected > 0) {
1175 urb = usb_alloc_urb(0, GFP_KERNEL);
1180 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1181 &urb->transfer_dma);
1186 if (copy_from_user(buf, user_buffer, count)) {
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);
1196 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1205 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1212 static 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
1251 #define cPIDinonce 0x3
1252 #define cCCnoerror 0x0
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) {
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);
1288 up(&ftdi->u132_lock);
1295 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1298 u8 addressofs = config_offset / 4;
1299 wait:if (ftdi->disconnected > 0) {
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);
1320 up(&ftdi->u132_lock);
1327 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1330 u8 addressofs = mem_offset / 4;
1331 wait:if (ftdi->disconnected > 0) {
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);
1352 up(&ftdi->u132_lock);
1359 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
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) {
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;
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);
1402 up(&ftdi->u132_lock);
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) {
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 &
1431 command->length = 0x04;
1432 respond->address = command->address = addressofs;
1433 command->width = 0x00 | (width & 0x0F);
1434 command->follows = 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);
1446 up(&ftdi->u132_lock);
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) {
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 &
1475 command->length = 0x04;
1476 respond->address = command->address = addressofs;
1477 command->width = 0x00 | (width & 0x0F);
1478 command->follows = 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);
1490 up(&ftdi->u132_lock);
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) {
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) {
1518 } else if (ftdi->initialized == 0) {
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)
1532 command->width = usb_maxpacket(urb->dev, urb->pipe,
1533 usb_pipeout(urb->pipe));
1534 command->follows = 8;
1536 command->buffer = urb->setup_packet;
1537 target->callback = callback;
1538 target->endp = endp;
1541 ftdi->command_next += 1;
1542 ftdi_elan_kick_command_queue(ftdi);
1543 up(&ftdi->u132_lock);
1546 up(&ftdi->u132_lock);
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) {
1575 } else if (ftdi->initialized == 0) {
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 -
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;
1593 command->length = 0x8000 | (remaining_length -
1595 command->address = (toggle_bits << 6) | (ep_number << 2)
1597 command->width = usb_maxpacket(urb->dev, urb->pipe,
1598 usb_pipeout(urb->pipe));
1599 command->follows = 0;
1601 command->buffer = NULL;
1602 target->callback = callback;
1603 target->endp = endp;
1606 ftdi->command_next += 1;
1607 ftdi_elan_kick_command_queue(ftdi);
1608 up(&ftdi->u132_lock);
1611 up(&ftdi->u132_lock);
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) {
1640 } else if (ftdi->initialized == 0) {
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)
1654 command->width = usb_maxpacket(urb->dev, urb->pipe,
1655 usb_pipeout(urb->pipe));
1656 command->follows = 0;
1658 command->buffer = NULL;
1659 target->callback = callback;
1660 target->endp = endp;
1663 ftdi->command_next += 1;
1664 ftdi_elan_kick_command_queue(ftdi);
1665 up(&ftdi->u132_lock);
1668 up(&ftdi->u132_lock);
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) {
1697 } else if (ftdi->initialized == 0) {
1701 down(&ftdi->u132_lock);
1702 command_size = ftdi->command_next - ftdi->command_head;
1703 if (command_size < COMMAND_SIZE) {
1707 char data[30 *3 + 4];
1709 int m = (sizeof(data) - 1) / 3;
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)
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);
1723 command->buffer = urb->transfer_buffer +
1725 command->length = 0x8000 | (command->follows - 1);
1726 b = command->buffer;
1727 urb_size = command->follows;
1729 while (urb_size-- > 0) {
1731 } else if (i++ < m) {
1732 int w = sprintf(d, " %02X", *b++);
1736 d += sprintf(d, " ..");
1738 target->callback = callback;
1739 target->endp = endp;
1742 ftdi->command_next += 1;
1743 ftdi_elan_kick_command_queue(ftdi);
1744 up(&ftdi->u132_lock);
1747 up(&ftdi->u132_lock);
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) {
1776 } else if (ftdi->initialized == 0) {
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 -
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;
1794 command->length = 0x8000 | (remaining_length -
1796 command->address = (toggle_bits << 6) | (ep_number << 2)
1798 command->width = usb_maxpacket(urb->dev, urb->pipe,
1799 usb_pipeout(urb->pipe));
1800 command->follows = 0;
1802 command->buffer = NULL;
1803 target->callback = callback;
1804 target->endp = endp;
1807 ftdi->command_next += 1;
1808 ftdi_elan_kick_command_queue(ftdi);
1809 up(&ftdi->u132_lock);
1812 up(&ftdi->u132_lock);
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,
1835 u8 ed = ed_number - 1;
1836 if (ftdi->disconnected > 0) {
1838 } else if (ftdi->initialized == 0) {
1841 struct u132_target *target = &ftdi->target[ed];
1842 down(&ftdi->u132_lock);
1843 if (target->abandoning > 0) {
1844 up(&ftdi->u132_lock);
1847 target->abandoning = 1;
1848 wait_1:if (target->active == 1) {
1849 int command_size = ftdi->command_next -
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) |
1857 command->length = 0x00;
1858 command->address = 0x00;
1859 command->width = 0x00;
1860 command->follows = 0;
1862 command->buffer = &command->value;
1863 ftdi->command_next += 1;
1864 ftdi_elan_kick_command_queue(ftdi);
1866 up(&ftdi->u132_lock);
1868 down(&ftdi->u132_lock);
1872 up(&ftdi->u132_lock);
1878 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
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;
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];
1901 int m = (sizeof(diag) - 1) / 3;
1902 char *b = ftdi->bulk_in_buffer;
1905 while (packet_bytes-- > 0) {
1907 if (bytes_read < m) {
1908 d += sprintf(d, " %02X",
1910 } else if (bytes_read > m) {
1912 d += sprintf(d, " ..");
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) {
1922 } else if (retry_on_status-- > 0) {
1925 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
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) {
1936 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1940 } else if (retval == -ETIMEDOUT) {
1941 if (retry_on_timeout-- > 0) {
1944 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1948 } else if (retval == 0) {
1949 if (retry_on_empty-- > 0) {
1952 dev_err(&ftdi->udev->dev, "empty packet retry l"
1957 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1966 * send the long flush sequence
1969 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1976 urb = usb_alloc_urb(0, GFP_KERNEL);
1978 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1982 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1984 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
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);
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);
2009 * send the reset sequence
2012 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2019 urb = usb_alloc_urb(0, GFP_KERNEL);
2021 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2025 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2027 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
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);
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);
2052 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2056 int retry_on_timeout = 5;
2057 int retry_on_empty = 10;
2059 retval = ftdi_elan_flush_input_fifo(ftdi);
2062 ftdi->bulk_in_left = 0;
2063 ftdi->bulk_in_last = -1;
2064 while (long_stop-- > 0) {
2067 retval = ftdi_elan_synchronize_flush(ftdi);
2070 retval = ftdi_elan_flush_input_fifo(ftdi);
2073 reset:retval = ftdi_elan_synchronize_reset(ftdi);
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];
2088 int m = (sizeof(diag) - 1) / 3;
2089 char *b = ftdi->bulk_in_buffer;
2091 unsigned char c = 0;
2093 while (packet_bytes-- > 0) {
2095 if (bytes_read < m) {
2096 d += sprintf(d, " %02X", c);
2097 } else if (bytes_read > m) {
2099 d += sprintf(d, " ..");
2108 } else if (read_stop-- > 0) {
2111 dev_err(&ftdi->udev->dev, "retr"
2112 "y limit reached\n");
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) {
2124 } else if (s1 == 0x31 && s2 == 0x60) {
2125 if (read_stop-- > 0) {
2128 dev_err(&ftdi->udev->dev, "retr"
2129 "y limit reached\n");
2133 if (read_stop-- > 0) {
2136 dev_err(&ftdi->udev->dev, "retr"
2137 "y limit reached\n");
2141 } else if (packet_bytes > 0) {
2142 if (read_stop-- > 0) {
2145 dev_err(&ftdi->udev->dev, "retry limit "
2149 } else if (retval == -ETIMEDOUT) {
2150 if (retry_on_timeout-- > 0) {
2153 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2154 "try limit reached\n");
2157 } else if (retval == 0) {
2158 if (retry_on_empty-- > 0) {
2161 dev_err(&ftdi->udev->dev, "empty packet"
2162 " retry limit reached\n");
2167 dev_err(&ftdi->udev->dev, "error = %d\n",
2169 if (read_stop-- > 0) {
2172 dev_err(&ftdi->udev->dev, "retry limit "
2179 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
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;
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];
2197 int m = (sizeof(diag) - 1) / 3;
2198 char *b = ftdi->bulk_in_buffer;
2201 while (packet_bytes-- > 0) {
2203 if (bytes_read < m) {
2204 d += sprintf(d, " %02X",
2206 } else if (bytes_read > m) {
2208 d += sprintf(d, " ..");
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) {
2218 } else if (retry_on_status-- > 0) {
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) {
2231 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2235 } else if (retval == -ETIMEDOUT) {
2236 if (retry_on_timeout-- > 0) {
2239 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2243 } else if (retval == 0) {
2244 if (retry_on_empty-- > 0) {
2247 dev_err(&ftdi->udev->dev, "empty packet retry l"
2252 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2259 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2261 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2264 if (ftdi->controlreg & 0x00400000) {
2265 if (ftdi->card_ejected) {
2267 ftdi->card_ejected = 1;
2268 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2269 "%08X\n", ftdi->controlreg);
2273 u8 fn = ftdi->function - 1;
2274 int activePCIfn = fn << 8;
2279 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2283 pciVID = pcidata & 0xFFFF;
2284 pciPID = (pcidata >> 16) & 0xFFFF;
2285 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2286 ftdi->platform_data.device) {
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);
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 | \
2311 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2325 int mask = OHCI_INTR_INIT;
2327 int reset_timeout = 30; /* ... allow extra time */
2329 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2332 retval = ftdi_read_pcimem(ftdi, control, &control);
2335 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2338 num_ports = rh_a & RH_A_NDP;
2339 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
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);
2349 switch (hc_control & OHCI_CTRL_HCFS) {
2353 case OHCI_USB_SUSPEND:
2354 case OHCI_USB_RESUME:
2355 hc_control &= OHCI_CTRL_RWC;
2356 hc_control |= OHCI_USB_RESUME;
2360 hc_control &= OHCI_CTRL_RWC;
2361 hc_control |= OHCI_USB_RESET;
2365 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2368 retval = ftdi_read_pcimem(ftdi, control, &control);
2372 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
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);
2383 retval = ftdi_read_pcimem(ftdi, control, &control);
2386 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2389 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2393 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2396 if (0 != (status & OHCI_HCR)) {
2397 if (--reset_timeout == 0) {
2398 dev_err(&ftdi->udev->dev, "USB HC reset timed o"
2407 if (quirk & OHCI_QUIRK_INITRESET) {
2408 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2411 retval = ftdi_read_pcimem(ftdi, control, &control);
2415 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2418 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2421 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2424 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2427 retval = ftdi_write_pcimem(ftdi, fminterval,
2428 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2431 retval = ftdi_write_pcimem(ftdi, periodicstart,
2432 ((9 *hc_fminterval) / 10) & 0x3fff);
2435 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2438 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2441 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2442 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2443 quirk |= OHCI_QUIRK_INITRESET;
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);
2454 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2457 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2460 retval = ftdi_read_pcimem(ftdi, control, &control);
2463 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2466 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
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 |
2474 return retval; /* handle root hub init quirks ... */
2475 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
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);
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);
2491 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2494 retval = ftdi_write_pcimem(ftdi, roothub.b,
2495 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2498 retval = ftdi_read_pcimem(ftdi, control, &control);
2501 mdelay((roothub_a >> 23) & 0x1fe);
2502 for (temp = 0; temp < num_ports; temp++) {
2504 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2514 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2520 int activePCIfn = fn << 8;
2521 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2525 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2529 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2533 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2537 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2542 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2546 latence_timer &= 0xFFFF00FF;
2547 latence_timer |= 0x00001600;
2548 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2552 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2557 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2561 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2565 for (reg = 0; reg <= 0x54; reg += 4) {
2566 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2573 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2579 int activePCIfn = fn << 8;
2580 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2584 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2588 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2592 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2596 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2601 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2605 latence_timer &= 0xFFFF00FF;
2606 latence_timer |= 0x00001600;
2607 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2611 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2616 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2620 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2627 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2631 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2634 result = ftdi_elan_check_controller(ftdi, quirk);
2635 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2641 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2646 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2649 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2653 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2656 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2659 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2662 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2665 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2669 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2672 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2675 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2678 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2681 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2685 sensebits = (controlreg >> 16) & 0x000F;
2686 if (0x0D == sensebits)
2692 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2698 int activePCIfn = 0;
2699 int max_devices = 0;
2700 int controllers = 0;
2701 int unrecognized = 0;
2703 for (fn = 0; (fn < 4); fn++) {
2707 activePCIfn = fn << 8;
2708 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
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);
2717 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2719 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2721 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2722 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2724 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2726 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2728 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2729 devices = ftdi_elan_found_controller(ftdi, fn,
2732 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2733 devices = ftdi_elan_found_controller(ftdi, fn,
2734 OHCI_QUIRK_ZFMICRO);
2736 } else if (0 == pcidata) {
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);
2752 } else if (controllers > 0) {
2754 } else if (unrecognized > 0) {
2757 ftdi->enumerated = 0;
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;
2773 int retval = -ENOMEM;
2774 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2776 printk(KERN_ERR "Out of memory\n");
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);
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"
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");
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);
2827 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2829 usb_set_intfdata(interface, NULL);
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,
2840 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2841 ftdi->bulk_in_endpointAddr == 0x83 &&
2842 ftdi->bulk_out_endpointAddr == 0x04) {
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));
2852 dev_err(&ftdi->udev->dev,
2853 "Could not find ELAN's U132 device\n");
2858 ftdi_elan_put_kref(ftdi);
2863 static void ftdi_elan_disconnect(struct usb_interface *interface)
2865 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2866 ftdi->disconnected += 1;
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);
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)
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 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2913 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2914 result = usb_register(&ftdi_elan_driver);
2916 printk(KERN_ERR "usb_register failed. Error number %d\n",
2921 static void __exit ftdi_elan_exit(void)
2923 struct usb_ftdi *ftdi;
2924 struct usb_ftdi *temp;
2925 usb_deregister(&ftdi_elan_driver);
2926 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2927 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2928 ftdi_status_cancel_work(ftdi);
2929 ftdi_command_cancel_work(ftdi);
2930 ftdi_response_cancel_work(ftdi);
2931 } flush_workqueue(status_queue);
2932 destroy_workqueue(status_queue);
2933 status_queue = NULL;
2934 flush_workqueue(command_queue);
2935 destroy_workqueue(command_queue);
2936 command_queue = NULL;
2937 flush_workqueue(respond_queue);
2938 destroy_workqueue(respond_queue);
2939 respond_queue = NULL;
2943 module_init(ftdi_elan_init);
2944 module_exit(ftdi_elan_exit);