assert_spec x86
set usb_raw_device ""

#
# Check if USB_RAW_DEVICE is set for Qemu
#
if {[have_include power_on/qemu]} {
	if {![info exists ::env(USB_RAW_DEVICE)]} {
		puts "\nPlease define USB_RAW_DEVICE environment variable and set it to your USB device <bus.device>\n"
		exit 0
	}
	set usb_raw_device $::env(USB_RAW_DEVICE)
}

create_boot_directory
import_from_depot [depot_user]/src/[base_src] \
                  [depot_user]/pkg/test_usb_host_drv-[board] \
                  [depot_user]/src/report_rom \
                  [depot_user]/src/init

build { server/usb_terminal test/terminal_echo }

install_config {
<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>

	<start name="timer">
		<resource name="RAM" quantum="2M"/>
		<provides><service name="Timer"/></provides>
	</start>

	<start name="report_rom">
		<resource name="RAM" quantum="2M"/>
		<provides><service name="Report"/>
		          <service name="ROM"/></provides>
		<config verbose="yes"/>
	</start>

	<start name="usb_drv" caps="1500" managing_system="yes">
		<binary name="init"/>
		<resource name="RAM" quantum="32M"/>
		<provides> <service name="Usb"/> </provides>
		<route>
			<service name="ROM" label="config">
				<parent label="drivers.config"/> </service>
			<service name="Timer">  <child name="timer"/> </service>
			<service name="Report">  <child name="report_rom"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>

	<start name="usb_terminal">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Terminal"/></provides>
	</start>

	<start name="test-terminal_echo">
		<resource name="RAM" quantum="2M"/>
	</start>
</config>}

#
# Define USB host controller config
#
set fd [open [run_dir]/genode/usb_host_drv.config w]
puts $fd {
<config bios_handoff="yes">
	<report devices="yes"/>
	<policy label="usb_terminal -> usb_serial" vendor_id="0x403" product_id="0x6001"/>
</config>}
close $fd

build_boot_image { test-terminal_echo usb_terminal }

append qemu_args " -chardev socket,path=$usb_raw_device,id=ser "
append qemu_args " -device nec-usb-xhci,id=xhci -device usb-serial,bus=xhci.0,chardev=ser -nographic"

run_genode_until forever
