em28xx-audio.c 27 KB

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