pcm.c 34 KB

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