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/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <asm/uaccess.h>
47 #include <linux/usb.h>
48 #include <linux/workqueue.h>
49 #include <linux/platform_device.h>
50 MODULE_AUTHOR("Tony Olech");
51 MODULE_DESCRIPTION("FTDI ELAN driver");
52 MODULE_LICENSE("GPL");
53 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
54 extern struct platform_driver u132_platform_driver;
55 static struct workqueue_struct *status_queue;
56 static struct workqueue_struct *command_queue;
57 static struct workqueue_struct *respond_queue;
59 * ftdi_module_lock exists to protect access to global variables
62 static struct semaphore ftdi_module_lock;
63 static int ftdi_instances = 0;
64 static struct list_head ftdi_static_list;
66 * end of the global variables protected by ftdi_module_lock
69 #define TD_DEVNOTRESP 5
70 /* Define these values to match your devices*/
71 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
72 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
73 /* table of devices that work with this driver*/
74 static struct usb_device_id ftdi_elan_table[] = {
75 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
76 { /* Terminating entry */ }
79 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
80 /* only the jtag(firmware upgrade device) interface requires
81 * a device file and corresponding minor number, but the
82 * interface is created unconditionally - I suppose it could
83 * be configured or not according to a module parameter.
84 * But since we(now) require one interface per device,
85 * and since it unlikely that a normal installation would
86 * require more than a couple of elan-ftdi devices, 8 seems
87 * like a reasonable limit to have here, and if someone
88 * really requires more than 8 devices, then they can frig the
91 #define USB_FTDI_ELAN_MINOR_BASE 192
92 #define COMMAND_BITS 5
93 #define COMMAND_SIZE (1<<COMMAND_BITS)
94 #define COMMAND_MASK (COMMAND_SIZE-1)
104 #define RESPOND_BITS 5
105 #define RESPOND_SIZE (1<<RESPOND_BITS)
106 #define RESPOND_MASK (RESPOND_SIZE-1)
107 struct u132_respond {
112 struct completion wait_completion;
127 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
128 int toggle_bits, int error_count, int condition_code,
129 int repeat_number, int halted, int skipped, int actual,
132 /* Structure to hold all of our device specific stuff*/
134 struct list_head ftdi_list;
135 struct semaphore u132_lock;
138 struct u132_command command[COMMAND_SIZE];
141 struct u132_respond respond[RESPOND_SIZE];
142 struct u132_target target[4];
143 char device_name[16];
144 unsigned synchronized:1;
145 unsigned enumerated:1;
146 unsigned registered:1;
147 unsigned initialized:1;
148 unsigned card_ejected:1;
154 int status_queue_delay;
155 struct semaphore sw_lock;
156 struct usb_device *udev;
157 struct usb_interface *interface;
158 struct usb_class_driver *class;
159 struct work_struct status_work;
160 struct work_struct command_work;
161 struct work_struct respond_work;
162 struct u132_platform_data platform_data;
163 struct resource resources[0];
164 struct platform_device platform_dev;
165 unsigned char *bulk_in_buffer;
169 __u8 bulk_in_endpointAddr;
170 __u8 bulk_out_endpointAddr;
173 u8 response[4 + 1024];
178 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
179 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
181 static struct usb_driver ftdi_elan_driver;
182 static void ftdi_elan_delete(struct kref *kref)
184 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
185 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
186 usb_put_dev(ftdi->udev);
187 ftdi->disconnected += 1;
188 down(&ftdi_module_lock);
189 list_del_init(&ftdi->ftdi_list);
191 up(&ftdi_module_lock);
192 kfree(ftdi->bulk_in_buffer);
193 ftdi->bulk_in_buffer = NULL;
196 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
198 kref_put(&ftdi->kref, ftdi_elan_delete);
201 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
203 kref_get(&ftdi->kref);
206 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
208 kref_init(&ftdi->kref);
211 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
214 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
216 } else if (queue_work(status_queue, &ftdi->status_work))
218 kref_put(&ftdi->kref, ftdi_elan_delete);
222 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
225 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
226 kref_get(&ftdi->kref);
227 } else if (queue_work(status_queue, &ftdi->status_work))
228 kref_get(&ftdi->kref);
232 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
234 if (cancel_delayed_work(&ftdi->status_work))
235 kref_put(&ftdi->kref, ftdi_elan_delete);
238 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
241 if (queue_delayed_work(command_queue, &ftdi->command_work,
244 } else if (queue_work(command_queue, &ftdi->command_work))
246 kref_put(&ftdi->kref, ftdi_elan_delete);
250 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
253 if (queue_delayed_work(command_queue, &ftdi->command_work,
255 kref_get(&ftdi->kref);
256 } else if (queue_work(command_queue, &ftdi->command_work))
257 kref_get(&ftdi->kref);
261 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
263 if (cancel_delayed_work(&ftdi->command_work))
264 kref_put(&ftdi->kref, ftdi_elan_delete);
267 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
271 if (queue_delayed_work(respond_queue, &ftdi->respond_work,
274 } else if (queue_work(respond_queue, &ftdi->respond_work))
276 kref_put(&ftdi->kref, ftdi_elan_delete);
280 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
283 if (queue_delayed_work(respond_queue, &ftdi->respond_work,
285 kref_get(&ftdi->kref);
286 } else if (queue_work(respond_queue, &ftdi->respond_work))
287 kref_get(&ftdi->kref);
291 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
293 if (cancel_delayed_work(&ftdi->respond_work))
294 kref_put(&ftdi->kref, ftdi_elan_delete);
297 void ftdi_elan_gone_away(struct platform_device *pdev)
299 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
300 ftdi->gone_away += 1;
301 ftdi_elan_put_kref(ftdi);
305 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
306 static void ftdi_release_platform_dev(struct device *dev)
311 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
312 struct u132_target *target, u8 *buffer, int length);
313 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
314 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
315 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
316 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
317 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
318 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
319 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
320 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
321 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
322 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
325 if (ftdi->platform_dev.dev.parent)
327 ftdi_elan_get_kref(ftdi);
328 ftdi->platform_data.potpg = 100;
329 ftdi->platform_data.reset = NULL;
330 ftdi->platform_dev.id = ftdi->sequence_num;
331 ftdi->platform_dev.resource = ftdi->resources;
332 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
333 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
334 ftdi->platform_dev.dev.parent = NULL;
335 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
336 ftdi->platform_dev.dev.dma_mask = NULL;
337 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
338 ftdi->platform_dev.name = ftdi->device_name;
339 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
340 request_module("u132_hcd");
341 dev_info(&ftdi->udev->dev, "registering '%s'\n",
342 ftdi->platform_dev.name);
343 result = platform_device_register(&ftdi->platform_dev);
347 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
349 down(&ftdi->u132_lock);
350 while (ftdi->respond_next > ftdi->respond_head) {
351 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
352 ftdi->respond_head++];
353 *respond->result = -ESHUTDOWN;
355 complete(&respond->wait_completion);
356 } up(&ftdi->u132_lock);
359 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
362 down(&ftdi->u132_lock);
363 while (ed_number-- > 0) {
364 struct u132_target *target = &ftdi->target[ed_number];
365 if (target->active == 1) {
366 target->condition_code = TD_DEVNOTRESP;
367 up(&ftdi->u132_lock);
368 ftdi_elan_do_callback(ftdi, target, NULL, 0);
369 down(&ftdi->u132_lock);
375 up(&ftdi->u132_lock);
378 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
381 down(&ftdi->u132_lock);
382 while (ed_number-- > 0) {
383 struct u132_target *target = &ftdi->target[ed_number];
384 target->abandoning = 1;
385 wait_1:if (target->active == 1) {
386 int command_size = ftdi->command_next -
388 if (command_size < COMMAND_SIZE) {
389 struct u132_command *command = &ftdi->command[
390 COMMAND_MASK & ftdi->command_next];
391 command->header = 0x80 | (ed_number << 5) | 0x4;
392 command->length = 0x00;
393 command->address = 0x00;
394 command->width = 0x00;
395 command->follows = 0;
397 command->buffer = &command->value;
398 ftdi->command_next += 1;
399 ftdi_elan_kick_command_queue(ftdi);
401 up(&ftdi->u132_lock);
403 down(&ftdi->u132_lock);
407 wait_2:if (target->active == 1) {
408 int command_size = ftdi->command_next -
410 if (command_size < COMMAND_SIZE) {
411 struct u132_command *command = &ftdi->command[
412 COMMAND_MASK & ftdi->command_next];
413 command->header = 0x90 | (ed_number << 5);
414 command->length = 0x00;
415 command->address = 0x00;
416 command->width = 0x00;
417 command->follows = 0;
419 command->buffer = &command->value;
420 ftdi->command_next += 1;
421 ftdi_elan_kick_command_queue(ftdi);
423 up(&ftdi->u132_lock);
425 down(&ftdi->u132_lock);
433 up(&ftdi->u132_lock);
436 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
439 down(&ftdi->u132_lock);
440 while (ed_number-- > 0) {
441 struct u132_target *target = &ftdi->target[ed_number];
442 target->abandoning = 1;
443 wait:if (target->active == 1) {
444 int command_size = ftdi->command_next -
446 if (command_size < COMMAND_SIZE) {
447 struct u132_command *command = &ftdi->command[
448 COMMAND_MASK & ftdi->command_next];
449 command->header = 0x80 | (ed_number << 5) | 0x4;
450 command->length = 0x00;
451 command->address = 0x00;
452 command->width = 0x00;
453 command->follows = 0;
455 command->buffer = &command->value;
456 ftdi->command_next += 1;
457 ftdi_elan_kick_command_queue(ftdi);
459 up(&ftdi->u132_lock);
461 down(&ftdi->u132_lock);
469 up(&ftdi->u132_lock);
472 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
474 ftdi_command_queue_work(ftdi, 0);
478 static void ftdi_elan_command_work(void *data)
480 struct usb_ftdi *ftdi = data;
481 if (ftdi->disconnected > 0) {
482 ftdi_elan_put_kref(ftdi);
485 int retval = ftdi_elan_command_engine(ftdi);
486 if (retval == -ESHUTDOWN) {
487 ftdi->disconnected += 1;
488 } else if (retval == -ENODEV) {
489 ftdi->disconnected += 1;
491 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
492 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
497 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
499 ftdi_respond_queue_work(ftdi, 0);
503 static void ftdi_elan_respond_work(void *data)
505 struct usb_ftdi *ftdi = data;
506 if (ftdi->disconnected > 0) {
507 ftdi_elan_put_kref(ftdi);
510 int retval = ftdi_elan_respond_engine(ftdi);
512 } else if (retval == -ESHUTDOWN) {
513 ftdi->disconnected += 1;
514 } else if (retval == -ENODEV) {
515 ftdi->disconnected += 1;
516 } else if (retval == -EILSEQ) {
517 ftdi->disconnected += 1;
519 ftdi->disconnected += 1;
520 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
522 if (ftdi->disconnected > 0) {
523 ftdi_elan_abandon_completions(ftdi);
524 ftdi_elan_abandon_targets(ftdi);
526 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
533 * the sw_lock is initially held and will be freed
534 * after the FTDI has been synchronized
537 static void ftdi_elan_status_work(void *data)
539 struct usb_ftdi *ftdi = data;
540 int work_delay_in_msec = 0;
541 if (ftdi->disconnected > 0) {
542 ftdi_elan_put_kref(ftdi);
544 } else if (ftdi->synchronized == 0) {
545 down(&ftdi->sw_lock);
546 if (ftdi_elan_synchronize(ftdi) == 0) {
547 ftdi->synchronized = 1;
548 ftdi_command_queue_work(ftdi, 1);
549 ftdi_respond_queue_work(ftdi, 1);
551 work_delay_in_msec = 100;
553 dev_err(&ftdi->udev->dev, "synchronize failed\n");
555 work_delay_in_msec = 10 *1000;
557 } else if (ftdi->stuck_status > 0) {
558 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
559 ftdi->stuck_status = 0;
560 ftdi->synchronized = 0;
561 } else if ((ftdi->stuck_status++ % 60) == 1) {
562 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
563 "- please remove\n");
565 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
566 "- checked %d times\n", ftdi->stuck_status);
567 work_delay_in_msec = 100;
568 } else if (ftdi->enumerated == 0) {
569 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
570 ftdi->enumerated = 1;
571 work_delay_in_msec = 250;
573 work_delay_in_msec = 1000;
574 } else if (ftdi->initialized == 0) {
575 if (ftdi_elan_setupOHCI(ftdi) == 0) {
576 ftdi->initialized = 1;
577 work_delay_in_msec = 500;
579 dev_err(&ftdi->udev->dev, "initialized failed - trying "
580 "again in 10 seconds\n");
581 work_delay_in_msec = 10 *1000;
583 } else if (ftdi->registered == 0) {
584 work_delay_in_msec = 10;
585 if (ftdi_elan_hcd_init(ftdi) == 0) {
586 ftdi->registered = 1;
588 dev_err(&ftdi->udev->dev, "register failed\n");
589 work_delay_in_msec = 250;
591 if (ftdi_elan_checkingPCI(ftdi) == 0) {
592 work_delay_in_msec = 250;
593 } else if (ftdi->controlreg & 0x00400000) {
594 if (ftdi->gone_away > 0) {
595 dev_err(&ftdi->udev->dev, "PCI device eject con"
596 "firmed platform_dev.dev.parent=%p plat"
598 ftdi->platform_dev.dev.parent,
599 &ftdi->platform_dev.dev);
600 platform_device_unregister(&ftdi->platform_dev);
601 ftdi->platform_dev.dev.parent = NULL;
602 ftdi->registered = 0;
603 ftdi->enumerated = 0;
604 ftdi->card_ejected = 0;
605 ftdi->initialized = 0;
608 ftdi_elan_flush_targets(ftdi);
609 work_delay_in_msec = 250;
611 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
613 ftdi_elan_cancel_targets(ftdi);
614 work_delay_in_msec = 500;
615 ftdi->enumerated = 0;
616 ftdi->initialized = 0;
619 if (ftdi->disconnected > 0) {
620 ftdi_elan_put_kref(ftdi);
623 ftdi_status_requeue_work(ftdi,
624 msecs_to_jiffies(work_delay_in_msec));
631 * file_operations for the jtag interface
633 * the usage count for the device is incremented on open()
634 * and decremented on release()
636 static int ftdi_elan_open(struct inode *inode, struct file *file)
638 int subminor = iminor(inode);
639 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
642 printk(KERN_ERR "can't find device for minor %d\n", subminor);
645 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
649 if (down_interruptible(&ftdi->sw_lock)) {
652 ftdi_elan_get_kref(ftdi);
653 file->private_data = ftdi;
660 static int ftdi_elan_release(struct inode *inode, struct file *file)
662 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
665 up(&ftdi->sw_lock); /* decrement the count on our device */
666 ftdi_elan_put_kref(ftdi);
671 #define FTDI_ELAN_IOC_MAGIC 0xA1
672 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
673 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
674 unsigned int cmd, unsigned long arg)
677 case FTDI_ELAN_IOCDEBUG:{
679 int size = strncpy_from_user(line,
680 (const char __user *)arg, sizeof(line));
684 printk(KERN_ERR "TODO: ioctl %s\n", line);
696 * blocking bulk reads are used to get data from the device
699 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
700 size_t count, loff_t *ppos)
702 char data[30 *3 + 4];
704 int m = (sizeof(data) - 1) / 3;
706 int retry_on_empty = 10;
707 int retry_on_timeout = 5;
708 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
709 if (ftdi->disconnected > 0) {
713 have:if (ftdi->bulk_in_left > 0) {
715 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
716 ftdi->bulk_in_left -= 1;
717 if (bytes_read < m) {
718 d += sprintf(d, " %02X", 0x000000FF & *p);
719 } else if (bytes_read > m) {
721 d += sprintf(d, " ..");
722 if (copy_to_user(buffer++, p, 1)) {
731 more:if (count > 0) {
732 int packet_bytes = 0;
733 int retval = usb_bulk_msg(ftdi->udev,
734 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
735 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
736 &packet_bytes, msecs_to_jiffies(50));
737 if (packet_bytes > 2) {
738 ftdi->bulk_in_left = packet_bytes - 2;
739 ftdi->bulk_in_last = 1;
741 } else if (retval == -ETIMEDOUT) {
742 if (retry_on_timeout-- > 0) {
744 } else if (bytes_read > 0) {
748 } else if (retval == 0) {
749 if (retry_on_empty-- > 0) {
759 static void ftdi_elan_write_bulk_callback(struct urb *urb)
761 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
762 if (urb->status && !(urb->status == -ENOENT || urb->status ==
763 -ECONNRESET || urb->status == -ESHUTDOWN)) {
764 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
765 "d\n", urb, urb->status);
767 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
768 urb->transfer_buffer, urb->transfer_dma);
771 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
772 char *buf, int command_size, int total_size)
776 int I = command_size;
777 int i = ftdi->command_head;
779 struct u132_command *command = &ftdi->command[COMMAND_MASK &
781 int F = command->follows;
782 u8 *f = command->buffer;
783 if (command->header & 0x80) {
784 ed_commands |= 1 << (0x3 & (command->header >> 5));
786 buf[b++] = command->header;
787 buf[b++] = (command->length >> 0) & 0x00FF;
788 buf[b++] = (command->length >> 8) & 0x00FF;
789 buf[b++] = command->address;
790 buf[b++] = command->width;
798 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
801 int I = command_size;
802 int i = ftdi->command_head;
804 struct u132_command *command = &ftdi->command[COMMAND_MASK &
806 total_size += 5 + command->follows;
810 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
817 int command_size = ftdi->command_next - ftdi->command_head;
818 if (command_size == 0)
820 total_size = ftdi_elan_total_command_size(ftdi, command_size);
821 urb = usb_alloc_urb(0, GFP_KERNEL);
823 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
824 "ands totaling %d bytes to the Uxxx\n", command_size,
828 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
831 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
832 "ommands totaling %d bytes to the Uxxx\n", command_size,
837 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
838 command_size, total_size);
839 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
840 ftdi->bulk_out_endpointAddr), buf, total_size,
841 ftdi_elan_write_bulk_callback, ftdi);
842 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
844 char diag[40 *3 + 4];
848 int s = (sizeof(diag) - 1) / 3;
850 while (s-- > 0 && m-- > 0) {
851 if (s > 0 || m == 0) {
852 d += sprintf(d, " %02X", *c++);
854 d += sprintf(d, " ..");
857 retval = usb_submit_urb(urb, GFP_KERNEL);
859 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
860 "%d commands totaling %d bytes to the Uxxx\n", retval,
861 urb, command_size, total_size);
862 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
866 usb_free_urb(urb); /* release our reference to this urb,
867 the USB core will eventually free it entirely */
868 ftdi->command_head += command_size;
869 ftdi_elan_kick_respond_queue(ftdi);
873 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
874 struct u132_target *target, u8 *buffer, int length)
876 struct urb *urb = target->urb;
877 int halted = target->halted;
878 int skipped = target->skipped;
879 int actual = target->actual;
880 int non_null = target->non_null;
881 int toggle_bits = target->toggle_bits;
882 int error_count = target->error_count;
883 int condition_code = target->condition_code;
884 int repeat_number = target->repeat_number;
885 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
886 int, int, int, int) = target->callback;
888 target->callback = NULL;
889 (*callback) (target->endp, urb, buffer, length, toggle_bits,
890 error_count, condition_code, repeat_number, halted, skipped,
894 static char *have_ed_set_response(struct usb_ftdi *ftdi,
895 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
898 int payload = (ed_length >> 0) & 0x07FF;
899 down(&ftdi->u132_lock);
901 target->non_null = (ed_length >> 15) & 0x0001;
902 target->repeat_number = (ed_length >> 11) & 0x000F;
903 if (ed_type == 0x02) {
904 if (payload == 0 || target->abandoning > 0) {
905 target->abandoning = 0;
906 up(&ftdi->u132_lock);
907 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
912 return ftdi->response;
914 ftdi->expected = 4 + payload;
916 up(&ftdi->u132_lock);
919 } else if (ed_type == 0x03) {
920 if (payload == 0 || target->abandoning > 0) {
921 target->abandoning = 0;
922 up(&ftdi->u132_lock);
923 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
928 return ftdi->response;
930 ftdi->expected = 4 + payload;
932 up(&ftdi->u132_lock);
935 } else if (ed_type == 0x01) {
936 target->abandoning = 0;
937 up(&ftdi->u132_lock);
938 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
943 return ftdi->response;
945 target->abandoning = 0;
946 up(&ftdi->u132_lock);
947 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
952 return ftdi->response;
956 static char *have_ed_get_response(struct usb_ftdi *ftdi,
957 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
960 down(&ftdi->u132_lock);
961 target->condition_code = TD_DEVNOTRESP;
962 target->actual = (ed_length >> 0) & 0x01FF;
963 target->non_null = (ed_length >> 15) & 0x0001;
964 target->repeat_number = (ed_length >> 11) & 0x000F;
965 up(&ftdi->u132_lock);
967 ftdi_elan_do_callback(ftdi, target, NULL, 0);
968 target->abandoning = 0;
972 return ftdi->response;
977 * The engine tries to empty the FTDI fifo
979 * all responses found in the fifo data are dispatched thus
980 * the response buffer can only ever hold a maximum sized
981 * response from the Uxxx.
984 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
986 u8 *b = ftdi->response + ftdi->recieved;
988 int retry_on_empty = 1;
989 int retry_on_timeout = 3;
990 int empty_packets = 0;
992 int packet_bytes = 0;
993 int retval = usb_bulk_msg(ftdi->udev,
994 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
995 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
996 &packet_bytes, msecs_to_jiffies(500));
997 char diag[30 *3 + 4];
999 int m = packet_bytes;
1000 u8 *c = ftdi->bulk_in_buffer;
1001 int s = (sizeof(diag) - 1) / 3;
1003 while (s-- > 0 && m-- > 0) {
1004 if (s > 0 || m == 0) {
1005 d += sprintf(d, " %02X", *c++);
1007 d += sprintf(d, " ..");
1009 if (packet_bytes > 2) {
1010 ftdi->bulk_in_left = packet_bytes - 2;
1011 ftdi->bulk_in_last = 1;
1013 } else if (retval == -ETIMEDOUT) {
1014 if (retry_on_timeout-- > 0) {
1015 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1016 "t_bytes = %d with total %d bytes%s\n",
1017 packet_bytes, bytes_read, diag);
1019 } else if (bytes_read > 0) {
1020 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
1024 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1025 "t_bytes = %d with total %d bytes%s\n",
1026 packet_bytes, bytes_read, diag);
1029 } else if (retval == -EILSEQ) {
1030 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1031 " = %d with total %d bytes%s\n", retval,
1032 packet_bytes, bytes_read, diag);
1034 } else if (retval) {
1035 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1036 " = %d with total %d bytes%s\n", retval,
1037 packet_bytes, bytes_read, diag);
1039 } else if (packet_bytes == 2) {
1040 unsigned char s0 = ftdi->bulk_in_buffer[0];
1041 unsigned char s1 = ftdi->bulk_in_buffer[1];
1043 if (s0 == 0x31 && s1 == 0x60) {
1044 if (retry_on_empty-- > 0) {
1048 } else if (s0 == 0x31 && s1 == 0x00) {
1049 if (retry_on_empty-- > 0) {
1054 if (retry_on_empty-- > 0) {
1059 } else if (packet_bytes == 1) {
1060 if (retry_on_empty-- > 0) {
1065 if (retry_on_empty-- > 0) {
1074 have:if (ftdi->bulk_in_left > 0) {
1075 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1077 ftdi->bulk_in_left -= 1;
1078 if (ftdi->recieved == 0 && c == 0xFF) {
1082 if (++ftdi->recieved < ftdi->expected) {
1084 } else if (ftdi->ed_found) {
1085 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1086 u16 ed_length = (ftdi->response[2] << 8) |
1088 struct u132_target *target = &ftdi->target[ed_number];
1089 int payload = (ed_length >> 0) & 0x07FF;
1090 char diag[30 *3 + 4];
1093 u8 *c = 4 + ftdi->response;
1094 int s = (sizeof(diag) - 1) / 3;
1096 while (s-- > 0 && m-- > 0) {
1097 if (s > 0 || m == 0) {
1098 d += sprintf(d, " %02X", *c++);
1100 d += sprintf(d, " ..");
1102 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1109 } else if (ftdi->expected == 8) {
1111 int respond_head = ftdi->respond_head++;
1112 struct u132_respond *respond = &ftdi->respond[
1113 RESPOND_MASK & respond_head];
1114 u32 data = ftdi->response[7];
1116 data |= ftdi->response[6];
1118 data |= ftdi->response[5];
1120 data |= ftdi->response[4];
1121 *respond->value = data;
1122 *respond->result = 0;
1123 complete(&respond->wait_completion);
1128 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1129 if (buscmd == 0x00) {
1130 } else if (buscmd == 0x02) {
1131 } else if (buscmd == 0x06) {
1132 } else if (buscmd == 0x0A) {
1134 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1135 "lue = %08X\n", buscmd, data);
1138 if ((ftdi->response[0] & 0x80) == 0x00) {
1142 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1143 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1144 u16 ed_length = (ftdi->response[2] << 8) |
1146 struct u132_target *target = &ftdi->target[
1148 target->halted = (ftdi->response[0] >> 3) &
1150 target->skipped = (ftdi->response[0] >> 2) &
1152 target->toggle_bits = (ftdi->response[3] >> 6)
1154 target->error_count = (ftdi->response[3] >> 4)
1156 target->condition_code = (ftdi->response[
1158 if ((ftdi->response[0] & 0x10) == 0x00) {
1159 b = have_ed_set_response(ftdi, target,
1160 ed_length, ed_number, ed_type,
1164 b = have_ed_get_response(ftdi, target,
1165 ed_length, ed_number, ed_type,
1177 * create a urb, and a buffer for it, and copy the data to the urb
1180 static ssize_t ftdi_elan_write(struct file *file,
1181 const char __user *user_buffer, size_t count,
1187 struct usb_ftdi *ftdi = file->private_data;
1189 if (ftdi->disconnected > 0) {
1195 urb = usb_alloc_urb(0, GFP_KERNEL);
1200 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1201 &urb->transfer_dma);
1206 if (copy_from_user(buf, user_buffer, count)) {
1210 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1211 ftdi->bulk_out_endpointAddr), buf, count,
1212 ftdi_elan_write_bulk_callback, ftdi);
1213 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1214 retval = usb_submit_urb(urb, GFP_KERNEL);
1216 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1225 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1232 static struct file_operations ftdi_elan_fops = {
1233 .owner = THIS_MODULE,
1234 .llseek = no_llseek,
1235 .ioctl = ftdi_elan_ioctl,
1236 .read = ftdi_elan_read,
1237 .write = ftdi_elan_write,
1238 .open = ftdi_elan_open,
1239 .release = ftdi_elan_release,
1243 * usb class driver info in order to get a minor number from the usb core,
1244 * and to have the device registered with the driver core
1246 static struct usb_class_driver ftdi_elan_jtag_class = {
1247 .name = "ftdi-%d-jtag",
1248 .fops = &ftdi_elan_fops,
1249 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1253 * the following definitions are for the
1254 * ELAN FPGA state machgine processor that
1255 * lies on the other side of the FTDI chip
1257 #define cPCIu132rd 0x0
1258 #define cPCIu132wr 0x1
1259 #define cPCIiord 0x2
1260 #define cPCIiowr 0x3
1261 #define cPCImemrd 0x6
1262 #define cPCImemwr 0x7
1263 #define cPCIcfgrd 0xA
1264 #define cPCIcfgwr 0xB
1265 #define cPCInull 0xF
1266 #define cU132cmd_status 0x0
1267 #define cU132flash 0x1
1268 #define cPIDsetup 0x0
1271 #define cPIDinonce 0x3
1272 #define cCCnoerror 0x0
1274 #define cCCbitstuff 0x2
1275 #define cCCtoggle 0x3
1276 #define cCCstall 0x4
1277 #define cCCnoresp 0x5
1278 #define cCCbadpid1 0x6
1279 #define cCCbadpid2 0x7
1280 #define cCCdataoverrun 0x8
1281 #define cCCdataunderrun 0x9
1282 #define cCCbuffoverrun 0xC
1283 #define cCCbuffunderrun 0xD
1284 #define cCCnotaccessed 0xF
1285 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1287 wait:if (ftdi->disconnected > 0) {
1291 down(&ftdi->u132_lock);
1292 command_size = ftdi->command_next - ftdi->command_head;
1293 if (command_size < COMMAND_SIZE) {
1294 struct u132_command *command = &ftdi->command[
1295 COMMAND_MASK & ftdi->command_next];
1296 command->header = 0x00 | cPCIu132wr;
1297 command->length = 0x04;
1298 command->address = 0x00;
1299 command->width = 0x00;
1300 command->follows = 4;
1301 command->value = data;
1302 command->buffer = &command->value;
1303 ftdi->command_next += 1;
1304 ftdi_elan_kick_command_queue(ftdi);
1305 up(&ftdi->u132_lock);
1308 up(&ftdi->u132_lock);
1315 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1318 u8 addressofs = config_offset / 4;
1319 wait:if (ftdi->disconnected > 0) {
1323 down(&ftdi->u132_lock);
1324 command_size = ftdi->command_next - ftdi->command_head;
1325 if (command_size < COMMAND_SIZE) {
1326 struct u132_command *command = &ftdi->command[
1327 COMMAND_MASK & ftdi->command_next];
1328 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1329 command->length = 0x04;
1330 command->address = addressofs;
1331 command->width = 0x00 | (width & 0x0F);
1332 command->follows = 4;
1333 command->value = data;
1334 command->buffer = &command->value;
1335 ftdi->command_next += 1;
1336 ftdi_elan_kick_command_queue(ftdi);
1337 up(&ftdi->u132_lock);
1340 up(&ftdi->u132_lock);
1347 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1350 u8 addressofs = mem_offset / 4;
1351 wait:if (ftdi->disconnected > 0) {
1355 down(&ftdi->u132_lock);
1356 command_size = ftdi->command_next - ftdi->command_head;
1357 if (command_size < COMMAND_SIZE) {
1358 struct u132_command *command = &ftdi->command[
1359 COMMAND_MASK & ftdi->command_next];
1360 command->header = 0x00 | (cPCImemwr & 0x0F);
1361 command->length = 0x04;
1362 command->address = addressofs;
1363 command->width = 0x00 | (width & 0x0F);
1364 command->follows = 4;
1365 command->value = data;
1366 command->buffer = &command->value;
1367 ftdi->command_next += 1;
1368 ftdi_elan_kick_command_queue(ftdi);
1369 up(&ftdi->u132_lock);
1372 up(&ftdi->u132_lock);
1379 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1382 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1383 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1387 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1388 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1390 wait:if (ftdi->disconnected > 0) {
1395 down(&ftdi->u132_lock);
1396 command_size = ftdi->command_next - ftdi->command_head;
1397 respond_size = ftdi->respond_next - ftdi->respond_head;
1398 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1400 struct u132_command *command = &ftdi->command[
1401 COMMAND_MASK & ftdi->command_next];
1402 struct u132_respond *respond = &ftdi->respond[
1403 RESPOND_MASK & ftdi->respond_next];
1404 int result = -ENODEV;
1405 respond->result = &result;
1406 respond->header = command->header = 0x00 | cPCIu132rd;
1407 command->length = 0x04;
1408 respond->address = command->address = cU132cmd_status;
1409 command->width = 0x00;
1410 command->follows = 0;
1412 command->buffer = NULL;
1413 respond->value = data;
1414 init_completion(&respond->wait_completion);
1415 ftdi->command_next += 1;
1416 ftdi->respond_next += 1;
1417 ftdi_elan_kick_command_queue(ftdi);
1418 up(&ftdi->u132_lock);
1419 wait_for_completion(&respond->wait_completion);
1422 up(&ftdi->u132_lock);
1429 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1430 u8 width, u32 *data)
1432 u8 addressofs = config_offset / 4;
1433 wait:if (ftdi->disconnected > 0) {
1438 down(&ftdi->u132_lock);
1439 command_size = ftdi->command_next - ftdi->command_head;
1440 respond_size = ftdi->respond_next - ftdi->respond_head;
1441 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1443 struct u132_command *command = &ftdi->command[
1444 COMMAND_MASK & ftdi->command_next];
1445 struct u132_respond *respond = &ftdi->respond[
1446 RESPOND_MASK & ftdi->respond_next];
1447 int result = -ENODEV;
1448 respond->result = &result;
1449 respond->header = command->header = 0x00 | (cPCIcfgrd &
1451 command->length = 0x04;
1452 respond->address = command->address = addressofs;
1453 command->width = 0x00 | (width & 0x0F);
1454 command->follows = 0;
1456 command->buffer = NULL;
1457 respond->value = data;
1458 init_completion(&respond->wait_completion);
1459 ftdi->command_next += 1;
1460 ftdi->respond_next += 1;
1461 ftdi_elan_kick_command_queue(ftdi);
1462 up(&ftdi->u132_lock);
1463 wait_for_completion(&respond->wait_completion);
1466 up(&ftdi->u132_lock);
1473 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1474 u8 width, u32 *data)
1476 u8 addressofs = mem_offset / 4;
1477 wait:if (ftdi->disconnected > 0) {
1482 down(&ftdi->u132_lock);
1483 command_size = ftdi->command_next - ftdi->command_head;
1484 respond_size = ftdi->respond_next - ftdi->respond_head;
1485 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1487 struct u132_command *command = &ftdi->command[
1488 COMMAND_MASK & ftdi->command_next];
1489 struct u132_respond *respond = &ftdi->respond[
1490 RESPOND_MASK & ftdi->respond_next];
1491 int result = -ENODEV;
1492 respond->result = &result;
1493 respond->header = command->header = 0x00 | (cPCImemrd &
1495 command->length = 0x04;
1496 respond->address = command->address = addressofs;
1497 command->width = 0x00 | (width & 0x0F);
1498 command->follows = 0;
1500 command->buffer = NULL;
1501 respond->value = data;
1502 init_completion(&respond->wait_completion);
1503 ftdi->command_next += 1;
1504 ftdi->respond_next += 1;
1505 ftdi_elan_kick_command_queue(ftdi);
1506 up(&ftdi->u132_lock);
1507 wait_for_completion(&respond->wait_completion);
1510 up(&ftdi->u132_lock);
1517 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1518 u8 width, u32 *data)
1520 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1521 if (ftdi->initialized == 0) {
1524 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1528 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1529 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1530 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1531 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1532 int toggle_bits, int error_count, int condition_code, int repeat_number,
1533 int halted, int skipped, int actual, int non_null))
1535 u8 ed = ed_number - 1;
1536 wait:if (ftdi->disconnected > 0) {
1538 } else if (ftdi->initialized == 0) {
1542 down(&ftdi->u132_lock);
1543 command_size = ftdi->command_next - ftdi->command_head;
1544 if (command_size < COMMAND_SIZE) {
1545 struct u132_target *target = &ftdi->target[ed];
1546 struct u132_command *command = &ftdi->command[
1547 COMMAND_MASK & ftdi->command_next];
1548 command->header = 0x80 | (ed << 5);
1549 command->length = 0x8007;
1550 command->address = (toggle_bits << 6) | (ep_number << 2)
1552 command->width = usb_maxpacket(urb->dev, urb->pipe,
1553 usb_pipeout(urb->pipe));
1554 command->follows = 8;
1556 command->buffer = urb->setup_packet;
1557 target->callback = callback;
1558 target->endp = endp;
1561 ftdi->command_next += 1;
1562 ftdi_elan_kick_command_queue(ftdi);
1563 up(&ftdi->u132_lock);
1566 up(&ftdi->u132_lock);
1573 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, 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 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1580 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1581 ep_number, toggle_bits, callback);
1585 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1586 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1587 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1588 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1589 int toggle_bits, int error_count, int condition_code, int repeat_number,
1590 int halted, int skipped, int actual, int non_null))
1592 u8 ed = ed_number - 1;
1593 wait:if (ftdi->disconnected > 0) {
1595 } else if (ftdi->initialized == 0) {
1599 down(&ftdi->u132_lock);
1600 command_size = ftdi->command_next - ftdi->command_head;
1601 if (command_size < COMMAND_SIZE) {
1602 struct u132_target *target = &ftdi->target[ed];
1603 struct u132_command *command = &ftdi->command[
1604 COMMAND_MASK & ftdi->command_next];
1605 int remaining_length = urb->transfer_buffer_length -
1607 command->header = 0x82 | (ed << 5);
1608 if (remaining_length == 0) {
1609 command->length = 0x0000;
1610 } else if (remaining_length > 1024) {
1611 command->length = 0x8000 | 1023;
1613 command->length = 0x8000 | (remaining_length -
1615 command->address = (toggle_bits << 6) | (ep_number << 2)
1617 command->width = usb_maxpacket(urb->dev, urb->pipe,
1618 usb_pipeout(urb->pipe));
1619 command->follows = 0;
1621 command->buffer = NULL;
1622 target->callback = callback;
1623 target->endp = endp;
1626 ftdi->command_next += 1;
1627 ftdi_elan_kick_command_queue(ftdi);
1628 up(&ftdi->u132_lock);
1631 up(&ftdi->u132_lock);
1638 int usb_ftdi_elan_edset_input(struct platform_device *pdev, 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 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1645 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1646 ep_number, toggle_bits, callback);
1650 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1651 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1652 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1653 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1654 int toggle_bits, int error_count, int condition_code, int repeat_number,
1655 int halted, int skipped, int actual, int non_null))
1657 u8 ed = ed_number - 1;
1658 wait:if (ftdi->disconnected > 0) {
1660 } else if (ftdi->initialized == 0) {
1664 down(&ftdi->u132_lock);
1665 command_size = ftdi->command_next - ftdi->command_head;
1666 if (command_size < COMMAND_SIZE) {
1667 struct u132_target *target = &ftdi->target[ed];
1668 struct u132_command *command = &ftdi->command[
1669 COMMAND_MASK & ftdi->command_next];
1670 command->header = 0x81 | (ed << 5);
1671 command->length = 0x0000;
1672 command->address = (toggle_bits << 6) | (ep_number << 2)
1674 command->width = usb_maxpacket(urb->dev, urb->pipe,
1675 usb_pipeout(urb->pipe));
1676 command->follows = 0;
1678 command->buffer = NULL;
1679 target->callback = callback;
1680 target->endp = endp;
1683 ftdi->command_next += 1;
1684 ftdi_elan_kick_command_queue(ftdi);
1685 up(&ftdi->u132_lock);
1688 up(&ftdi->u132_lock);
1695 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, 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 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1702 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1703 ep_number, toggle_bits, callback);
1707 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1708 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1709 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1710 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1711 int toggle_bits, int error_count, int condition_code, int repeat_number,
1712 int halted, int skipped, int actual, int non_null))
1714 u8 ed = ed_number - 1;
1715 wait:if (ftdi->disconnected > 0) {
1717 } else if (ftdi->initialized == 0) {
1721 down(&ftdi->u132_lock);
1722 command_size = ftdi->command_next - ftdi->command_head;
1723 if (command_size < COMMAND_SIZE) {
1727 char data[30 *3 + 4];
1729 int m = (sizeof(data) - 1) / 3;
1731 struct u132_target *target = &ftdi->target[ed];
1732 struct u132_command *command = &ftdi->command[
1733 COMMAND_MASK & ftdi->command_next];
1734 command->header = 0x81 | (ed << 5);
1735 command->address = (toggle_bits << 6) | (ep_number << 2)
1737 command->width = usb_maxpacket(urb->dev, urb->pipe,
1738 usb_pipeout(urb->pipe));
1739 command->follows = min(1024,
1740 urb->transfer_buffer_length -
1741 urb->actual_length);
1743 command->buffer = urb->transfer_buffer +
1745 command->length = 0x8000 | (command->follows - 1);
1746 b = command->buffer;
1747 urb_size = command->follows;
1749 while (urb_size-- > 0) {
1751 } else if (i++ < m) {
1752 int w = sprintf(d, " %02X", *b++);
1756 d += sprintf(d, " ..");
1758 target->callback = callback;
1759 target->endp = endp;
1762 ftdi->command_next += 1;
1763 ftdi_elan_kick_command_queue(ftdi);
1764 up(&ftdi->u132_lock);
1767 up(&ftdi->u132_lock);
1774 int usb_ftdi_elan_edset_output(struct platform_device *pdev, 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 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1781 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1782 ep_number, toggle_bits, callback);
1786 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1787 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1788 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1789 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1790 int toggle_bits, int error_count, int condition_code, int repeat_number,
1791 int halted, int skipped, int actual, int non_null))
1793 u8 ed = ed_number - 1;
1794 wait:if (ftdi->disconnected > 0) {
1796 } else if (ftdi->initialized == 0) {
1800 down(&ftdi->u132_lock);
1801 command_size = ftdi->command_next - ftdi->command_head;
1802 if (command_size < COMMAND_SIZE) {
1803 int remaining_length = urb->transfer_buffer_length -
1805 struct u132_target *target = &ftdi->target[ed];
1806 struct u132_command *command = &ftdi->command[
1807 COMMAND_MASK & ftdi->command_next];
1808 command->header = 0x83 | (ed << 5);
1809 if (remaining_length == 0) {
1810 command->length = 0x0000;
1811 } else if (remaining_length > 1024) {
1812 command->length = 0x8000 | 1023;
1814 command->length = 0x8000 | (remaining_length -
1816 command->address = (toggle_bits << 6) | (ep_number << 2)
1818 command->width = usb_maxpacket(urb->dev, urb->pipe,
1819 usb_pipeout(urb->pipe));
1820 command->follows = 0;
1822 command->buffer = NULL;
1823 target->callback = callback;
1824 target->endp = endp;
1827 ftdi->command_next += 1;
1828 ftdi_elan_kick_command_queue(ftdi);
1829 up(&ftdi->u132_lock);
1832 up(&ftdi->u132_lock);
1839 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1840 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1841 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1842 int toggle_bits, int error_count, int condition_code, int repeat_number,
1843 int halted, int skipped, int actual, int non_null))
1845 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1846 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1847 ep_number, toggle_bits, callback);
1851 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1852 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1855 u8 ed = ed_number - 1;
1856 if (ftdi->disconnected > 0) {
1858 } else if (ftdi->initialized == 0) {
1861 struct u132_target *target = &ftdi->target[ed];
1862 down(&ftdi->u132_lock);
1863 if (target->abandoning > 0) {
1864 up(&ftdi->u132_lock);
1867 target->abandoning = 1;
1868 wait_1:if (target->active == 1) {
1869 int command_size = ftdi->command_next -
1871 if (command_size < COMMAND_SIZE) {
1872 struct u132_command *command =
1873 &ftdi->command[COMMAND_MASK &
1874 ftdi->command_next];
1875 command->header = 0x80 | (ed << 5) |
1877 command->length = 0x00;
1878 command->address = 0x00;
1879 command->width = 0x00;
1880 command->follows = 0;
1882 command->buffer = &command->value;
1883 ftdi->command_next += 1;
1884 ftdi_elan_kick_command_queue(ftdi);
1886 up(&ftdi->u132_lock);
1888 down(&ftdi->u132_lock);
1892 up(&ftdi->u132_lock);
1898 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1901 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1902 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1906 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1907 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1909 int retry_on_empty = 10;
1910 int retry_on_timeout = 5;
1911 int retry_on_status = 20;
1913 int packet_bytes = 0;
1914 int retval = usb_bulk_msg(ftdi->udev,
1915 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1916 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1917 &packet_bytes, msecs_to_jiffies(100));
1918 if (packet_bytes > 2) {
1919 char diag[30 *3 + 4];
1921 int m = (sizeof(diag) - 1) / 3;
1922 char *b = ftdi->bulk_in_buffer;
1925 while (packet_bytes-- > 0) {
1927 if (bytes_read < m) {
1928 d += sprintf(d, " %02X",
1930 } else if (bytes_read > m) {
1932 d += sprintf(d, " ..");
1937 } else if (packet_bytes > 1) {
1938 char s1 = ftdi->bulk_in_buffer[0];
1939 char s2 = ftdi->bulk_in_buffer[1];
1940 if (s1 == 0x31 && s2 == 0x60) {
1942 } else if (retry_on_status-- > 0) {
1945 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1949 } else if (packet_bytes > 0) {
1950 char b1 = ftdi->bulk_in_buffer[0];
1951 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1952 "TDI = %02X\n", b1);
1953 if (retry_on_status-- > 0) {
1956 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1960 } else if (retval == -ETIMEDOUT) {
1961 if (retry_on_timeout-- > 0) {
1964 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1968 } else if (retval == 0) {
1969 if (retry_on_empty-- > 0) {
1972 dev_err(&ftdi->udev->dev, "empty packet retry l"
1977 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1986 * send the long flush sequence
1989 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1996 urb = usb_alloc_urb(0, GFP_KERNEL);
1998 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
2002 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2004 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
2011 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2012 ftdi->bulk_out_endpointAddr), buf, i,
2013 ftdi_elan_write_bulk_callback, ftdi);
2014 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2015 retval = usb_submit_urb(urb, GFP_KERNEL);
2017 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2018 "flush sequence\n");
2019 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2029 * send the reset sequence
2032 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2039 urb = usb_alloc_urb(0, GFP_KERNEL);
2041 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2045 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2047 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2056 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2057 ftdi->bulk_out_endpointAddr), buf, i,
2058 ftdi_elan_write_bulk_callback, ftdi);
2059 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2060 retval = usb_submit_urb(urb, GFP_KERNEL);
2062 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2063 "reset sequence\n");
2064 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2072 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2076 int retry_on_timeout = 5;
2077 int retry_on_empty = 10;
2079 retval = ftdi_elan_flush_input_fifo(ftdi);
2082 ftdi->bulk_in_left = 0;
2083 ftdi->bulk_in_last = -1;
2084 while (long_stop-- > 0) {
2087 retval = ftdi_elan_synchronize_flush(ftdi);
2090 retval = ftdi_elan_flush_input_fifo(ftdi);
2093 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2099 int packet_bytes = 0;
2100 retval = usb_bulk_msg(ftdi->udev,
2101 usb_rcvbulkpipe(ftdi->udev,
2102 ftdi->bulk_in_endpointAddr),
2103 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2104 &packet_bytes, msecs_to_jiffies(500));
2105 if (packet_bytes > 2) {
2106 char diag[30 *3 + 4];
2108 int m = (sizeof(diag) - 1) / 3;
2109 char *b = ftdi->bulk_in_buffer;
2111 unsigned char c = 0;
2113 while (packet_bytes-- > 0) {
2115 if (bytes_read < m) {
2116 d += sprintf(d, " %02X", c);
2117 } else if (bytes_read > m) {
2119 d += sprintf(d, " ..");
2128 } else if (read_stop-- > 0) {
2131 dev_err(&ftdi->udev->dev, "retr"
2132 "y limit reached\n");
2136 } else if (packet_bytes > 1) {
2137 unsigned char s1 = ftdi->bulk_in_buffer[0];
2138 unsigned char s2 = ftdi->bulk_in_buffer[1];
2139 if (s1 == 0x31 && s2 == 0x00) {
2140 if (read_stuck-- > 0) {
2144 } else if (s1 == 0x31 && s2 == 0x60) {
2145 if (read_stop-- > 0) {
2148 dev_err(&ftdi->udev->dev, "retr"
2149 "y limit reached\n");
2153 if (read_stop-- > 0) {
2156 dev_err(&ftdi->udev->dev, "retr"
2157 "y limit reached\n");
2161 } else if (packet_bytes > 0) {
2162 if (read_stop-- > 0) {
2165 dev_err(&ftdi->udev->dev, "retry limit "
2169 } else if (retval == -ETIMEDOUT) {
2170 if (retry_on_timeout-- > 0) {
2173 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2174 "try limit reached\n");
2177 } else if (retval == 0) {
2178 if (retry_on_empty-- > 0) {
2181 dev_err(&ftdi->udev->dev, "empty packet"
2182 " retry limit reached\n");
2187 dev_err(&ftdi->udev->dev, "error = %d\n",
2189 if (read_stop-- > 0) {
2192 dev_err(&ftdi->udev->dev, "retry limit "
2199 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2203 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2205 int retry_on_empty = 10;
2206 int retry_on_timeout = 5;
2207 int retry_on_status = 50;
2209 int packet_bytes = 0;
2210 int retval = usb_bulk_msg(ftdi->udev,
2211 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2212 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2213 &packet_bytes, msecs_to_jiffies(1000));
2214 if (packet_bytes > 2) {
2215 char diag[30 *3 + 4];
2217 int m = (sizeof(diag) - 1) / 3;
2218 char *b = ftdi->bulk_in_buffer;
2221 while (packet_bytes-- > 0) {
2223 if (bytes_read < m) {
2224 d += sprintf(d, " %02X",
2226 } else if (bytes_read > m) {
2228 d += sprintf(d, " ..");
2233 } else if (packet_bytes > 1) {
2234 char s1 = ftdi->bulk_in_buffer[0];
2235 char s2 = ftdi->bulk_in_buffer[1];
2236 if (s1 == 0x31 && s2 == 0x60) {
2238 } else if (retry_on_status-- > 0) {
2243 } else if (packet_bytes > 0) {
2244 char b1 = ftdi->bulk_in_buffer[0];
2245 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2246 "TDI = %02X\n", b1);
2247 if (retry_on_status-- > 0) {
2251 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2255 } else if (retval == -ETIMEDOUT) {
2256 if (retry_on_timeout-- > 0) {
2259 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2263 } else if (retval == 0) {
2264 if (retry_on_empty-- > 0) {
2267 dev_err(&ftdi->udev->dev, "empty packet retry l"
2272 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2279 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2281 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2284 if (ftdi->controlreg & 0x00400000) {
2285 if (ftdi->card_ejected) {
2287 ftdi->card_ejected = 1;
2288 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2289 "%08X\n", ftdi->controlreg);
2293 u8 fn = ftdi->function - 1;
2294 int activePCIfn = fn << 8;
2299 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2303 pciVID = pcidata & 0xFFFF;
2304 pciPID = (pcidata >> 16) & 0xFFFF;
2305 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2306 ftdi->platform_data.device) {
2309 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2310 "ce=%04X pciPID=%04X\n",
2311 ftdi->platform_data.vendor, pciVID,
2312 ftdi->platform_data.device, pciPID);
2318 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2327 int activePCIfn = 0;
2330 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2333 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2337 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2340 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2343 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2346 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2349 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2353 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2356 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2359 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2362 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2365 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2369 for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
2370 activePCIfn = fn << 8;
2371 ftdi->function = fn + 1;
2372 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2376 pciVID = pcidata & 0xFFFF;
2377 pciPID = (pcidata >> 16) & 0xFFFF;
2378 if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
2380 } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
2382 } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
2384 } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
2386 } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
2389 if (foundOHCI == 0) {
2392 ftdi->platform_data.vendor = pciVID;
2393 ftdi->platform_data.device = pciPID;
2394 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2398 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2402 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2406 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2410 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2415 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2419 latence_timer &= 0xFFFF00FF;
2420 latence_timer |= 0x00001600;
2421 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2425 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2430 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2434 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2441 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2446 int reset_repeat = 0;
2448 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
2452 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2457 if (reset_repeat++ > 100) {
2467 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
2470 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2474 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
2477 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2481 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
2484 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2488 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
2491 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2496 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
2499 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2503 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2507 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2511 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
2514 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2518 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2522 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2526 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
2529 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2534 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
2538 power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2541 if (!(pcidata & 1)) {
2547 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2551 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2555 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2558 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2562 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
2565 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2571 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2576 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2581 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2585 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2588 dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
2589 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2598 * we use only the first bulk-in and bulk-out endpoints
2600 static int ftdi_elan_probe(struct usb_interface *interface,
2601 const struct usb_device_id *id)
2603 struct usb_host_interface *iface_desc;
2604 struct usb_endpoint_descriptor *endpoint;
2607 int retval = -ENOMEM;
2608 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2610 printk(KERN_ERR "Out of memory\n");
2613 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2614 down(&ftdi_module_lock);
2615 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2616 ftdi->sequence_num = ++ftdi_instances;
2617 up(&ftdi_module_lock);
2618 ftdi_elan_init_kref(ftdi);
2619 init_MUTEX(&ftdi->sw_lock);
2620 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2621 ftdi->interface = interface;
2622 init_MUTEX(&ftdi->u132_lock);
2624 iface_desc = interface->cur_altsetting;
2625 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2626 endpoint = &iface_desc->endpoint[i].desc;
2627 if (!ftdi->bulk_in_endpointAddr &&
2628 usb_endpoint_is_bulk_in(endpoint)) {
2629 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2630 ftdi->bulk_in_size = buffer_size;
2631 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2632 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2633 if (!ftdi->bulk_in_buffer) {
2634 dev_err(&ftdi->udev->dev, "Could not allocate b"
2640 if (!ftdi->bulk_out_endpointAddr &&
2641 usb_endpoint_is_bulk_out(endpoint)) {
2642 ftdi->bulk_out_endpointAddr =
2643 endpoint->bEndpointAddress;
2646 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2647 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2648 "-out endpoints\n");
2652 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2653 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2654 ftdi->bulk_out_endpointAddr);
2655 usb_set_intfdata(interface, ftdi);
2656 if (iface_desc->desc.bInterfaceNumber == 0 &&
2657 ftdi->bulk_in_endpointAddr == 0x81 &&
2658 ftdi->bulk_out_endpointAddr == 0x02) {
2659 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2661 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2663 usb_set_intfdata(interface, NULL);
2667 ftdi->class = &ftdi_elan_jtag_class;
2668 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2669 "%d now attached to ftdi%d\n", ftdi,
2670 iface_desc->desc.bInterfaceNumber,
2674 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2675 ftdi->bulk_in_endpointAddr == 0x83 &&
2676 ftdi->bulk_out_endpointAddr == 0x04) {
2678 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2679 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2680 INIT_WORK(&ftdi->status_work, ftdi_elan_status_work,
2682 INIT_WORK(&ftdi->command_work, ftdi_elan_command_work,
2684 INIT_WORK(&ftdi->respond_work, ftdi_elan_respond_work,
2686 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2689 dev_err(&ftdi->udev->dev,
2690 "Could not find ELAN's U132 device\n");
2695 ftdi_elan_put_kref(ftdi);
2700 static void ftdi_elan_disconnect(struct usb_interface *interface)
2702 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2703 ftdi->disconnected += 1;
2705 int minor = interface->minor;
2706 struct usb_class_driver *class = ftdi->class;
2707 usb_set_intfdata(interface, NULL);
2708 usb_deregister_dev(interface, class);
2709 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2710 "or %d now disconnected\n", minor);
2712 ftdi_status_cancel_work(ftdi);
2713 ftdi_command_cancel_work(ftdi);
2714 ftdi_response_cancel_work(ftdi);
2715 ftdi_elan_abandon_completions(ftdi);
2716 ftdi_elan_abandon_targets(ftdi);
2717 if (ftdi->registered) {
2718 platform_device_unregister(&ftdi->platform_dev);
2719 ftdi->synchronized = 0;
2720 ftdi->enumerated = 0;
2721 ftdi->registered = 0;
2723 flush_workqueue(status_queue);
2724 flush_workqueue(command_queue);
2725 flush_workqueue(respond_queue);
2726 ftdi->disconnected += 1;
2727 usb_set_intfdata(interface, NULL);
2728 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2729 "face now disconnected\n");
2731 ftdi_elan_put_kref(ftdi);
2734 static struct usb_driver ftdi_elan_driver = {
2735 .name = "ftdi-elan",
2736 .probe = ftdi_elan_probe,
2737 .disconnect = ftdi_elan_disconnect,
2738 .id_table = ftdi_elan_table,
2740 static int __init ftdi_elan_init(void)
2743 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2744 __TIME__, __DATE__);
2745 init_MUTEX(&ftdi_module_lock);
2746 INIT_LIST_HEAD(&ftdi_static_list);
2747 status_queue = create_singlethread_workqueue("ftdi-status-control");
2748 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2749 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2750 result = usb_register(&ftdi_elan_driver);
2752 printk(KERN_ERR "usb_register failed. Error number %d\n",
2757 static void __exit ftdi_elan_exit(void)
2759 struct usb_ftdi *ftdi;
2760 struct usb_ftdi *temp;
2761 usb_deregister(&ftdi_elan_driver);
2762 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2763 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2764 ftdi_status_cancel_work(ftdi);
2765 ftdi_command_cancel_work(ftdi);
2766 ftdi_response_cancel_work(ftdi);
2767 } flush_workqueue(status_queue);
2768 destroy_workqueue(status_queue);
2769 status_queue = NULL;
2770 flush_workqueue(command_queue);
2771 destroy_workqueue(command_queue);
2772 command_queue = NULL;
2773 flush_workqueue(respond_queue);
2774 destroy_workqueue(respond_queue);
2775 respond_queue = NULL;
2779 module_init(ftdi_elan_init);
2780 module_exit(ftdi_elan_exit);