mod_gadget.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Renesas USB driver
  4. *
  5. * Copyright (C) 2011 Renesas Solutions Corp.
  6. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/usb/ch9.h>
  14. #include <linux/usb/gadget.h>
  15. #include <linux/usb/otg.h>
  16. #include "common.h"
  17. /*
  18. * struct
  19. */
  20. struct usbhsg_request {
  21. struct usb_request req;
  22. struct usbhs_pkt pkt;
  23. };
  24. #define EP_NAME_SIZE 8
  25. struct usbhsg_gpriv;
  26. struct usbhsg_uep {
  27. struct usb_ep ep;
  28. struct usbhs_pipe *pipe;
  29. spinlock_t lock; /* protect the pipe */
  30. char ep_name[EP_NAME_SIZE];
  31. struct usbhsg_gpriv *gpriv;
  32. };
  33. struct usbhsg_gpriv {
  34. struct usb_gadget gadget;
  35. struct usbhs_mod mod;
  36. struct usbhsg_uep *uep;
  37. int uep_size;
  38. struct usb_gadget_driver *driver;
  39. struct usb_phy *transceiver;
  40. bool vbus_active;
  41. u32 status;
  42. #define USBHSG_STATUS_STARTED (1 << 0)
  43. #define USBHSG_STATUS_REGISTERD (1 << 1)
  44. #define USBHSG_STATUS_WEDGE (1 << 2)
  45. #define USBHSG_STATUS_SELF_POWERED (1 << 3)
  46. #define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
  47. };
  48. struct usbhsg_recip_handle {
  49. char *name;
  50. int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  51. struct usb_ctrlrequest *ctrl);
  52. int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  53. struct usb_ctrlrequest *ctrl);
  54. int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  55. struct usb_ctrlrequest *ctrl);
  56. };
  57. /*
  58. * macro
  59. */
  60. #define usbhsg_priv_to_gpriv(priv) \
  61. container_of( \
  62. usbhs_mod_get(priv, USBHS_GADGET), \
  63. struct usbhsg_gpriv, mod)
  64. #define __usbhsg_for_each_uep(start, pos, g, i) \
  65. for ((i) = start; \
  66. ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
  67. (i)++)
  68. #define usbhsg_for_each_uep(pos, gpriv, i) \
  69. __usbhsg_for_each_uep(1, pos, gpriv, i)
  70. #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
  71. __usbhsg_for_each_uep(0, pos, gpriv, i)
  72. #define usbhsg_gadget_to_gpriv(g)\
  73. container_of(g, struct usbhsg_gpriv, gadget)
  74. #define usbhsg_req_to_ureq(r)\
  75. container_of(r, struct usbhsg_request, req)
  76. #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
  77. #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
  78. #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
  79. #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
  80. #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
  81. #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
  82. #define usbhsg_uep_to_pipe(u) ((u)->pipe)
  83. #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
  84. #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
  85. #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
  86. #define usbhsg_pkt_to_ureq(i) \
  87. container_of(i, struct usbhsg_request, pkt)
  88. #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
  89. /* status */
  90. #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
  91. #define usbhsg_status_set(gp, b) (gp->status |= b)
  92. #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
  93. #define usbhsg_status_has(gp, b) (gp->status & b)
  94. /*
  95. * queue push/pop
  96. */
  97. static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
  98. struct usbhsg_request *ureq,
  99. int status)
  100. {
  101. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  102. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  103. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  104. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  105. if (pipe)
  106. dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
  107. ureq->req.status = status;
  108. spin_unlock(usbhs_priv_to_lock(priv));
  109. usb_gadget_giveback_request(&uep->ep, &ureq->req);
  110. spin_lock(usbhs_priv_to_lock(priv));
  111. }
  112. static void usbhsg_queue_pop(struct usbhsg_uep *uep,
  113. struct usbhsg_request *ureq,
  114. int status)
  115. {
  116. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  117. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  118. unsigned long flags;
  119. usbhs_lock(priv, flags);
  120. __usbhsg_queue_pop(uep, ureq, status);
  121. usbhs_unlock(priv, flags);
  122. }
  123. static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
  124. {
  125. struct usbhs_pipe *pipe = pkt->pipe;
  126. struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
  127. struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
  128. unsigned long flags;
  129. ureq->req.actual = pkt->actual;
  130. usbhs_lock(priv, flags);
  131. if (uep)
  132. __usbhsg_queue_pop(uep, ureq, 0);
  133. usbhs_unlock(priv, flags);
  134. }
  135. static void usbhsg_queue_push(struct usbhsg_uep *uep,
  136. struct usbhsg_request *ureq)
  137. {
  138. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  139. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  140. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  141. struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
  142. struct usb_request *req = &ureq->req;
  143. req->actual = 0;
  144. req->status = -EINPROGRESS;
  145. usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
  146. req->buf, req->length, req->zero, -1);
  147. usbhs_pkt_start(pipe);
  148. dev_dbg(dev, "pipe %d : queue push (%d)\n",
  149. usbhs_pipe_number(pipe),
  150. req->length);
  151. }
  152. /*
  153. * dma map/unmap
  154. */
  155. static int usbhsg_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
  156. int map)
  157. {
  158. struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
  159. struct usb_request *req = &ureq->req;
  160. struct usbhs_pipe *pipe = pkt->pipe;
  161. enum dma_data_direction dir;
  162. int ret = 0;
  163. dir = usbhs_pipe_is_dir_host(pipe);
  164. if (map) {
  165. /* it can not use scatter/gather */
  166. WARN_ON(req->num_sgs);
  167. ret = usb_gadget_map_request_by_dev(dma_dev, req, dir);
  168. if (ret < 0)
  169. return ret;
  170. pkt->dma = req->dma;
  171. } else {
  172. usb_gadget_unmap_request_by_dev(dma_dev, req, dir);
  173. }
  174. return ret;
  175. }
  176. /*
  177. * USB_TYPE_STANDARD / clear feature functions
  178. */
  179. static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
  180. struct usbhsg_uep *uep,
  181. struct usb_ctrlrequest *ctrl)
  182. {
  183. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  184. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  185. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  186. usbhs_dcp_control_transfer_done(pipe);
  187. return 0;
  188. }
  189. static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
  190. struct usbhsg_uep *uep,
  191. struct usb_ctrlrequest *ctrl)
  192. {
  193. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  194. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  195. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
  196. usbhs_pipe_disable(pipe);
  197. usbhs_pipe_sequence_data0(pipe);
  198. usbhs_pipe_enable(pipe);
  199. }
  200. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  201. usbhs_pkt_start(pipe);
  202. return 0;
  203. }
  204. static struct usbhsg_recip_handle req_clear_feature = {
  205. .name = "clear feature",
  206. .device = usbhsg_recip_handler_std_control_done,
  207. .interface = usbhsg_recip_handler_std_control_done,
  208. .endpoint = usbhsg_recip_handler_std_clear_endpoint,
  209. };
  210. /*
  211. * USB_TYPE_STANDARD / set feature functions
  212. */
  213. static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
  214. struct usbhsg_uep *uep,
  215. struct usb_ctrlrequest *ctrl)
  216. {
  217. switch (le16_to_cpu(ctrl->wValue)) {
  218. case USB_DEVICE_TEST_MODE:
  219. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  220. udelay(100);
  221. usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
  222. break;
  223. default:
  224. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  225. break;
  226. }
  227. return 0;
  228. }
  229. static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
  230. struct usbhsg_uep *uep,
  231. struct usb_ctrlrequest *ctrl)
  232. {
  233. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  234. usbhs_pipe_stall(pipe);
  235. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  236. return 0;
  237. }
  238. static struct usbhsg_recip_handle req_set_feature = {
  239. .name = "set feature",
  240. .device = usbhsg_recip_handler_std_set_device,
  241. .interface = usbhsg_recip_handler_std_control_done,
  242. .endpoint = usbhsg_recip_handler_std_set_endpoint,
  243. };
  244. /*
  245. * USB_TYPE_STANDARD / get status functions
  246. */
  247. static void __usbhsg_recip_send_complete(struct usb_ep *ep,
  248. struct usb_request *req)
  249. {
  250. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  251. /* free allocated recip-buffer/usb_request */
  252. kfree(ureq->pkt.buf);
  253. usb_ep_free_request(ep, req);
  254. }
  255. static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
  256. unsigned short status)
  257. {
  258. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  259. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  260. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  261. struct usb_request *req;
  262. unsigned short *buf;
  263. /* alloc new usb_request for recip */
  264. req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
  265. if (!req) {
  266. dev_err(dev, "recip request allocation fail\n");
  267. return;
  268. }
  269. /* alloc recip data buffer */
  270. buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  271. if (!buf) {
  272. usb_ep_free_request(&dcp->ep, req);
  273. return;
  274. }
  275. /* recip data is status */
  276. *buf = cpu_to_le16(status);
  277. /* allocated usb_request/buffer will be freed */
  278. req->complete = __usbhsg_recip_send_complete;
  279. req->buf = buf;
  280. req->length = sizeof(*buf);
  281. req->zero = 0;
  282. /* push packet */
  283. pipe->handler = &usbhs_fifo_pio_push_handler;
  284. usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
  285. }
  286. static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
  287. struct usbhsg_uep *uep,
  288. struct usb_ctrlrequest *ctrl)
  289. {
  290. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  291. unsigned short status = 0;
  292. if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
  293. status = 1 << USB_DEVICE_SELF_POWERED;
  294. __usbhsg_recip_send_status(gpriv, status);
  295. return 0;
  296. }
  297. static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
  298. struct usbhsg_uep *uep,
  299. struct usb_ctrlrequest *ctrl)
  300. {
  301. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  302. unsigned short status = 0;
  303. __usbhsg_recip_send_status(gpriv, status);
  304. return 0;
  305. }
  306. static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
  307. struct usbhsg_uep *uep,
  308. struct usb_ctrlrequest *ctrl)
  309. {
  310. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  311. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  312. unsigned short status = 0;
  313. if (usbhs_pipe_is_stall(pipe))
  314. status = 1 << USB_ENDPOINT_HALT;
  315. __usbhsg_recip_send_status(gpriv, status);
  316. return 0;
  317. }
  318. static struct usbhsg_recip_handle req_get_status = {
  319. .name = "get status",
  320. .device = usbhsg_recip_handler_std_get_device,
  321. .interface = usbhsg_recip_handler_std_get_interface,
  322. .endpoint = usbhsg_recip_handler_std_get_endpoint,
  323. };
  324. /*
  325. * USB_TYPE handler
  326. */
  327. static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
  328. struct usbhsg_recip_handle *handler,
  329. struct usb_ctrlrequest *ctrl)
  330. {
  331. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  332. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  333. struct usbhsg_uep *uep;
  334. struct usbhs_pipe *pipe;
  335. int recip = ctrl->bRequestType & USB_RECIP_MASK;
  336. int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  337. int ret = 0;
  338. int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  339. struct usb_ctrlrequest *ctrl);
  340. char *msg;
  341. uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
  342. pipe = usbhsg_uep_to_pipe(uep);
  343. if (!pipe) {
  344. dev_err(dev, "wrong recip request\n");
  345. return -EINVAL;
  346. }
  347. switch (recip) {
  348. case USB_RECIP_DEVICE:
  349. msg = "DEVICE";
  350. func = handler->device;
  351. break;
  352. case USB_RECIP_INTERFACE:
  353. msg = "INTERFACE";
  354. func = handler->interface;
  355. break;
  356. case USB_RECIP_ENDPOINT:
  357. msg = "ENDPOINT";
  358. func = handler->endpoint;
  359. break;
  360. default:
  361. dev_warn(dev, "unsupported RECIP(%d)\n", recip);
  362. func = NULL;
  363. ret = -EINVAL;
  364. }
  365. if (func) {
  366. dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
  367. ret = func(priv, uep, ctrl);
  368. }
  369. return ret;
  370. }
  371. /*
  372. * irq functions
  373. *
  374. * it will be called from usbhs_interrupt
  375. */
  376. static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
  377. struct usbhs_irq_state *irq_state)
  378. {
  379. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  380. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  381. gpriv->gadget.speed = usbhs_bus_get_speed(priv);
  382. dev_dbg(dev, "state = %x : speed : %d\n",
  383. usbhs_status_get_device_state(irq_state),
  384. gpriv->gadget.speed);
  385. return 0;
  386. }
  387. static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
  388. struct usbhs_irq_state *irq_state)
  389. {
  390. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  391. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  392. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  393. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  394. struct usb_ctrlrequest ctrl;
  395. struct usbhsg_recip_handle *recip_handler = NULL;
  396. int stage = usbhs_status_get_ctrl_stage(irq_state);
  397. int ret = 0;
  398. dev_dbg(dev, "stage = %d\n", stage);
  399. /*
  400. * see Manual
  401. *
  402. * "Operation"
  403. * - "Interrupt Function"
  404. * - "Control Transfer Stage Transition Interrupt"
  405. * - Fig. "Control Transfer Stage Transitions"
  406. */
  407. switch (stage) {
  408. case READ_DATA_STAGE:
  409. pipe->handler = &usbhs_fifo_pio_push_handler;
  410. break;
  411. case WRITE_DATA_STAGE:
  412. pipe->handler = &usbhs_fifo_pio_pop_handler;
  413. break;
  414. case NODATA_STATUS_STAGE:
  415. pipe->handler = &usbhs_ctrl_stage_end_handler;
  416. break;
  417. case READ_STATUS_STAGE:
  418. case WRITE_STATUS_STAGE:
  419. usbhs_dcp_control_transfer_done(pipe);
  420. default:
  421. return ret;
  422. }
  423. /*
  424. * get usb request
  425. */
  426. usbhs_usbreq_get_val(priv, &ctrl);
  427. switch (ctrl.bRequestType & USB_TYPE_MASK) {
  428. case USB_TYPE_STANDARD:
  429. switch (ctrl.bRequest) {
  430. case USB_REQ_CLEAR_FEATURE:
  431. recip_handler = &req_clear_feature;
  432. break;
  433. case USB_REQ_SET_FEATURE:
  434. recip_handler = &req_set_feature;
  435. break;
  436. case USB_REQ_GET_STATUS:
  437. recip_handler = &req_get_status;
  438. break;
  439. }
  440. }
  441. /*
  442. * setup stage / run recip
  443. */
  444. if (recip_handler)
  445. ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
  446. else
  447. ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
  448. if (ret < 0)
  449. usbhs_pipe_stall(pipe);
  450. return ret;
  451. }
  452. /*
  453. *
  454. * usb_dcp_ops
  455. *
  456. */
  457. static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
  458. {
  459. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  460. struct usbhs_pkt *pkt;
  461. while (1) {
  462. pkt = usbhs_pkt_pop(pipe, NULL);
  463. if (!pkt)
  464. break;
  465. usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ESHUTDOWN);
  466. }
  467. usbhs_pipe_disable(pipe);
  468. return 0;
  469. }
  470. /*
  471. *
  472. * usb_ep_ops
  473. *
  474. */
  475. static int usbhsg_ep_enable(struct usb_ep *ep,
  476. const struct usb_endpoint_descriptor *desc)
  477. {
  478. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  479. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  480. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  481. struct usbhs_pipe *pipe;
  482. int ret = -EIO;
  483. unsigned long flags;
  484. usbhs_lock(priv, flags);
  485. /*
  486. * if it already have pipe,
  487. * nothing to do
  488. */
  489. if (uep->pipe) {
  490. usbhs_pipe_clear(uep->pipe);
  491. usbhs_pipe_sequence_data0(uep->pipe);
  492. ret = 0;
  493. goto usbhsg_ep_enable_end;
  494. }
  495. pipe = usbhs_pipe_malloc(priv,
  496. usb_endpoint_type(desc),
  497. usb_endpoint_dir_in(desc));
  498. if (pipe) {
  499. uep->pipe = pipe;
  500. pipe->mod_private = uep;
  501. /* set epnum / maxp */
  502. usbhs_pipe_config_update(pipe, 0,
  503. usb_endpoint_num(desc),
  504. usb_endpoint_maxp(desc));
  505. /*
  506. * usbhs_fifo_dma_push/pop_handler try to
  507. * use dmaengine if possible.
  508. * It will use pio handler if impossible.
  509. */
  510. if (usb_endpoint_dir_in(desc)) {
  511. pipe->handler = &usbhs_fifo_dma_push_handler;
  512. } else {
  513. pipe->handler = &usbhs_fifo_dma_pop_handler;
  514. usbhs_xxxsts_clear(priv, BRDYSTS,
  515. usbhs_pipe_number(pipe));
  516. }
  517. ret = 0;
  518. }
  519. usbhsg_ep_enable_end:
  520. usbhs_unlock(priv, flags);
  521. return ret;
  522. }
  523. static int usbhsg_ep_disable(struct usb_ep *ep)
  524. {
  525. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  526. struct usbhs_pipe *pipe;
  527. unsigned long flags;
  528. spin_lock_irqsave(&uep->lock, flags);
  529. pipe = usbhsg_uep_to_pipe(uep);
  530. if (!pipe)
  531. goto out;
  532. usbhsg_pipe_disable(uep);
  533. usbhs_pipe_free(pipe);
  534. uep->pipe->mod_private = NULL;
  535. uep->pipe = NULL;
  536. out:
  537. spin_unlock_irqrestore(&uep->lock, flags);
  538. return 0;
  539. }
  540. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  541. gfp_t gfp_flags)
  542. {
  543. struct usbhsg_request *ureq;
  544. ureq = kzalloc(sizeof *ureq, gfp_flags);
  545. if (!ureq)
  546. return NULL;
  547. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  548. return &ureq->req;
  549. }
  550. static void usbhsg_ep_free_request(struct usb_ep *ep,
  551. struct usb_request *req)
  552. {
  553. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  554. WARN_ON(!list_empty(&ureq->pkt.node));
  555. kfree(ureq);
  556. }
  557. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  558. gfp_t gfp_flags)
  559. {
  560. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  561. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  562. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  563. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  564. /* param check */
  565. if (usbhsg_is_not_connected(gpriv) ||
  566. unlikely(!gpriv->driver) ||
  567. unlikely(!pipe))
  568. return -ESHUTDOWN;
  569. usbhsg_queue_push(uep, ureq);
  570. return 0;
  571. }
  572. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  573. {
  574. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  575. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  576. struct usbhs_pipe *pipe;
  577. unsigned long flags;
  578. spin_lock_irqsave(&uep->lock, flags);
  579. pipe = usbhsg_uep_to_pipe(uep);
  580. if (pipe)
  581. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  582. /*
  583. * To dequeue a request, this driver should call the usbhsg_queue_pop()
  584. * even if the pipe is NULL.
  585. */
  586. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  587. spin_unlock_irqrestore(&uep->lock, flags);
  588. return 0;
  589. }
  590. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  591. {
  592. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  593. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  594. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  595. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  596. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  597. unsigned long flags;
  598. usbhsg_pipe_disable(uep);
  599. dev_dbg(dev, "set halt %d (pipe %d)\n",
  600. halt, usbhs_pipe_number(pipe));
  601. /******************** spin lock ********************/
  602. usbhs_lock(priv, flags);
  603. if (halt)
  604. usbhs_pipe_stall(pipe);
  605. else
  606. usbhs_pipe_disable(pipe);
  607. if (halt && wedge)
  608. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  609. else
  610. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  611. usbhs_unlock(priv, flags);
  612. /******************** spin unlock ******************/
  613. return 0;
  614. }
  615. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  616. {
  617. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  618. }
  619. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  620. {
  621. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  622. }
  623. static const struct usb_ep_ops usbhsg_ep_ops = {
  624. .enable = usbhsg_ep_enable,
  625. .disable = usbhsg_ep_disable,
  626. .alloc_request = usbhsg_ep_alloc_request,
  627. .free_request = usbhsg_ep_free_request,
  628. .queue = usbhsg_ep_queue,
  629. .dequeue = usbhsg_ep_dequeue,
  630. .set_halt = usbhsg_ep_set_halt,
  631. .set_wedge = usbhsg_ep_set_wedge,
  632. };
  633. /*
  634. * pullup control
  635. */
  636. static int usbhsg_can_pullup(struct usbhs_priv *priv)
  637. {
  638. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  639. return gpriv->driver &&
  640. usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  641. }
  642. static void usbhsg_update_pullup(struct usbhs_priv *priv)
  643. {
  644. if (usbhsg_can_pullup(priv))
  645. usbhs_sys_function_pullup(priv, 1);
  646. else
  647. usbhs_sys_function_pullup(priv, 0);
  648. }
  649. /*
  650. * usb module start/end
  651. */
  652. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  653. {
  654. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  655. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  656. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  657. struct device *dev = usbhs_priv_to_dev(priv);
  658. unsigned long flags;
  659. int ret = 0;
  660. /******************** spin lock ********************/
  661. usbhs_lock(priv, flags);
  662. usbhsg_status_set(gpriv, status);
  663. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  664. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  665. ret = -1; /* not ready */
  666. usbhs_unlock(priv, flags);
  667. /******************** spin unlock ********************/
  668. if (ret < 0)
  669. return 0; /* not ready is not error */
  670. /*
  671. * enable interrupt and systems if ready
  672. */
  673. dev_dbg(dev, "start gadget\n");
  674. /*
  675. * pipe initialize and enable DCP
  676. */
  677. usbhs_fifo_init(priv);
  678. usbhs_pipe_init(priv,
  679. usbhsg_dma_map_ctrl);
  680. /* dcp init instead of usbhsg_ep_enable() */
  681. dcp->pipe = usbhs_dcp_malloc(priv);
  682. dcp->pipe->mod_private = dcp;
  683. usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
  684. /*
  685. * system config enble
  686. * - HI speed
  687. * - function
  688. * - usb module
  689. */
  690. usbhs_sys_function_ctrl(priv, 1);
  691. usbhsg_update_pullup(priv);
  692. /*
  693. * enable irq callback
  694. */
  695. mod->irq_dev_state = usbhsg_irq_dev_state;
  696. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  697. usbhs_irq_callback_update(priv, mod);
  698. return 0;
  699. }
  700. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  701. {
  702. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  703. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  704. struct usbhsg_uep *uep;
  705. struct device *dev = usbhs_priv_to_dev(priv);
  706. unsigned long flags;
  707. int ret = 0, i;
  708. /******************** spin lock ********************/
  709. usbhs_lock(priv, flags);
  710. usbhsg_status_clr(gpriv, status);
  711. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  712. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  713. ret = -1; /* already done */
  714. usbhs_unlock(priv, flags);
  715. /******************** spin unlock ********************/
  716. if (ret < 0)
  717. return 0; /* already done is not error */
  718. /*
  719. * disable interrupt and systems if 1st try
  720. */
  721. usbhs_fifo_quit(priv);
  722. /* disable all irq */
  723. mod->irq_dev_state = NULL;
  724. mod->irq_ctrl_stage = NULL;
  725. usbhs_irq_callback_update(priv, mod);
  726. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  727. /* disable sys */
  728. usbhs_sys_set_test_mode(priv, 0);
  729. usbhs_sys_function_ctrl(priv, 0);
  730. /* disable all eps */
  731. usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
  732. usbhsg_ep_disable(&uep->ep);
  733. dev_dbg(dev, "stop gadget\n");
  734. return 0;
  735. }
  736. /*
  737. * VBUS provided by the PHY
  738. */
  739. static int usbhsm_phy_get_vbus(struct platform_device *pdev)
  740. {
  741. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  742. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  743. return gpriv->vbus_active;
  744. }
  745. static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
  746. {
  747. struct usbhs_mod_info *info = &priv->mod_info;
  748. info->irq_vbus = NULL;
  749. priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
  750. usbhs_irq_callback_update(priv, NULL);
  751. }
  752. /*
  753. *
  754. * linux usb function
  755. *
  756. */
  757. static int usbhsg_gadget_start(struct usb_gadget *gadget,
  758. struct usb_gadget_driver *driver)
  759. {
  760. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  761. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  762. struct device *dev = usbhs_priv_to_dev(priv);
  763. int ret;
  764. if (!driver ||
  765. !driver->setup ||
  766. driver->max_speed < USB_SPEED_FULL)
  767. return -EINVAL;
  768. /* connect to bus through transceiver */
  769. if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
  770. ret = otg_set_peripheral(gpriv->transceiver->otg,
  771. &gpriv->gadget);
  772. if (ret) {
  773. dev_err(dev, "%s: can't bind to transceiver\n",
  774. gpriv->gadget.name);
  775. return ret;
  776. }
  777. /* get vbus using phy versions */
  778. usbhs_mod_phy_mode(priv);
  779. }
  780. /* first hook up the driver ... */
  781. gpriv->driver = driver;
  782. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  783. }
  784. static int usbhsg_gadget_stop(struct usb_gadget *gadget)
  785. {
  786. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  787. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  788. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  789. if (!IS_ERR_OR_NULL(gpriv->transceiver))
  790. otg_set_peripheral(gpriv->transceiver->otg, NULL);
  791. gpriv->driver = NULL;
  792. return 0;
  793. }
  794. /*
  795. * usb gadget ops
  796. */
  797. static int usbhsg_get_frame(struct usb_gadget *gadget)
  798. {
  799. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  800. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  801. return usbhs_frame_get_num(priv);
  802. }
  803. static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
  804. {
  805. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  806. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  807. unsigned long flags;
  808. usbhs_lock(priv, flags);
  809. if (is_on)
  810. usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  811. else
  812. usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  813. usbhsg_update_pullup(priv);
  814. usbhs_unlock(priv, flags);
  815. return 0;
  816. }
  817. static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
  818. {
  819. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  820. if (is_self)
  821. usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
  822. else
  823. usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
  824. gadget->is_selfpowered = (is_self != 0);
  825. return 0;
  826. }
  827. static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
  828. {
  829. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  830. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  831. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  832. gpriv->vbus_active = !!is_active;
  833. renesas_usbhs_call_notify_hotplug(pdev);
  834. return 0;
  835. }
  836. static const struct usb_gadget_ops usbhsg_gadget_ops = {
  837. .get_frame = usbhsg_get_frame,
  838. .set_selfpowered = usbhsg_set_selfpowered,
  839. .udc_start = usbhsg_gadget_start,
  840. .udc_stop = usbhsg_gadget_stop,
  841. .pullup = usbhsg_pullup,
  842. .vbus_session = usbhsg_vbus_session,
  843. };
  844. static int usbhsg_start(struct usbhs_priv *priv)
  845. {
  846. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  847. }
  848. static int usbhsg_stop(struct usbhs_priv *priv)
  849. {
  850. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  851. /* cable disconnect */
  852. if (gpriv->driver &&
  853. gpriv->driver->disconnect)
  854. gpriv->driver->disconnect(&gpriv->gadget);
  855. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  856. }
  857. int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  858. {
  859. struct usbhsg_gpriv *gpriv;
  860. struct usbhsg_uep *uep;
  861. struct device *dev = usbhs_priv_to_dev(priv);
  862. struct renesas_usbhs_driver_pipe_config *pipe_configs =
  863. usbhs_get_dparam(priv, pipe_configs);
  864. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  865. int i;
  866. int ret;
  867. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  868. if (!gpriv)
  869. return -ENOMEM;
  870. uep = kcalloc(pipe_size, sizeof(struct usbhsg_uep), GFP_KERNEL);
  871. if (!uep) {
  872. ret = -ENOMEM;
  873. goto usbhs_mod_gadget_probe_err_gpriv;
  874. }
  875. gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
  876. dev_info(dev, "%stransceiver found\n",
  877. !IS_ERR(gpriv->transceiver) ? "" : "no ");
  878. /*
  879. * CAUTION
  880. *
  881. * There is no guarantee that it is possible to access usb module here.
  882. * Don't accesses to it.
  883. * The accesse will be enable after "usbhsg_start"
  884. */
  885. /*
  886. * register itself
  887. */
  888. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  889. /* init gpriv */
  890. gpriv->mod.name = "gadget";
  891. gpriv->mod.start = usbhsg_start;
  892. gpriv->mod.stop = usbhsg_stop;
  893. gpriv->uep = uep;
  894. gpriv->uep_size = pipe_size;
  895. usbhsg_status_init(gpriv);
  896. /*
  897. * init gadget
  898. */
  899. gpriv->gadget.dev.parent = dev;
  900. gpriv->gadget.name = "renesas_usbhs_udc";
  901. gpriv->gadget.ops = &usbhsg_gadget_ops;
  902. gpriv->gadget.max_speed = USB_SPEED_HIGH;
  903. gpriv->gadget.quirk_avoids_skb_reserve = usbhs_get_dparam(priv,
  904. has_usb_dmac);
  905. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  906. /*
  907. * init usb_ep
  908. */
  909. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  910. uep->gpriv = gpriv;
  911. uep->pipe = NULL;
  912. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  913. uep->ep.name = uep->ep_name;
  914. uep->ep.ops = &usbhsg_ep_ops;
  915. INIT_LIST_HEAD(&uep->ep.ep_list);
  916. spin_lock_init(&uep->lock);
  917. /* init DCP */
  918. if (usbhsg_is_dcp(uep)) {
  919. gpriv->gadget.ep0 = &uep->ep;
  920. usb_ep_set_maxpacket_limit(&uep->ep, 64);
  921. uep->ep.caps.type_control = true;
  922. } else {
  923. /* init normal pipe */
  924. if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
  925. uep->ep.caps.type_iso = true;
  926. if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
  927. uep->ep.caps.type_bulk = true;
  928. if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
  929. uep->ep.caps.type_int = true;
  930. usb_ep_set_maxpacket_limit(&uep->ep,
  931. pipe_configs[i].bufsize);
  932. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  933. }
  934. uep->ep.caps.dir_in = true;
  935. uep->ep.caps.dir_out = true;
  936. }
  937. ret = usb_add_gadget_udc(dev, &gpriv->gadget);
  938. if (ret)
  939. goto err_add_udc;
  940. dev_info(dev, "gadget probed\n");
  941. return 0;
  942. err_add_udc:
  943. kfree(gpriv->uep);
  944. usbhs_mod_gadget_probe_err_gpriv:
  945. kfree(gpriv);
  946. return ret;
  947. }
  948. void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  949. {
  950. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  951. usb_del_gadget_udc(&gpriv->gadget);
  952. kfree(gpriv->uep);
  953. kfree(gpriv);
  954. }