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. * 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. int state = usbhs_status_get_device_state(irq_state);
  382. gpriv->gadget.speed = usbhs_bus_get_speed(priv);
  383. dev_dbg(dev, "state = %x : speed : %d\n", state, gpriv->gadget.speed);
  384. if (gpriv->gadget.speed != USB_SPEED_UNKNOWN &&
  385. (state & SUSPENDED_STATE)) {
  386. if (gpriv->driver && gpriv->driver->suspend)
  387. gpriv->driver->suspend(&gpriv->gadget);
  388. usb_gadget_set_state(&gpriv->gadget, USB_STATE_SUSPENDED);
  389. }
  390. return 0;
  391. }
  392. static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
  393. struct usbhs_irq_state *irq_state)
  394. {
  395. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  396. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  397. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  398. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  399. struct usb_ctrlrequest ctrl;
  400. struct usbhsg_recip_handle *recip_handler = NULL;
  401. int stage = usbhs_status_get_ctrl_stage(irq_state);
  402. int ret = 0;
  403. dev_dbg(dev, "stage = %d\n", stage);
  404. /*
  405. * see Manual
  406. *
  407. * "Operation"
  408. * - "Interrupt Function"
  409. * - "Control Transfer Stage Transition Interrupt"
  410. * - Fig. "Control Transfer Stage Transitions"
  411. */
  412. switch (stage) {
  413. case READ_DATA_STAGE:
  414. pipe->handler = &usbhs_fifo_pio_push_handler;
  415. break;
  416. case WRITE_DATA_STAGE:
  417. pipe->handler = &usbhs_fifo_pio_pop_handler;
  418. break;
  419. case NODATA_STATUS_STAGE:
  420. pipe->handler = &usbhs_ctrl_stage_end_handler;
  421. break;
  422. case READ_STATUS_STAGE:
  423. case WRITE_STATUS_STAGE:
  424. usbhs_dcp_control_transfer_done(pipe);
  425. /* fall through */
  426. default:
  427. return ret;
  428. }
  429. /*
  430. * get usb request
  431. */
  432. usbhs_usbreq_get_val(priv, &ctrl);
  433. switch (ctrl.bRequestType & USB_TYPE_MASK) {
  434. case USB_TYPE_STANDARD:
  435. switch (ctrl.bRequest) {
  436. case USB_REQ_CLEAR_FEATURE:
  437. recip_handler = &req_clear_feature;
  438. break;
  439. case USB_REQ_SET_FEATURE:
  440. recip_handler = &req_set_feature;
  441. break;
  442. case USB_REQ_GET_STATUS:
  443. recip_handler = &req_get_status;
  444. break;
  445. }
  446. }
  447. /*
  448. * setup stage / run recip
  449. */
  450. if (recip_handler)
  451. ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
  452. else
  453. ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
  454. if (ret < 0)
  455. usbhs_pipe_stall(pipe);
  456. return ret;
  457. }
  458. /*
  459. *
  460. * usb_dcp_ops
  461. *
  462. */
  463. static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
  464. {
  465. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  466. struct usbhs_pkt *pkt;
  467. while (1) {
  468. pkt = usbhs_pkt_pop(pipe, NULL);
  469. if (!pkt)
  470. break;
  471. usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ESHUTDOWN);
  472. }
  473. usbhs_pipe_disable(pipe);
  474. return 0;
  475. }
  476. /*
  477. *
  478. * usb_ep_ops
  479. *
  480. */
  481. static int usbhsg_ep_enable(struct usb_ep *ep,
  482. const struct usb_endpoint_descriptor *desc)
  483. {
  484. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  485. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  486. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  487. struct usbhs_pipe *pipe;
  488. int ret = -EIO;
  489. unsigned long flags;
  490. usbhs_lock(priv, flags);
  491. /*
  492. * if it already have pipe,
  493. * nothing to do
  494. */
  495. if (uep->pipe) {
  496. usbhs_pipe_clear(uep->pipe);
  497. usbhs_pipe_sequence_data0(uep->pipe);
  498. ret = 0;
  499. goto usbhsg_ep_enable_end;
  500. }
  501. pipe = usbhs_pipe_malloc(priv,
  502. usb_endpoint_type(desc),
  503. usb_endpoint_dir_in(desc));
  504. if (pipe) {
  505. uep->pipe = pipe;
  506. pipe->mod_private = uep;
  507. /* set epnum / maxp */
  508. usbhs_pipe_config_update(pipe, 0,
  509. usb_endpoint_num(desc),
  510. usb_endpoint_maxp(desc));
  511. /*
  512. * usbhs_fifo_dma_push/pop_handler try to
  513. * use dmaengine if possible.
  514. * It will use pio handler if impossible.
  515. */
  516. if (usb_endpoint_dir_in(desc)) {
  517. pipe->handler = &usbhs_fifo_dma_push_handler;
  518. } else {
  519. pipe->handler = &usbhs_fifo_dma_pop_handler;
  520. usbhs_xxxsts_clear(priv, BRDYSTS,
  521. usbhs_pipe_number(pipe));
  522. }
  523. ret = 0;
  524. }
  525. usbhsg_ep_enable_end:
  526. usbhs_unlock(priv, flags);
  527. return ret;
  528. }
  529. static int usbhsg_ep_disable(struct usb_ep *ep)
  530. {
  531. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  532. struct usbhs_pipe *pipe;
  533. unsigned long flags;
  534. spin_lock_irqsave(&uep->lock, flags);
  535. pipe = usbhsg_uep_to_pipe(uep);
  536. if (!pipe)
  537. goto out;
  538. usbhsg_pipe_disable(uep);
  539. usbhs_pipe_free(pipe);
  540. uep->pipe->mod_private = NULL;
  541. uep->pipe = NULL;
  542. out:
  543. spin_unlock_irqrestore(&uep->lock, flags);
  544. return 0;
  545. }
  546. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  547. gfp_t gfp_flags)
  548. {
  549. struct usbhsg_request *ureq;
  550. ureq = kzalloc(sizeof *ureq, gfp_flags);
  551. if (!ureq)
  552. return NULL;
  553. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  554. return &ureq->req;
  555. }
  556. static void usbhsg_ep_free_request(struct usb_ep *ep,
  557. struct usb_request *req)
  558. {
  559. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  560. WARN_ON(!list_empty(&ureq->pkt.node));
  561. kfree(ureq);
  562. }
  563. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  564. gfp_t gfp_flags)
  565. {
  566. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  567. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  568. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  569. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  570. /* param check */
  571. if (usbhsg_is_not_connected(gpriv) ||
  572. unlikely(!gpriv->driver) ||
  573. unlikely(!pipe))
  574. return -ESHUTDOWN;
  575. usbhsg_queue_push(uep, ureq);
  576. return 0;
  577. }
  578. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  579. {
  580. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  581. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  582. struct usbhs_pipe *pipe;
  583. unsigned long flags;
  584. spin_lock_irqsave(&uep->lock, flags);
  585. pipe = usbhsg_uep_to_pipe(uep);
  586. if (pipe)
  587. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  588. /*
  589. * To dequeue a request, this driver should call the usbhsg_queue_pop()
  590. * even if the pipe is NULL.
  591. */
  592. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  593. spin_unlock_irqrestore(&uep->lock, flags);
  594. return 0;
  595. }
  596. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  597. {
  598. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  599. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  600. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  601. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  602. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  603. unsigned long flags;
  604. int ret = 0;
  605. dev_dbg(dev, "set halt %d (pipe %d)\n",
  606. halt, usbhs_pipe_number(pipe));
  607. /******************** spin lock ********************/
  608. usbhs_lock(priv, flags);
  609. /*
  610. * According to usb_ep_set_halt()'s description, this function should
  611. * return -EAGAIN if the IN endpoint has any queue or data. Note
  612. * that the usbhs_pipe_is_dir_in() returns false if the pipe is an
  613. * IN endpoint in the gadget mode.
  614. */
  615. if (!usbhs_pipe_is_dir_in(pipe) && (__usbhsf_pkt_get(pipe) ||
  616. usbhs_pipe_contains_transmittable_data(pipe))) {
  617. ret = -EAGAIN;
  618. goto out;
  619. }
  620. if (halt)
  621. usbhs_pipe_stall(pipe);
  622. else
  623. usbhs_pipe_disable(pipe);
  624. if (halt && wedge)
  625. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  626. else
  627. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  628. out:
  629. usbhs_unlock(priv, flags);
  630. /******************** spin unlock ******************/
  631. return ret;
  632. }
  633. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  634. {
  635. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  636. }
  637. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  638. {
  639. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  640. }
  641. static const struct usb_ep_ops usbhsg_ep_ops = {
  642. .enable = usbhsg_ep_enable,
  643. .disable = usbhsg_ep_disable,
  644. .alloc_request = usbhsg_ep_alloc_request,
  645. .free_request = usbhsg_ep_free_request,
  646. .queue = usbhsg_ep_queue,
  647. .dequeue = usbhsg_ep_dequeue,
  648. .set_halt = usbhsg_ep_set_halt,
  649. .set_wedge = usbhsg_ep_set_wedge,
  650. };
  651. /*
  652. * pullup control
  653. */
  654. static int usbhsg_can_pullup(struct usbhs_priv *priv)
  655. {
  656. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  657. return gpriv->driver &&
  658. usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  659. }
  660. static void usbhsg_update_pullup(struct usbhs_priv *priv)
  661. {
  662. if (usbhsg_can_pullup(priv))
  663. usbhs_sys_function_pullup(priv, 1);
  664. else
  665. usbhs_sys_function_pullup(priv, 0);
  666. }
  667. /*
  668. * usb module start/end
  669. */
  670. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  671. {
  672. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  673. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  674. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  675. struct device *dev = usbhs_priv_to_dev(priv);
  676. unsigned long flags;
  677. int ret = 0;
  678. /******************** spin lock ********************/
  679. usbhs_lock(priv, flags);
  680. usbhsg_status_set(gpriv, status);
  681. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  682. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  683. ret = -1; /* not ready */
  684. usbhs_unlock(priv, flags);
  685. /******************** spin unlock ********************/
  686. if (ret < 0)
  687. return 0; /* not ready is not error */
  688. /*
  689. * enable interrupt and systems if ready
  690. */
  691. dev_dbg(dev, "start gadget\n");
  692. /*
  693. * pipe initialize and enable DCP
  694. */
  695. usbhs_fifo_init(priv);
  696. usbhs_pipe_init(priv,
  697. usbhsg_dma_map_ctrl);
  698. /* dcp init instead of usbhsg_ep_enable() */
  699. dcp->pipe = usbhs_dcp_malloc(priv);
  700. dcp->pipe->mod_private = dcp;
  701. usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
  702. /*
  703. * system config enble
  704. * - HI speed
  705. * - function
  706. * - usb module
  707. */
  708. usbhs_sys_function_ctrl(priv, 1);
  709. usbhsg_update_pullup(priv);
  710. /*
  711. * enable irq callback
  712. */
  713. mod->irq_dev_state = usbhsg_irq_dev_state;
  714. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  715. usbhs_irq_callback_update(priv, mod);
  716. return 0;
  717. }
  718. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  719. {
  720. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  721. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  722. struct usbhsg_uep *uep;
  723. struct device *dev = usbhs_priv_to_dev(priv);
  724. unsigned long flags;
  725. int ret = 0, i;
  726. /******************** spin lock ********************/
  727. usbhs_lock(priv, flags);
  728. usbhsg_status_clr(gpriv, status);
  729. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  730. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  731. ret = -1; /* already done */
  732. usbhs_unlock(priv, flags);
  733. /******************** spin unlock ********************/
  734. if (ret < 0)
  735. return 0; /* already done is not error */
  736. /*
  737. * disable interrupt and systems if 1st try
  738. */
  739. usbhs_fifo_quit(priv);
  740. /* disable all irq */
  741. mod->irq_dev_state = NULL;
  742. mod->irq_ctrl_stage = NULL;
  743. usbhs_irq_callback_update(priv, mod);
  744. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  745. /* disable sys */
  746. usbhs_sys_set_test_mode(priv, 0);
  747. usbhs_sys_function_ctrl(priv, 0);
  748. /* disable all eps */
  749. usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
  750. usbhsg_ep_disable(&uep->ep);
  751. dev_dbg(dev, "stop gadget\n");
  752. return 0;
  753. }
  754. /*
  755. * VBUS provided by the PHY
  756. */
  757. static int usbhsm_phy_get_vbus(struct platform_device *pdev)
  758. {
  759. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  760. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  761. return gpriv->vbus_active;
  762. }
  763. static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
  764. {
  765. struct usbhs_mod_info *info = &priv->mod_info;
  766. info->irq_vbus = NULL;
  767. priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
  768. usbhs_irq_callback_update(priv, NULL);
  769. }
  770. /*
  771. *
  772. * linux usb function
  773. *
  774. */
  775. static int usbhsg_gadget_start(struct usb_gadget *gadget,
  776. struct usb_gadget_driver *driver)
  777. {
  778. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  779. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  780. struct device *dev = usbhs_priv_to_dev(priv);
  781. int ret;
  782. if (!driver ||
  783. !driver->setup ||
  784. driver->max_speed < USB_SPEED_FULL)
  785. return -EINVAL;
  786. /* connect to bus through transceiver */
  787. if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
  788. ret = otg_set_peripheral(gpriv->transceiver->otg,
  789. &gpriv->gadget);
  790. if (ret) {
  791. dev_err(dev, "%s: can't bind to transceiver\n",
  792. gpriv->gadget.name);
  793. return ret;
  794. }
  795. /* get vbus using phy versions */
  796. usbhs_mod_phy_mode(priv);
  797. }
  798. /* first hook up the driver ... */
  799. gpriv->driver = driver;
  800. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  801. }
  802. static int usbhsg_gadget_stop(struct usb_gadget *gadget)
  803. {
  804. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  805. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  806. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  807. if (!IS_ERR_OR_NULL(gpriv->transceiver))
  808. otg_set_peripheral(gpriv->transceiver->otg, NULL);
  809. gpriv->driver = NULL;
  810. return 0;
  811. }
  812. /*
  813. * usb gadget ops
  814. */
  815. static int usbhsg_get_frame(struct usb_gadget *gadget)
  816. {
  817. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  818. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  819. return usbhs_frame_get_num(priv);
  820. }
  821. static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
  822. {
  823. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  824. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  825. unsigned long flags;
  826. usbhs_lock(priv, flags);
  827. if (is_on)
  828. usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  829. else
  830. usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  831. usbhsg_update_pullup(priv);
  832. usbhs_unlock(priv, flags);
  833. return 0;
  834. }
  835. static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
  836. {
  837. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  838. if (is_self)
  839. usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
  840. else
  841. usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
  842. gadget->is_selfpowered = (is_self != 0);
  843. return 0;
  844. }
  845. static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
  846. {
  847. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  848. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  849. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  850. gpriv->vbus_active = !!is_active;
  851. renesas_usbhs_call_notify_hotplug(pdev);
  852. return 0;
  853. }
  854. static const struct usb_gadget_ops usbhsg_gadget_ops = {
  855. .get_frame = usbhsg_get_frame,
  856. .set_selfpowered = usbhsg_set_selfpowered,
  857. .udc_start = usbhsg_gadget_start,
  858. .udc_stop = usbhsg_gadget_stop,
  859. .pullup = usbhsg_pullup,
  860. .vbus_session = usbhsg_vbus_session,
  861. };
  862. static int usbhsg_start(struct usbhs_priv *priv)
  863. {
  864. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  865. }
  866. static int usbhsg_stop(struct usbhs_priv *priv)
  867. {
  868. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  869. /* cable disconnect */
  870. if (gpriv->driver &&
  871. gpriv->driver->disconnect)
  872. gpriv->driver->disconnect(&gpriv->gadget);
  873. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  874. }
  875. int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  876. {
  877. struct usbhsg_gpriv *gpriv;
  878. struct usbhsg_uep *uep;
  879. struct device *dev = usbhs_priv_to_dev(priv);
  880. struct renesas_usbhs_driver_pipe_config *pipe_configs =
  881. usbhs_get_dparam(priv, pipe_configs);
  882. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  883. int i;
  884. int ret;
  885. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  886. if (!gpriv)
  887. return -ENOMEM;
  888. uep = kcalloc(pipe_size, sizeof(struct usbhsg_uep), GFP_KERNEL);
  889. if (!uep) {
  890. ret = -ENOMEM;
  891. goto usbhs_mod_gadget_probe_err_gpriv;
  892. }
  893. gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
  894. dev_info(dev, "%stransceiver found\n",
  895. !IS_ERR(gpriv->transceiver) ? "" : "no ");
  896. /*
  897. * CAUTION
  898. *
  899. * There is no guarantee that it is possible to access usb module here.
  900. * Don't accesses to it.
  901. * The accesse will be enable after "usbhsg_start"
  902. */
  903. /*
  904. * register itself
  905. */
  906. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  907. /* init gpriv */
  908. gpriv->mod.name = "gadget";
  909. gpriv->mod.start = usbhsg_start;
  910. gpriv->mod.stop = usbhsg_stop;
  911. gpriv->uep = uep;
  912. gpriv->uep_size = pipe_size;
  913. usbhsg_status_init(gpriv);
  914. /*
  915. * init gadget
  916. */
  917. gpriv->gadget.dev.parent = dev;
  918. gpriv->gadget.name = "renesas_usbhs_udc";
  919. gpriv->gadget.ops = &usbhsg_gadget_ops;
  920. gpriv->gadget.max_speed = USB_SPEED_HIGH;
  921. gpriv->gadget.quirk_avoids_skb_reserve = usbhs_get_dparam(priv,
  922. has_usb_dmac);
  923. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  924. /*
  925. * init usb_ep
  926. */
  927. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  928. uep->gpriv = gpriv;
  929. uep->pipe = NULL;
  930. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  931. uep->ep.name = uep->ep_name;
  932. uep->ep.ops = &usbhsg_ep_ops;
  933. INIT_LIST_HEAD(&uep->ep.ep_list);
  934. spin_lock_init(&uep->lock);
  935. /* init DCP */
  936. if (usbhsg_is_dcp(uep)) {
  937. gpriv->gadget.ep0 = &uep->ep;
  938. usb_ep_set_maxpacket_limit(&uep->ep, 64);
  939. uep->ep.caps.type_control = true;
  940. } else {
  941. /* init normal pipe */
  942. if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
  943. uep->ep.caps.type_iso = true;
  944. if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
  945. uep->ep.caps.type_bulk = true;
  946. if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
  947. uep->ep.caps.type_int = true;
  948. usb_ep_set_maxpacket_limit(&uep->ep,
  949. pipe_configs[i].bufsize);
  950. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  951. }
  952. uep->ep.caps.dir_in = true;
  953. uep->ep.caps.dir_out = true;
  954. }
  955. ret = usb_add_gadget_udc(dev, &gpriv->gadget);
  956. if (ret)
  957. goto err_add_udc;
  958. dev_info(dev, "gadget probed\n");
  959. return 0;
  960. err_add_udc:
  961. kfree(gpriv->uep);
  962. usbhs_mod_gadget_probe_err_gpriv:
  963. kfree(gpriv);
  964. return ret;
  965. }
  966. void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  967. {
  968. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  969. usb_del_gadget_udc(&gpriv->gadget);
  970. kfree(gpriv->uep);
  971. kfree(gpriv);
  972. }