mod_gadget.c 33 KB

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