sl811-hcd.c 46 KB

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