pcm.c 34 KB

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