pcm.c 34 KB

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