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