pcm.c 35 KB

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