code:
=============================================================
MOXA Smartio/Industio Family Device Driver Installation Guide
=============================================================

Copyright (C) 2008, Moxa Inc.
Copyright (C) 2021, Jiri Slaby

.. Content

   1. Introduction
   2. System Requirement
   3. Installation
      3.1 Hardware installation
      3.2 Device naming convention
   4. Utilities
   5. Setserial
   6. Troubleshooting

1. Introduction
^^^^^^^^^^^^^^^

   The Smartio/Industio/UPCI family Linux driver supports following multiport
   boards:

    - 2 ports multiport board
	CP-102U, CP-102UL, CP-102UF
	CP-132U-I, CP-132UL,
	CP-132, CP-132I, CP132S, CP-132IS,
	(CP-102, CP-102S)

    - 4 ports multiport board
	CP-104EL,
	CP-104UL, CP-104JU,
	CP-134U, CP-134U-I,
	C104H/PCI, C104HS/PCI,
	CP-114, CP-114I, CP-114S, CP-114IS, CP-114UL,
	(C114HI, CT-114I),
	POS-104UL,
	CB-114,
	CB-134I

    - 8 ports multiport board
	CP-118EL, CP-168EL,
	CP-118U, CP-168U,
	C168H/PCI,
	CB-108

   If a compatibility problem occurs, please contact Moxa at
   support@moxa.com.tw.

   In addition to device driver, useful utilities are also provided in this
   version. They are:

    - msdiag
		 Diagnostic program for displaying installed Moxa
                 Smartio/Industio boards.
    - msmon
		 Monitor program to observe data count and line status signals.
    - msterm     A simple terminal program which is useful in testing serial
	         ports.

   All the drivers and utilities are published in form of source code under
   GNU General Public License in this version. Please refer to GNU General
   Public License announcement in each source code file for more detail.

   In Moxa's Web sites, you may always find the latest driver at
   https://www.moxa.com/.

   This version of driver can be installed as a Loadable Module (Module driver)
   or built-in into kernel (Static driver). Before you install the driver,
   please refer to hardware installation procedure in the User's Manual.

   We assume the user should be familiar with following documents:

   - Serial-HOWTO
   - Kernel-HOWTO

2. System Requirement
^^^^^^^^^^^^^^^^^^^^^

   - Maximum 4 boards can be installed in combination

3. Installation
^^^^^^^^^^^^^^^

3.1 Hardware installation
=========================

PCI/UPCI board
--------------

   You may need to adjust IRQ usage in BIOS to avoid IRQ conflict with other
   ISA devices. Please refer to hardware installation procedure in User's
   Manual in advance.

PCI IRQ Sharing
---------------

   Each port within the same multiport board shares the same IRQ. Up to
   4 Moxa Smartio/Industio PCI Family multiport boards can be installed
   together on one system and they can share the same IRQ.

3.2 Device naming convention
============================

   The device node is named "ttyMxx".

Device naming when more than 2 boards installed
-----------------------------------------------

   Naming convention for each Smartio/Industio multiport board is
   pre-defined as below.

   ============ ===============
   Board Num.	Device node
   1st board	ttyM0  - ttyM7
   2nd board	ttyM8  - ttyM15
   3rd board	ttyM16 - ttyM23
   4th board	ttyM24 - ttyM31
   ============ ===============

4. Utilities
^^^^^^^^^^^^

   There are 3 utilities contained in this driver. They are msdiag, msmon and
   msterm. These 3 utilities are released in form of source code. They should
   be compiled into executable file and copied into /usr/bin.

msdiag - Diagnostic
===================

   This utility provides the function to display what Moxa Smartio/Industio
   board was found by the driver in the system.

msmon - Port Monitoring
=======================

   This utility gives the user a quick view about all the MOXA ports'
   activities. One can easily learn each port's total received/transmitted
   (Rx/Tx) character count since the time when the monitoring is started.

   Rx/Tx throughputs per second are also reported in interval basis (e.g.
   the last 5 seconds) and in average basis (since the time the monitoring
   is started). You can reset all ports' count by <HOME> key. <+> <->
   (plus/minus) keys to change the displaying time interval. Press <ENTER>
   on the port, that cursor stay, to view the port's communication
   parameters, signal status, and input/output queue.

msterm - Terminal Emulation
===========================

   This utility provides data sending and receiving ability of all tty ports,
   especially for MOXA ports. It is quite useful for testing simple
   application, for example, sending AT command to a modem connected to the
   port or used as a terminal for login purpose. Note that this is only a
   dumb terminal emulation without handling full screen operation.

5. Setserial
^^^^^^^^^^^^

   Supported Setserial parameters are listed as below.

   ============== =============================================================
   uart		  set UART type(16450 --> disable FIFO, 16550A --> enable FIFO)
   close_delay	  set the amount of time (in 1/100 of a second) that DTR
		  should be kept low while being closed.
   closing_wait   set the amount of time (in 1/100 of a second) that the
		  serial port should wait for data to be drained while
		  being closed, before the receiver is disabled.
   spd_hi	  Use 57.6kb when the application requests 38.4kb.
   spd_vhi	  Use 115.2kb when the application requests 38.4kb.
   spd_shi	  Use 230.4kb when the application requests 38.4kb.
   spd_warp	  Use 460.8kb when the application requests 38.4kb.
   spd_normal	  Use 38.4kb when the application requests 38.4kb.
   spd_cust	  Use the custom divisor to set the speed when the
		  application requests 38.4kb.
   divisor	  This option sets the custom division.
   baud_base	  This option sets the base baud rate.
   ============== =============================================================

6. Troubleshooting
^^^^^^^^^^^^^^^^^^

   The boot time error messages and solutions are stated as clearly as
   possible. If all the possible solutions fail, please contact our technical
   support team to get more help.

   Error msg:
	      More than 4 Moxa Smartio/Industio family boards found. Fifth board
              and after are ignored.

   Solution:
   To avoid this problem, please unplug fifth and after board, because Moxa
   driver supports up to 4 boards.


code:
==============================
GSM 0710 tty multiplexor HOWTO
==============================

.. contents:: :local:

This line discipline implements the GSM 07.10 multiplexing protocol
detailed in the following 3GPP document:

	https://www.3gpp.org/ftp/Specs/archive/07_series/07.10/0710-720.zip

This document give some hints on how to use this driver with GPRS and 3G
modems connected to a physical serial port.

How to use it
=============

Config Initiator
----------------

#. Initialize the modem in 0710 mux mode (usually ``AT+CMUX=`` command) through
   its serial port. Depending on the modem used, you can pass more or less
   parameters to this command.

#. Switch the serial line to using the n_gsm line discipline by using
   ``TIOCSETD`` ioctl.

#. Configure the mux using ``GSMIOC_GETCONF``/``GSMIOC_SETCONF`` ioctl.

#. Obtain base gsmtty number for the used serial port.

   Major parts of the initialization program
   (a good starting point is util-linux-ng/sys-utils/ldattach.c)::

      #include <stdio.h>
      #include <stdint.h>
      #include <linux/gsmmux.h>
      #include <linux/tty.h>

      #define DEFAULT_SPEED	B115200
      #define SERIAL_PORT	/dev/ttyS0

      int ldisc = N_GSM0710;
      struct gsm_config c;
      struct termios configuration;
      uint32_t first;

      /* open the serial port connected to the modem */
      fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);

      /* configure the serial port : speed, flow control ... */

      /* send the AT commands to switch the modem to CMUX mode
         and check that it's successful (should return OK) */
      write(fd, "AT+CMUX=0\r", 10);

      /* experience showed that some modems need some time before
         being able to answer to the first MUX packet so a delay
         may be needed here in some case */
      sleep(3);

      /* use n_gsm line discipline */
      ioctl(fd, TIOCSETD, &ldisc);

      /* get n_gsm configuration */
      ioctl(fd, GSMIOC_GETCONF, &c);
      /* we are initiator and need encoding 0 (basic) */
      c.initiator = 1;
      c.encapsulation = 0;
      /* our modem defaults to a maximum size of 127 bytes */
      c.mru = 127;
      c.mtu = 127;
      /* set the new configuration */
      ioctl(fd, GSMIOC_SETCONF, &c);
      /* get first gsmtty device node */
      ioctl(fd, GSMIOC_GETFIRST, &first);
      printf("first muxed line: /dev/gsmtty%i\n", first);

      /* and wait for ever to keep the line discipline enabled */
      daemon(0,0);
      pause();

#. Use these devices as plain serial ports.

   For example, it's possible:

   - to use *gnokii* to send / receive SMS on ``ttygsm1``
   - to use *ppp* to establish a datalink on ``ttygsm2``

#. First close all virtual ports before closing the physical port.

   Note that after closing the physical port the modem is still in multiplexing
   mode. This may prevent a successful re-opening of the port later. To avoid
   this situation either reset the modem if your hardware allows that or send
   a disconnect command frame manually before initializing the multiplexing mode
   for the second time. The byte sequence for the disconnect command frame is::

      0xf9, 0x03, 0xef, 0x03, 0xc3, 0x16, 0xf9

Config Requester
----------------

