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