GNU Linux-libre 4.4.297-gnu1
[releases.git] / drivers / usb / host / max3421-hcd.c
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/jiffies.h>
59 #include <linux/module.h>
60 #include <linux/spi/spi.h>
61 #include <linux/usb.h>
62 #include <linux/usb/hcd.h>
63
64 #include <linux/platform_data/max3421-hcd.h>
65
66 #define DRIVER_DESC     "MAX3421 USB Host-Controller Driver"
67 #define DRIVER_VERSION  "1.0"
68
69 /* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
70 #define USB_MAX_FRAME_NUMBER    0x7ff
71 #define USB_MAX_RETRIES         3 /* # of retries before error is reported */
72
73 /*
74  * Max. # of times we're willing to retransmit a request immediately in
75  * resposne to a NAK.  Afterwards, we fall back on trying once a frame.
76  */
77 #define NAK_MAX_FAST_RETRANSMITS        2
78
79 #define POWER_BUDGET    500     /* in mA; use 8 for low-power port testing */
80
81 /* Port-change mask: */
82 #define PORT_C_MASK     ((USB_PORT_STAT_C_CONNECTION |  \
83                           USB_PORT_STAT_C_ENABLE |      \
84                           USB_PORT_STAT_C_SUSPEND |     \
85                           USB_PORT_STAT_C_OVERCURRENT | \
86                           USB_PORT_STAT_C_RESET) << 16)
87
88 enum max3421_rh_state {
89         MAX3421_RH_RESET,
90         MAX3421_RH_SUSPENDED,
91         MAX3421_RH_RUNNING
92 };
93
94 enum pkt_state {
95         PKT_STATE_SETUP,        /* waiting to send setup packet to ctrl pipe */
96         PKT_STATE_TRANSFER,     /* waiting to xfer transfer_buffer */
97         PKT_STATE_TERMINATE     /* waiting to terminate control transfer */
98 };
99
100 enum scheduling_pass {
101         SCHED_PASS_PERIODIC,
102         SCHED_PASS_NON_PERIODIC,
103         SCHED_PASS_DONE
104 };
105
106 /* Bit numbers for max3421_hcd->todo: */
107 enum {
108         ENABLE_IRQ = 0,
109         RESET_HCD,
110         RESET_PORT,
111         CHECK_UNLINK,
112         IOPIN_UPDATE
113 };
114
115 struct max3421_dma_buf {
116         u8 data[2];
117 };
118
119 struct max3421_hcd {
120         spinlock_t lock;
121
122         struct task_struct *spi_thread;
123
124         enum max3421_rh_state rh_state;
125         /* lower 16 bits contain port status, upper 16 bits the change mask: */
126         u32 port_status;
127
128         unsigned active:1;
129
130         struct list_head ep_list;       /* list of EP's with work */
131
132         /*
133          * The following are owned by spi_thread (may be accessed by
134          * SPI-thread without acquiring the HCD lock:
135          */
136         u8 rev;                         /* chip revision */
137         u16 frame_number;
138         /*
139          * kmalloc'd buffers guaranteed to be in separate (DMA)
140          * cache-lines:
141          */
142         struct max3421_dma_buf *tx;
143         struct max3421_dma_buf *rx;
144         /*
145          * URB we're currently processing.  Must not be reset to NULL
146          * unless MAX3421E chip is idle:
147          */
148         struct urb *curr_urb;
149         enum scheduling_pass sched_pass;
150         int urb_done;                   /* > 0 -> no errors, < 0: errno */
151         size_t curr_len;
152         u8 hien;
153         u8 mode;
154         u8 iopins[2];
155         unsigned long todo;
156 #ifdef DEBUG
157         unsigned long err_stat[16];
158 #endif
159 };
160
161 struct max3421_ep {
162         struct usb_host_endpoint *ep;
163         struct list_head ep_list;
164         u32 naks;
165         u16 last_active;                /* frame # this ep was last active */
166         enum pkt_state pkt_state;
167         u8 retries;
168         u8 retransmit;                  /* packet needs retransmission */
169 };
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 {
487         int rcvtog, sndtog;
488         u8 hctl;
489
490         /* setup new endpoint's toggle bits: */
491         rcvtog = usb_gettoggle(dev, epnum, 0);
492         sndtog = usb_gettoggle(dev, epnum, 1);
493         hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) |
494                 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
495
496         spi_wr8(hcd, MAX3421_REG_HCTL, hctl);
497
498         /*
499          * Note: devnum for one and the same device can change during
500          * address-assignment so it's best to just always load the
501          * address whenever the end-point changed/was forced.
502          */
503         spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum);
504 }
505
506 static int
507 max3421_ctrl_setup(struct usb_hcd *hcd, struct urb *urb)
508 {
509         spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8);
510         return MAX3421_HXFR_SETUP;
511 }
512
513 static int
514 max3421_transfer_in(struct usb_hcd *hcd, struct urb *urb)
515 {
516         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
517         int epnum = usb_pipeendpoint(urb->pipe);
518
519         max3421_hcd->curr_len = 0;
520         max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT);
521         return MAX3421_HXFR_BULK_IN(epnum);
522 }
523
524 static int
525 max3421_transfer_out(struct usb_hcd *hcd, struct urb *urb, int fast_retransmit)
526 {
527         struct spi_device *spi = to_spi_device(hcd->self.controller);
528         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
529         int epnum = usb_pipeendpoint(urb->pipe);
530         u32 max_packet;
531         void *src;
532
533         src = urb->transfer_buffer + urb->actual_length;
534
535         if (fast_retransmit) {
536                 if (max3421_hcd->rev == 0x12) {
537                         /* work around rev 0x12 bug: */
538                         spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
539                         spi_wr8(hcd, MAX3421_REG_SNDFIFO, ((u8 *) src)[0]);
540                         spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
541                 }
542                 return MAX3421_HXFR_BULK_OUT(epnum);
543         }
544
545         max_packet = usb_maxpacket(urb->dev, urb->pipe, 1);
546
547         if (max_packet > MAX3421_FIFO_SIZE) {
548                 /*
549                  * We do not support isochronous transfers at this
550                  * time.
551                  */
552                 dev_err(&spi->dev,
553                         "%s: packet-size of %u too big (limit is %u bytes)",
554                         __func__, max_packet, MAX3421_FIFO_SIZE);
555                 max3421_hcd->urb_done = -EMSGSIZE;
556                 return -EMSGSIZE;
557         }
558         max3421_hcd->curr_len = min((urb->transfer_buffer_length -
559                                      urb->actual_length), max_packet);
560
561         spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len);
562         spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
563         return MAX3421_HXFR_BULK_OUT(epnum);
564 }
565
566 /*
567  * Issue the next host-transfer command.
568  * Caller must NOT hold HCD spinlock.
569  */
570 static void
571 max3421_next_transfer(struct usb_hcd *hcd, int fast_retransmit)
572 {
573         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
574         struct urb *urb = max3421_hcd->curr_urb;
575         struct max3421_ep *max3421_ep;
576         int cmd = -EINVAL;
577
578         if (!urb)
579                 return; /* nothing to do */
580
581         max3421_ep = urb->ep->hcpriv;
582
583         switch (max3421_ep->pkt_state) {
584         case PKT_STATE_SETUP:
585                 cmd = max3421_ctrl_setup(hcd, urb);
586                 break;
587
588         case PKT_STATE_TRANSFER:
589                 if (usb_urb_dir_in(urb))
590                         cmd = max3421_transfer_in(hcd, urb);
591                 else
592                         cmd = max3421_transfer_out(hcd, urb, fast_retransmit);
593                 break;
594
595         case PKT_STATE_TERMINATE:
596                 /*
597                  * IN transfers are terminated with HS_OUT token,
598                  * OUT transfers with HS_IN:
599                  */
600                 if (usb_urb_dir_in(urb))
601                         cmd = MAX3421_HXFR_HS_OUT;
602                 else
603                         cmd = MAX3421_HXFR_HS_IN;
604                 break;
605         }
606
607         if (cmd < 0)
608                 return;
609
610         /* issue the command and wait for host-xfer-done interrupt: */
611
612         spi_wr8(hcd, MAX3421_REG_HXFR, cmd);
613         max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
614 }
615
616 /*
617  * Find the next URB to process and start its execution.
618  *
619  * At this time, we do not anticipate ever connecting a USB hub to the
620  * MAX3421 chip, so at most USB device can be connected and we can use
621  * a simplistic scheduler: at the start of a frame, schedule all
622  * periodic transfers.  Once that is done, use the remainder of the
623  * frame to process non-periodic (bulk & control) transfers.
624  *
625  * Preconditions:
626  * o Caller must NOT hold HCD spinlock.
627  * o max3421_hcd->curr_urb MUST BE NULL.
628  * o MAX3421E chip must be idle.
629  */
630 static int
631 max3421_select_and_start_urb(struct usb_hcd *hcd)
632 {
633         struct spi_device *spi = to_spi_device(hcd->self.controller);
634         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
635         struct urb *urb, *curr_urb = NULL;
636         struct max3421_ep *max3421_ep;
637         int epnum;
638         struct usb_host_endpoint *ep;
639         struct list_head *pos;
640         unsigned long flags;
641
642         spin_lock_irqsave(&max3421_hcd->lock, flags);
643
644         for (;
645              max3421_hcd->sched_pass < SCHED_PASS_DONE;
646              ++max3421_hcd->sched_pass)
647                 list_for_each(pos, &max3421_hcd->ep_list) {
648                         urb = NULL;
649                         max3421_ep = container_of(pos, struct max3421_ep,
650                                                   ep_list);
651                         ep = max3421_ep->ep;
652
653                         switch (usb_endpoint_type(&ep->desc)) {
654                         case USB_ENDPOINT_XFER_ISOC:
655                         case USB_ENDPOINT_XFER_INT:
656                                 if (max3421_hcd->sched_pass !=
657                                     SCHED_PASS_PERIODIC)
658                                         continue;
659                                 break;
660
661                         case USB_ENDPOINT_XFER_CONTROL:
662                         case USB_ENDPOINT_XFER_BULK:
663                                 if (max3421_hcd->sched_pass !=
664                                     SCHED_PASS_NON_PERIODIC)
665                                         continue;
666                                 break;
667                         }
668
669                         if (list_empty(&ep->urb_list))
670                                 continue;       /* nothing to do */
671                         urb = list_first_entry(&ep->urb_list, struct urb,
672                                                urb_list);
673                         if (urb->unlinked) {
674                                 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
675                                         __func__, urb, urb->unlinked);
676                                 max3421_hcd->curr_urb = urb;
677                                 max3421_hcd->urb_done = 1;
678                                 spin_unlock_irqrestore(&max3421_hcd->lock,
679                                                        flags);
680                                 return 1;
681                         }
682
683                         switch (usb_endpoint_type(&ep->desc)) {
684                         case USB_ENDPOINT_XFER_CONTROL:
685                                 /*
686                                  * Allow one control transaction per
687                                  * frame per endpoint:
688                                  */
689                                 if (frame_diff(max3421_ep->last_active,
690                                                max3421_hcd->frame_number) == 0)
691                                         continue;
692                                 break;
693
694                         case USB_ENDPOINT_XFER_BULK:
695                                 if (max3421_ep->retransmit
696                                     && (frame_diff(max3421_ep->last_active,
697                                                    max3421_hcd->frame_number)
698                                         == 0))
699                                         /*
700                                          * We already tried this EP
701                                          * during this frame and got a
702                                          * NAK or error; wait for next frame
703                                          */
704                                         continue;
705                                 break;
706
707                         case USB_ENDPOINT_XFER_ISOC:
708                         case USB_ENDPOINT_XFER_INT:
709                                 if (frame_diff(max3421_hcd->frame_number,
710                                                max3421_ep->last_active)
711                                     < urb->interval)
712                                         /*
713                                          * We already processed this
714                                          * end-point in the current
715                                          * frame
716                                          */
717                                         continue;
718                                 break;
719                         }
720
721                         /* move current ep to tail: */
722                         list_move_tail(pos, &max3421_hcd->ep_list);
723                         curr_urb = urb;
724                         goto done;
725                 }
726 done:
727         if (!curr_urb) {
728                 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
729                 return 0;
730         }
731
732         urb = max3421_hcd->curr_urb = curr_urb;
733         epnum = usb_endpoint_num(&urb->ep->desc);
734         if (max3421_ep->retransmit)
735                 /* restart (part of) a USB transaction: */
736                 max3421_ep->retransmit = 0;
737         else {
738                 /* start USB transaction: */
739                 if (usb_endpoint_xfer_control(&ep->desc)) {
740                         /*
741                          * See USB 2.0 spec section 8.6.1
742                          * Initialization via SETUP Token:
743                          */
744                         usb_settoggle(urb->dev, epnum, 0, 1);
745                         usb_settoggle(urb->dev, epnum, 1, 1);
746                         max3421_ep->pkt_state = PKT_STATE_SETUP;
747                 } else
748                         max3421_ep->pkt_state = PKT_STATE_TRANSFER;
749         }
750
751         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
752
753         max3421_ep->last_active = max3421_hcd->frame_number;
754         max3421_set_address(hcd, urb->dev, epnum);
755         max3421_set_speed(hcd, urb->dev);
756         max3421_next_transfer(hcd, 0);
757         return 1;
758 }
759
760 /*
761  * Check all endpoints for URBs that got unlinked.
762  *
763  * Caller must NOT hold HCD spinlock.
764  */
765 static int
766 max3421_check_unlink(struct usb_hcd *hcd)
767 {
768         struct spi_device *spi = to_spi_device(hcd->self.controller);
769         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
770         struct list_head *pos, *upos, *next_upos;
771         struct max3421_ep *max3421_ep;
772         struct usb_host_endpoint *ep;
773         struct urb *urb;
774         unsigned long flags;
775         int retval = 0;
776
777         spin_lock_irqsave(&max3421_hcd->lock, flags);
778         list_for_each(pos, &max3421_hcd->ep_list) {
779                 max3421_ep = container_of(pos, struct max3421_ep, ep_list);
780                 ep = max3421_ep->ep;
781                 list_for_each_safe(upos, next_upos, &ep->urb_list) {
782                         urb = container_of(upos, struct urb, urb_list);
783                         if (urb->unlinked) {
784                                 retval = 1;
785                                 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
786                                         __func__, urb, urb->unlinked);
787                                 usb_hcd_unlink_urb_from_ep(hcd, urb);
788                                 spin_unlock_irqrestore(&max3421_hcd->lock,
789                                                        flags);
790                                 usb_hcd_giveback_urb(hcd, urb, 0);
791                                 spin_lock_irqsave(&max3421_hcd->lock, flags);
792                         }
793                 }
794         }
795         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
796         return retval;
797 }
798
799 /*
800  * Caller must NOT hold HCD spinlock.
801  */
802 static void
803 max3421_slow_retransmit(struct usb_hcd *hcd)
804 {
805         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
806         struct urb *urb = max3421_hcd->curr_urb;
807         struct max3421_ep *max3421_ep;
808
809         max3421_ep = urb->ep->hcpriv;
810         max3421_ep->retransmit = 1;
811         max3421_hcd->curr_urb = NULL;
812 }
813
814 /*
815  * Caller must NOT hold HCD spinlock.
816  */
817 static void
818 max3421_recv_data_available(struct usb_hcd *hcd)
819 {
820         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
821         struct urb *urb = max3421_hcd->curr_urb;
822         size_t remaining, transfer_size;
823         u8 rcvbc;
824
825         rcvbc = spi_rd8(hcd, MAX3421_REG_RCVBC);
826
827         if (rcvbc > MAX3421_FIFO_SIZE)
828                 rcvbc = MAX3421_FIFO_SIZE;
829         if (urb->actual_length >= urb->transfer_buffer_length)
830                 remaining = 0;
831         else
832                 remaining = urb->transfer_buffer_length - urb->actual_length;
833         transfer_size = rcvbc;
834         if (transfer_size > remaining)
835                 transfer_size = remaining;
836         if (transfer_size > 0) {
837                 void *dst = urb->transfer_buffer + urb->actual_length;
838
839                 spi_rd_buf(hcd, MAX3421_REG_RCVFIFO, dst, transfer_size);
840                 urb->actual_length += transfer_size;
841                 max3421_hcd->curr_len = transfer_size;
842         }
843
844         /* ack the RCVDAV irq now that the FIFO has been read: */
845         spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT));
846 }
847
848 static void
849 max3421_handle_error(struct usb_hcd *hcd, u8 hrsl)
850 {
851         struct spi_device *spi = to_spi_device(hcd->self.controller);
852         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
853         u8 result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
854         struct urb *urb = max3421_hcd->curr_urb;
855         struct max3421_ep *max3421_ep = urb->ep->hcpriv;
856         int switch_sndfifo;
857
858         /*
859          * If an OUT command results in any response other than OK
860          * (i.e., error or NAK), we have to perform a dummy-write to
861          * SNDBC so the FIFO gets switched back to us.  Otherwise, we
862          * get out of sync with the SNDFIFO double buffer.
863          */
864         switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER &&
865                           usb_urb_dir_out(urb));
866
867         switch (result_code) {
868         case MAX3421_HRSL_OK:
869                 return;                 /* this shouldn't happen */
870
871         case MAX3421_HRSL_WRONGPID:     /* received wrong PID */
872         case MAX3421_HRSL_BUSY:         /* SIE busy */
873         case MAX3421_HRSL_BADREQ:       /* bad val in HXFR */
874         case MAX3421_HRSL_UNDEF:        /* reserved */
875         case MAX3421_HRSL_KERR:         /* K-state instead of response */
876         case MAX3421_HRSL_JERR:         /* J-state instead of response */
877                 /*
878                  * packet experienced an error that we cannot recover
879                  * from; report error
880                  */
881                 max3421_hcd->urb_done = hrsl_to_error[result_code];
882                 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
883                         __func__, hrsl);
884                 break;
885
886         case MAX3421_HRSL_TOGERR:
887                 if (usb_urb_dir_in(urb))
888                         ; /* don't do anything (device will switch toggle) */
889                 else {
890                         /* flip the send toggle bit: */
891                         int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
892
893                         sndtog ^= 1;
894                         spi_wr8(hcd, MAX3421_REG_HCTL,
895                                 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
896                 }
897                 /* FALL THROUGH */
898         case MAX3421_HRSL_BADBC:        /* bad byte count */
899         case MAX3421_HRSL_PIDERR:       /* received PID is corrupted */
900         case MAX3421_HRSL_PKTERR:       /* packet error (stuff, EOP) */
901         case MAX3421_HRSL_CRCERR:       /* CRC error */
902         case MAX3421_HRSL_BABBLE:       /* device talked too long */
903         case MAX3421_HRSL_TIMEOUT:
904                 if (max3421_ep->retries++ < USB_MAX_RETRIES)
905                         /* retry the packet again in the next frame */
906                         max3421_slow_retransmit(hcd);
907                 else {
908                         /* Based on ohci.h cc_to_err[]: */
909                         max3421_hcd->urb_done = hrsl_to_error[result_code];
910                         dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
911                                 __func__, hrsl);
912                 }
913                 break;
914
915         case MAX3421_HRSL_STALL:
916                 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
917                         __func__, hrsl);
918                 max3421_hcd->urb_done = hrsl_to_error[result_code];
919                 break;
920
921         case MAX3421_HRSL_NAK:
922                 /*
923                  * Device wasn't ready for data or has no data
924                  * available: retry the packet again.
925                  */
926                 if (max3421_ep->naks++ < NAK_MAX_FAST_RETRANSMITS) {
927                         max3421_next_transfer(hcd, 1);
928                         switch_sndfifo = 0;
929                 } else
930                         max3421_slow_retransmit(hcd);
931                 break;
932         }
933         if (switch_sndfifo)
934                 spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
935 }
936
937 /*
938  * Caller must NOT hold HCD spinlock.
939  */
940 static int
941 max3421_transfer_in_done(struct usb_hcd *hcd, struct urb *urb)
942 {
943         struct spi_device *spi = to_spi_device(hcd->self.controller);
944         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
945         u32 max_packet;
946
947         if (urb->actual_length >= urb->transfer_buffer_length)
948                 return 1;       /* read is complete, so we're done */
949
950         /*
951          * USB 2.0 Section 5.3.2 Pipes: packets must be full size
952          * except for last one.
953          */
954         max_packet = usb_maxpacket(urb->dev, urb->pipe, 0);
955         if (max_packet > MAX3421_FIFO_SIZE) {
956                 /*
957                  * We do not support isochronous transfers at this
958                  * time...
959                  */
960                 dev_err(&spi->dev,
961                         "%s: packet-size of %u too big (limit is %u bytes)",
962                         __func__, max_packet, MAX3421_FIFO_SIZE);
963                 return -EINVAL;
964         }
965
966         if (max3421_hcd->curr_len < max_packet) {
967                 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
968                         /*
969                          * remaining > 0 and received an
970                          * unexpected partial packet ->
971                          * error
972                          */
973                         return -EREMOTEIO;
974                 } else
975                         /* short read, but it's OK */
976                         return 1;
977         }
978         return 0;       /* not done */
979 }
980
981 /*
982  * Caller must NOT hold HCD spinlock.
983  */
984 static int
985 max3421_transfer_out_done(struct usb_hcd *hcd, struct urb *urb)
986 {
987         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
988
989         urb->actual_length += max3421_hcd->curr_len;
990         if (urb->actual_length < urb->transfer_buffer_length)
991                 return 0;
992         if (urb->transfer_flags & URB_ZERO_PACKET) {
993                 /*
994                  * Some hardware needs a zero-size packet at the end
995                  * of a bulk-out transfer if the last transfer was a
996                  * full-sized packet (i.e., such hardware use <
997                  * max_packet as an indicator that the end of the
998                  * packet has been reached).
999                  */
1000                 u32 max_packet = usb_maxpacket(urb->dev, urb->pipe, 1);
1001
1002                 if (max3421_hcd->curr_len == max_packet)
1003                         return 0;
1004         }
1005         return 1;
1006 }
1007
1008 /*
1009  * Caller must NOT hold HCD spinlock.
1010  */
1011 static void
1012 max3421_host_transfer_done(struct usb_hcd *hcd)
1013 {
1014         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1015         struct urb *urb = max3421_hcd->curr_urb;
1016         struct max3421_ep *max3421_ep;
1017         u8 result_code, hrsl;
1018         int urb_done = 0;
1019
1020         max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) |
1021                                BIT(MAX3421_HI_RCVDAV_BIT));
1022
1023         hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1024         result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
1025
1026 #ifdef DEBUG
1027         ++max3421_hcd->err_stat[result_code];
1028 #endif
1029
1030         max3421_ep = urb->ep->hcpriv;
1031
1032         if (unlikely(result_code != MAX3421_HRSL_OK)) {
1033                 max3421_handle_error(hcd, hrsl);
1034                 return;
1035         }
1036
1037         max3421_ep->naks = 0;
1038         max3421_ep->retries = 0;
1039         switch (max3421_ep->pkt_state) {
1040
1041         case PKT_STATE_SETUP:
1042                 if (urb->transfer_buffer_length > 0)
1043                         max3421_ep->pkt_state = PKT_STATE_TRANSFER;
1044                 else
1045                         max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1046                 break;
1047
1048         case PKT_STATE_TRANSFER:
1049                 if (usb_urb_dir_in(urb))
1050                         urb_done = max3421_transfer_in_done(hcd, urb);
1051                 else
1052                         urb_done = max3421_transfer_out_done(hcd, urb);
1053                 if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1054                         /*
1055                          * We aren't really done - we still need to
1056                          * terminate the control transfer:
1057                          */
1058                         max3421_hcd->urb_done = urb_done = 0;
1059                         max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1060                 }
1061                 break;
1062
1063         case PKT_STATE_TERMINATE:
1064                 urb_done = 1;
1065                 break;
1066         }
1067
1068         if (urb_done)
1069                 max3421_hcd->urb_done = urb_done;
1070         else
1071                 max3421_next_transfer(hcd, 0);
1072 }
1073
1074 /*
1075  * Caller must NOT hold HCD spinlock.
1076  */
1077 static void
1078 max3421_detect_conn(struct usb_hcd *hcd)
1079 {
1080         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1081         unsigned int jk, have_conn = 0;
1082         u32 old_port_status, chg;
1083         unsigned long flags;
1084         u8 hrsl, mode;
1085
1086         hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1087
1088         jk = ((((hrsl >> MAX3421_HRSL_JSTATUS_BIT) & 1) << 0) |
1089               (((hrsl >> MAX3421_HRSL_KSTATUS_BIT) & 1) << 1));
1090
1091         mode = max3421_hcd->mode;
1092
1093         switch (jk) {
1094         case 0x0: /* SE0: disconnect */
1095                 /*
1096                  * Turn off SOFKAENAB bit to avoid getting interrupt
1097                  * every milli-second:
1098                  */
1099                 mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
1100                 break;
1101
1102         case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */
1103         case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */
1104                 if (jk == 0x2)
1105                         /* need to switch to the other speed: */
1106                         mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
1107                 /* turn on SOFKAENAB bit: */
1108                 mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
1109                 have_conn = 1;
1110                 break;
1111
1112         case 0x3: /* illegal */
1113                 break;
1114         }
1115
1116         max3421_hcd->mode = mode;
1117         spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1118
1119         spin_lock_irqsave(&max3421_hcd->lock, flags);
1120         old_port_status = max3421_hcd->port_status;
1121         if (have_conn)
1122                 max3421_hcd->port_status |=  USB_PORT_STAT_CONNECTION;
1123         else
1124                 max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
1125         if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
1126                 max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
1127         else
1128                 max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
1129         chg = (old_port_status ^ max3421_hcd->port_status);
1130         max3421_hcd->port_status |= chg << 16;
1131         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1132 }
1133
1134 static irqreturn_t
1135 max3421_irq_handler(int irq, void *dev_id)
1136 {
1137         struct usb_hcd *hcd = dev_id;
1138         struct spi_device *spi = to_spi_device(hcd->self.controller);
1139         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1140
1141         if (max3421_hcd->spi_thread &&
1142             max3421_hcd->spi_thread->state != TASK_RUNNING)
1143                 wake_up_process(max3421_hcd->spi_thread);
1144         if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo))
1145                 disable_irq_nosync(spi->irq);
1146         return IRQ_HANDLED;
1147 }
1148
1149 #ifdef DEBUG
1150
1151 static void
1152 dump_eps(struct usb_hcd *hcd)
1153 {
1154         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1155         struct max3421_ep *max3421_ep;
1156         struct usb_host_endpoint *ep;
1157         struct list_head *pos, *upos;
1158         char ubuf[512], *dp, *end;
1159         unsigned long flags;
1160         struct urb *urb;
1161         int epnum, ret;
1162
1163         spin_lock_irqsave(&max3421_hcd->lock, flags);
1164         list_for_each(pos, &max3421_hcd->ep_list) {
1165                 max3421_ep = container_of(pos, struct max3421_ep, ep_list);
1166                 ep = max3421_ep->ep;
1167
1168                 dp = ubuf;
1169                 end = dp + sizeof(ubuf);
1170                 *dp = '\0';
1171                 list_for_each(upos, &ep->urb_list) {
1172                         urb = container_of(upos, struct urb, urb_list);
1173                         ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1174                                        usb_pipetype(urb->pipe),
1175                                        usb_urb_dir_in(urb) ? "IN" : "OUT",
1176                                        urb->actual_length,
1177                                        urb->transfer_buffer_length);
1178                         if (ret < 0 || ret >= end - dp)
1179                                 break;  /* error or buffer full */
1180                         dp += ret;
1181                 }
1182
1183                 epnum = usb_endpoint_num(&ep->desc);
1184                 pr_info("EP%0u %u lst %04u rtr %u nak %6u rxmt %u: %s\n",
1185                         epnum, max3421_ep->pkt_state, max3421_ep->last_active,
1186                         max3421_ep->retries, max3421_ep->naks,
1187                         max3421_ep->retransmit, ubuf);
1188         }
1189         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1190 }
1191
1192 #endif /* DEBUG */
1193
1194 /* Return zero if no work was performed, 1 otherwise.  */
1195 static int
1196 max3421_handle_irqs(struct usb_hcd *hcd)
1197 {
1198         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1199         u32 chg, old_port_status;
1200         unsigned long flags;
1201         u8 hirq;
1202
1203         /*
1204          * Read and ack pending interrupts (CPU must never
1205          * clear SNDBAV directly and RCVDAV must be cleared by
1206          * max3421_recv_data_available()!):
1207          */
1208         hirq = spi_rd8(hcd, MAX3421_REG_HIRQ);
1209         hirq &= max3421_hcd->hien;
1210         if (!hirq)
1211                 return 0;
1212
1213         spi_wr8(hcd, MAX3421_REG_HIRQ,
1214                 hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
1215                          BIT(MAX3421_HI_RCVDAV_BIT)));
1216
1217         if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
1218                 max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
1219                                              & USB_MAX_FRAME_NUMBER);
1220                 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1221         }
1222
1223         if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
1224                 max3421_recv_data_available(hcd);
1225
1226         if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
1227                 max3421_host_transfer_done(hcd);
1228
1229         if (hirq & BIT(MAX3421_HI_CONDET_BIT))
1230                 max3421_detect_conn(hcd);
1231
1232         /*
1233          * Now process interrupts that may affect HCD state
1234          * other than the end-points:
1235          */
1236         spin_lock_irqsave(&max3421_hcd->lock, flags);
1237
1238         old_port_status = max3421_hcd->port_status;
1239         if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
1240                 if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
1241                         /* BUSEVENT due to completion of Bus Reset */
1242                         max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
1243                         max3421_hcd->port_status |=  USB_PORT_STAT_ENABLE;
1244                 } else {
1245                         /* BUSEVENT due to completion of Bus Resume */
1246                         pr_info("%s: BUSEVENT Bus Resume Done\n", __func__);
1247                 }
1248         }
1249         if (hirq & BIT(MAX3421_HI_RWU_BIT))
1250                 pr_info("%s: RWU\n", __func__);
1251         if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
1252                 pr_info("%s: SUSDN\n", __func__);
1253
1254         chg = (old_port_status ^ max3421_hcd->port_status);
1255         max3421_hcd->port_status |= chg << 16;
1256
1257         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1258
1259 #ifdef DEBUG
1260         {
1261                 static unsigned long last_time;
1262                 char sbuf[16 * 16], *dp, *end;
1263                 int i;
1264
1265                 if (time_after(jiffies, last_time + 5*HZ)) {
1266                         dp = sbuf;
1267                         end = sbuf + sizeof(sbuf);
1268                         *dp = '\0';
1269                         for (i = 0; i < 16; ++i) {
1270                                 int ret = snprintf(dp, end - dp, " %lu",
1271                                                    max3421_hcd->err_stat[i]);
1272                                 if (ret < 0 || ret >= end - dp)
1273                                         break;  /* error or buffer full */
1274                                 dp += ret;
1275                         }
1276                         pr_info("%s: hrsl_stats %s\n", __func__, sbuf);
1277                         memset(max3421_hcd->err_stat, 0,
1278                                sizeof(max3421_hcd->err_stat));
1279                         last_time = jiffies;
1280
1281                         dump_eps(hcd);
1282                 }
1283         }
1284 #endif
1285         return 1;
1286 }
1287
1288 static int
1289 max3421_reset_hcd(struct usb_hcd *hcd)
1290 {
1291         struct spi_device *spi = to_spi_device(hcd->self.controller);
1292         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1293         int timeout;
1294
1295         /* perform a chip reset and wait for OSCIRQ signal to appear: */
1296         spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT));
1297         /* clear reset: */
1298         spi_wr8(hcd, MAX3421_REG_USBCTL, 0);
1299         timeout = 1000;
1300         while (1) {
1301                 if (spi_rd8(hcd, MAX3421_REG_USBIRQ)
1302                     & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
1303                         break;
1304                 if (--timeout < 0) {
1305                         dev_err(&spi->dev,
1306                                 "timed out waiting for oscillator OK signal");
1307                         return 1;
1308                 }
1309                 cond_resched();
1310         }
1311
1312         /*
1313          * Turn on host mode, automatic generation of SOF packets, and
1314          * enable pull-down registers on DM/DP:
1315          */
1316         max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) |
1317                              BIT(MAX3421_MODE_SOFKAENAB_BIT) |
1318                              BIT(MAX3421_MODE_DMPULLDN_BIT) |
1319                              BIT(MAX3421_MODE_DPPULLDN_BIT));
1320         spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1321
1322         /* reset frame-number: */
1323         max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER;
1324         spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT));
1325
1326         /* sample the state of the D+ and D- lines */
1327         spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT));
1328         max3421_detect_conn(hcd);
1329
1330         /* enable frame, connection-detected, and bus-event interrupts: */
1331         max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
1332                              BIT(MAX3421_HI_CONDET_BIT) |
1333                              BIT(MAX3421_HI_BUSEVENT_BIT));
1334         spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1335
1336         /* enable interrupts: */
1337         spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT));
1338         return 1;
1339 }
1340
1341 static int
1342 max3421_urb_done(struct usb_hcd *hcd)
1343 {
1344         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1345         unsigned long flags;
1346         struct urb *urb;
1347         int status;
1348
1349         status = max3421_hcd->urb_done;
1350         max3421_hcd->urb_done = 0;
1351         if (status > 0)
1352                 status = 0;
1353         urb = max3421_hcd->curr_urb;
1354         if (urb) {
1355                 /* save the old end-points toggles: */
1356                 u8 hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1357                 int rcvtog = (hrsl >> MAX3421_HRSL_RCVTOGRD_BIT) & 1;
1358                 int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
1359                 int epnum = usb_endpoint_num(&urb->ep->desc);
1360
1361                 /* no locking: HCD (i.e., we) own toggles, don't we? */
1362                 usb_settoggle(urb->dev, epnum, 0, rcvtog);
1363                 usb_settoggle(urb->dev, epnum, 1, sndtog);
1364
1365                 max3421_hcd->curr_urb = NULL;
1366                 spin_lock_irqsave(&max3421_hcd->lock, flags);
1367                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1368                 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1369
1370                 /* must be called without the HCD spinlock: */
1371                 usb_hcd_giveback_urb(hcd, urb, status);
1372         }
1373         return 1;
1374 }
1375
1376 static int
1377 max3421_spi_thread(void *dev_id)
1378 {
1379         struct usb_hcd *hcd = dev_id;
1380         struct spi_device *spi = to_spi_device(hcd->self.controller);
1381         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1382         int i, i_worked = 1;
1383
1384         /* set full-duplex SPI mode, low-active interrupt pin: */
1385         spi_wr8(hcd, MAX3421_REG_PINCTL,
1386                 (BIT(MAX3421_PINCTL_FDUPSPI_BIT) |      /* full-duplex */
1387                  BIT(MAX3421_PINCTL_INTLEVEL_BIT)));    /* low-active irq */
1388
1389         while (!kthread_should_stop()) {
1390                 max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION);
1391                 if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13)
1392                         break;
1393                 dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev);
1394                 msleep(10000);
1395         }
1396         dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
1397                  max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word,
1398                  spi->irq);
1399
1400         while (!kthread_should_stop()) {
1401                 if (!i_worked) {
1402                         /*
1403                          * We'll be waiting for wakeups from the hard
1404                          * interrupt handler, so now is a good time to
1405                          * sync our hien with the chip:
1406                          */
1407                         spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1408
1409                         set_current_state(TASK_INTERRUPTIBLE);
1410                         if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo))
1411                                 enable_irq(spi->irq);
1412                         schedule();
1413                         __set_current_state(TASK_RUNNING);
1414                 }
1415
1416                 i_worked = 0;
1417
1418                 if (max3421_hcd->urb_done)
1419                         i_worked |= max3421_urb_done(hcd);
1420                 else if (max3421_handle_irqs(hcd))
1421                         i_worked = 1;
1422                 else if (!max3421_hcd->curr_urb)
1423                         i_worked |= max3421_select_and_start_urb(hcd);
1424
1425                 if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo))
1426                         /* reset the HCD: */
1427                         i_worked |= max3421_reset_hcd(hcd);
1428                 if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) {
1429                         /* perform a USB bus reset: */
1430                         spi_wr8(hcd, MAX3421_REG_HCTL,
1431                                 BIT(MAX3421_HCTL_BUSRST_BIT));
1432                         i_worked = 1;
1433                 }
1434                 if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo))
1435                         i_worked |= max3421_check_unlink(hcd);
1436                 if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) {
1437                         /*
1438                          * IOPINS1/IOPINS2 do not auto-increment, so we can't
1439                          * use spi_wr_buf().
1440                          */
1441                         for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) {
1442                                 u8 val = spi_rd8(hcd, MAX3421_REG_IOPINS1);
1443
1444                                 val = ((val & 0xf0) |
1445                                        (max3421_hcd->iopins[i] & 0x0f));
1446                                 spi_wr8(hcd, MAX3421_REG_IOPINS1 + i, val);
1447                                 max3421_hcd->iopins[i] = val;
1448                         }
1449                         i_worked = 1;
1450                 }
1451         }
1452         set_current_state(TASK_RUNNING);
1453         dev_info(&spi->dev, "SPI thread exiting");
1454         return 0;
1455 }
1456
1457 static int
1458 max3421_reset_port(struct usb_hcd *hcd)
1459 {
1460         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1461
1462         max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
1463                                       USB_PORT_STAT_LOW_SPEED);
1464         max3421_hcd->port_status |= USB_PORT_STAT_RESET;
1465         set_bit(RESET_PORT, &max3421_hcd->todo);
1466         wake_up_process(max3421_hcd->spi_thread);
1467         return 0;
1468 }
1469
1470 static int
1471 max3421_reset(struct usb_hcd *hcd)
1472 {
1473         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1474
1475         hcd->self.sg_tablesize = 0;
1476         hcd->speed = HCD_USB2;
1477         hcd->self.root_hub->speed = USB_SPEED_FULL;
1478         set_bit(RESET_HCD, &max3421_hcd->todo);
1479         wake_up_process(max3421_hcd->spi_thread);
1480         return 0;
1481 }
1482
1483 static int
1484 max3421_start(struct usb_hcd *hcd)
1485 {
1486         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1487
1488         spin_lock_init(&max3421_hcd->lock);
1489         max3421_hcd->rh_state = MAX3421_RH_RUNNING;
1490
1491         INIT_LIST_HEAD(&max3421_hcd->ep_list);
1492
1493         hcd->power_budget = POWER_BUDGET;
1494         hcd->state = HC_STATE_RUNNING;
1495         hcd->uses_new_polling = 1;
1496         return 0;
1497 }
1498
1499 static void
1500 max3421_stop(struct usb_hcd *hcd)
1501 {
1502 }
1503
1504 static int
1505 max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1506 {
1507         struct spi_device *spi = to_spi_device(hcd->self.controller);
1508         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1509         struct max3421_ep *max3421_ep;
1510         unsigned long flags;
1511         int retval;
1512
1513         switch (usb_pipetype(urb->pipe)) {
1514         case PIPE_INTERRUPT:
1515         case PIPE_ISOCHRONOUS:
1516                 if (urb->interval < 0) {
1517                         dev_err(&spi->dev,
1518                           "%s: interval=%d for intr-/iso-pipe; expected > 0\n",
1519                                 __func__, urb->interval);
1520                         return -EINVAL;
1521                 }
1522         default:
1523                 break;
1524         }
1525
1526         spin_lock_irqsave(&max3421_hcd->lock, flags);
1527
1528         max3421_ep = urb->ep->hcpriv;
1529         if (!max3421_ep) {
1530                 /* gets freed in max3421_endpoint_disable: */
1531                 max3421_ep = kzalloc(sizeof(struct max3421_ep), GFP_ATOMIC);
1532                 if (!max3421_ep) {
1533                         retval = -ENOMEM;
1534                         goto out;
1535                 }
1536                 max3421_ep->ep = urb->ep;
1537                 max3421_ep->last_active = max3421_hcd->frame_number;
1538                 urb->ep->hcpriv = max3421_ep;
1539
1540                 list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
1541         }
1542
1543         retval = usb_hcd_link_urb_to_ep(hcd, urb);
1544         if (retval == 0) {
1545                 /* Since we added to the queue, restart scheduling: */
1546                 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1547                 wake_up_process(max3421_hcd->spi_thread);
1548         }
1549
1550 out:
1551         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1552         return retval;
1553 }
1554
1555 static int
1556 max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1557 {
1558         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1559         unsigned long flags;
1560         int retval;
1561
1562         spin_lock_irqsave(&max3421_hcd->lock, flags);
1563
1564         /*
1565          * This will set urb->unlinked which in turn causes the entry
1566          * to be dropped at the next opportunity.
1567          */
1568         retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1569         if (retval == 0) {
1570                 set_bit(CHECK_UNLINK, &max3421_hcd->todo);
1571                 wake_up_process(max3421_hcd->spi_thread);
1572         }
1573         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1574         return retval;
1575 }
1576
1577 static void
1578 max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1579 {
1580         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1581         unsigned long flags;
1582
1583         spin_lock_irqsave(&max3421_hcd->lock, flags);
1584
1585         if (ep->hcpriv) {
1586                 struct max3421_ep *max3421_ep = ep->hcpriv;
1587
1588                 /* remove myself from the ep_list: */
1589                 if (!list_empty(&max3421_ep->ep_list))
1590                         list_del(&max3421_ep->ep_list);
1591                 kfree(max3421_ep);
1592                 ep->hcpriv = NULL;
1593         }
1594
1595         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1596 }
1597
1598 static int
1599 max3421_get_frame_number(struct usb_hcd *hcd)
1600 {
1601         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1602         return max3421_hcd->frame_number;
1603 }
1604
1605 /*
1606  * Should return a non-zero value when any port is undergoing a resume
1607  * transition while the root hub is suspended.
1608  */
1609 static int
1610 max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
1611 {
1612         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1613         unsigned long flags;
1614         int retval = 0;
1615
1616         spin_lock_irqsave(&max3421_hcd->lock, flags);
1617         if (!HCD_HW_ACCESSIBLE(hcd))
1618                 goto done;
1619
1620         *buf = 0;
1621         if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
1622                 *buf = (1 << 1); /* a hub over-current condition exists */
1623                 dev_dbg(hcd->self.controller,
1624                         "port status 0x%08x has changes\n",
1625                         max3421_hcd->port_status);
1626                 retval = 1;
1627                 if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
1628                         usb_hcd_resume_root_hub(hcd);
1629         }
1630 done:
1631         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1632         return retval;
1633 }
1634
1635 static inline void
1636 hub_descriptor(struct usb_hub_descriptor *desc)
1637 {
1638         memset(desc, 0, sizeof(*desc));
1639         /*
1640          * See Table 11-13: Hub Descriptor in USB 2.0 spec.
1641          */
1642         desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */
1643         desc->bDescLength = 9;
1644         desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM |
1645                                                 HUB_CHAR_COMMON_OCPM);
1646         desc->bNbrPorts = 1;
1647 }
1648
1649 /*
1650  * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1651  * VALUE (0 or 1).  PIN_NUMBER may be in the range from 1-8.  For
1652  * any other value, this function acts as a no-op.
1653  */
1654 static void
1655 max3421_gpout_set_value(struct usb_hcd *hcd, u8 pin_number, u8 value)
1656 {
1657         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1658         u8 mask, idx;
1659
1660         --pin_number;
1661         if (pin_number > 7)
1662                 return;
1663
1664         mask = 1u << pin_number;
1665         idx = pin_number / 4;
1666
1667         if (value)
1668                 max3421_hcd->iopins[idx] |=  mask;
1669         else
1670                 max3421_hcd->iopins[idx] &= ~mask;
1671         set_bit(IOPIN_UPDATE, &max3421_hcd->todo);
1672         wake_up_process(max3421_hcd->spi_thread);
1673 }
1674
1675 static int
1676 max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
1677                     char *buf, u16 length)
1678 {
1679         struct spi_device *spi = to_spi_device(hcd->self.controller);
1680         struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1681         struct max3421_hcd_platform_data *pdata;
1682         unsigned long flags;
1683         int retval = 0;
1684
1685         spin_lock_irqsave(&max3421_hcd->lock, flags);
1686
1687         pdata = spi->dev.platform_data;
1688
1689         switch (type_req) {
1690         case ClearHubFeature:
1691                 break;
1692         case ClearPortFeature:
1693                 switch (value) {
1694                 case USB_PORT_FEAT_SUSPEND:
1695                         break;
1696                 case USB_PORT_FEAT_POWER:
1697                         dev_dbg(hcd->self.controller, "power-off\n");
1698                         max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1699                                                 !pdata->vbus_active_level);
1700                         /* FALLS THROUGH */
1701                 default:
1702                         max3421_hcd->port_status &= ~(1 << value);
1703                 }
1704                 break;
1705         case GetHubDescriptor:
1706                 hub_descriptor((struct usb_hub_descriptor *) buf);
1707                 break;
1708
1709         case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1710         case GetPortErrorCount:
1711         case SetHubDepth:
1712                 /* USB3 only */
1713                 goto error;
1714
1715         case GetHubStatus:
1716                 *(__le32 *) buf = cpu_to_le32(0);
1717                 break;
1718
1719         case GetPortStatus:
1720                 if (index != 1) {
1721                         retval = -EPIPE;
1722                         goto error;
1723                 }
1724                 ((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
1725                 ((__le16 *) buf)[1] =
1726                         cpu_to_le16(max3421_hcd->port_status >> 16);
1727                 break;
1728
1729         case SetHubFeature:
1730                 retval = -EPIPE;
1731                 break;
1732
1733         case SetPortFeature:
1734                 switch (value) {
1735                 case USB_PORT_FEAT_LINK_STATE:
1736                 case USB_PORT_FEAT_U1_TIMEOUT:
1737                 case USB_PORT_FEAT_U2_TIMEOUT:
1738                 case USB_PORT_FEAT_BH_PORT_RESET:
1739                         goto error;
1740                 case USB_PORT_FEAT_SUSPEND:
1741                         if (max3421_hcd->active)
1742                                 max3421_hcd->port_status |=
1743                                         USB_PORT_STAT_SUSPEND;
1744                         break;
1745                 case USB_PORT_FEAT_POWER:
1746                         dev_dbg(hcd->self.controller, "power-on\n");
1747                         max3421_hcd->port_status |= USB_PORT_STAT_POWER;
1748                         max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1749                                                 pdata->vbus_active_level);
1750                         break;
1751                 case USB_PORT_FEAT_RESET:
1752                         max3421_reset_port(hcd);
1753                         /* FALLS THROUGH */
1754                 default:
1755                         if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
1756                             != 0)
1757                                 max3421_hcd->port_status |= (1 << value);
1758                 }
1759                 break;
1760
1761         default:
1762                 dev_dbg(hcd->self.controller,
1763                         "hub control req%04x v%04x i%04x l%d\n",
1764                         type_req, value, index, length);
1765 error:          /* "protocol stall" on error */
1766                 retval = -EPIPE;
1767         }
1768
1769         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1770         return retval;
1771 }
1772
1773 static int
1774 max3421_bus_suspend(struct usb_hcd *hcd)
1775 {
1776         return -1;
1777 }
1778
1779 static int
1780 max3421_bus_resume(struct usb_hcd *hcd)
1781 {
1782         return -1;
1783 }
1784
1785 /*
1786  * The SPI driver already takes care of DMA-mapping/unmapping, so no
1787  * reason to do it twice.
1788  */
1789 static int
1790 max3421_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1791 {
1792         return 0;
1793 }
1794
1795 static void
1796 max3421_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1797 {
1798 }
1799
1800 static struct hc_driver max3421_hcd_desc = {
1801         .description =          "max3421",
1802         .product_desc =         DRIVER_DESC,
1803         .hcd_priv_size =        sizeof(struct max3421_hcd),
1804         .flags =                HCD_USB11,
1805         .reset =                max3421_reset,
1806         .start =                max3421_start,
1807         .stop =                 max3421_stop,
1808         .get_frame_number =     max3421_get_frame_number,
1809         .urb_enqueue =          max3421_urb_enqueue,
1810         .urb_dequeue =          max3421_urb_dequeue,
1811         .map_urb_for_dma =      max3421_map_urb_for_dma,
1812         .unmap_urb_for_dma =    max3421_unmap_urb_for_dma,
1813         .endpoint_disable =     max3421_endpoint_disable,
1814         .hub_status_data =      max3421_hub_status_data,
1815         .hub_control =          max3421_hub_control,
1816         .bus_suspend =          max3421_bus_suspend,
1817         .bus_resume =           max3421_bus_resume,
1818 };
1819
1820 static int
1821 max3421_probe(struct spi_device *spi)
1822 {
1823         struct max3421_hcd *max3421_hcd;
1824         struct usb_hcd *hcd = NULL;
1825         int retval = -ENOMEM;
1826
1827         if (spi_setup(spi) < 0) {
1828                 dev_err(&spi->dev, "Unable to setup SPI bus");
1829                 return -EFAULT;
1830         }
1831
1832         hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
1833                              dev_name(&spi->dev));
1834         if (!hcd) {
1835                 dev_err(&spi->dev, "failed to create HCD structure\n");
1836                 goto error;
1837         }
1838         set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1839         max3421_hcd = hcd_to_max3421(hcd);
1840         INIT_LIST_HEAD(&max3421_hcd->ep_list);
1841         spi_set_drvdata(spi, max3421_hcd);
1842
1843         max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL);
1844         if (!max3421_hcd->tx) {
1845                 dev_err(&spi->dev, "failed to kmalloc tx buffer\n");
1846                 goto error;
1847         }
1848         max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL);
1849         if (!max3421_hcd->rx) {
1850                 dev_err(&spi->dev, "failed to kmalloc rx buffer\n");
1851                 goto error;
1852         }
1853
1854         max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
1855                                               "max3421_spi_thread");
1856         if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
1857                 dev_err(&spi->dev,
1858                         "failed to create SPI thread (out of memory)\n");
1859                 goto error;
1860         }
1861
1862         retval = usb_add_hcd(hcd, 0, 0);
1863         if (retval) {
1864                 dev_err(&spi->dev, "failed to add HCD\n");
1865                 goto error;
1866         }
1867
1868         retval = request_irq(spi->irq, max3421_irq_handler,
1869                              IRQF_TRIGGER_LOW, "max3421", hcd);
1870         if (retval < 0) {
1871                 dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
1872                 goto error;
1873         }
1874         return 0;
1875
1876 error:
1877         if (hcd) {
1878                 kfree(max3421_hcd->tx);
1879                 kfree(max3421_hcd->rx);
1880                 if (max3421_hcd->spi_thread)
1881                         kthread_stop(max3421_hcd->spi_thread);
1882                 usb_put_hcd(hcd);
1883         }
1884         return retval;
1885 }
1886
1887 static int
1888 max3421_remove(struct spi_device *spi)
1889 {
1890         struct max3421_hcd *max3421_hcd;
1891         struct usb_hcd *hcd;
1892         unsigned long flags;
1893
1894         max3421_hcd = spi_get_drvdata(spi);
1895         hcd = max3421_to_hcd(max3421_hcd);
1896
1897         usb_remove_hcd(hcd);
1898
1899         spin_lock_irqsave(&max3421_hcd->lock, flags);
1900
1901         kthread_stop(max3421_hcd->spi_thread);
1902
1903         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1904
1905         free_irq(spi->irq, hcd);
1906
1907         usb_put_hcd(hcd);
1908         return 0;
1909 }
1910
1911 static struct spi_driver max3421_driver = {
1912         .probe          = max3421_probe,
1913         .remove         = max3421_remove,
1914         .driver         = {
1915                 .name   = "max3421-hcd",
1916         },
1917 };
1918
1919 module_spi_driver(max3421_driver);
1920
1921 MODULE_DESCRIPTION(DRIVER_DESC);
1922 MODULE_AUTHOR("David Mosberger <davidm@egauge.net>");
1923 MODULE_LICENSE("GPL");