em28xx-audio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Empiatech em28x1 audio extension
  3. *
  4. * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
  5. *
  6. * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
  7. * - Port to work with the in-kernel driver
  8. * - Several cleanups
  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 <media/v4l2-common.h>
  44. #include "em28xx.h"
  45. static int debug;
  46. module_param(debug, int, 0644);
  47. MODULE_PARM_DESC(debug, "activates debug info");
  48. #define dprintk(fmt, arg...) do { \
  49. if (debug) \
  50. printk(KERN_INFO "em28xx-audio %s: " fmt, \
  51. __func__, ##arg); \
  52. } while (0)
  53. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  54. static int em28xx_isoc_audio_deinit(struct em28xx *dev)
  55. {
  56. int i;
  57. dprintk("Stopping isoc\n");
  58. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  59. usb_unlink_urb(dev->adev->urb[i]);
  60. usb_free_urb(dev->adev->urb[i]);
  61. dev->adev->urb[i] = NULL;
  62. }
  63. return 0;
  64. }
  65. static void em28xx_audio_isocirq(struct urb *urb)
  66. {
  67. struct em28xx *dev = urb->context;
  68. int i;
  69. unsigned int oldptr;
  70. int period_elapsed = 0;
  71. int status;
  72. unsigned char *cp;
  73. unsigned int stride;
  74. struct snd_pcm_substream *substream;
  75. struct snd_pcm_runtime *runtime;
  76. if (dev->adev->capture_pcm_substream) {
  77. substream = dev->adev->capture_pcm_substream;
  78. runtime = substream->runtime;
  79. stride = runtime->frame_bits >> 3;
  80. for (i = 0; i < urb->number_of_packets; i++) {
  81. int length =
  82. urb->iso_frame_desc[i].actual_length / stride;
  83. cp = (unsigned char *)urb->transfer_buffer +
  84. urb->iso_frame_desc[i].offset;
  85. if (!length)
  86. continue;
  87. oldptr = dev->adev->hwptr_done_capture;
  88. if (oldptr + length >= runtime->buffer_size) {
  89. unsigned int cnt =
  90. runtime->buffer_size - oldptr;
  91. memcpy(runtime->dma_area + oldptr * stride, cp,
  92. cnt * stride);
  93. memcpy(runtime->dma_area, cp + cnt * stride,
  94. length * stride - cnt * stride);
  95. } else {
  96. memcpy(runtime->dma_area + oldptr * stride, cp,
  97. length * stride);
  98. }
  99. snd_pcm_stream_lock(substream);
  100. dev->adev->hwptr_done_capture += length;
  101. if (dev->adev->hwptr_done_capture >=
  102. runtime->buffer_size)
  103. dev->adev->hwptr_done_capture -=
  104. runtime->buffer_size;
  105. dev->adev->capture_transfer_done += length;
  106. if (dev->adev->capture_transfer_done >=
  107. runtime->period_size) {
  108. dev->adev->capture_transfer_done -=
  109. runtime->period_size;
  110. period_elapsed = 1;
  111. }
  112. snd_pcm_stream_unlock(substream);
  113. }
  114. if (period_elapsed)
  115. snd_pcm_period_elapsed(substream);
  116. }
  117. urb->status = 0;
  118. if (dev->adev->shutdown)
  119. return;
  120. status = usb_submit_urb(urb, GFP_ATOMIC);
  121. if (status < 0) {
  122. em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
  123. status);
  124. }
  125. return;
  126. }
  127. static int em28xx_init_audio_isoc(struct em28xx *dev)
  128. {
  129. int i, errCode;
  130. const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
  131. EM28XX_AUDIO_MAX_PACKET_SIZE;
  132. dprintk("Starting isoc transfers\n");
  133. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  134. struct urb *urb;
  135. int j, k;
  136. dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
  137. if (!dev->adev->transfer_buffer[i])
  138. return -ENOMEM;
  139. memset(dev->adev->transfer_buffer[i], 0x80, sb_size);
  140. urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
  141. if (!urb) {
  142. em28xx_errdev("usb_alloc_urb failed!\n");
  143. for (j = 0; j < i; j++) {
  144. usb_free_urb(dev->adev->urb[j]);
  145. kfree(dev->adev->transfer_buffer[j]);
  146. }
  147. return -ENOMEM;
  148. }
  149. urb->dev = dev->udev;
  150. urb->context = dev;
  151. urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
  152. urb->transfer_flags = URB_ISO_ASAP;
  153. urb->transfer_buffer = dev->adev->transfer_buffer[i];
  154. urb->interval = 1;
  155. urb->complete = em28xx_audio_isocirq;
  156. urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
  157. urb->transfer_buffer_length = sb_size;
  158. for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
  159. j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
  160. urb->iso_frame_desc[j].offset = k;
  161. urb->iso_frame_desc[j].length =
  162. EM28XX_AUDIO_MAX_PACKET_SIZE;
  163. }
  164. dev->adev->urb[i] = urb;
  165. }
  166. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  167. errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC);
  168. if (errCode) {
  169. em28xx_isoc_audio_deinit(dev);
  170. return errCode;
  171. }
  172. }
  173. return 0;
  174. }
  175. static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
  176. {
  177. dprintk("%s transfer\n", (dev->adev->capture_stream == STREAM_ON)?
  178. "stop" : "start");
  179. switch (cmd) {
  180. case EM28XX_CAPTURE_STREAM_EN:
  181. if (dev->adev->capture_stream == STREAM_OFF && arg == 1) {
  182. dev->adev->capture_stream = STREAM_ON;
  183. em28xx_init_audio_isoc(dev);
  184. } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) {
  185. dev->adev->capture_stream = STREAM_OFF;
  186. em28xx_isoc_audio_deinit(dev);
  187. } else {
  188. printk(KERN_ERR "An underrun very likely occurred. "
  189. "Ignoring it.\n");
  190. }
  191. return 0;
  192. default:
  193. return -EINVAL;
  194. }
  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("Alocating 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_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. printk(KERN_ERR "BUG: em28xx can't find device struct."
  237. " Can't proceed with open\n");
  238. return -ENODEV;
  239. }
  240. /* Sets volume, mute, etc */
  241. dev->mute = 0;
  242. mutex_lock(&dev->lock);
  243. ret = em28xx_audio_analog_set(dev);
  244. mutex_unlock(&dev->lock);
  245. if (ret < 0)
  246. goto err;
  247. runtime->hw = snd_em28xx_hw_capture;
  248. if (dev->alt == 0 && dev->adev->users == 0) {
  249. int errCode;
  250. dev->alt = 7;
  251. errCode = usb_set_interface(dev->udev, 0, 7);
  252. dprintk("changing alternate number to 7\n");
  253. }
  254. dev->adev->users++;
  255. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  256. dev->adev->capture_pcm_substream = substream;
  257. runtime->private_data = dev;
  258. return 0;
  259. err:
  260. printk(KERN_ERR "Error while configuring em28xx mixer\n");
  261. return ret;
  262. }
  263. static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
  264. {
  265. struct em28xx *dev = snd_pcm_substream_chip(substream);
  266. dev->adev->users--;
  267. dprintk("closing device\n");
  268. dev->mute = 1;
  269. mutex_lock(&dev->lock);
  270. em28xx_audio_analog_set(dev);
  271. mutex_unlock(&dev->lock);
  272. if (dev->adev->users == 0 && dev->adev->shutdown == 1) {
  273. dprintk("audio users: %d\n", dev->adev->users);
  274. dprintk("disabling audio stream!\n");
  275. dev->adev->shutdown = 0;
  276. dprintk("released lock\n");
  277. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
  278. }
  279. return 0;
  280. }
  281. static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
  282. struct snd_pcm_hw_params *hw_params)
  283. {
  284. unsigned int channels, rate, format;
  285. int ret;
  286. dprintk("Setting capture parameters\n");
  287. ret = snd_pcm_alloc_vmalloc_buffer(substream,
  288. params_buffer_bytes(hw_params));
  289. format = params_format(hw_params);
  290. rate = params_rate(hw_params);
  291. channels = params_channels(hw_params);
  292. /* TODO: set up em28xx audio chip to deliver the correct audio format,
  293. current default is 48000hz multiplexed => 96000hz mono
  294. which shouldn't matter since analogue TV only supports mono */
  295. return 0;
  296. }
  297. static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
  298. {
  299. struct em28xx *dev = snd_pcm_substream_chip(substream);
  300. dprintk("Stop capture, if needed\n");
  301. if (dev->adev->capture_stream == STREAM_ON)
  302. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
  303. return 0;
  304. }
  305. static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
  306. {
  307. return 0;
  308. }
  309. static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
  310. int cmd)
  311. {
  312. struct em28xx *dev = snd_pcm_substream_chip(substream);
  313. dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
  314. "start": "stop");
  315. switch (cmd) {
  316. case SNDRV_PCM_TRIGGER_START:
  317. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
  318. return 0;
  319. case SNDRV_PCM_TRIGGER_STOP:
  320. dev->adev->shutdown = 1;
  321. return 0;
  322. default:
  323. return -EINVAL;
  324. }
  325. }
  326. static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
  327. *substream)
  328. {
  329. struct em28xx *dev;
  330. snd_pcm_uframes_t hwptr_done;
  331. dev = snd_pcm_substream_chip(substream);
  332. hwptr_done = dev->adev->hwptr_done_capture;
  333. return hwptr_done;
  334. }
  335. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  336. unsigned long offset)
  337. {
  338. void *pageptr = subs->runtime->dma_area + offset;
  339. return vmalloc_to_page(pageptr);
  340. }
  341. static struct snd_pcm_ops snd_em28xx_pcm_capture = {
  342. .open = snd_em28xx_capture_open,
  343. .close = snd_em28xx_pcm_close,
  344. .ioctl = snd_pcm_lib_ioctl,
  345. .hw_params = snd_em28xx_hw_capture_params,
  346. .hw_free = snd_em28xx_hw_capture_free,
  347. .prepare = snd_em28xx_prepare,
  348. .trigger = snd_em28xx_capture_trigger,
  349. .pointer = snd_em28xx_capture_pointer,
  350. .page = snd_pcm_get_vmalloc_page,
  351. };
  352. static int em28xx_audio_init(struct em28xx *dev)
  353. {
  354. struct em28xx_audio *adev;
  355. struct snd_pcm *pcm;
  356. struct snd_card *card;
  357. static int devnr;
  358. int ret, err;
  359. if (dev->has_audio_class) {
  360. /* This device does not support the extension (in this case
  361. the device is expecting the snd-usb-audio module) */
  362. return 0;
  363. }
  364. printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
  365. "non standard usbaudio\n");
  366. printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
  367. "Rechberger\n");
  368. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  369. if (!adev) {
  370. printk(KERN_ERR "em28xx-audio.c: out of memory\n");
  371. return -1;
  372. }
  373. card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
  374. if (card == NULL) {
  375. kfree(adev);
  376. return -ENOMEM;
  377. }
  378. spin_lock_init(&adev->slock);
  379. err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  380. if (err < 0) {
  381. snd_card_free(card);
  382. return err;
  383. }
  384. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  385. pcm->info_flags = 0;
  386. pcm->private_data = dev;
  387. strcpy(pcm->name, "Empia 28xx Capture");
  388. strcpy(card->driver, "Empia Em28xx Audio");
  389. strcpy(card->shortname, "Em28xx Audio");
  390. strcpy(card->longname, "Empia Em28xx Audio");
  391. err = snd_card_register(card);
  392. if (err < 0) {
  393. snd_card_free(card);
  394. return err;
  395. }
  396. adev->sndcard = card;
  397. adev->udev = dev->udev;
  398. dev->adev = adev;
  399. return 0;
  400. }
  401. static int em28xx_audio_fini(struct em28xx *dev)
  402. {
  403. if (dev == NULL)
  404. return 0;
  405. if (dev->has_audio_class) {
  406. /* This device does not support the extension (in this case
  407. the device is expecting the snd-usb-audio module */
  408. return 0;
  409. }
  410. if (dev->adev) {
  411. snd_card_free(dev->adev->sndcard);
  412. kfree(dev->adev);
  413. dev->adev = NULL;
  414. }
  415. return 0;
  416. }
  417. static struct em28xx_ops audio_ops = {
  418. .id = EM28XX_AUDIO,
  419. .name = "Em28xx Audio Extension",
  420. .init = em28xx_audio_init,
  421. .fini = em28xx_audio_fini,
  422. };
  423. static int __init em28xx_alsa_register(void)
  424. {
  425. return em28xx_register_extension(&audio_ops);
  426. }
  427. static void __exit em28xx_alsa_unregister(void)
  428. {
  429. em28xx_unregister_extension(&audio_ops);
  430. }
  431. MODULE_LICENSE("GPL");
  432. MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
  433. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  434. MODULE_DESCRIPTION("Em28xx Audio driver");
  435. module_init(em28xx_alsa_register);
  436. module_exit(em28xx_alsa_unregister);