1 /*-
2 * Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 #include <bsd_global.h>
27
28 struct usb_process usb_process[USB_PROC_MAX];
29
30 static device_t usb_pci_root;
31
32 int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev,
33 int type, int *rid, unsigned int flags);
34 int (*ofw_bus_status_ok_cb)(device_t dev);
35 int (*ofw_bus_is_compatible_cb)(device_t dev, char *name);
36
37 /*------------------------------------------------------------------------*
38 * Implementation of busdma API
39 *------------------------------------------------------------------------*/
40 int
bus_dma_tag_create(bus_dma_tag_t parent,bus_size_t alignment,bus_size_t boundary,bus_addr_t lowaddr,bus_addr_t highaddr,bus_dma_filter_t * filter,void * filterarg,bus_size_t maxsize,int nsegments,bus_size_t maxsegsz,int flags,bus_dma_lock_t * lockfunc,void * lockfuncarg,bus_dma_tag_t * dmat)41 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
42 bus_size_t boundary, bus_addr_t lowaddr,
43 bus_addr_t highaddr, bus_dma_filter_t *filter,
44 void *filterarg, bus_size_t maxsize, int nsegments,
45 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
46 void *lockfuncarg, bus_dma_tag_t *dmat)
47 {
48 struct bus_dma_tag *ret;
49
50 ret = malloc(sizeof(struct bus_dma_tag), XXX, XXX);
51 if (*dmat == NULL)
52 return (ENOMEM);
53 ret->alignment = alignment;
54 ret->maxsize = maxsize;
55
56 *dmat = ret;
57
58 return (0);
59 }
60
61 int
bus_dmamem_alloc(bus_dma_tag_t dmat,void ** vaddr,int flags,bus_dmamap_t * mapp)62 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
63 bus_dmamap_t *mapp)
64 {
65 void *addr;
66
67 addr = malloc(dmat->maxsize + dmat->alignment, XXX, XXX);
68 if (addr == NULL)
69 return (ENOMEM);
70
71 *mapp = addr;
72 addr = (void*)(((uintptr_t)addr + dmat->alignment - 1) & ~(dmat->alignment - 1));
73
74 *vaddr = addr;
75 return (0);
76 }
77
78 int
bus_dmamap_load(bus_dma_tag_t dmat,bus_dmamap_t map,void * buf,bus_size_t buflen,bus_dmamap_callback_t * callback,void * callback_arg,int flags)79 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
80 bus_size_t buflen, bus_dmamap_callback_t *callback,
81 void *callback_arg, int flags)
82 {
83 bus_dma_segment_t segs[1];
84
85 segs[0].ds_addr = (uintptr_t)buf;
86 segs[0].ds_len = buflen;
87
88 (*callback)(callback_arg, segs, 1, 0);
89
90 return (0);
91 }
92
93 void
bus_dmamap_sync(bus_dma_tag_t dmat,bus_dmamap_t map,int flags)94 bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, int flags)
95 {
96 /* Assuming coherent memory */
97 __asm__ __volatile__("": : :"memory");
98 }
99
100 void
bus_dmamem_free(bus_dma_tag_t dmat,void * vaddr,bus_dmamap_t map)101 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
102 {
103
104 free(map, XXX);
105 }
106
107 int
bus_dma_tag_destroy(bus_dma_tag_t dmat)108 bus_dma_tag_destroy(bus_dma_tag_t dmat)
109 {
110
111 free(dmat, XXX);
112 return (0);
113 }
114
115 /*------------------------------------------------------------------------*
116 * Implementation of resource management API
117 *------------------------------------------------------------------------*/
118
119 struct resource *
bus_alloc_resource_any(device_t dev,int type,int * rid,unsigned int flags)120 bus_alloc_resource_any(device_t dev, int type, int *rid, unsigned int flags)
121 {
122 struct resource *res;
123 int ret = EINVAL;
124
125 res = malloc(sizeof(*res), XXX, XXX);
126 if (res == NULL)
127 return (NULL);
128
129 res->__r_i = malloc(sizeof(struct resource_i), XXX, XXX);
130 if (res->__r_i == NULL) {
131 free(res, XXX);
132 return (NULL);
133 }
134
135 if (bus_alloc_resource_any_cb != NULL)
136 ret = (*bus_alloc_resource_any_cb)(res, dev, type, rid, flags);
137 if (ret == 0)
138 return (res);
139
140 free(res->__r_i, XXX);
141 free(res, XXX);
142 return (NULL);
143 }
144
145 int
bus_alloc_resources(device_t dev,struct resource_spec * rs,struct resource ** res)146 bus_alloc_resources(device_t dev, struct resource_spec *rs,
147 struct resource **res)
148 {
149 int i;
150
151 for (i = 0; rs[i].type != -1; i++)
152 res[i] = NULL;
153 for (i = 0; rs[i].type != -1; i++) {
154 res[i] = bus_alloc_resource_any(dev,
155 rs[i].type, &rs[i].rid, rs[i].flags);
156 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
157 bus_release_resources(dev, rs, res);
158 return (ENXIO);
159 }
160 }
161 return (0);
162 }
163
164 void
bus_release_resources(device_t dev,const struct resource_spec * rs,struct resource ** res)165 bus_release_resources(device_t dev, const struct resource_spec *rs,
166 struct resource **res)
167 {
168 int i;
169
170 for (i = 0; rs[i].type != -1; i++)
171 if (res[i] != NULL) {
172 bus_release_resource(
173 dev, rs[i].type, rs[i].rid, res[i]);
174 res[i] = NULL;
175 }
176 }
177
178 int
bus_setup_intr(device_t dev,struct resource * r,int flags,driver_filter_t filter,driver_intr_t handler,void * arg,void ** cookiep)179 bus_setup_intr(device_t dev, struct resource *r, int flags,
180 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
181 {
182
183 dev->dev_irq_filter = filter;
184 dev->dev_irq_fn = handler;
185 dev->dev_irq_arg = arg;
186
187 return (0);
188 }
189
190 int
bus_teardown_intr(device_t dev,struct resource * r,void * cookie)191 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
192 {
193
194 dev->dev_irq_filter = NULL;
195 dev->dev_irq_fn = NULL;
196 dev->dev_irq_arg = NULL;
197
198 return (0);
199 }
200
201 int
bus_release_resource(device_t dev,int type,int rid,struct resource * r)202 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
203 {
204 /* Resource releasing is not supported */
205 return (EINVAL);
206 }
207
208 int
bus_generic_attach(device_t dev)209 bus_generic_attach(device_t dev)
210 {
211 device_t child;
212
213 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
214 device_probe_and_attach(child);
215 }
216
217 return (0);
218 }
219
220 bus_space_tag_t
rman_get_bustag(struct resource * r)221 rman_get_bustag(struct resource *r)
222 {
223
224 return (r->r_bustag);
225 }
226
227 bus_space_handle_t
rman_get_bushandle(struct resource * r)228 rman_get_bushandle(struct resource *r)
229 {
230
231 return (r->r_bushandle);
232 }
233
234 u_long
rman_get_size(struct resource * r)235 rman_get_size(struct resource *r)
236 {
237
238 return (r->__r_i->r_end - r->__r_i->r_start + 1);
239 }
240
241 int
ofw_bus_status_okay(device_t dev)242 ofw_bus_status_okay(device_t dev)
243 {
244 if (ofw_bus_status_ok_cb == NULL)
245 return (0);
246
247 return ((*ofw_bus_status_ok_cb)(dev));
248 }
249
250 int
ofw_bus_is_compatible(device_t dev,char * name)251 ofw_bus_is_compatible(device_t dev, char *name)
252 {
253 if (ofw_bus_is_compatible_cb == NULL)
254 return (0);
255
256 return ((*ofw_bus_is_compatible_cb)(dev, name));
257 }
258
259 /*------------------------------------------------------------------------*
260 * Implementation of mutex API
261 *------------------------------------------------------------------------*/
262
263 struct mtx Giant;
264
265 static void
mtx_system_init(void * arg)266 mtx_system_init(void *arg)
267 {
268 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
269 }
270 SYSINIT(mtx_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, mtx_system_init, NULL);
271
272 void
mtx_init(struct mtx * mtx,const char * name,const char * type,int opt)273 mtx_init(struct mtx *mtx, const char *name, const char *type, int opt)
274 {
275 mtx->owned = 0;
276 mtx->parent = mtx;
277 }
278
279 void
mtx_lock(struct mtx * mtx)280 mtx_lock(struct mtx *mtx)
281 {
282 mtx = mtx->parent;
283 mtx->owned++;
284 }
285
286 void
mtx_unlock(struct mtx * mtx)287 mtx_unlock(struct mtx *mtx)
288 {
289 mtx = mtx->parent;
290 mtx->owned--;
291 }
292
293 int
mtx_owned(struct mtx * mtx)294 mtx_owned(struct mtx *mtx)
295 {
296 mtx = mtx->parent;
297 return (mtx->owned != 0);
298 }
299
300 void
mtx_destroy(struct mtx * mtx)301 mtx_destroy(struct mtx *mtx)
302 {
303 /* NOP */
304 }
305
306 /*------------------------------------------------------------------------*
307 * Implementation of shared/exclusive mutex API
308 *------------------------------------------------------------------------*/
309
310 void
sx_init_flags(struct sx * sx,const char * name,int flags)311 sx_init_flags(struct sx *sx, const char *name, int flags)
312 {
313 sx->owned = 0;
314 }
315
316 void
sx_destroy(struct sx * sx)317 sx_destroy(struct sx *sx)
318 {
319 /* NOP */
320 }
321
322 void
sx_xlock(struct sx * sx)323 sx_xlock(struct sx *sx)
324 {
325 sx->owned++;
326 }
327
328 void
sx_xunlock(struct sx * sx)329 sx_xunlock(struct sx *sx)
330 {
331 sx->owned--;
332 }
333
334 int
sx_xlocked(struct sx * sx)335 sx_xlocked(struct sx *sx)
336 {
337 return (sx->owned != 0);
338 }
339
340 /*------------------------------------------------------------------------*
341 * Implementaiton of condition variable API
342 *------------------------------------------------------------------------*/
343
344 void
cv_init(struct cv * cv,const char * desc)345 cv_init(struct cv *cv, const char *desc)
346 {
347 cv->sleeping = 0;
348 }
349
350 void
cv_destroy(struct cv * cv)351 cv_destroy(struct cv *cv)
352 {
353 /* NOP */
354 }
355
356 void
cv_wait(struct cv * cv,struct mtx * mtx)357 cv_wait(struct cv *cv, struct mtx *mtx)
358 {
359 cv_timedwait(cv, mtx, -1);
360 }
361
362 int
cv_timedwait(struct cv * cv,struct mtx * mtx,int timo)363 cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
364 {
365 int start = ticks;
366 int delta;
367 int time = 0;
368
369 if (cv->sleeping)
370 return (EWOULDBLOCK); /* not allowed */
371
372 cv->sleeping = 1;
373
374 while (cv->sleeping) {
375 if (timo >= 0) {
376 delta = ticks - start;
377 if (delta >= timo || delta < 0)
378 break;
379 }
380 mtx_unlock(mtx);
381
382 usb_idle();
383
384 if (++time >= (1000000 / hz)) {
385 time = 0;
386 callout_process(1);
387 }
388
389 /* Sleep for 1 us */
390 delay(1);
391
392 mtx_lock(mtx);
393 }
394
395 if (cv->sleeping) {
396 cv->sleeping = 0;
397 return (EWOULDBLOCK); /* not allowed */
398 }
399 return (0);
400 }
401
402 void
cv_signal(struct cv * cv)403 cv_signal(struct cv *cv)
404 {
405 cv->sleeping = 0;
406 }
407
408 void
cv_broadcast(struct cv * cv)409 cv_broadcast(struct cv *cv)
410 {
411 cv->sleeping = 0;
412 }
413
414 /*------------------------------------------------------------------------*
415 * Implementation of callout API
416 *------------------------------------------------------------------------*/
417
418 static void callout_proc_msg(struct usb_proc_msg *);
419
420 volatile int ticks = 0;
421
422 static LIST_HEAD(, callout) head_callout = LIST_HEAD_INITIALIZER(&head_callout);
423
424 static struct mtx mtx_callout;
425 static struct usb_proc_msg callout_msg[2];
426
427 static void
callout_system_init(void * arg)428 callout_system_init(void *arg)
429 {
430 mtx_init(&mtx_callout, "callout-mtx", NULL, MTX_DEF | MTX_RECURSE);
431
432 callout_msg[0].pm_callback = &callout_proc_msg;
433 callout_msg[1].pm_callback = &callout_proc_msg;
434 }
435 SYSINIT(callout_system_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, callout_system_init, NULL);
436
437 static void
callout_callback(struct callout * c)438 callout_callback(struct callout *c)
439 {
440 mtx_lock(c->mtx);
441
442 mtx_lock(&mtx_callout);
443 if (c->entry.le_prev != NULL) {
444 LIST_REMOVE(c, entry);
445 c->entry.le_prev = NULL;
446 }
447 mtx_unlock(&mtx_callout);
448
449 if (c->c_func != NULL)
450 (c->c_func) (c->c_arg);
451
452 if (!(c->flags & CALLOUT_RETURNUNLOCKED))
453 mtx_unlock(c->mtx);
454 }
455
456 void
callout_process(int timeout)457 callout_process(int timeout)
458 {
459 ticks += timeout;
460 usb_proc_msignal(usb_process + 2, &callout_msg[0], &callout_msg[1]);
461 }
462
463 static void
callout_proc_msg(struct usb_proc_msg * pmsg)464 callout_proc_msg(struct usb_proc_msg *pmsg)
465 {
466 struct callout *c;
467 int delta;
468
469 repeat:
470 mtx_lock(&mtx_callout);
471
472 LIST_FOREACH(c, &head_callout, entry) {
473
474 delta = c->timeout - ticks;
475 if (delta < 0) {
476 mtx_unlock(&mtx_callout);
477
478 callout_callback(c);
479
480 goto repeat;
481 }
482 }
483 mtx_unlock(&mtx_callout);
484 }
485
486 void
callout_init_mtx(struct callout * c,struct mtx * mtx,int flags)487 callout_init_mtx(struct callout *c, struct mtx *mtx, int flags)
488 {
489 memset(c, 0, sizeof(*c));
490
491 if (mtx == NULL)
492 mtx = &Giant;
493
494 c->mtx = mtx;
495 c->flags = (flags & CALLOUT_RETURNUNLOCKED);
496 }
497
498 void
callout_reset(struct callout * c,int to_ticks,void (* func)(void *),void * arg)499 callout_reset(struct callout *c, int to_ticks,
500 void (*func) (void *), void *arg)
501 {
502 callout_stop(c);
503
504 c->c_func = func;
505 c->c_arg = arg;
506 c->timeout = ticks + to_ticks;
507
508 mtx_lock(&mtx_callout);
509 LIST_INSERT_HEAD(&head_callout, c, entry);
510 mtx_unlock(&mtx_callout);
511 }
512
513 void
callout_stop(struct callout * c)514 callout_stop(struct callout *c)
515 {
516 mtx_lock(&mtx_callout);
517
518 if (c->entry.le_prev != NULL) {
519 LIST_REMOVE(c, entry);
520 c->entry.le_prev = NULL;
521 }
522 mtx_unlock(&mtx_callout);
523
524 c->c_func = NULL;
525 c->c_arg = NULL;
526 }
527
528 void
callout_drain(struct callout * c)529 callout_drain(struct callout *c)
530 {
531 if (c->mtx == NULL)
532 return; /* not initialised */
533
534 mtx_lock(c->mtx);
535 callout_stop(c);
536 mtx_unlock(c->mtx);
537 }
538
539 int
callout_pending(struct callout * c)540 callout_pending(struct callout *c)
541 {
542 int retval;
543
544 mtx_lock(&mtx_callout);
545 retval = (c->entry.le_prev != NULL);
546 mtx_unlock(&mtx_callout);
547
548 return (retval);
549 }
550
551 /*------------------------------------------------------------------------*
552 * Implementation of device API
553 *------------------------------------------------------------------------*/
554
555 static const char unknown_string[] = { "unknown" };
556
557 static TAILQ_HEAD(, module_data) module_head =
558 TAILQ_HEAD_INITIALIZER(module_head);
559 static TAILQ_HEAD(, devclass) devclasses =
560 TAILQ_HEAD_INITIALIZER(devclasses);
561
562 int
bus_generic_resume(device_t dev)563 bus_generic_resume(device_t dev)
564 {
565 return (0);
566 }
567
568 int
bus_generic_shutdown(device_t dev)569 bus_generic_shutdown(device_t dev)
570 {
571 return (0);
572 }
573
574 int
bus_generic_suspend(device_t dev)575 bus_generic_suspend(device_t dev)
576 {
577 return (0);
578 }
579
580 int
bus_generic_print_child(device_t dev,device_t child)581 bus_generic_print_child(device_t dev, device_t child)
582 {
583 return (0);
584 }
585
586 void
bus_generic_driver_added(device_t dev,driver_t * driver)587 bus_generic_driver_added(device_t dev, driver_t *driver)
588 {
589 return;
590 }
591
592 device_t
device_get_parent(device_t dev)593 device_get_parent(device_t dev)
594 {
595 return (dev ? dev->dev_parent : NULL);
596 }
597
598 void
device_set_interrupt(device_t dev,driver_filter_t * filter,driver_intr_t * fn,void * arg)599 device_set_interrupt(device_t dev, driver_filter_t *filter,
600 driver_intr_t *fn, void *arg)
601 {
602 dev->dev_irq_filter = filter;
603 dev->dev_irq_fn = fn;
604 dev->dev_irq_arg = arg;
605 }
606
607 void
device_run_interrupts(device_t parent)608 device_run_interrupts(device_t parent)
609 {
610 device_t child;
611
612 if (parent == NULL)
613 return;
614
615 TAILQ_FOREACH(child, &parent->dev_children, dev_link) {
616 int status;
617 if (child->dev_irq_filter != NULL)
618 status = child->dev_irq_filter(child->dev_irq_arg);
619 else
620 status = FILTER_SCHEDULE_THREAD;
621
622 if (status == FILTER_SCHEDULE_THREAD) {
623 if (child->dev_irq_fn != NULL)
624 (child->dev_irq_fn) (child->dev_irq_arg);
625 }
626 }
627 }
628
629 void
device_set_ivars(device_t dev,void * ivars)630 device_set_ivars(device_t dev, void *ivars)
631 {
632 dev->dev_aux = ivars;
633 }
634
635 void *
device_get_ivars(device_t dev)636 device_get_ivars(device_t dev)
637 {
638 return (dev ? dev->dev_aux : NULL);
639 }
640
641 int
device_get_unit(device_t dev)642 device_get_unit(device_t dev)
643 {
644 return (dev ? dev->dev_unit : 0);
645 }
646
647 int
bus_generic_detach(device_t dev)648 bus_generic_detach(device_t dev)
649 {
650 device_t child;
651 int error;
652
653 if (!dev->dev_attached)
654 return (EBUSY);
655
656 TAILQ_FOREACH(child, &dev->dev_children, dev_link) {
657 if ((error = device_detach(child)) != 0)
658 return (error);
659 }
660 return (0);
661 }
662
663 const char *
device_get_nameunit(device_t dev)664 device_get_nameunit(device_t dev)
665 {
666 if (dev && dev->dev_nameunit[0])
667 return (dev->dev_nameunit);
668
669 return (unknown_string);
670 }
671
672 static devclass_t
devclass_create(const char * classname)673 devclass_create(const char *classname)
674 {
675 devclass_t dc;
676
677 dc = malloc(sizeof(*dc), M_DEVBUF, M_WAITOK | M_ZERO);
678 if (dc == NULL) {
679 return (NULL);
680 }
681 dc->name = classname;
682 TAILQ_INSERT_TAIL(&devclasses, dc, link);
683 return (dc);
684 }
685
686 static devclass_t
devclass_find_create(const char * classname)687 devclass_find_create(const char *classname)
688 {
689 devclass_t dc;
690
691 dc = devclass_find(classname);
692 if (dc == NULL)
693 dc = devclass_create(classname);
694 return (dc);
695 }
696
697 static uint8_t
devclass_add_device(devclass_t dc,device_t dev)698 devclass_add_device(devclass_t dc, device_t dev)
699 {
700 device_t *pp_dev;
701 device_t *end;
702 uint8_t unit;
703
704 pp_dev = dc->dev_list;
705 end = pp_dev + DEVCLASS_MAXUNIT;
706 unit = 0;
707
708 while (pp_dev != end) {
709 if (*pp_dev == NULL) {
710 *pp_dev = dev;
711 dev->dev_class = dc;
712 dev->dev_unit = unit;
713 snprintf(dev->dev_nameunit,
714 sizeof(dev->dev_nameunit),
715 "%s%d", dc->name, unit);
716 return (0);
717 }
718 pp_dev++;
719 unit++;
720 }
721 DPRINTF("Could not add device to devclass.\n");
722 return (1);
723 }
724
725 static void
devclass_delete_device(devclass_t dc,device_t dev)726 devclass_delete_device(devclass_t dc, device_t dev)
727 {
728 if (dc == NULL) {
729 return;
730 }
731 dc->dev_list[dev->dev_unit] = NULL;
732 dev->dev_class = NULL;
733 }
734
735 static device_t
make_device(device_t parent,const char * name)736 make_device(device_t parent, const char *name)
737 {
738 device_t dev = NULL;
739 devclass_t dc = NULL;
740
741 if (name) {
742
743 dc = devclass_find_create(name);
744
745 if (!dc) {
746
747 DPRINTF("%s:%d:%s: can't find device "
748 "class %s\n", __FILE__, __LINE__,
749 __FUNCTION__, name);
750
751 goto done;
752 }
753 }
754 dev = malloc(sizeof(*dev),
755 M_DEVBUF, M_WAITOK | M_ZERO);
756
757 if (dev == NULL)
758 goto done;
759
760 dev->dev_parent = parent;
761 TAILQ_INIT(&dev->dev_children);
762
763 if (name) {
764 dev->dev_fixed_class = 1;
765 if (devclass_add_device(dc, dev)) {
766 goto error;
767 }
768 }
769 done:
770 return (dev);
771
772 error:
773 if (dev) {
774 free(dev, M_DEVBUF);
775 }
776 return (NULL);
777 }
778
779 device_t
device_add_child(device_t dev,const char * name,int unit)780 device_add_child(device_t dev, const char *name, int unit)
781 {
782 device_t child;
783
784 if (unit != -1) {
785 device_printf(dev, "Unit is not -1\n");
786 }
787 child = make_device(dev, name);
788 if (child == NULL) {
789 device_printf(dev, "Could not add child '%s'\n", name);
790 goto done;
791 }
792 if (dev == NULL) {
793 /* no parent */
794 goto done;
795 }
796 TAILQ_INSERT_TAIL(&dev->dev_children, child, dev_link);
797 done:
798 return (child);
799 }
800
801 int
device_delete_child(device_t dev,device_t child)802 device_delete_child(device_t dev, device_t child)
803 {
804 int error = 0;
805 device_t grandchild;
806
807 /* detach parent before deleting children, if any */
808 error = device_detach(child);
809 if (error)
810 goto done;
811
812 /* remove children second */
813 while ((grandchild = TAILQ_FIRST(&child->dev_children))) {
814 error = device_delete_child(child, grandchild);
815 if (error) {
816 device_printf(dev, "Error deleting child!\n");
817 goto done;
818 }
819 }
820
821 if (child->dev_class != NULL)
822 devclass_delete_device(child->dev_class, child);
823
824 if (dev != NULL) {
825 /* remove child from parent */
826 TAILQ_REMOVE(&dev->dev_children, child, dev_link);
827 }
828 free(child, M_DEVBUF);
829
830 done:
831 return (error);
832 }
833
834 int
device_delete_children(device_t dev)835 device_delete_children(device_t dev)
836 {
837 device_t child;
838 int error = 0;
839
840 while ((child = TAILQ_FIRST(&dev->dev_children))) {
841 error = device_delete_child(dev, child);
842 if (error) {
843 device_printf(dev, "Error deleting child!\n");
844 break;
845 }
846 }
847 return (error);
848 }
849
850 void
device_quiet(device_t dev)851 device_quiet(device_t dev)
852 {
853 dev->dev_quiet = 1;
854 }
855
856 const char *
device_get_desc(device_t dev)857 device_get_desc(device_t dev)
858 {
859 if (dev)
860 return &(dev->dev_desc[0]);
861 return (unknown_string);
862 }
863
864 static int
default_method(void)865 default_method(void)
866 {
867 /* do nothing */
868 DPRINTF("Default method called\n");
869 return (0);
870 }
871
872 void *
device_get_method(device_t dev,const char * what)873 device_get_method(device_t dev, const char *what)
874 {
875 const struct device_method *mtod;
876
877 mtod = dev->dev_module->driver->methods;
878 while (mtod->func != NULL) {
879 if (strcmp(mtod->desc, what) == 0) {
880 return (mtod->func);
881 }
882 mtod++;
883 }
884 return ((void *)&default_method);
885 }
886
887 const char *
device_get_name(device_t dev)888 device_get_name(device_t dev)
889 {
890 if (dev == NULL || dev->dev_module == NULL)
891 return (unknown_string);
892
893 return (dev->dev_module->driver->name);
894 }
895
896 static int
device_allocate_softc(device_t dev)897 device_allocate_softc(device_t dev)
898 {
899 const struct module_data *mod;
900
901 mod = dev->dev_module;
902
903 if ((dev->dev_softc_alloc == 0) &&
904 (mod->driver->size != 0)) {
905 dev->dev_sc = malloc(mod->driver->size,
906 M_DEVBUF, M_WAITOK | M_ZERO);
907
908 if (dev->dev_sc == NULL)
909 return (ENOMEM);
910
911 dev->dev_softc_alloc = 1;
912 }
913 return (0);
914 }
915
916 int
device_probe_and_attach(device_t dev)917 device_probe_and_attach(device_t dev)
918 {
919 const struct module_data *mod;
920 const char *bus_name_parent;
921 devclass_t dc;
922
923 if (dev->dev_attached)
924 return (0); /* fail-safe */
925
926 /*
927 * Find a module for our device, if any
928 */
929 bus_name_parent = device_get_name(device_get_parent(dev));
930
931 TAILQ_FOREACH(mod, &module_head, entry) {
932 if (strcmp(mod->bus_name, bus_name_parent) != 0)
933 continue;
934
935 dc = devclass_find(mod->mod_name);
936
937 /* Does this device need assigning to the new devclass? */
938 if (dev->dev_class != dc) {
939 if (dev->dev_fixed_class)
940 continue;
941 if (dev->dev_class != NULL)
942 devclass_delete_device(dev->dev_class, dev);
943 if (devclass_add_device(dc, dev)) {
944 continue;
945 }
946 }
947
948 dev->dev_module = mod;
949 if (DEVICE_PROBE(dev) <= 0) {
950
951 if (device_allocate_softc(dev) == 0) {
952
953 if (DEVICE_ATTACH(dev) == 0) {
954 /* success */
955 dev->dev_attached = 1;
956 return (0);
957 }
958 }
959 }
960 /* else try next driver */
961
962 device_detach(dev);
963 }
964
965 return (ENODEV);
966 }
967
968 int
device_detach(device_t dev)969 device_detach(device_t dev)
970 {
971 const struct module_data *mod = dev->dev_module;
972 int error;
973
974 if (dev->dev_attached) {
975
976 error = DEVICE_DETACH(dev);
977 if (error) {
978 return error;
979 }
980 dev->dev_attached = 0;
981 }
982 device_set_softc(dev, NULL);
983 dev->dev_module = NULL;
984
985 if (dev->dev_fixed_class == 0)
986 devclass_delete_device(dev->dev_class, dev);
987
988 return (0);
989 }
990
991 void
device_set_softc(device_t dev,void * softc)992 device_set_softc(device_t dev, void *softc)
993 {
994 if (dev->dev_softc_alloc) {
995 free(dev->dev_sc, M_DEVBUF);
996 dev->dev_sc = NULL;
997 }
998 dev->dev_sc = softc;
999 dev->dev_softc_alloc = 0;
1000 }
1001
1002 void *
device_get_softc(device_t dev)1003 device_get_softc(device_t dev)
1004 {
1005 if (dev == NULL)
1006 return (NULL);
1007
1008 return (dev->dev_sc);
1009 }
1010
1011 int
device_is_attached(device_t dev)1012 device_is_attached(device_t dev)
1013 {
1014 return (dev->dev_attached);
1015 }
1016
1017 void
device_set_desc(device_t dev,const char * desc)1018 device_set_desc(device_t dev, const char *desc)
1019 {
1020 snprintf(dev->dev_desc, sizeof(dev->dev_desc), "%s", desc);
1021 }
1022
1023 void
device_set_desc_copy(device_t dev,const char * desc)1024 device_set_desc_copy(device_t dev, const char *desc)
1025 {
1026 device_set_desc(dev, desc);
1027 }
1028
1029 void *
devclass_get_softc(devclass_t dc,int unit)1030 devclass_get_softc(devclass_t dc, int unit)
1031 {
1032 return (device_get_softc(devclass_get_device(dc, unit)));
1033 }
1034
1035 int
devclass_get_maxunit(devclass_t dc)1036 devclass_get_maxunit(devclass_t dc)
1037 {
1038 int max_unit = 0;
1039
1040 if (dc) {
1041 max_unit = DEVCLASS_MAXUNIT;
1042 while (max_unit--) {
1043 if (dc->dev_list[max_unit]) {
1044 break;
1045 }
1046 }
1047 max_unit++;
1048 }
1049 return (max_unit);
1050 }
1051
1052 device_t
devclass_get_device(devclass_t dc,int unit)1053 devclass_get_device(devclass_t dc, int unit)
1054 {
1055 return (((unit < 0) || (unit >= DEVCLASS_MAXUNIT) || (dc == NULL)) ?
1056 NULL : dc->dev_list[unit]);
1057 }
1058
1059 devclass_t
devclass_find(const char * classname)1060 devclass_find(const char *classname)
1061 {
1062 devclass_t dc;
1063
1064 TAILQ_FOREACH(dc, &devclasses, link) {
1065 if (strcmp(dc->name, classname) == 0)
1066 return (dc);
1067 }
1068 return (NULL);
1069 }
1070
1071 void
module_register(void * data)1072 module_register(void *data)
1073 {
1074 struct module_data *mdata = data;
1075
1076 TAILQ_INSERT_TAIL(&module_head, mdata, entry);
1077 (void)devclass_find_create(mdata->mod_name);
1078 }
1079
1080 /*------------------------------------------------------------------------*
1081 * System startup
1082 *------------------------------------------------------------------------*/
1083
1084 static void
sysinit_run(const void ** ppdata)1085 sysinit_run(const void **ppdata)
1086 {
1087 const struct sysinit *psys;
1088
1089 while ((psys = *ppdata) != NULL) {
1090 (psys->func) (psys->data);
1091 ppdata++;
1092 }
1093 }
1094
1095 /*------------------------------------------------------------------------*
1096 * USB process API
1097 *------------------------------------------------------------------------*/
1098
1099 static int usb_do_process(struct usb_process *);
1100 static int usb_proc_level = -1;
1101 static struct mtx usb_proc_mtx;
1102
1103 void
usb_idle(void)1104 usb_idle(void)
1105 {
1106 int old_level = usb_proc_level;
1107 int old_giant = Giant.owned;
1108 int worked;
1109
1110 device_run_interrupts(usb_pci_root);
1111
1112 do {
1113 worked = 0;
1114 Giant.owned = 0;
1115
1116 while (++usb_proc_level < USB_PROC_MAX)
1117 worked |= usb_do_process(usb_process + usb_proc_level);
1118
1119 usb_proc_level = old_level;
1120 Giant.owned = old_giant;
1121
1122 } while (worked);
1123 }
1124
1125 void
usb_init(void)1126 usb_init(void)
1127 {
1128 sysinit_run(sysinit_data);
1129 }
1130
1131 void
usb_uninit(void)1132 usb_uninit(void)
1133 {
1134 sysinit_run(sysuninit_data);
1135 }
1136
1137 static void
usb_process_init_sub(struct usb_process * up)1138 usb_process_init_sub(struct usb_process *up)
1139 {
1140 TAILQ_INIT(&up->up_qhead);
1141
1142 cv_init(&up->up_cv, "-");
1143 cv_init(&up->up_drain, "usbdrain");
1144
1145 up->up_mtx = &usb_proc_mtx;
1146 }
1147
1148 static void
usb_process_init(void * arg)1149 usb_process_init(void *arg)
1150 {
1151 uint8_t x;
1152
1153 mtx_init(&usb_proc_mtx, "usb-proc-mtx", NULL, MTX_DEF | MTX_RECURSE);
1154
1155 for (x = 0; x != USB_PROC_MAX; x++)
1156 usb_process_init_sub(&usb_process[x]);
1157
1158 }
1159 SYSINIT(usb_process_init, SI_SUB_LOCK, SI_ORDER_MIDDLE, usb_process_init, NULL);
1160
1161 static int
usb_do_process(struct usb_process * up)1162 usb_do_process(struct usb_process *up)
1163 {
1164 struct usb_proc_msg *pm;
1165 int worked = 0;
1166
1167 mtx_lock(&usb_proc_mtx);
1168
1169 repeat:
1170 pm = TAILQ_FIRST(&up->up_qhead);
1171
1172 if (pm != NULL) {
1173
1174 worked = 1;
1175
1176 (pm->pm_callback) (pm);
1177
1178 if (pm == TAILQ_FIRST(&up->up_qhead)) {
1179 /* nothing changed */
1180 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
1181 pm->pm_qentry.tqe_prev = NULL;
1182 }
1183 goto repeat;
1184 }
1185 mtx_unlock(&usb_proc_mtx);
1186
1187 return (worked);
1188 }
1189
1190 void *
usb_proc_msignal(struct usb_process * up,void * _pm0,void * _pm1)1191 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
1192 {
1193 struct usb_proc_msg *pm0 = _pm0;
1194 struct usb_proc_msg *pm1 = _pm1;
1195 struct usb_proc_msg *pm2;
1196 usb_size_t d;
1197 uint8_t t;
1198
1199 t = 0;
1200
1201 if (pm0->pm_qentry.tqe_prev) {
1202 t |= 1;
1203 }
1204 if (pm1->pm_qentry.tqe_prev) {
1205 t |= 2;
1206 }
1207 if (t == 0) {
1208 /*
1209 * No entries are queued. Queue "pm0" and use the existing
1210 * message number.
1211 */
1212 pm2 = pm0;
1213 } else if (t == 1) {
1214 /* Check if we need to increment the message number. */
1215 if (pm0->pm_num == up->up_msg_num) {
1216 up->up_msg_num++;
1217 }
1218 pm2 = pm1;
1219 } else if (t == 2) {
1220 /* Check if we need to increment the message number. */
1221 if (pm1->pm_num == up->up_msg_num) {
1222 up->up_msg_num++;
1223 }
1224 pm2 = pm0;
1225 } else if (t == 3) {
1226 /*
1227 * Both entries are queued. Re-queue the entry closest to
1228 * the end.
1229 */
1230 d = (pm1->pm_num - pm0->pm_num);
1231
1232 /* Check sign after subtraction */
1233 if (d & 0x80000000) {
1234 pm2 = pm0;
1235 } else {
1236 pm2 = pm1;
1237 }
1238
1239 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
1240 } else {
1241 pm2 = NULL; /* panic - should not happen */
1242 }
1243
1244 /* Put message last on queue */
1245
1246 pm2->pm_num = up->up_msg_num;
1247 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
1248
1249 return (pm2);
1250 }
1251
1252 /*------------------------------------------------------------------------*
1253 * usb_proc_is_gone
1254 *
1255 * Return values:
1256 * 0: USB process is running
1257 * Else: USB process is tearing down
1258 *------------------------------------------------------------------------*/
1259 uint8_t
usb_proc_is_gone(struct usb_process * up)1260 usb_proc_is_gone(struct usb_process *up)
1261 {
1262 return (0);
1263 }
1264
1265 /*------------------------------------------------------------------------*
1266 * usb_proc_mwait
1267 *
1268 * This function will return when the USB process message pointed to
1269 * by "pm" is no longer on a queue. This function must be called
1270 * having "usb_proc_mtx" locked.
1271 *------------------------------------------------------------------------*/
1272 void
usb_proc_mwait(struct usb_process * up,void * _pm0,void * _pm1)1273 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
1274 {
1275 struct usb_proc_msg *pm0 = _pm0;
1276 struct usb_proc_msg *pm1 = _pm1;
1277
1278 /* Just remove the messages from the queue. */
1279 if (pm0->pm_qentry.tqe_prev) {
1280 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
1281 pm0->pm_qentry.tqe_prev = NULL;
1282 }
1283 if (pm1->pm_qentry.tqe_prev) {
1284 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
1285 pm1->pm_qentry.tqe_prev = NULL;
1286 }
1287 }
1288
1289 /*------------------------------------------------------------------------*
1290 * SYSTEM attach
1291 *------------------------------------------------------------------------*/
1292
1293 #ifdef USB_PCI_PROBE_LIST
1294 static device_method_t pci_methods[] = {
1295 DEVMETHOD_END
1296 };
1297
1298 static driver_t pci_driver = {
1299 .name = "pci",
1300 .methods = pci_methods,
1301 };
1302
1303 static devclass_t pci_devclass;
1304
1305 DRIVER_MODULE(pci, pci, pci_driver, pci_devclass, 0, 0);
1306
1307 static const char *usb_pci_devices[] = {
1308 USB_PCI_PROBE_LIST
1309 };
1310
1311 #define USB_PCI_USB_MAX (sizeof(usb_pci_devices) / sizeof(void *))
1312
1313 static device_t usb_pci_dev[USB_PCI_USB_MAX];
1314
1315 static void
usb_pci_mod_load(void * arg)1316 usb_pci_mod_load(void *arg)
1317 {
1318 uint32_t x;
1319
1320 usb_pci_root = device_add_child(NULL, "pci", -1);
1321 if (usb_pci_root == NULL)
1322 return;
1323
1324 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1325 usb_pci_dev[x] = device_add_child(usb_pci_root, usb_pci_devices[x], -1);
1326 if (usb_pci_dev[x] == NULL)
1327 continue;
1328 if (device_probe_and_attach(usb_pci_dev[x])) {
1329 device_printf(usb_pci_dev[x],
1330 "WARNING: Probe and attach failed!\n");
1331 }
1332 }
1333 }
1334 SYSINIT(usb_pci_mod_load, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_load, 0);
1335
1336 static void
usb_pci_mod_unload(void * arg)1337 usb_pci_mod_unload(void *arg)
1338 {
1339 uint32_t x;
1340
1341 for (x = 0; x != USB_PCI_USB_MAX; x++) {
1342 if (usb_pci_dev[x]) {
1343 device_detach(usb_pci_dev[x]);
1344 device_delete_child(usb_pci_root, usb_pci_dev[x]);
1345 }
1346 }
1347 if (usb_pci_root)
1348 device_delete_child(NULL, usb_pci_root);
1349 }
1350 SYSUNINIT(usb_pci_mod_unload, SI_SUB_RUN_SCHEDULER, SI_ORDER_MIDDLE, usb_pci_mod_unload, 0);
1351 #endif
1352
1353 /*------------------------------------------------------------------------*
1354 * MALLOC API
1355 *------------------------------------------------------------------------*/
1356
1357 #ifndef HAVE_MALLOC
1358 #define USB_POOL_ALIGN 8
1359
1360 static uint8_t usb_pool[USB_POOL_SIZE] __aligned(USB_POOL_ALIGN);
1361 static uint32_t usb_pool_rem = USB_POOL_SIZE;
1362 static uint32_t usb_pool_entries;
1363
1364 struct malloc_hdr {
1365 TAILQ_ENTRY(malloc_hdr) entry;
1366 uint32_t size;
1367 } __aligned(USB_POOL_ALIGN);
1368
1369 static TAILQ_HEAD(, malloc_hdr) malloc_head =
1370 TAILQ_HEAD_INITIALIZER(malloc_head);
1371
1372 void *
usb_malloc(unsigned long size)1373 usb_malloc(unsigned long size)
1374 {
1375 struct malloc_hdr *hdr;
1376
1377 size = (size + USB_POOL_ALIGN - 1) & ~(USB_POOL_ALIGN - 1);
1378 size += sizeof(struct malloc_hdr);
1379
1380 TAILQ_FOREACH(hdr, &malloc_head, entry) {
1381 if (hdr->size == size)
1382 break;
1383 }
1384
1385 if (hdr) {
1386 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1387 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1388
1389 TAILQ_REMOVE(&malloc_head, hdr, entry);
1390 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1391 return (hdr + 1);
1392 }
1393 if (usb_pool_rem >= size) {
1394 hdr = (void *)(usb_pool + USB_POOL_SIZE - usb_pool_rem);
1395 hdr->size = size;
1396
1397 usb_pool_rem -= size;
1398 usb_pool_entries++;
1399
1400 DPRINTF("MALLOC: Entries = %d; Remainder = %d; Size = %d\n",
1401 (int)usb_pool_entries, (int)usb_pool_rem, (int)size);
1402
1403 memset(hdr + 1, 0, hdr->size - sizeof(*hdr));
1404 return (hdr + 1);
1405 }
1406 return (NULL);
1407 }
1408
1409 void
usb_free(void * arg)1410 usb_free(void *arg)
1411 {
1412 struct malloc_hdr *hdr;
1413
1414 if (arg == NULL)
1415 return;
1416
1417 hdr = arg;
1418 hdr--;
1419
1420 TAILQ_INSERT_TAIL(&malloc_head, hdr, entry);
1421 }
1422 #endif
1423
1424 char *
usb_strdup(const char * str)1425 usb_strdup(const char *str)
1426 {
1427 char *tmp;
1428 int len;
1429
1430 len = 1 + strlen(str);
1431
1432 tmp = malloc(len,XXX,XXX);
1433 if (tmp == NULL)
1434 return (NULL);
1435
1436 memcpy(tmp, str, len);
1437 return (tmp);
1438 }
1439