r8a66597-hcd.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  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. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
  650. urb);
  651. spin_unlock(&r8a66597->lock);
  652. usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
  653. -ENODEV);
  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 = le16_to_cpu(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, cpu_to_le16(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. static int is_set_address(unsigned char *setup_packet)
  864. {
  865. if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
  866. setup_packet[1] == USB_REQ_SET_ADDRESS)
  867. return 1;
  868. else
  869. return 0;
  870. }
  871. /* this function must be called with interrupt disabled */
  872. static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  873. {
  874. BUG_ON(!td);
  875. switch (td->type) {
  876. case USB_PID_SETUP:
  877. if (is_set_address(td->urb->setup_packet)) {
  878. td->set_address = 1;
  879. td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
  880. td->urb);
  881. if (td->urb->setup_packet[2] == 0)
  882. return -EPIPE;
  883. }
  884. prepare_setup_packet(r8a66597, td);
  885. break;
  886. case USB_PID_IN:
  887. prepare_packet_read(r8a66597, td);
  888. break;
  889. case USB_PID_OUT:
  890. prepare_packet_write(r8a66597, td);
  891. break;
  892. case USB_PID_ACK:
  893. prepare_status_packet(r8a66597, td);
  894. break;
  895. default:
  896. err("invalid type.");
  897. break;
  898. }
  899. return 0;
  900. }
  901. static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
  902. {
  903. if (usb_pipeisoc(urb->pipe)) {
  904. if (urb->number_of_packets == td->iso_cnt)
  905. return 1;
  906. }
  907. /* control or bulk or interrupt */
  908. if ((urb->transfer_buffer_length <= urb->actual_length) ||
  909. (td->short_packet) || (td->zero_packet))
  910. return 1;
  911. return 0;
  912. }
  913. /* this function must be called with interrupt disabled */
  914. static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  915. {
  916. unsigned long time;
  917. BUG_ON(!td);
  918. if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
  919. !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
  920. r8a66597->timeout_map |= 1 << td->pipenum;
  921. switch (usb_pipetype(td->urb->pipe)) {
  922. case PIPE_INTERRUPT:
  923. case PIPE_ISOCHRONOUS:
  924. time = 30;
  925. break;
  926. default:
  927. time = 300;
  928. break;
  929. }
  930. mod_timer(&r8a66597->td_timer[td->pipenum],
  931. jiffies + msecs_to_jiffies(time));
  932. }
  933. }
  934. /* this function must be called with interrupt disabled */
  935. static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
  936. u16 pipenum, struct urb *urb, int status)
  937. __releases(r8a66597->lock) __acquires(r8a66597->lock)
  938. {
  939. int restart = 0;
  940. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  941. r8a66597->timeout_map &= ~(1 << pipenum);
  942. if (likely(td)) {
  943. if (td->set_address && (status != 0 || urb->unlinked))
  944. r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
  945. pipe_toggle_save(r8a66597, td->pipe, urb);
  946. list_del(&td->queue);
  947. kfree(td);
  948. }
  949. if (!list_empty(&r8a66597->pipe_queue[pipenum]))
  950. restart = 1;
  951. if (likely(urb)) {
  952. if (usb_pipeisoc(urb->pipe))
  953. urb->start_frame = r8a66597_get_frame(hcd);
  954. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
  955. spin_unlock(&r8a66597->lock);
  956. usb_hcd_giveback_urb(hcd, urb, status);
  957. spin_lock(&r8a66597->lock);
  958. }
  959. if (restart) {
  960. td = r8a66597_get_td(r8a66597, pipenum);
  961. if (unlikely(!td))
  962. return;
  963. start_transfer(r8a66597, td);
  964. set_td_timer(r8a66597, td);
  965. }
  966. }
  967. static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
  968. {
  969. u16 tmp;
  970. int rcv_len, bufsize, urb_len, size;
  971. u16 *buf;
  972. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  973. struct urb *urb;
  974. int finish = 0;
  975. int status = 0;
  976. if (unlikely(!td))
  977. return;
  978. urb = td->urb;
  979. fifo_change_from_pipe(r8a66597, td->pipe);
  980. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  981. if (unlikely((tmp & FRDY) == 0)) {
  982. pipe_stop(r8a66597, td->pipe);
  983. pipe_irq_disable(r8a66597, pipenum);
  984. err("in fifo not ready (%d)", pipenum);
  985. finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
  986. return;
  987. }
  988. /* prepare parameters */
  989. rcv_len = tmp & DTLN;
  990. if (usb_pipeisoc(urb->pipe)) {
  991. buf = (u16 *)(urb->transfer_buffer +
  992. urb->iso_frame_desc[td->iso_cnt].offset);
  993. urb_len = urb->iso_frame_desc[td->iso_cnt].length;
  994. } else {
  995. buf = (void *)urb->transfer_buffer + urb->actual_length;
  996. urb_len = urb->transfer_buffer_length - urb->actual_length;
  997. }
  998. bufsize = min(urb_len, (int) td->maxpacket);
  999. if (rcv_len <= bufsize) {
  1000. size = rcv_len;
  1001. } else {
  1002. size = bufsize;
  1003. status = -EOVERFLOW;
  1004. finish = 1;
  1005. }
  1006. /* update parameters */
  1007. urb->actual_length += size;
  1008. if (rcv_len == 0)
  1009. td->zero_packet = 1;
  1010. if (rcv_len < bufsize) {
  1011. td->short_packet = 1;
  1012. }
  1013. if (usb_pipeisoc(urb->pipe)) {
  1014. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1015. urb->iso_frame_desc[td->iso_cnt].status = status;
  1016. td->iso_cnt++;
  1017. finish = 0;
  1018. }
  1019. /* check transfer finish */
  1020. if (finish || check_transfer_finish(td, urb)) {
  1021. pipe_stop(r8a66597, td->pipe);
  1022. pipe_irq_disable(r8a66597, pipenum);
  1023. finish = 1;
  1024. }
  1025. /* read fifo */
  1026. if (urb->transfer_buffer) {
  1027. if (size == 0)
  1028. r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
  1029. else
  1030. r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
  1031. buf, size);
  1032. }
  1033. if (finish && pipenum != 0)
  1034. finish_request(r8a66597, td, pipenum, urb, status);
  1035. }
  1036. static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
  1037. {
  1038. u16 tmp;
  1039. int bufsize, size;
  1040. u16 *buf;
  1041. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1042. struct urb *urb;
  1043. if (unlikely(!td))
  1044. return;
  1045. urb = td->urb;
  1046. fifo_change_from_pipe(r8a66597, td->pipe);
  1047. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1048. if (unlikely((tmp & FRDY) == 0)) {
  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, urb, -EPIPE);
  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, ~(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, int status)
  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 (check_transfer_finish(td, urb))
  1103. td->type = USB_PID_ACK;
  1104. break;
  1105. case USB_PID_SETUP:
  1106. if (urb->transfer_buffer_length == urb->actual_length)
  1107. td->type = USB_PID_ACK;
  1108. else if (usb_pipeout(urb->pipe))
  1109. td->type = USB_PID_OUT;
  1110. else
  1111. td->type = USB_PID_IN;
  1112. break;
  1113. case USB_PID_ACK:
  1114. finish = 1;
  1115. break;
  1116. }
  1117. if (finish || status != 0 || urb->unlinked)
  1118. finish_request(r8a66597, td, 0, urb, status);
  1119. else
  1120. start_transfer(r8a66597, td);
  1121. }
  1122. static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
  1123. {
  1124. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1125. if (td) {
  1126. u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
  1127. if (pid == PID_NAK)
  1128. return -ECONNRESET;
  1129. else
  1130. return -EPIPE;
  1131. }
  1132. return 0;
  1133. }
  1134. static void irq_pipe_ready(struct r8a66597 *r8a66597)
  1135. {
  1136. u16 check;
  1137. u16 pipenum;
  1138. u16 mask;
  1139. struct r8a66597_td *td;
  1140. mask = r8a66597_read(r8a66597, BRDYSTS)
  1141. & r8a66597_read(r8a66597, BRDYENB);
  1142. r8a66597_write(r8a66597, ~mask, BRDYSTS);
  1143. if (mask & BRDY0) {
  1144. td = r8a66597_get_td(r8a66597, 0);
  1145. if (td && td->type == USB_PID_IN)
  1146. packet_read(r8a66597, 0);
  1147. else
  1148. pipe_irq_disable(r8a66597, 0);
  1149. check_next_phase(r8a66597, 0);
  1150. }
  1151. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1152. check = 1 << pipenum;
  1153. if (mask & check) {
  1154. td = r8a66597_get_td(r8a66597, pipenum);
  1155. if (unlikely(!td))
  1156. continue;
  1157. if (td->type == USB_PID_IN)
  1158. packet_read(r8a66597, pipenum);
  1159. else if (td->type == USB_PID_OUT)
  1160. packet_write(r8a66597, pipenum);
  1161. }
  1162. }
  1163. }
  1164. static void irq_pipe_empty(struct r8a66597 *r8a66597)
  1165. {
  1166. u16 tmp;
  1167. u16 check;
  1168. u16 pipenum;
  1169. u16 mask;
  1170. struct r8a66597_td *td;
  1171. mask = r8a66597_read(r8a66597, BEMPSTS)
  1172. & r8a66597_read(r8a66597, BEMPENB);
  1173. r8a66597_write(r8a66597, ~mask, BEMPSTS);
  1174. if (mask & BEMP0) {
  1175. cfifo_change(r8a66597, 0);
  1176. td = r8a66597_get_td(r8a66597, 0);
  1177. if (td && td->type != USB_PID_OUT)
  1178. disable_irq_empty(r8a66597, 0);
  1179. check_next_phase(r8a66597, 0);
  1180. }
  1181. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1182. check = 1 << pipenum;
  1183. if (mask & check) {
  1184. struct r8a66597_td *td;
  1185. td = r8a66597_get_td(r8a66597, pipenum);
  1186. if (unlikely(!td))
  1187. continue;
  1188. tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
  1189. if ((tmp & INBUFM) == 0) {
  1190. disable_irq_empty(r8a66597, pipenum);
  1191. pipe_irq_disable(r8a66597, pipenum);
  1192. finish_request(r8a66597, td, pipenum, td->urb,
  1193. 0);
  1194. }
  1195. }
  1196. }
  1197. }
  1198. static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
  1199. {
  1200. u16 check;
  1201. u16 pipenum;
  1202. u16 mask;
  1203. int status;
  1204. mask = r8a66597_read(r8a66597, NRDYSTS)
  1205. & r8a66597_read(r8a66597, NRDYENB);
  1206. r8a66597_write(r8a66597, ~mask, NRDYSTS);
  1207. if (mask & NRDY0) {
  1208. cfifo_change(r8a66597, 0);
  1209. status = get_urb_error(r8a66597, 0);
  1210. pipe_irq_disable(r8a66597, 0);
  1211. check_next_phase(r8a66597, status);
  1212. }
  1213. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1214. check = 1 << pipenum;
  1215. if (mask & check) {
  1216. struct r8a66597_td *td;
  1217. td = r8a66597_get_td(r8a66597, pipenum);
  1218. if (unlikely(!td))
  1219. continue;
  1220. status = get_urb_error(r8a66597, pipenum);
  1221. pipe_irq_disable(r8a66597, pipenum);
  1222. pipe_stop(r8a66597, td->pipe);
  1223. finish_request(r8a66597, td, pipenum, td->urb, status);
  1224. }
  1225. }
  1226. }
  1227. static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
  1228. {
  1229. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1230. rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  1231. rh->scount = R8A66597_MAX_SAMPLING;
  1232. mod_timer(&r8a66597->rh_timer, jiffies + msecs_to_jiffies(50));
  1233. }
  1234. static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
  1235. {
  1236. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1237. u16 intsts0, intsts1, intsts2;
  1238. u16 intenb0, intenb1, intenb2;
  1239. u16 mask0, mask1, mask2;
  1240. int status;
  1241. spin_lock(&r8a66597->lock);
  1242. intsts0 = r8a66597_read(r8a66597, INTSTS0);
  1243. intsts1 = r8a66597_read(r8a66597, INTSTS1);
  1244. intsts2 = r8a66597_read(r8a66597, INTSTS2);
  1245. intenb0 = r8a66597_read(r8a66597, INTENB0);
  1246. intenb1 = r8a66597_read(r8a66597, INTENB1);
  1247. intenb2 = r8a66597_read(r8a66597, INTENB2);
  1248. mask2 = intsts2 & intenb2;
  1249. mask1 = intsts1 & intenb1;
  1250. mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
  1251. if (mask2) {
  1252. if (mask2 & ATTCH) {
  1253. r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
  1254. r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
  1255. /* start usb bus sampling */
  1256. start_root_hub_sampling(r8a66597, 1);
  1257. }
  1258. if (mask2 & DTCH) {
  1259. r8a66597_write(r8a66597, ~DTCH, INTSTS2);
  1260. r8a66597_bclr(r8a66597, DTCHE, INTENB2);
  1261. r8a66597_usb_disconnect(r8a66597, 1);
  1262. }
  1263. }
  1264. if (mask1) {
  1265. if (mask1 & ATTCH) {
  1266. r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
  1267. r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
  1268. /* start usb bus sampling */
  1269. start_root_hub_sampling(r8a66597, 0);
  1270. }
  1271. if (mask1 & DTCH) {
  1272. r8a66597_write(r8a66597, ~DTCH, INTSTS1);
  1273. r8a66597_bclr(r8a66597, DTCHE, INTENB1);
  1274. r8a66597_usb_disconnect(r8a66597, 0);
  1275. }
  1276. if (mask1 & SIGN) {
  1277. r8a66597_write(r8a66597, ~SIGN, INTSTS1);
  1278. status = get_urb_error(r8a66597, 0);
  1279. check_next_phase(r8a66597, status);
  1280. }
  1281. if (mask1 & SACK) {
  1282. r8a66597_write(r8a66597, ~SACK, INTSTS1);
  1283. check_next_phase(r8a66597, 0);
  1284. }
  1285. }
  1286. if (mask0) {
  1287. if (mask0 & BRDY)
  1288. irq_pipe_ready(r8a66597);
  1289. if (mask0 & BEMP)
  1290. irq_pipe_empty(r8a66597);
  1291. if (mask0 & NRDY)
  1292. irq_pipe_nrdy(r8a66597);
  1293. }
  1294. spin_unlock(&r8a66597->lock);
  1295. return IRQ_HANDLED;
  1296. }
  1297. /* this function must be called with interrupt disabled */
  1298. static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
  1299. {
  1300. u16 tmp;
  1301. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1302. if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
  1303. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1304. tmp = r8a66597_read(r8a66597, dvstctr_reg);
  1305. if ((tmp & USBRST) == USBRST) {
  1306. r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
  1307. dvstctr_reg);
  1308. mod_timer(&r8a66597->rh_timer,
  1309. jiffies + msecs_to_jiffies(50));
  1310. } else
  1311. r8a66597_usb_connect(r8a66597, port);
  1312. }
  1313. if (rh->scount > 0) {
  1314. tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  1315. if (tmp == rh->old_syssts) {
  1316. rh->scount--;
  1317. if (rh->scount == 0) {
  1318. if (tmp == FS_JSTS) {
  1319. r8a66597_bset(r8a66597, HSE,
  1320. get_syscfg_reg(port));
  1321. r8a66597_usb_preconnect(r8a66597, port);
  1322. } else if (tmp == LS_JSTS) {
  1323. r8a66597_bclr(r8a66597, HSE,
  1324. get_syscfg_reg(port));
  1325. r8a66597_usb_preconnect(r8a66597, port);
  1326. } else if (tmp == SE0)
  1327. r8a66597_bset(r8a66597, ATTCHE,
  1328. get_intenb_reg(port));
  1329. } else {
  1330. mod_timer(&r8a66597->rh_timer,
  1331. jiffies + msecs_to_jiffies(50));
  1332. }
  1333. } else {
  1334. rh->scount = R8A66597_MAX_SAMPLING;
  1335. rh->old_syssts = tmp;
  1336. mod_timer(&r8a66597->rh_timer,
  1337. jiffies + msecs_to_jiffies(50));
  1338. }
  1339. }
  1340. }
  1341. static void r8a66597_td_timer(unsigned long _r8a66597)
  1342. {
  1343. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1344. unsigned long flags;
  1345. u16 pipenum;
  1346. struct r8a66597_td *td, *new_td = NULL;
  1347. struct r8a66597_pipe *pipe;
  1348. spin_lock_irqsave(&r8a66597->lock, flags);
  1349. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1350. if (!(r8a66597->timeout_map & (1 << pipenum)))
  1351. continue;
  1352. if (timer_pending(&r8a66597->td_timer[pipenum]))
  1353. continue;
  1354. td = r8a66597_get_td(r8a66597, pipenum);
  1355. if (!td) {
  1356. r8a66597->timeout_map &= ~(1 << pipenum);
  1357. continue;
  1358. }
  1359. if (td->urb->actual_length) {
  1360. set_td_timer(r8a66597, td);
  1361. break;
  1362. }
  1363. pipe = td->pipe;
  1364. pipe_stop(r8a66597, pipe);
  1365. new_td = td;
  1366. do {
  1367. list_move_tail(&new_td->queue,
  1368. &r8a66597->pipe_queue[pipenum]);
  1369. new_td = r8a66597_get_td(r8a66597, pipenum);
  1370. if (!new_td) {
  1371. new_td = td;
  1372. break;
  1373. }
  1374. } while (td != new_td && td->address == new_td->address);
  1375. start_transfer(r8a66597, new_td);
  1376. if (td == new_td)
  1377. r8a66597->timeout_map &= ~(1 << pipenum);
  1378. else
  1379. set_td_timer(r8a66597, new_td);
  1380. break;
  1381. }
  1382. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1383. }
  1384. static void r8a66597_timer(unsigned long _r8a66597)
  1385. {
  1386. struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
  1387. unsigned long flags;
  1388. spin_lock_irqsave(&r8a66597->lock, flags);
  1389. r8a66597_root_hub_control(r8a66597, 0);
  1390. r8a66597_root_hub_control(r8a66597, 1);
  1391. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1392. }
  1393. static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  1394. {
  1395. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  1396. if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
  1397. (urb->dev->state == USB_STATE_CONFIGURED))
  1398. return 1;
  1399. else
  1400. return 0;
  1401. }
  1402. static int r8a66597_start(struct usb_hcd *hcd)
  1403. {
  1404. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1405. hcd->state = HC_STATE_RUNNING;
  1406. return enable_controller(r8a66597);
  1407. }
  1408. static void r8a66597_stop(struct usb_hcd *hcd)
  1409. {
  1410. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1411. disable_controller(r8a66597);
  1412. }
  1413. static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
  1414. {
  1415. unsigned int usb_address = usb_pipedevice(urb->pipe);
  1416. u16 root_port, hub_port;
  1417. if (usb_address == 0) {
  1418. get_port_number(urb->dev->devpath,
  1419. &root_port, &hub_port);
  1420. set_devadd_reg(r8a66597, 0,
  1421. get_r8a66597_usb_speed(urb->dev->speed),
  1422. get_parent_r8a66597_address(r8a66597, urb->dev),
  1423. hub_port, root_port);
  1424. }
  1425. }
  1426. static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
  1427. struct urb *urb,
  1428. struct usb_host_endpoint *hep)
  1429. {
  1430. struct r8a66597_td *td;
  1431. u16 pipenum;
  1432. td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
  1433. if (td == NULL)
  1434. return NULL;
  1435. pipenum = r8a66597_get_pipenum(urb, hep);
  1436. td->pipenum = pipenum;
  1437. td->pipe = hep->hcpriv;
  1438. td->urb = urb;
  1439. td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
  1440. td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
  1441. !usb_pipein(urb->pipe));
  1442. if (usb_pipecontrol(urb->pipe))
  1443. td->type = USB_PID_SETUP;
  1444. else if (usb_pipein(urb->pipe))
  1445. td->type = USB_PID_IN;
  1446. else
  1447. td->type = USB_PID_OUT;
  1448. INIT_LIST_HEAD(&td->queue);
  1449. return td;
  1450. }
  1451. static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
  1452. struct urb *urb,
  1453. gfp_t mem_flags)
  1454. {
  1455. struct usb_host_endpoint *hep = urb->ep;
  1456. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1457. struct r8a66597_td *td = NULL;
  1458. int ret, request = 0;
  1459. unsigned long flags;
  1460. spin_lock_irqsave(&r8a66597->lock, flags);
  1461. if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
  1462. ret = -ENODEV;
  1463. goto error_not_linked;
  1464. }
  1465. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1466. if (ret)
  1467. goto error_not_linked;
  1468. if (!hep->hcpriv) {
  1469. hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
  1470. GFP_ATOMIC);
  1471. if (!hep->hcpriv) {
  1472. ret = -ENOMEM;
  1473. goto error;
  1474. }
  1475. set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
  1476. if (usb_pipeendpoint(urb->pipe))
  1477. init_pipe_info(r8a66597, urb, hep, &hep->desc);
  1478. }
  1479. if (unlikely(check_pipe_config(r8a66597, urb)))
  1480. init_pipe_config(r8a66597, urb);
  1481. set_address_zero(r8a66597, urb);
  1482. td = r8a66597_make_td(r8a66597, urb, hep);
  1483. if (td == NULL) {
  1484. ret = -ENOMEM;
  1485. goto error;
  1486. }
  1487. if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
  1488. request = 1;
  1489. list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
  1490. urb->hcpriv = td;
  1491. if (request) {
  1492. ret = start_transfer(r8a66597, td);
  1493. if (ret < 0) {
  1494. list_del(&td->queue);
  1495. kfree(td);
  1496. }
  1497. } else
  1498. set_td_timer(r8a66597, td);
  1499. error:
  1500. if (ret)
  1501. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1502. error_not_linked:
  1503. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1504. return ret;
  1505. }
  1506. static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1507. int status)
  1508. {
  1509. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1510. struct r8a66597_td *td;
  1511. unsigned long flags;
  1512. int rc;
  1513. spin_lock_irqsave(&r8a66597->lock, flags);
  1514. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1515. if (rc)
  1516. goto done;
  1517. if (urb->hcpriv) {
  1518. td = urb->hcpriv;
  1519. pipe_stop(r8a66597, td->pipe);
  1520. pipe_irq_disable(r8a66597, td->pipenum);
  1521. disable_irq_empty(r8a66597, td->pipenum);
  1522. finish_request(r8a66597, td, td->pipenum, urb, status);
  1523. }
  1524. done:
  1525. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1526. return rc;
  1527. }
  1528. static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
  1529. struct usb_host_endpoint *hep)
  1530. {
  1531. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1532. struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
  1533. struct r8a66597_td *td;
  1534. struct urb *urb = NULL;
  1535. u16 pipenum;
  1536. unsigned long flags;
  1537. if (pipe == NULL)
  1538. return;
  1539. pipenum = pipe->info.pipenum;
  1540. if (pipenum == 0) {
  1541. kfree(hep->hcpriv);
  1542. hep->hcpriv = NULL;
  1543. return;
  1544. }
  1545. spin_lock_irqsave(&r8a66597->lock, flags);
  1546. pipe_stop(r8a66597, pipe);
  1547. pipe_irq_disable(r8a66597, pipenum);
  1548. disable_irq_empty(r8a66597, pipenum);
  1549. td = r8a66597_get_td(r8a66597, pipenum);
  1550. if (td)
  1551. urb = td->urb;
  1552. finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
  1553. kfree(hep->hcpriv);
  1554. hep->hcpriv = NULL;
  1555. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1556. }
  1557. static int r8a66597_get_frame(struct usb_hcd *hcd)
  1558. {
  1559. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1560. return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
  1561. }
  1562. static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
  1563. {
  1564. int chix;
  1565. if (udev->state == USB_STATE_CONFIGURED &&
  1566. udev->parent && udev->parent->devnum > 1 &&
  1567. udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
  1568. map[udev->devnum/32] |= (1 << (udev->devnum % 32));
  1569. for (chix = 0; chix < udev->maxchild; chix++) {
  1570. struct usb_device *childdev = udev->children[chix];
  1571. if (childdev)
  1572. collect_usb_address_map(childdev, map);
  1573. }
  1574. }
  1575. /* this function must be called with interrupt disabled */
  1576. static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
  1577. int addr)
  1578. {
  1579. struct r8a66597_device *dev;
  1580. struct list_head *list = &r8a66597->child_device;
  1581. list_for_each_entry(dev, list, device_list) {
  1582. if (!dev)
  1583. continue;
  1584. if (dev->usb_address != addr)
  1585. continue;
  1586. return dev;
  1587. }
  1588. err("get_r8a66597_device fail.(%d)\n", addr);
  1589. return NULL;
  1590. }
  1591. static void update_usb_address_map(struct r8a66597 *r8a66597,
  1592. struct usb_device *root_hub,
  1593. unsigned long *map)
  1594. {
  1595. int i, j, addr;
  1596. unsigned long diff;
  1597. unsigned long flags;
  1598. for (i = 0; i < 4; i++) {
  1599. diff = r8a66597->child_connect_map[i] ^ map[i];
  1600. if (!diff)
  1601. continue;
  1602. for (j = 0; j < 32; j++) {
  1603. if (!(diff & (1 << j)))
  1604. continue;
  1605. addr = i * 32 + j;
  1606. if (map[i] & (1 << j))
  1607. set_child_connect_map(r8a66597, addr);
  1608. else {
  1609. struct r8a66597_device *dev;
  1610. spin_lock_irqsave(&r8a66597->lock, flags);
  1611. dev = get_r8a66597_device(r8a66597, addr);
  1612. disable_r8a66597_pipe_all(r8a66597, dev);
  1613. free_usb_address(r8a66597, dev);
  1614. put_child_connect_map(r8a66597, addr);
  1615. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1616. }
  1617. }
  1618. }
  1619. }
  1620. static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
  1621. struct usb_hcd *hcd)
  1622. {
  1623. struct usb_bus *bus;
  1624. unsigned long now_map[4];
  1625. memset(now_map, 0, sizeof(now_map));
  1626. list_for_each_entry(bus, &usb_bus_list, bus_list) {
  1627. if (!bus->root_hub)
  1628. continue;
  1629. if (bus->busnum != hcd->self.busnum)
  1630. continue;
  1631. collect_usb_address_map(bus->root_hub, now_map);
  1632. update_usb_address_map(r8a66597, bus->root_hub, now_map);
  1633. }
  1634. }
  1635. static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
  1636. {
  1637. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1638. unsigned long flags;
  1639. int i;
  1640. r8a66597_check_detect_child(r8a66597, hcd);
  1641. spin_lock_irqsave(&r8a66597->lock, flags);
  1642. *buf = 0; /* initialize (no change) */
  1643. for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
  1644. if (r8a66597->root_hub[i].port & 0xffff0000)
  1645. *buf |= 1 << (i + 1);
  1646. }
  1647. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1648. return (*buf != 0);
  1649. }
  1650. static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
  1651. struct usb_hub_descriptor *desc)
  1652. {
  1653. desc->bDescriptorType = 0x29;
  1654. desc->bHubContrCurrent = 0;
  1655. desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
  1656. desc->bDescLength = 9;
  1657. desc->bPwrOn2PwrGood = 0;
  1658. desc->wHubCharacteristics = cpu_to_le16(0x0011);
  1659. desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
  1660. desc->bitmap[1] = ~0;
  1661. }
  1662. static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1663. u16 wIndex, char *buf, u16 wLength)
  1664. {
  1665. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1666. int ret;
  1667. int port = (wIndex & 0x00FF) - 1;
  1668. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1669. unsigned long flags;
  1670. ret = 0;
  1671. spin_lock_irqsave(&r8a66597->lock, flags);
  1672. switch (typeReq) {
  1673. case ClearHubFeature:
  1674. case SetHubFeature:
  1675. switch (wValue) {
  1676. case C_HUB_OVER_CURRENT:
  1677. case C_HUB_LOCAL_POWER:
  1678. break;
  1679. default:
  1680. goto error;
  1681. }
  1682. break;
  1683. case ClearPortFeature:
  1684. if (wIndex > R8A66597_MAX_ROOT_HUB)
  1685. goto error;
  1686. if (wLength != 0)
  1687. goto error;
  1688. switch (wValue) {
  1689. case USB_PORT_FEAT_ENABLE:
  1690. rh->port &= (1 << USB_PORT_FEAT_POWER);
  1691. break;
  1692. case USB_PORT_FEAT_SUSPEND:
  1693. break;
  1694. case USB_PORT_FEAT_POWER:
  1695. r8a66597_port_power(r8a66597, port, 0);
  1696. break;
  1697. case USB_PORT_FEAT_C_ENABLE:
  1698. case USB_PORT_FEAT_C_SUSPEND:
  1699. case USB_PORT_FEAT_C_CONNECTION:
  1700. case USB_PORT_FEAT_C_OVER_CURRENT:
  1701. case USB_PORT_FEAT_C_RESET:
  1702. break;
  1703. default:
  1704. goto error;
  1705. }
  1706. rh->port &= ~(1 << wValue);
  1707. break;
  1708. case GetHubDescriptor:
  1709. r8a66597_hub_descriptor(r8a66597,
  1710. (struct usb_hub_descriptor *)buf);
  1711. break;
  1712. case GetHubStatus:
  1713. *buf = 0x00;
  1714. break;
  1715. case GetPortStatus:
  1716. if (wIndex > R8A66597_MAX_ROOT_HUB)
  1717. goto error;
  1718. *(u32 *)buf = cpu_to_le32(rh->port);
  1719. break;
  1720. case SetPortFeature:
  1721. if (wIndex > R8A66597_MAX_ROOT_HUB)
  1722. goto error;
  1723. if (wLength != 0)
  1724. goto error;
  1725. switch (wValue) {
  1726. case USB_PORT_FEAT_SUSPEND:
  1727. break;
  1728. case USB_PORT_FEAT_POWER:
  1729. r8a66597_port_power(r8a66597, port, 1);
  1730. rh->port |= (1 << USB_PORT_FEAT_POWER);
  1731. break;
  1732. case USB_PORT_FEAT_RESET: {
  1733. struct r8a66597_device *dev = rh->dev;
  1734. rh->port |= (1 << USB_PORT_FEAT_RESET);
  1735. disable_r8a66597_pipe_all(r8a66597, dev);
  1736. free_usb_address(r8a66597, dev);
  1737. r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
  1738. get_dvstctr_reg(port));
  1739. mod_timer(&r8a66597->rh_timer,
  1740. jiffies + msecs_to_jiffies(50));
  1741. }
  1742. break;
  1743. default:
  1744. goto error;
  1745. }
  1746. rh->port |= 1 << wValue;
  1747. break;
  1748. default:
  1749. error:
  1750. ret = -EPIPE;
  1751. break;
  1752. }
  1753. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1754. return ret;
  1755. }
  1756. static struct hc_driver r8a66597_hc_driver = {
  1757. .description = hcd_name,
  1758. .hcd_priv_size = sizeof(struct r8a66597),
  1759. .irq = r8a66597_irq,
  1760. /*
  1761. * generic hardware linkage
  1762. */
  1763. .flags = HCD_USB2,
  1764. .start = r8a66597_start,
  1765. .stop = r8a66597_stop,
  1766. /*
  1767. * managing i/o requests and associated device resources
  1768. */
  1769. .urb_enqueue = r8a66597_urb_enqueue,
  1770. .urb_dequeue = r8a66597_urb_dequeue,
  1771. .endpoint_disable = r8a66597_endpoint_disable,
  1772. /*
  1773. * periodic schedule support
  1774. */
  1775. .get_frame_number = r8a66597_get_frame,
  1776. /*
  1777. * root hub support
  1778. */
  1779. .hub_status_data = r8a66597_hub_status_data,
  1780. .hub_control = r8a66597_hub_control,
  1781. };
  1782. #if defined(CONFIG_PM)
  1783. static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
  1784. {
  1785. pdev->dev.power.power_state = state;
  1786. return 0;
  1787. }
  1788. static int r8a66597_resume(struct platform_device *pdev)
  1789. {
  1790. pdev->dev.power.power_state = PMSG_ON;
  1791. return 0;
  1792. }
  1793. #else /* if defined(CONFIG_PM) */
  1794. #define r8a66597_suspend NULL
  1795. #define r8a66597_resume NULL
  1796. #endif
  1797. static int __init_or_module r8a66597_remove(struct platform_device *pdev)
  1798. {
  1799. struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
  1800. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1801. del_timer_sync(&r8a66597->rh_timer);
  1802. usb_remove_hcd(hcd);
  1803. iounmap((void *)r8a66597->reg);
  1804. usb_put_hcd(hcd);
  1805. return 0;
  1806. }
  1807. #define resource_len(r) (((r)->end - (r)->start) + 1)
  1808. static int __init r8a66597_probe(struct platform_device *pdev)
  1809. {
  1810. struct resource *res = NULL;
  1811. int irq = -1;
  1812. void __iomem *reg = NULL;
  1813. struct usb_hcd *hcd = NULL;
  1814. struct r8a66597 *r8a66597;
  1815. int ret = 0;
  1816. int i;
  1817. if (pdev->dev.dma_mask) {
  1818. ret = -EINVAL;
  1819. err("dma not support");
  1820. goto clean_up;
  1821. }
  1822. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1823. (char *)hcd_name);
  1824. if (!res) {
  1825. ret = -ENODEV;
  1826. err("platform_get_resource_byname error.");
  1827. goto clean_up;
  1828. }
  1829. irq = platform_get_irq(pdev, 0);
  1830. if (irq < 0) {
  1831. ret = -ENODEV;
  1832. err("platform_get_irq error.");
  1833. goto clean_up;
  1834. }
  1835. reg = ioremap(res->start, resource_len(res));
  1836. if (reg == NULL) {
  1837. ret = -ENOMEM;
  1838. err("ioremap error.");
  1839. goto clean_up;
  1840. }
  1841. /* initialize hcd */
  1842. hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
  1843. if (!hcd) {
  1844. ret = -ENOMEM;
  1845. err("Failed to create hcd");
  1846. goto clean_up;
  1847. }
  1848. r8a66597 = hcd_to_r8a66597(hcd);
  1849. memset(r8a66597, 0, sizeof(struct r8a66597));
  1850. dev_set_drvdata(&pdev->dev, r8a66597);
  1851. spin_lock_init(&r8a66597->lock);
  1852. init_timer(&r8a66597->rh_timer);
  1853. r8a66597->rh_timer.function = r8a66597_timer;
  1854. r8a66597->rh_timer.data = (unsigned long)r8a66597;
  1855. r8a66597->reg = (unsigned long)reg;
  1856. for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
  1857. INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
  1858. init_timer(&r8a66597->td_timer[i]);
  1859. r8a66597->td_timer[i].function = r8a66597_td_timer;
  1860. r8a66597->td_timer[i].data = (unsigned long)r8a66597;
  1861. }
  1862. INIT_LIST_HEAD(&r8a66597->child_device);
  1863. hcd->rsrc_start = res->start;
  1864. ret = usb_add_hcd(hcd, irq, 0);
  1865. if (ret != 0) {
  1866. err("Failed to add hcd");
  1867. goto clean_up;
  1868. }
  1869. return 0;
  1870. clean_up:
  1871. if (reg)
  1872. iounmap(reg);
  1873. return ret;
  1874. }
  1875. static struct platform_driver r8a66597_driver = {
  1876. .probe = r8a66597_probe,
  1877. .remove = r8a66597_remove,
  1878. .suspend = r8a66597_suspend,
  1879. .resume = r8a66597_resume,
  1880. .driver = {
  1881. .name = (char *) hcd_name,
  1882. },
  1883. };
  1884. static int __init r8a66597_init(void)
  1885. {
  1886. if (usb_disabled())
  1887. return -ENODEV;
  1888. info("driver %s, %s", hcd_name, DRIVER_VERSION);
  1889. return platform_driver_register(&r8a66597_driver);
  1890. }
  1891. module_init(r8a66597_init);
  1892. static void __exit r8a66597_cleanup(void)
  1893. {
  1894. platform_driver_unregister(&r8a66597_driver);
  1895. }
  1896. module_exit(r8a66597_cleanup);