isp1760-udc.c 36 KB

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