mod_gadget.c 28 KB

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