r8a66597-hcd.c 60 KB

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