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