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