aloop.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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, *cruntime;
  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. cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  180. substream->runtime;
  181. check = runtime->format != cruntime->format ||
  182. runtime->rate != cruntime->rate ||
  183. runtime->channels != cruntime->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 void params_change_substream(struct loopback_pcm *dpcm,
  250. struct snd_pcm_runtime *runtime)
  251. {
  252. struct snd_pcm_runtime *dst_runtime;
  253. if (dpcm == NULL || dpcm->substream == NULL)
  254. return;
  255. dst_runtime = dpcm->substream->runtime;
  256. if (dst_runtime == NULL)
  257. return;
  258. dst_runtime->hw = dpcm->cable->hw;
  259. }
  260. static void params_change(struct snd_pcm_substream *substream)
  261. {
  262. struct snd_pcm_runtime *runtime = substream->runtime;
  263. struct loopback_pcm *dpcm = runtime->private_data;
  264. struct loopback_cable *cable = dpcm->cable;
  265. cable->hw.formats = (1ULL << runtime->format);
  266. cable->hw.rate_min = runtime->rate;
  267. cable->hw.rate_max = runtime->rate;
  268. cable->hw.channels_min = runtime->channels;
  269. cable->hw.channels_max = runtime->channels;
  270. params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  271. runtime);
  272. params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  273. runtime);
  274. }
  275. static int loopback_prepare(struct snd_pcm_substream *substream)
  276. {
  277. struct snd_pcm_runtime *runtime = substream->runtime;
  278. struct loopback_pcm *dpcm = runtime->private_data;
  279. struct loopback_cable *cable = dpcm->cable;
  280. int bps, salign;
  281. salign = (snd_pcm_format_width(runtime->format) *
  282. runtime->channels) / 8;
  283. bps = salign * runtime->rate;
  284. if (bps <= 0 || salign <= 0)
  285. return -EINVAL;
  286. dpcm->buf_pos = 0;
  287. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  288. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  289. /* clear capture buffer */
  290. dpcm->silent_size = dpcm->pcm_buffer_size;
  291. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  292. runtime->buffer_size * runtime->channels);
  293. }
  294. dpcm->irq_pos = 0;
  295. dpcm->period_update_pending = 0;
  296. dpcm->pcm_bps = bps;
  297. dpcm->pcm_salign = salign;
  298. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  299. mutex_lock(&dpcm->loopback->cable_lock);
  300. if (!(cable->valid & ~(1 << substream->stream)) ||
  301. (get_setup(dpcm)->notify &&
  302. substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  303. params_change(substream);
  304. cable->valid |= 1 << substream->stream;
  305. mutex_unlock(&dpcm->loopback->cable_lock);
  306. return 0;
  307. }
  308. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  309. {
  310. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  311. char *dst = runtime->dma_area;
  312. unsigned int dst_off = dpcm->buf_pos;
  313. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  314. return;
  315. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  316. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  317. for (;;) {
  318. unsigned int size = bytes;
  319. if (dst_off + size > dpcm->pcm_buffer_size)
  320. size = dpcm->pcm_buffer_size - dst_off;
  321. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  322. bytes_to_frames(runtime, size) *
  323. runtime->channels);
  324. dpcm->silent_size += size;
  325. bytes -= size;
  326. if (!bytes)
  327. break;
  328. dst_off = 0;
  329. }
  330. }
  331. static void copy_play_buf(struct loopback_pcm *play,
  332. struct loopback_pcm *capt,
  333. unsigned int bytes)
  334. {
  335. struct snd_pcm_runtime *runtime = play->substream->runtime;
  336. char *src = runtime->dma_area;
  337. char *dst = capt->substream->runtime->dma_area;
  338. unsigned int src_off = play->buf_pos;
  339. unsigned int dst_off = capt->buf_pos;
  340. unsigned int clear_bytes = 0;
  341. /* check if playback is draining, trim the capture copy size
  342. * when our pointer is at the end of playback ring buffer */
  343. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  344. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  345. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  346. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  347. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  348. appl_ptr1 += play->buf_pos / play->pcm_salign;
  349. if (appl_ptr < appl_ptr1)
  350. appl_ptr1 -= runtime->buffer_size;
  351. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  352. if (diff < bytes) {
  353. clear_bytes = bytes - diff;
  354. bytes = diff;
  355. }
  356. }
  357. for (;;) {
  358. unsigned int size = bytes;
  359. if (src_off + size > play->pcm_buffer_size)
  360. size = play->pcm_buffer_size - src_off;
  361. if (dst_off + size > capt->pcm_buffer_size)
  362. size = capt->pcm_buffer_size - dst_off;
  363. memcpy(dst + dst_off, src + src_off, size);
  364. capt->silent_size = 0;
  365. bytes -= size;
  366. if (!bytes)
  367. break;
  368. src_off = (src_off + size) % play->pcm_buffer_size;
  369. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  370. }
  371. if (clear_bytes > 0) {
  372. clear_capture_buf(capt, clear_bytes);
  373. capt->silent_size = 0;
  374. }
  375. }
  376. #define BYTEPOS_UPDATE_POSONLY 0
  377. #define BYTEPOS_UPDATE_CLEAR 1
  378. #define BYTEPOS_UPDATE_COPY 2
  379. static void loopback_bytepos_update(struct loopback_pcm *dpcm,
  380. unsigned int delta,
  381. unsigned int cmd)
  382. {
  383. unsigned int count;
  384. unsigned long last_pos;
  385. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  386. dpcm->irq_pos += delta * dpcm->pcm_bps;
  387. count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  388. if (!count)
  389. return;
  390. if (cmd == BYTEPOS_UPDATE_CLEAR)
  391. clear_capture_buf(dpcm, count);
  392. else if (cmd == BYTEPOS_UPDATE_COPY)
  393. copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  394. dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  395. count);
  396. dpcm->buf_pos += count;
  397. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  398. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  399. dpcm->irq_pos %= dpcm->period_size_frac;
  400. dpcm->period_update_pending = 1;
  401. }
  402. }
  403. static void loopback_pos_update(struct loopback_cable *cable)
  404. {
  405. struct loopback_pcm *dpcm_play =
  406. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  407. struct loopback_pcm *dpcm_capt =
  408. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  409. unsigned long delta_play = 0, delta_capt = 0;
  410. spin_lock(&cable->lock);
  411. if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  412. delta_play = jiffies - dpcm_play->last_jiffies;
  413. dpcm_play->last_jiffies += delta_play;
  414. }
  415. if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  416. delta_capt = jiffies - dpcm_capt->last_jiffies;
  417. dpcm_capt->last_jiffies += delta_capt;
  418. }
  419. if (delta_play == 0 && delta_capt == 0) {
  420. spin_unlock(&cable->lock);
  421. return;
  422. }
  423. if (delta_play > delta_capt) {
  424. loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
  425. BYTEPOS_UPDATE_POSONLY);
  426. delta_play = delta_capt;
  427. } else if (delta_play < delta_capt) {
  428. loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
  429. BYTEPOS_UPDATE_CLEAR);
  430. delta_capt = delta_play;
  431. }
  432. if (delta_play == 0 && delta_capt == 0) {
  433. spin_unlock(&cable->lock);
  434. return;
  435. }
  436. /* note delta_capt == delta_play at this moment */
  437. loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
  438. loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
  439. spin_unlock(&cable->lock);
  440. }
  441. static void loopback_timer_function(unsigned long data)
  442. {
  443. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  444. int stream;
  445. loopback_pos_update(dpcm->cable);
  446. stream = dpcm->substream->stream;
  447. if (dpcm->cable->running & (1 << stream))
  448. loopback_timer_start(dpcm);
  449. if (dpcm->period_update_pending) {
  450. dpcm->period_update_pending = 0;
  451. if (dpcm->cable->running & (1 << stream))
  452. snd_pcm_period_elapsed(dpcm->substream);
  453. }
  454. }
  455. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  456. {
  457. struct snd_pcm_runtime *runtime = substream->runtime;
  458. struct loopback_pcm *dpcm = runtime->private_data;
  459. loopback_pos_update(dpcm->cable);
  460. return bytes_to_frames(runtime, dpcm->buf_pos);
  461. }
  462. static struct snd_pcm_hardware loopback_pcm_hardware =
  463. {
  464. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  465. SNDRV_PCM_INFO_MMAP_VALID),
  466. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  467. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  468. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
  469. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  470. .rate_min = 8000,
  471. .rate_max = 192000,
  472. .channels_min = 1,
  473. .channels_max = 32,
  474. .buffer_bytes_max = 2 * 1024 * 1024,
  475. .period_bytes_min = 64,
  476. .period_bytes_max = 2 * 1024 * 1024,
  477. .periods_min = 1,
  478. .periods_max = 1024,
  479. .fifo_size = 0,
  480. };
  481. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  482. {
  483. struct loopback_pcm *dpcm = runtime->private_data;
  484. kfree(dpcm);
  485. }
  486. static int loopback_hw_params(struct snd_pcm_substream *substream,
  487. struct snd_pcm_hw_params *params)
  488. {
  489. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  490. }
  491. static int loopback_hw_free(struct snd_pcm_substream *substream)
  492. {
  493. struct snd_pcm_runtime *runtime = substream->runtime;
  494. struct loopback_pcm *dpcm = runtime->private_data;
  495. struct loopback_cable *cable = dpcm->cable;
  496. mutex_lock(&dpcm->loopback->cable_lock);
  497. cable->valid &= ~(1 << substream->stream);
  498. mutex_unlock(&dpcm->loopback->cable_lock);
  499. return snd_pcm_lib_free_pages(substream);
  500. }
  501. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  502. {
  503. if (!substream->pcm->device)
  504. return substream->stream;
  505. else
  506. return !substream->stream;
  507. }
  508. static int rule_format(struct snd_pcm_hw_params *params,
  509. struct snd_pcm_hw_rule *rule)
  510. {
  511. struct snd_pcm_hardware *hw = rule->private;
  512. struct snd_mask *maskp = hw_param_mask(params, rule->var);
  513. maskp->bits[0] &= (u_int32_t)hw->formats;
  514. maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
  515. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  516. if (! maskp->bits[0] && ! maskp->bits[1])
  517. return -EINVAL;
  518. return 0;
  519. }
  520. static int rule_rate(struct snd_pcm_hw_params *params,
  521. struct snd_pcm_hw_rule *rule)
  522. {
  523. struct snd_pcm_hardware *hw = rule->private;
  524. struct snd_interval t;
  525. t.min = hw->rate_min;
  526. t.max = hw->rate_max;
  527. t.openmin = t.openmax = 0;
  528. t.integer = 0;
  529. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  530. }
  531. static int rule_channels(struct snd_pcm_hw_params *params,
  532. struct snd_pcm_hw_rule *rule)
  533. {
  534. struct snd_pcm_hardware *hw = rule->private;
  535. struct snd_interval t;
  536. t.min = hw->channels_min;
  537. t.max = hw->channels_max;
  538. t.openmin = t.openmax = 0;
  539. t.integer = 0;
  540. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  541. }
  542. static int loopback_open(struct snd_pcm_substream *substream)
  543. {
  544. struct snd_pcm_runtime *runtime = substream->runtime;
  545. struct loopback *loopback = substream->private_data;
  546. struct loopback_pcm *dpcm;
  547. struct loopback_cable *cable;
  548. int err = 0;
  549. int dev = get_cable_index(substream);
  550. mutex_lock(&loopback->cable_lock);
  551. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  552. if (!dpcm) {
  553. err = -ENOMEM;
  554. goto unlock;
  555. }
  556. dpcm->loopback = loopback;
  557. dpcm->substream = substream;
  558. setup_timer(&dpcm->timer, loopback_timer_function,
  559. (unsigned long)dpcm);
  560. cable = loopback->cables[substream->number][dev];
  561. if (!cable) {
  562. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  563. if (!cable) {
  564. kfree(dpcm);
  565. err = -ENOMEM;
  566. goto unlock;
  567. }
  568. spin_lock_init(&cable->lock);
  569. cable->hw = loopback_pcm_hardware;
  570. loopback->cables[substream->number][dev] = cable;
  571. }
  572. dpcm->cable = cable;
  573. cable->streams[substream->stream] = dpcm;
  574. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  575. /* use dynamic rules based on actual runtime->hw values */
  576. /* note that the default rules created in the PCM midlevel code */
  577. /* are cached -> they do not reflect the actual state */
  578. err = snd_pcm_hw_rule_add(runtime, 0,
  579. SNDRV_PCM_HW_PARAM_FORMAT,
  580. rule_format, &runtime->hw,
  581. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  582. if (err < 0)
  583. goto unlock;
  584. err = snd_pcm_hw_rule_add(runtime, 0,
  585. SNDRV_PCM_HW_PARAM_RATE,
  586. rule_rate, &runtime->hw,
  587. SNDRV_PCM_HW_PARAM_RATE, -1);
  588. if (err < 0)
  589. goto unlock;
  590. err = snd_pcm_hw_rule_add(runtime, 0,
  591. SNDRV_PCM_HW_PARAM_CHANNELS,
  592. rule_channels, &runtime->hw,
  593. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  594. if (err < 0)
  595. goto unlock;
  596. runtime->private_data = dpcm;
  597. runtime->private_free = loopback_runtime_free;
  598. if (get_notify(dpcm))
  599. runtime->hw = loopback_pcm_hardware;
  600. else
  601. runtime->hw = cable->hw;
  602. unlock:
  603. mutex_unlock(&loopback->cable_lock);
  604. return err;
  605. }
  606. static int loopback_close(struct snd_pcm_substream *substream)
  607. {
  608. struct loopback *loopback = substream->private_data;
  609. struct loopback_pcm *dpcm = substream->runtime->private_data;
  610. struct loopback_cable *cable;
  611. int dev = get_cable_index(substream);
  612. loopback_timer_stop(dpcm);
  613. mutex_lock(&loopback->cable_lock);
  614. cable = loopback->cables[substream->number][dev];
  615. if (cable->streams[!substream->stream]) {
  616. /* other stream is still alive */
  617. cable->streams[substream->stream] = NULL;
  618. } else {
  619. /* free the cable */
  620. loopback->cables[substream->number][dev] = NULL;
  621. kfree(cable);
  622. }
  623. mutex_unlock(&loopback->cable_lock);
  624. return 0;
  625. }
  626. static struct snd_pcm_ops loopback_playback_ops = {
  627. .open = loopback_open,
  628. .close = loopback_close,
  629. .ioctl = snd_pcm_lib_ioctl,
  630. .hw_params = loopback_hw_params,
  631. .hw_free = loopback_hw_free,
  632. .prepare = loopback_prepare,
  633. .trigger = loopback_trigger,
  634. .pointer = loopback_pointer,
  635. };
  636. static struct snd_pcm_ops loopback_capture_ops = {
  637. .open = loopback_open,
  638. .close = loopback_close,
  639. .ioctl = snd_pcm_lib_ioctl,
  640. .hw_params = loopback_hw_params,
  641. .hw_free = loopback_hw_free,
  642. .prepare = loopback_prepare,
  643. .trigger = loopback_trigger,
  644. .pointer = loopback_pointer,
  645. };
  646. static int __devinit loopback_pcm_new(struct loopback *loopback,
  647. int device, int substreams)
  648. {
  649. struct snd_pcm *pcm;
  650. int err;
  651. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  652. substreams, substreams, &pcm);
  653. if (err < 0)
  654. return err;
  655. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  656. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  657. pcm->private_data = loopback;
  658. pcm->info_flags = 0;
  659. strcpy(pcm->name, "Loopback PCM");
  660. loopback->pcm[device] = pcm;
  661. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  662. snd_dma_continuous_data(GFP_KERNEL),
  663. 0, 2 * 1024 * 1024);
  664. return 0;
  665. }
  666. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  667. struct snd_ctl_elem_info *uinfo)
  668. {
  669. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  670. uinfo->count = 1;
  671. uinfo->value.integer.min = 80000;
  672. uinfo->value.integer.max = 120000;
  673. uinfo->value.integer.step = 1;
  674. return 0;
  675. }
  676. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  677. struct snd_ctl_elem_value *ucontrol)
  678. {
  679. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  680. ucontrol->value.integer.value[0] =
  681. loopback->setup[kcontrol->id.subdevice]
  682. [kcontrol->id.device].rate_shift;
  683. return 0;
  684. }
  685. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  686. struct snd_ctl_elem_value *ucontrol)
  687. {
  688. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  689. unsigned int val;
  690. int change = 0;
  691. val = ucontrol->value.integer.value[0];
  692. if (val < 80000)
  693. val = 80000;
  694. if (val > 120000)
  695. val = 120000;
  696. mutex_lock(&loopback->cable_lock);
  697. if (val != loopback->setup[kcontrol->id.subdevice]
  698. [kcontrol->id.device].rate_shift) {
  699. loopback->setup[kcontrol->id.subdevice]
  700. [kcontrol->id.device].rate_shift = val;
  701. change = 1;
  702. }
  703. mutex_unlock(&loopback->cable_lock);
  704. return change;
  705. }
  706. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  707. struct snd_ctl_elem_value *ucontrol)
  708. {
  709. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  710. ucontrol->value.integer.value[0] =
  711. loopback->setup[kcontrol->id.subdevice]
  712. [kcontrol->id.device].notify;
  713. return 0;
  714. }
  715. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  716. struct snd_ctl_elem_value *ucontrol)
  717. {
  718. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  719. unsigned int val;
  720. int change = 0;
  721. val = ucontrol->value.integer.value[0] ? 1 : 0;
  722. if (val != loopback->setup[kcontrol->id.subdevice]
  723. [kcontrol->id.device].notify) {
  724. loopback->setup[kcontrol->id.subdevice]
  725. [kcontrol->id.device].notify = val;
  726. change = 1;
  727. }
  728. return change;
  729. }
  730. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  731. struct snd_ctl_elem_value *ucontrol)
  732. {
  733. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  734. struct loopback_cable *cable = loopback->cables
  735. [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  736. unsigned int val = 0;
  737. if (cable != NULL)
  738. val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  739. 1 : 0;
  740. ucontrol->value.integer.value[0] = val;
  741. return 0;
  742. }
  743. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  744. struct snd_ctl_elem_info *uinfo)
  745. {
  746. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  747. uinfo->count = 1;
  748. uinfo->value.integer.min = 0;
  749. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  750. uinfo->value.integer.step = 1;
  751. return 0;
  752. }
  753. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  754. struct snd_ctl_elem_value *ucontrol)
  755. {
  756. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  757. ucontrol->value.integer.value[0] =
  758. loopback->setup[kcontrol->id.subdevice]
  759. [kcontrol->id.device].format;
  760. return 0;
  761. }
  762. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  763. struct snd_ctl_elem_info *uinfo)
  764. {
  765. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  766. uinfo->count = 1;
  767. uinfo->value.integer.min = 0;
  768. uinfo->value.integer.max = 192000;
  769. uinfo->value.integer.step = 1;
  770. return 0;
  771. }
  772. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  773. struct snd_ctl_elem_value *ucontrol)
  774. {
  775. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  776. ucontrol->value.integer.value[0] =
  777. loopback->setup[kcontrol->id.subdevice]
  778. [kcontrol->id.device].rate;
  779. return 0;
  780. }
  781. static int loopback_channels_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 = 1;
  787. uinfo->value.integer.max = 1024;
  788. uinfo->value.integer.step = 1;
  789. return 0;
  790. }
  791. static int loopback_channels_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].channels;
  798. return 0;
  799. }
  800. static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
  801. {
  802. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  803. .name = "PCM Rate Shift 100000",
  804. .info = loopback_rate_shift_info,
  805. .get = loopback_rate_shift_get,
  806. .put = loopback_rate_shift_put,
  807. },
  808. {
  809. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  810. .name = "PCM Notify",
  811. .info = snd_ctl_boolean_mono_info,
  812. .get = loopback_notify_get,
  813. .put = loopback_notify_put,
  814. },
  815. #define ACTIVE_IDX 2
  816. {
  817. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  818. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  819. .name = "PCM Slave Active",
  820. .info = snd_ctl_boolean_mono_info,
  821. .get = loopback_active_get,
  822. },
  823. #define FORMAT_IDX 3
  824. {
  825. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  826. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  827. .name = "PCM Slave Format",
  828. .info = loopback_format_info,
  829. .get = loopback_format_get
  830. },
  831. #define RATE_IDX 4
  832. {
  833. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  834. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  835. .name = "PCM Slave Rate",
  836. .info = loopback_rate_info,
  837. .get = loopback_rate_get
  838. },
  839. #define CHANNELS_IDX 5
  840. {
  841. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  842. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  843. .name = "PCM Slave Channels",
  844. .info = loopback_channels_info,
  845. .get = loopback_channels_get
  846. }
  847. };
  848. static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
  849. {
  850. struct snd_card *card = loopback->card;
  851. struct snd_pcm *pcm;
  852. struct snd_kcontrol *kctl;
  853. struct loopback_setup *setup;
  854. int err, dev, substr, substr_count, idx;
  855. strcpy(card->mixername, "Loopback Mixer");
  856. for (dev = 0; dev < 2; dev++) {
  857. pcm = loopback->pcm[dev];
  858. substr_count =
  859. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  860. for (substr = 0; substr < substr_count; substr++) {
  861. setup = &loopback->setup[substr][dev];
  862. setup->notify = notify;
  863. setup->rate_shift = NO_PITCH;
  864. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  865. setup->rate = 48000;
  866. setup->channels = 2;
  867. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  868. idx++) {
  869. kctl = snd_ctl_new1(&loopback_controls[idx],
  870. loopback);
  871. if (!kctl)
  872. return -ENOMEM;
  873. kctl->id.device = dev;
  874. kctl->id.subdevice = substr;
  875. switch (idx) {
  876. case ACTIVE_IDX:
  877. setup->active_id = kctl->id;
  878. break;
  879. case FORMAT_IDX:
  880. setup->format_id = kctl->id;
  881. break;
  882. case RATE_IDX:
  883. setup->rate_id = kctl->id;
  884. break;
  885. case CHANNELS_IDX:
  886. setup->channels_id = kctl->id;
  887. break;
  888. default:
  889. break;
  890. }
  891. err = snd_ctl_add(card, kctl);
  892. if (err < 0)
  893. return err;
  894. }
  895. }
  896. }
  897. return 0;
  898. }
  899. static int __devinit loopback_probe(struct platform_device *devptr)
  900. {
  901. struct snd_card *card;
  902. struct loopback *loopback;
  903. int dev = devptr->id;
  904. int err;
  905. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  906. sizeof(struct loopback), &card);
  907. if (err < 0)
  908. return err;
  909. loopback = card->private_data;
  910. if (pcm_substreams[dev] < 1)
  911. pcm_substreams[dev] = 1;
  912. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  913. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  914. loopback->card = card;
  915. mutex_init(&loopback->cable_lock);
  916. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  917. if (err < 0)
  918. goto __nodev;
  919. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  920. if (err < 0)
  921. goto __nodev;
  922. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  923. if (err < 0)
  924. goto __nodev;
  925. strcpy(card->driver, "Loopback");
  926. strcpy(card->shortname, "Loopback");
  927. sprintf(card->longname, "Loopback %i", dev + 1);
  928. err = snd_card_register(card);
  929. if (!err) {
  930. platform_set_drvdata(devptr, card);
  931. return 0;
  932. }
  933. __nodev:
  934. snd_card_free(card);
  935. return err;
  936. }
  937. static int __devexit loopback_remove(struct platform_device *devptr)
  938. {
  939. snd_card_free(platform_get_drvdata(devptr));
  940. platform_set_drvdata(devptr, NULL);
  941. return 0;
  942. }
  943. #ifdef CONFIG_PM
  944. static int loopback_suspend(struct platform_device *pdev,
  945. pm_message_t state)
  946. {
  947. struct snd_card *card = platform_get_drvdata(pdev);
  948. struct loopback *loopback = card->private_data;
  949. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  950. snd_pcm_suspend_all(loopback->pcm[0]);
  951. snd_pcm_suspend_all(loopback->pcm[1]);
  952. return 0;
  953. }
  954. static int loopback_resume(struct platform_device *pdev)
  955. {
  956. struct snd_card *card = platform_get_drvdata(pdev);
  957. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  958. return 0;
  959. }
  960. #endif
  961. #define SND_LOOPBACK_DRIVER "snd_aloop"
  962. static struct platform_driver loopback_driver = {
  963. .probe = loopback_probe,
  964. .remove = __devexit_p(loopback_remove),
  965. #ifdef CONFIG_PM
  966. .suspend = loopback_suspend,
  967. .resume = loopback_resume,
  968. #endif
  969. .driver = {
  970. .name = SND_LOOPBACK_DRIVER
  971. },
  972. };
  973. static void loopback_unregister_all(void)
  974. {
  975. int i;
  976. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  977. platform_device_unregister(devices[i]);
  978. platform_driver_unregister(&loopback_driver);
  979. }
  980. static int __init alsa_card_loopback_init(void)
  981. {
  982. int i, err, cards;
  983. err = platform_driver_register(&loopback_driver);
  984. if (err < 0)
  985. return err;
  986. cards = 0;
  987. for (i = 0; i < SNDRV_CARDS; i++) {
  988. struct platform_device *device;
  989. if (!enable[i])
  990. continue;
  991. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  992. i, NULL, 0);
  993. if (IS_ERR(device))
  994. continue;
  995. if (!platform_get_drvdata(device)) {
  996. platform_device_unregister(device);
  997. continue;
  998. }
  999. devices[i] = device;
  1000. cards++;
  1001. }
  1002. if (!cards) {
  1003. #ifdef MODULE
  1004. printk(KERN_ERR "aloop: No loopback enabled\n");
  1005. #endif
  1006. loopback_unregister_all();
  1007. return -ENODEV;
  1008. }
  1009. return 0;
  1010. }
  1011. static void __exit alsa_card_loopback_exit(void)
  1012. {
  1013. loopback_unregister_all();
  1014. }
  1015. module_init(alsa_card_loopback_init)
  1016. module_exit(alsa_card_loopback_exit)