blob: 3a7bfe7a8874c321990bb802910ea4a902671c9a [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>
41#include <linux/bitops.h>
Alan Sternb62df452006-10-10 10:54:00 -040042#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/uaccess.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/system.h>
48
49#include "../core/hcd.h"
50#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 */
Alan Sterndccf4a42005-12-17 17:58:46 -050056#define DRIVER_VERSION "v3.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
58Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
59Alan Stern"
60#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * show all queues in /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 Stern6c1b4452005-04-21 16:04:58 -0400144 uhci_to_hcd(uhci)->poll_rh = 0;
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{
180 /* Set the frame length to the default: 1 ms exactly */
181 outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
182
183 /* Store the frame list base address */
Alan Sterna1d59ce2005-09-16 14:22:51 -0400184 outl(uhci->frame_dma_handle, uhci->io_addr + USBFLBASEADD);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400185
186 /* Set the current frame number */
Alan Sternc4334722006-05-19 16:34:57 -0400187 outw(uhci->frame_number & UHCI_MAX_SOF_NUMBER,
188 uhci->io_addr + USBFRNUM);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400189
Alan Sternf37be9b2005-10-17 10:42:06 -0400190 /* Mark controller as not halted before we enable interrupts */
191 uhci_to_hcd(uhci)->state = HC_STATE_SUSPENDED;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400192 mb();
193
194 /* Enable PIRQ */
195 pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
196 USBLEGSUP_DEFAULT);
197}
198
199
Alan Sternc8f4fe42005-04-09 17:27:32 -0400200static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
Alan Stern014e73c2005-04-09 17:24:42 -0400201{
Alan Sternc8f4fe42005-04-09 17:27:32 -0400202 int port;
Alan Stern014e73c2005-04-09 17:24:42 -0400203
Alan Stern5f8364b2006-12-05 16:29:55 -0500204 /* If we have to ignore overcurrent events then almost by definition
205 * we can't depend on resume-detect interrupts. */
206 if (ignore_oc)
207 return 1;
208
Alan Sternc8f4fe42005-04-09 17:27:32 -0400209 switch (to_pci_dev(uhci_dev(uhci))->vendor) {
210 default:
211 break;
Alan Stern014e73c2005-04-09 17:24:42 -0400212
Alan Sternc8f4fe42005-04-09 17:27:32 -0400213 case PCI_VENDOR_ID_GENESYS:
214 /* Genesys Logic's GL880S controllers don't generate
215 * resume-detect interrupts.
216 */
217 return 1;
218
219 case PCI_VENDOR_ID_INTEL:
220 /* Some of Intel's USB controllers have a bug that causes
221 * resume-detect interrupts if any port has an over-current
222 * condition. To make matters worse, some motherboards
223 * hardwire unused USB ports' over-current inputs active!
224 * To prevent problems, we will not enable resume-detect
225 * interrupts if any ports are OC.
226 */
227 for (port = 0; port < uhci->rh_numports; ++port) {
228 if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
229 USBPORTSC_OC)
230 return 1;
231 }
232 break;
233 }
234 return 0;
235}
236
Alan Sternd8f12ab2008-04-22 10:49:15 -0400237static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
Alan Sternb62df452006-10-10 10:54:00 -0400238{
Alan Sternb62df452006-10-10 10:54:00 -0400239 int port;
Jeff Garzik18552562007-10-03 15:15:40 -0400240 const char *sys_info;
Alan Sternc80a70d2006-12-15 16:06:25 -0500241 static char bad_Asus_board[] = "A7V8X";
Alan Sternb62df452006-10-10 10:54:00 -0400242
243 /* One of Asus's motherboards has a bug which causes it to
244 * wake up immediately from suspend-to-RAM if any of the ports
245 * are connected. In such cases we will not set EGSM.
246 */
Alan Sternc80a70d2006-12-15 16:06:25 -0500247 sys_info = dmi_get_system_info(DMI_BOARD_NAME);
248 if (sys_info && !strcmp(sys_info, bad_Asus_board)) {
Alan Sternb62df452006-10-10 10:54:00 -0400249 for (port = 0; port < uhci->rh_numports; ++port) {
250 if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
251 USBPORTSC_CCS)
252 return 1;
253 }
254 }
255
256 return 0;
257}
258
Alan Sterna8bed8b2005-04-09 17:29:00 -0400259static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
Alan Sternc8f4fe42005-04-09 17:27:32 -0400260__releases(uhci->lock)
261__acquires(uhci->lock)
262{
263 int auto_stop;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400264 int int_enable, egsm_enable, wakeup_enable;
Alan Stern58a97ff2008-04-14 12:17:10 -0400265 struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400266
267 auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
Alan Stern58a97ff2008-04-14 12:17:10 -0400268 dev_dbg(&rhdev->dev, "%s%s\n", __func__,
Alan Sternc8f4fe42005-04-09 17:27:32 -0400269 (auto_stop ? " (auto-stop)" : ""));
270
Alan Sternd8f12ab2008-04-22 10:49:15 -0400271 /* Start off by assuming Resume-Detect interrupts and EGSM work
272 * and that remote wakeups should be enabled.
Alan Sternc8f4fe42005-04-09 17:27:32 -0400273 */
Alan Sternb62df452006-10-10 10:54:00 -0400274 egsm_enable = USBCMD_EGSM;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400275 uhci->RD_enable = 1;
Alan Stern1f09df82005-09-05 13:59:51 -0400276 int_enable = USBINTR_RESUME;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400277 wakeup_enable = 1;
278
279 /* In auto-stop mode wakeups must always be detected, but
280 * Resume-Detect interrupts may be prohibited. (In the absence
281 * of CONFIG_PM, they are always disallowed.)
282 */
283 if (auto_stop) {
284 if (!device_may_wakeup(&rhdev->dev))
285 int_enable = 0;
286
287 /* In bus-suspend mode wakeups may be disabled, but if they are
288 * allowed then so are Resume-Detect interrupts.
289 */
290 } else {
Alan Stern58a97ff2008-04-14 12:17:10 -0400291#ifdef CONFIG_PM
Alan Sternd8f12ab2008-04-22 10:49:15 -0400292 if (!rhdev->do_remote_wakeup)
293 wakeup_enable = 0;
Alan Stern58a97ff2008-04-14 12:17:10 -0400294#endif
Alan Sternd8f12ab2008-04-22 10:49:15 -0400295 }
296
297 /* EGSM causes the root hub to echo a 'K' signal (resume) out any
298 * port which requests a remote wakeup. According to the USB spec,
299 * every hub is supposed to do this. But if we are ignoring
300 * remote-wakeup requests anyway then there's no point to it.
301 * We also shouldn't enable EGSM if it's broken.
302 */
303 if (!wakeup_enable || global_suspend_mode_is_broken(uhci))
304 egsm_enable = 0;
305
306 /* If we're ignoring wakeup events then there's no reason to
307 * enable Resume-Detect interrupts. We also shouldn't enable
308 * them if they are broken or disallowed.
309 *
310 * This logic may lead us to enabling RD but not EGSM. The UHCI
311 * spec foolishly says that RD works only when EGSM is on, but
312 * there's no harm in enabling it anyway -- perhaps some chips
313 * will implement it!
314 */
315 if (!wakeup_enable || resume_detect_interrupts_are_broken(uhci) ||
316 !int_enable)
317 uhci->RD_enable = int_enable = 0;
Alan Sternb62df452006-10-10 10:54:00 -0400318
Alan Sternc8f4fe42005-04-09 17:27:32 -0400319 outw(int_enable, uhci->io_addr + USBINTR);
Alan Sternb62df452006-10-10 10:54:00 -0400320 outw(egsm_enable | USBCMD_CF, uhci->io_addr + USBCMD);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400321 mb();
Alan Sternc8f4fe42005-04-09 17:27:32 -0400322 udelay(5);
323
324 /* If we're auto-stopping then no devices have been attached
325 * for a while, so there shouldn't be any active URBs and the
326 * controller should stop after a few microseconds. Otherwise
327 * we will give the controller one frame to stop.
328 */
329 if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
330 uhci->rh_state = UHCI_RH_SUSPENDING;
331 spin_unlock_irq(&uhci->lock);
332 msleep(1);
333 spin_lock_irq(&uhci->lock);
Alan Sterne323de42006-06-05 12:21:30 -0400334 if (uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400335 return;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400336 }
337 if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
Alan Stern58a97ff2008-04-14 12:17:10 -0400338 dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
Alan Sternc8f4fe42005-04-09 17:27:32 -0400339
Alan Stern014e73c2005-04-09 17:24:42 -0400340 uhci_get_current_frame_number(uhci);
Alan Sternc8f4fe42005-04-09 17:27:32 -0400341
342 uhci->rh_state = new_state;
Alan Stern014e73c2005-04-09 17:24:42 -0400343 uhci->is_stopped = UHCI_IS_STOPPED;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400344
345 /* If interrupts don't work and remote wakeup is enabled then
346 * the suspended root hub needs to be polled.
347 */
348 uhci_to_hcd(uhci)->poll_rh = (!int_enable && wakeup_enable);
Alan Stern014e73c2005-04-09 17:24:42 -0400349
David Howells7d12e782006-10-05 14:55:46 +0100350 uhci_scan_schedule(uhci);
Alan Stern84afddd2006-05-12 11:35:45 -0400351 uhci_fsbr_off(uhci);
Alan Stern014e73c2005-04-09 17:24:42 -0400352}
353
Alan Sterna8bed8b2005-04-09 17:29:00 -0400354static void start_rh(struct uhci_hcd *uhci)
355{
Alan Sternf37be9b2005-10-17 10:42:06 -0400356 uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400357 uhci->is_stopped = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400358
359 /* Mark it configured and running with a 64-byte max packet.
360 * All interrupts are enabled, even though RESUME won't do anything.
361 */
362 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
363 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
364 uhci->io_addr + USBINTR);
365 mb();
Alan Stern6c1b4452005-04-21 16:04:58 -0400366 uhci->rh_state = UHCI_RH_RUNNING;
367 uhci_to_hcd(uhci)->poll_rh = 1;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400368}
369
370static void wakeup_rh(struct uhci_hcd *uhci)
Alan Sternc8f4fe42005-04-09 17:27:32 -0400371__releases(uhci->lock)
372__acquires(uhci->lock)
Alan Stern014e73c2005-04-09 17:24:42 -0400373{
David Brownellbe3cbc52006-06-05 12:16:39 -0400374 dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800375 "%s%s\n", __func__,
Alan Sternc8f4fe42005-04-09 17:27:32 -0400376 uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
377 " (auto-start)" : "");
Alan Stern014e73c2005-04-09 17:24:42 -0400378
Alan Sternc8f4fe42005-04-09 17:27:32 -0400379 /* If we are auto-stopped then no devices are attached so there's
380 * no need for wakeup signals. Otherwise we send Global Resume
381 * for 20 ms.
382 */
383 if (uhci->rh_state == UHCI_RH_SUSPENDED) {
Alan Sternd8f12ab2008-04-22 10:49:15 -0400384 unsigned egsm;
385
386 /* Keep EGSM on if it was set before */
387 egsm = inw(uhci->io_addr + USBCMD) & USBCMD_EGSM;
Alan Sternc8f4fe42005-04-09 17:27:32 -0400388 uhci->rh_state = UHCI_RH_RESUMING;
Alan Sternd8f12ab2008-04-22 10:49:15 -0400389 outw(USBCMD_FGR | USBCMD_CF | egsm, uhci->io_addr + USBCMD);
Alan Sternc8f4fe42005-04-09 17:27:32 -0400390 spin_unlock_irq(&uhci->lock);
391 msleep(20);
392 spin_lock_irq(&uhci->lock);
Alan Sterne323de42006-06-05 12:21:30 -0400393 if (uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400394 return;
Alan Stern014e73c2005-04-09 17:24:42 -0400395
Alan Sternc8f4fe42005-04-09 17:27:32 -0400396 /* End Global Resume and wait for EOP to be sent */
397 outw(USBCMD_CF, uhci->io_addr + USBCMD);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400398 mb();
Alan Sternc8f4fe42005-04-09 17:27:32 -0400399 udelay(4);
400 if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
401 dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
Alan Stern014e73c2005-04-09 17:24:42 -0400402 }
Alan Sternc8f4fe42005-04-09 17:27:32 -0400403
Alan Sterna8bed8b2005-04-09 17:29:00 -0400404 start_rh(uhci);
Alan Stern014e73c2005-04-09 17:24:42 -0400405
Alan Stern6c1b4452005-04-21 16:04:58 -0400406 /* Restart root hub polling */
407 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
Alan Stern014e73c2005-04-09 17:24:42 -0400408}
409
David Howells7d12e782006-10-05 14:55:46 +0100410static irqreturn_t uhci_irq(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned short status;
414
415 /*
416 * Read the interrupt status, and write it back to clear the
417 * interrupt cause. Contrary to the UHCI specification, the
418 * "HC Halted" status bit is persistent: it is RO, not R/WC.
419 */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400420 status = inw(uhci->io_addr + USBSTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
422 return IRQ_NONE;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400423 outw(status, uhci->io_addr + USBSTS); /* Clear it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
426 if (status & USBSTS_HSE)
427 dev_err(uhci_dev(uhci), "host system error, "
428 "PCI problems?\n");
429 if (status & USBSTS_HCPE)
430 dev_err(uhci_dev(uhci), "host controller process "
431 "error, something bad happened!\n");
Alan Stern4daaa872005-04-09 17:30:08 -0400432 if (status & USBSTS_HCH) {
Alan Stern442258e2007-12-06 14:47:08 -0500433 spin_lock(&uhci->lock);
Alan Stern4daaa872005-04-09 17:30:08 -0400434 if (uhci->rh_state >= UHCI_RH_RUNNING) {
435 dev_err(uhci_dev(uhci),
436 "host controller halted, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 "very bad!\n");
Alan Stern8d402e12005-12-17 18:03:37 -0500438 if (debug > 1 && errbuf) {
439 /* Print the schedule for debugging */
440 uhci_sprint_schedule(uhci,
441 errbuf, ERRBUF_LEN);
442 lprintk(errbuf);
443 }
Alan Sterne323de42006-06-05 12:21:30 -0400444 uhci_hc_died(uhci);
Alan Stern1f09df82005-09-05 13:59:51 -0400445
446 /* Force a callback in case there are
447 * pending unlinks */
448 mod_timer(&hcd->rh_timer, jiffies);
Alan Stern4daaa872005-04-09 17:30:08 -0400449 }
Alan Stern442258e2007-12-06 14:47:08 -0500450 spin_unlock(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453
454 if (status & USBSTS_RD)
Alan Stern6c1b4452005-04-21 16:04:58 -0400455 usb_hcd_poll_rh_status(hcd);
Alan Stern1f09df82005-09-05 13:59:51 -0400456 else {
Alan Stern442258e2007-12-06 14:47:08 -0500457 spin_lock(&uhci->lock);
David Howells7d12e782006-10-05 14:55:46 +0100458 uhci_scan_schedule(uhci);
Alan Stern442258e2007-12-06 14:47:08 -0500459 spin_unlock(&uhci->lock);
Alan Stern1f09df82005-09-05 13:59:51 -0400460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 return IRQ_HANDLED;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
466 * Store the current frame number in uhci->frame_number if the controller
Alan Sternc4334722006-05-19 16:34:57 -0400467 * is runnning. Expand from 11 bits (of which we use only 10) to a
468 * full-sized integer.
469 *
470 * Like many other parts of the driver, this code relies on being polled
471 * more than once per second as long as the controller is running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
473static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
474{
Alan Sternc4334722006-05-19 16:34:57 -0400475 if (!uhci->is_stopped) {
476 unsigned delta;
477
478 delta = (inw(uhci->io_addr + USBFRNUM) - uhci->frame_number) &
479 (UHCI_NUMFRAMES - 1);
480 uhci->frame_number += delta;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * De-allocate all resources
486 */
487static void release_uhci(struct uhci_hcd *uhci)
488{
489 int i;
490
Alan Stern8d402e12005-12-17 18:03:37 -0500491 if (DEBUG_CONFIGURED) {
492 spin_lock_irq(&uhci->lock);
493 uhci->is_initialized = 0;
494 spin_unlock_irq(&uhci->lock);
495
496 debugfs_remove(uhci->dentry);
497 }
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 for (i = 0; i < UHCI_NUM_SKELQH; i++)
Alan Stern8b4cd422005-09-16 14:17:45 -0400500 uhci_free_qh(uhci, uhci->skelqh[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alan Stern8b4cd422005-09-16 14:17:45 -0400502 uhci_free_td(uhci, uhci->term_td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Alan Stern8b4cd422005-09-16 14:17:45 -0400504 dma_pool_destroy(uhci->qh_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Alan Stern8b4cd422005-09-16 14:17:45 -0400506 dma_pool_destroy(uhci->td_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alan Sterna1d59ce2005-09-16 14:22:51 -0400508 kfree(uhci->frame_cpu);
509
510 dma_free_coherent(uhci_dev(uhci),
511 UHCI_NUMFRAMES * sizeof(*uhci->frame),
512 uhci->frame, uhci->frame_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
David Brownellbe3cbc52006-06-05 12:16:39 -0400515static int uhci_init(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Sternc074b412005-04-22 14:39:12 -0400518 unsigned io_size = (unsigned) hcd->rsrc_len;
519 int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 uhci->io_addr = (unsigned long) hcd->rsrc_start;
522
Alan Sternc074b412005-04-22 14:39:12 -0400523 /* The UHCI spec says devices must have 2 ports, and goes on to say
524 * they may have more but gives no way to determine how many there
Alan Sterne07fefa2005-05-31 16:33:21 -0400525 * are. However according to the UHCI spec, Bit 7 of the port
Alan Sternc074b412005-04-22 14:39:12 -0400526 * status and control register is always set to 1. So we try to
Alan Sterne07fefa2005-05-31 16:33:21 -0400527 * use this to our advantage. Another common failure mode when
528 * a nonexistent register is addressed is to return all ones, so
529 * we test for that also.
Alan Sternc074b412005-04-22 14:39:12 -0400530 */
531 for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
532 unsigned int portstatus;
533
534 portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
Alan Sterne07fefa2005-05-31 16:33:21 -0400535 if (!(portstatus & 0x0080) || portstatus == 0xffff)
Alan Sternc074b412005-04-22 14:39:12 -0400536 break;
537 }
538 if (debug)
539 dev_info(uhci_dev(uhci), "detected %d ports\n", port);
540
Alan Sterne07fefa2005-05-31 16:33:21 -0400541 /* Anything greater than 7 is weird so we'll ignore it. */
542 if (port > UHCI_RH_MAXCHILD) {
Alan Sternc074b412005-04-22 14:39:12 -0400543 dev_info(uhci_dev(uhci), "port count misdetected? "
544 "forcing to 2 ports\n");
545 port = 2;
546 }
547 uhci->rh_numports = port;
548
Alan Sterna8bed8b2005-04-09 17:29:00 -0400549 /* Kick BIOS off this hardware and reset if the controller
550 * isn't already safely quiescent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400552 check_and_reset_hc(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554}
555
Alan Stern02597d22005-04-28 14:51:27 -0400556/* Make sure the controller is quiescent and that we're not using it
557 * any more. This is mainly for the benefit of programs which, like kexec,
558 * expect the hardware to be idle: not doing DMA or generating IRQs.
559 *
560 * This routine may be called in a damaged or failing kernel. Hence we
561 * do not acquire the spinlock before shutting down the controller.
562 */
563static void uhci_shutdown(struct pci_dev *pdev)
564{
565 struct usb_hcd *hcd = (struct usb_hcd *) pci_get_drvdata(pdev);
566
Alan Sterne323de42006-06-05 12:21:30 -0400567 uhci_hc_died(hcd_to_uhci(hcd));
Alan Stern02597d22005-04-28 14:51:27 -0400568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/*
571 * Allocate a frame list, and then setup the skeleton
572 *
573 * The hardware doesn't really know any difference
574 * in the queues, but the order does matter for the
Alan Stern17230ac2007-02-19 15:52:45 -0500575 * protocols higher up. The order in which the queues
576 * are encountered by the hardware is:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
Alan Stern17230ac2007-02-19 15:52:45 -0500578 * - All isochronous events are handled before any
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * of the queues. We don't do that here, because
580 * we'll create the actual TD entries on demand.
Alan Stern17230ac2007-02-19 15:52:45 -0500581 * - The first queue is the high-period interrupt queue.
582 * - The second queue is the period-1 interrupt and async
583 * (low-speed control, full-speed control, then bulk) queue.
584 * - The third queue is the terminating bandwidth reclamation queue,
585 * which contains no members, loops back to itself, and is present
586 * only when FSBR is on and there are no full-speed control or bulk QHs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588static int uhci_start(struct usb_hcd *hcd)
589{
590 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
591 int retval = -EBUSY;
Alan Sternc074b412005-04-22 14:39:12 -0400592 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct dentry *dentry;
594
Alan Stern6c1b4452005-04-21 16:04:58 -0400595 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_lock_init(&uhci->lock);
Alan Sternc5e3b742006-06-05 12:28:57 -0400598 setup_timer(&uhci->fsbr_timer, uhci_fsbr_timeout,
599 (unsigned long) uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500600 INIT_LIST_HEAD(&uhci->idle_qh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 init_waitqueue_head(&uhci->waitqh);
602
Alan Stern8d402e12005-12-17 18:03:37 -0500603 if (DEBUG_CONFIGURED) {
604 dentry = debugfs_create_file(hcd->self.bus_name,
605 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
606 uhci, &uhci_debug_operations);
607 if (!dentry) {
608 dev_err(uhci_dev(uhci), "couldn't create uhci "
609 "debugfs entry\n");
610 retval = -ENOMEM;
611 goto err_create_debug_entry;
612 }
613 uhci->dentry = dentry;
614 }
615
Alan Sterna1d59ce2005-09-16 14:22:51 -0400616 uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
617 UHCI_NUMFRAMES * sizeof(*uhci->frame),
618 &uhci->frame_dma_handle, 0);
619 if (!uhci->frame) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 dev_err(uhci_dev(uhci), "unable to allocate "
621 "consistent memory for frame list\n");
Alan Sterna1d59ce2005-09-16 14:22:51 -0400622 goto err_alloc_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Alan Sterna1d59ce2005-09-16 14:22:51 -0400624 memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Alan Sterna1d59ce2005-09-16 14:22:51 -0400626 uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
627 GFP_KERNEL);
628 if (!uhci->frame_cpu) {
629 dev_err(uhci_dev(uhci), "unable to allocate "
630 "memory for frame pointers\n");
631 goto err_alloc_frame_cpu;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
635 sizeof(struct uhci_td), 16, 0);
636 if (!uhci->td_pool) {
637 dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
638 goto err_create_td_pool;
639 }
640
641 uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
642 sizeof(struct uhci_qh), 16, 0);
643 if (!uhci->qh_pool) {
644 dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
645 goto err_create_qh_pool;
646 }
647
Alan Stern25321782005-04-25 11:14:31 -0400648 uhci->term_td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!uhci->term_td) {
650 dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
651 goto err_alloc_term_td;
652 }
653
654 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500655 uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!uhci->skelqh[i]) {
657 dev_err(uhci_dev(uhci), "unable to allocate QH\n");
658 goto err_alloc_skelqh;
659 }
660 }
661
662 /*
Alan Stern17230ac2007-02-19 15:52:45 -0500663 * 8 Interrupt queues; link all higher int queues to int1 = async
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Alan Stern17230ac2007-02-19 15:52:45 -0500665 for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
666 uhci->skelqh[i]->link = LINK_TO_QH(uhci->skel_async_qh);
Alan Sterne009f1b2007-03-19 15:31:42 -0400667 uhci->skel_async_qh->link = UHCI_PTR_TERM;
668 uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_term_qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* This dummy TD is to work around a bug in Intel PIIX controllers */
Alan Sternfa346562005-11-30 11:57:51 -0500671 uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
Alan Stern17230ac2007-02-19 15:52:45 -0500672 (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
673 uhci->term_td->link = UHCI_PTR_TERM;
674 uhci->skel_async_qh->element = uhci->skel_term_qh->element =
675 LINK_TO_TD(uhci->term_td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /*
678 * Fill the frame list: make all entries point to the proper
679 * interrupt queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
681 for (i = 0; i < UHCI_NUMFRAMES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /* Only place we don't use the frame list routines */
Alan Sternf3fe2392007-01-08 12:00:28 -0500684 uhci->frame[i] = uhci_frame_skel_link(uhci, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 /*
688 * Some architectures require a full mb() to enforce completion of
Alan Sterna8bed8b2005-04-09 17:29:00 -0400689 * the memory writes above before the I/O transfers in configure_hc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
691 mb();
Alan Sterna8bed8b2005-04-09 17:29:00 -0400692
693 configure_hc(uhci);
Alan Stern8d402e12005-12-17 18:03:37 -0500694 uhci->is_initialized = 1;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400695 start_rh(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697
698/*
699 * error exits:
700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701err_alloc_skelqh:
Alan Stern8b4cd422005-09-16 14:17:45 -0400702 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
703 if (uhci->skelqh[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 uhci_free_qh(uhci, uhci->skelqh[i]);
Alan Stern8b4cd422005-09-16 14:17:45 -0400705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 uhci_free_td(uhci, uhci->term_td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709err_alloc_term_td:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 dma_pool_destroy(uhci->qh_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712err_create_qh_pool:
713 dma_pool_destroy(uhci->td_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715err_create_td_pool:
Alan Sterna1d59ce2005-09-16 14:22:51 -0400716 kfree(uhci->frame_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Alan Sterna1d59ce2005-09-16 14:22:51 -0400718err_alloc_frame_cpu:
719 dma_free_coherent(uhci_dev(uhci),
720 UHCI_NUMFRAMES * sizeof(*uhci->frame),
721 uhci->frame, uhci->frame_dma_handle);
722
723err_alloc_frame:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 debugfs_remove(uhci->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726err_create_debug_entry:
727 return retval;
728}
729
730static void uhci_stop(struct usb_hcd *hcd)
731{
732 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 spin_lock_irq(&uhci->lock);
Alan Sterne323de42006-06-05 12:21:30 -0400735 if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) && !uhci->dead)
736 uhci_hc_died(uhci);
David Howells7d12e782006-10-05 14:55:46 +0100737 uhci_scan_schedule(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 spin_unlock_irq(&uhci->lock);
Alan Stern6c1b4452005-04-21 16:04:58 -0400739
Alan Sternc5e3b742006-06-05 12:28:57 -0400740 del_timer_sync(&uhci->fsbr_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 release_uhci(uhci);
742}
743
744#ifdef CONFIG_PM
Alan Sterna8bed8b2005-04-09 17:29:00 -0400745static int uhci_rh_suspend(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
David Brownellbe3cbc52006-06-05 12:16:39 -0400748 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 spin_lock_irq(&uhci->lock);
David Brownellbe3cbc52006-06-05 12:16:39 -0400751 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
752 rc = -ESHUTDOWN;
Alan Sterne323de42006-06-05 12:21:30 -0400753 else if (!uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400754 suspend_rh(uhci, UHCI_RH_SUSPENDED);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400755 spin_unlock_irq(&uhci->lock);
David Brownellbe3cbc52006-06-05 12:16:39 -0400756 return rc;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400757}
758
759static int uhci_rh_resume(struct usb_hcd *hcd)
760{
761 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Stern4daaa872005-04-09 17:30:08 -0400762 int rc = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400763
764 spin_lock_irq(&uhci->lock);
Alan Sterncfa59da2007-06-21 16:25:35 -0400765 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
David Brownellbe3cbc52006-06-05 12:16:39 -0400766 rc = -ESHUTDOWN;
Alan Sterncfa59da2007-06-21 16:25:35 -0400767 else if (!uhci->dead)
Alan Stern4daaa872005-04-09 17:30:08 -0400768 wakeup_rh(uhci);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400769 spin_unlock_irq(&uhci->lock);
Alan Stern4daaa872005-04-09 17:30:08 -0400770 return rc;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400771}
772
Alan Stern7be7d742008-04-03 18:03:06 -0400773static int uhci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
Alan Sterna8bed8b2005-04-09 17:29:00 -0400774{
775 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Stern4daaa872005-04-09 17:30:08 -0400776 int rc = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400777
Harvey Harrison441b62c2008-03-03 16:08:34 -0800778 dev_dbg(uhci_dev(uhci), "%s\n", __func__);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400779
780 spin_lock_irq(&uhci->lock);
Alan Sterne323de42006-06-05 12:21:30 -0400781 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) || uhci->dead)
782 goto done_okay; /* Already suspended or dead */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400783
Alan Stern4daaa872005-04-09 17:30:08 -0400784 if (uhci->rh_state > UHCI_RH_SUSPENDED) {
785 dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
Alan Stern4daaa872005-04-09 17:30:08 -0400786 rc = -EBUSY;
787 goto done;
788 };
789
Alan Sterna8bed8b2005-04-09 17:29:00 -0400790 /* All PCI host controllers are required to disable IRQ generation
791 * at the source, so we must turn off PIRQ.
792 */
793 pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0);
Alan Stern42245e62005-12-16 11:09:01 -0800794 mb();
Alan Stern1f09df82005-09-05 13:59:51 -0400795 hcd->poll_rh = 0;
Alan Sterna8bed8b2005-04-09 17:29:00 -0400796
797 /* FIXME: Enable non-PME# remote wakeup? */
798
David Brownell18584992006-08-14 23:11:06 -0700799 /* make sure snapshot being resumed re-enumerates everything */
800 if (message.event == PM_EVENT_PRETHAW)
801 uhci_hc_died(uhci);
802
Alan Sterne323de42006-06-05 12:21:30 -0400803done_okay:
804 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern4daaa872005-04-09 17:30:08 -0400805done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 spin_unlock_irq(&uhci->lock);
Alan Stern4daaa872005-04-09 17:30:08 -0400807 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Alan Stern7be7d742008-04-03 18:03:06 -0400810static int uhci_pci_resume(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Harvey Harrison441b62c2008-03-03 16:08:34 -0800814 dev_dbg(uhci_dev(uhci), "%s\n", __func__);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400815
Alan Stern687f5f32005-11-30 17:16:19 -0500816 /* Since we aren't in D3 any more, it's safe to set this flag
Alan Sterne323de42006-06-05 12:21:30 -0400817 * even if the controller was dead.
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100818 */
819 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern42245e62005-12-16 11:09:01 -0800820 mb();
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 spin_lock_irq(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Alan Sterna8bed8b2005-04-09 17:29:00 -0400824 /* FIXME: Disable non-PME# remote wakeup? */
825
Alan Sterne323de42006-06-05 12:21:30 -0400826 /* The firmware or a boot kernel may have changed the controller
827 * settings during a system wakeup. Check it and reconfigure
828 * to avoid problems.
Alan Sterna8bed8b2005-04-09 17:29:00 -0400829 */
830 check_and_reset_hc(uhci);
Alan Sterne323de42006-06-05 12:21:30 -0400831
832 /* If the controller was dead before, it's back alive now */
Alan Sterna8bed8b2005-04-09 17:29:00 -0400833 configure_hc(uhci);
834
Alan Stern1c50c312005-11-14 11:45:38 -0500835 if (uhci->rh_state == UHCI_RH_RESET) {
836
837 /* The controller had to be reset */
838 usb_root_hub_lost_power(hcd->self.root_hub);
Alan Sterna8bed8b2005-04-09 17:29:00 -0400839 suspend_rh(uhci, UHCI_RH_SUSPENDED);
Alan Stern1c50c312005-11-14 11:45:38 -0500840 }
Alan Sterna8bed8b2005-04-09 17:29:00 -0400841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 spin_unlock_irq(&uhci->lock);
Alan Stern6c1b4452005-04-21 16:04:58 -0400843
Alan Sternd8f12ab2008-04-22 10:49:15 -0400844 /* If interrupts don't work and remote wakeup is enabled then
845 * the suspended root hub needs to be polled.
846 */
847 if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup) {
Alan Stern1f09df82005-09-05 13:59:51 -0400848 hcd->poll_rh = 1;
Alan Stern6c1b4452005-04-21 16:04:58 -0400849 usb_hcd_poll_rh_status(hcd);
Alan Stern1f09df82005-09-05 13:59:51 -0400850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852}
853#endif
854
Alan Sterndccf4a42005-12-17 17:58:46 -0500855/* Wait until a particular device/endpoint's QH is idle, and free it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -0500857 struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Sterndccf4a42005-12-17 17:58:46 -0500860 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Alan Sterndccf4a42005-12-17 17:58:46 -0500862 spin_lock_irq(&uhci->lock);
863 qh = (struct uhci_qh *) hep->hcpriv;
864 if (qh == NULL)
865 goto done;
866
867 while (qh->state != QH_STATE_IDLE) {
868 ++uhci->num_waiting;
869 spin_unlock_irq(&uhci->lock);
870 wait_event_interruptible(uhci->waitqh,
871 qh->state == QH_STATE_IDLE);
872 spin_lock_irq(&uhci->lock);
873 --uhci->num_waiting;
874 }
875
876 uhci_free_qh(uhci, qh);
877done:
878 spin_unlock_irq(&uhci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
882{
883 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
Alan Sternc4334722006-05-19 16:34:57 -0400884 unsigned frame_number;
885 unsigned delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* Minimize latency by avoiding the spinlock */
Alan Sternc4334722006-05-19 16:34:57 -0400888 frame_number = uhci->frame_number;
889 barrier();
890 delta = (inw(uhci->io_addr + USBFRNUM) - frame_number) &
891 (UHCI_NUMFRAMES - 1);
892 return frame_number + delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895static const char hcd_name[] = "uhci_hcd";
896
897static const struct hc_driver uhci_driver = {
898 .description = hcd_name,
899 .product_desc = "UHCI Host Controller",
900 .hcd_priv_size = sizeof(struct uhci_hcd),
901
902 /* Generic hardware linkage */
903 .irq = uhci_irq,
904 .flags = HCD_USB11,
905
906 /* Basic lifecycle operations */
David Brownellbe3cbc52006-06-05 12:16:39 -0400907 .reset = uhci_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .start = uhci_start,
909#ifdef CONFIG_PM
Alan Stern7be7d742008-04-03 18:03:06 -0400910 .pci_suspend = uhci_pci_suspend,
911 .pci_resume = uhci_pci_resume,
Alan Stern0c0382e2005-10-13 17:08:02 -0400912 .bus_suspend = uhci_rh_suspend,
913 .bus_resume = uhci_rh_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif
915 .stop = uhci_stop,
916
917 .urb_enqueue = uhci_urb_enqueue,
918 .urb_dequeue = uhci_urb_dequeue,
919
920 .endpoint_disable = uhci_hcd_endpoint_disable,
921 .get_frame_number = uhci_hcd_get_frame_number,
922
923 .hub_status_data = uhci_hub_status_data,
924 .hub_control = uhci_hub_control,
925};
926
927static const struct pci_device_id uhci_pci_ids[] = { {
928 /* handle any USB UHCI controller */
Jean Delvarec67808e2006-04-09 20:07:35 +0200929 PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_UHCI, ~0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 .driver_data = (unsigned long) &uhci_driver,
931 }, { /* end: all zeroes */ }
932};
933
934MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
935
936static struct pci_driver uhci_pci_driver = {
937 .name = (char *)hcd_name,
938 .id_table = uhci_pci_ids,
939
940 .probe = usb_hcd_pci_probe,
941 .remove = usb_hcd_pci_remove,
Alan Stern02597d22005-04-28 14:51:27 -0400942 .shutdown = uhci_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944#ifdef CONFIG_PM
945 .suspend = usb_hcd_pci_suspend,
946 .resume = usb_hcd_pci_resume,
947#endif /* PM */
948};
949
950static int __init uhci_hcd_init(void)
951{
952 int retval = -ENOMEM;
953
Alan Stern5f8364b2006-12-05 16:29:55 -0500954 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION "%s\n",
955 ignore_oc ? ", overcurrent ignored" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (usb_disabled())
958 return -ENODEV;
959
Alan Stern8d402e12005-12-17 18:03:37 -0500960 if (DEBUG_CONFIGURED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
962 if (!errbuf)
963 goto errbuf_failed;
Alan Stern8d402e12005-12-17 18:03:37 -0500964 uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
965 if (!uhci_debugfs_root)
966 goto debug_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
Paul Mundt20c2df82007-07-20 10:11:58 +0900970 sizeof(struct urb_priv), 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!uhci_up_cachep)
972 goto up_failed;
973
974 retval = pci_register_driver(&uhci_pci_driver);
975 if (retval)
976 goto init_failed;
977
978 return 0;
979
980init_failed:
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700981 kmem_cache_destroy(uhci_up_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983up_failed:
984 debugfs_remove(uhci_debugfs_root);
985
986debug_failed:
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700987 kfree(errbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989errbuf_failed:
990
991 return retval;
992}
993
994static void __exit uhci_hcd_cleanup(void)
995{
996 pci_unregister_driver(&uhci_pci_driver);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700997 kmem_cache_destroy(uhci_up_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 debugfs_remove(uhci_debugfs_root);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700999 kfree(errbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002module_init(uhci_hcd_init);
1003module_exit(uhci_hcd_cleanup);
1004
1005MODULE_AUTHOR(DRIVER_AUTHOR);
1006MODULE_DESCRIPTION(DRIVER_DESC);
1007MODULE_LICENSE("GPL");