# `NanoKVMGo.USB`
[🔗](https://github.com/cocoa-xu/nano_kvm_go/blob/v0.4.0/lib/nano_kvm_go/usb.ex#L1)

The NanoKVM-Go's USB gadget, as its Nerves system sets it up at boot.

The system makes the gadget `/sys/kernel/config/usb_gadget/nanokvm` with a network (NCM) function and
binds it to the device controller; a gadget library such as
`Kuroneko.Gadget` adds the KVM functions. `gadget_options/1` gives such a
library what this board needs:

  * the largest packets its USB controller (DWC3) takes: its smallest
    transmit FIFO holds 832 bytes of one, and its receive FIFO three of
    682
  * the names the target computer sees: "NanoKVM-Go Audio" and its
    directions, and the drive "NanoKVM Virtual Media"
  * the network function and its addresses

The network's addresses stay the same from boot to boot, so that the
computer keeps one network interface for the NanoKVM: the system gives it
those in the U-Boot environment's `usbnet_devaddr` and `usbnet_hostaddr`,
else addresses made from the serial number
(`default_network_addresses/1`). `persist_network_addresses/2` puts new
ones there, through `Nerves.Runtime.KV` or another module with its
`put/1` (the `:kv` option).

    children = [
      {Kuroneko.Gadget, NanoKVMGo.USB.gadget_options() ++ [functions: [:keyboard, :mouse]]}
    ]

The microphone and the speaker of the USB audio function play and record
through `NanoKVMGo.Microphone` and `NanoKVMGo.Audio`.

# `mac_address`

```elixir
@type mac_address() :: String.t()
```

A MAC address, such as `"02:12:34:56:78:9a"`.

# `network_addresses`

```elixir
@type network_addresses() :: %{
  device_address: mac_address(),
  host_address: mac_address()
}
```

# `option`

```elixir
@type option() :: {:serial_number, String.t() | nil} | {:kv, module()}
```

# `default_network_addresses`

```elixir
@spec default_network_addresses(String.t()) :: network_addresses()
```

The addresses the NanoKVM-Go system gives the USB network when the U-Boot
environment has none: the first six bytes of the SHA-256 of the serial
number (`boardid`'s) for the NanoKVM's end and the next six for the
computer's, as locally administered unicast addresses.

    iex> NanoKVMGo.USB.default_network_addresses("0123456789abcdef")
    %{device_address: "9e:9f:51:11:f7:b2", host_address: "7a:78:1f:1f:1d:dd"}

# `gadget`

```elixir
@spec gadget() :: Path.t()
```

The configfs directory of the gadget the NanoKVM-Go system sets up.

# `gadget_options`

```elixir
@spec gadget_options([option()]) :: keyword()
```

The options of the gadget on this board, for `Kuroneko.Gadget`: `:gadget`,
`:endpoints`, `:max_packet`, `:names` and `:network`. The AX620E's DWC3 has
eight IN and eight OUT endpoints, seven of each besides endpoint 0.

## Options

  * `:serial_number` - what the default network addresses are made from,
    by default the board's (`boardid`'s); `nil` for no default addresses
  * `:kv` - the U-Boot environment, where the network addresses persist,
    `Nerves.Runtime.KV` by default

# `persist_network_addresses`

```elixir
@spec persist_network_addresses(
  %{optional(:device_address | :host_address) =&gt; mac_address() | :default},
  [{:kv, module()}]
) :: :ok | {:error, term()}
```

Persists USB network addresses in the U-Boot environment, where the
NanoKVM-Go system reads them at the next boots: `:device_address` (the
NanoKVM's end), `:host_address` (the computer's), each an address or
`:default` for the one made from the serial number.

## Options

  * `:kv` - the U-Boot environment, `Nerves.Runtime.KV` by default

---

*Consult [api-reference.md](api-reference.md) for complete listing*
