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 *------------------------------------------------------------------------*/
40 mtx_system_init(void *arg)
42 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
44 SYSINIT(mtx_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, mtx_system_init, NULL);
47 mtx_init(struct mtx *mtx, const char *name, const char *type, int opt)
54 mtx_lock(struct mtx *mtx)
61 mtx_unlock(struct mtx *mtx)
68 mtx_owned(struct mtx *mtx)
71 return (mtx->owned != 0);
75 mtx_destroy(struct mtx *mtx)
80 /*------------------------------------------------------------------------*
81 * Implementation of shared/exclusive mutex API
82 *------------------------------------------------------------------------*/
85 sx_init_flags(struct sx *sx, const char *name, int flags)
91 sx_destroy(struct sx *sx)
97 sx_xlock(struct sx *sx)
103 sx_xunlock(struct sx *sx)
109 sx_xlocked(struct sx *sx)
111 return (sx->owned != 0);
114 /*------------------------------------------------------------------------*
115 * Implementaiton of condition variable API
116 *------------------------------------------------------------------------*/
119 cv_init(struct cv *cv, const char *desc)
125 cv_destroy(struct cv *cv)
131 cv_wait(struct cv *cv, struct mtx *mtx)
133 cv_timedwait(cv, mtx, -1);
137 cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
143 return (EWOULDBLOCK); /* not allowed */
147 while (cv->sleeping) {
149 delta = ticks - start;
150 if (delta >= timo || delta < 0)
162 return (EWOULDBLOCK); /* not allowed */
168 cv_signal(struct cv *cv)
174 cv_broadcast(struct cv *cv)
179 /*------------------------------------------------------------------------*
180 * Implementation of callout API
181 *------------------------------------------------------------------------*/
183 static void callout_proc_msg(struct usb_proc_msg *);
185 volatile int ticks = 0;
187 static LIST_HEAD(, callout) head_callout = LIST_HEAD_INITIALIZER(&head_callout);
189 static struct mtx mtx_callout;
190 static struct usb_proc_msg callout_msg[2];
193 callout_system_init(void *arg)
195 mtx_init(&mtx_callout, "callout-mtx", NULL, MTX_DEF | MTX_RECURSE);
197 callout_msg[0].pm_callback = &callout_proc_msg;
198 callout_msg[1].pm_callback = &callout_proc_msg;
200 SYSINIT(callout_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, callout_system_init, NULL);
203 callout_callback(struct callout *c)
207 mtx_lock(&mtx_callout);
208 if (c->entry.le_prev != NULL) {
209 LIST_REMOVE(c, entry);
210 c->entry.le_prev = NULL;
212 mtx_unlock(&mtx_callout);
217 if (!(c->flags & CALLOUT_RETURNUNLOCKED))
222 callout_process(int timeout)
225 usb_proc_msignal(usb_process + 2, &callout_msg[0], &callout_msg[1]);
229 callout_proc_msg(struct usb_proc_msg *pmsg)
235 mtx_lock(&mtx_callout);
237 LIST_FOREACH(c, &head_callout, entry) {
239 delta = c->timeout - ticks;
241 mtx_unlock(&mtx_callout);
248 mtx_unlock(&mtx_callout);
252 callout_init_mtx(struct callout *c, struct mtx *mtx, int flags)
254 memset(c, 0, sizeof(*c));
260 c->flags = (flags & CALLOUT_RETURNUNLOCKED);
264 callout_reset(struct callout *c, int to_ticks,
265 void (*func) (void *), void *arg)
271 c->timeout = ticks + to_ticks;
273 mtx_lock(&mtx_callout);
274 LIST_INSERT_HEAD(&head_callout, c, entry);
275 mtx_unlock(&mtx_callout);
279 callout_stop(struct callout *c)
281 mtx_lock(&mtx_callout);
283 if (c->entry.le_prev != NULL) {
284 LIST_REMOVE(c, entry);
285 c->entry.le_prev = NULL;
287 mtx_unlock(&mtx_callout);
294 callout_drain(struct callout *c)
297 return; /* not initialised */
305 callout_pending(struct callout *c)
309 mtx_lock(&mtx_callout);
310 retval = (c->entry.le_prev != NULL);
311 mtx_unlock(&mtx_callout);
316 /*------------------------------------------------------------------------*
317 * Implementation of device API
318 *------------------------------------------------------------------------*/
320 static const char unknown_string[] = { "unknown" };
322 static TAILQ_HEAD(, module_data) module_head =
323 TAILQ_HEAD_INITIALIZER(module_head);
326 devclass_equal(const char *a, const char *b)
347 bus_generic_resume(device_t dev)
353 bus_generic_shutdown(device_t dev)
359 bus_generic_suspend(device_t dev)
365 bus_generic_print_child(device_t dev, device_t child)
371 bus_generic_driver_added(device_t dev, driver_t *driver)
377 device_get_parent(device_t dev)
379 return (dev ? dev->dev_parent : NULL);
383 device_set_interrupt(device_t dev, intr_fn_t *fn, void *arg)
385 dev->dev_irq_fn = fn;
386 dev->dev_irq_arg = arg;
390 device_run_interrupts(device_t parent)
397 TAILQ_FOREACH(child, &parent->dev_children, dev_link) {
398 if (child->dev_irq_fn != NULL)
399 (child->dev_irq_fn) (child->dev_irq_arg);
404 device_set_ivars(device_t dev, void *ivars)
406 dev->dev_aux = ivars;
410 device_get_ivars(device_t dev)
412 return (dev ? dev->dev_aux : NULL);
416 device_get_unit(device_t dev)
418 return (dev ? dev->dev_unit : 0);
422 bus_generic_detach(device_t dev)
427 if (!dev->dev_attached)
430 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
431 if ((error = device_detach(child)) != 0)
438 device_get_nameunit(device_t dev)
440 if (dev && dev->dev_nameunit[0])
441 return (dev->dev_nameunit);
443 return (unknown_string);
447 devclass_create(devclass_t *dc_pp)
452 if (dc_pp[0] == NULL) {
453 dc_pp[0] = malloc(sizeof(**(dc_pp)),
454 M_DEVBUF, M_WAITOK | M_ZERO);
456 if (dc_pp[0] == NULL) {
463 static const struct module_data *
464 devclass_find_create(const char *classname)
466 const struct module_data *mod;
468 TAILQ_FOREACH(mod, &module_head, entry) {
469 if (devclass_equal(mod->mod_name, classname)) {
470 if (devclass_create(mod->devclass_pp)) {
480 devclass_add_device(const struct module_data *mod, device_t dev)
486 pp_dev = mod->devclass_pp[0]->dev_list;
487 end = pp_dev + DEVCLASS_MAXUNIT;
490 while (pp_dev != end) {
491 if (*pp_dev == NULL) {
493 dev->dev_unit = unit;
494 dev->dev_module = mod;
495 snprintf(dev->dev_nameunit,
496 sizeof(dev->dev_nameunit),
497 "%s%d", device_get_name(dev), unit);
503 DPRINTF("Could not add device to devclass.\n");
508 devclass_delete_device(const struct module_data *mod, device_t dev)
513 mod->devclass_pp[0]->dev_list[dev->dev_unit] = NULL;
514 dev->dev_module = NULL;
518 make_device(device_t parent, const char *name)
521 const struct module_data *mod = NULL;
525 mod = devclass_find_create(name);
529 DPRINTF("%s:%d:%s: can't find device "
530 "class %s\n", __FILE__, __LINE__,
536 dev = malloc(sizeof(*dev),
537 M_DEVBUF, M_WAITOK | M_ZERO);
542 dev->dev_parent = parent;
543 TAILQ_INIT(&dev->dev_children);
546 dev->dev_fixed_class = 1;
547 if (devclass_add_device(mod, dev)) {
562 device_add_child(device_t dev, const char *name, int unit)
567 device_printf(dev, "Unit is not -1\n");
569 child = make_device(dev, name);
571 device_printf(dev, "Could not add child '%s'\n", name);
578 TAILQ_INSERT_TAIL(&dev->dev_children, child, dev_link);
584 device_delete_child(device_t dev, device_t child)
589 /* remove children first */
591 while ((grandchild = TAILQ_FIRST(&child->dev_children))) {
592 error = device_delete_child(child, grandchild);
594 device_printf(dev, "Error deleting child!\n");
599 error = device_detach(child);
604 devclass_delete_device(child->dev_module, child);
607 /* remove child from parent */
608 TAILQ_REMOVE(&dev->dev_children, child, dev_link);
610 free(child, M_DEVBUF);
617 device_delete_children(device_t dev)
622 while ((child = TAILQ_FIRST(&dev->dev_children))) {
623 error = device_delete_child(dev, child);
625 device_printf(dev, "Error deleting child!\n");
633 device_quiet(device_t dev)
639 device_get_desc(device_t dev)
642 return &(dev->dev_desc[0]);
643 return (unknown_string);
650 DPRINTF("Default method called\n");
655 device_get_method(device_t dev, const char *what)
657 const struct device_method *mtod;
659 mtod = dev->dev_module->driver->methods;
660 while (mtod->func != NULL) {
661 if (devclass_equal(mtod->desc, what)) {
666 return ((void *)&default_method);
670 device_get_name(device_t dev)
673 return (unknown_string);
675 return (dev->dev_module->driver->name);
679 device_allocate_softc(device_t dev)
681 const struct module_data *mod;
683 mod = dev->dev_module;
685 if ((dev->dev_softc_alloc == 0) &&
686 (mod->driver->size != 0)) {
687 dev->dev_sc = malloc(mod->driver->size,
688 M_DEVBUF, M_WAITOK | M_ZERO);
690 if (dev->dev_sc == NULL)
693 dev->dev_softc_alloc = 1;
699 device_probe_and_attach(device_t dev)
701 const struct module_data *mod;
702 const char *bus_name_parent;
704 bus_name_parent = device_get_name(device_get_parent(dev));
706 if (dev->dev_attached)
707 return (0); /* fail-safe */
709 if (dev->dev_fixed_class) {
711 mod = dev->dev_module;
713 if (DEVICE_PROBE(dev) <= 0) {
715 if (device_allocate_softc(dev) == 0) {
717 if (DEVICE_ATTACH(dev) == 0) {
719 dev->dev_attached = 1;
729 * Else find a module for our device, if any
732 TAILQ_FOREACH(mod, &module_head, entry) {
733 if (devclass_equal(mod->bus_name, bus_name_parent)) {
734 if (devclass_create(mod->devclass_pp)) {
737 if (devclass_add_device(mod, dev)) {
740 if (DEVICE_PROBE(dev) <= 0) {
742 if (device_allocate_softc(dev) == 0) {
744 if (DEVICE_ATTACH(dev) == 0) {
746 dev->dev_attached = 1;
751 /* else try next driver */
762 device_detach(device_t dev)
764 const struct module_data *mod = dev->dev_module;
767 if (dev->dev_attached) {
769 error = DEVICE_DETACH(dev);
773 dev->dev_attached = 0;
775 device_set_softc(dev, NULL);
777 if (dev->dev_fixed_class == 0)
778 devclass_delete_device(mod, dev);
784 device_set_softc(device_t dev, void *softc)
786 if (dev->dev_softc_alloc) {
787 free(dev->dev_sc, M_DEVBUF);
791 dev->dev_softc_alloc = 0;
795 device_get_softc(device_t dev)
800 return (dev->dev_sc);
804 device_is_attached(device_t dev)
806 return (dev->dev_attached);
810 device_set_desc(device_t dev, const char *desc)
812 snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc);
816 device_set_desc_copy(device_t dev, const char *desc)
818 device_set_desc(dev, desc);
822 devclass_get_softc(devclass_t dc, int unit)
824 return (device_get_softc(devclass_get_device(dc, unit)));
828 devclass_get_maxunit(devclass_t dc)
833 max_unit = DEVCLASS_MAXUNIT;
835 if (dc->dev_list[max_unit]) {
845 devclass_get_device(devclass_t dc, int unit)
847 return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ?
848 NULL : dc->dev_list[unit]);
852 devclass_find(const char *classname)
854 const struct module_data *mod;
856 TAILQ_FOREACH(mod, &module_head, entry) {
857 if (devclass_equal(mod->mod_name, classname))
858 return (mod->devclass_pp[0]);
864 module_register(void *data)
866 struct module_data *mdata = data;
868 TAILQ_INSERT_TAIL(&module_head, mdata, entry);
871 /*------------------------------------------------------------------------*
873 *------------------------------------------------------------------------*/
876 sysinit_run(const void **ppdata)
878 const struct sysinit *psys;
880 while ((psys = *ppdata) != NULL) {
881 (psys->func) (psys->data);
886 /*------------------------------------------------------------------------*
888 *------------------------------------------------------------------------*/
890 static int usb_do_process(struct usb_process *);
891 static int usb_proc_level = -1;
892 static struct mtx usb_proc_mtx;
897 int old_level = usb_proc_level;
898 int old_giant = Giant.owned;
901 device_run_interrupts(usb_pci_root);
907 while (++usb_proc_level < USB_PROC_MAX)
908 worked |= usb_do_process(usb_process + usb_proc_level);
910 usb_proc_level = old_level;
911 Giant.owned = old_giant;
919 sysinit_run(sysinit_data);
925 sysinit_run(sysuninit_data);
929 usb_process_init_sub(struct usb_process *up)
931 TAILQ_INIT(&up->up_qhead);
933 cv_init(&up->up_cv, "-");
934 cv_init(&up->up_drain, "usbdrain");
936 up->up_mtx = &usb_proc_mtx;
940 usb_process_init(void *arg)
944 mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE);
946 for (x = 0; x != USB_PROC_MAX; x++)
947 usb_process_init_sub(&usb_process[x]);
950 SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL);
953 usb_do_process(struct usb_process *up)
955 struct usb_proc_msg *pm;
958 mtx_lock(&usb_proc_mtx);
961 pm = TAILQ_FIRST(&up->up_qhead);
967 (pm->pm_callback) (pm);
969 if (pm == TAILQ_FIRST(&up->up_qhead)) {
970 /* nothing changed */
971 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
972 pm->pm_qentry.tqe_prev = NULL;
976 mtx_unlock(&usb_proc_mtx);
982 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
984 struct usb_proc_msg *pm0 = _pm0;
985 struct usb_proc_msg *pm1 = _pm1;
986 struct usb_proc_msg *pm2;
992 if (pm0->pm_qentry.tqe_prev) {
995 if (pm1->pm_qentry.tqe_prev) {
1000 * No entries are queued. Queue "pm0" and use the existing
1004 } else if (t == 1) {
1005 /* Check if we need to increment the message number. */
1006 if (pm0->pm_num == up->up_msg_num) {
1010 } else if (t == 2) {
1011 /* Check if we need to increment the message number. */
1012 if (pm1->pm_num == up->up_msg_num) {
1016 } else if (t == 3) {
1018 * Both entries are queued. Re-queue the entry closest to
1021 d = (pm1->pm_num - pm0->pm_num);
1023 /* Check sign after subtraction */
1024 if (d & 0x80000000) {
1030 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
1032 pm2 = NULL; /* panic - should not happen */
1035 /* Put message last on queue */
1037 pm2->pm_num = up->up_msg_num;
1038 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
1043 /*------------------------------------------------------------------------*
1047 * 0: USB process is running
1048 * Else: USB process is tearing down
1049 *------------------------------------------------------------------------*/
1051 usb_proc_is_gone(struct usb_process *up)
1056 /*------------------------------------------------------------------------*
1059 * This function will return when the USB process message pointed to
1060 * by "pm" is no longer on a queue. This function must be called
1061 * having "usb_proc_mtx" locked.
1062 *------------------------------------------------------------------------*/
1064 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
1066 struct usb_proc_msg *pm0 = _pm0;
1067 struct usb_proc_msg *pm1 = _pm1;
1069 /* Just remove the messages from the queue. */
1070 if (pm0->pm_qentry.tqe_prev) {
1071 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
1072 pm0->pm_qentry.tqe_prev = NULL;
1074 if (pm1->pm_qentry.tqe_prev) {
1075 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
1076 pm1->pm_qentry.tqe_prev = NULL;
1080 /*------------------------------------------------------------------------*
1082 *------------------------------------------------------------------------*/
1084 static device_method_t pci_methods[] = {
1088 static driver_t pci_driver = {
1090 .methods = pci_methods,
1093 static devclass_t pci_devclass;
1095 DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0);
1097 static const char *usb_pci_devices[] = {
1098 #ifdef USB_PROBE_LIST
1103 #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *))
1105 static device_t usb_pci_dev[USB_PCI_USB_MAX];
1108 usb_pci_mod_load(void *arg)
1112 usb_pci_root = device_add_child(NULL, "pci", -1);
1113 if (usb_pci_root == NULL)
1116 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1117 usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1);
1118 if (usb_pci_dev[x] == NULL)
1120 if (device_probe_and_attach(usb_pci_dev[x])) {
1121 device_printf(usb_pci_dev[x],
1122 "WARNING: Probe and attach failed!\n");
1126 SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0);
1129 usb_pci_mod_unload(void *arg)
1133 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1134 if (usb_pci_dev[x]) {
1135 device_detach(usb_pci_dev[x]);
1136 device_delete_child(usb_pci_root, usb_pci_dev[x]);
1140 device_delete_child(NULL, usb_pci_root);
1142 SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0);
1144 /*------------------------------------------------------------------------*
1146 *------------------------------------------------------------------------*/
1148 #define USB_POOL_ALIGN 8
1150 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN);
1151 static uint32_t usb_pool_rem = USB_POOL_SIZE;
1152 static uint32_t usb_pool_entries;
1155 TAILQ_ENTRY(malloc_hdr) entry;
1157 } __aligned(USB_POOL_ALIGN);
1159 static TAILQ_HEAD(, malloc_hdr) malloc_head =
1160 TAILQ_HEAD_INITIALIZER(malloc_head);
1163 usb_malloc(unsigned long size)
1165 struct malloc_hdr *hdr;
1167 size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1);
1168 size += sizeof(struct malloc_hdr);
1170 TAILQ_FOREACH(hdr, &malloc_head, entry) {
1171 if (hdr->size == size)
1176 printf("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1177 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1179 TAILQ_REMOVE(&malloc_head, hdr, entry);
1180 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1183 if (usb_pool_rem >= size) {
1184 hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem);
1187 usb_pool_rem -= size;
1190 printf("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1191 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1193 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1202 struct malloc_hdr *hdr;
1210 TAILQ_INSERT_TAIL(&malloc_head, hdr, entry);
1214 usb_strdup(const char *str)
1219 len = 1 + strlen(str);
1221 tmp = usb_malloc(len);
1225 memcpy(tmp, str, len);