em28xx-audio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Empiatech em28x1 audio extension
  3. *
  4. * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
  5. *
  6. * Copyright (C) 2007-2011 Mauro Carvalho Chehab <mchehab@redhat.com>
  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 <media/v4l2-common.h>
  45. #include "em28xx.h"
  46. static int debug;
  47. module_param(debug, int, 0644);
  48. MODULE_PARM_DESC(debug, "activates debug info");
  49. #define dprintk(fmt, arg...) do { \
  50. if (debug) \
  51. printk(KERN_INFO "em28xx-audio %s: " fmt, \
  52. __func__, ##arg); \
  53. } while (0)
  54. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  55. static int em28xx_deinit_isoc_audio(struct em28xx *dev)
  56. {
  57. int i;
  58. dprintk("Stopping isoc\n");
  59. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  60. if (!irqs_disabled())
  61. usb_kill_urb(dev->adev.urb[i]);
  62. else
  63. usb_unlink_urb(dev->adev.urb[i]);
  64. usb_free_urb(dev->adev.urb[i]);
  65. dev->adev.urb[i] = NULL;
  66. kfree(dev->adev.transfer_buffer[i]);
  67. dev->adev.transfer_buffer[i] = NULL;
  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. switch (urb->status) {
  83. case 0: /* success */
  84. case -ETIMEDOUT: /* NAK */
  85. break;
  86. case -ECONNRESET: /* kill */
  87. case -ENOENT:
  88. case -ESHUTDOWN:
  89. return;
  90. default: /* error */
  91. dprintk("urb completition error %d.\n", urb->status);
  92. break;
  93. }
  94. if (atomic_read(&dev->stream_started) == 0)
  95. return;
  96. if (dev->adev.capture_pcm_substream) {
  97. substream = dev->adev.capture_pcm_substream;
  98. runtime = substream->runtime;
  99. stride = runtime->frame_bits >> 3;
  100. for (i = 0; i < urb->number_of_packets; i++) {
  101. int length =
  102. urb->iso_frame_desc[i].actual_length / stride;
  103. cp = (unsigned char *)urb->transfer_buffer +
  104. urb->iso_frame_desc[i].offset;
  105. if (!length)
  106. continue;
  107. oldptr = dev->adev.hwptr_done_capture;
  108. if (oldptr + length >= runtime->buffer_size) {
  109. unsigned int cnt =
  110. runtime->buffer_size - oldptr;
  111. memcpy(runtime->dma_area + oldptr * stride, cp,
  112. cnt * stride);
  113. memcpy(runtime->dma_area, cp + cnt * stride,
  114. length * stride - cnt * stride);
  115. } else {
  116. memcpy(runtime->dma_area + oldptr * stride, cp,
  117. length * stride);
  118. }
  119. snd_pcm_stream_lock(substream);
  120. dev->adev.hwptr_done_capture += length;
  121. if (dev->adev.hwptr_done_capture >=
  122. runtime->buffer_size)
  123. dev->adev.hwptr_done_capture -=
  124. runtime->buffer_size;
  125. dev->adev.capture_transfer_done += length;
  126. if (dev->adev.capture_transfer_done >=
  127. runtime->period_size) {
  128. dev->adev.capture_transfer_done -=
  129. runtime->period_size;
  130. period_elapsed = 1;
  131. }
  132. snd_pcm_stream_unlock(substream);
  133. }
  134. if (period_elapsed)
  135. snd_pcm_period_elapsed(substream);
  136. }
  137. urb->status = 0;
  138. status = usb_submit_urb(urb, GFP_ATOMIC);
  139. if (status < 0) {
  140. em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
  141. status);
  142. }
  143. return;
  144. }
  145. static int em28xx_init_audio_isoc(struct em28xx *dev)
  146. {
  147. int i, errCode;
  148. const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
  149. EM28XX_AUDIO_MAX_PACKET_SIZE;
  150. dprintk("Starting isoc transfers\n");
  151. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  152. struct urb *urb;
  153. int j, k;
  154. dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
  155. if (!dev->adev.transfer_buffer[i])
  156. return -ENOMEM;
  157. memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
  158. urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
  159. if (!urb) {
  160. em28xx_errdev("usb_alloc_urb failed!\n");
  161. for (j = 0; j < i; j++) {
  162. usb_free_urb(dev->adev.urb[j]);
  163. kfree(dev->adev.transfer_buffer[j]);
  164. }
  165. return -ENOMEM;
  166. }
  167. urb->dev = dev->udev;
  168. urb->context = dev;
  169. urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
  170. urb->transfer_flags = URB_ISO_ASAP;
  171. urb->transfer_buffer = dev->adev.transfer_buffer[i];
  172. urb->interval = 1;
  173. urb->complete = em28xx_audio_isocirq;
  174. urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
  175. urb->transfer_buffer_length = sb_size;
  176. for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
  177. j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
  178. urb->iso_frame_desc[j].offset = k;
  179. urb->iso_frame_desc[j].length =
  180. EM28XX_AUDIO_MAX_PACKET_SIZE;
  181. }
  182. dev->adev.urb[i] = urb;
  183. }
  184. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  185. errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
  186. if (errCode) {
  187. em28xx_errdev("submit of audio urb failed\n");
  188. em28xx_deinit_isoc_audio(dev);
  189. atomic_set(&dev->stream_started, 0);
  190. return errCode;
  191. }
  192. }
  193. return 0;
  194. }
  195. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  196. size_t size)
  197. {
  198. struct snd_pcm_runtime *runtime = subs->runtime;
  199. dprintk("Allocating vbuffer\n");
  200. if (runtime->dma_area) {
  201. if (runtime->dma_bytes > size)
  202. return 0;
  203. vfree(runtime->dma_area);
  204. }
  205. runtime->dma_area = vmalloc(size);
  206. if (!runtime->dma_area)
  207. return -ENOMEM;
  208. runtime->dma_bytes = size;
  209. return 0;
  210. }
  211. static struct snd_pcm_hardware snd_em28xx_hw_capture = {
  212. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  213. SNDRV_PCM_INFO_MMAP |
  214. SNDRV_PCM_INFO_INTERLEAVED |
  215. SNDRV_PCM_INFO_BATCH |
  216. SNDRV_PCM_INFO_MMAP_VALID,
  217. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  218. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
  219. .rate_min = 48000,
  220. .rate_max = 48000,
  221. .channels_min = 2,
  222. .channels_max = 2,
  223. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  224. .period_bytes_min = 64, /* 12544/2, */
  225. .period_bytes_max = 12544,
  226. .periods_min = 2,
  227. .periods_max = 98, /* 12544, */
  228. };
  229. static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
  230. {
  231. struct em28xx *dev = snd_pcm_substream_chip(substream);
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. int ret = 0;
  234. dprintk("opening device and trying to acquire exclusive lock\n");
  235. if (!dev) {
  236. em28xx_err("BUG: em28xx can't find device struct."
  237. " Can't proceed with open\n");
  238. return -ENODEV;
  239. }
  240. runtime->hw = snd_em28xx_hw_capture;
  241. if ((dev->alt == 0 || dev->audio_ifnum) && dev->adev.users == 0) {
  242. if (dev->audio_ifnum)
  243. dev->alt = 1;
  244. else
  245. dev->alt = 7;
  246. dprintk("changing alternate number on interface %d to %d\n",
  247. dev->audio_ifnum, dev->alt);
  248. usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt);
  249. /* Sets volume, mute, etc */
  250. dev->mute = 0;
  251. mutex_lock(&dev->lock);
  252. ret = em28xx_audio_analog_set(dev);
  253. if (ret < 0)
  254. goto err;
  255. dev->adev.users++;
  256. mutex_unlock(&dev->lock);
  257. }
  258. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  259. dev->adev.capture_pcm_substream = substream;
  260. runtime->private_data = dev;
  261. return 0;
  262. err:
  263. mutex_unlock(&dev->lock);
  264. em28xx_err("Error while configuring em28xx mixer\n");
  265. return ret;
  266. }
  267. static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
  268. {
  269. struct em28xx *dev = snd_pcm_substream_chip(substream);
  270. dprintk("closing device\n");
  271. dev->mute = 1;
  272. mutex_lock(&dev->lock);
  273. dev->adev.users--;
  274. if (atomic_read(&dev->stream_started) > 0) {
  275. atomic_set(&dev->stream_started, 0);
  276. schedule_work(&dev->wq_trigger);
  277. }
  278. em28xx_audio_analog_set(dev);
  279. if (substream->runtime->dma_area) {
  280. dprintk("freeing\n");
  281. vfree(substream->runtime->dma_area);
  282. substream->runtime->dma_area = NULL;
  283. }
  284. mutex_unlock(&dev->lock);
  285. return 0;
  286. }
  287. static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
  288. struct snd_pcm_hw_params *hw_params)
  289. {
  290. unsigned int channels, rate, format;
  291. int ret;
  292. dprintk("Setting capture parameters\n");
  293. ret = snd_pcm_alloc_vmalloc_buffer(substream,
  294. params_buffer_bytes(hw_params));
  295. if (ret < 0)
  296. return ret;
  297. format = params_format(hw_params);
  298. rate = params_rate(hw_params);
  299. channels = params_channels(hw_params);
  300. /* TODO: set up em28xx audio chip to deliver the correct audio format,
  301. current default is 48000hz multiplexed => 96000hz mono
  302. which shouldn't matter since analogue TV only supports mono */
  303. return 0;
  304. }
  305. static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
  306. {
  307. struct em28xx *dev = snd_pcm_substream_chip(substream);
  308. dprintk("Stop capture, if needed\n");
  309. if (atomic_read(&dev->stream_started) > 0) {
  310. atomic_set(&dev->stream_started, 0);
  311. schedule_work(&dev->wq_trigger);
  312. }
  313. return 0;
  314. }
  315. static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
  316. {
  317. struct em28xx *dev = snd_pcm_substream_chip(substream);
  318. dev->adev.hwptr_done_capture = 0;
  319. dev->adev.capture_transfer_done = 0;
  320. return 0;
  321. }
  322. static void audio_trigger(struct work_struct *work)
  323. {
  324. struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
  325. if (atomic_read(&dev->stream_started)) {
  326. dprintk("starting capture");
  327. em28xx_init_audio_isoc(dev);
  328. } else {
  329. dprintk("stopping capture");
  330. em28xx_deinit_isoc_audio(dev);
  331. }
  332. }
  333. static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
  334. int cmd)
  335. {
  336. struct em28xx *dev = snd_pcm_substream_chip(substream);
  337. int retval = 0;
  338. switch (cmd) {
  339. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  340. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  341. case SNDRV_PCM_TRIGGER_START:
  342. atomic_set(&dev->stream_started, 1);
  343. break;
  344. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  345. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  346. case SNDRV_PCM_TRIGGER_STOP:
  347. atomic_set(&dev->stream_started, 0);
  348. break;
  349. default:
  350. retval = -EINVAL;
  351. }
  352. schedule_work(&dev->wq_trigger);
  353. return retval;
  354. }
  355. static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
  356. *substream)
  357. {
  358. unsigned long flags;
  359. struct em28xx *dev;
  360. snd_pcm_uframes_t hwptr_done;
  361. dev = snd_pcm_substream_chip(substream);
  362. spin_lock_irqsave(&dev->adev.slock, flags);
  363. hwptr_done = dev->adev.hwptr_done_capture;
  364. spin_unlock_irqrestore(&dev->adev.slock, flags);
  365. return hwptr_done;
  366. }
  367. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  368. unsigned long offset)
  369. {
  370. void *pageptr = subs->runtime->dma_area + offset;
  371. return vmalloc_to_page(pageptr);
  372. }
  373. /*
  374. * AC97 volume control support
  375. */
  376. static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
  377. struct snd_ctl_elem_info *info)
  378. {
  379. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  380. info->count = 2;
  381. info->value.integer.min = 0;
  382. info->value.integer.max = 0x1f;
  383. return 0;
  384. }
  385. static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
  386. struct snd_ctl_elem_value *value)
  387. {
  388. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  389. u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
  390. (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
  391. int rc;
  392. mutex_lock(&dev->lock);
  393. rc = em28xx_read_ac97(dev, kcontrol->private_value);
  394. if (rc < 0)
  395. goto err;
  396. val |= rc & 0x8000; /* Preserve the mute flag */
  397. rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
  398. if (rc < 0)
  399. goto err;
  400. dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
  401. (val & 0x8000) ? "muted " : "",
  402. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  403. val, (int)kcontrol->private_value);
  404. err:
  405. mutex_unlock(&dev->lock);
  406. return rc;
  407. }
  408. static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
  409. struct snd_ctl_elem_value *value)
  410. {
  411. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  412. int val;
  413. mutex_lock(&dev->lock);
  414. val = em28xx_read_ac97(dev, kcontrol->private_value);
  415. mutex_unlock(&dev->lock);
  416. if (val < 0)
  417. return val;
  418. dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
  419. (val & 0x8000) ? "muted " : "",
  420. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  421. val, (int)kcontrol->private_value);
  422. value->value.integer.value[0] = 0x1f - (val & 0x1f);
  423. value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
  424. return 0;
  425. }
  426. static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
  427. struct snd_ctl_elem_value *value)
  428. {
  429. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  430. u16 val = value->value.integer.value[0];
  431. int rc;
  432. mutex_lock(&dev->lock);
  433. rc = em28xx_read_ac97(dev, kcontrol->private_value);
  434. if (rc < 0)
  435. goto err;
  436. if (val)
  437. rc &= 0x1f1f;
  438. else
  439. rc |= 0x8000;
  440. rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
  441. if (rc < 0)
  442. goto err;
  443. dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
  444. (val & 0x8000) ? "muted " : "",
  445. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  446. val, (int)kcontrol->private_value);
  447. err:
  448. mutex_unlock(&dev->lock);
  449. return rc;
  450. }
  451. static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
  452. struct snd_ctl_elem_value *value)
  453. {
  454. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  455. int val;
  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. if (val & 0x8000)
  462. value->value.integer.value[0] = 0;
  463. else
  464. value->value.integer.value[0] = 1;
  465. dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
  466. (val & 0x8000) ? "muted " : "",
  467. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  468. val, (int)kcontrol->private_value);
  469. return 0;
  470. }
  471. static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
  472. static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
  473. char *name, int id)
  474. {
  475. int err;
  476. char ctl_name[44];
  477. struct snd_kcontrol *kctl;
  478. struct snd_kcontrol_new tmp;
  479. memset (&tmp, 0, sizeof(tmp));
  480. tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  481. tmp.private_value = id,
  482. tmp.name = ctl_name,
  483. /* Add Mute Control */
  484. sprintf(ctl_name, "%s Switch", name);
  485. tmp.get = em28xx_vol_get_mute;
  486. tmp.put = em28xx_vol_put_mute;
  487. tmp.info = snd_ctl_boolean_mono_info;
  488. kctl = snd_ctl_new1(&tmp, dev);
  489. err = snd_ctl_add(card, kctl);
  490. if (err < 0)
  491. return err;
  492. dprintk("Added control %s for ac97 volume control 0x%04x\n",
  493. ctl_name, id);
  494. memset (&tmp, 0, sizeof(tmp));
  495. tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  496. tmp.private_value = id,
  497. tmp.name = ctl_name,
  498. /* Add Volume Control */
  499. sprintf(ctl_name, "%s Volume", name);
  500. tmp.get = em28xx_vol_get;
  501. tmp.put = em28xx_vol_put;
  502. tmp.info = em28xx_vol_info;
  503. tmp.tlv.p = em28xx_db_scale,
  504. kctl = snd_ctl_new1(&tmp, dev);
  505. err = snd_ctl_add(card, kctl);
  506. if (err < 0)
  507. return err;
  508. dprintk("Added control %s for ac97 volume control 0x%04x\n",
  509. ctl_name, id);
  510. return 0;
  511. }
  512. /*
  513. * register/unregister code and data
  514. */
  515. static struct snd_pcm_ops snd_em28xx_pcm_capture = {
  516. .open = snd_em28xx_capture_open,
  517. .close = snd_em28xx_pcm_close,
  518. .ioctl = snd_pcm_lib_ioctl,
  519. .hw_params = snd_em28xx_hw_capture_params,
  520. .hw_free = snd_em28xx_hw_capture_free,
  521. .prepare = snd_em28xx_prepare,
  522. .trigger = snd_em28xx_capture_trigger,
  523. .pointer = snd_em28xx_capture_pointer,
  524. .page = snd_pcm_get_vmalloc_page,
  525. };
  526. static int em28xx_audio_init(struct em28xx *dev)
  527. {
  528. struct em28xx_audio *adev = &dev->adev;
  529. struct snd_pcm *pcm;
  530. struct snd_card *card;
  531. static int devnr;
  532. int err;
  533. if (!dev->has_alsa_audio || dev->audio_ifnum < 0) {
  534. /* This device does not support the extension (in this case
  535. the device is expecting the snd-usb-audio module or
  536. doesn't have analog audio support at all) */
  537. return 0;
  538. }
  539. printk(KERN_INFO "em28xx-audio.c: probing for em28xx Audio Vendor Class\n");
  540. printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
  541. "Rechberger\n");
  542. printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2007-2011 Mauro Carvalho Chehab\n");
  543. err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
  544. &card);
  545. if (err < 0)
  546. return err;
  547. spin_lock_init(&adev->slock);
  548. err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  549. if (err < 0) {
  550. snd_card_free(card);
  551. return err;
  552. }
  553. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  554. pcm->info_flags = 0;
  555. pcm->private_data = dev;
  556. strcpy(pcm->name, "Empia 28xx Capture");
  557. snd_card_set_dev(card, &dev->udev->dev);
  558. strcpy(card->driver, "Em28xx-Audio");
  559. strcpy(card->shortname, "Em28xx Audio");
  560. strcpy(card->longname, "Empia Em28xx Audio");
  561. INIT_WORK(&dev->wq_trigger, audio_trigger);
  562. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  563. em28xx_cvol_new(card, dev, "Video", AC97_VIDEO_VOL);
  564. em28xx_cvol_new(card, dev, "Line In", AC97_LINEIN_VOL);
  565. em28xx_cvol_new(card, dev, "Phone", AC97_PHONE_VOL);
  566. em28xx_cvol_new(card, dev, "Microphone", AC97_PHONE_VOL);
  567. em28xx_cvol_new(card, dev, "CD", AC97_CD_VOL);
  568. em28xx_cvol_new(card, dev, "AUX", AC97_AUX_VOL);
  569. em28xx_cvol_new(card, dev, "PCM", AC97_PCM_OUT_VOL);
  570. em28xx_cvol_new(card, dev, "Master", AC97_MASTER_VOL);
  571. em28xx_cvol_new(card, dev, "Line", AC97_LINE_LEVEL_VOL);
  572. em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO_VOL);
  573. em28xx_cvol_new(card, dev, "LFE", AC97_LFE_MASTER_VOL);
  574. em28xx_cvol_new(card, dev, "Surround", AC97_SURR_MASTER_VOL);
  575. }
  576. err = snd_card_register(card);
  577. if (err < 0) {
  578. snd_card_free(card);
  579. return err;
  580. }
  581. adev->sndcard = card;
  582. adev->udev = dev->udev;
  583. return 0;
  584. }
  585. static int em28xx_audio_fini(struct em28xx *dev)
  586. {
  587. if (dev == NULL)
  588. return 0;
  589. if (dev->has_alsa_audio != 1) {
  590. /* This device does not support the extension (in this case
  591. the device is expecting the snd-usb-audio module or
  592. doesn't have analog audio support at all) */
  593. return 0;
  594. }
  595. if (dev->adev.sndcard) {
  596. snd_card_free(dev->adev.sndcard);
  597. dev->adev.sndcard = NULL;
  598. }
  599. return 0;
  600. }
  601. static struct em28xx_ops audio_ops = {
  602. .id = EM28XX_AUDIO,
  603. .name = "Em28xx Audio Extension",
  604. .init = em28xx_audio_init,
  605. .fini = em28xx_audio_fini,
  606. };
  607. static int __init em28xx_alsa_register(void)
  608. {
  609. return em28xx_register_extension(&audio_ops);
  610. }
  611. static void __exit em28xx_alsa_unregister(void)
  612. {
  613. em28xx_unregister_extension(&audio_ops);
  614. }
  615. MODULE_LICENSE("GPL");
  616. MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
  617. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  618. MODULE_DESCRIPTION("Em28xx Audio driver");
  619. module_init(em28xx_alsa_register);
  620. module_exit(em28xx_alsa_unregister);