xref: /openbmc/linux/drivers/usb/host/max3421-hcd.c (revision 206204a1)
1 /*
2  * MAX3421 Host Controller driver for USB.
3  *
4  * Author: David Mosberger-Tang <davidm@egauge.net>
5  *
6  * (C) Copyright 2014 David Mosberger-Tang <davidm@egauge.net>
7  *
8  * MAX3421 is a chip implementing a USB 2.0 Full-/Low-Speed host
9  * controller on a SPI bus.
10  *
11  * Based on:
12  *	o MAX3421E datasheet
13  *		http://datasheets.maximintegrated.com/en/ds/MAX3421E.pdf
14  *	o MAX3421E Programming Guide
15  *		http://www.hdl.co.jp/ftpdata/utl-001/AN3785.pdf
16  *	o gadget/dummy_hcd.c
17  *		For USB HCD implementation.
18  *	o Arduino MAX3421 driver
19  *	     https://github.com/felis/USB_Host_Shield_2.0/blob/master/Usb.cpp
20  *
21  * This file is licenced under the GPL v2.
22  *
23  * Important note on worst-case (full-speed) packet size constraints
24  * (See USB 2.0 Section 5.6.3 and following):
25  *
26  *	- control:	  64 bytes
27  *	- isochronous:	1023 bytes
28  *	- interrupt:	  64 bytes
29  *	- bulk:		  64 bytes
30  *
31  * Since the MAX3421 FIFO size is 64 bytes, we do not have to work about
32  * multi-FIFO writes/reads for a single USB packet *except* for isochronous
33  * transfers.  We don't support isochronous transfers at this time, so we
34  * just assume that a USB packet always fits into a single FIFO buffer.
35  *
36  * NOTE: The June 2006 version of "MAX3421E Programming Guide"
37  * (AN3785) has conflicting info for the RCVDAVIRQ bit:
38  *
39  *	The description of RCVDAVIRQ says "The CPU *must* clear
40  *	this IRQ bit (by writing a 1 to it) before reading the
41  *	RCVFIFO data.
42  *
43  * However, the earlier section on "Programming BULK-IN
44  * Transfers" says * that:
45  *
46  *	After the CPU retrieves the data, it clears the
47  *	RCVDAVIRQ bit.
48  *
49  * The December 2006 version has been corrected and it consistently
50  * states the second behavior is the correct one.
51  *
52  * Synchronous SPI transactions sleep so we can't perform any such
53  * transactions while holding a spin-lock (and/or while interrupts are
54  * masked).  To achieve this, all SPI transactions are issued from a
55  * single thread (max3421_spi_thread).
56  */
57 
58 #include <linux/module.h>
59 #include <linux/spi/spi.h>
60 #include <linux/usb.h>
61 #include <linux/usb/hcd.h>
62 
63 #include <linux/platform_data/max3421-hcd.h>
64 
65 #define DRIVER_DESC	"MAX3421 USB Host-Controller Driver"
66 #define DRIVER_VERSION	"1.0"
67 
68 /* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
69 #define USB_MAX_FRAME_NUMBER	0x7ff
70 #define USB_MAX_RETRIES		3 /* # of retries before error is reported */
71 
72 /*
73  * Max. # of times we're willing to retransmit a request immediately in
74  * resposne to a NAK.  Afterwards, we fall back on trying once a frame.
75  */
76 #define NAK_MAX_FAST_RETRANSMITS	2
77 
78 #define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */
79 
80 /* Port-change mask: */
81 #define PORT_C_MASK	((USB_PORT_STAT_C_CONNECTION |	\
82 			  USB_PORT_STAT_C_ENABLE |	\
83 			  USB_PORT_STAT_C_SUSPEND |	\
84 			  USB_PORT_STAT_C_OVERCURRENT | \
85 			  USB_PORT_STAT_C_RESET) << 16)
86 
87 enum max3421_rh_state {
88 	MAX3421_RH_RESET,
89 	MAX3421_RH_SUSPENDED,
90 	MAX3421_RH_RUNNING
91 };
92 
93 enum pkt_state {
94 	PKT_STATE_SETUP,	/* waiting to send setup packet to ctrl pipe */
95 	PKT_STATE_TRANSFER,	/* waiting to xfer transfer_buffer */
96 	PKT_STATE_TERMINATE	/* waiting to terminate control transfer */
97 };
98 
99 enum scheduling_pass {
100 	SCHED_PASS_PERIODIC,
101 	SCHED_PASS_NON_PERIODIC,
102 	SCHED_PASS_DONE
103 };
104 
105 struct max3421_dma_buf {
106 	u8 data[2];
107 };
108 
109 struct max3421_hcd {
110 	spinlock_t lock;
111 
112 	struct task_struct *spi_thread;
113 
114 	struct max3421_hcd *next;
115 
116 	enum max3421_rh_state rh_state;
117 	/* lower 16 bits contain port status, upper 16 bits the change mask: */
118 	u32 port_status;
119 
120 	unsigned active:1;
121 
122 	struct list_head ep_list;	/* list of EP's with work */
123 
124 	/*
125 	 * The following are owned by spi_thread (may be accessed by
126 	 * SPI-thread without acquiring the HCD lock:
127 	 */
128 	u8 rev;				/* chip revision */
129 	u16 frame_number;
130 	/*
131 	 * kmalloc'd buffers guaranteed to be in separate (DMA)
132 	 * cache-lines:
133 	 */
134 	struct max3421_dma_buf *tx;
135 	struct max3421_dma_buf *rx;
136 	/*
137 	 * URB we're currently processing.  Must not be reset to NULL
138 	 * unless MAX3421E chip is idle:
139 	 */
140 	struct urb *curr_urb;
141 	enum scheduling_pass sched_pass;
142 	struct usb_device *loaded_dev;	/* dev that's loaded into the chip */
143 	int loaded_epnum;		/* epnum whose toggles are loaded */
144 	int urb_done;			/* > 0 -> no errors, < 0: errno */
145 	size_t curr_len;
146 	u8 hien;
147 	u8 mode;
148 	u8 iopins[2];
149 	unsigned int do_enable_irq:1;
150 	unsigned int do_reset_hcd:1;
151 	unsigned int do_reset_port:1;
152 	unsigned int do_check_unlink:1;
153 	unsigned int do_iopin_update:1;
154 #ifdef DEBUG
155 	unsigned long err_stat[16];
156 #endif
157 };
158 
159 struct max3421_ep {
160 	struct usb_host_endpoint *ep;
161 	struct list_head ep_list;
162 	u32 naks;
163 	u16 last_active;		/* frame # this ep was last active */
164 	enum pkt_state pkt_state;
165 	u8 retries;
166 	u8 retransmit;			/* packet needs retransmission */
167 };
168 
169 static struct max3421_hcd *max3421_hcd_list;
170 
171 #define MAX3421_FIFO_SIZE	64
172 
173 #define MAX3421_SPI_DIR_RD	0	/* read register from MAX3421 */
174 #define MAX3421_SPI_DIR_WR	1	/* write register to MAX3421 */
175 
176 /* SPI commands: */
177 #define MAX3421_SPI_DIR_SHIFT	1
178 #define MAX3421_SPI_REG_SHIFT	3
179 
180 #define MAX3421_REG_RCVFIFO	1
181 #define MAX3421_REG_SNDFIFO	2
182 #define MAX3421_REG_SUDFIFO	4
183 #define MAX3421_REG_RCVBC	6
184 #define MAX3421_REG_SNDBC	7
185 #define MAX3421_REG_USBIRQ	13
186 #define MAX3421_REG_USBIEN	14
187 #define MAX3421_REG_USBCTL	15
188 #define MAX3421_REG_CPUCTL	16
189 #define MAX3421_REG_PINCTL	17
190 #define MAX3421_REG_REVISION	18
191 #define MAX3421_REG_IOPINS1	20
192 #define MAX3421_REG_IOPINS2	21
193 #define MAX3421_REG_GPINIRQ	22
194 #define MAX3421_REG_GPINIEN	23
195 #define MAX3421_REG_GPINPOL	24
196 #define MAX3421_REG_HIRQ	25
197 #define MAX3421_REG_HIEN	26
198 #define MAX3421_REG_MODE	27
199 #define MAX3421_REG_PERADDR	28
200 #define MAX3421_REG_HCTL	29
201 #define MAX3421_REG_HXFR	30
202 #define MAX3421_REG_HRSL	31
203 
204 enum {
205 	MAX3421_USBIRQ_OSCOKIRQ_BIT = 0,
206 	MAX3421_USBIRQ_NOVBUSIRQ_BIT = 5,
207 	MAX3421_USBIRQ_VBUSIRQ_BIT
208 };
209 
210 enum {
211 	MAX3421_CPUCTL_IE_BIT = 0,
212 	MAX3421_CPUCTL_PULSEWID0_BIT = 6,
213 	MAX3421_CPUCTL_PULSEWID1_BIT
214 };
215 
216 enum {
217 	MAX3421_USBCTL_PWRDOWN_BIT = 4,
218 	MAX3421_USBCTL_CHIPRES_BIT
219 };
220 
221 enum {
222 	MAX3421_PINCTL_GPXA_BIT	= 0,
223 	MAX3421_PINCTL_GPXB_BIT,
224 	MAX3421_PINCTL_POSINT_BIT,
225 	MAX3421_PINCTL_INTLEVEL_BIT,
226 	MAX3421_PINCTL_FDUPSPI_BIT,
227 	MAX3421_PINCTL_EP0INAK_BIT,
228 	MAX3421_PINCTL_EP2INAK_BIT,
229 	MAX3421_PINCTL_EP3INAK_BIT,
230 };
231 
232 enum {
233 	MAX3421_HI_BUSEVENT_BIT = 0,	/* bus-reset/-resume */
234 	MAX3421_HI_RWU_BIT,		/* remote wakeup */
235 	MAX3421_HI_RCVDAV_BIT,		/* receive FIFO data available */
236 	MAX3421_HI_SNDBAV_BIT,		/* send buffer available */
237 	MAX3421_HI_SUSDN_BIT,		/* suspend operation done */
238 	MAX3421_HI_CONDET_BIT,		/* peripheral connect/disconnect */
239 	MAX3421_HI_FRAME_BIT,		/* frame generator */
240 	MAX3421_HI_HXFRDN_BIT,		/* host transfer done */
241 };
242 
243 enum {
244 	MAX3421_HCTL_BUSRST_BIT = 0,
245 	MAX3421_HCTL_FRMRST_BIT,
246 	MAX3421_HCTL_SAMPLEBUS_BIT,
247 	MAX3421_HCTL_SIGRSM_BIT,
248 	MAX3421_HCTL_RCVTOG0_BIT,
249 	MAX3421_HCTL_RCVTOG1_BIT,
250 	MAX3421_HCTL_SNDTOG0_BIT,
251 	MAX3421_HCTL_SNDTOG1_BIT
252 };
253 
254 enum {
255 	MAX3421_MODE_HOST_BIT = 0,
256 	MAX3421_MODE_LOWSPEED_BIT,
257 	MAX3421_MODE_HUBPRE_BIT,
258 	MAX3421_MODE_SOFKAENAB_BIT,
259 	MAX3421_MODE_SEPIRQ_BIT,
260 	MAX3421_MODE_DELAYISO_BIT,
261 	MAX3421_MODE_DMPULLDN_BIT,
262 	MAX3421_MODE_DPPULLDN_BIT
263 };
264 
265 enum {
266 	MAX3421_HRSL_OK = 0,
267 	MAX3421_HRSL_BUSY,
268 	MAX3421_HRSL_BADREQ,
269 	MAX3421_HRSL_UNDEF,
270 	MAX3421_HRSL_NAK,
271 	MAX3421_HRSL_STALL,
272 	MAX3421_HRSL_TOGERR,
273 	MAX3421_HRSL_WRONGPID,
274 	MAX3421_HRSL_BADBC,
275 	MAX3421_HRSL_PIDERR,
276 	MAX3421_HRSL_PKTERR,
277 	MAX3421_HRSL_CRCERR,
278 	MAX3421_HRSL_KERR,
279 	MAX3421_HRSL_JERR,
280 	MAX3421_HRSL_TIMEOUT,
281 	MAX3421_HRSL_BABBLE,
282 	MAX3421_HRSL_RESULT_MASK = 0xf,
283 	MAX3421_HRSL_RCVTOGRD_BIT = 4,
284 	MAX3421_HRSL_SNDTOGRD_BIT,
285 	MAX3421_HRSL_KSTATUS_BIT,
286 	MAX3421_HRSL_JSTATUS_BIT
287 };
288 
289 /* Return same error-codes as ohci.h:cc_to_error: */
290 static const int hrsl_to_error[] = {
291 	[MAX3421_HRSL_OK] =		0,
292 	[MAX3421_HRSL_BUSY] =		-EINVAL,
293 	[MAX3421_HRSL_BADREQ] =		-EINVAL,
294 	[MAX3421_HRSL_UNDEF] =		-EINVAL,
295 	[MAX3421_HRSL_NAK] =		-EAGAIN,
296 	[MAX3421_HRSL_STALL] =		-EPIPE,
297 	[MAX3421_HRSL_TOGERR] =		-EILSEQ,
298 	[MAX3421_HRSL_WRONGPID] =	-EPROTO,
299 	[MAX3421_HRSL_BADBC] =		-EREMOTEIO,
300 	[MAX3421_HRSL_PIDERR] =		-EPROTO,
301 	[MAX3421_HRSL_PKTERR] =		-EPROTO,
302 	[MAX3421_HRSL_CRCERR] =		-EILSEQ,
303 	[MAX3421_HRSL_KERR] =		-EIO,
304 	[MAX3421_HRSL_JERR] =		-EIO,
305 	[MAX3421_HRSL_TIMEOUT] =	-ETIME,
306 	[MAX3421_HRSL_BABBLE] =		-EOVERFLOW
307 };
308 
309 /*
310  * See http://www.beyondlogic.org/usbnutshell/usb4.shtml#Control for a
311  * reasonable overview of how control transfers use the the IN/OUT
312  * tokens.
313  */
314 #define MAX3421_HXFR_BULK_IN(ep)	(0x00 | (ep))	/* bulk or interrupt */
315 #define MAX3421_HXFR_SETUP		 0x10
316 #define MAX3421_HXFR_BULK_OUT(ep)	(0x20 | (ep))	/* bulk or interrupt */
317 #define MAX3421_HXFR_ISO_IN(ep)		(0x40 | (ep))
318 #define MAX3421_HXFR_ISO_OUT(ep)	(0x60 | (ep))
319 #define MAX3421_HXFR_HS_IN		 0x80		/* handshake in */
320 #define MAX3421_HXFR_HS_OUT		 0xa0		/* handshake out */
321 
322 #define field(val, bit)	((val) << (bit))
323 
324 static inline s16
325 frame_diff(u16 left, u16 right)
326 {
327 	return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1);
328 }
329 
330 static inline struct max3421_hcd *
331 hcd_to_max3421(struct usb_hcd *hcd)
332 {
333 	return (struct max3421_hcd *) hcd->hcd_priv;
334 }
335 
336 static inline struct usb_hcd *
337 max3421_to_hcd(struct max3421_hcd *max3421_hcd)
338 {
339 	return container_of((void *) max3421_hcd, struct usb_hcd, hcd_priv);
340 }
341 
342 static u8
343 spi_rd8(struct usb_hcd *hcd, unsigned int reg)
344 {
345 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
346 	struct spi_device *spi = to_spi_device(hcd->self.controller);
347 	struct spi_transfer transfer;
348 	struct spi_message msg;
349 
350 	memset(&transfer, 0, sizeof(transfer));
351 
352 	spi_message_init(&msg);
353 
354 	max3421_hcd->tx->data[0] =
355 		(field(reg, MAX3421_SPI_REG_SHIFT) |
356 		 field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
357 
358 	transfer.tx_buf = max3421_hcd->tx->data;
359 	transfer.rx_buf = max3421_hcd->rx->data;
360 	transfer.len = 2;
361 
362 	spi_message_add_tail(&transfer, &msg);
363 	spi_sync(spi, &msg);
364 
365 	return max3421_hcd->rx->data[1];
366 }
367 
368 static void
369 spi_wr8(struct usb_hcd *hcd, unsigned int reg, u8 val)
370 {
371 	struct spi_device *spi = to_spi_device(hcd->self.controller);
372 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
373 	struct spi_transfer transfer;
374 	struct spi_message msg;
375 
376 	memset(&transfer, 0, sizeof(transfer));
377 
378 	spi_message_init(&msg);
379 
380 	max3421_hcd->tx->data[0] =
381 		(field(reg, MAX3421_SPI_REG_SHIFT) |
382 		 field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
383 	max3421_hcd->tx->data[1] = val;
384 
385 	transfer.tx_buf = max3421_hcd->tx->data;
386 	transfer.len = 2;
387 
388 	spi_message_add_tail(&transfer, &msg);
389 	spi_sync(spi, &msg);
390 }
391 
392 static void
393 spi_rd_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
394 {
395 	struct spi_device *spi = to_spi_device(hcd->self.controller);
396 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
397 	struct spi_transfer transfer[2];
398 	struct spi_message msg;
399 
400 	memset(transfer, 0, sizeof(transfer));
401 
402 	spi_message_init(&msg);
403 
404 	max3421_hcd->tx->data[0] =
405 		(field(reg, MAX3421_SPI_REG_SHIFT) |
406 		 field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
407 	transfer[0].tx_buf = max3421_hcd->tx->data;
408 	transfer[0].len = 1;
409 
410 	transfer[1].rx_buf = buf;
411 	transfer[1].len = len;
412 
413 	spi_message_add_tail(&transfer[0], &msg);
414 	spi_message_add_tail(&transfer[1], &msg);
415 	spi_sync(spi, &msg);
416 }
417 
418 static void
419 spi_wr_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
420 {
421 	struct spi_device *spi = to_spi_device(hcd->self.controller);
422 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
423 	struct spi_transfer transfer[2];
424 	struct spi_message msg;
425 
426 	memset(transfer, 0, sizeof(transfer));
427 
428 	spi_message_init(&msg);
429 
430 	max3421_hcd->tx->data[0] =
431 		(field(reg, MAX3421_SPI_REG_SHIFT) |
432 		 field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
433 
434 	transfer[0].tx_buf = max3421_hcd->tx->data;
435 	transfer[0].len = 1;
436 
437 	transfer[1].tx_buf = buf;
438 	transfer[1].len = len;
439 
440 	spi_message_add_tail(&transfer[0], &msg);
441 	spi_message_add_tail(&transfer[1], &msg);
442 	spi_sync(spi, &msg);
443 }
444 
445 /*
446  * Figure out the correct setting for the LOWSPEED and HUBPRE mode
447  * bits.  The HUBPRE bit needs to be set when MAX3421E operates at
448  * full speed, but it's talking to a low-speed device (i.e., through a
449  * hub).  Setting that bit ensures that every low-speed packet is
450  * preceded by a full-speed PRE PID.  Possible configurations:
451  *
452  * Hub speed:	Device speed:	=>	LOWSPEED bit:	HUBPRE bit:
453  *	FULL	FULL		=>	0		0
454  *	FULL	LOW		=>	1		1
455  *	LOW	LOW		=>	1		0
456  *	LOW	FULL		=>	1		0
457  */
458 static void
459 max3421_set_speed(struct usb_hcd *hcd, struct usb_device *dev)
460 {
461 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
462 	u8 mode_lowspeed, mode_hubpre, mode = max3421_hcd->mode;
463 
464 	mode_lowspeed = BIT(MAX3421_MODE_LOWSPEED_BIT);
465 	mode_hubpre   = BIT(MAX3421_MODE_HUBPRE_BIT);
466 	if (max3421_hcd->port_status & USB_PORT_STAT_LOW_SPEED) {
467 		mode |=  mode_lowspeed;
468 		mode &= ~mode_hubpre;
469 	} else if (dev->speed == USB_SPEED_LOW) {
470 		mode |= mode_lowspeed | mode_hubpre;
471 	} else {
472 		mode &= ~(mode_lowspeed | mode_hubpre);
473 	}
474 	if (mode != max3421_hcd->mode) {
475 		max3421_hcd->mode = mode;
476 		spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
477 	}
478 
479 }
480 
481 /*
482  * Caller must NOT hold HCD spinlock.
483  */
484 static void
485 max3421_set_address(struct usb_hcd *hcd, struct usb_device *dev, int epnum,
486 		    int force_toggles)
487 {
488 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
489 	int old_epnum, same_ep, rcvtog, sndtog;
490 	struct usb_device *old_dev;
491 	u8 hctl;
492 
493 	old_dev = max3421_hcd->loaded_dev;
494 	old_epnum = max3421_hcd->loaded_epnum;
495 
496 	same_ep = (dev == old_dev && epnum == old_epnum);
497 	if (same_ep && !force_toggles)
498 		return;
499 
500 	if (old_dev && !same_ep) {
501 		/* save the old end-points toggles: */
502 		u8 hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
503 
504 		rcvtog = (hrsl >> MAX3421_HRSL_RCVTOGRD_BIT) & 1;
505 		sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
506 
507 		/* no locking: HCD (i.e., we) own toggles, don't we? */
508 		usb_settoggle(old_dev, old_epnum, 0, rcvtog);
509 		usb_settoggle(old_dev, old_epnum, 1, sndtog);
510 	}
511 	/* setup new endpoint's toggle bits: */
512 	rcvtog = usb_gettoggle(dev, epnum, 0);
513 	sndtog = usb_gettoggle(dev, epnum, 1);
514 	hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) |
515 		BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
516 
517 	max3421_hcd->loaded_epnum = epnum;
518 	spi_wr8(hcd, MAX3421_REG_HCTL, hctl);
519 
520 	/*
521 	 * Note: devnum for one and the same device can change during
522 	 * address-assignment so it's best to just always load the
523 	 * address whenever the end-point changed/was forced.
524 	 */
525 	max3421_hcd->loaded_dev = dev;
526 	spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum);
527 }
528 
529 static int
530 max3421_ctrl_setup(struct usb_hcd *hcd, struct urb *urb)
531 {
532 	spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8);
533 	return MAX3421_HXFR_SETUP;
534 }
535 
536 static int
537 max3421_transfer_in(struct usb_hcd *hcd, struct urb *urb)
538 {
539 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
540 	int epnum = usb_pipeendpoint(urb->pipe);
541 
542 	max3421_hcd->curr_len = 0;
543 	max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT);
544 	return MAX3421_HXFR_BULK_IN(epnum);
545 }
546 
547 static int
548 max3421_transfer_out(struct usb_hcd *hcd, struct urb *urb, int fast_retransmit)
549 {
550 	struct spi_device *spi = to_spi_device(hcd->self.controller);
551 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
552 	int epnum = usb_pipeendpoint(urb->pipe);
553 	u32 max_packet;
554 	void *src;
555 
556 	src = urb->transfer_buffer + urb->actual_length;
557 
558 	if (fast_retransmit) {
559 		if (max3421_hcd->rev == 0x12) {
560 			/* work around rev 0x12 bug: */
561 			spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
562 			spi_wr8(hcd, MAX3421_REG_SNDFIFO, ((u8 *) src)[0]);
563 			spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
564 		}
565 		return MAX3421_HXFR_BULK_OUT(epnum);
566 	}
567 
568 	max_packet = usb_maxpacket(urb->dev, urb->pipe, 1);
569 
570 	if (max_packet > MAX3421_FIFO_SIZE) {
571 		/*
572 		 * We do not support isochronous transfers at this
573 		 * time.
574 		 */
575 		dev_err(&spi->dev,
576 			"%s: packet-size of %u too big (limit is %u bytes)",
577 			__func__, max_packet, MAX3421_FIFO_SIZE);
578 		max3421_hcd->urb_done = -EMSGSIZE;
579 		return -EMSGSIZE;
580 	}
581 	max3421_hcd->curr_len = min((urb->transfer_buffer_length -
582 				     urb->actual_length), max_packet);
583 
584 	spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len);
585 	spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
586 	return MAX3421_HXFR_BULK_OUT(epnum);
587 }
588 
589 /*
590  * Issue the next host-transfer command.
591  * Caller must NOT hold HCD spinlock.
592  */
593 static void
594 max3421_next_transfer(struct usb_hcd *hcd, int fast_retransmit)
595 {
596 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
597 	struct urb *urb = max3421_hcd->curr_urb;
598 	struct max3421_ep *max3421_ep;
599 	int cmd = -EINVAL;
600 
601 	if (!urb)
602 		return;	/* nothing to do */
603 
604 	max3421_ep = urb->ep->hcpriv;
605 
606 	switch (max3421_ep->pkt_state) {
607 	case PKT_STATE_SETUP:
608 		cmd = max3421_ctrl_setup(hcd, urb);
609 		break;
610 
611 	case PKT_STATE_TRANSFER:
612 		if (usb_urb_dir_in(urb))
613 			cmd = max3421_transfer_in(hcd, urb);
614 		else
615 			cmd = max3421_transfer_out(hcd, urb, fast_retransmit);
616 		break;
617 
618 	case PKT_STATE_TERMINATE:
619 		/*
620 		 * IN transfers are terminated with HS_OUT token,
621 		 * OUT transfers with HS_IN:
622 		 */
623 		if (usb_urb_dir_in(urb))
624 			cmd = MAX3421_HXFR_HS_OUT;
625 		else
626 			cmd = MAX3421_HXFR_HS_IN;
627 		break;
628 	}
629 
630 	if (cmd < 0)
631 		return;
632 
633 	/* issue the command and wait for host-xfer-done interrupt: */
634 
635 	spi_wr8(hcd, MAX3421_REG_HXFR, cmd);
636 	max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
637 }
638 
639 /*
640  * Find the next URB to process and start its execution.
641  *
642  * At this time, we do not anticipate ever connecting a USB hub to the
643  * MAX3421 chip, so at most USB device can be connected and we can use
644  * a simplistic scheduler: at the start of a frame, schedule all
645  * periodic transfers.  Once that is done, use the remainder of the
646  * frame to process non-periodic (bulk & control) transfers.
647  *
648  * Preconditions:
649  * o Caller must NOT hold HCD spinlock.
650  * o max3421_hcd->curr_urb MUST BE NULL.
651  * o MAX3421E chip must be idle.
652  */
653 static int
654 max3421_select_and_start_urb(struct usb_hcd *hcd)
655 {
656 	struct spi_device *spi = to_spi_device(hcd->self.controller);
657 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
658 	struct urb *urb, *curr_urb = NULL;
659 	struct max3421_ep *max3421_ep;
660 	int epnum, force_toggles = 0;
661 	struct usb_host_endpoint *ep;
662 	struct list_head *pos;
663 	unsigned long flags;
664 
665 	spin_lock_irqsave(&max3421_hcd->lock, flags);
666 
667 	for (;
668 	     max3421_hcd->sched_pass < SCHED_PASS_DONE;
669 	     ++max3421_hcd->sched_pass)
670 		list_for_each(pos, &max3421_hcd->ep_list) {
671 			urb = NULL;
672 			max3421_ep = container_of(pos, struct max3421_ep,
673 						  ep_list);
674 			ep = max3421_ep->ep;
675 
676 			switch (usb_endpoint_type(&ep->desc)) {
677 			case USB_ENDPOINT_XFER_ISOC:
678 			case USB_ENDPOINT_XFER_INT:
679 				if (max3421_hcd->sched_pass !=
680 				    SCHED_PASS_PERIODIC)
681 					continue;
682 				break;
683 
684 			case USB_ENDPOINT_XFER_CONTROL:
685 			case USB_ENDPOINT_XFER_BULK:
686 				if (max3421_hcd->sched_pass !=
687 				    SCHED_PASS_NON_PERIODIC)
688 					continue;
689 				break;
690 			}
691 
692 			if (list_empty(&ep->urb_list))
693 				continue;	/* nothing to do */
694 			urb = list_first_entry(&ep->urb_list, struct urb,
695 					       urb_list);
696 			if (urb->unlinked) {
697 				dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
698 					__func__, urb, urb->unlinked);
699 				max3421_hcd->curr_urb = urb;
700 				max3421_hcd->urb_done = 1;
701 				spin_unlock_irqrestore(&max3421_hcd->lock,
702 						       flags);
703 				return 1;
704 			}
705 
706 			switch (usb_endpoint_type(&ep->desc)) {
707 			case USB_ENDPOINT_XFER_CONTROL:
708 				/*
709 				 * Allow one control transaction per
710 				 * frame per endpoint:
711 				 */
712 				if (frame_diff(max3421_ep->last_active,
713 					       max3421_hcd->frame_number) == 0)
714 					continue;
715 				break;
716 
717 			case USB_ENDPOINT_XFER_BULK:
718 				if (max3421_ep->retransmit
719 				    && (frame_diff(max3421_ep->last_active,
720 						   max3421_hcd->frame_number)
721 					== 0))
722 					/*
723 					 * We already tried this EP
724 					 * during this frame and got a
725 					 * NAK or error; wait for next frame
726 					 */
727 					continue;
728 				break;
729 
730 			case USB_ENDPOINT_XFER_ISOC:
731 			case USB_ENDPOINT_XFER_INT:
732 				if (frame_diff(max3421_hcd->frame_number,
733 					       max3421_ep->last_active)
734 				    < urb->interval)
735 					/*
736 					 * We already processed this
737 					 * end-point in the current
738 					 * frame
739 					 */
740 					continue;
741 				break;
742 			}
743 
744 			/* move current ep to tail: */
745 			list_move_tail(pos, &max3421_hcd->ep_list);
746 			curr_urb = urb;
747 			goto done;
748 		}
749 done:
750 	if (!curr_urb) {
751 		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
752 		return 0;
753 	}
754 
755 	urb = max3421_hcd->curr_urb = curr_urb;
756 	epnum = usb_endpoint_num(&urb->ep->desc);
757 	if (max3421_ep->retransmit)
758 		/* restart (part of) a USB transaction: */
759 		max3421_ep->retransmit = 0;
760 	else {
761 		/* start USB transaction: */
762 		if (usb_endpoint_xfer_control(&ep->desc)) {
763 			/*
764 			 * See USB 2.0 spec section 8.6.1
765 			 * Initialization via SETUP Token:
766 			 */
767 			usb_settoggle(urb->dev, epnum, 0, 1);
768 			usb_settoggle(urb->dev, epnum, 1, 1);
769 			max3421_ep->pkt_state = PKT_STATE_SETUP;
770 			force_toggles = 1;
771 		} else
772 			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
773 	}
774 
775 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
776 
777 	max3421_ep->last_active = max3421_hcd->frame_number;
778 	max3421_set_address(hcd, urb->dev, epnum, force_toggles);
779 	max3421_set_speed(hcd, urb->dev);
780 	max3421_next_transfer(hcd, 0);
781 	return 1;
782 }
783 
784 /*
785  * Check all endpoints for URBs that got unlinked.
786  *
787  * Caller must NOT hold HCD spinlock.
788  */
789 static int
790 max3421_check_unlink(struct usb_hcd *hcd)
791 {
792 	struct spi_device *spi = to_spi_device(hcd->self.controller);
793 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
794 	struct list_head *pos, *upos, *next_upos;
795 	struct max3421_ep *max3421_ep;
796 	struct usb_host_endpoint *ep;
797 	struct urb *urb;
798 	unsigned long flags;
799 	int retval = 0;
800 
801 	spin_lock_irqsave(&max3421_hcd->lock, flags);
802 	list_for_each(pos, &max3421_hcd->ep_list) {
803 		max3421_ep = container_of(pos, struct max3421_ep, ep_list);
804 		ep = max3421_ep->ep;
805 		list_for_each_safe(upos, next_upos, &ep->urb_list) {
806 			urb = container_of(upos, struct urb, urb_list);
807 			if (urb->unlinked) {
808 				retval = 1;
809 				dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
810 					__func__, urb, urb->unlinked);
811 				usb_hcd_unlink_urb_from_ep(hcd, urb);
812 				spin_unlock_irqrestore(&max3421_hcd->lock,
813 						       flags);
814 				usb_hcd_giveback_urb(hcd, urb, 0);
815 				spin_lock_irqsave(&max3421_hcd->lock, flags);
816 			}
817 		}
818 	}
819 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
820 	return retval;
821 }
822 
823 /*
824  * Caller must NOT hold HCD spinlock.
825  */
826 static void
827 max3421_slow_retransmit(struct usb_hcd *hcd)
828 {
829 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
830 	struct urb *urb = max3421_hcd->curr_urb;
831 	struct max3421_ep *max3421_ep;
832 
833 	max3421_ep = urb->ep->hcpriv;
834 	max3421_ep->retransmit = 1;
835 	max3421_hcd->curr_urb = NULL;
836 }
837 
838 /*
839  * Caller must NOT hold HCD spinlock.
840  */
841 static void
842 max3421_recv_data_available(struct usb_hcd *hcd)
843 {
844 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
845 	struct urb *urb = max3421_hcd->curr_urb;
846 	size_t remaining, transfer_size;
847 	u8 rcvbc;
848 
849 	rcvbc = spi_rd8(hcd, MAX3421_REG_RCVBC);
850 
851 	if (rcvbc > MAX3421_FIFO_SIZE)
852 		rcvbc = MAX3421_FIFO_SIZE;
853 	if (urb->actual_length >= urb->transfer_buffer_length)
854 		remaining = 0;
855 	else
856 		remaining = urb->transfer_buffer_length - urb->actual_length;
857 	transfer_size = rcvbc;
858 	if (transfer_size > remaining)
859 		transfer_size = remaining;
860 	if (transfer_size > 0) {
861 		void *dst = urb->transfer_buffer + urb->actual_length;
862 
863 		spi_rd_buf(hcd, MAX3421_REG_RCVFIFO, dst, transfer_size);
864 		urb->actual_length += transfer_size;
865 		max3421_hcd->curr_len = transfer_size;
866 	}
867 
868 	/* ack the RCVDAV irq now that the FIFO has been read: */
869 	spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT));
870 }
871 
872 static void
873 max3421_handle_error(struct usb_hcd *hcd, u8 hrsl)
874 {
875 	struct spi_device *spi = to_spi_device(hcd->self.controller);
876 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
877 	u8 result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
878 	struct urb *urb = max3421_hcd->curr_urb;
879 	struct max3421_ep *max3421_ep = urb->ep->hcpriv;
880 	int switch_sndfifo;
881 
882 	/*
883 	 * If an OUT command results in any response other than OK
884 	 * (i.e., error or NAK), we have to perform a dummy-write to
885 	 * SNDBC so the FIFO gets switched back to us.  Otherwise, we
886 	 * get out of sync with the SNDFIFO double buffer.
887 	 */
888 	switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER &&
889 			  usb_urb_dir_out(urb));
890 
891 	switch (result_code) {
892 	case MAX3421_HRSL_OK:
893 		return;			/* this shouldn't happen */
894 
895 	case MAX3421_HRSL_WRONGPID:	/* received wrong PID */
896 	case MAX3421_HRSL_BUSY:		/* SIE busy */
897 	case MAX3421_HRSL_BADREQ:	/* bad val in HXFR */
898 	case MAX3421_HRSL_UNDEF:	/* reserved */
899 	case MAX3421_HRSL_KERR:		/* K-state instead of response */
900 	case MAX3421_HRSL_JERR:		/* J-state instead of response */
901 		/*
902 		 * packet experienced an error that we cannot recover
903 		 * from; report error
904 		 */
905 		max3421_hcd->urb_done = hrsl_to_error[result_code];
906 		dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
907 			__func__, hrsl);
908 		break;
909 
910 	case MAX3421_HRSL_TOGERR:
911 		if (usb_urb_dir_in(urb))
912 			; /* don't do anything (device will switch toggle) */
913 		else {
914 			/* flip the send toggle bit: */
915 			int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
916 
917 			sndtog ^= 1;
918 			spi_wr8(hcd, MAX3421_REG_HCTL,
919 				BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
920 		}
921 		/* FALL THROUGH */
922 	case MAX3421_HRSL_BADBC:	/* bad byte count */
923 	case MAX3421_HRSL_PIDERR:	/* received PID is corrupted */
924 	case MAX3421_HRSL_PKTERR:	/* packet error (stuff, EOP) */
925 	case MAX3421_HRSL_CRCERR:	/* CRC error */
926 	case MAX3421_HRSL_BABBLE:	/* device talked too long */
927 	case MAX3421_HRSL_TIMEOUT:
928 		if (max3421_ep->retries++ < USB_MAX_RETRIES)
929 			/* retry the packet again in the next frame */
930 			max3421_slow_retransmit(hcd);
931 		else {
932 			/* Based on ohci.h cc_to_err[]: */
933 			max3421_hcd->urb_done = hrsl_to_error[result_code];
934 			dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
935 				__func__, hrsl);
936 		}
937 		break;
938 
939 	case MAX3421_HRSL_STALL:
940 		dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
941 			__func__, hrsl);
942 		max3421_hcd->urb_done = hrsl_to_error[result_code];
943 		break;
944 
945 	case MAX3421_HRSL_NAK:
946 		/*
947 		 * Device wasn't ready for data or has no data
948 		 * available: retry the packet again.
949 		 */
950 		if (max3421_ep->naks++ < NAK_MAX_FAST_RETRANSMITS) {
951 			max3421_next_transfer(hcd, 1);
952 			switch_sndfifo = 0;
953 		} else
954 			max3421_slow_retransmit(hcd);
955 		break;
956 	}
957 	if (switch_sndfifo)
958 		spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
959 }
960 
961 /*
962  * Caller must NOT hold HCD spinlock.
963  */
964 static int
965 max3421_transfer_in_done(struct usb_hcd *hcd, struct urb *urb)
966 {
967 	struct spi_device *spi = to_spi_device(hcd->self.controller);
968 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
969 	u32 max_packet;
970 
971 	if (urb->actual_length >= urb->transfer_buffer_length)
972 		return 1;	/* read is complete, so we're done */
973 
974 	/*
975 	 * USB 2.0 Section 5.3.2 Pipes: packets must be full size
976 	 * except for last one.
977 	 */
978 	max_packet = usb_maxpacket(urb->dev, urb->pipe, 0);
979 	if (max_packet > MAX3421_FIFO_SIZE) {
980 		/*
981 		 * We do not support isochronous transfers at this
982 		 * time...
983 		 */
984 		dev_err(&spi->dev,
985 			"%s: packet-size of %u too big (limit is %u bytes)",
986 			__func__, max_packet, MAX3421_FIFO_SIZE);
987 		return -EINVAL;
988 	}
989 
990 	if (max3421_hcd->curr_len < max_packet) {
991 		if (urb->transfer_flags & URB_SHORT_NOT_OK) {
992 			/*
993 			 * remaining > 0 and received an
994 			 * unexpected partial packet ->
995 			 * error
996 			 */
997 			return -EREMOTEIO;
998 		} else
999 			/* short read, but it's OK */
1000 			return 1;
1001 	}
1002 	return 0;	/* not done */
1003 }
1004 
1005 /*
1006  * Caller must NOT hold HCD spinlock.
1007  */
1008 static int
1009 max3421_transfer_out_done(struct usb_hcd *hcd, struct urb *urb)
1010 {
1011 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1012 
1013 	urb->actual_length += max3421_hcd->curr_len;
1014 	if (urb->actual_length < urb->transfer_buffer_length)
1015 		return 0;
1016 	if (urb->transfer_flags & URB_ZERO_PACKET) {
1017 		/*
1018 		 * Some hardware needs a zero-size packet at the end
1019 		 * of a bulk-out transfer if the last transfer was a
1020 		 * full-sized packet (i.e., such hardware use <
1021 		 * max_packet as an indicator that the end of the
1022 		 * packet has been reached).
1023 		 */
1024 		u32 max_packet = usb_maxpacket(urb->dev, urb->pipe, 1);
1025 
1026 		if (max3421_hcd->curr_len == max_packet)
1027 			return 0;
1028 	}
1029 	return 1;
1030 }
1031 
1032 /*
1033  * Caller must NOT hold HCD spinlock.
1034  */
1035 static void
1036 max3421_host_transfer_done(struct usb_hcd *hcd)
1037 {
1038 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1039 	struct urb *urb = max3421_hcd->curr_urb;
1040 	struct max3421_ep *max3421_ep;
1041 	u8 result_code, hrsl;
1042 	int urb_done = 0;
1043 
1044 	max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) |
1045 			       BIT(MAX3421_HI_RCVDAV_BIT));
1046 
1047 	hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1048 	result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
1049 
1050 #ifdef DEBUG
1051 	++max3421_hcd->err_stat[result_code];
1052 #endif
1053 
1054 	max3421_ep = urb->ep->hcpriv;
1055 
1056 	if (unlikely(result_code != MAX3421_HRSL_OK)) {
1057 		max3421_handle_error(hcd, hrsl);
1058 		return;
1059 	}
1060 
1061 	max3421_ep->naks = 0;
1062 	max3421_ep->retries = 0;
1063 	switch (max3421_ep->pkt_state) {
1064 
1065 	case PKT_STATE_SETUP:
1066 		if (urb->transfer_buffer_length > 0)
1067 			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
1068 		else
1069 			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1070 		break;
1071 
1072 	case PKT_STATE_TRANSFER:
1073 		if (usb_urb_dir_in(urb))
1074 			urb_done = max3421_transfer_in_done(hcd, urb);
1075 		else
1076 			urb_done = max3421_transfer_out_done(hcd, urb);
1077 		if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1078 			/*
1079 			 * We aren't really done - we still need to
1080 			 * terminate the control transfer:
1081 			 */
1082 			max3421_hcd->urb_done = urb_done = 0;
1083 			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1084 		}
1085 		break;
1086 
1087 	case PKT_STATE_TERMINATE:
1088 		urb_done = 1;
1089 		break;
1090 	}
1091 
1092 	if (urb_done)
1093 		max3421_hcd->urb_done = urb_done;
1094 	else
1095 		max3421_next_transfer(hcd, 0);
1096 }
1097 
1098 /*
1099  * Caller must NOT hold HCD spinlock.
1100  */
1101 static void
1102 max3421_detect_conn(struct usb_hcd *hcd)
1103 {
1104 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1105 	unsigned int jk, have_conn = 0;
1106 	u32 old_port_status, chg;
1107 	unsigned long flags;
1108 	u8 hrsl, mode;
1109 
1110 	hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1111 
1112 	jk = ((((hrsl >> MAX3421_HRSL_JSTATUS_BIT) & 1) << 0) |
1113 	      (((hrsl >> MAX3421_HRSL_KSTATUS_BIT) & 1) << 1));
1114 
1115 	mode = max3421_hcd->mode;
1116 
1117 	switch (jk) {
1118 	case 0x0: /* SE0: disconnect */
1119 		/*
1120 		 * Turn off SOFKAENAB bit to avoid getting interrupt
1121 		 * every milli-second:
1122 		 */
1123 		mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
1124 		break;
1125 
1126 	case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */
1127 	case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */
1128 		if (jk == 0x2)
1129 			/* need to switch to the other speed: */
1130 			mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
1131 		/* turn on SOFKAENAB bit: */
1132 		mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
1133 		have_conn = 1;
1134 		break;
1135 
1136 	case 0x3: /* illegal */
1137 		break;
1138 	}
1139 
1140 	max3421_hcd->mode = mode;
1141 	spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1142 
1143 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1144 	old_port_status = max3421_hcd->port_status;
1145 	if (have_conn)
1146 		max3421_hcd->port_status |=  USB_PORT_STAT_CONNECTION;
1147 	else
1148 		max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
1149 	if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
1150 		max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
1151 	else
1152 		max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
1153 	chg = (old_port_status ^ max3421_hcd->port_status);
1154 	max3421_hcd->port_status |= chg << 16;
1155 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1156 }
1157 
1158 static irqreturn_t
1159 max3421_irq_handler(int irq, void *dev_id)
1160 {
1161 	struct usb_hcd *hcd = dev_id;
1162 	struct spi_device *spi = to_spi_device(hcd->self.controller);
1163 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1164 
1165 	if (max3421_hcd->spi_thread &&
1166 	    max3421_hcd->spi_thread->state != TASK_RUNNING)
1167 		wake_up_process(max3421_hcd->spi_thread);
1168 	if (!max3421_hcd->do_enable_irq) {
1169 		max3421_hcd->do_enable_irq = 1;
1170 		disable_irq_nosync(spi->irq);
1171 	}
1172 	return IRQ_HANDLED;
1173 }
1174 
1175 #ifdef DEBUG
1176 
1177 static void
1178 dump_eps(struct usb_hcd *hcd)
1179 {
1180 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1181 	struct max3421_ep *max3421_ep;
1182 	struct usb_host_endpoint *ep;
1183 	struct list_head *pos, *upos;
1184 	char ubuf[512], *dp, *end;
1185 	unsigned long flags;
1186 	struct urb *urb;
1187 	int epnum, ret;
1188 
1189 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1190 	list_for_each(pos, &max3421_hcd->ep_list) {
1191 		max3421_ep = container_of(pos, struct max3421_ep, ep_list);
1192 		ep = max3421_ep->ep;
1193 
1194 		dp = ubuf;
1195 		end = dp + sizeof(ubuf);
1196 		*dp = '\0';
1197 		list_for_each(upos, &ep->urb_list) {
1198 			urb = container_of(upos, struct urb, urb_list);
1199 			ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1200 				       usb_pipetype(urb->pipe),
1201 				       usb_urb_dir_in(urb) ? "IN" : "OUT",
1202 				       urb->actual_length,
1203 				       urb->transfer_buffer_length);
1204 			if (ret < 0 || ret >= end - dp)
1205 				break;	/* error or buffer full */
1206 			dp += ret;
1207 		}
1208 
1209 		epnum = usb_endpoint_num(&ep->desc);
1210 		pr_info("EP%0u %u lst %04u rtr %u nak %6u rxmt %u: %s\n",
1211 			epnum, max3421_ep->pkt_state, max3421_ep->last_active,
1212 			max3421_ep->retries, max3421_ep->naks,
1213 			max3421_ep->retransmit, ubuf);
1214 	}
1215 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1216 }
1217 
1218 #endif /* DEBUG */
1219 
1220 /* Return zero if no work was performed, 1 otherwise.  */
1221 static int
1222 max3421_handle_irqs(struct usb_hcd *hcd)
1223 {
1224 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1225 	u32 chg, old_port_status;
1226 	unsigned long flags;
1227 	u8 hirq;
1228 
1229 	/*
1230 	 * Read and ack pending interrupts (CPU must never
1231 	 * clear SNDBAV directly and RCVDAV must be cleared by
1232 	 * max3421_recv_data_available()!):
1233 	 */
1234 	hirq = spi_rd8(hcd, MAX3421_REG_HIRQ);
1235 	hirq &= max3421_hcd->hien;
1236 	if (!hirq)
1237 		return 0;
1238 
1239 	spi_wr8(hcd, MAX3421_REG_HIRQ,
1240 		hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
1241 			 BIT(MAX3421_HI_RCVDAV_BIT)));
1242 
1243 	if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
1244 		max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
1245 					     & USB_MAX_FRAME_NUMBER);
1246 		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1247 	}
1248 
1249 	if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
1250 		max3421_recv_data_available(hcd);
1251 
1252 	if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
1253 		max3421_host_transfer_done(hcd);
1254 
1255 	if (hirq & BIT(MAX3421_HI_CONDET_BIT))
1256 		max3421_detect_conn(hcd);
1257 
1258 	/*
1259 	 * Now process interrupts that may affect HCD state
1260 	 * other than the end-points:
1261 	 */
1262 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1263 
1264 	old_port_status = max3421_hcd->port_status;
1265 	if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
1266 		if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
1267 			/* BUSEVENT due to completion of Bus Reset */
1268 			max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
1269 			max3421_hcd->port_status |=  USB_PORT_STAT_ENABLE;
1270 		} else {
1271 			/* BUSEVENT due to completion of Bus Resume */
1272 			pr_info("%s: BUSEVENT Bus Resume Done\n", __func__);
1273 		}
1274 	}
1275 	if (hirq & BIT(MAX3421_HI_RWU_BIT))
1276 		pr_info("%s: RWU\n", __func__);
1277 	if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
1278 		pr_info("%s: SUSDN\n", __func__);
1279 
1280 	chg = (old_port_status ^ max3421_hcd->port_status);
1281 	max3421_hcd->port_status |= chg << 16;
1282 
1283 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1284 
1285 #ifdef DEBUG
1286 	{
1287 		static unsigned long last_time;
1288 		char sbuf[16 * 16], *dp, *end;
1289 		int i;
1290 
1291 		if (jiffies - last_time > 5*HZ) {
1292 			dp = sbuf;
1293 			end = sbuf + sizeof(sbuf);
1294 			*dp = '\0';
1295 			for (i = 0; i < 16; ++i) {
1296 				int ret = snprintf(dp, end - dp, " %lu",
1297 						   max3421_hcd->err_stat[i]);
1298 				if (ret < 0 || ret >= end - dp)
1299 					break;	/* error or buffer full */
1300 				dp += ret;
1301 			}
1302 			pr_info("%s: hrsl_stats %s\n", __func__, sbuf);
1303 			memset(max3421_hcd->err_stat, 0,
1304 			       sizeof(max3421_hcd->err_stat));
1305 			last_time = jiffies;
1306 
1307 			dump_eps(hcd);
1308 		}
1309 	}
1310 #endif
1311 	return 1;
1312 }
1313 
1314 static int
1315 max3421_reset_hcd(struct usb_hcd *hcd)
1316 {
1317 	struct spi_device *spi = to_spi_device(hcd->self.controller);
1318 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1319 	int timeout;
1320 
1321 	/* perform a chip reset and wait for OSCIRQ signal to appear: */
1322 	spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT));
1323 	/* clear reset: */
1324 	spi_wr8(hcd, MAX3421_REG_USBCTL, 0);
1325 	timeout = 1000;
1326 	while (1) {
1327 		if (spi_rd8(hcd, MAX3421_REG_USBIRQ)
1328 		    & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
1329 			break;
1330 		if (--timeout < 0) {
1331 			dev_err(&spi->dev,
1332 				"timed out waiting for oscillator OK signal");
1333 			return 1;
1334 		}
1335 		cond_resched();
1336 	}
1337 
1338 	/*
1339 	 * Turn on host mode, automatic generation of SOF packets, and
1340 	 * enable pull-down registers on DM/DP:
1341 	 */
1342 	max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) |
1343 			     BIT(MAX3421_MODE_SOFKAENAB_BIT) |
1344 			     BIT(MAX3421_MODE_DMPULLDN_BIT) |
1345 			     BIT(MAX3421_MODE_DPPULLDN_BIT));
1346 	spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1347 
1348 	/* reset frame-number: */
1349 	max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER;
1350 	spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT));
1351 
1352 	/* sample the state of the D+ and D- lines */
1353 	spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT));
1354 	max3421_detect_conn(hcd);
1355 
1356 	/* enable frame, connection-detected, and bus-event interrupts: */
1357 	max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
1358 			     BIT(MAX3421_HI_CONDET_BIT) |
1359 			     BIT(MAX3421_HI_BUSEVENT_BIT));
1360 	spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1361 
1362 	/* enable interrupts: */
1363 	spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT));
1364 	return 1;
1365 }
1366 
1367 static int
1368 max3421_urb_done(struct usb_hcd *hcd)
1369 {
1370 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1371 	unsigned long flags;
1372 	struct urb *urb;
1373 	int status;
1374 
1375 	status = max3421_hcd->urb_done;
1376 	max3421_hcd->urb_done = 0;
1377 	if (status > 0)
1378 		status = 0;
1379 	urb = max3421_hcd->curr_urb;
1380 	if (urb) {
1381 		max3421_hcd->curr_urb = NULL;
1382 		spin_lock_irqsave(&max3421_hcd->lock, flags);
1383 		usb_hcd_unlink_urb_from_ep(hcd, urb);
1384 		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1385 
1386 		/* must be called without the HCD spinlock: */
1387 		usb_hcd_giveback_urb(hcd, urb, status);
1388 	}
1389 	return 1;
1390 }
1391 
1392 static int
1393 max3421_spi_thread(void *dev_id)
1394 {
1395 	struct usb_hcd *hcd = dev_id;
1396 	struct spi_device *spi = to_spi_device(hcd->self.controller);
1397 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1398 	int i, i_worked = 1;
1399 
1400 	/* set full-duplex SPI mode, low-active interrupt pin: */
1401 	spi_wr8(hcd, MAX3421_REG_PINCTL,
1402 		(BIT(MAX3421_PINCTL_FDUPSPI_BIT) |	/* full-duplex */
1403 		 BIT(MAX3421_PINCTL_INTLEVEL_BIT)));	/* low-active irq */
1404 
1405 	while (!kthread_should_stop()) {
1406 		max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION);
1407 		if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13)
1408 			break;
1409 		dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev);
1410 		msleep(10000);
1411 	}
1412 	dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
1413 		 max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word,
1414 		 spi->irq);
1415 
1416 	while (!kthread_should_stop()) {
1417 		if (!i_worked) {
1418 			/*
1419 			 * We'll be waiting for wakeups from the hard
1420 			 * interrupt handler, so now is a good time to
1421 			 * sync our hien with the chip:
1422 			 */
1423 			spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1424 
1425 			set_current_state(TASK_INTERRUPTIBLE);
1426 			if (max3421_hcd->do_enable_irq) {
1427 				max3421_hcd->do_enable_irq = 0;
1428 				enable_irq(spi->irq);
1429 			}
1430 			schedule();
1431 			__set_current_state(TASK_RUNNING);
1432 		}
1433 
1434 		i_worked = 0;
1435 
1436 		if (max3421_hcd->urb_done)
1437 			i_worked |= max3421_urb_done(hcd);
1438 		else if (max3421_handle_irqs(hcd))
1439 			i_worked = 1;
1440 		else if (!max3421_hcd->curr_urb)
1441 			i_worked |= max3421_select_and_start_urb(hcd);
1442 
1443 		if (max3421_hcd->do_reset_hcd) {
1444 			/* reset the HCD: */
1445 			max3421_hcd->do_reset_hcd = 0;
1446 			i_worked |= max3421_reset_hcd(hcd);
1447 		}
1448 		if (max3421_hcd->do_reset_port) {
1449 			/* perform a USB bus reset: */
1450 			max3421_hcd->do_reset_port = 0;
1451 			spi_wr8(hcd, MAX3421_REG_HCTL,
1452 				BIT(MAX3421_HCTL_BUSRST_BIT));
1453 			i_worked = 1;
1454 		}
1455 		if (max3421_hcd->do_check_unlink) {
1456 			max3421_hcd->do_check_unlink = 0;
1457 			i_worked |= max3421_check_unlink(hcd);
1458 		}
1459 		if (max3421_hcd->do_iopin_update) {
1460 			/*
1461 			 * IOPINS1/IOPINS2 do not auto-increment, so we can't
1462 			 * use spi_wr_buf().
1463 			 */
1464 			for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) {
1465 				u8 val = spi_rd8(hcd, MAX3421_REG_IOPINS1);
1466 
1467 				val = ((val & 0xf0) |
1468 				       (max3421_hcd->iopins[i] & 0x0f));
1469 				spi_wr8(hcd, MAX3421_REG_IOPINS1 + i, val);
1470 				max3421_hcd->iopins[i] = val;
1471 			}
1472 			max3421_hcd->do_iopin_update = 0;
1473 			i_worked = 1;
1474 		}
1475 	}
1476 	set_current_state(TASK_RUNNING);
1477 	dev_info(&spi->dev, "SPI thread exiting");
1478 	return 0;
1479 }
1480 
1481 static int
1482 max3421_reset_port(struct usb_hcd *hcd)
1483 {
1484 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1485 
1486 	max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
1487 				      USB_PORT_STAT_LOW_SPEED);
1488 	max3421_hcd->do_reset_port = 1;
1489 	wake_up_process(max3421_hcd->spi_thread);
1490 	return 0;
1491 }
1492 
1493 static int
1494 max3421_reset(struct usb_hcd *hcd)
1495 {
1496 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1497 
1498 	hcd->self.sg_tablesize = 0;
1499 	hcd->speed = HCD_USB2;
1500 	hcd->self.root_hub->speed = USB_SPEED_FULL;
1501 	max3421_hcd->do_reset_hcd = 1;
1502 	wake_up_process(max3421_hcd->spi_thread);
1503 	return 0;
1504 }
1505 
1506 static int
1507 max3421_start(struct usb_hcd *hcd)
1508 {
1509 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1510 
1511 	spin_lock_init(&max3421_hcd->lock);
1512 	max3421_hcd->rh_state = MAX3421_RH_RUNNING;
1513 
1514 	INIT_LIST_HEAD(&max3421_hcd->ep_list);
1515 
1516 	hcd->power_budget = POWER_BUDGET;
1517 	hcd->state = HC_STATE_RUNNING;
1518 	hcd->uses_new_polling = 1;
1519 	return 0;
1520 }
1521 
1522 static void
1523 max3421_stop(struct usb_hcd *hcd)
1524 {
1525 }
1526 
1527 static int
1528 max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1529 {
1530 	struct spi_device *spi = to_spi_device(hcd->self.controller);
1531 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1532 	struct max3421_ep *max3421_ep;
1533 	unsigned long flags;
1534 	int retval;
1535 
1536 	switch (usb_pipetype(urb->pipe)) {
1537 	case PIPE_INTERRUPT:
1538 	case PIPE_ISOCHRONOUS:
1539 		if (urb->interval < 0) {
1540 			dev_err(&spi->dev,
1541 			  "%s: interval=%d for intr-/iso-pipe; expected > 0\n",
1542 				__func__, urb->interval);
1543 			return -EINVAL;
1544 		}
1545 	default:
1546 		break;
1547 	}
1548 
1549 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1550 
1551 	max3421_ep = urb->ep->hcpriv;
1552 	if (!max3421_ep) {
1553 		/* gets freed in max3421_endpoint_disable: */
1554 		max3421_ep = kzalloc(sizeof(struct max3421_ep), mem_flags);
1555 		if (!max3421_ep) {
1556 			retval = -ENOMEM;
1557 			goto out;
1558 		}
1559 		max3421_ep->ep = urb->ep;
1560 		max3421_ep->last_active = max3421_hcd->frame_number;
1561 		urb->ep->hcpriv = max3421_ep;
1562 
1563 		list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
1564 	}
1565 
1566 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
1567 	if (retval == 0) {
1568 		/* Since we added to the queue, restart scheduling: */
1569 		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1570 		wake_up_process(max3421_hcd->spi_thread);
1571 	}
1572 
1573 out:
1574 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1575 	return retval;
1576 }
1577 
1578 static int
1579 max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1580 {
1581 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1582 	unsigned long flags;
1583 	int retval;
1584 
1585 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1586 
1587 	/*
1588 	 * This will set urb->unlinked which in turn causes the entry
1589 	 * to be dropped at the next opportunity.
1590 	 */
1591 	retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1592 	if (retval == 0) {
1593 		max3421_hcd->do_check_unlink = 1;
1594 		wake_up_process(max3421_hcd->spi_thread);
1595 	}
1596 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1597 	return retval;
1598 }
1599 
1600 static void
1601 max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1602 {
1603 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1604 	unsigned long flags;
1605 
1606 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1607 
1608 	if (ep->hcpriv) {
1609 		struct max3421_ep *max3421_ep = ep->hcpriv;
1610 
1611 		/* remove myself from the ep_list: */
1612 		if (!list_empty(&max3421_ep->ep_list))
1613 			list_del(&max3421_ep->ep_list);
1614 		kfree(max3421_ep);
1615 		ep->hcpriv = NULL;
1616 	}
1617 
1618 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1619 }
1620 
1621 static int
1622 max3421_get_frame_number(struct usb_hcd *hcd)
1623 {
1624 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1625 	return max3421_hcd->frame_number;
1626 }
1627 
1628 /*
1629  * Should return a non-zero value when any port is undergoing a resume
1630  * transition while the root hub is suspended.
1631  */
1632 static int
1633 max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
1634 {
1635 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1636 	unsigned long flags;
1637 	int retval = 0;
1638 
1639 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1640 	if (!HCD_HW_ACCESSIBLE(hcd))
1641 		goto done;
1642 
1643 	*buf = 0;
1644 	if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
1645 		*buf = (1 << 1); /* a hub over-current condition exists */
1646 		dev_dbg(hcd->self.controller,
1647 			"port status 0x%08x has changes\n",
1648 			max3421_hcd->port_status);
1649 		retval = 1;
1650 		if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
1651 			usb_hcd_resume_root_hub(hcd);
1652 	}
1653 done:
1654 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1655 	return retval;
1656 }
1657 
1658 static inline void
1659 hub_descriptor(struct usb_hub_descriptor *desc)
1660 {
1661 	memset(desc, 0, sizeof(*desc));
1662 	/*
1663 	 * See Table 11-13: Hub Descriptor in USB 2.0 spec.
1664 	 */
1665 	desc->bDescriptorType = 0x29;	/* hub descriptor */
1666 	desc->bDescLength = 9;
1667 	desc->wHubCharacteristics = cpu_to_le16(0x0001);
1668 	desc->bNbrPorts = 1;
1669 }
1670 
1671 /*
1672  * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1673  * VALUE (0 or 1).  PIN_NUMBER may be in the range from 1-8.  For
1674  * any other value, this function acts as a no-op.
1675  */
1676 static void
1677 max3421_gpout_set_value(struct usb_hcd *hcd, u8 pin_number, u8 value)
1678 {
1679 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1680 	u8 mask, idx;
1681 
1682 	--pin_number;
1683 	if (pin_number > 7)
1684 		return;
1685 
1686 	mask = 1u << pin_number;
1687 	idx = pin_number / 4;
1688 
1689 	if (value)
1690 		max3421_hcd->iopins[idx] |=  mask;
1691 	else
1692 		max3421_hcd->iopins[idx] &= ~mask;
1693 	max3421_hcd->do_iopin_update = 1;
1694 	wake_up_process(max3421_hcd->spi_thread);
1695 }
1696 
1697 static int
1698 max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
1699 		    char *buf, u16 length)
1700 {
1701 	struct spi_device *spi = to_spi_device(hcd->self.controller);
1702 	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1703 	struct max3421_hcd_platform_data *pdata;
1704 	unsigned long flags;
1705 	int retval = 0;
1706 
1707 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1708 
1709 	pdata = spi->dev.platform_data;
1710 
1711 	switch (type_req) {
1712 	case ClearHubFeature:
1713 		break;
1714 	case ClearPortFeature:
1715 		switch (value) {
1716 		case USB_PORT_FEAT_SUSPEND:
1717 			break;
1718 		case USB_PORT_FEAT_POWER:
1719 			dev_dbg(hcd->self.controller, "power-off\n");
1720 			max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1721 						!pdata->vbus_active_level);
1722 			/* FALLS THROUGH */
1723 		default:
1724 			max3421_hcd->port_status &= ~(1 << value);
1725 		}
1726 		break;
1727 	case GetHubDescriptor:
1728 		hub_descriptor((struct usb_hub_descriptor *) buf);
1729 		break;
1730 
1731 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1732 	case GetPortErrorCount:
1733 	case SetHubDepth:
1734 		/* USB3 only */
1735 		goto error;
1736 
1737 	case GetHubStatus:
1738 		*(__le32 *) buf = cpu_to_le32(0);
1739 		break;
1740 
1741 	case GetPortStatus:
1742 		if (index != 1) {
1743 			retval = -EPIPE;
1744 			goto error;
1745 		}
1746 		((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
1747 		((__le16 *) buf)[1] =
1748 			cpu_to_le16(max3421_hcd->port_status >> 16);
1749 		break;
1750 
1751 	case SetHubFeature:
1752 		retval = -EPIPE;
1753 		break;
1754 
1755 	case SetPortFeature:
1756 		switch (value) {
1757 		case USB_PORT_FEAT_LINK_STATE:
1758 		case USB_PORT_FEAT_U1_TIMEOUT:
1759 		case USB_PORT_FEAT_U2_TIMEOUT:
1760 		case USB_PORT_FEAT_BH_PORT_RESET:
1761 			goto error;
1762 		case USB_PORT_FEAT_SUSPEND:
1763 			if (max3421_hcd->active)
1764 				max3421_hcd->port_status |=
1765 					USB_PORT_STAT_SUSPEND;
1766 			break;
1767 		case USB_PORT_FEAT_POWER:
1768 			dev_dbg(hcd->self.controller, "power-on\n");
1769 			max3421_hcd->port_status |= USB_PORT_STAT_POWER;
1770 			max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1771 						pdata->vbus_active_level);
1772 			break;
1773 		case USB_PORT_FEAT_RESET:
1774 			max3421_reset_port(hcd);
1775 			/* FALLS THROUGH */
1776 		default:
1777 			if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
1778 			    != 0)
1779 				max3421_hcd->port_status |= (1 << value);
1780 		}
1781 		break;
1782 
1783 	default:
1784 		dev_dbg(hcd->self.controller,
1785 			"hub control req%04x v%04x i%04x l%d\n",
1786 			type_req, value, index, length);
1787 error:		/* "protocol stall" on error */
1788 		retval = -EPIPE;
1789 	}
1790 
1791 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1792 	return retval;
1793 }
1794 
1795 static int
1796 max3421_bus_suspend(struct usb_hcd *hcd)
1797 {
1798 	return -1;
1799 }
1800 
1801 static int
1802 max3421_bus_resume(struct usb_hcd *hcd)
1803 {
1804 	return -1;
1805 }
1806 
1807 /*
1808  * The SPI driver already takes care of DMA-mapping/unmapping, so no
1809  * reason to do it twice.
1810  */
1811 static int
1812 max3421_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1813 {
1814 	return 0;
1815 }
1816 
1817 static void
1818 max3421_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1819 {
1820 }
1821 
1822 static struct hc_driver max3421_hcd_desc = {
1823 	.description =		"max3421",
1824 	.product_desc =		DRIVER_DESC,
1825 	.hcd_priv_size =	sizeof(struct max3421_hcd),
1826 	.flags =		HCD_USB11,
1827 	.reset =		max3421_reset,
1828 	.start =		max3421_start,
1829 	.stop =			max3421_stop,
1830 	.get_frame_number =	max3421_get_frame_number,
1831 	.urb_enqueue =		max3421_urb_enqueue,
1832 	.urb_dequeue =		max3421_urb_dequeue,
1833 	.map_urb_for_dma =	max3421_map_urb_for_dma,
1834 	.unmap_urb_for_dma =	max3421_unmap_urb_for_dma,
1835 	.endpoint_disable =	max3421_endpoint_disable,
1836 	.hub_status_data =	max3421_hub_status_data,
1837 	.hub_control =		max3421_hub_control,
1838 	.bus_suspend =		max3421_bus_suspend,
1839 	.bus_resume =		max3421_bus_resume,
1840 };
1841 
1842 static int
1843 max3421_probe(struct spi_device *spi)
1844 {
1845 	struct max3421_hcd *max3421_hcd;
1846 	struct usb_hcd *hcd = NULL;
1847 	int retval = -ENOMEM;
1848 
1849 	if (spi_setup(spi) < 0) {
1850 		dev_err(&spi->dev, "Unable to setup SPI bus");
1851 		return -EFAULT;
1852 	}
1853 
1854 	hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
1855 			     dev_name(&spi->dev));
1856 	if (!hcd) {
1857 		dev_err(&spi->dev, "failed to create HCD structure\n");
1858 		goto error;
1859 	}
1860 	set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1861 	max3421_hcd = hcd_to_max3421(hcd);
1862 	max3421_hcd->next = max3421_hcd_list;
1863 	max3421_hcd_list = max3421_hcd;
1864 	INIT_LIST_HEAD(&max3421_hcd->ep_list);
1865 
1866 	max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL);
1867 	if (!max3421_hcd->tx) {
1868 		dev_err(&spi->dev, "failed to kmalloc tx buffer\n");
1869 		goto error;
1870 	}
1871 	max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL);
1872 	if (!max3421_hcd->rx) {
1873 		dev_err(&spi->dev, "failed to kmalloc rx buffer\n");
1874 		goto error;
1875 	}
1876 
1877 	max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
1878 					      "max3421_spi_thread");
1879 	if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
1880 		dev_err(&spi->dev,
1881 			"failed to create SPI thread (out of memory)\n");
1882 		goto error;
1883 	}
1884 
1885 	retval = usb_add_hcd(hcd, 0, 0);
1886 	if (retval) {
1887 		dev_err(&spi->dev, "failed to add HCD\n");
1888 		goto error;
1889 	}
1890 
1891 	retval = request_irq(spi->irq, max3421_irq_handler,
1892 			     IRQF_TRIGGER_LOW, "max3421", hcd);
1893 	if (retval < 0) {
1894 		dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
1895 		goto error;
1896 	}
1897 	return 0;
1898 
1899 error:
1900 	if (hcd) {
1901 		kfree(max3421_hcd->tx);
1902 		kfree(max3421_hcd->rx);
1903 		if (max3421_hcd->spi_thread)
1904 			kthread_stop(max3421_hcd->spi_thread);
1905 		usb_put_hcd(hcd);
1906 	}
1907 	return retval;
1908 }
1909 
1910 static int
1911 max3421_remove(struct spi_device *spi)
1912 {
1913 	struct max3421_hcd *max3421_hcd = NULL, **prev;
1914 	struct usb_hcd *hcd = NULL;
1915 	unsigned long flags;
1916 
1917 	for (prev = &max3421_hcd_list; *prev; prev = &(*prev)->next) {
1918 		max3421_hcd = *prev;
1919 		hcd = max3421_to_hcd(max3421_hcd);
1920 		if (hcd->self.controller == &spi->dev)
1921 			break;
1922 	}
1923 	if (!max3421_hcd) {
1924 		dev_err(&spi->dev, "no MAX3421 HCD found for SPI device %p\n",
1925 			spi);
1926 		return -ENODEV;
1927 	}
1928 
1929 	usb_remove_hcd(hcd);
1930 
1931 	spin_lock_irqsave(&max3421_hcd->lock, flags);
1932 
1933 	kthread_stop(max3421_hcd->spi_thread);
1934 	*prev = max3421_hcd->next;
1935 
1936 	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1937 
1938 	free_irq(spi->irq, hcd);
1939 
1940 	usb_put_hcd(hcd);
1941 	return 0;
1942 }
1943 
1944 static struct spi_driver max3421_driver = {
1945 	.probe		= max3421_probe,
1946 	.remove		= max3421_remove,
1947 	.driver		= {
1948 		.name	= "max3421-hcd",
1949 		.owner	= THIS_MODULE,
1950 	},
1951 };
1952 
1953 module_spi_driver(max3421_driver);
1954 
1955 MODULE_DESCRIPTION(DRIVER_DESC);
1956 MODULE_AUTHOR("David Mosberger <davidm@egauge.net>");
1957 MODULE_LICENSE("GPL");
1958