isp1760-udc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. /*
  2. * Driver for the NXP ISP1761 device controller
  3. *
  4. * Copyright 2014 Ideas on Board Oy
  5. *
  6. * Contacts:
  7. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. */
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/timer.h>
  20. #include <linux/usb.h>
  21. #include "isp1760-core.h"
  22. #include "isp1760-regs.h"
  23. #include "isp1760-udc.h"
  24. #define ISP1760_VBUS_POLL_INTERVAL msecs_to_jiffies(500)
  25. struct isp1760_request {
  26. struct usb_request req;
  27. struct list_head queue;
  28. struct isp1760_ep *ep;
  29. unsigned int packet_size;
  30. };
  31. static inline struct isp1760_udc *gadget_to_udc(struct usb_gadget *gadget)
  32. {
  33. return container_of(gadget, struct isp1760_udc, gadget);
  34. }
  35. static inline struct isp1760_ep *ep_to_udc_ep(struct usb_ep *ep)
  36. {
  37. return container_of(ep, struct isp1760_ep, ep);
  38. }
  39. static inline struct isp1760_request *req_to_udc_req(struct usb_request *req)
  40. {
  41. return container_of(req, struct isp1760_request, req);
  42. }
  43. static inline u32 isp1760_udc_read(struct isp1760_udc *udc, u16 reg)
  44. {
  45. return isp1760_read32(udc->regs, reg);
  46. }
  47. static inline void isp1760_udc_write(struct isp1760_udc *udc, u16 reg, u32 val)
  48. {
  49. isp1760_write32(udc->regs, reg, val);
  50. }
  51. /* -----------------------------------------------------------------------------
  52. * Endpoint Management
  53. */
  54. static struct isp1760_ep *isp1760_udc_find_ep(struct isp1760_udc *udc,
  55. u16 index)
  56. {
  57. unsigned int i;
  58. if (index == 0)
  59. return &udc->ep[0];
  60. for (i = 1; i < ARRAY_SIZE(udc->ep); ++i) {
  61. if (udc->ep[i].addr == index)
  62. return udc->ep[i].desc ? &udc->ep[i] : NULL;
  63. }
  64. return NULL;
  65. }
  66. static void __isp1760_udc_select_ep(struct isp1760_ep *ep, int dir)
  67. {
  68. isp1760_udc_write(ep->udc, DC_EPINDEX,
  69. DC_ENDPIDX(ep->addr & USB_ENDPOINT_NUMBER_MASK) |
  70. (dir == USB_DIR_IN ? DC_EPDIR : 0));
  71. }
  72. /**
  73. * isp1760_udc_select_ep - Select an endpoint for register access
  74. * @ep: The endpoint
  75. *
  76. * The ISP1761 endpoint registers are banked. This function selects the target
  77. * endpoint for banked register access. The selection remains valid until the
  78. * next call to this function, the next direct access to the EPINDEX register
  79. * or the next reset, whichever comes first.
  80. *
  81. * Called with the UDC spinlock held.
  82. */
  83. static void isp1760_udc_select_ep(struct isp1760_ep *ep)
  84. {
  85. __isp1760_udc_select_ep(ep, ep->addr & USB_ENDPOINT_DIR_MASK);
  86. }
  87. /* Called with the UDC spinlock held. */
  88. static void isp1760_udc_ctrl_send_status(struct isp1760_ep *ep, int dir)
  89. {
  90. struct isp1760_udc *udc = ep->udc;
  91. /*
  92. * Proceed to the status stage. The status stage data packet flows in
  93. * the direction opposite to the data stage data packets, we thus need
  94. * to select the OUT/IN endpoint for IN/OUT transfers.
  95. */
  96. isp1760_udc_write(udc, DC_EPINDEX, DC_ENDPIDX(0) |
  97. (dir == USB_DIR_IN ? 0 : DC_EPDIR));
  98. isp1760_udc_write(udc, DC_CTRLFUNC, DC_STATUS);
  99. /*
  100. * The hardware will terminate the request automatically and go back to
  101. * the setup stage without notifying us.
  102. */
  103. udc->ep0_state = ISP1760_CTRL_SETUP;
  104. }
  105. /* Called without the UDC spinlock held. */
  106. static void isp1760_udc_request_complete(struct isp1760_ep *ep,
  107. struct isp1760_request *req,
  108. int status)
  109. {
  110. struct isp1760_udc *udc = ep->udc;
  111. unsigned long flags;
  112. dev_dbg(ep->udc->isp->dev, "completing request %p with status %d\n",
  113. req, status);
  114. req->ep = NULL;
  115. req->req.status = status;
  116. req->req.complete(&ep->ep, &req->req);
  117. spin_lock_irqsave(&udc->lock, flags);
  118. /*
  119. * When completing control OUT requests, move to the status stage after
  120. * calling the request complete callback. This gives the gadget an
  121. * opportunity to stall the control transfer if needed.
  122. */
  123. if (status == 0 && ep->addr == 0 && udc->ep0_dir == USB_DIR_OUT)
  124. isp1760_udc_ctrl_send_status(ep, USB_DIR_OUT);
  125. spin_unlock_irqrestore(&udc->lock, flags);
  126. }
  127. static void isp1760_udc_ctrl_send_stall(struct isp1760_ep *ep)
  128. {
  129. struct isp1760_udc *udc = ep->udc;
  130. unsigned long flags;
  131. dev_dbg(ep->udc->isp->dev, "%s(ep%02x)\n", __func__, ep->addr);
  132. spin_lock_irqsave(&udc->lock, flags);
  133. /* Stall both the IN and OUT endpoints. */
  134. __isp1760_udc_select_ep(ep, USB_DIR_OUT);
  135. isp1760_udc_write(udc, DC_CTRLFUNC, DC_STALL);
  136. __isp1760_udc_select_ep(ep, USB_DIR_IN);
  137. isp1760_udc_write(udc, DC_CTRLFUNC, DC_STALL);
  138. /* A protocol stall completes the control transaction. */
  139. udc->ep0_state = ISP1760_CTRL_SETUP;
  140. spin_unlock_irqrestore(&udc->lock, flags);
  141. }
  142. /* -----------------------------------------------------------------------------
  143. * Data Endpoints
  144. */
  145. /* Called with the UDC spinlock held. */
  146. static bool isp1760_udc_receive(struct isp1760_ep *ep,
  147. struct isp1760_request *req)
  148. {
  149. struct isp1760_udc *udc = ep->udc;
  150. unsigned int len;
  151. u32 *buf;
  152. int i;
  153. isp1760_udc_select_ep(ep);
  154. len = isp1760_udc_read(udc, DC_BUFLEN) & DC_DATACOUNT_MASK;
  155. dev_dbg(udc->isp->dev, "%s: received %u bytes (%u/%u done)\n",
  156. __func__, len, req->req.actual, req->req.length);
  157. len = min(len, req->req.length - req->req.actual);
  158. if (!len) {
  159. /*
  160. * There's no data to be read from the FIFO, acknowledge the RX
  161. * interrupt by clearing the buffer.
  162. *
  163. * TODO: What if another packet arrives in the meantime ? The
  164. * datasheet doesn't clearly document how this should be
  165. * handled.
  166. */
  167. isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
  168. return false;
  169. }
  170. buf = req->req.buf + req->req.actual;
  171. /*
  172. * Make sure not to read more than one extra byte, otherwise data from
  173. * the next packet might be removed from the FIFO.
  174. */
  175. for (i = len; i > 2; i -= 4, ++buf)
  176. *buf = le32_to_cpu(isp1760_udc_read(udc, DC_DATAPORT));
  177. if (i > 0)
  178. *(u16 *)buf = le16_to_cpu(readw(udc->regs + DC_DATAPORT));
  179. req->req.actual += len;
  180. /*
  181. * TODO: The short_not_ok flag isn't supported yet, but isn't used by
  182. * any gadget driver either.
  183. */
  184. dev_dbg(udc->isp->dev,
  185. "%s: req %p actual/length %u/%u maxpacket %u packet size %u\n",
  186. __func__, req, req->req.actual, req->req.length, ep->maxpacket,
  187. len);
  188. ep->rx_pending = false;
  189. /*
  190. * Complete the request if all data has been received or if a short
  191. * packet has been received.
  192. */
  193. if (req->req.actual == req->req.length || len < ep->maxpacket) {
  194. list_del(&req->queue);
  195. return true;
  196. }
  197. return false;
  198. }
  199. static void isp1760_udc_transmit(struct isp1760_ep *ep,
  200. struct isp1760_request *req)
  201. {
  202. struct isp1760_udc *udc = ep->udc;
  203. u32 *buf = req->req.buf + req->req.actual;
  204. int i;
  205. req->packet_size = min(req->req.length - req->req.actual,
  206. ep->maxpacket);
  207. dev_dbg(udc->isp->dev, "%s: transferring %u bytes (%u/%u done)\n",
  208. __func__, req->packet_size, req->req.actual,
  209. req->req.length);
  210. __isp1760_udc_select_ep(ep, USB_DIR_IN);
  211. if (req->packet_size)
  212. isp1760_udc_write(udc, DC_BUFLEN, req->packet_size);
  213. /*
  214. * Make sure not to write more than one extra byte, otherwise extra data
  215. * will stay in the FIFO and will be transmitted during the next control
  216. * request. The endpoint control CLBUF bit is supposed to allow flushing
  217. * the FIFO for this kind of conditions, but doesn't seem to work.
  218. */
  219. for (i = req->packet_size; i > 2; i -= 4, ++buf)
  220. isp1760_udc_write(udc, DC_DATAPORT, cpu_to_le32(*buf));
  221. if (i > 0)
  222. writew(cpu_to_le16(*(u16 *)buf), udc->regs + DC_DATAPORT);
  223. if (ep->addr == 0)
  224. isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
  225. if (!req->packet_size)
  226. isp1760_udc_write(udc, DC_CTRLFUNC, DC_VENDP);
  227. }
  228. static void isp1760_ep_rx_ready(struct isp1760_ep *ep)
  229. {
  230. struct isp1760_udc *udc = ep->udc;
  231. struct isp1760_request *req;
  232. bool complete;
  233. spin_lock(&udc->lock);
  234. if (ep->addr == 0 && udc->ep0_state != ISP1760_CTRL_DATA_OUT) {
  235. spin_unlock(&udc->lock);
  236. dev_dbg(udc->isp->dev, "%s: invalid ep0 state %u\n", __func__,
  237. udc->ep0_state);
  238. return;
  239. }
  240. if (ep->addr != 0 && !ep->desc) {
  241. spin_unlock(&udc->lock);
  242. dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
  243. ep->addr);
  244. return;
  245. }
  246. if (list_empty(&ep->queue)) {
  247. ep->rx_pending = true;
  248. spin_unlock(&udc->lock);
  249. dev_dbg(udc->isp->dev, "%s: ep%02x (%p) has no request queued\n",
  250. __func__, ep->addr, ep);
  251. return;
  252. }
  253. req = list_first_entry(&ep->queue, struct isp1760_request,
  254. queue);
  255. complete = isp1760_udc_receive(ep, req);
  256. spin_unlock(&udc->lock);
  257. if (complete)
  258. isp1760_udc_request_complete(ep, req, 0);
  259. }
  260. static void isp1760_ep_tx_complete(struct isp1760_ep *ep)
  261. {
  262. struct isp1760_udc *udc = ep->udc;
  263. struct isp1760_request *complete = NULL;
  264. struct isp1760_request *req;
  265. bool need_zlp;
  266. spin_lock(&udc->lock);
  267. if (ep->addr == 0 && udc->ep0_state != ISP1760_CTRL_DATA_IN) {
  268. spin_unlock(&udc->lock);
  269. dev_dbg(udc->isp->dev, "TX IRQ: invalid endpoint state %u\n",
  270. udc->ep0_state);
  271. return;
  272. }
  273. if (list_empty(&ep->queue)) {
  274. /*
  275. * This can happen for the control endpoint when the reply to
  276. * the GET_STATUS IN control request is sent directly by the
  277. * setup IRQ handler. Just proceed to the status stage.
  278. */
  279. if (ep->addr == 0) {
  280. isp1760_udc_ctrl_send_status(ep, USB_DIR_IN);
  281. spin_unlock(&udc->lock);
  282. return;
  283. }
  284. spin_unlock(&udc->lock);
  285. dev_dbg(udc->isp->dev, "%s: ep%02x has no request queued\n",
  286. __func__, ep->addr);
  287. return;
  288. }
  289. req = list_first_entry(&ep->queue, struct isp1760_request,
  290. queue);
  291. req->req.actual += req->packet_size;
  292. need_zlp = req->req.actual == req->req.length &&
  293. !(req->req.length % ep->maxpacket) &&
  294. req->packet_size && req->req.zero;
  295. dev_dbg(udc->isp->dev,
  296. "TX IRQ: req %p actual/length %u/%u maxpacket %u packet size %u zero %u need zlp %u\n",
  297. req, req->req.actual, req->req.length, ep->maxpacket,
  298. req->packet_size, req->req.zero, need_zlp);
  299. /*
  300. * Complete the request if all data has been sent and we don't need to
  301. * transmit a zero length packet.
  302. */
  303. if (req->req.actual == req->req.length && !need_zlp) {
  304. complete = req;
  305. list_del(&req->queue);
  306. if (ep->addr == 0)
  307. isp1760_udc_ctrl_send_status(ep, USB_DIR_IN);
  308. if (!list_empty(&ep->queue))
  309. req = list_first_entry(&ep->queue,
  310. struct isp1760_request, queue);
  311. else
  312. req = NULL;
  313. }
  314. /*
  315. * Transmit the next packet or start the next request, if any.
  316. *
  317. * TODO: If the endpoint is stalled the next request shouldn't be
  318. * started, but what about the next packet ?
  319. */
  320. if (req)
  321. isp1760_udc_transmit(ep, req);
  322. spin_unlock(&udc->lock);
  323. if (complete)
  324. isp1760_udc_request_complete(ep, complete, 0);
  325. }
  326. static int __isp1760_udc_set_halt(struct isp1760_ep *ep, bool halt)
  327. {
  328. struct isp1760_udc *udc = ep->udc;
  329. dev_dbg(udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
  330. halt ? "set" : "clear", ep->addr);
  331. if (ep->desc && usb_endpoint_xfer_isoc(ep->desc)) {
  332. dev_dbg(udc->isp->dev, "%s: ep%02x is isochronous\n", __func__,
  333. ep->addr);
  334. return -EINVAL;
  335. }
  336. isp1760_udc_select_ep(ep);
  337. isp1760_udc_write(udc, DC_CTRLFUNC, halt ? DC_STALL : 0);
  338. if (ep->addr == 0) {
  339. /* When halting the control endpoint, stall both IN and OUT. */
  340. __isp1760_udc_select_ep(ep, USB_DIR_IN);
  341. isp1760_udc_write(udc, DC_CTRLFUNC, halt ? DC_STALL : 0);
  342. } else if (!halt) {
  343. /* Reset the data PID by cycling the endpoint enable bit. */
  344. u16 eptype = isp1760_udc_read(udc, DC_EPTYPE);
  345. isp1760_udc_write(udc, DC_EPTYPE, eptype & ~DC_EPENABLE);
  346. isp1760_udc_write(udc, DC_EPTYPE, eptype);
  347. /*
  348. * Disabling the endpoint emptied the transmit FIFO, fill it
  349. * again if a request is pending.
  350. *
  351. * TODO: Does the gadget framework require synchronizatino with
  352. * the TX IRQ handler ?
  353. */
  354. if ((ep->addr & USB_DIR_IN) && !list_empty(&ep->queue)) {
  355. struct isp1760_request *req;
  356. req = list_first_entry(&ep->queue,
  357. struct isp1760_request, queue);
  358. isp1760_udc_transmit(ep, req);
  359. }
  360. }
  361. ep->halted = halt;
  362. return 0;
  363. }
  364. /* -----------------------------------------------------------------------------
  365. * Control Endpoint
  366. */
  367. static int isp1760_udc_get_status(struct isp1760_udc *udc,
  368. const struct usb_ctrlrequest *req)
  369. {
  370. struct isp1760_ep *ep;
  371. u16 status;
  372. if (req->wLength != cpu_to_le16(2) || req->wValue != cpu_to_le16(0))
  373. return -EINVAL;
  374. switch (req->bRequestType) {
  375. case USB_DIR_IN | USB_RECIP_DEVICE:
  376. status = udc->devstatus;
  377. break;
  378. case USB_DIR_IN | USB_RECIP_INTERFACE:
  379. status = 0;
  380. break;
  381. case USB_DIR_IN | USB_RECIP_ENDPOINT:
  382. ep = isp1760_udc_find_ep(udc, le16_to_cpu(req->wIndex));
  383. if (!ep)
  384. return -EINVAL;
  385. status = 0;
  386. if (ep->halted)
  387. status |= 1 << USB_ENDPOINT_HALT;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. isp1760_udc_write(udc, DC_EPINDEX, DC_ENDPIDX(0) | DC_EPDIR);
  393. isp1760_udc_write(udc, DC_BUFLEN, 2);
  394. writew(cpu_to_le16(status), udc->regs + DC_DATAPORT);
  395. isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
  396. dev_dbg(udc->isp->dev, "%s: status 0x%04x\n", __func__, status);
  397. return 0;
  398. }
  399. static int isp1760_udc_set_address(struct isp1760_udc *udc, u16 addr)
  400. {
  401. if (addr > 127) {
  402. dev_dbg(udc->isp->dev, "invalid device address %u\n", addr);
  403. return -EINVAL;
  404. }
  405. if (udc->gadget.state != USB_STATE_DEFAULT &&
  406. udc->gadget.state != USB_STATE_ADDRESS) {
  407. dev_dbg(udc->isp->dev, "can't set address in state %u\n",
  408. udc->gadget.state);
  409. return -EINVAL;
  410. }
  411. usb_gadget_set_state(&udc->gadget, addr ? USB_STATE_ADDRESS :
  412. USB_STATE_DEFAULT);
  413. isp1760_udc_write(udc, DC_ADDRESS, DC_DEVEN | addr);
  414. spin_lock(&udc->lock);
  415. isp1760_udc_ctrl_send_status(&udc->ep[0], USB_DIR_OUT);
  416. spin_unlock(&udc->lock);
  417. return 0;
  418. }
  419. static bool isp1760_ep0_setup_standard(struct isp1760_udc *udc,
  420. struct usb_ctrlrequest *req)
  421. {
  422. bool stall;
  423. switch (req->bRequest) {
  424. case USB_REQ_GET_STATUS:
  425. return isp1760_udc_get_status(udc, req);
  426. case USB_REQ_CLEAR_FEATURE:
  427. switch (req->bRequestType) {
  428. case USB_DIR_OUT | USB_RECIP_DEVICE: {
  429. /* TODO: Handle remote wakeup feature. */
  430. return true;
  431. }
  432. case USB_DIR_OUT | USB_RECIP_ENDPOINT: {
  433. u16 index = le16_to_cpu(req->wIndex);
  434. struct isp1760_ep *ep;
  435. if (req->wLength != cpu_to_le16(0) ||
  436. req->wValue != cpu_to_le16(USB_ENDPOINT_HALT))
  437. return true;
  438. ep = isp1760_udc_find_ep(udc, index);
  439. if (!ep)
  440. return true;
  441. spin_lock(&udc->lock);
  442. /*
  443. * If the endpoint is wedged only the gadget can clear
  444. * the halt feature. Pretend success in that case, but
  445. * keep the endpoint halted.
  446. */
  447. if (!ep->wedged)
  448. stall = __isp1760_udc_set_halt(ep, false);
  449. else
  450. stall = false;
  451. if (!stall)
  452. isp1760_udc_ctrl_send_status(&udc->ep[0],
  453. USB_DIR_OUT);
  454. spin_unlock(&udc->lock);
  455. return stall;
  456. }
  457. default:
  458. return true;
  459. }
  460. break;
  461. case USB_REQ_SET_FEATURE:
  462. switch (req->bRequestType) {
  463. case USB_DIR_OUT | USB_RECIP_DEVICE: {
  464. /* TODO: Handle remote wakeup and test mode features */
  465. return true;
  466. }
  467. case USB_DIR_OUT | USB_RECIP_ENDPOINT: {
  468. u16 index = le16_to_cpu(req->wIndex);
  469. struct isp1760_ep *ep;
  470. if (req->wLength != cpu_to_le16(0) ||
  471. req->wValue != cpu_to_le16(USB_ENDPOINT_HALT))
  472. return true;
  473. ep = isp1760_udc_find_ep(udc, index);
  474. if (!ep)
  475. return true;
  476. spin_lock(&udc->lock);
  477. stall = __isp1760_udc_set_halt(ep, true);
  478. if (!stall)
  479. isp1760_udc_ctrl_send_status(&udc->ep[0],
  480. USB_DIR_OUT);
  481. spin_unlock(&udc->lock);
  482. return stall;
  483. }
  484. default:
  485. return true;
  486. }
  487. break;
  488. case USB_REQ_SET_ADDRESS:
  489. if (req->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
  490. return true;
  491. return isp1760_udc_set_address(udc, le16_to_cpu(req->wValue));
  492. case USB_REQ_SET_CONFIGURATION:
  493. if (req->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
  494. return true;
  495. if (udc->gadget.state != USB_STATE_ADDRESS &&
  496. udc->gadget.state != USB_STATE_CONFIGURED)
  497. return true;
  498. stall = udc->driver->setup(&udc->gadget, req) < 0;
  499. if (stall)
  500. return true;
  501. usb_gadget_set_state(&udc->gadget, req->wValue ?
  502. USB_STATE_CONFIGURED : USB_STATE_ADDRESS);
  503. /*
  504. * SET_CONFIGURATION (and SET_INTERFACE) must reset the halt
  505. * feature on all endpoints. There is however no need to do so
  506. * explicitly here as the gadget driver will disable and
  507. * reenable endpoints, clearing the halt feature.
  508. */
  509. return false;
  510. default:
  511. return udc->driver->setup(&udc->gadget, req) < 0;
  512. }
  513. }
  514. static void isp1760_ep0_setup(struct isp1760_udc *udc)
  515. {
  516. union {
  517. struct usb_ctrlrequest r;
  518. u32 data[2];
  519. } req;
  520. unsigned int count;
  521. bool stall = false;
  522. spin_lock(&udc->lock);
  523. isp1760_udc_write(udc, DC_EPINDEX, DC_EP0SETUP);
  524. count = isp1760_udc_read(udc, DC_BUFLEN) & DC_DATACOUNT_MASK;
  525. if (count != sizeof(req)) {
  526. spin_unlock(&udc->lock);
  527. dev_err(udc->isp->dev, "invalid length %u for setup packet\n",
  528. count);
  529. isp1760_udc_ctrl_send_stall(&udc->ep[0]);
  530. return;
  531. }
  532. req.data[0] = isp1760_udc_read(udc, DC_DATAPORT);
  533. req.data[1] = isp1760_udc_read(udc, DC_DATAPORT);
  534. if (udc->ep0_state != ISP1760_CTRL_SETUP) {
  535. spin_unlock(&udc->lock);
  536. dev_dbg(udc->isp->dev, "unexpected SETUP packet\n");
  537. return;
  538. }
  539. /* Move to the data stage. */
  540. if (!req.r.wLength)
  541. udc->ep0_state = ISP1760_CTRL_STATUS;
  542. else if (req.r.bRequestType & USB_DIR_IN)
  543. udc->ep0_state = ISP1760_CTRL_DATA_IN;
  544. else
  545. udc->ep0_state = ISP1760_CTRL_DATA_OUT;
  546. udc->ep0_dir = req.r.bRequestType & USB_DIR_IN;
  547. udc->ep0_length = le16_to_cpu(req.r.wLength);
  548. spin_unlock(&udc->lock);
  549. dev_dbg(udc->isp->dev,
  550. "%s: bRequestType 0x%02x bRequest 0x%02x wValue 0x%04x wIndex 0x%04x wLength 0x%04x\n",
  551. __func__, req.r.bRequestType, req.r.bRequest,
  552. le16_to_cpu(req.r.wValue), le16_to_cpu(req.r.wIndex),
  553. le16_to_cpu(req.r.wLength));
  554. if ((req.r.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  555. stall = isp1760_ep0_setup_standard(udc, &req.r);
  556. else
  557. stall = udc->driver->setup(&udc->gadget, &req.r) < 0;
  558. if (stall)
  559. isp1760_udc_ctrl_send_stall(&udc->ep[0]);
  560. }
  561. /* -----------------------------------------------------------------------------
  562. * Gadget Endpoint Operations
  563. */
  564. static int isp1760_ep_enable(struct usb_ep *ep,
  565. const struct usb_endpoint_descriptor *desc)
  566. {
  567. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  568. struct isp1760_udc *udc = uep->udc;
  569. unsigned long flags;
  570. unsigned int type;
  571. dev_dbg(uep->udc->isp->dev, "%s\n", __func__);
  572. /*
  573. * Validate the descriptor. The control endpoint can't be enabled
  574. * manually.
  575. */
  576. if (desc->bDescriptorType != USB_DT_ENDPOINT ||
  577. desc->bEndpointAddress == 0 ||
  578. desc->bEndpointAddress != uep->addr ||
  579. le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket) {
  580. dev_dbg(udc->isp->dev,
  581. "%s: invalid descriptor type %u addr %02x ep addr %02x max packet size %u/%u\n",
  582. __func__, desc->bDescriptorType,
  583. desc->bEndpointAddress, uep->addr,
  584. le16_to_cpu(desc->wMaxPacketSize), ep->maxpacket);
  585. return -EINVAL;
  586. }
  587. switch (usb_endpoint_type(desc)) {
  588. case USB_ENDPOINT_XFER_ISOC:
  589. type = DC_ENDPTYP_ISOC;
  590. break;
  591. case USB_ENDPOINT_XFER_BULK:
  592. type = DC_ENDPTYP_BULK;
  593. break;
  594. case USB_ENDPOINT_XFER_INT:
  595. type = DC_ENDPTYP_INTERRUPT;
  596. break;
  597. case USB_ENDPOINT_XFER_CONTROL:
  598. default:
  599. dev_dbg(udc->isp->dev, "%s: control endpoints unsupported\n",
  600. __func__);
  601. return -EINVAL;
  602. }
  603. spin_lock_irqsave(&udc->lock, flags);
  604. uep->desc = desc;
  605. uep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  606. uep->rx_pending = false;
  607. uep->halted = false;
  608. uep->wedged = false;
  609. isp1760_udc_select_ep(uep);
  610. isp1760_udc_write(udc, DC_EPMAXPKTSZ, uep->maxpacket);
  611. isp1760_udc_write(udc, DC_BUFLEN, uep->maxpacket);
  612. isp1760_udc_write(udc, DC_EPTYPE, DC_EPENABLE | type);
  613. spin_unlock_irqrestore(&udc->lock, flags);
  614. return 0;
  615. }
  616. static int isp1760_ep_disable(struct usb_ep *ep)
  617. {
  618. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  619. struct isp1760_udc *udc = uep->udc;
  620. struct isp1760_request *req, *nreq;
  621. LIST_HEAD(req_list);
  622. unsigned long flags;
  623. dev_dbg(udc->isp->dev, "%s\n", __func__);
  624. spin_lock_irqsave(&udc->lock, flags);
  625. if (!uep->desc) {
  626. dev_dbg(udc->isp->dev, "%s: endpoint not enabled\n", __func__);
  627. spin_unlock_irqrestore(&udc->lock, flags);
  628. return -EINVAL;
  629. }
  630. uep->desc = NULL;
  631. uep->maxpacket = 0;
  632. isp1760_udc_select_ep(uep);
  633. isp1760_udc_write(udc, DC_EPTYPE, 0);
  634. /* TODO Synchronize with the IRQ handler */
  635. list_splice_init(&uep->queue, &req_list);
  636. spin_unlock_irqrestore(&udc->lock, flags);
  637. list_for_each_entry_safe(req, nreq, &req_list, queue) {
  638. list_del(&req->queue);
  639. isp1760_udc_request_complete(uep, req, -ESHUTDOWN);
  640. }
  641. return 0;
  642. }
  643. static struct usb_request *isp1760_ep_alloc_request(struct usb_ep *ep,
  644. gfp_t gfp_flags)
  645. {
  646. struct isp1760_request *req;
  647. req = kzalloc(sizeof(*req), gfp_flags);
  648. if (!req)
  649. return NULL;
  650. return &req->req;
  651. }
  652. static void isp1760_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
  653. {
  654. struct isp1760_request *req = req_to_udc_req(_req);
  655. kfree(req);
  656. }
  657. static int isp1760_ep_queue(struct usb_ep *ep, struct usb_request *_req,
  658. gfp_t gfp_flags)
  659. {
  660. struct isp1760_request *req = req_to_udc_req(_req);
  661. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  662. struct isp1760_udc *udc = uep->udc;
  663. bool complete = false;
  664. unsigned long flags;
  665. int ret = 0;
  666. _req->status = -EINPROGRESS;
  667. _req->actual = 0;
  668. spin_lock_irqsave(&udc->lock, flags);
  669. dev_dbg(udc->isp->dev,
  670. "%s: req %p (%u bytes%s) ep %p(0x%02x)\n", __func__, _req,
  671. _req->length, _req->zero ? " (zlp)" : "", uep, uep->addr);
  672. req->ep = uep;
  673. if (uep->addr == 0) {
  674. if (_req->length != udc->ep0_length &&
  675. udc->ep0_state != ISP1760_CTRL_DATA_IN) {
  676. dev_dbg(udc->isp->dev,
  677. "%s: invalid length %u for req %p\n",
  678. __func__, _req->length, req);
  679. ret = -EINVAL;
  680. goto done;
  681. }
  682. switch (udc->ep0_state) {
  683. case ISP1760_CTRL_DATA_IN:
  684. dev_dbg(udc->isp->dev, "%s: transmitting req %p\n",
  685. __func__, req);
  686. list_add_tail(&req->queue, &uep->queue);
  687. isp1760_udc_transmit(uep, req);
  688. break;
  689. case ISP1760_CTRL_DATA_OUT:
  690. list_add_tail(&req->queue, &uep->queue);
  691. __isp1760_udc_select_ep(uep, USB_DIR_OUT);
  692. isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
  693. break;
  694. case ISP1760_CTRL_STATUS:
  695. complete = true;
  696. break;
  697. default:
  698. dev_dbg(udc->isp->dev, "%s: invalid ep0 state\n",
  699. __func__);
  700. ret = -EINVAL;
  701. break;
  702. }
  703. } else if (uep->desc) {
  704. bool empty = list_empty(&uep->queue);
  705. list_add_tail(&req->queue, &uep->queue);
  706. if ((uep->addr & USB_DIR_IN) && !uep->halted && empty)
  707. isp1760_udc_transmit(uep, req);
  708. else if (!(uep->addr & USB_DIR_IN) && uep->rx_pending)
  709. complete = isp1760_udc_receive(uep, req);
  710. } else {
  711. dev_dbg(udc->isp->dev,
  712. "%s: can't queue request to disabled ep%02x\n",
  713. __func__, uep->addr);
  714. ret = -ESHUTDOWN;
  715. }
  716. done:
  717. if (ret < 0)
  718. req->ep = NULL;
  719. spin_unlock_irqrestore(&udc->lock, flags);
  720. if (complete)
  721. isp1760_udc_request_complete(uep, req, 0);
  722. return ret;
  723. }
  724. static int isp1760_ep_dequeue(struct usb_ep *ep, struct usb_request *_req)
  725. {
  726. struct isp1760_request *req = req_to_udc_req(_req);
  727. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  728. struct isp1760_udc *udc = uep->udc;
  729. unsigned long flags;
  730. dev_dbg(uep->udc->isp->dev, "%s(ep%02x)\n", __func__, uep->addr);
  731. spin_lock_irqsave(&udc->lock, flags);
  732. if (req->ep != uep)
  733. req = NULL;
  734. else
  735. list_del(&req->queue);
  736. spin_unlock_irqrestore(&udc->lock, flags);
  737. if (!req)
  738. return -EINVAL;
  739. isp1760_udc_request_complete(uep, req, -ECONNRESET);
  740. return 0;
  741. }
  742. static int __isp1760_ep_set_halt(struct isp1760_ep *uep, bool stall, bool wedge)
  743. {
  744. struct isp1760_udc *udc = uep->udc;
  745. int ret;
  746. if (!uep->addr) {
  747. /*
  748. * Halting the control endpoint is only valid as a delayed error
  749. * response to a SETUP packet. Make sure EP0 is in the right
  750. * stage and that the gadget isn't trying to clear the halt
  751. * condition.
  752. */
  753. if (WARN_ON(udc->ep0_state == ISP1760_CTRL_SETUP || !stall ||
  754. wedge)) {
  755. return -EINVAL;
  756. }
  757. }
  758. if (uep->addr && !uep->desc) {
  759. dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
  760. uep->addr);
  761. return -EINVAL;
  762. }
  763. if (uep->addr & USB_DIR_IN) {
  764. /* Refuse to halt IN endpoints with active transfers. */
  765. if (!list_empty(&uep->queue)) {
  766. dev_dbg(udc->isp->dev,
  767. "%s: ep%02x has request pending\n", __func__,
  768. uep->addr);
  769. return -EAGAIN;
  770. }
  771. }
  772. ret = __isp1760_udc_set_halt(uep, stall);
  773. if (ret < 0)
  774. return ret;
  775. if (!uep->addr) {
  776. /*
  777. * Stalling EP0 completes the control transaction, move back to
  778. * the SETUP state.
  779. */
  780. udc->ep0_state = ISP1760_CTRL_SETUP;
  781. return 0;
  782. }
  783. if (wedge)
  784. uep->wedged = true;
  785. else if (!stall)
  786. uep->wedged = false;
  787. return 0;
  788. }
  789. static int isp1760_ep_set_halt(struct usb_ep *ep, int value)
  790. {
  791. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  792. unsigned long flags;
  793. int ret;
  794. dev_dbg(uep->udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
  795. value ? "set" : "clear", uep->addr);
  796. spin_lock_irqsave(&uep->udc->lock, flags);
  797. ret = __isp1760_ep_set_halt(uep, value, false);
  798. spin_unlock_irqrestore(&uep->udc->lock, flags);
  799. return ret;
  800. }
  801. static int isp1760_ep_set_wedge(struct usb_ep *ep)
  802. {
  803. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  804. unsigned long flags;
  805. int ret;
  806. dev_dbg(uep->udc->isp->dev, "%s: set wedge on ep%02x)\n", __func__,
  807. uep->addr);
  808. spin_lock_irqsave(&uep->udc->lock, flags);
  809. ret = __isp1760_ep_set_halt(uep, true, true);
  810. spin_unlock_irqrestore(&uep->udc->lock, flags);
  811. return ret;
  812. }
  813. static void isp1760_ep_fifo_flush(struct usb_ep *ep)
  814. {
  815. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  816. struct isp1760_udc *udc = uep->udc;
  817. unsigned long flags;
  818. spin_lock_irqsave(&udc->lock, flags);
  819. isp1760_udc_select_ep(uep);
  820. /*
  821. * Set the CLBUF bit twice to flush both buffers in case double
  822. * buffering is enabled.
  823. */
  824. isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
  825. isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
  826. spin_unlock_irqrestore(&udc->lock, flags);
  827. }
  828. static const struct usb_ep_ops isp1760_ep_ops = {
  829. .enable = isp1760_ep_enable,
  830. .disable = isp1760_ep_disable,
  831. .alloc_request = isp1760_ep_alloc_request,
  832. .free_request = isp1760_ep_free_request,
  833. .queue = isp1760_ep_queue,
  834. .dequeue = isp1760_ep_dequeue,
  835. .set_halt = isp1760_ep_set_halt,
  836. .set_wedge = isp1760_ep_set_wedge,
  837. .fifo_flush = isp1760_ep_fifo_flush,
  838. };
  839. /* -----------------------------------------------------------------------------
  840. * Device States
  841. */
  842. /* Called with the UDC spinlock held. */
  843. static void isp1760_udc_connect(struct isp1760_udc *udc)
  844. {
  845. usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
  846. mod_timer(&udc->vbus_timer, jiffies + ISP1760_VBUS_POLL_INTERVAL);
  847. }
  848. /* Called with the UDC spinlock held. */
  849. static void isp1760_udc_disconnect(struct isp1760_udc *udc)
  850. {
  851. if (udc->gadget.state < USB_STATE_POWERED)
  852. return;
  853. dev_dbg(udc->isp->dev, "Device disconnected in state %u\n",
  854. udc->gadget.state);
  855. udc->gadget.speed = USB_SPEED_UNKNOWN;
  856. usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
  857. if (udc->driver->disconnect)
  858. udc->driver->disconnect(&udc->gadget);
  859. del_timer(&udc->vbus_timer);
  860. /* TODO Reset all endpoints ? */
  861. }
  862. static void isp1760_udc_init_hw(struct isp1760_udc *udc)
  863. {
  864. /*
  865. * The device controller currently shares its interrupt with the host
  866. * controller, the DC_IRQ polarity and signaling mode are ignored. Set
  867. * the to active-low level-triggered.
  868. *
  869. * Configure the control, in and out pipes to generate interrupts on
  870. * ACK tokens only (and NYET for the out pipe). The default
  871. * configuration also generates an interrupt on the first NACK token.
  872. */
  873. isp1760_udc_write(udc, DC_INTCONF, DC_CDBGMOD_ACK | DC_DDBGMODIN_ACK |
  874. DC_DDBGMODOUT_ACK_NYET);
  875. isp1760_udc_write(udc, DC_INTENABLE, DC_IEPRXTX(7) | DC_IEPRXTX(6) |
  876. DC_IEPRXTX(5) | DC_IEPRXTX(4) | DC_IEPRXTX(3) |
  877. DC_IEPRXTX(2) | DC_IEPRXTX(1) | DC_IEPRXTX(0) |
  878. DC_IEP0SETUP | DC_IEVBUS | DC_IERESM | DC_IESUSP |
  879. DC_IEHS_STA | DC_IEBRST);
  880. if (udc->connected)
  881. isp1760_set_pullup(udc->isp, true);
  882. isp1760_udc_write(udc, DC_ADDRESS, DC_DEVEN);
  883. }
  884. static void isp1760_udc_reset(struct isp1760_udc *udc)
  885. {
  886. unsigned long flags;
  887. spin_lock_irqsave(&udc->lock, flags);
  888. /*
  889. * The bus reset has reset most registers to their default value,
  890. * reinitialize the UDC hardware.
  891. */
  892. isp1760_udc_init_hw(udc);
  893. udc->ep0_state = ISP1760_CTRL_SETUP;
  894. udc->gadget.speed = USB_SPEED_FULL;
  895. usb_gadget_udc_reset(&udc->gadget, udc->driver);
  896. spin_unlock_irqrestore(&udc->lock, flags);
  897. }
  898. static void isp1760_udc_suspend(struct isp1760_udc *udc)
  899. {
  900. if (udc->gadget.state < USB_STATE_DEFAULT)
  901. return;
  902. if (udc->driver->suspend)
  903. udc->driver->suspend(&udc->gadget);
  904. }
  905. static void isp1760_udc_resume(struct isp1760_udc *udc)
  906. {
  907. if (udc->gadget.state < USB_STATE_DEFAULT)
  908. return;
  909. if (udc->driver->resume)
  910. udc->driver->resume(&udc->gadget);
  911. }
  912. /* -----------------------------------------------------------------------------
  913. * Gadget Operations
  914. */
  915. static int isp1760_udc_get_frame(struct usb_gadget *gadget)
  916. {
  917. struct isp1760_udc *udc = gadget_to_udc(gadget);
  918. return isp1760_udc_read(udc, DC_FRAMENUM) & ((1 << 11) - 1);
  919. }
  920. static int isp1760_udc_wakeup(struct usb_gadget *gadget)
  921. {
  922. struct isp1760_udc *udc = gadget_to_udc(gadget);
  923. dev_dbg(udc->isp->dev, "%s\n", __func__);
  924. return -ENOTSUPP;
  925. }
  926. static int isp1760_udc_set_selfpowered(struct usb_gadget *gadget,
  927. int is_selfpowered)
  928. {
  929. struct isp1760_udc *udc = gadget_to_udc(gadget);
  930. if (is_selfpowered)
  931. udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
  932. else
  933. udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  934. return 0;
  935. }
  936. static int isp1760_udc_pullup(struct usb_gadget *gadget, int is_on)
  937. {
  938. struct isp1760_udc *udc = gadget_to_udc(gadget);
  939. isp1760_set_pullup(udc->isp, is_on);
  940. udc->connected = is_on;
  941. return 0;
  942. }
  943. static int isp1760_udc_start(struct usb_gadget *gadget,
  944. struct usb_gadget_driver *driver)
  945. {
  946. struct isp1760_udc *udc = gadget_to_udc(gadget);
  947. unsigned long flags;
  948. /* The hardware doesn't support low speed. */
  949. if (driver->max_speed < USB_SPEED_FULL) {
  950. dev_err(udc->isp->dev, "Invalid gadget driver\n");
  951. return -EINVAL;
  952. }
  953. spin_lock_irqsave(&udc->lock, flags);
  954. if (udc->driver) {
  955. dev_err(udc->isp->dev, "UDC already has a gadget driver\n");
  956. spin_unlock_irqrestore(&udc->lock, flags);
  957. return -EBUSY;
  958. }
  959. udc->driver = driver;
  960. spin_unlock_irqrestore(&udc->lock, flags);
  961. dev_dbg(udc->isp->dev, "starting UDC with driver %s\n",
  962. driver->function);
  963. udc->devstatus = 0;
  964. udc->connected = true;
  965. usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
  966. /* DMA isn't supported yet, don't enable the DMA clock. */
  967. isp1760_udc_write(udc, DC_MODE, DC_GLINTENA);
  968. isp1760_udc_init_hw(udc);
  969. dev_dbg(udc->isp->dev, "UDC started with driver %s\n",
  970. driver->function);
  971. return 0;
  972. }
  973. static int isp1760_udc_stop(struct usb_gadget *gadget)
  974. {
  975. struct isp1760_udc *udc = gadget_to_udc(gadget);
  976. unsigned long flags;
  977. dev_dbg(udc->isp->dev, "%s\n", __func__);
  978. del_timer_sync(&udc->vbus_timer);
  979. isp1760_udc_write(udc, DC_MODE, 0);
  980. spin_lock_irqsave(&udc->lock, flags);
  981. udc->driver = NULL;
  982. spin_unlock_irqrestore(&udc->lock, flags);
  983. return 0;
  984. }
  985. static struct usb_gadget_ops isp1760_udc_ops = {
  986. .get_frame = isp1760_udc_get_frame,
  987. .wakeup = isp1760_udc_wakeup,
  988. .set_selfpowered = isp1760_udc_set_selfpowered,
  989. .pullup = isp1760_udc_pullup,
  990. .udc_start = isp1760_udc_start,
  991. .udc_stop = isp1760_udc_stop,
  992. };
  993. /* -----------------------------------------------------------------------------
  994. * Interrupt Handling
  995. */
  996. static irqreturn_t isp1760_udc_irq(int irq, void *dev)
  997. {
  998. struct isp1760_udc *udc = dev;
  999. unsigned int i;
  1000. u32 status;
  1001. status = isp1760_udc_read(udc, DC_INTERRUPT)
  1002. & isp1760_udc_read(udc, DC_INTENABLE);
  1003. isp1760_udc_write(udc, DC_INTERRUPT, status);
  1004. if (status & DC_IEVBUS) {
  1005. dev_dbg(udc->isp->dev, "%s(VBUS)\n", __func__);
  1006. /* The VBUS interrupt is only triggered when VBUS appears. */
  1007. spin_lock(&udc->lock);
  1008. isp1760_udc_connect(udc);
  1009. spin_unlock(&udc->lock);
  1010. }
  1011. if (status & DC_IEBRST) {
  1012. dev_dbg(udc->isp->dev, "%s(BRST)\n", __func__);
  1013. isp1760_udc_reset(udc);
  1014. }
  1015. for (i = 0; i <= 7; ++i) {
  1016. struct isp1760_ep *ep = &udc->ep[i*2];
  1017. if (status & DC_IEPTX(i)) {
  1018. dev_dbg(udc->isp->dev, "%s(EPTX%u)\n", __func__, i);
  1019. isp1760_ep_tx_complete(ep);
  1020. }
  1021. if (status & DC_IEPRX(i)) {
  1022. dev_dbg(udc->isp->dev, "%s(EPRX%u)\n", __func__, i);
  1023. isp1760_ep_rx_ready(i ? ep - 1 : ep);
  1024. }
  1025. }
  1026. if (status & DC_IEP0SETUP) {
  1027. dev_dbg(udc->isp->dev, "%s(EP0SETUP)\n", __func__);
  1028. isp1760_ep0_setup(udc);
  1029. }
  1030. if (status & DC_IERESM) {
  1031. dev_dbg(udc->isp->dev, "%s(RESM)\n", __func__);
  1032. isp1760_udc_resume(udc);
  1033. }
  1034. if (status & DC_IESUSP) {
  1035. dev_dbg(udc->isp->dev, "%s(SUSP)\n", __func__);
  1036. spin_lock(&udc->lock);
  1037. if (!(isp1760_udc_read(udc, DC_MODE) & DC_VBUSSTAT))
  1038. isp1760_udc_disconnect(udc);
  1039. else
  1040. isp1760_udc_suspend(udc);
  1041. spin_unlock(&udc->lock);
  1042. }
  1043. if (status & DC_IEHS_STA) {
  1044. dev_dbg(udc->isp->dev, "%s(HS_STA)\n", __func__);
  1045. udc->gadget.speed = USB_SPEED_HIGH;
  1046. }
  1047. return status ? IRQ_HANDLED : IRQ_NONE;
  1048. }
  1049. static void isp1760_udc_vbus_poll(unsigned long data)
  1050. {
  1051. struct isp1760_udc *udc = (struct isp1760_udc *)data;
  1052. unsigned long flags;
  1053. spin_lock_irqsave(&udc->lock, flags);
  1054. if (!(isp1760_udc_read(udc, DC_MODE) & DC_VBUSSTAT))
  1055. isp1760_udc_disconnect(udc);
  1056. else if (udc->gadget.state >= USB_STATE_POWERED)
  1057. mod_timer(&udc->vbus_timer,
  1058. jiffies + ISP1760_VBUS_POLL_INTERVAL);
  1059. spin_unlock_irqrestore(&udc->lock, flags);
  1060. }
  1061. /* -----------------------------------------------------------------------------
  1062. * Registration
  1063. */
  1064. static void isp1760_udc_init_eps(struct isp1760_udc *udc)
  1065. {
  1066. unsigned int i;
  1067. INIT_LIST_HEAD(&udc->gadget.ep_list);
  1068. for (i = 0; i < ARRAY_SIZE(udc->ep); ++i) {
  1069. struct isp1760_ep *ep = &udc->ep[i];
  1070. unsigned int ep_num = (i + 1) / 2;
  1071. bool is_in = !(i & 1);
  1072. ep->udc = udc;
  1073. INIT_LIST_HEAD(&ep->queue);
  1074. ep->addr = (ep_num && is_in ? USB_DIR_IN : USB_DIR_OUT)
  1075. | ep_num;
  1076. ep->desc = NULL;
  1077. sprintf(ep->name, "ep%u%s", ep_num,
  1078. ep_num ? (is_in ? "in" : "out") : "");
  1079. ep->ep.ops = &isp1760_ep_ops;
  1080. ep->ep.name = ep->name;
  1081. /*
  1082. * Hardcode the maximum packet sizes for now, to 64 bytes for
  1083. * the control endpoint and 512 bytes for all other endpoints.
  1084. * This fits in the 8kB FIFO without double-buffering.
  1085. */
  1086. if (ep_num == 0) {
  1087. ep->ep.maxpacket = 64;
  1088. ep->maxpacket = 64;
  1089. udc->gadget.ep0 = &ep->ep;
  1090. } else {
  1091. ep->ep.maxpacket = 512;
  1092. ep->maxpacket = 0;
  1093. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  1094. }
  1095. }
  1096. }
  1097. static int isp1760_udc_init(struct isp1760_udc *udc)
  1098. {
  1099. u16 scratch;
  1100. u32 chipid;
  1101. /*
  1102. * Check that the controller is present by writing to the scratch
  1103. * register, modifying the bus pattern by reading from the chip ID
  1104. * register, and reading the scratch register value back. The chip ID
  1105. * and scratch register contents must match the expected values.
  1106. */
  1107. isp1760_udc_write(udc, DC_SCRATCH, 0xbabe);
  1108. chipid = isp1760_udc_read(udc, DC_CHIPID);
  1109. scratch = isp1760_udc_read(udc, DC_SCRATCH);
  1110. if (scratch != 0xbabe) {
  1111. dev_err(udc->isp->dev,
  1112. "udc: scratch test failed (0x%04x/0x%08x)\n",
  1113. scratch, chipid);
  1114. return -ENODEV;
  1115. }
  1116. if (chipid != 0x00011582 && chipid != 0x00158210) {
  1117. dev_err(udc->isp->dev, "udc: invalid chip ID 0x%08x\n", chipid);
  1118. return -ENODEV;
  1119. }
  1120. /* Reset the device controller. */
  1121. isp1760_udc_write(udc, DC_MODE, DC_SFRESET);
  1122. usleep_range(10000, 11000);
  1123. isp1760_udc_write(udc, DC_MODE, 0);
  1124. usleep_range(10000, 11000);
  1125. return 0;
  1126. }
  1127. int isp1760_udc_register(struct isp1760_device *isp, int irq,
  1128. unsigned long irqflags)
  1129. {
  1130. struct isp1760_udc *udc = &isp->udc;
  1131. const char *devname;
  1132. int ret;
  1133. udc->irq = -1;
  1134. udc->isp = isp;
  1135. udc->regs = isp->regs;
  1136. spin_lock_init(&udc->lock);
  1137. setup_timer(&udc->vbus_timer, isp1760_udc_vbus_poll,
  1138. (unsigned long)udc);
  1139. ret = isp1760_udc_init(udc);
  1140. if (ret < 0)
  1141. return ret;
  1142. devname = dev_name(isp->dev);
  1143. udc->irqname = kmalloc(strlen(devname) + 7, GFP_KERNEL);
  1144. if (!udc->irqname)
  1145. return -ENOMEM;
  1146. sprintf(udc->irqname, "%s (udc)", devname);
  1147. ret = request_irq(irq, isp1760_udc_irq, IRQF_SHARED | irqflags,
  1148. udc->irqname, udc);
  1149. if (ret < 0)
  1150. goto error;
  1151. udc->irq = irq;
  1152. /*
  1153. * Initialize the gadget static fields and register its device. Gadget
  1154. * fields that vary during the life time of the gadget are initialized
  1155. * by the UDC core.
  1156. */
  1157. udc->gadget.ops = &isp1760_udc_ops;
  1158. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1159. udc->gadget.max_speed = USB_SPEED_HIGH;
  1160. udc->gadget.name = "isp1761_udc";
  1161. isp1760_udc_init_eps(udc);
  1162. ret = usb_add_gadget_udc(isp->dev, &udc->gadget);
  1163. if (ret < 0)
  1164. goto error;
  1165. return 0;
  1166. error:
  1167. if (udc->irq >= 0)
  1168. free_irq(udc->irq, udc);
  1169. kfree(udc->irqname);
  1170. return ret;
  1171. }
  1172. void isp1760_udc_unregister(struct isp1760_device *isp)
  1173. {
  1174. struct isp1760_udc *udc = &isp->udc;
  1175. if (!udc->isp)
  1176. return;
  1177. usb_del_gadget_udc(&udc->gadget);
  1178. free_irq(udc->irq, udc);
  1179. kfree(udc->irqname);
  1180. }