Linux Audio

Check our new training course

Embedded Linux training

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