Lines Matching +full:loop +full:- +full:powered

1 // SPDX-License-Identifier: GPL-2.0+
6 * (C) Copyright 2019-2020 Linaro Ltd
191 #define DRIVER_DESC "MAX3420 USB Device-Mode Driver"
192 static const char driver_name[] = "max3420-udc";
203 struct spi_device *spi = udc->spi; in spi_ack_ctrl()
222 struct spi_device *spi = udc->spi; in spi_rd8_ack()
249 struct spi_device *spi = udc->spi; in spi_wr8_ack()
275 struct spi_device *spi = udc->spi; in spi_rd_buf()
297 struct spi_device *spi = udc->spi; in spi_wr_buf()
318 struct max3420_udc *udc = ep->udc; in spi_max3420_enable()
323 spin_lock_irqsave(&ep->lock, flags); in spi_max3420_enable()
324 todo = ep->todo & ENABLE_EP; in spi_max3420_enable()
325 ep->todo &= ~ENABLE_EP; in spi_max3420_enable()
326 spin_unlock_irqrestore(&ep->lock, flags); in spi_max3420_enable()
328 if (!todo || ep->id == 0) in spi_max3420_enable()
335 epdis &= ~BIT(ep->id + 4); in spi_max3420_enable()
336 epien |= BIT(ep->id + 1); in spi_max3420_enable()
338 epdis |= BIT(ep->id + 4); in spi_max3420_enable()
339 epien &= ~BIT(ep->id + 1); in spi_max3420_enable()
350 struct max3420_udc *udc = ep->udc; in spi_max3420_stall()
355 spin_lock_irqsave(&ep->lock, flags); in spi_max3420_stall()
356 todo = ep->todo & STALL_EP; in spi_max3420_stall()
357 ep->todo &= ~STALL_EP; in spi_max3420_stall()
358 spin_unlock_irqrestore(&ep->lock, flags); in spi_max3420_stall()
360 if (!todo || ep->id == 0) in spi_max3420_stall()
365 ep->halted = 1; in spi_max3420_stall()
366 epstalls |= BIT(ep->id + 1); in spi_max3420_stall()
370 ep->halted = 0; in spi_max3420_stall()
371 epstalls &= ~BIT(ep->id + 1); in spi_max3420_stall()
373 clrtogs |= BIT(ep->id + 1); in spi_max3420_stall()
387 spin_lock_irqsave(&udc->lock, flags); in spi_max3420_rwkup()
388 wake_remote = udc->todo & REMOTE_WAKEUP; in spi_max3420_rwkup()
389 udc->todo &= ~REMOTE_WAKEUP; in spi_max3420_rwkup()
390 spin_unlock_irqrestore(&udc->lock, flags); in spi_max3420_rwkup()
392 if (!wake_remote || !udc->suspended) in spi_max3420_rwkup()
395 /* Set Remote-WkUp Signal*/ in spi_max3420_rwkup()
402 /* Clear Remote-WkUp Signal*/ in spi_max3420_rwkup()
407 udc->suspended = false; in spi_max3420_rwkup()
420 max3420_nuke(&udc->ep[i], -ECONNRESET); in __max3420_stop()
427 if (udc->is_selfpowered) in __max3420_stop()
438 /* Need this delay if bus-powered, in __max3420_start()
439 * but even for self-powered it helps stability in __max3420_start()
459 /* Enable PULL-UP only when Vbus detected */ in __max3420_start()
465 if (udc->is_selfpowered) in __max3420_start()
482 spin_lock_irqsave(&udc->lock, flags); in max3420_start()
483 todo = udc->todo & UDC_START; in max3420_start()
484 udc->todo &= ~UDC_START; in max3420_start()
485 spin_unlock_irqrestore(&udc->lock, flags); in max3420_start()
490 if (udc->vbus_active && udc->softconnect) in max3420_start()
503 spin_lock_irqsave(&udc->lock, flags); in max3420_vbus_handler()
505 udc->vbus_active = !udc->vbus_active; in max3420_vbus_handler()
506 udc->todo |= UDC_START; in max3420_vbus_handler()
507 usb_udc_vbus_handler(&udc->gadget, udc->vbus_active); in max3420_vbus_handler()
508 usb_gadget_set_state(&udc->gadget, udc->vbus_active in max3420_vbus_handler()
510 spin_unlock_irqrestore(&udc->lock, flags); in max3420_vbus_handler()
512 if (udc->thread_task) in max3420_vbus_handler()
513 wake_up_process(udc->thread_task); in max3420_vbus_handler()
521 struct spi_device *spi = udc->spi; in max3420_irq_handler()
524 spin_lock_irqsave(&udc->lock, flags); in max3420_irq_handler()
525 if ((udc->todo & ENABLE_IRQ) == 0) { in max3420_irq_handler()
526 disable_irq_nosync(spi->irq); in max3420_irq_handler()
527 udc->todo |= ENABLE_IRQ; in max3420_irq_handler()
529 spin_unlock_irqrestore(&udc->lock, flags); in max3420_irq_handler()
531 if (udc->thread_task) in max3420_irq_handler()
532 wake_up_process(udc->thread_task); in max3420_irq_handler()
542 switch (udc->setup.bRequestType & USB_RECIP_MASK) { in max3420_getstatus()
545 status = udc->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED; in max3420_getstatus()
546 status |= (udc->remote_wkp << USB_DEVICE_REMOTE_WAKEUP); in max3420_getstatus()
549 if (udc->driver->setup(&udc->gadget, &udc->setup) < 0) in max3420_getstatus()
553 ep = &udc->ep[udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK]; in max3420_getstatus()
554 if (udc->setup.wIndex & USB_DIR_IN) { in max3420_getstatus()
555 if (!ep->ep_usb.caps.dir_in) in max3420_getstatus()
558 if (!ep->ep_usb.caps.dir_out) in max3420_getstatus()
561 if (ep->halted) in max3420_getstatus()
573 dev_err(udc->dev, "Can't respond to getstatus request\n"); in max3420_getstatus()
580 int set = udc->setup.bRequest == USB_REQ_SET_FEATURE; in max3420_set_clear_feature()
584 switch (udc->setup.bRequestType) { in max3420_set_clear_feature()
586 if (udc->setup.wValue != USB_DEVICE_REMOTE_WAKEUP) in max3420_set_clear_feature()
589 if (udc->setup.bRequest == USB_REQ_SET_FEATURE) in max3420_set_clear_feature()
590 udc->remote_wkp = 1; in max3420_set_clear_feature()
592 udc->remote_wkp = 0; in max3420_set_clear_feature()
597 if (udc->setup.wValue != USB_ENDPOINT_HALT) in max3420_set_clear_feature()
600 id = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK; in max3420_set_clear_feature()
601 ep = &udc->ep[id]; in max3420_set_clear_feature()
603 spin_lock_irqsave(&ep->lock, flags); in max3420_set_clear_feature()
604 ep->todo &= ~STALL_EP; in max3420_set_clear_feature()
606 ep->todo |= STALL; in max3420_set_clear_feature()
608 ep->todo |= UNSTALL; in max3420_set_clear_feature()
609 spin_unlock_irqrestore(&ep->lock, flags); in max3420_set_clear_feature()
617 dev_err(udc->dev, "Can't respond to SET/CLEAR FEATURE\n"); in max3420_set_clear_feature()
627 udc->setup = setup; in max3420_handle_setup()
628 udc->setup.wValue = cpu_to_le16(setup.wValue); in max3420_handle_setup()
629 udc->setup.wIndex = cpu_to_le16(setup.wIndex); in max3420_handle_setup()
630 udc->setup.wLength = cpu_to_le16(setup.wLength); in max3420_handle_setup()
632 switch (udc->setup.bRequest) { in max3420_handle_setup()
635 if ((udc->setup.bRequestType & in max3420_handle_setup()
643 if (udc->setup.bRequestType != (USB_DIR_OUT | in max3420_handle_setup()
648 dev_dbg(udc->dev, "Assigned Address=%d\n", udc->setup.wValue); in max3420_handle_setup()
653 if ((udc->setup.bRequestType & USB_TYPE_MASK) in max3420_handle_setup()
661 if (udc->driver->setup(&udc->gadget, &setup) < 0) { in max3420_handle_setup()
670 struct max3420_ep *ep = req->ep; in max3420_req_done()
671 struct max3420_udc *udc = ep->udc; in max3420_req_done()
673 if (req->usb_req.status == -EINPROGRESS) in max3420_req_done()
674 req->usb_req.status = status; in max3420_req_done()
676 status = req->usb_req.status; in max3420_req_done()
678 if (status && status != -ESHUTDOWN) in max3420_req_done()
679 dev_err(udc->dev, "%s done %p, status %d\n", in max3420_req_done()
680 ep->ep_usb.name, req, status); in max3420_req_done()
682 if (req->usb_req.complete) in max3420_req_done()
683 req->usb_req.complete(&ep->ep_usb, &req->usb_req); in max3420_req_done()
688 struct max3420_ep *ep = &udc->ep[ep_id]; in max3420_do_data()
693 if (list_empty(&ep->queue)) in max3420_do_data()
696 req = list_first_entry(&ep->queue, struct max3420_req, queue); in max3420_do_data()
697 buf = req->usb_req.buf + req->usb_req.actual; in max3420_do_data()
699 psz = ep->ep_usb.maxpacket; in max3420_do_data()
700 length = req->usb_req.length - req->usb_req.actual; in max3420_do_data()
720 if (length < ep->ep_usb.maxpacket) in max3420_do_data()
724 req->usb_req.actual += length; in max3420_do_data()
726 if (req->usb_req.actual == req->usb_req.length) in max3420_do_data()
733 spin_lock_irqsave(&ep->lock, flags); in max3420_do_data()
734 list_del_init(&req->queue); in max3420_do_data()
735 spin_unlock_irqrestore(&ep->lock, flags); in max3420_do_data()
768 dev_dbg(udc->dev, "Cable plugged in\n"); in max3420_handle_irqs()
774 dev_dbg(udc->dev, "Cable pulled out\n"); in max3420_handle_irqs()
780 dev_dbg(udc->dev, "USB Reset - Start\n"); in max3420_handle_irqs()
786 dev_dbg(udc->dev, "USB Reset - END\n"); in max3420_handle_irqs()
795 dev_dbg(udc->dev, "USB Suspend - Enter\n"); in max3420_handle_irqs()
796 udc->suspended = true; in max3420_handle_irqs()
802 dev_dbg(udc->dev, "USB Suspend - Exit\n"); in max3420_handle_irqs()
803 udc->suspended = false; in max3420_handle_irqs()
809 dev_dbg(udc->dev, "Asked Host to wakeup\n"); in max3420_handle_irqs()
815 dev_dbg(udc->dev, "Osc stabilized, start work\n"); in max3420_handle_irqs()
844 struct spi_device *spi = udc->spi; in max3420_thread()
854 spin_lock_irqsave(&udc->lock, flags); in max3420_thread()
855 if (udc->todo & ENABLE_IRQ) { in max3420_thread()
856 enable_irq(spi->irq); in max3420_thread()
857 udc->todo &= ~ENABLE_IRQ; in max3420_thread()
859 spin_unlock_irqrestore(&udc->lock, flags); in max3420_thread()
865 mutex_lock(&udc->spi_bus_mutex); in max3420_thread()
867 /* If bus-vbus_active and disconnected */ in max3420_thread()
868 if (!udc->vbus_active || !udc->softconnect) in max3420_thread()
869 goto loop; in max3420_thread()
873 goto loop; in max3420_thread()
878 goto loop; in max3420_thread()
883 goto loop; in max3420_thread()
889 struct max3420_ep *ep = &udc->ep[i]; in max3420_thread()
896 loop: in max3420_thread()
897 mutex_unlock(&udc->spi_bus_mutex); in max3420_thread()
901 dev_info(udc->dev, "SPI thread exiting\n"); in max3420_thread()
908 struct max3420_udc *udc = ep->udc; in max3420_ep_set_halt()
911 spin_lock_irqsave(&ep->lock, flags); in max3420_ep_set_halt()
913 ep->todo &= ~STALL_EP; in max3420_ep_set_halt()
915 ep->todo |= STALL; in max3420_ep_set_halt()
917 ep->todo |= UNSTALL; in max3420_ep_set_halt()
919 spin_unlock_irqrestore(&ep->lock, flags); in max3420_ep_set_halt()
921 wake_up_process(udc->thread_task); in max3420_ep_set_halt()
923 dev_dbg(udc->dev, "%sStall %s\n", stall ? "" : "Un", ep->name); in max3420_ep_set_halt()
933 spin_lock_irqsave(&ep->lock, flags); in __max3420_ep_enable()
934 ep->ep_usb.desc = desc; in __max3420_ep_enable()
935 ep->ep_usb.maxpacket = maxp; in __max3420_ep_enable()
937 ep->todo &= ~ENABLE_EP; in __max3420_ep_enable()
938 ep->todo |= ENABLE; in __max3420_ep_enable()
939 spin_unlock_irqrestore(&ep->lock, flags); in __max3420_ep_enable()
948 struct max3420_udc *udc = ep->udc; in max3420_ep_enable()
952 wake_up_process(udc->thread_task); in max3420_ep_enable()
962 spin_lock_irqsave(&ep->lock, flags); in max3420_nuke()
964 list_for_each_entry_safe(req, r, &ep->queue, queue) { in max3420_nuke()
965 list_del_init(&req->queue); in max3420_nuke()
967 spin_unlock_irqrestore(&ep->lock, flags); in max3420_nuke()
969 spin_lock_irqsave(&ep->lock, flags); in max3420_nuke()
972 spin_unlock_irqrestore(&ep->lock, flags); in max3420_nuke()
977 struct max3420_udc *udc = ep->udc; in __max3420_ep_disable()
980 spin_lock_irqsave(&ep->lock, flags); in __max3420_ep_disable()
982 ep->ep_usb.desc = NULL; in __max3420_ep_disable()
984 ep->todo &= ~ENABLE_EP; in __max3420_ep_disable()
985 ep->todo |= DISABLE; in __max3420_ep_disable()
987 spin_unlock_irqrestore(&ep->lock, flags); in __max3420_ep_disable()
989 dev_dbg(udc->dev, "Disabled %s\n", ep->name); in __max3420_ep_disable()
995 struct max3420_udc *udc = ep->udc; in max3420_ep_disable()
997 max3420_nuke(ep, -ESHUTDOWN); in max3420_ep_disable()
1001 wake_up_process(udc->thread_task); in max3420_ep_disable()
1016 req->ep = ep; in max3420_alloc_request()
1018 return &req->usb_req; in max3420_alloc_request()
1031 struct max3420_udc *udc = ep->udc; in max3420_ep_queue()
1034 _req->status = -EINPROGRESS; in max3420_ep_queue()
1035 _req->actual = 0; in max3420_ep_queue()
1037 spin_lock_irqsave(&ep->lock, flags); in max3420_ep_queue()
1038 list_add_tail(&req->queue, &ep->queue); in max3420_ep_queue()
1039 spin_unlock_irqrestore(&ep->lock, flags); in max3420_ep_queue()
1041 wake_up_process(udc->thread_task); in max3420_ep_queue()
1053 spin_lock_irqsave(&ep->lock, flags); in max3420_ep_dequeue()
1056 list_for_each_entry(iter, &ep->queue, queue) { in max3420_ep_dequeue()
1059 list_del_init(&req->queue); in max3420_ep_dequeue()
1064 spin_unlock_irqrestore(&ep->lock, flags); in max3420_ep_dequeue()
1067 max3420_req_done(req, -ECONNRESET); in max3420_ep_dequeue()
1086 int ret = -EINVAL; in max3420_wakeup()
1088 spin_lock_irqsave(&udc->lock, flags); in max3420_wakeup()
1091 if (udc->remote_wkp) { in max3420_wakeup()
1092 udc->todo |= REMOTE_WAKEUP; in max3420_wakeup()
1096 spin_unlock_irqrestore(&udc->lock, flags); in max3420_wakeup()
1098 if (udc->thread_task) in max3420_wakeup()
1099 wake_up_process(udc->thread_task); in max3420_wakeup()
1109 spin_lock_irqsave(&udc->lock, flags); in max3420_udc_start()
1111 driver->driver.bus = NULL; in max3420_udc_start()
1112 udc->driver = driver; in max3420_udc_start()
1113 udc->gadget.speed = USB_SPEED_FULL; in max3420_udc_start()
1115 udc->gadget.is_selfpowered = udc->is_selfpowered; in max3420_udc_start()
1116 udc->remote_wkp = 0; in max3420_udc_start()
1117 udc->softconnect = true; in max3420_udc_start()
1118 udc->todo |= UDC_START; in max3420_udc_start()
1119 spin_unlock_irqrestore(&udc->lock, flags); in max3420_udc_start()
1121 if (udc->thread_task) in max3420_udc_start()
1122 wake_up_process(udc->thread_task); in max3420_udc_start()
1132 spin_lock_irqsave(&udc->lock, flags); in max3420_udc_stop()
1133 udc->is_selfpowered = udc->gadget.is_selfpowered; in max3420_udc_stop()
1134 udc->gadget.speed = USB_SPEED_UNKNOWN; in max3420_udc_stop()
1135 udc->driver = NULL; in max3420_udc_stop()
1136 udc->softconnect = false; in max3420_udc_stop()
1137 udc->todo |= UDC_START; in max3420_udc_stop()
1138 spin_unlock_irqrestore(&udc->lock, flags); in max3420_udc_stop()
1140 if (udc->thread_task) in max3420_udc_stop()
1141 wake_up_process(udc->thread_task); in max3420_udc_stop()
1156 INIT_LIST_HEAD(&udc->gadget.ep_list); in max3420_eps_init()
1159 struct max3420_ep *ep = &udc->ep[idx]; in max3420_eps_init()
1161 spin_lock_init(&ep->lock); in max3420_eps_init()
1162 INIT_LIST_HEAD(&ep->queue); in max3420_eps_init()
1164 ep->udc = udc; in max3420_eps_init()
1165 ep->id = idx; in max3420_eps_init()
1166 ep->halted = 0; in max3420_eps_init()
1167 ep->maxpacket = 0; in max3420_eps_init()
1168 ep->ep_usb.name = ep->name; in max3420_eps_init()
1169 ep->ep_usb.ops = &max3420_ep_ops; in max3420_eps_init()
1170 usb_ep_set_maxpacket_limit(&ep->ep_usb, MAX3420_EP_MAX_PACKET); in max3420_eps_init()
1173 ep->ep_usb.desc = &ep0_desc; in max3420_eps_init()
1174 ep->ep_usb.maxpacket = usb_endpoint_maxp(&ep0_desc); in max3420_eps_init()
1175 ep->ep_usb.caps.type_control = true; in max3420_eps_init()
1176 ep->ep_usb.caps.dir_in = true; in max3420_eps_init()
1177 ep->ep_usb.caps.dir_out = true; in max3420_eps_init()
1178 snprintf(ep->name, MAX3420_EPNAME_SIZE, "ep0"); in max3420_eps_init()
1183 ep->ep_usb.caps.dir_in = false; in max3420_eps_init()
1184 ep->ep_usb.caps.dir_out = true; in max3420_eps_init()
1185 snprintf(ep->name, MAX3420_EPNAME_SIZE, "ep1-bulk-out"); in max3420_eps_init()
1187 ep->ep_usb.caps.dir_in = true; in max3420_eps_init()
1188 ep->ep_usb.caps.dir_out = false; in max3420_eps_init()
1189 snprintf(ep->name, MAX3420_EPNAME_SIZE, in max3420_eps_init()
1190 "ep%d-bulk-in", idx); in max3420_eps_init()
1192 ep->ep_usb.caps.type_iso = false; in max3420_eps_init()
1193 ep->ep_usb.caps.type_int = false; in max3420_eps_init()
1194 ep->ep_usb.caps.type_bulk = true; in max3420_eps_init()
1196 list_add_tail(&ep->ep_usb.ep_list, in max3420_eps_init()
1197 &udc->gadget.ep_list); in max3420_eps_init()
1207 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) { in max3420_probe()
1208 dev_err(&spi->dev, "UDC needs full duplex to work\n"); in max3420_probe()
1209 return -EINVAL; in max3420_probe()
1212 spi->mode = SPI_MODE_3; in max3420_probe()
1213 spi->bits_per_word = 8; in max3420_probe()
1217 dev_err(&spi->dev, "Unable to setup SPI bus\n"); in max3420_probe()
1218 return -EFAULT; in max3420_probe()
1221 udc = devm_kzalloc(&spi->dev, sizeof(*udc), GFP_KERNEL); in max3420_probe()
1223 return -ENOMEM; in max3420_probe()
1225 udc->spi = spi; in max3420_probe()
1227 udc->remote_wkp = 0; in max3420_probe()
1230 udc->gadget.ops = &max3420_udc_ops; in max3420_probe()
1231 udc->gadget.max_speed = USB_SPEED_FULL; in max3420_probe()
1232 udc->gadget.speed = USB_SPEED_UNKNOWN; in max3420_probe()
1233 udc->gadget.ep0 = &udc->ep[0].ep_usb; in max3420_probe()
1234 udc->gadget.name = driver_name; in max3420_probe()
1236 spin_lock_init(&udc->lock); in max3420_probe()
1237 mutex_init(&udc->spi_bus_mutex); in max3420_probe()
1239 udc->ep0req.ep = &udc->ep[0]; in max3420_probe()
1240 udc->ep0req.usb_req.buf = udc->ep0buf; in max3420_probe()
1241 INIT_LIST_HEAD(&udc->ep0req.queue); in max3420_probe()
1250 err = usb_add_gadget_udc(&spi->dev, &udc->gadget); in max3420_probe()
1254 udc->dev = &udc->gadget.dev; in max3420_probe()
1258 irq = of_irq_get_byname(spi->dev.of_node, "udc"); in max3420_probe()
1259 err = devm_request_irq(&spi->dev, irq, max3420_irq_handler, 0, in max3420_probe()
1264 udc->thread_task = kthread_create(max3420_thread, udc, in max3420_probe()
1265 "max3420-thread"); in max3420_probe()
1266 if (IS_ERR(udc->thread_task)) { in max3420_probe()
1267 err = PTR_ERR(udc->thread_task); in max3420_probe()
1271 irq = of_irq_get_byname(spi->dev.of_node, "vbus"); in max3420_probe()
1272 if (irq <= 0) { /* no vbus irq implies self-powered design */ in max3420_probe()
1273 udc->is_selfpowered = 1; in max3420_probe()
1274 udc->vbus_active = true; in max3420_probe()
1275 udc->todo |= UDC_START; in max3420_probe()
1276 usb_udc_vbus_handler(&udc->gadget, udc->vbus_active); in max3420_probe()
1277 usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED); in max3420_probe()
1280 udc->is_selfpowered = 0; in max3420_probe()
1284 udc->vbus_active = true; in max3420_probe()
1286 err = devm_request_irq(&spi->dev, irq, in max3420_probe()
1295 usb_del_gadget_udc(&udc->gadget); in max3420_probe()
1304 usb_del_gadget_udc(&udc->gadget); in max3420_remove()
1306 spin_lock_irqsave(&udc->lock, flags); in max3420_remove()
1308 kthread_stop(udc->thread_task); in max3420_remove()
1310 spin_unlock_irqrestore(&udc->lock, flags); in max3420_remove()
1314 { .compatible = "maxim,max3420-udc"},
1315 { .compatible = "maxim,max3421-udc"},
1322 .name = "max3420-udc",