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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Framework for USB devices.
* Some of them may be embedded into usbd and some of
* them may exist as /bin/usb/* binaries on their own.
*
* When embedded, devmain() is given a ref of an already
* configured and open Dev. If devmain()
* does not fail it should release this ref when done and
* use incref to add further refs to it.
*/
#include <u.h>
#include <libc.h>
#include <thread.h>
#include "usb.h"
#include "usbd.h"
static Lock masklck;
extern Devtab devtab[];
static char* cputype;
int
getdevnb(uvlong *maskp)
{
int i;
lock(&masklck);
for(i = 0; i < 8 * sizeof *maskp; i++)
if((*maskp & (1ULL<<i)) == 0){
*maskp |= 1ULL<<i;
unlock(&masklck);
return i;
}
unlock(&masklck);
return -1;
}
void
putdevnb(uvlong *maskp, int id)
{
lock(&masklck);
if(id >= 0)
*maskp &= ~(1ULL<<id);
unlock(&masklck);
}
static int
cspmatch(Devtab *dt, int dcsp)
{
int i;
int csp;
for(i = 0; i < nelem(dt->csps); i++)
if((csp=dt->csps[i]) != 0)
if(csp == dcsp)
return 1;
else if((csp&DCL) && (csp&~DCL) == Class(dcsp))
return 1;
return 0;
}
static int
devmatch(Devtab *dt, Usbdev *d)
{
int i;
int c;
Conf *cp;
if(dt->vid != -1 && d->vid != dt->vid)
return 0;
if(dt->did != -1 && d->did != dt->did)
return 0;
if(cspmatch(dt, d->csp))
return 1;
for(c = 0; c < Nconf; c++)
if((cp=d->conf[c]) != nil)
for(i = 0; i < Niface; i++)
if(cp->iface[i] != nil)
if(cspmatch(dt, cp->iface[i]->csp))
return 1;
return 0;
}
/* We can't use procexec to execute drivers, because
* procexec mounts #| at /mnt/temp and we do *not*
* have /mnt/temp at boot time.
* Instead, we use access to guess if we can execute the file.
* and reply as procexec. Be careful that the child inherits
* all the shared state of the thread library. It should run unnoticed.
*/
static void
xexec(Channel *c, char *nm, char *args[])
{
int pid;
if(access(nm, AEXEC) == 0){
pid = rfork(RFFDG|RFREND|RFPROC);
switch(pid){
case 0:
exec(nm, args);
_exits("exec");
case -1:
break;
default:
sendul(c, pid);
threadexits(nil);
}
}
}
typedef struct Sarg Sarg;
struct Sarg{
Port *pp;
Devtab* dt;
Channel*rc;
char fname[80];
char args[128];
char *argv[40];
};
static void
startdevproc(void *a)
{
Sarg *sa = a;
Dev *d;
Devtab *dt;
int argc;
char *args, *argse, **argv;
char *fname;
threadsetgrp(threadid());
d = sa->pp->dev;
dt = sa->dt;
args = sa->args;
argse = sa->args + sizeof sa->args;
argv = sa->argv;
fname = sa->fname;
sa->pp->devmaskp = &dt->devmask;
sa->pp->devnb = getdevnb(&dt->devmask);
if(sa->pp->devnb < 0){
sa->pp->devmaskp = nil;
sa->pp->devnb = 0;
}else
args = seprint(args, argse, "-N %d", sa->pp->devnb);
if(dt->args != nil)
seprint(args, argse, " %s", dt->args);
args = sa->args;
dprint(2, "%s: start: %s %s\n", argv0, dt->name, args);
argv[0] = dt->name;
argc = 1;
if(args[0] != 0)
argc += tokenize(args, argv+1, nelem(sa->argv)-2);
argv[argc] = nil;
if(dt->init == nil){
if(d->dfd > 0 ){
close(d->dfd);
d->dfd = -1;
}
rfork(RFCFDG);
open("/dev/null", OREAD);
open("/dev/cons", OWRITE);
open("/dev/cons", OWRITE);
xexec(sa->rc, argv[0], argv);
snprint(fname, sizeof(sa->fname), "/bin/usb/%s", dt->name);
xexec(sa->rc, fname, argv);
snprint(fname, sizeof(sa->fname), "/boot/%s", dt->name);
xexec(sa->rc, fname, argv);
if(cputype == nil)
cputype = getenv("cputype");
if(cputype != nil){
snprint(fname, sizeof(sa->fname), "/%s/bin/%s",
cputype, dt->name);
argv[0] = fname;
xexec(sa->rc, fname, argv);
}
fprint(2, "%s: %s: not found. can't exec\n", argv0, dt->name);
sendul(sa->rc, -1);
threadexits("exec");
}else{
sa->pp->dev = opendev(d->dir);
sendul(sa->rc, 0);
if(dt->init(d, argc, argv) < 0)
fprint(2, "%s: %s: %r\n", argv0, dt->name);
closedev(d);
free(sa);
}
threadexits(nil);
}
static void
writeinfo(Dev *d)
{
char buf[128];
char *s;
char *se;
Usbdev *ud;
Conf *c;
Iface *ifc;
int i, j;
ud = d->usb;
s = buf;
se = buf+sizeof(buf);
s = seprint(s, se, "info %s csp %#08ulx", classname(ud->class), ud->csp);
for(i = 0; i < ud->nconf; i++){
c = ud->conf[i];
if(c == nil)
break;
for(j = 0; j < nelem(c->iface); j++){
ifc = c->iface[j];
if(ifc == nil)
break;
if(ifc->csp != ud->csp)
s = seprint(s, se, " csp %#08ulx", ifc->csp);
}
}
s = seprint(s, se, " vid %06#x did %06#x", ud->vid, ud->did);
seprint(s, se, " %q %q", ud->vendor, ud->product);
devctl(d, "%s", buf);
}
int
startdev(Port *pp)
{
Dev *d;
Usbdev *ud;
Devtab *dt;
Sarg *sa;
Channel *rc;
d = pp->dev;
assert(d);
ud = d->usb;
assert(ud != nil);
writeinfo(d);
if(ud->class == Clhub){
/*
* Hubs are handled directly by this process avoiding
* concurrent operation so that at most one device
* has the config address in use.
* We cancel kernel debug for these eps. too chatty.
*/
pp->hub = newhub(d->dir, d);
if(pp->hub == nil)
fprint(2, "%s: %s: %r\n", argv0, d->dir);
else
fprint(2, "usb/hub... ");
if(usbdebug > 1)
devctl(d, "debug 0"); /* polled hubs are chatty */
return pp->hub == nil ? -1 : 0;
}
for(dt = devtab; dt->name != nil; dt++)
if(devmatch(dt, ud))
break;
/*
* From here on the device is for the driver.
* When we return pp->dev contains a Dev just for us
* with only the ctl open. Both devs are released on the last closedev:
* driver's upon I/O errors and ours upon port dettach.
*/
if(dt->name == nil){
dprint(2, "%s: no configured entry for %s (csp %#08lx)\n",
argv0, d->dir, ud->csp);
close(d->dfd);
d->dfd = -1;
return 0;
}
sa = emallocz(sizeof(Sarg), 1);
sa->pp = pp;
sa->dt = dt;
rc = sa->rc = chancreate(sizeof(ulong), 1);
procrfork(startdevproc, sa, Stack, RFNOTEG);
if(recvul(rc) != 0)
free(sa);
chanfree(rc);
fprint(2, "usb/%s... ", dt->name);
sleep(Spawndelay); /* in case we re-spawn too fast */
return 0;
}
|