aloop.c 32 KB

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