blob: 684800c66bb4de32e90b877ff617ba04305e7013 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
4 *
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
7 *
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
9 * tony.olech@elandigitalsystems.com
10 *
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
15 *
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.
19 *
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)
27 *
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.
32 *
33 */
34
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
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>
51MODULE_AUTHOR("Tony Olech");
52MODULE_DESCRIPTION("FTDI ELAN driver");
53MODULE_LICENSE("GPL");
54#define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55static bool distrust_firmware = 1;
56module_param(distrust_firmware, bool, 0);
57MODULE_PARM_DESC(distrust_firmware,
58 "true to distrust firmware power/overcurrent setup");
59extern struct platform_driver u132_platform_driver;
60/*
61 * ftdi_module_lock exists to protect access to global variables
62 *
63 */
64static struct mutex ftdi_module_lock;
65static int ftdi_instances = 0;
66static struct list_head ftdi_static_list;
67/*
68 * end of the global variables protected by ftdi_module_lock
69 */
70#include "usb_u132.h"
71#include <asm/io.h>
72#include <linux/usb/hcd.h>
73
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>
78 */
79
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*/
85static const struct usb_device_id ftdi_elan_table[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
87 { /* Terminating entry */ }
88};
89
90MODULE_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
100 * code and recompile
101 */
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)
106struct u132_command {
107 u8 header;
108 u16 length;
109 u8 address;
110 u8 width;
111 u32 value;
112 int follows;
113 void *buffer;
114};
115#define RESPOND_BITS 5
116#define RESPOND_SIZE (1<<RESPOND_BITS)
117#define RESPOND_MASK (RESPOND_SIZE-1)
118struct u132_respond {
119 u8 header;
120 u8 address;
121 u32 *value;
122 int *result;
123 struct completion wait_completion;
124};
125struct u132_target {
126 void *endp;
127 struct urb *urb;
128 int toggle_bits;
129 int error_count;
130 int condition_code;
131 int repeat_number;
132 int halted;
133 int skipped;
134 int actual;
135 int non_null;
136 int active;
137 int abandoning;
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,
141 int non_null);
142};
143/* Structure to hold all of our device specific stuff*/
144struct usb_ftdi {
145 struct list_head ftdi_list;
146 struct mutex u132_lock;
147 int command_next;
148 int command_head;
149 struct u132_command command[COMMAND_SIZE];
150 int respond_next;
151 int respond_head;
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;
160 int function;
161 int sequence_num;
162 int disconnected;
163 int gone_away;
164 int stuck_status;
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;
177 size_t bulk_in_size;
178 size_t bulk_in_last;
179 size_t bulk_in_left;
180 __u8 bulk_in_endpointAddr;
181 __u8 bulk_out_endpointAddr;
182 struct kref kref;
183 u32 controlreg;
184 u8 response[4 + 1024];
185 int expected;
186 int received;
187 int ed_found;
188};
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, \
191 platform_dev)
192static struct usb_driver ftdi_elan_driver;
193static void ftdi_elan_delete(struct kref *kref)
194{
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);
201 ftdi_instances -= 1;
202 mutex_unlock(&ftdi_module_lock);
203 kfree(ftdi->bulk_in_buffer);
204 ftdi->bulk_in_buffer = NULL;
205 kfree(ftdi);
206}
207
208static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
209{
210 kref_put(&ftdi->kref, ftdi_elan_delete);
211}
212
213static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
214{
215 kref_get(&ftdi->kref);
216}
217
218static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
219{
220 kref_init(&ftdi->kref);
221}
222
223static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
224{
225 if (!schedule_delayed_work(&ftdi->status_work, delta))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
227}
228
229static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
230{
231 if (schedule_delayed_work(&ftdi->status_work, delta))
232 kref_get(&ftdi->kref);
233}
234
235static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
236{
237 if (cancel_delayed_work_sync(&ftdi->status_work))
238 kref_put(&ftdi->kref, ftdi_elan_delete);
239}
240
241static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
242{
243 if (!schedule_delayed_work(&ftdi->command_work, delta))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
245}
246
247static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
248{
249 if (schedule_delayed_work(&ftdi->command_work, delta))
250 kref_get(&ftdi->kref);
251}
252
253static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
254{
255 if (cancel_delayed_work_sync(&ftdi->command_work))
256 kref_put(&ftdi->kref, ftdi_elan_delete);
257}
258
259static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
260 unsigned int delta)
261{
262 if (!schedule_delayed_work(&ftdi->respond_work, delta))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
264}
265
266static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
267{
268 if (schedule_delayed_work(&ftdi->respond_work, delta))
269 kref_get(&ftdi->kref);
270}
271
272static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
273{
274 if (cancel_delayed_work_sync(&ftdi->respond_work))
275 kref_put(&ftdi->kref, ftdi_elan_delete);
276}
277
278void ftdi_elan_gone_away(struct platform_device *pdev)
279{
280 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
281 ftdi->gone_away += 1;
282 ftdi_elan_put_kref(ftdi);
283}
284
285
286EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
287static void ftdi_release_platform_dev(struct device *dev)
288{
289 dev->parent = NULL;
290}
291
292static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
293 struct u132_target *target, u8 *buffer, int length);
294static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
295static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
296static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
297static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
298static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
299static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
300static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
301static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
302static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
303static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
304{
305 if (ftdi->platform_dev.dev.parent)
306 return -EBUSY;
307
308 ftdi_elan_get_kref(ftdi);
309 ftdi->platform_data.potpg = 100;
310 ftdi->platform_data.reset = NULL;
311 ftdi->platform_dev.id = ftdi->sequence_num;
312 ftdi->platform_dev.resource = ftdi->resources;
313 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
314 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
315 ftdi->platform_dev.dev.parent = NULL;
316 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
317 ftdi->platform_dev.dev.dma_mask = NULL;
318 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
319 ftdi->platform_dev.name = ftdi->device_name;
320 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
321 request_module("u132_hcd");
322 dev_info(&ftdi->udev->dev, "registering '%s'\n",
323 ftdi->platform_dev.name);
324
325 return platform_device_register(&ftdi->platform_dev);
326}
327
328static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
329{
330 mutex_lock(&ftdi->u132_lock);
331 while (ftdi->respond_next > ftdi->respond_head) {
332 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
333 ftdi->respond_head++];
334 *respond->result = -ESHUTDOWN;
335 *respond->value = 0;
336 complete(&respond->wait_completion);
337 } mutex_unlock(&ftdi->u132_lock);
338}
339
340static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
341{
342 int ed_number = 4;
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);
351 }
352 }
353 ftdi->received = 0;
354 ftdi->expected = 4;
355 ftdi->ed_found = 0;
356 mutex_unlock(&ftdi->u132_lock);
357}
358
359static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
360{
361 int ed_number = 4;
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 -
368 ftdi->command_head;
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;
377 command->value = 0;
378 command->buffer = &command->value;
379 ftdi->command_next += 1;
380 ftdi_elan_kick_command_queue(ftdi);
381 } else {
382 mutex_unlock(&ftdi->u132_lock);
383 msleep(100);
384 mutex_lock(&ftdi->u132_lock);
385 goto wait_1;
386 }
387 }
388 wait_2:if (target->active == 1) {
389 int command_size = ftdi->command_next -
390 ftdi->command_head;
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;
399 command->value = 0;
400 command->buffer = &command->value;
401 ftdi->command_next += 1;
402 ftdi_elan_kick_command_queue(ftdi);
403 } else {
404 mutex_unlock(&ftdi->u132_lock);
405 msleep(100);
406 mutex_lock(&ftdi->u132_lock);
407 goto wait_2;
408 }
409 }
410 }
411 ftdi->received = 0;
412 ftdi->expected = 4;
413 ftdi->ed_found = 0;
414 mutex_unlock(&ftdi->u132_lock);
415}
416
417static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
418{
419 int ed_number = 4;
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 -
426 ftdi->command_head;
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;
435 command->value = 0;
436 command->buffer = &command->value;
437 ftdi->command_next += 1;
438 ftdi_elan_kick_command_queue(ftdi);
439 } else {
440 mutex_unlock(&ftdi->u132_lock);
441 msleep(100);
442 mutex_lock(&ftdi->u132_lock);
443 goto wait;
444 }
445 }
446 }
447 ftdi->received = 0;
448 ftdi->expected = 4;
449 ftdi->ed_found = 0;
450 mutex_unlock(&ftdi->u132_lock);
451}
452
453static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
454{
455 ftdi_command_queue_work(ftdi, 0);
456}
457
458static void ftdi_elan_command_work(struct work_struct *work)
459{
460 struct usb_ftdi *ftdi =
461 container_of(work, struct usb_ftdi, command_work.work);
462
463 if (ftdi->disconnected > 0) {
464 ftdi_elan_put_kref(ftdi);
465 return;
466 } else {
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;
472 } else if (retval)
473 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
474 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
475 return;
476 }
477}
478
479static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
480{
481 ftdi_respond_queue_work(ftdi, 0);
482}
483
484static void ftdi_elan_respond_work(struct work_struct *work)
485{
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);
490 return;
491 } else {
492 int retval = ftdi_elan_respond_engine(ftdi);
493 if (retval == 0) {
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;
500 } else {
501 ftdi->disconnected += 1;
502 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
503 }
504 if (ftdi->disconnected > 0) {
505 ftdi_elan_abandon_completions(ftdi);
506 ftdi_elan_abandon_targets(ftdi);
507 }
508 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
509 return;
510 }
511}
512
513
514/*
515 * the sw_lock is initially held and will be freed
516 * after the FTDI has been synchronized
517 *
518 */
519static void ftdi_elan_status_work(struct work_struct *work)
520{
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);
526 return;
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);
533 up(&ftdi->sw_lock);
534 work_delay_in_msec = 100;
535 } else {
536 dev_err(&ftdi->udev->dev, "synchronize failed\n");
537 up(&ftdi->sw_lock);
538 work_delay_in_msec = 10 *1000;
539 }
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");
546 } else
547 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
548 ftdi->stuck_status);
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;
554 } else
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;
560 } else {
561 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
562 work_delay_in_msec = 1 *1000;
563 }
564 } else if (ftdi->registered == 0) {
565 work_delay_in_msec = 10;
566 if (ftdi_elan_hcd_init(ftdi) == 0) {
567 ftdi->registered = 1;
568 } else
569 dev_err(&ftdi->udev->dev, "register failed\n");
570 work_delay_in_msec = 250;
571 } else {
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;
585 ftdi->gone_away = 0;
586 } else
587 ftdi_elan_flush_targets(ftdi);
588 work_delay_in_msec = 250;
589 } else {
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;
595 }
596 }
597 if (ftdi->disconnected > 0) {
598 ftdi_elan_put_kref(ftdi);
599 return;
600 } else {
601 ftdi_status_requeue_work(ftdi,
602 msecs_to_jiffies(work_delay_in_msec));
603 return;
604 }
605}
606
607
608/*
609 * file_operations for the jtag interface
610 *
611 * the usage count for the device is incremented on open()
612 * and decremented on release()
613 */
614static int ftdi_elan_open(struct inode *inode, struct file *file)
615{
616 int subminor;
617 struct usb_interface *interface;
618
619 subminor = iminor(inode);
620 interface = usb_find_interface(&ftdi_elan_driver, subminor);
621
622 if (!interface) {
623 pr_err("can't find device for minor %d\n", subminor);
624 return -ENODEV;
625 } else {
626 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
627 if (!ftdi) {
628 return -ENODEV;
629 } else {
630 if (down_interruptible(&ftdi->sw_lock)) {
631 return -EINTR;
632 } else {
633 ftdi_elan_get_kref(ftdi);
634 file->private_data = ftdi;
635 return 0;
636 }
637 }
638 }
639}
640
641static int ftdi_elan_release(struct inode *inode, struct file *file)
642{
643 struct usb_ftdi *ftdi = file->private_data;
644 if (ftdi == NULL)
645 return -ENODEV;
646 up(&ftdi->sw_lock); /* decrement the count on our device */
647 ftdi_elan_put_kref(ftdi);
648 return 0;
649}
650
651
652/*
653 *
654 * blocking bulk reads are used to get data from the device
655 *
656 */
657static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
658 size_t count, loff_t *ppos)
659{
660 char data[30 *3 + 4];
661 char *d = data;
662 int m = (sizeof(data) - 1) / 3 - 1;
663 int bytes_read = 0;
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) {
668 return -ENODEV;
669 }
670 data[0] = 0;
671have:if (ftdi->bulk_in_left > 0) {
672 if (count-- > 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) {
678 } else
679 d += sprintf(d, " ..");
680 if (copy_to_user(buffer++, p, 1)) {
681 return -EFAULT;
682 } else {
683 bytes_read += 1;
684 goto have;
685 }
686 } else
687 return bytes_read;
688 }
689more: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,
694 &packet_bytes, 50);
695 if (packet_bytes > 2) {
696 ftdi->bulk_in_left = packet_bytes - 2;
697 ftdi->bulk_in_last = 1;
698 goto have;
699 } else if (retval == -ETIMEDOUT) {
700 if (retry_on_timeout-- > 0) {
701 goto more;
702 } else if (bytes_read > 0) {
703 return bytes_read;
704 } else
705 return retval;
706 } else if (retval == 0) {
707 if (retry_on_empty-- > 0) {
708 goto more;
709 } else
710 return bytes_read;
711 } else
712 return retval;
713 } else
714 return bytes_read;
715}
716
717static void ftdi_elan_write_bulk_callback(struct urb *urb)
718{
719 struct usb_ftdi *ftdi = urb->context;
720 int status = urb->status;
721
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);
726 }
727 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
728 urb->transfer_buffer, urb->transfer_dma);
729}
730
731static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
732 char *buf, int command_size, int total_size)
733{
734 int ed_commands = 0;
735 int b = 0;
736 int I = command_size;
737 int i = ftdi->command_head;
738 while (I-- > 0) {
739 struct u132_command *command = &ftdi->command[COMMAND_MASK &
740 i++];
741 int F = command->follows;
742 u8 *f = command->buffer;
743 if (command->header & 0x80) {
744 ed_commands |= 1 << (0x3 & (command->header >> 5));
745 }
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;
751 while (F-- > 0) {
752 buf[b++] = *f++;
753 }
754 }
755 return ed_commands;
756}
757
758static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
759{
760 int total_size = 0;
761 int I = command_size;
762 int i = ftdi->command_head;
763 while (I-- > 0) {
764 struct u132_command *command = &ftdi->command[COMMAND_MASK &
765 i++];
766 total_size += 5 + command->follows;
767 } return total_size;
768}
769
770static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
771{
772 int retval;
773 char *buf;
774 int ed_commands;
775 int total_size;
776 struct urb *urb;
777 int command_size = ftdi->command_next - ftdi->command_head;
778 if (command_size == 0)
779 return 0;
780 total_size = ftdi_elan_total_command_size(ftdi, command_size);
781 urb = usb_alloc_urb(0, GFP_KERNEL);
782 if (!urb)
783 return -ENOMEM;
784 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
785 &urb->transfer_dma);
786 if (!buf) {
787 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
788 command_size, total_size);
789 usb_free_urb(urb);
790 return -ENOMEM;
791 }
792 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
793 command_size, total_size);
794 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
795 ftdi->bulk_out_endpointAddr), buf, total_size,
796 ftdi_elan_write_bulk_callback, ftdi);
797 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
798 if (ed_commands) {
799 char diag[40 *3 + 4];
800 char *d = diag;
801 int m = total_size;
802 u8 *c = buf;
803 int s = (sizeof(diag) - 1) / 3;
804 diag[0] = 0;
805 while (s-- > 0 && m-- > 0) {
806 if (s > 0 || m == 0) {
807 d += sprintf(d, " %02X", *c++);
808 } else
809 d += sprintf(d, " ..");
810 }
811 }
812 retval = usb_submit_urb(urb, GFP_KERNEL);
813 if (retval) {
814 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
815 retval, urb, command_size, total_size);
816 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
817 usb_free_urb(urb);
818 return retval;
819 }
820 usb_free_urb(urb); /* release our reference to this urb,
821 the USB core will eventually free it entirely */
822 ftdi->command_head += command_size;
823 ftdi_elan_kick_respond_queue(ftdi);
824 return 0;
825}
826
827static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
828 struct u132_target *target, u8 *buffer, int length)
829{
830 struct urb *urb = target->urb;
831 int halted = target->halted;
832 int skipped = target->skipped;
833 int actual = target->actual;
834 int non_null = target->non_null;
835 int toggle_bits = target->toggle_bits;
836 int error_count = target->error_count;
837 int condition_code = target->condition_code;
838 int repeat_number = target->repeat_number;
839 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
840 int, int, int, int) = target->callback;
841 target->active -= 1;
842 target->callback = NULL;
843 (*callback) (target->endp, urb, buffer, length, toggle_bits,
844 error_count, condition_code, repeat_number, halted, skipped,
845 actual, non_null);
846}
847
848static char *have_ed_set_response(struct usb_ftdi *ftdi,
849 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
850 char *b)
851{
852 int payload = (ed_length >> 0) & 0x07FF;
853 mutex_lock(&ftdi->u132_lock);
854 target->actual = 0;
855 target->non_null = (ed_length >> 15) & 0x0001;
856 target->repeat_number = (ed_length >> 11) & 0x000F;
857 if (ed_type == 0x02 || ed_type == 0x03) {
858 if (payload == 0 || target->abandoning > 0) {
859 target->abandoning = 0;
860 mutex_unlock(&ftdi->u132_lock);
861 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
862 payload);
863 ftdi->received = 0;
864 ftdi->expected = 4;
865 ftdi->ed_found = 0;
866 return ftdi->response;
867 } else {
868 ftdi->expected = 4 + payload;
869 ftdi->ed_found = 1;
870 mutex_unlock(&ftdi->u132_lock);
871 return b;
872 }
873 } else {
874 target->abandoning = 0;
875 mutex_unlock(&ftdi->u132_lock);
876 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
877 payload);
878 ftdi->received = 0;
879 ftdi->expected = 4;
880 ftdi->ed_found = 0;
881 return ftdi->response;
882 }
883}
884
885static char *have_ed_get_response(struct usb_ftdi *ftdi,
886 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
887 char *b)
888{
889 mutex_lock(&ftdi->u132_lock);
890 target->condition_code = TD_DEVNOTRESP;
891 target->actual = (ed_length >> 0) & 0x01FF;
892 target->non_null = (ed_length >> 15) & 0x0001;
893 target->repeat_number = (ed_length >> 11) & 0x000F;
894 mutex_unlock(&ftdi->u132_lock);
895 if (target->active)
896 ftdi_elan_do_callback(ftdi, target, NULL, 0);
897 target->abandoning = 0;
898 ftdi->received = 0;
899 ftdi->expected = 4;
900 ftdi->ed_found = 0;
901 return ftdi->response;
902}
903
904
905/*
906 * The engine tries to empty the FTDI fifo
907 *
908 * all responses found in the fifo data are dispatched thus
909 * the response buffer can only ever hold a maximum sized
910 * response from the Uxxx.
911 *
912 */
913static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
914{
915 u8 *b = ftdi->response + ftdi->received;
916 int bytes_read = 0;
917 int retry_on_empty = 1;
918 int retry_on_timeout = 3;
919read:{
920 int packet_bytes = 0;
921 int retval = usb_bulk_msg(ftdi->udev,
922 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
923 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
924 &packet_bytes, 500);
925 char diag[30 *3 + 4];
926 char *d = diag;
927 int m = packet_bytes;
928 u8 *c = ftdi->bulk_in_buffer;
929 int s = (sizeof(diag) - 1) / 3;
930 diag[0] = 0;
931 while (s-- > 0 && m-- > 0) {
932 if (s > 0 || m == 0) {
933 d += sprintf(d, " %02X", *c++);
934 } else
935 d += sprintf(d, " ..");
936 }
937 if (packet_bytes > 2) {
938 ftdi->bulk_in_left = packet_bytes - 2;
939 ftdi->bulk_in_last = 1;
940 goto have;
941 } else if (retval == -ETIMEDOUT) {
942 if (retry_on_timeout-- > 0) {
943 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
944 packet_bytes, bytes_read, diag);
945 goto more;
946 } else if (bytes_read > 0) {
947 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
948 bytes_read, diag);
949 return -ENOMEM;
950 } else {
951 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
952 packet_bytes, bytes_read, diag);
953 return -ENOMEM;
954 }
955 } else if (retval == -EILSEQ) {
956 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
957 retval, packet_bytes, bytes_read, diag);
958 return retval;
959 } else if (retval) {
960 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
961 retval, packet_bytes, bytes_read, diag);
962 return retval;
963 } else {
964 if (retry_on_empty-- > 0) {
965 goto more;
966 } else
967 return 0;
968 }
969 }
970more:{
971 goto read;
972 }
973have:if (ftdi->bulk_in_left > 0) {
974 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
975 bytes_read += 1;
976 ftdi->bulk_in_left -= 1;
977 if (ftdi->received == 0 && c == 0xFF) {
978 goto have;
979 } else
980 *b++ = c;
981 if (++ftdi->received < ftdi->expected) {
982 goto have;
983 } else if (ftdi->ed_found) {
984 int ed_number = (ftdi->response[0] >> 5) & 0x03;
985 u16 ed_length = (ftdi->response[2] << 8) |
986 ftdi->response[1];
987 struct u132_target *target = &ftdi->target[ed_number];
988 int payload = (ed_length >> 0) & 0x07FF;
989 char diag[30 *3 + 4];
990 char *d = diag;
991 int m = payload;
992 u8 *c = 4 + ftdi->response;
993 int s = (sizeof(diag) - 1) / 3;
994 diag[0] = 0;
995 while (s-- > 0 && m-- > 0) {
996 if (s > 0 || m == 0) {
997 d += sprintf(d, " %02X", *c++);
998 } else
999 d += sprintf(d, " ..");
1000 }
1001 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1002 payload);
1003 ftdi->received = 0;
1004 ftdi->expected = 4;
1005 ftdi->ed_found = 0;
1006 b = ftdi->response;
1007 goto have;
1008 } else if (ftdi->expected == 8) {
1009 u8 buscmd;
1010 int respond_head = ftdi->respond_head++;
1011 struct u132_respond *respond = &ftdi->respond[
1012 RESPOND_MASK & respond_head];
1013 u32 data = ftdi->response[7];
1014 data <<= 8;
1015 data |= ftdi->response[6];
1016 data <<= 8;
1017 data |= ftdi->response[5];
1018 data <<= 8;
1019 data |= ftdi->response[4];
1020 *respond->value = data;
1021 *respond->result = 0;
1022 complete(&respond->wait_completion);
1023 ftdi->received = 0;
1024 ftdi->expected = 4;
1025 ftdi->ed_found = 0;
1026 b = ftdi->response;
1027 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1028 if (buscmd == 0x00) {
1029 } else if (buscmd == 0x02) {
1030 } else if (buscmd == 0x06) {
1031 } else if (buscmd == 0x0A) {
1032 } else
1033 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1034 buscmd, data);
1035 goto have;
1036 } else {
1037 if ((ftdi->response[0] & 0x80) == 0x00) {
1038 ftdi->expected = 8;
1039 goto have;
1040 } else {
1041 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1042 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1043 u16 ed_length = (ftdi->response[2] << 8) |
1044 ftdi->response[1];
1045 struct u132_target *target = &ftdi->target[
1046 ed_number];
1047 target->halted = (ftdi->response[0] >> 3) &
1048 0x01;
1049 target->skipped = (ftdi->response[0] >> 2) &
1050 0x01;
1051 target->toggle_bits = (ftdi->response[3] >> 6)
1052 & 0x03;
1053 target->error_count = (ftdi->response[3] >> 4)
1054 & 0x03;
1055 target->condition_code = (ftdi->response[
1056 3] >> 0) & 0x0F;
1057 if ((ftdi->response[0] & 0x10) == 0x00) {
1058 b = have_ed_set_response(ftdi, target,
1059 ed_length, ed_number, ed_type,
1060 b);
1061 goto have;
1062 } else {
1063 b = have_ed_get_response(ftdi, target,
1064 ed_length, ed_number, ed_type,
1065 b);
1066 goto have;
1067 }
1068 }
1069 }
1070 } else
1071 goto more;
1072}
1073
1074
1075/*
1076 * create a urb, and a buffer for it, and copy the data to the urb
1077 *
1078 */
1079static ssize_t ftdi_elan_write(struct file *file,
1080 const char __user *user_buffer, size_t count,
1081 loff_t *ppos)
1082{
1083 int retval = 0;
1084 struct urb *urb;
1085 char *buf;
1086 struct usb_ftdi *ftdi = file->private_data;
1087
1088 if (ftdi->disconnected > 0) {
1089 return -ENODEV;
1090 }
1091 if (count == 0) {
1092 goto exit;
1093 }
1094 urb = usb_alloc_urb(0, GFP_KERNEL);
1095 if (!urb) {
1096 retval = -ENOMEM;
1097 goto error_1;
1098 }
1099 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1100 &urb->transfer_dma);
1101 if (!buf) {
1102 retval = -ENOMEM;
1103 goto error_2;
1104 }
1105 if (copy_from_user(buf, user_buffer, count)) {
1106 retval = -EFAULT;
1107 goto error_3;
1108 }
1109 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1110 ftdi->bulk_out_endpointAddr), buf, count,
1111 ftdi_elan_write_bulk_callback, ftdi);
1112 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1113 retval = usb_submit_urb(urb, GFP_KERNEL);
1114 if (retval) {
1115 dev_err(&ftdi->udev->dev,
1116 "failed submitting write urb, error %d\n", retval);
1117 goto error_3;
1118 }
1119 usb_free_urb(urb);
1120
1121exit:
1122 return count;
1123error_3:
1124 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1125error_2:
1126 usb_free_urb(urb);
1127error_1:
1128 return retval;
1129}
1130
1131static const struct file_operations ftdi_elan_fops = {
1132 .owner = THIS_MODULE,
1133 .llseek = no_llseek,
1134 .read = ftdi_elan_read,
1135 .write = ftdi_elan_write,
1136 .open = ftdi_elan_open,
1137 .release = ftdi_elan_release,
1138};
1139
1140/*
1141 * usb class driver info in order to get a minor number from the usb core,
1142 * and to have the device registered with the driver core
1143 */
1144static struct usb_class_driver ftdi_elan_jtag_class = {
1145 .name = "ftdi-%d-jtag",
1146 .fops = &ftdi_elan_fops,
1147 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1148};
1149
1150/*
1151 * the following definitions are for the
1152 * ELAN FPGA state machgine processor that
1153 * lies on the other side of the FTDI chip
1154 */
1155#define cPCIu132rd 0x0
1156#define cPCIu132wr 0x1
1157#define cPCIiord 0x2
1158#define cPCIiowr 0x3
1159#define cPCImemrd 0x6
1160#define cPCImemwr 0x7
1161#define cPCIcfgrd 0xA
1162#define cPCIcfgwr 0xB
1163#define cPCInull 0xF
1164#define cU132cmd_status 0x0
1165#define cU132flash 0x1
1166#define cPIDsetup 0x0
1167#define cPIDout 0x1
1168#define cPIDin 0x2
1169#define cPIDinonce 0x3
1170#define cCCnoerror 0x0
1171#define cCCcrc 0x1
1172#define cCCbitstuff 0x2
1173#define cCCtoggle 0x3
1174#define cCCstall 0x4
1175#define cCCnoresp 0x5
1176#define cCCbadpid1 0x6
1177#define cCCbadpid2 0x7
1178#define cCCdataoverrun 0x8
1179#define cCCdataunderrun 0x9
1180#define cCCbuffoverrun 0xC
1181#define cCCbuffunderrun 0xD
1182#define cCCnotaccessed 0xF
1183static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1184{
1185wait:if (ftdi->disconnected > 0) {
1186 return -ENODEV;
1187 } else {
1188 int command_size;
1189 mutex_lock(&ftdi->u132_lock);
1190 command_size = ftdi->command_next - ftdi->command_head;
1191 if (command_size < COMMAND_SIZE) {
1192 struct u132_command *command = &ftdi->command[
1193 COMMAND_MASK & ftdi->command_next];
1194 command->header = 0x00 | cPCIu132wr;
1195 command->length = 0x04;
1196 command->address = 0x00;
1197 command->width = 0x00;
1198 command->follows = 4;
1199 command->value = data;
1200 command->buffer = &command->value;
1201 ftdi->command_next += 1;
1202 ftdi_elan_kick_command_queue(ftdi);
1203 mutex_unlock(&ftdi->u132_lock);
1204 return 0;
1205 } else {
1206 mutex_unlock(&ftdi->u132_lock);
1207 msleep(100);
1208 goto wait;
1209 }
1210 }
1211}
1212
1213static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1214 u8 width, u32 data)
1215{
1216 u8 addressofs = config_offset / 4;
1217wait:if (ftdi->disconnected > 0) {
1218 return -ENODEV;
1219 } else {
1220 int command_size;
1221 mutex_lock(&ftdi->u132_lock);
1222 command_size = ftdi->command_next - ftdi->command_head;
1223 if (command_size < COMMAND_SIZE) {
1224 struct u132_command *command = &ftdi->command[
1225 COMMAND_MASK & ftdi->command_next];
1226 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1227 command->length = 0x04;
1228 command->address = addressofs;
1229 command->width = 0x00 | (width & 0x0F);
1230 command->follows = 4;
1231 command->value = data;
1232 command->buffer = &command->value;
1233 ftdi->command_next += 1;
1234 ftdi_elan_kick_command_queue(ftdi);
1235 mutex_unlock(&ftdi->u132_lock);
1236 return 0;
1237 } else {
1238 mutex_unlock(&ftdi->u132_lock);
1239 msleep(100);
1240 goto wait;
1241 }
1242 }
1243}
1244
1245static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1246 u8 width, u32 data)
1247{
1248 u8 addressofs = mem_offset / 4;
1249wait:if (ftdi->disconnected > 0) {
1250 return -ENODEV;
1251 } else {
1252 int command_size;
1253 mutex_lock(&ftdi->u132_lock);
1254 command_size = ftdi->command_next - ftdi->command_head;
1255 if (command_size < COMMAND_SIZE) {
1256 struct u132_command *command = &ftdi->command[
1257 COMMAND_MASK & ftdi->command_next];
1258 command->header = 0x00 | (cPCImemwr & 0x0F);
1259 command->length = 0x04;
1260 command->address = addressofs;
1261 command->width = 0x00 | (width & 0x0F);
1262 command->follows = 4;
1263 command->value = data;
1264 command->buffer = &command->value;
1265 ftdi->command_next += 1;
1266 ftdi_elan_kick_command_queue(ftdi);
1267 mutex_unlock(&ftdi->u132_lock);
1268 return 0;
1269 } else {
1270 mutex_unlock(&ftdi->u132_lock);
1271 msleep(100);
1272 goto wait;
1273 }
1274 }
1275}
1276
1277int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1278 u8 width, u32 data)
1279{
1280 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1281 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1282}
1283
1284
1285EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1286static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1287{
1288wait:if (ftdi->disconnected > 0) {
1289 return -ENODEV;
1290 } else {
1291 int command_size;
1292 int respond_size;
1293 mutex_lock(&ftdi->u132_lock);
1294 command_size = ftdi->command_next - ftdi->command_head;
1295 respond_size = ftdi->respond_next - ftdi->respond_head;
1296 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1297 {
1298 struct u132_command *command = &ftdi->command[
1299 COMMAND_MASK & ftdi->command_next];
1300 struct u132_respond *respond = &ftdi->respond[
1301 RESPOND_MASK & ftdi->respond_next];
1302 int result = -ENODEV;
1303 respond->result = &result;
1304 respond->header = command->header = 0x00 | cPCIu132rd;
1305 command->length = 0x04;
1306 respond->address = command->address = cU132cmd_status;
1307 command->width = 0x00;
1308 command->follows = 0;
1309 command->value = 0;
1310 command->buffer = NULL;
1311 respond->value = data;
1312 init_completion(&respond->wait_completion);
1313 ftdi->command_next += 1;
1314 ftdi->respond_next += 1;
1315 ftdi_elan_kick_command_queue(ftdi);
1316 mutex_unlock(&ftdi->u132_lock);
1317 wait_for_completion(&respond->wait_completion);
1318 return result;
1319 } else {
1320 mutex_unlock(&ftdi->u132_lock);
1321 msleep(100);
1322 goto wait;
1323 }
1324 }
1325}
1326
1327static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1328 u8 width, u32 *data)
1329{
1330 u8 addressofs = config_offset / 4;
1331wait:if (ftdi->disconnected > 0) {
1332 return -ENODEV;
1333 } else {
1334 int command_size;
1335 int respond_size;
1336 mutex_lock(&ftdi->u132_lock);
1337 command_size = ftdi->command_next - ftdi->command_head;
1338 respond_size = ftdi->respond_next - ftdi->respond_head;
1339 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1340 {
1341 struct u132_command *command = &ftdi->command[
1342 COMMAND_MASK & ftdi->command_next];
1343 struct u132_respond *respond = &ftdi->respond[
1344 RESPOND_MASK & ftdi->respond_next];
1345 int result = -ENODEV;
1346 respond->result = &result;
1347 respond->header = command->header = 0x00 | (cPCIcfgrd &
1348 0x0F);
1349 command->length = 0x04;
1350 respond->address = command->address = addressofs;
1351 command->width = 0x00 | (width & 0x0F);
1352 command->follows = 0;
1353 command->value = 0;
1354 command->buffer = NULL;
1355 respond->value = data;
1356 init_completion(&respond->wait_completion);
1357 ftdi->command_next += 1;
1358 ftdi->respond_next += 1;
1359 ftdi_elan_kick_command_queue(ftdi);
1360 mutex_unlock(&ftdi->u132_lock);
1361 wait_for_completion(&respond->wait_completion);
1362 return result;
1363 } else {
1364 mutex_unlock(&ftdi->u132_lock);
1365 msleep(100);
1366 goto wait;
1367 }
1368 }
1369}
1370
1371static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1372 u8 width, u32 *data)
1373{
1374 u8 addressofs = mem_offset / 4;
1375wait:if (ftdi->disconnected > 0) {
1376 return -ENODEV;
1377 } else {
1378 int command_size;
1379 int respond_size;
1380 mutex_lock(&ftdi->u132_lock);
1381 command_size = ftdi->command_next - ftdi->command_head;
1382 respond_size = ftdi->respond_next - ftdi->respond_head;
1383 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1384 {
1385 struct u132_command *command = &ftdi->command[
1386 COMMAND_MASK & ftdi->command_next];
1387 struct u132_respond *respond = &ftdi->respond[
1388 RESPOND_MASK & ftdi->respond_next];
1389 int result = -ENODEV;
1390 respond->result = &result;
1391 respond->header = command->header = 0x00 | (cPCImemrd &
1392 0x0F);
1393 command->length = 0x04;
1394 respond->address = command->address = addressofs;
1395 command->width = 0x00 | (width & 0x0F);
1396 command->follows = 0;
1397 command->value = 0;
1398 command->buffer = NULL;
1399 respond->value = data;
1400 init_completion(&respond->wait_completion);
1401 ftdi->command_next += 1;
1402 ftdi->respond_next += 1;
1403 ftdi_elan_kick_command_queue(ftdi);
1404 mutex_unlock(&ftdi->u132_lock);
1405 wait_for_completion(&respond->wait_completion);
1406 return result;
1407 } else {
1408 mutex_unlock(&ftdi->u132_lock);
1409 msleep(100);
1410 goto wait;
1411 }
1412 }
1413}
1414
1415int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1416 u8 width, u32 *data)
1417{
1418 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1419 if (ftdi->initialized == 0) {
1420 return -ENODEV;
1421 } else
1422 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1423}
1424
1425
1426EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1427static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1428 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1429 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1430 int toggle_bits, int error_count, int condition_code, int repeat_number,
1431 int halted, int skipped, int actual, int non_null))
1432{
1433 u8 ed = ed_number - 1;
1434wait:if (ftdi->disconnected > 0) {
1435 return -ENODEV;
1436 } else if (ftdi->initialized == 0) {
1437 return -ENODEV;
1438 } else {
1439 int command_size;
1440 mutex_lock(&ftdi->u132_lock);
1441 command_size = ftdi->command_next - ftdi->command_head;
1442 if (command_size < COMMAND_SIZE) {
1443 struct u132_target *target = &ftdi->target[ed];
1444 struct u132_command *command = &ftdi->command[
1445 COMMAND_MASK & ftdi->command_next];
1446 command->header = 0x80 | (ed << 5);
1447 command->length = 0x8007;
1448 command->address = (toggle_bits << 6) | (ep_number << 2)
1449 | (address << 0);
1450 command->width = usb_maxpacket(urb->dev, urb->pipe,
1451 usb_pipeout(urb->pipe));
1452 command->follows = 8;
1453 command->value = 0;
1454 command->buffer = urb->setup_packet;
1455 target->callback = callback;
1456 target->endp = endp;
1457 target->urb = urb;
1458 target->active = 1;
1459 ftdi->command_next += 1;
1460 ftdi_elan_kick_command_queue(ftdi);
1461 mutex_unlock(&ftdi->u132_lock);
1462 return 0;
1463 } else {
1464 mutex_unlock(&ftdi->u132_lock);
1465 msleep(100);
1466 goto wait;
1467 }
1468 }
1469}
1470
1471int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1472 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1473 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1474 int toggle_bits, int error_count, int condition_code, int repeat_number,
1475 int halted, int skipped, int actual, int non_null))
1476{
1477 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1478 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1479 ep_number, toggle_bits, callback);
1480}
1481
1482
1483EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1484static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1485 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1486 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1487 int toggle_bits, int error_count, int condition_code, int repeat_number,
1488 int halted, int skipped, int actual, int non_null))
1489{
1490 u8 ed = ed_number - 1;
1491wait:if (ftdi->disconnected > 0) {
1492 return -ENODEV;
1493 } else if (ftdi->initialized == 0) {
1494 return -ENODEV;
1495 } else {
1496 int command_size;
1497 mutex_lock(&ftdi->u132_lock);
1498 command_size = ftdi->command_next - ftdi->command_head;
1499 if (command_size < COMMAND_SIZE) {
1500 struct u132_target *target = &ftdi->target[ed];
1501 struct u132_command *command = &ftdi->command[
1502 COMMAND_MASK & ftdi->command_next];
1503 u32 remaining_length = urb->transfer_buffer_length -
1504 urb->actual_length;
1505 command->header = 0x82 | (ed << 5);
1506 if (remaining_length == 0) {
1507 command->length = 0x0000;
1508 } else if (remaining_length > 1024) {
1509 command->length = 0x8000 | 1023;
1510 } else
1511 command->length = 0x8000 | (remaining_length -
1512 1);
1513 command->address = (toggle_bits << 6) | (ep_number << 2)
1514 | (address << 0);
1515 command->width = usb_maxpacket(urb->dev, urb->pipe,
1516 usb_pipeout(urb->pipe));
1517 command->follows = 0;
1518 command->value = 0;
1519 command->buffer = NULL;
1520 target->callback = callback;
1521 target->endp = endp;
1522 target->urb = urb;
1523 target->active = 1;
1524 ftdi->command_next += 1;
1525 ftdi_elan_kick_command_queue(ftdi);
1526 mutex_unlock(&ftdi->u132_lock);
1527 return 0;
1528 } else {
1529 mutex_unlock(&ftdi->u132_lock);
1530 msleep(100);
1531 goto wait;
1532 }
1533 }
1534}
1535
1536int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1537 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1538 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1539 int toggle_bits, int error_count, int condition_code, int repeat_number,
1540 int halted, int skipped, int actual, int non_null))
1541{
1542 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1543 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1544 ep_number, toggle_bits, callback);
1545}
1546
1547
1548EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1549static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1550 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1551 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1552 int toggle_bits, int error_count, int condition_code, int repeat_number,
1553 int halted, int skipped, int actual, int non_null))
1554{
1555 u8 ed = ed_number - 1;
1556wait:if (ftdi->disconnected > 0) {
1557 return -ENODEV;
1558 } else if (ftdi->initialized == 0) {
1559 return -ENODEV;
1560 } else {
1561 int command_size;
1562 mutex_lock(&ftdi->u132_lock);
1563 command_size = ftdi->command_next - ftdi->command_head;
1564 if (command_size < COMMAND_SIZE) {
1565 struct u132_target *target = &ftdi->target[ed];
1566 struct u132_command *command = &ftdi->command[
1567 COMMAND_MASK & ftdi->command_next];
1568 command->header = 0x81 | (ed << 5);
1569 command->length = 0x0000;
1570 command->address = (toggle_bits << 6) | (ep_number << 2)
1571 | (address << 0);
1572 command->width = usb_maxpacket(urb->dev, urb->pipe,
1573 usb_pipeout(urb->pipe));
1574 command->follows = 0;
1575 command->value = 0;
1576 command->buffer = NULL;
1577 target->callback = callback;
1578 target->endp = endp;
1579 target->urb = urb;
1580 target->active = 1;
1581 ftdi->command_next += 1;
1582 ftdi_elan_kick_command_queue(ftdi);
1583 mutex_unlock(&ftdi->u132_lock);
1584 return 0;
1585 } else {
1586 mutex_unlock(&ftdi->u132_lock);
1587 msleep(100);
1588 goto wait;
1589 }
1590 }
1591}
1592
1593int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1594 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1595 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1596 int toggle_bits, int error_count, int condition_code, int repeat_number,
1597 int halted, int skipped, int actual, int non_null))
1598{
1599 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1600 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1601 ep_number, toggle_bits, callback);
1602}
1603
1604
1605EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1606static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1607 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1608 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1609 int toggle_bits, int error_count, int condition_code, int repeat_number,
1610 int halted, int skipped, int actual, int non_null))
1611{
1612 u8 ed = ed_number - 1;
1613wait:if (ftdi->disconnected > 0) {
1614 return -ENODEV;
1615 } else if (ftdi->initialized == 0) {
1616 return -ENODEV;
1617 } else {
1618 int command_size;
1619 mutex_lock(&ftdi->u132_lock);
1620 command_size = ftdi->command_next - ftdi->command_head;
1621 if (command_size < COMMAND_SIZE) {
1622 u8 *b;
1623 u16 urb_size;
1624 int i = 0;
1625 char data[30 *3 + 4];
1626 char *d = data;
1627 int m = (sizeof(data) - 1) / 3 - 1;
1628 int l = 0;
1629 struct u132_target *target = &ftdi->target[ed];
1630 struct u132_command *command = &ftdi->command[
1631 COMMAND_MASK & ftdi->command_next];
1632 command->header = 0x81 | (ed << 5);
1633 command->address = (toggle_bits << 6) | (ep_number << 2)
1634 | (address << 0);
1635 command->width = usb_maxpacket(urb->dev, urb->pipe,
1636 usb_pipeout(urb->pipe));
1637 command->follows = min_t(u32, 1024,
1638 urb->transfer_buffer_length -
1639 urb->actual_length);
1640 command->value = 0;
1641 command->buffer = urb->transfer_buffer +
1642 urb->actual_length;
1643 command->length = 0x8000 | (command->follows - 1);
1644 b = command->buffer;
1645 urb_size = command->follows;
1646 data[0] = 0;
1647 while (urb_size-- > 0) {
1648 if (i > m) {
1649 } else if (i++ < m) {
1650 int w = sprintf(d, " %02X", *b++);
1651 d += w;
1652 l += w;
1653 } else
1654 d += sprintf(d, " ..");
1655 }
1656 target->callback = callback;
1657 target->endp = endp;
1658 target->urb = urb;
1659 target->active = 1;
1660 ftdi->command_next += 1;
1661 ftdi_elan_kick_command_queue(ftdi);
1662 mutex_unlock(&ftdi->u132_lock);
1663 return 0;
1664 } else {
1665 mutex_unlock(&ftdi->u132_lock);
1666 msleep(100);
1667 goto wait;
1668 }
1669 }
1670}
1671
1672int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1673 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1674 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1675 int toggle_bits, int error_count, int condition_code, int repeat_number,
1676 int halted, int skipped, int actual, int non_null))
1677{
1678 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1679 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1680 ep_number, toggle_bits, callback);
1681}
1682
1683
1684EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1685static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1686 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1687 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1688 int toggle_bits, int error_count, int condition_code, int repeat_number,
1689 int halted, int skipped, int actual, int non_null))
1690{
1691 u8 ed = ed_number - 1;
1692wait:if (ftdi->disconnected > 0) {
1693 return -ENODEV;
1694 } else if (ftdi->initialized == 0) {
1695 return -ENODEV;
1696 } else {
1697 int command_size;
1698 mutex_lock(&ftdi->u132_lock);
1699 command_size = ftdi->command_next - ftdi->command_head;
1700 if (command_size < COMMAND_SIZE) {
1701 u32 remaining_length = urb->transfer_buffer_length -
1702 urb->actual_length;
1703 struct u132_target *target = &ftdi->target[ed];
1704 struct u132_command *command = &ftdi->command[
1705 COMMAND_MASK & ftdi->command_next];
1706 command->header = 0x83 | (ed << 5);
1707 if (remaining_length == 0) {
1708 command->length = 0x0000;
1709 } else if (remaining_length > 1024) {
1710 command->length = 0x8000 | 1023;
1711 } else
1712 command->length = 0x8000 | (remaining_length -
1713 1);
1714 command->address = (toggle_bits << 6) | (ep_number << 2)
1715 | (address << 0);
1716 command->width = usb_maxpacket(urb->dev, urb->pipe,
1717 usb_pipeout(urb->pipe));
1718 command->follows = 0;
1719 command->value = 0;
1720 command->buffer = NULL;
1721 target->callback = callback;
1722 target->endp = endp;
1723 target->urb = urb;
1724 target->active = 1;
1725 ftdi->command_next += 1;
1726 ftdi_elan_kick_command_queue(ftdi);
1727 mutex_unlock(&ftdi->u132_lock);
1728 return 0;
1729 } else {
1730 mutex_unlock(&ftdi->u132_lock);
1731 msleep(100);
1732 goto wait;
1733 }
1734 }
1735}
1736
1737int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1738 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1739 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1740 int toggle_bits, int error_count, int condition_code, int repeat_number,
1741 int halted, int skipped, int actual, int non_null))
1742{
1743 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1744 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1745 ep_number, toggle_bits, callback);
1746}
1747
1748
1749EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1750static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1751 void *endp)
1752{
1753 u8 ed = ed_number - 1;
1754 if (ftdi->disconnected > 0) {
1755 return -ENODEV;
1756 } else if (ftdi->initialized == 0) {
1757 return -ENODEV;
1758 } else {
1759 struct u132_target *target = &ftdi->target[ed];
1760 mutex_lock(&ftdi->u132_lock);
1761 if (target->abandoning > 0) {
1762 mutex_unlock(&ftdi->u132_lock);
1763 return 0;
1764 } else {
1765 target->abandoning = 1;
1766 wait_1:if (target->active == 1) {
1767 int command_size = ftdi->command_next -
1768 ftdi->command_head;
1769 if (command_size < COMMAND_SIZE) {
1770 struct u132_command *command =
1771 &ftdi->command[COMMAND_MASK &
1772 ftdi->command_next];
1773 command->header = 0x80 | (ed << 5) |
1774 0x4;
1775 command->length = 0x00;
1776 command->address = 0x00;
1777 command->width = 0x00;
1778 command->follows = 0;
1779 command->value = 0;
1780 command->buffer = &command->value;
1781 ftdi->command_next += 1;
1782 ftdi_elan_kick_command_queue(ftdi);
1783 } else {
1784 mutex_unlock(&ftdi->u132_lock);
1785 msleep(100);
1786 mutex_lock(&ftdi->u132_lock);
1787 goto wait_1;
1788 }
1789 }
1790 mutex_unlock(&ftdi->u132_lock);
1791 return 0;
1792 }
1793 }
1794}
1795
1796int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1797 void *endp)
1798{
1799 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1800 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1801}
1802
1803
1804EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1805static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1806{
1807 int retry_on_empty = 10;
1808 int retry_on_timeout = 5;
1809 int retry_on_status = 20;
1810more:{
1811 int packet_bytes = 0;
1812 int retval = usb_bulk_msg(ftdi->udev,
1813 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1814 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1815 &packet_bytes, 100);
1816 if (packet_bytes > 2) {
1817 char diag[30 *3 + 4];
1818 char *d = diag;
1819 int m = (sizeof(diag) - 1) / 3 - 1;
1820 char *b = ftdi->bulk_in_buffer;
1821 int bytes_read = 0;
1822 diag[0] = 0;
1823 while (packet_bytes-- > 0) {
1824 char c = *b++;
1825 if (bytes_read < m) {
1826 d += sprintf(d, " %02X",
1827 0x000000FF & c);
1828 } else if (bytes_read > m) {
1829 } else
1830 d += sprintf(d, " ..");
1831 bytes_read += 1;
1832 continue;
1833 }
1834 goto more;
1835 } else if (packet_bytes > 1) {
1836 char s1 = ftdi->bulk_in_buffer[0];
1837 char s2 = ftdi->bulk_in_buffer[1];
1838 if (s1 == 0x31 && s2 == 0x60) {
1839 return 0;
1840 } else if (retry_on_status-- > 0) {
1841 goto more;
1842 } else {
1843 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1844 return -EFAULT;
1845 }
1846 } else if (packet_bytes > 0) {
1847 char b1 = ftdi->bulk_in_buffer[0];
1848 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1849 b1);
1850 if (retry_on_status-- > 0) {
1851 goto more;
1852 } else {
1853 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1854 return -EFAULT;
1855 }
1856 } else if (retval == -ETIMEDOUT) {
1857 if (retry_on_timeout-- > 0) {
1858 goto more;
1859 } else {
1860 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1861 return -ENOMEM;
1862 }
1863 } else if (retval == 0) {
1864 if (retry_on_empty-- > 0) {
1865 goto more;
1866 } else {
1867 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1868 return -ENOMEM;
1869 }
1870 } else {
1871 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1872 return retval;
1873 }
1874 }
1875 return -1;
1876}
1877
1878
1879/*
1880 * send the long flush sequence
1881 *
1882 */
1883static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1884{
1885 int retval;
1886 struct urb *urb;
1887 char *buf;
1888 int I = 257;
1889 int i = 0;
1890 urb = usb_alloc_urb(0, GFP_KERNEL);
1891 if (!urb)
1892 return -ENOMEM;
1893 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1894 if (!buf) {
1895 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1896 usb_free_urb(urb);
1897 return -ENOMEM;
1898 }
1899 while (I-- > 0)
1900 buf[i++] = 0x55;
1901 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1902 ftdi->bulk_out_endpointAddr), buf, i,
1903 ftdi_elan_write_bulk_callback, ftdi);
1904 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1905 retval = usb_submit_urb(urb, GFP_KERNEL);
1906 if (retval) {
1907 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1908 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1909 usb_free_urb(urb);
1910 return -ENOMEM;
1911 }
1912 usb_free_urb(urb);
1913 return 0;
1914}
1915
1916
1917/*
1918 * send the reset sequence
1919 *
1920 */
1921static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1922{
1923 int retval;
1924 struct urb *urb;
1925 char *buf;
1926 int I = 4;
1927 int i = 0;
1928 urb = usb_alloc_urb(0, GFP_KERNEL);
1929 if (!urb)
1930 return -ENOMEM;
1931 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1932 if (!buf) {
1933 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1934 usb_free_urb(urb);
1935 return -ENOMEM;
1936 }
1937 buf[i++] = 0x55;
1938 buf[i++] = 0xAA;
1939 buf[i++] = 0x5A;
1940 buf[i++] = 0xA5;
1941 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1942 ftdi->bulk_out_endpointAddr), buf, i,
1943 ftdi_elan_write_bulk_callback, ftdi);
1944 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1945 retval = usb_submit_urb(urb, GFP_KERNEL);
1946 if (retval) {
1947 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1948 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1949 usb_free_urb(urb);
1950 return -ENOMEM;
1951 }
1952 usb_free_urb(urb);
1953 return 0;
1954}
1955
1956static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1957{
1958 int retval;
1959 int long_stop = 10;
1960 int retry_on_timeout = 5;
1961 int retry_on_empty = 10;
1962 int err_count = 0;
1963 retval = ftdi_elan_flush_input_fifo(ftdi);
1964 if (retval)
1965 return retval;
1966 ftdi->bulk_in_left = 0;
1967 ftdi->bulk_in_last = -1;
1968 while (long_stop-- > 0) {
1969 int read_stop;
1970 int read_stuck;
1971 retval = ftdi_elan_synchronize_flush(ftdi);
1972 if (retval)
1973 return retval;
1974 retval = ftdi_elan_flush_input_fifo(ftdi);
1975 if (retval)
1976 return retval;
1977 reset:retval = ftdi_elan_synchronize_reset(ftdi);
1978 if (retval)
1979 return retval;
1980 read_stop = 100;
1981 read_stuck = 10;
1982 read:{
1983 int packet_bytes = 0;
1984 retval = usb_bulk_msg(ftdi->udev,
1985 usb_rcvbulkpipe(ftdi->udev,
1986 ftdi->bulk_in_endpointAddr),
1987 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1988 &packet_bytes, 500);
1989 if (packet_bytes > 2) {
1990 char diag[30 *3 + 4];
1991 char *d = diag;
1992 int m = (sizeof(diag) - 1) / 3 - 1;
1993 char *b = ftdi->bulk_in_buffer;
1994 int bytes_read = 0;
1995 unsigned char c = 0;
1996 diag[0] = 0;
1997 while (packet_bytes-- > 0) {
1998 c = *b++;
1999 if (bytes_read < m) {
2000 d += sprintf(d, " %02X", c);
2001 } else if (bytes_read > m) {
2002 } else
2003 d += sprintf(d, " ..");
2004 bytes_read += 1;
2005 continue;
2006 }
2007 if (c == 0x7E) {
2008 return 0;
2009 } else {
2010 if (c == 0x55) {
2011 goto read;
2012 } else if (read_stop-- > 0) {
2013 goto read;
2014 } else {
2015 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2016 continue;
2017 }
2018 }
2019 } else if (packet_bytes > 1) {
2020 unsigned char s1 = ftdi->bulk_in_buffer[0];
2021 unsigned char s2 = ftdi->bulk_in_buffer[1];
2022 if (s1 == 0x31 && s2 == 0x00) {
2023 if (read_stuck-- > 0) {
2024 goto read;
2025 } else
2026 goto reset;
2027 } else {
2028 if (read_stop-- > 0) {
2029 goto read;
2030 } else {
2031 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2032 continue;
2033 }
2034 }
2035 } else if (packet_bytes > 0) {
2036 if (read_stop-- > 0) {
2037 goto read;
2038 } else {
2039 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2040 continue;
2041 }
2042 } else if (retval == -ETIMEDOUT) {
2043 if (retry_on_timeout-- > 0) {
2044 goto read;
2045 } else {
2046 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2047 continue;
2048 }
2049 } else if (retval == 0) {
2050 if (retry_on_empty-- > 0) {
2051 goto read;
2052 } else {
2053 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2054 continue;
2055 }
2056 } else {
2057 err_count += 1;
2058 dev_err(&ftdi->udev->dev, "error = %d\n",
2059 retval);
2060 if (read_stop-- > 0) {
2061 goto read;
2062 } else {
2063 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2064 continue;
2065 }
2066 }
2067 }
2068 }
2069 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2070 return -EFAULT;
2071}
2072
2073static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2074{
2075 int retry_on_empty = 10;
2076 int retry_on_timeout = 5;
2077 int retry_on_status = 50;
2078more:{
2079 int packet_bytes = 0;
2080 int retval = usb_bulk_msg(ftdi->udev,
2081 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2082 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2083 &packet_bytes, 1000);
2084 if (packet_bytes > 2) {
2085 char diag[30 *3 + 4];
2086 char *d = diag;
2087 int m = (sizeof(diag) - 1) / 3 - 1;
2088 char *b = ftdi->bulk_in_buffer;
2089 int bytes_read = 0;
2090 diag[0] = 0;
2091 while (packet_bytes-- > 0) {
2092 char c = *b++;
2093 if (bytes_read < m) {
2094 d += sprintf(d, " %02X",
2095 0x000000FF & c);
2096 } else if (bytes_read > m) {
2097 } else
2098 d += sprintf(d, " ..");
2099 bytes_read += 1;
2100 continue;
2101 }
2102 goto more;
2103 } else if (packet_bytes > 1) {
2104 char s1 = ftdi->bulk_in_buffer[0];
2105 char s2 = ftdi->bulk_in_buffer[1];
2106 if (s1 == 0x31 && s2 == 0x60) {
2107 return 0;
2108 } else if (retry_on_status-- > 0) {
2109 msleep(5);
2110 goto more;
2111 } else
2112 return -EFAULT;
2113 } else if (packet_bytes > 0) {
2114 char b1 = ftdi->bulk_in_buffer[0];
2115 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2116 if (retry_on_status-- > 0) {
2117 msleep(5);
2118 goto more;
2119 } else {
2120 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2121 return -EFAULT;
2122 }
2123 } else if (retval == -ETIMEDOUT) {
2124 if (retry_on_timeout-- > 0) {
2125 goto more;
2126 } else {
2127 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2128 return -ENOMEM;
2129 }
2130 } else if (retval == 0) {
2131 if (retry_on_empty-- > 0) {
2132 goto more;
2133 } else {
2134 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2135 return -ENOMEM;
2136 }
2137 } else {
2138 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2139 return -ENOMEM;
2140 }
2141 }
2142 return -1;
2143}
2144
2145static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2146{
2147 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2148 if (UxxxStatus)
2149 return UxxxStatus;
2150 if (ftdi->controlreg & 0x00400000) {
2151 if (ftdi->card_ejected) {
2152 } else {
2153 ftdi->card_ejected = 1;
2154 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2155 ftdi->controlreg);
2156 }
2157 return -ENODEV;
2158 } else {
2159 u8 fn = ftdi->function - 1;
2160 int activePCIfn = fn << 8;
2161 u32 pcidata;
2162 u32 pciVID;
2163 u32 pciPID;
2164 int reg = 0;
2165 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2166 &pcidata);
2167 if (UxxxStatus)
2168 return UxxxStatus;
2169 pciVID = pcidata & 0xFFFF;
2170 pciPID = (pcidata >> 16) & 0xFFFF;
2171 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2172 ftdi->platform_data.device) {
2173 return 0;
2174 } else {
2175 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2176 ftdi->platform_data.vendor, pciVID,
2177 ftdi->platform_data.device, pciPID);
2178 return -ENODEV;
2179 }
2180 }
2181}
2182
2183
2184#define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2185 offsetof(struct ohci_regs, member), 0, data);
2186#define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2187 offsetof(struct ohci_regs, member), 0, data);
2188
2189#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2190#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2191 OHCI_INTR_WDH)
2192static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2193{
2194 int devices = 0;
2195 int retval;
2196 u32 hc_control;
2197 int num_ports;
2198 u32 control;
2199 u32 rh_a = -1;
2200 u32 status;
2201 u32 fminterval;
2202 u32 hc_fminterval;
2203 u32 periodicstart;
2204 u32 cmdstatus;
2205 u32 roothub_a;
2206 int mask = OHCI_INTR_INIT;
2207 int sleep_time = 0;
2208 int reset_timeout = 30; /* ... allow extra time */
2209 int temp;
2210 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2211 if (retval)
2212 return retval;
2213 retval = ftdi_read_pcimem(ftdi, control, &control);
2214 if (retval)
2215 return retval;
2216 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2217 if (retval)
2218 return retval;
2219 num_ports = rh_a & RH_A_NDP;
2220 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2221 if (retval)
2222 return retval;
2223 hc_fminterval &= 0x3fff;
2224 if (hc_fminterval != FI) {
2225 }
2226 hc_fminterval |= FSMP(hc_fminterval) << 16;
2227 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2228 if (retval)
2229 return retval;
2230 switch (hc_control & OHCI_CTRL_HCFS) {
2231 case OHCI_USB_OPER:
2232 sleep_time = 0;
2233 break;
2234 case OHCI_USB_SUSPEND:
2235 case OHCI_USB_RESUME:
2236 hc_control &= OHCI_CTRL_RWC;
2237 hc_control |= OHCI_USB_RESUME;
2238 sleep_time = 10;
2239 break;
2240 default:
2241 hc_control &= OHCI_CTRL_RWC;
2242 hc_control |= OHCI_USB_RESET;
2243 sleep_time = 50;
2244 break;
2245 }
2246 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2247 if (retval)
2248 return retval;
2249 retval = ftdi_read_pcimem(ftdi, control, &control);
2250 if (retval)
2251 return retval;
2252 msleep(sleep_time);
2253 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2254 if (retval)
2255 return retval;
2256 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2257 for (temp = 0; temp < num_ports; temp++) {
2258 retval = ftdi_write_pcimem(ftdi,
2259 roothub.portstatus[temp], RH_PS_LSDA);
2260 if (retval)
2261 return retval;
2262 }
2263 }
2264 retval = ftdi_read_pcimem(ftdi, control, &control);
2265 if (retval)
2266 return retval;
2267retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2268 if (retval)
2269 return retval;
2270 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2271 if (retval)
2272 return retval;
2273extra:{
2274 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2275 if (retval)
2276 return retval;
2277 if (0 != (status & OHCI_HCR)) {
2278 if (--reset_timeout == 0) {
2279 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2280 return -ENODEV;
2281 } else {
2282 msleep(5);
2283 goto extra;
2284 }
2285 }
2286 }
2287 if (quirk & OHCI_QUIRK_INITRESET) {
2288 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2289 if (retval)
2290 return retval;
2291 retval = ftdi_read_pcimem(ftdi, control, &control);
2292 if (retval)
2293 return retval;
2294 }
2295 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2296 if (retval)
2297 return retval;
2298 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2299 if (retval)
2300 return retval;
2301 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2302 if (retval)
2303 return retval;
2304 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2305 if (retval)
2306 return retval;
2307 retval = ftdi_write_pcimem(ftdi, fminterval,
2308 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2309 if (retval)
2310 return retval;
2311 retval = ftdi_write_pcimem(ftdi, periodicstart,
2312 ((9 *hc_fminterval) / 10) & 0x3fff);
2313 if (retval)
2314 return retval;
2315 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2316 if (retval)
2317 return retval;
2318 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2319 if (retval)
2320 return retval;
2321 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2322 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2323 quirk |= OHCI_QUIRK_INITRESET;
2324 goto retry;
2325 } else
2326 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2327 fminterval, periodicstart);
2328 } /* start controller operations */
2329 hc_control &= OHCI_CTRL_RWC;
2330 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2331 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2332 if (retval)
2333 return retval;
2334 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2335 if (retval)
2336 return retval;
2337 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2338 if (retval)
2339 return retval;
2340 retval = ftdi_read_pcimem(ftdi, control, &control);
2341 if (retval)
2342 return retval;
2343 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2344 if (retval)
2345 return retval;
2346 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2347 if (retval)
2348 return retval;
2349 retval = ftdi_write_pcimem(ftdi, intrdisable,
2350 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2351 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2352 OHCI_INTR_SO);
2353 if (retval)
2354 return retval; /* handle root hub init quirks ... */
2355 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2356 if (retval)
2357 return retval;
2358 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2359 if (quirk & OHCI_QUIRK_SUPERIO) {
2360 roothub_a |= RH_A_NOCP;
2361 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2362 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2363 if (retval)
2364 return retval;
2365 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2366 roothub_a |= RH_A_NPS;
2367 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2368 if (retval)
2369 return retval;
2370 }
2371 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2372 if (retval)
2373 return retval;
2374 retval = ftdi_write_pcimem(ftdi, roothub.b,
2375 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2376 if (retval)
2377 return retval;
2378 retval = ftdi_read_pcimem(ftdi, control, &control);
2379 if (retval)
2380 return retval;
2381 mdelay((roothub_a >> 23) & 0x1fe);
2382 for (temp = 0; temp < num_ports; temp++) {
2383 u32 portstatus;
2384 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2385 &portstatus);
2386 if (retval)
2387 return retval;
2388 if (1 & portstatus)
2389 devices += 1;
2390 }
2391 return devices;
2392}
2393
2394static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2395{
2396 u32 latence_timer;
2397 int UxxxStatus;
2398 u32 pcidata;
2399 int reg = 0;
2400 int activePCIfn = fn << 8;
2401 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2402 if (UxxxStatus)
2403 return UxxxStatus;
2404 reg = 16;
2405 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2406 0xFFFFFFFF);
2407 if (UxxxStatus)
2408 return UxxxStatus;
2409 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2410 &pcidata);
2411 if (UxxxStatus)
2412 return UxxxStatus;
2413 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2414 0xF0000000);
2415 if (UxxxStatus)
2416 return UxxxStatus;
2417 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2418 &pcidata);
2419 if (UxxxStatus)
2420 return UxxxStatus;
2421 reg = 12;
2422 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2423 &latence_timer);
2424 if (UxxxStatus)
2425 return UxxxStatus;
2426 latence_timer &= 0xFFFF00FF;
2427 latence_timer |= 0x00001600;
2428 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2429 latence_timer);
2430 if (UxxxStatus)
2431 return UxxxStatus;
2432 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2433 &pcidata);
2434 if (UxxxStatus)
2435 return UxxxStatus;
2436 reg = 4;
2437 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2438 0x06);
2439 if (UxxxStatus)
2440 return UxxxStatus;
2441 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2442 &pcidata);
2443 if (UxxxStatus)
2444 return UxxxStatus;
2445 for (reg = 0; reg <= 0x54; reg += 4) {
2446 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2447 if (UxxxStatus)
2448 return UxxxStatus;
2449 }
2450 return 0;
2451}
2452
2453static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2454{
2455 u32 latence_timer;
2456 int UxxxStatus;
2457 u32 pcidata;
2458 int reg = 0;
2459 int activePCIfn = fn << 8;
2460 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2461 if (UxxxStatus)
2462 return UxxxStatus;
2463 reg = 16;
2464 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2465 0xFFFFFFFF);
2466 if (UxxxStatus)
2467 return UxxxStatus;
2468 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2469 &pcidata);
2470 if (UxxxStatus)
2471 return UxxxStatus;
2472 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2473 0x00000000);
2474 if (UxxxStatus)
2475 return UxxxStatus;
2476 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2477 &pcidata);
2478 if (UxxxStatus)
2479 return UxxxStatus;
2480 reg = 12;
2481 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2482 &latence_timer);
2483 if (UxxxStatus)
2484 return UxxxStatus;
2485 latence_timer &= 0xFFFF00FF;
2486 latence_timer |= 0x00001600;
2487 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2488 latence_timer);
2489 if (UxxxStatus)
2490 return UxxxStatus;
2491 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2492 &pcidata);
2493 if (UxxxStatus)
2494 return UxxxStatus;
2495 reg = 4;
2496 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2497 0x00);
2498 if (UxxxStatus)
2499 return UxxxStatus;
2500 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2501}
2502
2503static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2504{
2505 int result;
2506 int UxxxStatus;
2507 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2508 if (UxxxStatus)
2509 return UxxxStatus;
2510 result = ftdi_elan_check_controller(ftdi, quirk);
2511 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2512 if (UxxxStatus)
2513 return UxxxStatus;
2514 return result;
2515}
2516
2517static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2518{
2519 u32 controlreg;
2520 u8 sensebits;
2521 int UxxxStatus;
2522 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2523 if (UxxxStatus)
2524 return UxxxStatus;
2525 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2526 if (UxxxStatus)
2527 return UxxxStatus;
2528 msleep(750);
2529 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2530 if (UxxxStatus)
2531 return UxxxStatus;
2532 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2533 if (UxxxStatus)
2534 return UxxxStatus;
2535 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2536 if (UxxxStatus)
2537 return UxxxStatus;
2538 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2539 if (UxxxStatus)
2540 return UxxxStatus;
2541 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2542 if (UxxxStatus)
2543 return UxxxStatus;
2544 msleep(250);
2545 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2546 if (UxxxStatus)
2547 return UxxxStatus;
2548 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2549 if (UxxxStatus)
2550 return UxxxStatus;
2551 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2552 if (UxxxStatus)
2553 return UxxxStatus;
2554 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2555 if (UxxxStatus)
2556 return UxxxStatus;
2557 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2558 if (UxxxStatus)
2559 return UxxxStatus;
2560 msleep(1000);
2561 sensebits = (controlreg >> 16) & 0x000F;
2562 if (0x0D == sensebits)
2563 return 0;
2564 else
2565 return - ENXIO;
2566}
2567
2568static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2569{
2570 int UxxxStatus;
2571 u32 pcidata;
2572 int reg = 0;
2573 u8 fn;
2574 int activePCIfn = 0;
2575 int max_devices = 0;
2576 int controllers = 0;
2577 int unrecognized = 0;
2578 ftdi->function = 0;
2579 for (fn = 0; (fn < 4); fn++) {
2580 u32 pciVID = 0;
2581 u32 pciPID = 0;
2582 int devices = 0;
2583 activePCIfn = fn << 8;
2584 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2585 &pcidata);
2586 if (UxxxStatus)
2587 return UxxxStatus;
2588 pciVID = pcidata & 0xFFFF;
2589 pciPID = (pcidata >> 16) & 0xFFFF;
2590 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2591 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2592 controllers += 1;
2593 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2594 {
2595 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2596 controllers += 1;
2597 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2598 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2599 controllers += 1;
2600 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2601 {
2602 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2603 controllers += 1;
2604 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2605 devices = ftdi_elan_found_controller(ftdi, fn,
2606 OHCI_QUIRK_AMD756);
2607 controllers += 1;
2608 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2609 devices = ftdi_elan_found_controller(ftdi, fn,
2610 OHCI_QUIRK_ZFMICRO);
2611 controllers += 1;
2612 } else if (0 == pcidata) {
2613 } else
2614 unrecognized += 1;
2615 if (devices > max_devices) {
2616 max_devices = devices;
2617 ftdi->function = fn + 1;
2618 ftdi->platform_data.vendor = pciVID;
2619 ftdi->platform_data.device = pciPID;
2620 }
2621 }
2622 if (ftdi->function > 0) {
2623 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2624 } else if (controllers > 0) {
2625 return -ENXIO;
2626 } else if (unrecognized > 0) {
2627 return -ENXIO;
2628 } else {
2629 ftdi->enumerated = 0;
2630 return -ENXIO;
2631 }
2632}
2633
2634
2635/*
2636 * we use only the first bulk-in and bulk-out endpoints
2637 */
2638static int ftdi_elan_probe(struct usb_interface *interface,
2639 const struct usb_device_id *id)
2640{
2641 struct usb_host_interface *iface_desc;
2642 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2643 int retval;
2644 struct usb_ftdi *ftdi;
2645
2646 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2647 if (!ftdi)
2648 return -ENOMEM;
2649
2650 mutex_lock(&ftdi_module_lock);
2651 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2652 ftdi->sequence_num = ++ftdi_instances;
2653 mutex_unlock(&ftdi_module_lock);
2654 ftdi_elan_init_kref(ftdi);
2655 sema_init(&ftdi->sw_lock, 1);
2656 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2657 ftdi->interface = interface;
2658 mutex_init(&ftdi->u132_lock);
2659 ftdi->expected = 4;
2660
2661 iface_desc = interface->cur_altsetting;
2662 retval = usb_find_common_endpoints(iface_desc,
2663 &bulk_in, &bulk_out, NULL, NULL);
2664 if (retval) {
2665 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2666 goto error;
2667 }
2668
2669 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2670 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2671 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2672 if (!ftdi->bulk_in_buffer) {
2673 retval = -ENOMEM;
2674 goto error;
2675 }
2676
2677 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2678
2679 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2680 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2681 ftdi->bulk_out_endpointAddr);
2682 usb_set_intfdata(interface, ftdi);
2683 if (iface_desc->desc.bInterfaceNumber == 0 &&
2684 ftdi->bulk_in_endpointAddr == 0x81 &&
2685 ftdi->bulk_out_endpointAddr == 0x02) {
2686 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2687 if (retval) {
2688 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2689 usb_set_intfdata(interface, NULL);
2690 retval = -ENOMEM;
2691 goto error;
2692 } else {
2693 ftdi->class = &ftdi_elan_jtag_class;
2694 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2695 ftdi, iface_desc->desc.bInterfaceNumber,
2696 interface->minor);
2697 return 0;
2698 }
2699 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2700 ftdi->bulk_in_endpointAddr == 0x83 &&
2701 ftdi->bulk_out_endpointAddr == 0x04) {
2702 ftdi->class = NULL;
2703 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2704 ftdi, iface_desc->desc.bInterfaceNumber);
2705 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2706 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2707 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2708 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2709 return 0;
2710 } else {
2711 dev_err(&ftdi->udev->dev,
2712 "Could not find ELAN's U132 device\n");
2713 retval = -ENODEV;
2714 goto error;
2715 }
2716error:if (ftdi) {
2717 ftdi_elan_put_kref(ftdi);
2718 }
2719 return retval;
2720}
2721
2722static void ftdi_elan_disconnect(struct usb_interface *interface)
2723{
2724 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2725 ftdi->disconnected += 1;
2726 if (ftdi->class) {
2727 int minor = interface->minor;
2728 struct usb_class_driver *class = ftdi->class;
2729 usb_set_intfdata(interface, NULL);
2730 usb_deregister_dev(interface, class);
2731 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2732 minor);
2733 } else {
2734 ftdi_status_cancel_work(ftdi);
2735 ftdi_command_cancel_work(ftdi);
2736 ftdi_response_cancel_work(ftdi);
2737 ftdi_elan_abandon_completions(ftdi);
2738 ftdi_elan_abandon_targets(ftdi);
2739 if (ftdi->registered) {
2740 platform_device_unregister(&ftdi->platform_dev);
2741 ftdi->synchronized = 0;
2742 ftdi->enumerated = 0;
2743 ftdi->initialized = 0;
2744 ftdi->registered = 0;
2745 }
2746 ftdi->disconnected += 1;
2747 usb_set_intfdata(interface, NULL);
2748 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2749 }
2750 ftdi_elan_put_kref(ftdi);
2751}
2752
2753static struct usb_driver ftdi_elan_driver = {
2754 .name = "ftdi-elan",
2755 .probe = ftdi_elan_probe,
2756 .disconnect = ftdi_elan_disconnect,
2757 .id_table = ftdi_elan_table,
2758};
2759static int __init ftdi_elan_init(void)
2760{
2761 int result;
2762 pr_info("driver %s\n", ftdi_elan_driver.name);
2763 mutex_init(&ftdi_module_lock);
2764 INIT_LIST_HEAD(&ftdi_static_list);
2765 result = usb_register(&ftdi_elan_driver);
2766 if (result) {
2767 pr_err("usb_register failed. Error number %d\n", result);
2768 }
2769 return result;
2770
2771}
2772
2773static void __exit ftdi_elan_exit(void)
2774{
2775 struct usb_ftdi *ftdi;
2776 struct usb_ftdi *temp;
2777 usb_deregister(&ftdi_elan_driver);
2778 pr_info("ftdi_u132 driver deregistered\n");
2779 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2780 ftdi_status_cancel_work(ftdi);
2781 ftdi_command_cancel_work(ftdi);
2782 ftdi_response_cancel_work(ftdi);
2783 }
2784}
2785
2786
2787module_init(ftdi_elan_init);
2788module_exit(ftdi_elan_exit);