aloop.c 35 KB

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