#. Receive ``AT+CMUX=`` command through its serial port, initialize mux mode
   config.

#. Switch the serial line to using the *n_gsm* line discipline by using
   ``TIOCSETD`` ioctl.

#. Configure the mux using ``GSMIOC_GETCONF``/``GSMIOC_SETCONF`` ioctl.

#. Obtain base gsmtty number for the used serial port::

        #include <stdio.h>
        #include <stdint.h>
        #include <linux/gsmmux.h>
        #include <linux/tty.h>
        #define DEFAULT_SPEED	B115200
        #define SERIAL_PORT	/dev/ttyS0

	int ldisc = N_GSM0710;
	struct gsm_config c;
	struct termios configuration;
	uint32_t first;

	/* open the serial port */
	fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);

	/* configure the serial port : speed, flow control ... */

	/* get serial data and check "AT+CMUX=command" parameter ... */

	/* use n_gsm line discipline */
	ioctl(fd, TIOCSETD, &ldisc);

	/* get n_gsm configuration */
	ioctl(fd, GSMIOC_GETCONF, &c);
	/* we are requester and need encoding 0 (basic) */
	c.initiator = 0;
	c.encapsulation = 0;
	/* our modem defaults to a maximum size of 127 bytes */
	c.mru = 127;
	c.mtu = 127;
	/* set the new configuration */
	ioctl(fd, GSMIOC_SETCONF, &c);
	/* get first gsmtty device node */
	ioctl(fd, GSMIOC_GETFIRST, &first);
	printf("first muxed line: /dev/gsmtty%i\n", first);

	/* and wait for ever to keep the line discipline enabled */
	daemon(0,0);
	pause();

11-03-08 - Eric Bénard - <eric@eukrea.com>

repo_name: angery/linux


code:
.. SPDX-License-Identifier: GPL-2.0

=====
N_TTY
=====

.. contents:: :local:

The default (and fallback) :doc:`TTY line discipline <tty_ldisc>`. It tries to
handle characters as per POSIX.

External Functions
==================

.. kernel-doc:: drivers/tty/n_tty.c
   :export:

Internal Functions
==================

.. kernel-doc:: drivers/tty/n_tty.c
   :internal:


code:
.. SPDX-License-Identifier: GPL-2.0

==========
TTY Buffer
==========

.. contents:: :local:

Here, we document functions for taking care of tty buffer and their flipping.
Drivers are supposed to fill the buffer by one of those functions below and
then flip the buffer, so that the data are passed to :doc:`line discipline
<tty_ldisc>` for further processing.

Flip Buffer Management
======================

.. kernel-doc:: drivers/tty/tty_buffer.c
   :identifiers: tty_prepare_flip_string tty_insert_flip_string_fixed_flag
           tty_insert_flip_string_flags __tty_insert_flip_char
           tty_flip_buffer_push tty_ldisc_receive_buf

----

Other Functions
===============

.. kernel-doc:: drivers/tty/tty_buffer.c
   :identifiers: tty_buffer_space_avail tty_buffer_set_limit

----

Buffer Locking
==============

These are used only in special circumstances. Avoid them.

.. kernel-doc:: drivers/tty/tty_buffer.c
   :identifiers: tty_buffer_lock_exclusive tty_buffer_unlock_exclusive

----

Internal Functions
==================

.. kernel-doc:: drivers/tty/tty_buffer.c
   :internal:


code:
.. SPDX-License-Identifier: GPL-2.0

=============================
TTY Driver and TTY Operations
=============================

.. contents:: :local:

Allocation
==========

The first thing a driver needs to do is to allocate a struct tty_driver. This
is done by tty_alloc_driver() (or __tty_alloc_driver()). Next, the newly
allocated structure is filled with information. See `TTY Driver Reference`_ at
the end of this document on what actually shall be filled in.

The allocation routines expect a number of devices the driver can handle at
most and flags. Flags are those starting ``TTY_DRIVER_`` listed and described
in `TTY Driver Flags`_ below.

When the driver is about to be freed, tty_driver_kref_put() is called on that.
It will decrements the reference count and if it reaches zero, the driver is
freed.

For reference, both allocation and deallocation functions are explained here in
detail:

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: __tty_alloc_driver tty_driver_kref_put

TTY Driver Flags
----------------

Here comes the documentation of flags accepted by tty_alloc_driver() (or
__tty_alloc_driver()):

.. kernel-doc:: include/linux/tty_driver.h
   :doc: TTY Driver Flags

----

Registration
============

When a struct tty_driver is allocated and filled in, it can be registered using
tty_register_driver(). It is recommended to pass ``TTY_DRIVER_DYNAMIC_DEV`` in
flags of tty_alloc_driver(). If it is not passed, *all* devices are also
registered during tty_register_driver() and the following paragraph of
registering devices can be skipped for such drivers. However, the struct
tty_port part in `Registering Devices`_ is still relevant there.

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_register_driver tty_unregister_driver

Registering Devices
-------------------

Every TTY device shall be backed by a struct tty_port. Usually, TTY drivers
embed tty_port into device's private structures. Further details about handling
tty_port can be found in :doc:`tty_port`. The driver is also recommended to use
tty_port's reference counting by tty_port_get() and tty_port_put(). The final
put is supposed to free the tty_port including the device's private struct.

Unless ``TTY_DRIVER_DYNAMIC_DEV`` was passed as flags to tty_alloc_driver(),
TTY driver is supposed to register every device discovered in the system
(the latter is preferred). This is performed by tty_register_device(). Or by
tty_register_device_attr() if the driver wants to expose some information
through struct attribute_group. Both of them register ``index``'th device and
upon return, the device can be opened. There are also preferred tty_port
variants described in `Linking Devices to Ports`_ later. It is up to driver to
manage free indices and choosing the right one. The TTY layer only refuses to
register more devices than passed to tty_alloc_driver().

When the device is opened, the TTY layer allocates struct tty_struct and starts
calling operations from :c:member:`tty_driver.ops`, see `TTY Operations
Reference`_.

The registration routines are documented as follows:

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_register_device tty_register_device_attr
        tty_unregister_device

----

Linking Devices to Ports
------------------------
As stated earlier, every TTY device shall have a struct tty_port assigned to
it. It must be known to the TTY layer at :c:member:`tty_driver.ops.install()`
at latest.  There are few helpers to *link* the two. Ideally, the driver uses
tty_port_register_device() or tty_port_register_device_attr() instead of
tty_register_device() and tty_register_device_attr() at the registration time.
This way, the driver needs not care about linking later on.

If that is not possible, the driver still can link the tty_port to a specific
index *before* the actual registration by tty_port_link_device(). If it still
does not fit, tty_port_install() can be used from the
:c:member:`tty_driver.ops.install` hook as a last resort. The last one is
dedicated mostly for in-memory devices like PTY where tty_ports are allocated
on demand.

The linking routines are documented here:

.. kernel-doc::  drivers/tty/tty_port.c
   :identifiers: tty_port_link_device tty_port_register_device
        tty_port_register_device_attr

----

TTY Driver Reference
====================

All members of struct tty_driver are documented here. The required members are
noted at the end. struct tty_operations are documented next.

.. kernel-doc:: include/linux/tty_driver.h
   :identifiers: tty_driver

----

TTY Operations Reference
========================

When a TTY is registered, these driver hooks can be invoked by the TTY layer:

.. kernel-doc:: include/linux/tty_driver.h
   :identifiers: tty_operations


code:
.. SPDX-License-Identifier: GPL-2.0

=============
TTY Internals
=============

.. contents:: :local:

Kopen
=====

These functions serve for opening a TTY from the kernelspace:

.. kernel-doc:: drivers/tty/tty_io.c
      :identifiers: tty_kopen_exclusive tty_kopen_shared tty_kclose

----

Exported Internal Functions
===========================

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_release_struct tty_dev_name_to_number tty_get_icount

----

Internal Functions
==================

.. kernel-doc:: drivers/tty/tty_io.c
   :internal:


code:
.. SPDX-License-Identifier: GPL-2.0

===================
TTY Line Discipline
===================

.. contents:: :local:

TTY line discipline process all incoming and outgoing character from/to a tty
device. The default line discipline is :doc:`N_TTY <n_tty>`. It is also a
fallback if establishing any other discipline for a tty fails. If even N_TTY
fails, N_NULL takes over. That never fails, but also does not process any
characters -- it throws them away.

Registration
============

Line disciplines are registered with tty_register_ldisc() passing the ldisc
structure. At the point of registration the discipline must be ready to use and
it is possible it will get used before the call returns success. If the call
returns an error then it won’t get called. Do not re-use ldisc numbers as they
are part of the userspace ABI and writing over an existing ldisc will cause
demons to eat your computer. You must not re-register over the top of the line
discipline even with the same data or your computer again will be eaten by
demons. In order to remove a line discipline call tty_unregister_ldisc().

Heed this warning: the reference count field of the registered copies of the
tty_ldisc structure in the ldisc table counts the number of lines using this
discipline. The reference count of the tty_ldisc structure within a tty counts
the number of active users of the ldisc at this instant. In effect it counts
the number of threads of execution within an ldisc method (plus those about to
enter and exit although this detail matters not).

