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