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