em28xx-audio.c 12 KB

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