sl811-hcd.c 46 KB

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