proc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. */
  4. #include <linux/init.h>
  5. #include <linux/usb.h>
  6. #include <sound/core.h>
  7. #include <sound/info.h>
  8. #include <sound/pcm.h>
  9. #include "usbaudio.h"
  10. #include "helper.h"
  11. #include "card.h"
  12. #include "endpoint.h"
  13. #include "proc.h"
  14. /* convert our full speed USB rate into sampling rate in Hz */
  15. static inline unsigned get_full_speed_hz(unsigned int usb_rate)
  16. {
  17. return (usb_rate * 125 + (1 << 12)) >> 13;
  18. }
  19. /* convert our high speed USB rate into sampling rate in Hz */
  20. static inline unsigned get_high_speed_hz(unsigned int usb_rate)
  21. {
  22. return (usb_rate * 125 + (1 << 9)) >> 10;
  23. }
  24. /*
  25. * common proc files to show the usb device info
  26. */
  27. static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  28. {
  29. struct snd_usb_audio *chip = entry->private_data;
  30. if (!atomic_read(&chip->shutdown))
  31. snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
  32. }
  33. static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  34. {
  35. struct snd_usb_audio *chip = entry->private_data;
  36. if (!atomic_read(&chip->shutdown))
  37. snd_iprintf(buffer, "%04x:%04x\n",
  38. USB_ID_VENDOR(chip->usb_id),
  39. USB_ID_PRODUCT(chip->usb_id));
  40. }
  41. void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
  42. {
  43. snd_card_ro_proc_new(chip->card, "usbbus", chip,
  44. proc_audio_usbbus_read);
  45. snd_card_ro_proc_new(chip->card, "usbid", chip,
  46. proc_audio_usbid_read);
  47. }
  48. static const char * const channel_labels[] = {
  49. [SNDRV_CHMAP_NA] = "N/A",
  50. [SNDRV_CHMAP_MONO] = "MONO",
  51. [SNDRV_CHMAP_FL] = "FL",
  52. [SNDRV_CHMAP_FR] = "FR",
  53. [SNDRV_CHMAP_FC] = "FC",
  54. [SNDRV_CHMAP_LFE] = "LFE",
  55. [SNDRV_CHMAP_RL] = "RL",
  56. [SNDRV_CHMAP_RR] = "RR",
  57. [SNDRV_CHMAP_FLC] = "FLC",
  58. [SNDRV_CHMAP_FRC] = "FRC",
  59. [SNDRV_CHMAP_RC] = "RC",
  60. [SNDRV_CHMAP_SL] = "SL",
  61. [SNDRV_CHMAP_SR] = "SR",
  62. [SNDRV_CHMAP_TC] = "TC",
  63. [SNDRV_CHMAP_TFL] = "TFL",
  64. [SNDRV_CHMAP_TFC] = "TFC",
  65. [SNDRV_CHMAP_TFR] = "TFR",
  66. [SNDRV_CHMAP_TRL] = "TRL",
  67. [SNDRV_CHMAP_TRC] = "TRC",
  68. [SNDRV_CHMAP_TRR] = "TRR",
  69. [SNDRV_CHMAP_TFLC] = "TFLC",
  70. [SNDRV_CHMAP_TFRC] = "TFRC",
  71. [SNDRV_CHMAP_LLFE] = "LLFE",
  72. [SNDRV_CHMAP_RLFE] = "RLFE",
  73. [SNDRV_CHMAP_TSL] = "TSL",
  74. [SNDRV_CHMAP_TSR] = "TSR",
  75. [SNDRV_CHMAP_BC] = "BC",
  76. [SNDRV_CHMAP_RLC] = "RLC",
  77. [SNDRV_CHMAP_RRC] = "RRC",
  78. };
  79. /*
  80. * proc interface for list the supported pcm formats
  81. */
  82. static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
  83. {
  84. struct audioformat *fp;
  85. static const char * const sync_types[4] = {
  86. "NONE", "ASYNC", "ADAPTIVE", "SYNC"
  87. };
  88. list_for_each_entry(fp, &subs->fmt_list, list) {
  89. snd_pcm_format_t fmt;
  90. snd_iprintf(buffer, " Interface %d\n", fp->iface);
  91. snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
  92. snd_iprintf(buffer, " Format:");
  93. pcm_for_each_format(fmt)
  94. if (fp->formats & pcm_format_to_bits(fmt))
  95. snd_iprintf(buffer, " %s",
  96. snd_pcm_format_name(fmt));
  97. snd_iprintf(buffer, "\n");
  98. snd_iprintf(buffer, " Channels: %d\n", fp->channels);
  99. snd_iprintf(buffer, " Endpoint: 0x%02x (%d %s) (%s)\n",
  100. fp->endpoint,
  101. fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
  102. fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
  103. sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);
  104. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
  105. snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
  106. fp->rate_min, fp->rate_max);
  107. } else {
  108. unsigned int i;
  109. snd_iprintf(buffer, " Rates: ");
  110. for (i = 0; i < fp->nr_rates; i++) {
  111. if (i > 0)
  112. snd_iprintf(buffer, ", ");
  113. snd_iprintf(buffer, "%d", fp->rate_table[i]);
  114. }
  115. snd_iprintf(buffer, "\n");
  116. }
  117. if (subs->speed != USB_SPEED_FULL)
  118. snd_iprintf(buffer, " Data packet interval: %d us\n",
  119. 125 * (1 << fp->datainterval));
  120. snd_iprintf(buffer, " Bits: %d\n", fp->fmt_bits);
  121. if (fp->dsd_raw)
  122. snd_iprintf(buffer, " DSD raw: DOP=%d, bitrev=%d\n",
  123. fp->dsd_dop, fp->dsd_bitrev);
  124. if (fp->chmap) {
  125. const struct snd_pcm_chmap_elem *map = fp->chmap;
  126. int c;
  127. snd_iprintf(buffer, " Channel map:");
  128. for (c = 0; c < map->channels; c++) {
  129. if (map->map[c] >= ARRAY_SIZE(channel_labels) ||
  130. !channel_labels[map->map[c]])
  131. snd_iprintf(buffer, " --");
  132. else
  133. snd_iprintf(buffer, " %s",
  134. channel_labels[map->map[c]]);
  135. }
  136. snd_iprintf(buffer, "\n");
  137. }
  138. if (fp->sync_ep) {
  139. snd_iprintf(buffer, " Sync Endpoint: 0x%02x (%d %s)\n",
  140. fp->sync_ep,
  141. fp->sync_ep & USB_ENDPOINT_NUMBER_MASK,
  142. fp->sync_ep & USB_DIR_IN ? "IN" : "OUT");
  143. snd_iprintf(buffer, " Sync EP Interface: %d\n",
  144. fp->sync_iface);
  145. snd_iprintf(buffer, " Sync EP Altset: %d\n",
  146. fp->sync_altsetting);
  147. snd_iprintf(buffer, " Implicit Feedback Mode: %s\n",
  148. fp->implicit_fb ? "Yes" : "No");
  149. }
  150. // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
  151. // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
  152. }
  153. }
  154. static void proc_dump_ep_status(struct snd_usb_substream *subs,
  155. struct snd_usb_endpoint *data_ep,
  156. struct snd_usb_endpoint *sync_ep,
  157. struct snd_info_buffer *buffer)
  158. {
  159. if (!data_ep)
  160. return;
  161. snd_iprintf(buffer, " Packet Size = %d\n", data_ep->curpacksize);
  162. snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
  163. subs->speed == USB_SPEED_FULL
  164. ? get_full_speed_hz(data_ep->freqm)
  165. : get_high_speed_hz(data_ep->freqm),
  166. data_ep->freqm >> 16, data_ep->freqm & 0xffff);
  167. if (sync_ep && data_ep->freqshift != INT_MIN) {
  168. int res = 16 - data_ep->freqshift;
  169. snd_iprintf(buffer, " Feedback Format = %d.%d\n",
  170. (sync_ep->syncmaxsize > 3 ? 32 : 24) - res, res);
  171. }
  172. }
  173. static void proc_dump_substream_status(struct snd_usb_audio *chip,
  174. struct snd_usb_substream *subs,
  175. struct snd_info_buffer *buffer)
  176. {
  177. mutex_lock(&chip->mutex);
  178. if (subs->running) {
  179. snd_iprintf(buffer, " Status: Running\n");
  180. if (subs->cur_audiofmt) {
  181. snd_iprintf(buffer, " Interface = %d\n", subs->cur_audiofmt->iface);
  182. snd_iprintf(buffer, " Altset = %d\n", subs->cur_audiofmt->altsetting);
  183. }
  184. proc_dump_ep_status(subs, subs->data_endpoint, subs->sync_endpoint, buffer);
  185. } else {
  186. snd_iprintf(buffer, " Status: Stop\n");
  187. }
  188. mutex_unlock(&chip->mutex);
  189. }
  190. static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  191. {
  192. struct snd_usb_stream *stream = entry->private_data;
  193. struct snd_usb_audio *chip = stream->chip;
  194. snd_iprintf(buffer, "%s : %s\n", chip->card->longname, stream->pcm->name);
  195. if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
  196. snd_iprintf(buffer, "\nPlayback:\n");
  197. proc_dump_substream_status(chip, &stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
  198. proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
  199. }
  200. if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
  201. snd_iprintf(buffer, "\nCapture:\n");
  202. proc_dump_substream_status(chip, &stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
  203. proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
  204. }
  205. }
  206. void snd_usb_proc_pcm_format_add(struct snd_usb_stream *stream)
  207. {
  208. char name[32];
  209. struct snd_card *card = stream->chip->card;
  210. sprintf(name, "stream%d", stream->pcm_index);
  211. snd_card_ro_proc_new(card, name, stream, proc_pcm_format_read);
  212. }