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_alloc_resource_any(device_t dev, int type, int *rid, unsigned int flags)
56 res = malloc(sizeof(*res), XXX, XXX);
60 res->__r_i = malloc(sizeof(struct resource_i), XXX, XXX);
61 if (res->__r_i == NULL) {
66 if (bus_alloc_resource_any_cb != NULL)
67 ret = (*bus_alloc_resource_any_cb)(res, dev, type, rid, flags);
71 free(res->__r_i, XXX);
77 bus_alloc_resources(device_t dev, struct resource_spec *rs,
78 struct resource **res)
82 for (i = 0; rs[i].type != -1; i++)
84 for (i = 0; rs[i].type != -1; i++) {
85 res[i] = bus_alloc_resource_any(dev,
86 rs[i].type, &rs[i].rid, rs[i].flags);
87 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
88 bus_release_resources(dev, rs, res);
96 bus_release_resources(device_t dev, const struct resource_spec *rs,
97 struct resource **res)
101 for (i = 0; rs[i].type != -1; i++)
102 if (res[i] != NULL) {
103 bus_release_resource(
104 dev, rs[i].type, rs[i].rid, res[i]);
110 bus_setup_intr(device_t dev, struct resource *r, int flags,
111 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
114 dev->dev_irq_filter = filter;
115 dev->dev_irq_fn = handler;
116 dev->dev_irq_arg = arg;
122 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
125 dev->dev_irq_filter = NULL;
126 dev->dev_irq_fn = NULL;
127 dev->dev_irq_arg = NULL;
133 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
135 /* Resource releasing is not supported */
140 bus_generic_attach(device_t dev)
144 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
145 device_probe_and_attach(child);
152 rman_get_bustag(struct resource *r)
155 return (r->r_bustag);
159 rman_get_bushandle(struct resource *r)
162 return (r->r_bushandle);
166 rman_get_size(struct resource *r)
169 return (r->__r_i->r_end - r->__r_i->r_start + 1);
173 ofw_bus_status_okay(device_t dev)
175 if (ofw_bus_status_ok_cb == NULL)
178 return ((*ofw_bus_status_ok_cb)(dev));
182 ofw_bus_is_compatible(device_t dev, char *name)
184 if (ofw_bus_is_compatible_cb == NULL)
187 return ((*ofw_bus_is_compatible_cb)(dev, name));
191 mtx_init(struct mtx *mtx, const char *name, const char *type, int opt)
198 mtx_lock(struct mtx *mtx)
205 mtx_unlock(struct mtx *mtx)
212 mtx_owned(struct mtx *mtx)
215 return (mtx->owned != 0);
219 mtx_destroy(struct mtx *mtx)
224 /*------------------------------------------------------------------------*
225 * Implementation of shared/exclusive mutex API
226 *------------------------------------------------------------------------*/
229 sx_init_flags(struct sx *sx, const char *name, int flags)
235 sx_destroy(struct sx *sx)
241 sx_xlock(struct sx *sx)
247 sx_xunlock(struct sx *sx)
253 sx_xlocked(struct sx *sx)
255 return (sx->owned != 0);
258 /*------------------------------------------------------------------------*
259 * Implementaiton of condition variable API
260 *------------------------------------------------------------------------*/
263 cv_init(struct cv *cv, const char *desc)
269 cv_destroy(struct cv *cv)
275 cv_wait(struct cv *cv, struct mtx *mtx)
277 cv_timedwait(cv, mtx, -1);
281 cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
288 return (EWOULDBLOCK); /* not allowed */
292 while (cv->sleeping) {
294 delta = ticks - start;
295 if (delta >= timo || delta < 0)
302 if (++time >= (1000000 / hz)) {
315 return (EWOULDBLOCK); /* not allowed */
321 cv_signal(struct cv *cv)
327 cv_broadcast(struct cv *cv)
332 /*------------------------------------------------------------------------*
333 * Implementation of callout API
334 *------------------------------------------------------------------------*/
336 static void callout_proc_msg(struct usb_proc_msg *);
338 volatile int ticks = 0;
340 static LIST_HEAD(, callout) head_callout = LIST_HEAD_INITIALIZER(&head_callout);
342 static struct mtx mtx_callout;
343 static struct usb_proc_msg callout_msg[2];
346 callout_system_init(void *arg)
348 mtx_init(&mtx_callout, "callout-mtx", NULL, MTX_DEF | MTX_RECURSE);
350 callout_msg[0].pm_callback = &callout_proc_msg;
351 callout_msg[1].pm_callback = &callout_proc_msg;
353 SYSINIT(callout_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, callout_system_init, NULL);
356 callout_callback(struct callout *c)
360 mtx_lock(&mtx_callout);
361 if (c->entry.le_prev != NULL) {
362 LIST_REMOVE(c, entry);
363 c->entry.le_prev = NULL;
365 mtx_unlock(&mtx_callout);
370 if (!(c->flags & CALLOUT_RETURNUNLOCKED))
375 callout_process(int timeout)
378 usb_proc_msignal(usb_process + 2, &callout_msg[0], &callout_msg[1]);
382 callout_proc_msg(struct usb_proc_msg *pmsg)
388 mtx_lock(&mtx_callout);
390 LIST_FOREACH(c, &head_callout, entry) {
392 delta = c->timeout - ticks;
394 mtx_unlock(&mtx_callout);
401 mtx_unlock(&mtx_callout);
405 callout_init_mtx(struct callout *c, struct mtx *mtx, int flags)
407 memset(c, 0, sizeof(*c));
413 c->flags = (flags & CALLOUT_RETURNUNLOCKED);
417 callout_reset(struct callout *c, int to_ticks,
418 void (*func) (void *), void *arg)
424 c->timeout = ticks + to_ticks;
426 mtx_lock(&mtx_callout);
427 LIST_INSERT_HEAD(&head_callout, c, entry);
428 mtx_unlock(&mtx_callout);
432 callout_stop(struct callout *c)
434 mtx_lock(&mtx_callout);
436 if (c->entry.le_prev != NULL) {
437 LIST_REMOVE(c, entry);
438 c->entry.le_prev = NULL;
440 mtx_unlock(&mtx_callout);
447 callout_drain(struct callout *c)
450 return; /* not initialised */
458 callout_pending(struct callout *c)
462 mtx_lock(&mtx_callout);
463 retval = (c->entry.le_prev != NULL);
464 mtx_unlock(&mtx_callout);
469 /*------------------------------------------------------------------------*
470 * Implementation of device API
471 *------------------------------------------------------------------------*/
473 static const char unknown_string[] = { "unknown" };
475 static TAILQ_HEAD(, module_data) module_head =
476 TAILQ_HEAD_INITIALIZER(module_head);
479 devclass_equal(const char *a, const char *b)
500 bus_generic_resume(device_t dev)
506 bus_generic_shutdown(device_t dev)
512 bus_generic_suspend(device_t dev)
518 bus_generic_print_child(device_t dev, device_t child)
524 bus_generic_driver_added(device_t dev, driver_t *driver)
530 device_get_parent(device_t dev)
532 return (dev ? dev->dev_parent : NULL);
536 device_set_interrupt(device_t dev, driver_filter_t *filter,
537 driver_intr_t *fn, void *arg)
539 dev->dev_irq_filter = filter;
540 dev->dev_irq_fn = fn;
541 dev->dev_irq_arg = arg;
545 device_run_interrupts(device_t parent)
552 TAILQ_FOREACH(child, &parent->dev_children, dev_link) {
554 if (child->dev_irq_filter != NULL)
555 status = child->dev_irq_filter(child->dev_irq_arg);
557 status = FILTER_SCHEDULE_THREAD;
559 if (status == FILTER_SCHEDULE_THREAD) {
560 if (child->dev_irq_fn != NULL)
561 (child->dev_irq_fn) (child->dev_irq_arg);
567 device_set_ivars(device_t dev, void *ivars)
569 dev->dev_aux = ivars;
573 device_get_ivars(device_t dev)
575 return (dev ? dev->dev_aux : NULL);
579 device_get_unit(device_t dev)
581 return (dev ? dev->dev_unit : 0);
585 bus_generic_detach(device_t dev)
590 if (!dev->dev_attached)
593 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
594 if ((error = device_detach(child)) != 0)
601 device_get_nameunit(device_t dev)
603 if (dev && dev->dev_nameunit[0])
604 return (dev->dev_nameunit);
606 return (unknown_string);
610 devclass_create(devclass_t *dc_pp)
615 if (dc_pp[0] == NULL) {
616 dc_pp[0] = malloc(sizeof(**(dc_pp)),
617 M_DEVBUF, M_WAITOK | M_ZERO);
619 if (dc_pp[0] == NULL) {
626 static const struct module_data *
627 devclass_find_create(const char *classname)
629 const struct module_data *mod;
631 TAILQ_FOREACH(mod, &module_head, entry) {
632 if (devclass_equal(mod->mod_name, classname)) {
633 if (devclass_create(mod->devclass_pp)) {
643 devclass_add_device(const struct module_data *mod, device_t dev)
649 pp_dev = mod->devclass_pp[0]->dev_list;
650 end = pp_dev + DEVCLASS_MAXUNIT;
653 while (pp_dev != end) {
654 if (*pp_dev == NULL) {
656 dev->dev_unit = unit;
657 dev->dev_module = mod;
658 snprintf(dev->dev_nameunit,
659 sizeof(dev->dev_nameunit),
660 "%s%d", device_get_name(dev), unit);
666 DPRINTF("Could not add device to devclass.\n");
671 devclass_delete_device(const struct module_data *mod, device_t dev)
676 mod->devclass_pp[0]->dev_list[dev->dev_unit] = NULL;
677 dev->dev_module = NULL;
681 make_device(device_t parent, const char *name)
684 const struct module_data *mod = NULL;
688 mod = devclass_find_create(name);
692 DPRINTF("%s:%d:%s: can't find device "
693 "class %s\n", __FILE__, __LINE__,
699 dev = malloc(sizeof(*dev),
700 M_DEVBUF, M_WAITOK | M_ZERO);
705 dev->dev_parent = parent;
706 TAILQ_INIT(&dev->dev_children);
709 dev->dev_fixed_class = 1;
710 if (devclass_add_device(mod, dev)) {
725 device_add_child(device_t dev, const char *name, int unit)
730 device_printf(dev, "Unit is not -1\n");
732 child = make_device(dev, name);
734 device_printf(dev, "Could not add child '%s'\n", name);
741 TAILQ_INSERT_TAIL(&dev->dev_children, child, dev_link);
747 device_delete_child(device_t dev, device_t child)
752 /* remove children first */
754 while ((grandchild = TAILQ_FIRST(&child->dev_children))) {
755 error = device_delete_child(child, grandchild);
757 device_printf(dev, "Error deleting child!\n");
762 error = device_detach(child);
767 devclass_delete_device(child->dev_module, child);
770 /* remove child from parent */
771 TAILQ_REMOVE(&dev->dev_children, child, dev_link);
773 free(child, M_DEVBUF);
780 device_delete_children(device_t dev)
785 while ((child = TAILQ_FIRST(&dev->dev_children))) {
786 error = device_delete_child(dev, child);
788 device_printf(dev, "Error deleting child!\n");
796 device_quiet(device_t dev)
802 device_get_desc(device_t dev)
805 return &(dev->dev_desc[0]);
806 return (unknown_string);
813 DPRINTF("Default method called\n");
818 device_get_method(device_t dev, const char *what)
820 const struct device_method *mtod;
822 mtod = dev->dev_module->driver->methods;
823 while (mtod->func != NULL) {
824 if (devclass_equal(mtod->desc, what)) {
829 return ((void *)&default_method);
833 device_get_name(device_t dev)
836 return (unknown_string);
838 return (dev->dev_module->driver->name);
842 device_allocate_softc(device_t dev)
844 const struct module_data *mod;
846 mod = dev->dev_module;
848 if ((dev->dev_softc_alloc == 0) &&
849 (mod->driver->size != 0)) {
850 dev->dev_sc = malloc(mod->driver->size,
851 M_DEVBUF, M_WAITOK | M_ZERO);
853 if (dev->dev_sc == NULL)
856 dev->dev_softc_alloc = 1;
862 device_probe_and_attach(device_t dev)
864 const struct module_data *mod;
865 const char *bus_name_parent;
867 bus_name_parent = device_get_name(device_get_parent(dev));
869 if (dev->dev_attached)
870 return (0); /* fail-safe */
872 if (dev->dev_fixed_class) {
874 mod = dev->dev_module;
876 if (DEVICE_PROBE(dev) <= 0) {
878 if (device_allocate_softc(dev) == 0) {
880 if (DEVICE_ATTACH(dev) == 0) {
882 dev->dev_attached = 1;
892 * Else find a module for our device, if any
895 TAILQ_FOREACH(mod, &module_head, entry) {
896 if (devclass_equal(mod->bus_name, bus_name_parent)) {
897 if (devclass_create(mod->devclass_pp)) {
900 if (devclass_add_device(mod, dev)) {
903 if (DEVICE_PROBE(dev) <= 0) {
905 if (device_allocate_softc(dev) == 0) {
907 if (DEVICE_ATTACH(dev) == 0) {
909 dev->dev_attached = 1;
914 /* else try next driver */
925 device_detach(device_t dev)
927 const struct module_data *mod = dev->dev_module;
930 if (dev->dev_attached) {
932 error = DEVICE_DETACH(dev);
936 dev->dev_attached = 0;
938 device_set_softc(dev, NULL);
940 if (dev->dev_fixed_class == 0)
941 devclass_delete_device(mod, dev);
947 device_set_softc(device_t dev, void *softc)
949 if (dev->dev_softc_alloc) {
950 free(dev->dev_sc, M_DEVBUF);
954 dev->dev_softc_alloc = 0;
958 device_get_softc(device_t dev)
963 return (dev->dev_sc);
967 device_is_attached(device_t dev)
969 return (dev->dev_attached);
973 device_set_desc(device_t dev, const char *desc)
975 snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc);
979 device_set_desc_copy(device_t dev, const char *desc)
981 device_set_desc(dev, desc);
985 devclass_get_softc(devclass_t dc, int unit)
987 return (device_get_softc(devclass_get_device(dc, unit)));
991 devclass_get_maxunit(devclass_t dc)
996 max_unit = DEVCLASS_MAXUNIT;
998 if (dc->dev_list[max_unit]) {
1008 devclass_get_device(devclass_t dc, int unit)
1010 return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ?
1011 NULL : dc->dev_list[unit]);
1015 devclass_find(const char *classname)
1017 const struct module_data *mod;
1019 TAILQ_FOREACH(mod, &module_head, entry) {
1020 if (devclass_equal(mod->driver->name, classname))
1021 return (mod->devclass_pp[0]);
1027 module_register(void *data)
1029 struct module_data *mdata = data;
1031 TAILQ_INSERT_TAIL(&module_head, mdata, entry);
1034 /*------------------------------------------------------------------------*
1036 *------------------------------------------------------------------------*/
1039 sysinit_run(const void **ppdata)
1041 const struct sysinit *psys;
1043 while ((psys = *ppdata) != NULL) {
1044 (psys->func) (psys->data);
1049 /*------------------------------------------------------------------------*
1051 *------------------------------------------------------------------------*/
1053 static int usb_do_process(struct usb_process *);
1054 static int usb_proc_level = -1;
1055 static struct mtx usb_proc_mtx;
1060 int old_level = usb_proc_level;
1061 int old_giant = Giant.owned;
1064 device_run_interrupts(usb_pci_root);
1070 while (++usb_proc_level < USB_PROC_MAX)
1071 worked |= usb_do_process(usb_process + usb_proc_level);
1073 usb_proc_level = old_level;
1074 Giant.owned = old_giant;
1082 sysinit_run(sysinit_data);
1088 sysinit_run(sysuninit_data);
1092 usb_process_init_sub(struct usb_process *up)
1094 TAILQ_INIT(&up->up_qhead);
1096 cv_init(&up->up_cv, "-");
1097 cv_init(&up->up_drain, "usbdrain");
1099 up->up_mtx = &usb_proc_mtx;
1103 usb_process_init(void *arg)
1107 mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE);
1109 for (x = 0; x != USB_PROC_MAX; x++)
1110 usb_process_init_sub(&usb_process[x]);
1113 SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL);
1116 usb_do_process(struct usb_process *up)
1118 struct usb_proc_msg *pm;
1121 mtx_lock(&usb_proc_mtx);
1124 pm = TAILQ_FIRST(&up->up_qhead);
1130 (pm->pm_callback) (pm);
1132 if (pm == TAILQ_FIRST(&up->up_qhead)) {
1133 /* nothing changed */
1134 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
1135 pm->pm_qentry.tqe_prev = NULL;
1139 mtx_unlock(&usb_proc_mtx);
1145 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
1147 struct usb_proc_msg *pm0 = _pm0;
1148 struct usb_proc_msg *pm1 = _pm1;
1149 struct usb_proc_msg *pm2;
1155 if (pm0->pm_qentry.tqe_prev) {
1158 if (pm1->pm_qentry.tqe_prev) {
1163 * No entries are queued. Queue "pm0" and use the existing
1167 } else if (t == 1) {
1168 /* Check if we need to increment the message number. */
1169 if (pm0->pm_num == up->up_msg_num) {
1173 } else if (t == 2) {
1174 /* Check if we need to increment the message number. */
1175 if (pm1->pm_num == up->up_msg_num) {
1179 } else if (t == 3) {
1181 * Both entries are queued. Re-queue the entry closest to
1184 d = (pm1->pm_num - pm0->pm_num);
1186 /* Check sign after subtraction */
1187 if (d & 0x80000000) {
1193 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
1195 pm2 = NULL; /* panic - should not happen */
1198 /* Put message last on queue */
1200 pm2->pm_num = up->up_msg_num;
1201 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
1206 /*------------------------------------------------------------------------*
1210 * 0: USB process is running
1211 * Else: USB process is tearing down
1212 *------------------------------------------------------------------------*/
1214 usb_proc_is_gone(struct usb_process *up)
1219 /*------------------------------------------------------------------------*
1222 * This function will return when the USB process message pointed to
1223 * by "pm" is no longer on a queue. This function must be called
1224 * having "usb_proc_mtx" locked.
1225 *------------------------------------------------------------------------*/
1227 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
1229 struct usb_proc_msg *pm0 = _pm0;
1230 struct usb_proc_msg *pm1 = _pm1;
1232 /* Just remove the messages from the queue. */
1233 if (pm0->pm_qentry.tqe_prev) {
1234 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
1235 pm0->pm_qentry.tqe_prev = NULL;
1237 if (pm1->pm_qentry.tqe_prev) {
1238 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
1239 pm1->pm_qentry.tqe_prev = NULL;
1243 /*------------------------------------------------------------------------*
1245 *------------------------------------------------------------------------*/
1247 #ifdef USB_PCI_PROBE_LIST
1248 static device_method_t pci_methods[] = {
1252 static driver_t pci_driver = {
1254 .methods = pci_methods,
1257 static devclass_t pci_devclass;
1259 DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0);
1261 static const char *usb_pci_devices[] = {
1265 #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *))
1267 static device_t usb_pci_dev[USB_PCI_USB_MAX];
1270 usb_pci_mod_load(void *arg)
1274 usb_pci_root = device_add_child(NULL, "pci", -1);
1275 if (usb_pci_root == NULL)
1278 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1279 usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1);
1280 if (usb_pci_dev[x] == NULL)
1282 if (device_probe_and_attach(usb_pci_dev[x])) {
1283 device_printf(usb_pci_dev[x],
1284 "WARNING: Probe and attach failed!\n");
1288 SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0);
1291 usb_pci_mod_unload(void *arg)
1295 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1296 if (usb_pci_dev[x]) {
1297 device_detach(usb_pci_dev[x]);
1298 device_delete_child(usb_pci_root, usb_pci_dev[x]);
1302 device_delete_child(NULL, usb_pci_root);
1304 SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0);
1307 /*------------------------------------------------------------------------*
1309 *------------------------------------------------------------------------*/
1312 #define USB_POOL_ALIGN 8
1314 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN);
1315 static uint32_t usb_pool_rem = USB_POOL_SIZE;
1316 static uint32_t usb_pool_entries;
1319 TAILQ_ENTRY(malloc_hdr) entry;
1321 } __aligned(USB_POOL_ALIGN);
1323 static TAILQ_HEAD(, malloc_hdr) malloc_head =
1324 TAILQ_HEAD_INITIALIZER(malloc_head);
1327 usb_malloc(unsigned long size)
1329 struct malloc_hdr *hdr;
1331 size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1);
1332 size += sizeof(struct malloc_hdr);
1334 TAILQ_FOREACH(hdr, &malloc_head, entry) {
1335 if (hdr->size == size)
1340 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1341 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1343 TAILQ_REMOVE(&malloc_head, hdr, entry);
1344 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1347 if (usb_pool_rem >= size) {
1348 hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem);
1351 usb_pool_rem -= size;
1354 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1355 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1357 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1366 struct malloc_hdr *hdr;
1374 TAILQ_INSERT_TAIL(&malloc_head, hdr, entry);
1379 usb_strdup(const char *str)
1384 len = 1 + strlen(str);
1386 tmp = malloc(len,XXX,XXX);
1390 memcpy(tmp, str, len);