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): */
75 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
77 /* Port-change mask: */
104 /* Bit numbers for max3421_hcd->todo: */
132 * SPI-thread without acquiring the HCD lock:
138 * cache-lines:
148 int urb_done; /* > 0 -> no errors, < 0: errno */
231 MAX3421_HI_BUSEVENT_BIT = 0, /* bus-reset/-resume */
287 /* Return same error-codes as ohci.h:cc_to_error: */
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
320 #define field(val, bit) ((val) << (bit)) argument
325 return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1); in frame_diff()
331 return (struct max3421_hcd *) hcd->hcd_priv; in hcd_to_max3421()
344 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_rd8()
352 max3421_hcd->tx->data[0] = in spi_rd8()
356 transfer.tx_buf = max3421_hcd->tx->data; in spi_rd8()
357 transfer.rx_buf = max3421_hcd->rx->data; in spi_rd8()
363 return max3421_hcd->rx->data[1]; in spi_rd8()
369 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_wr8()
378 max3421_hcd->tx->data[0] = in spi_wr8()
381 max3421_hcd->tx->data[1] = val; in spi_wr8()
383 transfer.tx_buf = max3421_hcd->tx->data; in spi_wr8()
393 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_rd_buf()
402 max3421_hcd->tx->data[0] = in spi_rd_buf()
405 transfer[0].tx_buf = max3421_hcd->tx->data; in spi_rd_buf()
419 struct spi_device *spi = to_spi_device(hcd->self.controller); in spi_wr_buf()
428 max3421_hcd->tx->data[0] = in spi_wr_buf()
432 transfer[0].tx_buf = max3421_hcd->tx->data; in spi_wr_buf()
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:
450 * Hub speed: Device speed: => LOWSPEED bit: HUBPRE bit:
451 * FULL FULL => 0 0
452 * FULL LOW => 1 1
454 * LOW FULL => 1 0
460 u8 mode_lowspeed, mode_hubpre, mode = max3421_hcd->mode; in max3421_set_speed()
462 mode_lowspeed = BIT(MAX3421_MODE_LOWSPEED_BIT); in max3421_set_speed()
463 mode_hubpre = BIT(MAX3421_MODE_HUBPRE_BIT); in max3421_set_speed()
464 if (max3421_hcd->port_status & USB_PORT_STAT_LOW_SPEED) { in max3421_set_speed()
467 } else if (dev->speed == USB_SPEED_LOW) { in max3421_set_speed()
472 if (mode != max3421_hcd->mode) { in max3421_set_speed()
473 max3421_hcd->mode = mode; in max3421_set_speed()
474 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode); in max3421_set_speed()
491 hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) | in max3421_set_address()
492 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT)); in max3421_set_address()
498 * address-assignment so it's best to just always load the in max3421_set_address()
499 * address whenever the end-point changed/was forced. in max3421_set_address()
501 spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum); in max3421_set_address()
507 spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8); in max3421_ctrl_setup()
515 int epnum = usb_pipeendpoint(urb->pipe); in max3421_transfer_in()
517 max3421_hcd->curr_len = 0; in max3421_transfer_in()
518 max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT); in max3421_transfer_in()
525 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_transfer_out()
527 int epnum = usb_pipeendpoint(urb->pipe); in max3421_transfer_out()
531 src = urb->transfer_buffer + urb->actual_length; in max3421_transfer_out()
534 if (max3421_hcd->rev == 0x12) { in max3421_transfer_out()
538 spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len); in max3421_transfer_out()
543 max_packet = usb_maxpacket(urb->dev, urb->pipe); in max3421_transfer_out()
550 dev_err(&spi->dev, in max3421_transfer_out()
551 "%s: packet-size of %u too big (limit is %u bytes)", in max3421_transfer_out()
553 max3421_hcd->urb_done = -EMSGSIZE; in max3421_transfer_out()
554 return -EMSGSIZE; in max3421_transfer_out()
556 max3421_hcd->curr_len = min((urb->transfer_buffer_length - in max3421_transfer_out()
557 urb->actual_length), max_packet); in max3421_transfer_out()
559 spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len); in max3421_transfer_out()
560 spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len); in max3421_transfer_out()
565 * Issue the next host-transfer command.
572 struct urb *urb = max3421_hcd->curr_urb; in max3421_next_transfer()
574 int cmd = -EINVAL; in max3421_next_transfer()
579 max3421_ep = urb->ep->hcpriv; in max3421_next_transfer()
581 switch (max3421_ep->pkt_state) { in max3421_next_transfer()
608 /* issue the command and wait for host-xfer-done interrupt: */ in max3421_next_transfer()
611 max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT); in max3421_next_transfer()
621 * frame to process non-periodic (bulk & control) transfers.
625 * o max3421_hcd->curr_urb MUST BE NULL.
631 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_select_and_start_urb()
640 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_select_and_start_urb()
643 max3421_hcd->sched_pass < SCHED_PASS_DONE; in max3421_select_and_start_urb()
644 ++max3421_hcd->sched_pass) in max3421_select_and_start_urb()
645 list_for_each(pos, &max3421_hcd->ep_list) { in max3421_select_and_start_urb()
649 ep = max3421_ep->ep; in max3421_select_and_start_urb()
651 switch (usb_endpoint_type(&ep->desc)) { in max3421_select_and_start_urb()
654 if (max3421_hcd->sched_pass != in max3421_select_and_start_urb()
661 if (max3421_hcd->sched_pass != in max3421_select_and_start_urb()
667 if (list_empty(&ep->urb_list)) in max3421_select_and_start_urb()
669 urb = list_first_entry(&ep->urb_list, struct urb, in max3421_select_and_start_urb()
671 if (urb->unlinked) { in max3421_select_and_start_urb()
672 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d", in max3421_select_and_start_urb()
673 __func__, urb, urb->unlinked); in max3421_select_and_start_urb()
674 max3421_hcd->curr_urb = urb; in max3421_select_and_start_urb()
675 max3421_hcd->urb_done = 1; in max3421_select_and_start_urb()
676 spin_unlock_irqrestore(&max3421_hcd->lock, in max3421_select_and_start_urb()
681 switch (usb_endpoint_type(&ep->desc)) { in max3421_select_and_start_urb()
687 if (frame_diff(max3421_ep->last_active, in max3421_select_and_start_urb()
688 max3421_hcd->frame_number) == 0) in max3421_select_and_start_urb()
693 if (max3421_ep->retransmit in max3421_select_and_start_urb()
694 && (frame_diff(max3421_ep->last_active, in max3421_select_and_start_urb()
695 max3421_hcd->frame_number) in max3421_select_and_start_urb()
707 if (frame_diff(max3421_hcd->frame_number, in max3421_select_and_start_urb()
708 max3421_ep->last_active) in max3421_select_and_start_urb()
709 < urb->interval) in max3421_select_and_start_urb()
712 * end-point in the current in max3421_select_and_start_urb()
720 list_move_tail(pos, &max3421_hcd->ep_list); in max3421_select_and_start_urb()
726 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_select_and_start_urb()
730 urb = max3421_hcd->curr_urb = curr_urb; in max3421_select_and_start_urb()
731 epnum = usb_endpoint_num(&urb->ep->desc); in max3421_select_and_start_urb()
732 if (max3421_ep->retransmit) in max3421_select_and_start_urb()
734 max3421_ep->retransmit = 0; in max3421_select_and_start_urb()
737 if (usb_endpoint_xfer_control(&ep->desc)) { in max3421_select_and_start_urb()
742 usb_settoggle(urb->dev, epnum, 0, 1); in max3421_select_and_start_urb()
743 usb_settoggle(urb->dev, epnum, 1, 1); in max3421_select_and_start_urb()
744 max3421_ep->pkt_state = PKT_STATE_SETUP; in max3421_select_and_start_urb()
746 max3421_ep->pkt_state = PKT_STATE_TRANSFER; in max3421_select_and_start_urb()
749 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_select_and_start_urb()
751 max3421_ep->last_active = max3421_hcd->frame_number; in max3421_select_and_start_urb()
752 max3421_set_address(hcd, urb->dev, epnum); in max3421_select_and_start_urb()
753 max3421_set_speed(hcd, urb->dev); in max3421_select_and_start_urb()
766 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_check_unlink()
774 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_check_unlink()
775 list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) { in max3421_check_unlink()
776 ep = max3421_ep->ep; in max3421_check_unlink()
777 list_for_each_entry_safe(urb, next, &ep->urb_list, urb_list) { in max3421_check_unlink()
778 if (urb->unlinked) { in max3421_check_unlink()
780 dev_dbg(&spi->dev, "%s: URB %p unlinked=%d", in max3421_check_unlink()
781 __func__, urb, urb->unlinked); in max3421_check_unlink()
783 spin_unlock_irqrestore(&max3421_hcd->lock, in max3421_check_unlink()
786 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_check_unlink()
790 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_check_unlink()
801 struct urb *urb = max3421_hcd->curr_urb; in max3421_slow_retransmit()
804 max3421_ep = urb->ep->hcpriv; in max3421_slow_retransmit()
805 max3421_ep->retransmit = 1; in max3421_slow_retransmit()
806 max3421_hcd->curr_urb = NULL; in max3421_slow_retransmit()
816 struct urb *urb = max3421_hcd->curr_urb; in max3421_recv_data_available()
824 if (urb->actual_length >= urb->transfer_buffer_length) in max3421_recv_data_available()
827 remaining = urb->transfer_buffer_length - urb->actual_length; in max3421_recv_data_available()
832 void *dst = urb->transfer_buffer + urb->actual_length; in max3421_recv_data_available()
835 urb->actual_length += transfer_size; in max3421_recv_data_available()
836 max3421_hcd->curr_len = transfer_size; in max3421_recv_data_available()
840 spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT)); in max3421_recv_data_available()
846 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_handle_error()
849 struct urb *urb = max3421_hcd->curr_urb; in max3421_handle_error()
850 struct max3421_ep *max3421_ep = urb->ep->hcpriv; in max3421_handle_error()
855 * (i.e., error or NAK), we have to perform a dummy-write to in max3421_handle_error()
859 switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER && in max3421_handle_error()
870 case MAX3421_HRSL_KERR: /* K-state instead of response */ in max3421_handle_error()
871 case MAX3421_HRSL_JERR: /* J-state instead of response */ in max3421_handle_error()
876 max3421_hcd->urb_done = hrsl_to_error[result_code]; in max3421_handle_error()
877 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x", in max3421_handle_error()
885 /* flip the send toggle bit: */ in max3421_handle_error()
890 BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT)); in max3421_handle_error()
899 if (max3421_ep->retries++ < USB_MAX_RETRIES) in max3421_handle_error()
904 max3421_hcd->urb_done = hrsl_to_error[result_code]; in max3421_handle_error()
905 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x", in max3421_handle_error()
911 dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x", in max3421_handle_error()
913 max3421_hcd->urb_done = hrsl_to_error[result_code]; in max3421_handle_error()
935 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_transfer_in_done()
939 if (urb->actual_length >= urb->transfer_buffer_length) in max3421_transfer_in_done()
943 * USB 2.0 Section 5.3.2 Pipes: packets must be full size in max3421_transfer_in_done()
946 max_packet = usb_maxpacket(urb->dev, urb->pipe); in max3421_transfer_in_done()
952 dev_err(&spi->dev, in max3421_transfer_in_done()
953 "%s: packet-size of %u too big (limit is %u bytes)", in max3421_transfer_in_done()
955 return -EINVAL; in max3421_transfer_in_done()
958 if (max3421_hcd->curr_len < max_packet) { in max3421_transfer_in_done()
959 if (urb->transfer_flags & URB_SHORT_NOT_OK) { in max3421_transfer_in_done()
962 * unexpected partial packet -> in max3421_transfer_in_done()
965 return -EREMOTEIO; in max3421_transfer_in_done()
981 urb->actual_length += max3421_hcd->curr_len; in max3421_transfer_out_done()
982 if (urb->actual_length < urb->transfer_buffer_length) in max3421_transfer_out_done()
984 if (urb->transfer_flags & URB_ZERO_PACKET) { in max3421_transfer_out_done()
986 * Some hardware needs a zero-size packet at the end in max3421_transfer_out_done()
987 * of a bulk-out transfer if the last transfer was a in max3421_transfer_out_done()
988 * full-sized packet (i.e., such hardware use < in max3421_transfer_out_done()
992 u32 max_packet = usb_maxpacket(urb->dev, urb->pipe); in max3421_transfer_out_done()
994 if (max3421_hcd->curr_len == max_packet) in max3421_transfer_out_done()
1007 struct urb *urb = max3421_hcd->curr_urb; in max3421_host_transfer_done()
1012 max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) | in max3421_host_transfer_done()
1013 BIT(MAX3421_HI_RCVDAV_BIT)); in max3421_host_transfer_done()
1019 ++max3421_hcd->err_stat[result_code]; in max3421_host_transfer_done()
1022 max3421_ep = urb->ep->hcpriv; in max3421_host_transfer_done()
1029 max3421_ep->naks = 0; in max3421_host_transfer_done()
1030 max3421_ep->retries = 0; in max3421_host_transfer_done()
1031 switch (max3421_ep->pkt_state) { in max3421_host_transfer_done()
1034 if (urb->transfer_buffer_length > 0) in max3421_host_transfer_done()
1035 max3421_ep->pkt_state = PKT_STATE_TRANSFER; in max3421_host_transfer_done()
1037 max3421_ep->pkt_state = PKT_STATE_TERMINATE; in max3421_host_transfer_done()
1045 if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) { in max3421_host_transfer_done()
1047 * We aren't really done - we still need to in max3421_host_transfer_done()
1050 max3421_hcd->urb_done = urb_done = 0; in max3421_host_transfer_done()
1051 max3421_ep->pkt_state = PKT_STATE_TERMINATE; in max3421_host_transfer_done()
1061 max3421_hcd->urb_done = urb_done; in max3421_host_transfer_done()
1083 mode = max3421_hcd->mode; in max3421_detect_conn()
1088 * Turn off SOFKAENAB bit to avoid getting interrupt in max3421_detect_conn()
1089 * every milli-second: in max3421_detect_conn()
1091 mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT); in max3421_detect_conn()
1094 case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */ in max3421_detect_conn()
1095 case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */ in max3421_detect_conn()
1098 mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT); in max3421_detect_conn()
1099 /* turn on SOFKAENAB bit: */ in max3421_detect_conn()
1100 mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT); in max3421_detect_conn()
1108 max3421_hcd->mode = mode; in max3421_detect_conn()
1109 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode); in max3421_detect_conn()
1111 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_detect_conn()
1112 old_port_status = max3421_hcd->port_status; in max3421_detect_conn()
1114 max3421_hcd->port_status |= USB_PORT_STAT_CONNECTION; in max3421_detect_conn()
1116 max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION; in max3421_detect_conn()
1117 if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT)) in max3421_detect_conn()
1118 max3421_hcd->port_status |= USB_PORT_STAT_LOW_SPEED; in max3421_detect_conn()
1120 max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED; in max3421_detect_conn()
1121 chg = (old_port_status ^ max3421_hcd->port_status); in max3421_detect_conn()
1122 max3421_hcd->port_status |= chg << 16; in max3421_detect_conn()
1123 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_detect_conn()
1130 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_irq_handler()
1133 if (max3421_hcd->spi_thread) in max3421_irq_handler()
1134 wake_up_process(max3421_hcd->spi_thread); in max3421_irq_handler()
1135 if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo)) in max3421_irq_handler()
1136 disable_irq_nosync(spi->irq); in max3421_irq_handler()
1153 spin_lock_irqsave(&max3421_hcd->lock, flags); in dump_eps()
1154 list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) { in dump_eps()
1155 ep = max3421_ep->ep; in dump_eps()
1160 list_for_each_entry(urb, &ep->urb_list, urb_list) { in dump_eps()
1161 ret = scnprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb, in dump_eps()
1162 usb_pipetype(urb->pipe), in dump_eps()
1164 urb->actual_length, in dump_eps()
1165 urb->transfer_buffer_length); in dump_eps()
1166 if (ret == end - dp - 1) in dump_eps()
1167 break; /* error or buffer full */ in dump_eps()
1171 epnum = usb_endpoint_num(&ep->desc); in dump_eps()
1173 epnum, max3421_ep->pkt_state, max3421_ep->last_active, in dump_eps()
1174 max3421_ep->retries, max3421_ep->naks, in dump_eps()
1175 max3421_ep->retransmit, ubuf); in dump_eps()
1177 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in dump_eps()
1197 hirq &= max3421_hcd->hien; in max3421_handle_irqs()
1202 hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) | in max3421_handle_irqs()
1203 BIT(MAX3421_HI_RCVDAV_BIT))); in max3421_handle_irqs()
1205 if (hirq & BIT(MAX3421_HI_FRAME_BIT)) { in max3421_handle_irqs()
1206 max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1) in max3421_handle_irqs()
1208 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC; in max3421_handle_irqs()
1211 if (hirq & BIT(MAX3421_HI_RCVDAV_BIT)) in max3421_handle_irqs()
1214 if (hirq & BIT(MAX3421_HI_HXFRDN_BIT)) in max3421_handle_irqs()
1217 if (hirq & BIT(MAX3421_HI_CONDET_BIT)) in max3421_handle_irqs()
1222 * other than the end-points: in max3421_handle_irqs()
1224 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_handle_irqs()
1226 old_port_status = max3421_hcd->port_status; in max3421_handle_irqs()
1227 if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) { in max3421_handle_irqs()
1228 if (max3421_hcd->port_status & USB_PORT_STAT_RESET) { in max3421_handle_irqs()
1230 max3421_hcd->port_status &= ~USB_PORT_STAT_RESET; in max3421_handle_irqs()
1231 max3421_hcd->port_status |= USB_PORT_STAT_ENABLE; in max3421_handle_irqs()
1237 if (hirq & BIT(MAX3421_HI_RWU_BIT)) in max3421_handle_irqs()
1239 if (hirq & BIT(MAX3421_HI_SUSDN_BIT)) in max3421_handle_irqs()
1242 chg = (old_port_status ^ max3421_hcd->port_status); in max3421_handle_irqs()
1243 max3421_hcd->port_status |= chg << 16; in max3421_handle_irqs()
1245 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_handle_irqs()
1258 int ret = scnprintf(dp, end - dp, " %lu", in max3421_handle_irqs()
1259 max3421_hcd->err_stat[i]); in max3421_handle_irqs()
1260 if (ret == end - dp - 1) in max3421_handle_irqs()
1261 break; /* error or buffer full */ in max3421_handle_irqs()
1265 memset(max3421_hcd->err_stat, 0, in max3421_handle_irqs()
1266 sizeof(max3421_hcd->err_stat)); in max3421_handle_irqs()
1279 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_reset_hcd()
1284 spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT)); in max3421_reset_hcd()
1290 & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT)) in max3421_reset_hcd()
1292 if (--timeout < 0) { in max3421_reset_hcd()
1293 dev_err(&spi->dev, in max3421_reset_hcd()
1302 * enable pull-down registers on DM/DP: in max3421_reset_hcd()
1304 max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) | in max3421_reset_hcd()
1305 BIT(MAX3421_MODE_SOFKAENAB_BIT) | in max3421_reset_hcd()
1306 BIT(MAX3421_MODE_DMPULLDN_BIT) | in max3421_reset_hcd()
1307 BIT(MAX3421_MODE_DPPULLDN_BIT)); in max3421_reset_hcd()
1308 spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode); in max3421_reset_hcd()
1310 /* reset frame-number: */ in max3421_reset_hcd()
1311 max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER; in max3421_reset_hcd()
1312 spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT)); in max3421_reset_hcd()
1314 /* sample the state of the D+ and D- lines */ in max3421_reset_hcd()
1315 spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT)); in max3421_reset_hcd()
1318 /* enable frame, connection-detected, and bus-event interrupts: */ in max3421_reset_hcd()
1319 max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) | in max3421_reset_hcd()
1320 BIT(MAX3421_HI_CONDET_BIT) | in max3421_reset_hcd()
1321 BIT(MAX3421_HI_BUSEVENT_BIT)); in max3421_reset_hcd()
1322 spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien); in max3421_reset_hcd()
1325 spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT)); in max3421_reset_hcd()
1337 status = max3421_hcd->urb_done; in max3421_urb_done()
1338 max3421_hcd->urb_done = 0; in max3421_urb_done()
1341 urb = max3421_hcd->curr_urb; in max3421_urb_done()
1343 /* save the old end-points toggles: */ in max3421_urb_done()
1347 int epnum = usb_endpoint_num(&urb->ep->desc); in max3421_urb_done()
1350 usb_settoggle(urb->dev, epnum, 0, rcvtog); in max3421_urb_done()
1351 usb_settoggle(urb->dev, epnum, 1, sndtog); in max3421_urb_done()
1353 max3421_hcd->curr_urb = NULL; in max3421_urb_done()
1354 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_urb_done()
1356 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_urb_done()
1368 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_spi_thread()
1372 /* set full-duplex SPI mode, low-active interrupt pin: */ in max3421_spi_thread()
1374 (BIT(MAX3421_PINCTL_FDUPSPI_BIT) | /* full-duplex */ in max3421_spi_thread()
1375 BIT(MAX3421_PINCTL_INTLEVEL_BIT))); /* low-active irq */ in max3421_spi_thread()
1378 max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION); in max3421_spi_thread()
1379 if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13) in max3421_spi_thread()
1381 dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev); in max3421_spi_thread()
1384 dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n", in max3421_spi_thread()
1385 max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word, in max3421_spi_thread()
1386 spi->irq); in max3421_spi_thread()
1395 spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien); in max3421_spi_thread()
1398 if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo)) in max3421_spi_thread()
1399 enable_irq(spi->irq); in max3421_spi_thread()
1406 if (max3421_hcd->urb_done) in max3421_spi_thread()
1410 else if (!max3421_hcd->curr_urb) in max3421_spi_thread()
1413 if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo)) in max3421_spi_thread()
1416 if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) { in max3421_spi_thread()
1419 BIT(MAX3421_HCTL_BUSRST_BIT)); in max3421_spi_thread()
1422 if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo)) in max3421_spi_thread()
1424 if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) { in max3421_spi_thread()
1426 * IOPINS1/IOPINS2 do not auto-increment, so we can't in max3421_spi_thread()
1429 for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) { in max3421_spi_thread()
1433 (max3421_hcd->iopins[i] & 0x0f)); in max3421_spi_thread()
1435 max3421_hcd->iopins[i] = val; in max3421_spi_thread()
1441 dev_info(&spi->dev, "SPI thread exiting"); in max3421_spi_thread()
1450 max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE | in max3421_reset_port()
1452 max3421_hcd->port_status |= USB_PORT_STAT_RESET; in max3421_reset_port()
1453 set_bit(RESET_PORT, &max3421_hcd->todo); in max3421_reset_port()
1454 wake_up_process(max3421_hcd->spi_thread); in max3421_reset_port()
1463 hcd->self.sg_tablesize = 0; in max3421_reset()
1464 hcd->speed = HCD_USB2; in max3421_reset()
1465 hcd->self.root_hub->speed = USB_SPEED_FULL; in max3421_reset()
1466 set_bit(RESET_HCD, &max3421_hcd->todo); in max3421_reset()
1467 wake_up_process(max3421_hcd->spi_thread); in max3421_reset()
1476 spin_lock_init(&max3421_hcd->lock); in max3421_start()
1477 max3421_hcd->rh_state = MAX3421_RH_RUNNING; in max3421_start()
1479 INIT_LIST_HEAD(&max3421_hcd->ep_list); in max3421_start()
1481 hcd->power_budget = POWER_BUDGET; in max3421_start()
1482 hcd->state = HC_STATE_RUNNING; in max3421_start()
1483 hcd->uses_new_polling = 1; in max3421_start()
1495 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_urb_enqueue()
1501 switch (usb_pipetype(urb->pipe)) { in max3421_urb_enqueue()
1504 if (urb->interval < 0) { in max3421_urb_enqueue()
1505 dev_err(&spi->dev, in max3421_urb_enqueue()
1506 "%s: interval=%d for intr-/iso-pipe; expected > 0\n", in max3421_urb_enqueue()
1507 __func__, urb->interval); in max3421_urb_enqueue()
1508 return -EINVAL; in max3421_urb_enqueue()
1515 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_urb_enqueue()
1517 max3421_ep = urb->ep->hcpriv; in max3421_urb_enqueue()
1522 retval = -ENOMEM; in max3421_urb_enqueue()
1525 max3421_ep->ep = urb->ep; in max3421_urb_enqueue()
1526 max3421_ep->last_active = max3421_hcd->frame_number; in max3421_urb_enqueue()
1527 urb->ep->hcpriv = max3421_ep; in max3421_urb_enqueue()
1529 list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list); in max3421_urb_enqueue()
1535 max3421_hcd->sched_pass = SCHED_PASS_PERIODIC; in max3421_urb_enqueue()
1536 wake_up_process(max3421_hcd->spi_thread); in max3421_urb_enqueue()
1540 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_urb_enqueue()
1551 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_urb_dequeue()
1554 * This will set urb->unlinked which in turn causes the entry in max3421_urb_dequeue()
1559 set_bit(CHECK_UNLINK, &max3421_hcd->todo); in max3421_urb_dequeue()
1560 wake_up_process(max3421_hcd->spi_thread); in max3421_urb_dequeue()
1562 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_urb_dequeue()
1572 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_endpoint_disable()
1574 if (ep->hcpriv) { in max3421_endpoint_disable()
1575 struct max3421_ep *max3421_ep = ep->hcpriv; in max3421_endpoint_disable()
1578 if (!list_empty(&max3421_ep->ep_list)) in max3421_endpoint_disable()
1579 list_del(&max3421_ep->ep_list); in max3421_endpoint_disable()
1581 ep->hcpriv = NULL; in max3421_endpoint_disable()
1584 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_endpoint_disable()
1591 return max3421_hcd->frame_number; in max3421_get_frame_number()
1595 * Should return a non-zero value when any port is undergoing a resume
1605 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_hub_status_data()
1610 if ((max3421_hcd->port_status & PORT_C_MASK) != 0) { in max3421_hub_status_data()
1611 *buf = (1 << 1); /* a hub over-current condition exists */ in max3421_hub_status_data()
1612 dev_dbg(hcd->self.controller, in max3421_hub_status_data()
1614 max3421_hcd->port_status); in max3421_hub_status_data()
1616 if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED) in max3421_hub_status_data()
1620 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_hub_status_data()
1629 * See Table 11-13: Hub Descriptor in USB 2.0 spec. in hub_descriptor()
1631 desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */ in hub_descriptor()
1632 desc->bDescLength = 9; in hub_descriptor()
1633 desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | in hub_descriptor()
1635 desc->bNbrPorts = 1; in hub_descriptor()
1639 * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1640 * VALUE (0 or 1). PIN_NUMBER may be in the range from 1-8. For
1641 * any other value, this function acts as a no-op.
1649 --pin_number; in max3421_gpout_set_value()
1657 max3421_hcd->iopins[idx] |= mask; in max3421_gpout_set_value()
1659 max3421_hcd->iopins[idx] &= ~mask; in max3421_gpout_set_value()
1660 set_bit(IOPIN_UPDATE, &max3421_hcd->todo); in max3421_gpout_set_value()
1661 wake_up_process(max3421_hcd->spi_thread); in max3421_gpout_set_value()
1668 struct spi_device *spi = to_spi_device(hcd->self.controller); in max3421_hub_control()
1674 pdata = spi->dev.platform_data; in max3421_hub_control()
1676 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_hub_control()
1686 dev_dbg(hcd->self.controller, "power-off\n"); in max3421_hub_control()
1687 max3421_gpout_set_value(hcd, pdata->vbus_gpout, in max3421_hub_control()
1688 !pdata->vbus_active_level); in max3421_hub_control()
1691 max3421_hcd->port_status &= ~(1 << value); in max3421_hub_control()
1710 retval = -EPIPE; in max3421_hub_control()
1713 ((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status); in max3421_hub_control()
1715 cpu_to_le16(max3421_hcd->port_status >> 16); in max3421_hub_control()
1719 retval = -EPIPE; in max3421_hub_control()
1730 if (max3421_hcd->active) in max3421_hub_control()
1731 max3421_hcd->port_status |= in max3421_hub_control()
1735 dev_dbg(hcd->self.controller, "power-on\n"); in max3421_hub_control()
1736 max3421_hcd->port_status |= USB_PORT_STAT_POWER; in max3421_hub_control()
1737 max3421_gpout_set_value(hcd, pdata->vbus_gpout, in max3421_hub_control()
1738 pdata->vbus_active_level); in max3421_hub_control()
1744 if ((max3421_hcd->port_status & USB_PORT_STAT_POWER) in max3421_hub_control()
1746 max3421_hcd->port_status |= (1 << value); in max3421_hub_control()
1751 dev_dbg(hcd->self.controller, in max3421_hub_control()
1755 retval = -EPIPE; in max3421_hub_control()
1758 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_hub_control()
1765 return -1; in max3421_bus_suspend()
1771 return -1; in max3421_bus_resume()
1799 return -EINVAL; in max3421_of_vbus_en_pin()
1801 retval = of_property_read_u32_array(dev->of_node, "maxim,vbus-en-pin", value, 2); in max3421_of_vbus_en_pin()
1803 dev_err(dev, "device tree node property 'maxim,vbus-en-pin' is missing\n"); in max3421_of_vbus_en_pin()
1806 dev_info(dev, "property 'maxim,vbus-en-pin' value is <%d %d>\n", value[0], value[1]); in max3421_of_vbus_en_pin()
1808 pdata->vbus_gpout = value[0]; in max3421_of_vbus_en_pin()
1809 pdata->vbus_active_level = value[1]; in max3421_of_vbus_en_pin()
1817 struct device *dev = &spi->dev; in max3421_probe()
1824 dev_err(&spi->dev, "Unable to setup SPI bus"); in max3421_probe()
1825 return -EFAULT; in max3421_probe()
1828 if (!spi->irq) { in max3421_probe()
1830 return -EFAULT; in max3421_probe()
1833 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { in max3421_probe()
1834 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL); in max3421_probe()
1836 retval = -ENOMEM; in max3421_probe()
1843 spi->dev.platform_data = pdata; in max3421_probe()
1846 pdata = spi->dev.platform_data; in max3421_probe()
1848 dev_err(&spi->dev, "driver configuration data is not provided\n"); in max3421_probe()
1849 retval = -EFAULT; in max3421_probe()
1852 if (pdata->vbus_active_level > 1) { in max3421_probe()
1853 …dev_err(&spi->dev, "vbus active level value %d is out of range (0/1)\n", pdata->vbus_active_level); in max3421_probe()
1854 retval = -EINVAL; in max3421_probe()
1857 if (pdata->vbus_gpout < 1 || pdata->vbus_gpout > MAX3421_GPOUT_COUNT) { in max3421_probe()
1858 dev_err(&spi->dev, "vbus gpout value %d is out of range (1..8)\n", pdata->vbus_gpout); in max3421_probe()
1859 retval = -EINVAL; in max3421_probe()
1863 retval = -ENOMEM; in max3421_probe()
1864 hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev, in max3421_probe()
1865 dev_name(&spi->dev)); in max3421_probe()
1867 dev_err(&spi->dev, "failed to create HCD structure\n"); in max3421_probe()
1870 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in max3421_probe()
1872 INIT_LIST_HEAD(&max3421_hcd->ep_list); in max3421_probe()
1875 max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL); in max3421_probe()
1876 if (!max3421_hcd->tx) in max3421_probe()
1878 max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL); in max3421_probe()
1879 if (!max3421_hcd->rx) in max3421_probe()
1882 max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd, in max3421_probe()
1884 if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) { in max3421_probe()
1885 dev_err(&spi->dev, in max3421_probe()
1892 dev_err(&spi->dev, "failed to add HCD\n"); in max3421_probe()
1896 retval = request_irq(spi->irq, max3421_irq_handler, in max3421_probe()
1899 dev_err(&spi->dev, "failed to request irq %d\n", spi->irq); in max3421_probe()
1905 if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) { in max3421_probe()
1906 devm_kfree(&spi->dev, pdata); in max3421_probe()
1907 spi->dev.platform_data = NULL; in max3421_probe()
1911 kfree(max3421_hcd->tx); in max3421_probe()
1912 kfree(max3421_hcd->rx); in max3421_probe()
1913 if (max3421_hcd->spi_thread) in max3421_probe()
1914 kthread_stop(max3421_hcd->spi_thread); in max3421_probe()
1932 spin_lock_irqsave(&max3421_hcd->lock, flags); in max3421_remove()
1934 kthread_stop(max3421_hcd->spi_thread); in max3421_remove()
1936 spin_unlock_irqrestore(&max3421_hcd->lock, flags); in max3421_remove()
1938 free_irq(spi->irq, hcd); in max3421_remove()
1953 .name = "max3421-hcd",