pcm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. kfree(runtime->hwptr_log);
  900. #endif
  901. kfree(runtime);
  902. substream->runtime = NULL;
  903. put_pid(substream->pid);
  904. substream->pid = NULL;
  905. substream->pstr->substream_opened--;
  906. }
  907. static ssize_t show_pcm_class(struct device *dev,
  908. struct device_attribute *attr, char *buf)
  909. {
  910. struct snd_pcm *pcm;
  911. const char *str;
  912. static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
  913. [SNDRV_PCM_CLASS_GENERIC] = "generic",
  914. [SNDRV_PCM_CLASS_MULTI] = "multi",
  915. [SNDRV_PCM_CLASS_MODEM] = "modem",
  916. [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
  917. };
  918. if (! (pcm = dev_get_drvdata(dev)) ||
  919. pcm->dev_class > SNDRV_PCM_CLASS_LAST)
  920. str = "none";
  921. else
  922. str = strs[pcm->dev_class];
  923. return snprintf(buf, PAGE_SIZE, "%s\n", str);
  924. }
  925. static struct device_attribute pcm_attrs =
  926. __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
  927. static int snd_pcm_dev_register(struct snd_device *device)
  928. {
  929. int cidx, err;
  930. struct snd_pcm_substream *substream;
  931. struct snd_pcm_notify *notify;
  932. char str[16];
  933. struct snd_pcm *pcm;
  934. struct device *dev;
  935. if (snd_BUG_ON(!device || !device->device_data))
  936. return -ENXIO;
  937. pcm = device->device_data;
  938. mutex_lock(&register_mutex);
  939. err = snd_pcm_add(pcm);
  940. if (err) {
  941. mutex_unlock(&register_mutex);
  942. return err;
  943. }
  944. for (cidx = 0; cidx < 2; cidx++) {
  945. int devtype = -1;
  946. if (pcm->streams[cidx].substream == NULL || pcm->internal)
  947. continue;
  948. switch (cidx) {
  949. case SNDRV_PCM_STREAM_PLAYBACK:
  950. sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
  951. devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
  952. break;
  953. case SNDRV_PCM_STREAM_CAPTURE:
  954. sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
  955. devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
  956. break;
  957. }
  958. /* device pointer to use, pcm->dev takes precedence if
  959. * it is assigned, otherwise fall back to card's device
  960. * if possible */
  961. dev = pcm->dev;
  962. if (!dev)
  963. dev = snd_card_get_device_link(pcm->card);
  964. /* register pcm */
  965. err = snd_register_device_for_dev(devtype, pcm->card,
  966. pcm->device,
  967. &snd_pcm_f_ops[cidx],
  968. pcm, str, dev);
  969. if (err < 0) {
  970. list_del(&pcm->list);
  971. mutex_unlock(&register_mutex);
  972. return err;
  973. }
  974. snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
  975. &pcm_attrs);
  976. for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
  977. snd_pcm_timer_init(substream);
  978. }
  979. list_for_each_entry(notify, &snd_pcm_notify_list, list)
  980. notify->n_register(pcm);
  981. mutex_unlock(&register_mutex);
  982. return 0;
  983. }
  984. static int snd_pcm_dev_disconnect(struct snd_device *device)
  985. {
  986. struct snd_pcm *pcm = device->device_data;
  987. struct snd_pcm_notify *notify;
  988. struct snd_pcm_substream *substream;
  989. int cidx, devtype;
  990. mutex_lock(&register_mutex);
  991. if (list_empty(&pcm->list))
  992. goto unlock;
  993. mutex_lock(&pcm->open_mutex);
  994. wake_up(&pcm->open_wait);
  995. list_del_init(&pcm->list);
  996. for (cidx = 0; cidx < 2; cidx++)
  997. for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
  998. snd_pcm_stream_lock_irq(substream);
  999. if (substream->runtime) {
  1000. substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
  1001. wake_up(&substream->runtime->sleep);
  1002. wake_up(&substream->runtime->tsleep);
  1003. }
  1004. snd_pcm_stream_unlock_irq(substream);
  1005. }
  1006. list_for_each_entry(notify, &snd_pcm_notify_list, list) {
  1007. notify->n_disconnect(pcm);
  1008. }
  1009. for (cidx = 0; cidx < 2; cidx++) {
  1010. devtype = -1;
  1011. switch (cidx) {
  1012. case SNDRV_PCM_STREAM_PLAYBACK:
  1013. devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
  1014. break;
  1015. case SNDRV_PCM_STREAM_CAPTURE:
  1016. devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
  1017. break;
  1018. }
  1019. snd_unregister_device(devtype, pcm->card, pcm->device);
  1020. if (pcm->streams[cidx].chmap_kctl) {
  1021. snd_ctl_remove(pcm->card, pcm->streams[cidx].chmap_kctl);
  1022. pcm->streams[cidx].chmap_kctl = NULL;
  1023. }
  1024. }
  1025. mutex_unlock(&pcm->open_mutex);
  1026. unlock:
  1027. mutex_unlock(&register_mutex);
  1028. return 0;
  1029. }
  1030. int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
  1031. {
  1032. struct snd_pcm *pcm;
  1033. if (snd_BUG_ON(!notify ||
  1034. !notify->n_register ||
  1035. !notify->n_unregister ||
  1036. !notify->n_disconnect))
  1037. return -EINVAL;
  1038. mutex_lock(&register_mutex);
  1039. if (nfree) {
  1040. list_del(&notify->list);
  1041. list_for_each_entry(pcm, &snd_pcm_devices, list)
  1042. notify->n_unregister(pcm);
  1043. } else {
  1044. list_add_tail(&notify->list, &snd_pcm_notify_list);
  1045. list_for_each_entry(pcm, &snd_pcm_devices, list)
  1046. notify->n_register(pcm);
  1047. }
  1048. mutex_unlock(&register_mutex);
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL(snd_pcm_notify);
  1052. #ifdef CONFIG_PROC_FS
  1053. /*
  1054. * Info interface
  1055. */
  1056. static void snd_pcm_proc_read(struct snd_info_entry *entry,
  1057. struct snd_info_buffer *buffer)
  1058. {
  1059. struct snd_pcm *pcm;
  1060. mutex_lock(&register_mutex);
  1061. list_for_each_entry(pcm, &snd_pcm_devices, list) {
  1062. snd_iprintf(buffer, "%02i-%02i: %s : %s",
  1063. pcm->card->number, pcm->device, pcm->id, pcm->name);
  1064. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
  1065. snd_iprintf(buffer, " : playback %i",
  1066. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
  1067. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
  1068. snd_iprintf(buffer, " : capture %i",
  1069. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
  1070. snd_iprintf(buffer, "\n");
  1071. }
  1072. mutex_unlock(&register_mutex);
  1073. }
  1074. static struct snd_info_entry *snd_pcm_proc_entry;
  1075. static void snd_pcm_proc_init(void)
  1076. {
  1077. struct snd_info_entry *entry;
  1078. if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
  1079. snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
  1080. if (snd_info_register(entry) < 0) {
  1081. snd_info_free_entry(entry);
  1082. entry = NULL;
  1083. }
  1084. }
  1085. snd_pcm_proc_entry = entry;
  1086. }
  1087. static void snd_pcm_proc_done(void)
  1088. {
  1089. snd_info_free_entry(snd_pcm_proc_entry);
  1090. }
  1091. #else /* !CONFIG_PROC_FS */
  1092. #define snd_pcm_proc_init()
  1093. #define snd_pcm_proc_done()
  1094. #endif /* CONFIG_PROC_FS */
  1095. /*
  1096. * ENTRY functions
  1097. */
  1098. static int __init alsa_pcm_init(void)
  1099. {
  1100. snd_ctl_register_ioctl(snd_pcm_control_ioctl);
  1101. snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
  1102. snd_pcm_proc_init();
  1103. return 0;
  1104. }
  1105. static void __exit alsa_pcm_exit(void)
  1106. {
  1107. snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
  1108. snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
  1109. snd_pcm_proc_done();
  1110. }
  1111. module_init(alsa_pcm_init)
  1112. module_exit(alsa_pcm_exit)