mod_gadget.c 27 KB

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