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