pcm.c 34 KB

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