// SPDX-License-Identifier: GPL-2.0+ /* * USB Peripheral Controller driver for Aeroflex Gaisler GRUSBDC. * * 2013 (c) Aeroflex Gaisler AB * * This driver supports GRUSBDC USB Device Controller cores available in the * GRLIB VHDL IP core library. * * Full documentation of the GRUSBDC core can be found here: * https://www.gaisler.com/products/grlib/grip.pdf * * Contributors: * - Andreas Larsson <[email protected]> * - Marko Isomaki */ /* * A GRUSBDC core can have up to 16 IN endpoints and 16 OUT endpoints each * individually configurable to any of the four USB transfer types. This driver * only supports cores in DMA mode. */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/errno.h> #include <linux/list.h> #include <linux/interrupt.h> #include <linux/device.h> #include <linux/usb.h> #include <linux/usb/ch9.h> #include <linux/usb/gadget.h> #include <linux/dma-mapping.h> #include <linux/dmapool.h> #include <linux/debugfs.h> #include <linux/seq_file.h> #include <linux/of.h> #include <asm/byteorder.h> #include "gr_udc.h" #define DRIVER_NAME … #define DRIVER_DESC … static const char driver_name[] = …; #define gr_read32(x) … #define gr_write32(x, v) … /* USB speed and corresponding string calculated from status register value */ #define GR_SPEED(status) … #define GR_SPEED_STR(status) … /* Size of hardware buffer calculated from epctrl register value */ #define GR_BUFFER_SIZE(epctrl) … /* ---------------------------------------------------------------------- */ /* Debug printout functionality */ static const char * const gr_modestring[] = …; static const char *gr_ep0state_string(enum gr_ep0state state) { … } #ifdef VERBOSE_DEBUG static void gr_dbgprint_request(const char *str, struct gr_ep *ep, struct gr_request *req) { … } static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request, u16 value, u16 index, u16 length) { … } #else /* !VERBOSE_DEBUG */ static void gr_dbgprint_request(const char *str, struct gr_ep *ep, struct gr_request *req) {} static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request, u16 value, u16 index, u16 length) {} #endif /* VERBOSE_DEBUG */ /* ---------------------------------------------------------------------- */ /* Debugfs functionality */ #ifdef CONFIG_USB_GADGET_DEBUG_FS static void gr_seq_ep_show(struct seq_file *seq, struct gr_ep *ep) { … } static int gr_dfs_show(struct seq_file *seq, void *v) { … } DEFINE_SHOW_ATTRIBUTE(…); static void gr_dfs_create(struct gr_udc *dev) { … } static void gr_dfs_delete(struct gr_udc *dev) { … } #else /* !CONFIG_USB_GADGET_DEBUG_FS */ static void gr_dfs_create(struct gr_udc *dev) {} static void gr_dfs_delete(struct gr_udc *dev) {} #endif /* CONFIG_USB_GADGET_DEBUG_FS */ /* ---------------------------------------------------------------------- */ /* DMA and request handling */ /* Allocates a new struct gr_dma_desc, sets paddr and zeroes the rest */ static struct gr_dma_desc *gr_alloc_dma_desc(struct gr_ep *ep, gfp_t gfp_flags) { … } static inline void gr_free_dma_desc(struct gr_udc *dev, struct gr_dma_desc *desc) { … } /* Frees the chain of struct gr_dma_desc for the given request */ static void gr_free_dma_desc_chain(struct gr_udc *dev, struct gr_request *req) { … } static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req); /* * Frees allocated resources and calls the appropriate completion function/setup * package handler for a finished request. * * Must be called with dev->lock held and irqs disabled. */ static void gr_finish_request(struct gr_ep *ep, struct gr_request *req, int status) __releases(&dev->lock) __acquires(&dev->lock) { … } static struct usb_request *gr_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) { … } /* * Starts DMA for endpoint ep if there are requests in the queue. * * Must be called with dev->lock held and with !ep->stopped. */ static void gr_start_dma(struct gr_ep *ep) { … } /* * Finishes the first request in the ep's queue and, if available, starts the * next request in queue. * * Must be called with dev->lock held, irqs disabled and with !ep->stopped. */ static void gr_dma_advance(struct gr_ep *ep, int status) { … } /* * Abort DMA for an endpoint. Sets the abort DMA bit which causes an ongoing DMA * transfer to be canceled and clears GR_DMACTRL_DA. * * Must be called with dev->lock held. */ static void gr_abort_dma(struct gr_ep *ep) { … } /* * Allocates and sets up a struct gr_dma_desc and putting it on the descriptor * chain. * * Size is not used for OUT endpoints. Hardware can not be instructed to handle * smaller buffer than MAXPL in the OUT direction. */ static int gr_add_dma_desc(struct gr_ep *ep, struct gr_request *req, dma_addr_t data, unsigned size, gfp_t gfp_flags) { … } /* * Sets up a chain of struct gr_dma_descriptors pointing to buffers that * together covers req->req.length bytes of the buffer at DMA address * req->req.dma for the OUT direction. * * The first descriptor in the chain is enabled, the rest disabled. The * interrupt handler will later enable them one by one when needed so we can * find out when the transfer is finished. For OUT endpoints, all descriptors * therefore generate interrutps. */ static int gr_setup_out_desc_list(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags) { … } /* * Sets up a chain of struct gr_dma_descriptors pointing to buffers that * together covers req->req.length bytes of the buffer at DMA address * req->req.dma for the IN direction. * * When more data is provided than the maximum payload size, the hardware splits * this up into several payloads automatically. Moreover, ep->bytes_per_buffer * is always set to a multiple of the maximum payload (restricted to the valid * number of maximum payloads during high bandwidth isochronous or interrupt * transfers) * * All descriptors are enabled from the beginning and we only generate an * interrupt for the last one indicating that the entire request has been pushed * to hardware. */ static int gr_setup_in_desc_list(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags) { … } /* Must be called with dev->lock held */ static int gr_queue(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags) { … } /* * Queue a request from within the driver. * * Must be called with dev->lock held. */ static inline int gr_queue_int(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags) { … } /* ---------------------------------------------------------------------- */ /* General helper functions */ /* * Dequeue ALL requests. * * Must be called with dev->lock held and irqs disabled. */ static void gr_ep_nuke(struct gr_ep *ep) { … } /* * Reset the hardware state of this endpoint. * * Must be called with dev->lock held. */ static void gr_ep_reset(struct gr_ep *ep) { … } /* * Generate STALL on ep0in/out. * * Must be called with dev->lock held. */ static void gr_control_stall(struct gr_udc *dev) { … } /* * Halts, halts and wedges, or clears halt for an endpoint. * * Must be called with dev->lock held. */ static int gr_ep_halt_wedge(struct gr_ep *ep, int halt, int wedge, int fromhost) { … } /* Must be called with dev->lock held */ static inline void gr_set_ep0state(struct gr_udc *dev, enum gr_ep0state value) { … } /* * Should only be called when endpoints can not generate interrupts. * * Must be called with dev->lock held. */ static void gr_disable_interrupts_and_pullup(struct gr_udc *dev) { … } /* * Stop all device activity and disable data line pullup. * * Must be called with dev->lock held and irqs disabled. */ static void gr_stop_activity(struct gr_udc *dev) { … } /* ---------------------------------------------------------------------- */ /* ep0 setup packet handling */ static void gr_ep0_testmode_complete(struct usb_ep *_ep, struct usb_request *_req) { … } static void gr_ep0_dummy_complete(struct usb_ep *_ep, struct usb_request *_req) { … } /* * Queue a response on ep0in. * * Must be called with dev->lock held. */ static int gr_ep0_respond(struct gr_udc *dev, u8 *buf, int length, void (*complete)(struct usb_ep *ep, struct usb_request *req)) { … } /* * Queue a 2 byte response on ep0in. * * Must be called with dev->lock held. */ static inline int gr_ep0_respond_u16(struct gr_udc *dev, u16 response) { … } /* * Queue a ZLP response on ep0in. * * Must be called with dev->lock held. */ static inline int gr_ep0_respond_empty(struct gr_udc *dev) { … } /* * This is run when a SET_ADDRESS request is received. First writes * the new address to the control register which is updated internally * when the next IN packet is ACKED. * * Must be called with dev->lock held. */ static void gr_set_address(struct gr_udc *dev, u8 address) { … } /* * Returns negative for STALL, 0 for successful handling and positive for * delegation. * * Must be called with dev->lock held. */ static int gr_device_request(struct gr_udc *dev, u8 type, u8 request, u16 value, u16 index) { … } /* * Returns negative for STALL, 0 for successful handling and positive for * delegation. * * Must be called with dev->lock held. */ static int gr_interface_request(struct gr_udc *dev, u8 type, u8 request, u16 value, u16 index) { … } /* * Returns negative for STALL, 0 for successful handling and positive for * delegation. * * Must be called with dev->lock held. */ static int gr_endpoint_request(struct gr_udc *dev, u8 type, u8 request, u16 value, u16 index) { … } /* Must be called with dev->lock held */ static void gr_ep0out_requeue(struct gr_udc *dev) { … } /* * The main function dealing with setup requests on ep0. * * Must be called with dev->lock held and irqs disabled */ static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req) __releases(&dev->lock) __acquires(&dev->lock) { … } /* ---------------------------------------------------------------------- */ /* VBUS and USB reset handling */ /* Must be called with dev->lock held and irqs disabled */ static void gr_vbus_connected(struct gr_udc *dev, u32 status) { … } /* Must be called with dev->lock held */ static void gr_enable_vbus_detect(struct gr_udc *dev) { … } /* Must be called with dev->lock held and irqs disabled */ static void gr_vbus_disconnected(struct gr_udc *dev) { … } /* Must be called with dev->lock held and irqs disabled */ static void gr_udc_usbreset(struct gr_udc *dev, u32 status) { … } /* ---------------------------------------------------------------------- */ /* Irq handling */ /* * Handles interrupts from in endpoints. Returns whether something was handled. * * Must be called with dev->lock held, irqs disabled and with !ep->stopped. */ static int gr_handle_in_ep(struct gr_ep *ep) { … } /* * Handles interrupts from out endpoints. Returns whether something was handled. * * Must be called with dev->lock held, irqs disabled and with !ep->stopped. */ static int gr_handle_out_ep(struct gr_ep *ep) { … } /* * Handle state changes. Returns whether something was handled. * * Must be called with dev->lock held and irqs disabled. */ static int gr_handle_state_changes(struct gr_udc *dev) { … } /* Non-interrupt context irq handler */ static irqreturn_t gr_irq_handler(int irq, void *_dev) { … } /* Interrupt context irq handler */ static irqreturn_t gr_irq(int irq, void *_dev) { … } /* ---------------------------------------------------------------------- */ /* USB ep ops */ /* Enable endpoint. Not for ep0in and ep0out that are handled separately. */ static int gr_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) { … } /* Disable endpoint. Not for ep0in and ep0out that are handled separately. */ static int gr_ep_disable(struct usb_ep *_ep) { … } /* * Frees a request, but not any DMA buffers associated with it * (gr_finish_request should already have taken care of that). */ static void gr_free_request(struct usb_ep *_ep, struct usb_request *_req) { … } /* Queue a request from the gadget */ static int gr_queue_ext(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags) { … } /* Dequeue JUST ONE request */ static int gr_dequeue(struct usb_ep *_ep, struct usb_request *_req) { … } /* Helper for gr_set_halt and gr_set_wedge */ static int gr_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge) { … } /* Halt endpoint */ static int gr_set_halt(struct usb_ep *_ep, int halt) { … } /* Halt and wedge endpoint */ static int gr_set_wedge(struct usb_ep *_ep) { … } /* * Return the total number of bytes currently stored in the internal buffers of * the endpoint. */ static int gr_fifo_status(struct usb_ep *_ep) { … } /* Empty data from internal buffers of an endpoint. */ static void gr_fifo_flush(struct usb_ep *_ep) { … } static const struct usb_ep_ops gr_ep_ops = …; /* ---------------------------------------------------------------------- */ /* USB Gadget ops */ static int gr_get_frame(struct usb_gadget *_gadget) { … } static int gr_wakeup(struct usb_gadget *_gadget) { … } static int gr_pullup(struct usb_gadget *_gadget, int is_on) { … } static int gr_udc_start(struct usb_gadget *gadget, struct usb_gadget_driver *driver) { … } static int gr_udc_stop(struct usb_gadget *gadget) { … } static const struct usb_gadget_ops gr_ops = …; /* ---------------------------------------------------------------------- */ /* Module probe, removal and of-matching */ static const char * const onames[] = …; static const char * const inames[] = …; /* Must be called with dev->lock held */ static int gr_ep_init(struct gr_udc *dev, int num, int is_in, u32 maxplimit) { … } /* Must be called with dev->lock held */ static int gr_udc_init(struct gr_udc *dev) { … } static void gr_ep_remove(struct gr_udc *dev, int num, int is_in) { … } static void gr_remove(struct platform_device *pdev) { … } static int gr_request_irq(struct gr_udc *dev, int irq) { … } static int gr_probe(struct platform_device *pdev) { … } static const struct of_device_id gr_match[] = …; MODULE_DEVICE_TABLE(of, gr_match); static struct platform_driver gr_driver = …; module_platform_driver(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…); MODULE_LICENSE(…) …;