lh7a40x_udc.c 50 KB

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