Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * MAX3421 Host Controller driver for USB.
0004  *
0005  * Author: David Mosberger-Tang <davidm@egauge.net>
0006  *
0007  * (C) Copyright 2014 David Mosberger-Tang <davidm@egauge.net>
0008  *
0009  * MAX3421 is a chip implementing a USB 2.0 Full-/Low-Speed host
0010  * controller on a SPI bus.
0011  *
0012  * Based on:
0013  *  o MAX3421E datasheet
0014  *      https://datasheets.maximintegrated.com/en/ds/MAX3421E.pdf
0015  *  o MAX3421E Programming Guide
0016  *      https://www.hdl.co.jp/ftpdata/utl-001/AN3785.pdf
0017  *  o gadget/dummy_hcd.c
0018  *      For USB HCD implementation.
0019  *  o Arduino MAX3421 driver
0020  *       https://github.com/felis/USB_Host_Shield_2.0/blob/master/Usb.cpp
0021  *
0022  * This file is licenced under the GPL v2.
0023  *
0024  * Important note on worst-case (full-speed) packet size constraints
0025  * (See USB 2.0 Section 5.6.3 and following):
0026  *
0027  *  - control:    64 bytes
0028  *  - isochronous:  1023 bytes
0029  *  - interrupt:      64 bytes
0030  *  - bulk:       64 bytes
0031  *
0032  * Since the MAX3421 FIFO size is 64 bytes, we do not have to work about
0033  * multi-FIFO writes/reads for a single USB packet *except* for isochronous
0034  * transfers.  We don't support isochronous transfers at this time, so we
0035  * just assume that a USB packet always fits into a single FIFO buffer.
0036  *
0037  * NOTE: The June 2006 version of "MAX3421E Programming Guide"
0038  * (AN3785) has conflicting info for the RCVDAVIRQ bit:
0039  *
0040  *  The description of RCVDAVIRQ says "The CPU *must* clear
0041  *  this IRQ bit (by writing a 1 to it) before reading the
0042  *  RCVFIFO data.
0043  *
0044  * However, the earlier section on "Programming BULK-IN
0045  * Transfers" says * that:
0046  *
0047  *  After the CPU retrieves the data, it clears the
0048  *  RCVDAVIRQ bit.
0049  *
0050  * The December 2006 version has been corrected and it consistently
0051  * states the second behavior is the correct one.
0052  *
0053  * Synchronous SPI transactions sleep so we can't perform any such
0054  * transactions while holding a spin-lock (and/or while interrupts are
0055  * masked).  To achieve this, all SPI transactions are issued from a
0056  * single thread (max3421_spi_thread).
0057  */
0058 
0059 #include <linux/jiffies.h>
0060 #include <linux/module.h>
0061 #include <linux/spi/spi.h>
0062 #include <linux/usb.h>
0063 #include <linux/usb/hcd.h>
0064 #include <linux/of.h>
0065 
0066 #include <linux/platform_data/max3421-hcd.h>
0067 
0068 #define DRIVER_DESC "MAX3421 USB Host-Controller Driver"
0069 #define DRIVER_VERSION  "1.0"
0070 
0071 /* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
0072 #define USB_MAX_FRAME_NUMBER    0x7ff
0073 #define USB_MAX_RETRIES     3 /* # of retries before error is reported */
0074 
0075 /*
0076  * Max. # of times we're willing to retransmit a request immediately in
0077  * resposne to a NAK.  Afterwards, we fall back on trying once a frame.
0078  */
0079 #define NAK_MAX_FAST_RETRANSMITS    2
0080 
0081 #define POWER_BUDGET    500 /* in mA; use 8 for low-power port testing */
0082 
0083 /* Port-change mask: */
0084 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION |  \
0085               USB_PORT_STAT_C_ENABLE |  \
0086               USB_PORT_STAT_C_SUSPEND | \
0087               USB_PORT_STAT_C_OVERCURRENT | \
0088               USB_PORT_STAT_C_RESET) << 16)
0089 
0090 #define MAX3421_GPOUT_COUNT 8
0091 
0092 enum max3421_rh_state {
0093     MAX3421_RH_RESET,
0094     MAX3421_RH_SUSPENDED,
0095     MAX3421_RH_RUNNING
0096 };
0097 
0098 enum pkt_state {
0099     PKT_STATE_SETUP,    /* waiting to send setup packet to ctrl pipe */
0100     PKT_STATE_TRANSFER, /* waiting to xfer transfer_buffer */
0101     PKT_STATE_TERMINATE /* waiting to terminate control transfer */
0102 };
0103 
0104 enum scheduling_pass {
0105     SCHED_PASS_PERIODIC,
0106     SCHED_PASS_NON_PERIODIC,
0107     SCHED_PASS_DONE
0108 };
0109 
0110 /* Bit numbers for max3421_hcd->todo: */
0111 enum {
0112     ENABLE_IRQ = 0,
0113     RESET_HCD,
0114     RESET_PORT,
0115     CHECK_UNLINK,
0116     IOPIN_UPDATE
0117 };
0118 
0119 struct max3421_dma_buf {
0120     u8 data[2];
0121 };
0122 
0123 struct max3421_hcd {
0124     spinlock_t lock;
0125 
0126     struct task_struct *spi_thread;
0127 
0128     enum max3421_rh_state rh_state;
0129     /* lower 16 bits contain port status, upper 16 bits the change mask: */
0130     u32 port_status;
0131 
0132     unsigned active:1;
0133 
0134     struct list_head ep_list;   /* list of EP's with work */
0135 
0136     /*
0137      * The following are owned by spi_thread (may be accessed by
0138      * SPI-thread without acquiring the HCD lock:
0139      */
0140     u8 rev;             /* chip revision */
0141     u16 frame_number;
0142     /*
0143      * kmalloc'd buffers guaranteed to be in separate (DMA)
0144      * cache-lines:
0145      */
0146     struct max3421_dma_buf *tx;
0147     struct max3421_dma_buf *rx;
0148     /*
0149      * URB we're currently processing.  Must not be reset to NULL
0150      * unless MAX3421E chip is idle:
0151      */
0152     struct urb *curr_urb;
0153     enum scheduling_pass sched_pass;
0154     int urb_done;           /* > 0 -> no errors, < 0: errno */
0155     size_t curr_len;
0156     u8 hien;
0157     u8 mode;
0158     u8 iopins[2];
0159     unsigned long todo;
0160 #ifdef DEBUG
0161     unsigned long err_stat[16];
0162 #endif
0163 };
0164 
0165 struct max3421_ep {
0166     struct usb_host_endpoint *ep;
0167     struct list_head ep_list;
0168     u32 naks;
0169     u16 last_active;        /* frame # this ep was last active */
0170     enum pkt_state pkt_state;
0171     u8 retries;
0172     u8 retransmit;          /* packet needs retransmission */
0173 };
0174 
0175 #define MAX3421_FIFO_SIZE   64
0176 
0177 #define MAX3421_SPI_DIR_RD  0   /* read register from MAX3421 */
0178 #define MAX3421_SPI_DIR_WR  1   /* write register to MAX3421 */
0179 
0180 /* SPI commands: */
0181 #define MAX3421_SPI_DIR_SHIFT   1
0182 #define MAX3421_SPI_REG_SHIFT   3
0183 
0184 #define MAX3421_REG_RCVFIFO 1
0185 #define MAX3421_REG_SNDFIFO 2
0186 #define MAX3421_REG_SUDFIFO 4
0187 #define MAX3421_REG_RCVBC   6
0188 #define MAX3421_REG_SNDBC   7
0189 #define MAX3421_REG_USBIRQ  13
0190 #define MAX3421_REG_USBIEN  14
0191 #define MAX3421_REG_USBCTL  15
0192 #define MAX3421_REG_CPUCTL  16
0193 #define MAX3421_REG_PINCTL  17
0194 #define MAX3421_REG_REVISION    18
0195 #define MAX3421_REG_IOPINS1 20
0196 #define MAX3421_REG_IOPINS2 21
0197 #define MAX3421_REG_GPINIRQ 22
0198 #define MAX3421_REG_GPINIEN 23
0199 #define MAX3421_REG_GPINPOL 24
0200 #define MAX3421_REG_HIRQ    25
0201 #define MAX3421_REG_HIEN    26
0202 #define MAX3421_REG_MODE    27
0203 #define MAX3421_REG_PERADDR 28
0204 #define MAX3421_REG_HCTL    29
0205 #define MAX3421_REG_HXFR    30
0206 #define MAX3421_REG_HRSL    31
0207 
0208 enum {
0209     MAX3421_USBIRQ_OSCOKIRQ_BIT = 0,
0210     MAX3421_USBIRQ_NOVBUSIRQ_BIT = 5,
0211     MAX3421_USBIRQ_VBUSIRQ_BIT
0212 };
0213 
0214 enum {
0215     MAX3421_CPUCTL_IE_BIT = 0,
0216     MAX3421_CPUCTL_PULSEWID0_BIT = 6,
0217     MAX3421_CPUCTL_PULSEWID1_BIT
0218 };
0219 
0220 enum {
0221     MAX3421_USBCTL_PWRDOWN_BIT = 4,
0222     MAX3421_USBCTL_CHIPRES_BIT
0223 };
0224 
0225 enum {
0226     MAX3421_PINCTL_GPXA_BIT = 0,
0227     MAX3421_PINCTL_GPXB_BIT,
0228     MAX3421_PINCTL_POSINT_BIT,
0229     MAX3421_PINCTL_INTLEVEL_BIT,
0230     MAX3421_PINCTL_FDUPSPI_BIT,
0231     MAX3421_PINCTL_EP0INAK_BIT,
0232     MAX3421_PINCTL_EP2INAK_BIT,
0233     MAX3421_PINCTL_EP3INAK_BIT,
0234 };
0235 
0236 enum {
0237     MAX3421_HI_BUSEVENT_BIT = 0,    /* bus-reset/-resume */
0238     MAX3421_HI_RWU_BIT,     /* remote wakeup */
0239     MAX3421_HI_RCVDAV_BIT,      /* receive FIFO data available */
0240     MAX3421_HI_SNDBAV_BIT,      /* send buffer available */
0241     MAX3421_HI_SUSDN_BIT,       /* suspend operation done */
0242     MAX3421_HI_CONDET_BIT,      /* peripheral connect/disconnect */
0243     MAX3421_HI_FRAME_BIT,       /* frame generator */
0244     MAX3421_HI_HXFRDN_BIT,      /* host transfer done */
0245 };
0246 
0247 enum {
0248     MAX3421_HCTL_BUSRST_BIT = 0,
0249     MAX3421_HCTL_FRMRST_BIT,
0250     MAX3421_HCTL_SAMPLEBUS_BIT,
0251     MAX3421_HCTL_SIGRSM_BIT,
0252     MAX3421_HCTL_RCVTOG0_BIT,
0253     MAX3421_HCTL_RCVTOG1_BIT,
0254     MAX3421_HCTL_SNDTOG0_BIT,
0255     MAX3421_HCTL_SNDTOG1_BIT
0256 };
0257 
0258 enum {
0259     MAX3421_MODE_HOST_BIT = 0,
0260     MAX3421_MODE_LOWSPEED_BIT,
0261     MAX3421_MODE_HUBPRE_BIT,
0262     MAX3421_MODE_SOFKAENAB_BIT,
0263     MAX3421_MODE_SEPIRQ_BIT,
0264     MAX3421_MODE_DELAYISO_BIT,
0265     MAX3421_MODE_DMPULLDN_BIT,
0266     MAX3421_MODE_DPPULLDN_BIT
0267 };
0268 
0269 enum {
0270     MAX3421_HRSL_OK = 0,
0271     MAX3421_HRSL_BUSY,
0272     MAX3421_HRSL_BADREQ,
0273     MAX3421_HRSL_UNDEF,
0274     MAX3421_HRSL_NAK,
0275     MAX3421_HRSL_STALL,
0276     MAX3421_HRSL_TOGERR,
0277     MAX3421_HRSL_WRONGPID,
0278     MAX3421_HRSL_BADBC,
0279     MAX3421_HRSL_PIDERR,
0280     MAX3421_HRSL_PKTERR,
0281     MAX3421_HRSL_CRCERR,
0282     MAX3421_HRSL_KERR,
0283     MAX3421_HRSL_JERR,
0284     MAX3421_HRSL_TIMEOUT,
0285     MAX3421_HRSL_BABBLE,
0286     MAX3421_HRSL_RESULT_MASK = 0xf,
0287     MAX3421_HRSL_RCVTOGRD_BIT = 4,
0288     MAX3421_HRSL_SNDTOGRD_BIT,
0289     MAX3421_HRSL_KSTATUS_BIT,
0290     MAX3421_HRSL_JSTATUS_BIT
0291 };
0292 
0293 /* Return same error-codes as ohci.h:cc_to_error: */
0294 static const int hrsl_to_error[] = {
0295     [MAX3421_HRSL_OK] =     0,
0296     [MAX3421_HRSL_BUSY] =       -EINVAL,
0297     [MAX3421_HRSL_BADREQ] =     -EINVAL,
0298     [MAX3421_HRSL_UNDEF] =      -EINVAL,
0299     [MAX3421_HRSL_NAK] =        -EAGAIN,
0300     [MAX3421_HRSL_STALL] =      -EPIPE,
0301     [MAX3421_HRSL_TOGERR] =     -EILSEQ,
0302     [MAX3421_HRSL_WRONGPID] =   -EPROTO,
0303     [MAX3421_HRSL_BADBC] =      -EREMOTEIO,
0304     [MAX3421_HRSL_PIDERR] =     -EPROTO,
0305     [MAX3421_HRSL_PKTERR] =     -EPROTO,
0306     [MAX3421_HRSL_CRCERR] =     -EILSEQ,
0307     [MAX3421_HRSL_KERR] =       -EIO,
0308     [MAX3421_HRSL_JERR] =       -EIO,
0309     [MAX3421_HRSL_TIMEOUT] =    -ETIME,
0310     [MAX3421_HRSL_BABBLE] =     -EOVERFLOW
0311 };
0312 
0313 /*
0314  * See https://www.beyondlogic.org/usbnutshell/usb4.shtml#Control for a
0315  * reasonable overview of how control transfers use the IN/OUT
0316  * tokens.
0317  */
0318 #define MAX3421_HXFR_BULK_IN(ep)    (0x00 | (ep))   /* bulk or interrupt */
0319 #define MAX3421_HXFR_SETUP       0x10
0320 #define MAX3421_HXFR_BULK_OUT(ep)   (0x20 | (ep))   /* bulk or interrupt */
0321 #define MAX3421_HXFR_ISO_IN(ep)     (0x40 | (ep))
0322 #define MAX3421_HXFR_ISO_OUT(ep)    (0x60 | (ep))
0323 #define MAX3421_HXFR_HS_IN       0x80       /* handshake in */
0324 #define MAX3421_HXFR_HS_OUT      0xa0       /* handshake out */
0325 
0326 #define field(val, bit) ((val) << (bit))
0327 
0328 static inline s16
0329 frame_diff(u16 left, u16 right)
0330 {
0331     return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1);
0332 }
0333 
0334 static inline struct max3421_hcd *
0335 hcd_to_max3421(struct usb_hcd *hcd)
0336 {
0337     return (struct max3421_hcd *) hcd->hcd_priv;
0338 }
0339 
0340 static inline struct usb_hcd *
0341 max3421_to_hcd(struct max3421_hcd *max3421_hcd)
0342 {
0343     return container_of((void *) max3421_hcd, struct usb_hcd, hcd_priv);
0344 }
0345 
0346 static u8
0347 spi_rd8(struct usb_hcd *hcd, unsigned int reg)
0348 {
0349     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0350     struct spi_device *spi = to_spi_device(hcd->self.controller);
0351     struct spi_transfer transfer;
0352     struct spi_message msg;
0353 
0354     memset(&transfer, 0, sizeof(transfer));
0355 
0356     spi_message_init(&msg);
0357 
0358     max3421_hcd->tx->data[0] =
0359         (field(reg, MAX3421_SPI_REG_SHIFT) |
0360          field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
0361 
0362     transfer.tx_buf = max3421_hcd->tx->data;
0363     transfer.rx_buf = max3421_hcd->rx->data;
0364     transfer.len = 2;
0365 
0366     spi_message_add_tail(&transfer, &msg);
0367     spi_sync(spi, &msg);
0368 
0369     return max3421_hcd->rx->data[1];
0370 }
0371 
0372 static void
0373 spi_wr8(struct usb_hcd *hcd, unsigned int reg, u8 val)
0374 {
0375     struct spi_device *spi = to_spi_device(hcd->self.controller);
0376     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0377     struct spi_transfer transfer;
0378     struct spi_message msg;
0379 
0380     memset(&transfer, 0, sizeof(transfer));
0381 
0382     spi_message_init(&msg);
0383 
0384     max3421_hcd->tx->data[0] =
0385         (field(reg, MAX3421_SPI_REG_SHIFT) |
0386          field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
0387     max3421_hcd->tx->data[1] = val;
0388 
0389     transfer.tx_buf = max3421_hcd->tx->data;
0390     transfer.len = 2;
0391 
0392     spi_message_add_tail(&transfer, &msg);
0393     spi_sync(spi, &msg);
0394 }
0395 
0396 static void
0397 spi_rd_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
0398 {
0399     struct spi_device *spi = to_spi_device(hcd->self.controller);
0400     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0401     struct spi_transfer transfer[2];
0402     struct spi_message msg;
0403 
0404     memset(transfer, 0, sizeof(transfer));
0405 
0406     spi_message_init(&msg);
0407 
0408     max3421_hcd->tx->data[0] =
0409         (field(reg, MAX3421_SPI_REG_SHIFT) |
0410          field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
0411     transfer[0].tx_buf = max3421_hcd->tx->data;
0412     transfer[0].len = 1;
0413 
0414     transfer[1].rx_buf = buf;
0415     transfer[1].len = len;
0416 
0417     spi_message_add_tail(&transfer[0], &msg);
0418     spi_message_add_tail(&transfer[1], &msg);
0419     spi_sync(spi, &msg);
0420 }
0421 
0422 static void
0423 spi_wr_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
0424 {
0425     struct spi_device *spi = to_spi_device(hcd->self.controller);
0426     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0427     struct spi_transfer transfer[2];
0428     struct spi_message msg;
0429 
0430     memset(transfer, 0, sizeof(transfer));
0431 
0432     spi_message_init(&msg);
0433 
0434     max3421_hcd->tx->data[0] =
0435         (field(reg, MAX3421_SPI_REG_SHIFT) |
0436          field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
0437 
0438     transfer[0].tx_buf = max3421_hcd->tx->data;
0439     transfer[0].len = 1;
0440 
0441     transfer[1].tx_buf = buf;
0442     transfer[1].len = len;
0443 
0444     spi_message_add_tail(&transfer[0], &msg);
0445     spi_message_add_tail(&transfer[1], &msg);
0446     spi_sync(spi, &msg);
0447 }
0448 
0449 /*
0450  * Figure out the correct setting for the LOWSPEED and HUBPRE mode
0451  * bits.  The HUBPRE bit needs to be set when MAX3421E operates at
0452  * full speed, but it's talking to a low-speed device (i.e., through a
0453  * hub).  Setting that bit ensures that every low-speed packet is
0454  * preceded by a full-speed PRE PID.  Possible configurations:
0455  *
0456  * Hub speed:   Device speed:   =>  LOWSPEED bit:   HUBPRE bit:
0457  *  FULL    FULL        =>  0       0
0458  *  FULL    LOW     =>  1       1
0459  *  LOW LOW     =>  1       0
0460  *  LOW FULL        =>  1       0
0461  */
0462 static void
0463 max3421_set_speed(struct usb_hcd *hcd, struct usb_device *dev)
0464 {
0465     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0466     u8 mode_lowspeed, mode_hubpre, mode = max3421_hcd->mode;
0467 
0468     mode_lowspeed = BIT(MAX3421_MODE_LOWSPEED_BIT);
0469     mode_hubpre   = BIT(MAX3421_MODE_HUBPRE_BIT);
0470     if (max3421_hcd->port_status & USB_PORT_STAT_LOW_SPEED) {
0471         mode |=  mode_lowspeed;
0472         mode &= ~mode_hubpre;
0473     } else if (dev->speed == USB_SPEED_LOW) {
0474         mode |= mode_lowspeed | mode_hubpre;
0475     } else {
0476         mode &= ~(mode_lowspeed | mode_hubpre);
0477     }
0478     if (mode != max3421_hcd->mode) {
0479         max3421_hcd->mode = mode;
0480         spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
0481     }
0482 
0483 }
0484 
0485 /*
0486  * Caller must NOT hold HCD spinlock.
0487  */
0488 static void
0489 max3421_set_address(struct usb_hcd *hcd, struct usb_device *dev, int epnum)
0490 {
0491     int rcvtog, sndtog;
0492     u8 hctl;
0493 
0494     /* setup new endpoint's toggle bits: */
0495     rcvtog = usb_gettoggle(dev, epnum, 0);
0496     sndtog = usb_gettoggle(dev, epnum, 1);
0497     hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) |
0498         BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
0499 
0500     spi_wr8(hcd, MAX3421_REG_HCTL, hctl);
0501 
0502     /*
0503      * Note: devnum for one and the same device can change during
0504      * address-assignment so it's best to just always load the
0505      * address whenever the end-point changed/was forced.
0506      */
0507     spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum);
0508 }
0509 
0510 static int
0511 max3421_ctrl_setup(struct usb_hcd *hcd, struct urb *urb)
0512 {
0513     spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8);
0514     return MAX3421_HXFR_SETUP;
0515 }
0516 
0517 static int
0518 max3421_transfer_in(struct usb_hcd *hcd, struct urb *urb)
0519 {
0520     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0521     int epnum = usb_pipeendpoint(urb->pipe);
0522 
0523     max3421_hcd->curr_len = 0;
0524     max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT);
0525     return MAX3421_HXFR_BULK_IN(epnum);
0526 }
0527 
0528 static int
0529 max3421_transfer_out(struct usb_hcd *hcd, struct urb *urb, int fast_retransmit)
0530 {
0531     struct spi_device *spi = to_spi_device(hcd->self.controller);
0532     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0533     int epnum = usb_pipeendpoint(urb->pipe);
0534     u32 max_packet;
0535     void *src;
0536 
0537     src = urb->transfer_buffer + urb->actual_length;
0538 
0539     if (fast_retransmit) {
0540         if (max3421_hcd->rev == 0x12) {
0541             /* work around rev 0x12 bug: */
0542             spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
0543             spi_wr8(hcd, MAX3421_REG_SNDFIFO, ((u8 *) src)[0]);
0544             spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
0545         }
0546         return MAX3421_HXFR_BULK_OUT(epnum);
0547     }
0548 
0549     max_packet = usb_maxpacket(urb->dev, urb->pipe);
0550 
0551     if (max_packet > MAX3421_FIFO_SIZE) {
0552         /*
0553          * We do not support isochronous transfers at this
0554          * time.
0555          */
0556         dev_err(&spi->dev,
0557             "%s: packet-size of %u too big (limit is %u bytes)",
0558             __func__, max_packet, MAX3421_FIFO_SIZE);
0559         max3421_hcd->urb_done = -EMSGSIZE;
0560         return -EMSGSIZE;
0561     }
0562     max3421_hcd->curr_len = min((urb->transfer_buffer_length -
0563                      urb->actual_length), max_packet);
0564 
0565     spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len);
0566     spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
0567     return MAX3421_HXFR_BULK_OUT(epnum);
0568 }
0569 
0570 /*
0571  * Issue the next host-transfer command.
0572  * Caller must NOT hold HCD spinlock.
0573  */
0574 static void
0575 max3421_next_transfer(struct usb_hcd *hcd, int fast_retransmit)
0576 {
0577     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0578     struct urb *urb = max3421_hcd->curr_urb;
0579     struct max3421_ep *max3421_ep;
0580     int cmd = -EINVAL;
0581 
0582     if (!urb)
0583         return; /* nothing to do */
0584 
0585     max3421_ep = urb->ep->hcpriv;
0586 
0587     switch (max3421_ep->pkt_state) {
0588     case PKT_STATE_SETUP:
0589         cmd = max3421_ctrl_setup(hcd, urb);
0590         break;
0591 
0592     case PKT_STATE_TRANSFER:
0593         if (usb_urb_dir_in(urb))
0594             cmd = max3421_transfer_in(hcd, urb);
0595         else
0596             cmd = max3421_transfer_out(hcd, urb, fast_retransmit);
0597         break;
0598 
0599     case PKT_STATE_TERMINATE:
0600         /*
0601          * IN transfers are terminated with HS_OUT token,
0602          * OUT transfers with HS_IN:
0603          */
0604         if (usb_urb_dir_in(urb))
0605             cmd = MAX3421_HXFR_HS_OUT;
0606         else
0607             cmd = MAX3421_HXFR_HS_IN;
0608         break;
0609     }
0610 
0611     if (cmd < 0)
0612         return;
0613 
0614     /* issue the command and wait for host-xfer-done interrupt: */
0615 
0616     spi_wr8(hcd, MAX3421_REG_HXFR, cmd);
0617     max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
0618 }
0619 
0620 /*
0621  * Find the next URB to process and start its execution.
0622  *
0623  * At this time, we do not anticipate ever connecting a USB hub to the
0624  * MAX3421 chip, so at most USB device can be connected and we can use
0625  * a simplistic scheduler: at the start of a frame, schedule all
0626  * periodic transfers.  Once that is done, use the remainder of the
0627  * frame to process non-periodic (bulk & control) transfers.
0628  *
0629  * Preconditions:
0630  * o Caller must NOT hold HCD spinlock.
0631  * o max3421_hcd->curr_urb MUST BE NULL.
0632  * o MAX3421E chip must be idle.
0633  */
0634 static int
0635 max3421_select_and_start_urb(struct usb_hcd *hcd)
0636 {
0637     struct spi_device *spi = to_spi_device(hcd->self.controller);
0638     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0639     struct urb *urb, *curr_urb = NULL;
0640     struct max3421_ep *max3421_ep;
0641     int epnum;
0642     struct usb_host_endpoint *ep;
0643     struct list_head *pos;
0644     unsigned long flags;
0645 
0646     spin_lock_irqsave(&max3421_hcd->lock, flags);
0647 
0648     for (;
0649          max3421_hcd->sched_pass < SCHED_PASS_DONE;
0650          ++max3421_hcd->sched_pass)
0651         list_for_each(pos, &max3421_hcd->ep_list) {
0652             urb = NULL;
0653             max3421_ep = container_of(pos, struct max3421_ep,
0654                           ep_list);
0655             ep = max3421_ep->ep;
0656 
0657             switch (usb_endpoint_type(&ep->desc)) {
0658             case USB_ENDPOINT_XFER_ISOC:
0659             case USB_ENDPOINT_XFER_INT:
0660                 if (max3421_hcd->sched_pass !=
0661                     SCHED_PASS_PERIODIC)
0662                     continue;
0663                 break;
0664 
0665             case USB_ENDPOINT_XFER_CONTROL:
0666             case USB_ENDPOINT_XFER_BULK:
0667                 if (max3421_hcd->sched_pass !=
0668                     SCHED_PASS_NON_PERIODIC)
0669                     continue;
0670                 break;
0671             }
0672 
0673             if (list_empty(&ep->urb_list))
0674                 continue;   /* nothing to do */
0675             urb = list_first_entry(&ep->urb_list, struct urb,
0676                            urb_list);
0677             if (urb->unlinked) {
0678                 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
0679                     __func__, urb, urb->unlinked);
0680                 max3421_hcd->curr_urb = urb;
0681                 max3421_hcd->urb_done = 1;
0682                 spin_unlock_irqrestore(&max3421_hcd->lock,
0683                                flags);
0684                 return 1;
0685             }
0686 
0687             switch (usb_endpoint_type(&ep->desc)) {
0688             case USB_ENDPOINT_XFER_CONTROL:
0689                 /*
0690                  * Allow one control transaction per
0691                  * frame per endpoint:
0692                  */
0693                 if (frame_diff(max3421_ep->last_active,
0694                            max3421_hcd->frame_number) == 0)
0695                     continue;
0696                 break;
0697 
0698             case USB_ENDPOINT_XFER_BULK:
0699                 if (max3421_ep->retransmit
0700                     && (frame_diff(max3421_ep->last_active,
0701                            max3421_hcd->frame_number)
0702                     == 0))
0703                     /*
0704                      * We already tried this EP
0705                      * during this frame and got a
0706                      * NAK or error; wait for next frame
0707                      */
0708                     continue;
0709                 break;
0710 
0711             case USB_ENDPOINT_XFER_ISOC:
0712             case USB_ENDPOINT_XFER_INT:
0713                 if (frame_diff(max3421_hcd->frame_number,
0714                            max3421_ep->last_active)
0715                     < urb->interval)
0716                     /*
0717                      * We already processed this
0718                      * end-point in the current
0719                      * frame
0720                      */
0721                     continue;
0722                 break;
0723             }
0724 
0725             /* move current ep to tail: */
0726             list_move_tail(pos, &max3421_hcd->ep_list);
0727             curr_urb = urb;
0728             goto done;
0729         }
0730 done:
0731     if (!curr_urb) {
0732         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
0733         return 0;
0734     }
0735 
0736     urb = max3421_hcd->curr_urb = curr_urb;
0737     epnum = usb_endpoint_num(&urb->ep->desc);
0738     if (max3421_ep->retransmit)
0739         /* restart (part of) a USB transaction: */
0740         max3421_ep->retransmit = 0;
0741     else {
0742         /* start USB transaction: */
0743         if (usb_endpoint_xfer_control(&ep->desc)) {
0744             /*
0745              * See USB 2.0 spec section 8.6.1
0746              * Initialization via SETUP Token:
0747              */
0748             usb_settoggle(urb->dev, epnum, 0, 1);
0749             usb_settoggle(urb->dev, epnum, 1, 1);
0750             max3421_ep->pkt_state = PKT_STATE_SETUP;
0751         } else
0752             max3421_ep->pkt_state = PKT_STATE_TRANSFER;
0753     }
0754 
0755     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
0756 
0757     max3421_ep->last_active = max3421_hcd->frame_number;
0758     max3421_set_address(hcd, urb->dev, epnum);
0759     max3421_set_speed(hcd, urb->dev);
0760     max3421_next_transfer(hcd, 0);
0761     return 1;
0762 }
0763 
0764 /*
0765  * Check all endpoints for URBs that got unlinked.
0766  *
0767  * Caller must NOT hold HCD spinlock.
0768  */
0769 static int
0770 max3421_check_unlink(struct usb_hcd *hcd)
0771 {
0772     struct spi_device *spi = to_spi_device(hcd->self.controller);
0773     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0774     struct max3421_ep *max3421_ep;
0775     struct usb_host_endpoint *ep;
0776     struct urb *urb, *next;
0777     unsigned long flags;
0778     int retval = 0;
0779 
0780     spin_lock_irqsave(&max3421_hcd->lock, flags);
0781     list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
0782         ep = max3421_ep->ep;
0783         list_for_each_entry_safe(urb, next, &ep->urb_list, urb_list) {
0784             if (urb->unlinked) {
0785                 retval = 1;
0786                 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
0787                     __func__, urb, urb->unlinked);
0788                 usb_hcd_unlink_urb_from_ep(hcd, urb);
0789                 spin_unlock_irqrestore(&max3421_hcd->lock,
0790                                flags);
0791                 usb_hcd_giveback_urb(hcd, urb, 0);
0792                 spin_lock_irqsave(&max3421_hcd->lock, flags);
0793             }
0794         }
0795     }
0796     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
0797     return retval;
0798 }
0799 
0800 /*
0801  * Caller must NOT hold HCD spinlock.
0802  */
0803 static void
0804 max3421_slow_retransmit(struct usb_hcd *hcd)
0805 {
0806     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0807     struct urb *urb = max3421_hcd->curr_urb;
0808     struct max3421_ep *max3421_ep;
0809 
0810     max3421_ep = urb->ep->hcpriv;
0811     max3421_ep->retransmit = 1;
0812     max3421_hcd->curr_urb = NULL;
0813 }
0814 
0815 /*
0816  * Caller must NOT hold HCD spinlock.
0817  */
0818 static void
0819 max3421_recv_data_available(struct usb_hcd *hcd)
0820 {
0821     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0822     struct urb *urb = max3421_hcd->curr_urb;
0823     size_t remaining, transfer_size;
0824     u8 rcvbc;
0825 
0826     rcvbc = spi_rd8(hcd, MAX3421_REG_RCVBC);
0827 
0828     if (rcvbc > MAX3421_FIFO_SIZE)
0829         rcvbc = MAX3421_FIFO_SIZE;
0830     if (urb->actual_length >= urb->transfer_buffer_length)
0831         remaining = 0;
0832     else
0833         remaining = urb->transfer_buffer_length - urb->actual_length;
0834     transfer_size = rcvbc;
0835     if (transfer_size > remaining)
0836         transfer_size = remaining;
0837     if (transfer_size > 0) {
0838         void *dst = urb->transfer_buffer + urb->actual_length;
0839 
0840         spi_rd_buf(hcd, MAX3421_REG_RCVFIFO, dst, transfer_size);
0841         urb->actual_length += transfer_size;
0842         max3421_hcd->curr_len = transfer_size;
0843     }
0844 
0845     /* ack the RCVDAV irq now that the FIFO has been read: */
0846     spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT));
0847 }
0848 
0849 static void
0850 max3421_handle_error(struct usb_hcd *hcd, u8 hrsl)
0851 {
0852     struct spi_device *spi = to_spi_device(hcd->self.controller);
0853     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0854     u8 result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
0855     struct urb *urb = max3421_hcd->curr_urb;
0856     struct max3421_ep *max3421_ep = urb->ep->hcpriv;
0857     int switch_sndfifo;
0858 
0859     /*
0860      * If an OUT command results in any response other than OK
0861      * (i.e., error or NAK), we have to perform a dummy-write to
0862      * SNDBC so the FIFO gets switched back to us.  Otherwise, we
0863      * get out of sync with the SNDFIFO double buffer.
0864      */
0865     switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER &&
0866               usb_urb_dir_out(urb));
0867 
0868     switch (result_code) {
0869     case MAX3421_HRSL_OK:
0870         return;         /* this shouldn't happen */
0871 
0872     case MAX3421_HRSL_WRONGPID: /* received wrong PID */
0873     case MAX3421_HRSL_BUSY:     /* SIE busy */
0874     case MAX3421_HRSL_BADREQ:   /* bad val in HXFR */
0875     case MAX3421_HRSL_UNDEF:    /* reserved */
0876     case MAX3421_HRSL_KERR:     /* K-state instead of response */
0877     case MAX3421_HRSL_JERR:     /* J-state instead of response */
0878         /*
0879          * packet experienced an error that we cannot recover
0880          * from; report error
0881          */
0882         max3421_hcd->urb_done = hrsl_to_error[result_code];
0883         dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
0884             __func__, hrsl);
0885         break;
0886 
0887     case MAX3421_HRSL_TOGERR:
0888         if (usb_urb_dir_in(urb))
0889             ; /* don't do anything (device will switch toggle) */
0890         else {
0891             /* flip the send toggle bit: */
0892             int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
0893 
0894             sndtog ^= 1;
0895             spi_wr8(hcd, MAX3421_REG_HCTL,
0896                 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
0897         }
0898         fallthrough;
0899     case MAX3421_HRSL_BADBC:    /* bad byte count */
0900     case MAX3421_HRSL_PIDERR:   /* received PID is corrupted */
0901     case MAX3421_HRSL_PKTERR:   /* packet error (stuff, EOP) */
0902     case MAX3421_HRSL_CRCERR:   /* CRC error */
0903     case MAX3421_HRSL_BABBLE:   /* device talked too long */
0904     case MAX3421_HRSL_TIMEOUT:
0905         if (max3421_ep->retries++ < USB_MAX_RETRIES)
0906             /* retry the packet again in the next frame */
0907             max3421_slow_retransmit(hcd);
0908         else {
0909             /* Based on ohci.h cc_to_err[]: */
0910             max3421_hcd->urb_done = hrsl_to_error[result_code];
0911             dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
0912                 __func__, hrsl);
0913         }
0914         break;
0915 
0916     case MAX3421_HRSL_STALL:
0917         dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
0918             __func__, hrsl);
0919         max3421_hcd->urb_done = hrsl_to_error[result_code];
0920         break;
0921 
0922     case MAX3421_HRSL_NAK:
0923         /*
0924          * Device wasn't ready for data or has no data
0925          * available: retry the packet again.
0926          */
0927         if (max3421_ep->naks++ < NAK_MAX_FAST_RETRANSMITS) {
0928             max3421_next_transfer(hcd, 1);
0929             switch_sndfifo = 0;
0930         } else
0931             max3421_slow_retransmit(hcd);
0932         break;
0933     }
0934     if (switch_sndfifo)
0935         spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
0936 }
0937 
0938 /*
0939  * Caller must NOT hold HCD spinlock.
0940  */
0941 static int
0942 max3421_transfer_in_done(struct usb_hcd *hcd, struct urb *urb)
0943 {
0944     struct spi_device *spi = to_spi_device(hcd->self.controller);
0945     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0946     u32 max_packet;
0947 
0948     if (urb->actual_length >= urb->transfer_buffer_length)
0949         return 1;   /* read is complete, so we're done */
0950 
0951     /*
0952      * USB 2.0 Section 5.3.2 Pipes: packets must be full size
0953      * except for last one.
0954      */
0955     max_packet = usb_maxpacket(urb->dev, urb->pipe);
0956     if (max_packet > MAX3421_FIFO_SIZE) {
0957         /*
0958          * We do not support isochronous transfers at this
0959          * time...
0960          */
0961         dev_err(&spi->dev,
0962             "%s: packet-size of %u too big (limit is %u bytes)",
0963             __func__, max_packet, MAX3421_FIFO_SIZE);
0964         return -EINVAL;
0965     }
0966 
0967     if (max3421_hcd->curr_len < max_packet) {
0968         if (urb->transfer_flags & URB_SHORT_NOT_OK) {
0969             /*
0970              * remaining > 0 and received an
0971              * unexpected partial packet ->
0972              * error
0973              */
0974             return -EREMOTEIO;
0975         } else
0976             /* short read, but it's OK */
0977             return 1;
0978     }
0979     return 0;   /* not done */
0980 }
0981 
0982 /*
0983  * Caller must NOT hold HCD spinlock.
0984  */
0985 static int
0986 max3421_transfer_out_done(struct usb_hcd *hcd, struct urb *urb)
0987 {
0988     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
0989 
0990     urb->actual_length += max3421_hcd->curr_len;
0991     if (urb->actual_length < urb->transfer_buffer_length)
0992         return 0;
0993     if (urb->transfer_flags & URB_ZERO_PACKET) {
0994         /*
0995          * Some hardware needs a zero-size packet at the end
0996          * of a bulk-out transfer if the last transfer was a
0997          * full-sized packet (i.e., such hardware use <
0998          * max_packet as an indicator that the end of the
0999          * packet has been reached).
1000          */
1001         u32 max_packet = usb_maxpacket(urb->dev, urb->pipe);
1002 
1003         if (max3421_hcd->curr_len == max_packet)
1004             return 0;
1005     }
1006     return 1;
1007 }
1008 
1009 /*
1010  * Caller must NOT hold HCD spinlock.
1011  */
1012 static void
1013 max3421_host_transfer_done(struct usb_hcd *hcd)
1014 {
1015     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1016     struct urb *urb = max3421_hcd->curr_urb;
1017     struct max3421_ep *max3421_ep;
1018     u8 result_code, hrsl;
1019     int urb_done = 0;
1020 
1021     max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) |
1022                    BIT(MAX3421_HI_RCVDAV_BIT));
1023 
1024     hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1025     result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
1026 
1027 #ifdef DEBUG
1028     ++max3421_hcd->err_stat[result_code];
1029 #endif
1030 
1031     max3421_ep = urb->ep->hcpriv;
1032 
1033     if (unlikely(result_code != MAX3421_HRSL_OK)) {
1034         max3421_handle_error(hcd, hrsl);
1035         return;
1036     }
1037 
1038     max3421_ep->naks = 0;
1039     max3421_ep->retries = 0;
1040     switch (max3421_ep->pkt_state) {
1041 
1042     case PKT_STATE_SETUP:
1043         if (urb->transfer_buffer_length > 0)
1044             max3421_ep->pkt_state = PKT_STATE_TRANSFER;
1045         else
1046             max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1047         break;
1048 
1049     case PKT_STATE_TRANSFER:
1050         if (usb_urb_dir_in(urb))
1051             urb_done = max3421_transfer_in_done(hcd, urb);
1052         else
1053             urb_done = max3421_transfer_out_done(hcd, urb);
1054         if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1055             /*
1056              * We aren't really done - we still need to
1057              * terminate the control transfer:
1058              */
1059             max3421_hcd->urb_done = urb_done = 0;
1060             max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1061         }
1062         break;
1063 
1064     case PKT_STATE_TERMINATE:
1065         urb_done = 1;
1066         break;
1067     }
1068 
1069     if (urb_done)
1070         max3421_hcd->urb_done = urb_done;
1071     else
1072         max3421_next_transfer(hcd, 0);
1073 }
1074 
1075 /*
1076  * Caller must NOT hold HCD spinlock.
1077  */
1078 static void
1079 max3421_detect_conn(struct usb_hcd *hcd)
1080 {
1081     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1082     unsigned int jk, have_conn = 0;
1083     u32 old_port_status, chg;
1084     unsigned long flags;
1085     u8 hrsl, mode;
1086 
1087     hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1088 
1089     jk = ((((hrsl >> MAX3421_HRSL_JSTATUS_BIT) & 1) << 0) |
1090           (((hrsl >> MAX3421_HRSL_KSTATUS_BIT) & 1) << 1));
1091 
1092     mode = max3421_hcd->mode;
1093 
1094     switch (jk) {
1095     case 0x0: /* SE0: disconnect */
1096         /*
1097          * Turn off SOFKAENAB bit to avoid getting interrupt
1098          * every milli-second:
1099          */
1100         mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
1101         break;
1102 
1103     case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */
1104     case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */
1105         if (jk == 0x2)
1106             /* need to switch to the other speed: */
1107             mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
1108         /* turn on SOFKAENAB bit: */
1109         mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
1110         have_conn = 1;
1111         break;
1112 
1113     case 0x3: /* illegal */
1114         break;
1115     }
1116 
1117     max3421_hcd->mode = mode;
1118     spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1119 
1120     spin_lock_irqsave(&max3421_hcd->lock, flags);
1121     old_port_status = max3421_hcd->port_status;
1122     if (have_conn)
1123         max3421_hcd->port_status |=  USB_PORT_STAT_CONNECTION;
1124     else
1125         max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
1126     if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
1127         max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
1128     else
1129         max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
1130     chg = (old_port_status ^ max3421_hcd->port_status);
1131     max3421_hcd->port_status |= chg << 16;
1132     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1133 }
1134 
1135 static irqreturn_t
1136 max3421_irq_handler(int irq, void *dev_id)
1137 {
1138     struct usb_hcd *hcd = dev_id;
1139     struct spi_device *spi = to_spi_device(hcd->self.controller);
1140     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1141 
1142     if (max3421_hcd->spi_thread)
1143         wake_up_process(max3421_hcd->spi_thread);
1144     if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo))
1145         disable_irq_nosync(spi->irq);
1146     return IRQ_HANDLED;
1147 }
1148 
1149 #ifdef DEBUG
1150 
1151 static void
1152 dump_eps(struct usb_hcd *hcd)
1153 {
1154     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1155     struct max3421_ep *max3421_ep;
1156     struct usb_host_endpoint *ep;
1157     char ubuf[512], *dp, *end;
1158     unsigned long flags;
1159     struct urb *urb;
1160     int epnum, ret;
1161 
1162     spin_lock_irqsave(&max3421_hcd->lock, flags);
1163     list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
1164         ep = max3421_ep->ep;
1165 
1166         dp = ubuf;
1167         end = dp + sizeof(ubuf);
1168         *dp = '\0';
1169         list_for_each_entry(urb, &ep->urb_list, urb_list) {
1170             ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1171                        usb_pipetype(urb->pipe),
1172                        usb_urb_dir_in(urb) ? "IN" : "OUT",
1173                        urb->actual_length,
1174                        urb->transfer_buffer_length);
1175             if (ret < 0 || ret >= end - dp)
1176                 break;  /* error or buffer full */
1177             dp += ret;
1178         }
1179 
1180         epnum = usb_endpoint_num(&ep->desc);
1181         pr_info("EP%0u %u lst %04u rtr %u nak %6u rxmt %u: %s\n",
1182             epnum, max3421_ep->pkt_state, max3421_ep->last_active,
1183             max3421_ep->retries, max3421_ep->naks,
1184             max3421_ep->retransmit, ubuf);
1185     }
1186     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1187 }
1188 
1189 #endif /* DEBUG */
1190 
1191 /* Return zero if no work was performed, 1 otherwise.  */
1192 static int
1193 max3421_handle_irqs(struct usb_hcd *hcd)
1194 {
1195     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1196     u32 chg, old_port_status;
1197     unsigned long flags;
1198     u8 hirq;
1199 
1200     /*
1201      * Read and ack pending interrupts (CPU must never
1202      * clear SNDBAV directly and RCVDAV must be cleared by
1203      * max3421_recv_data_available()!):
1204      */
1205     hirq = spi_rd8(hcd, MAX3421_REG_HIRQ);
1206     hirq &= max3421_hcd->hien;
1207     if (!hirq)
1208         return 0;
1209 
1210     spi_wr8(hcd, MAX3421_REG_HIRQ,
1211         hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
1212              BIT(MAX3421_HI_RCVDAV_BIT)));
1213 
1214     if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
1215         max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
1216                          & USB_MAX_FRAME_NUMBER);
1217         max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1218     }
1219 
1220     if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
1221         max3421_recv_data_available(hcd);
1222 
1223     if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
1224         max3421_host_transfer_done(hcd);
1225 
1226     if (hirq & BIT(MAX3421_HI_CONDET_BIT))
1227         max3421_detect_conn(hcd);
1228 
1229     /*
1230      * Now process interrupts that may affect HCD state
1231      * other than the end-points:
1232      */
1233     spin_lock_irqsave(&max3421_hcd->lock, flags);
1234 
1235     old_port_status = max3421_hcd->port_status;
1236     if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
1237         if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
1238             /* BUSEVENT due to completion of Bus Reset */
1239             max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
1240             max3421_hcd->port_status |=  USB_PORT_STAT_ENABLE;
1241         } else {
1242             /* BUSEVENT due to completion of Bus Resume */
1243             pr_info("%s: BUSEVENT Bus Resume Done\n", __func__);
1244         }
1245     }
1246     if (hirq & BIT(MAX3421_HI_RWU_BIT))
1247         pr_info("%s: RWU\n", __func__);
1248     if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
1249         pr_info("%s: SUSDN\n", __func__);
1250 
1251     chg = (old_port_status ^ max3421_hcd->port_status);
1252     max3421_hcd->port_status |= chg << 16;
1253 
1254     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1255 
1256 #ifdef DEBUG
1257     {
1258         static unsigned long last_time;
1259         char sbuf[16 * 16], *dp, *end;
1260         int i;
1261 
1262         if (time_after(jiffies, last_time + 5*HZ)) {
1263             dp = sbuf;
1264             end = sbuf + sizeof(sbuf);
1265             *dp = '\0';
1266             for (i = 0; i < 16; ++i) {
1267                 int ret = snprintf(dp, end - dp, " %lu",
1268                            max3421_hcd->err_stat[i]);
1269                 if (ret < 0 || ret >= end - dp)
1270                     break;  /* error or buffer full */
1271                 dp += ret;
1272             }
1273             pr_info("%s: hrsl_stats %s\n", __func__, sbuf);
1274             memset(max3421_hcd->err_stat, 0,
1275                    sizeof(max3421_hcd->err_stat));
1276             last_time = jiffies;
1277 
1278             dump_eps(hcd);
1279         }
1280     }
1281 #endif
1282     return 1;
1283 }
1284 
1285 static int
1286 max3421_reset_hcd(struct usb_hcd *hcd)
1287 {
1288     struct spi_device *spi = to_spi_device(hcd->self.controller);
1289     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1290     int timeout;
1291 
1292     /* perform a chip reset and wait for OSCIRQ signal to appear: */
1293     spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT));
1294     /* clear reset: */
1295     spi_wr8(hcd, MAX3421_REG_USBCTL, 0);
1296     timeout = 1000;
1297     while (1) {
1298         if (spi_rd8(hcd, MAX3421_REG_USBIRQ)
1299             & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
1300             break;
1301         if (--timeout < 0) {
1302             dev_err(&spi->dev,
1303                 "timed out waiting for oscillator OK signal");
1304             return 1;
1305         }
1306         cond_resched();
1307     }
1308 
1309     /*
1310      * Turn on host mode, automatic generation of SOF packets, and
1311      * enable pull-down registers on DM/DP:
1312      */
1313     max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) |
1314                  BIT(MAX3421_MODE_SOFKAENAB_BIT) |
1315                  BIT(MAX3421_MODE_DMPULLDN_BIT) |
1316                  BIT(MAX3421_MODE_DPPULLDN_BIT));
1317     spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1318 
1319     /* reset frame-number: */
1320     max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER;
1321     spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT));
1322 
1323     /* sample the state of the D+ and D- lines */
1324     spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT));
1325     max3421_detect_conn(hcd);
1326 
1327     /* enable frame, connection-detected, and bus-event interrupts: */
1328     max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
1329                  BIT(MAX3421_HI_CONDET_BIT) |
1330                  BIT(MAX3421_HI_BUSEVENT_BIT));
1331     spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1332 
1333     /* enable interrupts: */
1334     spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT));
1335     return 1;
1336 }
1337 
1338 static int
1339 max3421_urb_done(struct usb_hcd *hcd)
1340 {
1341     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1342     unsigned long flags;
1343     struct urb *urb;
1344     int status;
1345 
1346     status = max3421_hcd->urb_done;
1347     max3421_hcd->urb_done = 0;
1348     if (status > 0)
1349         status = 0;
1350     urb = max3421_hcd->curr_urb;
1351     if (urb) {
1352         /* save the old end-points toggles: */
1353         u8 hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1354         int rcvtog = (hrsl >> MAX3421_HRSL_RCVTOGRD_BIT) & 1;
1355         int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
1356         int epnum = usb_endpoint_num(&urb->ep->desc);
1357 
1358         /* no locking: HCD (i.e., we) own toggles, don't we? */
1359         usb_settoggle(urb->dev, epnum, 0, rcvtog);
1360         usb_settoggle(urb->dev, epnum, 1, sndtog);
1361 
1362         max3421_hcd->curr_urb = NULL;
1363         spin_lock_irqsave(&max3421_hcd->lock, flags);
1364         usb_hcd_unlink_urb_from_ep(hcd, urb);
1365         spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1366 
1367         /* must be called without the HCD spinlock: */
1368         usb_hcd_giveback_urb(hcd, urb, status);
1369     }
1370     return 1;
1371 }
1372 
1373 static int
1374 max3421_spi_thread(void *dev_id)
1375 {
1376     struct usb_hcd *hcd = dev_id;
1377     struct spi_device *spi = to_spi_device(hcd->self.controller);
1378     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1379     int i, i_worked = 1;
1380 
1381     /* set full-duplex SPI mode, low-active interrupt pin: */
1382     spi_wr8(hcd, MAX3421_REG_PINCTL,
1383         (BIT(MAX3421_PINCTL_FDUPSPI_BIT) |  /* full-duplex */
1384          BIT(MAX3421_PINCTL_INTLEVEL_BIT)));    /* low-active irq */
1385 
1386     while (!kthread_should_stop()) {
1387         max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION);
1388         if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13)
1389             break;
1390         dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev);
1391         msleep(10000);
1392     }
1393     dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
1394          max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word,
1395          spi->irq);
1396 
1397     while (!kthread_should_stop()) {
1398         if (!i_worked) {
1399             /*
1400              * We'll be waiting for wakeups from the hard
1401              * interrupt handler, so now is a good time to
1402              * sync our hien with the chip:
1403              */
1404             spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1405 
1406             set_current_state(TASK_INTERRUPTIBLE);
1407             if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo))
1408                 enable_irq(spi->irq);
1409             schedule();
1410             __set_current_state(TASK_RUNNING);
1411         }
1412 
1413         i_worked = 0;
1414 
1415         if (max3421_hcd->urb_done)
1416             i_worked |= max3421_urb_done(hcd);
1417         else if (max3421_handle_irqs(hcd))
1418             i_worked = 1;
1419         else if (!max3421_hcd->curr_urb)
1420             i_worked |= max3421_select_and_start_urb(hcd);
1421 
1422         if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo))
1423             /* reset the HCD: */
1424             i_worked |= max3421_reset_hcd(hcd);
1425         if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) {
1426             /* perform a USB bus reset: */
1427             spi_wr8(hcd, MAX3421_REG_HCTL,
1428                 BIT(MAX3421_HCTL_BUSRST_BIT));
1429             i_worked = 1;
1430         }
1431         if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo))
1432             i_worked |= max3421_check_unlink(hcd);
1433         if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) {
1434             /*
1435              * IOPINS1/IOPINS2 do not auto-increment, so we can't
1436              * use spi_wr_buf().
1437              */
1438             for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) {
1439                 u8 val = spi_rd8(hcd, MAX3421_REG_IOPINS1);
1440 
1441                 val = ((val & 0xf0) |
1442                        (max3421_hcd->iopins[i] & 0x0f));
1443                 spi_wr8(hcd, MAX3421_REG_IOPINS1 + i, val);
1444                 max3421_hcd->iopins[i] = val;
1445             }
1446             i_worked = 1;
1447         }
1448     }
1449     set_current_state(TASK_RUNNING);
1450     dev_info(&spi->dev, "SPI thread exiting");
1451     return 0;
1452 }
1453 
1454 static int
1455 max3421_reset_port(struct usb_hcd *hcd)
1456 {
1457     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1458 
1459     max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
1460                       USB_PORT_STAT_LOW_SPEED);
1461     max3421_hcd->port_status |= USB_PORT_STAT_RESET;
1462     set_bit(RESET_PORT, &max3421_hcd->todo);
1463     wake_up_process(max3421_hcd->spi_thread);
1464     return 0;
1465 }
1466 
1467 static int
1468 max3421_reset(struct usb_hcd *hcd)
1469 {
1470     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1471 
1472     hcd->self.sg_tablesize = 0;
1473     hcd->speed = HCD_USB2;
1474     hcd->self.root_hub->speed = USB_SPEED_FULL;
1475     set_bit(RESET_HCD, &max3421_hcd->todo);
1476     wake_up_process(max3421_hcd->spi_thread);
1477     return 0;
1478 }
1479 
1480 static int
1481 max3421_start(struct usb_hcd *hcd)
1482 {
1483     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1484 
1485     spin_lock_init(&max3421_hcd->lock);
1486     max3421_hcd->rh_state = MAX3421_RH_RUNNING;
1487 
1488     INIT_LIST_HEAD(&max3421_hcd->ep_list);
1489 
1490     hcd->power_budget = POWER_BUDGET;
1491     hcd->state = HC_STATE_RUNNING;
1492     hcd->uses_new_polling = 1;
1493     return 0;
1494 }
1495 
1496 static void
1497 max3421_stop(struct usb_hcd *hcd)
1498 {
1499 }
1500 
1501 static int
1502 max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1503 {
1504     struct spi_device *spi = to_spi_device(hcd->self.controller);
1505     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1506     struct max3421_ep *max3421_ep;
1507     unsigned long flags;
1508     int retval;
1509 
1510     switch (usb_pipetype(urb->pipe)) {
1511     case PIPE_INTERRUPT:
1512     case PIPE_ISOCHRONOUS:
1513         if (urb->interval < 0) {
1514             dev_err(&spi->dev,
1515               "%s: interval=%d for intr-/iso-pipe; expected > 0\n",
1516                 __func__, urb->interval);
1517             return -EINVAL;
1518         }
1519         break;
1520     default:
1521         break;
1522     }
1523 
1524     spin_lock_irqsave(&max3421_hcd->lock, flags);
1525 
1526     max3421_ep = urb->ep->hcpriv;
1527     if (!max3421_ep) {
1528         /* gets freed in max3421_endpoint_disable: */
1529         max3421_ep = kzalloc(sizeof(struct max3421_ep), GFP_ATOMIC);
1530         if (!max3421_ep) {
1531             retval = -ENOMEM;
1532             goto out;
1533         }
1534         max3421_ep->ep = urb->ep;
1535         max3421_ep->last_active = max3421_hcd->frame_number;
1536         urb->ep->hcpriv = max3421_ep;
1537 
1538         list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
1539     }
1540 
1541     retval = usb_hcd_link_urb_to_ep(hcd, urb);
1542     if (retval == 0) {
1543         /* Since we added to the queue, restart scheduling: */
1544         max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1545         wake_up_process(max3421_hcd->spi_thread);
1546     }
1547 
1548 out:
1549     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1550     return retval;
1551 }
1552 
1553 static int
1554 max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1555 {
1556     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1557     unsigned long flags;
1558     int retval;
1559 
1560     spin_lock_irqsave(&max3421_hcd->lock, flags);
1561 
1562     /*
1563      * This will set urb->unlinked which in turn causes the entry
1564      * to be dropped at the next opportunity.
1565      */
1566     retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1567     if (retval == 0) {
1568         set_bit(CHECK_UNLINK, &max3421_hcd->todo);
1569         wake_up_process(max3421_hcd->spi_thread);
1570     }
1571     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1572     return retval;
1573 }
1574 
1575 static void
1576 max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1577 {
1578     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1579     unsigned long flags;
1580 
1581     spin_lock_irqsave(&max3421_hcd->lock, flags);
1582 
1583     if (ep->hcpriv) {
1584         struct max3421_ep *max3421_ep = ep->hcpriv;
1585 
1586         /* remove myself from the ep_list: */
1587         if (!list_empty(&max3421_ep->ep_list))
1588             list_del(&max3421_ep->ep_list);
1589         kfree(max3421_ep);
1590         ep->hcpriv = NULL;
1591     }
1592 
1593     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1594 }
1595 
1596 static int
1597 max3421_get_frame_number(struct usb_hcd *hcd)
1598 {
1599     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1600     return max3421_hcd->frame_number;
1601 }
1602 
1603 /*
1604  * Should return a non-zero value when any port is undergoing a resume
1605  * transition while the root hub is suspended.
1606  */
1607 static int
1608 max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
1609 {
1610     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1611     unsigned long flags;
1612     int retval = 0;
1613 
1614     spin_lock_irqsave(&max3421_hcd->lock, flags);
1615     if (!HCD_HW_ACCESSIBLE(hcd))
1616         goto done;
1617 
1618     *buf = 0;
1619     if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
1620         *buf = (1 << 1); /* a hub over-current condition exists */
1621         dev_dbg(hcd->self.controller,
1622             "port status 0x%08x has changes\n",
1623             max3421_hcd->port_status);
1624         retval = 1;
1625         if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
1626             usb_hcd_resume_root_hub(hcd);
1627     }
1628 done:
1629     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1630     return retval;
1631 }
1632 
1633 static inline void
1634 hub_descriptor(struct usb_hub_descriptor *desc)
1635 {
1636     memset(desc, 0, sizeof(*desc));
1637     /*
1638      * See Table 11-13: Hub Descriptor in USB 2.0 spec.
1639      */
1640     desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */
1641     desc->bDescLength = 9;
1642     desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM |
1643                         HUB_CHAR_COMMON_OCPM);
1644     desc->bNbrPorts = 1;
1645 }
1646 
1647 /*
1648  * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1649  * VALUE (0 or 1).  PIN_NUMBER may be in the range from 1-8.  For
1650  * any other value, this function acts as a no-op.
1651  */
1652 static void
1653 max3421_gpout_set_value(struct usb_hcd *hcd, u8 pin_number, u8 value)
1654 {
1655     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1656     u8 mask, idx;
1657 
1658     --pin_number;
1659     if (pin_number >= MAX3421_GPOUT_COUNT)
1660         return;
1661 
1662     mask = 1u << (pin_number % 4);
1663     idx = pin_number / 4;
1664 
1665     if (value)
1666         max3421_hcd->iopins[idx] |=  mask;
1667     else
1668         max3421_hcd->iopins[idx] &= ~mask;
1669     set_bit(IOPIN_UPDATE, &max3421_hcd->todo);
1670     wake_up_process(max3421_hcd->spi_thread);
1671 }
1672 
1673 static int
1674 max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
1675             char *buf, u16 length)
1676 {
1677     struct spi_device *spi = to_spi_device(hcd->self.controller);
1678     struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1679     struct max3421_hcd_platform_data *pdata;
1680     unsigned long flags;
1681     int retval = 0;
1682 
1683     pdata = spi->dev.platform_data;
1684 
1685     spin_lock_irqsave(&max3421_hcd->lock, flags);
1686 
1687     switch (type_req) {
1688     case ClearHubFeature:
1689         break;
1690     case ClearPortFeature:
1691         switch (value) {
1692         case USB_PORT_FEAT_SUSPEND:
1693             break;
1694         case USB_PORT_FEAT_POWER:
1695             dev_dbg(hcd->self.controller, "power-off\n");
1696             max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1697                         !pdata->vbus_active_level);
1698             fallthrough;
1699         default:
1700             max3421_hcd->port_status &= ~(1 << value);
1701         }
1702         break;
1703     case GetHubDescriptor:
1704         hub_descriptor((struct usb_hub_descriptor *) buf);
1705         break;
1706 
1707     case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1708     case GetPortErrorCount:
1709     case SetHubDepth:
1710         /* USB3 only */
1711         goto error;
1712 
1713     case GetHubStatus:
1714         *(__le32 *) buf = cpu_to_le32(0);
1715         break;
1716 
1717     case GetPortStatus:
1718         if (index != 1) {
1719             retval = -EPIPE;
1720             goto error;
1721         }
1722         ((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
1723         ((__le16 *) buf)[1] =
1724             cpu_to_le16(max3421_hcd->port_status >> 16);
1725         break;
1726 
1727     case SetHubFeature:
1728         retval = -EPIPE;
1729         break;
1730 
1731     case SetPortFeature:
1732         switch (value) {
1733         case USB_PORT_FEAT_LINK_STATE:
1734         case USB_PORT_FEAT_U1_TIMEOUT:
1735         case USB_PORT_FEAT_U2_TIMEOUT:
1736         case USB_PORT_FEAT_BH_PORT_RESET:
1737             goto error;
1738         case USB_PORT_FEAT_SUSPEND:
1739             if (max3421_hcd->active)
1740                 max3421_hcd->port_status |=
1741                     USB_PORT_STAT_SUSPEND;
1742             break;
1743         case USB_PORT_FEAT_POWER:
1744             dev_dbg(hcd->self.controller, "power-on\n");
1745             max3421_hcd->port_status |= USB_PORT_STAT_POWER;
1746             max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1747                         pdata->vbus_active_level);
1748             break;
1749         case USB_PORT_FEAT_RESET:
1750             max3421_reset_port(hcd);
1751             fallthrough;
1752         default:
1753             if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
1754                 != 0)
1755                 max3421_hcd->port_status |= (1 << value);
1756         }
1757         break;
1758 
1759     default:
1760         dev_dbg(hcd->self.controller,
1761             "hub control req%04x v%04x i%04x l%d\n",
1762             type_req, value, index, length);
1763 error:      /* "protocol stall" on error */
1764         retval = -EPIPE;
1765     }
1766 
1767     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1768     return retval;
1769 }
1770 
1771 static int
1772 max3421_bus_suspend(struct usb_hcd *hcd)
1773 {
1774     return -1;
1775 }
1776 
1777 static int
1778 max3421_bus_resume(struct usb_hcd *hcd)
1779 {
1780     return -1;
1781 }
1782 
1783 static const struct hc_driver max3421_hcd_desc = {
1784     .description =      "max3421",
1785     .product_desc =     DRIVER_DESC,
1786     .hcd_priv_size =    sizeof(struct max3421_hcd),
1787     .flags =        HCD_USB11,
1788     .reset =        max3421_reset,
1789     .start =        max3421_start,
1790     .stop =         max3421_stop,
1791     .get_frame_number = max3421_get_frame_number,
1792     .urb_enqueue =      max3421_urb_enqueue,
1793     .urb_dequeue =      max3421_urb_dequeue,
1794     .endpoint_disable = max3421_endpoint_disable,
1795     .hub_status_data =  max3421_hub_status_data,
1796     .hub_control =      max3421_hub_control,
1797     .bus_suspend =      max3421_bus_suspend,
1798     .bus_resume =       max3421_bus_resume,
1799 };
1800 
1801 static int
1802 max3421_of_vbus_en_pin(struct device *dev, struct max3421_hcd_platform_data *pdata)
1803 {
1804     int retval;
1805     uint32_t value[2];
1806 
1807     if (!pdata)
1808         return -EINVAL;
1809 
1810     retval = of_property_read_u32_array(dev->of_node, "maxim,vbus-en-pin", value, 2);
1811     if (retval) {
1812         dev_err(dev, "device tree node property 'maxim,vbus-en-pin' is missing\n");
1813         return retval;
1814     }
1815     dev_info(dev, "property 'maxim,vbus-en-pin' value is <%d %d>\n", value[0], value[1]);
1816 
1817     pdata->vbus_gpout = value[0];
1818     pdata->vbus_active_level = value[1];
1819 
1820     return 0;
1821 }
1822 
1823 static int
1824 max3421_probe(struct spi_device *spi)
1825 {
1826     struct device *dev = &spi->dev;
1827     struct max3421_hcd *max3421_hcd;
1828     struct usb_hcd *hcd = NULL;
1829     struct max3421_hcd_platform_data *pdata = NULL;
1830     int retval;
1831 
1832     if (spi_setup(spi) < 0) {
1833         dev_err(&spi->dev, "Unable to setup SPI bus");
1834         return -EFAULT;
1835     }
1836 
1837     if (!spi->irq) {
1838         dev_err(dev, "Failed to get SPI IRQ");
1839         return -EFAULT;
1840     }
1841 
1842     if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1843         pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1844         if (!pdata) {
1845             retval = -ENOMEM;
1846             goto error;
1847         }
1848         retval = max3421_of_vbus_en_pin(dev, pdata);
1849         if (retval)
1850             goto error;
1851 
1852         spi->dev.platform_data = pdata;
1853     }
1854 
1855     pdata = spi->dev.platform_data;
1856     if (!pdata) {
1857         dev_err(&spi->dev, "driver configuration data is not provided\n");
1858         retval = -EFAULT;
1859         goto error;
1860     }
1861     if (pdata->vbus_active_level > 1) {
1862         dev_err(&spi->dev, "vbus active level value %d is out of range (0/1)\n", pdata->vbus_active_level);
1863         retval = -EINVAL;
1864         goto error;
1865     }
1866     if (pdata->vbus_gpout < 1 || pdata->vbus_gpout > MAX3421_GPOUT_COUNT) {
1867         dev_err(&spi->dev, "vbus gpout value %d is out of range (1..8)\n", pdata->vbus_gpout);
1868         retval = -EINVAL;
1869         goto error;
1870     }
1871 
1872     retval = -ENOMEM;
1873     hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
1874                  dev_name(&spi->dev));
1875     if (!hcd) {
1876         dev_err(&spi->dev, "failed to create HCD structure\n");
1877         goto error;
1878     }
1879     set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1880     max3421_hcd = hcd_to_max3421(hcd);
1881     INIT_LIST_HEAD(&max3421_hcd->ep_list);
1882     spi_set_drvdata(spi, max3421_hcd);
1883 
1884     max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL);
1885     if (!max3421_hcd->tx)
1886         goto error;
1887     max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL);
1888     if (!max3421_hcd->rx)
1889         goto error;
1890 
1891     max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
1892                           "max3421_spi_thread");
1893     if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
1894         dev_err(&spi->dev,
1895             "failed to create SPI thread (out of memory)\n");
1896         goto error;
1897     }
1898 
1899     retval = usb_add_hcd(hcd, 0, 0);
1900     if (retval) {
1901         dev_err(&spi->dev, "failed to add HCD\n");
1902         goto error;
1903     }
1904 
1905     retval = request_irq(spi->irq, max3421_irq_handler,
1906                  IRQF_TRIGGER_LOW, "max3421", hcd);
1907     if (retval < 0) {
1908         dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
1909         goto error;
1910     }
1911     return 0;
1912 
1913 error:
1914     if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
1915         devm_kfree(&spi->dev, pdata);
1916         spi->dev.platform_data = NULL;
1917     }
1918 
1919     if (hcd) {
1920         kfree(max3421_hcd->tx);
1921         kfree(max3421_hcd->rx);
1922         if (max3421_hcd->spi_thread)
1923             kthread_stop(max3421_hcd->spi_thread);
1924         usb_put_hcd(hcd);
1925     }
1926     return retval;
1927 }
1928 
1929 static void
1930 max3421_remove(struct spi_device *spi)
1931 {
1932     struct max3421_hcd *max3421_hcd;
1933     struct usb_hcd *hcd;
1934     unsigned long flags;
1935 
1936     max3421_hcd = spi_get_drvdata(spi);
1937     hcd = max3421_to_hcd(max3421_hcd);
1938 
1939     usb_remove_hcd(hcd);
1940 
1941     spin_lock_irqsave(&max3421_hcd->lock, flags);
1942 
1943     kthread_stop(max3421_hcd->spi_thread);
1944 
1945     spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1946 
1947     free_irq(spi->irq, hcd);
1948 
1949     usb_put_hcd(hcd);
1950 }
1951 
1952 static const struct of_device_id max3421_of_match_table[] = {
1953     { .compatible = "maxim,max3421", },
1954     {},
1955 };
1956 MODULE_DEVICE_TABLE(of, max3421_of_match_table);
1957 
1958 static struct spi_driver max3421_driver = {
1959     .probe      = max3421_probe,
1960     .remove     = max3421_remove,
1961     .driver     = {
1962         .name   = "max3421-hcd",
1963         .of_match_table = of_match_ptr(max3421_of_match_table),
1964     },
1965 };
1966 
1967 module_spi_driver(max3421_driver);
1968 
1969 MODULE_DESCRIPTION(DRIVER_DESC);
1970 MODULE_AUTHOR("David Mosberger <davidm@egauge.net>");
1971 MODULE_LICENSE("GPL");