aloop.c 35 KB

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