# `Kuroneko.Gadget.Function`
[🔗](https://github.com/cocoa-xu/kuroneko_gadget/blob/v0.5.0/lib/kuroneko/gadget/function.ex#L1)

A kind of USB function the kernel provides, for `Kuroneko.Gadget`.

Linux sorts USB gadget functions by driver: HID, mass storage, the
network functions (NCM, ECM, RNDIS...), USB Audio, serial ports,
FunctionFS. A module implementing this behaviour is one such driver,
whose configfs attributes are its settings:
`Kuroneko.Gadget.HID`, `Kuroneko.Gadget.MassStorage`,
`Kuroneko.Gadget.NCM`, `Kuroneko.Gadget.UAC2`, `Kuroneko.Gadget.ACM`
and `Kuroneko.Gadget.FunctionFS` come with this library.

An instance goes in the functions as `{Module, name: "x", ...}`, or
`Module` alone for its default name, the options besides `:name` being
the module's:

    Kuroneko.Gadget.configure([
      {Kuroneko.Gadget.HID, name: "pad", descriptor: %{report_desc: pad, report_length: 4}},
      Kuroneko.Gadget.ACM
    ])

The gadget creates each instance's configfs directory, `type.name`, has
the module set it up, links it into the configuration and counts its
endpoints against the controller's. An instance whose settings change is
changed in place where the module can, else created anew; one left out is
removed.

Settings are compared with `==`. An exception in `c:setup/2`,
`c:update/3`, `c:ready/2` or `c:teardown/2` fails the change, which leaves
the functions as they were.

# `settings`

```elixir
@type settings() :: term()
```

# `default_name`

```elixir
@callback default_name() :: String.t()
```

The name of an instance given without one.

# `endpoints`

```elixir
@callback endpoints(settings()) :: %{in: non_neg_integer(), out: non_neg_integer()}
```

The endpoints an instance takes besides endpoint 0.

# `read`
*optional* 

```elixir
@callback read(dir :: Path.t()) :: {:ok, settings()} | :error
```

The settings of the instance in `dir`, or `:error` when it is not one of
this module's. Without it, an instance found on the gadget is taken to
have the settings it was set up with or asked for.

# `ready`
*optional* 

```elixir
@callback ready(dir :: Path.t(), settings()) :: :ok | {:error, term()}
```

Readies an instance before the gadget connects, and again whenever the
functions are configured. When this takes the gadget off the bus, as
FunctionFS does, the gadget connects again.

# `settings`

```elixir
@callback settings(keyword()) :: {:ok, settings()} | {:error, term()}
```

Checks an instance's options, `:name` aside, and returns its settings.

# `setup`

```elixir
@callback setup(dir :: Path.t(), settings()) :: :ok | {:error, term()}
```

Sets up the attributes in an instance's new configfs directory.

# `teardown`
*optional* 

```elixir
@callback teardown(dir :: Path.t(), settings()) :: :ok | {:error, term()}
```

Releases an instance before its directory is removed.

# `type`

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

The configfs function type, such as `"hid"` or `"mass_storage"`.

# `update`
*optional* 

```elixir
@callback update(dir :: Path.t(), from :: settings(), to :: settings()) ::
  :ok | {:error, term()}
```

Changes an instance's settings in place, while it is out of the
configuration. Without it, an instance whose settings change is created
anew.

---

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