sl811-hcd.c 46 KB

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