sl811-hcd.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /*
  2. * SL811HS HCD (Host Controller Driver) for USB.
  3. *
  4. * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
  5. * Copyright (C) 2004-2005 David Brownell
  6. *
  7. * Periodic scheduling is based on Roman's OHCI code
  8. * Copyright (C) 1999 Roman Weissgaerber
  9. *
  10. * The SL811HS controller handles host side USB (like the SL11H, but with
  11. * another register set and SOF generation) as well as peripheral side USB
  12. * (like the SL811S). This driver version doesn't implement the Gadget API
  13. * for the peripheral role; or OTG (that'd need much external circuitry).
  14. *
  15. * For documentation, see the SL811HS spec and the "SL811HS Embedded Host"
  16. * document (providing significant pieces missing from that spec); plus
  17. * the SL811S spec if you want peripheral side info.
  18. */
  19. /*
  20. * Status: Passed basic stress testing, works with hubs, mice, keyboards,
  21. * and usb-storage.
  22. *
  23. * TODO:
  24. * - usb suspend/resume triggered by sl811 (with USB_SUSPEND)
  25. * - various issues noted in the code
  26. * - performance work; use both register banks; ...
  27. * - use urb->iso_frame_desc[] with ISO transfers
  28. */
  29. #undef VERBOSE
  30. #undef PACKET_TRACE
  31. #include <linux/config.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/kernel.h>
  35. #include <linux/delay.h>
  36. #include <linux/ioport.h>
  37. #include <linux/sched.h>
  38. #include <linux/slab.h>
  39. #include <linux/smp_lock.h>
  40. #include <linux/errno.h>
  41. #include <linux/init.h>
  42. #include <linux/timer.h>
  43. #include <linux/list.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/usb.h>
  46. #include <linux/usb_sl811.h>
  47. #include <linux/platform_device.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <asm/system.h>
  51. #include <asm/byteorder.h>
  52. #include "../core/hcd.h"
  53. #include "sl811.h"
  54. MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
  55. MODULE_LICENSE("GPL");
  56. #define DRIVER_VERSION "19 May 2005"
  57. #ifndef DEBUG
  58. # define STUB_DEBUG_FILE
  59. #endif
  60. /* for now, use only one transfer register bank */
  61. #undef USE_B
  62. /* this doesn't understand urb->iso_frame_desc[], but if you had a driver
  63. * that just queued one ISO frame per URB then iso transfers "should" work
  64. * using the normal urb status fields.
  65. */
  66. #define DISABLE_ISO
  67. // #define QUIRK2
  68. #define QUIRK3
  69. static const char hcd_name[] = "sl811-hcd";
  70. /*-------------------------------------------------------------------------*/
  71. static void port_power(struct sl811 *sl811, int is_on)
  72. {
  73. struct usb_hcd *hcd = sl811_to_hcd(sl811);
  74. /* hub is inactive unless the port is powered */
  75. if (is_on) {
  76. if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
  77. return;
  78. sl811->port1 = (1 << USB_PORT_FEAT_POWER);
  79. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  80. hcd->self.controller->power.power_state = PMSG_ON;
  81. } else {
  82. sl811->port1 = 0;
  83. sl811->irq_enable = 0;
  84. hcd->state = HC_STATE_HALT;
  85. hcd->self.controller->power.power_state = PMSG_SUSPEND;
  86. }
  87. sl811->ctrl1 = 0;
  88. sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
  89. sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
  90. if (sl811->board && sl811->board->port_power) {
  91. /* switch VBUS, at 500mA unless hub power budget gets set */
  92. DBG("power %s\n", is_on ? "on" : "off");
  93. sl811->board->port_power(hcd->self.controller, is_on);
  94. }
  95. /* reset as thoroughly as we can */
  96. if (sl811->board && sl811->board->reset)
  97. sl811->board->reset(hcd->self.controller);
  98. else {
  99. sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
  100. mdelay(20);
  101. }
  102. sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
  103. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  104. sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
  105. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  106. // if !is_on, put into lowpower mode now
  107. }
  108. /*-------------------------------------------------------------------------*/
  109. /* This is a PIO-only HCD. Queueing appends URBs to the endpoint's queue,
  110. * and may start I/O. Endpoint queues are scanned during completion irq
  111. * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
  112. *
  113. * Using an external DMA engine to copy a packet at a time could work,
  114. * though setup/teardown costs may be too big to make it worthwhile.
  115. */
  116. /* SETUP starts a new control request. Devices are not allowed to
  117. * STALL or NAK these; they must cancel any pending control requests.
  118. */
  119. static void setup_packet(
  120. struct sl811 *sl811,
  121. struct sl811h_ep *ep,
  122. struct urb *urb,
  123. u8 bank,
  124. u8 control
  125. )
  126. {
  127. u8 addr;
  128. u8 len;
  129. void __iomem *data_reg;
  130. addr = SL811HS_PACKET_BUF(bank == 0);
  131. len = sizeof(struct usb_ctrlrequest);
  132. data_reg = sl811->data_reg;
  133. sl811_write_buf(sl811, addr, urb->setup_packet, len);
  134. /* autoincrementing */
  135. sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
  136. writeb(len, data_reg);
  137. writeb(SL_SETUP /* | ep->epnum */, data_reg);
  138. writeb(usb_pipedevice(urb->pipe), data_reg);
  139. /* always OUT/data0 */ ;
  140. sl811_write(sl811, bank + SL11H_HOSTCTLREG,
  141. control | SL11H_HCTLMASK_OUT);
  142. ep->length = 0;
  143. PACKET("SETUP qh%p\n", ep);
  144. }
  145. /* STATUS finishes control requests, often after IN or OUT data packets */
  146. static void status_packet(
  147. struct sl811 *sl811,
  148. struct sl811h_ep *ep,
  149. struct urb *urb,
  150. u8 bank,
  151. u8 control
  152. )
  153. {
  154. int do_out;
  155. void __iomem *data_reg;
  156. do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
  157. data_reg = sl811->data_reg;
  158. /* autoincrementing */
  159. sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
  160. writeb(0, data_reg);
  161. writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
  162. writeb(usb_pipedevice(urb->pipe), data_reg);
  163. /* always data1; sometimes IN */
  164. control |= SL11H_HCTLMASK_TOGGLE;
  165. if (do_out)
  166. control |= SL11H_HCTLMASK_OUT;
  167. sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
  168. ep->length = 0;
  169. PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
  170. do_out ? "out" : "in", ep);
  171. }
  172. /* IN packets can be used with any type of endpoint. here we just
  173. * start the transfer, data from the peripheral may arrive later.
  174. * urb->iso_frame_desc is currently ignored here...
  175. */
  176. static void in_packet(
  177. struct sl811 *sl811,
  178. struct sl811h_ep *ep,
  179. struct urb *urb,
  180. u8 bank,
  181. u8 control
  182. )
  183. {
  184. u8 addr;
  185. u8 len;
  186. void __iomem *data_reg;
  187. /* avoid losing data on overflow */
  188. len = ep->maxpacket;
  189. addr = SL811HS_PACKET_BUF(bank == 0);
  190. if (!(control & SL11H_HCTLMASK_ISOCH)
  191. && usb_gettoggle(urb->dev, ep->epnum, 0))
  192. control |= SL11H_HCTLMASK_TOGGLE;
  193. data_reg = sl811->data_reg;
  194. /* autoincrementing */
  195. sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
  196. writeb(len, data_reg);
  197. writeb(SL_IN | ep->epnum, data_reg);
  198. writeb(usb_pipedevice(urb->pipe), data_reg);
  199. sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
  200. ep->length = min((int)len,
  201. urb->transfer_buffer_length - urb->actual_length);
  202. PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
  203. !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
  204. }
  205. /* OUT packets can be used with any type of endpoint.
  206. * urb->iso_frame_desc is currently ignored here...
  207. */
  208. static void out_packet(
  209. struct sl811 *sl811,
  210. struct sl811h_ep *ep,
  211. struct urb *urb,
  212. u8 bank,
  213. u8 control
  214. )
  215. {
  216. void *buf;
  217. u8 addr;
  218. u8 len;
  219. void __iomem *data_reg;
  220. buf = urb->transfer_buffer + urb->actual_length;
  221. prefetch(buf);
  222. len = min((int)ep->maxpacket,
  223. urb->transfer_buffer_length - urb->actual_length);
  224. if (!(control & SL11H_HCTLMASK_ISOCH)
  225. && usb_gettoggle(urb->dev, ep->epnum, 1))
  226. control |= SL11H_HCTLMASK_TOGGLE;
  227. addr = SL811HS_PACKET_BUF(bank == 0);
  228. data_reg = sl811->data_reg;
  229. sl811_write_buf(sl811, addr, buf, len);
  230. /* autoincrementing */
  231. sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
  232. writeb(len, data_reg);
  233. writeb(SL_OUT | ep->epnum, data_reg);
  234. writeb(usb_pipedevice(urb->pipe), data_reg);
  235. sl811_write(sl811, bank + SL11H_HOSTCTLREG,
  236. control | SL11H_HCTLMASK_OUT);
  237. ep->length = len;
  238. PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
  239. !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
  240. }
  241. /*-------------------------------------------------------------------------*/
  242. /* caller updates on-chip enables later */
  243. static inline void sofirq_on(struct sl811 *sl811)
  244. {
  245. if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
  246. return;
  247. VDBG("sof irq on\n");
  248. sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
  249. }
  250. static inline void sofirq_off(struct sl811 *sl811)
  251. {
  252. if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
  253. return;
  254. VDBG("sof irq off\n");
  255. sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
  256. }
  257. /*-------------------------------------------------------------------------*/
  258. /* pick the next endpoint for a transaction, and issue it.
  259. * frames start with periodic transfers (after whatever is pending
  260. * from the previous frame), and the rest of the time is async
  261. * transfers, scheduled round-robin.
  262. */
  263. static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
  264. {
  265. struct sl811h_ep *ep;
  266. struct urb *urb;
  267. int fclock;
  268. u8 control;
  269. /* use endpoint at schedule head */
  270. if (sl811->next_periodic) {
  271. ep = sl811->next_periodic;
  272. sl811->next_periodic = ep->next;
  273. } else {
  274. if (sl811->next_async)
  275. ep = sl811->next_async;
  276. else if (!list_empty(&sl811->async))
  277. ep = container_of(sl811->async.next,
  278. struct sl811h_ep, schedule);
  279. else {
  280. /* could set up the first fullspeed periodic
  281. * transfer for the next frame ...
  282. */
  283. return NULL;
  284. }
  285. #ifdef USE_B
  286. if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
  287. return NULL;
  288. #endif
  289. if (ep->schedule.next == &sl811->async)
  290. sl811->next_async = NULL;
  291. else
  292. sl811->next_async = container_of(ep->schedule.next,
  293. struct sl811h_ep, schedule);
  294. }
  295. if (unlikely(list_empty(&ep->hep->urb_list))) {
  296. DBG("empty %p queue?\n", ep);
  297. return NULL;
  298. }
  299. urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
  300. control = ep->defctrl;
  301. /* if this frame doesn't have enough time left to transfer this
  302. * packet, wait till the next frame. too-simple algorithm...
  303. */
  304. fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
  305. fclock -= 100; /* setup takes not much time */
  306. if (urb->dev->speed == USB_SPEED_LOW) {
  307. if (control & SL11H_HCTLMASK_PREAMBLE) {
  308. /* also note erratum 1: some hubs won't work */
  309. fclock -= 800;
  310. }
  311. fclock -= ep->maxpacket << 8;
  312. /* erratum 2: AFTERSOF only works for fullspeed */
  313. if (fclock < 0) {
  314. if (ep->period)
  315. sl811->stat_overrun++;
  316. sofirq_on(sl811);
  317. return NULL;
  318. }
  319. } else {
  320. fclock -= 12000 / 19; /* 19 64byte packets/msec */
  321. if (fclock < 0) {
  322. if (ep->period)
  323. sl811->stat_overrun++;
  324. control |= SL11H_HCTLMASK_AFTERSOF;
  325. /* throttle bulk/control irq noise */
  326. } else if (ep->nak_count)
  327. control |= SL11H_HCTLMASK_AFTERSOF;
  328. }
  329. switch (ep->nextpid) {
  330. case USB_PID_IN:
  331. in_packet(sl811, ep, urb, bank, control);
  332. break;
  333. case USB_PID_OUT:
  334. out_packet(sl811, ep, urb, bank, control);
  335. break;
  336. case USB_PID_SETUP:
  337. setup_packet(sl811, ep, urb, bank, control);
  338. break;
  339. case USB_PID_ACK: /* for control status */
  340. status_packet(sl811, ep, urb, bank, control);
  341. break;
  342. default:
  343. DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
  344. ep = NULL;
  345. }
  346. return ep;
  347. }
  348. #define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
  349. static inline void start_transfer(struct sl811 *sl811)
  350. {
  351. if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
  352. return;
  353. if (sl811->active_a == NULL) {
  354. sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
  355. if (sl811->active_a != NULL)
  356. sl811->jiffies_a = jiffies + MIN_JIFFIES;
  357. }
  358. #ifdef USE_B
  359. if (sl811->active_b == NULL) {
  360. sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
  361. if (sl811->active_b != NULL)
  362. sl811->jiffies_b = jiffies + MIN_JIFFIES;
  363. }
  364. #endif
  365. }
  366. static void finish_request(
  367. struct sl811 *sl811,
  368. struct sl811h_ep *ep,
  369. struct urb *urb,
  370. struct pt_regs *regs,
  371. int status
  372. ) __releases(sl811->lock) __acquires(sl811->lock)
  373. {
  374. unsigned i;
  375. if (usb_pipecontrol(urb->pipe))
  376. ep->nextpid = USB_PID_SETUP;
  377. spin_lock(&urb->lock);
  378. if (urb->status == -EINPROGRESS)
  379. urb->status = status;
  380. urb->hcpriv = NULL;
  381. spin_unlock(&urb->lock);
  382. spin_unlock(&sl811->lock);
  383. usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, regs);
  384. spin_lock(&sl811->lock);
  385. /* leave active endpoints in the schedule */
  386. if (!list_empty(&ep->hep->urb_list))
  387. return;
  388. /* async deschedule? */
  389. if (!list_empty(&ep->schedule)) {
  390. list_del_init(&ep->schedule);
  391. if (ep == sl811->next_async)
  392. sl811->next_async = NULL;
  393. return;
  394. }
  395. /* periodic deschedule */
  396. DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
  397. for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
  398. struct sl811h_ep *temp;
  399. struct sl811h_ep **prev = &sl811->periodic[i];
  400. while (*prev && ((temp = *prev) != ep))
  401. prev = &temp->next;
  402. if (*prev)
  403. *prev = ep->next;
  404. sl811->load[i] -= ep->load;
  405. }
  406. ep->branch = PERIODIC_SIZE;
  407. sl811->periodic_count--;
  408. sl811_to_hcd(sl811)->self.bandwidth_allocated
  409. -= ep->load / ep->period;
  410. if (ep == sl811->next_periodic)
  411. sl811->next_periodic = ep->next;
  412. /* we might turn SOFs back on again for the async schedule */
  413. if (sl811->periodic_count == 0)
  414. sofirq_off(sl811);
  415. }
  416. static void
  417. done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank, struct pt_regs *regs)
  418. {
  419. u8 status;
  420. struct urb *urb;
  421. int urbstat = -EINPROGRESS;
  422. if (unlikely(!ep))
  423. return;
  424. status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
  425. urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
  426. /* we can safely ignore NAKs */
  427. if (status & SL11H_STATMASK_NAK) {
  428. // PACKET("...NAK_%02x qh%p\n", bank, ep);
  429. if (!ep->period)
  430. ep->nak_count++;
  431. ep->error_count = 0;
  432. /* ACK advances transfer, toggle, and maybe queue */
  433. } else if (status & SL11H_STATMASK_ACK) {
  434. struct usb_device *udev = urb->dev;
  435. int len;
  436. unsigned char *buf;
  437. /* urb->iso_frame_desc is currently ignored here... */
  438. ep->nak_count = ep->error_count = 0;
  439. switch (ep->nextpid) {
  440. case USB_PID_OUT:
  441. // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
  442. urb->actual_length += ep->length;
  443. usb_dotoggle(udev, ep->epnum, 1);
  444. if (urb->actual_length
  445. == urb->transfer_buffer_length) {
  446. if (usb_pipecontrol(urb->pipe))
  447. ep->nextpid = USB_PID_ACK;
  448. /* some bulk protocols terminate OUT transfers
  449. * by a short packet, using ZLPs not padding.
  450. */
  451. else if (ep->length < ep->maxpacket
  452. || !(urb->transfer_flags
  453. & URB_ZERO_PACKET))
  454. urbstat = 0;
  455. }
  456. break;
  457. case USB_PID_IN:
  458. // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
  459. buf = urb->transfer_buffer + urb->actual_length;
  460. prefetchw(buf);
  461. len = ep->maxpacket - sl811_read(sl811,
  462. bank + SL11H_XFERCNTREG);
  463. if (len > ep->length) {
  464. len = ep->length;
  465. urb->status = -EOVERFLOW;
  466. }
  467. urb->actual_length += len;
  468. sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
  469. buf, len);
  470. usb_dotoggle(udev, ep->epnum, 0);
  471. if (urb->actual_length == urb->transfer_buffer_length)
  472. urbstat = 0;
  473. else if (len < ep->maxpacket) {
  474. if (urb->transfer_flags & URB_SHORT_NOT_OK)
  475. urbstat = -EREMOTEIO;
  476. else
  477. urbstat = 0;
  478. }
  479. if (usb_pipecontrol(urb->pipe)
  480. && (urbstat == -EREMOTEIO
  481. || urbstat == 0)) {
  482. /* NOTE if the status stage STALLs (why?),
  483. * this reports the wrong urb status.
  484. */
  485. spin_lock(&urb->lock);
  486. if (urb->status == -EINPROGRESS)
  487. urb->status = urbstat;
  488. spin_unlock(&urb->lock);
  489. urb = NULL;
  490. ep->nextpid = USB_PID_ACK;
  491. }
  492. break;
  493. case USB_PID_SETUP:
  494. // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
  495. if (urb->transfer_buffer_length == urb->actual_length)
  496. ep->nextpid = USB_PID_ACK;
  497. else if (usb_pipeout(urb->pipe)) {
  498. usb_settoggle(udev, 0, 1, 1);
  499. ep->nextpid = USB_PID_OUT;
  500. } else {
  501. usb_settoggle(udev, 0, 0, 1);
  502. ep->nextpid = USB_PID_IN;
  503. }
  504. break;
  505. case USB_PID_ACK:
  506. // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
  507. urbstat = 0;
  508. break;
  509. }
  510. /* STALL stops all transfers */
  511. } else if (status & SL11H_STATMASK_STALL) {
  512. PACKET("...STALL_%02x qh%p\n", bank, ep);
  513. ep->nak_count = ep->error_count = 0;
  514. urbstat = -EPIPE;
  515. /* error? retry, until "3 strikes" */
  516. } else if (++ep->error_count >= 3) {
  517. if (status & SL11H_STATMASK_TMOUT)
  518. urbstat = -ETIMEDOUT;
  519. else if (status & SL11H_STATMASK_OVF)
  520. urbstat = -EOVERFLOW;
  521. else
  522. urbstat = -EPROTO;
  523. ep->error_count = 0;
  524. PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
  525. bank, status, ep, urbstat);
  526. }
  527. if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS))
  528. finish_request(sl811, ep, urb, regs, urbstat);
  529. }
  530. static inline u8 checkdone(struct sl811 *sl811)
  531. {
  532. u8 ctl;
  533. u8 irqstat = 0;
  534. if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
  535. ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
  536. if (ctl & SL11H_HCTLMASK_ARM)
  537. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
  538. DBG("%s DONE_A: ctrl %02x sts %02x\n",
  539. (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
  540. ctl,
  541. sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
  542. irqstat |= SL11H_INTMASK_DONE_A;
  543. }
  544. #ifdef USE_B
  545. if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
  546. ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
  547. if (ctl & SL11H_HCTLMASK_ARM)
  548. sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
  549. DBG("%s DONE_B: ctrl %02x sts %02x\n",
  550. (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
  551. ctl,
  552. sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
  553. irqstat |= SL11H_INTMASK_DONE_A;
  554. }
  555. #endif
  556. return irqstat;
  557. }
  558. static irqreturn_t sl811h_irq(struct usb_hcd *hcd, struct pt_regs *regs)
  559. {
  560. struct sl811 *sl811 = hcd_to_sl811(hcd);
  561. u8 irqstat;
  562. irqreturn_t ret = IRQ_NONE;
  563. unsigned retries = 5;
  564. spin_lock(&sl811->lock);
  565. retry:
  566. irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
  567. if (irqstat) {
  568. sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
  569. irqstat &= sl811->irq_enable;
  570. }
  571. #ifdef QUIRK2
  572. /* this may no longer be necessary ... */
  573. if (irqstat == 0) {
  574. irqstat = checkdone(sl811);
  575. if (irqstat)
  576. sl811->stat_lost++;
  577. }
  578. #endif
  579. /* USB packets, not necessarily handled in the order they're
  580. * issued ... that's fine if they're different endpoints.
  581. */
  582. if (irqstat & SL11H_INTMASK_DONE_A) {
  583. done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF), regs);
  584. sl811->active_a = NULL;
  585. sl811->stat_a++;
  586. }
  587. #ifdef USE_B
  588. if (irqstat & SL11H_INTMASK_DONE_B) {
  589. done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF), regs);
  590. sl811->active_b = NULL;
  591. sl811->stat_b++;
  592. }
  593. #endif
  594. if (irqstat & SL11H_INTMASK_SOFINTR) {
  595. unsigned index;
  596. index = sl811->frame++ % (PERIODIC_SIZE - 1);
  597. sl811->stat_sof++;
  598. /* be graceful about almost-inevitable periodic schedule
  599. * overruns: continue the previous frame's transfers iff
  600. * this one has nothing scheduled.
  601. */
  602. if (sl811->next_periodic) {
  603. // ERR("overrun to slot %d\n", index);
  604. sl811->stat_overrun++;
  605. }
  606. if (sl811->periodic[index])
  607. sl811->next_periodic = sl811->periodic[index];
  608. }
  609. /* khubd manages debouncing and wakeup */
  610. if (irqstat & SL11H_INTMASK_INSRMV) {
  611. sl811->stat_insrmv++;
  612. /* most stats are reset for each VBUS session */
  613. sl811->stat_wake = 0;
  614. sl811->stat_sof = 0;
  615. sl811->stat_a = 0;
  616. sl811->stat_b = 0;
  617. sl811->stat_lost = 0;
  618. sl811->ctrl1 = 0;
  619. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  620. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  621. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  622. /* usbcore nukes other pending transactions on disconnect */
  623. if (sl811->active_a) {
  624. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
  625. finish_request(sl811, sl811->active_a,
  626. container_of(sl811->active_a
  627. ->hep->urb_list.next,
  628. struct urb, urb_list),
  629. NULL, -ESHUTDOWN);
  630. sl811->active_a = NULL;
  631. }
  632. #ifdef USE_B
  633. if (sl811->active_b) {
  634. sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
  635. finish_request(sl811, sl811->active_b,
  636. container_of(sl811->active_b
  637. ->hep->urb_list.next,
  638. struct urb, urb_list),
  639. NULL, -ESHUTDOWN);
  640. sl811->active_b = NULL;
  641. }
  642. #endif
  643. /* port status seems weird until after reset, so
  644. * force the reset and make khubd clean up later.
  645. */
  646. sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
  647. | (1 << USB_PORT_FEAT_CONNECTION);
  648. } else if (irqstat & SL11H_INTMASK_RD) {
  649. if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
  650. DBG("wakeup\n");
  651. sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
  652. sl811->stat_wake++;
  653. } else
  654. irqstat &= ~SL11H_INTMASK_RD;
  655. }
  656. if (irqstat) {
  657. if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
  658. start_transfer(sl811);
  659. ret = IRQ_HANDLED;
  660. if (retries--)
  661. goto retry;
  662. }
  663. if (sl811->periodic_count == 0 && list_empty(&sl811->async))
  664. sofirq_off(sl811);
  665. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  666. spin_unlock(&sl811->lock);
  667. return ret;
  668. }
  669. /*-------------------------------------------------------------------------*/
  670. /* usb 1.1 says max 90% of a frame is available for periodic transfers.
  671. * this driver doesn't promise that much since it's got to handle an
  672. * IRQ per packet; irq handling latencies also use up that time.
  673. *
  674. * NOTE: the periodic schedule is a sparse tree, with the load for
  675. * each branch minimized. see fig 3.5 in the OHCI spec for example.
  676. */
  677. #define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */
  678. static int balance(struct sl811 *sl811, u16 period, u16 load)
  679. {
  680. int i, branch = -ENOSPC;
  681. /* search for the least loaded schedule branch of that period
  682. * which has enough bandwidth left unreserved.
  683. */
  684. for (i = 0; i < period ; i++) {
  685. if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
  686. int j;
  687. for (j = i; j < PERIODIC_SIZE; j += period) {
  688. if ((sl811->load[j] + load)
  689. > MAX_PERIODIC_LOAD)
  690. break;
  691. }
  692. if (j < PERIODIC_SIZE)
  693. continue;
  694. branch = i;
  695. }
  696. }
  697. return branch;
  698. }
  699. /*-------------------------------------------------------------------------*/
  700. static int sl811h_urb_enqueue(
  701. struct usb_hcd *hcd,
  702. struct usb_host_endpoint *hep,
  703. struct urb *urb,
  704. gfp_t mem_flags
  705. ) {
  706. struct sl811 *sl811 = hcd_to_sl811(hcd);
  707. struct usb_device *udev = urb->dev;
  708. unsigned int pipe = urb->pipe;
  709. int is_out = !usb_pipein(pipe);
  710. int type = usb_pipetype(pipe);
  711. int epnum = usb_pipeendpoint(pipe);
  712. struct sl811h_ep *ep = NULL;
  713. unsigned long flags;
  714. int i;
  715. int retval = 0;
  716. #ifdef DISABLE_ISO
  717. if (type == PIPE_ISOCHRONOUS)
  718. return -ENOSPC;
  719. #endif
  720. /* avoid all allocations within spinlocks */
  721. if (!hep->hcpriv)
  722. ep = kzalloc(sizeof *ep, mem_flags);
  723. spin_lock_irqsave(&sl811->lock, flags);
  724. /* don't submit to a dead or disabled port */
  725. if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
  726. || !HC_IS_RUNNING(hcd->state)) {
  727. retval = -ENODEV;
  728. kfree(ep);
  729. goto fail;
  730. }
  731. if (hep->hcpriv) {
  732. kfree(ep);
  733. ep = hep->hcpriv;
  734. } else if (!ep) {
  735. retval = -ENOMEM;
  736. goto fail;
  737. } else {
  738. INIT_LIST_HEAD(&ep->schedule);
  739. ep->udev = usb_get_dev(udev);
  740. ep->epnum = epnum;
  741. ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
  742. ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
  743. usb_settoggle(udev, epnum, is_out, 0);
  744. if (type == PIPE_CONTROL)
  745. ep->nextpid = USB_PID_SETUP;
  746. else if (is_out)
  747. ep->nextpid = USB_PID_OUT;
  748. else
  749. ep->nextpid = USB_PID_IN;
  750. if (ep->maxpacket > H_MAXPACKET) {
  751. /* iso packets up to 240 bytes could work... */
  752. DBG("dev %d ep%d maxpacket %d\n",
  753. udev->devnum, epnum, ep->maxpacket);
  754. retval = -EINVAL;
  755. goto fail;
  756. }
  757. if (udev->speed == USB_SPEED_LOW) {
  758. /* send preamble for external hub? */
  759. if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
  760. ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
  761. }
  762. switch (type) {
  763. case PIPE_ISOCHRONOUS:
  764. case PIPE_INTERRUPT:
  765. if (urb->interval > PERIODIC_SIZE)
  766. urb->interval = PERIODIC_SIZE;
  767. ep->period = urb->interval;
  768. ep->branch = PERIODIC_SIZE;
  769. if (type == PIPE_ISOCHRONOUS)
  770. ep->defctrl |= SL11H_HCTLMASK_ISOCH;
  771. ep->load = usb_calc_bus_time(udev->speed, !is_out,
  772. (type == PIPE_ISOCHRONOUS),
  773. usb_maxpacket(udev, pipe, is_out))
  774. / 1000;
  775. break;
  776. }
  777. ep->hep = hep;
  778. hep->hcpriv = ep;
  779. }
  780. /* maybe put endpoint into schedule */
  781. switch (type) {
  782. case PIPE_CONTROL:
  783. case PIPE_BULK:
  784. if (list_empty(&ep->schedule))
  785. list_add_tail(&ep->schedule, &sl811->async);
  786. break;
  787. case PIPE_ISOCHRONOUS:
  788. case PIPE_INTERRUPT:
  789. urb->interval = ep->period;
  790. if (ep->branch < PERIODIC_SIZE) {
  791. /* NOTE: the phase is correct here, but the value
  792. * needs offsetting by the transfer queue depth.
  793. * All current drivers ignore start_frame, so this
  794. * is unlikely to ever matter...
  795. */
  796. urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
  797. + ep->branch;
  798. break;
  799. }
  800. retval = balance(sl811, ep->period, ep->load);
  801. if (retval < 0)
  802. goto fail;
  803. ep->branch = retval;
  804. retval = 0;
  805. urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
  806. + ep->branch;
  807. /* sort each schedule branch by period (slow before fast)
  808. * to share the faster parts of the tree without needing
  809. * dummy/placeholder nodes
  810. */
  811. DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
  812. for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
  813. struct sl811h_ep **prev = &sl811->periodic[i];
  814. struct sl811h_ep *here = *prev;
  815. while (here && ep != here) {
  816. if (ep->period > here->period)
  817. break;
  818. prev = &here->next;
  819. here = *prev;
  820. }
  821. if (ep != here) {
  822. ep->next = here;
  823. *prev = ep;
  824. }
  825. sl811->load[i] += ep->load;
  826. }
  827. sl811->periodic_count++;
  828. hcd->self.bandwidth_allocated += ep->load / ep->period;
  829. sofirq_on(sl811);
  830. }
  831. /* in case of unlink-during-submit */
  832. spin_lock(&urb->lock);
  833. if (urb->status != -EINPROGRESS) {
  834. spin_unlock(&urb->lock);
  835. finish_request(sl811, ep, urb, NULL, 0);
  836. retval = 0;
  837. goto fail;
  838. }
  839. urb->hcpriv = hep;
  840. spin_unlock(&urb->lock);
  841. start_transfer(sl811);
  842. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  843. fail:
  844. spin_unlock_irqrestore(&sl811->lock, flags);
  845. return retval;
  846. }
  847. static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  848. {
  849. struct sl811 *sl811 = hcd_to_sl811(hcd);
  850. struct usb_host_endpoint *hep;
  851. unsigned long flags;
  852. struct sl811h_ep *ep;
  853. int retval = 0;
  854. spin_lock_irqsave(&sl811->lock, flags);
  855. hep = urb->hcpriv;
  856. if (!hep)
  857. goto fail;
  858. ep = hep->hcpriv;
  859. if (ep) {
  860. /* finish right away if this urb can't be active ...
  861. * note that some drivers wrongly expect delays
  862. */
  863. if (ep->hep->urb_list.next != &urb->urb_list) {
  864. /* not front of queue? never active */
  865. /* for active transfers, we expect an IRQ */
  866. } else if (sl811->active_a == ep) {
  867. if (time_before_eq(sl811->jiffies_a, jiffies)) {
  868. /* happens a lot with lowspeed?? */
  869. DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
  870. sl811_read(sl811,
  871. SL811_EP_A(SL11H_HOSTCTLREG)),
  872. sl811_read(sl811,
  873. SL811_EP_A(SL11H_PKTSTATREG)));
  874. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
  875. 0);
  876. sl811->active_a = NULL;
  877. } else
  878. urb = NULL;
  879. #ifdef USE_B
  880. } else if (sl811->active_b == ep) {
  881. if (time_before_eq(sl811->jiffies_a, jiffies)) {
  882. /* happens a lot with lowspeed?? */
  883. DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
  884. sl811_read(sl811,
  885. SL811_EP_B(SL11H_HOSTCTLREG)),
  886. sl811_read(sl811,
  887. SL811_EP_B(SL11H_PKTSTATREG)));
  888. sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
  889. 0);
  890. sl811->active_b = NULL;
  891. } else
  892. urb = NULL;
  893. #endif
  894. } else {
  895. /* front of queue for inactive endpoint */
  896. }
  897. if (urb)
  898. finish_request(sl811, ep, urb, NULL, 0);
  899. else
  900. VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
  901. (sl811->active_a == ep) ? "A" : "B");
  902. } else
  903. fail:
  904. retval = -EINVAL;
  905. spin_unlock_irqrestore(&sl811->lock, flags);
  906. return retval;
  907. }
  908. static void
  909. sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  910. {
  911. struct sl811h_ep *ep = hep->hcpriv;
  912. if (!ep)
  913. return;
  914. /* assume we'd just wait for the irq */
  915. if (!list_empty(&hep->urb_list))
  916. msleep(3);
  917. if (!list_empty(&hep->urb_list))
  918. WARN("ep %p not empty?\n", ep);
  919. usb_put_dev(ep->udev);
  920. kfree(ep);
  921. hep->hcpriv = NULL;
  922. }
  923. static int
  924. sl811h_get_frame(struct usb_hcd *hcd)
  925. {
  926. struct sl811 *sl811 = hcd_to_sl811(hcd);
  927. /* wrong except while periodic transfers are scheduled;
  928. * never matches the on-the-wire frame;
  929. * subject to overruns.
  930. */
  931. return sl811->frame;
  932. }
  933. /*-------------------------------------------------------------------------*/
  934. /* the virtual root hub timer IRQ checks for hub status */
  935. static int
  936. sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
  937. {
  938. struct sl811 *sl811 = hcd_to_sl811(hcd);
  939. #ifdef QUIRK3
  940. unsigned long flags;
  941. /* non-SMP HACK: use root hub timer as i/o watchdog
  942. * this seems essential when SOF IRQs aren't in use...
  943. */
  944. local_irq_save(flags);
  945. if (!timer_pending(&sl811->timer)) {
  946. if (sl811h_irq( /* ~0, */ hcd, NULL) != IRQ_NONE)
  947. sl811->stat_lost++;
  948. }
  949. local_irq_restore(flags);
  950. #endif
  951. if (!(sl811->port1 & (0xffff << 16)))
  952. return 0;
  953. /* tell khubd port 1 changed */
  954. *buf = (1 << 1);
  955. return 1;
  956. }
  957. static void
  958. sl811h_hub_descriptor (
  959. struct sl811 *sl811,
  960. struct usb_hub_descriptor *desc
  961. ) {
  962. u16 temp = 0;
  963. desc->bDescriptorType = 0x29;
  964. desc->bHubContrCurrent = 0;
  965. desc->bNbrPorts = 1;
  966. desc->bDescLength = 9;
  967. /* per-port power switching (gang of one!), or none */
  968. desc->bPwrOn2PwrGood = 0;
  969. if (sl811->board && sl811->board->port_power) {
  970. desc->bPwrOn2PwrGood = sl811->board->potpg;
  971. if (!desc->bPwrOn2PwrGood)
  972. desc->bPwrOn2PwrGood = 10;
  973. temp = 0x0001;
  974. } else
  975. temp = 0x0002;
  976. /* no overcurrent errors detection/handling */
  977. temp |= 0x0010;
  978. desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
  979. /* two bitmaps: ports removable, and legacy PortPwrCtrlMask */
  980. desc->bitmap[0] = 0 << 1;
  981. desc->bitmap[1] = ~0;
  982. }
  983. static void
  984. sl811h_timer(unsigned long _sl811)
  985. {
  986. struct sl811 *sl811 = (void *) _sl811;
  987. unsigned long flags;
  988. u8 irqstat;
  989. u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
  990. const u32 mask = (1 << USB_PORT_FEAT_CONNECTION)
  991. | (1 << USB_PORT_FEAT_ENABLE)
  992. | (1 << USB_PORT_FEAT_LOWSPEED);
  993. spin_lock_irqsave(&sl811->lock, flags);
  994. /* stop special signaling */
  995. sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
  996. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  997. udelay(3);
  998. irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
  999. switch (signaling) {
  1000. case SL11H_CTL1MASK_SE0:
  1001. DBG("end reset\n");
  1002. sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
  1003. | (1 << USB_PORT_FEAT_POWER);
  1004. sl811->ctrl1 = 0;
  1005. /* don't wrongly ack RD */
  1006. if (irqstat & SL11H_INTMASK_INSRMV)
  1007. irqstat &= ~SL11H_INTMASK_RD;
  1008. break;
  1009. case SL11H_CTL1MASK_K:
  1010. DBG("end resume\n");
  1011. sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
  1012. break;
  1013. default:
  1014. DBG("odd timer signaling: %02x\n", signaling);
  1015. break;
  1016. }
  1017. sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
  1018. if (irqstat & SL11H_INTMASK_RD) {
  1019. /* usbcore nukes all pending transactions on disconnect */
  1020. if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
  1021. sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
  1022. | (1 << USB_PORT_FEAT_C_ENABLE);
  1023. sl811->port1 &= ~mask;
  1024. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  1025. } else {
  1026. sl811->port1 |= mask;
  1027. if (irqstat & SL11H_INTMASK_DP)
  1028. sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
  1029. sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
  1030. }
  1031. if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
  1032. u8 ctrl2 = SL811HS_CTL2_INIT;
  1033. sl811->irq_enable |= SL11H_INTMASK_DONE_A;
  1034. #ifdef USE_B
  1035. sl811->irq_enable |= SL11H_INTMASK_DONE_B;
  1036. #endif
  1037. if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
  1038. sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
  1039. ctrl2 |= SL811HS_CTL2MASK_DSWAP;
  1040. }
  1041. /* start SOFs flowing, kickstarting with A registers */
  1042. sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
  1043. sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
  1044. sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
  1045. /* autoincrementing */
  1046. sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
  1047. writeb(SL_SOF, sl811->data_reg);
  1048. writeb(0, sl811->data_reg);
  1049. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
  1050. SL11H_HCTLMASK_ARM);
  1051. /* khubd provides debounce delay */
  1052. } else {
  1053. sl811->ctrl1 = 0;
  1054. }
  1055. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1056. /* reenable irqs */
  1057. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  1058. spin_unlock_irqrestore(&sl811->lock, flags);
  1059. }
  1060. static int
  1061. sl811h_hub_control(
  1062. struct usb_hcd *hcd,
  1063. u16 typeReq,
  1064. u16 wValue,
  1065. u16 wIndex,
  1066. char *buf,
  1067. u16 wLength
  1068. ) {
  1069. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1070. int retval = 0;
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&sl811->lock, flags);
  1073. switch (typeReq) {
  1074. case ClearHubFeature:
  1075. case SetHubFeature:
  1076. switch (wValue) {
  1077. case C_HUB_OVER_CURRENT:
  1078. case C_HUB_LOCAL_POWER:
  1079. break;
  1080. default:
  1081. goto error;
  1082. }
  1083. break;
  1084. case ClearPortFeature:
  1085. if (wIndex != 1 || wLength != 0)
  1086. goto error;
  1087. switch (wValue) {
  1088. case USB_PORT_FEAT_ENABLE:
  1089. sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
  1090. sl811->ctrl1 = 0;
  1091. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1092. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  1093. sl811_write(sl811, SL11H_IRQ_ENABLE,
  1094. sl811->irq_enable);
  1095. break;
  1096. case USB_PORT_FEAT_SUSPEND:
  1097. if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
  1098. break;
  1099. /* 20 msec of resume/K signaling, other irqs blocked */
  1100. DBG("start resume...\n");
  1101. sl811->irq_enable = 0;
  1102. sl811_write(sl811, SL11H_IRQ_ENABLE,
  1103. sl811->irq_enable);
  1104. sl811->ctrl1 |= SL11H_CTL1MASK_K;
  1105. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1106. mod_timer(&sl811->timer, jiffies
  1107. + msecs_to_jiffies(20));
  1108. break;
  1109. case USB_PORT_FEAT_POWER:
  1110. port_power(sl811, 0);
  1111. break;
  1112. case USB_PORT_FEAT_C_ENABLE:
  1113. case USB_PORT_FEAT_C_SUSPEND:
  1114. case USB_PORT_FEAT_C_CONNECTION:
  1115. case USB_PORT_FEAT_C_OVER_CURRENT:
  1116. case USB_PORT_FEAT_C_RESET:
  1117. break;
  1118. default:
  1119. goto error;
  1120. }
  1121. sl811->port1 &= ~(1 << wValue);
  1122. break;
  1123. case GetHubDescriptor:
  1124. sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
  1125. break;
  1126. case GetHubStatus:
  1127. *(__le32 *) buf = cpu_to_le32(0);
  1128. break;
  1129. case GetPortStatus:
  1130. if (wIndex != 1)
  1131. goto error;
  1132. *(__le32 *) buf = cpu_to_le32(sl811->port1);
  1133. #ifndef VERBOSE
  1134. if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
  1135. #endif
  1136. DBG("GetPortStatus %08x\n", sl811->port1);
  1137. break;
  1138. case SetPortFeature:
  1139. if (wIndex != 1 || wLength != 0)
  1140. goto error;
  1141. switch (wValue) {
  1142. case USB_PORT_FEAT_SUSPEND:
  1143. if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
  1144. goto error;
  1145. if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
  1146. goto error;
  1147. DBG("suspend...\n");
  1148. sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
  1149. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1150. break;
  1151. case USB_PORT_FEAT_POWER:
  1152. port_power(sl811, 1);
  1153. break;
  1154. case USB_PORT_FEAT_RESET:
  1155. if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
  1156. goto error;
  1157. if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
  1158. break;
  1159. /* 50 msec of reset/SE0 signaling, irqs blocked */
  1160. sl811->irq_enable = 0;
  1161. sl811_write(sl811, SL11H_IRQ_ENABLE,
  1162. sl811->irq_enable);
  1163. sl811->ctrl1 = SL11H_CTL1MASK_SE0;
  1164. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1165. sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
  1166. mod_timer(&sl811->timer, jiffies
  1167. + msecs_to_jiffies(50));
  1168. break;
  1169. default:
  1170. goto error;
  1171. }
  1172. sl811->port1 |= 1 << wValue;
  1173. break;
  1174. default:
  1175. error:
  1176. /* "protocol stall" on error */
  1177. retval = -EPIPE;
  1178. }
  1179. spin_unlock_irqrestore(&sl811->lock, flags);
  1180. return retval;
  1181. }
  1182. #ifdef CONFIG_PM
  1183. static int
  1184. sl811h_bus_suspend(struct usb_hcd *hcd)
  1185. {
  1186. // SOFs off
  1187. DBG("%s\n", __FUNCTION__);
  1188. return 0;
  1189. }
  1190. static int
  1191. sl811h_bus_resume(struct usb_hcd *hcd)
  1192. {
  1193. // SOFs on
  1194. DBG("%s\n", __FUNCTION__);
  1195. return 0;
  1196. }
  1197. #else
  1198. #define sl811h_bus_suspend NULL
  1199. #define sl811h_bus_resume NULL
  1200. #endif
  1201. /*-------------------------------------------------------------------------*/
  1202. #ifdef STUB_DEBUG_FILE
  1203. static inline void create_debug_file(struct sl811 *sl811) { }
  1204. static inline void remove_debug_file(struct sl811 *sl811) { }
  1205. #else
  1206. #include <linux/proc_fs.h>
  1207. #include <linux/seq_file.h>
  1208. static void dump_irq(struct seq_file *s, char *label, u8 mask)
  1209. {
  1210. seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
  1211. (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
  1212. (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
  1213. (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
  1214. (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
  1215. (mask & SL11H_INTMASK_RD) ? " rd" : "",
  1216. (mask & SL11H_INTMASK_DP) ? " dp" : "");
  1217. }
  1218. static int proc_sl811h_show(struct seq_file *s, void *unused)
  1219. {
  1220. struct sl811 *sl811 = s->private;
  1221. struct sl811h_ep *ep;
  1222. unsigned i;
  1223. seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
  1224. sl811_to_hcd(sl811)->product_desc,
  1225. hcd_name, DRIVER_VERSION,
  1226. sl811->port1);
  1227. seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
  1228. seq_printf(s, "current session: done_a %ld done_b %ld "
  1229. "wake %ld sof %ld overrun %ld lost %ld\n\n",
  1230. sl811->stat_a, sl811->stat_b,
  1231. sl811->stat_wake, sl811->stat_sof,
  1232. sl811->stat_overrun, sl811->stat_lost);
  1233. spin_lock_irq(&sl811->lock);
  1234. if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
  1235. seq_printf(s, "(suspended)\n\n");
  1236. else {
  1237. u8 t = sl811_read(sl811, SL11H_CTLREG1);
  1238. seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
  1239. (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
  1240. ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
  1241. case SL11H_CTL1MASK_NORMAL: s = ""; break;
  1242. case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
  1243. case SL11H_CTL1MASK_K: s = " k/resume"; break;
  1244. default: s = "j"; break;
  1245. }; s; }),
  1246. (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
  1247. (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
  1248. dump_irq(s, "irq_enable",
  1249. sl811_read(sl811, SL11H_IRQ_ENABLE));
  1250. dump_irq(s, "irq_status",
  1251. sl811_read(sl811, SL11H_IRQ_STATUS));
  1252. seq_printf(s, "frame clocks remaining: %d\n",
  1253. sl811_read(sl811, SL11H_SOFTMRREG) << 6);
  1254. }
  1255. seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
  1256. sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
  1257. sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
  1258. seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
  1259. sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
  1260. sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
  1261. seq_printf(s, "\n");
  1262. list_for_each_entry (ep, &sl811->async, schedule) {
  1263. struct urb *urb;
  1264. seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
  1265. " nak %d err %d\n",
  1266. (ep == sl811->active_a) ? "(A) " : "",
  1267. (ep == sl811->active_b) ? "(B) " : "",
  1268. ep, ep->epnum,
  1269. ({ char *s; switch (ep->nextpid) {
  1270. case USB_PID_IN: s = "in"; break;
  1271. case USB_PID_OUT: s = "out"; break;
  1272. case USB_PID_SETUP: s = "setup"; break;
  1273. case USB_PID_ACK: s = "status"; break;
  1274. default: s = "?"; break;
  1275. }; s;}),
  1276. ep->maxpacket,
  1277. ep->nak_count, ep->error_count);
  1278. list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
  1279. seq_printf(s, " urb%p, %d/%d\n", urb,
  1280. urb->actual_length,
  1281. urb->transfer_buffer_length);
  1282. }
  1283. }
  1284. if (!list_empty(&sl811->async))
  1285. seq_printf(s, "\n");
  1286. seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
  1287. for (i = 0; i < PERIODIC_SIZE; i++) {
  1288. ep = sl811->periodic[i];
  1289. if (!ep)
  1290. continue;
  1291. seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
  1292. /* DUMB: prints shared entries multiple times */
  1293. do {
  1294. seq_printf(s,
  1295. " %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
  1296. "err %d\n",
  1297. (ep == sl811->active_a) ? "(A) " : "",
  1298. (ep == sl811->active_b) ? "(B) " : "",
  1299. ep->period, ep,
  1300. (ep->udev->speed == USB_SPEED_FULL)
  1301. ? "" : "ls ",
  1302. ep->udev->devnum, ep->epnum,
  1303. (ep->epnum == 0) ? ""
  1304. : ((ep->nextpid == USB_PID_IN)
  1305. ? "in"
  1306. : "out"),
  1307. ep->maxpacket, ep->error_count);
  1308. ep = ep->next;
  1309. } while (ep);
  1310. }
  1311. spin_unlock_irq(&sl811->lock);
  1312. seq_printf(s, "\n");
  1313. return 0;
  1314. }
  1315. static int proc_sl811h_open(struct inode *inode, struct file *file)
  1316. {
  1317. return single_open(file, proc_sl811h_show, PDE(inode)->data);
  1318. }
  1319. static struct file_operations proc_ops = {
  1320. .open = proc_sl811h_open,
  1321. .read = seq_read,
  1322. .llseek = seq_lseek,
  1323. .release = single_release,
  1324. };
  1325. /* expect just one sl811 per system */
  1326. static const char proc_filename[] = "driver/sl811h";
  1327. static void create_debug_file(struct sl811 *sl811)
  1328. {
  1329. struct proc_dir_entry *pde;
  1330. pde = create_proc_entry(proc_filename, 0, NULL);
  1331. if (pde == NULL)
  1332. return;
  1333. pde->proc_fops = &proc_ops;
  1334. pde->data = sl811;
  1335. sl811->pde = pde;
  1336. }
  1337. static void remove_debug_file(struct sl811 *sl811)
  1338. {
  1339. if (sl811->pde)
  1340. remove_proc_entry(proc_filename, NULL);
  1341. }
  1342. #endif
  1343. /*-------------------------------------------------------------------------*/
  1344. static void
  1345. sl811h_stop(struct usb_hcd *hcd)
  1346. {
  1347. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1348. unsigned long flags;
  1349. del_timer_sync(&hcd->rh_timer);
  1350. spin_lock_irqsave(&sl811->lock, flags);
  1351. port_power(sl811, 0);
  1352. spin_unlock_irqrestore(&sl811->lock, flags);
  1353. }
  1354. static int
  1355. sl811h_start(struct usb_hcd *hcd)
  1356. {
  1357. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1358. /* chip has been reset, VBUS power is off */
  1359. hcd->state = HC_STATE_RUNNING;
  1360. if (sl811->board) {
  1361. if (!device_can_wakeup(hcd->self.controller))
  1362. device_init_wakeup(hcd->self.controller,
  1363. sl811->board->can_wakeup);
  1364. hcd->power_budget = sl811->board->power * 2;
  1365. }
  1366. /* enable power and interupts */
  1367. port_power(sl811, 1);
  1368. return 0;
  1369. }
  1370. /*-------------------------------------------------------------------------*/
  1371. static struct hc_driver sl811h_hc_driver = {
  1372. .description = hcd_name,
  1373. .hcd_priv_size = sizeof(struct sl811),
  1374. /*
  1375. * generic hardware linkage
  1376. */
  1377. .irq = sl811h_irq,
  1378. .flags = HCD_USB11 | HCD_MEMORY,
  1379. /* Basic lifecycle operations */
  1380. .start = sl811h_start,
  1381. .stop = sl811h_stop,
  1382. /*
  1383. * managing i/o requests and associated device resources
  1384. */
  1385. .urb_enqueue = sl811h_urb_enqueue,
  1386. .urb_dequeue = sl811h_urb_dequeue,
  1387. .endpoint_disable = sl811h_endpoint_disable,
  1388. /*
  1389. * periodic schedule support
  1390. */
  1391. .get_frame_number = sl811h_get_frame,
  1392. /*
  1393. * root hub support
  1394. */
  1395. .hub_status_data = sl811h_hub_status_data,
  1396. .hub_control = sl811h_hub_control,
  1397. .bus_suspend = sl811h_bus_suspend,
  1398. .bus_resume = sl811h_bus_resume,
  1399. };
  1400. /*-------------------------------------------------------------------------*/
  1401. static int __devexit
  1402. sl811h_remove(struct platform_device *dev)
  1403. {
  1404. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1405. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1406. struct resource *res;
  1407. remove_debug_file(sl811);
  1408. usb_remove_hcd(hcd);
  1409. /* some platforms may use IORESOURCE_IO */
  1410. res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  1411. if (res)
  1412. iounmap(sl811->data_reg);
  1413. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1414. if (res)
  1415. iounmap(sl811->addr_reg);
  1416. usb_put_hcd(hcd);
  1417. return 0;
  1418. }
  1419. static int __devinit
  1420. sl811h_probe(struct platform_device *dev)
  1421. {
  1422. struct usb_hcd *hcd;
  1423. struct sl811 *sl811;
  1424. struct resource *addr, *data;
  1425. int irq;
  1426. void __iomem *addr_reg;
  1427. void __iomem *data_reg;
  1428. int retval;
  1429. u8 tmp, ioaddr = 0;
  1430. /* basic sanity checks first. board-specific init logic should
  1431. * have initialized these three resources and probably board
  1432. * specific platform_data. we don't probe for IRQs, and do only
  1433. * minimal sanity checking.
  1434. */
  1435. irq = platform_get_irq(dev, 0);
  1436. if (dev->num_resources < 3 || irq < 0)
  1437. return -ENODEV;
  1438. /* refuse to confuse usbcore */
  1439. if (dev->dev.dma_mask) {
  1440. DBG("no we won't dma\n");
  1441. return -EINVAL;
  1442. }
  1443. /* the chip may be wired for either kind of addressing */
  1444. addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1445. data = platform_get_resource(dev, IORESOURCE_MEM, 1);
  1446. retval = -EBUSY;
  1447. if (!addr || !data) {
  1448. addr = platform_get_resource(dev, IORESOURCE_IO, 0);
  1449. data = platform_get_resource(dev, IORESOURCE_IO, 1);
  1450. if (!addr || !data)
  1451. return -ENODEV;
  1452. ioaddr = 1;
  1453. addr_reg = (void __iomem *) addr->start;
  1454. data_reg = (void __iomem *) data->start;
  1455. } else {
  1456. addr_reg = ioremap(addr->start, 1);
  1457. if (addr_reg == NULL) {
  1458. retval = -ENOMEM;
  1459. goto err2;
  1460. }
  1461. data_reg = ioremap(data->start, 1);
  1462. if (data_reg == NULL) {
  1463. retval = -ENOMEM;
  1464. goto err4;
  1465. }
  1466. }
  1467. /* allocate and initialize hcd */
  1468. hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev->dev.bus_id);
  1469. if (!hcd) {
  1470. retval = -ENOMEM;
  1471. goto err5;
  1472. }
  1473. hcd->rsrc_start = addr->start;
  1474. sl811 = hcd_to_sl811(hcd);
  1475. spin_lock_init(&sl811->lock);
  1476. INIT_LIST_HEAD(&sl811->async);
  1477. sl811->board = dev->dev.platform_data;
  1478. init_timer(&sl811->timer);
  1479. sl811->timer.function = sl811h_timer;
  1480. sl811->timer.data = (unsigned long) sl811;
  1481. sl811->addr_reg = addr_reg;
  1482. sl811->data_reg = data_reg;
  1483. spin_lock_irq(&sl811->lock);
  1484. port_power(sl811, 0);
  1485. spin_unlock_irq(&sl811->lock);
  1486. msleep(200);
  1487. tmp = sl811_read(sl811, SL11H_HWREVREG);
  1488. switch (tmp >> 4) {
  1489. case 1:
  1490. hcd->product_desc = "SL811HS v1.2";
  1491. break;
  1492. case 2:
  1493. hcd->product_desc = "SL811HS v1.5";
  1494. break;
  1495. default:
  1496. /* reject case 0, SL11S is less functional */
  1497. DBG("chiprev %02x\n", tmp);
  1498. retval = -ENXIO;
  1499. goto err6;
  1500. }
  1501. /* The chip's IRQ is level triggered, active high. A requirement
  1502. * for platform device setup is to cope with things like signal
  1503. * inverters (e.g. CF is active low) or working only with edge
  1504. * triggers (e.g. most ARM CPUs). Initial driver stress testing
  1505. * was on a system with single edge triggering, so most sorts of
  1506. * triggering arrangement should work.
  1507. */
  1508. retval = usb_add_hcd(hcd, irq, SA_INTERRUPT | SA_SHIRQ);
  1509. if (retval != 0)
  1510. goto err6;
  1511. create_debug_file(sl811);
  1512. return retval;
  1513. err6:
  1514. usb_put_hcd(hcd);
  1515. err5:
  1516. if (!ioaddr)
  1517. iounmap(data_reg);
  1518. err4:
  1519. if (!ioaddr)
  1520. iounmap(addr_reg);
  1521. err2:
  1522. DBG("init error, %d\n", retval);
  1523. return retval;
  1524. }
  1525. #ifdef CONFIG_PM
  1526. /* for this device there's no useful distinction between the controller
  1527. * and its root hub, except that the root hub only gets direct PM calls
  1528. * when CONFIG_USB_SUSPEND is enabled.
  1529. */
  1530. static int
  1531. sl811h_suspend(struct platform_device *dev, pm_message_t state)
  1532. {
  1533. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1534. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1535. int retval = 0;
  1536. if (state.event == PM_EVENT_FREEZE)
  1537. retval = sl811h_bus_suspend(hcd);
  1538. else if (state.event == PM_EVENT_SUSPEND)
  1539. port_power(sl811, 0);
  1540. if (retval == 0)
  1541. dev->dev.power.power_state = state;
  1542. return retval;
  1543. }
  1544. static int
  1545. sl811h_resume(struct platform_device *dev)
  1546. {
  1547. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1548. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1549. /* with no "check to see if VBUS is still powered" board hook,
  1550. * let's assume it'd only be powered to enable remote wakeup.
  1551. */
  1552. if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND
  1553. || !device_can_wakeup(&hcd->self.root_hub->dev)) {
  1554. sl811->port1 = 0;
  1555. port_power(sl811, 1);
  1556. usb_root_hub_lost_power(hcd->self.root_hub);
  1557. return 0;
  1558. }
  1559. dev->dev.power.power_state = PMSG_ON;
  1560. return sl811h_bus_resume(hcd);
  1561. }
  1562. #else
  1563. #define sl811h_suspend NULL
  1564. #define sl811h_resume NULL
  1565. #endif
  1566. /* this driver is exported so sl811_cs can depend on it */
  1567. struct platform_driver sl811h_driver = {
  1568. .probe = sl811h_probe,
  1569. .remove = __devexit_p(sl811h_remove),
  1570. .suspend = sl811h_suspend,
  1571. .resume = sl811h_resume,
  1572. .driver = {
  1573. .name = (char *) hcd_name,
  1574. .owner = THIS_MODULE,
  1575. },
  1576. };
  1577. EXPORT_SYMBOL(sl811h_driver);
  1578. /*-------------------------------------------------------------------------*/
  1579. static int __init sl811h_init(void)
  1580. {
  1581. if (usb_disabled())
  1582. return -ENODEV;
  1583. INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
  1584. return platform_driver_register(&sl811h_driver);
  1585. }
  1586. module_init(sl811h_init);
  1587. static void __exit sl811h_cleanup(void)
  1588. {
  1589. platform_driver_unregister(&sl811h_driver);
  1590. }
  1591. module_exit(sl811h_cleanup);