r8a66597-hcd.c 62 KB

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