r8a66597-hcd.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  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)
  685. continue;
  686. if (td->address != address)
  687. continue;
  688. urb = td->urb;
  689. list_del(&td->queue);
  690. kfree(td);
  691. if (urb) {
  692. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
  693. urb);
  694. spin_unlock(&r8a66597->lock);
  695. usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
  696. -ENODEV);
  697. spin_lock(&r8a66597->lock);
  698. }
  699. break;
  700. }
  701. }
  702. /* this function must be called with interrupt disabled */
  703. static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
  704. struct r8a66597_device *dev)
  705. {
  706. int check_ep0 = 0;
  707. u16 pipenum;
  708. if (!dev)
  709. return;
  710. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  711. if (!dev->pipe_cnt[pipenum])
  712. continue;
  713. if (!check_ep0) {
  714. check_ep0 = 1;
  715. force_dequeue(r8a66597, 0, dev->address);
  716. }
  717. r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
  718. dev->pipe_cnt[pipenum] = 0;
  719. force_dequeue(r8a66597, pipenum, dev->address);
  720. }
  721. dbg("disable_pipe");
  722. r8a66597->dma_map &= ~(dev->dma_map);
  723. dev->dma_map = 0;
  724. }
  725. static u16 get_interval(struct urb *urb, __u8 interval)
  726. {
  727. u16 time = 1;
  728. int i;
  729. if (urb->dev->speed == USB_SPEED_HIGH) {
  730. if (interval > IITV)
  731. time = IITV;
  732. else
  733. time = interval ? interval - 1 : 0;
  734. } else {
  735. if (interval > 128) {
  736. time = IITV;
  737. } else {
  738. /* calculate the nearest value for PIPEPERI */
  739. for (i = 0; i < 7; i++) {
  740. if ((1 << i) < interval &&
  741. (1 << (i + 1) > interval))
  742. time = 1 << i;
  743. }
  744. }
  745. }
  746. return time;
  747. }
  748. static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
  749. {
  750. __u8 i;
  751. unsigned long time = 1;
  752. if (usb_pipeisoc(urb->pipe))
  753. return 0;
  754. if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
  755. for (i = 0; i < (interval - 1); i++)
  756. time *= 2;
  757. time = time * 125 / 1000; /* uSOF -> msec */
  758. } else {
  759. time = interval;
  760. }
  761. return time;
  762. }
  763. /* this function must be called with interrupt disabled */
  764. static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
  765. struct usb_host_endpoint *hep,
  766. struct usb_endpoint_descriptor *ep)
  767. {
  768. struct r8a66597_pipe_info info;
  769. info.pipenum = get_empty_pipenum(r8a66597, ep);
  770. info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
  771. info.epnum = usb_endpoint_num(ep);
  772. info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
  773. info.type = get_r8a66597_type(usb_endpoint_type(ep));
  774. info.bufnum = get_bufnum(info.pipenum);
  775. info.buf_bsize = get_buf_bsize(info.pipenum);
  776. if (info.type == R8A66597_BULK) {
  777. info.interval = 0;
  778. info.timer_interval = 0;
  779. } else {
  780. info.interval = get_interval(urb, ep->bInterval);
  781. info.timer_interval = get_timer_interval(urb, ep->bInterval);
  782. }
  783. if (usb_endpoint_dir_in(ep))
  784. info.dir_in = 1;
  785. else
  786. info.dir_in = 0;
  787. enable_r8a66597_pipe(r8a66597, urb, hep, &info);
  788. }
  789. static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  790. {
  791. struct r8a66597_device *dev;
  792. dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  793. dev->state = USB_STATE_CONFIGURED;
  794. }
  795. static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
  796. u16 pipenum)
  797. {
  798. if (pipenum == 0 && usb_pipeout(urb->pipe))
  799. enable_irq_empty(r8a66597, pipenum);
  800. else
  801. enable_irq_ready(r8a66597, pipenum);
  802. if (!usb_pipeisoc(urb->pipe))
  803. enable_irq_nrdy(r8a66597, pipenum);
  804. }
  805. static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
  806. {
  807. disable_irq_ready(r8a66597, pipenum);
  808. disable_irq_nrdy(r8a66597, pipenum);
  809. }
  810. static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
  811. {
  812. mod_timer(&r8a66597->rh_timer,
  813. jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
  814. }
  815. static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
  816. int connect)
  817. {
  818. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  819. rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  820. rh->scount = R8A66597_MAX_SAMPLING;
  821. if (connect)
  822. rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
  823. else
  824. rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
  825. rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
  826. r8a66597_root_hub_start_polling(r8a66597);
  827. }
  828. /* this function must be called with interrupt disabled */
  829. static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
  830. u16 syssts)
  831. {
  832. if (syssts == SE0) {
  833. r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
  834. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  835. return;
  836. }
  837. if (syssts == FS_JSTS)
  838. r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
  839. else if (syssts == LS_JSTS)
  840. r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
  841. r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
  842. r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
  843. if (r8a66597->bus_suspended)
  844. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  845. }
  846. /* this function must be called with interrupt disabled */
  847. static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
  848. {
  849. u16 speed = get_rh_usb_speed(r8a66597, port);
  850. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  851. if (speed == HSMODE)
  852. rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
  853. else if (speed == LSMODE)
  854. rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
  855. rh->port &= ~(1 << USB_PORT_FEAT_RESET);
  856. rh->port |= 1 << USB_PORT_FEAT_ENABLE;
  857. }
  858. /* this function must be called with interrupt disabled */
  859. static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
  860. {
  861. struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
  862. disable_r8a66597_pipe_all(r8a66597, dev);
  863. free_usb_address(r8a66597, dev);
  864. start_root_hub_sampling(r8a66597, port, 0);
  865. }
  866. /* this function must be called with interrupt disabled */
  867. static void prepare_setup_packet(struct r8a66597 *r8a66597,
  868. struct r8a66597_td *td)
  869. {
  870. int i;
  871. __le16 *p = (__le16 *)td->urb->setup_packet;
  872. unsigned long setup_addr = USBREQ;
  873. r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
  874. DCPMAXP);
  875. r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
  876. for (i = 0; i < 4; i++) {
  877. r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
  878. setup_addr += 2;
  879. }
  880. r8a66597_write(r8a66597, SUREQ, DCPCTR);
  881. }
  882. /* this function must be called with interrupt disabled */
  883. static void prepare_packet_read(struct r8a66597 *r8a66597,
  884. struct r8a66597_td *td)
  885. {
  886. struct urb *urb = td->urb;
  887. if (usb_pipecontrol(urb->pipe)) {
  888. r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
  889. r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
  890. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  891. if (urb->actual_length == 0) {
  892. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  893. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  894. }
  895. pipe_irq_disable(r8a66597, td->pipenum);
  896. pipe_start(r8a66597, td->pipe);
  897. pipe_irq_enable(r8a66597, urb, td->pipenum);
  898. } else {
  899. if (urb->actual_length == 0) {
  900. pipe_irq_disable(r8a66597, td->pipenum);
  901. pipe_setting(r8a66597, td);
  902. pipe_stop(r8a66597, td->pipe);
  903. r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
  904. if (td->pipe->pipetre) {
  905. r8a66597_write(r8a66597, TRCLR,
  906. td->pipe->pipetre);
  907. r8a66597_write(r8a66597,
  908. DIV_ROUND_UP
  909. (urb->transfer_buffer_length,
  910. td->maxpacket),
  911. td->pipe->pipetrn);
  912. r8a66597_bset(r8a66597, TRENB,
  913. td->pipe->pipetre);
  914. }
  915. pipe_start(r8a66597, td->pipe);
  916. pipe_irq_enable(r8a66597, urb, td->pipenum);
  917. }
  918. }
  919. }
  920. /* this function must be called with interrupt disabled */
  921. static void prepare_packet_write(struct r8a66597 *r8a66597,
  922. struct r8a66597_td *td)
  923. {
  924. u16 tmp;
  925. struct urb *urb = td->urb;
  926. if (usb_pipecontrol(urb->pipe)) {
  927. pipe_stop(r8a66597, td->pipe);
  928. r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
  929. r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
  930. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  931. if (urb->actual_length == 0) {
  932. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  933. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  934. }
  935. } else {
  936. if (urb->actual_length == 0)
  937. pipe_setting(r8a66597, td);
  938. if (td->pipe->pipetre)
  939. r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
  940. }
  941. r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
  942. fifo_change_from_pipe(r8a66597, td->pipe);
  943. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  944. if (unlikely((tmp & FRDY) == 0))
  945. pipe_irq_enable(r8a66597, urb, td->pipenum);
  946. else
  947. packet_write(r8a66597, td->pipenum);
  948. pipe_start(r8a66597, td->pipe);
  949. }
  950. /* this function must be called with interrupt disabled */
  951. static void prepare_status_packet(struct r8a66597 *r8a66597,
  952. struct r8a66597_td *td)
  953. {
  954. struct urb *urb = td->urb;
  955. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  956. pipe_stop(r8a66597, td->pipe);
  957. if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
  958. r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
  959. r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
  960. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  961. r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
  962. r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
  963. enable_irq_empty(r8a66597, 0);
  964. } else {
  965. r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
  966. r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
  967. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  968. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  969. enable_irq_ready(r8a66597, 0);
  970. }
  971. enable_irq_nrdy(r8a66597, 0);
  972. pipe_start(r8a66597, td->pipe);
  973. }
  974. static int is_set_address(unsigned char *setup_packet)
  975. {
  976. if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
  977. setup_packet[1] == USB_REQ_SET_ADDRESS)
  978. return 1;
  979. else
  980. return 0;
  981. }
  982. /* this function must be called with interrupt disabled */
  983. static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  984. {
  985. BUG_ON(!td);
  986. switch (td->type) {
  987. case USB_PID_SETUP:
  988. if (is_set_address(td->urb->setup_packet)) {
  989. td->set_address = 1;
  990. td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
  991. td->urb);
  992. if (td->urb->setup_packet[2] == 0)
  993. return -EPIPE;
  994. }
  995. prepare_setup_packet(r8a66597, td);
  996. break;
  997. case USB_PID_IN:
  998. prepare_packet_read(r8a66597, td);
  999. break;
  1000. case USB_PID_OUT:
  1001. prepare_packet_write(r8a66597, td);
  1002. break;
  1003. case USB_PID_ACK:
  1004. prepare_status_packet(r8a66597, td);
  1005. break;
  1006. default:
  1007. printk(KERN_ERR "r8a66597: invalid type.\n");
  1008. break;
  1009. }
  1010. return 0;
  1011. }
  1012. static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
  1013. {
  1014. if (usb_pipeisoc(urb->pipe)) {
  1015. if (urb->number_of_packets == td->iso_cnt)
  1016. return 1;
  1017. }
  1018. /* control or bulk or interrupt */
  1019. if ((urb->transfer_buffer_length <= urb->actual_length) ||
  1020. (td->short_packet) || (td->zero_packet))
  1021. return 1;
  1022. return 0;
  1023. }
  1024. /* this function must be called with interrupt disabled */
  1025. static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  1026. {
  1027. unsigned long time;
  1028. BUG_ON(!td);
  1029. if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
  1030. !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
  1031. r8a66597->timeout_map |= 1 << td->pipenum;
  1032. switch (usb_pipetype(td->urb->pipe)) {
  1033. case PIPE_INTERRUPT:
  1034. case PIPE_ISOCHRONOUS:
  1035. time = 30;
  1036. break;
  1037. default:
  1038. time = 300;
  1039. break;
  1040. }
  1041. mod_timer(&r8a66597->td_timer[td->pipenum],
  1042. jiffies + msecs_to_jiffies(time));
  1043. }
  1044. }
  1045. /* this function must be called with interrupt disabled */
  1046. static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
  1047. u16 pipenum, struct urb *urb, int status)
  1048. __releases(r8a66597->lock) __acquires(r8a66597->lock)
  1049. {
  1050. int restart = 0;
  1051. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1052. r8a66597->timeout_map &= ~(1 << pipenum);
  1053. if (likely(td)) {
  1054. if (td->set_address && (status != 0 || urb->unlinked))
  1055. r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
  1056. pipe_toggle_save(r8a66597, td->pipe, urb);
  1057. list_del(&td->queue);
  1058. kfree(td);
  1059. }
  1060. if (!list_empty(&r8a66597->pipe_queue[pipenum]))
  1061. restart = 1;
  1062. if (likely(urb)) {
  1063. if (usb_pipeisoc(urb->pipe))
  1064. urb->start_frame = r8a66597_get_frame(hcd);
  1065. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
  1066. spin_unlock(&r8a66597->lock);
  1067. usb_hcd_giveback_urb(hcd, urb, status);
  1068. spin_lock(&r8a66597->lock);
  1069. }
  1070. if (restart) {
  1071. td = r8a66597_get_td(r8a66597, pipenum);
  1072. if (unlikely(!td))
  1073. return;
  1074. start_transfer(r8a66597, td);
  1075. set_td_timer(r8a66597, td);
  1076. }
  1077. }
  1078. static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
  1079. {
  1080. u16 tmp;
  1081. int rcv_len, bufsize, urb_len, size;
  1082. u16 *buf;
  1083. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1084. struct urb *urb;
  1085. int finish = 0;
  1086. int status = 0;
  1087. if (unlikely(!td))
  1088. return;
  1089. urb = td->urb;
  1090. fifo_change_from_pipe(r8a66597, td->pipe);
  1091. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1092. if (unlikely((tmp & FRDY) == 0)) {
  1093. pipe_stop(r8a66597, td->pipe);
  1094. pipe_irq_disable(r8a66597, pipenum);
  1095. printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
  1096. finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
  1097. return;
  1098. }
  1099. /* prepare parameters */
  1100. rcv_len = tmp & DTLN;
  1101. if (usb_pipeisoc(urb->pipe)) {
  1102. buf = (u16 *)(urb->transfer_buffer +
  1103. urb->iso_frame_desc[td->iso_cnt].offset);
  1104. urb_len = urb->iso_frame_desc[td->iso_cnt].length;
  1105. } else {
  1106. buf = (void *)urb->transfer_buffer + urb->actual_length;
  1107. urb_len = urb->transfer_buffer_length - urb->actual_length;
  1108. }
  1109. bufsize = min(urb_len, (int) td->maxpacket);
  1110. if (rcv_len <= bufsize) {
  1111. size = rcv_len;
  1112. } else {
  1113. size = bufsize;
  1114. status = -EOVERFLOW;
  1115. finish = 1;
  1116. }
  1117. /* update parameters */
  1118. urb->actual_length += size;
  1119. if (rcv_len == 0)
  1120. td->zero_packet = 1;
  1121. if (rcv_len < bufsize) {
  1122. td->short_packet = 1;
  1123. }
  1124. if (usb_pipeisoc(urb->pipe)) {
  1125. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1126. urb->iso_frame_desc[td->iso_cnt].status = status;
  1127. td->iso_cnt++;
  1128. finish = 0;
  1129. }
  1130. /* check transfer finish */
  1131. if (finish || check_transfer_finish(td, urb)) {
  1132. pipe_stop(r8a66597, td->pipe);
  1133. pipe_irq_disable(r8a66597, pipenum);
  1134. finish = 1;
  1135. }
  1136. /* read fifo */
  1137. if (urb->transfer_buffer) {
  1138. if (size == 0)
  1139. r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
  1140. else
  1141. r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
  1142. buf, size);
  1143. }
  1144. if (finish && pipenum != 0)
  1145. finish_request(r8a66597, td, pipenum, urb, status);
  1146. }
  1147. static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
  1148. {
  1149. u16 tmp;
  1150. int bufsize, size;
  1151. u16 *buf;
  1152. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1153. struct urb *urb;
  1154. if (unlikely(!td))
  1155. return;
  1156. urb = td->urb;
  1157. fifo_change_from_pipe(r8a66597, td->pipe);
  1158. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1159. if (unlikely((tmp & FRDY) == 0)) {
  1160. pipe_stop(r8a66597, td->pipe);
  1161. pipe_irq_disable(r8a66597, pipenum);
  1162. printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
  1163. finish_request(r8a66597, td, pipenum, urb, -EPIPE);
  1164. return;
  1165. }
  1166. /* prepare parameters */
  1167. bufsize = td->maxpacket;
  1168. if (usb_pipeisoc(urb->pipe)) {
  1169. buf = (u16 *)(urb->transfer_buffer +
  1170. urb->iso_frame_desc[td->iso_cnt].offset);
  1171. size = min(bufsize,
  1172. (int)urb->iso_frame_desc[td->iso_cnt].length);
  1173. } else {
  1174. buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
  1175. size = min_t(u32, bufsize,
  1176. urb->transfer_buffer_length - urb->actual_length);
  1177. }
  1178. /* write fifo */
  1179. if (pipenum > 0)
  1180. r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
  1181. if (urb->transfer_buffer) {
  1182. r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
  1183. if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
  1184. r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
  1185. }
  1186. /* update parameters */
  1187. urb->actual_length += size;
  1188. if (usb_pipeisoc(urb->pipe)) {
  1189. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1190. urb->iso_frame_desc[td->iso_cnt].status = 0;
  1191. td->iso_cnt++;
  1192. }
  1193. /* check transfer finish */
  1194. if (check_transfer_finish(td, urb)) {
  1195. disable_irq_ready(r8a66597, pipenum);
  1196. enable_irq_empty(r8a66597, pipenum);
  1197. if (!usb_pipeisoc(urb->pipe))
  1198. enable_irq_nrdy(r8a66597, pipenum);
  1199. } else
  1200. pipe_irq_enable(r8a66597, urb, pipenum);
  1201. }
  1202. static void check_next_phase(struct r8a66597 *r8a66597, int status)
  1203. {
  1204. struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
  1205. struct urb *urb;
  1206. u8 finish = 0;
  1207. if (unlikely(!td))
  1208. return;
  1209. urb = td->urb;
  1210. switch (td->type) {
  1211. case USB_PID_IN:
  1212. case USB_PID_OUT:
  1213. if (check_transfer_finish(td, urb))
  1214. td->type = USB_PID_ACK;
  1215. break;
  1216. case USB_PID_SETUP:
  1217. if (urb->transfer_buffer_length == urb->actual_length)
  1218. td->type = USB_PID_ACK;
  1219. else if (usb_pipeout(urb->pipe))
  1220. td->type = USB_PID_OUT;
  1221. else
  1222. td->type = USB_PID_IN;
  1223. break;
  1224. case USB_PID_ACK:
  1225. finish = 1;
  1226. break;
  1227. }
  1228. if (finish || status != 0 || urb->unlinked)
  1229. finish_request(r8a66597, td, 0, urb, status);
  1230. else
  1231. start_transfer(r8a66597, td);
  1232. }
  1233. static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
  1234. {
  1235. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1236. if (td) {
  1237. u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
  1238. if (pid == PID_NAK)
  1239. return -ECONNRESET;
  1240. else
  1241. return -EPIPE;
  1242. }
  1243. return 0;
  1244. }
  1245. static void irq_pipe_ready(struct r8a66597 *r8a66597)
  1246. {
  1247. u16 check;
  1248. u16 pipenum;
  1249. u16 mask;
  1250. struct r8a66597_td *td;
  1251. mask = r8a66597_read(r8a66597, BRDYSTS)
  1252. & r8a66597_read(r8a66597, BRDYENB);
  1253. r8a66597_write(r8a66597, ~mask, BRDYSTS);
  1254. if (mask & BRDY0) {
  1255. td = r8a66597_get_td(r8a66597, 0);
  1256. if (td && td->type == USB_PID_IN)
  1257. packet_read(r8a66597, 0);
  1258. else
  1259. pipe_irq_disable(r8a66597, 0);
  1260. check_next_phase(r8a66597, 0);
  1261. }
  1262. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1263. check = 1 << pipenum;
  1264. if (mask & check) {
  1265. td = r8a66597_get_td(r8a66597, pipenum);
  1266. if (unlikely(!td))
  1267. continue;
  1268. if (td->type == USB_PID_IN)
  1269. packet_read(r8a66597, pipenum);
  1270. else if (td->type == USB_PID_OUT)
  1271. packet_write(r8a66597, pipenum);
  1272. }
  1273. }
  1274. }
  1275. static void irq_pipe_empty(struct r8a66597 *r8a66597)
  1276. {
  1277. u16 tmp;
  1278. u16 check;
  1279. u16 pipenum;
  1280. u16 mask;
  1281. struct r8a66597_td *td;
  1282. mask = r8a66597_read(r8a66597, BEMPSTS)
  1283. & r8a66597_read(r8a66597, BEMPENB);
  1284. r8a66597_write(r8a66597, ~mask, BEMPSTS);
  1285. if (mask & BEMP0) {
  1286. cfifo_change(r8a66597, 0);
  1287. td = r8a66597_get_td(r8a66597, 0);
  1288. if (td && td->type != USB_PID_OUT)
  1289. disable_irq_empty(r8a66597, 0);
  1290. check_next_phase(r8a66597, 0);
  1291. }
  1292. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1293. check = 1 << pipenum;
  1294. if (mask & check) {
  1295. struct r8a66597_td *td;
  1296. td = r8a66597_get_td(r8a66597, pipenum);
  1297. if (unlikely(!td))
  1298. continue;
  1299. tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
  1300. if ((tmp & INBUFM) == 0) {
  1301. disable_irq_empty(r8a66597, pipenum);
  1302. pipe_irq_disable(r8a66597, pipenum);
  1303. finish_request(r8a66597, td, pipenum, td->urb,
  1304. 0);
  1305. }
  1306. }
  1307. }
  1308. }
  1309. static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
  1310. {
  1311. u16 check;
  1312. u16 pipenum;
  1313. u16 mask;
  1314. int status;
  1315. mask = r8a66597_read(r8a66597, NRDYSTS)
  1316. & r8a66597_read(r8a66597, NRDYENB);
  1317. r8a66597_write(r8a66597, ~mask, NRDYSTS);
  1318. if (mask & NRDY0) {
  1319. cfifo_change(r8a66597, 0);
  1320. status = get_urb_error(r8a66597, 0);
  1321. pipe_irq_disable(r8a66597, 0);
  1322. check_next_phase(r8a66597, status);
  1323. }
  1324. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1325. check = 1 << pipenum;
  1326. if (mask & check) {
  1327. struct r8a66597_td *td;
  1328. td = r8a66597_get_td(r8a66597, pipenum);
  1329. if (unlikely(!td))
  1330. continue;
  1331. status = get_urb_error(r8a66597, pipenum);
  1332. pipe_irq_disable(r8a66597, pipenum);
  1333. pipe_stop(r8a66597, td->pipe);
  1334. finish_request(r8a66597, td, pipenum, td->urb, status);
  1335. }
  1336. }
  1337. }
  1338. static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
  1339. {
  1340. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1341. u16 intsts0, intsts1, intsts2;
  1342. u16 intenb0, intenb1, intenb2;
  1343. u16 mask0, mask1, mask2;
  1344. int status;
  1345. spin_lock(&r8a66597->lock);
  1346. intsts0 = r8a66597_read(r8a66597, INTSTS0);
  1347. intsts1 = r8a66597_read(r8a66597, INTSTS1);
  1348. intsts2 = r8a66597_read(r8a66597, INTSTS2);
  1349. intenb0 = r8a66597_read(r8a66597, INTENB0);
  1350. intenb1 = r8a66597_read(r8a66597, INTENB1);
  1351. intenb2 = r8a66597_read(r8a66597, INTENB2);
  1352. mask2 = intsts2 & intenb2;
  1353. mask1 = intsts1 & intenb1;
  1354. mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
  1355. if (mask2) {
  1356. if (mask2 & ATTCH) {
  1357. r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
  1358. r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
  1359. /* start usb bus sampling */
  1360. start_root_hub_sampling(r8a66597, 1, 1);
  1361. }
  1362. if (mask2 & DTCH) {
  1363. r8a66597_write(r8a66597, ~DTCH, INTSTS2);
  1364. r8a66597_bclr(r8a66597, DTCHE, INTENB2);
  1365. r8a66597_usb_disconnect(r8a66597, 1);
  1366. }
  1367. if (mask2 & BCHG) {
  1368. r8a66597_write(r8a66597, ~BCHG, INTSTS2);
  1369. r8a66597_bclr(r8a66597, BCHGE, INTENB2);
  1370. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  1371. }
  1372. }
  1373. if (mask1) {
  1374. if (mask1 & ATTCH) {
  1375. r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
  1376. r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
  1377. /* start usb bus sampling */
  1378. start_root_hub_sampling(r8a66597, 0, 1);
  1379. }
  1380. if (mask1 & DTCH) {
  1381. r8a66597_write(r8a66597, ~DTCH, INTSTS1);
  1382. r8a66597_bclr(r8a66597, DTCHE, INTENB1);
  1383. r8a66597_usb_disconnect(r8a66597, 0);
  1384. }
  1385. if (mask1 & BCHG) {
  1386. r8a66597_write(r8a66597, ~BCHG, INTSTS1);
  1387. r8a66597_bclr(r8a66597, BCHGE, INTENB1);
  1388. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  1389. }
  1390. if (mask1 & SIGN) {
  1391. r8a66597_write(r8a66597, ~SIGN, INTSTS1);
  1392. status = get_urb_error(r8a66597, 0);
  1393. check_next_phase(r8a66597, status);
  1394. }
  1395. if (mask1 & SACK) {
  1396. r8a66597_write(r8a66597, ~SACK, INTSTS1);
  1397. check_next_phase(r8a66597, 0);
  1398. }
  1399. }
  1400. if (mask0) {
  1401. if (mask0 & BRDY)
  1402. irq_pipe_ready(r8a66597);
  1403. if (mask0 & BEMP)
  1404. irq_pipe_empty(r8a66597);
  1405. if (mask0 & NRDY)
  1406. irq_pipe_nrdy(r8a66597);
  1407. }
  1408. spin_unlock(&r8a66597->lock);
  1409. return IRQ_HANDLED;
  1410. }
  1411. /* this function must be called with interrupt disabled */
  1412. static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
  1413. {
  1414. u16 tmp;
  1415. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1416. if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
  1417. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1418. tmp = r8a66597_read(r8a66597, dvstctr_reg);
  1419. if ((tmp & USBRST) == USBRST) {
  1420. r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
  1421. dvstctr_reg);
  1422. r8a66597_root_hub_start_polling(r8a66597);
  1423. } else
  1424. r8a66597_usb_connect(r8a66597, port);
  1425. }
  1426. if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
  1427. r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
  1428. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  1429. }
  1430. if (rh->scount > 0) {
  1431. tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  1432. if (tmp == rh->old_syssts) {
  1433. rh->scount--;
  1434. if (rh->scount == 0)
  1435. r8a66597_check_syssts(r8a66597, port, tmp);
  1436. else
  1437. r8a66597_root_hub_start_polling(r8a66597);
  1438. } else {
  1439. rh->scount = R8A66597_MAX_SAMPLING;
  1440. rh->old_syssts = tmp;
  1441. r8a66597_root_hub_start_polling(r8a66597);
  1442. }
  1443. }
  1444. }
  1445. static void r8a66597_interval_timer(unsigned long _r8a66597)
  1446. {
  1447. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1448. unsigned long flags;
  1449. u16 pipenum;
  1450. struct r8a66597_td *td;
  1451. spin_lock_irqsave(&r8a66597->lock, flags);
  1452. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1453. if (!(r8a66597->interval_map & (1 << pipenum)))
  1454. continue;
  1455. if (timer_pending(&r8a66597->interval_timer[pipenum]))
  1456. continue;
  1457. td = r8a66597_get_td(r8a66597, pipenum);
  1458. if (td)
  1459. start_transfer(r8a66597, td);
  1460. }
  1461. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1462. }
  1463. static void r8a66597_td_timer(unsigned long _r8a66597)
  1464. {
  1465. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1466. unsigned long flags;
  1467. u16 pipenum;
  1468. struct r8a66597_td *td, *new_td = NULL;
  1469. struct r8a66597_pipe *pipe;
  1470. spin_lock_irqsave(&r8a66597->lock, flags);
  1471. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1472. if (!(r8a66597->timeout_map & (1 << pipenum)))
  1473. continue;
  1474. if (timer_pending(&r8a66597->td_timer[pipenum]))
  1475. continue;
  1476. td = r8a66597_get_td(r8a66597, pipenum);
  1477. if (!td) {
  1478. r8a66597->timeout_map &= ~(1 << pipenum);
  1479. continue;
  1480. }
  1481. if (td->urb->actual_length) {
  1482. set_td_timer(r8a66597, td);
  1483. break;
  1484. }
  1485. pipe = td->pipe;
  1486. pipe_stop(r8a66597, pipe);
  1487. new_td = td;
  1488. do {
  1489. list_move_tail(&new_td->queue,
  1490. &r8a66597->pipe_queue[pipenum]);
  1491. new_td = r8a66597_get_td(r8a66597, pipenum);
  1492. if (!new_td) {
  1493. new_td = td;
  1494. break;
  1495. }
  1496. } while (td != new_td && td->address == new_td->address);
  1497. start_transfer(r8a66597, new_td);
  1498. if (td == new_td)
  1499. r8a66597->timeout_map &= ~(1 << pipenum);
  1500. else
  1501. set_td_timer(r8a66597, new_td);
  1502. break;
  1503. }
  1504. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1505. }
  1506. static void r8a66597_timer(unsigned long _r8a66597)
  1507. {
  1508. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1509. unsigned long flags;
  1510. int port;
  1511. spin_lock_irqsave(&r8a66597->lock, flags);
  1512. for (port = 0; port < r8a66597->max_root_hub; port++)
  1513. r8a66597_root_hub_control(r8a66597, port);
  1514. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1515. }
  1516. static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  1517. {
  1518. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  1519. if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
  1520. (urb->dev->state == USB_STATE_CONFIGURED))
  1521. return 1;
  1522. else
  1523. return 0;
  1524. }
  1525. static int r8a66597_start(struct usb_hcd *hcd)
  1526. {
  1527. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1528. hcd->state = HC_STATE_RUNNING;
  1529. return enable_controller(r8a66597);
  1530. }
  1531. static void r8a66597_stop(struct usb_hcd *hcd)
  1532. {
  1533. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1534. disable_controller(r8a66597);
  1535. }
  1536. static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
  1537. {
  1538. unsigned int usb_address = usb_pipedevice(urb->pipe);
  1539. u16 root_port, hub_port;
  1540. if (usb_address == 0) {
  1541. get_port_number(r8a66597, urb->dev->devpath,
  1542. &root_port, &hub_port);
  1543. set_devadd_reg(r8a66597, 0,
  1544. get_r8a66597_usb_speed(urb->dev->speed),
  1545. get_parent_r8a66597_address(r8a66597, urb->dev),
  1546. hub_port, root_port);
  1547. }
  1548. }
  1549. static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
  1550. struct urb *urb,
  1551. struct usb_host_endpoint *hep)
  1552. {
  1553. struct r8a66597_td *td;
  1554. u16 pipenum;
  1555. td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
  1556. if (td == NULL)
  1557. return NULL;
  1558. pipenum = r8a66597_get_pipenum(urb, hep);
  1559. td->pipenum = pipenum;
  1560. td->pipe = hep->hcpriv;
  1561. td->urb = urb;
  1562. td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
  1563. td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
  1564. !usb_pipein(urb->pipe));
  1565. if (usb_pipecontrol(urb->pipe))
  1566. td->type = USB_PID_SETUP;
  1567. else if (usb_pipein(urb->pipe))
  1568. td->type = USB_PID_IN;
  1569. else
  1570. td->type = USB_PID_OUT;
  1571. INIT_LIST_HEAD(&td->queue);
  1572. return td;
  1573. }
  1574. static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
  1575. struct urb *urb,
  1576. gfp_t mem_flags)
  1577. {
  1578. struct usb_host_endpoint *hep = urb->ep;
  1579. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1580. struct r8a66597_td *td = NULL;
  1581. int ret, request = 0;
  1582. unsigned long flags;
  1583. spin_lock_irqsave(&r8a66597->lock, flags);
  1584. if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
  1585. ret = -ENODEV;
  1586. goto error_not_linked;
  1587. }
  1588. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1589. if (ret)
  1590. goto error_not_linked;
  1591. if (!hep->hcpriv) {
  1592. hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
  1593. GFP_ATOMIC);
  1594. if (!hep->hcpriv) {
  1595. ret = -ENOMEM;
  1596. goto error;
  1597. }
  1598. set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
  1599. if (usb_pipeendpoint(urb->pipe))
  1600. init_pipe_info(r8a66597, urb, hep, &hep->desc);
  1601. }
  1602. if (unlikely(check_pipe_config(r8a66597, urb)))
  1603. init_pipe_config(r8a66597, urb);
  1604. set_address_zero(r8a66597, urb);
  1605. td = r8a66597_make_td(r8a66597, urb, hep);
  1606. if (td == NULL) {
  1607. ret = -ENOMEM;
  1608. goto error;
  1609. }
  1610. if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
  1611. request = 1;
  1612. list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
  1613. urb->hcpriv = td;
  1614. if (request) {
  1615. if (td->pipe->info.timer_interval) {
  1616. r8a66597->interval_map |= 1 << td->pipenum;
  1617. mod_timer(&r8a66597->interval_timer[td->pipenum],
  1618. jiffies + msecs_to_jiffies(
  1619. td->pipe->info.timer_interval));
  1620. } else {
  1621. ret = start_transfer(r8a66597, td);
  1622. if (ret < 0) {
  1623. list_del(&td->queue);
  1624. kfree(td);
  1625. }
  1626. }
  1627. } else
  1628. set_td_timer(r8a66597, td);
  1629. error:
  1630. if (ret)
  1631. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1632. error_not_linked:
  1633. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1634. return ret;
  1635. }
  1636. static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1637. int status)
  1638. {
  1639. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1640. struct r8a66597_td *td;
  1641. unsigned long flags;
  1642. int rc;
  1643. spin_lock_irqsave(&r8a66597->lock, flags);
  1644. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1645. if (rc)
  1646. goto done;
  1647. if (urb->hcpriv) {
  1648. td = urb->hcpriv;
  1649. pipe_stop(r8a66597, td->pipe);
  1650. pipe_irq_disable(r8a66597, td->pipenum);
  1651. disable_irq_empty(r8a66597, td->pipenum);
  1652. finish_request(r8a66597, td, td->pipenum, urb, status);
  1653. }
  1654. done:
  1655. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1656. return rc;
  1657. }
  1658. static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
  1659. struct usb_host_endpoint *hep)
  1660. {
  1661. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1662. struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
  1663. struct r8a66597_td *td;
  1664. struct urb *urb = NULL;
  1665. u16 pipenum;
  1666. unsigned long flags;
  1667. if (pipe == NULL)
  1668. return;
  1669. pipenum = pipe->info.pipenum;
  1670. if (pipenum == 0) {
  1671. kfree(hep->hcpriv);
  1672. hep->hcpriv = NULL;
  1673. return;
  1674. }
  1675. spin_lock_irqsave(&r8a66597->lock, flags);
  1676. pipe_stop(r8a66597, pipe);
  1677. pipe_irq_disable(r8a66597, pipenum);
  1678. disable_irq_empty(r8a66597, pipenum);
  1679. td = r8a66597_get_td(r8a66597, pipenum);
  1680. if (td)
  1681. urb = td->urb;
  1682. finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
  1683. kfree(hep->hcpriv);
  1684. hep->hcpriv = NULL;
  1685. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1686. }
  1687. static int r8a66597_get_frame(struct usb_hcd *hcd)
  1688. {
  1689. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1690. return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
  1691. }
  1692. static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
  1693. {
  1694. int chix;
  1695. if (udev->state == USB_STATE_CONFIGURED &&
  1696. udev->parent && udev->parent->devnum > 1 &&
  1697. udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
  1698. map[udev->devnum/32] |= (1 << (udev->devnum % 32));
  1699. for (chix = 0; chix < udev->maxchild; chix++) {
  1700. struct usb_device *childdev = udev->children[chix];
  1701. if (childdev)
  1702. collect_usb_address_map(childdev, map);
  1703. }
  1704. }
  1705. /* this function must be called with interrupt disabled */
  1706. static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
  1707. int addr)
  1708. {
  1709. struct r8a66597_device *dev;
  1710. struct list_head *list = &r8a66597->child_device;
  1711. list_for_each_entry(dev, list, device_list) {
  1712. if (!dev)
  1713. continue;
  1714. if (dev->usb_address != addr)
  1715. continue;
  1716. return dev;
  1717. }
  1718. printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
  1719. return NULL;
  1720. }
  1721. static void update_usb_address_map(struct r8a66597 *r8a66597,
  1722. struct usb_device *root_hub,
  1723. unsigned long *map)
  1724. {
  1725. int i, j, addr;
  1726. unsigned long diff;
  1727. unsigned long flags;
  1728. for (i = 0; i < 4; i++) {
  1729. diff = r8a66597->child_connect_map[i] ^ map[i];
  1730. if (!diff)
  1731. continue;
  1732. for (j = 0; j < 32; j++) {
  1733. if (!(diff & (1 << j)))
  1734. continue;
  1735. addr = i * 32 + j;
  1736. if (map[i] & (1 << j))
  1737. set_child_connect_map(r8a66597, addr);
  1738. else {
  1739. struct r8a66597_device *dev;
  1740. spin_lock_irqsave(&r8a66597->lock, flags);
  1741. dev = get_r8a66597_device(r8a66597, addr);
  1742. disable_r8a66597_pipe_all(r8a66597, dev);
  1743. free_usb_address(r8a66597, dev);
  1744. put_child_connect_map(r8a66597, addr);
  1745. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1746. }
  1747. }
  1748. }
  1749. }
  1750. static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
  1751. struct usb_hcd *hcd)
  1752. {
  1753. struct usb_bus *bus;
  1754. unsigned long now_map[4];
  1755. memset(now_map, 0, sizeof(now_map));
  1756. list_for_each_entry(bus, &usb_bus_list, bus_list) {
  1757. if (!bus->root_hub)
  1758. continue;
  1759. if (bus->busnum != hcd->self.busnum)
  1760. continue;
  1761. collect_usb_address_map(bus->root_hub, now_map);
  1762. update_usb_address_map(r8a66597, bus->root_hub, now_map);
  1763. }
  1764. }
  1765. static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
  1766. {
  1767. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1768. unsigned long flags;
  1769. int i;
  1770. r8a66597_check_detect_child(r8a66597, hcd);
  1771. spin_lock_irqsave(&r8a66597->lock, flags);
  1772. *buf = 0; /* initialize (no change) */
  1773. for (i = 0; i < r8a66597->max_root_hub; i++) {
  1774. if (r8a66597->root_hub[i].port & 0xffff0000)
  1775. *buf |= 1 << (i + 1);
  1776. }
  1777. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1778. return (*buf != 0);
  1779. }
  1780. static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
  1781. struct usb_hub_descriptor *desc)
  1782. {
  1783. desc->bDescriptorType = 0x29;
  1784. desc->bHubContrCurrent = 0;
  1785. desc->bNbrPorts = r8a66597->max_root_hub;
  1786. desc->bDescLength = 9;
  1787. desc->bPwrOn2PwrGood = 0;
  1788. desc->wHubCharacteristics = cpu_to_le16(0x0011);
  1789. desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
  1790. desc->bitmap[1] = ~0;
  1791. }
  1792. static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1793. u16 wIndex, char *buf, u16 wLength)
  1794. {
  1795. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1796. int ret;
  1797. int port = (wIndex & 0x00FF) - 1;
  1798. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1799. unsigned long flags;
  1800. ret = 0;
  1801. spin_lock_irqsave(&r8a66597->lock, flags);
  1802. switch (typeReq) {
  1803. case ClearHubFeature:
  1804. case SetHubFeature:
  1805. switch (wValue) {
  1806. case C_HUB_OVER_CURRENT:
  1807. case C_HUB_LOCAL_POWER:
  1808. break;
  1809. default:
  1810. goto error;
  1811. }
  1812. break;
  1813. case ClearPortFeature:
  1814. if (wIndex > r8a66597->max_root_hub)
  1815. goto error;
  1816. if (wLength != 0)
  1817. goto error;
  1818. switch (wValue) {
  1819. case USB_PORT_FEAT_ENABLE:
  1820. rh->port &= ~(1 << USB_PORT_FEAT_POWER);
  1821. break;
  1822. case USB_PORT_FEAT_SUSPEND:
  1823. break;
  1824. case USB_PORT_FEAT_POWER:
  1825. r8a66597_port_power(r8a66597, port, 0);
  1826. break;
  1827. case USB_PORT_FEAT_C_ENABLE:
  1828. case USB_PORT_FEAT_C_SUSPEND:
  1829. case USB_PORT_FEAT_C_CONNECTION:
  1830. case USB_PORT_FEAT_C_OVER_CURRENT:
  1831. case USB_PORT_FEAT_C_RESET:
  1832. break;
  1833. default:
  1834. goto error;
  1835. }
  1836. rh->port &= ~(1 << wValue);
  1837. break;
  1838. case GetHubDescriptor:
  1839. r8a66597_hub_descriptor(r8a66597,
  1840. (struct usb_hub_descriptor *)buf);
  1841. break;
  1842. case GetHubStatus:
  1843. *buf = 0x00;
  1844. break;
  1845. case GetPortStatus:
  1846. if (wIndex > r8a66597->max_root_hub)
  1847. goto error;
  1848. *(__le32 *)buf = cpu_to_le32(rh->port);
  1849. break;
  1850. case SetPortFeature:
  1851. if (wIndex > r8a66597->max_root_hub)
  1852. goto error;
  1853. if (wLength != 0)
  1854. goto error;
  1855. switch (wValue) {
  1856. case USB_PORT_FEAT_SUSPEND:
  1857. break;
  1858. case USB_PORT_FEAT_POWER:
  1859. r8a66597_port_power(r8a66597, port, 1);
  1860. rh->port |= (1 << USB_PORT_FEAT_POWER);
  1861. break;
  1862. case USB_PORT_FEAT_RESET: {
  1863. struct r8a66597_device *dev = rh->dev;
  1864. rh->port |= (1 << USB_PORT_FEAT_RESET);
  1865. disable_r8a66597_pipe_all(r8a66597, dev);
  1866. free_usb_address(r8a66597, dev);
  1867. r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
  1868. get_dvstctr_reg(port));
  1869. mod_timer(&r8a66597->rh_timer,
  1870. jiffies + msecs_to_jiffies(50));
  1871. }
  1872. break;
  1873. default:
  1874. goto error;
  1875. }
  1876. rh->port |= 1 << wValue;
  1877. break;
  1878. default:
  1879. error:
  1880. ret = -EPIPE;
  1881. break;
  1882. }
  1883. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1884. return ret;
  1885. }
  1886. #if defined(CONFIG_PM)
  1887. static int r8a66597_bus_suspend(struct usb_hcd *hcd)
  1888. {
  1889. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1890. int port;
  1891. dbg("%s", __func__);
  1892. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1893. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1894. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1895. if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
  1896. continue;
  1897. dbg("suspend port = %d", port);
  1898. r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
  1899. rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
  1900. if (rh->dev->udev->do_remote_wakeup) {
  1901. msleep(3); /* waiting last SOF */
  1902. r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
  1903. r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
  1904. r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
  1905. }
  1906. }
  1907. r8a66597->bus_suspended = 1;
  1908. return 0;
  1909. }
  1910. static int r8a66597_bus_resume(struct usb_hcd *hcd)
  1911. {
  1912. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1913. int port;
  1914. dbg("%s", __func__);
  1915. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1916. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1917. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1918. if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
  1919. continue;
  1920. dbg("resume port = %d", port);
  1921. rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
  1922. rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
  1923. r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
  1924. msleep(50);
  1925. r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
  1926. }
  1927. return 0;
  1928. }
  1929. #else
  1930. #define r8a66597_bus_suspend NULL
  1931. #define r8a66597_bus_resume NULL
  1932. #endif
  1933. static struct hc_driver r8a66597_hc_driver = {
  1934. .description = hcd_name,
  1935. .hcd_priv_size = sizeof(struct r8a66597),
  1936. .irq = r8a66597_irq,
  1937. /*
  1938. * generic hardware linkage
  1939. */
  1940. .flags = HCD_USB2,
  1941. .start = r8a66597_start,
  1942. .stop = r8a66597_stop,
  1943. /*
  1944. * managing i/o requests and associated device resources
  1945. */
  1946. .urb_enqueue = r8a66597_urb_enqueue,
  1947. .urb_dequeue = r8a66597_urb_dequeue,
  1948. .endpoint_disable = r8a66597_endpoint_disable,
  1949. /*
  1950. * periodic schedule support
  1951. */
  1952. .get_frame_number = r8a66597_get_frame,
  1953. /*
  1954. * root hub support
  1955. */
  1956. .hub_status_data = r8a66597_hub_status_data,
  1957. .hub_control = r8a66597_hub_control,
  1958. .bus_suspend = r8a66597_bus_suspend,
  1959. .bus_resume = r8a66597_bus_resume,
  1960. };
  1961. #if defined(CONFIG_PM)
  1962. static int r8a66597_suspend(struct device *dev)
  1963. {
  1964. struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
  1965. int port;
  1966. dbg("%s", __func__);
  1967. disable_controller(r8a66597);
  1968. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1969. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1970. rh->port = 0x00000000;
  1971. }
  1972. return 0;
  1973. }
  1974. static int r8a66597_resume(struct device *dev)
  1975. {
  1976. struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
  1977. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1978. dbg("%s", __func__);
  1979. enable_controller(r8a66597);
  1980. usb_root_hub_lost_power(hcd->self.root_hub);
  1981. return 0;
  1982. }
  1983. static struct dev_pm_ops r8a66597_dev_pm_ops = {
  1984. .suspend = r8a66597_suspend,
  1985. .resume = r8a66597_resume,
  1986. .poweroff = r8a66597_suspend,
  1987. .restore = r8a66597_resume,
  1988. };
  1989. #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
  1990. #else /* if defined(CONFIG_PM) */
  1991. #define R8A66597_DEV_PM_OPS NULL
  1992. #endif
  1993. static int __init_or_module r8a66597_remove(struct platform_device *pdev)
  1994. {
  1995. struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
  1996. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1997. del_timer_sync(&r8a66597->rh_timer);
  1998. usb_remove_hcd(hcd);
  1999. iounmap((void *)r8a66597->reg);
  2000. #ifdef CONFIG_HAVE_CLK
  2001. if (r8a66597->pdata->on_chip)
  2002. clk_put(r8a66597->clk);
  2003. #endif
  2004. usb_put_hcd(hcd);
  2005. return 0;
  2006. }
  2007. static int __devinit r8a66597_probe(struct platform_device *pdev)
  2008. {
  2009. #ifdef CONFIG_HAVE_CLK
  2010. char clk_name[8];
  2011. #endif
  2012. struct resource *res = NULL, *ires;
  2013. int irq = -1;
  2014. void __iomem *reg = NULL;
  2015. struct usb_hcd *hcd = NULL;
  2016. struct r8a66597 *r8a66597;
  2017. int ret = 0;
  2018. int i;
  2019. unsigned long irq_trigger;
  2020. if (pdev->dev.dma_mask) {
  2021. ret = -EINVAL;
  2022. dev_err(&pdev->dev, "dma not supported\n");
  2023. goto clean_up;
  2024. }
  2025. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2026. if (!res) {
  2027. ret = -ENODEV;
  2028. dev_err(&pdev->dev, "platform_get_resource error.\n");
  2029. goto clean_up;
  2030. }
  2031. ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2032. if (!ires) {
  2033. ret = -ENODEV;
  2034. dev_err(&pdev->dev,
  2035. "platform_get_resource IORESOURCE_IRQ error.\n");
  2036. goto clean_up;
  2037. }
  2038. irq = ires->start;
  2039. irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
  2040. reg = ioremap(res->start, resource_size(res));
  2041. if (reg == NULL) {
  2042. ret = -ENOMEM;
  2043. dev_err(&pdev->dev, "ioremap error.\n");
  2044. goto clean_up;
  2045. }
  2046. if (pdev->dev.platform_data == NULL) {
  2047. dev_err(&pdev->dev, "no platform data\n");
  2048. ret = -ENODEV;
  2049. goto clean_up;
  2050. }
  2051. /* initialize hcd */
  2052. hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
  2053. if (!hcd) {
  2054. ret = -ENOMEM;
  2055. dev_err(&pdev->dev, "Failed to create hcd\n");
  2056. goto clean_up;
  2057. }
  2058. r8a66597 = hcd_to_r8a66597(hcd);
  2059. memset(r8a66597, 0, sizeof(struct r8a66597));
  2060. dev_set_drvdata(&pdev->dev, r8a66597);
  2061. r8a66597->pdata = pdev->dev.platform_data;
  2062. r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
  2063. if (r8a66597->pdata->on_chip) {
  2064. #ifdef CONFIG_HAVE_CLK
  2065. snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
  2066. r8a66597->clk = clk_get(&pdev->dev, clk_name);
  2067. if (IS_ERR(r8a66597->clk)) {
  2068. dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
  2069. clk_name);
  2070. ret = PTR_ERR(r8a66597->clk);
  2071. goto clean_up2;
  2072. }
  2073. #endif
  2074. r8a66597->max_root_hub = 1;
  2075. } else
  2076. r8a66597->max_root_hub = 2;
  2077. spin_lock_init(&r8a66597->lock);
  2078. init_timer(&r8a66597->rh_timer);
  2079. r8a66597->rh_timer.function = r8a66597_timer;
  2080. r8a66597->rh_timer.data = (unsigned long)r8a66597;
  2081. r8a66597->reg = (unsigned long)reg;
  2082. for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
  2083. INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
  2084. init_timer(&r8a66597->td_timer[i]);
  2085. r8a66597->td_timer[i].function = r8a66597_td_timer;
  2086. r8a66597->td_timer[i].data = (unsigned long)r8a66597;
  2087. setup_timer(&r8a66597->interval_timer[i],
  2088. r8a66597_interval_timer,
  2089. (unsigned long)r8a66597);
  2090. }
  2091. INIT_LIST_HEAD(&r8a66597->child_device);
  2092. hcd->rsrc_start = res->start;
  2093. ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
  2094. if (ret != 0) {
  2095. dev_err(&pdev->dev, "Failed to add hcd\n");
  2096. goto clean_up3;
  2097. }
  2098. return 0;
  2099. clean_up3:
  2100. #ifdef CONFIG_HAVE_CLK
  2101. if (r8a66597->pdata->on_chip)
  2102. clk_put(r8a66597->clk);
  2103. clean_up2:
  2104. #endif
  2105. usb_put_hcd(hcd);
  2106. clean_up:
  2107. if (reg)
  2108. iounmap(reg);
  2109. return ret;
  2110. }
  2111. static struct platform_driver r8a66597_driver = {
  2112. .probe = r8a66597_probe,
  2113. .remove = r8a66597_remove,
  2114. .driver = {
  2115. .name = (char *) hcd_name,
  2116. .owner = THIS_MODULE,
  2117. .pm = R8A66597_DEV_PM_OPS,
  2118. },
  2119. };
  2120. static int __init r8a66597_init(void)
  2121. {
  2122. if (usb_disabled())
  2123. return -ENODEV;
  2124. printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
  2125. DRIVER_VERSION);
  2126. return platform_driver_register(&r8a66597_driver);
  2127. }
  2128. module_init(r8a66597_init);
  2129. static void __exit r8a66597_cleanup(void)
  2130. {
  2131. platform_driver_unregister(&r8a66597_driver);
  2132. }
  2133. module_exit(r8a66597_cleanup);