s3c2410_udc.c 50 KB

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