pcm.c 35 KB

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