pcm.c 34 KB

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