r8a66597-hcd.c 63 KB

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