em28xx-audio.c 19 KB

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