mod_gadget.c 25 KB

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