r8a66597-hcd.c 63 KB

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