em28xx-audio.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * Empiatech em28x1 audio extension
  3. *
  4. * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
  5. *
  6. * Copyright (C) 2007-2014 Mauro Carvalho Chehab
  7. * - Port to work with the in-kernel driver
  8. * - Cleanups, fixes, alsa-controls, etc.
  9. *
  10. * This driver is based on my previous au600 usb pstn audio driver
  11. * and inherits all the copyrights
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/usb.h>
  29. #include <linux/init.h>
  30. #include <linux/sound.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/soundcard.h>
  33. #include <linux/slab.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/module.h>
  37. #include <sound/core.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/info.h>
  41. #include <sound/initval.h>
  42. #include <sound/control.h>
  43. #include <sound/tlv.h>
  44. #include <sound/ac97_codec.h>
  45. #include <media/v4l2-common.h>
  46. #include "em28xx.h"
  47. static int debug;
  48. module_param(debug, int, 0644);
  49. MODULE_PARM_DESC(debug, "activates debug info");
  50. #define EM28XX_MAX_AUDIO_BUFS 5
  51. #define EM28XX_MIN_AUDIO_PACKETS 64
  52. #define dprintk(fmt, arg...) do { \
  53. if (debug) \
  54. printk(KERN_INFO "em28xx-audio %s: " fmt, \
  55. __func__, ##arg); \
  56. } while (0)
  57. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  58. static int em28xx_deinit_isoc_audio(struct em28xx *dev)
  59. {
  60. int i;
  61. dprintk("Stopping isoc\n");
  62. for (i = 0; i < dev->adev.num_urb; i++) {
  63. struct urb *urb = dev->adev.urb[i];
  64. if (!irqs_disabled())
  65. usb_kill_urb(urb);
  66. else
  67. usb_unlink_urb(urb);
  68. }
  69. return 0;
  70. }
  71. static void em28xx_audio_isocirq(struct urb *urb)
  72. {
  73. struct em28xx *dev = urb->context;
  74. int i;
  75. unsigned int oldptr;
  76. int period_elapsed = 0;
  77. int status;
  78. unsigned char *cp;
  79. unsigned int stride;
  80. struct snd_pcm_substream *substream;
  81. struct snd_pcm_runtime *runtime;
  82. if (dev->disconnected) {
  83. dprintk("device disconnected while streaming. URB status=%d.\n", urb->status);
  84. atomic_set(&dev->adev.stream_started, 0);
  85. return;
  86. }
  87. switch (urb->status) {
  88. case 0: /* success */
  89. case -ETIMEDOUT: /* NAK */
  90. break;
  91. case -ECONNRESET: /* kill */
  92. case -ENOENT:
  93. case -ESHUTDOWN:
  94. return;
  95. default: /* error */
  96. dprintk("urb completition error %d.\n", urb->status);
  97. break;
  98. }
  99. if (atomic_read(&dev->adev.stream_started) == 0)
  100. return;
  101. if (dev->adev.capture_pcm_substream) {
  102. substream = dev->adev.capture_pcm_substream;
  103. runtime = substream->runtime;
  104. stride = runtime->frame_bits >> 3;
  105. for (i = 0; i < urb->number_of_packets; i++) {
  106. int length =
  107. urb->iso_frame_desc[i].actual_length / stride;
  108. cp = (unsigned char *)urb->transfer_buffer +
  109. urb->iso_frame_desc[i].offset;
  110. if (!length)
  111. continue;
  112. oldptr = dev->adev.hwptr_done_capture;
  113. if (oldptr + length >= runtime->buffer_size) {
  114. unsigned int cnt =
  115. runtime->buffer_size - oldptr;
  116. memcpy(runtime->dma_area + oldptr * stride, cp,
  117. cnt * stride);
  118. memcpy(runtime->dma_area, cp + cnt * stride,
  119. length * stride - cnt * stride);
  120. } else {
  121. memcpy(runtime->dma_area + oldptr * stride, cp,
  122. length * stride);
  123. }
  124. snd_pcm_stream_lock(substream);
  125. dev->adev.hwptr_done_capture += length;
  126. if (dev->adev.hwptr_done_capture >=
  127. runtime->buffer_size)
  128. dev->adev.hwptr_done_capture -=
  129. runtime->buffer_size;
  130. dev->adev.capture_transfer_done += length;
  131. if (dev->adev.capture_transfer_done >=
  132. runtime->period_size) {
  133. dev->adev.capture_transfer_done -=
  134. runtime->period_size;
  135. period_elapsed = 1;
  136. }
  137. snd_pcm_stream_unlock(substream);
  138. }
  139. if (period_elapsed)
  140. snd_pcm_period_elapsed(substream);
  141. }
  142. urb->status = 0;
  143. status = usb_submit_urb(urb, GFP_ATOMIC);
  144. if (status < 0)
  145. em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
  146. status);
  147. return;
  148. }
  149. static int em28xx_init_audio_isoc(struct em28xx *dev)
  150. {
  151. int i, errCode;
  152. dprintk("Starting isoc transfers\n");
  153. /* Start streaming */
  154. for (i = 0; i < dev->adev.num_urb; i++) {
  155. memset(dev->adev.transfer_buffer[i], 0x80,
  156. dev->adev.urb[i]->transfer_buffer_length);
  157. errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
  158. if (errCode) {
  159. em28xx_errdev("submit of audio urb failed (error=%i)\n",
  160. errCode);
  161. em28xx_deinit_isoc_audio(dev);
  162. atomic_set(&dev->adev.stream_started, 0);
  163. return errCode;
  164. }
  165. }
  166. return 0;
  167. }
  168. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  169. size_t size)
  170. {
  171. struct snd_pcm_runtime *runtime = subs->runtime;
  172. dprintk("Allocating vbuffer\n");
  173. if (runtime->dma_area) {
  174. if (runtime->dma_bytes > size)
  175. return 0;
  176. vfree(runtime->dma_area);
  177. }
  178. runtime->dma_area = vmalloc(size);
  179. if (!runtime->dma_area)
  180. return -ENOMEM;
  181. runtime->dma_bytes = size;
  182. return 0;
  183. }
  184. static struct snd_pcm_hardware snd_em28xx_hw_capture = {
  185. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  186. SNDRV_PCM_INFO_MMAP |
  187. SNDRV_PCM_INFO_INTERLEAVED |
  188. SNDRV_PCM_INFO_BATCH |
  189. SNDRV_PCM_INFO_MMAP_VALID,
  190. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  191. .rates = SNDRV_PCM_RATE_48000,
  192. .rate_min = 48000,
  193. .rate_max = 48000,
  194. .channels_min = 2,
  195. .channels_max = 2,
  196. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  197. /*
  198. * The period is 12.288 bytes. Allow a 10% of variation along its
  199. * value, in order to avoid overruns/underruns due to some clock
  200. * drift.
  201. *
  202. * FIXME: This period assumes 64 packets, and a 48000 PCM rate.
  203. * Calculate it dynamically.
  204. */
  205. .period_bytes_min = 11059,
  206. .period_bytes_max = 13516,
  207. .periods_min = 2,
  208. .periods_max = 98, /* 12544, */
  209. };
  210. static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
  211. {
  212. struct em28xx *dev = snd_pcm_substream_chip(substream);
  213. struct snd_pcm_runtime *runtime = substream->runtime;
  214. int nonblock, ret = 0;
  215. if (!dev) {
  216. em28xx_err("BUG: em28xx can't find device struct."
  217. " Can't proceed with open\n");
  218. return -ENODEV;
  219. }
  220. if (dev->disconnected)
  221. return -ENODEV;
  222. dprintk("opening device and trying to acquire exclusive lock\n");
  223. nonblock = !!(substream->f_flags & O_NONBLOCK);
  224. if (nonblock) {
  225. if (!mutex_trylock(&dev->lock))
  226. return -EAGAIN;
  227. } else
  228. mutex_lock(&dev->lock);
  229. runtime->hw = snd_em28xx_hw_capture;
  230. if (dev->adev.users == 0) {
  231. if (dev->alt == 0 || dev->is_audio_only) {
  232. if (dev->is_audio_only)
  233. /* audio is on a separate interface */
  234. dev->alt = 1;
  235. else
  236. /* audio is on the same interface as video */
  237. dev->alt = 7;
  238. /*
  239. * FIXME: The intention seems to be to select
  240. * the alt setting with the largest
  241. * wMaxPacketSize for the video endpoint.
  242. * At least dev->alt should be used instead, but
  243. * we should probably not touch it at all if it
  244. * is already >0, because wMaxPacketSize of the
  245. * audio endpoints seems to be the same for all.
  246. */
  247. dprintk("changing alternate number on interface %d to %d\n",
  248. dev->ifnum, dev->alt);
  249. usb_set_interface(dev->udev, dev->ifnum, dev->alt);
  250. }
  251. /* Sets volume, mute, etc */
  252. dev->mute = 0;
  253. ret = em28xx_audio_analog_set(dev);
  254. if (ret < 0)
  255. goto err;
  256. }
  257. kref_get(&dev->ref);
  258. dev->adev.users++;
  259. mutex_unlock(&dev->lock);
  260. /* Dynamically adjust the period size */
  261. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  262. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  263. dev->adev.period * 95 / 100,
  264. dev->adev.period * 105 / 100);
  265. dev->adev.capture_pcm_substream = substream;
  266. return 0;
  267. err:
  268. mutex_unlock(&dev->lock);
  269. em28xx_err("Error while configuring em28xx mixer\n");
  270. return ret;
  271. }
  272. static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
  273. {
  274. struct em28xx *dev = snd_pcm_substream_chip(substream);
  275. dprintk("closing device\n");
  276. dev->mute = 1;
  277. mutex_lock(&dev->lock);
  278. dev->adev.users--;
  279. if (atomic_read(&dev->adev.stream_started) > 0) {
  280. atomic_set(&dev->adev.stream_started, 0);
  281. schedule_work(&dev->adev.wq_trigger);
  282. }
  283. em28xx_audio_analog_set(dev);
  284. if (substream->runtime->dma_area) {
  285. dprintk("freeing\n");
  286. vfree(substream->runtime->dma_area);
  287. substream->runtime->dma_area = NULL;
  288. }
  289. mutex_unlock(&dev->lock);
  290. kref_put(&dev->ref, em28xx_free_device);
  291. return 0;
  292. }
  293. static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
  294. struct snd_pcm_hw_params *hw_params)
  295. {
  296. int ret;
  297. struct em28xx *dev = snd_pcm_substream_chip(substream);
  298. if (dev->disconnected)
  299. return -ENODEV;
  300. dprintk("Setting capture parameters\n");
  301. ret = snd_pcm_alloc_vmalloc_buffer(substream,
  302. params_buffer_bytes(hw_params));
  303. if (ret < 0)
  304. return ret;
  305. #if 0
  306. /* TODO: set up em28xx audio chip to deliver the correct audio format,
  307. current default is 48000hz multiplexed => 96000hz mono
  308. which shouldn't matter since analogue TV only supports mono */
  309. unsigned int channels, rate, format;
  310. format = params_format(hw_params);
  311. rate = params_rate(hw_params);
  312. channels = params_channels(hw_params);
  313. #endif
  314. return 0;
  315. }
  316. static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
  317. {
  318. struct em28xx *dev = snd_pcm_substream_chip(substream);
  319. struct em28xx_audio *adev = &dev->adev;
  320. dprintk("Stop capture, if needed\n");
  321. if (atomic_read(&adev->stream_started) > 0) {
  322. atomic_set(&adev->stream_started, 0);
  323. schedule_work(&adev->wq_trigger);
  324. }
  325. return 0;
  326. }
  327. static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
  328. {
  329. struct em28xx *dev = snd_pcm_substream_chip(substream);
  330. if (dev->disconnected)
  331. return -ENODEV;
  332. dev->adev.hwptr_done_capture = 0;
  333. dev->adev.capture_transfer_done = 0;
  334. return 0;
  335. }
  336. static void audio_trigger(struct work_struct *work)
  337. {
  338. struct em28xx_audio *adev =
  339. container_of(work, struct em28xx_audio, wq_trigger);
  340. struct em28xx *dev = container_of(adev, struct em28xx, adev);
  341. if (atomic_read(&adev->stream_started)) {
  342. dprintk("starting capture");
  343. em28xx_init_audio_isoc(dev);
  344. } else {
  345. dprintk("stopping capture");
  346. em28xx_deinit_isoc_audio(dev);
  347. }
  348. }
  349. static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
  350. int cmd)
  351. {
  352. struct em28xx *dev = snd_pcm_substream_chip(substream);
  353. int retval = 0;
  354. if (dev->disconnected)
  355. return -ENODEV;
  356. switch (cmd) {
  357. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  358. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  359. case SNDRV_PCM_TRIGGER_START:
  360. atomic_set(&dev->adev.stream_started, 1);
  361. break;
  362. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  363. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  364. case SNDRV_PCM_TRIGGER_STOP:
  365. atomic_set(&dev->adev.stream_started, 0);
  366. break;
  367. default:
  368. retval = -EINVAL;
  369. }
  370. schedule_work(&dev->adev.wq_trigger);
  371. return retval;
  372. }
  373. static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
  374. *substream)
  375. {
  376. unsigned long flags;
  377. struct em28xx *dev;
  378. snd_pcm_uframes_t hwptr_done;
  379. dev = snd_pcm_substream_chip(substream);
  380. if (dev->disconnected)
  381. return SNDRV_PCM_POS_XRUN;
  382. spin_lock_irqsave(&dev->adev.slock, flags);
  383. hwptr_done = dev->adev.hwptr_done_capture;
  384. spin_unlock_irqrestore(&dev->adev.slock, flags);
  385. return hwptr_done;
  386. }
  387. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  388. unsigned long offset)
  389. {
  390. void *pageptr = subs->runtime->dma_area + offset;
  391. return vmalloc_to_page(pageptr);
  392. }
  393. /*
  394. * AC97 volume control support
  395. */
  396. static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
  397. struct snd_ctl_elem_info *info)
  398. {
  399. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  400. if (dev->disconnected)
  401. return -ENODEV;
  402. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  403. info->count = 2;
  404. info->value.integer.min = 0;
  405. info->value.integer.max = 0x1f;
  406. return 0;
  407. }
  408. static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
  409. struct snd_ctl_elem_value *value)
  410. {
  411. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  412. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  413. u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
  414. (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
  415. int nonblock = 0;
  416. int rc;
  417. if (dev->disconnected)
  418. return -ENODEV;
  419. if (substream)
  420. nonblock = !!(substream->f_flags & O_NONBLOCK);
  421. if (nonblock) {
  422. if (!mutex_trylock(&dev->lock))
  423. return -EAGAIN;
  424. } else
  425. mutex_lock(&dev->lock);
  426. rc = em28xx_read_ac97(dev, kcontrol->private_value);
  427. if (rc < 0)
  428. goto err;
  429. val |= rc & 0x8000; /* Preserve the mute flag */
  430. rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
  431. if (rc < 0)
  432. goto err;
  433. dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
  434. (val & 0x8000) ? "muted " : "",
  435. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  436. val, (int)kcontrol->private_value);
  437. err:
  438. mutex_unlock(&dev->lock);
  439. return rc;
  440. }
  441. static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
  442. struct snd_ctl_elem_value *value)
  443. {
  444. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  445. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  446. int nonblock = 0;
  447. int val;
  448. if (dev->disconnected)
  449. return -ENODEV;
  450. if (substream)
  451. nonblock = !!(substream->f_flags & O_NONBLOCK);
  452. if (nonblock) {
  453. if (!mutex_trylock(&dev->lock))
  454. return -EAGAIN;
  455. } else
  456. mutex_lock(&dev->lock);
  457. val = em28xx_read_ac97(dev, kcontrol->private_value);
  458. mutex_unlock(&dev->lock);
  459. if (val < 0)
  460. return val;
  461. dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
  462. (val & 0x8000) ? "muted " : "",
  463. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  464. val, (int)kcontrol->private_value);
  465. value->value.integer.value[0] = 0x1f - (val & 0x1f);
  466. value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
  467. return 0;
  468. }
  469. static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
  470. struct snd_ctl_elem_value *value)
  471. {
  472. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  473. u16 val = value->value.integer.value[0];
  474. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  475. int nonblock = 0;
  476. int rc;
  477. if (dev->disconnected)
  478. return -ENODEV;
  479. if (substream)
  480. nonblock = !!(substream->f_flags & O_NONBLOCK);
  481. if (nonblock) {
  482. if (!mutex_trylock(&dev->lock))
  483. return -EAGAIN;
  484. } else
  485. mutex_lock(&dev->lock);
  486. rc = em28xx_read_ac97(dev, kcontrol->private_value);
  487. if (rc < 0)
  488. goto err;
  489. if (val)
  490. rc &= 0x1f1f;
  491. else
  492. rc |= 0x8000;
  493. rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
  494. if (rc < 0)
  495. goto err;
  496. dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
  497. (val & 0x8000) ? "muted " : "",
  498. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  499. val, (int)kcontrol->private_value);
  500. err:
  501. mutex_unlock(&dev->lock);
  502. return rc;
  503. }
  504. static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
  505. struct snd_ctl_elem_value *value)
  506. {
  507. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  508. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  509. int nonblock = 0;
  510. int val;
  511. if (dev->disconnected)
  512. return -ENODEV;
  513. if (substream)
  514. nonblock = !!(substream->f_flags & O_NONBLOCK);
  515. if (nonblock) {
  516. if (!mutex_trylock(&dev->lock))
  517. return -EAGAIN;
  518. } else
  519. mutex_lock(&dev->lock);
  520. val = em28xx_read_ac97(dev, kcontrol->private_value);
  521. mutex_unlock(&dev->lock);
  522. if (val < 0)
  523. return val;
  524. if (val & 0x8000)
  525. value->value.integer.value[0] = 0;
  526. else
  527. value->value.integer.value[0] = 1;
  528. dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
  529. (val & 0x8000) ? "muted " : "",
  530. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  531. val, (int)kcontrol->private_value);
  532. return 0;
  533. }
  534. static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
  535. static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
  536. char *name, int id)
  537. {
  538. int err;
  539. char ctl_name[44];
  540. struct snd_kcontrol *kctl;
  541. struct snd_kcontrol_new tmp;
  542. memset(&tmp, 0, sizeof(tmp));
  543. tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  544. tmp.private_value = id,
  545. tmp.name = ctl_name,
  546. /* Add Mute Control */
  547. sprintf(ctl_name, "%s Switch", name);
  548. tmp.get = em28xx_vol_get_mute;
  549. tmp.put = em28xx_vol_put_mute;
  550. tmp.info = snd_ctl_boolean_mono_info;
  551. kctl = snd_ctl_new1(&tmp, dev);
  552. err = snd_ctl_add(card, kctl);
  553. if (err < 0)
  554. return err;
  555. dprintk("Added control %s for ac97 volume control 0x%04x\n",
  556. ctl_name, id);
  557. memset(&tmp, 0, sizeof(tmp));
  558. tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  559. tmp.private_value = id,
  560. tmp.name = ctl_name,
  561. /* Add Volume Control */
  562. sprintf(ctl_name, "%s Volume", name);
  563. tmp.get = em28xx_vol_get;
  564. tmp.put = em28xx_vol_put;
  565. tmp.info = em28xx_vol_info;
  566. tmp.tlv.p = em28xx_db_scale,
  567. kctl = snd_ctl_new1(&tmp, dev);
  568. err = snd_ctl_add(card, kctl);
  569. if (err < 0)
  570. return err;
  571. dprintk("Added control %s for ac97 volume control 0x%04x\n",
  572. ctl_name, id);
  573. return 0;
  574. }
  575. /*
  576. * register/unregister code and data
  577. */
  578. static struct snd_pcm_ops snd_em28xx_pcm_capture = {
  579. .open = snd_em28xx_capture_open,
  580. .close = snd_em28xx_pcm_close,
  581. .ioctl = snd_pcm_lib_ioctl,
  582. .hw_params = snd_em28xx_hw_capture_params,
  583. .hw_free = snd_em28xx_hw_capture_free,
  584. .prepare = snd_em28xx_prepare,
  585. .trigger = snd_em28xx_capture_trigger,
  586. .pointer = snd_em28xx_capture_pointer,
  587. .page = snd_pcm_get_vmalloc_page,
  588. };
  589. static void em28xx_audio_free_urb(struct em28xx *dev)
  590. {
  591. int i;
  592. for (i = 0; i < dev->adev.num_urb; i++) {
  593. struct urb *urb = dev->adev.urb[i];
  594. if (!urb)
  595. continue;
  596. usb_free_coherent(dev->udev, urb->transfer_buffer_length,
  597. dev->adev.transfer_buffer[i],
  598. urb->transfer_dma);
  599. usb_free_urb(urb);
  600. }
  601. kfree(dev->adev.urb);
  602. kfree(dev->adev.transfer_buffer);
  603. dev->adev.num_urb = 0;
  604. }
  605. /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  606. static int em28xx_audio_ep_packet_size(struct usb_device *udev,
  607. struct usb_endpoint_descriptor *e)
  608. {
  609. int size = le16_to_cpu(e->wMaxPacketSize);
  610. if (udev->speed == USB_SPEED_HIGH)
  611. return (size & 0x7ff) * (1 + (((size) >> 11) & 0x03));
  612. return size & 0x7ff;
  613. }
  614. static int em28xx_audio_urb_init(struct em28xx *dev)
  615. {
  616. struct usb_interface *intf;
  617. struct usb_endpoint_descriptor *e, *ep = NULL;
  618. int i, ep_size, interval, num_urb, npackets;
  619. int urb_size, bytes_per_transfer;
  620. u8 alt;
  621. if (dev->ifnum)
  622. alt = 1;
  623. else
  624. alt = 7;
  625. intf = usb_ifnum_to_if(dev->udev, dev->ifnum);
  626. if (intf->num_altsetting <= alt) {
  627. em28xx_errdev("alt %d doesn't exist on interface %d\n",
  628. dev->ifnum, alt);
  629. return -ENODEV;
  630. }
  631. for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
  632. e = &intf->altsetting[alt].endpoint[i].desc;
  633. if (!usb_endpoint_dir_in(e))
  634. continue;
  635. if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
  636. ep = e;
  637. break;
  638. }
  639. }
  640. if (!ep) {
  641. em28xx_errdev("Couldn't find an audio endpoint");
  642. return -ENODEV;
  643. }
  644. ep_size = em28xx_audio_ep_packet_size(dev->udev, ep);
  645. interval = 1 << (ep->bInterval - 1);
  646. em28xx_info("Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
  647. EM28XX_EP_AUDIO, usb_speed_string(dev->udev->speed),
  648. dev->ifnum, alt,
  649. interval,
  650. ep_size);
  651. /* Calculate the number and size of URBs to better fit the audio samples */
  652. /*
  653. * Estimate the number of bytes per DMA transfer.
  654. *
  655. * This is given by the bit rate (for now, only 48000 Hz) multiplied
  656. * by 2 channels and 2 bytes/sample divided by the number of microframe
  657. * intervals and by the microframe rate (125 us)
  658. */
  659. bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval);
  660. /*
  661. * Estimate the number of transfer URBs. Don't let it go past the
  662. * maximum number of URBs that is known to be supported by the device.
  663. */
  664. num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size);
  665. if (num_urb > EM28XX_MAX_AUDIO_BUFS)
  666. num_urb = EM28XX_MAX_AUDIO_BUFS;
  667. /*
  668. * Now that we know the number of bytes per transfer and the number of
  669. * URBs, estimate the typical size of an URB, in order to adjust the
  670. * minimal number of packets.
  671. */
  672. urb_size = bytes_per_transfer / num_urb;
  673. /*
  674. * Now, calculate the amount of audio packets to be filled on each
  675. * URB. In order to preserve the old behaviour, use a minimal
  676. * threshold for this value.
  677. */
  678. npackets = EM28XX_MIN_AUDIO_PACKETS;
  679. if (urb_size > ep_size * npackets)
  680. npackets = DIV_ROUND_UP(urb_size, ep_size);
  681. em28xx_info("Number of URBs: %d, with %d packets and %d size\n",
  682. num_urb, npackets, urb_size);
  683. /* Estimate the bytes per period */
  684. dev->adev.period = urb_size * npackets;
  685. /* Allocate space to store the number of URBs to be used */
  686. dev->adev.transfer_buffer = kcalloc(num_urb,
  687. sizeof(*dev->adev.transfer_buffer),
  688. GFP_ATOMIC);
  689. if (!dev->adev.transfer_buffer) {
  690. return -ENOMEM;
  691. }
  692. dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_ATOMIC);
  693. if (!dev->adev.urb) {
  694. kfree(dev->adev.transfer_buffer);
  695. return -ENOMEM;
  696. }
  697. /* Alloc memory for each URB and for each transfer buffer */
  698. dev->adev.num_urb = num_urb;
  699. for (i = 0; i < num_urb; i++) {
  700. struct urb *urb;
  701. int j, k;
  702. void *buf;
  703. urb = usb_alloc_urb(npackets, GFP_ATOMIC);
  704. if (!urb) {
  705. em28xx_errdev("usb_alloc_urb failed!\n");
  706. em28xx_audio_free_urb(dev);
  707. return -ENOMEM;
  708. }
  709. dev->adev.urb[i] = urb;
  710. buf = usb_alloc_coherent(dev->udev, npackets * ep_size, GFP_ATOMIC,
  711. &urb->transfer_dma);
  712. if (!buf) {
  713. em28xx_errdev("usb_alloc_coherent failed!\n");
  714. em28xx_audio_free_urb(dev);
  715. return -ENOMEM;
  716. }
  717. dev->adev.transfer_buffer[i] = buf;
  718. urb->dev = dev->udev;
  719. urb->context = dev;
  720. urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
  721. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  722. urb->transfer_buffer = buf;
  723. urb->interval = interval;
  724. urb->complete = em28xx_audio_isocirq;
  725. urb->number_of_packets = npackets;
  726. urb->transfer_buffer_length = ep_size * npackets;
  727. for (j = k = 0; j < npackets; j++, k += ep_size) {
  728. urb->iso_frame_desc[j].offset = k;
  729. urb->iso_frame_desc[j].length = ep_size;
  730. }
  731. }
  732. return 0;
  733. }
  734. static int em28xx_audio_init(struct em28xx *dev)
  735. {
  736. struct em28xx_audio *adev = &dev->adev;
  737. struct snd_pcm *pcm;
  738. struct snd_card *card;
  739. static int devnr;
  740. int err;
  741. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
  742. /* This device does not support the extension (in this case
  743. the device is expecting the snd-usb-audio module or
  744. doesn't have analog audio support at all) */
  745. return 0;
  746. }
  747. em28xx_info("Binding audio extension\n");
  748. kref_get(&dev->ref);
  749. printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
  750. "Rechberger\n");
  751. printk(KERN_INFO
  752. "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n");
  753. err = snd_card_new(&dev->udev->dev, index[devnr], "Em28xx Audio",
  754. THIS_MODULE, 0, &card);
  755. if (err < 0)
  756. return err;
  757. spin_lock_init(&adev->slock);
  758. adev->sndcard = card;
  759. adev->udev = dev->udev;
  760. err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  761. if (err < 0)
  762. goto card_free;
  763. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  764. pcm->info_flags = 0;
  765. pcm->private_data = dev;
  766. strcpy(pcm->name, "Empia 28xx Capture");
  767. strcpy(card->driver, "Em28xx-Audio");
  768. strcpy(card->shortname, "Em28xx Audio");
  769. strcpy(card->longname, "Empia Em28xx Audio");
  770. INIT_WORK(&adev->wq_trigger, audio_trigger);
  771. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  772. em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
  773. em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
  774. em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
  775. em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
  776. em28xx_cvol_new(card, dev, "CD", AC97_CD);
  777. em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
  778. em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
  779. em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
  780. em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
  781. em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
  782. em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
  783. em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
  784. }
  785. err = em28xx_audio_urb_init(dev);
  786. if (err)
  787. goto card_free;
  788. err = snd_card_register(card);
  789. if (err < 0)
  790. goto urb_free;
  791. em28xx_info("Audio extension successfully initialized\n");
  792. return 0;
  793. urb_free:
  794. em28xx_audio_free_urb(dev);
  795. card_free:
  796. snd_card_free(card);
  797. adev->sndcard = NULL;
  798. return err;
  799. }
  800. static int em28xx_audio_fini(struct em28xx *dev)
  801. {
  802. if (dev == NULL)
  803. return 0;
  804. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
  805. /* This device does not support the extension (in this case
  806. the device is expecting the snd-usb-audio module or
  807. doesn't have analog audio support at all) */
  808. return 0;
  809. }
  810. em28xx_info("Closing audio extension\n");
  811. if (dev->adev.sndcard) {
  812. snd_card_disconnect(dev->adev.sndcard);
  813. flush_work(&dev->adev.wq_trigger);
  814. em28xx_audio_free_urb(dev);
  815. snd_card_free(dev->adev.sndcard);
  816. dev->adev.sndcard = NULL;
  817. }
  818. kref_put(&dev->ref, em28xx_free_device);
  819. return 0;
  820. }
  821. static int em28xx_audio_suspend(struct em28xx *dev)
  822. {
  823. if (dev == NULL)
  824. return 0;
  825. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
  826. return 0;
  827. em28xx_info("Suspending audio extension\n");
  828. em28xx_deinit_isoc_audio(dev);
  829. atomic_set(&dev->adev.stream_started, 0);
  830. return 0;
  831. }
  832. static int em28xx_audio_resume(struct em28xx *dev)
  833. {
  834. if (dev == NULL)
  835. return 0;
  836. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
  837. return 0;
  838. em28xx_info("Resuming audio extension\n");
  839. /* Nothing to do other than schedule_work() ?? */
  840. schedule_work(&dev->adev.wq_trigger);
  841. return 0;
  842. }
  843. static struct em28xx_ops audio_ops = {
  844. .id = EM28XX_AUDIO,
  845. .name = "Em28xx Audio Extension",
  846. .init = em28xx_audio_init,
  847. .fini = em28xx_audio_fini,
  848. .suspend = em28xx_audio_suspend,
  849. .resume = em28xx_audio_resume,
  850. };
  851. static int __init em28xx_alsa_register(void)
  852. {
  853. return em28xx_register_extension(&audio_ops);
  854. }
  855. static void __exit em28xx_alsa_unregister(void)
  856. {
  857. em28xx_unregister_extension(&audio_ops);
  858. }
  859. MODULE_LICENSE("GPL");
  860. MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
  861. MODULE_AUTHOR("Mauro Carvalho Chehab");
  862. MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
  863. MODULE_VERSION(EM28XX_VERSION);
  864. module_init(em28xx_alsa_register);
  865. module_exit(em28xx_alsa_unregister);