1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
72#define USB_MAX_FRAME_NUMBER 0x7ff
73#define USB_MAX_RETRIES 3
74
75
76
77
78
79#define NAK_MAX_FAST_RETRANSMITS 2
80
81#define POWER_BUDGET 500
82
83
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,
100 PKT_STATE_TRANSFER,
101 PKT_STATE_TERMINATE
102};
103
104enum scheduling_pass {
105 SCHED_PASS_PERIODIC,
106 SCHED_PASS_NON_PERIODIC,
107 SCHED_PASS_DONE
108};
109
110
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
132 u32 port_status;
133
134 unsigned active:1;
135
136 struct list_head ep_list;
137
138
139
140
141
142 u8 rev;
143 u16 frame_number;
144
145
146
147
148 struct max3421_dma_buf *tx;
149 struct max3421_dma_buf *rx;
150
151
152
153
154 struct urb *curr_urb;
155 enum scheduling_pass sched_pass;
156 struct usb_device *loaded_dev;
157 int loaded_epnum;
158 int urb_done;
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;
174 enum pkt_state pkt_state;
175 u8 retries;
176 u8 retransmit;
177};
178
179static struct max3421_hcd *max3421_hcd_list;
180
181#define MAX3421_FIFO_SIZE 64
182
183#define MAX3421_SPI_DIR_RD 0
184#define MAX3421_SPI_DIR_WR 1
185
186
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,
244 MAX3421_HI_RWU_BIT,
245 MAX3421_HI_RCVDAV_BIT,
246 MAX3421_HI_SNDBAV_BIT,
247 MAX3421_HI_SUSDN_BIT,
248 MAX3421_HI_CONDET_BIT,
249 MAX3421_HI_FRAME_BIT,
250 MAX3421_HI_HXFRDN_BIT,
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
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
321
322
323
324#define MAX3421_HXFR_BULK_IN(ep) (0x00 | (ep))
325#define MAX3421_HXFR_SETUP 0x10
326#define MAX3421_HXFR_BULK_OUT(ep) (0x20 | (ep))
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
330#define MAX3421_HXFR_HS_OUT 0xa0
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
457
458
459
460
461
462
463
464
465
466
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
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
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
518 usb_settoggle(old_dev, old_epnum, 0, rcvtog);
519 usb_settoggle(old_dev, old_epnum, 1, sndtog);
520 }
521
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
532
533
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
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
583
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
601
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;
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
631
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
644
645 spi_wr8(hcd, MAX3421_REG_HXFR, cmd);
646 max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
647}
648
649
650
651
652
653
654
655
656
657
658
659
660
661
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;
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
720
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
734
735
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
747
748
749
750 continue;
751 break;
752 }
753
754
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
769 max3421_ep->retransmit = 0;
770 else {
771
772 if (usb_endpoint_xfer_control(&ep->desc)) {
773
774
775
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
796
797
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
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
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
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
891
892
893
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;
901
902 case MAX3421_HRSL_WRONGPID:
903 case MAX3421_HRSL_BUSY:
904 case MAX3421_HRSL_BADREQ:
905 case MAX3421_HRSL_UNDEF:
906 case MAX3421_HRSL_KERR:
907 case MAX3421_HRSL_JERR:
908
909
910
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 ;
920 else {
921
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
929 case MAX3421_HRSL_BADBC:
930 case MAX3421_HRSL_PIDERR:
931 case MAX3421_HRSL_PKTERR:
932 case MAX3421_HRSL_CRCERR:
933 case MAX3421_HRSL_BABBLE:
934 case MAX3421_HRSL_TIMEOUT:
935 if (max3421_ep->retries++ < USB_MAX_RETRIES)
936
937 max3421_slow_retransmit(hcd);
938 else {
939
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
955
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
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;
980
981
982
983
984
985 max_packet = usb_maxpacket(urb->dev, urb->pipe, 0);
986 if (max_packet > MAX3421_FIFO_SIZE) {
987
988
989
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
1001
1002
1003
1004 return -EREMOTEIO;
1005 } else
1006
1007 return 1;
1008 }
1009 return 0;
1010}
1011
1012
1013
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
1026
1027
1028
1029
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
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
1087
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
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:
1126
1127
1128
1129
1130 mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
1131 break;
1132
1133 case 0x1:
1134 case 0x2:
1135 if (jk == 0x2)
1136
1137 mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
1138
1139 mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
1140 have_conn = 1;
1141 break;
1142
1143 case 0x3:
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 wake_up_process(max3421_hcd->spi_thread);
1174 if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo))
1175 disable_irq_nosync(spi->irq);
1176 return IRQ_HANDLED;
1177}
1178
1179#ifdef DEBUG
1180
1181static void
1182dump_eps(struct usb_hcd *hcd)
1183{
1184 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1185 struct max3421_ep *max3421_ep;
1186 struct usb_host_endpoint *ep;
1187 char ubuf[512], *dp, *end;
1188 unsigned long flags;
1189 struct urb *urb;
1190 int epnum, ret;
1191
1192 spin_lock_irqsave(&max3421_hcd->lock, flags);
1193 list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
1194 ep = max3421_ep->ep;
1195
1196 dp = ubuf;
1197 end = dp + sizeof(ubuf);
1198 *dp = '\0';
1199 list_for_each_entry(urb, &ep->urb_list, urb_list) {
1200 ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1201 usb_pipetype(urb->pipe),
1202 usb_urb_dir_in(urb) ? "IN" : "OUT",
1203 urb->actual_length,
1204 urb->transfer_buffer_length);
1205 if (ret < 0 || ret >= end - dp)
1206 break;
1207 dp += ret;
1208 }
1209
1210 epnum = usb_endpoint_num(&ep->desc);
1211 pr_info("EP%0u %u lst %04u rtr %u nak %6u rxmt %u: %s\n",
1212 epnum, max3421_ep->pkt_state, max3421_ep->last_active,
1213 max3421_ep->retries, max3421_ep->naks,
1214 max3421_ep->retransmit, ubuf);
1215 }
1216 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1217}
1218
1219#endif
1220
1221
1222static int
1223max3421_handle_irqs(struct usb_hcd *hcd)
1224{
1225 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1226 u32 chg, old_port_status;
1227 unsigned long flags;
1228 u8 hirq;
1229
1230
1231
1232
1233
1234
1235 hirq = spi_rd8(hcd, MAX3421_REG_HIRQ);
1236 hirq &= max3421_hcd->hien;
1237 if (!hirq)
1238 return 0;
1239
1240 spi_wr8(hcd, MAX3421_REG_HIRQ,
1241 hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
1242 BIT(MAX3421_HI_RCVDAV_BIT)));
1243
1244 if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
1245 max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
1246 & USB_MAX_FRAME_NUMBER);
1247 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1248 }
1249
1250 if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
1251 max3421_recv_data_available(hcd);
1252
1253 if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
1254 max3421_host_transfer_done(hcd);
1255
1256 if (hirq & BIT(MAX3421_HI_CONDET_BIT))
1257 max3421_detect_conn(hcd);
1258
1259
1260
1261
1262
1263 spin_lock_irqsave(&max3421_hcd->lock, flags);
1264
1265 old_port_status = max3421_hcd->port_status;
1266 if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
1267 if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
1268
1269 max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
1270 max3421_hcd->port_status |= USB_PORT_STAT_ENABLE;
1271 } else {
1272
1273 pr_info("%s: BUSEVENT Bus Resume Done\n", __func__);
1274 }
1275 }
1276 if (hirq & BIT(MAX3421_HI_RWU_BIT))
1277 pr_info("%s: RWU\n", __func__);
1278 if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
1279 pr_info("%s: SUSDN\n", __func__);
1280
1281 chg = (old_port_status ^ max3421_hcd->port_status);
1282 max3421_hcd->port_status |= chg << 16;
1283
1284 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1285
1286#ifdef DEBUG
1287 {
1288 static unsigned long last_time;
1289 char sbuf[16 * 16], *dp, *end;
1290 int i;
1291
1292 if (time_after(jiffies, last_time + 5*HZ)) {
1293 dp = sbuf;
1294 end = sbuf + sizeof(sbuf);
1295 *dp = '\0';
1296 for (i = 0; i < 16; ++i) {
1297 int ret = snprintf(dp, end - dp, " %lu",
1298 max3421_hcd->err_stat[i]);
1299 if (ret < 0 || ret >= end - dp)
1300 break;
1301 dp += ret;
1302 }
1303 pr_info("%s: hrsl_stats %s\n", __func__, sbuf);
1304 memset(max3421_hcd->err_stat, 0,
1305 sizeof(max3421_hcd->err_stat));
1306 last_time = jiffies;
1307
1308 dump_eps(hcd);
1309 }
1310 }
1311#endif
1312 return 1;
1313}
1314
1315static int
1316max3421_reset_hcd(struct usb_hcd *hcd)
1317{
1318 struct spi_device *spi = to_spi_device(hcd->self.controller);
1319 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1320 int timeout;
1321
1322
1323 spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT));
1324
1325 spi_wr8(hcd, MAX3421_REG_USBCTL, 0);
1326 timeout = 1000;
1327 while (1) {
1328 if (spi_rd8(hcd, MAX3421_REG_USBIRQ)
1329 & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
1330 break;
1331 if (--timeout < 0) {
1332 dev_err(&spi->dev,
1333 "timed out waiting for oscillator OK signal");
1334 return 1;
1335 }
1336 cond_resched();
1337 }
1338
1339
1340
1341
1342
1343 max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) |
1344 BIT(MAX3421_MODE_SOFKAENAB_BIT) |
1345 BIT(MAX3421_MODE_DMPULLDN_BIT) |
1346 BIT(MAX3421_MODE_DPPULLDN_BIT));
1347 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1348
1349
1350 max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER;
1351 spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT));
1352
1353
1354 spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT));
1355 max3421_detect_conn(hcd);
1356
1357
1358 max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
1359 BIT(MAX3421_HI_CONDET_BIT) |
1360 BIT(MAX3421_HI_BUSEVENT_BIT));
1361 spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1362
1363
1364 spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT));
1365 return 1;
1366}
1367
1368static int
1369max3421_urb_done(struct usb_hcd *hcd)
1370{
1371 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1372 unsigned long flags;
1373 struct urb *urb;
1374 int status;
1375
1376 status = max3421_hcd->urb_done;
1377 max3421_hcd->urb_done = 0;
1378 if (status > 0)
1379 status = 0;
1380 urb = max3421_hcd->curr_urb;
1381 if (urb) {
1382 max3421_hcd->curr_urb = NULL;
1383 spin_lock_irqsave(&max3421_hcd->lock, flags);
1384 usb_hcd_unlink_urb_from_ep(hcd, urb);
1385 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1386
1387
1388 usb_hcd_giveback_urb(hcd, urb, status);
1389 }
1390 return 1;
1391}
1392
1393static int
1394max3421_spi_thread(void *dev_id)
1395{
1396 struct usb_hcd *hcd = dev_id;
1397 struct spi_device *spi = to_spi_device(hcd->self.controller);
1398 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1399 int i, i_worked = 1;
1400
1401
1402 spi_wr8(hcd, MAX3421_REG_PINCTL,
1403 (BIT(MAX3421_PINCTL_FDUPSPI_BIT) |
1404 BIT(MAX3421_PINCTL_INTLEVEL_BIT)));
1405
1406 while (!kthread_should_stop()) {
1407 max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION);
1408 if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13)
1409 break;
1410 dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev);
1411 msleep(10000);
1412 }
1413 dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
1414 max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word,
1415 spi->irq);
1416
1417 while (!kthread_should_stop()) {
1418 if (!i_worked) {
1419
1420
1421
1422
1423
1424 spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1425
1426 set_current_state(TASK_INTERRUPTIBLE);
1427 if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo))
1428 enable_irq(spi->irq);
1429 schedule();
1430 __set_current_state(TASK_RUNNING);
1431 }
1432
1433 i_worked = 0;
1434
1435 if (max3421_hcd->urb_done)
1436 i_worked |= max3421_urb_done(hcd);
1437 else if (max3421_handle_irqs(hcd))
1438 i_worked = 1;
1439 else if (!max3421_hcd->curr_urb)
1440 i_worked |= max3421_select_and_start_urb(hcd);
1441
1442 if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo))
1443
1444 i_worked |= max3421_reset_hcd(hcd);
1445 if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) {
1446
1447 spi_wr8(hcd, MAX3421_REG_HCTL,
1448 BIT(MAX3421_HCTL_BUSRST_BIT));
1449 i_worked = 1;
1450 }
1451 if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo))
1452 i_worked |= max3421_check_unlink(hcd);
1453 if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) {
1454
1455
1456
1457
1458 for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) {
1459 u8 val = spi_rd8(hcd, MAX3421_REG_IOPINS1);
1460
1461 val = ((val & 0xf0) |
1462 (max3421_hcd->iopins[i] & 0x0f));
1463 spi_wr8(hcd, MAX3421_REG_IOPINS1 + i, val);
1464 max3421_hcd->iopins[i] = val;
1465 }
1466 i_worked = 1;
1467 }
1468 }
1469 set_current_state(TASK_RUNNING);
1470 dev_info(&spi->dev, "SPI thread exiting");
1471 return 0;
1472}
1473
1474static int
1475max3421_reset_port(struct usb_hcd *hcd)
1476{
1477 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1478
1479 max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
1480 USB_PORT_STAT_LOW_SPEED);
1481 max3421_hcd->port_status |= USB_PORT_STAT_RESET;
1482 set_bit(RESET_PORT, &max3421_hcd->todo);
1483 wake_up_process(max3421_hcd->spi_thread);
1484 return 0;
1485}
1486
1487static int
1488max3421_reset(struct usb_hcd *hcd)
1489{
1490 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1491
1492 hcd->self.sg_tablesize = 0;
1493 hcd->speed = HCD_USB2;
1494 hcd->self.root_hub->speed = USB_SPEED_FULL;
1495 set_bit(RESET_HCD, &max3421_hcd->todo);
1496 wake_up_process(max3421_hcd->spi_thread);
1497 return 0;
1498}
1499
1500static int
1501max3421_start(struct usb_hcd *hcd)
1502{
1503 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1504
1505 spin_lock_init(&max3421_hcd->lock);
1506 max3421_hcd->rh_state = MAX3421_RH_RUNNING;
1507
1508 INIT_LIST_HEAD(&max3421_hcd->ep_list);
1509
1510 hcd->power_budget = POWER_BUDGET;
1511 hcd->state = HC_STATE_RUNNING;
1512 hcd->uses_new_polling = 1;
1513 return 0;
1514}
1515
1516static void
1517max3421_stop(struct usb_hcd *hcd)
1518{
1519}
1520
1521static int
1522max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1523{
1524 struct spi_device *spi = to_spi_device(hcd->self.controller);
1525 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1526 struct max3421_ep *max3421_ep;
1527 unsigned long flags;
1528 int retval;
1529
1530 switch (usb_pipetype(urb->pipe)) {
1531 case PIPE_INTERRUPT:
1532 case PIPE_ISOCHRONOUS:
1533 if (urb->interval < 0) {
1534 dev_err(&spi->dev,
1535 "%s: interval=%d for intr-/iso-pipe; expected > 0\n",
1536 __func__, urb->interval);
1537 return -EINVAL;
1538 }
1539 default:
1540 break;
1541 }
1542
1543 spin_lock_irqsave(&max3421_hcd->lock, flags);
1544
1545 max3421_ep = urb->ep->hcpriv;
1546 if (!max3421_ep) {
1547
1548 max3421_ep = kzalloc(sizeof(struct max3421_ep), GFP_ATOMIC);
1549 if (!max3421_ep) {
1550 retval = -ENOMEM;
1551 goto out;
1552 }
1553 max3421_ep->ep = urb->ep;
1554 max3421_ep->last_active = max3421_hcd->frame_number;
1555 urb->ep->hcpriv = max3421_ep;
1556
1557 list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
1558 }
1559
1560 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1561 if (retval == 0) {
1562
1563 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1564 wake_up_process(max3421_hcd->spi_thread);
1565 }
1566
1567out:
1568 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1569 return retval;
1570}
1571
1572static int
1573max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1574{
1575 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1576 unsigned long flags;
1577 int retval;
1578
1579 spin_lock_irqsave(&max3421_hcd->lock, flags);
1580
1581
1582
1583
1584
1585 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1586 if (retval == 0) {
1587 set_bit(CHECK_UNLINK, &max3421_hcd->todo);
1588 wake_up_process(max3421_hcd->spi_thread);
1589 }
1590 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1591 return retval;
1592}
1593
1594static void
1595max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1596{
1597 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1598 unsigned long flags;
1599
1600 spin_lock_irqsave(&max3421_hcd->lock, flags);
1601
1602 if (ep->hcpriv) {
1603 struct max3421_ep *max3421_ep = ep->hcpriv;
1604
1605
1606 if (!list_empty(&max3421_ep->ep_list))
1607 list_del(&max3421_ep->ep_list);
1608 kfree(max3421_ep);
1609 ep->hcpriv = NULL;
1610 }
1611
1612 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1613}
1614
1615static int
1616max3421_get_frame_number(struct usb_hcd *hcd)
1617{
1618 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1619 return max3421_hcd->frame_number;
1620}
1621
1622
1623
1624
1625
1626static int
1627max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
1628{
1629 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1630 unsigned long flags;
1631 int retval = 0;
1632
1633 spin_lock_irqsave(&max3421_hcd->lock, flags);
1634 if (!HCD_HW_ACCESSIBLE(hcd))
1635 goto done;
1636
1637 *buf = 0;
1638 if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
1639 *buf = (1 << 1);
1640 dev_dbg(hcd->self.controller,
1641 "port status 0x%08x has changes\n",
1642 max3421_hcd->port_status);
1643 retval = 1;
1644 if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
1645 usb_hcd_resume_root_hub(hcd);
1646 }
1647done:
1648 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1649 return retval;
1650}
1651
1652static inline void
1653hub_descriptor(struct usb_hub_descriptor *desc)
1654{
1655 memset(desc, 0, sizeof(*desc));
1656
1657
1658
1659 desc->bDescriptorType = USB_DT_HUB;
1660 desc->bDescLength = 9;
1661 desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM |
1662 HUB_CHAR_COMMON_OCPM);
1663 desc->bNbrPorts = 1;
1664}
1665
1666
1667
1668
1669
1670
1671static void
1672max3421_gpout_set_value(struct usb_hcd *hcd, u8 pin_number, u8 value)
1673{
1674 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1675 u8 mask, idx;
1676
1677 --pin_number;
1678 if (pin_number >= MAX3421_GPOUT_COUNT)
1679 return;
1680
1681 mask = 1u << (pin_number % 4);
1682 idx = pin_number / 4;
1683
1684 if (value)
1685 max3421_hcd->iopins[idx] |= mask;
1686 else
1687 max3421_hcd->iopins[idx] &= ~mask;
1688 set_bit(IOPIN_UPDATE, &max3421_hcd->todo);
1689 wake_up_process(max3421_hcd->spi_thread);
1690}
1691
1692static int
1693max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
1694 char *buf, u16 length)
1695{
1696 struct spi_device *spi = to_spi_device(hcd->self.controller);
1697 struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1698 struct max3421_hcd_platform_data *pdata;
1699 unsigned long flags;
1700 int retval = 0;
1701
1702 pdata = spi->dev.platform_data;
1703
1704 spin_lock_irqsave(&max3421_hcd->lock, flags);
1705
1706 switch (type_req) {
1707 case ClearHubFeature:
1708 break;
1709 case ClearPortFeature:
1710 switch (value) {
1711 case USB_PORT_FEAT_SUSPEND:
1712 break;
1713 case USB_PORT_FEAT_POWER:
1714 dev_dbg(hcd->self.controller, "power-off\n");
1715 max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1716 !pdata->vbus_active_level);
1717
1718 default:
1719 max3421_hcd->port_status &= ~(1 << value);
1720 }
1721 break;
1722 case GetHubDescriptor:
1723 hub_descriptor((struct usb_hub_descriptor *) buf);
1724 break;
1725
1726 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1727 case GetPortErrorCount:
1728 case SetHubDepth:
1729
1730 goto error;
1731
1732 case GetHubStatus:
1733 *(__le32 *) buf = cpu_to_le32(0);
1734 break;
1735
1736 case GetPortStatus:
1737 if (index != 1) {
1738 retval = -EPIPE;
1739 goto error;
1740 }
1741 ((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
1742 ((__le16 *) buf)[1] =
1743 cpu_to_le16(max3421_hcd->port_status >> 16);
1744 break;
1745
1746 case SetHubFeature:
1747 retval = -EPIPE;
1748 break;
1749
1750 case SetPortFeature:
1751 switch (value) {
1752 case USB_PORT_FEAT_LINK_STATE:
1753 case USB_PORT_FEAT_U1_TIMEOUT:
1754 case USB_PORT_FEAT_U2_TIMEOUT:
1755 case USB_PORT_FEAT_BH_PORT_RESET:
1756 goto error;
1757 case USB_PORT_FEAT_SUSPEND:
1758 if (max3421_hcd->active)
1759 max3421_hcd->port_status |=
1760 USB_PORT_STAT_SUSPEND;
1761 break;
1762 case USB_PORT_FEAT_POWER:
1763 dev_dbg(hcd->self.controller, "power-on\n");
1764 max3421_hcd->port_status |= USB_PORT_STAT_POWER;
1765 max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1766 pdata->vbus_active_level);
1767 break;
1768 case USB_PORT_FEAT_RESET:
1769 max3421_reset_port(hcd);
1770
1771 default:
1772 if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
1773 != 0)
1774 max3421_hcd->port_status |= (1 << value);
1775 }
1776 break;
1777
1778 default:
1779 dev_dbg(hcd->self.controller,
1780 "hub control req%04x v%04x i%04x l%d\n",
1781 type_req, value, index, length);
1782error:
1783 retval = -EPIPE;
1784 }
1785
1786 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1787 return retval;
1788}
1789
1790static int
1791max3421_bus_suspend(struct usb_hcd *hcd)
1792{
1793 return -1;
1794}
1795
1796static int
1797max3421_bus_resume(struct usb_hcd *hcd)
1798{
1799 return -1;
1800}
1801
1802
1803
1804
1805
1806static int
1807max3421_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1808{
1809 return 0;
1810}
1811
1812static void
1813max3421_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1814{
1815}
1816
1817static const struct hc_driver max3421_hcd_desc = {
1818 .description = "max3421",
1819 .product_desc = DRIVER_DESC,
1820 .hcd_priv_size = sizeof(struct max3421_hcd),
1821 .flags = HCD_USB11,
1822 .reset = max3421_reset,
1823 .start = max3421_start,
1824 .stop = max3421_stop,
1825 .get_frame_number = max3421_get_frame_number,
1826 .urb_enqueue = max3421_urb_enqueue,
1827 .urb_dequeue = max3421_urb_dequeue,
1828 .map_urb_for_dma = max3421_map_urb_for_dma,
1829 .unmap_urb_for_dma = max3421_unmap_urb_for_dma,
1830 .endpoint_disable = max3421_endpoint_disable,
1831 .hub_status_data = max3421_hub_status_data,
1832 .hub_control = max3421_hub_control,
1833 .bus_suspend = max3421_bus_suspend,
1834 .bus_resume = max3421_bus_resume,
1835};
1836
1837static int
1838max3421_of_vbus_en_pin(struct device *dev, struct max3421_hcd_platform_data *pdata)
1839{
1840 int retval;
1841 uint32_t value[2];
1842
1843 if (!pdata)
1844 return -EINVAL;
1845
1846 retval = of_property_read_u32_array(dev->of_node, "maxim,vbus-en-pin", value, 2);
1847 if (retval) {
1848 dev_err(dev, "device tree node property 'maxim,vbus-en-pin' is missing\n");
1849 return retval;
1850 }
1851 dev_info(dev, "property 'maxim,vbus-en-pin' value is <%d %d>\n", value[0], value[1]);
1852
1853 pdata->vbus_gpout = value[0];
1854 pdata->vbus_active_level = value[1];
1855
1856 return 0;
1857}
1858
1859static int
1860max3421_probe(struct spi_device *spi)
1861{
1862 struct device *dev = &spi->dev;
1863 struct max3421_hcd *max3421_hcd;
1864 struct usb_hcd *hcd = NULL;
1865 struct max3421_hcd_platform_data *pdata = NULL;
1866 int retval = -ENOMEM;
1867
1868 if (spi_setup(spi) < 0) {
1869 dev_err(&spi->dev, "Unable to setup SPI bus");
1870 return -EFAULT;
1871 }
1872
1873 if (!spi->irq) {
1874 dev_err(dev, "Failed to get SPI IRQ");
1875 return -EFAULT;
1876 }
1877
1878 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1879 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1880 if (!pdata) {
1881 retval = -ENOMEM;
1882 goto error;
1883 }
1884 retval = max3421_of_vbus_en_pin(dev, pdata);
1885 if (retval)
1886 goto error;
1887
1888 spi->dev.platform_data = pdata;
1889 }
1890
1891 pdata = spi->dev.platform_data;
1892 if (!pdata) {
1893 dev_err(&spi->dev, "driver configuration data is not provided\n");
1894 retval = -EFAULT;
1895 goto error;
1896 }
1897 if (pdata->vbus_active_level > 1) {
1898 dev_err(&spi->dev, "vbus active level value %d is out of range (0/1)\n", pdata->vbus_active_level);
1899 retval = -EINVAL;
1900 goto error;
1901 }
1902 if (pdata->vbus_gpout < 1 || pdata->vbus_gpout > MAX3421_GPOUT_COUNT) {
1903 dev_err(&spi->dev, "vbus gpout value %d is out of range (1..8)\n", pdata->vbus_gpout);
1904 retval = -EINVAL;
1905 goto error;
1906 }
1907
1908 hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
1909 dev_name(&spi->dev));
1910 if (!hcd) {
1911 dev_err(&spi->dev, "failed to create HCD structure\n");
1912 goto error;
1913 }
1914 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1915 max3421_hcd = hcd_to_max3421(hcd);
1916 max3421_hcd->next = max3421_hcd_list;
1917 max3421_hcd_list = max3421_hcd;
1918 INIT_LIST_HEAD(&max3421_hcd->ep_list);
1919
1920 max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL);
1921 if (!max3421_hcd->tx)
1922 goto error;
1923 max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL);
1924 if (!max3421_hcd->rx)
1925 goto error;
1926
1927 max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
1928 "max3421_spi_thread");
1929 if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
1930 dev_err(&spi->dev,
1931 "failed to create SPI thread (out of memory)\n");
1932 goto error;
1933 }
1934
1935 retval = usb_add_hcd(hcd, 0, 0);
1936 if (retval) {
1937 dev_err(&spi->dev, "failed to add HCD\n");
1938 goto error;
1939 }
1940
1941 retval = request_irq(spi->irq, max3421_irq_handler,
1942 IRQF_TRIGGER_LOW, "max3421", hcd);
1943 if (retval < 0) {
1944 dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
1945 goto error;
1946 }
1947 return 0;
1948
1949error:
1950 if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
1951 devm_kfree(&spi->dev, pdata);
1952 spi->dev.platform_data = NULL;
1953 }
1954
1955 if (hcd) {
1956 kfree(max3421_hcd->tx);
1957 kfree(max3421_hcd->rx);
1958 if (max3421_hcd->spi_thread)
1959 kthread_stop(max3421_hcd->spi_thread);
1960 usb_put_hcd(hcd);
1961 }
1962 return retval;
1963}
1964
1965static int
1966max3421_remove(struct spi_device *spi)
1967{
1968 struct max3421_hcd *max3421_hcd = NULL, **prev;
1969 struct usb_hcd *hcd = NULL;
1970 unsigned long flags;
1971
1972 for (prev = &max3421_hcd_list; *prev; prev = &(*prev)->next) {
1973 max3421_hcd = *prev;
1974 hcd = max3421_to_hcd(max3421_hcd);
1975 if (hcd->self.controller == &spi->dev)
1976 break;
1977 }
1978 if (!max3421_hcd) {
1979 dev_err(&spi->dev, "no MAX3421 HCD found for SPI device %p\n",
1980 spi);
1981 return -ENODEV;
1982 }
1983
1984 usb_remove_hcd(hcd);
1985
1986 spin_lock_irqsave(&max3421_hcd->lock, flags);
1987
1988 kthread_stop(max3421_hcd->spi_thread);
1989 *prev = max3421_hcd->next;
1990
1991 spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1992
1993 free_irq(spi->irq, hcd);
1994
1995 usb_put_hcd(hcd);
1996 return 0;
1997}
1998
1999static const struct of_device_id max3421_of_match_table[] = {
2000 { .compatible = "maxim,max3421", },
2001 {},
2002};
2003MODULE_DEVICE_TABLE(of, max3421_of_match_table);
2004
2005static struct spi_driver max3421_driver = {
2006 .probe = max3421_probe,
2007 .remove = max3421_remove,
2008 .driver = {
2009 .name = "max3421-hcd",
2010 .of_match_table = of_match_ptr(max3421_of_match_table),
2011 },
2012};
2013
2014module_spi_driver(max3421_driver);
2015
2016MODULE_DESCRIPTION(DRIVER_DESC);
2017MODULE_AUTHOR("David Mosberger <davidm@egauge.net>");
2018MODULE_LICENSE("GPL");
2019