pcm.c 31 KB

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