18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SQ905 subdriver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * History and Acknowledgments 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The original Linux driver for SQ905 based cameras was written by 128c2ecf20Sopenharmony_ci * Marcell Lengyel and further developed by many other contributors 138c2ecf20Sopenharmony_ci * and is available from http://sourceforge.net/projects/sqcam/ 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * This driver takes advantage of the reverse engineering work done for 168c2ecf20Sopenharmony_ci * that driver and for libgphoto2 but shares no code with them. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * This driver has used as a base the finepix driver and other gspca 198c2ecf20Sopenharmony_ci * based drivers and may still contain code fragments taken from those 208c2ecf20Sopenharmony_ci * drivers. 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define MODULE_NAME "sq905" 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 288c2ecf20Sopenharmony_ci#include <linux/slab.h> 298c2ecf20Sopenharmony_ci#include "gspca.h" 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, Theodore Kilgore <kilgota@auburn.edu>"); 328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver"); 338c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* Default timeouts, in ms */ 368c2ecf20Sopenharmony_ci#define SQ905_CMD_TIMEOUT 500 378c2ecf20Sopenharmony_ci#define SQ905_DATA_TIMEOUT 1000 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* Maximum transfer size to use. */ 408c2ecf20Sopenharmony_ci#define SQ905_MAX_TRANSFER 0x8000 418c2ecf20Sopenharmony_ci#define FRAME_HEADER_LEN 64 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* The known modes, or registers. These go in the "value" slot. */ 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* 00 is "none" obviously */ 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define SQ905_BULK_READ 0x03 /* precedes any bulk read */ 488c2ecf20Sopenharmony_ci#define SQ905_COMMAND 0x06 /* precedes the command codes below */ 498c2ecf20Sopenharmony_ci#define SQ905_PING 0x07 /* when reading an "idling" command */ 508c2ecf20Sopenharmony_ci#define SQ905_READ_DONE 0xc0 /* ack bulk read completed */ 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* Any non-zero value in the bottom 2 bits of the 2nd byte of 538c2ecf20Sopenharmony_ci * the ID appears to indicate the camera can do 640*480. If the 548c2ecf20Sopenharmony_ci * LSB of that byte is set the image is just upside down, otherwise 558c2ecf20Sopenharmony_ci * it is rotated 180 degrees. */ 568c2ecf20Sopenharmony_ci#define SQ905_HIRES_MASK 0x00000300 578c2ecf20Sopenharmony_ci#define SQ905_ORIENTATION_MASK 0x00000100 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/* Some command codes. These go in the "index" slot. */ 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci#define SQ905_ID 0xf0 /* asks for model string */ 628c2ecf20Sopenharmony_ci#define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */ 638c2ecf20Sopenharmony_ci#define SQ905_DATA 0x30 /* accesses photo data, not used here */ 648c2ecf20Sopenharmony_ci#define SQ905_CLEAR 0xa0 /* clear everything */ 658c2ecf20Sopenharmony_ci#define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */ 668c2ecf20Sopenharmony_ci#define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */ 678c2ecf20Sopenharmony_ci#define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */ 688c2ecf20Sopenharmony_ci/* note that the capture command also controls the output dimensions */ 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/* Structure to hold all of our device specific stuff */ 718c2ecf20Sopenharmony_cistruct sd { 728c2ecf20Sopenharmony_ci struct gspca_dev gspca_dev; /* !! must be the first item */ 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci /* 758c2ecf20Sopenharmony_ci * Driver stuff 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ci struct work_struct work_struct; 788c2ecf20Sopenharmony_ci struct workqueue_struct *work_thread; 798c2ecf20Sopenharmony_ci}; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic struct v4l2_pix_format sq905_mode[] = { 828c2ecf20Sopenharmony_ci { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 838c2ecf20Sopenharmony_ci .bytesperline = 160, 848c2ecf20Sopenharmony_ci .sizeimage = 160 * 120, 858c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_SRGB, 868c2ecf20Sopenharmony_ci .priv = 0}, 878c2ecf20Sopenharmony_ci { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 888c2ecf20Sopenharmony_ci .bytesperline = 320, 898c2ecf20Sopenharmony_ci .sizeimage = 320 * 240, 908c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_SRGB, 918c2ecf20Sopenharmony_ci .priv = 0}, 928c2ecf20Sopenharmony_ci { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 938c2ecf20Sopenharmony_ci .bytesperline = 640, 948c2ecf20Sopenharmony_ci .sizeimage = 640 * 480, 958c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_SRGB, 968c2ecf20Sopenharmony_ci .priv = 0} 978c2ecf20Sopenharmony_ci}; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* 1008c2ecf20Sopenharmony_ci * Send a command to the camera. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_cistatic int sq905_command(struct gspca_dev *gspca_dev, u16 index) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci int ret; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci gspca_dev->usb_buf[0] = '\0'; 1078c2ecf20Sopenharmony_ci ret = usb_control_msg(gspca_dev->dev, 1088c2ecf20Sopenharmony_ci usb_sndctrlpipe(gspca_dev->dev, 0), 1098c2ecf20Sopenharmony_ci USB_REQ_SYNCH_FRAME, /* request */ 1108c2ecf20Sopenharmony_ci USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1118c2ecf20Sopenharmony_ci SQ905_COMMAND, index, gspca_dev->usb_buf, 1, 1128c2ecf20Sopenharmony_ci SQ905_CMD_TIMEOUT); 1138c2ecf20Sopenharmony_ci if (ret < 0) { 1148c2ecf20Sopenharmony_ci pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret); 1158c2ecf20Sopenharmony_ci return ret; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci ret = usb_control_msg(gspca_dev->dev, 1198c2ecf20Sopenharmony_ci usb_rcvctrlpipe(gspca_dev->dev, 0), 1208c2ecf20Sopenharmony_ci USB_REQ_SYNCH_FRAME, /* request */ 1218c2ecf20Sopenharmony_ci USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1228c2ecf20Sopenharmony_ci SQ905_PING, 0, gspca_dev->usb_buf, 1, 1238c2ecf20Sopenharmony_ci SQ905_CMD_TIMEOUT); 1248c2ecf20Sopenharmony_ci if (ret < 0) { 1258c2ecf20Sopenharmony_ci pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret); 1268c2ecf20Sopenharmony_ci return ret; 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* 1338c2ecf20Sopenharmony_ci * Acknowledge the end of a frame - see warning on sq905_command. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_cistatic int sq905_ack_frame(struct gspca_dev *gspca_dev) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci int ret; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci gspca_dev->usb_buf[0] = '\0'; 1408c2ecf20Sopenharmony_ci ret = usb_control_msg(gspca_dev->dev, 1418c2ecf20Sopenharmony_ci usb_sndctrlpipe(gspca_dev->dev, 0), 1428c2ecf20Sopenharmony_ci USB_REQ_SYNCH_FRAME, /* request */ 1438c2ecf20Sopenharmony_ci USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1448c2ecf20Sopenharmony_ci SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1, 1458c2ecf20Sopenharmony_ci SQ905_CMD_TIMEOUT); 1468c2ecf20Sopenharmony_ci if (ret < 0) { 1478c2ecf20Sopenharmony_ci pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret); 1488c2ecf20Sopenharmony_ci return ret; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci return 0; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/* 1558c2ecf20Sopenharmony_ci * request and read a block of data - see warning on sq905_command. 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_cistatic int 1588c2ecf20Sopenharmony_cisq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci int ret; 1618c2ecf20Sopenharmony_ci int act_len = 0; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci gspca_dev->usb_buf[0] = '\0'; 1648c2ecf20Sopenharmony_ci if (need_lock) 1658c2ecf20Sopenharmony_ci mutex_lock(&gspca_dev->usb_lock); 1668c2ecf20Sopenharmony_ci ret = usb_control_msg(gspca_dev->dev, 1678c2ecf20Sopenharmony_ci usb_sndctrlpipe(gspca_dev->dev, 0), 1688c2ecf20Sopenharmony_ci USB_REQ_SYNCH_FRAME, /* request */ 1698c2ecf20Sopenharmony_ci USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1708c2ecf20Sopenharmony_ci SQ905_BULK_READ, size, gspca_dev->usb_buf, 1718c2ecf20Sopenharmony_ci 1, SQ905_CMD_TIMEOUT); 1728c2ecf20Sopenharmony_ci if (need_lock) 1738c2ecf20Sopenharmony_ci mutex_unlock(&gspca_dev->usb_lock); 1748c2ecf20Sopenharmony_ci if (ret < 0) { 1758c2ecf20Sopenharmony_ci pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret); 1768c2ecf20Sopenharmony_ci return ret; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci ret = usb_bulk_msg(gspca_dev->dev, 1798c2ecf20Sopenharmony_ci usb_rcvbulkpipe(gspca_dev->dev, 0x81), 1808c2ecf20Sopenharmony_ci data, size, &act_len, SQ905_DATA_TIMEOUT); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* successful, it returns 0, otherwise negative */ 1838c2ecf20Sopenharmony_ci if (ret < 0 || act_len != size) { 1848c2ecf20Sopenharmony_ci pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size); 1858c2ecf20Sopenharmony_ci return -EIO; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci return 0; 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci/* 1918c2ecf20Sopenharmony_ci * This function is called as a workqueue function and runs whenever the camera 1928c2ecf20Sopenharmony_ci * is streaming data. Because it is a workqueue function it is allowed to sleep 1938c2ecf20Sopenharmony_ci * so we can use synchronous USB calls. To avoid possible collisions with other 1948c2ecf20Sopenharmony_ci * threads attempting to use gspca_dev->usb_buf we take the usb_lock when 1958c2ecf20Sopenharmony_ci * performing USB operations using it. In practice we don't really need this 1968c2ecf20Sopenharmony_ci * as the camera doesn't provide any controls. 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_cistatic void sq905_dostream(struct work_struct *work) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci struct sd *dev = container_of(work, struct sd, work_struct); 2018c2ecf20Sopenharmony_ci struct gspca_dev *gspca_dev = &dev->gspca_dev; 2028c2ecf20Sopenharmony_ci int bytes_left; /* bytes remaining in current frame. */ 2038c2ecf20Sopenharmony_ci int data_len; /* size to use for the next read. */ 2048c2ecf20Sopenharmony_ci int header_read; /* true if we have already read the frame header. */ 2058c2ecf20Sopenharmony_ci int packet_type; 2068c2ecf20Sopenharmony_ci int frame_sz; 2078c2ecf20Sopenharmony_ci int ret; 2088c2ecf20Sopenharmony_ci u8 *data; 2098c2ecf20Sopenharmony_ci u8 *buffer; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL); 2128c2ecf20Sopenharmony_ci if (!buffer) { 2138c2ecf20Sopenharmony_ci pr_err("Couldn't allocate USB buffer\n"); 2148c2ecf20Sopenharmony_ci goto quit_stream; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage 2188c2ecf20Sopenharmony_ci + FRAME_HEADER_LEN; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci while (gspca_dev->present && gspca_dev->streaming) { 2218c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 2228c2ecf20Sopenharmony_ci if (gspca_dev->frozen) 2238c2ecf20Sopenharmony_ci break; 2248c2ecf20Sopenharmony_ci#endif 2258c2ecf20Sopenharmony_ci /* request some data and then read it until we have 2268c2ecf20Sopenharmony_ci * a complete frame. */ 2278c2ecf20Sopenharmony_ci bytes_left = frame_sz; 2288c2ecf20Sopenharmony_ci header_read = 0; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci /* Note we do not check for gspca_dev->streaming here, as 2318c2ecf20Sopenharmony_ci we must finish reading an entire frame, otherwise the 2328c2ecf20Sopenharmony_ci next time we stream we start reading in the middle of a 2338c2ecf20Sopenharmony_ci frame. */ 2348c2ecf20Sopenharmony_ci while (bytes_left > 0 && gspca_dev->present) { 2358c2ecf20Sopenharmony_ci data_len = bytes_left > SQ905_MAX_TRANSFER ? 2368c2ecf20Sopenharmony_ci SQ905_MAX_TRANSFER : bytes_left; 2378c2ecf20Sopenharmony_ci ret = sq905_read_data(gspca_dev, buffer, data_len, 1); 2388c2ecf20Sopenharmony_ci if (ret < 0) 2398c2ecf20Sopenharmony_ci goto quit_stream; 2408c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_PACK, 2418c2ecf20Sopenharmony_ci "Got %d bytes out of %d for frame\n", 2428c2ecf20Sopenharmony_ci data_len, bytes_left); 2438c2ecf20Sopenharmony_ci bytes_left -= data_len; 2448c2ecf20Sopenharmony_ci data = buffer; 2458c2ecf20Sopenharmony_ci if (!header_read) { 2468c2ecf20Sopenharmony_ci packet_type = FIRST_PACKET; 2478c2ecf20Sopenharmony_ci /* The first 64 bytes of each frame are 2488c2ecf20Sopenharmony_ci * a header full of FF 00 bytes */ 2498c2ecf20Sopenharmony_ci data += FRAME_HEADER_LEN; 2508c2ecf20Sopenharmony_ci data_len -= FRAME_HEADER_LEN; 2518c2ecf20Sopenharmony_ci header_read = 1; 2528c2ecf20Sopenharmony_ci } else if (bytes_left == 0) { 2538c2ecf20Sopenharmony_ci packet_type = LAST_PACKET; 2548c2ecf20Sopenharmony_ci } else { 2558c2ecf20Sopenharmony_ci packet_type = INTER_PACKET; 2568c2ecf20Sopenharmony_ci } 2578c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, packet_type, 2588c2ecf20Sopenharmony_ci data, data_len); 2598c2ecf20Sopenharmony_ci /* If entire frame fits in one packet we still 2608c2ecf20Sopenharmony_ci need to add a LAST_PACKET */ 2618c2ecf20Sopenharmony_ci if (packet_type == FIRST_PACKET && 2628c2ecf20Sopenharmony_ci bytes_left == 0) 2638c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, LAST_PACKET, 2648c2ecf20Sopenharmony_ci NULL, 0); 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci if (gspca_dev->present) { 2678c2ecf20Sopenharmony_ci /* acknowledge the frame */ 2688c2ecf20Sopenharmony_ci mutex_lock(&gspca_dev->usb_lock); 2698c2ecf20Sopenharmony_ci ret = sq905_ack_frame(gspca_dev); 2708c2ecf20Sopenharmony_ci mutex_unlock(&gspca_dev->usb_lock); 2718c2ecf20Sopenharmony_ci if (ret < 0) 2728c2ecf20Sopenharmony_ci goto quit_stream; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ciquit_stream: 2768c2ecf20Sopenharmony_ci if (gspca_dev->present) { 2778c2ecf20Sopenharmony_ci mutex_lock(&gspca_dev->usb_lock); 2788c2ecf20Sopenharmony_ci sq905_command(gspca_dev, SQ905_CLEAR); 2798c2ecf20Sopenharmony_ci mutex_unlock(&gspca_dev->usb_lock); 2808c2ecf20Sopenharmony_ci } 2818c2ecf20Sopenharmony_ci kfree(buffer); 2828c2ecf20Sopenharmony_ci} 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci/* This function is called at probe time just before sd_init */ 2858c2ecf20Sopenharmony_cistatic int sd_config(struct gspca_dev *gspca_dev, 2868c2ecf20Sopenharmony_ci const struct usb_device_id *id) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci struct cam *cam = &gspca_dev->cam; 2898c2ecf20Sopenharmony_ci struct sd *dev = (struct sd *) gspca_dev; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci /* We don't use the buffer gspca allocates so make it small. */ 2928c2ecf20Sopenharmony_ci cam->bulk = 1; 2938c2ecf20Sopenharmony_ci cam->bulk_size = 64; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci INIT_WORK(&dev->work_struct, sq905_dostream); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci return 0; 2988c2ecf20Sopenharmony_ci} 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci/* called on streamoff with alt==0 and on disconnect */ 3018c2ecf20Sopenharmony_ci/* the usb_lock is held at entry - restore on exit */ 3028c2ecf20Sopenharmony_cistatic void sd_stop0(struct gspca_dev *gspca_dev) 3038c2ecf20Sopenharmony_ci{ 3048c2ecf20Sopenharmony_ci struct sd *dev = (struct sd *) gspca_dev; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci /* wait for the work queue to terminate */ 3078c2ecf20Sopenharmony_ci mutex_unlock(&gspca_dev->usb_lock); 3088c2ecf20Sopenharmony_ci /* This waits for sq905_dostream to finish */ 3098c2ecf20Sopenharmony_ci destroy_workqueue(dev->work_thread); 3108c2ecf20Sopenharmony_ci dev->work_thread = NULL; 3118c2ecf20Sopenharmony_ci mutex_lock(&gspca_dev->usb_lock); 3128c2ecf20Sopenharmony_ci} 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci/* this function is called at probe and resume time */ 3158c2ecf20Sopenharmony_cistatic int sd_init(struct gspca_dev *gspca_dev) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci u32 ident; 3188c2ecf20Sopenharmony_ci int ret; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* connect to the camera and read 3218c2ecf20Sopenharmony_ci * the model ID and process that and put it away. 3228c2ecf20Sopenharmony_ci */ 3238c2ecf20Sopenharmony_ci ret = sq905_command(gspca_dev, SQ905_CLEAR); 3248c2ecf20Sopenharmony_ci if (ret < 0) 3258c2ecf20Sopenharmony_ci return ret; 3268c2ecf20Sopenharmony_ci ret = sq905_command(gspca_dev, SQ905_ID); 3278c2ecf20Sopenharmony_ci if (ret < 0) 3288c2ecf20Sopenharmony_ci return ret; 3298c2ecf20Sopenharmony_ci ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0); 3308c2ecf20Sopenharmony_ci if (ret < 0) 3318c2ecf20Sopenharmony_ci return ret; 3328c2ecf20Sopenharmony_ci /* usb_buf is allocated with kmalloc so is aligned. 3338c2ecf20Sopenharmony_ci * Camera model number is the right way round if we assume this 3348c2ecf20Sopenharmony_ci * reverse engineered ID is supposed to be big endian. */ 3358c2ecf20Sopenharmony_ci ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf); 3368c2ecf20Sopenharmony_ci ret = sq905_command(gspca_dev, SQ905_CLEAR); 3378c2ecf20Sopenharmony_ci if (ret < 0) 3388c2ecf20Sopenharmony_ci return ret; 3398c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_CONF, "SQ905 camera ID %08x detected\n", ident); 3408c2ecf20Sopenharmony_ci gspca_dev->cam.cam_mode = sq905_mode; 3418c2ecf20Sopenharmony_ci gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode); 3428c2ecf20Sopenharmony_ci if (!(ident & SQ905_HIRES_MASK)) 3438c2ecf20Sopenharmony_ci gspca_dev->cam.nmodes--; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci if (ident & SQ905_ORIENTATION_MASK) 3468c2ecf20Sopenharmony_ci gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP; 3478c2ecf20Sopenharmony_ci else 3488c2ecf20Sopenharmony_ci gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP | 3498c2ecf20Sopenharmony_ci V4L2_IN_ST_HFLIP; 3508c2ecf20Sopenharmony_ci return 0; 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/* Set up for getting frames. */ 3548c2ecf20Sopenharmony_cistatic int sd_start(struct gspca_dev *gspca_dev) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct sd *dev = (struct sd *) gspca_dev; 3578c2ecf20Sopenharmony_ci int ret; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci /* "Open the shutter" and set size, to start capture */ 3608c2ecf20Sopenharmony_ci switch (gspca_dev->curr_mode) { 3618c2ecf20Sopenharmony_ci default: 3628c2ecf20Sopenharmony_ci/* case 2: */ 3638c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n"); 3648c2ecf20Sopenharmony_ci ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH); 3658c2ecf20Sopenharmony_ci break; 3668c2ecf20Sopenharmony_ci case 1: 3678c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n"); 3688c2ecf20Sopenharmony_ci ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED); 3698c2ecf20Sopenharmony_ci break; 3708c2ecf20Sopenharmony_ci case 0: 3718c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_STREAM, "Start streaming at low resolution\n"); 3728c2ecf20Sopenharmony_ci ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW); 3738c2ecf20Sopenharmony_ci } 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if (ret < 0) { 3768c2ecf20Sopenharmony_ci gspca_err(gspca_dev, "Start streaming command failed\n"); 3778c2ecf20Sopenharmony_ci return ret; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci /* Start the workqueue function to do the streaming */ 3808c2ecf20Sopenharmony_ci dev->work_thread = create_singlethread_workqueue(MODULE_NAME); 3818c2ecf20Sopenharmony_ci if (!dev->work_thread) 3828c2ecf20Sopenharmony_ci return -ENOMEM; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci queue_work(dev->work_thread, &dev->work_struct); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci return 0; 3878c2ecf20Sopenharmony_ci} 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci/* Table of supported USB devices */ 3908c2ecf20Sopenharmony_cistatic const struct usb_device_id device_table[] = { 3918c2ecf20Sopenharmony_ci {USB_DEVICE(0x2770, 0x9120)}, 3928c2ecf20Sopenharmony_ci {} 3938c2ecf20Sopenharmony_ci}; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, device_table); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci/* sub-driver description */ 3988c2ecf20Sopenharmony_cistatic const struct sd_desc sd_desc = { 3998c2ecf20Sopenharmony_ci .name = MODULE_NAME, 4008c2ecf20Sopenharmony_ci .config = sd_config, 4018c2ecf20Sopenharmony_ci .init = sd_init, 4028c2ecf20Sopenharmony_ci .start = sd_start, 4038c2ecf20Sopenharmony_ci .stop0 = sd_stop0, 4048c2ecf20Sopenharmony_ci}; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci/* -- device connect -- */ 4078c2ecf20Sopenharmony_cistatic int sd_probe(struct usb_interface *intf, 4088c2ecf20Sopenharmony_ci const struct usb_device_id *id) 4098c2ecf20Sopenharmony_ci{ 4108c2ecf20Sopenharmony_ci return gspca_dev_probe(intf, id, 4118c2ecf20Sopenharmony_ci &sd_desc, 4128c2ecf20Sopenharmony_ci sizeof(struct sd), 4138c2ecf20Sopenharmony_ci THIS_MODULE); 4148c2ecf20Sopenharmony_ci} 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_cistatic struct usb_driver sd_driver = { 4178c2ecf20Sopenharmony_ci .name = MODULE_NAME, 4188c2ecf20Sopenharmony_ci .id_table = device_table, 4198c2ecf20Sopenharmony_ci .probe = sd_probe, 4208c2ecf20Sopenharmony_ci .disconnect = gspca_disconnect, 4218c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 4228c2ecf20Sopenharmony_ci .suspend = gspca_suspend, 4238c2ecf20Sopenharmony_ci .resume = gspca_resume, 4248c2ecf20Sopenharmony_ci .reset_resume = gspca_resume, 4258c2ecf20Sopenharmony_ci#endif 4268c2ecf20Sopenharmony_ci}; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_cimodule_usb_driver(sd_driver); 429