pcm.c 35 KB

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