mod_gadget.c 25 KB

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