aloop.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * Loopback soundcard
  3. *
  4. * Original code:
  5. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  6. *
  7. * More accurate positioning and full-duplex support:
  8. * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
  9. *
  10. * Major (almost complete) rewrite:
  11. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  12. *
  13. * A next major update in 2010 (separate timers for playback and capture):
  14. * Copyright (c) Jaroslav Kysela <perex@perex.cz>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. */
  31. #include <linux/init.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/slab.h>
  34. #include <linux/time.h>
  35. #include <linux/wait.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/platform_device.h>
  38. #include <sound/core.h>
  39. #include <sound/control.h>
  40. #include <sound/pcm.h>
  41. #include <sound/info.h>
  42. #include <sound/initval.h>
  43. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  44. MODULE_DESCRIPTION("A loopback soundcard");
  45. MODULE_LICENSE("GPL");
  46. MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
  47. #define MAX_PCM_SUBSTREAMS 8
  48. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  49. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  50. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  51. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  52. static int pcm_notify[SNDRV_CARDS];
  53. module_param_array(index, int, NULL, 0444);
  54. MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
  55. module_param_array(id, charp, NULL, 0444);
  56. MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
  57. module_param_array(enable, bool, NULL, 0444);
  58. MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
  59. module_param_array(pcm_substreams, int, NULL, 0444);
  60. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
  61. module_param_array(pcm_notify, int, NULL, 0444);
  62. MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
  63. #define NO_PITCH 100000
  64. struct loopback_pcm;
  65. struct loopback_cable {
  66. spinlock_t lock;
  67. struct loopback_pcm *streams[2];
  68. struct snd_pcm_hardware hw;
  69. /* flags */
  70. unsigned int valid;
  71. unsigned int running;
  72. unsigned int pause;
  73. };
  74. struct loopback_setup {
  75. unsigned int notify: 1;
  76. unsigned int rate_shift;
  77. unsigned int format;
  78. unsigned int rate;
  79. unsigned int channels;
  80. struct snd_ctl_elem_id active_id;
  81. struct snd_ctl_elem_id format_id;
  82. struct snd_ctl_elem_id rate_id;
  83. struct snd_ctl_elem_id channels_id;
  84. };
  85. struct loopback {
  86. struct snd_card *card;
  87. struct mutex cable_lock;
  88. struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
  89. struct snd_pcm *pcm[2];
  90. struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
  91. };
  92. struct loopback_pcm {
  93. struct loopback *loopback;
  94. struct snd_pcm_substream *substream;
  95. struct loopback_cable *cable;
  96. unsigned int pcm_buffer_size;
  97. unsigned int buf_pos; /* position in buffer */
  98. unsigned int silent_size;
  99. /* PCM parameters */
  100. unsigned int pcm_period_size;
  101. unsigned int pcm_bps; /* bytes per second */
  102. unsigned int pcm_salign; /* bytes per sample * channels */
  103. unsigned int pcm_rate_shift; /* rate shift value */
  104. /* flags */
  105. unsigned int period_update_pending :1;
  106. /* timer stuff */
  107. unsigned int irq_pos; /* fractional IRQ position */
  108. unsigned int period_size_frac;
  109. unsigned long last_jiffies;
  110. struct timer_list timer;
  111. };
  112. static struct platform_device *devices[SNDRV_CARDS];
  113. static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
  114. {
  115. if (dpcm->pcm_rate_shift == NO_PITCH) {
  116. x /= HZ;
  117. } else {
  118. x = div_u64(NO_PITCH * (unsigned long long)x,
  119. HZ * (unsigned long long)dpcm->pcm_rate_shift);
  120. }
  121. return x - (x % dpcm->pcm_salign);
  122. }
  123. static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
  124. {
  125. if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
  126. return x * HZ;
  127. } else {
  128. x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
  129. NO_PITCH);
  130. }
  131. return x;
  132. }
  133. static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
  134. {
  135. int device = dpcm->substream->pstr->pcm->device;
  136. if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  137. device ^= 1;
  138. return &dpcm->loopback->setup[dpcm->substream->number][device];
  139. }
  140. static inline unsigned int get_notify(struct loopback_pcm *dpcm)
  141. {
  142. return get_setup(dpcm)->notify;
  143. }
  144. static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
  145. {
  146. return get_setup(dpcm)->rate_shift;
  147. }
  148. static void loopback_timer_start(struct loopback_pcm *dpcm)
  149. {
  150. unsigned long tick;
  151. unsigned int rate_shift = get_rate_shift(dpcm);
  152. if (rate_shift != dpcm->pcm_rate_shift) {
  153. dpcm->pcm_rate_shift = rate_shift;
  154. dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
  155. }
  156. if (dpcm->period_size_frac <= dpcm->irq_pos) {
  157. dpcm->irq_pos %= dpcm->period_size_frac;
  158. dpcm->period_update_pending = 1;
  159. }
  160. tick = dpcm->period_size_frac - dpcm->irq_pos;
  161. tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
  162. dpcm->timer.expires = jiffies + tick;
  163. add_timer(&dpcm->timer);
  164. }
  165. static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
  166. {
  167. del_timer(&dpcm->timer);
  168. dpcm->timer.expires = 0;
  169. }
  170. #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
  171. #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
  172. #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
  173. static int loopback_check_format(struct loopback_cable *cable, int stream)
  174. {
  175. struct snd_pcm_runtime *runtime, *cruntime;
  176. struct loopback_setup *setup;
  177. struct snd_card *card;
  178. int check;
  179. if (cable->valid != CABLE_VALID_BOTH) {
  180. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  181. goto __notify;
  182. return 0;
  183. }
  184. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  185. substream->runtime;
  186. cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  187. substream->runtime;
  188. check = runtime->format != cruntime->format ||
  189. runtime->rate != cruntime->rate ||
  190. runtime->channels != cruntime->channels;
  191. if (!check)
  192. return 0;
  193. if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  194. return -EIO;
  195. } else {
  196. snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  197. substream, SNDRV_PCM_STATE_DRAINING);
  198. __notify:
  199. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  200. substream->runtime;
  201. setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  202. card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
  203. if (setup->format != runtime->format) {
  204. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  205. &setup->format_id);
  206. setup->format = runtime->format;
  207. }
  208. if (setup->rate != runtime->rate) {
  209. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  210. &setup->rate_id);
  211. setup->rate = runtime->rate;
  212. }
  213. if (setup->channels != runtime->channels) {
  214. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  215. &setup->channels_id);
  216. setup->channels = runtime->channels;
  217. }
  218. }
  219. return 0;
  220. }
  221. static void loopback_active_notify(struct loopback_pcm *dpcm)
  222. {
  223. snd_ctl_notify(dpcm->loopback->card,
  224. SNDRV_CTL_EVENT_MASK_VALUE,
  225. &get_setup(dpcm)->active_id);
  226. }
  227. static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
  228. {
  229. struct snd_pcm_runtime *runtime = substream->runtime;
  230. struct loopback_pcm *dpcm = runtime->private_data;
  231. struct loopback_cable *cable = dpcm->cable;
  232. int err, stream = 1 << substream->stream;
  233. switch (cmd) {
  234. case SNDRV_PCM_TRIGGER_START:
  235. err = loopback_check_format(cable, substream->stream);
  236. if (err < 0)
  237. return err;
  238. dpcm->last_jiffies = jiffies;
  239. dpcm->pcm_rate_shift = 0;
  240. spin_lock(&cable->lock);
  241. cable->running |= stream;
  242. cable->pause &= ~stream;
  243. spin_unlock(&cable->lock);
  244. loopback_timer_start(dpcm);
  245. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  246. loopback_active_notify(dpcm);
  247. break;
  248. case SNDRV_PCM_TRIGGER_STOP:
  249. spin_lock(&cable->lock);
  250. cable->running &= ~stream;
  251. cable->pause &= ~stream;
  252. spin_unlock(&cable->lock);
  253. loopback_timer_stop(dpcm);
  254. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  255. loopback_active_notify(dpcm);
  256. break;
  257. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  258. spin_lock(&cable->lock);
  259. cable->pause |= stream;
  260. spin_unlock(&cable->lock);
  261. loopback_timer_stop(dpcm);
  262. break;
  263. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  264. spin_lock(&cable->lock);
  265. dpcm->last_jiffies = jiffies;
  266. cable->pause &= ~stream;
  267. spin_unlock(&cable->lock);
  268. loopback_timer_start(dpcm);
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. return 0;
  274. }
  275. static void params_change_substream(struct loopback_pcm *dpcm,
  276. struct snd_pcm_runtime *runtime)
  277. {
  278. struct snd_pcm_runtime *dst_runtime;
  279. if (dpcm == NULL || dpcm->substream == NULL)
  280. return;
  281. dst_runtime = dpcm->substream->runtime;
  282. if (dst_runtime == NULL)
  283. return;
  284. dst_runtime->hw = dpcm->cable->hw;
  285. }
  286. static void params_change(struct snd_pcm_substream *substream)
  287. {
  288. struct snd_pcm_runtime *runtime = substream->runtime;
  289. struct loopback_pcm *dpcm = runtime->private_data;
  290. struct loopback_cable *cable = dpcm->cable;
  291. cable->hw.formats = (1ULL << runtime->format);
  292. cable->hw.rate_min = runtime->rate;
  293. cable->hw.rate_max = runtime->rate;
  294. cable->hw.channels_min = runtime->channels;
  295. cable->hw.channels_max = runtime->channels;
  296. params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  297. runtime);
  298. params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  299. runtime);
  300. }
  301. static int loopback_prepare(struct snd_pcm_substream *substream)
  302. {
  303. struct snd_pcm_runtime *runtime = substream->runtime;
  304. struct loopback_pcm *dpcm = runtime->private_data;
  305. struct loopback_cable *cable = dpcm->cable;
  306. int bps, salign;
  307. salign = (snd_pcm_format_width(runtime->format) *
  308. runtime->channels) / 8;
  309. bps = salign * runtime->rate;
  310. if (bps <= 0 || salign <= 0)
  311. return -EINVAL;
  312. dpcm->buf_pos = 0;
  313. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  314. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  315. /* clear capture buffer */
  316. dpcm->silent_size = dpcm->pcm_buffer_size;
  317. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  318. runtime->buffer_size * runtime->channels);
  319. }
  320. dpcm->irq_pos = 0;
  321. dpcm->period_update_pending = 0;
  322. dpcm->pcm_bps = bps;
  323. dpcm->pcm_salign = salign;
  324. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  325. mutex_lock(&dpcm->loopback->cable_lock);
  326. if (!(cable->valid & ~(1 << substream->stream)) ||
  327. (get_setup(dpcm)->notify &&
  328. substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  329. params_change(substream);
  330. cable->valid |= 1 << substream->stream;
  331. mutex_unlock(&dpcm->loopback->cable_lock);
  332. return 0;
  333. }
  334. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  335. {
  336. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  337. char *dst = runtime->dma_area;
  338. unsigned int dst_off = dpcm->buf_pos;
  339. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  340. return;
  341. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  342. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  343. for (;;) {
  344. unsigned int size = bytes;
  345. if (dst_off + size > dpcm->pcm_buffer_size)
  346. size = dpcm->pcm_buffer_size - dst_off;
  347. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  348. bytes_to_frames(runtime, size) *
  349. runtime->channels);
  350. dpcm->silent_size += size;
  351. bytes -= size;
  352. if (!bytes)
  353. break;
  354. dst_off = 0;
  355. }
  356. }
  357. static void copy_play_buf(struct loopback_pcm *play,
  358. struct loopback_pcm *capt,
  359. unsigned int bytes)
  360. {
  361. struct snd_pcm_runtime *runtime = play->substream->runtime;
  362. char *src = runtime->dma_area;
  363. char *dst = capt->substream->runtime->dma_area;
  364. unsigned int src_off = play->buf_pos;
  365. unsigned int dst_off = capt->buf_pos;
  366. unsigned int clear_bytes = 0;
  367. /* check if playback is draining, trim the capture copy size
  368. * when our pointer is at the end of playback ring buffer */
  369. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  370. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  371. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  372. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  373. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  374. appl_ptr1 += play->buf_pos / play->pcm_salign;
  375. if (appl_ptr < appl_ptr1)
  376. appl_ptr1 -= runtime->buffer_size;
  377. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  378. if (diff < bytes) {
  379. clear_bytes = bytes - diff;
  380. bytes = diff;
  381. }
  382. }
  383. for (;;) {
  384. unsigned int size = bytes;
  385. if (src_off + size > play->pcm_buffer_size)
  386. size = play->pcm_buffer_size - src_off;
  387. if (dst_off + size > capt->pcm_buffer_size)
  388. size = capt->pcm_buffer_size - dst_off;
  389. memcpy(dst + dst_off, src + src_off, size);
  390. capt->silent_size = 0;
  391. bytes -= size;
  392. if (!bytes)
  393. break;
  394. src_off = (src_off + size) % play->pcm_buffer_size;
  395. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  396. }
  397. if (clear_bytes > 0) {
  398. clear_capture_buf(capt, clear_bytes);
  399. capt->silent_size = 0;
  400. }
  401. }
  402. #define BYTEPOS_UPDATE_POSONLY 0
  403. #define BYTEPOS_UPDATE_CLEAR 1
  404. #define BYTEPOS_UPDATE_COPY 2
  405. static void loopback_bytepos_update(struct loopback_pcm *dpcm,
  406. unsigned int delta,
  407. unsigned int cmd)
  408. {
  409. unsigned int count;
  410. unsigned long last_pos;
  411. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  412. dpcm->irq_pos += delta * dpcm->pcm_bps;
  413. count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  414. if (!count)
  415. return;
  416. if (cmd == BYTEPOS_UPDATE_CLEAR)
  417. clear_capture_buf(dpcm, count);
  418. else if (cmd == BYTEPOS_UPDATE_COPY)
  419. copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  420. dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  421. count);
  422. dpcm->buf_pos += count;
  423. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  424. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  425. dpcm->irq_pos %= dpcm->period_size_frac;
  426. dpcm->period_update_pending = 1;
  427. }
  428. }
  429. static unsigned int loopback_pos_update(struct loopback_cable *cable)
  430. {
  431. struct loopback_pcm *dpcm_play =
  432. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  433. struct loopback_pcm *dpcm_capt =
  434. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  435. unsigned long delta_play = 0, delta_capt = 0;
  436. unsigned int running;
  437. unsigned long flags;
  438. spin_lock_irqsave(&cable->lock, flags);
  439. running = cable->running ^ cable->pause;
  440. if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  441. delta_play = jiffies - dpcm_play->last_jiffies;
  442. dpcm_play->last_jiffies += delta_play;
  443. }
  444. if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  445. delta_capt = jiffies - dpcm_capt->last_jiffies;
  446. dpcm_capt->last_jiffies += delta_capt;
  447. }
  448. if (delta_play == 0 && delta_capt == 0)
  449. goto unlock;
  450. if (delta_play > delta_capt) {
  451. loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
  452. BYTEPOS_UPDATE_POSONLY);
  453. delta_play = delta_capt;
  454. } else if (delta_play < delta_capt) {
  455. loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
  456. BYTEPOS_UPDATE_CLEAR);
  457. delta_capt = delta_play;
  458. }
  459. if (delta_play == 0 && delta_capt == 0)
  460. goto unlock;
  461. /* note delta_capt == delta_play at this moment */
  462. loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
  463. loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
  464. unlock:
  465. spin_unlock_irqrestore(&cable->lock, flags);
  466. return running;
  467. }
  468. static void loopback_timer_function(unsigned long data)
  469. {
  470. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  471. unsigned int running;
  472. running = loopback_pos_update(dpcm->cable);
  473. if (running & (1 << dpcm->substream->stream)) {
  474. loopback_timer_start(dpcm);
  475. if (dpcm->period_update_pending) {
  476. dpcm->period_update_pending = 0;
  477. snd_pcm_period_elapsed(dpcm->substream);
  478. }
  479. }
  480. }
  481. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  482. {
  483. struct snd_pcm_runtime *runtime = substream->runtime;
  484. struct loopback_pcm *dpcm = runtime->private_data;
  485. loopback_pos_update(dpcm->cable);
  486. return bytes_to_frames(runtime, dpcm->buf_pos);
  487. }
  488. static struct snd_pcm_hardware loopback_pcm_hardware =
  489. {
  490. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  491. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  492. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  493. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  494. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
  495. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  496. .rate_min = 8000,
  497. .rate_max = 192000,
  498. .channels_min = 1,
  499. .channels_max = 32,
  500. .buffer_bytes_max = 2 * 1024 * 1024,
  501. .period_bytes_min = 64,
  502. /* note check overflow in frac_pos() using pcm_rate_shift before
  503. changing period_bytes_max value */
  504. .period_bytes_max = 1024 * 1024,
  505. .periods_min = 1,
  506. .periods_max = 1024,
  507. .fifo_size = 0,
  508. };
  509. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  510. {
  511. struct loopback_pcm *dpcm = runtime->private_data;
  512. kfree(dpcm);
  513. }
  514. static int loopback_hw_params(struct snd_pcm_substream *substream,
  515. struct snd_pcm_hw_params *params)
  516. {
  517. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  518. }
  519. static int loopback_hw_free(struct snd_pcm_substream *substream)
  520. {
  521. struct snd_pcm_runtime *runtime = substream->runtime;
  522. struct loopback_pcm *dpcm = runtime->private_data;
  523. struct loopback_cable *cable = dpcm->cable;
  524. mutex_lock(&dpcm->loopback->cable_lock);
  525. cable->valid &= ~(1 << substream->stream);
  526. mutex_unlock(&dpcm->loopback->cable_lock);
  527. return snd_pcm_lib_free_pages(substream);
  528. }
  529. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  530. {
  531. if (!substream->pcm->device)
  532. return substream->stream;
  533. else
  534. return !substream->stream;
  535. }
  536. static int rule_format(struct snd_pcm_hw_params *params,
  537. struct snd_pcm_hw_rule *rule)
  538. {
  539. struct snd_pcm_hardware *hw = rule->private;
  540. struct snd_mask *maskp = hw_param_mask(params, rule->var);
  541. maskp->bits[0] &= (u_int32_t)hw->formats;
  542. maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
  543. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  544. if (! maskp->bits[0] && ! maskp->bits[1])
  545. return -EINVAL;
  546. return 0;
  547. }
  548. static int rule_rate(struct snd_pcm_hw_params *params,
  549. struct snd_pcm_hw_rule *rule)
  550. {
  551. struct snd_pcm_hardware *hw = rule->private;
  552. struct snd_interval t;
  553. t.min = hw->rate_min;
  554. t.max = hw->rate_max;
  555. t.openmin = t.openmax = 0;
  556. t.integer = 0;
  557. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  558. }
  559. static int rule_channels(struct snd_pcm_hw_params *params,
  560. struct snd_pcm_hw_rule *rule)
  561. {
  562. struct snd_pcm_hardware *hw = rule->private;
  563. struct snd_interval t;
  564. t.min = hw->channels_min;
  565. t.max = hw->channels_max;
  566. t.openmin = t.openmax = 0;
  567. t.integer = 0;
  568. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  569. }
  570. static int loopback_open(struct snd_pcm_substream *substream)
  571. {
  572. struct snd_pcm_runtime *runtime = substream->runtime;
  573. struct loopback *loopback = substream->private_data;
  574. struct loopback_pcm *dpcm;
  575. struct loopback_cable *cable;
  576. int err = 0;
  577. int dev = get_cable_index(substream);
  578. mutex_lock(&loopback->cable_lock);
  579. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  580. if (!dpcm) {
  581. err = -ENOMEM;
  582. goto unlock;
  583. }
  584. dpcm->loopback = loopback;
  585. dpcm->substream = substream;
  586. setup_timer(&dpcm->timer, loopback_timer_function,
  587. (unsigned long)dpcm);
  588. cable = loopback->cables[substream->number][dev];
  589. if (!cable) {
  590. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  591. if (!cable) {
  592. kfree(dpcm);
  593. err = -ENOMEM;
  594. goto unlock;
  595. }
  596. spin_lock_init(&cable->lock);
  597. cable->hw = loopback_pcm_hardware;
  598. loopback->cables[substream->number][dev] = cable;
  599. }
  600. dpcm->cable = cable;
  601. cable->streams[substream->stream] = dpcm;
  602. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  603. /* use dynamic rules based on actual runtime->hw values */
  604. /* note that the default rules created in the PCM midlevel code */
  605. /* are cached -> they do not reflect the actual state */
  606. err = snd_pcm_hw_rule_add(runtime, 0,
  607. SNDRV_PCM_HW_PARAM_FORMAT,
  608. rule_format, &runtime->hw,
  609. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  610. if (err < 0)
  611. goto unlock;
  612. err = snd_pcm_hw_rule_add(runtime, 0,
  613. SNDRV_PCM_HW_PARAM_RATE,
  614. rule_rate, &runtime->hw,
  615. SNDRV_PCM_HW_PARAM_RATE, -1);
  616. if (err < 0)
  617. goto unlock;
  618. err = snd_pcm_hw_rule_add(runtime, 0,
  619. SNDRV_PCM_HW_PARAM_CHANNELS,
  620. rule_channels, &runtime->hw,
  621. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  622. if (err < 0)
  623. goto unlock;
  624. runtime->private_data = dpcm;
  625. runtime->private_free = loopback_runtime_free;
  626. if (get_notify(dpcm))
  627. runtime->hw = loopback_pcm_hardware;
  628. else
  629. runtime->hw = cable->hw;
  630. unlock:
  631. mutex_unlock(&loopback->cable_lock);
  632. return err;
  633. }
  634. static int loopback_close(struct snd_pcm_substream *substream)
  635. {
  636. struct loopback *loopback = substream->private_data;
  637. struct loopback_pcm *dpcm = substream->runtime->private_data;
  638. struct loopback_cable *cable;
  639. int dev = get_cable_index(substream);
  640. loopback_timer_stop(dpcm);
  641. mutex_lock(&loopback->cable_lock);
  642. cable = loopback->cables[substream->number][dev];
  643. if (cable->streams[!substream->stream]) {
  644. /* other stream is still alive */
  645. cable->streams[substream->stream] = NULL;
  646. } else {
  647. /* free the cable */
  648. loopback->cables[substream->number][dev] = NULL;
  649. kfree(cable);
  650. }
  651. mutex_unlock(&loopback->cable_lock);
  652. return 0;
  653. }
  654. static struct snd_pcm_ops loopback_playback_ops = {
  655. .open = loopback_open,
  656. .close = loopback_close,
  657. .ioctl = snd_pcm_lib_ioctl,
  658. .hw_params = loopback_hw_params,
  659. .hw_free = loopback_hw_free,
  660. .prepare = loopback_prepare,
  661. .trigger = loopback_trigger,
  662. .pointer = loopback_pointer,
  663. };
  664. static struct snd_pcm_ops loopback_capture_ops = {
  665. .open = loopback_open,
  666. .close = loopback_close,
  667. .ioctl = snd_pcm_lib_ioctl,
  668. .hw_params = loopback_hw_params,
  669. .hw_free = loopback_hw_free,
  670. .prepare = loopback_prepare,
  671. .trigger = loopback_trigger,
  672. .pointer = loopback_pointer,
  673. };
  674. static int __devinit loopback_pcm_new(struct loopback *loopback,
  675. int device, int substreams)
  676. {
  677. struct snd_pcm *pcm;
  678. int err;
  679. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  680. substreams, substreams, &pcm);
  681. if (err < 0)
  682. return err;
  683. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  684. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  685. pcm->private_data = loopback;
  686. pcm->info_flags = 0;
  687. strcpy(pcm->name, "Loopback PCM");
  688. loopback->pcm[device] = pcm;
  689. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  690. snd_dma_continuous_data(GFP_KERNEL),
  691. 0, 2 * 1024 * 1024);
  692. return 0;
  693. }
  694. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  695. struct snd_ctl_elem_info *uinfo)
  696. {
  697. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  698. uinfo->count = 1;
  699. uinfo->value.integer.min = 80000;
  700. uinfo->value.integer.max = 120000;
  701. uinfo->value.integer.step = 1;
  702. return 0;
  703. }
  704. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  705. struct snd_ctl_elem_value *ucontrol)
  706. {
  707. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  708. ucontrol->value.integer.value[0] =
  709. loopback->setup[kcontrol->id.subdevice]
  710. [kcontrol->id.device].rate_shift;
  711. return 0;
  712. }
  713. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  714. struct snd_ctl_elem_value *ucontrol)
  715. {
  716. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  717. unsigned int val;
  718. int change = 0;
  719. val = ucontrol->value.integer.value[0];
  720. if (val < 80000)
  721. val = 80000;
  722. if (val > 120000)
  723. val = 120000;
  724. mutex_lock(&loopback->cable_lock);
  725. if (val != loopback->setup[kcontrol->id.subdevice]
  726. [kcontrol->id.device].rate_shift) {
  727. loopback->setup[kcontrol->id.subdevice]
  728. [kcontrol->id.device].rate_shift = val;
  729. change = 1;
  730. }
  731. mutex_unlock(&loopback->cable_lock);
  732. return change;
  733. }
  734. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  735. struct snd_ctl_elem_value *ucontrol)
  736. {
  737. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  738. ucontrol->value.integer.value[0] =
  739. loopback->setup[kcontrol->id.subdevice]
  740. [kcontrol->id.device].notify;
  741. return 0;
  742. }
  743. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  744. struct snd_ctl_elem_value *ucontrol)
  745. {
  746. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  747. unsigned int val;
  748. int change = 0;
  749. val = ucontrol->value.integer.value[0] ? 1 : 0;
  750. if (val != loopback->setup[kcontrol->id.subdevice]
  751. [kcontrol->id.device].notify) {
  752. loopback->setup[kcontrol->id.subdevice]
  753. [kcontrol->id.device].notify = val;
  754. change = 1;
  755. }
  756. return change;
  757. }
  758. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  759. struct snd_ctl_elem_value *ucontrol)
  760. {
  761. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  762. struct loopback_cable *cable = loopback->cables
  763. [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  764. unsigned int val = 0;
  765. if (cable != NULL)
  766. val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  767. 1 : 0;
  768. ucontrol->value.integer.value[0] = val;
  769. return 0;
  770. }
  771. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  772. struct snd_ctl_elem_info *uinfo)
  773. {
  774. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  775. uinfo->count = 1;
  776. uinfo->value.integer.min = 0;
  777. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  778. uinfo->value.integer.step = 1;
  779. return 0;
  780. }
  781. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  782. struct snd_ctl_elem_value *ucontrol)
  783. {
  784. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  785. ucontrol->value.integer.value[0] =
  786. loopback->setup[kcontrol->id.subdevice]
  787. [kcontrol->id.device].format;
  788. return 0;
  789. }
  790. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  791. struct snd_ctl_elem_info *uinfo)
  792. {
  793. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  794. uinfo->count = 1;
  795. uinfo->value.integer.min = 0;
  796. uinfo->value.integer.max = 192000;
  797. uinfo->value.integer.step = 1;
  798. return 0;
  799. }
  800. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  801. struct snd_ctl_elem_value *ucontrol)
  802. {
  803. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  804. ucontrol->value.integer.value[0] =
  805. loopback->setup[kcontrol->id.subdevice]
  806. [kcontrol->id.device].rate;
  807. return 0;
  808. }
  809. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  810. struct snd_ctl_elem_info *uinfo)
  811. {
  812. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  813. uinfo->count = 1;
  814. uinfo->value.integer.min = 1;
  815. uinfo->value.integer.max = 1024;
  816. uinfo->value.integer.step = 1;
  817. return 0;
  818. }
  819. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  820. struct snd_ctl_elem_value *ucontrol)
  821. {
  822. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  823. ucontrol->value.integer.value[0] =
  824. loopback->setup[kcontrol->id.subdevice]
  825. [kcontrol->id.device].channels;
  826. return 0;
  827. }
  828. static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
  829. {
  830. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  831. .name = "PCM Rate Shift 100000",
  832. .info = loopback_rate_shift_info,
  833. .get = loopback_rate_shift_get,
  834. .put = loopback_rate_shift_put,
  835. },
  836. {
  837. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  838. .name = "PCM Notify",
  839. .info = snd_ctl_boolean_mono_info,
  840. .get = loopback_notify_get,
  841. .put = loopback_notify_put,
  842. },
  843. #define ACTIVE_IDX 2
  844. {
  845. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  846. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  847. .name = "PCM Slave Active",
  848. .info = snd_ctl_boolean_mono_info,
  849. .get = loopback_active_get,
  850. },
  851. #define FORMAT_IDX 3
  852. {
  853. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  854. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  855. .name = "PCM Slave Format",
  856. .info = loopback_format_info,
  857. .get = loopback_format_get
  858. },
  859. #define RATE_IDX 4
  860. {
  861. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  862. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  863. .name = "PCM Slave Rate",
  864. .info = loopback_rate_info,
  865. .get = loopback_rate_get
  866. },
  867. #define CHANNELS_IDX 5
  868. {
  869. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  870. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  871. .name = "PCM Slave Channels",
  872. .info = loopback_channels_info,
  873. .get = loopback_channels_get
  874. }
  875. };
  876. static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
  877. {
  878. struct snd_card *card = loopback->card;
  879. struct snd_pcm *pcm;
  880. struct snd_kcontrol *kctl;
  881. struct loopback_setup *setup;
  882. int err, dev, substr, substr_count, idx;
  883. strcpy(card->mixername, "Loopback Mixer");
  884. for (dev = 0; dev < 2; dev++) {
  885. pcm = loopback->pcm[dev];
  886. substr_count =
  887. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  888. for (substr = 0; substr < substr_count; substr++) {
  889. setup = &loopback->setup[substr][dev];
  890. setup->notify = notify;
  891. setup->rate_shift = NO_PITCH;
  892. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  893. setup->rate = 48000;
  894. setup->channels = 2;
  895. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  896. idx++) {
  897. kctl = snd_ctl_new1(&loopback_controls[idx],
  898. loopback);
  899. if (!kctl)
  900. return -ENOMEM;
  901. kctl->id.device = dev;
  902. kctl->id.subdevice = substr;
  903. switch (idx) {
  904. case ACTIVE_IDX:
  905. setup->active_id = kctl->id;
  906. break;
  907. case FORMAT_IDX:
  908. setup->format_id = kctl->id;
  909. break;
  910. case RATE_IDX:
  911. setup->rate_id = kctl->id;
  912. break;
  913. case CHANNELS_IDX:
  914. setup->channels_id = kctl->id;
  915. break;
  916. default:
  917. break;
  918. }
  919. err = snd_ctl_add(card, kctl);
  920. if (err < 0)
  921. return err;
  922. }
  923. }
  924. }
  925. return 0;
  926. }
  927. #ifdef CONFIG_PROC_FS
  928. static void print_dpcm_info(struct snd_info_buffer *buffer,
  929. struct loopback_pcm *dpcm,
  930. const char *id)
  931. {
  932. snd_iprintf(buffer, " %s\n", id);
  933. if (dpcm == NULL) {
  934. snd_iprintf(buffer, " inactive\n");
  935. return;
  936. }
  937. snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
  938. snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
  939. snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
  940. snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
  941. snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
  942. snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
  943. snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
  944. snd_iprintf(buffer, " update_pending:\t%u\n",
  945. dpcm->period_update_pending);
  946. snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
  947. snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
  948. snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
  949. dpcm->last_jiffies, jiffies);
  950. snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
  951. }
  952. static void print_substream_info(struct snd_info_buffer *buffer,
  953. struct loopback *loopback,
  954. int sub,
  955. int num)
  956. {
  957. struct loopback_cable *cable = loopback->cables[sub][num];
  958. snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
  959. if (cable == NULL) {
  960. snd_iprintf(buffer, " inactive\n");
  961. return;
  962. }
  963. snd_iprintf(buffer, " valid: %u\n", cable->valid);
  964. snd_iprintf(buffer, " running: %u\n", cable->running);
  965. snd_iprintf(buffer, " pause: %u\n", cable->pause);
  966. print_dpcm_info(buffer, cable->streams[0], "Playback");
  967. print_dpcm_info(buffer, cable->streams[1], "Capture");
  968. }
  969. static void print_cable_info(struct snd_info_entry *entry,
  970. struct snd_info_buffer *buffer)
  971. {
  972. struct loopback *loopback = entry->private_data;
  973. int sub, num;
  974. mutex_lock(&loopback->cable_lock);
  975. num = entry->name[strlen(entry->name)-1];
  976. num = num == '0' ? 0 : 1;
  977. for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
  978. print_substream_info(buffer, loopback, sub, num);
  979. mutex_unlock(&loopback->cable_lock);
  980. }
  981. static int __devinit loopback_proc_new(struct loopback *loopback, int cidx)
  982. {
  983. char name[32];
  984. struct snd_info_entry *entry;
  985. int err;
  986. snprintf(name, sizeof(name), "cable#%d", cidx);
  987. err = snd_card_proc_new(loopback->card, name, &entry);
  988. if (err < 0)
  989. return err;
  990. snd_info_set_text_ops(entry, loopback, print_cable_info);
  991. return 0;
  992. }
  993. #else /* !CONFIG_PROC_FS */
  994. #define loopback_proc_new(loopback, cidx) do { } while (0)
  995. #endif
  996. static int __devinit loopback_probe(struct platform_device *devptr)
  997. {
  998. struct snd_card *card;
  999. struct loopback *loopback;
  1000. int dev = devptr->id;
  1001. int err;
  1002. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  1003. sizeof(struct loopback), &card);
  1004. if (err < 0)
  1005. return err;
  1006. loopback = card->private_data;
  1007. if (pcm_substreams[dev] < 1)
  1008. pcm_substreams[dev] = 1;
  1009. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  1010. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  1011. loopback->card = card;
  1012. mutex_init(&loopback->cable_lock);
  1013. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  1014. if (err < 0)
  1015. goto __nodev;
  1016. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  1017. if (err < 0)
  1018. goto __nodev;
  1019. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  1020. if (err < 0)
  1021. goto __nodev;
  1022. loopback_proc_new(loopback, 0);
  1023. loopback_proc_new(loopback, 1);
  1024. strcpy(card->driver, "Loopback");
  1025. strcpy(card->shortname, "Loopback");
  1026. sprintf(card->longname, "Loopback %i", dev + 1);
  1027. err = snd_card_register(card);
  1028. if (!err) {
  1029. platform_set_drvdata(devptr, card);
  1030. return 0;
  1031. }
  1032. __nodev:
  1033. snd_card_free(card);
  1034. return err;
  1035. }
  1036. static int __devexit loopback_remove(struct platform_device *devptr)
  1037. {
  1038. snd_card_free(platform_get_drvdata(devptr));
  1039. platform_set_drvdata(devptr, NULL);
  1040. return 0;
  1041. }
  1042. #ifdef CONFIG_PM
  1043. static int loopback_suspend(struct platform_device *pdev,
  1044. pm_message_t state)
  1045. {
  1046. struct snd_card *card = platform_get_drvdata(pdev);
  1047. struct loopback *loopback = card->private_data;
  1048. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1049. snd_pcm_suspend_all(loopback->pcm[0]);
  1050. snd_pcm_suspend_all(loopback->pcm[1]);
  1051. return 0;
  1052. }
  1053. static int loopback_resume(struct platform_device *pdev)
  1054. {
  1055. struct snd_card *card = platform_get_drvdata(pdev);
  1056. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1057. return 0;
  1058. }
  1059. #endif
  1060. #define SND_LOOPBACK_DRIVER "snd_aloop"
  1061. static struct platform_driver loopback_driver = {
  1062. .probe = loopback_probe,
  1063. .remove = __devexit_p(loopback_remove),
  1064. #ifdef CONFIG_PM
  1065. .suspend = loopback_suspend,
  1066. .resume = loopback_resume,
  1067. #endif
  1068. .driver = {
  1069. .name = SND_LOOPBACK_DRIVER
  1070. },
  1071. };
  1072. static void loopback_unregister_all(void)
  1073. {
  1074. int i;
  1075. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1076. platform_device_unregister(devices[i]);
  1077. platform_driver_unregister(&loopback_driver);
  1078. }
  1079. static int __init alsa_card_loopback_init(void)
  1080. {
  1081. int i, err, cards;
  1082. err = platform_driver_register(&loopback_driver);
  1083. if (err < 0)
  1084. return err;
  1085. cards = 0;
  1086. for (i = 0; i < SNDRV_CARDS; i++) {
  1087. struct platform_device *device;
  1088. if (!enable[i])
  1089. continue;
  1090. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  1091. i, NULL, 0);
  1092. if (IS_ERR(device))
  1093. continue;
  1094. if (!platform_get_drvdata(device)) {
  1095. platform_device_unregister(device);
  1096. continue;
  1097. }
  1098. devices[i] = device;
  1099. cards++;
  1100. }
  1101. if (!cards) {
  1102. #ifdef MODULE
  1103. printk(KERN_ERR "aloop: No loopback enabled\n");
  1104. #endif
  1105. loopback_unregister_all();
  1106. return -ENODEV;
  1107. }
  1108. return 0;
  1109. }
  1110. static void __exit alsa_card_loopback_exit(void)
  1111. {
  1112. loopback_unregister_all();
  1113. }
  1114. module_init(alsa_card_loopback_init)
  1115. module_exit(alsa_card_loopback_exit)