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