sl811-hcd.c 46 KB

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