pcm.c 34 KB

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