aloop.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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/module.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/pcm_params.h>
  42. #include <sound/info.h>
  43. #include <sound/initval.h>
  44. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  45. MODULE_DESCRIPTION("A loopback soundcard");
  46. MODULE_LICENSE("GPL");
  47. MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
  48. #define MAX_PCM_SUBSTREAMS 8
  49. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  50. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  51. static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  52. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  53. static int pcm_notify[SNDRV_CARDS];
  54. module_param_array(index, int, NULL, 0444);
  55. MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
  56. module_param_array(id, charp, NULL, 0444);
  57. MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
  58. module_param_array(enable, bool, NULL, 0444);
  59. MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
  60. module_param_array(pcm_substreams, int, NULL, 0444);
  61. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
  62. module_param_array(pcm_notify, int, NULL, 0444);
  63. MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
  64. #define NO_PITCH 100000
  65. struct loopback_pcm;
  66. struct loopback_cable {
  67. spinlock_t lock;
  68. struct loopback_pcm *streams[2];
  69. struct snd_pcm_hardware hw;
  70. /* flags */
  71. unsigned int valid;
  72. unsigned int running;
  73. unsigned int pause;
  74. };
  75. struct loopback_setup {
  76. unsigned int notify: 1;
  77. unsigned int rate_shift;
  78. unsigned int format;
  79. unsigned int rate;
  80. unsigned int channels;
  81. struct snd_ctl_elem_id active_id;
  82. struct snd_ctl_elem_id format_id;
  83. struct snd_ctl_elem_id rate_id;
  84. struct snd_ctl_elem_id channels_id;
  85. };
  86. struct loopback {
  87. struct snd_card *card;
  88. struct mutex cable_lock;
  89. struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
  90. struct snd_pcm *pcm[2];
  91. struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
  92. };
  93. struct loopback_pcm {
  94. struct loopback *loopback;
  95. struct snd_pcm_substream *substream;
  96. struct loopback_cable *cable;
  97. unsigned int pcm_buffer_size;
  98. unsigned int buf_pos; /* position in buffer */
  99. unsigned int silent_size;
  100. /* PCM parameters */
  101. unsigned int pcm_period_size;
  102. unsigned int pcm_bps; /* bytes per second */
  103. unsigned int pcm_salign; /* bytes per sample * channels */
  104. unsigned int pcm_rate_shift; /* rate shift value */
  105. /* flags */
  106. unsigned int period_update_pending :1;
  107. /* timer stuff */
  108. unsigned int irq_pos; /* fractional IRQ position */
  109. unsigned int period_size_frac;
  110. unsigned int last_drift;
  111. unsigned long last_jiffies;
  112. struct timer_list timer;
  113. };
  114. static struct platform_device *devices[SNDRV_CARDS];
  115. static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
  116. {
  117. if (dpcm->pcm_rate_shift == NO_PITCH) {
  118. x /= HZ;
  119. } else {
  120. x = div_u64(NO_PITCH * (unsigned long long)x,
  121. HZ * (unsigned long long)dpcm->pcm_rate_shift);
  122. }
  123. return x - (x % dpcm->pcm_salign);
  124. }
  125. static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
  126. {
  127. if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
  128. return x * HZ;
  129. } else {
  130. x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
  131. NO_PITCH);
  132. }
  133. return x;
  134. }
  135. static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
  136. {
  137. int device = dpcm->substream->pstr->pcm->device;
  138. if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  139. device ^= 1;
  140. return &dpcm->loopback->setup[dpcm->substream->number][device];
  141. }
  142. static inline unsigned int get_notify(struct loopback_pcm *dpcm)
  143. {
  144. return get_setup(dpcm)->notify;
  145. }
  146. static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
  147. {
  148. return get_setup(dpcm)->rate_shift;
  149. }
  150. /* call in cable->lock */
  151. static void loopback_timer_start(struct loopback_pcm *dpcm)
  152. {
  153. unsigned long tick;
  154. unsigned int rate_shift = get_rate_shift(dpcm);
  155. if (rate_shift != dpcm->pcm_rate_shift) {
  156. dpcm->pcm_rate_shift = rate_shift;
  157. dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
  158. }
  159. if (dpcm->period_size_frac <= dpcm->irq_pos) {
  160. dpcm->irq_pos %= dpcm->period_size_frac;
  161. dpcm->period_update_pending = 1;
  162. }
  163. tick = dpcm->period_size_frac - dpcm->irq_pos;
  164. tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
  165. mod_timer(&dpcm->timer, jiffies + tick);
  166. }
  167. /* call in cable->lock */
  168. static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
  169. {
  170. del_timer(&dpcm->timer);
  171. dpcm->timer.expires = 0;
  172. }
  173. static inline void loopback_timer_stop_sync(struct loopback_pcm *dpcm)
  174. {
  175. del_timer_sync(&dpcm->timer);
  176. }
  177. #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
  178. #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
  179. #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
  180. static int loopback_check_format(struct loopback_cable *cable, int stream)
  181. {
  182. struct snd_pcm_runtime *runtime, *cruntime;
  183. struct loopback_setup *setup;
  184. struct snd_card *card;
  185. int check;
  186. if (cable->valid != CABLE_VALID_BOTH) {
  187. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  188. goto __notify;
  189. return 0;
  190. }
  191. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  192. substream->runtime;
  193. cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  194. substream->runtime;
  195. check = runtime->format != cruntime->format ||
  196. runtime->rate != cruntime->rate ||
  197. runtime->channels != cruntime->channels;
  198. if (!check)
  199. return 0;
  200. if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  201. return -EIO;
  202. } else {
  203. snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  204. substream, SNDRV_PCM_STATE_DRAINING);
  205. __notify:
  206. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  207. substream->runtime;
  208. setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  209. card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
  210. if (setup->format != runtime->format) {
  211. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  212. &setup->format_id);
  213. setup->format = runtime->format;
  214. }
  215. if (setup->rate != runtime->rate) {
  216. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  217. &setup->rate_id);
  218. setup->rate = runtime->rate;
  219. }
  220. if (setup->channels != runtime->channels) {
  221. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  222. &setup->channels_id);
  223. setup->channels = runtime->channels;
  224. }
  225. }
  226. return 0;
  227. }
  228. static void loopback_active_notify(struct loopback_pcm *dpcm)
  229. {
  230. snd_ctl_notify(dpcm->loopback->card,
  231. SNDRV_CTL_EVENT_MASK_VALUE,
  232. &get_setup(dpcm)->active_id);
  233. }
  234. static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
  235. {
  236. struct snd_pcm_runtime *runtime = substream->runtime;
  237. struct loopback_pcm *dpcm = runtime->private_data;
  238. struct loopback_cable *cable = dpcm->cable;
  239. int err, stream = 1 << substream->stream;
  240. switch (cmd) {
  241. case SNDRV_PCM_TRIGGER_START:
  242. err = loopback_check_format(cable, substream->stream);
  243. if (err < 0)
  244. return err;
  245. dpcm->last_jiffies = jiffies;
  246. dpcm->pcm_rate_shift = 0;
  247. dpcm->last_drift = 0;
  248. spin_lock(&cable->lock);
  249. cable->running |= stream;
  250. cable->pause &= ~stream;
  251. loopback_timer_start(dpcm);
  252. spin_unlock(&cable->lock);
  253. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  254. loopback_active_notify(dpcm);
  255. break;
  256. case SNDRV_PCM_TRIGGER_STOP:
  257. spin_lock(&cable->lock);
  258. cable->running &= ~stream;
  259. cable->pause &= ~stream;
  260. loopback_timer_stop(dpcm);
  261. spin_unlock(&cable->lock);
  262. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  263. loopback_active_notify(dpcm);
  264. break;
  265. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  266. case SNDRV_PCM_TRIGGER_SUSPEND:
  267. spin_lock(&cable->lock);
  268. cable->pause |= stream;
  269. loopback_timer_stop(dpcm);
  270. spin_unlock(&cable->lock);
  271. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  272. loopback_active_notify(dpcm);
  273. break;
  274. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  275. case SNDRV_PCM_TRIGGER_RESUME:
  276. spin_lock(&cable->lock);
  277. dpcm->last_jiffies = jiffies;
  278. cable->pause &= ~stream;
  279. loopback_timer_start(dpcm);
  280. spin_unlock(&cable->lock);
  281. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  282. loopback_active_notify(dpcm);
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. return 0;
  288. }
  289. static void params_change(struct snd_pcm_substream *substream)
  290. {
  291. struct snd_pcm_runtime *runtime = substream->runtime;
  292. struct loopback_pcm *dpcm = runtime->private_data;
  293. struct loopback_cable *cable = dpcm->cable;
  294. cable->hw.formats = pcm_format_to_bits(runtime->format);
  295. cable->hw.rate_min = runtime->rate;
  296. cable->hw.rate_max = runtime->rate;
  297. cable->hw.channels_min = runtime->channels;
  298. cable->hw.channels_max = runtime->channels;
  299. }
  300. static int loopback_prepare(struct snd_pcm_substream *substream)
  301. {
  302. struct snd_pcm_runtime *runtime = substream->runtime;
  303. struct loopback_pcm *dpcm = runtime->private_data;
  304. struct loopback_cable *cable = dpcm->cable;
  305. int bps, salign;
  306. loopback_timer_stop_sync(dpcm);
  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. static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
  403. unsigned int jiffies_delta)
  404. {
  405. unsigned long last_pos;
  406. unsigned int delta;
  407. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  408. dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
  409. delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  410. if (delta >= dpcm->last_drift)
  411. delta -= dpcm->last_drift;
  412. dpcm->last_drift = 0;
  413. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  414. dpcm->irq_pos %= dpcm->period_size_frac;
  415. dpcm->period_update_pending = 1;
  416. }
  417. return delta;
  418. }
  419. static inline void bytepos_finish(struct loopback_pcm *dpcm,
  420. unsigned int delta)
  421. {
  422. dpcm->buf_pos += delta;
  423. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  424. }
  425. /* call in cable->lock */
  426. static unsigned int loopback_pos_update(struct loopback_cable *cable)
  427. {
  428. struct loopback_pcm *dpcm_play =
  429. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  430. struct loopback_pcm *dpcm_capt =
  431. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  432. unsigned long delta_play = 0, delta_capt = 0;
  433. unsigned int running, count1, count2;
  434. running = cable->running ^ cable->pause;
  435. if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  436. delta_play = jiffies - dpcm_play->last_jiffies;
  437. dpcm_play->last_jiffies += delta_play;
  438. }
  439. if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  440. delta_capt = jiffies - dpcm_capt->last_jiffies;
  441. dpcm_capt->last_jiffies += delta_capt;
  442. }
  443. if (delta_play == 0 && delta_capt == 0)
  444. goto unlock;
  445. if (delta_play > delta_capt) {
  446. count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
  447. bytepos_finish(dpcm_play, count1);
  448. delta_play = delta_capt;
  449. } else if (delta_play < delta_capt) {
  450. count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
  451. clear_capture_buf(dpcm_capt, count1);
  452. bytepos_finish(dpcm_capt, count1);
  453. delta_capt = delta_play;
  454. }
  455. if (delta_play == 0 && delta_capt == 0)
  456. goto unlock;
  457. /* note delta_capt == delta_play at this moment */
  458. count1 = bytepos_delta(dpcm_play, delta_play);
  459. count2 = bytepos_delta(dpcm_capt, delta_capt);
  460. if (count1 < count2) {
  461. dpcm_capt->last_drift = count2 - count1;
  462. count1 = count2;
  463. } else if (count1 > count2) {
  464. dpcm_play->last_drift = count1 - count2;
  465. }
  466. copy_play_buf(dpcm_play, dpcm_capt, count1);
  467. bytepos_finish(dpcm_play, count1);
  468. bytepos_finish(dpcm_capt, count1);
  469. unlock:
  470. return running;
  471. }
  472. static void loopback_timer_function(unsigned long data)
  473. {
  474. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  475. unsigned long flags;
  476. spin_lock_irqsave(&dpcm->cable->lock, flags);
  477. if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
  478. loopback_timer_start(dpcm);
  479. if (dpcm->period_update_pending) {
  480. dpcm->period_update_pending = 0;
  481. spin_unlock_irqrestore(&dpcm->cable->lock, flags);
  482. /* need to unlock before calling below */
  483. snd_pcm_period_elapsed(dpcm->substream);
  484. return;
  485. }
  486. }
  487. spin_unlock_irqrestore(&dpcm->cable->lock, flags);
  488. }
  489. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  490. {
  491. struct snd_pcm_runtime *runtime = substream->runtime;
  492. struct loopback_pcm *dpcm = runtime->private_data;
  493. snd_pcm_uframes_t pos;
  494. spin_lock(&dpcm->cable->lock);
  495. loopback_pos_update(dpcm->cable);
  496. pos = dpcm->buf_pos;
  497. spin_unlock(&dpcm->cable->lock);
  498. return bytes_to_frames(runtime, pos);
  499. }
  500. static struct snd_pcm_hardware loopback_pcm_hardware =
  501. {
  502. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  503. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
  504. SNDRV_PCM_INFO_RESUME),
  505. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  506. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  507. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
  508. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  509. .rate_min = 8000,
  510. .rate_max = 192000,
  511. .channels_min = 1,
  512. .channels_max = 32,
  513. .buffer_bytes_max = 2 * 1024 * 1024,
  514. .period_bytes_min = 64,
  515. /* note check overflow in frac_pos() using pcm_rate_shift before
  516. changing period_bytes_max value */
  517. .period_bytes_max = 1024 * 1024,
  518. .periods_min = 1,
  519. .periods_max = 1024,
  520. .fifo_size = 0,
  521. };
  522. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  523. {
  524. struct loopback_pcm *dpcm = runtime->private_data;
  525. kfree(dpcm);
  526. }
  527. static int loopback_hw_params(struct snd_pcm_substream *substream,
  528. struct snd_pcm_hw_params *params)
  529. {
  530. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  531. params_buffer_bytes(params));
  532. }
  533. static int loopback_hw_free(struct snd_pcm_substream *substream)
  534. {
  535. struct snd_pcm_runtime *runtime = substream->runtime;
  536. struct loopback_pcm *dpcm = runtime->private_data;
  537. struct loopback_cable *cable = dpcm->cable;
  538. mutex_lock(&dpcm->loopback->cable_lock);
  539. cable->valid &= ~(1 << substream->stream);
  540. mutex_unlock(&dpcm->loopback->cable_lock);
  541. return snd_pcm_lib_free_vmalloc_buffer(substream);
  542. }
  543. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  544. {
  545. if (!substream->pcm->device)
  546. return substream->stream;
  547. else
  548. return !substream->stream;
  549. }
  550. static int rule_format(struct snd_pcm_hw_params *params,
  551. struct snd_pcm_hw_rule *rule)
  552. {
  553. struct loopback_pcm *dpcm = rule->private;
  554. struct loopback_cable *cable = dpcm->cable;
  555. struct snd_mask m;
  556. snd_mask_none(&m);
  557. mutex_lock(&dpcm->loopback->cable_lock);
  558. m.bits[0] = (u_int32_t)cable->hw.formats;
  559. m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
  560. mutex_unlock(&dpcm->loopback->cable_lock);
  561. return snd_mask_refine(hw_param_mask(params, rule->var), &m);
  562. }
  563. static int rule_rate(struct snd_pcm_hw_params *params,
  564. struct snd_pcm_hw_rule *rule)
  565. {
  566. struct loopback_pcm *dpcm = rule->private;
  567. struct loopback_cable *cable = dpcm->cable;
  568. struct snd_interval t;
  569. mutex_lock(&dpcm->loopback->cable_lock);
  570. t.min = cable->hw.rate_min;
  571. t.max = cable->hw.rate_max;
  572. mutex_unlock(&dpcm->loopback->cable_lock);
  573. t.openmin = t.openmax = 0;
  574. t.integer = 0;
  575. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  576. }
  577. static int rule_channels(struct snd_pcm_hw_params *params,
  578. struct snd_pcm_hw_rule *rule)
  579. {
  580. struct loopback_pcm *dpcm = rule->private;
  581. struct loopback_cable *cable = dpcm->cable;
  582. struct snd_interval t;
  583. mutex_lock(&dpcm->loopback->cable_lock);
  584. t.min = cable->hw.channels_min;
  585. t.max = cable->hw.channels_max;
  586. mutex_unlock(&dpcm->loopback->cable_lock);
  587. t.openmin = t.openmax = 0;
  588. t.integer = 0;
  589. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  590. }
  591. static void free_cable(struct snd_pcm_substream *substream)
  592. {
  593. struct loopback *loopback = substream->private_data;
  594. int dev = get_cable_index(substream);
  595. struct loopback_cable *cable;
  596. cable = loopback->cables[substream->number][dev];
  597. if (!cable)
  598. return;
  599. if (cable->streams[!substream->stream]) {
  600. /* other stream is still alive */
  601. spin_lock_irq(&cable->lock);
  602. cable->streams[substream->stream] = NULL;
  603. spin_unlock_irq(&cable->lock);
  604. } else {
  605. /* free the cable */
  606. loopback->cables[substream->number][dev] = NULL;
  607. kfree(cable);
  608. }
  609. }
  610. static int loopback_open(struct snd_pcm_substream *substream)
  611. {
  612. struct snd_pcm_runtime *runtime = substream->runtime;
  613. struct loopback *loopback = substream->private_data;
  614. struct loopback_pcm *dpcm;
  615. struct loopback_cable *cable = NULL;
  616. int err = 0;
  617. int dev = get_cable_index(substream);
  618. mutex_lock(&loopback->cable_lock);
  619. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  620. if (!dpcm) {
  621. err = -ENOMEM;
  622. goto unlock;
  623. }
  624. dpcm->loopback = loopback;
  625. dpcm->substream = substream;
  626. setup_timer(&dpcm->timer, loopback_timer_function,
  627. (unsigned long)dpcm);
  628. cable = loopback->cables[substream->number][dev];
  629. if (!cable) {
  630. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  631. if (!cable) {
  632. err = -ENOMEM;
  633. goto unlock;
  634. }
  635. spin_lock_init(&cable->lock);
  636. cable->hw = loopback_pcm_hardware;
  637. loopback->cables[substream->number][dev] = cable;
  638. }
  639. dpcm->cable = cable;
  640. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  641. /* use dynamic rules based on actual runtime->hw values */
  642. /* note that the default rules created in the PCM midlevel code */
  643. /* are cached -> they do not reflect the actual state */
  644. err = snd_pcm_hw_rule_add(runtime, 0,
  645. SNDRV_PCM_HW_PARAM_FORMAT,
  646. rule_format, dpcm,
  647. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  648. if (err < 0)
  649. goto unlock;
  650. err = snd_pcm_hw_rule_add(runtime, 0,
  651. SNDRV_PCM_HW_PARAM_RATE,
  652. rule_rate, dpcm,
  653. SNDRV_PCM_HW_PARAM_RATE, -1);
  654. if (err < 0)
  655. goto unlock;
  656. err = snd_pcm_hw_rule_add(runtime, 0,
  657. SNDRV_PCM_HW_PARAM_CHANNELS,
  658. rule_channels, dpcm,
  659. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  660. if (err < 0)
  661. goto unlock;
  662. runtime->private_data = dpcm;
  663. runtime->private_free = loopback_runtime_free;
  664. if (get_notify(dpcm))
  665. runtime->hw = loopback_pcm_hardware;
  666. else
  667. runtime->hw = cable->hw;
  668. spin_lock_irq(&cable->lock);
  669. cable->streams[substream->stream] = dpcm;
  670. spin_unlock_irq(&cable->lock);
  671. unlock:
  672. if (err < 0) {
  673. free_cable(substream);
  674. kfree(dpcm);
  675. }
  676. mutex_unlock(&loopback->cable_lock);
  677. return err;
  678. }
  679. static int loopback_close(struct snd_pcm_substream *substream)
  680. {
  681. struct loopback *loopback = substream->private_data;
  682. struct loopback_pcm *dpcm = substream->runtime->private_data;
  683. loopback_timer_stop_sync(dpcm);
  684. mutex_lock(&loopback->cable_lock);
  685. free_cable(substream);
  686. mutex_unlock(&loopback->cable_lock);
  687. return 0;
  688. }
  689. static struct snd_pcm_ops loopback_playback_ops = {
  690. .open = loopback_open,
  691. .close = loopback_close,
  692. .ioctl = snd_pcm_lib_ioctl,
  693. .hw_params = loopback_hw_params,
  694. .hw_free = loopback_hw_free,
  695. .prepare = loopback_prepare,
  696. .trigger = loopback_trigger,
  697. .pointer = loopback_pointer,
  698. .page = snd_pcm_lib_get_vmalloc_page,
  699. .mmap = snd_pcm_lib_mmap_vmalloc,
  700. };
  701. static struct snd_pcm_ops loopback_capture_ops = {
  702. .open = loopback_open,
  703. .close = loopback_close,
  704. .ioctl = snd_pcm_lib_ioctl,
  705. .hw_params = loopback_hw_params,
  706. .hw_free = loopback_hw_free,
  707. .prepare = loopback_prepare,
  708. .trigger = loopback_trigger,
  709. .pointer = loopback_pointer,
  710. .page = snd_pcm_lib_get_vmalloc_page,
  711. .mmap = snd_pcm_lib_mmap_vmalloc,
  712. };
  713. static int loopback_pcm_new(struct loopback *loopback,
  714. int device, int substreams)
  715. {
  716. struct snd_pcm *pcm;
  717. int err;
  718. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  719. substreams, substreams, &pcm);
  720. if (err < 0)
  721. return err;
  722. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  723. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  724. pcm->private_data = loopback;
  725. pcm->info_flags = 0;
  726. strcpy(pcm->name, "Loopback PCM");
  727. loopback->pcm[device] = pcm;
  728. return 0;
  729. }
  730. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  731. struct snd_ctl_elem_info *uinfo)
  732. {
  733. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  734. uinfo->count = 1;
  735. uinfo->value.integer.min = 80000;
  736. uinfo->value.integer.max = 120000;
  737. uinfo->value.integer.step = 1;
  738. return 0;
  739. }
  740. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  741. struct snd_ctl_elem_value *ucontrol)
  742. {
  743. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  744. mutex_lock(&loopback->cable_lock);
  745. ucontrol->value.integer.value[0] =
  746. loopback->setup[kcontrol->id.subdevice]
  747. [kcontrol->id.device].rate_shift;
  748. mutex_unlock(&loopback->cable_lock);
  749. return 0;
  750. }
  751. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  752. struct snd_ctl_elem_value *ucontrol)
  753. {
  754. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  755. unsigned int val;
  756. int change = 0;
  757. val = ucontrol->value.integer.value[0];
  758. if (val < 80000)
  759. val = 80000;
  760. if (val > 120000)
  761. val = 120000;
  762. mutex_lock(&loopback->cable_lock);
  763. if (val != loopback->setup[kcontrol->id.subdevice]
  764. [kcontrol->id.device].rate_shift) {
  765. loopback->setup[kcontrol->id.subdevice]
  766. [kcontrol->id.device].rate_shift = val;
  767. change = 1;
  768. }
  769. mutex_unlock(&loopback->cable_lock);
  770. return change;
  771. }
  772. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  773. struct snd_ctl_elem_value *ucontrol)
  774. {
  775. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  776. mutex_lock(&loopback->cable_lock);
  777. ucontrol->value.integer.value[0] =
  778. loopback->setup[kcontrol->id.subdevice]
  779. [kcontrol->id.device].notify;
  780. mutex_unlock(&loopback->cable_lock);
  781. return 0;
  782. }
  783. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  784. struct snd_ctl_elem_value *ucontrol)
  785. {
  786. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  787. unsigned int val;
  788. int change = 0;
  789. val = ucontrol->value.integer.value[0] ? 1 : 0;
  790. mutex_lock(&loopback->cable_lock);
  791. if (val != loopback->setup[kcontrol->id.subdevice]
  792. [kcontrol->id.device].notify) {
  793. loopback->setup[kcontrol->id.subdevice]
  794. [kcontrol->id.device].notify = val;
  795. change = 1;
  796. }
  797. mutex_unlock(&loopback->cable_lock);
  798. return change;
  799. }
  800. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  801. struct snd_ctl_elem_value *ucontrol)
  802. {
  803. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  804. struct loopback_cable *cable;
  805. unsigned int val = 0;
  806. mutex_lock(&loopback->cable_lock);
  807. cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  808. if (cable != NULL) {
  809. unsigned int running = cable->running ^ cable->pause;
  810. val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
  811. }
  812. mutex_unlock(&loopback->cable_lock);
  813. ucontrol->value.integer.value[0] = val;
  814. return 0;
  815. }
  816. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  817. struct snd_ctl_elem_info *uinfo)
  818. {
  819. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  820. uinfo->count = 1;
  821. uinfo->value.integer.min = 0;
  822. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  823. uinfo->value.integer.step = 1;
  824. return 0;
  825. }
  826. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  827. struct snd_ctl_elem_value *ucontrol)
  828. {
  829. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  830. ucontrol->value.integer.value[0] =
  831. loopback->setup[kcontrol->id.subdevice]
  832. [kcontrol->id.device].format;
  833. return 0;
  834. }
  835. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  836. struct snd_ctl_elem_info *uinfo)
  837. {
  838. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  839. uinfo->count = 1;
  840. uinfo->value.integer.min = 0;
  841. uinfo->value.integer.max = 192000;
  842. uinfo->value.integer.step = 1;
  843. return 0;
  844. }
  845. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  846. struct snd_ctl_elem_value *ucontrol)
  847. {
  848. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  849. mutex_lock(&loopback->cable_lock);
  850. ucontrol->value.integer.value[0] =
  851. loopback->setup[kcontrol->id.subdevice]
  852. [kcontrol->id.device].rate;
  853. mutex_unlock(&loopback->cable_lock);
  854. return 0;
  855. }
  856. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  857. struct snd_ctl_elem_info *uinfo)
  858. {
  859. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  860. uinfo->count = 1;
  861. uinfo->value.integer.min = 1;
  862. uinfo->value.integer.max = 1024;
  863. uinfo->value.integer.step = 1;
  864. return 0;
  865. }
  866. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  867. struct snd_ctl_elem_value *ucontrol)
  868. {
  869. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  870. mutex_lock(&loopback->cable_lock);
  871. ucontrol->value.integer.value[0] =
  872. loopback->setup[kcontrol->id.subdevice]
  873. [kcontrol->id.device].channels;
  874. mutex_unlock(&loopback->cable_lock);
  875. return 0;
  876. }
  877. static struct snd_kcontrol_new loopback_controls[] = {
  878. {
  879. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  880. .name = "PCM Rate Shift 100000",
  881. .info = loopback_rate_shift_info,
  882. .get = loopback_rate_shift_get,
  883. .put = loopback_rate_shift_put,
  884. },
  885. {
  886. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  887. .name = "PCM Notify",
  888. .info = snd_ctl_boolean_mono_info,
  889. .get = loopback_notify_get,
  890. .put = loopback_notify_put,
  891. },
  892. #define ACTIVE_IDX 2
  893. {
  894. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  895. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  896. .name = "PCM Slave Active",
  897. .info = snd_ctl_boolean_mono_info,
  898. .get = loopback_active_get,
  899. },
  900. #define FORMAT_IDX 3
  901. {
  902. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  903. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  904. .name = "PCM Slave Format",
  905. .info = loopback_format_info,
  906. .get = loopback_format_get
  907. },
  908. #define RATE_IDX 4
  909. {
  910. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  911. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  912. .name = "PCM Slave Rate",
  913. .info = loopback_rate_info,
  914. .get = loopback_rate_get
  915. },
  916. #define CHANNELS_IDX 5
  917. {
  918. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  919. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  920. .name = "PCM Slave Channels",
  921. .info = loopback_channels_info,
  922. .get = loopback_channels_get
  923. }
  924. };
  925. static int loopback_mixer_new(struct loopback *loopback, int notify)
  926. {
  927. struct snd_card *card = loopback->card;
  928. struct snd_pcm *pcm;
  929. struct snd_kcontrol *kctl;
  930. struct loopback_setup *setup;
  931. int err, dev, substr, substr_count, idx;
  932. strcpy(card->mixername, "Loopback Mixer");
  933. for (dev = 0; dev < 2; dev++) {
  934. pcm = loopback->pcm[dev];
  935. substr_count =
  936. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  937. for (substr = 0; substr < substr_count; substr++) {
  938. setup = &loopback->setup[substr][dev];
  939. setup->notify = notify;
  940. setup->rate_shift = NO_PITCH;
  941. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  942. setup->rate = 48000;
  943. setup->channels = 2;
  944. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  945. idx++) {
  946. kctl = snd_ctl_new1(&loopback_controls[idx],
  947. loopback);
  948. if (!kctl)
  949. return -ENOMEM;
  950. kctl->id.device = dev;
  951. kctl->id.subdevice = substr;
  952. switch (idx) {
  953. case ACTIVE_IDX:
  954. setup->active_id = kctl->id;
  955. break;
  956. case FORMAT_IDX:
  957. setup->format_id = kctl->id;
  958. break;
  959. case RATE_IDX:
  960. setup->rate_id = kctl->id;
  961. break;
  962. case CHANNELS_IDX:
  963. setup->channels_id = kctl->id;
  964. break;
  965. default:
  966. break;
  967. }
  968. err = snd_ctl_add(card, kctl);
  969. if (err < 0)
  970. return err;
  971. }
  972. }
  973. }
  974. return 0;
  975. }
  976. static void print_dpcm_info(struct snd_info_buffer *buffer,
  977. struct loopback_pcm *dpcm,
  978. const char *id)
  979. {
  980. snd_iprintf(buffer, " %s\n", id);
  981. if (dpcm == NULL) {
  982. snd_iprintf(buffer, " inactive\n");
  983. return;
  984. }
  985. snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
  986. snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
  987. snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
  988. snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
  989. snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
  990. snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
  991. snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
  992. snd_iprintf(buffer, " update_pending:\t%u\n",
  993. dpcm->period_update_pending);
  994. snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
  995. snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
  996. snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
  997. dpcm->last_jiffies, jiffies);
  998. snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
  999. }
  1000. static void print_substream_info(struct snd_info_buffer *buffer,
  1001. struct loopback *loopback,
  1002. int sub,
  1003. int num)
  1004. {
  1005. struct loopback_cable *cable = loopback->cables[sub][num];
  1006. snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
  1007. if (cable == NULL) {
  1008. snd_iprintf(buffer, " inactive\n");
  1009. return;
  1010. }
  1011. snd_iprintf(buffer, " valid: %u\n", cable->valid);
  1012. snd_iprintf(buffer, " running: %u\n", cable->running);
  1013. snd_iprintf(buffer, " pause: %u\n", cable->pause);
  1014. print_dpcm_info(buffer, cable->streams[0], "Playback");
  1015. print_dpcm_info(buffer, cable->streams[1], "Capture");
  1016. }
  1017. static void print_cable_info(struct snd_info_entry *entry,
  1018. struct snd_info_buffer *buffer)
  1019. {
  1020. struct loopback *loopback = entry->private_data;
  1021. int sub, num;
  1022. mutex_lock(&loopback->cable_lock);
  1023. num = entry->name[strlen(entry->name)-1];
  1024. num = num == '0' ? 0 : 1;
  1025. for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
  1026. print_substream_info(buffer, loopback, sub, num);
  1027. mutex_unlock(&loopback->cable_lock);
  1028. }
  1029. static int loopback_proc_new(struct loopback *loopback, int cidx)
  1030. {
  1031. char name[32];
  1032. struct snd_info_entry *entry;
  1033. int err;
  1034. snprintf(name, sizeof(name), "cable#%d", cidx);
  1035. err = snd_card_proc_new(loopback->card, name, &entry);
  1036. if (err < 0)
  1037. return err;
  1038. snd_info_set_text_ops(entry, loopback, print_cable_info);
  1039. return 0;
  1040. }
  1041. static int loopback_probe(struct platform_device *devptr)
  1042. {
  1043. struct snd_card *card;
  1044. struct loopback *loopback;
  1045. int dev = devptr->id;
  1046. int err;
  1047. err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  1048. sizeof(struct loopback), &card);
  1049. if (err < 0)
  1050. return err;
  1051. loopback = card->private_data;
  1052. if (pcm_substreams[dev] < 1)
  1053. pcm_substreams[dev] = 1;
  1054. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  1055. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  1056. loopback->card = card;
  1057. mutex_init(&loopback->cable_lock);
  1058. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  1059. if (err < 0)
  1060. goto __nodev;
  1061. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  1062. if (err < 0)
  1063. goto __nodev;
  1064. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  1065. if (err < 0)
  1066. goto __nodev;
  1067. loopback_proc_new(loopback, 0);
  1068. loopback_proc_new(loopback, 1);
  1069. strcpy(card->driver, "Loopback");
  1070. strcpy(card->shortname, "Loopback");
  1071. sprintf(card->longname, "Loopback %i", dev + 1);
  1072. err = snd_card_register(card);
  1073. if (!err) {
  1074. platform_set_drvdata(devptr, card);
  1075. return 0;
  1076. }
  1077. __nodev:
  1078. snd_card_free(card);
  1079. return err;
  1080. }
  1081. static int loopback_remove(struct platform_device *devptr)
  1082. {
  1083. snd_card_free(platform_get_drvdata(devptr));
  1084. return 0;
  1085. }
  1086. #ifdef CONFIG_PM_SLEEP
  1087. static int loopback_suspend(struct device *pdev)
  1088. {
  1089. struct snd_card *card = dev_get_drvdata(pdev);
  1090. struct loopback *loopback = card->private_data;
  1091. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1092. snd_pcm_suspend_all(loopback->pcm[0]);
  1093. snd_pcm_suspend_all(loopback->pcm[1]);
  1094. return 0;
  1095. }
  1096. static int loopback_resume(struct device *pdev)
  1097. {
  1098. struct snd_card *card = dev_get_drvdata(pdev);
  1099. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1100. return 0;
  1101. }
  1102. static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
  1103. #define LOOPBACK_PM_OPS &loopback_pm
  1104. #else
  1105. #define LOOPBACK_PM_OPS NULL
  1106. #endif
  1107. #define SND_LOOPBACK_DRIVER "snd_aloop"
  1108. static struct platform_driver loopback_driver = {
  1109. .probe = loopback_probe,
  1110. .remove = loopback_remove,
  1111. .driver = {
  1112. .name = SND_LOOPBACK_DRIVER,
  1113. .pm = LOOPBACK_PM_OPS,
  1114. },
  1115. };
  1116. static void loopback_unregister_all(void)
  1117. {
  1118. int i;
  1119. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1120. platform_device_unregister(devices[i]);
  1121. platform_driver_unregister(&loopback_driver);
  1122. }
  1123. static int __init alsa_card_loopback_init(void)
  1124. {
  1125. int i, err, cards;
  1126. err = platform_driver_register(&loopback_driver);
  1127. if (err < 0)
  1128. return err;
  1129. cards = 0;
  1130. for (i = 0; i < SNDRV_CARDS; i++) {
  1131. struct platform_device *device;
  1132. if (!enable[i])
  1133. continue;
  1134. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  1135. i, NULL, 0);
  1136. if (IS_ERR(device))
  1137. continue;
  1138. if (!platform_get_drvdata(device)) {
  1139. platform_device_unregister(device);
  1140. continue;
  1141. }
  1142. devices[i] = device;
  1143. cards++;
  1144. }
  1145. if (!cards) {
  1146. #ifdef MODULE
  1147. printk(KERN_ERR "aloop: No loopback enabled\n");
  1148. #endif
  1149. loopback_unregister_all();
  1150. return -ENODEV;
  1151. }
  1152. return 0;
  1153. }
  1154. static void __exit alsa_card_loopback_exit(void)
  1155. {
  1156. loopback_unregister_all();
  1157. }
  1158. module_init(alsa_card_loopback_init)
  1159. module_exit(alsa_card_loopback_exit)