1 // SPDX-License-Identifier: GPL-2.0
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
9 * tony.olech@elandigitalsystems.com
11 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
12 * based on various USB client drivers in the 2.6.15 linux kernel
13 * with constant reference to the 3rd Edition of Linux Device Drivers
14 * published by O'Reilly
16 * The U132 adapter is a USB to CardBus adapter specifically designed
17 * for PC cards that contain an OHCI host controller. Typical PC cards
18 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
20 * The U132 adapter will *NOT *work with PC cards that do not contain
21 * an OHCI controller. A simple way to test whether a PC card has an
22 * OHCI controller as an interface is to insert the PC card directly
23 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
24 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
25 * then there is a good chance that the U132 adapter will support the
26 * PC card.(you also need the specific client driver for the PC card)
28 * Please inform the Author and Maintainer about any PC cards that
29 * contain OHCI Host Controller and work when directly connected to
30 * an embedded CardBus slot but do not work when they are connected
31 * via an ELAN U132 adapter.
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/ioctl.h>
42 #include <linux/pci_ids.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/workqueue.h>
50 #include <linux/platform_device.h>
51 MODULE_AUTHOR("Tony Olech");
52 MODULE_DESCRIPTION("FTDI ELAN driver");
53 MODULE_LICENSE("GPL");
54 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55 static bool distrust_firmware
= 1;
56 module_param(distrust_firmware
, bool, 0);
57 MODULE_PARM_DESC(distrust_firmware
,
58 "true to distrust firmware power/overcurrent setup");
59 extern struct platform_driver u132_platform_driver
;
61 * ftdi_module_lock exists to protect access to global variables
64 static struct mutex ftdi_module_lock
;
65 static int ftdi_instances
= 0;
66 static struct list_head ftdi_static_list
;
68 * end of the global variables protected by ftdi_module_lock
72 #include <linux/usb/hcd.h>
74 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
75 * If you're going to try stuff like this, you need to split
76 * out shareable stuff (register declarations?) into its own
77 * file, maybe name <linux/usb/ohci.h>
80 #include "../host/ohci.h"
81 /* Define these values to match your devices*/
82 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
83 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
84 /* table of devices that work with this driver*/
85 static const struct usb_device_id ftdi_elan_table
[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID
, USB_FTDI_ELAN_PRODUCT_ID
)},
87 { /* Terminating entry */ }
90 MODULE_DEVICE_TABLE(usb
, ftdi_elan_table
);
91 /* only the jtag(firmware upgrade device) interface requires
92 * a device file and corresponding minor number, but the
93 * interface is created unconditionally - I suppose it could
94 * be configured or not according to a module parameter.
95 * But since we(now) require one interface per device,
96 * and since it unlikely that a normal installation would
97 * require more than a couple of elan-ftdi devices, 8 seems
98 * like a reasonable limit to have here, and if someone
99 * really requires more than 8 devices, then they can frig the
102 #define USB_FTDI_ELAN_MINOR_BASE 192
103 #define COMMAND_BITS 5
104 #define COMMAND_SIZE (1<<COMMAND_BITS)
105 #define COMMAND_MASK (COMMAND_SIZE-1)
106 struct u132_command
{
115 #define RESPOND_BITS 5
116 #define RESPOND_SIZE (1<<RESPOND_BITS)
117 #define RESPOND_MASK (RESPOND_SIZE-1)
118 struct u132_respond
{
123 struct completion wait_completion
;
138 void (*callback
)(void *endp
, struct urb
*urb
, u8
*buf
, int len
,
139 int toggle_bits
, int error_count
, int condition_code
,
140 int repeat_number
, int halted
, int skipped
, int actual
,
143 /* Structure to hold all of our device specific stuff*/
145 struct list_head ftdi_list
;
146 struct mutex u132_lock
;
149 struct u132_command command
[COMMAND_SIZE
];
152 struct u132_respond respond
[RESPOND_SIZE
];
153 struct u132_target target
[4];
154 char device_name
[16];
155 unsigned synchronized
:1;
156 unsigned enumerated
:1;
157 unsigned registered
:1;
158 unsigned initialized
:1;
159 unsigned card_ejected
:1;
165 int status_queue_delay
;
166 struct semaphore sw_lock
;
167 struct usb_device
*udev
;
168 struct usb_interface
*interface
;
169 struct usb_class_driver
*class;
170 struct delayed_work status_work
;
171 struct delayed_work command_work
;
172 struct delayed_work respond_work
;
173 struct u132_platform_data platform_data
;
174 struct resource resources
[0];
175 struct platform_device platform_dev
;
176 unsigned char *bulk_in_buffer
;
180 __u8 bulk_in_endpointAddr
;
181 __u8 bulk_out_endpointAddr
;
184 u8 response
[4 + 1024];
189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
192 static struct usb_driver ftdi_elan_driver
;
193 static void ftdi_elan_delete(struct kref
*kref
)
195 struct usb_ftdi
*ftdi
= kref_to_usb_ftdi(kref
);
196 dev_warn(&ftdi
->udev
->dev
, "FREEING ftdi=%p\n", ftdi
);
197 usb_put_dev(ftdi
->udev
);
198 ftdi
->disconnected
+= 1;
199 mutex_lock(&ftdi_module_lock
);
200 list_del_init(&ftdi
->ftdi_list
);
202 mutex_unlock(&ftdi_module_lock
);
203 kfree(ftdi
->bulk_in_buffer
);
204 ftdi
->bulk_in_buffer
= NULL
;
207 static void ftdi_elan_put_kref(struct usb_ftdi
*ftdi
)
209 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
212 static void ftdi_elan_get_kref(struct usb_ftdi
*ftdi
)
214 kref_get(&ftdi
->kref
);
217 static void ftdi_elan_init_kref(struct usb_ftdi
*ftdi
)
219 kref_init(&ftdi
->kref
);
222 static void ftdi_status_requeue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
224 if (!schedule_delayed_work(&ftdi
->status_work
, delta
))
225 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
228 static void ftdi_status_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
230 if (schedule_delayed_work(&ftdi
->status_work
, delta
))
231 kref_get(&ftdi
->kref
);
234 static void ftdi_status_cancel_work(struct usb_ftdi
*ftdi
)
236 if (cancel_delayed_work_sync(&ftdi
->status_work
))
237 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
240 static void ftdi_command_requeue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
242 if (!schedule_delayed_work(&ftdi
->command_work
, delta
))
243 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
246 static void ftdi_command_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
248 if (schedule_delayed_work(&ftdi
->command_work
, delta
))
249 kref_get(&ftdi
->kref
);
252 static void ftdi_command_cancel_work(struct usb_ftdi
*ftdi
)
254 if (cancel_delayed_work_sync(&ftdi
->command_work
))
255 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
258 static void ftdi_response_requeue_work(struct usb_ftdi
*ftdi
,
261 if (!schedule_delayed_work(&ftdi
->respond_work
, delta
))
262 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
265 static void ftdi_respond_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
267 if (schedule_delayed_work(&ftdi
->respond_work
, delta
))
268 kref_get(&ftdi
->kref
);
271 static void ftdi_response_cancel_work(struct usb_ftdi
*ftdi
)
273 if (cancel_delayed_work_sync(&ftdi
->respond_work
))
274 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
277 void ftdi_elan_gone_away(struct platform_device
*pdev
)
279 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
280 ftdi
->gone_away
+= 1;
281 ftdi_elan_put_kref(ftdi
);
285 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away
);
286 static void ftdi_release_platform_dev(struct device
*dev
)
291 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
292 struct u132_target
*target
, u8
*buffer
, int length
);
293 static void ftdi_elan_kick_command_queue(struct usb_ftdi
*ftdi
);
294 static void ftdi_elan_kick_respond_queue(struct usb_ftdi
*ftdi
);
295 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
);
296 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
);
297 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
);
298 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
);
299 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
);
300 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
);
301 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
);
302 static int ftdi_elan_hcd_init(struct usb_ftdi
*ftdi
)
304 if (ftdi
->platform_dev
.dev
.parent
)
307 ftdi_elan_get_kref(ftdi
);
308 ftdi
->platform_data
.potpg
= 100;
309 ftdi
->platform_data
.reset
= NULL
;
310 ftdi
->platform_dev
.id
= ftdi
->sequence_num
;
311 ftdi
->platform_dev
.resource
= ftdi
->resources
;
312 ftdi
->platform_dev
.num_resources
= ARRAY_SIZE(ftdi
->resources
);
313 ftdi
->platform_dev
.dev
.platform_data
= &ftdi
->platform_data
;
314 ftdi
->platform_dev
.dev
.parent
= NULL
;
315 ftdi
->platform_dev
.dev
.release
= ftdi_release_platform_dev
;
316 ftdi
->platform_dev
.dev
.dma_mask
= NULL
;
317 snprintf(ftdi
->device_name
, sizeof(ftdi
->device_name
), "u132_hcd");
318 ftdi
->platform_dev
.name
= ftdi
->device_name
;
319 dev_info(&ftdi
->udev
->dev
, "requesting module '%s'\n", "u132_hcd");
320 request_module("u132_hcd");
321 dev_info(&ftdi
->udev
->dev
, "registering '%s'\n",
322 ftdi
->platform_dev
.name
);
324 return platform_device_register(&ftdi
->platform_dev
);
327 static void ftdi_elan_abandon_completions(struct usb_ftdi
*ftdi
)
329 mutex_lock(&ftdi
->u132_lock
);
330 while (ftdi
->respond_next
> ftdi
->respond_head
) {
331 struct u132_respond
*respond
= &ftdi
->respond
[RESPOND_MASK
&
332 ftdi
->respond_head
++];
333 *respond
->result
= -ESHUTDOWN
;
335 complete(&respond
->wait_completion
);
337 mutex_unlock(&ftdi
->u132_lock
);
340 static void ftdi_elan_abandon_targets(struct usb_ftdi
*ftdi
)
343 mutex_lock(&ftdi
->u132_lock
);
344 while (ed_number
-- > 0) {
345 struct u132_target
*target
= &ftdi
->target
[ed_number
];
346 if (target
->active
== 1) {
347 target
->condition_code
= TD_DEVNOTRESP
;
348 mutex_unlock(&ftdi
->u132_lock
);
349 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
350 mutex_lock(&ftdi
->u132_lock
);
356 mutex_unlock(&ftdi
->u132_lock
);
359 static void ftdi_elan_flush_targets(struct usb_ftdi
*ftdi
)
362 mutex_lock(&ftdi
->u132_lock
);
363 while (ed_number
-- > 0) {
364 struct u132_target
*target
= &ftdi
->target
[ed_number
];
365 target
->abandoning
= 1;
366 wait_1
:if (target
->active
== 1) {
367 int command_size
= ftdi
->command_next
-
369 if (command_size
< COMMAND_SIZE
) {
370 struct u132_command
*command
= &ftdi
->command
[
371 COMMAND_MASK
& ftdi
->command_next
];
372 command
->header
= 0x80 | (ed_number
<< 5) | 0x4;
373 command
->length
= 0x00;
374 command
->address
= 0x00;
375 command
->width
= 0x00;
376 command
->follows
= 0;
378 command
->buffer
= &command
->value
;
379 ftdi
->command_next
+= 1;
380 ftdi_elan_kick_command_queue(ftdi
);
382 mutex_unlock(&ftdi
->u132_lock
);
384 mutex_lock(&ftdi
->u132_lock
);
388 wait_2
:if (target
->active
== 1) {
389 int command_size
= ftdi
->command_next
-
391 if (command_size
< COMMAND_SIZE
) {
392 struct u132_command
*command
= &ftdi
->command
[
393 COMMAND_MASK
& ftdi
->command_next
];
394 command
->header
= 0x90 | (ed_number
<< 5);
395 command
->length
= 0x00;
396 command
->address
= 0x00;
397 command
->width
= 0x00;
398 command
->follows
= 0;
400 command
->buffer
= &command
->value
;
401 ftdi
->command_next
+= 1;
402 ftdi_elan_kick_command_queue(ftdi
);
404 mutex_unlock(&ftdi
->u132_lock
);
406 mutex_lock(&ftdi
->u132_lock
);
414 mutex_unlock(&ftdi
->u132_lock
);
417 static void ftdi_elan_cancel_targets(struct usb_ftdi
*ftdi
)
420 mutex_lock(&ftdi
->u132_lock
);
421 while (ed_number
-- > 0) {
422 struct u132_target
*target
= &ftdi
->target
[ed_number
];
423 target
->abandoning
= 1;
424 wait
:if (target
->active
== 1) {
425 int command_size
= ftdi
->command_next
-
427 if (command_size
< COMMAND_SIZE
) {
428 struct u132_command
*command
= &ftdi
->command
[
429 COMMAND_MASK
& ftdi
->command_next
];
430 command
->header
= 0x80 | (ed_number
<< 5) | 0x4;
431 command
->length
= 0x00;
432 command
->address
= 0x00;
433 command
->width
= 0x00;
434 command
->follows
= 0;
436 command
->buffer
= &command
->value
;
437 ftdi
->command_next
+= 1;
438 ftdi_elan_kick_command_queue(ftdi
);
440 mutex_unlock(&ftdi
->u132_lock
);
442 mutex_lock(&ftdi
->u132_lock
);
450 mutex_unlock(&ftdi
->u132_lock
);
453 static void ftdi_elan_kick_command_queue(struct usb_ftdi
*ftdi
)
455 ftdi_command_queue_work(ftdi
, 0);
458 static void ftdi_elan_command_work(struct work_struct
*work
)
460 struct usb_ftdi
*ftdi
=
461 container_of(work
, struct usb_ftdi
, command_work
.work
);
463 if (ftdi
->disconnected
> 0) {
464 ftdi_elan_put_kref(ftdi
);
467 int retval
= ftdi_elan_command_engine(ftdi
);
468 if (retval
== -ESHUTDOWN
) {
469 ftdi
->disconnected
+= 1;
470 } else if (retval
== -ENODEV
) {
471 ftdi
->disconnected
+= 1;
473 dev_err(&ftdi
->udev
->dev
, "command error %d\n", retval
);
474 ftdi_command_requeue_work(ftdi
, msecs_to_jiffies(10));
479 static void ftdi_elan_kick_respond_queue(struct usb_ftdi
*ftdi
)
481 ftdi_respond_queue_work(ftdi
, 0);
484 static void ftdi_elan_respond_work(struct work_struct
*work
)
486 struct usb_ftdi
*ftdi
=
487 container_of(work
, struct usb_ftdi
, respond_work
.work
);
488 if (ftdi
->disconnected
> 0) {
489 ftdi_elan_put_kref(ftdi
);
492 int retval
= ftdi_elan_respond_engine(ftdi
);
494 } else if (retval
== -ESHUTDOWN
) {
495 ftdi
->disconnected
+= 1;
496 } else if (retval
== -ENODEV
) {
497 ftdi
->disconnected
+= 1;
498 } else if (retval
== -EILSEQ
) {
499 ftdi
->disconnected
+= 1;
501 ftdi
->disconnected
+= 1;
502 dev_err(&ftdi
->udev
->dev
, "respond error %d\n", retval
);
504 if (ftdi
->disconnected
> 0) {
505 ftdi_elan_abandon_completions(ftdi
);
506 ftdi_elan_abandon_targets(ftdi
);
508 ftdi_response_requeue_work(ftdi
, msecs_to_jiffies(10));
515 * the sw_lock is initially held and will be freed
516 * after the FTDI has been synchronized
519 static void ftdi_elan_status_work(struct work_struct
*work
)
521 struct usb_ftdi
*ftdi
=
522 container_of(work
, struct usb_ftdi
, status_work
.work
);
523 int work_delay_in_msec
= 0;
524 if (ftdi
->disconnected
> 0) {
525 ftdi_elan_put_kref(ftdi
);
527 } else if (ftdi
->synchronized
== 0) {
528 down(&ftdi
->sw_lock
);
529 if (ftdi_elan_synchronize(ftdi
) == 0) {
530 ftdi
->synchronized
= 1;
531 ftdi_command_queue_work(ftdi
, 1);
532 ftdi_respond_queue_work(ftdi
, 1);
534 work_delay_in_msec
= 100;
536 dev_err(&ftdi
->udev
->dev
, "synchronize failed\n");
538 work_delay_in_msec
= 10 *1000;
540 } else if (ftdi
->stuck_status
> 0) {
541 if (ftdi_elan_stuck_waiting(ftdi
) == 0) {
542 ftdi
->stuck_status
= 0;
543 ftdi
->synchronized
= 0;
544 } else if ((ftdi
->stuck_status
++ % 60) == 1) {
545 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted - please remove\n");
547 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted - checked %d times\n",
549 work_delay_in_msec
= 100;
550 } else if (ftdi
->enumerated
== 0) {
551 if (ftdi_elan_enumeratePCI(ftdi
) == 0) {
552 ftdi
->enumerated
= 1;
553 work_delay_in_msec
= 250;
555 work_delay_in_msec
= 1000;
556 } else if (ftdi
->initialized
== 0) {
557 if (ftdi_elan_setupOHCI(ftdi
) == 0) {
558 ftdi
->initialized
= 1;
559 work_delay_in_msec
= 500;
561 dev_err(&ftdi
->udev
->dev
, "initialized failed - trying again in 10 seconds\n");
562 work_delay_in_msec
= 1 *1000;
564 } else if (ftdi
->registered
== 0) {
565 work_delay_in_msec
= 10;
566 if (ftdi_elan_hcd_init(ftdi
) == 0) {
567 ftdi
->registered
= 1;
569 dev_err(&ftdi
->udev
->dev
, "register failed\n");
570 work_delay_in_msec
= 250;
572 if (ftdi_elan_checkingPCI(ftdi
) == 0) {
573 work_delay_in_msec
= 250;
574 } else if (ftdi
->controlreg
& 0x00400000) {
575 if (ftdi
->gone_away
> 0) {
576 dev_err(&ftdi
->udev
->dev
, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
577 ftdi
->platform_dev
.dev
.parent
,
578 &ftdi
->platform_dev
.dev
);
579 platform_device_unregister(&ftdi
->platform_dev
);
580 ftdi
->platform_dev
.dev
.parent
= NULL
;
581 ftdi
->registered
= 0;
582 ftdi
->enumerated
= 0;
583 ftdi
->card_ejected
= 0;
584 ftdi
->initialized
= 0;
587 ftdi_elan_flush_targets(ftdi
);
588 work_delay_in_msec
= 250;
590 dev_err(&ftdi
->udev
->dev
, "PCI device has disappeared\n");
591 ftdi_elan_cancel_targets(ftdi
);
592 work_delay_in_msec
= 500;
593 ftdi
->enumerated
= 0;
594 ftdi
->initialized
= 0;
597 if (ftdi
->disconnected
> 0) {
598 ftdi_elan_put_kref(ftdi
);
601 ftdi_status_requeue_work(ftdi
,
602 msecs_to_jiffies(work_delay_in_msec
));
609 * file_operations for the jtag interface
611 * the usage count for the device is incremented on open()
612 * and decremented on release()
614 static int ftdi_elan_open(struct inode
*inode
, struct file
*file
)
617 struct usb_interface
*interface
;
619 subminor
= iminor(inode
);
620 interface
= usb_find_interface(&ftdi_elan_driver
, subminor
);
623 pr_err("can't find device for minor %d\n", subminor
);
626 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
630 if (down_interruptible(&ftdi
->sw_lock
)) {
633 ftdi_elan_get_kref(ftdi
);
634 file
->private_data
= ftdi
;
641 static int ftdi_elan_release(struct inode
*inode
, struct file
*file
)
643 struct usb_ftdi
*ftdi
= file
->private_data
;
646 up(&ftdi
->sw_lock
); /* decrement the count on our device */
647 ftdi_elan_put_kref(ftdi
);
654 * blocking bulk reads are used to get data from the device
657 static ssize_t
ftdi_elan_read(struct file
*file
, char __user
*buffer
,
658 size_t count
, loff_t
*ppos
)
660 char data
[30 *3 + 4];
662 int m
= (sizeof(data
) - 1) / 3 - 1;
664 int retry_on_empty
= 10;
665 int retry_on_timeout
= 5;
666 struct usb_ftdi
*ftdi
= file
->private_data
;
667 if (ftdi
->disconnected
> 0) {
671 have
:if (ftdi
->bulk_in_left
> 0) {
673 char *p
= ++ftdi
->bulk_in_last
+ ftdi
->bulk_in_buffer
;
674 ftdi
->bulk_in_left
-= 1;
675 if (bytes_read
< m
) {
676 d
+= sprintf(d
, " %02X", 0x000000FF & *p
);
677 } else if (bytes_read
> m
) {
679 d
+= sprintf(d
, " ..");
680 if (copy_to_user(buffer
++, p
, 1)) {
689 more
:if (count
> 0) {
690 int packet_bytes
= 0;
691 int retval
= usb_bulk_msg(ftdi
->udev
,
692 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
693 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
695 if (packet_bytes
> 2) {
696 ftdi
->bulk_in_left
= packet_bytes
- 2;
697 ftdi
->bulk_in_last
= 1;
699 } else if (retval
== -ETIMEDOUT
) {
700 if (retry_on_timeout
-- > 0) {
702 } else if (bytes_read
> 0) {
706 } else if (retval
== 0) {
707 if (retry_on_empty
-- > 0) {
717 static void ftdi_elan_write_bulk_callback(struct urb
*urb
)
719 struct usb_ftdi
*ftdi
= urb
->context
;
720 int status
= urb
->status
;
722 if (status
&& !(status
== -ENOENT
|| status
== -ECONNRESET
||
723 status
== -ESHUTDOWN
)) {
724 dev_err(&ftdi
->udev
->dev
,
725 "urb=%p write bulk status received: %d\n", urb
, status
);
727 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
728 urb
->transfer_buffer
, urb
->transfer_dma
);
731 static int fill_buffer_with_all_queued_commands(struct usb_ftdi
*ftdi
,
732 char *buf
, int command_size
, int total_size
)
736 int I
= command_size
;
737 int i
= ftdi
->command_head
;
739 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
741 int F
= command
->follows
;
742 u8
*f
= command
->buffer
;
743 if (command
->header
& 0x80) {
744 ed_commands
|= 1 << (0x3 & (command
->header
>> 5));
746 buf
[b
++] = command
->header
;
747 buf
[b
++] = (command
->length
>> 0) & 0x00FF;
748 buf
[b
++] = (command
->length
>> 8) & 0x00FF;
749 buf
[b
++] = command
->address
;
750 buf
[b
++] = command
->width
;
758 static int ftdi_elan_total_command_size(struct usb_ftdi
*ftdi
, int command_size
)
761 int I
= command_size
;
762 int i
= ftdi
->command_head
;
764 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
766 total_size
+= 5 + command
->follows
;
771 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
)
778 int command_size
= ftdi
->command_next
- ftdi
->command_head
;
779 if (command_size
== 0)
781 total_size
= ftdi_elan_total_command_size(ftdi
, command_size
);
782 urb
= usb_alloc_urb(0, GFP_KERNEL
);
785 buf
= usb_alloc_coherent(ftdi
->udev
, total_size
, GFP_KERNEL
,
788 dev_err(&ftdi
->udev
->dev
, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
789 command_size
, total_size
);
793 ed_commands
= fill_buffer_with_all_queued_commands(ftdi
, buf
,
794 command_size
, total_size
);
795 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
796 ftdi
->bulk_out_endpointAddr
), buf
, total_size
,
797 ftdi_elan_write_bulk_callback
, ftdi
);
798 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
800 char diag
[40 *3 + 4];
804 int s
= (sizeof(diag
) - 1) / 3;
806 while (s
-- > 0 && m
-- > 0) {
807 if (s
> 0 || m
== 0) {
808 d
+= sprintf(d
, " %02X", *c
++);
810 d
+= sprintf(d
, " ..");
813 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
815 dev_err(&ftdi
->udev
->dev
, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
816 retval
, urb
, command_size
, total_size
);
817 usb_free_coherent(ftdi
->udev
, total_size
, buf
, urb
->transfer_dma
);
821 usb_free_urb(urb
); /* release our reference to this urb,
822 the USB core will eventually free it entirely */
823 ftdi
->command_head
+= command_size
;
824 ftdi_elan_kick_respond_queue(ftdi
);
828 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
829 struct u132_target
*target
, u8
*buffer
, int length
)
831 struct urb
*urb
= target
->urb
;
832 int halted
= target
->halted
;
833 int skipped
= target
->skipped
;
834 int actual
= target
->actual
;
835 int non_null
= target
->non_null
;
836 int toggle_bits
= target
->toggle_bits
;
837 int error_count
= target
->error_count
;
838 int condition_code
= target
->condition_code
;
839 int repeat_number
= target
->repeat_number
;
840 void (*callback
) (void *, struct urb
*, u8
*, int, int, int, int, int,
841 int, int, int, int) = target
->callback
;
843 target
->callback
= NULL
;
844 (*callback
) (target
->endp
, urb
, buffer
, length
, toggle_bits
,
845 error_count
, condition_code
, repeat_number
, halted
, skipped
,
849 static char *have_ed_set_response(struct usb_ftdi
*ftdi
,
850 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
853 int payload
= (ed_length
>> 0) & 0x07FF;
854 mutex_lock(&ftdi
->u132_lock
);
856 target
->non_null
= (ed_length
>> 15) & 0x0001;
857 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
858 if (ed_type
== 0x02 || ed_type
== 0x03) {
859 if (payload
== 0 || target
->abandoning
> 0) {
860 target
->abandoning
= 0;
861 mutex_unlock(&ftdi
->u132_lock
);
862 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
867 return ftdi
->response
;
869 ftdi
->expected
= 4 + payload
;
871 mutex_unlock(&ftdi
->u132_lock
);
875 target
->abandoning
= 0;
876 mutex_unlock(&ftdi
->u132_lock
);
877 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
882 return ftdi
->response
;
886 static char *have_ed_get_response(struct usb_ftdi
*ftdi
,
887 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
890 mutex_lock(&ftdi
->u132_lock
);
891 target
->condition_code
= TD_DEVNOTRESP
;
892 target
->actual
= (ed_length
>> 0) & 0x01FF;
893 target
->non_null
= (ed_length
>> 15) & 0x0001;
894 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
895 mutex_unlock(&ftdi
->u132_lock
);
897 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
898 target
->abandoning
= 0;
902 return ftdi
->response
;
907 * The engine tries to empty the FTDI fifo
909 * all responses found in the fifo data are dispatched thus
910 * the response buffer can only ever hold a maximum sized
911 * response from the Uxxx.
914 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
)
916 u8
*b
= ftdi
->response
+ ftdi
->received
;
918 int retry_on_empty
= 1;
919 int retry_on_timeout
= 3;
921 int packet_bytes
= 0;
922 int retval
= usb_bulk_msg(ftdi
->udev
,
923 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
924 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
926 char diag
[30 *3 + 4];
928 int m
= packet_bytes
;
929 u8
*c
= ftdi
->bulk_in_buffer
;
930 int s
= (sizeof(diag
) - 1) / 3;
932 while (s
-- > 0 && m
-- > 0) {
933 if (s
> 0 || m
== 0) {
934 d
+= sprintf(d
, " %02X", *c
++);
936 d
+= sprintf(d
, " ..");
938 if (packet_bytes
> 2) {
939 ftdi
->bulk_in_left
= packet_bytes
- 2;
940 ftdi
->bulk_in_last
= 1;
942 } else if (retval
== -ETIMEDOUT
) {
943 if (retry_on_timeout
-- > 0) {
944 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
945 packet_bytes
, bytes_read
, diag
);
947 } else if (bytes_read
> 0) {
948 dev_err(&ftdi
->udev
->dev
, "ONLY %d bytes%s\n",
952 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
953 packet_bytes
, bytes_read
, diag
);
956 } else if (retval
== -EILSEQ
) {
957 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes = %d with total %d bytes%s\n",
958 retval
, packet_bytes
, bytes_read
, diag
);
961 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes = %d with total %d bytes%s\n",
962 retval
, packet_bytes
, bytes_read
, diag
);
965 if (retry_on_empty
-- > 0) {
974 have
:if (ftdi
->bulk_in_left
> 0) {
975 u8 c
= ftdi
->bulk_in_buffer
[++ftdi
->bulk_in_last
];
977 ftdi
->bulk_in_left
-= 1;
978 if (ftdi
->received
== 0 && c
== 0xFF) {
982 if (++ftdi
->received
< ftdi
->expected
) {
984 } else if (ftdi
->ed_found
) {
985 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
986 u16 ed_length
= (ftdi
->response
[2] << 8) |
988 struct u132_target
*target
= &ftdi
->target
[ed_number
];
989 int payload
= (ed_length
>> 0) & 0x07FF;
990 char diag
[30 *3 + 4];
993 u8
*c
= 4 + ftdi
->response
;
994 int s
= (sizeof(diag
) - 1) / 3;
996 while (s
-- > 0 && m
-- > 0) {
997 if (s
> 0 || m
== 0) {
998 d
+= sprintf(d
, " %02X", *c
++);
1000 d
+= sprintf(d
, " ..");
1002 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
1009 } else if (ftdi
->expected
== 8) {
1011 int respond_head
= ftdi
->respond_head
++;
1012 struct u132_respond
*respond
= &ftdi
->respond
[
1013 RESPOND_MASK
& respond_head
];
1014 u32 data
= ftdi
->response
[7];
1016 data
|= ftdi
->response
[6];
1018 data
|= ftdi
->response
[5];
1020 data
|= ftdi
->response
[4];
1021 *respond
->value
= data
;
1022 *respond
->result
= 0;
1023 complete(&respond
->wait_completion
);
1028 buscmd
= (ftdi
->response
[0] >> 0) & 0x0F;
1029 if (buscmd
== 0x00) {
1030 } else if (buscmd
== 0x02) {
1031 } else if (buscmd
== 0x06) {
1032 } else if (buscmd
== 0x0A) {
1034 dev_err(&ftdi
->udev
->dev
, "Uxxx unknown(%0X) value = %08X\n",
1038 if ((ftdi
->response
[0] & 0x80) == 0x00) {
1042 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
1043 int ed_type
= (ftdi
->response
[0] >> 0) & 0x03;
1044 u16 ed_length
= (ftdi
->response
[2] << 8) |
1046 struct u132_target
*target
= &ftdi
->target
[
1048 target
->halted
= (ftdi
->response
[0] >> 3) &
1050 target
->skipped
= (ftdi
->response
[0] >> 2) &
1052 target
->toggle_bits
= (ftdi
->response
[3] >> 6)
1054 target
->error_count
= (ftdi
->response
[3] >> 4)
1056 target
->condition_code
= (ftdi
->response
[
1058 if ((ftdi
->response
[0] & 0x10) == 0x00) {
1059 b
= have_ed_set_response(ftdi
, target
,
1060 ed_length
, ed_number
, ed_type
,
1064 b
= have_ed_get_response(ftdi
, target
,
1065 ed_length
, ed_number
, ed_type
,
1077 * create a urb, and a buffer for it, and copy the data to the urb
1080 static ssize_t
ftdi_elan_write(struct file
*file
,
1081 const char __user
*user_buffer
, size_t count
,
1087 struct usb_ftdi
*ftdi
= file
->private_data
;
1089 if (ftdi
->disconnected
> 0) {
1095 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1100 buf
= usb_alloc_coherent(ftdi
->udev
, count
, GFP_KERNEL
,
1101 &urb
->transfer_dma
);
1106 if (copy_from_user(buf
, user_buffer
, count
)) {
1110 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1111 ftdi
->bulk_out_endpointAddr
), buf
, count
,
1112 ftdi_elan_write_bulk_callback
, ftdi
);
1113 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1114 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1116 dev_err(&ftdi
->udev
->dev
,
1117 "failed submitting write urb, error %d\n", retval
);
1125 usb_free_coherent(ftdi
->udev
, count
, buf
, urb
->transfer_dma
);
1132 static const struct file_operations ftdi_elan_fops
= {
1133 .owner
= THIS_MODULE
,
1134 .llseek
= no_llseek
,
1135 .read
= ftdi_elan_read
,
1136 .write
= ftdi_elan_write
,
1137 .open
= ftdi_elan_open
,
1138 .release
= ftdi_elan_release
,
1142 * usb class driver info in order to get a minor number from the usb core,
1143 * and to have the device registered with the driver core
1145 static struct usb_class_driver ftdi_elan_jtag_class
= {
1146 .name
= "ftdi-%d-jtag",
1147 .fops
= &ftdi_elan_fops
,
1148 .minor_base
= USB_FTDI_ELAN_MINOR_BASE
,
1152 * the following definitions are for the
1153 * ELAN FPGA state machgine processor that
1154 * lies on the other side of the FTDI chip
1156 #define cPCIu132rd 0x0
1157 #define cPCIu132wr 0x1
1158 #define cPCIiord 0x2
1159 #define cPCIiowr 0x3
1160 #define cPCImemrd 0x6
1161 #define cPCImemwr 0x7
1162 #define cPCIcfgrd 0xA
1163 #define cPCIcfgwr 0xB
1164 #define cPCInull 0xF
1165 #define cU132cmd_status 0x0
1166 #define cU132flash 0x1
1167 #define cPIDsetup 0x0
1170 #define cPIDinonce 0x3
1171 #define cCCnoerror 0x0
1173 #define cCCbitstuff 0x2
1174 #define cCCtoggle 0x3
1175 #define cCCstall 0x4
1176 #define cCCnoresp 0x5
1177 #define cCCbadpid1 0x6
1178 #define cCCbadpid2 0x7
1179 #define cCCdataoverrun 0x8
1180 #define cCCdataunderrun 0x9
1181 #define cCCbuffoverrun 0xC
1182 #define cCCbuffunderrun 0xD
1183 #define cCCnotaccessed 0xF
1184 static int ftdi_elan_write_reg(struct usb_ftdi
*ftdi
, u32 data
)
1186 wait
:if (ftdi
->disconnected
> 0) {
1190 mutex_lock(&ftdi
->u132_lock
);
1191 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1192 if (command_size
< COMMAND_SIZE
) {
1193 struct u132_command
*command
= &ftdi
->command
[
1194 COMMAND_MASK
& ftdi
->command_next
];
1195 command
->header
= 0x00 | cPCIu132wr
;
1196 command
->length
= 0x04;
1197 command
->address
= 0x00;
1198 command
->width
= 0x00;
1199 command
->follows
= 4;
1200 command
->value
= data
;
1201 command
->buffer
= &command
->value
;
1202 ftdi
->command_next
+= 1;
1203 ftdi_elan_kick_command_queue(ftdi
);
1204 mutex_unlock(&ftdi
->u132_lock
);
1207 mutex_unlock(&ftdi
->u132_lock
);
1214 static int ftdi_elan_write_config(struct usb_ftdi
*ftdi
, int config_offset
,
1217 u8 addressofs
= config_offset
/ 4;
1218 wait
:if (ftdi
->disconnected
> 0) {
1222 mutex_lock(&ftdi
->u132_lock
);
1223 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1224 if (command_size
< COMMAND_SIZE
) {
1225 struct u132_command
*command
= &ftdi
->command
[
1226 COMMAND_MASK
& ftdi
->command_next
];
1227 command
->header
= 0x00 | (cPCIcfgwr
& 0x0F);
1228 command
->length
= 0x04;
1229 command
->address
= addressofs
;
1230 command
->width
= 0x00 | (width
& 0x0F);
1231 command
->follows
= 4;
1232 command
->value
= data
;
1233 command
->buffer
= &command
->value
;
1234 ftdi
->command_next
+= 1;
1235 ftdi_elan_kick_command_queue(ftdi
);
1236 mutex_unlock(&ftdi
->u132_lock
);
1239 mutex_unlock(&ftdi
->u132_lock
);
1246 static int ftdi_elan_write_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1249 u8 addressofs
= mem_offset
/ 4;
1250 wait
:if (ftdi
->disconnected
> 0) {
1254 mutex_lock(&ftdi
->u132_lock
);
1255 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1256 if (command_size
< COMMAND_SIZE
) {
1257 struct u132_command
*command
= &ftdi
->command
[
1258 COMMAND_MASK
& ftdi
->command_next
];
1259 command
->header
= 0x00 | (cPCImemwr
& 0x0F);
1260 command
->length
= 0x04;
1261 command
->address
= addressofs
;
1262 command
->width
= 0x00 | (width
& 0x0F);
1263 command
->follows
= 4;
1264 command
->value
= data
;
1265 command
->buffer
= &command
->value
;
1266 ftdi
->command_next
+= 1;
1267 ftdi_elan_kick_command_queue(ftdi
);
1268 mutex_unlock(&ftdi
->u132_lock
);
1271 mutex_unlock(&ftdi
->u132_lock
);
1278 int usb_ftdi_elan_write_pcimem(struct platform_device
*pdev
, int mem_offset
,
1281 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1282 return ftdi_elan_write_pcimem(ftdi
, mem_offset
, width
, data
);
1286 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem
);
1287 static int ftdi_elan_read_reg(struct usb_ftdi
*ftdi
, u32
*data
)
1289 wait
:if (ftdi
->disconnected
> 0) {
1294 mutex_lock(&ftdi
->u132_lock
);
1295 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1296 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1297 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1299 struct u132_command
*command
= &ftdi
->command
[
1300 COMMAND_MASK
& ftdi
->command_next
];
1301 struct u132_respond
*respond
= &ftdi
->respond
[
1302 RESPOND_MASK
& ftdi
->respond_next
];
1303 int result
= -ENODEV
;
1304 respond
->result
= &result
;
1305 respond
->header
= command
->header
= 0x00 | cPCIu132rd
;
1306 command
->length
= 0x04;
1307 respond
->address
= command
->address
= cU132cmd_status
;
1308 command
->width
= 0x00;
1309 command
->follows
= 0;
1311 command
->buffer
= NULL
;
1312 respond
->value
= data
;
1313 init_completion(&respond
->wait_completion
);
1314 ftdi
->command_next
+= 1;
1315 ftdi
->respond_next
+= 1;
1316 ftdi_elan_kick_command_queue(ftdi
);
1317 mutex_unlock(&ftdi
->u132_lock
);
1318 wait_for_completion(&respond
->wait_completion
);
1321 mutex_unlock(&ftdi
->u132_lock
);
1328 static int ftdi_elan_read_config(struct usb_ftdi
*ftdi
, int config_offset
,
1329 u8 width
, u32
*data
)
1331 u8 addressofs
= config_offset
/ 4;
1332 wait
:if (ftdi
->disconnected
> 0) {
1337 mutex_lock(&ftdi
->u132_lock
);
1338 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1339 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1340 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1342 struct u132_command
*command
= &ftdi
->command
[
1343 COMMAND_MASK
& ftdi
->command_next
];
1344 struct u132_respond
*respond
= &ftdi
->respond
[
1345 RESPOND_MASK
& ftdi
->respond_next
];
1346 int result
= -ENODEV
;
1347 respond
->result
= &result
;
1348 respond
->header
= command
->header
= 0x00 | (cPCIcfgrd
&
1350 command
->length
= 0x04;
1351 respond
->address
= command
->address
= addressofs
;
1352 command
->width
= 0x00 | (width
& 0x0F);
1353 command
->follows
= 0;
1355 command
->buffer
= NULL
;
1356 respond
->value
= data
;
1357 init_completion(&respond
->wait_completion
);
1358 ftdi
->command_next
+= 1;
1359 ftdi
->respond_next
+= 1;
1360 ftdi_elan_kick_command_queue(ftdi
);
1361 mutex_unlock(&ftdi
->u132_lock
);
1362 wait_for_completion(&respond
->wait_completion
);
1365 mutex_unlock(&ftdi
->u132_lock
);
1372 static int ftdi_elan_read_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1373 u8 width
, u32
*data
)
1375 u8 addressofs
= mem_offset
/ 4;
1376 wait
:if (ftdi
->disconnected
> 0) {
1381 mutex_lock(&ftdi
->u132_lock
);
1382 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1383 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1384 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1386 struct u132_command
*command
= &ftdi
->command
[
1387 COMMAND_MASK
& ftdi
->command_next
];
1388 struct u132_respond
*respond
= &ftdi
->respond
[
1389 RESPOND_MASK
& ftdi
->respond_next
];
1390 int result
= -ENODEV
;
1391 respond
->result
= &result
;
1392 respond
->header
= command
->header
= 0x00 | (cPCImemrd
&
1394 command
->length
= 0x04;
1395 respond
->address
= command
->address
= addressofs
;
1396 command
->width
= 0x00 | (width
& 0x0F);
1397 command
->follows
= 0;
1399 command
->buffer
= NULL
;
1400 respond
->value
= data
;
1401 init_completion(&respond
->wait_completion
);
1402 ftdi
->command_next
+= 1;
1403 ftdi
->respond_next
+= 1;
1404 ftdi_elan_kick_command_queue(ftdi
);
1405 mutex_unlock(&ftdi
->u132_lock
);
1406 wait_for_completion(&respond
->wait_completion
);
1409 mutex_unlock(&ftdi
->u132_lock
);
1416 int usb_ftdi_elan_read_pcimem(struct platform_device
*pdev
, int mem_offset
,
1417 u8 width
, u32
*data
)
1419 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1420 if (ftdi
->initialized
== 0) {
1423 return ftdi_elan_read_pcimem(ftdi
, mem_offset
, width
, data
);
1427 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem
);
1428 static int ftdi_elan_edset_setup(struct usb_ftdi
*ftdi
, u8 ed_number
,
1429 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1430 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1431 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1432 int halted
, int skipped
, int actual
, int non_null
))
1434 u8 ed
= ed_number
- 1;
1435 wait
:if (ftdi
->disconnected
> 0) {
1437 } else if (ftdi
->initialized
== 0) {
1441 mutex_lock(&ftdi
->u132_lock
);
1442 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1443 if (command_size
< COMMAND_SIZE
) {
1444 struct u132_target
*target
= &ftdi
->target
[ed
];
1445 struct u132_command
*command
= &ftdi
->command
[
1446 COMMAND_MASK
& ftdi
->command_next
];
1447 command
->header
= 0x80 | (ed
<< 5);
1448 command
->length
= 0x8007;
1449 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1451 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1452 usb_pipeout(urb
->pipe
));
1453 command
->follows
= 8;
1455 command
->buffer
= urb
->setup_packet
;
1456 target
->callback
= callback
;
1457 target
->endp
= endp
;
1460 ftdi
->command_next
+= 1;
1461 ftdi_elan_kick_command_queue(ftdi
);
1462 mutex_unlock(&ftdi
->u132_lock
);
1465 mutex_unlock(&ftdi
->u132_lock
);
1472 int usb_ftdi_elan_edset_setup(struct platform_device
*pdev
, u8 ed_number
,
1473 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1474 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1475 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1476 int halted
, int skipped
, int actual
, int non_null
))
1478 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1479 return ftdi_elan_edset_setup(ftdi
, ed_number
, endp
, urb
, address
,
1480 ep_number
, toggle_bits
, callback
);
1484 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup
);
1485 static int ftdi_elan_edset_input(struct usb_ftdi
*ftdi
, u8 ed_number
,
1486 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1487 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1488 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1489 int halted
, int skipped
, int actual
, int non_null
))
1491 u8 ed
= ed_number
- 1;
1492 wait
:if (ftdi
->disconnected
> 0) {
1494 } else if (ftdi
->initialized
== 0) {
1498 mutex_lock(&ftdi
->u132_lock
);
1499 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1500 if (command_size
< COMMAND_SIZE
) {
1501 struct u132_target
*target
= &ftdi
->target
[ed
];
1502 struct u132_command
*command
= &ftdi
->command
[
1503 COMMAND_MASK
& ftdi
->command_next
];
1504 u32 remaining_length
= urb
->transfer_buffer_length
-
1506 command
->header
= 0x82 | (ed
<< 5);
1507 if (remaining_length
== 0) {
1508 command
->length
= 0x0000;
1509 } else if (remaining_length
> 1024) {
1510 command
->length
= 0x8000 | 1023;
1512 command
->length
= 0x8000 | (remaining_length
-
1514 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1516 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1517 usb_pipeout(urb
->pipe
));
1518 command
->follows
= 0;
1520 command
->buffer
= NULL
;
1521 target
->callback
= callback
;
1522 target
->endp
= endp
;
1525 ftdi
->command_next
+= 1;
1526 ftdi_elan_kick_command_queue(ftdi
);
1527 mutex_unlock(&ftdi
->u132_lock
);
1530 mutex_unlock(&ftdi
->u132_lock
);
1537 int usb_ftdi_elan_edset_input(struct platform_device
*pdev
, u8 ed_number
,
1538 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1539 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1540 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1541 int halted
, int skipped
, int actual
, int non_null
))
1543 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1544 return ftdi_elan_edset_input(ftdi
, ed_number
, endp
, urb
, address
,
1545 ep_number
, toggle_bits
, callback
);
1549 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input
);
1550 static int ftdi_elan_edset_empty(struct usb_ftdi
*ftdi
, u8 ed_number
,
1551 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1552 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1553 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1554 int halted
, int skipped
, int actual
, int non_null
))
1556 u8 ed
= ed_number
- 1;
1557 wait
:if (ftdi
->disconnected
> 0) {
1559 } else if (ftdi
->initialized
== 0) {
1563 mutex_lock(&ftdi
->u132_lock
);
1564 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1565 if (command_size
< COMMAND_SIZE
) {
1566 struct u132_target
*target
= &ftdi
->target
[ed
];
1567 struct u132_command
*command
= &ftdi
->command
[
1568 COMMAND_MASK
& ftdi
->command_next
];
1569 command
->header
= 0x81 | (ed
<< 5);
1570 command
->length
= 0x0000;
1571 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1573 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1574 usb_pipeout(urb
->pipe
));
1575 command
->follows
= 0;
1577 command
->buffer
= NULL
;
1578 target
->callback
= callback
;
1579 target
->endp
= endp
;
1582 ftdi
->command_next
+= 1;
1583 ftdi_elan_kick_command_queue(ftdi
);
1584 mutex_unlock(&ftdi
->u132_lock
);
1587 mutex_unlock(&ftdi
->u132_lock
);
1594 int usb_ftdi_elan_edset_empty(struct platform_device
*pdev
, u8 ed_number
,
1595 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1596 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1597 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1598 int halted
, int skipped
, int actual
, int non_null
))
1600 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1601 return ftdi_elan_edset_empty(ftdi
, ed_number
, endp
, urb
, address
,
1602 ep_number
, toggle_bits
, callback
);
1606 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty
);
1607 static int ftdi_elan_edset_output(struct usb_ftdi
*ftdi
, u8 ed_number
,
1608 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1609 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1610 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1611 int halted
, int skipped
, int actual
, int non_null
))
1613 u8 ed
= ed_number
- 1;
1614 wait
:if (ftdi
->disconnected
> 0) {
1616 } else if (ftdi
->initialized
== 0) {
1620 mutex_lock(&ftdi
->u132_lock
);
1621 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1622 if (command_size
< COMMAND_SIZE
) {
1626 char data
[30 *3 + 4];
1628 int m
= (sizeof(data
) - 1) / 3 - 1;
1630 struct u132_target
*target
= &ftdi
->target
[ed
];
1631 struct u132_command
*command
= &ftdi
->command
[
1632 COMMAND_MASK
& ftdi
->command_next
];
1633 command
->header
= 0x81 | (ed
<< 5);
1634 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1636 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1637 usb_pipeout(urb
->pipe
));
1638 command
->follows
= min_t(u32
, 1024,
1639 urb
->transfer_buffer_length
-
1640 urb
->actual_length
);
1642 command
->buffer
= urb
->transfer_buffer
+
1644 command
->length
= 0x8000 | (command
->follows
- 1);
1645 b
= command
->buffer
;
1646 urb_size
= command
->follows
;
1648 while (urb_size
-- > 0) {
1650 } else if (i
++ < m
) {
1651 int w
= sprintf(d
, " %02X", *b
++);
1655 d
+= sprintf(d
, " ..");
1657 target
->callback
= callback
;
1658 target
->endp
= endp
;
1661 ftdi
->command_next
+= 1;
1662 ftdi_elan_kick_command_queue(ftdi
);
1663 mutex_unlock(&ftdi
->u132_lock
);
1666 mutex_unlock(&ftdi
->u132_lock
);
1673 int usb_ftdi_elan_edset_output(struct platform_device
*pdev
, u8 ed_number
,
1674 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1675 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1676 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1677 int halted
, int skipped
, int actual
, int non_null
))
1679 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1680 return ftdi_elan_edset_output(ftdi
, ed_number
, endp
, urb
, address
,
1681 ep_number
, toggle_bits
, callback
);
1685 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output
);
1686 static int ftdi_elan_edset_single(struct usb_ftdi
*ftdi
, u8 ed_number
,
1687 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1688 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1689 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1690 int halted
, int skipped
, int actual
, int non_null
))
1692 u8 ed
= ed_number
- 1;
1693 wait
:if (ftdi
->disconnected
> 0) {
1695 } else if (ftdi
->initialized
== 0) {
1699 mutex_lock(&ftdi
->u132_lock
);
1700 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1701 if (command_size
< COMMAND_SIZE
) {
1702 u32 remaining_length
= urb
->transfer_buffer_length
-
1704 struct u132_target
*target
= &ftdi
->target
[ed
];
1705 struct u132_command
*command
= &ftdi
->command
[
1706 COMMAND_MASK
& ftdi
->command_next
];
1707 command
->header
= 0x83 | (ed
<< 5);
1708 if (remaining_length
== 0) {
1709 command
->length
= 0x0000;
1710 } else if (remaining_length
> 1024) {
1711 command
->length
= 0x8000 | 1023;
1713 command
->length
= 0x8000 | (remaining_length
-
1715 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1717 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1718 usb_pipeout(urb
->pipe
));
1719 command
->follows
= 0;
1721 command
->buffer
= NULL
;
1722 target
->callback
= callback
;
1723 target
->endp
= endp
;
1726 ftdi
->command_next
+= 1;
1727 ftdi_elan_kick_command_queue(ftdi
);
1728 mutex_unlock(&ftdi
->u132_lock
);
1731 mutex_unlock(&ftdi
->u132_lock
);
1738 int usb_ftdi_elan_edset_single(struct platform_device
*pdev
, u8 ed_number
,
1739 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1740 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1741 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1742 int halted
, int skipped
, int actual
, int non_null
))
1744 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1745 return ftdi_elan_edset_single(ftdi
, ed_number
, endp
, urb
, address
,
1746 ep_number
, toggle_bits
, callback
);
1750 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single
);
1751 static int ftdi_elan_edset_flush(struct usb_ftdi
*ftdi
, u8 ed_number
,
1754 u8 ed
= ed_number
- 1;
1755 if (ftdi
->disconnected
> 0) {
1757 } else if (ftdi
->initialized
== 0) {
1760 struct u132_target
*target
= &ftdi
->target
[ed
];
1761 mutex_lock(&ftdi
->u132_lock
);
1762 if (target
->abandoning
> 0) {
1763 mutex_unlock(&ftdi
->u132_lock
);
1766 target
->abandoning
= 1;
1767 wait_1
:if (target
->active
== 1) {
1768 int command_size
= ftdi
->command_next
-
1770 if (command_size
< COMMAND_SIZE
) {
1771 struct u132_command
*command
=
1772 &ftdi
->command
[COMMAND_MASK
&
1773 ftdi
->command_next
];
1774 command
->header
= 0x80 | (ed
<< 5) |
1776 command
->length
= 0x00;
1777 command
->address
= 0x00;
1778 command
->width
= 0x00;
1779 command
->follows
= 0;
1781 command
->buffer
= &command
->value
;
1782 ftdi
->command_next
+= 1;
1783 ftdi_elan_kick_command_queue(ftdi
);
1785 mutex_unlock(&ftdi
->u132_lock
);
1787 mutex_lock(&ftdi
->u132_lock
);
1791 mutex_unlock(&ftdi
->u132_lock
);
1797 int usb_ftdi_elan_edset_flush(struct platform_device
*pdev
, u8 ed_number
,
1800 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1801 return ftdi_elan_edset_flush(ftdi
, ed_number
, endp
);
1805 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush
);
1806 static int ftdi_elan_flush_input_fifo(struct usb_ftdi
*ftdi
)
1808 int retry_on_empty
= 10;
1809 int retry_on_timeout
= 5;
1810 int retry_on_status
= 20;
1812 int packet_bytes
= 0;
1813 int retval
= usb_bulk_msg(ftdi
->udev
,
1814 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
1815 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
1816 &packet_bytes
, 100);
1817 if (packet_bytes
> 2) {
1818 char diag
[30 *3 + 4];
1820 int m
= (sizeof(diag
) - 1) / 3 - 1;
1821 char *b
= ftdi
->bulk_in_buffer
;
1824 while (packet_bytes
-- > 0) {
1826 if (bytes_read
< m
) {
1827 d
+= sprintf(d
, " %02X",
1829 } else if (bytes_read
> m
) {
1831 d
+= sprintf(d
, " ..");
1836 } else if (packet_bytes
> 1) {
1837 char s1
= ftdi
->bulk_in_buffer
[0];
1838 char s2
= ftdi
->bulk_in_buffer
[1];
1839 if (s1
== 0x31 && s2
== 0x60) {
1841 } else if (retry_on_status
-- > 0) {
1844 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry limit reached\n");
1847 } else if (packet_bytes
> 0) {
1848 char b1
= ftdi
->bulk_in_buffer
[0];
1849 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from FTDI = %02X\n",
1851 if (retry_on_status
-- > 0) {
1854 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry limit reached\n");
1857 } else if (retval
== -ETIMEDOUT
) {
1858 if (retry_on_timeout
-- > 0) {
1861 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limit reached\n");
1864 } else if (retval
== 0) {
1865 if (retry_on_empty
-- > 0) {
1868 dev_err(&ftdi
->udev
->dev
, "empty packet retry limit reached\n");
1872 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
1881 * send the long flush sequence
1884 static int ftdi_elan_synchronize_flush(struct usb_ftdi
*ftdi
)
1891 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1894 buf
= usb_alloc_coherent(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
1896 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for flush sequence\n");
1902 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1903 ftdi
->bulk_out_endpointAddr
), buf
, i
,
1904 ftdi_elan_write_bulk_callback
, ftdi
);
1905 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1906 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1908 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the flush sequence\n");
1909 usb_free_coherent(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
1919 * send the reset sequence
1922 static int ftdi_elan_synchronize_reset(struct usb_ftdi
*ftdi
)
1929 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1932 buf
= usb_alloc_coherent(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
1934 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for the reset sequence\n");
1942 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1943 ftdi
->bulk_out_endpointAddr
), buf
, i
,
1944 ftdi_elan_write_bulk_callback
, ftdi
);
1945 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1946 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1948 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the reset sequence\n");
1949 usb_free_coherent(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
1957 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
)
1961 int retry_on_timeout
= 5;
1962 int retry_on_empty
= 10;
1964 retval
= ftdi_elan_flush_input_fifo(ftdi
);
1967 ftdi
->bulk_in_left
= 0;
1968 ftdi
->bulk_in_last
= -1;
1969 while (long_stop
-- > 0) {
1972 retval
= ftdi_elan_synchronize_flush(ftdi
);
1975 retval
= ftdi_elan_flush_input_fifo(ftdi
);
1978 reset
:retval
= ftdi_elan_synchronize_reset(ftdi
);
1984 int packet_bytes
= 0;
1985 retval
= usb_bulk_msg(ftdi
->udev
,
1986 usb_rcvbulkpipe(ftdi
->udev
,
1987 ftdi
->bulk_in_endpointAddr
),
1988 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
1989 &packet_bytes
, 500);
1990 if (packet_bytes
> 2) {
1991 char diag
[30 *3 + 4];
1993 int m
= (sizeof(diag
) - 1) / 3 - 1;
1994 char *b
= ftdi
->bulk_in_buffer
;
1996 unsigned char c
= 0;
1998 while (packet_bytes
-- > 0) {
2000 if (bytes_read
< m
) {
2001 d
+= sprintf(d
, " %02X", c
);
2002 } else if (bytes_read
> m
) {
2004 d
+= sprintf(d
, " ..");
2013 } else if (read_stop
-- > 0) {
2016 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2020 } else if (packet_bytes
> 1) {
2021 unsigned char s1
= ftdi
->bulk_in_buffer
[0];
2022 unsigned char s2
= ftdi
->bulk_in_buffer
[1];
2023 if (s1
== 0x31 && s2
== 0x00) {
2024 if (read_stuck
-- > 0) {
2029 if (read_stop
-- > 0) {
2032 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2036 } else if (packet_bytes
> 0) {
2037 if (read_stop
-- > 0) {
2040 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2043 } else if (retval
== -ETIMEDOUT
) {
2044 if (retry_on_timeout
-- > 0) {
2047 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limit reached\n");
2050 } else if (retval
== 0) {
2051 if (retry_on_empty
-- > 0) {
2054 dev_err(&ftdi
->udev
->dev
, "empty packet retry limit reached\n");
2059 dev_err(&ftdi
->udev
->dev
, "error = %d\n",
2061 if (read_stop
-- > 0) {
2064 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2070 dev_err(&ftdi
->udev
->dev
, "failed to synchronize\n");
2074 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
)
2076 int retry_on_empty
= 10;
2077 int retry_on_timeout
= 5;
2078 int retry_on_status
= 50;
2080 int packet_bytes
= 0;
2081 int retval
= usb_bulk_msg(ftdi
->udev
,
2082 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
2083 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
2084 &packet_bytes
, 1000);
2085 if (packet_bytes
> 2) {
2086 char diag
[30 *3 + 4];
2088 int m
= (sizeof(diag
) - 1) / 3 - 1;
2089 char *b
= ftdi
->bulk_in_buffer
;
2092 while (packet_bytes
-- > 0) {
2094 if (bytes_read
< m
) {
2095 d
+= sprintf(d
, " %02X",
2097 } else if (bytes_read
> m
) {
2099 d
+= sprintf(d
, " ..");
2104 } else if (packet_bytes
> 1) {
2105 char s1
= ftdi
->bulk_in_buffer
[0];
2106 char s2
= ftdi
->bulk_in_buffer
[1];
2107 if (s1
== 0x31 && s2
== 0x60) {
2109 } else if (retry_on_status
-- > 0) {
2114 } else if (packet_bytes
> 0) {
2115 char b1
= ftdi
->bulk_in_buffer
[0];
2116 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from FTDI = %02X\n", b1
);
2117 if (retry_on_status
-- > 0) {
2121 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry limit reached\n");
2124 } else if (retval
== -ETIMEDOUT
) {
2125 if (retry_on_timeout
-- > 0) {
2128 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limit reached\n");
2131 } else if (retval
== 0) {
2132 if (retry_on_empty
-- > 0) {
2135 dev_err(&ftdi
->udev
->dev
, "empty packet retry limit reached\n");
2139 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
2146 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
)
2148 int UxxxStatus
= ftdi_elan_read_reg(ftdi
, &ftdi
->controlreg
);
2151 if (ftdi
->controlreg
& 0x00400000) {
2152 if (ftdi
->card_ejected
) {
2154 ftdi
->card_ejected
= 1;
2155 dev_err(&ftdi
->udev
->dev
, "CARD EJECTED - controlreg = %08X\n",
2160 u8 fn
= ftdi
->function
- 1;
2161 int activePCIfn
= fn
<< 8;
2166 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2170 pciVID
= pcidata
& 0xFFFF;
2171 pciPID
= (pcidata
>> 16) & 0xFFFF;
2172 if (pciVID
== ftdi
->platform_data
.vendor
&& pciPID
==
2173 ftdi
->platform_data
.device
) {
2176 dev_err(&ftdi
->udev
->dev
, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2177 ftdi
->platform_data
.vendor
, pciVID
,
2178 ftdi
->platform_data
.device
, pciPID
);
2185 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2186 offsetof(struct ohci_regs, member), 0, data);
2187 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2188 offsetof(struct ohci_regs, member), 0, data);
2190 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2191 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2193 static int ftdi_elan_check_controller(struct usb_ftdi
*ftdi
, int quirk
)
2207 int mask
= OHCI_INTR_INIT
;
2209 int reset_timeout
= 30; /* ... allow extra time */
2211 retval
= ftdi_write_pcimem(ftdi
, intrdisable
, OHCI_INTR_MIE
);
2214 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2217 retval
= ftdi_read_pcimem(ftdi
, roothub
.a
, &rh_a
);
2220 num_ports
= rh_a
& RH_A_NDP
;
2221 retval
= ftdi_read_pcimem(ftdi
, fminterval
, &hc_fminterval
);
2224 hc_fminterval
&= 0x3fff;
2225 if (hc_fminterval
!= FI
) {
2227 hc_fminterval
|= FSMP(hc_fminterval
) << 16;
2228 retval
= ftdi_read_pcimem(ftdi
, control
, &hc_control
);
2231 switch (hc_control
& OHCI_CTRL_HCFS
) {
2235 case OHCI_USB_SUSPEND
:
2236 case OHCI_USB_RESUME
:
2237 hc_control
&= OHCI_CTRL_RWC
;
2238 hc_control
|= OHCI_USB_RESUME
;
2242 hc_control
&= OHCI_CTRL_RWC
;
2243 hc_control
|= OHCI_USB_RESET
;
2247 retval
= ftdi_write_pcimem(ftdi
, control
, hc_control
);
2250 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2254 retval
= ftdi_read_pcimem(ftdi
, roothub
.a
, &roothub_a
);
2257 if (!(roothub_a
& RH_A_NPS
)) { /* power down each port */
2258 for (temp
= 0; temp
< num_ports
; temp
++) {
2259 retval
= ftdi_write_pcimem(ftdi
,
2260 roothub
.portstatus
[temp
], RH_PS_LSDA
);
2265 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2268 retry
:retval
= ftdi_read_pcimem(ftdi
, cmdstatus
, &status
);
2271 retval
= ftdi_write_pcimem(ftdi
, cmdstatus
, OHCI_HCR
);
2275 retval
= ftdi_read_pcimem(ftdi
, cmdstatus
, &status
);
2278 if (0 != (status
& OHCI_HCR
)) {
2279 if (--reset_timeout
== 0) {
2280 dev_err(&ftdi
->udev
->dev
, "USB HC reset timed out!\n");
2288 if (quirk
& OHCI_QUIRK_INITRESET
) {
2289 retval
= ftdi_write_pcimem(ftdi
, control
, hc_control
);
2292 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2296 retval
= ftdi_write_pcimem(ftdi
, ed_controlhead
, 0x00000000);
2299 retval
= ftdi_write_pcimem(ftdi
, ed_bulkhead
, 0x11000000);
2302 retval
= ftdi_write_pcimem(ftdi
, hcca
, 0x00000000);
2305 retval
= ftdi_read_pcimem(ftdi
, fminterval
, &fminterval
);
2308 retval
= ftdi_write_pcimem(ftdi
, fminterval
,
2309 ((fminterval
& FIT
) ^ FIT
) | hc_fminterval
);
2312 retval
= ftdi_write_pcimem(ftdi
, periodicstart
,
2313 ((9 *hc_fminterval
) / 10) & 0x3fff);
2316 retval
= ftdi_read_pcimem(ftdi
, fminterval
, &fminterval
);
2319 retval
= ftdi_read_pcimem(ftdi
, periodicstart
, &periodicstart
);
2322 if (0 == (fminterval
& 0x3fff0000) || 0 == periodicstart
) {
2323 if (!(quirk
& OHCI_QUIRK_INITRESET
)) {
2324 quirk
|= OHCI_QUIRK_INITRESET
;
2327 dev_err(&ftdi
->udev
->dev
, "init err(%08x %04x)\n",
2328 fminterval
, periodicstart
);
2329 } /* start controller operations */
2330 hc_control
&= OHCI_CTRL_RWC
;
2331 hc_control
|= OHCI_CONTROL_INIT
| OHCI_CTRL_BLE
| OHCI_USB_OPER
;
2332 retval
= ftdi_write_pcimem(ftdi
, control
, hc_control
);
2335 retval
= ftdi_write_pcimem(ftdi
, cmdstatus
, OHCI_BLF
);
2338 retval
= ftdi_read_pcimem(ftdi
, cmdstatus
, &cmdstatus
);
2341 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2344 retval
= ftdi_write_pcimem(ftdi
, roothub
.status
, RH_HS_DRWE
);
2347 retval
= ftdi_write_pcimem(ftdi
, intrstatus
, mask
);
2350 retval
= ftdi_write_pcimem(ftdi
, intrdisable
,
2351 OHCI_INTR_MIE
| OHCI_INTR_OC
| OHCI_INTR_RHSC
| OHCI_INTR_FNO
|
2352 OHCI_INTR_UE
| OHCI_INTR_RD
| OHCI_INTR_SF
| OHCI_INTR_WDH
|
2355 return retval
; /* handle root hub init quirks ... */
2356 retval
= ftdi_read_pcimem(ftdi
, roothub
.a
, &roothub_a
);
2359 roothub_a
&= ~(RH_A_PSM
| RH_A_OCPM
);
2360 if (quirk
& OHCI_QUIRK_SUPERIO
) {
2361 roothub_a
|= RH_A_NOCP
;
2362 roothub_a
&= ~(RH_A_POTPGT
| RH_A_NPS
);
2363 retval
= ftdi_write_pcimem(ftdi
, roothub
.a
, roothub_a
);
2366 } else if ((quirk
& OHCI_QUIRK_AMD756
) || distrust_firmware
) {
2367 roothub_a
|= RH_A_NPS
;
2368 retval
= ftdi_write_pcimem(ftdi
, roothub
.a
, roothub_a
);
2372 retval
= ftdi_write_pcimem(ftdi
, roothub
.status
, RH_HS_LPSC
);
2375 retval
= ftdi_write_pcimem(ftdi
, roothub
.b
,
2376 (roothub_a
& RH_A_NPS
) ? 0 : RH_B_PPCM
);
2379 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2382 mdelay((roothub_a
>> 23) & 0x1fe);
2383 for (temp
= 0; temp
< num_ports
; temp
++) {
2385 retval
= ftdi_read_pcimem(ftdi
, roothub
.portstatus
[temp
],
2395 static int ftdi_elan_setup_controller(struct usb_ftdi
*ftdi
, int fn
)
2401 int activePCIfn
= fn
<< 8;
2402 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x2800);
2406 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2410 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2414 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2418 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2423 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2427 latence_timer
&= 0xFFFF00FF;
2428 latence_timer
|= 0x00001600;
2429 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2433 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2438 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2442 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2446 for (reg
= 0; reg
<= 0x54; reg
+= 4) {
2447 UxxxStatus
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2454 static int ftdi_elan_close_controller(struct usb_ftdi
*ftdi
, int fn
)
2460 int activePCIfn
= fn
<< 8;
2461 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x2800);
2465 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2469 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2473 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2477 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2482 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2486 latence_timer
&= 0xFFFF00FF;
2487 latence_timer
|= 0x00001600;
2488 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2492 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2497 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2501 return ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0, &pcidata
);
2504 static int ftdi_elan_found_controller(struct usb_ftdi
*ftdi
, int fn
, int quirk
)
2508 UxxxStatus
= ftdi_elan_setup_controller(ftdi
, fn
);
2511 result
= ftdi_elan_check_controller(ftdi
, quirk
);
2512 UxxxStatus
= ftdi_elan_close_controller(ftdi
, fn
);
2518 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
)
2523 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2526 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000000L
);
2530 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x100);
2533 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x500);
2536 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2539 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020CL
| 0x000);
2542 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020DL
| 0x000);
2546 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020FL
| 0x000);
2549 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2552 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x800);
2555 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2558 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2562 sensebits
= (controlreg
>> 16) & 0x000F;
2563 if (0x0D == sensebits
)
2569 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
)
2575 int activePCIfn
= 0;
2576 int max_devices
= 0;
2577 int controllers
= 0;
2578 int unrecognized
= 0;
2580 for (fn
= 0; (fn
< 4); fn
++) {
2584 activePCIfn
= fn
<< 8;
2585 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2589 pciVID
= pcidata
& 0xFFFF;
2590 pciPID
= (pcidata
>> 16) & 0xFFFF;
2591 if ((pciVID
== PCI_VENDOR_ID_OPTI
) && (pciPID
== 0xc861)) {
2592 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2594 } else if ((pciVID
== PCI_VENDOR_ID_NEC
) && (pciPID
== 0x0035))
2596 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2598 } else if ((pciVID
== PCI_VENDOR_ID_AL
) && (pciPID
== 0x5237)) {
2599 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2601 } else if ((pciVID
== PCI_VENDOR_ID_ATT
) && (pciPID
== 0x5802))
2603 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2605 } else if (pciVID
== PCI_VENDOR_ID_AMD
&& pciPID
== 0x740c) {
2606 devices
= ftdi_elan_found_controller(ftdi
, fn
,
2609 } else if (pciVID
== PCI_VENDOR_ID_COMPAQ
&& pciPID
== 0xa0f8) {
2610 devices
= ftdi_elan_found_controller(ftdi
, fn
,
2611 OHCI_QUIRK_ZFMICRO
);
2613 } else if (0 == pcidata
) {
2616 if (devices
> max_devices
) {
2617 max_devices
= devices
;
2618 ftdi
->function
= fn
+ 1;
2619 ftdi
->platform_data
.vendor
= pciVID
;
2620 ftdi
->platform_data
.device
= pciPID
;
2623 if (ftdi
->function
> 0) {
2624 return ftdi_elan_setup_controller(ftdi
, ftdi
->function
- 1);
2625 } else if (controllers
> 0) {
2627 } else if (unrecognized
> 0) {
2630 ftdi
->enumerated
= 0;
2637 * we use only the first bulk-in and bulk-out endpoints
2639 static int ftdi_elan_probe(struct usb_interface
*interface
,
2640 const struct usb_device_id
*id
)
2642 struct usb_host_interface
*iface_desc
;
2643 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
2645 struct usb_ftdi
*ftdi
;
2647 ftdi
= kzalloc(sizeof(struct usb_ftdi
), GFP_KERNEL
);
2651 mutex_lock(&ftdi_module_lock
);
2652 list_add_tail(&ftdi
->ftdi_list
, &ftdi_static_list
);
2653 ftdi
->sequence_num
= ++ftdi_instances
;
2654 mutex_unlock(&ftdi_module_lock
);
2655 ftdi_elan_init_kref(ftdi
);
2656 sema_init(&ftdi
->sw_lock
, 1);
2657 ftdi
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2658 ftdi
->interface
= interface
;
2659 mutex_init(&ftdi
->u132_lock
);
2662 iface_desc
= interface
->cur_altsetting
;
2663 retval
= usb_find_common_endpoints(iface_desc
,
2664 &bulk_in
, &bulk_out
, NULL
, NULL
);
2666 dev_err(&ftdi
->udev
->dev
, "Could not find both bulk-in and bulk-out endpoints\n");
2670 ftdi
->bulk_in_size
= usb_endpoint_maxp(bulk_in
);
2671 ftdi
->bulk_in_endpointAddr
= bulk_in
->bEndpointAddress
;
2672 ftdi
->bulk_in_buffer
= kmalloc(ftdi
->bulk_in_size
, GFP_KERNEL
);
2673 if (!ftdi
->bulk_in_buffer
) {
2678 ftdi
->bulk_out_endpointAddr
= bulk_out
->bEndpointAddress
;
2680 dev_info(&ftdi
->udev
->dev
, "interface %d has I=%02X O=%02X\n",
2681 iface_desc
->desc
.bInterfaceNumber
, ftdi
->bulk_in_endpointAddr
,
2682 ftdi
->bulk_out_endpointAddr
);
2683 usb_set_intfdata(interface
, ftdi
);
2684 if (iface_desc
->desc
.bInterfaceNumber
== 0 &&
2685 ftdi
->bulk_in_endpointAddr
== 0x81 &&
2686 ftdi
->bulk_out_endpointAddr
== 0x02) {
2687 retval
= usb_register_dev(interface
, &ftdi_elan_jtag_class
);
2689 dev_err(&ftdi
->udev
->dev
, "Not able to get a minor for this device\n");
2690 usb_set_intfdata(interface
, NULL
);
2694 ftdi
->class = &ftdi_elan_jtag_class
;
2695 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2696 ftdi
, iface_desc
->desc
.bInterfaceNumber
,
2700 } else if (iface_desc
->desc
.bInterfaceNumber
== 1 &&
2701 ftdi
->bulk_in_endpointAddr
== 0x83 &&
2702 ftdi
->bulk_out_endpointAddr
== 0x04) {
2704 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p ELAN interface %d now activated\n",
2705 ftdi
, iface_desc
->desc
.bInterfaceNumber
);
2706 INIT_DELAYED_WORK(&ftdi
->status_work
, ftdi_elan_status_work
);
2707 INIT_DELAYED_WORK(&ftdi
->command_work
, ftdi_elan_command_work
);
2708 INIT_DELAYED_WORK(&ftdi
->respond_work
, ftdi_elan_respond_work
);
2709 ftdi_status_queue_work(ftdi
, msecs_to_jiffies(3 *1000));
2712 dev_err(&ftdi
->udev
->dev
,
2713 "Could not find ELAN's U132 device\n");
2718 ftdi_elan_put_kref(ftdi
);
2723 static void ftdi_elan_disconnect(struct usb_interface
*interface
)
2725 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
2726 ftdi
->disconnected
+= 1;
2728 int minor
= interface
->minor
;
2729 struct usb_class_driver
*class = ftdi
->class;
2730 usb_set_intfdata(interface
, NULL
);
2731 usb_deregister_dev(interface
, class);
2732 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2735 ftdi_status_cancel_work(ftdi
);
2736 ftdi_command_cancel_work(ftdi
);
2737 ftdi_response_cancel_work(ftdi
);
2738 ftdi_elan_abandon_completions(ftdi
);
2739 ftdi_elan_abandon_targets(ftdi
);
2740 if (ftdi
->registered
) {
2741 platform_device_unregister(&ftdi
->platform_dev
);
2742 ftdi
->synchronized
= 0;
2743 ftdi
->enumerated
= 0;
2744 ftdi
->initialized
= 0;
2745 ftdi
->registered
= 0;
2747 ftdi
->disconnected
+= 1;
2748 usb_set_intfdata(interface
, NULL
);
2749 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 host controller interface now disconnected\n");
2751 ftdi_elan_put_kref(ftdi
);
2754 static struct usb_driver ftdi_elan_driver
= {
2755 .name
= "ftdi-elan",
2756 .probe
= ftdi_elan_probe
,
2757 .disconnect
= ftdi_elan_disconnect
,
2758 .id_table
= ftdi_elan_table
,
2760 static int __init
ftdi_elan_init(void)
2763 pr_info("driver %s\n", ftdi_elan_driver
.name
);
2764 mutex_init(&ftdi_module_lock
);
2765 INIT_LIST_HEAD(&ftdi_static_list
);
2766 result
= usb_register(&ftdi_elan_driver
);
2768 pr_err("usb_register failed. Error number %d\n", result
);
2774 static void __exit
ftdi_elan_exit(void)
2776 struct usb_ftdi
*ftdi
;
2777 struct usb_ftdi
*temp
;
2778 usb_deregister(&ftdi_elan_driver
);
2779 pr_info("ftdi_u132 driver deregistered\n");
2780 list_for_each_entry_safe(ftdi
, temp
, &ftdi_static_list
, ftdi_list
) {
2781 ftdi_status_cancel_work(ftdi
);
2782 ftdi_command_cancel_work(ftdi
);
2783 ftdi_response_cancel_work(ftdi
);
2788 module_init(ftdi_elan_init
);
2789 module_exit(ftdi_elan_exit
);