s3c2410_udc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. /*
  2. * linux/drivers/usb/gadget/s3c2410_udc.c
  3. *
  4. * Samsung S3C24xx series on-chip full speed USB device controllers
  5. *
  6. * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
  7. * Additional cleanups by Ben Dooks <ben-linux@fluff.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/timer.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/clk.h>
  37. #include <linux/gpio.h>
  38. #include <linux/prefetch.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/seq_file.h>
  41. #include <linux/usb.h>
  42. #include <linux/usb/gadget.h>
  43. #include <asm/byteorder.h>
  44. #include <asm/io.h>
  45. #include <asm/irq.h>
  46. #include <asm/system.h>
  47. #include <asm/unaligned.h>
  48. #include <mach/irqs.h>
  49. #include <mach/hardware.h>
  50. #include <plat/regs-udc.h>
  51. #include <plat/udc.h>
  52. #include "s3c2410_udc.h"
  53. #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
  54. #define DRIVER_VERSION "29 Apr 2007"
  55. #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
  56. "Arnaud Patard <arnaud.patard@rtp-net.org>"
  57. static const char gadget_name[] = "s3c2410_udc";
  58. static const char driver_desc[] = DRIVER_DESC;
  59. static struct s3c2410_udc *the_controller;
  60. static struct clk *udc_clock;
  61. static struct clk *usb_bus_clock;
  62. static void __iomem *base_addr;
  63. static u64 rsrc_start;
  64. static u64 rsrc_len;
  65. static struct dentry *s3c2410_udc_debugfs_root;
  66. static inline u32 udc_read(u32 reg)
  67. {
  68. return readb(base_addr + reg);
  69. }
  70. static inline void udc_write(u32 value, u32 reg)
  71. {
  72. writeb(value, base_addr + reg);
  73. }
  74. static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
  75. {
  76. writeb(value, base + reg);
  77. }
  78. static struct s3c2410_udc_mach_info *udc_info;
  79. /*************************** DEBUG FUNCTION ***************************/
  80. #define DEBUG_NORMAL 1
  81. #define DEBUG_VERBOSE 2
  82. #ifdef CONFIG_USB_S3C2410_DEBUG
  83. #define USB_S3C2410_DEBUG_LEVEL 0
  84. static uint32_t s3c2410_ticks = 0;
  85. static int dprintk(int level, const char *fmt, ...)
  86. {
  87. static char printk_buf[1024];
  88. static long prevticks;
  89. static int invocation;
  90. va_list args;
  91. int len;
  92. if (level > USB_S3C2410_DEBUG_LEVEL)
  93. return 0;
  94. if (s3c2410_ticks != prevticks) {
  95. prevticks = s3c2410_ticks;
  96. invocation = 0;
  97. }
  98. len = scnprintf(printk_buf,
  99. sizeof(printk_buf), "%1lu.%02d USB: ",
  100. prevticks, invocation++);
  101. va_start(args, fmt);
  102. len = vscnprintf(printk_buf+len,
  103. sizeof(printk_buf)-len, fmt, args);
  104. va_end(args);
  105. return printk(KERN_DEBUG "%s", printk_buf);
  106. }
  107. #else
  108. static int dprintk(int level, const char *fmt, ...)
  109. {
  110. return 0;
  111. }
  112. #endif
  113. static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
  114. {
  115. u32 addr_reg,pwr_reg,ep_int_reg,usb_int_reg;
  116. u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
  117. u32 ep1_i_csr1,ep1_i_csr2,ep1_o_csr1,ep1_o_csr2;
  118. u32 ep2_i_csr1,ep2_i_csr2,ep2_o_csr1,ep2_o_csr2;
  119. addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
  120. pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
  121. ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
  122. usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
  123. ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
  124. usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
  125. udc_write(0, S3C2410_UDC_INDEX_REG);
  126. ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
  127. udc_write(1, S3C2410_UDC_INDEX_REG);
  128. ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
  129. ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
  130. ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
  131. ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
  132. udc_write(2, S3C2410_UDC_INDEX_REG);
  133. ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
  134. ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
  135. ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
  136. ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
  137. seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
  138. "PWR_REG : 0x%04X\n"
  139. "EP_INT_REG : 0x%04X\n"
  140. "USB_INT_REG : 0x%04X\n"
  141. "EP_INT_EN_REG : 0x%04X\n"
  142. "USB_INT_EN_REG : 0x%04X\n"
  143. "EP0_CSR : 0x%04X\n"
  144. "EP1_I_CSR1 : 0x%04X\n"
  145. "EP1_I_CSR2 : 0x%04X\n"
  146. "EP1_O_CSR1 : 0x%04X\n"
  147. "EP1_O_CSR2 : 0x%04X\n"
  148. "EP2_I_CSR1 : 0x%04X\n"
  149. "EP2_I_CSR2 : 0x%04X\n"
  150. "EP2_O_CSR1 : 0x%04X\n"
  151. "EP2_O_CSR2 : 0x%04X\n",
  152. addr_reg,pwr_reg,ep_int_reg,usb_int_reg,
  153. ep_int_en_reg, usb_int_en_reg, ep0_csr,
  154. ep1_i_csr1,ep1_i_csr2,ep1_o_csr1,ep1_o_csr2,
  155. ep2_i_csr1,ep2_i_csr2,ep2_o_csr1,ep2_o_csr2
  156. );
  157. return 0;
  158. }
  159. static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
  160. struct file *file)
  161. {
  162. return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
  163. }
  164. static const struct file_operations s3c2410_udc_debugfs_fops = {
  165. .open = s3c2410_udc_debugfs_fops_open,
  166. .read = seq_read,
  167. .llseek = seq_lseek,
  168. .release = single_release,
  169. .owner = THIS_MODULE,
  170. };
  171. /* io macros */
  172. static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
  173. {
  174. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  175. udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
  176. S3C2410_UDC_EP0_CSR_REG);
  177. }
  178. static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
  179. {
  180. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  181. writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
  182. }
  183. static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
  184. {
  185. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  186. udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
  187. }
  188. static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
  189. {
  190. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  191. udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
  192. }
  193. static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
  194. {
  195. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  196. udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
  197. }
  198. inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
  199. {
  200. udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  201. udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
  202. }
  203. static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
  204. {
  205. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  206. udc_writeb(base,(S3C2410_UDC_EP0_CSR_SOPKTRDY
  207. | S3C2410_UDC_EP0_CSR_DE),
  208. S3C2410_UDC_EP0_CSR_REG);
  209. }
  210. static inline void s3c2410_udc_set_ep0_sse_out(void __iomem *base)
  211. {
  212. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  213. udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
  214. | S3C2410_UDC_EP0_CSR_SSE),
  215. S3C2410_UDC_EP0_CSR_REG);
  216. }
  217. static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
  218. {
  219. udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  220. udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
  221. | S3C2410_UDC_EP0_CSR_DE),
  222. S3C2410_UDC_EP0_CSR_REG);
  223. }
  224. /*------------------------- I/O ----------------------------------*/
  225. /*
  226. * s3c2410_udc_done
  227. */
  228. static void s3c2410_udc_done(struct s3c2410_ep *ep,
  229. struct s3c2410_request *req, int status)
  230. {
  231. unsigned halted = ep->halted;
  232. list_del_init(&req->queue);
  233. if (likely (req->req.status == -EINPROGRESS))
  234. req->req.status = status;
  235. else
  236. status = req->req.status;
  237. ep->halted = 1;
  238. req->req.complete(&ep->ep, &req->req);
  239. ep->halted = halted;
  240. }
  241. static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
  242. struct s3c2410_ep *ep, int status)
  243. {
  244. /* Sanity check */
  245. if (&ep->queue == NULL)
  246. return;
  247. while (!list_empty (&ep->queue)) {
  248. struct s3c2410_request *req;
  249. req = list_entry (ep->queue.next, struct s3c2410_request,
  250. queue);
  251. s3c2410_udc_done(ep, req, status);
  252. }
  253. }
  254. static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc *dev)
  255. {
  256. unsigned i;
  257. /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
  258. * fifos, and pending transactions mustn't be continued in any case.
  259. */
  260. for (i = 1; i < S3C2410_ENDPOINTS; i++)
  261. s3c2410_udc_nuke(dev, &dev->ep[i], -ECONNABORTED);
  262. }
  263. static inline int s3c2410_udc_fifo_count_out(void)
  264. {
  265. int tmp;
  266. tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
  267. tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
  268. return tmp;
  269. }
  270. /*
  271. * s3c2410_udc_write_packet
  272. */
  273. static inline int s3c2410_udc_write_packet(int fifo,
  274. struct s3c2410_request *req,
  275. unsigned max)
  276. {
  277. unsigned len = min(req->req.length - req->req.actual, max);
  278. u8 *buf = req->req.buf + req->req.actual;
  279. prefetch(buf);
  280. dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
  281. req->req.actual, req->req.length, len, req->req.actual + len);
  282. req->req.actual += len;
  283. udelay(5);
  284. writesb(base_addr + fifo, buf, len);
  285. return len;
  286. }
  287. /*
  288. * s3c2410_udc_write_fifo
  289. *
  290. * return: 0 = still running, 1 = completed, negative = errno
  291. */
  292. static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
  293. struct s3c2410_request *req)
  294. {
  295. unsigned count;
  296. int is_last;
  297. u32 idx;
  298. int fifo_reg;
  299. u32 ep_csr;
  300. idx = ep->bEndpointAddress & 0x7F;
  301. switch (idx) {
  302. default:
  303. idx = 0;
  304. case 0:
  305. fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
  306. break;
  307. case 1:
  308. fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
  309. break;
  310. case 2:
  311. fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
  312. break;
  313. case 3:
  314. fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
  315. break;
  316. case 4:
  317. fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
  318. break;
  319. }
  320. count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
  321. /* last packet is often short (sometimes a zlp) */
  322. if (count != ep->ep.maxpacket)
  323. is_last = 1;
  324. else if (req->req.length != req->req.actual || req->req.zero)
  325. is_last = 0;
  326. else
  327. is_last = 2;
  328. /* Only ep0 debug messages are interesting */
  329. if (idx == 0)
  330. dprintk(DEBUG_NORMAL,
  331. "Written ep%d %d.%d of %d b [last %d,z %d]\n",
  332. idx, count, req->req.actual, req->req.length,
  333. is_last, req->req.zero);
  334. if (is_last) {
  335. /* The order is important. It prevents sending 2 packets
  336. * at the same time */
  337. if (idx == 0) {
  338. /* Reset signal => no need to say 'data sent' */
  339. if (! (udc_read(S3C2410_UDC_USB_INT_REG)
  340. & S3C2410_UDC_USBINT_RESET))
  341. s3c2410_udc_set_ep0_de_in(base_addr);
  342. ep->dev->ep0state=EP0_IDLE;
  343. } else {
  344. udc_write(idx, S3C2410_UDC_INDEX_REG);
  345. ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
  346. udc_write(idx, S3C2410_UDC_INDEX_REG);
  347. udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
  348. S3C2410_UDC_IN_CSR1_REG);
  349. }
  350. s3c2410_udc_done(ep, req, 0);
  351. is_last = 1;
  352. } else {
  353. if (idx == 0) {
  354. /* Reset signal => no need to say 'data sent' */
  355. if (! (udc_read(S3C2410_UDC_USB_INT_REG)
  356. & S3C2410_UDC_USBINT_RESET))
  357. s3c2410_udc_set_ep0_ipr(base_addr);
  358. } else {
  359. udc_write(idx, S3C2410_UDC_INDEX_REG);
  360. ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
  361. udc_write(idx, S3C2410_UDC_INDEX_REG);
  362. udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
  363. S3C2410_UDC_IN_CSR1_REG);
  364. }
  365. }
  366. return is_last;
  367. }
  368. static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
  369. struct s3c2410_request *req, unsigned avail)
  370. {
  371. unsigned len;
  372. len = min(req->req.length - req->req.actual, avail);
  373. req->req.actual += len;
  374. readsb(fifo + base_addr, buf, len);
  375. return len;
  376. }
  377. /*
  378. * return: 0 = still running, 1 = queue empty, negative = errno
  379. */
  380. static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
  381. struct s3c2410_request *req)
  382. {
  383. u8 *buf;
  384. u32 ep_csr;
  385. unsigned bufferspace;
  386. int is_last=1;
  387. unsigned avail;
  388. int fifo_count = 0;
  389. u32 idx;
  390. int fifo_reg;
  391. idx = ep->bEndpointAddress & 0x7F;
  392. switch (idx) {
  393. default:
  394. idx = 0;
  395. case 0:
  396. fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
  397. break;
  398. case 1:
  399. fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
  400. break;
  401. case 2:
  402. fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
  403. break;
  404. case 3:
  405. fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
  406. break;
  407. case 4:
  408. fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
  409. break;
  410. }
  411. if (!req->req.length)
  412. return 1;
  413. buf = req->req.buf + req->req.actual;
  414. bufferspace = req->req.length - req->req.actual;
  415. if (!bufferspace) {
  416. dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
  417. return -1;
  418. }
  419. udc_write(idx, S3C2410_UDC_INDEX_REG);
  420. fifo_count = s3c2410_udc_fifo_count_out();
  421. dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
  422. if (fifo_count > ep->ep.maxpacket)
  423. avail = ep->ep.maxpacket;
  424. else
  425. avail = fifo_count;
  426. fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
  427. /* checking this with ep0 is not accurate as we already
  428. * read a control request
  429. **/
  430. if (idx != 0 && fifo_count < ep->ep.maxpacket) {
  431. is_last = 1;
  432. /* overflowed this request? flush extra data */
  433. if (fifo_count != avail)
  434. req->req.status = -EOVERFLOW;
  435. } else {
  436. is_last = (req->req.length <= req->req.actual) ? 1 : 0;
  437. }
  438. udc_write(idx, S3C2410_UDC_INDEX_REG);
  439. fifo_count = s3c2410_udc_fifo_count_out();
  440. /* Only ep0 debug messages are interesting */
  441. if (idx == 0)
  442. dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
  443. __func__, fifo_count,is_last);
  444. if (is_last) {
  445. if (idx == 0) {
  446. s3c2410_udc_set_ep0_de_out(base_addr);
  447. ep->dev->ep0state = EP0_IDLE;
  448. } else {
  449. udc_write(idx, S3C2410_UDC_INDEX_REG);
  450. ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
  451. udc_write(idx, S3C2410_UDC_INDEX_REG);
  452. udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
  453. S3C2410_UDC_OUT_CSR1_REG);
  454. }
  455. s3c2410_udc_done(ep, req, 0);
  456. } else {
  457. if (idx == 0) {
  458. s3c2410_udc_clear_ep0_opr(base_addr);
  459. } else {
  460. udc_write(idx, S3C2410_UDC_INDEX_REG);
  461. ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
  462. udc_write(idx, S3C2410_UDC_INDEX_REG);
  463. udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
  464. S3C2410_UDC_OUT_CSR1_REG);
  465. }
  466. }
  467. return is_last;
  468. }
  469. static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
  470. {
  471. unsigned char *outbuf = (unsigned char*)crq;
  472. int bytes_read = 0;
  473. udc_write(0, S3C2410_UDC_INDEX_REG);
  474. bytes_read = s3c2410_udc_fifo_count_out();
  475. dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
  476. if (bytes_read > sizeof(struct usb_ctrlrequest))
  477. bytes_read = sizeof(struct usb_ctrlrequest);
  478. readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
  479. dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
  480. bytes_read, crq->bRequest, crq->bRequestType,
  481. crq->wValue, crq->wIndex, crq->wLength);
  482. return bytes_read;
  483. }
  484. static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
  485. struct usb_ctrlrequest *crq)
  486. {
  487. u16 status = 0;
  488. u8 ep_num = crq->wIndex & 0x7F;
  489. u8 is_in = crq->wIndex & USB_DIR_IN;
  490. switch (crq->bRequestType & USB_RECIP_MASK) {
  491. case USB_RECIP_INTERFACE:
  492. break;
  493. case USB_RECIP_DEVICE:
  494. status = dev->devstatus;
  495. break;
  496. case USB_RECIP_ENDPOINT:
  497. if (ep_num > 4 || crq->wLength > 2)
  498. return 1;
  499. if (ep_num == 0) {
  500. udc_write(0, S3C2410_UDC_INDEX_REG);
  501. status = udc_read(S3C2410_UDC_IN_CSR1_REG);
  502. status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
  503. } else {
  504. udc_write(ep_num, S3C2410_UDC_INDEX_REG);
  505. if (is_in) {
  506. status = udc_read(S3C2410_UDC_IN_CSR1_REG);
  507. status = status & S3C2410_UDC_ICSR1_SENDSTL;
  508. } else {
  509. status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
  510. status = status & S3C2410_UDC_OCSR1_SENDSTL;
  511. }
  512. }
  513. status = status ? 1 : 0;
  514. break;
  515. default:
  516. return 1;
  517. }
  518. /* Seems to be needed to get it working. ouch :( */
  519. udelay(5);
  520. udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
  521. udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
  522. s3c2410_udc_set_ep0_de_in(base_addr);
  523. return 0;
  524. }
  525. /*------------------------- usb state machine -------------------------------*/
  526. static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
  527. static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
  528. struct s3c2410_ep *ep,
  529. struct usb_ctrlrequest *crq,
  530. u32 ep0csr)
  531. {
  532. int len, ret, tmp;
  533. /* start control request? */
  534. if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
  535. return;
  536. s3c2410_udc_nuke(dev, ep, -EPROTO);
  537. len = s3c2410_udc_read_fifo_crq(crq);
  538. if (len != sizeof(*crq)) {
  539. dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
  540. " wanted %d bytes got %d. Stalling out...\n",
  541. sizeof(*crq), len);
  542. s3c2410_udc_set_ep0_ss(base_addr);
  543. return;
  544. }
  545. dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
  546. crq->bRequest, crq->bRequestType, crq->wLength);
  547. /* cope with automagic for some standard requests. */
  548. dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
  549. == USB_TYPE_STANDARD;
  550. dev->req_config = 0;
  551. dev->req_pending = 1;
  552. switch (crq->bRequest) {
  553. case USB_REQ_SET_CONFIGURATION:
  554. dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ... \n");
  555. if (crq->bRequestType == USB_RECIP_DEVICE) {
  556. dev->req_config = 1;
  557. s3c2410_udc_set_ep0_de_out(base_addr);
  558. }
  559. break;
  560. case USB_REQ_SET_INTERFACE:
  561. dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ... \n");
  562. if (crq->bRequestType == USB_RECIP_INTERFACE) {
  563. dev->req_config = 1;
  564. s3c2410_udc_set_ep0_de_out(base_addr);
  565. }
  566. break;
  567. case USB_REQ_SET_ADDRESS:
  568. dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ... \n");
  569. if (crq->bRequestType == USB_RECIP_DEVICE) {
  570. tmp = crq->wValue & 0x7F;
  571. dev->address = tmp;
  572. udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
  573. S3C2410_UDC_FUNC_ADDR_REG);
  574. s3c2410_udc_set_ep0_de_out(base_addr);
  575. return;
  576. }
  577. break;
  578. case USB_REQ_GET_STATUS:
  579. dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ... \n");
  580. s3c2410_udc_clear_ep0_opr(base_addr);
  581. if (dev->req_std) {
  582. if (!s3c2410_udc_get_status(dev, crq)) {
  583. return;
  584. }
  585. }
  586. break;
  587. case USB_REQ_CLEAR_FEATURE:
  588. s3c2410_udc_clear_ep0_opr(base_addr);
  589. if (crq->bRequestType != USB_RECIP_ENDPOINT)
  590. break;
  591. if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
  592. break;
  593. s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
  594. s3c2410_udc_set_ep0_de_out(base_addr);
  595. return;
  596. case USB_REQ_SET_FEATURE:
  597. s3c2410_udc_clear_ep0_opr(base_addr);
  598. if (crq->bRequestType != USB_RECIP_ENDPOINT)
  599. break;
  600. if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
  601. break;
  602. s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
  603. s3c2410_udc_set_ep0_de_out(base_addr);
  604. return;
  605. default:
  606. s3c2410_udc_clear_ep0_opr(base_addr);
  607. break;
  608. }
  609. if (crq->bRequestType & USB_DIR_IN)
  610. dev->ep0state = EP0_IN_DATA_PHASE;
  611. else
  612. dev->ep0state = EP0_OUT_DATA_PHASE;
  613. if (!dev->driver)
  614. return;
  615. /* deliver the request to the gadget driver */
  616. ret = dev->driver->setup(&dev->gadget, crq);
  617. if (ret < 0) {
  618. if (dev->req_config) {
  619. dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
  620. crq->bRequest, ret);
  621. return;
  622. }
  623. if (ret == -EOPNOTSUPP)
  624. dprintk(DEBUG_NORMAL, "Operation not supported\n");
  625. else
  626. dprintk(DEBUG_NORMAL,
  627. "dev->driver->setup failed. (%d)\n", ret);
  628. udelay(5);
  629. s3c2410_udc_set_ep0_ss(base_addr);
  630. s3c2410_udc_set_ep0_de_out(base_addr);
  631. dev->ep0state = EP0_IDLE;
  632. /* deferred i/o == no response yet */
  633. } else if (dev->req_pending) {
  634. dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
  635. dev->req_pending=0;
  636. }
  637. dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
  638. }
  639. static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
  640. {
  641. u32 ep0csr;
  642. struct s3c2410_ep *ep = &dev->ep[0];
  643. struct s3c2410_request *req;
  644. struct usb_ctrlrequest crq;
  645. if (list_empty(&ep->queue))
  646. req = NULL;
  647. else
  648. req = list_entry(ep->queue.next, struct s3c2410_request, queue);
  649. /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
  650. * S3C2410_UDC_EP0_CSR_REG when index is zero */
  651. udc_write(0, S3C2410_UDC_INDEX_REG);
  652. ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
  653. dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
  654. ep0csr, ep0states[dev->ep0state]);
  655. /* clear stall status */
  656. if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
  657. s3c2410_udc_nuke(dev, ep, -EPIPE);
  658. dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
  659. s3c2410_udc_clear_ep0_sst(base_addr);
  660. dev->ep0state = EP0_IDLE;
  661. return;
  662. }
  663. /* clear setup end */
  664. if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
  665. dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
  666. s3c2410_udc_nuke(dev, ep, 0);
  667. s3c2410_udc_clear_ep0_se(base_addr);
  668. dev->ep0state = EP0_IDLE;
  669. }
  670. switch (dev->ep0state) {
  671. case EP0_IDLE:
  672. s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
  673. break;
  674. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
  675. dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
  676. if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req) {
  677. s3c2410_udc_write_fifo(ep, req);
  678. }
  679. break;
  680. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
  681. dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
  682. if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req ) {
  683. s3c2410_udc_read_fifo(ep,req);
  684. }
  685. break;
  686. case EP0_END_XFER:
  687. dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
  688. dev->ep0state = EP0_IDLE;
  689. break;
  690. case EP0_STALL:
  691. dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
  692. dev->ep0state = EP0_IDLE;
  693. break;
  694. }
  695. }
  696. /*
  697. * handle_ep - Manage I/O endpoints
  698. */
  699. static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
  700. {
  701. struct s3c2410_request *req;
  702. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  703. u32 ep_csr1;
  704. u32 idx;
  705. if (likely (!list_empty(&ep->queue)))
  706. req = list_entry(ep->queue.next,
  707. struct s3c2410_request, queue);
  708. else
  709. req = NULL;
  710. idx = ep->bEndpointAddress & 0x7F;
  711. if (is_in) {
  712. udc_write(idx, S3C2410_UDC_INDEX_REG);
  713. ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
  714. dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
  715. idx, ep_csr1, req ? 1 : 0);
  716. if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
  717. dprintk(DEBUG_VERBOSE, "st\n");
  718. udc_write(idx, S3C2410_UDC_INDEX_REG);
  719. udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
  720. S3C2410_UDC_IN_CSR1_REG);
  721. return;
  722. }
  723. if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req) {
  724. s3c2410_udc_write_fifo(ep,req);
  725. }
  726. } else {
  727. udc_write(idx, S3C2410_UDC_INDEX_REG);
  728. ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
  729. dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
  730. if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
  731. udc_write(idx, S3C2410_UDC_INDEX_REG);
  732. udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
  733. S3C2410_UDC_OUT_CSR1_REG);
  734. return;
  735. }
  736. if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req) {
  737. s3c2410_udc_read_fifo(ep,req);
  738. }
  739. }
  740. }
  741. #include <mach/regs-irq.h>
  742. /*
  743. * s3c2410_udc_irq - interrupt handler
  744. */
  745. static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
  746. {
  747. struct s3c2410_udc *dev = _dev;
  748. int usb_status;
  749. int usbd_status;
  750. int pwr_reg;
  751. int ep0csr;
  752. int i;
  753. u32 idx, idx2;
  754. unsigned long flags;
  755. spin_lock_irqsave(&dev->lock, flags);
  756. /* Driver connected ? */
  757. if (!dev->driver) {
  758. /* Clear interrupts */
  759. udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
  760. S3C2410_UDC_USB_INT_REG);
  761. udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
  762. S3C2410_UDC_EP_INT_REG);
  763. }
  764. /* Save index */
  765. idx = udc_read(S3C2410_UDC_INDEX_REG);
  766. /* Read status registers */
  767. usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
  768. usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
  769. pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
  770. udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
  771. ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
  772. dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
  773. usb_status, usbd_status, pwr_reg, ep0csr);
  774. /*
  775. * Now, handle interrupts. There's two types :
  776. * - Reset, Resume, Suspend coming -> usb_int_reg
  777. * - EP -> ep_int_reg
  778. */
  779. /* RESET */
  780. if (usb_status & S3C2410_UDC_USBINT_RESET) {
  781. /* two kind of reset :
  782. * - reset start -> pwr reg = 8
  783. * - reset end -> pwr reg = 0
  784. **/
  785. dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
  786. ep0csr, pwr_reg);
  787. dev->gadget.speed = USB_SPEED_UNKNOWN;
  788. udc_write(0x00, S3C2410_UDC_INDEX_REG);
  789. udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
  790. S3C2410_UDC_MAXP_REG);
  791. dev->address = 0;
  792. dev->ep0state = EP0_IDLE;
  793. dev->gadget.speed = USB_SPEED_FULL;
  794. /* clear interrupt */
  795. udc_write(S3C2410_UDC_USBINT_RESET,
  796. S3C2410_UDC_USB_INT_REG);
  797. udc_write(idx, S3C2410_UDC_INDEX_REG);
  798. spin_unlock_irqrestore(&dev->lock, flags);
  799. return IRQ_HANDLED;
  800. }
  801. /* RESUME */
  802. if (usb_status & S3C2410_UDC_USBINT_RESUME) {
  803. dprintk(DEBUG_NORMAL, "USB resume\n");
  804. /* clear interrupt */
  805. udc_write(S3C2410_UDC_USBINT_RESUME,
  806. S3C2410_UDC_USB_INT_REG);
  807. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  808. && dev->driver
  809. && dev->driver->resume)
  810. dev->driver->resume(&dev->gadget);
  811. }
  812. /* SUSPEND */
  813. if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
  814. dprintk(DEBUG_NORMAL, "USB suspend\n");
  815. /* clear interrupt */
  816. udc_write(S3C2410_UDC_USBINT_SUSPEND,
  817. S3C2410_UDC_USB_INT_REG);
  818. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  819. && dev->driver
  820. && dev->driver->suspend)
  821. dev->driver->suspend(&dev->gadget);
  822. dev->ep0state = EP0_IDLE;
  823. }
  824. /* EP */
  825. /* control traffic */
  826. /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
  827. * generate an interrupt
  828. */
  829. if (usbd_status & S3C2410_UDC_INT_EP0) {
  830. dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
  831. /* Clear the interrupt bit by setting it to 1 */
  832. udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
  833. s3c2410_udc_handle_ep0(dev);
  834. }
  835. /* endpoint data transfers */
  836. for (i = 1; i < S3C2410_ENDPOINTS; i++) {
  837. u32 tmp = 1 << i;
  838. if (usbd_status & tmp) {
  839. dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
  840. /* Clear the interrupt bit by setting it to 1 */
  841. udc_write(tmp, S3C2410_UDC_EP_INT_REG);
  842. s3c2410_udc_handle_ep(&dev->ep[i]);
  843. }
  844. }
  845. /* what else causes this interrupt? a receive! who is it? */
  846. if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
  847. for (i = 1; i < S3C2410_ENDPOINTS; i++) {
  848. idx2 = udc_read(S3C2410_UDC_INDEX_REG);
  849. udc_write(i, S3C2410_UDC_INDEX_REG);
  850. if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
  851. s3c2410_udc_handle_ep(&dev->ep[i]);
  852. /* restore index */
  853. udc_write(idx2, S3C2410_UDC_INDEX_REG);
  854. }
  855. }
  856. dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
  857. /* Restore old index */
  858. udc_write(idx, S3C2410_UDC_INDEX_REG);
  859. spin_unlock_irqrestore(&dev->lock, flags);
  860. return IRQ_HANDLED;
  861. }
  862. /*------------------------- s3c2410_ep_ops ----------------------------------*/
  863. static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
  864. {
  865. return container_of(ep, struct s3c2410_ep, ep);
  866. }
  867. static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
  868. {
  869. return container_of(gadget, struct s3c2410_udc, gadget);
  870. }
  871. static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
  872. {
  873. return container_of(req, struct s3c2410_request, req);
  874. }
  875. /*
  876. * s3c2410_udc_ep_enable
  877. */
  878. static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
  879. const struct usb_endpoint_descriptor *desc)
  880. {
  881. struct s3c2410_udc *dev;
  882. struct s3c2410_ep *ep;
  883. u32 max, tmp;
  884. unsigned long flags;
  885. u32 csr1,csr2;
  886. u32 int_en_reg;
  887. ep = to_s3c2410_ep(_ep);
  888. if (!_ep || !desc || ep->desc
  889. || _ep->name == ep0name
  890. || desc->bDescriptorType != USB_DT_ENDPOINT)
  891. return -EINVAL;
  892. dev = ep->dev;
  893. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  894. return -ESHUTDOWN;
  895. max = le16_to_cpu(desc->wMaxPacketSize) & 0x1fff;
  896. local_irq_save (flags);
  897. _ep->maxpacket = max & 0x7ff;
  898. ep->desc = desc;
  899. ep->halted = 0;
  900. ep->bEndpointAddress = desc->bEndpointAddress;
  901. /* set max packet */
  902. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  903. udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
  904. /* set type, direction, address; reset fifo counters */
  905. if (desc->bEndpointAddress & USB_DIR_IN) {
  906. csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
  907. csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
  908. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  909. udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
  910. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  911. udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
  912. } else {
  913. /* don't flush in fifo or it will cause endpoint interrupt */
  914. csr1 = S3C2410_UDC_ICSR1_CLRDT;
  915. csr2 = S3C2410_UDC_ICSR2_DMAIEN;
  916. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  917. udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
  918. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  919. udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
  920. csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
  921. csr2 = S3C2410_UDC_OCSR2_DMAIEN;
  922. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  923. udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
  924. udc_write(ep->num, S3C2410_UDC_INDEX_REG);
  925. udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
  926. }
  927. /* enable irqs */
  928. int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
  929. udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
  930. /* print some debug message */
  931. tmp = desc->bEndpointAddress;
  932. dprintk (DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
  933. _ep->name,ep->num, tmp,
  934. desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
  935. local_irq_restore (flags);
  936. s3c2410_udc_set_halt(_ep, 0);
  937. return 0;
  938. }
  939. /*
  940. * s3c2410_udc_ep_disable
  941. */
  942. static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
  943. {
  944. struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
  945. unsigned long flags;
  946. u32 int_en_reg;
  947. if (!_ep || !ep->desc) {
  948. dprintk(DEBUG_NORMAL, "%s not enabled\n",
  949. _ep ? ep->ep.name : NULL);
  950. return -EINVAL;
  951. }
  952. local_irq_save(flags);
  953. dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
  954. ep->desc = NULL;
  955. ep->halted = 1;
  956. s3c2410_udc_nuke (ep->dev, ep, -ESHUTDOWN);
  957. /* disable irqs */
  958. int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
  959. udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
  960. local_irq_restore(flags);
  961. dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
  962. return 0;
  963. }
  964. /*
  965. * s3c2410_udc_alloc_request
  966. */
  967. static struct usb_request *
  968. s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
  969. {
  970. struct s3c2410_request *req;
  971. dprintk(DEBUG_VERBOSE,"%s(%p,%d)\n", __func__, _ep, mem_flags);
  972. if (!_ep)
  973. return NULL;
  974. req = kzalloc (sizeof(struct s3c2410_request), mem_flags);
  975. if (!req)
  976. return NULL;
  977. INIT_LIST_HEAD (&req->queue);
  978. return &req->req;
  979. }
  980. /*
  981. * s3c2410_udc_free_request
  982. */
  983. static void
  984. s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
  985. {
  986. struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
  987. struct s3c2410_request *req = to_s3c2410_req(_req);
  988. dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
  989. if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
  990. return;
  991. WARN_ON (!list_empty (&req->queue));
  992. kfree(req);
  993. }
  994. /*
  995. * s3c2410_udc_queue
  996. */
  997. static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
  998. gfp_t gfp_flags)
  999. {
  1000. struct s3c2410_request *req = to_s3c2410_req(_req);
  1001. struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
  1002. struct s3c2410_udc *dev;
  1003. u32 ep_csr = 0;
  1004. int fifo_count = 0;
  1005. unsigned long flags;
  1006. if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  1007. dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
  1008. return -EINVAL;
  1009. }
  1010. dev = ep->dev;
  1011. if (unlikely (!dev->driver
  1012. || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  1013. return -ESHUTDOWN;
  1014. }
  1015. local_irq_save (flags);
  1016. if (unlikely(!_req || !_req->complete
  1017. || !_req->buf || !list_empty(&req->queue))) {
  1018. if (!_req)
  1019. dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
  1020. else {
  1021. dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
  1022. __func__, !_req->complete,!_req->buf,
  1023. !list_empty(&req->queue));
  1024. }
  1025. local_irq_restore(flags);
  1026. return -EINVAL;
  1027. }
  1028. _req->status = -EINPROGRESS;
  1029. _req->actual = 0;
  1030. dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
  1031. __func__, ep->bEndpointAddress, _req->length);
  1032. if (ep->bEndpointAddress) {
  1033. udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
  1034. ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
  1035. ? S3C2410_UDC_IN_CSR1_REG
  1036. : S3C2410_UDC_OUT_CSR1_REG);
  1037. fifo_count = s3c2410_udc_fifo_count_out();
  1038. } else {
  1039. udc_write(0, S3C2410_UDC_INDEX_REG);
  1040. ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
  1041. fifo_count = s3c2410_udc_fifo_count_out();
  1042. }
  1043. /* kickstart this i/o queue? */
  1044. if (list_empty(&ep->queue) && !ep->halted) {
  1045. if (ep->bEndpointAddress == 0 /* ep0 */) {
  1046. switch (dev->ep0state) {
  1047. case EP0_IN_DATA_PHASE:
  1048. if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
  1049. && s3c2410_udc_write_fifo(ep,
  1050. req)) {
  1051. dev->ep0state = EP0_IDLE;
  1052. req = NULL;
  1053. }
  1054. break;
  1055. case EP0_OUT_DATA_PHASE:
  1056. if ((!_req->length)
  1057. || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
  1058. && s3c2410_udc_read_fifo(ep,
  1059. req))) {
  1060. dev->ep0state = EP0_IDLE;
  1061. req = NULL;
  1062. }
  1063. break;
  1064. default:
  1065. local_irq_restore(flags);
  1066. return -EL2HLT;
  1067. }
  1068. } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
  1069. && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
  1070. && s3c2410_udc_write_fifo(ep, req)) {
  1071. req = NULL;
  1072. } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
  1073. && fifo_count
  1074. && s3c2410_udc_read_fifo(ep, req)) {
  1075. req = NULL;
  1076. }
  1077. }
  1078. /* pio or dma irq handler advances the queue. */
  1079. if (likely (req != 0))
  1080. list_add_tail(&req->queue, &ep->queue);
  1081. local_irq_restore(flags);
  1082. dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
  1083. return 0;
  1084. }
  1085. /*
  1086. * s3c2410_udc_dequeue
  1087. */
  1088. static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1089. {
  1090. struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
  1091. struct s3c2410_udc *udc;
  1092. int retval = -EINVAL;
  1093. unsigned long flags;
  1094. struct s3c2410_request *req = NULL;
  1095. dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
  1096. if (!the_controller->driver)
  1097. return -ESHUTDOWN;
  1098. if (!_ep || !_req)
  1099. return retval;
  1100. udc = to_s3c2410_udc(ep->gadget);
  1101. local_irq_save (flags);
  1102. list_for_each_entry (req, &ep->queue, queue) {
  1103. if (&req->req == _req) {
  1104. list_del_init (&req->queue);
  1105. _req->status = -ECONNRESET;
  1106. retval = 0;
  1107. break;
  1108. }
  1109. }
  1110. if (retval == 0) {
  1111. dprintk(DEBUG_VERBOSE,
  1112. "dequeued req %p from %s, len %d buf %p\n",
  1113. req, _ep->name, _req->length, _req->buf);
  1114. s3c2410_udc_done(ep, req, -ECONNRESET);
  1115. }
  1116. local_irq_restore (flags);
  1117. return retval;
  1118. }
  1119. /*
  1120. * s3c2410_udc_set_halt
  1121. */
  1122. static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
  1123. {
  1124. struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
  1125. u32 ep_csr = 0;
  1126. unsigned long flags;
  1127. u32 idx;
  1128. if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  1129. dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
  1130. return -EINVAL;
  1131. }
  1132. local_irq_save (flags);
  1133. idx = ep->bEndpointAddress & 0x7F;
  1134. if (idx == 0) {
  1135. s3c2410_udc_set_ep0_ss(base_addr);
  1136. s3c2410_udc_set_ep0_de_out(base_addr);
  1137. } else {
  1138. udc_write(idx, S3C2410_UDC_INDEX_REG);
  1139. ep_csr = udc_read((ep->bEndpointAddress &USB_DIR_IN)
  1140. ? S3C2410_UDC_IN_CSR1_REG
  1141. : S3C2410_UDC_OUT_CSR1_REG);
  1142. if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
  1143. if (value)
  1144. udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
  1145. S3C2410_UDC_IN_CSR1_REG);
  1146. else {
  1147. ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
  1148. udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
  1149. ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
  1150. udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
  1151. }
  1152. } else {
  1153. if (value)
  1154. udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
  1155. S3C2410_UDC_OUT_CSR1_REG);
  1156. else {
  1157. ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
  1158. udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
  1159. ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
  1160. udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
  1161. }
  1162. }
  1163. }
  1164. ep->halted = value ? 1 : 0;
  1165. local_irq_restore (flags);
  1166. return 0;
  1167. }
  1168. static const struct usb_ep_ops s3c2410_ep_ops = {
  1169. .enable = s3c2410_udc_ep_enable,
  1170. .disable = s3c2410_udc_ep_disable,
  1171. .alloc_request = s3c2410_udc_alloc_request,
  1172. .free_request = s3c2410_udc_free_request,
  1173. .queue = s3c2410_udc_queue,
  1174. .dequeue = s3c2410_udc_dequeue,
  1175. .set_halt = s3c2410_udc_set_halt,
  1176. };
  1177. /*------------------------- usb_gadget_ops ----------------------------------*/
  1178. /*
  1179. * s3c2410_udc_get_frame
  1180. */
  1181. static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
  1182. {
  1183. int tmp;
  1184. dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
  1185. tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
  1186. tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
  1187. return tmp;
  1188. }
  1189. /*
  1190. * s3c2410_udc_wakeup
  1191. */
  1192. static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
  1193. {
  1194. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1195. return 0;
  1196. }
  1197. /*
  1198. * s3c2410_udc_set_selfpowered
  1199. */
  1200. static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
  1201. {
  1202. struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
  1203. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1204. if (value)
  1205. udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
  1206. else
  1207. udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  1208. return 0;
  1209. }
  1210. static void s3c2410_udc_disable(struct s3c2410_udc *dev);
  1211. static void s3c2410_udc_enable(struct s3c2410_udc *dev);
  1212. static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
  1213. {
  1214. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1215. if (udc_info && (udc_info->udc_command ||
  1216. gpio_is_valid(udc_info->pullup_pin))) {
  1217. if (is_on)
  1218. s3c2410_udc_enable(udc);
  1219. else {
  1220. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1221. if (udc->driver && udc->driver->disconnect)
  1222. udc->driver->disconnect(&udc->gadget);
  1223. }
  1224. s3c2410_udc_disable(udc);
  1225. }
  1226. }
  1227. else
  1228. return -EOPNOTSUPP;
  1229. return 0;
  1230. }
  1231. static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
  1232. {
  1233. struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
  1234. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1235. udc->vbus = (is_active != 0);
  1236. s3c2410_udc_set_pullup(udc, is_active);
  1237. return 0;
  1238. }
  1239. static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
  1240. {
  1241. struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
  1242. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1243. s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
  1244. return 0;
  1245. }
  1246. static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
  1247. {
  1248. struct s3c2410_udc *dev = _dev;
  1249. unsigned int value;
  1250. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1251. value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
  1252. if (udc_info->vbus_pin_inverted)
  1253. value = !value;
  1254. if (value != dev->vbus)
  1255. s3c2410_udc_vbus_session(&dev->gadget, value);
  1256. return IRQ_HANDLED;
  1257. }
  1258. static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
  1259. {
  1260. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1261. if (udc_info && udc_info->vbus_draw) {
  1262. udc_info->vbus_draw(ma);
  1263. return 0;
  1264. }
  1265. return -ENOTSUPP;
  1266. }
  1267. static const struct usb_gadget_ops s3c2410_ops = {
  1268. .get_frame = s3c2410_udc_get_frame,
  1269. .wakeup = s3c2410_udc_wakeup,
  1270. .set_selfpowered = s3c2410_udc_set_selfpowered,
  1271. .pullup = s3c2410_udc_pullup,
  1272. .vbus_session = s3c2410_udc_vbus_session,
  1273. .vbus_draw = s3c2410_vbus_draw,
  1274. };
  1275. static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
  1276. {
  1277. if (!udc_info)
  1278. return;
  1279. if (udc_info->udc_command) {
  1280. udc_info->udc_command(S3C2410_UDC_P_DISABLE);
  1281. } else if (gpio_is_valid(udc_info->pullup_pin)) {
  1282. int value;
  1283. switch (cmd) {
  1284. case S3C2410_UDC_P_ENABLE:
  1285. value = 1;
  1286. break;
  1287. case S3C2410_UDC_P_DISABLE:
  1288. value = 0;
  1289. break;
  1290. default:
  1291. return;
  1292. }
  1293. value ^= udc_info->pullup_pin_inverted;
  1294. gpio_set_value(udc_info->pullup_pin, value);
  1295. }
  1296. }
  1297. /*------------------------- gadget driver handling---------------------------*/
  1298. /*
  1299. * s3c2410_udc_disable
  1300. */
  1301. static void s3c2410_udc_disable(struct s3c2410_udc *dev)
  1302. {
  1303. dprintk(DEBUG_NORMAL, "%s()\n", __func__);
  1304. /* Disable all interrupts */
  1305. udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
  1306. udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
  1307. /* Clear the interrupt registers */
  1308. udc_write(S3C2410_UDC_USBINT_RESET
  1309. | S3C2410_UDC_USBINT_RESUME
  1310. | S3C2410_UDC_USBINT_SUSPEND,
  1311. S3C2410_UDC_USB_INT_REG);
  1312. udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
  1313. /* Good bye, cruel world */
  1314. s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
  1315. /* Set speed to unknown */
  1316. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1317. }
  1318. /*
  1319. * s3c2410_udc_reinit
  1320. */
  1321. static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
  1322. {
  1323. u32 i;
  1324. /* device/ep0 records init */
  1325. INIT_LIST_HEAD (&dev->gadget.ep_list);
  1326. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1327. dev->ep0state = EP0_IDLE;
  1328. for (i = 0; i < S3C2410_ENDPOINTS; i++) {
  1329. struct s3c2410_ep *ep = &dev->ep[i];
  1330. if (i != 0)
  1331. list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
  1332. ep->dev = dev;
  1333. ep->desc = NULL;
  1334. ep->halted = 0;
  1335. INIT_LIST_HEAD (&ep->queue);
  1336. }
  1337. }
  1338. /*
  1339. * s3c2410_udc_enable
  1340. */
  1341. static void s3c2410_udc_enable(struct s3c2410_udc *dev)
  1342. {
  1343. int i;
  1344. dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
  1345. /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
  1346. dev->gadget.speed = USB_SPEED_FULL;
  1347. /* Set MAXP for all endpoints */
  1348. for (i = 0; i < S3C2410_ENDPOINTS; i++) {
  1349. udc_write(i, S3C2410_UDC_INDEX_REG);
  1350. udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
  1351. S3C2410_UDC_MAXP_REG);
  1352. }
  1353. /* Set default power state */
  1354. udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
  1355. /* Enable reset and suspend interrupt interrupts */
  1356. udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
  1357. S3C2410_UDC_USB_INT_EN_REG);
  1358. /* Enable ep0 interrupt */
  1359. udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
  1360. /* time to say "hello, world" */
  1361. s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
  1362. }
  1363. /*
  1364. * usb_gadget_probe_driver
  1365. */
  1366. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  1367. int (*bind)(struct usb_gadget *))
  1368. {
  1369. struct s3c2410_udc *udc = the_controller;
  1370. int retval;
  1371. dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
  1372. /* Sanity checks */
  1373. if (!udc)
  1374. return -ENODEV;
  1375. if (udc->driver)
  1376. return -EBUSY;
  1377. if (!bind || !driver->setup || driver->speed < USB_SPEED_FULL) {
  1378. printk(KERN_ERR "Invalid driver: bind %p setup %p speed %d\n",
  1379. bind, driver->setup, driver->speed);
  1380. return -EINVAL;
  1381. }
  1382. #if defined(MODULE)
  1383. if (!driver->unbind) {
  1384. printk(KERN_ERR "Invalid driver: no unbind method\n");
  1385. return -EINVAL;
  1386. }
  1387. #endif
  1388. /* Hook the driver */
  1389. udc->driver = driver;
  1390. udc->gadget.dev.driver = &driver->driver;
  1391. /* Bind the driver */
  1392. if ((retval = device_add(&udc->gadget.dev)) != 0) {
  1393. printk(KERN_ERR "Error in device_add() : %d\n",retval);
  1394. goto register_error;
  1395. }
  1396. dprintk(DEBUG_NORMAL, "binding gadget driver '%s'\n",
  1397. driver->driver.name);
  1398. if ((retval = bind(&udc->gadget)) != 0) {
  1399. device_del(&udc->gadget.dev);
  1400. goto register_error;
  1401. }
  1402. /* Enable udc */
  1403. s3c2410_udc_enable(udc);
  1404. return 0;
  1405. register_error:
  1406. udc->driver = NULL;
  1407. udc->gadget.dev.driver = NULL;
  1408. return retval;
  1409. }
  1410. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1411. /*
  1412. * usb_gadget_unregister_driver
  1413. */
  1414. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1415. {
  1416. struct s3c2410_udc *udc = the_controller;
  1417. if (!udc)
  1418. return -ENODEV;
  1419. if (!driver || driver != udc->driver || !driver->unbind)
  1420. return -EINVAL;
  1421. dprintk(DEBUG_NORMAL, "usb_gadget_unregister_driver() '%s'\n",
  1422. driver->driver.name);
  1423. /* report disconnect */
  1424. if (driver->disconnect)
  1425. driver->disconnect(&udc->gadget);
  1426. driver->unbind(&udc->gadget);
  1427. device_del(&udc->gadget.dev);
  1428. udc->driver = NULL;
  1429. /* Disable udc */
  1430. s3c2410_udc_disable(udc);
  1431. return 0;
  1432. }
  1433. /*---------------------------------------------------------------------------*/
  1434. static struct s3c2410_udc memory = {
  1435. .gadget = {
  1436. .ops = &s3c2410_ops,
  1437. .ep0 = &memory.ep[0].ep,
  1438. .name = gadget_name,
  1439. .dev = {
  1440. .init_name = "gadget",
  1441. },
  1442. },
  1443. /* control endpoint */
  1444. .ep[0] = {
  1445. .num = 0,
  1446. .ep = {
  1447. .name = ep0name,
  1448. .ops = &s3c2410_ep_ops,
  1449. .maxpacket = EP0_FIFO_SIZE,
  1450. },
  1451. .dev = &memory,
  1452. },
  1453. /* first group of endpoints */
  1454. .ep[1] = {
  1455. .num = 1,
  1456. .ep = {
  1457. .name = "ep1-bulk",
  1458. .ops = &s3c2410_ep_ops,
  1459. .maxpacket = EP_FIFO_SIZE,
  1460. },
  1461. .dev = &memory,
  1462. .fifo_size = EP_FIFO_SIZE,
  1463. .bEndpointAddress = 1,
  1464. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1465. },
  1466. .ep[2] = {
  1467. .num = 2,
  1468. .ep = {
  1469. .name = "ep2-bulk",
  1470. .ops = &s3c2410_ep_ops,
  1471. .maxpacket = EP_FIFO_SIZE,
  1472. },
  1473. .dev = &memory,
  1474. .fifo_size = EP_FIFO_SIZE,
  1475. .bEndpointAddress = 2,
  1476. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1477. },
  1478. .ep[3] = {
  1479. .num = 3,
  1480. .ep = {
  1481. .name = "ep3-bulk",
  1482. .ops = &s3c2410_ep_ops,
  1483. .maxpacket = EP_FIFO_SIZE,
  1484. },
  1485. .dev = &memory,
  1486. .fifo_size = EP_FIFO_SIZE,
  1487. .bEndpointAddress = 3,
  1488. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1489. },
  1490. .ep[4] = {
  1491. .num = 4,
  1492. .ep = {
  1493. .name = "ep4-bulk",
  1494. .ops = &s3c2410_ep_ops,
  1495. .maxpacket = EP_FIFO_SIZE,
  1496. },
  1497. .dev = &memory,
  1498. .fifo_size = EP_FIFO_SIZE,
  1499. .bEndpointAddress = 4,
  1500. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1501. }
  1502. };
  1503. /*
  1504. * probe - binds to the platform device
  1505. */
  1506. static int s3c2410_udc_probe(struct platform_device *pdev)
  1507. {
  1508. struct s3c2410_udc *udc = &memory;
  1509. struct device *dev = &pdev->dev;
  1510. int retval;
  1511. int irq;
  1512. dev_dbg(dev, "%s()\n", __func__);
  1513. usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
  1514. if (IS_ERR(usb_bus_clock)) {
  1515. dev_err(dev, "failed to get usb bus clock source\n");
  1516. return PTR_ERR(usb_bus_clock);
  1517. }
  1518. clk_enable(usb_bus_clock);
  1519. udc_clock = clk_get(NULL, "usb-device");
  1520. if (IS_ERR(udc_clock)) {
  1521. dev_err(dev, "failed to get udc clock source\n");
  1522. return PTR_ERR(udc_clock);
  1523. }
  1524. clk_enable(udc_clock);
  1525. mdelay(10);
  1526. dev_dbg(dev, "got and enabled clocks\n");
  1527. if (strncmp(pdev->name, "s3c2440", 7) == 0) {
  1528. dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
  1529. memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
  1530. memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
  1531. memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
  1532. memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
  1533. }
  1534. spin_lock_init (&udc->lock);
  1535. udc_info = pdev->dev.platform_data;
  1536. rsrc_start = S3C2410_PA_USBDEV;
  1537. rsrc_len = S3C24XX_SZ_USBDEV;
  1538. if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
  1539. return -EBUSY;
  1540. base_addr = ioremap(rsrc_start, rsrc_len);
  1541. if (!base_addr) {
  1542. retval = -ENOMEM;
  1543. goto err_mem;
  1544. }
  1545. device_initialize(&udc->gadget.dev);
  1546. udc->gadget.dev.parent = &pdev->dev;
  1547. udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
  1548. the_controller = udc;
  1549. platform_set_drvdata(pdev, udc);
  1550. s3c2410_udc_disable(udc);
  1551. s3c2410_udc_reinit(udc);
  1552. /* irq setup after old hardware state is cleaned up */
  1553. retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
  1554. IRQF_DISABLED, gadget_name, udc);
  1555. if (retval != 0) {
  1556. dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
  1557. retval = -EBUSY;
  1558. goto err_map;
  1559. }
  1560. dev_dbg(dev, "got irq %i\n", IRQ_USBD);
  1561. if (udc_info && udc_info->vbus_pin > 0) {
  1562. retval = gpio_request(udc_info->vbus_pin, "udc vbus");
  1563. if (retval < 0) {
  1564. dev_err(dev, "cannot claim vbus pin\n");
  1565. goto err_int;
  1566. }
  1567. irq = gpio_to_irq(udc_info->vbus_pin);
  1568. if (irq < 0) {
  1569. dev_err(dev, "no irq for gpio vbus pin\n");
  1570. goto err_gpio_claim;
  1571. }
  1572. retval = request_irq(irq, s3c2410_udc_vbus_irq,
  1573. IRQF_DISABLED | IRQF_TRIGGER_RISING
  1574. | IRQF_TRIGGER_FALLING | IRQF_SHARED,
  1575. gadget_name, udc);
  1576. if (retval != 0) {
  1577. dev_err(dev, "can't get vbus irq %d, err %d\n",
  1578. irq, retval);
  1579. retval = -EBUSY;
  1580. goto err_gpio_claim;
  1581. }
  1582. dev_dbg(dev, "got irq %i\n", irq);
  1583. } else {
  1584. udc->vbus = 1;
  1585. }
  1586. if (udc_info && !udc_info->udc_command &&
  1587. gpio_is_valid(udc_info->pullup_pin)) {
  1588. retval = gpio_request_one(udc_info->pullup_pin,
  1589. udc_info->vbus_pin_inverted ?
  1590. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  1591. "udc pullup");
  1592. if (retval)
  1593. goto err_vbus_irq;
  1594. }
  1595. if (s3c2410_udc_debugfs_root) {
  1596. udc->regs_info = debugfs_create_file("registers", S_IRUGO,
  1597. s3c2410_udc_debugfs_root,
  1598. udc, &s3c2410_udc_debugfs_fops);
  1599. if (!udc->regs_info)
  1600. dev_warn(dev, "debugfs file creation failed\n");
  1601. }
  1602. dev_dbg(dev, "probe ok\n");
  1603. return 0;
  1604. err_vbus_irq:
  1605. if (udc_info && udc_info->vbus_pin > 0)
  1606. free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
  1607. err_gpio_claim:
  1608. if (udc_info && udc_info->vbus_pin > 0)
  1609. gpio_free(udc_info->vbus_pin);
  1610. err_int:
  1611. free_irq(IRQ_USBD, udc);
  1612. err_map:
  1613. iounmap(base_addr);
  1614. err_mem:
  1615. release_mem_region(rsrc_start, rsrc_len);
  1616. return retval;
  1617. }
  1618. /*
  1619. * s3c2410_udc_remove
  1620. */
  1621. static int s3c2410_udc_remove(struct platform_device *pdev)
  1622. {
  1623. struct s3c2410_udc *udc = platform_get_drvdata(pdev);
  1624. unsigned int irq;
  1625. dev_dbg(&pdev->dev, "%s()\n", __func__);
  1626. if (udc->driver)
  1627. return -EBUSY;
  1628. debugfs_remove(udc->regs_info);
  1629. if (udc_info && !udc_info->udc_command &&
  1630. gpio_is_valid(udc_info->pullup_pin))
  1631. gpio_free(udc_info->pullup_pin);
  1632. if (udc_info && udc_info->vbus_pin > 0) {
  1633. irq = gpio_to_irq(udc_info->vbus_pin);
  1634. free_irq(irq, udc);
  1635. }
  1636. free_irq(IRQ_USBD, udc);
  1637. iounmap(base_addr);
  1638. release_mem_region(rsrc_start, rsrc_len);
  1639. platform_set_drvdata(pdev, NULL);
  1640. if (!IS_ERR(udc_clock) && udc_clock != NULL) {
  1641. clk_disable(udc_clock);
  1642. clk_put(udc_clock);
  1643. udc_clock = NULL;
  1644. }
  1645. if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
  1646. clk_disable(usb_bus_clock);
  1647. clk_put(usb_bus_clock);
  1648. usb_bus_clock = NULL;
  1649. }
  1650. dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
  1651. return 0;
  1652. }
  1653. #ifdef CONFIG_PM
  1654. static int s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
  1655. {
  1656. s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
  1657. return 0;
  1658. }
  1659. static int s3c2410_udc_resume(struct platform_device *pdev)
  1660. {
  1661. s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
  1662. return 0;
  1663. }
  1664. #else
  1665. #define s3c2410_udc_suspend NULL
  1666. #define s3c2410_udc_resume NULL
  1667. #endif
  1668. static struct platform_driver udc_driver_2410 = {
  1669. .driver = {
  1670. .name = "s3c2410-usbgadget",
  1671. .owner = THIS_MODULE,
  1672. },
  1673. .probe = s3c2410_udc_probe,
  1674. .remove = s3c2410_udc_remove,
  1675. .suspend = s3c2410_udc_suspend,
  1676. .resume = s3c2410_udc_resume,
  1677. };
  1678. static struct platform_driver udc_driver_2440 = {
  1679. .driver = {
  1680. .name = "s3c2440-usbgadget",
  1681. .owner = THIS_MODULE,
  1682. },
  1683. .probe = s3c2410_udc_probe,
  1684. .remove = s3c2410_udc_remove,
  1685. .suspend = s3c2410_udc_suspend,
  1686. .resume = s3c2410_udc_resume,
  1687. };
  1688. static int __init udc_init(void)
  1689. {
  1690. int retval;
  1691. dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
  1692. s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
  1693. if (IS_ERR(s3c2410_udc_debugfs_root)) {
  1694. printk(KERN_ERR "%s: debugfs dir creation failed %ld\n",
  1695. gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
  1696. s3c2410_udc_debugfs_root = NULL;
  1697. }
  1698. retval = platform_driver_register(&udc_driver_2410);
  1699. if (retval)
  1700. goto err;
  1701. retval = platform_driver_register(&udc_driver_2440);
  1702. if (retval)
  1703. goto err;
  1704. return 0;
  1705. err:
  1706. debugfs_remove(s3c2410_udc_debugfs_root);
  1707. return retval;
  1708. }
  1709. static void __exit udc_exit(void)
  1710. {
  1711. platform_driver_unregister(&udc_driver_2410);
  1712. platform_driver_unregister(&udc_driver_2440);
  1713. debugfs_remove(s3c2410_udc_debugfs_root);
  1714. }
  1715. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1716. module_init(udc_init);
  1717. module_exit(udc_exit);
  1718. MODULE_AUTHOR(DRIVER_AUTHOR);
  1719. MODULE_DESCRIPTION(DRIVER_DESC);
  1720. MODULE_VERSION(DRIVER_VERSION);
  1721. MODULE_LICENSE("GPL");
  1722. MODULE_ALIAS("platform:s3c2410-usbgadget");
  1723. MODULE_ALIAS("platform:s3c2440-usbgadget");