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 delayed_work status_work;
160 struct delayed_work command_work;
161 struct delayed_work 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)
213 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
214 kref_put(&ftdi->kref, ftdi_elan_delete);
217 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
219 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
220 kref_get(&ftdi->kref);
223 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
225 if (cancel_delayed_work(&ftdi->status_work))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
229 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
231 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
232 kref_put(&ftdi->kref, ftdi_elan_delete);
235 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
237 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
238 kref_get(&ftdi->kref);
241 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
243 if (cancel_delayed_work(&ftdi->command_work))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
247 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
250 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
251 kref_put(&ftdi->kref, ftdi_elan_delete);
254 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
256 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
257 kref_get(&ftdi->kref);
260 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
262 if (cancel_delayed_work(&ftdi->respond_work))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
266 void ftdi_elan_gone_away(struct platform_device *pdev)
268 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
269 ftdi->gone_away += 1;
270 ftdi_elan_put_kref(ftdi);
274 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
275 static void ftdi_release_platform_dev(struct device *dev)
280 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
281 struct u132_target *target, u8 *buffer, int length);
282 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
283 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
284 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
285 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
286 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
287 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
288 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
289 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
290 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
291 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
294 if (ftdi->platform_dev.dev.parent)
296 ftdi_elan_get_kref(ftdi);
297 ftdi->platform_data.potpg = 100;
298 ftdi->platform_data.reset = NULL;
299 ftdi->platform_dev.id = ftdi->sequence_num;
300 ftdi->platform_dev.resource = ftdi->resources;
301 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
302 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
303 ftdi->platform_dev.dev.parent = NULL;
304 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
305 ftdi->platform_dev.dev.dma_mask = NULL;
306 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
307 ftdi->platform_dev.name = ftdi->device_name;
308 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
309 request_module("u132_hcd");
310 dev_info(&ftdi->udev->dev, "registering '%s'\n",
311 ftdi->platform_dev.name);
312 result = platform_device_register(&ftdi->platform_dev);
316 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
318 down(&ftdi->u132_lock);
319 while (ftdi->respond_next > ftdi->respond_head) {
320 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
321 ftdi->respond_head++];
322 *respond->result = -ESHUTDOWN;
324 complete(&respond->wait_completion);
325 } up(&ftdi->u132_lock);
328 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
331 down(&ftdi->u132_lock);
332 while (ed_number-- > 0) {
333 struct u132_target *target = &ftdi->target[ed_number];
334 if (target->active == 1) {
335 target->condition_code = TD_DEVNOTRESP;
336 up(&ftdi->u132_lock);
337 ftdi_elan_do_callback(ftdi, target, NULL, 0);
338 down(&ftdi->u132_lock);
344 up(&ftdi->u132_lock);
347 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
350 down(&ftdi->u132_lock);
351 while (ed_number-- > 0) {
352 struct u132_target *target = &ftdi->target[ed_number];
353 target->abandoning = 1;
354 wait_1:if (target->active == 1) {
355 int command_size = ftdi->command_next -
357 if (command_size < COMMAND_SIZE) {
358 struct u132_command *command = &ftdi->command[
359 COMMAND_MASK & ftdi->command_next];
360 command->header = 0x80 | (ed_number << 5) | 0x4;
361 command->length = 0x00;
362 command->address = 0x00;
363 command->width = 0x00;
364 command->follows = 0;
366 command->buffer = &command->value;
367 ftdi->command_next += 1;
368 ftdi_elan_kick_command_queue(ftdi);
370 up(&ftdi->u132_lock);
372 down(&ftdi->u132_lock);
376 wait_2:if (target->active == 1) {
377 int command_size = ftdi->command_next -
379 if (command_size < COMMAND_SIZE) {
380 struct u132_command *command = &ftdi->command[
381 COMMAND_MASK & ftdi->command_next];
382 command->header = 0x90 | (ed_number << 5);
383 command->length = 0x00;
384 command->address = 0x00;
385 command->width = 0x00;
386 command->follows = 0;
388 command->buffer = &command->value;
389 ftdi->command_next += 1;
390 ftdi_elan_kick_command_queue(ftdi);
392 up(&ftdi->u132_lock);
394 down(&ftdi->u132_lock);
402 up(&ftdi->u132_lock);
405 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
408 down(&ftdi->u132_lock);
409 while (ed_number-- > 0) {
410 struct u132_target *target = &ftdi->target[ed_number];
411 target->abandoning = 1;
412 wait:if (target->active == 1) {
413 int command_size = ftdi->command_next -
415 if (command_size < COMMAND_SIZE) {
416 struct u132_command *command = &ftdi->command[
417 COMMAND_MASK & ftdi->command_next];
418 command->header = 0x80 | (ed_number << 5) | 0x4;
419 command->length = 0x00;
420 command->address = 0x00;
421 command->width = 0x00;
422 command->follows = 0;
424 command->buffer = &command->value;
425 ftdi->command_next += 1;
426 ftdi_elan_kick_command_queue(ftdi);
428 up(&ftdi->u132_lock);
430 down(&ftdi->u132_lock);
438 up(&ftdi->u132_lock);
441 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
443 ftdi_command_queue_work(ftdi, 0);
447 static void ftdi_elan_command_work(struct work_struct *work)
449 struct usb_ftdi *ftdi =
450 container_of(work, struct usb_ftdi, command_work.work);
452 if (ftdi->disconnected > 0) {
453 ftdi_elan_put_kref(ftdi);
456 int retval = ftdi_elan_command_engine(ftdi);
457 if (retval == -ESHUTDOWN) {
458 ftdi->disconnected += 1;
459 } else if (retval == -ENODEV) {
460 ftdi->disconnected += 1;
462 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
463 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
468 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
470 ftdi_respond_queue_work(ftdi, 0);
474 static void ftdi_elan_respond_work(struct work_struct *work)
476 struct usb_ftdi *ftdi =
477 container_of(work, struct usb_ftdi, respond_work.work);
478 if (ftdi->disconnected > 0) {
479 ftdi_elan_put_kref(ftdi);
482 int retval = ftdi_elan_respond_engine(ftdi);
484 } else if (retval == -ESHUTDOWN) {
485 ftdi->disconnected += 1;
486 } else if (retval == -ENODEV) {
487 ftdi->disconnected += 1;
488 } else if (retval == -EILSEQ) {
489 ftdi->disconnected += 1;
491 ftdi->disconnected += 1;
492 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
494 if (ftdi->disconnected > 0) {
495 ftdi_elan_abandon_completions(ftdi);
496 ftdi_elan_abandon_targets(ftdi);
498 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
505 * the sw_lock is initially held and will be freed
506 * after the FTDI has been synchronized
509 static void ftdi_elan_status_work(struct work_struct *work)
511 struct usb_ftdi *ftdi =
512 container_of(work, struct usb_ftdi, status_work.work);
513 int work_delay_in_msec = 0;
514 if (ftdi->disconnected > 0) {
515 ftdi_elan_put_kref(ftdi);
517 } else if (ftdi->synchronized == 0) {
518 down(&ftdi->sw_lock);
519 if (ftdi_elan_synchronize(ftdi) == 0) {
520 ftdi->synchronized = 1;
521 ftdi_command_queue_work(ftdi, 1);
522 ftdi_respond_queue_work(ftdi, 1);
524 work_delay_in_msec = 100;
526 dev_err(&ftdi->udev->dev, "synchronize failed\n");
528 work_delay_in_msec = 10 *1000;
530 } else if (ftdi->stuck_status > 0) {
531 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
532 ftdi->stuck_status = 0;
533 ftdi->synchronized = 0;
534 } else if ((ftdi->stuck_status++ % 60) == 1) {
535 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
536 "- please remove\n");
538 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
539 "- checked %d times\n", ftdi->stuck_status);
540 work_delay_in_msec = 100;
541 } else if (ftdi->enumerated == 0) {
542 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
543 ftdi->enumerated = 1;
544 work_delay_in_msec = 250;
546 work_delay_in_msec = 1000;
547 } else if (ftdi->initialized == 0) {
548 if (ftdi_elan_setupOHCI(ftdi) == 0) {
549 ftdi->initialized = 1;
550 work_delay_in_msec = 500;
552 dev_err(&ftdi->udev->dev, "initialized failed - trying "
553 "again in 10 seconds\n");
554 work_delay_in_msec = 10 *1000;
556 } else if (ftdi->registered == 0) {
557 work_delay_in_msec = 10;
558 if (ftdi_elan_hcd_init(ftdi) == 0) {
559 ftdi->registered = 1;
561 dev_err(&ftdi->udev->dev, "register failed\n");
562 work_delay_in_msec = 250;
564 if (ftdi_elan_checkingPCI(ftdi) == 0) {
565 work_delay_in_msec = 250;
566 } else if (ftdi->controlreg & 0x00400000) {
567 if (ftdi->gone_away > 0) {
568 dev_err(&ftdi->udev->dev, "PCI device eject con"
569 "firmed platform_dev.dev.parent=%p plat"
571 ftdi->platform_dev.dev.parent,
572 &ftdi->platform_dev.dev);
573 platform_device_unregister(&ftdi->platform_dev);
574 ftdi->platform_dev.dev.parent = NULL;
575 ftdi->registered = 0;
576 ftdi->enumerated = 0;
577 ftdi->card_ejected = 0;
578 ftdi->initialized = 0;
581 ftdi_elan_flush_targets(ftdi);
582 work_delay_in_msec = 250;
584 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
586 ftdi_elan_cancel_targets(ftdi);
587 work_delay_in_msec = 500;
588 ftdi->enumerated = 0;
589 ftdi->initialized = 0;
592 if (ftdi->disconnected > 0) {
593 ftdi_elan_put_kref(ftdi);
596 ftdi_status_requeue_work(ftdi,
597 msecs_to_jiffies(work_delay_in_msec));
604 * file_operations for the jtag interface
606 * the usage count for the device is incremented on open()
607 * and decremented on release()
609 static int ftdi_elan_open(struct inode *inode, struct file *file)
611 int subminor = iminor(inode);
612 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
615 printk(KERN_ERR "can't find device for minor %d\n", subminor);
618 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
622 if (down_interruptible(&ftdi->sw_lock)) {
625 ftdi_elan_get_kref(ftdi);
626 file->private_data = ftdi;
633 static int ftdi_elan_release(struct inode *inode, struct file *file)
635 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
638 up(&ftdi->sw_lock); /* decrement the count on our device */
639 ftdi_elan_put_kref(ftdi);
644 #define FTDI_ELAN_IOC_MAGIC 0xA1
645 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
646 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
647 unsigned int cmd, unsigned long arg)
650 case FTDI_ELAN_IOCDEBUG:{
652 int size = strncpy_from_user(line,
653 (const char __user *)arg, sizeof(line));
657 printk(KERN_ERR "TODO: ioctl %s\n", line);
669 * blocking bulk reads are used to get data from the device
672 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
673 size_t count, loff_t *ppos)
675 char data[30 *3 + 4];
677 int m = (sizeof(data) - 1) / 3;
679 int retry_on_empty = 10;
680 int retry_on_timeout = 5;
681 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
682 if (ftdi->disconnected > 0) {
686 have:if (ftdi->bulk_in_left > 0) {
688 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
689 ftdi->bulk_in_left -= 1;
690 if (bytes_read < m) {
691 d += sprintf(d, " %02X", 0x000000FF & *p);
692 } else if (bytes_read > m) {
694 d += sprintf(d, " ..");
695 if (copy_to_user(buffer++, p, 1)) {
704 more:if (count > 0) {
705 int packet_bytes = 0;
706 int retval = usb_bulk_msg(ftdi->udev,
707 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
708 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
709 &packet_bytes, msecs_to_jiffies(50));
710 if (packet_bytes > 2) {
711 ftdi->bulk_in_left = packet_bytes - 2;
712 ftdi->bulk_in_last = 1;
714 } else if (retval == -ETIMEDOUT) {
715 if (retry_on_timeout-- > 0) {
717 } else if (bytes_read > 0) {
721 } else if (retval == 0) {
722 if (retry_on_empty-- > 0) {
732 static void ftdi_elan_write_bulk_callback(struct urb *urb)
734 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
735 if (urb->status && !(urb->status == -ENOENT || urb->status ==
736 -ECONNRESET || urb->status == -ESHUTDOWN)) {
737 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
738 "d\n", urb, urb->status);
740 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
741 urb->transfer_buffer, urb->transfer_dma);
744 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
745 char *buf, int command_size, int total_size)
749 int I = command_size;
750 int i = ftdi->command_head;
752 struct u132_command *command = &ftdi->command[COMMAND_MASK &
754 int F = command->follows;
755 u8 *f = command->buffer;
756 if (command->header & 0x80) {
757 ed_commands |= 1 << (0x3 & (command->header >> 5));
759 buf[b++] = command->header;
760 buf[b++] = (command->length >> 0) & 0x00FF;
761 buf[b++] = (command->length >> 8) & 0x00FF;
762 buf[b++] = command->address;
763 buf[b++] = command->width;
771 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
774 int I = command_size;
775 int i = ftdi->command_head;
777 struct u132_command *command = &ftdi->command[COMMAND_MASK &
779 total_size += 5 + command->follows;
783 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
790 int command_size = ftdi->command_next - ftdi->command_head;
791 if (command_size == 0)
793 total_size = ftdi_elan_total_command_size(ftdi, command_size);
794 urb = usb_alloc_urb(0, GFP_KERNEL);
796 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
797 "ands totaling %d bytes to the Uxxx\n", command_size,
801 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
804 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
805 "ommands totaling %d bytes to the Uxxx\n", command_size,
810 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
811 command_size, total_size);
812 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
813 ftdi->bulk_out_endpointAddr), buf, total_size,
814 ftdi_elan_write_bulk_callback, ftdi);
815 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
817 char diag[40 *3 + 4];
821 int s = (sizeof(diag) - 1) / 3;
823 while (s-- > 0 && m-- > 0) {
824 if (s > 0 || m == 0) {
825 d += sprintf(d, " %02X", *c++);
827 d += sprintf(d, " ..");
830 retval = usb_submit_urb(urb, GFP_KERNEL);
832 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
833 "%d commands totaling %d bytes to the Uxxx\n", retval,
834 urb, command_size, total_size);
835 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
839 usb_free_urb(urb); /* release our reference to this urb,
840 the USB core will eventually free it entirely */
841 ftdi->command_head += command_size;
842 ftdi_elan_kick_respond_queue(ftdi);
846 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
847 struct u132_target *target, u8 *buffer, int length)
849 struct urb *urb = target->urb;
850 int halted = target->halted;
851 int skipped = target->skipped;
852 int actual = target->actual;
853 int non_null = target->non_null;
854 int toggle_bits = target->toggle_bits;
855 int error_count = target->error_count;
856 int condition_code = target->condition_code;
857 int repeat_number = target->repeat_number;
858 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
859 int, int, int, int) = target->callback;
861 target->callback = NULL;
862 (*callback) (target->endp, urb, buffer, length, toggle_bits,
863 error_count, condition_code, repeat_number, halted, skipped,
867 static char *have_ed_set_response(struct usb_ftdi *ftdi,
868 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
871 int payload = (ed_length >> 0) & 0x07FF;
872 down(&ftdi->u132_lock);
874 target->non_null = (ed_length >> 15) & 0x0001;
875 target->repeat_number = (ed_length >> 11) & 0x000F;
876 if (ed_type == 0x02) {
877 if (payload == 0 || target->abandoning > 0) {
878 target->abandoning = 0;
879 up(&ftdi->u132_lock);
880 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
885 return ftdi->response;
887 ftdi->expected = 4 + payload;
889 up(&ftdi->u132_lock);
892 } else if (ed_type == 0x03) {
893 if (payload == 0 || target->abandoning > 0) {
894 target->abandoning = 0;
895 up(&ftdi->u132_lock);
896 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
901 return ftdi->response;
903 ftdi->expected = 4 + payload;
905 up(&ftdi->u132_lock);
908 } else if (ed_type == 0x01) {
909 target->abandoning = 0;
910 up(&ftdi->u132_lock);
911 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
916 return ftdi->response;
918 target->abandoning = 0;
919 up(&ftdi->u132_lock);
920 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
925 return ftdi->response;
929 static char *have_ed_get_response(struct usb_ftdi *ftdi,
930 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
933 down(&ftdi->u132_lock);
934 target->condition_code = TD_DEVNOTRESP;
935 target->actual = (ed_length >> 0) & 0x01FF;
936 target->non_null = (ed_length >> 15) & 0x0001;
937 target->repeat_number = (ed_length >> 11) & 0x000F;
938 up(&ftdi->u132_lock);
940 ftdi_elan_do_callback(ftdi, target, NULL, 0);
941 target->abandoning = 0;
945 return ftdi->response;
950 * The engine tries to empty the FTDI fifo
952 * all responses found in the fifo data are dispatched thus
953 * the response buffer can only ever hold a maximum sized
954 * response from the Uxxx.
957 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
959 u8 *b = ftdi->response + ftdi->recieved;
961 int retry_on_empty = 1;
962 int retry_on_timeout = 3;
963 int empty_packets = 0;
965 int packet_bytes = 0;
966 int retval = usb_bulk_msg(ftdi->udev,
967 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
968 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
969 &packet_bytes, msecs_to_jiffies(500));
970 char diag[30 *3 + 4];
972 int m = packet_bytes;
973 u8 *c = ftdi->bulk_in_buffer;
974 int s = (sizeof(diag) - 1) / 3;
976 while (s-- > 0 && m-- > 0) {
977 if (s > 0 || m == 0) {
978 d += sprintf(d, " %02X", *c++);
980 d += sprintf(d, " ..");
982 if (packet_bytes > 2) {
983 ftdi->bulk_in_left = packet_bytes - 2;
984 ftdi->bulk_in_last = 1;
986 } else if (retval == -ETIMEDOUT) {
987 if (retry_on_timeout-- > 0) {
988 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
989 "t_bytes = %d with total %d bytes%s\n",
990 packet_bytes, bytes_read, diag);
992 } else if (bytes_read > 0) {
993 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
997 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
998 "t_bytes = %d with total %d bytes%s\n",
999 packet_bytes, bytes_read, diag);
1002 } else if (retval == -EILSEQ) {
1003 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1004 " = %d with total %d bytes%s\n", retval,
1005 packet_bytes, bytes_read, diag);
1007 } else if (retval) {
1008 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1009 " = %d with total %d bytes%s\n", retval,
1010 packet_bytes, bytes_read, diag);
1012 } else if (packet_bytes == 2) {
1013 unsigned char s0 = ftdi->bulk_in_buffer[0];
1014 unsigned char s1 = ftdi->bulk_in_buffer[1];
1016 if (s0 == 0x31 && s1 == 0x60) {
1017 if (retry_on_empty-- > 0) {
1021 } else if (s0 == 0x31 && s1 == 0x00) {
1022 if (retry_on_empty-- > 0) {
1027 if (retry_on_empty-- > 0) {
1032 } else if (packet_bytes == 1) {
1033 if (retry_on_empty-- > 0) {
1038 if (retry_on_empty-- > 0) {
1047 have:if (ftdi->bulk_in_left > 0) {
1048 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1050 ftdi->bulk_in_left -= 1;
1051 if (ftdi->recieved == 0 && c == 0xFF) {
1055 if (++ftdi->recieved < ftdi->expected) {
1057 } else if (ftdi->ed_found) {
1058 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1059 u16 ed_length = (ftdi->response[2] << 8) |
1061 struct u132_target *target = &ftdi->target[ed_number];
1062 int payload = (ed_length >> 0) & 0x07FF;
1063 char diag[30 *3 + 4];
1066 u8 *c = 4 + ftdi->response;
1067 int s = (sizeof(diag) - 1) / 3;
1069 while (s-- > 0 && m-- > 0) {
1070 if (s > 0 || m == 0) {
1071 d += sprintf(d, " %02X", *c++);
1073 d += sprintf(d, " ..");
1075 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1082 } else if (ftdi->expected == 8) {
1084 int respond_head = ftdi->respond_head++;
1085 struct u132_respond *respond = &ftdi->respond[
1086 RESPOND_MASK & respond_head];
1087 u32 data = ftdi->response[7];
1089 data |= ftdi->response[6];
1091 data |= ftdi->response[5];
1093 data |= ftdi->response[4];
1094 *respond->value = data;
1095 *respond->result = 0;
1096 complete(&respond->wait_completion);
1101 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1102 if (buscmd == 0x00) {
1103 } else if (buscmd == 0x02) {
1104 } else if (buscmd == 0x06) {
1105 } else if (buscmd == 0x0A) {
1107 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1108 "lue = %08X\n", buscmd, data);
1111 if ((ftdi->response[0] & 0x80) == 0x00) {
1115 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1116 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1117 u16 ed_length = (ftdi->response[2] << 8) |
1119 struct u132_target *target = &ftdi->target[
1121 target->halted = (ftdi->response[0] >> 3) &
1123 target->skipped = (ftdi->response[0] >> 2) &
1125 target->toggle_bits = (ftdi->response[3] >> 6)
1127 target->error_count = (ftdi->response[3] >> 4)
1129 target->condition_code = (ftdi->response[
1131 if ((ftdi->response[0] & 0x10) == 0x00) {
1132 b = have_ed_set_response(ftdi, target,
1133 ed_length, ed_number, ed_type,
1137 b = have_ed_get_response(ftdi, target,
1138 ed_length, ed_number, ed_type,
1150 * create a urb, and a buffer for it, and copy the data to the urb
1153 static ssize_t ftdi_elan_write(struct file *file,
1154 const char __user *user_buffer, size_t count,
1160 struct usb_ftdi *ftdi = file->private_data;
1162 if (ftdi->disconnected > 0) {
1168 urb = usb_alloc_urb(0, GFP_KERNEL);
1173 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1174 &urb->transfer_dma);
1179 if (copy_from_user(buf, user_buffer, count)) {
1183 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1184 ftdi->bulk_out_endpointAddr), buf, count,
1185 ftdi_elan_write_bulk_callback, ftdi);
1186 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1187 retval = usb_submit_urb(urb, GFP_KERNEL);
1189 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1198 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1205 static struct file_operations ftdi_elan_fops = {
1206 .owner = THIS_MODULE,
1207 .llseek = no_llseek,
1208 .ioctl = ftdi_elan_ioctl,
1209 .read = ftdi_elan_read,
1210 .write = ftdi_elan_write,
1211 .open = ftdi_elan_open,
1212 .release = ftdi_elan_release,
1216 * usb class driver info in order to get a minor number from the usb core,
1217 * and to have the device registered with the driver core
1219 static struct usb_class_driver ftdi_elan_jtag_class = {
1220 .name = "ftdi-%d-jtag",
1221 .fops = &ftdi_elan_fops,
1222 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1226 * the following definitions are for the
1227 * ELAN FPGA state machgine processor that
1228 * lies on the other side of the FTDI chip
1230 #define cPCIu132rd 0x0
1231 #define cPCIu132wr 0x1
1232 #define cPCIiord 0x2
1233 #define cPCIiowr 0x3
1234 #define cPCImemrd 0x6
1235 #define cPCImemwr 0x7
1236 #define cPCIcfgrd 0xA
1237 #define cPCIcfgwr 0xB
1238 #define cPCInull 0xF
1239 #define cU132cmd_status 0x0
1240 #define cU132flash 0x1
1241 #define cPIDsetup 0x0
1244 #define cPIDinonce 0x3
1245 #define cCCnoerror 0x0
1247 #define cCCbitstuff 0x2
1248 #define cCCtoggle 0x3
1249 #define cCCstall 0x4
1250 #define cCCnoresp 0x5
1251 #define cCCbadpid1 0x6
1252 #define cCCbadpid2 0x7
1253 #define cCCdataoverrun 0x8
1254 #define cCCdataunderrun 0x9
1255 #define cCCbuffoverrun 0xC
1256 #define cCCbuffunderrun 0xD
1257 #define cCCnotaccessed 0xF
1258 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1260 wait:if (ftdi->disconnected > 0) {
1264 down(&ftdi->u132_lock);
1265 command_size = ftdi->command_next - ftdi->command_head;
1266 if (command_size < COMMAND_SIZE) {
1267 struct u132_command *command = &ftdi->command[
1268 COMMAND_MASK & ftdi->command_next];
1269 command->header = 0x00 | cPCIu132wr;
1270 command->length = 0x04;
1271 command->address = 0x00;
1272 command->width = 0x00;
1273 command->follows = 4;
1274 command->value = data;
1275 command->buffer = &command->value;
1276 ftdi->command_next += 1;
1277 ftdi_elan_kick_command_queue(ftdi);
1278 up(&ftdi->u132_lock);
1281 up(&ftdi->u132_lock);
1288 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1291 u8 addressofs = config_offset / 4;
1292 wait:if (ftdi->disconnected > 0) {
1296 down(&ftdi->u132_lock);
1297 command_size = ftdi->command_next - ftdi->command_head;
1298 if (command_size < COMMAND_SIZE) {
1299 struct u132_command *command = &ftdi->command[
1300 COMMAND_MASK & ftdi->command_next];
1301 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1302 command->length = 0x04;
1303 command->address = addressofs;
1304 command->width = 0x00 | (width & 0x0F);
1305 command->follows = 4;
1306 command->value = data;
1307 command->buffer = &command->value;
1308 ftdi->command_next += 1;
1309 ftdi_elan_kick_command_queue(ftdi);
1310 up(&ftdi->u132_lock);
1313 up(&ftdi->u132_lock);
1320 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1323 u8 addressofs = mem_offset / 4;
1324 wait:if (ftdi->disconnected > 0) {
1328 down(&ftdi->u132_lock);
1329 command_size = ftdi->command_next - ftdi->command_head;
1330 if (command_size < COMMAND_SIZE) {
1331 struct u132_command *command = &ftdi->command[
1332 COMMAND_MASK & ftdi->command_next];
1333 command->header = 0x00 | (cPCImemwr & 0x0F);
1334 command->length = 0x04;
1335 command->address = addressofs;
1336 command->width = 0x00 | (width & 0x0F);
1337 command->follows = 4;
1338 command->value = data;
1339 command->buffer = &command->value;
1340 ftdi->command_next += 1;
1341 ftdi_elan_kick_command_queue(ftdi);
1342 up(&ftdi->u132_lock);
1345 up(&ftdi->u132_lock);
1352 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1355 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1356 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1360 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1361 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1363 wait:if (ftdi->disconnected > 0) {
1368 down(&ftdi->u132_lock);
1369 command_size = ftdi->command_next - ftdi->command_head;
1370 respond_size = ftdi->respond_next - ftdi->respond_head;
1371 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1373 struct u132_command *command = &ftdi->command[
1374 COMMAND_MASK & ftdi->command_next];
1375 struct u132_respond *respond = &ftdi->respond[
1376 RESPOND_MASK & ftdi->respond_next];
1377 int result = -ENODEV;
1378 respond->result = &result;
1379 respond->header = command->header = 0x00 | cPCIu132rd;
1380 command->length = 0x04;
1381 respond->address = command->address = cU132cmd_status;
1382 command->width = 0x00;
1383 command->follows = 0;
1385 command->buffer = NULL;
1386 respond->value = data;
1387 init_completion(&respond->wait_completion);
1388 ftdi->command_next += 1;
1389 ftdi->respond_next += 1;
1390 ftdi_elan_kick_command_queue(ftdi);
1391 up(&ftdi->u132_lock);
1392 wait_for_completion(&respond->wait_completion);
1395 up(&ftdi->u132_lock);
1402 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1403 u8 width, u32 *data)
1405 u8 addressofs = config_offset / 4;
1406 wait:if (ftdi->disconnected > 0) {
1411 down(&ftdi->u132_lock);
1412 command_size = ftdi->command_next - ftdi->command_head;
1413 respond_size = ftdi->respond_next - ftdi->respond_head;
1414 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1416 struct u132_command *command = &ftdi->command[
1417 COMMAND_MASK & ftdi->command_next];
1418 struct u132_respond *respond = &ftdi->respond[
1419 RESPOND_MASK & ftdi->respond_next];
1420 int result = -ENODEV;
1421 respond->result = &result;
1422 respond->header = command->header = 0x00 | (cPCIcfgrd &
1424 command->length = 0x04;
1425 respond->address = command->address = addressofs;
1426 command->width = 0x00 | (width & 0x0F);
1427 command->follows = 0;
1429 command->buffer = NULL;
1430 respond->value = data;
1431 init_completion(&respond->wait_completion);
1432 ftdi->command_next += 1;
1433 ftdi->respond_next += 1;
1434 ftdi_elan_kick_command_queue(ftdi);
1435 up(&ftdi->u132_lock);
1436 wait_for_completion(&respond->wait_completion);
1439 up(&ftdi->u132_lock);
1446 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1447 u8 width, u32 *data)
1449 u8 addressofs = mem_offset / 4;
1450 wait:if (ftdi->disconnected > 0) {
1455 down(&ftdi->u132_lock);
1456 command_size = ftdi->command_next - ftdi->command_head;
1457 respond_size = ftdi->respond_next - ftdi->respond_head;
1458 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1460 struct u132_command *command = &ftdi->command[
1461 COMMAND_MASK & ftdi->command_next];
1462 struct u132_respond *respond = &ftdi->respond[
1463 RESPOND_MASK & ftdi->respond_next];
1464 int result = -ENODEV;
1465 respond->result = &result;
1466 respond->header = command->header = 0x00 | (cPCImemrd &
1468 command->length = 0x04;
1469 respond->address = command->address = addressofs;
1470 command->width = 0x00 | (width & 0x0F);
1471 command->follows = 0;
1473 command->buffer = NULL;
1474 respond->value = data;
1475 init_completion(&respond->wait_completion);
1476 ftdi->command_next += 1;
1477 ftdi->respond_next += 1;
1478 ftdi_elan_kick_command_queue(ftdi);
1479 up(&ftdi->u132_lock);
1480 wait_for_completion(&respond->wait_completion);
1483 up(&ftdi->u132_lock);
1490 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1491 u8 width, u32 *data)
1493 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1494 if (ftdi->initialized == 0) {
1497 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1501 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1502 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1503 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1504 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1505 int toggle_bits, int error_count, int condition_code, int repeat_number,
1506 int halted, int skipped, int actual, int non_null))
1508 u8 ed = ed_number - 1;
1509 wait:if (ftdi->disconnected > 0) {
1511 } else if (ftdi->initialized == 0) {
1515 down(&ftdi->u132_lock);
1516 command_size = ftdi->command_next - ftdi->command_head;
1517 if (command_size < COMMAND_SIZE) {
1518 struct u132_target *target = &ftdi->target[ed];
1519 struct u132_command *command = &ftdi->command[
1520 COMMAND_MASK & ftdi->command_next];
1521 command->header = 0x80 | (ed << 5);
1522 command->length = 0x8007;
1523 command->address = (toggle_bits << 6) | (ep_number << 2)
1525 command->width = usb_maxpacket(urb->dev, urb->pipe,
1526 usb_pipeout(urb->pipe));
1527 command->follows = 8;
1529 command->buffer = urb->setup_packet;
1530 target->callback = callback;
1531 target->endp = endp;
1534 ftdi->command_next += 1;
1535 ftdi_elan_kick_command_queue(ftdi);
1536 up(&ftdi->u132_lock);
1539 up(&ftdi->u132_lock);
1546 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1547 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1548 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1549 int toggle_bits, int error_count, int condition_code, int repeat_number,
1550 int halted, int skipped, int actual, int non_null))
1552 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1553 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1554 ep_number, toggle_bits, callback);
1558 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1559 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1560 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1561 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1562 int toggle_bits, int error_count, int condition_code, int repeat_number,
1563 int halted, int skipped, int actual, int non_null))
1565 u8 ed = ed_number - 1;
1566 wait:if (ftdi->disconnected > 0) {
1568 } else if (ftdi->initialized == 0) {
1572 down(&ftdi->u132_lock);
1573 command_size = ftdi->command_next - ftdi->command_head;
1574 if (command_size < COMMAND_SIZE) {
1575 struct u132_target *target = &ftdi->target[ed];
1576 struct u132_command *command = &ftdi->command[
1577 COMMAND_MASK & ftdi->command_next];
1578 int remaining_length = urb->transfer_buffer_length -
1580 command->header = 0x82 | (ed << 5);
1581 if (remaining_length == 0) {
1582 command->length = 0x0000;
1583 } else if (remaining_length > 1024) {
1584 command->length = 0x8000 | 1023;
1586 command->length = 0x8000 | (remaining_length -
1588 command->address = (toggle_bits << 6) | (ep_number << 2)
1590 command->width = usb_maxpacket(urb->dev, urb->pipe,
1591 usb_pipeout(urb->pipe));
1592 command->follows = 0;
1594 command->buffer = NULL;
1595 target->callback = callback;
1596 target->endp = endp;
1599 ftdi->command_next += 1;
1600 ftdi_elan_kick_command_queue(ftdi);
1601 up(&ftdi->u132_lock);
1604 up(&ftdi->u132_lock);
1611 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1612 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1613 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1614 int toggle_bits, int error_count, int condition_code, int repeat_number,
1615 int halted, int skipped, int actual, int non_null))
1617 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1618 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1619 ep_number, toggle_bits, callback);
1623 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1624 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1625 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1626 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1627 int toggle_bits, int error_count, int condition_code, int repeat_number,
1628 int halted, int skipped, int actual, int non_null))
1630 u8 ed = ed_number - 1;
1631 wait:if (ftdi->disconnected > 0) {
1633 } else if (ftdi->initialized == 0) {
1637 down(&ftdi->u132_lock);
1638 command_size = ftdi->command_next - ftdi->command_head;
1639 if (command_size < COMMAND_SIZE) {
1640 struct u132_target *target = &ftdi->target[ed];
1641 struct u132_command *command = &ftdi->command[
1642 COMMAND_MASK & ftdi->command_next];
1643 command->header = 0x81 | (ed << 5);
1644 command->length = 0x0000;
1645 command->address = (toggle_bits << 6) | (ep_number << 2)
1647 command->width = usb_maxpacket(urb->dev, urb->pipe,
1648 usb_pipeout(urb->pipe));
1649 command->follows = 0;
1651 command->buffer = NULL;
1652 target->callback = callback;
1653 target->endp = endp;
1656 ftdi->command_next += 1;
1657 ftdi_elan_kick_command_queue(ftdi);
1658 up(&ftdi->u132_lock);
1661 up(&ftdi->u132_lock);
1668 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1669 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1670 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1671 int toggle_bits, int error_count, int condition_code, int repeat_number,
1672 int halted, int skipped, int actual, int non_null))
1674 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1675 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1676 ep_number, toggle_bits, callback);
1680 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1681 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1682 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1683 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1684 int toggle_bits, int error_count, int condition_code, int repeat_number,
1685 int halted, int skipped, int actual, int non_null))
1687 u8 ed = ed_number - 1;
1688 wait:if (ftdi->disconnected > 0) {
1690 } else if (ftdi->initialized == 0) {
1694 down(&ftdi->u132_lock);
1695 command_size = ftdi->command_next - ftdi->command_head;
1696 if (command_size < COMMAND_SIZE) {
1700 char data[30 *3 + 4];
1702 int m = (sizeof(data) - 1) / 3;
1704 struct u132_target *target = &ftdi->target[ed];
1705 struct u132_command *command = &ftdi->command[
1706 COMMAND_MASK & ftdi->command_next];
1707 command->header = 0x81 | (ed << 5);
1708 command->address = (toggle_bits << 6) | (ep_number << 2)
1710 command->width = usb_maxpacket(urb->dev, urb->pipe,
1711 usb_pipeout(urb->pipe));
1712 command->follows = min(1024,
1713 urb->transfer_buffer_length -
1714 urb->actual_length);
1716 command->buffer = urb->transfer_buffer +
1718 command->length = 0x8000 | (command->follows - 1);
1719 b = command->buffer;
1720 urb_size = command->follows;
1722 while (urb_size-- > 0) {
1724 } else if (i++ < m) {
1725 int w = sprintf(d, " %02X", *b++);
1729 d += sprintf(d, " ..");
1731 target->callback = callback;
1732 target->endp = endp;
1735 ftdi->command_next += 1;
1736 ftdi_elan_kick_command_queue(ftdi);
1737 up(&ftdi->u132_lock);
1740 up(&ftdi->u132_lock);
1747 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1748 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1749 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1750 int toggle_bits, int error_count, int condition_code, int repeat_number,
1751 int halted, int skipped, int actual, int non_null))
1753 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1754 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1755 ep_number, toggle_bits, callback);
1759 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1760 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1761 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1762 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1763 int toggle_bits, int error_count, int condition_code, int repeat_number,
1764 int halted, int skipped, int actual, int non_null))
1766 u8 ed = ed_number - 1;
1767 wait:if (ftdi->disconnected > 0) {
1769 } else if (ftdi->initialized == 0) {
1773 down(&ftdi->u132_lock);
1774 command_size = ftdi->command_next - ftdi->command_head;
1775 if (command_size < COMMAND_SIZE) {
1776 int remaining_length = urb->transfer_buffer_length -
1778 struct u132_target *target = &ftdi->target[ed];
1779 struct u132_command *command = &ftdi->command[
1780 COMMAND_MASK & ftdi->command_next];
1781 command->header = 0x83 | (ed << 5);
1782 if (remaining_length == 0) {
1783 command->length = 0x0000;
1784 } else if (remaining_length > 1024) {
1785 command->length = 0x8000 | 1023;
1787 command->length = 0x8000 | (remaining_length -
1789 command->address = (toggle_bits << 6) | (ep_number << 2)
1791 command->width = usb_maxpacket(urb->dev, urb->pipe,
1792 usb_pipeout(urb->pipe));
1793 command->follows = 0;
1795 command->buffer = NULL;
1796 target->callback = callback;
1797 target->endp = endp;
1800 ftdi->command_next += 1;
1801 ftdi_elan_kick_command_queue(ftdi);
1802 up(&ftdi->u132_lock);
1805 up(&ftdi->u132_lock);
1812 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1813 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1814 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1815 int toggle_bits, int error_count, int condition_code, int repeat_number,
1816 int halted, int skipped, int actual, int non_null))
1818 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1819 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1820 ep_number, toggle_bits, callback);
1824 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1825 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1828 u8 ed = ed_number - 1;
1829 if (ftdi->disconnected > 0) {
1831 } else if (ftdi->initialized == 0) {
1834 struct u132_target *target = &ftdi->target[ed];
1835 down(&ftdi->u132_lock);
1836 if (target->abandoning > 0) {
1837 up(&ftdi->u132_lock);
1840 target->abandoning = 1;
1841 wait_1:if (target->active == 1) {
1842 int command_size = ftdi->command_next -
1844 if (command_size < COMMAND_SIZE) {
1845 struct u132_command *command =
1846 &ftdi->command[COMMAND_MASK &
1847 ftdi->command_next];
1848 command->header = 0x80 | (ed << 5) |
1850 command->length = 0x00;
1851 command->address = 0x00;
1852 command->width = 0x00;
1853 command->follows = 0;
1855 command->buffer = &command->value;
1856 ftdi->command_next += 1;
1857 ftdi_elan_kick_command_queue(ftdi);
1859 up(&ftdi->u132_lock);
1861 down(&ftdi->u132_lock);
1865 up(&ftdi->u132_lock);
1871 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1874 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1875 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1879 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1880 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1882 int retry_on_empty = 10;
1883 int retry_on_timeout = 5;
1884 int retry_on_status = 20;
1886 int packet_bytes = 0;
1887 int retval = usb_bulk_msg(ftdi->udev,
1888 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1889 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1890 &packet_bytes, msecs_to_jiffies(100));
1891 if (packet_bytes > 2) {
1892 char diag[30 *3 + 4];
1894 int m = (sizeof(diag) - 1) / 3;
1895 char *b = ftdi->bulk_in_buffer;
1898 while (packet_bytes-- > 0) {
1900 if (bytes_read < m) {
1901 d += sprintf(d, " %02X",
1903 } else if (bytes_read > m) {
1905 d += sprintf(d, " ..");
1910 } else if (packet_bytes > 1) {
1911 char s1 = ftdi->bulk_in_buffer[0];
1912 char s2 = ftdi->bulk_in_buffer[1];
1913 if (s1 == 0x31 && s2 == 0x60) {
1915 } else if (retry_on_status-- > 0) {
1918 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1922 } else if (packet_bytes > 0) {
1923 char b1 = ftdi->bulk_in_buffer[0];
1924 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1925 "TDI = %02X\n", b1);
1926 if (retry_on_status-- > 0) {
1929 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1933 } else if (retval == -ETIMEDOUT) {
1934 if (retry_on_timeout-- > 0) {
1937 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1941 } else if (retval == 0) {
1942 if (retry_on_empty-- > 0) {
1945 dev_err(&ftdi->udev->dev, "empty packet retry l"
1950 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1959 * send the long flush sequence
1962 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1969 urb = usb_alloc_urb(0, GFP_KERNEL);
1971 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1975 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1977 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
1984 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1985 ftdi->bulk_out_endpointAddr), buf, i,
1986 ftdi_elan_write_bulk_callback, ftdi);
1987 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1988 retval = usb_submit_urb(urb, GFP_KERNEL);
1990 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
1991 "flush sequence\n");
1992 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2002 * send the reset sequence
2005 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2012 urb = usb_alloc_urb(0, GFP_KERNEL);
2014 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2018 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2020 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2029 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2030 ftdi->bulk_out_endpointAddr), buf, i,
2031 ftdi_elan_write_bulk_callback, ftdi);
2032 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2033 retval = usb_submit_urb(urb, GFP_KERNEL);
2035 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2036 "reset sequence\n");
2037 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2045 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2049 int retry_on_timeout = 5;
2050 int retry_on_empty = 10;
2052 retval = ftdi_elan_flush_input_fifo(ftdi);
2055 ftdi->bulk_in_left = 0;
2056 ftdi->bulk_in_last = -1;
2057 while (long_stop-- > 0) {
2060 retval = ftdi_elan_synchronize_flush(ftdi);
2063 retval = ftdi_elan_flush_input_fifo(ftdi);
2066 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2072 int packet_bytes = 0;
2073 retval = usb_bulk_msg(ftdi->udev,
2074 usb_rcvbulkpipe(ftdi->udev,
2075 ftdi->bulk_in_endpointAddr),
2076 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2077 &packet_bytes, msecs_to_jiffies(500));
2078 if (packet_bytes > 2) {
2079 char diag[30 *3 + 4];
2081 int m = (sizeof(diag) - 1) / 3;
2082 char *b = ftdi->bulk_in_buffer;
2084 unsigned char c = 0;
2086 while (packet_bytes-- > 0) {
2088 if (bytes_read < m) {
2089 d += sprintf(d, " %02X", c);
2090 } else if (bytes_read > m) {
2092 d += sprintf(d, " ..");
2101 } else if (read_stop-- > 0) {
2104 dev_err(&ftdi->udev->dev, "retr"
2105 "y limit reached\n");
2109 } else if (packet_bytes > 1) {
2110 unsigned char s1 = ftdi->bulk_in_buffer[0];
2111 unsigned char s2 = ftdi->bulk_in_buffer[1];
2112 if (s1 == 0x31 && s2 == 0x00) {
2113 if (read_stuck-- > 0) {
2117 } else if (s1 == 0x31 && s2 == 0x60) {
2118 if (read_stop-- > 0) {
2121 dev_err(&ftdi->udev->dev, "retr"
2122 "y limit reached\n");
2126 if (read_stop-- > 0) {
2129 dev_err(&ftdi->udev->dev, "retr"
2130 "y limit reached\n");
2134 } else if (packet_bytes > 0) {
2135 if (read_stop-- > 0) {
2138 dev_err(&ftdi->udev->dev, "retry limit "
2142 } else if (retval == -ETIMEDOUT) {
2143 if (retry_on_timeout-- > 0) {
2146 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2147 "try limit reached\n");
2150 } else if (retval == 0) {
2151 if (retry_on_empty-- > 0) {
2154 dev_err(&ftdi->udev->dev, "empty packet"
2155 " retry limit reached\n");
2160 dev_err(&ftdi->udev->dev, "error = %d\n",
2162 if (read_stop-- > 0) {
2165 dev_err(&ftdi->udev->dev, "retry limit "
2172 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2176 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2178 int retry_on_empty = 10;
2179 int retry_on_timeout = 5;
2180 int retry_on_status = 50;
2182 int packet_bytes = 0;
2183 int retval = usb_bulk_msg(ftdi->udev,
2184 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2185 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2186 &packet_bytes, msecs_to_jiffies(1000));
2187 if (packet_bytes > 2) {
2188 char diag[30 *3 + 4];
2190 int m = (sizeof(diag) - 1) / 3;
2191 char *b = ftdi->bulk_in_buffer;
2194 while (packet_bytes-- > 0) {
2196 if (bytes_read < m) {
2197 d += sprintf(d, " %02X",
2199 } else if (bytes_read > m) {
2201 d += sprintf(d, " ..");
2206 } else if (packet_bytes > 1) {
2207 char s1 = ftdi->bulk_in_buffer[0];
2208 char s2 = ftdi->bulk_in_buffer[1];
2209 if (s1 == 0x31 && s2 == 0x60) {
2211 } else if (retry_on_status-- > 0) {
2216 } else if (packet_bytes > 0) {
2217 char b1 = ftdi->bulk_in_buffer[0];
2218 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2219 "TDI = %02X\n", b1);
2220 if (retry_on_status-- > 0) {
2224 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2228 } else if (retval == -ETIMEDOUT) {
2229 if (retry_on_timeout-- > 0) {
2232 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2236 } else if (retval == 0) {
2237 if (retry_on_empty-- > 0) {
2240 dev_err(&ftdi->udev->dev, "empty packet retry l"
2245 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2252 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2254 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2257 if (ftdi->controlreg & 0x00400000) {
2258 if (ftdi->card_ejected) {
2260 ftdi->card_ejected = 1;
2261 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2262 "%08X\n", ftdi->controlreg);
2266 u8 fn = ftdi->function - 1;
2267 int activePCIfn = fn << 8;
2272 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2276 pciVID = pcidata & 0xFFFF;
2277 pciPID = (pcidata >> 16) & 0xFFFF;
2278 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2279 ftdi->platform_data.device) {
2282 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2283 "ce=%04X pciPID=%04X\n",
2284 ftdi->platform_data.vendor, pciVID,
2285 ftdi->platform_data.device, pciPID);
2291 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2300 int activePCIfn = 0;
2303 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2306 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2310 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2313 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2316 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2319 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2322 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2326 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2329 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2332 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2335 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2338 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2342 for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
2343 activePCIfn = fn << 8;
2344 ftdi->function = fn + 1;
2345 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2349 pciVID = pcidata & 0xFFFF;
2350 pciPID = (pcidata >> 16) & 0xFFFF;
2351 if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
2353 } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
2355 } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
2357 } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
2359 } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
2362 if (foundOHCI == 0) {
2365 ftdi->platform_data.vendor = pciVID;
2366 ftdi->platform_data.device = pciPID;
2367 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2371 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2375 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2379 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2383 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2388 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2392 latence_timer &= 0xFFFF00FF;
2393 latence_timer |= 0x00001600;
2394 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2398 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2403 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2407 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2414 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2419 int reset_repeat = 0;
2421 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
2425 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2430 if (reset_repeat++ > 100) {
2440 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
2443 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2447 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
2450 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2454 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
2457 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2461 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
2464 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2469 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
2472 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2476 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2480 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2484 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
2487 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2491 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2495 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2499 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
2502 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2507 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
2511 power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2514 if (!(pcidata & 1)) {
2520 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2524 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2528 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2531 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2535 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
2538 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2544 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2549 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2554 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2558 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2561 dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
2562 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2571 * we use only the first bulk-in and bulk-out endpoints
2573 static int ftdi_elan_probe(struct usb_interface *interface,
2574 const struct usb_device_id *id)
2576 struct usb_host_interface *iface_desc;
2577 struct usb_endpoint_descriptor *endpoint;
2580 int retval = -ENOMEM;
2581 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2583 printk(KERN_ERR "Out of memory\n");
2586 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2587 down(&ftdi_module_lock);
2588 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2589 ftdi->sequence_num = ++ftdi_instances;
2590 up(&ftdi_module_lock);
2591 ftdi_elan_init_kref(ftdi);
2592 init_MUTEX(&ftdi->sw_lock);
2593 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2594 ftdi->interface = interface;
2595 init_MUTEX(&ftdi->u132_lock);
2597 iface_desc = interface->cur_altsetting;
2598 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2599 endpoint = &iface_desc->endpoint[i].desc;
2600 if (!ftdi->bulk_in_endpointAddr &&
2601 usb_endpoint_is_bulk_in(endpoint)) {
2602 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2603 ftdi->bulk_in_size = buffer_size;
2604 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2605 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2606 if (!ftdi->bulk_in_buffer) {
2607 dev_err(&ftdi->udev->dev, "Could not allocate b"
2613 if (!ftdi->bulk_out_endpointAddr &&
2614 usb_endpoint_is_bulk_out(endpoint)) {
2615 ftdi->bulk_out_endpointAddr =
2616 endpoint->bEndpointAddress;
2619 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2620 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2621 "-out endpoints\n");
2625 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2626 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2627 ftdi->bulk_out_endpointAddr);
2628 usb_set_intfdata(interface, ftdi);
2629 if (iface_desc->desc.bInterfaceNumber == 0 &&
2630 ftdi->bulk_in_endpointAddr == 0x81 &&
2631 ftdi->bulk_out_endpointAddr == 0x02) {
2632 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2634 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2636 usb_set_intfdata(interface, NULL);
2640 ftdi->class = &ftdi_elan_jtag_class;
2641 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2642 "%d now attached to ftdi%d\n", ftdi,
2643 iface_desc->desc.bInterfaceNumber,
2647 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2648 ftdi->bulk_in_endpointAddr == 0x83 &&
2649 ftdi->bulk_out_endpointAddr == 0x04) {
2651 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2652 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2653 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2654 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2655 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2656 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2659 dev_err(&ftdi->udev->dev,
2660 "Could not find ELAN's U132 device\n");
2665 ftdi_elan_put_kref(ftdi);
2670 static void ftdi_elan_disconnect(struct usb_interface *interface)
2672 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2673 ftdi->disconnected += 1;
2675 int minor = interface->minor;
2676 struct usb_class_driver *class = ftdi->class;
2677 usb_set_intfdata(interface, NULL);
2678 usb_deregister_dev(interface, class);
2679 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2680 "or %d now disconnected\n", minor);
2682 ftdi_status_cancel_work(ftdi);
2683 ftdi_command_cancel_work(ftdi);
2684 ftdi_response_cancel_work(ftdi);
2685 ftdi_elan_abandon_completions(ftdi);
2686 ftdi_elan_abandon_targets(ftdi);
2687 if (ftdi->registered) {
2688 platform_device_unregister(&ftdi->platform_dev);
2689 ftdi->synchronized = 0;
2690 ftdi->enumerated = 0;
2691 ftdi->registered = 0;
2693 flush_workqueue(status_queue);
2694 flush_workqueue(command_queue);
2695 flush_workqueue(respond_queue);
2696 ftdi->disconnected += 1;
2697 usb_set_intfdata(interface, NULL);
2698 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2699 "face now disconnected\n");
2701 ftdi_elan_put_kref(ftdi);
2704 static struct usb_driver ftdi_elan_driver = {
2705 .name = "ftdi-elan",
2706 .probe = ftdi_elan_probe,
2707 .disconnect = ftdi_elan_disconnect,
2708 .id_table = ftdi_elan_table,
2710 static int __init ftdi_elan_init(void)
2713 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2714 __TIME__, __DATE__);
2715 init_MUTEX(&ftdi_module_lock);
2716 INIT_LIST_HEAD(&ftdi_static_list);
2717 status_queue = create_singlethread_workqueue("ftdi-status-control");
2718 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2719 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2720 result = usb_register(&ftdi_elan_driver);
2722 printk(KERN_ERR "usb_register failed. Error number %d\n",
2727 static void __exit ftdi_elan_exit(void)
2729 struct usb_ftdi *ftdi;
2730 struct usb_ftdi *temp;
2731 usb_deregister(&ftdi_elan_driver);
2732 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2733 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2734 ftdi_status_cancel_work(ftdi);
2735 ftdi_command_cancel_work(ftdi);
2736 ftdi_response_cancel_work(ftdi);
2737 } flush_workqueue(status_queue);
2738 destroy_workqueue(status_queue);
2739 status_queue = NULL;
2740 flush_workqueue(command_queue);
2741 destroy_workqueue(command_queue);
2742 command_queue = NULL;
2743 flush_workqueue(respond_queue);
2744 destroy_workqueue(respond_queue);
2745 respond_queue = NULL;
2749 module_init(ftdi_elan_init);
2750 module_exit(ftdi_elan_exit);