em28xx-audio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. if (!irqs_disabled())
  60. usb_kill_urb(dev->adev.urb[i]);
  61. else
  62. usb_unlink_urb(dev->adev.urb[i]);
  63. usb_free_urb(dev->adev.urb[i]);
  64. dev->adev.urb[i] = NULL;
  65. kfree(dev->adev.transfer_buffer[i]);
  66. dev->adev.transfer_buffer[i] = NULL;
  67. }
  68. return 0;
  69. }
  70. static void em28xx_audio_isocirq(struct urb *urb)
  71. {
  72. struct em28xx *dev = urb->context;
  73. int i;
  74. unsigned int oldptr;
  75. int period_elapsed = 0;
  76. int status;
  77. unsigned char *cp;
  78. unsigned int stride;
  79. struct snd_pcm_substream *substream;
  80. struct snd_pcm_runtime *runtime;
  81. if (dev->adev.capture_pcm_substream) {
  82. substream = dev->adev.capture_pcm_substream;
  83. runtime = substream->runtime;
  84. stride = runtime->frame_bits >> 3;
  85. for (i = 0; i < urb->number_of_packets; i++) {
  86. int length =
  87. urb->iso_frame_desc[i].actual_length / stride;
  88. cp = (unsigned char *)urb->transfer_buffer +
  89. urb->iso_frame_desc[i].offset;
  90. if (!length)
  91. continue;
  92. oldptr = dev->adev.hwptr_done_capture;
  93. if (oldptr + length >= runtime->buffer_size) {
  94. unsigned int cnt =
  95. runtime->buffer_size - oldptr;
  96. memcpy(runtime->dma_area + oldptr * stride, cp,
  97. cnt * stride);
  98. memcpy(runtime->dma_area, cp + cnt * stride,
  99. length * stride - cnt * stride);
  100. } else {
  101. memcpy(runtime->dma_area + oldptr * stride, cp,
  102. length * stride);
  103. }
  104. snd_pcm_stream_lock(substream);
  105. dev->adev.hwptr_done_capture += length;
  106. if (dev->adev.hwptr_done_capture >=
  107. runtime->buffer_size)
  108. dev->adev.hwptr_done_capture -=
  109. runtime->buffer_size;
  110. dev->adev.capture_transfer_done += length;
  111. if (dev->adev.capture_transfer_done >=
  112. runtime->period_size) {
  113. dev->adev.capture_transfer_done -=
  114. runtime->period_size;
  115. period_elapsed = 1;
  116. }
  117. snd_pcm_stream_unlock(substream);
  118. }
  119. if (period_elapsed)
  120. snd_pcm_period_elapsed(substream);
  121. }
  122. urb->status = 0;
  123. if (dev->adev.shutdown)
  124. return;
  125. status = usb_submit_urb(urb, GFP_ATOMIC);
  126. if (status < 0) {
  127. em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
  128. status);
  129. }
  130. return;
  131. }
  132. static int em28xx_init_audio_isoc(struct em28xx *dev)
  133. {
  134. int i, errCode;
  135. const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
  136. EM28XX_AUDIO_MAX_PACKET_SIZE;
  137. dprintk("Starting isoc transfers\n");
  138. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  139. struct urb *urb;
  140. int j, k;
  141. dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
  142. if (!dev->adev.transfer_buffer[i])
  143. return -ENOMEM;
  144. memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
  145. urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
  146. if (!urb) {
  147. em28xx_errdev("usb_alloc_urb failed!\n");
  148. for (j = 0; j < i; j++) {
  149. usb_free_urb(dev->adev.urb[j]);
  150. kfree(dev->adev.transfer_buffer[j]);
  151. }
  152. return -ENOMEM;
  153. }
  154. urb->dev = dev->udev;
  155. urb->context = dev;
  156. urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
  157. urb->transfer_flags = URB_ISO_ASAP;
  158. urb->transfer_buffer = dev->adev.transfer_buffer[i];
  159. urb->interval = 1;
  160. urb->complete = em28xx_audio_isocirq;
  161. urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
  162. urb->transfer_buffer_length = sb_size;
  163. for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
  164. j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
  165. urb->iso_frame_desc[j].offset = k;
  166. urb->iso_frame_desc[j].length =
  167. EM28XX_AUDIO_MAX_PACKET_SIZE;
  168. }
  169. dev->adev.urb[i] = urb;
  170. }
  171. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  172. errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
  173. if (errCode) {
  174. em28xx_isoc_audio_deinit(dev);
  175. return errCode;
  176. }
  177. }
  178. return 0;
  179. }
  180. static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
  181. {
  182. dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
  183. "stop" : "start");
  184. switch (cmd) {
  185. case EM28XX_CAPTURE_STREAM_EN:
  186. if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
  187. dev->adev.capture_stream = STREAM_ON;
  188. em28xx_init_audio_isoc(dev);
  189. } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
  190. dev->adev.capture_stream = STREAM_OFF;
  191. em28xx_isoc_audio_deinit(dev);
  192. } else {
  193. printk(KERN_ERR "An underrun very likely occurred. "
  194. "Ignoring it.\n");
  195. }
  196. return 0;
  197. default:
  198. return -EINVAL;
  199. }
  200. }
  201. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  202. size_t size)
  203. {
  204. struct snd_pcm_runtime *runtime = subs->runtime;
  205. dprintk("Alocating vbuffer\n");
  206. if (runtime->dma_area) {
  207. if (runtime->dma_bytes > size)
  208. return 0;
  209. vfree(runtime->dma_area);
  210. }
  211. runtime->dma_area = vmalloc(size);
  212. if (!runtime->dma_area)
  213. return -ENOMEM;
  214. runtime->dma_bytes = size;
  215. return 0;
  216. }
  217. static struct snd_pcm_hardware snd_em28xx_hw_capture = {
  218. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  219. SNDRV_PCM_INFO_MMAP |
  220. SNDRV_PCM_INFO_INTERLEAVED |
  221. SNDRV_PCM_INFO_MMAP_VALID,
  222. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  223. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
  224. .rate_min = 48000,
  225. .rate_max = 48000,
  226. .channels_min = 2,
  227. .channels_max = 2,
  228. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  229. .period_bytes_min = 64, /* 12544/2, */
  230. .period_bytes_max = 12544,
  231. .periods_min = 2,
  232. .periods_max = 98, /* 12544, */
  233. };
  234. static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
  235. {
  236. struct em28xx *dev = snd_pcm_substream_chip(substream);
  237. struct snd_pcm_runtime *runtime = substream->runtime;
  238. int ret = 0;
  239. dprintk("opening device and trying to acquire exclusive lock\n");
  240. if (!dev) {
  241. printk(KERN_ERR "BUG: em28xx can't find device struct."
  242. " Can't proceed with open\n");
  243. return -ENODEV;
  244. }
  245. /* Sets volume, mute, etc */
  246. dev->mute = 0;
  247. mutex_lock(&dev->lock);
  248. ret = em28xx_audio_analog_set(dev);
  249. mutex_unlock(&dev->lock);
  250. if (ret < 0)
  251. goto err;
  252. runtime->hw = snd_em28xx_hw_capture;
  253. if (dev->alt == 0 && dev->adev.users == 0) {
  254. int errCode;
  255. dev->alt = 7;
  256. errCode = usb_set_interface(dev->udev, 0, 7);
  257. dprintk("changing alternate number to 7\n");
  258. }
  259. dev->adev.users++;
  260. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  261. dev->adev.capture_pcm_substream = substream;
  262. runtime->private_data = dev;
  263. return 0;
  264. err:
  265. printk(KERN_ERR "Error while configuring em28xx mixer\n");
  266. return ret;
  267. }
  268. static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
  269. {
  270. struct em28xx *dev = snd_pcm_substream_chip(substream);
  271. dev->adev.users--;
  272. dprintk("closing device\n");
  273. dev->mute = 1;
  274. mutex_lock(&dev->lock);
  275. em28xx_audio_analog_set(dev);
  276. mutex_unlock(&dev->lock);
  277. if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
  278. dprintk("audio users: %d\n", dev->adev.users);
  279. dprintk("disabling audio stream!\n");
  280. dev->adev.shutdown = 0;
  281. dprintk("released lock\n");
  282. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
  283. }
  284. return 0;
  285. }
  286. static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
  287. struct snd_pcm_hw_params *hw_params)
  288. {
  289. unsigned int channels, rate, format;
  290. int ret;
  291. dprintk("Setting capture parameters\n");
  292. ret = snd_pcm_alloc_vmalloc_buffer(substream,
  293. params_buffer_bytes(hw_params));
  294. format = params_format(hw_params);
  295. rate = params_rate(hw_params);
  296. channels = params_channels(hw_params);
  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. return 0;
  301. }
  302. static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
  303. {
  304. struct em28xx *dev = snd_pcm_substream_chip(substream);
  305. dprintk("Stop capture, if needed\n");
  306. if (dev->adev.capture_stream == STREAM_ON)
  307. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
  308. return 0;
  309. }
  310. static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
  311. {
  312. return 0;
  313. }
  314. static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
  315. int cmd)
  316. {
  317. struct em28xx *dev = snd_pcm_substream_chip(substream);
  318. dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
  319. "start": "stop");
  320. switch (cmd) {
  321. case SNDRV_PCM_TRIGGER_START:
  322. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
  323. return 0;
  324. case SNDRV_PCM_TRIGGER_STOP:
  325. dev->adev.shutdown = 1;
  326. return 0;
  327. default:
  328. return -EINVAL;
  329. }
  330. }
  331. static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
  332. *substream)
  333. {
  334. unsigned long flags;
  335. struct em28xx *dev;
  336. snd_pcm_uframes_t hwptr_done;
  337. dev = snd_pcm_substream_chip(substream);
  338. spin_lock_irqsave(&dev->adev.slock, flags);
  339. hwptr_done = dev->adev.hwptr_done_capture;
  340. spin_unlock_irqrestore(&dev->adev.slock, flags);
  341. return hwptr_done;
  342. }
  343. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  344. unsigned long offset)
  345. {
  346. void *pageptr = subs->runtime->dma_area + offset;
  347. return vmalloc_to_page(pageptr);
  348. }
  349. static struct snd_pcm_ops snd_em28xx_pcm_capture = {
  350. .open = snd_em28xx_capture_open,
  351. .close = snd_em28xx_pcm_close,
  352. .ioctl = snd_pcm_lib_ioctl,
  353. .hw_params = snd_em28xx_hw_capture_params,
  354. .hw_free = snd_em28xx_hw_capture_free,
  355. .prepare = snd_em28xx_prepare,
  356. .trigger = snd_em28xx_capture_trigger,
  357. .pointer = snd_em28xx_capture_pointer,
  358. .page = snd_pcm_get_vmalloc_page,
  359. };
  360. static int em28xx_audio_init(struct em28xx *dev)
  361. {
  362. struct em28xx_audio *adev = &dev->adev;
  363. struct snd_pcm *pcm;
  364. struct snd_card *card;
  365. static int devnr;
  366. int err;
  367. if (dev->has_alsa_audio != 1) {
  368. /* This device does not support the extension (in this case
  369. the device is expecting the snd-usb-audio module or
  370. doesn't have analog audio support at all) */
  371. return 0;
  372. }
  373. printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
  374. "non standard usbaudio\n");
  375. printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
  376. "Rechberger\n");
  377. card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
  378. if (card == NULL)
  379. return -ENOMEM;
  380. spin_lock_init(&adev->slock);
  381. err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  382. if (err < 0) {
  383. snd_card_free(card);
  384. return err;
  385. }
  386. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  387. pcm->info_flags = 0;
  388. pcm->private_data = dev;
  389. strcpy(pcm->name, "Empia 28xx Capture");
  390. snd_card_set_dev(card, &dev->udev->dev);
  391. strcpy(card->driver, "Empia Em28xx Audio");
  392. strcpy(card->shortname, "Em28xx Audio");
  393. strcpy(card->longname, "Empia Em28xx Audio");
  394. err = snd_card_register(card);
  395. if (err < 0) {
  396. snd_card_free(card);
  397. return err;
  398. }
  399. adev->sndcard = card;
  400. adev->udev = dev->udev;
  401. return 0;
  402. }
  403. static int em28xx_audio_fini(struct em28xx *dev)
  404. {
  405. if (dev == NULL)
  406. return 0;
  407. if (dev->has_alsa_audio != 1) {
  408. /* This device does not support the extension (in this case
  409. the device is expecting the snd-usb-audio module or
  410. doesn't have analog audio support at all) */
  411. return 0;
  412. }
  413. if (dev->adev.sndcard) {
  414. snd_card_free(dev->adev.sndcard);
  415. dev->adev.sndcard = NULL;
  416. }
  417. return 0;
  418. }
  419. static struct em28xx_ops audio_ops = {
  420. .id = EM28XX_AUDIO,
  421. .name = "Em28xx Audio Extension",
  422. .init = em28xx_audio_init,
  423. .fini = em28xx_audio_fini,
  424. };
  425. static int __init em28xx_alsa_register(void)
  426. {
  427. return em28xx_register_extension(&audio_ops);
  428. }
  429. static void __exit em28xx_alsa_unregister(void)
  430. {
  431. em28xx_unregister_extension(&audio_ops);
  432. }
  433. MODULE_LICENSE("GPL");
  434. MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
  435. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  436. MODULE_DESCRIPTION("Em28xx Audio driver");
  437. module_init(em28xx_alsa_register);
  438. module_exit(em28xx_alsa_unregister);