pcm.c 38 KB

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