aloop.c 29 KB

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