em28xx-audio.c 27 KB

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