Lines Matching +full:loop +full:- +full:powered
1 // SPDX-License-Identifier: GPL-2.0+
6 * (C) Copyright 2019-2020 Linaro Ltd
189 #define DRIVER_DESC "MAX3420 USB Device-Mode Driver"
190 static const char driver_name[] = "max3420-udc";
201 struct spi_device *spi = udc->spi; in spi_ack_ctrl()
220 struct spi_device *spi = udc->spi; in spi_rd8_ack()
247 struct spi_device *spi = udc->spi; in spi_wr8_ack()
273 struct spi_device *spi = udc->spi; in spi_rd_buf()
295 struct spi_device *spi = udc->spi; in spi_wr_buf()
316 struct max3420_udc *udc = ep->udc; in spi_max3420_enable()
321 spin_lock_irqsave(&ep->lock, flags); in spi_max3420_enable()
322 todo = ep->todo & ENABLE_EP; in spi_max3420_enable()
323 ep->todo &= ~ENABLE_EP; in spi_max3420_enable()
324 spin_unlock_irqrestore(&ep->lock, flags); in spi_max3420_enable()
326 if (!todo || ep->id == 0) in spi_max3420_enable()
333 epdis &= ~BIT(ep->id + 4); in spi_max3420_enable()
334 epien |= BIT(ep->id + 1); in spi_max3420_enable()
336 epdis |= BIT(ep->id + 4); in spi_max3420_enable()
337 epien &= ~BIT(ep->id + 1); in spi_max3420_enable()
348 struct max3420_udc *udc = ep->udc; in spi_max3420_stall()
353 spin_lock_irqsave(&ep->lock, flags); in spi_max3420_stall()
354 todo = ep->todo & STALL_EP; in spi_max3420_stall()
355 ep->todo &= ~STALL_EP; in spi_max3420_stall()
356 spin_unlock_irqrestore(&ep->lock, flags); in spi_max3420_stall()
358 if (!todo || ep->id == 0) in spi_max3420_stall()
363 ep->halted = 1; in spi_max3420_stall()
364 epstalls |= BIT(ep->id + 1); in spi_max3420_stall()
368 ep->halted = 0; in spi_max3420_stall()
369 epstalls &= ~BIT(ep->id + 1); in spi_max3420_stall()
371 clrtogs |= BIT(ep->id + 1); in spi_max3420_stall()
385 spin_lock_irqsave(&udc->lock, flags); in spi_max3420_rwkup()
386 wake_remote = udc->todo & REMOTE_WAKEUP; in spi_max3420_rwkup()
387 udc->todo &= ~REMOTE_WAKEUP; in spi_max3420_rwkup()
388 spin_unlock_irqrestore(&udc->lock, flags); in spi_max3420_rwkup()
390 if (!wake_remote || !udc->suspended) in spi_max3420_rwkup()
393 /* Set Remote-WkUp Signal*/ in spi_max3420_rwkup()
400 /* Clear Remote-WkUp Signal*/ in spi_max3420_rwkup()
405 udc->suspended = false; in spi_max3420_rwkup()
418 max3420_nuke(&udc->ep[i], -ECONNRESET); in __max3420_stop()
425 if (udc->is_selfpowered) in __max3420_stop()
436 /* Need this delay if bus-powered, in __max3420_start()
437 * but even for self-powered it helps stability in __max3420_start()
457 /* Enable PULL-UP only when Vbus detected */ in __max3420_start()
463 if (udc->is_selfpowered) in __max3420_start()
480 spin_lock_irqsave(&udc->lock, flags); in max3420_start()
481 todo = udc->todo & UDC_START; in max3420_start()
482 udc->todo &= ~UDC_START; in max3420_start()
483 spin_unlock_irqrestore(&udc->lock, flags); in max3420_start()
488 if (udc->vbus_active && udc->softconnect) in max3420_start()
501 spin_lock_irqsave(&udc->lock, flags); in max3420_vbus_handler()
503 udc->vbus_active = !udc->vbus_active; in max3420_vbus_handler()
504 udc->todo |= UDC_START; in max3420_vbus_handler()
505 usb_udc_vbus_handler(&udc->gadget, udc->vbus_active); in max3420_vbus_handler()
506 usb_gadget_set_state(&udc->gadget, udc->vbus_active in max3420_vbus_handler()
508 spin_unlock_irqrestore(&udc->lock, flags); in max3420_vbus_handler()
510 if (udc->thread_task) in max3420_vbus_handler()
511 wake_up_process(udc->thread_task); in max3420_vbus_handler()
519 struct spi_device *spi = udc->spi; in max3420_irq_handler()
522 spin_lock_irqsave(&udc->lock, flags); in max3420_irq_handler()
523 if ((udc->todo & ENABLE_IRQ) == 0) { in max3420_irq_handler()
524 disable_irq_nosync(spi->irq); in max3420_irq_handler()
525 udc->todo |= ENABLE_IRQ; in max3420_irq_handler()
527 spin_unlock_irqrestore(&udc->lock, flags); in max3420_irq_handler()
529 if (udc->thread_task) in max3420_irq_handler()
530 wake_up_process(udc->thread_task); in max3420_irq_handler()
540 switch (udc->setup.bRequestType & USB_RECIP_MASK) { in max3420_getstatus()
543 status = udc->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED; in max3420_getstatus()
544 status |= (udc->remote_wkp << USB_DEVICE_REMOTE_WAKEUP); in max3420_getstatus()
547 if (udc->driver->setup(&udc->gadget, &udc->setup) < 0) in max3420_getstatus()
551 ep = &udc->ep[udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK]; in max3420_getstatus()
552 if (udc->setup.wIndex & USB_DIR_IN) { in max3420_getstatus()
553 if (!ep->ep_usb.caps.dir_in) in max3420_getstatus()
556 if (!ep->ep_usb.caps.dir_out) in max3420_getstatus()
559 if (ep->halted) in max3420_getstatus()
571 dev_err(udc->dev, "Can't respond to getstatus request\n"); in max3420_getstatus()
578 int set = udc->setup.bRequest == USB_REQ_SET_FEATURE; in max3420_set_clear_feature()
582 switch (udc->setup.bRequestType) { in max3420_set_clear_feature()
584 if (udc->setup.wValue != USB_DEVICE_REMOTE_WAKEUP) in max3420_set_clear_feature()
587 if (udc->setup.bRequest == USB_REQ_SET_FEATURE) in max3420_set_clear_feature()
588 udc->remote_wkp = 1; in max3420_set_clear_feature()
590 udc->remote_wkp = 0; in max3420_set_clear_feature()
595 if (udc->setup.wValue != USB_ENDPOINT_HALT) in max3420_set_clear_feature()
598 id = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK; in max3420_set_clear_feature()
599 ep = &udc->ep[id]; in max3420_set_clear_feature()
601 spin_lock_irqsave(&ep->lock, flags); in max3420_set_clear_feature()
602 ep->todo &= ~STALL_EP; in max3420_set_clear_feature()
604 ep->todo |= STALL; in max3420_set_clear_feature()
606 ep->todo |= UNSTALL; in max3420_set_clear_feature()
607 spin_unlock_irqrestore(&ep->lock, flags); in max3420_set_clear_feature()
615 dev_err(udc->dev, "Can't respond to SET/CLEAR FEATURE\n"); in max3420_set_clear_feature()
625 udc->setup = setup; in max3420_handle_setup()
626 udc->setup.wValue = cpu_to_le16(setup.wValue); in max3420_handle_setup()
627 udc->setup.wIndex = cpu_to_le16(setup.wIndex); in max3420_handle_setup()
628 udc->setup.wLength = cpu_to_le16(setup.wLength); in max3420_handle_setup()
630 switch (udc->setup.bRequest) { in max3420_handle_setup()
633 if ((udc->setup.bRequestType & in max3420_handle_setup()
641 if (udc->setup.bRequestType != (USB_DIR_OUT | in max3420_handle_setup()
646 dev_dbg(udc->dev, "Assigned Address=%d\n", udc->setup.wValue); in max3420_handle_setup()
651 if ((udc->setup.bRequestType & USB_TYPE_MASK) in max3420_handle_setup()
659 if (udc->driver->setup(&udc->gadget, &setup) < 0) { in max3420_handle_setup()
668 struct max3420_ep *ep = req->ep; in max3420_req_done()
669 struct max3420_udc *udc = ep->udc; in max3420_req_done()
671 if (req->usb_req.status == -EINPROGRESS) in max3420_req_done()
672 req->usb_req.status = status; in max3420_req_done()
674 status = req->usb_req.status; in max3420_req_done()
676 if (status && status != -ESHUTDOWN) in max3420_req_done()
677 dev_err(udc->dev, "%s done %p, status %d\n", in max3420_req_done()
678 ep->ep_usb.name, req, status); in max3420_req_done()
680 if (req->usb_req.complete) in max3420_req_done()
681 req->usb_req.complete(&ep->ep_usb, &req->usb_req); in max3420_req_done()
686 struct max3420_ep *ep = &udc->ep[ep_id]; in max3420_do_data()
691 if (list_empty(&ep->queue)) in max3420_do_data()
694 req = list_first_entry(&ep->queue, struct max3420_req, queue); in max3420_do_data()
695 buf = req->usb_req.buf + req->usb_req.actual; in max3420_do_data()
697 psz = ep->ep_usb.maxpacket; in max3420_do_data()
698 length = req->usb_req.length - req->usb_req.actual; in max3420_do_data()
718 if (length < ep->ep_usb.maxpacket) in max3420_do_data()
722 req->usb_req.actual += length; in max3420_do_data()
724 if (req->usb_req.actual == req->usb_req.length) in max3420_do_data()
731 spin_lock_irqsave(&ep->lock, flags); in max3420_do_data()
732 list_del_init(&req->queue); in max3420_do_data()
733 spin_unlock_irqrestore(&ep->lock, flags); in max3420_do_data()
766 dev_dbg(udc->dev, "Cable plugged in\n"); in max3420_handle_irqs()
772 dev_dbg(udc->dev, "Cable pulled out\n"); in max3420_handle_irqs()
778 dev_dbg(udc->dev, "USB Reset - Start\n"); in max3420_handle_irqs()
784 dev_dbg(udc->dev, "USB Reset - END\n"); in max3420_handle_irqs()
793 dev_dbg(udc->dev, "USB Suspend - Enter\n"); in max3420_handle_irqs()
794 udc->suspended = true; in max3420_handle_irqs()
800 dev_dbg(udc->dev, "USB Suspend - Exit\n"); in max3420_handle_irqs()
801 udc->suspended = false; in max3420_handle_irqs()
807 dev_dbg(udc->dev, "Asked Host to wakeup\n"); in max3420_handle_irqs()
813 dev_dbg(udc->dev, "Osc stabilized, start work\n"); in max3420_handle_irqs()
842 struct spi_device *spi = udc->spi; in max3420_thread()
852 spin_lock_irqsave(&udc->lock, flags); in max3420_thread()
853 if (udc->todo & ENABLE_IRQ) { in max3420_thread()
854 enable_irq(spi->irq); in max3420_thread()
855 udc->todo &= ~ENABLE_IRQ; in max3420_thread()
857 spin_unlock_irqrestore(&udc->lock, flags); in max3420_thread()
863 mutex_lock(&udc->spi_bus_mutex); in max3420_thread()
865 /* If bus-vbus_active and disconnected */ in max3420_thread()
866 if (!udc->vbus_active || !udc->softconnect) in max3420_thread()
867 goto loop; in max3420_thread()
871 goto loop; in max3420_thread()
876 goto loop; in max3420_thread()
881 goto loop; in max3420_thread()
887 struct max3420_ep *ep = &udc->ep[i]; in max3420_thread()
894 loop: in max3420_thread()
895 mutex_unlock(&udc->spi_bus_mutex); in max3420_thread()
899 dev_info(udc->dev, "SPI thread exiting\n"); in max3420_thread()
906 struct max3420_udc *udc = ep->udc; in max3420_ep_set_halt()
909 spin_lock_irqsave(&ep->lock, flags); in max3420_ep_set_halt()
911 ep->todo &= ~STALL_EP; in max3420_ep_set_halt()
913 ep->todo |= STALL; in max3420_ep_set_halt()
915 ep->todo |= UNSTALL; in max3420_ep_set_halt()
917 spin_unlock_irqrestore(&ep->lock, flags); in max3420_ep_set_halt()
919 wake_up_process(udc->thread_task); in max3420_ep_set_halt()
921 dev_dbg(udc->dev, "%sStall %s\n", stall ? "" : "Un", ep->name); in max3420_ep_set_halt()
931 spin_lock_irqsave(&ep->lock, flags); in __max3420_ep_enable()
932 ep->ep_usb.desc = desc; in __max3420_ep_enable()
933 ep->ep_usb.maxpacket = maxp; in __max3420_ep_enable()
935 ep->todo &= ~ENABLE_EP; in __max3420_ep_enable()
936 ep->todo |= ENABLE; in __max3420_ep_enable()
937 spin_unlock_irqrestore(&ep->lock, flags); in __max3420_ep_enable()
946 struct max3420_udc *udc = ep->udc; in max3420_ep_enable()
950 wake_up_process(udc->thread_task); in max3420_ep_enable()
960 spin_lock_irqsave(&ep->lock, flags); in max3420_nuke()
962 list_for_each_entry_safe(req, r, &ep->queue, queue) { in max3420_nuke()
963 list_del_init(&req->queue); in max3420_nuke()
965 spin_unlock_irqrestore(&ep->lock, flags); in max3420_nuke()
967 spin_lock_irqsave(&ep->lock, flags); in max3420_nuke()
970 spin_unlock_irqrestore(&ep->lock, flags); in max3420_nuke()
975 struct max3420_udc *udc = ep->udc; in __max3420_ep_disable()
978 spin_lock_irqsave(&ep->lock, flags); in __max3420_ep_disable()
980 ep->ep_usb.desc = NULL; in __max3420_ep_disable()
982 ep->todo &= ~ENABLE_EP; in __max3420_ep_disable()
983 ep->todo |= DISABLE; in __max3420_ep_disable()
985 spin_unlock_irqrestore(&ep->lock, flags); in __max3420_ep_disable()
987 dev_dbg(udc->dev, "Disabled %s\n", ep->name); in __max3420_ep_disable()
993 struct max3420_udc *udc = ep->udc; in max3420_ep_disable()
995 max3420_nuke(ep, -ESHUTDOWN); in max3420_ep_disable()
999 wake_up_process(udc->thread_task); in max3420_ep_disable()
1014 req->ep = ep; in max3420_alloc_request()
1016 return &req->usb_req; in max3420_alloc_request()
1029 struct max3420_udc *udc = ep->udc; in max3420_ep_queue()
1032 _req->status = -EINPROGRESS; in max3420_ep_queue()
1033 _req->actual = 0; in max3420_ep_queue()
1035 spin_lock_irqsave(&ep->lock, flags); in max3420_ep_queue()
1036 list_add_tail(&req->queue, &ep->queue); in max3420_ep_queue()
1037 spin_unlock_irqrestore(&ep->lock, flags); in max3420_ep_queue()
1039 wake_up_process(udc->thread_task); in max3420_ep_queue()
1051 spin_lock_irqsave(&ep->lock, flags); in max3420_ep_dequeue()
1054 list_for_each_entry(iter, &ep->queue, queue) { in max3420_ep_dequeue()
1057 list_del_init(&req->queue); in max3420_ep_dequeue()
1062 spin_unlock_irqrestore(&ep->lock, flags); in max3420_ep_dequeue()
1065 max3420_req_done(req, -ECONNRESET); in max3420_ep_dequeue()
1084 int ret = -EINVAL; in max3420_wakeup()
1086 spin_lock_irqsave(&udc->lock, flags); in max3420_wakeup()
1089 if (udc->remote_wkp) { in max3420_wakeup()
1090 udc->todo |= REMOTE_WAKEUP; in max3420_wakeup()
1094 spin_unlock_irqrestore(&udc->lock, flags); in max3420_wakeup()
1096 if (udc->thread_task) in max3420_wakeup()
1097 wake_up_process(udc->thread_task); in max3420_wakeup()
1107 spin_lock_irqsave(&udc->lock, flags); in max3420_udc_start()
1109 udc->driver = driver; in max3420_udc_start()
1110 udc->gadget.speed = USB_SPEED_FULL; in max3420_udc_start()
1112 udc->gadget.is_selfpowered = udc->is_selfpowered; in max3420_udc_start()
1113 udc->remote_wkp = 0; in max3420_udc_start()
1114 udc->softconnect = true; in max3420_udc_start()
1115 udc->todo |= UDC_START; in max3420_udc_start()
1116 spin_unlock_irqrestore(&udc->lock, flags); in max3420_udc_start()
1118 if (udc->thread_task) in max3420_udc_start()
1119 wake_up_process(udc->thread_task); in max3420_udc_start()
1129 spin_lock_irqsave(&udc->lock, flags); in max3420_udc_stop()
1130 udc->is_selfpowered = udc->gadget.is_selfpowered; in max3420_udc_stop()
1131 udc->gadget.speed = USB_SPEED_UNKNOWN; in max3420_udc_stop()
1132 udc->driver = NULL; in max3420_udc_stop()
1133 udc->softconnect = false; in max3420_udc_stop()
1134 udc->todo |= UDC_START; in max3420_udc_stop()
1135 spin_unlock_irqrestore(&udc->lock, flags); in max3420_udc_stop()
1137 if (udc->thread_task) in max3420_udc_stop()
1138 wake_up_process(udc->thread_task); in max3420_udc_stop()
1153 INIT_LIST_HEAD(&udc->gadget.ep_list); in max3420_eps_init()
1156 struct max3420_ep *ep = &udc->ep[idx]; in max3420_eps_init()
1158 spin_lock_init(&ep->lock); in max3420_eps_init()
1159 INIT_LIST_HEAD(&ep->queue); in max3420_eps_init()
1161 ep->udc = udc; in max3420_eps_init()
1162 ep->id = idx; in max3420_eps_init()
1163 ep->halted = 0; in max3420_eps_init()
1164 ep->maxpacket = 0; in max3420_eps_init()
1165 ep->ep_usb.name = ep->name; in max3420_eps_init()
1166 ep->ep_usb.ops = &max3420_ep_ops; in max3420_eps_init()
1167 usb_ep_set_maxpacket_limit(&ep->ep_usb, MAX3420_EP_MAX_PACKET); in max3420_eps_init()
1170 ep->ep_usb.desc = &ep0_desc; in max3420_eps_init()
1171 ep->ep_usb.maxpacket = usb_endpoint_maxp(&ep0_desc); in max3420_eps_init()
1172 ep->ep_usb.caps.type_control = true; in max3420_eps_init()
1173 ep->ep_usb.caps.dir_in = true; in max3420_eps_init()
1174 ep->ep_usb.caps.dir_out = true; in max3420_eps_init()
1175 snprintf(ep->name, MAX3420_EPNAME_SIZE, "ep0"); in max3420_eps_init()
1180 ep->ep_usb.caps.dir_in = false; in max3420_eps_init()
1181 ep->ep_usb.caps.dir_out = true; in max3420_eps_init()
1182 snprintf(ep->name, MAX3420_EPNAME_SIZE, "ep1-bulk-out"); in max3420_eps_init()
1184 ep->ep_usb.caps.dir_in = true; in max3420_eps_init()
1185 ep->ep_usb.caps.dir_out = false; in max3420_eps_init()
1186 snprintf(ep->name, MAX3420_EPNAME_SIZE, in max3420_eps_init()
1187 "ep%d-bulk-in", idx); in max3420_eps_init()
1189 ep->ep_usb.caps.type_iso = false; in max3420_eps_init()
1190 ep->ep_usb.caps.type_int = false; in max3420_eps_init()
1191 ep->ep_usb.caps.type_bulk = true; in max3420_eps_init()
1193 list_add_tail(&ep->ep_usb.ep_list, in max3420_eps_init()
1194 &udc->gadget.ep_list); in max3420_eps_init()
1204 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) { in max3420_probe()
1205 dev_err(&spi->dev, "UDC needs full duplex to work\n"); in max3420_probe()
1206 return -EINVAL; in max3420_probe()
1209 spi->mode = SPI_MODE_3; in max3420_probe()
1210 spi->bits_per_word = 8; in max3420_probe()
1214 dev_err(&spi->dev, "Unable to setup SPI bus\n"); in max3420_probe()
1215 return -EFAULT; in max3420_probe()
1218 udc = devm_kzalloc(&spi->dev, sizeof(*udc), GFP_KERNEL); in max3420_probe()
1220 return -ENOMEM; in max3420_probe()
1222 udc->spi = spi; in max3420_probe()
1224 udc->remote_wkp = 0; in max3420_probe()
1227 udc->gadget.ops = &max3420_udc_ops; in max3420_probe()
1228 udc->gadget.max_speed = USB_SPEED_FULL; in max3420_probe()
1229 udc->gadget.speed = USB_SPEED_UNKNOWN; in max3420_probe()
1230 udc->gadget.ep0 = &udc->ep[0].ep_usb; in max3420_probe()
1231 udc->gadget.name = driver_name; in max3420_probe()
1233 spin_lock_init(&udc->lock); in max3420_probe()
1234 mutex_init(&udc->spi_bus_mutex); in max3420_probe()
1236 udc->ep0req.ep = &udc->ep[0]; in max3420_probe()
1237 udc->ep0req.usb_req.buf = udc->ep0buf; in max3420_probe()
1238 INIT_LIST_HEAD(&udc->ep0req.queue); in max3420_probe()
1247 err = usb_add_gadget_udc(&spi->dev, &udc->gadget); in max3420_probe()
1251 udc->dev = &udc->gadget.dev; in max3420_probe()
1255 irq = of_irq_get_byname(spi->dev.of_node, "udc"); in max3420_probe()
1256 err = devm_request_irq(&spi->dev, irq, max3420_irq_handler, 0, in max3420_probe()
1261 udc->thread_task = kthread_create(max3420_thread, udc, in max3420_probe()
1262 "max3420-thread"); in max3420_probe()
1263 if (IS_ERR(udc->thread_task)) { in max3420_probe()
1264 err = PTR_ERR(udc->thread_task); in max3420_probe()
1268 irq = of_irq_get_byname(spi->dev.of_node, "vbus"); in max3420_probe()
1269 if (irq <= 0) { /* no vbus irq implies self-powered design */ in max3420_probe()
1270 udc->is_selfpowered = 1; in max3420_probe()
1271 udc->vbus_active = true; in max3420_probe()
1272 udc->todo |= UDC_START; in max3420_probe()
1273 usb_udc_vbus_handler(&udc->gadget, udc->vbus_active); in max3420_probe()
1274 usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED); in max3420_probe()
1277 udc->is_selfpowered = 0; in max3420_probe()
1281 udc->vbus_active = true; in max3420_probe()
1283 err = devm_request_irq(&spi->dev, irq, in max3420_probe()
1292 usb_del_gadget_udc(&udc->gadget); in max3420_probe()
1301 usb_del_gadget_udc(&udc->gadget); in max3420_remove()
1303 spin_lock_irqsave(&udc->lock, flags); in max3420_remove()
1305 kthread_stop(udc->thread_task); in max3420_remove()
1307 spin_unlock_irqrestore(&udc->lock, flags); in max3420_remove()
1311 { .compatible = "maxim,max3420-udc"},
1312 { .compatible = "maxim,max3421-udc"},
1319 .name = "max3420-udc",