.. kernel-doc:: drivers/tty/tty_ldisc.c
   :identifiers: tty_register_ldisc tty_unregister_ldisc

Other Functions
===============

.. kernel-doc:: drivers/tty/tty_ldisc.c
   :identifiers: tty_set_ldisc tty_ldisc_flush

Line Discipline Operations Reference
====================================

.. kernel-doc:: include/linux/tty_ldisc.h
   :identifiers: tty_ldisc_ops

Driver Access
=============

Line discipline methods can call the methods of the underlying hardware driver.
These are documented as a part of struct tty_operations.

TTY Flags
=========

Line discipline methods have access to :c:member:`tty_struct.flags` field. See
:doc:`tty_struct`.

Locking
=======

Callers to the line discipline functions from the tty layer are required to
take line discipline locks. The same is true of calls from the driver side
but not yet enforced.

.. kernel-doc:: drivers/tty/tty_ldisc.c
   :identifiers: tty_ldisc_ref_wait tty_ldisc_ref tty_ldisc_deref

While these functions are slightly slower than the old code they should have
minimal impact as most receive logic uses the flip buffers and they only
need to take a reference when they push bits up through the driver.

A caution: The :c:member:`tty_ldisc_ops.open()`,
:c:member:`tty_ldisc_ops.close()` and :c:member:`tty_driver.set_ldisc()`
functions are called with the ldisc unavailable. Thus tty_ldisc_ref() will fail
in this situation if used within these functions.  Ldisc and driver code
calling its own functions must be careful in this case.

Internal Functions
==================

.. kernel-doc:: drivers/tty/tty_ldisc.c
   :internal:


code:
.. SPDX-License-Identifier: GPL-2.0

========
TTY Port
========

.. contents:: :local:

The TTY drivers are advised to use struct tty_port helpers as much as possible.
If the drivers implement :c:member:`tty_port.ops.activate()` and
:c:member:`tty_port.ops.shutdown()`, they can use tty_port_open(),
tty_port_close(), and tty_port_hangup() in respective
:c:member:`tty_struct.ops` hooks.

The reference and details are contained in the `TTY Port Reference`_ and `TTY
Port Operations Reference`_ sections at the bottom.

TTY Port Functions
==================

Init & Destroy
--------------

.. kernel-doc::  drivers/tty/tty_port.c
   :identifiers: tty_port_init tty_port_destroy
        tty_port_get tty_port_put

Open/Close/Hangup Helpers
-------------------------

.. kernel-doc::  drivers/tty/tty_port.c
   :identifiers: tty_port_install tty_port_open tty_port_block_til_ready
        tty_port_close tty_port_close_start tty_port_close_end tty_port_hangup
        tty_port_shutdown

TTY Refcounting
---------------

.. kernel-doc::  drivers/tty/tty_port.c
   :identifiers: tty_port_tty_get tty_port_tty_set

TTY Helpers
-----------

.. kernel-doc::  drivers/tty/tty_port.c
   :identifiers: tty_port_tty_hangup tty_port_tty_wakeup

Modem Signals
-------------

.. kernel-doc::  drivers/tty/tty_port.c
   :identifiers: tty_port_carrier_raised tty_port_raise_dtr_rts
        tty_port_lower_dtr_rts

----

TTY Port Reference
==================

.. kernel-doc:: include/linux/tty_port.h
   :identifiers: tty_port

----

TTY Port Operations Reference
=============================

.. kernel-doc:: include/linux/tty_port.h
   :identifiers: tty_port_operations


code:
.. SPDX-License-Identifier: GPL-2.0

==========
TTY Struct
==========

.. contents:: :local:

struct tty_struct is allocated by the TTY layer upon the first open of the TTY
device and released after the last close. The TTY layer passes this structure
to most of struct tty_operation's hooks. Members of tty_struct are documented
in `TTY Struct Reference`_ at the bottom.

Initialization
==============

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_init_termios

Name
====

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_name

Reference counting
==================

.. kernel-doc:: include/linux/tty.h
   :identifiers: tty_kref_get

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_kref_put

Install
=======

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_standard_install

Read & Write
============

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_put_char

Start & Stop
============

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: start_tty stop_tty

Wakeup
======

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_wakeup

Hangup
======

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_hangup tty_vhangup tty_hung_up_p

Misc
====

.. kernel-doc:: drivers/tty/tty_io.c
   :identifiers: tty_do_resize

TTY Struct Flags
================

.. kernel-doc:: include/linux/tty.h
   :doc: TTY Struct Flags

TTY Struct Reference
====================

.. kernel-doc:: include/linux/tty.h
   :identifiers: tty_struct


code:
=======================
The Userspace I/O HOWTO
=======================

:Author: Hans-Jürgen Koch Linux developer, Linutronix
:Date:   2006-12-11

About this document
===================

Translations
------------

If you know of any translations for this document, or you are interested
in translating it, please email me hjk@hansjkoch.de.

Preface
-------

For many types of devices, creating a Linux kernel driver is overkill.
All that is really needed is some way to handle an interrupt and provide
access to the memory space of the device. The logic of controlling the
device does not necessarily have to be within the kernel, as the device
does not need to take advantage of any of other resources that the
kernel provides. One such common class of devices that are like this are
for industrial I/O cards.

To address this situation, the userspace I/O system (UIO) was designed.
For typical industrial I/O cards, only a very small kernel module is
needed. The main part of the driver will run in user space. This
simplifies development and reduces the risk of serious bugs within a
kernel module.

Please note that UIO is not an universal driver interface. Devices that
are already handled well by other kernel subsystems (like networking or
serial or USB) are no candidates for an UIO driver. Hardware that is
ideally suited for an UIO driver fulfills all of the following:

-  The device has memory that can be mapped. The device can be
   controlled completely by writing to this memory.

-  The device usually generates interrupts.

-  The device does not fit into one of the standard kernel subsystems.

Acknowledgments
---------------

I'd like to thank Thomas Gleixner and Benedikt Spranger of Linutronix,
who have not only written most of the UIO code, but also helped greatly
writing this HOWTO by giving me all kinds of background information.

Feedback
--------

Find something wrong with this document? (Or perhaps something right?) I
would love to hear from you. Please email me at hjk@hansjkoch.de.

About UIO
=========

If you use UIO for your card's driver, here's what you get:

-  only one small kernel module to write and maintain.

-  develop the main part of your driver in user space, with all the
   tools and libraries you're used to.

-  bugs in your driver won't crash the kernel.

-  updates of your driver can take place without recompiling the kernel.

How UIO works
-------------

Each UIO device is accessed through a device file and several sysfs
attribute files. The device file will be called ``/dev/uio0`` for the
first device, and ``/dev/uio1``, ``/dev/uio2`` and so on for subsequent
devices.

``/dev/uioX`` is used to access the address space of the card. Just use
:c:func:`mmap()` to access registers or RAM locations of your card.

Interrupts are handled by reading from ``/dev/uioX``. A blocking
:c:func:`read()` from ``/dev/uioX`` will return as soon as an
interrupt occurs. You can also use :c:func:`select()` on
``/dev/uioX`` to wait for an interrupt. The integer value read from
``/dev/uioX`` represents the total interrupt count. You can use this
number to figure out if you missed some interrupts.

For some hardware that has more than one interrupt source internally,
but not separate IRQ mask and status registers, there might be
situations where userspace cannot determine what the interrupt source
was if the kernel handler disables them by writing to the chip's IRQ
register. In such a case, the kernel has to disable the IRQ completely
to leave the chip's register untouched. Now the userspace part can
determine the cause of the interrupt, but it cannot re-enable
interrupts. Another cornercase is chips where re-enabling interrupts is
a read-modify-write operation to a combined IRQ status/acknowledge
register. This would be racy if a new interrupt occurred simultaneously.

To address these problems, UIO also implements a write() function. It is
normally not used and can be ignored for hardware that has only a single
interrupt source or has separate IRQ mask and status registers. If you
need it, however, a write to ``/dev/uioX`` will call the
:c:func:`irqcontrol()` function implemented by the driver. You have
to write a 32-bit value that is usually either 0 or 1 to disable or
enable interrupts. If a driver does not implement
:c:func:`irqcontrol()`, :c:func:`write()` will return with
``-ENOSYS``.

To handle interrupts properly, your custom kernel module can provide its
own interrupt handler. It will automatically be called by the built-in
handler.

For cards that don't generate interrupts but need to be polled, there is
the possibility to set up a timer that triggers the interrupt handler at
configurable time intervals. This interrupt simulation is done by
calling :c:func:`uio_event_notify()` from the timer's event
handler.

Each driver provides attributes that are used to read or write
variables. These attributes are accessible through sysfs files. A custom
kernel driver module can add its own attributes to the device owned by
the uio driver, but not added to the UIO device itself at this time.
This might change in the future if it would be found to be useful.

The following standard attributes are provided by the UIO framework:

-  ``name``: The name of your device. It is recommended to use the name
   of your kernel module for this.

-  ``version``: A version string defined by your driver. This allows the
   user space part of your driver to deal with different versions of the
   kernel module.

-  ``event``: The total number of interrupts handled by the driver since
   the last time the device node was read.

