xref: /freebsd/stand/usb/test/bsd_usbloader_test.c (revision 05890ca018513401980415f735eca8c94aaa644a)
1*05890ca0SHans Petter Selasky /* $FreeBSD$ */
2*05890ca0SHans Petter Selasky /*-
3*05890ca0SHans Petter Selasky  * Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
4*05890ca0SHans Petter Selasky  *
5*05890ca0SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
6*05890ca0SHans Petter Selasky  * modification, are permitted provided that the following conditions
7*05890ca0SHans Petter Selasky  * are met:
8*05890ca0SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
9*05890ca0SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
10*05890ca0SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
11*05890ca0SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
12*05890ca0SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
13*05890ca0SHans Petter Selasky  *
14*05890ca0SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*05890ca0SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*05890ca0SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*05890ca0SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*05890ca0SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*05890ca0SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*05890ca0SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*05890ca0SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*05890ca0SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*05890ca0SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*05890ca0SHans Petter Selasky  * SUCH DAMAGE.
25*05890ca0SHans Petter Selasky  */
26*05890ca0SHans Petter Selasky 
27*05890ca0SHans Petter Selasky #include <stdio.h>
28*05890ca0SHans Petter Selasky #include <stdint.h>
29*05890ca0SHans Petter Selasky #include <stdlib.h>
30*05890ca0SHans Petter Selasky #include <time.h>
31*05890ca0SHans Petter Selasky 
32*05890ca0SHans Petter Selasky extern int usleep(int);
33*05890ca0SHans Petter Selasky extern void callout_process(int);
34*05890ca0SHans Petter Selasky extern void usb_idle(void);
35*05890ca0SHans Petter Selasky extern void usb_init(void);
36*05890ca0SHans Petter Selasky extern void usb_uninit(void);
37*05890ca0SHans Petter Selasky 
38*05890ca0SHans Petter Selasky #define	hz 1000
39*05890ca0SHans Petter Selasky 
40*05890ca0SHans Petter Selasky #ifdef HAVE_MALLOC
41*05890ca0SHans Petter Selasky void *
42*05890ca0SHans Petter Selasky usb_malloc(size_t size)
43*05890ca0SHans Petter Selasky {
44*05890ca0SHans Petter Selasky 	return (malloc(size));
45*05890ca0SHans Petter Selasky }
46*05890ca0SHans Petter Selasky 
47*05890ca0SHans Petter Selasky void
48*05890ca0SHans Petter Selasky usb_free(void *ptr)
49*05890ca0SHans Petter Selasky {
50*05890ca0SHans Petter Selasky 	free(ptr);
51*05890ca0SHans Petter Selasky }
52*05890ca0SHans Petter Selasky #endif
53*05890ca0SHans Petter Selasky 
54*05890ca0SHans Petter Selasky void
55*05890ca0SHans Petter Selasky DELAY(unsigned int delay)
56*05890ca0SHans Petter Selasky {
57*05890ca0SHans Petter Selasky 	usleep(delay);
58*05890ca0SHans Petter Selasky }
59*05890ca0SHans Petter Selasky 
60*05890ca0SHans Petter Selasky void
61*05890ca0SHans Petter Selasky delay(unsigned int delay)
62*05890ca0SHans Petter Selasky {
63*05890ca0SHans Petter Selasky 	usleep(delay);
64*05890ca0SHans Petter Selasky }
65*05890ca0SHans Petter Selasky 
66*05890ca0SHans Petter Selasky int
67*05890ca0SHans Petter Selasky pause(const char *what, int timeout)
68*05890ca0SHans Petter Selasky {
69*05890ca0SHans Petter Selasky 	if (timeout == 0)
70*05890ca0SHans Petter Selasky 		timeout = 1;
71*05890ca0SHans Petter Selasky 
72*05890ca0SHans Petter Selasky 	usleep((1000000 / hz) * timeout);
73*05890ca0SHans Petter Selasky 
74*05890ca0SHans Petter Selasky 	return (0);
75*05890ca0SHans Petter Selasky }
76*05890ca0SHans Petter Selasky 
77*05890ca0SHans Petter Selasky int
78*05890ca0SHans Petter Selasky main(int argc, char **argv)
79*05890ca0SHans Petter Selasky {
80*05890ca0SHans Petter Selasky 	uint32_t time;
81*05890ca0SHans Petter Selasky 
82*05890ca0SHans Petter Selasky 	usb_init();
83*05890ca0SHans Petter Selasky 
84*05890ca0SHans Petter Selasky 	time = 0;
85*05890ca0SHans Petter Selasky 
86*05890ca0SHans Petter Selasky 	while (1) {
87*05890ca0SHans Petter Selasky 
88*05890ca0SHans Petter Selasky 		usb_idle();
89*05890ca0SHans Petter Selasky 
90*05890ca0SHans Petter Selasky 		usleep(1000);
91*05890ca0SHans Petter Selasky 
92*05890ca0SHans Petter Selasky 		if (++time >= (1000 / hz)) {
93*05890ca0SHans Petter Selasky 			time = 0;
94*05890ca0SHans Petter Selasky 			callout_process(1);
95*05890ca0SHans Petter Selasky 		}
96*05890ca0SHans Petter Selasky 	}
97*05890ca0SHans Petter Selasky 
98*05890ca0SHans Petter Selasky 	usb_uninit();
99*05890ca0SHans Petter Selasky 
100*05890ca0SHans Petter Selasky 	return (0);
101*05890ca0SHans Petter Selasky }
102