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