These attributes appear under the ``/sys/class/uio/uioX`` directory.
Please note that this directory might be a symlink, and not a real
directory. Any userspace code that accesses it must be able to handle
this.

Each UIO device can make one or more memory regions available for memory
mapping. This is necessary because some industrial I/O cards require
access to more than one PCI memory region in a driver.

Each mapping has its own directory in sysfs, the first mapping appears
as ``/sys/class/uio/uioX/maps/map0/``. Subsequent mappings create
directories ``map1/``, ``map2/``, and so on. These directories will only
appear if the size of the mapping is not 0.

Each ``mapX/`` directory contains four read-only files that show
attributes of the memory:

-  ``name``: A string identifier for this mapping. This is optional, the
   string can be empty. Drivers can set this to make it easier for
   userspace to find the correct mapping.

-  ``addr``: The address of memory that can be mapped.

-  ``size``: The size, in bytes, of the memory pointed to by addr.

-  ``offset``: The offset, in bytes, that has to be added to the pointer
   returned by :c:func:`mmap()` to get to the actual device memory.
   This is important if the device's memory is not page aligned.
   Remember that pointers returned by :c:func:`mmap()` are always
   page aligned, so it is good style to always add this offset.

From userspace, the different mappings are distinguished by adjusting
the ``offset`` parameter of the :c:func:`mmap()` call. To map the
memory of mapping N, you have to use N times the page size as your
offset::

    offset = N * getpagesize();

Sometimes there is hardware with memory-like regions that can not be
mapped with the technique described here, but there are still ways to
access them from userspace. The most common example are x86 ioports. On
x86 systems, userspace can access these ioports using
:c:func:`ioperm()`, :c:func:`iopl()`, :c:func:`inb()`,
:c:func:`outb()`, and similar functions.

Since these ioport regions can not be mapped, they will not appear under
``/sys/class/uio/uioX/maps/`` like the normal memory described above.
Without information about the port regions a hardware has to offer, it
becomes difficult for the userspace part of the driver to find out which
ports belong to which UIO device.

To address this situation, the new directory
``/sys/class/uio/uioX/portio/`` was added. It only exists if the driver
wants to pass information about one or more port regions to userspace.
If that is the case, subdirectories named ``port0``, ``port1``, and so
on, will appear underneath ``/sys/class/uio/uioX/portio/``.

Each ``portX/`` directory contains four read-only files that show name,
start, size, and type of the port region:

-  ``name``: A string identifier for this port region. The string is
   optional and can be empty. Drivers can set it to make it easier for
   userspace to find a certain port region.

-  ``start``: The first port of this region.

-  ``size``: The number of ports in this region.

-  ``porttype``: A string describing the type of port.

Writing your own kernel module
==============================

Please have a look at ``uio_cif.c`` as an example. The following
paragraphs explain the different sections of this file.

struct uio_info
---------------

This structure tells the framework the details of your driver, Some of
the members are required, others are optional.

-  ``const char *name``: Required. The name of your driver as it will
   appear in sysfs. I recommend using the name of your module for this.

-  ``const char *version``: Required. This string appears in
   ``/sys/class/uio/uioX/version``.

-  ``struct uio_mem mem[ MAX_UIO_MAPS ]``: Required if you have memory
   that can be mapped with :c:func:`mmap()`. For each mapping you
   need to fill one of the ``uio_mem`` structures. See the description
   below for details.

-  ``struct uio_port port[ MAX_UIO_PORTS_REGIONS ]``: Required if you
   want to pass information about ioports to userspace. For each port
   region you need to fill one of the ``uio_port`` structures. See the
   description below for details.

-  ``long irq``: Required. If your hardware generates an interrupt, it's
   your modules task to determine the irq number during initialization.
   If you don't have a hardware generated interrupt but want to trigger
   the interrupt handler in some other way, set ``irq`` to
   ``UIO_IRQ_CUSTOM``. If you had no interrupt at all, you could set
   ``irq`` to ``UIO_IRQ_NONE``, though this rarely makes sense.

-  ``unsigned long irq_flags``: Required if you've set ``irq`` to a
   hardware interrupt number. The flags given here will be used in the
   call to :c:func:`request_irq()`.

-  ``int (*mmap)(struct uio_info *info, struct vm_area_struct *vma)``:
   Optional. If you need a special :c:func:`mmap()`
   function, you can set it here. If this pointer is not NULL, your
   :c:func:`mmap()` will be called instead of the built-in one.

-  ``int (*open)(struct uio_info *info, struct inode *inode)``:
   Optional. You might want to have your own :c:func:`open()`,
   e.g. to enable interrupts only when your device is actually used.

-  ``int (*release)(struct uio_info *info, struct inode *inode)``:
   Optional. If you define your own :c:func:`open()`, you will
   probably also want a custom :c:func:`release()` function.

-  ``int (*irqcontrol)(struct uio_info *info, s32 irq_on)``:
   Optional. If you need to be able to enable or disable interrupts
   from userspace by writing to ``/dev/uioX``, you can implement this
   function. The parameter ``irq_on`` will be 0 to disable interrupts
   and 1 to enable them.

Usually, your device will have one or more memory regions that can be
mapped to user space. For each region, you have to set up a
``struct uio_mem`` in the ``mem[]`` array. Here's a description of the
fields of ``struct uio_mem``:

-  ``const char *name``: Optional. Set this to help identify the memory
   region, it will show up in the corresponding sysfs node.

-  ``int memtype``: Required if the mapping is used. Set this to
   ``UIO_MEM_PHYS`` if you have physical memory on your card to be
   mapped. Use ``UIO_MEM_LOGICAL`` for logical memory (e.g. allocated
   with :c:func:`__get_free_pages()` but not kmalloc()). There's also
   ``UIO_MEM_VIRTUAL`` for virtual memory.

-  ``phys_addr_t addr``: Required if the mapping is used. Fill in the
   address of your memory block. This address is the one that appears in
   sysfs.

-  ``resource_size_t size``: Fill in the size of the memory block that
   ``addr`` points to. If ``size`` is zero, the mapping is considered
   unused. Note that you *must* initialize ``size`` with zero for all
   unused mappings.

-  ``void *internal_addr``: If you have to access this memory region
   from within your kernel module, you will want to map it internally by
   using something like :c:func:`ioremap()`. Addresses returned by
   this function cannot be mapped to user space, so you must not store
   it in ``addr``. Use ``internal_addr`` instead to remember such an
   address.

Please do not touch the ``map`` element of ``struct uio_mem``! It is
used by the UIO framework to set up sysfs files for this mapping. Simply
leave it alone.

Sometimes, your device can have one or more port regions which can not
be mapped to userspace. But if there are other possibilities for
userspace to access these ports, it makes sense to make information
about the ports available in sysfs. For each region, you have to set up
a ``struct uio_port`` in the ``port[]`` array. Here's a description of
the fields of ``struct uio_port``:

-  ``char *porttype``: Required. Set this to one of the predefined
   constants. Use ``UIO_PORT_X86`` for the ioports found in x86
   architectures.

-  ``unsigned long start``: Required if the port region is used. Fill in
   the number of the first port of this region.

-  ``unsigned long size``: Fill in the number of ports in this region.
   If ``size`` is zero, the region is considered unused. Note that you
   *must* initialize ``size`` with zero for all unused regions.

Please do not touch the ``portio`` element of ``struct uio_port``! It is
used internally by the UIO framework to set up sysfs files for this
region. Simply leave it alone.

Adding an interrupt handler
---------------------------

What you need to do in your interrupt handler depends on your hardware
and on how you want to handle it. You should try to keep the amount of
code in your kernel interrupt handler low. If your hardware requires no
action that you *have* to perform after each interrupt, then your
handler can be empty.

If, on the other hand, your hardware *needs* some action to be performed
after each interrupt, then you *must* do it in your kernel module. Note
that you cannot rely on the userspace part of your driver. Your
userspace program can terminate at any time, possibly leaving your
hardware in a state where proper interrupt handling is still required.

There might also be applications where you want to read data from your
hardware at each interrupt and buffer it in a piece of kernel memory
you've allocated for that purpose. With this technique you could avoid
loss of data if your userspace program misses an interrupt.

A note on shared interrupts: Your driver should support interrupt
sharing whenever this is possible. It is possible if and only if your
driver can detect whether your hardware has triggered the interrupt or
not. This is usually done by looking at an interrupt status register. If
your driver sees that the IRQ bit is actually set, it will perform its
actions, and the handler returns IRQ_HANDLED. If the driver detects
that it was not your hardware that caused the interrupt, it will do
nothing and return IRQ_NONE, allowing the kernel to call the next
possible interrupt handler.

If you decide not to support shared interrupts, your card won't work in
computers with no free interrupts. As this frequently happens on the PC
platform, you can save yourself a lot of trouble by supporting interrupt
sharing.

Using uio_pdrv for platform devices
-----------------------------------

In many cases, UIO drivers for platform devices can be handled in a
generic way. In the same place where you define your
``struct platform_device``, you simply also implement your interrupt
handler and fill your ``struct uio_info``. A pointer to this
``struct uio_info`` is then used as ``platform_data`` for your platform
device.

