summaryrefslogtreecommitdiffstats
path: root/loader2/pcmcia.c
blob: ad844539997667e2627a269a83b454be3a717d9c (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * pcmcia.c - pcmcia 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.
 * Portions of this code from pcmcia-cs, copyright David A. Hinds
 * <dahinds@users.sourceforge.net>
 *
 * 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 <errno.h>
#include <fcntl.h>
#include <kudzu/kudzu.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

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

char * getPcicController() {
    struct device ** devices;
    static int probed = 0;
    static char * pcic = NULL;

    if (!probed) {
        probed = 1;
 
        devices = probeDevices(CLASS_SOCKET, BUS_PCI, 0);
        if (devices) {
            logMessage("found cardbus pci adapter");
            pcic = "yenta_socket";
        } else {
            devices = probeDevices(CLASS_SOCKET, BUS_MISC, 0);
            if (devices && strcmp (devices[0]->driver, "ignore") &&
                strcmp(devices[0]->driver, "unknown") && 
                strcmp(devices[0]->driver, "disabled")) {
                logMessage("found pcmcia adapter");
                pcic = strdup(devices[0]->driver);
            }
        }

        if (!pcic) {
            logMessage("no pcic controller found");
        }
        return pcic;
    } else {
        return pcic;
    }
}

int initializePcmciaController(moduleList modLoaded, moduleDeps modDeps,
                               moduleInfoSet modInfo, int flags) {
    char * pcic = NULL;
    char * mods;

    if (FL_NOPCMCIA(flags) || FL_TESTING(flags))
	return 0;

    pcic = getPcicController();
    if (!pcic)
        return 0;

    mods = sdupprintf("pcmcia_core:%s:ds", pcic);
    mlLoadModuleSet(mods, modLoaded, modDeps, modInfo, flags);

    return 0;
}



/* code from notting to activate pcmcia devices.  all kinds of wackiness */
static int pcmcia_major = 0;

static int lookup_dev(char *name) {
    FILE *f;
    int n;
    char s[32], t[32];
             
    f = fopen("/proc/devices", "r");
    if (f == NULL)
        return -errno;
    while (fgets(s, 32, f) != NULL) {
        if (sscanf(s, "%d %s", &n, t) == 2)
            if (strcmp(name, t) == 0)
                break;
    }
    fclose(f);
    if (strcmp(name, t) == 0)
        return n;
    else
        return -ENODEV;
}

static int open_sock(int sock) {
    int fd;
    char fn[64];
    dev_t dev = (pcmcia_major<<8) + sock;
        
    snprintf(fn, 64, "/tmp/pcmciadev-%d", getpid());
    if (mknod(fn, (S_IFCHR|0600), dev) == 0) {
        fd = open(fn, O_RDONLY);
        unlink(fn);
        if (fd >= 0)
            return fd;
    }
    return -1;
}

/* return whether or not we have pcmcia loaded */
int has_pcmcia(void) {
    if (pcmcia_major > 0)
        return pcmcia_major;
    pcmcia_major = lookup_dev("pcmcia");
    return pcmcia_major;
}

struct bind_info_t {
    char dev_info[32];
    unsigned char function;
    /* Not really a void *. Some convuluted structure that appears
     * to be NULL in cardmgr. */
    void *instance;
    char name[32];
    unsigned short major;
    unsigned short minor;
    void *next;
};


#define DS_BIND_REQUEST _IOWR('d', 60, struct bind_info_t)
#define DS_GET_DEVICE_INFO _IOWR('d', 61, struct bind_info_t)
int activate_pcmcia_device(struct pcmciaDevice *pdev) {
    int fd;
    struct bind_info_t * bind;
    int j, ret;

    if (has_pcmcia() <= 0) {
        logMessage("pcmcia not loaded, can't activate module");  
        return -1;
    }

    fd = open_sock(pdev->slot);
    if (fd < 0) {
        logMessage("unable to open slot");
        return -1;
    }

    bind = calloc(1, sizeof(struct bind_info_t));
    strcpy(bind->dev_info,pdev->driver);
    bind->function = pdev->function;
    if (ioctl(fd, DS_BIND_REQUEST, bind) == -1) {
        logMessage("failed to activate pcmcia device");
        return LOADER_ERROR;
    }

    for (ret = j = 0; j < 10; j++) {
        ret = ioctl(fd, DS_GET_DEVICE_INFO, bind);
        if ((ret == 0) || (errno != EAGAIN))
            break;
        usleep(100000);
    }

    if (j >= 10) {
        logMessage("activated, but unable to get device info");
        return LOADER_ERROR;
    }
    
    return LOADER_OK;
}

void startPcmciaDevices(moduleList modLoaded, int flags) {
    struct device ** devices;
    int i;

    /* no pcmcia, don't try to start the devices */
    if (has_pcmcia() <= 0)
        return;

    devices = probeDevices(CLASS_UNSPEC, BUS_PCMCIA, PROBE_LOADED);
    if (!devices) {
        logMessage("no devices to activate");
        return;
    }

    for (i = 0; devices[i]; i++) {
        if (devices[i]->bus != BUS_PCMCIA)
            continue;
        if (!(strcmp (devices[i]->driver, "ignore") && 
              strcmp (devices[i]->driver, "unknown") &&
              strcmp (devices[i]->driver, "disabled"))) 
            continue;
        if (!mlModuleInList(devices[i]->driver, modLoaded))
            continue;
        
        logMessage("going to activate device using %s", devices[i]->driver);
        activate_pcmcia_device((struct pcmciaDevice *)devices[i]);
    }
}