mod_gadget.c 24 KB

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