em28xx-audio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 const 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_audio_free_urb(dev);
  706. return -ENOMEM;
  707. }
  708. dev->adev.urb[i] = urb;
  709. buf = usb_alloc_coherent(dev->udev, npackets * ep_size, GFP_ATOMIC,
  710. &urb->transfer_dma);
  711. if (!buf) {
  712. em28xx_errdev("usb_alloc_coherent failed!\n");
  713. em28xx_audio_free_urb(dev);
  714. return -ENOMEM;
  715. }
  716. dev->adev.transfer_buffer[i] = buf;
  717. urb->dev = dev->udev;
  718. urb->context = dev;
  719. urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
  720. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  721. urb->transfer_buffer = buf;
  722. urb->interval = interval;
  723. urb->complete = em28xx_audio_isocirq;
  724. urb->number_of_packets = npackets;
  725. urb->transfer_buffer_length = ep_size * npackets;
  726. for (j = k = 0; j < npackets; j++, k += ep_size) {
  727. urb->iso_frame_desc[j].offset = k;
  728. urb->iso_frame_desc[j].length = ep_size;
  729. }
  730. }
  731. return 0;
  732. }
  733. static int em28xx_audio_init(struct em28xx *dev)
  734. {
  735. struct em28xx_audio *adev = &dev->adev;
  736. struct snd_pcm *pcm;
  737. struct snd_card *card;
  738. static int devnr;
  739. int err;
  740. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
  741. /* This device does not support the extension (in this case
  742. the device is expecting the snd-usb-audio module or
  743. doesn't have analog audio support at all) */
  744. return 0;
  745. }
  746. em28xx_info("Binding audio extension\n");
  747. kref_get(&dev->ref);
  748. printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
  749. "Rechberger\n");
  750. printk(KERN_INFO
  751. "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n");
  752. err = snd_card_new(&dev->udev->dev, index[devnr], "Em28xx Audio",
  753. THIS_MODULE, 0, &card);
  754. if (err < 0)
  755. return err;
  756. spin_lock_init(&adev->slock);
  757. adev->sndcard = card;
  758. adev->udev = dev->udev;
  759. err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  760. if (err < 0)
  761. goto card_free;
  762. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  763. pcm->info_flags = 0;
  764. pcm->private_data = dev;
  765. strcpy(pcm->name, "Empia 28xx Capture");
  766. strcpy(card->driver, "Em28xx-Audio");
  767. strcpy(card->shortname, "Em28xx Audio");
  768. strcpy(card->longname, "Empia Em28xx Audio");
  769. INIT_WORK(&adev->wq_trigger, audio_trigger);
  770. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  771. em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
  772. em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
  773. em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
  774. em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
  775. em28xx_cvol_new(card, dev, "CD", AC97_CD);
  776. em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
  777. em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
  778. em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
  779. em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
  780. em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
  781. em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
  782. em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
  783. }
  784. err = em28xx_audio_urb_init(dev);
  785. if (err)
  786. goto card_free;
  787. err = snd_card_register(card);
  788. if (err < 0)
  789. goto urb_free;
  790. em28xx_info("Audio extension successfully initialized\n");
  791. return 0;
  792. urb_free:
  793. em28xx_audio_free_urb(dev);
  794. card_free:
  795. snd_card_free(card);
  796. adev->sndcard = NULL;
  797. return err;
  798. }
  799. static int em28xx_audio_fini(struct em28xx *dev)
  800. {
  801. if (dev == NULL)
  802. return 0;
  803. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
  804. /* This device does not support the extension (in this case
  805. the device is expecting the snd-usb-audio module or
  806. doesn't have analog audio support at all) */
  807. return 0;
  808. }
  809. em28xx_info("Closing audio extension\n");
  810. if (dev->adev.sndcard) {
  811. snd_card_disconnect(dev->adev.sndcard);
  812. flush_work(&dev->adev.wq_trigger);
  813. em28xx_audio_free_urb(dev);
  814. snd_card_free(dev->adev.sndcard);
  815. dev->adev.sndcard = NULL;
  816. }
  817. kref_put(&dev->ref, em28xx_free_device);
  818. return 0;
  819. }
  820. static int em28xx_audio_suspend(struct em28xx *dev)
  821. {
  822. if (dev == NULL)
  823. return 0;
  824. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
  825. return 0;
  826. em28xx_info("Suspending audio extension\n");
  827. em28xx_deinit_isoc_audio(dev);
  828. atomic_set(&dev->adev.stream_started, 0);
  829. return 0;
  830. }
  831. static int em28xx_audio_resume(struct em28xx *dev)
  832. {
  833. if (dev == NULL)
  834. return 0;
  835. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
  836. return 0;
  837. em28xx_info("Resuming audio extension\n");
  838. /* Nothing to do other than schedule_work() ?? */
  839. schedule_work(&dev->adev.wq_trigger);
  840. return 0;
  841. }
  842. static struct em28xx_ops audio_ops = {
  843. .id = EM28XX_AUDIO,
  844. .name = "Em28xx Audio Extension",
  845. .init = em28xx_audio_init,
  846. .fini = em28xx_audio_fini,
  847. .suspend = em28xx_audio_suspend,
  848. .resume = em28xx_audio_resume,
  849. };
  850. static int __init em28xx_alsa_register(void)
  851. {
  852. return em28xx_register_extension(&audio_ops);
  853. }
  854. static void __exit em28xx_alsa_unregister(void)
  855. {
  856. em28xx_unregister_extension(&audio_ops);
  857. }
  858. MODULE_LICENSE("GPL");
  859. MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
  860. MODULE_AUTHOR("Mauro Carvalho Chehab");
  861. MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
  862. MODULE_VERSION(EM28XX_VERSION);
  863. module_init(em28xx_alsa_register);
  864. module_exit(em28xx_alsa_unregister);