isp1760-udc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. return &req->req;
  649. }
  650. static void isp1760_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
  651. {
  652. struct isp1760_request *req = req_to_udc_req(_req);
  653. kfree(req);
  654. }
  655. static int isp1760_ep_queue(struct usb_ep *ep, struct usb_request *_req,
  656. gfp_t gfp_flags)
  657. {
  658. struct isp1760_request *req = req_to_udc_req(_req);
  659. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  660. struct isp1760_udc *udc = uep->udc;
  661. bool complete = false;
  662. unsigned long flags;
  663. int ret = 0;
  664. _req->status = -EINPROGRESS;
  665. _req->actual = 0;
  666. spin_lock_irqsave(&udc->lock, flags);
  667. dev_dbg(udc->isp->dev,
  668. "%s: req %p (%u bytes%s) ep %p(0x%02x)\n", __func__, _req,
  669. _req->length, _req->zero ? " (zlp)" : "", uep, uep->addr);
  670. req->ep = uep;
  671. if (uep->addr == 0) {
  672. if (_req->length != udc->ep0_length &&
  673. udc->ep0_state != ISP1760_CTRL_DATA_IN) {
  674. dev_dbg(udc->isp->dev,
  675. "%s: invalid length %u for req %p\n",
  676. __func__, _req->length, req);
  677. ret = -EINVAL;
  678. goto done;
  679. }
  680. switch (udc->ep0_state) {
  681. case ISP1760_CTRL_DATA_IN:
  682. dev_dbg(udc->isp->dev, "%s: transmitting req %p\n",
  683. __func__, req);
  684. list_add_tail(&req->queue, &uep->queue);
  685. isp1760_udc_transmit(uep, req);
  686. break;
  687. case ISP1760_CTRL_DATA_OUT:
  688. list_add_tail(&req->queue, &uep->queue);
  689. __isp1760_udc_select_ep(uep, USB_DIR_OUT);
  690. isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
  691. break;
  692. case ISP1760_CTRL_STATUS:
  693. complete = true;
  694. break;
  695. default:
  696. dev_dbg(udc->isp->dev, "%s: invalid ep0 state\n",
  697. __func__);
  698. ret = -EINVAL;
  699. break;
  700. }
  701. } else if (uep->desc) {
  702. bool empty = list_empty(&uep->queue);
  703. list_add_tail(&req->queue, &uep->queue);
  704. if ((uep->addr & USB_DIR_IN) && !uep->halted && empty)
  705. isp1760_udc_transmit(uep, req);
  706. else if (!(uep->addr & USB_DIR_IN) && uep->rx_pending)
  707. complete = isp1760_udc_receive(uep, req);
  708. } else {
  709. dev_dbg(udc->isp->dev,
  710. "%s: can't queue request to disabled ep%02x\n",
  711. __func__, uep->addr);
  712. ret = -ESHUTDOWN;
  713. }
  714. done:
  715. if (ret < 0)
  716. req->ep = NULL;
  717. spin_unlock_irqrestore(&udc->lock, flags);
  718. if (complete)
  719. isp1760_udc_request_complete(uep, req, 0);
  720. return ret;
  721. }
  722. static int isp1760_ep_dequeue(struct usb_ep *ep, struct usb_request *_req)
  723. {
  724. struct isp1760_request *req = req_to_udc_req(_req);
  725. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  726. struct isp1760_udc *udc = uep->udc;
  727. unsigned long flags;
  728. dev_dbg(uep->udc->isp->dev, "%s(ep%02x)\n", __func__, uep->addr);
  729. spin_lock_irqsave(&udc->lock, flags);
  730. if (req->ep != uep)
  731. req = NULL;
  732. else
  733. list_del(&req->queue);
  734. spin_unlock_irqrestore(&udc->lock, flags);
  735. if (!req)
  736. return -EINVAL;
  737. isp1760_udc_request_complete(uep, req, -ECONNRESET);
  738. return 0;
  739. }
  740. static int __isp1760_ep_set_halt(struct isp1760_ep *uep, bool stall, bool wedge)
  741. {
  742. struct isp1760_udc *udc = uep->udc;
  743. int ret;
  744. if (!uep->addr) {
  745. /*
  746. * Halting the control endpoint is only valid as a delayed error
  747. * response to a SETUP packet. Make sure EP0 is in the right
  748. * stage and that the gadget isn't trying to clear the halt
  749. * condition.
  750. */
  751. if (WARN_ON(udc->ep0_state == ISP1760_CTRL_SETUP || !stall ||
  752. wedge)) {
  753. return -EINVAL;
  754. }
  755. }
  756. if (uep->addr && !uep->desc) {
  757. dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
  758. uep->addr);
  759. return -EINVAL;
  760. }
  761. if (uep->addr & USB_DIR_IN) {
  762. /* Refuse to halt IN endpoints with active transfers. */
  763. if (!list_empty(&uep->queue)) {
  764. dev_dbg(udc->isp->dev,
  765. "%s: ep%02x has request pending\n", __func__,
  766. uep->addr);
  767. return -EAGAIN;
  768. }
  769. }
  770. ret = __isp1760_udc_set_halt(uep, stall);
  771. if (ret < 0)
  772. return ret;
  773. if (!uep->addr) {
  774. /*
  775. * Stalling EP0 completes the control transaction, move back to
  776. * the SETUP state.
  777. */
  778. udc->ep0_state = ISP1760_CTRL_SETUP;
  779. return 0;
  780. }
  781. if (wedge)
  782. uep->wedged = true;
  783. else if (!stall)
  784. uep->wedged = false;
  785. return 0;
  786. }
  787. static int isp1760_ep_set_halt(struct usb_ep *ep, int value)
  788. {
  789. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  790. unsigned long flags;
  791. int ret;
  792. dev_dbg(uep->udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
  793. value ? "set" : "clear", uep->addr);
  794. spin_lock_irqsave(&uep->udc->lock, flags);
  795. ret = __isp1760_ep_set_halt(uep, value, false);
  796. spin_unlock_irqrestore(&uep->udc->lock, flags);
  797. return ret;
  798. }
  799. static int isp1760_ep_set_wedge(struct usb_ep *ep)
  800. {
  801. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  802. unsigned long flags;
  803. int ret;
  804. dev_dbg(uep->udc->isp->dev, "%s: set wedge on ep%02x)\n", __func__,
  805. uep->addr);
  806. spin_lock_irqsave(&uep->udc->lock, flags);
  807. ret = __isp1760_ep_set_halt(uep, true, true);
  808. spin_unlock_irqrestore(&uep->udc->lock, flags);
  809. return ret;
  810. }
  811. static void isp1760_ep_fifo_flush(struct usb_ep *ep)
  812. {
  813. struct isp1760_ep *uep = ep_to_udc_ep(ep);
  814. struct isp1760_udc *udc = uep->udc;
  815. unsigned long flags;
  816. spin_lock_irqsave(&udc->lock, flags);
  817. isp1760_udc_select_ep(uep);
  818. /*
  819. * Set the CLBUF bit twice to flush both buffers in case double
  820. * buffering is enabled.
  821. */
  822. isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
  823. isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
  824. spin_unlock_irqrestore(&udc->lock, flags);
  825. }
  826. static const struct usb_ep_ops isp1760_ep_ops = {
  827. .enable = isp1760_ep_enable,
  828. .disable = isp1760_ep_disable,
  829. .alloc_request = isp1760_ep_alloc_request,
  830. .free_request = isp1760_ep_free_request,
  831. .queue = isp1760_ep_queue,
  832. .dequeue = isp1760_ep_dequeue,
  833. .set_halt = isp1760_ep_set_halt,
  834. .set_wedge = isp1760_ep_set_wedge,
  835. .fifo_flush = isp1760_ep_fifo_flush,
  836. };
  837. /* -----------------------------------------------------------------------------
  838. * Device States
  839. */
  840. /* Called with the UDC spinlock held. */
  841. static void isp1760_udc_connect(struct isp1760_udc *udc)
  842. {
  843. usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
  844. mod_timer(&udc->vbus_timer, jiffies + ISP1760_VBUS_POLL_INTERVAL);
  845. }
  846. /* Called with the UDC spinlock held. */
  847. static void isp1760_udc_disconnect(struct isp1760_udc *udc)
  848. {
  849. if (udc->gadget.state < USB_STATE_POWERED)
  850. return;
  851. dev_dbg(udc->isp->dev, "Device disconnected in state %u\n",
  852. udc->gadget.state);
  853. udc->gadget.speed = USB_SPEED_UNKNOWN;
  854. usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
  855. if (udc->driver->disconnect)
  856. udc->driver->disconnect(&udc->gadget);
  857. del_timer(&udc->vbus_timer);
  858. /* TODO Reset all endpoints ? */
  859. }
  860. static void isp1760_udc_init_hw(struct isp1760_udc *udc)
  861. {
  862. /*
  863. * The device controller currently shares its interrupt with the host
  864. * controller, the DC_IRQ polarity and signaling mode are ignored. Set
  865. * the to active-low level-triggered.
  866. *
  867. * Configure the control, in and out pipes to generate interrupts on
  868. * ACK tokens only (and NYET for the out pipe). The default
  869. * configuration also generates an interrupt on the first NACK token.
  870. */
  871. isp1760_udc_write(udc, DC_INTCONF, DC_CDBGMOD_ACK | DC_DDBGMODIN_ACK |
  872. DC_DDBGMODOUT_ACK_NYET);
  873. isp1760_udc_write(udc, DC_INTENABLE, DC_IEPRXTX(7) | DC_IEPRXTX(6) |
  874. DC_IEPRXTX(5) | DC_IEPRXTX(4) | DC_IEPRXTX(3) |
  875. DC_IEPRXTX(2) | DC_IEPRXTX(1) | DC_IEPRXTX(0) |
  876. DC_IEP0SETUP | DC_IEVBUS | DC_IERESM | DC_IESUSP |
  877. DC_IEHS_STA | DC_IEBRST);
  878. if (udc->connected)
  879. isp1760_set_pullup(udc->isp, true);
  880. isp1760_udc_write(udc, DC_ADDRESS, DC_DEVEN);
  881. }
  882. static void isp1760_udc_reset(struct isp1760_udc *udc)
  883. {
  884. unsigned long flags;
  885. spin_lock_irqsave(&udc->lock, flags);
  886. /*
  887. * The bus reset has reset most registers to their default value,
  888. * reinitialize the UDC hardware.
  889. */
  890. isp1760_udc_init_hw(udc);
  891. udc->ep0_state = ISP1760_CTRL_SETUP;
  892. udc->gadget.speed = USB_SPEED_FULL;
  893. usb_gadget_udc_reset(&udc->gadget, udc->driver);
  894. spin_unlock_irqrestore(&udc->lock, flags);
  895. }
  896. static void isp1760_udc_suspend(struct isp1760_udc *udc)
  897. {
  898. if (udc->gadget.state < USB_STATE_DEFAULT)
  899. return;
  900. if (udc->driver->suspend)
  901. udc->driver->suspend(&udc->gadget);
  902. }
  903. static void isp1760_udc_resume(struct isp1760_udc *udc)
  904. {
  905. if (udc->gadget.state < USB_STATE_DEFAULT)
  906. return;
  907. if (udc->driver->resume)
  908. udc->driver->resume(&udc->gadget);
  909. }
  910. /* -----------------------------------------------------------------------------
  911. * Gadget Operations
  912. */
  913. static int isp1760_udc_get_frame(struct usb_gadget *gadget)
  914. {
  915. struct isp1760_udc *udc = gadget_to_udc(gadget);
  916. return isp1760_udc_read(udc, DC_FRAMENUM) & ((1 << 11) - 1);
  917. }
  918. static int isp1760_udc_wakeup(struct usb_gadget *gadget)
  919. {
  920. struct isp1760_udc *udc = gadget_to_udc(gadget);
  921. dev_dbg(udc->isp->dev, "%s\n", __func__);
  922. return -ENOTSUPP;
  923. }
  924. static int isp1760_udc_set_selfpowered(struct usb_gadget *gadget,
  925. int is_selfpowered)
  926. {
  927. struct isp1760_udc *udc = gadget_to_udc(gadget);
  928. if (is_selfpowered)
  929. udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
  930. else
  931. udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  932. return 0;
  933. }
  934. static int isp1760_udc_pullup(struct usb_gadget *gadget, int is_on)
  935. {
  936. struct isp1760_udc *udc = gadget_to_udc(gadget);
  937. isp1760_set_pullup(udc->isp, is_on);
  938. udc->connected = is_on;
  939. return 0;
  940. }
  941. static int isp1760_udc_start(struct usb_gadget *gadget,
  942. struct usb_gadget_driver *driver)
  943. {
  944. struct isp1760_udc *udc = gadget_to_udc(gadget);
  945. unsigned long flags;
  946. /* The hardware doesn't support low speed. */
  947. if (driver->max_speed < USB_SPEED_FULL) {
  948. dev_err(udc->isp->dev, "Invalid gadget driver\n");
  949. return -EINVAL;
  950. }
  951. spin_lock_irqsave(&udc->lock, flags);
  952. if (udc->driver) {
  953. dev_err(udc->isp->dev, "UDC already has a gadget driver\n");
  954. spin_unlock(&udc->lock);
  955. return -EBUSY;
  956. }
  957. udc->driver = driver;
  958. spin_unlock_irqrestore(&udc->lock, flags);
  959. dev_dbg(udc->isp->dev, "starting UDC with driver %s\n",
  960. driver->function);
  961. udc->devstatus = 0;
  962. udc->connected = true;
  963. usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
  964. /* DMA isn't supported yet, don't enable the DMA clock. */
  965. isp1760_udc_write(udc, DC_MODE, DC_GLINTENA);
  966. isp1760_udc_init_hw(udc);
  967. dev_dbg(udc->isp->dev, "UDC started with driver %s\n",
  968. driver->function);
  969. return 0;
  970. }
  971. static int isp1760_udc_stop(struct usb_gadget *gadget)
  972. {
  973. struct isp1760_udc *udc = gadget_to_udc(gadget);
  974. unsigned long flags;
  975. dev_dbg(udc->isp->dev, "%s\n", __func__);
  976. del_timer_sync(&udc->vbus_timer);
  977. isp1760_udc_write(udc, DC_MODE, 0);
  978. spin_lock_irqsave(&udc->lock, flags);
  979. udc->driver = NULL;
  980. spin_unlock_irqrestore(&udc->lock, flags);
  981. return 0;
  982. }
  983. static struct usb_gadget_ops isp1760_udc_ops = {
  984. .get_frame = isp1760_udc_get_frame,
  985. .wakeup = isp1760_udc_wakeup,
  986. .set_selfpowered = isp1760_udc_set_selfpowered,
  987. .pullup = isp1760_udc_pullup,
  988. .udc_start = isp1760_udc_start,
  989. .udc_stop = isp1760_udc_stop,
  990. };
  991. /* -----------------------------------------------------------------------------
  992. * Interrupt Handling
  993. */
  994. static irqreturn_t isp1760_udc_irq(int irq, void *dev)
  995. {
  996. struct isp1760_udc *udc = dev;
  997. unsigned int i;
  998. u32 status;
  999. status = isp1760_udc_read(udc, DC_INTERRUPT)
  1000. & isp1760_udc_read(udc, DC_INTENABLE);
  1001. isp1760_udc_write(udc, DC_INTERRUPT, status);
  1002. if (status & DC_IEVBUS) {
  1003. dev_dbg(udc->isp->dev, "%s(VBUS)\n", __func__);
  1004. /* The VBUS interrupt is only triggered when VBUS appears. */
  1005. spin_lock(&udc->lock);
  1006. isp1760_udc_connect(udc);
  1007. spin_unlock(&udc->lock);
  1008. }
  1009. if (status & DC_IEBRST) {
  1010. dev_dbg(udc->isp->dev, "%s(BRST)\n", __func__);
  1011. isp1760_udc_reset(udc);
  1012. }
  1013. for (i = 0; i <= 7; ++i) {
  1014. struct isp1760_ep *ep = &udc->ep[i*2];
  1015. if (status & DC_IEPTX(i)) {
  1016. dev_dbg(udc->isp->dev, "%s(EPTX%u)\n", __func__, i);
  1017. isp1760_ep_tx_complete(ep);
  1018. }
  1019. if (status & DC_IEPRX(i)) {
  1020. dev_dbg(udc->isp->dev, "%s(EPRX%u)\n", __func__, i);
  1021. isp1760_ep_rx_ready(i ? ep - 1 : ep);
  1022. }
  1023. }
  1024. if (status & DC_IEP0SETUP) {
  1025. dev_dbg(udc->isp->dev, "%s(EP0SETUP)\n", __func__);
  1026. isp1760_ep0_setup(udc);
  1027. }
  1028. if (status & DC_IERESM) {
  1029. dev_dbg(udc->isp->dev, "%s(RESM)\n", __func__);
  1030. isp1760_udc_resume(udc);
  1031. }
  1032. if (status & DC_IESUSP) {
  1033. dev_dbg(udc->isp->dev, "%s(SUSP)\n", __func__);
  1034. spin_lock(&udc->lock);
  1035. if (!(isp1760_udc_read(udc, DC_MODE) & DC_VBUSSTAT))
  1036. isp1760_udc_disconnect(udc);
  1037. else
  1038. isp1760_udc_suspend(udc);
  1039. spin_unlock(&udc->lock);
  1040. }
  1041. if (status & DC_IEHS_STA) {
  1042. dev_dbg(udc->isp->dev, "%s(HS_STA)\n", __func__);
  1043. udc->gadget.speed = USB_SPEED_HIGH;
  1044. }
  1045. return status ? IRQ_HANDLED : IRQ_NONE;
  1046. }
  1047. static void isp1760_udc_vbus_poll(unsigned long data)
  1048. {
  1049. struct isp1760_udc *udc = (struct isp1760_udc *)data;
  1050. unsigned long flags;
  1051. spin_lock_irqsave(&udc->lock, flags);
  1052. if (!(isp1760_udc_read(udc, DC_MODE) & DC_VBUSSTAT))
  1053. isp1760_udc_disconnect(udc);
  1054. else if (udc->gadget.state >= USB_STATE_POWERED)
  1055. mod_timer(&udc->vbus_timer,
  1056. jiffies + ISP1760_VBUS_POLL_INTERVAL);
  1057. spin_unlock_irqrestore(&udc->lock, flags);
  1058. }
  1059. /* -----------------------------------------------------------------------------
  1060. * Registration
  1061. */
  1062. static void isp1760_udc_init_eps(struct isp1760_udc *udc)
  1063. {
  1064. unsigned int i;
  1065. INIT_LIST_HEAD(&udc->gadget.ep_list);
  1066. for (i = 0; i < ARRAY_SIZE(udc->ep); ++i) {
  1067. struct isp1760_ep *ep = &udc->ep[i];
  1068. unsigned int ep_num = (i + 1) / 2;
  1069. bool is_in = !(i & 1);
  1070. ep->udc = udc;
  1071. INIT_LIST_HEAD(&ep->queue);
  1072. ep->addr = (ep_num && is_in ? USB_DIR_IN : USB_DIR_OUT)
  1073. | ep_num;
  1074. ep->desc = NULL;
  1075. sprintf(ep->name, "ep%u%s", ep_num,
  1076. ep_num ? (is_in ? "in" : "out") : "");
  1077. ep->ep.ops = &isp1760_ep_ops;
  1078. ep->ep.name = ep->name;
  1079. /*
  1080. * Hardcode the maximum packet sizes for now, to 64 bytes for
  1081. * the control endpoint and 512 bytes for all other endpoints.
  1082. * This fits in the 8kB FIFO without double-buffering.
  1083. */
  1084. if (ep_num == 0) {
  1085. ep->ep.maxpacket = 64;
  1086. ep->maxpacket = 64;
  1087. udc->gadget.ep0 = &ep->ep;
  1088. } else {
  1089. ep->ep.maxpacket = 512;
  1090. ep->maxpacket = 0;
  1091. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  1092. }
  1093. }
  1094. }
  1095. static int isp1760_udc_init(struct isp1760_udc *udc)
  1096. {
  1097. u16 scratch;
  1098. u32 chipid;
  1099. /*
  1100. * Check that the controller is present by writing to the scratch
  1101. * register, modifying the bus pattern by reading from the chip ID
  1102. * register, and reading the scratch register value back. The chip ID
  1103. * and scratch register contents must match the expected values.
  1104. */
  1105. isp1760_udc_write(udc, DC_SCRATCH, 0xbabe);
  1106. chipid = isp1760_udc_read(udc, DC_CHIPID);
  1107. scratch = isp1760_udc_read(udc, DC_SCRATCH);
  1108. if (scratch != 0xbabe) {
  1109. dev_err(udc->isp->dev,
  1110. "udc: scratch test failed (0x%04x/0x%08x)\n",
  1111. scratch, chipid);
  1112. return -ENODEV;
  1113. }
  1114. if (chipid != 0x00011582 && chipid != 0x00158210) {
  1115. dev_err(udc->isp->dev, "udc: invalid chip ID 0x%08x\n", chipid);
  1116. return -ENODEV;
  1117. }
  1118. /* Reset the device controller. */
  1119. isp1760_udc_write(udc, DC_MODE, DC_SFRESET);
  1120. usleep_range(10000, 11000);
  1121. isp1760_udc_write(udc, DC_MODE, 0);
  1122. usleep_range(10000, 11000);
  1123. return 0;
  1124. }
  1125. int isp1760_udc_register(struct isp1760_device *isp, int irq,
  1126. unsigned long irqflags)
  1127. {
  1128. struct isp1760_udc *udc = &isp->udc;
  1129. const char *devname;
  1130. int ret;
  1131. udc->irq = -1;
  1132. udc->isp = isp;
  1133. udc->regs = isp->regs;
  1134. spin_lock_init(&udc->lock);
  1135. setup_timer(&udc->vbus_timer, isp1760_udc_vbus_poll,
  1136. (unsigned long)udc);
  1137. ret = isp1760_udc_init(udc);
  1138. if (ret < 0)
  1139. return ret;
  1140. devname = dev_name(isp->dev);
  1141. udc->irqname = kmalloc(strlen(devname) + 7, GFP_KERNEL);
  1142. if (!udc->irqname)
  1143. return -ENOMEM;
  1144. sprintf(udc->irqname, "%s (udc)", devname);
  1145. ret = request_irq(irq, isp1760_udc_irq, IRQF_SHARED | irqflags,
  1146. udc->irqname, udc);
  1147. if (ret < 0)
  1148. goto error;
  1149. udc->irq = irq;
  1150. /*
  1151. * Initialize the gadget static fields and register its device. Gadget
  1152. * fields that vary during the life time of the gadget are initialized
  1153. * by the UDC core.
  1154. */
  1155. udc->gadget.ops = &isp1760_udc_ops;
  1156. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1157. udc->gadget.max_speed = USB_SPEED_HIGH;
  1158. udc->gadget.name = "isp1761_udc";
  1159. isp1760_udc_init_eps(udc);
  1160. ret = usb_add_gadget_udc(isp->dev, &udc->gadget);
  1161. if (ret < 0)
  1162. goto error;
  1163. return 0;
  1164. error:
  1165. if (udc->irq >= 0)
  1166. free_irq(udc->irq, udc);
  1167. kfree(udc->irqname);
  1168. return ret;
  1169. }
  1170. void isp1760_udc_unregister(struct isp1760_device *isp)
  1171. {
  1172. struct isp1760_udc *udc = &isp->udc;
  1173. if (!udc->isp)
  1174. return;
  1175. usb_del_gadget_udc(&udc->gadget);
  1176. free_irq(udc->irq, udc);
  1177. kfree(udc->irqname);
  1178. }