Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * SQ905 subdriver
0004  *
0005  * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore
0006  */
0007 
0008 /*
0009  * History and Acknowledgments
0010  *
0011  * The original Linux driver for SQ905 based cameras was written by
0012  * Marcell Lengyel and further developed by many other contributors
0013  * and is available from http://sourceforge.net/projects/sqcam/
0014  *
0015  * This driver takes advantage of the reverse engineering work done for
0016  * that driver and for libgphoto2 but shares no code with them.
0017  *
0018  * This driver has used as a base the finepix driver and other gspca
0019  * based drivers and may still contain code fragments taken from those
0020  * drivers.
0021  */
0022 
0023 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0024 
0025 #define MODULE_NAME "sq905"
0026 
0027 #include <linux/workqueue.h>
0028 #include <linux/slab.h>
0029 #include "gspca.h"
0030 
0031 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, Theodore Kilgore <kilgota@auburn.edu>");
0032 MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
0033 MODULE_LICENSE("GPL");
0034 
0035 /* Default timeouts, in ms */
0036 #define SQ905_CMD_TIMEOUT 500
0037 #define SQ905_DATA_TIMEOUT 1000
0038 
0039 /* Maximum transfer size to use. */
0040 #define SQ905_MAX_TRANSFER 0x8000
0041 #define FRAME_HEADER_LEN 64
0042 
0043 /* The known modes, or registers. These go in the "value" slot. */
0044 
0045 /* 00 is "none" obviously */
0046 
0047 #define SQ905_BULK_READ 0x03    /* precedes any bulk read */
0048 #define SQ905_COMMAND   0x06    /* precedes the command codes below */
0049 #define SQ905_PING  0x07    /* when reading an "idling" command */
0050 #define SQ905_READ_DONE 0xc0    /* ack bulk read completed */
0051 
0052 /* Any non-zero value in the bottom 2 bits of the 2nd byte of
0053  * the ID appears to indicate the camera can do 640*480. If the
0054  * LSB of that byte is set the image is just upside down, otherwise
0055  * it is rotated 180 degrees. */
0056 #define SQ905_HIRES_MASK    0x00000300
0057 #define SQ905_ORIENTATION_MASK  0x00000100
0058 
0059 /* Some command codes. These go in the "index" slot. */
0060 
0061 #define SQ905_ID      0xf0  /* asks for model string */
0062 #define SQ905_CONFIG  0x20  /* gets photo alloc. table, not used here */
0063 #define SQ905_DATA    0x30  /* accesses photo data, not used here */
0064 #define SQ905_CLEAR   0xa0  /* clear everything */
0065 #define SQ905_CAPTURE_LOW  0x60 /* Starts capture at 160x120 */
0066 #define SQ905_CAPTURE_MED  0x61 /* Starts capture at 320x240 */
0067 #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
0068 /* note that the capture command also controls the output dimensions */
0069 
0070 /* Structure to hold all of our device specific stuff */
0071 struct sd {
0072     struct gspca_dev gspca_dev; /* !! must be the first item */
0073 
0074     /*
0075      * Driver stuff
0076      */
0077     struct work_struct work_struct;
0078     struct workqueue_struct *work_thread;
0079 };
0080 
0081 static struct v4l2_pix_format sq905_mode[] = {
0082     { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0083         .bytesperline = 160,
0084         .sizeimage = 160 * 120,
0085         .colorspace = V4L2_COLORSPACE_SRGB,
0086         .priv = 0},
0087     { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0088         .bytesperline = 320,
0089         .sizeimage = 320 * 240,
0090         .colorspace = V4L2_COLORSPACE_SRGB,
0091         .priv = 0},
0092     { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0093         .bytesperline = 640,
0094         .sizeimage = 640 * 480,
0095         .colorspace = V4L2_COLORSPACE_SRGB,
0096         .priv = 0}
0097 };
0098 
0099 /*
0100  * Send a command to the camera.
0101  */
0102 static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
0103 {
0104     int ret;
0105 
0106     gspca_dev->usb_buf[0] = '\0';
0107     ret = usb_control_msg(gspca_dev->dev,
0108                   usb_sndctrlpipe(gspca_dev->dev, 0),
0109                   USB_REQ_SYNCH_FRAME,                /* request */
0110                   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0111                   SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
0112                   SQ905_CMD_TIMEOUT);
0113     if (ret < 0) {
0114         pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
0115         return ret;
0116     }
0117 
0118     ret = usb_control_msg(gspca_dev->dev,
0119                   usb_rcvctrlpipe(gspca_dev->dev, 0),
0120                   USB_REQ_SYNCH_FRAME,                /* request */
0121                   USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0122                   SQ905_PING, 0, gspca_dev->usb_buf, 1,
0123                   SQ905_CMD_TIMEOUT);
0124     if (ret < 0) {
0125         pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret);
0126         return ret;
0127     }
0128 
0129     return 0;
0130 }
0131 
0132 /*
0133  * Acknowledge the end of a frame - see warning on sq905_command.
0134  */
0135 static int sq905_ack_frame(struct gspca_dev *gspca_dev)
0136 {
0137     int ret;
0138 
0139     gspca_dev->usb_buf[0] = '\0';
0140     ret = usb_control_msg(gspca_dev->dev,
0141                   usb_sndctrlpipe(gspca_dev->dev, 0),
0142                   USB_REQ_SYNCH_FRAME,                /* request */
0143                   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0144                   SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
0145                   SQ905_CMD_TIMEOUT);
0146     if (ret < 0) {
0147         pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
0148         return ret;
0149     }
0150 
0151     return 0;
0152 }
0153 
0154 /*
0155  *  request and read a block of data - see warning on sq905_command.
0156  */
0157 static int
0158 sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
0159 {
0160     int ret;
0161     int act_len = 0;
0162 
0163     gspca_dev->usb_buf[0] = '\0';
0164     if (need_lock)
0165         mutex_lock(&gspca_dev->usb_lock);
0166     ret = usb_control_msg(gspca_dev->dev,
0167                   usb_sndctrlpipe(gspca_dev->dev, 0),
0168                   USB_REQ_SYNCH_FRAME,                /* request */
0169                   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0170                   SQ905_BULK_READ, size, gspca_dev->usb_buf,
0171                   1, SQ905_CMD_TIMEOUT);
0172     if (need_lock)
0173         mutex_unlock(&gspca_dev->usb_lock);
0174     if (ret < 0) {
0175         pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
0176         return ret;
0177     }
0178     ret = usb_bulk_msg(gspca_dev->dev,
0179                usb_rcvbulkpipe(gspca_dev->dev, 0x81),
0180                data, size, &act_len, SQ905_DATA_TIMEOUT);
0181 
0182     /* successful, it returns 0, otherwise  negative */
0183     if (ret < 0 || act_len != size) {
0184         pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size);
0185         return -EIO;
0186     }
0187     return 0;
0188 }
0189 
0190 /*
0191  * This function is called as a workqueue function and runs whenever the camera
0192  * is streaming data. Because it is a workqueue function it is allowed to sleep
0193  * so we can use synchronous USB calls. To avoid possible collisions with other
0194  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
0195  * performing USB operations using it. In practice we don't really need this
0196  * as the camera doesn't provide any controls.
0197  */
0198 static void sq905_dostream(struct work_struct *work)
0199 {
0200     struct sd *dev = container_of(work, struct sd, work_struct);
0201     struct gspca_dev *gspca_dev = &dev->gspca_dev;
0202     int bytes_left; /* bytes remaining in current frame. */
0203     int data_len;   /* size to use for the next read. */
0204     int header_read; /* true if we have already read the frame header. */
0205     int packet_type;
0206     int frame_sz;
0207     int ret;
0208     u8 *data;
0209     u8 *buffer;
0210 
0211     buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL);
0212     if (!buffer) {
0213         pr_err("Couldn't allocate USB buffer\n");
0214         goto quit_stream;
0215     }
0216 
0217     frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
0218             + FRAME_HEADER_LEN;
0219 
0220     while (gspca_dev->present && gspca_dev->streaming) {
0221 #ifdef CONFIG_PM
0222         if (gspca_dev->frozen)
0223             break;
0224 #endif
0225         /* request some data and then read it until we have
0226          * a complete frame. */
0227         bytes_left = frame_sz;
0228         header_read = 0;
0229 
0230         /* Note we do not check for gspca_dev->streaming here, as
0231            we must finish reading an entire frame, otherwise the
0232            next time we stream we start reading in the middle of a
0233            frame. */
0234         while (bytes_left > 0 && gspca_dev->present) {
0235             data_len = bytes_left > SQ905_MAX_TRANSFER ?
0236                 SQ905_MAX_TRANSFER : bytes_left;
0237             ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
0238             if (ret < 0)
0239                 goto quit_stream;
0240             gspca_dbg(gspca_dev, D_PACK,
0241                   "Got %d bytes out of %d for frame\n",
0242                   data_len, bytes_left);
0243             bytes_left -= data_len;
0244             data = buffer;
0245             if (!header_read) {
0246                 packet_type = FIRST_PACKET;
0247                 /* The first 64 bytes of each frame are
0248                  * a header full of FF 00 bytes */
0249                 data += FRAME_HEADER_LEN;
0250                 data_len -= FRAME_HEADER_LEN;
0251                 header_read = 1;
0252             } else if (bytes_left == 0) {
0253                 packet_type = LAST_PACKET;
0254             } else {
0255                 packet_type = INTER_PACKET;
0256             }
0257             gspca_frame_add(gspca_dev, packet_type,
0258                     data, data_len);
0259             /* If entire frame fits in one packet we still
0260                need to add a LAST_PACKET */
0261             if (packet_type == FIRST_PACKET &&
0262                 bytes_left == 0)
0263                 gspca_frame_add(gspca_dev, LAST_PACKET,
0264                         NULL, 0);
0265         }
0266         if (gspca_dev->present) {
0267             /* acknowledge the frame */
0268             mutex_lock(&gspca_dev->usb_lock);
0269             ret = sq905_ack_frame(gspca_dev);
0270             mutex_unlock(&gspca_dev->usb_lock);
0271             if (ret < 0)
0272                 goto quit_stream;
0273         }
0274     }
0275 quit_stream:
0276     if (gspca_dev->present) {
0277         mutex_lock(&gspca_dev->usb_lock);
0278         sq905_command(gspca_dev, SQ905_CLEAR);
0279         mutex_unlock(&gspca_dev->usb_lock);
0280     }
0281     kfree(buffer);
0282 }
0283 
0284 /* This function is called at probe time just before sd_init */
0285 static int sd_config(struct gspca_dev *gspca_dev,
0286         const struct usb_device_id *id)
0287 {
0288     struct cam *cam = &gspca_dev->cam;
0289     struct sd *dev = (struct sd *) gspca_dev;
0290 
0291     /* We don't use the buffer gspca allocates so make it small. */
0292     cam->bulk = 1;
0293     cam->bulk_size = 64;
0294 
0295     INIT_WORK(&dev->work_struct, sq905_dostream);
0296 
0297     return 0;
0298 }
0299 
0300 /* called on streamoff with alt==0 and on disconnect */
0301 /* the usb_lock is held at entry - restore on exit */
0302 static void sd_stop0(struct gspca_dev *gspca_dev)
0303 {
0304     struct sd *dev = (struct sd *) gspca_dev;
0305 
0306     /* wait for the work queue to terminate */
0307     mutex_unlock(&gspca_dev->usb_lock);
0308     /* This waits for sq905_dostream to finish */
0309     destroy_workqueue(dev->work_thread);
0310     dev->work_thread = NULL;
0311     mutex_lock(&gspca_dev->usb_lock);
0312 }
0313 
0314 /* this function is called at probe and resume time */
0315 static int sd_init(struct gspca_dev *gspca_dev)
0316 {
0317     u32 ident;
0318     int ret;
0319 
0320     /* connect to the camera and read
0321      * the model ID and process that and put it away.
0322      */
0323     ret = sq905_command(gspca_dev, SQ905_CLEAR);
0324     if (ret < 0)
0325         return ret;
0326     ret = sq905_command(gspca_dev, SQ905_ID);
0327     if (ret < 0)
0328         return ret;
0329     ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
0330     if (ret < 0)
0331         return ret;
0332     /* usb_buf is allocated with kmalloc so is aligned.
0333      * Camera model number is the right way round if we assume this
0334      * reverse engineered ID is supposed to be big endian. */
0335     ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
0336     ret = sq905_command(gspca_dev, SQ905_CLEAR);
0337     if (ret < 0)
0338         return ret;
0339     gspca_dbg(gspca_dev, D_CONF, "SQ905 camera ID %08x detected\n", ident);
0340     gspca_dev->cam.cam_mode = sq905_mode;
0341     gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
0342     if (!(ident & SQ905_HIRES_MASK))
0343         gspca_dev->cam.nmodes--;
0344 
0345     if (ident & SQ905_ORIENTATION_MASK)
0346         gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
0347     else
0348         gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
0349                          V4L2_IN_ST_HFLIP;
0350     return 0;
0351 }
0352 
0353 /* Set up for getting frames. */
0354 static int sd_start(struct gspca_dev *gspca_dev)
0355 {
0356     struct sd *dev = (struct sd *) gspca_dev;
0357     int ret;
0358 
0359     /* "Open the shutter" and set size, to start capture */
0360     switch (gspca_dev->curr_mode) {
0361     default:
0362 /*  case 2: */
0363         gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
0364         ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
0365         break;
0366     case 1:
0367         gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
0368         ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
0369         break;
0370     case 0:
0371         gspca_dbg(gspca_dev, D_STREAM, "Start streaming at low resolution\n");
0372         ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
0373     }
0374 
0375     if (ret < 0) {
0376         gspca_err(gspca_dev, "Start streaming command failed\n");
0377         return ret;
0378     }
0379     /* Start the workqueue function to do the streaming */
0380     dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
0381     if (!dev->work_thread)
0382         return -ENOMEM;
0383 
0384     queue_work(dev->work_thread, &dev->work_struct);
0385 
0386     return 0;
0387 }
0388 
0389 /* Table of supported USB devices */
0390 static const struct usb_device_id device_table[] = {
0391     {USB_DEVICE(0x2770, 0x9120)},
0392     {}
0393 };
0394 
0395 MODULE_DEVICE_TABLE(usb, device_table);
0396 
0397 /* sub-driver description */
0398 static const struct sd_desc sd_desc = {
0399     .name   = MODULE_NAME,
0400     .config = sd_config,
0401     .init   = sd_init,
0402     .start  = sd_start,
0403     .stop0  = sd_stop0,
0404 };
0405 
0406 /* -- device connect -- */
0407 static int sd_probe(struct usb_interface *intf,
0408         const struct usb_device_id *id)
0409 {
0410     return gspca_dev_probe(intf, id,
0411             &sd_desc,
0412             sizeof(struct sd),
0413             THIS_MODULE);
0414 }
0415 
0416 static struct usb_driver sd_driver = {
0417     .name       = MODULE_NAME,
0418     .id_table   = device_table,
0419     .probe      = sd_probe,
0420     .disconnect = gspca_disconnect,
0421 #ifdef CONFIG_PM
0422     .suspend = gspca_suspend,
0423     .resume  = gspca_resume,
0424     .reset_resume = gspca_resume,
0425 #endif
0426 };
0427 
0428 module_usb_driver(sd_driver);