3 * Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <bsd_global.h>
29 struct usb_process usb_process[USB_PROC_MAX];
31 static device_t usb_pci_root;
33 int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev,
34 int type, int *rid, unsigned int flags);
35 int (*ofw_bus_status_ok_cb)(device_t dev);
36 int (*ofw_bus_is_compatible_cb)(device_t dev, char *name);
38 /*------------------------------------------------------------------------*
39 * Implementation of busdma API
40 *------------------------------------------------------------------------*/
42 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
43 bus_size_t boundary, bus_addr_t lowaddr,
44 bus_addr_t highaddr, bus_dma_filter_t *filter,
45 void *filterarg, bus_size_t maxsize, int nsegments,
46 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
47 void *lockfuncarg, bus_dma_tag_t *dmat)
49 struct bus_dma_tag *ret;
51 ret = malloc(sizeof(struct bus_dma_tag), XXX, XXX);
54 ret->alignment = alignment;
55 ret->maxsize = maxsize;
63 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
68 addr = malloc(dmat->maxsize + dmat->alignment, XXX, XXX);
73 addr = (void*)(((uintptr_t)addr + dmat->alignment - 1) & ~(dmat->alignment - 1));
80 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
81 bus_size_t buflen, bus_dmamap_callback_t *callback,
82 void *callback_arg, int flags)
84 bus_dma_segment_t segs[1];
86 segs[0].ds_addr = (uintptr_t)buf;
87 segs[0].ds_len = buflen;
89 (*callback)(callback_arg, segs, 1, 0);
95 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
102 bus_dma_tag_destroy(bus_dma_tag_t dmat)
109 /*------------------------------------------------------------------------*
110 * Implementation of resource management API
111 *------------------------------------------------------------------------*/
114 bus_alloc_resource_any(device_t dev, int type, int *rid, unsigned int flags)
116 struct resource *res;
119 res = malloc(sizeof(*res), XXX, XXX);
123 res->__r_i = malloc(sizeof(struct resource_i), XXX, XXX);
124 if (res->__r_i == NULL) {
129 if (bus_alloc_resource_any_cb != NULL)
130 ret = (*bus_alloc_resource_any_cb)(res, dev, type, rid, flags);
134 free(res->__r_i, XXX);
140 bus_alloc_resources(device_t dev, struct resource_spec *rs,
141 struct resource **res)
145 for (i = 0; rs[i].type != -1; i++)
147 for (i = 0; rs[i].type != -1; i++) {
148 res[i] = bus_alloc_resource_any(dev,
149 rs[i].type, &rs[i].rid, rs[i].flags);
150 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
151 bus_release_resources(dev, rs, res);
159 bus_release_resources(device_t dev, const struct resource_spec *rs,
160 struct resource **res)
164 for (i = 0; rs[i].type != -1; i++)
165 if (res[i] != NULL) {
166 bus_release_resource(
167 dev, rs[i].type, rs[i].rid, res[i]);
173 bus_setup_intr(device_t dev, struct resource *r, int flags,
174 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
177 dev->dev_irq_filter = filter;
178 dev->dev_irq_fn = handler;
179 dev->dev_irq_arg = arg;
185 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
188 dev->dev_irq_filter = NULL;
189 dev->dev_irq_fn = NULL;
190 dev->dev_irq_arg = NULL;
196 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
198 /* Resource releasing is not supported */
203 bus_generic_attach(device_t dev)
207 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
208 device_probe_and_attach(child);
215 rman_get_bustag(struct resource *r)
218 return (r->r_bustag);
222 rman_get_bushandle(struct resource *r)
225 return (r->r_bushandle);
229 rman_get_size(struct resource *r)
232 return (r->__r_i->r_end - r->__r_i->r_start + 1);
236 ofw_bus_status_okay(device_t dev)
238 if (ofw_bus_status_ok_cb == NULL)
241 return ((*ofw_bus_status_ok_cb)(dev));
245 ofw_bus_is_compatible(device_t dev, char *name)
247 if (ofw_bus_is_compatible_cb == NULL)
250 return ((*ofw_bus_is_compatible_cb)(dev, name));
253 /*------------------------------------------------------------------------*
254 * Implementation of mutex API
255 *------------------------------------------------------------------------*/
260 mtx_system_init(void *arg)
262 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
264 SYSINIT(mtx_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, mtx_system_init, NULL);
267 mtx_init(struct mtx *mtx, const char *name, const char *type, int opt)
274 mtx_lock(struct mtx *mtx)
281 mtx_unlock(struct mtx *mtx)
288 mtx_owned(struct mtx *mtx)
291 return (mtx->owned != 0);
295 mtx_destroy(struct mtx *mtx)
300 /*------------------------------------------------------------------------*
301 * Implementation of shared/exclusive mutex API
302 *------------------------------------------------------------------------*/
305 sx_init_flags(struct sx *sx, const char *name, int flags)
311 sx_destroy(struct sx *sx)
317 sx_xlock(struct sx *sx)
323 sx_xunlock(struct sx *sx)
329 sx_xlocked(struct sx *sx)
331 return (sx->owned != 0);
334 /*------------------------------------------------------------------------*
335 * Implementaiton of condition variable API
336 *------------------------------------------------------------------------*/
339 cv_init(struct cv *cv, const char *desc)
345 cv_destroy(struct cv *cv)
351 cv_wait(struct cv *cv, struct mtx *mtx)
353 cv_timedwait(cv, mtx, -1);
357 cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
364 return (EWOULDBLOCK); /* not allowed */
368 while (cv->sleeping) {
370 delta = ticks - start;
371 if (delta >= timo || delta < 0)
378 if (++time >= (1000000 / hz)) {
391 return (EWOULDBLOCK); /* not allowed */
397 cv_signal(struct cv *cv)
403 cv_broadcast(struct cv *cv)
408 /*------------------------------------------------------------------------*
409 * Implementation of callout API
410 *------------------------------------------------------------------------*/
412 static void callout_proc_msg(struct usb_proc_msg *);
414 volatile int ticks = 0;
416 static LIST_HEAD(, callout) head_callout = LIST_HEAD_INITIALIZER(&head_callout);
418 static struct mtx mtx_callout;
419 static struct usb_proc_msg callout_msg[2];
422 callout_system_init(void *arg)
424 mtx_init(&mtx_callout, "callout-mtx", NULL, MTX_DEF | MTX_RECURSE);
426 callout_msg[0].pm_callback = &callout_proc_msg;
427 callout_msg[1].pm_callback = &callout_proc_msg;
429 SYSINIT(callout_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, callout_system_init, NULL);
432 callout_callback(struct callout *c)
436 mtx_lock(&mtx_callout);
437 if (c->entry.le_prev != NULL) {
438 LIST_REMOVE(c, entry);
439 c->entry.le_prev = NULL;
441 mtx_unlock(&mtx_callout);
443 if (c->c_func != NULL)
444 (c->c_func) (c->c_arg);
446 if (!(c->flags & CALLOUT_RETURNUNLOCKED))
451 callout_process(int timeout)
454 usb_proc_msignal(usb_process + 2, &callout_msg[0], &callout_msg[1]);
458 callout_proc_msg(struct usb_proc_msg *pmsg)
464 mtx_lock(&mtx_callout);
466 LIST_FOREACH(c, &head_callout, entry) {
468 delta = c->timeout - ticks;
470 mtx_unlock(&mtx_callout);
477 mtx_unlock(&mtx_callout);
481 callout_init_mtx(struct callout *c, struct mtx *mtx, int flags)
483 memset(c, 0, sizeof(*c));
489 c->flags = (flags & CALLOUT_RETURNUNLOCKED);
493 callout_reset(struct callout *c, int to_ticks,
494 void (*func) (void *), void *arg)
500 c->timeout = ticks + to_ticks;
502 mtx_lock(&mtx_callout);
503 LIST_INSERT_HEAD(&head_callout, c, entry);
504 mtx_unlock(&mtx_callout);
508 callout_stop(struct callout *c)
510 mtx_lock(&mtx_callout);
512 if (c->entry.le_prev != NULL) {
513 LIST_REMOVE(c, entry);
514 c->entry.le_prev = NULL;
516 mtx_unlock(&mtx_callout);
523 callout_drain(struct callout *c)
526 return; /* not initialised */
534 callout_pending(struct callout *c)
538 mtx_lock(&mtx_callout);
539 retval = (c->entry.le_prev != NULL);
540 mtx_unlock(&mtx_callout);
545 /*------------------------------------------------------------------------*
546 * Implementation of device API
547 *------------------------------------------------------------------------*/
549 static const char unknown_string[] = { "unknown" };
551 static TAILQ_HEAD(, module_data) module_head =
552 TAILQ_HEAD_INITIALIZER(module_head);
555 devclass_equal(const char *a, const char *b)
576 bus_generic_resume(device_t dev)
582 bus_generic_shutdown(device_t dev)
588 bus_generic_suspend(device_t dev)
594 bus_generic_print_child(device_t dev, device_t child)
600 bus_generic_driver_added(device_t dev, driver_t *driver)
606 device_get_parent(device_t dev)
608 return (dev ? dev->dev_parent : NULL);
612 device_set_interrupt(device_t dev, driver_filter_t *filter,
613 driver_intr_t *fn, void *arg)
615 dev->dev_irq_filter = filter;
616 dev->dev_irq_fn = fn;
617 dev->dev_irq_arg = arg;
621 device_run_interrupts(device_t parent)
628 TAILQ_FOREACH(child, &parent->dev_children, dev_link) {
630 if (child->dev_irq_filter != NULL)
631 status = child->dev_irq_filter(child->dev_irq_arg);
633 status = FILTER_SCHEDULE_THREAD;
635 if (status == FILTER_SCHEDULE_THREAD) {
636 if (child->dev_irq_fn != NULL)
637 (child->dev_irq_fn) (child->dev_irq_arg);
643 device_set_ivars(device_t dev, void *ivars)
645 dev->dev_aux = ivars;
649 device_get_ivars(device_t dev)
651 return (dev ? dev->dev_aux : NULL);
655 device_get_unit(device_t dev)
657 return (dev ? dev->dev_unit : 0);
661 bus_generic_detach(device_t dev)
666 if (!dev->dev_attached)
669 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
670 if ((error = device_detach(child)) != 0)
677 device_get_nameunit(device_t dev)
679 if (dev && dev->dev_nameunit[0])
680 return (dev->dev_nameunit);
682 return (unknown_string);
686 devclass_create(devclass_t *dc_pp)
691 if (dc_pp[0] == NULL) {
692 dc_pp[0] = malloc(sizeof(**(dc_pp)),
693 M_DEVBUF, M_WAITOK | M_ZERO);
695 if (dc_pp[0] == NULL) {
702 static const struct module_data *
703 devclass_find_create(const char *classname)
705 const struct module_data *mod;
707 TAILQ_FOREACH(mod, &module_head, entry) {
708 if (devclass_equal(mod->mod_name, classname)) {
709 if (devclass_create(mod->devclass_pp)) {
719 devclass_add_device(const struct module_data *mod, device_t dev)
725 pp_dev = mod->devclass_pp[0]->dev_list;
726 end = pp_dev + DEVCLASS_MAXUNIT;
729 while (pp_dev != end) {
730 if (*pp_dev == NULL) {
732 dev->dev_unit = unit;
733 dev->dev_module = mod;
734 snprintf(dev->dev_nameunit,
735 sizeof(dev->dev_nameunit),
736 "%s%d", device_get_name(dev), unit);
742 DPRINTF("Could not add device to devclass.\n");
747 devclass_delete_device(const struct module_data *mod, device_t dev)
752 mod->devclass_pp[0]->dev_list[dev->dev_unit] = NULL;
753 dev->dev_module = NULL;
757 make_device(device_t parent, const char *name)
760 const struct module_data *mod = NULL;
764 mod = devclass_find_create(name);
768 DPRINTF("%s:%d:%s: can't find device "
769 "class %s\n", __FILE__, __LINE__,
775 dev = malloc(sizeof(*dev),
776 M_DEVBUF, M_WAITOK | M_ZERO);
781 dev->dev_parent = parent;
782 TAILQ_INIT(&dev->dev_children);
785 dev->dev_fixed_class = 1;
786 if (devclass_add_device(mod, dev)) {
801 device_add_child(device_t dev, const char *name, int unit)
806 device_printf(dev, "Unit is not -1\n");
808 child = make_device(dev, name);
810 device_printf(dev, "Could not add child '%s'\n", name);
817 TAILQ_INSERT_TAIL(&dev->dev_children, child, dev_link);
823 device_delete_child(device_t dev, device_t child)
828 /* detach parent before deleting children, if any */
829 error = device_detach(child);
833 /* remove children second */
834 while ((grandchild = TAILQ_FIRST(&child->dev_children))) {
835 error = device_delete_child(child, grandchild);
837 device_printf(dev, "Error deleting child!\n");
842 devclass_delete_device(child->dev_module, child);
845 /* remove child from parent */
846 TAILQ_REMOVE(&dev->dev_children, child, dev_link);
848 free(child, M_DEVBUF);
855 device_delete_children(device_t dev)
860 while ((child = TAILQ_FIRST(&dev->dev_children))) {
861 error = device_delete_child(dev, child);
863 device_printf(dev, "Error deleting child!\n");
871 device_quiet(device_t dev)
877 device_get_desc(device_t dev)
880 return &(dev->dev_desc[0]);
881 return (unknown_string);
888 DPRINTF("Default method called\n");
893 device_get_method(device_t dev, const char *what)
895 const struct device_method *mtod;
897 mtod = dev->dev_module->driver->methods;
898 while (mtod->func != NULL) {
899 if (devclass_equal(mtod->desc, what)) {
904 return ((void *)&default_method);
908 device_get_name(device_t dev)
911 return (unknown_string);
913 return (dev->dev_module->driver->name);
917 device_allocate_softc(device_t dev)
919 const struct module_data *mod;
921 mod = dev->dev_module;
923 if ((dev->dev_softc_alloc == 0) &&
924 (mod->driver->size != 0)) {
925 dev->dev_sc = malloc(mod->driver->size,
926 M_DEVBUF, M_WAITOK | M_ZERO);
928 if (dev->dev_sc == NULL)
931 dev->dev_softc_alloc = 1;
937 device_probe_and_attach(device_t dev)
939 const struct module_data *mod;
940 const char *bus_name_parent;
942 bus_name_parent = device_get_name(device_get_parent(dev));
944 if (dev->dev_attached)
945 return (0); /* fail-safe */
947 if (dev->dev_fixed_class) {
949 mod = dev->dev_module;
951 if (DEVICE_PROBE(dev) <= 0) {
953 if (device_allocate_softc(dev) == 0) {
955 if (DEVICE_ATTACH(dev) == 0) {
957 dev->dev_attached = 1;
967 * Else find a module for our device, if any
970 TAILQ_FOREACH(mod, &module_head, entry) {
971 if (devclass_equal(mod->bus_name, bus_name_parent)) {
972 if (devclass_create(mod->devclass_pp)) {
975 if (devclass_add_device(mod, dev)) {
978 if (DEVICE_PROBE(dev) <= 0) {
980 if (device_allocate_softc(dev) == 0) {
982 if (DEVICE_ATTACH(dev) == 0) {
984 dev->dev_attached = 1;
989 /* else try next driver */
1000 device_detach(device_t dev)
1002 const struct module_data *mod = dev->dev_module;
1005 if (dev->dev_attached) {
1007 error = DEVICE_DETACH(dev);
1011 dev->dev_attached = 0;
1013 device_set_softc(dev, NULL);
1015 if (dev->dev_fixed_class == 0)
1016 devclass_delete_device(mod, dev);
1022 device_set_softc(device_t dev, void *softc)
1024 if (dev->dev_softc_alloc) {
1025 free(dev->dev_sc, M_DEVBUF);
1028 dev->dev_sc = softc;
1029 dev->dev_softc_alloc = 0;
1033 device_get_softc(device_t dev)
1038 return (dev->dev_sc);
1042 device_is_attached(device_t dev)
1044 return (dev->dev_attached);
1048 device_set_desc(device_t dev, const char *desc)
1050 snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc);
1054 device_set_desc_copy(device_t dev, const char *desc)
1056 device_set_desc(dev, desc);
1060 devclass_get_softc(devclass_t dc, int unit)
1062 return (device_get_softc(devclass_get_device(dc, unit)));
1066 devclass_get_maxunit(devclass_t dc)
1071 max_unit = DEVCLASS_MAXUNIT;
1072 while (max_unit--) {
1073 if (dc->dev_list[max_unit]) {
1083 devclass_get_device(devclass_t dc, int unit)
1085 return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ?
1086 NULL : dc->dev_list[unit]);
1090 devclass_find(const char *classname)
1092 const struct module_data *mod;
1094 TAILQ_FOREACH(mod, &module_head, entry) {
1095 if (devclass_equal(mod->driver->name, classname))
1096 return (mod->devclass_pp[0]);
1102 module_register(void *data)
1104 struct module_data *mdata = data;
1106 TAILQ_INSERT_TAIL(&module_head, mdata, entry);
1109 /*------------------------------------------------------------------------*
1111 *------------------------------------------------------------------------*/
1114 sysinit_run(const void **ppdata)
1116 const struct sysinit *psys;
1118 while ((psys = *ppdata) != NULL) {
1119 (psys->func) (psys->data);
1124 /*------------------------------------------------------------------------*
1126 *------------------------------------------------------------------------*/
1128 static int usb_do_process(struct usb_process *);
1129 static int usb_proc_level = -1;
1130 static struct mtx usb_proc_mtx;
1135 int old_level = usb_proc_level;
1136 int old_giant = Giant.owned;
1139 device_run_interrupts(usb_pci_root);
1145 while (++usb_proc_level < USB_PROC_MAX)
1146 worked |= usb_do_process(usb_process + usb_proc_level);
1148 usb_proc_level = old_level;
1149 Giant.owned = old_giant;
1157 sysinit_run(sysinit_data);
1163 sysinit_run(sysuninit_data);
1167 usb_process_init_sub(struct usb_process *up)
1169 TAILQ_INIT(&up->up_qhead);
1171 cv_init(&up->up_cv, "-");
1172 cv_init(&up->up_drain, "usbdrain");
1174 up->up_mtx = &usb_proc_mtx;
1178 usb_process_init(void *arg)
1182 mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE);
1184 for (x = 0; x != USB_PROC_MAX; x++)
1185 usb_process_init_sub(&usb_process[x]);
1188 SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL);
1191 usb_do_process(struct usb_process *up)
1193 struct usb_proc_msg *pm;
1196 mtx_lock(&usb_proc_mtx);
1199 pm = TAILQ_FIRST(&up->up_qhead);
1205 (pm->pm_callback) (pm);
1207 if (pm == TAILQ_FIRST(&up->up_qhead)) {
1208 /* nothing changed */
1209 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
1210 pm->pm_qentry.tqe_prev = NULL;
1214 mtx_unlock(&usb_proc_mtx);
1220 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
1222 struct usb_proc_msg *pm0 = _pm0;
1223 struct usb_proc_msg *pm1 = _pm1;
1224 struct usb_proc_msg *pm2;
1230 if (pm0->pm_qentry.tqe_prev) {
1233 if (pm1->pm_qentry.tqe_prev) {
1238 * No entries are queued. Queue "pm0" and use the existing
1242 } else if (t == 1) {
1243 /* Check if we need to increment the message number. */
1244 if (pm0->pm_num == up->up_msg_num) {
1248 } else if (t == 2) {
1249 /* Check if we need to increment the message number. */
1250 if (pm1->pm_num == up->up_msg_num) {
1254 } else if (t == 3) {
1256 * Both entries are queued. Re-queue the entry closest to
1259 d = (pm1->pm_num - pm0->pm_num);
1261 /* Check sign after subtraction */
1262 if (d & 0x80000000) {
1268 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
1270 pm2 = NULL; /* panic - should not happen */
1273 /* Put message last on queue */
1275 pm2->pm_num = up->up_msg_num;
1276 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
1281 /*------------------------------------------------------------------------*
1285 * 0: USB process is running
1286 * Else: USB process is tearing down
1287 *------------------------------------------------------------------------*/
1289 usb_proc_is_gone(struct usb_process *up)
1294 /*------------------------------------------------------------------------*
1297 * This function will return when the USB process message pointed to
1298 * by "pm" is no longer on a queue. This function must be called
1299 * having "usb_proc_mtx" locked.
1300 *------------------------------------------------------------------------*/
1302 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
1304 struct usb_proc_msg *pm0 = _pm0;
1305 struct usb_proc_msg *pm1 = _pm1;
1307 /* Just remove the messages from the queue. */
1308 if (pm0->pm_qentry.tqe_prev) {
1309 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
1310 pm0->pm_qentry.tqe_prev = NULL;
1312 if (pm1->pm_qentry.tqe_prev) {
1313 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
1314 pm1->pm_qentry.tqe_prev = NULL;
1318 /*------------------------------------------------------------------------*
1320 *------------------------------------------------------------------------*/
1322 #ifdef USB_PCI_PROBE_LIST
1323 static device_method_t pci_methods[] = {
1327 static driver_t pci_driver = {
1329 .methods = pci_methods,
1332 static devclass_t pci_devclass;
1334 DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0);
1336 static const char *usb_pci_devices[] = {
1340 #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *))
1342 static device_t usb_pci_dev[USB_PCI_USB_MAX];
1345 usb_pci_mod_load(void *arg)
1349 usb_pci_root = device_add_child(NULL, "pci", -1);
1350 if (usb_pci_root == NULL)
1353 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1354 usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1);
1355 if (usb_pci_dev[x] == NULL)
1357 if (device_probe_and_attach(usb_pci_dev[x])) {
1358 device_printf(usb_pci_dev[x],
1359 "WARNING: Probe and attach failed!\n");
1363 SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0);
1366 usb_pci_mod_unload(void *arg)
1370 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1371 if (usb_pci_dev[x]) {
1372 device_detach(usb_pci_dev[x]);
1373 device_delete_child(usb_pci_root, usb_pci_dev[x]);
1377 device_delete_child(NULL, usb_pci_root);
1379 SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0);
1382 /*------------------------------------------------------------------------*
1384 *------------------------------------------------------------------------*/
1387 #define USB_POOL_ALIGN 8
1389 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN);
1390 static uint32_t usb_pool_rem = USB_POOL_SIZE;
1391 static uint32_t usb_pool_entries;
1394 TAILQ_ENTRY(malloc_hdr) entry;
1396 } __aligned(USB_POOL_ALIGN);
1398 static TAILQ_HEAD(, malloc_hdr) malloc_head =
1399 TAILQ_HEAD_INITIALIZER(malloc_head);
1402 usb_malloc(unsigned long size)
1404 struct malloc_hdr *hdr;
1406 size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1);
1407 size += sizeof(struct malloc_hdr);
1409 TAILQ_FOREACH(hdr, &malloc_head, entry) {
1410 if (hdr->size == size)
1415 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1416 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1418 TAILQ_REMOVE(&malloc_head, hdr, entry);
1419 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1422 if (usb_pool_rem >= size) {
1423 hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem);
1426 usb_pool_rem -= size;
1429 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1430 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1432 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1441 struct malloc_hdr *hdr;
1449 TAILQ_INSERT_TAIL(&malloc_head, hdr, entry);
1454 usb_strdup(const char *str)
1459 len = 1 + strlen(str);
1461 tmp = malloc(len,XXX,XXX);
1465 memcpy(tmp, str, len);