Linux Audio

Check our new training course

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