pcm.c 35 KB

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