mod_gadget.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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, pos = (g)->uep + i; \
  70. i < (g)->uep_size; \
  71. i++, pos = (g)->uep + 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. ureq->req.complete(&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. 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. 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. 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. usbhsg_pipe_disable(uep);
  503. uep->pipe->mod_private = NULL;
  504. uep->pipe = NULL;
  505. return 0;
  506. }
  507. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  508. gfp_t gfp_flags)
  509. {
  510. struct usbhsg_request *ureq;
  511. ureq = kzalloc(sizeof *ureq, gfp_flags);
  512. if (!ureq)
  513. return NULL;
  514. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  515. return &ureq->req;
  516. }
  517. static void usbhsg_ep_free_request(struct usb_ep *ep,
  518. struct usb_request *req)
  519. {
  520. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  521. WARN_ON(!list_empty(&ureq->pkt.node));
  522. kfree(ureq);
  523. }
  524. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  525. gfp_t gfp_flags)
  526. {
  527. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  528. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  529. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  530. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  531. /* param check */
  532. if (usbhsg_is_not_connected(gpriv) ||
  533. unlikely(!gpriv->driver) ||
  534. unlikely(!pipe))
  535. return -ESHUTDOWN;
  536. usbhsg_queue_push(uep, ureq);
  537. return 0;
  538. }
  539. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  540. {
  541. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  542. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  543. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  544. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  545. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  546. return 0;
  547. }
  548. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  549. {
  550. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  551. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  552. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  553. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  554. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  555. unsigned long flags;
  556. usbhsg_pipe_disable(uep);
  557. dev_dbg(dev, "set halt %d (pipe %d)\n",
  558. halt, usbhs_pipe_number(pipe));
  559. /******************** spin lock ********************/
  560. usbhs_lock(priv, flags);
  561. if (halt)
  562. usbhs_pipe_stall(pipe);
  563. else
  564. usbhs_pipe_disable(pipe);
  565. if (halt && wedge)
  566. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  567. else
  568. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  569. usbhs_unlock(priv, flags);
  570. /******************** spin unlock ******************/
  571. return 0;
  572. }
  573. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  574. {
  575. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  576. }
  577. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  578. {
  579. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  580. }
  581. static struct usb_ep_ops usbhsg_ep_ops = {
  582. .enable = usbhsg_ep_enable,
  583. .disable = usbhsg_ep_disable,
  584. .alloc_request = usbhsg_ep_alloc_request,
  585. .free_request = usbhsg_ep_free_request,
  586. .queue = usbhsg_ep_queue,
  587. .dequeue = usbhsg_ep_dequeue,
  588. .set_halt = usbhsg_ep_set_halt,
  589. .set_wedge = usbhsg_ep_set_wedge,
  590. };
  591. /*
  592. * usb module start/end
  593. */
  594. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  595. {
  596. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  597. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  598. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  599. struct device *dev = usbhs_priv_to_dev(priv);
  600. unsigned long flags;
  601. int ret = 0;
  602. /******************** spin lock ********************/
  603. usbhs_lock(priv, flags);
  604. usbhsg_status_set(gpriv, status);
  605. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  606. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  607. ret = -1; /* not ready */
  608. usbhs_unlock(priv, flags);
  609. /******************** spin unlock ********************/
  610. if (ret < 0)
  611. return 0; /* not ready is not error */
  612. /*
  613. * enable interrupt and systems if ready
  614. */
  615. dev_dbg(dev, "start gadget\n");
  616. /*
  617. * pipe initialize and enable DCP
  618. */
  619. usbhs_pipe_init(priv,
  620. usbhsg_dma_map_ctrl);
  621. usbhs_fifo_init(priv);
  622. /* dcp init instead of usbhsg_ep_enable() */
  623. dcp->pipe = usbhs_dcp_malloc(priv);
  624. dcp->pipe->mod_private = dcp;
  625. usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
  626. /*
  627. * system config enble
  628. * - HI speed
  629. * - function
  630. * - usb module
  631. */
  632. usbhs_sys_function_ctrl(priv, 1);
  633. /*
  634. * enable irq callback
  635. */
  636. mod->irq_dev_state = usbhsg_irq_dev_state;
  637. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  638. usbhs_irq_callback_update(priv, mod);
  639. return 0;
  640. }
  641. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  642. {
  643. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  644. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  645. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  646. struct device *dev = usbhs_priv_to_dev(priv);
  647. unsigned long flags;
  648. int ret = 0;
  649. /******************** spin lock ********************/
  650. usbhs_lock(priv, flags);
  651. usbhsg_status_clr(gpriv, status);
  652. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  653. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  654. ret = -1; /* already done */
  655. usbhs_unlock(priv, flags);
  656. /******************** spin unlock ********************/
  657. if (ret < 0)
  658. return 0; /* already done is not error */
  659. /*
  660. * disable interrupt and systems if 1st try
  661. */
  662. usbhs_fifo_quit(priv);
  663. /* disable all irq */
  664. mod->irq_dev_state = NULL;
  665. mod->irq_ctrl_stage = NULL;
  666. usbhs_irq_callback_update(priv, mod);
  667. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  668. /* disable sys */
  669. usbhs_sys_set_test_mode(priv, 0);
  670. usbhs_sys_function_ctrl(priv, 0);
  671. usbhsg_ep_disable(&dcp->ep);
  672. dev_dbg(dev, "stop gadget\n");
  673. return 0;
  674. }
  675. /*
  676. *
  677. * linux usb function
  678. *
  679. */
  680. static int usbhsg_gadget_start(struct usb_gadget *gadget,
  681. struct usb_gadget_driver *driver)
  682. {
  683. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  684. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  685. if (!driver ||
  686. !driver->setup ||
  687. driver->max_speed < USB_SPEED_FULL)
  688. return -EINVAL;
  689. /* first hook up the driver ... */
  690. gpriv->driver = driver;
  691. gpriv->gadget.dev.driver = &driver->driver;
  692. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  693. }
  694. static int usbhsg_gadget_stop(struct usb_gadget *gadget,
  695. struct usb_gadget_driver *driver)
  696. {
  697. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  698. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  699. if (!driver ||
  700. !driver->unbind)
  701. return -EINVAL;
  702. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  703. gpriv->gadget.dev.driver = NULL;
  704. gpriv->driver = NULL;
  705. return 0;
  706. }
  707. /*
  708. * usb gadget ops
  709. */
  710. static int usbhsg_get_frame(struct usb_gadget *gadget)
  711. {
  712. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  713. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  714. return usbhs_frame_get_num(priv);
  715. }
  716. static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
  717. {
  718. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  719. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  720. usbhs_sys_function_pullup(priv, is_on);
  721. return 0;
  722. }
  723. static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
  724. {
  725. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  726. if (is_self)
  727. usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
  728. else
  729. usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
  730. return 0;
  731. }
  732. static const struct usb_gadget_ops usbhsg_gadget_ops = {
  733. .get_frame = usbhsg_get_frame,
  734. .set_selfpowered = usbhsg_set_selfpowered,
  735. .udc_start = usbhsg_gadget_start,
  736. .udc_stop = usbhsg_gadget_stop,
  737. .pullup = usbhsg_pullup,
  738. };
  739. static int usbhsg_start(struct usbhs_priv *priv)
  740. {
  741. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  742. }
  743. static int usbhsg_stop(struct usbhs_priv *priv)
  744. {
  745. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  746. /* cable disconnect */
  747. if (gpriv->driver &&
  748. gpriv->driver->disconnect)
  749. gpriv->driver->disconnect(&gpriv->gadget);
  750. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  751. }
  752. static void usbhs_mod_gadget_release(struct device *pdev)
  753. {
  754. /* do nothing */
  755. }
  756. int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  757. {
  758. struct usbhsg_gpriv *gpriv;
  759. struct usbhsg_uep *uep;
  760. struct device *dev = usbhs_priv_to_dev(priv);
  761. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  762. int i;
  763. int ret;
  764. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  765. if (!gpriv) {
  766. dev_err(dev, "Could not allocate gadget priv\n");
  767. return -ENOMEM;
  768. }
  769. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  770. if (!uep) {
  771. dev_err(dev, "Could not allocate ep\n");
  772. ret = -ENOMEM;
  773. goto usbhs_mod_gadget_probe_err_gpriv;
  774. }
  775. /*
  776. * CAUTION
  777. *
  778. * There is no guarantee that it is possible to access usb module here.
  779. * Don't accesses to it.
  780. * The accesse will be enable after "usbhsg_start"
  781. */
  782. /*
  783. * register itself
  784. */
  785. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  786. /* init gpriv */
  787. gpriv->mod.name = "gadget";
  788. gpriv->mod.start = usbhsg_start;
  789. gpriv->mod.stop = usbhsg_stop;
  790. gpriv->uep = uep;
  791. gpriv->uep_size = pipe_size;
  792. usbhsg_status_init(gpriv);
  793. /*
  794. * init gadget
  795. */
  796. dev_set_name(&gpriv->gadget.dev, "gadget");
  797. gpriv->gadget.dev.parent = dev;
  798. gpriv->gadget.dev.release = usbhs_mod_gadget_release;
  799. gpriv->gadget.name = "renesas_usbhs_udc";
  800. gpriv->gadget.ops = &usbhsg_gadget_ops;
  801. gpriv->gadget.max_speed = USB_SPEED_HIGH;
  802. ret = device_register(&gpriv->gadget.dev);
  803. if (ret < 0)
  804. goto err_add_udc;
  805. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  806. /*
  807. * init usb_ep
  808. */
  809. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  810. uep->gpriv = gpriv;
  811. uep->pipe = NULL;
  812. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  813. uep->ep.name = uep->ep_name;
  814. uep->ep.ops = &usbhsg_ep_ops;
  815. INIT_LIST_HEAD(&uep->ep.ep_list);
  816. /* init DCP */
  817. if (usbhsg_is_dcp(uep)) {
  818. gpriv->gadget.ep0 = &uep->ep;
  819. uep->ep.maxpacket = 64;
  820. }
  821. /* init normal pipe */
  822. else {
  823. uep->ep.maxpacket = 512;
  824. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  825. }
  826. }
  827. ret = usb_add_gadget_udc(dev, &gpriv->gadget);
  828. if (ret)
  829. goto err_register;
  830. dev_info(dev, "gadget probed\n");
  831. return 0;
  832. err_register:
  833. device_unregister(&gpriv->gadget.dev);
  834. err_add_udc:
  835. kfree(gpriv->uep);
  836. usbhs_mod_gadget_probe_err_gpriv:
  837. kfree(gpriv);
  838. return ret;
  839. }
  840. void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  841. {
  842. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  843. usb_del_gadget_udc(&gpriv->gadget);
  844. device_unregister(&gpriv->gadget.dev);
  845. kfree(gpriv->uep);
  846. kfree(gpriv);
  847. }