mod_gadget.c 28 KB

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