pcm.c 33 KB

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