sl811-hcd.c 46 KB

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