r8a66597-hcd.c 56 KB

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