Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2013 Federico Simoncelli
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions, and the following disclaimer,
0010  *    without modification.
0011  * 2. The name of the author may not be used to endorse or promote products
0012  *    derived from this software without specific prior written permission.
0013  *
0014  * Alternatively, this software may be distributed under the terms of the
0015  * GNU General Public License ("GPL").
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0020  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0021  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0022  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0023  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0024  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0025  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0027  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028  */
0029 /*
0030  * Fushicai USBTV007 Audio-Video Grabber Driver
0031  *
0032  * Product web site:
0033  * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
0034  *
0035  * No physical hardware was harmed running Windows during the
0036  * reverse-engineering activity
0037  */
0038 
0039 #include <sound/core.h>
0040 #include <sound/initval.h>
0041 #include <sound/ac97_codec.h>
0042 #include <sound/pcm_params.h>
0043 
0044 #include "usbtv.h"
0045 
0046 static const struct snd_pcm_hardware snd_usbtv_digital_hw = {
0047     .info = SNDRV_PCM_INFO_BATCH |
0048         SNDRV_PCM_INFO_MMAP |
0049         SNDRV_PCM_INFO_INTERLEAVED |
0050         SNDRV_PCM_INFO_BLOCK_TRANSFER |
0051         SNDRV_PCM_INFO_MMAP_VALID,
0052     .formats = SNDRV_PCM_FMTBIT_S16_LE,
0053     .rates = SNDRV_PCM_RATE_48000,
0054     .rate_min = 48000,
0055     .rate_max = 48000,
0056     .channels_min = 2,
0057     .channels_max = 2,
0058     .period_bytes_min = 11059,
0059     .period_bytes_max = 13516,
0060     .periods_min = 2,
0061     .periods_max = 98,
0062     .buffer_bytes_max = 62720 * 8, /* value in usbaudio.c */
0063 };
0064 
0065 static int snd_usbtv_pcm_open(struct snd_pcm_substream *substream)
0066 {
0067     struct usbtv *chip = snd_pcm_substream_chip(substream);
0068     struct snd_pcm_runtime *runtime = substream->runtime;
0069 
0070     chip->snd_substream = substream;
0071     runtime->hw = snd_usbtv_digital_hw;
0072 
0073     return 0;
0074 }
0075 
0076 static int snd_usbtv_pcm_close(struct snd_pcm_substream *substream)
0077 {
0078     struct usbtv *chip = snd_pcm_substream_chip(substream);
0079 
0080     if (atomic_read(&chip->snd_stream)) {
0081         atomic_set(&chip->snd_stream, 0);
0082         schedule_work(&chip->snd_trigger);
0083     }
0084 
0085     return 0;
0086 }
0087 
0088 static int snd_usbtv_prepare(struct snd_pcm_substream *substream)
0089 {
0090     struct usbtv *chip = snd_pcm_substream_chip(substream);
0091 
0092     chip->snd_buffer_pos = 0;
0093     chip->snd_period_pos = 0;
0094 
0095     return 0;
0096 }
0097 
0098 static void usbtv_audio_urb_received(struct urb *urb)
0099 {
0100     struct usbtv *chip = urb->context;
0101     struct snd_pcm_substream *substream = chip->snd_substream;
0102     struct snd_pcm_runtime *runtime = substream->runtime;
0103     size_t i, frame_bytes, chunk_length, buffer_pos, period_pos;
0104     int period_elapsed;
0105     unsigned long flags;
0106     void *urb_current;
0107 
0108     switch (urb->status) {
0109     case 0:
0110     case -ETIMEDOUT:
0111         break;
0112     case -ENOENT:
0113     case -EPROTO:
0114     case -ECONNRESET:
0115     case -ESHUTDOWN:
0116         return;
0117     default:
0118         dev_warn(chip->dev, "unknown audio urb status %i\n",
0119             urb->status);
0120     }
0121 
0122     if (!atomic_read(&chip->snd_stream))
0123         return;
0124 
0125     frame_bytes = runtime->frame_bits >> 3;
0126     chunk_length = USBTV_CHUNK / frame_bytes;
0127 
0128     buffer_pos = chip->snd_buffer_pos;
0129     period_pos = chip->snd_period_pos;
0130     period_elapsed = 0;
0131 
0132     for (i = 0; i < urb->actual_length; i += USBTV_CHUNK_SIZE) {
0133         urb_current = urb->transfer_buffer + i + USBTV_AUDIO_HDRSIZE;
0134 
0135         if (buffer_pos + chunk_length >= runtime->buffer_size) {
0136             size_t cnt = (runtime->buffer_size - buffer_pos) *
0137                 frame_bytes;
0138             memcpy(runtime->dma_area + buffer_pos * frame_bytes,
0139                 urb_current, cnt);
0140             memcpy(runtime->dma_area, urb_current + cnt,
0141                 chunk_length * frame_bytes - cnt);
0142         } else {
0143             memcpy(runtime->dma_area + buffer_pos * frame_bytes,
0144                 urb_current, chunk_length * frame_bytes);
0145         }
0146 
0147         buffer_pos += chunk_length;
0148         period_pos += chunk_length;
0149 
0150         if (buffer_pos >= runtime->buffer_size)
0151             buffer_pos -= runtime->buffer_size;
0152 
0153         if (period_pos >= runtime->period_size) {
0154             period_pos -= runtime->period_size;
0155             period_elapsed = 1;
0156         }
0157     }
0158 
0159     snd_pcm_stream_lock_irqsave(substream, flags);
0160 
0161     chip->snd_buffer_pos = buffer_pos;
0162     chip->snd_period_pos = period_pos;
0163 
0164     snd_pcm_stream_unlock_irqrestore(substream, flags);
0165 
0166     if (period_elapsed)
0167         snd_pcm_period_elapsed(substream);
0168 
0169     usb_submit_urb(urb, GFP_ATOMIC);
0170 }
0171 
0172 static int usbtv_audio_start(struct usbtv *chip)
0173 {
0174     unsigned int pipe;
0175     static const u16 setup[][2] = {
0176         /* These seem to enable the device. */
0177         { USBTV_BASE + 0x0008, 0x0001 },
0178         { USBTV_BASE + 0x01d0, 0x00ff },
0179         { USBTV_BASE + 0x01d9, 0x0002 },
0180 
0181         { USBTV_BASE + 0x01da, 0x0013 },
0182         { USBTV_BASE + 0x01db, 0x0012 },
0183         { USBTV_BASE + 0x01e9, 0x0002 },
0184         { USBTV_BASE + 0x01ec, 0x006c },
0185         { USBTV_BASE + 0x0294, 0x0020 },
0186         { USBTV_BASE + 0x0255, 0x00cf },
0187         { USBTV_BASE + 0x0256, 0x0020 },
0188         { USBTV_BASE + 0x01eb, 0x0030 },
0189         { USBTV_BASE + 0x027d, 0x00a6 },
0190         { USBTV_BASE + 0x0280, 0x0011 },
0191         { USBTV_BASE + 0x0281, 0x0040 },
0192         { USBTV_BASE + 0x0282, 0x0011 },
0193         { USBTV_BASE + 0x0283, 0x0040 },
0194         { 0xf891, 0x0010 },
0195 
0196         /* this sets the input from composite */
0197         { USBTV_BASE + 0x0284, 0x00aa },
0198     };
0199 
0200     chip->snd_bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
0201     if (chip->snd_bulk_urb == NULL)
0202         goto err_alloc_urb;
0203 
0204     pipe = usb_rcvbulkpipe(chip->udev, USBTV_AUDIO_ENDP);
0205 
0206     chip->snd_bulk_urb->transfer_buffer = kzalloc(
0207         USBTV_AUDIO_URBSIZE, GFP_KERNEL);
0208     if (chip->snd_bulk_urb->transfer_buffer == NULL)
0209         goto err_transfer_buffer;
0210 
0211     usb_fill_bulk_urb(chip->snd_bulk_urb, chip->udev, pipe,
0212         chip->snd_bulk_urb->transfer_buffer, USBTV_AUDIO_URBSIZE,
0213         usbtv_audio_urb_received, chip);
0214 
0215     /* starting the stream */
0216     usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
0217 
0218     usb_clear_halt(chip->udev, pipe);
0219     usb_submit_urb(chip->snd_bulk_urb, GFP_ATOMIC);
0220 
0221     return 0;
0222 
0223 err_transfer_buffer:
0224     usb_free_urb(chip->snd_bulk_urb);
0225     chip->snd_bulk_urb = NULL;
0226 
0227 err_alloc_urb:
0228     return -ENOMEM;
0229 }
0230 
0231 static int usbtv_audio_stop(struct usbtv *chip)
0232 {
0233     static const u16 setup[][2] = {
0234     /* The original windows driver sometimes sends also:
0235      *   { USBTV_BASE + 0x00a2, 0x0013 }
0236      * but it seems useless and its real effects are untested at
0237      * the moment.
0238      */
0239         { USBTV_BASE + 0x027d, 0x0000 },
0240         { USBTV_BASE + 0x0280, 0x0010 },
0241         { USBTV_BASE + 0x0282, 0x0010 },
0242     };
0243 
0244     if (chip->snd_bulk_urb) {
0245         usb_kill_urb(chip->snd_bulk_urb);
0246         kfree(chip->snd_bulk_urb->transfer_buffer);
0247         usb_free_urb(chip->snd_bulk_urb);
0248         chip->snd_bulk_urb = NULL;
0249     }
0250 
0251     usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
0252 
0253     return 0;
0254 }
0255 
0256 void usbtv_audio_suspend(struct usbtv *usbtv)
0257 {
0258     if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
0259         usb_kill_urb(usbtv->snd_bulk_urb);
0260 }
0261 
0262 void usbtv_audio_resume(struct usbtv *usbtv)
0263 {
0264     if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
0265         usb_submit_urb(usbtv->snd_bulk_urb, GFP_ATOMIC);
0266 }
0267 
0268 static void snd_usbtv_trigger(struct work_struct *work)
0269 {
0270     struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
0271 
0272     if (!chip->snd)
0273         return;
0274 
0275     if (atomic_read(&chip->snd_stream))
0276         usbtv_audio_start(chip);
0277     else
0278         usbtv_audio_stop(chip);
0279 }
0280 
0281 static int snd_usbtv_card_trigger(struct snd_pcm_substream *substream, int cmd)
0282 {
0283     struct usbtv *chip = snd_pcm_substream_chip(substream);
0284 
0285     switch (cmd) {
0286     case SNDRV_PCM_TRIGGER_START:
0287     case SNDRV_PCM_TRIGGER_RESUME:
0288     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0289         atomic_set(&chip->snd_stream, 1);
0290         break;
0291     case SNDRV_PCM_TRIGGER_STOP:
0292     case SNDRV_PCM_TRIGGER_SUSPEND:
0293     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0294         atomic_set(&chip->snd_stream, 0);
0295         break;
0296     default:
0297         return -EINVAL;
0298     }
0299 
0300     schedule_work(&chip->snd_trigger);
0301 
0302     return 0;
0303 }
0304 
0305 static snd_pcm_uframes_t snd_usbtv_pointer(struct snd_pcm_substream *substream)
0306 {
0307     struct usbtv *chip = snd_pcm_substream_chip(substream);
0308 
0309     return chip->snd_buffer_pos;
0310 }
0311 
0312 static const struct snd_pcm_ops snd_usbtv_pcm_ops = {
0313     .open = snd_usbtv_pcm_open,
0314     .close = snd_usbtv_pcm_close,
0315     .prepare = snd_usbtv_prepare,
0316     .trigger = snd_usbtv_card_trigger,
0317     .pointer = snd_usbtv_pointer,
0318 };
0319 
0320 int usbtv_audio_init(struct usbtv *usbtv)
0321 {
0322     int rv;
0323     struct snd_card *card;
0324     struct snd_pcm *pcm;
0325 
0326     INIT_WORK(&usbtv->snd_trigger, snd_usbtv_trigger);
0327     atomic_set(&usbtv->snd_stream, 0);
0328 
0329     rv = snd_card_new(&usbtv->udev->dev, SNDRV_DEFAULT_IDX1, "usbtv",
0330         THIS_MODULE, 0, &card);
0331     if (rv < 0)
0332         return rv;
0333 
0334     strscpy(card->driver, usbtv->dev->driver->name, sizeof(card->driver));
0335     strscpy(card->shortname, "usbtv", sizeof(card->shortname));
0336     snprintf(card->longname, sizeof(card->longname),
0337         "USBTV Audio at bus %d device %d", usbtv->udev->bus->busnum,
0338         usbtv->udev->devnum);
0339 
0340     snd_card_set_dev(card, usbtv->dev);
0341 
0342     usbtv->snd = card;
0343 
0344     rv = snd_pcm_new(card, "USBTV Audio", 0, 0, 1, &pcm);
0345     if (rv < 0)
0346         goto err;
0347 
0348     strscpy(pcm->name, "USBTV Audio Input", sizeof(pcm->name));
0349     pcm->info_flags = 0;
0350     pcm->private_data = usbtv;
0351 
0352     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usbtv_pcm_ops);
0353     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
0354         NULL, USBTV_AUDIO_BUFFER, USBTV_AUDIO_BUFFER);
0355 
0356     rv = snd_card_register(card);
0357     if (rv)
0358         goto err;
0359 
0360     return 0;
0361 
0362 err:
0363     usbtv->snd = NULL;
0364     snd_card_free(card);
0365 
0366     return rv;
0367 }
0368 
0369 void usbtv_audio_free(struct usbtv *usbtv)
0370 {
0371     cancel_work_sync(&usbtv->snd_trigger);
0372 
0373     if (usbtv->snd && usbtv->udev) {
0374         snd_card_free_when_closed(usbtv->snd);
0375         usbtv->snd = NULL;
0376     }
0377 }