pcm.c 35 KB

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