sl811-hcd.c 46 KB

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