diff --git a/Documentation/sound/soc/index.rst b/Documentation/sound/soc/index.rst index 22878d73f7a54a..2d2c0f35e6d131 100644 --- a/Documentation/sound/soc/index.rst +++ b/Documentation/sound/soc/index.rst @@ -19,3 +19,4 @@ The documentation is split into the following sections:- dpcm codec-to-codec usb + sof-uaol diff --git a/Documentation/sound/soc/sof-uaol.rst b/Documentation/sound/soc/sof-uaol.rst new file mode 100644 index 00000000000000..3acacd93323087 --- /dev/null +++ b/Documentation/sound/soc/sof-uaol.rst @@ -0,0 +1,1246 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================================== +SOF USB Audio Offload Link (UAOL) +=================================== + +Scope +===== + +This document describes the Linux implementation of USB audio offload on +Intel platforms with SOF. It covers the Linux host side, including the +configuration the host hands to the DSP firmware. It does not describe +firmware internals and is not a hardware specification. + +For the generic ASoC USB framework, see :doc:`usb`. Of the two control +models it describes, SOF uses the platform card route controls: writable +``USB Offload Playback Route`` and ``USB Offload Capture Route`` +controls, one per backend, on the *SOF platform* card, which select the USB +device the offloaded stream of that backend goes to. The read-only +``USB Offload ... Card/PCM Route`` controls on the *USB* sound card are not +created. + +.. contents:: In this document + :local: + :depth: 2 + +Architecture and design principles +================================== + +The core idea: a second path into the same USB PCM +-------------------------------------------------- + +Offload does not replace the normal USB audio path, and does not change the +USB sound card. snd-usb-audio still creates the USB card and its PCMs, and +those PCMs still move audio the usual way: the CPU submits URBs to the xHCI +controller. This document calls that the *URB path*. + +The SOF card adds a second way into the same USB PCM, the *offload path*. +A SOF frontend PCM carries the audio through the DSP and the UAOL link +directly into the USB PCM that a route control has selected. + +The diagram below shows, for playback, how a sound server such as PipeWire +or PulseAudio could choose between the two paths for a USB headset, and the +route the audio then takes. Capture uses the same paths in the opposite +direction:: + + +--------------------------------------------+ + | Desktop / Sound Server (PW / PA) | + | Target: USB Headset (Card #1, PCM #0) | + +---------------------+----------------------+ + | + +-----------------+-----------------+ + | 'Use USB Offload' Checkbox? | + [ UNCHECKED ] [ CHECKED ] + | | + | +----------v----------+ + | | 1. Has 'USB Offload | + | | Jack' (Plugged)? | + | +----+-----------+----+ + | No | Yes | + | | v + | | +-----------------+ + | | | 2. Is Headset | + | | | in Route? | + | | +----+-------+----+ + | | No | Yes | + | |<------+ | + | | v + v v [ UAOL Path ] + [ Standard URB Path ] <------------------+ 1. Set Route + Open: hw:, 2. Open: hw:, + | | + v v + +--------------------+ +--------------------+ + | snd-usb-audio | | SOF Frontend | + | Host Driver | +---------+----------+ + +---------+----------+ | Host DMA + | v + Submits | URBs +--------------------+ + v | Intel Audio DSP | + +--------------------+ | pipeline -> UAOL | + | System Memory | | DAI copier (IPC4 | + | Transfer Rings | | gateway, set up | + +---------+----------+ | by the UAOL BE) | + | +---------+----------+ + Periodic DMA | Link DMA over + | | the dedicated + | | UAOL link + | | + v v + +---------------------------------------------------------------------+ + | xHCI Controller Engine | + | [ System Memory Rings (URB) ] vs. [ Audio Sideband (UAOL) ] | + +----------------------------------+----------------------------------+ + | + Isochronous Packets + v + +-------------------------------+ + | Physical USB Headset Endpoint | + +-------------------------------+ + +The decision part of the diagram is userspace policy, shown as an example. +The "Use USB Offload" switch belongs to the sound server; the kernel provides +no such setting. What the kernel provides are the inputs to the decision and +the means to act on it: + +1. The ``USB Offload Jack`` tells whether any USB device can be offloaded at + all. +2. The items of the ``USB Offload Playback Route`` and ``USB Offload + Capture Route`` controls tell which ones. Each item name starts with + ``,``, the USB card and PCM numbers, so the sound server can + match the device it targets. +3. Selecting the item in the route control of a backend and opening the SOF + frontend PCM routed to that backend starts offload. The selection fails + with ``-EBUSY`` while an offloaded stream is set up on that backend. + +Opening the SOF frontend can still fail later, in ``hw_params``. The usual +reasons are that the USB card's PCM is already open, or that the device +cannot run the requested format over UAOL. The sound server should then fall +back to the URB path, just as it does when the jack or the route item is +missing. + +For userspace the difference between the two paths is which PCM device it +opens: + +* **Same device, same card, same controls.** The USB device, its USB card, + and the card's mixer controls stay as they are. Device controls such as the + USB device's own volume and mute remain on the USB card and apply to + whichever path is in use, because snd-usb-audio still owns the device. +* **Same userspace rules.** Whatever userspace applies to the USB card, for + example its UCM configuration, applies to the offload path as well. Only + the PCM device differs: the SOF card's frontend instead of the USB card's + PCM. +* **One path at a time per USB PCM and direction.** Both paths share the USB + substream's ownership flag. While the offload path holds it, opening the + USB card's PCM fails with ``-EBUSY``. While the USB card's PCM is open, the + offload stream's ``hw_params`` fails with ``-EBUSY``. A running stream is + never moved from one path to the other. +* **Offload is optional.** The URB path is always available, and is the + fallback whenever the offload path is not. + +What is offloaded? +------------------ + +UAOL (USB Audio Offload Link) is a dedicated hardware link between the Intel +audio controller and the xHCI controller's Audio Sideband interface. Once an +isochronous endpoint is assigned an Audio Sideband resource, the xHCI +controller exchanges that endpoint's audio data with the DSP over UAOL instead +of with transfer rings in system memory. The xHCI controller still schedules +the USB transactions; the DSP produces or consumes the audio. + +The CPU remains in charge of everything else: USB enumeration, control +requests and interface configuration, PCM negotiation, resource allocation, +and the stream lifecycle. The goal is to take the CPU off the steady-state +audio data path so that the host can stay idle for longer. + +This is not compressed-audio offload: the DSP carries PCM audio. Audio is +offloaded only when an application opens a SOF PCM that the topology routes +to a UAOL backend; opening the USB card's own PCM always uses the URB path. + +Two views of the same playback stream are useful. Capture is the mirror image, +with audio flowing upwards:: + + ASoC / ALSA view Audio data path + ---------------- --------------- + Application + | + v + FE: SOF platform-card PCM ...... Host DMA (system memory -> DSP) + | | + | DPCM, connected by v + | topology routes DSP pipeline (host copier, + | processing modules) + v | + BE: UAOL0-Play0 (no PCM) ........ UAOL DAI copier (IPC4 gateway) + CPU DAI "UAOL0 Playback Pin0" | + v + Audio link DMA + | + v + UAOL link + | + v + xHCI Audio Sideband + | + v + USB isochronous OUT endpoint + + Explicit playback feedback (separate return path): + USB feedback IN endpoint -> xHCI Audio Sideband -> UAOL link + -> feedback link DMA (capture direction) -> UAOL DAI copier + +The frontend (FE) is the PCM that the application opens. The backend (BE) is +the ASoC DAI link on the UAOL side. Topology routes connect the FE to the BE +through DPCM. The dotted lines link each ASoC object to the part of the data +path it controls. The FE controls the host DMA. The BE's CPU DAI callbacks +acquire the USB endpoints, set up the link DMAs, and configure the UAOL DAI +copier, which is the DSP-side gateway to the link. The BE is not a separate +processing stage. + +Control and setup run on the CPU, apart from the audio path:: + + Hotplug (device discovery): + snd-usb-audio enumeration / platform callbacks + -> USB offload library (candidate filter, sideband registration) + -> SOF hotplug callbacks + -> ASoC USB device list, route controls, and SOF jack + + Stream setup (BE hw_params): + SOF UAOL BE DAI + -> USB offload library + -> snd-usb-audio endpoint preparation + xHCI sideband resource binding + -> endpoint/resource identity returned to SOF + -> IPC4 gateway configuration and link DMA setup + +Unlike the Qualcomm USB offload design, the DSP does not get the xHCI transfer +and event rings and does not queue TRBs itself. The SOF path calls none of the +sideband ring-export or secondary-interrupter APIs; the endpoint-to-resource +assignment alone routes the data. + +Ownership boundaries +-------------------- + +The central rule is to retain each subsystem's existing responsibility: + +* **snd-usb-audio owns the USB device.** No competing USB class driver claims + it. Its descriptor parsing, format lists, interface configuration, endpoint + setup, and runtime-PM helpers are reused. +* **The USB offload library owns the USB-side offload records.** It filters + candidates, registers Audio Sideband access, claims USB substreams, and + acquires/releases endpoint resources. It does not build SOF IPC messages, + program audio registers, or own the ASoC USB port. +* **SOF owns the DSP and audio-controller side.** It publishes the ASoC USB + port, translates hotplug identities into ASoC USB devices, manages the jack, + allocates link DMAs, and completes the firmware gateway configuration. +* **The ASoC USB framework owns route policy/state.** It keeps the available + device list and the selected USB device/PCM pair for each direction. +* **The xHCI driver owns controller-side endpoint binding.** It reserves Audio + Sideband resources and attaches them to endpoints with xHCI + extended-property commands. +* **The topology and machine driver define the audio graph.** A route control + only chooses which USB device a UAOL backend talks to. It does not create a + frontend, load a topology, or change the DSP processing graph. + +A USB device moves through three states, which are easily confused: + +Available + The device passed the candidate filter, registered for Audio Sideband use, + and was published to the SOF port. It appears in the route controls and + counts toward the jack state. + +Selected + A direction's route control points at one of the device's USB PCMs. + Selecting a route does not claim the USB substream or allocate any endpoint + resource. + +Acquired + The BE's ``hw_params`` has claimed the USB substream and bound its active + endpoints to Audio Sideband resources. The actual endpoint and resource + identities are only known from this point. + +A device can be available and selected and still fail acquisition, for example +because its USB PCM is already open on the USB card. A stream that is already +running on the USB card is never moved to the DSP. + +Platform discovery and card construction +======================================== + +Build and runtime gates +----------------------- + +``CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD`` enables SOF's integration. Its +dependencies include ``SND_SOC_SOF_HDA_GENERIC``, ``SND_SOC_SOF_HDA_LINK``, +``SND_USB_OFFLOAD``, and ``SND_SOC_USB``; Kconfig also prevents incompatible +built-in/module combinations. The USB library requires ``SND_USB_AUDIO``, +``USB_XHCI_HCD``, and ``USB_XHCI_SIDEBAND``. + +Runtime discovery has two independent inputs: + +1. HDA extended-multilink enumeration finds Intel UAOL alternate links. +2. The booted IPC4 firmware reports ``SOF_IPC4_FW_CFG_UAOL_SUPPORT`` and + ``SOF_IPC4_HW_CFG_INTEL_UAOL_CAPS``. The latter supplies the link count, + stream capabilities, and transmit/receive FIFO sizes. + +The loader checks the size of the capability payload before keeping it. +``hda_uaol_link_caps()`` returns the cached capabilities of a link, and +``hda_uaol_available()`` tells whether the firmware supports UAOL at all. + +``hda_machine_register()`` registers with the offload library +(``usb_offload_register()``) after the firmware configuration has been +parsed, then registers the machine driver. The registration passes a +``struct usb_offload_res``: the SOF device, the hotplug and resource-range +callbacks, and two policy flags, ``s24_3le_as_s24_le`` and +``async_playback_on_host`` (see `Negotiation rules`_ and `Asynchronous +playback stays on the host`_). The library knows nothing about UAOL links or +xHCI controllers: it has one context per registered host, and the host +answers for the resources. + +A failure to set up offload does not stop the rest of the sound card from +registering; it is logged at debug level only. A working sound card +therefore does not prove that USB offload is working. + +Backend links and topology +-------------------------- + +``mach_params.uaol_link_mask`` tells the machine driver which UAOL links +exist, and ``mach_params.uaol_playback_num`` and ``uaol_capture_num`` how +many streams per direction each link runs at a time. ``hda_finalize_mach_params()`` +fills the stream counts in from the firmware's link capabilities once the +firmware has booted: a link with ``T`` output and ``I`` input streams carries +``T`` playback and ``I - T`` capture streams, the remaining ``T`` input +streams being the clock feedback of the playback streams (see `FIFO +partitioning and constraints`_). The static DAI count is the ceiling. It +clears the mask if UAOL support is not built in or the firmware does not +support it. The ``snd_sof_intel_hda_generic.uaol_link_mask`` module +parameter, set at load time, can override the discovered mask. Setting bits +for links that were not discovered produces a warning; it cannot add hardware +that is not there. + +The generic HDA board helper and the SoundWire machine driver add one +unidirectional backend per stream of each enabled link. With two streams per +direction: + +.. list-table:: + :header-rows: 1 + :widths: 20 30 50 + + * - Direction + - Backend link name + - CPU DAI name + * - Playback + - ``UAOL0-Play0``, ``UAOL0-Play1`` + - ``UAOL0 Playback Pin0``, ``UAOL0 Playback Pin1`` + * - Capture + - ``UAOL0-Cap0``, ``UAOL0-Cap1`` + - ``UAOL0 Capture Pin0``, ``UAOL0 Capture Pin1`` + +These are no-PCM backend links with a dummy codec. They are not user-visible +PCMs and not USB sound cards. In the names, the first ``0`` is the UAOL link +index and the trailing number the ordinal of the stream among those of its +direction. The ordinal only tells the backends apart: it is **not** the UAOL +stream the backend runs on, which is decided by the sideband resource its +endpoint gets at acquisition (see `Resource IDs, UAOL stream indices, and DMA +tags`_). Two backends of a direction are interchangeable. + +The backends are created ordinal first, playback before capture within an +ordinal: ``Play0``, ``Cap0``, ``Play1``, ``Cap1``. The first pair therefore +keeps two consecutive backend IDs whatever the stream count, and a topology +describing one stream per direction stays valid; a backend the topology +leaves out is simply unused. + +Each direction gets its own backend and DAI because the backend ``hw_params`` +fixup is not told the stream direction. A duplex backend could not tell +whether to use the topology's playback or capture DAI copier. + +The backend names are abbreviated on purpose. The topology names the DAI +copier widget ``dai-copier.UAOL..``, and alsa-lib keeps +element ids in ``SNDRV_CTL_ELEM_ID_NAME_MAXLEN`` bytes: from 38 characters on, +the ids of the widget's ``dai.word`` and ``dai.string`` tuple sections collide +and one set is silently dropped from the topology. + +A matching topology must provide the frontends, the routes, and the +``SOF_DAI_INTEL_UAOL`` DAI copiers. The topology decides the frontend PCM +numbers and the processing graph; the backend names above do not. + +In this implementation the HDA code limits the usable UAOL link count to one. +The multilink code has no per-instance offset for UAOL, so further sublinks +would all alias to sublink zero. Within the link, every backend has an ASoC +USB route of its own, so with two devices connected, or one device with two +PCM pairs, both backends of a direction can run at the same time, each on its +own device or PCM. + +Publishing the ASoC USB port +---------------------------- + +The ASoC USB port (``struct snd_soc_usb``) is where USB devices are published +and where the route state lives. There is one per audio controller. The first +UAOL CPU DAI to probe in the card creates it, and the other UAOL DAIs reuse +it. Every UAOL CPU DAI also claims a ``struct hda_uaol_stream`` of its own at +probe time; that is where the acquired device, the backend substream, the +feedback link DMA and the frontend narrowing rules of its backend live, and +which ASoC USB route of its direction the backend plays to. The link and the +route are parsed from the DAI name, ``UAOL Pin``: +``UAOL0 Playback Pin1`` is playback route 1 of link 0. + +The probe sets the ``is_route_busy`` callback, which reports whether the +backend of a route holds an acquired stream, gives the port as many routes +per direction as the link has streams (``snd_soc_usb_set_num_routes()``), +creates the ``USB Offload Jack``, creates the route controls, stores the +port in ``hdev->uaol_usb``, and finally calls ``snd_soc_usb_add_port()``. The +jack has to exist before the port is added, because adding the port +rediscovers devices that were connected earlier, and those report the jack. + +ASoC USB normally finds the port through a device-tree ``usb-soc-be`` +phandle. On these ACPI/PCI platforms there is none, so the port is found by +its component's device: SOF passes ``sdev->dev`` to +``snd_soc_usb_connect()`` and ``snd_soc_usb_disconnect()``. + +If the offload library is not running, the probe still creates the jack and +leaves it unplugged, but adds no port and no route controls. + +USB discovery and route population +================================== + +Candidate detection +------------------- + +The library registers a ``snd_usb_platform_ops`` table with connect and +disconnect callbacks. snd-usb-audio keeps driving the device and calls these +callbacks as its interfaces are probed and removed. + +The bridge accepts a device only if it passes the descriptor filter *and* +registers for xHCI Audio Sideband use: + +* The device must be full-speed or high-speed. SuperSpeed devices are rejected. +* For UAC1, the streaming interfaces are found from the AudioControl header's + interface collection. +* For UAC2, the streaming interfaces are found from the AudioControl + interface association descriptor. UAC3 devices take the same path. This + does not mean UAC3 is fully supported. +* For each AudioStreaming interface, the filter checks the first alternate + setting that has endpoints. That setting needs an isochronous data endpoint + with a nonzero maximum packet size, optionally followed by an explicit + isochronous feedback IN endpoint. More than two endpoints are rejected. + Interfaces of another subclass in the collection, MIDI for instance, are + skipped. +* One malformed or unsupported streaming interface rejects the whole device, + as does a device without any streaming endpoint. +* ``snd_usb_offload.offload_disable=1`` disables candidate selection. + +Sideband registration needs a valid xHCI slot and a controller with Audio +Sideband capability: xHCI 1.2 or later, extended-property support, and at +least one audio resource. On Intel hosts the device must be connected +directly to a root-hub port; devices behind an external hub are rejected. + +Registration uses the ``XHCI_SIDEBAND_AUDIO`` type, not the vendor sideband. +It does not allocate any endpoint resources; that happens only at +acquisition. + +Asynchronous playback stays on the host +--------------------------------------- + +An asynchronous OUT endpoint runs off the device's own clock, and the sender +has to follow that clock through the feedback the device provides. Whether +the DSP applies that feedback well enough is the host's call: the SOF +firmware spreads samples linearly over the service interval, which is +audible on the kind of equipment that uses asynchronous playback in the +first place: audio interfaces and mixers. Implicit feedback is no better: its +sync source is a capture data endpoint, which the DSP cannot use, so such a +stream would run with no rate tracking at all. + +SOF therefore registers with ``async_playback_on_host`` set, and the library +keeps asynchronous playback on the URB path. The decision is made per +``audioformat``, on the sync type of the data endpoint and its implicit +feedback flag, so it covers explicit and implicit feedback alike and does not +depend on the snd-usb-audio quirks that choose between the two: + +* A playback PCM whose formats all need feedback is left out of the device + identity, so it never appears in the playback route controls. The + library logs ``PCM playback needs clock feedback, not offloaded`` once + per connect. +* Formats that need feedback are skipped during negotiation, so a PCM that + mixes adaptive and asynchronous alternate settings only negotiates the + adaptive ones. +* ``usb_offload_acquire_stream()`` refuses a format that still needs + feedback with ``-EOPNOTSUPP``. + +Capture is not affected by its own sync type: an asynchronous IN endpoint +needs no feedback. It is affected when its data endpoint is the implicit +feedback source of a playback format the host keeps, as with the Behringer +UMC204HD's 24-bit playback: that endpoint is the playback's clock, and once +the DSP owned it the host could not run the playback at all. Such a capture +format stays on the host as well, and the library logs ``PCM capture is +tied to the playback, not offloaded``. A device left with no PCM +to offload in either direction is not reported to SOF at all, the library +logs ``no PCM can be offloaded``. + +``snd_usb_offload.allow_feedback=1`` overrides the host's flag for testing +the feedback path. Explicit feedback is then claimed as described under +`Acquiring the USB endpoints`_; implicit feedback is still left unused. + +From a USB chip to enumerated control items +------------------------------------------- + +The connection sequence is:: + + snd-usb-audio connect_cb(chip) + usb_offload_try_offload() descriptor/speed filter + xhci_sideband_register() controller eligibility + usb_offload_ua_build_id() card/chip/PCM identity + usb_offload_connect() + hda_uaol_notify_connect() + allocate hda_uaol_dev + copy identity and name storage + snd_soc_usb_connect(SOF device, &record->sdev) + append to available_devices + auto-select missing direction routes + notify control INFO / VALUE changes + report SND_JACK_USB + +``usb_offload_ua_build_id()`` walks ``chip->pcm_list``. A USB PCM adds a +playback or capture entry if it has formats for that direction that can be +offloaded (see `Asynchronous playback stays on the host`_). The identity +passed to SOF holds the ALSA card number, the USB chip index, and the PCM +device numbers and names. At most eight PCMs per direction are exported. + +Each entry's display name has this form, with the product name cut to 24 +characters and the PCM name to 16:: + + , | - + +so that several PCMs on one device, and identical devices on different cards, +can be told apart. The library passes the identity by value, and only for the +duration of the callback. ASoC USB, however, keeps pointers to it and links it +into its device list, so SOF copies it, names included, into its own +``hda_uaol_dev`` record. + +There is no control per device. A published port has one *route* per +backend of each direction, and a control per route: + +* ``USB Offload 0 Playback Route``, ``USB Offload 1 Playback Route``, ... +* ``USB Offload 0 Capture Route``, ``USB Offload 1 Capture Route``, ... + +The number in the name is the backend ordinal, the same as in +``UAOL0-Play``: route ``n`` of a direction is what backend ``n`` of that +direction plays to. The number is always present, also for a direction with +a single backend, so the names do not depend on the stream count. The +topologies name the frontend PCMs with the same ordinal, ``USB Offload 0`` +and ``USB Offload 1`` (``Deepbuffer USB Offload `` for the deep buffers), +so that a PCM, its route controls and the ``USB Offload Jack`` share a name +and pair up by number. + +Each is a writable enumerated control on the SOF platform card. Its item list +is rebuilt on every query. ASoC USB lists every available ``(device, USB +PCM)`` pair for that direction, in device-connection order and then PCM +order. A playback-only device adds no capture items, and the reverse. All +routes of a direction list the same items. + +An item number is a position in this live list. It is **not** a card number, +USB PCM number, endpoint number, or sideband resource ID. Hotplug can shift +positions even while the selected device stays the same, so userspace should +re-read the item list when it gets a control notification. + +Selection policy and changes +---------------------------- + +Any route can be pointed at any available pair, including a pair another +route of the direction already points at. A ``(device, USB PCM)`` pair can +only be offloaded once, though: of two routes pointing at the same pair, the +first backend to acquire it gets it and the second fails ``hw_params`` with +``-EBUSY`` until the first releases it. Swapping two devices between two +routes is therefore a plain sequence of two writes, through an intermediate +state where both routes point at the same device. + +When a device connects, ASoC USB walks the routes of each direction in +order. A route with no selection gets its default: the first available pair +no other route of the direction has, or, when every pair is taken, the first +available pair at all. A route that duplicates another route's pair holds +nothing of its own, so it is moved to a pair of the new device no route has +yet, unless a stream is set up on its backend. A route pointing at a pair of +its own is never changed by a connect. With two backends per direction, one +connected device offering one PCM pair puts both routes on it; when a second +device, or a second PCM of the first, appears, route 1 moves to it. Playback +and capture routes are independent of each other. + +When no pair is available, the control has one item, ``None``, and reads as +item zero. ``None`` only marks an empty list. Once devices are available it +disappears, so it cannot be used to turn offload off. + +A write resolves the item number to a device/PCM pair: + +* Writing the pair that is already selected does nothing. +* Writing an item number past the end of the list fails with ``-EINVAL``, + except item zero on an empty list, which does nothing. +* Selecting a different pair fails with ``-EBUSY`` while SOF holds an + acquired stream on that route's backend, from BE ``hw_params`` until + ``hw_free`` or the unplug of the device, whether or not the stream is + running. +* A write which changes the pair while the route's backend is acquiring its + stream is not refused, the backend is not busy yet. The backend reads the + selection back once it holds the stream and, finding it changed, releases + the stream again and fails its ``hw_params`` (or ``prepare`` after a + system suspend) with ``-EBUSY``. +* Otherwise the new pair becomes the selection. The next acquisition uses it. + The write itself allocates no USB or xHCI resources. + +A successful selection does not reserve any xHCI resource. + +On disconnect, the device's entries are removed. Every route that pointed at +the device is cleared, and then every empty route gets its default again, if +any pair is left. Connect and disconnect send +``SNDRV_CTL_EVENT_MASK_INFO`` to every route control of the direction and +add ``SNDRV_CTL_EVENT_MASK_VALUE`` where the selection changed. A stream +already acquired on the removed device is not moved to the new selection; see +`Physical disconnect`_. + +Probe ordering and rediscovery +------------------------------ + +USB enumeration, DSP startup, and machine-card binding need not occur in a +fixed order. Two rediscovery points close the gaps: + +1. Registering with the library installs the USB platform callbacks and + re-emits connect callbacks for USB chips that already exist. +2. Port publication calls ``snd_usb_rediscover_devices()`` again, after the + SOF card has somewhere to route them. + +If the SOF port does not exist yet, ``hda_uaol_notify_connect()`` publishes +nothing but still returns success. The library keeps its record of the +device and notifies SOF again on the next rediscovery. Both the library and +SOF ignore devices they already know, so rediscovery does not create +duplicate entries. + +PCM negotiation and offload acquisition +======================================= + +Negotiation rules +----------------- + +What the topology allows on a frontend is not necessarily what the selected +USB device can play. When a frontend that routes to a UAOL backend is opened, +``hda_apply_constraint_from_be()`` adds hardware rules derived from that +backend. + +There are three sets of rules, each covering rate, channels, and format: + +* **Link FIFO rules** limit the byte rate to what fits in the stream's share + of the UAOL FIFO. They apply even when no USB device is selected. The + service interval is not known yet, so they assume the shortest one, + 125 us, and the real fit is checked after the acquisition (see + `Acquiring the USB endpoints`_). +* **USB device rules** limit the parameters to the selected USB PCM's + ``audioformat`` list, leaving out formats that need clock feedback (see + `Asynchronous playback stays on the host`_). They look up the current + selection each time + they run, so a route change or hotplug after open is taken into account. + Each rule also depends on the parameter it refines, so a request which + changes only that one is checked as well. +* **Shared backend rules** apply to the second frontend of a backend, the + deep buffer. They allow only the rate, channels, and format the backend + was opened with, and allow everything while the backend is idle. + +The rules depend on each other: choosing a channel count, for example, can +narrow the allowed rates and formats. The USB rules produce ranges for rate +and channels and a mask for format; they do not reproduce gaps in a device's +discrete rate list. Acquisition then does an exact format lookup and can +still reject a combination that passed negotiation. + +SOF's pipeline represents 24-bit audio only as ``S24_LE`` (24 valid bits in +a 4-byte container), while many USB devices pack 24-bit audio in 3 bytes +(``S24_3LE``). SOF therefore sets ``s24_3le_as_s24_le`` when registering with +the library, and devices offering ``S24_3LE`` are also offered as ``S24_LE`` during +negotiation. Just before acquisition, ``usb_offload_resolve_format()`` keeps +``S24_LE`` if the device supports it at the chosen rate and channel count. +Otherwise it rewrites the BE parameters to ``S24_3LE``, and the DSP packs the +samples on their way to the link. DPCM hands the BE ``hw_params`` a copy of +the parameters, while the common prepare programs the link DMA again after a +stop from the BE's own copy, so ``uaol_dai_prepare()`` resolves that copy the +same way; otherwise a stream recovering from an xrun would run its link DMA +with the 4-byte container against the 3-byte one of the device. + +Acquiring the USB endpoints +--------------------------- + +``uaol_dai_hw_params()`` acquires the USB side first, through +``uaol_dai_start_stream()``, and then runs the common link DMA ``hw_params`` +setup also used for SSP and DMIC. A backend fed by two frontends, the +playback PCM and its deep buffer, runs ``hw_params`` once per frontend while +the first one has not started yet; the USB side is acquired by the first call +only and kept until the backend's ``hw_free``, which DPCM runs when the last +frontend lets go. + +The two frontends share one endpoint and one mixer, so the second one can +only run what the first one opened the backend with. The backend stores the +rate, channels, and format of the first call and refuses a later one that +differs with ``-EBUSY``, before the core prepares anything for it. A +frontend failing further down would otherwise take the widgets the first one +prepared with it. The shared backend rules above keep this from being +reached in the normal case. + +1. Map the selected ASoC USB device back to its ``usb_device`` and the + selected USB PCM number. If nothing is selected, or the library is not + running, this fails with ``-ENODEV``. +2. Resolve the 24-bit container (see above) and call + ``usb_offload_acquire_stream()`` with the direction, USB PCM number, and + BE parameters. +3. The library looks only at the selected USB PCM for a format that exactly + matches. If there is none, or the match needs clock feedback the host + keeps to itself, this fails with ``-EOPNOTSUPP``. A USB PCM is + offloaded once, so a PCM already acquired fails with ``-EBUSY``; the other + PCMs of the same device are not affected and can be acquired alongside. +4. Claim the USB substream with ``snd_usb_offload_claim_substream()``. This + uses the same ``subs->opened`` flag, under ``chip->mutex``, as opening the + USB card's PCM, so only one of the two can own the substream. +5. Resume the USB device, call ``snd_usb_hw_params()``, claim the data + endpoint and an explicit feedback endpoint with + ``snd_usb_endpoint_claim_offload()``, and prepare the sync endpoint, if + any, and the data endpoint. This selects the streaming alternate setting, + which makes the xHCI driver set up the endpoint. No URBs are submitted: + the host does not stream the data itself. A claimed endpoint cannot be + opened by another stream, the host's ``hw_params`` fails with ``-EBUSY``. + An endpoint which another stream has open already cannot be claimed and + the acquisition fails with ``-EBUSY``: with implicit feedback the capture + data endpoint is also the sync source of the device's playback. A freshly + prepared data endpoint gets the device's format quirk run, as + ``snd_usb_pcm_prepare()`` does, which sets the sample rate of devices that + take it through vendor requests. A quirk which leaves the host to drop + bytes from the stream or its packets to align the samples, as for the + MacroSilicon MS2100/MS2109 capture cards, fails the acquisition with + ``-EOPNOTSUPP``: only the URB path does that. +6. Reserve an Audio Sideband resource for the data endpoint and attach it + with ``xhci_sideband_add_endpoint()``. From now on the controller routes + this endpoint's data over UAOL. Which resources the endpoint may use is + SOF's choice, see `Resource IDs, UAOL stream indices, and DMA tags`_. +7. If the stream has an explicit feedback endpoint, reserve and attach a + resource for it too. An implicit-feedback source is **not** claimed: it is + another interface's audio data endpoint, not a feedback value the DSP can + use. +8. Query the xHCI endpoint properties and return them in + ``usb_offload_stream_hw``: the resource IDs actually assigned, the xHCI + controller's PCI bus/device/function, the slot ID, endpoint addresses, + maximum packet sizes, intervals, speed, whether a transaction translator + is used, and the USB PCM number the stream was acquired on, which names + the stream to ``usb_offload_quiesce_stream()``, + ``usb_offload_restart_stream()`` and ``usb_offload_release_stream()`` + together with the direction. +9. Back in SOF, check that the payload of two service intervals, rounded to + whole sample frames, fits in the stream's share of the link FIFO. The + interval is the one the library reports. A payload exactly as large as the + share fits; a larger one is refused with ``-EINVAL``, as an overrun is not + reported anywhere and only corrupts the audio. +10. Fill in the runtime fields of the UAOL gateway configuration, allocate + the feedback link DMA if needed, and record the acquired device and BE + substream. +11. Allocate the audio link DMA and add its IPC4 DMA configuration TLV. + +If any step fails, the steps already done are undone, including the USB +claim, and a failure in step 11 releases the USB side as well. On success, +the claim and the USB runtime-PM reference are held until the stream is +released. + +Resource IDs, UAOL stream indices, and DMA tags +----------------------------------------------- + +Several kinds of index are involved. They are independent of each other and +must not be mixed up: + +.. list-table:: + :header-rows: 1 + :widths: 35 65 + + * - Identifier + - Meaning + * - USB card / USB PCM number + - ALSA identity of the device, used by route selection. + * - USB endpoint address / xHCI slot ID + - The endpoint and its device, as the xHCI controller knows them. + * - xHCI Audio Sideband resource ID + - Allocated at acquisition. On Intel hosts, IDs 1--2 are for OUT + endpoints and IDs 3--6 for IN endpoints; SOF narrows these further + by function, see below. + * - UAOL stream index + - Sideband resource ID minus one. + * - Gateway node index + - The HDA link stream that hardware ties to a UAOL stream: + UAOL streams 0--5 map to link streams 13, 14, 15, 16, 29, 30. + * - HDA link DMA stream tag + - Allocated separately for the audio and feedback link DMAs and passed + to the firmware in DMA configuration TLVs. + +Resource IDs come from one allocator per xHCI controller, shared by all +devices on it, and are assigned when a stream is acquired. The firmware ties +the link's streams to functions through its FIFO layout: the ``T`` output +streams carry playback, the first ``I - T`` input streams carry capture, and +the last ``T`` input streams carry the clock feedback of the playback +streams. An endpoint on a stream outside its function's range would be +served from the wrong FIFO area. SOF therefore answers the library's +``get_resource_range`` callback with the resources of the endpoint's +function, and the library passes that range to +``xhci_sideband_alloc_resource_id()``, which reserves the first free one of +it; when all are taken the acquisition fails with ``-EBUSY``. With two output and four input streams this places +playback on resources 1--2, capture on 3--4, and feedback on 5--6. + +Within a range, neither the order in which devices were connected nor their +position in the route control predicts which ID a stream gets. SOF therefore +rebuilds the gateway binding from the values returned by each acquisition. + +``xhci_sideband_add_endpoint()`` issues the ``PROP_ECI_AUDIO`` / +``AUDIO_SUBTYPE_SET_RES`` extended-property command for the endpoint. This +command is what routes the endpoint to its sideband resource. Registering +the device or selecting a route does not. + +Firmware configuration and link DMA +=================================== + +Two-stage gateway configuration +------------------------------- + +The UAOL DAI copier's gateway configuration is filled in two stages, because +part of it depends on which USB device the stream ends up on. + +1. At topology load, ``sof_ipc4_widget_setup_uaol()`` allocates a + ``sof_ipc4_uaol_configuration_blob`` and fills in what does not depend on + the device: the link index, the FIFO layout, the timestamp conversion, and + all TLV headers. +2. At every acquisition, ``uaol_dai_set_endpoint()`` fills in the rest from + the ``usb_offload_stream_hw`` the library returned. + +The blob is a plain list of type/length/value records; lengths are in bytes. +All seven TLVs are always present, with the feedback fields zeroed when there +is no feedback endpoint: + +.. list-table:: + :header-rows: 1 + :widths: 10 30 60 + + * - Type + - Value + - Source + * - 0 + - xHCI bus/device/function + - PCI address of the xHCI controller, from the endpoint properties. + * - 1 + - Link/stream binding + - Topology link index; data resource ID minus one; if present, the + feedback resource ID minus one and the feedback endpoint's interval. + * - 2 + - FIFO start offsets + - Computed from the FIFO sizes and stream counts reported by the + firmware (see below). + * - 3 + - Data endpoint information + - Direction, endpoint number, slot ID, split/high-speed flags, maximum + packet size. + * - 4 + - Feedback endpoint information + - Same layout for the feedback endpoint, or zero if there is none. + * - 5 + - ART conversion + - Multiplier and divider both 1: the xHCI global time is already + counted in DSP timer ticks. + * - 6 + - Service interval, microseconds + - Derived from the data endpoint's ``bInterval``. + +For a high-speed data endpoint the service interval is +``125 * 2^(bInterval - 1)`` microseconds, for a full-speed one +``1000 * 2^(bInterval - 1)``: an isochronous ``bInterval`` is an exponent at +both speeds. ``bInterval`` is first clamped to 1--16. + +The copier's gateway node index is the link stream mapped from the data +resource ID (see the table above), not the audio link DMA's stream tag. When +the copier is created, the common IPC4 code appends the DMA configuration +TLVs to this blob and sends the result to the firmware. + +FIFO partitioning and constraints +--------------------------------- + +The UAOL link has one transmit and one receive FIFO, shared by all of its +streams. Which stream slot a USB endpoint uses follows from the sideband +resource ID it gets at acquisition. Even a single active stream can land on +any slot, so the FIFOs are divided among all the streams the hardware has, +not just the ones the topology uses. + +The firmware reports ``T`` output (transmit) streams and ``I`` input (receive) +streams. The receive FIFO also carries one eight-byte clock-feedback word per +transmit stream, at its start, and those feedback channels are counted in +``I``. The layout is:: + + audio_receive_streams = I - T + tx_size = round_down(max_tx_fifo_size / T, 8) + rx_base = T * 8 + rx_size = round_down((max_rx_fifo_size - rx_base) / + audio_receive_streams, 8) + +All start offsets are multiples of the eight-byte bus width. A link with no +transmit streams, or no receive streams left for audio, is rejected. + +A stream's share must hold two service intervals of audio, so that the DSP +can refill it while the link drains it. Samples occupy their valid width in +the FIFO, rounded up to whole bytes, not their container size. The +requirement is:: + + 2 * rate * channels * sample_bytes * service_interval_seconds + <= per_stream_fifo_size + +The service interval is only known once a device's alternate setting has +been chosen, which happens after negotiation. The frontend rules therefore +assume the shortest possible interval, 125 microseconds:: + + rate * channels * sample_bytes <= (fifo_size / 2) * 8000 + +This is only a coarse filter: with a longer service interval, a combination +that passes it can still overflow the FIFO. SOF does not repeat the check +with the real interval at acquisition. An overflow is not reported anywhere; +the audio is simply corrupted. Keep this in mind when a combination that +negotiated successfully produces corrupted audio. + +Audio and explicit-feedback DMAs +-------------------------------- + +UAOL uses the same IPC4 link DMA setup as SSP and DMIC, with a UAOL-specific +lookup of the multilink. The audio link DMA is described in DMA TLV slot +zero: HDA DMA method, preallocated by the host, ``dma_channel_id = tag - 1``, +and ``stream_id = tag``. + +A playback stream with an explicit feedback endpoint needs a second link DMA, +in the capture direction, to bring the feedback values to the DSP. It is part +of the playback stream, not a capture stream of its own: + +* It carries one 32-bit (``S32_LE``) mono value at the stream's rate. +* It is described in DMA TLV slot one; the firmware treats the second DMA as + the feedback channel. +* It is not the DAI's DMA data and is not registered as a ``link_substream``, + so the common code does not handle it. The UAOL DAI starts, stops, and + frees it itself. +* When it is freed, its TLV is cleared, so that a later device without a + feedback endpoint does not inherit a stale feedback DMA. + +Both link DMAs come from the shared HDA link-stream allocator. On ACE2+ it +avoids two hardware errata: a SoundWire stream cannot share a stream index +with an HDA, iDisp, or UAOL stream of the opposite direction, and a playback +stream index once used by HDA or iDisp cannot be used for another link type +until the controller is reset. Free sideband resources therefore do not +guarantee that the link DMAs can be allocated. + +Trigger, teardown, and power transitions +======================================== + +Start and pause +--------------- + +On ``START`` and ``PAUSE_RELEASE``, the UAOL DAI starts the feedback link DMA, +if there is one, and then runs the common HDA DAI trigger. That starts the +audio link DMA and sets the IPC4 pipeline to running. + +On ``PAUSE_PUSH``, the common trigger pauses the DSP pipeline and stops the +audio link DMA, and the UAOL DAI also stops the feedback link DMA. Unlike +``STOP`` and ``SUSPEND``, pausing leaves the USB endpoints running and keeps +all resources. + +Stop ordering is a hardware requirement +--------------------------------------- + +On ``STOP`` and ``SUSPEND`` the steps run in this order:: + + common HDA DAI trigger: + pause the DSP pipeline (the DSP stops its UAOL stream) + stop the audio link DMA, common link DMA cleanup + usb_offload_quiesce_stream(): + stop the xHCI feedback endpoint, if any + stop the xHCI data endpoint + stop the feedback link DMA + ... later: DAI copier freed, UAOL link powered down ... + hw_free: + free the audio link DMA + free the feedback link DMA, clear its TLV + detach endpoints and free sideband resources, USB hw_free, + drop the USB runtime-PM reference, release the substream claim + +The xHCI endpoints must be stopped in a narrow window: + +* **Not before the DSP has stopped.** The DSP cannot bring its UAOL stream to + idle while the xHCI controller no longer services an endpoint still bound + to the stream, and the stop times out. An endpoint already detached from + its resource, as on a disconnect, does not hold the stop up. +* **Not as late as hw_free.** By then the DAI copier has been freed and the + UAOL link powered down. An IN endpoint the controller still services, in + particular the feedback endpoint of a playback stream, has nowhere to + deliver its data and can lock up the controller. + +``usb_offload_quiesce_stream()`` only stops the endpoints. It keeps the +sideband resources and the substream claim, and +``usb_offload_release_stream()`` must still follow. Release detaches the +feedback endpoint before the data endpoint, frees their resource IDs, then +runs USB hw_free, drops the runtime-PM reference, and releases the claim. + +A stream can also be started again without ``hw_free`` in between, which is +how an xrun is recovered from: ``STOP``, ``prepare``, ``START``. A stopped +xHCI endpoint only runs again once its doorbell is rung, and starting the DSP +side of the link does not do that, so the ``START`` trigger of a quiesced +stream first restarts the endpoints with ``usb_offload_restart_stream()``: +the data endpoint, then the feedback endpoint. They then run before the DSP +starts its UAOL stream, as they do on the first start. A release forgets +that the endpoints were quiesced; after a system suspend the stream is +acquired again with fresh endpoints instead. + +Quiesce logs a failure to stop an endpoint only at debug level and still +returns success, and the trigger does not check its return value. A +successful stop therefore does not prove that the endpoints are idle. + +System suspend and resume +------------------------- + +System suspend frees the DSP pipeline and DAI copier, and resets the USB +device's streaming alternate setting, while the application keeps the PCM +open. The ``SUSPEND`` trigger records that the direction has to be acquired +again. ALSA only triggers a running stream, so a stream which is paused, or +not running, when the system suspends is not acquired again. + +On resume the backend gets only ``prepare``, not ``hw_params``. So +``uaol_dai_prepare()`` releases the old acquisition, acquires again with the +saved BE parameters, which rebuilds the endpoint binding, the gateway +configuration, and the feedback link DMA, and then runs the common prepare. +The resource IDs may differ from before the suspend. The widget stays +prepared over the suspend, so its IPC payload is not built again; the copier +setup refreshes the gateway configuration in it from the one the DAI +rewrote, along with the node ID and the DMA configuration TLVs. + +The flag is only cleared once the stream is acquired again, a failed +``prepare`` tries again the next time. A backend without a device, because +it was unplugged, fails ``prepare`` with ``-ENODEV`` rather than starting the +DSP on a gateway whose resources are gone. If the stream is closed while +suspended, the flag is cleared so that nothing is acquired again later. +Audio does not keep flowing through system suspend: the stream stops, and is +set up again when the PCM is prepared after resume. + +Jack semantics, unplug, and lifetime +==================================== + +What the jack means +------------------- + +SOF creates one card-wide jack, ``USB Offload Jack``, of type +``SND_JACK_USB``, and reports it directly with ``snd_jack_report()``. It does +not use the generic ``snd_soc_usb_setup_offload_jack()`` helper, which would +also hand the jack to the component through ``set_jack``. + +The jack answers one question: **is there at least one USB device that +offload can be routed to?** + +* It is reported plugged when a device is published to the SOF port. +* On disconnect it stays plugged as long as SOF still knows of another + device, and is reported unplugged when the last one goes away. +* A playback-only or a capture-only device is enough to plug it. +* It does not say which device is selected, whether a stream is running, + whether both directions are available, or whether acquisition will succeed. + +It is not the headset's own headphone or microphone jack, and there is no +jack per device. It switches no DAPM pins and moves no streams. Userspace +needs the route controls, and PCM negotiation, to know what it can play. + +With a working port and no offloadable device, both route controls show +``None`` and the jack is unplugged. A device rejected by the candidate filter +or by sideband registration, or one whose PCMs all stay with the host, never +plugs the jack. If the library failed to +probe altogether, the UAOL DAI still creates the jack, permanently unplugged, +but no route controls. + +Physical disconnect +------------------- + +Unplugging a device that has an acquired stream does not follow the orderly +stop sequence above. The USB side goes first, while the DSP may still be +running: + +1. snd-usb-audio calls the bridge's disconnect callback before marking the + chip as shut down. The bridge detaches the endpoints from their sideband + resources, frees the resources, and releases the USB substream. +2. The bridge then notifies SOF. ``hda_uaol_stop_frontends()`` finds the BE + substreams acquired on this device and, under the BE's DPCM lock, sets + every connected FE to ``SNDRV_PCM_STATE_DISCONNECTED``, stopping it first + if it is running. Without this the application would keep writing to a + PCM whose USB device no longer exists. The BE then forgets the device: + the USB side is already released and the ``usb_device`` goes away with + the disconnect, so the FE's later stop, ``hw_free``, and close do not + hand its stale pointer back to the bridge, where a newly plugged device + reusing the address could match. The feedback link DMA stays with the BE + until its ``hw_free``. +3. SOF removes its device record from ASoC USB, which drops the device's + route items and selects another device if one is available. The jack is + then updated from the remaining devices. +4. Finally the bridge unregisters the device's sideband handle. + +Card and library removal +------------------------ + +When the UAOL DAI is removed, SOF clears its port and jack pointers under its +UAOL mutex, removes and frees the port, and then frees the device records it +still holds. The jack belongs to the sound card and is freed with it. + +``usb_offload_unregister()`` lets snd-usb-audio run the disconnect callback +for every connected device and unregister the USB platform callbacks, both +under its ``register_mutex`` with ``snd_usb_release_platform_ops()``. The +bridge's device records are released and the host is told of the +disconnects as on an unplug, and no device can be freed under a record in +the meantime. Once it returns no callback can be running or start, and the +context is freed. + +Synchronization model +===================== + +The implementation has several distinct locks rather than one global stack +lock: + +* USB-audio ``register_mutex`` serializes platform-callback registration, + rediscovery, and USB-chip callbacks. +* ``usb_offload_ua_lock`` protects the active library context, per-device + bridge records, and USB-side stream acquisition/release. The library has + no lock of its own: the host callbacks are dispatched from the bridge. +* SOF ``uaol_mutex`` protects its published-port/device-record bookkeeping. +* ASoC USB ``ctx_mutex`` protects ports, available-device lists, selections, + and route-control operations. +* ``chip->mutex`` decides who owns a USB substream: the USB card's own PCM or + the offload path. +* The backend DPCM mutex stabilizes frontend/backend associations during + disconnect, with PCM stream locks used to change frontend state. +* The HDA bus register lock protects link-DMA allocator masks; each sideband + handle has its own mutex for sideband operations. + +Registration and rediscovery are never done while holding the library's +controller lock, and the bridge does not hold ``usb_offload_ua_lock`` while +it registers or unregisters with snd-usb-audio. The disconnect path drops +``usb_offload_ua_lock`` while it notifies SOF, because stopping the frontends +triggers the BE, which calls back into the library. Keep these rules in mind +when adding callbacks: a hotplug notification runs with locks held and can +re-enter other parts of the stack. + +Current limits and troubleshooting +================================== + +Current limits: + +* One usable UAOL link, with two streams per direction. A ``(device, USB + PCM)`` pair is offloaded once: the two backends of a direction need two + devices, or a device with two PCM pairs, to both run. +* One set of USB platform callbacks system-wide, and one active library + context, so one registered host. There is no mapping from xHCI + controllers to UAOL links yet: every eligible device on any controller is + published to the one host. +* On Intel hosts the xHCI controller has six Audio Sideband resources: two + for OUT and four for IN endpoints, of which SOF uses two for capture and + two for clock feedback. An explicit feedback endpoint uses one of the + feedback resources plus a capture-direction link DMA. +* The candidate filter is deliberately strict but only checks one alternate + setting per interface at connect time. A device that appears in the route + controls is not guaranteed to work in every format. +* SOF keeps asynchronous playback on the host unless ``allow_feedback`` is + set; see `Asynchronous playback stays on the host`_. +* Implicit feedback is not supported; such a sync source is left unused even + with ``allow_feedback``. Without it, the playback and the capture it syncs + to both stay on the host. With it, the two directions cannot be split, as + the capture data endpoint is also the sync source of the playback: while + the capture is offloaded, the device's playback cannot be opened on the + host, nor offloaded, and the other way around. A capture format with + implicit feedback of its own, which snd-usb-audio gives to a device that + only captures while its playback endpoint streams (the Behringer FCA1616), + always stays on the host: the DSP only gets the capture endpoint. + +When bring-up fails, work through the stack from the bottom up: + +1. **No UAOL backend:** check the Kconfig options, HDA multilink discovery, + the firmware's UAOL capability messages, the final ``uaol_link_mask``, and + that the topology's DAI names match the backends. +2. **No route controls:** check that SOF registered with the library and + that the UAOL DAI is bound in the card. A jack without route controls + means the library is not running. +3. **Route controls show only None:** check that the platform callbacks are + registered, the candidate-filter debug messages, the speed and + direct-connection rules, the controller's sideband capability, and the + rediscovery after the card registered. A playback route that stays + ``None`` while the capture route lists the device points at an + asynchronous playback endpoint; the library's debug message names the + PCM. +4. **Route selected but hw_params fails:** check the selected direction and + USB PCM, whether the USB card's own PCM is open, exact format support, + free sideband resources, and free link DMAs, including for feedback. +5. **Corrupted audio with accepted parameters:** check sample packing, the + device's real service interval against the FIFO share (negotiation + assumes 125 microseconds), and the endpoint fields in the gateway + configuration. +6. **Stop timeout or xHCI controller hang:** check the stop order: DSP + pipeline, then xHCI endpoints including feedback, then copier free and + link power-down. +7. **Failure after resume:** check that ``prepare`` acquired the stream again + and rebuilt the bindings, rather than reusing the old resource IDs. + +The route controls and the jack are card-interface controls, so ``amixer`` +needs ``iface=CARD``. With ``$SOF_CARD`` set to the SOF card number:: + + amixer -c "$SOF_CARD" controls | grep 'USB Offload' + amixer -c "$SOF_CARD" cget iface=CARD,name='USB Offload 0 Playback Route' + amixer -c "$SOF_CARD" cget iface=CARD,name='USB Offload 0 Capture Route' + amixer -c "$SOF_CARD" cget iface=CARD,name='USB Offload Jack' + +To point the first playback backend at item zero of the current list, and +the second at item one:: + + amixer -c "$SOF_CARD" cset iface=CARD,name='USB Offload 0 Playback Route' 0 + amixer -c "$SOF_CARD" cset iface=CARD,name='USB Offload 1 Playback Route' 1 + +To exercise offload, open the SOF frontend PCM that the topology routes to +that UAOL backend, not the USB card's PCM. Item numbers change with hotplug, +so do not store them as device identities; re-read the list instead. + +Source map +========== + +The following paths are relative to the kernel source root: + +.. list-table:: + :header-rows: 1 + :widths: 55 45 + + * - Source + - Primary responsibility + * - :file:`include/sound/usb-offload.h` + - Library API, host callbacks, device and stream-resource identities. + * - :file:`sound/usb/offload/init.c` + - Host registration, context lifecycle and hotplug dispatch. + * - :file:`sound/usb/offload/usb-audio.c` + - Candidate filter, platform-ops bridge, format refinement, resource + acquisition, quiesce, restart, release. + * - :file:`sound/usb/card.c`, :file:`sound/usb/pcm.c` + - USB platform callbacks and rediscovery; arbitration of substream + ownership between the USB card's PCM and offload. + * - :file:`sound/soc/soc-usb.c`, :file:`include/sound/soc-usb.h` + - Port lookup, available devices, route lists, controls and policy. + * - :file:`sound/soc/sof/intel/hda.c` + - Discovery, firmware gates, library callbacks, machine registration. + * - :file:`sound/soc/sof/intel/hda-uaol.c` + - SOF records, port, jack, constraints, endpoint-to-gateway binding, + backend lifecycle and disconnect handling. + * - :file:`sound/soc/sof/intel/hda-dai.c`, + :file:`sound/soc/sof/intel/hda-dai-ops.c` + - The DAI callbacks the UAOL DAI builds on, link DMA allocation, + feedback DMA, IPC4 trigger machinery. + * - :file:`sound/soc/sof/intel/hda-pcm.c` + - Applying backend-derived constraints during frontend open. + * - :file:`sound/soc/sof/intel/hda-mlink.c`, + :file:`sound/soc/sof/intel/lnl.c` + - UAOL multilink lookup and alternate-link offload enablement. + * - :file:`sound/soc/sof/ipc4-loader.c` + - Firmware UAOL support/capability parsing. + * - :file:`sound/soc/sof/ipc4-topology.c`, + :file:`sound/soc/sof/ipc4-topology.h` + - Gateway TLV layout, FIFO partitioning and copier IPC assembly. + * - :file:`sound/soc/intel/boards/sof_board_helpers.c`, + :file:`sound/soc/intel/boards/skl_hda_dsp_generic.c`, + :file:`sound/soc/intel/boards/sof_sdw.c`, + :file:`sound/soc/intel/boards/sof_uaol_common.h` + - Machine backend links and shared naming. + * - :file:`drivers/usb/host/xhci-sideband.c`, + :file:`include/linux/usb/xhci-sideband.h` + - Controller eligibility, sideband resource reservation, endpoint + binding/properties, and endpoint stop. diff --git a/Documentation/sound/soc/usb.rst b/Documentation/sound/soc/usb.rst index 9cb860547f22d6..86a28d690f53e5 100644 --- a/Documentation/sound/soc/usb.rst +++ b/Documentation/sound/soc/usb.rst @@ -57,12 +57,20 @@ Structures - ``connection_status_cb``: callback to notify connection events - ``update_offload_route_info``: callback to fetch selected USB sound card/PCM device + - ``is_route_busy``: callback reporting whether the backend has a stream open + on a route, consulted before a route selection is changed + - ``available_devices``: the connected ``snd_soc_usb_device`` entries + - ``routes``, ``num_routes``: the offload routes of each direction, see + `Platform Card Route Kcontrols`_ - ``priv_data``: driver data The snd_soc_usb structure can be referenced using the ASoC platform card device, or a USB device (udev->dev). This is created by the ASoC BE DAI link, and the USB sound entity will be able to pass information to the -ASoC BE DAI link using this structure. +ASoC BE DAI link using this structure. On device tree platforms the USB +device is tied to the port through the ``usb-soc-be`` phandle; without a +device tree node the port is found by the device its component was +registered with, so the caller passes that device. ``struct snd_soc_usb_device`` @@ -70,9 +78,12 @@ ASoC BE DAI link using this structure. - ``chip_idx``: USB sound chip array index - ``cpcm_idx``: capture pcm device indexes associated with the USB sound device - ``ppcm_idx``: playback pcm device indexes associated with the USB sound device + - ``cpcm_names``, ``ppcm_names``: optional human readable names of the PCMs, + parallel to the index arrays, shown as the items of the route kcontrols - ``num_playback``: number of playback streams - ``num_capture``: number of capture streams - - ``list``: list head for the USB sound device list + - ``list``: list head for the backend's own use + - ``available_node``: list head on ``available_devices``, owned by SoC USB The struct snd_soc_usb_device is created by the USB sound offload driver. This will carry basic parameters/limitations that will be used to @@ -106,9 +117,14 @@ Returns 0 on success, and -EOPNOTSUPP on failure. **snd_soc_usb_connect()** notifies the ASoC USB DPCM BE DAI link of a USB audio device detection. This can be utilized in the BE DAI driver to keep track of available USB audio devices. This is intended -to be called by the USB offload driver residing in USB SND. +to be called by the USB offload driver residing in USB SND. The device is +added to ``available_devices`` and every route of either direction that has +no selection yet is pointed at its first PCM no other route has. -Returns 0 on success, negative error code on failure. +Returns 0 on success, -ENODEV when no port is registered for ``usbdev`` (the +device is then not listed and must not be reported as disconnected later), +-EEXIST when ``sdev`` is connected already, negative error code on other +failures. .. code-block:: rst @@ -120,7 +136,8 @@ Returns 0 on success, negative error code on failure. **snd_soc_usb_disconnect()** notifies the ASoC USB DPCM BE DAI link of a USB audio device removal. This is intended to be called by the USB offload -driver that resides in USB SND. +driver that resides in USB SND. Every route that pointed at the device is +cleared and given a new default selection, if another device is available. .. code-block:: rst @@ -184,10 +201,64 @@ Returns 0 on success, negative otherwise. - ``data``: private data **snd_soc_usb_allocate_port()** allocates a SoC USB device and populates standard -parameters that is used for further operations. +parameters that is used for further operations. The port starts out with one +offload route per direction. Returns a pointer to struct soc_usb on success, negative on error. +.. code-block:: rst + + int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes); +.. + + - ``usb``: SoC USB device, not yet added with snd_soc_usb_add_port() + - ``direction``: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + - ``num_routes``: streams the backend can offload at a time in ``direction`` + +**snd_soc_usb_set_num_routes()** gives a backend which runs several offloaded +streams of a direction at a time a route, and with +snd_soc_usb_setup_offload_route_ctl() a kcontrol, per stream, so that each can +be pointed at a (device, PCM) pair of its own. To be called before the route +kcontrols are created and the port is added. + +Returns 0 on success, negative on error. + +.. code-block:: rst + + int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb); +.. + + - ``component``: DPCM BE DAI link component + - ``usb``: SoC USB device to attach the controls to + +**snd_soc_usb_setup_offload_route_ctl()** creates one writable enumerated +kcontrol per route of each direction on the platform sound card, see +`Platform Card Route Kcontrols`_. + +Returns 0 on success, negative on error. + +.. code-block:: rst + + int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index); +.. + + - ``usb``: SoC USB device + - ``direction``: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + - ``route``: the route of ``direction`` + - ``sdev``: set to the routed device, NULL if nothing is selected + - ``pcm_index``: set to the routed USB SND PCM device index + +**snd_soc_usb_get_selected_device()** returns the (device, PCM) pair a route +currently points at, for the backend to consult when it starts a stream +instead of picking a device itself. + +Returns 0 on success (also when nothing is selected), negative on error. + .. code-block:: rst void snd_soc_usb_free_port(struct snd_soc_usb *usb); @@ -339,9 +410,17 @@ USB Offload Related Kcontrols Details ------- A set of kcontrols can be utilized by applications to help select the proper sound -devices to enable USB audio offloading. SoC USB exposes the get_offload_dev() -callback that designs can use to ensure that the proper indices are returned to the -application. +devices to enable USB audio offloading. Two models are available: read-only +controls on the USB sound card reporting where a USB PCM is offloaded to +(`USB Sound Card Route Kcontrols`_), and writable controls on the platform +sound card selecting which USB device an offloaded stream goes to +(`Platform Card Route Kcontrols`_). A backend uses the one that fits how +its DSP is driven; the two can coexist. + +USB Sound Card Route Kcontrols +------------------------------ +SoC USB exposes the get_offload_dev() callback that designs can use to ensure +that the proper indices are returned to the application. Implementation -------------- @@ -456,6 +535,50 @@ execute the registered function calls to the DPCM BE DAI link. ret = snd_soc_usb_add_port(usb); .. +Platform Card Route Kcontrols +----------------------------- +A backend whose offloaded PCMs exist on the platform card whether or not a +USB device is connected, and which can offload any connected device, lets +the user choose the device instead: it has a *route* per stream it can +offload at a time in a direction (see snd_soc_usb_set_num_routes()), and +snd_soc_usb_setup_offload_route_ctl() creates a writable enumerated kcontrol +per route on the platform card: + + ``USB Offload Playback Route`` + + ``USB Offload Capture Route`` + +```` is the route and is always present, also with a single route per +direction, so the names do not depend on how many streams a backend runs. +A backend pairs its PCMs with the controls by naming them the same way, +``USB Offload ``. + +The items of a control are every (device, PCM) pair of the direction over +all connected devices, in connection order and then PCM order, named by the +backend through ``ppcm_names``/``cpcm_names``; a name should start with the +USB card and PCM numbers, ``,``, so that a sound server can match +the device it targets. The item list is rebuilt on every query, so it +always reflects what is connected; with nothing connected the single item is +``None``. + +Selection rules: + + - On connect, a route with no selection gets the first pair no other route + of the direction has, so that the routes spread over what is connected. + A route pointing at a pair another route also has is moved to a pair of + the new device no route has yet, unless a stream is running on it. + - On disconnect, routes pointing at the device are cleared and selected + again by the same rule from what remains. + - A write is refused with ``-EBUSY`` while the backend reports the route + busy through ``is_route_busy()``, i.e. while a stream is open on it. + Writing the current selection is a no-op. A selection by itself claims + no USB or DSP resource; the backend reads it with + snd_soc_usb_get_selected_device() when a stream starts. + +Control ``INFO`` and ``VALUE`` change notifications are sent whenever a +connect, a disconnect or a write changes the item list or the selection. +For a backend using this model see :doc:`sof-uaol`. + Existing USB Sound Kcontrol --------------------------- With the introduction of USB offload support, the above USB offload kcontrol diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c index af8d4b74c4ba0c..fce5fefdfed0e8 100644 --- a/drivers/usb/host/xhci-mem.c +++ b/drivers/usb/host/xhci-mem.c @@ -1933,6 +1933,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci) cancel_delayed_work_sync(&xhci->cmd_timer); + xa_destroy(&xhci->sideband_idr.id_xa); + for (i = 0; xhci->interrupters && i < xhci->max_interrupters; i++) { if (xhci->interrupters[i]) { xhci_remove_interrupter(xhci, xhci->interrupters[i]); @@ -2416,6 +2418,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Starting %s", __func__); + xa_init_flags(&xhci->sideband_idr.id_xa, XA_FLAGS_ALLOC); + xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Allocating internal virtual device array"); xhci->devs = kcalloc_node(xhci->max_slots + 1, sizeof(*xhci->devs), flags, dev_to_node(dev)); diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index c23434001e9cbf..5291565706af1b 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -1926,6 +1926,12 @@ static void handle_cmd_completion(struct xhci_hcd *xhci, break; case TRB_GET_BW: break; + case TRB_GET_EXT_PROP: + xhci_dbg(xhci, "Get Extended Property status 0x%08x\n", status); + break; + case TRB_SET_EXT_PROP: + xhci_dbg(xhci, "Set Extended Property status 0x%08x\n", status); + break; default: /* Skip over unknown commands on the event ring */ xhci_info(xhci, "INFO unknown command type %d\n", cmd_type); @@ -4494,3 +4500,52 @@ int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, return queue_command(xhci, cmd, 0, 0, 0, trb_slot_id | trb_ep_index | type, false); } + +int xhci_queue_get_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 property, u32 subtype, + u32 slot_id, u32 ep_index) +{ + u32 type = TRB_TYPE(TRB_GET_EXT_PROP); + u32 ctx_lo = 0; + u32 ctx_hi = 0; + + if (property != PROP_ECI_ALL) { + type |= SLOT_ID_FOR_TRB(slot_id) | + FIELD_PREP(TRB_PROP_EP_ID, ep_index + 1) | + FIELD_PREP(TRB_PROP_SUBTYPE, subtype); + + if (cmd->in_ctx) { + ctx_lo = lower_32_bits(cmd->in_ctx->dma); + ctx_hi = upper_32_bits(cmd->in_ctx->dma); + } + } + + return queue_command(xhci, cmd, ctx_lo, ctx_hi, + FIELD_PREP(TRB_PROP_ECI, property), type, false); +} + +int xhci_queue_set_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 param, u32 property, u32 subtype, + u32 slot_id, u32 ep_index) +{ + u32 type = TRB_TYPE(TRB_SET_EXT_PROP); + u32 cap_param; + u32 ctx_lo = 0; + u32 ctx_hi = 0; + + cap_param = FIELD_PREP(TRB_PROP_ECPARAM, param) | + FIELD_PREP(TRB_PROP_ECI, property); + + if (property != PROP_ECI_ALL) { + type |= SLOT_ID_FOR_TRB(slot_id) | + FIELD_PREP(TRB_PROP_EP_ID, ep_index + 1) | + FIELD_PREP(TRB_PROP_SUBTYPE, subtype); + + if (cmd->in_ctx) { + ctx_lo = lower_32_bits(cmd->in_ctx->dma); + ctx_hi = upper_32_bits(cmd->in_ctx->dma); + } + } + + return queue_command(xhci, cmd, ctx_lo, ctx_hi, cap_param, type, false); +} diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index a5deeee4d5dce4..d1134c2c047ff2 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -10,10 +10,89 @@ #include #include +#include #include "xhci.h" /* sideband internal helpers */ +static int +sideband_res_id_to_ep_index(struct xhci_sideband *sb, unsigned int res_id) +{ + int i; + + if (res_id == 0) + return -EINVAL; + + for (i = 0; i < EP_CTX_PER_DEV; i++) { + if (sb->eps_res_id[i] == res_id) + return i; + } + + return -ENODEV; +} + +static int +xhci_sideband_hw_max_resources(struct xhci_hcd *xhci) +{ + union xhci_extprop_ctx *ctx; + unsigned int res_max; + int ret; + + ctx = kzalloc_obj(*ctx, GFP_KERNEL); + if (!ctx) + return -ENOMEM; + + ret = xhci_get_ext_property(xhci, 0, 0, PROP_ECI_AUDIO, + AUDIO_SUBTYPE_GET_RES, ctx, sizeof(*ctx)); + if (ret < 0) { + kfree(ctx); + return ret; + } + + res_max = ctx->generic.byte[0]; + kfree(ctx); + + /* Intel hosts sideband resources are capped at 6 */ + if (xhci->quirks & XHCI_INTEL_HOST) + res_max = min(res_max, 6); + + return res_max; +} + +static bool +xhci_sideband_hw_capable(struct xhci_hcd *xhci) +{ + u16 buf = 0; + int res_max; + int ret; + + /* fastpath, all checks are done if resource limits are set */ + if (xhci->sideband_idr.id_lim.max > 0) + return true; + + /* xHCI 1.2 and later can support GSC bit in hcc_params2 register */ + if ((xhci->hci_version < 0x120) || !(xhci->hcc_params2 & HCC2_GSC)) + return false; + /* + * Note, normally xHC hardware writes directly to buf, so buf needs to + * be DMA mappable. PROP_ECI_ALL is special and returns 16 bits data in + * the command completion event, which is then copied to buf. + */ + ret = xhci_get_ext_property(xhci, 0, 0, PROP_ECI_ALL, 0, &buf, sizeof(buf)); + if (ret < 0 || !(buf & PROP_ECI_AUDIO)) + return false; + + /* check xHC has at least one resource for endpoint sideband use */ + res_max = xhci_sideband_hw_max_resources(xhci); + if (res_max > 0) { + xhci->sideband_idr.id_lim.min = 1; + xhci->sideband_idr.id_lim.max = res_max; + return true; + } + + return false; +} + static struct sg_table * xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring) { @@ -75,20 +154,78 @@ xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring) return NULL; } + +/* Return the current vdev of sb->udev, or NULL if it was freed or reallocated */ +static struct xhci_virt_device * +sideband_to_vdev(struct xhci_sideband *sb) +{ + struct xhci_virt_device *vdev; + + if (!sb->udev || !sb->udev->slot_id) + return NULL; + + vdev = sb->xhci->devs[sb->udev->slot_id]; + if (vdev && vdev->sideband == sb) + return vdev; + + return NULL; +} + +static bool +xhci_sideband_endpoint_belongs(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep) +{ + unsigned int epnum; + + if (!sb || !host_ep) + return false; + + epnum = usb_endpoint_num(&host_ep->desc); + + if (usb_endpoint_dir_in(&host_ep->desc)) + return sb->udev->ep_in[epnum] == host_ep; + + return sb->udev->ep_out[epnum] == host_ep; +} + +static int +xhci_sideband_hw_set_resource(struct xhci_sideband *sb, u32 param, u32 ep_index) +{ + unsigned int slot_id; + int ret; + + slot_id = sb->udev->slot_id; + + ret = xhci_set_ext_property(sb->xhci, slot_id, ep_index, PROP_ECI_AUDIO, + AUDIO_SUBTYPE_SET_RES, param); + return ret; +} + /* Caller must hold sb->mutex */ -static void +static int __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep) { + int ret = 0; + lockdep_assert_held(&sb->mutex); /* * Issue a stop endpoint command when an endpoint is removed. * The stop ep cmd handler will handle the ring cleanup. */ - xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL); + if (sb->type == XHCI_SIDEBAND_VENDOR) { + xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL); + + } else if (sb->type == XHCI_SIDEBAND_AUDIO) { + ret = xhci_sideband_hw_set_resource(sb, 0, ep->ep_index); + if (ret) + xhci_warn(sb->xhci, "Failed to clear sideband ep resource\n"); + } ep->sideband = NULL; sb->eps[ep->ep_index] = NULL; + + return ret; } /* Caller must hold sb->mutex */ @@ -106,6 +243,206 @@ __xhci_sideband_remove_interrupter(struct xhci_sideband *sb) /* sideband api functions */ +/** + * xhci_sideband_get_ep_properties - query xHC properties for an endpoint + * @sb: sideband instance for this usb device + * @host_ep: usb host endpoint + * @properties: returned properties. + */ +int +xhci_sideband_get_ep_properties(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + struct xhci_sideband_ep_properties *properties) +{ + union xhci_extprop_ctx *ctx; + unsigned int ep_index; + struct device *dev; + int ret = 0; + + guard(mutex)(&sb->mutex); + + if (!sideband_to_vdev(sb)) + return -ENODEV; + + if (!xhci_sideband_endpoint_belongs(sb, host_ep)) + return -EINVAL; + + if (sb->type != XHCI_SIDEBAND_AUDIO || !properties) + return -EINVAL; + + memset(properties, 0, sizeof(*properties)); + ep_index = xhci_get_endpoint_index(&host_ep->desc); + dev = xhci_to_hcd(sb->xhci)->self.controller; + + /* get the PCI B:D:F data of parent xHC */ + if (dev_is_pci(dev)) { + struct pci_dev *pdev = to_pci_dev(dev); + + properties->bus = pdev->bus->number; + properties->device = PCI_SLOT(pdev->devfn); + properties->function = PCI_FUNC(pdev->devfn); + } + + properties->slot_id = sb->udev->slot_id; + + properties->reserved_res_id = sb->eps_res_id[ep_index]; + + /* no resource id set for endpoint, don't query ep property */ + if (properties->reserved_res_id == 0) + return 0; + + ctx = kzalloc_obj(*ctx, GFP_KERNEL); + if (!ctx) + return -ENOMEM; + + /* query extended endpoint property from xHC */ + ret = xhci_get_ext_property(sb->xhci, sb->udev->slot_id, ep_index, + PROP_ECI_AUDIO, AUDIO_SUBTYPE_GET_EP_PROP, + ctx, sizeof(*ctx)); + if (!ret) { + properties->rh_port = ctx->audio_ep.rh_port; + properties->res_id = ctx->audio_ep.res_id; + properties->port_speed = ctx->audio_ep.port_speed_tt & 0xf; + properties->tt = !!(ctx->audio_ep.port_speed_tt & 0x10); + } + + kfree(ctx); + + return ret; +} +EXPORT_SYMBOL_GPL(xhci_sideband_get_ep_properties); + +/** + * xhci_sideband_alloc_resource_id - reserve a resource id to offload an endpoint + * @sb: sideband instance for this usb device + * @host_ep: usb host endpoint + * @min: lowest resource id acceptable to the caller, 0 for no preference + * @max: highest resource id acceptable to the caller, 0 for no preference + * + * Reserves a sideband resource id for this endpoint from the limited set of + * xHC resources ids. Only valid for XHCI_SIDEBAND_AUDIO sideband types. + * Once an endpoint has a valid resource id then it can be assigned to audio + * sideband by calling xhci_sideband_add_endpoint() + * + * The xHC limits which resources an endpoint of a direction may use. An + * offload engine which ties a function to particular resources narrows that + * further with @min and @max, which have to lie within the xHC's limits for + * the endpoint; the first free resource of the intersection is reserved. + * With both 0 the first free resource the xHC allows is reserved. + * + * An endpoint holds one reservation at a time; it has to be released with + * xhci_sideband_free_resource_id() before another one can be made. + * + * Returns: positive resource id on success, -EBUSY if all resources of the + * range are taken or the endpoint already holds a reservation, negative + * error otherwise. + */ +int +xhci_sideband_alloc_resource_id(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + unsigned int min, unsigned int max) +{ + struct xhci_sideband_idr *res; + struct xa_limit lim; + unsigned int ep_index; + unsigned int res_id; + int ret; + + guard(mutex)(&sb->mutex); + + if (!xhci_sideband_endpoint_belongs(sb, host_ep)) + return -EINVAL; + + if (sb->type != XHCI_SIDEBAND_AUDIO) + return -EINVAL; + + ep_index = xhci_get_endpoint_index(&host_ep->desc); + res = &sb->xhci->sideband_idr; + lim = res->id_lim; + + /* + * Intel xHC hardware limits resource IDs 1 and 2 to output endpoints. + * IDs 3-6 are for input endpoints + */ + if (sb->xhci->quirks & XHCI_INTEL_HOST) { + if (usb_endpoint_dir_out(&host_ep->desc)) + lim.max = 2; + else + lim.min = 3; + } + + if (min || max) { + if (min > max || min < lim.min || max > lim.max) + return -EINVAL; + lim.min = min; + lim.max = max; + } + + if (sb->eps_res_id[ep_index]) { + dev_dbg(&sb->udev->dev, "ep %u already holds resource id %u\n", + ep_index, sb->eps_res_id[ep_index]); + return -EBUSY; + } + + ret = xa_alloc(&res->id_xa, &res_id, sb, lim, GFP_KERNEL); + if (ret) + return ret; + + sb->eps_res_id[ep_index] = res_id; + + dev_dbg(&sb->udev->dev, "allocated sideband resource %u for ep %u\n", + res_id, ep_index); + + return res_id; +} +EXPORT_SYMBOL_GPL(xhci_sideband_alloc_resource_id); + +/** + * xhci_sideband_free_resource_id - release a resource id + * @sb: sideband instance for this usb device + * @res_id: resource id to free + * + * Free a previously reserved sideband resource id, making it available for + * endpoints. Don't release the resource id if the endpoint is in active + * sideband use. + * + * Returns: 0 on success, negative error otherwise. + */ +int +xhci_sideband_free_resource_id(struct xhci_sideband *sb, + unsigned long res_id) +{ + struct xhci_sideband_idr *res; + struct xhci_sideband *tmp_sb; + int ep_index; + + if (!sb) + return -EINVAL; + + res = &sb->xhci->sideband_idr; + + guard(mutex)(&sb->mutex); + + ep_index = sideband_res_id_to_ep_index(sb, res_id); + if (ep_index < 0) + return -ENODEV; + + tmp_sb = xa_load(&res->id_xa, res_id); + if (!tmp_sb || tmp_sb != sb) + return -ENODEV; + if (sb->eps[ep_index]) + return -EBUSY; + + xa_erase(&res->id_xa, res_id); + sb->eps_res_id[ep_index] = 0; + + dev_dbg(&sb->udev->dev, "erase ep %u resource id %lu\n", + ep_index, res_id); + + return 0; +} +EXPORT_SYMBOL_GPL(xhci_sideband_free_resource_id); + /** * xhci_sideband_notify_ep_ring_free - notify client of xfer ring free * @sb: sideband instance for this usb device @@ -146,16 +483,23 @@ int xhci_sideband_add_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep) { + struct xhci_virt_device *vdev; struct xhci_virt_ep *ep; unsigned int ep_index; + unsigned int res_id; + int ret; guard(mutex)(&sb->mutex); - if (!sb->vdev) + vdev = sideband_to_vdev(sb); + if (!vdev) return -ENODEV; + if (!xhci_sideband_endpoint_belongs(sb, host_ep)) + return -EINVAL; + ep_index = xhci_get_endpoint_index(&host_ep->desc); - ep = &sb->vdev->eps[ep_index]; + ep = &vdev->eps[ep_index]; if (ep->ep_state & EP_HAS_STREAMS) return -EINVAL; @@ -171,6 +515,19 @@ xhci_sideband_add_endpoint(struct xhci_sideband *sb, if (sb->eps[ep_index] || ep->sideband) return -EBUSY; + if (sb->type == XHCI_SIDEBAND_AUDIO) { + res_id = sb->eps_res_id[ep_index]; + if (res_id == 0) { + xhci_info(sb->xhci, "%s: Can't offload endpoint %d, missing resource id\n", + __func__, ep_index); + return -ENODEV; + } + + ret = xhci_sideband_hw_set_resource(sb, res_id, ep_index); + if (ret) + return ret; + } + ep->sideband = sb; sb->eps[ep_index] = ep; @@ -202,12 +559,12 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb, ep_index = xhci_get_endpoint_index(&host_ep->desc); ep = sb->eps[ep_index]; - if (!ep || !ep->sideband || ep->sideband != sb) + if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) { + sb->eps[ep_index] = NULL; return -ENODEV; + } - __xhci_sideband_remove_endpoint(sb, ep); - - return 0; + return __xhci_sideband_remove_endpoint(sb, ep); } EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint); @@ -221,13 +578,44 @@ xhci_sideband_stop_endpoint(struct xhci_sideband *sb, ep_index = xhci_get_endpoint_index(&host_ep->desc); ep = sb->eps[ep_index]; - if (!ep || !ep->sideband || ep->sideband != sb) + if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) return -EINVAL; return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL); } EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint); +/** + * xhci_sideband_start_endpoint - restart a sideband endpoint + * @sb: sideband instance for this usb device + * @host_ep: usb host endpoint + * + * Rings the doorbell of an endpoint stopped with xhci_sideband_stop_endpoint(), + * which is what moves a stopped endpoint back to the running state. Ringing + * the doorbell of a running endpoint does nothing. + * + * Return: 0 on success, negative error otherwise. + */ +int +xhci_sideband_start_endpoint(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep) +{ + struct xhci_virt_ep *ep; + unsigned int ep_index; + + ep_index = xhci_get_endpoint_index(&host_ep->desc); + ep = sb->eps[ep_index]; + + if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) + return -EINVAL; + + guard(spinlock_irqsave)(&sb->xhci->lock); + xhci_ring_ep_doorbell(sb->xhci, sb->udev->slot_id, ep_index, 0); + + return 0; +} +EXPORT_SYMBOL_GPL(xhci_sideband_start_endpoint); + /** * xhci_sideband_get_endpoint_buffer - gets the endpoint transfer buffer address * @sb: sideband instance for this usb device @@ -251,7 +639,7 @@ xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb, ep_index = xhci_get_endpoint_index(&host_ep->desc); ep = sb->eps[ep_index]; - if (!ep || !ep->ring || !ep->sideband || ep->sideband != sb) + if (!sideband_to_vdev(sb) || !ep || !ep->ring || ep->sideband != sb) return NULL; return xhci_ring_to_sgtable(sb, ep->ring); @@ -329,9 +717,6 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg, guard(mutex)(&sb->mutex); - if (!sb->vdev) - return -ENODEV; - if (sb->ir) return -EBUSY; @@ -408,14 +793,24 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, struct xhci_virt_device *vdev; struct xhci_sideband *sb; - /* - * Make sure the usb device is connected to a xhci controller. Fail - * registration if the type is anything other than XHCI_SIDEBAND_VENDOR, - * as this is the only type that is currently supported by xhci-sideband. - */ - if (!udev->slot_id || type != XHCI_SIDEBAND_VENDOR) + /* Make sure the usb device is connected to a xhci controller. */ + if (!udev->slot_id) return NULL; + switch (type) { + case XHCI_SIDEBAND_VENDOR: + break; + case XHCI_SIDEBAND_AUDIO: + if (!xhci_sideband_hw_capable(xhci)) { + dev_info(&udev->dev, "%s: Host not sideband capable\n", + __func__); + return NULL; + } + break; + default: + return NULL; + } + sb = kzalloc_node(sizeof(*sb), GFP_KERNEL, dev_to_node(hcd->self.sysdev)); if (!sb) return NULL; @@ -436,7 +831,7 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, } sb->xhci = xhci; - sb->vdev = vdev; + sb->udev = udev; sb->intf = intf; sb->type = type; sb->notify_client = notify_client; @@ -444,6 +839,8 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, spin_unlock_irq(&xhci->lock); + dev_dbg(&udev->dev, "Registered for sideband use\n"); + return sb; } EXPORT_SYMBOL_GPL(xhci_sideband_register); @@ -462,33 +859,48 @@ void xhci_sideband_unregister(struct xhci_sideband *sb) { struct xhci_virt_device *vdev; + struct usb_device *udev; struct xhci_hcd *xhci; + int res_id; int i; if (!sb) return; xhci = sb->xhci; + udev = sb->udev; scoped_guard(mutex, &sb->mutex) { - vdev = sb->vdev; - if (!vdev) - return; + vdev = sideband_to_vdev(sb); - for (i = 0; i < EP_CTX_PER_DEV; i++) - if (sb->eps[i]) + for (i = 0; i < EP_CTX_PER_DEV; i++) { + if (vdev && sb->eps[i]) __xhci_sideband_remove_endpoint(sb, sb->eps[i]); + if (sb->type != XHCI_SIDEBAND_AUDIO) + continue; + + res_id = sb->eps_res_id[i]; + if (res_id) { + dev_dbg(&udev->dev, "%s erase ep %u res_id %u\n", + __func__, i, res_id); + xa_erase(&xhci->sideband_idr.id_xa, res_id); + } + } + __xhci_sideband_remove_interrupter(sb); - sb->vdev = NULL; + sb->udev = NULL; } spin_lock_irq(&xhci->lock); sb->xhci = NULL; - vdev->sideband = NULL; + if (vdev) + vdev->sideband = NULL; spin_unlock_irq(&xhci->lock); + dev_dbg(&udev->dev, "sideband unregistered\n"); + kfree(sb); } EXPORT_SYMBOL_GPL(xhci_sideband_unregister); diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c index 30ac1bc4559b74..d764c94fa8590c 100644 --- a/drivers/usb/host/xhci.c +++ b/drivers/usb/host/xhci.c @@ -5263,6 +5263,121 @@ static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd, } #endif /* CONFIG_PM */ +int xhci_get_ext_property(struct xhci_hcd *xhci, u32 slot_id, u32 ep_index, + u32 property, u32 subtype, void *buf, size_t buf_len) +{ + struct device *dev = xhci_to_hcd(xhci)->self.sysdev; + struct xhci_container_ctx *ctx; + struct xhci_command *cmd; + unsigned long flags; + int ret; + + cmd = xhci_alloc_command(xhci, true, GFP_KERNEL); + if (!cmd) + return -ENOMEM; + + /* For PROP_ECI_ALL xHC does not write to buf, data is in event TRB */ + if (property == PROP_ECI_ALL) { + if (!buf || buf_len < sizeof(u16) || slot_id || subtype || ep_index) { + xhci_warn(xhci, "Invalid params while checking ext property support\n"); + ret = -EINVAL; + goto out; + } + } else if (slot_id && !xhci->devs[slot_id]) { + xhci_warn(xhci, "Invalid device for get ext property command\n"); + ret = -ENODEV; + goto out; + } else if (buf) { + ctx = kzalloc_node(sizeof(*ctx), GFP_KERNEL, dev_to_node(dev)); + if (!ctx) { + ret = -ENOMEM; + goto out; + } + ctx->bytes = buf; + ctx->size = buf_len; + ctx->dma = dma_map_single(dev, buf, buf_len, DMA_FROM_DEVICE); + if (dma_mapping_error(dev, ctx->dma)) { + kfree(ctx); + ret = -ENOMEM; + goto out; + } + cmd->in_ctx = ctx; + } + + spin_lock_irqsave(&xhci->lock, flags); + + ret = xhci_queue_get_ext_property(xhci, cmd, property, subtype, slot_id, ep_index); + + if (ret) { + spin_unlock_irqrestore(&xhci->lock, flags); + goto out; + } + + xhci_ring_cmd_db(xhci); + spin_unlock_irqrestore(&xhci->lock, flags); + + wait_for_completion(cmd->completion); + + if (cmd->status != COMP_SUCCESS) { + xhci_dbg(xhci, "Get ext property command failed %d\n", cmd->status); + ret = -EINVAL; + } else if (property == PROP_ECI_ALL) { + /* cmd->comp_param is u32, PROP_ECI_ALL only uses bits 15:0 */ + *(u16 *)buf = cmd->comp_param; + } +out: + if (cmd->in_ctx) { + dma_unmap_single(dev, ctx->dma, ctx->size, DMA_FROM_DEVICE); + kfree(ctx); + } + kfree(cmd->completion); + kfree(cmd); + + return ret; +} + +int xhci_set_ext_property(struct xhci_hcd *xhci, u32 slot_id, u32 ep_index, + u32 property, u32 subtype, u32 param) +{ + struct xhci_command *cmd; + unsigned long flags; + int ret; + + cmd = xhci_alloc_command(xhci, true, GFP_KERNEL); + if (!cmd) + return -ENOMEM; + + spin_lock_irqsave(&xhci->lock, flags); + + if (!slot_id || !xhci->devs[slot_id]) { + xhci_err(xhci, "Invalid device for set ext property command\n"); + spin_unlock_irqrestore(&xhci->lock, flags); + ret = -ENODEV; + goto out; + } + + ret = xhci_queue_set_ext_property(xhci, cmd, param, property, subtype, + slot_id, ep_index); + + if (ret) { + spin_unlock_irqrestore(&xhci->lock, flags); + goto out; + } + + xhci_ring_cmd_db(xhci); + spin_unlock_irqrestore(&xhci->lock, flags); + + wait_for_completion(cmd->completion); + + if (cmd->status != COMP_SUCCESS) { + xhci_dbg(xhci, "set ext property command failed %d\n", cmd->status); + ret = -EINVAL; + } +out: + xhci_free_command(xhci, cmd); + return ret; +} + /*-------------------------------------------------------------------------*/ /* Once a hub descriptor is fetched for a device, we need to update the xHC's diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index ec4bfeb4887cbf..ddfcb9b86a764d 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -532,6 +532,36 @@ struct xhci_input_control_ctx { #define EP_IS_DROPPED(ctrl_ctx, i) \ (le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) +/** + * xhci_extprop_ctx - union of extended property context structures + * + * Used with several different types of Get Extended Property commands. See: + * 7.9.1.1 Get Extended Property: Get Supported Resources Command + * 7.9.1.2 Get Extended Property: Get Endpoint Properties Command + * 7.10.3 Get Extended Property: Get Time Stamp Command + * + * Layout varies, but size is so far always 16 bytes. + * Define one extended property context for generic use. + * Define one extended property context for audio sideband extended capability + * endpoint properties + */ +struct xhci_generic_extprop_ctx { + u8 byte[16]; +} __packed; + +struct xhci_audio_ep_extprop_ctx { + u8 rh_port; + u8 res_id; + u8 port_speed_tt; + u8 rsvdz; + __le32 rsvdz2[3]; +} __packed; + +union xhci_extprop_ctx { + struct xhci_generic_extprop_ctx generic; + struct xhci_audio_ep_extprop_ctx audio_ep; +}; + /* Represents everything that is needed to issue a command on the command ring. * It's useful to pre-allocate these for commands that cannot fail due to * out-of-memory errors, like freeing streams. @@ -1022,6 +1052,22 @@ enum xhci_setup_dev { #define STREAM_ID_FOR_TRB(p) ((((p)) & 0xffff) << 16) #define SCT_FOR_TRB(p) (((p) & 0x7) << 1) +/* Get/Set Extended Property command TRB fields */ +#define TRB_PROP_ECI GENMASK(15, 0) +#define TRB_PROP_ECPARAM GENMASK(23, 16) + +#define TRB_PROP_SUBTYPE GENMASK(18, 16) +#define TRB_PROP_EP_ID GENMASK(23, 19) + +#define PROP_ECI_ALL 0 /* use extended capability 0 to list capabilities */ +#define PROP_ECI_AUDIO BIT(0) /* Audio sideband extended capability identifier */ + +/* Get/Set Extended Property Audio sideband subtypes */ +#define AUDIO_SUBTYPE_GET_RES 1 /* Get supported resources */ +#define AUDIO_SUBTYPE_GET_EP_PROP 2 /* Get endpoint properties */ + +#define AUDIO_SUBTYPE_SET_RES 1 /* Set resource assignment */ + /* Link TRB specific fields */ #define TRB_TC BIT(1) @@ -1148,7 +1194,12 @@ union xhci_trb { #define TRB_FORCE_HEADER 22 /* No-op Command - not for transfer rings */ #define TRB_CMD_NOOP 23 -/* TRB IDs 24-31 reserved */ +/* Get Extended Property Command (opt) */ +#define TRB_GET_EXT_PROP 24 +/* Set Extended Property Command (opt) */ +#define TRB_SET_EXT_PROP 25 +/* TRB IDs 26-31 reserved */ + /* Event TRBS */ /* Transfer Event */ #define TRB_TRANSFER 32 @@ -1504,6 +1555,11 @@ struct xhci_hub { u8 min_rev; }; +struct xhci_sideband_idr { + struct xarray id_xa; + struct xa_limit id_lim; +}; + /* There is one xhci_hcd structure per controller */ struct xhci_hcd { struct usb_hcd *main_hcd; @@ -1677,7 +1733,8 @@ struct xhci_hcd { struct dentry *debugfs_root; struct dentry *debugfs_slots; struct list_head regset_list; - + /* Sideband controlled endpoint resource ids, xhci 7.9.2.1 */ + struct xhci_sideband_idr sideband_idr; void *dbc; /* platform-specific data -- must come last */ unsigned long priv[] __aligned(sizeof(s64)); @@ -1952,6 +2009,14 @@ int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, enum xhci_ep_reset_type reset_type); int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, u32 slot_id); +int xhci_queue_get_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 property, u32 subtype, u32 slot_id, u32 ep_id); +int xhci_queue_set_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 param, u32 property, u32 subtype, u32 slot_id, u32 ep_id); +int xhci_set_ext_property(struct xhci_hcd *xhci, u32 slot_id, u32 ep_index, + u32 property, u32 subtype, u32 param); +int xhci_get_ext_property(struct xhci_hcd *xhci, u32 slot_id, u32 ep_index, + u32 property, u32 subtype, void *buf, size_t buf_len); void xhci_handle_command_timeout(struct work_struct *work); void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id, diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h index 005257085dcbd7..25d76681b2408c 100644 --- a/include/linux/usb/xhci-sideband.h +++ b/include/linux/usb/xhci-sideband.h @@ -36,23 +36,57 @@ struct xhci_sideband_event { void *evt_data; }; +/** + * struct xhci_sideband_ep_properties - controller properties of an endpoint. + * @bus: PCI bus number if xHCI is on a PCI bus + * @device: PCI device id if xHCI is on a PCI bus + * @function: PCI function number if xHCI is on a PCI bus + * @slot_id: xHCI slot_id of the usb device this endpoint is part of + * @reserved_res_id: driver reserved resource number for this endpoint + * @rh_port: root hub port number + * @res_id: xHCI sideband resource number in use by offloaded endpoint + * @port_speed: PORTSC protocol speed ID + * @tt: endpoint is behind a USB 2.0 transaction translator + * + * reserved_res_id is set after calling xhci_sideband_alloc_resource_id(), + * res_id is set after offloading the endpoint when xhci_sideband_add_endpoint() + * is called, and should match reserved_res_id at that point. + */ +struct xhci_sideband_ep_properties { + /* PCI bus, device and function */ + u8 bus; + u8 device; + u8 function; + + /* xHCI usb device slot_id */ + int slot_id; + u8 reserved_res_id; + + /* Properties queried with Get Endpoint Properties Command */ + u8 rh_port; + u8 res_id; + u8 port_speed; + bool tt; +}; + /** * struct xhci_sideband - representation of a sideband accessed usb device. * @xhci: The xhci host controller the usb device is connected to - * @vdev: the usb device accessed via sideband + * @udev: the usb device accessed via sideband * @eps: array of endpoints controlled via sideband + * @eps_res_id: array of resource ids for endpoints * @ir: event handling and buffer for sideband accessed device * @type: xHCI sideband type * @mutex: mutex for sideband operations * @intf: USB sideband client interface * @notify_client: callback for xHCI sideband sequences * - * FIXME usb device accessed via sideband Keeping track of sideband accessed usb devices. */ struct xhci_sideband { struct xhci_hcd *xhci; - struct xhci_virt_device *vdev; + struct usb_device *udev; struct xhci_virt_ep *eps[EP_CTX_PER_DEV]; + unsigned int eps_res_id[EP_CTX_PER_DEV]; struct xhci_interrupter *ir; enum xhci_sideband_type type; @@ -79,6 +113,9 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb, int xhci_sideband_stop_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep); +int +xhci_sideband_start_endpoint(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep); struct sg_table * xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep); @@ -99,6 +136,17 @@ void xhci_sideband_remove_interrupter(struct xhci_sideband *sb); int xhci_sideband_interrupter_id(struct xhci_sideband *sb); +int +xhci_sideband_alloc_resource_id(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + unsigned int min, unsigned int max); +int +xhci_sideband_free_resource_id(struct xhci_sideband *sb, + unsigned long res_id); +int +xhci_sideband_get_ep_properties(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + struct xhci_sideband_ep_properties *properties); #if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND) void xhci_sideband_notify_ep_ring_free(struct xhci_sideband *sb, diff --git a/include/sound/hda-mlink.h b/include/sound/hda-mlink.h index ba35f03576b9da..9cd4889791bb9e 100644 --- a/include/sound/hda-mlink.h +++ b/include/sound/hda-mlink.h @@ -74,6 +74,7 @@ enum hda_bus_ml_link_type hda_bus_ml_link_get_type(struct hdac_ext_link *hlink); struct hdac_ext_link *hdac_bus_eml_ssp_get_hlink(struct hdac_bus *bus); struct hdac_ext_link *hdac_bus_eml_dmic_get_hlink(struct hdac_bus *bus); struct hdac_ext_link *hdac_bus_eml_sdw_get_hlink(struct hdac_bus *bus); +struct hdac_ext_link *hdac_bus_eml_uaol_get_hlink(struct hdac_bus *bus); struct mutex *hdac_bus_eml_get_mutex(struct hdac_bus *bus, bool alt, int elid); @@ -202,6 +203,9 @@ hdac_bus_eml_dmic_get_hlink(struct hdac_bus *bus) { return NULL; } static inline struct hdac_ext_link * hdac_bus_eml_sdw_get_hlink(struct hdac_bus *bus) { return NULL; } +static inline struct hdac_ext_link * +hdac_bus_eml_uaol_get_hlink(struct hdac_bus *bus) { return NULL; } + static inline struct mutex * hdac_bus_eml_get_mutex(struct hdac_bus *bus, bool alt, int elid) { return NULL; } diff --git a/include/sound/soc-acpi.h b/include/sound/soc-acpi.h index 2ee303b791dc84..09b011bd660a6f 100644 --- a/include/sound/soc-acpi.h +++ b/include/sound/soc-acpi.h @@ -74,6 +74,9 @@ static inline struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg) * @subsystem_id_set: true if a value has been written to * subsystem_vendor and subsystem_device. * @bt_link_mask: BT offload link enabled on the board + * @uaol_link_mask: USB audio offload links available on the board + * @uaol_playback_num: playback streams of each USB audio offload link + * @uaol_capture_num: capture streams of each USB audio offload link */ struct snd_soc_acpi_mach_params { u32 acpi_ipc_irq_index; @@ -90,6 +93,9 @@ struct snd_soc_acpi_mach_params { unsigned short subsystem_rev; bool subsystem_id_set; u32 bt_link_mask; + u32 uaol_link_mask; + u32 uaol_playback_num; + u32 uaol_capture_num; }; /** diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h index 124acb1939e57f..39d3a13a069627 100644 --- a/include/sound/soc-usb.h +++ b/include/sound/soc-usb.h @@ -19,9 +19,12 @@ enum snd_soc_usb_kctl { * @chip_idx: USB sound chip array index * @cpcm_idx: capture PCM index array associated with USB device * @ppcm_idx: playback PCM index array associated with USB device + * @cpcm_names: capture PCM name array, parallel to @cpcm_idx + * @ppcm_names: playback PCM name array, parallel to @ppcm_idx * @num_capture: number of capture streams * @num_playback: number of playback streams - * @list: list head for SoC USB devices + * @list: list head for the backend's own use + * @available_node: link on snd_soc_usb.available_devices, owned by soc-usb **/ struct snd_soc_usb_device { int card_idx; @@ -30,10 +33,32 @@ struct snd_soc_usb_device { /* PCM index arrays */ unsigned int *cpcm_idx; /* TODO: capture path is not tested yet */ unsigned int *ppcm_idx; + /* human readable PCM name arrays, parallel to the index arrays above */ + const char * const *cpcm_names; + const char * const *ppcm_names; int num_capture; /* TODO: capture path is not tested yet */ int num_playback; struct list_head list; + struct list_head available_node; +}; + +/** + * struct snd_soc_usb_route - one offload route of a direction + * @sdev: the device currently routed to, NULL when nothing is selected + * @pcm_idx: the PCM index of @sdev currently routed to, -1 when nothing is + * selected + * @kctl: the enumerated route kcontrol, once created by + * snd_soc_usb_setup_offload_route_ctl() + * + * A backend able to run several offloaded streams in a direction at a time + * has a route per stream. Two routes may point at the same pair; only one of + * them can offload it at a time, the other fails when it tries. + **/ +struct snd_soc_usb_route { + struct snd_soc_usb_device *sdev; + int pcm_idx; + struct snd_kcontrol *kctl; }; /** @@ -47,6 +72,14 @@ struct snd_soc_usb_device { * an array used for a kcontrol output for either * the card or pcm index. "path" determines the * which entry to look for. (ie mapped card or pcm) + * @is_route_busy: callback reporting whether the backend already has a + * stream open on route @route of the given direction. Consulted + * before a route change is allowed, so a link a stream is + * actively using is never switched out from under it. + * @available_devices: list of currently connected snd_soc_usb_device entries + * @routes: the routes of each direction, @num_routes of them + * @num_routes: number of routes per direction, one unless the backend set + * more with snd_soc_usb_set_num_routes() * @priv_data: driver data **/ struct snd_soc_usb { @@ -59,6 +92,13 @@ struct snd_soc_usb { int card, int pcm, int direction, enum snd_soc_usb_kctl path, long *route); + bool (*is_route_busy)(struct snd_soc_usb *usb, int direction, + unsigned int route); + + struct list_head available_devices; + struct snd_soc_usb_route *routes[SNDRV_PCM_STREAM_LAST + 1]; + unsigned int num_routes[SNDRV_PCM_STREAM_LAST + 1]; + void *priv_data; }; @@ -80,8 +120,17 @@ int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *component, void *data); void snd_soc_usb_free_port(struct snd_soc_usb *usb); +int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes); void snd_soc_usb_add_port(struct snd_soc_usb *usb); void snd_soc_usb_remove_port(struct snd_soc_usb *usb); + +int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb); +int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index); #else static inline int snd_soc_usb_find_supported_format(int card_idx, struct snd_pcm_hw_params *params, @@ -113,9 +162,10 @@ static inline int snd_soc_usb_setup_offload_jack(struct snd_soc_component *compo return 0; } -static int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, - int direction, enum snd_soc_usb_kctl path, - long *route) +static inline int +snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, + int direction, enum snd_soc_usb_kctl path, + long *route) { return -ENODEV; } @@ -129,10 +179,30 @@ snd_soc_usb_allocate_port(struct snd_soc_component *component, void *data) static inline void snd_soc_usb_free_port(struct snd_soc_usb *usb) { } +static inline int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes) +{ + return -ENODEV; +} + static inline void snd_soc_usb_add_port(struct snd_soc_usb *usb) { } static inline void snd_soc_usb_remove_port(struct snd_soc_usb *usb) { } + +static inline int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb) +{ + return 0; +} + +static inline int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index) +{ + return -ENODEV; +} #endif /* IS_ENABLED(CONFIG_SND_SOC_USB) */ #endif /*__LINUX_SND_SOC_USB_H */ diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..70ba718519ba9f 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -434,7 +434,7 @@ enum sof_ipc4_fw_config_params { SOF_IPC4_FW_CFG_SCHEDULER_CONFIG, SOF_IPC4_FW_CFG_XTAL_FREQ_HZ, SOF_IPC4_FW_CFG_CLOCKS_CONFIG, - SOF_IPC4_FW_CFG_RESERVED, + SOF_IPC4_FW_CFG_UAOL_SUPPORT, SOF_IPC4_FW_CFG_POWER_GATING_POLICY, SOF_IPC4_FW_CFG_ASSERT_MODE, /* Reserved: 24 - 28 */ @@ -467,9 +467,25 @@ struct sof_ipc4_dx_state_info { } __packed __aligned(4); enum sof_ipc4_hw_config_params { + SOF_IPC4_HW_CFG_INTEL_UAOL_CAPS = 10, SOF_IPC4_HW_CFG_INTEL_MIC_PRIVACY_CAPS = 11, }; +#define SOF_IPC4_UAOL_INPUT_STREAMS_MASK GENMASK(3, 0) +#define SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK GENMASK(7, 4) +#define SOF_IPC4_UAOL_BIDIRECTIONAL_STREAMS_MASK GENMASK(12, 8) + +struct sof_ipc4_intel_uaol_link_caps { + u32 stream_caps; + u32 max_tx_fifo_size; + u32 max_rx_fifo_size; +} __packed __aligned(4); + +struct sof_ipc4_intel_uaol_caps { + u32 link_count; + struct sof_ipc4_intel_uaol_link_caps link_caps[]; +} __packed __aligned(4); + #define SOF_IPC_INTEL_MIC_PRIVACY_VERSION_PTL 1 struct sof_ipc4_intel_mic_privacy_cap { diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h new file mode 100644 index 00000000000000..c53273aea1c731 --- /dev/null +++ b/include/sound/usb-offload.h @@ -0,0 +1,232 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * USB audio offload. + * + * An offloaded USB audio stream is driven by a DSP instead of by the + * host: the host controller routes the endpoint's traffic to the DSP over + * a dedicated link, an xHCI Audio Sideband resource. + * + * The USB audio offload library tracks offload capable USB audio devices + * and forwards their lifecycle to the driver owning the DSP. That driver + * installs a struct usb_offload_ops and turns each callback into whatever + * its DSP expects, normally an IPC message. The library builds no IPC + * message, touches no register and owns no interrupt. + * + * Lifecycle: + * + * ctx = usb_offload_register(&res); + * ...devices are reported via ops->notify_connect/notify_disconnect... + * usb_offload_unregister(ctx); + * + * One context stands for one offload engine, the link over which a DSP + * reaches a host controller. A host with several of them registers one + * context for each. + * + * The data path is not driven from here. The host's backend DAI reserves + * the endpoint resources itself with usb_offload_acquire_stream(). + */ + +#ifndef __SOUND_USB_OFFLOAD_H +#define __SOUND_USB_OFFLOAD_H + +#include + +struct device; +struct snd_pcm_hw_params; +struct usb_device; +struct usb_offload_ctx; + +#define USB_OFFLOAD_MAX_PCM 8 +#define USB_OFFLOAD_PCM_NAME_LEN 64 + +/** + * struct usb_offload_usb_dev_info - identity of an offloadable USB device + * @card_idx: snd_card number + * @chip_idx: snd_usb_audio index + * @num_playback: populated @ppcm_idx entries + * @num_capture: populated @cpcm_idx entries + * @ppcm_idx: playback PCM device numbers + * @cpcm_idx: capture PCM device numbers + * @ppcm_names: playback PCM names, parallel to @ppcm_idx - ", | + * - " so that a device offering several + * destinations, and several devices offering the same generic PCM + * name, both stay distinguishable + * @cpcm_names: capture PCM names, parallel to @cpcm_idx, same convention + * + * Filled in by the snd-usb-audio ownership bridge, which holds the + * struct snd_usb_audio chip, and handed to the host on connect. + */ +struct usb_offload_usb_dev_info { + int card_idx; + int chip_idx; + int num_playback; + int num_capture; + unsigned int ppcm_idx[USB_OFFLOAD_MAX_PCM]; + unsigned int cpcm_idx[USB_OFFLOAD_MAX_PCM]; + char ppcm_names[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; + char cpcm_names[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; +}; + +/** + * struct usb_offload_ops - host callback table, library to host + * @notify_connect: an offloadable device appeared + * @notify_disconnect: an offloaded device went away + * @get_resource_range: the sideband resources an endpoint may be placed on, + * as an inclusive range of xHCI Audio Sideband resource + * IDs, @first to @last. @direction is the SNDRV_PCM_STREAM_* + * value of the stream and @feedback tells a playback stream's + * clock feedback endpoint from its data endpoint. Optional: + * an offload engine whose firmware ties a function to + * particular resources answers, one that does not care + * leaves it unset and the lowest free resource is taken. + * An error fails the acquisition of the stream. + * + * @notify_connect and @notify_disconnect are mandatory. All callbacks run in + * process context. + */ +struct usb_offload_ops { + int (*notify_connect)(struct device *dev, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id); + int (*notify_disconnect)(struct device *dev, struct usb_device *udev); + + int (*get_resource_range)(struct device *dev, int direction, bool feedback, + unsigned int *first, unsigned int *last); +}; + +/** + * struct usb_offload_res - what the host hands the library at registration + * @dev: the host's device, passed back to the @ops callbacks + * @ops: host callback table + * @s24_3le_as_s24_le: treat a device that only offers S24_3LE as if it also + * offered S24_LE when refining a stream's format, and resolve + * the negotiated format back to S24_3LE for the device in + * usb_offload_resolve_format(). Set this when the host's own + * pipeline only has a 24-bit representation as S24_LE (24 + * valid bits in a 4-byte container) and cannot take S24_3LE + * directly; leave false otherwise. + * @async_playback_on_host: keep playback streams which need clock feedback + * (an asynchronous OUT endpoint, explicit or implicit + * feedback) on the host: their PCMs are not reported in the + * device identity when no other format can be offloaded, the + * formats are skipped when refining and resolving hw_params, + * and usb_offload_acquire_stream() refuses them. So is the + * capture of the endpoint a kept playback format with + * implicit feedback syncs to, which is that playback's clock. + * Set this when the DSP applies the feedback in a way that + * degrades the audio. The allow_feedback module parameter of + * the library overrides it for testing. + */ +struct usb_offload_res { + struct device *dev; + const struct usb_offload_ops *ops; + bool s24_3le_as_s24_le; + bool async_playback_on_host; +}; + +struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res); +void usb_offload_unregister(struct usb_offload_ctx *ctx); + +/** + * struct usb_offload_ep_info - an endpoint handed to the DSP + * @ep_addr: bEndpointAddress of the endpoint + * @resource: xHCI Audio Sideband resource ID the endpoint is bound to + * @maxpacket: wMaxPacketSize of the endpoint + * @interval: period of the endpoint as an isochronous bInterval, 2^(n-1) + * frames (full speed) or microframes (high speed). For a feedback + * endpoint this is the refresh period the host would poll it at, + * which a UAC1 device states in bRefresh rather than bInterval. + */ +struct usb_offload_ep_info { + u8 ep_addr; + u8 resource; + u16 maxpacket; + u8 interval; +}; + +/** + * struct usb_offload_stream_hw - resources reserved for one offloaded stream + * @pcm_index: USB SND PCM device number the stream was acquired on, to be + * passed back to usb_offload_quiesce_stream(), + * usb_offload_restart_stream() and usb_offload_release_stream() + * @bus_devid: PCI bus, device and function of the xHCI controller, as + * PCI_DEVID() lays them out + * @slot_id: xHCI slot ID of the USB device + * @high_speed: the root port the device hangs off runs at high speed + * @split: the endpoint is reached through a USB 2.0 transaction translator + * @data: the isochronous data endpoint + * @feedback: the explicit clock feedback endpoint of a playback stream, + * valid when @has_feedback is set + * @has_feedback: @feedback has been reserved for the DSP + * @service_interval_us: service interval of @data in microseconds: a multiple + * of a 1 ms frame for a full speed device, of a 125 us + * microframe for a high speed one + * + * Filled in by usb_offload_acquire_stream() and released again by + * usb_offload_release_stream(). + */ +struct usb_offload_stream_hw { + int pcm_index; + u16 bus_devid; + u8 slot_id; + bool high_speed; + bool split; + struct usb_offload_ep_info data; + struct usb_offload_ep_info feedback; + bool has_feedback; + unsigned int service_interval_us; +}; + +/* + * Data-path resource acquisition + * + * acquire_stream() starts the stream on the USB side and reports the + * connected device's data endpoint so that the caller can program its DSP. + * The USB PCM is taken away from the host for as long as it is held; the + * other PCMs of the device are not affected and can be acquired alongside. + * So are the endpoints the DSP drives: one which another stream holds, like + * the capture endpoint a playback stream with implicit feedback syncs to, + * fails the acquisition with -EBUSY, and the host cannot open one while the + * stream is held. + * release_stream() reverses it. quiesce_stream() only stops the endpoints on + * the controller, for the caller to run once its own side of the link has + * stopped, before it is torn down; release_stream() still has to follow, or + * restart_stream() when the stream is started again without being released + * in between. All of them name the stream by its direction and the USB PCM + * device number acquire_stream() reported in usb_offload_stream_hw.pcm_index. + * + * @params is the format to run the device at and @direction an + * SNDRV_PCM_STREAM_* value. @pcm_index pins the search to one USB SND PCM + * device of a multi-stream device (e.g. via snd_soc_usb_get_selected_device()), + * or is negative to consider every PCM stream of the device. + */ +int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index, + struct snd_pcm_hw_params *params, + struct usb_offload_stream_hw *hw); +int usb_offload_release_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index); +int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index); +int usb_offload_restart_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index); + +/* + * Narrow one of SNDRV_PCM_HW_PARAM_RATE, _CHANNELS or _FORMAT (@var) in + * @params to what the selected USB PCM of @udev can run at, considering the + * other two parameters, as a hardware rule of the host's PCM would. The + * selection is the same as for usb_offload_acquire_stream(). + */ +int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index, int var, + struct snd_pcm_hw_params *params); + +int usb_offload_resolve_format(struct usb_offload_ctx *ctx, struct usb_device *udev, + int direction, int pcm_index, + struct snd_pcm_hw_params *params); + +#endif /* __SOUND_USB_OFFLOAD_H */ diff --git a/sound/hda/core/device.c b/sound/hda/core/device.c index 6187ffaedbfa3f..a27a48d15e2398 100644 --- a/sound/hda/core/device.c +++ b/sound/hda/core/device.c @@ -712,10 +712,10 @@ static const struct hda_rate_tbl rate_bits[] = { { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, #define AC_PAR_PCM_RATE_BITS 11 - /* up to bits 10, 384kHZ isn't supported properly */ + /* up to bits 10, the rest below are not autodetected values */ - /* not autodetected value */ { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, + { 384000, SNDRV_PCM_RATE_384000, HDA_RATE(48, 8, 1) }, { 0 } /* terminator */ }; diff --git a/sound/soc/intel/boards/skl_hda_dsp_generic.c b/sound/soc/intel/boards/skl_hda_dsp_generic.c index 08e5e22de9da43..4c6dddf852b97e 100644 --- a/sound/soc/intel/boards/skl_hda_dsp_generic.c +++ b/sound/soc/intel/boards/skl_hda_dsp_generic.c @@ -53,6 +53,7 @@ static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card) #define DMIC01_BE_ID 6 #define DMIC16K_BE_ID 7 #define BT_OFFLOAD_BE_ID 8 +#define UAOL_BE_ID 9 #define HDMI_IN_BE_ID 13 #define HDA_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_IDISP_HDMI, \ @@ -60,16 +61,16 @@ static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card) SOF_LINK_DMIC01, \ SOF_LINK_DMIC16K, \ SOF_LINK_BT_OFFLOAD, \ - SOF_LINK_HDMI_IN, \ - SOF_LINK_NONE) + SOF_LINK_UAOL, \ + SOF_LINK_HDMI_IN) #define HDA_LINK_IDS SOF_LINK_ORDER(IDISP_HDMI_BE_ID, \ HDA_BE_ID, \ DMIC01_BE_ID, \ DMIC16K_BE_ID, \ BT_OFFLOAD_BE_ID, \ - HDMI_IN_BE_ID, \ - 0) + UAOL_BE_ID, \ + HDMI_IN_BE_ID) static unsigned long skl_hda_get_board_quirk(struct snd_soc_acpi_mach_params *mach_params) @@ -130,6 +131,10 @@ static int skl_hda_audio_probe(struct platform_device *pdev) if (mach->mach_params.codec_mask & IDISP_CODEC_MASK) ctx->hdmi.idisp_codec = true; + ctx->uaol_link_mask = mach->mach_params.uaol_link_mask; + ctx->uaol_playback_num = mach->mach_params.uaol_playback_num; + ctx->uaol_capture_num = mach->mach_params.uaol_capture_num; + ctx->link_order_overwrite = HDA_LINK_ORDER; ctx->link_id_overwrite = HDA_LINK_IDS; diff --git a/sound/soc/intel/boards/sof_board_helpers.c b/sound/soc/intel/boards/sof_board_helpers.c index f741a1e142bee1..c025b6073d9482 100644 --- a/sound/soc/intel/boards/sof_board_helpers.c +++ b/sound/soc/intel/boards/sof_board_helpers.c @@ -6,6 +6,7 @@ #include "../common/soc-intel-quirks.h" #include "hda_dsp_common.h" #include "sof_board_helpers.h" +#include "sof_uaol_common.h" /* * Intel HDMI DAI Link @@ -497,6 +498,86 @@ static int set_hda_codec_link(struct device *dev, struct snd_soc_dai_link *link, return 0; } +static int set_uaol_link(struct device *dev, struct snd_soc_dai_link *link, + int be_id, int uaol_id, int stream, int index) +{ + bool playback = stream == SNDRV_PCM_STREAM_PLAYBACK; + struct snd_soc_dai_link_component *cpus; + + dev_dbg(dev, "link %d: uaol %d, %s %d\n", be_id, uaol_id, + playback ? "playback" : "capture", index); + + link->name = devm_kasprintf(dev, GFP_KERNEL, SOF_UAOL_LINK_NAME(stream), + uaol_id, index); + if (!link->name) + return -ENOMEM; + + /* cpus */ + cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), + GFP_KERNEL); + if (!cpus) + return -ENOMEM; + + cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, SOF_UAOL_CPU_DAI_NAME(stream), + uaol_id, index); + if (!cpus->dai_name) + return -ENOMEM; + + link->cpus = cpus; + link->num_cpus = 1; + + /* codecs */ + link->codecs = &snd_soc_dummy_dlc; + link->num_codecs = 1; + + /* platforms */ + link->platforms = platform_component; + link->num_platforms = ARRAY_SIZE(platform_component); + + link->id = be_id; + link->no_pcm = 1; + if (playback) + link->playback_only = 1; + else + link->capture_only = 1; + + return 0; +} + +/* + * One backend per stream of each direction of UAOL link @uaol_id. Ordinal + * first, playback before capture within an ordinal: the IDs of the first + * playback and capture backend then do not depend on how many streams the + * link has. + */ +static int set_uaol_links(struct device *dev, struct sof_card_private *ctx, + struct snd_soc_dai_link *links, int *idx, + unsigned long *be_id, int uaol_id) +{ + unsigned int num_streams = max(ctx->uaol_playback_num, ctx->uaol_capture_num); + int stream, i, ret; + + for (i = 0; i < num_streams; i++) { + for_each_pcm_streams(stream) { + unsigned int num = stream == SNDRV_PCM_STREAM_PLAYBACK ? + ctx->uaol_playback_num : ctx->uaol_capture_num; + + if (i >= num) + continue; + + ret = set_uaol_link(dev, &links[*idx], *be_id, uaol_id, + stream, i); + if (ret) + return ret; + + (*idx)++; + (*be_id)++; + } + } + + return 0; +} + static int calculate_num_links(struct sof_card_private *ctx) { int num_links = 0; @@ -530,6 +611,10 @@ static int calculate_num_links(struct sof_card_private *ctx) if (ctx->hda_codec_present) num_links += 2; + /* USB offload, one link per stream of each direction */ + num_links += hweight_long(ctx->uaol_link_mask) * + (ctx->uaol_playback_num + ctx->uaol_capture_num); + return num_links; } @@ -542,6 +627,7 @@ int sof_intel_board_set_dai_link(struct device *dev, struct snd_soc_card *card, int idx = 0; int ret; int ssp_hdmi_in = 0; + int uaol_id = 0; unsigned long link_order, link; unsigned long link_ids, be_id; @@ -713,6 +799,17 @@ int sof_intel_board_set_dai_link(struct device *dev, struct snd_soc_card *card, idx++; break; + case SOF_LINK_UAOL: + /* USB offload */ + for_each_set_bit(uaol_id, &ctx->uaol_link_mask, + BITS_PER_TYPE(ctx->uaol_link_mask)) { + ret = set_uaol_links(dev, ctx, links, &idx, &be_id, uaol_id); + if (ret) { + dev_err(dev, "fail to set uaol link, ret %d\n", ret); + return ret; + } + } + break; case SOF_LINK_NONE: /* caught here if it's not used as terminator in macro */ fallthrough; diff --git a/sound/soc/intel/boards/sof_board_helpers.h b/sound/soc/intel/boards/sof_board_helpers.h index 33a9601b770c54..dc2c974d68d57b 100644 --- a/sound/soc/intel/boards/sof_board_helpers.h +++ b/sound/soc/intel/boards/sof_board_helpers.h @@ -58,6 +58,7 @@ enum { SOF_LINK_BT_OFFLOAD, SOF_LINK_HDMI_IN, SOF_LINK_HDA, + SOF_LINK_UAOL, }; #define SOF_LINK_ORDER_MASK (0xF) @@ -121,6 +122,9 @@ struct sof_rt5682_private { * @ssp_amp: ssp port number of speaker BE link * @ssp_bt: ssp port number of BT offload BE link * @ssp_mask_hdmi_in: ssp port mask of HDMI-IN BE link + * @uaol_link_mask: mask of the USB offload links to create BE links for + * @uaol_playback_num: playback BE links to create per USB offload link + * @uaol_capture_num: capture BE links to create per USB offload link * @bt_offload_present: true to create BT offload BE link * @hda_codec_present: true to create HDA codec BE links * @codec_link: pointer to headset codec dai link @@ -144,6 +148,9 @@ struct sof_card_private { int ssp_amp; int ssp_bt; unsigned long ssp_mask_hdmi_in; + unsigned long uaol_link_mask; + unsigned int uaol_playback_num; + unsigned int uaol_capture_num; bool bt_offload_present; bool hda_codec_present; diff --git a/sound/soc/intel/boards/sof_sdw.c b/sound/soc/intel/boards/sof_sdw.c index 5a4620faa9514f..bef2b662ce7593 100644 --- a/sound/soc/intel/boards/sof_sdw.c +++ b/sound/soc/intel/boards/sof_sdw.c @@ -16,6 +16,7 @@ #include #include #include "sof_sdw_common.h" +#include "sof_uaol_common.h" #include "../../codecs/rt711.h" static unsigned long sof_sdw_quirk = RT711_JD1; @@ -1225,6 +1226,56 @@ static int create_bt_dailinks(struct snd_soc_card *card, return 0; } +static int create_uaol_dailinks(struct snd_soc_card *card, + struct snd_soc_dai_link **dai_links, int *be_id, + const struct snd_soc_acpi_mach_params *mach_params) +{ + unsigned long uaol_mask = mach_params->uaol_link_mask; + unsigned int num_streams = max(mach_params->uaol_playback_num, + mach_params->uaol_capture_num); + struct device *dev = card->dev; + int i, j, stream, ret; + + /* + * Ordinal first, playback before capture within an ordinal: the IDs of + * the first playback and capture backend then do not depend on how many + * streams the link has. + */ + for_each_set_bit(i, &uaol_mask, BITS_PER_TYPE(uaol_mask)) { + for (j = 0; j < num_streams; j++) { + for_each_pcm_streams(stream) { + bool playback = stream == SNDRV_PCM_STREAM_PLAYBACK; + char *cpu_dai_name; + char *name; + + if (j >= sof_uaol_stream_num(mach_params, stream)) + continue; + + name = devm_kasprintf(dev, GFP_KERNEL, + SOF_UAOL_LINK_NAME(stream), i, j); + cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL, + SOF_UAOL_CPU_DAI_NAME(stream), + i, j); + if (!name || !cpu_dai_name) + return -ENOMEM; + + ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, + name, playback, !playback, + cpu_dai_name, "dummy", + snd_soc_dummy_dlc.name, + snd_soc_dummy_dlc.dai_name, + 1, NULL, NULL); + if (ret) + return ret; + + (*dai_links)++; + } + } + } + + return 0; +} + static int create_echoref_dailink(struct snd_soc_card *card, struct snd_soc_dai_link **dai_links, int *be_id) { @@ -1257,7 +1308,7 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) { struct device *dev = card->dev; struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); - int sdw_be_num = 0, ssp_num = 0, dmic_num = 0, bt_num = 0; + int sdw_be_num = 0, ssp_num = 0, dmic_num = 0, bt_num = 0, uaol_num = 0; struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); struct intel_mc_ctx *intel_ctx = (struct intel_mc_ctx *)ctx->private; struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params; @@ -1351,9 +1402,11 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT || mach_params->bt_link_mask) bt_num = 1; - dev_dbg(dev, "DAI link numbers: sdw %d, ssp %d, dmic %d, hdmi %d, bt: %d\n", + uaol_num = sof_uaol_link_count(mach_params); + + dev_dbg(dev, "DAI link numbers: sdw %d, ssp %d, dmic %d, hdmi %d, bt: %d, uaol %d\n", sdw_be_num, ssp_num, dmic_num, - intel_ctx->hdmi.idisp_codec ? hdmi_num : 0, bt_num); + intel_ctx->hdmi.idisp_codec ? hdmi_num : 0, bt_num, uaol_num); codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL); if (!codec_conf) { @@ -1366,7 +1419,7 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) * This should be the last DAI link and it is expected both for monolithic * and functional SOF topologies to support echo reference. */ - num_links = sdw_be_num + ssp_num + dmic_num + hdmi_num + bt_num + 1; + num_links = sdw_be_num + ssp_num + dmic_num + hdmi_num + bt_num + uaol_num + 1; dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL); if (!dai_links) { ret = -ENOMEM; @@ -1415,6 +1468,13 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) goto err_end; } + /* UAOL */ + if (uaol_num) { + ret = create_uaol_dailinks(card, &dai_links, &be_id, mach_params); + if (ret) + goto err_end; + } + /* dummy echo ref link. keep this as the last DAI link. The DAI link ID does not matter */ ret = create_echoref_dailink(card, &dai_links, &be_id); if (ret) { diff --git a/sound/soc/intel/boards/sof_uaol_common.h b/sound/soc/intel/boards/sof_uaol_common.h new file mode 100644 index 00000000000000..e4d10275804823 --- /dev/null +++ b/sound/soc/intel/boards/sof_uaol_common.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright(c) 2026 Intel Corporation. + */ + +#ifndef __SOF_UAOL_COMMON_H +#define __SOF_UAOL_COMMON_H + +#include +#include +#include + +/* + * Each UAOL link has a number of streams per direction, and every one of + * them gets a backend DAI link of its own, unidirectional: the backend + * hw_params fixup is called without the stream direction, so a duplex link + * could not tell which of the two DAI copiers of the topology describes the + * stream being configured. Both names take the UAOL link index and then the + * ordinal of the stream among those of its direction. The ordinal only tells + * the backends apart, it is not the UAOL stream the backend ends up on. + * + * The backend names are abbreviated: the topology's DAI copier widget is + * named "dai-copier.UAOL..", and alsa-lib silently drops + * the widget's tuples once that exceeds 37 characters. + */ +#define SOF_UAOL_LINK_NAME(dir) \ + ((dir) == SNDRV_PCM_STREAM_PLAYBACK ? "UAOL%d-Play%d" : "UAOL%d-Cap%d") +#define SOF_UAOL_CPU_DAI_NAME(dir) \ + ((dir) == SNDRV_PCM_STREAM_PLAYBACK ? "UAOL%d Playback Pin%d" : "UAOL%d Capture Pin%d") + +static inline unsigned int +sof_uaol_stream_num(const struct snd_soc_acpi_mach_params *mach_params, int dir) +{ + return dir == SNDRV_PCM_STREAM_PLAYBACK ? mach_params->uaol_playback_num : + mach_params->uaol_capture_num; +} + +/* Backend DAI links of all the streams of all the UAOL links in the mask */ +static inline unsigned int +sof_uaol_link_count(const struct snd_soc_acpi_mach_params *mach_params) +{ + return hweight32(mach_params->uaol_link_mask) * + (mach_params->uaol_playback_num + mach_params->uaol_capture_num); +} + +#endif /* __SOF_UAOL_COMMON_H */ diff --git a/sound/soc/intel/common/sof-function-topology-lib.c b/sound/soc/intel/common/sof-function-topology-lib.c index 57e089f8e721dc..bef95bd629ebff 100644 --- a/sound/soc/intel/common/sof-function-topology-lib.c +++ b/sound/soc/intel/common/sof-function-topology-lib.c @@ -24,6 +24,7 @@ enum tplg_device_id { TPLG_DEVICE_SSP_BT, TPLG_DEVICE_SSP_HDMI_IN, TPLG_DEVICE_LOOPBACK_VIRTUAL, + TPLG_DEVICE_UAOL, TPLG_DEVICE_MAX }; @@ -213,6 +214,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_ if (get_ssp_tplg_dev(card->dev, dai_link, &hdmi_in_mask, &tplg_dev, &tplg_dev_name) < 0) continue; + } else if (strstr(dai_link->name, "UAOL")) { + tplg_dev = TPLG_DEVICE_UAOL; + tplg_dev_name = "uaol"; } else if (strstr(dai_link->name, "Loopback_Virtual")) { tplg_dev = TPLG_DEVICE_LOOPBACK_VIRTUAL; /* @@ -243,7 +247,8 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_ /* Check presence of sub-topologies */ if (!tplg_files_exist(card->dev, tplg_file)) { devm_kfree(card->dev, tplg_file); - if (best_effort) + /* without its fragment a UAOL link only offers no USB offload */ + if (best_effort || tplg_dev == TPLG_DEVICE_UAOL) continue; return 0; diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c index 6386404a8db8ca..fc52716496dfc5 100644 --- a/sound/soc/soc-usb.c +++ b/sound/soc/soc-usb.c @@ -24,7 +24,7 @@ static struct device_node *snd_soc_find_phandle(struct device *dev) return node; } -static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device_node *node) +static struct snd_soc_usb *snd_soc_usb_ctx_lookup_node(struct device_node *node) { struct snd_soc_usb *ctx; @@ -39,6 +39,25 @@ static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device_node *node) return NULL; } +static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device *dev) +{ + struct snd_soc_usb *ctx; + + if (!dev) + return NULL; + + if (dev->of_node) + return snd_soc_usb_ctx_lookup_node(dev->of_node); + + /* ACPI/PCI platforms have no of_node, match on the device itself */ + list_for_each_entry(ctx, &usb_ctx_list, list) { + if (ctx->component->dev == dev) + return ctx; + } + + return NULL; +} + static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev) { struct snd_soc_usb *ctx; @@ -46,13 +65,153 @@ static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev) node = snd_soc_find_phandle(dev); if (!IS_ERR(node)) { - ctx = snd_soc_usb_ctx_lookup(node); + ctx = snd_soc_usb_ctx_lookup_node(node); of_node_put(node); - } else { - ctx = snd_soc_usb_ctx_lookup(dev->of_node); + + return ctx; } - return ctx ? ctx : NULL; + return snd_soc_usb_ctx_lookup(dev); +} + +/** + * struct snd_soc_usb_route_entry - one (device, pcm) pair offloadable for a + * direction + * @sdev: the device this pcm belongs to + * @pcm_idx: the USB SND PCM device index within @sdev + * @name: human readable name for this entry, borrowed from @sdev, valid only + * as long as @sdev stays in usb->available_devices + */ +struct snd_soc_usb_route_entry { + struct snd_soc_usb_device *sdev; + int pcm_idx; + const char *name; +}; + +/* + * Caller holds ctx_mutex. Walks usb->available_devices in order, treating + * every (device, pcm) pair for @direction across every device as one flat, + * positionally indexed list - this positional index is what the route + * kcontrol's enumerated item number means. Returns false once @index runs + * past the end. + */ +static bool snd_soc_usb_route_entry_at(struct snd_soc_usb *usb, int direction, + unsigned int index, + struct snd_soc_usb_route_entry *entry) +{ + struct snd_soc_usb_device *sdev; + unsigned int *pcm_idx; + const char * const *pcm_names; + int num, i; + + list_for_each_entry(sdev, &usb->available_devices, available_node) { + if (direction == SNDRV_PCM_STREAM_PLAYBACK) { + num = sdev->num_playback; + pcm_idx = sdev->ppcm_idx; + pcm_names = sdev->ppcm_names; + } else { + num = sdev->num_capture; + pcm_idx = sdev->cpcm_idx; + pcm_names = sdev->cpcm_names; + } + + for (i = 0; i < num; i++) { + if (index == 0) { + entry->sdev = sdev; + entry->pcm_idx = pcm_idx[i]; + entry->name = (pcm_names && pcm_names[i]) ? + pcm_names[i] : "USB PCM"; + return true; + } + index--; + } + } + + return false; +} + +/* Caller holds ctx_mutex. -1 if nothing is selected or it is no longer listed. */ +static int snd_soc_usb_route_index_of(struct snd_soc_usb *usb, int direction, + unsigned int route) +{ + struct snd_soc_usb_route *r = &usb->routes[direction][route]; + struct snd_soc_usb_route_entry entry; + unsigned int i; + + if (!r->sdev) + return -1; + + for (i = 0; snd_soc_usb_route_entry_at(usb, direction, i, &entry); i++) { + if (entry.sdev == r->sdev && entry.pcm_idx == r->pcm_idx) + return i; + } + + return -1; +} + +/* + * Caller holds ctx_mutex. Whether a route of @direction other than @route + * is pointed at the pair. Legal, but only one of them can offload it at a + * time, so a route which duplicates another is as good as unassigned when a + * default selection is looked for. + */ +static bool snd_soc_usb_route_taken(struct snd_soc_usb *usb, int direction, + unsigned int route, + const struct snd_soc_usb_route_entry *entry) +{ + unsigned int i; + + for (i = 0; i < usb->num_routes[direction]; i++) { + struct snd_soc_usb_route *r = &usb->routes[direction][i]; + + if (i != route && r->sdev == entry->sdev && r->pcm_idx == entry->pcm_idx) + return true; + } + + return false; +} + +/* Caller holds ctx_mutex. Whether @route points at a pair another route of @direction has. */ +static bool snd_soc_usb_route_duplicate(struct snd_soc_usb *usb, int direction, + unsigned int route) +{ + struct snd_soc_usb_route *r = &usb->routes[direction][route]; + struct snd_soc_usb_route_entry entry = { .sdev = r->sdev, .pcm_idx = r->pcm_idx }; + + return r->sdev && snd_soc_usb_route_taken(usb, direction, route, &entry); +} + +/* + * Caller holds ctx_mutex. The pair @route of @direction gets by default: the + * first available pair no other route of the direction has, so that the + * routes spread over what is connected, or the first available pair at all + * when every pair is taken. @sdev limits the search to one device, NULL + * means any. @spare_only refuses the fallback to a taken pair. + */ +static bool snd_soc_usb_default_entry(struct snd_soc_usb *usb, int direction, + unsigned int route, + const struct snd_soc_usb_device *sdev, + bool spare_only, + struct snd_soc_usb_route_entry *entry) +{ + struct snd_soc_usb_route_entry first = { .sdev = NULL }; + unsigned int i; + + for (i = 0; snd_soc_usb_route_entry_at(usb, direction, i, entry); i++) { + if (sdev && entry->sdev != sdev) + continue; + if (!first.sdev) + first = *entry; + if (!snd_soc_usb_route_taken(usb, direction, route, entry)) + return true; + } + + if (spare_only || !first.sdev) + return false; + + *entry = first; + + return true; } /* SOC USB sound kcontrols */ @@ -127,6 +286,40 @@ int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, } EXPORT_SYMBOL_GPL(snd_soc_usb_update_offload_route); +/** + * snd_soc_usb_get_selected_device() - Get the device/pcm a route points at + * @usb: soc usb port + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * @route: the route of @direction, below usb->num_routes[direction] + * @sdev: set to the routed device, or NULL if nothing is selected + * @pcm_index: set to the routed device's USB SND PCM device index + * + * A backend consults this at stream acquire time instead of picking a device + * itself, so the stream goes to whatever the route kcontrol (or the + * connect-time default) currently points at. + * + * Return: 0 on success (even if nothing is selected - check @sdev), negative + * on error. + */ +int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index) +{ + if (!usb || direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + if (route >= usb->num_routes[direction]) + return -EINVAL; + + guard(mutex)(&ctx_mutex); + *sdev = usb->routes[direction][route].sdev; + *pcm_index = usb->routes[direction][route].pcm_idx; + + return 0; +} +EXPORT_SYMBOL_GPL(snd_soc_usb_get_selected_device); + /** * snd_soc_usb_find_priv_data() - Retrieve private data stored * @usbdev: device reference @@ -172,6 +365,30 @@ int snd_soc_usb_find_supported_format(int card_idx, } EXPORT_SYMBOL_GPL(snd_soc_usb_find_supported_format); +/* Not under ctx_mutex: the port is not yet, or no longer, on usb_ctx_list */ +static int snd_soc_usb_alloc_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes) +{ + struct snd_soc_usb_route *routes; + unsigned int i; + + if (!num_routes) + return -EINVAL; + + routes = kcalloc(num_routes, sizeof(*routes), GFP_KERNEL); + if (!routes) + return -ENOMEM; + + for (i = 0; i < num_routes; i++) + routes[i].pcm_idx = -1; + + kfree(usb->routes[direction]); + usb->routes[direction] = routes; + usb->num_routes[direction] = num_routes; + + return 0; +} + /** * snd_soc_usb_allocate_port() - allocate a SoC USB port for offloading support * @component: USB DPCM backend DAI component @@ -181,13 +398,15 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_find_supported_format); * different USB audio devices attached, in order to start audio offloading handled * by an ASoC entity. USB device plug in/out events are signaled with a * notification, but don't directly impact the memory allocated for the SoC USB - * port. + * port. The port starts out with one route per direction, see + * snd_soc_usb_set_num_routes(). * */ struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *component, void *data) { struct snd_soc_usb *usb; + int i; usb = kzalloc_obj(*usb); if (!usb) @@ -196,10 +415,41 @@ struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *componen usb->component = component; usb->priv_data = data; + INIT_LIST_HEAD(&usb->available_devices); + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { + if (snd_soc_usb_alloc_routes(usb, i, 1)) { + snd_soc_usb_free_port(usb); + return ERR_PTR(-ENOMEM); + } + } + return usb; } EXPORT_SYMBOL_GPL(snd_soc_usb_allocate_port); +/** + * snd_soc_usb_set_num_routes() - Set the number of offload routes of a direction + * @usb: allocated SoC USB port, not yet added with snd_soc_usb_add_port() + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * @num_routes: streams the backend can offload at a time in @direction + * + * A backend running several offloaded streams in a direction at a time gets + * a route, and with snd_soc_usb_setup_offload_route_ctl() a kcontrol, per + * stream, so that each can be pointed at a (device, pcm) pair of its own. + * To be called before the route kcontrols are created and the port is added. + * + * Return: 0 on success, negative on error. + */ +int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes) +{ + if (!usb || direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + return snd_soc_usb_alloc_routes(usb, direction, num_routes); +} +EXPORT_SYMBOL_GPL(snd_soc_usb_set_num_routes); + /** * snd_soc_usb_free_port() - free a SoC USB port used for offloading support * @usb: allocated SoC USB port @@ -210,7 +460,11 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_allocate_port); */ void snd_soc_usb_free_port(struct snd_soc_usb *usb) { + int i; + snd_soc_usb_remove_port(usb); + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) + kfree(usb->routes[i]); kfree(usb); } EXPORT_SYMBOL_GPL(snd_soc_usb_free_port); @@ -256,6 +510,93 @@ void snd_soc_usb_remove_port(struct snd_soc_usb *usb) } EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port); +/* + * Caller holds ctx_mutex. If nothing is currently selected for @route of + * @direction, point it at its default pair, see snd_soc_usb_default_entry(), + * and notify the backend. Reused for a fresh connect and for the disconnect + * fallback below - both are "something in the list has changed, is a default + * selection needed". + * @ctx: USB context containing the route state. + * @direction: PCM stream direction to select. + * @route: the route of @direction to select for. + * Returns true if a selection was actually made, so the caller knows the + * route kcontrol's value (not just its item list) needs to be notified. + */ +static bool snd_soc_usb_auto_select(struct snd_soc_usb *ctx, int direction, + unsigned int route) +{ + struct snd_soc_usb_route *r = &ctx->routes[direction][route]; + struct snd_soc_usb_route_entry entry; + + if (r->sdev) + return false; + + if (!snd_soc_usb_default_entry(ctx, direction, route, NULL, false, &entry)) + return false; + + r->sdev = entry.sdev; + r->pcm_idx = entry.pcm_idx; + + return true; +} + +/* + * Caller holds ctx_mutex. A route which duplicates another route's pair has + * nothing of its own to offload; when a device connects, move such a route + * to a pair of the new device no route has yet, unless a stream is running + * on it. A route pointing at a pair of its own is never changed by a + * connect. + */ +static bool snd_soc_usb_take_over_duplicate(struct snd_soc_usb *ctx, int direction, + unsigned int route, + struct snd_soc_usb_device *sdev) +{ + struct snd_soc_usb_route *r = &ctx->routes[direction][route]; + struct snd_soc_usb_route_entry entry; + + if (!snd_soc_usb_route_duplicate(ctx, direction, route)) + return false; + + if (ctx->is_route_busy && ctx->is_route_busy(ctx, direction, route)) + return false; + + if (!snd_soc_usb_default_entry(ctx, direction, route, sdev, true, &entry)) + return false; + + r->sdev = entry.sdev; + r->pcm_idx = entry.pcm_idx; + + return true; +} + +/* Caller holds ctx_mutex. */ +static void snd_soc_usb_notify_route_changed(struct snd_soc_usb *ctx, int direction, + unsigned int route, bool value_changed) +{ + struct snd_soc_usb_route *r = &ctx->routes[direction][route]; + unsigned int mask = SNDRV_CTL_EVENT_MASK_INFO; + + if (value_changed) + mask |= SNDRV_CTL_EVENT_MASK_VALUE; + + if (r->kctl && ctx->component && ctx->component->card) + snd_ctl_notify(ctx->component->card->snd_card, mask, &r->kctl->id); +} + +/* Caller holds ctx_mutex. Whether @sdev is on the port's available_devices. */ +static bool snd_soc_usb_device_listed(struct snd_soc_usb *ctx, + struct snd_soc_usb_device *sdev) +{ + struct snd_soc_usb_device *entry; + + list_for_each_entry(entry, &ctx->available_devices, available_node) { + if (entry == sdev) + return true; + } + + return false; +} + /** * snd_soc_usb_connect() - Notification of USB device connection * @usbdev: USB bus device @@ -265,26 +606,53 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port); * handle how the DPCM backend selects, which device to enable USB offloading * on. * + * Return: 0 on success, -ENODEV if no port is registered for @usbdev (the + * device is then not listed and must not be reported as disconnected later), + * -EEXIST if @sdev is connected already. */ int snd_soc_usb_connect(struct device *usbdev, struct snd_soc_usb_device *sdev) { struct snd_soc_usb *ctx; + int direction; + int ret = 0; if (!usbdev) return -ENODEV; mutex_lock(&ctx_mutex); ctx = snd_soc_find_usb_ctx(usbdev); - if (!ctx) + if (!ctx) { + ret = -ENODEV; + goto exit; + } + + /* the device is linked into the list through itself, it can only be listed once */ + if (WARN_ON(snd_soc_usb_device_listed(ctx, sdev))) { + ret = -EEXIST; goto exit; + } if (ctx->connection_status_cb) ctx->connection_status_cb(ctx, sdev, true); + list_add_tail(&sdev->available_node, &ctx->available_devices); + + for (direction = 0; direction <= SNDRV_PCM_STREAM_LAST; direction++) { + unsigned int route; + + for (route = 0; route < ctx->num_routes[direction]; route++) { + bool selected = snd_soc_usb_auto_select(ctx, direction, route) || + snd_soc_usb_take_over_duplicate(ctx, direction, + route, sdev); + + snd_soc_usb_notify_route_changed(ctx, direction, route, selected); + } + } + exit: mutex_unlock(&ctx_mutex); - return 0; + return ret; } EXPORT_SYMBOL_GPL(snd_soc_usb_connect); @@ -299,6 +667,7 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_connect); int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sdev) { struct snd_soc_usb *ctx; + int direction; if (!usbdev) return -ENODEV; @@ -311,6 +680,31 @@ int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sde if (ctx->connection_status_cb) ctx->connection_status_cb(ctx, sdev, false); + /* a device the platform never connected has nothing to take back */ + if (!snd_soc_usb_device_listed(ctx, sdev)) + goto exit; + + list_del(&sdev->available_node); + + for (direction = 0; direction <= SNDRV_PCM_STREAM_LAST; direction++) { + unsigned int route; + + for (route = 0; route < ctx->num_routes[direction]; route++) { + struct snd_soc_usb_route *r = &ctx->routes[direction][route]; + bool released = r->sdev == sdev; + bool selected; + + if (released) { + r->sdev = NULL; + r->pcm_idx = -1; + } + + selected = snd_soc_usb_auto_select(ctx, direction, route); + snd_soc_usb_notify_route_changed(ctx, direction, route, + released || selected); + } + } + exit: mutex_unlock(&ctx_mutex); @@ -318,5 +712,154 @@ int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sde } EXPORT_SYMBOL_GPL(snd_soc_usb_disconnect); +/* The kcontrol's private_value carries the direction and the route */ +#define SND_SOC_USB_KCTL_VALUE(direction, route) ((direction) | ((route) << 8)) +#define SND_SOC_USB_KCTL_DIRECTION(value) ((value) & 0xff) +#define SND_SOC_USB_KCTL_ROUTE(value) ((value) >> 8) + +static int snd_soc_usb_route_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct snd_soc_usb *usb = snd_kcontrol_chip(kcontrol); + int direction = SND_SOC_USB_KCTL_DIRECTION(kcontrol->private_value); + struct snd_soc_usb_route_entry entry; + unsigned int count; + + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; + uinfo->count = 1; + + guard(mutex)(&ctx_mutex); + + for (count = 0; snd_soc_usb_route_entry_at(usb, direction, count, &entry); count++) + ; + + /* with no route available the only item is "None" */ + if (!count) { + uinfo->value.enumerated.items = 1; + uinfo->value.enumerated.item = 0; + strscpy(uinfo->value.enumerated.name, "None", + sizeof(uinfo->value.enumerated.name)); + return 0; + } + + uinfo->value.enumerated.items = count; + + if (uinfo->value.enumerated.item >= count) + uinfo->value.enumerated.item = count - 1; + + snd_soc_usb_route_entry_at(usb, direction, uinfo->value.enumerated.item, &entry); + strscpy(uinfo->value.enumerated.name, entry.name, + sizeof(uinfo->value.enumerated.name)); + + return 0; +} + +static int snd_soc_usb_route_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_usb *usb = snd_kcontrol_chip(kcontrol); + int direction = SND_SOC_USB_KCTL_DIRECTION(kcontrol->private_value); + unsigned int route = SND_SOC_USB_KCTL_ROUTE(kcontrol->private_value); + int idx; + + scoped_guard(mutex, &ctx_mutex) + idx = snd_soc_usb_route_index_of(usb, direction, route); + + /* item 0 is "None" when nothing is available */ + ucontrol->value.enumerated.item[0] = idx < 0 ? 0 : idx; + + return 0; +} + +static int snd_soc_usb_route_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_usb *usb = snd_kcontrol_chip(kcontrol); + int direction = SND_SOC_USB_KCTL_DIRECTION(kcontrol->private_value); + unsigned int route = SND_SOC_USB_KCTL_ROUTE(kcontrol->private_value); + unsigned int requested = ucontrol->value.enumerated.item[0]; + struct snd_soc_usb_route *r = &usb->routes[direction][route]; + struct snd_soc_usb_route_entry entry; + + guard(mutex)(&ctx_mutex); + + /* item 0 is "None" when nothing is available, accept it as a no-op */ + if (!snd_soc_usb_route_entry_at(usb, direction, requested, &entry)) + return requested ? -EINVAL : 0; + + if (entry.sdev == r->sdev && entry.pcm_idx == r->pcm_idx) + return 0; + + if (usb->is_route_busy && usb->is_route_busy(usb, direction, route)) + return -EBUSY; + + r->sdev = entry.sdev; + r->pcm_idx = entry.pcm_idx; + + return 1; +} + +static const struct snd_kcontrol_new snd_soc_usb_route_ctl_template = { + .iface = SNDRV_CTL_ELEM_IFACE_CARD, + .info = snd_soc_usb_route_info, + .get = snd_soc_usb_route_get, + .put = snd_soc_usb_route_put, +}; + +/** + * snd_soc_usb_setup_offload_route_ctl() - Create the offload route kcontrols + * @component: USB DPCM backend DAI component + * @usb: soc usb port to attach the controls to + * + * Creates one writable enumerated kcontrol per route of each direction, + * "USB Offload 0 Playback Route", "USB Offload 1 Playback Route", "USB Offload + * 0 Capture Route" and so on, letting userspace pick which of the currently + * connected devices' PCMs the offloaded stream of that route is routed to. + * The number is the route and matches the backend's ordinal, whatever the + * number of routes a direction has. The item list is rebuilt live from + * usb->available_devices on every query, so it always reflects what is + * currently connected; with nothing connected the single item is "None". + * + * Return: 0 on success, negative on error. + */ +int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb) +{ + static const char * const names[] = { + [SNDRV_PCM_STREAM_PLAYBACK] = "Playback", + [SNDRV_PCM_STREAM_CAPTURE] = "Capture", + }; + int direction, ret; + + for (direction = 0; direction <= SNDRV_PCM_STREAM_LAST; direction++) { + unsigned int route; + + for (route = 0; route < usb->num_routes[direction]; route++) { + struct snd_kcontrol_new kctl = snd_soc_usb_route_ctl_template; + char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; + struct snd_kcontrol *kcontrol; + + snprintf(name, sizeof(name), "USB Offload %u %s Route", + route, names[direction]); + + kctl.name = name; + kctl.private_value = SND_SOC_USB_KCTL_VALUE(direction, route); + + kcontrol = snd_ctl_new1(&kctl, usb); + if (!kcontrol) + return -ENOMEM; + + ret = snd_ctl_add(component->card->snd_card, kcontrol); + if (ret < 0) + return ret; + + usb->routes[direction][route].kctl = kcontrol; + } + } + + return 0; +} +EXPORT_SYMBOL_GPL(snd_soc_usb_setup_offload_route_ctl); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("SoC USB driver for offloading"); diff --git a/sound/soc/sof/core.c b/sound/soc/sof/core.c index 1f38fe03e49395..0a2076e1d78021 100644 --- a/sound/soc/sof/core.c +++ b/sound/soc/sof/core.c @@ -825,18 +825,21 @@ EXPORT_SYMBOL(snd_sof_device_shutdown); int sof_machine_register(struct snd_sof_dev *sdev, void *pdata) { struct snd_sof_pdata *plat_data = pdata; - const char *drv_name; - const void *mach; - int size; + struct snd_soc_acpi_mach mach; - drv_name = plat_data->machine->drv_name; - mach = plat_data->machine; - size = sizeof(*plat_data->machine); + /* + * The machine driver gets a copy of the selected machine. The platform + * can adjust the params of that copy with what it only learned from the + * booted firmware, the selected machine itself stays as it was. + */ + memcpy(&mach, plat_data->machine, sizeof(mach)); + if (sof_ops(sdev)->finalize_mach_params) + sof_ops(sdev)->finalize_mach_params(sdev, &mach.mach_params); /* register machine driver, pass machine info as pdata */ plat_data->pdev_mach = - platform_device_register_data(sdev->dev, drv_name, - PLATFORM_DEVID_NONE, mach, size); + platform_device_register_data(sdev->dev, mach.drv_name, + PLATFORM_DEVID_NONE, &mach, sizeof(mach)); if (IS_ERR(plat_data->pdev_mach)) return PTR_ERR(plat_data->pdev_mach); diff --git a/sound/soc/sof/intel/Kconfig b/sound/soc/sof/intel/Kconfig index 02b41e4ccab264..5b709709a5bf83 100644 --- a/sound/soc/sof/intel/Kconfig +++ b/sound/soc/sof/intel/Kconfig @@ -406,6 +406,22 @@ config SND_SOC_SOF_INTEL_SOUNDWIRE Say Y if you want to enable SoundWire links with SOF. If unsure select "N". +config SND_SOC_SOF_INTEL_USB_OFFLOAD + bool "SOF support for USB audio offload" + depends on SND_SOC_SOF_HDA_GENERIC + depends on SND_SOC_SOF_HDA_LINK + depends on SND_USB_OFFLOAD + depends on SND_SOC_USB + depends on !(SND_USB_OFFLOAD=m && SND_SOC_SOF_HDA_COMMON=y) + depends on !(SND_SOC_USB=m && SND_SOC_SOF_HDA_COMMON=y) + help + This adds support for USB audio offload with Sound Open Firmware + for Intel(R) platforms. The DSP reaches the xHCI controller over a + USB Audio Offload Link (UAOL) and drives the endpoint itself, so + audio keeps playing while the host stays idle. + Say Y if you want to enable USB audio offload with SOF. + If unsure select "N". + endif ## SND_SOC_SOF_PCI endif ## SND_SOC_SOF_INTEL_TOPLEVEL diff --git a/sound/soc/sof/intel/Makefile b/sound/soc/sof/intel/Makefile index cc9783e933f8e5..5cb8326eecb686 100644 --- a/sound/soc/sof/intel/Makefile +++ b/sound/soc/sof/intel/Makefile @@ -15,6 +15,7 @@ snd-sof-intel-hda-mlink-y := hda-mlink.o snd-sof-intel-hda-sdw-bpt-objs := hda-sdw-bpt.o snd-sof-intel-hda-common-$(CONFIG_SND_SOC_SOF_HDA_PROBES) += hda-probes.o +snd-sof-intel-hda-common-$(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) += hda-uaol.o snd-sof-intel-hda-y := hda-codec.o diff --git a/sound/soc/sof/intel/hda-common-ops.c b/sound/soc/sof/intel/hda-common-ops.c index aa91f2b6fd657b..0912b8a1491e71 100644 --- a/sound/soc/sof/intel/hda-common-ops.c +++ b/sound/soc/sof/intel/hda-common-ops.c @@ -40,9 +40,10 @@ const struct snd_sof_dsp_ops sof_hda_common_ops = { /* machine driver */ .machine_select = hda_machine_select, - .machine_register = sof_machine_register, + .machine_register = hda_machine_register, .machine_unregister = sof_machine_unregister, .set_mach_params = hda_set_mach_params, + .finalize_mach_params = hda_finalize_mach_params, /* debug */ .dbg_dump = hda_dsp_dump, diff --git a/sound/soc/sof/intel/hda-dai-ops.c b/sound/soc/sof/intel/hda-dai-ops.c index c0f843d0062b67..47fd81fa1ee7a4 100644 --- a/sound/soc/sof/intel/hda-dai-ops.c +++ b/sound/soc/sof/intel/hda-dai-ops.c @@ -43,7 +43,7 @@ static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd, static struct hdac_ext_stream * hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream, - enum hda_bus_ml_link_type link_type) + enum hda_bus_ml_link_type link_type, int stream_dir) { struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); struct sof_intel_hda_dev *sof_hda = bus_to_sof_hda(bus); @@ -52,7 +52,6 @@ hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream struct snd_sof_dev *sdev; struct hdac_ext_stream *res = NULL; struct hdac_stream *hstream = NULL; - int stream_dir = substream->stream; bool is_multi = link_type == HDA_BUS_ML_LINK_HDA || link_type == HDA_BUS_ML_LINK_UAOL; bool is_play = stream_dir == SNDRV_PCM_STREAM_PLAYBACK; bool is_sdw = link_type == HDA_BUS_ML_LINK_SDW; @@ -92,7 +91,7 @@ hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream list_for_each_entry(hstream, &bus->stream_list, list) { struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream); - if (hstream->direction != substream->stream) + if (hstream->direction != stream_dir) continue; hda_stream = hstream_to_sof_hda_stream(hext_stream); @@ -160,6 +159,71 @@ hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream return res; } +/* Undo hda_link_stream_assign(); the stream index is already out of use */ +static void hda_link_stream_release(struct snd_sof_dev *sdev, + struct hdac_ext_stream *hext_stream, int dir) +{ + struct sof_intel_hda_dev *sof_hda = sdev->pdata->hw_pdata; + struct hdac_bus *bus = sof_to_bus(sdev); + unsigned int stream_idx = hext_stream->hstream.stream_tag - 1; + + /* + * Drop the stream index from the per-direction active concurrency masks. + * The two masks are mutually exclusive for a given stream/direction + * (and a stream of the SSP/DMIC kind appears in neither), so a blind + * clear of both is safe and lets us avoid having to remember the + * link type at allocation time. + */ + scoped_guard(spinlock_irq, &bus->reg_lock) { + sof_hda->link_dma_active_sdw_mask[dir] &= ~BIT(stream_idx); + sof_hda->link_dma_active_multi_mask[dir] &= ~BIT(stream_idx); + } + + snd_hdac_ext_stream_release(hext_stream, HDAC_EXT_STREAM_TYPE_LINK); +} + +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) + +/* + * A UAOL playback stream with an explicit feedback endpoint needs a second + * link DMA, in the capture direction, for the DSP to read the feedback + * through. It belongs to the playback substream but is not the DAI's DMA + * data and is not a stream of its own, so it is not registered as the + * link_substream either - hda_dai_suspend() has nothing to do with it. + */ +struct hdac_ext_stream *hda_uaol_assign_fb_link_stream(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream, + unsigned int format_val) +{ + struct hdac_ext_stream *hext_stream; + + hext_stream = hda_link_stream_assign(sof_to_bus(sdev), substream, + HDA_BUS_ML_LINK_UAOL, SNDRV_PCM_STREAM_CAPTURE); + if (!hext_stream) + return NULL; + + hext_stream->link_substream = NULL; + + snd_hdac_ext_stream_reset(hext_stream); + snd_hdac_ext_stream_setup(hext_stream, format_val); + + return hext_stream; +} + +void hda_uaol_release_fb_link_stream(struct snd_sof_dev *sdev, + struct hdac_ext_stream *hext_stream) +{ + struct sof_intel_hda_stream *hda_stream = hstream_to_sof_hda_stream(hext_stream); + + snd_hdac_ext_stream_clear(hext_stream); + hda_link_stream_release(sdev, hext_stream, SNDRV_PCM_STREAM_CAPTURE); + + /* free the host DMA channel reserved by the hostless stream */ + hda_stream->host_reserved = 0; +} + +#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) */ + static struct hdac_ext_stream *hda_get_hext_stream(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu_dai, struct snd_pcm_substream *substream) @@ -195,7 +259,8 @@ static struct hdac_ext_stream *hda_assign_hext_stream(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream; enum hda_bus_ml_link_type link_type = hda_bus_ml_link_get_type(hlink); - hext_stream = hda_link_stream_assign(sof_to_bus(sdev), substream, link_type); + hext_stream = hda_link_stream_assign(sof_to_bus(sdev), substream, link_type, + substream->stream); if (!hext_stream) return NULL; @@ -208,25 +273,9 @@ static void hda_release_hext_stream(struct snd_sof_dev *sdev, struct snd_soc_dai struct snd_pcm_substream *substream) { struct hdac_ext_stream *hext_stream = hda_get_hext_stream(sdev, cpu_dai, substream); - struct sof_intel_hda_dev *sof_hda = sdev->pdata->hw_pdata; - struct hdac_bus *bus = sof_to_bus(sdev); - int dir = substream->stream; - unsigned int stream_idx = hext_stream->hstream.stream_tag - 1; - - /* - * Drop the stream index from the per-direction active concurrency masks. - * The two masks are mutually exclusive for a given stream/direction - * (and a stream of the SSP/DMIC kind appears in neither), so a blind - * clear of both is safe and lets us avoid having to remember the - * link type at allocation time. - */ - scoped_guard(spinlock_irq, &bus->reg_lock) { - sof_hda->link_dma_active_sdw_mask[dir] &= ~BIT(stream_idx); - sof_hda->link_dma_active_multi_mask[dir] &= ~BIT(stream_idx); - } snd_soc_dai_set_dma_data(cpu_dai, substream, NULL); - snd_hdac_ext_stream_release(hext_stream, HDAC_EXT_STREAM_TYPE_LINK); + hda_link_stream_release(sdev, hext_stream, substream->stream); } static void hda_setup_hext_stream(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream, @@ -414,6 +463,14 @@ hda_ipc4_find_owning_pipe_widget(struct snd_pcm_substream *substream) return NULL; } +static struct hdac_ext_link *uaol_get_hlink(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream) +{ + struct hdac_bus *bus = sof_to_bus(sdev); + + return hdac_bus_eml_uaol_get_hlink(bus); +} + static int hda_ipc4_pre_trigger(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu_dai, struct snd_pcm_substream *substream, int cmd) { @@ -632,6 +689,19 @@ static const struct hda_dai_widget_dma_ops dmic_ipc4_dma_ops = { .get_hlink = dmic_get_hlink, }; +static const struct hda_dai_widget_dma_ops uaol_ipc4_dma_ops = { + .get_hext_stream = hda_ipc4_get_hext_stream, + .assign_hext_stream = hda_assign_hext_stream, + .release_hext_stream = hda_release_hext_stream, + .setup_hext_stream = hda_setup_hext_stream, + .reset_hext_stream = hda_reset_hext_stream, + .pre_trigger = hda_ipc4_pre_trigger, + .trigger = hda_trigger, + .post_trigger = hda_ipc4_post_trigger, + .calc_stream_format = generic_calc_stream_format, + .get_hlink = uaol_get_hlink, +}; + static const struct hda_dai_widget_dma_ops sdw_ipc4_dma_ops = { .get_hext_stream = hda_ipc4_get_hext_stream, .assign_hext_stream = hda_assign_hext_stream, @@ -798,6 +868,10 @@ hda_select_dai_widget_ops(struct snd_sof_dev *sdev, struct snd_sof_widget *swidg if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) return NULL; return &dmic_ipc4_dma_ops; + case SOF_DAI_INTEL_UAOL: + if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) + return NULL; + return &uaol_ipc4_dma_ops; case SOF_DAI_INTEL_ALH: if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) return NULL; diff --git a/sound/soc/sof/intel/hda-dai.c b/sound/soc/sof/intel/hda-dai.c index 7a8875a885ab34..86048893f62807 100644 --- a/sound/soc/sof/intel/hda-dai.c +++ b/sound/soc/sof/intel/hda-dai.c @@ -58,14 +58,6 @@ EXPORT_SYMBOL_NS(hda_dai_config, "SND_SOC_SOF_INTEL_HDA_COMMON"); #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK) -static struct snd_sof_dev *dai_to_sdev(struct snd_pcm_substream *substream, - struct snd_soc_dai *cpu_dai) -{ - struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); - - return widget_to_sdev(w); -} - static const struct hda_dai_widget_dma_ops * hda_dai_get_ops(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) { @@ -220,8 +212,8 @@ static int hda_link_dma_hw_params(struct snd_pcm_substream *substream, return 0; } -static int __maybe_unused hda_dai_hw_free(struct snd_pcm_substream *substream, - struct snd_soc_dai *cpu_dai) +int hda_dai_hw_free(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) { const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai); struct hdac_ext_stream *hext_stream; @@ -312,8 +304,8 @@ static bool hda_dai_has_wov_fe(struct snd_sof_dev *sdev, struct snd_pcm_substrea * In contrast to IPC3, the dai trigger in IPC4 mixes pipeline state changes * (over IPC channel) and DMA state change (direct host register changes). */ -static int __maybe_unused hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, - struct snd_soc_dai *dai) +int hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *dai) { const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, dai); struct hdac_ext_stream *hext_stream; @@ -407,15 +399,6 @@ static const struct snd_soc_dai_ops hda_dai_ops = { #endif -static struct sof_ipc4_copier *widget_to_copier(struct snd_soc_dapm_widget *w) -{ - struct snd_sof_widget *swidget = w->dobj.private; - struct snd_sof_dai *sdai = swidget->private; - struct sof_ipc4_copier *ipc4_copier = (struct sof_ipc4_copier *)sdai->private; - - return ipc4_copier; -} - static int non_hda_dai_hw_params_data(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *cpu_dai, @@ -504,9 +487,9 @@ static int non_hda_dai_hw_params_data(struct snd_pcm_substream *substream, return 0; } -static int non_hda_dai_hw_params(struct snd_pcm_substream *substream, - struct snd_pcm_hw_params *params, - struct snd_soc_dai *cpu_dai) +int non_hda_dai_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *cpu_dai) { struct snd_sof_dai_config_data data = { 0 }; unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS; @@ -514,8 +497,8 @@ static int non_hda_dai_hw_params(struct snd_pcm_substream *substream, return non_hda_dai_hw_params_data(substream, params, cpu_dai, &data, flags); } -static int non_hda_dai_prepare(struct snd_pcm_substream *substream, - struct snd_soc_dai *cpu_dai) +int non_hda_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) { struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); int stream = substream->stream; @@ -804,6 +787,7 @@ void hda_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) ssp_set_dai_drv_ops(sdev, ops); dmic_set_dai_drv_ops(sdev, ops); + hda_uaol_set_dai_drv_ops(sdev, ops); if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4 && !hda_use_tplg_nhlt) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; @@ -1004,7 +988,47 @@ struct snd_soc_dai_driver skl_dai[] = { }, }, #endif +/* + * The number of DAIs the component registers is a prefix of this array, so the + * UAOL DAIs are kept last: an unconditional entry in the middle would push the + * DAIs after it out of the range a configuration not counting the UAOL ones + * registers. The pin number tells the DAIs of a direction apart, it is not the + * UAOL stream the DAI runs on: that is picked when a USB device is acquired. + */ +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) +{ + .name = "UAOL0 Playback Pin0", + .playback = { + .channels_min = 1, + .channels_max = 8, + }, +}, +{ + .name = "UAOL0 Playback Pin1", + .playback = { + .channels_min = 1, + .channels_max = 8, + }, +}, +{ + .name = "UAOL0 Capture Pin0", + .capture = { + .channels_min = 1, + .channels_max = 8, + }, +}, +{ + .name = "UAOL0 Capture Pin1", + .capture = { + .channels_min = 1, + .channels_max = 8, + }, +}, +#endif }; + +static_assert(ARRAY_SIZE(skl_dai) == SOF_SKL_NUM_DAIS, + "SOF_SKL_NUM_DAIS does not cover skl_dai[]"); EXPORT_SYMBOL_NS(skl_dai, "SND_SOC_SOF_INTEL_HDA_COMMON"); int hda_dsp_dais_suspend(struct snd_sof_dev *sdev) diff --git a/sound/soc/sof/intel/hda-mlink.c b/sound/soc/sof/intel/hda-mlink.c index 6f02fb5b70cedc..1ddd214f56852e 100644 --- a/sound/soc/sof/intel/hda-mlink.c +++ b/sound/soc/sof/intel/hda-mlink.c @@ -997,6 +997,18 @@ struct hdac_ext_link *hdac_bus_eml_sdw_get_hlink(struct hdac_bus *bus) } EXPORT_SYMBOL_NS(hdac_bus_eml_sdw_get_hlink, "SND_SOC_SOF_HDA_MLINK"); +struct hdac_ext_link *hdac_bus_eml_uaol_get_hlink(struct hdac_bus *bus) +{ + struct hdac_ext2_link *h2link; + + h2link = find_ext2_link(bus, true, AZX_REG_ML_LEPTR_ID_INTEL_UAOL); + if (!h2link) + return NULL; + + return &h2link->hext_link; +} +EXPORT_SYMBOL_NS(hdac_bus_eml_uaol_get_hlink, "SND_SOC_SOF_HDA_MLINK"); + void hdac_bus_eml_enable_offload(struct hdac_bus *bus, bool alt, int elid, bool enable) { struct hdac_ext2_link *h2link; diff --git a/sound/soc/sof/intel/hda-pcm.c b/sound/soc/sof/intel/hda-pcm.c index 70abff12fef0ee..fd521546e190d4 100644 --- a/sound/soc/sof/intel/hda-pcm.c +++ b/sound/soc/sof/intel/hda-pcm.c @@ -71,6 +71,8 @@ u32 hda_dsp_get_mult_div(struct snd_sof_dev *sdev, int rate) return SDnFMT_BASE(1) | SDnFMT_MULT(4); case 192000: return SDnFMT_MULT(4); + case 384000: + return SDnFMT_MULT(8); default: dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n", rate); @@ -347,6 +349,14 @@ int hda_dsp_pcm_open(struct snd_sof_dev *sdev, !spcm->stream[substream->stream].pause_supported)) runtime->hw.info &= ~SNDRV_PCM_INFO_PAUSE; + /* + * The backends this frontend can reach are resolved before it is opened, + * and everything the core does to runtime->hw afterwards only narrows + * it, so this is the place to bring in a capability which is only known + * at runtime, like what a UAOL backend can carry. + */ + hda_apply_constraint_from_be(sdev, substream); + if (hda_always_enable_dmi_l1 || direction == SNDRV_PCM_STREAM_PLAYBACK || spcm->stream[substream->stream].d0i3_compatible) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c new file mode 100644 index 00000000000000..61c0bee3949745 --- /dev/null +++ b/sound/soc/sof/intel/hda-uaol.c @@ -0,0 +1,1340 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) +// +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// Copyright(c) 2026 Intel Corporation +// +// USB Audio Offload Link (UAOL) DAIs: the DSP drives a USB audio endpoint +// over the UAOL instead of the host. +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../ipc4-priv.h" +#include "../ipc4-topology.h" +#include "../sof-priv.h" +#include "../sof-audio.h" +#include "hda.h" + +/** + * struct hda_uaol_dev - an offload capable USB audio device + * @sdev: the device identity handed to the ASoC USB framework + * @udev: USB device this record stands for, the key used on disconnect + * @node: link on hdev->uaol_devices, this backend's own record of what it + * has allocated - not the routing list, see hdev->uaol_usb's + * available_devices for that + * @ppcm: backing store for @sdev.ppcm_idx + * @cpcm: backing store for @sdev.cpcm_idx + * @ppcm_names: backing store for @sdev.ppcm_names + * @cpcm_names: backing store for @sdev.cpcm_names + * @ppcm_name_storage: string storage @ppcm_names points into + * @cpcm_name_storage: string storage @cpcm_names points into + * + * The offload library reports the identity as a value, while the ASoC USB + * framework wants a list node it can keep, so the record is owned here. + */ +struct hda_uaol_dev { + struct snd_soc_usb_device sdev; + struct usb_device *udev; + struct list_head node; + unsigned int ppcm[USB_OFFLOAD_MAX_PCM]; + unsigned int cpcm[USB_OFFLOAD_MAX_PCM]; + const char *ppcm_names[USB_OFFLOAD_MAX_PCM]; + const char *cpcm_names[USB_OFFLOAD_MAX_PCM]; + char ppcm_name_storage[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; + char cpcm_name_storage[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; +}; + +/* Caller holds hdev->uaol_mutex. */ +static struct hda_uaol_dev *hda_uaol_find(struct sof_intel_hda_dev *hdev, + struct usb_device *udev) +{ + struct hda_uaol_dev *dev; + + list_for_each_entry(dev, &hdev->uaol_devices, node) { + if (dev->udev == udev) + return dev; + } + + return NULL; +} + +/* + * The identity is everything the port is told about the device; a re-notify + * with all of it unchanged carries nothing new. + */ +static bool hda_uaol_dev_identity_equal(const struct hda_uaol_dev *a, + const struct hda_uaol_dev *b) +{ + return a->sdev.card_idx == b->sdev.card_idx && + a->sdev.chip_idx == b->sdev.chip_idx && + a->sdev.num_playback == b->sdev.num_playback && + a->sdev.num_capture == b->sdev.num_capture && + !memcmp(a->ppcm, b->ppcm, sizeof(a->ppcm)) && + !memcmp(a->cpcm, b->cpcm, sizeof(a->cpcm)) && + !memcmp(a->ppcm_name_storage, b->ppcm_name_storage, + sizeof(a->ppcm_name_storage)) && + !memcmp(a->cpcm_name_storage, b->cpcm_name_storage, + sizeof(a->cpcm_name_storage)); +} + +/* + * Whether a stream is already open on @route of @direction, consulted by the + * route kcontrol before it lets a selection change - a link a running stream + * owns is never switched out from under it. + */ +static bool hda_uaol_is_route_busy(struct snd_soc_usb *usb, int direction, + unsigned int route) +{ + struct sof_intel_hda_dev *hdev = usb->priv_data; + int i; + + for (i = 0; i < ARRAY_SIZE(hdev->uaol_streams); i++) { + struct hda_uaol_stream *stream = &hdev->uaol_streams[i]; + + if (stream->dai && stream->direction == direction && + stream->route == route && stream->udev) + return true; + } + + return false; +} + +/* The backend state of a UAOL CPU DAI, claimed when the DAI probed */ +static struct hda_uaol_stream *hda_uaol_stream_get(struct sof_intel_hda_dev *hdev, + struct snd_soc_dai *dai) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(hdev->uaol_streams); i++) { + if (hdev->uaol_streams[i].dai == dai) + return &hdev->uaol_streams[i]; + } + + return NULL; +} + +int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id) +{ + struct snd_sof_dev *sdev = dev_get_drvdata(dev); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_dev *offload_dev, *old = NULL; + bool published = false; + int ret; + + offload_dev = kzalloc_obj(*offload_dev, GFP_KERNEL); + if (!offload_dev) + return -ENOMEM; + + offload_dev->udev = udev; + INIT_LIST_HEAD(&offload_dev->sdev.available_node); + offload_dev->sdev.card_idx = id->card_idx; + offload_dev->sdev.chip_idx = id->chip_idx; + offload_dev->sdev.num_playback = id->num_playback; + offload_dev->sdev.num_capture = id->num_capture; + memcpy(offload_dev->ppcm, id->ppcm_idx, sizeof(offload_dev->ppcm)); + memcpy(offload_dev->cpcm, id->cpcm_idx, sizeof(offload_dev->cpcm)); + memcpy(offload_dev->ppcm_name_storage, id->ppcm_names, + sizeof(offload_dev->ppcm_name_storage)); + memcpy(offload_dev->cpcm_name_storage, id->cpcm_names, + sizeof(offload_dev->cpcm_name_storage)); + for (int i = 0; i < USB_OFFLOAD_MAX_PCM; i++) { + offload_dev->ppcm_names[i] = offload_dev->ppcm_name_storage[i]; + offload_dev->cpcm_names[i] = offload_dev->cpcm_name_storage[i]; + } + offload_dev->sdev.ppcm_idx = offload_dev->ppcm; + offload_dev->sdev.cpcm_idx = offload_dev->cpcm; + offload_dev->sdev.ppcm_names = offload_dev->ppcm_names; + offload_dev->sdev.cpcm_names = offload_dev->cpcm_names; + + scoped_guard(mutex, &hdev->uaol_mutex) { + /* + * Without a card there is nothing to attach the device to. The + * port rediscovers the already enumerated devices when it comes + * up, so the event is not lost. + */ + if (!hdev->uaol_usb) { + kfree(offload_dev); + return 0; + } + + /* + * A notification for a listed device carries its updated + * identity: the PCM list grows as further interfaces of a + * multi-function device are probed. Take the stale entry off + * the list, it is owned here once unlinked, as on disconnect, + * and is replaced below. + */ + old = hda_uaol_find(hdev, udev); + if (old) { + if (hda_uaol_dev_identity_equal(old, offload_dev)) { + kfree(offload_dev); + return 0; + } + list_del(&old->node); + } + } + + if (old) { + snd_soc_usb_disconnect(sdev->dev, &old->sdev); + kfree(old); + } + + /* + * The port can go away with the card between the check and here, in + * which case the connect fails with no port to attach to. The device + * is published on uaol_devices only once connected: an entry not on + * the list cannot be freed by the card removal, so the connect always + * runs on memory owned here alone. + */ + ret = snd_soc_usb_connect(sdev->dev, &offload_dev->sdev); + if (ret) { + kfree(offload_dev); + return ret; + } + + scoped_guard(mutex, &hdev->uaol_mutex) { + /* + * The card removal already passed over this device if the port + * went away since the connect; take the device back instead of + * listing it, the port rediscovers it when it comes back up. + */ + if (hdev->uaol_usb) { + list_add_tail(&offload_dev->node, &hdev->uaol_devices); + if (hdev->uaol_jack.jack) + snd_jack_report(hdev->uaol_jack.jack, SND_JACK_USB); + published = true; + } + } + if (!published) { + snd_soc_usb_disconnect(sdev->dev, &offload_dev->sdev); + kfree(offload_dev); + } + + return 0; +} +EXPORT_SYMBOL_NS(hda_uaol_notify_connect, "SND_SOC_SOF_INTEL_HDA_COMMON"); + +/* + * Nothing else tells a frontend that the device it plays to has been unplugged: + * the link keeps running into a dead endpoint and the application keeps + * feeding it. End its stream the way the USB card ends its own users' streams + * when it goes away, so the application sees the device disappear instead of + * silence. + */ +static void hda_uaol_stop_frontends(struct sof_intel_hda_dev *hdev, + struct usb_device *udev) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(hdev->uaol_streams); i++) { + struct hda_uaol_stream *stream = &hdev->uaol_streams[i]; + struct snd_pcm_substream *be_substream = READ_ONCE(stream->be); + int dir = stream->direction; + struct snd_soc_pcm_runtime *be; + struct snd_soc_dpcm *dpcm; + + if (!be_substream) + continue; + + be = snd_soc_substream_to_rtd(be_substream); + + /* the backend's hw_params and hw_free run under this, so the pair is stable */ + snd_soc_dpcm_mutex_lock(be); + if (stream->udev != udev || stream->be != be_substream) { + snd_soc_dpcm_mutex_unlock(be); + continue; + } + + for_each_dpcm_fe(be, dir, dpcm) { + struct snd_pcm_substream *fe_substream = + snd_soc_dpcm_get_substream(dpcm->fe, dir); + + snd_pcm_stream_lock_irq(fe_substream); + if (fe_substream->runtime) { + if (snd_pcm_running(fe_substream)) + snd_pcm_stop(fe_substream, SNDRV_PCM_STATE_DISCONNECTED); + else + __snd_pcm_set_state(fe_substream->runtime, + SNDRV_PCM_STATE_DISCONNECTED); + } + snd_pcm_stream_unlock_irq(fe_substream); + } + + /* + * The library has already given the USB side back and the device + * goes away with the disconnect. Forget it, so that the trigger and + * hw_free of the frontends' teardown do not hand the stale pointer + * back to the library, where a device reusing the address would + * match. + */ + stream->udev = NULL; + stream->be = NULL; + snd_soc_dpcm_mutex_unlock(be); + } +} + +int hda_uaol_notify_disconnect(struct device *dev, struct usb_device *udev) +{ + struct snd_sof_dev *sdev = dev_get_drvdata(dev); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_dev *offload_dev; + bool available; + + hda_uaol_stop_frontends(hdev, udev); + + scoped_guard(mutex, &hdev->uaol_mutex) { + offload_dev = hda_uaol_find(hdev, udev); + if (offload_dev) + list_del(&offload_dev->node); + available = !list_empty(&hdev->uaol_devices); + } + if (!offload_dev) + return 0; + + snd_soc_usb_disconnect(sdev->dev, &offload_dev->sdev); + kfree(offload_dev); + + /* + * The jack follows the list and not this one event, so that removing + * one of several connected devices still leaves it reported. + */ + scoped_guard(mutex, &hdev->uaol_mutex) { + if (hdev->uaol_jack.jack) + snd_jack_report(hdev->uaol_jack.jack, available ? SND_JACK_USB : 0); + } + + return 0; +} +EXPORT_SYMBOL_NS(hda_uaol_notify_disconnect, "SND_SOC_SOF_INTEL_HDA_COMMON"); + +/* + * The ASoC USB port is published once the UAOL DAI is bound into a card, + * which is the point where an offloaded device has somewhere to be routed. + * The firmware has booted long before that, so the offload library cannot + * do this itself. + */ +static int uaol_dai_probe(struct snd_soc_dai *dai) +{ + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream; + struct snd_soc_usb *usb; + unsigned int link, route; + int direction, ret, i; + + /* + * The pin number of the DAI, "UAOL0 Playback Pin1", is the ordinal of + * the backend among those of its direction, which is the ASoC USB + * route it plays to. + */ + if (sscanf(dai->driver->name, "UAOL%u %*s Pin%u", &link, &route) != 2) + return -EINVAL; + direction = dai->driver->playback.channels_min ? SNDRV_PCM_STREAM_PLAYBACK : + SNDRV_PCM_STREAM_CAPTURE; + + /* a free slot has no DAI */ + stream = hda_uaol_stream_get(hdev, NULL); + if (!stream) + return -ENOSPC; + stream->hdev = hdev; + stream->dai = dai; + stream->direction = direction; + stream->route = route; + + if (!hdev->uaol) { + /* + * The UAOL PCMs exist when the controller and firmware + * support offload, even if the offload library itself failed to + * probe. Give userspace a jack it can read instead of a control + * that silently doesn't exist. + */ + if (hdev->uaol_jack.jack) + return 0; + + ret = snd_soc_card_jack_new(dai->component->card, "USB Offload Jack", + SND_JACK_USB, &hdev->uaol_jack); + if (ret) + goto err_free_slot; + + snd_jack_report(hdev->uaol_jack.jack, 0); + return 0; + } + + /* the port belongs to the controller, every UAOL CPU DAI probes here */ + if (hdev->uaol_usb) + return 0; + + usb = snd_soc_usb_allocate_port(dai->component, hdev); + if (IS_ERR(usb)) { + ret = PTR_ERR(usb); + goto err_free_slot; + } + + usb->is_route_busy = hda_uaol_is_route_busy; + + /* a route per backend of a direction, each with a device of its own */ + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { + ret = snd_soc_usb_set_num_routes(usb, i, max(hdev->uaol_stream_num[i], 1U)); + if (ret) + goto err_free_port; + } + + /* + * The UAOL PCMs exist whether or not a device is there to play to, and + * nothing else tells userspace which of the two it is. The jack has to + * be in place before the port goes up, as that is what reports the + * devices which are already connected. + */ + ret = snd_soc_card_jack_new(dai->component->card, "USB Offload Jack", + SND_JACK_USB, &hdev->uaol_jack); + if (ret) + goto err_free_port; + + ret = snd_soc_usb_setup_offload_route_ctl(dai->component, usb); + if (ret) + goto err_free_port; + + scoped_guard(mutex, &hdev->uaol_mutex) + hdev->uaol_usb = usb; + + /* rediscovers the devices which enumerated before the card existed */ + snd_soc_usb_add_port(usb); + + return 0; + +err_free_port: + snd_soc_usb_free_port(usb); +err_free_slot: + /* ASoC does not call remove for a DAI whose probe failed */ + stream->dai = NULL; + return ret; +} + +static int uaol_dai_remove(struct snd_soc_dai *dai) +{ + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream = hda_uaol_stream_get(hdev, dai); + struct hda_uaol_dev *dev, *tmp; + struct snd_soc_usb *usb; + + if (stream) + stream->dai = NULL; + + /* + * Taken away under the lock so that a connect racing with the card + * going away sees no port to attach to. The jack itself belongs to the + * card and goes away with it. + */ + scoped_guard(mutex, &hdev->uaol_mutex) { + usb = hdev->uaol_usb; + hdev->uaol_usb = NULL; + hdev->uaol_jack.jack = NULL; + } + if (!usb) + return 0; + + snd_soc_usb_remove_port(usb); + snd_soc_usb_free_port(usb); + + /* the port is gone, no disconnect event will report these anymore */ + scoped_guard(mutex, &hdev->uaol_mutex) { + list_for_each_entry_safe(dev, tmp, &hdev->uaol_devices, node) { + list_del(&dev->node); + kfree(dev); + } + } + + return 0; +} + +/* + * Which device a backend plays to is the ASoC USB route kcontrol's job to + * decide, per route (defaulting to the first connected pair no other route + * of the direction has, see snd_soc_usb_connect()'s auto-selection) - this + * just resolves that selection back to the Intel-side record that carries + * the actual usb_device pointer. + */ +static struct usb_device *hda_uaol_selected(struct sof_intel_hda_dev *hdev, + int direction, unsigned int route, + int *pcm_index) +{ + struct snd_soc_usb_device *sdev; + struct hda_uaol_dev *dev; + int ret; + + ret = snd_soc_usb_get_selected_device(hdev->uaol_usb, direction, route, + &sdev, pcm_index); + if (ret || !sdev) + return NULL; + + scoped_guard(mutex, &hdev->uaol_mutex) { + list_for_each_entry(dev, &hdev->uaol_devices, node) { + if (&dev->sdev == sdev) + return dev->udev; + } + } + + return NULL; +} + +/* + * The gateway stages a service interval worth of payload in the link FIFO, so + * the space a stream takes is its byte rate per interval. Two intervals have to + * fit for the DSP to be able to refill the FIFO while the link drains it. + */ +#define SOF_UAOL_FIFO_INTERVALS 2 + +/* + * The gateway is programmed with the valid bits of the format and the link + * packs the samples to that width on the wire, so the container is not what + * occupies the FIFO. + */ +static unsigned int uaol_sample_bytes(snd_pcm_format_t format) +{ + int width = snd_pcm_format_width(format); + + return width > 0 ? DIV_ROUND_UP(width, BITS_PER_BYTE) : 0; +} + +static unsigned int uaol_min_sample_bytes(const struct snd_mask *formats) +{ + unsigned int min_bytes = UINT_MAX; + snd_pcm_format_t format; + + pcm_for_each_format(format) { + unsigned int bytes; + + if (!snd_mask_test_format(formats, format)) + continue; + + bytes = uaol_sample_bytes(format); + if (bytes) + min_bytes = min(min_bytes, bytes); + } + + return min_bytes == UINT_MAX ? 0 : min_bytes; +} + +/* + * The budget is one dimensional in the byte rate, so the bound it puts on any + * one of the three parameters depends on the other two. Taking the smallest + * value the other two can still take keeps each bound the loosest one that is + * valid, and the refining converges on the combinations that fit. + */ +static int uaol_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) +{ + struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); + unsigned int budget = (uintptr_t)rule->private; + unsigned int bytes; + struct snd_interval t = { .integer = 1 }; + + bytes = uaol_min_sample_bytes(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + if (!bytes || !channels->min) + return 0; + + t.max = budget / (channels->min * bytes); + + return snd_interval_refine(hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE), &t); +} + +static int uaol_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) +{ + struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); + unsigned int budget = (uintptr_t)rule->private; + unsigned int bytes; + struct snd_interval t = { .integer = 1 }; + + bytes = uaol_min_sample_bytes(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + if (!bytes || !rate->min) + return 0; + + t.max = budget / (rate->min * bytes); + + return snd_interval_refine(hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS), &t); +} + +static int uaol_rule_format(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) +{ + struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); + struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); + unsigned int budget = (uintptr_t)rule->private; + snd_pcm_format_t format; + struct snd_mask m; + + if (!rate->min || !channels->min) + return 0; + + snd_mask_none(&m); + pcm_for_each_format(format) { + unsigned int bytes = uaol_sample_bytes(format); + + if (bytes && (u64)channels->min * bytes * rate->min <= budget) + snd_mask_set_format(&m, format); + } + + return snd_mask_refine(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), &m); +} + +/* + * The share of the link FIFO a stream gets is fixed when the gateway is + * configured, and the rate, the channel count and the format together decide + * whether what the stream produces fits in it. Overrunning it is not reported + * anywhere, the audio simply comes out corrupted, so keep the combinations that + * do not fit off the table. + * + * This is a property of the link and not of the connected device, hence it is + * applied whether or not there is a device to narrow to. + */ +static void hda_uaol_set_fifo_constraint(struct snd_sof_dev *sdev, + struct snd_soc_dai *be_dai, + struct snd_pcm_substream *substream) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(be_dai, substream->stream); + struct snd_pcm_runtime *runtime = substream->runtime; + struct sof_ipc4_copier *ipc4_copier; + unsigned int budget; + int size; + + if (!w) + return; + + ipc4_copier = widget_to_copier(w); + + size = sof_ipc4_uaol_get_fifo_size(sdev, ipc4_copier->dai_index, substream->stream); + if (size <= 0) + return; + + /* + * The real service interval is only known once a device's altsetting is + * actually resolved, so this early negotiation-time rule can only offer a + * coarse pre-filter: assume the shortest possible interval (125 us), which + * keeps high-bandwidth devices from being needlessly capped here. + */ + budget = size / SOF_UAOL_FIFO_INTERVALS * (USEC_PER_SEC / 125); + + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, + uaol_rule_rate, (void *)(uintptr_t)budget, + SNDRV_PCM_HW_PARAM_CHANNELS, SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, + uaol_rule_channels, (void *)(uintptr_t)budget, + SNDRV_PCM_HW_PARAM_RATE, SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, + uaol_rule_format, (void *)(uintptr_t)budget, + SNDRV_PCM_HW_PARAM_RATE, SNDRV_PCM_HW_PARAM_CHANNELS, -1); +} + +/* + * The device is resolved on every call rather than once at open, so that a + * device connected or removed, or the route kcontrol changing which device + * is selected, while a frontend sits open is picked up on the next + * negotiation. + */ +static int uaol_rule_device(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule, int var) +{ + struct hda_uaol_stream *stream = rule->private; + struct sof_intel_hda_dev *hdev = stream->hdev; + struct usb_device *udev; + int pcm_index; + + udev = hda_uaol_selected(hdev, stream->direction, stream->route, &pcm_index); + if (!udev) + return 0; + + return usb_offload_refine_hw_params(hdev->uaol, udev, stream->direction, + pcm_index, var, params); +} + +static int uaol_rule_device_rate(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + return uaol_rule_device(params, rule, SNDRV_PCM_HW_PARAM_RATE); +} + +static int uaol_rule_device_channels(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + return uaol_rule_device(params, rule, SNDRV_PCM_HW_PARAM_CHANNELS); +} + +static int uaol_rule_device_format(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + return uaol_rule_device(params, rule, SNDRV_PCM_HW_PARAM_FORMAT); +} + +/* + * A frontend sharing the backend with one which has it open, the deep buffer, + * can only run what the first one opened the backend with: the two share the + * endpoint and the mixer. Clone those parameters as the only ones allowed, so + * userspace negotiates against the running stream and not against what the + * topology offers. + * + * What the first frontend opened the backend with is only known once its + * hw_params ran, hence rules and not plain constraints. + */ +static int uaol_rule_shared_rate(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct hda_uaol_stream *stream = rule->private; + struct snd_interval t = { .integer = 1 }; + + /* pairs with the release which publishes the first frontend's parameters */ + if (!smp_load_acquire(&stream->params_valid)) + return 0; + + t.min = stream->rate; + t.max = stream->rate; + + return snd_interval_refine(hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE), &t); +} + +static int uaol_rule_shared_channels(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct hda_uaol_stream *stream = rule->private; + struct snd_interval t = { .integer = 1 }; + + /* pairs with the release which publishes the first frontend's parameters */ + if (!smp_load_acquire(&stream->params_valid)) + return 0; + + t.min = stream->channels; + t.max = stream->channels; + + return snd_interval_refine(hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS), &t); +} + +static int uaol_rule_shared_format(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct hda_uaol_stream *stream = rule->private; + struct snd_mask m; + + /* pairs with the release which publishes the first frontend's parameters */ + if (!smp_load_acquire(&stream->params_valid)) + return 0; + + snd_mask_none(&m); + snd_mask_set_format(&m, stream->format); + + return snd_mask_refine(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), &m); +} + +/* + * A UAOL frontend advertises what the topology configured the link for, which + * has nothing to do with the device that happens to be connected. Narrow it to + * what that device can be run at, so that userspace negotiates against the + * device instead of finding out at hw_params time that its choice is + * impossible. + * + * Rules and not plain constraints, because what one parameter can be depends on + * the other two: a device can well have a multichannel stream at one rate and a + * stereo one at another, and the ranges alone would offer the combination of the + * two that it does not have. Each rule reports what the parameters still allow, + * so the refining settles on a combination a single alternate setting can serve, + * which is what the acquire in uaol_dai_start_stream() goes looking for. The + * rules are given the backend they run for, which lives as long as the card. + */ +static void hda_uaol_set_constraint(struct snd_sof_dev *sdev, + struct snd_soc_dai *be_dai, + struct snd_pcm_substream *substream) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct snd_pcm_runtime *runtime = substream->runtime; + struct hda_uaol_stream *ctx; + + hda_uaol_set_fifo_constraint(sdev, be_dai, substream); + + if (!hdev->uaol) + return; + + ctx = hda_uaol_stream_get(hdev, be_dai); + if (!ctx) + return; + + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, + uaol_rule_device_rate, ctx, + SNDRV_PCM_HW_PARAM_RATE, SNDRV_PCM_HW_PARAM_CHANNELS, + SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, + uaol_rule_device_channels, ctx, + SNDRV_PCM_HW_PARAM_CHANNELS, SNDRV_PCM_HW_PARAM_RATE, + SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, + uaol_rule_device_format, ctx, + SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_RATE, + SNDRV_PCM_HW_PARAM_CHANNELS, -1); + + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, + uaol_rule_shared_rate, ctx, SNDRV_PCM_HW_PARAM_RATE, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, + uaol_rule_shared_channels, ctx, SNDRV_PCM_HW_PARAM_CHANNELS, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, + uaol_rule_shared_format, ctx, SNDRV_PCM_HW_PARAM_FORMAT, -1); +} + +static u32 uaol_ep_info(const struct usb_offload_stream_hw *hw, + const struct usb_offload_ep_info *ep) +{ + return FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_DIRECTION, + !!(ep->ep_addr & USB_DIR_IN)) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_NUMBER, + ep->ep_addr & USB_ENDPOINT_NUMBER_MASK) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_SLOT, hw->slot_id) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_SPLIT, hw->split) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_HIGH_SPEED, hw->high_speed) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_MAX_PACKET, ep->maxpacket); +} + +/* + * The gateway only knows which USB endpoint to drive once the device is + * acquired, so complete the configuration the topology set up. + */ +static int uaol_dai_set_endpoint(struct snd_soc_dai *cpu_dai, int direction, + const struct usb_offload_stream_hw *hw) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, direction); + struct sof_ipc4_uaol_configuration_blob *blob; + struct sof_ipc4_copier *ipc4_copier; + u32 stream = hw->data.resource - 1; + int link_stream; + + link_stream = sof_ipc4_uaol_link_stream(stream); + if (link_stream < 0) { + dev_err(cpu_dai->dev, "invalid UAOL stream %u\n", stream); + return link_stream; + } + + ipc4_copier = widget_to_copier(w); + blob = (struct sof_ipc4_uaol_configuration_blob *)ipc4_copier->copier_config; + + blob->config &= ~SOF_IPC4_UAOL_CONFIG_STREAM_IDX; + blob->config |= FIELD_PREP(SOF_IPC4_UAOL_CONFIG_STREAM_IDX, stream); + + ipc4_copier->data.gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; + ipc4_copier->data.gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX(link_stream); + + blob->xhci_bdf = FIELD_PREP(SOF_IPC4_UAOL_XHCI_BDF_BUS, + PCI_BUS_NUM(hw->bus_devid)) | + FIELD_PREP(SOF_IPC4_UAOL_XHCI_BDF_DEVICE, + PCI_SLOT(hw->bus_devid)) | + FIELD_PREP(SOF_IPC4_UAOL_XHCI_BDF_FUNCTION, + PCI_FUNC(hw->bus_devid)); + + blob->ep_info = uaol_ep_info(hw, &hw->data); + + /* Service interval from the USB endpoint descriptor */ + blob->service_interval_us = hw->service_interval_us; + + /* the feedback fields are left over from the previously offloaded device */ + if (hw->has_feedback) { + blob->feedback_ep_info = uaol_ep_info(hw, &hw->feedback); + blob->config &= ~(SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX | + SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD); + blob->config |= FIELD_PREP(SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX, + hw->feedback.resource - 1) | + /* bInterval form, the firmware polls every 2^(n-1) (micro)frames */ + FIELD_PREP(SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD, + hw->feedback.interval); + } else { + blob->feedback_ep_info = 0; + blob->config &= ~(SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX | + SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD); + } + + return 0; +} + +/* + * The negotiation-time rule assumes the shortest service interval. Once the + * device's endpoint is known, check the payload of SOF_UAOL_FIFO_INTERVALS + * actual service intervals, in whole sample frames, against the FIFO share. + * A stream taking exactly the whole share is valid, e.g. 384 kHz stereo on a + * 500 us endpoint, only a payload that is larger than the share overruns it. + */ +static int uaol_check_fifo_fit(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu_dai, + struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + const struct usb_offload_stream_hw *hw) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); + struct sof_ipc4_copier *ipc4_copier; + u64 frames, bytes; + int size; + + if (!w || !hw->service_interval_us) + return 0; + + ipc4_copier = widget_to_copier(w); + + size = sof_ipc4_uaol_get_fifo_size(sdev, ipc4_copier->dai_index, substream->stream); + if (size <= 0) + return 0; + + frames = DIV_ROUND_UP_ULL((u64)params_rate(params) * hw->service_interval_us, + USEC_PER_SEC); + bytes = frames * params_channels(params) * + uaol_sample_bytes(params_format(params)) * SOF_UAOL_FIFO_INTERVALS; + + if (bytes > size) { + dev_err(cpu_dai->dev, + "stream needs %llu bytes of link FIFO, only %d available\n", + bytes, size); + return -EINVAL; + } + + return 0; +} + +/* + * Index of the DMA config TLV describing the feedback link DMA. A UAOL + * backend has a single CPU DAI, so the audio link DMA is always TLV 0 and + * the firmware takes the second one as the feedback (host_dma_config[1]). + */ +#define SOF_UAOL_FB_DMA_TLV_IDX 1 + +static void uaol_dai_release_feedback(struct snd_sof_dev *sdev, + struct hda_uaol_stream *stream) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(stream->dai, stream->direction); + struct sof_ipc4_copier *ipc4_copier = widget_to_copier(w); + + /* a stale TLV would be sent along with the next, feedback-less, device */ + memset(&ipc4_copier->dma_config_tlv[SOF_UAOL_FB_DMA_TLV_IDX], 0, + sizeof(ipc4_copier->dma_config_tlv[SOF_UAOL_FB_DMA_TLV_IDX])); + + if (!stream->fb_hext_stream) + return; + + hda_uaol_release_fb_link_stream(sdev, stream->fb_hext_stream); + stream->fb_hext_stream = NULL; +} + +/* + * The feedback endpoint is read by the DSP through a link DMA of its own, in + * the capture direction. Its stream tag is what the firmware needs to bind + * the UAOL feedback stream to, handed over as a second DMA config TLV after + * the one of the audio link DMA. + */ +static int uaol_dai_setup_feedback(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct hda_uaol_stream *stream) +{ + struct snd_soc_dai *cpu_dai = stream->dai; + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_ipc4_copier *ipc4_copier = widget_to_copier(w); + struct sof_ipc4_dma_config_tlv *dma_config_tlv; + struct sof_ipc4_dma_config *dma_config; + struct hdac_ext_stream *hext_stream; + unsigned int format_val; + unsigned int format_bits; + int stream_tag; + + if (sdev->dspless_mode_selected) + return 0; + + /* one 32-bit feedback value per service interval */ + format_bits = snd_hdac_stream_format_bits(SNDRV_PCM_FORMAT_S32_LE, + SNDRV_PCM_SUBFORMAT_STD, 32); + format_val = snd_hdac_stream_format(1, format_bits, params_rate(params)); + + hext_stream = hda_uaol_assign_fb_link_stream(sdev, substream, format_val); + if (!hext_stream) { + dev_err(cpu_dai->dev, "no link DMA available for the USB feedback\n"); + return -EBUSY; + } + + stream->fb_hext_stream = hext_stream; + stream_tag = hdac_stream(hext_stream)->stream_tag; + + dma_config_tlv = &ipc4_copier->dma_config_tlv[SOF_UAOL_FB_DMA_TLV_IDX]; + dma_config_tlv->type = SOF_IPC4_GTW_DMA_CONFIG_ID; + dma_config_tlv->length = sizeof(dma_config_tlv->dma_config); + + dma_config = &dma_config_tlv->dma_config; + dma_config->dma_method = SOF_IPC4_DMA_METHOD_HDA; + dma_config->pre_allocated_by_host = 1; + dma_config->dma_channel_id = stream_tag - 1; + dma_config->stream_id = stream_tag; + dma_config->dma_stream_channel_map.device_count = 1; + dma_config->dma_priv_config_size = 0; + + dev_dbg(cpu_dai->dev, "USB feedback on link DMA stream %d\n", stream_tag); + + return 0; +} + +static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, + struct hda_uaol_stream *stream); + +/* + * Take the device away from the host and finish the gateway configuration + * with what the acquire reports. The two belong together: the sideband + * resource the endpoint lands on is what the stream index and the node + * index are derived from, and it need not come back the same. The stream + * is started from hw_params and, after a system suspend, from prepare, + * which is the only callback the resume path runs. + */ +static int uaol_dai_start_stream(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct hda_uaol_stream *stream) +{ + struct snd_soc_dai *cpu_dai = stream->dai; + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + int direction = substream->stream; + struct usb_offload_stream_hw hw; + struct usb_device *udev; + int pcm_index; + int ret; + + if (!hdev->uaol) + return -ENODEV; + + udev = hda_uaol_selected(hdev, direction, stream->route, &pcm_index); + if (!udev) { + dev_err(cpu_dai->dev, "no USB device selected for offload\n"); + return -ENODEV; + } + + /* + * SOF only ever hands this S24_LE; settle on the container the + * device actually has before asking for the stream, since a device + * packing 24-bit samples into 3 bytes will not recognise S24_LE. + * The rewrite lands in the params the link DMA is then set up from, + * so it runs the same 24-bit container - the DSP packs the samples on + * their way to the link. On hw_params those are a copy DPCM made for + * this call, which prepare resolves again for itself. + */ + ret = usb_offload_resolve_format(hdev->uaol, udev, direction, pcm_index, params); + if (ret < 0) + return ret; + + ret = usb_offload_acquire_stream(hdev->uaol, udev, direction, pcm_index, + params, &hw); + if (ret) { + dev_err(cpu_dai->dev, "failed to acquire the USB stream: %d\n", ret); + return ret; + } + + ret = uaol_check_fifo_fit(sdev, cpu_dai, substream, params, &hw); + if (ret) + goto err_release; + + ret = uaol_dai_set_endpoint(cpu_dai, direction, &hw); + if (ret) + goto err_release; + + if (hw.has_feedback) { + ret = uaol_dai_setup_feedback(substream, params, stream); + if (ret) + goto err_release; + } + + stream->udev = udev; + stream->pcm_index = hw.pcm_index; + stream->be = substream; + + /* + * The route control refuses a change once the stream is set here, and + * it decides that under the lock the selection is read back with: a + * change which went through while the device was being acquired is + * seen now, a later one finds the route busy. Back out of the former, + * the stream would play to a device the route no longer selects. + */ + if (hda_uaol_selected(hdev, direction, stream->route, &pcm_index) != udev || + pcm_index != hw.pcm_index) { + uaol_dai_stop_stream(substream, stream); + return -EBUSY; + } + + return 0; + +err_release: + usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); + return ret; +} + +static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, + struct hda_uaol_stream *stream) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, stream->dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct usb_device *udev = stream->udev; + + /* a stream given up after a stop has nothing to restart */ + stream->quiesced = false; + + /* also after an unplug, which takes only the USB side away */ + if (stream->fb_hext_stream) + uaol_dai_release_feedback(sdev, stream); + + if (!udev) + return; + + stream->udev = NULL; + stream->be = NULL; + usb_offload_release_stream(hdev->uaol, udev, stream->direction, stream->pcm_index); +} + +static int uaol_dai_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *cpu_dai) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + unsigned int rate, channels; + snd_pcm_format_t format; + int ret; + + if (!stream) + return -ENODEV; + + /* + * A second frontend joining the stream's mixer, the deep buffer, runs + * the backend's hw_params again while the first one still has it. The + * USB stream is acquired once, by whichever frontend came first, and + * stays until the last one is gone. + * + * The two share the one endpoint and the one mixer, so the second one + * can only run what the first one opened the backend with. Refuse it + * here, before the core prepares anything for it: a frontend failing + * further down would take what the first one prepared with it. + */ + if (stream->udev) { + /* pairs with the release once the first frontend's parameters are stored */ + if (!smp_load_acquire(&stream->params_valid) || + params_rate(params) != stream->rate || + params_channels(params) != stream->channels || + params_format(params) != stream->format) { + dev_err(cpu_dai->dev, + "stream is open at %u Hz, %u ch, format %d, not %u Hz, %u ch, format %d\n", + stream->rate, stream->channels, stream->format, + params_rate(params), params_channels(params), + params_format(params)); + return -EBUSY; + } + + return non_hda_dai_hw_params(substream, params, cpu_dai); + } + + /* the acquire resolves the format in place, keep what the frontend asked for */ + rate = params_rate(params); + channels = params_channels(params); + format = params_format(params); + + ret = uaol_dai_start_stream(substream, params, stream); + if (ret) + return ret; + + ret = non_hda_dai_hw_params(substream, params, cpu_dai); + if (ret) { + uaol_dai_stop_stream(substream, stream); + return ret; + } + + stream->rate = rate; + stream->channels = channels; + stream->format = format; + /* publish the parameters to the frontend rules and to a joining frontend */ + smp_store_release(&stream->params_valid, true); + + return 0; +} + +static int uaol_dai_hw_free(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + int ret = hda_dai_hw_free(substream, cpu_dai); + + if (stream) { + /* the next frontend to open the backend decides the parameters */ + smp_store_release(&stream->params_valid, false); + uaol_dai_stop_stream(substream, stream); + /* a stream closed while suspended has nothing left to reacquire */ + stream->suspended = false; + } + + return ret; +} + +/* + * A system suspend resets the pipeline and frees the DAI copier, and takes + * the device's streaming altsetting with it, while the stream itself stays + * open. The resume path only runs prepare on the backend, so nothing + * reestablishes the USB side on its own and the stream is acquired again + * from here, before the pipeline is rebuilt. + */ +static int uaol_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream = hda_uaol_stream_get(hdev, cpu_dai); + struct snd_pcm_hw_params *params; + int direction = substream->stream; + int ret; + + if (!stream) + return non_hda_dai_prepare(substream, cpu_dai); + + params = &rtd->dpcm[direction].hw_params; + + /* left set until the stream is back, for a failed prepare to retry */ + if (stream->suspended) { + uaol_dai_stop_stream(substream, stream); + + ret = uaol_dai_start_stream(substream, params, stream); + if (ret) + return ret; + + stream->suspended = false; + } + + /* the device is gone, the gateway would run on a released resource */ + if (!stream->udev) + return -ENODEV; + + /* + * The common prepare programs the link DMA again after a stop, from + * the backend's own copy of the params, which DPCM keeps at what the + * frontend negotiated. Resolve it to the container the stream was + * acquired with, as it was for hw_params. + */ + ret = usb_offload_resolve_format(hdev->uaol, stream->udev, direction, + stream->pcm_index, params); + if (ret < 0) + return ret; + + return non_hda_dai_prepare(substream, cpu_dai); +} + +/* + * The backend is triggered on the way into a system suspend, which is the + * only notice this driver gets that the stream will have to be reacquired. + * The feedback link DMA is not the DAI's DMA data, so the common trigger + * does not know about it and it is run from here, alongside the audio one. + * + * On the way down the xHCI endpoints are stopped right after the DSP has + * stopped the UAOL streams, not left to hw_free, which runs only after the + * DAI copier has been freed and the UAOL link powered off. An endpoint the + * controller keeps servicing past that point, the feedback IN endpoint in + * particular, has nowhere to deliver to and can lock up the controller. + * They are not stopped any earlier: a UAOL stream cannot go idle once the + * xHCI no longer services its endpoint, and the DSP times out stopping it. + * + * A stopped endpoint stays stopped, so a stream started again without a + * hw_free in between, as on the recovery from an xrun, restarts them before + * the DSP starts its side of the link, the way they run on the first start. + */ +static int uaol_dai_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *cpu_dai) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream = hda_uaol_stream_get(hdev, cpu_dai); + struct hdac_ext_stream *fb_hext_stream = NULL; + int direction = substream->stream; + int ret; + + if (!stream) + return hda_dai_trigger(substream, cmd, cpu_dai); + + if (direction == SNDRV_PCM_STREAM_PLAYBACK) + fb_hext_stream = stream->fb_hext_stream; + + if (cmd == SNDRV_PCM_TRIGGER_SUSPEND) + stream->suspended = true; + + if (cmd == SNDRV_PCM_TRIGGER_START && stream->quiesced && stream->udev) { + ret = usb_offload_restart_stream(hdev->uaol, stream->udev, direction, + stream->pcm_index); + if (ret) + return ret; + + stream->quiesced = false; + } + + if (fb_hext_stream && + (cmd == SNDRV_PCM_TRIGGER_START || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)) + snd_hdac_ext_stream_start(fb_hext_stream); + + ret = hda_dai_trigger(substream, cmd, cpu_dai); + + if ((cmd == SNDRV_PCM_TRIGGER_STOP || cmd == SNDRV_PCM_TRIGGER_SUSPEND) && + stream->udev) { + usb_offload_quiesce_stream(hdev->uaol, stream->udev, direction, + stream->pcm_index); + stream->quiesced = true; + } + + if (fb_hext_stream && + (cmd == SNDRV_PCM_TRIGGER_STOP || cmd == SNDRV_PCM_TRIGGER_SUSPEND || + cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)) + snd_hdac_ext_stream_clear(fb_hext_stream); + + return ret; +} + +static const struct snd_soc_dai_ops uaol_dai_ops = { + .probe = uaol_dai_probe, + .remove = uaol_dai_remove, + .hw_params = uaol_dai_hw_params, + .hw_free = uaol_dai_hw_free, + .trigger = uaol_dai_trigger, + .prepare = uaol_dai_prepare, +}; + +/* + * Called for every frontend the platform component opens, so the backends it + * routes to are what says whether there is anything to do here. + */ +void hda_apply_constraint_from_be(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream) +{ + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); + struct snd_soc_dpcm *dpcm; + + for_each_dpcm_be(rtd, substream->stream, dpcm) { + struct snd_soc_dai *be_dai = snd_soc_rtd_to_cpu(dpcm->be, 0); + + if (be_dai->driver->ops == &uaol_dai_ops) { + hda_uaol_set_constraint(sdev, be_dai, substream); + return; + } + } +} + +void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) +{ + const struct sof_intel_dsp_desc *chip; + int i; + + chip = get_chip_info(sdev->pdata); + + if (chip->hw_ip_version >= SOF_INTEL_ACE_2_0) { + for (i = 0; i < ops->num_drv; i++) { + if (strstr(ops->drv[i].name, "UAOL")) + ops->drv[i].ops = &uaol_dai_ops; + } + } +} + +MODULE_IMPORT_NS("USB_AUDIO_OFFLOAD"); diff --git a/sound/soc/sof/intel/hda.c b/sound/soc/sof/intel/hda.c index 45e968fb19104b..1a14e535eb19bd 100644 --- a/sound/soc/sof/intel/hda.c +++ b/sound/soc/sof/intel/hda.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -30,9 +31,11 @@ #include #include #include +#include #include "../sof-audio.h" #include "../sof-pci-dev.h" #include "../ops.h" +#include "../ipc4-priv.h" #include "../ipc4-topology.h" #include "../../intel/common/sof-function-topology-lib.h" #include "hda.h" @@ -418,6 +421,178 @@ static inline void hda_dsp_sdw_process_mic_privacy(struct snd_sof_dev *sdev) { } #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */ +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) + +/* Whether the UAOL links of the controller can be put to use at all */ +static bool hda_uaol_available(struct snd_sof_dev *sdev) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + + if (sdev->pdata->ipc_type != SOF_IPC_TYPE_4) + return false; + + if (!ipc4_data->uaol_supported || !ipc4_data->uaol_caps) { + dev_dbg(sdev->dev, "UAOL: no firmware support\n"); + return false; + } + + return true; +} + +/* The stream capabilities the firmware reported for UAOL link @link */ +static int hda_uaol_link_caps(struct snd_sof_dev *sdev, unsigned int link, u32 *caps) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_caps *uaol_caps; + + if (!hda_uaol_available(sdev)) + return -ENODEV; + + uaol_caps = ipc4_data->uaol_caps; + if (link >= uaol_caps->link_count) + return -ENODEV; + + *caps = uaol_caps->link_caps[link].stream_caps; + + return 0; +} + +/* + * The link's streams are tied to functions by the firmware's FIFO layout, and + * the xHCI resource an endpoint gets decides the stream it runs on (stream = + * resource - 1): the output streams come first, then the input streams which + * carry audio, and the last ones, one per output stream, carry the clock + * feedback. An endpoint has to be placed within the streams of its function. + */ +static int uaol_get_resource_range(struct device *dev, int direction, bool feedback, + unsigned int *first, unsigned int *last) +{ + unsigned int tx_streams, rx_streams, audio_rx_streams, start, count; + struct snd_sof_dev *sdev = dev_get_drvdata(dev); + u32 caps; + int ret; + + ret = hda_uaol_link_caps(sdev, 0, &caps); + if (ret) + return ret; + + tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps); + rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps); + audio_rx_streams = rx_streams > tx_streams ? rx_streams - tx_streams : 0; + + if (feedback) { + start = tx_streams + audio_rx_streams; + count = tx_streams; + } else if (direction == SNDRV_PCM_STREAM_PLAYBACK) { + start = 0; + count = tx_streams; + } else { + start = tx_streams; + count = audio_rx_streams; + } + + if (!count) + return -ENODEV; + + *first = start + 1; + *last = start + count; + + return 0; +} + +static const struct usb_offload_ops uaol_callback = { + .notify_connect = hda_uaol_notify_connect, + .notify_disconnect = hda_uaol_notify_disconnect, + .get_resource_range = uaol_get_resource_range, +}; + +/* One offload context in the library is one UAOL link, over which the DSP reaches an xHCI */ +static int hda_uaol_register(struct snd_sof_dev *sdev) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct usb_offload_res res = { + .dev = sdev->dev, + .ops = &uaol_callback, + /* + * SOF's pipeline only has a 24-bit representation as S24_LE + * (24 valid bits in a 4-byte container); a device that only + * packs its 24-bit audio into 3 bytes (S24_3LE) still needs to + * be usable. + */ + .s24_3le_as_s24_le = true, + /* + * The firmware applies the clock feedback of an asynchronous + * playback stream by spreading the samples linearly over the + * service interval, which is audible on the gear that uses + * asynchronous playback in the first place. + */ + .async_playback_on_host = true, + }; + struct usb_offload_ctx *uaol; + + INIT_LIST_HEAD(&hdev->uaol_devices); + mutex_init(&hdev->uaol_mutex); + + if (!hdev->uaol_link_count || !hda_uaol_available(sdev)) + return 0; + + uaol = usb_offload_register(&res); + if (IS_ERR(uaol)) { + dev_err(sdev->dev, "USB audio offload registration failed: %ld\n", + PTR_ERR(uaol)); + return PTR_ERR(uaol); + } + + hdev->uaol = uaol; + + return 0; +} + +static void hda_uaol_unregister(struct snd_sof_dev *sdev) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + + usb_offload_unregister(hdev->uaol); + hdev->uaol = NULL; +} + +#else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) */ + +static inline bool hda_uaol_available(struct snd_sof_dev *sdev) +{ + return false; +} + +static inline int hda_uaol_link_caps(struct snd_sof_dev *sdev, unsigned int link, u32 *caps) +{ + return -ENODEV; +} + +static inline int hda_uaol_register(struct snd_sof_dev *sdev) +{ + return 0; +} + +static inline void hda_uaol_unregister(struct snd_sof_dev *sdev) { } + +#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) */ + +/* + * The firmware configuration, and with it the offload capabilities, is only + * parsed once the DSP boot has completed, which is after the post_fw_run + * stage. This is the first hook that runs late enough to query it. + */ +int hda_machine_register(struct snd_sof_dev *sdev, void *pdata) +{ + int ret; + + ret = hda_uaol_register(sdev); + if (ret < 0) + dev_dbg(sdev->dev, "USB audio offload is not available: %d\n", ret); + + return sof_machine_register(sdev, pdata); +} + /* pre fw run operations */ int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev) { @@ -500,6 +675,10 @@ static int bt_link_mask_override = -1; module_param_named(bt_link_mask, bt_link_mask_override, int, 0444); MODULE_PARM_DESC(bt_link_mask, "SOF BT offload link mask"); +static int uaol_link_mask_override = -1; +module_param_named(uaol_link_mask, uaol_link_mask_override, int, 0444); +MODULE_PARM_DESC(uaol_link_mask, "SOF USB audio offload link mask"); + static int hda_init(struct snd_sof_dev *sdev) { struct hda_bus *hbus; @@ -639,6 +818,16 @@ static int hda_init_caps(struct snd_sof_dev *sdev) return ret; } + /* the UAOL links have been enumerated by hda_bus_ml_init() */ + hdev->uaol_link_count = hdac_bus_eml_get_count(bus, true, + AZX_REG_ML_LEPTR_ID_INTEL_UAOL); + if (hdev->uaol_link_count) { + /* hda-mlink has no UAOL instance offset, the sublinks all alias to sublink 0 */ + dev_dbg(sdev->dev, "UAOL links on the controller: %d (usable: 1)\n", + hdev->uaol_link_count); + hdev->uaol_link_count = 1; + } + /* Skip SoundWire if it is not supported */ if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) goto skip_soundwire; @@ -972,6 +1161,8 @@ void hda_dsp_remove(struct snd_sof_dev *sdev) hda_codec_device_remove(sdev); + hda_uaol_unregister(sdev); + hda_sdw_exit(sdev); if (!IS_ERR_OR_NULL(hda->dmic_dev)) @@ -1498,6 +1689,59 @@ void hda_set_mach_params(struct snd_soc_acpi_mach *mach, mach_params->dai_drivers = desc->ops->drv; } +/* + * The link runs an audio stream per output stream and per input stream not + * taken by the clock feedback of an output stream; the DAIs put a ceiling on + * how many of them are used. 0 when the link cannot be used. + */ +static unsigned int hda_uaol_stream_num(struct snd_sof_dev *sdev, int direction) +{ + unsigned int tx_streams, rx_streams; + u32 caps; + + if (hda_uaol_link_caps(sdev, 0, &caps)) + return 0; + + tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps); + rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps); + + if (direction == SNDRV_PCM_STREAM_PLAYBACK) + return min(tx_streams, SOF_UAOL_MAX_PLAYBACK_STREAMS); + + return min(rx_streams > tx_streams ? rx_streams - tx_streams : 0, + SOF_UAOL_MAX_CAPTURE_STREAMS); +} + +void hda_finalize_mach_params(struct snd_sof_dev *sdev, + struct snd_soc_acpi_mach_params *params) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + int i; + + if (!params->uaol_link_mask) + return; + + /* the UAOL DAIs size their offload routes by the same counts */ + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) + hdev->uaol_stream_num[i] = hda_uaol_stream_num(sdev, i); + params->uaol_playback_num = hdev->uaol_stream_num[SNDRV_PCM_STREAM_PLAYBACK]; + params->uaol_capture_num = hdev->uaol_stream_num[SNDRV_PCM_STREAM_CAPTURE]; + + /* + * The controller's UAOL links are only usable if the booted firmware + * supports them and the DAIs are built. + */ + if (!params->uaol_playback_num && !params->uaol_capture_num) { + dev_dbg(sdev->dev, "UAOL: withdrawing link mask %#x\n", + params->uaol_link_mask); + params->uaol_link_mask = 0; + return; + } + + dev_dbg(sdev->dev, "UAOL: link mask %#x, %u playback and %u capture streams per link\n", + params->uaol_link_mask, params->uaol_playback_num, params->uaol_capture_num); +} + static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach) { u32 dmic_ssp_quirk; @@ -1537,6 +1781,7 @@ struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev) u32 interface_mask = hda_get_interface_mask(sdev); struct snd_sof_pdata *sof_pdata = sdev->pdata; const struct sof_dev_desc *desc = sof_pdata->desc; + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; struct hdac_bus *bus = sof_to_bus(sdev); struct snd_soc_acpi_mach *mach = NULL; enum snd_soc_acpi_intel_codec codec_type, amp_type; @@ -1593,6 +1838,20 @@ struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev) mach->mach_params.bt_link_mask = 0; } + /* report the USB audio offload links enumerated on the controller */ + if (hdev->uaol_link_count) + mach->mach_params.uaol_link_mask = GENMASK(hdev->uaol_link_count - 1, 0); + + /* allow for module parameter override */ + if (uaol_link_mask_override != -1) { + dev_dbg(sdev->dev, "overriding UAOL link mask %#x by kernel param %#x\n", + mach->mach_params.uaol_link_mask, uaol_link_mask_override); + if (uaol_link_mask_override & ~mach->mach_params.uaol_link_mask) + dev_warn(sdev->dev, "UAOL link mask %#x exceeds the links on the controller %#x\n", + uaol_link_mask_override, mach->mach_params.uaol_link_mask); + mach->mach_params.uaol_link_mask = uaol_link_mask_override; + } + /* * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier * name string if quirk flag is set. @@ -1824,3 +2083,4 @@ MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK"); MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON"); MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH"); +MODULE_IMPORT_NS("USB_AUDIO_OFFLOAD"); diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index bc11519427649f..8c572ac2e26326 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -21,6 +21,11 @@ #include "../sof-audio.h" #include "shim.h" +struct usb_offload_ctx; +struct usb_device; +struct usb_offload_usb_dev_info; +struct snd_soc_usb; + /* PCI registers */ #define PCI_TCSEL 0x44 #define PCI_PGCTL PCI_TCSEL @@ -421,9 +426,24 @@ #define SOF_SKL_NUM_DAIS_NOCODEC 9 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) -#define SOF_SKL_NUM_DAIS 16 +#define SOF_SKL_NUM_DAIS_BASE 16 #else -#define SOF_SKL_NUM_DAIS SOF_SKL_NUM_DAIS_NOCODEC +#define SOF_SKL_NUM_DAIS_BASE SOF_SKL_NUM_DAIS_NOCODEC +#endif + +/* + * The UAOL DAIs are the last entries of skl_dai[]. A link has a stream per + * direction for each one it can run concurrently; how many of these the + * machine driver uses is reported in the mach_params. + */ +#define SOF_UAOL_MAX_PLAYBACK_STREAMS 2 +#define SOF_UAOL_MAX_CAPTURE_STREAMS 2 +#define SOF_UAOL_NUM_DAIS (SOF_UAOL_MAX_PLAYBACK_STREAMS + \ + SOF_UAOL_MAX_CAPTURE_STREAMS) +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) +#define SOF_SKL_NUM_DAIS (SOF_SKL_NUM_DAIS_BASE + SOF_UAOL_NUM_DAIS) +#else +#define SOF_SKL_NUM_DAIS SOF_SKL_NUM_DAIS_BASE #endif /* Intel HD Audio SRAM Window 0*/ @@ -492,6 +512,43 @@ struct sof_ace3_mic_privacy { struct work_struct work; }; +/* + * One UAOL backend: the DAI it is bound to and the USB stream it holds. This + * lives for the lifetime of the card and names the device only indirectly, by + * the route of its direction the backend plays to. + */ +struct hda_uaol_stream { + struct sof_intel_hda_dev *hdev; + /* the UAOL CPU DAI, NULL while no DAI is bound to this slot */ + struct snd_soc_dai *dai; + int direction; + /* the ASoC USB route of @direction this backend plays to */ + unsigned int route; + + /* device whose stream the DSP owns, NULL when idle */ + struct usb_device *udev; + /* USB PCM of @udev the stream was acquired on */ + int pcm_index; + /* backend substream the stream runs on, its frontends are stopped on unplug */ + struct snd_pcm_substream *be; + /* + * Parameters the first frontend opened the backend with, as it passed + * them in. A frontend joining it, the deep buffer, shares the backend + * and its mixer and has to use the same. + */ + unsigned int rate; + unsigned int channels; + snd_pcm_format_t format; + /* @rate, @channels and @format are valid, published after they are set */ + bool params_valid; + /* the stream was suspended and has to be reacquired on prepare */ + bool suspended; + /* the USB endpoints were stopped and have to be restarted on start */ + bool quiesced; + /* link DMA the DSP reads the playback feedback endpoint through, if any */ + struct hdac_ext_stream *fb_hext_stream; +}; + /* represents DSP HDA controller frontend - i.e. host facing control */ struct sof_intel_hda_dev { bool imrboot_supported; @@ -523,6 +580,11 @@ struct sof_intel_hda_dev { /* the maximum number of streams (playback + capture) supported */ u32 stream_max; + /* USB Audio Offload links on the controller, 0 if UAOL is not present */ + int uaol_link_count; + /* streams a link runs per direction, as reported to the machine driver */ + unsigned int uaol_stream_num[SNDRV_PCM_STREAM_LAST + 1]; + /* * ACE2+ link DMA stream allocation constraints (stream index = * stream_tag - 1, shared between input and output directions). All @@ -561,6 +623,23 @@ struct sof_intel_hda_dev { /* sdw context allocated by SoundWire driver */ struct sdw_intel_ctx *sdw; + /* USB audio offload context allocated by the offload library */ + struct usb_offload_ctx *uaol; + + /* ASoC USB port, non NULL while the UAOL DAI is bound into a card */ + struct snd_soc_usb *uaol_usb; + + /* offload capable USB audio devices */ + struct list_head uaol_devices; + /* protects uaol_devices */ + struct mutex uaol_mutex; + + /* reports whether any offload capable device is connected */ + struct snd_soc_jack uaol_jack; + + /* one per UAOL CPU DAI, claimed when the DAI probes */ + struct hda_uaol_stream uaol_streams[SOF_UAOL_NUM_DAIS]; + /* FW clock config, 0:HPRO, 1:LPRO */ bool clk_config_lpro; @@ -931,6 +1010,8 @@ static inline bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev) #endif +int hda_machine_register(struct snd_sof_dev *sdev, void *pdata); + int sdw_hda_dai_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *cpu_dai, @@ -1013,6 +1094,8 @@ void hda_unregister_clients(struct snd_sof_dev *sdev); struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev); void hda_set_mach_params(struct snd_soc_acpi_mach *mach, struct snd_sof_dev *sdev); +void hda_finalize_mach_params(struct snd_sof_dev *sdev, + struct snd_soc_acpi_mach_params *params); /* PCI driver selection and probe */ int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id); @@ -1098,6 +1181,12 @@ hda_select_dai_widget_ops(struct snd_sof_dev *sdev, struct snd_sof_widget *swidg int hda_dai_config(struct snd_soc_dapm_widget *w, unsigned int flags, struct snd_sof_dai_config_data *data); +struct hdac_ext_stream *hda_uaol_assign_fb_link_stream(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream, + unsigned int format_val); +void hda_uaol_release_fb_link_stream(struct snd_sof_dev *sdev, + struct hdac_ext_stream *hext_stream); + static inline struct snd_sof_dev *widget_to_sdev(struct snd_soc_dapm_widget *w) { struct snd_sof_widget *swidget = w->dobj.private; @@ -1106,4 +1195,47 @@ static inline struct snd_sof_dev *widget_to_sdev(struct snd_soc_dapm_widget *w) return snd_soc_component_get_drvdata(component); } +static inline struct snd_sof_dev *dai_to_sdev(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); + + return widget_to_sdev(w); +} + +static inline struct sof_ipc4_copier *widget_to_copier(struct snd_soc_dapm_widget *w) +{ + struct snd_sof_widget *swidget = w->dobj.private; + struct snd_sof_dai *sdai = swidget->private; + + return sdai->private; +} + +#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK) +/* the DAI callbacks the UAOL DAI builds on, shared with the SSP and DMIC DAIs */ +int non_hda_dai_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *cpu_dai); +int non_hda_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai); +int hda_dai_hw_free(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai); +int hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *dai); +#endif + +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) +void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops); +void hda_apply_constraint_from_be(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream); +int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id); +int hda_uaol_notify_disconnect(struct device *dev, struct usb_device *udev); +#else +static inline void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, + struct snd_sof_dsp_ops *ops) { } +static inline void hda_apply_constraint_from_be(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream) { } +#endif + #endif diff --git a/sound/soc/sof/ipc4-loader.c b/sound/soc/sof/ipc4-loader.c index 1da51f7490e579..fb35249044f78f 100644 --- a/sound/soc/sof/ipc4-loader.c +++ b/sound/soc/sof/ipc4-loader.c @@ -5,6 +5,7 @@ // // Copyright(c) 2022 Intel Corporation +#include #include #include #include @@ -449,6 +450,47 @@ static int sof_ipc4_query_sof_info(struct snd_sof_dev *sdev, return 0; } +static int sof_ipc4_query_uaol_caps(struct snd_sof_dev *sdev, void *caps_data, + size_t caps_size) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_caps *caps = caps_data; + size_t expected_size; + u32 i; + + if (caps_size < sizeof(*caps)) { + dev_err(sdev->dev, "Invalid UAOL capabilities size %zu\n", caps_size); + return -EINVAL; + } + + expected_size = struct_size(caps, link_caps, caps->link_count); + if (expected_size > caps_size) { + dev_err(sdev->dev, "Invalid UAOL link count %u for size %zu\n", + caps->link_count, caps_size); + return -EINVAL; + } + + ipc4_data->uaol_caps = devm_kmemdup(sdev->dev, caps, expected_size, GFP_KERNEL); + if (!ipc4_data->uaol_caps) + return -ENOMEM; + + dev_dbg(sdev->dev, "UAOL links reported by firmware: %u\n", caps->link_count); + + for (i = 0; i < caps->link_count; i++) { + struct sof_ipc4_intel_uaol_link_caps *link = &caps->link_caps[i]; + + dev_dbg(sdev->dev, + "UAOL link %u: streams in/out/bidir %u/%u/%u, FIFO tx/rx %u/%u\n", i, + (u32)FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, link->stream_caps), + (u32)FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, link->stream_caps), + (u32)FIELD_GET(SOF_IPC4_UAOL_BIDIRECTIONAL_STREAMS_MASK, + link->stream_caps), + link->max_tx_fifo_size, link->max_rx_fifo_size); + } + + return 0; +} + int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; @@ -524,6 +566,11 @@ int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) */ ipc4_data->libraries_restored = ipc4_data->fw_context_save; break; + case SOF_IPC4_FW_CFG_UAOL_SUPPORT: + ipc4_data->uaol_supported = *tuple->value; + trace_sof_ipc4_fw_config(sdev, "UAOL support", + *tuple->value); + break; case SOF_IPC4_FW_CFG_SOF_INFO: ret = sof_ipc4_query_sof_info(sdev, tuple->value, tuple->size); if (ret) @@ -554,6 +601,11 @@ int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset); switch (tuple->type) { + case SOF_IPC4_HW_CFG_INTEL_UAOL_CAPS: + /* a layout we cannot parse costs UAOL, not the DSP */ + if (sof_ipc4_query_uaol_caps(sdev, tuple->value, tuple->size)) + dev_warn(sdev->dev, "UAOL capabilities ignored\n"); + break; case SOF_IPC4_HW_CFG_INTEL_MIC_PRIVACY_CAPS: if (ipc4_data->intel_configure_mic_privacy) { struct sof_ipc4_intel_mic_privacy_cap *caps; diff --git a/sound/soc/sof/ipc4-priv.h b/sound/soc/sof/ipc4-priv.h index 31f7d68029ce71..175690f12c6062 100644 --- a/sound/soc/sof/ipc4-priv.h +++ b/sound/soc/sof/ipc4-priv.h @@ -80,6 +80,9 @@ struct snd_ipc4_nhlt { * @fw_context_save: Firmware supports full context save and restore * @libraries_restored: The libraries have been retained during firmware boot * @nhlt_list: The NHLT tables from the BIOS and the topology manifest + * @uaol_supported: The firmware has been built with UAOL support + * @uaol_caps: UAOL capabilities reported by the firmware, NULL if they were not + * reported * * @codec_info: Information about the available codecs in booted firmware. The * data is to be used by the code for compressed support. @@ -98,6 +101,8 @@ struct sof_ipc4_fw_data { bool fw_context_save; bool libraries_restored; struct list_head nhlt_list; + bool uaol_supported; + struct sof_ipc4_intel_uaol_caps *uaol_caps; void *codec_info; diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c index 4e72a1a93d34aa..f75ef2ce3ccfdd 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -805,6 +805,165 @@ static void sof_ipc4_widget_free_comp_pcm(struct snd_sof_widget *swidget) swidget->private = NULL; } +/** + * struct sof_ipc4_uaol_fifo_layout - division of a UAOL link FIFO + * @tx_size: space available to each transmit stream + * @rx_base: start offset of the first receive stream + * @rx_size: space available to each receive stream + */ +struct sof_ipc4_uaol_fifo_layout { + u32 tx_size; + u32 rx_base; + u32 rx_size; +}; + +/* + * Only the start offset of a stream is programmable, the space it can use + * reaches up to the start offset of the next one, so the FIFOs are divided + * evenly between the hardware's full stream count for the link - not how + * many DAIs SOF happens to configure. Which of the hardware's stream slots + * a given USB device's acquire lands on is decided by the xHCI sideband + * resource the endpoint gets, within the range of its function, which is + * independent of SOF's own widget count: even with only ever one stream + * actually running, that one stream can land on any slot of its function, + * so every slot needs a real, non-zero share regardless of how many are ever + * simultaneously active. Each transmit stream has a clock feedback channel + * taking one bus word at the beginning of the receive FIFO, so the receive + * streams carrying audio are the reported ones less the transmit stream + * count. + */ +static int sof_ipc4_uaol_get_fifo_layout(struct snd_sof_dev *sdev, int link_idx, + struct sof_ipc4_uaol_fifo_layout *layout) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_link_caps *caps; + u32 tx_streams, rx_streams; + + if (!ipc4_data->uaol_caps || link_idx >= ipc4_data->uaol_caps->link_count) { + dev_err(sdev->dev, "UAOL link %d is not supported by the firmware\n", + link_idx); + return -ENODEV; + } + + caps = &ipc4_data->uaol_caps->link_caps[link_idx]; + + tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps->stream_caps); + rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps->stream_caps); + + if (!tx_streams || rx_streams <= tx_streams) { + dev_err(sdev->dev, "UAOL link %d has no usable streams (caps %#x)\n", + link_idx, caps->stream_caps); + return -EINVAL; + } + + rx_streams -= tx_streams; + + /* + * The configuration has the start offsets of two streams per direction + * in fixed width fields, what the firmware reports has to fit them. + */ + if (tx_streams > SOF_IPC4_UAOL_FIFO_SAO_STREAMS || + rx_streams > SOF_IPC4_UAOL_FIFO_SAO_STREAMS) { + dev_err(sdev->dev, "UAOL link %d: streams (caps %#x) do not fit the gateway configuration\n", + link_idx, caps->stream_caps); + return -EINVAL; + } + + layout->tx_size = round_down(caps->max_tx_fifo_size / tx_streams, + SOF_IPC4_UAOL_FIFO_BUS_WIDTH); + layout->rx_base = tx_streams * SOF_IPC4_UAOL_FIFO_BUS_WIDTH; + if (caps->max_rx_fifo_size > layout->rx_base) + layout->rx_size = round_down((caps->max_rx_fifo_size - layout->rx_base) / + rx_streams, SOF_IPC4_UAOL_FIFO_BUS_WIDTH); + else + layout->rx_size = 0; + + if (!layout->tx_size || !layout->rx_size || + !FIELD_FIT(SOF_IPC4_UAOL_FIFO_SAO_TX1, layout->tx_size) || + !FIELD_FIT(SOF_IPC4_UAOL_FIFO_SAO_RX1, layout->rx_base + layout->rx_size)) { + dev_err(sdev->dev, "UAOL link %d: FIFO sizes tx %u rx %u do not fit the gateway configuration\n", + link_idx, caps->max_tx_fifo_size, caps->max_rx_fifo_size); + return -EINVAL; + } + + return 0; +} + +/** + * sof_ipc4_uaol_get_fifo_size - space in the link FIFO for one UAOL stream + * @sdev: SOF device + * @link_idx: index of the UAOL link + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * + * Return: the number of bytes a stream of @direction can use on the link, or a + * negative error code. + */ +int sof_ipc4_uaol_get_fifo_size(struct snd_sof_dev *sdev, int link_idx, int direction) +{ + struct sof_ipc4_uaol_fifo_layout layout; + int ret; + + ret = sof_ipc4_uaol_get_fifo_layout(sdev, link_idx, &layout); + if (ret) + return ret; + + return direction == SNDRV_PCM_STREAM_PLAYBACK ? layout.tx_size : layout.rx_size; +} +EXPORT_SYMBOL(sof_ipc4_uaol_get_fifo_size); + +static int sof_ipc4_widget_setup_uaol(struct snd_sof_dev *sdev, + struct snd_sof_widget *swidget, + struct sof_ipc4_copier *ipc4_copier) +{ + struct sof_ipc4_uaol_configuration_blob *blob; + struct sof_ipc4_uaol_fifo_layout layout; + int ret; + + ret = sof_ipc4_uaol_get_fifo_layout(sdev, ipc4_copier->dai_index, &layout); + if (ret) + return ret; + + blob = kzalloc_obj(*blob); + if (!blob) + return -ENOMEM; + + blob->xhci_bdf_tlv.type = SOF_IPC4_UAOL_XHCI_BDF_ID; + blob->xhci_bdf_tlv.length = sizeof(blob->xhci_bdf); + blob->config_tlv.type = SOF_IPC4_UAOL_CONFIG_ID; + blob->config_tlv.length = sizeof(blob->config); + blob->fifo_sao_tlv.type = SOF_IPC4_UAOL_FIFO_SAO_ID; + blob->fifo_sao_tlv.length = sizeof(blob->fifo_sao_tx) + sizeof(blob->fifo_sao_rx); + blob->ep_info_tlv.type = SOF_IPC4_UAOL_EP_INFO_ID; + blob->ep_info_tlv.length = sizeof(blob->ep_info); + blob->feedback_ep_info_tlv.type = SOF_IPC4_UAOL_FEEDBACK_EP_INFO_ID; + blob->feedback_ep_info_tlv.length = sizeof(blob->feedback_ep_info); + blob->art_divider_tlv.type = SOF_IPC4_UAOL_ART_DIVIDER_ID; + blob->art_divider_tlv.length = sizeof(blob->art_divider); + blob->service_interval_tlv.type = SOF_IPC4_UAOL_SERVICE_INTERVAL_ID; + blob->service_interval_tlv.length = sizeof(blob->service_interval_us); + + /* the stream is only known once the sideband resource is allocated */ + blob->config = FIELD_PREP(SOF_IPC4_UAOL_CONFIG_LINK_IDX, ipc4_copier->dai_index); + + /* the xHCI global time is already expressed in DSP timer ticks */ + blob->art_divider = FIELD_PREP(SOF_IPC4_UAOL_ART_MULTIPLIER, 1) | + FIELD_PREP(SOF_IPC4_UAOL_ART_DIVIDER, 1); + + /* + * The transmit stream 0 and the clock feedback channels both start at + * offset 0, every stream after them starts where its predecessor ends. + */ + blob->fifo_sao_tx = FIELD_PREP(SOF_IPC4_UAOL_FIFO_SAO_TX1, layout.tx_size); + blob->fifo_sao_rx = FIELD_PREP(SOF_IPC4_UAOL_FIFO_SAO_RX0, layout.rx_base) | + FIELD_PREP(SOF_IPC4_UAOL_FIFO_SAO_RX1, + layout.rx_base + layout.rx_size); + + ipc4_copier->copier_config = (uint32_t *)blob; + ipc4_copier->data.gtw_cfg.config_length = sizeof(*blob) >> 2; + + return 0; +} + static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget) { struct sof_ipc4_available_audio_format *available_fmt; @@ -925,6 +1084,11 @@ static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget) ipc4_copier->data.gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX_INTEL_DMIC(ipc4_copier->dai_index); break; + case SOF_DAI_INTEL_UAOL: + ret = sof_ipc4_widget_setup_uaol(sdev, swidget, ipc4_copier); + if (ret) + goto free_available_fmt; + break; default: ipc4_copier->gtw_attr = kzalloc_obj(*ipc4_copier->gtw_attr); if (!ipc4_copier->gtw_attr) { @@ -3568,9 +3732,11 @@ static int sof_ipc4_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget ipc_data = ipc4_copier->ipc_config_data; /* - * Refresh copier_data and dma_config_tlv in ipc_config_data. - * These may have been updated after ipc_prepare, e.g. when - * link DMA stream tags change after a suspend/resume cycle. + * Refresh copier_data, the gateway configuration and + * dma_config_tlv in ipc_config_data. These may have been + * updated after ipc_prepare, e.g. when link DMA stream tags + * change after a suspend/resume cycle, or when the DAI + * reconfigures its gateway for the resources it reacquired. * * copier_data->gtw_cfg.config_length does not include the * TLV size (it was restored after sof_ipc4_prepare_copier_module), @@ -3584,6 +3750,10 @@ static int sof_ipc4_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget memcpy(ipc_data, copier_data, sizeof(*copier_data)); copier_data->gtw_cfg.config_length = gtw_cfg_config_length / 4; + if (gtw_cfg_config_length && ipc4_copier->copier_config) + memcpy(ipc_data + sizeof(*copier_data), + ipc4_copier->copier_config, gtw_cfg_config_length); + if (tlv_size) memcpy(ipc_data + sizeof(*copier_data) + gtw_cfg_config_length, &ipc4_copier->dma_config_tlv, tlv_size); @@ -4134,6 +4304,9 @@ static int sof_ipc4_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget * case SOF_DAI_INTEL_SSP: /* nothing to do for SSP/DMIC */ break; + case SOF_DAI_INTEL_UAOL: + /* the node index is the link stream, set when the endpoint is known */ + break; default: dev_err(sdev->dev, "%s: unsupported dai type %d\n", __func__, ipc4_copier->dai_type); diff --git a/sound/soc/sof/ipc4-topology.h b/sound/soc/sof/ipc4-topology.h index b538b4183897a9..660e41f01e007d 100644 --- a/sound/soc/sof/ipc4-topology.h +++ b/sound/soc/sof/ipc4-topology.h @@ -326,6 +326,119 @@ struct sof_ipc4_alh_configuration_blob { struct sof_ipc4_dma_stream_ch_map alh_cfg; }; +/* + * The UAOL gateway configuration is a plain list of TLVs, which the firmware + * hands over to the UAOL hardware driver as it is. Unknown types are skipped, + * so the DMA config TLV can be appended to it like for any other gateway. + */ +#define SOF_IPC4_UAOL_XHCI_BDF_ID 0 +#define SOF_IPC4_UAOL_CONFIG_ID 1 +#define SOF_IPC4_UAOL_FIFO_SAO_ID 2 +#define SOF_IPC4_UAOL_EP_INFO_ID 3 +#define SOF_IPC4_UAOL_FEEDBACK_EP_INFO_ID 4 +#define SOF_IPC4_UAOL_ART_DIVIDER_ID 5 +#define SOF_IPC4_UAOL_SERVICE_INTERVAL_ID 6 + +/* PCI address of the xHCI controller the offloaded device is attached to */ +#define SOF_IPC4_UAOL_XHCI_BDF_BUS GENMASK(7, 0) +#define SOF_IPC4_UAOL_XHCI_BDF_DEVICE GENMASK(12, 8) +#define SOF_IPC4_UAOL_XHCI_BDF_FUNCTION GENMASK(15, 13) + +/* UAOL link and stream the gateway is bound to */ +#define SOF_IPC4_UAOL_CONFIG_LINK_IDX GENMASK(7, 0) +#define SOF_IPC4_UAOL_CONFIG_STREAM_IDX GENMASK(15, 8) +#define SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX GENMASK(23, 16) +#define SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD GENMASK(31, 24) + +/* Start offset of each stream within the UAOL FIFO */ +#define SOF_IPC4_UAOL_FIFO_SAO_TX0 GENMASK(11, 0) +#define SOF_IPC4_UAOL_FIFO_SAO_TX1 GENMASK(27, 16) +#define SOF_IPC4_UAOL_FIFO_SAO_RX0 GENMASK(11, 0) +#define SOF_IPC4_UAOL_FIFO_SAO_RX1 GENMASK(27, 16) + +/* Streams per direction the FIFO start offsets of the configuration describe */ +#define SOF_IPC4_UAOL_FIFO_SAO_STREAMS 2 + +/* The firmware rejects a start offset which is not aligned to the IOSF bus */ +#define SOF_IPC4_UAOL_FIFO_BUS_WIDTH 8 + +/* Identity of the offloaded USB endpoint */ +#define SOF_IPC4_UAOL_EP_INFO_DIRECTION BIT(0) +#define SOF_IPC4_UAOL_EP_INFO_NUMBER GENMASK(4, 1) +#define SOF_IPC4_UAOL_EP_INFO_SLOT GENMASK(12, 5) +#define SOF_IPC4_UAOL_EP_INFO_SPLIT BIT(13) +#define SOF_IPC4_UAOL_EP_INFO_HIGH_SPEED BIT(14) +#define SOF_IPC4_UAOL_EP_INFO_MAX_PACKET GENMASK(31, 16) + +/* Conversion of the xHCI global time to the DSP always running timer */ +#define SOF_IPC4_UAOL_ART_MULTIPLIER GENMASK(15, 0) +#define SOF_IPC4_UAOL_ART_DIVIDER GENMASK(31, 16) + +/** + * struct sof_ipc4_uaol_tlv - header of a UAOL gateway configuration TLV + * @type: one of the SOF_IPC4_UAOL_*_ID types + * @length: size of the value following this header, in bytes + */ +struct sof_ipc4_uaol_tlv { + u32 type; + u32 length; +} __packed; + +/** + * struct sof_ipc4_uaol_configuration_blob - UAOL gateway blob + * @xhci_bdf_tlv: header of the xHCI controller address + * @xhci_bdf: PCI address of the xHCI controller + * @config_tlv: header of the link and stream binding + * @config: UAOL link, stream and clock feedback binding + * @fifo_sao_tlv: header of the FIFO offsets + * @fifo_sao_tx: start offset of the two transmit streams + * @fifo_sao_rx: start offset of the two receive streams + * @ep_info_tlv: header of the data endpoint description + * @ep_info: identity of the offloaded data endpoint + * @feedback_ep_info_tlv: header of the feedback endpoint description + * @feedback_ep_info: identity of the clock feedback endpoint, 0 if there is none + * @art_divider_tlv: header of the timestamp conversion factors + * @art_divider: multiplier and divider of the xHCI to DSP timestamp conversion + * @service_interval_tlv: header of the service interval in microseconds + * @service_interval_us: service interval in microseconds, from the USB endpoint descriptor + * + * The firmware expects all of the TLVs to be present even when the values are + * left at zero. + */ +struct sof_ipc4_uaol_configuration_blob { + struct sof_ipc4_uaol_tlv xhci_bdf_tlv; + u32 xhci_bdf; + struct sof_ipc4_uaol_tlv config_tlv; + u32 config; + struct sof_ipc4_uaol_tlv fifo_sao_tlv; + u32 fifo_sao_tx; + u32 fifo_sao_rx; + struct sof_ipc4_uaol_tlv ep_info_tlv; + u32 ep_info; + struct sof_ipc4_uaol_tlv feedback_ep_info_tlv; + u32 feedback_ep_info; + struct sof_ipc4_uaol_tlv art_divider_tlv; + u32 art_divider; + struct sof_ipc4_uaol_tlv service_interval_tlv; + u32 service_interval_us; +} __packed; + +/* + * The gateway node id addresses a UAOL stream by the HDA link stream it uses. + * The mapping between the two is fixed in hardware. + */ +static inline int sof_ipc4_uaol_link_stream(unsigned int stream) +{ + static const u8 link_stream[] = { 13, 14, 15, 16, 29, 30 }; + + if (stream >= ARRAY_SIZE(link_stream)) + return -EINVAL; + + return link_stream[stream]; +} + +int sof_ipc4_uaol_get_fifo_size(struct snd_sof_dev *sdev, int link_idx, int direction); + /** * struct sof_ipc4_copier - copier config data * @data: IPC copier data diff --git a/sound/soc/sof/sof-priv.h b/sound/soc/sof/sof-priv.h index 83e4d83cc27cbb..9bbd57e21fd32f 100644 --- a/sound/soc/sof/sof-priv.h +++ b/sound/soc/sof/sof-priv.h @@ -359,6 +359,9 @@ struct snd_sof_dsp_ops { struct snd_soc_acpi_mach * (*machine_select)(struct snd_sof_dev *sdev); /* optional */ void (*set_mach_params)(struct snd_soc_acpi_mach *mach, struct snd_sof_dev *sdev); /* optional */ + /* last word on the params handed to the machine driver, runs with the firmware booted */ + void (*finalize_mach_params)(struct snd_sof_dev *sdev, + struct snd_soc_acpi_mach_params *params); /* optional */ /* IPC client ops */ int (*register_ipc_clients)(struct snd_sof_dev *sdev); /* optional */ diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c index 750916b57def74..1aae49687bfde9 100644 --- a/sound/soc/sof/topology.c +++ b/sound/soc/sof/topology.c @@ -298,6 +298,7 @@ static const struct sof_dai_types sof_dais[] = { {"HDA", SOF_DAI_INTEL_HDA}, {"DMIC", SOF_DAI_INTEL_DMIC}, {"ALH", SOF_DAI_INTEL_ALH}, + {"UAOL", SOF_DAI_INTEL_UAOL}, {"SAI", SOF_DAI_IMX_SAI}, {"ESAI", SOF_DAI_IMX_ESAI}, {"ACPBT", SOF_DAI_AMD_BT}, diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig index b4588915efa114..3ba255dbae3cc3 100644 --- a/sound/usb/Kconfig +++ b/sound/usb/Kconfig @@ -205,6 +205,7 @@ config SND_USB_AUDIO_QMI will be called snd-usb-audio-qmi. source "sound/usb/line6/Kconfig" +source "sound/usb/offload/Kconfig" endif # SND_USB diff --git a/sound/usb/Makefile b/sound/usb/Makefile index 2e842dcc73de1a..9759e2015ee96a 100644 --- a/sound/usb/Makefile +++ b/sound/usb/Makefile @@ -38,3 +38,4 @@ obj-$(CONFIG_SND_USB_US122L) += snd-usbmidi-lib.o obj-$(CONFIG_SND) += misc/ usx2y/ caiaq/ 6fire/ hiface/ bcd2000/ qcom/ obj-$(CONFIG_SND_USB_LINE6) += line6/ +obj-$(CONFIG_SND_USB_OFFLOAD) += offload/ diff --git a/sound/usb/card.c b/sound/usb/card.c index 59979b793fe91f..9451cf40e5d84d 100644 --- a/sound/usb/card.c +++ b/sound/usb/card.c @@ -195,6 +195,31 @@ void snd_usb_rediscover_devices(void) } EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices); +/* + * The counterpart of snd_usb_rediscover_devices(): run the disconnect + * callback for every connected USB SND device, then unregister the platform + * operations. A platform which keeps state for the devices it was told of + * lets go of all of them this way. Unregistering alone leaves it to clean up + * after a device which could be freed under it, as one disconnected in the + * meantime would not be reported anymore. + */ +void snd_usb_release_platform_ops(void) +{ + int i; + + guard(mutex)(®ister_mutex); + + if (platform_ops && platform_ops->disconnect_cb) { + for (i = 0; i < SNDRV_CARDS; i++) { + if (usb_chip[i]) + platform_ops->disconnect_cb(usb_chip[i]); + } + } + + platform_ops = NULL; +} +EXPORT_SYMBOL_GPL(snd_usb_release_platform_ops); + /* * Checks to see if requested audio profile, i.e sample rate, # of * channels, etc... is supported by the substream associated to the diff --git a/sound/usb/card.h b/sound/usb/card.h index f956bcf00f77b4..e2f51608b87cdb 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h @@ -67,6 +67,7 @@ struct snd_usb_endpoint { struct snd_usb_clock_ref *clock_ref; int opened; /* open refcount; protect with chip->mutex */ + bool offloaded; /* driven by an offload engine; protect with chip->mutex */ atomic_t running; /* running status */ int ep_num; /* the referenced endpoint number */ int type; /* SND_USB_ENDPOINT_TYPE_* */ @@ -167,7 +168,7 @@ struct snd_usb_substream { unsigned int pkt_offset_adj; /* Bytes to drop from beginning of packets (for non-compliant devices) */ unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream (for non-compliant devices) */ - unsigned int opened:1; /* pcm device opened */ + unsigned int opened:1; /* pcm device opened or claimed for offload */ unsigned int running: 1; /* running status */ unsigned int period_elapsed_pending; /* delay period handling */ @@ -224,6 +225,7 @@ snd_usb_find_suppported_substream(int card_idx, struct snd_pcm_hw_params *params int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops); int snd_usb_unregister_platform_ops(void); +void snd_usb_release_platform_ops(void); void snd_usb_rediscover_devices(void); #endif /* __USBAUDIO_CARD_H */ diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index 8dd0b3525e4b07..9973a4706ed7d3 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c @@ -792,6 +792,9 @@ bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip, * Note that this function doesn't configure the endpoint. The substream * needs to set it up later via snd_usb_endpoint_set_params() and * snd_usb_endpoint_prepare(). + * + * Returns the endpoint, or an ERR_PTR(): -EBUSY if an offload engine drives + * the endpoint, see snd_usb_endpoint_claim_offload(), -EINVAL otherwise. */ struct snd_usb_endpoint * snd_usb_endpoint_open(struct snd_usb_audio *chip, @@ -807,7 +810,7 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, ep = snd_usb_get_endpoint(chip, ep_num); if (!ep) { usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num); - return NULL; + return ERR_PTR(-EINVAL); } if (!ep->opened) { @@ -825,12 +828,12 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, ep->iface_ref = iface_ref_find(chip, ep->iface); if (!ep->iface_ref) - return NULL; + return ERR_PTR(-EINVAL); if (fp->protocol != UAC_VERSION_1) { ep->clock_ref = clock_ref_find(chip, fp->clock); if (!ep->clock_ref) - return NULL; + return ERR_PTR(-EINVAL); ep->clock_ref->opened++; } @@ -860,12 +863,18 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, } else { if (WARN_ON(!ep->iface_ref)) - return NULL; + return ERR_PTR(-EINVAL); + + /* the host must not submit URBs on an endpoint a DSP drives */ + if (ep->offloaded) { + usb_audio_dbg(chip, "EP 0x%x is offloaded\n", ep_num); + return ERR_PTR(-EBUSY); + } if (!endpoint_compatible(ep, fp, params)) { usb_audio_err(chip, "Incompatible EP setup for 0x%x\n", ep_num); - return NULL; + return ERR_PTR(-EINVAL); } usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n", @@ -977,10 +986,49 @@ void snd_usb_endpoint_close(struct snd_usb_audio *chip, ep->cur_rate = 0; ep->iface_ref = NULL; ep->clock_ref = NULL; + ep->offloaded = false; usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); } } +/** + * snd_usb_endpoint_claim_offload - hand an open endpoint over to an offload engine + * @chip: the USB audio chip + * @ep: the endpoint, opened for the substream which is offloaded + * + * A DSP drives the endpoint from now on, snd_usb_endpoint_open() refuses to + * share it with another stream, as no host stream must submit URBs on it. An + * endpoint which another stream has open as well, like the capture endpoint a + * playback stream with implicit feedback syncs to, cannot be claimed. + * + * Return: 0 on success, -EBUSY if @ep is in use elsewhere. + */ +int snd_usb_endpoint_claim_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep) +{ + guard(mutex)(&chip->mutex); + if (ep->opened > 1 || ep->offloaded) + return -EBUSY; + + ep->offloaded = true; + + return 0; +} +EXPORT_SYMBOL_GPL(snd_usb_endpoint_claim_offload); + +/** + * snd_usb_endpoint_release_offload - give a claimed endpoint back to the host + * @chip: the USB audio chip + * @ep: the endpoint snd_usb_endpoint_claim_offload() claimed + */ +void snd_usb_endpoint_release_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep) +{ + guard(mutex)(&chip->mutex); + ep->offloaded = false; +} +EXPORT_SYMBOL_GPL(snd_usb_endpoint_release_offload); + /* Prepare for suspening EP, called from the main suspend handler */ void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep) { diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h index ba70f52f68602e..2ea22835fcd968 100644 --- a/sound/usb/endpoint.h +++ b/sound/usb/endpoint.h @@ -18,6 +18,10 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, bool fixed_rate); void snd_usb_endpoint_close(struct snd_usb_audio *chip, struct snd_usb_endpoint *ep); +int snd_usb_endpoint_claim_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep); +void snd_usb_endpoint_release_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep); int snd_usb_endpoint_set_params(struct snd_usb_audio *chip, struct snd_usb_endpoint *ep); int snd_usb_endpoint_prepare(struct snd_usb_audio *chip, diff --git a/sound/usb/offload/Kconfig b/sound/usb/offload/Kconfig new file mode 100644 index 00000000000000..2361d6db2e0282 --- /dev/null +++ b/sound/usb/offload/Kconfig @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only +config SND_USB_OFFLOAD + tristate "USB Audio Offload library" + depends on SND_USB_AUDIO + depends on USB_XHCI_HCD && USB_XHCI_SIDEBAND + help + Library implementing the lifecycle of an offloaded USB audio + device, for a driver that owns a DSP capable of driving USB + endpoints. A host registers once, is told of the offload capable + devices as they come and go, and acquires their streams for the + DSP on demand. + + Device ownership stays with snd-usb-audio: the library hooks the + USB-SND platform-op callbacks to run a UAC candidate filter and + forward offloadable devices to the registered host. + + The data path is built on the xHCI Audio Sideband: the library + claims a sideband resource for each offloaded endpoint, which makes + the controller route the endpoint's traffic to the DSP. + + If unsure, say N. diff --git a/sound/usb/offload/Makefile b/sound/usb/offload/Makefile new file mode 100644 index 00000000000000..9ec94222e30653 --- /dev/null +++ b/sound/usb/offload/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Makefile for the USB Audio Offload library +# + +snd-usb-offload-y := init.o usb-audio.o +obj-$(CONFIG_SND_USB_OFFLOAD) += snd-usb-offload.o diff --git a/sound/usb/offload/init.c b/sound/usb/offload/init.c new file mode 100644 index 00000000000000..b79afd5088294a --- /dev/null +++ b/sound/usb/offload/init.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * USB audio offload library - top level lifecycle. + * + * Exported in the USB_AUDIO_OFFLOAD module namespace. + */ +#include +#include +#include +#include +#include +#include + +#include "offload.h" + +/** + * usb_offload_register - register an offload engine with the library + * @res: the host's device and callbacks + * + * Starts tracking the offload capable USB audio devices for the host and + * reports the ones that are already connected through res->ops. + * + * Return: the context to pass to the other API calls, or an ERR_PTR(). + */ +struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res) +{ + struct usb_offload_ctx *ctx; + int ret; + + if (!res || !res->dev || !res->ops || !res->ops->notify_connect || + !res->ops->notify_disconnect) + return ERR_PTR(-EINVAL); + + ctx = kzalloc_obj(*ctx); + if (!ctx) + return ERR_PTR(-ENOMEM); + + ctx->dev = res->dev; + ctx->ops = res->ops; + ctx->s24_3le_as_s24_le = res->s24_3le_as_s24_le; + ctx->async_playback_on_host = res->async_playback_on_host; + + ret = usb_offload_usb_audio_init(ctx); + if (ret) { + kfree(ctx); + return ERR_PTR(ret); + } + + /* + * Backfill the USB audio devices that enumerated before the bridge + * registered, e.g. when the DSP came up after USB enumeration finished. + */ + usb_offload_usb_audio_rediscover(); + + return ctx; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_register, "USB_AUDIO_OFFLOAD"); + +/** + * usb_offload_unregister - unregister an offload engine + * @ctx: the context usb_offload_register() returned + * + * Gives every stream still held back to the host, reports the devices as + * disconnected and frees the context. + */ +void usb_offload_unregister(struct usb_offload_ctx *ctx) +{ + if (IS_ERR_OR_NULL(ctx)) + return; + + usb_offload_usb_audio_exit(ctx); + kfree(ctx); +} +EXPORT_SYMBOL_NS_GPL(usb_offload_unregister, "USB_AUDIO_OFFLOAD"); + +/* + * Hotplug bridge - called by the USB SND side + * + * USB audio devices appear and disappear at runtime. Each event is + * delivered to the host via ops->notify_connect / notify_disconnect. On + * connect the USB-SND device identity (@id, card / chip / PCM indices) + * travels with the event so the host can build a struct snd_soc_usb_device + * without reaching into the USB-audio core. + */ +int usb_offload_connect(struct usb_offload_ctx *ctx, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id) +{ + int ret; + + ret = ctx->ops->notify_connect(ctx->dev, udev, id); + if (ret) { + dev_warn(ctx->dev, "USB offload: %s: connect rejected: %d\n", + dev_name(&udev->dev), ret); + return ret; + } + + dev_dbg(ctx->dev, "USB offload: %s connected\n", dev_name(&udev->dev)); + return 0; +} + +void usb_offload_disconnect(struct usb_offload_ctx *ctx, struct usb_device *udev) +{ + ctx->ops->notify_disconnect(ctx->dev, udev); + + dev_dbg(ctx->dev, "USB offload: %s disconnected\n", dev_name(&udev->dev)); +} + +/* + * Resource placement + * + * Runs from the stream acquisition path, under the usb-audio side's lock. + */ +int usb_offload_resource_range(struct usb_offload_ctx *ctx, int direction, + bool feedback, unsigned int *first, + unsigned int *last) +{ + if (ctx->ops->get_resource_range) + return ctx->ops->get_resource_range(ctx->dev, direction, + feedback, first, last); + + *first = 0; + *last = 0; + + return 0; +} + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("USB audio offload library"); diff --git a/sound/usb/offload/offload.h b/sound/usb/offload/offload.h new file mode 100644 index 00000000000000..e49433abd2eab3 --- /dev/null +++ b/sound/usb/offload/offload.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Internal definitions for the USB audio offload library. + * + * The host callback table lives in ; the host turns + * those callbacks into IPC messages. The ASoC USB framework port is not + * owned here: the host owns the snd_soc_usb port and drives the + * connection callbacks, the library only forwards hotplug events through + * ops->notify_connect / notify_disconnect. + */ + +#ifndef __SOUND_USB_OFFLOAD_LIB_H +#define __SOUND_USB_OFFLOAD_LIB_H + +#include + +/* + * One offload engine. Returned to the host by usb_offload_register() and + * passed back in to every other API call. + */ +struct usb_offload_ctx { + struct device *dev; + const struct usb_offload_ops *ops; + bool s24_3le_as_s24_le; + bool async_playback_on_host; +}; + +/* Where the host wants a stream's endpoint placed (called from usb-audio.c) */ +int usb_offload_resource_range(struct usb_offload_ctx *ctx, int direction, + bool feedback, unsigned int *first, + unsigned int *last); + +/* + * Candidate filter + USB-audio device ownership (usb-audio.c) + * + * No dedicated USB driver claims the device: snd-usb-audio keeps + * ownership and notifies us via snd_usb_platform_ops. We run a strict + * UAC descriptor filter and bridge the hotplug event into the library for + * offloadable devices. + */ + +/** + * struct usb_offload_candidate - parsed offload description of a UAC device. + * @ac_ifnum: audio-control interface number. + * @uac_version: UAC_VERSION_1 / _2 / _3. + * @num_eps: data and feedback endpoints across all streaming ifaces. + */ +struct usb_offload_candidate { + u8 ac_ifnum; + u8 uac_version; + u8 num_eps; +}; + +/* + * USB hotplug bridge: how the usb-audio side hands a device to the library, + * which propagates it to the host via ops->notify_connect / notify_disconnect. + */ +int usb_offload_connect(struct usb_offload_ctx *ctx, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id); +void usb_offload_disconnect(struct usb_offload_ctx *ctx, struct usb_device *udev); + +/* + * Register / unregister the snd_usb_platform_ops bridge. init() is called + * from usb_offload_register(), exit() from usb_offload_unregister(). Both + * manage the per-device offload records. + */ +int usb_offload_usb_audio_init(struct usb_offload_ctx *ctx); +void usb_offload_usb_audio_exit(struct usb_offload_ctx *ctx); + +/* + * Re-emit connect events for USB audio devices that enumerated before the + * platform ops were registered. Wraps the usb-audio core's + * snd_usb_rediscover_devices(), which owns the private sound/usb headers; the + * connect callback is idempotent so this is safe to call repeatedly. + */ +void usb_offload_usb_audio_rediscover(void); + +#endif /* __SOUND_USB_OFFLOAD_LIB_H */ diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c new file mode 100644 index 00000000000000..c58abfd375cf37 --- /dev/null +++ b/sound/usb/offload/usb-audio.c @@ -0,0 +1,1545 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * USB audio offload library - USB-audio device ownership + candidate filter. + * + * snd-usb-audio stays in charge of the device. This file hooks the + * snd_usb_platform_ops connect/disconnect notifications of the usb-audio + * core (sound/usb/card.c), runs a UAC descriptor filter to decide + * whether a device can be offloaded, and for the candidates it bridges the + * hotplug event into the library via usb_offload_connect() / + * usb_offload_disconnect(). + * + * The xHCI audio sideband resources and the DSP gateway configuration + * happen later, on the PCM data path: usb_offload_acquire_stream() starts + * the stream on the USB side and claims the endpoint for the DSP. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* PORTSC Port Speed ID of a high-speed root port, xHCI 7.2.2.1.1 */ +#define USB_OFFLOAD_PORT_SPEED_HIGH 3 + +/* + * snd_usb_audio and the snd_usb_platform_ops bridge live in the private + * usb-audio headers one directory up. The Qualcomm in-tree consumer + * (sound/usb/qcom/) includes them the same way. + */ +#include "../usbaudio.h" +#include "../card.h" +#include "../endpoint.h" +#include "../pcm.h" +#include "../power.h" +#include "../quirks.h" + +#include "offload.h" + +static bool offload_disable; +module_param(offload_disable, bool, 0444); +MODULE_PARM_DESC(offload_disable, + "Disable USB audio offload candidate selection (default: 0)"); + +static bool allow_feedback; +module_param(allow_feedback, bool, 0444); +MODULE_PARM_DESC(allow_feedback, + "Offload asynchronous playback streams also when the host keeps them (default: 0)"); + +/* + * An asynchronous OUT endpoint runs off the device's own clock and needs a + * feedback source for the sender to follow it. A host whose DSP applies that + * feedback in a way which degrades the audio keeps such streams to itself + * (usb_offload_res.async_playback_on_host), unless allowed for testing. This + * also covers implicit feedback, whose sync source is a capture data endpoint + * the DSP cannot use: that stream would run blind. A quirk can give an + * endpoint which is not declared asynchronous implicit feedback, so that is + * checked for on its own. + */ +static bool usb_offload_playback_kept(const struct usb_offload_ctx *ctx, + const struct audioformat *fp) +{ + if (!ctx->async_playback_on_host || allow_feedback) + return false; + + return fp->implicit_fb || + (fp->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_ASYNC; +} + +/* + * The capture endpoint a playback format with implicit feedback syncs to is + * that playback's clock. Giving it to the DSP would leave a playback stream + * which the host keeps without one, so the host keeps such a capture format + * as well. + * + * A capture format with implicit feedback of its own, which a quirk gives to + * a device that only captures while its playback endpoint streams, stays on + * the host too: the DSP only gets the capture endpoint and nothing would + * stream the playback one. + */ +static bool usb_offload_fmt_offloadable(const struct usb_offload_ctx *ctx, + struct snd_usb_audio *chip, + const struct audioformat *fp, int direction) +{ + const struct audioformat *pfp; + struct snd_usb_stream *as; + + if (direction == SNDRV_PCM_STREAM_PLAYBACK) + return !usb_offload_playback_kept(ctx, fp); + + if (fp->implicit_fb) + return false; + + list_for_each_entry(as, &chip->pcm_list, list) { + /* the list of a direction a stream does not have is not set up */ + if (!as->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) + continue; + + list_for_each_entry(pfp, &as->substream[SNDRV_PCM_STREAM_PLAYBACK].fmt_list, list) { + if (pfp->implicit_fb && pfp->sync_ep == fp->endpoint && + usb_offload_playback_kept(ctx, pfp)) + return false; + } + } + + return true; +} + +static bool usb_offload_subs_offloadable(const struct usb_offload_ctx *ctx, + const struct snd_usb_substream *subs) +{ + const struct audioformat *fp; + + list_for_each_entry(fp, &subs->fmt_list, list) + if (usb_offload_fmt_offloadable(ctx, subs->stream->chip, fp, + subs->direction)) + return true; + + return false; +} + +/* + * Candidate filter + * + * Walks the audio-control interface and checks each streaming interface's + * data + feedback endpoints. All-or-nothing: any malformed descriptor makes + * the whole device a non-candidate. + */ + +/* Common 3-byte header shared by all class-specific AC descriptors. */ +struct usb_offload_cs_hdr { + __u8 bLength; + __u8 bDescriptorType; + __u8 bDescriptorSubtype; +} __packed; + +/* Endpoint usage bits (bmAttributes), see USB_ENDPOINT_USAGE_* in ch9.h. */ +static inline int usb_offload_ep_usage(const struct usb_endpoint_descriptor *epd) +{ + return epd->bmAttributes & USB_ENDPOINT_USAGE_MASK; +} + +/* + * Check the data (and optional feedback) endpoint of one interface of the + * audio function and count them in @cand. Returns 0 (also when the interface + * is not a streaming one, like MIDI, or has no streaming altsetting) or a + * negative errno on a malformed descriptor. + */ +static int usb_offload_check_iface_eps(struct usb_interface *iface, + struct usb_offload_candidate *cand) +{ + struct usb_interface_descriptor *altd; + struct usb_endpoint_descriptor *epd; + struct usb_host_interface *alt; + int i; + + altd = &iface->altsetting[0].desc; + if (altd->bInterfaceClass != USB_CLASS_AUDIO || + altd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) + return 0; + + /* Find the first altsetting that actually has endpoints. */ + for (i = 0; i < iface->num_altsetting; i++) { + alt = &iface->altsetting[i]; + altd = &alt->desc; + + if (altd->bInterfaceClass != USB_CLASS_AUDIO || + altd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING || + altd->bNumEndpoints > 2) + return -EINVAL; + if (altd->bNumEndpoints) + break; + } + if (i == iface->num_altsetting) + return 0; /* no streaming altsetting: nothing to offload */ + + /* endpoint[0] is the data endpoint (mandatory, isochronous). */ + epd = &alt->endpoint[0].desc; + if (usb_offload_ep_usage(epd) != USB_ENDPOINT_USAGE_DATA || + !usb_endpoint_maxp(epd) || !usb_endpoint_xfer_isoc(epd)) + return -EINVAL; + cand->num_eps++; + + /* endpoint[1], when present, must be an IN isochronous feedback ep. */ + if (altd->bNumEndpoints == 2) { + epd = &alt->endpoint[1].desc; + if (usb_offload_ep_usage(epd) != USB_ENDPOINT_USAGE_FEEDBACK || + !usb_endpoint_maxp(epd) || !usb_endpoint_xfer_isoc(epd) || + usb_endpoint_dir_out(epd)) + return -EINVAL; + cand->num_eps++; + } + + return 0; +} + +static void *usb_offload_uac1_find_ac_desc(struct usb_host_interface *iface, + size_t desc_size, u8 type, u8 subtype) +{ + struct usb_offload_cs_hdr *hdr; + u8 *pos, *end; + + pos = iface->extra; + end = pos + iface->extralen; + + while (pos < end) { + hdr = (struct usb_offload_cs_hdr *)pos; + pos += hdr->bLength; + + if (pos > end || hdr->bLength < sizeof(*hdr)) + return NULL; + + if (hdr->bLength >= desc_size && + hdr->bDescriptorType == type && + hdr->bDescriptorSubtype == subtype) + return hdr; + } + + return NULL; +} + +static int usb_offload_uac1_enum_endpoints(struct usb_device *udev, + struct usb_interface *audctrl, + struct usb_offload_candidate *cand) +{ + struct usb_host_interface *host_iface; + struct uac1_ac_header_descriptor *acd; + struct usb_interface *iface; + int tail_bytes; + int ifnum, i; + int ret; + + host_iface = &audctrl->altsetting[0]; + acd = usb_offload_uac1_find_ac_desc(host_iface, sizeof(*acd), + USB_DT_CS_INTERFACE, UAC_HEADER); + if (!acd) + return -EINVAL; + + tail_bytes = (void *)(host_iface->extra + host_iface->extralen) - (void *)acd; + if (tail_bytes < sizeof(*acd)) + return -EINVAL; + if (!acd->bInCollection || tail_bytes < acd->bLength) + return -EINVAL; + if (acd->bLength < sizeof(*acd) + acd->bInCollection) + return -EINVAL; + + for (i = 0; i < acd->bInCollection; i++) { + ifnum = acd->baInterfaceNr[i]; + iface = usb_ifnum_to_if(udev, ifnum); + if (!iface) + return -EINVAL; + + ret = usb_offload_check_iface_eps(iface, cand); + if (ret) + return ret; + } + + return 0; +} + +static int usb_offload_uac2_enum_endpoints(struct usb_device *udev, + struct usb_interface *audctrl, + struct usb_offload_candidate *cand) +{ + struct usb_interface_assoc_descriptor *assoc; + struct usb_interface *iface; + int ifnum, i; + int ret; + + assoc = audctrl->intf_assoc; + /* At least one streaming interface besides the control one. */ + if (!assoc || assoc->bInterfaceCount < 2) + return -EINVAL; + + for (i = 0; i < assoc->bInterfaceCount; i++) { + ifnum = assoc->bFirstInterface + i; + iface = usb_ifnum_to_if(udev, ifnum); + if (!iface) + return -EINVAL; + if (iface == audctrl) + continue; + + ret = usb_offload_check_iface_eps(iface, cand); + if (ret) + return ret; + } + + return 0; +} + +static struct usb_interface * +usb_offload_find_audio_control(struct usb_device *udev, + struct usb_interface_descriptor **altd_out) +{ + struct usb_host_config *config = udev->actconfig; + struct usb_interface_descriptor *altd; + struct usb_interface *iface; + int i; + + if (!config) + return NULL; + + for (i = 0; i < config->desc.bNumInterfaces; i++) { + iface = config->interface[i]; + if (!iface || !iface->altsetting) + continue; + + altd = &iface->altsetting[0].desc; + if (altd->bInterfaceClass == USB_CLASS_AUDIO && + altd->bInterfaceSubClass == USB_SUBCLASS_AUDIOCONTROL) { + *altd_out = altd; + return iface; + } + } + + return NULL; +} + +static int usb_offload_try_offload(struct usb_device *udev, + struct usb_offload_candidate *cand) +{ + struct usb_interface_descriptor *altd; + struct usb_interface *audctrl; + int ret; + + if (!udev || !cand) + return -EINVAL; + if (offload_disable) + return -EOPNOTSUPP; + + switch (udev->speed) { + case USB_SPEED_FULL: + case USB_SPEED_HIGH: + break; + default: + return -EOPNOTSUPP; + } + + audctrl = usb_offload_find_audio_control(udev, &altd); + if (!audctrl) + return -EINVAL; + + memset(cand, 0, sizeof(*cand)); + cand->ac_ifnum = altd->bInterfaceNumber; + + switch (altd->bInterfaceProtocol) { + case UAC_VERSION_1: + cand->uac_version = UAC_VERSION_1; + ret = usb_offload_uac1_enum_endpoints(udev, audctrl, cand); + break; + case UAC_VERSION_2: + case UAC_VERSION_3: + cand->uac_version = altd->bInterfaceProtocol; + ret = usb_offload_uac2_enum_endpoints(udev, audctrl, cand); + break; + default: + return -EOPNOTSUPP; + } + + if (ret) + return ret; + if (!cand->num_eps) + return -EOPNOTSUPP; + + return 0; +} + +/* + * Device-ownership bridge (snd_usb_platform_ops) + * + * One module-global registration; the usb-audio core allows only a + * single set of platform ops. We map each offloaded snd_usb_audio chip + * to a record holding its candidate description. + */ + +/** + * struct usb_offload_ua_stream - one acquired stream of a device. + * @node: link on usb_offload_dev.streams. + * @subs: the claimed usb-audio substream. + * @direction: SNDRV_PCM_STREAM_* of the stream. + * @pcm_index: USB SND PCM device number of @subs. + * @resource: sideband resource ID of the data endpoint. + * @fb_resource: sideband resource ID of the feedback endpoint, 0 if none. + * @data_host_ep: the data endpoint. + * @feedback_host_ep: the feedback endpoint, NULL if none. + * + * A device can have several PCMs per direction, and each can be offloaded + * on a stream of its own at the same time. + */ +struct usb_offload_ua_stream { + struct list_head node; + struct snd_usb_substream *subs; + int direction; + int pcm_index; + u8 resource; + u8 fb_resource; + struct usb_host_endpoint *data_host_ep; + struct usb_host_endpoint *feedback_host_ep; +}; + +/** + * struct usb_offload_dev - per-offloaded-device record. + * @node: link on usb_offload_ua_devs. + * @chip: the owning usb-audio chip. + * @udev: shortcut to chip->dev. + * @sb: xHCI sideband instance for this device. + * @cand: parsed candidate description. + * @connected: true once usb_offload_connect() succeeded. + * @streams: the streams currently acquired on the device. + */ +struct usb_offload_dev { + struct list_head node; + struct snd_usb_audio *chip; + struct usb_device *udev; + struct xhci_sideband *sb; + struct usb_offload_candidate cand; + bool connected; + struct list_head streams; +}; + +static DEFINE_MUTEX(usb_offload_ua_lock); +static struct usb_offload_ctx *usb_offload_ua_ctx; /* active offload ctx */ +static LIST_HEAD(usb_offload_ua_devs); + +/* Caller holds usb_offload_ua_lock. */ +static struct usb_offload_dev *usb_offload_ua_find(struct snd_usb_audio *chip) +{ + struct usb_offload_dev *dev; + + list_for_each_entry(dev, &usb_offload_ua_devs, node) + if (dev->chip == chip) + return dev; + return NULL; +} + +/* Caller holds usb_offload_ua_lock. */ +static struct usb_offload_dev *usb_offload_ua_find_udev(struct usb_device *udev) +{ + struct usb_offload_dev *dev; + + list_for_each_entry(dev, &usb_offload_ua_devs, node) + if (dev->udev == udev) + return dev; + return NULL; +} + +/* Caller holds usb_offload_ua_lock. */ +static struct usb_offload_ua_stream * +usb_offload_ua_find_stream(struct usb_offload_dev *dev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + + list_for_each_entry(stream, &dev->streams, node) + if (stream->direction == direction && stream->pcm_index == pcm_index) + return stream; + return NULL; +} + +static void usb_offload_fill_ep(struct usb_offload_ep_info *desc, + const struct usb_host_endpoint *host_ep) +{ + desc->ep_addr = host_ep->desc.bEndpointAddress; + desc->maxpacket = usb_endpoint_maxp(&host_ep->desc); + desc->interval = host_ep->desc.bInterval; +} + +/* + * Whether the stream has an explicit clock feedback endpoint for the DSP to + * drive. usb-audio flags the data endpoint of a stream which syncs to an + * implicit feedback source, not the capture endpoint it syncs to. + */ +static bool usb_offload_ua_explicit_fb(struct snd_usb_substream *subs) +{ + return subs->sync_endpoint && !subs->data_endpoint->implicit_fb_sync; +} + +/* + * The endpoints the DSP drives are kept away from the host for as long as the + * stream is offloaded. One which another stream has open already, like the + * capture endpoint a host playback stream with implicit feedback syncs to, + * cannot be taken. + */ +static int usb_offload_ua_claim_eps(struct snd_usb_audio *chip, + struct snd_usb_substream *subs) +{ + int ret; + + ret = snd_usb_endpoint_claim_offload(chip, subs->data_endpoint); + if (ret) + return ret; + + if (usb_offload_ua_explicit_fb(subs)) { + ret = snd_usb_endpoint_claim_offload(chip, subs->sync_endpoint); + if (ret) + snd_usb_endpoint_release_offload(chip, subs->data_endpoint); + } + + return ret; +} + +static void usb_offload_ua_release_eps(struct snd_usb_audio *chip, + struct snd_usb_substream *subs) +{ + if (usb_offload_ua_explicit_fb(subs)) + snd_usb_endpoint_release_offload(chip, subs->sync_endpoint); + snd_usb_endpoint_release_offload(chip, subs->data_endpoint); +} + +/* + * Start the device streaming so that its endpoints, and with them their + * xHCI transfer rings, exist. This is what snd_usb_pcm_prepare() does for + * a host driven stream. Caller holds usb_offload_ua_lock. + */ +static int usb_offload_ua_start_stream(struct usb_offload_dev *dev, + struct snd_usb_substream *subs, + struct snd_pcm_hw_params *params) +{ + struct snd_usb_audio *chip = dev->chip; + int ret; + + ret = snd_usb_offload_claim_substream(subs); + if (ret) + return ret; + + ret = snd_usb_autoresume(chip); + if (ret < 0) + goto err_release; + + ret = snd_usb_hw_params(subs, params); + if (ret < 0) + goto err_suspend; + + /* before anything is set up on an endpoint another stream may run */ + ret = usb_offload_ua_claim_eps(chip, subs); + if (ret) { + dev_dbg(&dev->udev->dev, + "USB offload: endpoint in use by another stream\n"); + goto err_hw_free; + } + + if (atomic_read(&chip->shutdown)) { + ret = -ENODEV; + goto err_release_eps; + } + + { + CLASS(snd_usb_lock, pm)(chip); + + if (pm.err < 0) { + ret = pm.err; + goto err_release_eps; + } + + if (subs->sync_endpoint) { + ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint); + if (ret < 0) + goto err_release_eps; + } + + ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint); + if (ret < 0) + goto err_release_eps; + /* the device's own format setup, as snd_usb_pcm_prepare() runs it */ + if (ret > 0) + snd_usb_set_format_quirk(subs, subs->cur_audiofmt); + } + + /* + * A quirk can leave the host skipping bytes at the start of the stream + * or of each packet to get the samples aligned, which only the URB path + * does. The DSP would get them misaligned. + */ + if (subs->stream_offset_adj || subs->pkt_offset_adj) { + dev_dbg(&dev->udev->dev, + "USB offload: format needs the host to align the data, not offloaded\n"); + ret = -EOPNOTSUPP; + goto err_release_eps; + } + + return 0; + +err_release_eps: + usb_offload_ua_release_eps(chip, subs); +err_hw_free: + snd_usb_hw_free(subs); +err_suspend: + snd_usb_autosuspend(chip); +err_release: + snd_usb_offload_release_substream(subs); + + return ret; +} + +/* Caller holds usb_offload_ua_lock. */ +static void usb_offload_ua_stop_stream(struct usb_offload_dev *dev, + struct snd_usb_substream *subs) +{ + usb_offload_ua_release_eps(dev->chip, subs); + snd_usb_hw_free(subs); + snd_usb_autosuspend(dev->chip); + snd_usb_offload_release_substream(subs); +} + +/* + * Detach the stream's endpoints from their sideband resources, give the + * substream back to the host and drop the record. Caller holds + * usb_offload_ua_lock. + */ +static void usb_offload_ua_release(struct usb_offload_dev *dev, + struct usb_offload_ua_stream *stream) +{ + if (stream->feedback_host_ep) { + xhci_sideband_remove_endpoint(dev->sb, stream->feedback_host_ep); + xhci_sideband_free_resource_id(dev->sb, stream->fb_resource); + } + if (stream->data_host_ep) { + xhci_sideband_remove_endpoint(dev->sb, stream->data_host_ep); + xhci_sideband_free_resource_id(dev->sb, stream->resource); + } + + usb_offload_ua_stop_stream(dev, stream->subs); + + list_del(&stream->node); + kfree(stream); +} + +/* + * Reserve the sideband resource @host_ep is offloaded on. The host says + * where an endpoint of this function belongs when its engine ties the + * resources to functions, and the first free one of those is taken; a host + * without an opinion gets the first free resource of the controller. + * Caller holds usb_offload_ua_lock. + */ +static int usb_offload_ua_alloc_resource(struct usb_offload_dev *dev, + struct usb_host_endpoint *host_ep, + int direction, bool feedback) +{ + unsigned int first, last; + int ret; + + ret = usb_offload_resource_range(usb_offload_ua_ctx, direction, feedback, + &first, &last); + if (ret) + return ret; + + ret = xhci_sideband_alloc_resource_id(dev->sb, host_ep, first, last); + if (ret == -EBUSY) + dev_dbg(&dev->udev->dev, "USB offload: no free %s resource in %u-%u\n", + feedback ? "feedback" : "data", first, last); + + return ret; +} + +int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index, + struct snd_pcm_hw_params *params, + struct usb_offload_stream_hw *hw) +{ + const struct audioformat *format; + struct usb_host_endpoint *host_ep; + struct usb_host_endpoint *fb_host_ep = NULL; + struct usb_offload_ua_stream *stream; + struct snd_usb_substream *subs; + struct usb_offload_dev *dev; + struct xhci_sideband_ep_properties ep_prop; + struct xhci_sideband_ep_properties fb_ep_prop; + struct snd_usb_stream *as; + bool kept = false; + int fb_res_id = 0; + u8 interval; + int res_id; + int ret; + + if (!ctx || !udev || !params || !hw) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return -ENODEV; + + dev = usb_offload_ua_find_udev(udev); + if (!dev || !dev->connected) + return -ENODEV; + + /* + * snd_usb_find_suppported_substream() would do this, but it takes the + * usb-audio register_mutex to turn a card index into the chip. The + * chip is already known here, and the caller is a backend DAI holding + * the ASoC card's pcm_mutex, which that mutex nests inside. + */ + subs = NULL; + list_for_each_entry(as, &dev->chip->pcm_list, list) { + if (!as->pcm || (pcm_index >= 0 && as->pcm->device != pcm_index)) + continue; + if (!as->substream[direction].num_formats) + continue; + + format = snd_usb_find_substream_format(&as->substream[direction], + params); + if (!format) + continue; + /* find_format() already prefers a non-async altsetting if one matches */ + if (!usb_offload_fmt_offloadable(ctx, dev->chip, format, direction)) { + kept = true; + continue; + } + subs = &as->substream[direction]; + break; + } + if (!subs && kept) { + dev_dbg(&udev->dev, + "USB offload: %uch %uHz %s stays with the host, not offloaded\n", + params_channels(params), params_rate(params), + direction == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture"); + return -EOPNOTSUPP; + } + if (!subs) { + dev_dbg(&udev->dev, "USB offload: no %s format for %s %uch %uHz\n", + direction == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture", + snd_pcm_format_name(params_format(params)), + params_channels(params), params_rate(params)); + return -EOPNOTSUPP; + } + + /* a PCM is offloaded once; another PCM of the device may well be running */ + if (usb_offload_ua_find_stream(dev, direction, as->pcm->device)) + return -EBUSY; + + stream = kzalloc_obj(*stream); + if (!stream) + return -ENOMEM; + stream->subs = subs; + stream->direction = direction; + stream->pcm_index = as->pcm->device; + + ret = usb_offload_ua_start_stream(dev, subs, params); + if (ret) + goto err_free; + + host_ep = usb_pipe_endpoint(udev, subs->data_endpoint->pipe); + if (!host_ep) { + ret = -ENXIO; + goto err_stop; + } + + /* the endpoint exists only now, so the resource is reserved here */ + ret = usb_offload_ua_alloc_resource(dev, host_ep, direction, false); + if (ret < 0) + goto err_stop; + res_id = ret; + + ret = xhci_sideband_add_endpoint(dev->sb, host_ep); + if (ret) + goto err_free_data; + + /* + * Implicit feedback reuses another interface's capture data endpoint + * as a sync source; it carries audio samples, not a feedback value + * the DSP can interpret, so only an explicit sync endpoint is claimed. + */ + if (usb_offload_ua_explicit_fb(subs)) { + fb_host_ep = usb_pipe_endpoint(udev, subs->sync_endpoint->pipe); + if (!fb_host_ep) { + ret = -ENXIO; + goto err_remove_data; + } + + ret = usb_offload_ua_alloc_resource(dev, fb_host_ep, direction, true); + if (ret < 0) + goto err_remove_data; + fb_res_id = ret; + + ret = xhci_sideband_add_endpoint(dev->sb, fb_host_ep); + if (ret) + goto err_free_feedback; + } else { + fb_host_ep = NULL; + } + + ret = xhci_sideband_get_ep_properties(dev->sb, host_ep, &ep_prop); + if (ret) + goto err_remove_feedback; + + if (fb_host_ep) { + ret = xhci_sideband_get_ep_properties(dev->sb, fb_host_ep, + &fb_ep_prop); + if (ret) + goto err_remove_feedback; + } + + memset(hw, 0, sizeof(*hw)); + hw->pcm_index = stream->pcm_index; + hw->slot_id = ep_prop.slot_id; + hw->bus_devid = PCI_DEVID(ep_prop.bus, + PCI_DEVFN(ep_prop.device, ep_prop.function)); + usb_offload_fill_ep(&hw->data, host_ep); + hw->data.resource = ep_prop.res_id; + stream->resource = res_id; + stream->fb_resource = fb_res_id; + stream->data_host_ep = host_ep; + stream->feedback_host_ep = fb_host_ep; + if (fb_host_ep) { + usb_offload_fill_ep(&hw->feedback, fb_host_ep); + hw->feedback.resource = fb_ep_prop.res_id; + /* + * The period the feedback value is refreshed at, the one + * snd-usb-audio polls its sync endpoint at: bRefresh of a UAC1 + * endpoint, otherwise 2 frames at full speed and bInterval at + * high speed, 1 ms when that is not usable either. It is kept + * as log2 in syncinterval, here in bInterval form. + */ + hw->feedback.interval = subs->sync_endpoint->syncinterval + 1; + hw->has_feedback = true; + } + hw->split = ep_prop.tt; + /* + * The root port's speed is what the link runs at, also for a full + * speed device behind a high speed hub; the service interval is in + * the device's own frame units, so that one goes by udev->speed. + */ + hw->high_speed = ep_prop.port_speed == USB_OFFLOAD_PORT_SPEED_HIGH; + + /* + * An isochronous bInterval is an exponent at both speeds, in frames + * (1 ms) at full speed and in microframes (125 us) at high speed, which + * is how the xHC schedules the endpoint (xhci_parse_exponent_interval). + */ + interval = clamp_t(u8, hw->data.interval, 1, 16); + hw->service_interval_us = (udev->speed == USB_SPEED_HIGH ? 125 : 1000) << + (interval - 1); + + list_add_tail(&stream->node, &dev->streams); + + dev_dbg(&udev->dev, "USB offload: acquired pcm=%d ep=%#x\n", + hw->pcm_index, hw->data.ep_addr); + + return 0; + +err_remove_feedback: + if (fb_host_ep) + xhci_sideband_remove_endpoint(dev->sb, fb_host_ep); +err_free_feedback: + if (fb_host_ep) + xhci_sideband_free_resource_id(dev->sb, fb_res_id); +err_remove_data: + xhci_sideband_remove_endpoint(dev->sb, host_ep); +err_free_data: + xhci_sideband_free_resource_id(dev->sb, res_id); +err_stop: + usb_offload_ua_stop_stream(dev, subs); +err_free: + kfree(stream); + return ret; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_acquire_stream, "USB_AUDIO_OFFLOAD"); + +int usb_offload_release_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + struct usb_offload_dev *dev; + + if (!ctx || !udev) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return -ENODEV; + + dev = usb_offload_ua_find_udev(udev); + if (!dev) + return -ENODEV; + + stream = usb_offload_ua_find_stream(dev, direction, pcm_index); + if (!stream) + return -ENODEV; + + usb_offload_ua_release(dev, stream); + + return 0; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_release_stream, "USB_AUDIO_OFFLOAD"); + +/* + * The acquired stream of @udev, named by its direction and USB PCM, NULL if + * there is none. Caller holds usb_offload_ua_lock. + */ +static struct usb_offload_ua_stream * +usb_offload_ua_get_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, + int direction, int pcm_index, struct usb_offload_dev **dev) +{ + if (usb_offload_ua_ctx != ctx) + return NULL; + + *dev = usb_offload_ua_find_udev(udev); + if (!*dev) + return NULL; + + return usb_offload_ua_find_stream(*dev, direction, pcm_index); +} + +/* + * Stop the offloaded endpoints on the xHCI once the DSP has stopped its side + * of the link, before the link goes down: an isochronous IN endpoint that the + * controller still services then has nowhere to deliver its packets, which + * can stall the controller's periodic schedule. The feedback endpoint is the + * IN one on a playback stream, so it goes first. The resources stay claimed + * until usb_offload_release_stream(). + */ +int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + struct usb_offload_dev *dev; + int ret; + + if (!ctx || !udev) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + stream = usb_offload_ua_get_stream(ctx, udev, direction, pcm_index, &dev); + if (!stream) + return -ENODEV; + + if (stream->feedback_host_ep) { + ret = xhci_sideband_stop_endpoint(dev->sb, stream->feedback_host_ep); + if (ret) + dev_dbg(&udev->dev, "USB offload: stop feedback ep: %d\n", ret); + } + + if (stream->data_host_ep) { + ret = xhci_sideband_stop_endpoint(dev->sb, stream->data_host_ep); + if (ret) + dev_dbg(&udev->dev, "USB offload: stop ep: %d\n", ret); + } + + return 0; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_quiesce_stream, "USB_AUDIO_OFFLOAD"); + +/* + * Restart the endpoints usb_offload_quiesce_stream() stopped, for a stream + * which is started again without having been released in between, as when + * it recovers from an xrun. A stopped endpoint stays stopped until it is + * restarted, the DSP starting its side of the link does not do it. The + * feedback endpoint, which the quiesce stops first, comes back last. + */ +int usb_offload_restart_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + struct usb_offload_dev *dev; + int ret; + + if (!ctx || !udev) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + stream = usb_offload_ua_get_stream(ctx, udev, direction, pcm_index, &dev); + if (!stream) + return -ENODEV; + + if (stream->data_host_ep) { + ret = xhci_sideband_start_endpoint(dev->sb, stream->data_host_ep); + if (ret) { + dev_dbg(&udev->dev, "USB offload: start ep: %d\n", ret); + return ret; + } + } + + if (stream->feedback_host_ep) { + ret = xhci_sideband_start_endpoint(dev->sb, stream->feedback_host_ep); + if (ret) { + dev_dbg(&udev->dev, "USB offload: start feedback ep: %d\n", ret); + return ret; + } + } + + return 0; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_restart_stream, "USB_AUDIO_OFFLOAD"); + +static u64 usb_offload_add_s24_le(struct usb_offload_ctx *ctx, u64 formats) +{ + if (ctx->s24_3le_as_s24_le && (formats & SNDRV_PCM_FMTBIT_S24_3LE)) + formats |= SNDRV_PCM_FMTBIT_S24_LE; + + return formats; +} + +static bool usb_offload_fmt_in_range(const struct audioformat *fp, + struct snd_pcm_hw_params *params) +{ + struct snd_interval *c = hw_param_interval(params, + SNDRV_PCM_HW_PARAM_CHANNELS); + struct snd_interval *r = hw_param_interval(params, + SNDRV_PCM_HW_PARAM_RATE); + + if (fp->channels < c->min || fp->channels > c->max) + return false; + + if (fp->rate_min > r->max || + (fp->rate_min == r->max && r->openmax)) + return false; + if (fp->rate_max < r->min || + (fp->rate_max == r->min && r->openmin)) + return false; + + return true; +} + +static bool usb_offload_fmt_usable(struct usb_offload_ctx *ctx, + const struct audioformat *fp, + struct snd_pcm_hw_params *params) +{ + struct snd_mask fmts; + u64 formats = usb_offload_add_s24_le(ctx, fp->formats); + + snd_mask_none(&fmts); + fmts.bits[0] = lower_32_bits(formats); + fmts.bits[1] = upper_32_bits(formats); + snd_mask_intersect(&fmts, + hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + if (snd_mask_empty(&fmts)) + return false; + + return usb_offload_fmt_in_range(fp, params); +} + +static void usb_offload_fmt_rates(const struct audioformat *fp, + struct snd_pcm_hw_params *params, + unsigned int *rmin, unsigned int *rmax) +{ + struct snd_interval *r = hw_param_interval(params, + SNDRV_PCM_HW_PARAM_RATE); + unsigned int i; + + if (!fp->rate_table || !fp->nr_rates) { + *rmin = min(*rmin, fp->rate_min); + *rmax = max(*rmax, fp->rate_max); + return; + } + + for (i = 0; i < fp->nr_rates; i++) { + if (!snd_interval_test(r, fp->rate_table[i])) + continue; + + *rmin = min(*rmin, fp->rate_table[i]); + *rmax = max(*rmax, fp->rate_table[i]); + } +} + +int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index, int var, + struct snd_pcm_hw_params *params) +{ + unsigned int rmin = UINT_MAX, rmax = 0; + struct snd_usb_substream *subs; + struct usb_offload_dev *dev; + const struct audioformat *fp; + struct snd_usb_stream *as; + struct snd_interval t; + struct snd_mask m; + u64 formats = 0; + + if (!ctx || !udev || !params) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + if (var != SNDRV_PCM_HW_PARAM_RATE && + var != SNDRV_PCM_HW_PARAM_CHANNELS && + var != SNDRV_PCM_HW_PARAM_FORMAT) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return 0; + + dev = usb_offload_ua_find_udev(udev); + if (!dev || !dev->connected) + return 0; + + list_for_each_entry(as, &dev->chip->pcm_list, list) { + if (pcm_index >= 0 && + (!as->pcm || as->pcm->device != pcm_index)) + continue; + + subs = &as->substream[direction]; + if (!subs->ep_num) + continue; + + list_for_each_entry(fp, &subs->fmt_list, list) { + if (!usb_offload_fmt_offloadable(ctx, dev->chip, fp, direction)) + continue; + + /* + * Resolving FORMAT is what decides whether a format + * is usable in the first place - e.g. an alternate + * setting whose only format is S24_3LE must still be + * found here so that it can stand in for S24_LE + * below, so this is checked on rate and channels + * alone, unlike the other two parameters. + */ + if (var == SNDRV_PCM_HW_PARAM_FORMAT) { + if (usb_offload_fmt_in_range(fp, params)) + formats |= fp->formats; + continue; + } + + if (!usb_offload_fmt_usable(ctx, fp, params)) + continue; + + switch (var) { + case SNDRV_PCM_HW_PARAM_RATE: + usb_offload_fmt_rates(fp, params, &rmin, &rmax); + break; + case SNDRV_PCM_HW_PARAM_CHANNELS: + rmin = min(rmin, fp->channels); + rmax = max(rmax, fp->channels); + break; + } + } + } + + if (var == SNDRV_PCM_HW_PARAM_FORMAT) { + formats = usb_offload_add_s24_le(ctx, formats); + + snd_mask_none(&m); + m.bits[0] = lower_32_bits(formats); + m.bits[1] = upper_32_bits(formats); + + return snd_mask_refine(hw_param_mask(params, var), &m); + } + + if (rmin > rmax) { + hw_param_interval(params, var)->empty = 1; + return -EINVAL; + } + + t = (struct snd_interval){ .min = rmin, .max = rmax, .integer = 1 }; + + return snd_interval_refine(hw_param_interval(params, var), &t); +} +EXPORT_SYMBOL_NS_GPL(usb_offload_refine_hw_params, "USB_AUDIO_OFFLOAD"); + +/** + * usb_offload_resolve_format - settle on the container the device actually has + * @ctx: offload context the caller owns + * @udev: USB device the stream would run on + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * @pcm_index: USB SND PCM device index to check against, or negative to + * consider every PCM stream of the device + * @params: the parameters to resolve, as chosen by the S24_LE-only + * negotiation on the frontend + * + * A no-op unless the host set s24_3le_as_s24_le in its usb_offload_res, for a + * host whose pipeline only has a 24-bit representation as S24_LE (24 valid + * bits in a 4-byte container). Devices, however, commonly only pack 24-bit + * samples into 3 bytes (S24_3LE) - by the time a stream gets here the two + * have already been treated as interchangeable to let negotiation succeed + * at all, so it still has to be decided which container the device is + * actually going to be asked for. If no stream can serve S24_LE but one can + * serve S24_3LE for the same rate and channels, @params is rewritten to + * S24_3LE so that this reaches usb_offload_acquire_stream() in a container + * the device can accept. + * + * Formats other than S24_LE are left untouched. + * + * Return: 1 if @params was rewritten, 0 if it was left alone (including when + * s24_3le_as_s24_le is not set, or when nothing usable was found - + * usb_offload_acquire_stream() reports that), or + * a negative error code for invalid arguments. + */ +int usb_offload_resolve_format(struct usb_offload_ctx *ctx, struct usb_device *udev, + int direction, int pcm_index, + struct snd_pcm_hw_params *params) +{ + struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); + struct snd_usb_substream *subs; + struct usb_offload_dev *dev; + const struct audioformat *fp; + struct snd_usb_stream *as; + bool have_3le = false; + + if (!ctx || !udev || !params) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + if (!ctx->s24_3le_as_s24_le || !snd_mask_test_format(fmt, SNDRV_PCM_FORMAT_S24_LE)) + return 0; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return 0; + + dev = usb_offload_ua_find_udev(udev); + if (!dev || !dev->connected) + return 0; + + list_for_each_entry(as, &dev->chip->pcm_list, list) { + if (pcm_index >= 0 && (!as->pcm || as->pcm->device != pcm_index)) + continue; + + subs = &as->substream[direction]; + if (!subs->ep_num) + continue; + + list_for_each_entry(fp, &subs->fmt_list, list) { + if (!usb_offload_fmt_offloadable(ctx, dev->chip, fp, direction) || + !usb_offload_fmt_in_range(fp, params)) + continue; + + if (fp->formats & SNDRV_PCM_FMTBIT_S24_LE) + return 0; + + if (fp->formats & SNDRV_PCM_FMTBIT_S24_3LE) + have_3le = true; + } + } + + if (!have_3le) + return 0; + + snd_mask_none(fmt); + snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_3LE); + return 1; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_resolve_format, "USB_AUDIO_OFFLOAD"); + +/* Tear down one record. Caller holds usb_offload_ua_lock. */ +static void usb_offload_ua_teardown(struct usb_offload_dev *dev) +{ + struct usb_offload_ctx *ctx = usb_offload_ua_ctx; + struct usb_offload_ua_stream *stream, *tmp; + + /* + * Runs before the chip is marked shut down, so a stream still held + * over a physical unplug can be given back to the host properly. + */ + list_for_each_entry_safe(stream, tmp, &dev->streams, node) + usb_offload_ua_release(dev, stream); + + if (dev->connected && ctx) { + dev->connected = false; + mutex_unlock(&usb_offload_ua_lock); + usb_offload_disconnect(ctx, dev->udev); + mutex_lock(&usb_offload_ua_lock); + } + xhci_sideband_unregister(dev->sb); + list_del(&dev->node); + kfree(dev); +} + +/* + * Build the USB-SND device identity from the usb-audio chip. This is the + * only place the chip's card / PCM topology is read; the result travels + * to the host (via usb_offload_connect) so that it can construct a + * struct snd_soc_usb_device for the ASoC USB framework without the + * library ever including . + * + * @log: report PCMs left out of the offload list; the caller passes true + * only on the first connect of a device, as this runs on every re-notify. + */ +static void usb_offload_ua_build_id(const struct usb_offload_ctx *ctx, + struct snd_usb_audio *chip, + struct usb_offload_usb_dev_info *id, bool log) +{ + struct snd_usb_substream *subs; + struct snd_usb_stream *as; + const char *product; + const char *pcm_name; + int index; + + memset(id, 0, sizeof(*id)); + id->card_idx = chip->card ? chip->card->number : -1; + id->chip_idx = chip->index; + product = chip->dev->product ?: "USB Audio"; + + list_for_each_entry(as, &chip->pcm_list, list) { + if (!as->pcm) + continue; + pcm_name = as->pcm->name[0] ? as->pcm->name : "USB PCM"; + + subs = &as->substream[SNDRV_PCM_STREAM_PLAYBACK]; + if (subs->num_formats && !usb_offload_subs_offloadable(ctx, subs)) { + if (log) + dev_dbg(&chip->dev->dev, + "USB offload: PCM %u playback needs clock feedback, not offloaded\n", + as->pcm->device); + } else if (subs->num_formats && + id->num_playback < USB_OFFLOAD_MAX_PCM) { + index = id->num_playback++; + id->ppcm_idx[index] = as->pcm->device; + snprintf(id->ppcm_names[index], USB_OFFLOAD_PCM_NAME_LEN, + "%d,%u | %.24s - %.16s", id->card_idx, + as->pcm->device, product, pcm_name); + } + + subs = &as->substream[SNDRV_PCM_STREAM_CAPTURE]; + if (subs->num_formats && !usb_offload_subs_offloadable(ctx, subs)) { + if (log) + dev_dbg(&chip->dev->dev, + "USB offload: PCM %u capture is tied to the playback, not offloaded\n", + as->pcm->device); + } else if (subs->num_formats && + id->num_capture < USB_OFFLOAD_MAX_PCM) { + index = id->num_capture++; + id->cpcm_idx[index] = as->pcm->device; + snprintf(id->cpcm_names[index], USB_OFFLOAD_PCM_NAME_LEN, + "%d,%u | %.24s - %.16s", id->card_idx, + as->pcm->device, product, pcm_name); + } + } +} + +static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) +{ + struct usb_offload_usb_dev_info *id __free(kfree) = NULL; + struct usb_offload_ctx *ctx; + struct usb_offload_candidate cand; + struct usb_offload_dev *dev; + struct usb_interface *audctrl; + struct usb_device *udev; + int ret; + + if (!chip || !chip->dev) + return; + udev = chip->dev; + + ret = usb_offload_try_offload(udev, &cand); + if (ret) { + dev_dbg(&udev->dev, + "USB offload: device not a candidate: %d\n", ret); + return; + } + + id = kzalloc_obj(*id); + if (!id) + return; + + mutex_lock(&usb_offload_ua_lock); + + ctx = usb_offload_ua_ctx; + if (!ctx) + goto unlock; + + /* connect_cb fires per interface; offload the chip only once. */ + dev = usb_offload_ua_find(chip); + if (dev) { + bool connected = dev->connected; + + mutex_unlock(&usb_offload_ua_lock); + + if (connected) { + usb_offload_ua_build_id(ctx, chip, id, false); + ret = usb_offload_connect(ctx, udev, id); + if (ret) + dev_dbg(&udev->dev, + "USB offload: re-notify failed: %d\n", + ret); + } + return; + } + + /* a device whose PCMs all stay with the host has nothing to offload */ + usb_offload_ua_build_id(ctx, chip, id, true); + if (!id->num_playback && !id->num_capture) { + dev_dbg(&udev->dev, "USB offload: no PCM can be offloaded\n"); + goto unlock; + } + + dev = kzalloc_obj(*dev); + if (!dev) + goto unlock; + + dev->chip = chip; + dev->udev = udev; + dev->cand = cand; + INIT_LIST_HEAD(&dev->streams); + audctrl = usb_ifnum_to_if(udev, cand.ac_ifnum); + if (!audctrl) { + ret = -ENODEV; + goto free_dev_unlock; + } + + dev->sb = xhci_sideband_register(audctrl, XHCI_SIDEBAND_AUDIO, NULL); + if (!dev->sb) { + ret = -EOPNOTSUPP; + goto free_dev_unlock; + } + + list_add_tail(&dev->node, &usb_offload_ua_devs); + mutex_unlock(&usb_offload_ua_lock); + + /* Inform the DSP and the ASoC USB framework. */ + ret = usb_offload_connect(ctx, udev, id); + + mutex_lock(&usb_offload_ua_lock); + if (ret) { + list_del(&dev->node); + goto unregister_sb; + } + dev->connected = true; + mutex_unlock(&usb_offload_ua_lock); + + dev_dbg(&udev->dev, + "USB offload: device offloaded (UAC protocol %#x, %u endpoint(s))\n", + dev->cand.uac_version, dev->cand.num_eps); + return; + +unregister_sb: + xhci_sideband_unregister(dev->sb); +free_dev_unlock: + dev_dbg(&udev->dev, "USB offload: sideband setup failed: %d\n", ret); + kfree(dev); +unlock: + mutex_unlock(&usb_offload_ua_lock); +} + +static void usb_offload_ua_disconnect_cb(struct snd_usb_audio *chip) +{ + struct usb_offload_dev *dev; + + if (!chip) + return; + + guard(mutex)(&usb_offload_ua_lock); + + dev = usb_offload_ua_find(chip); + if (dev) { + dev_dbg(&dev->udev->dev, "USB offload: device removed\n"); + usb_offload_ua_teardown(dev); + } +} + +static struct snd_usb_platform_ops usb_offload_ua_ops = { + .connect_cb = usb_offload_ua_connect_cb, + .disconnect_cb = usb_offload_ua_disconnect_cb, +}; + +int usb_offload_usb_audio_init(struct usb_offload_ctx *ctx) +{ + int ret; + + if (!ctx) + return -EINVAL; + + scoped_guard(mutex, &usb_offload_ua_lock) { + if (usb_offload_ua_ctx) + return -EEXIST; + usb_offload_ua_ctx = ctx; + } + + /* + * register/unregister take the usb-audio register_mutex; do not hold + * usb_offload_ua_lock across them to avoid an A-B / B-A inversion with + * the connect/disconnect callbacks (which run under register_mutex and + * then take usb_offload_ua_lock). + */ + ret = snd_usb_register_platform_ops(&usb_offload_ua_ops); + if (ret) { + scoped_guard(mutex, &usb_offload_ua_lock) + usb_offload_ua_ctx = NULL; + return ret; + } + + dev_dbg(ctx->dev, "USB offload: usb-audio bridge registered\n"); + return 0; +} + +void usb_offload_usb_audio_exit(struct usb_offload_ctx *ctx) +{ + struct usb_offload_dev *dev, *tmp; + + scoped_guard(mutex, &usb_offload_ua_lock) { + if (usb_offload_ua_ctx != ctx) + return; + } + + /* + * Let go of every device while snd-usb-audio still reports their + * disconnect, and stop the callbacks. snd_usb_release_platform_ops() + * runs our disconnect callback for each device and unregisters under + * the usb-audio register_mutex, so a device cannot be freed in between + * unnoticed, and once it returns no callback is running and none can + * start. + */ + snd_usb_release_platform_ops(); + + scoped_guard(mutex, &usb_offload_ua_lock) { + /* every record is of a connected chip, there should be none left */ + list_for_each_entry_safe(dev, tmp, &usb_offload_ua_devs, node) + usb_offload_ua_teardown(dev); + usb_offload_ua_ctx = NULL; + } + + dev_dbg(ctx->dev, "USB offload: usb-audio bridge unregistered\n"); +} + +void usb_offload_usb_audio_rediscover(void) +{ + /* + * Walk the usb-audio core's connected-chip array and re-run our + * connect_cb for each. snd_usb_rediscover_devices() does this under + * the usb-audio register_mutex; our callback only re-notifies for + * chips already offloaded rather than setting them up again, and is + * a no-op if the platform ops were never registered, so this is + * always safe. + */ + snd_usb_rediscover_devices(); +} diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index c2f7b08607ac86..f7f6564e81d5d8 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -485,6 +485,7 @@ int snd_usb_hw_params(struct snd_usb_substream *subs, struct snd_usb_audio *chip = subs->stream->chip; const struct audioformat *fmt; const struct audioformat *sync_fmt; + struct snd_usb_endpoint *ep; bool fixed_rate, sync_fixed_rate; int ret; @@ -540,21 +541,21 @@ int snd_usb_hw_params(struct snd_usb_substream *subs, close_endpoints(chip, subs); } - subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate); - if (!subs->data_endpoint) { - ret = -EINVAL; + ep = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate); + if (IS_ERR(ep)) { + ret = PTR_ERR(ep); goto unlock; } + subs->data_endpoint = ep; if (fmt->sync_ep) { - subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt, - hw_params, - fmt == sync_fmt, - sync_fixed_rate); - if (!subs->sync_endpoint) { - ret = -EINVAL; + ep = snd_usb_endpoint_open(chip, sync_fmt, hw_params, + fmt == sync_fmt, sync_fixed_rate); + if (IS_ERR(ep)) { + ret = PTR_ERR(ep); goto unlock; } + subs->sync_endpoint = ep; snd_usb_endpoint_set_sync(chip, subs->data_endpoint, subs->sync_endpoint); @@ -1311,6 +1312,43 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream) return 0; } +/** + * snd_usb_offload_claim_substream - take a substream over for offloaded use + * @subs: the substream to claim + * + * Claims @subs on behalf of a DSP that drives the endpoints itself. It takes + * the same reference snd_usb_pcm_open() takes, so the two exclude each other: + * a claimed substream cannot be opened from user space, and a substream that + * user space already opened cannot be claimed. + * + * Return: 0 on success, -EBUSY if the substream is already in use. + */ +int snd_usb_offload_claim_substream(struct snd_usb_substream *subs) +{ + struct snd_usb_audio *chip = subs->stream->chip; + + guard(mutex)(&chip->mutex); + if (subs->opened) + return -EBUSY; + subs->opened = 1; + + return 0; +} +EXPORT_SYMBOL_GPL(snd_usb_offload_claim_substream); + +/** + * snd_usb_offload_release_substream - give a claimed substream back + * @subs: the substream to release + */ +void snd_usb_offload_release_substream(struct snd_usb_substream *subs) +{ + struct snd_usb_audio *chip = subs->stream->chip; + + guard(mutex)(&chip->mutex); + subs->opened = 0; +} +EXPORT_SYMBOL_GPL(snd_usb_offload_release_substream); + /* Since a URB can handle only a single linear buffer, we must use double * buffering when the data to be transferred overflows the buffer boundary. * To avoid inconsistencies when updating hwptr_done, we use double buffering diff --git a/sound/usb/pcm.h b/sound/usb/pcm.h index c096021adb2b11..900202b6ab2dde 100644 --- a/sound/usb/pcm.h +++ b/sound/usb/pcm.h @@ -26,4 +26,7 @@ snd_usb_find_substream_format(struct snd_usb_substream *subs, int snd_usb_hw_params(struct snd_usb_substream *subs, struct snd_pcm_hw_params *hw_params); int snd_usb_hw_free(struct snd_usb_substream *subs); + +int snd_usb_offload_claim_substream(struct snd_usb_substream *subs); +void snd_usb_offload_release_substream(struct snd_usb_substream *subs); #endif /* __USBAUDIO_PCM_H */ diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c index 4ec1f2a0b359ad..05848260226b8b 100644 --- a/sound/usb/quirks.c +++ b/sound/usb/quirks.c @@ -2137,6 +2137,7 @@ void snd_usb_set_format_quirk(struct snd_usb_substream *subs, break; } } +EXPORT_SYMBOL_GPL(snd_usb_set_format_quirk); int snd_usb_select_mode_quirk(struct snd_usb_audio *chip, const struct audioformat *fmt)