gadget.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * gadget.h - DesignWare USB3 DRD Gadget Header
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. * All rights reserved.
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. The names of the above-listed copyright holders may not be used
  20. * to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * ALTERNATIVELY, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2, as published by the Free
  25. * Software Foundation.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #ifndef __DRIVERS_USB_DWC3_GADGET_H
  40. #define __DRIVERS_USB_DWC3_GADGET_H
  41. #include <linux/list.h>
  42. #include <linux/usb/gadget.h>
  43. #include "io.h"
  44. struct dwc3;
  45. #define to_dwc3_ep(ep) (container_of(ep, struct dwc3_ep, endpoint))
  46. #define gadget_to_dwc(g) (container_of(g, struct dwc3, gadget))
  47. /**
  48. * struct dwc3_gadget_ep_depcfg_param1 - DEPCMDPAR0 for DEPCFG command
  49. * @interrupt_number: self-explanatory
  50. * @reserved7_5: set to zero
  51. * @xfer_complete_enable: event generated when transfer completed
  52. * @xfer_in_progress_enable: event generated when transfer in progress
  53. * @xfer_not_ready_enable: event generated when transfer not read
  54. * @fifo_error_enable: generates events when FIFO Underrun (IN eps)
  55. * or FIFO Overrun (OUT) eps
  56. * @reserved_12: set to zero
  57. * @stream_event_enable: event generated on stream
  58. * @reserved14_15: set to zero
  59. * @binterval_m1: bInterval minus 1
  60. * @stream_capable: this EP is capable of handling streams
  61. * @ep_number: self-explanatory
  62. * @bulk_based: Set to ‘1’ if this isochronous endpoint represents a bulk
  63. * data stream that ignores the relationship of bus time to the
  64. * intervals programmed in TRBs.
  65. * @fifo_based: Set to ‘1’ if this isochronous endpoint represents a
  66. * FIFO-based data stream where TRBs have fixed values and are never
  67. * written back by the core.
  68. */
  69. struct dwc3_gadget_ep_depcfg_param1 {
  70. u32 interrupt_number:5;
  71. u32 reserved7_5:3; /* set to zero */
  72. u32 xfer_complete_enable:1;
  73. u32 xfer_in_progress_enable:1;
  74. u32 xfer_not_ready_enable:1;
  75. u32 fifo_error_enable:1; /* IN-underrun, OUT-overrun */
  76. u32 reserved12:1; /* set to zero */
  77. u32 stream_event_enable:1;
  78. u32 reserved14_15:2;
  79. u32 binterval_m1:8; /* bInterval minus 1 */
  80. u32 stream_capable:1;
  81. u32 ep_number:5;
  82. u32 bulk_based:1;
  83. u32 fifo_based:1;
  84. } __packed;
  85. /**
  86. * struct dwc3_gadget_ep_depcfg_param0 - Parameter 0 for DEPCFG
  87. * @reserved0: set to zero
  88. * @ep_type: Endpoint Type (control, bulk, iso, interrupt)
  89. * @max_packet_size: max packet size in bytes
  90. * @reserved16_14: set to zero
  91. * @fifo_number: self-explanatory
  92. * @burst_size: burst size minus 1
  93. * @data_sequence_number: Must be 0 when an endpoint is initially configured
  94. * May be non-zero when an endpoint is configured after a power transition
  95. * that requires a save/restore.
  96. * @ignore_sequence_number: Set to ‘1’ to avoid resetting the sequence
  97. * number. This setting is used by software to modify the DEPEVTEN
  98. * event enable bits without modifying other endpoint settings.
  99. */
  100. struct dwc3_gadget_ep_depcfg_param0 {
  101. u32 reserved0:1;
  102. u32 ep_type:2;
  103. u32 max_packet_size:11;
  104. u32 reserved16_14:3;
  105. u32 fifo_number:5;
  106. u32 burst_size:4;
  107. u32 data_sequence_number:5;
  108. u32 ignore_sequence_number:1;
  109. } __packed;
  110. /**
  111. * struct dwc3_gadget_ep_depxfercfg_param0 - Parameter 0 of DEPXFERCFG
  112. * @number_xfer_resources: Defines the number of Transfer Resources allocated
  113. * to this endpoint. This field must be set to 1.
  114. * @reserved16_31: set to zero;
  115. */
  116. struct dwc3_gadget_ep_depxfercfg_param0 {
  117. u32 number_xfer_resources:16;
  118. u32 reserved16_31:16;
  119. } __packed;
  120. /**
  121. * struct dwc3_gadget_ep_depstrtxfer_param1 - Parameter 1 of DEPSTRTXFER
  122. * @transfer_desc_addr_low: Indicates the lower 32 bits of the external
  123. * memory's start address for the transfer descriptor. Because TRBs
  124. * must be aligned to a 16-byte boundary, the lower 4 bits of this
  125. * address must be 0.
  126. */
  127. struct dwc3_gadget_ep_depstrtxfer_param1 {
  128. u32 transfer_desc_addr_low;
  129. } __packed;
  130. /**
  131. * struct dwc3_gadget_ep_depstrtxfer_param1 - Parameter 1 of DEPSTRTXFER
  132. * @transfer_desc_addr_high: Indicates the higher 32 bits of the external
  133. * memory’s start address for the transfer descriptor.
  134. */
  135. struct dwc3_gadget_ep_depstrtxfer_param0 {
  136. u32 transfer_desc_addr_high;
  137. } __packed;
  138. struct dwc3_gadget_ep_cmd_params {
  139. union {
  140. u32 raw;
  141. } param2;
  142. union {
  143. u32 raw;
  144. struct dwc3_gadget_ep_depcfg_param1 depcfg;
  145. struct dwc3_gadget_ep_depstrtxfer_param1 depstrtxfer;
  146. } param1;
  147. union {
  148. u32 raw;
  149. struct dwc3_gadget_ep_depcfg_param0 depcfg;
  150. struct dwc3_gadget_ep_depxfercfg_param0 depxfercfg;
  151. struct dwc3_gadget_ep_depstrtxfer_param0 depstrtxfer;
  152. } param0;
  153. } __packed;
  154. /* -------------------------------------------------------------------------- */
  155. struct dwc3_request {
  156. struct usb_request request;
  157. struct list_head list;
  158. struct dwc3_ep *dep;
  159. u8 epnum;
  160. struct dwc3_trb_hw *trb;
  161. dma_addr_t trb_dma;
  162. unsigned direction:1;
  163. unsigned mapped:1;
  164. unsigned queued:1;
  165. };
  166. #define to_dwc3_request(r) (container_of(r, struct dwc3_request, request))
  167. static inline struct dwc3_request *next_request(struct list_head *list)
  168. {
  169. if (list_empty(list))
  170. return NULL;
  171. return list_first_entry(list, struct dwc3_request, list);
  172. }
  173. static inline void dwc3_gadget_move_request_queued(struct dwc3_request *req)
  174. {
  175. struct dwc3_ep *dep = req->dep;
  176. req->queued = true;
  177. list_move_tail(&req->list, &dep->req_queued);
  178. }
  179. #if defined(CONFIG_USB_GADGET_DWC3) || defined(CONFIG_USB_GADGET_DWC3_MODULE)
  180. int dwc3_gadget_init(struct dwc3 *dwc);
  181. void dwc3_gadget_exit(struct dwc3 *dwc);
  182. #else
  183. static inline int dwc3_gadget_init(struct dwc3 *dwc) { return 0; }
  184. static inline void dwc3_gadget_exit(struct dwc3 *dwc) { }
  185. #endif
  186. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  187. int status);
  188. void dwc3_ep0_interrupt(struct dwc3 *dwc, const struct dwc3_event_depevt *event);
  189. void dwc3_ep0_out_start(struct dwc3 *dwc);
  190. int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
  191. gfp_t gfp_flags);
  192. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value);
  193. int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
  194. unsigned cmd, struct dwc3_gadget_ep_cmd_params *params);
  195. void dwc3_map_buffer_to_dma(struct dwc3_request *req);
  196. void dwc3_unmap_buffer_from_dma(struct dwc3_request *req);
  197. /**
  198. * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW
  199. * @dwc: DesignWare USB3 Pointer
  200. * @number: DWC endpoint number
  201. *
  202. * Caller should take care of locking
  203. */
  204. static inline u32 dwc3_gadget_ep_get_transfer_index(struct dwc3 *dwc, u8 number)
  205. {
  206. u32 res_id;
  207. res_id = dwc3_readl(dwc->regs, DWC3_DEPCMD(number));
  208. return DWC3_DEPCMD_GET_RSC_IDX(res_id);
  209. }
  210. /**
  211. * dwc3_gadget_event_string - returns event name
  212. * @event: the event code
  213. */
  214. static inline const char *dwc3_gadget_event_string(u8 event)
  215. {
  216. switch (event) {
  217. case DWC3_DEVICE_EVENT_DISCONNECT:
  218. return "Disconnect";
  219. case DWC3_DEVICE_EVENT_RESET:
  220. return "Reset";
  221. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  222. return "Connection Done";
  223. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  224. return "Link Status Change";
  225. case DWC3_DEVICE_EVENT_WAKEUP:
  226. return "WakeUp";
  227. case DWC3_DEVICE_EVENT_EOPF:
  228. return "End-Of-Frame";
  229. case DWC3_DEVICE_EVENT_SOF:
  230. return "Start-Of-Frame";
  231. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  232. return "Erratic Error";
  233. case DWC3_DEVICE_EVENT_CMD_CMPL:
  234. return "Command Complete";
  235. case DWC3_DEVICE_EVENT_OVERFLOW:
  236. return "Overflow";
  237. }
  238. return "UNKNOWN";
  239. }
  240. /**
  241. * dwc3_ep_event_string - returns event name
  242. * @event: then event code
  243. */
  244. static inline const char *dwc3_ep_event_string(u8 event)
  245. {
  246. switch (event) {
  247. case DWC3_DEPEVT_XFERCOMPLETE:
  248. return "Transfer Complete";
  249. case DWC3_DEPEVT_XFERINPROGRESS:
  250. return "Transfer In-Progress";
  251. case DWC3_DEPEVT_XFERNOTREADY:
  252. return "Transfer Not Ready";
  253. case DWC3_DEPEVT_RXTXFIFOEVT:
  254. return "FIFO";
  255. case DWC3_DEPEVT_STREAMEVT:
  256. return "Stream";
  257. case DWC3_DEPEVT_EPCMDCMPLT:
  258. return "Endpoint Command Complete";
  259. }
  260. return "UNKNOWN";
  261. }
  262. #endif /* __DRIVERS_USB_DWC3_GADGET_H */