pcm.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. snd_pcm_stream_lock_irq(substream);
  322. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  323. snd_iprintf(buffer, "no setup\n");
  324. snd_pcm_stream_unlock_irq(substream);
  325. return;
  326. }
  327. snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
  328. snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
  329. snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
  330. snd_iprintf(buffer, "channels: %u\n", runtime->channels);
  331. snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
  332. snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
  333. snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
  334. snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
  335. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  336. if (substream->oss.oss) {
  337. snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
  338. snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
  339. snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
  340. snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
  341. snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
  342. snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
  343. }
  344. #endif
  345. snd_pcm_stream_unlock_irq(substream);
  346. }
  347. static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
  348. struct snd_info_buffer *buffer)
  349. {
  350. struct snd_pcm_substream *substream = entry->private_data;
  351. struct snd_pcm_runtime *runtime = substream->runtime;
  352. if (!runtime) {
  353. snd_iprintf(buffer, "closed\n");
  354. return;
  355. }
  356. snd_pcm_stream_lock_irq(substream);
  357. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  358. snd_iprintf(buffer, "no setup\n");
  359. snd_pcm_stream_unlock_irq(substream);
  360. return;
  361. }
  362. snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
  363. snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
  364. snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
  365. snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
  366. snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
  367. snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
  368. snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
  369. snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
  370. snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
  371. snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
  372. snd_pcm_stream_unlock_irq(substream);
  373. }
  374. static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
  375. struct snd_info_buffer *buffer)
  376. {
  377. struct snd_pcm_substream *substream = entry->private_data;
  378. struct snd_pcm_runtime *runtime = substream->runtime;
  379. struct snd_pcm_status status;
  380. int err;
  381. if (!runtime) {
  382. snd_iprintf(buffer, "closed\n");
  383. return;
  384. }
  385. memset(&status, 0, sizeof(status));
  386. err = snd_pcm_status(substream, &status);
  387. if (err < 0) {
  388. snd_iprintf(buffer, "error %d\n", err);
  389. return;
  390. }
  391. snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
  392. snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
  393. status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
  394. snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
  395. status.tstamp.tv_sec, status.tstamp.tv_nsec);
  396. snd_iprintf(buffer, "delay : %ld\n", status.delay);
  397. snd_iprintf(buffer, "avail : %ld\n", status.avail);
  398. snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
  399. snd_iprintf(buffer, "-----\n");
  400. snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
  401. snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
  402. }
  403. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  404. static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
  405. struct snd_info_buffer *buffer)
  406. {
  407. struct snd_pcm_str *pstr = entry->private_data;
  408. snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
  409. }
  410. static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
  411. struct snd_info_buffer *buffer)
  412. {
  413. struct snd_pcm_str *pstr = entry->private_data;
  414. char line[64];
  415. if (!snd_info_get_line(buffer, line, sizeof(line)))
  416. pstr->xrun_debug = simple_strtoul(line, NULL, 10);
  417. }
  418. #endif
  419. static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
  420. {
  421. struct snd_pcm *pcm = pstr->pcm;
  422. struct snd_info_entry *entry;
  423. char name[16];
  424. sprintf(name, "pcm%i%c", pcm->device,
  425. pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
  426. if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
  427. return -ENOMEM;
  428. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  429. if (snd_info_register(entry) < 0) {
  430. snd_info_free_entry(entry);
  431. return -ENOMEM;
  432. }
  433. pstr->proc_root = entry;
  434. if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
  435. snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
  436. if (snd_info_register(entry) < 0) {
  437. snd_info_free_entry(entry);
  438. entry = NULL;
  439. }
  440. }
  441. pstr->proc_info_entry = entry;
  442. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  443. if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
  444. pstr->proc_root)) != NULL) {
  445. entry->c.text.read = snd_pcm_xrun_debug_read;
  446. entry->c.text.write = snd_pcm_xrun_debug_write;
  447. entry->mode |= S_IWUSR;
  448. entry->private_data = pstr;
  449. if (snd_info_register(entry) < 0) {
  450. snd_info_free_entry(entry);
  451. entry = NULL;
  452. }
  453. }
  454. pstr->proc_xrun_debug_entry = entry;
  455. #endif
  456. return 0;
  457. }
  458. static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
  459. {
  460. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  461. if (pstr->proc_xrun_debug_entry) {
  462. snd_info_unregister(pstr->proc_xrun_debug_entry);
  463. pstr->proc_xrun_debug_entry = NULL;
  464. }
  465. #endif
  466. if (pstr->proc_info_entry) {
  467. snd_info_unregister(pstr->proc_info_entry);
  468. pstr->proc_info_entry = NULL;
  469. }
  470. if (pstr->proc_root) {
  471. snd_info_unregister(pstr->proc_root);
  472. pstr->proc_root = NULL;
  473. }
  474. return 0;
  475. }
  476. static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
  477. {
  478. struct snd_info_entry *entry;
  479. struct snd_card *card;
  480. char name[16];
  481. card = substream->pcm->card;
  482. sprintf(name, "sub%i", substream->number);
  483. if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
  484. return -ENOMEM;
  485. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  486. if (snd_info_register(entry) < 0) {
  487. snd_info_free_entry(entry);
  488. return -ENOMEM;
  489. }
  490. substream->proc_root = entry;
  491. if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
  492. snd_info_set_text_ops(entry, substream,
  493. snd_pcm_substream_proc_info_read);
  494. if (snd_info_register(entry) < 0) {
  495. snd_info_free_entry(entry);
  496. entry = NULL;
  497. }
  498. }
  499. substream->proc_info_entry = entry;
  500. if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
  501. snd_info_set_text_ops(entry, substream,
  502. snd_pcm_substream_proc_hw_params_read);
  503. if (snd_info_register(entry) < 0) {
  504. snd_info_free_entry(entry);
  505. entry = NULL;
  506. }
  507. }
  508. substream->proc_hw_params_entry = entry;
  509. if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
  510. snd_info_set_text_ops(entry, substream,
  511. snd_pcm_substream_proc_sw_params_read);
  512. if (snd_info_register(entry) < 0) {
  513. snd_info_free_entry(entry);
  514. entry = NULL;
  515. }
  516. }
  517. substream->proc_sw_params_entry = entry;
  518. if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
  519. snd_info_set_text_ops(entry, substream,
  520. snd_pcm_substream_proc_status_read);
  521. if (snd_info_register(entry) < 0) {
  522. snd_info_free_entry(entry);
  523. entry = NULL;
  524. }
  525. }
  526. substream->proc_status_entry = entry;
  527. return 0;
  528. }
  529. static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
  530. {
  531. if (substream->proc_info_entry) {
  532. snd_info_unregister(substream->proc_info_entry);
  533. substream->proc_info_entry = NULL;
  534. }
  535. if (substream->proc_hw_params_entry) {
  536. snd_info_unregister(substream->proc_hw_params_entry);
  537. substream->proc_hw_params_entry = NULL;
  538. }
  539. if (substream->proc_sw_params_entry) {
  540. snd_info_unregister(substream->proc_sw_params_entry);
  541. substream->proc_sw_params_entry = NULL;
  542. }
  543. if (substream->proc_status_entry) {
  544. snd_info_unregister(substream->proc_status_entry);
  545. substream->proc_status_entry = NULL;
  546. }
  547. if (substream->proc_root) {
  548. snd_info_unregister(substream->proc_root);
  549. substream->proc_root = NULL;
  550. }
  551. return 0;
  552. }
  553. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  554. static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
  555. static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
  556. static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
  557. static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
  558. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  559. /**
  560. * snd_pcm_new_stream - create a new PCM stream
  561. * @pcm: the pcm instance
  562. * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
  563. * @substream_count: the number of substreams
  564. *
  565. * Creates a new stream for the pcm.
  566. * The corresponding stream on the pcm must have been empty before
  567. * calling this, i.e. zero must be given to the argument of
  568. * snd_pcm_new().
  569. *
  570. * Returns zero if successful, or a negative error code on failure.
  571. */
  572. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
  573. {
  574. int idx, err;
  575. struct snd_pcm_str *pstr = &pcm->streams[stream];
  576. struct snd_pcm_substream *substream, *prev;
  577. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  578. mutex_init(&pstr->oss.setup_mutex);
  579. #endif
  580. pstr->stream = stream;
  581. pstr->pcm = pcm;
  582. pstr->substream_count = substream_count;
  583. if (substream_count > 0) {
  584. err = snd_pcm_stream_proc_init(pstr);
  585. if (err < 0) {
  586. snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
  587. return err;
  588. }
  589. }
  590. prev = NULL;
  591. for (idx = 0, prev = NULL; idx < substream_count; idx++) {
  592. substream = kzalloc(sizeof(*substream), GFP_KERNEL);
  593. if (substream == NULL) {
  594. snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
  595. return -ENOMEM;
  596. }
  597. substream->pcm = pcm;
  598. substream->pstr = pstr;
  599. substream->number = idx;
  600. substream->stream = stream;
  601. sprintf(substream->name, "subdevice #%i", idx);
  602. substream->buffer_bytes_max = UINT_MAX;
  603. if (prev == NULL)
  604. pstr->substream = substream;
  605. else
  606. prev->next = substream;
  607. err = snd_pcm_substream_proc_init(substream);
  608. if (err < 0) {
  609. snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
  610. kfree(substream);
  611. return err;
  612. }
  613. substream->group = &substream->self_group;
  614. spin_lock_init(&substream->self_group.lock);
  615. INIT_LIST_HEAD(&substream->self_group.substreams);
  616. list_add_tail(&substream->link_list, &substream->self_group.substreams);
  617. spin_lock_init(&substream->timer_lock);
  618. prev = substream;
  619. }
  620. return 0;
  621. }
  622. EXPORT_SYMBOL(snd_pcm_new_stream);
  623. /**
  624. * snd_pcm_new - create a new PCM instance
  625. * @card: the card instance
  626. * @id: the id string
  627. * @device: the device index (zero based)
  628. * @playback_count: the number of substreams for playback
  629. * @capture_count: the number of substreams for capture
  630. * @rpcm: the pointer to store the new pcm instance
  631. *
  632. * Creates a new PCM instance.
  633. *
  634. * The pcm operators have to be set afterwards to the new instance
  635. * via snd_pcm_set_ops().
  636. *
  637. * Returns zero if successful, or a negative error code on failure.
  638. */
  639. int snd_pcm_new(struct snd_card *card, char *id, int device,
  640. int playback_count, int capture_count,
  641. struct snd_pcm ** rpcm)
  642. {
  643. struct snd_pcm *pcm;
  644. int err;
  645. static struct snd_device_ops ops = {
  646. .dev_free = snd_pcm_dev_free,
  647. .dev_register = snd_pcm_dev_register,
  648. .dev_disconnect = snd_pcm_dev_disconnect,
  649. .dev_unregister = snd_pcm_dev_unregister
  650. };
  651. snd_assert(rpcm != NULL, return -EINVAL);
  652. *rpcm = NULL;
  653. snd_assert(card != NULL, return -ENXIO);
  654. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  655. if (pcm == NULL) {
  656. snd_printk(KERN_ERR "Cannot allocate PCM\n");
  657. return -ENOMEM;
  658. }
  659. pcm->card = card;
  660. pcm->device = device;
  661. if (id)
  662. strlcpy(pcm->id, id, sizeof(pcm->id));
  663. if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
  664. snd_pcm_free(pcm);
  665. return err;
  666. }
  667. if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
  668. snd_pcm_free(pcm);
  669. return err;
  670. }
  671. mutex_init(&pcm->open_mutex);
  672. init_waitqueue_head(&pcm->open_wait);
  673. if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
  674. snd_pcm_free(pcm);
  675. return err;
  676. }
  677. *rpcm = pcm;
  678. return 0;
  679. }
  680. EXPORT_SYMBOL(snd_pcm_new);
  681. static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
  682. {
  683. struct snd_pcm_substream *substream, *substream_next;
  684. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  685. struct snd_pcm_oss_setup *setup, *setupn;
  686. #endif
  687. substream = pstr->substream;
  688. while (substream) {
  689. substream_next = substream->next;
  690. snd_pcm_substream_proc_done(substream);
  691. kfree(substream);
  692. substream = substream_next;
  693. }
  694. snd_pcm_stream_proc_done(pstr);
  695. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  696. for (setup = pstr->oss.setup_list; setup; setup = setupn) {
  697. setupn = setup->next;
  698. kfree(setup->task_name);
  699. kfree(setup);
  700. }
  701. #endif
  702. }
  703. static int snd_pcm_free(struct snd_pcm *pcm)
  704. {
  705. snd_assert(pcm != NULL, return -ENXIO);
  706. if (pcm->private_free)
  707. pcm->private_free(pcm);
  708. snd_pcm_lib_preallocate_free_for_all(pcm);
  709. snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  710. snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
  711. kfree(pcm);
  712. return 0;
  713. }
  714. static int snd_pcm_dev_free(struct snd_device *device)
  715. {
  716. struct snd_pcm *pcm = device->device_data;
  717. return snd_pcm_free(pcm);
  718. }
  719. static void snd_pcm_tick_timer_func(unsigned long data)
  720. {
  721. struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
  722. snd_pcm_tick_elapsed(substream);
  723. }
  724. int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
  725. struct file *file,
  726. struct snd_pcm_substream **rsubstream)
  727. {
  728. struct snd_pcm_str * pstr;
  729. struct snd_pcm_substream *substream;
  730. struct snd_pcm_runtime *runtime;
  731. struct snd_ctl_file *kctl;
  732. struct snd_card *card;
  733. struct list_head *list;
  734. int prefer_subdevice = -1;
  735. size_t size;
  736. snd_assert(rsubstream != NULL, return -EINVAL);
  737. *rsubstream = NULL;
  738. snd_assert(pcm != NULL, return -ENXIO);
  739. pstr = &pcm->streams[stream];
  740. if (pstr->substream == NULL || pstr->substream_count == 0)
  741. return -ENODEV;
  742. card = pcm->card;
  743. down_read(&card->controls_rwsem);
  744. list_for_each(list, &card->ctl_files) {
  745. kctl = snd_ctl_file(list);
  746. if (kctl->pid == current->pid) {
  747. prefer_subdevice = kctl->prefer_pcm_subdevice;
  748. break;
  749. }
  750. }
  751. up_read(&card->controls_rwsem);
  752. switch (stream) {
  753. case SNDRV_PCM_STREAM_PLAYBACK:
  754. if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
  755. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
  756. if (SUBSTREAM_BUSY(substream))
  757. return -EAGAIN;
  758. }
  759. }
  760. break;
  761. case SNDRV_PCM_STREAM_CAPTURE:
  762. if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
  763. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
  764. if (SUBSTREAM_BUSY(substream))
  765. return -EAGAIN;
  766. }
  767. }
  768. break;
  769. default:
  770. return -EINVAL;
  771. }
  772. if (prefer_subdevice >= 0) {
  773. for (substream = pstr->substream; substream; substream = substream->next)
  774. if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
  775. goto __ok;
  776. }
  777. for (substream = pstr->substream; substream; substream = substream->next)
  778. if (!SUBSTREAM_BUSY(substream))
  779. break;
  780. __ok:
  781. if (substream == NULL)
  782. return -EAGAIN;
  783. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  784. if (runtime == NULL)
  785. return -ENOMEM;
  786. size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
  787. runtime->status = snd_malloc_pages(size, GFP_KERNEL);
  788. if (runtime->status == NULL) {
  789. kfree(runtime);
  790. return -ENOMEM;
  791. }
  792. memset((void*)runtime->status, 0, size);
  793. size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
  794. runtime->control = snd_malloc_pages(size, GFP_KERNEL);
  795. if (runtime->control == NULL) {
  796. snd_free_pages((void*)runtime->status,
  797. PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
  798. kfree(runtime);
  799. return -ENOMEM;
  800. }
  801. memset((void*)runtime->control, 0, size);
  802. init_waitqueue_head(&runtime->sleep);
  803. atomic_set(&runtime->mmap_count, 0);
  804. init_timer(&runtime->tick_timer);
  805. runtime->tick_timer.function = snd_pcm_tick_timer_func;
  806. runtime->tick_timer.data = (unsigned long) substream;
  807. runtime->status->state = SNDRV_PCM_STATE_OPEN;
  808. substream->runtime = runtime;
  809. substream->private_data = pcm->private_data;
  810. substream->ffile = file;
  811. pstr->substream_opened++;
  812. *rsubstream = substream;
  813. return 0;
  814. }
  815. void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
  816. {
  817. struct snd_pcm_runtime *runtime;
  818. substream->file = NULL;
  819. runtime = substream->runtime;
  820. snd_assert(runtime != NULL, return);
  821. if (runtime->private_free != NULL)
  822. runtime->private_free(runtime);
  823. snd_free_pages((void*)runtime->status,
  824. PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
  825. snd_free_pages((void*)runtime->control,
  826. PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
  827. kfree(runtime->hw_constraints.rules);
  828. kfree(runtime);
  829. substream->runtime = NULL;
  830. substream->pstr->substream_opened--;
  831. }
  832. static int snd_pcm_dev_register(struct snd_device *device)
  833. {
  834. int cidx, err;
  835. struct snd_pcm_substream *substream;
  836. struct list_head *list;
  837. char str[16];
  838. struct snd_pcm *pcm = device->device_data;
  839. snd_assert(pcm != NULL && device != NULL, return -ENXIO);
  840. mutex_lock(&register_mutex);
  841. if (snd_pcm_search(pcm->card, pcm->device)) {
  842. mutex_unlock(&register_mutex);
  843. return -EBUSY;
  844. }
  845. list_add_tail(&pcm->list, &snd_pcm_devices);
  846. for (cidx = 0; cidx < 2; cidx++) {
  847. int devtype = -1;
  848. if (pcm->streams[cidx].substream == NULL)
  849. continue;
  850. switch (cidx) {
  851. case SNDRV_PCM_STREAM_PLAYBACK:
  852. sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
  853. devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
  854. break;
  855. case SNDRV_PCM_STREAM_CAPTURE:
  856. sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
  857. devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
  858. break;
  859. }
  860. if ((err = snd_register_device(devtype, pcm->card,
  861. pcm->device,
  862. &snd_pcm_f_ops[cidx],
  863. pcm, str)) < 0)
  864. {
  865. list_del(&pcm->list);
  866. mutex_unlock(&register_mutex);
  867. return err;
  868. }
  869. for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
  870. snd_pcm_timer_init(substream);
  871. }
  872. list_for_each(list, &snd_pcm_notify_list) {
  873. struct snd_pcm_notify *notify;
  874. notify = list_entry(list, struct snd_pcm_notify, list);
  875. notify->n_register(pcm);
  876. }
  877. mutex_unlock(&register_mutex);
  878. return 0;
  879. }
  880. static int snd_pcm_dev_disconnect(struct snd_device *device)
  881. {
  882. struct snd_pcm *pcm = device->device_data;
  883. struct list_head *list;
  884. struct snd_pcm_substream *substream;
  885. int cidx;
  886. mutex_lock(&register_mutex);
  887. list_del_init(&pcm->list);
  888. for (cidx = 0; cidx < 2; cidx++)
  889. for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
  890. if (substream->runtime)
  891. substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
  892. list_for_each(list, &snd_pcm_notify_list) {
  893. struct snd_pcm_notify *notify;
  894. notify = list_entry(list, struct snd_pcm_notify, list);
  895. notify->n_disconnect(pcm);
  896. }
  897. mutex_unlock(&register_mutex);
  898. return 0;
  899. }
  900. static int snd_pcm_dev_unregister(struct snd_device *device)
  901. {
  902. int cidx, devtype;
  903. struct snd_pcm_substream *substream;
  904. struct list_head *list;
  905. struct snd_pcm *pcm = device->device_data;
  906. snd_assert(pcm != NULL, return -ENXIO);
  907. mutex_lock(&register_mutex);
  908. list_del(&pcm->list);
  909. for (cidx = 0; cidx < 2; cidx++) {
  910. devtype = -1;
  911. switch (cidx) {
  912. case SNDRV_PCM_STREAM_PLAYBACK:
  913. devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
  914. break;
  915. case SNDRV_PCM_STREAM_CAPTURE:
  916. devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
  917. break;
  918. }
  919. snd_unregister_device(devtype, pcm->card, pcm->device);
  920. for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
  921. snd_pcm_timer_done(substream);
  922. }
  923. list_for_each(list, &snd_pcm_notify_list) {
  924. struct snd_pcm_notify *notify;
  925. notify = list_entry(list, struct snd_pcm_notify, list);
  926. notify->n_unregister(pcm);
  927. }
  928. mutex_unlock(&register_mutex);
  929. return snd_pcm_free(pcm);
  930. }
  931. int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
  932. {
  933. struct list_head *p;
  934. snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
  935. mutex_lock(&register_mutex);
  936. if (nfree) {
  937. list_del(&notify->list);
  938. list_for_each(p, &snd_pcm_devices)
  939. notify->n_unregister(list_entry(p,
  940. struct snd_pcm, list));
  941. } else {
  942. list_add_tail(&notify->list, &snd_pcm_notify_list);
  943. list_for_each(p, &snd_pcm_devices)
  944. notify->n_register(list_entry(p, struct snd_pcm, list));
  945. }
  946. mutex_unlock(&register_mutex);
  947. return 0;
  948. }
  949. EXPORT_SYMBOL(snd_pcm_notify);
  950. #ifdef CONFIG_PROC_FS
  951. /*
  952. * Info interface
  953. */
  954. static void snd_pcm_proc_read(struct snd_info_entry *entry,
  955. struct snd_info_buffer *buffer)
  956. {
  957. struct list_head *p;
  958. struct snd_pcm *pcm;
  959. mutex_lock(&register_mutex);
  960. list_for_each(p, &snd_pcm_devices) {
  961. pcm = list_entry(p, struct snd_pcm, list);
  962. snd_iprintf(buffer, "%02i-%02i: %s : %s",
  963. pcm->card->number, pcm->device, pcm->id, pcm->name);
  964. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
  965. snd_iprintf(buffer, " : playback %i",
  966. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
  967. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
  968. snd_iprintf(buffer, " : capture %i",
  969. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
  970. snd_iprintf(buffer, "\n");
  971. }
  972. mutex_unlock(&register_mutex);
  973. }
  974. static struct snd_info_entry *snd_pcm_proc_entry = NULL;
  975. static void snd_pcm_proc_init(void)
  976. {
  977. struct snd_info_entry *entry;
  978. if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
  979. snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
  980. if (snd_info_register(entry) < 0) {
  981. snd_info_free_entry(entry);
  982. entry = NULL;
  983. }
  984. }
  985. snd_pcm_proc_entry = entry;
  986. }
  987. static void snd_pcm_proc_done(void)
  988. {
  989. if (snd_pcm_proc_entry)
  990. snd_info_unregister(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)