r8a66597-hcd.c 63 KB

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