mod_gadget.c 28 KB

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