aloop.c 35 KB

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