Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2013 Lubomir Rintel
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  * Following LWN articles were very useful in construction of this driver:
0036  * Video4Linux2 API series: http://lwn.net/Articles/203924/
0037  * videobuf2 API explanation: http://lwn.net/Articles/447435/
0038  * Thanks go to Jonathan Corbet for providing this quality documentation.
0039  * He is awesome.
0040  *
0041  * No physical hardware was harmed running Windows during the
0042  * reverse-engineering activity
0043  */
0044 
0045 #include "usbtv.h"
0046 
0047 int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
0048 {
0049     int ret;
0050     int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
0051     int i;
0052 
0053     for (i = 0; i < size; i++) {
0054         u16 index = regs[i][0];
0055         u16 value = regs[i][1];
0056 
0057         ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
0058             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0059             value, index, NULL, 0, USB_CTRL_GET_TIMEOUT);
0060         if (ret < 0)
0061             return ret;
0062     }
0063 
0064     return 0;
0065 }
0066 
0067 static int usbtv_probe(struct usb_interface *intf,
0068     const struct usb_device_id *id)
0069 {
0070     int ret;
0071     int size;
0072     struct device *dev = &intf->dev;
0073     struct usbtv *usbtv;
0074     struct usb_host_endpoint *ep;
0075 
0076     /* Checks that the device is what we think it is. */
0077     if (intf->num_altsetting != 2)
0078         return -ENODEV;
0079     if (intf->altsetting[1].desc.bNumEndpoints != 4)
0080         return -ENODEV;
0081 
0082     ep = &intf->altsetting[1].endpoint[0];
0083 
0084     /* Packet size is split into 11 bits of base size and count of
0085      * extra multiplies of it.*/
0086     size = usb_endpoint_maxp(&ep->desc);
0087     size = size * usb_endpoint_maxp_mult(&ep->desc);
0088 
0089     /* Device structure */
0090     usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
0091     if (usbtv == NULL)
0092         return -ENOMEM;
0093     usbtv->dev = dev;
0094     usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
0095 
0096     usbtv->iso_size = size;
0097 
0098     usb_set_intfdata(intf, usbtv);
0099 
0100     ret = usbtv_video_init(usbtv);
0101     if (ret < 0)
0102         goto usbtv_video_fail;
0103 
0104     ret = usbtv_audio_init(usbtv);
0105     if (ret < 0)
0106         goto usbtv_audio_fail;
0107 
0108     /* for simplicity we exploit the v4l2_device reference counting */
0109     v4l2_device_get(&usbtv->v4l2_dev);
0110 
0111     dev_info(dev, "Fushicai USBTV007 Audio-Video Grabber\n");
0112     return 0;
0113 
0114 usbtv_audio_fail:
0115     /* we must not free at this point */
0116     v4l2_device_get(&usbtv->v4l2_dev);
0117     /* this will undo the v4l2_device_get() */
0118     usbtv_video_free(usbtv);
0119 
0120 usbtv_video_fail:
0121     usb_set_intfdata(intf, NULL);
0122     usb_put_dev(usbtv->udev);
0123     kfree(usbtv);
0124 
0125     return ret;
0126 }
0127 
0128 static void usbtv_disconnect(struct usb_interface *intf)
0129 {
0130     struct usbtv *usbtv = usb_get_intfdata(intf);
0131 
0132     usb_set_intfdata(intf, NULL);
0133 
0134     if (!usbtv)
0135         return;
0136 
0137     usbtv_audio_free(usbtv);
0138     usbtv_video_free(usbtv);
0139 
0140     usb_put_dev(usbtv->udev);
0141     usbtv->udev = NULL;
0142 
0143     /* the usbtv structure will be deallocated when v4l2 will be
0144        done using it */
0145     v4l2_device_put(&usbtv->v4l2_dev);
0146 }
0147 
0148 static const struct usb_device_id usbtv_id_table[] = {
0149     { USB_DEVICE(0x1b71, 0x3002) },
0150     { USB_DEVICE(0x1f71, 0x3301) },
0151     { USB_DEVICE(0x1f71, 0x3306) },
0152     {}
0153 };
0154 MODULE_DEVICE_TABLE(usb, usbtv_id_table);
0155 
0156 MODULE_AUTHOR("Lubomir Rintel, Federico Simoncelli");
0157 MODULE_DESCRIPTION("Fushicai USBTV007 Audio-Video Grabber Driver");
0158 MODULE_LICENSE("Dual BSD/GPL");
0159 
0160 static struct usb_driver usbtv_usb_driver = {
0161     .name = "usbtv",
0162     .id_table = usbtv_id_table,
0163     .probe = usbtv_probe,
0164     .disconnect = usbtv_disconnect,
0165 };
0166 
0167 module_usb_driver(usbtv_usb_driver);