mod_gadget.c 22 KB

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