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