blob: fafccc2fd33140386e83413022a9a8c661b9e4be [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>
Eric Lescouet27729aa2010-04-24 23:21:52 +020048#include <linux/usb/hcd.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010049#include <linux/platform_device.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070050#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <asm/io.h>
53#include <asm/irq.h>
54#include <asm/system.h>
55#include <asm/byteorder.h>
Michael Hennerich8ca5bfab2009-12-21 12:53:24 -050056#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "sl811.h"
59
60
61MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
62MODULE_LICENSE("GPL");
Kay Sieversf4fce612008-04-10 21:29:22 -070063MODULE_ALIAS("platform:sl811-hcd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Brownell1e9a47b2005-05-26 05:55:55 -070065#define DRIVER_VERSION "19 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67
68#ifndef DEBUG
69# define STUB_DEBUG_FILE
70#endif
71
72/* for now, use only one transfer register bank */
73#undef USE_B
74
75/* this doesn't understand urb->iso_frame_desc[], but if you had a driver
76 * that just queued one ISO frame per URB then iso transfers "should" work
77 * using the normal urb status fields.
78 */
79#define DISABLE_ISO
80
81// #define QUIRK2
82#define QUIRK3
83
84static const char hcd_name[] = "sl811-hcd";
85
86/*-------------------------------------------------------------------------*/
87
88static void port_power(struct sl811 *sl811, int is_on)
89{
90 struct usb_hcd *hcd = sl811_to_hcd(sl811);
91
92 /* hub is inactive unless the port is powered */
93 if (is_on) {
Alan Stern749da5f2010-03-04 17:05:08 -050094 if (sl811->port1 & USB_PORT_STAT_POWER)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return;
96
Alan Stern749da5f2010-03-04 17:05:08 -050097 sl811->port1 = USB_PORT_STAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 sl811->irq_enable = SL11H_INTMASK_INSRMV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } else {
100 sl811->port1 = 0;
101 sl811->irq_enable = 0;
102 hcd->state = HC_STATE_HALT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 sl811->ctrl1 = 0;
105 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
106 sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
107
108 if (sl811->board && sl811->board->port_power) {
109 /* switch VBUS, at 500mA unless hub power budget gets set */
110 DBG("power %s\n", is_on ? "on" : "off");
111 sl811->board->port_power(hcd->self.controller, is_on);
112 }
113
114 /* reset as thoroughly as we can */
115 if (sl811->board && sl811->board->reset)
116 sl811->board->reset(hcd->self.controller);
David Brownell1e9a47b2005-05-26 05:55:55 -0700117 else {
118 sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
119 mdelay(20);
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
123 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
124 sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
125 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
126
127 // if !is_on, put into lowpower mode now
128}
129
130/*-------------------------------------------------------------------------*/
131
132/* This is a PIO-only HCD. Queueing appends URBs to the endpoint's queue,
133 * and may start I/O. Endpoint queues are scanned during completion irq
Steven Cole093cf722005-05-03 19:07:24 -0600134 * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * Using an external DMA engine to copy a packet at a time could work,
137 * though setup/teardown costs may be too big to make it worthwhile.
138 */
139
140/* SETUP starts a new control request. Devices are not allowed to
141 * STALL or NAK these; they must cancel any pending control requests.
142 */
143static void setup_packet(
144 struct sl811 *sl811,
145 struct sl811h_ep *ep,
146 struct urb *urb,
147 u8 bank,
148 u8 control
149)
150{
151 u8 addr;
152 u8 len;
153 void __iomem *data_reg;
154
155 addr = SL811HS_PACKET_BUF(bank == 0);
156 len = sizeof(struct usb_ctrlrequest);
157 data_reg = sl811->data_reg;
158 sl811_write_buf(sl811, addr, urb->setup_packet, len);
159
160 /* autoincrementing */
161 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
162 writeb(len, data_reg);
163 writeb(SL_SETUP /* | ep->epnum */, data_reg);
164 writeb(usb_pipedevice(urb->pipe), data_reg);
165
166 /* always OUT/data0 */ ;
167 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
168 control | SL11H_HCTLMASK_OUT);
169 ep->length = 0;
170 PACKET("SETUP qh%p\n", ep);
171}
172
173/* STATUS finishes control requests, often after IN or OUT data packets */
174static void status_packet(
175 struct sl811 *sl811,
176 struct sl811h_ep *ep,
177 struct urb *urb,
178 u8 bank,
179 u8 control
180)
181{
182 int do_out;
183 void __iomem *data_reg;
184
185 do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
186 data_reg = sl811->data_reg;
187
188 /* autoincrementing */
189 sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
190 writeb(0, data_reg);
191 writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
192 writeb(usb_pipedevice(urb->pipe), data_reg);
193
194 /* always data1; sometimes IN */
195 control |= SL11H_HCTLMASK_TOGGLE;
196 if (do_out)
197 control |= SL11H_HCTLMASK_OUT;
198 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
199 ep->length = 0;
200 PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
201 do_out ? "out" : "in", ep);
202}
203
204/* IN packets can be used with any type of endpoint. here we just
205 * start the transfer, data from the peripheral may arrive later.
206 * urb->iso_frame_desc is currently ignored here...
207 */
208static void in_packet(
209 struct sl811 *sl811,
210 struct sl811h_ep *ep,
211 struct urb *urb,
212 u8 bank,
213 u8 control
214)
215{
216 u8 addr;
217 u8 len;
218 void __iomem *data_reg;
219
220 /* avoid losing data on overflow */
221 len = ep->maxpacket;
222 addr = SL811HS_PACKET_BUF(bank == 0);
223 if (!(control & SL11H_HCTLMASK_ISOCH)
224 && usb_gettoggle(urb->dev, ep->epnum, 0))
225 control |= SL11H_HCTLMASK_TOGGLE;
226 data_reg = sl811->data_reg;
227
228 /* autoincrementing */
229 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
230 writeb(len, data_reg);
231 writeb(SL_IN | ep->epnum, data_reg);
232 writeb(usb_pipedevice(urb->pipe), data_reg);
233
234 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -0800235 ep->length = min_t(u32, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 urb->transfer_buffer_length - urb->actual_length);
237 PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
238 !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
239}
240
241/* OUT packets can be used with any type of endpoint.
242 * urb->iso_frame_desc is currently ignored here...
243 */
244static void out_packet(
245 struct sl811 *sl811,
246 struct sl811h_ep *ep,
247 struct urb *urb,
248 u8 bank,
249 u8 control
250)
251{
252 void *buf;
253 u8 addr;
254 u8 len;
255 void __iomem *data_reg;
256
257 buf = urb->transfer_buffer + urb->actual_length;
258 prefetch(buf);
259
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -0800260 len = min_t(u32, ep->maxpacket,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 urb->transfer_buffer_length - urb->actual_length);
262
263 if (!(control & SL11H_HCTLMASK_ISOCH)
264 && usb_gettoggle(urb->dev, ep->epnum, 1))
265 control |= SL11H_HCTLMASK_TOGGLE;
266 addr = SL811HS_PACKET_BUF(bank == 0);
267 data_reg = sl811->data_reg;
268
269 sl811_write_buf(sl811, addr, buf, len);
270
271 /* autoincrementing */
272 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
273 writeb(len, data_reg);
274 writeb(SL_OUT | ep->epnum, data_reg);
275 writeb(usb_pipedevice(urb->pipe), data_reg);
276
277 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
278 control | SL11H_HCTLMASK_OUT);
279 ep->length = len;
280 PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
281 !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
282}
283
284/*-------------------------------------------------------------------------*/
285
286/* caller updates on-chip enables later */
287
288static inline void sofirq_on(struct sl811 *sl811)
289{
290 if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
291 return;
292 VDBG("sof irq on\n");
293 sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
294}
295
296static inline void sofirq_off(struct sl811 *sl811)
297{
298 if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
299 return;
300 VDBG("sof irq off\n");
301 sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
302}
303
304/*-------------------------------------------------------------------------*/
305
306/* pick the next endpoint for a transaction, and issue it.
307 * frames start with periodic transfers (after whatever is pending
308 * from the previous frame), and the rest of the time is async
309 * transfers, scheduled round-robin.
310 */
311static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
312{
313 struct sl811h_ep *ep;
314 struct urb *urb;
315 int fclock;
316 u8 control;
317
318 /* use endpoint at schedule head */
319 if (sl811->next_periodic) {
320 ep = sl811->next_periodic;
321 sl811->next_periodic = ep->next;
322 } else {
323 if (sl811->next_async)
324 ep = sl811->next_async;
325 else if (!list_empty(&sl811->async))
326 ep = container_of(sl811->async.next,
327 struct sl811h_ep, schedule);
328 else {
329 /* could set up the first fullspeed periodic
330 * transfer for the next frame ...
331 */
332 return NULL;
333 }
334
335#ifdef USE_B
336 if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
337 return NULL;
338#endif
339
340 if (ep->schedule.next == &sl811->async)
341 sl811->next_async = NULL;
342 else
343 sl811->next_async = container_of(ep->schedule.next,
344 struct sl811h_ep, schedule);
345 }
346
347 if (unlikely(list_empty(&ep->hep->urb_list))) {
348 DBG("empty %p queue?\n", ep);
349 return NULL;
350 }
351
352 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
353 control = ep->defctrl;
354
355 /* if this frame doesn't have enough time left to transfer this
356 * packet, wait till the next frame. too-simple algorithm...
357 */
358 fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
359 fclock -= 100; /* setup takes not much time */
360 if (urb->dev->speed == USB_SPEED_LOW) {
361 if (control & SL11H_HCTLMASK_PREAMBLE) {
362 /* also note erratum 1: some hubs won't work */
363 fclock -= 800;
364 }
365 fclock -= ep->maxpacket << 8;
366
367 /* erratum 2: AFTERSOF only works for fullspeed */
368 if (fclock < 0) {
369 if (ep->period)
370 sl811->stat_overrun++;
371 sofirq_on(sl811);
372 return NULL;
373 }
374 } else {
375 fclock -= 12000 / 19; /* 19 64byte packets/msec */
376 if (fclock < 0) {
377 if (ep->period)
378 sl811->stat_overrun++;
379 control |= SL11H_HCTLMASK_AFTERSOF;
380
381 /* throttle bulk/control irq noise */
382 } else if (ep->nak_count)
383 control |= SL11H_HCTLMASK_AFTERSOF;
384 }
385
386
387 switch (ep->nextpid) {
388 case USB_PID_IN:
389 in_packet(sl811, ep, urb, bank, control);
390 break;
391 case USB_PID_OUT:
392 out_packet(sl811, ep, urb, bank, control);
393 break;
394 case USB_PID_SETUP:
395 setup_packet(sl811, ep, urb, bank, control);
396 break;
397 case USB_PID_ACK: /* for control status */
398 status_packet(sl811, ep, urb, bank, control);
399 break;
400 default:
401 DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
402 ep = NULL;
403 }
404 return ep;
405}
406
407#define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
408
409static inline void start_transfer(struct sl811 *sl811)
410{
Alan Stern749da5f2010-03-04 17:05:08 -0500411 if (sl811->port1 & USB_PORT_STAT_SUSPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return;
413 if (sl811->active_a == NULL) {
414 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
415 if (sl811->active_a != NULL)
416 sl811->jiffies_a = jiffies + MIN_JIFFIES;
417 }
418#ifdef USE_B
419 if (sl811->active_b == NULL) {
420 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
421 if (sl811->active_b != NULL)
422 sl811->jiffies_b = jiffies + MIN_JIFFIES;
423 }
424#endif
425}
426
427static void finish_request(
428 struct sl811 *sl811,
429 struct sl811h_ep *ep,
430 struct urb *urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int status
432) __releases(sl811->lock) __acquires(sl811->lock)
433{
434 unsigned i;
435
436 if (usb_pipecontrol(urb->pipe))
437 ep->nextpid = USB_PID_SETUP;
438
Alan Sterne9df41c2007-08-08 11:48:02 -0400439 usb_hcd_unlink_urb_from_ep(sl811_to_hcd(sl811), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 spin_unlock(&sl811->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400441 usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_lock(&sl811->lock);
443
444 /* leave active endpoints in the schedule */
445 if (!list_empty(&ep->hep->urb_list))
446 return;
447
448 /* async deschedule? */
449 if (!list_empty(&ep->schedule)) {
450 list_del_init(&ep->schedule);
451 if (ep == sl811->next_async)
452 sl811->next_async = NULL;
453 return;
454 }
455
456 /* periodic deschedule */
457 DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
458 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
459 struct sl811h_ep *temp;
460 struct sl811h_ep **prev = &sl811->periodic[i];
461
462 while (*prev && ((temp = *prev) != ep))
463 prev = &temp->next;
464 if (*prev)
465 *prev = ep->next;
466 sl811->load[i] -= ep->load;
David Brownell1e9a47b2005-05-26 05:55:55 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ep->branch = PERIODIC_SIZE;
469 sl811->periodic_count--;
470 sl811_to_hcd(sl811)->self.bandwidth_allocated
471 -= ep->load / ep->period;
472 if (ep == sl811->next_periodic)
473 sl811->next_periodic = ep->next;
474
475 /* we might turn SOFs back on again for the async schedule */
476 if (sl811->periodic_count == 0)
477 sofirq_off(sl811);
478}
479
480static void
David Howells7d12e782006-10-05 14:55:46 +0100481done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 u8 status;
484 struct urb *urb;
485 int urbstat = -EINPROGRESS;
486
487 if (unlikely(!ep))
488 return;
489
490 status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
491
492 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
493
494 /* we can safely ignore NAKs */
495 if (status & SL11H_STATMASK_NAK) {
496 // PACKET("...NAK_%02x qh%p\n", bank, ep);
497 if (!ep->period)
498 ep->nak_count++;
499 ep->error_count = 0;
500
501 /* ACK advances transfer, toggle, and maybe queue */
502 } else if (status & SL11H_STATMASK_ACK) {
503 struct usb_device *udev = urb->dev;
504 int len;
505 unsigned char *buf;
506
507 /* urb->iso_frame_desc is currently ignored here... */
508
509 ep->nak_count = ep->error_count = 0;
510 switch (ep->nextpid) {
511 case USB_PID_OUT:
512 // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
513 urb->actual_length += ep->length;
514 usb_dotoggle(udev, ep->epnum, 1);
515 if (urb->actual_length
516 == urb->transfer_buffer_length) {
517 if (usb_pipecontrol(urb->pipe))
518 ep->nextpid = USB_PID_ACK;
519
520 /* some bulk protocols terminate OUT transfers
521 * by a short packet, using ZLPs not padding.
522 */
523 else if (ep->length < ep->maxpacket
524 || !(urb->transfer_flags
525 & URB_ZERO_PACKET))
526 urbstat = 0;
527 }
528 break;
529 case USB_PID_IN:
530 // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
531 buf = urb->transfer_buffer + urb->actual_length;
532 prefetchw(buf);
533 len = ep->maxpacket - sl811_read(sl811,
534 bank + SL11H_XFERCNTREG);
535 if (len > ep->length) {
536 len = ep->length;
Alan Stern65e51092007-08-24 15:40:47 -0400537 urbstat = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 urb->actual_length += len;
540 sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
541 buf, len);
542 usb_dotoggle(udev, ep->epnum, 0);
Alan Stern65e51092007-08-24 15:40:47 -0400543 if (urbstat == -EINPROGRESS &&
544 (len < ep->maxpacket ||
545 urb->actual_length ==
546 urb->transfer_buffer_length)) {
547 if (usb_pipecontrol(urb->pipe))
548 ep->nextpid = USB_PID_ACK;
549 else
550 urbstat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 break;
553 case USB_PID_SETUP:
554 // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
555 if (urb->transfer_buffer_length == urb->actual_length)
556 ep->nextpid = USB_PID_ACK;
557 else if (usb_pipeout(urb->pipe)) {
558 usb_settoggle(udev, 0, 1, 1);
559 ep->nextpid = USB_PID_OUT;
560 } else {
561 usb_settoggle(udev, 0, 0, 1);
562 ep->nextpid = USB_PID_IN;
563 }
564 break;
565 case USB_PID_ACK:
566 // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
567 urbstat = 0;
568 break;
569 }
570
571 /* STALL stops all transfers */
572 } else if (status & SL11H_STATMASK_STALL) {
573 PACKET("...STALL_%02x qh%p\n", bank, ep);
574 ep->nak_count = ep->error_count = 0;
575 urbstat = -EPIPE;
576
577 /* error? retry, until "3 strikes" */
578 } else if (++ep->error_count >= 3) {
579 if (status & SL11H_STATMASK_TMOUT)
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700580 urbstat = -ETIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 else if (status & SL11H_STATMASK_OVF)
582 urbstat = -EOVERFLOW;
583 else
584 urbstat = -EPROTO;
585 ep->error_count = 0;
586 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
587 bank, status, ep, urbstat);
588 }
589
Alan Stern65e51092007-08-24 15:40:47 -0400590 if (urbstat != -EINPROGRESS || urb->unlinked)
David Howells7d12e782006-10-05 14:55:46 +0100591 finish_request(sl811, ep, urb, urbstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static inline u8 checkdone(struct sl811 *sl811)
595{
596 u8 ctl;
597 u8 irqstat = 0;
598
599 if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
600 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
601 if (ctl & SL11H_HCTLMASK_ARM)
602 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
603 DBG("%s DONE_A: ctrl %02x sts %02x\n",
604 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
605 ctl,
606 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
607 irqstat |= SL11H_INTMASK_DONE_A;
608 }
609#ifdef USE_B
610 if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
611 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
612 if (ctl & SL11H_HCTLMASK_ARM)
613 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
614 DBG("%s DONE_B: ctrl %02x sts %02x\n",
615 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
616 ctl,
617 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
618 irqstat |= SL11H_INTMASK_DONE_A;
619 }
620#endif
621 return irqstat;
622}
623
David Howells7d12e782006-10-05 14:55:46 +0100624static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct sl811 *sl811 = hcd_to_sl811(hcd);
627 u8 irqstat;
628 irqreturn_t ret = IRQ_NONE;
629 unsigned retries = 5;
630
631 spin_lock(&sl811->lock);
632
633retry:
634 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
635 if (irqstat) {
636 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
637 irqstat &= sl811->irq_enable;
638 }
639
640#ifdef QUIRK2
641 /* this may no longer be necessary ... */
David Brownell1e9a47b2005-05-26 05:55:55 -0700642 if (irqstat == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 irqstat = checkdone(sl811);
David Brownell1e9a47b2005-05-26 05:55:55 -0700644 if (irqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 sl811->stat_lost++;
646 }
647#endif
648
649 /* USB packets, not necessarily handled in the order they're
650 * issued ... that's fine if they're different endpoints.
651 */
652 if (irqstat & SL11H_INTMASK_DONE_A) {
David Howells7d12e782006-10-05 14:55:46 +0100653 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 sl811->active_a = NULL;
655 sl811->stat_a++;
656 }
657#ifdef USE_B
658 if (irqstat & SL11H_INTMASK_DONE_B) {
David Howells7d12e782006-10-05 14:55:46 +0100659 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 sl811->active_b = NULL;
661 sl811->stat_b++;
662 }
663#endif
664 if (irqstat & SL11H_INTMASK_SOFINTR) {
665 unsigned index;
666
667 index = sl811->frame++ % (PERIODIC_SIZE - 1);
668 sl811->stat_sof++;
669
670 /* be graceful about almost-inevitable periodic schedule
671 * overruns: continue the previous frame's transfers iff
672 * this one has nothing scheduled.
673 */
674 if (sl811->next_periodic) {
675 // ERR("overrun to slot %d\n", index);
676 sl811->stat_overrun++;
677 }
678 if (sl811->periodic[index])
679 sl811->next_periodic = sl811->periodic[index];
680 }
681
682 /* khubd manages debouncing and wakeup */
683 if (irqstat & SL11H_INTMASK_INSRMV) {
684 sl811->stat_insrmv++;
685
686 /* most stats are reset for each VBUS session */
687 sl811->stat_wake = 0;
688 sl811->stat_sof = 0;
689 sl811->stat_a = 0;
690 sl811->stat_b = 0;
691 sl811->stat_lost = 0;
692
693 sl811->ctrl1 = 0;
694 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
695
696 sl811->irq_enable = SL11H_INTMASK_INSRMV;
697 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
698
699 /* usbcore nukes other pending transactions on disconnect */
700 if (sl811->active_a) {
701 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
702 finish_request(sl811, sl811->active_a,
David Brownell1e9a47b2005-05-26 05:55:55 -0700703 container_of(sl811->active_a
704 ->hep->urb_list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct urb, urb_list),
David Howells7d12e782006-10-05 14:55:46 +0100706 -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 sl811->active_a = NULL;
708 }
709#ifdef USE_B
710 if (sl811->active_b) {
711 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
712 finish_request(sl811, sl811->active_b,
David Brownell1e9a47b2005-05-26 05:55:55 -0700713 container_of(sl811->active_b
714 ->hep->urb_list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct urb, urb_list),
716 NULL, -ESHUTDOWN);
717 sl811->active_b = NULL;
718 }
719#endif
720
Steven Cole093cf722005-05-03 19:07:24 -0600721 /* port status seems weird until after reset, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * force the reset and make khubd clean up later.
723 */
Michael Hennerich8a3461e2010-04-28 17:31:36 -0400724 if (irqstat & SL11H_INTMASK_RD)
Alan Stern749da5f2010-03-04 17:05:08 -0500725 sl811->port1 &= ~USB_PORT_STAT_CONNECTION;
Michael Hennerich8a3461e2010-04-28 17:31:36 -0400726 else
Alan Stern749da5f2010-03-04 17:05:08 -0500727 sl811->port1 |= USB_PORT_STAT_CONNECTION;
Hennerich, Michaeleb661bc2009-09-02 09:26:21 +0100728
Alan Stern749da5f2010-03-04 17:05:08 -0500729 sl811->port1 |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 } else if (irqstat & SL11H_INTMASK_RD) {
Alan Stern749da5f2010-03-04 17:05:08 -0500732 if (sl811->port1 & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 DBG("wakeup\n");
Alan Stern749da5f2010-03-04 17:05:08 -0500734 sl811->port1 |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 sl811->stat_wake++;
736 } else
737 irqstat &= ~SL11H_INTMASK_RD;
738 }
739
740 if (irqstat) {
Alan Stern749da5f2010-03-04 17:05:08 -0500741 if (sl811->port1 & USB_PORT_STAT_ENABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 start_transfer(sl811);
743 ret = IRQ_HANDLED;
744 if (retries--)
745 goto retry;
746 }
747
David Brownell1e9a47b2005-05-26 05:55:55 -0700748 if (sl811->periodic_count == 0 && list_empty(&sl811->async))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 sofirq_off(sl811);
750 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
751
752 spin_unlock(&sl811->lock);
753
754 return ret;
755}
756
757/*-------------------------------------------------------------------------*/
758
759/* usb 1.1 says max 90% of a frame is available for periodic transfers.
760 * this driver doesn't promise that much since it's got to handle an
761 * IRQ per packet; irq handling latencies also use up that time.
David Brownell4b2e7902005-09-22 00:49:07 -0700762 *
763 * NOTE: the periodic schedule is a sparse tree, with the load for
764 * each branch minimized. see fig 3.5 in the OHCI spec for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
766#define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */
767
768static int balance(struct sl811 *sl811, u16 period, u16 load)
769{
770 int i, branch = -ENOSPC;
771
772 /* search for the least loaded schedule branch of that period
773 * which has enough bandwidth left unreserved.
774 */
775 for (i = 0; i < period ; i++) {
776 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
777 int j;
778
779 for (j = i; j < PERIODIC_SIZE; j += period) {
780 if ((sl811->load[j] + load)
781 > MAX_PERIODIC_LOAD)
782 break;
783 }
784 if (j < PERIODIC_SIZE)
785 continue;
David Brownell1e9a47b2005-05-26 05:55:55 -0700786 branch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 }
789 return branch;
790}
791
792/*-------------------------------------------------------------------------*/
793
794static int sl811h_urb_enqueue(
795 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -0400797 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798) {
799 struct sl811 *sl811 = hcd_to_sl811(hcd);
800 struct usb_device *udev = urb->dev;
801 unsigned int pipe = urb->pipe;
802 int is_out = !usb_pipein(pipe);
803 int type = usb_pipetype(pipe);
804 int epnum = usb_pipeendpoint(pipe);
805 struct sl811h_ep *ep = NULL;
806 unsigned long flags;
807 int i;
Alan Sterne9df41c2007-08-08 11:48:02 -0400808 int retval;
809 struct usb_host_endpoint *hep = urb->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811#ifdef DISABLE_ISO
812 if (type == PIPE_ISOCHRONOUS)
813 return -ENOSPC;
814#endif
815
816 /* avoid all allocations within spinlocks */
Kulikov Vasiliy76be9322010-07-16 20:15:06 +0400817 if (!hep->hcpriv) {
Pekka Enberg7b842b62005-09-06 15:18:34 -0700818 ep = kzalloc(sizeof *ep, mem_flags);
Kulikov Vasiliy76be9322010-07-16 20:15:06 +0400819 if (ep == NULL)
820 return -ENOMEM;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 spin_lock_irqsave(&sl811->lock, flags);
824
825 /* don't submit to a dead or disabled port */
Alan Stern749da5f2010-03-04 17:05:08 -0500826 if (!(sl811->port1 & USB_PORT_STAT_ENABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 || !HC_IS_RUNNING(hcd->state)) {
828 retval = -ENODEV;
David Brownell4b2e7902005-09-22 00:49:07 -0700829 kfree(ep);
Alan Sterne9df41c2007-08-08 11:48:02 -0400830 goto fail_not_linked;
831 }
832 retval = usb_hcd_link_urb_to_ep(hcd, urb);
833 if (retval) {
834 kfree(ep);
835 goto fail_not_linked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
838 if (hep->hcpriv) {
839 kfree(ep);
840 ep = hep->hcpriv;
841 } else if (!ep) {
842 retval = -ENOMEM;
843 goto fail;
844
845 } else {
846 INIT_LIST_HEAD(&ep->schedule);
Alan Stern6a8e87b2006-01-19 10:46:27 -0500847 ep->udev = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ep->epnum = epnum;
849 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
850 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
851 usb_settoggle(udev, epnum, is_out, 0);
852
853 if (type == PIPE_CONTROL)
854 ep->nextpid = USB_PID_SETUP;
855 else if (is_out)
856 ep->nextpid = USB_PID_OUT;
857 else
858 ep->nextpid = USB_PID_IN;
859
860 if (ep->maxpacket > H_MAXPACKET) {
861 /* iso packets up to 240 bytes could work... */
862 DBG("dev %d ep%d maxpacket %d\n",
863 udev->devnum, epnum, ep->maxpacket);
864 retval = -EINVAL;
Jesper Juhl2bd15f12011-01-23 23:08:31 +0100865 kfree(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto fail;
867 }
868
869 if (udev->speed == USB_SPEED_LOW) {
870 /* send preamble for external hub? */
871 if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
872 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
873 }
874 switch (type) {
875 case PIPE_ISOCHRONOUS:
876 case PIPE_INTERRUPT:
877 if (urb->interval > PERIODIC_SIZE)
878 urb->interval = PERIODIC_SIZE;
879 ep->period = urb->interval;
880 ep->branch = PERIODIC_SIZE;
881 if (type == PIPE_ISOCHRONOUS)
882 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
883 ep->load = usb_calc_bus_time(udev->speed, !is_out,
884 (type == PIPE_ISOCHRONOUS),
885 usb_maxpacket(udev, pipe, is_out))
886 / 1000;
887 break;
888 }
889
David Brownell1e9a47b2005-05-26 05:55:55 -0700890 ep->hep = hep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 hep->hcpriv = ep;
892 }
893
894 /* maybe put endpoint into schedule */
895 switch (type) {
896 case PIPE_CONTROL:
897 case PIPE_BULK:
898 if (list_empty(&ep->schedule))
899 list_add_tail(&ep->schedule, &sl811->async);
900 break;
901 case PIPE_ISOCHRONOUS:
902 case PIPE_INTERRUPT:
903 urb->interval = ep->period;
David Brownell4b2e7902005-09-22 00:49:07 -0700904 if (ep->branch < PERIODIC_SIZE) {
905 /* NOTE: the phase is correct here, but the value
906 * needs offsetting by the transfer queue depth.
907 * All current drivers ignore start_frame, so this
908 * is unlikely to ever matter...
909 */
910 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
911 + ep->branch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
David Brownell4b2e7902005-09-22 00:49:07 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 retval = balance(sl811, ep->period, ep->load);
916 if (retval < 0)
917 goto fail;
918 ep->branch = retval;
919 retval = 0;
920 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
921 + ep->branch;
922
923 /* sort each schedule branch by period (slow before fast)
924 * to share the faster parts of the tree without needing
925 * dummy/placeholder nodes
926 */
927 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
928 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
929 struct sl811h_ep **prev = &sl811->periodic[i];
930 struct sl811h_ep *here = *prev;
931
932 while (here && ep != here) {
933 if (ep->period > here->period)
934 break;
935 prev = &here->next;
936 here = *prev;
937 }
938 if (ep != here) {
939 ep->next = here;
940 *prev = ep;
941 }
942 sl811->load[i] += ep->load;
943 }
944 sl811->periodic_count++;
945 hcd->self.bandwidth_allocated += ep->load / ep->period;
946 sofirq_on(sl811);
947 }
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 urb->hcpriv = hep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 start_transfer(sl811);
951 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
952fail:
Alan Sterne9df41c2007-08-08 11:48:02 -0400953 if (retval)
954 usb_hcd_unlink_urb_from_ep(hcd, urb);
955fail_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 spin_unlock_irqrestore(&sl811->lock, flags);
957 return retval;
958}
959
Alan Sterne9df41c2007-08-08 11:48:02 -0400960static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct sl811 *sl811 = hcd_to_sl811(hcd);
David Brownell1e9a47b2005-05-26 05:55:55 -0700963 struct usb_host_endpoint *hep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 unsigned long flags;
965 struct sl811h_ep *ep;
Alan Sterne9df41c2007-08-08 11:48:02 -0400966 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_lock_irqsave(&sl811->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400969 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
970 if (retval)
David Brownell1e9a47b2005-05-26 05:55:55 -0700971 goto fail;
972
Alan Sterne9df41c2007-08-08 11:48:02 -0400973 hep = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 ep = hep->hcpriv;
975 if (ep) {
976 /* finish right away if this urb can't be active ...
977 * note that some drivers wrongly expect delays
978 */
979 if (ep->hep->urb_list.next != &urb->urb_list) {
980 /* not front of queue? never active */
981
982 /* for active transfers, we expect an IRQ */
983 } else if (sl811->active_a == ep) {
984 if (time_before_eq(sl811->jiffies_a, jiffies)) {
985 /* happens a lot with lowspeed?? */
986 DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
987 sl811_read(sl811,
988 SL811_EP_A(SL11H_HOSTCTLREG)),
989 sl811_read(sl811,
990 SL811_EP_A(SL11H_PKTSTATREG)));
991 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
992 0);
993 sl811->active_a = NULL;
994 } else
995 urb = NULL;
996#ifdef USE_B
997 } else if (sl811->active_b == ep) {
998 if (time_before_eq(sl811->jiffies_a, jiffies)) {
999 /* happens a lot with lowspeed?? */
1000 DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
1001 sl811_read(sl811,
1002 SL811_EP_B(SL11H_HOSTCTLREG)),
1003 sl811_read(sl811,
1004 SL811_EP_B(SL11H_PKTSTATREG)));
1005 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1006 0);
1007 sl811->active_b = NULL;
1008 } else
1009 urb = NULL;
1010#endif
1011 } else {
1012 /* front of queue for inactive endpoint */
1013 }
1014
1015 if (urb)
David Howells7d12e782006-10-05 14:55:46 +01001016 finish_request(sl811, ep, urb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 else
1018 VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
1019 (sl811->active_a == ep) ? "A" : "B");
1020 } else
1021 retval = -EINVAL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001022 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 spin_unlock_irqrestore(&sl811->lock, flags);
1024 return retval;
1025}
1026
1027static void
1028sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1029{
1030 struct sl811h_ep *ep = hep->hcpriv;
1031
1032 if (!ep)
1033 return;
1034
1035 /* assume we'd just wait for the irq */
1036 if (!list_empty(&hep->urb_list))
1037 msleep(3);
1038 if (!list_empty(&hep->urb_list))
Arjan van de Venb6c63932008-07-25 01:45:52 -07001039 WARNING("ep %p not empty?\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree(ep);
1042 hep->hcpriv = NULL;
1043}
1044
1045static int
1046sl811h_get_frame(struct usb_hcd *hcd)
1047{
1048 struct sl811 *sl811 = hcd_to_sl811(hcd);
1049
1050 /* wrong except while periodic transfers are scheduled;
1051 * never matches the on-the-wire frame;
1052 * subject to overruns.
1053 */
1054 return sl811->frame;
1055}
1056
1057
1058/*-------------------------------------------------------------------------*/
1059
1060/* the virtual root hub timer IRQ checks for hub status */
1061static int
1062sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1063{
1064 struct sl811 *sl811 = hcd_to_sl811(hcd);
1065#ifdef QUIRK3
1066 unsigned long flags;
1067
1068 /* non-SMP HACK: use root hub timer as i/o watchdog
1069 * this seems essential when SOF IRQs aren't in use...
1070 */
1071 local_irq_save(flags);
1072 if (!timer_pending(&sl811->timer)) {
David Howells7d12e782006-10-05 14:55:46 +01001073 if (sl811h_irq( /* ~0, */ hcd) != IRQ_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 sl811->stat_lost++;
1075 }
1076 local_irq_restore(flags);
1077#endif
1078
1079 if (!(sl811->port1 & (0xffff << 16)))
1080 return 0;
1081
1082 /* tell khubd port 1 changed */
1083 *buf = (1 << 1);
1084 return 1;
1085}
1086
1087static void
1088sl811h_hub_descriptor (
1089 struct sl811 *sl811,
1090 struct usb_hub_descriptor *desc
1091) {
1092 u16 temp = 0;
1093
1094 desc->bDescriptorType = 0x29;
1095 desc->bHubContrCurrent = 0;
1096
1097 desc->bNbrPorts = 1;
1098 desc->bDescLength = 9;
1099
1100 /* per-port power switching (gang of one!), or none */
1101 desc->bPwrOn2PwrGood = 0;
1102 if (sl811->board && sl811->board->port_power) {
1103 desc->bPwrOn2PwrGood = sl811->board->potpg;
1104 if (!desc->bPwrOn2PwrGood)
1105 desc->bPwrOn2PwrGood = 10;
1106 temp = 0x0001;
1107 } else
1108 temp = 0x0002;
1109
1110 /* no overcurrent errors detection/handling */
1111 temp |= 0x0010;
1112
Al Virofd05e722008-04-28 07:00:16 +01001113 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Sarah Sharpda130512010-11-30 15:55:51 -08001115 /* ports removable, and legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001116 desc->u.hs.DeviceRemovable[0] = 0 << 1;
1117 desc->u.hs.DeviceRemovable[1] = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120static void
1121sl811h_timer(unsigned long _sl811)
1122{
1123 struct sl811 *sl811 = (void *) _sl811;
1124 unsigned long flags;
1125 u8 irqstat;
1126 u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
Alan Stern749da5f2010-03-04 17:05:08 -05001127 const u32 mask = USB_PORT_STAT_CONNECTION
1128 | USB_PORT_STAT_ENABLE
Alan Stern288ead42010-03-04 11:32:30 -05001129 | USB_PORT_STAT_LOW_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 spin_lock_irqsave(&sl811->lock, flags);
1132
1133 /* stop special signaling */
1134 sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1135 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1136 udelay(3);
1137
1138 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1139
1140 switch (signaling) {
1141 case SL11H_CTL1MASK_SE0:
1142 DBG("end reset\n");
Alan Stern749da5f2010-03-04 17:05:08 -05001143 sl811->port1 = (USB_PORT_STAT_C_RESET << 16)
1144 | USB_PORT_STAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sl811->ctrl1 = 0;
1146 /* don't wrongly ack RD */
1147 if (irqstat & SL11H_INTMASK_INSRMV)
1148 irqstat &= ~SL11H_INTMASK_RD;
1149 break;
1150 case SL11H_CTL1MASK_K:
1151 DBG("end resume\n");
Alan Stern749da5f2010-03-04 17:05:08 -05001152 sl811->port1 &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 default:
1155 DBG("odd timer signaling: %02x\n", signaling);
1156 break;
1157 }
1158 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1159
1160 if (irqstat & SL11H_INTMASK_RD) {
1161 /* usbcore nukes all pending transactions on disconnect */
Alan Stern749da5f2010-03-04 17:05:08 -05001162 if (sl811->port1 & USB_PORT_STAT_CONNECTION)
1163 sl811->port1 |= (USB_PORT_STAT_C_CONNECTION << 16)
1164 | (USB_PORT_STAT_C_ENABLE << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 sl811->port1 &= ~mask;
1166 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1167 } else {
1168 sl811->port1 |= mask;
1169 if (irqstat & SL11H_INTMASK_DP)
Alan Stern288ead42010-03-04 11:32:30 -05001170 sl811->port1 &= ~USB_PORT_STAT_LOW_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1172 }
1173
Alan Stern749da5f2010-03-04 17:05:08 -05001174 if (sl811->port1 & USB_PORT_STAT_CONNECTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 u8 ctrl2 = SL811HS_CTL2_INIT;
1176
1177 sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1178#ifdef USE_B
1179 sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1180#endif
Alan Stern288ead42010-03-04 11:32:30 -05001181 if (sl811->port1 & USB_PORT_STAT_LOW_SPEED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1183 ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1184 }
1185
1186 /* start SOFs flowing, kickstarting with A registers */
1187 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1188 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1189 sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1190
1191 /* autoincrementing */
1192 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1193 writeb(SL_SOF, sl811->data_reg);
1194 writeb(0, sl811->data_reg);
1195 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1196 SL11H_HCTLMASK_ARM);
1197
1198 /* khubd provides debounce delay */
1199 } else {
1200 sl811->ctrl1 = 0;
1201 }
1202 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1203
1204 /* reenable irqs */
1205 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1206 spin_unlock_irqrestore(&sl811->lock, flags);
1207}
1208
1209static int
1210sl811h_hub_control(
1211 struct usb_hcd *hcd,
1212 u16 typeReq,
1213 u16 wValue,
1214 u16 wIndex,
1215 char *buf,
1216 u16 wLength
1217) {
1218 struct sl811 *sl811 = hcd_to_sl811(hcd);
1219 int retval = 0;
1220 unsigned long flags;
1221
1222 spin_lock_irqsave(&sl811->lock, flags);
1223
1224 switch (typeReq) {
1225 case ClearHubFeature:
1226 case SetHubFeature:
1227 switch (wValue) {
1228 case C_HUB_OVER_CURRENT:
1229 case C_HUB_LOCAL_POWER:
1230 break;
1231 default:
1232 goto error;
1233 }
1234 break;
1235 case ClearPortFeature:
1236 if (wIndex != 1 || wLength != 0)
1237 goto error;
1238
1239 switch (wValue) {
1240 case USB_PORT_FEAT_ENABLE:
Alan Stern749da5f2010-03-04 17:05:08 -05001241 sl811->port1 &= USB_PORT_STAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 sl811->ctrl1 = 0;
1243 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1244 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1245 sl811_write(sl811, SL11H_IRQ_ENABLE,
1246 sl811->irq_enable);
1247 break;
1248 case USB_PORT_FEAT_SUSPEND:
Alan Stern749da5f2010-03-04 17:05:08 -05001249 if (!(sl811->port1 & USB_PORT_STAT_SUSPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251
1252 /* 20 msec of resume/K signaling, other irqs blocked */
1253 DBG("start resume...\n");
1254 sl811->irq_enable = 0;
1255 sl811_write(sl811, SL11H_IRQ_ENABLE,
1256 sl811->irq_enable);
1257 sl811->ctrl1 |= SL11H_CTL1MASK_K;
1258 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1259
1260 mod_timer(&sl811->timer, jiffies
1261 + msecs_to_jiffies(20));
1262 break;
1263 case USB_PORT_FEAT_POWER:
1264 port_power(sl811, 0);
1265 break;
1266 case USB_PORT_FEAT_C_ENABLE:
1267 case USB_PORT_FEAT_C_SUSPEND:
1268 case USB_PORT_FEAT_C_CONNECTION:
1269 case USB_PORT_FEAT_C_OVER_CURRENT:
1270 case USB_PORT_FEAT_C_RESET:
1271 break;
1272 default:
1273 goto error;
1274 }
1275 sl811->port1 &= ~(1 << wValue);
1276 break;
1277 case GetHubDescriptor:
1278 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1279 break;
1280 case GetHubStatus:
Michael Hennerich8ca5bfab2009-12-21 12:53:24 -05001281 put_unaligned_le32(0, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
1283 case GetPortStatus:
1284 if (wIndex != 1)
1285 goto error;
Michael Hennerich8ca5bfab2009-12-21 12:53:24 -05001286 put_unaligned_le32(sl811->port1, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288#ifndef VERBOSE
1289 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
1290#endif
1291 DBG("GetPortStatus %08x\n", sl811->port1);
1292 break;
1293 case SetPortFeature:
1294 if (wIndex != 1 || wLength != 0)
1295 goto error;
1296 switch (wValue) {
1297 case USB_PORT_FEAT_SUSPEND:
Alan Stern749da5f2010-03-04 17:05:08 -05001298 if (sl811->port1 & USB_PORT_STAT_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto error;
Alan Stern749da5f2010-03-04 17:05:08 -05001300 if (!(sl811->port1 & USB_PORT_STAT_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto error;
1302
1303 DBG("suspend...\n");
1304 sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1305 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1306 break;
1307 case USB_PORT_FEAT_POWER:
1308 port_power(sl811, 1);
1309 break;
1310 case USB_PORT_FEAT_RESET:
Alan Stern749da5f2010-03-04 17:05:08 -05001311 if (sl811->port1 & USB_PORT_STAT_SUSPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto error;
Alan Stern749da5f2010-03-04 17:05:08 -05001313 if (!(sl811->port1 & USB_PORT_STAT_POWER))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 break;
1315
1316 /* 50 msec of reset/SE0 signaling, irqs blocked */
1317 sl811->irq_enable = 0;
1318 sl811_write(sl811, SL11H_IRQ_ENABLE,
1319 sl811->irq_enable);
1320 sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1321 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
Alan Stern749da5f2010-03-04 17:05:08 -05001322 sl811->port1 |= USB_PORT_STAT_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 mod_timer(&sl811->timer, jiffies
1324 + msecs_to_jiffies(50));
1325 break;
1326 default:
1327 goto error;
1328 }
1329 sl811->port1 |= 1 << wValue;
1330 break;
1331
1332 default:
1333error:
1334 /* "protocol stall" on error */
1335 retval = -EPIPE;
1336 }
1337
1338 spin_unlock_irqrestore(&sl811->lock, flags);
1339 return retval;
1340}
1341
1342#ifdef CONFIG_PM
1343
1344static int
Alan Stern0c0382e2005-10-13 17:08:02 -04001345sl811h_bus_suspend(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 // SOFs off
Harvey Harrison441b62c2008-03-03 16:08:34 -08001348 DBG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350}
1351
1352static int
Alan Stern0c0382e2005-10-13 17:08:02 -04001353sl811h_bus_resume(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 // SOFs on
Harvey Harrison441b62c2008-03-03 16:08:34 -08001356 DBG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
1358}
1359
1360#else
1361
Alan Stern0c0382e2005-10-13 17:08:02 -04001362#define sl811h_bus_suspend NULL
1363#define sl811h_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365#endif
1366
1367
1368/*-------------------------------------------------------------------------*/
1369
1370#ifdef STUB_DEBUG_FILE
1371
1372static inline void create_debug_file(struct sl811 *sl811) { }
1373static inline void remove_debug_file(struct sl811 *sl811) { }
1374
1375#else
1376
1377#include <linux/proc_fs.h>
1378#include <linux/seq_file.h>
1379
1380static void dump_irq(struct seq_file *s, char *label, u8 mask)
1381{
1382 seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1383 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1384 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1385 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1386 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1387 (mask & SL11H_INTMASK_RD) ? " rd" : "",
1388 (mask & SL11H_INTMASK_DP) ? " dp" : "");
1389}
1390
1391static int proc_sl811h_show(struct seq_file *s, void *unused)
1392{
1393 struct sl811 *sl811 = s->private;
1394 struct sl811h_ep *ep;
1395 unsigned i;
1396
1397 seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1398 sl811_to_hcd(sl811)->product_desc,
1399 hcd_name, DRIVER_VERSION,
1400 sl811->port1);
1401
1402 seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1403 seq_printf(s, "current session: done_a %ld done_b %ld "
1404 "wake %ld sof %ld overrun %ld lost %ld\n\n",
1405 sl811->stat_a, sl811->stat_b,
1406 sl811->stat_wake, sl811->stat_sof,
1407 sl811->stat_overrun, sl811->stat_lost);
1408
1409 spin_lock_irq(&sl811->lock);
1410
1411 if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1412 seq_printf(s, "(suspended)\n\n");
1413 else {
1414 u8 t = sl811_read(sl811, SL11H_CTLREG1);
1415
1416 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1417 (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1418 ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1419 case SL11H_CTL1MASK_NORMAL: s = ""; break;
1420 case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1421 case SL11H_CTL1MASK_K: s = " k/resume"; break;
1422 default: s = "j"; break;
1423 }; s; }),
1424 (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1425 (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1426
1427 dump_irq(s, "irq_enable",
1428 sl811_read(sl811, SL11H_IRQ_ENABLE));
1429 dump_irq(s, "irq_status",
1430 sl811_read(sl811, SL11H_IRQ_STATUS));
1431 seq_printf(s, "frame clocks remaining: %d\n",
1432 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1433 }
1434
1435 seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1436 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1437 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1438 seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1439 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1440 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1441 seq_printf(s, "\n");
1442 list_for_each_entry (ep, &sl811->async, schedule) {
1443 struct urb *urb;
1444
1445 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1446 " nak %d err %d\n",
1447 (ep == sl811->active_a) ? "(A) " : "",
1448 (ep == sl811->active_b) ? "(B) " : "",
1449 ep, ep->epnum,
1450 ({ char *s; switch (ep->nextpid) {
1451 case USB_PID_IN: s = "in"; break;
1452 case USB_PID_OUT: s = "out"; break;
1453 case USB_PID_SETUP: s = "setup"; break;
1454 case USB_PID_ACK: s = "status"; break;
1455 default: s = "?"; break;
1456 }; s;}),
1457 ep->maxpacket,
1458 ep->nak_count, ep->error_count);
1459 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1460 seq_printf(s, " urb%p, %d/%d\n", urb,
1461 urb->actual_length,
1462 urb->transfer_buffer_length);
1463 }
1464 }
1465 if (!list_empty(&sl811->async))
1466 seq_printf(s, "\n");
1467
1468 seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1469
1470 for (i = 0; i < PERIODIC_SIZE; i++) {
1471 ep = sl811->periodic[i];
1472 if (!ep)
1473 continue;
1474 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1475
1476 /* DUMB: prints shared entries multiple times */
1477 do {
1478 seq_printf(s,
1479 " %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1480 "err %d\n",
1481 (ep == sl811->active_a) ? "(A) " : "",
1482 (ep == sl811->active_b) ? "(B) " : "",
1483 ep->period, ep,
1484 (ep->udev->speed == USB_SPEED_FULL)
1485 ? "" : "ls ",
1486 ep->udev->devnum, ep->epnum,
1487 (ep->epnum == 0) ? ""
1488 : ((ep->nextpid == USB_PID_IN)
1489 ? "in"
1490 : "out"),
1491 ep->maxpacket, ep->error_count);
1492 ep = ep->next;
1493 } while (ep);
1494 }
1495
1496 spin_unlock_irq(&sl811->lock);
1497 seq_printf(s, "\n");
1498
1499 return 0;
1500}
1501
1502static int proc_sl811h_open(struct inode *inode, struct file *file)
1503{
1504 return single_open(file, proc_sl811h_show, PDE(inode)->data);
1505}
1506
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03001507static const struct file_operations proc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 .open = proc_sl811h_open,
1509 .read = seq_read,
1510 .llseek = seq_lseek,
1511 .release = single_release,
1512};
1513
1514/* expect just one sl811 per system */
1515static const char proc_filename[] = "driver/sl811h";
1516
1517static void create_debug_file(struct sl811 *sl811)
1518{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07001519 sl811->pde = proc_create_data(proc_filename, 0, NULL, &proc_ops, sl811);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static void remove_debug_file(struct sl811 *sl811)
1523{
1524 if (sl811->pde)
1525 remove_proc_entry(proc_filename, NULL);
1526}
1527
1528#endif
1529
1530/*-------------------------------------------------------------------------*/
1531
1532static void
1533sl811h_stop(struct usb_hcd *hcd)
1534{
1535 struct sl811 *sl811 = hcd_to_sl811(hcd);
1536 unsigned long flags;
1537
1538 del_timer_sync(&hcd->rh_timer);
1539
1540 spin_lock_irqsave(&sl811->lock, flags);
1541 port_power(sl811, 0);
1542 spin_unlock_irqrestore(&sl811->lock, flags);
1543}
1544
1545static int
1546sl811h_start(struct usb_hcd *hcd)
1547{
1548 struct sl811 *sl811 = hcd_to_sl811(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* chip has been reset, VBUS power is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 hcd->state = HC_STATE_RUNNING;
1552
Alan Sternbc96c0a2005-04-25 11:21:31 -04001553 if (sl811->board) {
David Brownell0c8624f2005-11-07 15:31:25 -08001554 if (!device_can_wakeup(hcd->self.controller))
1555 device_init_wakeup(hcd->self.controller,
1556 sl811->board->can_wakeup);
Alan Sternbc96c0a2005-04-25 11:21:31 -04001557 hcd->power_budget = sl811->board->power * 2;
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001560 /* enable power and interrupts */
David Brownell1e9a47b2005-05-26 05:55:55 -07001561 port_power(sl811, 1);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564}
1565
1566/*-------------------------------------------------------------------------*/
1567
1568static struct hc_driver sl811h_hc_driver = {
1569 .description = hcd_name,
1570 .hcd_priv_size = sizeof(struct sl811),
1571
1572 /*
1573 * generic hardware linkage
1574 */
1575 .irq = sl811h_irq,
1576 .flags = HCD_USB11 | HCD_MEMORY,
1577
1578 /* Basic lifecycle operations */
1579 .start = sl811h_start,
1580 .stop = sl811h_stop,
1581
1582 /*
1583 * managing i/o requests and associated device resources
1584 */
1585 .urb_enqueue = sl811h_urb_enqueue,
1586 .urb_dequeue = sl811h_urb_dequeue,
1587 .endpoint_disable = sl811h_endpoint_disable,
1588
1589 /*
1590 * periodic schedule support
1591 */
1592 .get_frame_number = sl811h_get_frame,
1593
1594 /*
1595 * root hub support
1596 */
1597 .hub_status_data = sl811h_hub_status_data,
1598 .hub_control = sl811h_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04001599 .bus_suspend = sl811h_bus_suspend,
1600 .bus_resume = sl811h_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601};
1602
1603/*-------------------------------------------------------------------------*/
1604
David Brownell1e9a47b2005-05-26 05:55:55 -07001605static int __devexit
Russell King3ae5eae2005-11-09 22:32:44 +00001606sl811h_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Russell King3ae5eae2005-11-09 22:32:44 +00001608 struct usb_hcd *hcd = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct sl811 *sl811 = hcd_to_sl811(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct resource *res;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 remove_debug_file(sl811);
1613 usb_remove_hcd(hcd);
1614
David Brownell1e9a47b2005-05-26 05:55:55 -07001615 /* some platforms may use IORESOURCE_IO */
Russell King3ae5eae2005-11-09 22:32:44 +00001616 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
David Brownell1e9a47b2005-05-26 05:55:55 -07001617 if (res)
1618 iounmap(sl811->data_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Russell King3ae5eae2005-11-09 22:32:44 +00001620 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
David Brownell1e9a47b2005-05-26 05:55:55 -07001621 if (res)
1622 iounmap(sl811->addr_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 usb_put_hcd(hcd);
1625 return 0;
1626}
1627
David Brownell1e9a47b2005-05-26 05:55:55 -07001628static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +00001629sl811h_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct usb_hcd *hcd;
1632 struct sl811 *sl811;
Marc Zyngier27140212008-08-18 13:08:42 +02001633 struct resource *addr, *data, *ires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int irq;
1635 void __iomem *addr_reg;
1636 void __iomem *data_reg;
1637 int retval;
David Brownell1e9a47b2005-05-26 05:55:55 -07001638 u8 tmp, ioaddr = 0;
Marc Zyngier27140212008-08-18 13:08:42 +02001639 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /* basic sanity checks first. board-specific init logic should
1642 * have initialized these three resources and probably board
1643 * specific platform_data. we don't probe for IRQs, and do only
1644 * minimal sanity checking.
1645 */
Marc Zyngier27140212008-08-18 13:08:42 +02001646 ires = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1647 if (dev->num_resources < 3 || !ires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return -ENODEV;
1649
Marc Zyngier27140212008-08-18 13:08:42 +02001650 irq = ires->start;
1651 irqflags = ires->flags & IRQF_TRIGGER_MASK;
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* refuse to confuse usbcore */
Russell King3ae5eae2005-11-09 22:32:44 +00001654 if (dev->dev.dma_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 DBG("no we won't dma\n");
1656 return -EINVAL;
1657 }
1658
David Brownell1e9a47b2005-05-26 05:55:55 -07001659 /* the chip may be wired for either kind of addressing */
Russell King3ae5eae2005-11-09 22:32:44 +00001660 addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
1661 data = platform_get_resource(dev, IORESOURCE_MEM, 1);
David Brownell1e9a47b2005-05-26 05:55:55 -07001662 retval = -EBUSY;
1663 if (!addr || !data) {
Russell King3ae5eae2005-11-09 22:32:44 +00001664 addr = platform_get_resource(dev, IORESOURCE_IO, 0);
1665 data = platform_get_resource(dev, IORESOURCE_IO, 1);
David Brownell1e9a47b2005-05-26 05:55:55 -07001666 if (!addr || !data)
1667 return -ENODEV;
1668 ioaddr = 1;
Greg Kroah-Hartman2427ddd2006-06-12 17:07:52 -07001669 /*
1670 * NOTE: 64-bit resource->start is getting truncated
1671 * to avoid compiler warning, assuming that ->start
1672 * is always 32-bit for this case
1673 */
1674 addr_reg = (void __iomem *) (unsigned long) addr->start;
1675 data_reg = (void __iomem *) (unsigned long) data->start;
David Brownell1e9a47b2005-05-26 05:55:55 -07001676 } else {
1677 addr_reg = ioremap(addr->start, 1);
1678 if (addr_reg == NULL) {
1679 retval = -ENOMEM;
1680 goto err2;
1681 }
1682
1683 data_reg = ioremap(data->start, 1);
1684 if (data_reg == NULL) {
1685 retval = -ENOMEM;
1686 goto err4;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
1690 /* allocate and initialize hcd */
Kay Sievers7071a3c2008-05-02 06:02:41 +02001691 hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (!hcd) {
1693 retval = -ENOMEM;
1694 goto err5;
1695 }
1696 hcd->rsrc_start = addr->start;
1697 sl811 = hcd_to_sl811(hcd);
1698
1699 spin_lock_init(&sl811->lock);
1700 INIT_LIST_HEAD(&sl811->async);
Russell King3ae5eae2005-11-09 22:32:44 +00001701 sl811->board = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 init_timer(&sl811->timer);
1703 sl811->timer.function = sl811h_timer;
1704 sl811->timer.data = (unsigned long) sl811;
1705 sl811->addr_reg = addr_reg;
1706 sl811->data_reg = data_reg;
1707
1708 spin_lock_irq(&sl811->lock);
1709 port_power(sl811, 0);
1710 spin_unlock_irq(&sl811->lock);
1711 msleep(200);
1712
1713 tmp = sl811_read(sl811, SL11H_HWREVREG);
1714 switch (tmp >> 4) {
1715 case 1:
1716 hcd->product_desc = "SL811HS v1.2";
1717 break;
1718 case 2:
1719 hcd->product_desc = "SL811HS v1.5";
1720 break;
1721 default:
1722 /* reject case 0, SL11S is less functional */
1723 DBG("chiprev %02x\n", tmp);
1724 retval = -ENXIO;
1725 goto err6;
1726 }
1727
David Brownell1e9a47b2005-05-26 05:55:55 -07001728 /* The chip's IRQ is level triggered, active high. A requirement
1729 * for platform device setup is to cope with things like signal
1730 * inverters (e.g. CF is active low) or working only with edge
1731 * triggers (e.g. most ARM CPUs). Initial driver stress testing
1732 * was on a system with single edge triggering, so most sorts of
1733 * triggering arrangement should work.
Marc Zyngier27140212008-08-18 13:08:42 +02001734 *
1735 * Use resource IRQ flags if set by platform device setup.
David Brownell1e9a47b2005-05-26 05:55:55 -07001736 */
Marc Zyngier27140212008-08-18 13:08:42 +02001737 irqflags |= IRQF_SHARED;
1738 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (retval != 0)
1740 goto err6;
1741
1742 create_debug_file(sl811);
1743 return retval;
1744
1745 err6:
1746 usb_put_hcd(hcd);
1747 err5:
David Brownell1e9a47b2005-05-26 05:55:55 -07001748 if (!ioaddr)
1749 iounmap(data_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 err4:
David Brownell1e9a47b2005-05-26 05:55:55 -07001751 if (!ioaddr)
1752 iounmap(addr_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 err2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 DBG("init error, %d\n", retval);
1755 return retval;
1756}
1757
1758#ifdef CONFIG_PM
1759
1760/* for this device there's no useful distinction between the controller
David Brownell1e9a47b2005-05-26 05:55:55 -07001761 * and its root hub, except that the root hub only gets direct PM calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 * when CONFIG_USB_SUSPEND is enabled.
1763 */
1764
1765static int
Russell King3ae5eae2005-11-09 22:32:44 +00001766sl811h_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Russell King3ae5eae2005-11-09 22:32:44 +00001768 struct usb_hcd *hcd = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 struct sl811 *sl811 = hcd_to_sl811(hcd);
1770 int retval = 0;
1771
David Brownell18584992006-08-14 23:11:06 -07001772 switch (state.event) {
1773 case PM_EVENT_FREEZE:
Alan Stern0c0382e2005-10-13 17:08:02 -04001774 retval = sl811h_bus_suspend(hcd);
David Brownell18584992006-08-14 23:11:06 -07001775 break;
1776 case PM_EVENT_SUSPEND:
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01001777 case PM_EVENT_HIBERNATE:
David Brownell18584992006-08-14 23:11:06 -07001778 case PM_EVENT_PRETHAW: /* explicitly discard hw state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 port_power(sl811, 0);
David Brownell18584992006-08-14 23:11:06 -07001780 break;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return retval;
1783}
1784
1785static int
Russell King3ae5eae2005-11-09 22:32:44 +00001786sl811h_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Russell King3ae5eae2005-11-09 22:32:44 +00001788 struct usb_hcd *hcd = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct sl811 *sl811 = hcd_to_sl811(hcd);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /* with no "check to see if VBUS is still powered" board hook,
1792 * let's assume it'd only be powered to enable remote wakeup.
1793 */
Alan Stern70a1c9e2008-03-06 17:00:58 -05001794 if (!sl811->port1 || !device_can_wakeup(&hcd->self.root_hub->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sl811->port1 = 0;
1796 port_power(sl811, 1);
Alan Stern1c50c312005-11-14 11:45:38 -05001797 usb_root_hub_lost_power(hcd->self.root_hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799 }
1800
Alan Stern0c0382e2005-10-13 17:08:02 -04001801 return sl811h_bus_resume(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
1804#else
1805
1806#define sl811h_suspend NULL
1807#define sl811h_resume NULL
1808
1809#endif
1810
1811
David Brownell1e9a47b2005-05-26 05:55:55 -07001812/* this driver is exported so sl811_cs can depend on it */
Russell King3ae5eae2005-11-09 22:32:44 +00001813struct platform_driver sl811h_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 .probe = sl811h_probe,
David Brownell1e9a47b2005-05-26 05:55:55 -07001815 .remove = __devexit_p(sl811h_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 .suspend = sl811h_suspend,
1818 .resume = sl811h_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001819 .driver = {
1820 .name = (char *) hcd_name,
1821 .owner = THIS_MODULE,
1822 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823};
David Brownell1e9a47b2005-05-26 05:55:55 -07001824EXPORT_SYMBOL(sl811h_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826/*-------------------------------------------------------------------------*/
David Brownell1e9a47b2005-05-26 05:55:55 -07001827
1828static int __init sl811h_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 if (usb_disabled())
1831 return -ENODEV;
1832
1833 INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
Russell King3ae5eae2005-11-09 22:32:44 +00001834 return platform_driver_register(&sl811h_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836module_init(sl811h_init);
1837
David Brownell1e9a47b2005-05-26 05:55:55 -07001838static void __exit sl811h_cleanup(void)
1839{
Russell King3ae5eae2005-11-09 22:32:44 +00001840 platform_driver_unregister(&sl811h_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842module_exit(sl811h_cleanup);