mod_gadget.c 24 KB

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