em28xx-audio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Empiatech em28x1 audio extension
  3. *
  4. * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
  5. *
  6. * This driver is based on my previous au600 usb pstn audio driver
  7. * and inherits all the copyrights
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/usb.h>
  25. #include <linux/init.h>
  26. #include <linux/sound.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/soundcard.h>
  29. #include <linux/slab.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/moduleparam.h>
  33. #include <sound/driver.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/info.h>
  38. #include <sound/initval.h>
  39. #include <sound/control.h>
  40. //#include <linux/video_decoder.h>
  41. //#include <media/tuner.h>
  42. #include <media/v4l2-common.h>
  43. #include "em28xx.h"
  44. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  45. static int em28xx_cmd(struct em28xx *dev, int cmd, int arg);
  46. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  47. size_t size)
  48. {
  49. struct snd_pcm_runtime *runtime = subs->runtime;
  50. if (runtime->dma_area) {
  51. if (runtime->dma_bytes > size)
  52. return 0;
  53. vfree(runtime->dma_area);
  54. }
  55. runtime->dma_area = vmalloc(size);
  56. if (!runtime->dma_area)
  57. return -ENOMEM;
  58. runtime->dma_bytes = size;
  59. return 0;
  60. }
  61. static struct snd_pcm_hardware snd_em28xx_hw_capture = {
  62. .info =
  63. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
  64. SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID,
  65. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  66. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
  67. .rate_min = 48000,
  68. .rate_max = 48000,
  69. .channels_min = 2,
  70. .channels_max = 2,
  71. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  72. .period_bytes_min = 64, //12544/2,
  73. .period_bytes_max = 12544,
  74. .periods_min = 2,
  75. .periods_max = 98, //12544,
  76. };
  77. static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
  78. {
  79. int ret = 0;
  80. int mode;
  81. struct em28xx *dev = snd_pcm_substream_chip(substream);
  82. struct snd_pcm_runtime *runtime = substream->runtime;
  83. printk("opening radio device and trying to acquire exclusive lock\n");
  84. switch (dev->mode) {
  85. case TUNER_STUB_DVBC_TV:
  86. case TUNER_STUB_DVBT_TV:
  87. case TUNER_STUB_ATSC_TV:
  88. /* digital has no support for analog audio */
  89. if (ret != 0) {
  90. printk("device is already in use by DVB-T\n");
  91. return -EINVAL;
  92. } else {
  93. struct v4l2_tuner tuner;
  94. printk("switching device to FM mode\n");
  95. mode = TUNER_STUB_RADIO;
  96. memset(&tuner, 0x0, sizeof(struct v4l2_tuner));
  97. tuner.type = V4L2_TUNER_RADIO;
  98. /* enable GPIO for analog TV */
  99. dev->em28xx_gpio_control(dev, EM28XX_MODE,
  100. (void *)mode);
  101. dev->mode = mode;
  102. /* upload firmware */
  103. tuner_run_cmd(dev->tobj, TUNER_CMD_INIT, (void *)mode);
  104. /* required for devices which have kerneldriver dependencies */
  105. // em28xx_config(dev);
  106. // em28xx_config_i2c(dev);
  107. /* this is moreover to switch the decoder to FM */
  108. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, &tuner);
  109. dev->em28xx_write_regs(dev, 0x0f, "\x87", 1);
  110. ret = dev->em28xx_acquire(dev, EM28XX_RADIO, 1);
  111. em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, 0);
  112. /* TODO switch to FM mode */
  113. printk("em28xx-audio: %d mode\n", mode);
  114. tuner_run_cmd(dev->tobj, TUNER_CMD_G_MODE, &mode);
  115. printk("retrieved mode from tuner: %d\n", mode);
  116. }
  117. break;
  118. case TUNER_STUB_ANALOG_TV:
  119. printk("em28xx-audio: device is currently in analog TV mode\n");
  120. /* unmute by default */
  121. dev->em28xx_write_regs(dev, 0x0f, "\x87", 1);
  122. break;
  123. case TUNER_STUB_RADIO:
  124. /* check current mode and put a hard lock onto it */
  125. printk
  126. ("em28xx-audio: device is currently in analogue FM mode\n");
  127. /* unmute by default here */
  128. dev->em28xx_write_regs(dev, 0x0f, "\x87", 1);
  129. ret = dev->em28xx_acquire(dev, EM28XX_RADIO, 1);
  130. if (ret == 0)
  131. printk("device is locked in fmradio mode now\n");
  132. break;
  133. default:
  134. printk("em28xx-audio: unhandled mode %d\n", dev->mode);
  135. }
  136. runtime->hw = snd_em28xx_hw_capture;
  137. if (dev->alt == 0 && dev->adev->users == 0) {
  138. int errCode;
  139. dev->alt = 7;
  140. errCode = usb_set_interface(dev->udev, 0, 7);
  141. printk("changing alternate number to 7\n");
  142. }
  143. dev->adev->users++;
  144. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  145. dev->adev->capture_pcm_substream = substream;
  146. runtime->private_data = dev;
  147. return 0;
  148. }
  149. static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
  150. {
  151. struct em28xx *dev = snd_pcm_substream_chip(substream);
  152. int amode = 0;
  153. dev->adev->users--;
  154. /* decrease audio reference */
  155. switch (dev->mode) {
  156. case TUNER_STUB_ANALOG_TV:
  157. amode = EM28XX_VIDEO;
  158. break;
  159. case TUNER_STUB_RADIO:
  160. amode = EM28XX_RADIO;
  161. break;
  162. default:
  163. printk("invalid mode: %d\n", dev->mode);
  164. break;
  165. }
  166. dev->em28xx_acquire(dev, amode, 0);
  167. if (dev->adev->users == 0 && dev->adev->shutdown == 1) {
  168. printk("audio users: %d\n", dev->adev->users);
  169. printk("disabling audio stream!\n");
  170. dev->adev->shutdown = 0;
  171. printk("released lock\n");
  172. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
  173. }
  174. return 0;
  175. }
  176. static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
  177. struct snd_pcm_hw_params *hw_params)
  178. {
  179. unsigned int channels, rate, format;
  180. int ret;
  181. ret =
  182. snd_pcm_alloc_vmalloc_buffer(substream,
  183. params_buffer_bytes(hw_params));
  184. format = params_format(hw_params);
  185. rate = params_rate(hw_params);
  186. channels = params_channels(hw_params);
  187. /* TODO: set up em28xx audio chip to deliver the correct audio format, current default is 48000hz multiplexed => 96000hz mono
  188. which shouldn't matter since analogue TV only supports mono */
  189. return 0;
  190. }
  191. static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
  192. {
  193. struct em28xx *dev = snd_pcm_substream_chip(substream);
  194. if (dev->adev->capture_stream == STREAM_ON) {
  195. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
  196. }
  197. return 0;
  198. }
  199. static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
  200. {
  201. return 0;
  202. }
  203. static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
  204. int cmd)
  205. {
  206. struct em28xx *dev = snd_pcm_substream_chip(substream);
  207. switch (cmd) {
  208. case SNDRV_PCM_TRIGGER_START:
  209. em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
  210. return 0;
  211. case SNDRV_PCM_TRIGGER_STOP:
  212. dev->adev->shutdown = 1;
  213. return 0;
  214. default:
  215. return -EINVAL;
  216. }
  217. }
  218. static void em28xx_audio_isocirq(struct urb *urb)
  219. {
  220. struct em28xx *dev = urb->context;
  221. int i;
  222. unsigned int oldptr;
  223. unsigned long flags;
  224. int period_elapsed = 0;
  225. int status;
  226. unsigned char *cp;
  227. unsigned int stride;
  228. struct snd_pcm_substream *substream;
  229. struct snd_pcm_runtime *runtime;
  230. if (dev->adev->capture_pcm_substream) {
  231. substream = dev->adev->capture_pcm_substream;
  232. runtime = substream->runtime;
  233. stride = runtime->frame_bits >> 3;
  234. for (i = 0; i < urb->number_of_packets; i++) {
  235. int length =
  236. urb->iso_frame_desc[i].actual_length / stride;
  237. cp = (unsigned char *)urb->transfer_buffer +
  238. urb->iso_frame_desc[i].offset;
  239. if (!length)
  240. continue;
  241. spin_lock_irqsave(&dev->adev->slock, flags);
  242. oldptr = dev->adev->hwptr_done_capture;
  243. dev->adev->hwptr_done_capture += length;
  244. if (dev->adev->hwptr_done_capture >=
  245. runtime->buffer_size)
  246. dev->adev->hwptr_done_capture -=
  247. runtime->buffer_size;
  248. dev->adev->capture_transfer_done += length;
  249. if (dev->adev->capture_transfer_done >=
  250. runtime->period_size) {
  251. dev->adev->capture_transfer_done -=
  252. runtime->period_size;
  253. period_elapsed = 1;
  254. }
  255. spin_unlock_irqrestore(&dev->adev->slock, flags);
  256. if (oldptr + length >= runtime->buffer_size) {
  257. unsigned int cnt =
  258. runtime->buffer_size - oldptr - 1;
  259. memcpy(runtime->dma_area + oldptr * stride, cp,
  260. cnt * stride);
  261. memcpy(runtime->dma_area, cp + cnt,
  262. length * stride - cnt * stride);
  263. } else {
  264. memcpy(runtime->dma_area + oldptr * stride, cp,
  265. length * stride);
  266. }
  267. }
  268. if (period_elapsed) {
  269. snd_pcm_period_elapsed(substream);
  270. }
  271. }
  272. urb->status = 0;
  273. if (dev->adev->shutdown)
  274. return;
  275. if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
  276. em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
  277. status);
  278. }
  279. return;
  280. }
  281. static int em28xx_isoc_audio_deinit(struct em28xx *dev)
  282. {
  283. int i;
  284. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  285. usb_kill_urb(dev->adev->urb[i]);
  286. usb_free_urb(dev->adev->urb[i]);
  287. dev->adev->urb[i] = NULL;
  288. }
  289. return 0;
  290. }
  291. static int em28xx_init_audio_isoc(struct em28xx *dev)
  292. {
  293. int i;
  294. int errCode;
  295. const int sb_size =
  296. EM28XX_NUM_AUDIO_PACKETS * EM28XX_AUDIO_MAX_PACKET_SIZE;
  297. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  298. struct urb *urb;
  299. int j, k;
  300. dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
  301. if (!dev->adev->transfer_buffer[i]) {
  302. return -ENOMEM;
  303. }
  304. memset(dev->adev->transfer_buffer[i], 0x80, sb_size);
  305. urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
  306. if (urb) {
  307. urb->dev = dev->udev;
  308. urb->context = dev;
  309. urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
  310. urb->transfer_flags = URB_ISO_ASAP;
  311. urb->transfer_buffer = dev->adev->transfer_buffer[i];
  312. urb->interval = 1;
  313. urb->complete = em28xx_audio_isocirq;
  314. urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
  315. urb->transfer_buffer_length = sb_size;
  316. for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
  317. j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
  318. urb->iso_frame_desc[j].offset = k;
  319. urb->iso_frame_desc[j].length =
  320. EM28XX_AUDIO_MAX_PACKET_SIZE;
  321. }
  322. dev->adev->urb[i] = urb;
  323. } else {
  324. return -ENOMEM;
  325. }
  326. }
  327. for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
  328. errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC);
  329. if (errCode) {
  330. em28xx_isoc_audio_deinit(dev);
  331. return errCode;
  332. }
  333. }
  334. return 0;
  335. }
  336. static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
  337. {
  338. switch (cmd) {
  339. case EM28XX_CAPTURE_STREAM_EN:
  340. if (dev->adev->capture_stream == STREAM_OFF && arg == 1) {
  341. dev->adev->capture_stream = STREAM_ON;
  342. em28xx_init_audio_isoc(dev);
  343. } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) {
  344. dev->adev->capture_stream = STREAM_OFF;
  345. em28xx_isoc_audio_deinit(dev);
  346. } else {
  347. printk
  348. ("An underrun occured very likely... ignoring it\n");
  349. }
  350. return 0;
  351. default:
  352. return -EINVAL;
  353. }
  354. }
  355. static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
  356. *substream)
  357. {
  358. struct em28xx *dev;
  359. snd_pcm_uframes_t hwptr_done;
  360. dev = snd_pcm_substream_chip(substream);
  361. hwptr_done = dev->adev->hwptr_done_capture;
  362. return hwptr_done;
  363. }
  364. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  365. unsigned long offset)
  366. {
  367. void *pageptr = subs->runtime->dma_area + offset;
  368. return vmalloc_to_page(pageptr);
  369. }
  370. static struct snd_pcm_ops snd_em28xx_pcm_capture = {
  371. .open = snd_em28xx_capture_open,
  372. .close = snd_em28xx_pcm_close,
  373. .ioctl = snd_pcm_lib_ioctl,
  374. .hw_params = snd_em28xx_hw_capture_params,
  375. .hw_free = snd_em28xx_hw_capture_free,
  376. .prepare = snd_em28xx_prepare,
  377. .trigger = snd_em28xx_capture_trigger,
  378. .pointer = snd_em28xx_capture_pointer,
  379. .page = snd_pcm_get_vmalloc_page,
  380. };
  381. static int em28xx_audio_init(struct em28xx *dev)
  382. {
  383. struct em28xx_audio *adev;
  384. struct snd_pcm *pcm;
  385. struct snd_card *card;
  386. static int devnr;
  387. int ret;
  388. int err;
  389. printk("em28xx-audio.c: probing for em28x1 non standard usbaudio\n");
  390. printk("em28xx-audio.c: Copyright (C) 2006 Markus Rechberger\n");
  391. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  392. if (!adev) {
  393. printk("em28xx-audio.c: out of memory\n");
  394. return -1;
  395. }
  396. card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
  397. if (card == NULL) {
  398. kfree(adev);
  399. return -ENOMEM;
  400. }
  401. spin_lock_init(&adev->slock);
  402. ret = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  403. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  404. pcm->info_flags = 0;
  405. pcm->private_data = dev;
  406. strcpy(pcm->name, "Empia 28xx Capture");
  407. strcpy(card->driver, "Empia Em28xx Audio");
  408. strcpy(card->shortname, "Em28xx Audio");
  409. strcpy(card->longname, "Empia Em28xx Audio");
  410. if ((err = snd_card_register(card)) < 0) {
  411. snd_card_free(card);
  412. return -ENOMEM;
  413. }
  414. adev->sndcard = card;
  415. adev->udev = dev->udev;
  416. dev->adev = adev;
  417. return 0;
  418. }
  419. static int em28xx_audio_fini(struct em28xx *dev)
  420. {
  421. if (dev == NULL)
  422. return 0;
  423. if (dev->adev) {
  424. snd_card_free(dev->adev->sndcard);
  425. kfree(dev->adev);
  426. dev->adev = NULL;
  427. }
  428. return 0;
  429. }
  430. static struct em28xx_ops audio_ops = {
  431. .id = EM28XX_AUDIO,
  432. .name = "Em28xx Audio Extension",
  433. .init = em28xx_audio_init,
  434. .fini = em28xx_audio_fini,
  435. };
  436. static int __init em28xx_alsa_register(void)
  437. {
  438. request_module("em28xx");
  439. request_module("tuner");
  440. return em28xx_register_extension(&audio_ops);
  441. }
  442. static void __exit em28xx_alsa_unregister(void)
  443. {
  444. em28xx_unregister_extension(&audio_ops);
  445. }
  446. MODULE_LICENSE("GPL");
  447. MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
  448. MODULE_DESCRIPTION("Em28xx Audio driver");
  449. module_init(em28xx_alsa_register);
  450. module_exit(em28xx_alsa_unregister);