You also need to set up an array of ``struct resource`` containing
addresses and sizes of your memory mappings. This information is passed
to the driver using the ``.resource`` and ``.num_resources`` elements of
``struct platform_device``.

You now have to set the ``.name`` element of ``struct platform_device``
to ``"uio_pdrv"`` to use the generic UIO platform device driver. This
driver will fill the ``mem[]`` array according to the resources given,
and register the device.

The advantage of this approach is that you only have to edit a file you
need to edit anyway. You do not have to create an extra driver.

Using uio_pdrv_genirq for platform devices
------------------------------------------

Especially in embedded devices, you frequently find chips where the irq
pin is tied to its own dedicated interrupt line. In such cases, where
you can be really sure the interrupt is not shared, we can take the
concept of ``uio_pdrv`` one step further and use a generic interrupt
handler. That's what ``uio_pdrv_genirq`` does.

The setup for this driver is the same as described above for
``uio_pdrv``, except that you do not implement an interrupt handler. The
``.handler`` element of ``struct uio_info`` must remain ``NULL``. The
``.irq_flags`` element must not contain ``IRQF_SHARED``.

You will set the ``.name`` element of ``struct platform_device`` to
``"uio_pdrv_genirq"`` to use this driver.

The generic interrupt handler of ``uio_pdrv_genirq`` will simply disable
the interrupt line using :c:func:`disable_irq_nosync()`. After
doing its work, userspace can reenable the interrupt by writing
0x00000001 to the UIO device file. The driver already implements an
:c:func:`irq_control()` to make this possible, you must not
implement your own.

Using ``uio_pdrv_genirq`` not only saves a few lines of interrupt
handler code. You also do not need to know anything about the chip's
internal registers to create the kernel part of the driver. All you need
to know is the irq number of the pin the chip is connected to.

When used in a device-tree enabled system, the driver needs to be
probed with the ``"of_id"`` module parameter set to the ``"compatible"``
string of the node the driver is supposed to handle. By default, the
node's name (without the unit address) is exposed as name for the
UIO device in userspace. To set a custom name, a property named
``"linux,uio-name"`` may be specified in the DT node.

Using uio_dmem_genirq for platform devices
------------------------------------------

In addition to statically allocated memory ranges, they may also be a
desire to use dynamically allocated regions in a user space driver. In
particular, being able to access memory made available through the
dma-mapping API, may be particularly useful. The ``uio_dmem_genirq``
driver provides a way to accomplish this.

This driver is used in a similar manner to the ``"uio_pdrv_genirq"``
driver with respect to interrupt configuration and handling.

Set the ``.name`` element of ``struct platform_device`` to
``"uio_dmem_genirq"`` to use this driver.

When using this driver, fill in the ``.platform_data`` element of
``struct platform_device``, which is of type
``struct uio_dmem_genirq_pdata`` and which contains the following
elements:

-  ``struct uio_info uioinfo``: The same structure used as the
   ``uio_pdrv_genirq`` platform data

-  ``unsigned int *dynamic_region_sizes``: Pointer to list of sizes of
   dynamic memory regions to be mapped into user space.

-  ``unsigned int num_dynamic_regions``: Number of elements in
   ``dynamic_region_sizes`` array.

The dynamic regions defined in the platform data will be appended to the
`` mem[] `` array after the platform device resources, which implies
that the total number of static and dynamic memory regions cannot exceed
``MAX_UIO_MAPS``.

The dynamic memory regions will be allocated when the UIO device file,
``/dev/uioX`` is opened. Similar to static memory resources, the memory
region information for dynamic regions is then visible via sysfs at
``/sys/class/uio/uioX/maps/mapY/*``. The dynamic memory regions will be
freed when the UIO device file is closed. When no processes are holding
the device file open, the address returned to userspace is ~0.

Writing a driver in userspace
=============================

Once you have a working kernel module for your hardware, you can write
the userspace part of your driver. You don't need any special libraries,
your driver can be written in any reasonable language, you can use
floating point numbers and so on. In short, you can use all the tools
and libraries you'd normally use for writing a userspace application.

Getting information about your UIO device
-----------------------------------------

Information about all UIO devices is available in sysfs. The first thing
you should do in your driver is check ``name`` and ``version`` to make
sure you're talking to the right device and that its kernel driver has
the version you expect.

You should also make sure that the memory mapping you need exists and
has the size you expect.

There is a tool called ``lsuio`` that lists UIO devices and their
attributes. It is available here:

http://www.osadl.org/projects/downloads/UIO/user/

With ``lsuio`` you can quickly check if your kernel module is loaded and
which attributes it exports. Have a look at the manpage for details.

The source code of ``lsuio`` can serve as an example for getting
information about an UIO device. The file ``uio_helper.c`` contains a
lot of functions you could use in your userspace driver code.

mmap() device memory
--------------------

After you made sure you've got the right device with the memory mappings
you need, all you have to do is to call :c:func:`mmap()` to map the
device's memory to userspace.

The parameter ``offset`` of the :c:func:`mmap()` call has a special
meaning for UIO devices: It is used to select which mapping of your
device you want to map. To map the memory of mapping N, you have to use
N times the page size as your offset::

        offset = N * getpagesize();

N starts from zero, so if you've got only one memory range to map, set
``offset = 0``. A drawback of this technique is that memory is always
mapped beginning with its start address.

Waiting for interrupts
----------------------

After you successfully mapped your devices memory, you can access it
like an ordinary array. Usually, you will perform some initialization.
After that, your hardware starts working and will generate an interrupt
as soon as it's finished, has some data available, or needs your
attention because an error occurred.

``/dev/uioX`` is a read-only file. A :c:func:`read()` will always
block until an interrupt occurs. There is only one legal value for the
``count`` parameter of :c:func:`read()`, and that is the size of a
signed 32 bit integer (4). Any other value for ``count`` causes
:c:func:`read()` to fail. The signed 32 bit integer read is the
interrupt count of your device. If the value is one more than the value
you read the last time, everything is OK. If the difference is greater
than one, you missed interrupts.

You can also use :c:func:`select()` on ``/dev/uioX``.

Generic PCI UIO driver
======================

The generic driver is a kernel module named uio_pci_generic. It can
work with any device compliant to PCI 2.3 (circa 2002) and any compliant
PCI Express device. Using this, you only need to write the userspace
driver, removing the need to write a hardware-specific kernel module.

Making the driver recognize the device
--------------------------------------

Since the driver does not declare any device ids, it will not get loaded
automatically and will not automatically bind to any devices, you must
load it and allocate id to the driver yourself. For example::

     modprobe uio_pci_generic
     echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id

If there already is a hardware specific kernel driver for your device,
the generic driver still won't bind to it, in this case if you want to
use the generic driver (why would you?) you'll have to manually unbind
the hardware specific driver and bind the generic driver, like this::

        echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
        echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind

You can verify that the device has been bound to the driver by looking
for it in sysfs, for example like the following::

        ls -l /sys/bus/pci/devices/0000:00:19.0/driver

Which if successful should print::

      .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic

Note that the generic driver will not bind to old PCI 2.2 devices. If
binding the device failed, run the following command::

      dmesg

and look in the output for failure reasons.

Things to know about uio_pci_generic
------------------------------------

Interrupts are handled using the Interrupt Disable bit in the PCI
command register and Interrupt Status bit in the PCI status register.
All devices compliant to PCI 2.3 (circa 2002) and all compliant PCI
Express devices should support these bits. uio_pci_generic detects
this support, and won't bind to devices which do not support the
Interrupt Disable Bit in the command register.

On each interrupt, uio_pci_generic sets the Interrupt Disable bit.
This prevents the device from generating further interrupts until the
bit is cleared. The userspace driver should clear this bit before
blocking and waiting for more interrupts.

Writing userspace driver using uio_pci_generic
------------------------------------------------

Userspace driver can use pci sysfs interface, or the libpci library that
wraps it, to talk to the device and to re-enable interrupts by writing
to the command register.

Example code using uio_pci_generic
----------------------------------

Here is some sample userspace driver code using uio_pci_generic::

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>

    int main()
    {
        int uiofd;
        int configfd;
        int err;
        int i;
        unsigned icount;
        unsigned char command_high;

        uiofd = open("/dev/uio0", O_RDONLY);
        if (uiofd < 0) {
            perror("uio open:");
            return errno;
        }
        configfd = open("/sys/class/uio/uio0/device/config", O_RDWR);
        if (configfd < 0) {
            perror("config open:");
            return errno;
        }

        /* Read and cache command value */
        err = pread(configfd, &command_high, 1, 5);
        if (err != 1) {
            perror("command config read:");
            return errno;
        }
        command_high &= ~0x4;

        for(i = 0;; ++i) {
            /* Print out a message, for debugging. */
            if (i == 0)
                fprintf(stderr, "Started uio test driver.\n");
            else
                fprintf(stderr, "Interrupts: %d\n", icount);

            /****************************************/
            /* Here we got an interrupt from the
               device. Do something to it. */
            /****************************************/

            /* Re-enable interrupts. */
            err = pwrite(configfd, &command_high, 1, 5);
            if (err != 1) {
                perror("config write:");
                break;
            }

            /* Wait for next interrupt. */
            err = read(uiofd, &icount, 4);
            if (err != 4) {
                perror("uio read:");
                break;
            }

        }
        return errno;
    }

