isp1760-udc.c 37 KB

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