aloop.c 36 KB

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