Path: blob/master/drivers/media/video/gspca/sq905.c
17633 views
/*1* SQ905 subdriver2*3* Copyright (C) 2008, 2009 Adam Baker and 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* History and Acknowledgments22*23* The original Linux driver for SQ905 based cameras was written by24* Marcell Lengyel and furter developed by many other contributors25* and is available from http://sourceforge.net/projects/sqcam/26*27* This driver takes advantage of the reverse engineering work done for28* that driver and for libgphoto2 but shares no code with them.29*30* This driver has used as a base the finepix driver and other gspca31* based drivers and may still contain code fragments taken from those32* drivers.33*/3435#define MODULE_NAME "sq905"3637#include <linux/workqueue.h>38#include <linux/slab.h>39#include "gspca.h"4041MODULE_AUTHOR("Adam Baker <[email protected]>, "42"Theodore Kilgore <[email protected]>");43MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");44MODULE_LICENSE("GPL");4546/* Default timeouts, in ms */47#define SQ905_CMD_TIMEOUT 50048#define SQ905_DATA_TIMEOUT 10004950/* Maximum transfer size to use. */51#define SQ905_MAX_TRANSFER 0x800052#define FRAME_HEADER_LEN 645354/* The known modes, or registers. These go in the "value" slot. */5556/* 00 is "none" obviously */5758#define SQ905_BULK_READ 0x03 /* precedes any bulk read */59#define SQ905_COMMAND 0x06 /* precedes the command codes below */60#define SQ905_PING 0x07 /* when reading an "idling" command */61#define SQ905_READ_DONE 0xc0 /* ack bulk read completed */6263/* Any non-zero value in the bottom 2 bits of the 2nd byte of64* the ID appears to indicate the camera can do 640*480. If the65* LSB of that byte is set the image is just upside down, otherwise66* it is rotated 180 degrees. */67#define SQ905_HIRES_MASK 0x0000030068#define SQ905_ORIENTATION_MASK 0x000001006970/* Some command codes. These go in the "index" slot. */7172#define SQ905_ID 0xf0 /* asks for model string */73#define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */74#define SQ905_DATA 0x30 /* accesses photo data, not used here */75#define SQ905_CLEAR 0xa0 /* clear everything */76#define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */77#define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */78#define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */79/* note that the capture command also controls the output dimensions */8081/* Structure to hold all of our device specific stuff */82struct sd {83struct gspca_dev gspca_dev; /* !! must be the first item */8485/*86* Driver stuff87*/88struct work_struct work_struct;89struct workqueue_struct *work_thread;90};9192static struct v4l2_pix_format sq905_mode[] = {93{ 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,94.bytesperline = 160,95.sizeimage = 160 * 120,96.colorspace = V4L2_COLORSPACE_SRGB,97.priv = 0},98{ 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,99.bytesperline = 320,100.sizeimage = 320 * 240,101.colorspace = V4L2_COLORSPACE_SRGB,102.priv = 0},103{ 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,104.bytesperline = 640,105.sizeimage = 640 * 480,106.colorspace = V4L2_COLORSPACE_SRGB,107.priv = 0}108};109110/*111* Send a command to the camera.112*/113static int sq905_command(struct gspca_dev *gspca_dev, u16 index)114{115int ret;116117gspca_dev->usb_buf[0] = '\0';118ret = usb_control_msg(gspca_dev->dev,119usb_sndctrlpipe(gspca_dev->dev, 0),120USB_REQ_SYNCH_FRAME, /* request */121USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,122SQ905_COMMAND, index, gspca_dev->usb_buf, 1,123SQ905_CMD_TIMEOUT);124if (ret < 0) {125err("%s: usb_control_msg failed (%d)",126__func__, ret);127return ret;128}129130ret = usb_control_msg(gspca_dev->dev,131usb_sndctrlpipe(gspca_dev->dev, 0),132USB_REQ_SYNCH_FRAME, /* request */133USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,134SQ905_PING, 0, gspca_dev->usb_buf, 1,135SQ905_CMD_TIMEOUT);136if (ret < 0) {137err("%s: usb_control_msg failed 2 (%d)",138__func__, ret);139return ret;140}141142return 0;143}144145/*146* Acknowledge the end of a frame - see warning on sq905_command.147*/148static int sq905_ack_frame(struct gspca_dev *gspca_dev)149{150int ret;151152gspca_dev->usb_buf[0] = '\0';153ret = usb_control_msg(gspca_dev->dev,154usb_sndctrlpipe(gspca_dev->dev, 0),155USB_REQ_SYNCH_FRAME, /* request */156USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,157SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,158SQ905_CMD_TIMEOUT);159if (ret < 0) {160err("%s: usb_control_msg failed (%d)", __func__, ret);161return ret;162}163164return 0;165}166167/*168* request and read a block of data - see warning on sq905_command.169*/170static int171sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)172{173int ret;174int act_len;175176gspca_dev->usb_buf[0] = '\0';177if (need_lock)178mutex_lock(&gspca_dev->usb_lock);179ret = usb_control_msg(gspca_dev->dev,180usb_sndctrlpipe(gspca_dev->dev, 0),181USB_REQ_SYNCH_FRAME, /* request */182USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,183SQ905_BULK_READ, size, gspca_dev->usb_buf,1841, SQ905_CMD_TIMEOUT);185if (need_lock)186mutex_unlock(&gspca_dev->usb_lock);187if (ret < 0) {188err("%s: usb_control_msg failed (%d)", __func__, ret);189return ret;190}191ret = usb_bulk_msg(gspca_dev->dev,192usb_rcvbulkpipe(gspca_dev->dev, 0x81),193data, size, &act_len, SQ905_DATA_TIMEOUT);194195/* successful, it returns 0, otherwise negative */196if (ret < 0 || act_len != size) {197err("bulk read fail (%d) len %d/%d",198ret, act_len, size);199return -EIO;200}201return 0;202}203204/* This function is called as a workqueue function and runs whenever the camera205* is streaming data. Because it is a workqueue function it is allowed to sleep206* so we can use synchronous USB calls. To avoid possible collisions with other207* threads attempting to use the camera's USB interface we take the gspca208* usb_lock when performing USB operations. In practice the only thing we need209* to protect against is the usb_set_interface call that gspca makes during210* stream_off as the camera doesn't provide any controls that the user could try211* to change.212*/213static void sq905_dostream(struct work_struct *work)214{215struct sd *dev = container_of(work, struct sd, work_struct);216struct gspca_dev *gspca_dev = &dev->gspca_dev;217int bytes_left; /* bytes remaining in current frame. */218int data_len; /* size to use for the next read. */219int header_read; /* true if we have already read the frame header. */220int packet_type;221int frame_sz;222int ret;223u8 *data;224u8 *buffer;225226buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);227if (!buffer) {228err("Couldn't allocate USB buffer");229goto quit_stream;230}231232frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage233+ FRAME_HEADER_LEN;234235while (gspca_dev->present && gspca_dev->streaming) {236/* request some data and then read it until we have237* a complete frame. */238bytes_left = frame_sz;239header_read = 0;240241/* Note we do not check for gspca_dev->streaming here, as242we must finish reading an entire frame, otherwise the243next time we stream we start reading in the middle of a244frame. */245while (bytes_left > 0 && gspca_dev->present) {246data_len = bytes_left > SQ905_MAX_TRANSFER ?247SQ905_MAX_TRANSFER : bytes_left;248ret = sq905_read_data(gspca_dev, buffer, data_len, 1);249if (ret < 0)250goto quit_stream;251PDEBUG(D_PACK,252"Got %d bytes out of %d for frame",253data_len, bytes_left);254bytes_left -= data_len;255data = buffer;256if (!header_read) {257packet_type = FIRST_PACKET;258/* The first 64 bytes of each frame are259* a header full of FF 00 bytes */260data += FRAME_HEADER_LEN;261data_len -= FRAME_HEADER_LEN;262header_read = 1;263} else if (bytes_left == 0) {264packet_type = LAST_PACKET;265} else {266packet_type = INTER_PACKET;267}268gspca_frame_add(gspca_dev, packet_type,269data, data_len);270/* If entire frame fits in one packet we still271need to add a LAST_PACKET */272if (packet_type == FIRST_PACKET &&273bytes_left == 0)274gspca_frame_add(gspca_dev, LAST_PACKET,275NULL, 0);276}277if (gspca_dev->present) {278/* acknowledge the frame */279mutex_lock(&gspca_dev->usb_lock);280ret = sq905_ack_frame(gspca_dev);281mutex_unlock(&gspca_dev->usb_lock);282if (ret < 0)283goto quit_stream;284}285}286quit_stream:287if (gspca_dev->present) {288mutex_lock(&gspca_dev->usb_lock);289sq905_command(gspca_dev, SQ905_CLEAR);290mutex_unlock(&gspca_dev->usb_lock);291}292kfree(buffer);293}294295/* This function is called at probe time just before sd_init */296static int sd_config(struct gspca_dev *gspca_dev,297const struct usb_device_id *id)298{299struct cam *cam = &gspca_dev->cam;300struct sd *dev = (struct sd *) gspca_dev;301302/* We don't use the buffer gspca allocates so make it small. */303cam->bulk = 1;304cam->bulk_size = 64;305306INIT_WORK(&dev->work_struct, sq905_dostream);307308return 0;309}310311/* called on streamoff with alt==0 and on disconnect */312/* the usb_lock is held at entry - restore on exit */313static void sd_stop0(struct gspca_dev *gspca_dev)314{315struct sd *dev = (struct sd *) gspca_dev;316317/* wait for the work queue to terminate */318mutex_unlock(&gspca_dev->usb_lock);319/* This waits for sq905_dostream to finish */320destroy_workqueue(dev->work_thread);321dev->work_thread = NULL;322mutex_lock(&gspca_dev->usb_lock);323}324325/* this function is called at probe and resume time */326static int sd_init(struct gspca_dev *gspca_dev)327{328u32 ident;329int ret;330331/* connect to the camera and read332* the model ID and process that and put it away.333*/334ret = sq905_command(gspca_dev, SQ905_CLEAR);335if (ret < 0)336return ret;337ret = sq905_command(gspca_dev, SQ905_ID);338if (ret < 0)339return ret;340ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);341if (ret < 0)342return ret;343/* usb_buf is allocated with kmalloc so is aligned.344* Camera model number is the right way round if we assume this345* reverse engineered ID is supposed to be big endian. */346ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);347ret = sq905_command(gspca_dev, SQ905_CLEAR);348if (ret < 0)349return ret;350PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);351gspca_dev->cam.cam_mode = sq905_mode;352gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);353if (!(ident & SQ905_HIRES_MASK))354gspca_dev->cam.nmodes--;355356if (ident & SQ905_ORIENTATION_MASK)357gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;358else359gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |360V4L2_IN_ST_HFLIP;361return 0;362}363364/* Set up for getting frames. */365static int sd_start(struct gspca_dev *gspca_dev)366{367struct sd *dev = (struct sd *) gspca_dev;368int ret;369370/* "Open the shutter" and set size, to start capture */371switch (gspca_dev->curr_mode) {372default:373/* case 2: */374PDEBUG(D_STREAM, "Start streaming at high resolution");375ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);376break;377case 1:378PDEBUG(D_STREAM, "Start streaming at medium resolution");379ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);380break;381case 0:382PDEBUG(D_STREAM, "Start streaming at low resolution");383ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);384}385386if (ret < 0) {387PDEBUG(D_ERR, "Start streaming command failed");388return ret;389}390/* Start the workqueue function to do the streaming */391dev->work_thread = create_singlethread_workqueue(MODULE_NAME);392queue_work(dev->work_thread, &dev->work_struct);393394return 0;395}396397/* Table of supported USB devices */398static const struct usb_device_id device_table[] = {399{USB_DEVICE(0x2770, 0x9120)},400{}401};402403MODULE_DEVICE_TABLE(usb, device_table);404405/* sub-driver description */406static const struct sd_desc sd_desc = {407.name = MODULE_NAME,408.config = sd_config,409.init = sd_init,410.start = sd_start,411.stop0 = sd_stop0,412};413414/* -- device connect -- */415static int sd_probe(struct usb_interface *intf,416const struct usb_device_id *id)417{418return gspca_dev_probe(intf, id,419&sd_desc,420sizeof(struct sd),421THIS_MODULE);422}423424static struct usb_driver sd_driver = {425.name = MODULE_NAME,426.id_table = device_table,427.probe = sd_probe,428.disconnect = gspca_disconnect,429#ifdef CONFIG_PM430.suspend = gspca_suspend,431.resume = gspca_resume,432#endif433};434435/* -- module insert / remove -- */436static int __init sd_mod_init(void)437{438return usb_register(&sd_driver);439}440441static void __exit sd_mod_exit(void)442{443usb_deregister(&sd_driver);444}445446module_init(sd_mod_init);447module_exit(sd_mod_exit);448449450