^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SQ905 subdriver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * History and Acknowledgments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * The original Linux driver for SQ905 based cameras was written by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Marcell Lengyel and further developed by many other contributors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * and is available from http://sourceforge.net/projects/sqcam/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * This driver takes advantage of the reverse engineering work done for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * that driver and for libgphoto2 but shares no code with them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * This driver has used as a base the finepix driver and other gspca
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * based drivers and may still contain code fragments taken from those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MODULE_NAME "sq905"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "gspca.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, Theodore Kilgore <kilgota@auburn.edu>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* Default timeouts, in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define SQ905_CMD_TIMEOUT 500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define SQ905_DATA_TIMEOUT 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Maximum transfer size to use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define SQ905_MAX_TRANSFER 0x8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define FRAME_HEADER_LEN 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* The known modes, or registers. These go in the "value" slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* 00 is "none" obviously */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define SQ905_BULK_READ 0x03 /* precedes any bulk read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define SQ905_COMMAND 0x06 /* precedes the command codes below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define SQ905_PING 0x07 /* when reading an "idling" command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define SQ905_READ_DONE 0xc0 /* ack bulk read completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Any non-zero value in the bottom 2 bits of the 2nd byte of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * the ID appears to indicate the camera can do 640*480. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * LSB of that byte is set the image is just upside down, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * it is rotated 180 degrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define SQ905_HIRES_MASK 0x00000300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define SQ905_ORIENTATION_MASK 0x00000100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Some command codes. These go in the "index" slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define SQ905_ID 0xf0 /* asks for model string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define SQ905_DATA 0x30 /* accesses photo data, not used here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define SQ905_CLEAR 0xa0 /* clear everything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* note that the capture command also controls the output dimensions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Structure to hold all of our device specific stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct sd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct gspca_dev gspca_dev; /* !! must be the first item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Driver stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct work_struct work_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct workqueue_struct *work_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static struct v4l2_pix_format sq905_mode[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .bytesperline = 160,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .sizeimage = 160 * 120,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .colorspace = V4L2_COLORSPACE_SRGB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .priv = 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .bytesperline = 320,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .sizeimage = 320 * 240,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .colorspace = V4L2_COLORSPACE_SRGB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .priv = 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .bytesperline = 640,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .sizeimage = 640 * 480,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .colorspace = V4L2_COLORSPACE_SRGB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .priv = 0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Send a command to the camera.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) gspca_dev->usb_buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = usb_control_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) usb_sndctrlpipe(gspca_dev->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) USB_REQ_SYNCH_FRAME, /* request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) SQ905_CMD_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ret = usb_control_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) usb_rcvctrlpipe(gspca_dev->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) USB_REQ_SYNCH_FRAME, /* request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) SQ905_PING, 0, gspca_dev->usb_buf, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) SQ905_CMD_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * Acknowledge the end of a frame - see warning on sq905_command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int sq905_ack_frame(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) gspca_dev->usb_buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = usb_control_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) usb_sndctrlpipe(gspca_dev->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) USB_REQ_SYNCH_FRAME, /* request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) SQ905_CMD_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * request and read a block of data - see warning on sq905_command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int act_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) gspca_dev->usb_buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (need_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) mutex_lock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = usb_control_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) usb_sndctrlpipe(gspca_dev->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) USB_REQ_SYNCH_FRAME, /* request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) SQ905_BULK_READ, size, gspca_dev->usb_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 1, SQ905_CMD_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (need_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) mutex_unlock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ret = usb_bulk_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) usb_rcvbulkpipe(gspca_dev->dev, 0x81),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) data, size, &act_len, SQ905_DATA_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* successful, it returns 0, otherwise negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (ret < 0 || act_len != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * This function is called as a workqueue function and runs whenever the camera
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * is streaming data. Because it is a workqueue function it is allowed to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * so we can use synchronous USB calls. To avoid possible collisions with other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * performing USB operations using it. In practice we don't really need this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * as the camera doesn't provide any controls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static void sq905_dostream(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct sd *dev = container_of(work, struct sd, work_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct gspca_dev *gspca_dev = &dev->gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int bytes_left; /* bytes remaining in current frame. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int data_len; /* size to use for the next read. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int header_read; /* true if we have already read the frame header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int packet_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int frame_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) pr_err("Couldn't allocate USB buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) goto quit_stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) + FRAME_HEADER_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) while (gspca_dev->present && gspca_dev->streaming) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (gspca_dev->frozen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* request some data and then read it until we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * a complete frame. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) bytes_left = frame_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) header_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* Note we do not check for gspca_dev->streaming here, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) we must finish reading an entire frame, otherwise the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) next time we stream we start reading in the middle of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) frame. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) while (bytes_left > 0 && gspca_dev->present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) data_len = bytes_left > SQ905_MAX_TRANSFER ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) SQ905_MAX_TRANSFER : bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto quit_stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) gspca_dbg(gspca_dev, D_PACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) "Got %d bytes out of %d for frame\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) data_len, bytes_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) bytes_left -= data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) data = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!header_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) packet_type = FIRST_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* The first 64 bytes of each frame are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * a header full of FF 00 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) data += FRAME_HEADER_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) data_len -= FRAME_HEADER_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) header_read = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) } else if (bytes_left == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) packet_type = LAST_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) packet_type = INTER_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) gspca_frame_add(gspca_dev, packet_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* If entire frame fits in one packet we still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) need to add a LAST_PACKET */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (packet_type == FIRST_PACKET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) bytes_left == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) gspca_frame_add(gspca_dev, LAST_PACKET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (gspca_dev->present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* acknowledge the frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) mutex_lock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ret = sq905_ack_frame(gspca_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) mutex_unlock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto quit_stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) quit_stream:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (gspca_dev->present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) mutex_lock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) sq905_command(gspca_dev, SQ905_CLEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) mutex_unlock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* This function is called at probe time just before sd_init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int sd_config(struct gspca_dev *gspca_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct cam *cam = &gspca_dev->cam;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct sd *dev = (struct sd *) gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* We don't use the buffer gspca allocates so make it small. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) cam->bulk = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) cam->bulk_size = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) INIT_WORK(&dev->work_struct, sq905_dostream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /* called on streamoff with alt==0 and on disconnect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* the usb_lock is held at entry - restore on exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void sd_stop0(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct sd *dev = (struct sd *) gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* wait for the work queue to terminate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) mutex_unlock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* This waits for sq905_dostream to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) destroy_workqueue(dev->work_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) dev->work_thread = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) mutex_lock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* this function is called at probe and resume time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int sd_init(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) u32 ident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* connect to the camera and read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * the model ID and process that and put it away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ret = sq905_command(gspca_dev, SQ905_CLEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ret = sq905_command(gspca_dev, SQ905_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* usb_buf is allocated with kmalloc so is aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * Camera model number is the right way round if we assume this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * reverse engineered ID is supposed to be big endian. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ret = sq905_command(gspca_dev, SQ905_CLEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) gspca_dbg(gspca_dev, D_CONF, "SQ905 camera ID %08x detected\n", ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) gspca_dev->cam.cam_mode = sq905_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (!(ident & SQ905_HIRES_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) gspca_dev->cam.nmodes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (ident & SQ905_ORIENTATION_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) V4L2_IN_ST_HFLIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* Set up for getting frames. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static int sd_start(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct sd *dev = (struct sd *) gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* "Open the shutter" and set size, to start capture */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) switch (gspca_dev->curr_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* case 2: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) gspca_dbg(gspca_dev, D_STREAM, "Start streaming at low resolution\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) gspca_err(gspca_dev, "Start streaming command failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* Start the workqueue function to do the streaming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (!dev->work_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) queue_work(dev->work_thread, &dev->work_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* Table of supported USB devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static const struct usb_device_id device_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {USB_DEVICE(0x2770, 0x9120)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) MODULE_DEVICE_TABLE(usb, device_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* sub-driver description */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static const struct sd_desc sd_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .name = MODULE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .config = sd_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .init = sd_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .start = sd_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .stop0 = sd_stop0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /* -- device connect -- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static int sd_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return gspca_dev_probe(intf, id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) &sd_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) sizeof(struct sd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static struct usb_driver sd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .name = MODULE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .id_table = device_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .probe = sd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) .disconnect = gspca_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .suspend = gspca_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .resume = gspca_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) .reset_resume = gspca_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) module_usb_driver(sd_driver);