blob: 448b9d1f0e70acbfd6530dbbcb394f6d601597aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Host Controller Interface driver for USB.
3 *
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5 *
6 * (C) Copyright 1999 Linus Torvalds
7 * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
8 * (C) Copyright 1999 Randy Dunlap
9 * (C) Copyright 1999 Georg Acher, acher@in.tum.de
10 * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
11 * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
12 * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
Alan Stern17230ac2007-02-19 15:52:45 -050016 * (C) Copyright 2004-2007 Alan Stern, stern@rowland.harvard.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * Intel documents this fairly well, and as far as I know there
19 * are no royalties or anything like that, but even so there are
20 * people who decided that they want to do the same thing in a
21 * completely different way.
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/pci.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/delay.h>
30#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/unistd.h>
34#include <linux/interrupt.h>
35#include <linux/spinlock.h>
36#include <linux/debugfs.h>
37#include <linux/pm.h>
38#include <linux/dmapool.h>
39#include <linux/dma-mapping.h>
40#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020041#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/bitops.h>
Alan Sternb62df452006-10-10 10:54:00 -040043#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46#include <asm/io.h>
47#include <asm/irq.h>
48#include <asm/system.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "uhci-hcd.h"
Adrian Bunk75e2df62006-03-25 18:01:53 +010051#include "pci-quirks.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * Version Information
55 */
Joe Perches85ee7a12011-04-23 20:38:19 -070056#define DRIVER_AUTHOR \
57 "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, " \
58 "Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, " \
59 "Roman Weissgaerber, Alan Stern"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define DRIVER_DESC "USB Universal Host Controller Interface driver"
61
Alan Stern5f8364b2006-12-05 16:29:55 -050062/* for flakey hardware, ignore overcurrent indicators */
63static int ignore_oc;
64module_param(ignore_oc, bool, S_IRUGO);
65MODULE_PARM_DESC(ignore_oc, "ignore hardware overcurrent indications");
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * debug = 0, no debugging messages
Alan Stern687f5f32005-11-30 17:16:19 -050069 * debug = 1, dump failed URBs except for stalls
70 * debug = 2, dump all failed URBs (including stalls)
GeunSik Lim837cbb02009-09-07 21:39:05 +090071 * show all queues in /sys/kernel/debug/uhci/[pci_addr]
Alan Stern687f5f32005-11-30 17:16:19 -050072 * debug = 3, show all TDs in URBs when dumping
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74#ifdef DEBUG
Alan Stern8d402e12005-12-17 18:03:37 -050075#define DEBUG_CONFIGURED 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int debug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077module_param(debug, int, S_IRUGO | S_IWUSR);
78MODULE_PARM_DESC(debug, "Debug level");
Alan Stern8d402e12005-12-17 18:03:37 -050079
80#else
81#define DEBUG_CONFIGURED 0
82#define debug 0
83#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static char *errbuf;
86#define ERRBUF_LEN (32 * 1024)
87
Christoph Lametere18b8902006-12-06 20:33:20 -080088static struct kmem_cache *uhci_up_cachep; /* urb_priv */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Alan Stern6c1b4452005-04-21 16:04:58 -040090static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
91static void wakeup_rh(struct uhci_hcd *uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Alan Sternf3fe2392007-01-08 12:00:28 -050094/*
95 * Calculate the link pointer DMA value for the first Skeleton QH in a frame.
96 */
97static __le32 uhci_frame_skel_link(struct uhci_hcd *uhci, int frame)
98{
99 int skelnum;
100
101 /*
102 * The interrupt queues will be interleaved as evenly as possible.
103 * There's not much to be done about period-1 interrupts; they have
104 * to occur in every frame. But we can schedule period-2 interrupts
105 * in odd-numbered frames, period-4 interrupts in frames congruent
106 * to 2 (mod 4), and so on. This way each frame only has two
107 * interrupt QHs, which will help spread out bandwidth utilization.
108 *
109 * ffs (Find First bit Set) does exactly what we need:
Alan Stern17230ac2007-02-19 15:52:45 -0500110 * 1,3,5,... => ffs = 0 => use period-2 QH = skelqh[8],
111 * 2,6,10,... => ffs = 1 => use period-4 QH = skelqh[7], etc.
Alan Sternf3fe2392007-01-08 12:00:28 -0500112 * ffs >= 7 => not on any high-period queue, so use
Alan Stern17230ac2007-02-19 15:52:45 -0500113 * period-1 QH = skelqh[9].
Alan Sternf3fe2392007-01-08 12:00:28 -0500114 * Add in UHCI_NUMFRAMES to insure at least one bit is set.
115 */
116 skelnum = 8 - (int) __ffs(frame | UHCI_NUMFRAMES);
117 if (skelnum <= 1)
118 skelnum = 9;
Alan Stern28b93252007-02-19 15:51:51 -0500119 return LINK_TO_QH(uhci->skelqh[skelnum]);
Alan Sternf3fe2392007-01-08 12:00:28 -0500120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include "uhci-debug.c"
123#include "uhci-q.c"
Alan Stern1f09df82005-09-05 13:59:51 -0400124#include "uhci-hub.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Alan Sterna8bed8b2005-04-09 17:29:00 -0400126/*
Alan Sternbb200f62005-10-03 16:36:29 -0400127 * Finish up a host controller reset and update the recorded state.
Alan Sterna8bed8b2005-04-09 17:29:00 -0400128 */
Alan Sternbb200f62005-10-03 16:36:29 -0400129static void finish_reset(struct uhci_hcd *uhci)
Alan Stern014e73c2005-04-09 17:24:42 -0400130{
Alan Sternc074b412005-04-22 14:39:12 -0400131 int port;
132
Alan Sternc074b412005-04-22 14:39:12 -0400133 /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
134 * bits in the port status and control registers.
135 * We have to clear them by hand.
136 */
137 for (port = 0; port < uhci->rh_numports; ++port)
138 outw(0, uhci->io_addr + USBPORTSC1 + (port * 2));
139
Alan Stern8e326402006-04-04 14:47:44 -0400140 uhci->port_c_suspend = uhci->resuming_ports = 0;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400141 uhci->rh_state = UHCI_RH_RESET;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400142 uhci->is_stopped = UHCI_IS_STOPPED;
143 uhci_to_hcd(uhci)->state = HC_STATE_HALT;
Alan Stern541c7d42010-06-22 16:39:10 -0400144 clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
Alan Sterne323de42006-06-05 12:21:30 -0400145
146 uhci->dead = 0; /* Full reset resurrects the controller */
Alan Stern014e73c2005-04-09 17:24:42 -0400147}
148
Alan Sterna8bed8b2005-04-09 17:29:00 -0400149/*
Alan Stern4daaa872005-04-09 17:30:08 -0400150 * Last rites for a defunct/nonfunctional controller
Alan Stern02597d22005-04-28 14:51:27 -0400151 * or one we don't want to use any more.
Alan Stern4daaa872005-04-09 17:30:08 -0400152 */
Alan Sterne323de42006-06-05 12:21:30 -0400153static void uhci_hc_died(struct uhci_hcd *uhci)
Alan Stern4daaa872005-04-09 17:30:08 -0400154{
Alan Sterne323de42006-06-05 12:21:30 -0400155 uhci_get_current_frame_number(uhci);
Alan Sternbb200f62005-10-03 16:36:29 -0400156 uhci_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr);
157 finish_reset(uhci);
Alan Sterne323de42006-06-05 12:21:30 -0400158 uhci->dead = 1;
159
160 /* The current frame may already be partway finished */
161 ++uhci->frame_number;
Alan Stern4daaa872005-04-09 17:30:08 -0400162}
163
164/*
David Brownellbe3cbc52006-06-05 12:16:39 -0400165 * Initialize a controller that was newly discovered or has lost power
166 * or otherwise been reset while it was suspended. In none of these cases
167 * can we be sure of its previous state.
Alan Sterna8bed8b2005-04-09 17:29:00 -0400168 */
169static void check_and_reset_hc(struct uhci_hcd *uhci)
170{
Alan Sternbb200f62005-10-03 16:36:29 -0400171 if (uhci_check_and_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr))
172 finish_reset(uhci);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400173}
174
175/*
176 * Store the basic register settings needed by the controller.
177 */
178static void configure_hc(struct uhci_hcd *uhci)
179{
Alan Stern0d436b42010-06-25 14:02:49 -0400180 struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
181
Alan Sterna8bed8b2005-04-09 17:29:00 -0400182 /* Set the frame length to the default: 1 ms exactly */
183 outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
184
185 /* Store the frame list base address */
Alan Sterna1d59ce2005-09-16 14:22:51 -0400186 outl(uhci->frame_dma_handle, uhci->io_addr + USBFLBASEADD);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400187
188 /* Set the current frame number */
Alan Sternc4334722006-05-19 16:34:57 -0400189 outw(uhci->frame_number & UHCI_MAX_SOF_NUMBER,
190 uhci->io_addr + USBFRNUM);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400191
Alan Sternf37be9b2005-10-17 10:42:06 -0400192 /* Mark controller as not halted before we enable interrupts */
193 uhci_to_hcd(uhci)->state = HC_STATE_SUSPENDED;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400194 mb();
195
196 /* Enable PIRQ */
Alan Stern0d436b42010-06-25 14:02:49 -0400197 pci_write_config_word(pdev, USBLEGSUP, USBLEGSUP_DEFAULT);
198
199 /* Disable platform-specific non-PME# wakeup */
200 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
201 pci_write_config_byte(pdev, USBRES_INTEL, 0);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400202}
203
204
Alan Sternc8f4fe42005-04-09 17:27:32 -0400205static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
Alan Stern014e73c2005-04-09 17:24:42 -0400206{
Alan Sternc8f4fe42005-04-09 17:27:32 -0400207 int port;
Alan Stern014e73c2005-04-09 17:24:42 -0400208
Alan Stern5f8364b2006-12-05 16:29:55 -0500209 /* If we have to ignore overcurrent events then almost by definition
210 * we can't depend on resume-detect interrupts. */
211 if (ignore_oc)
212 return 1;
213
Alan Sternc8f4fe42005-04-09 17:27:32 -0400214 switch (to_pci_dev(uhci_dev(uhci))->vendor) {
215 default:
216 break;
Alan Stern014e73c2005-04-09 17:24:42 -0400217
Alan Sternc8f4fe42005-04-09 17:27:32 -0400218 case PCI_VENDOR_ID_GENESYS:
219 /* Genesys Logic's GL880S controllers don't generate
220 * resume-detect interrupts.
221 */
222 return 1;
223
224 case PCI_VENDOR_ID_INTEL:
225 /* Some of Intel's USB controllers have a bug that causes
226 * resume-detect interrupts if any port has an over-current
227 * condition. To make matters worse, some motherboards
228 * hardwire unused USB ports' over-current inputs active!
229 * To prevent problems, we will not enable resume-detect
230 * interrupts if any ports are OC.
231 */
232 for (port = 0; port < uhci->rh_numports; ++port) {
233 if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
234 USBPORTSC_OC)
235 return 1;
236 }
237 break;
238 }
239 return 0;
240}
241
Alan Sternd8f12ab2008-04-22 10:49:15 -0400242static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
Alan Sternb62df452006-10-10 10:54:00 -0400243{
Alan Sternb62df452006-10-10 10:54:00 -0400244 int port;
Jeff Garzik18552562007-10-03 15:15:40 -0400245 const char *sys_info;
Alan Sternc80a70d2006-12-15 16:06:25 -0500246 static char bad_Asus_board[] = "A7V8X";
Alan Sternb62df452006-10-10 10:54:00 -0400247
248 /* One of Asus's motherboards has a bug which causes it to
249 * wake up immediately from suspend-to-RAM if any of the ports
250 * are connected. In such cases we will not set EGSM.
251 */
Alan Sternc80a70d2006-12-15 16:06:25 -0500252 sys_info = dmi_get_system_info(DMI_BOARD_NAME);
253 if (sys_info && !strcmp(sys_info, bad_Asus_board)) {
Alan Sternb62df452006-10-10 10:54:00 -0400254 for (port = 0; port < uhci->rh_numports; ++port) {
255 if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
256 USBPORTSC_CCS)
257 return 1;
258 }
259 }
260
261 return 0;
262}
263
Alan Sterna8bed8b2005-04-09 17:29:00 -0400264static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
Alan Sternc8f4fe42005-04-09 17:27:32 -0400265__releases(uhci->lock)
266__acquires(uhci->lock)
267{
268 int auto_stop;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400269 int int_enable, egsm_enable, wakeup_enable;
Alan Stern58a97ff2008-04-14 12:17:10 -0400270 struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400271
272 auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
Alan Stern58a97ff2008-04-14 12:17:10 -0400273 dev_dbg(&rhdev->dev, "%s%s\n", __func__,
Alan Sternc8f4fe42005-04-09 17:27:32 -0400274 (auto_stop ? " (auto-stop)" : ""));
275
Alan Sternd8f12ab2008-04-22 10:49:15 -0400276 /* Start off by assuming Resume-Detect interrupts and EGSM work
277 * and that remote wakeups should be enabled.
Alan Sternc8f4fe42005-04-09 17:27:32 -0400278 */
Alan Sternb62df452006-10-10 10:54:00 -0400279 egsm_enable = USBCMD_EGSM;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400280 uhci->RD_enable = 1;
Alan Stern1f09df82005-09-05 13:59:51 -0400281 int_enable = USBINTR_RESUME;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400282 wakeup_enable = 1;
283
284 /* In auto-stop mode wakeups must always be detected, but
285 * Resume-Detect interrupts may be prohibited. (In the absence
286 * of CONFIG_PM, they are always disallowed.)
287 */
288 if (auto_stop) {
289 if (!device_may_wakeup(&rhdev->dev))
290 int_enable = 0;
291
292 /* In bus-suspend mode wakeups may be disabled, but if they are
293 * allowed then so are Resume-Detect interrupts.
294 */
295 } else {
Alan Stern58a97ff2008-04-14 12:17:10 -0400296#ifdef CONFIG_PM
Alan Sternd8f12ab2008-04-22 10:49:15 -0400297 if (!rhdev->do_remote_wakeup)
298 wakeup_enable = 0;
Alan Stern58a97ff2008-04-14 12:17:10 -0400299#endif
Alan Sternd8f12ab2008-04-22 10:49:15 -0400300 }
301
302 /* EGSM causes the root hub to echo a 'K' signal (resume) out any
303 * port which requests a remote wakeup. According to the USB spec,
304 * every hub is supposed to do this. But if we are ignoring
305 * remote-wakeup requests anyway then there's no point to it.
306 * We also shouldn't enable EGSM if it's broken.
307 */
308 if (!wakeup_enable || global_suspend_mode_is_broken(uhci))
309 egsm_enable = 0;
310
311 /* If we're ignoring wakeup events then there's no reason to
312 * enable Resume-Detect interrupts. We also shouldn't enable
313 * them if they are broken or disallowed.
314 *
315 * This logic may lead us to enabling RD but not EGSM. The UHCI
316 * spec foolishly says that RD works only when EGSM is on, but
317 * there's no harm in enabling it anyway -- perhaps some chips
318 * will implement it!
319 */
320 if (!wakeup_enable || resume_detect_interrupts_are_broken(uhci) ||
321 !int_enable)
322 uhci->RD_enable = int_enable = 0;
Alan Sternb62df452006-10-10 10:54:00 -0400323
Alan Sternc8f4fe42005-04-09 17:27:32 -0400324 outw(int_enable, uhci->io_addr + USBINTR);
Alan Sternb62df452006-10-10 10:54:00 -0400325 outw(egsm_enable | USBCMD_CF, uhci->io_addr + USBCMD);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400326 mb();
Alan Sternc8f4fe42005-04-09 17:27:32 -0400327 udelay(5);
328
329 /* If we're auto-stopping then no devices have been attached
330 * for a while, so there shouldn't be any active URBs and the
331 * controller should stop after a few microseconds. Otherwise
332 * we will give the controller one frame to stop.
333 */
334 if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
335 uhci->rh_state = UHCI_RH_SUSPENDING;
336 spin_unlock_irq(&uhci->lock);
337 msleep(1);
338 spin_lock_irq(&uhci->lock);
Alan Sterne323de42006-06-05 12:21:30 -0400339 if (uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400340 return;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400341 }
342 if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
Alan Stern58a97ff2008-04-14 12:17:10 -0400343 dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
Alan Sternc8f4fe42005-04-09 17:27:32 -0400344
Alan Stern014e73c2005-04-09 17:24:42 -0400345 uhci_get_current_frame_number(uhci);
Alan Sternc8f4fe42005-04-09 17:27:32 -0400346
347 uhci->rh_state = new_state;
Alan Stern014e73c2005-04-09 17:24:42 -0400348 uhci->is_stopped = UHCI_IS_STOPPED;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400349
350 /* If interrupts don't work and remote wakeup is enabled then
351 * the suspended root hub needs to be polled.
352 */
Alan Stern541c7d42010-06-22 16:39:10 -0400353 if (!int_enable && wakeup_enable)
354 set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
355 else
356 clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
Alan Stern014e73c2005-04-09 17:24:42 -0400357
David Howells7d12e782006-10-05 14:55:46 +0100358 uhci_scan_schedule(uhci);
Alan Stern84afddd2006-05-12 11:35:45 -0400359 uhci_fsbr_off(uhci);
Alan Stern014e73c2005-04-09 17:24:42 -0400360}
361
Alan Sterna8bed8b2005-04-09 17:29:00 -0400362static void start_rh(struct uhci_hcd *uhci)
363{
Alan Sternf37be9b2005-10-17 10:42:06 -0400364 uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400365 uhci->is_stopped = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400366
367 /* Mark it configured and running with a 64-byte max packet.
368 * All interrupts are enabled, even though RESUME won't do anything.
369 */
370 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
371 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
372 uhci->io_addr + USBINTR);
373 mb();
Alan Stern6c1b4452005-04-21 16:04:58 -0400374 uhci->rh_state = UHCI_RH_RUNNING;
Alan Stern541c7d42010-06-22 16:39:10 -0400375 set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400376}
377
378static void wakeup_rh(struct uhci_hcd *uhci)
Alan Sternc8f4fe42005-04-09 17:27:32 -0400379__releases(uhci->lock)
380__acquires(uhci->lock)
Alan Stern014e73c2005-04-09 17:24:42 -0400381{
David Brownellbe3cbc52006-06-05 12:16:39 -0400382 dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800383 "%s%s\n", __func__,
Alan Sternc8f4fe42005-04-09 17:27:32 -0400384 uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
385 " (auto-start)" : "");
Alan Stern014e73c2005-04-09 17:24:42 -0400386
Alan Sternc8f4fe42005-04-09 17:27:32 -0400387 /* If we are auto-stopped then no devices are attached so there's
388 * no need for wakeup signals. Otherwise we send Global Resume
389 * for 20 ms.
390 */
391 if (uhci->rh_state == UHCI_RH_SUSPENDED) {
Alan Sternd8f12ab2008-04-22 10:49:15 -0400392 unsigned egsm;
393
394 /* Keep EGSM on if it was set before */
395 egsm = inw(uhci->io_addr + USBCMD) & USBCMD_EGSM;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400396 uhci->rh_state = UHCI_RH_RESUMING;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400397 outw(USBCMD_FGR | USBCMD_CF | egsm, uhci->io_addr + USBCMD);
Alan Sternc8f4fe42005-04-09 17:27:32 -0400398 spin_unlock_irq(&uhci->lock);
399 msleep(20);
400 spin_lock_irq(&uhci->lock);
Alan Sterne323de42006-06-05 12:21:30 -0400401 if (uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400402 return;
Alan Stern014e73c2005-04-09 17:24:42 -0400403
Alan Sternc8f4fe42005-04-09 17:27:32 -0400404 /* End Global Resume and wait for EOP to be sent */
405 outw(USBCMD_CF, uhci->io_addr + USBCMD);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400406 mb();
Alan Sternc8f4fe42005-04-09 17:27:32 -0400407 udelay(4);
408 if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
409 dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
Alan Stern014e73c2005-04-09 17:24:42 -0400410 }
Alan Sternc8f4fe42005-04-09 17:27:32 -0400411
Alan Sterna8bed8b2005-04-09 17:29:00 -0400412 start_rh(uhci);
Alan Stern014e73c2005-04-09 17:24:42 -0400413
Alan Stern6c1b4452005-04-21 16:04:58 -0400414 /* Restart root hub polling */
415 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
Alan Stern014e73c2005-04-09 17:24:42 -0400416}
417
David Howells7d12e782006-10-05 14:55:46 +0100418static irqreturn_t uhci_irq(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned short status;
422
423 /*
424 * Read the interrupt status, and write it back to clear the
425 * interrupt cause. Contrary to the UHCI specification, the
426 * "HC Halted" status bit is persistent: it is RO, not R/WC.
427 */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400428 status = inw(uhci->io_addr + USBSTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
430 return IRQ_NONE;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400431 outw(status, uhci->io_addr + USBSTS); /* Clear it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
434 if (status & USBSTS_HSE)
435 dev_err(uhci_dev(uhci), "host system error, "
436 "PCI problems?\n");
437 if (status & USBSTS_HCPE)
438 dev_err(uhci_dev(uhci), "host controller process "
439 "error, something bad happened!\n");
Alan Stern4daaa872005-04-09 17:30:08 -0400440 if (status & USBSTS_HCH) {
Alan Stern442258e2007-12-06 14:47:08 -0500441 spin_lock(&uhci->lock);
Alan Stern4daaa872005-04-09 17:30:08 -0400442 if (uhci->rh_state >= UHCI_RH_RUNNING) {
443 dev_err(uhci_dev(uhci),
444 "host controller halted, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 "very bad!\n");
Alan Stern8d402e12005-12-17 18:03:37 -0500446 if (debug > 1 && errbuf) {
447 /* Print the schedule for debugging */
448 uhci_sprint_schedule(uhci,
449 errbuf, ERRBUF_LEN);
450 lprintk(errbuf);
451 }
Alan Sterne323de42006-06-05 12:21:30 -0400452 uhci_hc_died(uhci);
Alan Stern1f09df82005-09-05 13:59:51 -0400453
454 /* Force a callback in case there are
455 * pending unlinks */
456 mod_timer(&hcd->rh_timer, jiffies);
Alan Stern4daaa872005-04-09 17:30:08 -0400457 }
Alan Stern442258e2007-12-06 14:47:08 -0500458 spin_unlock(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 }
461
462 if (status & USBSTS_RD)
Alan Stern6c1b4452005-04-21 16:04:58 -0400463 usb_hcd_poll_rh_status(hcd);
Alan Stern1f09df82005-09-05 13:59:51 -0400464 else {
Alan Stern442258e2007-12-06 14:47:08 -0500465 spin_lock(&uhci->lock);
David Howells7d12e782006-10-05 14:55:46 +0100466 uhci_scan_schedule(uhci);
Alan Stern442258e2007-12-06 14:47:08 -0500467 spin_unlock(&uhci->lock);
Alan Stern1f09df82005-09-05 13:59:51 -0400468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 return IRQ_HANDLED;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
474 * Store the current frame number in uhci->frame_number if the controller
Justin P. Mattock06125be2011-02-26 20:33:57 -0800475 * is running. Expand from 11 bits (of which we use only 10) to a
Alan Sternc4334722006-05-19 16:34:57 -0400476 * full-sized integer.
477 *
478 * Like many other parts of the driver, this code relies on being polled
479 * more than once per second as long as the controller is running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
481static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
482{
Alan Sternc4334722006-05-19 16:34:57 -0400483 if (!uhci->is_stopped) {
484 unsigned delta;
485
486 delta = (inw(uhci->io_addr + USBFRNUM) - uhci->frame_number) &
487 (UHCI_NUMFRAMES - 1);
488 uhci->frame_number += delta;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * De-allocate all resources
494 */
495static void release_uhci(struct uhci_hcd *uhci)
496{
497 int i;
498
Alan Stern8d402e12005-12-17 18:03:37 -0500499 if (DEBUG_CONFIGURED) {
500 spin_lock_irq(&uhci->lock);
501 uhci->is_initialized = 0;
502 spin_unlock_irq(&uhci->lock);
503
504 debugfs_remove(uhci->dentry);
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 for (i = 0; i < UHCI_NUM_SKELQH; i++)
Alan Stern8b4cd422005-09-16 14:17:45 -0400508 uhci_free_qh(uhci, uhci->skelqh[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Alan Stern8b4cd422005-09-16 14:17:45 -0400510 uhci_free_td(uhci, uhci->term_td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Alan Stern8b4cd422005-09-16 14:17:45 -0400512 dma_pool_destroy(uhci->qh_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Alan Stern8b4cd422005-09-16 14:17:45 -0400514 dma_pool_destroy(uhci->td_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Alan Sterna1d59ce2005-09-16 14:22:51 -0400516 kfree(uhci->frame_cpu);
517
518 dma_free_coherent(uhci_dev(uhci),
519 UHCI_NUMFRAMES * sizeof(*uhci->frame),
520 uhci->frame, uhci->frame_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
David Brownellbe3cbc52006-06-05 12:16:39 -0400523static int uhci_init(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Sternc074b412005-04-22 14:39:12 -0400526 unsigned io_size = (unsigned) hcd->rsrc_len;
527 int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 uhci->io_addr = (unsigned long) hcd->rsrc_start;
530
Alan Sternc074b412005-04-22 14:39:12 -0400531 /* The UHCI spec says devices must have 2 ports, and goes on to say
532 * they may have more but gives no way to determine how many there
Alan Sterne07fefa2005-05-31 16:33:21 -0400533 * are. However according to the UHCI spec, Bit 7 of the port
Alan Sternc074b412005-04-22 14:39:12 -0400534 * status and control register is always set to 1. So we try to
Alan Sterne07fefa2005-05-31 16:33:21 -0400535 * use this to our advantage. Another common failure mode when
536 * a nonexistent register is addressed is to return all ones, so
537 * we test for that also.
Alan Sternc074b412005-04-22 14:39:12 -0400538 */
539 for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
540 unsigned int portstatus;
541
542 portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
Alan Sterne07fefa2005-05-31 16:33:21 -0400543 if (!(portstatus & 0x0080) || portstatus == 0xffff)
Alan Sternc074b412005-04-22 14:39:12 -0400544 break;
545 }
546 if (debug)
547 dev_info(uhci_dev(uhci), "detected %d ports\n", port);
548
Alan Sterne07fefa2005-05-31 16:33:21 -0400549 /* Anything greater than 7 is weird so we'll ignore it. */
550 if (port > UHCI_RH_MAXCHILD) {
Alan Sternc074b412005-04-22 14:39:12 -0400551 dev_info(uhci_dev(uhci), "port count misdetected? "
552 "forcing to 2 ports\n");
553 port = 2;
554 }
555 uhci->rh_numports = port;
556
Alan Sterna8bed8b2005-04-09 17:29:00 -0400557 /* Kick BIOS off this hardware and reset if the controller
558 * isn't already safely quiescent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400560 check_and_reset_hc(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
562}
563
Alan Stern02597d22005-04-28 14:51:27 -0400564/* Make sure the controller is quiescent and that we're not using it
565 * any more. This is mainly for the benefit of programs which, like kexec,
566 * expect the hardware to be idle: not doing DMA or generating IRQs.
567 *
568 * This routine may be called in a damaged or failing kernel. Hence we
569 * do not acquire the spinlock before shutting down the controller.
570 */
571static void uhci_shutdown(struct pci_dev *pdev)
572{
Joe Perchesce7eb322010-11-15 12:14:01 -0800573 struct usb_hcd *hcd = pci_get_drvdata(pdev);
Alan Stern02597d22005-04-28 14:51:27 -0400574
Alan Sterne323de42006-06-05 12:21:30 -0400575 uhci_hc_died(hcd_to_uhci(hcd));
Alan Stern02597d22005-04-28 14:51:27 -0400576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Allocate a frame list, and then setup the skeleton
580 *
581 * The hardware doesn't really know any difference
582 * in the queues, but the order does matter for the
Alan Stern17230ac2007-02-19 15:52:45 -0500583 * protocols higher up. The order in which the queues
584 * are encountered by the hardware is:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Alan Stern17230ac2007-02-19 15:52:45 -0500586 * - All isochronous events are handled before any
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * of the queues. We don't do that here, because
588 * we'll create the actual TD entries on demand.
Alan Stern17230ac2007-02-19 15:52:45 -0500589 * - The first queue is the high-period interrupt queue.
590 * - The second queue is the period-1 interrupt and async
591 * (low-speed control, full-speed control, then bulk) queue.
592 * - The third queue is the terminating bandwidth reclamation queue,
593 * which contains no members, loops back to itself, and is present
594 * only when FSBR is on and there are no full-speed control or bulk QHs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
596static int uhci_start(struct usb_hcd *hcd)
597{
598 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
599 int retval = -EBUSY;
Alan Sternc074b412005-04-22 14:39:12 -0400600 int i;
Alan Sternb4092142010-08-05 13:12:14 -0400601 struct dentry __maybe_unused *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Alan Stern6c1b4452005-04-21 16:04:58 -0400603 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 spin_lock_init(&uhci->lock);
Alan Sternc5e3b742006-06-05 12:28:57 -0400606 setup_timer(&uhci->fsbr_timer, uhci_fsbr_timeout,
607 (unsigned long) uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500608 INIT_LIST_HEAD(&uhci->idle_qh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 init_waitqueue_head(&uhci->waitqh);
610
Alan Sternb4092142010-08-05 13:12:14 -0400611#ifdef UHCI_DEBUG_OPS
612 dentry = debugfs_create_file(hcd->self.bus_name,
613 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
614 uhci, &uhci_debug_operations);
615 if (!dentry) {
616 dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
617 return -ENOMEM;
Alan Stern8d402e12005-12-17 18:03:37 -0500618 }
Alan Sternb4092142010-08-05 13:12:14 -0400619 uhci->dentry = dentry;
620#endif
Alan Stern8d402e12005-12-17 18:03:37 -0500621
Alan Sterna1d59ce2005-09-16 14:22:51 -0400622 uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
623 UHCI_NUMFRAMES * sizeof(*uhci->frame),
624 &uhci->frame_dma_handle, 0);
625 if (!uhci->frame) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 dev_err(uhci_dev(uhci), "unable to allocate "
627 "consistent memory for frame list\n");
Alan Sterna1d59ce2005-09-16 14:22:51 -0400628 goto err_alloc_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Alan Sterna1d59ce2005-09-16 14:22:51 -0400630 memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Alan Sterna1d59ce2005-09-16 14:22:51 -0400632 uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
633 GFP_KERNEL);
634 if (!uhci->frame_cpu) {
635 dev_err(uhci_dev(uhci), "unable to allocate "
636 "memory for frame pointers\n");
637 goto err_alloc_frame_cpu;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
641 sizeof(struct uhci_td), 16, 0);
642 if (!uhci->td_pool) {
643 dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
644 goto err_create_td_pool;
645 }
646
647 uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
648 sizeof(struct uhci_qh), 16, 0);
649 if (!uhci->qh_pool) {
650 dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
651 goto err_create_qh_pool;
652 }
653
Alan Stern25321782005-04-25 11:14:31 -0400654 uhci->term_td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!uhci->term_td) {
656 dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
657 goto err_alloc_term_td;
658 }
659
660 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500661 uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!uhci->skelqh[i]) {
663 dev_err(uhci_dev(uhci), "unable to allocate QH\n");
664 goto err_alloc_skelqh;
665 }
666 }
667
668 /*
Alan Stern17230ac2007-02-19 15:52:45 -0500669 * 8 Interrupt queues; link all higher int queues to int1 = async
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
Alan Stern17230ac2007-02-19 15:52:45 -0500671 for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
672 uhci->skelqh[i]->link = LINK_TO_QH(uhci->skel_async_qh);
Alan Sterne009f1b2007-03-19 15:31:42 -0400673 uhci->skel_async_qh->link = UHCI_PTR_TERM;
674 uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_term_qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* This dummy TD is to work around a bug in Intel PIIX controllers */
Alan Sternfa346562005-11-30 11:57:51 -0500677 uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
Alan Stern17230ac2007-02-19 15:52:45 -0500678 (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
679 uhci->term_td->link = UHCI_PTR_TERM;
680 uhci->skel_async_qh->element = uhci->skel_term_qh->element =
681 LINK_TO_TD(uhci->term_td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /*
684 * Fill the frame list: make all entries point to the proper
685 * interrupt queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
687 for (i = 0; i < UHCI_NUMFRAMES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* Only place we don't use the frame list routines */
Alan Sternf3fe2392007-01-08 12:00:28 -0500690 uhci->frame[i] = uhci_frame_skel_link(uhci, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 /*
694 * Some architectures require a full mb() to enforce completion of
Alan Sterna8bed8b2005-04-09 17:29:00 -0400695 * the memory writes above before the I/O transfers in configure_hc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
697 mb();
Alan Sterna8bed8b2005-04-09 17:29:00 -0400698
699 configure_hc(uhci);
Alan Stern8d402e12005-12-17 18:03:37 -0500700 uhci->is_initialized = 1;
Alan Sternba297ed2010-06-09 17:34:39 -0400701 spin_lock_irq(&uhci->lock);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400702 start_rh(uhci);
Alan Sternba297ed2010-06-09 17:34:39 -0400703 spin_unlock_irq(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705
706/*
707 * error exits:
708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709err_alloc_skelqh:
Alan Stern8b4cd422005-09-16 14:17:45 -0400710 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
711 if (uhci->skelqh[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 uhci_free_qh(uhci, uhci->skelqh[i]);
Alan Stern8b4cd422005-09-16 14:17:45 -0400713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 uhci_free_td(uhci, uhci->term_td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717err_alloc_term_td:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 dma_pool_destroy(uhci->qh_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720err_create_qh_pool:
721 dma_pool_destroy(uhci->td_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723err_create_td_pool:
Alan Sterna1d59ce2005-09-16 14:22:51 -0400724 kfree(uhci->frame_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Alan Sterna1d59ce2005-09-16 14:22:51 -0400726err_alloc_frame_cpu:
727 dma_free_coherent(uhci_dev(uhci),
728 UHCI_NUMFRAMES * sizeof(*uhci->frame),
729 uhci->frame, uhci->frame_dma_handle);
730
731err_alloc_frame:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 debugfs_remove(uhci->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return retval;
735}
736
737static void uhci_stop(struct usb_hcd *hcd)
738{
739 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock_irq(&uhci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400742 if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
Alan Sterne323de42006-06-05 12:21:30 -0400743 uhci_hc_died(uhci);
David Howells7d12e782006-10-05 14:55:46 +0100744 uhci_scan_schedule(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_unlock_irq(&uhci->lock);
Pete Zaitcevd23356da2010-01-08 15:39:22 -0700746 synchronize_irq(hcd->irq);
Alan Stern6c1b4452005-04-21 16:04:58 -0400747
Alan Sternc5e3b742006-06-05 12:28:57 -0400748 del_timer_sync(&uhci->fsbr_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 release_uhci(uhci);
750}
751
752#ifdef CONFIG_PM
Alan Sterna8bed8b2005-04-09 17:29:00 -0400753static int uhci_rh_suspend(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
David Brownellbe3cbc52006-06-05 12:16:39 -0400756 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 spin_lock_irq(&uhci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400759 if (!HCD_HW_ACCESSIBLE(hcd))
David Brownellbe3cbc52006-06-05 12:16:39 -0400760 rc = -ESHUTDOWN;
Alan Sterncec3a532010-01-08 11:18:20 -0500761 else if (uhci->dead)
762 ; /* Dead controllers tell no tales */
763
764 /* Once the controller is stopped, port resumes that are already
765 * in progress won't complete. Hence if remote wakeup is enabled
766 * for the root hub and any ports are in the middle of a resume or
767 * remote wakeup, we must fail the suspend.
768 */
769 else if (hcd->self.root_hub->do_remote_wakeup &&
770 uhci->resuming_ports) {
771 dev_dbg(uhci_dev(uhci), "suspend failed because a port "
772 "is resuming\n");
773 rc = -EBUSY;
774 } else
Alan Stern4daaa872005-04-09 17:30:08 -0400775 suspend_rh(uhci, UHCI_RH_SUSPENDED);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400776 spin_unlock_irq(&uhci->lock);
David Brownellbe3cbc52006-06-05 12:16:39 -0400777 return rc;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400778}
779
780static int uhci_rh_resume(struct usb_hcd *hcd)
781{
782 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Stern4daaa872005-04-09 17:30:08 -0400783 int rc = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400784
785 spin_lock_irq(&uhci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400786 if (!HCD_HW_ACCESSIBLE(hcd))
David Brownellbe3cbc52006-06-05 12:16:39 -0400787 rc = -ESHUTDOWN;
Alan Sterncfa59da2007-06-21 16:25:35 -0400788 else if (!uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400789 wakeup_rh(uhci);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400790 spin_unlock_irq(&uhci->lock);
Alan Stern4daaa872005-04-09 17:30:08 -0400791 return rc;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400792}
793
Alan Stern41472002010-06-25 14:02:14 -0400794static int uhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
Alan Sterna8bed8b2005-04-09 17:29:00 -0400795{
796 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Stern0d436b42010-06-25 14:02:49 -0400797 struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
Alan Stern4daaa872005-04-09 17:30:08 -0400798 int rc = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400799
Harvey Harrison441b62c2008-03-03 16:08:34 -0800800 dev_dbg(uhci_dev(uhci), "%s\n", __func__);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400801
802 spin_lock_irq(&uhci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400803 if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
Alan Sterne323de42006-06-05 12:21:30 -0400804 goto done_okay; /* Already suspended or dead */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400805
Alan Stern4daaa872005-04-09 17:30:08 -0400806 if (uhci->rh_state > UHCI_RH_SUSPENDED) {
807 dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
Alan Stern4daaa872005-04-09 17:30:08 -0400808 rc = -EBUSY;
809 goto done;
810 };
811
Alan Sterna8bed8b2005-04-09 17:29:00 -0400812 /* All PCI host controllers are required to disable IRQ generation
813 * at the source, so we must turn off PIRQ.
814 */
Alan Stern0d436b42010-06-25 14:02:49 -0400815 pci_write_config_word(pdev, USBLEGSUP, 0);
Alan Stern541c7d42010-06-22 16:39:10 -0400816 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400817
Alan Stern0d436b42010-06-25 14:02:49 -0400818 /* Enable platform-specific non-PME# wakeup */
819 if (do_wakeup) {
820 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
821 pci_write_config_byte(pdev, USBRES_INTEL,
822 USBPORT1EN | USBPORT2EN);
823 }
Alan Sterna8bed8b2005-04-09 17:29:00 -0400824
Alan Sterne323de42006-06-05 12:21:30 -0400825done_okay:
826 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern4daaa872005-04-09 17:30:08 -0400827done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 spin_unlock_irq(&uhci->lock);
Alan Stern4daaa872005-04-09 17:30:08 -0400829 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Alan Stern6ec4beb2009-04-27 13:33:41 -0400832static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Harvey Harrison441b62c2008-03-03 16:08:34 -0800836 dev_dbg(uhci_dev(uhci), "%s\n", __func__);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400837
Alan Stern687f5f32005-11-30 17:16:19 -0500838 /* Since we aren't in D3 any more, it's safe to set this flag
Alan Sterne323de42006-06-05 12:21:30 -0400839 * even if the controller was dead.
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100840 */
841 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 spin_lock_irq(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Alan Stern6ec4beb2009-04-27 13:33:41 -0400845 /* Make sure resume from hibernation re-enumerates everything */
846 if (hibernated)
847 uhci_hc_died(uhci);
848
Alan Sterne323de42006-06-05 12:21:30 -0400849 /* The firmware or a boot kernel may have changed the controller
850 * settings during a system wakeup. Check it and reconfigure
851 * to avoid problems.
Alan Sterna8bed8b2005-04-09 17:29:00 -0400852 */
853 check_and_reset_hc(uhci);
Alan Sterne323de42006-06-05 12:21:30 -0400854
855 /* If the controller was dead before, it's back alive now */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400856 configure_hc(uhci);
857
Alan Stern0d436b42010-06-25 14:02:49 -0400858 /* Tell the core if the controller had to be reset */
859 if (uhci->rh_state == UHCI_RH_RESET)
Alan Stern1c50c312005-11-14 11:45:38 -0500860 usb_root_hub_lost_power(hcd->self.root_hub);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 spin_unlock_irq(&uhci->lock);
Alan Stern6c1b4452005-04-21 16:04:58 -0400863
Alan Sternd8f12ab2008-04-22 10:49:15 -0400864 /* If interrupts don't work and remote wakeup is enabled then
865 * the suspended root hub needs to be polled.
866 */
Alan Sternee0b9be2010-06-25 14:02:24 -0400867 if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup)
Alan Stern541c7d42010-06-22 16:39:10 -0400868 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Alan Sternee0b9be2010-06-25 14:02:24 -0400869
870 /* Does the root hub have a port wakeup pending? */
871 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
873}
874#endif
875
Alan Sterndccf4a42005-12-17 17:58:46 -0500876/* Wait until a particular device/endpoint's QH is idle, and free it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -0500878 struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Sterndccf4a42005-12-17 17:58:46 -0500881 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Alan Sterndccf4a42005-12-17 17:58:46 -0500883 spin_lock_irq(&uhci->lock);
884 qh = (struct uhci_qh *) hep->hcpriv;
885 if (qh == NULL)
886 goto done;
887
888 while (qh->state != QH_STATE_IDLE) {
889 ++uhci->num_waiting;
890 spin_unlock_irq(&uhci->lock);
891 wait_event_interruptible(uhci->waitqh,
892 qh->state == QH_STATE_IDLE);
893 spin_lock_irq(&uhci->lock);
894 --uhci->num_waiting;
895 }
896
897 uhci_free_qh(uhci, qh);
898done:
899 spin_unlock_irq(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
903{
904 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Sternc4334722006-05-19 16:34:57 -0400905 unsigned frame_number;
906 unsigned delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /* Minimize latency by avoiding the spinlock */
Alan Sternc4334722006-05-19 16:34:57 -0400909 frame_number = uhci->frame_number;
910 barrier();
911 delta = (inw(uhci->io_addr + USBFRNUM) - frame_number) &
912 (UHCI_NUMFRAMES - 1);
913 return frame_number + delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static const char hcd_name[] = "uhci_hcd";
917
918static const struct hc_driver uhci_driver = {
919 .description = hcd_name,
920 .product_desc = "UHCI Host Controller",
921 .hcd_priv_size = sizeof(struct uhci_hcd),
922
923 /* Generic hardware linkage */
924 .irq = uhci_irq,
925 .flags = HCD_USB11,
926
927 /* Basic lifecycle operations */
David Brownellbe3cbc52006-06-05 12:16:39 -0400928 .reset = uhci_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 .start = uhci_start,
930#ifdef CONFIG_PM
Alan Stern7be7d742008-04-03 18:03:06 -0400931 .pci_suspend = uhci_pci_suspend,
932 .pci_resume = uhci_pci_resume,
Alan Stern0c0382e2005-10-13 17:08:02 -0400933 .bus_suspend = uhci_rh_suspend,
934 .bus_resume = uhci_rh_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#endif
936 .stop = uhci_stop,
937
938 .urb_enqueue = uhci_urb_enqueue,
939 .urb_dequeue = uhci_urb_dequeue,
940
941 .endpoint_disable = uhci_hcd_endpoint_disable,
942 .get_frame_number = uhci_hcd_get_frame_number,
943
944 .hub_status_data = uhci_hub_status_data,
945 .hub_control = uhci_hub_control,
946};
947
948static const struct pci_device_id uhci_pci_ids[] = { {
949 /* handle any USB UHCI controller */
Jean Delvarec67808e2006-04-09 20:07:35 +0200950 PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_UHCI, ~0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 .driver_data = (unsigned long) &uhci_driver,
952 }, { /* end: all zeroes */ }
953};
954
955MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
956
957static struct pci_driver uhci_pci_driver = {
958 .name = (char *)hcd_name,
959 .id_table = uhci_pci_ids,
960
961 .probe = usb_hcd_pci_probe,
962 .remove = usb_hcd_pci_remove,
Alan Stern02597d22005-04-28 14:51:27 -0400963 .shutdown = uhci_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Alan Sternabb30642009-04-27 13:33:24 -0400965#ifdef CONFIG_PM_SLEEP
966 .driver = {
967 .pm = &usb_hcd_pci_pm_ops
968 },
969#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970};
971
972static int __init uhci_hcd_init(void)
973{
974 int retval = -ENOMEM;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (usb_disabled())
977 return -ENODEV;
978
Alan Stern2b70f072008-10-02 11:47:15 -0400979 printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
980 ignore_oc ? ", overcurrent ignored" : "");
Alan Stern9beeee62008-10-02 11:48:13 -0400981 set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
Alan Stern2b70f072008-10-02 11:47:15 -0400982
Alan Stern8d402e12005-12-17 18:03:37 -0500983 if (DEBUG_CONFIGURED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
985 if (!errbuf)
986 goto errbuf_failed;
Greg Kroah-Hartmanec20df22009-04-24 15:15:00 -0700987 uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
Alan Stern8d402e12005-12-17 18:03:37 -0500988 if (!uhci_debugfs_root)
989 goto debug_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
Paul Mundt20c2df82007-07-20 10:11:58 +0900993 sizeof(struct urb_priv), 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!uhci_up_cachep)
995 goto up_failed;
996
997 retval = pci_register_driver(&uhci_pci_driver);
998 if (retval)
999 goto init_failed;
1000
1001 return 0;
1002
1003init_failed:
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001004 kmem_cache_destroy(uhci_up_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006up_failed:
1007 debugfs_remove(uhci_debugfs_root);
1008
1009debug_failed:
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001010 kfree(errbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012errbuf_failed:
1013
Alan Stern9beeee62008-10-02 11:48:13 -04001014 clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return retval;
1016}
1017
1018static void __exit uhci_hcd_cleanup(void)
1019{
1020 pci_unregister_driver(&uhci_pci_driver);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001021 kmem_cache_destroy(uhci_up_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 debugfs_remove(uhci_debugfs_root);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001023 kfree(errbuf);
Alan Stern9beeee62008-10-02 11:48:13 -04001024 clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027module_init(uhci_hcd_init);
1028module_exit(uhci_hcd_cleanup);
1029
1030MODULE_AUTHOR(DRIVER_AUTHOR);
1031MODULE_DESCRIPTION(DRIVER_DESC);
1032MODULE_LICENSE("GPL");