Generic Hyper-V UIO driver
==========================

The generic driver is a kernel module named uio_hv_generic. It
supports devices on the Hyper-V VMBus similar to uio_pci_generic on
PCI bus.

Making the driver recognize the device
--------------------------------------

Since the driver does not declare any device GUID's, it will not get
loaded automatically and will not automatically bind to any devices, you
must load it and allocate id to the driver yourself. For example, to use
the network device class GUID::

     modprobe uio_hv_generic
     echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" > /sys/bus/vmbus/drivers/uio_hv_generic/new_id

If there already is a hardware specific kernel driver for the device,
the generic driver still won't bind to it, in this case if you want to
use the generic driver for a userspace library you'll have to manually unbind
the hardware specific driver and bind the generic driver, using the device specific GUID
like this::

          echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/hv_netvsc/unbind
          echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/uio_hv_generic/bind

You can verify that the device has been bound to the driver by looking
for it in sysfs, for example like the following::

        ls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver

Which if successful should print::

      .../ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver -> ../../../bus/vmbus/drivers/uio_hv_generic

Things to know about uio_hv_generic
-----------------------------------

On each interrupt, uio_hv_generic sets the Interrupt Disable bit. This
prevents the device from generating further interrupts until the bit is
cleared. The userspace driver should clear this bit before blocking and
waiting for more interrupts.

When host rescinds a device, the interrupt file descriptor is marked down
and any reads of the interrupt file descriptor will return -EIO. Similar
to a closed socket or disconnected serial device.

The vmbus device regions are mapped into uio device resources:
    0) Channel ring buffers: guest to host and host to guest
    1) Guest to host interrupt signalling pages
    2) Guest to host monitor page
    3) Network receive buffer region
    4) Network send buffer region

If a subchannel is created by a request to host, then the uio_hv_generic
device driver will create a sysfs binary file for the per-channel ring buffer.
For example::

	/sys/bus/vmbus/devices/3811fe4d-0fa0-4b62-981a-74fc1084c757/channels/21/ring

Further information
===================

-  `OSADL homepage. <http://www.osadl.org>`_

-  `Linutronix homepage. <http://www.linutronix.de>`_


code:
.. _usb-urb:

USB Request Block (URB)
~~~~~~~~~~~~~~~~~~~~~~~

:Revised: 2000-Dec-05
:Again:   2002-Jul-06
:Again:   2005-Sep-19
:Again:   2017-Mar-29

.. note::

    The USB subsystem now has a substantial section at :ref:`usb-hostside-api`
    section, generated from the current source code.
    This particular documentation file isn't complete and may not be
    updated to the last version; don't rely on it except for a quick
    overview.

Basic concept or 'What is an URB?'
==================================

The basic idea of the new driver is message passing, the message itself is
called USB Request Block, or URB for short.

- An URB consists of all relevant information to execute any USB transaction
  and deliver the data and status back.

- Execution of an URB is inherently an asynchronous operation, i.e. the
  :c:func:`usb_submit_urb` call returns immediately after it has successfully
  queued the requested action.

- Transfers for one URB can be canceled with :c:func:`usb_unlink_urb`
  at any time.

- Each URB has a completion handler, which is called after the action
  has been successfully completed or canceled. The URB also contains a
  context-pointer for passing information to the completion handler.

- Each endpoint for a device logically supports a queue of requests.
  You can fill that queue, so that the USB hardware can still transfer
  data to an endpoint while your driver handles completion of another.
  This maximizes use of USB bandwidth, and supports seamless streaming
  of data to (or from) devices when using periodic transfer modes.

The URB structure
=================

Some of the fields in struct urb are::

  struct urb
  {
  // (IN) device and pipe specify the endpoint queue
	struct usb_device *dev;         // pointer to associated USB device
	unsigned int pipe;              // endpoint information

	unsigned int transfer_flags;    // URB_ISO_ASAP, URB_SHORT_NOT_OK, etc.

  // (IN) all urbs need completion routines
	void *context;                  // context for completion routine
	usb_complete_t complete;        // pointer to completion routine

  // (OUT) status after each completion
	int status;                     // returned status

  // (IN) buffer used for data transfers
	void *transfer_buffer;          // associated data buffer
	u32 transfer_buffer_length;     // data buffer length
	int number_of_packets;          // size of iso_frame_desc

  // (OUT) sometimes only part of CTRL/BULK/INTR transfer_buffer is used
	u32 actual_length;              // actual data buffer length

  // (IN) setup stage for CTRL (pass a struct usb_ctrlrequest)
	unsigned char *setup_packet;    // setup packet (control only)

  // Only for PERIODIC transfers (ISO, INTERRUPT)
    // (IN/OUT) start_frame is set unless URB_ISO_ASAP isn't set
	int start_frame;                // start frame
	int interval;                   // polling interval

    // ISO only: packets are only "best effort"; each can have errors
	int error_count;                // number of errors
	struct usb_iso_packet_descriptor iso_frame_desc[0];
  };

Your driver must create the "pipe" value using values from the appropriate
endpoint descriptor in an interface that it's claimed.

How to get an URB?
==================

URBs are allocated by calling :c:func:`usb_alloc_urb`::

	struct urb *usb_alloc_urb(int isoframes, int mem_flags)

Return value is a pointer to the allocated URB, 0 if allocation failed.
The parameter isoframes specifies the number of isochronous transfer frames
you want to schedule. For CTRL/BULK/INT, use 0.  The mem_flags parameter
holds standard memory allocation flags, letting you control (among other
things) whether the underlying code may block or not.

To free an URB, use :c:func:`usb_free_urb`::

	void usb_free_urb(struct urb *urb)

You may free an urb that you've submitted, but which hasn't yet been
returned to you in a completion callback.  It will automatically be
deallocated when it is no longer in use.

What has to be filled in?
=========================

Depending on the type of transaction, there are some inline functions
defined in ``linux/usb.h`` to simplify the initialization, such as
:c:func:`usb_fill_control_urb`, :c:func:`usb_fill_bulk_urb` and
:c:func:`usb_fill_int_urb`.  In general, they need the usb device pointer,
the pipe (usual format from usb.h), the transfer buffer, the desired transfer
length, the completion handler, and its context. Take a look at the some
existing drivers to see how they're used.

Flags:

- For ISO there are two startup behaviors: Specified start_frame or ASAP.
- For ASAP set ``URB_ISO_ASAP`` in transfer_flags.

If short packets should NOT be tolerated, set ``URB_SHORT_NOT_OK`` in
transfer_flags.

How to submit an URB?
=====================

Just call :c:func:`usb_submit_urb`::

	int usb_submit_urb(struct urb *urb, int mem_flags)

The ``mem_flags`` parameter, such as ``GFP_ATOMIC``, controls memory
allocation, such as whether the lower levels may block when memory is tight.

It immediately returns, either with status 0 (request queued) or some
error code, usually caused by the following:

- Out of memory (``-ENOMEM``)
- Unplugged device (``-ENODEV``)
- Stalled endpoint (``-EPIPE``)
- Too many queued ISO transfers (``-EAGAIN``)
- Too many requested ISO frames (``-EFBIG``)
- Invalid INT interval (``-EINVAL``)
- More than one packet for INT (``-EINVAL``)

After submission, ``urb->status`` is ``-EINPROGRESS``; however, you should
never look at that value except in your completion callback.

For isochronous endpoints, your completion handlers should (re)submit
URBs to the same endpoint with the ``URB_ISO_ASAP`` flag, using
multi-buffering, to get seamless ISO streaming.

How to cancel an already running URB?
=====================================

There are two ways to cancel an URB you've submitted but which hasn't
been returned to your driver yet.  For an asynchronous cancel, call
:c:func:`usb_unlink_urb`::

	int usb_unlink_urb(struct urb *urb)

It removes the urb from the internal list and frees all allocated
HW descriptors. The status is changed to reflect unlinking.  Note
that the URB will not normally have finished when :c:func:`usb_unlink_urb`
returns; you must still wait for the completion handler to be called.

To cancel an URB synchronously, call :c:func:`usb_kill_urb`::

	void usb_kill_urb(struct urb *urb)

It does everything :c:func:`usb_unlink_urb` does, and in addition it waits
until after the URB has been returned and the completion handler
has finished.  It also marks the URB as temporarily unusable, so
that if the completion handler or anyone else tries to resubmit it
they will get a ``-EPERM`` error.  Thus you can be sure that when
:c:func:`usb_kill_urb` returns, the URB is totally idle.

There is a lifetime issue to consider.  An URB may complete at any
time, and the completion handler may free the URB.  If this happens
while :c:func:`usb_unlink_urb` or :c:func:`usb_kill_urb` is running, it will
cause a memory-access violation.  The driver is responsible for avoiding this,
which often means some sort of lock will be needed to prevent the URB
from being deallocated while it is still in use.

