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"
77 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
78 * If you're going to try stuff like this, you need to split
79 * out shareable stuff (register declarations?) into its own
80 * file, maybe name <linux/usb/ohci.h>
83 #include "../host/ohci.h"
84 /* Define these values to match your devices*/
85 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
86 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
87 /* table of devices that work with this driver*/
88 static struct usb_device_id ftdi_elan_table[] = {
89 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
90 { /* Terminating entry */ }
93 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
94 /* only the jtag(firmware upgrade device) interface requires
95 * a device file and corresponding minor number, but the
96 * interface is created unconditionally - I suppose it could
97 * be configured or not according to a module parameter.
98 * But since we(now) require one interface per device,
99 * and since it unlikely that a normal installation would
100 * require more than a couple of elan-ftdi devices, 8 seems
101 * like a reasonable limit to have here, and if someone
102 * really requires more than 8 devices, then they can frig the
105 #define USB_FTDI_ELAN_MINOR_BASE 192
106 #define COMMAND_BITS 5
107 #define COMMAND_SIZE (1<<COMMAND_BITS)
108 #define COMMAND_MASK (COMMAND_SIZE-1)
109 struct u132_command {
118 #define RESPOND_BITS 5
119 #define RESPOND_SIZE (1<<RESPOND_BITS)
120 #define RESPOND_MASK (RESPOND_SIZE-1)
121 struct u132_respond {
126 struct completion wait_completion;
141 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
142 int toggle_bits, int error_count, int condition_code,
143 int repeat_number, int halted, int skipped, int actual,
146 /* Structure to hold all of our device specific stuff*/
148 struct list_head ftdi_list;
149 struct semaphore u132_lock;
152 struct u132_command command[COMMAND_SIZE];
155 struct u132_respond respond[RESPOND_SIZE];
156 struct u132_target target[4];
157 char device_name[16];
158 unsigned synchronized:1;
159 unsigned enumerated:1;
160 unsigned registered:1;
161 unsigned initialized:1;
162 unsigned card_ejected:1;
168 int status_queue_delay;
169 struct semaphore sw_lock;
170 struct usb_device *udev;
171 struct usb_interface *interface;
172 struct usb_class_driver *class;
173 struct delayed_work status_work;
174 struct delayed_work command_work;
175 struct delayed_work respond_work;
176 struct u132_platform_data platform_data;
177 struct resource resources[0];
178 struct platform_device platform_dev;
179 unsigned char *bulk_in_buffer;
183 __u8 bulk_in_endpointAddr;
184 __u8 bulk_out_endpointAddr;
187 u8 response[4 + 1024];
192 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
193 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
195 static struct usb_driver ftdi_elan_driver;
196 static void ftdi_elan_delete(struct kref *kref)
198 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
199 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
200 usb_put_dev(ftdi->udev);
201 ftdi->disconnected += 1;
202 down(&ftdi_module_lock);
203 list_del_init(&ftdi->ftdi_list);
205 up(&ftdi_module_lock);
206 kfree(ftdi->bulk_in_buffer);
207 ftdi->bulk_in_buffer = NULL;
210 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
212 kref_put(&ftdi->kref, ftdi_elan_delete);
215 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
217 kref_get(&ftdi->kref);
220 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
222 kref_init(&ftdi->kref);
225 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
227 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
228 kref_put(&ftdi->kref, ftdi_elan_delete);
231 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
233 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
234 kref_get(&ftdi->kref);
237 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
239 if (cancel_delayed_work(&ftdi->status_work))
240 kref_put(&ftdi->kref, ftdi_elan_delete);
243 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
245 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
246 kref_put(&ftdi->kref, ftdi_elan_delete);
249 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
251 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
252 kref_get(&ftdi->kref);
255 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
257 if (cancel_delayed_work(&ftdi->command_work))
258 kref_put(&ftdi->kref, ftdi_elan_delete);
261 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
264 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
265 kref_put(&ftdi->kref, ftdi_elan_delete);
268 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
270 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
271 kref_get(&ftdi->kref);
274 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
276 if (cancel_delayed_work(&ftdi->respond_work))
277 kref_put(&ftdi->kref, ftdi_elan_delete);
280 void ftdi_elan_gone_away(struct platform_device *pdev)
282 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
283 ftdi->gone_away += 1;
284 ftdi_elan_put_kref(ftdi);
288 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
289 static void ftdi_release_platform_dev(struct device *dev)
294 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
295 struct u132_target *target, u8 *buffer, int length);
296 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
297 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
298 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
299 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
300 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
301 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
302 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
303 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
304 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
305 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
308 if (ftdi->platform_dev.dev.parent)
310 ftdi_elan_get_kref(ftdi);
311 ftdi->platform_data.potpg = 100;
312 ftdi->platform_data.reset = NULL;
313 ftdi->platform_dev.id = ftdi->sequence_num;
314 ftdi->platform_dev.resource = ftdi->resources;
315 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
316 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
317 ftdi->platform_dev.dev.parent = NULL;
318 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
319 ftdi->platform_dev.dev.dma_mask = NULL;
320 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
321 ftdi->platform_dev.name = ftdi->device_name;
322 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
323 request_module("u132_hcd");
324 dev_info(&ftdi->udev->dev, "registering '%s'\n",
325 ftdi->platform_dev.name);
326 result = platform_device_register(&ftdi->platform_dev);
330 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
332 down(&ftdi->u132_lock);
333 while (ftdi->respond_next > ftdi->respond_head) {
334 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
335 ftdi->respond_head++];
336 *respond->result = -ESHUTDOWN;
338 complete(&respond->wait_completion);
339 } up(&ftdi->u132_lock);
342 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
345 down(&ftdi->u132_lock);
346 while (ed_number-- > 0) {
347 struct u132_target *target = &ftdi->target[ed_number];
348 if (target->active == 1) {
349 target->condition_code = TD_DEVNOTRESP;
350 up(&ftdi->u132_lock);
351 ftdi_elan_do_callback(ftdi, target, NULL, 0);
352 down(&ftdi->u132_lock);
358 up(&ftdi->u132_lock);
361 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
364 down(&ftdi->u132_lock);
365 while (ed_number-- > 0) {
366 struct u132_target *target = &ftdi->target[ed_number];
367 target->abandoning = 1;
368 wait_1:if (target->active == 1) {
369 int command_size = ftdi->command_next -
371 if (command_size < COMMAND_SIZE) {
372 struct u132_command *command = &ftdi->command[
373 COMMAND_MASK & ftdi->command_next];
374 command->header = 0x80 | (ed_number << 5) | 0x4;
375 command->length = 0x00;
376 command->address = 0x00;
377 command->width = 0x00;
378 command->follows = 0;
380 command->buffer = &command->value;
381 ftdi->command_next += 1;
382 ftdi_elan_kick_command_queue(ftdi);
384 up(&ftdi->u132_lock);
386 down(&ftdi->u132_lock);
390 wait_2:if (target->active == 1) {
391 int command_size = ftdi->command_next -
393 if (command_size < COMMAND_SIZE) {
394 struct u132_command *command = &ftdi->command[
395 COMMAND_MASK & ftdi->command_next];
396 command->header = 0x90 | (ed_number << 5);
397 command->length = 0x00;
398 command->address = 0x00;
399 command->width = 0x00;
400 command->follows = 0;
402 command->buffer = &command->value;
403 ftdi->command_next += 1;
404 ftdi_elan_kick_command_queue(ftdi);
406 up(&ftdi->u132_lock);
408 down(&ftdi->u132_lock);
416 up(&ftdi->u132_lock);
419 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
422 down(&ftdi->u132_lock);
423 while (ed_number-- > 0) {
424 struct u132_target *target = &ftdi->target[ed_number];
425 target->abandoning = 1;
426 wait:if (target->active == 1) {
427 int command_size = ftdi->command_next -
429 if (command_size < COMMAND_SIZE) {
430 struct u132_command *command = &ftdi->command[
431 COMMAND_MASK & ftdi->command_next];
432 command->header = 0x80 | (ed_number << 5) | 0x4;
433 command->length = 0x00;
434 command->address = 0x00;
435 command->width = 0x00;
436 command->follows = 0;
438 command->buffer = &command->value;
439 ftdi->command_next += 1;
440 ftdi_elan_kick_command_queue(ftdi);
442 up(&ftdi->u132_lock);
444 down(&ftdi->u132_lock);
452 up(&ftdi->u132_lock);
455 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
457 ftdi_command_queue_work(ftdi, 0);
461 static void ftdi_elan_command_work(struct work_struct *work)
463 struct usb_ftdi *ftdi =
464 container_of(work, struct usb_ftdi, command_work.work);
466 if (ftdi->disconnected > 0) {
467 ftdi_elan_put_kref(ftdi);
470 int retval = ftdi_elan_command_engine(ftdi);
471 if (retval == -ESHUTDOWN) {
472 ftdi->disconnected += 1;
473 } else if (retval == -ENODEV) {
474 ftdi->disconnected += 1;
476 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
477 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
482 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
484 ftdi_respond_queue_work(ftdi, 0);
488 static void ftdi_elan_respond_work(struct work_struct *work)
490 struct usb_ftdi *ftdi =
491 container_of(work, struct usb_ftdi, respond_work.work);
492 if (ftdi->disconnected > 0) {
493 ftdi_elan_put_kref(ftdi);
496 int retval = ftdi_elan_respond_engine(ftdi);
498 } else if (retval == -ESHUTDOWN) {
499 ftdi->disconnected += 1;
500 } else if (retval == -ENODEV) {
501 ftdi->disconnected += 1;
502 } else if (retval == -EILSEQ) {
503 ftdi->disconnected += 1;
505 ftdi->disconnected += 1;
506 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
508 if (ftdi->disconnected > 0) {
509 ftdi_elan_abandon_completions(ftdi);
510 ftdi_elan_abandon_targets(ftdi);
512 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
519 * the sw_lock is initially held and will be freed
520 * after the FTDI has been synchronized
523 static void ftdi_elan_status_work(struct work_struct *work)
525 struct usb_ftdi *ftdi =
526 container_of(work, struct usb_ftdi, status_work.work);
527 int work_delay_in_msec = 0;
528 if (ftdi->disconnected > 0) {
529 ftdi_elan_put_kref(ftdi);
531 } else if (ftdi->synchronized == 0) {
532 down(&ftdi->sw_lock);
533 if (ftdi_elan_synchronize(ftdi) == 0) {
534 ftdi->synchronized = 1;
535 ftdi_command_queue_work(ftdi, 1);
536 ftdi_respond_queue_work(ftdi, 1);
538 work_delay_in_msec = 100;
540 dev_err(&ftdi->udev->dev, "synchronize failed\n");
542 work_delay_in_msec = 10 *1000;
544 } else if (ftdi->stuck_status > 0) {
545 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
546 ftdi->stuck_status = 0;
547 ftdi->synchronized = 0;
548 } else if ((ftdi->stuck_status++ % 60) == 1) {
549 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
550 "- please remove\n");
552 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
553 "- checked %d times\n", ftdi->stuck_status);
554 work_delay_in_msec = 100;
555 } else if (ftdi->enumerated == 0) {
556 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
557 ftdi->enumerated = 1;
558 work_delay_in_msec = 250;
560 work_delay_in_msec = 1000;
561 } else if (ftdi->initialized == 0) {
562 if (ftdi_elan_setupOHCI(ftdi) == 0) {
563 ftdi->initialized = 1;
564 work_delay_in_msec = 500;
566 dev_err(&ftdi->udev->dev, "initialized failed - trying "
567 "again in 10 seconds\n");
568 work_delay_in_msec = 1 *1000;
570 } else if (ftdi->registered == 0) {
571 work_delay_in_msec = 10;
572 if (ftdi_elan_hcd_init(ftdi) == 0) {
573 ftdi->registered = 1;
575 dev_err(&ftdi->udev->dev, "register failed\n");
576 work_delay_in_msec = 250;
578 if (ftdi_elan_checkingPCI(ftdi) == 0) {
579 work_delay_in_msec = 250;
580 } else if (ftdi->controlreg & 0x00400000) {
581 if (ftdi->gone_away > 0) {
582 dev_err(&ftdi->udev->dev, "PCI device eject con"
583 "firmed platform_dev.dev.parent=%p plat"
585 ftdi->platform_dev.dev.parent,
586 &ftdi->platform_dev.dev);
587 platform_device_unregister(&ftdi->platform_dev);
588 ftdi->platform_dev.dev.parent = NULL;
589 ftdi->registered = 0;
590 ftdi->enumerated = 0;
591 ftdi->card_ejected = 0;
592 ftdi->initialized = 0;
595 ftdi_elan_flush_targets(ftdi);
596 work_delay_in_msec = 250;
598 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
600 ftdi_elan_cancel_targets(ftdi);
601 work_delay_in_msec = 500;
602 ftdi->enumerated = 0;
603 ftdi->initialized = 0;
606 if (ftdi->disconnected > 0) {
607 ftdi_elan_put_kref(ftdi);
610 ftdi_status_requeue_work(ftdi,
611 msecs_to_jiffies(work_delay_in_msec));
618 * file_operations for the jtag interface
620 * the usage count for the device is incremented on open()
621 * and decremented on release()
623 static int ftdi_elan_open(struct inode *inode, struct file *file)
625 int subminor = iminor(inode);
626 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
629 printk(KERN_ERR "can't find device for minor %d\n", subminor);
632 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
636 if (down_interruptible(&ftdi->sw_lock)) {
639 ftdi_elan_get_kref(ftdi);
640 file->private_data = ftdi;
647 static int ftdi_elan_release(struct inode *inode, struct file *file)
649 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
652 up(&ftdi->sw_lock); /* decrement the count on our device */
653 ftdi_elan_put_kref(ftdi);
658 #define FTDI_ELAN_IOC_MAGIC 0xA1
659 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
660 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
661 unsigned int cmd, unsigned long arg)
664 case FTDI_ELAN_IOCDEBUG:{
666 int size = strncpy_from_user(line,
667 (const char __user *)arg, sizeof(line));
671 printk(KERN_ERR "TODO: ioctl %s\n", line);
683 * blocking bulk reads are used to get data from the device
686 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
687 size_t count, loff_t *ppos)
689 char data[30 *3 + 4];
691 int m = (sizeof(data) - 1) / 3;
693 int retry_on_empty = 10;
694 int retry_on_timeout = 5;
695 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
696 if (ftdi->disconnected > 0) {
700 have:if (ftdi->bulk_in_left > 0) {
702 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
703 ftdi->bulk_in_left -= 1;
704 if (bytes_read < m) {
705 d += sprintf(d, " %02X", 0x000000FF & *p);
706 } else if (bytes_read > m) {
708 d += sprintf(d, " ..");
709 if (copy_to_user(buffer++, p, 1)) {
718 more:if (count > 0) {
719 int packet_bytes = 0;
720 int retval = usb_bulk_msg(ftdi->udev,
721 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
722 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
723 &packet_bytes, msecs_to_jiffies(50));
724 if (packet_bytes > 2) {
725 ftdi->bulk_in_left = packet_bytes - 2;
726 ftdi->bulk_in_last = 1;
728 } else if (retval == -ETIMEDOUT) {
729 if (retry_on_timeout-- > 0) {
731 } else if (bytes_read > 0) {
735 } else if (retval == 0) {
736 if (retry_on_empty-- > 0) {
746 static void ftdi_elan_write_bulk_callback(struct urb *urb)
748 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
749 if (urb->status && !(urb->status == -ENOENT || urb->status ==
750 -ECONNRESET || urb->status == -ESHUTDOWN)) {
751 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
752 "d\n", urb, urb->status);
754 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
755 urb->transfer_buffer, urb->transfer_dma);
758 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
759 char *buf, int command_size, int total_size)
763 int I = command_size;
764 int i = ftdi->command_head;
766 struct u132_command *command = &ftdi->command[COMMAND_MASK &
768 int F = command->follows;
769 u8 *f = command->buffer;
770 if (command->header & 0x80) {
771 ed_commands |= 1 << (0x3 & (command->header >> 5));
773 buf[b++] = command->header;
774 buf[b++] = (command->length >> 0) & 0x00FF;
775 buf[b++] = (command->length >> 8) & 0x00FF;
776 buf[b++] = command->address;
777 buf[b++] = command->width;
785 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
788 int I = command_size;
789 int i = ftdi->command_head;
791 struct u132_command *command = &ftdi->command[COMMAND_MASK &
793 total_size += 5 + command->follows;
797 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
804 int command_size = ftdi->command_next - ftdi->command_head;
805 if (command_size == 0)
807 total_size = ftdi_elan_total_command_size(ftdi, command_size);
808 urb = usb_alloc_urb(0, GFP_KERNEL);
810 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
811 "ands totaling %d bytes to the Uxxx\n", command_size,
815 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
818 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
819 "ommands totaling %d bytes to the Uxxx\n", command_size,
824 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
825 command_size, total_size);
826 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
827 ftdi->bulk_out_endpointAddr), buf, total_size,
828 ftdi_elan_write_bulk_callback, ftdi);
829 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
831 char diag[40 *3 + 4];
835 int s = (sizeof(diag) - 1) / 3;
837 while (s-- > 0 && m-- > 0) {
838 if (s > 0 || m == 0) {
839 d += sprintf(d, " %02X", *c++);
841 d += sprintf(d, " ..");
844 retval = usb_submit_urb(urb, GFP_KERNEL);
846 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
847 "%d commands totaling %d bytes to the Uxxx\n", retval,
848 urb, command_size, total_size);
849 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
853 usb_free_urb(urb); /* release our reference to this urb,
854 the USB core will eventually free it entirely */
855 ftdi->command_head += command_size;
856 ftdi_elan_kick_respond_queue(ftdi);
860 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
861 struct u132_target *target, u8 *buffer, int length)
863 struct urb *urb = target->urb;
864 int halted = target->halted;
865 int skipped = target->skipped;
866 int actual = target->actual;
867 int non_null = target->non_null;
868 int toggle_bits = target->toggle_bits;
869 int error_count = target->error_count;
870 int condition_code = target->condition_code;
871 int repeat_number = target->repeat_number;
872 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
873 int, int, int, int) = target->callback;
875 target->callback = NULL;
876 (*callback) (target->endp, urb, buffer, length, toggle_bits,
877 error_count, condition_code, repeat_number, halted, skipped,
881 static char *have_ed_set_response(struct usb_ftdi *ftdi,
882 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
885 int payload = (ed_length >> 0) & 0x07FF;
886 down(&ftdi->u132_lock);
888 target->non_null = (ed_length >> 15) & 0x0001;
889 target->repeat_number = (ed_length >> 11) & 0x000F;
890 if (ed_type == 0x02) {
891 if (payload == 0 || target->abandoning > 0) {
892 target->abandoning = 0;
893 up(&ftdi->u132_lock);
894 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
899 return ftdi->response;
901 ftdi->expected = 4 + payload;
903 up(&ftdi->u132_lock);
906 } else if (ed_type == 0x03) {
907 if (payload == 0 || target->abandoning > 0) {
908 target->abandoning = 0;
909 up(&ftdi->u132_lock);
910 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
915 return ftdi->response;
917 ftdi->expected = 4 + payload;
919 up(&ftdi->u132_lock);
922 } else if (ed_type == 0x01) {
923 target->abandoning = 0;
924 up(&ftdi->u132_lock);
925 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
930 return ftdi->response;
932 target->abandoning = 0;
933 up(&ftdi->u132_lock);
934 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
939 return ftdi->response;
943 static char *have_ed_get_response(struct usb_ftdi *ftdi,
944 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
947 down(&ftdi->u132_lock);
948 target->condition_code = TD_DEVNOTRESP;
949 target->actual = (ed_length >> 0) & 0x01FF;
950 target->non_null = (ed_length >> 15) & 0x0001;
951 target->repeat_number = (ed_length >> 11) & 0x000F;
952 up(&ftdi->u132_lock);
954 ftdi_elan_do_callback(ftdi, target, NULL, 0);
955 target->abandoning = 0;
959 return ftdi->response;
964 * The engine tries to empty the FTDI fifo
966 * all responses found in the fifo data are dispatched thus
967 * the response buffer can only ever hold a maximum sized
968 * response from the Uxxx.
971 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
973 u8 *b = ftdi->response + ftdi->recieved;
975 int retry_on_empty = 1;
976 int retry_on_timeout = 3;
977 int empty_packets = 0;
979 int packet_bytes = 0;
980 int retval = usb_bulk_msg(ftdi->udev,
981 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
982 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
983 &packet_bytes, msecs_to_jiffies(500));
984 char diag[30 *3 + 4];
986 int m = packet_bytes;
987 u8 *c = ftdi->bulk_in_buffer;
988 int s = (sizeof(diag) - 1) / 3;
990 while (s-- > 0 && m-- > 0) {
991 if (s > 0 || m == 0) {
992 d += sprintf(d, " %02X", *c++);
994 d += sprintf(d, " ..");
996 if (packet_bytes > 2) {
997 ftdi->bulk_in_left = packet_bytes - 2;
998 ftdi->bulk_in_last = 1;
1000 } else if (retval == -ETIMEDOUT) {
1001 if (retry_on_timeout-- > 0) {
1002 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1003 "t_bytes = %d with total %d bytes%s\n",
1004 packet_bytes, bytes_read, diag);
1006 } else if (bytes_read > 0) {
1007 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
1011 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1012 "t_bytes = %d with total %d bytes%s\n",
1013 packet_bytes, bytes_read, diag);
1016 } else if (retval == -EILSEQ) {
1017 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1018 " = %d with total %d bytes%s\n", retval,
1019 packet_bytes, bytes_read, diag);
1021 } else if (retval) {
1022 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1023 " = %d with total %d bytes%s\n", retval,
1024 packet_bytes, bytes_read, diag);
1026 } else if (packet_bytes == 2) {
1027 unsigned char s0 = ftdi->bulk_in_buffer[0];
1028 unsigned char s1 = ftdi->bulk_in_buffer[1];
1030 if (s0 == 0x31 && s1 == 0x60) {
1031 if (retry_on_empty-- > 0) {
1035 } else if (s0 == 0x31 && s1 == 0x00) {
1036 if (retry_on_empty-- > 0) {
1041 if (retry_on_empty-- > 0) {
1046 } else if (packet_bytes == 1) {
1047 if (retry_on_empty-- > 0) {
1052 if (retry_on_empty-- > 0) {
1061 have:if (ftdi->bulk_in_left > 0) {
1062 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1064 ftdi->bulk_in_left -= 1;
1065 if (ftdi->recieved == 0 && c == 0xFF) {
1069 if (++ftdi->recieved < ftdi->expected) {
1071 } else if (ftdi->ed_found) {
1072 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1073 u16 ed_length = (ftdi->response[2] << 8) |
1075 struct u132_target *target = &ftdi->target[ed_number];
1076 int payload = (ed_length >> 0) & 0x07FF;
1077 char diag[30 *3 + 4];
1080 u8 *c = 4 + ftdi->response;
1081 int s = (sizeof(diag) - 1) / 3;
1083 while (s-- > 0 && m-- > 0) {
1084 if (s > 0 || m == 0) {
1085 d += sprintf(d, " %02X", *c++);
1087 d += sprintf(d, " ..");
1089 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1096 } else if (ftdi->expected == 8) {
1098 int respond_head = ftdi->respond_head++;
1099 struct u132_respond *respond = &ftdi->respond[
1100 RESPOND_MASK & respond_head];
1101 u32 data = ftdi->response[7];
1103 data |= ftdi->response[6];
1105 data |= ftdi->response[5];
1107 data |= ftdi->response[4];
1108 *respond->value = data;
1109 *respond->result = 0;
1110 complete(&respond->wait_completion);
1115 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1116 if (buscmd == 0x00) {
1117 } else if (buscmd == 0x02) {
1118 } else if (buscmd == 0x06) {
1119 } else if (buscmd == 0x0A) {
1121 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1122 "lue = %08X\n", buscmd, data);
1125 if ((ftdi->response[0] & 0x80) == 0x00) {
1129 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1130 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1131 u16 ed_length = (ftdi->response[2] << 8) |
1133 struct u132_target *target = &ftdi->target[
1135 target->halted = (ftdi->response[0] >> 3) &
1137 target->skipped = (ftdi->response[0] >> 2) &
1139 target->toggle_bits = (ftdi->response[3] >> 6)
1141 target->error_count = (ftdi->response[3] >> 4)
1143 target->condition_code = (ftdi->response[
1145 if ((ftdi->response[0] & 0x10) == 0x00) {
1146 b = have_ed_set_response(ftdi, target,
1147 ed_length, ed_number, ed_type,
1151 b = have_ed_get_response(ftdi, target,
1152 ed_length, ed_number, ed_type,
1164 * create a urb, and a buffer for it, and copy the data to the urb
1167 static ssize_t ftdi_elan_write(struct file *file,
1168 const char __user *user_buffer, size_t count,
1174 struct usb_ftdi *ftdi = file->private_data;
1176 if (ftdi->disconnected > 0) {
1182 urb = usb_alloc_urb(0, GFP_KERNEL);
1187 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1188 &urb->transfer_dma);
1193 if (copy_from_user(buf, user_buffer, count)) {
1197 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1198 ftdi->bulk_out_endpointAddr), buf, count,
1199 ftdi_elan_write_bulk_callback, ftdi);
1200 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1201 retval = usb_submit_urb(urb, GFP_KERNEL);
1203 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1212 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1219 static const struct file_operations ftdi_elan_fops = {
1220 .owner = THIS_MODULE,
1221 .llseek = no_llseek,
1222 .ioctl = ftdi_elan_ioctl,
1223 .read = ftdi_elan_read,
1224 .write = ftdi_elan_write,
1225 .open = ftdi_elan_open,
1226 .release = ftdi_elan_release,
1230 * usb class driver info in order to get a minor number from the usb core,
1231 * and to have the device registered with the driver core
1233 static struct usb_class_driver ftdi_elan_jtag_class = {
1234 .name = "ftdi-%d-jtag",
1235 .fops = &ftdi_elan_fops,
1236 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1240 * the following definitions are for the
1241 * ELAN FPGA state machgine processor that
1242 * lies on the other side of the FTDI chip
1244 #define cPCIu132rd 0x0
1245 #define cPCIu132wr 0x1
1246 #define cPCIiord 0x2
1247 #define cPCIiowr 0x3
1248 #define cPCImemrd 0x6
1249 #define cPCImemwr 0x7
1250 #define cPCIcfgrd 0xA
1251 #define cPCIcfgwr 0xB
1252 #define cPCInull 0xF
1253 #define cU132cmd_status 0x0
1254 #define cU132flash 0x1
1255 #define cPIDsetup 0x0
1258 #define cPIDinonce 0x3
1259 #define cCCnoerror 0x0
1261 #define cCCbitstuff 0x2
1262 #define cCCtoggle 0x3
1263 #define cCCstall 0x4
1264 #define cCCnoresp 0x5
1265 #define cCCbadpid1 0x6
1266 #define cCCbadpid2 0x7
1267 #define cCCdataoverrun 0x8
1268 #define cCCdataunderrun 0x9
1269 #define cCCbuffoverrun 0xC
1270 #define cCCbuffunderrun 0xD
1271 #define cCCnotaccessed 0xF
1272 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1274 wait:if (ftdi->disconnected > 0) {
1278 down(&ftdi->u132_lock);
1279 command_size = ftdi->command_next - ftdi->command_head;
1280 if (command_size < COMMAND_SIZE) {
1281 struct u132_command *command = &ftdi->command[
1282 COMMAND_MASK & ftdi->command_next];
1283 command->header = 0x00 | cPCIu132wr;
1284 command->length = 0x04;
1285 command->address = 0x00;
1286 command->width = 0x00;
1287 command->follows = 4;
1288 command->value = data;
1289 command->buffer = &command->value;
1290 ftdi->command_next += 1;
1291 ftdi_elan_kick_command_queue(ftdi);
1292 up(&ftdi->u132_lock);
1295 up(&ftdi->u132_lock);
1302 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1305 u8 addressofs = config_offset / 4;
1306 wait:if (ftdi->disconnected > 0) {
1310 down(&ftdi->u132_lock);
1311 command_size = ftdi->command_next - ftdi->command_head;
1312 if (command_size < COMMAND_SIZE) {
1313 struct u132_command *command = &ftdi->command[
1314 COMMAND_MASK & ftdi->command_next];
1315 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1316 command->length = 0x04;
1317 command->address = addressofs;
1318 command->width = 0x00 | (width & 0x0F);
1319 command->follows = 4;
1320 command->value = data;
1321 command->buffer = &command->value;
1322 ftdi->command_next += 1;
1323 ftdi_elan_kick_command_queue(ftdi);
1324 up(&ftdi->u132_lock);
1327 up(&ftdi->u132_lock);
1334 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1337 u8 addressofs = mem_offset / 4;
1338 wait:if (ftdi->disconnected > 0) {
1342 down(&ftdi->u132_lock);
1343 command_size = ftdi->command_next - ftdi->command_head;
1344 if (command_size < COMMAND_SIZE) {
1345 struct u132_command *command = &ftdi->command[
1346 COMMAND_MASK & ftdi->command_next];
1347 command->header = 0x00 | (cPCImemwr & 0x0F);
1348 command->length = 0x04;
1349 command->address = addressofs;
1350 command->width = 0x00 | (width & 0x0F);
1351 command->follows = 4;
1352 command->value = data;
1353 command->buffer = &command->value;
1354 ftdi->command_next += 1;
1355 ftdi_elan_kick_command_queue(ftdi);
1356 up(&ftdi->u132_lock);
1359 up(&ftdi->u132_lock);
1366 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1369 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1370 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1374 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1375 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1377 wait:if (ftdi->disconnected > 0) {
1382 down(&ftdi->u132_lock);
1383 command_size = ftdi->command_next - ftdi->command_head;
1384 respond_size = ftdi->respond_next - ftdi->respond_head;
1385 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1387 struct u132_command *command = &ftdi->command[
1388 COMMAND_MASK & ftdi->command_next];
1389 struct u132_respond *respond = &ftdi->respond[
1390 RESPOND_MASK & ftdi->respond_next];
1391 int result = -ENODEV;
1392 respond->result = &result;
1393 respond->header = command->header = 0x00 | cPCIu132rd;
1394 command->length = 0x04;
1395 respond->address = command->address = cU132cmd_status;
1396 command->width = 0x00;
1397 command->follows = 0;
1399 command->buffer = NULL;
1400 respond->value = data;
1401 init_completion(&respond->wait_completion);
1402 ftdi->command_next += 1;
1403 ftdi->respond_next += 1;
1404 ftdi_elan_kick_command_queue(ftdi);
1405 up(&ftdi->u132_lock);
1406 wait_for_completion(&respond->wait_completion);
1409 up(&ftdi->u132_lock);
1416 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1417 u8 width, u32 *data)
1419 u8 addressofs = config_offset / 4;
1420 wait:if (ftdi->disconnected > 0) {
1425 down(&ftdi->u132_lock);
1426 command_size = ftdi->command_next - ftdi->command_head;
1427 respond_size = ftdi->respond_next - ftdi->respond_head;
1428 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1430 struct u132_command *command = &ftdi->command[
1431 COMMAND_MASK & ftdi->command_next];
1432 struct u132_respond *respond = &ftdi->respond[
1433 RESPOND_MASK & ftdi->respond_next];
1434 int result = -ENODEV;
1435 respond->result = &result;
1436 respond->header = command->header = 0x00 | (cPCIcfgrd &
1438 command->length = 0x04;
1439 respond->address = command->address = addressofs;
1440 command->width = 0x00 | (width & 0x0F);
1441 command->follows = 0;
1443 command->buffer = NULL;
1444 respond->value = data;
1445 init_completion(&respond->wait_completion);
1446 ftdi->command_next += 1;
1447 ftdi->respond_next += 1;
1448 ftdi_elan_kick_command_queue(ftdi);
1449 up(&ftdi->u132_lock);
1450 wait_for_completion(&respond->wait_completion);
1453 up(&ftdi->u132_lock);
1460 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1461 u8 width, u32 *data)
1463 u8 addressofs = mem_offset / 4;
1464 wait:if (ftdi->disconnected > 0) {
1469 down(&ftdi->u132_lock);
1470 command_size = ftdi->command_next - ftdi->command_head;
1471 respond_size = ftdi->respond_next - ftdi->respond_head;
1472 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1474 struct u132_command *command = &ftdi->command[
1475 COMMAND_MASK & ftdi->command_next];
1476 struct u132_respond *respond = &ftdi->respond[
1477 RESPOND_MASK & ftdi->respond_next];
1478 int result = -ENODEV;
1479 respond->result = &result;
1480 respond->header = command->header = 0x00 | (cPCImemrd &
1482 command->length = 0x04;
1483 respond->address = command->address = addressofs;
1484 command->width = 0x00 | (width & 0x0F);
1485 command->follows = 0;
1487 command->buffer = NULL;
1488 respond->value = data;
1489 init_completion(&respond->wait_completion);
1490 ftdi->command_next += 1;
1491 ftdi->respond_next += 1;
1492 ftdi_elan_kick_command_queue(ftdi);
1493 up(&ftdi->u132_lock);
1494 wait_for_completion(&respond->wait_completion);
1497 up(&ftdi->u132_lock);
1504 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1505 u8 width, u32 *data)
1507 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1508 if (ftdi->initialized == 0) {
1511 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1515 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1516 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1517 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1518 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1519 int toggle_bits, int error_count, int condition_code, int repeat_number,
1520 int halted, int skipped, int actual, int non_null))
1522 u8 ed = ed_number - 1;
1523 wait:if (ftdi->disconnected > 0) {
1525 } else if (ftdi->initialized == 0) {
1529 down(&ftdi->u132_lock);
1530 command_size = ftdi->command_next - ftdi->command_head;
1531 if (command_size < COMMAND_SIZE) {
1532 struct u132_target *target = &ftdi->target[ed];
1533 struct u132_command *command = &ftdi->command[
1534 COMMAND_MASK & ftdi->command_next];
1535 command->header = 0x80 | (ed << 5);
1536 command->length = 0x8007;
1537 command->address = (toggle_bits << 6) | (ep_number << 2)
1539 command->width = usb_maxpacket(urb->dev, urb->pipe,
1540 usb_pipeout(urb->pipe));
1541 command->follows = 8;
1543 command->buffer = urb->setup_packet;
1544 target->callback = callback;
1545 target->endp = endp;
1548 ftdi->command_next += 1;
1549 ftdi_elan_kick_command_queue(ftdi);
1550 up(&ftdi->u132_lock);
1553 up(&ftdi->u132_lock);
1560 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1561 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1562 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1563 int toggle_bits, int error_count, int condition_code, int repeat_number,
1564 int halted, int skipped, int actual, int non_null))
1566 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1567 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1568 ep_number, toggle_bits, callback);
1572 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1573 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1574 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1575 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1576 int toggle_bits, int error_count, int condition_code, int repeat_number,
1577 int halted, int skipped, int actual, int non_null))
1579 u8 ed = ed_number - 1;
1580 wait:if (ftdi->disconnected > 0) {
1582 } else if (ftdi->initialized == 0) {
1586 down(&ftdi->u132_lock);
1587 command_size = ftdi->command_next - ftdi->command_head;
1588 if (command_size < COMMAND_SIZE) {
1589 struct u132_target *target = &ftdi->target[ed];
1590 struct u132_command *command = &ftdi->command[
1591 COMMAND_MASK & ftdi->command_next];
1592 int remaining_length = urb->transfer_buffer_length -
1594 command->header = 0x82 | (ed << 5);
1595 if (remaining_length == 0) {
1596 command->length = 0x0000;
1597 } else if (remaining_length > 1024) {
1598 command->length = 0x8000 | 1023;
1600 command->length = 0x8000 | (remaining_length -
1602 command->address = (toggle_bits << 6) | (ep_number << 2)
1604 command->width = usb_maxpacket(urb->dev, urb->pipe,
1605 usb_pipeout(urb->pipe));
1606 command->follows = 0;
1608 command->buffer = NULL;
1609 target->callback = callback;
1610 target->endp = endp;
1613 ftdi->command_next += 1;
1614 ftdi_elan_kick_command_queue(ftdi);
1615 up(&ftdi->u132_lock);
1618 up(&ftdi->u132_lock);
1625 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1626 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1627 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1628 int toggle_bits, int error_count, int condition_code, int repeat_number,
1629 int halted, int skipped, int actual, int non_null))
1631 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1632 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1633 ep_number, toggle_bits, callback);
1637 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1638 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1639 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1640 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1641 int toggle_bits, int error_count, int condition_code, int repeat_number,
1642 int halted, int skipped, int actual, int non_null))
1644 u8 ed = ed_number - 1;
1645 wait:if (ftdi->disconnected > 0) {
1647 } else if (ftdi->initialized == 0) {
1651 down(&ftdi->u132_lock);
1652 command_size = ftdi->command_next - ftdi->command_head;
1653 if (command_size < COMMAND_SIZE) {
1654 struct u132_target *target = &ftdi->target[ed];
1655 struct u132_command *command = &ftdi->command[
1656 COMMAND_MASK & ftdi->command_next];
1657 command->header = 0x81 | (ed << 5);
1658 command->length = 0x0000;
1659 command->address = (toggle_bits << 6) | (ep_number << 2)
1661 command->width = usb_maxpacket(urb->dev, urb->pipe,
1662 usb_pipeout(urb->pipe));
1663 command->follows = 0;
1665 command->buffer = NULL;
1666 target->callback = callback;
1667 target->endp = endp;
1670 ftdi->command_next += 1;
1671 ftdi_elan_kick_command_queue(ftdi);
1672 up(&ftdi->u132_lock);
1675 up(&ftdi->u132_lock);
1682 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1683 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1684 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1685 int toggle_bits, int error_count, int condition_code, int repeat_number,
1686 int halted, int skipped, int actual, int non_null))
1688 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1689 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1690 ep_number, toggle_bits, callback);
1694 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1695 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1696 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1697 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1698 int toggle_bits, int error_count, int condition_code, int repeat_number,
1699 int halted, int skipped, int actual, int non_null))
1701 u8 ed = ed_number - 1;
1702 wait:if (ftdi->disconnected > 0) {
1704 } else if (ftdi->initialized == 0) {
1708 down(&ftdi->u132_lock);
1709 command_size = ftdi->command_next - ftdi->command_head;
1710 if (command_size < COMMAND_SIZE) {
1714 char data[30 *3 + 4];
1716 int m = (sizeof(data) - 1) / 3;
1718 struct u132_target *target = &ftdi->target[ed];
1719 struct u132_command *command = &ftdi->command[
1720 COMMAND_MASK & ftdi->command_next];
1721 command->header = 0x81 | (ed << 5);
1722 command->address = (toggle_bits << 6) | (ep_number << 2)
1724 command->width = usb_maxpacket(urb->dev, urb->pipe,
1725 usb_pipeout(urb->pipe));
1726 command->follows = min(1024,
1727 urb->transfer_buffer_length -
1728 urb->actual_length);
1730 command->buffer = urb->transfer_buffer +
1732 command->length = 0x8000 | (command->follows - 1);
1733 b = command->buffer;
1734 urb_size = command->follows;
1736 while (urb_size-- > 0) {
1738 } else if (i++ < m) {
1739 int w = sprintf(d, " %02X", *b++);
1743 d += sprintf(d, " ..");
1745 target->callback = callback;
1746 target->endp = endp;
1749 ftdi->command_next += 1;
1750 ftdi_elan_kick_command_queue(ftdi);
1751 up(&ftdi->u132_lock);
1754 up(&ftdi->u132_lock);
1761 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1762 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1763 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1764 int toggle_bits, int error_count, int condition_code, int repeat_number,
1765 int halted, int skipped, int actual, int non_null))
1767 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1768 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1769 ep_number, toggle_bits, callback);
1773 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1774 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1775 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1776 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1777 int toggle_bits, int error_count, int condition_code, int repeat_number,
1778 int halted, int skipped, int actual, int non_null))
1780 u8 ed = ed_number - 1;
1781 wait:if (ftdi->disconnected > 0) {
1783 } else if (ftdi->initialized == 0) {
1787 down(&ftdi->u132_lock);
1788 command_size = ftdi->command_next - ftdi->command_head;
1789 if (command_size < COMMAND_SIZE) {
1790 int remaining_length = urb->transfer_buffer_length -
1792 struct u132_target *target = &ftdi->target[ed];
1793 struct u132_command *command = &ftdi->command[
1794 COMMAND_MASK & ftdi->command_next];
1795 command->header = 0x83 | (ed << 5);
1796 if (remaining_length == 0) {
1797 command->length = 0x0000;
1798 } else if (remaining_length > 1024) {
1799 command->length = 0x8000 | 1023;
1801 command->length = 0x8000 | (remaining_length -
1803 command->address = (toggle_bits << 6) | (ep_number << 2)
1805 command->width = usb_maxpacket(urb->dev, urb->pipe,
1806 usb_pipeout(urb->pipe));
1807 command->follows = 0;
1809 command->buffer = NULL;
1810 target->callback = callback;
1811 target->endp = endp;
1814 ftdi->command_next += 1;
1815 ftdi_elan_kick_command_queue(ftdi);
1816 up(&ftdi->u132_lock);
1819 up(&ftdi->u132_lock);
1826 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1827 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1828 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1829 int toggle_bits, int error_count, int condition_code, int repeat_number,
1830 int halted, int skipped, int actual, int non_null))
1832 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1833 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1834 ep_number, toggle_bits, callback);
1838 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1839 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1842 u8 ed = ed_number - 1;
1843 if (ftdi->disconnected > 0) {
1845 } else if (ftdi->initialized == 0) {
1848 struct u132_target *target = &ftdi->target[ed];
1849 down(&ftdi->u132_lock);
1850 if (target->abandoning > 0) {
1851 up(&ftdi->u132_lock);
1854 target->abandoning = 1;
1855 wait_1:if (target->active == 1) {
1856 int command_size = ftdi->command_next -
1858 if (command_size < COMMAND_SIZE) {
1859 struct u132_command *command =
1860 &ftdi->command[COMMAND_MASK &
1861 ftdi->command_next];
1862 command->header = 0x80 | (ed << 5) |
1864 command->length = 0x00;
1865 command->address = 0x00;
1866 command->width = 0x00;
1867 command->follows = 0;
1869 command->buffer = &command->value;
1870 ftdi->command_next += 1;
1871 ftdi_elan_kick_command_queue(ftdi);
1873 up(&ftdi->u132_lock);
1875 down(&ftdi->u132_lock);
1879 up(&ftdi->u132_lock);
1885 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1888 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1889 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1893 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1894 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1896 int retry_on_empty = 10;
1897 int retry_on_timeout = 5;
1898 int retry_on_status = 20;
1900 int packet_bytes = 0;
1901 int retval = usb_bulk_msg(ftdi->udev,
1902 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1903 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1904 &packet_bytes, msecs_to_jiffies(100));
1905 if (packet_bytes > 2) {
1906 char diag[30 *3 + 4];
1908 int m = (sizeof(diag) - 1) / 3;
1909 char *b = ftdi->bulk_in_buffer;
1912 while (packet_bytes-- > 0) {
1914 if (bytes_read < m) {
1915 d += sprintf(d, " %02X",
1917 } else if (bytes_read > m) {
1919 d += sprintf(d, " ..");
1924 } else if (packet_bytes > 1) {
1925 char s1 = ftdi->bulk_in_buffer[0];
1926 char s2 = ftdi->bulk_in_buffer[1];
1927 if (s1 == 0x31 && s2 == 0x60) {
1929 } else if (retry_on_status-- > 0) {
1932 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1936 } else if (packet_bytes > 0) {
1937 char b1 = ftdi->bulk_in_buffer[0];
1938 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1939 "TDI = %02X\n", b1);
1940 if (retry_on_status-- > 0) {
1943 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1947 } else if (retval == -ETIMEDOUT) {
1948 if (retry_on_timeout-- > 0) {
1951 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1955 } else if (retval == 0) {
1956 if (retry_on_empty-- > 0) {
1959 dev_err(&ftdi->udev->dev, "empty packet retry l"
1964 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1973 * send the long flush sequence
1976 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1983 urb = usb_alloc_urb(0, GFP_KERNEL);
1985 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1989 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1991 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
1998 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1999 ftdi->bulk_out_endpointAddr), buf, i,
2000 ftdi_elan_write_bulk_callback, ftdi);
2001 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2002 retval = usb_submit_urb(urb, GFP_KERNEL);
2004 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2005 "flush sequence\n");
2006 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2016 * send the reset sequence
2019 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2026 urb = usb_alloc_urb(0, GFP_KERNEL);
2028 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2032 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2034 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2043 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2044 ftdi->bulk_out_endpointAddr), buf, i,
2045 ftdi_elan_write_bulk_callback, ftdi);
2046 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2047 retval = usb_submit_urb(urb, GFP_KERNEL);
2049 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2050 "reset sequence\n");
2051 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2059 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2063 int retry_on_timeout = 5;
2064 int retry_on_empty = 10;
2066 retval = ftdi_elan_flush_input_fifo(ftdi);
2069 ftdi->bulk_in_left = 0;
2070 ftdi->bulk_in_last = -1;
2071 while (long_stop-- > 0) {
2074 retval = ftdi_elan_synchronize_flush(ftdi);
2077 retval = ftdi_elan_flush_input_fifo(ftdi);
2080 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2086 int packet_bytes = 0;
2087 retval = usb_bulk_msg(ftdi->udev,
2088 usb_rcvbulkpipe(ftdi->udev,
2089 ftdi->bulk_in_endpointAddr),
2090 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2091 &packet_bytes, msecs_to_jiffies(500));
2092 if (packet_bytes > 2) {
2093 char diag[30 *3 + 4];
2095 int m = (sizeof(diag) - 1) / 3;
2096 char *b = ftdi->bulk_in_buffer;
2098 unsigned char c = 0;
2100 while (packet_bytes-- > 0) {
2102 if (bytes_read < m) {
2103 d += sprintf(d, " %02X", c);
2104 } else if (bytes_read > m) {
2106 d += sprintf(d, " ..");
2115 } else if (read_stop-- > 0) {
2118 dev_err(&ftdi->udev->dev, "retr"
2119 "y limit reached\n");
2123 } else if (packet_bytes > 1) {
2124 unsigned char s1 = ftdi->bulk_in_buffer[0];
2125 unsigned char s2 = ftdi->bulk_in_buffer[1];
2126 if (s1 == 0x31 && s2 == 0x00) {
2127 if (read_stuck-- > 0) {
2131 } else if (s1 == 0x31 && s2 == 0x60) {
2132 if (read_stop-- > 0) {
2135 dev_err(&ftdi->udev->dev, "retr"
2136 "y limit reached\n");
2140 if (read_stop-- > 0) {
2143 dev_err(&ftdi->udev->dev, "retr"
2144 "y limit reached\n");
2148 } else if (packet_bytes > 0) {
2149 if (read_stop-- > 0) {
2152 dev_err(&ftdi->udev->dev, "retry limit "
2156 } else if (retval == -ETIMEDOUT) {
2157 if (retry_on_timeout-- > 0) {
2160 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2161 "try limit reached\n");
2164 } else if (retval == 0) {
2165 if (retry_on_empty-- > 0) {
2168 dev_err(&ftdi->udev->dev, "empty packet"
2169 " retry limit reached\n");
2174 dev_err(&ftdi->udev->dev, "error = %d\n",
2176 if (read_stop-- > 0) {
2179 dev_err(&ftdi->udev->dev, "retry limit "
2186 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2190 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2192 int retry_on_empty = 10;
2193 int retry_on_timeout = 5;
2194 int retry_on_status = 50;
2196 int packet_bytes = 0;
2197 int retval = usb_bulk_msg(ftdi->udev,
2198 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2199 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2200 &packet_bytes, msecs_to_jiffies(1000));
2201 if (packet_bytes > 2) {
2202 char diag[30 *3 + 4];
2204 int m = (sizeof(diag) - 1) / 3;
2205 char *b = ftdi->bulk_in_buffer;
2208 while (packet_bytes-- > 0) {
2210 if (bytes_read < m) {
2211 d += sprintf(d, " %02X",
2213 } else if (bytes_read > m) {
2215 d += sprintf(d, " ..");
2220 } else if (packet_bytes > 1) {
2221 char s1 = ftdi->bulk_in_buffer[0];
2222 char s2 = ftdi->bulk_in_buffer[1];
2223 if (s1 == 0x31 && s2 == 0x60) {
2225 } else if (retry_on_status-- > 0) {
2230 } else if (packet_bytes > 0) {
2231 char b1 = ftdi->bulk_in_buffer[0];
2232 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2233 "TDI = %02X\n", b1);
2234 if (retry_on_status-- > 0) {
2238 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2242 } else if (retval == -ETIMEDOUT) {
2243 if (retry_on_timeout-- > 0) {
2246 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2250 } else if (retval == 0) {
2251 if (retry_on_empty-- > 0) {
2254 dev_err(&ftdi->udev->dev, "empty packet retry l"
2259 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2266 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2268 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2271 if (ftdi->controlreg & 0x00400000) {
2272 if (ftdi->card_ejected) {
2274 ftdi->card_ejected = 1;
2275 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2276 "%08X\n", ftdi->controlreg);
2280 u8 fn = ftdi->function - 1;
2281 int activePCIfn = fn << 8;
2286 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2290 pciVID = pcidata & 0xFFFF;
2291 pciPID = (pcidata >> 16) & 0xFFFF;
2292 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2293 ftdi->platform_data.device) {
2296 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2297 "ce=%04X pciPID=%04X\n",
2298 ftdi->platform_data.vendor, pciVID,
2299 ftdi->platform_data.device, pciPID);
2306 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2307 offsetof(struct ohci_regs, member), 0, data);
2308 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2309 offsetof(struct ohci_regs, member), 0, data);
2311 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2312 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2314 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2328 int mask = OHCI_INTR_INIT;
2330 int reset_timeout = 30; /* ... allow extra time */
2332 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2335 retval = ftdi_read_pcimem(ftdi, control, &control);
2338 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2341 num_ports = rh_a & RH_A_NDP;
2342 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2345 hc_fminterval &= 0x3fff;
2346 if (hc_fminterval != FI) {
2348 hc_fminterval |= FSMP(hc_fminterval) << 16;
2349 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2352 switch (hc_control & OHCI_CTRL_HCFS) {
2356 case OHCI_USB_SUSPEND:
2357 case OHCI_USB_RESUME:
2358 hc_control &= OHCI_CTRL_RWC;
2359 hc_control |= OHCI_USB_RESUME;
2363 hc_control &= OHCI_CTRL_RWC;
2364 hc_control |= OHCI_USB_RESET;
2368 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2371 retval = ftdi_read_pcimem(ftdi, control, &control);
2375 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2378 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2379 for (temp = 0; temp < num_ports; temp++) {
2380 retval = ftdi_write_pcimem(ftdi,
2381 roothub.portstatus[temp], RH_PS_LSDA);
2386 retval = ftdi_read_pcimem(ftdi, control, &control);
2389 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2392 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2396 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2399 if (0 != (status & OHCI_HCR)) {
2400 if (--reset_timeout == 0) {
2401 dev_err(&ftdi->udev->dev, "USB HC reset timed o"
2410 if (quirk & OHCI_QUIRK_INITRESET) {
2411 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2414 retval = ftdi_read_pcimem(ftdi, control, &control);
2418 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2421 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2424 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2427 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2430 retval = ftdi_write_pcimem(ftdi, fminterval,
2431 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2434 retval = ftdi_write_pcimem(ftdi, periodicstart,
2435 ((9 *hc_fminterval) / 10) & 0x3fff);
2438 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2441 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2444 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2445 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2446 quirk |= OHCI_QUIRK_INITRESET;
2449 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2450 fminterval, periodicstart);
2451 } /* start controller operations */
2452 hc_control &= OHCI_CTRL_RWC;
2453 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2454 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2457 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2460 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2463 retval = ftdi_read_pcimem(ftdi, control, &control);
2466 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2469 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2472 retval = ftdi_write_pcimem(ftdi, intrdisable,
2473 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2474 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2477 return retval; /* handle root hub init quirks ... */
2478 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2481 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2482 if (quirk & OHCI_QUIRK_SUPERIO) {
2483 roothub_a |= RH_A_NOCP;
2484 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2485 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2488 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2489 roothub_a |= RH_A_NPS;
2490 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2494 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2497 retval = ftdi_write_pcimem(ftdi, roothub.b,
2498 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2501 retval = ftdi_read_pcimem(ftdi, control, &control);
2504 mdelay((roothub_a >> 23) & 0x1fe);
2505 for (temp = 0; temp < num_ports; temp++) {
2507 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2517 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2523 int activePCIfn = fn << 8;
2524 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2528 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2532 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2536 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2540 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2545 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2549 latence_timer &= 0xFFFF00FF;
2550 latence_timer |= 0x00001600;
2551 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2555 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2560 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2564 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2568 for (reg = 0; reg <= 0x54; reg += 4) {
2569 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2576 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2582 int activePCIfn = fn << 8;
2583 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2587 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2591 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2595 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2599 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2604 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2608 latence_timer &= 0xFFFF00FF;
2609 latence_timer |= 0x00001600;
2610 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2614 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2619 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2623 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2630 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2634 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2637 result = ftdi_elan_check_controller(ftdi, quirk);
2638 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2644 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2649 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2652 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2656 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2659 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2662 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2665 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2668 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2672 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2675 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2678 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2681 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2684 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2688 sensebits = (controlreg >> 16) & 0x000F;
2689 if (0x0D == sensebits)
2695 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2701 int activePCIfn = 0;
2702 int max_devices = 0;
2703 int controllers = 0;
2704 int unrecognized = 0;
2706 for (fn = 0; (fn < 4); fn++) {
2710 activePCIfn = fn << 8;
2711 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2715 pciVID = pcidata & 0xFFFF;
2716 pciPID = (pcidata >> 16) & 0xFFFF;
2717 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2718 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2720 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2722 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2724 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2725 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2727 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2729 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2731 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2732 devices = ftdi_elan_found_controller(ftdi, fn,
2735 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2736 devices = ftdi_elan_found_controller(ftdi, fn,
2737 OHCI_QUIRK_ZFMICRO);
2739 } else if (0 == pcidata) {
2742 if (devices > max_devices) {
2743 max_devices = devices;
2744 ftdi->function = fn + 1;
2745 ftdi->platform_data.vendor = pciVID;
2746 ftdi->platform_data.device = pciPID;
2749 if (ftdi->function > 0) {
2750 UxxxStatus = ftdi_elan_setup_controller(ftdi,
2751 ftdi->function - 1);
2755 } else if (controllers > 0) {
2757 } else if (unrecognized > 0) {
2760 ftdi->enumerated = 0;
2767 * we use only the first bulk-in and bulk-out endpoints
2769 static int ftdi_elan_probe(struct usb_interface *interface,
2770 const struct usb_device_id *id)
2772 struct usb_host_interface *iface_desc;
2773 struct usb_endpoint_descriptor *endpoint;
2776 int retval = -ENOMEM;
2777 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2779 printk(KERN_ERR "Out of memory\n");
2782 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2783 down(&ftdi_module_lock);
2784 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2785 ftdi->sequence_num = ++ftdi_instances;
2786 up(&ftdi_module_lock);
2787 ftdi_elan_init_kref(ftdi);
2788 init_MUTEX(&ftdi->sw_lock);
2789 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2790 ftdi->interface = interface;
2791 init_MUTEX(&ftdi->u132_lock);
2793 iface_desc = interface->cur_altsetting;
2794 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2795 endpoint = &iface_desc->endpoint[i].desc;
2796 if (!ftdi->bulk_in_endpointAddr &&
2797 usb_endpoint_is_bulk_in(endpoint)) {
2798 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2799 ftdi->bulk_in_size = buffer_size;
2800 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2801 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2802 if (!ftdi->bulk_in_buffer) {
2803 dev_err(&ftdi->udev->dev, "Could not allocate b"
2809 if (!ftdi->bulk_out_endpointAddr &&
2810 usb_endpoint_is_bulk_out(endpoint)) {
2811 ftdi->bulk_out_endpointAddr =
2812 endpoint->bEndpointAddress;
2815 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2816 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2817 "-out endpoints\n");
2821 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2822 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2823 ftdi->bulk_out_endpointAddr);
2824 usb_set_intfdata(interface, ftdi);
2825 if (iface_desc->desc.bInterfaceNumber == 0 &&
2826 ftdi->bulk_in_endpointAddr == 0x81 &&
2827 ftdi->bulk_out_endpointAddr == 0x02) {
2828 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2830 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2832 usb_set_intfdata(interface, NULL);
2836 ftdi->class = &ftdi_elan_jtag_class;
2837 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2838 "%d now attached to ftdi%d\n", ftdi,
2839 iface_desc->desc.bInterfaceNumber,
2843 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2844 ftdi->bulk_in_endpointAddr == 0x83 &&
2845 ftdi->bulk_out_endpointAddr == 0x04) {
2847 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2848 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2849 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2850 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2851 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2852 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2855 dev_err(&ftdi->udev->dev,
2856 "Could not find ELAN's U132 device\n");
2861 ftdi_elan_put_kref(ftdi);
2866 static void ftdi_elan_disconnect(struct usb_interface *interface)
2868 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2869 ftdi->disconnected += 1;
2871 int minor = interface->minor;
2872 struct usb_class_driver *class = ftdi->class;
2873 usb_set_intfdata(interface, NULL);
2874 usb_deregister_dev(interface, class);
2875 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2876 "or %d now disconnected\n", minor);
2878 ftdi_status_cancel_work(ftdi);
2879 ftdi_command_cancel_work(ftdi);
2880 ftdi_response_cancel_work(ftdi);
2881 ftdi_elan_abandon_completions(ftdi);
2882 ftdi_elan_abandon_targets(ftdi);
2883 if (ftdi->registered) {
2884 platform_device_unregister(&ftdi->platform_dev);
2885 ftdi->synchronized = 0;
2886 ftdi->enumerated = 0;
2887 ftdi->initialized = 0;
2888 ftdi->registered = 0;
2890 flush_workqueue(status_queue);
2891 flush_workqueue(command_queue);
2892 flush_workqueue(respond_queue);
2893 ftdi->disconnected += 1;
2894 usb_set_intfdata(interface, NULL);
2895 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2896 "face now disconnected\n");
2898 ftdi_elan_put_kref(ftdi);
2901 static struct usb_driver ftdi_elan_driver = {
2902 .name = "ftdi-elan",
2903 .probe = ftdi_elan_probe,
2904 .disconnect = ftdi_elan_disconnect,
2905 .id_table = ftdi_elan_table,
2907 static int __init ftdi_elan_init(void)
2910 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2911 __TIME__, __DATE__);
2912 init_MUTEX(&ftdi_module_lock);
2913 INIT_LIST_HEAD(&ftdi_static_list);
2914 status_queue = create_singlethread_workqueue("ftdi-status-control");
2916 goto err_status_queue;
2917 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2919 goto err_command_queue;
2920 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2922 goto err_respond_queue;
2923 result = usb_register(&ftdi_elan_driver);
2925 destroy_workqueue(status_queue);
2926 destroy_workqueue(command_queue);
2927 destroy_workqueue(respond_queue);
2928 printk(KERN_ERR "usb_register failed. Error number %d\n",
2934 destroy_workqueue(command_queue);
2936 destroy_workqueue(status_queue);
2938 printk(KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name);
2942 static void __exit ftdi_elan_exit(void)
2944 struct usb_ftdi *ftdi;
2945 struct usb_ftdi *temp;
2946 usb_deregister(&ftdi_elan_driver);
2947 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2948 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2949 ftdi_status_cancel_work(ftdi);
2950 ftdi_command_cancel_work(ftdi);
2951 ftdi_response_cancel_work(ftdi);
2952 } flush_workqueue(status_queue);
2953 destroy_workqueue(status_queue);
2954 status_queue = NULL;
2955 flush_workqueue(command_queue);
2956 destroy_workqueue(command_queue);
2957 command_queue = NULL;
2958 flush_workqueue(respond_queue);
2959 destroy_workqueue(respond_queue);
2960 respond_queue = NULL;
2964 module_init(ftdi_elan_init);
2965 module_exit(ftdi_elan_exit);