pcm.c 31 KB

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