1 // SPDX-License-Identifier: GPL-2.0
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
12 * based on various USB client drivers in the 2.6.15 linux kernel
13 * with constant reference to the 3rd Edition of Linux Device Drivers
14 * published by O'Reilly
16 * The U132 adapter is a USB to CardBus adapter specifically designed
17 * for PC cards that contain an OHCI host controller. Typical PC cards
18 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
20 * The U132 adapter will *NOT *work with PC cards that do not contain
21 * an OHCI controller. A simple way to test whether a PC card has an
22 * OHCI controller as an interface is to insert the PC card directly
23 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
24 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
25 * then there is a good chance that the U132 adapter will support the
26 * PC card.(you also need the specific client driver for the PC card)
28 * Please inform the Author and Maintainer about any PC cards that
29 * contain OHCI Host Controller and work when directly connected to
30 * an embedded CardBus slot but do not work when they are connected
31 * via an ELAN U132 adapter.
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/ioctl.h>
42 #include <linux/pci_ids.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/mutex.h>
47 #include <linux/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 bool distrust_firmware = 1;
56 module_param(distrust_firmware, bool, 0);
57 MODULE_PARM_DESC(distrust_firmware,
58 "true to distrust firmware power/overcurrent setup");
59 extern struct platform_driver u132_platform_driver;
61 * ftdi_module_lock exists to protect access to global variables
64 static struct mutex ftdi_module_lock;
65 static int ftdi_instances = 0;
66 static struct list_head ftdi_static_list;
68 * end of the global variables protected by ftdi_module_lock
72 #include <linux/usb/hcd.h>
74 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
75 * If you're going to try stuff like this, you need to split
76 * out shareable stuff (register declarations?) into its own
77 * file, maybe name <linux/usb/ohci.h>
80 #include "../host/ohci.h"
81 /* Define these values to match your devices*/
82 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
83 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
84 /* table of devices that work with this driver*/
85 static const struct usb_device_id ftdi_elan_table[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
87 { /* Terminating entry */ }
90 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
91 /* only the jtag(firmware upgrade device) interface requires
92 * a device file and corresponding minor number, but the
93 * interface is created unconditionally - I suppose it could
94 * be configured or not according to a module parameter.
95 * But since we(now) require one interface per device,
96 * and since it unlikely that a normal installation would
97 * require more than a couple of elan-ftdi devices, 8 seems
98 * like a reasonable limit to have here, and if someone
99 * really requires more than 8 devices, then they can frig the
102 #define USB_FTDI_ELAN_MINOR_BASE 192
103 #define COMMAND_BITS 5
104 #define COMMAND_SIZE (1<<COMMAND_BITS)
105 #define COMMAND_MASK (COMMAND_SIZE-1)
106 struct u132_command {
115 #define RESPOND_BITS 5
116 #define RESPOND_SIZE (1<<RESPOND_BITS)
117 #define RESPOND_MASK (RESPOND_SIZE-1)
118 struct u132_respond {
123 struct completion wait_completion;
138 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
139 int toggle_bits, int error_count, int condition_code,
140 int repeat_number, int halted, int skipped, int actual,
143 /* Structure to hold all of our device specific stuff*/
145 struct list_head ftdi_list;
146 struct mutex u132_lock;
149 struct u132_command command[COMMAND_SIZE];
152 struct u132_respond respond[RESPOND_SIZE];
153 struct u132_target target[4];
154 char device_name[16];
155 unsigned synchronized:1;
156 unsigned enumerated:1;
157 unsigned registered:1;
158 unsigned initialized:1;
159 unsigned card_ejected:1;
165 int status_queue_delay;
166 struct semaphore sw_lock;
167 struct usb_device *udev;
168 struct usb_interface *interface;
169 struct usb_class_driver *class;
170 struct delayed_work status_work;
171 struct delayed_work command_work;
172 struct delayed_work respond_work;
173 struct u132_platform_data platform_data;
174 struct resource resources[0];
175 struct platform_device platform_dev;
176 unsigned char *bulk_in_buffer;
180 __u8 bulk_in_endpointAddr;
181 __u8 bulk_out_endpointAddr;
184 u8 response[4 + 1024];
189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
192 static struct usb_driver ftdi_elan_driver;
193 static void ftdi_elan_delete(struct kref *kref)
195 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
196 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
197 usb_put_dev(ftdi->udev);
198 ftdi->disconnected += 1;
199 mutex_lock(&ftdi_module_lock);
200 list_del_init(&ftdi->ftdi_list);
202 mutex_unlock(&ftdi_module_lock);
203 kfree(ftdi->bulk_in_buffer);
204 ftdi->bulk_in_buffer = NULL;
207 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
209 kref_put(&ftdi->kref, ftdi_elan_delete);
212 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
214 kref_get(&ftdi->kref);
217 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
219 kref_init(&ftdi->kref);
222 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
224 if (!schedule_delayed_work(&ftdi->status_work, delta))
225 kref_put(&ftdi->kref, ftdi_elan_delete);
228 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
230 if (schedule_delayed_work(&ftdi->status_work, delta))
231 kref_get(&ftdi->kref);
234 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
236 if (cancel_delayed_work_sync(&ftdi->status_work))
237 kref_put(&ftdi->kref, ftdi_elan_delete);
240 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
242 if (!schedule_delayed_work(&ftdi->command_work, delta))
243 kref_put(&ftdi->kref, ftdi_elan_delete);
246 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
248 if (schedule_delayed_work(&ftdi->command_work, delta))
249 kref_get(&ftdi->kref);
252 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
254 if (cancel_delayed_work_sync(&ftdi->command_work))
255 kref_put(&ftdi->kref, ftdi_elan_delete);
258 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
261 if (!schedule_delayed_work(&ftdi->respond_work, delta))
262 kref_put(&ftdi->kref, ftdi_elan_delete);
265 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
267 if (schedule_delayed_work(&ftdi->respond_work, delta))
268 kref_get(&ftdi->kref);
271 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
273 if (cancel_delayed_work_sync(&ftdi->respond_work))
274 kref_put(&ftdi->kref, ftdi_elan_delete);
277 void ftdi_elan_gone_away(struct platform_device *pdev)
279 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
280 ftdi->gone_away += 1;
281 ftdi_elan_put_kref(ftdi);
285 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
286 static void ftdi_release_platform_dev(struct device *dev)
291 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
292 struct u132_target *target, u8 *buffer, int length);
293 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
294 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
295 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
296 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
297 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
298 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
299 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
300 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
301 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
302 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
304 if (ftdi->platform_dev.dev.parent)
307 ftdi_elan_get_kref(ftdi);
308 ftdi->platform_data.potpg = 100;
309 ftdi->platform_data.reset = NULL;
310 ftdi->platform_dev.id = ftdi->sequence_num;
311 ftdi->platform_dev.resource = ftdi->resources;
312 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
313 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
314 ftdi->platform_dev.dev.parent = NULL;
315 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
316 ftdi->platform_dev.dev.dma_mask = NULL;
317 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
318 ftdi->platform_dev.name = ftdi->device_name;
319 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
320 request_module("u132_hcd");
321 dev_info(&ftdi->udev->dev, "registering '%s'\n",
322 ftdi->platform_dev.name);
324 return platform_device_register(&ftdi->platform_dev);
327 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
329 mutex_lock(&ftdi->u132_lock);
330 while (ftdi->respond_next > ftdi->respond_head) {
331 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
332 ftdi->respond_head++];
333 *respond->result = -ESHUTDOWN;
335 complete(&respond->wait_completion);
336 } mutex_unlock(&ftdi->u132_lock);
339 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
342 mutex_lock(&ftdi->u132_lock);
343 while (ed_number-- > 0) {
344 struct u132_target *target = &ftdi->target[ed_number];
345 if (target->active == 1) {
346 target->condition_code = TD_DEVNOTRESP;
347 mutex_unlock(&ftdi->u132_lock);
348 ftdi_elan_do_callback(ftdi, target, NULL, 0);
349 mutex_lock(&ftdi->u132_lock);
355 mutex_unlock(&ftdi->u132_lock);
358 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
361 mutex_lock(&ftdi->u132_lock);
362 while (ed_number-- > 0) {
363 struct u132_target *target = &ftdi->target[ed_number];
364 target->abandoning = 1;
365 wait_1:if (target->active == 1) {
366 int command_size = ftdi->command_next -
368 if (command_size < COMMAND_SIZE) {
369 struct u132_command *command = &ftdi->command[
370 COMMAND_MASK & ftdi->command_next];
371 command->header = 0x80 | (ed_number << 5) | 0x4;
372 command->length = 0x00;
373 command->address = 0x00;
374 command->width = 0x00;
375 command->follows = 0;
377 command->buffer = &command->value;
378 ftdi->command_next += 1;
379 ftdi_elan_kick_command_queue(ftdi);
381 mutex_unlock(&ftdi->u132_lock);
383 mutex_lock(&ftdi->u132_lock);
387 wait_2:if (target->active == 1) {
388 int command_size = ftdi->command_next -
390 if (command_size < COMMAND_SIZE) {
391 struct u132_command *command = &ftdi->command[
392 COMMAND_MASK & ftdi->command_next];
393 command->header = 0x90 | (ed_number << 5);
394 command->length = 0x00;
395 command->address = 0x00;
396 command->width = 0x00;
397 command->follows = 0;
399 command->buffer = &command->value;
400 ftdi->command_next += 1;
401 ftdi_elan_kick_command_queue(ftdi);
403 mutex_unlock(&ftdi->u132_lock);
405 mutex_lock(&ftdi->u132_lock);
413 mutex_unlock(&ftdi->u132_lock);
416 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
419 mutex_lock(&ftdi->u132_lock);
420 while (ed_number-- > 0) {
421 struct u132_target *target = &ftdi->target[ed_number];
422 target->abandoning = 1;
423 wait:if (target->active == 1) {
424 int command_size = ftdi->command_next -
426 if (command_size < COMMAND_SIZE) {
427 struct u132_command *command = &ftdi->command[
428 COMMAND_MASK & ftdi->command_next];
429 command->header = 0x80 | (ed_number << 5) | 0x4;
430 command->length = 0x00;
431 command->address = 0x00;
432 command->width = 0x00;
433 command->follows = 0;
435 command->buffer = &command->value;
436 ftdi->command_next += 1;
437 ftdi_elan_kick_command_queue(ftdi);
439 mutex_unlock(&ftdi->u132_lock);
441 mutex_lock(&ftdi->u132_lock);
449 mutex_unlock(&ftdi->u132_lock);
452 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
454 ftdi_command_queue_work(ftdi, 0);
457 static void ftdi_elan_command_work(struct work_struct *work)
459 struct usb_ftdi *ftdi =
460 container_of(work, struct usb_ftdi, command_work.work);
462 if (ftdi->disconnected > 0) {
463 ftdi_elan_put_kref(ftdi);
466 int retval = ftdi_elan_command_engine(ftdi);
467 if (retval == -ESHUTDOWN) {
468 ftdi->disconnected += 1;
469 } else if (retval == -ENODEV) {
470 ftdi->disconnected += 1;
472 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
473 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
478 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
480 ftdi_respond_queue_work(ftdi, 0);
483 static void ftdi_elan_respond_work(struct work_struct *work)
485 struct usb_ftdi *ftdi =
486 container_of(work, struct usb_ftdi, respond_work.work);
487 if (ftdi->disconnected > 0) {
488 ftdi_elan_put_kref(ftdi);
491 int retval = ftdi_elan_respond_engine(ftdi);
493 } else if (retval == -ESHUTDOWN) {
494 ftdi->disconnected += 1;
495 } else if (retval == -ENODEV) {
496 ftdi->disconnected += 1;
497 } else if (retval == -EILSEQ) {
498 ftdi->disconnected += 1;
500 ftdi->disconnected += 1;
501 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
503 if (ftdi->disconnected > 0) {
504 ftdi_elan_abandon_completions(ftdi);
505 ftdi_elan_abandon_targets(ftdi);
507 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
514 * the sw_lock is initially held and will be freed
515 * after the FTDI has been synchronized
518 static void ftdi_elan_status_work(struct work_struct *work)
520 struct usb_ftdi *ftdi =
521 container_of(work, struct usb_ftdi, status_work.work);
522 int work_delay_in_msec = 0;
523 if (ftdi->disconnected > 0) {
524 ftdi_elan_put_kref(ftdi);
526 } else if (ftdi->synchronized == 0) {
527 down(&ftdi->sw_lock);
528 if (ftdi_elan_synchronize(ftdi) == 0) {
529 ftdi->synchronized = 1;
530 ftdi_command_queue_work(ftdi, 1);
531 ftdi_respond_queue_work(ftdi, 1);
533 work_delay_in_msec = 100;
535 dev_err(&ftdi->udev->dev, "synchronize failed\n");
537 work_delay_in_msec = 10 *1000;
539 } else if (ftdi->stuck_status > 0) {
540 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
541 ftdi->stuck_status = 0;
542 ftdi->synchronized = 0;
543 } else if ((ftdi->stuck_status++ % 60) == 1) {
544 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
546 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
548 work_delay_in_msec = 100;
549 } else if (ftdi->enumerated == 0) {
550 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
551 ftdi->enumerated = 1;
552 work_delay_in_msec = 250;
554 work_delay_in_msec = 1000;
555 } else if (ftdi->initialized == 0) {
556 if (ftdi_elan_setupOHCI(ftdi) == 0) {
557 ftdi->initialized = 1;
558 work_delay_in_msec = 500;
560 dev_err(&ftdi->udev->dev, "initialized failed - trying 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 confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
576 ftdi->platform_dev.dev.parent,
577 &ftdi->platform_dev.dev);
578 platform_device_unregister(&ftdi->platform_dev);
579 ftdi->platform_dev.dev.parent = NULL;
580 ftdi->registered = 0;
581 ftdi->enumerated = 0;
582 ftdi->card_ejected = 0;
583 ftdi->initialized = 0;
586 ftdi_elan_flush_targets(ftdi);
587 work_delay_in_msec = 250;
589 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
590 ftdi_elan_cancel_targets(ftdi);
591 work_delay_in_msec = 500;
592 ftdi->enumerated = 0;
593 ftdi->initialized = 0;
596 if (ftdi->disconnected > 0) {
597 ftdi_elan_put_kref(ftdi);
600 ftdi_status_requeue_work(ftdi,
601 msecs_to_jiffies(work_delay_in_msec));
608 * file_operations for the jtag interface
610 * the usage count for the device is incremented on open()
611 * and decremented on release()
613 static int ftdi_elan_open(struct inode *inode, struct file *file)
616 struct usb_interface *interface;
618 subminor = iminor(inode);
619 interface = usb_find_interface(&ftdi_elan_driver, subminor);
622 pr_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 = file->private_data;
645 up(&ftdi->sw_lock); /* decrement the count on our device */
646 ftdi_elan_put_kref(ftdi);
653 * blocking bulk reads are used to get data from the device
656 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
657 size_t count, loff_t *ppos)
659 char data[30 *3 + 4];
661 int m = (sizeof(data) - 1) / 3 - 1;
663 int retry_on_empty = 10;
664 int retry_on_timeout = 5;
665 struct usb_ftdi *ftdi = file->private_data;
666 if (ftdi->disconnected > 0) {
670 have:if (ftdi->bulk_in_left > 0) {
672 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
673 ftdi->bulk_in_left -= 1;
674 if (bytes_read < m) {
675 d += sprintf(d, " %02X", 0x000000FF & *p);
676 } else if (bytes_read > m) {
678 d += sprintf(d, " ..");
679 if (copy_to_user(buffer++, p, 1)) {
688 more:if (count > 0) {
689 int packet_bytes = 0;
690 int retval = usb_bulk_msg(ftdi->udev,
691 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
692 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
694 if (packet_bytes > 2) {
695 ftdi->bulk_in_left = packet_bytes - 2;
696 ftdi->bulk_in_last = 1;
698 } else if (retval == -ETIMEDOUT) {
699 if (retry_on_timeout-- > 0) {
701 } else if (bytes_read > 0) {
705 } else if (retval == 0) {
706 if (retry_on_empty-- > 0) {
716 static void ftdi_elan_write_bulk_callback(struct urb *urb)
718 struct usb_ftdi *ftdi = urb->context;
719 int status = urb->status;
721 if (status && !(status == -ENOENT || status == -ECONNRESET ||
722 status == -ESHUTDOWN)) {
723 dev_err(&ftdi->udev->dev,
724 "urb=%p write bulk status received: %d\n", urb, status);
726 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
727 urb->transfer_buffer, urb->transfer_dma);
730 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
731 char *buf, int command_size, int total_size)
735 int I = command_size;
736 int i = ftdi->command_head;
738 struct u132_command *command = &ftdi->command[COMMAND_MASK &
740 int F = command->follows;
741 u8 *f = command->buffer;
742 if (command->header & 0x80) {
743 ed_commands |= 1 << (0x3 & (command->header >> 5));
745 buf[b++] = command->header;
746 buf[b++] = (command->length >> 0) & 0x00FF;
747 buf[b++] = (command->length >> 8) & 0x00FF;
748 buf[b++] = command->address;
749 buf[b++] = command->width;
757 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
760 int I = command_size;
761 int i = ftdi->command_head;
763 struct u132_command *command = &ftdi->command[COMMAND_MASK &
765 total_size += 5 + command->follows;
769 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
776 int command_size = ftdi->command_next - ftdi->command_head;
777 if (command_size == 0)
779 total_size = ftdi_elan_total_command_size(ftdi, command_size);
780 urb = usb_alloc_urb(0, GFP_KERNEL);
783 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
786 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
787 command_size, total_size);
791 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
792 command_size, total_size);
793 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
794 ftdi->bulk_out_endpointAddr), buf, total_size,
795 ftdi_elan_write_bulk_callback, ftdi);
796 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
798 char diag[40 *3 + 4];
802 int s = (sizeof(diag) - 1) / 3;
804 while (s-- > 0 && m-- > 0) {
805 if (s > 0 || m == 0) {
806 d += sprintf(d, " %02X", *c++);
808 d += sprintf(d, " ..");
811 retval = usb_submit_urb(urb, GFP_KERNEL);
813 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
814 retval, urb, command_size, total_size);
815 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
819 usb_free_urb(urb); /* release our reference to this urb,
820 the USB core will eventually free it entirely */
821 ftdi->command_head += command_size;
822 ftdi_elan_kick_respond_queue(ftdi);
826 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
827 struct u132_target *target, u8 *buffer, int length)
829 struct urb *urb = target->urb;
830 int halted = target->halted;
831 int skipped = target->skipped;
832 int actual = target->actual;
833 int non_null = target->non_null;
834 int toggle_bits = target->toggle_bits;
835 int error_count = target->error_count;
836 int condition_code = target->condition_code;
837 int repeat_number = target->repeat_number;
838 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
839 int, int, int, int) = target->callback;
841 target->callback = NULL;
842 (*callback) (target->endp, urb, buffer, length, toggle_bits,
843 error_count, condition_code, repeat_number, halted, skipped,
847 static char *have_ed_set_response(struct usb_ftdi *ftdi,
848 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
851 int payload = (ed_length >> 0) & 0x07FF;
852 mutex_lock(&ftdi->u132_lock);
854 target->non_null = (ed_length >> 15) & 0x0001;
855 target->repeat_number = (ed_length >> 11) & 0x000F;
856 if (ed_type == 0x02 || ed_type == 0x03) {
857 if (payload == 0 || target->abandoning > 0) {
858 target->abandoning = 0;
859 mutex_unlock(&ftdi->u132_lock);
860 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
865 return ftdi->response;
867 ftdi->expected = 4 + payload;
869 mutex_unlock(&ftdi->u132_lock);
873 target->abandoning = 0;
874 mutex_unlock(&ftdi->u132_lock);
875 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
880 return ftdi->response;
884 static char *have_ed_get_response(struct usb_ftdi *ftdi,
885 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
888 mutex_lock(&ftdi->u132_lock);
889 target->condition_code = TD_DEVNOTRESP;
890 target->actual = (ed_length >> 0) & 0x01FF;
891 target->non_null = (ed_length >> 15) & 0x0001;
892 target->repeat_number = (ed_length >> 11) & 0x000F;
893 mutex_unlock(&ftdi->u132_lock);
895 ftdi_elan_do_callback(ftdi, target, NULL, 0);
896 target->abandoning = 0;
900 return ftdi->response;
905 * The engine tries to empty the FTDI fifo
907 * all responses found in the fifo data are dispatched thus
908 * the response buffer can only ever hold a maximum sized
909 * response from the Uxxx.
912 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
914 u8 *b = ftdi->response + ftdi->received;
916 int retry_on_empty = 1;
917 int retry_on_timeout = 3;
918 int empty_packets = 0;
920 int packet_bytes = 0;
921 int retval = usb_bulk_msg(ftdi->udev,
922 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
923 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
925 char diag[30 *3 + 4];
927 int m = packet_bytes;
928 u8 *c = ftdi->bulk_in_buffer;
929 int s = (sizeof(diag) - 1) / 3;
931 while (s-- > 0 && m-- > 0) {
932 if (s > 0 || m == 0) {
933 d += sprintf(d, " %02X", *c++);
935 d += sprintf(d, " ..");
937 if (packet_bytes > 2) {
938 ftdi->bulk_in_left = packet_bytes - 2;
939 ftdi->bulk_in_last = 1;
941 } else if (retval == -ETIMEDOUT) {
942 if (retry_on_timeout-- > 0) {
943 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
944 packet_bytes, bytes_read, diag);
946 } else if (bytes_read > 0) {
947 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
951 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
952 packet_bytes, bytes_read, diag);
955 } else if (retval == -EILSEQ) {
956 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
957 retval, packet_bytes, bytes_read, diag);
960 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
961 retval, packet_bytes, bytes_read, diag);
963 } else if (packet_bytes == 2) {
964 unsigned char s0 = ftdi->bulk_in_buffer[0];
965 unsigned char s1 = ftdi->bulk_in_buffer[1];
967 if (s0 == 0x31 && s1 == 0x60) {
968 if (retry_on_empty-- > 0) {
972 } else if (s0 == 0x31 && s1 == 0x00) {
973 if (retry_on_empty-- > 0) {
978 if (retry_on_empty-- > 0) {
983 } else if (packet_bytes == 1) {
984 if (retry_on_empty-- > 0) {
989 if (retry_on_empty-- > 0) {
998 have:if (ftdi->bulk_in_left > 0) {
999 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1001 ftdi->bulk_in_left -= 1;
1002 if (ftdi->received == 0 && c == 0xFF) {
1006 if (++ftdi->received < ftdi->expected) {
1008 } else if (ftdi->ed_found) {
1009 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1010 u16 ed_length = (ftdi->response[2] << 8) |
1012 struct u132_target *target = &ftdi->target[ed_number];
1013 int payload = (ed_length >> 0) & 0x07FF;
1014 char diag[30 *3 + 4];
1017 u8 *c = 4 + ftdi->response;
1018 int s = (sizeof(diag) - 1) / 3;
1020 while (s-- > 0 && m-- > 0) {
1021 if (s > 0 || m == 0) {
1022 d += sprintf(d, " %02X", *c++);
1024 d += sprintf(d, " ..");
1026 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1033 } else if (ftdi->expected == 8) {
1035 int respond_head = ftdi->respond_head++;
1036 struct u132_respond *respond = &ftdi->respond[
1037 RESPOND_MASK & respond_head];
1038 u32 data = ftdi->response[7];
1040 data |= ftdi->response[6];
1042 data |= ftdi->response[5];
1044 data |= ftdi->response[4];
1045 *respond->value = data;
1046 *respond->result = 0;
1047 complete(&respond->wait_completion);
1052 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1053 if (buscmd == 0x00) {
1054 } else if (buscmd == 0x02) {
1055 } else if (buscmd == 0x06) {
1056 } else if (buscmd == 0x0A) {
1058 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1062 if ((ftdi->response[0] & 0x80) == 0x00) {
1066 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1067 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1068 u16 ed_length = (ftdi->response[2] << 8) |
1070 struct u132_target *target = &ftdi->target[
1072 target->halted = (ftdi->response[0] >> 3) &
1074 target->skipped = (ftdi->response[0] >> 2) &
1076 target->toggle_bits = (ftdi->response[3] >> 6)
1078 target->error_count = (ftdi->response[3] >> 4)
1080 target->condition_code = (ftdi->response[
1082 if ((ftdi->response[0] & 0x10) == 0x00) {
1083 b = have_ed_set_response(ftdi, target,
1084 ed_length, ed_number, ed_type,
1088 b = have_ed_get_response(ftdi, target,
1089 ed_length, ed_number, ed_type,
1101 * create a urb, and a buffer for it, and copy the data to the urb
1104 static ssize_t ftdi_elan_write(struct file *file,
1105 const char __user *user_buffer, size_t count,
1111 struct usb_ftdi *ftdi = file->private_data;
1113 if (ftdi->disconnected > 0) {
1119 urb = usb_alloc_urb(0, GFP_KERNEL);
1124 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1125 &urb->transfer_dma);
1130 if (copy_from_user(buf, user_buffer, count)) {
1134 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1135 ftdi->bulk_out_endpointAddr), buf, count,
1136 ftdi_elan_write_bulk_callback, ftdi);
1137 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1138 retval = usb_submit_urb(urb, GFP_KERNEL);
1140 dev_err(&ftdi->udev->dev,
1141 "failed submitting write urb, error %d\n", retval);
1149 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1156 static const struct file_operations ftdi_elan_fops = {
1157 .owner = THIS_MODULE,
1158 .llseek = no_llseek,
1159 .read = ftdi_elan_read,
1160 .write = ftdi_elan_write,
1161 .open = ftdi_elan_open,
1162 .release = ftdi_elan_release,
1166 * usb class driver info in order to get a minor number from the usb core,
1167 * and to have the device registered with the driver core
1169 static struct usb_class_driver ftdi_elan_jtag_class = {
1170 .name = "ftdi-%d-jtag",
1171 .fops = &ftdi_elan_fops,
1172 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1176 * the following definitions are for the
1177 * ELAN FPGA state machgine processor that
1178 * lies on the other side of the FTDI chip
1180 #define cPCIu132rd 0x0
1181 #define cPCIu132wr 0x1
1182 #define cPCIiord 0x2
1183 #define cPCIiowr 0x3
1184 #define cPCImemrd 0x6
1185 #define cPCImemwr 0x7
1186 #define cPCIcfgrd 0xA
1187 #define cPCIcfgwr 0xB
1188 #define cPCInull 0xF
1189 #define cU132cmd_status 0x0
1190 #define cU132flash 0x1
1191 #define cPIDsetup 0x0
1194 #define cPIDinonce 0x3
1195 #define cCCnoerror 0x0
1197 #define cCCbitstuff 0x2
1198 #define cCCtoggle 0x3
1199 #define cCCstall 0x4
1200 #define cCCnoresp 0x5
1201 #define cCCbadpid1 0x6
1202 #define cCCbadpid2 0x7
1203 #define cCCdataoverrun 0x8
1204 #define cCCdataunderrun 0x9
1205 #define cCCbuffoverrun 0xC
1206 #define cCCbuffunderrun 0xD
1207 #define cCCnotaccessed 0xF
1208 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1210 wait:if (ftdi->disconnected > 0) {
1214 mutex_lock(&ftdi->u132_lock);
1215 command_size = ftdi->command_next - ftdi->command_head;
1216 if (command_size < COMMAND_SIZE) {
1217 struct u132_command *command = &ftdi->command[
1218 COMMAND_MASK & ftdi->command_next];
1219 command->header = 0x00 | cPCIu132wr;
1220 command->length = 0x04;
1221 command->address = 0x00;
1222 command->width = 0x00;
1223 command->follows = 4;
1224 command->value = data;
1225 command->buffer = &command->value;
1226 ftdi->command_next += 1;
1227 ftdi_elan_kick_command_queue(ftdi);
1228 mutex_unlock(&ftdi->u132_lock);
1231 mutex_unlock(&ftdi->u132_lock);
1238 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1241 u8 addressofs = config_offset / 4;
1242 wait:if (ftdi->disconnected > 0) {
1246 mutex_lock(&ftdi->u132_lock);
1247 command_size = ftdi->command_next - ftdi->command_head;
1248 if (command_size < COMMAND_SIZE) {
1249 struct u132_command *command = &ftdi->command[
1250 COMMAND_MASK & ftdi->command_next];
1251 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1252 command->length = 0x04;
1253 command->address = addressofs;
1254 command->width = 0x00 | (width & 0x0F);
1255 command->follows = 4;
1256 command->value = data;
1257 command->buffer = &command->value;
1258 ftdi->command_next += 1;
1259 ftdi_elan_kick_command_queue(ftdi);
1260 mutex_unlock(&ftdi->u132_lock);
1263 mutex_unlock(&ftdi->u132_lock);
1270 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1273 u8 addressofs = mem_offset / 4;
1274 wait:if (ftdi->disconnected > 0) {
1278 mutex_lock(&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 | (cPCImemwr & 0x0F);
1284 command->length = 0x04;
1285 command->address = addressofs;
1286 command->width = 0x00 | (width & 0x0F);
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 mutex_unlock(&ftdi->u132_lock);
1295 mutex_unlock(&ftdi->u132_lock);
1302 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1305 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1306 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1310 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1311 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1313 wait:if (ftdi->disconnected > 0) {
1318 mutex_lock(&ftdi->u132_lock);
1319 command_size = ftdi->command_next - ftdi->command_head;
1320 respond_size = ftdi->respond_next - ftdi->respond_head;
1321 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1323 struct u132_command *command = &ftdi->command[
1324 COMMAND_MASK & ftdi->command_next];
1325 struct u132_respond *respond = &ftdi->respond[
1326 RESPOND_MASK & ftdi->respond_next];
1327 int result = -ENODEV;
1328 respond->result = &result;
1329 respond->header = command->header = 0x00 | cPCIu132rd;
1330 command->length = 0x04;
1331 respond->address = command->address = cU132cmd_status;
1332 command->width = 0x00;
1333 command->follows = 0;
1335 command->buffer = NULL;
1336 respond->value = data;
1337 init_completion(&respond->wait_completion);
1338 ftdi->command_next += 1;
1339 ftdi->respond_next += 1;
1340 ftdi_elan_kick_command_queue(ftdi);
1341 mutex_unlock(&ftdi->u132_lock);
1342 wait_for_completion(&respond->wait_completion);
1345 mutex_unlock(&ftdi->u132_lock);
1352 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1353 u8 width, u32 *data)
1355 u8 addressofs = config_offset / 4;
1356 wait:if (ftdi->disconnected > 0) {
1361 mutex_lock(&ftdi->u132_lock);
1362 command_size = ftdi->command_next - ftdi->command_head;
1363 respond_size = ftdi->respond_next - ftdi->respond_head;
1364 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1366 struct u132_command *command = &ftdi->command[
1367 COMMAND_MASK & ftdi->command_next];
1368 struct u132_respond *respond = &ftdi->respond[
1369 RESPOND_MASK & ftdi->respond_next];
1370 int result = -ENODEV;
1371 respond->result = &result;
1372 respond->header = command->header = 0x00 | (cPCIcfgrd &
1374 command->length = 0x04;
1375 respond->address = command->address = addressofs;
1376 command->width = 0x00 | (width & 0x0F);
1377 command->follows = 0;
1379 command->buffer = NULL;
1380 respond->value = data;
1381 init_completion(&respond->wait_completion);
1382 ftdi->command_next += 1;
1383 ftdi->respond_next += 1;
1384 ftdi_elan_kick_command_queue(ftdi);
1385 mutex_unlock(&ftdi->u132_lock);
1386 wait_for_completion(&respond->wait_completion);
1389 mutex_unlock(&ftdi->u132_lock);
1396 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1397 u8 width, u32 *data)
1399 u8 addressofs = mem_offset / 4;
1400 wait:if (ftdi->disconnected > 0) {
1405 mutex_lock(&ftdi->u132_lock);
1406 command_size = ftdi->command_next - ftdi->command_head;
1407 respond_size = ftdi->respond_next - ftdi->respond_head;
1408 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1410 struct u132_command *command = &ftdi->command[
1411 COMMAND_MASK & ftdi->command_next];
1412 struct u132_respond *respond = &ftdi->respond[
1413 RESPOND_MASK & ftdi->respond_next];
1414 int result = -ENODEV;
1415 respond->result = &result;
1416 respond->header = command->header = 0x00 | (cPCImemrd &
1418 command->length = 0x04;
1419 respond->address = command->address = addressofs;
1420 command->width = 0x00 | (width & 0x0F);
1421 command->follows = 0;
1423 command->buffer = NULL;
1424 respond->value = data;
1425 init_completion(&respond->wait_completion);
1426 ftdi->command_next += 1;
1427 ftdi->respond_next += 1;
1428 ftdi_elan_kick_command_queue(ftdi);
1429 mutex_unlock(&ftdi->u132_lock);
1430 wait_for_completion(&respond->wait_completion);
1433 mutex_unlock(&ftdi->u132_lock);
1440 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1441 u8 width, u32 *data)
1443 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1444 if (ftdi->initialized == 0) {
1447 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1451 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1452 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1453 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1454 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1455 int toggle_bits, int error_count, int condition_code, int repeat_number,
1456 int halted, int skipped, int actual, int non_null))
1458 u8 ed = ed_number - 1;
1459 wait:if (ftdi->disconnected > 0) {
1461 } else if (ftdi->initialized == 0) {
1465 mutex_lock(&ftdi->u132_lock);
1466 command_size = ftdi->command_next - ftdi->command_head;
1467 if (command_size < COMMAND_SIZE) {
1468 struct u132_target *target = &ftdi->target[ed];
1469 struct u132_command *command = &ftdi->command[
1470 COMMAND_MASK & ftdi->command_next];
1471 command->header = 0x80 | (ed << 5);
1472 command->length = 0x8007;
1473 command->address = (toggle_bits << 6) | (ep_number << 2)
1475 command->width = usb_maxpacket(urb->dev, urb->pipe,
1476 usb_pipeout(urb->pipe));
1477 command->follows = 8;
1479 command->buffer = urb->setup_packet;
1480 target->callback = callback;
1481 target->endp = endp;
1484 ftdi->command_next += 1;
1485 ftdi_elan_kick_command_queue(ftdi);
1486 mutex_unlock(&ftdi->u132_lock);
1489 mutex_unlock(&ftdi->u132_lock);
1496 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1497 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1498 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1499 int toggle_bits, int error_count, int condition_code, int repeat_number,
1500 int halted, int skipped, int actual, int non_null))
1502 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1503 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1504 ep_number, toggle_bits, callback);
1508 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1509 static int ftdi_elan_edset_input(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 mutex_lock(&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 u32 remaining_length = urb->transfer_buffer_length -
1530 command->header = 0x82 | (ed << 5);
1531 if (remaining_length == 0) {
1532 command->length = 0x0000;
1533 } else if (remaining_length > 1024) {
1534 command->length = 0x8000 | 1023;
1536 command->length = 0x8000 | (remaining_length -
1538 command->address = (toggle_bits << 6) | (ep_number << 2)
1540 command->width = usb_maxpacket(urb->dev, urb->pipe,
1541 usb_pipeout(urb->pipe));
1542 command->follows = 0;
1544 command->buffer = NULL;
1545 target->callback = callback;
1546 target->endp = endp;
1549 ftdi->command_next += 1;
1550 ftdi_elan_kick_command_queue(ftdi);
1551 mutex_unlock(&ftdi->u132_lock);
1554 mutex_unlock(&ftdi->u132_lock);
1561 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1562 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1563 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1564 int toggle_bits, int error_count, int condition_code, int repeat_number,
1565 int halted, int skipped, int actual, int non_null))
1567 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1568 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1569 ep_number, toggle_bits, callback);
1573 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1574 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1575 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1576 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1577 int toggle_bits, int error_count, int condition_code, int repeat_number,
1578 int halted, int skipped, int actual, int non_null))
1580 u8 ed = ed_number - 1;
1581 wait:if (ftdi->disconnected > 0) {
1583 } else if (ftdi->initialized == 0) {
1587 mutex_lock(&ftdi->u132_lock);
1588 command_size = ftdi->command_next - ftdi->command_head;
1589 if (command_size < COMMAND_SIZE) {
1590 struct u132_target *target = &ftdi->target[ed];
1591 struct u132_command *command = &ftdi->command[
1592 COMMAND_MASK & ftdi->command_next];
1593 command->header = 0x81 | (ed << 5);
1594 command->length = 0x0000;
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 mutex_unlock(&ftdi->u132_lock);
1611 mutex_unlock(&ftdi->u132_lock);
1618 int usb_ftdi_elan_edset_empty(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_empty(ftdi, ed_number, endp, urb, address,
1626 ep_number, toggle_bits, callback);
1630 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1631 static int ftdi_elan_edset_output(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 mutex_lock(&ftdi->u132_lock);
1645 command_size = ftdi->command_next - ftdi->command_head;
1646 if (command_size < COMMAND_SIZE) {
1650 char data[30 *3 + 4];
1652 int m = (sizeof(data) - 1) / 3 - 1;
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->address = (toggle_bits << 6) | (ep_number << 2)
1660 command->width = usb_maxpacket(urb->dev, urb->pipe,
1661 usb_pipeout(urb->pipe));
1662 command->follows = min_t(u32, 1024,
1663 urb->transfer_buffer_length -
1664 urb->actual_length);
1666 command->buffer = urb->transfer_buffer +
1668 command->length = 0x8000 | (command->follows - 1);
1669 b = command->buffer;
1670 urb_size = command->follows;
1672 while (urb_size-- > 0) {
1674 } else if (i++ < m) {
1675 int w = sprintf(d, " %02X", *b++);
1679 d += sprintf(d, " ..");
1681 target->callback = callback;
1682 target->endp = endp;
1685 ftdi->command_next += 1;
1686 ftdi_elan_kick_command_queue(ftdi);
1687 mutex_unlock(&ftdi->u132_lock);
1690 mutex_unlock(&ftdi->u132_lock);
1697 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1698 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1699 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1700 int toggle_bits, int error_count, int condition_code, int repeat_number,
1701 int halted, int skipped, int actual, int non_null))
1703 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1704 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1705 ep_number, toggle_bits, callback);
1709 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1710 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1711 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1712 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1713 int toggle_bits, int error_count, int condition_code, int repeat_number,
1714 int halted, int skipped, int actual, int non_null))
1716 u8 ed = ed_number - 1;
1717 wait:if (ftdi->disconnected > 0) {
1719 } else if (ftdi->initialized == 0) {
1723 mutex_lock(&ftdi->u132_lock);
1724 command_size = ftdi->command_next - ftdi->command_head;
1725 if (command_size < COMMAND_SIZE) {
1726 u32 remaining_length = urb->transfer_buffer_length -
1728 struct u132_target *target = &ftdi->target[ed];
1729 struct u132_command *command = &ftdi->command[
1730 COMMAND_MASK & ftdi->command_next];
1731 command->header = 0x83 | (ed << 5);
1732 if (remaining_length == 0) {
1733 command->length = 0x0000;
1734 } else if (remaining_length > 1024) {
1735 command->length = 0x8000 | 1023;
1737 command->length = 0x8000 | (remaining_length -
1739 command->address = (toggle_bits << 6) | (ep_number << 2)
1741 command->width = usb_maxpacket(urb->dev, urb->pipe,
1742 usb_pipeout(urb->pipe));
1743 command->follows = 0;
1745 command->buffer = NULL;
1746 target->callback = callback;
1747 target->endp = endp;
1750 ftdi->command_next += 1;
1751 ftdi_elan_kick_command_queue(ftdi);
1752 mutex_unlock(&ftdi->u132_lock);
1755 mutex_unlock(&ftdi->u132_lock);
1762 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1763 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1764 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1765 int toggle_bits, int error_count, int condition_code, int repeat_number,
1766 int halted, int skipped, int actual, int non_null))
1768 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1769 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1770 ep_number, toggle_bits, callback);
1774 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1775 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1778 u8 ed = ed_number - 1;
1779 if (ftdi->disconnected > 0) {
1781 } else if (ftdi->initialized == 0) {
1784 struct u132_target *target = &ftdi->target[ed];
1785 mutex_lock(&ftdi->u132_lock);
1786 if (target->abandoning > 0) {
1787 mutex_unlock(&ftdi->u132_lock);
1790 target->abandoning = 1;
1791 wait_1:if (target->active == 1) {
1792 int command_size = ftdi->command_next -
1794 if (command_size < COMMAND_SIZE) {
1795 struct u132_command *command =
1796 &ftdi->command[COMMAND_MASK &
1797 ftdi->command_next];
1798 command->header = 0x80 | (ed << 5) |
1800 command->length = 0x00;
1801 command->address = 0x00;
1802 command->width = 0x00;
1803 command->follows = 0;
1805 command->buffer = &command->value;
1806 ftdi->command_next += 1;
1807 ftdi_elan_kick_command_queue(ftdi);
1809 mutex_unlock(&ftdi->u132_lock);
1811 mutex_lock(&ftdi->u132_lock);
1815 mutex_unlock(&ftdi->u132_lock);
1821 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1824 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1825 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1829 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1830 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1832 int retry_on_empty = 10;
1833 int retry_on_timeout = 5;
1834 int retry_on_status = 20;
1836 int packet_bytes = 0;
1837 int retval = usb_bulk_msg(ftdi->udev,
1838 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1839 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1840 &packet_bytes, 100);
1841 if (packet_bytes > 2) {
1842 char diag[30 *3 + 4];
1844 int m = (sizeof(diag) - 1) / 3 - 1;
1845 char *b = ftdi->bulk_in_buffer;
1848 while (packet_bytes-- > 0) {
1850 if (bytes_read < m) {
1851 d += sprintf(d, " %02X",
1853 } else if (bytes_read > m) {
1855 d += sprintf(d, " ..");
1860 } else if (packet_bytes > 1) {
1861 char s1 = ftdi->bulk_in_buffer[0];
1862 char s2 = ftdi->bulk_in_buffer[1];
1863 if (s1 == 0x31 && s2 == 0x60) {
1865 } else if (retry_on_status-- > 0) {
1868 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1871 } else if (packet_bytes > 0) {
1872 char b1 = ftdi->bulk_in_buffer[0];
1873 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1875 if (retry_on_status-- > 0) {
1878 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1881 } else if (retval == -ETIMEDOUT) {
1882 if (retry_on_timeout-- > 0) {
1885 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1888 } else if (retval == 0) {
1889 if (retry_on_empty-- > 0) {
1892 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1896 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1905 * send the long flush sequence
1908 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1915 urb = usb_alloc_urb(0, GFP_KERNEL);
1918 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1920 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1926 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1927 ftdi->bulk_out_endpointAddr), buf, i,
1928 ftdi_elan_write_bulk_callback, ftdi);
1929 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1930 retval = usb_submit_urb(urb, GFP_KERNEL);
1932 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1933 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1943 * send the reset sequence
1946 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1953 urb = usb_alloc_urb(0, GFP_KERNEL);
1956 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1958 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1966 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1967 ftdi->bulk_out_endpointAddr), buf, i,
1968 ftdi_elan_write_bulk_callback, ftdi);
1969 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1970 retval = usb_submit_urb(urb, GFP_KERNEL);
1972 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1973 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1981 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1985 int retry_on_timeout = 5;
1986 int retry_on_empty = 10;
1988 retval = ftdi_elan_flush_input_fifo(ftdi);
1991 ftdi->bulk_in_left = 0;
1992 ftdi->bulk_in_last = -1;
1993 while (long_stop-- > 0) {
1996 retval = ftdi_elan_synchronize_flush(ftdi);
1999 retval = ftdi_elan_flush_input_fifo(ftdi);
2002 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2008 int packet_bytes = 0;
2009 retval = usb_bulk_msg(ftdi->udev,
2010 usb_rcvbulkpipe(ftdi->udev,
2011 ftdi->bulk_in_endpointAddr),
2012 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2013 &packet_bytes, 500);
2014 if (packet_bytes > 2) {
2015 char diag[30 *3 + 4];
2017 int m = (sizeof(diag) - 1) / 3 - 1;
2018 char *b = ftdi->bulk_in_buffer;
2020 unsigned char c = 0;
2022 while (packet_bytes-- > 0) {
2024 if (bytes_read < m) {
2025 d += sprintf(d, " %02X", c);
2026 } else if (bytes_read > m) {
2028 d += sprintf(d, " ..");
2037 } else if (read_stop-- > 0) {
2040 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2044 } else if (packet_bytes > 1) {
2045 unsigned char s1 = ftdi->bulk_in_buffer[0];
2046 unsigned char s2 = ftdi->bulk_in_buffer[1];
2047 if (s1 == 0x31 && s2 == 0x00) {
2048 if (read_stuck-- > 0) {
2052 } else if (s1 == 0x31 && s2 == 0x60) {
2053 if (read_stop-- > 0) {
2056 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2060 if (read_stop-- > 0) {
2063 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2067 } else if (packet_bytes > 0) {
2068 if (read_stop-- > 0) {
2071 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2074 } else if (retval == -ETIMEDOUT) {
2075 if (retry_on_timeout-- > 0) {
2078 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2081 } else if (retval == 0) {
2082 if (retry_on_empty-- > 0) {
2085 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2090 dev_err(&ftdi->udev->dev, "error = %d\n",
2092 if (read_stop-- > 0) {
2095 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2101 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2105 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2107 int retry_on_empty = 10;
2108 int retry_on_timeout = 5;
2109 int retry_on_status = 50;
2111 int packet_bytes = 0;
2112 int retval = usb_bulk_msg(ftdi->udev,
2113 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2114 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2115 &packet_bytes, 1000);
2116 if (packet_bytes > 2) {
2117 char diag[30 *3 + 4];
2119 int m = (sizeof(diag) - 1) / 3 - 1;
2120 char *b = ftdi->bulk_in_buffer;
2123 while (packet_bytes-- > 0) {
2125 if (bytes_read < m) {
2126 d += sprintf(d, " %02X",
2128 } else if (bytes_read > m) {
2130 d += sprintf(d, " ..");
2135 } else if (packet_bytes > 1) {
2136 char s1 = ftdi->bulk_in_buffer[0];
2137 char s2 = ftdi->bulk_in_buffer[1];
2138 if (s1 == 0x31 && s2 == 0x60) {
2140 } else if (retry_on_status-- > 0) {
2145 } else if (packet_bytes > 0) {
2146 char b1 = ftdi->bulk_in_buffer[0];
2147 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2148 if (retry_on_status-- > 0) {
2152 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2155 } else if (retval == -ETIMEDOUT) {
2156 if (retry_on_timeout-- > 0) {
2159 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2162 } else if (retval == 0) {
2163 if (retry_on_empty-- > 0) {
2166 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2170 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2177 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2179 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2182 if (ftdi->controlreg & 0x00400000) {
2183 if (ftdi->card_ejected) {
2185 ftdi->card_ejected = 1;
2186 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2191 u8 fn = ftdi->function - 1;
2192 int activePCIfn = fn << 8;
2197 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2201 pciVID = pcidata & 0xFFFF;
2202 pciPID = (pcidata >> 16) & 0xFFFF;
2203 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2204 ftdi->platform_data.device) {
2207 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2208 ftdi->platform_data.vendor, pciVID,
2209 ftdi->platform_data.device, pciPID);
2216 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2217 offsetof(struct ohci_regs, member), 0, data);
2218 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2219 offsetof(struct ohci_regs, member), 0, data);
2221 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2222 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2224 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2238 int mask = OHCI_INTR_INIT;
2240 int reset_timeout = 30; /* ... allow extra time */
2242 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2245 retval = ftdi_read_pcimem(ftdi, control, &control);
2248 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2251 num_ports = rh_a & RH_A_NDP;
2252 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2255 hc_fminterval &= 0x3fff;
2256 if (hc_fminterval != FI) {
2258 hc_fminterval |= FSMP(hc_fminterval) << 16;
2259 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2262 switch (hc_control & OHCI_CTRL_HCFS) {
2266 case OHCI_USB_SUSPEND:
2267 case OHCI_USB_RESUME:
2268 hc_control &= OHCI_CTRL_RWC;
2269 hc_control |= OHCI_USB_RESUME;
2273 hc_control &= OHCI_CTRL_RWC;
2274 hc_control |= OHCI_USB_RESET;
2278 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2281 retval = ftdi_read_pcimem(ftdi, control, &control);
2285 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2288 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2289 for (temp = 0; temp < num_ports; temp++) {
2290 retval = ftdi_write_pcimem(ftdi,
2291 roothub.portstatus[temp], RH_PS_LSDA);
2296 retval = ftdi_read_pcimem(ftdi, control, &control);
2299 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2302 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2306 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2309 if (0 != (status & OHCI_HCR)) {
2310 if (--reset_timeout == 0) {
2311 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2319 if (quirk & OHCI_QUIRK_INITRESET) {
2320 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2323 retval = ftdi_read_pcimem(ftdi, control, &control);
2327 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2330 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2333 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2336 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2339 retval = ftdi_write_pcimem(ftdi, fminterval,
2340 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2343 retval = ftdi_write_pcimem(ftdi, periodicstart,
2344 ((9 *hc_fminterval) / 10) & 0x3fff);
2347 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2350 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2353 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2354 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2355 quirk |= OHCI_QUIRK_INITRESET;
2358 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2359 fminterval, periodicstart);
2360 } /* start controller operations */
2361 hc_control &= OHCI_CTRL_RWC;
2362 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2363 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2366 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2369 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2372 retval = ftdi_read_pcimem(ftdi, control, &control);
2375 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2378 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2381 retval = ftdi_write_pcimem(ftdi, intrdisable,
2382 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2383 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2386 return retval; /* handle root hub init quirks ... */
2387 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2390 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2391 if (quirk & OHCI_QUIRK_SUPERIO) {
2392 roothub_a |= RH_A_NOCP;
2393 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2394 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2397 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2398 roothub_a |= RH_A_NPS;
2399 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2403 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2406 retval = ftdi_write_pcimem(ftdi, roothub.b,
2407 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2410 retval = ftdi_read_pcimem(ftdi, control, &control);
2413 mdelay((roothub_a >> 23) & 0x1fe);
2414 for (temp = 0; temp < num_ports; temp++) {
2416 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2426 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2432 int activePCIfn = fn << 8;
2433 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2437 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2441 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2445 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2449 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2454 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2458 latence_timer &= 0xFFFF00FF;
2459 latence_timer |= 0x00001600;
2460 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2464 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2469 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2473 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2477 for (reg = 0; reg <= 0x54; reg += 4) {
2478 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2485 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2491 int activePCIfn = fn << 8;
2492 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2496 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2500 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2504 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2508 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2513 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2517 latence_timer &= 0xFFFF00FF;
2518 latence_timer |= 0x00001600;
2519 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2523 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2528 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2532 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2535 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2539 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2542 result = ftdi_elan_check_controller(ftdi, quirk);
2543 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2549 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2554 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2557 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2561 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2564 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2567 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2570 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2573 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2577 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2580 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2583 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2586 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2589 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2593 sensebits = (controlreg >> 16) & 0x000F;
2594 if (0x0D == sensebits)
2600 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2606 int activePCIfn = 0;
2607 int max_devices = 0;
2608 int controllers = 0;
2609 int unrecognized = 0;
2611 for (fn = 0; (fn < 4); fn++) {
2615 activePCIfn = fn << 8;
2616 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2620 pciVID = pcidata & 0xFFFF;
2621 pciPID = (pcidata >> 16) & 0xFFFF;
2622 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2623 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2625 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2627 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2629 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2630 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2632 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2634 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2636 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2637 devices = ftdi_elan_found_controller(ftdi, fn,
2640 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2641 devices = ftdi_elan_found_controller(ftdi, fn,
2642 OHCI_QUIRK_ZFMICRO);
2644 } else if (0 == pcidata) {
2647 if (devices > max_devices) {
2648 max_devices = devices;
2649 ftdi->function = fn + 1;
2650 ftdi->platform_data.vendor = pciVID;
2651 ftdi->platform_data.device = pciPID;
2654 if (ftdi->function > 0) {
2655 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2656 } else if (controllers > 0) {
2658 } else if (unrecognized > 0) {
2661 ftdi->enumerated = 0;
2668 * we use only the first bulk-in and bulk-out endpoints
2670 static int ftdi_elan_probe(struct usb_interface *interface,
2671 const struct usb_device_id *id)
2673 struct usb_host_interface *iface_desc;
2674 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2676 struct usb_ftdi *ftdi;
2678 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2682 mutex_lock(&ftdi_module_lock);
2683 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2684 ftdi->sequence_num = ++ftdi_instances;
2685 mutex_unlock(&ftdi_module_lock);
2686 ftdi_elan_init_kref(ftdi);
2687 sema_init(&ftdi->sw_lock, 1);
2688 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2689 ftdi->interface = interface;
2690 mutex_init(&ftdi->u132_lock);
2693 iface_desc = interface->cur_altsetting;
2694 retval = usb_find_common_endpoints(iface_desc,
2695 &bulk_in, &bulk_out, NULL, NULL);
2697 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2701 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2702 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2703 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2704 if (!ftdi->bulk_in_buffer) {
2709 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2711 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2712 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2713 ftdi->bulk_out_endpointAddr);
2714 usb_set_intfdata(interface, ftdi);
2715 if (iface_desc->desc.bInterfaceNumber == 0 &&
2716 ftdi->bulk_in_endpointAddr == 0x81 &&
2717 ftdi->bulk_out_endpointAddr == 0x02) {
2718 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2720 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2721 usb_set_intfdata(interface, NULL);
2725 ftdi->class = &ftdi_elan_jtag_class;
2726 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2727 ftdi, iface_desc->desc.bInterfaceNumber,
2731 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2732 ftdi->bulk_in_endpointAddr == 0x83 &&
2733 ftdi->bulk_out_endpointAddr == 0x04) {
2735 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2736 ftdi, iface_desc->desc.bInterfaceNumber);
2737 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2738 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2739 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2740 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2743 dev_err(&ftdi->udev->dev,
2744 "Could not find ELAN's U132 device\n");
2749 ftdi_elan_put_kref(ftdi);
2754 static void ftdi_elan_disconnect(struct usb_interface *interface)
2756 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2757 ftdi->disconnected += 1;
2759 int minor = interface->minor;
2760 struct usb_class_driver *class = ftdi->class;
2761 usb_set_intfdata(interface, NULL);
2762 usb_deregister_dev(interface, class);
2763 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2766 ftdi_status_cancel_work(ftdi);
2767 ftdi_command_cancel_work(ftdi);
2768 ftdi_response_cancel_work(ftdi);
2769 ftdi_elan_abandon_completions(ftdi);
2770 ftdi_elan_abandon_targets(ftdi);
2771 if (ftdi->registered) {
2772 platform_device_unregister(&ftdi->platform_dev);
2773 ftdi->synchronized = 0;
2774 ftdi->enumerated = 0;
2775 ftdi->initialized = 0;
2776 ftdi->registered = 0;
2778 ftdi->disconnected += 1;
2779 usb_set_intfdata(interface, NULL);
2780 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2782 ftdi_elan_put_kref(ftdi);
2785 static struct usb_driver ftdi_elan_driver = {
2786 .name = "ftdi-elan",
2787 .probe = ftdi_elan_probe,
2788 .disconnect = ftdi_elan_disconnect,
2789 .id_table = ftdi_elan_table,
2791 static int __init ftdi_elan_init(void)
2794 pr_info("driver %s\n", ftdi_elan_driver.name);
2795 mutex_init(&ftdi_module_lock);
2796 INIT_LIST_HEAD(&ftdi_static_list);
2797 result = usb_register(&ftdi_elan_driver);
2799 pr_err("usb_register failed. Error number %d\n", result);
2805 static void __exit ftdi_elan_exit(void)
2807 struct usb_ftdi *ftdi;
2808 struct usb_ftdi *temp;
2809 usb_deregister(&ftdi_elan_driver);
2810 pr_info("ftdi_u132 driver deregistered\n");
2811 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2812 ftdi_status_cancel_work(ftdi);
2813 ftdi_command_cancel_work(ftdi);
2814 ftdi_response_cancel_work(ftdi);
2819 module_init(ftdi_elan_init);
2820 module_exit(ftdi_elan_exit);