sl811-hcd.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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. urb->hcpriv = NULL;
  378. spin_unlock(&urb->lock);
  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. urbstat = 0;
  470. else if (len < ep->maxpacket) {
  471. if (urb->transfer_flags & URB_SHORT_NOT_OK)
  472. urbstat = -EREMOTEIO;
  473. else
  474. urbstat = 0;
  475. }
  476. if (usb_pipecontrol(urb->pipe)
  477. && (urbstat == -EREMOTEIO
  478. || urbstat == 0)) {
  479. /* NOTE if the status stage STALLs (why?),
  480. * this reports the wrong urb status.
  481. */
  482. spin_lock(&urb->lock);
  483. if (urb->status == -EINPROGRESS)
  484. urb->status = urbstat;
  485. spin_unlock(&urb->lock);
  486. urb = NULL;
  487. ep->nextpid = USB_PID_ACK;
  488. }
  489. break;
  490. case USB_PID_SETUP:
  491. // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
  492. if (urb->transfer_buffer_length == urb->actual_length)
  493. ep->nextpid = USB_PID_ACK;
  494. else if (usb_pipeout(urb->pipe)) {
  495. usb_settoggle(udev, 0, 1, 1);
  496. ep->nextpid = USB_PID_OUT;
  497. } else {
  498. usb_settoggle(udev, 0, 0, 1);
  499. ep->nextpid = USB_PID_IN;
  500. }
  501. break;
  502. case USB_PID_ACK:
  503. // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
  504. urbstat = 0;
  505. break;
  506. }
  507. /* STALL stops all transfers */
  508. } else if (status & SL11H_STATMASK_STALL) {
  509. PACKET("...STALL_%02x qh%p\n", bank, ep);
  510. ep->nak_count = ep->error_count = 0;
  511. urbstat = -EPIPE;
  512. /* error? retry, until "3 strikes" */
  513. } else if (++ep->error_count >= 3) {
  514. if (status & SL11H_STATMASK_TMOUT)
  515. urbstat = -ETIME;
  516. else if (status & SL11H_STATMASK_OVF)
  517. urbstat = -EOVERFLOW;
  518. else
  519. urbstat = -EPROTO;
  520. ep->error_count = 0;
  521. PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
  522. bank, status, ep, urbstat);
  523. }
  524. if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS))
  525. finish_request(sl811, ep, urb, urbstat);
  526. }
  527. static inline u8 checkdone(struct sl811 *sl811)
  528. {
  529. u8 ctl;
  530. u8 irqstat = 0;
  531. if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
  532. ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
  533. if (ctl & SL11H_HCTLMASK_ARM)
  534. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
  535. DBG("%s DONE_A: ctrl %02x sts %02x\n",
  536. (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
  537. ctl,
  538. sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
  539. irqstat |= SL11H_INTMASK_DONE_A;
  540. }
  541. #ifdef USE_B
  542. if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
  543. ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
  544. if (ctl & SL11H_HCTLMASK_ARM)
  545. sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
  546. DBG("%s DONE_B: ctrl %02x sts %02x\n",
  547. (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
  548. ctl,
  549. sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
  550. irqstat |= SL11H_INTMASK_DONE_A;
  551. }
  552. #endif
  553. return irqstat;
  554. }
  555. static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
  556. {
  557. struct sl811 *sl811 = hcd_to_sl811(hcd);
  558. u8 irqstat;
  559. irqreturn_t ret = IRQ_NONE;
  560. unsigned retries = 5;
  561. spin_lock(&sl811->lock);
  562. retry:
  563. irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
  564. if (irqstat) {
  565. sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
  566. irqstat &= sl811->irq_enable;
  567. }
  568. #ifdef QUIRK2
  569. /* this may no longer be necessary ... */
  570. if (irqstat == 0) {
  571. irqstat = checkdone(sl811);
  572. if (irqstat)
  573. sl811->stat_lost++;
  574. }
  575. #endif
  576. /* USB packets, not necessarily handled in the order they're
  577. * issued ... that's fine if they're different endpoints.
  578. */
  579. if (irqstat & SL11H_INTMASK_DONE_A) {
  580. done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
  581. sl811->active_a = NULL;
  582. sl811->stat_a++;
  583. }
  584. #ifdef USE_B
  585. if (irqstat & SL11H_INTMASK_DONE_B) {
  586. done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
  587. sl811->active_b = NULL;
  588. sl811->stat_b++;
  589. }
  590. #endif
  591. if (irqstat & SL11H_INTMASK_SOFINTR) {
  592. unsigned index;
  593. index = sl811->frame++ % (PERIODIC_SIZE - 1);
  594. sl811->stat_sof++;
  595. /* be graceful about almost-inevitable periodic schedule
  596. * overruns: continue the previous frame's transfers iff
  597. * this one has nothing scheduled.
  598. */
  599. if (sl811->next_periodic) {
  600. // ERR("overrun to slot %d\n", index);
  601. sl811->stat_overrun++;
  602. }
  603. if (sl811->periodic[index])
  604. sl811->next_periodic = sl811->periodic[index];
  605. }
  606. /* khubd manages debouncing and wakeup */
  607. if (irqstat & SL11H_INTMASK_INSRMV) {
  608. sl811->stat_insrmv++;
  609. /* most stats are reset for each VBUS session */
  610. sl811->stat_wake = 0;
  611. sl811->stat_sof = 0;
  612. sl811->stat_a = 0;
  613. sl811->stat_b = 0;
  614. sl811->stat_lost = 0;
  615. sl811->ctrl1 = 0;
  616. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  617. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  618. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  619. /* usbcore nukes other pending transactions on disconnect */
  620. if (sl811->active_a) {
  621. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
  622. finish_request(sl811, sl811->active_a,
  623. container_of(sl811->active_a
  624. ->hep->urb_list.next,
  625. struct urb, urb_list),
  626. -ESHUTDOWN);
  627. sl811->active_a = NULL;
  628. }
  629. #ifdef USE_B
  630. if (sl811->active_b) {
  631. sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
  632. finish_request(sl811, sl811->active_b,
  633. container_of(sl811->active_b
  634. ->hep->urb_list.next,
  635. struct urb, urb_list),
  636. NULL, -ESHUTDOWN);
  637. sl811->active_b = NULL;
  638. }
  639. #endif
  640. /* port status seems weird until after reset, so
  641. * force the reset and make khubd clean up later.
  642. */
  643. sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
  644. | (1 << USB_PORT_FEAT_CONNECTION);
  645. } else if (irqstat & SL11H_INTMASK_RD) {
  646. if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
  647. DBG("wakeup\n");
  648. sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
  649. sl811->stat_wake++;
  650. } else
  651. irqstat &= ~SL11H_INTMASK_RD;
  652. }
  653. if (irqstat) {
  654. if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
  655. start_transfer(sl811);
  656. ret = IRQ_HANDLED;
  657. if (retries--)
  658. goto retry;
  659. }
  660. if (sl811->periodic_count == 0 && list_empty(&sl811->async))
  661. sofirq_off(sl811);
  662. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  663. spin_unlock(&sl811->lock);
  664. return ret;
  665. }
  666. /*-------------------------------------------------------------------------*/
  667. /* usb 1.1 says max 90% of a frame is available for periodic transfers.
  668. * this driver doesn't promise that much since it's got to handle an
  669. * IRQ per packet; irq handling latencies also use up that time.
  670. *
  671. * NOTE: the periodic schedule is a sparse tree, with the load for
  672. * each branch minimized. see fig 3.5 in the OHCI spec for example.
  673. */
  674. #define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */
  675. static int balance(struct sl811 *sl811, u16 period, u16 load)
  676. {
  677. int i, branch = -ENOSPC;
  678. /* search for the least loaded schedule branch of that period
  679. * which has enough bandwidth left unreserved.
  680. */
  681. for (i = 0; i < period ; i++) {
  682. if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
  683. int j;
  684. for (j = i; j < PERIODIC_SIZE; j += period) {
  685. if ((sl811->load[j] + load)
  686. > MAX_PERIODIC_LOAD)
  687. break;
  688. }
  689. if (j < PERIODIC_SIZE)
  690. continue;
  691. branch = i;
  692. }
  693. }
  694. return branch;
  695. }
  696. /*-------------------------------------------------------------------------*/
  697. static int sl811h_urb_enqueue(
  698. struct usb_hcd *hcd,
  699. struct usb_host_endpoint *hep,
  700. struct urb *urb,
  701. gfp_t mem_flags
  702. ) {
  703. struct sl811 *sl811 = hcd_to_sl811(hcd);
  704. struct usb_device *udev = urb->dev;
  705. unsigned int pipe = urb->pipe;
  706. int is_out = !usb_pipein(pipe);
  707. int type = usb_pipetype(pipe);
  708. int epnum = usb_pipeendpoint(pipe);
  709. struct sl811h_ep *ep = NULL;
  710. unsigned long flags;
  711. int i;
  712. int retval = 0;
  713. #ifdef DISABLE_ISO
  714. if (type == PIPE_ISOCHRONOUS)
  715. return -ENOSPC;
  716. #endif
  717. /* avoid all allocations within spinlocks */
  718. if (!hep->hcpriv)
  719. ep = kzalloc(sizeof *ep, mem_flags);
  720. spin_lock_irqsave(&sl811->lock, flags);
  721. /* don't submit to a dead or disabled port */
  722. if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
  723. || !HC_IS_RUNNING(hcd->state)) {
  724. retval = -ENODEV;
  725. kfree(ep);
  726. goto fail;
  727. }
  728. if (hep->hcpriv) {
  729. kfree(ep);
  730. ep = hep->hcpriv;
  731. } else if (!ep) {
  732. retval = -ENOMEM;
  733. goto fail;
  734. } else {
  735. INIT_LIST_HEAD(&ep->schedule);
  736. ep->udev = udev;
  737. ep->epnum = epnum;
  738. ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
  739. ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
  740. usb_settoggle(udev, epnum, is_out, 0);
  741. if (type == PIPE_CONTROL)
  742. ep->nextpid = USB_PID_SETUP;
  743. else if (is_out)
  744. ep->nextpid = USB_PID_OUT;
  745. else
  746. ep->nextpid = USB_PID_IN;
  747. if (ep->maxpacket > H_MAXPACKET) {
  748. /* iso packets up to 240 bytes could work... */
  749. DBG("dev %d ep%d maxpacket %d\n",
  750. udev->devnum, epnum, ep->maxpacket);
  751. retval = -EINVAL;
  752. goto fail;
  753. }
  754. if (udev->speed == USB_SPEED_LOW) {
  755. /* send preamble for external hub? */
  756. if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
  757. ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
  758. }
  759. switch (type) {
  760. case PIPE_ISOCHRONOUS:
  761. case PIPE_INTERRUPT:
  762. if (urb->interval > PERIODIC_SIZE)
  763. urb->interval = PERIODIC_SIZE;
  764. ep->period = urb->interval;
  765. ep->branch = PERIODIC_SIZE;
  766. if (type == PIPE_ISOCHRONOUS)
  767. ep->defctrl |= SL11H_HCTLMASK_ISOCH;
  768. ep->load = usb_calc_bus_time(udev->speed, !is_out,
  769. (type == PIPE_ISOCHRONOUS),
  770. usb_maxpacket(udev, pipe, is_out))
  771. / 1000;
  772. break;
  773. }
  774. ep->hep = hep;
  775. hep->hcpriv = ep;
  776. }
  777. /* maybe put endpoint into schedule */
  778. switch (type) {
  779. case PIPE_CONTROL:
  780. case PIPE_BULK:
  781. if (list_empty(&ep->schedule))
  782. list_add_tail(&ep->schedule, &sl811->async);
  783. break;
  784. case PIPE_ISOCHRONOUS:
  785. case PIPE_INTERRUPT:
  786. urb->interval = ep->period;
  787. if (ep->branch < PERIODIC_SIZE) {
  788. /* NOTE: the phase is correct here, but the value
  789. * needs offsetting by the transfer queue depth.
  790. * All current drivers ignore start_frame, so this
  791. * is unlikely to ever matter...
  792. */
  793. urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
  794. + ep->branch;
  795. break;
  796. }
  797. retval = balance(sl811, ep->period, ep->load);
  798. if (retval < 0)
  799. goto fail;
  800. ep->branch = retval;
  801. retval = 0;
  802. urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
  803. + ep->branch;
  804. /* sort each schedule branch by period (slow before fast)
  805. * to share the faster parts of the tree without needing
  806. * dummy/placeholder nodes
  807. */
  808. DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
  809. for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
  810. struct sl811h_ep **prev = &sl811->periodic[i];
  811. struct sl811h_ep *here = *prev;
  812. while (here && ep != here) {
  813. if (ep->period > here->period)
  814. break;
  815. prev = &here->next;
  816. here = *prev;
  817. }
  818. if (ep != here) {
  819. ep->next = here;
  820. *prev = ep;
  821. }
  822. sl811->load[i] += ep->load;
  823. }
  824. sl811->periodic_count++;
  825. hcd->self.bandwidth_allocated += ep->load / ep->period;
  826. sofirq_on(sl811);
  827. }
  828. /* in case of unlink-during-submit */
  829. spin_lock(&urb->lock);
  830. if (urb->status != -EINPROGRESS) {
  831. spin_unlock(&urb->lock);
  832. finish_request(sl811, ep, urb, 0);
  833. retval = 0;
  834. goto fail;
  835. }
  836. urb->hcpriv = hep;
  837. spin_unlock(&urb->lock);
  838. start_transfer(sl811);
  839. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  840. fail:
  841. spin_unlock_irqrestore(&sl811->lock, flags);
  842. return retval;
  843. }
  844. static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  845. {
  846. struct sl811 *sl811 = hcd_to_sl811(hcd);
  847. struct usb_host_endpoint *hep;
  848. unsigned long flags;
  849. struct sl811h_ep *ep;
  850. int retval = 0;
  851. spin_lock_irqsave(&sl811->lock, flags);
  852. hep = urb->hcpriv;
  853. if (!hep)
  854. goto fail;
  855. ep = hep->hcpriv;
  856. if (ep) {
  857. /* finish right away if this urb can't be active ...
  858. * note that some drivers wrongly expect delays
  859. */
  860. if (ep->hep->urb_list.next != &urb->urb_list) {
  861. /* not front of queue? never active */
  862. /* for active transfers, we expect an IRQ */
  863. } else if (sl811->active_a == ep) {
  864. if (time_before_eq(sl811->jiffies_a, jiffies)) {
  865. /* happens a lot with lowspeed?? */
  866. DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
  867. sl811_read(sl811,
  868. SL811_EP_A(SL11H_HOSTCTLREG)),
  869. sl811_read(sl811,
  870. SL811_EP_A(SL11H_PKTSTATREG)));
  871. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
  872. 0);
  873. sl811->active_a = NULL;
  874. } else
  875. urb = NULL;
  876. #ifdef USE_B
  877. } else if (sl811->active_b == ep) {
  878. if (time_before_eq(sl811->jiffies_a, jiffies)) {
  879. /* happens a lot with lowspeed?? */
  880. DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
  881. sl811_read(sl811,
  882. SL811_EP_B(SL11H_HOSTCTLREG)),
  883. sl811_read(sl811,
  884. SL811_EP_B(SL11H_PKTSTATREG)));
  885. sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
  886. 0);
  887. sl811->active_b = NULL;
  888. } else
  889. urb = NULL;
  890. #endif
  891. } else {
  892. /* front of queue for inactive endpoint */
  893. }
  894. if (urb)
  895. finish_request(sl811, ep, urb, 0);
  896. else
  897. VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
  898. (sl811->active_a == ep) ? "A" : "B");
  899. } else
  900. fail:
  901. retval = -EINVAL;
  902. spin_unlock_irqrestore(&sl811->lock, flags);
  903. return retval;
  904. }
  905. static void
  906. sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  907. {
  908. struct sl811h_ep *ep = hep->hcpriv;
  909. if (!ep)
  910. return;
  911. /* assume we'd just wait for the irq */
  912. if (!list_empty(&hep->urb_list))
  913. msleep(3);
  914. if (!list_empty(&hep->urb_list))
  915. WARN("ep %p not empty?\n", ep);
  916. kfree(ep);
  917. hep->hcpriv = NULL;
  918. }
  919. static int
  920. sl811h_get_frame(struct usb_hcd *hcd)
  921. {
  922. struct sl811 *sl811 = hcd_to_sl811(hcd);
  923. /* wrong except while periodic transfers are scheduled;
  924. * never matches the on-the-wire frame;
  925. * subject to overruns.
  926. */
  927. return sl811->frame;
  928. }
  929. /*-------------------------------------------------------------------------*/
  930. /* the virtual root hub timer IRQ checks for hub status */
  931. static int
  932. sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
  933. {
  934. struct sl811 *sl811 = hcd_to_sl811(hcd);
  935. #ifdef QUIRK3
  936. unsigned long flags;
  937. /* non-SMP HACK: use root hub timer as i/o watchdog
  938. * this seems essential when SOF IRQs aren't in use...
  939. */
  940. local_irq_save(flags);
  941. if (!timer_pending(&sl811->timer)) {
  942. if (sl811h_irq( /* ~0, */ hcd) != IRQ_NONE)
  943. sl811->stat_lost++;
  944. }
  945. local_irq_restore(flags);
  946. #endif
  947. if (!(sl811->port1 & (0xffff << 16)))
  948. return 0;
  949. /* tell khubd port 1 changed */
  950. *buf = (1 << 1);
  951. return 1;
  952. }
  953. static void
  954. sl811h_hub_descriptor (
  955. struct sl811 *sl811,
  956. struct usb_hub_descriptor *desc
  957. ) {
  958. u16 temp = 0;
  959. desc->bDescriptorType = 0x29;
  960. desc->bHubContrCurrent = 0;
  961. desc->bNbrPorts = 1;
  962. desc->bDescLength = 9;
  963. /* per-port power switching (gang of one!), or none */
  964. desc->bPwrOn2PwrGood = 0;
  965. if (sl811->board && sl811->board->port_power) {
  966. desc->bPwrOn2PwrGood = sl811->board->potpg;
  967. if (!desc->bPwrOn2PwrGood)
  968. desc->bPwrOn2PwrGood = 10;
  969. temp = 0x0001;
  970. } else
  971. temp = 0x0002;
  972. /* no overcurrent errors detection/handling */
  973. temp |= 0x0010;
  974. desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
  975. /* two bitmaps: ports removable, and legacy PortPwrCtrlMask */
  976. desc->bitmap[0] = 0 << 1;
  977. desc->bitmap[1] = ~0;
  978. }
  979. static void
  980. sl811h_timer(unsigned long _sl811)
  981. {
  982. struct sl811 *sl811 = (void *) _sl811;
  983. unsigned long flags;
  984. u8 irqstat;
  985. u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
  986. const u32 mask = (1 << USB_PORT_FEAT_CONNECTION)
  987. | (1 << USB_PORT_FEAT_ENABLE)
  988. | (1 << USB_PORT_FEAT_LOWSPEED);
  989. spin_lock_irqsave(&sl811->lock, flags);
  990. /* stop special signaling */
  991. sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
  992. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  993. udelay(3);
  994. irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
  995. switch (signaling) {
  996. case SL11H_CTL1MASK_SE0:
  997. DBG("end reset\n");
  998. sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
  999. | (1 << USB_PORT_FEAT_POWER);
  1000. sl811->ctrl1 = 0;
  1001. /* don't wrongly ack RD */
  1002. if (irqstat & SL11H_INTMASK_INSRMV)
  1003. irqstat &= ~SL11H_INTMASK_RD;
  1004. break;
  1005. case SL11H_CTL1MASK_K:
  1006. DBG("end resume\n");
  1007. sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
  1008. break;
  1009. default:
  1010. DBG("odd timer signaling: %02x\n", signaling);
  1011. break;
  1012. }
  1013. sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
  1014. if (irqstat & SL11H_INTMASK_RD) {
  1015. /* usbcore nukes all pending transactions on disconnect */
  1016. if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
  1017. sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
  1018. | (1 << USB_PORT_FEAT_C_ENABLE);
  1019. sl811->port1 &= ~mask;
  1020. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  1021. } else {
  1022. sl811->port1 |= mask;
  1023. if (irqstat & SL11H_INTMASK_DP)
  1024. sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
  1025. sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
  1026. }
  1027. if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
  1028. u8 ctrl2 = SL811HS_CTL2_INIT;
  1029. sl811->irq_enable |= SL11H_INTMASK_DONE_A;
  1030. #ifdef USE_B
  1031. sl811->irq_enable |= SL11H_INTMASK_DONE_B;
  1032. #endif
  1033. if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
  1034. sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
  1035. ctrl2 |= SL811HS_CTL2MASK_DSWAP;
  1036. }
  1037. /* start SOFs flowing, kickstarting with A registers */
  1038. sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
  1039. sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
  1040. sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
  1041. /* autoincrementing */
  1042. sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
  1043. writeb(SL_SOF, sl811->data_reg);
  1044. writeb(0, sl811->data_reg);
  1045. sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
  1046. SL11H_HCTLMASK_ARM);
  1047. /* khubd provides debounce delay */
  1048. } else {
  1049. sl811->ctrl1 = 0;
  1050. }
  1051. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1052. /* reenable irqs */
  1053. sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
  1054. spin_unlock_irqrestore(&sl811->lock, flags);
  1055. }
  1056. static int
  1057. sl811h_hub_control(
  1058. struct usb_hcd *hcd,
  1059. u16 typeReq,
  1060. u16 wValue,
  1061. u16 wIndex,
  1062. char *buf,
  1063. u16 wLength
  1064. ) {
  1065. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1066. int retval = 0;
  1067. unsigned long flags;
  1068. spin_lock_irqsave(&sl811->lock, flags);
  1069. switch (typeReq) {
  1070. case ClearHubFeature:
  1071. case SetHubFeature:
  1072. switch (wValue) {
  1073. case C_HUB_OVER_CURRENT:
  1074. case C_HUB_LOCAL_POWER:
  1075. break;
  1076. default:
  1077. goto error;
  1078. }
  1079. break;
  1080. case ClearPortFeature:
  1081. if (wIndex != 1 || wLength != 0)
  1082. goto error;
  1083. switch (wValue) {
  1084. case USB_PORT_FEAT_ENABLE:
  1085. sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
  1086. sl811->ctrl1 = 0;
  1087. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1088. sl811->irq_enable = SL11H_INTMASK_INSRMV;
  1089. sl811_write(sl811, SL11H_IRQ_ENABLE,
  1090. sl811->irq_enable);
  1091. break;
  1092. case USB_PORT_FEAT_SUSPEND:
  1093. if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
  1094. break;
  1095. /* 20 msec of resume/K signaling, other irqs blocked */
  1096. DBG("start resume...\n");
  1097. sl811->irq_enable = 0;
  1098. sl811_write(sl811, SL11H_IRQ_ENABLE,
  1099. sl811->irq_enable);
  1100. sl811->ctrl1 |= SL11H_CTL1MASK_K;
  1101. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1102. mod_timer(&sl811->timer, jiffies
  1103. + msecs_to_jiffies(20));
  1104. break;
  1105. case USB_PORT_FEAT_POWER:
  1106. port_power(sl811, 0);
  1107. break;
  1108. case USB_PORT_FEAT_C_ENABLE:
  1109. case USB_PORT_FEAT_C_SUSPEND:
  1110. case USB_PORT_FEAT_C_CONNECTION:
  1111. case USB_PORT_FEAT_C_OVER_CURRENT:
  1112. case USB_PORT_FEAT_C_RESET:
  1113. break;
  1114. default:
  1115. goto error;
  1116. }
  1117. sl811->port1 &= ~(1 << wValue);
  1118. break;
  1119. case GetHubDescriptor:
  1120. sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
  1121. break;
  1122. case GetHubStatus:
  1123. *(__le32 *) buf = cpu_to_le32(0);
  1124. break;
  1125. case GetPortStatus:
  1126. if (wIndex != 1)
  1127. goto error;
  1128. *(__le32 *) buf = cpu_to_le32(sl811->port1);
  1129. #ifndef VERBOSE
  1130. if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
  1131. #endif
  1132. DBG("GetPortStatus %08x\n", sl811->port1);
  1133. break;
  1134. case SetPortFeature:
  1135. if (wIndex != 1 || wLength != 0)
  1136. goto error;
  1137. switch (wValue) {
  1138. case USB_PORT_FEAT_SUSPEND:
  1139. if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
  1140. goto error;
  1141. if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
  1142. goto error;
  1143. DBG("suspend...\n");
  1144. sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
  1145. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1146. break;
  1147. case USB_PORT_FEAT_POWER:
  1148. port_power(sl811, 1);
  1149. break;
  1150. case USB_PORT_FEAT_RESET:
  1151. if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
  1152. goto error;
  1153. if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
  1154. break;
  1155. /* 50 msec of reset/SE0 signaling, irqs blocked */
  1156. sl811->irq_enable = 0;
  1157. sl811_write(sl811, SL11H_IRQ_ENABLE,
  1158. sl811->irq_enable);
  1159. sl811->ctrl1 = SL11H_CTL1MASK_SE0;
  1160. sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
  1161. sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
  1162. mod_timer(&sl811->timer, jiffies
  1163. + msecs_to_jiffies(50));
  1164. break;
  1165. default:
  1166. goto error;
  1167. }
  1168. sl811->port1 |= 1 << wValue;
  1169. break;
  1170. default:
  1171. error:
  1172. /* "protocol stall" on error */
  1173. retval = -EPIPE;
  1174. }
  1175. spin_unlock_irqrestore(&sl811->lock, flags);
  1176. return retval;
  1177. }
  1178. #ifdef CONFIG_PM
  1179. static int
  1180. sl811h_bus_suspend(struct usb_hcd *hcd)
  1181. {
  1182. // SOFs off
  1183. DBG("%s\n", __FUNCTION__);
  1184. return 0;
  1185. }
  1186. static int
  1187. sl811h_bus_resume(struct usb_hcd *hcd)
  1188. {
  1189. // SOFs on
  1190. DBG("%s\n", __FUNCTION__);
  1191. return 0;
  1192. }
  1193. #else
  1194. #define sl811h_bus_suspend NULL
  1195. #define sl811h_bus_resume NULL
  1196. #endif
  1197. /*-------------------------------------------------------------------------*/
  1198. #ifdef STUB_DEBUG_FILE
  1199. static inline void create_debug_file(struct sl811 *sl811) { }
  1200. static inline void remove_debug_file(struct sl811 *sl811) { }
  1201. #else
  1202. #include <linux/proc_fs.h>
  1203. #include <linux/seq_file.h>
  1204. static void dump_irq(struct seq_file *s, char *label, u8 mask)
  1205. {
  1206. seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
  1207. (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
  1208. (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
  1209. (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
  1210. (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
  1211. (mask & SL11H_INTMASK_RD) ? " rd" : "",
  1212. (mask & SL11H_INTMASK_DP) ? " dp" : "");
  1213. }
  1214. static int proc_sl811h_show(struct seq_file *s, void *unused)
  1215. {
  1216. struct sl811 *sl811 = s->private;
  1217. struct sl811h_ep *ep;
  1218. unsigned i;
  1219. seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
  1220. sl811_to_hcd(sl811)->product_desc,
  1221. hcd_name, DRIVER_VERSION,
  1222. sl811->port1);
  1223. seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
  1224. seq_printf(s, "current session: done_a %ld done_b %ld "
  1225. "wake %ld sof %ld overrun %ld lost %ld\n\n",
  1226. sl811->stat_a, sl811->stat_b,
  1227. sl811->stat_wake, sl811->stat_sof,
  1228. sl811->stat_overrun, sl811->stat_lost);
  1229. spin_lock_irq(&sl811->lock);
  1230. if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
  1231. seq_printf(s, "(suspended)\n\n");
  1232. else {
  1233. u8 t = sl811_read(sl811, SL11H_CTLREG1);
  1234. seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
  1235. (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
  1236. ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
  1237. case SL11H_CTL1MASK_NORMAL: s = ""; break;
  1238. case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
  1239. case SL11H_CTL1MASK_K: s = " k/resume"; break;
  1240. default: s = "j"; break;
  1241. }; s; }),
  1242. (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
  1243. (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
  1244. dump_irq(s, "irq_enable",
  1245. sl811_read(sl811, SL11H_IRQ_ENABLE));
  1246. dump_irq(s, "irq_status",
  1247. sl811_read(sl811, SL11H_IRQ_STATUS));
  1248. seq_printf(s, "frame clocks remaining: %d\n",
  1249. sl811_read(sl811, SL11H_SOFTMRREG) << 6);
  1250. }
  1251. seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
  1252. sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
  1253. sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
  1254. seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
  1255. sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
  1256. sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
  1257. seq_printf(s, "\n");
  1258. list_for_each_entry (ep, &sl811->async, schedule) {
  1259. struct urb *urb;
  1260. seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
  1261. " nak %d err %d\n",
  1262. (ep == sl811->active_a) ? "(A) " : "",
  1263. (ep == sl811->active_b) ? "(B) " : "",
  1264. ep, ep->epnum,
  1265. ({ char *s; switch (ep->nextpid) {
  1266. case USB_PID_IN: s = "in"; break;
  1267. case USB_PID_OUT: s = "out"; break;
  1268. case USB_PID_SETUP: s = "setup"; break;
  1269. case USB_PID_ACK: s = "status"; break;
  1270. default: s = "?"; break;
  1271. }; s;}),
  1272. ep->maxpacket,
  1273. ep->nak_count, ep->error_count);
  1274. list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
  1275. seq_printf(s, " urb%p, %d/%d\n", urb,
  1276. urb->actual_length,
  1277. urb->transfer_buffer_length);
  1278. }
  1279. }
  1280. if (!list_empty(&sl811->async))
  1281. seq_printf(s, "\n");
  1282. seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
  1283. for (i = 0; i < PERIODIC_SIZE; i++) {
  1284. ep = sl811->periodic[i];
  1285. if (!ep)
  1286. continue;
  1287. seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
  1288. /* DUMB: prints shared entries multiple times */
  1289. do {
  1290. seq_printf(s,
  1291. " %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
  1292. "err %d\n",
  1293. (ep == sl811->active_a) ? "(A) " : "",
  1294. (ep == sl811->active_b) ? "(B) " : "",
  1295. ep->period, ep,
  1296. (ep->udev->speed == USB_SPEED_FULL)
  1297. ? "" : "ls ",
  1298. ep->udev->devnum, ep->epnum,
  1299. (ep->epnum == 0) ? ""
  1300. : ((ep->nextpid == USB_PID_IN)
  1301. ? "in"
  1302. : "out"),
  1303. ep->maxpacket, ep->error_count);
  1304. ep = ep->next;
  1305. } while (ep);
  1306. }
  1307. spin_unlock_irq(&sl811->lock);
  1308. seq_printf(s, "\n");
  1309. return 0;
  1310. }
  1311. static int proc_sl811h_open(struct inode *inode, struct file *file)
  1312. {
  1313. return single_open(file, proc_sl811h_show, PDE(inode)->data);
  1314. }
  1315. static const struct file_operations proc_ops = {
  1316. .open = proc_sl811h_open,
  1317. .read = seq_read,
  1318. .llseek = seq_lseek,
  1319. .release = single_release,
  1320. };
  1321. /* expect just one sl811 per system */
  1322. static const char proc_filename[] = "driver/sl811h";
  1323. static void create_debug_file(struct sl811 *sl811)
  1324. {
  1325. struct proc_dir_entry *pde;
  1326. pde = create_proc_entry(proc_filename, 0, NULL);
  1327. if (pde == NULL)
  1328. return;
  1329. pde->proc_fops = &proc_ops;
  1330. pde->data = sl811;
  1331. sl811->pde = pde;
  1332. }
  1333. static void remove_debug_file(struct sl811 *sl811)
  1334. {
  1335. if (sl811->pde)
  1336. remove_proc_entry(proc_filename, NULL);
  1337. }
  1338. #endif
  1339. /*-------------------------------------------------------------------------*/
  1340. static void
  1341. sl811h_stop(struct usb_hcd *hcd)
  1342. {
  1343. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1344. unsigned long flags;
  1345. del_timer_sync(&hcd->rh_timer);
  1346. spin_lock_irqsave(&sl811->lock, flags);
  1347. port_power(sl811, 0);
  1348. spin_unlock_irqrestore(&sl811->lock, flags);
  1349. }
  1350. static int
  1351. sl811h_start(struct usb_hcd *hcd)
  1352. {
  1353. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1354. /* chip has been reset, VBUS power is off */
  1355. hcd->state = HC_STATE_RUNNING;
  1356. if (sl811->board) {
  1357. if (!device_can_wakeup(hcd->self.controller))
  1358. device_init_wakeup(hcd->self.controller,
  1359. sl811->board->can_wakeup);
  1360. hcd->power_budget = sl811->board->power * 2;
  1361. }
  1362. /* enable power and interupts */
  1363. port_power(sl811, 1);
  1364. return 0;
  1365. }
  1366. /*-------------------------------------------------------------------------*/
  1367. static struct hc_driver sl811h_hc_driver = {
  1368. .description = hcd_name,
  1369. .hcd_priv_size = sizeof(struct sl811),
  1370. /*
  1371. * generic hardware linkage
  1372. */
  1373. .irq = sl811h_irq,
  1374. .flags = HCD_USB11 | HCD_MEMORY,
  1375. /* Basic lifecycle operations */
  1376. .start = sl811h_start,
  1377. .stop = sl811h_stop,
  1378. /*
  1379. * managing i/o requests and associated device resources
  1380. */
  1381. .urb_enqueue = sl811h_urb_enqueue,
  1382. .urb_dequeue = sl811h_urb_dequeue,
  1383. .endpoint_disable = sl811h_endpoint_disable,
  1384. /*
  1385. * periodic schedule support
  1386. */
  1387. .get_frame_number = sl811h_get_frame,
  1388. /*
  1389. * root hub support
  1390. */
  1391. .hub_status_data = sl811h_hub_status_data,
  1392. .hub_control = sl811h_hub_control,
  1393. .bus_suspend = sl811h_bus_suspend,
  1394. .bus_resume = sl811h_bus_resume,
  1395. };
  1396. /*-------------------------------------------------------------------------*/
  1397. static int __devexit
  1398. sl811h_remove(struct platform_device *dev)
  1399. {
  1400. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1401. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1402. struct resource *res;
  1403. remove_debug_file(sl811);
  1404. usb_remove_hcd(hcd);
  1405. /* some platforms may use IORESOURCE_IO */
  1406. res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  1407. if (res)
  1408. iounmap(sl811->data_reg);
  1409. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1410. if (res)
  1411. iounmap(sl811->addr_reg);
  1412. usb_put_hcd(hcd);
  1413. return 0;
  1414. }
  1415. static int __devinit
  1416. sl811h_probe(struct platform_device *dev)
  1417. {
  1418. struct usb_hcd *hcd;
  1419. struct sl811 *sl811;
  1420. struct resource *addr, *data;
  1421. int irq;
  1422. void __iomem *addr_reg;
  1423. void __iomem *data_reg;
  1424. int retval;
  1425. u8 tmp, ioaddr = 0;
  1426. /* basic sanity checks first. board-specific init logic should
  1427. * have initialized these three resources and probably board
  1428. * specific platform_data. we don't probe for IRQs, and do only
  1429. * minimal sanity checking.
  1430. */
  1431. irq = platform_get_irq(dev, 0);
  1432. if (dev->num_resources < 3 || irq < 0)
  1433. return -ENODEV;
  1434. /* refuse to confuse usbcore */
  1435. if (dev->dev.dma_mask) {
  1436. DBG("no we won't dma\n");
  1437. return -EINVAL;
  1438. }
  1439. /* the chip may be wired for either kind of addressing */
  1440. addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1441. data = platform_get_resource(dev, IORESOURCE_MEM, 1);
  1442. retval = -EBUSY;
  1443. if (!addr || !data) {
  1444. addr = platform_get_resource(dev, IORESOURCE_IO, 0);
  1445. data = platform_get_resource(dev, IORESOURCE_IO, 1);
  1446. if (!addr || !data)
  1447. return -ENODEV;
  1448. ioaddr = 1;
  1449. /*
  1450. * NOTE: 64-bit resource->start is getting truncated
  1451. * to avoid compiler warning, assuming that ->start
  1452. * is always 32-bit for this case
  1453. */
  1454. addr_reg = (void __iomem *) (unsigned long) addr->start;
  1455. data_reg = (void __iomem *) (unsigned long) data->start;
  1456. } else {
  1457. addr_reg = ioremap(addr->start, 1);
  1458. if (addr_reg == NULL) {
  1459. retval = -ENOMEM;
  1460. goto err2;
  1461. }
  1462. data_reg = ioremap(data->start, 1);
  1463. if (data_reg == NULL) {
  1464. retval = -ENOMEM;
  1465. goto err4;
  1466. }
  1467. }
  1468. /* allocate and initialize hcd */
  1469. hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev->dev.bus_id);
  1470. if (!hcd) {
  1471. retval = -ENOMEM;
  1472. goto err5;
  1473. }
  1474. hcd->rsrc_start = addr->start;
  1475. sl811 = hcd_to_sl811(hcd);
  1476. spin_lock_init(&sl811->lock);
  1477. INIT_LIST_HEAD(&sl811->async);
  1478. sl811->board = dev->dev.platform_data;
  1479. init_timer(&sl811->timer);
  1480. sl811->timer.function = sl811h_timer;
  1481. sl811->timer.data = (unsigned long) sl811;
  1482. sl811->addr_reg = addr_reg;
  1483. sl811->data_reg = data_reg;
  1484. spin_lock_irq(&sl811->lock);
  1485. port_power(sl811, 0);
  1486. spin_unlock_irq(&sl811->lock);
  1487. msleep(200);
  1488. tmp = sl811_read(sl811, SL11H_HWREVREG);
  1489. switch (tmp >> 4) {
  1490. case 1:
  1491. hcd->product_desc = "SL811HS v1.2";
  1492. break;
  1493. case 2:
  1494. hcd->product_desc = "SL811HS v1.5";
  1495. break;
  1496. default:
  1497. /* reject case 0, SL11S is less functional */
  1498. DBG("chiprev %02x\n", tmp);
  1499. retval = -ENXIO;
  1500. goto err6;
  1501. }
  1502. /* The chip's IRQ is level triggered, active high. A requirement
  1503. * for platform device setup is to cope with things like signal
  1504. * inverters (e.g. CF is active low) or working only with edge
  1505. * triggers (e.g. most ARM CPUs). Initial driver stress testing
  1506. * was on a system with single edge triggering, so most sorts of
  1507. * triggering arrangement should work.
  1508. */
  1509. retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
  1510. if (retval != 0)
  1511. goto err6;
  1512. create_debug_file(sl811);
  1513. return retval;
  1514. err6:
  1515. usb_put_hcd(hcd);
  1516. err5:
  1517. if (!ioaddr)
  1518. iounmap(data_reg);
  1519. err4:
  1520. if (!ioaddr)
  1521. iounmap(addr_reg);
  1522. err2:
  1523. DBG("init error, %d\n", retval);
  1524. return retval;
  1525. }
  1526. #ifdef CONFIG_PM
  1527. /* for this device there's no useful distinction between the controller
  1528. * and its root hub, except that the root hub only gets direct PM calls
  1529. * when CONFIG_USB_SUSPEND is enabled.
  1530. */
  1531. static int
  1532. sl811h_suspend(struct platform_device *dev, pm_message_t state)
  1533. {
  1534. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1535. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1536. int retval = 0;
  1537. switch (state.event) {
  1538. case PM_EVENT_FREEZE:
  1539. retval = sl811h_bus_suspend(hcd);
  1540. break;
  1541. case PM_EVENT_SUSPEND:
  1542. case PM_EVENT_PRETHAW: /* explicitly discard hw state */
  1543. port_power(sl811, 0);
  1544. break;
  1545. }
  1546. if (retval == 0)
  1547. dev->dev.power.power_state = state;
  1548. return retval;
  1549. }
  1550. static int
  1551. sl811h_resume(struct platform_device *dev)
  1552. {
  1553. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1554. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1555. /* with no "check to see if VBUS is still powered" board hook,
  1556. * let's assume it'd only be powered to enable remote wakeup.
  1557. */
  1558. if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND
  1559. || !device_can_wakeup(&hcd->self.root_hub->dev)) {
  1560. sl811->port1 = 0;
  1561. port_power(sl811, 1);
  1562. usb_root_hub_lost_power(hcd->self.root_hub);
  1563. return 0;
  1564. }
  1565. dev->dev.power.power_state = PMSG_ON;
  1566. return sl811h_bus_resume(hcd);
  1567. }
  1568. #else
  1569. #define sl811h_suspend NULL
  1570. #define sl811h_resume NULL
  1571. #endif
  1572. /* this driver is exported so sl811_cs can depend on it */
  1573. struct platform_driver sl811h_driver = {
  1574. .probe = sl811h_probe,
  1575. .remove = __devexit_p(sl811h_remove),
  1576. .suspend = sl811h_suspend,
  1577. .resume = sl811h_resume,
  1578. .driver = {
  1579. .name = (char *) hcd_name,
  1580. .owner = THIS_MODULE,
  1581. },
  1582. };
  1583. EXPORT_SYMBOL(sl811h_driver);
  1584. /*-------------------------------------------------------------------------*/
  1585. static int __init sl811h_init(void)
  1586. {
  1587. if (usb_disabled())
  1588. return -ENODEV;
  1589. INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
  1590. return platform_driver_register(&sl811h_driver);
  1591. }
  1592. module_init(sl811h_init);
  1593. static void __exit sl811h_cleanup(void)
  1594. {
  1595. platform_driver_unregister(&sl811h_driver);
  1596. }
  1597. module_exit(sl811h_cleanup);