Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

@@ -0,0 +1,313 @@
What: /sys/bus/most/devices/.../description
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Provides information about the interface type and the physical
location of the device. Hardware attached via USB, for instance,
might return <usb_device 1-1.1:1.0>
Users:
What: /sys/bus/most/devices/.../interface
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the type of peripheral interface the device uses.
Users:
What: /sys/bus/most/devices/.../dci
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
If the network interface controller is attached via USB, a dci
directory is created that allows applications to read and
write the controller's DCI registers.
Users:
What: /sys/bus/most/devices/.../dci/arb_address
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to set an arbitrary DCI register address an
application wants to read from or write to.
Users:
What: /sys/bus/most/devices/.../dci/arb_value
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to read and write the DCI register whose address
is stored in arb_address.
Users:
What: /sys/bus/most/devices/.../dci/mep_eui48_hi
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MAC address.
Users:
What: /sys/bus/most/devices/.../dci/mep_eui48_lo
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MAC address.
Users:
What: /sys/bus/most/devices/.../dci/mep_eui48_mi
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MAC address.
Users:
What: /sys/bus/most/devices/.../dci/mep_filter
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP filter address.
Users:
What: /sys/bus/most/devices/.../dci/mep_hash0
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/bus/most/devices/.../dci/mep_hash1
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/bus/most/devices/.../dci/mep_hash2
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/bus/most/devices/.../dci/mep_hash3
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/bus/most/devices/.../dci/ni_state
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the current network interface state.
Users:
What: /sys/bus/most/devices/.../dci/node_address
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the current node address.
Users:
What: /sys/bus/most/devices/.../dci/node_position
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the current node position.
Users:
What: /sys/bus/most/devices/.../dci/packet_bandwidth
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the configured packet bandwidth.
Users:
What: /sys/bus/most/devices/.../dci/sync_ep
Date: June 2016
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Triggers the controller's synchronization process for a certain
endpoint.
Users:
What: /sys/bus/most/devices/.../<channel>/
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
For every channel of the device a directory is created, whose
name is dictated by the HDM. This enables an application to
collect information about the channel's capabilities and
configure it.
Users:
What: /sys/bus/most/devices/.../<channel>/available_datatypes
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the data types the current channel can transport.
Users:
What: /sys/bus/most/devices/.../<channel>/available_directions
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the directions the current channel is capable of.
Users:
What: /sys/bus/most/devices/.../<channel>/number_of_packet_buffers
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the number of packet buffers the current channel can
handle.
Users:
What: /sys/bus/most/devices/.../<channel>/number_of_stream_buffers
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the number of streaming buffers the current channel can
handle.
Users:
What: /sys/bus/most/devices/.../<channel>/size_of_packet_buffer
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the size of a packet buffer the current channel can
handle.
Users:
What: /sys/bus/most/devices/.../<channel>/size_of_stream_buffer
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the size of a streaming buffer the current channel can
handle.
Users:
What: /sys/bus/most/devices/.../<channel>/set_number_of_buffers
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the number of buffers of the current channel.
Users:
What: /sys/bus/most/devices/.../<channel>/set_buffer_size
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the size of a buffer of the current channel.
Users:
What: /sys/bus/most/devices/.../<channel>/set_direction
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the direction of the current channel.
The following strings will be accepted:
'dir_tx',
'dir_rx'
Users:
What: /sys/bus/most/devices/.../<channel>/set_datatype
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the data type of the current channel.
The following strings will be accepted:
'control',
'async',
'sync',
'isoc_avp'
Users:
What: /sys/bus/most/devices/.../<channel>/set_subbuffer_size
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the subbuffer size of the current channel.
Users:
What: /sys/bus/most/devices/.../<channel>/set_packets_per_xact
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the number of packets per transaction of
the current channel. This is only needed network interface
controller is attached via USB.
Users:
What: /sys/bus/most/devices/.../<channel>/channel_starving
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates whether current channel ran out of buffers.
Users:
What: /sys/bus/most/drivers/mostcore/add_link
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to link a channel to a component of the
mostcore. A link created by writing to this file is
referred to as pipe.
Users:
What: /sys/bus/most/drivers/mostcore/remove_link
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to unlink a channel from a component.
Users:
What: /sys/bus/most/drivers/mostcore/components
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to retrieve a list of registered components.
Users:
What: /sys/bus/most/drivers/mostcore/links
Date: March 2017
KernelVersion: 4.15
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to retrieve a list of established links.
Users:
@@ -0,0 +1,315 @@
What: /sys/class/most/mostcore/aims
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
List of AIMs that have been loaded.
Users:
What: /sys/class/most/mostcore/aims/<aim>/add_link
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to establish a connection of a channel and the
current AIM.
Users:
What: /sys/class/most/mostcore/aims/<aim>/remove_link
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to remove a connected channel from the
current AIM.
Users:
What: /sys/class/most/mostcore/devices
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
List of attached MOST interfaces.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/description
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Provides information about the interface type and the physical
location of the device. Hardware attached via USB, for instance,
might return <usb_device 1-1.1:1.0>
Users:
What: /sys/class/most/mostcore/devices/<mdev>/interface
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the type of peripheral interface the current device
uses.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
If the network interface controller is attached via USB, a dci
directory is created that allows applications to use the
controller's direct communication interface (DCI) to exchange
information.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/arb_address
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to set an arbitrary DCI register address an
application wants to read from or write to.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/arb_value
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to read from or write to the arbitrary DCI register
whose address is stored in arb_address.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_eui48_hi
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MAC address.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_eui48_lo
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MAC address.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_eui48_mi
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MAC address.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_filter
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP filter address.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_hash0
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_hash1
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_hash2
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/mep_hash3
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is used to check and configure the MEP hash table.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/ni_state
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the current network interface state.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/node_address
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the current node address.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/node_position
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the current node position.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/packet_bandwidth
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the configured packet bandwidth.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/dci/sync_ep
Date: June 2016
KernelVersion: 4.9
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Triggers the controller's synchronization process for a certain
endpoint.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
For every channel of the device a directory is created, whose
name is dictated by the HDM. This enables an application to
collect information about the channel's capabilities and
configure it.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/available_datatypes
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the data types the current channel can transport.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/available_directions
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the directions the current channel is capable of.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/number_of_packet_buffers
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the number of packet buffers the current channel can
handle.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/number_of_stream_buffers
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the number of streaming buffers the current channel can
handle.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/size_of_packet_buffer
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the size of a packet buffer the current channel can
handle.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/size_of_stream_buffer
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates the size of a streaming buffer the current channel can
handle.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/set_number_of_buffers
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the number of buffers of the current channel.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/set_buffer_size
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the size of a buffer of the current channel.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/set_direction
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the direction of the current channel.
The following strings will be accepted:
'dir_tx',
'dir_rx'
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/set_datatype
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the data type of the current channel.
The following strings will be accepted:
'control',
'async',
'sync',
'isoc_avp'
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/set_subbuffer_size
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the subbuffer size of the current channel.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/set_packets_per_xact
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
This is to configure the number of packets per transaction of
the current channel. This is only needed network interface
controller is attached via USB.
Users:
What: /sys/class/most/mostcore/devices/<mdev>/<channel>/channel_starving
Date: June 2015
KernelVersion: 4.3
Contact: Christian Gromm <christian.gromm@microchip.com>
Description:
Indicates whether current channel ran out of buffers.
Users:
@@ -0,0 +1,198 @@
Section 1 Overview
The Media Oriented Systems Transport (MOST) driver gives Linux applications
access a MOST network: The Automotive Information Backbone and the de-facto
standard for high-bandwidth automotive multimedia networking.
MOST defines the protocol, hardware and software layers necessary to allow
for the efficient and low-cost transport of control, real-time and packet
data using a single medium (physical layer). Media currently in use are
fiber optics, unshielded twisted pair cables (UTP) and coax cables. MOST
also supports various speed grades up to 150 Mbps.
For more information on MOST, visit the MOST Cooperation website:
www.mostcooperation.com.
Cars continue to evolve into sophisticated consumer electronics platforms,
increasing the demand for reliable and simple solutions to support audio,
video and data communications. MOST can be used to connect multiple
consumer devices via optical or electrical physical layers directly to one
another or in a network configuration. As a synchronous network, MOST
provides excellent Quality of Service and seamless connectivity for
audio/video streaming. Therefore, the driver perfectly fits to the mission
of Automotive Grade Linux to create open source software solutions for
automotive applications.
The MOST driver uses module stacking to divide the associated modules into
three layers. From bottom up these layers are: the adapter layer, the core
layer and the application layer. The core layer implements the MOST
subsystem and consists basically of the module core.c and its API. It
registers the MOST bus with the kernel's device model, handles the data
routing through all three layers, the configuration of the driver, the
representation of the configuration interface in sysfs and the buffer
management.
For each of the other two layers a set of modules is provided. Those can be
arbitrarily combined with the core to meet the connectivity of the desired
system architecture.
A module of the adapter layer is basically a device driver for a different
subsystem. It is registered with the core to connect the MOST subsystem to
the attached network interface controller hardware. Hence, a given module
of this layer is designed to handle exactly one of the peripheral
interfaces (e.g. USB, MediaLB, I2C) the hardware provides.
A module of the application layer is referred to as a core comoponent,
which kind of extends the core by providing connectivity to the user space.
Applications, then, can access a MOST network via character devices, an
ALSA soundcard, a Network adapter or a V4L2 capture device.
To physically access MOST, an Intelligent Network Interface Controller
(INIC) is needed. For more information on available controllers visit:
www.microchip.com
Section 1.1 Adapter Layer
The adapter layer contains a pool of device drivers. For each peripheral
interface the hardware supports there is one suitable module that handles
the interface. Adapter drivers encapsulate the peripheral interface
specific knowledge of the MOST driver stack and provide an easy way of
extending the number of supported interfaces. Currently the following
interfaces are available:
1) MediaLB (DIM2)
Host wants to communicate with hardware via MediaLB.
2) I2C
Host wants to communicate with the hardware via I2C.
3) USB
Host wants to communicate with the hardware via USB.
Once an adapter driver recognizes a MOST device being attached, it
registers it with the core, which, in turn, assigns the necessary members
of the embedded struct device (e.g. the bus this device belongs to and
attribute groups) and registers it with the kernel's device model.
Section 1.2 Core Layer
This layer implements the MOST subsystem. It contains the core module and
the header file most.h that exposes the API of the core. When inserted in
the kernel, it registers the MOST bus_type with the kernel's device model
and registers itself as a device driver for this bus. Besides these meta
tasks the core populates the configuration directory for a registered MOST
device (represented by struct most_interface) in sysfs and processes the
configuration of the device's interface. The core layer also handles the
buffer management and the data/message routing.
Section 1.3 Application Layer
This layer contains a pool of device drivers that are components of the
core designed to make up the userspace experience of the MOST driver stack.
Depending on how an application is meant to interface the driver, one or
more modules of this pool can be registered with the core. Currently the
following components are available
1) Character Device
Userspace can access the driver by means of character devices.
2) Networking
Standard networking applications (e.g. iperf) can by used to access
the driver via the networking subsystem.
3) Video4Linux (v4l2)
Standard video applications (e.g. VLC) can by used to access the
driver via the V4L subsystem.
4) Advanced Linux Sound Architecture (ALSA)
Standard sound applications (e.g. aplay, arecord, audacity) can by
used to access the driver via the ALSA subsystem.
Section 2 Usage of the MOST Driver
Section 2.1 Configuration
See ABI/sysfs-bus-most.txt
Section 2.2 Routing Channels
To connect a configured channel to a certain core component and make it
accessible for user space applications, the driver attribute 'add_link' is
used. The configuration string passed to it has the following format:
"device_name:channel_name:component_name:link_name[.param]"
It is the concatenation of up to four substrings separated by a colon. The
substrings contain the names of the MOST interface, the channel, the
component driver and a custom name with which the link is going to be
referenced with. Since some components need additional information, the
link name can be extended with a component-specific parameter (separated by
a dot). In case the character device component is loaded, the handle would
also appear as a device node in the /dev directory.
Cdev component example:
$ echo "mdev0:ep_81:cdev:my_rx_channel" >$(DRV_DIR)/add_link
Sound component example:
The sound component needs an additional parameter to determine the audio
resolution that is going to be used. The following formats are available:
- "1x8" (Mono)
- "2x16" (16-bit stereo)
- "2x24" (24-bit stereo)
- "2x32" (32-bit stereo)
- "6x16" (16-bit surround 5.1)
$ echo "mdev0:ep_81:sound:most51_playback.6x16" >$(DRV_DIR)/add_link
Section 2.3 USB Padding
When transceiving synchronous or isochronous data, the number of packets
per USB transaction and the sub-buffer size need to be configured. These
values are needed for the driver to process buffer padding, as expected by
hardware, which is for performance optimization purposes of the USB
transmission.
When transmitting synchronous data the allocated channel width needs to be
written to 'set_subbuffer_size'. Additionally, the number of MOST frames
that should travel to the host within one USB transaction need to be
written to 'packets_per_xact'.
The driver, then, calculates the synchronous threshold as follows:
frame_size = set_subbuffer_size * packets_per_xact
In case 'packets_per_xact' is set to 0xFF the maximum number of packets,
allocated within one MOST frame, is calculated that fit into _one_ 512 byte
USB full packet.
frame_size = floor(MTU_USB / bandwidth_sync) * bandwidth_sync
This frame_size is the number of synchronous data within an USB
transaction, which renders MTU_USB - frame_size bytes for padding.
When transmitting isochronous AVP data the desired packet size needs to be
written to 'set_subbuffer_size' and hardware will always expect two
isochronous packets within one USB transaction. This renders
MTU_USB - (2 * set_subbuffer_size)
bytes for padding.
Note that at least (2 * set_subbuffer_size) bytes for isochronous data or
(set_subbuffer_size * packts_per_xact) bytes for synchronous data need to
be put in the transmission buffer and passed to the driver.
Since adapter drivers are allowed to change a chosen configuration to best
fit its constraints, it is recommended to always double check the
configuration and read back the previously written files.
+34
View File
@@ -0,0 +1,34 @@
menuconfig MOST
tristate "MOST support"
depends on HAS_DMA
default n
---help---
Say Y here if you want to enable MOST support.
This driver needs at least one additional component to enable the
desired access from userspace (e.g. character devices) and one that
matches the network controller's hardware interface (e.g. USB).
To compile this driver as a module, choose M here: the
module will be called most_core.
If in doubt, say N here.
if MOST
source "drivers/staging/most/cdev/Kconfig"
source "drivers/staging/most/net/Kconfig"
source "drivers/staging/most/sound/Kconfig"
source "drivers/staging/most/video/Kconfig"
source "drivers/staging/most/dim2/Kconfig"
source "drivers/staging/most/i2c/Kconfig"
source "drivers/staging/most/usb/Kconfig"
endif
+12
View File
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_MOST) += most_core.o
most_core-y := core.o
ccflags-y += -Idrivers/staging/
obj-$(CONFIG_MOST_CDEV) += cdev/
obj-$(CONFIG_MOST_NET) += net/
obj-$(CONFIG_MOST_SOUND) += sound/
obj-$(CONFIG_MOST_VIDEO) += video/
obj-$(CONFIG_MOST_DIM2) += dim2/
obj-$(CONFIG_MOST_I2C) += i2c/
obj-$(CONFIG_MOST_USB) += usb/
+8
View File
@@ -0,0 +1,8 @@
* Get through code review with Greg Kroah-Hartman
Contact:
To:
Christian Gromm <christian.gromm@microchip.com>
Cc:
Michael Fabry <Michael.Fabry@microchip.com>
Christian Gromm <chris@engineersdelight.de>
+12
View File
@@ -0,0 +1,12 @@
#
# MOST Cdev configuration
#
config MOST_CDEV
tristate "Cdev"
---help---
Say Y here if you want to commumicate via character devices.
To compile this driver as a module, choose M here: the
module will be called most_cdev.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_CDEV) += most_cdev.o
most_cdev-objs := cdev.o
ccflags-y += -Idrivers/staging/
+561
View File
@@ -0,0 +1,561 @@
// SPDX-License-Identifier: GPL-2.0
/*
* cdev.c - Character device component for Mostcore
*
* Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#include <linux/uaccess.h>
#include <linux/idr.h>
#include "most/core.h"
#define CHRDEV_REGION_SIZE 50
static struct cdev_component {
dev_t devno;
struct ida minor_id;
unsigned int major;
struct class *class;
struct core_component cc;
} comp;
struct comp_channel {
wait_queue_head_t wq;
spinlock_t unlink; /* synchronization lock to unlink channels */
struct cdev cdev;
struct device *dev;
struct mutex io_mutex;
struct most_interface *iface;
struct most_channel_config *cfg;
unsigned int channel_id;
dev_t devno;
size_t mbo_offs;
DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
int access_ref;
struct list_head list;
};
#define to_channel(d) container_of(d, struct comp_channel, cdev)
static struct list_head channel_list;
static spinlock_t ch_list_lock;
static inline bool ch_has_mbo(struct comp_channel *c)
{
return channel_has_mbo(c->iface, c->channel_id, &comp.cc) > 0;
}
static inline struct mbo *ch_get_mbo(struct comp_channel *c, struct mbo **mbo)
{
if (!kfifo_peek(&c->fifo, mbo)) {
*mbo = most_get_mbo(c->iface, c->channel_id, &comp.cc);
if (*mbo)
kfifo_in(&c->fifo, mbo, 1);
}
return *mbo;
}
static struct comp_channel *get_channel(struct most_interface *iface, int id)
{
struct comp_channel *c, *tmp;
unsigned long flags;
int found_channel = 0;
spin_lock_irqsave(&ch_list_lock, flags);
list_for_each_entry_safe(c, tmp, &channel_list, list) {
if ((c->iface == iface) && (c->channel_id == id)) {
found_channel = 1;
break;
}
}
spin_unlock_irqrestore(&ch_list_lock, flags);
if (!found_channel)
return NULL;
return c;
}
static void stop_channel(struct comp_channel *c)
{
struct mbo *mbo;
while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
most_put_mbo(mbo);
most_stop_channel(c->iface, c->channel_id, &comp.cc);
}
static void destroy_cdev(struct comp_channel *c)
{
unsigned long flags;
device_destroy(comp.class, c->devno);
cdev_del(&c->cdev);
spin_lock_irqsave(&ch_list_lock, flags);
list_del(&c->list);
spin_unlock_irqrestore(&ch_list_lock, flags);
}
static void destroy_channel(struct comp_channel *c)
{
ida_simple_remove(&comp.minor_id, MINOR(c->devno));
kfifo_free(&c->fifo);
kfree(c);
}
/**
* comp_open - implements the syscall to open the device
* @inode: inode pointer
* @filp: file pointer
*
* This stores the channel pointer in the private data field of
* the file structure and activates the channel within the core.
*/
static int comp_open(struct inode *inode, struct file *filp)
{
struct comp_channel *c;
int ret;
c = to_channel(inode->i_cdev);
filp->private_data = c;
if (((c->cfg->direction == MOST_CH_RX) &&
((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
((c->cfg->direction == MOST_CH_TX) &&
((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
pr_info("WARN: Access flags mismatch\n");
return -EACCES;
}
mutex_lock(&c->io_mutex);
if (!c->dev) {
pr_info("WARN: Device is destroyed\n");
mutex_unlock(&c->io_mutex);
return -ENODEV;
}
if (c->access_ref) {
pr_info("WARN: Device is busy\n");
mutex_unlock(&c->io_mutex);
return -EBUSY;
}
c->mbo_offs = 0;
ret = most_start_channel(c->iface, c->channel_id, &comp.cc);
if (!ret)
c->access_ref = 1;
mutex_unlock(&c->io_mutex);
return ret;
}
/**
* comp_close - implements the syscall to close the device
* @inode: inode pointer
* @filp: file pointer
*
* This stops the channel within the core.
*/
static int comp_close(struct inode *inode, struct file *filp)
{
struct comp_channel *c = to_channel(inode->i_cdev);
mutex_lock(&c->io_mutex);
spin_lock(&c->unlink);
c->access_ref = 0;
spin_unlock(&c->unlink);
if (c->dev) {
stop_channel(c);
mutex_unlock(&c->io_mutex);
} else {
mutex_unlock(&c->io_mutex);
destroy_channel(c);
}
return 0;
}
/**
* comp_write - implements the syscall to write to the device
* @filp: file pointer
* @buf: pointer to user buffer
* @count: number of bytes to write
* @offset: offset from where to start writing
*/
static ssize_t comp_write(struct file *filp, const char __user *buf,
size_t count, loff_t *offset)
{
int ret;
size_t to_copy, left;
struct mbo *mbo = NULL;
struct comp_channel *c = filp->private_data;
mutex_lock(&c->io_mutex);
while (c->dev && !ch_get_mbo(c, &mbo)) {
mutex_unlock(&c->io_mutex);
if ((filp->f_flags & O_NONBLOCK))
return -EAGAIN;
if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev))
return -ERESTARTSYS;
mutex_lock(&c->io_mutex);
}
if (unlikely(!c->dev)) {
ret = -ENODEV;
goto unlock;
}
to_copy = min(count, c->cfg->buffer_size - c->mbo_offs);
left = copy_from_user(mbo->virt_address + c->mbo_offs, buf, to_copy);
if (left == to_copy) {
ret = -EFAULT;
goto unlock;
}
c->mbo_offs += to_copy - left;
if (c->mbo_offs >= c->cfg->buffer_size ||
c->cfg->data_type == MOST_CH_CONTROL ||
c->cfg->data_type == MOST_CH_ASYNC) {
kfifo_skip(&c->fifo);
mbo->buffer_length = c->mbo_offs;
c->mbo_offs = 0;
most_submit_mbo(mbo);
}
ret = to_copy - left;
unlock:
mutex_unlock(&c->io_mutex);
return ret;
}
/**
* comp_read - implements the syscall to read from the device
* @filp: file pointer
* @buf: pointer to user buffer
* @count: number of bytes to read
* @offset: offset from where to start reading
*/
static ssize_t
comp_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
size_t to_copy, not_copied, copied;
struct mbo *mbo = NULL;
struct comp_channel *c = filp->private_data;
mutex_lock(&c->io_mutex);
while (c->dev && !kfifo_peek(&c->fifo, &mbo)) {
mutex_unlock(&c->io_mutex);
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(c->wq,
(!kfifo_is_empty(&c->fifo) ||
(!c->dev))))
return -ERESTARTSYS;
mutex_lock(&c->io_mutex);
}
/* make sure we don't submit to gone devices */
if (unlikely(!c->dev)) {
mutex_unlock(&c->io_mutex);
return -ENODEV;
}
to_copy = min_t(size_t,
count,
mbo->processed_length - c->mbo_offs);
not_copied = copy_to_user(buf,
mbo->virt_address + c->mbo_offs,
to_copy);
copied = to_copy - not_copied;
c->mbo_offs += copied;
if (c->mbo_offs >= mbo->processed_length) {
kfifo_skip(&c->fifo);
most_put_mbo(mbo);
c->mbo_offs = 0;
}
mutex_unlock(&c->io_mutex);
return copied;
}
static __poll_t comp_poll(struct file *filp, poll_table *wait)
{
struct comp_channel *c = filp->private_data;
__poll_t mask = 0;
poll_wait(filp, &c->wq, wait);
mutex_lock(&c->io_mutex);
if (c->cfg->direction == MOST_CH_RX) {
if (!c->dev || !kfifo_is_empty(&c->fifo))
mask |= EPOLLIN | EPOLLRDNORM;
} else {
if (!c->dev || !kfifo_is_empty(&c->fifo) || ch_has_mbo(c))
mask |= EPOLLOUT | EPOLLWRNORM;
}
mutex_unlock(&c->io_mutex);
return mask;
}
/**
* Initialization of struct file_operations
*/
static const struct file_operations channel_fops = {
.owner = THIS_MODULE,
.read = comp_read,
.write = comp_write,
.open = comp_open,
.release = comp_close,
.poll = comp_poll,
};
/**
* comp_disconnect_channel - disconnect a channel
* @iface: pointer to interface instance
* @channel_id: channel index
*
* This frees allocated memory and removes the cdev that represents this
* channel in user space.
*/
static int comp_disconnect_channel(struct most_interface *iface, int channel_id)
{
struct comp_channel *c;
if (!iface) {
pr_info("Bad interface pointer\n");
return -EINVAL;
}
c = get_channel(iface, channel_id);
if (!c)
return -ENXIO;
mutex_lock(&c->io_mutex);
spin_lock(&c->unlink);
c->dev = NULL;
spin_unlock(&c->unlink);
destroy_cdev(c);
if (c->access_ref) {
stop_channel(c);
wake_up_interruptible(&c->wq);
mutex_unlock(&c->io_mutex);
} else {
mutex_unlock(&c->io_mutex);
destroy_channel(c);
}
return 0;
}
/**
* comp_rx_completion - completion handler for rx channels
* @mbo: pointer to buffer object that has completed
*
* This searches for the channel linked to this MBO and stores it in the local
* fifo buffer.
*/
static int comp_rx_completion(struct mbo *mbo)
{
struct comp_channel *c;
if (!mbo)
return -EINVAL;
c = get_channel(mbo->ifp, mbo->hdm_channel_id);
if (!c)
return -ENXIO;
spin_lock(&c->unlink);
if (!c->access_ref || !c->dev) {
spin_unlock(&c->unlink);
return -ENODEV;
}
kfifo_in(&c->fifo, &mbo, 1);
spin_unlock(&c->unlink);
#ifdef DEBUG_MESG
if (kfifo_is_full(&c->fifo))
pr_info("WARN: Fifo is full\n");
#endif
wake_up_interruptible(&c->wq);
return 0;
}
/**
* comp_tx_completion - completion handler for tx channels
* @iface: pointer to interface instance
* @channel_id: channel index/ID
*
* This wakes sleeping processes in the wait-queue.
*/
static int comp_tx_completion(struct most_interface *iface, int channel_id)
{
struct comp_channel *c;
if (!iface) {
pr_info("Bad interface pointer\n");
return -EINVAL;
}
if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
pr_info("Channel ID out of range\n");
return -EINVAL;
}
c = get_channel(iface, channel_id);
if (!c)
return -ENXIO;
wake_up_interruptible(&c->wq);
return 0;
}
/**
* comp_probe - probe function of the driver module
* @iface: pointer to interface instance
* @channel_id: channel index/ID
* @cfg: pointer to actual channel configuration
* @name: name of the device to be created
*
* This allocates achannel object and creates the device node in /dev
*
* Returns 0 on success or error code otherwise.
*/
static int comp_probe(struct most_interface *iface, int channel_id,
struct most_channel_config *cfg, char *name)
{
struct comp_channel *c;
unsigned long cl_flags;
int retval;
int current_minor;
if ((!iface) || (!cfg) || (!name)) {
pr_info("Probing component with bad arguments");
return -EINVAL;
}
c = get_channel(iface, channel_id);
if (c)
return -EEXIST;
current_minor = ida_simple_get(&comp.minor_id, 0, 0, GFP_KERNEL);
if (current_minor < 0)
return current_minor;
c = kzalloc(sizeof(*c), GFP_KERNEL);
if (!c) {
retval = -ENOMEM;
goto error_alloc_channel;
}
c->devno = MKDEV(comp.major, current_minor);
cdev_init(&c->cdev, &channel_fops);
c->cdev.owner = THIS_MODULE;
retval = cdev_add(&c->cdev, c->devno, 1);
if (retval < 0)
goto err_free_c;
c->iface = iface;
c->cfg = cfg;
c->channel_id = channel_id;
c->access_ref = 0;
spin_lock_init(&c->unlink);
INIT_KFIFO(c->fifo);
retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
if (retval) {
pr_info("failed to alloc channel kfifo");
goto error_alloc_kfifo;
}
init_waitqueue_head(&c->wq);
mutex_init(&c->io_mutex);
spin_lock_irqsave(&ch_list_lock, cl_flags);
list_add_tail(&c->list, &channel_list);
spin_unlock_irqrestore(&ch_list_lock, cl_flags);
c->dev = device_create(comp.class, NULL, c->devno, NULL, "%s", name);
if (IS_ERR(c->dev)) {
retval = PTR_ERR(c->dev);
pr_info("failed to create new device node %s\n", name);
goto error_create_device;
}
kobject_uevent(&c->dev->kobj, KOBJ_ADD);
return 0;
error_create_device:
kfifo_free(&c->fifo);
list_del(&c->list);
error_alloc_kfifo:
cdev_del(&c->cdev);
err_free_c:
kfree(c);
error_alloc_channel:
ida_simple_remove(&comp.minor_id, current_minor);
return retval;
}
static struct cdev_component comp = {
.cc = {
.name = "cdev",
.probe_channel = comp_probe,
.disconnect_channel = comp_disconnect_channel,
.rx_completion = comp_rx_completion,
.tx_completion = comp_tx_completion,
},
};
static int __init mod_init(void)
{
int err;
pr_info("init()\n");
comp.class = class_create(THIS_MODULE, "most_cdev");
if (IS_ERR(comp.class)) {
pr_info("No udev support.\n");
return PTR_ERR(comp.class);
}
INIT_LIST_HEAD(&channel_list);
spin_lock_init(&ch_list_lock);
ida_init(&comp.minor_id);
err = alloc_chrdev_region(&comp.devno, 0, CHRDEV_REGION_SIZE, "cdev");
if (err < 0)
goto dest_ida;
comp.major = MAJOR(comp.devno);
err = most_register_component(&comp.cc);
if (err)
goto free_cdev;
return 0;
free_cdev:
unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
dest_ida:
ida_destroy(&comp.minor_id);
class_destroy(comp.class);
return err;
}
static void __exit mod_exit(void)
{
struct comp_channel *c, *tmp;
pr_info("exit module\n");
most_deregister_component(&comp.cc);
list_for_each_entry_safe(c, tmp, &channel_list, list) {
destroy_cdev(c);
destroy_channel(c);
}
unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
ida_destroy(&comp.minor_id);
class_destroy(comp.class);
}
module_init(mod_init);
module_exit(mod_exit);
MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("character device component for mostcore");
File diff suppressed because it is too large. Load diff
+322
View File
@@ -0,0 +1,322 @@
// SPDX-License-Identifier: GPL-2.0
/*
* most.h - API for component and adapter drivers
*
* Copyright (C) 2013-2015, Microchip Technology Germany II GmbH & Co. KG
*/
#ifndef __MOST_CORE_H__
#define __MOST_CORE_H__
#include <linux/types.h>
#include <linux/device.h>
struct module;
struct interface_private;
/**
* Interface type
*/
enum most_interface_type {
ITYPE_LOOPBACK = 1,
ITYPE_I2C,
ITYPE_I2S,
ITYPE_TSI,
ITYPE_HBI,
ITYPE_MEDIALB_DIM,
ITYPE_MEDIALB_DIM2,
ITYPE_USB,
ITYPE_PCIE
};
/**
* Channel direction.
*/
enum most_channel_direction {
MOST_CH_RX = 1 << 0,
MOST_CH_TX = 1 << 1,
};
/**
* Channel data type.
*/
enum most_channel_data_type {
MOST_CH_CONTROL = 1 << 0,
MOST_CH_ASYNC = 1 << 1,
MOST_CH_ISOC = 1 << 2,
MOST_CH_SYNC = 1 << 5,
};
enum mbo_status_flags {
/* MBO was processed successfully (data was send or received )*/
MBO_SUCCESS = 0,
/* The MBO contains wrong or missing information. */
MBO_E_INVAL,
/* MBO was completed as HDM Channel will be closed */
MBO_E_CLOSE,
};
/**
* struct most_channel_capability - Channel capability
* @direction: Supported channel directions.
* The value is bitwise OR-combination of the values from the
* enumeration most_channel_direction. Zero is allowed value and means
* "channel may not be used".
* @data_type: Supported channel data types.
* The value is bitwise OR-combination of the values from the
* enumeration most_channel_data_type. Zero is allowed value and means
* "channel may not be used".
* @num_buffers_packet: Maximum number of buffers supported by this channel
* for packet data types (Async,Control,QoS)
* @buffer_size_packet: Maximum buffer size supported by this channel
* for packet data types (Async,Control,QoS)
* @num_buffers_streaming: Maximum number of buffers supported by this channel
* for streaming data types (Sync,AV Packetized)
* @buffer_size_streaming: Maximum buffer size supported by this channel
* for streaming data types (Sync,AV Packetized)
* @name_suffix: Optional suffix providean by an HDM that is attached to the
* regular channel name.
*
* Describes the capabilities of a MOST channel like supported Data Types
* and directions. This information is provided by an HDM for the MostCore.
*
* The Core creates read only sysfs attribute files in
* /sys/devices/most/mdev#/<channel>/ with the
* following attributes:
* -available_directions
* -available_datatypes
* -number_of_packet_buffers
* -number_of_stream_buffers
* -size_of_packet_buffer
* -size_of_stream_buffer
* where content of each file is a string with all supported properties of this
* very channel attribute.
*/
struct most_channel_capability {
u16 direction;
u16 data_type;
u16 num_buffers_packet;
u16 buffer_size_packet;
u16 num_buffers_streaming;
u16 buffer_size_streaming;
const char *name_suffix;
};
/**
* struct most_channel_config - stores channel configuration
* @direction: direction of the channel
* @data_type: data type travelling over this channel
* @num_buffers: number of buffers
* @buffer_size: size of a buffer for AIM.
* Buffer size may be cutted down by HDM in a configure callback
* to match to a given interface and channel type.
* @extra_len: additional buffer space for internal HDM purposes like padding.
* May be set by HDM in a configure callback if needed.
* @subbuffer_size: size of a subbuffer
* @packets_per_xact: number of MOST frames that are packet inside one USB
* packet. This is USB specific
*
* Describes the configuration for a MOST channel. This information is
* provided from the MostCore to a HDM (like the Medusa PCIe Interface) as a
* parameter of the "configure" function call.
*/
struct most_channel_config {
enum most_channel_direction direction;
enum most_channel_data_type data_type;
u16 num_buffers;
u16 buffer_size;
u16 extra_len;
u16 subbuffer_size;
u16 packets_per_xact;
u16 dbr_size;
};
/*
* struct mbo - MOST Buffer Object.
* @context: context for core completion handler
* @priv: private data for HDM
*
* public: documented fields that are used for the communications
* between MostCore and HDMs
*
* @list: list head for use by the mbo's current owner
* @ifp: (in) associated interface instance
* @num_buffers_ptr: amount of pool buffers
* @hdm_channel_id: (in) HDM channel instance
* @virt_address: (in) kernel virtual address of the buffer
* @bus_address: (in) bus address of the buffer
* @buffer_length: (in) buffer payload length
* @processed_length: (out) processed length
* @status: (out) transfer status
* @complete: (in) completion routine
*
* The core allocates and initializes the MBO.
*
* The HDM receives MBO for transfer from the core with the call to enqueue().
* The HDM copies the data to- or from the buffer depending on configured
* channel direction, set "processed_length" and "status" and completes
* the transfer procedure by calling the completion routine.
*
* Finally, the MBO is being deallocated or recycled for further
* transfers of the same or a different HDM.
*
* Directions of usage:
* The core driver should never access any MBO fields (even if marked
* as "public") while the MBO is owned by an HDM. The ownership starts with
* the call of enqueue() and ends with the call of its complete() routine.
*
* II.
* Every HDM attached to the core driver _must_ ensure that it returns any MBO
* it owns (due to a previous call to enqueue() by the core driver) before it
* de-registers an interface or gets unloaded from the kernel. If this direction
* is violated memory leaks will occur, since the core driver does _not_ track
* MBOs it is currently not in control of.
*
*/
struct mbo {
void *context;
void *priv;
struct list_head list;
struct most_interface *ifp;
int *num_buffers_ptr;
u16 hdm_channel_id;
void *virt_address;
dma_addr_t bus_address;
u16 buffer_length;
u16 processed_length;
enum mbo_status_flags status;
void (*complete)(struct mbo *mbo);
};
/**
* Interface instance description.
*
* Describes an interface of a MOST device the core driver is bound to.
* This structure is allocated and initialized in the HDM. MostCore may not
* modify this structure.
*
* @dev: the actual device
* @mod: module
* @interface Interface type. \sa most_interface_type.
* @description PRELIMINARY.
* Unique description of the device instance from point of view of the
* interface in free text form (ASCII).
* It may be a hexadecimal presentation of the memory address for the MediaLB
* IP or USB device ID with USB properties for USB interface, etc.
* @num_channels Number of channels and size of the channel_vector.
* @channel_vector Properties of the channels.
* Array index represents channel ID by the driver.
* @configure Callback to change data type for the channel of the
* interface instance. May be zero if the instance of the interface is not
* configurable. Parameter channel_config describes direction and data
* type for the channel, configured by the higher level. The content of
* @enqueue Delivers MBO to the HDM for processing.
* After HDM completes Rx- or Tx- operation the processed MBO shall
* be returned back to the MostCore using completion routine.
* The reason to get the MBO delivered from the MostCore after the channel
* is poisoned is the re-opening of the channel by the application.
* In this case the HDM shall hold MBOs and service the channel as usual.
* The HDM must be able to hold at least one MBO for each channel.
* The callback returns a negative value on error, otherwise 0.
* @poison_channel Informs HDM about closing the channel. The HDM shall
* cancel all transfers and synchronously or asynchronously return
* all enqueued for this channel MBOs using the completion routine.
* The callback returns a negative value on error, otherwise 0.
* @request_netinfo: triggers retrieving of network info from the HDM by
* means of "Message exchange over MDP/MEP"
* The call of the function request_netinfo with the parameter on_netinfo as
* NULL prohibits use of the previously obtained function pointer.
* @priv Private field used by mostcore to store context information.
*/
struct most_interface {
struct device dev;
struct device *driver_dev;
struct module *mod;
enum most_interface_type interface;
const char *description;
unsigned int num_channels;
struct most_channel_capability *channel_vector;
void *(*dma_alloc)(struct mbo *mbo, u32 size);
void (*dma_free)(struct mbo *mbo, u32 size);
int (*configure)(struct most_interface *iface, int channel_idx,
struct most_channel_config *channel_config);
int (*enqueue)(struct most_interface *iface, int channel_idx,
struct mbo *mbo);
int (*poison_channel)(struct most_interface *iface, int channel_idx);
void (*request_netinfo)(struct most_interface *iface, int channel_idx,
void (*on_netinfo)(struct most_interface *iface,
unsigned char link_stat,
unsigned char *mac_addr));
void *priv;
struct interface_private *p;
};
#define to_most_interface(d) container_of(d, struct most_interface, dev)
/**
* struct core_component - identifies a loadable component for the mostcore
* @list: list_head
* @name: component name
* @probe_channel: function for core to notify driver about channel connection
* @disconnect_channel: callback function to disconnect a certain channel
* @rx_completion: completion handler for received packets
* @tx_completion: completion handler for transmitted packets
*/
struct core_component {
struct list_head list;
const char *name;
int (*probe_channel)(struct most_interface *iface, int channel_idx,
struct most_channel_config *cfg, char *name);
int (*disconnect_channel)(struct most_interface *iface,
int channel_idx);
int (*rx_completion)(struct mbo *mbo);
int (*tx_completion)(struct most_interface *iface, int channel_idx);
};
/**
* most_register_interface - Registers instance of the interface.
* @iface: Pointer to the interface instance description.
*
* Returns a pointer to the kobject of the generated instance.
*
* Note: HDM has to ensure that any reference held on the kobj is
* released before deregistering the interface.
*/
int most_register_interface(struct most_interface *iface);
/**
* Deregisters instance of the interface.
* @intf_instance Pointer to the interface instance description.
*/
void most_deregister_interface(struct most_interface *iface);
void most_submit_mbo(struct mbo *mbo);
/**
* most_stop_enqueue - prevents core from enqueing MBOs
* @iface: pointer to interface
* @channel_idx: channel index
*/
void most_stop_enqueue(struct most_interface *iface, int channel_idx);
/**
* most_resume_enqueue - allow core to enqueue MBOs again
* @iface: pointer to interface
* @channel_idx: channel index
*
* This clears the enqueue halt flag and enqueues all MBOs currently
* in wait fifo.
*/
void most_resume_enqueue(struct most_interface *iface, int channel_idx);
int most_register_component(struct core_component *comp);
int most_deregister_component(struct core_component *comp);
struct mbo *most_get_mbo(struct most_interface *iface, int channel_idx,
struct core_component *comp);
void most_put_mbo(struct mbo *mbo);
int channel_has_mbo(struct most_interface *iface, int channel_idx,
struct core_component *comp);
int most_start_channel(struct most_interface *iface, int channel_idx,
struct core_component *comp);
int most_stop_channel(struct most_interface *iface, int channel_idx,
struct core_component *comp);
#endif /* MOST_CORE_H_ */
+16
View File
@@ -0,0 +1,16 @@
#
# MediaLB configuration
#
config MOST_DIM2
tristate "DIM2"
depends on HAS_IOMEM && OF
---help---
Say Y here if you want to connect via MediaLB to network transceiver.
This device driver is platform dependent and needs an additional
platform driver to be installed. For more information contact
maintainer of this driver.
To compile this driver as a module, choose M here: the
module will be called most_dim2.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_DIM2) += most_dim2.o
most_dim2-objs := dim2.o hal.o sysfs.o
ccflags-y += -Idrivers/staging/
File diff suppressed because it is too large. Load diff
+51
View File
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0
/*
* errors.h - Definitions of errors for DIM2 HAL API
* (MediaLB, Device Interface Macro IP, OS62420)
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
#ifndef _MOST_DIM_ERRORS_H
#define _MOST_DIM_ERRORS_H
/**
* MOST DIM errors.
*/
enum dim_errors_t {
/** Not an error */
DIM_NO_ERROR = 0,
/** Bad base address for DIM2 IP */
DIM_INIT_ERR_DIM_ADDR = 0x10,
/**< Bad MediaLB clock */
DIM_INIT_ERR_MLB_CLOCK,
/** Bad channel address */
DIM_INIT_ERR_CHANNEL_ADDRESS,
/** Out of DBR memory */
DIM_INIT_ERR_OUT_OF_MEMORY,
/** DIM API is called while DIM is not initialized successfully */
DIM_ERR_DRIVER_NOT_INITIALIZED = 0x20,
/**
* Configuration does not respect hardware limitations
* for isochronous or synchronous channels
*/
DIM_ERR_BAD_CONFIG,
/**
* Buffer size does not respect hardware limitations
* for isochronous or synchronous channels
*/
DIM_ERR_BAD_BUFFER_SIZE,
DIM_ERR_UNDERFLOW,
DIM_ERR_OVERFLOW,
};
#endif /* _MOST_DIM_ERRORS_H */
+982
View File
@@ -0,0 +1,982 @@
// SPDX-License-Identifier: GPL-2.0
/*
* hal.c - DIM2 HAL implementation
* (MediaLB, Device Interface Macro IP, OS62420)
*
* Copyright (C) 2015-2016, Microchip Technology Germany II GmbH & Co. KG
*/
/* Author: Andrey Shvetsov <andrey.shvetsov@k2l.de> */
#include "hal.h"
#include "errors.h"
#include "reg.h"
#include <linux/stddef.h>
#include <linux/kernel.h>
/*
* Size factor for isochronous DBR buffer.
* Minimal value is 3.
*/
#define ISOC_DBR_FACTOR 3u
/*
* Number of 32-bit units for DBR map.
*
* 1: block size is 512, max allocation is 16K
* 2: block size is 256, max allocation is 8K
* 4: block size is 128, max allocation is 4K
* 8: block size is 64, max allocation is 2K
*
* Min allocated space is block size.
* Max possible allocated space is 32 blocks.
*/
#define DBR_MAP_SIZE 2
/* -------------------------------------------------------------------------- */
/* not configurable area */
#define CDT 0x00
#define ADT 0x40
#define MLB_CAT 0x80
#define AHB_CAT 0x88
#define DBR_SIZE (16 * 1024) /* specified by IP */
#define DBR_BLOCK_SIZE (DBR_SIZE / 32 / DBR_MAP_SIZE)
#define ROUND_UP_TO(x, d) (DIV_ROUND_UP(x, (d)) * (d))
/* -------------------------------------------------------------------------- */
/* generic helper functions and macros */
static inline u32 bit_mask(u8 position)
{
return (u32)1 << position;
}
static inline bool dim_on_error(u8 error_id, const char *error_message)
{
dimcb_on_error(error_id, error_message);
return false;
}
/* -------------------------------------------------------------------------- */
/* types and local variables */
struct async_tx_dbr {
u8 ch_addr;
u16 rpc;
u16 wpc;
u16 rest_size;
u16 sz_queue[CDT0_RPC_MASK + 1];
};
struct lld_global_vars_t {
bool dim_is_initialized;
bool mcm_is_initialized;
struct dim2_regs __iomem *dim2; /* DIM2 core base address */
struct async_tx_dbr atx_dbr;
u32 fcnt;
u32 dbr_map[DBR_MAP_SIZE];
};
static struct lld_global_vars_t g = { false };
/* -------------------------------------------------------------------------- */
static int dbr_get_mask_size(u16 size)
{
int i;
for (i = 0; i < 6; i++)
if (size <= (DBR_BLOCK_SIZE << i))
return 1 << i;
return 0;
}
/**
* Allocates DBR memory.
* @param size Allocating memory size.
* @return Offset in DBR memory by success or DBR_SIZE if out of memory.
*/
static int alloc_dbr(u16 size)
{
int mask_size;
int i, block_idx = 0;
if (size <= 0)
return DBR_SIZE; /* out of memory */
mask_size = dbr_get_mask_size(size);
if (mask_size == 0)
return DBR_SIZE; /* out of memory */
for (i = 0; i < DBR_MAP_SIZE; i++) {
u32 const blocks = DIV_ROUND_UP(size, DBR_BLOCK_SIZE);
u32 mask = ~((~(u32)0) << blocks);
do {
if ((g.dbr_map[i] & mask) == 0) {
g.dbr_map[i] |= mask;
return block_idx * DBR_BLOCK_SIZE;
}
block_idx += mask_size;
/* do shift left with 2 steps in case mask_size == 32 */
mask <<= mask_size - 1;
} while ((mask <<= 1) != 0);
}
return DBR_SIZE; /* out of memory */
}
static void free_dbr(int offs, int size)
{
int block_idx = offs / DBR_BLOCK_SIZE;
u32 const blocks = DIV_ROUND_UP(size, DBR_BLOCK_SIZE);
u32 mask = ~((~(u32)0) << blocks);
mask <<= block_idx % 32;
g.dbr_map[block_idx / 32] &= ~mask;
}
/* -------------------------------------------------------------------------- */
static void dim2_transfer_madr(u32 val)
{
dimcb_io_write(&g.dim2->MADR, val);
/* wait for transfer completion */
while ((dimcb_io_read(&g.dim2->MCTL) & 1) != 1)
continue;
dimcb_io_write(&g.dim2->MCTL, 0); /* clear transfer complete */
}
static void dim2_clear_dbr(u16 addr, u16 size)
{
enum { MADR_TB_BIT = 30, MADR_WNR_BIT = 31 };
u16 const end_addr = addr + size;
u32 const cmd = bit_mask(MADR_WNR_BIT) | bit_mask(MADR_TB_BIT);
dimcb_io_write(&g.dim2->MCTL, 0); /* clear transfer complete */
dimcb_io_write(&g.dim2->MDAT0, 0);
for (; addr < end_addr; addr++)
dim2_transfer_madr(cmd | addr);
}
static u32 dim2_read_ctr(u32 ctr_addr, u16 mdat_idx)
{
dim2_transfer_madr(ctr_addr);
return dimcb_io_read((&g.dim2->MDAT0) + mdat_idx);
}
static void dim2_write_ctr_mask(u32 ctr_addr, const u32 *mask, const u32 *value)
{
enum { MADR_WNR_BIT = 31 };
dimcb_io_write(&g.dim2->MCTL, 0); /* clear transfer complete */
if (mask[0] != 0)
dimcb_io_write(&g.dim2->MDAT0, value[0]);
if (mask[1] != 0)
dimcb_io_write(&g.dim2->MDAT1, value[1]);
if (mask[2] != 0)
dimcb_io_write(&g.dim2->MDAT2, value[2]);
if (mask[3] != 0)
dimcb_io_write(&g.dim2->MDAT3, value[3]);
dimcb_io_write(&g.dim2->MDWE0, mask[0]);
dimcb_io_write(&g.dim2->MDWE1, mask[1]);
dimcb_io_write(&g.dim2->MDWE2, mask[2]);
dimcb_io_write(&g.dim2->MDWE3, mask[3]);
dim2_transfer_madr(bit_mask(MADR_WNR_BIT) | ctr_addr);
}
static inline void dim2_write_ctr(u32 ctr_addr, const u32 *value)
{
u32 const mask[4] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
dim2_write_ctr_mask(ctr_addr, mask, value);
}
static inline void dim2_clear_ctr(u32 ctr_addr)
{
u32 const value[4] = { 0, 0, 0, 0 };
dim2_write_ctr(ctr_addr, value);
}
static void dim2_configure_cat(u8 cat_base, u8 ch_addr, u8 ch_type,
bool read_not_write)
{
bool isoc_fce = ch_type == CAT_CT_VAL_ISOC;
bool sync_mfe = ch_type == CAT_CT_VAL_SYNC;
u16 const cat =
(read_not_write << CAT_RNW_BIT) |
(ch_type << CAT_CT_SHIFT) |
(ch_addr << CAT_CL_SHIFT) |
(isoc_fce << CAT_FCE_BIT) |
(sync_mfe << CAT_MFE_BIT) |
(false << CAT_MT_BIT) |
(true << CAT_CE_BIT);
u8 const ctr_addr = cat_base + ch_addr / 8;
u8 const idx = (ch_addr % 8) / 2;
u8 const shift = (ch_addr % 2) * 16;
u32 mask[4] = { 0, 0, 0, 0 };
u32 value[4] = { 0, 0, 0, 0 };
mask[idx] = (u32)0xFFFF << shift;
value[idx] = cat << shift;
dim2_write_ctr_mask(ctr_addr, mask, value);
}
static void dim2_clear_cat(u8 cat_base, u8 ch_addr)
{
u8 const ctr_addr = cat_base + ch_addr / 8;
u8 const idx = (ch_addr % 8) / 2;
u8 const shift = (ch_addr % 2) * 16;
u32 mask[4] = { 0, 0, 0, 0 };
u32 value[4] = { 0, 0, 0, 0 };
mask[idx] = (u32)0xFFFF << shift;
dim2_write_ctr_mask(ctr_addr, mask, value);
}
static void dim2_configure_cdt(u8 ch_addr, u16 dbr_address, u16 hw_buffer_size,
u16 packet_length)
{
u32 cdt[4] = { 0, 0, 0, 0 };
if (packet_length)
cdt[1] = ((packet_length - 1) << CDT1_BS_ISOC_SHIFT);
cdt[3] =
((hw_buffer_size - 1) << CDT3_BD_SHIFT) |
(dbr_address << CDT3_BA_SHIFT);
dim2_write_ctr(CDT + ch_addr, cdt);
}
static u16 dim2_rpc(u8 ch_addr)
{
u32 cdt0 = dim2_read_ctr(CDT + ch_addr, 0);
return (cdt0 >> CDT0_RPC_SHIFT) & CDT0_RPC_MASK;
}
static void dim2_clear_cdt(u8 ch_addr)
{
u32 cdt[4] = { 0, 0, 0, 0 };
dim2_write_ctr(CDT + ch_addr, cdt);
}
static void dim2_configure_adt(u8 ch_addr)
{
u32 adt[4] = { 0, 0, 0, 0 };
adt[0] =
(true << ADT0_CE_BIT) |
(true << ADT0_LE_BIT) |
(0 << ADT0_PG_BIT);
dim2_write_ctr(ADT + ch_addr, adt);
}
static void dim2_clear_adt(u8 ch_addr)
{
u32 adt[4] = { 0, 0, 0, 0 };
dim2_write_ctr(ADT + ch_addr, adt);
}
static void dim2_start_ctrl_async(u8 ch_addr, u8 idx, u32 buf_addr,
u16 buffer_size)
{
u8 const shift = idx * 16;
u32 mask[4] = { 0, 0, 0, 0 };
u32 adt[4] = { 0, 0, 0, 0 };
mask[1] =
bit_mask(ADT1_PS_BIT + shift) |
bit_mask(ADT1_RDY_BIT + shift) |
(ADT1_CTRL_ASYNC_BD_MASK << (ADT1_BD_SHIFT + shift));
adt[1] =
(true << (ADT1_PS_BIT + shift)) |
(true << (ADT1_RDY_BIT + shift)) |
((buffer_size - 1) << (ADT1_BD_SHIFT + shift));
mask[idx + 2] = 0xFFFFFFFF;
adt[idx + 2] = buf_addr;
dim2_write_ctr_mask(ADT + ch_addr, mask, adt);
}
static void dim2_start_isoc_sync(u8 ch_addr, u8 idx, u32 buf_addr,
u16 buffer_size)
{
u8 const shift = idx * 16;
u32 mask[4] = { 0, 0, 0, 0 };
u32 adt[4] = { 0, 0, 0, 0 };
mask[1] =
bit_mask(ADT1_RDY_BIT + shift) |
(ADT1_ISOC_SYNC_BD_MASK << (ADT1_BD_SHIFT + shift));
adt[1] =
(true << (ADT1_RDY_BIT + shift)) |
((buffer_size - 1) << (ADT1_BD_SHIFT + shift));
mask[idx + 2] = 0xFFFFFFFF;
adt[idx + 2] = buf_addr;
dim2_write_ctr_mask(ADT + ch_addr, mask, adt);
}
static void dim2_clear_ctram(void)
{
u32 ctr_addr;
for (ctr_addr = 0; ctr_addr < 0x90; ctr_addr++)
dim2_clear_ctr(ctr_addr);
}
static void dim2_configure_channel(
u8 ch_addr, u8 type, u8 is_tx, u16 dbr_address, u16 hw_buffer_size,
u16 packet_length)
{
dim2_configure_cdt(ch_addr, dbr_address, hw_buffer_size, packet_length);
dim2_configure_cat(MLB_CAT, ch_addr, type, is_tx ? 1 : 0);
dim2_configure_adt(ch_addr);
dim2_configure_cat(AHB_CAT, ch_addr, type, is_tx ? 0 : 1);
/* unmask interrupt for used channel, enable mlb_sys_int[0] interrupt */
dimcb_io_write(&g.dim2->ACMR0,
dimcb_io_read(&g.dim2->ACMR0) | bit_mask(ch_addr));
}
static void dim2_clear_channel(u8 ch_addr)
{
/* mask interrupt for used channel, disable mlb_sys_int[0] interrupt */
dimcb_io_write(&g.dim2->ACMR0,
dimcb_io_read(&g.dim2->ACMR0) & ~bit_mask(ch_addr));
dim2_clear_cat(AHB_CAT, ch_addr);
dim2_clear_adt(ch_addr);
dim2_clear_cat(MLB_CAT, ch_addr);
dim2_clear_cdt(ch_addr);
/* clear channel status bit */
dimcb_io_write(&g.dim2->ACSR0, bit_mask(ch_addr));
}
/* -------------------------------------------------------------------------- */
/* trace async tx dbr fill state */
static inline u16 norm_pc(u16 pc)
{
return pc & CDT0_RPC_MASK;
}
static void dbrcnt_init(u8 ch_addr, u16 dbr_size)
{
g.atx_dbr.rest_size = dbr_size;
g.atx_dbr.rpc = dim2_rpc(ch_addr);
g.atx_dbr.wpc = g.atx_dbr.rpc;
}
static void dbrcnt_enq(int buf_sz)
{
g.atx_dbr.rest_size -= buf_sz;
g.atx_dbr.sz_queue[norm_pc(g.atx_dbr.wpc)] = buf_sz;
g.atx_dbr.wpc++;
}
u16 dim_dbr_space(struct dim_channel *ch)
{
u16 cur_rpc;
struct async_tx_dbr *dbr = &g.atx_dbr;
if (ch->addr != dbr->ch_addr)
return 0xFFFF;
cur_rpc = dim2_rpc(ch->addr);
while (norm_pc(dbr->rpc) != cur_rpc) {
dbr->rest_size += dbr->sz_queue[norm_pc(dbr->rpc)];
dbr->rpc++;
}
if ((u16)(dbr->wpc - dbr->rpc) >= CDT0_RPC_MASK)
return 0;
return dbr->rest_size;
}
/* -------------------------------------------------------------------------- */
/* channel state helpers */
static void state_init(struct int_ch_state *state)
{
state->request_counter = 0;
state->service_counter = 0;
state->idx1 = 0;
state->idx2 = 0;
state->level = 0;
}
/* -------------------------------------------------------------------------- */
/* macro helper functions */
static inline bool check_channel_address(u32 ch_address)
{
return ch_address > 0 && (ch_address % 2) == 0 &&
(ch_address / 2) <= (u32)CAT_CL_MASK;
}
static inline bool check_packet_length(u32 packet_length)
{
u16 const max_size = ((u16)CDT3_BD_ISOC_MASK + 1u) / ISOC_DBR_FACTOR;
if (packet_length <= 0)
return false; /* too small */
if (packet_length > max_size)
return false; /* too big */
if (packet_length - 1u > (u32)CDT1_BS_ISOC_MASK)
return false; /* too big */
return true;
}
static inline bool check_bytes_per_frame(u32 bytes_per_frame)
{
u16 const bd_factor = g.fcnt + 2;
u16 const max_size = ((u16)CDT3_BD_MASK + 1u) >> bd_factor;
if (bytes_per_frame <= 0)
return false; /* too small */
if (bytes_per_frame > max_size)
return false; /* too big */
return true;
}
static inline u16 norm_ctrl_async_buffer_size(u16 buf_size)
{
u16 const max_size = (u16)ADT1_CTRL_ASYNC_BD_MASK + 1u;
if (buf_size > max_size)
return max_size;
return buf_size;
}
static inline u16 norm_isoc_buffer_size(u16 buf_size, u16 packet_length)
{
u16 n;
u16 const max_size = (u16)ADT1_ISOC_SYNC_BD_MASK + 1u;
if (buf_size > max_size)
buf_size = max_size;
n = buf_size / packet_length;
if (n < 2u)
return 0; /* too small buffer for given packet_length */
return packet_length * n;
}
static inline u16 norm_sync_buffer_size(u16 buf_size, u16 bytes_per_frame)
{
u16 n;
u16 const max_size = (u16)ADT1_ISOC_SYNC_BD_MASK + 1u;
u32 const unit = bytes_per_frame << g.fcnt;
if (buf_size > max_size)
buf_size = max_size;
n = buf_size / unit;
if (n < 1u)
return 0; /* too small buffer for given bytes_per_frame */
return unit * n;
}
static void dim2_cleanup(void)
{
/* disable MediaLB */
dimcb_io_write(&g.dim2->MLBC0, false << MLBC0_MLBEN_BIT);
dim2_clear_ctram();
/* disable mlb_int interrupt */
dimcb_io_write(&g.dim2->MIEN, 0);
/* clear status for all dma channels */
dimcb_io_write(&g.dim2->ACSR0, 0xFFFFFFFF);
dimcb_io_write(&g.dim2->ACSR1, 0xFFFFFFFF);
/* mask interrupts for all channels */
dimcb_io_write(&g.dim2->ACMR0, 0);
dimcb_io_write(&g.dim2->ACMR1, 0);
}
static void dim2_initialize(bool enable_6pin, u8 mlb_clock)
{
dim2_cleanup();
/* configure and enable MediaLB */
dimcb_io_write(&g.dim2->MLBC0,
enable_6pin << MLBC0_MLBPEN_BIT |
mlb_clock << MLBC0_MLBCLK_SHIFT |
g.fcnt << MLBC0_FCNT_SHIFT |
true << MLBC0_MLBEN_BIT);
/* activate all HBI channels */
dimcb_io_write(&g.dim2->HCMR0, 0xFFFFFFFF);
dimcb_io_write(&g.dim2->HCMR1, 0xFFFFFFFF);
/* enable HBI */
dimcb_io_write(&g.dim2->HCTL, bit_mask(HCTL_EN_BIT));
/* configure DMA */
dimcb_io_write(&g.dim2->ACTL,
ACTL_DMA_MODE_VAL_DMA_MODE_1 << ACTL_DMA_MODE_BIT |
true << ACTL_SCE_BIT);
}
static bool dim2_is_mlb_locked(void)
{
u32 const mask0 = bit_mask(MLBC0_MLBLK_BIT);
u32 const mask1 = bit_mask(MLBC1_CLKMERR_BIT) |
bit_mask(MLBC1_LOCKERR_BIT);
u32 const c1 = dimcb_io_read(&g.dim2->MLBC1);
u32 const nda_mask = (u32)MLBC1_NDA_MASK << MLBC1_NDA_SHIFT;
dimcb_io_write(&g.dim2->MLBC1, c1 & nda_mask);
return (dimcb_io_read(&g.dim2->MLBC1) & mask1) == 0 &&
(dimcb_io_read(&g.dim2->MLBC0) & mask0) != 0;
}
/* -------------------------------------------------------------------------- */
/* channel help routines */
static inline bool service_channel(u8 ch_addr, u8 idx)
{
u8 const shift = idx * 16;
u32 const adt1 = dim2_read_ctr(ADT + ch_addr, 1);
u32 mask[4] = { 0, 0, 0, 0 };
u32 adt_w[4] = { 0, 0, 0, 0 };
if (((adt1 >> (ADT1_DNE_BIT + shift)) & 1) == 0)
return false;
mask[1] =
bit_mask(ADT1_DNE_BIT + shift) |
bit_mask(ADT1_ERR_BIT + shift) |
bit_mask(ADT1_RDY_BIT + shift);
dim2_write_ctr_mask(ADT + ch_addr, mask, adt_w);
/* clear channel status bit */
dimcb_io_write(&g.dim2->ACSR0, bit_mask(ch_addr));
return true;
}
/* -------------------------------------------------------------------------- */
/* channel init routines */
static void isoc_init(struct dim_channel *ch, u8 ch_addr, u16 packet_length)
{
state_init(&ch->state);
ch->addr = ch_addr;
ch->packet_length = packet_length;
ch->bytes_per_frame = 0;
ch->done_sw_buffers_number = 0;
}
static void sync_init(struct dim_channel *ch, u8 ch_addr, u16 bytes_per_frame)
{
state_init(&ch->state);
ch->addr = ch_addr;
ch->packet_length = 0;
ch->bytes_per_frame = bytes_per_frame;
ch->done_sw_buffers_number = 0;
}
static void channel_init(struct dim_channel *ch, u8 ch_addr)
{
state_init(&ch->state);
ch->addr = ch_addr;
ch->packet_length = 0;
ch->bytes_per_frame = 0;
ch->done_sw_buffers_number = 0;
}
/* returns true if channel interrupt state is cleared */
static bool channel_service_interrupt(struct dim_channel *ch)
{
struct int_ch_state *const state = &ch->state;
if (!service_channel(ch->addr, state->idx2))
return false;
state->idx2 ^= 1;
state->request_counter++;
return true;
}
static bool channel_start(struct dim_channel *ch, u32 buf_addr, u16 buf_size)
{
struct int_ch_state *const state = &ch->state;
if (buf_size <= 0)
return dim_on_error(DIM_ERR_BAD_BUFFER_SIZE, "Bad buffer size");
if (ch->packet_length == 0 && ch->bytes_per_frame == 0 &&
buf_size != norm_ctrl_async_buffer_size(buf_size))
return dim_on_error(DIM_ERR_BAD_BUFFER_SIZE,
"Bad control/async buffer size");
if (ch->packet_length &&
buf_size != norm_isoc_buffer_size(buf_size, ch->packet_length))
return dim_on_error(DIM_ERR_BAD_BUFFER_SIZE,
"Bad isochronous buffer size");
if (ch->bytes_per_frame &&
buf_size != norm_sync_buffer_size(buf_size, ch->bytes_per_frame))
return dim_on_error(DIM_ERR_BAD_BUFFER_SIZE,
"Bad synchronous buffer size");
if (state->level >= 2u)
return dim_on_error(DIM_ERR_OVERFLOW, "Channel overflow");
++state->level;
if (ch->addr == g.atx_dbr.ch_addr)
dbrcnt_enq(buf_size);
if (ch->packet_length || ch->bytes_per_frame)
dim2_start_isoc_sync(ch->addr, state->idx1, buf_addr, buf_size);
else
dim2_start_ctrl_async(ch->addr, state->idx1, buf_addr,
buf_size);
state->idx1 ^= 1;
return true;
}
static u8 channel_service(struct dim_channel *ch)
{
struct int_ch_state *const state = &ch->state;
if (state->service_counter != state->request_counter) {
state->service_counter++;
if (state->level == 0)
return DIM_ERR_UNDERFLOW;
--state->level;
ch->done_sw_buffers_number++;
}
return DIM_NO_ERROR;
}
static bool channel_detach_buffers(struct dim_channel *ch, u16 buffers_number)
{
if (buffers_number > ch->done_sw_buffers_number)
return dim_on_error(DIM_ERR_UNDERFLOW, "Channel underflow");
ch->done_sw_buffers_number -= buffers_number;
return true;
}
/* -------------------------------------------------------------------------- */
/* API */
u8 dim_startup(struct dim2_regs __iomem *dim_base_address, u32 mlb_clock,
u32 fcnt)
{
g.dim_is_initialized = false;
if (!dim_base_address)
return DIM_INIT_ERR_DIM_ADDR;
/* MediaLB clock: 0 - 256 fs, 1 - 512 fs, 2 - 1024 fs, 3 - 2048 fs */
/* MediaLB clock: 4 - 3072 fs, 5 - 4096 fs, 6 - 6144 fs, 7 - 8192 fs */
if (mlb_clock >= 8)
return DIM_INIT_ERR_MLB_CLOCK;
if (fcnt > MLBC0_FCNT_MAX_VAL)
return DIM_INIT_ERR_MLB_CLOCK;
g.dim2 = dim_base_address;
g.fcnt = fcnt;
g.dbr_map[0] = 0;
g.dbr_map[1] = 0;
dim2_initialize(mlb_clock >= 3, mlb_clock);
g.dim_is_initialized = true;
return DIM_NO_ERROR;
}
void dim_shutdown(void)
{
g.dim_is_initialized = false;
dim2_cleanup();
}
bool dim_get_lock_state(void)
{
return dim2_is_mlb_locked();
}
static u8 init_ctrl_async(struct dim_channel *ch, u8 type, u8 is_tx,
u16 ch_address, u16 hw_buffer_size)
{
if (!g.dim_is_initialized || !ch)
return DIM_ERR_DRIVER_NOT_INITIALIZED;
if (!check_channel_address(ch_address))
return DIM_INIT_ERR_CHANNEL_ADDRESS;
if (!ch->dbr_size)
ch->dbr_size = ROUND_UP_TO(hw_buffer_size, DBR_BLOCK_SIZE);
ch->dbr_addr = alloc_dbr(ch->dbr_size);
if (ch->dbr_addr >= DBR_SIZE)
return DIM_INIT_ERR_OUT_OF_MEMORY;
channel_init(ch, ch_address / 2);
dim2_configure_channel(ch->addr, type, is_tx,
ch->dbr_addr, ch->dbr_size, 0);
return DIM_NO_ERROR;
}
void dim_service_mlb_int_irq(void)
{
dimcb_io_write(&g.dim2->MS0, 0);
dimcb_io_write(&g.dim2->MS1, 0);
}
u16 dim_norm_ctrl_async_buffer_size(u16 buf_size)
{
return norm_ctrl_async_buffer_size(buf_size);
}
/**
* Retrieves maximal possible correct buffer size for isochronous data type
* conform to given packet length and not bigger than given buffer size.
*
* Returns non-zero correct buffer size or zero by error.
*/
u16 dim_norm_isoc_buffer_size(u16 buf_size, u16 packet_length)
{
if (!check_packet_length(packet_length))
return 0;
return norm_isoc_buffer_size(buf_size, packet_length);
}
/**
* Retrieves maximal possible correct buffer size for synchronous data type
* conform to given bytes per frame and not bigger than given buffer size.
*
* Returns non-zero correct buffer size or zero by error.
*/
u16 dim_norm_sync_buffer_size(u16 buf_size, u16 bytes_per_frame)
{
if (!check_bytes_per_frame(bytes_per_frame))
return 0;
return norm_sync_buffer_size(buf_size, bytes_per_frame);
}
u8 dim_init_control(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 max_buffer_size)
{
return init_ctrl_async(ch, CAT_CT_VAL_CONTROL, is_tx, ch_address,
max_buffer_size);
}
u8 dim_init_async(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 max_buffer_size)
{
u8 ret = init_ctrl_async(ch, CAT_CT_VAL_ASYNC, is_tx, ch_address,
max_buffer_size);
if (is_tx && !g.atx_dbr.ch_addr) {
g.atx_dbr.ch_addr = ch->addr;
dbrcnt_init(ch->addr, ch->dbr_size);
dimcb_io_write(&g.dim2->MIEN, bit_mask(20));
}
return ret;
}
u8 dim_init_isoc(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 packet_length)
{
if (!g.dim_is_initialized || !ch)
return DIM_ERR_DRIVER_NOT_INITIALIZED;
if (!check_channel_address(ch_address))
return DIM_INIT_ERR_CHANNEL_ADDRESS;
if (!check_packet_length(packet_length))
return DIM_ERR_BAD_CONFIG;
if (!ch->dbr_size)
ch->dbr_size = packet_length * ISOC_DBR_FACTOR;
ch->dbr_addr = alloc_dbr(ch->dbr_size);
if (ch->dbr_addr >= DBR_SIZE)
return DIM_INIT_ERR_OUT_OF_MEMORY;
isoc_init(ch, ch_address / 2, packet_length);
dim2_configure_channel(ch->addr, CAT_CT_VAL_ISOC, is_tx, ch->dbr_addr,
ch->dbr_size, packet_length);
return DIM_NO_ERROR;
}
u8 dim_init_sync(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 bytes_per_frame)
{
u16 bd_factor = g.fcnt + 2;
if (!g.dim_is_initialized || !ch)
return DIM_ERR_DRIVER_NOT_INITIALIZED;
if (!check_channel_address(ch_address))
return DIM_INIT_ERR_CHANNEL_ADDRESS;
if (!check_bytes_per_frame(bytes_per_frame))
return DIM_ERR_BAD_CONFIG;
if (!ch->dbr_size)
ch->dbr_size = bytes_per_frame << bd_factor;
ch->dbr_addr = alloc_dbr(ch->dbr_size);
if (ch->dbr_addr >= DBR_SIZE)
return DIM_INIT_ERR_OUT_OF_MEMORY;
sync_init(ch, ch_address / 2, bytes_per_frame);
dim2_clear_dbr(ch->dbr_addr, ch->dbr_size);
dim2_configure_channel(ch->addr, CAT_CT_VAL_SYNC, is_tx,
ch->dbr_addr, ch->dbr_size, 0);
return DIM_NO_ERROR;
}
u8 dim_destroy_channel(struct dim_channel *ch)
{
if (!g.dim_is_initialized || !ch)
return DIM_ERR_DRIVER_NOT_INITIALIZED;
if (ch->addr == g.atx_dbr.ch_addr) {
dimcb_io_write(&g.dim2->MIEN, 0);
g.atx_dbr.ch_addr = 0;
}
dim2_clear_channel(ch->addr);
if (ch->dbr_addr < DBR_SIZE)
free_dbr(ch->dbr_addr, ch->dbr_size);
ch->dbr_addr = DBR_SIZE;
return DIM_NO_ERROR;
}
void dim_service_ahb_int_irq(struct dim_channel *const *channels)
{
bool state_changed;
if (!g.dim_is_initialized) {
dim_on_error(DIM_ERR_DRIVER_NOT_INITIALIZED,
"DIM is not initialized");
return;
}
if (!channels) {
dim_on_error(DIM_ERR_DRIVER_NOT_INITIALIZED, "Bad channels");
return;
}
/*
* Use while-loop and a flag to make sure the age is changed back at
* least once, otherwise the interrupt may never come if CPU generates
* interrupt on changing age.
* This cycle runs not more than number of channels, because
* channel_service_interrupt() routine doesn't start the channel again.
*/
do {
struct dim_channel *const *ch = channels;
state_changed = false;
while (*ch) {
state_changed |= channel_service_interrupt(*ch);
++ch;
}
} while (state_changed);
}
u8 dim_service_channel(struct dim_channel *ch)
{
if (!g.dim_is_initialized || !ch)
return DIM_ERR_DRIVER_NOT_INITIALIZED;
return channel_service(ch);
}
struct dim_ch_state_t *dim_get_channel_state(struct dim_channel *ch,
struct dim_ch_state_t *state_ptr)
{
if (!ch || !state_ptr)
return NULL;
state_ptr->ready = ch->state.level < 2;
state_ptr->done_buffers = ch->done_sw_buffers_number;
return state_ptr;
}
bool dim_enqueue_buffer(struct dim_channel *ch, u32 buffer_addr,
u16 buffer_size)
{
if (!ch)
return dim_on_error(DIM_ERR_DRIVER_NOT_INITIALIZED,
"Bad channel");
return channel_start(ch, buffer_addr, buffer_size);
}
bool dim_detach_buffers(struct dim_channel *ch, u16 buffers_number)
{
if (!ch)
return dim_on_error(DIM_ERR_DRIVER_NOT_INITIALIZED,
"Bad channel");
return channel_detach_buffers(ch, buffers_number);
}
+106
View File
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: GPL-2.0
/*
* hal.h - DIM2 HAL interface
* (MediaLB, Device Interface Macro IP, OS62420)
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
#ifndef _DIM2_HAL_H
#define _DIM2_HAL_H
#include <linux/types.h>
#include "reg.h"
/*
* The values below are specified in the hardware specification.
* So, they should not be changed until the hardware specification changes.
*/
enum mlb_clk_speed {
CLK_256FS = 0,
CLK_512FS = 1,
CLK_1024FS = 2,
CLK_2048FS = 3,
CLK_3072FS = 4,
CLK_4096FS = 5,
CLK_6144FS = 6,
CLK_8192FS = 7,
};
struct dim_ch_state_t {
bool ready; /* Shows readiness to enqueue next buffer */
u16 done_buffers; /* Number of completed buffers */
};
struct int_ch_state {
/* changed only in interrupt context */
volatile int request_counter;
/* changed only in task context */
volatile int service_counter;
u8 idx1;
u8 idx2;
u8 level; /* [0..2], buffering level */
};
struct dim_channel {
struct int_ch_state state;
u8 addr;
u16 dbr_addr;
u16 dbr_size;
u16 packet_length; /*< Isochronous packet length in bytes. */
u16 bytes_per_frame; /*< Synchronous bytes per frame. */
u16 done_sw_buffers_number; /*< Done software buffers number. */
};
u8 dim_startup(struct dim2_regs __iomem *dim_base_address, u32 mlb_clock,
u32 fcnt);
void dim_shutdown(void);
bool dim_get_lock_state(void);
u16 dim_norm_ctrl_async_buffer_size(u16 buf_size);
u16 dim_norm_isoc_buffer_size(u16 buf_size, u16 packet_length);
u16 dim_norm_sync_buffer_size(u16 buf_size, u16 bytes_per_frame);
u8 dim_init_control(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 max_buffer_size);
u8 dim_init_async(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 max_buffer_size);
u8 dim_init_isoc(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 packet_length);
u8 dim_init_sync(struct dim_channel *ch, u8 is_tx, u16 ch_address,
u16 bytes_per_frame);
u8 dim_destroy_channel(struct dim_channel *ch);
void dim_service_mlb_int_irq(void);
void dim_service_ahb_int_irq(struct dim_channel *const *channels);
u8 dim_service_channel(struct dim_channel *ch);
struct dim_ch_state_t *dim_get_channel_state(struct dim_channel *ch,
struct dim_ch_state_t *state_ptr);
u16 dim_dbr_space(struct dim_channel *ch);
bool dim_enqueue_buffer(struct dim_channel *ch, u32 buffer_addr,
u16 buffer_size);
bool dim_detach_buffers(struct dim_channel *ch, u16 buffers_number);
u32 dimcb_io_read(u32 __iomem *ptr32);
void dimcb_io_write(u32 __iomem *ptr32, u32 value);
void dimcb_on_error(u8 error_id, const char *error_message);
#endif /* _DIM2_HAL_H */
+157
View File
@@ -0,0 +1,157 @@
// SPDX-License-Identifier: GPL-2.0
/*
* reg.h - Definitions for registers of DIM2
* (MediaLB, Device Interface Macro IP, OS62420)
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
#ifndef DIM2_OS62420_H
#define DIM2_OS62420_H
#include <linux/types.h>
struct dim2_regs {
u32 MLBC0; /* 0x00 */
u32 rsvd0[1]; /* 0x01 */
u32 MLBPC0; /* 0x02 */
u32 MS0; /* 0x03 */
u32 rsvd1[1]; /* 0x04 */
u32 MS1; /* 0x05 */
u32 rsvd2[2]; /* 0x06 */
u32 MSS; /* 0x08 */
u32 MSD; /* 0x09 */
u32 rsvd3[1]; /* 0x0A */
u32 MIEN; /* 0x0B */
u32 rsvd4[1]; /* 0x0C */
u32 MLBPC2; /* 0x0D */
u32 MLBPC1; /* 0x0E */
u32 MLBC1; /* 0x0F */
u32 rsvd5[0x10]; /* 0x10 */
u32 HCTL; /* 0x20 */
u32 rsvd6[1]; /* 0x21 */
u32 HCMR0; /* 0x22 */
u32 HCMR1; /* 0x23 */
u32 HCER0; /* 0x24 */
u32 HCER1; /* 0x25 */
u32 HCBR0; /* 0x26 */
u32 HCBR1; /* 0x27 */
u32 rsvd7[8]; /* 0x28 */
u32 MDAT0; /* 0x30 */
u32 MDAT1; /* 0x31 */
u32 MDAT2; /* 0x32 */
u32 MDAT3; /* 0x33 */
u32 MDWE0; /* 0x34 */
u32 MDWE1; /* 0x35 */
u32 MDWE2; /* 0x36 */
u32 MDWE3; /* 0x37 */
u32 MCTL; /* 0x38 */
u32 MADR; /* 0x39 */
u32 rsvd8[0xb6]; /* 0x3A */
u32 ACTL; /* 0xF0 */
u32 rsvd9[3]; /* 0xF1 */
u32 ACSR0; /* 0xF4 */
u32 ACSR1; /* 0xF5 */
u32 ACMR0; /* 0xF6 */
u32 ACMR1; /* 0xF7 */
};
#define DIM2_MASK(n) (~((~(u32)0) << (n)))
enum {
MLBC0_MLBLK_BIT = 7,
MLBC0_MLBPEN_BIT = 5,
MLBC0_MLBCLK_SHIFT = 2,
MLBC0_MLBCLK_VAL_256FS = 0,
MLBC0_MLBCLK_VAL_512FS = 1,
MLBC0_MLBCLK_VAL_1024FS = 2,
MLBC0_MLBCLK_VAL_2048FS = 3,
MLBC0_FCNT_SHIFT = 15,
MLBC0_FCNT_MASK = 7,
MLBC0_FCNT_MAX_VAL = 6,
MLBC0_MLBEN_BIT = 0,
MIEN_CTX_BREAK_BIT = 29,
MIEN_CTX_PE_BIT = 28,
MIEN_CTX_DONE_BIT = 27,
MIEN_CRX_BREAK_BIT = 26,
MIEN_CRX_PE_BIT = 25,
MIEN_CRX_DONE_BIT = 24,
MIEN_ATX_BREAK_BIT = 22,
MIEN_ATX_PE_BIT = 21,
MIEN_ATX_DONE_BIT = 20,
MIEN_ARX_BREAK_BIT = 19,
MIEN_ARX_PE_BIT = 18,
MIEN_ARX_DONE_BIT = 17,
MIEN_SYNC_PE_BIT = 16,
MIEN_ISOC_BUFO_BIT = 1,
MIEN_ISOC_PE_BIT = 0,
MLBC1_NDA_SHIFT = 8,
MLBC1_NDA_MASK = 0xFF,
MLBC1_CLKMERR_BIT = 7,
MLBC1_LOCKERR_BIT = 6,
ACTL_DMA_MODE_BIT = 2,
ACTL_DMA_MODE_VAL_DMA_MODE_0 = 0,
ACTL_DMA_MODE_VAL_DMA_MODE_1 = 1,
ACTL_SCE_BIT = 0,
HCTL_EN_BIT = 15
};
enum {
CDT0_RPC_SHIFT = 16 + 11,
CDT0_RPC_MASK = DIM2_MASK(5),
CDT1_BS_ISOC_SHIFT = 0,
CDT1_BS_ISOC_MASK = DIM2_MASK(9),
CDT3_BD_SHIFT = 0,
CDT3_BD_MASK = DIM2_MASK(12),
CDT3_BD_ISOC_MASK = DIM2_MASK(13),
CDT3_BA_SHIFT = 16,
ADT0_CE_BIT = 15,
ADT0_LE_BIT = 14,
ADT0_PG_BIT = 13,
ADT1_RDY_BIT = 15,
ADT1_DNE_BIT = 14,
ADT1_ERR_BIT = 13,
ADT1_PS_BIT = 12,
ADT1_MEP_BIT = 11,
ADT1_BD_SHIFT = 0,
ADT1_CTRL_ASYNC_BD_MASK = DIM2_MASK(11),
ADT1_ISOC_SYNC_BD_MASK = DIM2_MASK(13),
CAT_FCE_BIT = 14,
CAT_MFE_BIT = 14,
CAT_MT_BIT = 13,
CAT_RNW_BIT = 12,
CAT_CE_BIT = 11,
CAT_CT_SHIFT = 8,
CAT_CT_VAL_SYNC = 0,
CAT_CT_VAL_CONTROL = 1,
CAT_CT_VAL_ASYNC = 2,
CAT_CT_VAL_ISOC = 3,
CAT_CL_SHIFT = 0,
CAT_CL_MASK = DIM2_MASK(6)
};
#endif /* DIM2_OS62420_H */
+49
View File
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-2.0
/*
* sysfs.c - MediaLB sysfs information
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
/* Author: Andrey Shvetsov <andrey.shvetsov@k2l.de> */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#include "sysfs.h"
#include <linux/device.h>
static ssize_t state_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
bool state = dim2_sysfs_get_state_cb();
return sprintf(buf, "%s\n", state ? "locked" : "");
}
static DEVICE_ATTR_RO(state);
static struct attribute *dev_attrs[] = {
&dev_attr_state.attr,
NULL,
};
static struct attribute_group dev_attr_group = {
.attrs = dev_attrs,
};
static const struct attribute_group *dev_attr_groups[] = {
&dev_attr_group,
NULL,
};
int dim2_sysfs_probe(struct device *dev)
{
dev->groups = dev_attr_groups;
return device_register(dev);
}
void dim2_sysfs_destroy(struct device *dev)
{
device_unregister(dev);
}
+30
View File
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: GPL-2.0
/*
* sysfs.h - MediaLB sysfs information
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
/* Author: Andrey Shvetsov <andrey.shvetsov@k2l.de> */
#ifndef DIM2_SYSFS_H
#define DIM2_SYSFS_H
#include <linux/kobject.h>
struct medialb_bus {
struct kobject kobj_group;
};
struct device;
int dim2_sysfs_probe(struct device *dev);
void dim2_sysfs_destroy(struct device *dev);
/*
* callback,
* must deliver MediaLB state as true if locked or false if unlocked
*/
bool dim2_sysfs_get_state_cb(void);
#endif /* DIM2_SYSFS_H */
+12
View File
@@ -0,0 +1,12 @@
#
# MOST I2C configuration
#
config MOST_I2C
tristate "I2C"
depends on I2C
---help---
Say Y here if you want to connect via I2C to network tranceiver.
To compile this driver as a module, choose M here: the
module will be called most_i2c.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_I2C) += most_i2c.o
most_i2c-objs := i2c.o
ccflags-y += -Idrivers/staging/
+374
View File
@@ -0,0 +1,374 @@
// SPDX-License-Identifier: GPL-2.0
/*
* i2c.c - Hardware Dependent Module for I2C Interface
*
* Copyright (C) 2013-2015, Microchip Technology Germany II GmbH & Co. KG
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include "most/core.h"
enum { CH_RX, CH_TX, NUM_CHANNELS };
#define MAX_BUFFERS_CONTROL 32
#define MAX_BUF_SIZE_CONTROL 256
/**
* list_first_mbo - get the first mbo from a list
* @ptr: the list head to take the mbo from.
*/
#define list_first_mbo(ptr) \
list_first_entry(ptr, struct mbo, list)
static unsigned int polling_rate;
module_param(polling_rate, uint, 0644);
MODULE_PARM_DESC(polling_rate, "Polling rate [Hz]. Default = 0 (use IRQ)");
struct hdm_i2c {
struct most_interface most_iface;
struct most_channel_capability capabilities[NUM_CHANNELS];
struct i2c_client *client;
struct rx {
struct delayed_work dwork;
struct list_head list;
bool int_disabled;
unsigned int delay;
} rx;
char name[64];
};
#define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
static irqreturn_t most_irq_handler(int, void *);
static void pending_rx_work(struct work_struct *);
/**
* configure_channel - called from MOST core to configure a channel
* @iface: interface the channel belongs to
* @channel: channel to be configured
* @channel_config: structure that holds the configuration information
*
* Return 0 on success, negative on failure.
*
* Receives configuration information from MOST core and initialize the
* corresponding channel.
*/
static int configure_channel(struct most_interface *most_iface,
int ch_idx,
struct most_channel_config *channel_config)
{
int ret;
struct hdm_i2c *dev = to_hdm(most_iface);
unsigned int delay, pr;
BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
if (channel_config->data_type != MOST_CH_CONTROL) {
pr_err("bad data type for channel %d\n", ch_idx);
return -EPERM;
}
if (channel_config->direction != dev->capabilities[ch_idx].direction) {
pr_err("bad direction for channel %d\n", ch_idx);
return -EPERM;
}
if (channel_config->direction == MOST_CH_RX) {
if (!polling_rate) {
if (dev->client->irq <= 0) {
pr_err("bad irq: %d\n", dev->client->irq);
return -ENOENT;
}
dev->rx.int_disabled = false;
ret = request_irq(dev->client->irq, most_irq_handler, 0,
dev->client->name, dev);
if (ret) {
pr_err("request_irq(%d) failed: %d\n",
dev->client->irq, ret);
return ret;
}
} else {
delay = msecs_to_jiffies(MSEC_PER_SEC / polling_rate);
dev->rx.delay = delay ? delay : 1;
pr = MSEC_PER_SEC / jiffies_to_msecs(dev->rx.delay);
pr_info("polling rate is %u Hz\n", pr);
}
}
return 0;
}
/**
* enqueue - called from MOST core to enqueue a buffer for data transfer
* @iface: intended interface
* @channel: ID of the channel the buffer is intended for
* @mbo: pointer to the buffer object
*
* Return 0 on success, negative on failure.
*
* Transmit the data over I2C if it is a "write" request or push the buffer into
* list if it is an "read" request
*/
static int enqueue(struct most_interface *most_iface,
int ch_idx, struct mbo *mbo)
{
struct hdm_i2c *dev = to_hdm(most_iface);
int ret;
BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
if (ch_idx == CH_RX) {
/* RX */
if (!polling_rate)
disable_irq(dev->client->irq);
cancel_delayed_work_sync(&dev->rx.dwork);
list_add_tail(&mbo->list, &dev->rx.list);
if (dev->rx.int_disabled || polling_rate)
pending_rx_work(&dev->rx.dwork.work);
if (!polling_rate)
enable_irq(dev->client->irq);
} else {
/* TX */
ret = i2c_master_send(dev->client, mbo->virt_address,
mbo->buffer_length);
if (ret <= 0) {
mbo->processed_length = 0;
mbo->status = MBO_E_INVAL;
} else {
mbo->processed_length = mbo->buffer_length;
mbo->status = MBO_SUCCESS;
}
mbo->complete(mbo);
}
return 0;
}
/**
* poison_channel - called from MOST core to poison buffers of a channel
* @iface: pointer to the interface the channel to be poisoned belongs to
* @channel_id: corresponding channel ID
*
* Return 0 on success, negative on failure.
*
* If channel direction is RX, complete the buffers in list with
* status MBO_E_CLOSE
*/
static int poison_channel(struct most_interface *most_iface,
int ch_idx)
{
struct hdm_i2c *dev = to_hdm(most_iface);
struct mbo *mbo;
BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
if (ch_idx == CH_RX) {
if (!polling_rate)
free_irq(dev->client->irq, dev);
cancel_delayed_work_sync(&dev->rx.dwork);
while (!list_empty(&dev->rx.list)) {
mbo = list_first_mbo(&dev->rx.list);
list_del(&mbo->list);
mbo->processed_length = 0;
mbo->status = MBO_E_CLOSE;
mbo->complete(mbo);
}
}
return 0;
}
static void do_rx_work(struct hdm_i2c *dev)
{
struct mbo *mbo;
unsigned char msg[MAX_BUF_SIZE_CONTROL];
int ret;
u16 pml, data_size;
/* Read PML (2 bytes) */
ret = i2c_master_recv(dev->client, msg, 2);
if (ret <= 0) {
pr_err("Failed to receive PML\n");
return;
}
pml = (msg[0] << 8) | msg[1];
if (!pml)
return;
data_size = pml + 2;
/* Read the whole message, including PML */
ret = i2c_master_recv(dev->client, msg, data_size);
if (ret <= 0) {
pr_err("Failed to receive a Port Message\n");
return;
}
mbo = list_first_mbo(&dev->rx.list);
list_del(&mbo->list);
mbo->processed_length = min(data_size, mbo->buffer_length);
memcpy(mbo->virt_address, msg, mbo->processed_length);
mbo->status = MBO_SUCCESS;
mbo->complete(mbo);
}
/**
* pending_rx_work - Read pending messages through I2C
* @work: definition of this work item
*
* Invoked by the Interrupt Service Routine, most_irq_handler()
*/
static void pending_rx_work(struct work_struct *work)
{
struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
if (list_empty(&dev->rx.list))
return;
do_rx_work(dev);
if (polling_rate) {
schedule_delayed_work(&dev->rx.dwork, dev->rx.delay);
} else {
dev->rx.int_disabled = false;
enable_irq(dev->client->irq);
}
}
/*
* most_irq_handler - Interrupt Service Routine
* @irq: irq number
* @_dev: private data
*
* Schedules a delayed work
*
* By default the interrupt line behavior is Active Low. Once an interrupt is
* generated by the device, until driver clears the interrupt (by reading
* the PMP message), device keeps the interrupt line in low state. Since i2c
* read is done in work queue, the interrupt line must be disabled temporarily
* to avoid ISR being called repeatedly. Re-enable the interrupt in workqueue,
* after reading the message.
*
* Note: If we use the interrupt line in Falling edge mode, there is a
* possibility to miss interrupts when ISR is getting executed.
*
*/
static irqreturn_t most_irq_handler(int irq, void *_dev)
{
struct hdm_i2c *dev = _dev;
disable_irq_nosync(irq);
dev->rx.int_disabled = true;
schedule_delayed_work(&dev->rx.dwork, 0);
return IRQ_HANDLED;
}
/*
* i2c_probe - i2c probe handler
* @client: i2c client device structure
* @id: i2c client device id
*
* Return 0 on success, negative on failure.
*
* Register the i2c client device as a MOST interface
*/
static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct hdm_i2c *dev;
int ret, i;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
/* ID format: i2c-<bus>-<address> */
snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
client->adapter->nr, client->addr);
for (i = 0; i < NUM_CHANNELS; i++) {
dev->capabilities[i].data_type = MOST_CH_CONTROL;
dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
}
dev->capabilities[CH_RX].direction = MOST_CH_RX;
dev->capabilities[CH_RX].name_suffix = "rx";
dev->capabilities[CH_TX].direction = MOST_CH_TX;
dev->capabilities[CH_TX].name_suffix = "tx";
dev->most_iface.interface = ITYPE_I2C;
dev->most_iface.description = dev->name;
dev->most_iface.num_channels = NUM_CHANNELS;
dev->most_iface.channel_vector = dev->capabilities;
dev->most_iface.configure = configure_channel;
dev->most_iface.enqueue = enqueue;
dev->most_iface.poison_channel = poison_channel;
INIT_LIST_HEAD(&dev->rx.list);
INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
dev->client = client;
i2c_set_clientdata(client, dev);
ret = most_register_interface(&dev->most_iface);
if (ret) {
pr_err("Failed to register i2c as a MOST interface\n");
kfree(dev);
return ret;
}
return 0;
}
/*
* i2c_remove - i2c remove handler
* @client: i2c client device structure
*
* Return 0 on success.
*
* Unregister the i2c client device as a MOST interface
*/
static int i2c_remove(struct i2c_client *client)
{
struct hdm_i2c *dev = i2c_get_clientdata(client);
most_deregister_interface(&dev->most_iface);
kfree(dev);
return 0;
}
static const struct i2c_device_id i2c_id[] = {
{ "most_i2c", 0 },
{ }, /* Terminating entry */
};
MODULE_DEVICE_TABLE(i2c, i2c_id);
static struct i2c_driver i2c_driver = {
.driver = {
.name = "hdm_i2c",
},
.probe = i2c_probe,
.remove = i2c_remove,
.id_table = i2c_id,
};
module_i2c_driver(i2c_driver);
MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
MODULE_DESCRIPTION("I2C Hardware Dependent Module");
MODULE_LICENSE("GPL");
+13
View File
@@ -0,0 +1,13 @@
#
# MOST Networking configuration
#
config MOST_NET
tristate "Net"
depends on NET
---help---
Say Y here if you want to commumicate via a networking device.
To compile this driver as a module, choose M here: the
module will be called most_net.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_NET) += most_net.o
most_net-objs := net.o
ccflags-y += -Idrivers/staging/
+572
View File
@@ -0,0 +1,572 @@
// SPDX-License-Identifier: GPL-2.0
/*
* net.c - Networking component for Mostcore
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/wait.h>
#include <linux/kobject.h>
#include "most/core.h"
#define MEP_HDR_LEN 8
#define MDP_HDR_LEN 16
#define MAMAC_DATA_LEN (1024 - MDP_HDR_LEN)
#define PMHL 5
#define PMS_TELID_UNSEGM_MAMAC 0x0A
#define PMS_FIFONO_MDP 0x01
#define PMS_FIFONO_MEP 0x04
#define PMS_MSGTYPE_DATA 0x04
#define PMS_DEF_PRIO 0
#define MEP_DEF_RETRY 15
#define PMS_FIFONO_MASK 0x07
#define PMS_FIFONO_SHIFT 3
#define PMS_RETRY_SHIFT 4
#define PMS_TELID_MASK 0x0F
#define PMS_TELID_SHIFT 4
#define HB(value) ((u8)((u16)(value) >> 8))
#define LB(value) ((u8)(value))
#define EXTRACT_BIT_SET(bitset_name, value) \
(((value) >> bitset_name##_SHIFT) & bitset_name##_MASK)
#define PMS_IS_MEP(buf, len) \
((len) > MEP_HDR_LEN && \
EXTRACT_BIT_SET(PMS_FIFONO, (buf)[3]) == PMS_FIFONO_MEP)
static inline bool pms_is_mamac(char *buf, u32 len)
{
return (len > MDP_HDR_LEN &&
EXTRACT_BIT_SET(PMS_FIFONO, buf[3]) == PMS_FIFONO_MDP &&
EXTRACT_BIT_SET(PMS_TELID, buf[14]) == PMS_TELID_UNSEGM_MAMAC);
}
struct net_dev_channel {
bool linked;
int ch_id;
};
struct net_dev_context {
struct most_interface *iface;
bool is_mamac;
struct net_device *dev;
struct net_dev_channel rx;
struct net_dev_channel tx;
struct list_head list;
};
static struct list_head net_devices = LIST_HEAD_INIT(net_devices);
static struct mutex probe_disc_mt; /* ch->linked = true, most_nd_open */
static struct spinlock list_lock; /* list_head, ch->linked = false, dev_hold */
static struct core_component comp;
static int skb_to_mamac(const struct sk_buff *skb, struct mbo *mbo)
{
u8 *buff = mbo->virt_address;
const u8 broadcast[] = { 0x03, 0xFF };
const u8 *dest_addr = skb->data + 4;
const u8 *eth_type = skb->data + 12;
unsigned int payload_len = skb->len - ETH_HLEN;
unsigned int mdp_len = payload_len + MDP_HDR_LEN;
if (mdp_len < skb->len) {
pr_err("drop: too large packet! (%u)\n", skb->len);
return -EINVAL;
}
if (mbo->buffer_length < mdp_len) {
pr_err("drop: too small buffer! (%d for %d)\n",
mbo->buffer_length, mdp_len);
return -EINVAL;
}
if (skb->len < ETH_HLEN) {
pr_err("drop: too small packet! (%d)\n", skb->len);
return -EINVAL;
}
if (dest_addr[0] == 0xFF && dest_addr[1] == 0xFF)
dest_addr = broadcast;
*buff++ = HB(mdp_len - 2);
*buff++ = LB(mdp_len - 2);
*buff++ = PMHL;
*buff++ = (PMS_FIFONO_MDP << PMS_FIFONO_SHIFT) | PMS_MSGTYPE_DATA;
*buff++ = PMS_DEF_PRIO;
*buff++ = dest_addr[0];
*buff++ = dest_addr[1];
*buff++ = 0x00;
*buff++ = HB(payload_len + 6);
*buff++ = LB(payload_len + 6);
/* end of FPH here */
*buff++ = eth_type[0];
*buff++ = eth_type[1];
*buff++ = 0;
*buff++ = 0;
*buff++ = PMS_TELID_UNSEGM_MAMAC << 4 | HB(payload_len);
*buff++ = LB(payload_len);
memcpy(buff, skb->data + ETH_HLEN, payload_len);
mbo->buffer_length = mdp_len;
return 0;
}
static int skb_to_mep(const struct sk_buff *skb, struct mbo *mbo)
{
u8 *buff = mbo->virt_address;
unsigned int mep_len = skb->len + MEP_HDR_LEN;
if (mep_len < skb->len) {
pr_err("drop: too large packet! (%u)\n", skb->len);
return -EINVAL;
}
if (mbo->buffer_length < mep_len) {
pr_err("drop: too small buffer! (%d for %d)\n",
mbo->buffer_length, mep_len);
return -EINVAL;
}
*buff++ = HB(mep_len - 2);
*buff++ = LB(mep_len - 2);
*buff++ = PMHL;
*buff++ = (PMS_FIFONO_MEP << PMS_FIFONO_SHIFT) | PMS_MSGTYPE_DATA;
*buff++ = (MEP_DEF_RETRY << PMS_RETRY_SHIFT) | PMS_DEF_PRIO;
*buff++ = 0;
*buff++ = 0;
*buff++ = 0;
memcpy(buff, skb->data, skb->len);
mbo->buffer_length = mep_len;
return 0;
}
static int most_nd_set_mac_address(struct net_device *dev, void *p)
{
struct net_dev_context *nd = netdev_priv(dev);
int err = eth_mac_addr(dev, p);
if (err)
return err;
nd->is_mamac =
(dev->dev_addr[0] == 0 && dev->dev_addr[1] == 0 &&
dev->dev_addr[2] == 0 && dev->dev_addr[3] == 0);
/*
* Set default MTU for the given packet type.
* It is still possible to change MTU using ip tools afterwards.
*/
dev->mtu = nd->is_mamac ? MAMAC_DATA_LEN : ETH_DATA_LEN;
return 0;
}
static void on_netinfo(struct most_interface *iface,
unsigned char link_stat, unsigned char *mac_addr);
static int most_nd_open(struct net_device *dev)
{
struct net_dev_context *nd = netdev_priv(dev);
int ret = 0;
mutex_lock(&probe_disc_mt);
if (most_start_channel(nd->iface, nd->rx.ch_id, &comp)) {
netdev_err(dev, "most_start_channel() failed\n");
ret = -EBUSY;
goto unlock;
}
if (most_start_channel(nd->iface, nd->tx.ch_id, &comp)) {
netdev_err(dev, "most_start_channel() failed\n");
most_stop_channel(nd->iface, nd->rx.ch_id, &comp);
ret = -EBUSY;
goto unlock;
}
netif_carrier_off(dev);
if (is_valid_ether_addr(dev->dev_addr))
netif_dormant_off(dev);
else
netif_dormant_on(dev);
netif_wake_queue(dev);
if (nd->iface->request_netinfo)
nd->iface->request_netinfo(nd->iface, nd->tx.ch_id, on_netinfo);
unlock:
mutex_unlock(&probe_disc_mt);
return ret;
}
static int most_nd_stop(struct net_device *dev)
{
struct net_dev_context *nd = netdev_priv(dev);
netif_stop_queue(dev);
if (nd->iface->request_netinfo)
nd->iface->request_netinfo(nd->iface, nd->tx.ch_id, NULL);
most_stop_channel(nd->iface, nd->rx.ch_id, &comp);
most_stop_channel(nd->iface, nd->tx.ch_id, &comp);
return 0;
}
static netdev_tx_t most_nd_start_xmit(struct sk_buff *skb,
struct net_device *dev)
{
struct net_dev_context *nd = netdev_priv(dev);
struct mbo *mbo;
int ret;
mbo = most_get_mbo(nd->iface, nd->tx.ch_id, &comp);
if (!mbo) {
netif_stop_queue(dev);
dev->stats.tx_fifo_errors++;
return NETDEV_TX_BUSY;
}
if (nd->is_mamac)
ret = skb_to_mamac(skb, mbo);
else
ret = skb_to_mep(skb, mbo);
if (ret) {
most_put_mbo(mbo);
dev->stats.tx_dropped++;
kfree_skb(skb);
return NETDEV_TX_OK;
}
most_submit_mbo(mbo);
dev->stats.tx_packets++;
dev->stats.tx_bytes += skb->len;
kfree_skb(skb);
return NETDEV_TX_OK;
}
static const struct net_device_ops most_nd_ops = {
.ndo_open = most_nd_open,
.ndo_stop = most_nd_stop,
.ndo_start_xmit = most_nd_start_xmit,
.ndo_set_mac_address = most_nd_set_mac_address,
};
static void most_nd_setup(struct net_device *dev)
{
ether_setup(dev);
dev->netdev_ops = &most_nd_ops;
}
static struct net_dev_context *get_net_dev(struct most_interface *iface)
{
struct net_dev_context *nd;
list_for_each_entry(nd, &net_devices, list)
if (nd->iface == iface)
return nd;
return NULL;
}
static struct net_dev_context *get_net_dev_hold(struct most_interface *iface)
{
struct net_dev_context *nd;
unsigned long flags;
spin_lock_irqsave(&list_lock, flags);
nd = get_net_dev(iface);
if (nd && nd->rx.linked && nd->tx.linked)
dev_hold(nd->dev);
else
nd = NULL;
spin_unlock_irqrestore(&list_lock, flags);
return nd;
}
static int comp_probe_channel(struct most_interface *iface, int channel_idx,
struct most_channel_config *ccfg, char *name)
{
struct net_dev_context *nd;
struct net_dev_channel *ch;
struct net_device *dev;
unsigned long flags;
int ret = 0;
if (!iface)
return -EINVAL;
if (ccfg->data_type != MOST_CH_ASYNC)
return -EINVAL;
mutex_lock(&probe_disc_mt);
nd = get_net_dev(iface);
if (!nd) {
dev = alloc_netdev(sizeof(struct net_dev_context), "meth%d",
NET_NAME_UNKNOWN, most_nd_setup);
if (!dev) {
ret = -ENOMEM;
goto unlock;
}
nd = netdev_priv(dev);
nd->iface = iface;
nd->dev = dev;
spin_lock_irqsave(&list_lock, flags);
list_add(&nd->list, &net_devices);
spin_unlock_irqrestore(&list_lock, flags);
ch = ccfg->direction == MOST_CH_TX ? &nd->tx : &nd->rx;
} else {
ch = ccfg->direction == MOST_CH_TX ? &nd->tx : &nd->rx;
if (ch->linked) {
pr_err("direction is allocated\n");
ret = -EINVAL;
goto unlock;
}
if (register_netdev(nd->dev)) {
pr_err("register_netdev() failed\n");
ret = -EINVAL;
goto unlock;
}
}
ch->ch_id = channel_idx;
ch->linked = true;
unlock:
mutex_unlock(&probe_disc_mt);
return ret;
}
static int comp_disconnect_channel(struct most_interface *iface,
int channel_idx)
{
struct net_dev_context *nd;
struct net_dev_channel *ch;
unsigned long flags;
int ret = 0;
mutex_lock(&probe_disc_mt);
nd = get_net_dev(iface);
if (!nd) {
ret = -EINVAL;
goto unlock;
}
if (nd->rx.linked && channel_idx == nd->rx.ch_id) {
ch = &nd->rx;
} else if (nd->tx.linked && channel_idx == nd->tx.ch_id) {
ch = &nd->tx;
} else {
ret = -EINVAL;
goto unlock;
}
if (nd->rx.linked && nd->tx.linked) {
spin_lock_irqsave(&list_lock, flags);
ch->linked = false;
spin_unlock_irqrestore(&list_lock, flags);
/*
* do not call most_stop_channel() here, because channels are
* going to be closed in ndo_stop() after unregister_netdev()
*/
unregister_netdev(nd->dev);
} else {
spin_lock_irqsave(&list_lock, flags);
list_del(&nd->list);
spin_unlock_irqrestore(&list_lock, flags);
free_netdev(nd->dev);
}
unlock:
mutex_unlock(&probe_disc_mt);
return ret;
}
static int comp_resume_tx_channel(struct most_interface *iface,
int channel_idx)
{
struct net_dev_context *nd;
nd = get_net_dev_hold(iface);
if (!nd)
return 0;
if (nd->tx.ch_id != channel_idx)
goto put_nd;
netif_wake_queue(nd->dev);
put_nd:
dev_put(nd->dev);
return 0;
}
static int comp_rx_data(struct mbo *mbo)
{
const u32 zero = 0;
struct net_dev_context *nd;
char *buf = mbo->virt_address;
u32 len = mbo->processed_length;
struct sk_buff *skb;
struct net_device *dev;
unsigned int skb_len;
int ret = 0;
nd = get_net_dev_hold(mbo->ifp);
if (!nd)
return -EIO;
if (nd->rx.ch_id != mbo->hdm_channel_id) {
ret = -EIO;
goto put_nd;
}
dev = nd->dev;
if (nd->is_mamac) {
if (!pms_is_mamac(buf, len)) {
ret = -EIO;
goto put_nd;
}
skb = dev_alloc_skb(len - MDP_HDR_LEN + 2 * ETH_ALEN + 2);
} else {
if (!PMS_IS_MEP(buf, len)) {
ret = -EIO;
goto put_nd;
}
skb = dev_alloc_skb(len - MEP_HDR_LEN);
}
if (!skb) {
dev->stats.rx_dropped++;
pr_err_once("drop packet: no memory for skb\n");
goto out;
}
skb->dev = dev;
if (nd->is_mamac) {
/* dest */
ether_addr_copy(skb_put(skb, ETH_ALEN), dev->dev_addr);
/* src */
skb_put_data(skb, &zero, 4);
skb_put_data(skb, buf + 5, 2);
/* eth type */
skb_put_data(skb, buf + 10, 2);
buf += MDP_HDR_LEN;
len -= MDP_HDR_LEN;
} else {
buf += MEP_HDR_LEN;
len -= MEP_HDR_LEN;
}
skb_put_data(skb, buf, len);
skb->protocol = eth_type_trans(skb, dev);
skb_len = skb->len;
if (netif_rx(skb) == NET_RX_SUCCESS) {
dev->stats.rx_packets++;
dev->stats.rx_bytes += skb_len;
} else {
dev->stats.rx_dropped++;
}
out:
most_put_mbo(mbo);
put_nd:
dev_put(nd->dev);
return ret;
}
static struct core_component comp = {
.name = "net",
.probe_channel = comp_probe_channel,
.disconnect_channel = comp_disconnect_channel,
.tx_completion = comp_resume_tx_channel,
.rx_completion = comp_rx_data,
};
static int __init most_net_init(void)
{
spin_lock_init(&list_lock);
mutex_init(&probe_disc_mt);
return most_register_component(&comp);
}
static void __exit most_net_exit(void)
{
most_deregister_component(&comp);
}
/**
* on_netinfo - callback for HDM to be informed about HW's MAC
* @param iface - most interface instance
* @param link_stat - link status
* @param mac_addr - MAC address
*/
static void on_netinfo(struct most_interface *iface,
unsigned char link_stat, unsigned char *mac_addr)
{
struct net_dev_context *nd;
struct net_device *dev;
const u8 *m = mac_addr;
nd = get_net_dev_hold(iface);
if (!nd)
return;
dev = nd->dev;
if (link_stat)
netif_carrier_on(dev);
else
netif_carrier_off(dev);
if (m && is_valid_ether_addr(m)) {
if (!is_valid_ether_addr(dev->dev_addr)) {
netdev_info(dev, "set mac %02x-%02x-%02x-%02x-%02x-%02x\n",
m[0], m[1], m[2], m[3], m[4], m[5]);
ether_addr_copy(dev->dev_addr, m);
netif_dormant_off(dev);
} else if (!ether_addr_equal(dev->dev_addr, m)) {
netdev_warn(dev, "reject mac %02x-%02x-%02x-%02x-%02x-%02x\n",
m[0], m[1], m[2], m[3], m[4], m[5]);
}
}
dev_put(nd->dev);
}
module_init(most_net_init);
module_exit(most_net_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
MODULE_DESCRIPTION("Networking Component Module for Mostcore");
+13
View File
@@ -0,0 +1,13 @@
#
# MOST ALSA configuration
#
config MOST_SOUND
tristate "Sound"
depends on SND
select SND_PCM
---help---
Say Y here if you want to commumicate via ALSA/sound devices.
To compile this driver as a module, choose M here: the
module will be called most_sound.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_SOUND) += most_sound.o
most_sound-objs := sound.o
ccflags-y += -Idrivers/staging/
+762
View File
@@ -0,0 +1,762 @@
// SPDX-License-Identifier: GPL-2.0
/*
* sound.c - Sound component for Mostcore
*
* Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <most/core.h>
#define DRIVER_NAME "sound"
static struct list_head dev_list;
static struct core_component comp;
/**
* struct channel - private structure to keep channel specific data
* @substream: stores the substream structure
* @iface: interface for which the channel belongs to
* @cfg: channel configuration
* @card: registered sound card
* @list: list for private use
* @id: channel index
* @period_pos: current period position (ring buffer)
* @buffer_pos: current buffer position (ring buffer)
* @is_stream_running: identifies whether a stream is running or not
* @opened: set when the stream is opened
* @playback_task: playback thread
* @playback_waitq: waitq used by playback thread
*/
struct channel {
struct snd_pcm_substream *substream;
struct snd_pcm_hardware pcm_hardware;
struct most_interface *iface;
struct most_channel_config *cfg;
struct snd_card *card;
struct list_head list;
int id;
unsigned int period_pos;
unsigned int buffer_pos;
bool is_stream_running;
struct task_struct *playback_task;
wait_queue_head_t playback_waitq;
void (*copy_fn)(void *alsa, void *most, unsigned int bytes);
};
#define MOST_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
SNDRV_PCM_INFO_MMAP_VALID | \
SNDRV_PCM_INFO_BATCH | \
SNDRV_PCM_INFO_INTERLEAVED | \
SNDRV_PCM_INFO_BLOCK_TRANSFER)
#define swap16(val) ( \
(((u16)(val) << 8) & (u16)0xFF00) | \
(((u16)(val) >> 8) & (u16)0x00FF))
#define swap32(val) ( \
(((u32)(val) << 24) & (u32)0xFF000000) | \
(((u32)(val) << 8) & (u32)0x00FF0000) | \
(((u32)(val) >> 8) & (u32)0x0000FF00) | \
(((u32)(val) >> 24) & (u32)0x000000FF))
static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes)
{
unsigned int i = 0;
while (i < (bytes / 2)) {
dest[i] = swap16(source[i]);
i++;
}
}
static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes)
{
unsigned int i = 0;
if (bytes < 2)
return;
while (i < bytes - 2) {
dest[i] = source[i + 2];
dest[i + 1] = source[i + 1];
dest[i + 2] = source[i];
i += 3;
}
}
static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes)
{
unsigned int i = 0;
while (i < bytes / 4) {
dest[i] = swap32(source[i]);
i++;
}
}
static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
{
memcpy(most, alsa, bytes);
}
static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
{
swap_copy16(most, alsa, bytes);
}
static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
{
swap_copy24(most, alsa, bytes);
}
static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
{
swap_copy32(most, alsa, bytes);
}
static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
{
memcpy(alsa, most, bytes);
}
static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
{
swap_copy16(alsa, most, bytes);
}
static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
{
swap_copy24(alsa, most, bytes);
}
static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes)
{
swap_copy32(alsa, most, bytes);
}
/**
* get_channel - get pointer to channel
* @iface: interface structure
* @channel_id: channel ID
*
* This traverses the channel list and returns the channel matching the
* ID and interface.
*
* Returns pointer to channel on success or NULL otherwise.
*/
static struct channel *get_channel(struct most_interface *iface,
int channel_id)
{
struct channel *channel, *tmp;
list_for_each_entry_safe(channel, tmp, &dev_list, list) {
if ((channel->iface == iface) && (channel->id == channel_id))
return channel;
}
return NULL;
}
/**
* copy_data - implements data copying function
* @channel: channel
* @mbo: MBO from core
*
* Copy data from/to ring buffer to/from MBO and update the buffer position
*/
static bool copy_data(struct channel *channel, struct mbo *mbo)
{
struct snd_pcm_runtime *const runtime = channel->substream->runtime;
unsigned int const frame_bytes = channel->cfg->subbuffer_size;
unsigned int const buffer_size = runtime->buffer_size;
unsigned int frames;
unsigned int fr0;
if (channel->cfg->direction & MOST_CH_RX)
frames = mbo->processed_length / frame_bytes;
else
frames = mbo->buffer_length / frame_bytes;
fr0 = min(buffer_size - channel->buffer_pos, frames);
channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes,
mbo->virt_address,
fr0 * frame_bytes);
if (frames > fr0) {
/* wrap around at end of ring buffer */
channel->copy_fn(runtime->dma_area,
mbo->virt_address + fr0 * frame_bytes,
(frames - fr0) * frame_bytes);
}
channel->buffer_pos += frames;
if (channel->buffer_pos >= buffer_size)
channel->buffer_pos -= buffer_size;
channel->period_pos += frames;
if (channel->period_pos >= runtime->period_size) {
channel->period_pos -= runtime->period_size;
return true;
}
return false;
}
/**
* playback_thread - function implements the playback thread
* @data: private data
*
* Thread which does the playback functionality in a loop. It waits for a free
* MBO from mostcore for a particular channel and copy the data from ring buffer
* to MBO. Submit the MBO back to mostcore, after copying the data.
*
* Returns 0 on success or error code otherwise.
*/
static int playback_thread(void *data)
{
struct channel *const channel = data;
while (!kthread_should_stop()) {
struct mbo *mbo = NULL;
bool period_elapsed = false;
wait_event_interruptible(
channel->playback_waitq,
kthread_should_stop() ||
(channel->is_stream_running &&
(mbo = most_get_mbo(channel->iface, channel->id,
&comp))));
if (!mbo)
continue;
if (channel->is_stream_running)
period_elapsed = copy_data(channel, mbo);
else
memset(mbo->virt_address, 0, mbo->buffer_length);
most_submit_mbo(mbo);
if (period_elapsed)
snd_pcm_period_elapsed(channel->substream);
}
return 0;
}
/**
* pcm_open - implements open callback function for PCM middle layer
* @substream: pointer to ALSA PCM substream
*
* This is called when a PCM substream is opened. At least, the function should
* initialize the runtime->hw record.
*
* Returns 0 on success or error code otherwise.
*/
static int pcm_open(struct snd_pcm_substream *substream)
{
struct channel *channel = substream->private_data;
struct snd_pcm_runtime *runtime = substream->runtime;
struct most_channel_config *cfg = channel->cfg;
channel->substream = substream;
if (cfg->direction == MOST_CH_TX) {
channel->playback_task = kthread_run(playback_thread, channel,
"most_audio_playback");
if (IS_ERR(channel->playback_task)) {
pr_err("Couldn't start thread\n");
return PTR_ERR(channel->playback_task);
}
}
if (most_start_channel(channel->iface, channel->id, &comp)) {
pr_err("most_start_channel() failed!\n");
if (cfg->direction == MOST_CH_TX)
kthread_stop(channel->playback_task);
return -EBUSY;
}
runtime->hw = channel->pcm_hardware;
return 0;
}
/**
* pcm_close - implements close callback function for PCM middle layer
* @substream: sub-stream pointer
*
* Obviously, this is called when a PCM substream is closed. Any private
* instance for a PCM substream allocated in the open callback will be
* released here.
*
* Returns 0 on success or error code otherwise.
*/
static int pcm_close(struct snd_pcm_substream *substream)
{
struct channel *channel = substream->private_data;
if (channel->cfg->direction == MOST_CH_TX)
kthread_stop(channel->playback_task);
most_stop_channel(channel->iface, channel->id, &comp);
return 0;
}
/**
* pcm_hw_params - implements hw_params callback function for PCM middle layer
* @substream: sub-stream pointer
* @hw_params: contains the hardware parameters set by the application
*
* This is called when the hardware parameters is set by the application, that
* is, once when the buffer size, the period size, the format, etc. are defined
* for the PCM substream. Many hardware setups should be done is this callback,
* including the allocation of buffers.
*
* Returns 0 on success or error code otherwise.
*/
static int pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hw_params)
{
struct channel *channel = substream->private_data;
if ((params_channels(hw_params) > channel->pcm_hardware.channels_max) ||
(params_channels(hw_params) < channel->pcm_hardware.channels_min)) {
pr_err("Requested number of channels not supported.\n");
return -EINVAL;
}
return snd_pcm_lib_alloc_vmalloc_buffer(substream,
params_buffer_bytes(hw_params));
}
/**
* pcm_hw_free - implements hw_free callback function for PCM middle layer
* @substream: substream pointer
*
* This is called to release the resources allocated via hw_params.
* This function will be always called before the close callback is called.
*
* Returns 0 on success or error code otherwise.
*/
static int pcm_hw_free(struct snd_pcm_substream *substream)
{
return snd_pcm_lib_free_vmalloc_buffer(substream);
}
/**
* pcm_prepare - implements prepare callback function for PCM middle layer
* @substream: substream pointer
*
* This callback is called when the PCM is "prepared". Format rate, sample rate,
* etc., can be set here. This callback can be called many times at each setup.
*
* Returns 0 on success or error code otherwise.
*/
static int pcm_prepare(struct snd_pcm_substream *substream)
{
struct channel *channel = substream->private_data;
struct snd_pcm_runtime *runtime = substream->runtime;
struct most_channel_config *cfg = channel->cfg;
int width = snd_pcm_format_physical_width(runtime->format);
channel->copy_fn = NULL;
if (cfg->direction == MOST_CH_TX) {
if (snd_pcm_format_big_endian(runtime->format) || width == 8)
channel->copy_fn = alsa_to_most_memcpy;
else if (width == 16)
channel->copy_fn = alsa_to_most_copy16;
else if (width == 24)
channel->copy_fn = alsa_to_most_copy24;
else if (width == 32)
channel->copy_fn = alsa_to_most_copy32;
} else {
if (snd_pcm_format_big_endian(runtime->format) || width == 8)
channel->copy_fn = most_to_alsa_memcpy;
else if (width == 16)
channel->copy_fn = most_to_alsa_copy16;
else if (width == 24)
channel->copy_fn = most_to_alsa_copy24;
else if (width == 32)
channel->copy_fn = most_to_alsa_copy32;
}
if (!channel->copy_fn) {
pr_err("unsupported format\n");
return -EINVAL;
}
channel->period_pos = 0;
channel->buffer_pos = 0;
return 0;
}
/**
* pcm_trigger - implements trigger callback function for PCM middle layer
* @substream: substream pointer
* @cmd: action to perform
*
* This is called when the PCM is started, stopped or paused. The action will be
* specified in the second argument, SNDRV_PCM_TRIGGER_XXX
*
* Returns 0 on success or error code otherwise.
*/
static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
struct channel *channel = substream->private_data;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
channel->is_stream_running = true;
wake_up_interruptible(&channel->playback_waitq);
return 0;
case SNDRV_PCM_TRIGGER_STOP:
channel->is_stream_running = false;
return 0;
default:
pr_info("%s(), invalid\n", __func__);
return -EINVAL;
}
return 0;
}
/**
* pcm_pointer - implements pointer callback function for PCM middle layer
* @substream: substream pointer
*
* This callback is called when the PCM middle layer inquires the current
* hardware position on the buffer. The position must be returned in frames,
* ranging from 0 to buffer_size-1.
*/
static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
{
struct channel *channel = substream->private_data;
return channel->buffer_pos;
}
/**
* Initialization of struct snd_pcm_ops
*/
static const struct snd_pcm_ops pcm_ops = {
.open = pcm_open,
.close = pcm_close,
.ioctl = snd_pcm_lib_ioctl,
.hw_params = pcm_hw_params,
.hw_free = pcm_hw_free,
.prepare = pcm_prepare,
.trigger = pcm_trigger,
.pointer = pcm_pointer,
.page = snd_pcm_lib_get_vmalloc_page,
};
static int split_arg_list(char *buf, char **card_name, u16 *ch_num,
char **sample_res)
{
char *num;
int ret;
*card_name = strsep(&buf, ".");
if (!*card_name) {
pr_err("Missing sound card name\n");
return -EIO;
}
num = strsep(&buf, "x");
if (!num)
goto err;
ret = kstrtou16(num, 0, ch_num);
if (ret)
goto err;
*sample_res = strsep(&buf, ".\n");
if (!*sample_res)
goto err;
return 0;
err:
pr_err("Bad PCM format\n");
return -EIO;
}
static const struct sample_resolution_info {
const char *sample_res;
int bytes;
u64 formats;
} sinfo[] = {
{ "8", 1, SNDRV_PCM_FMTBIT_S8 },
{ "16", 2, SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE },
{ "24", 3, SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE },
{ "32", 4, SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE },
};
static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
u16 ch_num, char *sample_res,
struct most_channel_config *cfg)
{
int i;
for (i = 0; i < ARRAY_SIZE(sinfo); i++) {
if (!strcmp(sample_res, sinfo[i].sample_res))
goto found;
}
pr_err("Unsupported PCM format\n");
return -EIO;
found:
if (!ch_num) {
pr_err("Bad number of channels\n");
return -EINVAL;
}
if (cfg->subbuffer_size != ch_num * sinfo[i].bytes) {
pr_err("Audio resolution doesn't fit subbuffer size\n");
return -EINVAL;
}
pcm_hw->info = MOST_PCM_INFO;
pcm_hw->rates = SNDRV_PCM_RATE_48000;
pcm_hw->rate_min = 48000;
pcm_hw->rate_max = 48000;
pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
pcm_hw->period_bytes_min = cfg->buffer_size;
pcm_hw->period_bytes_max = cfg->buffer_size;
pcm_hw->periods_min = 1;
pcm_hw->periods_max = cfg->num_buffers;
pcm_hw->channels_min = ch_num;
pcm_hw->channels_max = ch_num;
pcm_hw->formats = sinfo[i].formats;
return 0;
}
/**
* audio_probe_channel - probe function of the driver module
* @iface: pointer to interface instance
* @channel_id: channel index/ID
* @cfg: pointer to actual channel configuration
* @arg_list: string that provides the name of the device to be created in /dev
* plus the desired audio resolution
*
* Creates sound card, pcm device, sets pcm ops and registers sound card.
*
* Returns 0 on success or error code otherwise.
*/
static int audio_probe_channel(struct most_interface *iface, int channel_id,
struct most_channel_config *cfg,
char *arg_list)
{
struct channel *channel;
struct snd_card *card;
struct snd_pcm *pcm;
int playback_count = 0;
int capture_count = 0;
int ret;
int direction;
char *card_name;
u16 ch_num;
char *sample_res;
if (!iface)
return -EINVAL;
if (cfg->data_type != MOST_CH_SYNC) {
pr_err("Incompatible channel type\n");
return -EINVAL;
}
if (get_channel(iface, channel_id)) {
pr_err("channel (%s:%d) is already linked\n",
iface->description, channel_id);
return -EINVAL;
}
if (cfg->direction == MOST_CH_TX) {
playback_count = 1;
direction = SNDRV_PCM_STREAM_PLAYBACK;
} else {
capture_count = 1;
direction = SNDRV_PCM_STREAM_CAPTURE;
}
ret = split_arg_list(arg_list, &card_name, &ch_num, &sample_res);
if (ret < 0)
return ret;
ret = snd_card_new(&iface->dev, -1, card_name, THIS_MODULE,
sizeof(*channel), &card);
if (ret < 0)
return ret;
channel = card->private_data;
channel->card = card;
channel->cfg = cfg;
channel->iface = iface;
channel->id = channel_id;
init_waitqueue_head(&channel->playback_waitq);
ret = audio_set_hw_params(&channel->pcm_hardware, ch_num, sample_res,
cfg);
if (ret)
goto err_free_card;
snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
snprintf(card->shortname, sizeof(card->shortname), "Microchip MOST:%d",
card->number);
snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
card->shortname, iface->description, channel_id);
ret = snd_pcm_new(card, card_name, 0, playback_count,
capture_count, &pcm);
if (ret < 0)
goto err_free_card;
pcm->private_data = channel;
snd_pcm_set_ops(pcm, direction, &pcm_ops);
ret = snd_card_register(card);
if (ret < 0)
goto err_free_card;
list_add_tail(&channel->list, &dev_list);
return 0;
err_free_card:
snd_card_free(card);
return ret;
}
/**
* audio_disconnect_channel - function to disconnect a channel
* @iface: pointer to interface instance
* @channel_id: channel index
*
* This frees allocated memory and removes the sound card from ALSA
*
* Returns 0 on success or error code otherwise.
*/
static int audio_disconnect_channel(struct most_interface *iface,
int channel_id)
{
struct channel *channel;
channel = get_channel(iface, channel_id);
if (!channel) {
pr_err("sound_disconnect_channel(), invalid channel %d\n",
channel_id);
return -EINVAL;
}
list_del(&channel->list);
snd_card_free(channel->card);
return 0;
}
/**
* audio_rx_completion - completion handler for rx channels
* @mbo: pointer to buffer object that has completed
*
* This searches for the channel this MBO belongs to and copy the data from MBO
* to ring buffer
*
* Returns 0 on success or error code otherwise.
*/
static int audio_rx_completion(struct mbo *mbo)
{
struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
bool period_elapsed = false;
if (!channel) {
pr_err("sound_rx_completion(), invalid channel %d\n",
mbo->hdm_channel_id);
return -EINVAL;
}
if (channel->is_stream_running)
period_elapsed = copy_data(channel, mbo);
most_put_mbo(mbo);
if (period_elapsed)
snd_pcm_period_elapsed(channel->substream);
return 0;
}
/**
* audio_tx_completion - completion handler for tx channels
* @iface: pointer to interface instance
* @channel_id: channel index/ID
*
* This searches the channel that belongs to this combination of interface
* pointer and channel ID and wakes a process sitting in the wait queue of
* this channel.
*
* Returns 0 on success or error code otherwise.
*/
static int audio_tx_completion(struct most_interface *iface, int channel_id)
{
struct channel *channel = get_channel(iface, channel_id);
if (!channel) {
pr_err("sound_tx_completion(), invalid channel %d\n",
channel_id);
return -EINVAL;
}
wake_up_interruptible(&channel->playback_waitq);
return 0;
}
/**
* Initialization of the struct core_component
*/
static struct core_component comp = {
.name = DRIVER_NAME,
.probe_channel = audio_probe_channel,
.disconnect_channel = audio_disconnect_channel,
.rx_completion = audio_rx_completion,
.tx_completion = audio_tx_completion,
};
static int __init audio_init(void)
{
pr_info("init()\n");
INIT_LIST_HEAD(&dev_list);
return most_register_component(&comp);
}
static void __exit audio_exit(void)
{
struct channel *channel, *tmp;
pr_info("exit()\n");
list_for_each_entry_safe(channel, tmp, &dev_list, list) {
list_del(&channel->list);
snd_card_free(channel->card);
}
most_deregister_component(&comp);
}
module_init(audio_init);
module_exit(audio_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
MODULE_DESCRIPTION("Sound Component Module for Mostcore");
+13
View File
@@ -0,0 +1,13 @@
#
# MOST USB configuration
#
config MOST_USB
tristate "USB"
depends on USB && NET
---help---
Say Y here if you want to connect via USB to network tranceiver.
This device driver depends on the networking AIM.
To compile this driver as a module, choose M here: the
module will be called most_usb.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_USB) += most_usb.o
most_usb-objs := usb.o
ccflags-y += -Idrivers/staging/
File diff suppressed because it is too large. Load diff
+12
View File
@@ -0,0 +1,12 @@
#
# MOST V4L2 configuration
#
config MOST_VIDEO
tristate "Video"
depends on VIDEO_V4L2
---help---
Say Y here if you want to commumicate via Video 4 Linux.
To compile this driver as a module, choose M here: the
module will be called most_video.
+4
View File
@@ -0,0 +1,4 @@
obj-$(CONFIG_MOST_VIDEO) += most_video.o
most_video-objs := video.o
ccflags-y += -Idrivers/staging/
+577
View File
@@ -0,0 +1,577 @@
// SPDX-License-Identifier: GPL-2.0
/*
* video.c - V4L2 component for Mostcore
*
* Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/suspend.h>
#include <linux/videodev2.h>
#include <linux/mutex.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-fh.h>
#include "most/core.h"
#define V4L2_CMP_MAX_INPUT 1
static struct core_component comp;
struct most_video_dev {
struct most_interface *iface;
int ch_idx;
struct list_head list;
bool mute;
struct list_head pending_mbos;
spinlock_t list_lock;
struct v4l2_device v4l2_dev;
atomic_t access_ref;
struct video_device *vdev;
unsigned int ctrl_input;
struct mutex lock;
wait_queue_head_t wait_data;
};
struct comp_fh {
/* must be the first field of this struct! */
struct v4l2_fh fh;
struct most_video_dev *mdev;
u32 offs;
};
static struct list_head video_devices = LIST_HEAD_INIT(video_devices);
static struct spinlock list_lock;
static inline bool data_ready(struct most_video_dev *mdev)
{
return !list_empty(&mdev->pending_mbos);
}
static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
{
return list_first_entry(&mdev->pending_mbos, struct mbo, list);
}
static int comp_vdev_open(struct file *filp)
{
int ret;
struct video_device *vdev = video_devdata(filp);
struct most_video_dev *mdev = video_drvdata(filp);
struct comp_fh *fh;
switch (vdev->vfl_type) {
case VFL_TYPE_GRABBER:
break;
default:
return -EINVAL;
}
fh = kzalloc(sizeof(*fh), GFP_KERNEL);
if (!fh)
return -ENOMEM;
if (!atomic_inc_and_test(&mdev->access_ref)) {
v4l2_err(&mdev->v4l2_dev, "too many clients\n");
ret = -EBUSY;
goto err_dec;
}
fh->mdev = mdev;
v4l2_fh_init(&fh->fh, vdev);
filp->private_data = fh;
v4l2_fh_add(&fh->fh);
ret = most_start_channel(mdev->iface, mdev->ch_idx, &comp);
if (ret) {
v4l2_err(&mdev->v4l2_dev, "most_start_channel() failed\n");
goto err_rm;
}
return 0;
err_rm:
v4l2_fh_del(&fh->fh);
v4l2_fh_exit(&fh->fh);
err_dec:
atomic_dec(&mdev->access_ref);
kfree(fh);
return ret;
}
static int comp_vdev_close(struct file *filp)
{
struct comp_fh *fh = filp->private_data;
struct most_video_dev *mdev = fh->mdev;
struct mbo *mbo, *tmp;
/*
* We need to put MBOs back before we call most_stop_channel()
* to deallocate MBOs.
* From the other hand mostcore still calling rx_completion()
* to deliver MBOs until most_stop_channel() is called.
* Use mute to work around this issue.
* This must be implemented in core.
*/
spin_lock_irq(&mdev->list_lock);
mdev->mute = true;
list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
list_del(&mbo->list);
spin_unlock_irq(&mdev->list_lock);
most_put_mbo(mbo);
spin_lock_irq(&mdev->list_lock);
}
spin_unlock_irq(&mdev->list_lock);
most_stop_channel(mdev->iface, mdev->ch_idx, &comp);
mdev->mute = false;
v4l2_fh_del(&fh->fh);
v4l2_fh_exit(&fh->fh);
atomic_dec(&mdev->access_ref);
kfree(fh);
return 0;
}
static ssize_t comp_vdev_read(struct file *filp, char __user *buf,
size_t count, loff_t *pos)
{
struct comp_fh *fh = filp->private_data;
struct most_video_dev *mdev = fh->mdev;
int ret = 0;
if (*pos)
return -ESPIPE;
if (!mdev)
return -ENODEV;
/* wait for the first buffer */
if (!(filp->f_flags & O_NONBLOCK)) {
if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
return -ERESTARTSYS;
}
if (!data_ready(mdev))
return -EAGAIN;
while (count > 0 && data_ready(mdev)) {
struct mbo *const mbo = get_top_mbo(mdev);
int const rem = mbo->processed_length - fh->offs;
int const cnt = rem < count ? rem : count;
if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
v4l2_err(&mdev->v4l2_dev, "read: copy_to_user failed\n");
if (!ret)
ret = -EFAULT;
return ret;
}
fh->offs += cnt;
count -= cnt;
buf += cnt;
ret += cnt;
if (cnt >= rem) {
fh->offs = 0;
spin_lock_irq(&mdev->list_lock);
list_del(&mbo->list);
spin_unlock_irq(&mdev->list_lock);
most_put_mbo(mbo);
}
}
return ret;
}
static __poll_t comp_vdev_poll(struct file *filp, poll_table *wait)
{
struct comp_fh *fh = filp->private_data;
struct most_video_dev *mdev = fh->mdev;
__poll_t mask = 0;
/* only wait if no data is available */
if (!data_ready(mdev))
poll_wait(filp, &mdev->wait_data, wait);
if (data_ready(mdev))
mask |= EPOLLIN | EPOLLRDNORM;
return mask;
}
static void comp_set_format_struct(struct v4l2_format *f)
{
f->fmt.pix.width = 8;
f->fmt.pix.height = 8;
f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
f->fmt.pix.bytesperline = 0;
f->fmt.pix.sizeimage = 188 * 2;
f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
f->fmt.pix.field = V4L2_FIELD_NONE;
f->fmt.pix.priv = 0;
}
static int comp_set_format(struct most_video_dev *mdev, unsigned int cmd,
struct v4l2_format *format)
{
if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
return -EINVAL;
if (cmd == VIDIOC_TRY_FMT)
return 0;
comp_set_format_struct(format);
return 0;
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct comp_fh *fh = priv;
struct most_video_dev *mdev = fh->mdev;
strlcpy(cap->driver, "v4l2_component", sizeof(cap->driver));
strlcpy(cap->card, "MOST", sizeof(cap->card));
snprintf(cap->bus_info, sizeof(cap->bus_info),
"%s", mdev->iface->description);
cap->capabilities =
V4L2_CAP_READWRITE |
V4L2_CAP_TUNER |
V4L2_CAP_VIDEO_CAPTURE;
return 0;
}
static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index)
return -EINVAL;
strcpy(f->description, "MPEG");
f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
f->flags = V4L2_FMT_FLAG_COMPRESSED;
f->pixelformat = V4L2_PIX_FMT_MPEG;
return 0;
}
static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
comp_set_format_struct(f);
return 0;
}
static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct comp_fh *fh = priv;
struct most_video_dev *mdev = fh->mdev;
return comp_set_format(mdev, VIDIOC_TRY_FMT, f);
}
static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct comp_fh *fh = priv;
struct most_video_dev *mdev = fh->mdev;
return comp_set_format(mdev, VIDIOC_S_FMT, f);
}
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
{
*norm = V4L2_STD_UNKNOWN;
return 0;
}
static int vidioc_enum_input(struct file *file, void *priv,
struct v4l2_input *input)
{
struct comp_fh *fh = priv;
struct most_video_dev *mdev = fh->mdev;
if (input->index >= V4L2_CMP_MAX_INPUT)
return -EINVAL;
strcpy(input->name, "MOST Video");
input->type |= V4L2_INPUT_TYPE_CAMERA;
input->audioset = 0;
input->std = mdev->vdev->tvnorms;
return 0;
}
static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
struct comp_fh *fh = priv;
struct most_video_dev *mdev = fh->mdev;
*i = mdev->ctrl_input;
return 0;
}
static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
{
struct comp_fh *fh = priv;
struct most_video_dev *mdev = fh->mdev;
if (index >= V4L2_CMP_MAX_INPUT)
return -EINVAL;
mdev->ctrl_input = index;
return 0;
}
static const struct v4l2_file_operations comp_fops = {
.owner = THIS_MODULE,
.open = comp_vdev_open,
.release = comp_vdev_close,
.read = comp_vdev_read,
.poll = comp_vdev_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops video_ioctl_ops = {
.vidioc_querycap = vidioc_querycap,
.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
.vidioc_g_std = vidioc_g_std,
.vidioc_enum_input = vidioc_enum_input,
.vidioc_g_input = vidioc_g_input,
.vidioc_s_input = vidioc_s_input,
};
static const struct video_device comp_videodev_template = {
.fops = &comp_fops,
.release = video_device_release,
.ioctl_ops = &video_ioctl_ops,
.tvnorms = V4L2_STD_UNKNOWN,
};
/**************************************************************************/
static struct most_video_dev *get_comp_dev(
struct most_interface *iface, int channel_idx)
{
struct most_video_dev *mdev;
unsigned long flags;
spin_lock_irqsave(&list_lock, flags);
list_for_each_entry(mdev, &video_devices, list) {
if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
spin_unlock_irqrestore(&list_lock, flags);
return mdev;
}
}
spin_unlock_irqrestore(&list_lock, flags);
return NULL;
}
static int comp_rx_data(struct mbo *mbo)
{
unsigned long flags;
struct most_video_dev *mdev =
get_comp_dev(mbo->ifp, mbo->hdm_channel_id);
if (!mdev)
return -EIO;
spin_lock_irqsave(&mdev->list_lock, flags);
if (unlikely(mdev->mute)) {
spin_unlock_irqrestore(&mdev->list_lock, flags);
return -EIO;
}
list_add_tail(&mbo->list, &mdev->pending_mbos);
spin_unlock_irqrestore(&mdev->list_lock, flags);
wake_up_interruptible(&mdev->wait_data);
return 0;
}
static int comp_register_videodev(struct most_video_dev *mdev)
{
int ret;
init_waitqueue_head(&mdev->wait_data);
/* allocate and fill v4l2 video struct */
mdev->vdev = video_device_alloc();
if (!mdev->vdev)
return -ENOMEM;
/* Fill the video capture device struct */
*mdev->vdev = comp_videodev_template;
mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
mdev->vdev->lock = &mdev->lock;
snprintf(mdev->vdev->name, sizeof(mdev->vdev->name), "MOST: %s",
mdev->v4l2_dev.name);
/* Register the v4l2 device */
video_set_drvdata(mdev->vdev, mdev);
ret = video_register_device(mdev->vdev, VFL_TYPE_GRABBER, -1);
if (ret) {
v4l2_err(&mdev->v4l2_dev, "video_register_device failed (%d)\n",
ret);
video_device_release(mdev->vdev);
}
return ret;
}
static void comp_unregister_videodev(struct most_video_dev *mdev)
{
video_unregister_device(mdev->vdev);
}
static void comp_v4l2_dev_release(struct v4l2_device *v4l2_dev)
{
struct most_video_dev *mdev =
container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
v4l2_device_unregister(v4l2_dev);
kfree(mdev);
}
static int comp_probe_channel(struct most_interface *iface, int channel_idx,
struct most_channel_config *ccfg, char *name)
{
int ret;
struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
if (mdev) {
pr_err("channel already linked\n");
return -EEXIST;
}
if (ccfg->direction != MOST_CH_RX) {
pr_err("wrong direction, expect rx\n");
return -EINVAL;
}
if (ccfg->data_type != MOST_CH_SYNC &&
ccfg->data_type != MOST_CH_ISOC) {
pr_err("wrong channel type, expect sync or isoc\n");
return -EINVAL;
}
mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
if (!mdev)
return -ENOMEM;
mutex_init(&mdev->lock);
atomic_set(&mdev->access_ref, -1);
spin_lock_init(&mdev->list_lock);
INIT_LIST_HEAD(&mdev->pending_mbos);
mdev->iface = iface;
mdev->ch_idx = channel_idx;
mdev->v4l2_dev.release = comp_v4l2_dev_release;
/* Create the v4l2_device */
strlcpy(mdev->v4l2_dev.name, name, sizeof(mdev->v4l2_dev.name));
ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
if (ret) {
pr_err("v4l2_device_register() failed\n");
kfree(mdev);
return ret;
}
ret = comp_register_videodev(mdev);
if (ret)
goto err_unreg;
spin_lock_irq(&list_lock);
list_add(&mdev->list, &video_devices);
spin_unlock_irq(&list_lock);
return 0;
err_unreg:
v4l2_device_disconnect(&mdev->v4l2_dev);
v4l2_device_put(&mdev->v4l2_dev);
return ret;
}
static int comp_disconnect_channel(struct most_interface *iface,
int channel_idx)
{
struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
if (!mdev) {
pr_err("no such channel is linked\n");
return -ENOENT;
}
spin_lock_irq(&list_lock);
list_del(&mdev->list);
spin_unlock_irq(&list_lock);
comp_unregister_videodev(mdev);
v4l2_device_disconnect(&mdev->v4l2_dev);
v4l2_device_put(&mdev->v4l2_dev);
return 0;
}
static struct core_component comp = {
.name = "video",
.probe_channel = comp_probe_channel,
.disconnect_channel = comp_disconnect_channel,
.rx_completion = comp_rx_data,
};
static int __init comp_init(void)
{
spin_lock_init(&list_lock);
return most_register_component(&comp);
}
static void __exit comp_exit(void)
{
struct most_video_dev *mdev, *tmp;
/*
* As the mostcore currently doesn't call disconnect_channel()
* for linked channels while we call most_deregister_component()
* we simulate this call here.
* This must be fixed in core.
*/
spin_lock_irq(&list_lock);
list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
list_del(&mdev->list);
spin_unlock_irq(&list_lock);
comp_unregister_videodev(mdev);
v4l2_device_disconnect(&mdev->v4l2_dev);
v4l2_device_put(&mdev->v4l2_dev);
spin_lock_irq(&list_lock);
}
spin_unlock_irq(&list_lock);
most_deregister_component(&comp);
BUG_ON(!list_empty(&video_devices));
}
module_init(comp_init);
module_exit(comp_exit);
MODULE_DESCRIPTION("V4L2 Component Module for Mostcore");
MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
MODULE_LICENSE("GPL");