sl811-hcd.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  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. /*
  1453. * NOTE: 64-bit resource->start is getting truncated
  1454. * to avoid compiler warning, assuming that ->start
  1455. * is always 32-bit for this case
  1456. */
  1457. addr_reg = (void __iomem *) (unsigned long) addr->start;
  1458. data_reg = (void __iomem *) (unsigned long) 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, 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->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 platform_device *dev, pm_message_t state)
  1536. {
  1537. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1538. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1539. int retval = 0;
  1540. if (state.event == PM_EVENT_FREEZE)
  1541. retval = sl811h_bus_suspend(hcd);
  1542. else if (state.event == PM_EVENT_SUSPEND)
  1543. port_power(sl811, 0);
  1544. if (retval == 0)
  1545. dev->dev.power.power_state = state;
  1546. return retval;
  1547. }
  1548. static int
  1549. sl811h_resume(struct platform_device *dev)
  1550. {
  1551. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1552. struct sl811 *sl811 = hcd_to_sl811(hcd);
  1553. /* with no "check to see if VBUS is still powered" board hook,
  1554. * let's assume it'd only be powered to enable remote wakeup.
  1555. */
  1556. if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND
  1557. || !device_can_wakeup(&hcd->self.root_hub->dev)) {
  1558. sl811->port1 = 0;
  1559. port_power(sl811, 1);
  1560. usb_root_hub_lost_power(hcd->self.root_hub);
  1561. return 0;
  1562. }
  1563. dev->dev.power.power_state = PMSG_ON;
  1564. return sl811h_bus_resume(hcd);
  1565. }
  1566. #else
  1567. #define sl811h_suspend NULL
  1568. #define sl811h_resume NULL
  1569. #endif
  1570. /* this driver is exported so sl811_cs can depend on it */
  1571. struct platform_driver sl811h_driver = {
  1572. .probe = sl811h_probe,
  1573. .remove = __devexit_p(sl811h_remove),
  1574. .suspend = sl811h_suspend,
  1575. .resume = sl811h_resume,
  1576. .driver = {
  1577. .name = (char *) hcd_name,
  1578. .owner = THIS_MODULE,
  1579. },
  1580. };
  1581. EXPORT_SYMBOL(sl811h_driver);
  1582. /*-------------------------------------------------------------------------*/
  1583. static int __init sl811h_init(void)
  1584. {
  1585. if (usb_disabled())
  1586. return -ENODEV;
  1587. INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
  1588. return platform_driver_register(&sl811h_driver);
  1589. }
  1590. module_init(sl811h_init);
  1591. static void __exit sl811h_cleanup(void)
  1592. {
  1593. platform_driver_unregister(&sl811h_driver);
  1594. }
  1595. module_exit(sl811h_cleanup);