summaryrefslogtreecommitdiffstats
path: root/loader2/usb.c
blob: bf2fb73089b4014a645681d3b6ebf8f590d94f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * usb.c - usb probing/module loading functionality
 *
 * Erik Troan <ewt@redhat.com>
 * Matt Wilson <msw@redhat.com>
 * Michael Fulbright <msf@redhat.com>
 * Jeremy Katz <katzj@redhat.com>
 *
 * Copyright 1999 - 2003 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * General Public License.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
#include <kudzu/kudzu.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "loader.h"
#include "log.h"
#include "modules.h"
#include "moduledeps.h"

#include "../isys/imount.h"

/* boot flags */
extern int flags;

/* This forces a pause between initializing usb and trusting the /proc 
   stuff */
void sleepUntilUsbIsStable(void) {
    struct stat sb;
    time_t last = 0;
    int i, count = 0;

    /* sleep for a maximum of 20 seconds, minimum of 2 seconds */
    logMessage(DEBUGLVL, "waiting for usb to become stable...");
    for (i = 0; i < 20; i++) {
	stat("/proc/bus/usb/devices", &sb);
	if (last == sb.st_mtime) {
	    count++;
	    /* if we get the same mtime twice in a row, should be
	       good enough to use now */
	    if (count > 1)
		break;
	} else {
	    /* if we didn't match mtimes, reset the stability counter */
	    count = 0;
	}
	last = sb.st_mtime;
	sleep(1);
    }
    logMessage(DEBUGLVL, "%d seconds.", i);
}

int usbInitialize(moduleList modLoaded, moduleDeps modDeps,
			 moduleInfoSet modInfo) {
    struct device ** devices;
    char * buf;
    int i;
    char * loadUsbStorage = NULL;

    if (FL_NOUSB(flags)) return 0;

    logMessage(INFO, "looking for usb controllers");

    devices = probeDevices(CLASS_USB, BUS_PCI, 0);

    if (!devices) {
        logMessage(DEBUGLVL, "no usb controller found");
        return 0;
    }

    /* JKFIXME: if we looked for all of them, we could batch this up and it
     * would be faster */
    for (i=0; devices[i]; i++) {
        if (!devices[i]->driver) continue;
        logMessage(INFO, "found USB controller %s", devices[i]->driver);

        if (mlLoadModuleSet(devices[i]->driver, modLoaded, modDeps, modInfo)) {
            /* dont return, just keep going. */
            /* may have USB built into kernel */
            /* return 1; */
        }

    }
    free(devices);


    if (FL_TESTING(flags)) return 0;

    if (doPwMount("/proc/bus/usb", "/proc/bus/usb", "usbfs", 0, NULL))
	logMessage(ERROR, "failed to mount device usbfs: %s", strerror(errno));

    /* sleep so we make sure usb devices get properly enumerated.
       that way we should block when initializing each usb driver until
       the device is ready for use */
    sleepUntilUsbIsStable();

    if (!FL_NOUSBSTORAGE(flags)) {
        devices = probeDevices(CLASS_HD | CLASS_FLOPPY | CLASS_CDROM, 
                               BUS_USB, PROBE_ALL);
        if (devices) {
            if (FL_UB(flags))
                loadUsbStorage = ":ub";
            else 
                loadUsbStorage = ":usb-storage";
            free(devices);
        }
    }

    buf = alloca(40);
    sprintf(buf, "hid:keybdev%s", (loadUsbStorage ? loadUsbStorage : ""));
    mlLoadModuleSet(buf, modLoaded, modDeps, modInfo);
    sleep(1);

    return 0;
}

void usbInitializeMouse(moduleList modLoaded, moduleDeps modDeps,
                        moduleInfoSet modInfo) {
    if (FL_NOUSB(flags)) return;

    logMessage(INFO, "looking for USB mouse...");
    if (probeDevices(CLASS_MOUSE, BUS_USB, PROBE_ALL)) {
        logMessage(INFO, "USB mouse found, loading mousedev module");
        if (mlLoadModuleSet("mousedev", modLoaded, modDeps, modInfo)) {
            logMessage (DEBUGLVL, "failed to loading mousedev module");
            return;
        }
    }
}