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 int last_drift;
  110. unsigned long last_jiffies;
  111. struct timer_list timer;
  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. if (rate_shift != dpcm->pcm_rate_shift) {
  154. dpcm->pcm_rate_shift = rate_shift;
  155. dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
  156. }
  157. if (dpcm->period_size_frac <= dpcm->irq_pos) {
  158. dpcm->irq_pos %= dpcm->period_size_frac;
  159. dpcm->period_update_pending = 1;
  160. }
  161. tick = dpcm->period_size_frac - dpcm->irq_pos;
  162. tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
  163. dpcm->timer.expires = jiffies + tick;
  164. add_timer(&dpcm->timer);
  165. }
  166. static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
  167. {
  168. del_timer(&dpcm->timer);
  169. dpcm->timer.expires = 0;
  170. }
  171. #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
  172. #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
  173. #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
  174. static int loopback_check_format(struct loopback_cable *cable, int stream)
  175. {
  176. struct snd_pcm_runtime *runtime, *cruntime;
  177. struct loopback_setup *setup;
  178. struct snd_card *card;
  179. int check;
  180. if (cable->valid != CABLE_VALID_BOTH) {
  181. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  182. goto __notify;
  183. return 0;
  184. }
  185. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  186. substream->runtime;
  187. cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  188. substream->runtime;
  189. check = runtime->format != cruntime->format ||
  190. runtime->rate != cruntime->rate ||
  191. runtime->channels != cruntime->channels;
  192. if (!check)
  193. return 0;
  194. if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  195. return -EIO;
  196. } else {
  197. snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  198. substream, SNDRV_PCM_STATE_DRAINING);
  199. __notify:
  200. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  201. substream->runtime;
  202. setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  203. card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
  204. if (setup->format != runtime->format) {
  205. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  206. &setup->format_id);
  207. setup->format = runtime->format;
  208. }
  209. if (setup->rate != runtime->rate) {
  210. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  211. &setup->rate_id);
  212. setup->rate = runtime->rate;
  213. }
  214. if (setup->channels != runtime->channels) {
  215. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  216. &setup->channels_id);
  217. setup->channels = runtime->channels;
  218. }
  219. }
  220. return 0;
  221. }
  222. static void loopback_active_notify(struct loopback_pcm *dpcm)
  223. {
  224. snd_ctl_notify(dpcm->loopback->card,
  225. SNDRV_CTL_EVENT_MASK_VALUE,
  226. &get_setup(dpcm)->active_id);
  227. }
  228. static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
  229. {
  230. struct snd_pcm_runtime *runtime = substream->runtime;
  231. struct loopback_pcm *dpcm = runtime->private_data;
  232. struct loopback_cable *cable = dpcm->cable;
  233. int err, stream = 1 << substream->stream;
  234. switch (cmd) {
  235. case SNDRV_PCM_TRIGGER_START:
  236. err = loopback_check_format(cable, substream->stream);
  237. if (err < 0)
  238. return err;
  239. dpcm->last_jiffies = jiffies;
  240. dpcm->pcm_rate_shift = 0;
  241. dpcm->last_drift = 0;
  242. spin_lock(&cable->lock);
  243. cable->running |= stream;
  244. cable->pause &= ~stream;
  245. spin_unlock(&cable->lock);
  246. loopback_timer_start(dpcm);
  247. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  248. loopback_active_notify(dpcm);
  249. break;
  250. case SNDRV_PCM_TRIGGER_STOP:
  251. spin_lock(&cable->lock);
  252. cable->running &= ~stream;
  253. cable->pause &= ~stream;
  254. spin_unlock(&cable->lock);
  255. loopback_timer_stop(dpcm);
  256. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  257. loopback_active_notify(dpcm);
  258. break;
  259. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  260. spin_lock(&cable->lock);
  261. cable->pause |= stream;
  262. spin_unlock(&cable->lock);
  263. loopback_timer_stop(dpcm);
  264. break;
  265. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  266. spin_lock(&cable->lock);
  267. dpcm->last_jiffies = jiffies;
  268. cable->pause &= ~stream;
  269. spin_unlock(&cable->lock);
  270. loopback_timer_start(dpcm);
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. static void params_change_substream(struct loopback_pcm *dpcm,
  278. struct snd_pcm_runtime *runtime)
  279. {
  280. struct snd_pcm_runtime *dst_runtime;
  281. if (dpcm == NULL || dpcm->substream == NULL)
  282. return;
  283. dst_runtime = dpcm->substream->runtime;
  284. if (dst_runtime == NULL)
  285. return;
  286. dst_runtime->hw = dpcm->cable->hw;
  287. }
  288. static void params_change(struct snd_pcm_substream *substream)
  289. {
  290. struct snd_pcm_runtime *runtime = substream->runtime;
  291. struct loopback_pcm *dpcm = runtime->private_data;
  292. struct loopback_cable *cable = dpcm->cable;
  293. cable->hw.formats = (1ULL << runtime->format);
  294. cable->hw.rate_min = runtime->rate;
  295. cable->hw.rate_max = runtime->rate;
  296. cable->hw.channels_min = runtime->channels;
  297. cable->hw.channels_max = runtime->channels;
  298. params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  299. runtime);
  300. params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  301. runtime);
  302. }
  303. static int loopback_prepare(struct snd_pcm_substream *substream)
  304. {
  305. struct snd_pcm_runtime *runtime = substream->runtime;
  306. struct loopback_pcm *dpcm = runtime->private_data;
  307. struct loopback_cable *cable = dpcm->cable;
  308. int bps, salign;
  309. salign = (snd_pcm_format_width(runtime->format) *
  310. runtime->channels) / 8;
  311. bps = salign * runtime->rate;
  312. if (bps <= 0 || salign <= 0)
  313. return -EINVAL;
  314. dpcm->buf_pos = 0;
  315. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  316. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  317. /* clear capture buffer */
  318. dpcm->silent_size = dpcm->pcm_buffer_size;
  319. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  320. runtime->buffer_size * runtime->channels);
  321. }
  322. dpcm->irq_pos = 0;
  323. dpcm->period_update_pending = 0;
  324. dpcm->pcm_bps = bps;
  325. dpcm->pcm_salign = salign;
  326. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  327. mutex_lock(&dpcm->loopback->cable_lock);
  328. if (!(cable->valid & ~(1 << substream->stream)) ||
  329. (get_setup(dpcm)->notify &&
  330. substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  331. params_change(substream);
  332. cable->valid |= 1 << substream->stream;
  333. mutex_unlock(&dpcm->loopback->cable_lock);
  334. return 0;
  335. }
  336. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  337. {
  338. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  339. char *dst = runtime->dma_area;
  340. unsigned int dst_off = dpcm->buf_pos;
  341. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  342. return;
  343. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  344. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  345. for (;;) {
  346. unsigned int size = bytes;
  347. if (dst_off + size > dpcm->pcm_buffer_size)
  348. size = dpcm->pcm_buffer_size - dst_off;
  349. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  350. bytes_to_frames(runtime, size) *
  351. runtime->channels);
  352. dpcm->silent_size += size;
  353. bytes -= size;
  354. if (!bytes)
  355. break;
  356. dst_off = 0;
  357. }
  358. }
  359. static void copy_play_buf(struct loopback_pcm *play,
  360. struct loopback_pcm *capt,
  361. unsigned int bytes)
  362. {
  363. struct snd_pcm_runtime *runtime = play->substream->runtime;
  364. char *src = runtime->dma_area;
  365. char *dst = capt->substream->runtime->dma_area;
  366. unsigned int src_off = play->buf_pos;
  367. unsigned int dst_off = capt->buf_pos;
  368. unsigned int clear_bytes = 0;
  369. /* check if playback is draining, trim the capture copy size
  370. * when our pointer is at the end of playback ring buffer */
  371. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  372. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  373. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  374. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  375. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  376. appl_ptr1 += play->buf_pos / play->pcm_salign;
  377. if (appl_ptr < appl_ptr1)
  378. appl_ptr1 -= runtime->buffer_size;
  379. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  380. if (diff < bytes) {
  381. clear_bytes = bytes - diff;
  382. bytes = diff;
  383. }
  384. }
  385. for (;;) {
  386. unsigned int size = bytes;
  387. if (src_off + size > play->pcm_buffer_size)
  388. size = play->pcm_buffer_size - src_off;
  389. if (dst_off + size > capt->pcm_buffer_size)
  390. size = capt->pcm_buffer_size - dst_off;
  391. memcpy(dst + dst_off, src + src_off, size);
  392. capt->silent_size = 0;
  393. bytes -= size;
  394. if (!bytes)
  395. break;
  396. src_off = (src_off + size) % play->pcm_buffer_size;
  397. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  398. }
  399. if (clear_bytes > 0) {
  400. clear_capture_buf(capt, clear_bytes);
  401. capt->silent_size = 0;
  402. }
  403. }
  404. static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
  405. unsigned int jiffies_delta)
  406. {
  407. unsigned long last_pos;
  408. unsigned int delta;
  409. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  410. dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
  411. delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  412. if (delta >= dpcm->last_drift)
  413. delta -= dpcm->last_drift;
  414. dpcm->last_drift = 0;
  415. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  416. dpcm->irq_pos %= dpcm->period_size_frac;
  417. dpcm->period_update_pending = 1;
  418. }
  419. return delta;
  420. }
  421. static inline void bytepos_finish(struct loopback_pcm *dpcm,
  422. unsigned int delta)
  423. {
  424. dpcm->buf_pos += delta;
  425. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  426. }
  427. static unsigned int loopback_pos_update(struct loopback_cable *cable)
  428. {
  429. struct loopback_pcm *dpcm_play =
  430. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  431. struct loopback_pcm *dpcm_capt =
  432. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  433. unsigned long delta_play = 0, delta_capt = 0;
  434. unsigned int running, count1, count2;
  435. unsigned long flags;
  436. spin_lock_irqsave(&cable->lock, flags);
  437. running = cable->running ^ cable->pause;
  438. if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  439. delta_play = jiffies - dpcm_play->last_jiffies;
  440. dpcm_play->last_jiffies += delta_play;
  441. }
  442. if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  443. delta_capt = jiffies - dpcm_capt->last_jiffies;
  444. dpcm_capt->last_jiffies += delta_capt;
  445. }
  446. if (delta_play == 0 && delta_capt == 0)
  447. goto unlock;
  448. if (delta_play > delta_capt) {
  449. count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
  450. bytepos_finish(dpcm_play, count1);
  451. delta_play = delta_capt;
  452. } else if (delta_play < delta_capt) {
  453. count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
  454. clear_capture_buf(dpcm_capt, count1);
  455. bytepos_finish(dpcm_capt, count1);
  456. delta_capt = delta_play;
  457. }
  458. if (delta_play == 0 && delta_capt == 0)
  459. goto unlock;
  460. /* note delta_capt == delta_play at this moment */
  461. count1 = bytepos_delta(dpcm_play, delta_play);
  462. count2 = bytepos_delta(dpcm_capt, delta_capt);
  463. if (count1 < count2) {
  464. dpcm_capt->last_drift = count2 - count1;
  465. count1 = count2;
  466. } else if (count1 > count2) {
  467. dpcm_play->last_drift = count1 - count2;
  468. }
  469. copy_play_buf(dpcm_play, dpcm_capt, count1);
  470. bytepos_finish(dpcm_play, count1);
  471. bytepos_finish(dpcm_capt, count1);
  472. unlock:
  473. spin_unlock_irqrestore(&cable->lock, flags);
  474. return running;
  475. }
  476. static void loopback_timer_function(unsigned long data)
  477. {
  478. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  479. unsigned int running;
  480. running = loopback_pos_update(dpcm->cable);
  481. if (running & (1 << dpcm->substream->stream)) {
  482. loopback_timer_start(dpcm);
  483. if (dpcm->period_update_pending) {
  484. dpcm->period_update_pending = 0;
  485. snd_pcm_period_elapsed(dpcm->substream);
  486. }
  487. }
  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. loopback_pos_update(dpcm->cable);
  494. return bytes_to_frames(runtime, dpcm->buf_pos);
  495. }
  496. static struct snd_pcm_hardware loopback_pcm_hardware =
  497. {
  498. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  499. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  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. cable = loopback->cables[substream->number][dev];
  598. if (!cable) {
  599. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  600. if (!cable) {
  601. kfree(dpcm);
  602. err = -ENOMEM;
  603. goto unlock;
  604. }
  605. spin_lock_init(&cable->lock);
  606. cable->hw = loopback_pcm_hardware;
  607. loopback->cables[substream->number][dev] = cable;
  608. }
  609. dpcm->cable = cable;
  610. cable->streams[substream->stream] = dpcm;
  611. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  612. /* use dynamic rules based on actual runtime->hw values */
  613. /* note that the default rules created in the PCM midlevel code */
  614. /* are cached -> they do not reflect the actual state */
  615. err = snd_pcm_hw_rule_add(runtime, 0,
  616. SNDRV_PCM_HW_PARAM_FORMAT,
  617. rule_format, &runtime->hw,
  618. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  619. if (err < 0)
  620. goto unlock;
  621. err = snd_pcm_hw_rule_add(runtime, 0,
  622. SNDRV_PCM_HW_PARAM_RATE,
  623. rule_rate, &runtime->hw,
  624. SNDRV_PCM_HW_PARAM_RATE, -1);
  625. if (err < 0)
  626. goto unlock;
  627. err = snd_pcm_hw_rule_add(runtime, 0,
  628. SNDRV_PCM_HW_PARAM_CHANNELS,
  629. rule_channels, &runtime->hw,
  630. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  631. if (err < 0)
  632. goto unlock;
  633. runtime->private_data = dpcm;
  634. runtime->private_free = loopback_runtime_free;
  635. if (get_notify(dpcm))
  636. runtime->hw = loopback_pcm_hardware;
  637. else
  638. runtime->hw = cable->hw;
  639. unlock:
  640. mutex_unlock(&loopback->cable_lock);
  641. return err;
  642. }
  643. static int loopback_close(struct snd_pcm_substream *substream)
  644. {
  645. struct loopback *loopback = substream->private_data;
  646. struct loopback_pcm *dpcm = substream->runtime->private_data;
  647. struct loopback_cable *cable;
  648. int dev = get_cable_index(substream);
  649. loopback_timer_stop(dpcm);
  650. mutex_lock(&loopback->cable_lock);
  651. cable = loopback->cables[substream->number][dev];
  652. if (cable->streams[!substream->stream]) {
  653. /* other stream is still alive */
  654. cable->streams[substream->stream] = NULL;
  655. } else {
  656. /* free the cable */
  657. loopback->cables[substream->number][dev] = NULL;
  658. kfree(cable);
  659. }
  660. mutex_unlock(&loopback->cable_lock);
  661. return 0;
  662. }
  663. static struct snd_pcm_ops loopback_playback_ops = {
  664. .open = loopback_open,
  665. .close = loopback_close,
  666. .ioctl = snd_pcm_lib_ioctl,
  667. .hw_params = loopback_hw_params,
  668. .hw_free = loopback_hw_free,
  669. .prepare = loopback_prepare,
  670. .trigger = loopback_trigger,
  671. .pointer = loopback_pointer,
  672. .page = snd_pcm_lib_get_vmalloc_page,
  673. .mmap = snd_pcm_lib_mmap_vmalloc,
  674. };
  675. static struct snd_pcm_ops loopback_capture_ops = {
  676. .open = loopback_open,
  677. .close = loopback_close,
  678. .ioctl = snd_pcm_lib_ioctl,
  679. .hw_params = loopback_hw_params,
  680. .hw_free = loopback_hw_free,
  681. .prepare = loopback_prepare,
  682. .trigger = loopback_trigger,
  683. .pointer = loopback_pointer,
  684. .page = snd_pcm_lib_get_vmalloc_page,
  685. .mmap = snd_pcm_lib_mmap_vmalloc,
  686. };
  687. static int __devinit loopback_pcm_new(struct loopback *loopback,
  688. int device, int substreams)
  689. {
  690. struct snd_pcm *pcm;
  691. int err;
  692. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  693. substreams, substreams, &pcm);
  694. if (err < 0)
  695. return err;
  696. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  697. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  698. pcm->private_data = loopback;
  699. pcm->info_flags = 0;
  700. strcpy(pcm->name, "Loopback PCM");
  701. loopback->pcm[device] = pcm;
  702. return 0;
  703. }
  704. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  705. struct snd_ctl_elem_info *uinfo)
  706. {
  707. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  708. uinfo->count = 1;
  709. uinfo->value.integer.min = 80000;
  710. uinfo->value.integer.max = 120000;
  711. uinfo->value.integer.step = 1;
  712. return 0;
  713. }
  714. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  715. struct snd_ctl_elem_value *ucontrol)
  716. {
  717. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  718. ucontrol->value.integer.value[0] =
  719. loopback->setup[kcontrol->id.subdevice]
  720. [kcontrol->id.device].rate_shift;
  721. return 0;
  722. }
  723. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  724. struct snd_ctl_elem_value *ucontrol)
  725. {
  726. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  727. unsigned int val;
  728. int change = 0;
  729. val = ucontrol->value.integer.value[0];
  730. if (val < 80000)
  731. val = 80000;
  732. if (val > 120000)
  733. val = 120000;
  734. mutex_lock(&loopback->cable_lock);
  735. if (val != loopback->setup[kcontrol->id.subdevice]
  736. [kcontrol->id.device].rate_shift) {
  737. loopback->setup[kcontrol->id.subdevice]
  738. [kcontrol->id.device].rate_shift = val;
  739. change = 1;
  740. }
  741. mutex_unlock(&loopback->cable_lock);
  742. return change;
  743. }
  744. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  745. struct snd_ctl_elem_value *ucontrol)
  746. {
  747. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  748. ucontrol->value.integer.value[0] =
  749. loopback->setup[kcontrol->id.subdevice]
  750. [kcontrol->id.device].notify;
  751. return 0;
  752. }
  753. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  754. struct snd_ctl_elem_value *ucontrol)
  755. {
  756. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  757. unsigned int val;
  758. int change = 0;
  759. val = ucontrol->value.integer.value[0] ? 1 : 0;
  760. if (val != loopback->setup[kcontrol->id.subdevice]
  761. [kcontrol->id.device].notify) {
  762. loopback->setup[kcontrol->id.subdevice]
  763. [kcontrol->id.device].notify = val;
  764. change = 1;
  765. }
  766. return change;
  767. }
  768. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  769. struct snd_ctl_elem_value *ucontrol)
  770. {
  771. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  772. struct loopback_cable *cable = loopback->cables
  773. [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  774. unsigned int val = 0;
  775. if (cable != NULL)
  776. val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  777. 1 : 0;
  778. ucontrol->value.integer.value[0] = val;
  779. return 0;
  780. }
  781. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  782. struct snd_ctl_elem_info *uinfo)
  783. {
  784. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  785. uinfo->count = 1;
  786. uinfo->value.integer.min = 0;
  787. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  788. uinfo->value.integer.step = 1;
  789. return 0;
  790. }
  791. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  792. struct snd_ctl_elem_value *ucontrol)
  793. {
  794. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  795. ucontrol->value.integer.value[0] =
  796. loopback->setup[kcontrol->id.subdevice]
  797. [kcontrol->id.device].format;
  798. return 0;
  799. }
  800. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  801. struct snd_ctl_elem_info *uinfo)
  802. {
  803. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  804. uinfo->count = 1;
  805. uinfo->value.integer.min = 0;
  806. uinfo->value.integer.max = 192000;
  807. uinfo->value.integer.step = 1;
  808. return 0;
  809. }
  810. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  811. struct snd_ctl_elem_value *ucontrol)
  812. {
  813. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  814. ucontrol->value.integer.value[0] =
  815. loopback->setup[kcontrol->id.subdevice]
  816. [kcontrol->id.device].rate;
  817. return 0;
  818. }
  819. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  820. struct snd_ctl_elem_info *uinfo)
  821. {
  822. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  823. uinfo->count = 1;
  824. uinfo->value.integer.min = 1;
  825. uinfo->value.integer.max = 1024;
  826. uinfo->value.integer.step = 1;
  827. return 0;
  828. }
  829. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  830. struct snd_ctl_elem_value *ucontrol)
  831. {
  832. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  833. ucontrol->value.integer.value[0] =
  834. loopback->setup[kcontrol->id.subdevice]
  835. [kcontrol->id.device].channels;
  836. return 0;
  837. }
  838. static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
  839. {
  840. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  841. .name = "PCM Rate Shift 100000",
  842. .info = loopback_rate_shift_info,
  843. .get = loopback_rate_shift_get,
  844. .put = loopback_rate_shift_put,
  845. },
  846. {
  847. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  848. .name = "PCM Notify",
  849. .info = snd_ctl_boolean_mono_info,
  850. .get = loopback_notify_get,
  851. .put = loopback_notify_put,
  852. },
  853. #define ACTIVE_IDX 2
  854. {
  855. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  856. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  857. .name = "PCM Slave Active",
  858. .info = snd_ctl_boolean_mono_info,
  859. .get = loopback_active_get,
  860. },
  861. #define FORMAT_IDX 3
  862. {
  863. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  864. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  865. .name = "PCM Slave Format",
  866. .info = loopback_format_info,
  867. .get = loopback_format_get
  868. },
  869. #define RATE_IDX 4
  870. {
  871. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  872. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  873. .name = "PCM Slave Rate",
  874. .info = loopback_rate_info,
  875. .get = loopback_rate_get
  876. },
  877. #define CHANNELS_IDX 5
  878. {
  879. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  880. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  881. .name = "PCM Slave Channels",
  882. .info = loopback_channels_info,
  883. .get = loopback_channels_get
  884. }
  885. };
  886. static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
  887. {
  888. struct snd_card *card = loopback->card;
  889. struct snd_pcm *pcm;
  890. struct snd_kcontrol *kctl;
  891. struct loopback_setup *setup;
  892. int err, dev, substr, substr_count, idx;
  893. strcpy(card->mixername, "Loopback Mixer");
  894. for (dev = 0; dev < 2; dev++) {
  895. pcm = loopback->pcm[dev];
  896. substr_count =
  897. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  898. for (substr = 0; substr < substr_count; substr++) {
  899. setup = &loopback->setup[substr][dev];
  900. setup->notify = notify;
  901. setup->rate_shift = NO_PITCH;
  902. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  903. setup->rate = 48000;
  904. setup->channels = 2;
  905. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  906. idx++) {
  907. kctl = snd_ctl_new1(&loopback_controls[idx],
  908. loopback);
  909. if (!kctl)
  910. return -ENOMEM;
  911. kctl->id.device = dev;
  912. kctl->id.subdevice = substr;
  913. switch (idx) {
  914. case ACTIVE_IDX:
  915. setup->active_id = kctl->id;
  916. break;
  917. case FORMAT_IDX:
  918. setup->format_id = kctl->id;
  919. break;
  920. case RATE_IDX:
  921. setup->rate_id = kctl->id;
  922. break;
  923. case CHANNELS_IDX:
  924. setup->channels_id = kctl->id;
  925. break;
  926. default:
  927. break;
  928. }
  929. err = snd_ctl_add(card, kctl);
  930. if (err < 0)
  931. return err;
  932. }
  933. }
  934. }
  935. return 0;
  936. }
  937. #ifdef CONFIG_PROC_FS
  938. static void print_dpcm_info(struct snd_info_buffer *buffer,
  939. struct loopback_pcm *dpcm,
  940. const char *id)
  941. {
  942. snd_iprintf(buffer, " %s\n", id);
  943. if (dpcm == NULL) {
  944. snd_iprintf(buffer, " inactive\n");
  945. return;
  946. }
  947. snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
  948. snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
  949. snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
  950. snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
  951. snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
  952. snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
  953. snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
  954. snd_iprintf(buffer, " update_pending:\t%u\n",
  955. dpcm->period_update_pending);
  956. snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
  957. snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
  958. snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
  959. dpcm->last_jiffies, jiffies);
  960. snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
  961. }
  962. static void print_substream_info(struct snd_info_buffer *buffer,
  963. struct loopback *loopback,
  964. int sub,
  965. int num)
  966. {
  967. struct loopback_cable *cable = loopback->cables[sub][num];
  968. snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
  969. if (cable == NULL) {
  970. snd_iprintf(buffer, " inactive\n");
  971. return;
  972. }
  973. snd_iprintf(buffer, " valid: %u\n", cable->valid);
  974. snd_iprintf(buffer, " running: %u\n", cable->running);
  975. snd_iprintf(buffer, " pause: %u\n", cable->pause);
  976. print_dpcm_info(buffer, cable->streams[0], "Playback");
  977. print_dpcm_info(buffer, cable->streams[1], "Capture");
  978. }
  979. static void print_cable_info(struct snd_info_entry *entry,
  980. struct snd_info_buffer *buffer)
  981. {
  982. struct loopback *loopback = entry->private_data;
  983. int sub, num;
  984. mutex_lock(&loopback->cable_lock);
  985. num = entry->name[strlen(entry->name)-1];
  986. num = num == '0' ? 0 : 1;
  987. for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
  988. print_substream_info(buffer, loopback, sub, num);
  989. mutex_unlock(&loopback->cable_lock);
  990. }
  991. static int __devinit loopback_proc_new(struct loopback *loopback, int cidx)
  992. {
  993. char name[32];
  994. struct snd_info_entry *entry;
  995. int err;
  996. snprintf(name, sizeof(name), "cable#%d", cidx);
  997. err = snd_card_proc_new(loopback->card, name, &entry);
  998. if (err < 0)
  999. return err;
  1000. snd_info_set_text_ops(entry, loopback, print_cable_info);
  1001. return 0;
  1002. }
  1003. #else /* !CONFIG_PROC_FS */
  1004. #define loopback_proc_new(loopback, cidx) do { } while (0)
  1005. #endif
  1006. static int __devinit loopback_probe(struct platform_device *devptr)
  1007. {
  1008. struct snd_card *card;
  1009. struct loopback *loopback;
  1010. int dev = devptr->id;
  1011. int err;
  1012. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  1013. sizeof(struct loopback), &card);
  1014. if (err < 0)
  1015. return err;
  1016. loopback = card->private_data;
  1017. if (pcm_substreams[dev] < 1)
  1018. pcm_substreams[dev] = 1;
  1019. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  1020. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  1021. loopback->card = card;
  1022. mutex_init(&loopback->cable_lock);
  1023. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  1024. if (err < 0)
  1025. goto __nodev;
  1026. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  1027. if (err < 0)
  1028. goto __nodev;
  1029. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  1030. if (err < 0)
  1031. goto __nodev;
  1032. loopback_proc_new(loopback, 0);
  1033. loopback_proc_new(loopback, 1);
  1034. strcpy(card->driver, "Loopback");
  1035. strcpy(card->shortname, "Loopback");
  1036. sprintf(card->longname, "Loopback %i", dev + 1);
  1037. err = snd_card_register(card);
  1038. if (!err) {
  1039. platform_set_drvdata(devptr, card);
  1040. return 0;
  1041. }
  1042. __nodev:
  1043. snd_card_free(card);
  1044. return err;
  1045. }
  1046. static int __devexit loopback_remove(struct platform_device *devptr)
  1047. {
  1048. snd_card_free(platform_get_drvdata(devptr));
  1049. platform_set_drvdata(devptr, NULL);
  1050. return 0;
  1051. }
  1052. #ifdef CONFIG_PM
  1053. static int loopback_suspend(struct device *pdev)
  1054. {
  1055. struct snd_card *card = dev_get_drvdata(pdev);
  1056. struct loopback *loopback = card->private_data;
  1057. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1058. snd_pcm_suspend_all(loopback->pcm[0]);
  1059. snd_pcm_suspend_all(loopback->pcm[1]);
  1060. return 0;
  1061. }
  1062. static int loopback_resume(struct device *pdev)
  1063. {
  1064. struct snd_card *card = dev_get_drvdata(pdev);
  1065. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1066. return 0;
  1067. }
  1068. static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
  1069. #define LOOPBACK_PM_OPS &loopback_pm
  1070. #else
  1071. #define LOOPBACK_PM_OPS NULL
  1072. #endif
  1073. #define SND_LOOPBACK_DRIVER "snd_aloop"
  1074. static struct platform_driver loopback_driver = {
  1075. .probe = loopback_probe,
  1076. .remove = __devexit_p(loopback_remove),
  1077. .driver = {
  1078. .name = SND_LOOPBACK_DRIVER,
  1079. .owner = THIS_MODULE,
  1080. .pm = LOOPBACK_PM_OPS,
  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)