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