pcm.c 35 KB

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