Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2* Host Controller Driver for the Elan Digital Systems U132 adapter
   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 host 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/module.h>
  40#include <linux/moduleparam.h>
  41#include <linux/delay.h>
  42#include <linux/ioport.h>
  43#include <linux/pci_ids.h>
  44#include <linux/sched.h>
  45#include <linux/slab.h>
  46#include <linux/errno.h>
  47#include <linux/init.h>
  48#include <linux/timer.h>
  49#include <linux/list.h>
  50#include <linux/interrupt.h>
  51#include <linux/usb.h>
  52#include <linux/usb/hcd.h>
  53#include <linux/workqueue.h>
  54#include <linux/platform_device.h>
  55#include <linux/mutex.h>
  56#include <asm/io.h>
  57#include <asm/irq.h>
  58#include <asm/system.h>
  59#include <asm/byteorder.h>
  60
  61	/* FIXME ohci.h is ONLY for internal use by the OHCI driver.
  62	 * If you're going to try stuff like this, you need to split
  63	 * out shareable stuff (register declarations?) into its own
  64	 * file, maybe name <linux/usb/ohci.h>
  65	 */
  66
  67#include "ohci.h"
  68#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  69#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
  70	OHCI_INTR_WDH)
  71MODULE_AUTHOR("Tony Olech - Elan Digital Systems Limited");
  72MODULE_DESCRIPTION("U132 USB Host Controller Driver");
  73MODULE_LICENSE("GPL");
  74#define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
  75INT_MODULE_PARM(testing, 0);
  76/* Some boards misreport power switching/overcurrent*/
  77static int distrust_firmware = 1;
  78module_param(distrust_firmware, bool, 0);
  79MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
  80	"t setup");
  81static DECLARE_WAIT_QUEUE_HEAD(u132_hcd_wait);
  82/*
  83* u132_module_lock exists to protect access to global variables
  84*
  85*/
  86static struct mutex u132_module_lock;
  87static int u132_exiting;
  88static int u132_instances;
  89static struct list_head u132_static_list;
  90/*
  91* end of the global variables protected by u132_module_lock
  92*/
  93static struct workqueue_struct *workqueue;
  94#define MAX_U132_PORTS 7
  95#define MAX_U132_ADDRS 128
  96#define MAX_U132_UDEVS 4
  97#define MAX_U132_ENDPS 100
  98#define MAX_U132_RINGS 4
  99static const char *cc_to_text[16] = {
 100	"No Error ",
 101	"CRC Error ",
 102	"Bit Stuff ",
 103	"Data Togg ",
 104	"Stall ",
 105	"DevNotResp ",
 106	"PIDCheck ",
 107	"UnExpPID ",
 108	"DataOver ",
 109	"DataUnder ",
 110	"(for hw) ",
 111	"(for hw) ",
 112	"BufferOver ",
 113	"BuffUnder ",
 114	"(for HCD) ",
 115	"(for HCD) "
 116};
 117struct u132_port {
 118	struct u132 *u132;
 119	int reset;
 120	int enable;
 121	int power;
 122	int Status;
 123};
 124struct u132_addr {
 125	u8 address;
 126};
 127struct u132_udev {
 128	struct kref kref;
 129	struct usb_device *usb_device;
 130	u8 enumeration;
 131	u8 udev_number;
 132	u8 usb_addr;
 133	u8 portnumber;
 134	u8 endp_number_in[16];
 135	u8 endp_number_out[16];
 136};
 137#define ENDP_QUEUE_SHIFT 3
 138#define ENDP_QUEUE_SIZE (1<<ENDP_QUEUE_SHIFT)
 139#define ENDP_QUEUE_MASK (ENDP_QUEUE_SIZE-1)
 140struct u132_urbq {
 141	struct list_head urb_more;
 142	struct urb *urb;
 143};
 144struct u132_spin {
 145	spinlock_t slock;
 146};
 147struct u132_endp {
 148	struct kref kref;
 149	u8 udev_number;
 150	u8 endp_number;
 151	u8 usb_addr;
 152	u8 usb_endp;
 153	struct u132 *u132;
 154	struct list_head endp_ring;
 155	struct u132_ring *ring;
 156	unsigned toggle_bits:2;
 157	unsigned active:1;
 158	unsigned delayed:1;
 159	unsigned input:1;
 160	unsigned output:1;
 161	unsigned pipetype:2;
 162	unsigned dequeueing:1;
 163	unsigned edset_flush:1;
 164	unsigned spare_bits:14;
 165	unsigned long jiffies;
 166	struct usb_host_endpoint *hep;
 167	struct u132_spin queue_lock;
 168	u16 queue_size;
 169	u16 queue_last;
 170	u16 queue_next;
 171	struct urb *urb_list[ENDP_QUEUE_SIZE];
 172	struct list_head urb_more;
 173	struct delayed_work scheduler;
 174};
 175struct u132_ring {
 176	unsigned in_use:1;
 177	unsigned length:7;
 178	u8 number;
 179	struct u132 *u132;
 180	struct u132_endp *curr_endp;
 181	struct delayed_work scheduler;
 182};
 183struct u132 {
 184	struct kref kref;
 185	struct list_head u132_list;
 186	struct mutex sw_lock;
 187	struct mutex scheduler_lock;
 188	struct u132_platform_data *board;
 189	struct platform_device *platform_dev;
 190	struct u132_ring ring[MAX_U132_RINGS];
 191	int sequence_num;
 192	int going;
 193	int power;
 194	int reset;
 195	int num_ports;
 196	u32 hc_control;
 197	u32 hc_fminterval;
 198	u32 hc_roothub_status;
 199	u32 hc_roothub_a;
 200	u32 hc_roothub_portstatus[MAX_ROOT_PORTS];
 201	int flags;
 202	unsigned long next_statechange;
 203	struct delayed_work monitor;
 204	int num_endpoints;
 205	struct u132_addr addr[MAX_U132_ADDRS];
 206	struct u132_udev udev[MAX_U132_UDEVS];
 207	struct u132_port port[MAX_U132_PORTS];
 208	struct u132_endp *endp[MAX_U132_ENDPS];
 209};
 210
 211/*
 212* these cannot be inlines because we need the structure offset!!
 213* Does anyone have a better way?????
 214*/
 215#define ftdi_read_pcimem(pdev, member, data) usb_ftdi_elan_read_pcimem(pdev, \
 216	offsetof(struct ohci_regs, member), 0, data);
 217#define ftdi_write_pcimem(pdev, member, data) usb_ftdi_elan_write_pcimem(pdev, \
 218	offsetof(struct ohci_regs, member), 0, data);
 219#define u132_read_pcimem(u132, member, data) \
 220	usb_ftdi_elan_read_pcimem(u132->platform_dev, offsetof(struct \
 221	ohci_regs, member), 0, data);
 222#define u132_write_pcimem(u132, member, data) \
 223	usb_ftdi_elan_write_pcimem(u132->platform_dev, offsetof(struct \
 224	ohci_regs, member), 0, data);
 225static inline struct u132 *udev_to_u132(struct u132_udev *udev)
 226{
 227	u8 udev_number = udev->udev_number;
 228	return container_of(udev, struct u132, udev[udev_number]);
 229}
 230
 231static inline struct u132 *hcd_to_u132(struct usb_hcd *hcd)
 232{
 233	return (struct u132 *)(hcd->hcd_priv);
 234}
 235
 236static inline struct usb_hcd *u132_to_hcd(struct u132 *u132)
 237{
 238	return container_of((void *)u132, struct usb_hcd, hcd_priv);
 239}
 240
 241static inline void u132_disable(struct u132 *u132)
 242{
 243	u132_to_hcd(u132)->state = HC_STATE_HALT;
 244}
 245
 246
 247#define kref_to_u132(d) container_of(d, struct u132, kref)
 248#define kref_to_u132_endp(d) container_of(d, struct u132_endp, kref)
 249#define kref_to_u132_udev(d) container_of(d, struct u132_udev, kref)
 250#include "../misc/usb_u132.h"
 251static const char hcd_name[] = "u132_hcd";
 252#define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | \
 253	USB_PORT_STAT_C_SUSPEND | USB_PORT_STAT_C_OVERCURRENT | \
 254	USB_PORT_STAT_C_RESET) << 16)
 255static void u132_hcd_delete(struct kref *kref)
 256{
 257	struct u132 *u132 = kref_to_u132(kref);
 258	struct platform_device *pdev = u132->platform_dev;
 259	struct usb_hcd *hcd = u132_to_hcd(u132);
 260	u132->going += 1;
 261	mutex_lock(&u132_module_lock);
 262	list_del_init(&u132->u132_list);
 263	u132_instances -= 1;
 264	mutex_unlock(&u132_module_lock);
 265	dev_warn(&u132->platform_dev->dev, "FREEING the hcd=%p and thus the u13"
 266		"2=%p going=%d pdev=%p\n", hcd, u132, u132->going, pdev);
 267	usb_put_hcd(hcd);
 268}
 269
 270static inline void u132_u132_put_kref(struct u132 *u132)
 271{
 272	kref_put(&u132->kref, u132_hcd_delete);
 273}
 274
 275static inline void u132_u132_init_kref(struct u132 *u132)
 276{
 277	kref_init(&u132->kref);
 278}
 279
 280static void u132_udev_delete(struct kref *kref)
 281{
 282	struct u132_udev *udev = kref_to_u132_udev(kref);
 283	udev->udev_number = 0;
 284	udev->usb_device = NULL;
 285	udev->usb_addr = 0;
 286	udev->enumeration = 0;
 287}
 288
 289static inline void u132_udev_put_kref(struct u132 *u132, struct u132_udev *udev)
 290{
 291	kref_put(&udev->kref, u132_udev_delete);
 292}
 293
 294static inline void u132_udev_get_kref(struct u132 *u132, struct u132_udev *udev)
 295{
 296	kref_get(&udev->kref);
 297}
 298
 299static inline void u132_udev_init_kref(struct u132 *u132,
 300	struct u132_udev *udev)
 301{
 302	kref_init(&udev->kref);
 303}
 304
 305static inline void u132_ring_put_kref(struct u132 *u132, struct u132_ring *ring)
 306{
 307	kref_put(&u132->kref, u132_hcd_delete);
 308}
 309
 310static void u132_ring_requeue_work(struct u132 *u132, struct u132_ring *ring,
 311	unsigned int delta)
 312{
 313	if (delta > 0) {
 314		if (queue_delayed_work(workqueue, &ring->scheduler, delta))
 315			return;
 316	} else if (queue_delayed_work(workqueue, &ring->scheduler, 0))
 317		return;
 318	kref_put(&u132->kref, u132_hcd_delete);
 319}
 320
 321static void u132_ring_queue_work(struct u132 *u132, struct u132_ring *ring,
 322	unsigned int delta)
 323{
 324	kref_get(&u132->kref);
 325	u132_ring_requeue_work(u132, ring, delta);
 326}
 327
 328static void u132_ring_cancel_work(struct u132 *u132, struct u132_ring *ring)
 329{
 330	if (cancel_delayed_work(&ring->scheduler))
 331		kref_put(&u132->kref, u132_hcd_delete);
 332}
 333
 334static void u132_endp_delete(struct kref *kref)
 335{
 336	struct u132_endp *endp = kref_to_u132_endp(kref);
 337	struct u132 *u132 = endp->u132;
 338	u8 usb_addr = endp->usb_addr;
 339	u8 usb_endp = endp->usb_endp;
 340	u8 address = u132->addr[usb_addr].address;
 341	struct u132_udev *udev = &u132->udev[address];
 342	u8 endp_number = endp->endp_number;
 343	struct usb_host_endpoint *hep = endp->hep;
 344	struct u132_ring *ring = endp->ring;
 345	struct list_head *head = &endp->endp_ring;
 346	ring->length -= 1;
 347	if (endp == ring->curr_endp) {
 348		if (list_empty(head)) {
 349			ring->curr_endp = NULL;
 350			list_del(head);
 351		} else {
 352			struct u132_endp *next_endp = list_entry(head->next,
 353				struct u132_endp, endp_ring);
 354			ring->curr_endp = next_endp;
 355			list_del(head);
 356		}
 357	} else
 358		list_del(head);
 359	if (endp->input) {
 360		udev->endp_number_in[usb_endp] = 0;
 361		u132_udev_put_kref(u132, udev);
 362	}
 363	if (endp->output) {
 364		udev->endp_number_out[usb_endp] = 0;
 365		u132_udev_put_kref(u132, udev);
 366	}
 367	u132->endp[endp_number - 1] = NULL;
 368	hep->hcpriv = NULL;
 369	kfree(endp);
 370	u132_u132_put_kref(u132);
 371}
 372
 373static inline void u132_endp_put_kref(struct u132 *u132, struct u132_endp *endp)
 374{
 375	kref_put(&endp->kref, u132_endp_delete);
 376}
 377
 378static inline void u132_endp_get_kref(struct u132 *u132, struct u132_endp *endp)
 379{
 380	kref_get(&endp->kref);
 381}
 382
 383static inline void u132_endp_init_kref(struct u132 *u132,
 384	struct u132_endp *endp)
 385{
 386	kref_init(&endp->kref);
 387	kref_get(&u132->kref);
 388}
 389
 390static void u132_endp_queue_work(struct u132 *u132, struct u132_endp *endp,
 391	unsigned int delta)
 392{
 393	if (queue_delayed_work(workqueue, &endp->scheduler, delta))
 394		kref_get(&endp->kref);
 395}
 396
 397static void u132_endp_cancel_work(struct u132 *u132, struct u132_endp *endp)
 398{
 399	if (cancel_delayed_work(&endp->scheduler))
 400		kref_put(&endp->kref, u132_endp_delete);
 401}
 402
 403static inline void u132_monitor_put_kref(struct u132 *u132)
 404{
 405	kref_put(&u132->kref, u132_hcd_delete);
 406}
 407
 408static void u132_monitor_queue_work(struct u132 *u132, unsigned int delta)
 409{
 410	if (queue_delayed_work(workqueue, &u132->monitor, delta))
 411		kref_get(&u132->kref);
 412}
 413
 414static void u132_monitor_requeue_work(struct u132 *u132, unsigned int delta)
 415{
 416	if (!queue_delayed_work(workqueue, &u132->monitor, delta))
 417		kref_put(&u132->kref, u132_hcd_delete);
 418}
 419
 420static void u132_monitor_cancel_work(struct u132 *u132)
 421{
 422	if (cancel_delayed_work(&u132->monitor))
 423		kref_put(&u132->kref, u132_hcd_delete);
 424}
 425
 426static int read_roothub_info(struct u132 *u132)
 427{
 428	u32 revision;
 429	int retval;
 430	retval = u132_read_pcimem(u132, revision, &revision);
 431	if (retval) {
 432		dev_err(&u132->platform_dev->dev, "error %d accessing device co"
 433			"ntrol\n", retval);
 434		return retval;
 435	} else if ((revision & 0xFF) == 0x10) {
 436	} else if ((revision & 0xFF) == 0x11) {
 437	} else {
 438		dev_err(&u132->platform_dev->dev, "device revision is not valid"
 439			" %08X\n", revision);
 440		return -ENODEV;
 441	}
 442	retval = u132_read_pcimem(u132, control, &u132->hc_control);
 443	if (retval) {
 444		dev_err(&u132->platform_dev->dev, "error %d accessing device co"
 445			"ntrol\n", retval);
 446		return retval;
 447	}
 448	retval = u132_read_pcimem(u132, roothub.status,
 449		&u132->hc_roothub_status);
 450	if (retval) {
 451		dev_err(&u132->platform_dev->dev, "error %d accessing device re"
 452			"g roothub.status\n", retval);
 453		return retval;
 454	}
 455	retval = u132_read_pcimem(u132, roothub.a, &u132->hc_roothub_a);
 456	if (retval) {
 457		dev_err(&u132->platform_dev->dev, "error %d accessing device re"
 458			"g roothub.a\n", retval);
 459		return retval;
 460	}
 461	{
 462		int I = u132->num_ports;
 463		int i = 0;
 464		while (I-- > 0) {
 465			retval = u132_read_pcimem(u132, roothub.portstatus[i],
 466				&u132->hc_roothub_portstatus[i]);
 467			if (retval) {
 468				dev_err(&u132->platform_dev->dev, "error %d acc"
 469					"essing device roothub.portstatus[%d]\n"
 470					, retval, i);
 471				return retval;
 472			} else
 473				i += 1;
 474		}
 475	}
 476	return 0;
 477}
 478
 479static void u132_hcd_monitor_work(struct work_struct *work)
 480{
 481	struct u132 *u132 = container_of(work, struct u132, monitor.work);
 482	if (u132->going > 1) {
 483		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 484			, u132->going);
 485		u132_monitor_put_kref(u132);
 486		return;
 487	} else if (u132->going > 0) {
 488		dev_err(&u132->platform_dev->dev, "device is being removed\n");
 489		u132_monitor_put_kref(u132);
 490		return;
 491	} else {
 492		int retval;
 493		mutex_lock(&u132->sw_lock);
 494		retval = read_roothub_info(u132);
 495		if (retval) {
 496			struct usb_hcd *hcd = u132_to_hcd(u132);
 497			u132_disable(u132);
 498			u132->going = 1;
 499			mutex_unlock(&u132->sw_lock);
 500			usb_hc_died(hcd);
 501			ftdi_elan_gone_away(u132->platform_dev);
 502			u132_monitor_put_kref(u132);
 503			return;
 504		} else {
 505			u132_monitor_requeue_work(u132, 500);
 506			mutex_unlock(&u132->sw_lock);
 507			return;
 508		}
 509	}
 510}
 511
 512static void u132_hcd_giveback_urb(struct u132 *u132, struct u132_endp *endp,
 513	struct urb *urb, int status)
 514{
 515	struct u132_ring *ring;
 516	unsigned long irqs;
 517	struct usb_hcd *hcd = u132_to_hcd(u132);
 518	urb->error_count = 0;
 519	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
 520	usb_hcd_unlink_urb_from_ep(hcd, urb);
 521	endp->queue_next += 1;
 522	if (ENDP_QUEUE_SIZE > --endp->queue_size) {
 523		endp->active = 0;
 524		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 525	} else {
 526		struct list_head *next = endp->urb_more.next;
 527		struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
 528			urb_more);
 529		list_del(next);
 530		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
 531			urbq->urb;
 532		endp->active = 0;
 533		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 534		kfree(urbq);
 535	}
 536	mutex_lock(&u132->scheduler_lock);
 537	ring = endp->ring;
 538	ring->in_use = 0;
 539	u132_ring_cancel_work(u132, ring);
 540	u132_ring_queue_work(u132, ring, 0);
 541	mutex_unlock(&u132->scheduler_lock);
 542	u132_endp_put_kref(u132, endp);
 543	usb_hcd_giveback_urb(hcd, urb, status);
 544}
 545
 546static void u132_hcd_forget_urb(struct u132 *u132, struct u132_endp *endp,
 547	struct urb *urb, int status)
 548{
 549	u132_endp_put_kref(u132, endp);
 550}
 551
 552static void u132_hcd_abandon_urb(struct u132 *u132, struct u132_endp *endp,
 553	struct urb *urb, int status)
 554{
 555	unsigned long irqs;
 556	struct usb_hcd *hcd = u132_to_hcd(u132);
 557	urb->error_count = 0;
 558	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
 559	usb_hcd_unlink_urb_from_ep(hcd, urb);
 560	endp->queue_next += 1;
 561	if (ENDP_QUEUE_SIZE > --endp->queue_size) {
 562		endp->active = 0;
 563		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 564	} else {
 565		struct list_head *next = endp->urb_more.next;
 566		struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
 567			urb_more);
 568		list_del(next);
 569		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
 570			urbq->urb;
 571		endp->active = 0;
 572		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 573		kfree(urbq);
 574	}
 575	usb_hcd_giveback_urb(hcd, urb, status);
 576}
 577
 578static inline int edset_input(struct u132 *u132, struct u132_ring *ring,
 579	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 580	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 581	int toggle_bits, int error_count, int condition_code, int repeat_number,
 582	 int halted, int skipped, int actual, int non_null))
 583{
 584	return usb_ftdi_elan_edset_input(u132->platform_dev, ring->number, endp,
 585		 urb, address, endp->usb_endp, toggle_bits, callback);
 586}
 587
 588static inline int edset_setup(struct u132 *u132, struct u132_ring *ring,
 589	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 590	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 591	int toggle_bits, int error_count, int condition_code, int repeat_number,
 592	 int halted, int skipped, int actual, int non_null))
 593{
 594	return usb_ftdi_elan_edset_setup(u132->platform_dev, ring->number, endp,
 595		 urb, address, endp->usb_endp, toggle_bits, callback);
 596}
 597
 598static inline int edset_single(struct u132 *u132, struct u132_ring *ring,
 599	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 600	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 601	int toggle_bits, int error_count, int condition_code, int repeat_number,
 602	 int halted, int skipped, int actual, int non_null))
 603{
 604	return usb_ftdi_elan_edset_single(u132->platform_dev, ring->number,
 605		endp, urb, address, endp->usb_endp, toggle_bits, callback);
 606}
 607
 608static inline int edset_output(struct u132 *u132, struct u132_ring *ring,
 609	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 610	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 611	int toggle_bits, int error_count, int condition_code, int repeat_number,
 612	 int halted, int skipped, int actual, int non_null))
 613{
 614	return usb_ftdi_elan_edset_output(u132->platform_dev, ring->number,
 615		endp, urb, address, endp->usb_endp, toggle_bits, callback);
 616}
 617
 618
 619/*
 620* must not LOCK sw_lock
 621*
 622*/
 623static void u132_hcd_interrupt_recv(void *data, struct urb *urb, u8 *buf,
 624	int len, int toggle_bits, int error_count, int condition_code,
 625	int repeat_number, int halted, int skipped, int actual, int non_null)
 626{
 627	struct u132_endp *endp = data;
 628	struct u132 *u132 = endp->u132;
 629	u8 address = u132->addr[endp->usb_addr].address;
 630	struct u132_udev *udev = &u132->udev[address];
 631	mutex_lock(&u132->scheduler_lock);
 632	if (u132->going > 1) {
 633		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 634			, u132->going);
 635		mutex_unlock(&u132->scheduler_lock);
 636		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 637		return;
 638	} else if (endp->dequeueing) {
 639		endp->dequeueing = 0;
 640		mutex_unlock(&u132->scheduler_lock);
 641		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 642		return;
 643	} else if (u132->going > 0) {
 644		dev_err(&u132->platform_dev->dev, "device is being removed "
 645				"urb=%p\n", urb);
 646		mutex_unlock(&u132->scheduler_lock);
 647		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 648		return;
 649	} else if (!urb->unlinked) {
 650		struct u132_ring *ring = endp->ring;
 651		u8 *u = urb->transfer_buffer + urb->actual_length;
 652		u8 *b = buf;
 653		int L = len;
 654
 655		while (L-- > 0)
 656			*u++ = *b++;
 657
 658		urb->actual_length += len;
 659		if ((condition_code == TD_CC_NOERROR) &&
 660			(urb->transfer_buffer_length > urb->actual_length)) {
 661			endp->toggle_bits = toggle_bits;
 662			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 663				1 & toggle_bits);
 664			if (urb->actual_length > 0) {
 665				int retval;
 666				mutex_unlock(&u132->scheduler_lock);
 667				retval = edset_single(u132, ring, endp, urb,
 668					address, endp->toggle_bits,
 669					u132_hcd_interrupt_recv);
 670				if (retval != 0)
 671					u132_hcd_giveback_urb(u132, endp, urb,
 672						retval);
 673			} else {
 674				ring->in_use = 0;
 675				endp->active = 0;
 676				endp->jiffies = jiffies +
 677					msecs_to_jiffies(urb->interval);
 678				u132_ring_cancel_work(u132, ring);
 679				u132_ring_queue_work(u132, ring, 0);
 680				mutex_unlock(&u132->scheduler_lock);
 681				u132_endp_put_kref(u132, endp);
 682			}
 683			return;
 684		} else if ((condition_code == TD_DATAUNDERRUN) &&
 685			((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
 686			endp->toggle_bits = toggle_bits;
 687			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 688				1 & toggle_bits);
 689			mutex_unlock(&u132->scheduler_lock);
 690			u132_hcd_giveback_urb(u132, endp, urb, 0);
 691			return;
 692		} else {
 693			if (condition_code == TD_CC_NOERROR) {
 694				endp->toggle_bits = toggle_bits;
 695				usb_settoggle(udev->usb_device, endp->usb_endp,
 696					0, 1 & toggle_bits);
 697			} else if (condition_code == TD_CC_STALL) {
 698				endp->toggle_bits = 0x2;
 699				usb_settoggle(udev->usb_device, endp->usb_endp,
 700					0, 0);
 701			} else {
 702				endp->toggle_bits = 0x2;
 703				usb_settoggle(udev->usb_device, endp->usb_endp,
 704					0, 0);
 705				dev_err(&u132->platform_dev->dev, "urb=%p givin"
 706					"g back INTERRUPT %s\n", urb,
 707					cc_to_text[condition_code]);
 708			}
 709			mutex_unlock(&u132->scheduler_lock);
 710			u132_hcd_giveback_urb(u132, endp, urb,
 711				cc_to_error[condition_code]);
 712			return;
 713		}
 714	} else {
 715		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 716				"unlinked=%d\n", urb, urb->unlinked);
 717		mutex_unlock(&u132->scheduler_lock);
 718		u132_hcd_giveback_urb(u132, endp, urb, 0);
 719		return;
 720	}
 721}
 722
 723static void u132_hcd_bulk_output_sent(void *data, struct urb *urb, u8 *buf,
 724	int len, int toggle_bits, int error_count, int condition_code,
 725	int repeat_number, int halted, int skipped, int actual, int non_null)
 726{
 727	struct u132_endp *endp = data;
 728	struct u132 *u132 = endp->u132;
 729	u8 address = u132->addr[endp->usb_addr].address;
 730	mutex_lock(&u132->scheduler_lock);
 731	if (u132->going > 1) {
 732		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 733			, u132->going);
 734		mutex_unlock(&u132->scheduler_lock);
 735		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 736		return;
 737	} else if (endp->dequeueing) {
 738		endp->dequeueing = 0;
 739		mutex_unlock(&u132->scheduler_lock);
 740		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 741		return;
 742	} else if (u132->going > 0) {
 743		dev_err(&u132->platform_dev->dev, "device is being removed "
 744				"urb=%p\n", urb);
 745		mutex_unlock(&u132->scheduler_lock);
 746		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 747		return;
 748	} else if (!urb->unlinked) {
 749		struct u132_ring *ring = endp->ring;
 750		urb->actual_length += len;
 751		endp->toggle_bits = toggle_bits;
 752		if (urb->transfer_buffer_length > urb->actual_length) {
 753			int retval;
 754			mutex_unlock(&u132->scheduler_lock);
 755			retval = edset_output(u132, ring, endp, urb, address,
 756				endp->toggle_bits, u132_hcd_bulk_output_sent);
 757			if (retval != 0)
 758				u132_hcd_giveback_urb(u132, endp, urb, retval);
 759			return;
 760		} else {
 761			mutex_unlock(&u132->scheduler_lock);
 762			u132_hcd_giveback_urb(u132, endp, urb, 0);
 763			return;
 764		}
 765	} else {
 766		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 767				"unlinked=%d\n", urb, urb->unlinked);
 768		mutex_unlock(&u132->scheduler_lock);
 769		u132_hcd_giveback_urb(u132, endp, urb, 0);
 770		return;
 771	}
 772}
 773
 774static void u132_hcd_bulk_input_recv(void *data, struct urb *urb, u8 *buf,
 775	int len, int toggle_bits, int error_count, int condition_code,
 776	int repeat_number, int halted, int skipped, int actual, int non_null)
 777{
 778	struct u132_endp *endp = data;
 779	struct u132 *u132 = endp->u132;
 780	u8 address = u132->addr[endp->usb_addr].address;
 781	struct u132_udev *udev = &u132->udev[address];
 782	mutex_lock(&u132->scheduler_lock);
 783	if (u132->going > 1) {
 784		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 785			, u132->going);
 786		mutex_unlock(&u132->scheduler_lock);
 787		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 788		return;
 789	} else if (endp->dequeueing) {
 790		endp->dequeueing = 0;
 791		mutex_unlock(&u132->scheduler_lock);
 792		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 793		return;
 794	} else if (u132->going > 0) {
 795		dev_err(&u132->platform_dev->dev, "device is being removed "
 796				"urb=%p\n", urb);
 797		mutex_unlock(&u132->scheduler_lock);
 798		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 799		return;
 800	} else if (!urb->unlinked) {
 801		struct u132_ring *ring = endp->ring;
 802		u8 *u = urb->transfer_buffer + urb->actual_length;
 803		u8 *b = buf;
 804		int L = len;
 805
 806		while (L-- > 0)
 807			*u++ = *b++;
 808
 809		urb->actual_length += len;
 810		if ((condition_code == TD_CC_NOERROR) &&
 811			(urb->transfer_buffer_length > urb->actual_length)) {
 812			int retval;
 813			endp->toggle_bits = toggle_bits;
 814			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 815				1 & toggle_bits);
 816			mutex_unlock(&u132->scheduler_lock);
 817			retval = usb_ftdi_elan_edset_input(u132->platform_dev,
 818				ring->number, endp, urb, address,
 819				endp->usb_endp, endp->toggle_bits,
 820				u132_hcd_bulk_input_recv);
 821			if (retval != 0)
 822				u132_hcd_giveback_urb(u132, endp, urb, retval);
 823			return;
 824		} else if (condition_code == TD_CC_NOERROR) {
 825			endp->toggle_bits = toggle_bits;
 826			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 827				1 & toggle_bits);
 828			mutex_unlock(&u132->scheduler_lock);
 829			u132_hcd_giveback_urb(u132, endp, urb,
 830				cc_to_error[condition_code]);
 831			return;
 832		} else if ((condition_code == TD_DATAUNDERRUN) &&
 833			((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
 834			endp->toggle_bits = toggle_bits;
 835			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 836				1 & toggle_bits);
 837			mutex_unlock(&u132->scheduler_lock);
 838			u132_hcd_giveback_urb(u132, endp, urb, 0);
 839			return;
 840		} else if (condition_code == TD_DATAUNDERRUN) {
 841			endp->toggle_bits = toggle_bits;
 842			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 843				1 & toggle_bits);
 844			dev_warn(&u132->platform_dev->dev, "urb=%p(SHORT NOT OK"
 845				") giving back BULK IN %s\n", urb,
 846				cc_to_text[condition_code]);
 847			mutex_unlock(&u132->scheduler_lock);
 848			u132_hcd_giveback_urb(u132, endp, urb, 0);
 849			return;
 850		} else if (condition_code == TD_CC_STALL) {
 851			endp->toggle_bits = 0x2;
 852			usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
 853			mutex_unlock(&u132->scheduler_lock);
 854			u132_hcd_giveback_urb(u132, endp, urb,
 855				cc_to_error[condition_code]);
 856			return;
 857		} else {
 858			endp->toggle_bits = 0x2;
 859			usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
 860			dev_err(&u132->platform_dev->dev, "urb=%p giving back B"
 861				"ULK IN code=%d %s\n", urb, condition_code,
 862				cc_to_text[condition_code]);
 863			mutex_unlock(&u132->scheduler_lock);
 864			u132_hcd_giveback_urb(u132, endp, urb,
 865				cc_to_error[condition_code]);
 866			return;
 867		}
 868	} else {
 869		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 870				"unlinked=%d\n", urb, urb->unlinked);
 871		mutex_unlock(&u132->scheduler_lock);
 872		u132_hcd_giveback_urb(u132, endp, urb, 0);
 873		return;
 874	}
 875}
 876
 877static void u132_hcd_configure_empty_sent(void *data, struct urb *urb, u8 *buf,
 878	int len, int toggle_bits, int error_count, int condition_code,
 879	int repeat_number, int halted, int skipped, int actual, int non_null)
 880{
 881	struct u132_endp *endp = data;
 882	struct u132 *u132 = endp->u132;
 883	mutex_lock(&u132->scheduler_lock);
 884	if (u132->going > 1) {
 885		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 886			, u132->going);
 887		mutex_unlock(&u132->scheduler_lock);
 888		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 889		return;
 890	} else if (endp->dequeueing) {
 891		endp->dequeueing = 0;
 892		mutex_unlock(&u132->scheduler_lock);
 893		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 894		return;
 895	} else if (u132->going > 0) {
 896		dev_err(&u132->platform_dev->dev, "device is being removed "
 897				"urb=%p\n", urb);
 898		mutex_unlock(&u132->scheduler_lock);
 899		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 900		return;
 901	} else if (!urb->unlinked) {
 902		mutex_unlock(&u132->scheduler_lock);
 903		u132_hcd_giveback_urb(u132, endp, urb, 0);
 904		return;
 905	} else {
 906		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 907				"unlinked=%d\n", urb, urb->unlinked);
 908		mutex_unlock(&u132->scheduler_lock);
 909		u132_hcd_giveback_urb(u132, endp, urb, 0);
 910		return;
 911	}
 912}
 913
 914static void u132_hcd_configure_input_recv(void *data, struct urb *urb, u8 *buf,
 915	int len, int toggle_bits, int error_count, int condition_code,
 916	int repeat_number, int halted, int skipped, int actual, int non_null)
 917{
 918	struct u132_endp *endp = data;
 919	struct u132 *u132 = endp->u132;
 920	u8 address = u132->addr[endp->usb_addr].address;
 921	mutex_lock(&u132->scheduler_lock);
 922	if (u132->going > 1) {
 923		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 924			, u132->going);
 925		mutex_unlock(&u132->scheduler_lock);
 926		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 927		return;
 928	} else if (endp->dequeueing) {
 929		endp->dequeueing = 0;
 930		mutex_unlock(&u132->scheduler_lock);
 931		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 932		return;
 933	} else if (u132->going > 0) {
 934		dev_err(&u132->platform_dev->dev, "device is being removed "
 935				"urb=%p\n", urb);
 936		mutex_unlock(&u132->scheduler_lock);
 937		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 938		return;
 939	} else if (!urb->unlinked) {
 940		struct u132_ring *ring = endp->ring;
 941		u8 *u = urb->transfer_buffer;
 942		u8 *b = buf;
 943		int L = len;
 944
 945		while (L-- > 0)
 946			*u++ = *b++;
 947
 948		urb->actual_length = len;
 949		if ((condition_code == TD_CC_NOERROR) || ((condition_code ==
 950			TD_DATAUNDERRUN) && ((urb->transfer_flags &
 951			URB_SHORT_NOT_OK) == 0))) {
 952			int retval;
 953			mutex_unlock(&u132->scheduler_lock);
 954			retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
 955				ring->number, endp, urb, address,
 956				endp->usb_endp, 0x3,
 957				u132_hcd_configure_empty_sent);
 958			if (retval != 0)
 959				u132_hcd_giveback_urb(u132, endp, urb, retval);
 960			return;
 961		} else if (condition_code == TD_CC_STALL) {
 962			mutex_unlock(&u132->scheduler_lock);
 963			dev_warn(&u132->platform_dev->dev, "giving back SETUP I"
 964				"NPUT STALL urb %p\n", urb);
 965			u132_hcd_giveback_urb(u132, endp, urb,
 966				cc_to_error[condition_code]);
 967			return;
 968		} else {
 969			mutex_unlock(&u132->scheduler_lock);
 970			dev_err(&u132->platform_dev->dev, "giving back SETUP IN"
 971				"PUT %s urb %p\n", cc_to_text[condition_code],
 972				urb);
 973			u132_hcd_giveback_urb(u132, endp, urb,
 974				cc_to_error[condition_code]);
 975			return;
 976		}
 977	} else {
 978		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 979				"unlinked=%d\n", urb, urb->unlinked);
 980		mutex_unlock(&u132->scheduler_lock);
 981		u132_hcd_giveback_urb(u132, endp, urb, 0);
 982		return;
 983	}
 984}
 985
 986static void u132_hcd_configure_empty_recv(void *data, struct urb *urb, u8 *buf,
 987	int len, int toggle_bits, int error_count, int condition_code,
 988	int repeat_number, int halted, int skipped, int actual, int non_null)
 989{
 990	struct u132_endp *endp = data;
 991	struct u132 *u132 = endp->u132;
 992	mutex_lock(&u132->scheduler_lock);
 993	if (u132->going > 1) {
 994		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 995			, u132->going);
 996		mutex_unlock(&u132->scheduler_lock);
 997		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 998		return;
 999	} else if (endp->dequeueing) {
1000		endp->dequeueing = 0;
1001		mutex_unlock(&u132->scheduler_lock);
1002		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1003		return;
1004	} else if (u132->going > 0) {
1005		dev_err(&u132->platform_dev->dev, "device is being removed "
1006				"urb=%p\n", urb);
1007		mutex_unlock(&u132->scheduler_lock);
1008		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1009		return;
1010	} else if (!urb->unlinked) {
1011		mutex_unlock(&u132->scheduler_lock);
1012		u132_hcd_giveback_urb(u132, endp, urb, 0);
1013		return;
1014	} else {
1015		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1016				"unlinked=%d\n", urb, urb->unlinked);
1017		mutex_unlock(&u132->scheduler_lock);
1018		u132_hcd_giveback_urb(u132, endp, urb, 0);
1019		return;
1020	}
1021}
1022
1023static void u132_hcd_configure_setup_sent(void *data, struct urb *urb, u8 *buf,
1024	int len, int toggle_bits, int error_count, int condition_code,
1025	int repeat_number, int halted, int skipped, int actual, int non_null)
1026{
1027	struct u132_endp *endp = data;
1028	struct u132 *u132 = endp->u132;
1029	u8 address = u132->addr[endp->usb_addr].address;
1030	mutex_lock(&u132->scheduler_lock);
1031	if (u132->going > 1) {
1032		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1033			, u132->going);
1034		mutex_unlock(&u132->scheduler_lock);
1035		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1036		return;
1037	} else if (endp->dequeueing) {
1038		endp->dequeueing = 0;
1039		mutex_unlock(&u132->scheduler_lock);
1040		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1041		return;
1042	} else if (u132->going > 0) {
1043		dev_err(&u132->platform_dev->dev, "device is being removed "
1044				"urb=%p\n", urb);
1045		mutex_unlock(&u132->scheduler_lock);
1046		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1047		return;
1048	} else if (!urb->unlinked) {
1049		if (usb_pipein(urb->pipe)) {
1050			int retval;
1051			struct u132_ring *ring = endp->ring;
1052			mutex_unlock(&u132->scheduler_lock);
1053			retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1054				ring->number, endp, urb, address,
1055				endp->usb_endp, 0,
1056				u132_hcd_configure_input_recv);
1057			if (retval != 0)
1058				u132_hcd_giveback_urb(u132, endp, urb, retval);
1059			return;
1060		} else {
1061			int retval;
1062			struct u132_ring *ring = endp->ring;
1063			mutex_unlock(&u132->scheduler_lock);
1064			retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1065				ring->number, endp, urb, address,
1066				endp->usb_endp, 0,
1067				u132_hcd_configure_empty_recv);
1068			if (retval != 0)
1069				u132_hcd_giveback_urb(u132, endp, urb, retval);
1070			return;
1071		}
1072	} else {
1073		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1074				"unlinked=%d\n", urb, urb->unlinked);
1075		mutex_unlock(&u132->scheduler_lock);
1076		u132_hcd_giveback_urb(u132, endp, urb, 0);
1077		return;
1078	}
1079}
1080
1081static void u132_hcd_enumeration_empty_recv(void *data, struct urb *urb,
1082	u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1083	int repeat_number, int halted, int skipped, int actual, int non_null)
1084{
1085	struct u132_endp *endp = data;
1086	struct u132 *u132 = endp->u132;
1087	u8 address = u132->addr[endp->usb_addr].address;
1088	struct u132_udev *udev = &u132->udev[address];
1089	mutex_lock(&u132->scheduler_lock);
1090	if (u132->going > 1) {
1091		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1092			, u132->going);
1093		mutex_unlock(&u132->scheduler_lock);
1094		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1095		return;
1096	} else if (endp->dequeueing) {
1097		endp->dequeueing = 0;
1098		mutex_unlock(&u132->scheduler_lock);
1099		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1100		return;
1101	} else if (u132->going > 0) {
1102		dev_err(&u132->platform_dev->dev, "device is being removed "
1103				"urb=%p\n", urb);
1104		mutex_unlock(&u132->scheduler_lock);
1105		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1106		return;
1107	} else if (!urb->unlinked) {
1108		u132->addr[0].address = 0;
1109		endp->usb_addr = udev->usb_addr;
1110		mutex_unlock(&u132->scheduler_lock);
1111		u132_hcd_giveback_urb(u132, endp, urb, 0);
1112		return;
1113	} else {
1114		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1115				"unlinked=%d\n", urb, urb->unlinked);
1116		mutex_unlock(&u132->scheduler_lock);
1117		u132_hcd_giveback_urb(u132, endp, urb, 0);
1118		return;
1119	}
1120}
1121
1122static void u132_hcd_enumeration_address_sent(void *data, struct urb *urb,
1123	u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1124	int repeat_number, int halted, int skipped, int actual, int non_null)
1125{
1126	struct u132_endp *endp = data;
1127	struct u132 *u132 = endp->u132;
1128	mutex_lock(&u132->scheduler_lock);
1129	if (u132->going > 1) {
1130		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1131			, u132->going);
1132		mutex_unlock(&u132->scheduler_lock);
1133		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1134		return;
1135	} else if (endp->dequeueing) {
1136		endp->dequeueing = 0;
1137		mutex_unlock(&u132->scheduler_lock);
1138		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1139		return;
1140	} else if (u132->going > 0) {
1141		dev_err(&u132->platform_dev->dev, "device is being removed "
1142				"urb=%p\n", urb);
1143		mutex_unlock(&u132->scheduler_lock);
1144		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1145		return;
1146	} else if (!urb->unlinked) {
1147		int retval;
1148		struct u132_ring *ring = endp->ring;
1149		mutex_unlock(&u132->scheduler_lock);
1150		retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1151			ring->number, endp, urb, 0, endp->usb_endp, 0,
1152			u132_hcd_enumeration_empty_recv);
1153		if (retval != 0)
1154			u132_hcd_giveback_urb(u132, endp, urb, retval);
1155		return;
1156	} else {
1157		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1158				"unlinked=%d\n", urb, urb->unlinked);
1159		mutex_unlock(&u132->scheduler_lock);
1160		u132_hcd_giveback_urb(u132, endp, urb, 0);
1161		return;
1162	}
1163}
1164
1165static void u132_hcd_initial_empty_sent(void *data, struct urb *urb, u8 *buf,
1166	int len, int toggle_bits, int error_count, int condition_code,
1167	int repeat_number, int halted, int skipped, int actual, int non_null)
1168{
1169	struct u132_endp *endp = data;
1170	struct u132 *u132 = endp->u132;
1171	mutex_lock(&u132->scheduler_lock);
1172	if (u132->going > 1) {
1173		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1174			, u132->going);
1175		mutex_unlock(&u132->scheduler_lock);
1176		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1177		return;
1178	} else if (endp->dequeueing) {
1179		endp->dequeueing = 0;
1180		mutex_unlock(&u132->scheduler_lock);
1181		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1182		return;
1183	} else if (u132->going > 0) {
1184		dev_err(&u132->platform_dev->dev, "device is being removed "
1185				"urb=%p\n", urb);
1186		mutex_unlock(&u132->scheduler_lock);
1187		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1188		return;
1189	} else if (!urb->unlinked) {
1190		mutex_unlock(&u132->scheduler_lock);
1191		u132_hcd_giveback_urb(u132, endp, urb, 0);
1192		return;
1193	} else {
1194		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1195				"unlinked=%d\n", urb, urb->unlinked);
1196		mutex_unlock(&u132->scheduler_lock);
1197		u132_hcd_giveback_urb(u132, endp, urb, 0);
1198		return;
1199	}
1200}
1201
1202static void u132_hcd_initial_input_recv(void *data, struct urb *urb, u8 *buf,
1203	int len, int toggle_bits, int error_count, int condition_code,
1204	int repeat_number, int halted, int skipped, int actual, int non_null)
1205{
1206	struct u132_endp *endp = data;
1207	struct u132 *u132 = endp->u132;
1208	u8 address = u132->addr[endp->usb_addr].address;
1209	mutex_lock(&u132->scheduler_lock);
1210	if (u132->going > 1) {
1211		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1212			, u132->going);
1213		mutex_unlock(&u132->scheduler_lock);
1214		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1215		return;
1216	} else if (endp->dequeueing) {
1217		endp->dequeueing = 0;
1218		mutex_unlock(&u132->scheduler_lock);
1219		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1220		return;
1221	} else if (u132->going > 0) {
1222		dev_err(&u132->platform_dev->dev, "device is being removed "
1223				"urb=%p\n", urb);
1224		mutex_unlock(&u132->scheduler_lock);
1225		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1226		return;
1227	} else if (!urb->unlinked) {
1228		int retval;
1229		struct u132_ring *ring = endp->ring;
1230		u8 *u = urb->transfer_buffer;
1231		u8 *b = buf;
1232		int L = len;
1233
1234		while (L-- > 0)
1235			*u++ = *b++;
1236
1237		urb->actual_length = len;
1238		mutex_unlock(&u132->scheduler_lock);
1239		retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
1240			ring->number, endp, urb, address, endp->usb_endp, 0x3,
1241			u132_hcd_initial_empty_sent);
1242		if (retval != 0)
1243			u132_hcd_giveback_urb(u132, endp, urb, retval);
1244		return;
1245	} else {
1246		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1247				"unlinked=%d\n", urb, urb->unlinked);
1248		mutex_unlock(&u132->scheduler_lock);
1249		u132_hcd_giveback_urb(u132, endp, urb, 0);
1250		return;
1251	}
1252}
1253
1254static void u132_hcd_initial_setup_sent(void *data, struct urb *urb, u8 *buf,
1255	int len, int toggle_bits, int error_count, int condition_code,
1256	int repeat_number, int halted, int skipped, int actual, int non_null)
1257{
1258	struct u132_endp *endp = data;
1259	struct u132 *u132 = endp->u132;
1260	u8 address = u132->addr[endp->usb_addr].address;
1261	mutex_lock(&u132->scheduler_lock);
1262	if (u132->going > 1) {
1263		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1264			, u132->going);
1265		mutex_unlock(&u132->scheduler_lock);
1266		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1267		return;
1268	} else if (endp->dequeueing) {
1269		endp->dequeueing = 0;
1270		mutex_unlock(&u132->scheduler_lock);
1271		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1272		return;
1273	} else if (u132->going > 0) {
1274		dev_err(&u132->platform_dev->dev, "device is being removed "
1275				"urb=%p\n", urb);
1276		mutex_unlock(&u132->scheduler_lock);
1277		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1278		return;
1279	} else if (!urb->unlinked) {
1280		int retval;
1281		struct u132_ring *ring = endp->ring;
1282		mutex_unlock(&u132->scheduler_lock);
1283		retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1284			ring->number, endp, urb, address, endp->usb_endp, 0,
1285			u132_hcd_initial_input_recv);
1286		if (retval != 0)
1287			u132_hcd_giveback_urb(u132, endp, urb, retval);
1288		return;
1289	} else {
1290		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1291				"unlinked=%d\n", urb, urb->unlinked);
1292		mutex_unlock(&u132->scheduler_lock);
1293		u132_hcd_giveback_urb(u132, endp, urb, 0);
1294		return;
1295	}
1296}
1297
1298/*
1299* this work function is only executed from the work queue
1300*
1301*/
1302static void u132_hcd_ring_work_scheduler(struct work_struct *work)
1303{
1304	struct u132_ring *ring =
1305		container_of(work, struct u132_ring, scheduler.work);
1306	struct u132 *u132 = ring->u132;
1307	mutex_lock(&u132->scheduler_lock);
1308	if (ring->in_use) {
1309		mutex_unlock(&u132->scheduler_lock);
1310		u132_ring_put_kref(u132, ring);
1311		return;
1312	} else if (ring->curr_endp) {
1313		struct u132_endp *last_endp = ring->curr_endp;
1314		struct list_head *scan;
1315		struct list_head *head = &last_endp->endp_ring;
1316		unsigned long wakeup = 0;
1317		list_for_each(scan, head) {
1318			struct u132_endp *endp = list_entry(scan,
1319				struct u132_endp, endp_ring);
1320			if (endp->queue_next == endp->queue_last) {
1321			} else if ((endp->delayed == 0)
1322				|| time_after_eq(jiffies, endp->jiffies)) {
1323				ring->curr_endp = endp;
1324				u132_endp_cancel_work(u132, last_endp);
1325				u132_endp_queue_work(u132, last_endp, 0);
1326				mutex_unlock(&u132->scheduler_lock);
1327				u132_ring_put_kref(u132, ring);
1328				return;
1329			} else {
1330				unsigned long delta = endp->jiffies - jiffies;
1331				if (delta > wakeup)
1332					wakeup = delta;
1333			}
1334		}
1335		if (last_endp->queue_next == last_endp->queue_last) {
1336		} else if ((last_endp->delayed == 0) || time_after_eq(jiffies,
1337			last_endp->jiffies)) {
1338			u132_endp_cancel_work(u132, last_endp);
1339			u132_endp_queue_work(u132, last_endp, 0);
1340			mutex_unlock(&u132->scheduler_lock);
1341			u132_ring_put_kref(u132, ring);
1342			return;
1343		} else {
1344			unsigned long delta = last_endp->jiffies - jiffies;
1345			if (delta > wakeup)
1346				wakeup = delta;
1347		}
1348		if (wakeup > 0) {
1349			u132_ring_requeue_work(u132, ring, wakeup);
1350			mutex_unlock(&u132->scheduler_lock);
1351			return;
1352		} else {
1353			mutex_unlock(&u132->scheduler_lock);
1354			u132_ring_put_kref(u132, ring);
1355			return;
1356		}
1357	} else {
1358		mutex_unlock(&u132->scheduler_lock);
1359		u132_ring_put_kref(u132, ring);
1360		return;
1361	}
1362}
1363
1364static void u132_hcd_endp_work_scheduler(struct work_struct *work)
1365{
1366	struct u132_ring *ring;
1367	struct u132_endp *endp =
1368		container_of(work, struct u132_endp, scheduler.work);
1369	struct u132 *u132 = endp->u132;
1370	mutex_lock(&u132->scheduler_lock);
1371	ring = endp->ring;
1372	if (endp->edset_flush) {
1373		endp->edset_flush = 0;
1374		if (endp->dequeueing)
1375			usb_ftdi_elan_edset_flush(u132->platform_dev,
1376				ring->number, endp);
1377		mutex_unlock(&u132->scheduler_lock);
1378		u132_endp_put_kref(u132, endp);
1379		return;
1380	} else if (endp->active) {
1381		mutex_unlock(&u132->scheduler_lock);
1382		u132_endp_put_kref(u132, endp);
1383		return;
1384	} else if (ring->in_use) {
1385		mutex_unlock(&u132->scheduler_lock);
1386		u132_endp_put_kref(u132, endp);
1387		return;
1388	} else if (endp->queue_next == endp->queue_last) {
1389		mutex_unlock(&u132->scheduler_lock);
1390		u132_endp_put_kref(u132, endp);
1391		return;
1392	} else if (endp->pipetype == PIPE_INTERRUPT) {
1393		u8 address = u132->addr[endp->usb_addr].address;
1394		if (ring->in_use) {
1395			mutex_unlock(&u132->scheduler_lock);
1396			u132_endp_put_kref(u132, endp);
1397			return;
1398		} else {
1399			int retval;
1400			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1401				endp->queue_next];
1402			endp->active = 1;
1403			ring->curr_endp = endp;
1404			ring->in_use = 1;
1405			mutex_unlock(&u132->scheduler_lock);
1406			retval = edset_single(u132, ring, endp, urb, address,
1407				endp->toggle_bits, u132_hcd_interrupt_recv);
1408			if (retval != 0)
1409				u132_hcd_giveback_urb(u132, endp, urb, retval);
1410			return;
1411		}
1412	} else if (endp->pipetype == PIPE_CONTROL) {
1413		u8 address = u132->addr[endp->usb_addr].address;
1414		if (ring->in_use) {
1415			mutex_unlock(&u132->scheduler_lock);
1416			u132_endp_put_kref(u132, endp);
1417			return;
1418		} else if (address == 0) {
1419			int retval;
1420			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1421				endp->queue_next];
1422			endp->active = 1;
1423			ring->curr_endp = endp;
1424			ring->in_use = 1;
1425			mutex_unlock(&u132->scheduler_lock);
1426			retval = edset_setup(u132, ring, endp, urb, address,
1427				0x2, u132_hcd_initial_setup_sent);
1428			if (retval != 0)
1429				u132_hcd_giveback_urb(u132, endp, urb, retval);
1430			return;
1431		} else if (endp->usb_addr == 0) {
1432			int retval;
1433			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1434				endp->queue_next];
1435			endp->active = 1;
1436			ring->curr_endp = endp;
1437			ring->in_use = 1;
1438			mutex_unlock(&u132->scheduler_lock);
1439			retval = edset_setup(u132, ring, endp, urb, 0, 0x2,
1440				u132_hcd_enumeration_address_sent);
1441			if (retval != 0)
1442				u132_hcd_giveback_urb(u132, endp, urb, retval);
1443			return;
1444		} else {
1445			int retval;
1446			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1447				endp->queue_next];
1448			address = u132->addr[endp->usb_addr].address;
1449			endp->active = 1;
1450			ring->curr_endp = endp;
1451			ring->in_use = 1;
1452			mutex_unlock(&u132->scheduler_lock);
1453			retval = edset_setup(u132, ring, endp, urb, address,
1454				0x2, u132_hcd_configure_setup_sent);
1455			if (retval != 0)
1456				u132_hcd_giveback_urb(u132, endp, urb, retval);
1457			return;
1458		}
1459	} else {
1460		if (endp->input) {
1461			u8 address = u132->addr[endp->usb_addr].address;
1462			if (ring->in_use) {
1463				mutex_unlock(&u132->scheduler_lock);
1464				u132_endp_put_kref(u132, endp);
1465				return;
1466			} else {
1467				int retval;
1468				struct urb *urb = endp->urb_list[
1469					ENDP_QUEUE_MASK & endp->queue_next];
1470				endp->active = 1;
1471				ring->curr_endp = endp;
1472				ring->in_use = 1;
1473				mutex_unlock(&u132->scheduler_lock);
1474				retval = edset_input(u132, ring, endp, urb,
1475					address, endp->toggle_bits,
1476					u132_hcd_bulk_input_recv);
1477				if (retval == 0) {
1478				} else
1479					u132_hcd_giveback_urb(u132, endp, urb,
1480						retval);
1481				return;
1482			}
1483		} else {	/* output pipe */
1484			u8 address = u132->addr[endp->usb_addr].address;
1485			if (ring->in_use) {
1486				mutex_unlock(&u132->scheduler_lock);
1487				u132_endp_put_kref(u132, endp);
1488				return;
1489			} else {
1490				int retval;
1491				struct urb *urb = endp->urb_list[
1492					ENDP_QUEUE_MASK & endp->queue_next];
1493				endp->active = 1;
1494				ring->curr_endp = endp;
1495				ring->in_use = 1;
1496				mutex_unlock(&u132->scheduler_lock);
1497				retval = edset_output(u132, ring, endp, urb,
1498					address, endp->toggle_bits,
1499					u132_hcd_bulk_output_sent);
1500				if (retval == 0) {
1501				} else
1502					u132_hcd_giveback_urb(u132, endp, urb,
1503						retval);
1504				return;
1505			}
1506		}
1507	}
1508}
1509#ifdef CONFIG_PM
1510
1511static void port_power(struct u132 *u132, int pn, int is_on)
1512{
1513	u132->port[pn].power = is_on;
1514}
1515
1516#endif
1517
1518static void u132_power(struct u132 *u132, int is_on)
1519{
1520	struct usb_hcd *hcd = u132_to_hcd(u132)
1521		;	/* hub is inactive unless the port is powered */
1522	if (is_on) {
1523		if (u132->power)
1524			return;
1525		u132->power = 1;
1526	} else {
1527		u132->power = 0;
1528		hcd->state = HC_STATE_HALT;
1529	}
1530}
1531
1532static int u132_periodic_reinit(struct u132 *u132)
1533{
1534	int retval;
1535	u32 fi = u132->hc_fminterval & 0x03fff;
1536	u32 fit;
1537	u32 fminterval;
1538	retval = u132_read_pcimem(u132, fminterval, &fminterval);
1539	if (retval)
1540		return retval;
1541	fit = fminterval & FIT;
1542	retval = u132_write_pcimem(u132, fminterval,
1543		(fit ^ FIT) | u132->hc_fminterval);
1544	if (retval)
1545		return retval;
1546	retval = u132_write_pcimem(u132, periodicstart,
1547		((9 * fi) / 10) & 0x3fff);
1548	if (retval)
1549		return retval;
1550	return 0;
1551}
1552
1553static char *hcfs2string(int state)
1554{
1555	switch (state) {
1556	case OHCI_USB_RESET:
1557		return "reset";
1558	case OHCI_USB_RESUME:
1559		return "resume";
1560	case OHCI_USB_OPER:
1561		return "operational";
1562	case OHCI_USB_SUSPEND:
1563		return "suspend";
1564	}
1565	return "?";
1566}
1567
1568static int u132_init(struct u132 *u132)
1569{
1570	int retval;
1571	u32 control;
1572	u132_disable(u132);
1573	u132->next_statechange = jiffies;
1574	retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
1575	if (retval)
1576		return retval;
1577	retval = u132_read_pcimem(u132, control, &control);
1578	if (retval)
1579		return retval;
1580	if (u132->num_ports == 0) {
1581		u32 rh_a = -1;
1582		retval = u132_read_pcimem(u132, roothub.a, &rh_a);
1583		if (retval)
1584			return retval;
1585		u132->num_ports = rh_a & RH_A_NDP;
1586		retval = read_roothub_info(u132);
1587		if (retval)
1588			return retval;
1589	}
1590	if (u132->num_ports > MAX_U132_PORTS)
1591		return -EINVAL;
1592
1593	return 0;
1594}
1595
1596
1597/* Start an OHCI controller, set the BUS operational
1598* resets USB and controller
1599* enable interrupts
1600*/
1601static int u132_run(struct u132 *u132)
1602{
1603	int retval;
1604	u32 control;
1605	u32 status;
1606	u32 fminterval;
1607	u32 periodicstart;
1608	u32 cmdstatus;
1609	u32 roothub_a;
1610	int mask = OHCI_INTR_INIT;
1611	int first = u132->hc_fminterval == 0;
1612	int sleep_time = 0;
1613	int reset_timeout = 30;	/* ... allow extra time */
1614	u132_disable(u132);
1615	if (first) {
1616		u32 temp;
1617		retval = u132_read_pcimem(u132, fminterval, &temp);
1618		if (retval)
1619			return retval;
1620		u132->hc_fminterval = temp & 0x3fff;
1621		u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
1622	}
1623	retval = u132_read_pcimem(u132, control, &u132->hc_control);
1624	if (retval)
1625		return retval;
1626	dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
1627		"= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
1628		u132->hc_control);
1629	switch (u132->hc_control & OHCI_CTRL_HCFS) {
1630	case OHCI_USB_OPER:
1631		sleep_time = 0;
1632		break;
1633	case OHCI_USB_SUSPEND:
1634	case OHCI_USB_RESUME:
1635		u132->hc_control &= OHCI_CTRL_RWC;
1636		u132->hc_control |= OHCI_USB_RESUME;
1637		sleep_time = 10;
1638		break;
1639	default:
1640		u132->hc_control &= OHCI_CTRL_RWC;
1641		u132->hc_control |= OHCI_USB_RESET;
1642		sleep_time = 50;
1643		break;
1644	}
1645	retval = u132_write_pcimem(u132, control, u132->hc_control);
1646	if (retval)
1647		return retval;
1648	retval = u132_read_pcimem(u132, control, &control);
1649	if (retval)
1650		return retval;
1651	msleep(sleep_time);
1652	retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1653	if (retval)
1654		return retval;
1655	if (!(roothub_a & RH_A_NPS)) {
1656		int temp;	/* power down each port */
1657		for (temp = 0; temp < u132->num_ports; temp++) {
1658			retval = u132_write_pcimem(u132,
1659				roothub.portstatus[temp], RH_PS_LSDA);
1660			if (retval)
1661				return retval;
1662		}
1663	}
1664	retval = u132_read_pcimem(u132, control, &control);
1665	if (retval)
1666		return retval;
1667retry:
1668	retval = u132_read_pcimem(u132, cmdstatus, &status);
1669	if (retval)
1670		return retval;
1671	retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
1672	if (retval)
1673		return retval;
1674extra:	{
1675		retval = u132_read_pcimem(u132, cmdstatus, &status);
1676		if (retval)
1677			return retval;
1678		if (0 != (status & OHCI_HCR)) {
1679			if (--reset_timeout == 0) {
1680				dev_err(&u132->platform_dev->dev, "USB HC reset"
1681					" timed out!\n");
1682				return -ENODEV;
1683			} else {
1684				msleep(5);
1685				goto extra;
1686			}
1687		}
1688	}
1689	if (u132->flags & OHCI_QUIRK_INITRESET) {
1690		retval = u132_write_pcimem(u132, control, u132->hc_control);
1691		if (retval)
1692			return retval;
1693		retval = u132_read_pcimem(u132, control, &control);
1694		if (retval)
1695			return retval;
1696	}
1697	retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
1698	if (retval)
1699		return retval;
1700	retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
1701	if (retval)
1702		return retval;
1703	retval = u132_write_pcimem(u132, hcca, 0x00000000);
1704	if (retval)
1705		return retval;
1706	retval = u132_periodic_reinit(u132);
1707	if (retval)
1708		return retval;
1709	retval = u132_read_pcimem(u132, fminterval, &fminterval);
1710	if (retval)
1711		return retval;
1712	retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
1713	if (retval)
1714		return retval;
1715	if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
1716		if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
1717			u132->flags |= OHCI_QUIRK_INITRESET;
1718			goto retry;
1719		} else
1720			dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
1721				"\n", fminterval, periodicstart);
1722	}			/* start controller operations */
1723	u132->hc_control &= OHCI_CTRL_RWC;
1724	u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
1725	retval = u132_write_pcimem(u132, control, u132->hc_control);
1726	if (retval)
1727		return retval;
1728	retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
1729	if (retval)
1730		return retval;
1731	retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
1732	if (retval)
1733		return retval;
1734	retval = u132_read_pcimem(u132, control, &control);
1735	if (retval)
1736		return retval;
1737	u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1738	retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
1739	if (retval)
1740		return retval;
1741	retval = u132_write_pcimem(u132, intrstatus, mask);
1742	if (retval)
1743		return retval;
1744	retval = u132_write_pcimem(u132, intrdisable,
1745		OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
1746		OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
1747		OHCI_INTR_SO);
1748	if (retval)
1749		return retval;	/* handle root hub init quirks ... */
1750	retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1751	if (retval)
1752		return retval;
1753	roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
1754	if (u132->flags & OHCI_QUIRK_SUPERIO) {
1755		roothub_a |= RH_A_NOCP;
1756		roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
1757		retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1758		if (retval)
1759			return retval;
1760	} else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
1761		roothub_a |= RH_A_NPS;
1762		retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1763		if (retval)
1764			return retval;
1765	}
1766	retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
1767	if (retval)
1768		return retval;
1769	retval = u132_write_pcimem(u132, roothub.b,
1770		(roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
1771	if (retval)
1772		return retval;
1773	retval = u132_read_pcimem(u132, control, &control);
1774	if (retval)
1775		return retval;
1776	mdelay((roothub_a >> 23) & 0x1fe);
1777	u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1778	return 0;
1779}
1780
1781static void u132_hcd_stop(struct usb_hcd *hcd)
1782{
1783	struct u132 *u132 = hcd_to_u132(hcd);
1784	if (u132->going > 1) {
1785		dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
1786			"een removed %d\n", u132, hcd, u132->going);
1787	} else if (u132->going > 0) {
1788		dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
1789			"ed\n", hcd);
1790	} else {
1791		mutex_lock(&u132->sw_lock);
1792		msleep(100);
1793		u132_power(u132, 0);
1794		mutex_unlock(&u132->sw_lock);
1795	}
1796}
1797
1798static int u132_hcd_start(struct usb_hcd *hcd)
1799{
1800	struct u132 *u132 = hcd_to_u132(hcd);
1801	if (u132->going > 1) {
1802		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1803			, u132->going);
1804		return -ENODEV;
1805	} else if (u132->going > 0) {
1806		dev_err(&u132->platform_dev->dev, "device is being removed\n");
1807		return -ESHUTDOWN;
1808	} else if (hcd->self.controller) {
1809		int retval;
1810		struct platform_device *pdev =
1811			to_platform_device(hcd->self.controller);
1812		u16 vendor = ((struct u132_platform_data *)
1813			(pdev->dev.platform_data))->vendor;
1814		u16 device = ((struct u132_platform_data *)
1815			(pdev->dev.platform_data))->device;
1816		mutex_lock(&u132->sw_lock);
1817		msleep(10);
1818		if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
1819			u132->flags = OHCI_QUIRK_AMD756;
1820		} else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
1821			dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
1822				"ounds unavailable\n");
1823		} else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
1824			u132->flags |= OHCI_QUIRK_ZFMICRO;
1825		retval = u132_run(u132);
1826		if (retval) {
1827			u132_disable(u132);
1828			u132->going = 1;
1829		}
1830		msleep(100);
1831		mutex_unlock(&u132->sw_lock);
1832		return retval;
1833	} else {
1834		dev_err(&u132->platform_dev->dev, "platform_device missing\n");
1835		return -ENODEV;
1836	}
1837}
1838
1839static int u132_hcd_reset(struct usb_hcd *hcd)
1840{
1841	struct u132 *u132 = hcd_to_u132(hcd);
1842	if (u132->going > 1) {
1843		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1844			, u132->going);
1845		return -ENODEV;
1846	} else if (u132->going > 0) {
1847		dev_err(&u132->platform_dev->dev, "device is being removed\n");
1848		return -ESHUTDOWN;
1849	} else {
1850		int retval;
1851		mutex_lock(&u132->sw_lock);
1852		retval = u132_init(u132);
1853		if (retval) {
1854			u132_disable(u132);
1855			u132->going = 1;
1856		}
1857		mutex_unlock(&u132->sw_lock);
1858		return retval;
1859	}
1860}
1861
1862static int create_endpoint_and_queue_int(struct u132 *u132,
1863	struct u132_udev *udev, struct urb *urb,
1864	struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1865	gfp_t mem_flags)
1866{
1867	struct u132_ring *ring;
1868	unsigned long irqs;
1869	int rc;
1870	u8 endp_number;
1871	struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1872
1873	if (!endp)
1874		return -ENOMEM;
1875
1876	spin_lock_init(&endp->queue_lock.slock);
1877	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1878	rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1879	if (rc) {
1880		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1881		kfree(endp);
1882		return rc;
1883	}
1884
1885	endp_number = ++u132->num_endpoints;
1886	urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1887	INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1888	INIT_LIST_HEAD(&endp->urb_more);
1889	ring = endp->ring = &u132->ring[0];
1890	if (ring->curr_endp) {
1891		list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
1892	} else {
1893		INIT_LIST_HEAD(&endp->endp_ring);
1894		ring->curr_endp = endp;
1895	}
1896	ring->length += 1;
1897	endp->dequeueing = 0;
1898	endp->edset_flush = 0;
1899	endp->active = 0;
1900	endp->delayed = 0;
1901	endp->endp_number = endp_number;
1902	endp->u132 = u132;
1903	endp->hep = urb->ep;
1904	endp->pipetype = usb_pipetype(urb->pipe);
1905	u132_endp_init_kref(u132, endp);
1906	if (usb_pipein(urb->pipe)) {
1907		endp->toggle_bits = 0x2;
1908		usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1909		endp->input = 1;
1910		endp->output = 0;
1911		udev->endp_number_in[usb_endp] = endp_number;
1912		u132_udev_get_kref(u132, udev);
1913	} else {
1914		endp->toggle_bits = 0x2;
1915		usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1916		endp->input = 0;
1917		endp->output = 1;
1918		udev->endp_number_out[usb_endp] = endp_number;
1919		u132_udev_get_kref(u132, udev);
1920	}
1921	urb->hcpriv = u132;
1922	endp->delayed = 1;
1923	endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1924	endp->udev_number = address;
1925	endp->usb_addr = usb_addr;
1926	endp->usb_endp = usb_endp;
1927	endp->queue_size = 1;
1928	endp->queue_last = 0;
1929	endp->queue_next = 0;
1930	endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1931	spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1932	u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
1933	return 0;
1934}
1935
1936static int queue_int_on_old_endpoint(struct u132 *u132,
1937	struct u132_udev *udev, struct urb *urb,
1938	struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
1939	u8 usb_endp, u8 address)
1940{
1941	urb->hcpriv = u132;
1942	endp->delayed = 1;
1943	endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1944	if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
1945		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1946	} else {
1947		struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
1948			GFP_ATOMIC);
1949		if (urbq == NULL) {
1950			endp->queue_size -= 1;
1951			return -ENOMEM;
1952		} else {
1953			list_add_tail(&urbq->urb_more, &endp->urb_more);
1954			urbq->urb = urb;
1955		}
1956	}
1957	return 0;
1958}
1959
1960static int create_endpoint_and_queue_bulk(struct u132 *u132,
1961	struct u132_udev *udev, struct urb *urb,
1962	struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1963	gfp_t mem_flags)
1964{
1965	int ring_number;
1966	struct u132_ring *ring;
1967	unsigned long irqs;
1968	int rc;
1969	u8 endp_number;
1970	struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1971
1972	if (!endp)
1973		return -ENOMEM;
1974
1975	spin_lock_init(&endp->queue_lock.slock);
1976	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1977	rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1978	if (rc) {
1979		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1980		kfree(endp);
1981		return rc;
1982	}
1983
1984	endp_number = ++u132->num_endpoints;
1985	urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1986	INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1987	INIT_LIST_HEAD(&endp->urb_more);
1988	endp->dequeueing = 0;
1989	endp->edset_flush = 0;
1990	endp->active = 0;
1991	endp->delayed = 0;
1992	endp->endp_number = endp_number;
1993	endp->u132 = u132;
1994	endp->hep = urb->ep;
1995	endp->pipetype = usb_pipetype(urb->pipe);
1996	u132_endp_init_kref(u132, endp);
1997	if (usb_pipein(urb->pipe)) {
1998		endp->toggle_bits = 0x2;
1999		usb_settoggle(udev->usb_device, usb_endp, 0, 0);
2000		ring_number = 3;
2001		endp->input = 1;
2002		endp->output = 0;
2003		udev->endp_number_in[usb_endp] = endp_number;
2004		u132_udev_get_kref(u132, udev);
2005	} else {
2006		endp->toggle_bits = 0x2;
2007		usb_settoggle(udev->usb_device, usb_endp, 1, 0);
2008		ring_number = 2;
2009		endp->input = 0;
2010		endp->output = 1;
2011		udev->endp_number_out[usb_endp] = endp_number;
2012		u132_udev_get_kref(u132, udev);
2013	}
2014	ring = endp->ring = &u132->ring[ring_number - 1];
2015	if (ring->curr_endp) {
2016		list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2017	} else {
2018		INIT_LIST_HEAD(&endp->endp_ring);
2019		ring->curr_endp = endp;
2020	}
2021	ring->length += 1;
2022	urb->hcpriv = u132;
2023	endp->udev_number = address;
2024	endp->usb_addr = usb_addr;
2025	endp->usb_endp = usb_endp;
2026	endp->queue_size = 1;
2027	endp->queue_last = 0;
2028	endp->queue_next = 0;
2029	endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2030	spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2031	u132_endp_queue_work(u132, endp, 0);
2032	return 0;
2033}
2034
2035static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
2036	struct urb *urb,
2037	struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2038	u8 usb_endp, u8 address)
2039{
2040	urb->hcpriv = u132;
2041	if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2042		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2043	} else {
2044		struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
2045			GFP_ATOMIC);
2046		if (urbq == NULL) {
2047			endp->queue_size -= 1;
2048			return -ENOMEM;
2049		} else {
2050			list_add_tail(&urbq->urb_more, &endp->urb_more);
2051			urbq->urb = urb;
2052		}
2053	}
2054	return 0;
2055}
2056
2057static int create_endpoint_and_queue_control(struct u132 *u132,
2058	struct urb *urb,
2059	struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
2060	gfp_t mem_flags)
2061{
2062	struct u132_ring *ring;
2063	unsigned long irqs;
2064	int rc;
2065	u8 endp_number;
2066	struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
2067
2068	if (!endp)
2069		return -ENOMEM;
2070
2071	spin_lock_init(&endp->queue_lock.slock);
2072	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2073	rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
2074	if (rc) {
2075		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2076		kfree(endp);
2077		return rc;
2078	}
2079
2080	endp_number = ++u132->num_endpoints;
2081	urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
2082	INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2083	INIT_LIST_HEAD(&endp->urb_more);
2084	ring = endp->ring = &u132->ring[0];
2085	if (ring->curr_endp) {
2086		list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2087	} else {
2088		INIT_LIST_HEAD(&endp->endp_ring);
2089		ring->curr_endp = endp;
2090	}
2091	ring->length += 1;
2092	endp->dequeueing = 0;
2093	endp->edset_flush = 0;
2094	endp->active = 0;
2095	endp->delayed = 0;
2096	endp->endp_number = endp_number;
2097	endp->u132 = u132;
2098	endp->hep = urb->ep;
2099	u132_endp_init_kref(u132, endp);
2100	u132_endp_get_kref(u132, endp);
2101	if (usb_addr == 0) {
2102		u8 address = u132->addr[usb_addr].address;
2103		struct u132_udev *udev = &u132->udev[address];
2104		endp->udev_number = address;
2105		endp->usb_addr = usb_addr;
2106		endp->usb_endp = usb_endp;
2107		endp->input = 1;
2108		endp->output = 1;
2109		endp->pipetype = usb_pipetype(urb->pipe);
2110		u132_udev_init_kref(u132, udev);
2111		u132_udev_get_kref(u132, udev);
2112		udev->endp_number_in[usb_endp] = endp_number;
2113		udev->endp_number_out[usb_endp] = endp_number;
2114		urb->hcpriv = u132;
2115		endp->queue_size = 1;
2116		endp->queue_last = 0;
2117		endp->queue_next = 0;
2118		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2119		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2120		u132_endp_queue_work(u132, endp, 0);
2121		return 0;
2122	} else {		/*(usb_addr > 0) */
2123		u8 address = u132->addr[usb_addr].address;
2124		struct u132_udev *udev = &u132->udev[address];
2125		endp->udev_number = address;
2126		endp->usb_addr = usb_addr;
2127		endp->usb_endp = usb_endp;
2128		endp->input = 1;
2129		endp->output = 1;
2130		endp->pipetype = usb_pipetype(urb->pipe);
2131		u132_udev_get_kref(u132, udev);
2132		udev->enumeration = 2;
2133		udev->endp_number_in[usb_endp] = endp_number;
2134		udev->endp_number_out[usb_endp] = endp_number;
2135		urb->hcpriv = u132;
2136		endp->queue_size = 1;
2137		endp->queue_last = 0;
2138		endp->queue_next = 0;
2139		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2140		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2141		u132_endp_queue_work(u132, endp, 0);
2142		return 0;
2143	}
2144}
2145
2146static int queue_control_on_old_endpoint(struct u132 *u132,
2147	struct urb *urb,
2148	struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2149	u8 usb_endp)
2150{
2151	if (usb_addr == 0) {
2152		if (usb_pipein(urb->pipe)) {
2153			urb->hcpriv = u132;
2154			if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2155				endp->urb_list[ENDP_QUEUE_MASK &
2156					endp->queue_last++] = urb;
2157			} else {
2158				struct u132_urbq *urbq =
2159					kmalloc(sizeof(struct u132_urbq),
2160					GFP_ATOMIC);
2161				if (urbq == NULL) {
2162					endp->queue_size -= 1;
2163					return -ENOMEM;
2164				} else {
2165					list_add_tail(&urbq->urb_more,
2166						&endp->urb_more);
2167					urbq->urb = urb;
2168				}
2169			}
2170			return 0;
2171		} else {	/* usb_pipeout(urb->pipe) */
2172			struct u132_addr *addr = &u132->addr[usb_dev->devnum];
2173			int I = MAX_U132_UDEVS;
2174			int i = 0;
2175			while (--I > 0) {
2176				struct u132_udev *udev = &u132->udev[++i];
2177				if (udev->usb_device) {
2178					continue;
2179				} else {
2180					udev->enumeration = 1;
2181					u132->addr[0].address = i;
2182					endp->udev_number = i;
2183					udev->udev_number = i;
2184					udev->usb_addr = usb_dev->devnum;
2185					u132_udev_init_kref(u132, udev);
2186					udev->endp_number_in[usb_endp] =
2187						endp->endp_number;
2188					u132_udev_get_kref(u132, udev);
2189					udev->endp_number_out[usb_endp] =
2190						endp->endp_number;
2191					udev->usb_device = usb_dev;
2192					((u8 *) (urb->setup_packet))[2] =
2193						addr->address = i;
2194					u132_udev_get_kref(u132, udev);
2195					break;
2196				}
2197			}
2198			if (I == 0) {
2199				dev_err(&u132->platform_dev->dev, "run out of d"
2200					"evice space\n");
2201				return -EINVAL;
2202			}
2203			urb->hcpriv = u132;
2204			if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2205				endp->urb_list[ENDP_QUEUE_MASK &
2206					endp->queue_last++] = urb;
2207			} else {
2208				struct u132_urbq *urbq =
2209					kmalloc(sizeof(struct u132_urbq),
2210					GFP_ATOMIC);
2211				if (urbq == NULL) {
2212					endp->queue_size -= 1;
2213					return -ENOMEM;
2214				} else {
2215					list_add_tail(&urbq->urb_more,
2216						&endp->urb_more);
2217					urbq->urb = urb;
2218				}
2219			}
2220			return 0;
2221		}
2222	} else {		/*(usb_addr > 0) */
2223		u8 address = u132->addr[usb_addr].address;
2224		struct u132_udev *udev = &u132->udev[address];
2225		urb->hcpriv = u132;
2226		if (udev->enumeration != 2)
2227			udev->enumeration = 2;
2228		if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2229			endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
2230				urb;
2231		} else {
2232			struct u132_urbq *urbq =
2233				kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
2234			if (urbq == NULL) {
2235				endp->queue_size -= 1;
2236				return -ENOMEM;
2237			} else {
2238				list_add_tail(&urbq->urb_more, &endp->urb_more);
2239				urbq->urb = urb;
2240			}
2241		}
2242		return 0;
2243	}
2244}
2245
2246static int u132_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2247		gfp_t mem_flags)
2248{
2249	struct u132 *u132 = hcd_to_u132(hcd);
2250	if (irqs_disabled()) {
2251		if (__GFP_WAIT & mem_flags) {
2252			printk(KERN_ERR "invalid context for function that migh"
2253				"t sleep\n");
2254			return -EINVAL;
2255		}
2256	}
2257	if (u132->going > 1) {
2258		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2259			, u132->going);
2260		return -ENODEV;
2261	} else if (u132->going > 0) {
2262		dev_err(&u132->platform_dev->dev, "device is being removed "
2263				"urb=%p\n", urb);
2264		return -ESHUTDOWN;
2265	} else {
2266		u8 usb_addr = usb_pipedevice(urb->pipe);
2267		u8 usb_endp = usb_pipeendpoint(urb->pipe);
2268		struct usb_device *usb_dev = urb->dev;
2269		if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2270			u8 address = u132->addr[usb_addr].address;
2271			struct u132_udev *udev = &u132->udev[address];
2272			struct u132_endp *endp = urb->ep->hcpriv;
2273			urb->actual_length = 0;
2274			if (endp) {
2275				unsigned long irqs;
2276				int retval;
2277				spin_lock_irqsave(&endp->queue_lock.slock,
2278					irqs);
2279				retval = usb_hcd_link_urb_to_ep(hcd, urb);
2280				if (retval == 0) {
2281					retval = queue_int_on_old_endpoint(
2282							u132, udev, urb,
2283							usb_dev, endp,
2284							usb_addr, usb_endp,
2285							address);
2286					if (retval)
2287						usb_hcd_unlink_urb_from_ep(
2288	hcd, urb);
2289				}
2290				spin_unlock_irqrestore(&endp->queue_lock.slock,
2291					irqs);
2292				if (retval) {
2293					return retval;
2294				} else {
2295					u132_endp_queue_work(u132, endp,
2296						msecs_to_jiffies(urb->interval))
2297						;
2298					return 0;
2299				}
2300			} else if (u132->num_endpoints == MAX_U132_ENDPS) {
2301				return -EINVAL;
2302			} else {	/*(endp == NULL) */
2303				return create_endpoint_and_queue_int(u132, udev,
2304						urb, usb_dev, usb_addr,
2305						usb_endp, address, mem_flags);
2306			}
2307		} else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2308			dev_err(&u132->platform_dev->dev, "the hardware does no"
2309				"t support PIPE_ISOCHRONOUS\n");
2310			return -EINVAL;
2311		} else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2312			u8 address = u132->addr[usb_addr].address;
2313			struct u132_udev *udev = &u132->udev[address];
2314			struct u132_endp *endp = urb->ep->hcpriv;
2315			urb->actual_length = 0;
2316			if (endp) {
2317				unsigned long irqs;
2318				int retval;
2319				spin_lock_irqsave(&endp->queue_lock.slock,
2320					irqs);
2321				retval = usb_hcd_link_urb_to_ep(hcd, urb);
2322				if (retval == 0) {
2323					retval = queue_bulk_on_old_endpoint(
2324							u132, udev, urb,
2325							usb_dev, endp,
2326							usb_addr, usb_endp,
2327							address);
2328					if (retval)
2329						usb_hcd_unlink_urb_from_ep(
2330	hcd, urb);
2331				}
2332				spin_unlock_irqrestore(&endp->queue_lock.slock,
2333					irqs);
2334				if (retval) {
2335					return retval;
2336				} else {
2337					u132_endp_queue_work(u132, endp, 0);
2338					return 0;
2339				}
2340			} else if (u132->num_endpoints == MAX_U132_ENDPS) {
2341				return -EINVAL;
2342			} else
2343				return create_endpoint_and_queue_bulk(u132,
2344					udev, urb, usb_dev, usb_addr,
2345					usb_endp, address, mem_flags);
2346		} else {
2347			struct u132_endp *endp = urb->ep->hcpriv;
2348			u16 urb_size = 8;
2349			u8 *b = urb->setup_packet;
2350			int i = 0;
2351			char data[30 * 3 + 4];
2352			char *d = data;
2353			int m = (sizeof(data) - 1) / 3;
2354			int l = 0;
2355			data[0] = 0;
2356			while (urb_size-- > 0) {
2357				if (i > m) {
2358				} else if (i++ < m) {
2359					int w = sprintf(d, " %02X", *b++);
2360					d += w;
2361					l += w;
2362				} else
2363					d += sprintf(d, " ..");
2364			}
2365			if (endp) {
2366				unsigned long irqs;
2367				int retval;
2368				spin_lock_irqsave(&endp->queue_lock.slock,
2369					irqs);
2370				retval = usb_hcd_link_urb_to_ep(hcd, urb);
2371				if (retval == 0) {
2372					retval = queue_control_on_old_endpoint(
2373							u132, urb, usb_dev,
2374							endp, usb_addr,
2375							usb_endp);
2376					if (retval)
2377						usb_hcd_unlink_urb_from_ep(
2378								hcd, urb);
2379				}
2380				spin_unlock_irqrestore(&endp->queue_lock.slock,
2381					irqs);
2382				if (retval) {
2383					return retval;
2384				} else {
2385					u132_endp_queue_work(u132, endp, 0);
2386					return 0;
2387				}
2388			} else if (u132->num_endpoints == MAX_U132_ENDPS) {
2389				return -EINVAL;
2390			} else
2391				return create_endpoint_and_queue_control(u132,
2392					urb, usb_dev, usb_addr, usb_endp,
2393					mem_flags);
2394		}
2395	}
2396}
2397
2398static int dequeue_from_overflow_chain(struct u132 *u132,
2399	struct u132_endp *endp, struct urb *urb)
2400{
2401	struct list_head *scan;
2402	struct list_head *head = &endp->urb_more;
2403	list_for_each(scan, head) {
2404		struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
2405			urb_more);
2406		if (urbq->urb == urb) {
2407			struct usb_hcd *hcd = u132_to_hcd(u132);
2408			list_del(scan);
2409			endp->queue_size -= 1;
2410			urb->error_count = 0;
2411			usb_hcd_giveback_urb(hcd, urb, 0);
2412			return 0;
2413		} else
2414			continue;
2415	}
2416	dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
2417		"[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
2418		"\n", urb, endp->endp_number, endp, endp->ring->number,
2419		endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2420		endp->usb_endp, endp->usb_addr, endp->queue_size,
2421		endp->queue_next, endp->queue_last);
2422	return -EINVAL;
2423}
2424
2425static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
2426		struct urb *urb, int status)
2427{
2428	unsigned long irqs;
2429	int rc;
2430
2431	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2432	rc = usb_hcd_check_unlink_urb(u132_to_hcd(u132), urb, status);
2433	if (rc) {
2434		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2435		return rc;
2436	}
2437	if (endp->queue_size == 0) {
2438		dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
2439			"=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
2440			endp->endp_number, endp, endp->ring->number,
2441			endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2442			endp->usb_endp, endp->usb_addr);
2443		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2444		return -EINVAL;
2445	}
2446	if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
2447		if (endp->active) {
2448			endp->dequeueing = 1;
2449			endp->edset_flush = 1;
2450			u132_endp_queue_work(u132, endp, 0);
2451			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2452			return 0;
2453		} else {
2454			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2455			u132_hcd_abandon_urb(u132, endp, urb, status);
2456			return 0;
2457		}
2458	} else {
2459		u16 queue_list = 0;
2460		u16 queue_size = endp->queue_size;
2461		u16 queue_scan = endp->queue_next;
2462		struct urb **urb_slot = NULL;
2463		while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2464			if (urb == endp->urb_list[ENDP_QUEUE_MASK &
2465				++queue_scan]) {
2466				urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2467					queue_scan];
2468				break;
2469			} else
2470				continue;
2471		}
2472		while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2473			*urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
2474				++queue_scan];
2475			urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2476				queue_scan];
2477		}
2478		if (urb_slot) {
2479			struct usb_hcd *hcd = u132_to_hcd(u132);
2480
2481			usb_hcd_unlink_urb_from_ep(hcd, urb);
2482			endp->queue_size -= 1;
2483			if (list_empty(&endp->urb_more)) {
2484				spin_unlock_irqrestore(&endp->queue_lock.slock,
2485					irqs);
2486			} else {
2487				struct list_head *next = endp->urb_more.next;
2488				struct u132_urbq *urbq = list_entry(next,
2489					struct u132_urbq, urb_more);
2490				list_del(next);
2491				*urb_slot = urbq->urb;
2492				spin_unlock_irqrestore(&endp->queue_lock.slock,
2493					irqs);
2494				kfree(urbq);
2495			} urb->error_count = 0;
2496			usb_hcd_giveback_urb(hcd, urb, status);
2497			return 0;
2498		} else if (list_empty(&endp->urb_more)) {
2499			dev_err(&u132->platform_dev->dev, "urb=%p not found in "
2500				"endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
2501				"=%d size=%d next=%04X last=%04X\n", urb,
2502				endp->endp_number, endp, endp->ring->number,
2503				endp->input ? 'I' : ' ',
2504				endp->output ? 'O' : ' ', endp->usb_endp,
2505				endp->usb_addr, endp->queue_size,
2506				endp->queue_next, endp->queue_last);
2507			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2508			return -EINVAL;
2509		} else {
2510			int retval;
2511
2512			usb_hcd_unlink_urb_from_ep(u132_to_hcd(u132), urb);
2513			retval = dequeue_from_overflow_chain(u132, endp,
2514				urb);
2515			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2516			return retval;
2517		}
2518	}
2519}
2520
2521static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2522{
2523	struct u132 *u132 = hcd_to_u132(hcd);
2524	if (u132->going > 2) {
2525		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2526			, u132->going);
2527		return -ENODEV;
2528	} else {
2529		u8 usb_addr = usb_pipedevice(urb->pipe);
2530		u8 usb_endp = usb_pipeendpoint(urb->pipe);
2531		u8 address = u132->addr[usb_addr].address;
2532		struct u132_udev *udev = &u132->udev[address];
2533		if (usb_pipein(urb->pipe)) {
2534			u8 endp_number = udev->endp_number_in[usb_endp];
2535			struct u132_endp *endp = u132->endp[endp_number - 1];
2536			return u132_endp_urb_dequeue(u132, endp, urb, status);
2537		} else {
2538			u8 endp_number = udev->endp_number_out[usb_endp];
2539			struct u132_endp *endp = u132->endp[endp_number - 1];
2540			return u132_endp_urb_dequeue(u132, endp, urb, status);
2541		}
2542	}
2543}
2544
2545static void u132_endpoint_disable(struct usb_hcd *hcd,
2546	struct usb_host_endpoint *hep)
2547{
2548	struct u132 *u132 = hcd_to_u132(hcd);
2549	if (u132->going > 2) {
2550		dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
2551			") has been removed %d\n", u132, hcd, hep,
2552			u132->going);
2553	} else {
2554		struct u132_endp *endp = hep->hcpriv;
2555		if (endp)
2556			u132_endp_put_kref(u132, endp);
2557	}
2558}
2559
2560static int u132_get_frame(struct usb_hcd *hcd)
2561{
2562	struct u132 *u132 = hcd_to_u132(hcd);
2563	if (u132->going > 1) {
2564		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2565			, u132->going);
2566		return -ENODEV;
2567	} else if (u132->going > 0) {
2568		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2569		return -ESHUTDOWN;
2570	} else {
2571		int frame = 0;
2572		dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
2573		msleep(100);
2574		return frame;
2575	}
2576}
2577
2578static int u132_roothub_descriptor(struct u132 *u132,
2579	struct usb_hub_descriptor *desc)
2580{
2581	int retval;
2582	u16 temp;
2583	u32 rh_a = -1;
2584	u32 rh_b = -1;
2585	retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2586	if (retval)
2587		return retval;
2588	desc->bDescriptorType = 0x29;
2589	desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2590	desc->bHubContrCurrent = 0;
2591	desc->bNbrPorts = u132->num_ports;
2592	temp = 1 + (u132->num_ports / 8);
2593	desc->bDescLength = 7 + 2 * temp;
2594	temp = 0;
2595	if (rh_a & RH_A_NPS)
2596		temp |= 0x0002;
2597	if (rh_a & RH_A_PSM)
2598		temp |= 0x0001;
2599	if (rh_a & RH_A_NOCP)
2600		temp |= 0x0010;
2601	else if (rh_a & RH_A_OCPM)
2602		temp |= 0x0008;
2603	desc->wHubCharacteristics = cpu_to_le16(temp);
2604	retval = u132_read_pcimem(u132, roothub.b, &rh_b);
2605	if (retval)
2606		return retval;
2607	memset(desc->u.hs.DeviceRemovable, 0xff,
2608			sizeof(desc->u.hs.DeviceRemovable));
2609	desc->u.hs.DeviceRemovable[0] = rh_b & RH_B_DR;
2610	if (u132->num_ports > 7) {
2611		desc->u.hs.DeviceRemovable[1] = (rh_b & RH_B_DR) >> 8;
2612		desc->u.hs.DeviceRemovable[2] = 0xff;
2613	} else
2614		desc->u.hs.DeviceRemovable[1] = 0xff;
2615	return 0;
2616}
2617
2618static int u132_roothub_status(struct u132 *u132, __le32 *desc)
2619{
2620	u32 rh_status = -1;
2621	int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
2622	*desc = cpu_to_le32(rh_status);
2623	return ret_status;
2624}
2625
2626static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
2627{
2628	if (wIndex == 0 || wIndex > u132->num_ports) {
2629		return -EINVAL;
2630	} else {
2631		int port = wIndex - 1;
2632		u32 rh_portstatus = -1;
2633		int ret_portstatus = u132_read_pcimem(u132,
2634			roothub.portstatus[port], &rh_portstatus);
2635		*desc = cpu_to_le32(rh_portstatus);
2636		if (*(u16 *) (desc + 2)) {
2637			dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
2638				"ge = %08X\n", port, *desc);
2639		}
2640		return ret_portstatus;
2641	}
2642}
2643
2644
2645/* this timer value might be vendor-specific ... */
2646#define PORT_RESET_HW_MSEC 10
2647#define PORT_RESET_MSEC 10
2648/* wrap-aware logic morphed from <linux/jiffies.h> */
2649#define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
2650static int u132_roothub_portreset(struct u132 *u132, int port_index)
2651{
2652	int retval;
2653	u32 fmnumber;
2654	u16 now;
2655	u16 reset_done;
2656	retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2657	if (retval)
2658		return retval;
2659	now = fmnumber;
2660	reset_done = now + PORT_RESET_MSEC;
2661	do {
2662		u32 portstat;
2663		do {
2664			retval = u132_read_pcimem(u132,
2665				roothub.portstatus[port_index], &portstat);
2666			if (retval)
2667				return retval;
2668			if (RH_PS_PRS & portstat)
2669				continue;
2670			else
2671				break;
2672		} while (tick_before(now, reset_done));
2673		if (RH_PS_PRS & portstat)
2674			return -ENODEV;
2675		if (RH_PS_CCS & portstat) {
2676			if (RH_PS_PRSC & portstat) {
2677				retval = u132_write_pcimem(u132,
2678					roothub.portstatus[port_index],
2679					RH_PS_PRSC);
2680				if (retval)
2681					return retval;
2682			}
2683		} else
2684			break;	/* start the next reset,
2685				sleep till it's probably done */
2686		retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2687			 RH_PS_PRS);
2688		if (retval)
2689			return retval;
2690		msleep(PORT_RESET_HW_MSEC);
2691		retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2692		if (retval)
2693			return retval;
2694		now = fmnumber;
2695	} while (tick_before(now, reset_done));
2696	return 0;
2697}
2698
2699static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
2700	u16 wIndex)
2701{
2702	if (wIndex == 0 || wIndex > u132->num_ports) {
2703		return -EINVAL;
2704	} else {
2705		int retval;
2706		int port_index = wIndex - 1;
2707		struct u132_port *port = &u132->port[port_index];
2708		port->Status &= ~(1 << wValue);
2709		switch (wValue) {
2710		case USB_PORT_FEAT_SUSPEND:
2711			retval = u132_write_pcimem(u132,
2712				roothub.portstatus[port_index], RH_PS_PSS);
2713			if (retval)
2714				return retval;
2715			return 0;
2716		case USB_PORT_FEAT_POWER:
2717			retval = u132_write_pcimem(u132,
2718				roothub.portstatus[port_index], RH_PS_PPS);
2719			if (retval)
2720				return retval;
2721			return 0;
2722		case USB_PORT_FEAT_RESET:
2723			retval = u132_roothub_portreset(u132, port_index);
2724			if (retval)
2725				return retval;
2726			return 0;
2727		default:
2728			return -EPIPE;
2729		}
2730	}
2731}
2732
2733static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
2734	u16 wIndex)
2735{
2736	if (wIndex == 0 || wIndex > u132->num_ports) {
2737		return -EINVAL;
2738	} else {
2739		int port_index = wIndex - 1;
2740		u32 temp;
2741		int retval;
2742		struct u132_port *port = &u132->port[port_index];
2743		port->Status &= ~(1 << wValue);
2744		switch (wValue) {
2745		case USB_PORT_FEAT_ENABLE:
2746			temp = RH_PS_CCS;
2747			break;
2748		case USB_PORT_FEAT_C_ENABLE:
2749			temp = RH_PS_PESC;
2750			break;
2751		case USB_PORT_FEAT_SUSPEND:
2752			temp = RH_PS_POCI;
2753			if ((u132->hc_control & OHCI_CTRL_HCFS)
2754				!= OHCI_USB_OPER) {
2755				dev_err(&u132->platform_dev->dev, "TODO resume_"
2756					"root_hub\n");
2757			}
2758			break;
2759		case USB_PORT_FEAT_C_SUSPEND:
2760			temp = RH_PS_PSSC;
2761			break;
2762		case USB_PORT_FEAT_POWER:
2763			temp = RH_PS_LSDA;
2764			break;
2765		case USB_PORT_FEAT_C_CONNECTION:
2766			temp = RH_PS_CSC;
2767			break;
2768		case USB_PORT_FEAT_C_OVER_CURRENT:
2769			temp = RH_PS_OCIC;
2770			break;
2771		case USB_PORT_FEAT_C_RESET:
2772			temp = RH_PS_PRSC;
2773			break;
2774		default:
2775			return -EPIPE;
2776		}
2777		retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2778			 temp);
2779		if (retval)
2780			return retval;
2781		return 0;
2782	}
2783}
2784
2785
2786/* the virtual root hub timer IRQ checks for hub status*/
2787static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
2788{
2789	struct u132 *u132 = hcd_to_u132(hcd);
2790	if (u132->going > 1) {
2791		dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
2792			"ed %d\n", hcd, u132->going);
2793		return -ENODEV;
2794	} else if (u132->going > 0) {
2795		dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
2796			"ed\n", hcd);
2797		return -ESHUTDOWN;
2798	} else {
2799		int i, changed = 0, length = 1;
2800		if (u132->flags & OHCI_QUIRK_AMD756) {
2801			if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
2802				dev_err(&u132->platform_dev->dev, "bogus NDP, r"
2803					"ereads as NDP=%d\n",
2804					u132->hc_roothub_a & RH_A_NDP);
2805				goto done;
2806			}
2807		}
2808		if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC))
2809			buf[0] = changed = 1;
2810		else
2811			buf[0] = 0;
2812		if (u132->num_ports > 7) {
2813			buf[1] = 0;
2814			length++;
2815		}
2816		for (i = 0; i < u132->num_ports; i++) {
2817			if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
2818				RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
2819				RH_PS_PRSC)) {
2820				changed = 1;
2821				if (i < 7)
2822					buf[0] |= 1 << (i + 1);
2823				else
2824					buf[1] |= 1 << (i - 7);
2825				continue;
2826			}
2827			if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS))
2828				continue;
2829
2830			if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS))
2831				continue;
2832		}
2833done:
2834		return changed ? length : 0;
2835	}
2836}
2837
2838static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2839	u16 wIndex, char *buf, u16 wLength)
2840{
2841	struct u132 *u132 = hcd_to_u132(hcd);
2842	if (u132->going > 1) {
2843		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2844			, u132->going);
2845		return -ENODEV;
2846	} else if (u132->going > 0) {
2847		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2848		return -ESHUTDOWN;
2849	} else {
2850		int retval = 0;
2851		mutex_lock(&u132->sw_lock);
2852		switch (typeReq) {
2853		case ClearHubFeature:
2854			switch (wValue) {
2855			case C_HUB_OVER_CURRENT:
2856			case C_HUB_LOCAL_POWER:
2857				break;
2858			default:
2859				goto stall;
2860			}
2861			break;
2862		case SetHubFeature:
2863			switch (wValue) {
2864			case C_HUB_OVER_CURRENT:
2865			case C_HUB_LOCAL_POWER:
2866				break;
2867			default:
2868				goto stall;
2869			}
2870			break;
2871		case ClearPortFeature:{
2872				retval = u132_roothub_clearportfeature(u132,
2873					wValue, wIndex);
2874				if (retval)
2875					goto error;
2876				break;
2877			}
2878		case GetHubDescriptor:{
2879				retval = u132_roothub_descriptor(u132,
2880					(struct usb_hub_descriptor *)buf);
2881				if (retval)
2882					goto error;
2883				break;
2884			}
2885		case GetHubStatus:{
2886				retval = u132_roothub_status(u132,
2887					(__le32 *) buf);
2888				if (retval)
2889					goto error;
2890				break;
2891			}
2892		case GetPortStatus:{
2893				retval = u132_roothub_portstatus(u132,
2894					(__le32 *) buf, wIndex);
2895				if (retval)
2896					goto error;
2897				break;
2898			}
2899		case SetPortFeature:{
2900				retval = u132_roothub_setportfeature(u132,
2901					wValue, wIndex);
2902				if (retval)
2903					goto error;
2904				break;
2905			}
2906		default:
2907			goto stall;
2908		error:
2909			u132_disable(u132);
2910			u132->going = 1;
2911			break;
2912		stall:
2913			retval = -EPIPE;
2914			break;
2915		}
2916		mutex_unlock(&u132->sw_lock);
2917		return retval;
2918	}
2919}
2920
2921static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
2922{
2923	struct u132 *u132 = hcd_to_u132(hcd);
2924	if (u132->going > 1) {
2925		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2926			, u132->going);
2927		return -ENODEV;
2928	} else if (u132->going > 0) {
2929		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2930		return -ESHUTDOWN;
2931	} else
2932		return 0;
2933}
2934
2935
2936#ifdef CONFIG_PM
2937static int u132_bus_suspend(struct usb_hcd *hcd)
2938{
2939	struct u132 *u132 = hcd_to_u132(hcd);
2940	if (u132->going > 1) {
2941		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2942			, u132->going);
2943		return -ENODEV;
2944	} else if (u132->going > 0) {
2945		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2946		return -ESHUTDOWN;
2947	} else
2948		return 0;
2949}
2950
2951static int u132_bus_resume(struct usb_hcd *hcd)
2952{
2953	struct u132 *u132 = hcd_to_u132(hcd);
2954	if (u132->going > 1) {
2955		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2956			, u132->going);
2957		return -ENODEV;
2958	} else if (u132->going > 0) {
2959		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2960		return -ESHUTDOWN;
2961	} else
2962		return 0;
2963}
2964
2965#else
2966#define u132_bus_suspend NULL
2967#define u132_bus_resume NULL
2968#endif
2969static struct hc_driver u132_hc_driver = {
2970	.description = hcd_name,
2971	.hcd_priv_size = sizeof(struct u132),
2972	.irq = NULL,
2973	.flags = HCD_USB11 | HCD_MEMORY,
2974	.reset = u132_hcd_reset,
2975	.start = u132_hcd_start,
2976	.stop = u132_hcd_stop,
2977	.urb_enqueue = u132_urb_enqueue,
2978	.urb_dequeue = u132_urb_dequeue,
2979	.endpoint_disable = u132_endpoint_disable,
2980	.get_frame_number = u132_get_frame,
2981	.hub_status_data = u132_hub_status_data,
2982	.hub_control = u132_hub_control,
2983	.bus_suspend = u132_bus_suspend,
2984	.bus_resume = u132_bus_resume,
2985	.start_port_reset = u132_start_port_reset,
2986};
2987
2988/*
2989* This function may be called by the USB core whilst the "usb_all_devices_rwsem"
2990* is held for writing, thus this module must not call usb_remove_hcd()
2991* synchronously - but instead should immediately stop activity to the
2992* device and asynchronously call usb_remove_hcd()
2993*/
2994static int __devexit u132_remove(struct platform_device *pdev)
2995{
2996	struct usb_hcd *hcd = platform_get_drvdata(pdev);
2997	if (hcd) {
2998		struct u132 *u132 = hcd_to_u132(hcd);
2999		if (u132->going++ > 1) {
3000			dev_err(&u132->platform_dev->dev, "already being remove"
3001				"d\n");
3002			return -ENODEV;
3003		} else {
3004			int rings = MAX_U132_RINGS;
3005			int endps = MAX_U132_ENDPS;
3006			dev_err(&u132->platform_dev->dev, "removing device u132"
3007				".%d\n", u132->sequence_num);
3008			msleep(100);
3009			mutex_lock(&u132->sw_lock);
3010			u132_monitor_cancel_work(u132);
3011			while (rings-- > 0) {
3012				struct u132_ring *ring = &u132->ring[rings];
3013				u132_ring_cancel_work(u132, ring);
3014			} while (endps-- > 0) {
3015				struct u132_endp *endp = u132->endp[endps];
3016				if (endp)
3017					u132_endp_cancel_work(u132, endp);
3018			}
3019			u132->going += 1;
3020			printk(KERN_INFO "removing device u132.%d\n",
3021				u132->sequence_num);
3022			mutex_unlock(&u132->sw_lock);
3023			usb_remove_hcd(hcd);
3024			u132_u132_put_kref(u132);
3025			return 0;
3026		}
3027	} else
3028		return 0;
3029}
3030
3031static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
3032{
3033	int rings = MAX_U132_RINGS;
3034	int ports = MAX_U132_PORTS;
3035	int addrs = MAX_U132_ADDRS;
3036	int udevs = MAX_U132_UDEVS;
3037	int endps = MAX_U132_ENDPS;
3038	u132->board = pdev->dev.platform_data;
3039	u132->platform_dev = pdev;
3040	u132->power = 0;
3041	u132->reset = 0;
3042	mutex_init(&u132->sw_lock);
3043	mutex_init(&u132->scheduler_lock);
3044	while (rings-- > 0) {
3045		struct u132_ring *ring = &u132->ring[rings];
3046		ring->u132 = u132;
3047		ring->number = rings + 1;
3048		ring->length = 0;
3049		ring->curr_endp = NULL;
3050		INIT_DELAYED_WORK(&ring->scheduler,
3051				  u132_hcd_ring_work_scheduler);
3052	}
3053	mutex_lock(&u132->sw_lock);
3054	INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
3055	while (ports-- > 0) {
3056		struct u132_port *port = &u132->port[ports];
3057		port->u132 = u132;
3058		port->reset = 0;
3059		port->enable = 0;
3060		port->power = 0;
3061		port->Status = 0;
3062	}
3063	while (addrs-- > 0) {
3064		struct u132_addr *addr = &u132->addr[addrs];
3065		addr->address = 0;
3066	}
3067	while (udevs-- > 0) {
3068		struct u132_udev *udev = &u132->udev[udevs];
3069		int i = ARRAY_SIZE(udev->endp_number_in);
3070		int o = ARRAY_SIZE(udev->endp_number_out);
3071		udev->usb_device = NULL;
3072		udev->udev_number = 0;
3073		udev->usb_addr = 0;
3074		udev->portnumber = 0;
3075		while (i-- > 0)
3076			udev->endp_number_in[i] = 0;
3077
3078		while (o-- > 0)
3079			udev->endp_number_out[o] = 0;
3080
3081	}
3082	while (endps-- > 0)
3083		u132->endp[endps] = NULL;
3084
3085	mutex_unlock(&u132->sw_lock);
3086}
3087
3088static int __devinit u132_probe(struct platform_device *pdev)
3089{
3090	struct usb_hcd *hcd;
3091	int retval;
3092	u32 control;
3093	u32 rh_a = -1;
3094	u32 num_ports;
3095
3096	msleep(100);
3097	if (u132_exiting > 0)
3098		return -ENODEV;
3099
3100	retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
3101	if (retval)
3102		return retval;
3103	retval = ftdi_read_pcimem(pdev, control, &control);
3104	if (retval)
3105		return retval;
3106	retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
3107	if (retval)
3108		return retval;
3109	num_ports = rh_a & RH_A_NDP;	/* refuse to confuse usbcore */
3110	if (pdev->dev.dma_mask)
3111		return -EINVAL;
3112
3113	hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, dev_name(&pdev->dev));
3114	if (!hcd) {
3115		printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3116			);
3117		ftdi_elan_gone_away(pdev);
3118		return -ENOMEM;
3119	} else {
3120		struct u132 *u132 = hcd_to_u132(hcd);
3121		retval = 0;
3122		hcd->rsrc_start = 0;
3123		mutex_lock(&u132_module_lock);
3124		list_add_tail(&u132->u132_list, &u132_static_list);
3125		u132->sequence_num = ++u132_instances;
3126		mutex_unlock(&u132_module_lock);
3127		u132_u132_init_kref(u132);
3128		u132_initialise(u132, pdev);
3129		hcd->product_desc = "ELAN U132 Host Controller";
3130		retval = usb_add_hcd(hcd, 0, 0);
3131		if (retval != 0) {
3132			dev_err(&u132->platform_dev->dev, "init error %d\n",
3133				retval);
3134			u132_u132_put_kref(u132);
3135			return retval;
3136		} else {
3137			u132_monitor_queue_work(u132, 100);
3138			return 0;
3139		}
3140	}
3141}
3142
3143
3144#ifdef CONFIG_PM
3145/* for this device there's no useful distinction between the controller
3146* and its root hub, except that the root hub only gets direct PM calls
3147* when CONFIG_USB_SUSPEND is enabled.
3148*/
3149static int u132_suspend(struct platform_device *pdev, pm_message_t state)
3150{
3151	struct usb_hcd *hcd = platform_get_drvdata(pdev);
3152	struct u132 *u132 = hcd_to_u132(hcd);
3153	if (u132->going > 1) {
3154		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3155			, u132->going);
3156		return -ENODEV;
3157	} else if (u132->going > 0) {
3158		dev_err(&u132->platform_dev->dev, "device is being removed\n");
3159		return -ESHUTDOWN;
3160	} else {
3161		int retval = 0, ports;
3162
3163		switch (state.event) {
3164		case PM_EVENT_FREEZE:
3165			retval = u132_bus_suspend(hcd);
3166			break;
3167		case PM_EVENT_SUSPEND:
3168		case PM_EVENT_HIBERNATE:
3169			ports = MAX_U132_PORTS;
3170			while (ports-- > 0) {
3171				port_power(u132, ports, 0);
3172			}
3173			break;
3174		}
3175		return retval;
3176	}
3177}
3178
3179static int u132_resume(struct platform_device *pdev)
3180{
3181	struct usb_hcd *hcd = platform_get_drvdata(pdev);
3182	struct u132 *u132 = hcd_to_u132(hcd);
3183	if (u132->going > 1) {
3184		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3185			, u132->going);
3186		return -ENODEV;
3187	} else if (u132->going > 0) {
3188		dev_err(&u132->platform_dev->dev, "device is being removed\n");
3189		return -ESHUTDOWN;
3190	} else {
3191		int retval = 0;
3192		if (!u132->port[0].power) {
3193			int ports = MAX_U132_PORTS;
3194			while (ports-- > 0) {
3195				port_power(u132, ports, 1);
3196			}
3197			retval = 0;
3198		} else {
3199			retval = u132_bus_resume(hcd);
3200		}
3201		return retval;
3202	}
3203}
3204
3205#else
3206#define u132_suspend NULL
3207#define u132_resume NULL
3208#endif
3209/*
3210* this driver is loaded explicitly by ftdi_u132
3211*
3212* the platform_driver struct is static because it is per type of module
3213*/
3214static struct platform_driver u132_platform_driver = {
3215	.probe = u132_probe,
3216	.remove = __devexit_p(u132_remove),
3217	.suspend = u132_suspend,
3218	.resume = u132_resume,
3219	.driver = {
3220		   .name = (char *)hcd_name,
3221		   .owner = THIS_MODULE,
3222		   },
3223};
3224static int __init u132_hcd_init(void)
3225{
3226	int retval;
3227	INIT_LIST_HEAD(&u132_static_list);
3228	u132_instances = 0;
3229	u132_exiting = 0;
3230	mutex_init(&u132_module_lock);
3231	if (usb_disabled())
3232		return -ENODEV;
3233	printk(KERN_INFO "driver %s\n", hcd_name);
3234	workqueue = create_singlethread_workqueue("u132");
3235	retval = platform_driver_register(&u132_platform_driver);
3236	return retval;
3237}
3238
3239
3240module_init(u132_hcd_init);
3241static void __exit u132_hcd_exit(void)
3242{
3243	struct u132 *u132;
3244	struct u132 *temp;
3245	mutex_lock(&u132_module_lock);
3246	u132_exiting += 1;
3247	mutex_unlock(&u132_module_lock);
3248	list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
3249		platform_device_unregister(u132->platform_dev);
3250	}
3251	platform_driver_unregister(&u132_platform_driver);
3252	printk(KERN_INFO "u132-hcd driver deregistered\n");
3253	wait_event(u132_hcd_wait, u132_instances == 0);
3254	flush_workqueue(workqueue);
3255	destroy_workqueue(workqueue);
3256}
3257
3258
3259module_exit(u132_hcd_exit);
3260MODULE_LICENSE("GPL");
3261MODULE_ALIAS("platform:u132_hcd");
v3.5.6
   1/*
   2* Host Controller Driver for the Elan Digital Systems U132 adapter
   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 host 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/module.h>
  40#include <linux/moduleparam.h>
  41#include <linux/delay.h>
  42#include <linux/ioport.h>
  43#include <linux/pci_ids.h>
  44#include <linux/sched.h>
  45#include <linux/slab.h>
  46#include <linux/errno.h>
  47#include <linux/init.h>
  48#include <linux/timer.h>
  49#include <linux/list.h>
  50#include <linux/interrupt.h>
  51#include <linux/usb.h>
  52#include <linux/usb/hcd.h>
  53#include <linux/workqueue.h>
  54#include <linux/platform_device.h>
  55#include <linux/mutex.h>
  56#include <asm/io.h>
  57#include <asm/irq.h>
 
  58#include <asm/byteorder.h>
  59
  60	/* FIXME ohci.h is ONLY for internal use by the OHCI driver.
  61	 * If you're going to try stuff like this, you need to split
  62	 * out shareable stuff (register declarations?) into its own
  63	 * file, maybe name <linux/usb/ohci.h>
  64	 */
  65
  66#include "ohci.h"
  67#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  68#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
  69	OHCI_INTR_WDH)
  70MODULE_AUTHOR("Tony Olech - Elan Digital Systems Limited");
  71MODULE_DESCRIPTION("U132 USB Host Controller Driver");
  72MODULE_LICENSE("GPL");
  73#define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
  74INT_MODULE_PARM(testing, 0);
  75/* Some boards misreport power switching/overcurrent*/
  76static bool distrust_firmware = 1;
  77module_param(distrust_firmware, bool, 0);
  78MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
  79	"t setup");
  80static DECLARE_WAIT_QUEUE_HEAD(u132_hcd_wait);
  81/*
  82* u132_module_lock exists to protect access to global variables
  83*
  84*/
  85static struct mutex u132_module_lock;
  86static int u132_exiting;
  87static int u132_instances;
  88static struct list_head u132_static_list;
  89/*
  90* end of the global variables protected by u132_module_lock
  91*/
  92static struct workqueue_struct *workqueue;
  93#define MAX_U132_PORTS 7
  94#define MAX_U132_ADDRS 128
  95#define MAX_U132_UDEVS 4
  96#define MAX_U132_ENDPS 100
  97#define MAX_U132_RINGS 4
  98static const char *cc_to_text[16] = {
  99	"No Error ",
 100	"CRC Error ",
 101	"Bit Stuff ",
 102	"Data Togg ",
 103	"Stall ",
 104	"DevNotResp ",
 105	"PIDCheck ",
 106	"UnExpPID ",
 107	"DataOver ",
 108	"DataUnder ",
 109	"(for hw) ",
 110	"(for hw) ",
 111	"BufferOver ",
 112	"BuffUnder ",
 113	"(for HCD) ",
 114	"(for HCD) "
 115};
 116struct u132_port {
 117	struct u132 *u132;
 118	int reset;
 119	int enable;
 120	int power;
 121	int Status;
 122};
 123struct u132_addr {
 124	u8 address;
 125};
 126struct u132_udev {
 127	struct kref kref;
 128	struct usb_device *usb_device;
 129	u8 enumeration;
 130	u8 udev_number;
 131	u8 usb_addr;
 132	u8 portnumber;
 133	u8 endp_number_in[16];
 134	u8 endp_number_out[16];
 135};
 136#define ENDP_QUEUE_SHIFT 3
 137#define ENDP_QUEUE_SIZE (1<<ENDP_QUEUE_SHIFT)
 138#define ENDP_QUEUE_MASK (ENDP_QUEUE_SIZE-1)
 139struct u132_urbq {
 140	struct list_head urb_more;
 141	struct urb *urb;
 142};
 143struct u132_spin {
 144	spinlock_t slock;
 145};
 146struct u132_endp {
 147	struct kref kref;
 148	u8 udev_number;
 149	u8 endp_number;
 150	u8 usb_addr;
 151	u8 usb_endp;
 152	struct u132 *u132;
 153	struct list_head endp_ring;
 154	struct u132_ring *ring;
 155	unsigned toggle_bits:2;
 156	unsigned active:1;
 157	unsigned delayed:1;
 158	unsigned input:1;
 159	unsigned output:1;
 160	unsigned pipetype:2;
 161	unsigned dequeueing:1;
 162	unsigned edset_flush:1;
 163	unsigned spare_bits:14;
 164	unsigned long jiffies;
 165	struct usb_host_endpoint *hep;
 166	struct u132_spin queue_lock;
 167	u16 queue_size;
 168	u16 queue_last;
 169	u16 queue_next;
 170	struct urb *urb_list[ENDP_QUEUE_SIZE];
 171	struct list_head urb_more;
 172	struct delayed_work scheduler;
 173};
 174struct u132_ring {
 175	unsigned in_use:1;
 176	unsigned length:7;
 177	u8 number;
 178	struct u132 *u132;
 179	struct u132_endp *curr_endp;
 180	struct delayed_work scheduler;
 181};
 182struct u132 {
 183	struct kref kref;
 184	struct list_head u132_list;
 185	struct mutex sw_lock;
 186	struct mutex scheduler_lock;
 187	struct u132_platform_data *board;
 188	struct platform_device *platform_dev;
 189	struct u132_ring ring[MAX_U132_RINGS];
 190	int sequence_num;
 191	int going;
 192	int power;
 193	int reset;
 194	int num_ports;
 195	u32 hc_control;
 196	u32 hc_fminterval;
 197	u32 hc_roothub_status;
 198	u32 hc_roothub_a;
 199	u32 hc_roothub_portstatus[MAX_ROOT_PORTS];
 200	int flags;
 201	unsigned long next_statechange;
 202	struct delayed_work monitor;
 203	int num_endpoints;
 204	struct u132_addr addr[MAX_U132_ADDRS];
 205	struct u132_udev udev[MAX_U132_UDEVS];
 206	struct u132_port port[MAX_U132_PORTS];
 207	struct u132_endp *endp[MAX_U132_ENDPS];
 208};
 209
 210/*
 211* these cannot be inlines because we need the structure offset!!
 212* Does anyone have a better way?????
 213*/
 214#define ftdi_read_pcimem(pdev, member, data) usb_ftdi_elan_read_pcimem(pdev, \
 215	offsetof(struct ohci_regs, member), 0, data);
 216#define ftdi_write_pcimem(pdev, member, data) usb_ftdi_elan_write_pcimem(pdev, \
 217	offsetof(struct ohci_regs, member), 0, data);
 218#define u132_read_pcimem(u132, member, data) \
 219	usb_ftdi_elan_read_pcimem(u132->platform_dev, offsetof(struct \
 220	ohci_regs, member), 0, data);
 221#define u132_write_pcimem(u132, member, data) \
 222	usb_ftdi_elan_write_pcimem(u132->platform_dev, offsetof(struct \
 223	ohci_regs, member), 0, data);
 224static inline struct u132 *udev_to_u132(struct u132_udev *udev)
 225{
 226	u8 udev_number = udev->udev_number;
 227	return container_of(udev, struct u132, udev[udev_number]);
 228}
 229
 230static inline struct u132 *hcd_to_u132(struct usb_hcd *hcd)
 231{
 232	return (struct u132 *)(hcd->hcd_priv);
 233}
 234
 235static inline struct usb_hcd *u132_to_hcd(struct u132 *u132)
 236{
 237	return container_of((void *)u132, struct usb_hcd, hcd_priv);
 238}
 239
 240static inline void u132_disable(struct u132 *u132)
 241{
 242	u132_to_hcd(u132)->state = HC_STATE_HALT;
 243}
 244
 245
 246#define kref_to_u132(d) container_of(d, struct u132, kref)
 247#define kref_to_u132_endp(d) container_of(d, struct u132_endp, kref)
 248#define kref_to_u132_udev(d) container_of(d, struct u132_udev, kref)
 249#include "../misc/usb_u132.h"
 250static const char hcd_name[] = "u132_hcd";
 251#define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | \
 252	USB_PORT_STAT_C_SUSPEND | USB_PORT_STAT_C_OVERCURRENT | \
 253	USB_PORT_STAT_C_RESET) << 16)
 254static void u132_hcd_delete(struct kref *kref)
 255{
 256	struct u132 *u132 = kref_to_u132(kref);
 257	struct platform_device *pdev = u132->platform_dev;
 258	struct usb_hcd *hcd = u132_to_hcd(u132);
 259	u132->going += 1;
 260	mutex_lock(&u132_module_lock);
 261	list_del_init(&u132->u132_list);
 262	u132_instances -= 1;
 263	mutex_unlock(&u132_module_lock);
 264	dev_warn(&u132->platform_dev->dev, "FREEING the hcd=%p and thus the u13"
 265		"2=%p going=%d pdev=%p\n", hcd, u132, u132->going, pdev);
 266	usb_put_hcd(hcd);
 267}
 268
 269static inline void u132_u132_put_kref(struct u132 *u132)
 270{
 271	kref_put(&u132->kref, u132_hcd_delete);
 272}
 273
 274static inline void u132_u132_init_kref(struct u132 *u132)
 275{
 276	kref_init(&u132->kref);
 277}
 278
 279static void u132_udev_delete(struct kref *kref)
 280{
 281	struct u132_udev *udev = kref_to_u132_udev(kref);
 282	udev->udev_number = 0;
 283	udev->usb_device = NULL;
 284	udev->usb_addr = 0;
 285	udev->enumeration = 0;
 286}
 287
 288static inline void u132_udev_put_kref(struct u132 *u132, struct u132_udev *udev)
 289{
 290	kref_put(&udev->kref, u132_udev_delete);
 291}
 292
 293static inline void u132_udev_get_kref(struct u132 *u132, struct u132_udev *udev)
 294{
 295	kref_get(&udev->kref);
 296}
 297
 298static inline void u132_udev_init_kref(struct u132 *u132,
 299	struct u132_udev *udev)
 300{
 301	kref_init(&udev->kref);
 302}
 303
 304static inline void u132_ring_put_kref(struct u132 *u132, struct u132_ring *ring)
 305{
 306	kref_put(&u132->kref, u132_hcd_delete);
 307}
 308
 309static void u132_ring_requeue_work(struct u132 *u132, struct u132_ring *ring,
 310	unsigned int delta)
 311{
 312	if (delta > 0) {
 313		if (queue_delayed_work(workqueue, &ring->scheduler, delta))
 314			return;
 315	} else if (queue_delayed_work(workqueue, &ring->scheduler, 0))
 316		return;
 317	kref_put(&u132->kref, u132_hcd_delete);
 318}
 319
 320static void u132_ring_queue_work(struct u132 *u132, struct u132_ring *ring,
 321	unsigned int delta)
 322{
 323	kref_get(&u132->kref);
 324	u132_ring_requeue_work(u132, ring, delta);
 325}
 326
 327static void u132_ring_cancel_work(struct u132 *u132, struct u132_ring *ring)
 328{
 329	if (cancel_delayed_work(&ring->scheduler))
 330		kref_put(&u132->kref, u132_hcd_delete);
 331}
 332
 333static void u132_endp_delete(struct kref *kref)
 334{
 335	struct u132_endp *endp = kref_to_u132_endp(kref);
 336	struct u132 *u132 = endp->u132;
 337	u8 usb_addr = endp->usb_addr;
 338	u8 usb_endp = endp->usb_endp;
 339	u8 address = u132->addr[usb_addr].address;
 340	struct u132_udev *udev = &u132->udev[address];
 341	u8 endp_number = endp->endp_number;
 342	struct usb_host_endpoint *hep = endp->hep;
 343	struct u132_ring *ring = endp->ring;
 344	struct list_head *head = &endp->endp_ring;
 345	ring->length -= 1;
 346	if (endp == ring->curr_endp) {
 347		if (list_empty(head)) {
 348			ring->curr_endp = NULL;
 349			list_del(head);
 350		} else {
 351			struct u132_endp *next_endp = list_entry(head->next,
 352				struct u132_endp, endp_ring);
 353			ring->curr_endp = next_endp;
 354			list_del(head);
 355		}
 356	} else
 357		list_del(head);
 358	if (endp->input) {
 359		udev->endp_number_in[usb_endp] = 0;
 360		u132_udev_put_kref(u132, udev);
 361	}
 362	if (endp->output) {
 363		udev->endp_number_out[usb_endp] = 0;
 364		u132_udev_put_kref(u132, udev);
 365	}
 366	u132->endp[endp_number - 1] = NULL;
 367	hep->hcpriv = NULL;
 368	kfree(endp);
 369	u132_u132_put_kref(u132);
 370}
 371
 372static inline void u132_endp_put_kref(struct u132 *u132, struct u132_endp *endp)
 373{
 374	kref_put(&endp->kref, u132_endp_delete);
 375}
 376
 377static inline void u132_endp_get_kref(struct u132 *u132, struct u132_endp *endp)
 378{
 379	kref_get(&endp->kref);
 380}
 381
 382static inline void u132_endp_init_kref(struct u132 *u132,
 383	struct u132_endp *endp)
 384{
 385	kref_init(&endp->kref);
 386	kref_get(&u132->kref);
 387}
 388
 389static void u132_endp_queue_work(struct u132 *u132, struct u132_endp *endp,
 390	unsigned int delta)
 391{
 392	if (queue_delayed_work(workqueue, &endp->scheduler, delta))
 393		kref_get(&endp->kref);
 394}
 395
 396static void u132_endp_cancel_work(struct u132 *u132, struct u132_endp *endp)
 397{
 398	if (cancel_delayed_work(&endp->scheduler))
 399		kref_put(&endp->kref, u132_endp_delete);
 400}
 401
 402static inline void u132_monitor_put_kref(struct u132 *u132)
 403{
 404	kref_put(&u132->kref, u132_hcd_delete);
 405}
 406
 407static void u132_monitor_queue_work(struct u132 *u132, unsigned int delta)
 408{
 409	if (queue_delayed_work(workqueue, &u132->monitor, delta))
 410		kref_get(&u132->kref);
 411}
 412
 413static void u132_monitor_requeue_work(struct u132 *u132, unsigned int delta)
 414{
 415	if (!queue_delayed_work(workqueue, &u132->monitor, delta))
 416		kref_put(&u132->kref, u132_hcd_delete);
 417}
 418
 419static void u132_monitor_cancel_work(struct u132 *u132)
 420{
 421	if (cancel_delayed_work(&u132->monitor))
 422		kref_put(&u132->kref, u132_hcd_delete);
 423}
 424
 425static int read_roothub_info(struct u132 *u132)
 426{
 427	u32 revision;
 428	int retval;
 429	retval = u132_read_pcimem(u132, revision, &revision);
 430	if (retval) {
 431		dev_err(&u132->platform_dev->dev, "error %d accessing device co"
 432			"ntrol\n", retval);
 433		return retval;
 434	} else if ((revision & 0xFF) == 0x10) {
 435	} else if ((revision & 0xFF) == 0x11) {
 436	} else {
 437		dev_err(&u132->platform_dev->dev, "device revision is not valid"
 438			" %08X\n", revision);
 439		return -ENODEV;
 440	}
 441	retval = u132_read_pcimem(u132, control, &u132->hc_control);
 442	if (retval) {
 443		dev_err(&u132->platform_dev->dev, "error %d accessing device co"
 444			"ntrol\n", retval);
 445		return retval;
 446	}
 447	retval = u132_read_pcimem(u132, roothub.status,
 448		&u132->hc_roothub_status);
 449	if (retval) {
 450		dev_err(&u132->platform_dev->dev, "error %d accessing device re"
 451			"g roothub.status\n", retval);
 452		return retval;
 453	}
 454	retval = u132_read_pcimem(u132, roothub.a, &u132->hc_roothub_a);
 455	if (retval) {
 456		dev_err(&u132->platform_dev->dev, "error %d accessing device re"
 457			"g roothub.a\n", retval);
 458		return retval;
 459	}
 460	{
 461		int I = u132->num_ports;
 462		int i = 0;
 463		while (I-- > 0) {
 464			retval = u132_read_pcimem(u132, roothub.portstatus[i],
 465				&u132->hc_roothub_portstatus[i]);
 466			if (retval) {
 467				dev_err(&u132->platform_dev->dev, "error %d acc"
 468					"essing device roothub.portstatus[%d]\n"
 469					, retval, i);
 470				return retval;
 471			} else
 472				i += 1;
 473		}
 474	}
 475	return 0;
 476}
 477
 478static void u132_hcd_monitor_work(struct work_struct *work)
 479{
 480	struct u132 *u132 = container_of(work, struct u132, monitor.work);
 481	if (u132->going > 1) {
 482		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 483			, u132->going);
 484		u132_monitor_put_kref(u132);
 485		return;
 486	} else if (u132->going > 0) {
 487		dev_err(&u132->platform_dev->dev, "device is being removed\n");
 488		u132_monitor_put_kref(u132);
 489		return;
 490	} else {
 491		int retval;
 492		mutex_lock(&u132->sw_lock);
 493		retval = read_roothub_info(u132);
 494		if (retval) {
 495			struct usb_hcd *hcd = u132_to_hcd(u132);
 496			u132_disable(u132);
 497			u132->going = 1;
 498			mutex_unlock(&u132->sw_lock);
 499			usb_hc_died(hcd);
 500			ftdi_elan_gone_away(u132->platform_dev);
 501			u132_monitor_put_kref(u132);
 502			return;
 503		} else {
 504			u132_monitor_requeue_work(u132, 500);
 505			mutex_unlock(&u132->sw_lock);
 506			return;
 507		}
 508	}
 509}
 510
 511static void u132_hcd_giveback_urb(struct u132 *u132, struct u132_endp *endp,
 512	struct urb *urb, int status)
 513{
 514	struct u132_ring *ring;
 515	unsigned long irqs;
 516	struct usb_hcd *hcd = u132_to_hcd(u132);
 517	urb->error_count = 0;
 518	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
 519	usb_hcd_unlink_urb_from_ep(hcd, urb);
 520	endp->queue_next += 1;
 521	if (ENDP_QUEUE_SIZE > --endp->queue_size) {
 522		endp->active = 0;
 523		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 524	} else {
 525		struct list_head *next = endp->urb_more.next;
 526		struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
 527			urb_more);
 528		list_del(next);
 529		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
 530			urbq->urb;
 531		endp->active = 0;
 532		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 533		kfree(urbq);
 534	}
 535	mutex_lock(&u132->scheduler_lock);
 536	ring = endp->ring;
 537	ring->in_use = 0;
 538	u132_ring_cancel_work(u132, ring);
 539	u132_ring_queue_work(u132, ring, 0);
 540	mutex_unlock(&u132->scheduler_lock);
 541	u132_endp_put_kref(u132, endp);
 542	usb_hcd_giveback_urb(hcd, urb, status);
 543}
 544
 545static void u132_hcd_forget_urb(struct u132 *u132, struct u132_endp *endp,
 546	struct urb *urb, int status)
 547{
 548	u132_endp_put_kref(u132, endp);
 549}
 550
 551static void u132_hcd_abandon_urb(struct u132 *u132, struct u132_endp *endp,
 552	struct urb *urb, int status)
 553{
 554	unsigned long irqs;
 555	struct usb_hcd *hcd = u132_to_hcd(u132);
 556	urb->error_count = 0;
 557	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
 558	usb_hcd_unlink_urb_from_ep(hcd, urb);
 559	endp->queue_next += 1;
 560	if (ENDP_QUEUE_SIZE > --endp->queue_size) {
 561		endp->active = 0;
 562		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 563	} else {
 564		struct list_head *next = endp->urb_more.next;
 565		struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
 566			urb_more);
 567		list_del(next);
 568		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
 569			urbq->urb;
 570		endp->active = 0;
 571		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
 572		kfree(urbq);
 573	}
 574	usb_hcd_giveback_urb(hcd, urb, status);
 575}
 576
 577static inline int edset_input(struct u132 *u132, struct u132_ring *ring,
 578	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 579	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 580	int toggle_bits, int error_count, int condition_code, int repeat_number,
 581	 int halted, int skipped, int actual, int non_null))
 582{
 583	return usb_ftdi_elan_edset_input(u132->platform_dev, ring->number, endp,
 584		 urb, address, endp->usb_endp, toggle_bits, callback);
 585}
 586
 587static inline int edset_setup(struct u132 *u132, struct u132_ring *ring,
 588	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 589	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 590	int toggle_bits, int error_count, int condition_code, int repeat_number,
 591	 int halted, int skipped, int actual, int non_null))
 592{
 593	return usb_ftdi_elan_edset_setup(u132->platform_dev, ring->number, endp,
 594		 urb, address, endp->usb_endp, toggle_bits, callback);
 595}
 596
 597static inline int edset_single(struct u132 *u132, struct u132_ring *ring,
 598	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 599	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 600	int toggle_bits, int error_count, int condition_code, int repeat_number,
 601	 int halted, int skipped, int actual, int non_null))
 602{
 603	return usb_ftdi_elan_edset_single(u132->platform_dev, ring->number,
 604		endp, urb, address, endp->usb_endp, toggle_bits, callback);
 605}
 606
 607static inline int edset_output(struct u132 *u132, struct u132_ring *ring,
 608	struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
 609	void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
 610	int toggle_bits, int error_count, int condition_code, int repeat_number,
 611	 int halted, int skipped, int actual, int non_null))
 612{
 613	return usb_ftdi_elan_edset_output(u132->platform_dev, ring->number,
 614		endp, urb, address, endp->usb_endp, toggle_bits, callback);
 615}
 616
 617
 618/*
 619* must not LOCK sw_lock
 620*
 621*/
 622static void u132_hcd_interrupt_recv(void *data, struct urb *urb, u8 *buf,
 623	int len, int toggle_bits, int error_count, int condition_code,
 624	int repeat_number, int halted, int skipped, int actual, int non_null)
 625{
 626	struct u132_endp *endp = data;
 627	struct u132 *u132 = endp->u132;
 628	u8 address = u132->addr[endp->usb_addr].address;
 629	struct u132_udev *udev = &u132->udev[address];
 630	mutex_lock(&u132->scheduler_lock);
 631	if (u132->going > 1) {
 632		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 633			, u132->going);
 634		mutex_unlock(&u132->scheduler_lock);
 635		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 636		return;
 637	} else if (endp->dequeueing) {
 638		endp->dequeueing = 0;
 639		mutex_unlock(&u132->scheduler_lock);
 640		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 641		return;
 642	} else if (u132->going > 0) {
 643		dev_err(&u132->platform_dev->dev, "device is being removed "
 644				"urb=%p\n", urb);
 645		mutex_unlock(&u132->scheduler_lock);
 646		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 647		return;
 648	} else if (!urb->unlinked) {
 649		struct u132_ring *ring = endp->ring;
 650		u8 *u = urb->transfer_buffer + urb->actual_length;
 651		u8 *b = buf;
 652		int L = len;
 653
 654		while (L-- > 0)
 655			*u++ = *b++;
 656
 657		urb->actual_length += len;
 658		if ((condition_code == TD_CC_NOERROR) &&
 659			(urb->transfer_buffer_length > urb->actual_length)) {
 660			endp->toggle_bits = toggle_bits;
 661			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 662				1 & toggle_bits);
 663			if (urb->actual_length > 0) {
 664				int retval;
 665				mutex_unlock(&u132->scheduler_lock);
 666				retval = edset_single(u132, ring, endp, urb,
 667					address, endp->toggle_bits,
 668					u132_hcd_interrupt_recv);
 669				if (retval != 0)
 670					u132_hcd_giveback_urb(u132, endp, urb,
 671						retval);
 672			} else {
 673				ring->in_use = 0;
 674				endp->active = 0;
 675				endp->jiffies = jiffies +
 676					msecs_to_jiffies(urb->interval);
 677				u132_ring_cancel_work(u132, ring);
 678				u132_ring_queue_work(u132, ring, 0);
 679				mutex_unlock(&u132->scheduler_lock);
 680				u132_endp_put_kref(u132, endp);
 681			}
 682			return;
 683		} else if ((condition_code == TD_DATAUNDERRUN) &&
 684			((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
 685			endp->toggle_bits = toggle_bits;
 686			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 687				1 & toggle_bits);
 688			mutex_unlock(&u132->scheduler_lock);
 689			u132_hcd_giveback_urb(u132, endp, urb, 0);
 690			return;
 691		} else {
 692			if (condition_code == TD_CC_NOERROR) {
 693				endp->toggle_bits = toggle_bits;
 694				usb_settoggle(udev->usb_device, endp->usb_endp,
 695					0, 1 & toggle_bits);
 696			} else if (condition_code == TD_CC_STALL) {
 697				endp->toggle_bits = 0x2;
 698				usb_settoggle(udev->usb_device, endp->usb_endp,
 699					0, 0);
 700			} else {
 701				endp->toggle_bits = 0x2;
 702				usb_settoggle(udev->usb_device, endp->usb_endp,
 703					0, 0);
 704				dev_err(&u132->platform_dev->dev, "urb=%p givin"
 705					"g back INTERRUPT %s\n", urb,
 706					cc_to_text[condition_code]);
 707			}
 708			mutex_unlock(&u132->scheduler_lock);
 709			u132_hcd_giveback_urb(u132, endp, urb,
 710				cc_to_error[condition_code]);
 711			return;
 712		}
 713	} else {
 714		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 715				"unlinked=%d\n", urb, urb->unlinked);
 716		mutex_unlock(&u132->scheduler_lock);
 717		u132_hcd_giveback_urb(u132, endp, urb, 0);
 718		return;
 719	}
 720}
 721
 722static void u132_hcd_bulk_output_sent(void *data, struct urb *urb, u8 *buf,
 723	int len, int toggle_bits, int error_count, int condition_code,
 724	int repeat_number, int halted, int skipped, int actual, int non_null)
 725{
 726	struct u132_endp *endp = data;
 727	struct u132 *u132 = endp->u132;
 728	u8 address = u132->addr[endp->usb_addr].address;
 729	mutex_lock(&u132->scheduler_lock);
 730	if (u132->going > 1) {
 731		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 732			, u132->going);
 733		mutex_unlock(&u132->scheduler_lock);
 734		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 735		return;
 736	} else if (endp->dequeueing) {
 737		endp->dequeueing = 0;
 738		mutex_unlock(&u132->scheduler_lock);
 739		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 740		return;
 741	} else if (u132->going > 0) {
 742		dev_err(&u132->platform_dev->dev, "device is being removed "
 743				"urb=%p\n", urb);
 744		mutex_unlock(&u132->scheduler_lock);
 745		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 746		return;
 747	} else if (!urb->unlinked) {
 748		struct u132_ring *ring = endp->ring;
 749		urb->actual_length += len;
 750		endp->toggle_bits = toggle_bits;
 751		if (urb->transfer_buffer_length > urb->actual_length) {
 752			int retval;
 753			mutex_unlock(&u132->scheduler_lock);
 754			retval = edset_output(u132, ring, endp, urb, address,
 755				endp->toggle_bits, u132_hcd_bulk_output_sent);
 756			if (retval != 0)
 757				u132_hcd_giveback_urb(u132, endp, urb, retval);
 758			return;
 759		} else {
 760			mutex_unlock(&u132->scheduler_lock);
 761			u132_hcd_giveback_urb(u132, endp, urb, 0);
 762			return;
 763		}
 764	} else {
 765		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 766				"unlinked=%d\n", urb, urb->unlinked);
 767		mutex_unlock(&u132->scheduler_lock);
 768		u132_hcd_giveback_urb(u132, endp, urb, 0);
 769		return;
 770	}
 771}
 772
 773static void u132_hcd_bulk_input_recv(void *data, struct urb *urb, u8 *buf,
 774	int len, int toggle_bits, int error_count, int condition_code,
 775	int repeat_number, int halted, int skipped, int actual, int non_null)
 776{
 777	struct u132_endp *endp = data;
 778	struct u132 *u132 = endp->u132;
 779	u8 address = u132->addr[endp->usb_addr].address;
 780	struct u132_udev *udev = &u132->udev[address];
 781	mutex_lock(&u132->scheduler_lock);
 782	if (u132->going > 1) {
 783		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 784			, u132->going);
 785		mutex_unlock(&u132->scheduler_lock);
 786		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 787		return;
 788	} else if (endp->dequeueing) {
 789		endp->dequeueing = 0;
 790		mutex_unlock(&u132->scheduler_lock);
 791		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 792		return;
 793	} else if (u132->going > 0) {
 794		dev_err(&u132->platform_dev->dev, "device is being removed "
 795				"urb=%p\n", urb);
 796		mutex_unlock(&u132->scheduler_lock);
 797		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 798		return;
 799	} else if (!urb->unlinked) {
 800		struct u132_ring *ring = endp->ring;
 801		u8 *u = urb->transfer_buffer + urb->actual_length;
 802		u8 *b = buf;
 803		int L = len;
 804
 805		while (L-- > 0)
 806			*u++ = *b++;
 807
 808		urb->actual_length += len;
 809		if ((condition_code == TD_CC_NOERROR) &&
 810			(urb->transfer_buffer_length > urb->actual_length)) {
 811			int retval;
 812			endp->toggle_bits = toggle_bits;
 813			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 814				1 & toggle_bits);
 815			mutex_unlock(&u132->scheduler_lock);
 816			retval = usb_ftdi_elan_edset_input(u132->platform_dev,
 817				ring->number, endp, urb, address,
 818				endp->usb_endp, endp->toggle_bits,
 819				u132_hcd_bulk_input_recv);
 820			if (retval != 0)
 821				u132_hcd_giveback_urb(u132, endp, urb, retval);
 822			return;
 823		} else if (condition_code == TD_CC_NOERROR) {
 824			endp->toggle_bits = toggle_bits;
 825			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 826				1 & toggle_bits);
 827			mutex_unlock(&u132->scheduler_lock);
 828			u132_hcd_giveback_urb(u132, endp, urb,
 829				cc_to_error[condition_code]);
 830			return;
 831		} else if ((condition_code == TD_DATAUNDERRUN) &&
 832			((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
 833			endp->toggle_bits = toggle_bits;
 834			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 835				1 & toggle_bits);
 836			mutex_unlock(&u132->scheduler_lock);
 837			u132_hcd_giveback_urb(u132, endp, urb, 0);
 838			return;
 839		} else if (condition_code == TD_DATAUNDERRUN) {
 840			endp->toggle_bits = toggle_bits;
 841			usb_settoggle(udev->usb_device, endp->usb_endp, 0,
 842				1 & toggle_bits);
 843			dev_warn(&u132->platform_dev->dev, "urb=%p(SHORT NOT OK"
 844				") giving back BULK IN %s\n", urb,
 845				cc_to_text[condition_code]);
 846			mutex_unlock(&u132->scheduler_lock);
 847			u132_hcd_giveback_urb(u132, endp, urb, 0);
 848			return;
 849		} else if (condition_code == TD_CC_STALL) {
 850			endp->toggle_bits = 0x2;
 851			usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
 852			mutex_unlock(&u132->scheduler_lock);
 853			u132_hcd_giveback_urb(u132, endp, urb,
 854				cc_to_error[condition_code]);
 855			return;
 856		} else {
 857			endp->toggle_bits = 0x2;
 858			usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
 859			dev_err(&u132->platform_dev->dev, "urb=%p giving back B"
 860				"ULK IN code=%d %s\n", urb, condition_code,
 861				cc_to_text[condition_code]);
 862			mutex_unlock(&u132->scheduler_lock);
 863			u132_hcd_giveback_urb(u132, endp, urb,
 864				cc_to_error[condition_code]);
 865			return;
 866		}
 867	} else {
 868		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 869				"unlinked=%d\n", urb, urb->unlinked);
 870		mutex_unlock(&u132->scheduler_lock);
 871		u132_hcd_giveback_urb(u132, endp, urb, 0);
 872		return;
 873	}
 874}
 875
 876static void u132_hcd_configure_empty_sent(void *data, struct urb *urb, u8 *buf,
 877	int len, int toggle_bits, int error_count, int condition_code,
 878	int repeat_number, int halted, int skipped, int actual, int non_null)
 879{
 880	struct u132_endp *endp = data;
 881	struct u132 *u132 = endp->u132;
 882	mutex_lock(&u132->scheduler_lock);
 883	if (u132->going > 1) {
 884		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 885			, u132->going);
 886		mutex_unlock(&u132->scheduler_lock);
 887		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 888		return;
 889	} else if (endp->dequeueing) {
 890		endp->dequeueing = 0;
 891		mutex_unlock(&u132->scheduler_lock);
 892		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 893		return;
 894	} else if (u132->going > 0) {
 895		dev_err(&u132->platform_dev->dev, "device is being removed "
 896				"urb=%p\n", urb);
 897		mutex_unlock(&u132->scheduler_lock);
 898		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 899		return;
 900	} else if (!urb->unlinked) {
 901		mutex_unlock(&u132->scheduler_lock);
 902		u132_hcd_giveback_urb(u132, endp, urb, 0);
 903		return;
 904	} else {
 905		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 906				"unlinked=%d\n", urb, urb->unlinked);
 907		mutex_unlock(&u132->scheduler_lock);
 908		u132_hcd_giveback_urb(u132, endp, urb, 0);
 909		return;
 910	}
 911}
 912
 913static void u132_hcd_configure_input_recv(void *data, struct urb *urb, u8 *buf,
 914	int len, int toggle_bits, int error_count, int condition_code,
 915	int repeat_number, int halted, int skipped, int actual, int non_null)
 916{
 917	struct u132_endp *endp = data;
 918	struct u132 *u132 = endp->u132;
 919	u8 address = u132->addr[endp->usb_addr].address;
 920	mutex_lock(&u132->scheduler_lock);
 921	if (u132->going > 1) {
 922		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 923			, u132->going);
 924		mutex_unlock(&u132->scheduler_lock);
 925		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 926		return;
 927	} else if (endp->dequeueing) {
 928		endp->dequeueing = 0;
 929		mutex_unlock(&u132->scheduler_lock);
 930		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
 931		return;
 932	} else if (u132->going > 0) {
 933		dev_err(&u132->platform_dev->dev, "device is being removed "
 934				"urb=%p\n", urb);
 935		mutex_unlock(&u132->scheduler_lock);
 936		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
 937		return;
 938	} else if (!urb->unlinked) {
 939		struct u132_ring *ring = endp->ring;
 940		u8 *u = urb->transfer_buffer;
 941		u8 *b = buf;
 942		int L = len;
 943
 944		while (L-- > 0)
 945			*u++ = *b++;
 946
 947		urb->actual_length = len;
 948		if ((condition_code == TD_CC_NOERROR) || ((condition_code ==
 949			TD_DATAUNDERRUN) && ((urb->transfer_flags &
 950			URB_SHORT_NOT_OK) == 0))) {
 951			int retval;
 952			mutex_unlock(&u132->scheduler_lock);
 953			retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
 954				ring->number, endp, urb, address,
 955				endp->usb_endp, 0x3,
 956				u132_hcd_configure_empty_sent);
 957			if (retval != 0)
 958				u132_hcd_giveback_urb(u132, endp, urb, retval);
 959			return;
 960		} else if (condition_code == TD_CC_STALL) {
 961			mutex_unlock(&u132->scheduler_lock);
 962			dev_warn(&u132->platform_dev->dev, "giving back SETUP I"
 963				"NPUT STALL urb %p\n", urb);
 964			u132_hcd_giveback_urb(u132, endp, urb,
 965				cc_to_error[condition_code]);
 966			return;
 967		} else {
 968			mutex_unlock(&u132->scheduler_lock);
 969			dev_err(&u132->platform_dev->dev, "giving back SETUP IN"
 970				"PUT %s urb %p\n", cc_to_text[condition_code],
 971				urb);
 972			u132_hcd_giveback_urb(u132, endp, urb,
 973				cc_to_error[condition_code]);
 974			return;
 975		}
 976	} else {
 977		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
 978				"unlinked=%d\n", urb, urb->unlinked);
 979		mutex_unlock(&u132->scheduler_lock);
 980		u132_hcd_giveback_urb(u132, endp, urb, 0);
 981		return;
 982	}
 983}
 984
 985static void u132_hcd_configure_empty_recv(void *data, struct urb *urb, u8 *buf,
 986	int len, int toggle_bits, int error_count, int condition_code,
 987	int repeat_number, int halted, int skipped, int actual, int non_null)
 988{
 989	struct u132_endp *endp = data;
 990	struct u132 *u132 = endp->u132;
 991	mutex_lock(&u132->scheduler_lock);
 992	if (u132->going > 1) {
 993		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
 994			, u132->going);
 995		mutex_unlock(&u132->scheduler_lock);
 996		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
 997		return;
 998	} else if (endp->dequeueing) {
 999		endp->dequeueing = 0;
1000		mutex_unlock(&u132->scheduler_lock);
1001		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1002		return;
1003	} else if (u132->going > 0) {
1004		dev_err(&u132->platform_dev->dev, "device is being removed "
1005				"urb=%p\n", urb);
1006		mutex_unlock(&u132->scheduler_lock);
1007		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1008		return;
1009	} else if (!urb->unlinked) {
1010		mutex_unlock(&u132->scheduler_lock);
1011		u132_hcd_giveback_urb(u132, endp, urb, 0);
1012		return;
1013	} else {
1014		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1015				"unlinked=%d\n", urb, urb->unlinked);
1016		mutex_unlock(&u132->scheduler_lock);
1017		u132_hcd_giveback_urb(u132, endp, urb, 0);
1018		return;
1019	}
1020}
1021
1022static void u132_hcd_configure_setup_sent(void *data, struct urb *urb, u8 *buf,
1023	int len, int toggle_bits, int error_count, int condition_code,
1024	int repeat_number, int halted, int skipped, int actual, int non_null)
1025{
1026	struct u132_endp *endp = data;
1027	struct u132 *u132 = endp->u132;
1028	u8 address = u132->addr[endp->usb_addr].address;
1029	mutex_lock(&u132->scheduler_lock);
1030	if (u132->going > 1) {
1031		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1032			, u132->going);
1033		mutex_unlock(&u132->scheduler_lock);
1034		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1035		return;
1036	} else if (endp->dequeueing) {
1037		endp->dequeueing = 0;
1038		mutex_unlock(&u132->scheduler_lock);
1039		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1040		return;
1041	} else if (u132->going > 0) {
1042		dev_err(&u132->platform_dev->dev, "device is being removed "
1043				"urb=%p\n", urb);
1044		mutex_unlock(&u132->scheduler_lock);
1045		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1046		return;
1047	} else if (!urb->unlinked) {
1048		if (usb_pipein(urb->pipe)) {
1049			int retval;
1050			struct u132_ring *ring = endp->ring;
1051			mutex_unlock(&u132->scheduler_lock);
1052			retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1053				ring->number, endp, urb, address,
1054				endp->usb_endp, 0,
1055				u132_hcd_configure_input_recv);
1056			if (retval != 0)
1057				u132_hcd_giveback_urb(u132, endp, urb, retval);
1058			return;
1059		} else {
1060			int retval;
1061			struct u132_ring *ring = endp->ring;
1062			mutex_unlock(&u132->scheduler_lock);
1063			retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1064				ring->number, endp, urb, address,
1065				endp->usb_endp, 0,
1066				u132_hcd_configure_empty_recv);
1067			if (retval != 0)
1068				u132_hcd_giveback_urb(u132, endp, urb, retval);
1069			return;
1070		}
1071	} else {
1072		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1073				"unlinked=%d\n", urb, urb->unlinked);
1074		mutex_unlock(&u132->scheduler_lock);
1075		u132_hcd_giveback_urb(u132, endp, urb, 0);
1076		return;
1077	}
1078}
1079
1080static void u132_hcd_enumeration_empty_recv(void *data, struct urb *urb,
1081	u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1082	int repeat_number, int halted, int skipped, int actual, int non_null)
1083{
1084	struct u132_endp *endp = data;
1085	struct u132 *u132 = endp->u132;
1086	u8 address = u132->addr[endp->usb_addr].address;
1087	struct u132_udev *udev = &u132->udev[address];
1088	mutex_lock(&u132->scheduler_lock);
1089	if (u132->going > 1) {
1090		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1091			, u132->going);
1092		mutex_unlock(&u132->scheduler_lock);
1093		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1094		return;
1095	} else if (endp->dequeueing) {
1096		endp->dequeueing = 0;
1097		mutex_unlock(&u132->scheduler_lock);
1098		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1099		return;
1100	} else if (u132->going > 0) {
1101		dev_err(&u132->platform_dev->dev, "device is being removed "
1102				"urb=%p\n", urb);
1103		mutex_unlock(&u132->scheduler_lock);
1104		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1105		return;
1106	} else if (!urb->unlinked) {
1107		u132->addr[0].address = 0;
1108		endp->usb_addr = udev->usb_addr;
1109		mutex_unlock(&u132->scheduler_lock);
1110		u132_hcd_giveback_urb(u132, endp, urb, 0);
1111		return;
1112	} else {
1113		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1114				"unlinked=%d\n", urb, urb->unlinked);
1115		mutex_unlock(&u132->scheduler_lock);
1116		u132_hcd_giveback_urb(u132, endp, urb, 0);
1117		return;
1118	}
1119}
1120
1121static void u132_hcd_enumeration_address_sent(void *data, struct urb *urb,
1122	u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1123	int repeat_number, int halted, int skipped, int actual, int non_null)
1124{
1125	struct u132_endp *endp = data;
1126	struct u132 *u132 = endp->u132;
1127	mutex_lock(&u132->scheduler_lock);
1128	if (u132->going > 1) {
1129		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1130			, u132->going);
1131		mutex_unlock(&u132->scheduler_lock);
1132		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1133		return;
1134	} else if (endp->dequeueing) {
1135		endp->dequeueing = 0;
1136		mutex_unlock(&u132->scheduler_lock);
1137		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1138		return;
1139	} else if (u132->going > 0) {
1140		dev_err(&u132->platform_dev->dev, "device is being removed "
1141				"urb=%p\n", urb);
1142		mutex_unlock(&u132->scheduler_lock);
1143		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1144		return;
1145	} else if (!urb->unlinked) {
1146		int retval;
1147		struct u132_ring *ring = endp->ring;
1148		mutex_unlock(&u132->scheduler_lock);
1149		retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1150			ring->number, endp, urb, 0, endp->usb_endp, 0,
1151			u132_hcd_enumeration_empty_recv);
1152		if (retval != 0)
1153			u132_hcd_giveback_urb(u132, endp, urb, retval);
1154		return;
1155	} else {
1156		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1157				"unlinked=%d\n", urb, urb->unlinked);
1158		mutex_unlock(&u132->scheduler_lock);
1159		u132_hcd_giveback_urb(u132, endp, urb, 0);
1160		return;
1161	}
1162}
1163
1164static void u132_hcd_initial_empty_sent(void *data, struct urb *urb, u8 *buf,
1165	int len, int toggle_bits, int error_count, int condition_code,
1166	int repeat_number, int halted, int skipped, int actual, int non_null)
1167{
1168	struct u132_endp *endp = data;
1169	struct u132 *u132 = endp->u132;
1170	mutex_lock(&u132->scheduler_lock);
1171	if (u132->going > 1) {
1172		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1173			, u132->going);
1174		mutex_unlock(&u132->scheduler_lock);
1175		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1176		return;
1177	} else if (endp->dequeueing) {
1178		endp->dequeueing = 0;
1179		mutex_unlock(&u132->scheduler_lock);
1180		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1181		return;
1182	} else if (u132->going > 0) {
1183		dev_err(&u132->platform_dev->dev, "device is being removed "
1184				"urb=%p\n", urb);
1185		mutex_unlock(&u132->scheduler_lock);
1186		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1187		return;
1188	} else if (!urb->unlinked) {
1189		mutex_unlock(&u132->scheduler_lock);
1190		u132_hcd_giveback_urb(u132, endp, urb, 0);
1191		return;
1192	} else {
1193		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1194				"unlinked=%d\n", urb, urb->unlinked);
1195		mutex_unlock(&u132->scheduler_lock);
1196		u132_hcd_giveback_urb(u132, endp, urb, 0);
1197		return;
1198	}
1199}
1200
1201static void u132_hcd_initial_input_recv(void *data, struct urb *urb, u8 *buf,
1202	int len, int toggle_bits, int error_count, int condition_code,
1203	int repeat_number, int halted, int skipped, int actual, int non_null)
1204{
1205	struct u132_endp *endp = data;
1206	struct u132 *u132 = endp->u132;
1207	u8 address = u132->addr[endp->usb_addr].address;
1208	mutex_lock(&u132->scheduler_lock);
1209	if (u132->going > 1) {
1210		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1211			, u132->going);
1212		mutex_unlock(&u132->scheduler_lock);
1213		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1214		return;
1215	} else if (endp->dequeueing) {
1216		endp->dequeueing = 0;
1217		mutex_unlock(&u132->scheduler_lock);
1218		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1219		return;
1220	} else if (u132->going > 0) {
1221		dev_err(&u132->platform_dev->dev, "device is being removed "
1222				"urb=%p\n", urb);
1223		mutex_unlock(&u132->scheduler_lock);
1224		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1225		return;
1226	} else if (!urb->unlinked) {
1227		int retval;
1228		struct u132_ring *ring = endp->ring;
1229		u8 *u = urb->transfer_buffer;
1230		u8 *b = buf;
1231		int L = len;
1232
1233		while (L-- > 0)
1234			*u++ = *b++;
1235
1236		urb->actual_length = len;
1237		mutex_unlock(&u132->scheduler_lock);
1238		retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
1239			ring->number, endp, urb, address, endp->usb_endp, 0x3,
1240			u132_hcd_initial_empty_sent);
1241		if (retval != 0)
1242			u132_hcd_giveback_urb(u132, endp, urb, retval);
1243		return;
1244	} else {
1245		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1246				"unlinked=%d\n", urb, urb->unlinked);
1247		mutex_unlock(&u132->scheduler_lock);
1248		u132_hcd_giveback_urb(u132, endp, urb, 0);
1249		return;
1250	}
1251}
1252
1253static void u132_hcd_initial_setup_sent(void *data, struct urb *urb, u8 *buf,
1254	int len, int toggle_bits, int error_count, int condition_code,
1255	int repeat_number, int halted, int skipped, int actual, int non_null)
1256{
1257	struct u132_endp *endp = data;
1258	struct u132 *u132 = endp->u132;
1259	u8 address = u132->addr[endp->usb_addr].address;
1260	mutex_lock(&u132->scheduler_lock);
1261	if (u132->going > 1) {
1262		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1263			, u132->going);
1264		mutex_unlock(&u132->scheduler_lock);
1265		u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1266		return;
1267	} else if (endp->dequeueing) {
1268		endp->dequeueing = 0;
1269		mutex_unlock(&u132->scheduler_lock);
1270		u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1271		return;
1272	} else if (u132->going > 0) {
1273		dev_err(&u132->platform_dev->dev, "device is being removed "
1274				"urb=%p\n", urb);
1275		mutex_unlock(&u132->scheduler_lock);
1276		u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1277		return;
1278	} else if (!urb->unlinked) {
1279		int retval;
1280		struct u132_ring *ring = endp->ring;
1281		mutex_unlock(&u132->scheduler_lock);
1282		retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1283			ring->number, endp, urb, address, endp->usb_endp, 0,
1284			u132_hcd_initial_input_recv);
1285		if (retval != 0)
1286			u132_hcd_giveback_urb(u132, endp, urb, retval);
1287		return;
1288	} else {
1289		dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1290				"unlinked=%d\n", urb, urb->unlinked);
1291		mutex_unlock(&u132->scheduler_lock);
1292		u132_hcd_giveback_urb(u132, endp, urb, 0);
1293		return;
1294	}
1295}
1296
1297/*
1298* this work function is only executed from the work queue
1299*
1300*/
1301static void u132_hcd_ring_work_scheduler(struct work_struct *work)
1302{
1303	struct u132_ring *ring =
1304		container_of(work, struct u132_ring, scheduler.work);
1305	struct u132 *u132 = ring->u132;
1306	mutex_lock(&u132->scheduler_lock);
1307	if (ring->in_use) {
1308		mutex_unlock(&u132->scheduler_lock);
1309		u132_ring_put_kref(u132, ring);
1310		return;
1311	} else if (ring->curr_endp) {
1312		struct u132_endp *last_endp = ring->curr_endp;
1313		struct list_head *scan;
1314		struct list_head *head = &last_endp->endp_ring;
1315		unsigned long wakeup = 0;
1316		list_for_each(scan, head) {
1317			struct u132_endp *endp = list_entry(scan,
1318				struct u132_endp, endp_ring);
1319			if (endp->queue_next == endp->queue_last) {
1320			} else if ((endp->delayed == 0)
1321				|| time_after_eq(jiffies, endp->jiffies)) {
1322				ring->curr_endp = endp;
1323				u132_endp_cancel_work(u132, last_endp);
1324				u132_endp_queue_work(u132, last_endp, 0);
1325				mutex_unlock(&u132->scheduler_lock);
1326				u132_ring_put_kref(u132, ring);
1327				return;
1328			} else {
1329				unsigned long delta = endp->jiffies - jiffies;
1330				if (delta > wakeup)
1331					wakeup = delta;
1332			}
1333		}
1334		if (last_endp->queue_next == last_endp->queue_last) {
1335		} else if ((last_endp->delayed == 0) || time_after_eq(jiffies,
1336			last_endp->jiffies)) {
1337			u132_endp_cancel_work(u132, last_endp);
1338			u132_endp_queue_work(u132, last_endp, 0);
1339			mutex_unlock(&u132->scheduler_lock);
1340			u132_ring_put_kref(u132, ring);
1341			return;
1342		} else {
1343			unsigned long delta = last_endp->jiffies - jiffies;
1344			if (delta > wakeup)
1345				wakeup = delta;
1346		}
1347		if (wakeup > 0) {
1348			u132_ring_requeue_work(u132, ring, wakeup);
1349			mutex_unlock(&u132->scheduler_lock);
1350			return;
1351		} else {
1352			mutex_unlock(&u132->scheduler_lock);
1353			u132_ring_put_kref(u132, ring);
1354			return;
1355		}
1356	} else {
1357		mutex_unlock(&u132->scheduler_lock);
1358		u132_ring_put_kref(u132, ring);
1359		return;
1360	}
1361}
1362
1363static void u132_hcd_endp_work_scheduler(struct work_struct *work)
1364{
1365	struct u132_ring *ring;
1366	struct u132_endp *endp =
1367		container_of(work, struct u132_endp, scheduler.work);
1368	struct u132 *u132 = endp->u132;
1369	mutex_lock(&u132->scheduler_lock);
1370	ring = endp->ring;
1371	if (endp->edset_flush) {
1372		endp->edset_flush = 0;
1373		if (endp->dequeueing)
1374			usb_ftdi_elan_edset_flush(u132->platform_dev,
1375				ring->number, endp);
1376		mutex_unlock(&u132->scheduler_lock);
1377		u132_endp_put_kref(u132, endp);
1378		return;
1379	} else if (endp->active) {
1380		mutex_unlock(&u132->scheduler_lock);
1381		u132_endp_put_kref(u132, endp);
1382		return;
1383	} else if (ring->in_use) {
1384		mutex_unlock(&u132->scheduler_lock);
1385		u132_endp_put_kref(u132, endp);
1386		return;
1387	} else if (endp->queue_next == endp->queue_last) {
1388		mutex_unlock(&u132->scheduler_lock);
1389		u132_endp_put_kref(u132, endp);
1390		return;
1391	} else if (endp->pipetype == PIPE_INTERRUPT) {
1392		u8 address = u132->addr[endp->usb_addr].address;
1393		if (ring->in_use) {
1394			mutex_unlock(&u132->scheduler_lock);
1395			u132_endp_put_kref(u132, endp);
1396			return;
1397		} else {
1398			int retval;
1399			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1400				endp->queue_next];
1401			endp->active = 1;
1402			ring->curr_endp = endp;
1403			ring->in_use = 1;
1404			mutex_unlock(&u132->scheduler_lock);
1405			retval = edset_single(u132, ring, endp, urb, address,
1406				endp->toggle_bits, u132_hcd_interrupt_recv);
1407			if (retval != 0)
1408				u132_hcd_giveback_urb(u132, endp, urb, retval);
1409			return;
1410		}
1411	} else if (endp->pipetype == PIPE_CONTROL) {
1412		u8 address = u132->addr[endp->usb_addr].address;
1413		if (ring->in_use) {
1414			mutex_unlock(&u132->scheduler_lock);
1415			u132_endp_put_kref(u132, endp);
1416			return;
1417		} else if (address == 0) {
1418			int retval;
1419			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1420				endp->queue_next];
1421			endp->active = 1;
1422			ring->curr_endp = endp;
1423			ring->in_use = 1;
1424			mutex_unlock(&u132->scheduler_lock);
1425			retval = edset_setup(u132, ring, endp, urb, address,
1426				0x2, u132_hcd_initial_setup_sent);
1427			if (retval != 0)
1428				u132_hcd_giveback_urb(u132, endp, urb, retval);
1429			return;
1430		} else if (endp->usb_addr == 0) {
1431			int retval;
1432			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1433				endp->queue_next];
1434			endp->active = 1;
1435			ring->curr_endp = endp;
1436			ring->in_use = 1;
1437			mutex_unlock(&u132->scheduler_lock);
1438			retval = edset_setup(u132, ring, endp, urb, 0, 0x2,
1439				u132_hcd_enumeration_address_sent);
1440			if (retval != 0)
1441				u132_hcd_giveback_urb(u132, endp, urb, retval);
1442			return;
1443		} else {
1444			int retval;
1445			struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1446				endp->queue_next];
1447			address = u132->addr[endp->usb_addr].address;
1448			endp->active = 1;
1449			ring->curr_endp = endp;
1450			ring->in_use = 1;
1451			mutex_unlock(&u132->scheduler_lock);
1452			retval = edset_setup(u132, ring, endp, urb, address,
1453				0x2, u132_hcd_configure_setup_sent);
1454			if (retval != 0)
1455				u132_hcd_giveback_urb(u132, endp, urb, retval);
1456			return;
1457		}
1458	} else {
1459		if (endp->input) {
1460			u8 address = u132->addr[endp->usb_addr].address;
1461			if (ring->in_use) {
1462				mutex_unlock(&u132->scheduler_lock);
1463				u132_endp_put_kref(u132, endp);
1464				return;
1465			} else {
1466				int retval;
1467				struct urb *urb = endp->urb_list[
1468					ENDP_QUEUE_MASK & endp->queue_next];
1469				endp->active = 1;
1470				ring->curr_endp = endp;
1471				ring->in_use = 1;
1472				mutex_unlock(&u132->scheduler_lock);
1473				retval = edset_input(u132, ring, endp, urb,
1474					address, endp->toggle_bits,
1475					u132_hcd_bulk_input_recv);
1476				if (retval == 0) {
1477				} else
1478					u132_hcd_giveback_urb(u132, endp, urb,
1479						retval);
1480				return;
1481			}
1482		} else {	/* output pipe */
1483			u8 address = u132->addr[endp->usb_addr].address;
1484			if (ring->in_use) {
1485				mutex_unlock(&u132->scheduler_lock);
1486				u132_endp_put_kref(u132, endp);
1487				return;
1488			} else {
1489				int retval;
1490				struct urb *urb = endp->urb_list[
1491					ENDP_QUEUE_MASK & endp->queue_next];
1492				endp->active = 1;
1493				ring->curr_endp = endp;
1494				ring->in_use = 1;
1495				mutex_unlock(&u132->scheduler_lock);
1496				retval = edset_output(u132, ring, endp, urb,
1497					address, endp->toggle_bits,
1498					u132_hcd_bulk_output_sent);
1499				if (retval == 0) {
1500				} else
1501					u132_hcd_giveback_urb(u132, endp, urb,
1502						retval);
1503				return;
1504			}
1505		}
1506	}
1507}
1508#ifdef CONFIG_PM
1509
1510static void port_power(struct u132 *u132, int pn, int is_on)
1511{
1512	u132->port[pn].power = is_on;
1513}
1514
1515#endif
1516
1517static void u132_power(struct u132 *u132, int is_on)
1518{
1519	struct usb_hcd *hcd = u132_to_hcd(u132)
1520		;	/* hub is inactive unless the port is powered */
1521	if (is_on) {
1522		if (u132->power)
1523			return;
1524		u132->power = 1;
1525	} else {
1526		u132->power = 0;
1527		hcd->state = HC_STATE_HALT;
1528	}
1529}
1530
1531static int u132_periodic_reinit(struct u132 *u132)
1532{
1533	int retval;
1534	u32 fi = u132->hc_fminterval & 0x03fff;
1535	u32 fit;
1536	u32 fminterval;
1537	retval = u132_read_pcimem(u132, fminterval, &fminterval);
1538	if (retval)
1539		return retval;
1540	fit = fminterval & FIT;
1541	retval = u132_write_pcimem(u132, fminterval,
1542		(fit ^ FIT) | u132->hc_fminterval);
1543	if (retval)
1544		return retval;
1545	retval = u132_write_pcimem(u132, periodicstart,
1546		((9 * fi) / 10) & 0x3fff);
1547	if (retval)
1548		return retval;
1549	return 0;
1550}
1551
1552static char *hcfs2string(int state)
1553{
1554	switch (state) {
1555	case OHCI_USB_RESET:
1556		return "reset";
1557	case OHCI_USB_RESUME:
1558		return "resume";
1559	case OHCI_USB_OPER:
1560		return "operational";
1561	case OHCI_USB_SUSPEND:
1562		return "suspend";
1563	}
1564	return "?";
1565}
1566
1567static int u132_init(struct u132 *u132)
1568{
1569	int retval;
1570	u32 control;
1571	u132_disable(u132);
1572	u132->next_statechange = jiffies;
1573	retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
1574	if (retval)
1575		return retval;
1576	retval = u132_read_pcimem(u132, control, &control);
1577	if (retval)
1578		return retval;
1579	if (u132->num_ports == 0) {
1580		u32 rh_a = -1;
1581		retval = u132_read_pcimem(u132, roothub.a, &rh_a);
1582		if (retval)
1583			return retval;
1584		u132->num_ports = rh_a & RH_A_NDP;
1585		retval = read_roothub_info(u132);
1586		if (retval)
1587			return retval;
1588	}
1589	if (u132->num_ports > MAX_U132_PORTS)
1590		return -EINVAL;
1591
1592	return 0;
1593}
1594
1595
1596/* Start an OHCI controller, set the BUS operational
1597* resets USB and controller
1598* enable interrupts
1599*/
1600static int u132_run(struct u132 *u132)
1601{
1602	int retval;
1603	u32 control;
1604	u32 status;
1605	u32 fminterval;
1606	u32 periodicstart;
1607	u32 cmdstatus;
1608	u32 roothub_a;
1609	int mask = OHCI_INTR_INIT;
1610	int first = u132->hc_fminterval == 0;
1611	int sleep_time = 0;
1612	int reset_timeout = 30;	/* ... allow extra time */
1613	u132_disable(u132);
1614	if (first) {
1615		u32 temp;
1616		retval = u132_read_pcimem(u132, fminterval, &temp);
1617		if (retval)
1618			return retval;
1619		u132->hc_fminterval = temp & 0x3fff;
1620		u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
1621	}
1622	retval = u132_read_pcimem(u132, control, &u132->hc_control);
1623	if (retval)
1624		return retval;
1625	dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
1626		"= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
1627		u132->hc_control);
1628	switch (u132->hc_control & OHCI_CTRL_HCFS) {
1629	case OHCI_USB_OPER:
1630		sleep_time = 0;
1631		break;
1632	case OHCI_USB_SUSPEND:
1633	case OHCI_USB_RESUME:
1634		u132->hc_control &= OHCI_CTRL_RWC;
1635		u132->hc_control |= OHCI_USB_RESUME;
1636		sleep_time = 10;
1637		break;
1638	default:
1639		u132->hc_control &= OHCI_CTRL_RWC;
1640		u132->hc_control |= OHCI_USB_RESET;
1641		sleep_time = 50;
1642		break;
1643	}
1644	retval = u132_write_pcimem(u132, control, u132->hc_control);
1645	if (retval)
1646		return retval;
1647	retval = u132_read_pcimem(u132, control, &control);
1648	if (retval)
1649		return retval;
1650	msleep(sleep_time);
1651	retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1652	if (retval)
1653		return retval;
1654	if (!(roothub_a & RH_A_NPS)) {
1655		int temp;	/* power down each port */
1656		for (temp = 0; temp < u132->num_ports; temp++) {
1657			retval = u132_write_pcimem(u132,
1658				roothub.portstatus[temp], RH_PS_LSDA);
1659			if (retval)
1660				return retval;
1661		}
1662	}
1663	retval = u132_read_pcimem(u132, control, &control);
1664	if (retval)
1665		return retval;
1666retry:
1667	retval = u132_read_pcimem(u132, cmdstatus, &status);
1668	if (retval)
1669		return retval;
1670	retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
1671	if (retval)
1672		return retval;
1673extra:	{
1674		retval = u132_read_pcimem(u132, cmdstatus, &status);
1675		if (retval)
1676			return retval;
1677		if (0 != (status & OHCI_HCR)) {
1678			if (--reset_timeout == 0) {
1679				dev_err(&u132->platform_dev->dev, "USB HC reset"
1680					" timed out!\n");
1681				return -ENODEV;
1682			} else {
1683				msleep(5);
1684				goto extra;
1685			}
1686		}
1687	}
1688	if (u132->flags & OHCI_QUIRK_INITRESET) {
1689		retval = u132_write_pcimem(u132, control, u132->hc_control);
1690		if (retval)
1691			return retval;
1692		retval = u132_read_pcimem(u132, control, &control);
1693		if (retval)
1694			return retval;
1695	}
1696	retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
1697	if (retval)
1698		return retval;
1699	retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
1700	if (retval)
1701		return retval;
1702	retval = u132_write_pcimem(u132, hcca, 0x00000000);
1703	if (retval)
1704		return retval;
1705	retval = u132_periodic_reinit(u132);
1706	if (retval)
1707		return retval;
1708	retval = u132_read_pcimem(u132, fminterval, &fminterval);
1709	if (retval)
1710		return retval;
1711	retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
1712	if (retval)
1713		return retval;
1714	if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
1715		if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
1716			u132->flags |= OHCI_QUIRK_INITRESET;
1717			goto retry;
1718		} else
1719			dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
1720				"\n", fminterval, periodicstart);
1721	}			/* start controller operations */
1722	u132->hc_control &= OHCI_CTRL_RWC;
1723	u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
1724	retval = u132_write_pcimem(u132, control, u132->hc_control);
1725	if (retval)
1726		return retval;
1727	retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
1728	if (retval)
1729		return retval;
1730	retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
1731	if (retval)
1732		return retval;
1733	retval = u132_read_pcimem(u132, control, &control);
1734	if (retval)
1735		return retval;
1736	u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1737	retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
1738	if (retval)
1739		return retval;
1740	retval = u132_write_pcimem(u132, intrstatus, mask);
1741	if (retval)
1742		return retval;
1743	retval = u132_write_pcimem(u132, intrdisable,
1744		OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
1745		OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
1746		OHCI_INTR_SO);
1747	if (retval)
1748		return retval;	/* handle root hub init quirks ... */
1749	retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1750	if (retval)
1751		return retval;
1752	roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
1753	if (u132->flags & OHCI_QUIRK_SUPERIO) {
1754		roothub_a |= RH_A_NOCP;
1755		roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
1756		retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1757		if (retval)
1758			return retval;
1759	} else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
1760		roothub_a |= RH_A_NPS;
1761		retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1762		if (retval)
1763			return retval;
1764	}
1765	retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
1766	if (retval)
1767		return retval;
1768	retval = u132_write_pcimem(u132, roothub.b,
1769		(roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
1770	if (retval)
1771		return retval;
1772	retval = u132_read_pcimem(u132, control, &control);
1773	if (retval)
1774		return retval;
1775	mdelay((roothub_a >> 23) & 0x1fe);
1776	u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1777	return 0;
1778}
1779
1780static void u132_hcd_stop(struct usb_hcd *hcd)
1781{
1782	struct u132 *u132 = hcd_to_u132(hcd);
1783	if (u132->going > 1) {
1784		dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
1785			"een removed %d\n", u132, hcd, u132->going);
1786	} else if (u132->going > 0) {
1787		dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
1788			"ed\n", hcd);
1789	} else {
1790		mutex_lock(&u132->sw_lock);
1791		msleep(100);
1792		u132_power(u132, 0);
1793		mutex_unlock(&u132->sw_lock);
1794	}
1795}
1796
1797static int u132_hcd_start(struct usb_hcd *hcd)
1798{
1799	struct u132 *u132 = hcd_to_u132(hcd);
1800	if (u132->going > 1) {
1801		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1802			, u132->going);
1803		return -ENODEV;
1804	} else if (u132->going > 0) {
1805		dev_err(&u132->platform_dev->dev, "device is being removed\n");
1806		return -ESHUTDOWN;
1807	} else if (hcd->self.controller) {
1808		int retval;
1809		struct platform_device *pdev =
1810			to_platform_device(hcd->self.controller);
1811		u16 vendor = ((struct u132_platform_data *)
1812			(pdev->dev.platform_data))->vendor;
1813		u16 device = ((struct u132_platform_data *)
1814			(pdev->dev.platform_data))->device;
1815		mutex_lock(&u132->sw_lock);
1816		msleep(10);
1817		if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
1818			u132->flags = OHCI_QUIRK_AMD756;
1819		} else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
1820			dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
1821				"ounds unavailable\n");
1822		} else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
1823			u132->flags |= OHCI_QUIRK_ZFMICRO;
1824		retval = u132_run(u132);
1825		if (retval) {
1826			u132_disable(u132);
1827			u132->going = 1;
1828		}
1829		msleep(100);
1830		mutex_unlock(&u132->sw_lock);
1831		return retval;
1832	} else {
1833		dev_err(&u132->platform_dev->dev, "platform_device missing\n");
1834		return -ENODEV;
1835	}
1836}
1837
1838static int u132_hcd_reset(struct usb_hcd *hcd)
1839{
1840	struct u132 *u132 = hcd_to_u132(hcd);
1841	if (u132->going > 1) {
1842		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1843			, u132->going);
1844		return -ENODEV;
1845	} else if (u132->going > 0) {
1846		dev_err(&u132->platform_dev->dev, "device is being removed\n");
1847		return -ESHUTDOWN;
1848	} else {
1849		int retval;
1850		mutex_lock(&u132->sw_lock);
1851		retval = u132_init(u132);
1852		if (retval) {
1853			u132_disable(u132);
1854			u132->going = 1;
1855		}
1856		mutex_unlock(&u132->sw_lock);
1857		return retval;
1858	}
1859}
1860
1861static int create_endpoint_and_queue_int(struct u132 *u132,
1862	struct u132_udev *udev, struct urb *urb,
1863	struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1864	gfp_t mem_flags)
1865{
1866	struct u132_ring *ring;
1867	unsigned long irqs;
1868	int rc;
1869	u8 endp_number;
1870	struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1871
1872	if (!endp)
1873		return -ENOMEM;
1874
1875	spin_lock_init(&endp->queue_lock.slock);
1876	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1877	rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1878	if (rc) {
1879		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1880		kfree(endp);
1881		return rc;
1882	}
1883
1884	endp_number = ++u132->num_endpoints;
1885	urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1886	INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1887	INIT_LIST_HEAD(&endp->urb_more);
1888	ring = endp->ring = &u132->ring[0];
1889	if (ring->curr_endp) {
1890		list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
1891	} else {
1892		INIT_LIST_HEAD(&endp->endp_ring);
1893		ring->curr_endp = endp;
1894	}
1895	ring->length += 1;
1896	endp->dequeueing = 0;
1897	endp->edset_flush = 0;
1898	endp->active = 0;
1899	endp->delayed = 0;
1900	endp->endp_number = endp_number;
1901	endp->u132 = u132;
1902	endp->hep = urb->ep;
1903	endp->pipetype = usb_pipetype(urb->pipe);
1904	u132_endp_init_kref(u132, endp);
1905	if (usb_pipein(urb->pipe)) {
1906		endp->toggle_bits = 0x2;
1907		usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1908		endp->input = 1;
1909		endp->output = 0;
1910		udev->endp_number_in[usb_endp] = endp_number;
1911		u132_udev_get_kref(u132, udev);
1912	} else {
1913		endp->toggle_bits = 0x2;
1914		usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1915		endp->input = 0;
1916		endp->output = 1;
1917		udev->endp_number_out[usb_endp] = endp_number;
1918		u132_udev_get_kref(u132, udev);
1919	}
1920	urb->hcpriv = u132;
1921	endp->delayed = 1;
1922	endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1923	endp->udev_number = address;
1924	endp->usb_addr = usb_addr;
1925	endp->usb_endp = usb_endp;
1926	endp->queue_size = 1;
1927	endp->queue_last = 0;
1928	endp->queue_next = 0;
1929	endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1930	spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1931	u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
1932	return 0;
1933}
1934
1935static int queue_int_on_old_endpoint(struct u132 *u132,
1936	struct u132_udev *udev, struct urb *urb,
1937	struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
1938	u8 usb_endp, u8 address)
1939{
1940	urb->hcpriv = u132;
1941	endp->delayed = 1;
1942	endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1943	if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
1944		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1945	} else {
1946		struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
1947			GFP_ATOMIC);
1948		if (urbq == NULL) {
1949			endp->queue_size -= 1;
1950			return -ENOMEM;
1951		} else {
1952			list_add_tail(&urbq->urb_more, &endp->urb_more);
1953			urbq->urb = urb;
1954		}
1955	}
1956	return 0;
1957}
1958
1959static int create_endpoint_and_queue_bulk(struct u132 *u132,
1960	struct u132_udev *udev, struct urb *urb,
1961	struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1962	gfp_t mem_flags)
1963{
1964	int ring_number;
1965	struct u132_ring *ring;
1966	unsigned long irqs;
1967	int rc;
1968	u8 endp_number;
1969	struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1970
1971	if (!endp)
1972		return -ENOMEM;
1973
1974	spin_lock_init(&endp->queue_lock.slock);
1975	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1976	rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1977	if (rc) {
1978		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1979		kfree(endp);
1980		return rc;
1981	}
1982
1983	endp_number = ++u132->num_endpoints;
1984	urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1985	INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1986	INIT_LIST_HEAD(&endp->urb_more);
1987	endp->dequeueing = 0;
1988	endp->edset_flush = 0;
1989	endp->active = 0;
1990	endp->delayed = 0;
1991	endp->endp_number = endp_number;
1992	endp->u132 = u132;
1993	endp->hep = urb->ep;
1994	endp->pipetype = usb_pipetype(urb->pipe);
1995	u132_endp_init_kref(u132, endp);
1996	if (usb_pipein(urb->pipe)) {
1997		endp->toggle_bits = 0x2;
1998		usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1999		ring_number = 3;
2000		endp->input = 1;
2001		endp->output = 0;
2002		udev->endp_number_in[usb_endp] = endp_number;
2003		u132_udev_get_kref(u132, udev);
2004	} else {
2005		endp->toggle_bits = 0x2;
2006		usb_settoggle(udev->usb_device, usb_endp, 1, 0);
2007		ring_number = 2;
2008		endp->input = 0;
2009		endp->output = 1;
2010		udev->endp_number_out[usb_endp] = endp_number;
2011		u132_udev_get_kref(u132, udev);
2012	}
2013	ring = endp->ring = &u132->ring[ring_number - 1];
2014	if (ring->curr_endp) {
2015		list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2016	} else {
2017		INIT_LIST_HEAD(&endp->endp_ring);
2018		ring->curr_endp = endp;
2019	}
2020	ring->length += 1;
2021	urb->hcpriv = u132;
2022	endp->udev_number = address;
2023	endp->usb_addr = usb_addr;
2024	endp->usb_endp = usb_endp;
2025	endp->queue_size = 1;
2026	endp->queue_last = 0;
2027	endp->queue_next = 0;
2028	endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2029	spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2030	u132_endp_queue_work(u132, endp, 0);
2031	return 0;
2032}
2033
2034static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
2035	struct urb *urb,
2036	struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2037	u8 usb_endp, u8 address)
2038{
2039	urb->hcpriv = u132;
2040	if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2041		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2042	} else {
2043		struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
2044			GFP_ATOMIC);
2045		if (urbq == NULL) {
2046			endp->queue_size -= 1;
2047			return -ENOMEM;
2048		} else {
2049			list_add_tail(&urbq->urb_more, &endp->urb_more);
2050			urbq->urb = urb;
2051		}
2052	}
2053	return 0;
2054}
2055
2056static int create_endpoint_and_queue_control(struct u132 *u132,
2057	struct urb *urb,
2058	struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
2059	gfp_t mem_flags)
2060{
2061	struct u132_ring *ring;
2062	unsigned long irqs;
2063	int rc;
2064	u8 endp_number;
2065	struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
2066
2067	if (!endp)
2068		return -ENOMEM;
2069
2070	spin_lock_init(&endp->queue_lock.slock);
2071	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2072	rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
2073	if (rc) {
2074		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2075		kfree(endp);
2076		return rc;
2077	}
2078
2079	endp_number = ++u132->num_endpoints;
2080	urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
2081	INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2082	INIT_LIST_HEAD(&endp->urb_more);
2083	ring = endp->ring = &u132->ring[0];
2084	if (ring->curr_endp) {
2085		list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2086	} else {
2087		INIT_LIST_HEAD(&endp->endp_ring);
2088		ring->curr_endp = endp;
2089	}
2090	ring->length += 1;
2091	endp->dequeueing = 0;
2092	endp->edset_flush = 0;
2093	endp->active = 0;
2094	endp->delayed = 0;
2095	endp->endp_number = endp_number;
2096	endp->u132 = u132;
2097	endp->hep = urb->ep;
2098	u132_endp_init_kref(u132, endp);
2099	u132_endp_get_kref(u132, endp);
2100	if (usb_addr == 0) {
2101		u8 address = u132->addr[usb_addr].address;
2102		struct u132_udev *udev = &u132->udev[address];
2103		endp->udev_number = address;
2104		endp->usb_addr = usb_addr;
2105		endp->usb_endp = usb_endp;
2106		endp->input = 1;
2107		endp->output = 1;
2108		endp->pipetype = usb_pipetype(urb->pipe);
2109		u132_udev_init_kref(u132, udev);
2110		u132_udev_get_kref(u132, udev);
2111		udev->endp_number_in[usb_endp] = endp_number;
2112		udev->endp_number_out[usb_endp] = endp_number;
2113		urb->hcpriv = u132;
2114		endp->queue_size = 1;
2115		endp->queue_last = 0;
2116		endp->queue_next = 0;
2117		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2118		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2119		u132_endp_queue_work(u132, endp, 0);
2120		return 0;
2121	} else {		/*(usb_addr > 0) */
2122		u8 address = u132->addr[usb_addr].address;
2123		struct u132_udev *udev = &u132->udev[address];
2124		endp->udev_number = address;
2125		endp->usb_addr = usb_addr;
2126		endp->usb_endp = usb_endp;
2127		endp->input = 1;
2128		endp->output = 1;
2129		endp->pipetype = usb_pipetype(urb->pipe);
2130		u132_udev_get_kref(u132, udev);
2131		udev->enumeration = 2;
2132		udev->endp_number_in[usb_endp] = endp_number;
2133		udev->endp_number_out[usb_endp] = endp_number;
2134		urb->hcpriv = u132;
2135		endp->queue_size = 1;
2136		endp->queue_last = 0;
2137		endp->queue_next = 0;
2138		endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2139		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2140		u132_endp_queue_work(u132, endp, 0);
2141		return 0;
2142	}
2143}
2144
2145static int queue_control_on_old_endpoint(struct u132 *u132,
2146	struct urb *urb,
2147	struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2148	u8 usb_endp)
2149{
2150	if (usb_addr == 0) {
2151		if (usb_pipein(urb->pipe)) {
2152			urb->hcpriv = u132;
2153			if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2154				endp->urb_list[ENDP_QUEUE_MASK &
2155					endp->queue_last++] = urb;
2156			} else {
2157				struct u132_urbq *urbq =
2158					kmalloc(sizeof(struct u132_urbq),
2159					GFP_ATOMIC);
2160				if (urbq == NULL) {
2161					endp->queue_size -= 1;
2162					return -ENOMEM;
2163				} else {
2164					list_add_tail(&urbq->urb_more,
2165						&endp->urb_more);
2166					urbq->urb = urb;
2167				}
2168			}
2169			return 0;
2170		} else {	/* usb_pipeout(urb->pipe) */
2171			struct u132_addr *addr = &u132->addr[usb_dev->devnum];
2172			int I = MAX_U132_UDEVS;
2173			int i = 0;
2174			while (--I > 0) {
2175				struct u132_udev *udev = &u132->udev[++i];
2176				if (udev->usb_device) {
2177					continue;
2178				} else {
2179					udev->enumeration = 1;
2180					u132->addr[0].address = i;
2181					endp->udev_number = i;
2182					udev->udev_number = i;
2183					udev->usb_addr = usb_dev->devnum;
2184					u132_udev_init_kref(u132, udev);
2185					udev->endp_number_in[usb_endp] =
2186						endp->endp_number;
2187					u132_udev_get_kref(u132, udev);
2188					udev->endp_number_out[usb_endp] =
2189						endp->endp_number;
2190					udev->usb_device = usb_dev;
2191					((u8 *) (urb->setup_packet))[2] =
2192						addr->address = i;
2193					u132_udev_get_kref(u132, udev);
2194					break;
2195				}
2196			}
2197			if (I == 0) {
2198				dev_err(&u132->platform_dev->dev, "run out of d"
2199					"evice space\n");
2200				return -EINVAL;
2201			}
2202			urb->hcpriv = u132;
2203			if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2204				endp->urb_list[ENDP_QUEUE_MASK &
2205					endp->queue_last++] = urb;
2206			} else {
2207				struct u132_urbq *urbq =
2208					kmalloc(sizeof(struct u132_urbq),
2209					GFP_ATOMIC);
2210				if (urbq == NULL) {
2211					endp->queue_size -= 1;
2212					return -ENOMEM;
2213				} else {
2214					list_add_tail(&urbq->urb_more,
2215						&endp->urb_more);
2216					urbq->urb = urb;
2217				}
2218			}
2219			return 0;
2220		}
2221	} else {		/*(usb_addr > 0) */
2222		u8 address = u132->addr[usb_addr].address;
2223		struct u132_udev *udev = &u132->udev[address];
2224		urb->hcpriv = u132;
2225		if (udev->enumeration != 2)
2226			udev->enumeration = 2;
2227		if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2228			endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
2229				urb;
2230		} else {
2231			struct u132_urbq *urbq =
2232				kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
2233			if (urbq == NULL) {
2234				endp->queue_size -= 1;
2235				return -ENOMEM;
2236			} else {
2237				list_add_tail(&urbq->urb_more, &endp->urb_more);
2238				urbq->urb = urb;
2239			}
2240		}
2241		return 0;
2242	}
2243}
2244
2245static int u132_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2246		gfp_t mem_flags)
2247{
2248	struct u132 *u132 = hcd_to_u132(hcd);
2249	if (irqs_disabled()) {
2250		if (__GFP_WAIT & mem_flags) {
2251			printk(KERN_ERR "invalid context for function that migh"
2252				"t sleep\n");
2253			return -EINVAL;
2254		}
2255	}
2256	if (u132->going > 1) {
2257		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2258			, u132->going);
2259		return -ENODEV;
2260	} else if (u132->going > 0) {
2261		dev_err(&u132->platform_dev->dev, "device is being removed "
2262				"urb=%p\n", urb);
2263		return -ESHUTDOWN;
2264	} else {
2265		u8 usb_addr = usb_pipedevice(urb->pipe);
2266		u8 usb_endp = usb_pipeendpoint(urb->pipe);
2267		struct usb_device *usb_dev = urb->dev;
2268		if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2269			u8 address = u132->addr[usb_addr].address;
2270			struct u132_udev *udev = &u132->udev[address];
2271			struct u132_endp *endp = urb->ep->hcpriv;
2272			urb->actual_length = 0;
2273			if (endp) {
2274				unsigned long irqs;
2275				int retval;
2276				spin_lock_irqsave(&endp->queue_lock.slock,
2277					irqs);
2278				retval = usb_hcd_link_urb_to_ep(hcd, urb);
2279				if (retval == 0) {
2280					retval = queue_int_on_old_endpoint(
2281							u132, udev, urb,
2282							usb_dev, endp,
2283							usb_addr, usb_endp,
2284							address);
2285					if (retval)
2286						usb_hcd_unlink_urb_from_ep(
2287	hcd, urb);
2288				}
2289				spin_unlock_irqrestore(&endp->queue_lock.slock,
2290					irqs);
2291				if (retval) {
2292					return retval;
2293				} else {
2294					u132_endp_queue_work(u132, endp,
2295						msecs_to_jiffies(urb->interval))
2296						;
2297					return 0;
2298				}
2299			} else if (u132->num_endpoints == MAX_U132_ENDPS) {
2300				return -EINVAL;
2301			} else {	/*(endp == NULL) */
2302				return create_endpoint_and_queue_int(u132, udev,
2303						urb, usb_dev, usb_addr,
2304						usb_endp, address, mem_flags);
2305			}
2306		} else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2307			dev_err(&u132->platform_dev->dev, "the hardware does no"
2308				"t support PIPE_ISOCHRONOUS\n");
2309			return -EINVAL;
2310		} else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2311			u8 address = u132->addr[usb_addr].address;
2312			struct u132_udev *udev = &u132->udev[address];
2313			struct u132_endp *endp = urb->ep->hcpriv;
2314			urb->actual_length = 0;
2315			if (endp) {
2316				unsigned long irqs;
2317				int retval;
2318				spin_lock_irqsave(&endp->queue_lock.slock,
2319					irqs);
2320				retval = usb_hcd_link_urb_to_ep(hcd, urb);
2321				if (retval == 0) {
2322					retval = queue_bulk_on_old_endpoint(
2323							u132, udev, urb,
2324							usb_dev, endp,
2325							usb_addr, usb_endp,
2326							address);
2327					if (retval)
2328						usb_hcd_unlink_urb_from_ep(
2329	hcd, urb);
2330				}
2331				spin_unlock_irqrestore(&endp->queue_lock.slock,
2332					irqs);
2333				if (retval) {
2334					return retval;
2335				} else {
2336					u132_endp_queue_work(u132, endp, 0);
2337					return 0;
2338				}
2339			} else if (u132->num_endpoints == MAX_U132_ENDPS) {
2340				return -EINVAL;
2341			} else
2342				return create_endpoint_and_queue_bulk(u132,
2343					udev, urb, usb_dev, usb_addr,
2344					usb_endp, address, mem_flags);
2345		} else {
2346			struct u132_endp *endp = urb->ep->hcpriv;
2347			u16 urb_size = 8;
2348			u8 *b = urb->setup_packet;
2349			int i = 0;
2350			char data[30 * 3 + 4];
2351			char *d = data;
2352			int m = (sizeof(data) - 1) / 3;
2353			int l = 0;
2354			data[0] = 0;
2355			while (urb_size-- > 0) {
2356				if (i > m) {
2357				} else if (i++ < m) {
2358					int w = sprintf(d, " %02X", *b++);
2359					d += w;
2360					l += w;
2361				} else
2362					d += sprintf(d, " ..");
2363			}
2364			if (endp) {
2365				unsigned long irqs;
2366				int retval;
2367				spin_lock_irqsave(&endp->queue_lock.slock,
2368					irqs);
2369				retval = usb_hcd_link_urb_to_ep(hcd, urb);
2370				if (retval == 0) {
2371					retval = queue_control_on_old_endpoint(
2372							u132, urb, usb_dev,
2373							endp, usb_addr,
2374							usb_endp);
2375					if (retval)
2376						usb_hcd_unlink_urb_from_ep(
2377								hcd, urb);
2378				}
2379				spin_unlock_irqrestore(&endp->queue_lock.slock,
2380					irqs);
2381				if (retval) {
2382					return retval;
2383				} else {
2384					u132_endp_queue_work(u132, endp, 0);
2385					return 0;
2386				}
2387			} else if (u132->num_endpoints == MAX_U132_ENDPS) {
2388				return -EINVAL;
2389			} else
2390				return create_endpoint_and_queue_control(u132,
2391					urb, usb_dev, usb_addr, usb_endp,
2392					mem_flags);
2393		}
2394	}
2395}
2396
2397static int dequeue_from_overflow_chain(struct u132 *u132,
2398	struct u132_endp *endp, struct urb *urb)
2399{
2400	struct list_head *scan;
2401	struct list_head *head = &endp->urb_more;
2402	list_for_each(scan, head) {
2403		struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
2404			urb_more);
2405		if (urbq->urb == urb) {
2406			struct usb_hcd *hcd = u132_to_hcd(u132);
2407			list_del(scan);
2408			endp->queue_size -= 1;
2409			urb->error_count = 0;
2410			usb_hcd_giveback_urb(hcd, urb, 0);
2411			return 0;
2412		} else
2413			continue;
2414	}
2415	dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
2416		"[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
2417		"\n", urb, endp->endp_number, endp, endp->ring->number,
2418		endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2419		endp->usb_endp, endp->usb_addr, endp->queue_size,
2420		endp->queue_next, endp->queue_last);
2421	return -EINVAL;
2422}
2423
2424static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
2425		struct urb *urb, int status)
2426{
2427	unsigned long irqs;
2428	int rc;
2429
2430	spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2431	rc = usb_hcd_check_unlink_urb(u132_to_hcd(u132), urb, status);
2432	if (rc) {
2433		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2434		return rc;
2435	}
2436	if (endp->queue_size == 0) {
2437		dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
2438			"=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
2439			endp->endp_number, endp, endp->ring->number,
2440			endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2441			endp->usb_endp, endp->usb_addr);
2442		spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2443		return -EINVAL;
2444	}
2445	if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
2446		if (endp->active) {
2447			endp->dequeueing = 1;
2448			endp->edset_flush = 1;
2449			u132_endp_queue_work(u132, endp, 0);
2450			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2451			return 0;
2452		} else {
2453			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2454			u132_hcd_abandon_urb(u132, endp, urb, status);
2455			return 0;
2456		}
2457	} else {
2458		u16 queue_list = 0;
2459		u16 queue_size = endp->queue_size;
2460		u16 queue_scan = endp->queue_next;
2461		struct urb **urb_slot = NULL;
2462		while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2463			if (urb == endp->urb_list[ENDP_QUEUE_MASK &
2464				++queue_scan]) {
2465				urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2466					queue_scan];
2467				break;
2468			} else
2469				continue;
2470		}
2471		while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2472			*urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
2473				++queue_scan];
2474			urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2475				queue_scan];
2476		}
2477		if (urb_slot) {
2478			struct usb_hcd *hcd = u132_to_hcd(u132);
2479
2480			usb_hcd_unlink_urb_from_ep(hcd, urb);
2481			endp->queue_size -= 1;
2482			if (list_empty(&endp->urb_more)) {
2483				spin_unlock_irqrestore(&endp->queue_lock.slock,
2484					irqs);
2485			} else {
2486				struct list_head *next = endp->urb_more.next;
2487				struct u132_urbq *urbq = list_entry(next,
2488					struct u132_urbq, urb_more);
2489				list_del(next);
2490				*urb_slot = urbq->urb;
2491				spin_unlock_irqrestore(&endp->queue_lock.slock,
2492					irqs);
2493				kfree(urbq);
2494			} urb->error_count = 0;
2495			usb_hcd_giveback_urb(hcd, urb, status);
2496			return 0;
2497		} else if (list_empty(&endp->urb_more)) {
2498			dev_err(&u132->platform_dev->dev, "urb=%p not found in "
2499				"endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
2500				"=%d size=%d next=%04X last=%04X\n", urb,
2501				endp->endp_number, endp, endp->ring->number,
2502				endp->input ? 'I' : ' ',
2503				endp->output ? 'O' : ' ', endp->usb_endp,
2504				endp->usb_addr, endp->queue_size,
2505				endp->queue_next, endp->queue_last);
2506			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2507			return -EINVAL;
2508		} else {
2509			int retval;
2510
2511			usb_hcd_unlink_urb_from_ep(u132_to_hcd(u132), urb);
2512			retval = dequeue_from_overflow_chain(u132, endp,
2513				urb);
2514			spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2515			return retval;
2516		}
2517	}
2518}
2519
2520static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2521{
2522	struct u132 *u132 = hcd_to_u132(hcd);
2523	if (u132->going > 2) {
2524		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2525			, u132->going);
2526		return -ENODEV;
2527	} else {
2528		u8 usb_addr = usb_pipedevice(urb->pipe);
2529		u8 usb_endp = usb_pipeendpoint(urb->pipe);
2530		u8 address = u132->addr[usb_addr].address;
2531		struct u132_udev *udev = &u132->udev[address];
2532		if (usb_pipein(urb->pipe)) {
2533			u8 endp_number = udev->endp_number_in[usb_endp];
2534			struct u132_endp *endp = u132->endp[endp_number - 1];
2535			return u132_endp_urb_dequeue(u132, endp, urb, status);
2536		} else {
2537			u8 endp_number = udev->endp_number_out[usb_endp];
2538			struct u132_endp *endp = u132->endp[endp_number - 1];
2539			return u132_endp_urb_dequeue(u132, endp, urb, status);
2540		}
2541	}
2542}
2543
2544static void u132_endpoint_disable(struct usb_hcd *hcd,
2545	struct usb_host_endpoint *hep)
2546{
2547	struct u132 *u132 = hcd_to_u132(hcd);
2548	if (u132->going > 2) {
2549		dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
2550			") has been removed %d\n", u132, hcd, hep,
2551			u132->going);
2552	} else {
2553		struct u132_endp *endp = hep->hcpriv;
2554		if (endp)
2555			u132_endp_put_kref(u132, endp);
2556	}
2557}
2558
2559static int u132_get_frame(struct usb_hcd *hcd)
2560{
2561	struct u132 *u132 = hcd_to_u132(hcd);
2562	if (u132->going > 1) {
2563		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2564			, u132->going);
2565		return -ENODEV;
2566	} else if (u132->going > 0) {
2567		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2568		return -ESHUTDOWN;
2569	} else {
2570		int frame = 0;
2571		dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
2572		msleep(100);
2573		return frame;
2574	}
2575}
2576
2577static int u132_roothub_descriptor(struct u132 *u132,
2578	struct usb_hub_descriptor *desc)
2579{
2580	int retval;
2581	u16 temp;
2582	u32 rh_a = -1;
2583	u32 rh_b = -1;
2584	retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2585	if (retval)
2586		return retval;
2587	desc->bDescriptorType = 0x29;
2588	desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2589	desc->bHubContrCurrent = 0;
2590	desc->bNbrPorts = u132->num_ports;
2591	temp = 1 + (u132->num_ports / 8);
2592	desc->bDescLength = 7 + 2 * temp;
2593	temp = 0;
2594	if (rh_a & RH_A_NPS)
2595		temp |= 0x0002;
2596	if (rh_a & RH_A_PSM)
2597		temp |= 0x0001;
2598	if (rh_a & RH_A_NOCP)
2599		temp |= 0x0010;
2600	else if (rh_a & RH_A_OCPM)
2601		temp |= 0x0008;
2602	desc->wHubCharacteristics = cpu_to_le16(temp);
2603	retval = u132_read_pcimem(u132, roothub.b, &rh_b);
2604	if (retval)
2605		return retval;
2606	memset(desc->u.hs.DeviceRemovable, 0xff,
2607			sizeof(desc->u.hs.DeviceRemovable));
2608	desc->u.hs.DeviceRemovable[0] = rh_b & RH_B_DR;
2609	if (u132->num_ports > 7) {
2610		desc->u.hs.DeviceRemovable[1] = (rh_b & RH_B_DR) >> 8;
2611		desc->u.hs.DeviceRemovable[2] = 0xff;
2612	} else
2613		desc->u.hs.DeviceRemovable[1] = 0xff;
2614	return 0;
2615}
2616
2617static int u132_roothub_status(struct u132 *u132, __le32 *desc)
2618{
2619	u32 rh_status = -1;
2620	int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
2621	*desc = cpu_to_le32(rh_status);
2622	return ret_status;
2623}
2624
2625static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
2626{
2627	if (wIndex == 0 || wIndex > u132->num_ports) {
2628		return -EINVAL;
2629	} else {
2630		int port = wIndex - 1;
2631		u32 rh_portstatus = -1;
2632		int ret_portstatus = u132_read_pcimem(u132,
2633			roothub.portstatus[port], &rh_portstatus);
2634		*desc = cpu_to_le32(rh_portstatus);
2635		if (*(u16 *) (desc + 2)) {
2636			dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
2637				"ge = %08X\n", port, *desc);
2638		}
2639		return ret_portstatus;
2640	}
2641}
2642
2643
2644/* this timer value might be vendor-specific ... */
2645#define PORT_RESET_HW_MSEC 10
2646#define PORT_RESET_MSEC 10
2647/* wrap-aware logic morphed from <linux/jiffies.h> */
2648#define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
2649static int u132_roothub_portreset(struct u132 *u132, int port_index)
2650{
2651	int retval;
2652	u32 fmnumber;
2653	u16 now;
2654	u16 reset_done;
2655	retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2656	if (retval)
2657		return retval;
2658	now = fmnumber;
2659	reset_done = now + PORT_RESET_MSEC;
2660	do {
2661		u32 portstat;
2662		do {
2663			retval = u132_read_pcimem(u132,
2664				roothub.portstatus[port_index], &portstat);
2665			if (retval)
2666				return retval;
2667			if (RH_PS_PRS & portstat)
2668				continue;
2669			else
2670				break;
2671		} while (tick_before(now, reset_done));
2672		if (RH_PS_PRS & portstat)
2673			return -ENODEV;
2674		if (RH_PS_CCS & portstat) {
2675			if (RH_PS_PRSC & portstat) {
2676				retval = u132_write_pcimem(u132,
2677					roothub.portstatus[port_index],
2678					RH_PS_PRSC);
2679				if (retval)
2680					return retval;
2681			}
2682		} else
2683			break;	/* start the next reset,
2684				sleep till it's probably done */
2685		retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2686			 RH_PS_PRS);
2687		if (retval)
2688			return retval;
2689		msleep(PORT_RESET_HW_MSEC);
2690		retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2691		if (retval)
2692			return retval;
2693		now = fmnumber;
2694	} while (tick_before(now, reset_done));
2695	return 0;
2696}
2697
2698static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
2699	u16 wIndex)
2700{
2701	if (wIndex == 0 || wIndex > u132->num_ports) {
2702		return -EINVAL;
2703	} else {
2704		int retval;
2705		int port_index = wIndex - 1;
2706		struct u132_port *port = &u132->port[port_index];
2707		port->Status &= ~(1 << wValue);
2708		switch (wValue) {
2709		case USB_PORT_FEAT_SUSPEND:
2710			retval = u132_write_pcimem(u132,
2711				roothub.portstatus[port_index], RH_PS_PSS);
2712			if (retval)
2713				return retval;
2714			return 0;
2715		case USB_PORT_FEAT_POWER:
2716			retval = u132_write_pcimem(u132,
2717				roothub.portstatus[port_index], RH_PS_PPS);
2718			if (retval)
2719				return retval;
2720			return 0;
2721		case USB_PORT_FEAT_RESET:
2722			retval = u132_roothub_portreset(u132, port_index);
2723			if (retval)
2724				return retval;
2725			return 0;
2726		default:
2727			return -EPIPE;
2728		}
2729	}
2730}
2731
2732static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
2733	u16 wIndex)
2734{
2735	if (wIndex == 0 || wIndex > u132->num_ports) {
2736		return -EINVAL;
2737	} else {
2738		int port_index = wIndex - 1;
2739		u32 temp;
2740		int retval;
2741		struct u132_port *port = &u132->port[port_index];
2742		port->Status &= ~(1 << wValue);
2743		switch (wValue) {
2744		case USB_PORT_FEAT_ENABLE:
2745			temp = RH_PS_CCS;
2746			break;
2747		case USB_PORT_FEAT_C_ENABLE:
2748			temp = RH_PS_PESC;
2749			break;
2750		case USB_PORT_FEAT_SUSPEND:
2751			temp = RH_PS_POCI;
2752			if ((u132->hc_control & OHCI_CTRL_HCFS)
2753				!= OHCI_USB_OPER) {
2754				dev_err(&u132->platform_dev->dev, "TODO resume_"
2755					"root_hub\n");
2756			}
2757			break;
2758		case USB_PORT_FEAT_C_SUSPEND:
2759			temp = RH_PS_PSSC;
2760			break;
2761		case USB_PORT_FEAT_POWER:
2762			temp = RH_PS_LSDA;
2763			break;
2764		case USB_PORT_FEAT_C_CONNECTION:
2765			temp = RH_PS_CSC;
2766			break;
2767		case USB_PORT_FEAT_C_OVER_CURRENT:
2768			temp = RH_PS_OCIC;
2769			break;
2770		case USB_PORT_FEAT_C_RESET:
2771			temp = RH_PS_PRSC;
2772			break;
2773		default:
2774			return -EPIPE;
2775		}
2776		retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2777			 temp);
2778		if (retval)
2779			return retval;
2780		return 0;
2781	}
2782}
2783
2784
2785/* the virtual root hub timer IRQ checks for hub status*/
2786static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
2787{
2788	struct u132 *u132 = hcd_to_u132(hcd);
2789	if (u132->going > 1) {
2790		dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
2791			"ed %d\n", hcd, u132->going);
2792		return -ENODEV;
2793	} else if (u132->going > 0) {
2794		dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
2795			"ed\n", hcd);
2796		return -ESHUTDOWN;
2797	} else {
2798		int i, changed = 0, length = 1;
2799		if (u132->flags & OHCI_QUIRK_AMD756) {
2800			if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
2801				dev_err(&u132->platform_dev->dev, "bogus NDP, r"
2802					"ereads as NDP=%d\n",
2803					u132->hc_roothub_a & RH_A_NDP);
2804				goto done;
2805			}
2806		}
2807		if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC))
2808			buf[0] = changed = 1;
2809		else
2810			buf[0] = 0;
2811		if (u132->num_ports > 7) {
2812			buf[1] = 0;
2813			length++;
2814		}
2815		for (i = 0; i < u132->num_ports; i++) {
2816			if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
2817				RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
2818				RH_PS_PRSC)) {
2819				changed = 1;
2820				if (i < 7)
2821					buf[0] |= 1 << (i + 1);
2822				else
2823					buf[1] |= 1 << (i - 7);
2824				continue;
2825			}
2826			if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS))
2827				continue;
2828
2829			if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS))
2830				continue;
2831		}
2832done:
2833		return changed ? length : 0;
2834	}
2835}
2836
2837static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2838	u16 wIndex, char *buf, u16 wLength)
2839{
2840	struct u132 *u132 = hcd_to_u132(hcd);
2841	if (u132->going > 1) {
2842		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2843			, u132->going);
2844		return -ENODEV;
2845	} else if (u132->going > 0) {
2846		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2847		return -ESHUTDOWN;
2848	} else {
2849		int retval = 0;
2850		mutex_lock(&u132->sw_lock);
2851		switch (typeReq) {
2852		case ClearHubFeature:
2853			switch (wValue) {
2854			case C_HUB_OVER_CURRENT:
2855			case C_HUB_LOCAL_POWER:
2856				break;
2857			default:
2858				goto stall;
2859			}
2860			break;
2861		case SetHubFeature:
2862			switch (wValue) {
2863			case C_HUB_OVER_CURRENT:
2864			case C_HUB_LOCAL_POWER:
2865				break;
2866			default:
2867				goto stall;
2868			}
2869			break;
2870		case ClearPortFeature:{
2871				retval = u132_roothub_clearportfeature(u132,
2872					wValue, wIndex);
2873				if (retval)
2874					goto error;
2875				break;
2876			}
2877		case GetHubDescriptor:{
2878				retval = u132_roothub_descriptor(u132,
2879					(struct usb_hub_descriptor *)buf);
2880				if (retval)
2881					goto error;
2882				break;
2883			}
2884		case GetHubStatus:{
2885				retval = u132_roothub_status(u132,
2886					(__le32 *) buf);
2887				if (retval)
2888					goto error;
2889				break;
2890			}
2891		case GetPortStatus:{
2892				retval = u132_roothub_portstatus(u132,
2893					(__le32 *) buf, wIndex);
2894				if (retval)
2895					goto error;
2896				break;
2897			}
2898		case SetPortFeature:{
2899				retval = u132_roothub_setportfeature(u132,
2900					wValue, wIndex);
2901				if (retval)
2902					goto error;
2903				break;
2904			}
2905		default:
2906			goto stall;
2907		error:
2908			u132_disable(u132);
2909			u132->going = 1;
2910			break;
2911		stall:
2912			retval = -EPIPE;
2913			break;
2914		}
2915		mutex_unlock(&u132->sw_lock);
2916		return retval;
2917	}
2918}
2919
2920static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
2921{
2922	struct u132 *u132 = hcd_to_u132(hcd);
2923	if (u132->going > 1) {
2924		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2925			, u132->going);
2926		return -ENODEV;
2927	} else if (u132->going > 0) {
2928		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2929		return -ESHUTDOWN;
2930	} else
2931		return 0;
2932}
2933
2934
2935#ifdef CONFIG_PM
2936static int u132_bus_suspend(struct usb_hcd *hcd)
2937{
2938	struct u132 *u132 = hcd_to_u132(hcd);
2939	if (u132->going > 1) {
2940		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2941			, u132->going);
2942		return -ENODEV;
2943	} else if (u132->going > 0) {
2944		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2945		return -ESHUTDOWN;
2946	} else
2947		return 0;
2948}
2949
2950static int u132_bus_resume(struct usb_hcd *hcd)
2951{
2952	struct u132 *u132 = hcd_to_u132(hcd);
2953	if (u132->going > 1) {
2954		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2955			, u132->going);
2956		return -ENODEV;
2957	} else if (u132->going > 0) {
2958		dev_err(&u132->platform_dev->dev, "device is being removed\n");
2959		return -ESHUTDOWN;
2960	} else
2961		return 0;
2962}
2963
2964#else
2965#define u132_bus_suspend NULL
2966#define u132_bus_resume NULL
2967#endif
2968static struct hc_driver u132_hc_driver = {
2969	.description = hcd_name,
2970	.hcd_priv_size = sizeof(struct u132),
2971	.irq = NULL,
2972	.flags = HCD_USB11 | HCD_MEMORY,
2973	.reset = u132_hcd_reset,
2974	.start = u132_hcd_start,
2975	.stop = u132_hcd_stop,
2976	.urb_enqueue = u132_urb_enqueue,
2977	.urb_dequeue = u132_urb_dequeue,
2978	.endpoint_disable = u132_endpoint_disable,
2979	.get_frame_number = u132_get_frame,
2980	.hub_status_data = u132_hub_status_data,
2981	.hub_control = u132_hub_control,
2982	.bus_suspend = u132_bus_suspend,
2983	.bus_resume = u132_bus_resume,
2984	.start_port_reset = u132_start_port_reset,
2985};
2986
2987/*
2988* This function may be called by the USB core whilst the "usb_all_devices_rwsem"
2989* is held for writing, thus this module must not call usb_remove_hcd()
2990* synchronously - but instead should immediately stop activity to the
2991* device and asynchronously call usb_remove_hcd()
2992*/
2993static int __devexit u132_remove(struct platform_device *pdev)
2994{
2995	struct usb_hcd *hcd = platform_get_drvdata(pdev);
2996	if (hcd) {
2997		struct u132 *u132 = hcd_to_u132(hcd);
2998		if (u132->going++ > 1) {
2999			dev_err(&u132->platform_dev->dev, "already being remove"
3000				"d\n");
3001			return -ENODEV;
3002		} else {
3003			int rings = MAX_U132_RINGS;
3004			int endps = MAX_U132_ENDPS;
3005			dev_err(&u132->platform_dev->dev, "removing device u132"
3006				".%d\n", u132->sequence_num);
3007			msleep(100);
3008			mutex_lock(&u132->sw_lock);
3009			u132_monitor_cancel_work(u132);
3010			while (rings-- > 0) {
3011				struct u132_ring *ring = &u132->ring[rings];
3012				u132_ring_cancel_work(u132, ring);
3013			} while (endps-- > 0) {
3014				struct u132_endp *endp = u132->endp[endps];
3015				if (endp)
3016					u132_endp_cancel_work(u132, endp);
3017			}
3018			u132->going += 1;
3019			printk(KERN_INFO "removing device u132.%d\n",
3020				u132->sequence_num);
3021			mutex_unlock(&u132->sw_lock);
3022			usb_remove_hcd(hcd);
3023			u132_u132_put_kref(u132);
3024			return 0;
3025		}
3026	} else
3027		return 0;
3028}
3029
3030static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
3031{
3032	int rings = MAX_U132_RINGS;
3033	int ports = MAX_U132_PORTS;
3034	int addrs = MAX_U132_ADDRS;
3035	int udevs = MAX_U132_UDEVS;
3036	int endps = MAX_U132_ENDPS;
3037	u132->board = pdev->dev.platform_data;
3038	u132->platform_dev = pdev;
3039	u132->power = 0;
3040	u132->reset = 0;
3041	mutex_init(&u132->sw_lock);
3042	mutex_init(&u132->scheduler_lock);
3043	while (rings-- > 0) {
3044		struct u132_ring *ring = &u132->ring[rings];
3045		ring->u132 = u132;
3046		ring->number = rings + 1;
3047		ring->length = 0;
3048		ring->curr_endp = NULL;
3049		INIT_DELAYED_WORK(&ring->scheduler,
3050				  u132_hcd_ring_work_scheduler);
3051	}
3052	mutex_lock(&u132->sw_lock);
3053	INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
3054	while (ports-- > 0) {
3055		struct u132_port *port = &u132->port[ports];
3056		port->u132 = u132;
3057		port->reset = 0;
3058		port->enable = 0;
3059		port->power = 0;
3060		port->Status = 0;
3061	}
3062	while (addrs-- > 0) {
3063		struct u132_addr *addr = &u132->addr[addrs];
3064		addr->address = 0;
3065	}
3066	while (udevs-- > 0) {
3067		struct u132_udev *udev = &u132->udev[udevs];
3068		int i = ARRAY_SIZE(udev->endp_number_in);
3069		int o = ARRAY_SIZE(udev->endp_number_out);
3070		udev->usb_device = NULL;
3071		udev->udev_number = 0;
3072		udev->usb_addr = 0;
3073		udev->portnumber = 0;
3074		while (i-- > 0)
3075			udev->endp_number_in[i] = 0;
3076
3077		while (o-- > 0)
3078			udev->endp_number_out[o] = 0;
3079
3080	}
3081	while (endps-- > 0)
3082		u132->endp[endps] = NULL;
3083
3084	mutex_unlock(&u132->sw_lock);
3085}
3086
3087static int __devinit u132_probe(struct platform_device *pdev)
3088{
3089	struct usb_hcd *hcd;
3090	int retval;
3091	u32 control;
3092	u32 rh_a = -1;
3093	u32 num_ports;
3094
3095	msleep(100);
3096	if (u132_exiting > 0)
3097		return -ENODEV;
3098
3099	retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
3100	if (retval)
3101		return retval;
3102	retval = ftdi_read_pcimem(pdev, control, &control);
3103	if (retval)
3104		return retval;
3105	retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
3106	if (retval)
3107		return retval;
3108	num_ports = rh_a & RH_A_NDP;	/* refuse to confuse usbcore */
3109	if (pdev->dev.dma_mask)
3110		return -EINVAL;
3111
3112	hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, dev_name(&pdev->dev));
3113	if (!hcd) {
3114		printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3115			);
3116		ftdi_elan_gone_away(pdev);
3117		return -ENOMEM;
3118	} else {
3119		struct u132 *u132 = hcd_to_u132(hcd);
3120		retval = 0;
3121		hcd->rsrc_start = 0;
3122		mutex_lock(&u132_module_lock);
3123		list_add_tail(&u132->u132_list, &u132_static_list);
3124		u132->sequence_num = ++u132_instances;
3125		mutex_unlock(&u132_module_lock);
3126		u132_u132_init_kref(u132);
3127		u132_initialise(u132, pdev);
3128		hcd->product_desc = "ELAN U132 Host Controller";
3129		retval = usb_add_hcd(hcd, 0, 0);
3130		if (retval != 0) {
3131			dev_err(&u132->platform_dev->dev, "init error %d\n",
3132				retval);
3133			u132_u132_put_kref(u132);
3134			return retval;
3135		} else {
3136			u132_monitor_queue_work(u132, 100);
3137			return 0;
3138		}
3139	}
3140}
3141
3142
3143#ifdef CONFIG_PM
3144/* for this device there's no useful distinction between the controller
3145* and its root hub, except that the root hub only gets direct PM calls
3146* when CONFIG_USB_SUSPEND is enabled.
3147*/
3148static int u132_suspend(struct platform_device *pdev, pm_message_t state)
3149{
3150	struct usb_hcd *hcd = platform_get_drvdata(pdev);
3151	struct u132 *u132 = hcd_to_u132(hcd);
3152	if (u132->going > 1) {
3153		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3154			, u132->going);
3155		return -ENODEV;
3156	} else if (u132->going > 0) {
3157		dev_err(&u132->platform_dev->dev, "device is being removed\n");
3158		return -ESHUTDOWN;
3159	} else {
3160		int retval = 0, ports;
3161
3162		switch (state.event) {
3163		case PM_EVENT_FREEZE:
3164			retval = u132_bus_suspend(hcd);
3165			break;
3166		case PM_EVENT_SUSPEND:
3167		case PM_EVENT_HIBERNATE:
3168			ports = MAX_U132_PORTS;
3169			while (ports-- > 0) {
3170				port_power(u132, ports, 0);
3171			}
3172			break;
3173		}
3174		return retval;
3175	}
3176}
3177
3178static int u132_resume(struct platform_device *pdev)
3179{
3180	struct usb_hcd *hcd = platform_get_drvdata(pdev);
3181	struct u132 *u132 = hcd_to_u132(hcd);
3182	if (u132->going > 1) {
3183		dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3184			, u132->going);
3185		return -ENODEV;
3186	} else if (u132->going > 0) {
3187		dev_err(&u132->platform_dev->dev, "device is being removed\n");
3188		return -ESHUTDOWN;
3189	} else {
3190		int retval = 0;
3191		if (!u132->port[0].power) {
3192			int ports = MAX_U132_PORTS;
3193			while (ports-- > 0) {
3194				port_power(u132, ports, 1);
3195			}
3196			retval = 0;
3197		} else {
3198			retval = u132_bus_resume(hcd);
3199		}
3200		return retval;
3201	}
3202}
3203
3204#else
3205#define u132_suspend NULL
3206#define u132_resume NULL
3207#endif
3208/*
3209* this driver is loaded explicitly by ftdi_u132
3210*
3211* the platform_driver struct is static because it is per type of module
3212*/
3213static struct platform_driver u132_platform_driver = {
3214	.probe = u132_probe,
3215	.remove = __devexit_p(u132_remove),
3216	.suspend = u132_suspend,
3217	.resume = u132_resume,
3218	.driver = {
3219		   .name = (char *)hcd_name,
3220		   .owner = THIS_MODULE,
3221		   },
3222};
3223static int __init u132_hcd_init(void)
3224{
3225	int retval;
3226	INIT_LIST_HEAD(&u132_static_list);
3227	u132_instances = 0;
3228	u132_exiting = 0;
3229	mutex_init(&u132_module_lock);
3230	if (usb_disabled())
3231		return -ENODEV;
3232	printk(KERN_INFO "driver %s\n", hcd_name);
3233	workqueue = create_singlethread_workqueue("u132");
3234	retval = platform_driver_register(&u132_platform_driver);
3235	return retval;
3236}
3237
3238
3239module_init(u132_hcd_init);
3240static void __exit u132_hcd_exit(void)
3241{
3242	struct u132 *u132;
3243	struct u132 *temp;
3244	mutex_lock(&u132_module_lock);
3245	u132_exiting += 1;
3246	mutex_unlock(&u132_module_lock);
3247	list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
3248		platform_device_unregister(u132->platform_dev);
3249	}
3250	platform_driver_unregister(&u132_platform_driver);
3251	printk(KERN_INFO "u132-hcd driver deregistered\n");
3252	wait_event(u132_hcd_wait, u132_instances == 0);
3253	flush_workqueue(workqueue);
3254	destroy_workqueue(workqueue);
3255}
3256
3257
3258module_exit(u132_hcd_exit);
3259MODULE_LICENSE("GPL");
3260MODULE_ALIAS("platform:u132_hcd");