pcm.c 34 KB

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