This project is mirrored from Pull mirroring updated .
  1. 23 Nov, 2020 4 commits
    • Takashi Iwai's avatar
      ALSA: usb-audio: Refactor endpoint management · bf6313a0
      Takashi Iwai authored
      This is an intensive surgery for the endpoint and stream management
      for achieving more robust and clean code.
      The goals of this patch are:
      - More clear endpoint resource changes
      - The interface altsetting control in a single place
      Below are brief description of the whole changes.
      First off, most of the endpoint operations are moved into endpoint.c,
      so that the snd_usb_endpoint object is only referred in other places.
      The endpoint object is acquired and released via the new functions
      snd_usb_endpoint_open() and snd_usb_endpoint_close() that are called
      at PCM hw_params and hw_free callbacks, respectively.  Those are
      ref-counted and EPs can manage the multiple opens.
      The open callback receives the audioformat and hw_params arguments,
      and those are used for initializing the EP parameters; especially the
      endpoint, interface and altset numbers are read from there, as well as
      the PCM parameters like the format, rate and channels.  Those are
      stored in snd_usb_endpoint object.  If it's the secondary open, the
      function checks whether the given parameters are compatible with the
      already opened EP setup, too.
      The coupling with a sync EP (including an implicit feedback sync) is
      done by the sole snd_usb_endpoint_set_sync() call.
      The configuration of each endpoint is done in a single shot via
      snd_usb_endpoint_configure() call.  This is the place where most of
      PCM configurations are done.  A few flags and special handling in the
      snd_usb_substream are dropped along with this change.
      A significant difference wrt the configuration from the previous code
      is the order of USB host interface setups.  Now the interface is
      always disabled at beginning and (re-)enabled at the last step of
      snd_usb_endpoint_configure(), in order to be compliant with the
      standard UAC2/3.  For UAC1, the interface is set before the parameter
      setups since there seem devices that require it (e.g. Yamaha THR10),
      just like how it was done in the previous driver code.
      The start/stop are almost same as before, also single-shots.  The URB
      callbacks need to be set via snd_usb_endpoint_set_callback() like the
      previous code at the trigger phase, too.
      Finally, the flag for the re-setup is set at the device suspend
      through the full EP list, instead of PCM trigger.  This catches the
      overlooked cases where the PCM hasn't been running yet but the device
      needs the full setup after resume.
      Tested-by: default avatarKeith Milner <>
      Tested-by: default avatarDylan Robinson <>
      Signed-off-by: default avatarTakashi Iwai <>
    • Takashi Iwai's avatar
      ALSA: usb-audio: Set callbacks via snd_usb_endpoint_set_callback() · 96e221f3
      Takashi Iwai authored
      The prepare_data_urb and retire_data_urb fields of the endpoint object
      are set dynamically at PCM trigger start/stop.  Those are evaluated in
      the endpoint handler, but there can be a race, especially if two
      different PCM substreams are handling the same endpoint for the
      implicit feedback case.  Also, the data_subs field of the endpoint is
      set and accessed dynamically, too, which has the same risk.
      As a slight improvement for the concurrency, this patch introduces the
      function to set the callbacks and the data in a shot with the memory
      barrier.  In the reader side, it's also fetched with the memory
      There is still a room of race if prepare and retire callbacks are set
      during executing the URB completion.  But such an inconsistency may
      happen only for the implicit fb source, i.e. it's only about the
      capture stream.  And luckily, the capture stream never sets the
      prepare callback, hence the problem doesn't happen practically.
      Tested-by: default avatarKeith Milner <>
      Tested-by: default avatarDylan Robinson <>
      Signed-off-by: default avatarTakashi Iwai <>
    • Takashi Iwai's avatar
      ALSA: usb-audio: Create endpoint objects at parsing phase · 54cb3190
      Takashi Iwai authored
      Currently snd_usb_endpoint objects are created at first when the
      substream is opened and tries to assign the endpoints corresponding to
      the matching audioformat.  But since basically the all endpoints have
      been already parsed and the information have been obtained, we may
      create the endpoint objects statically at the init phase.  It's easier
      to manage for the implicit fb case, for example.
      This patch changes the endpoint object management and lets the parser
      to create the all endpoint objects.
      This change shouldn't bring any functional changes.
      Tested-by: default avatarKeith Milner <>
      Tested-by: default avatarDylan Robinson <>
      Signed-off-by: default avatarTakashi Iwai <>
    • Takashi Iwai's avatar
      ALSA: usb-audio: Add snd_usb_get_endpoint() helper · c7474d09
      Takashi Iwai authored
      Factor out the code to obtain snd_usb_endpoint object matching with
      the given endpoint.  It'll be used in the later patch to add the
      implicit feedback hw-constraint.
      No functional change by this patch itself.
      Tested-by: default avatarKeith Milner <>
      Tested-by: default avatarDylan Robinson <>
      Signed-off-by: default avatarTakashi Iwai <>
  2. 24 Apr, 2020 1 commit
    • Alexander Tsoy's avatar
      ALSA: usb-audio: Improve frames size computation · f0bd62b6
      Alexander Tsoy authored
      For computation of the the next frame size current value of fs/fps and
      accumulated fractional parts of fs/fps are used, where values are stored
      in Q16.16 format. This is quite natural for computing frame size for
      asynchronous endpoints driven by explicit feedback, since in this case
      fs/fps is a value provided by the feedback endpoint and it's already in
      the Q format. If an error is accumulated over time, the device can
      adjust fs/fps value to prevent buffer overruns/underruns.
      But for synchronous endpoints the accuracy provided by these computations
      is not enough. Due to accumulated error the driver periodically produces
      frames with incorrect size (+/- 1 audio sample).
      This patch fixes this issue by implementing a different algorithm for
      frame size computation. It is based on accumulating of the remainders
      from division fs/fps and it doesn't accumulate errors over time. This
      new method is enabled for synchronous and adaptive playback endpoints.
      Signed-off-by: default avatarAlexander Tsoy <>
      Signed-off-by: default avatarTakashi Iwai <>
  3. 02 Nov, 2017 1 commit
    • Greg Kroah-Hartman's avatar
      License cleanup: add SPDX GPL-2.0 license identifier to files with no license · b2441318
      Greg Kroah-Hartman authored
      Many source files in the tree are missing licensing information, which
      makes it harder for compliance tools to determine the correct license.
      By default all files without license information are under the default
      license of the kernel, which is GPL version 2.
      Update the files which contain no license information with the 'GPL-2.0'
      SPDX license identifier.  The SPDX identifier is a legally binding
      shorthand, which can be used instead of the full boiler plate text.
      This patch is based on work done by Thomas Gleixner and Kate Stewart and
      Philippe Ombredanne.
      How this work was done:
      Patches were generated and checked against linux-4.14-rc6 for a subset of
      the use cases:
       - file had no licensing information it it.
       - file was a */uapi/* one with no licensing information in it,
       - file was a */uapi/* one with existing licensing information,
      Further patches will be generated in subsequent months to fix up cases
      where non-standard license headers were used, and references to license
      had to be inferred by heuristics based on keywords.
      The analysis to determine which SPDX License Identifier to be applied to
      a file was done in a spreadsheet of side by side results from of the
      output of two independent scanners (ScanCode & Windriver) producing SPDX
      tag:value files created by Philippe Ombredanne.  Philippe prepared the
      base worksheet, and did an initial spot review of a few 1000 files.
      The 4.13 kernel was the starting point of the analysis with 60,537 files
      assessed.  Kate Stewart did a file by file comparison of the scanner
      results in the spreadsheet to determine which SPDX license identifier(s)
      to be applied to the file. She confirmed any determination that was not
      immediately clear with lawyers working with the Linux Foundation.
      Criteria used to select files for SPDX license identifier tagging was:
       - Files considered eligible had to be source code files.
       - Make and config files were included as candidates if they contained >5
         lines of source
       - File already had some variant of a license header in it (even if <5
      All documentation files were explicitly excluded.
      The following heuristics were used to determine which SPDX license
      identifiers to apply.
       - when both scanners couldn't find any license traces, file was
         considered to have no license information in it, and the top level
         COPYING file license applied.
         For non */uapi/* files that summary was:
         SPDX license identifier                            # files
         GPL-2.0                                              11139
         and resulted in the first patch in this series.
         If that file was a */uapi/* path one, it was "GPL-2.0 WITH
         Linux-syscall-note" otherwise it was "GPL-2.0".  Results of that was:
         SPDX license identifier                            # files
         GPL-2.0 WITH Linux-syscall-note                        930
         and resulted in the second patch in this series.
       - if a file had some form of licensing information in it, and was one
         of the */uapi/* ones, it was denoted with the Linux-syscall-note if
         any GPL family license was found in the file or had no licensing in
         it (per prior point).  Results summary:
         SPDX license identifier                            # files
         GPL-2.0 WITH Linux-syscall-note                       270
         GPL-2.0+ WITH Linux-syscall-note                      169
         ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause)    21
         ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)    17
         LGPL-2.1+ WITH Linux-syscall-note                      15
         GPL-1.0+ WITH Linux-syscall-note                       14
         ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause)    5
         LGPL-2.0+ WITH Linux-syscall-note                       4
         LGPL-2.1 WITH Linux-syscall-note                        3
         ((GPL-2.0 WITH Linux-syscall-note) OR MIT)              3
         ((GPL-2.0 WITH Linux-syscall-note) AND MIT)             1
         and that resulted in the third patch in this series.
       - when the two scanners agreed on the detected license(s), that became
         the concluded license(s).
       - when there was disagreement between the two scanners (one detected a
         license but the other didn't, or they both detected different
         licenses) a manual inspection of the file occurred.
       - In most cases a manual inspection of the information in the file
         resulted in a clear resolution of the license that should apply (and
         which scanner probably needed to revisit its heuristics).
       - When it was not immediately clear, the license identifier was
         confirmed with lawyers working with the Linux Foundation.
       - If there was any question as to the appropriate license identifier,
         the file was flagged for further research and to be revisited later
         in time.
      In total, over 70 hours of logged manual review was done on the
      spreadsheet to determine the SPDX license identifiers to apply to the
      source files by Kate, Philippe, Thomas and, in some cases, confirmation
      by lawyers working with the Linux Foundation.
      Kate also obtained a third independent scan of the 4.13 code base from
      FOSSology, and compared selected files where the other two scanners
      disagreed against that SPDX file, to see if there was new insights.  The
      Windriver scanner is based on an older version of FOSSology in part, so
      they are related.
      Thomas did random spot checks in about 500 files from the spreadsheets
      for the uapi headers and agreed with SPDX license identifier in the
      files he inspected. For the non-uapi files Thomas did random spot checks
      in about 15000 files.
      In initial set of patches against 4.14-rc6, 3 files were found to have
      copy/paste license identifier errors, and have been fixed to reflect the
      correct identifier.
      Additionally Philippe spent 10 hours this week doing a detailed manual
      inspection and review of the 12,461 patched files from the initial patch
      version early this week with:
       - a full scancode scan run, collecting the matched texts, detected
         license ids and scores
       - reviewing anything where there was a license detected (about 500+
         files) to ensure that the applied SPDX license was correct
       - reviewing anything where there was no detection but the patch license
         was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
         SPDX license was correct
      This produced a worksheet with 20 files needing minor correction.  This
      worksheet was then exported into 3 different .csv files for the
      different types of files to be modified.
      These .csv files were then reviewed by Greg.  Thomas wrote a script to
      parse the csv files and add the proper SPDX tag to the file, in the
      format that the file expected.  This script was further refined by Greg
      based on the output to detect more types of files automatically and to
      distinguish between header and source .c files (which need different
      comment types.)  Finally Greg ran the script using the .csv files to
      generate the patches.
      Reviewed-by: default avatarKate Stewart <>
      Reviewed-by: default avatarPhilippe Ombredanne <>
      Reviewed-by: default avatarThomas Gleixner <>
      Signed-off-by: default avatarGreg Kroah-Hartman <>
  4. 05 Jan, 2017 1 commit
    • Ioan-Adrian Ratiu's avatar
      ALSA: usb-audio: Fix irq/process data synchronization · 1d0f9530
      Ioan-Adrian Ratiu authored
      Commit 16200948 ("ALSA: usb-audio: Fix race at stopping the stream") was
      incomplete causing another more severe kernel panic, so it got reverted.
      This fixes both the original problem and its fallout kernel race/crash.
      The original fix is to move the endpoint member NULL clearing logic inside
      wait_clear_urbs() so the irq triggering the urb completion doesn't call
      retire_capture/playback_urb() after the NULL clearing and generate a panic.
      However this creates a new race between snd_usb_endpoint_start()'s call
      to wait_clear_urbs() and the irq urb completion handler which again calls
      retire_capture/playback_urb() leading to a new NULL dereference.
      We keep the EP deactivation code in snd_usb_endpoint_start() because
      removing it will break the EP reference counting (see [1] [2] for info),
      however we don't need the "can_sleep" mechanism anymore because a new
      function was introduced (snd_usb_endpoint_sync_pending_stop()) which
      synchronizes pending stops and gets called inside the pcm prepare callback.
      It also makes sense to remove can_sleep because it was also removed from
      deactivate_urbs() signature in [3] so we benefit from more simplification.
      [1] commit 015618b9 ("ALSA: snd-usb: Fix URB cancellation at stream start")
      [2] commit e9ba389c ("ALSA: usb-audio: Fix scheduling-while-atomic bug in PCM capture stream")
      [3] commit ccc1696d ("ALSA: usb-audio: simplify endpoint deactivation code")
      Fixes: f8114f85
       ("Revert "ALSA: usb-audio: Fix race at stopping the stream"")
      Signed-off-by: default avatarIoan-Adrian Ratiu <>
      Signed-off-by: default avatarTakashi Iwai <>
  5. 04 Nov, 2014 1 commit
  6. 26 Jun, 2014 1 commit
    • Takashi Iwai's avatar
      ALSA: usb-audio: Fix races at disconnection and PCM closing · 92a586bd
      Takashi Iwai authored
      When a USB-audio device is disconnected while PCM is still running, we
      still see some race: the disconnect callback calls
      snd_usb_endpoint_free() that calls release_urbs() and then kfree()
      while a PCM stream would be closed at the same time and calls
      stop_endpoints() that leads to wait_clear_urbs().  That is, the EP
      object might be deallocated while a PCM stream is syncing with
      wait_clear_urbs() with the same EP.
      Basically calling multiple wait_clear_urbs() would work fine, also
      calling wait_clear_urbs() and release_urbs() would work, too, as
      wait_clear_urbs() just reads some fields in ep.  The problem is the
      succeeding kfree() in snd_pcm_endpoint_free().
      This patch moves out the EP deallocation into the later point, the
      destructor callback.  At this stage, all PCMs must have been already
      closed, so it's safe to free the objects.
      Reported-by: default avatarAlan Stern <>
      Cc: <>
      Signed-off-by: default avatarTakashi Iwai <>
  7. 07 Oct, 2013 1 commit
  8. 26 Sep, 2013 1 commit
    • Alan Stern's avatar
      ALSA: improve buffer size computations for USB PCM audio · 976b6c06
      Alan Stern authored
      This patch changes the way URBs are allocated and their sizes are
      determined for PCM playback in the snd-usb-audio driver.  Currently
      the driver allocates too few URBs for endpoints that don't use
      implicit sync, making underruns more likely to occur.  This may be a
      holdover from before I/O delays could be measured accurately; in any
      case, it is no longer necessary.
      The patch allocates as many URBs as possible, subject to four
      	The total number of URBs for the endpoint is not allowed to
      	exceed MAX_URBS (which the patch increases from 8 to 12).
      	The total number of packets per URB is not allowed to exceed
      	MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
      	decreased from 20 to 6.
      	The total duration of queued data is not allowed to exceed
      	MAX_QUEUE, which is decreased from 24 ms to 18 ms.
      	The total number of ALSA frames in the output queue is not
      	allowed to exceed the ALSA buffer size.
      The last requirement is the hardest to implement.  Currently the
      number of URBs needed to fill a buffer cannot be determined in
      advance, because a buffer contains a fixed number of frames whereas
      the number of frames in an URB varies to match shifts in the device's
      clock rate.  To solve this problem, the patch changes the logic for
      deciding how many packets an URB should contain.  Rather than using as
      many as possible without exceeding an ALSA period boundary, now the
      driver uses only as many packets as needed to transfer a predetermined
      number of frames.  As a result, unless the device's clock has an
      exceedingly variable rate, the number of URBs making up each period
      (and hence each buffer) will remain constant.
      The overall effect of the patch is that playback works better in
      low-latency settings.  The user can still specify values for
      frames/period and periods/buffer that exceed the capabilities of the
      hardware, of course.  But for values that are within those
      capabilities, the performance will be improved.  For example, testing
      shows that a high-speed device can handle 32 frames/period and 3
      periods/buffer at 48 KHz, whereas the current driver starts to get
      glitchy at 64 frames/period and 2 periods/buffer.
      A side effect of these changes is that the "nrpacks" module parameter
      is no longer used.  The patch removes it.
      Signed-off-by: default avatarAlan Stern <>
      CC: Clemens Ladisch <>
      Tested-by: default avatarDaniel Mack <>
      Tested-by: default avatarEldad Zack <>
      Signed-off-by: default avatarTakashi Iwai <>
  9. 04 Apr, 2013 1 commit
  10. 21 Nov, 2012 2 commits
  11. 08 Nov, 2012 1 commit
  12. 19 Sep, 2012 1 commit
  13. 31 Aug, 2012 1 commit
    • Daniel Mack's avatar
      ALSA: snd-usb: fix calls to next_packet_size · 245baf98
      Daniel Mack authored
      In order to support devices with implicit feedback streaming models,
      packet sizes are now stored with each individual urb, and the PCM
      handling code which fills the buffers purely relies on the size fields
      However, calling snd_usb_audio_next_packet_size() for all possible
      packets in an URB at once, prior to letting the PCM code do its job
      does in fact not lead to the same behaviour than what the old code did:
      The PCM code will break its loop once a period boundary is reached,
      consequently using up less packets that it really could.
      As snd_usb_audio_next_packet_size() implements a feedback mechanism to
      the endpoints phase accumulator, the number of calls to that function
      matters, and when called too often, the data rate runs out of bounds.
      Fix this by making the next_packet function public, and call it from the
      PCM code as before if the packet data sizes are not defined.
      Signed-off-by: default avatarDaniel Mack <>
      Cc: [v3.5+]
      Signed-off-by: default avatarTakashi Iwai <>
  14. 30 Aug, 2012 1 commit
    • Daniel Mack's avatar
      ALSA: snd-usb: Fix URB cancellation at stream start · 015618b9
      Daniel Mack authored
      Commit e9ba389c
       ("ALSA: usb-audio: Fix scheduling-while-atomic bug in
      PCM capture stream") fixed a scheduling-while-atomic bug that happened
      when snd_usb_endpoint_start was called from the trigger callback, which
      is an atmic context. However, the patch breaks the idea of the endpoints
      reference counting, which is the reason why the driver has been
      refactored lately.
      Revert that commit and let snd_usb_endpoint_start() take care of the URB
      cancellation again. As this function is called from both atomic and
      non-atomic context, add a flag to denote whether the function may sleep.
      Signed-off-by: default avatarDaniel Mack <>
      Cc: [3.5+]
      Signed-off-by: default avatarTakashi Iwai <>
  15. 13 Apr, 2012 3 commits
    • Daniel Mack's avatar
      ALSA: snd-usb: remove old streaming logic · d399ff95
      Daniel Mack authored
      Signed-off-by: default avatarDaniel Mack <>
      Signed-off-by: default avatarTakashi Iwai <>
    • Daniel Mack's avatar
      ALSA: snd-usb: switch over to new endpoint streaming logic · edcd3633
      Daniel Mack authored
      With the previous commit that added the new streaming model, all
      endpoint and streaming related code is now in endpoint.c, and pcm.c
      only acts as a wrapper for handling the packet's payload.
      Signed-off-by: default avatarDaniel Mack <>
      Signed-off-by: default avatarTakashi Iwai <>
    • Daniel Mack's avatar
      ALSA: snd-usb: implement new endpoint streaming model · 8fdff6a3
      Daniel Mack authored
      This patch adds a new generic streaming logic for audio over USB.
      It defines a model (snd_usb_endpoint) that handles everything that
      is related to an USB endpoint and its streaming. There are functions to
      activate and deactivate an endpoint (which call usb_set_interface()),
      and to start and stop its URBs. It also has function pointers to be
      called when data was received or is about to be sent, and pointer to
      a sync slave (another snd_usb_endpoint) that is informed when data has
      been received.
      A snd_usb_endpoint knows about its state and implements a refcounting,
      so only the first user will actually start the URBs and only the last
      one to stop it will tear them down again.
      With this sort of abstraction, the actual streaming is decoupled from
      the pcm handling, which makes the "implicit feedback" mechanisms easy to
      In order to split changes properly, this patch only adds the new
      implementation but leaves the old one around, so the the driver doesn't
      change its behaviour. The switch to actually use the new code is
      submitted separately.
      Signed-off-by: default avatarDaniel Mack <>
      Signed-off-by: default avatarTakashi Iwai <>
  16. 14 Sep, 2011 2 commits
  17. 05 Mar, 2010 1 commit
    • Daniel Mack's avatar
      ALSA: usb-audio: refactor code · e5779998
      Daniel Mack authored
      Clean up the usb audio driver by factoring out a lot of functions to
      separate files. Code for procfs, quirks, urbs, format parsers etc all
      got a new home now.
      Moved almost all special quirk handling to quirks.c and introduced new
      generic functions to handle them, so the exceptions do not pollute the
      whole driver.
      Renamed usbaudio.c to card.c because this is what it actually does now.
      Renamed usbmidi.c to midi.c for namespace clarity.
      Removed more things from usbaudio.h.
      The non-standard drivers were adopted accordingly.
      Signed-off-by: default avatarDaniel Mack <>
      Cc: Clemens Ladisch <>
      Signed-off-by: default avatarTakashi Iwai <>