em28xx-audio.c 27 KB

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