mod_gadget.c 25 KB

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