r8a66597-hcd.c 62 KB

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