aloop.c 34 KB

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