Lines Matching +full:full +full:- +full:bit

1 // SPDX-License-Identifier: GPL-2.0
5 * Author: David Mosberger-Tang <davidm@egauge.net>
7 * (C) Copyright 2014 David Mosberger-Tang <davidm@egauge.net>
9 * MAX3421 is a chip implementing a USB 2.0 Full-/Low-Speed host
16 * https://www.hdl.co.jp/ftpdata/utl-001/AN3785.pdf
24 * Important note on worst-case (full-speed) packet size constraints
27 * - control: 64 bytes
28 * - isochronous: 1023 bytes
29 * - interrupt: 64 bytes
30 * - bulk: 64 bytes
33 * multi-FIFO writes/reads for a single USB packet *except* for isochronous
38 * (AN3785) has conflicting info for the RCVDAVIRQ bit:
41 * this IRQ bit (by writing a 1 to it) before reading the
44 * However, the earlier section on "Programming BULK-IN
48 * RCVDAVIRQ bit.
54 * transactions while holding a spin-lock (and/or while interrupts are
66 #include <linux/platform_data/max3421-hcd.h>
68 #define DRIVER_DESC "MAX3421 USB Host-Controller Driver"
71 /* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
81 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
83 /* Port-change mask: */
110 /* Bit numbers for max3421_hcd->todo: */
140 * SPI-thread without acquiring the HCD lock:
146 * cache-lines:
158 int urb_done; /* > 0 -> no errors, < 0: errno */
243 MAX3421_HI_BUSEVENT_BIT = 0, /* bus-reset/-resume */
299 /* Return same error-codes as ohci.h:cc_to_error: */
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
332 #define field(val, bit) ((val) << (bit)) argument
337 return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1); in frame_diff()
343 return (struct max3421_hcd *) hcd->hcd_priv; in hcd_to_max3421()
356 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_rd8()
364 max3421_hcd->tx->data[0] = in spi_rd8()
368 transfer.tx_buf = max3421_hcd->tx->data; in spi_rd8()
369 transfer.rx_buf = max3421_hcd->rx->data; in spi_rd8()
375 return max3421_hcd->rx->data[1]; in spi_rd8()
381 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_wr8()
390 max3421_hcd->tx->data[0] = in spi_wr8()
393 max3421_hcd->tx->data[1] = val; in spi_wr8()
395 transfer.tx_buf = max3421_hcd->tx->data; in spi_wr8()
405 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_rd_buf()
414 max3421_hcd->tx->data[0] = in spi_rd_buf()
417 transfer[0].tx_buf = max3421_hcd->tx->data; in spi_rd_buf()
431 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_wr_buf()
440 max3421_hcd->tx->data[0] = in spi_wr_buf()
444 transfer[0].tx_buf = max3421_hcd->tx->data; in spi_wr_buf()
457 * bits. The HUBPRE bit needs to be set when MAX3421E operates at
458 * full speed, but it's talking to a low-speed device (i.e., through a
459 * hub). Setting that bit ensures that every low-speed packet is
460 * preceded by a full-speed PRE PID. Possible configurations:
462 * Hub speed: Device speed: => LOWSPEED bit: HUBPRE bit:
463 * FULL FULL => 0 0
464 * FULL LOW => 1 1
466 * LOW FULL => 1 0
472 u8 mode_lowspeed, mode_hubpre, mode = max3421_hcd->mode; in max3421_set_speed()
474 mode_lowspeed = BIT(MAX3421_MODE_LOWSPEED_BIT); in max3421_set_speed()
475 mode_hubpre = BIT(MAX3421_MODE_HUBPRE_BIT); in max3421_set_speed()
476 if (max3421_hcd->port_status & USB_PORT_STAT_LOW_SPEED) { in max3421_set_speed()
479 } else if (dev->speed == USB_SPEED_LOW) { in max3421_set_speed()
484 if (mode != max3421_hcd->mode) { in max3421_set_speed()
485 max3421_hcd->mode = mode; in max3421_set_speed()
486 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode); in max3421_set_speed()
503 old_dev = max3421_hcd->loaded_dev; in max3421_set_address()
504 old_epnum = max3421_hcd->loaded_epnum; in max3421_set_address()
511 /* save the old end-points toggles: */ in max3421_set_address()
524 hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) | in max3421_set_address()
525 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT)); in max3421_set_address()
527 max3421_hcd->loaded_epnum = epnum; in max3421_set_address()
532 * address-assignment so it's best to just always load the in max3421_set_address()
533 * address whenever the end-point changed/was forced. in max3421_set_address()
535 max3421_hcd->loaded_dev = dev; in max3421_set_address()
536 spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum); in max3421_set_address()
542 spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8); in max3421_ctrl_setup()
550 int epnum = usb_pipeendpoint(urb->pipe); in max3421_transfer_in()
552 max3421_hcd->curr_len = 0; in max3421_transfer_in()
553 max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT); in max3421_transfer_in()
560 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_transfer_out()
562 int epnum = usb_pipeendpoint(urb->pipe); in max3421_transfer_out()
566 src = urb->transfer_buffer + urb->actual_length; in max3421_transfer_out()
569 if (max3421_hcd->rev == 0x12) { in max3421_transfer_out()
573 spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len); in max3421_transfer_out()
578 max_packet = usb_maxpacket(urb->dev, urb->pipe, 1); in max3421_transfer_out()
585 dev_err(&spi->dev, in max3421_transfer_out()
586 "%s: packet-size of %u too big (limit is %u bytes)", in max3421_transfer_out()
588 max3421_hcd->urb_done = -EMSGSIZE; in max3421_transfer_out()
589 return -EMSGSIZE; in max3421_transfer_out()
591 max3421_hcd->curr_len = min((urb->transfer_buffer_length - in max3421_transfer_out()
592 urb->actual_length), max_packet); in max3421_transfer_out()
594 spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len); in max3421_transfer_out()
595 spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len); in max3421_transfer_out()
600 * Issue the next host-transfer command.
607 struct urb *urb = max3421_hcd->curr_urb; in max3421_next_transfer()
609 int cmd = -EINVAL; in max3421_next_transfer()
614 max3421_ep = urb->ep->hcpriv; in max3421_next_transfer()
616 switch (max3421_ep->pkt_state) { in max3421_next_transfer()
643 /* issue the command and wait for host-xfer-done interrupt: */ in max3421_next_transfer()
646 max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT); in max3421_next_transfer()
656 * frame to process non-periodic (bulk & control) transfers.
660 * o max3421_hcd->curr_urb MUST BE NULL.
666 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_select_and_start_urb()
675 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_select_and_start_urb()
678 max3421_hcd->sched_pass < SCHED_PASS_DONE; in max3421_select_and_start_urb()
679 ++max3421_hcd->sched_pass) in max3421_select_and_start_urb()
680 list_for_each(pos, &max3421_hcd->ep_list) { in max3421_select_and_start_urb()
684 ep = max3421_ep->ep; in max3421_select_and_start_urb()
686 switch (usb_endpoint_type(&ep->desc)) { in max3421_select_and_start_urb()
689 if (max3421_hcd->sched_pass != in max3421_select_and_start_urb()
696 if (max3421_hcd->sched_pass != in max3421_select_and_start_urb()
702 if (list_empty(&ep->urb_list)) in max3421_select_and_start_urb()
704 urb = list_first_entry(&ep->urb_list, struct urb, in max3421_select_and_start_urb()
706 if (urb->unlinked) { in max3421_select_and_start_urb()
707 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d", in max3421_select_and_start_urb()
708 __func__, urb, urb->unlinked); in max3421_select_and_start_urb()
709 max3421_hcd->curr_urb = urb; in max3421_select_and_start_urb()
710 max3421_hcd->urb_done = 1; in max3421_select_and_start_urb()
711 spin_unlock_irqrestore(&max3421_hcd->lock, in max3421_select_and_start_urb()
716 switch (usb_endpoint_type(&ep->desc)) { in max3421_select_and_start_urb()
722 if (frame_diff(max3421_ep->last_active, in max3421_select_and_start_urb()
723 max3421_hcd->frame_number) == 0) in max3421_select_and_start_urb()
728 if (max3421_ep->retransmit in max3421_select_and_start_urb()
729 && (frame_diff(max3421_ep->last_active, in max3421_select_and_start_urb()
730 max3421_hcd->frame_number) in max3421_select_and_start_urb()
742 if (frame_diff(max3421_hcd->frame_number, in max3421_select_and_start_urb()
743 max3421_ep->last_active) in max3421_select_and_start_urb()
744 < urb->interval) in max3421_select_and_start_urb()
747 * end-point in the current in max3421_select_and_start_urb()
755 list_move_tail(pos, &max3421_hcd->ep_list); in max3421_select_and_start_urb()
761 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_select_and_start_urb()
765 urb = max3421_hcd->curr_urb = curr_urb; in max3421_select_and_start_urb()
766 epnum = usb_endpoint_num(&urb->ep->desc); in max3421_select_and_start_urb()
767 if (max3421_ep->retransmit) in max3421_select_and_start_urb()
769 max3421_ep->retransmit = 0; in max3421_select_and_start_urb()
772 if (usb_endpoint_xfer_control(&ep->desc)) { in max3421_select_and_start_urb()
777 usb_settoggle(urb->dev, epnum, 0, 1); in max3421_select_and_start_urb()
778 usb_settoggle(urb->dev, epnum, 1, 1); in max3421_select_and_start_urb()
779 max3421_ep->pkt_state = PKT_STATE_SETUP; in max3421_select_and_start_urb()
782 max3421_ep->pkt_state = PKT_STATE_TRANSFER; in max3421_select_and_start_urb()
785 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_select_and_start_urb()
787 max3421_ep->last_active = max3421_hcd->frame_number; in max3421_select_and_start_urb()
788 max3421_set_address(hcd, urb->dev, epnum, force_toggles); in max3421_select_and_start_urb()
789 max3421_set_speed(hcd, urb->dev); in max3421_select_and_start_urb()
802 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_check_unlink()
810 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_check_unlink()
811 list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) { in max3421_check_unlink()
812 ep = max3421_ep->ep; in max3421_check_unlink()
813 list_for_each_entry_safe(urb, next, &ep->urb_list, urb_list) { in max3421_check_unlink()
814 if (urb->unlinked) { in max3421_check_unlink()
816 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d", in max3421_check_unlink()
817 __func__, urb, urb->unlinked); in max3421_check_unlink()
819 spin_unlock_irqrestore(&max3421_hcd->lock, in max3421_check_unlink()
822 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_check_unlink()
826 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_check_unlink()
837 struct urb *urb = max3421_hcd->curr_urb; in max3421_slow_retransmit()
840 max3421_ep = urb->ep->hcpriv; in max3421_slow_retransmit()
841 max3421_ep->retransmit = 1; in max3421_slow_retransmit()
842 max3421_hcd->curr_urb = NULL; in max3421_slow_retransmit()
852 struct urb *urb = max3421_hcd->curr_urb; in max3421_recv_data_available()
860 if (urb->actual_length >= urb->transfer_buffer_length) in max3421_recv_data_available()
863 remaining = urb->transfer_buffer_length - urb->actual_length; in max3421_recv_data_available()
868 void *dst = urb->transfer_buffer + urb->actual_length; in max3421_recv_data_available()
871 urb->actual_length += transfer_size; in max3421_recv_data_available()
872 max3421_hcd->curr_len = transfer_size; in max3421_recv_data_available()
876 spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT)); in max3421_recv_data_available()
882 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_handle_error()
885 struct urb *urb = max3421_hcd->curr_urb; in max3421_handle_error()
886 struct max3421_ep *max3421_ep = urb->ep->hcpriv; in max3421_handle_error()
891 * (i.e., error or NAK), we have to perform a dummy-write to in max3421_handle_error()
895 switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER && in max3421_handle_error()
906 case MAX3421_HRSL_KERR: /* K-state instead of response */ in max3421_handle_error()
907 case MAX3421_HRSL_JERR: /* J-state instead of response */ in max3421_handle_error()
912 max3421_hcd->urb_done = hrsl_to_error[result_code]; in max3421_handle_error()
913 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x", in max3421_handle_error()
921 /* flip the send toggle bit: */ in max3421_handle_error()
926 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT)); in max3421_handle_error()
935 if (max3421_ep->retries++ < USB_MAX_RETRIES) in max3421_handle_error()
940 max3421_hcd->urb_done = hrsl_to_error[result_code]; in max3421_handle_error()
941 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x", in max3421_handle_error()
947 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x", in max3421_handle_error()
949 max3421_hcd->urb_done = hrsl_to_error[result_code]; in max3421_handle_error()
957 if (max3421_ep->naks++ < NAK_MAX_FAST_RETRANSMITS) { in max3421_handle_error()
974 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_transfer_in_done()
978 if (urb->actual_length >= urb->transfer_buffer_length) in max3421_transfer_in_done()
982 * USB 2.0 Section 5.3.2 Pipes: packets must be full size in max3421_transfer_in_done()
985 max_packet = usb_maxpacket(urb->dev, urb->pipe, 0); in max3421_transfer_in_done()
991 dev_err(&spi->dev, in max3421_transfer_in_done()
992 "%s: packet-size of %u too big (limit is %u bytes)", in max3421_transfer_in_done()
994 return -EINVAL; in max3421_transfer_in_done()
997 if (max3421_hcd->curr_len < max_packet) { in max3421_transfer_in_done()
998 if (urb->transfer_flags & URB_SHORT_NOT_OK) { in max3421_transfer_in_done()
1001 * unexpected partial packet -> in max3421_transfer_in_done()
1004 return -EREMOTEIO; in max3421_transfer_in_done()
1020 urb->actual_length += max3421_hcd->curr_len; in max3421_transfer_out_done()
1021 if (urb->actual_length < urb->transfer_buffer_length) in max3421_transfer_out_done()
1023 if (urb->transfer_flags & URB_ZERO_PACKET) { in max3421_transfer_out_done()
1025 * Some hardware needs a zero-size packet at the end in max3421_transfer_out_done()
1026 * of a bulk-out transfer if the last transfer was a in max3421_transfer_out_done()
1027 * full-sized packet (i.e., such hardware use < in max3421_transfer_out_done()
1031 u32 max_packet = usb_maxpacket(urb->dev, urb->pipe, 1); in max3421_transfer_out_done()
1033 if (max3421_hcd->curr_len == max_packet) in max3421_transfer_out_done()
1046 struct urb *urb = max3421_hcd->curr_urb; in max3421_host_transfer_done()
1051 max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) | in max3421_host_transfer_done()
1052 BIT(MAX3421_HI_RCVDAV_BIT)); in max3421_host_transfer_done()
1058 ++max3421_hcd->err_stat[result_code]; in max3421_host_transfer_done()
1061 max3421_ep = urb->ep->hcpriv; in max3421_host_transfer_done()
1068 max3421_ep->naks = 0; in max3421_host_transfer_done()
1069 max3421_ep->retries = 0; in max3421_host_transfer_done()
1070 switch (max3421_ep->pkt_state) { in max3421_host_transfer_done()
1073 if (urb->transfer_buffer_length > 0) in max3421_host_transfer_done()
1074 max3421_ep->pkt_state = PKT_STATE_TRANSFER; in max3421_host_transfer_done()
1076 max3421_ep->pkt_state = PKT_STATE_TERMINATE; in max3421_host_transfer_done()
1084 if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) { in max3421_host_transfer_done()
1086 * We aren't really done - we still need to in max3421_host_transfer_done()
1089 max3421_hcd->urb_done = urb_done = 0; in max3421_host_transfer_done()
1090 max3421_ep->pkt_state = PKT_STATE_TERMINATE; in max3421_host_transfer_done()
1100 max3421_hcd->urb_done = urb_done; in max3421_host_transfer_done()
1122 mode = max3421_hcd->mode; in max3421_detect_conn()
1127 * Turn off SOFKAENAB bit to avoid getting interrupt in max3421_detect_conn()
1128 * every milli-second: in max3421_detect_conn()
1130 mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT); in max3421_detect_conn()
1133 case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */ in max3421_detect_conn()
1134 case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */ in max3421_detect_conn()
1137 mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT); in max3421_detect_conn()
1138 /* turn on SOFKAENAB bit: */ in max3421_detect_conn()
1139 mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT); in max3421_detect_conn()
1147 max3421_hcd->mode = mode; in max3421_detect_conn()
1148 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode); in max3421_detect_conn()
1150 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_detect_conn()
1151 old_port_status = max3421_hcd->port_status; in max3421_detect_conn()
1153 max3421_hcd->port_status |= USB_PORT_STAT_CONNECTION; in max3421_detect_conn()
1155 max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION; in max3421_detect_conn()
1156 if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT)) in max3421_detect_conn()
1157 max3421_hcd->port_status |= USB_PORT_STAT_LOW_SPEED; in max3421_detect_conn()
1159 max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED; in max3421_detect_conn()
1160 chg = (old_port_status ^ max3421_hcd->port_status); in max3421_detect_conn()
1161 max3421_hcd->port_status |= chg << 16; in max3421_detect_conn()
1162 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_detect_conn()
1169 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_irq_handler()
1172 if (max3421_hcd->spi_thread && in max3421_irq_handler()
1173 max3421_hcd->spi_thread->state != TASK_RUNNING) in max3421_irq_handler()
1174 wake_up_process(max3421_hcd->spi_thread); in max3421_irq_handler()
1175 if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo)) in max3421_irq_handler()
1176 disable_irq_nosync(spi->irq); in max3421_irq_handler()
1193 spin_lock_irqsave(&max3421_hcd->lock, flags); in dump_eps()
1194 list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) { in dump_eps()
1195 ep = max3421_ep->ep; in dump_eps()
1200 list_for_each_entry(urb, &ep->urb_list, urb_list) { in dump_eps()
1201 ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb, in dump_eps()
1202 usb_pipetype(urb->pipe), in dump_eps()
1204 urb->actual_length, in dump_eps()
1205 urb->transfer_buffer_length); in dump_eps()
1206 if (ret < 0 || ret >= end - dp) in dump_eps()
1207 break; /* error or buffer full */ in dump_eps()
1211 epnum = usb_endpoint_num(&ep->desc); in dump_eps()
1213 epnum, max3421_ep->pkt_state, max3421_ep->last_active, in dump_eps()
1214 max3421_ep->retries, max3421_ep->naks, in dump_eps()
1215 max3421_ep->retransmit, ubuf); in dump_eps()
1217 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in dump_eps()
1237 hirq &= max3421_hcd->hien; in max3421_handle_irqs()
1242 hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) | in max3421_handle_irqs()
1243 BIT(MAX3421_HI_RCVDAV_BIT))); in max3421_handle_irqs()
1245 if (hirq & BIT(MAX3421_HI_FRAME_BIT)) { in max3421_handle_irqs()
1246 max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1) in max3421_handle_irqs()
1248 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC; in max3421_handle_irqs()
1251 if (hirq & BIT(MAX3421_HI_RCVDAV_BIT)) in max3421_handle_irqs()
1254 if (hirq & BIT(MAX3421_HI_HXFRDN_BIT)) in max3421_handle_irqs()
1257 if (hirq & BIT(MAX3421_HI_CONDET_BIT)) in max3421_handle_irqs()
1262 * other than the end-points: in max3421_handle_irqs()
1264 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_handle_irqs()
1266 old_port_status = max3421_hcd->port_status; in max3421_handle_irqs()
1267 if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) { in max3421_handle_irqs()
1268 if (max3421_hcd->port_status & USB_PORT_STAT_RESET) { in max3421_handle_irqs()
1270 max3421_hcd->port_status &= ~USB_PORT_STAT_RESET; in max3421_handle_irqs()
1271 max3421_hcd->port_status |= USB_PORT_STAT_ENABLE; in max3421_handle_irqs()
1277 if (hirq & BIT(MAX3421_HI_RWU_BIT)) in max3421_handle_irqs()
1279 if (hirq & BIT(MAX3421_HI_SUSDN_BIT)) in max3421_handle_irqs()
1282 chg = (old_port_status ^ max3421_hcd->port_status); in max3421_handle_irqs()
1283 max3421_hcd->port_status |= chg << 16; in max3421_handle_irqs()
1285 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_handle_irqs()
1298 int ret = snprintf(dp, end - dp, " %lu", in max3421_handle_irqs()
1299 max3421_hcd->err_stat[i]); in max3421_handle_irqs()
1300 if (ret < 0 || ret >= end - dp) in max3421_handle_irqs()
1301 break; /* error or buffer full */ in max3421_handle_irqs()
1305 memset(max3421_hcd->err_stat, 0, in max3421_handle_irqs()
1306 sizeof(max3421_hcd->err_stat)); in max3421_handle_irqs()
1319 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_reset_hcd()
1324 spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT)); in max3421_reset_hcd()
1330 & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT)) in max3421_reset_hcd()
1332 if (--timeout < 0) { in max3421_reset_hcd()
1333 dev_err(&spi->dev, in max3421_reset_hcd()
1342 * enable pull-down registers on DM/DP: in max3421_reset_hcd()
1344 max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) | in max3421_reset_hcd()
1345 BIT(MAX3421_MODE_SOFKAENAB_BIT) | in max3421_reset_hcd()
1346 BIT(MAX3421_MODE_DMPULLDN_BIT) | in max3421_reset_hcd()
1347 BIT(MAX3421_MODE_DPPULLDN_BIT)); in max3421_reset_hcd()
1348 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode); in max3421_reset_hcd()
1350 /* reset frame-number: */ in max3421_reset_hcd()
1351 max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER; in max3421_reset_hcd()
1352 spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT)); in max3421_reset_hcd()
1354 /* sample the state of the D+ and D- lines */ in max3421_reset_hcd()
1355 spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT)); in max3421_reset_hcd()
1358 /* enable frame, connection-detected, and bus-event interrupts: */ in max3421_reset_hcd()
1359 max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) | in max3421_reset_hcd()
1360 BIT(MAX3421_HI_CONDET_BIT) | in max3421_reset_hcd()
1361 BIT(MAX3421_HI_BUSEVENT_BIT)); in max3421_reset_hcd()
1362 spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien); in max3421_reset_hcd()
1365 spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT)); in max3421_reset_hcd()
1377 status = max3421_hcd->urb_done; in max3421_urb_done()
1378 max3421_hcd->urb_done = 0; in max3421_urb_done()
1381 urb = max3421_hcd->curr_urb; in max3421_urb_done()
1383 max3421_hcd->curr_urb = NULL; in max3421_urb_done()
1384 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_urb_done()
1386 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_urb_done()
1398 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_spi_thread()
1402 /* set full-duplex SPI mode, low-active interrupt pin: */ in max3421_spi_thread()
1404 (BIT(MAX3421_PINCTL_FDUPSPI_BIT) | /* full-duplex */ in max3421_spi_thread()
1405 BIT(MAX3421_PINCTL_INTLEVEL_BIT))); /* low-active irq */ in max3421_spi_thread()
1408 max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION); in max3421_spi_thread()
1409 if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13) in max3421_spi_thread()
1411 dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev); in max3421_spi_thread()
1414 dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n", in max3421_spi_thread()
1415 max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word, in max3421_spi_thread()
1416 spi->irq); in max3421_spi_thread()
1425 spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien); in max3421_spi_thread()
1428 if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo)) in max3421_spi_thread()
1429 enable_irq(spi->irq); in max3421_spi_thread()
1436 if (max3421_hcd->urb_done) in max3421_spi_thread()
1440 else if (!max3421_hcd->curr_urb) in max3421_spi_thread()
1443 if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo)) in max3421_spi_thread()
1446 if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) { in max3421_spi_thread()
1449 BIT(MAX3421_HCTL_BUSRST_BIT)); in max3421_spi_thread()
1452 if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo)) in max3421_spi_thread()
1454 if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) { in max3421_spi_thread()
1456 * IOPINS1/IOPINS2 do not auto-increment, so we can't in max3421_spi_thread()
1459 for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) { in max3421_spi_thread()
1463 (max3421_hcd->iopins[i] & 0x0f)); in max3421_spi_thread()
1465 max3421_hcd->iopins[i] = val; in max3421_spi_thread()
1471 dev_info(&spi->dev, "SPI thread exiting"); in max3421_spi_thread()
1480 max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE | in max3421_reset_port()
1482 max3421_hcd->port_status |= USB_PORT_STAT_RESET; in max3421_reset_port()
1483 set_bit(RESET_PORT, &max3421_hcd->todo); in max3421_reset_port()
1484 wake_up_process(max3421_hcd->spi_thread); in max3421_reset_port()
1493 hcd->self.sg_tablesize = 0; in max3421_reset()
1494 hcd->speed = HCD_USB2; in max3421_reset()
1495 hcd->self.root_hub->speed = USB_SPEED_FULL; in max3421_reset()
1496 set_bit(RESET_HCD, &max3421_hcd->todo); in max3421_reset()
1497 wake_up_process(max3421_hcd->spi_thread); in max3421_reset()
1506 spin_lock_init(&max3421_hcd->lock); in max3421_start()
1507 max3421_hcd->rh_state = MAX3421_RH_RUNNING; in max3421_start()
1509 INIT_LIST_HEAD(&max3421_hcd->ep_list); in max3421_start()
1511 hcd->power_budget = POWER_BUDGET; in max3421_start()
1512 hcd->state = HC_STATE_RUNNING; in max3421_start()
1513 hcd->uses_new_polling = 1; in max3421_start()
1525 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_urb_enqueue()
1531 switch (usb_pipetype(urb->pipe)) { in max3421_urb_enqueue()
1534 if (urb->interval < 0) { in max3421_urb_enqueue()
1535 dev_err(&spi->dev, in max3421_urb_enqueue()
1536 "%s: interval=%d for intr-/iso-pipe; expected > 0\n", in max3421_urb_enqueue()
1537 __func__, urb->interval); in max3421_urb_enqueue()
1538 return -EINVAL; in max3421_urb_enqueue()
1544 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_urb_enqueue()
1546 max3421_ep = urb->ep->hcpriv; in max3421_urb_enqueue()
1551 retval = -ENOMEM; in max3421_urb_enqueue()
1554 max3421_ep->ep = urb->ep; in max3421_urb_enqueue()
1555 max3421_ep->last_active = max3421_hcd->frame_number; in max3421_urb_enqueue()
1556 urb->ep->hcpriv = max3421_ep; in max3421_urb_enqueue()
1558 list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list); in max3421_urb_enqueue()
1564 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC; in max3421_urb_enqueue()
1565 wake_up_process(max3421_hcd->spi_thread); in max3421_urb_enqueue()
1569 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_urb_enqueue()
1580 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_urb_dequeue()
1583 * This will set urb->unlinked which in turn causes the entry in max3421_urb_dequeue()
1588 set_bit(CHECK_UNLINK, &max3421_hcd->todo); in max3421_urb_dequeue()
1589 wake_up_process(max3421_hcd->spi_thread); in max3421_urb_dequeue()
1591 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_urb_dequeue()
1601 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_endpoint_disable()
1603 if (ep->hcpriv) { in max3421_endpoint_disable()
1604 struct max3421_ep *max3421_ep = ep->hcpriv; in max3421_endpoint_disable()
1607 if (!list_empty(&max3421_ep->ep_list)) in max3421_endpoint_disable()
1608 list_del(&max3421_ep->ep_list); in max3421_endpoint_disable()
1610 ep->hcpriv = NULL; in max3421_endpoint_disable()
1613 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_endpoint_disable()
1620 return max3421_hcd->frame_number; in max3421_get_frame_number()
1624 * Should return a non-zero value when any port is undergoing a resume
1634 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_hub_status_data()
1639 if ((max3421_hcd->port_status & PORT_C_MASK) != 0) { in max3421_hub_status_data()
1640 *buf = (1 << 1); /* a hub over-current condition exists */ in max3421_hub_status_data()
1641 dev_dbg(hcd->self.controller, in max3421_hub_status_data()
1643 max3421_hcd->port_status); in max3421_hub_status_data()
1645 if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED) in max3421_hub_status_data()
1649 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_hub_status_data()
1658 * See Table 11-13: Hub Descriptor in USB 2.0 spec. in hub_descriptor()
1660 desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */ in hub_descriptor()
1661 desc->bDescLength = 9; in hub_descriptor()
1662 desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | in hub_descriptor()
1664 desc->bNbrPorts = 1; in hub_descriptor()
1668 * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1669 * VALUE (0 or 1). PIN_NUMBER may be in the range from 1-8. For
1670 * any other value, this function acts as a no-op.
1678 --pin_number; in max3421_gpout_set_value()
1686 max3421_hcd->iopins[idx] |= mask; in max3421_gpout_set_value()
1688 max3421_hcd->iopins[idx] &= ~mask; in max3421_gpout_set_value()
1689 set_bit(IOPIN_UPDATE, &max3421_hcd->todo); in max3421_gpout_set_value()
1690 wake_up_process(max3421_hcd->spi_thread); in max3421_gpout_set_value()
1697 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_hub_control()
1703 pdata = spi->dev.platform_data; in max3421_hub_control()
1705 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_hub_control()
1715 dev_dbg(hcd->self.controller, "power-off\n"); in max3421_hub_control()
1716 max3421_gpout_set_value(hcd, pdata->vbus_gpout, in max3421_hub_control()
1717 !pdata->vbus_active_level); in max3421_hub_control()
1720 max3421_hcd->port_status &= ~(1 << value); in max3421_hub_control()
1739 retval = -EPIPE; in max3421_hub_control()
1742 ((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status); in max3421_hub_control()
1744 cpu_to_le16(max3421_hcd->port_status >> 16); in max3421_hub_control()
1748 retval = -EPIPE; in max3421_hub_control()
1759 if (max3421_hcd->active) in max3421_hub_control()
1760 max3421_hcd->port_status |= in max3421_hub_control()
1764 dev_dbg(hcd->self.controller, "power-on\n"); in max3421_hub_control()
1765 max3421_hcd->port_status |= USB_PORT_STAT_POWER; in max3421_hub_control()
1766 max3421_gpout_set_value(hcd, pdata->vbus_gpout, in max3421_hub_control()
1767 pdata->vbus_active_level); in max3421_hub_control()
1773 if ((max3421_hcd->port_status & USB_PORT_STAT_POWER) in max3421_hub_control()
1775 max3421_hcd->port_status |= (1 << value); in max3421_hub_control()
1780 dev_dbg(hcd->self.controller, in max3421_hub_control()
1784 retval = -EPIPE; in max3421_hub_control()
1787 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_hub_control()
1794 return -1; in max3421_bus_suspend()
1800 return -1; in max3421_bus_resume()
1828 return -EINVAL; in max3421_of_vbus_en_pin()
1830 retval = of_property_read_u32_array(dev->of_node, "maxim,vbus-en-pin", value, 2); in max3421_of_vbus_en_pin()
1832 dev_err(dev, "device tree node property 'maxim,vbus-en-pin' is missing\n"); in max3421_of_vbus_en_pin()
1835 dev_info(dev, "property 'maxim,vbus-en-pin' value is <%d %d>\n", value[0], value[1]); in max3421_of_vbus_en_pin()
1837 pdata->vbus_gpout = value[0]; in max3421_of_vbus_en_pin()
1838 pdata->vbus_active_level = value[1]; in max3421_of_vbus_en_pin()
1846 struct device *dev = &spi->dev; in max3421_probe()
1850 int retval = -ENOMEM; in max3421_probe()
1853 dev_err(&spi->dev, "Unable to setup SPI bus"); in max3421_probe()
1854 return -EFAULT; in max3421_probe()
1857 if (!spi->irq) { in max3421_probe()
1859 return -EFAULT; in max3421_probe()
1862 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { in max3421_probe()
1863 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL); in max3421_probe()
1865 retval = -ENOMEM; in max3421_probe()
1872 spi->dev.platform_data = pdata; in max3421_probe()
1875 pdata = spi->dev.platform_data; in max3421_probe()
1877 dev_err(&spi->dev, "driver configuration data is not provided\n"); in max3421_probe()
1878 retval = -EFAULT; in max3421_probe()
1881 if (pdata->vbus_active_level > 1) { in max3421_probe()
1882 …dev_err(&spi->dev, "vbus active level value %d is out of range (0/1)\n", pdata->vbus_active_level); in max3421_probe()
1883 retval = -EINVAL; in max3421_probe()
1886 if (pdata->vbus_gpout < 1 || pdata->vbus_gpout > MAX3421_GPOUT_COUNT) { in max3421_probe()
1887 dev_err(&spi->dev, "vbus gpout value %d is out of range (1..8)\n", pdata->vbus_gpout); in max3421_probe()
1888 retval = -EINVAL; in max3421_probe()
1892 hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev, in max3421_probe()
1893 dev_name(&spi->dev)); in max3421_probe()
1895 dev_err(&spi->dev, "failed to create HCD structure\n"); in max3421_probe()
1898 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in max3421_probe()
1900 max3421_hcd->next = max3421_hcd_list; in max3421_probe()
1902 INIT_LIST_HEAD(&max3421_hcd->ep_list); in max3421_probe()
1904 max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL); in max3421_probe()
1905 if (!max3421_hcd->tx) in max3421_probe()
1907 max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL); in max3421_probe()
1908 if (!max3421_hcd->rx) in max3421_probe()
1911 max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd, in max3421_probe()
1913 if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) { in max3421_probe()
1914 dev_err(&spi->dev, in max3421_probe()
1921 dev_err(&spi->dev, "failed to add HCD\n"); in max3421_probe()
1925 retval = request_irq(spi->irq, max3421_irq_handler, in max3421_probe()
1928 dev_err(&spi->dev, "failed to request irq %d\n", spi->irq); in max3421_probe()
1934 if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) { in max3421_probe()
1935 devm_kfree(&spi->dev, pdata); in max3421_probe()
1936 spi->dev.platform_data = NULL; in max3421_probe()
1940 kfree(max3421_hcd->tx); in max3421_probe()
1941 kfree(max3421_hcd->rx); in max3421_probe()
1942 if (max3421_hcd->spi_thread) in max3421_probe()
1943 kthread_stop(max3421_hcd->spi_thread); in max3421_probe()
1956 for (prev = &max3421_hcd_list; *prev; prev = &(*prev)->next) { in max3421_remove()
1959 if (hcd->self.controller == &spi->dev) in max3421_remove()
1963 dev_err(&spi->dev, "no MAX3421 HCD found for SPI device %p\n", in max3421_remove()
1965 return -ENODEV; in max3421_remove()
1970 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_remove()
1972 kthread_stop(max3421_hcd->spi_thread); in max3421_remove()
1973 *prev = max3421_hcd->next; in max3421_remove()
1975 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_remove()
1977 free_irq(spi->irq, hcd); in max3421_remove()
1993 .name = "max3421-hcd",