em28xx-audio.c 13 KB

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