r8a66597-hcd.c 63 KB

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