pcm.c 35 KB

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