On the other hand, since usb_unlink_urb may end up calling the
completion handler, the handler must not take any lock that is held
when usb_unlink_urb is invoked.  The general solution to this problem
is to increment the URB's reference count while holding the lock, then
drop the lock and call usb_unlink_urb or usb_kill_urb, and then
decrement the URB's reference count.  You increment the reference
count by calling :c:func`usb_get_urb`::

	struct urb *usb_get_urb(struct urb *urb)

(ignore the return value; it is the same as the argument) and
decrement the reference count by calling :c:func:`usb_free_urb`.  Of course,
none of this is necessary if there's no danger of the URB being freed
by the completion handler.

What about the completion handler?
==================================

The handler is of the following type::

	typedef void (*usb_complete_t)(struct urb *)

I.e., it gets the URB that caused the completion call. In the completion
handler, you should have a look at ``urb->status`` to detect any USB errors.
Since the context parameter is included in the URB, you can pass
information to the completion handler.

Note that even when an error (or unlink) is reported, data may have been
transferred.  That's because USB transfers are packetized; it might take
sixteen packets to transfer your 1KByte buffer, and ten of them might
have transferred successfully before the completion was called.

.. warning::

   NEVER SLEEP IN A COMPLETION HANDLER.

   These are often called in atomic context.

In the current kernel, completion handlers run with local interrupts
disabled, but in the future this will be changed, so don't assume that
local IRQs are always disabled inside completion handlers.

How to do isochronous (ISO) transfers?
======================================

Besides the fields present on a bulk transfer, for ISO, you also
have to set ``urb->interval`` to say how often to make transfers; it's
often one per frame (which is once every microframe for highspeed devices).
The actual interval used will be a power of two that's no bigger than what
you specify. You can use the :c:func:`usb_fill_int_urb` macro to fill
most ISO transfer fields.

For ISO transfers you also have to fill a :c:type:`usb_iso_packet_descriptor`
structure, allocated at the end of the URB by :c:func:`usb_alloc_urb`, for
each packet you want to schedule.

The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
interval value that is less than or equal to the requested interval value.  If
``URB_ISO_ASAP`` scheduling is used, ``urb->start_frame`` is also updated.

For each entry you have to specify the data offset for this frame (base is
transfer_buffer), and the length you want to write/expect to read.
After completion, actual_length contains the actual transferred length and
status contains the resulting status for the ISO transfer for this frame.
It is allowed to specify a varying length from frame to frame (e.g. for
audio synchronisation/adaptive transfer rates). You can also use the length
0 to omit one or more frames (striping).

For scheduling you can choose your own start frame or ``URB_ISO_ASAP``. As
explained earlier, if you always keep at least one URB queued and your
completion keeps (re)submitting a later URB, you'll get smooth ISO streaming
(if usb bandwidth utilization allows).

If you specify your own start frame, make sure it's several frames in advance
of the current frame.  You might want this model if you're synchronizing
ISO data with some other event stream.

How to start interrupt (INT) transfers?
=======================================

Interrupt transfers, like isochronous transfers, are periodic, and happen
in intervals that are powers of two (1, 2, 4 etc) units.  Units are frames
for full and low speed devices, and microframes for high speed ones.
You can use the :c:func:`usb_fill_int_urb` macro to fill INT transfer fields.

The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
interval value that is less than or equal to the requested interval value.

In Linux 2.6, unlike earlier versions, interrupt URBs are not automagically
restarted when they complete.  They end when the completion handler is
called, just like other URBs.  If you want an interrupt URB to be restarted,
your completion handler must resubmit it.
s


code:
USB Anchors
~~~~~~~~~~~

What is anchor?
===============

A USB driver needs to support some callbacks requiring
a driver to cease all IO to an interface. To do so, a
driver has to keep track of the URBs it has submitted
to know they've all completed or to call usb_kill_urb
for them. The anchor is a data structure takes care of
keeping track of URBs and provides methods to deal with
multiple URBs.

Allocation and Initialisation
=============================

There's no API to allocate an anchor. It is simply declared
as struct usb_anchor. :c:func:`init_usb_anchor` must be called to
initialise the data structure.

Deallocation
============

Once it has no more URBs associated with it, the anchor can be
freed with normal memory management operations.

Association and disassociation of URBs with anchors
===================================================

An association of URBs to an anchor is made by an explicit
call to :c:func:`usb_anchor_urb`. The association is maintained until
an URB is finished by (successful) completion. Thus disassociation
is automatic. A function is provided to forcibly finish (kill)
all URBs associated with an anchor.
Furthermore, disassociation can be made with :c:func:`usb_unanchor_urb`

Operations on multitudes of URBs
================================

:c:func:`usb_kill_anchored_urbs`
--------------------------------

This function kills all URBs associated with an anchor. The URBs
are called in the reverse temporal order they were submitted.
This way no data can be reordered.

:c:func:`usb_unlink_anchored_urbs`
----------------------------------

This function unlinks all URBs associated with an anchor. The URBs
are processed in the reverse temporal order they were submitted.
This is similar to :c:func:`usb_kill_anchored_urbs`, but it will not sleep.
Therefore no guarantee is made that the URBs have been unlinked when
the call returns. They may be unlinked later but will be unlinked in
finite time.

:c:func:`usb_scuttle_anchored_urbs`
-----------------------------------

All URBs of an anchor are unanchored en masse.

:c:func:`usb_wait_anchor_empty_timeout`
---------------------------------------

This function waits for all URBs associated with an anchor to finish
or a timeout, whichever comes first. Its return value will tell you
whether the timeout was reached.

:c:func:`usb_anchor_empty`
--------------------------

Returns true if no URBs are associated with an anchor. Locking
is the caller's responsibility.

:c:func:`usb_get_from_anchor`
-----------------------------

Returns the oldest anchored URB of an anchor. The URB is unanchored
and returned with a reference. As you may mix URBs to several
destinations in one anchor you have no guarantee the chronologically
first submitted URB is returned.


code:
USB bulk streams
~~~~~~~~~~~~~~~~

Background
==========

Bulk endpoint streams were added in the USB 3.0 specification.  Streams allow a
device driver to overload a bulk endpoint so that multiple transfers can be
queued at once.

