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);
435 if (c->c_func != NULL)
436 (c->c_func) (c->c_arg);
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 /* detach parent before deleting children, if any */
821 error = device_detach(child);
825 /* remove children second */
826 while ((grandchild = TAILQ_FIRST(&child->dev_children))) {
827 error = device_delete_child(child, grandchild);
829 device_printf(dev, "Error deleting child!\n");
834 devclass_delete_device(child->dev_module, child);
837 /* remove child from parent */
838 TAILQ_REMOVE(&dev->dev_children, child, dev_link);
840 free(child, M_DEVBUF);
847 device_delete_children(device_t dev)
852 while ((child = TAILQ_FIRST(&dev->dev_children))) {
853 error = device_delete_child(dev, child);
855 device_printf(dev, "Error deleting child!\n");
863 device_quiet(device_t dev)
869 device_get_desc(device_t dev)
872 return &(dev->dev_desc[0]);
873 return (unknown_string);
880 DPRINTF("Default method called\n");
885 device_get_method(device_t dev, const char *what)
887 const struct device_method *mtod;
889 mtod = dev->dev_module->driver->methods;
890 while (mtod->func != NULL) {
891 if (devclass_equal(mtod->desc, what)) {
896 return ((void *)&default_method);
900 device_get_name(device_t dev)
903 return (unknown_string);
905 return (dev->dev_module->driver->name);
909 device_allocate_softc(device_t dev)
911 const struct module_data *mod;
913 mod = dev->dev_module;
915 if ((dev->dev_softc_alloc == 0) &&
916 (mod->driver->size != 0)) {
917 dev->dev_sc = malloc(mod->driver->size,
918 M_DEVBUF, M_WAITOK | M_ZERO);
920 if (dev->dev_sc == NULL)
923 dev->dev_softc_alloc = 1;
929 device_probe_and_attach(device_t dev)
931 const struct module_data *mod;
932 const char *bus_name_parent;
934 bus_name_parent = device_get_name(device_get_parent(dev));
936 if (dev->dev_attached)
937 return (0); /* fail-safe */
939 if (dev->dev_fixed_class) {
941 mod = dev->dev_module;
943 if (DEVICE_PROBE(dev) <= 0) {
945 if (device_allocate_softc(dev) == 0) {
947 if (DEVICE_ATTACH(dev) == 0) {
949 dev->dev_attached = 1;
959 * Else find a module for our device, if any
962 TAILQ_FOREACH(mod, &module_head, entry) {
963 if (devclass_equal(mod->bus_name, bus_name_parent)) {
964 if (devclass_create(mod->devclass_pp)) {
967 if (devclass_add_device(mod, dev)) {
970 if (DEVICE_PROBE(dev) <= 0) {
972 if (device_allocate_softc(dev) == 0) {
974 if (DEVICE_ATTACH(dev) == 0) {
976 dev->dev_attached = 1;
981 /* else try next driver */
992 device_detach(device_t dev)
994 const struct module_data *mod = dev->dev_module;
997 if (dev->dev_attached) {
999 error = DEVICE_DETACH(dev);
1003 dev->dev_attached = 0;
1005 device_set_softc(dev, NULL);
1007 if (dev->dev_fixed_class == 0)
1008 devclass_delete_device(mod, dev);
1014 device_set_softc(device_t dev, void *softc)
1016 if (dev->dev_softc_alloc) {
1017 free(dev->dev_sc, M_DEVBUF);
1020 dev->dev_sc = softc;
1021 dev->dev_softc_alloc = 0;
1025 device_get_softc(device_t dev)
1030 return (dev->dev_sc);
1034 device_is_attached(device_t dev)
1036 return (dev->dev_attached);
1040 device_set_desc(device_t dev, const char *desc)
1042 snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc);
1046 device_set_desc_copy(device_t dev, const char *desc)
1048 device_set_desc(dev, desc);
1052 devclass_get_softc(devclass_t dc, int unit)
1054 return (device_get_softc(devclass_get_device(dc, unit)));
1058 devclass_get_maxunit(devclass_t dc)
1063 max_unit = DEVCLASS_MAXUNIT;
1064 while (max_unit--) {
1065 if (dc->dev_list[max_unit]) {
1075 devclass_get_device(devclass_t dc, int unit)
1077 return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ?
1078 NULL : dc->dev_list[unit]);
1082 devclass_find(const char *classname)
1084 const struct module_data *mod;
1086 TAILQ_FOREACH(mod, &module_head, entry) {
1087 if (devclass_equal(mod->driver->name, classname))
1088 return (mod->devclass_pp[0]);
1094 module_register(void *data)
1096 struct module_data *mdata = data;
1098 TAILQ_INSERT_TAIL(&module_head, mdata, entry);
1101 /*------------------------------------------------------------------------*
1103 *------------------------------------------------------------------------*/
1106 sysinit_run(const void **ppdata)
1108 const struct sysinit *psys;
1110 while ((psys = *ppdata) != NULL) {
1111 (psys->func) (psys->data);
1116 /*------------------------------------------------------------------------*
1118 *------------------------------------------------------------------------*/
1120 static int usb_do_process(struct usb_process *);
1121 static int usb_proc_level = -1;
1122 static struct mtx usb_proc_mtx;
1127 int old_level = usb_proc_level;
1128 int old_giant = Giant.owned;
1131 device_run_interrupts(usb_pci_root);
1137 while (++usb_proc_level < USB_PROC_MAX)
1138 worked |= usb_do_process(usb_process + usb_proc_level);
1140 usb_proc_level = old_level;
1141 Giant.owned = old_giant;
1149 sysinit_run(sysinit_data);
1155 sysinit_run(sysuninit_data);
1159 usb_process_init_sub(struct usb_process *up)
1161 TAILQ_INIT(&up->up_qhead);
1163 cv_init(&up->up_cv, "-");
1164 cv_init(&up->up_drain, "usbdrain");
1166 up->up_mtx = &usb_proc_mtx;
1170 usb_process_init(void *arg)
1174 mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE);
1176 for (x = 0; x != USB_PROC_MAX; x++)
1177 usb_process_init_sub(&usb_process[x]);
1180 SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL);
1183 usb_do_process(struct usb_process *up)
1185 struct usb_proc_msg *pm;
1188 mtx_lock(&usb_proc_mtx);
1191 pm = TAILQ_FIRST(&up->up_qhead);
1197 (pm->pm_callback) (pm);
1199 if (pm == TAILQ_FIRST(&up->up_qhead)) {
1200 /* nothing changed */
1201 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
1202 pm->pm_qentry.tqe_prev = NULL;
1206 mtx_unlock(&usb_proc_mtx);
1212 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
1214 struct usb_proc_msg *pm0 = _pm0;
1215 struct usb_proc_msg *pm1 = _pm1;
1216 struct usb_proc_msg *pm2;
1222 if (pm0->pm_qentry.tqe_prev) {
1225 if (pm1->pm_qentry.tqe_prev) {
1230 * No entries are queued. Queue "pm0" and use the existing
1234 } else if (t == 1) {
1235 /* Check if we need to increment the message number. */
1236 if (pm0->pm_num == up->up_msg_num) {
1240 } else if (t == 2) {
1241 /* Check if we need to increment the message number. */
1242 if (pm1->pm_num == up->up_msg_num) {
1246 } else if (t == 3) {
1248 * Both entries are queued. Re-queue the entry closest to
1251 d = (pm1->pm_num - pm0->pm_num);
1253 /* Check sign after subtraction */
1254 if (d & 0x80000000) {
1260 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
1262 pm2 = NULL; /* panic - should not happen */
1265 /* Put message last on queue */
1267 pm2->pm_num = up->up_msg_num;
1268 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
1273 /*------------------------------------------------------------------------*
1277 * 0: USB process is running
1278 * Else: USB process is tearing down
1279 *------------------------------------------------------------------------*/
1281 usb_proc_is_gone(struct usb_process *up)
1286 /*------------------------------------------------------------------------*
1289 * This function will return when the USB process message pointed to
1290 * by "pm" is no longer on a queue. This function must be called
1291 * having "usb_proc_mtx" locked.
1292 *------------------------------------------------------------------------*/
1294 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
1296 struct usb_proc_msg *pm0 = _pm0;
1297 struct usb_proc_msg *pm1 = _pm1;
1299 /* Just remove the messages from the queue. */
1300 if (pm0->pm_qentry.tqe_prev) {
1301 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
1302 pm0->pm_qentry.tqe_prev = NULL;
1304 if (pm1->pm_qentry.tqe_prev) {
1305 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
1306 pm1->pm_qentry.tqe_prev = NULL;
1310 /*------------------------------------------------------------------------*
1312 *------------------------------------------------------------------------*/
1314 #ifdef USB_PCI_PROBE_LIST
1315 static device_method_t pci_methods[] = {
1319 static driver_t pci_driver = {
1321 .methods = pci_methods,
1324 static devclass_t pci_devclass;
1326 DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0);
1328 static const char *usb_pci_devices[] = {
1332 #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *))
1334 static device_t usb_pci_dev[USB_PCI_USB_MAX];
1337 usb_pci_mod_load(void *arg)
1341 usb_pci_root = device_add_child(NULL, "pci", -1);
1342 if (usb_pci_root == NULL)
1345 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1346 usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1);
1347 if (usb_pci_dev[x] == NULL)
1349 if (device_probe_and_attach(usb_pci_dev[x])) {
1350 device_printf(usb_pci_dev[x],
1351 "WARNING: Probe and attach failed!\n");
1355 SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0);
1358 usb_pci_mod_unload(void *arg)
1362 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1363 if (usb_pci_dev[x]) {
1364 device_detach(usb_pci_dev[x]);
1365 device_delete_child(usb_pci_root, usb_pci_dev[x]);
1369 device_delete_child(NULL, usb_pci_root);
1371 SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0);
1374 /*------------------------------------------------------------------------*
1376 *------------------------------------------------------------------------*/
1379 #define USB_POOL_ALIGN 8
1381 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN);
1382 static uint32_t usb_pool_rem = USB_POOL_SIZE;
1383 static uint32_t usb_pool_entries;
1386 TAILQ_ENTRY(malloc_hdr) entry;
1388 } __aligned(USB_POOL_ALIGN);
1390 static TAILQ_HEAD(, malloc_hdr) malloc_head =
1391 TAILQ_HEAD_INITIALIZER(malloc_head);
1394 usb_malloc(unsigned long size)
1396 struct malloc_hdr *hdr;
1398 size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1);
1399 size += sizeof(struct malloc_hdr);
1401 TAILQ_FOREACH(hdr, &malloc_head, entry) {
1402 if (hdr->size == size)
1407 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1408 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1410 TAILQ_REMOVE(&malloc_head, hdr, entry);
1411 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1414 if (usb_pool_rem >= size) {
1415 hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem);
1418 usb_pool_rem -= size;
1421 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1422 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1424 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1433 struct malloc_hdr *hdr;
1441 TAILQ_INSERT_TAIL(&malloc_head, hdr, entry);
1446 usb_strdup(const char *str)
1451 len = 1 + strlen(str);
1453 tmp = malloc(len,XXX,XXX);
1457 memcpy(tmp, str, len);