mod_gadget.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include "common.h"
  23. /*
  24. * struct
  25. */
  26. struct usbhsg_request {
  27. struct usb_request req;
  28. struct list_head node;
  29. };
  30. #define EP_NAME_SIZE 8
  31. struct usbhsg_gpriv;
  32. struct usbhsg_pipe_handle;
  33. struct usbhsg_uep {
  34. struct usb_ep ep;
  35. struct usbhs_pipe *pipe;
  36. struct list_head list;
  37. char ep_name[EP_NAME_SIZE];
  38. struct usbhsg_gpriv *gpriv;
  39. struct usbhsg_pipe_handle *handler;
  40. };
  41. struct usbhsg_gpriv {
  42. struct usb_gadget gadget;
  43. struct usbhs_mod mod;
  44. struct usbhsg_uep *uep;
  45. int uep_size;
  46. struct usb_gadget_driver *driver;
  47. u32 status;
  48. #define USBHSG_STATUS_STARTED (1 << 0)
  49. #define USBHSG_STATUS_REGISTERD (1 << 1)
  50. #define USBHSG_STATUS_WEDGE (1 << 2)
  51. };
  52. struct usbhsg_pipe_handle {
  53. int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
  54. int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
  55. void (*irq_mask)(struct usbhsg_uep *uep, int enable);
  56. };
  57. struct usbhsg_recip_handle {
  58. char *name;
  59. int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  60. struct usb_ctrlrequest *ctrl);
  61. int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  62. struct usb_ctrlrequest *ctrl);
  63. int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  64. struct usb_ctrlrequest *ctrl);
  65. };
  66. /*
  67. * macro
  68. */
  69. #define usbhsg_priv_to_gpriv(priv) \
  70. container_of( \
  71. usbhs_mod_get(priv, USBHS_GADGET), \
  72. struct usbhsg_gpriv, mod)
  73. #define __usbhsg_for_each_uep(start, pos, g, i) \
  74. for (i = start, pos = (g)->uep; \
  75. i < (g)->uep_size; \
  76. i++, pos = (g)->uep + i)
  77. #define usbhsg_for_each_uep(pos, gpriv, i) \
  78. __usbhsg_for_each_uep(1, pos, gpriv, i)
  79. #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
  80. __usbhsg_for_each_uep(0, pos, gpriv, i)
  81. #define usbhsg_gadget_to_gpriv(g)\
  82. container_of(g, struct usbhsg_gpriv, gadget)
  83. #define usbhsg_req_to_ureq(r)\
  84. container_of(r, struct usbhsg_request, req)
  85. #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
  86. #define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
  87. #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
  88. #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
  89. #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
  90. #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
  91. #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
  92. #define usbhsg_uep_to_pipe(u) ((u)->pipe)
  93. #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
  94. #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
  95. #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
  96. /* status */
  97. #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
  98. #define usbhsg_status_set(gp, b) (gp->status |= b)
  99. #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
  100. #define usbhsg_status_has(gp, b) (gp->status & b)
  101. /*
  102. * list push/pop
  103. */
  104. static void usbhsg_queue_push(struct usbhsg_uep *uep,
  105. struct usbhsg_request *ureq)
  106. {
  107. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  108. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  109. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  110. /*
  111. ********* assume under spin lock *********
  112. */
  113. list_del_init(&ureq->node);
  114. list_add_tail(&ureq->node, &uep->list);
  115. ureq->req.actual = 0;
  116. ureq->req.status = -EINPROGRESS;
  117. dev_dbg(dev, "pipe %d : queue push (%d)\n",
  118. usbhs_pipe_number(pipe),
  119. ureq->req.length);
  120. }
  121. static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
  122. {
  123. /*
  124. ********* assume under spin lock *********
  125. */
  126. if (list_empty(&uep->list))
  127. return NULL;
  128. return list_entry(uep->list.next, struct usbhsg_request, node);
  129. }
  130. #define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
  131. #define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
  132. static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
  133. {
  134. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  135. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  136. struct usbhsg_request *ureq;
  137. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  138. unsigned long flags;
  139. int is_locked;
  140. int ret = 0;
  141. if (!uep->handler) {
  142. dev_err(dev, "no handler function\n");
  143. return -EIO;
  144. }
  145. /*
  146. * CAUTION [*queue handler*]
  147. *
  148. * This function will be called for start/restart queue operation.
  149. * OTOH the most much worry for USB driver is spinlock nest.
  150. * Specially it are
  151. * - usb_ep_ops :: queue
  152. * - usb_request :: complete
  153. *
  154. * But the caller of this function need not care about spinlock.
  155. * This function is using spin_trylock_irqsave for it.
  156. * if "is_locked" is 1, this mean this function lock it.
  157. * but if it is 0, this mean it is already under spin lock.
  158. * see also
  159. * CAUTION [*endpoint queue*]
  160. * CAUTION [*request complete*]
  161. */
  162. /****************** spin try lock *******************/
  163. is_locked = spin_trylock_irqsave(lock, flags);
  164. ureq = usbhsg_queue_get(uep);
  165. if (ureq) {
  166. if (prepare)
  167. ret = uep->handler->prepare(uep, ureq);
  168. else
  169. ret = uep->handler->try_run(uep, ureq);
  170. }
  171. if (is_locked)
  172. spin_unlock_irqrestore(lock, flags);
  173. /******************** spin unlock ******************/
  174. return ret;
  175. }
  176. static void usbhsg_queue_pop(struct usbhsg_uep *uep,
  177. struct usbhsg_request *ureq,
  178. int status)
  179. {
  180. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  181. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  182. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  183. /*
  184. ********* assume under spin lock *********
  185. */
  186. /*
  187. * CAUTION [*request complete*]
  188. *
  189. * There is a possibility not to be called in correct order
  190. * if "complete" is called without spinlock.
  191. *
  192. * So, this function assume it is under spinlock,
  193. * and call usb_request :: complete.
  194. *
  195. * But this "complete" will push next usb_request.
  196. * It mean "usb_ep_ops :: queue" which is using spinlock is called
  197. * under spinlock.
  198. *
  199. * To avoid dead-lock, this driver is using spin_trylock.
  200. * CAUTION [*endpoint queue*]
  201. * CAUTION [*queue handler*]
  202. */
  203. dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
  204. list_del_init(&ureq->node);
  205. ureq->req.status = status;
  206. ureq->req.complete(&uep->ep, &ureq->req);
  207. /* more request ? */
  208. if (0 == status)
  209. usbhsg_queue_prepare(uep);
  210. }
  211. /*
  212. * irq enable/disable function
  213. */
  214. #define usbhsg_irq_callback_ctrl(uep, status, enable) \
  215. ({ \
  216. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
  217. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
  218. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
  219. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  220. if (!mod) \
  221. return; \
  222. if (enable) \
  223. mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
  224. else \
  225. mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
  226. usbhs_irq_callback_update(priv, mod); \
  227. })
  228. static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
  229. {
  230. usbhsg_irq_callback_ctrl(uep, bempsts, enable);
  231. }
  232. static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
  233. {
  234. usbhsg_irq_callback_ctrl(uep, brdysts, enable);
  235. }
  236. /*
  237. * handler function
  238. */
  239. static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
  240. struct usbhsg_request *ureq)
  241. {
  242. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  243. /*
  244. ********* assume under spin lock *********
  245. */
  246. usbhs_dcp_control_transfer_done(pipe);
  247. usbhsg_queue_pop(uep, ureq, 0);
  248. return 0;
  249. }
  250. static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
  251. struct usbhsg_request *ureq)
  252. {
  253. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  254. struct usb_request *req = &ureq->req;
  255. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  256. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  257. void *buf;
  258. int remainder, send;
  259. int is_done = 0;
  260. int enable;
  261. int maxp;
  262. /*
  263. ********* assume under spin lock *********
  264. */
  265. maxp = usbhs_pipe_get_maxpacket(pipe);
  266. buf = req->buf + req->actual;
  267. remainder = req->length - req->actual;
  268. send = usbhs_fifo_write(pipe, buf, remainder);
  269. /*
  270. * send < 0 : pipe busy
  271. * send = 0 : send zero packet
  272. * send > 0 : send data
  273. *
  274. * send <= max_packet
  275. */
  276. if (send > 0)
  277. req->actual += send;
  278. /* send all packet ? */
  279. if (send < remainder)
  280. is_done = 0; /* there are remainder data */
  281. else if (send < maxp)
  282. is_done = 1; /* short packet */
  283. else
  284. is_done = !req->zero; /* send zero packet ? */
  285. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  286. usbhs_pipe_number(pipe),
  287. remainder, send, is_done, req->zero);
  288. /*
  289. * enable interrupt and send again in irq handler
  290. * if it still have remainder data which should be sent.
  291. */
  292. enable = !is_done;
  293. uep->handler->irq_mask(uep, enable);
  294. /*
  295. * usbhs_fifo_enable execute
  296. * - after callback_update,
  297. * - before queue_pop / stage_end
  298. */
  299. usbhs_fifo_enable(pipe);
  300. /*
  301. * all data were sent ?
  302. */
  303. if (is_done) {
  304. /* it care below call in
  305. "function mode" */
  306. if (usbhsg_is_dcp(uep))
  307. usbhs_dcp_control_transfer_done(pipe);
  308. usbhsg_queue_pop(uep, ureq, 0);
  309. }
  310. return 0;
  311. }
  312. static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
  313. struct usbhsg_request *ureq)
  314. {
  315. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  316. /*
  317. ********* assume under spin lock *********
  318. */
  319. usbhs_fifo_prepare_write(pipe);
  320. usbhsg_try_run_send_packet(uep, ureq);
  321. return 0;
  322. }
  323. static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
  324. struct usbhsg_request *ureq)
  325. {
  326. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  327. struct usb_request *req = &ureq->req;
  328. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  329. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  330. void *buf;
  331. int maxp;
  332. int remainder, recv;
  333. int is_done = 0;
  334. /*
  335. ********* assume under spin lock *********
  336. */
  337. maxp = usbhs_pipe_get_maxpacket(pipe);
  338. buf = req->buf + req->actual;
  339. remainder = req->length - req->actual;
  340. recv = usbhs_fifo_read(pipe, buf, remainder);
  341. /*
  342. * recv < 0 : pipe busy
  343. * recv >= 0 : receive data
  344. *
  345. * recv <= max_packet
  346. */
  347. if (recv < 0)
  348. return -EBUSY;
  349. /* update parameters */
  350. req->actual += recv;
  351. if ((recv == remainder) || /* receive all data */
  352. (recv < maxp)) /* short packet */
  353. is_done = 1;
  354. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  355. usbhs_pipe_number(pipe),
  356. remainder, recv, is_done, req->zero);
  357. /* read all data ? */
  358. if (is_done) {
  359. int disable = 0;
  360. uep->handler->irq_mask(uep, disable);
  361. usbhs_fifo_disable(pipe);
  362. usbhsg_queue_pop(uep, ureq, 0);
  363. }
  364. return 0;
  365. }
  366. static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
  367. struct usbhsg_request *ureq)
  368. {
  369. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  370. int enable = 1;
  371. int ret;
  372. /*
  373. ********* assume under spin lock *********
  374. */
  375. ret = usbhs_fifo_prepare_read(pipe);
  376. if (ret < 0)
  377. return ret;
  378. /*
  379. * data will be read in interrupt handler
  380. */
  381. uep->handler->irq_mask(uep, enable);
  382. return ret;
  383. }
  384. static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
  385. .prepare = usbhsg_prepare_send_packet,
  386. .try_run = usbhsg_try_run_send_packet,
  387. .irq_mask = usbhsg_irq_empty_ctrl,
  388. };
  389. static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
  390. .prepare = usbhsg_prepare_send_packet,
  391. .try_run = usbhsg_try_run_send_packet,
  392. .irq_mask = usbhsg_irq_ready_ctrl,
  393. };
  394. static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
  395. .prepare = usbhsg_prepare_receive_packet,
  396. .try_run = usbhsg_try_run_receive_packet,
  397. .irq_mask = usbhsg_irq_ready_ctrl,
  398. };
  399. static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
  400. .prepare = usbhsg_try_run_ctrl_stage_end,
  401. .try_run = usbhsg_try_run_ctrl_stage_end,
  402. };
  403. /*
  404. * DCP pipe can NOT use "ready interrupt" for "send"
  405. * it should use "empty" interrupt.
  406. * see
  407. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  408. *
  409. * on the other hand, normal pipe can use "ready interrupt" for "send"
  410. * even though it is single/double buffer
  411. */
  412. #define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
  413. #define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
  414. #define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
  415. #define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
  416. /*
  417. * USB_TYPE_STANDARD / clear feature functions
  418. */
  419. static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
  420. struct usbhsg_uep *uep,
  421. struct usb_ctrlrequest *ctrl)
  422. {
  423. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  424. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  425. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  426. usbhs_dcp_control_transfer_done(pipe);
  427. return 0;
  428. }
  429. static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
  430. struct usbhsg_uep *uep,
  431. struct usb_ctrlrequest *ctrl)
  432. {
  433. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  434. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  435. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
  436. usbhs_fifo_disable(pipe);
  437. usbhs_pipe_clear_sequence(pipe);
  438. usbhs_fifo_enable(pipe);
  439. }
  440. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  441. usbhsg_queue_prepare(uep);
  442. return 0;
  443. }
  444. struct usbhsg_recip_handle req_clear_feature = {
  445. .name = "clear feature",
  446. .device = usbhsg_recip_handler_std_control_done,
  447. .interface = usbhsg_recip_handler_std_control_done,
  448. .endpoint = usbhsg_recip_handler_std_clear_endpoint,
  449. };
  450. /*
  451. * USB_TYPE handler
  452. */
  453. static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
  454. struct usbhsg_recip_handle *handler,
  455. struct usb_ctrlrequest *ctrl)
  456. {
  457. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  458. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  459. struct usbhsg_uep *uep;
  460. int recip = ctrl->bRequestType & USB_RECIP_MASK;
  461. int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  462. int ret;
  463. int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  464. struct usb_ctrlrequest *ctrl);
  465. char *msg;
  466. uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
  467. switch (recip) {
  468. case USB_RECIP_DEVICE:
  469. msg = "DEVICE";
  470. func = handler->device;
  471. break;
  472. case USB_RECIP_INTERFACE:
  473. msg = "INTERFACE";
  474. func = handler->interface;
  475. break;
  476. case USB_RECIP_ENDPOINT:
  477. msg = "ENDPOINT";
  478. func = handler->endpoint;
  479. break;
  480. default:
  481. dev_warn(dev, "unsupported RECIP(%d)\n", recip);
  482. func = NULL;
  483. ret = -EINVAL;
  484. }
  485. if (func) {
  486. dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
  487. ret = func(priv, uep, ctrl);
  488. }
  489. return ret;
  490. }
  491. /*
  492. * irq functions
  493. *
  494. * it will be called from usbhs_interrupt
  495. */
  496. static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
  497. struct usbhs_irq_state *irq_state)
  498. {
  499. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  500. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  501. gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
  502. dev_dbg(dev, "state = %x : speed : %d\n",
  503. usbhs_status_get_device_state(irq_state),
  504. gpriv->gadget.speed);
  505. return 0;
  506. }
  507. static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
  508. struct usbhs_irq_state *irq_state)
  509. {
  510. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  511. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  512. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  513. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  514. struct usb_ctrlrequest ctrl;
  515. struct usbhsg_recip_handle *recip_handler = NULL;
  516. int stage = usbhs_status_get_ctrl_stage(irq_state);
  517. int ret = 0;
  518. dev_dbg(dev, "stage = %d\n", stage);
  519. /*
  520. * see Manual
  521. *
  522. * "Operation"
  523. * - "Interrupt Function"
  524. * - "Control Transfer Stage Transition Interrupt"
  525. * - Fig. "Control Transfer Stage Transitions"
  526. */
  527. switch (stage) {
  528. case READ_DATA_STAGE:
  529. dcp->handler = &usbhsg_handler_send_ctrl;
  530. break;
  531. case WRITE_DATA_STAGE:
  532. dcp->handler = &usbhsg_handler_recv_ctrl;
  533. break;
  534. case NODATA_STATUS_STAGE:
  535. dcp->handler = &usbhsg_handler_ctrl_stage_end;
  536. break;
  537. default:
  538. return ret;
  539. }
  540. /*
  541. * get usb request
  542. */
  543. usbhs_usbreq_get_val(priv, &ctrl);
  544. switch (ctrl.bRequestType & USB_TYPE_MASK) {
  545. case USB_TYPE_STANDARD:
  546. switch (ctrl.bRequest) {
  547. case USB_REQ_CLEAR_FEATURE:
  548. recip_handler = &req_clear_feature;
  549. break;
  550. }
  551. }
  552. /*
  553. * setup stage / run recip
  554. */
  555. if (recip_handler)
  556. ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
  557. else
  558. ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
  559. if (ret < 0)
  560. usbhs_fifo_stall(pipe);
  561. return ret;
  562. }
  563. static int usbhsg_irq_empty(struct usbhs_priv *priv,
  564. struct usbhs_irq_state *irq_state)
  565. {
  566. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  567. struct usbhsg_uep *uep;
  568. struct usbhs_pipe *pipe;
  569. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  570. int i, ret;
  571. if (!irq_state->bempsts) {
  572. dev_err(dev, "debug %s !!\n", __func__);
  573. return -EIO;
  574. }
  575. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  576. /*
  577. * search interrupted "pipe"
  578. * not "uep".
  579. */
  580. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  581. if (!(irq_state->bempsts & (1 << i)))
  582. continue;
  583. uep = usbhsg_pipe_to_uep(pipe);
  584. ret = usbhsg_queue_handle(uep);
  585. if (ret < 0)
  586. dev_err(dev, "send error %d : %d\n", i, ret);
  587. }
  588. return 0;
  589. }
  590. static int usbhsg_irq_ready(struct usbhs_priv *priv,
  591. struct usbhs_irq_state *irq_state)
  592. {
  593. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  594. struct usbhsg_uep *uep;
  595. struct usbhs_pipe *pipe;
  596. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  597. int i, ret;
  598. if (!irq_state->brdysts) {
  599. dev_err(dev, "debug %s !!\n", __func__);
  600. return -EIO;
  601. }
  602. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  603. /*
  604. * search interrupted "pipe"
  605. * not "uep".
  606. */
  607. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  608. if (!(irq_state->brdysts & (1 << i)))
  609. continue;
  610. uep = usbhsg_pipe_to_uep(pipe);
  611. ret = usbhsg_queue_handle(uep);
  612. if (ret < 0)
  613. dev_err(dev, "receive error %d : %d\n", i, ret);
  614. }
  615. return 0;
  616. }
  617. /*
  618. *
  619. * usb_dcp_ops
  620. *
  621. */
  622. static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
  623. {
  624. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  625. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  626. struct usbhs_pipe *pipe;
  627. /*
  628. ********* assume under spin lock *********
  629. */
  630. pipe = usbhs_dcp_malloc(priv);
  631. if (!pipe)
  632. return -EIO;
  633. uep->pipe = pipe;
  634. uep->pipe->mod_private = uep;
  635. INIT_LIST_HEAD(&uep->list);
  636. return 0;
  637. }
  638. #define usbhsg_dcp_disable usbhsg_pipe_disable
  639. static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
  640. {
  641. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  642. struct usbhsg_request *ureq;
  643. int disable = 0;
  644. /*
  645. ********* assume under spin lock *********
  646. */
  647. usbhs_fifo_disable(pipe);
  648. /*
  649. * disable pipe irq
  650. */
  651. usbhsg_irq_empty_ctrl(uep, disable);
  652. usbhsg_irq_ready_ctrl(uep, disable);
  653. while (1) {
  654. ureq = usbhsg_queue_get(uep);
  655. if (!ureq)
  656. break;
  657. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  658. }
  659. uep->pipe->mod_private = NULL;
  660. uep->pipe = NULL;
  661. return 0;
  662. }
  663. /*
  664. *
  665. * usb_ep_ops
  666. *
  667. */
  668. static int usbhsg_ep_enable(struct usb_ep *ep,
  669. const struct usb_endpoint_descriptor *desc)
  670. {
  671. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  672. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  673. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  674. struct usbhs_pipe *pipe;
  675. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  676. unsigned long flags;
  677. int ret = -EIO;
  678. /******************** spin lock ********************/
  679. spin_lock_irqsave(lock, flags);
  680. pipe = usbhs_pipe_malloc(priv, desc);
  681. if (pipe) {
  682. uep->pipe = pipe;
  683. pipe->mod_private = uep;
  684. INIT_LIST_HEAD(&uep->list);
  685. if (usb_endpoint_dir_in(desc))
  686. uep->handler = &usbhsg_handler_send_packet;
  687. else
  688. uep->handler = &usbhsg_handler_recv_packet;
  689. ret = 0;
  690. }
  691. spin_unlock_irqrestore(lock, flags);
  692. /******************** spin unlock ******************/
  693. return ret;
  694. }
  695. static int usbhsg_ep_disable(struct usb_ep *ep)
  696. {
  697. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  698. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  699. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  700. unsigned long flags;
  701. int ret;
  702. /******************** spin lock ********************/
  703. spin_lock_irqsave(lock, flags);
  704. ret = usbhsg_pipe_disable(uep);
  705. spin_unlock_irqrestore(lock, flags);
  706. /******************** spin unlock ******************/
  707. return ret;
  708. }
  709. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  710. gfp_t gfp_flags)
  711. {
  712. struct usbhsg_request *ureq;
  713. ureq = kzalloc(sizeof *ureq, gfp_flags);
  714. if (!ureq)
  715. return NULL;
  716. INIT_LIST_HEAD(&ureq->node);
  717. return &ureq->req;
  718. }
  719. static void usbhsg_ep_free_request(struct usb_ep *ep,
  720. struct usb_request *req)
  721. {
  722. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  723. WARN_ON(!list_empty(&ureq->node));
  724. kfree(ureq);
  725. }
  726. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  727. gfp_t gfp_flags)
  728. {
  729. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  730. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  731. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  732. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  733. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  734. unsigned long flags;
  735. int ret = 0;
  736. int is_locked;
  737. /*
  738. * CAUTION [*endpoint queue*]
  739. *
  740. * This function will be called from usb_request :: complete
  741. * or usb driver timing.
  742. * If this function is called from usb_request :: complete,
  743. * it is already under spinlock on this driver.
  744. * but it is called frm usb driver, this function should call spinlock.
  745. *
  746. * This function is using spin_trylock_irqsave to solve this issue.
  747. * if "is_locked" is 1, this mean this function lock it.
  748. * but if it is 0, this mean it is already under spin lock.
  749. * see also
  750. * CAUTION [*queue handler*]
  751. * CAUTION [*request complete*]
  752. */
  753. /******************** spin lock ********************/
  754. is_locked = spin_trylock_irqsave(lock, flags);
  755. /* param check */
  756. if (usbhsg_is_not_connected(gpriv) ||
  757. unlikely(!gpriv->driver) ||
  758. unlikely(!pipe))
  759. ret = -ESHUTDOWN;
  760. else
  761. usbhsg_queue_push(uep, ureq);
  762. if (is_locked)
  763. spin_unlock_irqrestore(lock, flags);
  764. /******************** spin unlock ******************/
  765. usbhsg_queue_prepare(uep);
  766. return ret;
  767. }
  768. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  769. {
  770. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  771. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  772. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  773. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  774. unsigned long flags;
  775. int is_locked;
  776. /*
  777. * see
  778. * CAUTION [*queue handler*]
  779. * CAUTION [*endpoint queue*]
  780. * CAUTION [*request complete*]
  781. */
  782. /******************** spin lock ********************/
  783. is_locked = spin_trylock_irqsave(lock, flags);
  784. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  785. if (is_locked)
  786. spin_unlock_irqrestore(lock, flags);
  787. /******************** spin unlock ******************/
  788. return 0;
  789. }
  790. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  791. {
  792. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  793. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  794. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  795. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  796. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  797. unsigned long flags;
  798. int ret = -EAGAIN;
  799. int is_locked;
  800. /*
  801. * see
  802. * CAUTION [*queue handler*]
  803. * CAUTION [*endpoint queue*]
  804. * CAUTION [*request complete*]
  805. */
  806. /******************** spin lock ********************/
  807. is_locked = spin_trylock_irqsave(lock, flags);
  808. if (!usbhsg_queue_get(uep)) {
  809. dev_dbg(dev, "set halt %d (pipe %d)\n",
  810. halt, usbhs_pipe_number(pipe));
  811. if (halt)
  812. usbhs_fifo_stall(pipe);
  813. else
  814. usbhs_fifo_disable(pipe);
  815. if (halt && wedge)
  816. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  817. else
  818. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  819. ret = 0;
  820. }
  821. if (is_locked)
  822. spin_unlock_irqrestore(lock, flags);
  823. /******************** spin unlock ******************/
  824. return ret;
  825. }
  826. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  827. {
  828. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  829. }
  830. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  831. {
  832. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  833. }
  834. static struct usb_ep_ops usbhsg_ep_ops = {
  835. .enable = usbhsg_ep_enable,
  836. .disable = usbhsg_ep_disable,
  837. .alloc_request = usbhsg_ep_alloc_request,
  838. .free_request = usbhsg_ep_free_request,
  839. .queue = usbhsg_ep_queue,
  840. .dequeue = usbhsg_ep_dequeue,
  841. .set_halt = usbhsg_ep_set_halt,
  842. .set_wedge = usbhsg_ep_set_wedge,
  843. };
  844. /*
  845. * usb module start/end
  846. */
  847. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  848. {
  849. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  850. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  851. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  852. struct device *dev = usbhs_priv_to_dev(priv);
  853. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  854. unsigned long flags;
  855. /******************** spin lock ********************/
  856. spin_lock_irqsave(lock, flags);
  857. /*
  858. * enable interrupt and systems if ready
  859. */
  860. usbhsg_status_set(gpriv, status);
  861. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  862. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  863. goto usbhsg_try_start_unlock;
  864. dev_dbg(dev, "start gadget\n");
  865. /*
  866. * pipe initialize and enable DCP
  867. */
  868. usbhs_pipe_init(priv);
  869. usbhsg_dcp_enable(dcp);
  870. /*
  871. * system config enble
  872. * - HI speed
  873. * - function
  874. * - usb module
  875. */
  876. usbhs_sys_hispeed_ctrl(priv, 1);
  877. usbhs_sys_function_ctrl(priv, 1);
  878. usbhs_sys_usb_ctrl(priv, 1);
  879. /*
  880. * enable irq callback
  881. */
  882. mod->irq_dev_state = usbhsg_irq_dev_state;
  883. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  884. mod->irq_empty = usbhsg_irq_empty;
  885. mod->irq_ready = usbhsg_irq_ready;
  886. mod->irq_bempsts = 0;
  887. mod->irq_brdysts = 0;
  888. usbhs_irq_callback_update(priv, mod);
  889. usbhsg_try_start_unlock:
  890. spin_unlock_irqrestore(lock, flags);
  891. /******************** spin unlock ********************/
  892. return 0;
  893. }
  894. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  895. {
  896. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  897. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  898. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  899. struct device *dev = usbhs_priv_to_dev(priv);
  900. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  901. unsigned long flags;
  902. /******************** spin lock ********************/
  903. spin_lock_irqsave(lock, flags);
  904. /*
  905. * disable interrupt and systems if 1st try
  906. */
  907. usbhsg_status_clr(gpriv, status);
  908. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  909. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  910. goto usbhsg_try_stop_unlock;
  911. /* disable all irq */
  912. mod->irq_dev_state = NULL;
  913. mod->irq_ctrl_stage = NULL;
  914. mod->irq_empty = NULL;
  915. mod->irq_ready = NULL;
  916. mod->irq_bempsts = 0;
  917. mod->irq_brdysts = 0;
  918. usbhs_irq_callback_update(priv, mod);
  919. usbhsg_dcp_disable(dcp);
  920. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  921. /* disable sys */
  922. usbhs_sys_hispeed_ctrl(priv, 0);
  923. usbhs_sys_function_ctrl(priv, 0);
  924. usbhs_sys_usb_ctrl(priv, 0);
  925. spin_unlock_irqrestore(lock, flags);
  926. /******************** spin unlock ********************/
  927. if (gpriv->driver &&
  928. gpriv->driver->disconnect)
  929. gpriv->driver->disconnect(&gpriv->gadget);
  930. dev_dbg(dev, "stop gadget\n");
  931. return 0;
  932. usbhsg_try_stop_unlock:
  933. spin_unlock_irqrestore(lock, flags);
  934. return 0;
  935. }
  936. /*
  937. *
  938. * linux usb function
  939. *
  940. */
  941. struct usbhsg_gpriv *the_controller;
  942. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  943. int (*bind)(struct usb_gadget *))
  944. {
  945. struct usbhsg_gpriv *gpriv = the_controller;
  946. struct usbhs_priv *priv;
  947. struct device *dev;
  948. int ret;
  949. if (!bind ||
  950. !driver ||
  951. !driver->setup ||
  952. driver->speed != USB_SPEED_HIGH)
  953. return -EINVAL;
  954. if (!gpriv)
  955. return -ENODEV;
  956. if (gpriv->driver)
  957. return -EBUSY;
  958. dev = usbhsg_gpriv_to_dev(gpriv);
  959. priv = usbhsg_gpriv_to_priv(gpriv);
  960. /* first hook up the driver ... */
  961. gpriv->driver = driver;
  962. gpriv->gadget.dev.driver = &driver->driver;
  963. ret = device_add(&gpriv->gadget.dev);
  964. if (ret) {
  965. dev_err(dev, "device_add error %d\n", ret);
  966. goto add_fail;
  967. }
  968. ret = bind(&gpriv->gadget);
  969. if (ret) {
  970. dev_err(dev, "bind to driver %s error %d\n",
  971. driver->driver.name, ret);
  972. goto bind_fail;
  973. }
  974. dev_dbg(dev, "bind %s\n", driver->driver.name);
  975. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  976. bind_fail:
  977. device_del(&gpriv->gadget.dev);
  978. add_fail:
  979. gpriv->driver = NULL;
  980. gpriv->gadget.dev.driver = NULL;
  981. return ret;
  982. }
  983. EXPORT_SYMBOL(usb_gadget_probe_driver);
  984. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  985. {
  986. struct usbhsg_gpriv *gpriv = the_controller;
  987. struct usbhs_priv *priv;
  988. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  989. if (!gpriv)
  990. return -ENODEV;
  991. if (!driver ||
  992. !driver->unbind ||
  993. driver != gpriv->driver)
  994. return -EINVAL;
  995. dev = usbhsg_gpriv_to_dev(gpriv);
  996. priv = usbhsg_gpriv_to_priv(gpriv);
  997. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  998. device_del(&gpriv->gadget.dev);
  999. gpriv->driver = NULL;
  1000. if (driver->disconnect)
  1001. driver->disconnect(&gpriv->gadget);
  1002. driver->unbind(&gpriv->gadget);
  1003. dev_dbg(dev, "unbind %s\n", driver->driver.name);
  1004. return 0;
  1005. }
  1006. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1007. /*
  1008. * usb gadget ops
  1009. */
  1010. static int usbhsg_get_frame(struct usb_gadget *gadget)
  1011. {
  1012. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  1013. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  1014. return usbhs_frame_get_num(priv);
  1015. }
  1016. static struct usb_gadget_ops usbhsg_gadget_ops = {
  1017. .get_frame = usbhsg_get_frame,
  1018. };
  1019. static int usbhsg_start(struct usbhs_priv *priv)
  1020. {
  1021. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  1022. }
  1023. static int usbhsg_stop(struct usbhs_priv *priv)
  1024. {
  1025. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  1026. }
  1027. int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  1028. {
  1029. struct usbhsg_gpriv *gpriv;
  1030. struct usbhsg_uep *uep;
  1031. struct device *dev = usbhs_priv_to_dev(priv);
  1032. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  1033. int i;
  1034. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  1035. if (!gpriv) {
  1036. dev_err(dev, "Could not allocate gadget priv\n");
  1037. return -ENOMEM;
  1038. }
  1039. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  1040. if (!uep) {
  1041. dev_err(dev, "Could not allocate ep\n");
  1042. goto usbhs_mod_gadget_probe_err_gpriv;
  1043. }
  1044. /*
  1045. * CAUTION
  1046. *
  1047. * There is no guarantee that it is possible to access usb module here.
  1048. * Don't accesses to it.
  1049. * The accesse will be enable after "usbhsg_start"
  1050. */
  1051. /*
  1052. * register itself
  1053. */
  1054. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  1055. /* init gpriv */
  1056. gpriv->mod.name = "gadget";
  1057. gpriv->mod.start = usbhsg_start;
  1058. gpriv->mod.stop = usbhsg_stop;
  1059. gpriv->uep = uep;
  1060. gpriv->uep_size = pipe_size;
  1061. usbhsg_status_init(gpriv);
  1062. /*
  1063. * init gadget
  1064. */
  1065. device_initialize(&gpriv->gadget.dev);
  1066. dev_set_name(&gpriv->gadget.dev, "gadget");
  1067. gpriv->gadget.dev.parent = dev;
  1068. gpriv->gadget.name = "renesas_usbhs_udc";
  1069. gpriv->gadget.ops = &usbhsg_gadget_ops;
  1070. gpriv->gadget.is_dualspeed = 1;
  1071. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  1072. /*
  1073. * init usb_ep
  1074. */
  1075. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  1076. uep->gpriv = gpriv;
  1077. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  1078. uep->ep.name = uep->ep_name;
  1079. uep->ep.ops = &usbhsg_ep_ops;
  1080. INIT_LIST_HEAD(&uep->ep.ep_list);
  1081. INIT_LIST_HEAD(&uep->list);
  1082. /* init DCP */
  1083. if (usbhsg_is_dcp(uep)) {
  1084. gpriv->gadget.ep0 = &uep->ep;
  1085. uep->ep.maxpacket = 64;
  1086. }
  1087. /* init normal pipe */
  1088. else {
  1089. uep->ep.maxpacket = 512;
  1090. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  1091. }
  1092. }
  1093. the_controller = gpriv;
  1094. dev_info(dev, "gadget probed\n");
  1095. return 0;
  1096. usbhs_mod_gadget_probe_err_gpriv:
  1097. kfree(gpriv);
  1098. return -ENOMEM;
  1099. }
  1100. void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  1101. {
  1102. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  1103. kfree(gpriv);
  1104. }