blob: c11c5d8f94e0f871cdc7d87c769eeb1ec7b6e88f [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:
Alan Stern98f541c2013-05-01 12:13:54 -040025 * - usb suspend/resume triggered by sl811 (with PM_RUNTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * - 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
Michael Hennerich8ca5bfab2009-12-21 12:53:24 -050055#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sl811.h"
58
59
60MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
61MODULE_LICENSE("GPL");
Kay Sieversf4fce612008-04-10 21:29:22 -070062MODULE_ALIAS("platform:sl811-hcd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David Brownell1e9a47b2005-05-26 05:55:55 -070064#define DRIVER_VERSION "19 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66
67#ifndef DEBUG
68# define STUB_DEBUG_FILE
69#endif
70
71/* for now, use only one transfer register bank */
72#undef USE_B
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074// #define QUIRK2
75#define QUIRK3
76
77static const char hcd_name[] = "sl811-hcd";
78
79/*-------------------------------------------------------------------------*/
80
81static void port_power(struct sl811 *sl811, int is_on)
82{
83 struct usb_hcd *hcd = sl811_to_hcd(sl811);
84
85 /* hub is inactive unless the port is powered */
86 if (is_on) {
Alan Stern749da5f2010-03-04 17:05:08 -050087 if (sl811->port1 & USB_PORT_STAT_POWER)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return;
89
Alan Stern749da5f2010-03-04 17:05:08 -050090 sl811->port1 = USB_PORT_STAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 sl811->irq_enable = SL11H_INTMASK_INSRMV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 } else {
93 sl811->port1 = 0;
94 sl811->irq_enable = 0;
95 hcd->state = HC_STATE_HALT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97 sl811->ctrl1 = 0;
98 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
99 sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
100
101 if (sl811->board && sl811->board->port_power) {
102 /* switch VBUS, at 500mA unless hub power budget gets set */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700103 dev_dbg(hcd->self.controller, "power %s\n",
104 is_on ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 sl811->board->port_power(hcd->self.controller, is_on);
106 }
107
108 /* reset as thoroughly as we can */
109 if (sl811->board && sl811->board->reset)
110 sl811->board->reset(hcd->self.controller);
David Brownell1e9a47b2005-05-26 05:55:55 -0700111 else {
112 sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
113 mdelay(20);
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
117 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
118 sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
119 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
120
121 // if !is_on, put into lowpower mode now
122}
123
124/*-------------------------------------------------------------------------*/
125
126/* This is a PIO-only HCD. Queueing appends URBs to the endpoint's queue,
127 * and may start I/O. Endpoint queues are scanned during completion irq
Steven Cole093cf722005-05-03 19:07:24 -0600128 * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * Using an external DMA engine to copy a packet at a time could work,
131 * though setup/teardown costs may be too big to make it worthwhile.
132 */
133
134/* SETUP starts a new control request. Devices are not allowed to
135 * STALL or NAK these; they must cancel any pending control requests.
136 */
137static void setup_packet(
138 struct sl811 *sl811,
139 struct sl811h_ep *ep,
140 struct urb *urb,
141 u8 bank,
142 u8 control
143)
144{
145 u8 addr;
146 u8 len;
147 void __iomem *data_reg;
148
149 addr = SL811HS_PACKET_BUF(bank == 0);
150 len = sizeof(struct usb_ctrlrequest);
151 data_reg = sl811->data_reg;
152 sl811_write_buf(sl811, addr, urb->setup_packet, len);
153
154 /* autoincrementing */
155 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
156 writeb(len, data_reg);
157 writeb(SL_SETUP /* | ep->epnum */, data_reg);
158 writeb(usb_pipedevice(urb->pipe), data_reg);
159
Peter Senna Tschudin2edd9682012-09-12 19:03:22 +0200160 /* always OUT/data0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
162 control | SL11H_HCTLMASK_OUT);
163 ep->length = 0;
164 PACKET("SETUP qh%p\n", ep);
165}
166
167/* STATUS finishes control requests, often after IN or OUT data packets */
168static void status_packet(
169 struct sl811 *sl811,
170 struct sl811h_ep *ep,
171 struct urb *urb,
172 u8 bank,
173 u8 control
174)
175{
176 int do_out;
177 void __iomem *data_reg;
178
179 do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
180 data_reg = sl811->data_reg;
181
182 /* autoincrementing */
183 sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
184 writeb(0, data_reg);
185 writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
186 writeb(usb_pipedevice(urb->pipe), data_reg);
187
188 /* always data1; sometimes IN */
189 control |= SL11H_HCTLMASK_TOGGLE;
190 if (do_out)
191 control |= SL11H_HCTLMASK_OUT;
192 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
193 ep->length = 0;
194 PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
195 do_out ? "out" : "in", ep);
196}
197
198/* IN packets can be used with any type of endpoint. here we just
199 * start the transfer, data from the peripheral may arrive later.
200 * urb->iso_frame_desc is currently ignored here...
201 */
202static void in_packet(
203 struct sl811 *sl811,
204 struct sl811h_ep *ep,
205 struct urb *urb,
206 u8 bank,
207 u8 control
208)
209{
210 u8 addr;
211 u8 len;
212 void __iomem *data_reg;
213
214 /* avoid losing data on overflow */
215 len = ep->maxpacket;
216 addr = SL811HS_PACKET_BUF(bank == 0);
217 if (!(control & SL11H_HCTLMASK_ISOCH)
218 && usb_gettoggle(urb->dev, ep->epnum, 0))
219 control |= SL11H_HCTLMASK_TOGGLE;
220 data_reg = sl811->data_reg;
221
222 /* autoincrementing */
223 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
224 writeb(len, data_reg);
225 writeb(SL_IN | ep->epnum, data_reg);
226 writeb(usb_pipedevice(urb->pipe), data_reg);
227
228 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -0800229 ep->length = min_t(u32, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 urb->transfer_buffer_length - urb->actual_length);
231 PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
232 !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
233}
234
235/* OUT packets can be used with any type of endpoint.
236 * urb->iso_frame_desc is currently ignored here...
237 */
238static void out_packet(
239 struct sl811 *sl811,
240 struct sl811h_ep *ep,
241 struct urb *urb,
242 u8 bank,
243 u8 control
244)
245{
246 void *buf;
247 u8 addr;
248 u8 len;
249 void __iomem *data_reg;
250
251 buf = urb->transfer_buffer + urb->actual_length;
252 prefetch(buf);
253
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -0800254 len = min_t(u32, ep->maxpacket,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 urb->transfer_buffer_length - urb->actual_length);
256
257 if (!(control & SL11H_HCTLMASK_ISOCH)
258 && usb_gettoggle(urb->dev, ep->epnum, 1))
259 control |= SL11H_HCTLMASK_TOGGLE;
260 addr = SL811HS_PACKET_BUF(bank == 0);
261 data_reg = sl811->data_reg;
262
263 sl811_write_buf(sl811, addr, buf, len);
264
265 /* autoincrementing */
266 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
267 writeb(len, data_reg);
268 writeb(SL_OUT | ep->epnum, data_reg);
269 writeb(usb_pipedevice(urb->pipe), data_reg);
270
271 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
272 control | SL11H_HCTLMASK_OUT);
273 ep->length = len;
274 PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
275 !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
276}
277
278/*-------------------------------------------------------------------------*/
279
280/* caller updates on-chip enables later */
281
282static inline void sofirq_on(struct sl811 *sl811)
283{
284 if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
285 return;
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700286 dev_dbg(sl811_to_hcd(sl811)->self.controller, "sof irq on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
288}
289
290static inline void sofirq_off(struct sl811 *sl811)
291{
292 if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
293 return;
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700294 dev_dbg(sl811_to_hcd(sl811)->self.controller, "sof irq off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
296}
297
298/*-------------------------------------------------------------------------*/
299
300/* pick the next endpoint for a transaction, and issue it.
301 * frames start with periodic transfers (after whatever is pending
302 * from the previous frame), and the rest of the time is async
303 * transfers, scheduled round-robin.
304 */
305static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
306{
307 struct sl811h_ep *ep;
308 struct urb *urb;
309 int fclock;
310 u8 control;
311
312 /* use endpoint at schedule head */
313 if (sl811->next_periodic) {
314 ep = sl811->next_periodic;
315 sl811->next_periodic = ep->next;
316 } else {
317 if (sl811->next_async)
318 ep = sl811->next_async;
319 else if (!list_empty(&sl811->async))
320 ep = container_of(sl811->async.next,
321 struct sl811h_ep, schedule);
322 else {
323 /* could set up the first fullspeed periodic
324 * transfer for the next frame ...
325 */
326 return NULL;
327 }
328
329#ifdef USE_B
330 if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
331 return NULL;
332#endif
333
334 if (ep->schedule.next == &sl811->async)
335 sl811->next_async = NULL;
336 else
337 sl811->next_async = container_of(ep->schedule.next,
338 struct sl811h_ep, schedule);
339 }
340
341 if (unlikely(list_empty(&ep->hep->urb_list))) {
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700342 dev_dbg(sl811_to_hcd(sl811)->self.controller,
343 "empty %p queue?\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return NULL;
345 }
346
347 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
348 control = ep->defctrl;
349
350 /* if this frame doesn't have enough time left to transfer this
351 * packet, wait till the next frame. too-simple algorithm...
352 */
353 fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
354 fclock -= 100; /* setup takes not much time */
355 if (urb->dev->speed == USB_SPEED_LOW) {
356 if (control & SL11H_HCTLMASK_PREAMBLE) {
357 /* also note erratum 1: some hubs won't work */
358 fclock -= 800;
359 }
360 fclock -= ep->maxpacket << 8;
361
362 /* erratum 2: AFTERSOF only works for fullspeed */
363 if (fclock < 0) {
364 if (ep->period)
365 sl811->stat_overrun++;
366 sofirq_on(sl811);
367 return NULL;
368 }
369 } else {
370 fclock -= 12000 / 19; /* 19 64byte packets/msec */
371 if (fclock < 0) {
372 if (ep->period)
373 sl811->stat_overrun++;
374 control |= SL11H_HCTLMASK_AFTERSOF;
375
376 /* throttle bulk/control irq noise */
377 } else if (ep->nak_count)
378 control |= SL11H_HCTLMASK_AFTERSOF;
379 }
380
381
382 switch (ep->nextpid) {
383 case USB_PID_IN:
384 in_packet(sl811, ep, urb, bank, control);
385 break;
386 case USB_PID_OUT:
387 out_packet(sl811, ep, urb, bank, control);
388 break;
389 case USB_PID_SETUP:
390 setup_packet(sl811, ep, urb, bank, control);
391 break;
392 case USB_PID_ACK: /* for control status */
393 status_packet(sl811, ep, urb, bank, control);
394 break;
395 default:
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700396 dev_dbg(sl811_to_hcd(sl811)->self.controller,
397 "bad ep%p pid %02x\n", ep, ep->nextpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ep = NULL;
399 }
400 return ep;
401}
402
403#define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
404
405static inline void start_transfer(struct sl811 *sl811)
406{
Alan Stern749da5f2010-03-04 17:05:08 -0500407 if (sl811->port1 & USB_PORT_STAT_SUSPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return;
409 if (sl811->active_a == NULL) {
410 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
411 if (sl811->active_a != NULL)
412 sl811->jiffies_a = jiffies + MIN_JIFFIES;
413 }
414#ifdef USE_B
415 if (sl811->active_b == NULL) {
416 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
417 if (sl811->active_b != NULL)
418 sl811->jiffies_b = jiffies + MIN_JIFFIES;
419 }
420#endif
421}
422
423static void finish_request(
424 struct sl811 *sl811,
425 struct sl811h_ep *ep,
426 struct urb *urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int status
428) __releases(sl811->lock) __acquires(sl811->lock)
429{
430 unsigned i;
431
432 if (usb_pipecontrol(urb->pipe))
433 ep->nextpid = USB_PID_SETUP;
434
Alan Sterne9df41c2007-08-08 11:48:02 -0400435 usb_hcd_unlink_urb_from_ep(sl811_to_hcd(sl811), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 spin_unlock(&sl811->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400437 usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_lock(&sl811->lock);
439
440 /* leave active endpoints in the schedule */
441 if (!list_empty(&ep->hep->urb_list))
442 return;
443
444 /* async deschedule? */
445 if (!list_empty(&ep->schedule)) {
446 list_del_init(&ep->schedule);
447 if (ep == sl811->next_async)
448 sl811->next_async = NULL;
449 return;
450 }
451
452 /* periodic deschedule */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700453 dev_dbg(sl811_to_hcd(sl811)->self.controller,
454 "deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
456 struct sl811h_ep *temp;
457 struct sl811h_ep **prev = &sl811->periodic[i];
458
459 while (*prev && ((temp = *prev) != ep))
460 prev = &temp->next;
461 if (*prev)
462 *prev = ep->next;
463 sl811->load[i] -= ep->load;
David Brownell1e9a47b2005-05-26 05:55:55 -0700464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ep->branch = PERIODIC_SIZE;
466 sl811->periodic_count--;
467 sl811_to_hcd(sl811)->self.bandwidth_allocated
468 -= ep->load / ep->period;
469 if (ep == sl811->next_periodic)
470 sl811->next_periodic = ep->next;
471
472 /* we might turn SOFs back on again for the async schedule */
473 if (sl811->periodic_count == 0)
474 sofirq_off(sl811);
475}
476
477static void
David Howells7d12e782006-10-05 14:55:46 +0100478done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 u8 status;
481 struct urb *urb;
482 int urbstat = -EINPROGRESS;
483
484 if (unlikely(!ep))
485 return;
486
487 status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
488
489 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
490
491 /* we can safely ignore NAKs */
492 if (status & SL11H_STATMASK_NAK) {
493 // PACKET("...NAK_%02x qh%p\n", bank, ep);
494 if (!ep->period)
495 ep->nak_count++;
496 ep->error_count = 0;
497
498 /* ACK advances transfer, toggle, and maybe queue */
499 } else if (status & SL11H_STATMASK_ACK) {
500 struct usb_device *udev = urb->dev;
501 int len;
502 unsigned char *buf;
503
504 /* urb->iso_frame_desc is currently ignored here... */
505
506 ep->nak_count = ep->error_count = 0;
507 switch (ep->nextpid) {
508 case USB_PID_OUT:
509 // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
510 urb->actual_length += ep->length;
511 usb_dotoggle(udev, ep->epnum, 1);
512 if (urb->actual_length
513 == urb->transfer_buffer_length) {
514 if (usb_pipecontrol(urb->pipe))
515 ep->nextpid = USB_PID_ACK;
516
517 /* some bulk protocols terminate OUT transfers
518 * by a short packet, using ZLPs not padding.
519 */
520 else if (ep->length < ep->maxpacket
521 || !(urb->transfer_flags
522 & URB_ZERO_PACKET))
523 urbstat = 0;
524 }
525 break;
526 case USB_PID_IN:
527 // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
528 buf = urb->transfer_buffer + urb->actual_length;
529 prefetchw(buf);
530 len = ep->maxpacket - sl811_read(sl811,
531 bank + SL11H_XFERCNTREG);
532 if (len > ep->length) {
533 len = ep->length;
Alan Stern65e51092007-08-24 15:40:47 -0400534 urbstat = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 urb->actual_length += len;
537 sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
538 buf, len);
539 usb_dotoggle(udev, ep->epnum, 0);
Alan Stern65e51092007-08-24 15:40:47 -0400540 if (urbstat == -EINPROGRESS &&
541 (len < ep->maxpacket ||
542 urb->actual_length ==
543 urb->transfer_buffer_length)) {
544 if (usb_pipecontrol(urb->pipe))
545 ep->nextpid = USB_PID_ACK;
546 else
547 urbstat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 break;
550 case USB_PID_SETUP:
551 // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
552 if (urb->transfer_buffer_length == urb->actual_length)
553 ep->nextpid = USB_PID_ACK;
554 else if (usb_pipeout(urb->pipe)) {
555 usb_settoggle(udev, 0, 1, 1);
556 ep->nextpid = USB_PID_OUT;
557 } else {
558 usb_settoggle(udev, 0, 0, 1);
559 ep->nextpid = USB_PID_IN;
560 }
561 break;
562 case USB_PID_ACK:
563 // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
564 urbstat = 0;
565 break;
566 }
567
568 /* STALL stops all transfers */
569 } else if (status & SL11H_STATMASK_STALL) {
570 PACKET("...STALL_%02x qh%p\n", bank, ep);
571 ep->nak_count = ep->error_count = 0;
572 urbstat = -EPIPE;
573
574 /* error? retry, until "3 strikes" */
575 } else if (++ep->error_count >= 3) {
576 if (status & SL11H_STATMASK_TMOUT)
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700577 urbstat = -ETIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 else if (status & SL11H_STATMASK_OVF)
579 urbstat = -EOVERFLOW;
580 else
581 urbstat = -EPROTO;
582 ep->error_count = 0;
583 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
584 bank, status, ep, urbstat);
585 }
586
Alan Stern65e51092007-08-24 15:40:47 -0400587 if (urbstat != -EINPROGRESS || urb->unlinked)
David Howells7d12e782006-10-05 14:55:46 +0100588 finish_request(sl811, ep, urb, urbstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591static inline u8 checkdone(struct sl811 *sl811)
592{
593 u8 ctl;
594 u8 irqstat = 0;
595
596 if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
597 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
598 if (ctl & SL11H_HCTLMASK_ARM)
599 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700600 dev_dbg(sl811_to_hcd(sl811)->self.controller,
601 "%s DONE_A: ctrl %02x sts %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
603 ctl,
604 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
605 irqstat |= SL11H_INTMASK_DONE_A;
606 }
607#ifdef USE_B
608 if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
609 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
610 if (ctl & SL11H_HCTLMASK_ARM)
611 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700612 dev_dbg(sl811_to_hcd(sl811)->self.controller,
613 "%s DONE_B: ctrl %02x sts %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
615 ctl,
616 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
617 irqstat |= SL11H_INTMASK_DONE_A;
618 }
619#endif
620 return irqstat;
621}
622
David Howells7d12e782006-10-05 14:55:46 +0100623static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct sl811 *sl811 = hcd_to_sl811(hcd);
626 u8 irqstat;
627 irqreturn_t ret = IRQ_NONE;
628 unsigned retries = 5;
629
630 spin_lock(&sl811->lock);
631
632retry:
633 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
634 if (irqstat) {
635 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
636 irqstat &= sl811->irq_enable;
637 }
638
639#ifdef QUIRK2
640 /* this may no longer be necessary ... */
David Brownell1e9a47b2005-05-26 05:55:55 -0700641 if (irqstat == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 irqstat = checkdone(sl811);
David Brownell1e9a47b2005-05-26 05:55:55 -0700643 if (irqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 sl811->stat_lost++;
645 }
646#endif
647
648 /* USB packets, not necessarily handled in the order they're
649 * issued ... that's fine if they're different endpoints.
650 */
651 if (irqstat & SL11H_INTMASK_DONE_A) {
David Howells7d12e782006-10-05 14:55:46 +0100652 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 sl811->active_a = NULL;
654 sl811->stat_a++;
655 }
656#ifdef USE_B
657 if (irqstat & SL11H_INTMASK_DONE_B) {
David Howells7d12e782006-10-05 14:55:46 +0100658 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 sl811->active_b = NULL;
660 sl811->stat_b++;
661 }
662#endif
663 if (irqstat & SL11H_INTMASK_SOFINTR) {
664 unsigned index;
665
666 index = sl811->frame++ % (PERIODIC_SIZE - 1);
667 sl811->stat_sof++;
668
669 /* be graceful about almost-inevitable periodic schedule
670 * overruns: continue the previous frame's transfers iff
671 * this one has nothing scheduled.
672 */
673 if (sl811->next_periodic) {
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700674 // dev_err(hcd->self.controller, "overrun to slot %d\n", index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 sl811->stat_overrun++;
676 }
677 if (sl811->periodic[index])
678 sl811->next_periodic = sl811->periodic[index];
679 }
680
681 /* khubd manages debouncing and wakeup */
682 if (irqstat & SL11H_INTMASK_INSRMV) {
683 sl811->stat_insrmv++;
684
685 /* most stats are reset for each VBUS session */
686 sl811->stat_wake = 0;
687 sl811->stat_sof = 0;
688 sl811->stat_a = 0;
689 sl811->stat_b = 0;
690 sl811->stat_lost = 0;
691
692 sl811->ctrl1 = 0;
693 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
694
695 sl811->irq_enable = SL11H_INTMASK_INSRMV;
696 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
697
698 /* usbcore nukes other pending transactions on disconnect */
699 if (sl811->active_a) {
700 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
701 finish_request(sl811, sl811->active_a,
David Brownell1e9a47b2005-05-26 05:55:55 -0700702 container_of(sl811->active_a
703 ->hep->urb_list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct urb, urb_list),
David Howells7d12e782006-10-05 14:55:46 +0100705 -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sl811->active_a = NULL;
707 }
708#ifdef USE_B
709 if (sl811->active_b) {
710 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
711 finish_request(sl811, sl811->active_b,
David Brownell1e9a47b2005-05-26 05:55:55 -0700712 container_of(sl811->active_b
713 ->hep->urb_list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct urb, urb_list),
715 NULL, -ESHUTDOWN);
716 sl811->active_b = NULL;
717 }
718#endif
719
Steven Cole093cf722005-05-03 19:07:24 -0600720 /* port status seems weird until after reset, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * force the reset and make khubd clean up later.
722 */
Michael Hennerich8a3461e2010-04-28 17:31:36 -0400723 if (irqstat & SL11H_INTMASK_RD)
Alan Stern749da5f2010-03-04 17:05:08 -0500724 sl811->port1 &= ~USB_PORT_STAT_CONNECTION;
Michael Hennerich8a3461e2010-04-28 17:31:36 -0400725 else
Alan Stern749da5f2010-03-04 17:05:08 -0500726 sl811->port1 |= USB_PORT_STAT_CONNECTION;
Hennerich, Michaeleb661bc2009-09-02 09:26:21 +0100727
Alan Stern749da5f2010-03-04 17:05:08 -0500728 sl811->port1 |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 } else if (irqstat & SL11H_INTMASK_RD) {
Alan Stern749da5f2010-03-04 17:05:08 -0500731 if (sl811->port1 & USB_PORT_STAT_SUSPEND) {
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700732 dev_dbg(hcd->self.controller, "wakeup\n");
Alan Stern749da5f2010-03-04 17:05:08 -0500733 sl811->port1 |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 sl811->stat_wake++;
735 } else
736 irqstat &= ~SL11H_INTMASK_RD;
737 }
738
739 if (irqstat) {
Alan Stern749da5f2010-03-04 17:05:08 -0500740 if (sl811->port1 & USB_PORT_STAT_ENABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 start_transfer(sl811);
742 ret = IRQ_HANDLED;
743 if (retries--)
744 goto retry;
745 }
746
David Brownell1e9a47b2005-05-26 05:55:55 -0700747 if (sl811->periodic_count == 0 && list_empty(&sl811->async))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 sofirq_off(sl811);
749 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
750
751 spin_unlock(&sl811->lock);
752
753 return ret;
754}
755
756/*-------------------------------------------------------------------------*/
757
758/* usb 1.1 says max 90% of a frame is available for periodic transfers.
759 * this driver doesn't promise that much since it's got to handle an
760 * IRQ per packet; irq handling latencies also use up that time.
David Brownell4b2e7902005-09-22 00:49:07 -0700761 *
762 * NOTE: the periodic schedule is a sparse tree, with the load for
763 * each branch minimized. see fig 3.5 in the OHCI spec for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
765#define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */
766
767static int balance(struct sl811 *sl811, u16 period, u16 load)
768{
769 int i, branch = -ENOSPC;
770
771 /* search for the least loaded schedule branch of that period
772 * which has enough bandwidth left unreserved.
773 */
774 for (i = 0; i < period ; i++) {
775 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
776 int j;
777
778 for (j = i; j < PERIODIC_SIZE; j += period) {
779 if ((sl811->load[j] + load)
780 > MAX_PERIODIC_LOAD)
781 break;
782 }
783 if (j < PERIODIC_SIZE)
784 continue;
David Brownell1e9a47b2005-05-26 05:55:55 -0700785 branch = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 }
788 return branch;
789}
790
791/*-------------------------------------------------------------------------*/
792
793static int sl811h_urb_enqueue(
794 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -0400796 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797) {
798 struct sl811 *sl811 = hcd_to_sl811(hcd);
799 struct usb_device *udev = urb->dev;
800 unsigned int pipe = urb->pipe;
801 int is_out = !usb_pipein(pipe);
802 int type = usb_pipetype(pipe);
803 int epnum = usb_pipeendpoint(pipe);
804 struct sl811h_ep *ep = NULL;
805 unsigned long flags;
806 int i;
Alan Sterne9df41c2007-08-08 11:48:02 -0400807 int retval;
808 struct usb_host_endpoint *hep = urb->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Mike Frysingerad7c56f2011-03-22 15:35:39 -0400810#ifndef CONFIG_USB_SL811_HCD_ISO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (type == PIPE_ISOCHRONOUS)
812 return -ENOSPC;
813#endif
814
815 /* avoid all allocations within spinlocks */
Kulikov Vasiliy76be9322010-07-16 20:15:06 +0400816 if (!hep->hcpriv) {
Pekka Enberg7b842b62005-09-06 15:18:34 -0700817 ep = kzalloc(sizeof *ep, mem_flags);
Kulikov Vasiliy76be9322010-07-16 20:15:06 +0400818 if (ep == NULL)
819 return -ENOMEM;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 spin_lock_irqsave(&sl811->lock, flags);
823
824 /* don't submit to a dead or disabled port */
Alan Stern749da5f2010-03-04 17:05:08 -0500825 if (!(sl811->port1 & USB_PORT_STAT_ENABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 || !HC_IS_RUNNING(hcd->state)) {
827 retval = -ENODEV;
David Brownell4b2e7902005-09-22 00:49:07 -0700828 kfree(ep);
Alan Sterne9df41c2007-08-08 11:48:02 -0400829 goto fail_not_linked;
830 }
831 retval = usb_hcd_link_urb_to_ep(hcd, urb);
832 if (retval) {
833 kfree(ep);
834 goto fail_not_linked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 if (hep->hcpriv) {
838 kfree(ep);
839 ep = hep->hcpriv;
840 } else if (!ep) {
841 retval = -ENOMEM;
842 goto fail;
843
844 } else {
845 INIT_LIST_HEAD(&ep->schedule);
Alan Stern6a8e87b2006-01-19 10:46:27 -0500846 ep->udev = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ep->epnum = epnum;
848 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
849 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
850 usb_settoggle(udev, epnum, is_out, 0);
851
852 if (type == PIPE_CONTROL)
853 ep->nextpid = USB_PID_SETUP;
854 else if (is_out)
855 ep->nextpid = USB_PID_OUT;
856 else
857 ep->nextpid = USB_PID_IN;
858
859 if (ep->maxpacket > H_MAXPACKET) {
860 /* iso packets up to 240 bytes could work... */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700861 dev_dbg(hcd->self.controller,
862 "dev %d ep%d maxpacket %d\n", udev->devnum,
863 epnum, ep->maxpacket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 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 */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700927 dev_dbg(hcd->self.controller, "schedule qh%d/%p branch %d\n",
928 ep->period, ep, ep->branch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
930 struct sl811h_ep **prev = &sl811->periodic[i];
931 struct sl811h_ep *here = *prev;
932
933 while (here && ep != here) {
934 if (ep->period > here->period)
935 break;
936 prev = &here->next;
937 here = *prev;
938 }
939 if (ep != here) {
940 ep->next = here;
941 *prev = ep;
942 }
943 sl811->load[i] += ep->load;
944 }
945 sl811->periodic_count++;
946 hcd->self.bandwidth_allocated += ep->load / ep->period;
947 sofirq_on(sl811);
948 }
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 urb->hcpriv = hep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 start_transfer(sl811);
952 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
953fail:
Alan Sterne9df41c2007-08-08 11:48:02 -0400954 if (retval)
955 usb_hcd_unlink_urb_from_ep(hcd, urb);
956fail_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 spin_unlock_irqrestore(&sl811->lock, flags);
958 return retval;
959}
960
Alan Sterne9df41c2007-08-08 11:48:02 -0400961static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 struct sl811 *sl811 = hcd_to_sl811(hcd);
David Brownell1e9a47b2005-05-26 05:55:55 -0700964 struct usb_host_endpoint *hep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 unsigned long flags;
966 struct sl811h_ep *ep;
Alan Sterne9df41c2007-08-08 11:48:02 -0400967 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 spin_lock_irqsave(&sl811->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400970 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
971 if (retval)
David Brownell1e9a47b2005-05-26 05:55:55 -0700972 goto fail;
973
Alan Sterne9df41c2007-08-08 11:48:02 -0400974 hep = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ep = hep->hcpriv;
976 if (ep) {
977 /* finish right away if this urb can't be active ...
978 * note that some drivers wrongly expect delays
979 */
980 if (ep->hep->urb_list.next != &urb->urb_list) {
981 /* not front of queue? never active */
982
983 /* for active transfers, we expect an IRQ */
984 } else if (sl811->active_a == ep) {
985 if (time_before_eq(sl811->jiffies_a, jiffies)) {
986 /* happens a lot with lowspeed?? */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -0700987 dev_dbg(hcd->self.controller,
988 "giveup on DONE_A: ctrl %02x sts %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 sl811_read(sl811,
990 SL811_EP_A(SL11H_HOSTCTLREG)),
991 sl811_read(sl811,
992 SL811_EP_A(SL11H_PKTSTATREG)));
993 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
994 0);
995 sl811->active_a = NULL;
996 } else
997 urb = NULL;
998#ifdef USE_B
999 } else if (sl811->active_b == ep) {
1000 if (time_before_eq(sl811->jiffies_a, jiffies)) {
1001 /* happens a lot with lowspeed?? */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001002 dev_dbg(hcd->self.controller,
1003 "giveup on DONE_B: ctrl %02x sts %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sl811_read(sl811,
1005 SL811_EP_B(SL11H_HOSTCTLREG)),
1006 sl811_read(sl811,
1007 SL811_EP_B(SL11H_PKTSTATREG)));
1008 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1009 0);
1010 sl811->active_b = NULL;
1011 } else
1012 urb = NULL;
1013#endif
1014 } else {
1015 /* front of queue for inactive endpoint */
1016 }
1017
1018 if (urb)
David Howells7d12e782006-10-05 14:55:46 +01001019 finish_request(sl811, ep, urb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 else
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001021 dev_dbg(sl811_to_hcd(sl811)->self.controller,
1022 "dequeue, urb %p active %s; wait4irq\n", urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 (sl811->active_a == ep) ? "A" : "B");
1024 } else
1025 retval = -EINVAL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001026 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 spin_unlock_irqrestore(&sl811->lock, flags);
1028 return retval;
1029}
1030
1031static void
1032sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1033{
1034 struct sl811h_ep *ep = hep->hcpriv;
1035
1036 if (!ep)
1037 return;
1038
1039 /* assume we'd just wait for the irq */
1040 if (!list_empty(&hep->urb_list))
1041 msleep(3);
1042 if (!list_empty(&hep->urb_list))
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001043 dev_warn(hcd->self.controller, "ep %p not empty?\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 kfree(ep);
1046 hep->hcpriv = NULL;
1047}
1048
1049static int
1050sl811h_get_frame(struct usb_hcd *hcd)
1051{
1052 struct sl811 *sl811 = hcd_to_sl811(hcd);
1053
1054 /* wrong except while periodic transfers are scheduled;
1055 * never matches the on-the-wire frame;
1056 * subject to overruns.
1057 */
1058 return sl811->frame;
1059}
1060
1061
1062/*-------------------------------------------------------------------------*/
1063
1064/* the virtual root hub timer IRQ checks for hub status */
1065static int
1066sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1067{
1068 struct sl811 *sl811 = hcd_to_sl811(hcd);
1069#ifdef QUIRK3
1070 unsigned long flags;
1071
1072 /* non-SMP HACK: use root hub timer as i/o watchdog
1073 * this seems essential when SOF IRQs aren't in use...
1074 */
1075 local_irq_save(flags);
1076 if (!timer_pending(&sl811->timer)) {
David Howells7d12e782006-10-05 14:55:46 +01001077 if (sl811h_irq( /* ~0, */ hcd) != IRQ_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 sl811->stat_lost++;
1079 }
1080 local_irq_restore(flags);
1081#endif
1082
1083 if (!(sl811->port1 & (0xffff << 16)))
1084 return 0;
1085
1086 /* tell khubd port 1 changed */
1087 *buf = (1 << 1);
1088 return 1;
1089}
1090
1091static void
1092sl811h_hub_descriptor (
1093 struct sl811 *sl811,
1094 struct usb_hub_descriptor *desc
1095) {
1096 u16 temp = 0;
1097
1098 desc->bDescriptorType = 0x29;
1099 desc->bHubContrCurrent = 0;
1100
1101 desc->bNbrPorts = 1;
1102 desc->bDescLength = 9;
1103
1104 /* per-port power switching (gang of one!), or none */
1105 desc->bPwrOn2PwrGood = 0;
1106 if (sl811->board && sl811->board->port_power) {
1107 desc->bPwrOn2PwrGood = sl811->board->potpg;
1108 if (!desc->bPwrOn2PwrGood)
1109 desc->bPwrOn2PwrGood = 10;
1110 temp = 0x0001;
1111 } else
1112 temp = 0x0002;
1113
1114 /* no overcurrent errors detection/handling */
1115 temp |= 0x0010;
1116
Al Virofd05e722008-04-28 07:00:16 +01001117 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Sarah Sharpda130512010-11-30 15:55:51 -08001119 /* ports removable, and legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001120 desc->u.hs.DeviceRemovable[0] = 0 << 1;
1121 desc->u.hs.DeviceRemovable[1] = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124static void
1125sl811h_timer(unsigned long _sl811)
1126{
1127 struct sl811 *sl811 = (void *) _sl811;
1128 unsigned long flags;
1129 u8 irqstat;
1130 u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
Alan Stern749da5f2010-03-04 17:05:08 -05001131 const u32 mask = USB_PORT_STAT_CONNECTION
1132 | USB_PORT_STAT_ENABLE
Alan Stern288ead42010-03-04 11:32:30 -05001133 | USB_PORT_STAT_LOW_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 spin_lock_irqsave(&sl811->lock, flags);
1136
1137 /* stop special signaling */
1138 sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1139 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1140 udelay(3);
1141
1142 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1143
1144 switch (signaling) {
1145 case SL11H_CTL1MASK_SE0:
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001146 dev_dbg(sl811_to_hcd(sl811)->self.controller, "end reset\n");
Alan Stern749da5f2010-03-04 17:05:08 -05001147 sl811->port1 = (USB_PORT_STAT_C_RESET << 16)
1148 | USB_PORT_STAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sl811->ctrl1 = 0;
1150 /* don't wrongly ack RD */
1151 if (irqstat & SL11H_INTMASK_INSRMV)
1152 irqstat &= ~SL11H_INTMASK_RD;
1153 break;
1154 case SL11H_CTL1MASK_K:
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001155 dev_dbg(sl811_to_hcd(sl811)->self.controller, "end resume\n");
Alan Stern749da5f2010-03-04 17:05:08 -05001156 sl811->port1 &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 default:
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001159 dev_dbg(sl811_to_hcd(sl811)->self.controller,
1160 "odd timer signaling: %02x\n", signaling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 }
1163 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1164
1165 if (irqstat & SL11H_INTMASK_RD) {
1166 /* usbcore nukes all pending transactions on disconnect */
Alan Stern749da5f2010-03-04 17:05:08 -05001167 if (sl811->port1 & USB_PORT_STAT_CONNECTION)
1168 sl811->port1 |= (USB_PORT_STAT_C_CONNECTION << 16)
1169 | (USB_PORT_STAT_C_ENABLE << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 sl811->port1 &= ~mask;
1171 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1172 } else {
1173 sl811->port1 |= mask;
1174 if (irqstat & SL11H_INTMASK_DP)
Alan Stern288ead42010-03-04 11:32:30 -05001175 sl811->port1 &= ~USB_PORT_STAT_LOW_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1177 }
1178
Alan Stern749da5f2010-03-04 17:05:08 -05001179 if (sl811->port1 & USB_PORT_STAT_CONNECTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 u8 ctrl2 = SL811HS_CTL2_INIT;
1181
1182 sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1183#ifdef USE_B
1184 sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1185#endif
Alan Stern288ead42010-03-04 11:32:30 -05001186 if (sl811->port1 & USB_PORT_STAT_LOW_SPEED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1188 ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1189 }
1190
1191 /* start SOFs flowing, kickstarting with A registers */
1192 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1193 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1194 sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1195
1196 /* autoincrementing */
1197 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1198 writeb(SL_SOF, sl811->data_reg);
1199 writeb(0, sl811->data_reg);
1200 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1201 SL11H_HCTLMASK_ARM);
1202
1203 /* khubd provides debounce delay */
1204 } else {
1205 sl811->ctrl1 = 0;
1206 }
1207 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1208
1209 /* reenable irqs */
1210 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1211 spin_unlock_irqrestore(&sl811->lock, flags);
1212}
1213
1214static int
1215sl811h_hub_control(
1216 struct usb_hcd *hcd,
1217 u16 typeReq,
1218 u16 wValue,
1219 u16 wIndex,
1220 char *buf,
1221 u16 wLength
1222) {
1223 struct sl811 *sl811 = hcd_to_sl811(hcd);
1224 int retval = 0;
1225 unsigned long flags;
1226
1227 spin_lock_irqsave(&sl811->lock, flags);
1228
1229 switch (typeReq) {
1230 case ClearHubFeature:
1231 case SetHubFeature:
1232 switch (wValue) {
1233 case C_HUB_OVER_CURRENT:
1234 case C_HUB_LOCAL_POWER:
1235 break;
1236 default:
1237 goto error;
1238 }
1239 break;
1240 case ClearPortFeature:
1241 if (wIndex != 1 || wLength != 0)
1242 goto error;
1243
1244 switch (wValue) {
1245 case USB_PORT_FEAT_ENABLE:
Alan Stern749da5f2010-03-04 17:05:08 -05001246 sl811->port1 &= USB_PORT_STAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 sl811->ctrl1 = 0;
1248 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1249 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1250 sl811_write(sl811, SL11H_IRQ_ENABLE,
1251 sl811->irq_enable);
1252 break;
1253 case USB_PORT_FEAT_SUSPEND:
Alan Stern749da5f2010-03-04 17:05:08 -05001254 if (!(sl811->port1 & USB_PORT_STAT_SUSPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256
1257 /* 20 msec of resume/K signaling, other irqs blocked */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001258 dev_dbg(hcd->self.controller, "start resume...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 sl811->irq_enable = 0;
1260 sl811_write(sl811, SL11H_IRQ_ENABLE,
1261 sl811->irq_enable);
1262 sl811->ctrl1 |= SL11H_CTL1MASK_K;
1263 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1264
1265 mod_timer(&sl811->timer, jiffies
1266 + msecs_to_jiffies(20));
1267 break;
1268 case USB_PORT_FEAT_POWER:
1269 port_power(sl811, 0);
1270 break;
1271 case USB_PORT_FEAT_C_ENABLE:
1272 case USB_PORT_FEAT_C_SUSPEND:
1273 case USB_PORT_FEAT_C_CONNECTION:
1274 case USB_PORT_FEAT_C_OVER_CURRENT:
1275 case USB_PORT_FEAT_C_RESET:
1276 break;
1277 default:
1278 goto error;
1279 }
1280 sl811->port1 &= ~(1 << wValue);
1281 break;
1282 case GetHubDescriptor:
1283 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1284 break;
1285 case GetHubStatus:
Michael Hennerich8ca5bfab2009-12-21 12:53:24 -05001286 put_unaligned_le32(0, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case GetPortStatus:
1289 if (wIndex != 1)
1290 goto error;
Michael Hennerich8ca5bfab2009-12-21 12:53:24 -05001291 put_unaligned_le32(sl811->port1, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293#ifndef VERBOSE
1294 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
1295#endif
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001296 dev_dbg(hcd->self.controller, "GetPortStatus %08x\n",
1297 sl811->port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
1299 case SetPortFeature:
1300 if (wIndex != 1 || wLength != 0)
1301 goto error;
1302 switch (wValue) {
1303 case USB_PORT_FEAT_SUSPEND:
Alan Stern749da5f2010-03-04 17:05:08 -05001304 if (sl811->port1 & USB_PORT_STAT_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto error;
Alan Stern749da5f2010-03-04 17:05:08 -05001306 if (!(sl811->port1 & USB_PORT_STAT_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto error;
1308
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001309 dev_dbg(hcd->self.controller,"suspend...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1311 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1312 break;
1313 case USB_PORT_FEAT_POWER:
1314 port_power(sl811, 1);
1315 break;
1316 case USB_PORT_FEAT_RESET:
Alan Stern749da5f2010-03-04 17:05:08 -05001317 if (sl811->port1 & USB_PORT_STAT_SUSPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 goto error;
Alan Stern749da5f2010-03-04 17:05:08 -05001319 if (!(sl811->port1 & USB_PORT_STAT_POWER))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321
1322 /* 50 msec of reset/SE0 signaling, irqs blocked */
1323 sl811->irq_enable = 0;
1324 sl811_write(sl811, SL11H_IRQ_ENABLE,
1325 sl811->irq_enable);
1326 sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1327 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
Alan Stern749da5f2010-03-04 17:05:08 -05001328 sl811->port1 |= USB_PORT_STAT_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 mod_timer(&sl811->timer, jiffies
1330 + msecs_to_jiffies(50));
1331 break;
1332 default:
1333 goto error;
1334 }
1335 sl811->port1 |= 1 << wValue;
1336 break;
1337
1338 default:
1339error:
1340 /* "protocol stall" on error */
1341 retval = -EPIPE;
1342 }
1343
1344 spin_unlock_irqrestore(&sl811->lock, flags);
1345 return retval;
1346}
1347
1348#ifdef CONFIG_PM
1349
1350static int
Alan Stern0c0382e2005-10-13 17:08:02 -04001351sl811h_bus_suspend(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 // SOFs off
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001354 dev_dbg(hcd->self.controller, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 0;
1356}
1357
1358static int
Alan Stern0c0382e2005-10-13 17:08:02 -04001359sl811h_bus_resume(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 // SOFs on
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001362 dev_dbg(hcd->self.controller, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364}
1365
1366#else
1367
Alan Stern0c0382e2005-10-13 17:08:02 -04001368#define sl811h_bus_suspend NULL
1369#define sl811h_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371#endif
1372
1373
1374/*-------------------------------------------------------------------------*/
1375
1376#ifdef STUB_DEBUG_FILE
1377
1378static inline void create_debug_file(struct sl811 *sl811) { }
1379static inline void remove_debug_file(struct sl811 *sl811) { }
1380
1381#else
1382
1383#include <linux/proc_fs.h>
1384#include <linux/seq_file.h>
1385
1386static void dump_irq(struct seq_file *s, char *label, u8 mask)
1387{
1388 seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1389 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1390 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1391 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1392 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1393 (mask & SL11H_INTMASK_RD) ? " rd" : "",
1394 (mask & SL11H_INTMASK_DP) ? " dp" : "");
1395}
1396
1397static int proc_sl811h_show(struct seq_file *s, void *unused)
1398{
1399 struct sl811 *sl811 = s->private;
1400 struct sl811h_ep *ep;
1401 unsigned i;
1402
1403 seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1404 sl811_to_hcd(sl811)->product_desc,
1405 hcd_name, DRIVER_VERSION,
1406 sl811->port1);
1407
1408 seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1409 seq_printf(s, "current session: done_a %ld done_b %ld "
1410 "wake %ld sof %ld overrun %ld lost %ld\n\n",
1411 sl811->stat_a, sl811->stat_b,
1412 sl811->stat_wake, sl811->stat_sof,
1413 sl811->stat_overrun, sl811->stat_lost);
1414
1415 spin_lock_irq(&sl811->lock);
1416
1417 if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1418 seq_printf(s, "(suspended)\n\n");
1419 else {
1420 u8 t = sl811_read(sl811, SL11H_CTLREG1);
1421
1422 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1423 (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1424 ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1425 case SL11H_CTL1MASK_NORMAL: s = ""; break;
1426 case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1427 case SL11H_CTL1MASK_K: s = " k/resume"; break;
1428 default: s = "j"; break;
1429 }; s; }),
1430 (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1431 (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1432
1433 dump_irq(s, "irq_enable",
1434 sl811_read(sl811, SL11H_IRQ_ENABLE));
1435 dump_irq(s, "irq_status",
1436 sl811_read(sl811, SL11H_IRQ_STATUS));
1437 seq_printf(s, "frame clocks remaining: %d\n",
1438 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1439 }
1440
1441 seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1442 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1443 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1444 seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1445 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1446 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1447 seq_printf(s, "\n");
1448 list_for_each_entry (ep, &sl811->async, schedule) {
1449 struct urb *urb;
1450
1451 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1452 " nak %d err %d\n",
1453 (ep == sl811->active_a) ? "(A) " : "",
1454 (ep == sl811->active_b) ? "(B) " : "",
1455 ep, ep->epnum,
1456 ({ char *s; switch (ep->nextpid) {
1457 case USB_PID_IN: s = "in"; break;
1458 case USB_PID_OUT: s = "out"; break;
1459 case USB_PID_SETUP: s = "setup"; break;
1460 case USB_PID_ACK: s = "status"; break;
1461 default: s = "?"; break;
1462 }; s;}),
1463 ep->maxpacket,
1464 ep->nak_count, ep->error_count);
1465 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1466 seq_printf(s, " urb%p, %d/%d\n", urb,
1467 urb->actual_length,
1468 urb->transfer_buffer_length);
1469 }
1470 }
1471 if (!list_empty(&sl811->async))
1472 seq_printf(s, "\n");
1473
1474 seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1475
1476 for (i = 0; i < PERIODIC_SIZE; i++) {
1477 ep = sl811->periodic[i];
1478 if (!ep)
1479 continue;
1480 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1481
1482 /* DUMB: prints shared entries multiple times */
1483 do {
1484 seq_printf(s,
1485 " %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1486 "err %d\n",
1487 (ep == sl811->active_a) ? "(A) " : "",
1488 (ep == sl811->active_b) ? "(B) " : "",
1489 ep->period, ep,
1490 (ep->udev->speed == USB_SPEED_FULL)
1491 ? "" : "ls ",
1492 ep->udev->devnum, ep->epnum,
1493 (ep->epnum == 0) ? ""
1494 : ((ep->nextpid == USB_PID_IN)
1495 ? "in"
1496 : "out"),
1497 ep->maxpacket, ep->error_count);
1498 ep = ep->next;
1499 } while (ep);
1500 }
1501
1502 spin_unlock_irq(&sl811->lock);
1503 seq_printf(s, "\n");
1504
1505 return 0;
1506}
1507
1508static int proc_sl811h_open(struct inode *inode, struct file *file)
1509{
Al Virod9dda782013-03-31 18:16:14 -04001510 return single_open(file, proc_sl811h_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03001513static const struct file_operations proc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 .open = proc_sl811h_open,
1515 .read = seq_read,
1516 .llseek = seq_lseek,
1517 .release = single_release,
1518};
1519
1520/* expect just one sl811 per system */
1521static const char proc_filename[] = "driver/sl811h";
1522
1523static void create_debug_file(struct sl811 *sl811)
1524{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07001525 sl811->pde = proc_create_data(proc_filename, 0, NULL, &proc_ops, sl811);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528static void remove_debug_file(struct sl811 *sl811)
1529{
1530 if (sl811->pde)
1531 remove_proc_entry(proc_filename, NULL);
1532}
1533
1534#endif
1535
1536/*-------------------------------------------------------------------------*/
1537
1538static void
1539sl811h_stop(struct usb_hcd *hcd)
1540{
1541 struct sl811 *sl811 = hcd_to_sl811(hcd);
1542 unsigned long flags;
1543
1544 del_timer_sync(&hcd->rh_timer);
1545
1546 spin_lock_irqsave(&sl811->lock, flags);
1547 port_power(sl811, 0);
1548 spin_unlock_irqrestore(&sl811->lock, flags);
1549}
1550
1551static int
1552sl811h_start(struct usb_hcd *hcd)
1553{
1554 struct sl811 *sl811 = hcd_to_sl811(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /* chip has been reset, VBUS power is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 hcd->state = HC_STATE_RUNNING;
1558
Alan Sternbc96c0a2005-04-25 11:21:31 -04001559 if (sl811->board) {
David Brownell0c8624f2005-11-07 15:31:25 -08001560 if (!device_can_wakeup(hcd->self.controller))
1561 device_init_wakeup(hcd->self.controller,
1562 sl811->board->can_wakeup);
Alan Sternbc96c0a2005-04-25 11:21:31 -04001563 hcd->power_budget = sl811->board->power * 2;
1564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001566 /* enable power and interrupts */
David Brownell1e9a47b2005-05-26 05:55:55 -07001567 port_power(sl811, 1);
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570}
1571
1572/*-------------------------------------------------------------------------*/
1573
1574static struct hc_driver sl811h_hc_driver = {
1575 .description = hcd_name,
1576 .hcd_priv_size = sizeof(struct sl811),
1577
1578 /*
1579 * generic hardware linkage
1580 */
1581 .irq = sl811h_irq,
1582 .flags = HCD_USB11 | HCD_MEMORY,
1583
1584 /* Basic lifecycle operations */
1585 .start = sl811h_start,
1586 .stop = sl811h_stop,
1587
1588 /*
1589 * managing i/o requests and associated device resources
1590 */
1591 .urb_enqueue = sl811h_urb_enqueue,
1592 .urb_dequeue = sl811h_urb_dequeue,
1593 .endpoint_disable = sl811h_endpoint_disable,
1594
1595 /*
1596 * periodic schedule support
1597 */
1598 .get_frame_number = sl811h_get_frame,
1599
1600 /*
1601 * root hub support
1602 */
1603 .hub_status_data = sl811h_hub_status_data,
1604 .hub_control = sl811h_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04001605 .bus_suspend = sl811h_bus_suspend,
1606 .bus_resume = sl811h_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607};
1608
1609/*-------------------------------------------------------------------------*/
1610
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001611static int
Russell King3ae5eae2005-11-09 22:32:44 +00001612sl811h_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Russell King3ae5eae2005-11-09 22:32:44 +00001614 struct usb_hcd *hcd = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 struct sl811 *sl811 = hcd_to_sl811(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 struct resource *res;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 remove_debug_file(sl811);
1619 usb_remove_hcd(hcd);
1620
David Brownell1e9a47b2005-05-26 05:55:55 -07001621 /* some platforms may use IORESOURCE_IO */
Russell King3ae5eae2005-11-09 22:32:44 +00001622 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
David Brownell1e9a47b2005-05-26 05:55:55 -07001623 if (res)
1624 iounmap(sl811->data_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Russell King3ae5eae2005-11-09 22:32:44 +00001626 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
David Brownell1e9a47b2005-05-26 05:55:55 -07001627 if (res)
1628 iounmap(sl811->addr_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 usb_put_hcd(hcd);
1631 return 0;
1632}
1633
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001634static int
Russell King3ae5eae2005-11-09 22:32:44 +00001635sl811h_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct usb_hcd *hcd;
1638 struct sl811 *sl811;
Marc Zyngier27140212008-08-18 13:08:42 +02001639 struct resource *addr, *data, *ires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int irq;
1641 void __iomem *addr_reg;
1642 void __iomem *data_reg;
1643 int retval;
David Brownell1e9a47b2005-05-26 05:55:55 -07001644 u8 tmp, ioaddr = 0;
Marc Zyngier27140212008-08-18 13:08:42 +02001645 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Tobias Klauser6ef1a922012-02-28 12:57:23 +01001647 if (usb_disabled())
1648 return -ENODEV;
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /* basic sanity checks first. board-specific init logic should
1651 * have initialized these three resources and probably board
1652 * specific platform_data. we don't probe for IRQs, and do only
1653 * minimal sanity checking.
1654 */
Marc Zyngier27140212008-08-18 13:08:42 +02001655 ires = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1656 if (dev->num_resources < 3 || !ires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return -ENODEV;
1658
Marc Zyngier27140212008-08-18 13:08:42 +02001659 irq = ires->start;
1660 irqflags = ires->flags & IRQF_TRIGGER_MASK;
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* refuse to confuse usbcore */
Russell King3ae5eae2005-11-09 22:32:44 +00001663 if (dev->dev.dma_mask) {
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001664 dev_dbg(&dev->dev, "no we won't dma\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return -EINVAL;
1666 }
1667
David Brownell1e9a47b2005-05-26 05:55:55 -07001668 /* the chip may be wired for either kind of addressing */
Russell King3ae5eae2005-11-09 22:32:44 +00001669 addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
1670 data = platform_get_resource(dev, IORESOURCE_MEM, 1);
David Brownell1e9a47b2005-05-26 05:55:55 -07001671 retval = -EBUSY;
1672 if (!addr || !data) {
Russell King3ae5eae2005-11-09 22:32:44 +00001673 addr = platform_get_resource(dev, IORESOURCE_IO, 0);
1674 data = platform_get_resource(dev, IORESOURCE_IO, 1);
David Brownell1e9a47b2005-05-26 05:55:55 -07001675 if (!addr || !data)
1676 return -ENODEV;
1677 ioaddr = 1;
Greg Kroah-Hartman2427ddd2006-06-12 17:07:52 -07001678 /*
1679 * NOTE: 64-bit resource->start is getting truncated
1680 * to avoid compiler warning, assuming that ->start
1681 * is always 32-bit for this case
1682 */
1683 addr_reg = (void __iomem *) (unsigned long) addr->start;
1684 data_reg = (void __iomem *) (unsigned long) data->start;
David Brownell1e9a47b2005-05-26 05:55:55 -07001685 } else {
1686 addr_reg = ioremap(addr->start, 1);
1687 if (addr_reg == NULL) {
1688 retval = -ENOMEM;
1689 goto err2;
1690 }
1691
1692 data_reg = ioremap(data->start, 1);
1693 if (data_reg == NULL) {
1694 retval = -ENOMEM;
1695 goto err4;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698
1699 /* allocate and initialize hcd */
Kay Sievers7071a3c2008-05-02 06:02:41 +02001700 hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!hcd) {
1702 retval = -ENOMEM;
1703 goto err5;
1704 }
1705 hcd->rsrc_start = addr->start;
1706 sl811 = hcd_to_sl811(hcd);
1707
1708 spin_lock_init(&sl811->lock);
1709 INIT_LIST_HEAD(&sl811->async);
Russell King3ae5eae2005-11-09 22:32:44 +00001710 sl811->board = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 init_timer(&sl811->timer);
1712 sl811->timer.function = sl811h_timer;
1713 sl811->timer.data = (unsigned long) sl811;
1714 sl811->addr_reg = addr_reg;
1715 sl811->data_reg = data_reg;
1716
1717 spin_lock_irq(&sl811->lock);
1718 port_power(sl811, 0);
1719 spin_unlock_irq(&sl811->lock);
1720 msleep(200);
1721
1722 tmp = sl811_read(sl811, SL11H_HWREVREG);
1723 switch (tmp >> 4) {
1724 case 1:
1725 hcd->product_desc = "SL811HS v1.2";
1726 break;
1727 case 2:
1728 hcd->product_desc = "SL811HS v1.5";
1729 break;
1730 default:
1731 /* reject case 0, SL11S is less functional */
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001732 dev_dbg(&dev->dev, "chiprev %02x\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 retval = -ENXIO;
1734 goto err6;
1735 }
1736
David Brownell1e9a47b2005-05-26 05:55:55 -07001737 /* The chip's IRQ is level triggered, active high. A requirement
1738 * for platform device setup is to cope with things like signal
1739 * inverters (e.g. CF is active low) or working only with edge
1740 * triggers (e.g. most ARM CPUs). Initial driver stress testing
1741 * was on a system with single edge triggering, so most sorts of
1742 * triggering arrangement should work.
Marc Zyngier27140212008-08-18 13:08:42 +02001743 *
1744 * Use resource IRQ flags if set by platform device setup.
David Brownell1e9a47b2005-05-26 05:55:55 -07001745 */
Marc Zyngier27140212008-08-18 13:08:42 +02001746 irqflags |= IRQF_SHARED;
Yong Zhangb5dd18d2011-09-07 16:10:52 +08001747 retval = usb_add_hcd(hcd, irq, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (retval != 0)
1749 goto err6;
1750
1751 create_debug_file(sl811);
1752 return retval;
1753
1754 err6:
1755 usb_put_hcd(hcd);
1756 err5:
David Brownell1e9a47b2005-05-26 05:55:55 -07001757 if (!ioaddr)
1758 iounmap(data_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 err4:
David Brownell1e9a47b2005-05-26 05:55:55 -07001760 if (!ioaddr)
1761 iounmap(addr_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 err2:
Greg Kroah-Hartmane4528282013-06-28 11:33:03 -07001763 dev_dbg(&dev->dev, "init error, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return retval;
1765}
1766
1767#ifdef CONFIG_PM
1768
1769/* for this device there's no useful distinction between the controller
David Brownell1e9a47b2005-05-26 05:55:55 -07001770 * and its root hub, except that the root hub only gets direct PM calls
Alan Stern84ebc102013-03-27 16:14:46 -04001771 * when CONFIG_PM_RUNTIME is enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
1773
1774static int
Russell King3ae5eae2005-11-09 22:32:44 +00001775sl811h_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Russell King3ae5eae2005-11-09 22:32:44 +00001777 struct usb_hcd *hcd = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct sl811 *sl811 = hcd_to_sl811(hcd);
1779 int retval = 0;
1780
David Brownell18584992006-08-14 23:11:06 -07001781 switch (state.event) {
1782 case PM_EVENT_FREEZE:
Alan Stern0c0382e2005-10-13 17:08:02 -04001783 retval = sl811h_bus_suspend(hcd);
David Brownell18584992006-08-14 23:11:06 -07001784 break;
1785 case PM_EVENT_SUSPEND:
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01001786 case PM_EVENT_HIBERNATE:
David Brownell18584992006-08-14 23:11:06 -07001787 case PM_EVENT_PRETHAW: /* explicitly discard hw state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 port_power(sl811, 0);
David Brownell18584992006-08-14 23:11:06 -07001789 break;
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return retval;
1792}
1793
1794static int
Russell King3ae5eae2005-11-09 22:32:44 +00001795sl811h_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Russell King3ae5eae2005-11-09 22:32:44 +00001797 struct usb_hcd *hcd = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct sl811 *sl811 = hcd_to_sl811(hcd);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /* with no "check to see if VBUS is still powered" board hook,
1801 * let's assume it'd only be powered to enable remote wakeup.
1802 */
Alan Stern70a1c9e2008-03-06 17:00:58 -05001803 if (!sl811->port1 || !device_can_wakeup(&hcd->self.root_hub->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 sl811->port1 = 0;
1805 port_power(sl811, 1);
Alan Stern1c50c312005-11-14 11:45:38 -05001806 usb_root_hub_lost_power(hcd->self.root_hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
1808 }
1809
Alan Stern0c0382e2005-10-13 17:08:02 -04001810 return sl811h_bus_resume(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813#else
1814
1815#define sl811h_suspend NULL
1816#define sl811h_resume NULL
1817
1818#endif
1819
1820
David Brownell1e9a47b2005-05-26 05:55:55 -07001821/* this driver is exported so sl811_cs can depend on it */
Russell King3ae5eae2005-11-09 22:32:44 +00001822struct platform_driver sl811h_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 .probe = sl811h_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001824 .remove = sl811h_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 .suspend = sl811h_suspend,
1827 .resume = sl811h_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001828 .driver = {
1829 .name = (char *) hcd_name,
1830 .owner = THIS_MODULE,
1831 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832};
David Brownell1e9a47b2005-05-26 05:55:55 -07001833EXPORT_SYMBOL(sl811h_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Tobias Klauser6ef1a922012-02-28 12:57:23 +01001835module_platform_driver(sl811h_driver);