pcm.c 35 KB

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