mod_gadget.c 25 KB

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