USB: use mutex instead of semaphore in the FTDI ELAN driver
[linux-2.6/kvm.git] / drivers / usb / misc / ftdi-elan.c
blob7cc6883a6fa3330f7b3e19bde4f2a9d5286a087f
1 /*
2 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
4 * Copyright(C) 2006 Elan Digital Systems Limited
5 * http://www.elandigitalsystems.com
7 * Author and Maintainer - Tony Olech - Elan Digital Systems
8 * tony.olech@elandigitalsystems.com
10 * This program is free software;you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
15 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
16 * based on various USB client drivers in the 2.6.15 linux kernel
17 * with constant reference to the 3rd Edition of Linux Device Drivers
18 * published by O'Reilly
20 * The U132 adapter is a USB to CardBus adapter specifically designed
21 * for PC cards that contain an OHCI host controller. Typical PC cards
22 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
24 * The U132 adapter will *NOT *work with PC cards that do not contain
25 * an OHCI controller. A simple way to test whether a PC card has an
26 * OHCI controller as an interface is to insert the PC card directly
27 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
28 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
29 * then there is a good chance that the U132 adapter will support the
30 * PC card.(you also need the specific client driver for the PC card)
32 * Please inform the Author and Maintainer about any PC cards that
33 * contain OHCI Host Controller and work when directly connected to
34 * an embedded CardBus slot but do not work when they are connected
35 * via an ELAN U132 adapter.
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <linux/ioctl.h>
43 #include <linux/pci_ids.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <linux/kref.h>
47 #include <linux/mutex.h>
48 #include <asm/uaccess.h>
49 #include <linux/usb.h>
50 #include <linux/workqueue.h>
51 #include <linux/platform_device.h>
52 MODULE_AUTHOR("Tony Olech");
53 MODULE_DESCRIPTION("FTDI ELAN driver");
54 MODULE_LICENSE("GPL");
55 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
56 static int distrust_firmware = 1;
57 module_param(distrust_firmware, bool, 0);
58 MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
59 "t setup");
60 extern struct platform_driver u132_platform_driver;
61 static struct workqueue_struct *status_queue;
62 static struct workqueue_struct *command_queue;
63 static struct workqueue_struct *respond_queue;
65 * ftdi_module_lock exists to protect access to global variables
68 static struct mutex ftdi_module_lock;
69 static int ftdi_instances = 0;
70 static struct list_head ftdi_static_list;
72 * end of the global variables protected by ftdi_module_lock
74 #include "usb_u132.h"
75 #include <asm/io.h>
76 #include "../core/hcd.h"
78 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
79 * If you're going to try stuff like this, you need to split
80 * out shareable stuff (register declarations?) into its own
81 * file, maybe name <linux/usb/ohci.h>
84 #include "../host/ohci.h"
85 /* Define these values to match your devices*/
86 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
87 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
88 /* table of devices that work with this driver*/
89 static struct usb_device_id ftdi_elan_table[] = {
90 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
91 { /* Terminating entry */ }
94 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
95 /* only the jtag(firmware upgrade device) interface requires
96 * a device file and corresponding minor number, but the
97 * interface is created unconditionally - I suppose it could
98 * be configured or not according to a module parameter.
99 * But since we(now) require one interface per device,
100 * and since it unlikely that a normal installation would
101 * require more than a couple of elan-ftdi devices, 8 seems
102 * like a reasonable limit to have here, and if someone
103 * really requires more than 8 devices, then they can frig the
104 * code and recompile
106 #define USB_FTDI_ELAN_MINOR_BASE 192
107 #define COMMAND_BITS 5
108 #define COMMAND_SIZE (1<<COMMAND_BITS)
109 #define COMMAND_MASK (COMMAND_SIZE-1)
110 struct u132_command {
111 u8 header;
112 u16 length;
113 u8 address;
114 u8 width;
115 u32 value;
116 int follows;
117 void *buffer;
119 #define RESPOND_BITS 5
120 #define RESPOND_SIZE (1<<RESPOND_BITS)
121 #define RESPOND_MASK (RESPOND_SIZE-1)
122 struct u132_respond {
123 u8 header;
124 u8 address;
125 u32 *value;
126 int *result;
127 struct completion wait_completion;
129 struct u132_target {
130 void *endp;
131 struct urb *urb;
132 int toggle_bits;
133 int error_count;
134 int condition_code;
135 int repeat_number;
136 int halted;
137 int skipped;
138 int actual;
139 int non_null;
140 int active;
141 int abandoning;
142 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
143 int toggle_bits, int error_count, int condition_code,
144 int repeat_number, int halted, int skipped, int actual,
145 int non_null);
147 /* Structure to hold all of our device specific stuff*/
148 struct usb_ftdi {
149 struct list_head ftdi_list;
150 struct semaphore u132_lock;
151 int command_next;
152 int command_head;
153 struct u132_command command[COMMAND_SIZE];
154 int respond_next;
155 int respond_head;
156 struct u132_respond respond[RESPOND_SIZE];
157 struct u132_target target[4];
158 char device_name[16];
159 unsigned synchronized:1;
160 unsigned enumerated:1;
161 unsigned registered:1;
162 unsigned initialized:1;
163 unsigned card_ejected:1;
164 int function;
165 int sequence_num;
166 int disconnected;
167 int gone_away;
168 int stuck_status;
169 int status_queue_delay;
170 struct semaphore sw_lock;
171 struct usb_device *udev;
172 struct usb_interface *interface;
173 struct usb_class_driver *class;
174 struct delayed_work status_work;
175 struct delayed_work command_work;
176 struct delayed_work respond_work;
177 struct u132_platform_data platform_data;
178 struct resource resources[0];
179 struct platform_device platform_dev;
180 unsigned char *bulk_in_buffer;
181 size_t bulk_in_size;
182 size_t bulk_in_last;
183 size_t bulk_in_left;
184 __u8 bulk_in_endpointAddr;
185 __u8 bulk_out_endpointAddr;
186 struct kref kref;
187 u32 controlreg;
188 u8 response[4 + 1024];
189 int expected;
190 int recieved;
191 int ed_found;
193 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
194 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
195 platform_dev)
196 static struct usb_driver ftdi_elan_driver;
197 static void ftdi_elan_delete(struct kref *kref)
199 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
200 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
201 usb_put_dev(ftdi->udev);
202 ftdi->disconnected += 1;
203 mutex_lock(&ftdi_module_lock);
204 list_del_init(&ftdi->ftdi_list);
205 ftdi_instances -= 1;
206 mutex_unlock(&ftdi_module_lock);
207 kfree(ftdi->bulk_in_buffer);
208 ftdi->bulk_in_buffer = NULL;
211 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
213 kref_put(&ftdi->kref, ftdi_elan_delete);
216 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
218 kref_get(&ftdi->kref);
221 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
223 kref_init(&ftdi->kref);
226 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
228 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
229 kref_put(&ftdi->kref, ftdi_elan_delete);
232 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
234 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
235 kref_get(&ftdi->kref);
238 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
240 if (cancel_delayed_work(&ftdi->status_work))
241 kref_put(&ftdi->kref, ftdi_elan_delete);
244 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
246 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
247 kref_put(&ftdi->kref, ftdi_elan_delete);
250 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
252 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
253 kref_get(&ftdi->kref);
256 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
258 if (cancel_delayed_work(&ftdi->command_work))
259 kref_put(&ftdi->kref, ftdi_elan_delete);
262 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
263 unsigned int delta)
265 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
266 kref_put(&ftdi->kref, ftdi_elan_delete);
269 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
271 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
272 kref_get(&ftdi->kref);
275 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
277 if (cancel_delayed_work(&ftdi->respond_work))
278 kref_put(&ftdi->kref, ftdi_elan_delete);
281 void ftdi_elan_gone_away(struct platform_device *pdev)
283 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
284 ftdi->gone_away += 1;
285 ftdi_elan_put_kref(ftdi);
289 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
290 static void ftdi_release_platform_dev(struct device *dev)
292 dev->parent = NULL;
295 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
296 struct u132_target *target, u8 *buffer, int length);
297 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
298 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
299 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
300 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
301 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
302 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
303 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
304 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
305 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
306 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
308 int result;
309 if (ftdi->platform_dev.dev.parent)
310 return -EBUSY;
311 ftdi_elan_get_kref(ftdi);
312 ftdi->platform_data.potpg = 100;
313 ftdi->platform_data.reset = NULL;
314 ftdi->platform_dev.id = ftdi->sequence_num;
315 ftdi->platform_dev.resource = ftdi->resources;
316 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
317 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
318 ftdi->platform_dev.dev.parent = NULL;
319 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
320 ftdi->platform_dev.dev.dma_mask = NULL;
321 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
322 ftdi->platform_dev.name = ftdi->device_name;
323 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
324 request_module("u132_hcd");
325 dev_info(&ftdi->udev->dev, "registering '%s'\n",
326 ftdi->platform_dev.name);
327 result = platform_device_register(&ftdi->platform_dev);
328 return result;
331 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
333 down(&ftdi->u132_lock);
334 while (ftdi->respond_next > ftdi->respond_head) {
335 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
336 ftdi->respond_head++];
337 *respond->result = -ESHUTDOWN;
338 *respond->value = 0;
339 complete(&respond->wait_completion);
340 } up(&ftdi->u132_lock);
343 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
345 int ed_number = 4;
346 down(&ftdi->u132_lock);
347 while (ed_number-- > 0) {
348 struct u132_target *target = &ftdi->target[ed_number];
349 if (target->active == 1) {
350 target->condition_code = TD_DEVNOTRESP;
351 up(&ftdi->u132_lock);
352 ftdi_elan_do_callback(ftdi, target, NULL, 0);
353 down(&ftdi->u132_lock);
356 ftdi->recieved = 0;
357 ftdi->expected = 4;
358 ftdi->ed_found = 0;
359 up(&ftdi->u132_lock);
362 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
364 int ed_number = 4;
365 down(&ftdi->u132_lock);
366 while (ed_number-- > 0) {
367 struct u132_target *target = &ftdi->target[ed_number];
368 target->abandoning = 1;
369 wait_1:if (target->active == 1) {
370 int command_size = ftdi->command_next -
371 ftdi->command_head;
372 if (command_size < COMMAND_SIZE) {
373 struct u132_command *command = &ftdi->command[
374 COMMAND_MASK & ftdi->command_next];
375 command->header = 0x80 | (ed_number << 5) | 0x4;
376 command->length = 0x00;
377 command->address = 0x00;
378 command->width = 0x00;
379 command->follows = 0;
380 command->value = 0;
381 command->buffer = &command->value;
382 ftdi->command_next += 1;
383 ftdi_elan_kick_command_queue(ftdi);
384 } else {
385 up(&ftdi->u132_lock);
386 msleep(100);
387 down(&ftdi->u132_lock);
388 goto wait_1;
391 wait_2:if (target->active == 1) {
392 int command_size = ftdi->command_next -
393 ftdi->command_head;
394 if (command_size < COMMAND_SIZE) {
395 struct u132_command *command = &ftdi->command[
396 COMMAND_MASK & ftdi->command_next];
397 command->header = 0x90 | (ed_number << 5);
398 command->length = 0x00;
399 command->address = 0x00;
400 command->width = 0x00;
401 command->follows = 0;
402 command->value = 0;
403 command->buffer = &command->value;
404 ftdi->command_next += 1;
405 ftdi_elan_kick_command_queue(ftdi);
406 } else {
407 up(&ftdi->u132_lock);
408 msleep(100);
409 down(&ftdi->u132_lock);
410 goto wait_2;
414 ftdi->recieved = 0;
415 ftdi->expected = 4;
416 ftdi->ed_found = 0;
417 up(&ftdi->u132_lock);
420 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
422 int ed_number = 4;
423 down(&ftdi->u132_lock);
424 while (ed_number-- > 0) {
425 struct u132_target *target = &ftdi->target[ed_number];
426 target->abandoning = 1;
427 wait:if (target->active == 1) {
428 int command_size = ftdi->command_next -
429 ftdi->command_head;
430 if (command_size < COMMAND_SIZE) {
431 struct u132_command *command = &ftdi->command[
432 COMMAND_MASK & ftdi->command_next];
433 command->header = 0x80 | (ed_number << 5) | 0x4;
434 command->length = 0x00;
435 command->address = 0x00;
436 command->width = 0x00;
437 command->follows = 0;
438 command->value = 0;
439 command->buffer = &command->value;
440 ftdi->command_next += 1;
441 ftdi_elan_kick_command_queue(ftdi);
442 } else {
443 up(&ftdi->u132_lock);
444 msleep(100);
445 down(&ftdi->u132_lock);
446 goto wait;
450 ftdi->recieved = 0;
451 ftdi->expected = 4;
452 ftdi->ed_found = 0;
453 up(&ftdi->u132_lock);
456 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
458 ftdi_command_queue_work(ftdi, 0);
459 return;
462 static void ftdi_elan_command_work(struct work_struct *work)
464 struct usb_ftdi *ftdi =
465 container_of(work, struct usb_ftdi, command_work.work);
467 if (ftdi->disconnected > 0) {
468 ftdi_elan_put_kref(ftdi);
469 return;
470 } else {
471 int retval = ftdi_elan_command_engine(ftdi);
472 if (retval == -ESHUTDOWN) {
473 ftdi->disconnected += 1;
474 } else if (retval == -ENODEV) {
475 ftdi->disconnected += 1;
476 } else if (retval)
477 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
478 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
479 return;
483 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
485 ftdi_respond_queue_work(ftdi, 0);
486 return;
489 static void ftdi_elan_respond_work(struct work_struct *work)
491 struct usb_ftdi *ftdi =
492 container_of(work, struct usb_ftdi, respond_work.work);
493 if (ftdi->disconnected > 0) {
494 ftdi_elan_put_kref(ftdi);
495 return;
496 } else {
497 int retval = ftdi_elan_respond_engine(ftdi);
498 if (retval == 0) {
499 } else if (retval == -ESHUTDOWN) {
500 ftdi->disconnected += 1;
501 } else if (retval == -ENODEV) {
502 ftdi->disconnected += 1;
503 } else if (retval == -EILSEQ) {
504 ftdi->disconnected += 1;
505 } else {
506 ftdi->disconnected += 1;
507 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
509 if (ftdi->disconnected > 0) {
510 ftdi_elan_abandon_completions(ftdi);
511 ftdi_elan_abandon_targets(ftdi);
513 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
514 return;
520 * the sw_lock is initially held and will be freed
521 * after the FTDI has been synchronized
524 static void ftdi_elan_status_work(struct work_struct *work)
526 struct usb_ftdi *ftdi =
527 container_of(work, struct usb_ftdi, status_work.work);
528 int work_delay_in_msec = 0;
529 if (ftdi->disconnected > 0) {
530 ftdi_elan_put_kref(ftdi);
531 return;
532 } else if (ftdi->synchronized == 0) {
533 down(&ftdi->sw_lock);
534 if (ftdi_elan_synchronize(ftdi) == 0) {
535 ftdi->synchronized = 1;
536 ftdi_command_queue_work(ftdi, 1);
537 ftdi_respond_queue_work(ftdi, 1);
538 up(&ftdi->sw_lock);
539 work_delay_in_msec = 100;
540 } else {
541 dev_err(&ftdi->udev->dev, "synchronize failed\n");
542 up(&ftdi->sw_lock);
543 work_delay_in_msec = 10 *1000;
545 } else if (ftdi->stuck_status > 0) {
546 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
547 ftdi->stuck_status = 0;
548 ftdi->synchronized = 0;
549 } else if ((ftdi->stuck_status++ % 60) == 1) {
550 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
551 "- please remove\n");
552 } else
553 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
554 "- checked %d times\n", ftdi->stuck_status);
555 work_delay_in_msec = 100;
556 } else if (ftdi->enumerated == 0) {
557 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
558 ftdi->enumerated = 1;
559 work_delay_in_msec = 250;
560 } else
561 work_delay_in_msec = 1000;
562 } else if (ftdi->initialized == 0) {
563 if (ftdi_elan_setupOHCI(ftdi) == 0) {
564 ftdi->initialized = 1;
565 work_delay_in_msec = 500;
566 } else {
567 dev_err(&ftdi->udev->dev, "initialized failed - trying "
568 "again in 10 seconds\n");
569 work_delay_in_msec = 1 *1000;
571 } else if (ftdi->registered == 0) {
572 work_delay_in_msec = 10;
573 if (ftdi_elan_hcd_init(ftdi) == 0) {
574 ftdi->registered = 1;
575 } else
576 dev_err(&ftdi->udev->dev, "register failed\n");
577 work_delay_in_msec = 250;
578 } else {
579 if (ftdi_elan_checkingPCI(ftdi) == 0) {
580 work_delay_in_msec = 250;
581 } else if (ftdi->controlreg & 0x00400000) {
582 if (ftdi->gone_away > 0) {
583 dev_err(&ftdi->udev->dev, "PCI device eject con"
584 "firmed platform_dev.dev.parent=%p plat"
585 "form_dev.dev=%p\n",
586 ftdi->platform_dev.dev.parent,
587 &ftdi->platform_dev.dev);
588 platform_device_unregister(&ftdi->platform_dev);
589 ftdi->platform_dev.dev.parent = NULL;
590 ftdi->registered = 0;
591 ftdi->enumerated = 0;
592 ftdi->card_ejected = 0;
593 ftdi->initialized = 0;
594 ftdi->gone_away = 0;
595 } else
596 ftdi_elan_flush_targets(ftdi);
597 work_delay_in_msec = 250;
598 } else {
599 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
601 ftdi_elan_cancel_targets(ftdi);
602 work_delay_in_msec = 500;
603 ftdi->enumerated = 0;
604 ftdi->initialized = 0;
607 if (ftdi->disconnected > 0) {
608 ftdi_elan_put_kref(ftdi);
609 return;
610 } else {
611 ftdi_status_requeue_work(ftdi,
612 msecs_to_jiffies(work_delay_in_msec));
613 return;
619 * file_operations for the jtag interface
621 * the usage count for the device is incremented on open()
622 * and decremented on release()
624 static int ftdi_elan_open(struct inode *inode, struct file *file)
626 int subminor = iminor(inode);
627 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
628 subminor);
629 if (!interface) {
630 printk(KERN_ERR "can't find device for minor %d\n", subminor);
631 return -ENODEV;
632 } else {
633 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
634 if (!ftdi) {
635 return -ENODEV;
636 } else {
637 if (down_interruptible(&ftdi->sw_lock)) {
638 return -EINTR;
639 } else {
640 ftdi_elan_get_kref(ftdi);
641 file->private_data = ftdi;
642 return 0;
648 static int ftdi_elan_release(struct inode *inode, struct file *file)
650 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
651 if (ftdi == NULL)
652 return -ENODEV;
653 up(&ftdi->sw_lock); /* decrement the count on our device */
654 ftdi_elan_put_kref(ftdi);
655 return 0;
659 #define FTDI_ELAN_IOC_MAGIC 0xA1
660 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
661 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
662 unsigned int cmd, unsigned long arg)
664 switch (cmd) {
665 case FTDI_ELAN_IOCDEBUG:{
666 char line[132];
667 int size = strncpy_from_user(line,
668 (const char __user *)arg, sizeof(line));
669 if (size < 0) {
670 return -EINVAL;
671 } else {
672 printk(KERN_ERR "TODO: ioctl %s\n", line);
673 return 0;
676 default:
677 return -EFAULT;
684 * blocking bulk reads are used to get data from the device
687 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
688 size_t count, loff_t *ppos)
690 char data[30 *3 + 4];
691 char *d = data;
692 int m = (sizeof(data) - 1) / 3;
693 int bytes_read = 0;
694 int retry_on_empty = 10;
695 int retry_on_timeout = 5;
696 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
697 if (ftdi->disconnected > 0) {
698 return -ENODEV;
700 data[0] = 0;
701 have:if (ftdi->bulk_in_left > 0) {
702 if (count-- > 0) {
703 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
704 ftdi->bulk_in_left -= 1;
705 if (bytes_read < m) {
706 d += sprintf(d, " %02X", 0x000000FF & *p);
707 } else if (bytes_read > m) {
708 } else
709 d += sprintf(d, " ..");
710 if (copy_to_user(buffer++, p, 1)) {
711 return -EFAULT;
712 } else {
713 bytes_read += 1;
714 goto have;
716 } else
717 return bytes_read;
719 more:if (count > 0) {
720 int packet_bytes = 0;
721 int retval = usb_bulk_msg(ftdi->udev,
722 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
723 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
724 &packet_bytes, msecs_to_jiffies(50));
725 if (packet_bytes > 2) {
726 ftdi->bulk_in_left = packet_bytes - 2;
727 ftdi->bulk_in_last = 1;
728 goto have;
729 } else if (retval == -ETIMEDOUT) {
730 if (retry_on_timeout-- > 0) {
731 goto more;
732 } else if (bytes_read > 0) {
733 return bytes_read;
734 } else
735 return retval;
736 } else if (retval == 0) {
737 if (retry_on_empty-- > 0) {
738 goto more;
739 } else
740 return bytes_read;
741 } else
742 return retval;
743 } else
744 return bytes_read;
747 static void ftdi_elan_write_bulk_callback(struct urb *urb)
749 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
750 if (urb->status && !(urb->status == -ENOENT || urb->status ==
751 -ECONNRESET || urb->status == -ESHUTDOWN)) {
752 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
753 "d\n", urb, urb->status);
755 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
756 urb->transfer_buffer, urb->transfer_dma);
759 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
760 char *buf, int command_size, int total_size)
762 int ed_commands = 0;
763 int b = 0;
764 int I = command_size;
765 int i = ftdi->command_head;
766 while (I-- > 0) {
767 struct u132_command *command = &ftdi->command[COMMAND_MASK &
768 i++];
769 int F = command->follows;
770 u8 *f = command->buffer;
771 if (command->header & 0x80) {
772 ed_commands |= 1 << (0x3 & (command->header >> 5));
774 buf[b++] = command->header;
775 buf[b++] = (command->length >> 0) & 0x00FF;
776 buf[b++] = (command->length >> 8) & 0x00FF;
777 buf[b++] = command->address;
778 buf[b++] = command->width;
779 while (F-- > 0) {
780 buf[b++] = *f++;
783 return ed_commands;
786 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
788 int total_size = 0;
789 int I = command_size;
790 int i = ftdi->command_head;
791 while (I-- > 0) {
792 struct u132_command *command = &ftdi->command[COMMAND_MASK &
793 i++];
794 total_size += 5 + command->follows;
795 } return total_size;
798 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
800 int retval;
801 char *buf;
802 int ed_commands;
803 int total_size;
804 struct urb *urb;
805 int command_size = ftdi->command_next - ftdi->command_head;
806 if (command_size == 0)
807 return 0;
808 total_size = ftdi_elan_total_command_size(ftdi, command_size);
809 urb = usb_alloc_urb(0, GFP_KERNEL);
810 if (!urb) {
811 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
812 "ands totaling %d bytes to the Uxxx\n", command_size,
813 total_size);
814 return -ENOMEM;
816 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
817 &urb->transfer_dma);
818 if (!buf) {
819 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
820 "ommands totaling %d bytes to the Uxxx\n", command_size,
821 total_size);
822 usb_free_urb(urb);
823 return -ENOMEM;
825 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
826 command_size, total_size);
827 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
828 ftdi->bulk_out_endpointAddr), buf, total_size,
829 ftdi_elan_write_bulk_callback, ftdi);
830 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
831 if (ed_commands) {
832 char diag[40 *3 + 4];
833 char *d = diag;
834 int m = total_size;
835 u8 *c = buf;
836 int s = (sizeof(diag) - 1) / 3;
837 diag[0] = 0;
838 while (s-- > 0 && m-- > 0) {
839 if (s > 0 || m == 0) {
840 d += sprintf(d, " %02X", *c++);
841 } else
842 d += sprintf(d, " ..");
845 retval = usb_submit_urb(urb, GFP_KERNEL);
846 if (retval) {
847 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
848 "%d commands totaling %d bytes to the Uxxx\n", retval,
849 urb, command_size, total_size);
850 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
851 usb_free_urb(urb);
852 return retval;
854 usb_free_urb(urb); /* release our reference to this urb,
855 the USB core will eventually free it entirely */
856 ftdi->command_head += command_size;
857 ftdi_elan_kick_respond_queue(ftdi);
858 return 0;
861 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
862 struct u132_target *target, u8 *buffer, int length)
864 struct urb *urb = target->urb;
865 int halted = target->halted;
866 int skipped = target->skipped;
867 int actual = target->actual;
868 int non_null = target->non_null;
869 int toggle_bits = target->toggle_bits;
870 int error_count = target->error_count;
871 int condition_code = target->condition_code;
872 int repeat_number = target->repeat_number;
873 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
874 int, int, int, int) = target->callback;
875 target->active -= 1;
876 target->callback = NULL;
877 (*callback) (target->endp, urb, buffer, length, toggle_bits,
878 error_count, condition_code, repeat_number, halted, skipped,
879 actual, non_null);
882 static char *have_ed_set_response(struct usb_ftdi *ftdi,
883 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
884 char *b)
886 int payload = (ed_length >> 0) & 0x07FF;
887 down(&ftdi->u132_lock);
888 target->actual = 0;
889 target->non_null = (ed_length >> 15) & 0x0001;
890 target->repeat_number = (ed_length >> 11) & 0x000F;
891 if (ed_type == 0x02) {
892 if (payload == 0 || target->abandoning > 0) {
893 target->abandoning = 0;
894 up(&ftdi->u132_lock);
895 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
896 payload);
897 ftdi->recieved = 0;
898 ftdi->expected = 4;
899 ftdi->ed_found = 0;
900 return ftdi->response;
901 } else {
902 ftdi->expected = 4 + payload;
903 ftdi->ed_found = 1;
904 up(&ftdi->u132_lock);
905 return b;
907 } else if (ed_type == 0x03) {
908 if (payload == 0 || target->abandoning > 0) {
909 target->abandoning = 0;
910 up(&ftdi->u132_lock);
911 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
912 payload);
913 ftdi->recieved = 0;
914 ftdi->expected = 4;
915 ftdi->ed_found = 0;
916 return ftdi->response;
917 } else {
918 ftdi->expected = 4 + payload;
919 ftdi->ed_found = 1;
920 up(&ftdi->u132_lock);
921 return b;
923 } else if (ed_type == 0x01) {
924 target->abandoning = 0;
925 up(&ftdi->u132_lock);
926 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
927 payload);
928 ftdi->recieved = 0;
929 ftdi->expected = 4;
930 ftdi->ed_found = 0;
931 return ftdi->response;
932 } else {
933 target->abandoning = 0;
934 up(&ftdi->u132_lock);
935 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
936 payload);
937 ftdi->recieved = 0;
938 ftdi->expected = 4;
939 ftdi->ed_found = 0;
940 return ftdi->response;
944 static char *have_ed_get_response(struct usb_ftdi *ftdi,
945 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
946 char *b)
948 down(&ftdi->u132_lock);
949 target->condition_code = TD_DEVNOTRESP;
950 target->actual = (ed_length >> 0) & 0x01FF;
951 target->non_null = (ed_length >> 15) & 0x0001;
952 target->repeat_number = (ed_length >> 11) & 0x000F;
953 up(&ftdi->u132_lock);
954 if (target->active)
955 ftdi_elan_do_callback(ftdi, target, NULL, 0);
956 target->abandoning = 0;
957 ftdi->recieved = 0;
958 ftdi->expected = 4;
959 ftdi->ed_found = 0;
960 return ftdi->response;
965 * The engine tries to empty the FTDI fifo
967 * all responses found in the fifo data are dispatched thus
968 * the response buffer can only ever hold a maximum sized
969 * response from the Uxxx.
972 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
974 u8 *b = ftdi->response + ftdi->recieved;
975 int bytes_read = 0;
976 int retry_on_empty = 1;
977 int retry_on_timeout = 3;
978 int empty_packets = 0;
979 read:{
980 int packet_bytes = 0;
981 int retval = usb_bulk_msg(ftdi->udev,
982 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
983 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
984 &packet_bytes, msecs_to_jiffies(500));
985 char diag[30 *3 + 4];
986 char *d = diag;
987 int m = packet_bytes;
988 u8 *c = ftdi->bulk_in_buffer;
989 int s = (sizeof(diag) - 1) / 3;
990 diag[0] = 0;
991 while (s-- > 0 && m-- > 0) {
992 if (s > 0 || m == 0) {
993 d += sprintf(d, " %02X", *c++);
994 } else
995 d += sprintf(d, " ..");
997 if (packet_bytes > 2) {
998 ftdi->bulk_in_left = packet_bytes - 2;
999 ftdi->bulk_in_last = 1;
1000 goto have;
1001 } else if (retval == -ETIMEDOUT) {
1002 if (retry_on_timeout-- > 0) {
1003 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1004 "t_bytes = %d with total %d bytes%s\n",
1005 packet_bytes, bytes_read, diag);
1006 goto more;
1007 } else if (bytes_read > 0) {
1008 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
1009 bytes_read, diag);
1010 return -ENOMEM;
1011 } else {
1012 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1013 "t_bytes = %d with total %d bytes%s\n",
1014 packet_bytes, bytes_read, diag);
1015 return -ENOMEM;
1017 } else if (retval == -EILSEQ) {
1018 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1019 " = %d with total %d bytes%s\n", retval,
1020 packet_bytes, bytes_read, diag);
1021 return retval;
1022 } else if (retval) {
1023 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1024 " = %d with total %d bytes%s\n", retval,
1025 packet_bytes, bytes_read, diag);
1026 return retval;
1027 } else if (packet_bytes == 2) {
1028 unsigned char s0 = ftdi->bulk_in_buffer[0];
1029 unsigned char s1 = ftdi->bulk_in_buffer[1];
1030 empty_packets += 1;
1031 if (s0 == 0x31 && s1 == 0x60) {
1032 if (retry_on_empty-- > 0) {
1033 goto more;
1034 } else
1035 return 0;
1036 } else if (s0 == 0x31 && s1 == 0x00) {
1037 if (retry_on_empty-- > 0) {
1038 goto more;
1039 } else
1040 return 0;
1041 } else {
1042 if (retry_on_empty-- > 0) {
1043 goto more;
1044 } else
1045 return 0;
1047 } else if (packet_bytes == 1) {
1048 if (retry_on_empty-- > 0) {
1049 goto more;
1050 } else
1051 return 0;
1052 } else {
1053 if (retry_on_empty-- > 0) {
1054 goto more;
1055 } else
1056 return 0;
1059 more:{
1060 goto read;
1062 have:if (ftdi->bulk_in_left > 0) {
1063 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1064 bytes_read += 1;
1065 ftdi->bulk_in_left -= 1;
1066 if (ftdi->recieved == 0 && c == 0xFF) {
1067 goto have;
1068 } else
1069 *b++ = c;
1070 if (++ftdi->recieved < ftdi->expected) {
1071 goto have;
1072 } else if (ftdi->ed_found) {
1073 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1074 u16 ed_length = (ftdi->response[2] << 8) |
1075 ftdi->response[1];
1076 struct u132_target *target = &ftdi->target[ed_number];
1077 int payload = (ed_length >> 0) & 0x07FF;
1078 char diag[30 *3 + 4];
1079 char *d = diag;
1080 int m = payload;
1081 u8 *c = 4 + ftdi->response;
1082 int s = (sizeof(diag) - 1) / 3;
1083 diag[0] = 0;
1084 while (s-- > 0 && m-- > 0) {
1085 if (s > 0 || m == 0) {
1086 d += sprintf(d, " %02X", *c++);
1087 } else
1088 d += sprintf(d, " ..");
1090 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1091 payload);
1092 ftdi->recieved = 0;
1093 ftdi->expected = 4;
1094 ftdi->ed_found = 0;
1095 b = ftdi->response;
1096 goto have;
1097 } else if (ftdi->expected == 8) {
1098 u8 buscmd;
1099 int respond_head = ftdi->respond_head++;
1100 struct u132_respond *respond = &ftdi->respond[
1101 RESPOND_MASK & respond_head];
1102 u32 data = ftdi->response[7];
1103 data <<= 8;
1104 data |= ftdi->response[6];
1105 data <<= 8;
1106 data |= ftdi->response[5];
1107 data <<= 8;
1108 data |= ftdi->response[4];
1109 *respond->value = data;
1110 *respond->result = 0;
1111 complete(&respond->wait_completion);
1112 ftdi->recieved = 0;
1113 ftdi->expected = 4;
1114 ftdi->ed_found = 0;
1115 b = ftdi->response;
1116 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1117 if (buscmd == 0x00) {
1118 } else if (buscmd == 0x02) {
1119 } else if (buscmd == 0x06) {
1120 } else if (buscmd == 0x0A) {
1121 } else
1122 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1123 "lue = %08X\n", buscmd, data);
1124 goto have;
1125 } else {
1126 if ((ftdi->response[0] & 0x80) == 0x00) {
1127 ftdi->expected = 8;
1128 goto have;
1129 } else {
1130 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1131 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1132 u16 ed_length = (ftdi->response[2] << 8) |
1133 ftdi->response[1];
1134 struct u132_target *target = &ftdi->target[
1135 ed_number];
1136 target->halted = (ftdi->response[0] >> 3) &
1137 0x01;
1138 target->skipped = (ftdi->response[0] >> 2) &
1139 0x01;
1140 target->toggle_bits = (ftdi->response[3] >> 6)
1141 & 0x03;
1142 target->error_count = (ftdi->response[3] >> 4)
1143 & 0x03;
1144 target->condition_code = (ftdi->response[
1145 3] >> 0) & 0x0F;
1146 if ((ftdi->response[0] & 0x10) == 0x00) {
1147 b = have_ed_set_response(ftdi, target,
1148 ed_length, ed_number, ed_type,
1150 goto have;
1151 } else {
1152 b = have_ed_get_response(ftdi, target,
1153 ed_length, ed_number, ed_type,
1155 goto have;
1159 } else
1160 goto more;
1165 * create a urb, and a buffer for it, and copy the data to the urb
1168 static ssize_t ftdi_elan_write(struct file *file,
1169 const char __user *user_buffer, size_t count,
1170 loff_t *ppos)
1172 int retval = 0;
1173 struct urb *urb;
1174 char *buf;
1175 struct usb_ftdi *ftdi = file->private_data;
1177 if (ftdi->disconnected > 0) {
1178 return -ENODEV;
1180 if (count == 0) {
1181 goto exit;
1183 urb = usb_alloc_urb(0, GFP_KERNEL);
1184 if (!urb) {
1185 retval = -ENOMEM;
1186 goto error_1;
1188 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1189 &urb->transfer_dma);
1190 if (!buf) {
1191 retval = -ENOMEM;
1192 goto error_2;
1194 if (copy_from_user(buf, user_buffer, count)) {
1195 retval = -EFAULT;
1196 goto error_3;
1198 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1199 ftdi->bulk_out_endpointAddr), buf, count,
1200 ftdi_elan_write_bulk_callback, ftdi);
1201 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1202 retval = usb_submit_urb(urb, GFP_KERNEL);
1203 if (retval) {
1204 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1205 "d\n", retval);
1206 goto error_3;
1208 usb_free_urb(urb);
1210 exit:
1211 return count;
1212 error_3:
1213 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1214 error_2:
1215 usb_free_urb(urb);
1216 error_1:
1217 return retval;
1220 static const struct file_operations ftdi_elan_fops = {
1221 .owner = THIS_MODULE,
1222 .llseek = no_llseek,
1223 .ioctl = ftdi_elan_ioctl,
1224 .read = ftdi_elan_read,
1225 .write = ftdi_elan_write,
1226 .open = ftdi_elan_open,
1227 .release = ftdi_elan_release,
1231 * usb class driver info in order to get a minor number from the usb core,
1232 * and to have the device registered with the driver core
1234 static struct usb_class_driver ftdi_elan_jtag_class = {
1235 .name = "ftdi-%d-jtag",
1236 .fops = &ftdi_elan_fops,
1237 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1241 * the following definitions are for the
1242 * ELAN FPGA state machgine processor that
1243 * lies on the other side of the FTDI chip
1245 #define cPCIu132rd 0x0
1246 #define cPCIu132wr 0x1
1247 #define cPCIiord 0x2
1248 #define cPCIiowr 0x3
1249 #define cPCImemrd 0x6
1250 #define cPCImemwr 0x7
1251 #define cPCIcfgrd 0xA
1252 #define cPCIcfgwr 0xB
1253 #define cPCInull 0xF
1254 #define cU132cmd_status 0x0
1255 #define cU132flash 0x1
1256 #define cPIDsetup 0x0
1257 #define cPIDout 0x1
1258 #define cPIDin 0x2
1259 #define cPIDinonce 0x3
1260 #define cCCnoerror 0x0
1261 #define cCCcrc 0x1
1262 #define cCCbitstuff 0x2
1263 #define cCCtoggle 0x3
1264 #define cCCstall 0x4
1265 #define cCCnoresp 0x5
1266 #define cCCbadpid1 0x6
1267 #define cCCbadpid2 0x7
1268 #define cCCdataoverrun 0x8
1269 #define cCCdataunderrun 0x9
1270 #define cCCbuffoverrun 0xC
1271 #define cCCbuffunderrun 0xD
1272 #define cCCnotaccessed 0xF
1273 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1275 wait:if (ftdi->disconnected > 0) {
1276 return -ENODEV;
1277 } else {
1278 int command_size;
1279 down(&ftdi->u132_lock);
1280 command_size = ftdi->command_next - ftdi->command_head;
1281 if (command_size < COMMAND_SIZE) {
1282 struct u132_command *command = &ftdi->command[
1283 COMMAND_MASK & ftdi->command_next];
1284 command->header = 0x00 | cPCIu132wr;
1285 command->length = 0x04;
1286 command->address = 0x00;
1287 command->width = 0x00;
1288 command->follows = 4;
1289 command->value = data;
1290 command->buffer = &command->value;
1291 ftdi->command_next += 1;
1292 ftdi_elan_kick_command_queue(ftdi);
1293 up(&ftdi->u132_lock);
1294 return 0;
1295 } else {
1296 up(&ftdi->u132_lock);
1297 msleep(100);
1298 goto wait;
1303 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1304 u8 width, u32 data)
1306 u8 addressofs = config_offset / 4;
1307 wait:if (ftdi->disconnected > 0) {
1308 return -ENODEV;
1309 } else {
1310 int command_size;
1311 down(&ftdi->u132_lock);
1312 command_size = ftdi->command_next - ftdi->command_head;
1313 if (command_size < COMMAND_SIZE) {
1314 struct u132_command *command = &ftdi->command[
1315 COMMAND_MASK & ftdi->command_next];
1316 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1317 command->length = 0x04;
1318 command->address = addressofs;
1319 command->width = 0x00 | (width & 0x0F);
1320 command->follows = 4;
1321 command->value = data;
1322 command->buffer = &command->value;
1323 ftdi->command_next += 1;
1324 ftdi_elan_kick_command_queue(ftdi);
1325 up(&ftdi->u132_lock);
1326 return 0;
1327 } else {
1328 up(&ftdi->u132_lock);
1329 msleep(100);
1330 goto wait;
1335 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1336 u8 width, u32 data)
1338 u8 addressofs = mem_offset / 4;
1339 wait:if (ftdi->disconnected > 0) {
1340 return -ENODEV;
1341 } else {
1342 int command_size;
1343 down(&ftdi->u132_lock);
1344 command_size = ftdi->command_next - ftdi->command_head;
1345 if (command_size < COMMAND_SIZE) {
1346 struct u132_command *command = &ftdi->command[
1347 COMMAND_MASK & ftdi->command_next];
1348 command->header = 0x00 | (cPCImemwr & 0x0F);
1349 command->length = 0x04;
1350 command->address = addressofs;
1351 command->width = 0x00 | (width & 0x0F);
1352 command->follows = 4;
1353 command->value = data;
1354 command->buffer = &command->value;
1355 ftdi->command_next += 1;
1356 ftdi_elan_kick_command_queue(ftdi);
1357 up(&ftdi->u132_lock);
1358 return 0;
1359 } else {
1360 up(&ftdi->u132_lock);
1361 msleep(100);
1362 goto wait;
1367 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1368 u8 width, u32 data)
1370 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1371 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1375 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1376 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1378 wait:if (ftdi->disconnected > 0) {
1379 return -ENODEV;
1380 } else {
1381 int command_size;
1382 int respond_size;
1383 down(&ftdi->u132_lock);
1384 command_size = ftdi->command_next - ftdi->command_head;
1385 respond_size = ftdi->respond_next - ftdi->respond_head;
1386 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1388 struct u132_command *command = &ftdi->command[
1389 COMMAND_MASK & ftdi->command_next];
1390 struct u132_respond *respond = &ftdi->respond[
1391 RESPOND_MASK & ftdi->respond_next];
1392 int result = -ENODEV;
1393 respond->result = &result;
1394 respond->header = command->header = 0x00 | cPCIu132rd;
1395 command->length = 0x04;
1396 respond->address = command->address = cU132cmd_status;
1397 command->width = 0x00;
1398 command->follows = 0;
1399 command->value = 0;
1400 command->buffer = NULL;
1401 respond->value = data;
1402 init_completion(&respond->wait_completion);
1403 ftdi->command_next += 1;
1404 ftdi->respond_next += 1;
1405 ftdi_elan_kick_command_queue(ftdi);
1406 up(&ftdi->u132_lock);
1407 wait_for_completion(&respond->wait_completion);
1408 return result;
1409 } else {
1410 up(&ftdi->u132_lock);
1411 msleep(100);
1412 goto wait;
1417 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1418 u8 width, u32 *data)
1420 u8 addressofs = config_offset / 4;
1421 wait:if (ftdi->disconnected > 0) {
1422 return -ENODEV;
1423 } else {
1424 int command_size;
1425 int respond_size;
1426 down(&ftdi->u132_lock);
1427 command_size = ftdi->command_next - ftdi->command_head;
1428 respond_size = ftdi->respond_next - ftdi->respond_head;
1429 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1431 struct u132_command *command = &ftdi->command[
1432 COMMAND_MASK & ftdi->command_next];
1433 struct u132_respond *respond = &ftdi->respond[
1434 RESPOND_MASK & ftdi->respond_next];
1435 int result = -ENODEV;
1436 respond->result = &result;
1437 respond->header = command->header = 0x00 | (cPCIcfgrd &
1438 0x0F);
1439 command->length = 0x04;
1440 respond->address = command->address = addressofs;
1441 command->width = 0x00 | (width & 0x0F);
1442 command->follows = 0;
1443 command->value = 0;
1444 command->buffer = NULL;
1445 respond->value = data;
1446 init_completion(&respond->wait_completion);
1447 ftdi->command_next += 1;
1448 ftdi->respond_next += 1;
1449 ftdi_elan_kick_command_queue(ftdi);
1450 up(&ftdi->u132_lock);
1451 wait_for_completion(&respond->wait_completion);
1452 return result;
1453 } else {
1454 up(&ftdi->u132_lock);
1455 msleep(100);
1456 goto wait;
1461 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1462 u8 width, u32 *data)
1464 u8 addressofs = mem_offset / 4;
1465 wait:if (ftdi->disconnected > 0) {
1466 return -ENODEV;
1467 } else {
1468 int command_size;
1469 int respond_size;
1470 down(&ftdi->u132_lock);
1471 command_size = ftdi->command_next - ftdi->command_head;
1472 respond_size = ftdi->respond_next - ftdi->respond_head;
1473 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1475 struct u132_command *command = &ftdi->command[
1476 COMMAND_MASK & ftdi->command_next];
1477 struct u132_respond *respond = &ftdi->respond[
1478 RESPOND_MASK & ftdi->respond_next];
1479 int result = -ENODEV;
1480 respond->result = &result;
1481 respond->header = command->header = 0x00 | (cPCImemrd &
1482 0x0F);
1483 command->length = 0x04;
1484 respond->address = command->address = addressofs;
1485 command->width = 0x00 | (width & 0x0F);
1486 command->follows = 0;
1487 command->value = 0;
1488 command->buffer = NULL;
1489 respond->value = data;
1490 init_completion(&respond->wait_completion);
1491 ftdi->command_next += 1;
1492 ftdi->respond_next += 1;
1493 ftdi_elan_kick_command_queue(ftdi);
1494 up(&ftdi->u132_lock);
1495 wait_for_completion(&respond->wait_completion);
1496 return result;
1497 } else {
1498 up(&ftdi->u132_lock);
1499 msleep(100);
1500 goto wait;
1505 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1506 u8 width, u32 *data)
1508 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1509 if (ftdi->initialized == 0) {
1510 return -ENODEV;
1511 } else
1512 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1516 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1517 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1518 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1519 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1520 int toggle_bits, int error_count, int condition_code, int repeat_number,
1521 int halted, int skipped, int actual, int non_null))
1523 u8 ed = ed_number - 1;
1524 wait:if (ftdi->disconnected > 0) {
1525 return -ENODEV;
1526 } else if (ftdi->initialized == 0) {
1527 return -ENODEV;
1528 } else {
1529 int command_size;
1530 down(&ftdi->u132_lock);
1531 command_size = ftdi->command_next - ftdi->command_head;
1532 if (command_size < COMMAND_SIZE) {
1533 struct u132_target *target = &ftdi->target[ed];
1534 struct u132_command *command = &ftdi->command[
1535 COMMAND_MASK & ftdi->command_next];
1536 command->header = 0x80 | (ed << 5);
1537 command->length = 0x8007;
1538 command->address = (toggle_bits << 6) | (ep_number << 2)
1539 | (address << 0);
1540 command->width = usb_maxpacket(urb->dev, urb->pipe,
1541 usb_pipeout(urb->pipe));
1542 command->follows = 8;
1543 command->value = 0;
1544 command->buffer = urb->setup_packet;
1545 target->callback = callback;
1546 target->endp = endp;
1547 target->urb = urb;
1548 target->active = 1;
1549 ftdi->command_next += 1;
1550 ftdi_elan_kick_command_queue(ftdi);
1551 up(&ftdi->u132_lock);
1552 return 0;
1553 } else {
1554 up(&ftdi->u132_lock);
1555 msleep(100);
1556 goto wait;
1561 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1562 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1563 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1564 int toggle_bits, int error_count, int condition_code, int repeat_number,
1565 int halted, int skipped, int actual, int non_null))
1567 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1568 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1569 ep_number, toggle_bits, callback);
1573 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1574 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1575 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1576 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1577 int toggle_bits, int error_count, int condition_code, int repeat_number,
1578 int halted, int skipped, int actual, int non_null))
1580 u8 ed = ed_number - 1;
1581 wait:if (ftdi->disconnected > 0) {
1582 return -ENODEV;
1583 } else if (ftdi->initialized == 0) {
1584 return -ENODEV;
1585 } else {
1586 int command_size;
1587 down(&ftdi->u132_lock);
1588 command_size = ftdi->command_next - ftdi->command_head;
1589 if (command_size < COMMAND_SIZE) {
1590 struct u132_target *target = &ftdi->target[ed];
1591 struct u132_command *command = &ftdi->command[
1592 COMMAND_MASK & ftdi->command_next];
1593 int remaining_length = urb->transfer_buffer_length -
1594 urb->actual_length;
1595 command->header = 0x82 | (ed << 5);
1596 if (remaining_length == 0) {
1597 command->length = 0x0000;
1598 } else if (remaining_length > 1024) {
1599 command->length = 0x8000 | 1023;
1600 } else
1601 command->length = 0x8000 | (remaining_length -
1603 command->address = (toggle_bits << 6) | (ep_number << 2)
1604 | (address << 0);
1605 command->width = usb_maxpacket(urb->dev, urb->pipe,
1606 usb_pipeout(urb->pipe));
1607 command->follows = 0;
1608 command->value = 0;
1609 command->buffer = NULL;
1610 target->callback = callback;
1611 target->endp = endp;
1612 target->urb = urb;
1613 target->active = 1;
1614 ftdi->command_next += 1;
1615 ftdi_elan_kick_command_queue(ftdi);
1616 up(&ftdi->u132_lock);
1617 return 0;
1618 } else {
1619 up(&ftdi->u132_lock);
1620 msleep(100);
1621 goto wait;
1626 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1627 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1628 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1629 int toggle_bits, int error_count, int condition_code, int repeat_number,
1630 int halted, int skipped, int actual, int non_null))
1632 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1633 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1634 ep_number, toggle_bits, callback);
1638 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1639 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1640 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1641 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1642 int toggle_bits, int error_count, int condition_code, int repeat_number,
1643 int halted, int skipped, int actual, int non_null))
1645 u8 ed = ed_number - 1;
1646 wait:if (ftdi->disconnected > 0) {
1647 return -ENODEV;
1648 } else if (ftdi->initialized == 0) {
1649 return -ENODEV;
1650 } else {
1651 int command_size;
1652 down(&ftdi->u132_lock);
1653 command_size = ftdi->command_next - ftdi->command_head;
1654 if (command_size < COMMAND_SIZE) {
1655 struct u132_target *target = &ftdi->target[ed];
1656 struct u132_command *command = &ftdi->command[
1657 COMMAND_MASK & ftdi->command_next];
1658 command->header = 0x81 | (ed << 5);
1659 command->length = 0x0000;
1660 command->address = (toggle_bits << 6) | (ep_number << 2)
1661 | (address << 0);
1662 command->width = usb_maxpacket(urb->dev, urb->pipe,
1663 usb_pipeout(urb->pipe));
1664 command->follows = 0;
1665 command->value = 0;
1666 command->buffer = NULL;
1667 target->callback = callback;
1668 target->endp = endp;
1669 target->urb = urb;
1670 target->active = 1;
1671 ftdi->command_next += 1;
1672 ftdi_elan_kick_command_queue(ftdi);
1673 up(&ftdi->u132_lock);
1674 return 0;
1675 } else {
1676 up(&ftdi->u132_lock);
1677 msleep(100);
1678 goto wait;
1683 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1684 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1685 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1686 int toggle_bits, int error_count, int condition_code, int repeat_number,
1687 int halted, int skipped, int actual, int non_null))
1689 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1690 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1691 ep_number, toggle_bits, callback);
1695 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1696 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1697 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1698 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1699 int toggle_bits, int error_count, int condition_code, int repeat_number,
1700 int halted, int skipped, int actual, int non_null))
1702 u8 ed = ed_number - 1;
1703 wait:if (ftdi->disconnected > 0) {
1704 return -ENODEV;
1705 } else if (ftdi->initialized == 0) {
1706 return -ENODEV;
1707 } else {
1708 int command_size;
1709 down(&ftdi->u132_lock);
1710 command_size = ftdi->command_next - ftdi->command_head;
1711 if (command_size < COMMAND_SIZE) {
1712 u8 *b;
1713 u16 urb_size;
1714 int i = 0;
1715 char data[30 *3 + 4];
1716 char *d = data;
1717 int m = (sizeof(data) - 1) / 3;
1718 int l = 0;
1719 struct u132_target *target = &ftdi->target[ed];
1720 struct u132_command *command = &ftdi->command[
1721 COMMAND_MASK & ftdi->command_next];
1722 command->header = 0x81 | (ed << 5);
1723 command->address = (toggle_bits << 6) | (ep_number << 2)
1724 | (address << 0);
1725 command->width = usb_maxpacket(urb->dev, urb->pipe,
1726 usb_pipeout(urb->pipe));
1727 command->follows = min(1024,
1728 urb->transfer_buffer_length -
1729 urb->actual_length);
1730 command->value = 0;
1731 command->buffer = urb->transfer_buffer +
1732 urb->actual_length;
1733 command->length = 0x8000 | (command->follows - 1);
1734 b = command->buffer;
1735 urb_size = command->follows;
1736 data[0] = 0;
1737 while (urb_size-- > 0) {
1738 if (i > m) {
1739 } else if (i++ < m) {
1740 int w = sprintf(d, " %02X", *b++);
1741 d += w;
1742 l += w;
1743 } else
1744 d += sprintf(d, " ..");
1746 target->callback = callback;
1747 target->endp = endp;
1748 target->urb = urb;
1749 target->active = 1;
1750 ftdi->command_next += 1;
1751 ftdi_elan_kick_command_queue(ftdi);
1752 up(&ftdi->u132_lock);
1753 return 0;
1754 } else {
1755 up(&ftdi->u132_lock);
1756 msleep(100);
1757 goto wait;
1762 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1763 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1764 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1765 int toggle_bits, int error_count, int condition_code, int repeat_number,
1766 int halted, int skipped, int actual, int non_null))
1768 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1769 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1770 ep_number, toggle_bits, callback);
1774 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1775 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1776 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1777 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1778 int toggle_bits, int error_count, int condition_code, int repeat_number,
1779 int halted, int skipped, int actual, int non_null))
1781 u8 ed = ed_number - 1;
1782 wait:if (ftdi->disconnected > 0) {
1783 return -ENODEV;
1784 } else if (ftdi->initialized == 0) {
1785 return -ENODEV;
1786 } else {
1787 int command_size;
1788 down(&ftdi->u132_lock);
1789 command_size = ftdi->command_next - ftdi->command_head;
1790 if (command_size < COMMAND_SIZE) {
1791 int remaining_length = urb->transfer_buffer_length -
1792 urb->actual_length;
1793 struct u132_target *target = &ftdi->target[ed];
1794 struct u132_command *command = &ftdi->command[
1795 COMMAND_MASK & ftdi->command_next];
1796 command->header = 0x83 | (ed << 5);
1797 if (remaining_length == 0) {
1798 command->length = 0x0000;
1799 } else if (remaining_length > 1024) {
1800 command->length = 0x8000 | 1023;
1801 } else
1802 command->length = 0x8000 | (remaining_length -
1804 command->address = (toggle_bits << 6) | (ep_number << 2)
1805 | (address << 0);
1806 command->width = usb_maxpacket(urb->dev, urb->pipe,
1807 usb_pipeout(urb->pipe));
1808 command->follows = 0;
1809 command->value = 0;
1810 command->buffer = NULL;
1811 target->callback = callback;
1812 target->endp = endp;
1813 target->urb = urb;
1814 target->active = 1;
1815 ftdi->command_next += 1;
1816 ftdi_elan_kick_command_queue(ftdi);
1817 up(&ftdi->u132_lock);
1818 return 0;
1819 } else {
1820 up(&ftdi->u132_lock);
1821 msleep(100);
1822 goto wait;
1827 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1828 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1829 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1830 int toggle_bits, int error_count, int condition_code, int repeat_number,
1831 int halted, int skipped, int actual, int non_null))
1833 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1834 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1835 ep_number, toggle_bits, callback);
1839 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1840 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1841 void *endp)
1843 u8 ed = ed_number - 1;
1844 if (ftdi->disconnected > 0) {
1845 return -ENODEV;
1846 } else if (ftdi->initialized == 0) {
1847 return -ENODEV;
1848 } else {
1849 struct u132_target *target = &ftdi->target[ed];
1850 down(&ftdi->u132_lock);
1851 if (target->abandoning > 0) {
1852 up(&ftdi->u132_lock);
1853 return 0;
1854 } else {
1855 target->abandoning = 1;
1856 wait_1:if (target->active == 1) {
1857 int command_size = ftdi->command_next -
1858 ftdi->command_head;
1859 if (command_size < COMMAND_SIZE) {
1860 struct u132_command *command =
1861 &ftdi->command[COMMAND_MASK &
1862 ftdi->command_next];
1863 command->header = 0x80 | (ed << 5) |
1864 0x4;
1865 command->length = 0x00;
1866 command->address = 0x00;
1867 command->width = 0x00;
1868 command->follows = 0;
1869 command->value = 0;
1870 command->buffer = &command->value;
1871 ftdi->command_next += 1;
1872 ftdi_elan_kick_command_queue(ftdi);
1873 } else {
1874 up(&ftdi->u132_lock);
1875 msleep(100);
1876 down(&ftdi->u132_lock);
1877 goto wait_1;
1880 up(&ftdi->u132_lock);
1881 return 0;
1886 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1887 void *endp)
1889 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1890 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1894 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1895 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1897 int retry_on_empty = 10;
1898 int retry_on_timeout = 5;
1899 int retry_on_status = 20;
1900 more:{
1901 int packet_bytes = 0;
1902 int retval = usb_bulk_msg(ftdi->udev,
1903 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1904 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1905 &packet_bytes, msecs_to_jiffies(100));
1906 if (packet_bytes > 2) {
1907 char diag[30 *3 + 4];
1908 char *d = diag;
1909 int m = (sizeof(diag) - 1) / 3;
1910 char *b = ftdi->bulk_in_buffer;
1911 int bytes_read = 0;
1912 diag[0] = 0;
1913 while (packet_bytes-- > 0) {
1914 char c = *b++;
1915 if (bytes_read < m) {
1916 d += sprintf(d, " %02X",
1917 0x000000FF & c);
1918 } else if (bytes_read > m) {
1919 } else
1920 d += sprintf(d, " ..");
1921 bytes_read += 1;
1922 continue;
1924 goto more;
1925 } else if (packet_bytes > 1) {
1926 char s1 = ftdi->bulk_in_buffer[0];
1927 char s2 = ftdi->bulk_in_buffer[1];
1928 if (s1 == 0x31 && s2 == 0x60) {
1929 return 0;
1930 } else if (retry_on_status-- > 0) {
1931 goto more;
1932 } else {
1933 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1934 "imit reached\n");
1935 return -EFAULT;
1937 } else if (packet_bytes > 0) {
1938 char b1 = ftdi->bulk_in_buffer[0];
1939 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1940 "TDI = %02X\n", b1);
1941 if (retry_on_status-- > 0) {
1942 goto more;
1943 } else {
1944 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1945 "imit reached\n");
1946 return -EFAULT;
1948 } else if (retval == -ETIMEDOUT) {
1949 if (retry_on_timeout-- > 0) {
1950 goto more;
1951 } else {
1952 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1953 "t reached\n");
1954 return -ENOMEM;
1956 } else if (retval == 0) {
1957 if (retry_on_empty-- > 0) {
1958 goto more;
1959 } else {
1960 dev_err(&ftdi->udev->dev, "empty packet retry l"
1961 "imit reached\n");
1962 return -ENOMEM;
1964 } else {
1965 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1966 return retval;
1969 return -1;
1974 * send the long flush sequence
1977 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1979 int retval;
1980 struct urb *urb;
1981 char *buf;
1982 int I = 257;
1983 int i = 0;
1984 urb = usb_alloc_urb(0, GFP_KERNEL);
1985 if (!urb) {
1986 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1987 "ence\n");
1988 return -ENOMEM;
1990 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1991 if (!buf) {
1992 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
1993 "uence\n");
1994 usb_free_urb(urb);
1995 return -ENOMEM;
1997 while (I-- > 0)
1998 buf[i++] = 0x55;
1999 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2000 ftdi->bulk_out_endpointAddr), buf, i,
2001 ftdi_elan_write_bulk_callback, ftdi);
2002 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2003 retval = usb_submit_urb(urb, GFP_KERNEL);
2004 if (retval) {
2005 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2006 "flush sequence\n");
2007 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2008 usb_free_urb(urb);
2009 return -ENOMEM;
2011 usb_free_urb(urb);
2012 return 0;
2017 * send the reset sequence
2020 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2022 int retval;
2023 struct urb *urb;
2024 char *buf;
2025 int I = 4;
2026 int i = 0;
2027 urb = usb_alloc_urb(0, GFP_KERNEL);
2028 if (!urb) {
2029 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2030 "quence\n");
2031 return -ENOMEM;
2033 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2034 if (!buf) {
2035 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2036 " sequence\n");
2037 usb_free_urb(urb);
2038 return -ENOMEM;
2040 buf[i++] = 0x55;
2041 buf[i++] = 0xAA;
2042 buf[i++] = 0x5A;
2043 buf[i++] = 0xA5;
2044 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2045 ftdi->bulk_out_endpointAddr), buf, i,
2046 ftdi_elan_write_bulk_callback, ftdi);
2047 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2048 retval = usb_submit_urb(urb, GFP_KERNEL);
2049 if (retval) {
2050 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2051 "reset sequence\n");
2052 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2053 usb_free_urb(urb);
2054 return -ENOMEM;
2056 usb_free_urb(urb);
2057 return 0;
2060 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2062 int retval;
2063 int long_stop = 10;
2064 int retry_on_timeout = 5;
2065 int retry_on_empty = 10;
2066 int err_count = 0;
2067 retval = ftdi_elan_flush_input_fifo(ftdi);
2068 if (retval)
2069 return retval;
2070 ftdi->bulk_in_left = 0;
2071 ftdi->bulk_in_last = -1;
2072 while (long_stop-- > 0) {
2073 int read_stop;
2074 int read_stuck;
2075 retval = ftdi_elan_synchronize_flush(ftdi);
2076 if (retval)
2077 return retval;
2078 retval = ftdi_elan_flush_input_fifo(ftdi);
2079 if (retval)
2080 return retval;
2081 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2082 if (retval)
2083 return retval;
2084 read_stop = 100;
2085 read_stuck = 10;
2086 read:{
2087 int packet_bytes = 0;
2088 retval = usb_bulk_msg(ftdi->udev,
2089 usb_rcvbulkpipe(ftdi->udev,
2090 ftdi->bulk_in_endpointAddr),
2091 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2092 &packet_bytes, msecs_to_jiffies(500));
2093 if (packet_bytes > 2) {
2094 char diag[30 *3 + 4];
2095 char *d = diag;
2096 int m = (sizeof(diag) - 1) / 3;
2097 char *b = ftdi->bulk_in_buffer;
2098 int bytes_read = 0;
2099 unsigned char c = 0;
2100 diag[0] = 0;
2101 while (packet_bytes-- > 0) {
2102 c = *b++;
2103 if (bytes_read < m) {
2104 d += sprintf(d, " %02X", c);
2105 } else if (bytes_read > m) {
2106 } else
2107 d += sprintf(d, " ..");
2108 bytes_read += 1;
2109 continue;
2111 if (c == 0x7E) {
2112 return 0;
2113 } else {
2114 if (c == 0x55) {
2115 goto read;
2116 } else if (read_stop-- > 0) {
2117 goto read;
2118 } else {
2119 dev_err(&ftdi->udev->dev, "retr"
2120 "y limit reached\n");
2121 continue;
2124 } else if (packet_bytes > 1) {
2125 unsigned char s1 = ftdi->bulk_in_buffer[0];
2126 unsigned char s2 = ftdi->bulk_in_buffer[1];
2127 if (s1 == 0x31 && s2 == 0x00) {
2128 if (read_stuck-- > 0) {
2129 goto read;
2130 } else
2131 goto reset;
2132 } else if (s1 == 0x31 && s2 == 0x60) {
2133 if (read_stop-- > 0) {
2134 goto read;
2135 } else {
2136 dev_err(&ftdi->udev->dev, "retr"
2137 "y limit reached\n");
2138 continue;
2140 } else {
2141 if (read_stop-- > 0) {
2142 goto read;
2143 } else {
2144 dev_err(&ftdi->udev->dev, "retr"
2145 "y limit reached\n");
2146 continue;
2149 } else if (packet_bytes > 0) {
2150 if (read_stop-- > 0) {
2151 goto read;
2152 } else {
2153 dev_err(&ftdi->udev->dev, "retry limit "
2154 "reached\n");
2155 continue;
2157 } else if (retval == -ETIMEDOUT) {
2158 if (retry_on_timeout-- > 0) {
2159 goto read;
2160 } else {
2161 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2162 "try limit reached\n");
2163 continue;
2165 } else if (retval == 0) {
2166 if (retry_on_empty-- > 0) {
2167 goto read;
2168 } else {
2169 dev_err(&ftdi->udev->dev, "empty packet"
2170 " retry limit reached\n");
2171 continue;
2173 } else {
2174 err_count += 1;
2175 dev_err(&ftdi->udev->dev, "error = %d\n",
2176 retval);
2177 if (read_stop-- > 0) {
2178 goto read;
2179 } else {
2180 dev_err(&ftdi->udev->dev, "retry limit "
2181 "reached\n");
2182 continue;
2187 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2188 return -EFAULT;
2191 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2193 int retry_on_empty = 10;
2194 int retry_on_timeout = 5;
2195 int retry_on_status = 50;
2196 more:{
2197 int packet_bytes = 0;
2198 int retval = usb_bulk_msg(ftdi->udev,
2199 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2200 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2201 &packet_bytes, msecs_to_jiffies(1000));
2202 if (packet_bytes > 2) {
2203 char diag[30 *3 + 4];
2204 char *d = diag;
2205 int m = (sizeof(diag) - 1) / 3;
2206 char *b = ftdi->bulk_in_buffer;
2207 int bytes_read = 0;
2208 diag[0] = 0;
2209 while (packet_bytes-- > 0) {
2210 char c = *b++;
2211 if (bytes_read < m) {
2212 d += sprintf(d, " %02X",
2213 0x000000FF & c);
2214 } else if (bytes_read > m) {
2215 } else
2216 d += sprintf(d, " ..");
2217 bytes_read += 1;
2218 continue;
2220 goto more;
2221 } else if (packet_bytes > 1) {
2222 char s1 = ftdi->bulk_in_buffer[0];
2223 char s2 = ftdi->bulk_in_buffer[1];
2224 if (s1 == 0x31 && s2 == 0x60) {
2225 return 0;
2226 } else if (retry_on_status-- > 0) {
2227 msleep(5);
2228 goto more;
2229 } else
2230 return -EFAULT;
2231 } else if (packet_bytes > 0) {
2232 char b1 = ftdi->bulk_in_buffer[0];
2233 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2234 "TDI = %02X\n", b1);
2235 if (retry_on_status-- > 0) {
2236 msleep(5);
2237 goto more;
2238 } else {
2239 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2240 "imit reached\n");
2241 return -EFAULT;
2243 } else if (retval == -ETIMEDOUT) {
2244 if (retry_on_timeout-- > 0) {
2245 goto more;
2246 } else {
2247 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2248 "t reached\n");
2249 return -ENOMEM;
2251 } else if (retval == 0) {
2252 if (retry_on_empty-- > 0) {
2253 goto more;
2254 } else {
2255 dev_err(&ftdi->udev->dev, "empty packet retry l"
2256 "imit reached\n");
2257 return -ENOMEM;
2259 } else {
2260 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2261 return -ENOMEM;
2264 return -1;
2267 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2269 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2270 if (UxxxStatus)
2271 return UxxxStatus;
2272 if (ftdi->controlreg & 0x00400000) {
2273 if (ftdi->card_ejected) {
2274 } else {
2275 ftdi->card_ejected = 1;
2276 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2277 "%08X\n", ftdi->controlreg);
2279 return -ENODEV;
2280 } else {
2281 u8 fn = ftdi->function - 1;
2282 int activePCIfn = fn << 8;
2283 u32 pcidata;
2284 u32 pciVID;
2285 u32 pciPID;
2286 int reg = 0;
2287 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2288 &pcidata);
2289 if (UxxxStatus)
2290 return UxxxStatus;
2291 pciVID = pcidata & 0xFFFF;
2292 pciPID = (pcidata >> 16) & 0xFFFF;
2293 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2294 ftdi->platform_data.device) {
2295 return 0;
2296 } else {
2297 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2298 "ce=%04X pciPID=%04X\n",
2299 ftdi->platform_data.vendor, pciVID,
2300 ftdi->platform_data.device, pciPID);
2301 return -ENODEV;
2307 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2308 offsetof(struct ohci_regs, member), 0, data);
2309 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2310 offsetof(struct ohci_regs, member), 0, data);
2312 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2313 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2314 OHCI_INTR_WDH)
2315 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2317 int devices = 0;
2318 int retval;
2319 u32 hc_control;
2320 int num_ports;
2321 u32 control;
2322 u32 rh_a = -1;
2323 u32 status;
2324 u32 fminterval;
2325 u32 hc_fminterval;
2326 u32 periodicstart;
2327 u32 cmdstatus;
2328 u32 roothub_a;
2329 int mask = OHCI_INTR_INIT;
2330 int sleep_time = 0;
2331 int reset_timeout = 30; /* ... allow extra time */
2332 int temp;
2333 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2334 if (retval)
2335 return retval;
2336 retval = ftdi_read_pcimem(ftdi, control, &control);
2337 if (retval)
2338 return retval;
2339 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2340 if (retval)
2341 return retval;
2342 num_ports = rh_a & RH_A_NDP;
2343 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2344 if (retval)
2345 return retval;
2346 hc_fminterval &= 0x3fff;
2347 if (hc_fminterval != FI) {
2349 hc_fminterval |= FSMP(hc_fminterval) << 16;
2350 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2351 if (retval)
2352 return retval;
2353 switch (hc_control & OHCI_CTRL_HCFS) {
2354 case OHCI_USB_OPER:
2355 sleep_time = 0;
2356 break;
2357 case OHCI_USB_SUSPEND:
2358 case OHCI_USB_RESUME:
2359 hc_control &= OHCI_CTRL_RWC;
2360 hc_control |= OHCI_USB_RESUME;
2361 sleep_time = 10;
2362 break;
2363 default:
2364 hc_control &= OHCI_CTRL_RWC;
2365 hc_control |= OHCI_USB_RESET;
2366 sleep_time = 50;
2367 break;
2369 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2370 if (retval)
2371 return retval;
2372 retval = ftdi_read_pcimem(ftdi, control, &control);
2373 if (retval)
2374 return retval;
2375 msleep(sleep_time);
2376 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2377 if (retval)
2378 return retval;
2379 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2380 for (temp = 0; temp < num_ports; temp++) {
2381 retval = ftdi_write_pcimem(ftdi,
2382 roothub.portstatus[temp], RH_PS_LSDA);
2383 if (retval)
2384 return retval;
2387 retval = ftdi_read_pcimem(ftdi, control, &control);
2388 if (retval)
2389 return retval;
2390 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2391 if (retval)
2392 return retval;
2393 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2394 if (retval)
2395 return retval;
2396 extra:{
2397 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2398 if (retval)
2399 return retval;
2400 if (0 != (status & OHCI_HCR)) {
2401 if (--reset_timeout == 0) {
2402 dev_err(&ftdi->udev->dev, "USB HC reset timed o"
2403 "ut!\n");
2404 return -ENODEV;
2405 } else {
2406 msleep(5);
2407 goto extra;
2411 if (quirk & OHCI_QUIRK_INITRESET) {
2412 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2413 if (retval)
2414 return retval;
2415 retval = ftdi_read_pcimem(ftdi, control, &control);
2416 if (retval)
2417 return retval;
2419 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2420 if (retval)
2421 return retval;
2422 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2423 if (retval)
2424 return retval;
2425 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2426 if (retval)
2427 return retval;
2428 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2429 if (retval)
2430 return retval;
2431 retval = ftdi_write_pcimem(ftdi, fminterval,
2432 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2433 if (retval)
2434 return retval;
2435 retval = ftdi_write_pcimem(ftdi, periodicstart,
2436 ((9 *hc_fminterval) / 10) & 0x3fff);
2437 if (retval)
2438 return retval;
2439 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2440 if (retval)
2441 return retval;
2442 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2443 if (retval)
2444 return retval;
2445 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2446 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2447 quirk |= OHCI_QUIRK_INITRESET;
2448 goto retry;
2449 } else
2450 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2451 fminterval, periodicstart);
2452 } /* start controller operations */
2453 hc_control &= OHCI_CTRL_RWC;
2454 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2455 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2456 if (retval)
2457 return retval;
2458 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2459 if (retval)
2460 return retval;
2461 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2462 if (retval)
2463 return retval;
2464 retval = ftdi_read_pcimem(ftdi, control, &control);
2465 if (retval)
2466 return retval;
2467 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2468 if (retval)
2469 return retval;
2470 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2471 if (retval)
2472 return retval;
2473 retval = ftdi_write_pcimem(ftdi, intrdisable,
2474 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2475 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2476 OHCI_INTR_SO);
2477 if (retval)
2478 return retval; /* handle root hub init quirks ... */
2479 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2480 if (retval)
2481 return retval;
2482 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2483 if (quirk & OHCI_QUIRK_SUPERIO) {
2484 roothub_a |= RH_A_NOCP;
2485 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2486 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2487 if (retval)
2488 return retval;
2489 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2490 roothub_a |= RH_A_NPS;
2491 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2492 if (retval)
2493 return retval;
2495 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2496 if (retval)
2497 return retval;
2498 retval = ftdi_write_pcimem(ftdi, roothub.b,
2499 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2500 if (retval)
2501 return retval;
2502 retval = ftdi_read_pcimem(ftdi, control, &control);
2503 if (retval)
2504 return retval;
2505 mdelay((roothub_a >> 23) & 0x1fe);
2506 for (temp = 0; temp < num_ports; temp++) {
2507 u32 portstatus;
2508 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2509 &portstatus);
2510 if (retval)
2511 return retval;
2512 if (1 & portstatus)
2513 devices += 1;
2515 return devices;
2518 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2520 u32 latence_timer;
2521 int UxxxStatus;
2522 u32 pcidata;
2523 int reg = 0;
2524 int activePCIfn = fn << 8;
2525 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2526 if (UxxxStatus)
2527 return UxxxStatus;
2528 reg = 16;
2529 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2530 0xFFFFFFFF);
2531 if (UxxxStatus)
2532 return UxxxStatus;
2533 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2534 &pcidata);
2535 if (UxxxStatus)
2536 return UxxxStatus;
2537 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2538 0xF0000000);
2539 if (UxxxStatus)
2540 return UxxxStatus;
2541 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2542 &pcidata);
2543 if (UxxxStatus)
2544 return UxxxStatus;
2545 reg = 12;
2546 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2547 &latence_timer);
2548 if (UxxxStatus)
2549 return UxxxStatus;
2550 latence_timer &= 0xFFFF00FF;
2551 latence_timer |= 0x00001600;
2552 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2553 latence_timer);
2554 if (UxxxStatus)
2555 return UxxxStatus;
2556 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2557 &pcidata);
2558 if (UxxxStatus)
2559 return UxxxStatus;
2560 reg = 4;
2561 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2562 0x06);
2563 if (UxxxStatus)
2564 return UxxxStatus;
2565 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2566 &pcidata);
2567 if (UxxxStatus)
2568 return UxxxStatus;
2569 for (reg = 0; reg <= 0x54; reg += 4) {
2570 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2571 if (UxxxStatus)
2572 return UxxxStatus;
2574 return 0;
2577 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2579 u32 latence_timer;
2580 int UxxxStatus;
2581 u32 pcidata;
2582 int reg = 0;
2583 int activePCIfn = fn << 8;
2584 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2585 if (UxxxStatus)
2586 return UxxxStatus;
2587 reg = 16;
2588 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2589 0xFFFFFFFF);
2590 if (UxxxStatus)
2591 return UxxxStatus;
2592 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2593 &pcidata);
2594 if (UxxxStatus)
2595 return UxxxStatus;
2596 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2597 0x00000000);
2598 if (UxxxStatus)
2599 return UxxxStatus;
2600 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2601 &pcidata);
2602 if (UxxxStatus)
2603 return UxxxStatus;
2604 reg = 12;
2605 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2606 &latence_timer);
2607 if (UxxxStatus)
2608 return UxxxStatus;
2609 latence_timer &= 0xFFFF00FF;
2610 latence_timer |= 0x00001600;
2611 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2612 latence_timer);
2613 if (UxxxStatus)
2614 return UxxxStatus;
2615 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2616 &pcidata);
2617 if (UxxxStatus)
2618 return UxxxStatus;
2619 reg = 4;
2620 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2621 0x00);
2622 if (UxxxStatus)
2623 return UxxxStatus;
2624 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2625 &pcidata);
2626 if (UxxxStatus)
2627 return UxxxStatus;
2628 return 0;
2631 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2633 int result;
2634 int UxxxStatus;
2635 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2636 if (UxxxStatus)
2637 return UxxxStatus;
2638 result = ftdi_elan_check_controller(ftdi, quirk);
2639 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2640 if (UxxxStatus)
2641 return UxxxStatus;
2642 return result;
2645 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2647 u32 controlreg;
2648 u8 sensebits;
2649 int UxxxStatus;
2650 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2651 if (UxxxStatus)
2652 return UxxxStatus;
2653 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2654 if (UxxxStatus)
2655 return UxxxStatus;
2656 msleep(750);
2657 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2658 if (UxxxStatus)
2659 return UxxxStatus;
2660 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2661 if (UxxxStatus)
2662 return UxxxStatus;
2663 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2664 if (UxxxStatus)
2665 return UxxxStatus;
2666 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2667 if (UxxxStatus)
2668 return UxxxStatus;
2669 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2670 if (UxxxStatus)
2671 return UxxxStatus;
2672 msleep(250);
2673 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2674 if (UxxxStatus)
2675 return UxxxStatus;
2676 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2677 if (UxxxStatus)
2678 return UxxxStatus;
2679 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2680 if (UxxxStatus)
2681 return UxxxStatus;
2682 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2683 if (UxxxStatus)
2684 return UxxxStatus;
2685 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2686 if (UxxxStatus)
2687 return UxxxStatus;
2688 msleep(1000);
2689 sensebits = (controlreg >> 16) & 0x000F;
2690 if (0x0D == sensebits)
2691 return 0;
2692 else
2693 return - ENXIO;
2696 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2698 int UxxxStatus;
2699 u32 pcidata;
2700 int reg = 0;
2701 u8 fn;
2702 int activePCIfn = 0;
2703 int max_devices = 0;
2704 int controllers = 0;
2705 int unrecognized = 0;
2706 ftdi->function = 0;
2707 for (fn = 0; (fn < 4); fn++) {
2708 u32 pciVID = 0;
2709 u32 pciPID = 0;
2710 int devices = 0;
2711 activePCIfn = fn << 8;
2712 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2713 &pcidata);
2714 if (UxxxStatus)
2715 return UxxxStatus;
2716 pciVID = pcidata & 0xFFFF;
2717 pciPID = (pcidata >> 16) & 0xFFFF;
2718 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2719 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2720 controllers += 1;
2721 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2723 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2724 controllers += 1;
2725 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2726 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2727 controllers += 1;
2728 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2730 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2731 controllers += 1;
2732 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2733 devices = ftdi_elan_found_controller(ftdi, fn,
2734 OHCI_QUIRK_AMD756);
2735 controllers += 1;
2736 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2737 devices = ftdi_elan_found_controller(ftdi, fn,
2738 OHCI_QUIRK_ZFMICRO);
2739 controllers += 1;
2740 } else if (0 == pcidata) {
2741 } else
2742 unrecognized += 1;
2743 if (devices > max_devices) {
2744 max_devices = devices;
2745 ftdi->function = fn + 1;
2746 ftdi->platform_data.vendor = pciVID;
2747 ftdi->platform_data.device = pciPID;
2750 if (ftdi->function > 0) {
2751 UxxxStatus = ftdi_elan_setup_controller(ftdi,
2752 ftdi->function - 1);
2753 if (UxxxStatus)
2754 return UxxxStatus;
2755 return 0;
2756 } else if (controllers > 0) {
2757 return -ENXIO;
2758 } else if (unrecognized > 0) {
2759 return -ENXIO;
2760 } else {
2761 ftdi->enumerated = 0;
2762 return -ENXIO;
2768 * we use only the first bulk-in and bulk-out endpoints
2770 static int ftdi_elan_probe(struct usb_interface *interface,
2771 const struct usb_device_id *id)
2773 struct usb_host_interface *iface_desc;
2774 struct usb_endpoint_descriptor *endpoint;
2775 size_t buffer_size;
2776 int i;
2777 int retval = -ENOMEM;
2778 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2779 if (ftdi == NULL) {
2780 printk(KERN_ERR "Out of memory\n");
2781 return -ENOMEM;
2783 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2784 mutex_lock(&ftdi_module_lock);
2785 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2786 ftdi->sequence_num = ++ftdi_instances;
2787 mutex_unlock(&ftdi_module_lock);
2788 ftdi_elan_init_kref(ftdi);
2789 init_MUTEX(&ftdi->sw_lock);
2790 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2791 ftdi->interface = interface;
2792 init_MUTEX(&ftdi->u132_lock);
2793 ftdi->expected = 4;
2794 iface_desc = interface->cur_altsetting;
2795 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2796 endpoint = &iface_desc->endpoint[i].desc;
2797 if (!ftdi->bulk_in_endpointAddr &&
2798 usb_endpoint_is_bulk_in(endpoint)) {
2799 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2800 ftdi->bulk_in_size = buffer_size;
2801 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2802 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2803 if (!ftdi->bulk_in_buffer) {
2804 dev_err(&ftdi->udev->dev, "Could not allocate b"
2805 "ulk_in_buffer\n");
2806 retval = -ENOMEM;
2807 goto error;
2810 if (!ftdi->bulk_out_endpointAddr &&
2811 usb_endpoint_is_bulk_out(endpoint)) {
2812 ftdi->bulk_out_endpointAddr =
2813 endpoint->bEndpointAddress;
2816 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2817 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2818 "-out endpoints\n");
2819 retval = -ENODEV;
2820 goto error;
2822 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2823 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2824 ftdi->bulk_out_endpointAddr);
2825 usb_set_intfdata(interface, ftdi);
2826 if (iface_desc->desc.bInterfaceNumber == 0 &&
2827 ftdi->bulk_in_endpointAddr == 0x81 &&
2828 ftdi->bulk_out_endpointAddr == 0x02) {
2829 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2830 if (retval) {
2831 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2832 "this device.\n");
2833 usb_set_intfdata(interface, NULL);
2834 retval = -ENOMEM;
2835 goto error;
2836 } else {
2837 ftdi->class = &ftdi_elan_jtag_class;
2838 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2839 "%d now attached to ftdi%d\n", ftdi,
2840 iface_desc->desc.bInterfaceNumber,
2841 interface->minor);
2842 return 0;
2844 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2845 ftdi->bulk_in_endpointAddr == 0x83 &&
2846 ftdi->bulk_out_endpointAddr == 0x04) {
2847 ftdi->class = NULL;
2848 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2849 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2850 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2851 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2852 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2853 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2854 return 0;
2855 } else {
2856 dev_err(&ftdi->udev->dev,
2857 "Could not find ELAN's U132 device\n");
2858 retval = -ENODEV;
2859 goto error;
2861 error:if (ftdi) {
2862 ftdi_elan_put_kref(ftdi);
2864 return retval;
2867 static void ftdi_elan_disconnect(struct usb_interface *interface)
2869 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2870 ftdi->disconnected += 1;
2871 if (ftdi->class) {
2872 int minor = interface->minor;
2873 struct usb_class_driver *class = ftdi->class;
2874 usb_set_intfdata(interface, NULL);
2875 usb_deregister_dev(interface, class);
2876 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2877 "or %d now disconnected\n", minor);
2878 } else {
2879 ftdi_status_cancel_work(ftdi);
2880 ftdi_command_cancel_work(ftdi);
2881 ftdi_response_cancel_work(ftdi);
2882 ftdi_elan_abandon_completions(ftdi);
2883 ftdi_elan_abandon_targets(ftdi);
2884 if (ftdi->registered) {
2885 platform_device_unregister(&ftdi->platform_dev);
2886 ftdi->synchronized = 0;
2887 ftdi->enumerated = 0;
2888 ftdi->initialized = 0;
2889 ftdi->registered = 0;
2891 flush_workqueue(status_queue);
2892 flush_workqueue(command_queue);
2893 flush_workqueue(respond_queue);
2894 ftdi->disconnected += 1;
2895 usb_set_intfdata(interface, NULL);
2896 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2897 "face now disconnected\n");
2899 ftdi_elan_put_kref(ftdi);
2902 static struct usb_driver ftdi_elan_driver = {
2903 .name = "ftdi-elan",
2904 .probe = ftdi_elan_probe,
2905 .disconnect = ftdi_elan_disconnect,
2906 .id_table = ftdi_elan_table,
2908 static int __init ftdi_elan_init(void)
2910 int result;
2911 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2912 __TIME__, __DATE__);
2913 mutex_init(&ftdi_module_lock);
2914 INIT_LIST_HEAD(&ftdi_static_list);
2915 status_queue = create_singlethread_workqueue("ftdi-status-control");
2916 if (!status_queue)
2917 goto err_status_queue;
2918 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2919 if (!command_queue)
2920 goto err_command_queue;
2921 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2922 if (!respond_queue)
2923 goto err_respond_queue;
2924 result = usb_register(&ftdi_elan_driver);
2925 if (result) {
2926 destroy_workqueue(status_queue);
2927 destroy_workqueue(command_queue);
2928 destroy_workqueue(respond_queue);
2929 printk(KERN_ERR "usb_register failed. Error number %d\n",
2930 result);
2932 return result;
2934 err_respond_queue:
2935 destroy_workqueue(command_queue);
2936 err_command_queue:
2937 destroy_workqueue(status_queue);
2938 err_status_queue:
2939 printk(KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name);
2940 return -ENOMEM;
2943 static void __exit ftdi_elan_exit(void)
2945 struct usb_ftdi *ftdi;
2946 struct usb_ftdi *temp;
2947 usb_deregister(&ftdi_elan_driver);
2948 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2949 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2950 ftdi_status_cancel_work(ftdi);
2951 ftdi_command_cancel_work(ftdi);
2952 ftdi_response_cancel_work(ftdi);
2953 } flush_workqueue(status_queue);
2954 destroy_workqueue(status_queue);
2955 status_queue = NULL;
2956 flush_workqueue(command_queue);
2957 destroy_workqueue(command_queue);
2958 command_queue = NULL;
2959 flush_workqueue(respond_queue);
2960 destroy_workqueue(respond_queue);
2961 respond_queue = NULL;
2965 module_init(ftdi_elan_init);
2966 module_exit(ftdi_elan_exit);