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