1246145Shselasky/* $FreeBSD: releng/11.0/sys/boot/usb/bsd_usbloader_test.c 298647 2016-04-26 15:33:53Z hselasky $ */
2246145Shselasky/*-
3246145Shselasky * Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
4246145Shselasky *
5246145Shselasky * Redistribution and use in source and binary forms, with or without
6246145Shselasky * modification, are permitted provided that the following conditions
7246145Shselasky * are met:
8246145Shselasky * 1. Redistributions of source code must retain the above copyright
9246145Shselasky *    notice, this list of conditions and the following disclaimer.
10246145Shselasky * 2. Redistributions in binary form must reproduce the above copyright
11246145Shselasky *    notice, this list of conditions and the following disclaimer in the
12246145Shselasky *    documentation and/or other materials provided with the distribution.
13246145Shselasky *
14246145Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15246145Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16246145Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17246145Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18246145Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19246145Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20246145Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21246145Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22246145Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23246145Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24246145Shselasky * SUCH DAMAGE.
25246145Shselasky */
26246145Shselasky
27246145Shselasky#include <stdio.h>
28246145Shselasky#include <stdint.h>
29266887Shselasky#include <stdlib.h>
30246145Shselasky#include <time.h>
31246145Shselasky
32246145Shselaskyextern int usleep(int);
33246145Shselaskyextern void callout_process(int);
34246145Shselaskyextern void usb_idle(void);
35246145Shselaskyextern void usb_init(void);
36246145Shselaskyextern void usb_uninit(void);
37246145Shselasky
38246145Shselasky#define	hz 1000
39246145Shselasky
40266894Shselasky#ifdef HAVE_MALLOC
41266887Shselaskyvoid *
42266887Shselaskyusb_malloc(size_t size)
43266887Shselasky{
44266887Shselasky	return (malloc(size));
45266887Shselasky}
46266887Shselasky
47246145Shselaskyvoid
48266887Shselaskyusb_free(void *ptr)
49266887Shselasky{
50266887Shselasky	free(ptr);
51266887Shselasky}
52266894Shselasky#endif
53266887Shselasky
54266887Shselaskyvoid
55246145ShselaskyDELAY(unsigned int delay)
56246145Shselasky{
57246145Shselasky	usleep(delay);
58246145Shselasky}
59246145Shselasky
60298647Shselaskyvoid
61298647Shselaskydelay(unsigned int delay)
62298647Shselasky{
63298647Shselasky	usleep(delay);
64298647Shselasky}
65298647Shselasky
66246145Shselaskyint
67246145Shselaskypause(const char *what, int timeout)
68246145Shselasky{
69246145Shselasky	if (timeout == 0)
70246145Shselasky		timeout = 1;
71246145Shselasky
72246145Shselasky	usleep((1000000 / hz) * timeout);
73246145Shselasky
74246145Shselasky	return (0);
75246145Shselasky}
76246145Shselasky
77246145Shselaskyint
78246145Shselaskymain(int argc, char **argv)
79246145Shselasky{
80246145Shselasky	uint32_t time;
81246145Shselasky
82246145Shselasky	usb_init();
83246145Shselasky
84246145Shselasky	time = 0;
85246145Shselasky
86246145Shselasky	while (1) {
87246145Shselasky
88246145Shselasky		usb_idle();
89246145Shselasky
90246145Shselasky		usleep(1000);
91246145Shselasky
92246145Shselasky		if (++time >= (1000 / hz)) {
93246145Shselasky			time = 0;
94246145Shselasky			callout_process(1);
95246145Shselasky		}
96246145Shselasky	}
97246145Shselasky
98246145Shselasky	usb_uninit();
99246145Shselasky
100246145Shselasky	return (0);
101246145Shselasky}
102