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