r8a66597-hcd.c 56 KB

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