/
driver.c
397 lines (308 loc) · 8.1 KB
/
driver.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright 2007-2010, Axel Dörfler. All rights reserved.
* Copyright 2009-2022, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "device.h"
#include "sysinit.h"
#include <stdlib.h>
#include <sys/sockio.h>
#include <Drivers.h>
#include <ether_driver.h>
#include <compat/sys/haiku-module.h>
#include <compat/sys/bus.h>
#include <compat/sys/mbuf.h>
#include <compat/net/ethernet.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usb_device.h>
//#define TRACE_DRIVER
#ifdef TRACE_DRIVER
# define TRACE(x) dprintf x
#else
# define TRACE(x)
#endif
static struct {
driver_t* driver;
int bus;
struct pci_info pci_info;
struct freebsd_usb_device usb_dev;
struct usb_attach_arg uaa;
} sProbedDevices[MAX_DEVICES];
const char* gDeviceNameList[MAX_DEVICES + 1];
struct ifnet* gDevices[MAX_DEVICES];
int32 gDeviceCount;
static status_t
init_root_device(device_t *_root, int bus_type)
{
static driver_t sRootDriverPCI = {
"pci",
NULL,
sizeof(struct root_device_softc),
};
static driver_t sRootDriverUSB = {
"uhub",
NULL,
sizeof(struct root_device_softc),
};
device_t root = device_add_child(NULL, NULL, 0);
if (root == NULL)
return B_NO_MEMORY;
root->softc = malloc(sizeof(struct root_device_softc));
if (root->softc == NULL) {
device_delete_child(NULL, root);
return B_NO_MEMORY;
}
bzero(root->softc, sizeof(struct root_device_softc));
if (bus_type == BUS_pci)
root->driver = &sRootDriverPCI;
else if (bus_type == BUS_uhub)
root->driver = &sRootDriverUSB;
else
panic("unknown bus type");
((struct root_device_softc*)root->softc)->bus = bus_type;
root->root = root;
if (_root != NULL)
*_root = root;
return B_OK;
}
static status_t
add_child_device(driver_t* driver, device_t root, device_t* _child)
{
device_t child = device_add_child_driver(root, driver->name, driver, 0);
if (child == NULL)
return B_ERROR;
if (_child != NULL)
*_child = child;
return B_OK;
}
static void
uninit_probed_devices()
{
for (int p = 0; sProbedDevices[p].bus != BUS_INVALID; p++) {
if (sProbedDevices[p].bus == BUS_pci) {
gPci->unreserve_device(sProbedDevices[p].pci_info.bus,
sProbedDevices[p].pci_info.device, sProbedDevices[p].pci_info.function,
gDriverName, NULL);
} else if (sProbedDevices->bus == BUS_uhub) {
usb_cleanup_device(&sProbedDevices[p].usb_dev);
}
}
}
// #pragma mark - Haiku Driver API
static status_t
init_hardware_pci(driver_t* drivers[])
{
status_t status;
int i = 0;
pci_info* info;
device_t root;
int p = 0;
while (sProbedDevices[p].bus != BUS_INVALID)
p++;
status = init_pci();
if (status != B_OK)
return status;
status = init_root_device(&root, BUS_pci);
if (status != B_OK)
return status;
bool found = false;
for (info = get_device_pci_info(root); gPci->get_nth_pci_info(i, info) == B_OK; i++) {
int best = 0;
driver_t* driver = NULL;
for (int index = 0; drivers[index] != NULL; index++) {
int result;
device_t device = NULL;
status = add_child_device(drivers[index], root, &device);
if (status < B_OK)
break;
result = device->methods.probe(device);
if (result >= 0 && (driver == NULL || result > best)) {
TRACE(("%s, found %s at %d (%d)\n", gDriverName,
device_get_desc(device), i, result));
driver = drivers[index];
best = result;
}
device_delete_child(root, device);
}
if (driver == NULL)
continue;
// We've found a driver; now try to reserve the device and store it
if (gPci->reserve_device(info->bus, info->device, info->function,
gDriverName, NULL) != B_OK) {
dprintf("%s: Failed to reserve PCI:%d:%d:%d\n",
gDriverName, info->bus, info->device, info->function);
continue;
}
sProbedDevices[p].bus = BUS_pci;
sProbedDevices[p].driver = driver;
sProbedDevices[p].pci_info = *info;
found = true;
p++;
}
sProbedDevices[p].bus = BUS_INVALID;
device_delete_child(NULL, root);
if (found)
return B_OK;
uninit_pci();
return B_NOT_SUPPORTED;
}
static status_t
init_hardware_uhub(driver_t* drivers[])
{
status_t status;
device_t root;
int p = 0;
while (sProbedDevices[p].bus != BUS_INVALID)
p++;
status = init_usb();
if (status != B_OK)
return status;
status = init_root_device(&root, BUS_uhub);
if (status != B_OK)
return status;
bool found = false;
uint32 cookie = 0;
struct freebsd_usb_device udev = {};
while ((status = get_next_usb_device(&cookie, &udev)) == B_OK) {
int best = 0;
driver_t* driver = NULL;
struct usb_attach_arg uaa;
status = get_usb_device_attach_arg(&udev, &uaa);
if (status != B_OK)
continue;
for (int index = 0; drivers[index] != NULL; index++) {
int result;
device_t device = NULL;
status = add_child_device(drivers[index], root, &device);
if (status < B_OK)
break;
device_set_ivars(device, &uaa);
result = device->methods.probe(device);
if (result >= 0 && (driver == NULL || result > best)) {
TRACE(("%s, found %s at %d (%d)\n", gDriverName,
device_get_desc(device), i, result));
driver = drivers[index];
best = result;
}
device_delete_child(root, device);
}
if (driver == NULL)
continue;
sProbedDevices[p].bus = BUS_uhub;
sProbedDevices[p].driver = driver;
sProbedDevices[p].usb_dev = udev;
sProbedDevices[p].uaa = uaa;
sProbedDevices[p].uaa.device = &sProbedDevices[p].usb_dev;
// We just "transferred ownership" of usb_dev to sProbedDevices.
memset(&udev, 0, sizeof(udev));
found = true;
p++;
}
sProbedDevices[p].bus = BUS_INVALID;
device_delete_child(NULL, root);
usb_cleanup_device(&udev);
if (found)
return B_OK;
uninit_usb();
return B_NOT_SUPPORTED;
}
status_t
_fbsd_init_hardware(driver_t* pci_drivers[], driver_t* uhub_drivers[])
{
sProbedDevices[0].bus = BUS_INVALID;
if (pci_drivers != NULL)
init_hardware_pci(pci_drivers);
if (uhub_drivers != NULL)
init_hardware_uhub(uhub_drivers);
return (sProbedDevices[0].bus != BUS_INVALID) ? B_OK : B_NOT_SUPPORTED;
}
status_t
_fbsd_init_drivers()
{
status_t status = init_mutexes();
if (status < B_OK)
goto err2;
status = init_mbufs();
if (status < B_OK)
goto err3;
status = init_callout();
if (status < B_OK)
goto err4;
if (HAIKU_DRIVER_REQUIRES(FBSD_TASKQUEUES)) {
status = init_taskqueues();
if (status < B_OK)
goto err5;
}
init_sysinit();
status = init_wlan_stack();
if (status < B_OK)
goto err6;
// Always hold the giant lock during attach.
mtx_lock(&Giant);
for (int p = 0; sProbedDevices[p].bus != BUS_INVALID; p++) {
device_t root, device = NULL;
status = init_root_device(&root, sProbedDevices[p].bus);
if (status != B_OK)
break;
if (sProbedDevices[p].bus == BUS_pci) {
pci_info* info = get_device_pci_info(root);
*info = sProbedDevices[p].pci_info;
} else if (sProbedDevices[p].bus == BUS_uhub) {
struct root_device_softc* root_softc = (struct root_device_softc*)root->softc;
root_softc->usb_dev = &sProbedDevices[p].usb_dev;
}
status = add_child_device(sProbedDevices[p].driver, root, &device);
if (status != B_OK)
break;
if (sProbedDevices[p].bus == BUS_uhub)
device_set_ivars(device, &sProbedDevices[p].uaa);
// some drivers expect probe() to be called before attach()
// (i.e. they set driver softc in probe(), etc.)
if (device->methods.probe(device) >= 0
&& device_attach(device) == 0) {
dprintf("%s: init_driver(%p)\n", gDriverName,
sProbedDevices[p].driver);
} else
device_delete_child(NULL, root);
}
mtx_unlock(&Giant);
if (gDeviceCount > 0)
return B_OK;
if (status == B_OK)
status = B_ERROR;
err7:
uninit_wlan_stack();
err6:
uninit_sysinit();
if (HAIKU_DRIVER_REQUIRES(FBSD_TASKQUEUES))
uninit_taskqueues();
err5:
uninit_callout();
err4:
uninit_mbufs();
err3:
uninit_mutexes();
err2:
uninit_probed_devices();
uninit_usb();
uninit_pci();
return status;
}
status_t
_fbsd_uninit_drivers()
{
for (int i = 0; i < gDeviceCount; i++)
device_delete_child(NULL, gDevices[i]->root_device);
uninit_wlan_stack();
uninit_sysinit();
if (HAIKU_DRIVER_REQUIRES(FBSD_TASKQUEUES))
uninit_taskqueues();
uninit_callout();
uninit_mbufs();
uninit_mutexes();
uninit_probed_devices();
uninit_usb();
uninit_pci();
return B_OK;
}