pcm.c 36 KB

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