lh7a40x_udc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. /*
  2. * linux/drivers/usb/gadget/lh7a40x_udc.c
  3. * Sharp LH7A40x on-chip full speed USB device controllers
  4. *
  5. * Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
  6. * Copyright (C) 2004 Bo Henriksen, Nordic ID
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include "lh7a40x_udc.h"
  24. //#define DEBUG printk
  25. //#define DEBUG_EP0 printk
  26. //#define DEBUG_SETUP printk
  27. #ifndef DEBUG_EP0
  28. # define DEBUG_EP0(fmt,args...)
  29. #endif
  30. #ifndef DEBUG_SETUP
  31. # define DEBUG_SETUP(fmt,args...)
  32. #endif
  33. #ifndef DEBUG
  34. # define NO_STATES
  35. # define DEBUG(fmt,args...)
  36. #endif
  37. #define DRIVER_DESC "LH7A40x USB Device Controller"
  38. #define DRIVER_VERSION __DATE__
  39. #ifndef _BIT /* FIXME - what happended to _BIT in 2.6.7bk18? */
  40. #define _BIT(x) (1<<(x))
  41. #endif
  42. struct lh7a40x_udc *the_controller;
  43. static const char driver_name[] = "lh7a40x_udc";
  44. static const char driver_desc[] = DRIVER_DESC;
  45. static const char ep0name[] = "ep0-control";
  46. /*
  47. Local definintions.
  48. */
  49. #ifndef NO_STATES
  50. static char *state_names[] = {
  51. "WAIT_FOR_SETUP",
  52. "DATA_STATE_XMIT",
  53. "DATA_STATE_NEED_ZLP",
  54. "WAIT_FOR_OUT_STATUS",
  55. "DATA_STATE_RECV"
  56. };
  57. #endif
  58. /*
  59. Local declarations.
  60. */
  61. static int lh7a40x_ep_enable(struct usb_ep *ep,
  62. const struct usb_endpoint_descriptor *);
  63. static int lh7a40x_ep_disable(struct usb_ep *ep);
  64. static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, int);
  65. static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
  66. static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned, dma_addr_t *,
  67. int);
  68. static void lh7a40x_free_buffer(struct usb_ep *ep, void *, dma_addr_t,
  69. unsigned);
  70. static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, int);
  71. static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
  72. static int lh7a40x_set_halt(struct usb_ep *ep, int);
  73. static int lh7a40x_fifo_status(struct usb_ep *ep);
  74. static int lh7a40x_fifo_status(struct usb_ep *ep);
  75. static void lh7a40x_fifo_flush(struct usb_ep *ep);
  76. static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
  77. static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
  78. static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
  79. int status);
  80. static void pio_irq_enable(int bEndpointAddress);
  81. static void pio_irq_disable(int bEndpointAddress);
  82. static void stop_activity(struct lh7a40x_udc *dev,
  83. struct usb_gadget_driver *driver);
  84. static void flush(struct lh7a40x_ep *ep);
  85. static void udc_enable(struct lh7a40x_udc *dev);
  86. static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
  87. static struct usb_ep_ops lh7a40x_ep_ops = {
  88. .enable = lh7a40x_ep_enable,
  89. .disable = lh7a40x_ep_disable,
  90. .alloc_request = lh7a40x_alloc_request,
  91. .free_request = lh7a40x_free_request,
  92. .alloc_buffer = lh7a40x_alloc_buffer,
  93. .free_buffer = lh7a40x_free_buffer,
  94. .queue = lh7a40x_queue,
  95. .dequeue = lh7a40x_dequeue,
  96. .set_halt = lh7a40x_set_halt,
  97. .fifo_status = lh7a40x_fifo_status,
  98. .fifo_flush = lh7a40x_fifo_flush,
  99. };
  100. /* Inline code */
  101. static __inline__ int write_packet(struct lh7a40x_ep *ep,
  102. struct lh7a40x_request *req, int max)
  103. {
  104. u8 *buf;
  105. int length, count;
  106. volatile u32 *fifo = (volatile u32 *)ep->fifo;
  107. buf = req->req.buf + req->req.actual;
  108. prefetch(buf);
  109. length = req->req.length - req->req.actual;
  110. length = min(length, max);
  111. req->req.actual += length;
  112. DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
  113. count = length;
  114. while (count--) {
  115. *fifo = *buf++;
  116. }
  117. return length;
  118. }
  119. static __inline__ void usb_set_index(u32 ep)
  120. {
  121. *(volatile u32 *)io_p2v(USB_INDEX) = ep;
  122. }
  123. static __inline__ u32 usb_read(u32 port)
  124. {
  125. return *(volatile u32 *)io_p2v(port);
  126. }
  127. static __inline__ void usb_write(u32 val, u32 port)
  128. {
  129. *(volatile u32 *)io_p2v(port) = val;
  130. }
  131. static __inline__ void usb_set(u32 val, u32 port)
  132. {
  133. volatile u32 *ioport = (volatile u32 *)io_p2v(port);
  134. u32 after = (*ioport) | val;
  135. *ioport = after;
  136. }
  137. static __inline__ void usb_clear(u32 val, u32 port)
  138. {
  139. volatile u32 *ioport = (volatile u32 *)io_p2v(port);
  140. u32 after = (*ioport) & ~val;
  141. *ioport = after;
  142. }
  143. /*-------------------------------------------------------------------------*/
  144. #define GPIO_PORTC_DR (0x80000E08)
  145. #define GPIO_PORTC_DDR (0x80000E18)
  146. #define GPIO_PORTC_PDR (0x80000E70)
  147. /* get port C pin data register */
  148. #define get_portc_pdr(bit) ((usb_read(GPIO_PORTC_PDR) & _BIT(bit)) != 0)
  149. /* get port C data direction register */
  150. #define get_portc_ddr(bit) ((usb_read(GPIO_PORTC_DDR) & _BIT(bit)) != 0)
  151. /* set port C data register */
  152. #define set_portc_dr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DR) : usb_clear(_BIT(bit), GPIO_PORTC_DR))
  153. /* set port C data direction register */
  154. #define set_portc_ddr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DDR) : usb_clear(_BIT(bit), GPIO_PORTC_DDR))
  155. /*
  156. * LPD7A404 GPIO's:
  157. * Port C bit 1 = USB Port 1 Power Enable
  158. * Port C bit 2 = USB Port 1 Data Carrier Detect
  159. */
  160. #define is_usb_connected() get_portc_pdr(2)
  161. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  162. static const char proc_node_name[] = "driver/udc";
  163. static int
  164. udc_proc_read(char *page, char **start, off_t off, int count,
  165. int *eof, void *_dev)
  166. {
  167. char *buf = page;
  168. struct lh7a40x_udc *dev = _dev;
  169. char *next = buf;
  170. unsigned size = count;
  171. unsigned long flags;
  172. int t;
  173. if (off != 0)
  174. return 0;
  175. local_irq_save(flags);
  176. /* basic device status */
  177. t = scnprintf(next, size,
  178. DRIVER_DESC "\n"
  179. "%s version: %s\n"
  180. "Gadget driver: %s\n"
  181. "Host: %s\n\n",
  182. driver_name, DRIVER_VERSION,
  183. dev->driver ? dev->driver->driver.name : "(none)",
  184. is_usb_connected()? "full speed" : "disconnected");
  185. size -= t;
  186. next += t;
  187. t = scnprintf(next, size,
  188. "GPIO:\n"
  189. " Port C bit 1: %d, dir %d\n"
  190. " Port C bit 2: %d, dir %d\n\n",
  191. get_portc_pdr(1), get_portc_ddr(1),
  192. get_portc_pdr(2), get_portc_ddr(2)
  193. );
  194. size -= t;
  195. next += t;
  196. t = scnprintf(next, size,
  197. "DCP pullup: %d\n\n",
  198. (usb_read(USB_PM) & PM_USB_DCP) != 0);
  199. size -= t;
  200. next += t;
  201. local_irq_restore(flags);
  202. *eof = 1;
  203. return count - size;
  204. }
  205. #define create_proc_files() create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
  206. #define remove_proc_files() remove_proc_entry(proc_node_name, NULL)
  207. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  208. #define create_proc_files() do {} while (0)
  209. #define remove_proc_files() do {} while (0)
  210. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  211. /*
  212. * udc_disable - disable USB device controller
  213. */
  214. static void udc_disable(struct lh7a40x_udc *dev)
  215. {
  216. DEBUG("%s, %p\n", __FUNCTION__, dev);
  217. udc_set_address(dev, 0);
  218. /* Disable interrupts */
  219. usb_write(0, USB_IN_INT_EN);
  220. usb_write(0, USB_OUT_INT_EN);
  221. usb_write(0, USB_INT_EN);
  222. /* Disable the USB */
  223. usb_write(0, USB_PM);
  224. #ifdef CONFIG_ARCH_LH7A404
  225. /* Disable USB power */
  226. set_portc_dr(1, 0);
  227. #endif
  228. /* if hardware supports it, disconnect from usb */
  229. /* make_usb_disappear(); */
  230. dev->ep0state = WAIT_FOR_SETUP;
  231. dev->gadget.speed = USB_SPEED_UNKNOWN;
  232. dev->usb_address = 0;
  233. }
  234. /*
  235. * udc_reinit - initialize software state
  236. */
  237. static void udc_reinit(struct lh7a40x_udc *dev)
  238. {
  239. u32 i;
  240. DEBUG("%s, %p\n", __FUNCTION__, dev);
  241. /* device/ep0 records init */
  242. INIT_LIST_HEAD(&dev->gadget.ep_list);
  243. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  244. dev->ep0state = WAIT_FOR_SETUP;
  245. /* basic endpoint records init */
  246. for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
  247. struct lh7a40x_ep *ep = &dev->ep[i];
  248. if (i != 0)
  249. list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
  250. ep->desc = 0;
  251. ep->stopped = 0;
  252. INIT_LIST_HEAD(&ep->queue);
  253. ep->pio_irqs = 0;
  254. }
  255. /* the rest was statically initialized, and is read-only */
  256. }
  257. #define BYTES2MAXP(x) (x / 8)
  258. #define MAXP2BYTES(x) (x * 8)
  259. /* until it's enabled, this UDC should be completely invisible
  260. * to any USB host.
  261. */
  262. static void udc_enable(struct lh7a40x_udc *dev)
  263. {
  264. int ep;
  265. DEBUG("%s, %p\n", __FUNCTION__, dev);
  266. dev->gadget.speed = USB_SPEED_UNKNOWN;
  267. #ifdef CONFIG_ARCH_LH7A404
  268. /* Set Port C bit 1 & 2 as output */
  269. set_portc_ddr(1, 1);
  270. set_portc_ddr(2, 1);
  271. /* Enable USB power */
  272. set_portc_dr(1, 0);
  273. #endif
  274. /*
  275. * C.f Chapter 18.1.3.1 Initializing the USB
  276. */
  277. /* Disable the USB */
  278. usb_clear(PM_USB_ENABLE, USB_PM);
  279. /* Reset APB & I/O sides of the USB */
  280. usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
  281. mdelay(5);
  282. usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
  283. /* Set MAXP values for each */
  284. for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
  285. struct lh7a40x_ep *ep_reg = &dev->ep[ep];
  286. u32 csr;
  287. usb_set_index(ep);
  288. switch (ep_reg->ep_type) {
  289. case ep_bulk_in:
  290. case ep_interrupt:
  291. usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
  292. ep_reg->csr2);
  293. /* Fall through */
  294. case ep_control:
  295. usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
  296. USB_IN_MAXP);
  297. break;
  298. case ep_bulk_out:
  299. usb_clear(USB_OUT_CSR2_USB_DMA_EN |
  300. USB_OUT_CSR2_AUTO_CLR, ep_reg->csr2);
  301. usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
  302. USB_OUT_MAXP);
  303. break;
  304. }
  305. /* Read & Write CSR1, just in case */
  306. csr = usb_read(ep_reg->csr1);
  307. usb_write(csr, ep_reg->csr1);
  308. flush(ep_reg);
  309. }
  310. /* Disable interrupts */
  311. usb_write(0, USB_IN_INT_EN);
  312. usb_write(0, USB_OUT_INT_EN);
  313. usb_write(0, USB_INT_EN);
  314. /* Enable interrupts */
  315. usb_set(USB_IN_INT_EP0, USB_IN_INT_EN);
  316. usb_set(USB_INT_RESET_INT | USB_INT_RESUME_INT, USB_INT_EN);
  317. /* Dont enable rest of the interrupts */
  318. /* usb_set(USB_IN_INT_EP3 | USB_IN_INT_EP1 | USB_IN_INT_EP0, USB_IN_INT_EN);
  319. usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN); */
  320. /* Enable SUSPEND */
  321. usb_set(PM_ENABLE_SUSPEND, USB_PM);
  322. /* Enable the USB */
  323. usb_set(PM_USB_ENABLE, USB_PM);
  324. #ifdef CONFIG_ARCH_LH7A404
  325. /* NOTE: DOES NOT WORK! */
  326. /* Let host detect UDC:
  327. * Software must write a 0 to the PMR:DCP_CTRL bit to turn this
  328. * transistor on and pull the USBDP pin HIGH.
  329. */
  330. /* usb_clear(PM_USB_DCP, USB_PM);
  331. usb_set(PM_USB_DCP, USB_PM); */
  332. #endif
  333. }
  334. /*
  335. Register entry point for the peripheral controller driver.
  336. */
  337. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  338. {
  339. struct lh7a40x_udc *dev = the_controller;
  340. int retval;
  341. DEBUG("%s: %s\n", __FUNCTION__, driver->driver.name);
  342. if (!driver
  343. || driver->speed != USB_SPEED_FULL
  344. || !driver->bind
  345. || !driver->unbind || !driver->disconnect || !driver->setup)
  346. return -EINVAL;
  347. if (!dev)
  348. return -ENODEV;
  349. if (dev->driver)
  350. return -EBUSY;
  351. /* first hook up the driver ... */
  352. dev->driver = driver;
  353. dev->gadget.dev.driver = &driver->driver;
  354. device_add(&dev->gadget.dev);
  355. retval = driver->bind(&dev->gadget);
  356. if (retval) {
  357. printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
  358. driver->driver.name, retval);
  359. device_del(&dev->gadget.dev);
  360. dev->driver = 0;
  361. dev->gadget.dev.driver = 0;
  362. return retval;
  363. }
  364. /* ... then enable host detection and ep0; and we're ready
  365. * for set_configuration as well as eventual disconnect.
  366. * NOTE: this shouldn't power up until later.
  367. */
  368. printk("%s: registered gadget driver '%s'\n", dev->gadget.name,
  369. driver->driver.name);
  370. udc_enable(dev);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL(usb_gadget_register_driver);
  374. /*
  375. Unregister entry point for the peripheral controller driver.
  376. */
  377. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  378. {
  379. struct lh7a40x_udc *dev = the_controller;
  380. unsigned long flags;
  381. if (!dev)
  382. return -ENODEV;
  383. if (!driver || driver != dev->driver)
  384. return -EINVAL;
  385. spin_lock_irqsave(&dev->lock, flags);
  386. dev->driver = 0;
  387. stop_activity(dev, driver);
  388. spin_unlock_irqrestore(&dev->lock, flags);
  389. driver->unbind(&dev->gadget);
  390. device_del(&dev->gadget.dev);
  391. udc_disable(dev);
  392. DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
  393. return 0;
  394. }
  395. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  396. /*-------------------------------------------------------------------------*/
  397. /** Write request to FIFO (max write == maxp size)
  398. * Return: 0 = still running, 1 = completed, negative = errno
  399. * NOTE: INDEX register must be set for EP
  400. */
  401. static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
  402. {
  403. u32 max;
  404. u32 csr;
  405. max = le16_to_cpu(ep->desc->wMaxPacketSize);
  406. csr = usb_read(ep->csr1);
  407. DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
  408. if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
  409. unsigned count;
  410. int is_last, is_short;
  411. count = write_packet(ep, req, max);
  412. usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
  413. /* last packet is usually short (or a zlp) */
  414. if (unlikely(count != max))
  415. is_last = is_short = 1;
  416. else {
  417. if (likely(req->req.length != req->req.actual)
  418. || req->req.zero)
  419. is_last = 0;
  420. else
  421. is_last = 1;
  422. /* interrupt/iso maxpacket may not fill the fifo */
  423. is_short = unlikely(max < ep_maxpacket(ep));
  424. }
  425. DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
  426. ep->ep.name, count,
  427. is_last ? "/L" : "", is_short ? "/S" : "",
  428. req->req.length - req->req.actual, req);
  429. /* requests complete when all IN data is in the FIFO */
  430. if (is_last) {
  431. done(ep, req, 0);
  432. if (list_empty(&ep->queue)) {
  433. pio_irq_disable(ep_index(ep));
  434. }
  435. return 1;
  436. }
  437. } else {
  438. DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
  439. }
  440. return 0;
  441. }
  442. /** Read to request from FIFO (max read == bytes in fifo)
  443. * Return: 0 = still running, 1 = completed, negative = errno
  444. * NOTE: INDEX register must be set for EP
  445. */
  446. static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
  447. {
  448. u32 csr;
  449. u8 *buf;
  450. unsigned bufferspace, count, is_short;
  451. volatile u32 *fifo = (volatile u32 *)ep->fifo;
  452. /* make sure there's a packet in the FIFO. */
  453. csr = usb_read(ep->csr1);
  454. if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY)) {
  455. DEBUG("%s: Packet NOT ready!\n", __FUNCTION__);
  456. return -EINVAL;
  457. }
  458. buf = req->req.buf + req->req.actual;
  459. prefetchw(buf);
  460. bufferspace = req->req.length - req->req.actual;
  461. /* read all bytes from this packet */
  462. count = usb_read(USB_OUT_FIFO_WC1);
  463. req->req.actual += min(count, bufferspace);
  464. is_short = (count < ep->ep.maxpacket);
  465. DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
  466. ep->ep.name, csr, count,
  467. is_short ? "/S" : "", req, req->req.actual, req->req.length);
  468. while (likely(count-- != 0)) {
  469. u8 byte = (u8) (*fifo & 0xff);
  470. if (unlikely(bufferspace == 0)) {
  471. /* this happens when the driver's buffer
  472. * is smaller than what the host sent.
  473. * discard the extra data.
  474. */
  475. if (req->req.status != -EOVERFLOW)
  476. printk("%s overflow %d\n", ep->ep.name, count);
  477. req->req.status = -EOVERFLOW;
  478. } else {
  479. *buf++ = byte;
  480. bufferspace--;
  481. }
  482. }
  483. usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
  484. /* completion */
  485. if (is_short || req->req.actual == req->req.length) {
  486. done(ep, req, 0);
  487. usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
  488. if (list_empty(&ep->queue))
  489. pio_irq_disable(ep_index(ep));
  490. return 1;
  491. }
  492. /* finished that packet. the next one may be waiting... */
  493. return 0;
  494. }
  495. /*
  496. * done - retire a request; caller blocked irqs
  497. * INDEX register is preserved to keep same
  498. */
  499. static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
  500. {
  501. unsigned int stopped = ep->stopped;
  502. u32 index;
  503. DEBUG("%s, %p\n", __FUNCTION__, ep);
  504. list_del_init(&req->queue);
  505. if (likely(req->req.status == -EINPROGRESS))
  506. req->req.status = status;
  507. else
  508. status = req->req.status;
  509. if (status && status != -ESHUTDOWN)
  510. DEBUG("complete %s req %p stat %d len %u/%u\n",
  511. ep->ep.name, &req->req, status,
  512. req->req.actual, req->req.length);
  513. /* don't modify queue heads during completion callback */
  514. ep->stopped = 1;
  515. /* Read current index (completion may modify it) */
  516. index = usb_read(USB_INDEX);
  517. spin_unlock(&ep->dev->lock);
  518. req->req.complete(&ep->ep, &req->req);
  519. spin_lock(&ep->dev->lock);
  520. /* Restore index */
  521. usb_set_index(index);
  522. ep->stopped = stopped;
  523. }
  524. /** Enable EP interrupt */
  525. static void pio_irq_enable(int ep)
  526. {
  527. DEBUG("%s: %d\n", __FUNCTION__, ep);
  528. switch (ep) {
  529. case 1:
  530. usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
  531. break;
  532. case 2:
  533. usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
  534. break;
  535. case 3:
  536. usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
  537. break;
  538. default:
  539. DEBUG("Unknown endpoint: %d\n", ep);
  540. break;
  541. }
  542. }
  543. /** Disable EP interrupt */
  544. static void pio_irq_disable(int ep)
  545. {
  546. DEBUG("%s: %d\n", __FUNCTION__, ep);
  547. switch (ep) {
  548. case 1:
  549. usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
  550. break;
  551. case 2:
  552. usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
  553. break;
  554. case 3:
  555. usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
  556. break;
  557. default:
  558. DEBUG("Unknown endpoint: %d\n", ep);
  559. break;
  560. }
  561. }
  562. /*
  563. * nuke - dequeue ALL requests
  564. */
  565. void nuke(struct lh7a40x_ep *ep, int status)
  566. {
  567. struct lh7a40x_request *req;
  568. DEBUG("%s, %p\n", __FUNCTION__, ep);
  569. /* Flush FIFO */
  570. flush(ep);
  571. /* called with irqs blocked */
  572. while (!list_empty(&ep->queue)) {
  573. req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
  574. done(ep, req, status);
  575. }
  576. /* Disable IRQ if EP is enabled (has descriptor) */
  577. if (ep->desc)
  578. pio_irq_disable(ep_index(ep));
  579. }
  580. /*
  581. void nuke_all(struct lh7a40x_udc *dev)
  582. {
  583. int n;
  584. for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
  585. struct lh7a40x_ep *ep = &dev->ep[n];
  586. usb_set_index(n);
  587. nuke(ep, 0);
  588. }
  589. }*/
  590. /*
  591. static void flush_all(struct lh7a40x_udc *dev)
  592. {
  593. int n;
  594. for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
  595. {
  596. struct lh7a40x_ep *ep = &dev->ep[n];
  597. flush(ep);
  598. }
  599. }
  600. */
  601. /** Flush EP
  602. * NOTE: INDEX register must be set before this call
  603. */
  604. static void flush(struct lh7a40x_ep *ep)
  605. {
  606. DEBUG("%s, %p\n", __FUNCTION__, ep);
  607. switch (ep->ep_type) {
  608. case ep_control:
  609. /* check, by implication c.f. 15.1.2.11 */
  610. break;
  611. case ep_bulk_in:
  612. case ep_interrupt:
  613. /* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
  614. usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
  615. break;
  616. case ep_bulk_out:
  617. /* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
  618. usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
  619. break;
  620. }
  621. }
  622. /**
  623. * lh7a40x_in_epn - handle IN interrupt
  624. */
  625. static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
  626. {
  627. u32 csr;
  628. struct lh7a40x_ep *ep = &dev->ep[ep_idx];
  629. struct lh7a40x_request *req;
  630. usb_set_index(ep_idx);
  631. csr = usb_read(ep->csr1);
  632. DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
  633. if (csr & USB_IN_CSR1_SENT_STALL) {
  634. DEBUG("USB_IN_CSR1_SENT_STALL\n");
  635. usb_set(USB_IN_CSR1_SENT_STALL /*|USB_IN_CSR1_SEND_STALL */ ,
  636. ep->csr1);
  637. return;
  638. }
  639. if (!ep->desc) {
  640. DEBUG("%s: NO EP DESC\n", __FUNCTION__);
  641. return;
  642. }
  643. if (list_empty(&ep->queue))
  644. req = 0;
  645. else
  646. req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
  647. DEBUG("req: %p\n", req);
  648. if (!req)
  649. return;
  650. write_fifo(ep, req);
  651. }
  652. /* ********************************************************************************************* */
  653. /* Bulk OUT (recv)
  654. */
  655. static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
  656. {
  657. struct lh7a40x_ep *ep = &dev->ep[ep_idx];
  658. struct lh7a40x_request *req;
  659. DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
  660. usb_set_index(ep_idx);
  661. if (ep->desc) {
  662. u32 csr;
  663. csr = usb_read(ep->csr1);
  664. while ((csr =
  665. usb_read(ep->
  666. csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
  667. USB_OUT_CSR1_SENT_STALL)) {
  668. DEBUG("%s: %x\n", __FUNCTION__, csr);
  669. if (csr & USB_OUT_CSR1_SENT_STALL) {
  670. DEBUG("%s: stall sent, flush fifo\n",
  671. __FUNCTION__);
  672. /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
  673. flush(ep);
  674. } else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
  675. if (list_empty(&ep->queue))
  676. req = 0;
  677. else
  678. req =
  679. list_entry(ep->queue.next,
  680. struct lh7a40x_request,
  681. queue);
  682. if (!req) {
  683. printk("%s: NULL REQ %d\n",
  684. __FUNCTION__, ep_idx);
  685. flush(ep);
  686. break;
  687. } else {
  688. read_fifo(ep, req);
  689. }
  690. }
  691. }
  692. } else {
  693. /* Throw packet away.. */
  694. printk("%s: No descriptor?!?\n", __FUNCTION__);
  695. flush(ep);
  696. }
  697. }
  698. static void stop_activity(struct lh7a40x_udc *dev,
  699. struct usb_gadget_driver *driver)
  700. {
  701. int i;
  702. /* don't disconnect drivers more than once */
  703. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  704. driver = 0;
  705. dev->gadget.speed = USB_SPEED_UNKNOWN;
  706. /* prevent new request submissions, kill any outstanding requests */
  707. for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
  708. struct lh7a40x_ep *ep = &dev->ep[i];
  709. ep->stopped = 1;
  710. usb_set_index(i);
  711. nuke(ep, -ESHUTDOWN);
  712. }
  713. /* report disconnect; the driver is already quiesced */
  714. if (driver) {
  715. spin_unlock(&dev->lock);
  716. driver->disconnect(&dev->gadget);
  717. spin_lock(&dev->lock);
  718. }
  719. /* re-init driver-visible data structures */
  720. udc_reinit(dev);
  721. }
  722. /** Handle USB RESET interrupt
  723. */
  724. static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
  725. {
  726. #if 0 /* def CONFIG_ARCH_LH7A404 */
  727. /* Does not work always... */
  728. DEBUG("%s: %d\n", __FUNCTION__, dev->usb_address);
  729. if (!dev->usb_address) {
  730. /*usb_set(USB_RESET_IO, USB_RESET);
  731. mdelay(5);
  732. usb_clear(USB_RESET_IO, USB_RESET); */
  733. return;
  734. }
  735. /* Put the USB controller into reset. */
  736. usb_set(USB_RESET_IO, USB_RESET);
  737. /* Set Device ID to 0 */
  738. udc_set_address(dev, 0);
  739. /* Let PLL2 settle down */
  740. mdelay(5);
  741. /* Release the USB controller from reset */
  742. usb_clear(USB_RESET_IO, USB_RESET);
  743. /* Re-enable UDC */
  744. udc_enable(dev);
  745. #endif
  746. dev->gadget.speed = USB_SPEED_FULL;
  747. }
  748. /*
  749. * lh7a40x usb client interrupt handler.
  750. */
  751. static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev, struct pt_regs *r)
  752. {
  753. struct lh7a40x_udc *dev = _dev;
  754. DEBUG("\n\n");
  755. spin_lock(&dev->lock);
  756. for (;;) {
  757. u32 intr_in = usb_read(USB_IN_INT);
  758. u32 intr_out = usb_read(USB_OUT_INT);
  759. u32 intr_int = usb_read(USB_INT);
  760. /* Test also against enable bits.. (lh7a40x errata).. Sigh.. */
  761. u32 in_en = usb_read(USB_IN_INT_EN);
  762. u32 out_en = usb_read(USB_OUT_INT_EN);
  763. if (!intr_out && !intr_in && !intr_int)
  764. break;
  765. DEBUG("%s (on state %s)\n", __FUNCTION__,
  766. state_names[dev->ep0state]);
  767. DEBUG("intr_out = %x\n", intr_out);
  768. DEBUG("intr_in = %x\n", intr_in);
  769. DEBUG("intr_int = %x\n", intr_int);
  770. if (intr_in) {
  771. usb_write(intr_in, USB_IN_INT);
  772. if ((intr_in & USB_IN_INT_EP1)
  773. && (in_en & USB_IN_INT_EP1)) {
  774. DEBUG("USB_IN_INT_EP1\n");
  775. lh7a40x_in_epn(dev, 1, intr_in);
  776. }
  777. if ((intr_in & USB_IN_INT_EP3)
  778. && (in_en & USB_IN_INT_EP3)) {
  779. DEBUG("USB_IN_INT_EP3\n");
  780. lh7a40x_in_epn(dev, 3, intr_in);
  781. }
  782. if (intr_in & USB_IN_INT_EP0) {
  783. DEBUG("USB_IN_INT_EP0 (control)\n");
  784. lh7a40x_handle_ep0(dev, intr_in);
  785. }
  786. }
  787. if (intr_out) {
  788. usb_write(intr_out, USB_OUT_INT);
  789. if ((intr_out & USB_OUT_INT_EP2)
  790. && (out_en & USB_OUT_INT_EP2)) {
  791. DEBUG("USB_OUT_INT_EP2\n");
  792. lh7a40x_out_epn(dev, 2, intr_out);
  793. }
  794. }
  795. if (intr_int) {
  796. usb_write(intr_int, USB_INT);
  797. if (intr_int & USB_INT_RESET_INT) {
  798. lh7a40x_reset_intr(dev);
  799. }
  800. if (intr_int & USB_INT_RESUME_INT) {
  801. DEBUG("USB resume\n");
  802. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  803. && dev->driver
  804. && dev->driver->resume
  805. && is_usb_connected()) {
  806. dev->driver->resume(&dev->gadget);
  807. }
  808. }
  809. if (intr_int & USB_INT_SUSPEND_INT) {
  810. DEBUG("USB suspend%s\n",
  811. is_usb_connected()? "" : "+disconnect");
  812. if (!is_usb_connected()) {
  813. stop_activity(dev, dev->driver);
  814. } else if (dev->gadget.speed !=
  815. USB_SPEED_UNKNOWN && dev->driver
  816. && dev->driver->suspend) {
  817. dev->driver->suspend(&dev->gadget);
  818. }
  819. }
  820. }
  821. }
  822. spin_unlock(&dev->lock);
  823. return IRQ_HANDLED;
  824. }
  825. static int lh7a40x_ep_enable(struct usb_ep *_ep,
  826. const struct usb_endpoint_descriptor *desc)
  827. {
  828. struct lh7a40x_ep *ep;
  829. struct lh7a40x_udc *dev;
  830. unsigned long flags;
  831. DEBUG("%s, %p\n", __FUNCTION__, _ep);
  832. ep = container_of(_ep, struct lh7a40x_ep, ep);
  833. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  834. || desc->bDescriptorType != USB_DT_ENDPOINT
  835. || ep->bEndpointAddress != desc->bEndpointAddress
  836. || ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
  837. DEBUG("%s, bad ep or descriptor\n", __FUNCTION__);
  838. return -EINVAL;
  839. }
  840. /* xfer types must match, except that interrupt ~= bulk */
  841. if (ep->bmAttributes != desc->bmAttributes
  842. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  843. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  844. DEBUG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
  845. return -EINVAL;
  846. }
  847. /* hardware _could_ do smaller, but driver doesn't */
  848. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  849. && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
  850. || !desc->wMaxPacketSize) {
  851. DEBUG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
  852. return -ERANGE;
  853. }
  854. dev = ep->dev;
  855. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  856. DEBUG("%s, bogus device state\n", __FUNCTION__);
  857. return -ESHUTDOWN;
  858. }
  859. spin_lock_irqsave(&ep->dev->lock, flags);
  860. ep->stopped = 0;
  861. ep->desc = desc;
  862. ep->pio_irqs = 0;
  863. ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  864. /* Reset halt state (does flush) */
  865. lh7a40x_set_halt(_ep, 0);
  866. spin_unlock_irqrestore(&ep->dev->lock, flags);
  867. DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
  868. return 0;
  869. }
  870. /** Disable EP
  871. * NOTE: Sets INDEX register
  872. */
  873. static int lh7a40x_ep_disable(struct usb_ep *_ep)
  874. {
  875. struct lh7a40x_ep *ep;
  876. unsigned long flags;
  877. DEBUG("%s, %p\n", __FUNCTION__, _ep);
  878. ep = container_of(_ep, struct lh7a40x_ep, ep);
  879. if (!_ep || !ep->desc) {
  880. DEBUG("%s, %s not enabled\n", __FUNCTION__,
  881. _ep ? ep->ep.name : NULL);
  882. return -EINVAL;
  883. }
  884. spin_lock_irqsave(&ep->dev->lock, flags);
  885. usb_set_index(ep_index(ep));
  886. /* Nuke all pending requests (does flush) */
  887. nuke(ep, -ESHUTDOWN);
  888. /* Disable ep IRQ */
  889. pio_irq_disable(ep_index(ep));
  890. ep->desc = 0;
  891. ep->stopped = 1;
  892. spin_unlock_irqrestore(&ep->dev->lock, flags);
  893. DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
  894. return 0;
  895. }
  896. static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
  897. unsigned gfp_flags)
  898. {
  899. struct lh7a40x_request *req;
  900. DEBUG("%s, %p\n", __FUNCTION__, ep);
  901. req = kmalloc(sizeof *req, gfp_flags);
  902. if (!req)
  903. return 0;
  904. memset(req, 0, sizeof *req);
  905. INIT_LIST_HEAD(&req->queue);
  906. return &req->req;
  907. }
  908. static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
  909. {
  910. struct lh7a40x_request *req;
  911. DEBUG("%s, %p\n", __FUNCTION__, ep);
  912. req = container_of(_req, struct lh7a40x_request, req);
  913. WARN_ON(!list_empty(&req->queue));
  914. kfree(req);
  915. }
  916. static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned bytes,
  917. dma_addr_t * dma, unsigned gfp_flags)
  918. {
  919. char *retval;
  920. DEBUG("%s (%p, %d, %d)\n", __FUNCTION__, ep, bytes, gfp_flags);
  921. retval = kmalloc(bytes, gfp_flags & ~(__GFP_DMA | __GFP_HIGHMEM));
  922. if (retval)
  923. *dma = virt_to_bus(retval);
  924. return retval;
  925. }
  926. static void lh7a40x_free_buffer(struct usb_ep *ep, void *buf, dma_addr_t dma,
  927. unsigned bytes)
  928. {
  929. DEBUG("%s, %p\n", __FUNCTION__, ep);
  930. kfree(buf);
  931. }
  932. /** Queue one request
  933. * Kickstart transfer if needed
  934. * NOTE: Sets INDEX register
  935. */
  936. static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
  937. unsigned gfp_flags)
  938. {
  939. struct lh7a40x_request *req;
  940. struct lh7a40x_ep *ep;
  941. struct lh7a40x_udc *dev;
  942. unsigned long flags;
  943. DEBUG("\n\n\n%s, %p\n", __FUNCTION__, _ep);
  944. req = container_of(_req, struct lh7a40x_request, req);
  945. if (unlikely
  946. (!_req || !_req->complete || !_req->buf
  947. || !list_empty(&req->queue))) {
  948. DEBUG("%s, bad params\n", __FUNCTION__);
  949. return -EINVAL;
  950. }
  951. ep = container_of(_ep, struct lh7a40x_ep, ep);
  952. if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  953. DEBUG("%s, bad ep\n", __FUNCTION__);
  954. return -EINVAL;
  955. }
  956. dev = ep->dev;
  957. if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  958. DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
  959. return -ESHUTDOWN;
  960. }
  961. DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
  962. _req->buf);
  963. spin_lock_irqsave(&dev->lock, flags);
  964. _req->status = -EINPROGRESS;
  965. _req->actual = 0;
  966. /* kickstart this i/o queue? */
  967. DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
  968. ep->stopped);
  969. if (list_empty(&ep->queue) && likely(!ep->stopped)) {
  970. u32 csr;
  971. if (unlikely(ep_index(ep) == 0)) {
  972. /* EP0 */
  973. list_add_tail(&req->queue, &ep->queue);
  974. lh7a40x_ep0_kick(dev, ep);
  975. req = 0;
  976. } else if (ep_is_in(ep)) {
  977. /* EP1 & EP3 */
  978. usb_set_index(ep_index(ep));
  979. csr = usb_read(ep->csr1);
  980. pio_irq_enable(ep_index(ep));
  981. if ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY) == 0) {
  982. if (write_fifo(ep, req) == 1)
  983. req = 0;
  984. }
  985. } else {
  986. /* EP2 */
  987. usb_set_index(ep_index(ep));
  988. csr = usb_read(ep->csr1);
  989. pio_irq_enable(ep_index(ep));
  990. if (!(csr & USB_OUT_CSR1_FIFO_FULL)) {
  991. if (read_fifo(ep, req) == 1)
  992. req = 0;
  993. }
  994. }
  995. }
  996. /* pio or dma irq handler advances the queue. */
  997. if (likely(req != 0))
  998. list_add_tail(&req->queue, &ep->queue);
  999. spin_unlock_irqrestore(&dev->lock, flags);
  1000. return 0;
  1001. }
  1002. /* dequeue JUST ONE request */
  1003. static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1004. {
  1005. struct lh7a40x_ep *ep;
  1006. struct lh7a40x_request *req;
  1007. unsigned long flags;
  1008. DEBUG("%s, %p\n", __FUNCTION__, _ep);
  1009. ep = container_of(_ep, struct lh7a40x_ep, ep);
  1010. if (!_ep || ep->ep.name == ep0name)
  1011. return -EINVAL;
  1012. spin_lock_irqsave(&ep->dev->lock, flags);
  1013. /* make sure it's actually queued on this endpoint */
  1014. list_for_each_entry(req, &ep->queue, queue) {
  1015. if (&req->req == _req)
  1016. break;
  1017. }
  1018. if (&req->req != _req) {
  1019. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1020. return -EINVAL;
  1021. }
  1022. done(ep, req, -ECONNRESET);
  1023. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1024. return 0;
  1025. }
  1026. /** Halt specific EP
  1027. * Return 0 if success
  1028. * NOTE: Sets INDEX register to EP !
  1029. */
  1030. static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
  1031. {
  1032. struct lh7a40x_ep *ep;
  1033. unsigned long flags;
  1034. ep = container_of(_ep, struct lh7a40x_ep, ep);
  1035. if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  1036. DEBUG("%s, bad ep\n", __FUNCTION__);
  1037. return -EINVAL;
  1038. }
  1039. usb_set_index(ep_index(ep));
  1040. DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
  1041. spin_lock_irqsave(&ep->dev->lock, flags);
  1042. if (ep_index(ep) == 0) {
  1043. /* EP0 */
  1044. usb_set(EP0_SEND_STALL, ep->csr1);
  1045. } else if (ep_is_in(ep)) {
  1046. u32 csr = usb_read(ep->csr1);
  1047. if (value && ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY)
  1048. || !list_empty(&ep->queue))) {
  1049. /*
  1050. * Attempts to halt IN endpoints will fail (returning -EAGAIN)
  1051. * if any transfer requests are still queued, or if the controller
  1052. * FIFO still holds bytes that the host hasn’t collected.
  1053. */
  1054. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1055. DEBUG
  1056. ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
  1057. (csr & USB_IN_CSR1_FIFO_NOT_EMPTY),
  1058. !list_empty(&ep->queue));
  1059. return -EAGAIN;
  1060. }
  1061. flush(ep);
  1062. if (value)
  1063. usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
  1064. else {
  1065. usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
  1066. usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
  1067. }
  1068. } else {
  1069. flush(ep);
  1070. if (value)
  1071. usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
  1072. else {
  1073. usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
  1074. usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
  1075. }
  1076. }
  1077. if (value) {
  1078. ep->stopped = 1;
  1079. } else {
  1080. ep->stopped = 0;
  1081. }
  1082. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1083. DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
  1084. return 0;
  1085. }
  1086. /** Return bytes in EP FIFO
  1087. * NOTE: Sets INDEX register to EP
  1088. */
  1089. static int lh7a40x_fifo_status(struct usb_ep *_ep)
  1090. {
  1091. u32 csr;
  1092. int count = 0;
  1093. struct lh7a40x_ep *ep;
  1094. ep = container_of(_ep, struct lh7a40x_ep, ep);
  1095. if (!_ep) {
  1096. DEBUG("%s, bad ep\n", __FUNCTION__);
  1097. return -ENODEV;
  1098. }
  1099. DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
  1100. /* LPD can't report unclaimed bytes from IN fifos */
  1101. if (ep_is_in(ep))
  1102. return -EOPNOTSUPP;
  1103. usb_set_index(ep_index(ep));
  1104. csr = usb_read(ep->csr1);
  1105. if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
  1106. csr & USB_OUT_CSR1_OUT_PKT_RDY) {
  1107. count = usb_read(USB_OUT_FIFO_WC1);
  1108. }
  1109. return count;
  1110. }
  1111. /** Flush EP FIFO
  1112. * NOTE: Sets INDEX register to EP
  1113. */
  1114. static void lh7a40x_fifo_flush(struct usb_ep *_ep)
  1115. {
  1116. struct lh7a40x_ep *ep;
  1117. ep = container_of(_ep, struct lh7a40x_ep, ep);
  1118. if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  1119. DEBUG("%s, bad ep\n", __FUNCTION__);
  1120. return;
  1121. }
  1122. usb_set_index(ep_index(ep));
  1123. flush(ep);
  1124. }
  1125. /****************************************************************/
  1126. /* End Point 0 related functions */
  1127. /****************************************************************/
  1128. /* return: 0 = still running, 1 = completed, negative = errno */
  1129. static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
  1130. {
  1131. u32 max;
  1132. unsigned count;
  1133. int is_last;
  1134. max = ep_maxpacket(ep);
  1135. DEBUG_EP0("%s\n", __FUNCTION__);
  1136. count = write_packet(ep, req, max);
  1137. /* last packet is usually short (or a zlp) */
  1138. if (unlikely(count != max))
  1139. is_last = 1;
  1140. else {
  1141. if (likely(req->req.length != req->req.actual) || req->req.zero)
  1142. is_last = 0;
  1143. else
  1144. is_last = 1;
  1145. }
  1146. DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
  1147. ep->ep.name, count,
  1148. is_last ? "/L" : "", req->req.length - req->req.actual, req);
  1149. /* requests complete when all IN data is in the FIFO */
  1150. if (is_last) {
  1151. done(ep, req, 0);
  1152. return 1;
  1153. }
  1154. return 0;
  1155. }
  1156. static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
  1157. unsigned char *cp, int max)
  1158. {
  1159. int bytes;
  1160. int count = usb_read(USB_OUT_FIFO_WC1);
  1161. volatile u32 *fifo = (volatile u32 *)ep->fifo;
  1162. if (count > max)
  1163. count = max;
  1164. bytes = count;
  1165. while (count--)
  1166. *cp++ = *fifo & 0xFF;
  1167. return bytes;
  1168. }
  1169. static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
  1170. unsigned char *cp, int count)
  1171. {
  1172. volatile u32 *fifo = (volatile u32 *)ep->fifo;
  1173. DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
  1174. while (count--)
  1175. *fifo = *cp++;
  1176. }
  1177. static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
  1178. {
  1179. u32 csr;
  1180. u8 *buf;
  1181. unsigned bufferspace, count, is_short;
  1182. volatile u32 *fifo = (volatile u32 *)ep->fifo;
  1183. DEBUG_EP0("%s\n", __FUNCTION__);
  1184. csr = usb_read(USB_EP0_CSR);
  1185. if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
  1186. return 0;
  1187. buf = req->req.buf + req->req.actual;
  1188. prefetchw(buf);
  1189. bufferspace = req->req.length - req->req.actual;
  1190. /* read all bytes from this packet */
  1191. if (likely(csr & EP0_OUT_PKT_RDY)) {
  1192. count = usb_read(USB_OUT_FIFO_WC1);
  1193. req->req.actual += min(count, bufferspace);
  1194. } else /* zlp */
  1195. count = 0;
  1196. is_short = (count < ep->ep.maxpacket);
  1197. DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
  1198. ep->ep.name, csr, count,
  1199. is_short ? "/S" : "", req, req->req.actual, req->req.length);
  1200. while (likely(count-- != 0)) {
  1201. u8 byte = (u8) (*fifo & 0xff);
  1202. if (unlikely(bufferspace == 0)) {
  1203. /* this happens when the driver's buffer
  1204. * is smaller than what the host sent.
  1205. * discard the extra data.
  1206. */
  1207. if (req->req.status != -EOVERFLOW)
  1208. DEBUG_EP0("%s overflow %d\n", ep->ep.name,
  1209. count);
  1210. req->req.status = -EOVERFLOW;
  1211. } else {
  1212. *buf++ = byte;
  1213. bufferspace--;
  1214. }
  1215. }
  1216. /* completion */
  1217. if (is_short || req->req.actual == req->req.length) {
  1218. done(ep, req, 0);
  1219. return 1;
  1220. }
  1221. /* finished that packet. the next one may be waiting... */
  1222. return 0;
  1223. }
  1224. /**
  1225. * udc_set_address - set the USB address for this device
  1226. * @address:
  1227. *
  1228. * Called from control endpoint function after it decodes a set address setup packet.
  1229. */
  1230. static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
  1231. {
  1232. DEBUG_EP0("%s: %d\n", __FUNCTION__, address);
  1233. /* c.f. 15.1.2.2 Table 15-4 address will be used after DATA_END is set */
  1234. dev->usb_address = address;
  1235. usb_set((address & USB_FA_FUNCTION_ADDR), USB_FA);
  1236. usb_set(USB_FA_ADDR_UPDATE | (address & USB_FA_FUNCTION_ADDR), USB_FA);
  1237. /* usb_read(USB_FA); */
  1238. }
  1239. /*
  1240. * DATA_STATE_RECV (OUT_PKT_RDY)
  1241. * - if error
  1242. * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
  1243. * - else
  1244. * set EP0_CLR_OUT bit
  1245. if last set EP0_DATA_END bit
  1246. */
  1247. static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
  1248. {
  1249. struct lh7a40x_request *req;
  1250. struct lh7a40x_ep *ep = &dev->ep[0];
  1251. int ret;
  1252. DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
  1253. if (list_empty(&ep->queue))
  1254. req = 0;
  1255. else
  1256. req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
  1257. if (req) {
  1258. if (req->req.length == 0) {
  1259. DEBUG_EP0("ZERO LENGTH OUT!\n");
  1260. usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
  1261. dev->ep0state = WAIT_FOR_SETUP;
  1262. return;
  1263. }
  1264. ret = read_fifo_ep0(ep, req);
  1265. if (ret) {
  1266. /* Done! */
  1267. DEBUG_EP0("%s: finished, waiting for status\n",
  1268. __FUNCTION__);
  1269. usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
  1270. dev->ep0state = WAIT_FOR_SETUP;
  1271. } else {
  1272. /* Not done yet.. */
  1273. DEBUG_EP0("%s: not finished\n", __FUNCTION__);
  1274. usb_set(EP0_CLR_OUT, USB_EP0_CSR);
  1275. }
  1276. } else {
  1277. DEBUG_EP0("NO REQ??!\n");
  1278. }
  1279. }
  1280. /*
  1281. * DATA_STATE_XMIT
  1282. */
  1283. static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
  1284. {
  1285. struct lh7a40x_request *req;
  1286. struct lh7a40x_ep *ep = &dev->ep[0];
  1287. int ret, need_zlp = 0;
  1288. DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
  1289. if (list_empty(&ep->queue))
  1290. req = 0;
  1291. else
  1292. req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
  1293. if (!req) {
  1294. DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
  1295. return 0;
  1296. }
  1297. if (req->req.length == 0) {
  1298. usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
  1299. dev->ep0state = WAIT_FOR_SETUP;
  1300. return 1;
  1301. }
  1302. if (req->req.length - req->req.actual == EP0_PACKETSIZE) {
  1303. /* Next write will end with the packet size, */
  1304. /* so we need Zero-length-packet */
  1305. need_zlp = 1;
  1306. }
  1307. ret = write_fifo_ep0(ep, req);
  1308. if (ret == 1 && !need_zlp) {
  1309. /* Last packet */
  1310. DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
  1311. usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
  1312. dev->ep0state = WAIT_FOR_SETUP;
  1313. } else {
  1314. DEBUG_EP0("%s: not finished\n", __FUNCTION__);
  1315. usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
  1316. }
  1317. if (need_zlp) {
  1318. DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__);
  1319. usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
  1320. dev->ep0state = DATA_STATE_NEED_ZLP;
  1321. }
  1322. return 1;
  1323. }
  1324. static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
  1325. struct usb_ctrlrequest *ctrl)
  1326. {
  1327. struct lh7a40x_ep *ep0 = &dev->ep[0];
  1328. struct lh7a40x_ep *qep;
  1329. int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
  1330. u16 val = 0;
  1331. if (reqtype == USB_RECIP_INTERFACE) {
  1332. /* This is not supported.
  1333. * And according to the USB spec, this one does nothing..
  1334. * Just return 0
  1335. */
  1336. DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
  1337. } else if (reqtype == USB_RECIP_DEVICE) {
  1338. DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
  1339. val |= (1 << 0); /* Self powered */
  1340. /*val |= (1<<1); *//* Remote wakeup */
  1341. } else if (reqtype == USB_RECIP_ENDPOINT) {
  1342. int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
  1343. DEBUG_SETUP
  1344. ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
  1345. ep_num, ctrl->wLength);
  1346. if (ctrl->wLength > 2 || ep_num > 3)
  1347. return -EOPNOTSUPP;
  1348. qep = &dev->ep[ep_num];
  1349. if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
  1350. && ep_index(qep) != 0) {
  1351. return -EOPNOTSUPP;
  1352. }
  1353. usb_set_index(ep_index(qep));
  1354. /* Return status on next IN token */
  1355. switch (qep->ep_type) {
  1356. case ep_control:
  1357. val =
  1358. (usb_read(qep->csr1) & EP0_SEND_STALL) ==
  1359. EP0_SEND_STALL;
  1360. break;
  1361. case ep_bulk_in:
  1362. case ep_interrupt:
  1363. val =
  1364. (usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
  1365. USB_IN_CSR1_SEND_STALL;
  1366. break;
  1367. case ep_bulk_out:
  1368. val =
  1369. (usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
  1370. USB_OUT_CSR1_SEND_STALL;
  1371. break;
  1372. }
  1373. /* Back to EP0 index */
  1374. usb_set_index(0);
  1375. DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
  1376. ctrl->wIndex, val);
  1377. } else {
  1378. DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
  1379. return -EOPNOTSUPP;
  1380. }
  1381. /* Clear "out packet ready" */
  1382. usb_set((EP0_CLR_OUT), USB_EP0_CSR);
  1383. /* Put status to FIFO */
  1384. lh7a40x_fifo_write(ep0, (u8 *) & val, sizeof(val));
  1385. /* Issue "In packet ready" */
  1386. usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
  1387. return 0;
  1388. }
  1389. /*
  1390. * WAIT_FOR_SETUP (OUT_PKT_RDY)
  1391. * - read data packet from EP0 FIFO
  1392. * - decode command
  1393. * - if error
  1394. * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
  1395. * - else
  1396. * set EP0_CLR_OUT | EP0_DATA_END bits
  1397. */
  1398. static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
  1399. {
  1400. struct lh7a40x_ep *ep = &dev->ep[0];
  1401. struct usb_ctrlrequest ctrl;
  1402. int i, bytes, is_in;
  1403. DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
  1404. /* Nuke all previous transfers */
  1405. nuke(ep, -EPROTO);
  1406. /* read control req from fifo (8 bytes) */
  1407. bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
  1408. DEBUG_SETUP("Read CTRL REQ %d bytes\n", bytes);
  1409. DEBUG_SETUP("CTRL.bRequestType = %d (is_in %d)\n", ctrl.bRequestType,
  1410. ctrl.bRequestType == USB_DIR_IN);
  1411. DEBUG_SETUP("CTRL.bRequest = %d\n", ctrl.bRequest);
  1412. DEBUG_SETUP("CTRL.wLength = %d\n", ctrl.wLength);
  1413. DEBUG_SETUP("CTRL.wValue = %d (%d)\n", ctrl.wValue, ctrl.wValue >> 8);
  1414. DEBUG_SETUP("CTRL.wIndex = %d\n", ctrl.wIndex);
  1415. /* Set direction of EP0 */
  1416. if (likely(ctrl.bRequestType & USB_DIR_IN)) {
  1417. ep->bEndpointAddress |= USB_DIR_IN;
  1418. is_in = 1;
  1419. } else {
  1420. ep->bEndpointAddress &= ~USB_DIR_IN;
  1421. is_in = 0;
  1422. }
  1423. dev->req_pending = 1;
  1424. /* Handle some SETUP packets ourselves */
  1425. switch (ctrl.bRequest) {
  1426. case USB_REQ_SET_ADDRESS:
  1427. if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
  1428. break;
  1429. DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
  1430. udc_set_address(dev, ctrl.wValue);
  1431. usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
  1432. return;
  1433. case USB_REQ_GET_STATUS:{
  1434. if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
  1435. return;
  1436. case USB_REQ_CLEAR_FEATURE:
  1437. case USB_REQ_SET_FEATURE:
  1438. if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
  1439. struct lh7a40x_ep *qep;
  1440. int ep_num = (ctrl.wIndex & 0x0f);
  1441. /* Support only HALT feature */
  1442. if (ctrl.wValue != 0 || ctrl.wLength != 0
  1443. || ep_num > 3 || ep_num < 1)
  1444. break;
  1445. qep = &dev->ep[ep_num];
  1446. if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
  1447. DEBUG_SETUP("SET_FEATURE (%d)\n",
  1448. ep_num);
  1449. lh7a40x_set_halt(&qep->ep, 1);
  1450. } else {
  1451. DEBUG_SETUP("CLR_FEATURE (%d)\n",
  1452. ep_num);
  1453. lh7a40x_set_halt(&qep->ep, 0);
  1454. }
  1455. usb_set_index(0);
  1456. /* Reply with a ZLP on next IN token */
  1457. usb_set((EP0_CLR_OUT | EP0_DATA_END),
  1458. USB_EP0_CSR);
  1459. return;
  1460. }
  1461. break;
  1462. }
  1463. default:
  1464. break;
  1465. }
  1466. if (likely(dev->driver)) {
  1467. /* device-2-host (IN) or no data setup command, process immediately */
  1468. spin_unlock(&dev->lock);
  1469. i = dev->driver->setup(&dev->gadget, &ctrl);
  1470. spin_lock(&dev->lock);
  1471. if (i < 0) {
  1472. /* setup processing failed, force stall */
  1473. DEBUG_SETUP
  1474. (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
  1475. i);
  1476. usb_set_index(0);
  1477. usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
  1478. USB_EP0_CSR);
  1479. /* ep->stopped = 1; */
  1480. dev->ep0state = WAIT_FOR_SETUP;
  1481. }
  1482. }
  1483. }
  1484. /*
  1485. * DATA_STATE_NEED_ZLP
  1486. */
  1487. static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
  1488. {
  1489. DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
  1490. /* c.f. Table 15-14 */
  1491. usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
  1492. dev->ep0state = WAIT_FOR_SETUP;
  1493. }
  1494. /*
  1495. * handle ep0 interrupt
  1496. */
  1497. static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
  1498. {
  1499. struct lh7a40x_ep *ep = &dev->ep[0];
  1500. u32 csr;
  1501. /* Set index 0 */
  1502. usb_set_index(0);
  1503. csr = usb_read(USB_EP0_CSR);
  1504. DEBUG_EP0("%s: csr = %x\n", __FUNCTION__, csr);
  1505. /*
  1506. * For overview of what we should be doing see c.f. Chapter 18.1.2.4
  1507. * We will follow that outline here modified by our own global state
  1508. * indication which provides hints as to what we think should be
  1509. * happening..
  1510. */
  1511. /*
  1512. * if SENT_STALL is set
  1513. * - clear the SENT_STALL bit
  1514. */
  1515. if (csr & EP0_SENT_STALL) {
  1516. DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __FUNCTION__, csr);
  1517. usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
  1518. nuke(ep, -ECONNABORTED);
  1519. dev->ep0state = WAIT_FOR_SETUP;
  1520. return;
  1521. }
  1522. /*
  1523. * if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
  1524. * - fill EP0 FIFO
  1525. * - if last packet
  1526. * - set IN_PKT_RDY | DATA_END
  1527. * - else
  1528. * set IN_PKT_RDY
  1529. */
  1530. if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
  1531. DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
  1532. __FUNCTION__);
  1533. switch (dev->ep0state) {
  1534. case DATA_STATE_XMIT:
  1535. DEBUG_EP0("continue with DATA_STATE_XMIT\n");
  1536. lh7a40x_ep0_in(dev, csr);
  1537. return;
  1538. case DATA_STATE_NEED_ZLP:
  1539. DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
  1540. lh7a40x_ep0_in_zlp(dev, csr);
  1541. return;
  1542. default:
  1543. /* Stall? */
  1544. DEBUG_EP0("Odd state!! state = %s\n",
  1545. state_names[dev->ep0state]);
  1546. dev->ep0state = WAIT_FOR_SETUP;
  1547. /* nuke(ep, 0); */
  1548. /* usb_set(EP0_SEND_STALL, ep->csr1); */
  1549. break;
  1550. }
  1551. }
  1552. /*
  1553. * if SETUP_END is set
  1554. * - abort the last transfer
  1555. * - set SERVICED_SETUP_END_BIT
  1556. */
  1557. if (csr & EP0_SETUP_END) {
  1558. DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __FUNCTION__, csr);
  1559. usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
  1560. nuke(ep, 0);
  1561. dev->ep0state = WAIT_FOR_SETUP;
  1562. }
  1563. /*
  1564. * if EP0_OUT_PKT_RDY is set
  1565. * - read data packet from EP0 FIFO
  1566. * - decode command
  1567. * - if error
  1568. * set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
  1569. * - else
  1570. * set SERVICED_OUT_PKT_RDY | DATA_END bits
  1571. */
  1572. if (csr & EP0_OUT_PKT_RDY) {
  1573. DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
  1574. csr);
  1575. switch (dev->ep0state) {
  1576. case WAIT_FOR_SETUP:
  1577. DEBUG_EP0("WAIT_FOR_SETUP\n");
  1578. lh7a40x_ep0_setup(dev, csr);
  1579. break;
  1580. case DATA_STATE_RECV:
  1581. DEBUG_EP0("DATA_STATE_RECV\n");
  1582. lh7a40x_ep0_out(dev, csr);
  1583. break;
  1584. default:
  1585. /* send stall? */
  1586. DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
  1587. dev->ep0state);
  1588. break;
  1589. }
  1590. }
  1591. }
  1592. static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
  1593. {
  1594. u32 csr;
  1595. usb_set_index(0);
  1596. csr = usb_read(USB_EP0_CSR);
  1597. DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
  1598. /* Clear "out packet ready" */
  1599. usb_set(EP0_CLR_OUT, USB_EP0_CSR);
  1600. if (ep_is_in(ep)) {
  1601. dev->ep0state = DATA_STATE_XMIT;
  1602. lh7a40x_ep0_in(dev, csr);
  1603. } else {
  1604. dev->ep0state = DATA_STATE_RECV;
  1605. lh7a40x_ep0_out(dev, csr);
  1606. }
  1607. }
  1608. /* ---------------------------------------------------------------------------
  1609. * device-scoped parts of the api to the usb controller hardware
  1610. * ---------------------------------------------------------------------------
  1611. */
  1612. static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
  1613. {
  1614. u32 frame1 = usb_read(USB_FRM_NUM1); /* Least significant 8 bits */
  1615. u32 frame2 = usb_read(USB_FRM_NUM2); /* Most significant 3 bits */
  1616. DEBUG("%s, %p\n", __FUNCTION__, _gadget);
  1617. return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
  1618. }
  1619. static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
  1620. {
  1621. /* host may not have enabled remote wakeup */
  1622. /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
  1623. return -EHOSTUNREACH;
  1624. udc_set_mask_UDCCR(UDCCR_RSM); */
  1625. return -ENOTSUPP;
  1626. }
  1627. static const struct usb_gadget_ops lh7a40x_udc_ops = {
  1628. .get_frame = lh7a40x_udc_get_frame,
  1629. .wakeup = lh7a40x_udc_wakeup,
  1630. /* current versions must always be self-powered */
  1631. };
  1632. static void nop_release(struct device *dev)
  1633. {
  1634. DEBUG("%s %s\n", __FUNCTION__, dev->bus_id);
  1635. }
  1636. static struct lh7a40x_udc memory = {
  1637. .usb_address = 0,
  1638. .gadget = {
  1639. .ops = &lh7a40x_udc_ops,
  1640. .ep0 = &memory.ep[0].ep,
  1641. .name = driver_name,
  1642. .dev = {
  1643. .bus_id = "gadget",
  1644. .release = nop_release,
  1645. },
  1646. },
  1647. /* control endpoint */
  1648. .ep[0] = {
  1649. .ep = {
  1650. .name = ep0name,
  1651. .ops = &lh7a40x_ep_ops,
  1652. .maxpacket = EP0_PACKETSIZE,
  1653. },
  1654. .dev = &memory,
  1655. .bEndpointAddress = 0,
  1656. .bmAttributes = 0,
  1657. .ep_type = ep_control,
  1658. .fifo = io_p2v(USB_EP0_FIFO),
  1659. .csr1 = USB_EP0_CSR,
  1660. .csr2 = USB_EP0_CSR,
  1661. },
  1662. /* first group of endpoints */
  1663. .ep[1] = {
  1664. .ep = {
  1665. .name = "ep1in-bulk",
  1666. .ops = &lh7a40x_ep_ops,
  1667. .maxpacket = 64,
  1668. },
  1669. .dev = &memory,
  1670. .bEndpointAddress = USB_DIR_IN | 1,
  1671. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1672. .ep_type = ep_bulk_in,
  1673. .fifo = io_p2v(USB_EP1_FIFO),
  1674. .csr1 = USB_IN_CSR1,
  1675. .csr2 = USB_IN_CSR2,
  1676. },
  1677. .ep[2] = {
  1678. .ep = {
  1679. .name = "ep2out-bulk",
  1680. .ops = &lh7a40x_ep_ops,
  1681. .maxpacket = 64,
  1682. },
  1683. .dev = &memory,
  1684. .bEndpointAddress = 2,
  1685. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1686. .ep_type = ep_bulk_out,
  1687. .fifo = io_p2v(USB_EP2_FIFO),
  1688. .csr1 = USB_OUT_CSR1,
  1689. .csr2 = USB_OUT_CSR2,
  1690. },
  1691. .ep[3] = {
  1692. .ep = {
  1693. .name = "ep3in-int",
  1694. .ops = &lh7a40x_ep_ops,
  1695. .maxpacket = 64,
  1696. },
  1697. .dev = &memory,
  1698. .bEndpointAddress = USB_DIR_IN | 3,
  1699. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1700. .ep_type = ep_interrupt,
  1701. .fifo = io_p2v(USB_EP3_FIFO),
  1702. .csr1 = USB_IN_CSR1,
  1703. .csr2 = USB_IN_CSR2,
  1704. },
  1705. };
  1706. /*
  1707. * probe - binds to the platform device
  1708. */
  1709. static int lh7a40x_udc_probe(struct device *_dev)
  1710. {
  1711. struct lh7a40x_udc *dev = &memory;
  1712. int retval;
  1713. DEBUG("%s: %p\n", __FUNCTION__, _dev);
  1714. spin_lock_init(&dev->lock);
  1715. dev->dev = _dev;
  1716. device_initialize(&dev->gadget.dev);
  1717. dev->gadget.dev.parent = _dev;
  1718. the_controller = dev;
  1719. dev_set_drvdata(_dev, dev);
  1720. udc_disable(dev);
  1721. udc_reinit(dev);
  1722. /* irq setup after old hardware state is cleaned up */
  1723. retval =
  1724. request_irq(IRQ_USBINTR, lh7a40x_udc_irq, SA_INTERRUPT, driver_name,
  1725. dev);
  1726. if (retval != 0) {
  1727. DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
  1728. IRQ_USBINTR, retval);
  1729. return -EBUSY;
  1730. }
  1731. create_proc_files();
  1732. return retval;
  1733. }
  1734. static int lh7a40x_udc_remove(struct device *_dev)
  1735. {
  1736. struct lh7a40x_udc *dev = _dev->driver_data;
  1737. DEBUG("%s: %p\n", __FUNCTION__, dev);
  1738. udc_disable(dev);
  1739. remove_proc_files();
  1740. usb_gadget_unregister_driver(dev->driver);
  1741. free_irq(IRQ_USBINTR, dev);
  1742. dev_set_drvdata(_dev, 0);
  1743. the_controller = 0;
  1744. return 0;
  1745. }
  1746. /*-------------------------------------------------------------------------*/
  1747. static struct device_driver udc_driver = {
  1748. .name = (char *)driver_name,
  1749. .bus = &platform_bus_type,
  1750. .probe = lh7a40x_udc_probe,
  1751. .remove = lh7a40x_udc_remove
  1752. /* FIXME power management support */
  1753. /* .suspend = ... disable UDC */
  1754. /* .resume = ... re-enable UDC */
  1755. };
  1756. static int __init udc_init(void)
  1757. {
  1758. DEBUG("%s: %s version %s\n", __FUNCTION__, driver_name, DRIVER_VERSION);
  1759. return driver_register(&udc_driver);
  1760. }
  1761. static void __exit udc_exit(void)
  1762. {
  1763. driver_unregister(&udc_driver);
  1764. }
  1765. module_init(udc_init);
  1766. module_exit(udc_exit);
  1767. MODULE_DESCRIPTION(DRIVER_DESC);
  1768. MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
  1769. MODULE_LICENSE("GPL");