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