aloop.c 35 KB

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