mod_gadget.c 25 KB

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