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 /*------------------------------------------------------------------------*
34 * Implementation of mutex API
35 *------------------------------------------------------------------------*/
38 int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev,
39 int type, int *rid, unsigned int flags);
40 int (*ofw_bus_status_ok_cb)(device_t dev);
41 int (*ofw_bus_is_compatible_cb)(device_t dev, char *name);
44 mtx_system_init(void *arg)
46 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
48 SYSINIT(mtx_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, mtx_system_init, NULL);
51 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
52 bus_size_t boundary, bus_addr_t lowaddr,
53 bus_addr_t highaddr, bus_dma_filter_t *filter,
54 void *filterarg, bus_size_t maxsize, int nsegments,
55 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
56 void *lockfuncarg, bus_dma_tag_t *dmat)
58 struct bus_dma_tag *ret;
60 ret = malloc(sizeof(struct bus_dma_tag), XXX, XXX);
63 ret->alignment = alignment;
64 ret->maxsize = maxsize;
72 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
77 addr = malloc(dmat->maxsize + dmat->alignment, XXX, XXX);
82 addr = (void*)(((uintptr_t)addr + dmat->alignment - 1) & ~(dmat->alignment - 1));
89 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
90 bus_size_t buflen, bus_dmamap_callback_t *callback,
91 void *callback_arg, int flags)
93 bus_dma_segment_t segs[1];
95 segs[0].ds_addr = (uintptr_t)buf;
96 segs[0].ds_len = buflen;
98 (*callback)(callback_arg, segs, 1, 0);
104 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
111 bus_dma_tag_destroy(bus_dma_tag_t dmat)
119 bus_alloc_resource_any(device_t dev, int type, int *rid, unsigned int flags)
121 struct resource *res;
124 res = malloc(sizeof(*res), XXX, XXX);
128 res->__r_i = malloc(sizeof(struct resource_i), XXX, XXX);
129 if (res->__r_i == NULL) {
134 if (bus_alloc_resource_any_cb != NULL)
135 ret = (*bus_alloc_resource_any_cb)(res, dev, type, rid, flags);
139 free(res->__r_i, XXX);
145 bus_alloc_resources(device_t dev, struct resource_spec *rs,
146 struct resource **res)
150 for (i = 0; rs[i].type != -1; i++)
152 for (i = 0; rs[i].type != -1; i++) {
153 res[i] = bus_alloc_resource_any(dev,
154 rs[i].type, &rs[i].rid, rs[i].flags);
155 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
156 bus_release_resources(dev, rs, res);
164 bus_release_resources(device_t dev, const struct resource_spec *rs,
165 struct resource **res)
169 for (i = 0; rs[i].type != -1; i++)
170 if (res[i] != NULL) {
171 bus_release_resource(
172 dev, rs[i].type, rs[i].rid, res[i]);
178 bus_setup_intr(device_t dev, struct resource *r, int flags,
179 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
182 dev->dev_irq_filter = filter;
183 dev->dev_irq_fn = handler;
184 dev->dev_irq_arg = arg;
190 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
193 dev->dev_irq_filter = NULL;
194 dev->dev_irq_fn = NULL;
195 dev->dev_irq_arg = NULL;
201 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
203 /* Resource releasing is not supported */
208 bus_generic_attach(device_t dev)
212 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
213 device_probe_and_attach(child);
220 rman_get_bustag(struct resource *r)
223 return (r->r_bustag);
227 rman_get_bushandle(struct resource *r)
230 return (r->r_bushandle);
234 rman_get_size(struct resource *r)
237 return (r->__r_i->r_end - r->__r_i->r_start + 1);
241 ofw_bus_status_okay(device_t dev)
243 if (ofw_bus_status_ok_cb == NULL)
246 return ((*ofw_bus_status_ok_cb)(dev));
250 ofw_bus_is_compatible(device_t dev, char *name)
252 if (ofw_bus_is_compatible_cb == NULL)
255 return ((*ofw_bus_is_compatible_cb)(dev, name));
259 mtx_init(struct mtx *mtx, const char *name, const char *type, int opt)
266 mtx_lock(struct mtx *mtx)
273 mtx_unlock(struct mtx *mtx)
280 mtx_owned(struct mtx *mtx)
283 return (mtx->owned != 0);
287 mtx_destroy(struct mtx *mtx)
292 /*------------------------------------------------------------------------*
293 * Implementation of shared/exclusive mutex API
294 *------------------------------------------------------------------------*/
297 sx_init_flags(struct sx *sx, const char *name, int flags)
303 sx_destroy(struct sx *sx)
309 sx_xlock(struct sx *sx)
315 sx_xunlock(struct sx *sx)
321 sx_xlocked(struct sx *sx)
323 return (sx->owned != 0);
326 /*------------------------------------------------------------------------*
327 * Implementaiton of condition variable API
328 *------------------------------------------------------------------------*/
331 cv_init(struct cv *cv, const char *desc)
337 cv_destroy(struct cv *cv)
343 cv_wait(struct cv *cv, struct mtx *mtx)
345 cv_timedwait(cv, mtx, -1);
349 cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
356 return (EWOULDBLOCK); /* not allowed */
360 while (cv->sleeping) {
362 delta = ticks - start;
363 if (delta >= timo || delta < 0)
370 if (++time >= (1000000 / hz)) {
383 return (EWOULDBLOCK); /* not allowed */
389 cv_signal(struct cv *cv)
395 cv_broadcast(struct cv *cv)
400 /*------------------------------------------------------------------------*
401 * Implementation of callout API
402 *------------------------------------------------------------------------*/
404 static void callout_proc_msg(struct usb_proc_msg *);
406 volatile int ticks = 0;
408 static LIST_HEAD(, callout) head_callout = LIST_HEAD_INITIALIZER(&head_callout);
410 static struct mtx mtx_callout;
411 static struct usb_proc_msg callout_msg[2];
414 callout_system_init(void *arg)
416 mtx_init(&mtx_callout, "callout-mtx", NULL, MTX_DEF | MTX_RECURSE);
418 callout_msg[0].pm_callback = &callout_proc_msg;
419 callout_msg[1].pm_callback = &callout_proc_msg;
421 SYSINIT(callout_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, callout_system_init, NULL);
424 callout_callback(struct callout *c)
428 mtx_lock(&mtx_callout);
429 if (c->entry.le_prev != NULL) {
430 LIST_REMOVE(c, entry);
431 c->entry.le_prev = NULL;
433 mtx_unlock(&mtx_callout);
438 if (!(c->flags & CALLOUT_RETURNUNLOCKED))
443 callout_process(int timeout)
446 usb_proc_msignal(usb_process + 2, &callout_msg[0], &callout_msg[1]);
450 callout_proc_msg(struct usb_proc_msg *pmsg)
456 mtx_lock(&mtx_callout);
458 LIST_FOREACH(c, &head_callout, entry) {
460 delta = c->timeout - ticks;
462 mtx_unlock(&mtx_callout);
469 mtx_unlock(&mtx_callout);
473 callout_init_mtx(struct callout *c, struct mtx *mtx, int flags)
475 memset(c, 0, sizeof(*c));
481 c->flags = (flags & CALLOUT_RETURNUNLOCKED);
485 callout_reset(struct callout *c, int to_ticks,
486 void (*func) (void *), void *arg)
492 c->timeout = ticks + to_ticks;
494 mtx_lock(&mtx_callout);
495 LIST_INSERT_HEAD(&head_callout, c, entry);
496 mtx_unlock(&mtx_callout);
500 callout_stop(struct callout *c)
502 mtx_lock(&mtx_callout);
504 if (c->entry.le_prev != NULL) {
505 LIST_REMOVE(c, entry);
506 c->entry.le_prev = NULL;
508 mtx_unlock(&mtx_callout);
515 callout_drain(struct callout *c)
518 return; /* not initialised */
526 callout_pending(struct callout *c)
530 mtx_lock(&mtx_callout);
531 retval = (c->entry.le_prev != NULL);
532 mtx_unlock(&mtx_callout);
537 /*------------------------------------------------------------------------*
538 * Implementation of device API
539 *------------------------------------------------------------------------*/
541 static const char unknown_string[] = { "unknown" };
543 static TAILQ_HEAD(, module_data) module_head =
544 TAILQ_HEAD_INITIALIZER(module_head);
547 devclass_equal(const char *a, const char *b)
568 bus_generic_resume(device_t dev)
574 bus_generic_shutdown(device_t dev)
580 bus_generic_suspend(device_t dev)
586 bus_generic_print_child(device_t dev, device_t child)
592 bus_generic_driver_added(device_t dev, driver_t *driver)
598 device_get_parent(device_t dev)
600 return (dev ? dev->dev_parent : NULL);
604 device_set_interrupt(device_t dev, driver_filter_t *filter,
605 driver_intr_t *fn, void *arg)
607 dev->dev_irq_filter = filter;
608 dev->dev_irq_fn = fn;
609 dev->dev_irq_arg = arg;
613 device_run_interrupts(device_t parent)
620 TAILQ_FOREACH(child, &parent->dev_children, dev_link) {
622 if (child->dev_irq_filter != NULL)
623 status = child->dev_irq_filter(child->dev_irq_arg);
625 status = FILTER_SCHEDULE_THREAD;
627 if (status == FILTER_SCHEDULE_THREAD) {
628 if (child->dev_irq_fn != NULL)
629 (child->dev_irq_fn) (child->dev_irq_arg);
635 device_set_ivars(device_t dev, void *ivars)
637 dev->dev_aux = ivars;
641 device_get_ivars(device_t dev)
643 return (dev ? dev->dev_aux : NULL);
647 device_get_unit(device_t dev)
649 return (dev ? dev->dev_unit : 0);
653 bus_generic_detach(device_t dev)
658 if (!dev->dev_attached)
661 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
662 if ((error = device_detach(child)) != 0)
669 device_get_nameunit(device_t dev)
671 if (dev && dev->dev_nameunit[0])
672 return (dev->dev_nameunit);
674 return (unknown_string);
678 devclass_create(devclass_t *dc_pp)
683 if (dc_pp[0] == NULL) {
684 dc_pp[0] = malloc(sizeof(**(dc_pp)),
685 M_DEVBUF, M_WAITOK | M_ZERO);
687 if (dc_pp[0] == NULL) {
694 static const struct module_data *
695 devclass_find_create(const char *classname)
697 const struct module_data *mod;
699 TAILQ_FOREACH(mod, &module_head, entry) {
700 if (devclass_equal(mod->mod_name, classname)) {
701 if (devclass_create(mod->devclass_pp)) {
711 devclass_add_device(const struct module_data *mod, device_t dev)
717 pp_dev = mod->devclass_pp[0]->dev_list;
718 end = pp_dev + DEVCLASS_MAXUNIT;
721 while (pp_dev != end) {
722 if (*pp_dev == NULL) {
724 dev->dev_unit = unit;
725 dev->dev_module = mod;
726 snprintf(dev->dev_nameunit,
727 sizeof(dev->dev_nameunit),
728 "%s%d", device_get_name(dev), unit);
734 DPRINTF("Could not add device to devclass.\n");
739 devclass_delete_device(const struct module_data *mod, device_t dev)
744 mod->devclass_pp[0]->dev_list[dev->dev_unit] = NULL;
745 dev->dev_module = NULL;
749 make_device(device_t parent, const char *name)
752 const struct module_data *mod = NULL;
756 mod = devclass_find_create(name);
760 DPRINTF("%s:%d:%s: can't find device "
761 "class %s\n", __FILE__, __LINE__,
767 dev = malloc(sizeof(*dev),
768 M_DEVBUF, M_WAITOK | M_ZERO);
773 dev->dev_parent = parent;
774 TAILQ_INIT(&dev->dev_children);
777 dev->dev_fixed_class = 1;
778 if (devclass_add_device(mod, dev)) {
793 device_add_child(device_t dev, const char *name, int unit)
798 device_printf(dev, "Unit is not -1\n");
800 child = make_device(dev, name);
802 device_printf(dev, "Could not add child '%s'\n", name);
809 TAILQ_INSERT_TAIL(&dev->dev_children, child, dev_link);
815 device_delete_child(device_t dev, device_t child)
820 /* remove children first */
822 while ((grandchild = TAILQ_FIRST(&child->dev_children))) {
823 error = device_delete_child(child, grandchild);
825 device_printf(dev, "Error deleting child!\n");
830 error = device_detach(child);
835 devclass_delete_device(child->dev_module, child);
838 /* remove child from parent */
839 TAILQ_REMOVE(&dev->dev_children, child, dev_link);
841 free(child, M_DEVBUF);
848 device_delete_children(device_t dev)
853 while ((child = TAILQ_FIRST(&dev->dev_children))) {
854 error = device_delete_child(dev, child);
856 device_printf(dev, "Error deleting child!\n");
864 device_quiet(device_t dev)
870 device_get_desc(device_t dev)
873 return &(dev->dev_desc[0]);
874 return (unknown_string);
881 DPRINTF("Default method called\n");
886 device_get_method(device_t dev, const char *what)
888 const struct device_method *mtod;
890 mtod = dev->dev_module->driver->methods;
891 while (mtod->func != NULL) {
892 if (devclass_equal(mtod->desc, what)) {
897 return ((void *)&default_method);
901 device_get_name(device_t dev)
904 return (unknown_string);
906 return (dev->dev_module->driver->name);
910 device_allocate_softc(device_t dev)
912 const struct module_data *mod;
914 mod = dev->dev_module;
916 if ((dev->dev_softc_alloc == 0) &&
917 (mod->driver->size != 0)) {
918 dev->dev_sc = malloc(mod->driver->size,
919 M_DEVBUF, M_WAITOK | M_ZERO);
921 if (dev->dev_sc == NULL)
924 dev->dev_softc_alloc = 1;
930 device_probe_and_attach(device_t dev)
932 const struct module_data *mod;
933 const char *bus_name_parent;
935 bus_name_parent = device_get_name(device_get_parent(dev));
937 if (dev->dev_attached)
938 return (0); /* fail-safe */
940 if (dev->dev_fixed_class) {
942 mod = dev->dev_module;
944 if (DEVICE_PROBE(dev) <= 0) {
946 if (device_allocate_softc(dev) == 0) {
948 if (DEVICE_ATTACH(dev) == 0) {
950 dev->dev_attached = 1;
960 * Else find a module for our device, if any
963 TAILQ_FOREACH(mod, &module_head, entry) {
964 if (devclass_equal(mod->bus_name, bus_name_parent)) {
965 if (devclass_create(mod->devclass_pp)) {
968 if (devclass_add_device(mod, dev)) {
971 if (DEVICE_PROBE(dev) <= 0) {
973 if (device_allocate_softc(dev) == 0) {
975 if (DEVICE_ATTACH(dev) == 0) {
977 dev->dev_attached = 1;
982 /* else try next driver */
993 device_detach(device_t dev)
995 const struct module_data *mod = dev->dev_module;
998 if (dev->dev_attached) {
1000 error = DEVICE_DETACH(dev);
1004 dev->dev_attached = 0;
1006 device_set_softc(dev, NULL);
1008 if (dev->dev_fixed_class == 0)
1009 devclass_delete_device(mod, dev);
1015 device_set_softc(device_t dev, void *softc)
1017 if (dev->dev_softc_alloc) {
1018 free(dev->dev_sc, M_DEVBUF);
1021 dev->dev_sc = softc;
1022 dev->dev_softc_alloc = 0;
1026 device_get_softc(device_t dev)
1031 return (dev->dev_sc);
1035 device_is_attached(device_t dev)
1037 return (dev->dev_attached);
1041 device_set_desc(device_t dev, const char *desc)
1043 snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc);
1047 device_set_desc_copy(device_t dev, const char *desc)
1049 device_set_desc(dev, desc);
1053 devclass_get_softc(devclass_t dc, int unit)
1055 return (device_get_softc(devclass_get_device(dc, unit)));
1059 devclass_get_maxunit(devclass_t dc)
1064 max_unit = DEVCLASS_MAXUNIT;
1065 while (max_unit--) {
1066 if (dc->dev_list[max_unit]) {
1076 devclass_get_device(devclass_t dc, int unit)
1078 return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ?
1079 NULL : dc->dev_list[unit]);
1083 devclass_find(const char *classname)
1085 const struct module_data *mod;
1087 TAILQ_FOREACH(mod, &module_head, entry) {
1088 if (devclass_equal(mod->driver->name, classname))
1089 return (mod->devclass_pp[0]);
1095 module_register(void *data)
1097 struct module_data *mdata = data;
1099 TAILQ_INSERT_TAIL(&module_head, mdata, entry);
1102 /*------------------------------------------------------------------------*
1104 *------------------------------------------------------------------------*/
1107 sysinit_run(const void **ppdata)
1109 const struct sysinit *psys;
1111 while ((psys = *ppdata) != NULL) {
1112 (psys->func) (psys->data);
1117 /*------------------------------------------------------------------------*
1119 *------------------------------------------------------------------------*/
1121 static int usb_do_process(struct usb_process *);
1122 static int usb_proc_level = -1;
1123 static struct mtx usb_proc_mtx;
1128 int old_level = usb_proc_level;
1129 int old_giant = Giant.owned;
1132 device_run_interrupts(usb_pci_root);
1138 while (++usb_proc_level < USB_PROC_MAX)
1139 worked |= usb_do_process(usb_process + usb_proc_level);
1141 usb_proc_level = old_level;
1142 Giant.owned = old_giant;
1150 sysinit_run(sysinit_data);
1156 sysinit_run(sysuninit_data);
1160 usb_process_init_sub(struct usb_process *up)
1162 TAILQ_INIT(&up->up_qhead);
1164 cv_init(&up->up_cv, "-");
1165 cv_init(&up->up_drain, "usbdrain");
1167 up->up_mtx = &usb_proc_mtx;
1171 usb_process_init(void *arg)
1175 mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE);
1177 for (x = 0; x != USB_PROC_MAX; x++)
1178 usb_process_init_sub(&usb_process[x]);
1181 SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL);
1184 usb_do_process(struct usb_process *up)
1186 struct usb_proc_msg *pm;
1189 mtx_lock(&usb_proc_mtx);
1192 pm = TAILQ_FIRST(&up->up_qhead);
1198 (pm->pm_callback) (pm);
1200 if (pm == TAILQ_FIRST(&up->up_qhead)) {
1201 /* nothing changed */
1202 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
1203 pm->pm_qentry.tqe_prev = NULL;
1207 mtx_unlock(&usb_proc_mtx);
1213 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
1215 struct usb_proc_msg *pm0 = _pm0;
1216 struct usb_proc_msg *pm1 = _pm1;
1217 struct usb_proc_msg *pm2;
1223 if (pm0->pm_qentry.tqe_prev) {
1226 if (pm1->pm_qentry.tqe_prev) {
1231 * No entries are queued. Queue "pm0" and use the existing
1235 } else if (t == 1) {
1236 /* Check if we need to increment the message number. */
1237 if (pm0->pm_num == up->up_msg_num) {
1241 } else if (t == 2) {
1242 /* Check if we need to increment the message number. */
1243 if (pm1->pm_num == up->up_msg_num) {
1247 } else if (t == 3) {
1249 * Both entries are queued. Re-queue the entry closest to
1252 d = (pm1->pm_num - pm0->pm_num);
1254 /* Check sign after subtraction */
1255 if (d & 0x80000000) {
1261 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
1263 pm2 = NULL; /* panic - should not happen */
1266 /* Put message last on queue */
1268 pm2->pm_num = up->up_msg_num;
1269 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
1274 /*------------------------------------------------------------------------*
1278 * 0: USB process is running
1279 * Else: USB process is tearing down
1280 *------------------------------------------------------------------------*/
1282 usb_proc_is_gone(struct usb_process *up)
1287 /*------------------------------------------------------------------------*
1290 * This function will return when the USB process message pointed to
1291 * by "pm" is no longer on a queue. This function must be called
1292 * having "usb_proc_mtx" locked.
1293 *------------------------------------------------------------------------*/
1295 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
1297 struct usb_proc_msg *pm0 = _pm0;
1298 struct usb_proc_msg *pm1 = _pm1;
1300 /* Just remove the messages from the queue. */
1301 if (pm0->pm_qentry.tqe_prev) {
1302 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
1303 pm0->pm_qentry.tqe_prev = NULL;
1305 if (pm1->pm_qentry.tqe_prev) {
1306 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
1307 pm1->pm_qentry.tqe_prev = NULL;
1311 /*------------------------------------------------------------------------*
1313 *------------------------------------------------------------------------*/
1315 #ifdef USB_PCI_PROBE_LIST
1316 static device_method_t pci_methods[] = {
1320 static driver_t pci_driver = {
1322 .methods = pci_methods,
1325 static devclass_t pci_devclass;
1327 DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0);
1329 static const char *usb_pci_devices[] = {
1333 #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *))
1335 static device_t usb_pci_dev[USB_PCI_USB_MAX];
1338 usb_pci_mod_load(void *arg)
1342 usb_pci_root = device_add_child(NULL, "pci", -1);
1343 if (usb_pci_root == NULL)
1346 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1347 usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1);
1348 if (usb_pci_dev[x] == NULL)
1350 if (device_probe_and_attach(usb_pci_dev[x])) {
1351 device_printf(usb_pci_dev[x],
1352 "WARNING: Probe and attach failed!\n");
1356 SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0);
1359 usb_pci_mod_unload(void *arg)
1363 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1364 if (usb_pci_dev[x]) {
1365 device_detach(usb_pci_dev[x]);
1366 device_delete_child(usb_pci_root, usb_pci_dev[x]);
1370 device_delete_child(NULL, usb_pci_root);
1372 SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0);
1375 /*------------------------------------------------------------------------*
1377 *------------------------------------------------------------------------*/
1380 #define USB_POOL_ALIGN 8
1382 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN);
1383 static uint32_t usb_pool_rem = USB_POOL_SIZE;
1384 static uint32_t usb_pool_entries;
1387 TAILQ_ENTRY(malloc_hdr) entry;
1389 } __aligned(USB_POOL_ALIGN);
1391 static TAILQ_HEAD(, malloc_hdr) malloc_head =
1392 TAILQ_HEAD_INITIALIZER(malloc_head);
1395 usb_malloc(unsigned long size)
1397 struct malloc_hdr *hdr;
1399 size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1);
1400 size += sizeof(struct malloc_hdr);
1402 TAILQ_FOREACH(hdr, &malloc_head, entry) {
1403 if (hdr->size == size)
1408 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1409 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1411 TAILQ_REMOVE(&malloc_head, hdr, entry);
1412 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1415 if (usb_pool_rem >= size) {
1416 hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem);
1419 usb_pool_rem -= size;
1422 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1423 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1425 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1434 struct malloc_hdr *hdr;
1442 TAILQ_INSERT_TAIL(&malloc_head, hdr, entry);
1447 usb_strdup(const char *str)
1452 len = 1 + strlen(str);
1454 tmp = malloc(len,XXX,XXX);
1458 memcpy(tmp, str, len);