pcm.c 34 KB

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