mod_gadget.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include "common.h"
  23. /*
  24. * struct
  25. */
  26. struct usbhsg_request {
  27. struct usb_request req;
  28. struct list_head node;
  29. };
  30. #define EP_NAME_SIZE 8
  31. struct usbhsg_gpriv;
  32. struct usbhsg_pipe_handle;
  33. struct usbhsg_uep {
  34. struct usb_ep ep;
  35. struct usbhs_pipe *pipe;
  36. struct list_head list;
  37. char ep_name[EP_NAME_SIZE];
  38. struct usbhsg_gpriv *gpriv;
  39. struct usbhsg_pipe_handle *handler;
  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. u32 status;
  48. #define USBHSG_STATUS_STARTED (1 << 0)
  49. #define USBHSG_STATUS_REGISTERD (1 << 1)
  50. #define USBHSG_STATUS_WEDGE (1 << 2)
  51. };
  52. struct usbhsg_pipe_handle {
  53. int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
  54. int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
  55. void (*irq_mask)(struct usbhsg_uep *uep, int enable);
  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, pos = (g)->uep; \
  75. i < (g)->uep_size; \
  76. i++, pos = (g)->uep + 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_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
  87. #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
  88. #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
  89. #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
  90. #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
  91. #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
  92. #define usbhsg_uep_to_pipe(u) ((u)->pipe)
  93. #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
  94. #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
  95. #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
  96. /* status */
  97. #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
  98. #define usbhsg_status_set(gp, b) (gp->status |= b)
  99. #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
  100. #define usbhsg_status_has(gp, b) (gp->status & b)
  101. /*
  102. * list push/pop
  103. */
  104. static void usbhsg_queue_push(struct usbhsg_uep *uep,
  105. struct usbhsg_request *ureq)
  106. {
  107. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  108. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  109. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  110. /*
  111. ********* assume under spin lock *********
  112. */
  113. list_del_init(&ureq->node);
  114. list_add_tail(&ureq->node, &uep->list);
  115. ureq->req.actual = 0;
  116. ureq->req.status = -EINPROGRESS;
  117. dev_dbg(dev, "pipe %d : queue push (%d)\n",
  118. usbhs_pipe_number(pipe),
  119. ureq->req.length);
  120. }
  121. static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
  122. {
  123. /*
  124. ********* assume under spin lock *********
  125. */
  126. if (list_empty(&uep->list))
  127. return NULL;
  128. return list_entry(uep->list.next, struct usbhsg_request, node);
  129. }
  130. #define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
  131. #define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
  132. static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
  133. {
  134. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  135. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  136. struct usbhsg_request *ureq;
  137. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  138. unsigned long flags;
  139. int is_locked;
  140. int ret = 0;
  141. if (!uep->handler) {
  142. dev_err(dev, "no handler function\n");
  143. return -EIO;
  144. }
  145. /*
  146. * CAUTION [*queue handler*]
  147. *
  148. * This function will be called for start/restart queue operation.
  149. * OTOH the most much worry for USB driver is spinlock nest.
  150. * Specially it are
  151. * - usb_ep_ops :: queue
  152. * - usb_request :: complete
  153. *
  154. * But the caller of this function need not care about spinlock.
  155. * This function is using spin_trylock_irqsave for it.
  156. * if "is_locked" is 1, this mean this function lock it.
  157. * but if it is 0, this mean it is already under spin lock.
  158. * see also
  159. * CAUTION [*endpoint queue*]
  160. * CAUTION [*request complete*]
  161. */
  162. /****************** spin try lock *******************/
  163. is_locked = spin_trylock_irqsave(lock, flags);
  164. ureq = usbhsg_queue_get(uep);
  165. if (ureq) {
  166. if (prepare)
  167. ret = uep->handler->prepare(uep, ureq);
  168. else
  169. ret = uep->handler->try_run(uep, ureq);
  170. }
  171. if (is_locked)
  172. spin_unlock_irqrestore(lock, flags);
  173. /******************** spin unlock ******************/
  174. return ret;
  175. }
  176. static void usbhsg_queue_pop(struct usbhsg_uep *uep,
  177. struct usbhsg_request *ureq,
  178. int status)
  179. {
  180. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  181. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  182. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  183. /*
  184. ********* assume under spin lock *********
  185. */
  186. /*
  187. * CAUTION [*request complete*]
  188. *
  189. * There is a possibility not to be called in correct order
  190. * if "complete" is called without spinlock.
  191. *
  192. * So, this function assume it is under spinlock,
  193. * and call usb_request :: complete.
  194. *
  195. * But this "complete" will push next usb_request.
  196. * It mean "usb_ep_ops :: queue" which is using spinlock is called
  197. * under spinlock.
  198. *
  199. * To avoid dead-lock, this driver is using spin_trylock.
  200. * CAUTION [*endpoint queue*]
  201. * CAUTION [*queue handler*]
  202. */
  203. dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
  204. list_del_init(&ureq->node);
  205. ureq->req.status = status;
  206. ureq->req.complete(&uep->ep, &ureq->req);
  207. /* more request ? */
  208. if (0 == status)
  209. usbhsg_queue_prepare(uep);
  210. }
  211. /*
  212. * irq enable/disable function
  213. */
  214. #define usbhsg_irq_callback_ctrl(uep, status, enable) \
  215. ({ \
  216. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
  217. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
  218. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
  219. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  220. if (!mod) \
  221. return; \
  222. if (enable) \
  223. mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
  224. else \
  225. mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
  226. usbhs_irq_callback_update(priv, mod); \
  227. })
  228. static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
  229. {
  230. usbhsg_irq_callback_ctrl(uep, bempsts, enable);
  231. }
  232. static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
  233. {
  234. usbhsg_irq_callback_ctrl(uep, brdysts, enable);
  235. }
  236. /*
  237. * handler function
  238. */
  239. static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
  240. struct usbhsg_request *ureq)
  241. {
  242. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  243. /*
  244. ********* assume under spin lock *********
  245. */
  246. usbhs_dcp_control_transfer_done(pipe);
  247. usbhsg_queue_pop(uep, ureq, 0);
  248. return 0;
  249. }
  250. static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
  251. struct usbhsg_request *ureq)
  252. {
  253. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  254. struct usb_request *req = &ureq->req;
  255. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  256. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  257. void *buf;
  258. int remainder, send;
  259. int is_done = 0;
  260. int enable;
  261. int maxp;
  262. /*
  263. ********* assume under spin lock *********
  264. */
  265. maxp = usbhs_pipe_get_maxpacket(pipe);
  266. buf = req->buf + req->actual;
  267. remainder = req->length - req->actual;
  268. send = usbhs_fifo_write(pipe, buf, remainder);
  269. /*
  270. * send < 0 : pipe busy
  271. * send = 0 : send zero packet
  272. * send > 0 : send data
  273. *
  274. * send <= max_packet
  275. */
  276. if (send > 0)
  277. req->actual += send;
  278. /* send all packet ? */
  279. if (send < remainder)
  280. is_done = 0; /* there are remainder data */
  281. else if (send < maxp)
  282. is_done = 1; /* short packet */
  283. else
  284. is_done = !req->zero; /* send zero packet ? */
  285. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  286. usbhs_pipe_number(pipe),
  287. remainder, send, is_done, req->zero);
  288. /*
  289. * enable interrupt and send again in irq handler
  290. * if it still have remainder data which should be sent.
  291. */
  292. enable = !is_done;
  293. uep->handler->irq_mask(uep, enable);
  294. /*
  295. * usbhs_fifo_enable execute
  296. * - after callback_update,
  297. * - before queue_pop / stage_end
  298. */
  299. usbhs_fifo_enable(pipe);
  300. /*
  301. * all data were sent ?
  302. */
  303. if (is_done) {
  304. /* it care below call in
  305. "function mode" */
  306. if (usbhsg_is_dcp(uep))
  307. usbhs_dcp_control_transfer_done(pipe);
  308. usbhsg_queue_pop(uep, ureq, 0);
  309. }
  310. return 0;
  311. }
  312. static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
  313. struct usbhsg_request *ureq)
  314. {
  315. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  316. /*
  317. ********* assume under spin lock *********
  318. */
  319. usbhs_fifo_prepare_write(pipe);
  320. usbhsg_try_run_send_packet(uep, ureq);
  321. return 0;
  322. }
  323. static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
  324. struct usbhsg_request *ureq)
  325. {
  326. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  327. struct usb_request *req = &ureq->req;
  328. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  329. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  330. void *buf;
  331. int maxp;
  332. int remainder, recv;
  333. int is_done = 0;
  334. /*
  335. ********* assume under spin lock *********
  336. */
  337. maxp = usbhs_pipe_get_maxpacket(pipe);
  338. buf = req->buf + req->actual;
  339. remainder = req->length - req->actual;
  340. recv = usbhs_fifo_read(pipe, buf, remainder);
  341. /*
  342. * recv < 0 : pipe busy
  343. * recv >= 0 : receive data
  344. *
  345. * recv <= max_packet
  346. */
  347. if (recv < 0)
  348. return -EBUSY;
  349. /* update parameters */
  350. req->actual += recv;
  351. if ((recv == remainder) || /* receive all data */
  352. (recv < maxp)) /* short packet */
  353. is_done = 1;
  354. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  355. usbhs_pipe_number(pipe),
  356. remainder, recv, is_done, req->zero);
  357. /* read all data ? */
  358. if (is_done) {
  359. int disable = 0;
  360. uep->handler->irq_mask(uep, disable);
  361. usbhs_fifo_disable(pipe);
  362. usbhsg_queue_pop(uep, ureq, 0);
  363. }
  364. return 0;
  365. }
  366. static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
  367. struct usbhsg_request *ureq)
  368. {
  369. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  370. int enable = 1;
  371. int ret;
  372. /*
  373. ********* assume under spin lock *********
  374. */
  375. ret = usbhs_fifo_prepare_read(pipe);
  376. if (ret < 0)
  377. return ret;
  378. /*
  379. * data will be read in interrupt handler
  380. */
  381. uep->handler->irq_mask(uep, enable);
  382. return ret;
  383. }
  384. static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
  385. .prepare = usbhsg_prepare_send_packet,
  386. .try_run = usbhsg_try_run_send_packet,
  387. .irq_mask = usbhsg_irq_empty_ctrl,
  388. };
  389. static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
  390. .prepare = usbhsg_prepare_send_packet,
  391. .try_run = usbhsg_try_run_send_packet,
  392. .irq_mask = usbhsg_irq_ready_ctrl,
  393. };
  394. static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
  395. .prepare = usbhsg_prepare_receive_packet,
  396. .try_run = usbhsg_try_run_receive_packet,
  397. .irq_mask = usbhsg_irq_ready_ctrl,
  398. };
  399. static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
  400. .prepare = usbhsg_try_run_ctrl_stage_end,
  401. .try_run = usbhsg_try_run_ctrl_stage_end,
  402. };
  403. /*
  404. * DCP pipe can NOT use "ready interrupt" for "send"
  405. * it should use "empty" interrupt.
  406. * see
  407. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  408. *
  409. * on the other hand, normal pipe can use "ready interrupt" for "send"
  410. * even though it is single/double buffer
  411. */
  412. #define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
  413. #define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
  414. #define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
  415. #define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
  416. /*
  417. * USB_TYPE_STANDARD / clear feature functions
  418. */
  419. static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
  420. struct usbhsg_uep *uep,
  421. struct usb_ctrlrequest *ctrl)
  422. {
  423. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  424. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  425. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  426. usbhs_dcp_control_transfer_done(pipe);
  427. return 0;
  428. }
  429. static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
  430. struct usbhsg_uep *uep,
  431. struct usb_ctrlrequest *ctrl)
  432. {
  433. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  434. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  435. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
  436. usbhs_fifo_disable(pipe);
  437. usbhs_pipe_clear_sequence(pipe);
  438. usbhs_fifo_enable(pipe);
  439. }
  440. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  441. usbhsg_queue_prepare(uep);
  442. return 0;
  443. }
  444. struct usbhsg_recip_handle req_clear_feature = {
  445. .name = "clear feature",
  446. .device = usbhsg_recip_handler_std_control_done,
  447. .interface = usbhsg_recip_handler_std_control_done,
  448. .endpoint = usbhsg_recip_handler_std_clear_endpoint,
  449. };
  450. /*
  451. * USB_TYPE handler
  452. */
  453. static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
  454. struct usbhsg_recip_handle *handler,
  455. struct usb_ctrlrequest *ctrl)
  456. {
  457. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  458. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  459. struct usbhsg_uep *uep;
  460. int recip = ctrl->bRequestType & USB_RECIP_MASK;
  461. int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  462. int ret;
  463. int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  464. struct usb_ctrlrequest *ctrl);
  465. char *msg;
  466. uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
  467. switch (recip) {
  468. case USB_RECIP_DEVICE:
  469. msg = "DEVICE";
  470. func = handler->device;
  471. break;
  472. case USB_RECIP_INTERFACE:
  473. msg = "INTERFACE";
  474. func = handler->interface;
  475. break;
  476. case USB_RECIP_ENDPOINT:
  477. msg = "ENDPOINT";
  478. func = handler->endpoint;
  479. break;
  480. default:
  481. dev_warn(dev, "unsupported RECIP(%d)\n", recip);
  482. func = NULL;
  483. ret = -EINVAL;
  484. }
  485. if (func) {
  486. dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
  487. ret = func(priv, uep, ctrl);
  488. }
  489. return ret;
  490. }
  491. /*
  492. * irq functions
  493. *
  494. * it will be called from usbhs_interrupt
  495. */
  496. static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
  497. struct usbhs_irq_state *irq_state)
  498. {
  499. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  500. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  501. gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
  502. dev_dbg(dev, "state = %x : speed : %d\n",
  503. usbhs_status_get_device_state(irq_state),
  504. gpriv->gadget.speed);
  505. return 0;
  506. }
  507. static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
  508. struct usbhs_irq_state *irq_state)
  509. {
  510. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  511. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  512. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  513. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  514. struct usb_ctrlrequest ctrl;
  515. struct usbhsg_recip_handle *recip_handler = NULL;
  516. int stage = usbhs_status_get_ctrl_stage(irq_state);
  517. int ret = 0;
  518. dev_dbg(dev, "stage = %d\n", stage);
  519. /*
  520. * see Manual
  521. *
  522. * "Operation"
  523. * - "Interrupt Function"
  524. * - "Control Transfer Stage Transition Interrupt"
  525. * - Fig. "Control Transfer Stage Transitions"
  526. */
  527. switch (stage) {
  528. case READ_DATA_STAGE:
  529. dcp->handler = &usbhsg_handler_send_ctrl;
  530. break;
  531. case WRITE_DATA_STAGE:
  532. dcp->handler = &usbhsg_handler_recv_ctrl;
  533. break;
  534. case NODATA_STATUS_STAGE:
  535. dcp->handler = &usbhsg_handler_ctrl_stage_end;
  536. break;
  537. default:
  538. return ret;
  539. }
  540. /*
  541. * get usb request
  542. */
  543. usbhs_usbreq_get_val(priv, &ctrl);
  544. switch (ctrl.bRequestType & USB_TYPE_MASK) {
  545. case USB_TYPE_STANDARD:
  546. switch (ctrl.bRequest) {
  547. case USB_REQ_CLEAR_FEATURE:
  548. recip_handler = &req_clear_feature;
  549. break;
  550. }
  551. }
  552. /*
  553. * setup stage / run recip
  554. */
  555. if (recip_handler)
  556. ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
  557. else
  558. ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
  559. if (ret < 0)
  560. usbhs_fifo_stall(pipe);
  561. return ret;
  562. }
  563. static int usbhsg_irq_empty(struct usbhs_priv *priv,
  564. struct usbhs_irq_state *irq_state)
  565. {
  566. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  567. struct usbhsg_uep *uep;
  568. struct usbhs_pipe *pipe;
  569. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  570. int i, ret;
  571. if (!irq_state->bempsts) {
  572. dev_err(dev, "debug %s !!\n", __func__);
  573. return -EIO;
  574. }
  575. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  576. /*
  577. * search interrupted "pipe"
  578. * not "uep".
  579. */
  580. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  581. if (!(irq_state->bempsts & (1 << i)))
  582. continue;
  583. uep = usbhsg_pipe_to_uep(pipe);
  584. ret = usbhsg_queue_handle(uep);
  585. if (ret < 0)
  586. dev_err(dev, "send error %d : %d\n", i, ret);
  587. }
  588. return 0;
  589. }
  590. static int usbhsg_irq_ready(struct usbhs_priv *priv,
  591. struct usbhs_irq_state *irq_state)
  592. {
  593. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  594. struct usbhsg_uep *uep;
  595. struct usbhs_pipe *pipe;
  596. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  597. int i, ret;
  598. if (!irq_state->brdysts) {
  599. dev_err(dev, "debug %s !!\n", __func__);
  600. return -EIO;
  601. }
  602. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  603. /*
  604. * search interrupted "pipe"
  605. * not "uep".
  606. */
  607. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  608. if (!(irq_state->brdysts & (1 << i)))
  609. continue;
  610. uep = usbhsg_pipe_to_uep(pipe);
  611. ret = usbhsg_queue_handle(uep);
  612. if (ret < 0)
  613. dev_err(dev, "receive error %d : %d\n", i, ret);
  614. }
  615. return 0;
  616. }
  617. /*
  618. *
  619. * usb_dcp_ops
  620. *
  621. */
  622. static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
  623. {
  624. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  625. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  626. struct usbhs_pipe *pipe;
  627. /*
  628. ********* assume under spin lock *********
  629. */
  630. pipe = usbhs_dcp_malloc(priv);
  631. if (!pipe)
  632. return -EIO;
  633. uep->pipe = pipe;
  634. uep->pipe->mod_private = uep;
  635. INIT_LIST_HEAD(&uep->list);
  636. return 0;
  637. }
  638. #define usbhsg_dcp_disable usbhsg_pipe_disable
  639. static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
  640. {
  641. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  642. struct usbhsg_request *ureq;
  643. int disable = 0;
  644. /*
  645. ********* assume under spin lock *********
  646. */
  647. usbhs_fifo_disable(pipe);
  648. /*
  649. * disable pipe irq
  650. */
  651. usbhsg_irq_empty_ctrl(uep, disable);
  652. usbhsg_irq_ready_ctrl(uep, disable);
  653. while (1) {
  654. ureq = usbhsg_queue_get(uep);
  655. if (!ureq)
  656. break;
  657. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  658. }
  659. return 0;
  660. }
  661. static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
  662. {
  663. int i;
  664. struct usbhsg_uep *uep;
  665. usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
  666. uep->pipe = NULL;
  667. }
  668. /*
  669. *
  670. * usb_ep_ops
  671. *
  672. */
  673. static int usbhsg_ep_enable(struct usb_ep *ep,
  674. const struct usb_endpoint_descriptor *desc)
  675. {
  676. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  677. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  678. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  679. struct usbhs_pipe *pipe;
  680. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  681. unsigned long flags;
  682. int ret = -EIO;
  683. /*
  684. * if it already have pipe,
  685. * nothing to do
  686. */
  687. if (uep->pipe)
  688. return 0;
  689. /******************** spin lock ********************/
  690. spin_lock_irqsave(lock, flags);
  691. pipe = usbhs_pipe_malloc(priv, desc);
  692. if (pipe) {
  693. uep->pipe = pipe;
  694. pipe->mod_private = uep;
  695. INIT_LIST_HEAD(&uep->list);
  696. if (usb_endpoint_dir_in(desc))
  697. uep->handler = &usbhsg_handler_send_packet;
  698. else
  699. uep->handler = &usbhsg_handler_recv_packet;
  700. ret = 0;
  701. }
  702. spin_unlock_irqrestore(lock, flags);
  703. /******************** spin unlock ******************/
  704. return ret;
  705. }
  706. static int usbhsg_ep_disable(struct usb_ep *ep)
  707. {
  708. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  709. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  710. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  711. unsigned long flags;
  712. int ret;
  713. /******************** spin lock ********************/
  714. spin_lock_irqsave(lock, flags);
  715. ret = usbhsg_pipe_disable(uep);
  716. spin_unlock_irqrestore(lock, flags);
  717. /******************** spin unlock ******************/
  718. return ret;
  719. }
  720. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  721. gfp_t gfp_flags)
  722. {
  723. struct usbhsg_request *ureq;
  724. ureq = kzalloc(sizeof *ureq, gfp_flags);
  725. if (!ureq)
  726. return NULL;
  727. INIT_LIST_HEAD(&ureq->node);
  728. return &ureq->req;
  729. }
  730. static void usbhsg_ep_free_request(struct usb_ep *ep,
  731. struct usb_request *req)
  732. {
  733. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  734. WARN_ON(!list_empty(&ureq->node));
  735. kfree(ureq);
  736. }
  737. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  738. gfp_t gfp_flags)
  739. {
  740. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  741. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  742. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  743. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  744. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  745. unsigned long flags;
  746. int ret = 0;
  747. int is_locked;
  748. /*
  749. * CAUTION [*endpoint queue*]
  750. *
  751. * This function will be called from usb_request :: complete
  752. * or usb driver timing.
  753. * If this function is called from usb_request :: complete,
  754. * it is already under spinlock on this driver.
  755. * but it is called frm usb driver, this function should call spinlock.
  756. *
  757. * This function is using spin_trylock_irqsave to solve this issue.
  758. * if "is_locked" is 1, this mean this function lock it.
  759. * but if it is 0, this mean it is already under spin lock.
  760. * see also
  761. * CAUTION [*queue handler*]
  762. * CAUTION [*request complete*]
  763. */
  764. /******************** spin lock ********************/
  765. is_locked = spin_trylock_irqsave(lock, flags);
  766. /* param check */
  767. if (usbhsg_is_not_connected(gpriv) ||
  768. unlikely(!gpriv->driver) ||
  769. unlikely(!pipe))
  770. ret = -ESHUTDOWN;
  771. else
  772. usbhsg_queue_push(uep, ureq);
  773. if (is_locked)
  774. spin_unlock_irqrestore(lock, flags);
  775. /******************** spin unlock ******************/
  776. usbhsg_queue_prepare(uep);
  777. return ret;
  778. }
  779. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  780. {
  781. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  782. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  783. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  784. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  785. unsigned long flags;
  786. int is_locked;
  787. /*
  788. * see
  789. * CAUTION [*queue handler*]
  790. * CAUTION [*endpoint queue*]
  791. * CAUTION [*request complete*]
  792. */
  793. /******************** spin lock ********************/
  794. is_locked = spin_trylock_irqsave(lock, flags);
  795. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  796. if (is_locked)
  797. spin_unlock_irqrestore(lock, flags);
  798. /******************** spin unlock ******************/
  799. return 0;
  800. }
  801. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  802. {
  803. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  804. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  805. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  806. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  807. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  808. unsigned long flags;
  809. int ret = -EAGAIN;
  810. int is_locked;
  811. /*
  812. * see
  813. * CAUTION [*queue handler*]
  814. * CAUTION [*endpoint queue*]
  815. * CAUTION [*request complete*]
  816. */
  817. /******************** spin lock ********************/
  818. is_locked = spin_trylock_irqsave(lock, flags);
  819. if (!usbhsg_queue_get(uep)) {
  820. dev_dbg(dev, "set halt %d (pipe %d)\n",
  821. halt, usbhs_pipe_number(pipe));
  822. if (halt)
  823. usbhs_fifo_stall(pipe);
  824. else
  825. usbhs_fifo_disable(pipe);
  826. if (halt && wedge)
  827. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  828. else
  829. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  830. ret = 0;
  831. }
  832. if (is_locked)
  833. spin_unlock_irqrestore(lock, flags);
  834. /******************** spin unlock ******************/
  835. return ret;
  836. }
  837. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  838. {
  839. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  840. }
  841. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  842. {
  843. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  844. }
  845. static struct usb_ep_ops usbhsg_ep_ops = {
  846. .enable = usbhsg_ep_enable,
  847. .disable = usbhsg_ep_disable,
  848. .alloc_request = usbhsg_ep_alloc_request,
  849. .free_request = usbhsg_ep_free_request,
  850. .queue = usbhsg_ep_queue,
  851. .dequeue = usbhsg_ep_dequeue,
  852. .set_halt = usbhsg_ep_set_halt,
  853. .set_wedge = usbhsg_ep_set_wedge,
  854. };
  855. /*
  856. * usb module start/end
  857. */
  858. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  859. {
  860. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  861. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  862. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  863. struct device *dev = usbhs_priv_to_dev(priv);
  864. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  865. unsigned long flags;
  866. /******************** spin lock ********************/
  867. spin_lock_irqsave(lock, flags);
  868. /*
  869. * enable interrupt and systems if ready
  870. */
  871. usbhsg_status_set(gpriv, status);
  872. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  873. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  874. goto usbhsg_try_start_unlock;
  875. dev_dbg(dev, "start gadget\n");
  876. /*
  877. * pipe initialize and enable DCP
  878. */
  879. usbhs_pipe_init(priv);
  880. usbhsg_uep_init(gpriv);
  881. usbhsg_dcp_enable(dcp);
  882. /*
  883. * system config enble
  884. * - HI speed
  885. * - function
  886. * - usb module
  887. */
  888. usbhs_sys_hispeed_ctrl(priv, 1);
  889. usbhs_sys_function_ctrl(priv, 1);
  890. usbhs_sys_usb_ctrl(priv, 1);
  891. /*
  892. * enable irq callback
  893. */
  894. mod->irq_dev_state = usbhsg_irq_dev_state;
  895. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  896. mod->irq_empty = usbhsg_irq_empty;
  897. mod->irq_ready = usbhsg_irq_ready;
  898. mod->irq_bempsts = 0;
  899. mod->irq_brdysts = 0;
  900. usbhs_irq_callback_update(priv, mod);
  901. usbhsg_try_start_unlock:
  902. spin_unlock_irqrestore(lock, flags);
  903. /******************** spin unlock ********************/
  904. return 0;
  905. }
  906. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  907. {
  908. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  909. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  910. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  911. struct device *dev = usbhs_priv_to_dev(priv);
  912. spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
  913. unsigned long flags;
  914. /******************** spin lock ********************/
  915. spin_lock_irqsave(lock, flags);
  916. /*
  917. * disable interrupt and systems if 1st try
  918. */
  919. usbhsg_status_clr(gpriv, status);
  920. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  921. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  922. goto usbhsg_try_stop_unlock;
  923. /* disable all irq */
  924. mod->irq_dev_state = NULL;
  925. mod->irq_ctrl_stage = NULL;
  926. mod->irq_empty = NULL;
  927. mod->irq_ready = NULL;
  928. mod->irq_bempsts = 0;
  929. mod->irq_brdysts = 0;
  930. usbhs_irq_callback_update(priv, mod);
  931. usbhsg_dcp_disable(dcp);
  932. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  933. /* disable sys */
  934. usbhs_sys_hispeed_ctrl(priv, 0);
  935. usbhs_sys_function_ctrl(priv, 0);
  936. usbhs_sys_usb_ctrl(priv, 0);
  937. spin_unlock_irqrestore(lock, flags);
  938. /******************** spin unlock ********************/
  939. if (gpriv->driver &&
  940. gpriv->driver->disconnect)
  941. gpriv->driver->disconnect(&gpriv->gadget);
  942. dev_dbg(dev, "stop gadget\n");
  943. return 0;
  944. usbhsg_try_stop_unlock:
  945. spin_unlock_irqrestore(lock, flags);
  946. return 0;
  947. }
  948. /*
  949. *
  950. * linux usb function
  951. *
  952. */
  953. struct usbhsg_gpriv *the_controller;
  954. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  955. int (*bind)(struct usb_gadget *))
  956. {
  957. struct usbhsg_gpriv *gpriv = the_controller;
  958. struct usbhs_priv *priv;
  959. struct device *dev;
  960. int ret;
  961. if (!bind ||
  962. !driver ||
  963. !driver->setup ||
  964. driver->speed != USB_SPEED_HIGH)
  965. return -EINVAL;
  966. if (!gpriv)
  967. return -ENODEV;
  968. if (gpriv->driver)
  969. return -EBUSY;
  970. dev = usbhsg_gpriv_to_dev(gpriv);
  971. priv = usbhsg_gpriv_to_priv(gpriv);
  972. /* first hook up the driver ... */
  973. gpriv->driver = driver;
  974. gpriv->gadget.dev.driver = &driver->driver;
  975. ret = device_add(&gpriv->gadget.dev);
  976. if (ret) {
  977. dev_err(dev, "device_add error %d\n", ret);
  978. goto add_fail;
  979. }
  980. ret = bind(&gpriv->gadget);
  981. if (ret) {
  982. dev_err(dev, "bind to driver %s error %d\n",
  983. driver->driver.name, ret);
  984. goto bind_fail;
  985. }
  986. dev_dbg(dev, "bind %s\n", driver->driver.name);
  987. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  988. bind_fail:
  989. device_del(&gpriv->gadget.dev);
  990. add_fail:
  991. gpriv->driver = NULL;
  992. gpriv->gadget.dev.driver = NULL;
  993. return ret;
  994. }
  995. EXPORT_SYMBOL(usb_gadget_probe_driver);
  996. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  997. {
  998. struct usbhsg_gpriv *gpriv = the_controller;
  999. struct usbhs_priv *priv;
  1000. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  1001. if (!gpriv)
  1002. return -ENODEV;
  1003. if (!driver ||
  1004. !driver->unbind ||
  1005. driver != gpriv->driver)
  1006. return -EINVAL;
  1007. dev = usbhsg_gpriv_to_dev(gpriv);
  1008. priv = usbhsg_gpriv_to_priv(gpriv);
  1009. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  1010. device_del(&gpriv->gadget.dev);
  1011. gpriv->driver = NULL;
  1012. if (driver->disconnect)
  1013. driver->disconnect(&gpriv->gadget);
  1014. driver->unbind(&gpriv->gadget);
  1015. dev_dbg(dev, "unbind %s\n", driver->driver.name);
  1016. return 0;
  1017. }
  1018. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1019. /*
  1020. * usb gadget ops
  1021. */
  1022. static int usbhsg_get_frame(struct usb_gadget *gadget)
  1023. {
  1024. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  1025. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  1026. return usbhs_frame_get_num(priv);
  1027. }
  1028. static struct usb_gadget_ops usbhsg_gadget_ops = {
  1029. .get_frame = usbhsg_get_frame,
  1030. };
  1031. static int usbhsg_start(struct usbhs_priv *priv)
  1032. {
  1033. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  1034. }
  1035. static int usbhsg_stop(struct usbhs_priv *priv)
  1036. {
  1037. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  1038. }
  1039. int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  1040. {
  1041. struct usbhsg_gpriv *gpriv;
  1042. struct usbhsg_uep *uep;
  1043. struct device *dev = usbhs_priv_to_dev(priv);
  1044. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  1045. int i;
  1046. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  1047. if (!gpriv) {
  1048. dev_err(dev, "Could not allocate gadget priv\n");
  1049. return -ENOMEM;
  1050. }
  1051. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  1052. if (!uep) {
  1053. dev_err(dev, "Could not allocate ep\n");
  1054. goto usbhs_mod_gadget_probe_err_gpriv;
  1055. }
  1056. /*
  1057. * CAUTION
  1058. *
  1059. * There is no guarantee that it is possible to access usb module here.
  1060. * Don't accesses to it.
  1061. * The accesse will be enable after "usbhsg_start"
  1062. */
  1063. /*
  1064. * register itself
  1065. */
  1066. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  1067. /* init gpriv */
  1068. gpriv->mod.name = "gadget";
  1069. gpriv->mod.start = usbhsg_start;
  1070. gpriv->mod.stop = usbhsg_stop;
  1071. gpriv->uep = uep;
  1072. gpriv->uep_size = pipe_size;
  1073. usbhsg_status_init(gpriv);
  1074. /*
  1075. * init gadget
  1076. */
  1077. device_initialize(&gpriv->gadget.dev);
  1078. dev_set_name(&gpriv->gadget.dev, "gadget");
  1079. gpriv->gadget.dev.parent = dev;
  1080. gpriv->gadget.name = "renesas_usbhs_udc";
  1081. gpriv->gadget.ops = &usbhsg_gadget_ops;
  1082. gpriv->gadget.is_dualspeed = 1;
  1083. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  1084. /*
  1085. * init usb_ep
  1086. */
  1087. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  1088. uep->gpriv = gpriv;
  1089. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  1090. uep->ep.name = uep->ep_name;
  1091. uep->ep.ops = &usbhsg_ep_ops;
  1092. INIT_LIST_HEAD(&uep->ep.ep_list);
  1093. INIT_LIST_HEAD(&uep->list);
  1094. /* init DCP */
  1095. if (usbhsg_is_dcp(uep)) {
  1096. gpriv->gadget.ep0 = &uep->ep;
  1097. uep->ep.maxpacket = 64;
  1098. }
  1099. /* init normal pipe */
  1100. else {
  1101. uep->ep.maxpacket = 512;
  1102. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  1103. }
  1104. }
  1105. the_controller = gpriv;
  1106. dev_info(dev, "gadget probed\n");
  1107. return 0;
  1108. usbhs_mod_gadget_probe_err_gpriv:
  1109. kfree(gpriv);
  1110. return -ENOMEM;
  1111. }
  1112. void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  1113. {
  1114. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  1115. kfree(gpriv);
  1116. }