mod_gadget.c 28 KB

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