sl811-hcd.c 46 KB

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