pcm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Digital Audio (PCM) abstract layer
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/time.h>
  10. #include <linux/mutex.h>
  11. #include <linux/device.h>
  12. #include <linux/nospec.h>
  13. #include <sound/core.h>
  14. #include <sound/minors.h>
  15. #include <sound/pcm.h>
  16. #include <sound/timer.h>
  17. #include <sound/control.h>
  18. #include <sound/info.h>
  19. #include "pcm_local.h"
  20. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
  21. MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
  22. MODULE_LICENSE("GPL");
  23. static LIST_HEAD(snd_pcm_devices);
  24. static DEFINE_MUTEX(register_mutex);
  25. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  26. static LIST_HEAD(snd_pcm_notify_list);
  27. #endif
  28. static int snd_pcm_free(struct snd_pcm *pcm);
  29. static int snd_pcm_dev_free(struct snd_device *device);
  30. static int snd_pcm_dev_register(struct snd_device *device);
  31. static int snd_pcm_dev_disconnect(struct snd_device *device);
  32. static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
  33. {
  34. struct snd_pcm *pcm;
  35. list_for_each_entry(pcm, &snd_pcm_devices, list) {
  36. if (pcm->card == card && pcm->device == device)
  37. return pcm;
  38. }
  39. return NULL;
  40. }
  41. static int snd_pcm_next(struct snd_card *card, int device)
  42. {
  43. struct snd_pcm *pcm;
  44. list_for_each_entry(pcm, &snd_pcm_devices, list) {
  45. if (pcm->card == card && pcm->device > device)
  46. return pcm->device;
  47. else if (pcm->card->number > card->number)
  48. return -1;
  49. }
  50. return -1;
  51. }
  52. static int snd_pcm_add(struct snd_pcm *newpcm)
  53. {
  54. struct snd_pcm *pcm;
  55. if (newpcm->internal)
  56. return 0;
  57. list_for_each_entry(pcm, &snd_pcm_devices, list) {
  58. if (pcm->card == newpcm->card && pcm->device == newpcm->device)
  59. return -EBUSY;
  60. if (pcm->card->number > newpcm->card->number ||
  61. (pcm->card == newpcm->card &&
  62. pcm->device > newpcm->device)) {
  63. list_add(&newpcm->list, pcm->list.prev);
  64. return 0;
  65. }
  66. }
  67. list_add_tail(&newpcm->list, &snd_pcm_devices);
  68. return 0;
  69. }
  70. static int snd_pcm_control_ioctl(struct snd_card *card,
  71. struct snd_ctl_file *control,
  72. unsigned int cmd, unsigned long arg)
  73. {
  74. switch (cmd) {
  75. case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
  76. {
  77. int device;
  78. if (get_user(device, (int __user *)arg))
  79. return -EFAULT;
  80. scoped_guard(mutex, &register_mutex)
  81. device = snd_pcm_next(card, device);
  82. if (put_user(device, (int __user *)arg))
  83. return -EFAULT;
  84. return 0;
  85. }
  86. case SNDRV_CTL_IOCTL_PCM_INFO:
  87. {
  88. struct snd_pcm_info __user *info;
  89. unsigned int device, subdevice;
  90. int stream;
  91. struct snd_pcm *pcm;
  92. struct snd_pcm_str *pstr;
  93. struct snd_pcm_substream *substream;
  94. info = (struct snd_pcm_info __user *)arg;
  95. if (get_user(device, &info->device))
  96. return -EFAULT;
  97. if (get_user(stream, &info->stream))
  98. return -EFAULT;
  99. if (stream < 0 || stream > 1)
  100. return -EINVAL;
  101. stream = array_index_nospec(stream, 2);
  102. if (get_user(subdevice, &info->subdevice))
  103. return -EFAULT;
  104. guard(mutex)(&register_mutex);
  105. pcm = snd_pcm_get(card, device);
  106. if (pcm == NULL)
  107. return -ENXIO;
  108. pstr = &pcm->streams[stream];
  109. if (pstr->substream_count == 0)
  110. return -ENOENT;
  111. if (subdevice >= pstr->substream_count)
  112. return -ENXIO;
  113. for (substream = pstr->substream; substream;
  114. substream = substream->next)
  115. if (substream->number == (int)subdevice)
  116. break;
  117. if (substream == NULL)
  118. return -ENXIO;
  119. guard(mutex)(&pcm->open_mutex);
  120. return snd_pcm_info_user(substream, info);
  121. }
  122. case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
  123. {
  124. int val;
  125. if (get_user(val, (int __user *)arg))
  126. return -EFAULT;
  127. control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
  128. return 0;
  129. }
  130. }
  131. return -ENOIOCTLCMD;
  132. }
  133. #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
  134. static const char * const snd_pcm_format_names[] = {
  135. FORMAT(S8),
  136. FORMAT(U8),
  137. FORMAT(S16_LE),
  138. FORMAT(S16_BE),
  139. FORMAT(U16_LE),
  140. FORMAT(U16_BE),
  141. FORMAT(S24_LE),
  142. FORMAT(S24_BE),
  143. FORMAT(U24_LE),
  144. FORMAT(U24_BE),
  145. FORMAT(S32_LE),
  146. FORMAT(S32_BE),
  147. FORMAT(U32_LE),
  148. FORMAT(U32_BE),
  149. FORMAT(FLOAT_LE),
  150. FORMAT(FLOAT_BE),
  151. FORMAT(FLOAT64_LE),
  152. FORMAT(FLOAT64_BE),
  153. FORMAT(IEC958_SUBFRAME_LE),
  154. FORMAT(IEC958_SUBFRAME_BE),
  155. FORMAT(MU_LAW),
  156. FORMAT(A_LAW),
  157. FORMAT(IMA_ADPCM),
  158. FORMAT(MPEG),
  159. FORMAT(GSM),
  160. FORMAT(SPECIAL),
  161. FORMAT(S24_3LE),
  162. FORMAT(S24_3BE),
  163. FORMAT(U24_3LE),
  164. FORMAT(U24_3BE),
  165. FORMAT(S20_3LE),
  166. FORMAT(S20_3BE),
  167. FORMAT(U20_3LE),
  168. FORMAT(U20_3BE),
  169. FORMAT(S18_3LE),
  170. FORMAT(S18_3BE),
  171. FORMAT(U18_3LE),
  172. FORMAT(U18_3BE),
  173. FORMAT(G723_24),
  174. FORMAT(G723_24_1B),
  175. FORMAT(G723_40),
  176. FORMAT(G723_40_1B),
  177. FORMAT(DSD_U8),
  178. FORMAT(DSD_U16_LE),
  179. FORMAT(DSD_U32_LE),
  180. FORMAT(DSD_U16_BE),
  181. FORMAT(DSD_U32_BE),
  182. FORMAT(S20_LE),
  183. FORMAT(S20_BE),
  184. FORMAT(U20_LE),
  185. FORMAT(U20_BE),
  186. };
  187. /**
  188. * snd_pcm_format_name - Return a name string for the given PCM format
  189. * @format: PCM format
  190. *
  191. * Return: the format name string
  192. */
  193. const char *snd_pcm_format_name(snd_pcm_format_t format)
  194. {
  195. unsigned int format_num = (__force unsigned int)format;
  196. if (format_num >= ARRAY_SIZE(snd_pcm_format_names) || !snd_pcm_format_names[format_num])
  197. return "Unknown";
  198. return snd_pcm_format_names[format_num];
  199. }
  200. EXPORT_SYMBOL_GPL(snd_pcm_format_name);
  201. #ifdef CONFIG_SND_VERBOSE_PROCFS
  202. #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
  203. #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
  204. #define READY(v) [SNDRV_PCM_READY_##v] = #v
  205. #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
  206. #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
  207. #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
  208. #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
  209. #define START(v) [SNDRV_PCM_START_##v] = #v
  210. #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
  211. static const char * const snd_pcm_stream_names[] = {
  212. STREAM(PLAYBACK),
  213. STREAM(CAPTURE),
  214. };
  215. static const char * const snd_pcm_state_names[] = {
  216. STATE(OPEN),
  217. STATE(SETUP),
  218. STATE(PREPARED),
  219. STATE(RUNNING),
  220. STATE(XRUN),
  221. STATE(DRAINING),
  222. STATE(PAUSED),
  223. STATE(SUSPENDED),
  224. STATE(DISCONNECTED),
  225. };
  226. static const char * const snd_pcm_access_names[] = {
  227. ACCESS(MMAP_INTERLEAVED),
  228. ACCESS(MMAP_NONINTERLEAVED),
  229. ACCESS(MMAP_COMPLEX),
  230. ACCESS(RW_INTERLEAVED),
  231. ACCESS(RW_NONINTERLEAVED),
  232. };
  233. static const char * const snd_pcm_subformat_names[] = {
  234. SUBFORMAT(STD),
  235. SUBFORMAT(MSBITS_MAX),
  236. SUBFORMAT(MSBITS_20),
  237. SUBFORMAT(MSBITS_24),
  238. };
  239. static const char * const snd_pcm_tstamp_mode_names[] = {
  240. TSTAMP(NONE),
  241. TSTAMP(ENABLE),
  242. };
  243. static const char *snd_pcm_stream_name(int stream)
  244. {
  245. return snd_pcm_stream_names[stream];
  246. }
  247. static const char *snd_pcm_access_name(snd_pcm_access_t access)
  248. {
  249. return snd_pcm_access_names[(__force int)access];
  250. }
  251. static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
  252. {
  253. return snd_pcm_subformat_names[(__force int)subformat];
  254. }
  255. static const char *snd_pcm_tstamp_mode_name(int mode)
  256. {
  257. return snd_pcm_tstamp_mode_names[mode];
  258. }
  259. static const char *snd_pcm_state_name(snd_pcm_state_t state)
  260. {
  261. return snd_pcm_state_names[(__force int)state];
  262. }
  263. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  264. #include <linux/soundcard.h>
  265. static const char *snd_pcm_oss_format_name(int format)
  266. {
  267. switch (format) {
  268. case AFMT_MU_LAW:
  269. return "MU_LAW";
  270. case AFMT_A_LAW:
  271. return "A_LAW";
  272. case AFMT_IMA_ADPCM:
  273. return "IMA_ADPCM";
  274. case AFMT_U8:
  275. return "U8";
  276. case AFMT_S16_LE:
  277. return "S16_LE";
  278. case AFMT_S16_BE:
  279. return "S16_BE";
  280. case AFMT_S8:
  281. return "S8";
  282. case AFMT_U16_LE:
  283. return "U16_LE";
  284. case AFMT_U16_BE:
  285. return "U16_BE";
  286. case AFMT_MPEG:
  287. return "MPEG";
  288. default:
  289. return "unknown";
  290. }
  291. }
  292. #endif
  293. static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
  294. struct snd_info_buffer *buffer)
  295. {
  296. struct snd_pcm_info *info __free(kfree) = NULL;
  297. int err;
  298. if (! substream)
  299. return;
  300. info = kmalloc(sizeof(*info), GFP_KERNEL);
  301. if (!info)
  302. return;
  303. err = snd_pcm_info(substream, info);
  304. if (err < 0) {
  305. snd_iprintf(buffer, "error %d\n", err);
  306. return;
  307. }
  308. snd_iprintf(buffer, "card: %d\n", info->card);
  309. snd_iprintf(buffer, "device: %d\n", info->device);
  310. snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
  311. snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
  312. snd_iprintf(buffer, "id: %s\n", info->id);
  313. snd_iprintf(buffer, "name: %s\n", info->name);
  314. snd_iprintf(buffer, "subname: %s\n", info->subname);
  315. snd_iprintf(buffer, "class: %d\n", info->dev_class);
  316. snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
  317. snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
  318. snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
  319. }
  320. static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
  321. struct snd_info_buffer *buffer)
  322. {
  323. snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
  324. buffer);
  325. }
  326. static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
  327. struct snd_info_buffer *buffer)
  328. {
  329. snd_pcm_proc_info_read(entry->private_data, buffer);
  330. }
  331. static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
  332. struct snd_info_buffer *buffer)
  333. {
  334. struct snd_pcm_substream *substream = entry->private_data;
  335. struct snd_pcm_runtime *runtime;
  336. guard(mutex)(&substream->pcm->open_mutex);
  337. runtime = substream->runtime;
  338. if (!runtime) {
  339. snd_iprintf(buffer, "closed\n");
  340. return;
  341. }
  342. if (runtime->state == SNDRV_PCM_STATE_OPEN) {
  343. snd_iprintf(buffer, "no setup\n");
  344. return;
  345. }
  346. snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
  347. snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
  348. snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
  349. snd_iprintf(buffer, "channels: %u\n", runtime->channels);
  350. snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
  351. snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
  352. snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
  353. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  354. if (substream->oss.oss) {
  355. snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
  356. snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
  357. snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
  358. snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
  359. snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
  360. snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
  361. }
  362. #endif
  363. }
  364. static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
  365. struct snd_info_buffer *buffer)
  366. {
  367. struct snd_pcm_substream *substream = entry->private_data;
  368. struct snd_pcm_runtime *runtime;
  369. guard(mutex)(&substream->pcm->open_mutex);
  370. runtime = substream->runtime;
  371. if (!runtime) {
  372. snd_iprintf(buffer, "closed\n");
  373. return;
  374. }
  375. if (runtime->state == SNDRV_PCM_STATE_OPEN) {
  376. snd_iprintf(buffer, "no setup\n");
  377. return;
  378. }
  379. snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
  380. snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
  381. snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
  382. snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
  383. snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
  384. snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
  385. snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
  386. snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
  387. }
  388. static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
  389. struct snd_info_buffer *buffer)
  390. {
  391. struct snd_pcm_substream *substream = entry->private_data;
  392. struct snd_pcm_runtime *runtime;
  393. struct snd_pcm_status64 status;
  394. int err;
  395. guard(mutex)(&substream->pcm->open_mutex);
  396. runtime = substream->runtime;
  397. if (!runtime) {
  398. snd_iprintf(buffer, "closed\n");
  399. return;
  400. }
  401. memset(&status, 0, sizeof(status));
  402. err = snd_pcm_status64(substream, &status);
  403. if (err < 0) {
  404. snd_iprintf(buffer, "error %d\n", err);
  405. return;
  406. }
  407. snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
  408. snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
  409. snd_iprintf(buffer, "trigger_time: %lld.%09lld\n",
  410. status.trigger_tstamp_sec, status.trigger_tstamp_nsec);
  411. snd_iprintf(buffer, "tstamp : %lld.%09lld\n",
  412. status.tstamp_sec, status.tstamp_nsec);
  413. snd_iprintf(buffer, "delay : %ld\n", status.delay);
  414. snd_iprintf(buffer, "avail : %ld\n", status.avail);
  415. snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
  416. snd_iprintf(buffer, "-----\n");
  417. snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
  418. snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
  419. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  420. snd_iprintf(buffer, "xrun_counter: %d\n", substream->xrun_counter);
  421. #endif
  422. }
  423. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  424. static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
  425. struct snd_info_buffer *buffer)
  426. {
  427. struct snd_pcm_substream *substream = entry->private_data;
  428. snd_pcm_stop_xrun(substream);
  429. }
  430. static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
  431. struct snd_info_buffer *buffer)
  432. {
  433. struct snd_pcm_str *pstr = entry->private_data;
  434. snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
  435. }
  436. static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
  437. struct snd_info_buffer *buffer)
  438. {
  439. struct snd_pcm_str *pstr = entry->private_data;
  440. char line[64];
  441. if (!snd_info_get_line(buffer, line, sizeof(line)))
  442. pstr->xrun_debug = simple_strtoul(line, NULL, 10);
  443. }
  444. #endif
  445. static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
  446. {
  447. struct snd_pcm *pcm = pstr->pcm;
  448. struct snd_info_entry *entry;
  449. char name[16];
  450. sprintf(name, "pcm%i%c", pcm->device,
  451. pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
  452. entry = snd_info_create_card_entry(pcm->card, name,
  453. pcm->card->proc_root);
  454. if (!entry)
  455. return -ENOMEM;
  456. entry->mode = S_IFDIR | 0555;
  457. pstr->proc_root = entry;
  458. entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root);
  459. if (entry)
  460. snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
  461. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  462. entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
  463. pstr->proc_root);
  464. if (entry) {
  465. snd_info_set_text_ops(entry, pstr, snd_pcm_xrun_debug_read);
  466. entry->c.text.write = snd_pcm_xrun_debug_write;
  467. entry->mode |= 0200;
  468. }
  469. #endif
  470. return 0;
  471. }
  472. static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
  473. {
  474. snd_info_free_entry(pstr->proc_root);
  475. pstr->proc_root = NULL;
  476. return 0;
  477. }
  478. static struct snd_info_entry *
  479. create_substream_info_entry(struct snd_pcm_substream *substream,
  480. const char *name,
  481. void (*read)(struct snd_info_entry *,
  482. struct snd_info_buffer *))
  483. {
  484. struct snd_info_entry *entry;
  485. entry = snd_info_create_card_entry(substream->pcm->card, name,
  486. substream->proc_root);
  487. if (entry)
  488. snd_info_set_text_ops(entry, substream, read);
  489. return entry;
  490. }
  491. static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
  492. {
  493. struct snd_info_entry *entry;
  494. struct snd_card *card;
  495. char name[16];
  496. card = substream->pcm->card;
  497. sprintf(name, "sub%i", substream->number);
  498. entry = snd_info_create_card_entry(card, name,
  499. substream->pstr->proc_root);
  500. if (!entry)
  501. return -ENOMEM;
  502. entry->mode = S_IFDIR | 0555;
  503. substream->proc_root = entry;
  504. create_substream_info_entry(substream, "info",
  505. snd_pcm_substream_proc_info_read);
  506. create_substream_info_entry(substream, "hw_params",
  507. snd_pcm_substream_proc_hw_params_read);
  508. create_substream_info_entry(substream, "sw_params",
  509. snd_pcm_substream_proc_sw_params_read);
  510. create_substream_info_entry(substream, "status",
  511. snd_pcm_substream_proc_status_read);
  512. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  513. entry = create_substream_info_entry(substream, "xrun_injection", NULL);
  514. if (entry) {
  515. entry->c.text.write = snd_pcm_xrun_injection_write;
  516. entry->mode = S_IFREG | 0200;
  517. }
  518. #endif /* CONFIG_SND_PCM_XRUN_DEBUG */
  519. return 0;
  520. }
  521. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  522. static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
  523. static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
  524. static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
  525. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  526. static const struct attribute_group *pcm_dev_attr_groups[];
  527. /*
  528. * PM callbacks: we need to deal only with suspend here, as the resume is
  529. * triggered either from user-space or the driver's resume callback
  530. */
  531. #ifdef CONFIG_PM_SLEEP
  532. static int do_pcm_suspend(struct device *dev)
  533. {
  534. struct snd_pcm_str *pstr = dev_get_drvdata(dev);
  535. if (!pstr->pcm->no_device_suspend)
  536. snd_pcm_suspend_all(pstr->pcm);
  537. return 0;
  538. }
  539. #endif
  540. static const struct dev_pm_ops pcm_dev_pm_ops = {
  541. SET_SYSTEM_SLEEP_PM_OPS(do_pcm_suspend, NULL)
  542. };
  543. /* device type for PCM -- basically only for passing PM callbacks */
  544. static const struct device_type pcm_dev_type = {
  545. .name = "pcm",
  546. .pm = &pcm_dev_pm_ops,
  547. };
  548. /**
  549. * snd_pcm_new_stream - create a new PCM stream
  550. * @pcm: the pcm instance
  551. * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
  552. * @substream_count: the number of substreams
  553. *
  554. * Creates a new stream for the pcm.
  555. * The corresponding stream on the pcm must have been empty before
  556. * calling this, i.e. zero must be given to the argument of
  557. * snd_pcm_new().
  558. *
  559. * Return: Zero if successful, or a negative error code on failure.
  560. */
  561. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
  562. {
  563. int idx, err;
  564. struct snd_pcm_str *pstr = &pcm->streams[stream];
  565. struct snd_pcm_substream *substream, *prev;
  566. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  567. mutex_init(&pstr->oss.setup_mutex);
  568. #endif
  569. pstr->stream = stream;
  570. pstr->pcm = pcm;
  571. pstr->substream_count = substream_count;
  572. if (!substream_count)
  573. return 0;
  574. err = snd_device_alloc(&pstr->dev, pcm->card);
  575. if (err < 0)
  576. return err;
  577. dev_set_name(pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device,
  578. stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
  579. pstr->dev->groups = pcm_dev_attr_groups;
  580. pstr->dev->type = &pcm_dev_type;
  581. dev_set_drvdata(pstr->dev, pstr);
  582. if (!pcm->internal) {
  583. err = snd_pcm_stream_proc_init(pstr);
  584. if (err < 0) {
  585. pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
  586. return err;
  587. }
  588. }
  589. prev = NULL;
  590. for (idx = 0, prev = NULL; idx < substream_count; idx++) {
  591. substream = kzalloc(sizeof(*substream), GFP_KERNEL);
  592. if (!substream)
  593. return -ENOMEM;
  594. substream->pcm = pcm;
  595. substream->pstr = pstr;
  596. substream->number = idx;
  597. substream->stream = stream;
  598. sprintf(substream->name, "subdevice #%i", idx);
  599. substream->buffer_bytes_max = UINT_MAX;
  600. if (prev == NULL)
  601. pstr->substream = substream;
  602. else
  603. prev->next = substream;
  604. if (!pcm->internal) {
  605. err = snd_pcm_substream_proc_init(substream);
  606. if (err < 0) {
  607. pcm_err(pcm,
  608. "Error in snd_pcm_stream_proc_init\n");
  609. if (prev == NULL)
  610. pstr->substream = NULL;
  611. else
  612. prev->next = NULL;
  613. kfree(substream);
  614. return err;
  615. }
  616. }
  617. substream->group = &substream->self_group;
  618. snd_pcm_group_init(&substream->self_group);
  619. list_add_tail(&substream->link_list, &substream->self_group.substreams);
  620. atomic_set(&substream->mmap_count, 0);
  621. prev = substream;
  622. }
  623. return 0;
  624. }
  625. EXPORT_SYMBOL(snd_pcm_new_stream);
  626. static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
  627. int playback_count, int capture_count, bool internal,
  628. struct snd_pcm **rpcm)
  629. {
  630. struct snd_pcm *pcm;
  631. int err;
  632. static const struct snd_device_ops ops = {
  633. .dev_free = snd_pcm_dev_free,
  634. .dev_register = snd_pcm_dev_register,
  635. .dev_disconnect = snd_pcm_dev_disconnect,
  636. };
  637. static const struct snd_device_ops internal_ops = {
  638. .dev_free = snd_pcm_dev_free,
  639. };
  640. if (snd_BUG_ON(!card))
  641. return -ENXIO;
  642. if (rpcm)
  643. *rpcm = NULL;
  644. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  645. if (!pcm)
  646. return -ENOMEM;
  647. pcm->card = card;
  648. pcm->device = device;
  649. pcm->internal = internal;
  650. mutex_init(&pcm->open_mutex);
  651. init_waitqueue_head(&pcm->open_wait);
  652. INIT_LIST_HEAD(&pcm->list);
  653. if (id)
  654. strscpy(pcm->id, id, sizeof(pcm->id));
  655. err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  656. playback_count);
  657. if (err < 0)
  658. goto free_pcm;
  659. err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count);
  660. if (err < 0)
  661. goto free_pcm;
  662. err = snd_device_new(card, SNDRV_DEV_PCM, pcm,
  663. internal ? &internal_ops : &ops);
  664. if (err < 0)
  665. goto free_pcm;
  666. if (rpcm)
  667. *rpcm = pcm;
  668. return 0;
  669. free_pcm:
  670. snd_pcm_free(pcm);
  671. return err;
  672. }
  673. /**
  674. * snd_pcm_new - create a new PCM instance
  675. * @card: the card instance
  676. * @id: the id string
  677. * @device: the device index (zero based)
  678. * @playback_count: the number of substreams for playback
  679. * @capture_count: the number of substreams for capture
  680. * @rpcm: the pointer to store the new pcm instance
  681. *
  682. * Creates a new PCM instance.
  683. *
  684. * The pcm operators have to be set afterwards to the new instance
  685. * via snd_pcm_set_ops().
  686. *
  687. * Return: Zero if successful, or a negative error code on failure.
  688. */
  689. int snd_pcm_new(struct snd_card *card, const char *id, int device,
  690. int playback_count, int capture_count, struct snd_pcm **rpcm)
  691. {
  692. return _snd_pcm_new(card, id, device, playback_count, capture_count,
  693. false, rpcm);
  694. }
  695. EXPORT_SYMBOL(snd_pcm_new);
  696. /**
  697. * snd_pcm_new_internal - create a new internal PCM instance
  698. * @card: the card instance
  699. * @id: the id string
  700. * @device: the device index (zero based - shared with normal PCMs)
  701. * @playback_count: the number of substreams for playback
  702. * @capture_count: the number of substreams for capture
  703. * @rpcm: the pointer to store the new pcm instance
  704. *
  705. * Creates a new internal PCM instance with no userspace device or procfs
  706. * entries. This is used by ASoC Back End PCMs in order to create a PCM that
  707. * will only be used internally by kernel drivers. i.e. it cannot be opened
  708. * by userspace. It provides existing ASoC components drivers with a substream
  709. * and access to any private data.
  710. *
  711. * The pcm operators have to be set afterwards to the new instance
  712. * via snd_pcm_set_ops().
  713. *
  714. * Return: Zero if successful, or a negative error code on failure.
  715. */
  716. int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
  717. int playback_count, int capture_count,
  718. struct snd_pcm **rpcm)
  719. {
  720. return _snd_pcm_new(card, id, device, playback_count, capture_count,
  721. true, rpcm);
  722. }
  723. EXPORT_SYMBOL(snd_pcm_new_internal);
  724. static void free_chmap(struct snd_pcm_str *pstr)
  725. {
  726. if (pstr->chmap_kctl) {
  727. struct snd_card *card = pstr->pcm->card;
  728. snd_ctl_remove(card, pstr->chmap_kctl);
  729. pstr->chmap_kctl = NULL;
  730. }
  731. }
  732. static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
  733. {
  734. struct snd_pcm_substream *substream, *substream_next;
  735. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  736. struct snd_pcm_oss_setup *setup, *setupn;
  737. #endif
  738. /* free all proc files under the stream */
  739. snd_pcm_stream_proc_done(pstr);
  740. substream = pstr->substream;
  741. while (substream) {
  742. substream_next = substream->next;
  743. snd_pcm_timer_done(substream);
  744. kfree(substream);
  745. substream = substream_next;
  746. }
  747. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  748. for (setup = pstr->oss.setup_list; setup; setup = setupn) {
  749. setupn = setup->next;
  750. kfree(setup->task_name);
  751. kfree(setup);
  752. }
  753. #endif
  754. free_chmap(pstr);
  755. if (pstr->substream_count)
  756. put_device(pstr->dev);
  757. }
  758. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  759. #define pcm_call_notify(pcm, call) \
  760. do { \
  761. struct snd_pcm_notify *_notify; \
  762. list_for_each_entry(_notify, &snd_pcm_notify_list, list) \
  763. _notify->call(pcm); \
  764. } while (0)
  765. #else
  766. #define pcm_call_notify(pcm, call) do {} while (0)
  767. #endif
  768. static int snd_pcm_free(struct snd_pcm *pcm)
  769. {
  770. if (!pcm)
  771. return 0;
  772. if (!pcm->internal)
  773. pcm_call_notify(pcm, n_unregister);
  774. if (pcm->private_free)
  775. pcm->private_free(pcm);
  776. snd_pcm_lib_preallocate_free_for_all(pcm);
  777. snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  778. snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
  779. kfree(pcm);
  780. return 0;
  781. }
  782. static int snd_pcm_dev_free(struct snd_device *device)
  783. {
  784. struct snd_pcm *pcm = device->device_data;
  785. return snd_pcm_free(pcm);
  786. }
  787. int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
  788. struct file *file,
  789. struct snd_pcm_substream **rsubstream)
  790. {
  791. struct snd_pcm_str * pstr;
  792. struct snd_pcm_substream *substream;
  793. struct snd_pcm_runtime *runtime;
  794. struct snd_card *card;
  795. int prefer_subdevice;
  796. size_t size;
  797. if (snd_BUG_ON(!pcm || !rsubstream))
  798. return -ENXIO;
  799. if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
  800. stream != SNDRV_PCM_STREAM_CAPTURE))
  801. return -EINVAL;
  802. *rsubstream = NULL;
  803. pstr = &pcm->streams[stream];
  804. if (pstr->substream == NULL || pstr->substream_count == 0)
  805. return -ENODEV;
  806. card = pcm->card;
  807. prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
  808. if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
  809. int opposite = !stream;
  810. for (substream = pcm->streams[opposite].substream; substream;
  811. substream = substream->next) {
  812. if (SUBSTREAM_BUSY(substream))
  813. return -EAGAIN;
  814. }
  815. }
  816. if (file->f_flags & O_APPEND) {
  817. if (prefer_subdevice < 0) {
  818. if (pstr->substream_count > 1)
  819. return -EINVAL; /* must be unique */
  820. substream = pstr->substream;
  821. } else {
  822. for (substream = pstr->substream; substream;
  823. substream = substream->next)
  824. if (substream->number == prefer_subdevice)
  825. break;
  826. }
  827. if (! substream)
  828. return -ENODEV;
  829. if (! SUBSTREAM_BUSY(substream))
  830. return -EBADFD;
  831. substream->ref_count++;
  832. *rsubstream = substream;
  833. return 0;
  834. }
  835. for (substream = pstr->substream; substream; substream = substream->next) {
  836. if (!SUBSTREAM_BUSY(substream) &&
  837. (prefer_subdevice == -1 ||
  838. substream->number == prefer_subdevice))
  839. break;
  840. }
  841. if (substream == NULL)
  842. return -EAGAIN;
  843. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  844. if (runtime == NULL)
  845. return -ENOMEM;
  846. size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
  847. runtime->status = alloc_pages_exact(size, GFP_KERNEL);
  848. if (runtime->status == NULL) {
  849. kfree(runtime);
  850. return -ENOMEM;
  851. }
  852. memset(runtime->status, 0, size);
  853. size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
  854. runtime->control = alloc_pages_exact(size, GFP_KERNEL);
  855. if (runtime->control == NULL) {
  856. free_pages_exact(runtime->status,
  857. PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
  858. kfree(runtime);
  859. return -ENOMEM;
  860. }
  861. memset(runtime->control, 0, size);
  862. init_waitqueue_head(&runtime->sleep);
  863. init_waitqueue_head(&runtime->tsleep);
  864. __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_OPEN);
  865. mutex_init(&runtime->buffer_mutex);
  866. atomic_set(&runtime->buffer_accessing, 0);
  867. substream->runtime = runtime;
  868. substream->private_data = pcm->private_data;
  869. substream->ref_count = 1;
  870. substream->f_flags = file->f_flags;
  871. substream->pid = get_pid(task_pid(current));
  872. pstr->substream_opened++;
  873. *rsubstream = substream;
  874. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  875. substream->xrun_counter = 0;
  876. #endif /* CONFIG_SND_PCM_XRUN_DEBUG */
  877. return 0;
  878. }
  879. void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
  880. {
  881. struct snd_pcm_runtime *runtime;
  882. if (PCM_RUNTIME_CHECK(substream))
  883. return;
  884. runtime = substream->runtime;
  885. if (runtime->private_free != NULL)
  886. runtime->private_free(runtime);
  887. free_pages_exact(runtime->status,
  888. PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
  889. free_pages_exact(runtime->control,
  890. PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
  891. kfree(runtime->hw_constraints.rules);
  892. /* Avoid concurrent access to runtime via PCM timer interface */
  893. if (substream->timer) {
  894. scoped_guard(spinlock_irq, &substream->timer->lock)
  895. substream->runtime = NULL;
  896. } else {
  897. substream->runtime = NULL;
  898. }
  899. mutex_destroy(&runtime->buffer_mutex);
  900. snd_fasync_free(runtime->fasync);
  901. kfree(runtime);
  902. put_pid(substream->pid);
  903. substream->pid = NULL;
  904. substream->pstr->substream_opened--;
  905. }
  906. static ssize_t pcm_class_show(struct device *dev,
  907. struct device_attribute *attr, char *buf)
  908. {
  909. struct snd_pcm_str *pstr = dev_get_drvdata(dev);
  910. struct snd_pcm *pcm = pstr->pcm;
  911. const char *str;
  912. static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
  913. [SNDRV_PCM_CLASS_GENERIC] = "generic",
  914. [SNDRV_PCM_CLASS_MULTI] = "multi",
  915. [SNDRV_PCM_CLASS_MODEM] = "modem",
  916. [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
  917. };
  918. if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
  919. str = "none";
  920. else
  921. str = strs[pcm->dev_class];
  922. return sysfs_emit(buf, "%s\n", str);
  923. }
  924. static DEVICE_ATTR_RO(pcm_class);
  925. static struct attribute *pcm_dev_attrs[] = {
  926. &dev_attr_pcm_class.attr,
  927. NULL
  928. };
  929. static const struct attribute_group pcm_dev_attr_group = {
  930. .attrs = pcm_dev_attrs,
  931. };
  932. static const struct attribute_group *pcm_dev_attr_groups[] = {
  933. &pcm_dev_attr_group,
  934. NULL
  935. };
  936. static int snd_pcm_dev_register(struct snd_device *device)
  937. {
  938. int cidx, err;
  939. struct snd_pcm_substream *substream;
  940. struct snd_pcm *pcm;
  941. if (snd_BUG_ON(!device || !device->device_data))
  942. return -ENXIO;
  943. pcm = device->device_data;
  944. guard(mutex)(&register_mutex);
  945. err = snd_pcm_add(pcm);
  946. if (err)
  947. return err;
  948. for (cidx = 0; cidx < 2; cidx++) {
  949. int devtype = -1;
  950. if (pcm->streams[cidx].substream == NULL)
  951. continue;
  952. switch (cidx) {
  953. case SNDRV_PCM_STREAM_PLAYBACK:
  954. devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
  955. break;
  956. case SNDRV_PCM_STREAM_CAPTURE:
  957. devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
  958. break;
  959. }
  960. /* register pcm */
  961. err = snd_register_device(devtype, pcm->card, pcm->device,
  962. &snd_pcm_f_ops[cidx], pcm,
  963. pcm->streams[cidx].dev);
  964. if (err < 0) {
  965. list_del_init(&pcm->list);
  966. return err;
  967. }
  968. for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
  969. snd_pcm_timer_init(substream);
  970. }
  971. pcm_call_notify(pcm, n_register);
  972. return err;
  973. }
  974. static int snd_pcm_dev_disconnect(struct snd_device *device)
  975. {
  976. struct snd_pcm *pcm = device->device_data;
  977. struct snd_pcm_substream *substream;
  978. int cidx;
  979. guard(mutex)(&register_mutex);
  980. guard(mutex)(&pcm->open_mutex);
  981. wake_up(&pcm->open_wait);
  982. list_del_init(&pcm->list);
  983. for_each_pcm_substream(pcm, cidx, substream) {
  984. snd_pcm_stream_lock_irq(substream);
  985. if (substream->runtime) {
  986. if (snd_pcm_running(substream))
  987. snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
  988. /* to be sure, set the state unconditionally */
  989. __snd_pcm_set_state(substream->runtime,
  990. SNDRV_PCM_STATE_DISCONNECTED);
  991. wake_up(&substream->runtime->sleep);
  992. wake_up(&substream->runtime->tsleep);
  993. }
  994. snd_pcm_stream_unlock_irq(substream);
  995. }
  996. for_each_pcm_substream(pcm, cidx, substream)
  997. snd_pcm_sync_stop(substream, false);
  998. pcm_call_notify(pcm, n_disconnect);
  999. for (cidx = 0; cidx < 2; cidx++) {
  1000. if (pcm->streams[cidx].dev)
  1001. snd_unregister_device(pcm->streams[cidx].dev);
  1002. free_chmap(&pcm->streams[cidx]);
  1003. }
  1004. return 0;
  1005. }
  1006. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  1007. /**
  1008. * snd_pcm_notify - Add/remove the notify list
  1009. * @notify: PCM notify list
  1010. * @nfree: 0 = register, 1 = unregister
  1011. *
  1012. * This adds the given notifier to the global list so that the callback is
  1013. * called for each registered PCM devices. This exists only for PCM OSS
  1014. * emulation, so far.
  1015. *
  1016. * Return: zero if successful, or a negative error code
  1017. */
  1018. int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
  1019. {
  1020. struct snd_pcm *pcm;
  1021. if (snd_BUG_ON(!notify ||
  1022. !notify->n_register ||
  1023. !notify->n_unregister ||
  1024. !notify->n_disconnect))
  1025. return -EINVAL;
  1026. guard(mutex)(&register_mutex);
  1027. if (nfree) {
  1028. list_del(&notify->list);
  1029. list_for_each_entry(pcm, &snd_pcm_devices, list)
  1030. notify->n_unregister(pcm);
  1031. } else {
  1032. list_add_tail(&notify->list, &snd_pcm_notify_list);
  1033. list_for_each_entry(pcm, &snd_pcm_devices, list)
  1034. notify->n_register(pcm);
  1035. }
  1036. return 0;
  1037. }
  1038. EXPORT_SYMBOL(snd_pcm_notify);
  1039. #endif /* CONFIG_SND_PCM_OSS */
  1040. #ifdef CONFIG_SND_PROC_FS
  1041. /*
  1042. * Info interface
  1043. */
  1044. static void snd_pcm_proc_read(struct snd_info_entry *entry,
  1045. struct snd_info_buffer *buffer)
  1046. {
  1047. struct snd_pcm *pcm;
  1048. guard(mutex)(&register_mutex);
  1049. list_for_each_entry(pcm, &snd_pcm_devices, list) {
  1050. snd_iprintf(buffer, "%02i-%02i: %s : %s",
  1051. pcm->card->number, pcm->device, pcm->id, pcm->name);
  1052. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
  1053. snd_iprintf(buffer, " : playback %i",
  1054. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
  1055. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
  1056. snd_iprintf(buffer, " : capture %i",
  1057. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
  1058. snd_iprintf(buffer, "\n");
  1059. }
  1060. }
  1061. static struct snd_info_entry *snd_pcm_proc_entry;
  1062. static void snd_pcm_proc_init(void)
  1063. {
  1064. struct snd_info_entry *entry;
  1065. entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL);
  1066. if (entry) {
  1067. snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
  1068. if (snd_info_register(entry) < 0) {
  1069. snd_info_free_entry(entry);
  1070. entry = NULL;
  1071. }
  1072. }
  1073. snd_pcm_proc_entry = entry;
  1074. }
  1075. static void snd_pcm_proc_done(void)
  1076. {
  1077. snd_info_free_entry(snd_pcm_proc_entry);
  1078. }
  1079. #else /* !CONFIG_SND_PROC_FS */
  1080. #define snd_pcm_proc_init()
  1081. #define snd_pcm_proc_done()
  1082. #endif /* CONFIG_SND_PROC_FS */
  1083. /*
  1084. * ENTRY functions
  1085. */
  1086. static int __init alsa_pcm_init(void)
  1087. {
  1088. snd_ctl_register_ioctl(snd_pcm_control_ioctl);
  1089. snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
  1090. snd_pcm_proc_init();
  1091. return 0;
  1092. }
  1093. static void __exit alsa_pcm_exit(void)
  1094. {
  1095. snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
  1096. snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
  1097. snd_pcm_proc_done();
  1098. }
  1099. module_init(alsa_pcm_init)
  1100. module_exit(alsa_pcm_exit)