mod_gadget.c 24 KB

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