r8a66597-hcd.c 62 KB

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