mod_gadget.c 28 KB

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