em28xx-audio.c 13 KB

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