xref: /kernel/linux/linux-5.10/sound/usb/proc.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/init.h>
68c2ecf20Sopenharmony_ci#include <linux/usb.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <sound/core.h>
98c2ecf20Sopenharmony_ci#include <sound/info.h>
108c2ecf20Sopenharmony_ci#include <sound/pcm.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "usbaudio.h"
138c2ecf20Sopenharmony_ci#include "helper.h"
148c2ecf20Sopenharmony_ci#include "card.h"
158c2ecf20Sopenharmony_ci#include "endpoint.h"
168c2ecf20Sopenharmony_ci#include "proc.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* convert our full speed USB rate into sampling rate in Hz */
198c2ecf20Sopenharmony_cistatic inline unsigned get_full_speed_hz(unsigned int usb_rate)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	return (usb_rate * 125 + (1 << 12)) >> 13;
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* convert our high speed USB rate into sampling rate in Hz */
258c2ecf20Sopenharmony_cistatic inline unsigned get_high_speed_hz(unsigned int usb_rate)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	return (usb_rate * 125 + (1 << 9)) >> 10;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * common proc files to show the usb device info
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_cistatic void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip = entry->private_data;
368c2ecf20Sopenharmony_ci	if (!atomic_read(&chip->shutdown))
378c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip = entry->private_data;
438c2ecf20Sopenharmony_ci	if (!atomic_read(&chip->shutdown))
448c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "%04x:%04x\n",
458c2ecf20Sopenharmony_ci			    USB_ID_VENDOR(chip->usb_id),
468c2ecf20Sopenharmony_ci			    USB_ID_PRODUCT(chip->usb_id));
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_civoid snd_usb_audio_create_proc(struct snd_usb_audio *chip)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(chip->card, "usbbus", chip,
528c2ecf20Sopenharmony_ci			     proc_audio_usbbus_read);
538c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(chip->card, "usbid", chip,
548c2ecf20Sopenharmony_ci			     proc_audio_usbid_read);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic const char * const channel_labels[] = {
588c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_NA]	= "N/A",
598c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_MONO]	= "MONO",
608c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_FL]	= "FL",
618c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_FR]	= "FR",
628c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_FC]	= "FC",
638c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_LFE]	= "LFE",
648c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_RL]	= "RL",
658c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_RR]	= "RR",
668c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_FLC]	= "FLC",
678c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_FRC]	= "FRC",
688c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_RC]	= "RC",
698c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_SL]	= "SL",
708c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_SR]	= "SR",
718c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TC]	= "TC",
728c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TFL]	= "TFL",
738c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TFC]	= "TFC",
748c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TFR]	= "TFR",
758c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TRL]	= "TRL",
768c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TRC]	= "TRC",
778c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TRR]	= "TRR",
788c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TFLC]	= "TFLC",
798c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TFRC]	= "TFRC",
808c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_LLFE]	= "LLFE",
818c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_RLFE]	= "RLFE",
828c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TSL]	= "TSL",
838c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_TSR]	= "TSR",
848c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_BC]	= "BC",
858c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_RLC]	= "RLC",
868c2ecf20Sopenharmony_ci	[SNDRV_CHMAP_RRC]	= "RRC",
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/*
908c2ecf20Sopenharmony_ci * proc interface for list the supported pcm formats
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistatic void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct audioformat *fp;
958c2ecf20Sopenharmony_ci	static const char * const sync_types[4] = {
968c2ecf20Sopenharmony_ci		"NONE", "ASYNC", "ADAPTIVE", "SYNC"
978c2ecf20Sopenharmony_ci	};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	list_for_each_entry(fp, &subs->fmt_list, list) {
1008c2ecf20Sopenharmony_ci		snd_pcm_format_t fmt;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "  Interface %d\n", fp->iface);
1038c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Altset %d\n", fp->altsetting);
1048c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Format:");
1058c2ecf20Sopenharmony_ci		pcm_for_each_format(fmt)
1068c2ecf20Sopenharmony_ci			if (fp->formats & pcm_format_to_bits(fmt))
1078c2ecf20Sopenharmony_ci				snd_iprintf(buffer, " %s",
1088c2ecf20Sopenharmony_ci					    snd_pcm_format_name(fmt));
1098c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\n");
1108c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Channels: %d\n", fp->channels);
1118c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Endpoint: %d %s (%s)\n",
1128c2ecf20Sopenharmony_ci			    fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
1138c2ecf20Sopenharmony_ci			    fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
1148c2ecf20Sopenharmony_ci			    sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);
1158c2ecf20Sopenharmony_ci		if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1168c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "    Rates: %d - %d (continuous)\n",
1178c2ecf20Sopenharmony_ci				    fp->rate_min, fp->rate_max);
1188c2ecf20Sopenharmony_ci		} else {
1198c2ecf20Sopenharmony_ci			unsigned int i;
1208c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "    Rates: ");
1218c2ecf20Sopenharmony_ci			for (i = 0; i < fp->nr_rates; i++) {
1228c2ecf20Sopenharmony_ci				if (i > 0)
1238c2ecf20Sopenharmony_ci					snd_iprintf(buffer, ", ");
1248c2ecf20Sopenharmony_ci				snd_iprintf(buffer, "%d", fp->rate_table[i]);
1258c2ecf20Sopenharmony_ci			}
1268c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "\n");
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci		if (subs->speed != USB_SPEED_FULL)
1298c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "    Data packet interval: %d us\n",
1308c2ecf20Sopenharmony_ci				    125 * (1 << fp->datainterval));
1318c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Bits: %d\n", fp->fmt_bits);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci		if (fp->dsd_raw)
1348c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "    DSD raw: DOP=%d, bitrev=%d\n",
1358c2ecf20Sopenharmony_ci				    fp->dsd_dop, fp->dsd_bitrev);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		if (fp->chmap) {
1388c2ecf20Sopenharmony_ci			const struct snd_pcm_chmap_elem *map = fp->chmap;
1398c2ecf20Sopenharmony_ci			int c;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "    Channel map:");
1428c2ecf20Sopenharmony_ci			for (c = 0; c < map->channels; c++) {
1438c2ecf20Sopenharmony_ci				if (map->map[c] >= ARRAY_SIZE(channel_labels) ||
1448c2ecf20Sopenharmony_ci				    !channel_labels[map->map[c]])
1458c2ecf20Sopenharmony_ci					snd_iprintf(buffer, " --");
1468c2ecf20Sopenharmony_ci				else
1478c2ecf20Sopenharmony_ci					snd_iprintf(buffer, " %s",
1488c2ecf20Sopenharmony_ci						    channel_labels[map->map[c]]);
1498c2ecf20Sopenharmony_ci			}
1508c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "\n");
1518c2ecf20Sopenharmony_ci		}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		// snd_iprintf(buffer, "    Max Packet Size = %d\n", fp->maxpacksize);
1548c2ecf20Sopenharmony_ci		// snd_iprintf(buffer, "    EP Attribute = %#x\n", fp->attributes);
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic void proc_dump_ep_status(struct snd_usb_substream *subs,
1598c2ecf20Sopenharmony_ci				struct snd_usb_endpoint *data_ep,
1608c2ecf20Sopenharmony_ci				struct snd_usb_endpoint *sync_ep,
1618c2ecf20Sopenharmony_ci				struct snd_info_buffer *buffer)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	if (!data_ep)
1648c2ecf20Sopenharmony_ci		return;
1658c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "    Packet Size = %d\n", data_ep->curpacksize);
1668c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "    Momentary freq = %u Hz (%#x.%04x)\n",
1678c2ecf20Sopenharmony_ci		    subs->speed == USB_SPEED_FULL
1688c2ecf20Sopenharmony_ci		    ? get_full_speed_hz(data_ep->freqm)
1698c2ecf20Sopenharmony_ci		    : get_high_speed_hz(data_ep->freqm),
1708c2ecf20Sopenharmony_ci		    data_ep->freqm >> 16, data_ep->freqm & 0xffff);
1718c2ecf20Sopenharmony_ci	if (sync_ep && data_ep->freqshift != INT_MIN) {
1728c2ecf20Sopenharmony_ci		int res = 16 - data_ep->freqshift;
1738c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Feedback Format = %d.%d\n",
1748c2ecf20Sopenharmony_ci			    (sync_ep->syncmaxsize > 3 ? 32 : 24) - res, res);
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	if (subs->running) {
1818c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "  Status: Running\n");
1828c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Interface = %d\n", subs->interface);
1838c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "    Altset = %d\n", subs->altset_idx);
1848c2ecf20Sopenharmony_ci		proc_dump_ep_status(subs, subs->data_endpoint, subs->sync_endpoint, buffer);
1858c2ecf20Sopenharmony_ci	} else {
1868c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "  Status: Stop\n");
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct snd_usb_stream *stream = entry->private_data;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
1978c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\nPlayback:\n");
1988c2ecf20Sopenharmony_ci		proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
1998c2ecf20Sopenharmony_ci		proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci	if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2028c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\nCapture:\n");
2038c2ecf20Sopenharmony_ci		proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2048c2ecf20Sopenharmony_ci		proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_civoid snd_usb_proc_pcm_format_add(struct snd_usb_stream *stream)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	char name[32];
2118c2ecf20Sopenharmony_ci	struct snd_card *card = stream->chip->card;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	sprintf(name, "stream%d", stream->pcm_index);
2148c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(card, name, stream, proc_pcm_format_read);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
217