pcm.c 31 KB

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