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/config.h>
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/list.h>
43 #include <linux/ioctl.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <linux/kref.h>
47 #include <asm/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/workqueue.h>
50 #include <linux/platform_device.h>
51 MODULE_AUTHOR("Tony Olech");
52 MODULE_DESCRIPTION("FTDI ELAN driver");
53 MODULE_LICENSE("GPL");
54 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55 extern struct platform_driver u132_platform_driver
;
56 static struct workqueue_struct
*status_queue
;
57 static struct workqueue_struct
*command_queue
;
58 static struct workqueue_struct
*respond_queue
;
60 * ftdi_module_lock exists to protect access to global variables
63 static struct semaphore ftdi_module_lock
;
64 static int ftdi_instances
= 0;
65 static struct list_head ftdi_static_list
;
67 * end of the global variables protected by ftdi_module_lock
70 #define TD_DEVNOTRESP 5
71 /* Define these values to match your devices*/
72 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
73 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
74 /* table of devices that work with this driver*/
75 static struct usb_device_id ftdi_elan_table
[] = {
76 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID
, USB_FTDI_ELAN_PRODUCT_ID
)},
77 { /* Terminating entry */ }
80 MODULE_DEVICE_TABLE(usb
, ftdi_elan_table
);
81 /* only the jtag(firmware upgrade device) interface requires
82 * a device file and corresponding minor number, but the
83 * interface is created unconditionally - I suppose it could
84 * be configured or not according to a module parameter.
85 * But since we(now) require one interface per device,
86 * and since it unlikely that a normal installation would
87 * require more than a couple of elan-ftdi devices, 8 seems
88 * like a reasonable limit to have here, and if someone
89 * really requires more than 8 devices, then they can frig the
92 #define USB_FTDI_ELAN_MINOR_BASE 192
93 #define COMMAND_BITS 5
94 #define COMMAND_SIZE (1<<COMMAND_BITS)
95 #define COMMAND_MASK (COMMAND_SIZE-1)
105 #define RESPOND_BITS 5
106 #define RESPOND_SIZE (1<<RESPOND_BITS)
107 #define RESPOND_MASK (RESPOND_SIZE-1)
108 struct u132_respond
{
113 struct completion wait_completion
;
128 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
129 int toggle_bits
, int error_count
, int condition_code
,
130 int repeat_number
, int halted
, int skipped
, int actual
,
133 /* Structure to hold all of our device specific stuff*/
135 struct list_head ftdi_list
;
136 struct semaphore u132_lock
;
139 struct u132_command command
[COMMAND_SIZE
];
142 struct u132_respond respond
[RESPOND_SIZE
];
143 struct u132_target target
[4];
144 char device_name
[16];
145 unsigned synchronized
:1;
146 unsigned enumerated
:1;
147 unsigned registered
:1;
148 unsigned initialized
:1;
149 unsigned card_ejected
:1;
155 int status_queue_delay
;
156 struct semaphore sw_lock
;
157 struct usb_device
*udev
;
158 struct usb_interface
*interface
;
159 struct usb_class_driver
*class;
160 struct work_struct status_work
;
161 struct work_struct command_work
;
162 struct work_struct respond_work
;
163 struct u132_platform_data platform_data
;
164 struct resource resources
[0];
165 struct platform_device platform_dev
;
166 unsigned char *bulk_in_buffer
;
170 __u8 bulk_in_endpointAddr
;
171 __u8 bulk_out_endpointAddr
;
174 u8 response
[4 + 1024];
179 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
180 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
182 static struct usb_driver ftdi_elan_driver
;
183 static void ftdi_elan_delete(struct kref
*kref
)
185 struct usb_ftdi
*ftdi
= kref_to_usb_ftdi(kref
);
186 dev_warn(&ftdi
->udev
->dev
, "FREEING ftdi=%p\n", ftdi
);
187 usb_put_dev(ftdi
->udev
);
188 ftdi
->disconnected
+= 1;
189 down(&ftdi_module_lock
);
190 list_del_init(&ftdi
->ftdi_list
);
192 up(&ftdi_module_lock
);
193 kfree(ftdi
->bulk_in_buffer
);
194 ftdi
->bulk_in_buffer
= NULL
;
197 static void ftdi_elan_put_kref(struct usb_ftdi
*ftdi
)
199 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
202 static void ftdi_elan_get_kref(struct usb_ftdi
*ftdi
)
204 kref_get(&ftdi
->kref
);
207 static void ftdi_elan_init_kref(struct usb_ftdi
*ftdi
)
209 kref_init(&ftdi
->kref
);
212 static void ftdi_status_requeue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
215 if (queue_delayed_work(status_queue
, &ftdi
->status_work
, delta
))
217 } else if (queue_work(status_queue
, &ftdi
->status_work
))
219 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
223 static void ftdi_status_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
226 if (queue_delayed_work(status_queue
, &ftdi
->status_work
, delta
))
227 kref_get(&ftdi
->kref
);
228 } else if (queue_work(status_queue
, &ftdi
->status_work
))
229 kref_get(&ftdi
->kref
);
233 static void ftdi_status_cancel_work(struct usb_ftdi
*ftdi
)
235 if (cancel_delayed_work(&ftdi
->status_work
))
236 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
239 static void ftdi_command_requeue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
242 if (queue_delayed_work(command_queue
, &ftdi
->command_work
,
245 } else if (queue_work(command_queue
, &ftdi
->command_work
))
247 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
251 static void ftdi_command_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
254 if (queue_delayed_work(command_queue
, &ftdi
->command_work
,
256 kref_get(&ftdi
->kref
);
257 } else if (queue_work(command_queue
, &ftdi
->command_work
))
258 kref_get(&ftdi
->kref
);
262 static void ftdi_command_cancel_work(struct usb_ftdi
*ftdi
)
264 if (cancel_delayed_work(&ftdi
->command_work
))
265 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
268 static void ftdi_response_requeue_work(struct usb_ftdi
*ftdi
,
272 if (queue_delayed_work(respond_queue
, &ftdi
->respond_work
,
275 } else if (queue_work(respond_queue
, &ftdi
->respond_work
))
277 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
281 static void ftdi_respond_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
284 if (queue_delayed_work(respond_queue
, &ftdi
->respond_work
,
286 kref_get(&ftdi
->kref
);
287 } else if (queue_work(respond_queue
, &ftdi
->respond_work
))
288 kref_get(&ftdi
->kref
);
292 static void ftdi_response_cancel_work(struct usb_ftdi
*ftdi
)
294 if (cancel_delayed_work(&ftdi
->respond_work
))
295 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
298 void ftdi_elan_gone_away(struct platform_device
*pdev
)
300 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
301 ftdi
->gone_away
+= 1;
302 ftdi_elan_put_kref(ftdi
);
306 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away
);
307 void ftdi_release_platform_dev(struct device
*dev
)
312 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
313 struct u132_target
*target
, u8
*buffer
, int length
);
314 static void ftdi_elan_kick_command_queue(struct usb_ftdi
*ftdi
);
315 static void ftdi_elan_kick_respond_queue(struct usb_ftdi
*ftdi
);
316 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
);
317 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
);
318 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
);
319 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
);
320 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
);
321 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
);
322 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
);
323 static int ftdi_elan_hcd_init(struct usb_ftdi
*ftdi
)
326 if (ftdi
->platform_dev
.dev
.parent
)
328 ftdi_elan_get_kref(ftdi
);
329 ftdi
->platform_data
.potpg
= 100;
330 ftdi
->platform_data
.reset
= NULL
;
331 ftdi
->platform_dev
.id
= ftdi
->sequence_num
;
332 ftdi
->platform_dev
.resource
= ftdi
->resources
;
333 ftdi
->platform_dev
.num_resources
= ARRAY_SIZE(ftdi
->resources
);
334 ftdi
->platform_dev
.dev
.platform_data
= &ftdi
->platform_data
;
335 ftdi
->platform_dev
.dev
.parent
= NULL
;
336 ftdi
->platform_dev
.dev
.release
= ftdi_release_platform_dev
;
337 ftdi
->platform_dev
.dev
.dma_mask
= NULL
;
338 snprintf(ftdi
->device_name
, sizeof(ftdi
->device_name
), "u132_hcd");
339 ftdi
->platform_dev
.name
= ftdi
->device_name
;
340 dev_info(&ftdi
->udev
->dev
, "requesting module '%s'\n", "u132_hcd");
341 request_module("u132_hcd");
342 dev_info(&ftdi
->udev
->dev
, "registering '%s'\n",
343 ftdi
->platform_dev
.name
);
344 result
= platform_device_register(&ftdi
->platform_dev
);
348 static void ftdi_elan_abandon_completions(struct usb_ftdi
*ftdi
)
350 down(&ftdi
->u132_lock
);
351 while (ftdi
->respond_next
> ftdi
->respond_head
) {
352 struct u132_respond
*respond
= &ftdi
->respond
[RESPOND_MASK
&
353 ftdi
->respond_head
++];
354 *respond
->result
= -ESHUTDOWN
;
356 complete(&respond
->wait_completion
);
357 } up(&ftdi
->u132_lock
);
360 static void ftdi_elan_abandon_targets(struct usb_ftdi
*ftdi
)
363 down(&ftdi
->u132_lock
);
364 while (ed_number
-- > 0) {
365 struct u132_target
*target
= &ftdi
->target
[ed_number
];
366 if (target
->active
== 1) {
367 target
->condition_code
= TD_DEVNOTRESP
;
368 up(&ftdi
->u132_lock
);
369 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
370 down(&ftdi
->u132_lock
);
376 up(&ftdi
->u132_lock
);
379 static void ftdi_elan_flush_targets(struct usb_ftdi
*ftdi
)
382 down(&ftdi
->u132_lock
);
383 while (ed_number
-- > 0) {
384 struct u132_target
*target
= &ftdi
->target
[ed_number
];
385 target
->abandoning
= 1;
386 wait_1
:if (target
->active
== 1) {
387 int command_size
= ftdi
->command_next
-
389 if (command_size
< COMMAND_SIZE
) {
390 struct u132_command
*command
= &ftdi
->command
[
391 COMMAND_MASK
& ftdi
->command_next
];
392 command
->header
= 0x80 | (ed_number
<< 5) | 0x4;
393 command
->length
= 0x00;
394 command
->address
= 0x00;
395 command
->width
= 0x00;
396 command
->follows
= 0;
398 command
->buffer
= &command
->value
;
399 ftdi
->command_next
+= 1;
400 ftdi_elan_kick_command_queue(ftdi
);
402 up(&ftdi
->u132_lock
);
404 down(&ftdi
->u132_lock
);
408 wait_2
:if (target
->active
== 1) {
409 int command_size
= ftdi
->command_next
-
411 if (command_size
< COMMAND_SIZE
) {
412 struct u132_command
*command
= &ftdi
->command
[
413 COMMAND_MASK
& ftdi
->command_next
];
414 command
->header
= 0x90 | (ed_number
<< 5);
415 command
->length
= 0x00;
416 command
->address
= 0x00;
417 command
->width
= 0x00;
418 command
->follows
= 0;
420 command
->buffer
= &command
->value
;
421 ftdi
->command_next
+= 1;
422 ftdi_elan_kick_command_queue(ftdi
);
424 up(&ftdi
->u132_lock
);
426 down(&ftdi
->u132_lock
);
434 up(&ftdi
->u132_lock
);
437 static void ftdi_elan_cancel_targets(struct usb_ftdi
*ftdi
)
440 down(&ftdi
->u132_lock
);
441 while (ed_number
-- > 0) {
442 struct u132_target
*target
= &ftdi
->target
[ed_number
];
443 target
->abandoning
= 1;
444 wait
:if (target
->active
== 1) {
445 int command_size
= ftdi
->command_next
-
447 if (command_size
< COMMAND_SIZE
) {
448 struct u132_command
*command
= &ftdi
->command
[
449 COMMAND_MASK
& ftdi
->command_next
];
450 command
->header
= 0x80 | (ed_number
<< 5) | 0x4;
451 command
->length
= 0x00;
452 command
->address
= 0x00;
453 command
->width
= 0x00;
454 command
->follows
= 0;
456 command
->buffer
= &command
->value
;
457 ftdi
->command_next
+= 1;
458 ftdi_elan_kick_command_queue(ftdi
);
460 up(&ftdi
->u132_lock
);
462 down(&ftdi
->u132_lock
);
470 up(&ftdi
->u132_lock
);
473 static void ftdi_elan_kick_command_queue(struct usb_ftdi
*ftdi
)
475 ftdi_command_queue_work(ftdi
, 0);
479 static void ftdi_elan_command_work(void *data
)
481 struct usb_ftdi
*ftdi
= data
;
482 if (ftdi
->disconnected
> 0) {
483 ftdi_elan_put_kref(ftdi
);
486 int retval
= ftdi_elan_command_engine(ftdi
);
487 if (retval
== -ESHUTDOWN
) {
488 ftdi
->disconnected
+= 1;
489 } else if (retval
== -ENODEV
) {
490 ftdi
->disconnected
+= 1;
492 dev_err(&ftdi
->udev
->dev
, "command error %d\n", retval
);
493 ftdi_command_requeue_work(ftdi
, msecs_to_jiffies(10));
498 static void ftdi_elan_kick_respond_queue(struct usb_ftdi
*ftdi
)
500 ftdi_respond_queue_work(ftdi
, 0);
504 static void ftdi_elan_respond_work(void *data
)
506 struct usb_ftdi
*ftdi
= data
;
507 if (ftdi
->disconnected
> 0) {
508 ftdi_elan_put_kref(ftdi
);
511 int retval
= ftdi_elan_respond_engine(ftdi
);
513 } else if (retval
== -ESHUTDOWN
) {
514 ftdi
->disconnected
+= 1;
515 } else if (retval
== -ENODEV
) {
516 ftdi
->disconnected
+= 1;
517 } else if (retval
== -ENODEV
) {
518 ftdi
->disconnected
+= 1;
519 } else if (retval
== -EILSEQ
) {
520 ftdi
->disconnected
+= 1;
522 ftdi
->disconnected
+= 1;
523 dev_err(&ftdi
->udev
->dev
, "respond error %d\n", retval
);
525 if (ftdi
->disconnected
> 0) {
526 ftdi_elan_abandon_completions(ftdi
);
527 ftdi_elan_abandon_targets(ftdi
);
529 ftdi_response_requeue_work(ftdi
, msecs_to_jiffies(10));
536 * the sw_lock is initially held and will be freed
537 * after the FTDI has been synchronized
540 static void ftdi_elan_status_work(void *data
)
542 struct usb_ftdi
*ftdi
= data
;
543 int work_delay_in_msec
= 0;
544 if (ftdi
->disconnected
> 0) {
545 ftdi_elan_put_kref(ftdi
);
547 } else if (ftdi
->synchronized
== 0) {
548 down(&ftdi
->sw_lock
);
549 if (ftdi_elan_synchronize(ftdi
) == 0) {
550 ftdi
->synchronized
= 1;
551 ftdi_command_queue_work(ftdi
, 1);
552 ftdi_respond_queue_work(ftdi
, 1);
554 work_delay_in_msec
= 100;
556 dev_err(&ftdi
->udev
->dev
, "synchronize failed\n");
558 work_delay_in_msec
= 10 *1000;
560 } else if (ftdi
->stuck_status
> 0) {
561 if (ftdi_elan_stuck_waiting(ftdi
) == 0) {
562 ftdi
->stuck_status
= 0;
563 ftdi
->synchronized
= 0;
564 } else if ((ftdi
->stuck_status
++ % 60) == 1) {
565 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted "
566 "- please remove\n");
568 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted "
569 "- checked %d times\n", ftdi
->stuck_status
);
570 work_delay_in_msec
= 100;
571 } else if (ftdi
->enumerated
== 0) {
572 if (ftdi_elan_enumeratePCI(ftdi
) == 0) {
573 ftdi
->enumerated
= 1;
574 work_delay_in_msec
= 250;
576 work_delay_in_msec
= 1000;
577 } else if (ftdi
->initialized
== 0) {
578 if (ftdi_elan_setupOHCI(ftdi
) == 0) {
579 ftdi
->initialized
= 1;
580 work_delay_in_msec
= 500;
582 dev_err(&ftdi
->udev
->dev
, "initialized failed - trying "
583 "again in 10 seconds\n");
584 work_delay_in_msec
= 10 *1000;
586 } else if (ftdi
->registered
== 0) {
587 work_delay_in_msec
= 10;
588 if (ftdi_elan_hcd_init(ftdi
) == 0) {
589 ftdi
->registered
= 1;
591 dev_err(&ftdi
->udev
->dev
, "register failed\n");
592 work_delay_in_msec
= 250;
594 if (ftdi_elan_checkingPCI(ftdi
) == 0) {
595 work_delay_in_msec
= 250;
596 } else if (ftdi
->controlreg
& 0x00400000) {
597 if (ftdi
->gone_away
> 0) {
598 dev_err(&ftdi
->udev
->dev
, "PCI device eject con"
599 "firmed platform_dev.dev.parent=%p plat"
601 ftdi
->platform_dev
.dev
.parent
,
602 &ftdi
->platform_dev
.dev
);
603 platform_device_unregister(&ftdi
->platform_dev
);
604 ftdi
->platform_dev
.dev
.parent
= NULL
;
605 ftdi
->registered
= 0;
606 ftdi
->enumerated
= 0;
607 ftdi
->card_ejected
= 0;
608 ftdi
->initialized
= 0;
611 ftdi_elan_flush_targets(ftdi
);
612 work_delay_in_msec
= 250;
614 dev_err(&ftdi
->udev
->dev
, "PCI device has disappeared\n"
616 ftdi_elan_cancel_targets(ftdi
);
617 work_delay_in_msec
= 500;
618 ftdi
->enumerated
= 0;
619 ftdi
->initialized
= 0;
622 if (ftdi
->disconnected
> 0) {
623 ftdi_elan_put_kref(ftdi
);
626 ftdi_status_requeue_work(ftdi
,
627 msecs_to_jiffies(work_delay_in_msec
));
634 * file_operations for the jtag interface
636 * the usage count for the device is incremented on open()
637 * and decremented on release()
639 static int ftdi_elan_open(struct inode
*inode
, struct file
*file
)
641 int subminor
= iminor(inode
);
642 struct usb_interface
*interface
= usb_find_interface(&ftdi_elan_driver
,
645 printk(KERN_ERR
"can't find device for minor %d\n", subminor
);
648 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
652 if (down_interruptible(&ftdi
->sw_lock
)) {
655 ftdi_elan_get_kref(ftdi
);
656 file
->private_data
= ftdi
;
663 static int ftdi_elan_release(struct inode
*inode
, struct file
*file
)
665 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)file
->private_data
;
668 up(&ftdi
->sw_lock
); /* decrement the count on our device */
669 ftdi_elan_put_kref(ftdi
);
674 #define FTDI_ELAN_IOC_MAGIC 0xA1
675 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
676 static int ftdi_elan_ioctl(struct inode
*inode
, struct file
*file
,
677 unsigned int cmd
, unsigned long arg
)
680 case FTDI_ELAN_IOCDEBUG
:{
682 int size
= strncpy_from_user(line
,
683 (const char __user
*)arg
, sizeof(line
));
687 printk(KERN_ERR
"TODO: ioctl %s\n", line
);
699 * blocking bulk reads are used to get data from the device
702 static ssize_t
ftdi_elan_read(struct file
*file
, char __user
*buffer
,
703 size_t count
, loff_t
*ppos
)
705 char data
[30 *3 + 4];
707 int m
= (sizeof(data
) - 1) / 3;
709 int retry_on_empty
= 10;
710 int retry_on_timeout
= 5;
711 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)file
->private_data
;
712 if (ftdi
->disconnected
> 0) {
716 have
:if (ftdi
->bulk_in_left
> 0) {
718 char *p
= ++ftdi
->bulk_in_last
+ ftdi
->bulk_in_buffer
;
719 ftdi
->bulk_in_left
-= 1;
720 if (bytes_read
< m
) {
721 d
+= sprintf(d
, " %02X", 0x000000FF & *p
);
722 } else if (bytes_read
> m
) {
724 d
+= sprintf(d
, " ..");
725 if (copy_to_user(buffer
++, p
, 1)) {
734 more
:if (count
> 0) {
735 int packet_bytes
= 0;
736 int retval
= usb_bulk_msg(ftdi
->udev
,
737 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
738 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
739 &packet_bytes
, msecs_to_jiffies(50));
740 if (packet_bytes
> 2) {
741 ftdi
->bulk_in_left
= packet_bytes
- 2;
742 ftdi
->bulk_in_last
= 1;
744 } else if (retval
== -ETIMEDOUT
) {
745 if (retry_on_timeout
-- > 0) {
747 } else if (bytes_read
> 0) {
751 } else if (retval
== 0) {
752 if (retry_on_empty
-- > 0) {
762 static void ftdi_elan_write_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
764 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)urb
->context
;
765 if (urb
->status
&& !(urb
->status
== -ENOENT
|| urb
->status
==
766 -ECONNRESET
|| urb
->status
== -ESHUTDOWN
)) {
767 dev_err(&ftdi
->udev
->dev
, "urb=%p write bulk status received: %"
768 "d\n", urb
, urb
->status
);
770 usb_buffer_free(urb
->dev
, urb
->transfer_buffer_length
,
771 urb
->transfer_buffer
, urb
->transfer_dma
);
774 static int fill_buffer_with_all_queued_commands(struct usb_ftdi
*ftdi
,
775 char *buf
, int command_size
, int total_size
)
779 int I
= command_size
;
780 int i
= ftdi
->command_head
;
782 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
784 int F
= command
->follows
;
785 u8
*f
= command
->buffer
;
786 if (command
->header
& 0x80) {
787 ed_commands
|= 1 << (0x3 & (command
->header
>> 5));
789 buf
[b
++] = command
->header
;
790 buf
[b
++] = (command
->length
>> 0) & 0x00FF;
791 buf
[b
++] = (command
->length
>> 8) & 0x00FF;
792 buf
[b
++] = command
->address
;
793 buf
[b
++] = command
->width
;
801 static int ftdi_elan_total_command_size(struct usb_ftdi
*ftdi
, int command_size
)
804 int I
= command_size
;
805 int i
= ftdi
->command_head
;
807 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
809 total_size
+= 5 + command
->follows
;
813 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
)
820 int command_size
= ftdi
->command_next
- ftdi
->command_head
;
821 if (command_size
== 0)
823 total_size
= ftdi_elan_total_command_size(ftdi
, command_size
);
824 urb
= usb_alloc_urb(0, GFP_KERNEL
);
826 dev_err(&ftdi
->udev
->dev
, "could not get a urb to write %d comm"
827 "ands totaling %d bytes to the Uxxx\n", command_size
,
831 buf
= usb_buffer_alloc(ftdi
->udev
, total_size
, GFP_KERNEL
,
834 dev_err(&ftdi
->udev
->dev
, "could not get a buffer to write %d c"
835 "ommands totaling %d bytes to the Uxxx\n", command_size
,
840 ed_commands
= fill_buffer_with_all_queued_commands(ftdi
, buf
,
841 command_size
, total_size
);
842 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
843 ftdi
->bulk_out_endpointAddr
), buf
, total_size
,
844 ftdi_elan_write_bulk_callback
, ftdi
);
845 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
847 char diag
[40 *3 + 4];
851 int s
= (sizeof(diag
) - 1) / 3;
853 while (s
-- > 0 && m
-- > 0) {
854 if (s
> 0 || m
== 0) {
855 d
+= sprintf(d
, " %02X", *c
++);
857 d
+= sprintf(d
, " ..");
860 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
862 dev_err(&ftdi
->udev
->dev
, "failed %d to submit urb %p to write "
863 "%d commands totaling %d bytes to the Uxxx\n", retval
,
864 urb
, command_size
, total_size
);
865 usb_buffer_free(ftdi
->udev
, total_size
, buf
, urb
->transfer_dma
);
869 usb_free_urb(urb
); /* release our reference to this urb,
870 the USB core will eventually free it entirely */
871 ftdi
->command_head
+= command_size
;
872 ftdi_elan_kick_respond_queue(ftdi
);
876 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
877 struct u132_target
*target
, u8
*buffer
, int length
)
879 struct urb
*urb
= target
->urb
;
880 int halted
= target
->halted
;
881 int skipped
= target
->skipped
;
882 int actual
= target
->actual
;
883 int non_null
= target
->non_null
;
884 int toggle_bits
= target
->toggle_bits
;
885 int error_count
= target
->error_count
;
886 int condition_code
= target
->condition_code
;
887 int repeat_number
= target
->repeat_number
;
888 void (*callback
) (void *, struct urb
*, u8
*, int, int, int, int, int,
889 int, int, int, int) = target
->callback
;
891 target
->callback
= NULL
;
892 (*callback
) (target
->endp
, urb
, buffer
, length
, toggle_bits
,
893 error_count
, condition_code
, repeat_number
, halted
, skipped
,
897 static char *have_ed_set_response(struct usb_ftdi
*ftdi
,
898 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
901 int payload
= (ed_length
>> 0) & 0x07FF;
902 down(&ftdi
->u132_lock
);
904 target
->non_null
= (ed_length
>> 15) & 0x0001;
905 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
906 if (ed_type
== 0x02) {
907 if (payload
== 0 || target
->abandoning
> 0) {
908 target
->abandoning
= 0;
909 up(&ftdi
->u132_lock
);
910 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
915 return ftdi
->response
;
917 ftdi
->expected
= 4 + payload
;
919 up(&ftdi
->u132_lock
);
922 } else if (ed_type
== 0x03) {
923 if (payload
== 0 || target
->abandoning
> 0) {
924 target
->abandoning
= 0;
925 up(&ftdi
->u132_lock
);
926 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
931 return ftdi
->response
;
933 ftdi
->expected
= 4 + payload
;
935 up(&ftdi
->u132_lock
);
938 } else if (ed_type
== 0x01) {
939 target
->abandoning
= 0;
940 up(&ftdi
->u132_lock
);
941 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
946 return ftdi
->response
;
948 target
->abandoning
= 0;
949 up(&ftdi
->u132_lock
);
950 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
955 return ftdi
->response
;
959 static char *have_ed_get_response(struct usb_ftdi
*ftdi
,
960 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
963 down(&ftdi
->u132_lock
);
964 target
->condition_code
= TD_DEVNOTRESP
;
965 target
->actual
= (ed_length
>> 0) & 0x01FF;
966 target
->non_null
= (ed_length
>> 15) & 0x0001;
967 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
968 up(&ftdi
->u132_lock
);
970 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
971 target
->abandoning
= 0;
975 return ftdi
->response
;
980 * The engine tries to empty the FTDI fifo
982 * all responses found in the fifo data are dispatched thus
983 * the response buffer can only ever hold a maximum sized
984 * response from the Uxxx.
987 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
)
989 u8
*b
= ftdi
->response
+ ftdi
->recieved
;
991 int retry_on_empty
= 1;
992 int retry_on_timeout
= 3;
993 int empty_packets
= 0;
995 int packet_bytes
= 0;
996 int retval
= usb_bulk_msg(ftdi
->udev
,
997 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
998 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
999 &packet_bytes
, msecs_to_jiffies(500));
1000 char diag
[30 *3 + 4];
1002 int m
= packet_bytes
;
1003 u8
*c
= ftdi
->bulk_in_buffer
;
1004 int s
= (sizeof(diag
) - 1) / 3;
1006 while (s
-- > 0 && m
-- > 0) {
1007 if (s
> 0 || m
== 0) {
1008 d
+= sprintf(d
, " %02X", *c
++);
1010 d
+= sprintf(d
, " ..");
1012 if (packet_bytes
> 2) {
1013 ftdi
->bulk_in_left
= packet_bytes
- 2;
1014 ftdi
->bulk_in_last
= 1;
1016 } else if (retval
== -ETIMEDOUT
) {
1017 if (retry_on_timeout
-- > 0) {
1018 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packe"
1019 "t_bytes = %d with total %d bytes%s\n",
1020 packet_bytes
, bytes_read
, diag
);
1022 } else if (bytes_read
> 0) {
1023 dev_err(&ftdi
->udev
->dev
, "ONLY %d bytes%s\n",
1027 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packe"
1028 "t_bytes = %d with total %d bytes%s\n",
1029 packet_bytes
, bytes_read
, diag
);
1032 } else if (retval
== -EILSEQ
) {
1033 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes"
1034 " = %d with total %d bytes%s\n", retval
,
1035 packet_bytes
, bytes_read
, diag
);
1037 } else if (retval
) {
1038 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes"
1039 " = %d with total %d bytes%s\n", retval
,
1040 packet_bytes
, bytes_read
, diag
);
1042 } else if (packet_bytes
== 2) {
1043 unsigned char s0
= ftdi
->bulk_in_buffer
[0];
1044 unsigned char s1
= ftdi
->bulk_in_buffer
[1];
1046 if (s0
== 0x31 && s1
== 0x60) {
1047 if (retry_on_empty
-- > 0) {
1051 } else if (s0
== 0x31 && s1
== 0x00) {
1052 if (retry_on_empty
-- > 0) {
1057 if (retry_on_empty
-- > 0) {
1062 } else if (packet_bytes
== 1) {
1063 if (retry_on_empty
-- > 0) {
1068 if (retry_on_empty
-- > 0) {
1077 have
:if (ftdi
->bulk_in_left
> 0) {
1078 u8 c
= ftdi
->bulk_in_buffer
[++ftdi
->bulk_in_last
];
1080 ftdi
->bulk_in_left
-= 1;
1081 if (ftdi
->recieved
== 0 && c
== 0xFF) {
1085 if (++ftdi
->recieved
< ftdi
->expected
) {
1087 } else if (ftdi
->ed_found
) {
1088 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
1089 u16 ed_length
= (ftdi
->response
[2] << 8) |
1091 struct u132_target
*target
= &ftdi
->target
[ed_number
];
1092 int payload
= (ed_length
>> 0) & 0x07FF;
1093 char diag
[30 *3 + 4];
1096 u8
*c
= 4 + ftdi
->response
;
1097 int s
= (sizeof(diag
) - 1) / 3;
1099 while (s
-- > 0 && m
-- > 0) {
1100 if (s
> 0 || m
== 0) {
1101 d
+= sprintf(d
, " %02X", *c
++);
1103 d
+= sprintf(d
, " ..");
1105 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
1112 } else if (ftdi
->expected
== 8) {
1114 int respond_head
= ftdi
->respond_head
++;
1115 struct u132_respond
*respond
= &ftdi
->respond
[
1116 RESPOND_MASK
& respond_head
];
1117 u32 data
= ftdi
->response
[7];
1119 data
|= ftdi
->response
[6];
1121 data
|= ftdi
->response
[5];
1123 data
|= ftdi
->response
[4];
1124 *respond
->value
= data
;
1125 *respond
->result
= 0;
1126 complete(&respond
->wait_completion
);
1131 buscmd
= (ftdi
->response
[0] >> 0) & 0x0F;
1132 if (buscmd
== 0x00) {
1133 } else if (buscmd
== 0x02) {
1134 } else if (buscmd
== 0x06) {
1135 } else if (buscmd
== 0x0A) {
1137 dev_err(&ftdi
->udev
->dev
, "Uxxx unknown(%0X) va"
1138 "lue = %08X\n", buscmd
, data
);
1141 if ((ftdi
->response
[0] & 0x80) == 0x00) {
1145 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
1146 int ed_type
= (ftdi
->response
[0] >> 0) & 0x03;
1147 u16 ed_length
= (ftdi
->response
[2] << 8) |
1149 struct u132_target
*target
= &ftdi
->target
[
1151 target
->halted
= (ftdi
->response
[0] >> 3) &
1153 target
->skipped
= (ftdi
->response
[0] >> 2) &
1155 target
->toggle_bits
= (ftdi
->response
[3] >> 6)
1157 target
->error_count
= (ftdi
->response
[3] >> 4)
1159 target
->condition_code
= (ftdi
->response
[
1161 if ((ftdi
->response
[0] & 0x10) == 0x00) {
1162 b
= have_ed_set_response(ftdi
, target
,
1163 ed_length
, ed_number
, ed_type
,
1167 b
= have_ed_get_response(ftdi
, target
,
1168 ed_length
, ed_number
, ed_type
,
1180 * create a urb, and a buffer for it, and copy the data to the urb
1183 static ssize_t
ftdi_elan_write(struct file
*file
,
1184 const char __user
*user_buffer
, size_t count
,
1190 char data
[30 *3 + 4];
1192 const char __user
*s
= user_buffer
;
1193 int m
= (sizeof(data
) - 1) / 3;
1194 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)file
->private_data
;
1195 if (ftdi
->disconnected
> 0) {
1201 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1206 buf
= usb_buffer_alloc(ftdi
->udev
, count
, GFP_KERNEL
,
1207 &urb
->transfer_dma
);
1212 if (copy_from_user(buf
, user_buffer
, count
)) {
1216 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1217 ftdi
->bulk_out_endpointAddr
), buf
, count
,
1218 ftdi_elan_write_bulk_callback
, ftdi
);
1219 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1220 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1222 dev_err(&ftdi
->udev
->dev
, "failed submitting write urb, error %"
1231 d
+= sprintf(d
, " %02X", 0x000000FF & *s
++);
1233 d
+= sprintf(d
, " ..");
1237 d
+= sprintf(d
, " %02X", 0x000000FF & *s
++);
1241 error_4
: error_3
:usb_buffer_free(ftdi
->udev
, count
, buf
,
1243 error_2
:usb_free_urb(urb
);
1244 error_1
:return retval
;
1247 static struct file_operations ftdi_elan_fops
= {
1248 .owner
= THIS_MODULE
,
1249 .llseek
= no_llseek
,
1250 .ioctl
= ftdi_elan_ioctl
,
1251 .read
= ftdi_elan_read
,
1252 .write
= ftdi_elan_write
,
1253 .open
= ftdi_elan_open
,
1254 .release
= ftdi_elan_release
,
1258 * usb class driver info in order to get a minor number from the usb core,
1259 * and to have the device registered with the driver core
1261 static struct usb_class_driver ftdi_elan_jtag_class
= {
1262 .name
= "ftdi-%d-jtag",
1263 .fops
= &ftdi_elan_fops
,
1264 .minor_base
= USB_FTDI_ELAN_MINOR_BASE
,
1268 * the following definitions are for the
1269 * ELAN FPGA state machgine processor that
1270 * lies on the other side of the FTDI chip
1272 #define cPCIu132rd 0x0
1273 #define cPCIu132wr 0x1
1274 #define cPCIiord 0x2
1275 #define cPCIiowr 0x3
1276 #define cPCImemrd 0x6
1277 #define cPCImemwr 0x7
1278 #define cPCIcfgrd 0xA
1279 #define cPCIcfgwr 0xB
1280 #define cPCInull 0xF
1281 #define cU132cmd_status 0x0
1282 #define cU132flash 0x1
1283 #define cPIDsetup 0x0
1286 #define cPIDinonce 0x3
1287 #define cCCnoerror 0x0
1289 #define cCCbitstuff 0x2
1290 #define cCCtoggle 0x3
1291 #define cCCstall 0x4
1292 #define cCCnoresp 0x5
1293 #define cCCbadpid1 0x6
1294 #define cCCbadpid2 0x7
1295 #define cCCdataoverrun 0x8
1296 #define cCCdataunderrun 0x9
1297 #define cCCbuffoverrun 0xC
1298 #define cCCbuffunderrun 0xD
1299 #define cCCnotaccessed 0xF
1300 static int ftdi_elan_write_reg(struct usb_ftdi
*ftdi
, u32 data
)
1302 wait
:if (ftdi
->disconnected
> 0) {
1306 down(&ftdi
->u132_lock
);
1307 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1308 if (command_size
< COMMAND_SIZE
) {
1309 struct u132_command
*command
= &ftdi
->command
[
1310 COMMAND_MASK
& ftdi
->command_next
];
1311 command
->header
= 0x00 | cPCIu132wr
;
1312 command
->length
= 0x04;
1313 command
->address
= 0x00;
1314 command
->width
= 0x00;
1315 command
->follows
= 4;
1316 command
->value
= data
;
1317 command
->buffer
= &command
->value
;
1318 ftdi
->command_next
+= 1;
1319 ftdi_elan_kick_command_queue(ftdi
);
1320 up(&ftdi
->u132_lock
);
1323 up(&ftdi
->u132_lock
);
1330 static int ftdi_elan_write_config(struct usb_ftdi
*ftdi
, int config_offset
,
1333 u8 addressofs
= config_offset
/ 4;
1334 wait
:if (ftdi
->disconnected
> 0) {
1338 down(&ftdi
->u132_lock
);
1339 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1340 if (command_size
< COMMAND_SIZE
) {
1341 struct u132_command
*command
= &ftdi
->command
[
1342 COMMAND_MASK
& ftdi
->command_next
];
1343 command
->header
= 0x00 | (cPCIcfgwr
& 0x0F);
1344 command
->length
= 0x04;
1345 command
->address
= addressofs
;
1346 command
->width
= 0x00 | (width
& 0x0F);
1347 command
->follows
= 4;
1348 command
->value
= data
;
1349 command
->buffer
= &command
->value
;
1350 ftdi
->command_next
+= 1;
1351 ftdi_elan_kick_command_queue(ftdi
);
1352 up(&ftdi
->u132_lock
);
1355 up(&ftdi
->u132_lock
);
1362 static int ftdi_elan_write_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1365 u8 addressofs
= mem_offset
/ 4;
1366 wait
:if (ftdi
->disconnected
> 0) {
1370 down(&ftdi
->u132_lock
);
1371 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1372 if (command_size
< COMMAND_SIZE
) {
1373 struct u132_command
*command
= &ftdi
->command
[
1374 COMMAND_MASK
& ftdi
->command_next
];
1375 command
->header
= 0x00 | (cPCImemwr
& 0x0F);
1376 command
->length
= 0x04;
1377 command
->address
= addressofs
;
1378 command
->width
= 0x00 | (width
& 0x0F);
1379 command
->follows
= 4;
1380 command
->value
= data
;
1381 command
->buffer
= &command
->value
;
1382 ftdi
->command_next
+= 1;
1383 ftdi_elan_kick_command_queue(ftdi
);
1384 up(&ftdi
->u132_lock
);
1387 up(&ftdi
->u132_lock
);
1394 int usb_ftdi_elan_write_pcimem(struct platform_device
*pdev
, int mem_offset
,
1397 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1398 return ftdi_elan_write_pcimem(ftdi
, mem_offset
, width
, data
);
1402 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem
);
1403 static int ftdi_elan_read_reg(struct usb_ftdi
*ftdi
, u32
*data
)
1405 wait
:if (ftdi
->disconnected
> 0) {
1410 down(&ftdi
->u132_lock
);
1411 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1412 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1413 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1415 struct u132_command
*command
= &ftdi
->command
[
1416 COMMAND_MASK
& ftdi
->command_next
];
1417 struct u132_respond
*respond
= &ftdi
->respond
[
1418 RESPOND_MASK
& ftdi
->respond_next
];
1419 int result
= -ENODEV
;
1420 respond
->result
= &result
;
1421 respond
->header
= command
->header
= 0x00 | cPCIu132rd
;
1422 command
->length
= 0x04;
1423 respond
->address
= command
->address
= cU132cmd_status
;
1424 command
->width
= 0x00;
1425 command
->follows
= 0;
1427 command
->buffer
= NULL
;
1428 respond
->value
= data
;
1429 init_completion(&respond
->wait_completion
);
1430 ftdi
->command_next
+= 1;
1431 ftdi
->respond_next
+= 1;
1432 ftdi_elan_kick_command_queue(ftdi
);
1433 up(&ftdi
->u132_lock
);
1434 wait_for_completion(&respond
->wait_completion
);
1437 up(&ftdi
->u132_lock
);
1444 int usb_ftdi_elan_read_reg(struct platform_device
*pdev
, u32
*data
)
1446 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1447 return ftdi_elan_read_reg(ftdi
, data
);
1451 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_reg
);
1452 static int ftdi_elan_read_config(struct usb_ftdi
*ftdi
, int config_offset
,
1453 u8 width
, u32
*data
)
1455 u8 addressofs
= config_offset
/ 4;
1456 wait
:if (ftdi
->disconnected
> 0) {
1461 down(&ftdi
->u132_lock
);
1462 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1463 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1464 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1466 struct u132_command
*command
= &ftdi
->command
[
1467 COMMAND_MASK
& ftdi
->command_next
];
1468 struct u132_respond
*respond
= &ftdi
->respond
[
1469 RESPOND_MASK
& ftdi
->respond_next
];
1470 int result
= -ENODEV
;
1471 respond
->result
= &result
;
1472 respond
->header
= command
->header
= 0x00 | (cPCIcfgrd
&
1474 command
->length
= 0x04;
1475 respond
->address
= command
->address
= addressofs
;
1476 command
->width
= 0x00 | (width
& 0x0F);
1477 command
->follows
= 0;
1479 command
->buffer
= NULL
;
1480 respond
->value
= data
;
1481 init_completion(&respond
->wait_completion
);
1482 ftdi
->command_next
+= 1;
1483 ftdi
->respond_next
+= 1;
1484 ftdi_elan_kick_command_queue(ftdi
);
1485 up(&ftdi
->u132_lock
);
1486 wait_for_completion(&respond
->wait_completion
);
1489 up(&ftdi
->u132_lock
);
1496 static int ftdi_elan_read_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1497 u8 width
, u32
*data
)
1499 u8 addressofs
= mem_offset
/ 4;
1500 wait
:if (ftdi
->disconnected
> 0) {
1505 down(&ftdi
->u132_lock
);
1506 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1507 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1508 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1510 struct u132_command
*command
= &ftdi
->command
[
1511 COMMAND_MASK
& ftdi
->command_next
];
1512 struct u132_respond
*respond
= &ftdi
->respond
[
1513 RESPOND_MASK
& ftdi
->respond_next
];
1514 int result
= -ENODEV
;
1515 respond
->result
= &result
;
1516 respond
->header
= command
->header
= 0x00 | (cPCImemrd
&
1518 command
->length
= 0x04;
1519 respond
->address
= command
->address
= addressofs
;
1520 command
->width
= 0x00 | (width
& 0x0F);
1521 command
->follows
= 0;
1523 command
->buffer
= NULL
;
1524 respond
->value
= data
;
1525 init_completion(&respond
->wait_completion
);
1526 ftdi
->command_next
+= 1;
1527 ftdi
->respond_next
+= 1;
1528 ftdi_elan_kick_command_queue(ftdi
);
1529 up(&ftdi
->u132_lock
);
1530 wait_for_completion(&respond
->wait_completion
);
1533 up(&ftdi
->u132_lock
);
1540 int usb_ftdi_elan_read_pcimem(struct platform_device
*pdev
, int mem_offset
,
1541 u8 width
, u32
*data
)
1543 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1544 if (ftdi
->initialized
== 0) {
1547 return ftdi_elan_read_pcimem(ftdi
, mem_offset
, width
, data
);
1551 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem
);
1552 static int ftdi_elan_edset_setup(struct usb_ftdi
*ftdi
, u8 ed_number
,
1553 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1554 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1555 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1556 int halted
, int skipped
, int actual
, int non_null
))
1558 u8 ed
= ed_number
- 1;
1559 wait
:if (ftdi
->disconnected
> 0) {
1561 } else if (ftdi
->initialized
== 0) {
1565 down(&ftdi
->u132_lock
);
1566 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1567 if (command_size
< COMMAND_SIZE
) {
1568 struct u132_target
*target
= &ftdi
->target
[ed
];
1569 struct u132_command
*command
= &ftdi
->command
[
1570 COMMAND_MASK
& ftdi
->command_next
];
1571 command
->header
= 0x80 | (ed
<< 5);
1572 command
->length
= 0x8007;
1573 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1575 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1576 usb_pipeout(urb
->pipe
));
1577 command
->follows
= 8;
1579 command
->buffer
= urb
->setup_packet
;
1580 target
->callback
= callback
;
1581 target
->endp
= endp
;
1584 ftdi
->command_next
+= 1;
1585 ftdi_elan_kick_command_queue(ftdi
);
1586 up(&ftdi
->u132_lock
);
1589 up(&ftdi
->u132_lock
);
1596 int usb_ftdi_elan_edset_setup(struct platform_device
*pdev
, u8 ed_number
,
1597 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1598 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1599 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1600 int halted
, int skipped
, int actual
, int non_null
))
1602 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1603 return ftdi_elan_edset_setup(ftdi
, ed_number
, endp
, urb
, address
,
1604 ep_number
, toggle_bits
, callback
);
1608 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup
);
1609 static int ftdi_elan_edset_input(struct usb_ftdi
*ftdi
, u8 ed_number
,
1610 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1611 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1612 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1613 int halted
, int skipped
, int actual
, int non_null
))
1615 u8 ed
= ed_number
- 1;
1616 wait
:if (ftdi
->disconnected
> 0) {
1618 } else if (ftdi
->initialized
== 0) {
1622 down(&ftdi
->u132_lock
);
1623 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1624 if (command_size
< COMMAND_SIZE
) {
1625 struct u132_target
*target
= &ftdi
->target
[ed
];
1626 struct u132_command
*command
= &ftdi
->command
[
1627 COMMAND_MASK
& ftdi
->command_next
];
1628 int remaining_length
= urb
->transfer_buffer_length
-
1630 command
->header
= 0x82 | (ed
<< 5);
1631 if (remaining_length
== 0) {
1632 command
->length
= 0x0000;
1633 } else if (remaining_length
> 1024) {
1634 command
->length
= 0x8000 | 1023;
1636 command
->length
= 0x8000 | (remaining_length
-
1638 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1640 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1641 usb_pipeout(urb
->pipe
));
1642 command
->follows
= 0;
1644 command
->buffer
= NULL
;
1645 target
->callback
= callback
;
1646 target
->endp
= endp
;
1649 ftdi
->command_next
+= 1;
1650 ftdi_elan_kick_command_queue(ftdi
);
1651 up(&ftdi
->u132_lock
);
1654 up(&ftdi
->u132_lock
);
1661 int usb_ftdi_elan_edset_input(struct platform_device
*pdev
, u8 ed_number
,
1662 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1663 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1664 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1665 int halted
, int skipped
, int actual
, int non_null
))
1667 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1668 return ftdi_elan_edset_input(ftdi
, ed_number
, endp
, urb
, address
,
1669 ep_number
, toggle_bits
, callback
);
1673 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input
);
1674 static int ftdi_elan_edset_empty(struct usb_ftdi
*ftdi
, u8 ed_number
,
1675 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1676 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1677 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1678 int halted
, int skipped
, int actual
, int non_null
))
1680 u8 ed
= ed_number
- 1;
1681 wait
:if (ftdi
->disconnected
> 0) {
1683 } else if (ftdi
->initialized
== 0) {
1687 down(&ftdi
->u132_lock
);
1688 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1689 if (command_size
< COMMAND_SIZE
) {
1690 struct u132_target
*target
= &ftdi
->target
[ed
];
1691 struct u132_command
*command
= &ftdi
->command
[
1692 COMMAND_MASK
& ftdi
->command_next
];
1693 command
->header
= 0x81 | (ed
<< 5);
1694 command
->length
= 0x0000;
1695 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1697 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1698 usb_pipeout(urb
->pipe
));
1699 command
->follows
= 0;
1701 command
->buffer
= NULL
;
1702 target
->callback
= callback
;
1703 target
->endp
= endp
;
1706 ftdi
->command_next
+= 1;
1707 ftdi_elan_kick_command_queue(ftdi
);
1708 up(&ftdi
->u132_lock
);
1711 up(&ftdi
->u132_lock
);
1718 int usb_ftdi_elan_edset_empty(struct platform_device
*pdev
, u8 ed_number
,
1719 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1720 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1721 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1722 int halted
, int skipped
, int actual
, int non_null
))
1724 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1725 return ftdi_elan_edset_empty(ftdi
, ed_number
, endp
, urb
, address
,
1726 ep_number
, toggle_bits
, callback
);
1730 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty
);
1731 static int ftdi_elan_edset_output(struct usb_ftdi
*ftdi
, u8 ed_number
,
1732 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1733 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1734 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1735 int halted
, int skipped
, int actual
, int non_null
))
1737 u8 ed
= ed_number
- 1;
1738 wait
:if (ftdi
->disconnected
> 0) {
1740 } else if (ftdi
->initialized
== 0) {
1744 down(&ftdi
->u132_lock
);
1745 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1746 if (command_size
< COMMAND_SIZE
) {
1750 char data
[30 *3 + 4];
1752 int m
= (sizeof(data
) - 1) / 3;
1754 struct u132_target
*target
= &ftdi
->target
[ed
];
1755 struct u132_command
*command
= &ftdi
->command
[
1756 COMMAND_MASK
& ftdi
->command_next
];
1757 command
->header
= 0x81 | (ed
<< 5);
1758 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1760 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1761 usb_pipeout(urb
->pipe
));
1762 command
->follows
= min(1024,
1763 urb
->transfer_buffer_length
-
1764 urb
->actual_length
);
1766 command
->buffer
= urb
->transfer_buffer
+
1768 command
->length
= 0x8000 | (command
->follows
- 1);
1769 b
= command
->buffer
;
1770 urb_size
= command
->follows
;
1772 while (urb_size
-- > 0) {
1774 } else if (i
++ < m
) {
1775 int w
= sprintf(d
, " %02X", *b
++);
1779 d
+= sprintf(d
, " ..");
1781 target
->callback
= callback
;
1782 target
->endp
= endp
;
1785 ftdi
->command_next
+= 1;
1786 ftdi_elan_kick_command_queue(ftdi
);
1787 up(&ftdi
->u132_lock
);
1790 up(&ftdi
->u132_lock
);
1797 int usb_ftdi_elan_edset_output(struct platform_device
*pdev
, u8 ed_number
,
1798 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1799 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1800 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1801 int halted
, int skipped
, int actual
, int non_null
))
1803 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1804 return ftdi_elan_edset_output(ftdi
, ed_number
, endp
, urb
, address
,
1805 ep_number
, toggle_bits
, callback
);
1809 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output
);
1810 static int ftdi_elan_edset_single(struct usb_ftdi
*ftdi
, u8 ed_number
,
1811 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1812 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1813 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1814 int halted
, int skipped
, int actual
, int non_null
))
1816 u8 ed
= ed_number
- 1;
1817 wait
:if (ftdi
->disconnected
> 0) {
1819 } else if (ftdi
->initialized
== 0) {
1823 down(&ftdi
->u132_lock
);
1824 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1825 if (command_size
< COMMAND_SIZE
) {
1826 int remaining_length
= urb
->transfer_buffer_length
-
1828 struct u132_target
*target
= &ftdi
->target
[ed
];
1829 struct u132_command
*command
= &ftdi
->command
[
1830 COMMAND_MASK
& ftdi
->command_next
];
1831 command
->header
= 0x83 | (ed
<< 5);
1832 if (remaining_length
== 0) {
1833 command
->length
= 0x0000;
1834 } else if (remaining_length
> 1024) {
1835 command
->length
= 0x8000 | 1023;
1837 command
->length
= 0x8000 | (remaining_length
-
1839 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1841 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1842 usb_pipeout(urb
->pipe
));
1843 command
->follows
= 0;
1845 command
->buffer
= NULL
;
1846 target
->callback
= callback
;
1847 target
->endp
= endp
;
1850 ftdi
->command_next
+= 1;
1851 ftdi_elan_kick_command_queue(ftdi
);
1852 up(&ftdi
->u132_lock
);
1855 up(&ftdi
->u132_lock
);
1862 int usb_ftdi_elan_edset_single(struct platform_device
*pdev
, u8 ed_number
,
1863 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1864 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1865 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1866 int halted
, int skipped
, int actual
, int non_null
))
1868 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1869 return ftdi_elan_edset_single(ftdi
, ed_number
, endp
, urb
, address
,
1870 ep_number
, toggle_bits
, callback
);
1874 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single
);
1875 static int ftdi_elan_edset_flush(struct usb_ftdi
*ftdi
, u8 ed_number
,
1878 u8 ed
= ed_number
- 1;
1879 if (ftdi
->disconnected
> 0) {
1881 } else if (ftdi
->initialized
== 0) {
1884 struct u132_target
*target
= &ftdi
->target
[ed
];
1885 down(&ftdi
->u132_lock
);
1886 if (target
->abandoning
> 0) {
1887 up(&ftdi
->u132_lock
);
1890 target
->abandoning
= 1;
1891 wait_1
:if (target
->active
== 1) {
1892 int command_size
= ftdi
->command_next
-
1894 if (command_size
< COMMAND_SIZE
) {
1895 struct u132_command
*command
=
1896 &ftdi
->command
[COMMAND_MASK
&
1897 ftdi
->command_next
];
1898 command
->header
= 0x80 | (ed
<< 5) |
1900 command
->length
= 0x00;
1901 command
->address
= 0x00;
1902 command
->width
= 0x00;
1903 command
->follows
= 0;
1905 command
->buffer
= &command
->value
;
1906 ftdi
->command_next
+= 1;
1907 ftdi_elan_kick_command_queue(ftdi
);
1909 up(&ftdi
->u132_lock
);
1911 down(&ftdi
->u132_lock
);
1915 up(&ftdi
->u132_lock
);
1921 int usb_ftdi_elan_edset_flush(struct platform_device
*pdev
, u8 ed_number
,
1924 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1925 return ftdi_elan_edset_flush(ftdi
, ed_number
, endp
);
1929 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush
);
1930 static int ftdi_elan_flush_input_fifo(struct usb_ftdi
*ftdi
)
1932 int retry_on_empty
= 10;
1933 int retry_on_timeout
= 5;
1934 int retry_on_status
= 20;
1936 int packet_bytes
= 0;
1937 int retval
= usb_bulk_msg(ftdi
->udev
,
1938 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
1939 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
1940 &packet_bytes
, msecs_to_jiffies(100));
1941 if (packet_bytes
> 2) {
1942 char diag
[30 *3 + 4];
1944 int m
= (sizeof(diag
) - 1) / 3;
1945 char *b
= ftdi
->bulk_in_buffer
;
1948 while (packet_bytes
-- > 0) {
1950 if (bytes_read
< m
) {
1951 d
+= sprintf(d
, " %02X",
1953 } else if (bytes_read
> m
) {
1955 d
+= sprintf(d
, " ..");
1960 } else if (packet_bytes
> 1) {
1961 char s1
= ftdi
->bulk_in_buffer
[0];
1962 char s2
= ftdi
->bulk_in_buffer
[1];
1963 if (s1
== 0x31 && s2
== 0x60) {
1965 } else if (retry_on_status
-- > 0) {
1968 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry l"
1972 } else if (packet_bytes
> 0) {
1973 char b1
= ftdi
->bulk_in_buffer
[0];
1974 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from F"
1975 "TDI = %02X\n", b1
);
1976 if (retry_on_status
-- > 0) {
1979 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry l"
1983 } else if (retval
== -ETIMEDOUT
) {
1984 if (retry_on_timeout
-- > 0) {
1987 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limi"
1991 } else if (retval
== 0) {
1992 if (retry_on_empty
-- > 0) {
1995 dev_err(&ftdi
->udev
->dev
, "empty packet retry l"
2000 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
2009 * send the long flush sequence
2012 static int ftdi_elan_synchronize_flush(struct usb_ftdi
*ftdi
)
2019 urb
= usb_alloc_urb(0, GFP_KERNEL
);
2021 dev_err(&ftdi
->udev
->dev
, "could not alloc a urb for flush sequ"
2025 buf
= usb_buffer_alloc(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
2027 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for flush seq"
2034 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
2035 ftdi
->bulk_out_endpointAddr
), buf
, i
,
2036 ftdi_elan_write_bulk_callback
, ftdi
);
2037 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
2038 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
2040 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the "
2041 "flush sequence\n");
2042 usb_buffer_free(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
2052 * send the reset sequence
2055 static int ftdi_elan_synchronize_reset(struct usb_ftdi
*ftdi
)
2062 urb
= usb_alloc_urb(0, GFP_KERNEL
);
2064 dev_err(&ftdi
->udev
->dev
, "could not get a urb for the reset se"
2068 buf
= usb_buffer_alloc(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
2070 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for the reset"
2079 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
2080 ftdi
->bulk_out_endpointAddr
), buf
, i
,
2081 ftdi_elan_write_bulk_callback
, ftdi
);
2082 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
2083 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
2085 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the "
2086 "reset sequence\n");
2087 usb_buffer_free(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
2095 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
)
2099 int retry_on_timeout
= 5;
2100 int retry_on_empty
= 10;
2102 retval
= ftdi_elan_flush_input_fifo(ftdi
);
2105 ftdi
->bulk_in_left
= 0;
2106 ftdi
->bulk_in_last
= -1;
2107 while (long_stop
-- > 0) {
2110 retval
= ftdi_elan_synchronize_flush(ftdi
);
2113 retval
= ftdi_elan_flush_input_fifo(ftdi
);
2116 reset
:retval
= ftdi_elan_synchronize_reset(ftdi
);
2122 int packet_bytes
= 0;
2123 retval
= usb_bulk_msg(ftdi
->udev
,
2124 usb_rcvbulkpipe(ftdi
->udev
,
2125 ftdi
->bulk_in_endpointAddr
),
2126 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
2127 &packet_bytes
, msecs_to_jiffies(500));
2128 if (packet_bytes
> 2) {
2129 char diag
[30 *3 + 4];
2131 int m
= (sizeof(diag
) - 1) / 3;
2132 char *b
= ftdi
->bulk_in_buffer
;
2134 unsigned char c
= 0;
2136 while (packet_bytes
-- > 0) {
2138 if (bytes_read
< m
) {
2139 d
+= sprintf(d
, " %02X", c
);
2140 } else if (bytes_read
> m
) {
2142 d
+= sprintf(d
, " ..");
2151 } else if (read_stop
-- > 0) {
2154 dev_err(&ftdi
->udev
->dev
, "retr"
2155 "y limit reached\n");
2159 } else if (packet_bytes
> 1) {
2160 unsigned char s1
= ftdi
->bulk_in_buffer
[0];
2161 unsigned char s2
= ftdi
->bulk_in_buffer
[1];
2162 if (s1
== 0x31 && s2
== 0x00) {
2163 if (read_stuck
-- > 0) {
2167 } else if (s1
== 0x31 && s2
== 0x60) {
2168 if (read_stop
-- > 0) {
2171 dev_err(&ftdi
->udev
->dev
, "retr"
2172 "y limit reached\n");
2176 if (read_stop
-- > 0) {
2179 dev_err(&ftdi
->udev
->dev
, "retr"
2180 "y limit reached\n");
2184 } else if (packet_bytes
> 0) {
2185 if (read_stop
-- > 0) {
2188 dev_err(&ftdi
->udev
->dev
, "retry limit "
2192 } else if (retval
== -ETIMEDOUT
) {
2193 if (retry_on_timeout
-- > 0) {
2196 dev_err(&ftdi
->udev
->dev
, "TIMED OUT re"
2197 "try limit reached\n");
2200 } else if (retval
== 0) {
2201 if (retry_on_empty
-- > 0) {
2204 dev_err(&ftdi
->udev
->dev
, "empty packet"
2205 " retry limit reached\n");
2210 dev_err(&ftdi
->udev
->dev
, "error = %d\n",
2212 if (read_stop
-- > 0) {
2215 dev_err(&ftdi
->udev
->dev
, "retry limit "
2222 dev_err(&ftdi
->udev
->dev
, "failed to synchronize\n");
2226 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
)
2228 int retry_on_empty
= 10;
2229 int retry_on_timeout
= 5;
2230 int retry_on_status
= 50;
2232 int packet_bytes
= 0;
2233 int retval
= usb_bulk_msg(ftdi
->udev
,
2234 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
2235 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
2236 &packet_bytes
, msecs_to_jiffies(1000));
2237 if (packet_bytes
> 2) {
2238 char diag
[30 *3 + 4];
2240 int m
= (sizeof(diag
) - 1) / 3;
2241 char *b
= ftdi
->bulk_in_buffer
;
2244 while (packet_bytes
-- > 0) {
2246 if (bytes_read
< m
) {
2247 d
+= sprintf(d
, " %02X",
2249 } else if (bytes_read
> m
) {
2251 d
+= sprintf(d
, " ..");
2256 } else if (packet_bytes
> 1) {
2257 char s1
= ftdi
->bulk_in_buffer
[0];
2258 char s2
= ftdi
->bulk_in_buffer
[1];
2259 if (s1
== 0x31 && s2
== 0x60) {
2261 } else if (retry_on_status
-- > 0) {
2266 } else if (packet_bytes
> 0) {
2267 char b1
= ftdi
->bulk_in_buffer
[0];
2268 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from F"
2269 "TDI = %02X\n", b1
);
2270 if (retry_on_status
-- > 0) {
2274 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry l"
2278 } else if (retval
== -ETIMEDOUT
) {
2279 if (retry_on_timeout
-- > 0) {
2282 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limi"
2286 } else if (retval
== 0) {
2287 if (retry_on_empty
-- > 0) {
2290 dev_err(&ftdi
->udev
->dev
, "empty packet retry l"
2295 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
2302 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
)
2304 int UxxxStatus
= ftdi_elan_read_reg(ftdi
, &ftdi
->controlreg
);
2307 if (ftdi
->controlreg
& 0x00400000) {
2308 if (ftdi
->card_ejected
) {
2310 ftdi
->card_ejected
= 1;
2311 dev_err(&ftdi
->udev
->dev
, "CARD EJECTED - controlreg = "
2312 "%08X\n", ftdi
->controlreg
);
2316 u8 fn
= ftdi
->function
- 1;
2317 int activePCIfn
= fn
<< 8;
2322 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2326 pciVID
= pcidata
& 0xFFFF;
2327 pciPID
= (pcidata
>> 16) & 0xFFFF;
2328 if (pciVID
== ftdi
->platform_data
.vendor
&& pciPID
==
2329 ftdi
->platform_data
.device
) {
2332 dev_err(&ftdi
->udev
->dev
, "vendor=%04X pciVID=%04X devi"
2333 "ce=%04X pciPID=%04X\n",
2334 ftdi
->platform_data
.vendor
, pciVID
,
2335 ftdi
->platform_data
.device
, pciPID
);
2341 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
)
2350 int activePCIfn
= 0;
2353 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2356 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000000L
);
2360 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x100);
2363 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x500);
2366 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2369 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020CL
| 0x000);
2372 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020DL
| 0x000);
2376 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020FL
| 0x000);
2379 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2382 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x800);
2385 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2388 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2392 for (fn
= 0; (fn
< 4) && (!foundOHCI
); fn
++) {
2393 activePCIfn
= fn
<< 8;
2394 ftdi
->function
= fn
+ 1;
2395 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2399 pciVID
= pcidata
& 0xFFFF;
2400 pciPID
= (pcidata
>> 16) & 0xFFFF;
2401 if ((pciVID
== 0x1045) && (pciPID
== 0xc861)) {
2403 } else if ((pciVID
== 0x1033) && (pciPID
== 0x0035)) {
2405 } else if ((pciVID
== 0x10b9) && (pciPID
== 0x5237)) {
2407 } else if ((pciVID
== 0x11c1) && (pciPID
== 0x5802)) {
2409 } else if ((pciVID
== 0x11AB) && (pciPID
== 0x1FA6)) {
2412 if (foundOHCI
== 0) {
2415 ftdi
->platform_data
.vendor
= pciVID
;
2416 ftdi
->platform_data
.device
= pciPID
;
2417 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x2800);
2421 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2425 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2429 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2433 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2438 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2442 latence_timer
&= 0xFFFF00FF;
2443 latence_timer
|= 0x00001600;
2444 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2448 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2453 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2457 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2464 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
)
2469 int reset_repeat
= 0;
2471 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x0e, 0x01);
2475 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2480 if (reset_repeat
++ > 100) {
2490 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x11000000);
2493 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2497 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x2edf);
2500 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2504 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x2edf2edf);
2507 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2511 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0xA0);
2514 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2519 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x0e, 0x04);
2522 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2526 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2530 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2534 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x00001200);
2537 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2541 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2545 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2549 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x28002edf);
2552 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2557 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x10000);
2561 power_check
:U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2564 if (!(pcidata
& 1)) {
2570 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2574 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2578 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x02);
2581 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2585 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x10);
2588 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2594 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x02);
2599 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2604 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2608 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2611 dump_regs
:for (reg
= 0; reg
<= 0x54; reg
+= 4) {
2612 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2621 * we use only the first bulk-in and bulk-out endpoints
2623 static int ftdi_elan_probe(struct usb_interface
*interface
,
2624 const struct usb_device_id
*id
)
2626 struct usb_host_interface
*iface_desc
;
2627 struct usb_endpoint_descriptor
*endpoint
;
2630 int retval
= -ENOMEM
;
2631 struct usb_ftdi
*ftdi
= kmalloc(sizeof(struct usb_ftdi
), GFP_KERNEL
);
2633 printk(KERN_ERR
"Out of memory\n");
2636 memset(ftdi
, 0x00, sizeof(struct usb_ftdi
));
2637 down(&ftdi_module_lock
);
2638 list_add_tail(&ftdi
->ftdi_list
, &ftdi_static_list
);
2639 ftdi
->sequence_num
= ++ftdi_instances
;
2640 up(&ftdi_module_lock
);
2641 ftdi_elan_init_kref(ftdi
);
2642 init_MUTEX(&ftdi
->sw_lock
);
2643 ftdi
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2644 ftdi
->interface
= interface
;
2645 init_MUTEX(&ftdi
->u132_lock
);
2647 iface_desc
= interface
->cur_altsetting
;
2648 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2649 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2650 if (!ftdi
->bulk_in_endpointAddr
&&
2651 ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
2652 == USB_DIR_IN
) && ((endpoint
->bmAttributes
&
2653 USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_BULK
))
2655 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
2656 ftdi
->bulk_in_size
= buffer_size
;
2657 ftdi
->bulk_in_endpointAddr
= endpoint
->bEndpointAddress
;
2658 ftdi
->bulk_in_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
2659 if (!ftdi
->bulk_in_buffer
) {
2660 dev_err(&ftdi
->udev
->dev
, "Could not allocate b"
2666 if (!ftdi
->bulk_out_endpointAddr
&&
2667 ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
2668 == USB_DIR_OUT
) && ((endpoint
->bmAttributes
&
2669 USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_BULK
))
2671 ftdi
->bulk_out_endpointAddr
=
2672 endpoint
->bEndpointAddress
;
2675 if (!(ftdi
->bulk_in_endpointAddr
&& ftdi
->bulk_out_endpointAddr
)) {
2676 dev_err(&ftdi
->udev
->dev
, "Could not find both bulk-in and bulk"
2677 "-out endpoints\n");
2681 dev_info(&ftdi
->udev
->dev
, "interface %d has I=%02X O=%02X\n",
2682 iface_desc
->desc
.bInterfaceNumber
, ftdi
->bulk_in_endpointAddr
,
2683 ftdi
->bulk_out_endpointAddr
);
2684 usb_set_intfdata(interface
, ftdi
);
2685 if (iface_desc
->desc
.bInterfaceNumber
== 0 &&
2686 ftdi
->bulk_in_endpointAddr
== 0x81 &&
2687 ftdi
->bulk_out_endpointAddr
== 0x02) {
2688 retval
= usb_register_dev(interface
, &ftdi_elan_jtag_class
);
2690 dev_err(&ftdi
->udev
->dev
, "Not able to get a minor for "
2692 usb_set_intfdata(interface
, NULL
);
2696 ftdi
->class = &ftdi_elan_jtag_class
;
2697 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p JTAG interface "
2698 "%d now attached to ftdi%d\n", ftdi
,
2699 iface_desc
->desc
.bInterfaceNumber
,
2703 } else if (iface_desc
->desc
.bInterfaceNumber
== 1 &&
2704 ftdi
->bulk_in_endpointAddr
== 0x83 &&
2705 ftdi
->bulk_out_endpointAddr
== 0x04) {
2707 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p ELAN interface %d now a"
2708 "ctivated\n", ftdi
, iface_desc
->desc
.bInterfaceNumber
);
2709 INIT_WORK(&ftdi
->status_work
, ftdi_elan_status_work
,
2711 INIT_WORK(&ftdi
->command_work
, ftdi_elan_command_work
,
2713 INIT_WORK(&ftdi
->respond_work
, ftdi_elan_respond_work
,
2715 ftdi_status_queue_work(ftdi
, msecs_to_jiffies(3 *1000));
2718 dev_err(&ftdi
->udev
->dev
,
2719 "Could not find ELAN's U132 device\n");
2724 ftdi_elan_put_kref(ftdi
);
2729 static void ftdi_elan_disconnect(struct usb_interface
*interface
)
2731 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
2732 ftdi
->disconnected
+= 1;
2734 int minor
= interface
->minor
;
2735 struct usb_class_driver
*class = ftdi
->class;
2736 usb_set_intfdata(interface
, NULL
);
2737 usb_deregister_dev(interface
, class);
2738 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 jtag interface on min"
2739 "or %d now disconnected\n", minor
);
2741 ftdi_status_cancel_work(ftdi
);
2742 ftdi_command_cancel_work(ftdi
);
2743 ftdi_response_cancel_work(ftdi
);
2744 ftdi_elan_abandon_completions(ftdi
);
2745 ftdi_elan_abandon_targets(ftdi
);
2746 if (ftdi
->registered
) {
2747 platform_device_unregister(&ftdi
->platform_dev
);
2748 ftdi
->synchronized
= 0;
2749 ftdi
->enumerated
= 0;
2750 ftdi
->registered
= 0;
2752 flush_workqueue(status_queue
);
2753 flush_workqueue(command_queue
);
2754 flush_workqueue(respond_queue
);
2755 ftdi
->disconnected
+= 1;
2756 usb_set_intfdata(interface
, NULL
);
2757 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 host controller inter"
2758 "face now disconnected\n");
2760 ftdi_elan_put_kref(ftdi
);
2763 static struct usb_driver ftdi_elan_driver
= {
2764 .name
= "ftdi-elan",
2765 .probe
= ftdi_elan_probe
,
2766 .disconnect
= ftdi_elan_disconnect
,
2767 .id_table
= ftdi_elan_table
,
2769 static int __init
ftdi_elan_init(void)
2772 printk(KERN_INFO
"driver %s built at %s on %s\n", ftdi_elan_driver
.name
,
2773 __TIME__
, __DATE__
);
2774 init_MUTEX(&ftdi_module_lock
);
2775 INIT_LIST_HEAD(&ftdi_static_list
);
2776 status_queue
= create_singlethread_workqueue("ftdi-status-control");
2777 command_queue
= create_singlethread_workqueue("ftdi-command-engine");
2778 respond_queue
= create_singlethread_workqueue("ftdi-respond-engine");
2779 result
= usb_register(&ftdi_elan_driver
);
2781 printk(KERN_ERR
"usb_register failed. Error number %d\n",
2786 static void __exit
ftdi_elan_exit(void)
2788 struct usb_ftdi
*ftdi
;
2789 struct usb_ftdi
*temp
;
2790 usb_deregister(&ftdi_elan_driver
);
2791 printk(KERN_INFO
"ftdi_u132 driver deregistered\n");
2792 list_for_each_entry_safe(ftdi
, temp
, &ftdi_static_list
, ftdi_list
) {
2793 ftdi_status_cancel_work(ftdi
);
2794 ftdi_command_cancel_work(ftdi
);
2795 ftdi_response_cancel_work(ftdi
);
2796 } flush_workqueue(status_queue
);
2797 destroy_workqueue(status_queue
);
2798 status_queue
= NULL
;
2799 flush_workqueue(command_queue
);
2800 destroy_workqueue(command_queue
);
2801 command_queue
= NULL
;
2802 flush_workqueue(respond_queue
);
2803 destroy_workqueue(respond_queue
);
2804 respond_queue
= NULL
;
2808 module_init(ftdi_elan_init
);
2809 module_exit(ftdi_elan_exit
);