Lines Matching +full:halt +full:- +full:regs
1 // SPDX-License-Identifier: GPL-2.0+
14 * - Andreas Larsson <andreas@gaisler.com>
15 * - Marko Isomaki
36 #include <linux/dma-mapping.h>
64 /* ---------------------------------------------------------------------- */
93 int buflen = ep->is_in ? req->req.length : req->req.actual; in gr_dbgprint_request()
97 dev_dbg(ep->dev->dev, "%s: 0x%p, %d bytes data%s:\n", str, req, buflen, in gr_dbgprint_request()
100 rowlen, 4, req->req.buf, plen, false); in gr_dbgprint_request()
106 dev_vdbg(dev->dev, "REQ: %02x.%02x v%04x i%04x l%04x\n", in gr_dbgprint_devreq()
119 /* ---------------------------------------------------------------------- */
126 u32 epctrl = gr_read32(&ep->regs->epctrl); in gr_seq_ep_show()
127 u32 epstat = gr_read32(&ep->regs->epstat); in gr_seq_ep_show()
131 seq_printf(seq, "%s:\n", ep->ep.name); in gr_seq_ep_show()
136 seq_printf(seq, " dma_start = %d\n", ep->dma_start); in gr_seq_ep_show()
137 seq_printf(seq, " stopped = %d\n", ep->stopped); in gr_seq_ep_show()
138 seq_printf(seq, " wedged = %d\n", ep->wedged); in gr_seq_ep_show()
139 seq_printf(seq, " callback = %d\n", ep->callback); in gr_seq_ep_show()
140 seq_printf(seq, " maxpacket = %d\n", ep->ep.maxpacket); in gr_seq_ep_show()
141 seq_printf(seq, " maxpacket_limit = %d\n", ep->ep.maxpacket_limit); in gr_seq_ep_show()
142 seq_printf(seq, " bytes_per_buffer = %d\n", ep->bytes_per_buffer); in gr_seq_ep_show()
156 if (list_empty(&ep->queue)) { in gr_seq_ep_show()
162 list_for_each_entry(req, &ep->queue, queue) { in gr_seq_ep_show()
167 &req->req.buf, req->req.actual, req->req.length); in gr_seq_ep_show()
169 next = req->first_desc; in gr_seq_ep_show()
172 next = desc->next_desc; in gr_seq_ep_show()
174 desc == req->curr_desc ? 'c' : ' ', in gr_seq_ep_show()
175 desc, desc->paddr, desc->ctrl, desc->data); in gr_seq_ep_show()
176 } while (desc != req->last_desc); in gr_seq_ep_show()
183 struct gr_udc *dev = seq->private; in gr_dfs_show()
184 u32 control = gr_read32(&dev->regs->control); in gr_dfs_show()
185 u32 status = gr_read32(&dev->regs->status); in gr_dfs_show()
189 usb_state_string(dev->gadget.state)); in gr_dfs_show()
193 seq_printf(seq, "ep0state = %s\n", gr_ep0state_string(dev->ep0state)); in gr_dfs_show()
194 seq_printf(seq, "irq_enabled = %d\n", dev->irq_enabled); in gr_dfs_show()
195 seq_printf(seq, "remote_wakeup = %d\n", dev->remote_wakeup); in gr_dfs_show()
196 seq_printf(seq, "test_mode = %d\n", dev->test_mode); in gr_dfs_show()
199 list_for_each_entry(ep, &dev->ep_list, ep_list) in gr_dfs_show()
211 root = debugfs_create_dir(dev_name(dev->dev), usb_debug_root); in gr_dfs_create()
217 debugfs_lookup_and_remove(dev_name(dev->dev), usb_debug_root); in gr_dfs_delete()
227 /* ---------------------------------------------------------------------- */
236 dma_desc = dma_pool_zalloc(ep->dev->desc_pool, gfp_flags, &paddr); in gr_alloc_dma_desc()
238 dev_err(ep->dev->dev, "Could not allocate from DMA pool\n"); in gr_alloc_dma_desc()
242 dma_desc->paddr = paddr; in gr_alloc_dma_desc()
250 dma_pool_free(dev->desc_pool, desc, (dma_addr_t)desc->paddr); in gr_free_dma_desc()
259 next = req->first_desc; in gr_free_dma_desc_chain()
265 next = desc->next_desc; in gr_free_dma_desc_chain()
267 } while (desc != req->last_desc); in gr_free_dma_desc_chain()
269 req->first_desc = NULL; in gr_free_dma_desc_chain()
270 req->curr_desc = NULL; in gr_free_dma_desc_chain()
271 req->last_desc = NULL; in gr_free_dma_desc_chain()
280 * Must be called with dev->lock held and irqs disabled.
284 __releases(&dev->lock) in gr_finish_request()
285 __acquires(&dev->lock) in gr_finish_request()
289 list_del_init(&req->queue); in gr_finish_request()
291 if (likely(req->req.status == -EINPROGRESS)) in gr_finish_request()
292 req->req.status = status; in gr_finish_request()
294 status = req->req.status; in gr_finish_request()
296 dev = ep->dev; in gr_finish_request()
297 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in); in gr_finish_request()
300 if (ep->is_in) { /* For OUT, req->req.actual gets updated bit by bit */ in gr_finish_request()
301 req->req.actual = req->req.length; in gr_finish_request()
302 } else if (req->oddlen && req->req.actual > req->evenlen) { in gr_finish_request()
305 * divisible by ep->ep.maxpacket and the last descriptor was in gr_finish_request()
308 char *buftail = ((char *)req->req.buf + req->evenlen); in gr_finish_request()
310 memcpy(buftail, ep->tailbuf, req->oddlen); in gr_finish_request()
312 if (req->req.actual > req->req.length) { in gr_finish_request()
314 dev_dbg(ep->dev->dev, "Overflow for ep %s\n", in gr_finish_request()
315 ep->ep.name); in gr_finish_request()
317 req->req.status = -EOVERFLOW; in gr_finish_request()
322 if (ep->is_in) in gr_finish_request()
328 /* Prevent changes to ep->queue during callback */ in gr_finish_request()
329 ep->callback = 1; in gr_finish_request()
330 if (req == dev->ep0reqo && !status) { in gr_finish_request()
331 if (req->setup) in gr_finish_request()
334 dev_err(dev->dev, in gr_finish_request()
336 } else if (req->req.complete) { in gr_finish_request()
337 spin_unlock(&dev->lock); in gr_finish_request()
339 usb_gadget_giveback_request(&ep->ep, &req->req); in gr_finish_request()
341 spin_lock(&dev->lock); in gr_finish_request()
343 ep->callback = 0; in gr_finish_request()
354 INIT_LIST_HEAD(&req->queue); in gr_alloc_request()
356 return &req->req; in gr_alloc_request()
362 * Must be called with dev->lock held and with !ep->stopped.
369 if (list_empty(&ep->queue)) { in gr_start_dma()
370 ep->dma_start = 0; in gr_start_dma()
374 req = list_first_entry(&ep->queue, struct gr_request, queue); in gr_start_dma()
377 BUG_ON(!req->curr_desc); in gr_start_dma()
381 * ep->ep.maxpacket. It could lead to buffer overruns if an unexpectedly in gr_start_dma()
385 if (!ep->is_in && req->oddlen) in gr_start_dma()
386 req->last_desc->data = ep->tailbuf_paddr; in gr_start_dma()
391 gr_write32(&ep->regs->dmaaddr, req->curr_desc->paddr); in gr_start_dma()
394 dmactrl = gr_read32(&ep->regs->dmactrl); in gr_start_dma()
395 gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_DA); in gr_start_dma()
397 ep->dma_start = 1; in gr_start_dma()
404 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
410 req = list_first_entry(&ep->queue, struct gr_request, queue); in gr_dma_advance()
412 gr_start_dma(ep); /* Regardless of ep->dma_start */ in gr_dma_advance()
419 * Must be called with dev->lock held.
425 dmactrl = gr_read32(&ep->regs->dmactrl); in gr_abort_dma()
426 gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_AD); in gr_abort_dma()
443 return -ENOMEM; in gr_add_dma_desc()
445 desc->data = data; in gr_add_dma_desc()
446 if (ep->is_in) in gr_add_dma_desc()
447 desc->ctrl = in gr_add_dma_desc()
450 desc->ctrl = GR_DESC_OUT_CTRL_IE; in gr_add_dma_desc()
452 if (!req->first_desc) { in gr_add_dma_desc()
453 req->first_desc = desc; in gr_add_dma_desc()
454 req->curr_desc = desc; in gr_add_dma_desc()
456 req->last_desc->next_desc = desc; in gr_add_dma_desc()
457 req->last_desc->next = desc->paddr; in gr_add_dma_desc()
458 req->last_desc->ctrl |= GR_DESC_OUT_CTRL_NX; in gr_add_dma_desc()
460 req->last_desc = desc; in gr_add_dma_desc()
467 * together covers req->req.length bytes of the buffer at DMA address
468 * req->req.dma for the OUT direction.
482 req->first_desc = NULL; /* Signals that no allocation is done yet */ in gr_setup_out_desc_list()
483 bytes_left = req->req.length; in gr_setup_out_desc_list()
486 dma_addr_t start = req->req.dma + bytes_used; in gr_setup_out_desc_list()
487 u16 size = min(bytes_left, ep->bytes_per_buffer); in gr_setup_out_desc_list()
489 if (size < ep->bytes_per_buffer) { in gr_setup_out_desc_list()
491 req->evenlen = req->req.length - bytes_left; in gr_setup_out_desc_list()
492 req->oddlen = size; in gr_setup_out_desc_list()
499 bytes_left -= size; in gr_setup_out_desc_list()
503 req->first_desc->ctrl |= GR_DESC_OUT_CTRL_EN; in gr_setup_out_desc_list()
508 gr_free_dma_desc_chain(ep->dev, req); in gr_setup_out_desc_list()
515 * together covers req->req.length bytes of the buffer at DMA address
516 * req->req.dma for the IN direction.
519 * this up into several payloads automatically. Moreover, ep->bytes_per_buffer
535 req->first_desc = NULL; /* Signals that no allocation is done yet */ in gr_setup_in_desc_list()
536 bytes_left = req->req.length; in gr_setup_in_desc_list()
539 dma_addr_t start = req->req.dma + bytes_used; in gr_setup_in_desc_list()
540 u16 size = min(bytes_left, ep->bytes_per_buffer); in gr_setup_in_desc_list()
546 bytes_left -= size; in gr_setup_in_desc_list()
552 * available when req->req.zero is set and the data length is even in gr_setup_in_desc_list()
553 * multiples of ep->ep.maxpacket. in gr_setup_in_desc_list()
555 if (req->req.zero && (req->req.length % ep->ep.maxpacket == 0)) { in gr_setup_in_desc_list()
565 req->last_desc->ctrl |= GR_DESC_IN_CTRL_PI; in gr_setup_in_desc_list()
570 gr_free_dma_desc_chain(ep->dev, req); in gr_setup_in_desc_list()
575 /* Must be called with dev->lock held */
578 struct gr_udc *dev = ep->dev; in gr_queue()
581 if (unlikely(!ep->ep.desc && ep->num != 0)) { in gr_queue()
582 dev_err(dev->dev, "No ep descriptor for %s\n", ep->ep.name); in gr_queue()
583 return -EINVAL; in gr_queue()
586 if (unlikely(!req->req.buf || !list_empty(&req->queue))) { in gr_queue()
587 dev_err(dev->dev, in gr_queue()
589 ep->ep.name, req->req.buf, list_empty(&req->queue)); in gr_queue()
590 return -EINVAL; in gr_queue()
593 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) { in gr_queue()
594 dev_err(dev->dev, "-ESHUTDOWN"); in gr_queue()
595 return -ESHUTDOWN; in gr_queue()
599 if (dev->ep0state == GR_EP0_SUSPEND) { in gr_queue()
600 dev_err(dev->dev, "-EBUSY"); in gr_queue()
601 return -EBUSY; in gr_queue()
605 ret = usb_gadget_map_request(&dev->gadget, &req->req, ep->is_in); in gr_queue()
607 dev_err(dev->dev, "usb_gadget_map_request"); in gr_queue()
611 if (ep->is_in) in gr_queue()
618 req->req.status = -EINPROGRESS; in gr_queue()
619 req->req.actual = 0; in gr_queue()
620 list_add_tail(&req->queue, &ep->queue); in gr_queue()
623 if (!ep->dma_start && likely(!ep->stopped)) in gr_queue()
632 * Must be called with dev->lock held.
637 if (ep->is_in) in gr_queue_int()
643 /* ---------------------------------------------------------------------- */
649 * Must be called with dev->lock held and irqs disabled.
655 ep->stopped = 1; in gr_ep_nuke()
656 ep->dma_start = 0; in gr_ep_nuke()
659 while (!list_empty(&ep->queue)) { in gr_ep_nuke()
660 req = list_first_entry(&ep->queue, struct gr_request, queue); in gr_ep_nuke()
661 gr_finish_request(ep, req, -ESHUTDOWN); in gr_ep_nuke()
668 * Must be called with dev->lock held.
672 gr_write32(&ep->regs->epctrl, 0); in gr_ep_reset()
673 gr_write32(&ep->regs->dmactrl, 0); in gr_ep_reset()
675 ep->ep.maxpacket = MAX_CTRL_PL_SIZE; in gr_ep_reset()
676 ep->ep.desc = NULL; in gr_ep_reset()
677 ep->stopped = 1; in gr_ep_reset()
678 ep->dma_start = 0; in gr_ep_reset()
684 * Must be called with dev->lock held.
690 epctrl = gr_read32(&dev->epo[0].regs->epctrl); in gr_control_stall()
691 gr_write32(&dev->epo[0].regs->epctrl, epctrl | GR_EPCTRL_CS); in gr_control_stall()
692 epctrl = gr_read32(&dev->epi[0].regs->epctrl); in gr_control_stall()
693 gr_write32(&dev->epi[0].regs->epctrl, epctrl | GR_EPCTRL_CS); in gr_control_stall()
695 dev->ep0state = GR_EP0_STALL; in gr_control_stall()
699 * Halts, halts and wedges, or clears halt for an endpoint.
701 * Must be called with dev->lock held.
703 static int gr_ep_halt_wedge(struct gr_ep *ep, int halt, int wedge, int fromhost) in gr_ep_halt_wedge() argument
708 if (ep->num && !ep->ep.desc) in gr_ep_halt_wedge()
709 return -EINVAL; in gr_ep_halt_wedge()
711 if (ep->num && ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) in gr_ep_halt_wedge()
712 return -EOPNOTSUPP; in gr_ep_halt_wedge()
714 /* Never actually halt ep0, and therefore never clear halt for ep0 */ in gr_ep_halt_wedge()
715 if (!ep->num) { in gr_ep_halt_wedge()
716 if (halt && !fromhost) { in gr_ep_halt_wedge()
717 /* ep0 halt from gadget - generate protocol stall */ in gr_ep_halt_wedge()
718 gr_control_stall(ep->dev); in gr_ep_halt_wedge()
719 dev_dbg(ep->dev->dev, "EP: stall ep0\n"); in gr_ep_halt_wedge()
722 return -EINVAL; in gr_ep_halt_wedge()
725 dev_dbg(ep->dev->dev, "EP: %s halt %s\n", in gr_ep_halt_wedge()
726 (halt ? (wedge ? "wedge" : "set") : "clear"), ep->ep.name); in gr_ep_halt_wedge()
728 epctrl = gr_read32(&ep->regs->epctrl); in gr_ep_halt_wedge()
729 if (halt) { in gr_ep_halt_wedge()
730 /* Set HALT */ in gr_ep_halt_wedge()
731 gr_write32(&ep->regs->epctrl, epctrl | GR_EPCTRL_EH); in gr_ep_halt_wedge()
732 ep->stopped = 1; in gr_ep_halt_wedge()
734 ep->wedged = 1; in gr_ep_halt_wedge()
736 gr_write32(&ep->regs->epctrl, epctrl & ~GR_EPCTRL_EH); in gr_ep_halt_wedge()
737 ep->stopped = 0; in gr_ep_halt_wedge()
738 ep->wedged = 0; in gr_ep_halt_wedge()
741 if (!ep->dma_start) in gr_ep_halt_wedge()
748 /* Must be called with dev->lock held */
751 if (dev->ep0state != value) in gr_set_ep0state()
752 dev_vdbg(dev->dev, "STATE: ep0state=%s\n", in gr_set_ep0state()
754 dev->ep0state = value; in gr_set_ep0state()
760 * Must be called with dev->lock held.
764 gr_write32(&dev->regs->control, 0); in gr_disable_interrupts_and_pullup()
766 dev->irq_enabled = 0; in gr_disable_interrupts_and_pullup()
772 * Must be called with dev->lock held and irqs disabled.
778 list_for_each_entry(ep, &dev->ep_list, ep_list) in gr_stop_activity()
784 usb_gadget_set_state(&dev->gadget, USB_STATE_NOTATTACHED); in gr_stop_activity()
787 /* ---------------------------------------------------------------------- */
798 dev = ep->dev; in gr_ep0_testmode_complete()
800 spin_lock(&dev->lock); in gr_ep0_testmode_complete()
802 control = gr_read32(&dev->regs->control); in gr_ep0_testmode_complete()
803 control |= GR_CONTROL_TM | (dev->test_mode << GR_CONTROL_TS_POS); in gr_ep0_testmode_complete()
804 gr_write32(&dev->regs->control, control); in gr_ep0_testmode_complete()
806 spin_unlock(&dev->lock); in gr_ep0_testmode_complete()
817 * Must be called with dev->lock held.
823 u8 *reqbuf = dev->ep0reqi->req.buf; in gr_ep0_respond()
829 dev->ep0reqi->req.length = length; in gr_ep0_respond()
830 dev->ep0reqi->req.complete = complete; in gr_ep0_respond()
832 status = gr_queue_int(&dev->epi[0], dev->ep0reqi, GFP_ATOMIC); in gr_ep0_respond()
834 dev_err(dev->dev, in gr_ep0_respond()
843 * Must be called with dev->lock held.
856 * Must be called with dev->lock held.
868 * Must be called with dev->lock held.
874 control = gr_read32(&dev->regs->control) & ~GR_CONTROL_UA_MASK; in gr_set_address()
877 gr_write32(&dev->regs->control, control); in gr_set_address()
884 * Must be called with dev->lock held.
894 dev_dbg(dev->dev, "STATUS: address %d\n", value & 0xff); in gr_device_request()
897 usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS); in gr_device_request()
899 usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT); in gr_device_request()
904 response = 0x0001 | (dev->remote_wakeup ? 0x0002 : 0); in gr_device_request()
911 dev->remote_wakeup = 1; in gr_device_request()
918 dev->test_mode = test; in gr_device_request()
929 dev->remote_wakeup = 0; in gr_device_request()
942 * Must be called with dev->lock held.
947 if (dev->gadget.state != USB_STATE_CONFIGURED) in gr_interface_request()
948 return -1; in gr_interface_request()
976 * Must be called with dev->lock held.
987 if ((is_in && epnum >= dev->nepi) || (!is_in && epnum >= dev->nepo)) in gr_endpoint_request()
988 return -1; in gr_endpoint_request()
990 if (dev->gadget.state != USB_STATE_CONFIGURED && epnum != 0) in gr_endpoint_request()
991 return -1; in gr_endpoint_request()
993 ep = (is_in ? &dev->epi[epnum] : &dev->epo[epnum]); in gr_endpoint_request()
997 halted = gr_read32(&ep->regs->epctrl) & GR_EPCTRL_EH; in gr_endpoint_request()
1013 if (ep->wedged) in gr_endpoint_request()
1014 return -1; in gr_endpoint_request()
1026 /* Must be called with dev->lock held */
1029 int ret = gr_queue_int(&dev->epo[0], dev->ep0reqo, GFP_ATOMIC); in gr_ep0out_requeue()
1032 dev_err(dev->dev, "Could not queue ep0out setup request: %d\n", in gr_ep0out_requeue()
1039 * Must be called with dev->lock held and irqs disabled
1042 __releases(&dev->lock) in gr_ep0_setup()
1043 __acquires(&dev->lock) in gr_ep0_setup()
1058 /* Restore from ep0 halt */ in gr_ep0_setup()
1059 if (dev->ep0state == GR_EP0_STALL) { in gr_ep0_setup()
1061 if (!req->req.actual) in gr_ep0_setup()
1065 if (dev->ep0state == GR_EP0_ISTATUS) { in gr_ep0_setup()
1067 if (req->req.actual > 0) in gr_ep0_setup()
1068 dev_dbg(dev->dev, in gr_ep0_setup()
1073 } else if (dev->ep0state != GR_EP0_SETUP) { in gr_ep0_setup()
1074 dev_info(dev->dev, in gr_ep0_setup()
1075 "Unexpected ep0out request at state %s - stalling\n", in gr_ep0_setup()
1076 gr_ep0state_string(dev->ep0state)); in gr_ep0_setup()
1080 } else if (!req->req.actual) { in gr_ep0_setup()
1081 dev_dbg(dev->dev, "Unexpected ZLP at state %s\n", in gr_ep0_setup()
1082 gr_ep0state_string(dev->ep0state)); in gr_ep0_setup()
1087 for (i = 0; i < req->req.actual; i++) in gr_ep0_setup()
1088 u.raw[i] = ((u8 *)req->req.buf)[i]; in gr_ep0_setup()
1125 spin_unlock(&dev->lock); in gr_ep0_setup()
1127 dev_vdbg(dev->dev, "DELEGATE\n"); in gr_ep0_setup()
1128 status = dev->driver->setup(&dev->gadget, &u.ctrl); in gr_ep0_setup()
1130 spin_lock(&dev->lock); in gr_ep0_setup()
1135 dev_vdbg(dev->dev, "STALL\n"); in gr_ep0_setup()
1142 dev_dbg(dev->dev, "STATUS: deconfigured\n"); in gr_ep0_setup()
1143 usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS); in gr_ep0_setup()
1146 dev_dbg(dev->dev, "STATUS: configured: %d\n", value); in gr_ep0_setup()
1147 usb_gadget_set_state(&dev->gadget, in gr_ep0_setup()
1153 if (dev->ep0state == GR_EP0_ODATA) in gr_ep0_setup()
1155 else if (dev->ep0state == GR_EP0_IDATA) in gr_ep0_setup()
1164 /* ---------------------------------------------------------------------- */
1167 /* Must be called with dev->lock held and irqs disabled */
1172 dev->gadget.speed = GR_SPEED(status); in gr_vbus_connected()
1173 usb_gadget_set_state(&dev->gadget, USB_STATE_POWERED); in gr_vbus_connected()
1178 gr_write32(&dev->regs->control, control); in gr_vbus_connected()
1181 /* Must be called with dev->lock held */
1186 dev->irq_enabled = 1; in gr_enable_vbus_detect()
1188 gr_write32(&dev->regs->control, GR_CONTROL_VI); in gr_enable_vbus_detect()
1191 status = gr_read32(&dev->regs->status); in gr_enable_vbus_detect()
1196 /* Must be called with dev->lock held and irqs disabled */
1202 if (dev->driver && dev->driver->disconnect) { in gr_vbus_disconnected()
1203 spin_unlock(&dev->lock); in gr_vbus_disconnected()
1205 dev->driver->disconnect(&dev->gadget); in gr_vbus_disconnected()
1207 spin_lock(&dev->lock); in gr_vbus_disconnected()
1213 /* Must be called with dev->lock held and irqs disabled */
1218 usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT); in gr_udc_usbreset()
1219 dev->gadget.speed = GR_SPEED(status); in gr_udc_usbreset()
1221 gr_ep_nuke(&dev->epo[0]); in gr_udc_usbreset()
1222 gr_ep_nuke(&dev->epi[0]); in gr_udc_usbreset()
1223 dev->epo[0].stopped = 0; in gr_udc_usbreset()
1224 dev->epi[0].stopped = 0; in gr_udc_usbreset()
1228 /* ---------------------------------------------------------------------- */
1234 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1240 req = list_first_entry(&ep->queue, struct gr_request, queue); in gr_handle_in_ep()
1241 if (!req->last_desc) in gr_handle_in_ep()
1244 if (READ_ONCE(req->last_desc->ctrl) & GR_DESC_IN_CTRL_EN) in gr_handle_in_ep()
1247 if (gr_read32(&ep->regs->epstat) & (GR_EPSTAT_B1 | GR_EPSTAT_B0)) in gr_handle_in_ep()
1259 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1267 struct gr_udc *dev = ep->dev; in gr_handle_out_ep()
1269 req = list_first_entry(&ep->queue, struct gr_request, queue); in gr_handle_out_ep()
1270 if (!req->curr_desc) in gr_handle_out_ep()
1273 ctrl = READ_ONCE(req->curr_desc->ctrl); in gr_handle_out_ep()
1279 req->req.actual += len; in gr_handle_out_ep()
1281 req->setup = 1; in gr_handle_out_ep()
1283 if (len < ep->ep.maxpacket || req->req.actual >= req->req.length) { in gr_handle_out_ep()
1284 /* Short packet or >= expected size - we are done */ in gr_handle_out_ep()
1286 if ((ep == &dev->epo[0]) && (dev->ep0state == GR_EP0_OSTATUS)) { in gr_handle_out_ep()
1291 * ep0_setup that can change dev->ep0state. in gr_handle_out_ep()
1300 req->curr_desc = req->curr_desc->next_desc; in gr_handle_out_ep()
1301 req->curr_desc->ctrl |= GR_DESC_OUT_CTRL_EN; in gr_handle_out_ep()
1303 ep_dmactrl = gr_read32(&ep->regs->dmactrl); in gr_handle_out_ep()
1304 gr_write32(&ep->regs->dmactrl, ep_dmactrl | GR_DMACTRL_DA); in gr_handle_out_ep()
1313 * Must be called with dev->lock held and irqs disabled.
1317 u32 status = gr_read32(&dev->regs->status); in gr_handle_state_changes()
1319 int powstate = !(dev->gadget.state == USB_STATE_NOTATTACHED || in gr_handle_state_changes()
1320 dev->gadget.state == USB_STATE_ATTACHED); in gr_handle_state_changes()
1324 dev_dbg(dev->dev, "STATUS: vbus valid detected\n"); in gr_handle_state_changes()
1331 dev_dbg(dev->dev, "STATUS: vbus invalid detected\n"); in gr_handle_state_changes()
1338 dev_dbg(dev->dev, "STATUS: USB reset - speed is %s\n", in gr_handle_state_changes()
1340 gr_write32(&dev->regs->status, GR_STATUS_UR); in gr_handle_state_changes()
1346 if (dev->gadget.speed != GR_SPEED(status)) { in gr_handle_state_changes()
1347 dev_dbg(dev->dev, "STATUS: USB Speed change to %s\n", in gr_handle_state_changes()
1349 dev->gadget.speed = GR_SPEED(status); in gr_handle_state_changes()
1354 if ((dev->ep0state != GR_EP0_SUSPEND) && !(status & GR_STATUS_SU)) { in gr_handle_state_changes()
1355 dev_dbg(dev->dev, "STATUS: USB suspend\n"); in gr_handle_state_changes()
1357 dev->suspended_from = dev->gadget.state; in gr_handle_state_changes()
1358 usb_gadget_set_state(&dev->gadget, USB_STATE_SUSPENDED); in gr_handle_state_changes()
1360 if ((dev->gadget.speed != USB_SPEED_UNKNOWN) && in gr_handle_state_changes()
1361 dev->driver && dev->driver->suspend) { in gr_handle_state_changes()
1362 spin_unlock(&dev->lock); in gr_handle_state_changes()
1364 dev->driver->suspend(&dev->gadget); in gr_handle_state_changes()
1366 spin_lock(&dev->lock); in gr_handle_state_changes()
1372 if ((dev->ep0state == GR_EP0_SUSPEND) && (status & GR_STATUS_SU)) { in gr_handle_state_changes()
1373 dev_dbg(dev->dev, "STATUS: USB resume\n"); in gr_handle_state_changes()
1374 if (dev->suspended_from == USB_STATE_POWERED) in gr_handle_state_changes()
1378 usb_gadget_set_state(&dev->gadget, dev->suspended_from); in gr_handle_state_changes()
1380 if ((dev->gadget.speed != USB_SPEED_UNKNOWN) && in gr_handle_state_changes()
1381 dev->driver && dev->driver->resume) { in gr_handle_state_changes()
1382 spin_unlock(&dev->lock); in gr_handle_state_changes()
1384 dev->driver->resume(&dev->gadget); in gr_handle_state_changes()
1386 spin_lock(&dev->lock); in gr_handle_state_changes()
1394 /* Non-interrupt context irq handler */
1403 spin_lock_irqsave(&dev->lock, flags); in gr_irq_handler()
1405 if (!dev->irq_enabled) in gr_irq_handler()
1413 for (i = 0; i < dev->nepi; i++) { in gr_irq_handler()
1414 ep = &dev->epi[i]; in gr_irq_handler()
1415 if (!ep->stopped && !ep->callback && !list_empty(&ep->queue)) in gr_irq_handler()
1420 for (i = 0; i < dev->nepo; i++) { in gr_irq_handler()
1421 ep = &dev->epo[i]; in gr_irq_handler()
1422 if (!ep->stopped && !ep->callback && !list_empty(&ep->queue)) in gr_irq_handler()
1434 list_for_each_entry(ep, &dev->ep_list, ep_list) { in gr_irq_handler()
1435 if (gr_read32(&ep->regs->dmactrl) & GR_DMACTRL_AE) { in gr_irq_handler()
1436 dev_err(dev->dev, in gr_irq_handler()
1438 ep->ep.name); in gr_irq_handler()
1445 spin_unlock_irqrestore(&dev->lock, flags); in gr_irq_handler()
1455 if (!dev->irq_enabled) in gr_irq()
1461 /* ---------------------------------------------------------------------- */
1477 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) in gr_ep_enable()
1478 return -EINVAL; in gr_ep_enable()
1480 dev = ep->dev; in gr_ep_enable()
1483 if (ep == &dev->epo[0] || ep == &dev->epi[0]) in gr_ep_enable()
1484 return -EINVAL; in gr_ep_enable()
1486 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) in gr_ep_enable()
1487 return -ESHUTDOWN; in gr_ep_enable()
1490 epctrl = gr_read32(&ep->regs->epctrl); in gr_ep_enable()
1492 return -EBUSY; in gr_ep_enable()
1495 if (!ep->is_in != !usb_endpoint_dir_in(desc)) in gr_ep_enable()
1496 return -EINVAL; in gr_ep_enable()
1499 if ((!ep->is_in && ep->num >= dev->nepo) || in gr_ep_enable()
1500 (ep->is_in && ep->num >= dev->nepi)) in gr_ep_enable()
1501 return -EINVAL; in gr_ep_enable()
1512 dev_err(dev->dev, "Unknown transfer type for %s\n", in gr_ep_enable()
1513 ep->ep.name); in gr_ep_enable()
1514 return -EINVAL; in gr_ep_enable()
1518 * Bits 10-0 set the max payload. 12-11 set the number of in gr_ep_enable()
1522 nt = usb_endpoint_maxp_mult(desc) - 1; in gr_ep_enable()
1525 dev_err(dev->dev, in gr_ep_enable()
1528 return -EINVAL; in gr_ep_enable()
1530 dev_err(dev->dev, in gr_ep_enable()
1532 return -EINVAL; in gr_ep_enable()
1534 dev_err(dev->dev, "Hw buffer size %d < max payload %d * %d\n", in gr_ep_enable()
1536 return -EINVAL; in gr_ep_enable()
1538 dev_err(dev->dev, "Max payload cannot be set to 0\n"); in gr_ep_enable()
1539 return -EINVAL; in gr_ep_enable()
1540 } else if (max > ep->ep.maxpacket_limit) { in gr_ep_enable()
1541 dev_err(dev->dev, "Requested max payload %d > limit %d\n", in gr_ep_enable()
1542 max, ep->ep.maxpacket_limit); in gr_ep_enable()
1543 return -EINVAL; in gr_ep_enable()
1546 spin_lock(&ep->dev->lock); in gr_ep_enable()
1548 if (!ep->stopped) { in gr_ep_enable()
1549 spin_unlock(&ep->dev->lock); in gr_ep_enable()
1550 return -EBUSY; in gr_ep_enable()
1553 ep->stopped = 0; in gr_ep_enable()
1554 ep->wedged = 0; in gr_ep_enable()
1555 ep->ep.desc = desc; in gr_ep_enable()
1556 ep->ep.maxpacket = max; in gr_ep_enable()
1557 ep->dma_start = 0; in gr_ep_enable()
1563 * regardless of direction when using high-bandwidth mode. in gr_ep_enable()
1565 ep->bytes_per_buffer = (nt + 1) * max; in gr_ep_enable()
1566 } else if (ep->is_in) { in gr_ep_enable()
1572 ep->bytes_per_buffer = (buffer_size / max) * max; in gr_ep_enable()
1578 ep->bytes_per_buffer = max; in gr_ep_enable()
1585 if (ep->is_in) in gr_ep_enable()
1587 gr_write32(&ep->regs->epctrl, epctrl); in gr_ep_enable()
1589 gr_write32(&ep->regs->dmactrl, GR_DMACTRL_IE | GR_DMACTRL_AI); in gr_ep_enable()
1591 spin_unlock(&ep->dev->lock); in gr_ep_enable()
1593 dev_dbg(ep->dev->dev, "EP: %s enabled - %s with %d bytes/buffer\n", in gr_ep_enable()
1594 ep->ep.name, gr_modestring[mode], ep->bytes_per_buffer); in gr_ep_enable()
1606 if (!_ep || !ep->ep.desc) in gr_ep_disable()
1607 return -ENODEV; in gr_ep_disable()
1609 dev = ep->dev; in gr_ep_disable()
1612 if (ep == &dev->epo[0] || ep == &dev->epi[0]) in gr_ep_disable()
1613 return -EINVAL; in gr_ep_disable()
1615 if (dev->ep0state == GR_EP0_SUSPEND) in gr_ep_disable()
1616 return -EBUSY; in gr_ep_disable()
1618 dev_dbg(ep->dev->dev, "EP: disable %s\n", ep->ep.name); in gr_ep_disable()
1620 spin_lock_irqsave(&dev->lock, flags); in gr_ep_disable()
1624 ep->ep.desc = NULL; in gr_ep_disable()
1626 spin_unlock_irqrestore(&dev->lock, flags); in gr_ep_disable()
1644 WARN(!list_empty(&req->queue), in gr_free_request()
1660 return -EINVAL; in gr_queue_ext()
1664 dev = ep->dev; in gr_queue_ext()
1666 spin_lock(&ep->dev->lock); in gr_queue_ext()
1674 if ((ep == &dev->epi[0]) && (dev->ep0state == GR_EP0_ODATA)) { in gr_queue_ext()
1675 ep = &dev->epo[0]; in gr_queue_ext()
1676 ep->ep.driver_data = dev->epi[0].ep.driver_data; in gr_queue_ext()
1679 if (ep->is_in) in gr_queue_ext()
1684 spin_unlock(&ep->dev->lock); in gr_queue_ext()
1699 if (!_ep || !_req || (!ep->ep.desc && ep->num != 0)) in gr_dequeue()
1700 return -EINVAL; in gr_dequeue()
1701 dev = ep->dev; in gr_dequeue()
1702 if (!dev->driver) in gr_dequeue()
1703 return -ESHUTDOWN; in gr_dequeue()
1706 if (dev->ep0state == GR_EP0_SUSPEND) in gr_dequeue()
1707 return -EBUSY; in gr_dequeue()
1709 spin_lock_irqsave(&dev->lock, flags); in gr_dequeue()
1712 list_for_each_entry(iter, &ep->queue, queue) { in gr_dequeue()
1713 if (&iter->req != _req) in gr_dequeue()
1719 ret = -EINVAL; in gr_dequeue()
1723 if (list_first_entry(&ep->queue, struct gr_request, queue) == req) { in gr_dequeue()
1726 if (ep->stopped) in gr_dequeue()
1727 gr_finish_request(ep, req, -ECONNRESET); in gr_dequeue()
1729 gr_dma_advance(ep, -ECONNRESET); in gr_dequeue()
1730 } else if (!list_empty(&req->queue)) { in gr_dequeue()
1731 /* Not being processed - gr_finish_request dequeues it */ in gr_dequeue()
1732 gr_finish_request(ep, req, -ECONNRESET); in gr_dequeue()
1734 ret = -EOPNOTSUPP; in gr_dequeue()
1738 spin_unlock_irqrestore(&dev->lock, flags); in gr_dequeue()
1744 static int gr_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge) in gr_set_halt_wedge() argument
1750 return -ENODEV; in gr_set_halt_wedge()
1753 spin_lock(&ep->dev->lock); in gr_set_halt_wedge()
1756 if (halt && ep->is_in && !list_empty(&ep->queue)) { in gr_set_halt_wedge()
1757 ret = -EAGAIN; in gr_set_halt_wedge()
1761 ret = gr_ep_halt_wedge(ep, halt, wedge, 0); in gr_set_halt_wedge()
1764 spin_unlock(&ep->dev->lock); in gr_set_halt_wedge()
1769 /* Halt endpoint */
1770 static int gr_set_halt(struct usb_ep *_ep, int halt) in gr_set_halt() argument
1772 return gr_set_halt_wedge(_ep, halt, 0); in gr_set_halt()
1775 /* Halt and wedge endpoint */
1792 return -ENODEV; in gr_fifo_status()
1795 epstat = gr_read32(&ep->regs->epstat); in gr_fifo_status()
1815 dev_vdbg(ep->dev->dev, "EP: flush fifo %s\n", ep->ep.name); in gr_fifo_flush()
1817 spin_lock(&ep->dev->lock); in gr_fifo_flush()
1819 epctrl = gr_read32(&ep->regs->epctrl); in gr_fifo_flush()
1821 gr_write32(&ep->regs->epctrl, epctrl); in gr_fifo_flush()
1823 spin_unlock(&ep->dev->lock); in gr_fifo_flush()
1842 /* ---------------------------------------------------------------------- */
1850 return -ENODEV; in gr_get_frame()
1852 return gr_read32(&dev->regs->status) & GR_STATUS_FN_MASK; in gr_get_frame()
1860 return -ENODEV; in gr_wakeup()
1864 if (!dev->remote_wakeup) in gr_wakeup()
1865 return -EINVAL; in gr_wakeup()
1867 spin_lock(&dev->lock); in gr_wakeup()
1869 gr_write32(&dev->regs->control, in gr_wakeup()
1870 gr_read32(&dev->regs->control) | GR_CONTROL_RW); in gr_wakeup()
1872 spin_unlock(&dev->lock); in gr_wakeup()
1883 return -ENODEV; in gr_pullup()
1886 spin_lock(&dev->lock); in gr_pullup()
1888 control = gr_read32(&dev->regs->control); in gr_pullup()
1893 gr_write32(&dev->regs->control, control); in gr_pullup()
1895 spin_unlock(&dev->lock); in gr_pullup()
1905 spin_lock(&dev->lock); in gr_udc_start()
1908 dev->driver = driver; in gr_udc_start()
1913 spin_unlock(&dev->lock); in gr_udc_start()
1923 spin_lock_irqsave(&dev->lock, flags); in gr_udc_stop()
1925 dev->driver = NULL; in gr_udc_stop()
1928 spin_unlock_irqrestore(&dev->lock, flags); in gr_udc_stop()
1942 /* ---------------------------------------------------------------------- */
1943 /* Module probe, removal and of-matching */
1957 /* Must be called with dev->lock held */
1966 ep = &dev->epi[num]; in gr_ep_init()
1967 ep->ep.name = inames[num]; in gr_ep_init()
1968 ep->regs = &dev->regs->epi[num]; in gr_ep_init()
1970 ep = &dev->epo[num]; in gr_ep_init()
1971 ep->ep.name = onames[num]; in gr_ep_init()
1972 ep->regs = &dev->regs->epo[num]; in gr_ep_init()
1976 ep->num = num; in gr_ep_init()
1977 ep->is_in = is_in; in gr_ep_init()
1978 ep->dev = dev; in gr_ep_init()
1979 ep->ep.ops = &gr_ep_ops; in gr_ep_init()
1980 INIT_LIST_HEAD(&ep->queue); in gr_ep_init()
1983 _req = gr_alloc_request(&ep->ep, GFP_ATOMIC); in gr_ep_init()
1985 return -ENOMEM; in gr_ep_init()
1987 buf = devm_kzalloc(dev->dev, PAGE_SIZE, GFP_DMA | GFP_ATOMIC); in gr_ep_init()
1989 gr_free_request(&ep->ep, _req); in gr_ep_init()
1990 return -ENOMEM; in gr_ep_init()
1994 req->req.buf = buf; in gr_ep_init()
1995 req->req.length = MAX_CTRL_PL_SIZE; in gr_ep_init()
1998 dev->ep0reqi = req; /* Complete gets set as used */ in gr_ep_init()
2000 dev->ep0reqo = req; /* Completion treated separately */ in gr_ep_init()
2002 usb_ep_set_maxpacket_limit(&ep->ep, MAX_CTRL_PL_SIZE); in gr_ep_init()
2003 ep->bytes_per_buffer = MAX_CTRL_PL_SIZE; in gr_ep_init()
2005 ep->ep.caps.type_control = true; in gr_ep_init()
2007 usb_ep_set_maxpacket_limit(&ep->ep, (u16)maxplimit); in gr_ep_init()
2008 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list); in gr_ep_init()
2010 ep->ep.caps.type_iso = true; in gr_ep_init()
2011 ep->ep.caps.type_bulk = true; in gr_ep_init()
2012 ep->ep.caps.type_int = true; in gr_ep_init()
2014 list_add_tail(&ep->ep_list, &dev->ep_list); in gr_ep_init()
2017 ep->ep.caps.dir_in = true; in gr_ep_init()
2019 ep->ep.caps.dir_out = true; in gr_ep_init()
2021 ep->tailbuf = dma_alloc_coherent(dev->dev, ep->ep.maxpacket_limit, in gr_ep_init()
2022 &ep->tailbuf_paddr, GFP_ATOMIC); in gr_ep_init()
2023 if (!ep->tailbuf) in gr_ep_init()
2024 return -ENOMEM; in gr_ep_init()
2029 /* Must be called with dev->lock held */
2032 struct device_node *np = dev->dev->of_node; in gr_udc_init()
2041 INIT_LIST_HEAD(&dev->gadget.ep_list); in gr_udc_init()
2042 dev->gadget.speed = USB_SPEED_UNKNOWN; in gr_udc_init()
2043 dev->gadget.ep0 = &dev->epi[0].ep; in gr_udc_init()
2045 INIT_LIST_HEAD(&dev->ep_list); in gr_udc_init()
2048 for (i = 0; i < dev->nepo; i++) { in gr_udc_init()
2056 for (i = 0; i < dev->nepi; i++) { in gr_udc_init()
2065 dev->remote_wakeup = 0; in gr_udc_init()
2070 gr_write32(&dev->epo[0].regs->epctrl, epctrl_val); in gr_udc_init()
2071 gr_write32(&dev->epi[0].regs->epctrl, epctrl_val | GR_EPCTRL_PI); in gr_udc_init()
2072 gr_write32(&dev->epo[0].regs->dmactrl, dmactrl_val); in gr_udc_init()
2073 gr_write32(&dev->epi[0].regs->dmactrl, dmactrl_val); in gr_udc_init()
2083 ep = &dev->epi[num]; in gr_ep_remove()
2085 ep = &dev->epo[num]; in gr_ep_remove()
2087 if (ep->tailbuf) in gr_ep_remove()
2088 dma_free_coherent(dev->dev, ep->ep.maxpacket_limit, in gr_ep_remove()
2089 ep->tailbuf, ep->tailbuf_paddr); in gr_ep_remove()
2097 if (dev->added) in gr_remove()
2098 usb_del_gadget_udc(&dev->gadget); /* Shuts everything down */ in gr_remove()
2099 if (dev->driver) { in gr_remove()
2100 dev_err(&pdev->dev, in gr_remove()
2106 dma_pool_destroy(dev->desc_pool); in gr_remove()
2109 gr_free_request(&dev->epi[0].ep, &dev->ep0reqi->req); in gr_remove()
2110 gr_free_request(&dev->epo[0].ep, &dev->ep0reqo->req); in gr_remove()
2112 for (i = 0; i < dev->nepo; i++) in gr_remove()
2114 for (i = 0; i < dev->nepi; i++) in gr_remove()
2119 return devm_request_threaded_irq(dev->dev, irq, gr_irq, gr_irq_handler, in gr_request_irq()
2126 struct gr_regs __iomem *regs; in gr_probe() local
2130 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); in gr_probe()
2132 return -ENOMEM; in gr_probe()
2133 dev->dev = &pdev->dev; in gr_probe()
2135 regs = devm_platform_ioremap_resource(pdev, 0); in gr_probe()
2136 if (IS_ERR(regs)) in gr_probe()
2137 return PTR_ERR(regs); in gr_probe()
2139 dev->irq = platform_get_irq(pdev, 0); in gr_probe()
2140 if (dev->irq < 0) in gr_probe()
2141 return dev->irq; in gr_probe()
2144 dev->irqi = platform_get_irq(pdev, 1); in gr_probe()
2145 if (dev->irqi > 0) { in gr_probe()
2146 dev->irqo = platform_get_irq(pdev, 2); in gr_probe()
2147 if (dev->irqo < 0) in gr_probe()
2148 return dev->irqo; in gr_probe()
2150 dev->irqi = 0; in gr_probe()
2153 dev->gadget.name = driver_name; in gr_probe()
2154 dev->gadget.max_speed = USB_SPEED_HIGH; in gr_probe()
2155 dev->gadget.ops = &gr_ops; in gr_probe()
2157 spin_lock_init(&dev->lock); in gr_probe()
2158 dev->regs = regs; in gr_probe()
2163 status = gr_read32(&dev->regs->status); in gr_probe()
2164 dev->nepi = ((status & GR_STATUS_NEPI_MASK) >> GR_STATUS_NEPI_POS) + 1; in gr_probe()
2165 dev->nepo = ((status & GR_STATUS_NEPO_MASK) >> GR_STATUS_NEPO_POS) + 1; in gr_probe()
2168 dev_err(dev->dev, "Slave mode cores are not supported\n"); in gr_probe()
2169 return -ENODEV; in gr_probe()
2172 /* --- Effects of the following calls might need explicit cleanup --- */ in gr_probe()
2175 dev->desc_pool = dma_pool_create("desc_pool", dev->dev, in gr_probe()
2177 if (!dev->desc_pool) { in gr_probe()
2178 dev_err(dev->dev, "Could not allocate DMA pool"); in gr_probe()
2179 return -ENOMEM; in gr_probe()
2183 retval = usb_add_gadget_udc(dev->dev, &dev->gadget); in gr_probe()
2185 dev_err(dev->dev, "Could not add gadget udc"); in gr_probe()
2188 dev->added = 1; in gr_probe()
2190 spin_lock(&dev->lock); in gr_probe()
2194 spin_unlock(&dev->lock); in gr_probe()
2201 spin_unlock(&dev->lock); in gr_probe()
2205 retval = gr_request_irq(dev, dev->irq); in gr_probe()
2207 dev_err(dev->dev, "Failed to request irq %d\n", dev->irq); in gr_probe()
2211 if (dev->irqi) { in gr_probe()
2212 retval = gr_request_irq(dev, dev->irqi); in gr_probe()
2214 dev_err(dev->dev, "Failed to request irqi %d\n", in gr_probe()
2215 dev->irqi); in gr_probe()
2218 retval = gr_request_irq(dev, dev->irqo); in gr_probe()
2220 dev_err(dev->dev, "Failed to request irqo %d\n", in gr_probe()
2221 dev->irqo); in gr_probe()
2226 if (dev->irqi) in gr_probe()
2227 dev_info(dev->dev, "regs: %p, irqs %d, %d, %d\n", dev->regs, in gr_probe()
2228 dev->irq, dev->irqi, dev->irqo); in gr_probe()
2230 dev_info(dev->dev, "regs: %p, irq %d\n", dev->regs, dev->irq); in gr_probe()