mod_gadget.c 32 KB

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