r8a66597-hcd.c 63 KB

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