r8a66597-hcd.c 58 KB

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