Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * SQ905C subdriver
0004  *
0005  * Copyright (C) 2009 Theodore Kilgore
0006  */
0007 
0008 /*
0009  *
0010  * This driver uses work done in
0011  * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
0012  *
0013  * This driver has also used as a base the sq905c driver
0014  * and may contain code fragments from it.
0015  */
0016 
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018 
0019 #define MODULE_NAME "sq905c"
0020 
0021 #include <linux/workqueue.h>
0022 #include <linux/slab.h>
0023 #include "gspca.h"
0024 
0025 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
0026 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
0027 MODULE_LICENSE("GPL");
0028 
0029 /* Default timeouts, in ms */
0030 #define SQ905C_CMD_TIMEOUT 500
0031 #define SQ905C_DATA_TIMEOUT 1000
0032 
0033 /* Maximum transfer size to use. */
0034 #define SQ905C_MAX_TRANSFER 0x8000
0035 
0036 #define FRAME_HEADER_LEN 0x50
0037 
0038 /* Commands. These go in the "value" slot. */
0039 #define SQ905C_CLEAR   0xa0     /* clear everything */
0040 #define SQ905C_GET_ID  0x14f4       /* Read version number */
0041 #define SQ905C_CAPTURE_LOW 0xa040   /* Starts capture at 160x120 */
0042 #define SQ905C_CAPTURE_MED 0x1440   /* Starts capture at 320x240 */
0043 #define SQ905C_CAPTURE_HI 0x2840    /* Starts capture at 320x240 */
0044 
0045 /* For capture, this must go in the "index" slot. */
0046 #define SQ905C_CAPTURE_INDEX 0x110f
0047 
0048 /* Structure to hold all of our device specific stuff */
0049 struct sd {
0050     struct gspca_dev gspca_dev; /* !! must be the first item */
0051     const struct v4l2_pix_format *cap_mode;
0052     /* Driver stuff */
0053     struct work_struct work_struct;
0054     struct workqueue_struct *work_thread;
0055 };
0056 
0057 /*
0058  * Most of these cameras will do 640x480 and 320x240. 160x120 works
0059  * in theory but gives very poor output. Therefore, not supported.
0060  * The 0x2770:0x9050 cameras have max resolution of 320x240.
0061  */
0062 static struct v4l2_pix_format sq905c_mode[] = {
0063     { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
0064         .bytesperline = 320,
0065         .sizeimage = 320 * 240,
0066         .colorspace = V4L2_COLORSPACE_SRGB,
0067         .priv = 0},
0068     { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
0069         .bytesperline = 640,
0070         .sizeimage = 640 * 480,
0071         .colorspace = V4L2_COLORSPACE_SRGB,
0072         .priv = 0}
0073 };
0074 
0075 /* Send a command to the camera. */
0076 static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
0077 {
0078     int ret;
0079 
0080     ret = usb_control_msg(gspca_dev->dev,
0081                   usb_sndctrlpipe(gspca_dev->dev, 0),
0082                   USB_REQ_SYNCH_FRAME,                /* request */
0083                   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0084                   command, index, NULL, 0,
0085                   SQ905C_CMD_TIMEOUT);
0086     if (ret < 0) {
0087         pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
0088         return ret;
0089     }
0090 
0091     return 0;
0092 }
0093 
0094 static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
0095                int size)
0096 {
0097     int ret;
0098 
0099     ret = usb_control_msg(gspca_dev->dev,
0100                   usb_rcvctrlpipe(gspca_dev->dev, 0),
0101                   USB_REQ_SYNCH_FRAME,      /* request */
0102                   USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0103                   command, index, gspca_dev->usb_buf, size,
0104                   SQ905C_CMD_TIMEOUT);
0105     if (ret < 0) {
0106         pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
0107         return ret;
0108     }
0109 
0110     return 0;
0111 }
0112 
0113 /*
0114  * This function is called as a workqueue function and runs whenever the camera
0115  * is streaming data. Because it is a workqueue function it is allowed to sleep
0116  * so we can use synchronous USB calls. To avoid possible collisions with other
0117  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
0118  * performing USB operations using it. In practice we don't really need this
0119  * as the camera doesn't provide any controls.
0120  */
0121 static void sq905c_dostream(struct work_struct *work)
0122 {
0123     struct sd *dev = container_of(work, struct sd, work_struct);
0124     struct gspca_dev *gspca_dev = &dev->gspca_dev;
0125     int bytes_left; /* bytes remaining in current frame. */
0126     int data_len;   /* size to use for the next read. */
0127     int act_len;
0128     int packet_type;
0129     int ret;
0130     u8 *buffer;
0131 
0132     buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL);
0133     if (!buffer) {
0134         pr_err("Couldn't allocate USB buffer\n");
0135         goto quit_stream;
0136     }
0137 
0138     while (gspca_dev->present && gspca_dev->streaming) {
0139 #ifdef CONFIG_PM
0140         if (gspca_dev->frozen)
0141             break;
0142 #endif
0143         /* Request the header, which tells the size to download */
0144         ret = usb_bulk_msg(gspca_dev->dev,
0145                 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
0146                 buffer, FRAME_HEADER_LEN, &act_len,
0147                 SQ905C_DATA_TIMEOUT);
0148         gspca_dbg(gspca_dev, D_STREAM,
0149               "Got %d bytes out of %d for header\n",
0150               act_len, FRAME_HEADER_LEN);
0151         if (ret < 0 || act_len < FRAME_HEADER_LEN)
0152             goto quit_stream;
0153         /* size is read from 4 bytes starting 0x40, little endian */
0154         bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
0155                     |(buffer[0x43]<<24);
0156         gspca_dbg(gspca_dev, D_STREAM, "bytes_left = 0x%x\n",
0157               bytes_left);
0158         /* We keep the header. It has other information, too. */
0159         packet_type = FIRST_PACKET;
0160         gspca_frame_add(gspca_dev, packet_type,
0161                 buffer, FRAME_HEADER_LEN);
0162         while (bytes_left > 0 && gspca_dev->present) {
0163             data_len = bytes_left > SQ905C_MAX_TRANSFER ?
0164                 SQ905C_MAX_TRANSFER : bytes_left;
0165             ret = usb_bulk_msg(gspca_dev->dev,
0166                 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
0167                 buffer, data_len, &act_len,
0168                 SQ905C_DATA_TIMEOUT);
0169             if (ret < 0 || act_len < data_len)
0170                 goto quit_stream;
0171             gspca_dbg(gspca_dev, D_STREAM,
0172                   "Got %d bytes out of %d for frame\n",
0173                   data_len, bytes_left);
0174             bytes_left -= data_len;
0175             if (bytes_left == 0)
0176                 packet_type = LAST_PACKET;
0177             else
0178                 packet_type = INTER_PACKET;
0179             gspca_frame_add(gspca_dev, packet_type,
0180                     buffer, data_len);
0181         }
0182     }
0183 quit_stream:
0184     if (gspca_dev->present) {
0185         mutex_lock(&gspca_dev->usb_lock);
0186         sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
0187         mutex_unlock(&gspca_dev->usb_lock);
0188     }
0189     kfree(buffer);
0190 }
0191 
0192 /* This function is called at probe time just before sd_init */
0193 static int sd_config(struct gspca_dev *gspca_dev,
0194         const struct usb_device_id *id)
0195 {
0196     struct cam *cam = &gspca_dev->cam;
0197     struct sd *dev = (struct sd *) gspca_dev;
0198     int ret;
0199 
0200     gspca_dbg(gspca_dev, D_PROBE,
0201           "SQ9050 camera detected (vid/pid 0x%04X:0x%04X)\n",
0202           id->idVendor, id->idProduct);
0203 
0204     ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
0205     if (ret < 0) {
0206         gspca_err(gspca_dev, "Get version command failed\n");
0207         return ret;
0208     }
0209 
0210     ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
0211     if (ret < 0) {
0212         gspca_err(gspca_dev, "Reading version command failed\n");
0213         return ret;
0214     }
0215     /* Note we leave out the usb id and the manufacturing date */
0216     gspca_dbg(gspca_dev, D_PROBE,
0217           "SQ9050 ID string: %02x - %*ph\n",
0218           gspca_dev->usb_buf[3], 6, gspca_dev->usb_buf + 14);
0219 
0220     cam->cam_mode = sq905c_mode;
0221     cam->nmodes = 2;
0222     if (gspca_dev->usb_buf[15] == 0)
0223         cam->nmodes = 1;
0224     /* We don't use the buffer gspca allocates so make it small. */
0225     cam->bulk_size = 32;
0226     cam->bulk = 1;
0227     INIT_WORK(&dev->work_struct, sq905c_dostream);
0228     return 0;
0229 }
0230 
0231 /* called on streamoff with alt==0 and on disconnect */
0232 /* the usb_lock is held at entry - restore on exit */
0233 static void sd_stop0(struct gspca_dev *gspca_dev)
0234 {
0235     struct sd *dev = (struct sd *) gspca_dev;
0236 
0237     /* wait for the work queue to terminate */
0238     mutex_unlock(&gspca_dev->usb_lock);
0239     /* This waits for sq905c_dostream to finish */
0240     destroy_workqueue(dev->work_thread);
0241     dev->work_thread = NULL;
0242     mutex_lock(&gspca_dev->usb_lock);
0243 }
0244 
0245 /* this function is called at probe and resume time */
0246 static int sd_init(struct gspca_dev *gspca_dev)
0247 {
0248     /* connect to the camera and reset it. */
0249     return sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
0250 }
0251 
0252 /* Set up for getting frames. */
0253 static int sd_start(struct gspca_dev *gspca_dev)
0254 {
0255     struct sd *dev = (struct sd *) gspca_dev;
0256     int ret;
0257 
0258     dev->cap_mode = gspca_dev->cam.cam_mode;
0259     /* "Open the shutter" and set size, to start capture */
0260     switch (gspca_dev->pixfmt.width) {
0261     case 640:
0262         gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
0263         dev->cap_mode++;
0264         ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
0265                         SQ905C_CAPTURE_INDEX);
0266         break;
0267     default: /* 320 */
0268         gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
0269         ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
0270                         SQ905C_CAPTURE_INDEX);
0271     }
0272 
0273     if (ret < 0) {
0274         gspca_err(gspca_dev, "Start streaming command failed\n");
0275         return ret;
0276     }
0277     /* Start the workqueue function to do the streaming */
0278     dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
0279     if (!dev->work_thread)
0280         return -ENOMEM;
0281 
0282     queue_work(dev->work_thread, &dev->work_struct);
0283 
0284     return 0;
0285 }
0286 
0287 /* Table of supported USB devices */
0288 static const struct usb_device_id device_table[] = {
0289     {USB_DEVICE(0x2770, 0x905c)},
0290     {USB_DEVICE(0x2770, 0x9050)},
0291     {USB_DEVICE(0x2770, 0x9051)},
0292     {USB_DEVICE(0x2770, 0x9052)},
0293     {USB_DEVICE(0x2770, 0x913d)},
0294     {}
0295 };
0296 
0297 MODULE_DEVICE_TABLE(usb, device_table);
0298 
0299 /* sub-driver description */
0300 static const struct sd_desc sd_desc = {
0301     .name   = MODULE_NAME,
0302     .config = sd_config,
0303     .init   = sd_init,
0304     .start  = sd_start,
0305     .stop0  = sd_stop0,
0306 };
0307 
0308 /* -- device connect -- */
0309 static int sd_probe(struct usb_interface *intf,
0310         const struct usb_device_id *id)
0311 {
0312     return gspca_dev_probe(intf, id,
0313             &sd_desc,
0314             sizeof(struct sd),
0315             THIS_MODULE);
0316 }
0317 
0318 static struct usb_driver sd_driver = {
0319     .name       = MODULE_NAME,
0320     .id_table   = device_table,
0321     .probe      = sd_probe,
0322     .disconnect = gspca_disconnect,
0323 #ifdef CONFIG_PM
0324     .suspend = gspca_suspend,
0325     .resume  = gspca_resume,
0326     .reset_resume = gspca_resume,
0327 #endif
0328 };
0329 
0330 module_usb_driver(sd_driver);