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