r8a66597-hcd.c 56 KB

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