r8a66597-hcd.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /*
  2. * R8A66597 HCD (Host Controller Driver)
  3. *
  4. * Copyright (C) 2006-2007 Renesas Solutions Corp.
  5. * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
  6. * Portions Copyright (C) 2004-2005 David Brownell
  7. * Portions Copyright (C) 1999 Roman Weissgaerber
  8. *
  9. * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/timer.h>
  31. #include <linux/delay.h>
  32. #include <linux/list.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/usb.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/io.h>
  37. #include <linux/irq.h>
  38. #include "../core/hcd.h"
  39. #include "r8a66597.h"
  40. MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
  41. MODULE_LICENSE("GPL");
  42. MODULE_AUTHOR("Yoshihiro Shimoda");
  43. MODULE_ALIAS("platform:r8a66597_hcd");
  44. #define DRIVER_VERSION "2009-05-26"
  45. static const char hcd_name[] = "r8a66597_hcd";
  46. static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
  47. static int r8a66597_get_frame(struct usb_hcd *hcd);
  48. /* this function must be called with interrupt disabled */
  49. static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  50. unsigned long reg)
  51. {
  52. u16 tmp;
  53. tmp = r8a66597_read(r8a66597, INTENB0);
  54. r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  55. r8a66597_bset(r8a66597, 1 << pipenum, reg);
  56. r8a66597_write(r8a66597, tmp, INTENB0);
  57. }
  58. /* this function must be called with interrupt disabled */
  59. static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  60. unsigned long reg)
  61. {
  62. u16 tmp;
  63. tmp = r8a66597_read(r8a66597, INTENB0);
  64. r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  65. r8a66597_bclr(r8a66597, 1 << pipenum, reg);
  66. r8a66597_write(r8a66597, tmp, INTENB0);
  67. }
  68. static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
  69. u16 usbspd, u8 upphub, u8 hubport, int port)
  70. {
  71. u16 val;
  72. unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
  73. val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
  74. r8a66597_write(r8a66597, val, devadd_reg);
  75. }
  76. static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
  77. {
  78. u16 tmp;
  79. int i = 0;
  80. if (r8a66597->pdata->on_chip) {
  81. #ifdef CONFIG_HAVE_CLK
  82. clk_enable(r8a66597->clk);
  83. #endif
  84. do {
  85. r8a66597_write(r8a66597, SCKE, SYSCFG0);
  86. tmp = r8a66597_read(r8a66597, SYSCFG0);
  87. if (i++ > 1000) {
  88. printk(KERN_ERR "r8a66597: reg access fail.\n");
  89. return -ENXIO;
  90. }
  91. } while ((tmp & SCKE) != SCKE);
  92. r8a66597_write(r8a66597, 0x04, 0x02);
  93. } else {
  94. do {
  95. r8a66597_write(r8a66597, USBE, SYSCFG0);
  96. tmp = r8a66597_read(r8a66597, SYSCFG0);
  97. if (i++ > 1000) {
  98. printk(KERN_ERR "r8a66597: reg access fail.\n");
  99. return -ENXIO;
  100. }
  101. } while ((tmp & USBE) != USBE);
  102. r8a66597_bclr(r8a66597, USBE, SYSCFG0);
  103. r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
  104. XTAL, SYSCFG0);
  105. i = 0;
  106. r8a66597_bset(r8a66597, XCKE, SYSCFG0);
  107. do {
  108. msleep(1);
  109. tmp = r8a66597_read(r8a66597, SYSCFG0);
  110. if (i++ > 500) {
  111. printk(KERN_ERR "r8a66597: reg access fail.\n");
  112. return -ENXIO;
  113. }
  114. } while ((tmp & SCKE) != SCKE);
  115. }
  116. return 0;
  117. }
  118. static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
  119. {
  120. r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
  121. udelay(1);
  122. if (r8a66597->pdata->on_chip) {
  123. #ifdef CONFIG_HAVE_CLK
  124. clk_disable(r8a66597->clk);
  125. #endif
  126. } else {
  127. r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
  128. r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
  129. r8a66597_bclr(r8a66597, USBE, SYSCFG0);
  130. }
  131. }
  132. static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
  133. {
  134. u16 val;
  135. val = port ? DRPD : DCFM | DRPD;
  136. r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
  137. r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
  138. r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
  139. r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
  140. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  141. }
  142. static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
  143. {
  144. u16 val, tmp;
  145. r8a66597_write(r8a66597, 0, get_intenb_reg(port));
  146. r8a66597_write(r8a66597, 0, get_intsts_reg(port));
  147. r8a66597_port_power(r8a66597, port, 0);
  148. do {
  149. tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
  150. udelay(640);
  151. } while (tmp == EDGESTS);
  152. val = port ? DRPD : DCFM | DRPD;
  153. r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
  154. r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
  155. }
  156. static int enable_controller(struct r8a66597 *r8a66597)
  157. {
  158. int ret, port;
  159. u16 vif = r8a66597->pdata->vif ? LDRV : 0;
  160. u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
  161. u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
  162. ret = r8a66597_clock_enable(r8a66597);
  163. if (ret < 0)
  164. return ret;
  165. r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
  166. r8a66597_bset(r8a66597, USBE, SYSCFG0);
  167. r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  168. r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
  169. r8a66597_bset(r8a66597, BRDY0, BRDYENB);
  170. r8a66597_bset(r8a66597, BEMP0, BEMPENB);
  171. r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
  172. r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
  173. r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
  174. r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
  175. r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
  176. for (port = 0; port < r8a66597->max_root_hub; port++)
  177. r8a66597_enable_port(r8a66597, port);
  178. return 0;
  179. }
  180. static void disable_controller(struct r8a66597 *r8a66597)
  181. {
  182. int port;
  183. /* disable interrupts */
  184. r8a66597_write(r8a66597, 0, INTENB0);
  185. r8a66597_write(r8a66597, 0, INTENB1);
  186. r8a66597_write(r8a66597, 0, BRDYENB);
  187. r8a66597_write(r8a66597, 0, BEMPENB);
  188. r8a66597_write(r8a66597, 0, NRDYENB);
  189. /* clear status */
  190. r8a66597_write(r8a66597, 0, BRDYSTS);
  191. r8a66597_write(r8a66597, 0, NRDYSTS);
  192. r8a66597_write(r8a66597, 0, BEMPSTS);
  193. for (port = 0; port < r8a66597->max_root_hub; port++)
  194. r8a66597_disable_port(r8a66597, port);
  195. r8a66597_clock_disable(r8a66597);
  196. }
  197. static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
  198. struct usb_device *udev)
  199. {
  200. struct r8a66597_device *dev;
  201. if (udev->parent && udev->parent->devnum != 1)
  202. udev = udev->parent;
  203. dev = dev_get_drvdata(&udev->dev);
  204. if (dev)
  205. return dev->address;
  206. else
  207. return 0;
  208. }
  209. static int is_child_device(char *devpath)
  210. {
  211. return (devpath[2] ? 1 : 0);
  212. }
  213. static int is_hub_limit(char *devpath)
  214. {
  215. return ((strlen(devpath) >= 4) ? 1 : 0);
  216. }
  217. static void get_port_number(struct r8a66597 *r8a66597,
  218. char *devpath, u16 *root_port, u16 *hub_port)
  219. {
  220. if (root_port) {
  221. *root_port = (devpath[0] & 0x0F) - 1;
  222. if (*root_port >= r8a66597->max_root_hub)
  223. printk(KERN_ERR "r8a66597: Illegal root port number.\n");
  224. }
  225. if (hub_port)
  226. *hub_port = devpath[2] & 0x0F;
  227. }
  228. static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
  229. {
  230. u16 usbspd = 0;
  231. switch (speed) {
  232. case USB_SPEED_LOW:
  233. usbspd = LSMODE;
  234. break;
  235. case USB_SPEED_FULL:
  236. usbspd = FSMODE;
  237. break;
  238. case USB_SPEED_HIGH:
  239. usbspd = HSMODE;
  240. break;
  241. default:
  242. printk(KERN_ERR "r8a66597: unknown speed\n");
  243. break;
  244. }
  245. return usbspd;
  246. }
  247. static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
  248. {
  249. int idx;
  250. idx = address / 32;
  251. r8a66597->child_connect_map[idx] |= 1 << (address % 32);
  252. }
  253. static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
  254. {
  255. int idx;
  256. idx = address / 32;
  257. r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
  258. }
  259. static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
  260. {
  261. u16 pipenum = pipe->info.pipenum;
  262. const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
  263. const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
  264. const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
  265. if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
  266. dma_ch = R8A66597_PIPE_NO_DMA;
  267. pipe->fifoaddr = fifoaddr[dma_ch];
  268. pipe->fifosel = fifosel[dma_ch];
  269. pipe->fifoctr = fifoctr[dma_ch];
  270. if (pipenum == 0)
  271. pipe->pipectr = DCPCTR;
  272. else
  273. pipe->pipectr = get_pipectr_addr(pipenum);
  274. if (check_bulk_or_isoc(pipenum)) {
  275. pipe->pipetre = get_pipetre_addr(pipenum);
  276. pipe->pipetrn = get_pipetrn_addr(pipenum);
  277. } else {
  278. pipe->pipetre = 0;
  279. pipe->pipetrn = 0;
  280. }
  281. }
  282. static struct r8a66597_device *
  283. get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
  284. {
  285. if (usb_pipedevice(urb->pipe) == 0)
  286. return &r8a66597->device0;
  287. return dev_get_drvdata(&urb->dev->dev);
  288. }
  289. static int make_r8a66597_device(struct r8a66597 *r8a66597,
  290. struct urb *urb, u8 addr)
  291. {
  292. struct r8a66597_device *dev;
  293. int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
  294. dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
  295. if (dev == NULL)
  296. return -ENOMEM;
  297. dev_set_drvdata(&urb->dev->dev, dev);
  298. dev->udev = urb->dev;
  299. dev->address = addr;
  300. dev->usb_address = usb_address;
  301. dev->state = USB_STATE_ADDRESS;
  302. dev->ep_in_toggle = 0;
  303. dev->ep_out_toggle = 0;
  304. INIT_LIST_HEAD(&dev->device_list);
  305. list_add_tail(&dev->device_list, &r8a66597->child_device);
  306. get_port_number(r8a66597, urb->dev->devpath,
  307. &dev->root_port, &dev->hub_port);
  308. if (!is_child_device(urb->dev->devpath))
  309. r8a66597->root_hub[dev->root_port].dev = dev;
  310. set_devadd_reg(r8a66597, dev->address,
  311. get_r8a66597_usb_speed(urb->dev->speed),
  312. get_parent_r8a66597_address(r8a66597, urb->dev),
  313. dev->hub_port, dev->root_port);
  314. return 0;
  315. }
  316. /* this function must be called with interrupt disabled */
  317. static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
  318. {
  319. u8 addr; /* R8A66597's address */
  320. struct r8a66597_device *dev;
  321. if (is_hub_limit(urb->dev->devpath)) {
  322. dev_err(&urb->dev->dev, "External hub limit reached.\n");
  323. return 0;
  324. }
  325. dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  326. if (dev && dev->state >= USB_STATE_ADDRESS)
  327. return dev->address;
  328. for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
  329. if (r8a66597->address_map & (1 << addr))
  330. continue;
  331. dbg("alloc_address: r8a66597_addr=%d", addr);
  332. r8a66597->address_map |= 1 << addr;
  333. if (make_r8a66597_device(r8a66597, urb, addr) < 0)
  334. return 0;
  335. return addr;
  336. }
  337. dev_err(&urb->dev->dev,
  338. "cannot communicate with a USB device more than 10.(%x)\n",
  339. r8a66597->address_map);
  340. return 0;
  341. }
  342. /* this function must be called with interrupt disabled */
  343. static void free_usb_address(struct r8a66597 *r8a66597,
  344. struct r8a66597_device *dev)
  345. {
  346. int port;
  347. if (!dev)
  348. return;
  349. dbg("free_addr: addr=%d", dev->address);
  350. dev->state = USB_STATE_DEFAULT;
  351. r8a66597->address_map &= ~(1 << dev->address);
  352. dev->address = 0;
  353. dev_set_drvdata(&dev->udev->dev, NULL);
  354. list_del(&dev->device_list);
  355. kfree(dev);
  356. for (port = 0; port < r8a66597->max_root_hub; port++) {
  357. if (r8a66597->root_hub[port].dev == dev) {
  358. r8a66597->root_hub[port].dev = NULL;
  359. break;
  360. }
  361. }
  362. }
  363. static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
  364. u16 mask, u16 loop)
  365. {
  366. u16 tmp;
  367. int i = 0;
  368. do {
  369. tmp = r8a66597_read(r8a66597, reg);
  370. if (i++ > 1000000) {
  371. printk(KERN_ERR "r8a66597: register%lx, loop %x "
  372. "is timeout\n", reg, loop);
  373. break;
  374. }
  375. ndelay(1);
  376. } while ((tmp & mask) != loop);
  377. }
  378. /* this function must be called with interrupt disabled */
  379. static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
  380. {
  381. u16 tmp;
  382. tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
  383. if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
  384. r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
  385. r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
  386. }
  387. /* this function must be called with interrupt disabled */
  388. static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
  389. {
  390. u16 tmp;
  391. tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
  392. if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
  393. r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
  394. r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
  395. r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
  396. }
  397. /* this function must be called with interrupt disabled */
  398. static void clear_all_buffer(struct r8a66597 *r8a66597,
  399. struct r8a66597_pipe *pipe)
  400. {
  401. u16 tmp;
  402. if (!pipe || pipe->info.pipenum == 0)
  403. return;
  404. pipe_stop(r8a66597, pipe);
  405. r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
  406. tmp = r8a66597_read(r8a66597, pipe->pipectr);
  407. tmp = r8a66597_read(r8a66597, pipe->pipectr);
  408. tmp = r8a66597_read(r8a66597, pipe->pipectr);
  409. r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
  410. }
  411. /* this function must be called with interrupt disabled */
  412. static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
  413. struct r8a66597_pipe *pipe, int toggle)
  414. {
  415. if (toggle)
  416. r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
  417. else
  418. r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
  419. }
  420. static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
  421. {
  422. if (r8a66597->pdata->on_chip)
  423. return MBW_32;
  424. else
  425. return MBW_16;
  426. }
  427. /* this function must be called with interrupt disabled */
  428. static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
  429. {
  430. unsigned short mbw = mbw_value(r8a66597);
  431. r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
  432. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
  433. }
  434. /* this function must be called with interrupt disabled */
  435. static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
  436. struct r8a66597_pipe *pipe)
  437. {
  438. unsigned short mbw = mbw_value(r8a66597);
  439. cfifo_change(r8a66597, 0);
  440. r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
  441. r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
  442. r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
  443. pipe->fifosel);
  444. r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
  445. }
  446. static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
  447. {
  448. struct r8a66597_pipe *pipe = hep->hcpriv;
  449. if (usb_pipeendpoint(urb->pipe) == 0)
  450. return 0;
  451. else
  452. return pipe->info.pipenum;
  453. }
  454. static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
  455. {
  456. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  457. return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
  458. }
  459. static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
  460. int urb_pipe)
  461. {
  462. if (!dev)
  463. return NULL;
  464. return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
  465. }
  466. /* this function must be called with interrupt disabled */
  467. static void pipe_toggle_set(struct r8a66597 *r8a66597,
  468. struct r8a66597_pipe *pipe,
  469. struct urb *urb, int set)
  470. {
  471. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  472. unsigned char endpoint = usb_pipeendpoint(urb->pipe);
  473. unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
  474. if (!toggle)
  475. return;
  476. if (set)
  477. *toggle |= 1 << endpoint;
  478. else
  479. *toggle &= ~(1 << endpoint);
  480. }
  481. /* this function must be called with interrupt disabled */
  482. static void pipe_toggle_save(struct r8a66597 *r8a66597,
  483. struct r8a66597_pipe *pipe,
  484. struct urb *urb)
  485. {
  486. if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
  487. pipe_toggle_set(r8a66597, pipe, urb, 1);
  488. else
  489. pipe_toggle_set(r8a66597, pipe, urb, 0);
  490. }
  491. /* this function must be called with interrupt disabled */
  492. static void pipe_toggle_restore(struct r8a66597 *r8a66597,
  493. struct r8a66597_pipe *pipe,
  494. struct urb *urb)
  495. {
  496. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  497. unsigned char endpoint = usb_pipeendpoint(urb->pipe);
  498. unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
  499. if (!toggle)
  500. return;
  501. r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
  502. }
  503. /* this function must be called with interrupt disabled */
  504. static void pipe_buffer_setting(struct r8a66597 *r8a66597,
  505. struct r8a66597_pipe_info *info)
  506. {
  507. u16 val = 0;
  508. if (info->pipenum == 0)
  509. return;
  510. r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
  511. r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
  512. r8a66597_write(r8a66597, info->pipenum, PIPESEL);
  513. if (!info->dir_in)
  514. val |= R8A66597_DIR;
  515. if (info->type == R8A66597_BULK && info->dir_in)
  516. val |= R8A66597_DBLB | R8A66597_SHTNAK;
  517. val |= info->type | info->epnum;
  518. r8a66597_write(r8a66597, val, PIPECFG);
  519. r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
  520. PIPEBUF);
  521. r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
  522. PIPEMAXP);
  523. r8a66597_write(r8a66597, info->interval, PIPEPERI);
  524. }
  525. /* this function must be called with interrupt disabled */
  526. static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  527. {
  528. struct r8a66597_pipe_info *info;
  529. struct urb *urb = td->urb;
  530. if (td->pipenum > 0) {
  531. info = &td->pipe->info;
  532. cfifo_change(r8a66597, 0);
  533. pipe_buffer_setting(r8a66597, info);
  534. if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  535. usb_pipeout(urb->pipe)) &&
  536. !usb_pipecontrol(urb->pipe)) {
  537. r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
  538. pipe_toggle_set(r8a66597, td->pipe, urb, 0);
  539. clear_all_buffer(r8a66597, td->pipe);
  540. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  541. usb_pipeout(urb->pipe), 1);
  542. }
  543. pipe_toggle_restore(r8a66597, td->pipe, urb);
  544. }
  545. }
  546. /* this function must be called with interrupt disabled */
  547. static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
  548. struct usb_endpoint_descriptor *ep)
  549. {
  550. u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
  551. memset(array, 0, sizeof(array));
  552. switch (usb_endpoint_type(ep)) {
  553. case USB_ENDPOINT_XFER_BULK:
  554. if (usb_endpoint_dir_in(ep))
  555. array[i++] = 4;
  556. else {
  557. array[i++] = 3;
  558. array[i++] = 5;
  559. }
  560. break;
  561. case USB_ENDPOINT_XFER_INT:
  562. if (usb_endpoint_dir_in(ep)) {
  563. array[i++] = 6;
  564. array[i++] = 7;
  565. array[i++] = 8;
  566. } else
  567. array[i++] = 9;
  568. break;
  569. case USB_ENDPOINT_XFER_ISOC:
  570. if (usb_endpoint_dir_in(ep))
  571. array[i++] = 2;
  572. else
  573. array[i++] = 1;
  574. break;
  575. default:
  576. printk(KERN_ERR "r8a66597: Illegal type\n");
  577. return 0;
  578. }
  579. i = 1;
  580. min = array[0];
  581. while (array[i] != 0) {
  582. if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
  583. min = array[i];
  584. i++;
  585. }
  586. return min;
  587. }
  588. static u16 get_r8a66597_type(__u8 type)
  589. {
  590. u16 r8a66597_type;
  591. switch (type) {
  592. case USB_ENDPOINT_XFER_BULK:
  593. r8a66597_type = R8A66597_BULK;
  594. break;
  595. case USB_ENDPOINT_XFER_INT:
  596. r8a66597_type = R8A66597_INT;
  597. break;
  598. case USB_ENDPOINT_XFER_ISOC:
  599. r8a66597_type = R8A66597_ISO;
  600. break;
  601. default:
  602. printk(KERN_ERR "r8a66597: Illegal type\n");
  603. r8a66597_type = 0x0000;
  604. break;
  605. }
  606. return r8a66597_type;
  607. }
  608. static u16 get_bufnum(u16 pipenum)
  609. {
  610. u16 bufnum = 0;
  611. if (pipenum == 0)
  612. bufnum = 0;
  613. else if (check_bulk_or_isoc(pipenum))
  614. bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
  615. else if (check_interrupt(pipenum))
  616. bufnum = 4 + (pipenum - 6);
  617. else
  618. printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
  619. return bufnum;
  620. }
  621. static u16 get_buf_bsize(u16 pipenum)
  622. {
  623. u16 buf_bsize = 0;
  624. if (pipenum == 0)
  625. buf_bsize = 3;
  626. else if (check_bulk_or_isoc(pipenum))
  627. buf_bsize = R8A66597_BUF_BSIZE - 1;
  628. else if (check_interrupt(pipenum))
  629. buf_bsize = 0;
  630. else
  631. printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
  632. return buf_bsize;
  633. }
  634. /* this function must be called with interrupt disabled */
  635. static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
  636. struct r8a66597_device *dev,
  637. struct r8a66597_pipe *pipe,
  638. struct urb *urb)
  639. {
  640. int i;
  641. struct r8a66597_pipe_info *info = &pipe->info;
  642. unsigned short mbw = mbw_value(r8a66597);
  643. /* pipe dma is only for external controlles */
  644. if (r8a66597->pdata->on_chip)
  645. return;
  646. if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
  647. for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
  648. if ((r8a66597->dma_map & (1 << i)) != 0)
  649. continue;
  650. dev_info(&dev->udev->dev,
  651. "address %d, EndpointAddress 0x%02x use "
  652. "DMA FIFO\n", usb_pipedevice(urb->pipe),
  653. info->dir_in ?
  654. USB_ENDPOINT_DIR_MASK + info->epnum
  655. : info->epnum);
  656. r8a66597->dma_map |= 1 << i;
  657. dev->dma_map |= 1 << i;
  658. set_pipe_reg_addr(pipe, i);
  659. cfifo_change(r8a66597, 0);
  660. r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
  661. mbw | CURPIPE, pipe->fifosel);
  662. r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
  663. pipe->info.pipenum);
  664. r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
  665. break;
  666. }
  667. }
  668. }
  669. /* this function must be called with interrupt disabled */
  670. static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
  671. struct usb_host_endpoint *hep,
  672. struct r8a66597_pipe_info *info)
  673. {
  674. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  675. struct r8a66597_pipe *pipe = hep->hcpriv;
  676. dbg("enable_pipe:");
  677. pipe->info = *info;
  678. set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
  679. r8a66597->pipe_cnt[pipe->info.pipenum]++;
  680. dev->pipe_cnt[pipe->info.pipenum]++;
  681. enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
  682. }
  683. /* this function must be called with interrupt disabled */
  684. static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
  685. {
  686. struct r8a66597_td *td, *next;
  687. struct urb *urb;
  688. struct list_head *list = &r8a66597->pipe_queue[pipenum];
  689. if (list_empty(list))
  690. return;
  691. list_for_each_entry_safe(td, next, list, queue) {
  692. if (td->address != address)
  693. continue;
  694. urb = td->urb;
  695. list_del(&td->queue);
  696. kfree(td);
  697. if (urb) {
  698. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
  699. urb);
  700. spin_unlock(&r8a66597->lock);
  701. usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
  702. -ENODEV);
  703. spin_lock(&r8a66597->lock);
  704. }
  705. break;
  706. }
  707. }
  708. /* this function must be called with interrupt disabled */
  709. static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
  710. struct r8a66597_device *dev)
  711. {
  712. int check_ep0 = 0;
  713. u16 pipenum;
  714. if (!dev)
  715. return;
  716. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  717. if (!dev->pipe_cnt[pipenum])
  718. continue;
  719. if (!check_ep0) {
  720. check_ep0 = 1;
  721. force_dequeue(r8a66597, 0, dev->address);
  722. }
  723. r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
  724. dev->pipe_cnt[pipenum] = 0;
  725. force_dequeue(r8a66597, pipenum, dev->address);
  726. }
  727. dbg("disable_pipe");
  728. r8a66597->dma_map &= ~(dev->dma_map);
  729. dev->dma_map = 0;
  730. }
  731. static u16 get_interval(struct urb *urb, __u8 interval)
  732. {
  733. u16 time = 1;
  734. int i;
  735. if (urb->dev->speed == USB_SPEED_HIGH) {
  736. if (interval > IITV)
  737. time = IITV;
  738. else
  739. time = interval ? interval - 1 : 0;
  740. } else {
  741. if (interval > 128) {
  742. time = IITV;
  743. } else {
  744. /* calculate the nearest value for PIPEPERI */
  745. for (i = 0; i < 7; i++) {
  746. if ((1 << i) < interval &&
  747. (1 << (i + 1) > interval))
  748. time = 1 << i;
  749. }
  750. }
  751. }
  752. return time;
  753. }
  754. static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
  755. {
  756. __u8 i;
  757. unsigned long time = 1;
  758. if (usb_pipeisoc(urb->pipe))
  759. return 0;
  760. if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
  761. for (i = 0; i < (interval - 1); i++)
  762. time *= 2;
  763. time = time * 125 / 1000; /* uSOF -> msec */
  764. } else {
  765. time = interval;
  766. }
  767. return time;
  768. }
  769. /* this function must be called with interrupt disabled */
  770. static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
  771. struct usb_host_endpoint *hep,
  772. struct usb_endpoint_descriptor *ep)
  773. {
  774. struct r8a66597_pipe_info info;
  775. info.pipenum = get_empty_pipenum(r8a66597, ep);
  776. info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
  777. info.epnum = usb_endpoint_num(ep);
  778. info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
  779. info.type = get_r8a66597_type(usb_endpoint_type(ep));
  780. info.bufnum = get_bufnum(info.pipenum);
  781. info.buf_bsize = get_buf_bsize(info.pipenum);
  782. if (info.type == R8A66597_BULK) {
  783. info.interval = 0;
  784. info.timer_interval = 0;
  785. } else {
  786. info.interval = get_interval(urb, ep->bInterval);
  787. info.timer_interval = get_timer_interval(urb, ep->bInterval);
  788. }
  789. if (usb_endpoint_dir_in(ep))
  790. info.dir_in = 1;
  791. else
  792. info.dir_in = 0;
  793. enable_r8a66597_pipe(r8a66597, urb, hep, &info);
  794. }
  795. static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  796. {
  797. struct r8a66597_device *dev;
  798. dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  799. dev->state = USB_STATE_CONFIGURED;
  800. }
  801. static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
  802. u16 pipenum)
  803. {
  804. if (pipenum == 0 && usb_pipeout(urb->pipe))
  805. enable_irq_empty(r8a66597, pipenum);
  806. else
  807. enable_irq_ready(r8a66597, pipenum);
  808. if (!usb_pipeisoc(urb->pipe))
  809. enable_irq_nrdy(r8a66597, pipenum);
  810. }
  811. static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
  812. {
  813. disable_irq_ready(r8a66597, pipenum);
  814. disable_irq_nrdy(r8a66597, pipenum);
  815. }
  816. static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
  817. {
  818. mod_timer(&r8a66597->rh_timer,
  819. jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
  820. }
  821. static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
  822. int connect)
  823. {
  824. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  825. rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  826. rh->scount = R8A66597_MAX_SAMPLING;
  827. if (connect)
  828. rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
  829. else
  830. rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
  831. rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
  832. r8a66597_root_hub_start_polling(r8a66597);
  833. }
  834. /* this function must be called with interrupt disabled */
  835. static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
  836. u16 syssts)
  837. {
  838. if (syssts == SE0) {
  839. r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
  840. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  841. } else {
  842. if (syssts == FS_JSTS)
  843. r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
  844. else if (syssts == LS_JSTS)
  845. r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
  846. r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
  847. r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
  848. if (r8a66597->bus_suspended)
  849. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  850. }
  851. usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
  852. }
  853. /* this function must be called with interrupt disabled */
  854. static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
  855. {
  856. u16 speed = get_rh_usb_speed(r8a66597, port);
  857. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  858. rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
  859. (1 << USB_PORT_FEAT_LOWSPEED));
  860. if (speed == HSMODE)
  861. rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
  862. else if (speed == LSMODE)
  863. rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
  864. rh->port &= ~(1 << USB_PORT_FEAT_RESET);
  865. rh->port |= 1 << USB_PORT_FEAT_ENABLE;
  866. }
  867. /* this function must be called with interrupt disabled */
  868. static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
  869. {
  870. struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
  871. disable_r8a66597_pipe_all(r8a66597, dev);
  872. free_usb_address(r8a66597, dev);
  873. start_root_hub_sampling(r8a66597, port, 0);
  874. }
  875. /* this function must be called with interrupt disabled */
  876. static void prepare_setup_packet(struct r8a66597 *r8a66597,
  877. struct r8a66597_td *td)
  878. {
  879. int i;
  880. __le16 *p = (__le16 *)td->urb->setup_packet;
  881. unsigned long setup_addr = USBREQ;
  882. r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
  883. DCPMAXP);
  884. r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
  885. for (i = 0; i < 4; i++) {
  886. r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
  887. setup_addr += 2;
  888. }
  889. r8a66597_write(r8a66597, SUREQ, DCPCTR);
  890. }
  891. /* this function must be called with interrupt disabled */
  892. static void prepare_packet_read(struct r8a66597 *r8a66597,
  893. struct r8a66597_td *td)
  894. {
  895. struct urb *urb = td->urb;
  896. if (usb_pipecontrol(urb->pipe)) {
  897. r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
  898. r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
  899. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  900. if (urb->actual_length == 0) {
  901. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  902. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  903. }
  904. pipe_irq_disable(r8a66597, td->pipenum);
  905. pipe_start(r8a66597, td->pipe);
  906. pipe_irq_enable(r8a66597, urb, td->pipenum);
  907. } else {
  908. if (urb->actual_length == 0) {
  909. pipe_irq_disable(r8a66597, td->pipenum);
  910. pipe_setting(r8a66597, td);
  911. pipe_stop(r8a66597, td->pipe);
  912. r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
  913. if (td->pipe->pipetre) {
  914. r8a66597_write(r8a66597, TRCLR,
  915. td->pipe->pipetre);
  916. r8a66597_write(r8a66597,
  917. DIV_ROUND_UP
  918. (urb->transfer_buffer_length,
  919. td->maxpacket),
  920. td->pipe->pipetrn);
  921. r8a66597_bset(r8a66597, TRENB,
  922. td->pipe->pipetre);
  923. }
  924. pipe_start(r8a66597, td->pipe);
  925. pipe_irq_enable(r8a66597, urb, td->pipenum);
  926. }
  927. }
  928. }
  929. /* this function must be called with interrupt disabled */
  930. static void prepare_packet_write(struct r8a66597 *r8a66597,
  931. struct r8a66597_td *td)
  932. {
  933. u16 tmp;
  934. struct urb *urb = td->urb;
  935. if (usb_pipecontrol(urb->pipe)) {
  936. pipe_stop(r8a66597, td->pipe);
  937. r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
  938. r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
  939. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  940. if (urb->actual_length == 0) {
  941. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  942. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  943. }
  944. } else {
  945. if (urb->actual_length == 0)
  946. pipe_setting(r8a66597, td);
  947. if (td->pipe->pipetre)
  948. r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
  949. }
  950. r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
  951. fifo_change_from_pipe(r8a66597, td->pipe);
  952. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  953. if (unlikely((tmp & FRDY) == 0))
  954. pipe_irq_enable(r8a66597, urb, td->pipenum);
  955. else
  956. packet_write(r8a66597, td->pipenum);
  957. pipe_start(r8a66597, td->pipe);
  958. }
  959. /* this function must be called with interrupt disabled */
  960. static void prepare_status_packet(struct r8a66597 *r8a66597,
  961. struct r8a66597_td *td)
  962. {
  963. struct urb *urb = td->urb;
  964. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  965. pipe_stop(r8a66597, td->pipe);
  966. if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
  967. r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
  968. r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
  969. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  970. r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
  971. r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
  972. enable_irq_empty(r8a66597, 0);
  973. } else {
  974. r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
  975. r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
  976. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  977. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  978. enable_irq_ready(r8a66597, 0);
  979. }
  980. enable_irq_nrdy(r8a66597, 0);
  981. pipe_start(r8a66597, td->pipe);
  982. }
  983. static int is_set_address(unsigned char *setup_packet)
  984. {
  985. if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
  986. setup_packet[1] == USB_REQ_SET_ADDRESS)
  987. return 1;
  988. else
  989. return 0;
  990. }
  991. /* this function must be called with interrupt disabled */
  992. static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  993. {
  994. BUG_ON(!td);
  995. switch (td->type) {
  996. case USB_PID_SETUP:
  997. if (is_set_address(td->urb->setup_packet)) {
  998. td->set_address = 1;
  999. td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
  1000. td->urb);
  1001. if (td->urb->setup_packet[2] == 0)
  1002. return -EPIPE;
  1003. }
  1004. prepare_setup_packet(r8a66597, td);
  1005. break;
  1006. case USB_PID_IN:
  1007. prepare_packet_read(r8a66597, td);
  1008. break;
  1009. case USB_PID_OUT:
  1010. prepare_packet_write(r8a66597, td);
  1011. break;
  1012. case USB_PID_ACK:
  1013. prepare_status_packet(r8a66597, td);
  1014. break;
  1015. default:
  1016. printk(KERN_ERR "r8a66597: invalid type.\n");
  1017. break;
  1018. }
  1019. return 0;
  1020. }
  1021. static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
  1022. {
  1023. if (usb_pipeisoc(urb->pipe)) {
  1024. if (urb->number_of_packets == td->iso_cnt)
  1025. return 1;
  1026. }
  1027. /* control or bulk or interrupt */
  1028. if ((urb->transfer_buffer_length <= urb->actual_length) ||
  1029. (td->short_packet) || (td->zero_packet))
  1030. return 1;
  1031. return 0;
  1032. }
  1033. /* this function must be called with interrupt disabled */
  1034. static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  1035. {
  1036. unsigned long time;
  1037. BUG_ON(!td);
  1038. if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
  1039. !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
  1040. r8a66597->timeout_map |= 1 << td->pipenum;
  1041. switch (usb_pipetype(td->urb->pipe)) {
  1042. case PIPE_INTERRUPT:
  1043. case PIPE_ISOCHRONOUS:
  1044. time = 30;
  1045. break;
  1046. default:
  1047. time = 300;
  1048. break;
  1049. }
  1050. mod_timer(&r8a66597->td_timer[td->pipenum],
  1051. jiffies + msecs_to_jiffies(time));
  1052. }
  1053. }
  1054. /* this function must be called with interrupt disabled */
  1055. static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
  1056. u16 pipenum, struct urb *urb, int status)
  1057. __releases(r8a66597->lock) __acquires(r8a66597->lock)
  1058. {
  1059. int restart = 0;
  1060. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1061. r8a66597->timeout_map &= ~(1 << pipenum);
  1062. if (likely(td)) {
  1063. if (td->set_address && (status != 0 || urb->unlinked))
  1064. r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
  1065. pipe_toggle_save(r8a66597, td->pipe, urb);
  1066. list_del(&td->queue);
  1067. kfree(td);
  1068. }
  1069. if (!list_empty(&r8a66597->pipe_queue[pipenum]))
  1070. restart = 1;
  1071. if (likely(urb)) {
  1072. if (usb_pipeisoc(urb->pipe))
  1073. urb->start_frame = r8a66597_get_frame(hcd);
  1074. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
  1075. spin_unlock(&r8a66597->lock);
  1076. usb_hcd_giveback_urb(hcd, urb, status);
  1077. spin_lock(&r8a66597->lock);
  1078. }
  1079. if (restart) {
  1080. td = r8a66597_get_td(r8a66597, pipenum);
  1081. if (unlikely(!td))
  1082. return;
  1083. start_transfer(r8a66597, td);
  1084. set_td_timer(r8a66597, td);
  1085. }
  1086. }
  1087. static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
  1088. {
  1089. u16 tmp;
  1090. int rcv_len, bufsize, urb_len, size;
  1091. u16 *buf;
  1092. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1093. struct urb *urb;
  1094. int finish = 0;
  1095. int status = 0;
  1096. if (unlikely(!td))
  1097. return;
  1098. urb = td->urb;
  1099. fifo_change_from_pipe(r8a66597, td->pipe);
  1100. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1101. if (unlikely((tmp & FRDY) == 0)) {
  1102. pipe_stop(r8a66597, td->pipe);
  1103. pipe_irq_disable(r8a66597, pipenum);
  1104. printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
  1105. finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
  1106. return;
  1107. }
  1108. /* prepare parameters */
  1109. rcv_len = tmp & DTLN;
  1110. if (usb_pipeisoc(urb->pipe)) {
  1111. buf = (u16 *)(urb->transfer_buffer +
  1112. urb->iso_frame_desc[td->iso_cnt].offset);
  1113. urb_len = urb->iso_frame_desc[td->iso_cnt].length;
  1114. } else {
  1115. buf = (void *)urb->transfer_buffer + urb->actual_length;
  1116. urb_len = urb->transfer_buffer_length - urb->actual_length;
  1117. }
  1118. bufsize = min(urb_len, (int) td->maxpacket);
  1119. if (rcv_len <= bufsize) {
  1120. size = rcv_len;
  1121. } else {
  1122. size = bufsize;
  1123. status = -EOVERFLOW;
  1124. finish = 1;
  1125. }
  1126. /* update parameters */
  1127. urb->actual_length += size;
  1128. if (rcv_len == 0)
  1129. td->zero_packet = 1;
  1130. if (rcv_len < bufsize) {
  1131. td->short_packet = 1;
  1132. }
  1133. if (usb_pipeisoc(urb->pipe)) {
  1134. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1135. urb->iso_frame_desc[td->iso_cnt].status = status;
  1136. td->iso_cnt++;
  1137. finish = 0;
  1138. }
  1139. /* check transfer finish */
  1140. if (finish || check_transfer_finish(td, urb)) {
  1141. pipe_stop(r8a66597, td->pipe);
  1142. pipe_irq_disable(r8a66597, pipenum);
  1143. finish = 1;
  1144. }
  1145. /* read fifo */
  1146. if (urb->transfer_buffer) {
  1147. if (size == 0)
  1148. r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
  1149. else
  1150. r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
  1151. buf, size);
  1152. }
  1153. if (finish && pipenum != 0)
  1154. finish_request(r8a66597, td, pipenum, urb, status);
  1155. }
  1156. static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
  1157. {
  1158. u16 tmp;
  1159. int bufsize, size;
  1160. u16 *buf;
  1161. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1162. struct urb *urb;
  1163. if (unlikely(!td))
  1164. return;
  1165. urb = td->urb;
  1166. fifo_change_from_pipe(r8a66597, td->pipe);
  1167. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1168. if (unlikely((tmp & FRDY) == 0)) {
  1169. pipe_stop(r8a66597, td->pipe);
  1170. pipe_irq_disable(r8a66597, pipenum);
  1171. printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
  1172. finish_request(r8a66597, td, pipenum, urb, -EPIPE);
  1173. return;
  1174. }
  1175. /* prepare parameters */
  1176. bufsize = td->maxpacket;
  1177. if (usb_pipeisoc(urb->pipe)) {
  1178. buf = (u16 *)(urb->transfer_buffer +
  1179. urb->iso_frame_desc[td->iso_cnt].offset);
  1180. size = min(bufsize,
  1181. (int)urb->iso_frame_desc[td->iso_cnt].length);
  1182. } else {
  1183. buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
  1184. size = min_t(u32, bufsize,
  1185. urb->transfer_buffer_length - urb->actual_length);
  1186. }
  1187. /* write fifo */
  1188. if (pipenum > 0)
  1189. r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
  1190. if (urb->transfer_buffer) {
  1191. r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
  1192. if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
  1193. r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
  1194. }
  1195. /* update parameters */
  1196. urb->actual_length += size;
  1197. if (usb_pipeisoc(urb->pipe)) {
  1198. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1199. urb->iso_frame_desc[td->iso_cnt].status = 0;
  1200. td->iso_cnt++;
  1201. }
  1202. /* check transfer finish */
  1203. if (check_transfer_finish(td, urb)) {
  1204. disable_irq_ready(r8a66597, pipenum);
  1205. enable_irq_empty(r8a66597, pipenum);
  1206. if (!usb_pipeisoc(urb->pipe))
  1207. enable_irq_nrdy(r8a66597, pipenum);
  1208. } else
  1209. pipe_irq_enable(r8a66597, urb, pipenum);
  1210. }
  1211. static void check_next_phase(struct r8a66597 *r8a66597, int status)
  1212. {
  1213. struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
  1214. struct urb *urb;
  1215. u8 finish = 0;
  1216. if (unlikely(!td))
  1217. return;
  1218. urb = td->urb;
  1219. switch (td->type) {
  1220. case USB_PID_IN:
  1221. case USB_PID_OUT:
  1222. if (check_transfer_finish(td, urb))
  1223. td->type = USB_PID_ACK;
  1224. break;
  1225. case USB_PID_SETUP:
  1226. if (urb->transfer_buffer_length == urb->actual_length)
  1227. td->type = USB_PID_ACK;
  1228. else if (usb_pipeout(urb->pipe))
  1229. td->type = USB_PID_OUT;
  1230. else
  1231. td->type = USB_PID_IN;
  1232. break;
  1233. case USB_PID_ACK:
  1234. finish = 1;
  1235. break;
  1236. }
  1237. if (finish || status != 0 || urb->unlinked)
  1238. finish_request(r8a66597, td, 0, urb, status);
  1239. else
  1240. start_transfer(r8a66597, td);
  1241. }
  1242. static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
  1243. {
  1244. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1245. if (td) {
  1246. u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
  1247. if (pid == PID_NAK)
  1248. return -ECONNRESET;
  1249. else
  1250. return -EPIPE;
  1251. }
  1252. return 0;
  1253. }
  1254. static void irq_pipe_ready(struct r8a66597 *r8a66597)
  1255. {
  1256. u16 check;
  1257. u16 pipenum;
  1258. u16 mask;
  1259. struct r8a66597_td *td;
  1260. mask = r8a66597_read(r8a66597, BRDYSTS)
  1261. & r8a66597_read(r8a66597, BRDYENB);
  1262. r8a66597_write(r8a66597, ~mask, BRDYSTS);
  1263. if (mask & BRDY0) {
  1264. td = r8a66597_get_td(r8a66597, 0);
  1265. if (td && td->type == USB_PID_IN)
  1266. packet_read(r8a66597, 0);
  1267. else
  1268. pipe_irq_disable(r8a66597, 0);
  1269. check_next_phase(r8a66597, 0);
  1270. }
  1271. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1272. check = 1 << pipenum;
  1273. if (mask & check) {
  1274. td = r8a66597_get_td(r8a66597, pipenum);
  1275. if (unlikely(!td))
  1276. continue;
  1277. if (td->type == USB_PID_IN)
  1278. packet_read(r8a66597, pipenum);
  1279. else if (td->type == USB_PID_OUT)
  1280. packet_write(r8a66597, pipenum);
  1281. }
  1282. }
  1283. }
  1284. static void irq_pipe_empty(struct r8a66597 *r8a66597)
  1285. {
  1286. u16 tmp;
  1287. u16 check;
  1288. u16 pipenum;
  1289. u16 mask;
  1290. struct r8a66597_td *td;
  1291. mask = r8a66597_read(r8a66597, BEMPSTS)
  1292. & r8a66597_read(r8a66597, BEMPENB);
  1293. r8a66597_write(r8a66597, ~mask, BEMPSTS);
  1294. if (mask & BEMP0) {
  1295. cfifo_change(r8a66597, 0);
  1296. td = r8a66597_get_td(r8a66597, 0);
  1297. if (td && td->type != USB_PID_OUT)
  1298. disable_irq_empty(r8a66597, 0);
  1299. check_next_phase(r8a66597, 0);
  1300. }
  1301. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1302. check = 1 << pipenum;
  1303. if (mask & check) {
  1304. struct r8a66597_td *td;
  1305. td = r8a66597_get_td(r8a66597, pipenum);
  1306. if (unlikely(!td))
  1307. continue;
  1308. tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
  1309. if ((tmp & INBUFM) == 0) {
  1310. disable_irq_empty(r8a66597, pipenum);
  1311. pipe_irq_disable(r8a66597, pipenum);
  1312. finish_request(r8a66597, td, pipenum, td->urb,
  1313. 0);
  1314. }
  1315. }
  1316. }
  1317. }
  1318. static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
  1319. {
  1320. u16 check;
  1321. u16 pipenum;
  1322. u16 mask;
  1323. int status;
  1324. mask = r8a66597_read(r8a66597, NRDYSTS)
  1325. & r8a66597_read(r8a66597, NRDYENB);
  1326. r8a66597_write(r8a66597, ~mask, NRDYSTS);
  1327. if (mask & NRDY0) {
  1328. cfifo_change(r8a66597, 0);
  1329. status = get_urb_error(r8a66597, 0);
  1330. pipe_irq_disable(r8a66597, 0);
  1331. check_next_phase(r8a66597, status);
  1332. }
  1333. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1334. check = 1 << pipenum;
  1335. if (mask & check) {
  1336. struct r8a66597_td *td;
  1337. td = r8a66597_get_td(r8a66597, pipenum);
  1338. if (unlikely(!td))
  1339. continue;
  1340. status = get_urb_error(r8a66597, pipenum);
  1341. pipe_irq_disable(r8a66597, pipenum);
  1342. pipe_stop(r8a66597, td->pipe);
  1343. finish_request(r8a66597, td, pipenum, td->urb, status);
  1344. }
  1345. }
  1346. }
  1347. static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
  1348. {
  1349. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1350. u16 intsts0, intsts1, intsts2;
  1351. u16 intenb0, intenb1, intenb2;
  1352. u16 mask0, mask1, mask2;
  1353. int status;
  1354. spin_lock(&r8a66597->lock);
  1355. intsts0 = r8a66597_read(r8a66597, INTSTS0);
  1356. intsts1 = r8a66597_read(r8a66597, INTSTS1);
  1357. intsts2 = r8a66597_read(r8a66597, INTSTS2);
  1358. intenb0 = r8a66597_read(r8a66597, INTENB0);
  1359. intenb1 = r8a66597_read(r8a66597, INTENB1);
  1360. intenb2 = r8a66597_read(r8a66597, INTENB2);
  1361. mask2 = intsts2 & intenb2;
  1362. mask1 = intsts1 & intenb1;
  1363. mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
  1364. if (mask2) {
  1365. if (mask2 & ATTCH) {
  1366. r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
  1367. r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
  1368. /* start usb bus sampling */
  1369. start_root_hub_sampling(r8a66597, 1, 1);
  1370. }
  1371. if (mask2 & DTCH) {
  1372. r8a66597_write(r8a66597, ~DTCH, INTSTS2);
  1373. r8a66597_bclr(r8a66597, DTCHE, INTENB2);
  1374. r8a66597_usb_disconnect(r8a66597, 1);
  1375. }
  1376. if (mask2 & BCHG) {
  1377. r8a66597_write(r8a66597, ~BCHG, INTSTS2);
  1378. r8a66597_bclr(r8a66597, BCHGE, INTENB2);
  1379. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  1380. }
  1381. }
  1382. if (mask1) {
  1383. if (mask1 & ATTCH) {
  1384. r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
  1385. r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
  1386. /* start usb bus sampling */
  1387. start_root_hub_sampling(r8a66597, 0, 1);
  1388. }
  1389. if (mask1 & DTCH) {
  1390. r8a66597_write(r8a66597, ~DTCH, INTSTS1);
  1391. r8a66597_bclr(r8a66597, DTCHE, INTENB1);
  1392. r8a66597_usb_disconnect(r8a66597, 0);
  1393. }
  1394. if (mask1 & BCHG) {
  1395. r8a66597_write(r8a66597, ~BCHG, INTSTS1);
  1396. r8a66597_bclr(r8a66597, BCHGE, INTENB1);
  1397. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  1398. }
  1399. if (mask1 & SIGN) {
  1400. r8a66597_write(r8a66597, ~SIGN, INTSTS1);
  1401. status = get_urb_error(r8a66597, 0);
  1402. check_next_phase(r8a66597, status);
  1403. }
  1404. if (mask1 & SACK) {
  1405. r8a66597_write(r8a66597, ~SACK, INTSTS1);
  1406. check_next_phase(r8a66597, 0);
  1407. }
  1408. }
  1409. if (mask0) {
  1410. if (mask0 & BRDY)
  1411. irq_pipe_ready(r8a66597);
  1412. if (mask0 & BEMP)
  1413. irq_pipe_empty(r8a66597);
  1414. if (mask0 & NRDY)
  1415. irq_pipe_nrdy(r8a66597);
  1416. }
  1417. spin_unlock(&r8a66597->lock);
  1418. return IRQ_HANDLED;
  1419. }
  1420. /* this function must be called with interrupt disabled */
  1421. static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
  1422. {
  1423. u16 tmp;
  1424. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1425. if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
  1426. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1427. tmp = r8a66597_read(r8a66597, dvstctr_reg);
  1428. if ((tmp & USBRST) == USBRST) {
  1429. r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
  1430. dvstctr_reg);
  1431. r8a66597_root_hub_start_polling(r8a66597);
  1432. } else
  1433. r8a66597_usb_connect(r8a66597, port);
  1434. }
  1435. if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
  1436. r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
  1437. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  1438. }
  1439. if (rh->scount > 0) {
  1440. tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  1441. if (tmp == rh->old_syssts) {
  1442. rh->scount--;
  1443. if (rh->scount == 0)
  1444. r8a66597_check_syssts(r8a66597, port, tmp);
  1445. else
  1446. r8a66597_root_hub_start_polling(r8a66597);
  1447. } else {
  1448. rh->scount = R8A66597_MAX_SAMPLING;
  1449. rh->old_syssts = tmp;
  1450. r8a66597_root_hub_start_polling(r8a66597);
  1451. }
  1452. }
  1453. }
  1454. static void r8a66597_interval_timer(unsigned long _r8a66597)
  1455. {
  1456. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1457. unsigned long flags;
  1458. u16 pipenum;
  1459. struct r8a66597_td *td;
  1460. spin_lock_irqsave(&r8a66597->lock, flags);
  1461. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1462. if (!(r8a66597->interval_map & (1 << pipenum)))
  1463. continue;
  1464. if (timer_pending(&r8a66597->interval_timer[pipenum]))
  1465. continue;
  1466. td = r8a66597_get_td(r8a66597, pipenum);
  1467. if (td)
  1468. start_transfer(r8a66597, td);
  1469. }
  1470. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1471. }
  1472. static void r8a66597_td_timer(unsigned long _r8a66597)
  1473. {
  1474. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1475. unsigned long flags;
  1476. u16 pipenum;
  1477. struct r8a66597_td *td, *new_td = NULL;
  1478. struct r8a66597_pipe *pipe;
  1479. spin_lock_irqsave(&r8a66597->lock, flags);
  1480. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1481. if (!(r8a66597->timeout_map & (1 << pipenum)))
  1482. continue;
  1483. if (timer_pending(&r8a66597->td_timer[pipenum]))
  1484. continue;
  1485. td = r8a66597_get_td(r8a66597, pipenum);
  1486. if (!td) {
  1487. r8a66597->timeout_map &= ~(1 << pipenum);
  1488. continue;
  1489. }
  1490. if (td->urb->actual_length) {
  1491. set_td_timer(r8a66597, td);
  1492. break;
  1493. }
  1494. pipe = td->pipe;
  1495. pipe_stop(r8a66597, pipe);
  1496. new_td = td;
  1497. do {
  1498. list_move_tail(&new_td->queue,
  1499. &r8a66597->pipe_queue[pipenum]);
  1500. new_td = r8a66597_get_td(r8a66597, pipenum);
  1501. if (!new_td) {
  1502. new_td = td;
  1503. break;
  1504. }
  1505. } while (td != new_td && td->address == new_td->address);
  1506. start_transfer(r8a66597, new_td);
  1507. if (td == new_td)
  1508. r8a66597->timeout_map &= ~(1 << pipenum);
  1509. else
  1510. set_td_timer(r8a66597, new_td);
  1511. break;
  1512. }
  1513. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1514. }
  1515. static void r8a66597_timer(unsigned long _r8a66597)
  1516. {
  1517. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1518. unsigned long flags;
  1519. int port;
  1520. spin_lock_irqsave(&r8a66597->lock, flags);
  1521. for (port = 0; port < r8a66597->max_root_hub; port++)
  1522. r8a66597_root_hub_control(r8a66597, port);
  1523. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1524. }
  1525. static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  1526. {
  1527. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  1528. if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
  1529. (urb->dev->state == USB_STATE_CONFIGURED))
  1530. return 1;
  1531. else
  1532. return 0;
  1533. }
  1534. static int r8a66597_start(struct usb_hcd *hcd)
  1535. {
  1536. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1537. hcd->state = HC_STATE_RUNNING;
  1538. return enable_controller(r8a66597);
  1539. }
  1540. static void r8a66597_stop(struct usb_hcd *hcd)
  1541. {
  1542. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1543. disable_controller(r8a66597);
  1544. }
  1545. static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
  1546. {
  1547. unsigned int usb_address = usb_pipedevice(urb->pipe);
  1548. u16 root_port, hub_port;
  1549. if (usb_address == 0) {
  1550. get_port_number(r8a66597, urb->dev->devpath,
  1551. &root_port, &hub_port);
  1552. set_devadd_reg(r8a66597, 0,
  1553. get_r8a66597_usb_speed(urb->dev->speed),
  1554. get_parent_r8a66597_address(r8a66597, urb->dev),
  1555. hub_port, root_port);
  1556. }
  1557. }
  1558. static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
  1559. struct urb *urb,
  1560. struct usb_host_endpoint *hep)
  1561. {
  1562. struct r8a66597_td *td;
  1563. u16 pipenum;
  1564. td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
  1565. if (td == NULL)
  1566. return NULL;
  1567. pipenum = r8a66597_get_pipenum(urb, hep);
  1568. td->pipenum = pipenum;
  1569. td->pipe = hep->hcpriv;
  1570. td->urb = urb;
  1571. td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
  1572. td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
  1573. !usb_pipein(urb->pipe));
  1574. if (usb_pipecontrol(urb->pipe))
  1575. td->type = USB_PID_SETUP;
  1576. else if (usb_pipein(urb->pipe))
  1577. td->type = USB_PID_IN;
  1578. else
  1579. td->type = USB_PID_OUT;
  1580. INIT_LIST_HEAD(&td->queue);
  1581. return td;
  1582. }
  1583. static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
  1584. struct urb *urb,
  1585. gfp_t mem_flags)
  1586. {
  1587. struct usb_host_endpoint *hep = urb->ep;
  1588. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1589. struct r8a66597_td *td = NULL;
  1590. int ret, request = 0;
  1591. unsigned long flags;
  1592. spin_lock_irqsave(&r8a66597->lock, flags);
  1593. if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
  1594. ret = -ENODEV;
  1595. goto error_not_linked;
  1596. }
  1597. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1598. if (ret)
  1599. goto error_not_linked;
  1600. if (!hep->hcpriv) {
  1601. hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
  1602. GFP_ATOMIC);
  1603. if (!hep->hcpriv) {
  1604. ret = -ENOMEM;
  1605. goto error;
  1606. }
  1607. set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
  1608. if (usb_pipeendpoint(urb->pipe))
  1609. init_pipe_info(r8a66597, urb, hep, &hep->desc);
  1610. }
  1611. if (unlikely(check_pipe_config(r8a66597, urb)))
  1612. init_pipe_config(r8a66597, urb);
  1613. set_address_zero(r8a66597, urb);
  1614. td = r8a66597_make_td(r8a66597, urb, hep);
  1615. if (td == NULL) {
  1616. ret = -ENOMEM;
  1617. goto error;
  1618. }
  1619. if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
  1620. request = 1;
  1621. list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
  1622. urb->hcpriv = td;
  1623. if (request) {
  1624. if (td->pipe->info.timer_interval) {
  1625. r8a66597->interval_map |= 1 << td->pipenum;
  1626. mod_timer(&r8a66597->interval_timer[td->pipenum],
  1627. jiffies + msecs_to_jiffies(
  1628. td->pipe->info.timer_interval));
  1629. } else {
  1630. ret = start_transfer(r8a66597, td);
  1631. if (ret < 0) {
  1632. list_del(&td->queue);
  1633. kfree(td);
  1634. }
  1635. }
  1636. } else
  1637. set_td_timer(r8a66597, td);
  1638. error:
  1639. if (ret)
  1640. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1641. error_not_linked:
  1642. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1643. return ret;
  1644. }
  1645. static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1646. int status)
  1647. {
  1648. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1649. struct r8a66597_td *td;
  1650. unsigned long flags;
  1651. int rc;
  1652. spin_lock_irqsave(&r8a66597->lock, flags);
  1653. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1654. if (rc)
  1655. goto done;
  1656. if (urb->hcpriv) {
  1657. td = urb->hcpriv;
  1658. pipe_stop(r8a66597, td->pipe);
  1659. pipe_irq_disable(r8a66597, td->pipenum);
  1660. disable_irq_empty(r8a66597, td->pipenum);
  1661. finish_request(r8a66597, td, td->pipenum, urb, status);
  1662. }
  1663. done:
  1664. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1665. return rc;
  1666. }
  1667. static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
  1668. struct usb_host_endpoint *hep)
  1669. {
  1670. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1671. struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
  1672. struct r8a66597_td *td;
  1673. struct urb *urb = NULL;
  1674. u16 pipenum;
  1675. unsigned long flags;
  1676. if (pipe == NULL)
  1677. return;
  1678. pipenum = pipe->info.pipenum;
  1679. if (pipenum == 0) {
  1680. kfree(hep->hcpriv);
  1681. hep->hcpriv = NULL;
  1682. return;
  1683. }
  1684. spin_lock_irqsave(&r8a66597->lock, flags);
  1685. pipe_stop(r8a66597, pipe);
  1686. pipe_irq_disable(r8a66597, pipenum);
  1687. disable_irq_empty(r8a66597, pipenum);
  1688. td = r8a66597_get_td(r8a66597, pipenum);
  1689. if (td)
  1690. urb = td->urb;
  1691. finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
  1692. kfree(hep->hcpriv);
  1693. hep->hcpriv = NULL;
  1694. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1695. }
  1696. static int r8a66597_get_frame(struct usb_hcd *hcd)
  1697. {
  1698. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1699. return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
  1700. }
  1701. static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
  1702. {
  1703. int chix;
  1704. if (udev->state == USB_STATE_CONFIGURED &&
  1705. udev->parent && udev->parent->devnum > 1 &&
  1706. udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
  1707. map[udev->devnum/32] |= (1 << (udev->devnum % 32));
  1708. for (chix = 0; chix < udev->maxchild; chix++) {
  1709. struct usb_device *childdev = udev->children[chix];
  1710. if (childdev)
  1711. collect_usb_address_map(childdev, map);
  1712. }
  1713. }
  1714. /* this function must be called with interrupt disabled */
  1715. static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
  1716. int addr)
  1717. {
  1718. struct r8a66597_device *dev;
  1719. struct list_head *list = &r8a66597->child_device;
  1720. list_for_each_entry(dev, list, device_list) {
  1721. if (dev->usb_address != addr)
  1722. continue;
  1723. return dev;
  1724. }
  1725. printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
  1726. return NULL;
  1727. }
  1728. static void update_usb_address_map(struct r8a66597 *r8a66597,
  1729. struct usb_device *root_hub,
  1730. unsigned long *map)
  1731. {
  1732. int i, j, addr;
  1733. unsigned long diff;
  1734. unsigned long flags;
  1735. for (i = 0; i < 4; i++) {
  1736. diff = r8a66597->child_connect_map[i] ^ map[i];
  1737. if (!diff)
  1738. continue;
  1739. for (j = 0; j < 32; j++) {
  1740. if (!(diff & (1 << j)))
  1741. continue;
  1742. addr = i * 32 + j;
  1743. if (map[i] & (1 << j))
  1744. set_child_connect_map(r8a66597, addr);
  1745. else {
  1746. struct r8a66597_device *dev;
  1747. spin_lock_irqsave(&r8a66597->lock, flags);
  1748. dev = get_r8a66597_device(r8a66597, addr);
  1749. disable_r8a66597_pipe_all(r8a66597, dev);
  1750. free_usb_address(r8a66597, dev);
  1751. put_child_connect_map(r8a66597, addr);
  1752. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1753. }
  1754. }
  1755. }
  1756. }
  1757. static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
  1758. struct usb_hcd *hcd)
  1759. {
  1760. struct usb_bus *bus;
  1761. unsigned long now_map[4];
  1762. memset(now_map, 0, sizeof(now_map));
  1763. list_for_each_entry(bus, &usb_bus_list, bus_list) {
  1764. if (!bus->root_hub)
  1765. continue;
  1766. if (bus->busnum != hcd->self.busnum)
  1767. continue;
  1768. collect_usb_address_map(bus->root_hub, now_map);
  1769. update_usb_address_map(r8a66597, bus->root_hub, now_map);
  1770. }
  1771. }
  1772. static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
  1773. {
  1774. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1775. unsigned long flags;
  1776. int i;
  1777. r8a66597_check_detect_child(r8a66597, hcd);
  1778. spin_lock_irqsave(&r8a66597->lock, flags);
  1779. *buf = 0; /* initialize (no change) */
  1780. for (i = 0; i < r8a66597->max_root_hub; i++) {
  1781. if (r8a66597->root_hub[i].port & 0xffff0000)
  1782. *buf |= 1 << (i + 1);
  1783. }
  1784. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1785. return (*buf != 0);
  1786. }
  1787. static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
  1788. struct usb_hub_descriptor *desc)
  1789. {
  1790. desc->bDescriptorType = 0x29;
  1791. desc->bHubContrCurrent = 0;
  1792. desc->bNbrPorts = r8a66597->max_root_hub;
  1793. desc->bDescLength = 9;
  1794. desc->bPwrOn2PwrGood = 0;
  1795. desc->wHubCharacteristics = cpu_to_le16(0x0011);
  1796. desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
  1797. desc->bitmap[1] = ~0;
  1798. }
  1799. static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1800. u16 wIndex, char *buf, u16 wLength)
  1801. {
  1802. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1803. int ret;
  1804. int port = (wIndex & 0x00FF) - 1;
  1805. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1806. unsigned long flags;
  1807. ret = 0;
  1808. spin_lock_irqsave(&r8a66597->lock, flags);
  1809. switch (typeReq) {
  1810. case ClearHubFeature:
  1811. case SetHubFeature:
  1812. switch (wValue) {
  1813. case C_HUB_OVER_CURRENT:
  1814. case C_HUB_LOCAL_POWER:
  1815. break;
  1816. default:
  1817. goto error;
  1818. }
  1819. break;
  1820. case ClearPortFeature:
  1821. if (wIndex > r8a66597->max_root_hub)
  1822. goto error;
  1823. if (wLength != 0)
  1824. goto error;
  1825. switch (wValue) {
  1826. case USB_PORT_FEAT_ENABLE:
  1827. rh->port &= ~(1 << USB_PORT_FEAT_POWER);
  1828. break;
  1829. case USB_PORT_FEAT_SUSPEND:
  1830. break;
  1831. case USB_PORT_FEAT_POWER:
  1832. r8a66597_port_power(r8a66597, port, 0);
  1833. break;
  1834. case USB_PORT_FEAT_C_ENABLE:
  1835. case USB_PORT_FEAT_C_SUSPEND:
  1836. case USB_PORT_FEAT_C_CONNECTION:
  1837. case USB_PORT_FEAT_C_OVER_CURRENT:
  1838. case USB_PORT_FEAT_C_RESET:
  1839. break;
  1840. default:
  1841. goto error;
  1842. }
  1843. rh->port &= ~(1 << wValue);
  1844. break;
  1845. case GetHubDescriptor:
  1846. r8a66597_hub_descriptor(r8a66597,
  1847. (struct usb_hub_descriptor *)buf);
  1848. break;
  1849. case GetHubStatus:
  1850. *buf = 0x00;
  1851. break;
  1852. case GetPortStatus:
  1853. if (wIndex > r8a66597->max_root_hub)
  1854. goto error;
  1855. *(__le32 *)buf = cpu_to_le32(rh->port);
  1856. break;
  1857. case SetPortFeature:
  1858. if (wIndex > r8a66597->max_root_hub)
  1859. goto error;
  1860. if (wLength != 0)
  1861. goto error;
  1862. switch (wValue) {
  1863. case USB_PORT_FEAT_SUSPEND:
  1864. break;
  1865. case USB_PORT_FEAT_POWER:
  1866. r8a66597_port_power(r8a66597, port, 1);
  1867. rh->port |= (1 << USB_PORT_FEAT_POWER);
  1868. break;
  1869. case USB_PORT_FEAT_RESET: {
  1870. struct r8a66597_device *dev = rh->dev;
  1871. rh->port |= (1 << USB_PORT_FEAT_RESET);
  1872. disable_r8a66597_pipe_all(r8a66597, dev);
  1873. free_usb_address(r8a66597, dev);
  1874. r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
  1875. get_dvstctr_reg(port));
  1876. mod_timer(&r8a66597->rh_timer,
  1877. jiffies + msecs_to_jiffies(50));
  1878. }
  1879. break;
  1880. default:
  1881. goto error;
  1882. }
  1883. rh->port |= 1 << wValue;
  1884. break;
  1885. default:
  1886. error:
  1887. ret = -EPIPE;
  1888. break;
  1889. }
  1890. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1891. return ret;
  1892. }
  1893. #if defined(CONFIG_PM)
  1894. static int r8a66597_bus_suspend(struct usb_hcd *hcd)
  1895. {
  1896. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1897. int port;
  1898. dbg("%s", __func__);
  1899. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1900. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1901. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1902. if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
  1903. continue;
  1904. dbg("suspend port = %d", port);
  1905. r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
  1906. rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
  1907. if (rh->dev->udev->do_remote_wakeup) {
  1908. msleep(3); /* waiting last SOF */
  1909. r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
  1910. r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
  1911. r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
  1912. }
  1913. }
  1914. r8a66597->bus_suspended = 1;
  1915. return 0;
  1916. }
  1917. static int r8a66597_bus_resume(struct usb_hcd *hcd)
  1918. {
  1919. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1920. int port;
  1921. dbg("%s", __func__);
  1922. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1923. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1924. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1925. if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
  1926. continue;
  1927. dbg("resume port = %d", port);
  1928. rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
  1929. rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
  1930. r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
  1931. msleep(50);
  1932. r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
  1933. }
  1934. return 0;
  1935. }
  1936. #else
  1937. #define r8a66597_bus_suspend NULL
  1938. #define r8a66597_bus_resume NULL
  1939. #endif
  1940. static struct hc_driver r8a66597_hc_driver = {
  1941. .description = hcd_name,
  1942. .hcd_priv_size = sizeof(struct r8a66597),
  1943. .irq = r8a66597_irq,
  1944. /*
  1945. * generic hardware linkage
  1946. */
  1947. .flags = HCD_USB2,
  1948. .start = r8a66597_start,
  1949. .stop = r8a66597_stop,
  1950. /*
  1951. * managing i/o requests and associated device resources
  1952. */
  1953. .urb_enqueue = r8a66597_urb_enqueue,
  1954. .urb_dequeue = r8a66597_urb_dequeue,
  1955. .endpoint_disable = r8a66597_endpoint_disable,
  1956. /*
  1957. * periodic schedule support
  1958. */
  1959. .get_frame_number = r8a66597_get_frame,
  1960. /*
  1961. * root hub support
  1962. */
  1963. .hub_status_data = r8a66597_hub_status_data,
  1964. .hub_control = r8a66597_hub_control,
  1965. .bus_suspend = r8a66597_bus_suspend,
  1966. .bus_resume = r8a66597_bus_resume,
  1967. };
  1968. #if defined(CONFIG_PM)
  1969. static int r8a66597_suspend(struct device *dev)
  1970. {
  1971. struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
  1972. int port;
  1973. dbg("%s", __func__);
  1974. disable_controller(r8a66597);
  1975. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1976. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1977. rh->port = 0x00000000;
  1978. }
  1979. return 0;
  1980. }
  1981. static int r8a66597_resume(struct device *dev)
  1982. {
  1983. struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
  1984. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1985. dbg("%s", __func__);
  1986. enable_controller(r8a66597);
  1987. usb_root_hub_lost_power(hcd->self.root_hub);
  1988. return 0;
  1989. }
  1990. static const struct dev_pm_ops r8a66597_dev_pm_ops = {
  1991. .suspend = r8a66597_suspend,
  1992. .resume = r8a66597_resume,
  1993. .poweroff = r8a66597_suspend,
  1994. .restore = r8a66597_resume,
  1995. };
  1996. #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
  1997. #else /* if defined(CONFIG_PM) */
  1998. #define R8A66597_DEV_PM_OPS NULL
  1999. #endif
  2000. static int __init_or_module r8a66597_remove(struct platform_device *pdev)
  2001. {
  2002. struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
  2003. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  2004. del_timer_sync(&r8a66597->rh_timer);
  2005. usb_remove_hcd(hcd);
  2006. iounmap((void *)r8a66597->reg);
  2007. #ifdef CONFIG_HAVE_CLK
  2008. if (r8a66597->pdata->on_chip)
  2009. clk_put(r8a66597->clk);
  2010. #endif
  2011. usb_put_hcd(hcd);
  2012. return 0;
  2013. }
  2014. static int __devinit r8a66597_probe(struct platform_device *pdev)
  2015. {
  2016. #ifdef CONFIG_HAVE_CLK
  2017. char clk_name[8];
  2018. #endif
  2019. struct resource *res = NULL, *ires;
  2020. int irq = -1;
  2021. void __iomem *reg = NULL;
  2022. struct usb_hcd *hcd = NULL;
  2023. struct r8a66597 *r8a66597;
  2024. int ret = 0;
  2025. int i;
  2026. unsigned long irq_trigger;
  2027. if (pdev->dev.dma_mask) {
  2028. ret = -EINVAL;
  2029. dev_err(&pdev->dev, "dma not supported\n");
  2030. goto clean_up;
  2031. }
  2032. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2033. if (!res) {
  2034. ret = -ENODEV;
  2035. dev_err(&pdev->dev, "platform_get_resource error.\n");
  2036. goto clean_up;
  2037. }
  2038. ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2039. if (!ires) {
  2040. ret = -ENODEV;
  2041. dev_err(&pdev->dev,
  2042. "platform_get_resource IORESOURCE_IRQ error.\n");
  2043. goto clean_up;
  2044. }
  2045. irq = ires->start;
  2046. irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
  2047. reg = ioremap(res->start, resource_size(res));
  2048. if (reg == NULL) {
  2049. ret = -ENOMEM;
  2050. dev_err(&pdev->dev, "ioremap error.\n");
  2051. goto clean_up;
  2052. }
  2053. if (pdev->dev.platform_data == NULL) {
  2054. dev_err(&pdev->dev, "no platform data\n");
  2055. ret = -ENODEV;
  2056. goto clean_up;
  2057. }
  2058. /* initialize hcd */
  2059. hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
  2060. if (!hcd) {
  2061. ret = -ENOMEM;
  2062. dev_err(&pdev->dev, "Failed to create hcd\n");
  2063. goto clean_up;
  2064. }
  2065. r8a66597 = hcd_to_r8a66597(hcd);
  2066. memset(r8a66597, 0, sizeof(struct r8a66597));
  2067. dev_set_drvdata(&pdev->dev, r8a66597);
  2068. r8a66597->pdata = pdev->dev.platform_data;
  2069. r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
  2070. if (r8a66597->pdata->on_chip) {
  2071. #ifdef CONFIG_HAVE_CLK
  2072. snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
  2073. r8a66597->clk = clk_get(&pdev->dev, clk_name);
  2074. if (IS_ERR(r8a66597->clk)) {
  2075. dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
  2076. clk_name);
  2077. ret = PTR_ERR(r8a66597->clk);
  2078. goto clean_up2;
  2079. }
  2080. #endif
  2081. r8a66597->max_root_hub = 1;
  2082. } else
  2083. r8a66597->max_root_hub = 2;
  2084. spin_lock_init(&r8a66597->lock);
  2085. init_timer(&r8a66597->rh_timer);
  2086. r8a66597->rh_timer.function = r8a66597_timer;
  2087. r8a66597->rh_timer.data = (unsigned long)r8a66597;
  2088. r8a66597->reg = (unsigned long)reg;
  2089. /* make sure no interrupts are pending */
  2090. ret = r8a66597_clock_enable(r8a66597);
  2091. if (ret < 0)
  2092. goto clean_up3;
  2093. disable_controller(r8a66597);
  2094. for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
  2095. INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
  2096. init_timer(&r8a66597->td_timer[i]);
  2097. r8a66597->td_timer[i].function = r8a66597_td_timer;
  2098. r8a66597->td_timer[i].data = (unsigned long)r8a66597;
  2099. setup_timer(&r8a66597->interval_timer[i],
  2100. r8a66597_interval_timer,
  2101. (unsigned long)r8a66597);
  2102. }
  2103. INIT_LIST_HEAD(&r8a66597->child_device);
  2104. hcd->rsrc_start = res->start;
  2105. ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
  2106. if (ret != 0) {
  2107. dev_err(&pdev->dev, "Failed to add hcd\n");
  2108. goto clean_up3;
  2109. }
  2110. return 0;
  2111. clean_up3:
  2112. #ifdef CONFIG_HAVE_CLK
  2113. if (r8a66597->pdata->on_chip)
  2114. clk_put(r8a66597->clk);
  2115. clean_up2:
  2116. #endif
  2117. usb_put_hcd(hcd);
  2118. clean_up:
  2119. if (reg)
  2120. iounmap(reg);
  2121. return ret;
  2122. }
  2123. static struct platform_driver r8a66597_driver = {
  2124. .probe = r8a66597_probe,
  2125. .remove = r8a66597_remove,
  2126. .driver = {
  2127. .name = (char *) hcd_name,
  2128. .owner = THIS_MODULE,
  2129. .pm = R8A66597_DEV_PM_OPS,
  2130. },
  2131. };
  2132. static int __init r8a66597_init(void)
  2133. {
  2134. if (usb_disabled())
  2135. return -ENODEV;
  2136. printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
  2137. DRIVER_VERSION);
  2138. return platform_driver_register(&r8a66597_driver);
  2139. }
  2140. module_init(r8a66597_init);
  2141. static void __exit r8a66597_cleanup(void)
  2142. {
  2143. platform_driver_unregister(&r8a66597_driver);
  2144. }
  2145. module_exit(r8a66597_cleanup);