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