Path: blob/master/drivers/media/video/gspca/sq905c.c
17653 views
/*1* SQ905C subdriver2*3* Copyright (C) 2009 Theodore Kilgore4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*/1920/*21*22* This driver uses work done in23* libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.24*25* This driver has also used as a base the sq905c driver26* and may contain code fragments from it.27*/2829#define MODULE_NAME "sq905c"3031#include <linux/workqueue.h>32#include <linux/slab.h>33#include "gspca.h"3435MODULE_AUTHOR("Theodore Kilgore <[email protected]>");36MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");37MODULE_LICENSE("GPL");3839/* Default timeouts, in ms */40#define SQ905C_CMD_TIMEOUT 50041#define SQ905C_DATA_TIMEOUT 10004243/* Maximum transfer size to use. */44#define SQ905C_MAX_TRANSFER 0x80004546#define FRAME_HEADER_LEN 0x504748/* Commands. These go in the "value" slot. */49#define SQ905C_CLEAR 0xa0 /* clear everything */50#define SQ905C_GET_ID 0x14f4 /* Read version number */51#define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */52#define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */53#define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */5455/* For capture, this must go in the "index" slot. */56#define SQ905C_CAPTURE_INDEX 0x110f5758/* Structure to hold all of our device specific stuff */59struct sd {60struct gspca_dev gspca_dev; /* !! must be the first item */61const struct v4l2_pix_format *cap_mode;62/* Driver stuff */63struct work_struct work_struct;64struct workqueue_struct *work_thread;65};6667/*68* Most of these cameras will do 640x480 and 320x240. 160x120 works69* in theory but gives very poor output. Therefore, not supported.70* The 0x2770:0x9050 cameras have max resolution of 320x240.71*/72static struct v4l2_pix_format sq905c_mode[] = {73{ 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,74.bytesperline = 320,75.sizeimage = 320 * 240,76.colorspace = V4L2_COLORSPACE_SRGB,77.priv = 0},78{ 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,79.bytesperline = 640,80.sizeimage = 640 * 480,81.colorspace = V4L2_COLORSPACE_SRGB,82.priv = 0}83};8485/* Send a command to the camera. */86static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)87{88int ret;8990ret = usb_control_msg(gspca_dev->dev,91usb_sndctrlpipe(gspca_dev->dev, 0),92USB_REQ_SYNCH_FRAME, /* request */93USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,94command, index, NULL, 0,95SQ905C_CMD_TIMEOUT);96if (ret < 0) {97err("%s: usb_control_msg failed (%d)",98__func__, ret);99return ret;100}101102return 0;103}104105static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,106int size)107{108int ret;109110ret = usb_control_msg(gspca_dev->dev,111usb_rcvctrlpipe(gspca_dev->dev, 0),112USB_REQ_SYNCH_FRAME, /* request */113USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,114command, index, gspca_dev->usb_buf, size,115SQ905C_CMD_TIMEOUT);116if (ret < 0) {117err("%s: usb_control_msg failed (%d)",118__func__, ret);119return ret;120}121122return 0;123}124125/* This function is called as a workqueue function and runs whenever the camera126* is streaming data. Because it is a workqueue function it is allowed to sleep127* so we can use synchronous USB calls. To avoid possible collisions with other128* threads attempting to use the camera's USB interface the gspca usb_lock is129* used when performing the one USB control operation inside the workqueue,130* which tells the camera to close the stream. In practice the only thing131* which needs to be protected against is the usb_set_interface call that132* gspca makes during stream_off. Otherwise the camera doesn't provide any133* controls that the user could try to change.134*/135static void sq905c_dostream(struct work_struct *work)136{137struct sd *dev = container_of(work, struct sd, work_struct);138struct gspca_dev *gspca_dev = &dev->gspca_dev;139int bytes_left; /* bytes remaining in current frame. */140int data_len; /* size to use for the next read. */141int act_len;142int packet_type;143int ret;144u8 *buffer;145146buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);147if (!buffer) {148err("Couldn't allocate USB buffer");149goto quit_stream;150}151152while (gspca_dev->present && gspca_dev->streaming) {153/* Request the header, which tells the size to download */154ret = usb_bulk_msg(gspca_dev->dev,155usb_rcvbulkpipe(gspca_dev->dev, 0x81),156buffer, FRAME_HEADER_LEN, &act_len,157SQ905C_DATA_TIMEOUT);158PDEBUG(D_STREAM,159"Got %d bytes out of %d for header",160act_len, FRAME_HEADER_LEN);161if (ret < 0 || act_len < FRAME_HEADER_LEN)162goto quit_stream;163/* size is read from 4 bytes starting 0x40, little endian */164bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)165|(buffer[0x43]<<24);166PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);167/* We keep the header. It has other information, too. */168packet_type = FIRST_PACKET;169gspca_frame_add(gspca_dev, packet_type,170buffer, FRAME_HEADER_LEN);171while (bytes_left > 0 && gspca_dev->present) {172data_len = bytes_left > SQ905C_MAX_TRANSFER ?173SQ905C_MAX_TRANSFER : bytes_left;174ret = usb_bulk_msg(gspca_dev->dev,175usb_rcvbulkpipe(gspca_dev->dev, 0x81),176buffer, data_len, &act_len,177SQ905C_DATA_TIMEOUT);178if (ret < 0 || act_len < data_len)179goto quit_stream;180PDEBUG(D_STREAM,181"Got %d bytes out of %d for frame",182data_len, bytes_left);183bytes_left -= data_len;184if (bytes_left == 0)185packet_type = LAST_PACKET;186else187packet_type = INTER_PACKET;188gspca_frame_add(gspca_dev, packet_type,189buffer, data_len);190}191}192quit_stream:193if (gspca_dev->present) {194mutex_lock(&gspca_dev->usb_lock);195sq905c_command(gspca_dev, SQ905C_CLEAR, 0);196mutex_unlock(&gspca_dev->usb_lock);197}198kfree(buffer);199}200201/* This function is called at probe time just before sd_init */202static int sd_config(struct gspca_dev *gspca_dev,203const struct usb_device_id *id)204{205struct cam *cam = &gspca_dev->cam;206struct sd *dev = (struct sd *) gspca_dev;207int ret;208209PDEBUG(D_PROBE,210"SQ9050 camera detected"211" (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);212213ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);214if (ret < 0) {215PDEBUG(D_ERR, "Get version command failed");216return ret;217}218219ret = sq905c_read(gspca_dev, 0xf5, 0, 20);220if (ret < 0) {221PDEBUG(D_ERR, "Reading version command failed");222return ret;223}224/* Note we leave out the usb id and the manufacturing date */225PDEBUG(D_PROBE,226"SQ9050 ID string: %02x - %02x %02x %02x %02x %02x %02x",227gspca_dev->usb_buf[3],228gspca_dev->usb_buf[14], gspca_dev->usb_buf[15],229gspca_dev->usb_buf[16], gspca_dev->usb_buf[17],230gspca_dev->usb_buf[18], gspca_dev->usb_buf[19]);231232cam->cam_mode = sq905c_mode;233cam->nmodes = 2;234if (gspca_dev->usb_buf[15] == 0)235cam->nmodes = 1;236/* We don't use the buffer gspca allocates so make it small. */237cam->bulk_size = 32;238cam->bulk = 1;239INIT_WORK(&dev->work_struct, sq905c_dostream);240return 0;241}242243/* called on streamoff with alt==0 and on disconnect */244/* the usb_lock is held at entry - restore on exit */245static void sd_stop0(struct gspca_dev *gspca_dev)246{247struct sd *dev = (struct sd *) gspca_dev;248249/* wait for the work queue to terminate */250mutex_unlock(&gspca_dev->usb_lock);251/* This waits for sq905c_dostream to finish */252destroy_workqueue(dev->work_thread);253dev->work_thread = NULL;254mutex_lock(&gspca_dev->usb_lock);255}256257/* this function is called at probe and resume time */258static int sd_init(struct gspca_dev *gspca_dev)259{260int ret;261262/* connect to the camera and reset it. */263ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);264return ret;265}266267/* Set up for getting frames. */268static int sd_start(struct gspca_dev *gspca_dev)269{270struct sd *dev = (struct sd *) gspca_dev;271int ret;272273dev->cap_mode = gspca_dev->cam.cam_mode;274/* "Open the shutter" and set size, to start capture */275switch (gspca_dev->width) {276case 640:277PDEBUG(D_STREAM, "Start streaming at high resolution");278dev->cap_mode++;279ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,280SQ905C_CAPTURE_INDEX);281break;282default: /* 320 */283PDEBUG(D_STREAM, "Start streaming at medium resolution");284ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,285SQ905C_CAPTURE_INDEX);286}287288if (ret < 0) {289PDEBUG(D_ERR, "Start streaming command failed");290return ret;291}292/* Start the workqueue function to do the streaming */293dev->work_thread = create_singlethread_workqueue(MODULE_NAME);294queue_work(dev->work_thread, &dev->work_struct);295296return 0;297}298299/* Table of supported USB devices */300static const struct usb_device_id device_table[] = {301{USB_DEVICE(0x2770, 0x905c)},302{USB_DEVICE(0x2770, 0x9050)},303{USB_DEVICE(0x2770, 0x9051)},304{USB_DEVICE(0x2770, 0x9052)},305{USB_DEVICE(0x2770, 0x913d)},306{}307};308309MODULE_DEVICE_TABLE(usb, device_table);310311/* sub-driver description */312static const struct sd_desc sd_desc = {313.name = MODULE_NAME,314.config = sd_config,315.init = sd_init,316.start = sd_start,317.stop0 = sd_stop0,318};319320/* -- device connect -- */321static int sd_probe(struct usb_interface *intf,322const struct usb_device_id *id)323{324return gspca_dev_probe(intf, id,325&sd_desc,326sizeof(struct sd),327THIS_MODULE);328}329330static struct usb_driver sd_driver = {331.name = MODULE_NAME,332.id_table = device_table,333.probe = sd_probe,334.disconnect = gspca_disconnect,335#ifdef CONFIG_PM336.suspend = gspca_suspend,337.resume = gspca_resume,338#endif339};340341/* -- module insert / remove -- */342static int __init sd_mod_init(void)343{344return usb_register(&sd_driver);345}346347static void __exit sd_mod_exit(void)348{349usb_deregister(&sd_driver);350}351352module_init(sd_mod_init);353module_exit(sd_mod_exit);354355356