Streams are defined in sections 4.4.6.4 and 8.12.1.4 of the Universal Serial Bus
3.0 specification at https://www.usb.org/developers/docs/  The USB Attached SCSI
Protocol, which uses streams to queue multiple SCSI commands, can be found on
the T10 website (https://t10.org/).

Device-side implications
========================

Once a buffer has been queued to a stream ring, the device is notified (through
an out-of-band mechanism on another endpoint) that data is ready for that stream
ID.  The device then tells the host which "stream" it wants to start.  The host
can also initiate a transfer on a stream without the device asking, but the
device can refuse that transfer.  Devices can switch between streams at any
time.

Driver implications
===================

::

  int usb_alloc_streams(struct usb_interface *interface,
		struct usb_host_endpoint **eps, unsigned int num_eps,
		unsigned int num_streams, gfp_t mem_flags);

Device drivers will call this API to request that the host controller driver
allocate memory so the driver can use up to num_streams stream IDs.  They must
pass an array of usb_host_endpoints that need to be setup with similar stream
IDs.  This is to ensure that a UASP driver will be able to use the same stream
ID for the bulk IN and OUT endpoints used in a Bi-directional command sequence.

The return value is an error condition (if one of the endpoints doesn't support
streams, or the xHCI driver ran out of memory), or the number of streams the
host controller allocated for this endpoint.  The xHCI host controller hardware
declares how many stream IDs it can support, and each bulk endpoint on a
SuperSpeed device will say how many stream IDs it can handle.  Therefore,
drivers should be able to deal with being allocated less stream IDs than they
requested.

Do NOT call this function if you have URBs enqueued for any of the endpoints
passed in as arguments.  Do not call this function to request less than two
streams.

Drivers will only be allowed to call this API once for the same endpoint
without calling usb_free_streams().  This is a simplification for the xHCI host
controller driver, and may change in the future.

Picking new Stream IDs to use
=============================

Stream ID 0 is reserved, and should not be used to communicate with devices.  If
usb_alloc_streams() returns with a value of N, you may use streams 1 though N.
To queue an URB for a specific stream, set the urb->stream_id value.  If the
endpoint does not support streams, an error will be returned.

Note that new API to choose the next stream ID will have to be added if the xHCI
driver supports secondary stream IDs.

Clean up
========

If a driver wishes to stop using streams to communicate with the device, it
should call::

  void usb_free_streams(struct usb_interface *interface,
		struct usb_host_endpoint **eps, unsigned int num_eps,
		gfp_t mem_flags);

All stream IDs will be deallocated when the driver releases the interface, to
ensure that drivers that don't support streams will be able to use the endpoint.


code:
USB core callbacks
~~~~~~~~~~~~~~~~~~

What callbacks will usbcore do?
===============================

Usbcore will call into a driver through callbacks defined in the driver
structure and through the completion handler of URBs a driver submits.
Only the former are in the scope of this document. These two kinds of
callbacks are completely independent of each other. Information on the
completion callback can be found in :ref:`usb-urb`.

The callbacks defined in the driver structure are:

1. Hotplugging callbacks:

 - @probe:
	Called to see if the driver is willing to manage a particular
	interface on a device.

 - @disconnect:
	Called when the interface is no longer accessible, usually
	because its device has been (or is being) disconnected or the
	driver module is being unloaded.

2. Odd backdoor through usbfs:

 - @ioctl:
	Used for drivers that want to talk to userspace through
	the "usbfs" filesystem.  This lets devices provide ways to
	expose information to user space regardless of where they
	do (or don't) show up otherwise in the filesystem.

3. Power management (PM) callbacks:

 - @suspend:
	Called when the device is going to be suspended.

 - @resume:
	Called when the device is being resumed.

 - @reset_resume:
	Called when the suspended device has been reset instead
	of being resumed.

4. Device level operations:

 - @pre_reset:
	Called when the device is about to be reset.

 - @post_reset:
	Called after the device has been reset

The ioctl interface (2) should be used only if you have a very good
reason. Sysfs is preferred these days. The PM callbacks are covered
separately in :ref:`usb-power-management`.

Calling conventions
===================

All callbacks are mutually exclusive. There's no need for locking
against other USB callbacks. All callbacks are called from a task
context. You may sleep. However, it is important that all sleeps have a
small fixed upper limit in time. In particular you must not call out to
user space and await results.

Hotplugging callbacks
=====================

These callbacks are intended to associate and disassociate a driver with
an interface. A driver's bond to an interface is exclusive.

The probe() callback
--------------------

::

  int (*probe) (struct usb_interface *intf,
		const struct usb_device_id *id);

Accept or decline an interface. If you accept the device return 0,
otherwise -ENODEV or -ENXIO. Other error codes should be used only if a
genuine error occurred during initialisation which prevented a driver
from accepting a device that would else have been accepted.
You are strongly encouraged to use usbcore's facility,
usb_set_intfdata(), to associate a data structure with an interface, so
that you know which internal state and identity you associate with a
particular interface. The device will not be suspended and you may do IO
to the interface you are called for and endpoint 0 of the device. Device
initialisation that doesn't take too long is a good idea here.

The disconnect() callback
-------------------------

::

  void (*disconnect) (struct usb_interface *intf);

This callback is a signal to break any connection with an interface.
You are not allowed any IO to a device after returning from this
callback. You also may not do any other operation that may interfere
with another driver bound the interface, eg. a power management
operation.
If you are called due to a physical disconnection, all your URBs will be
killed by usbcore. Note that in this case disconnect will be called some
time after the physical disconnection. Thus your driver must be prepared
to deal with failing IO even prior to the callback.

Device level callbacks
======================

pre_reset
---------

::

  int (*pre_reset)(struct usb_interface *intf);

A driver or user space is triggering a reset on the device which
contains the interface passed as an argument. Cease IO, wait for all
outstanding URBs to complete, and save any device state you need to
restore.  No more URBs may be submitted until the post_reset method
is called.

If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
are in atomic context.

post_reset
----------

::

  int (*post_reset)(struct usb_interface *intf);

The reset has completed.  Restore any saved device state and begin
using the device again.

If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
are in atomic context.

Call sequences
==============

No callbacks other than probe will be invoked for an interface
that isn't bound to your driver.

Probe will never be called for an interface bound to a driver.
Hence following a successful probe, disconnect will be called
before there is another probe for the same interface.

Once your driver is bound to an interface, disconnect can be
called at any time except in between pre_reset and post_reset.
pre_reset is always followed by post_reset, even if the reset
failed or the device has been unplugged.

suspend is always followed by one of: resume, reset_resume, or
disconnect.


code:
USB DMA
~~~~~~~

In Linux 2.5 kernels (and later), USB device drivers have additional control
over how DMA may be used to perform I/O operations.  The APIs are detailed
in the kernel usb programming guide (kerneldoc, from the source code).

API overview
============

The big picture is that USB drivers can continue to ignore most DMA issues,
though they still must provide DMA-ready buffers (see
Documentation/core-api/dma-api-howto.rst).  That's how they've worked through
the 2.4 (and earlier) kernels, or they can now be DMA-aware.

DMA-aware usb drivers:

- New calls enable DMA-aware drivers, letting them allocate dma buffers and
  manage dma mappings for existing dma-ready buffers (see below).

- URBs have an additional "transfer_dma" field, as well as a transfer_flags
  bit saying if it's valid.  (Control requests also have "setup_dma", but
  drivers must not use it.)

- "usbcore" will map this DMA address, if a DMA-aware driver didn't do
  it first and set ``URB_NO_TRANSFER_DMA_MAP``.  HCDs
  don't manage dma mappings for URBs.

- There's a new "generic DMA API", parts of which are usable by USB device
  drivers.  Never use dma_set_mask() on any USB interface or device; that
  would potentially break all devices sharing that bus.

Eliminating copies
==================

It's good to avoid making CPUs copy data needlessly.  The costs can add up,
and effects like cache-trashing can impose subtle penalties.

- If you're doing lots of small data transfers from the same buffer all
  the time, that can really burn up resources on systems which use an
  IOMMU to manage the DMA mappings.  It can cost MUCH more to set up and
  tear down the IOMMU mappings with each request than perform the I/O!

  For those specific cases, USB has primitives to allocate less expensive
  memory.  They work like kmalloc and kfree versions that give you the right
  kind of addresses to store in urb->transfer_buffer and urb->transfer_dma.
  You'd also set ``URB_NO_TRANSFER_DMA_MAP`` in urb->transfer_flags::

	void *usb_alloc_coherent (struct usb_device *dev, size_t size,
		int mem_flags, dma_addr_t *dma);

	void usb_free_coherent (struct usb_device *dev, size_t size,
		void *addr, dma_addr_t dma);

  Most drivers should **NOT** be using these primitives; they don't need
  to use this type of memory ("dma-coherent"), and memory returned from
  :c:func:`kmalloc` will work just fine.

  The memory buffer returned is "dma-coherent"; sometimes you might need to
  force a consistent memory access ordering by using memory barriers.  It's
  not using a streaming DMA mapping, so it's good for small transfers on
  systems where the I/O would otherwise thrash an IOMMU mapping.  (See
  Documentation/core-api/dma-api-howto.rst for definitions of "coherent" and
  "streaming" DMA mappings.)

  Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
  space-efficient.

  On most systems the memory returned will be uncached, because the
  semantics of dma-coherent memory require either bypassing CPU caches
  or using cache hardware with bus-snooping support.  While x86 hardware
  has such bus-snooping, many other systems use software to flush cache
  lines to prevent DMA conflicts.

- Devices on some EHCI controllers could handle DMA to/from high memory.

  Unfortunately, the current Linux DMA infrastructure doesn't have a sane
  way to expose these capabilities ... and in any case, HIGHMEM is mostly a
  design wart specific to x86_32.  So your best bet is to ensure you never
  pass a highmem buffer into a USB driver.  That's easy; it's the default
  behavior.  Just don't override it; e.g. with ``NETIF_F_HIGHDMA``.

  This may force your callers to do some bounce buffering, copying from
  high memory to "normal" DMA memory.  If you can come up with a good way
  to fix this issue (for x86_32 machines with over 1 GByte of memory),
  feel free to submit patches.

Working with existing buffers
=============================

Existing buffers aren't usable for DMA without first being mapped into the
DMA address space of the device.  However, most buffers passed to your
driver can safely be used with such DMA mapping.  (See the first section
of Documentation/core-api/dma-api-howto.rst, titled "What memory is DMA-able?")

- When you're using scatterlists, you can map everything at once.  On some
  systems, this kicks in an IOMMU and turns the scatterlists into single
  DMA transactions::

	int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
		struct scatterlist *sg, int nents);

	void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
		struct scatterlist *sg, int n_hw_ents);

	void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
		struct scatterlist *sg, int n_hw_ents);

  It's probably easier to use the new ``usb_sg_*()`` calls, which do the DMA
  mapping and apply other tweaks to make scatterlist i/o be fast.

- Some drivers may prefer to work with the model that they're mapping large
  buffers, synchronizing their safe re-use.  (If there's no re-use, then let
  usbcore do the map/unmap.)  Large periodic transfers make good examples
  here, since it's cheaper to just synchronize the buffer than to unmap it
  each time an urb completes and then re-map it on during resubmission.

  These calls all work with initialized urbs:  ``urb->dev``, ``urb->pipe``,
  ``urb->transfer_buffer``, and ``urb->transfer_buffer_length`` must all be
  valid when these calls are used (``urb->setup_packet`` must be valid too
  if urb is a control request)::

	struct urb *usb_buffer_map (struct urb *urb);

	void usb_buffer_dmasync (struct urb *urb);

	void usb_buffer_unmap (struct urb *urb);

  The calls manage ``urb->transfer_dma`` for you, and set
  ``URB_NO_TRANSFER_DMA_MAP`` so that usbcore won't map or unmap the buffer.
  They cannot be used for setup_packet buffers in control requests.

Note that several of those interfaces are currently commented out, since
they don't have current users.  See the source code.  Other than the dmasync
calls (where the underlying DMA primitives have changed), most of them can
easily be commented back in if you want to use them.