xref: /openbmc/linux/drivers/media/usb/gspca/vicam.c (revision 8730046c)
1 /*
2  * gspca ViCam subdriver
3  *
4  * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
5  *
6  * Based on the usbvideo vicam driver, which is:
7  *
8  * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
9  *                    Chris Cheney (chris.cheney@gmail.com),
10  *                    Pavel Machek (pavel@ucw.cz),
11  *                    John Tyner (jtyner@cs.ucr.edu),
12  *                    Monroe Williams (monroe@pobox.com)
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */
28 
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 
31 #define MODULE_NAME "vicam"
32 #define HEADER_SIZE 64
33 
34 #include <linux/workqueue.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <linux/ihex.h>
38 #include "gspca.h"
39 
40 #define VICAM_FIRMWARE "vicam/firmware.fw"
41 
42 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
43 MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
44 MODULE_LICENSE("GPL");
45 MODULE_FIRMWARE(VICAM_FIRMWARE);
46 
47 struct sd {
48 	struct gspca_dev gspca_dev;	/* !! must be the first item */
49 	struct work_struct work_struct;
50 };
51 
52 /* The vicam sensor has a resolution of 512 x 244, with I believe square
53    pixels, but this is forced to a 4:3 ratio by optics. So it has
54    non square pixels :( */
55 static struct v4l2_pix_format vicam_mode[] = {
56 	{ 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
57 		.bytesperline = 256,
58 		.sizeimage = 256 * 122,
59 		.colorspace = V4L2_COLORSPACE_SRGB,},
60 	/* 2 modes with somewhat more square pixels */
61 	{ 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
62 		.bytesperline = 256,
63 		.sizeimage = 256 * 200,
64 		.colorspace = V4L2_COLORSPACE_SRGB,},
65 	{ 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
66 		.bytesperline = 256,
67 		.sizeimage = 256 * 240,
68 		.colorspace = V4L2_COLORSPACE_SRGB,},
69 #if 0   /* This mode has extremely non square pixels, testing use only */
70 	{ 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
71 		.bytesperline = 512,
72 		.sizeimage = 512 * 122,
73 		.colorspace = V4L2_COLORSPACE_SRGB,},
74 #endif
75 	{ 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
76 		.bytesperline = 512,
77 		.sizeimage = 512 * 244,
78 		.colorspace = V4L2_COLORSPACE_SRGB,},
79 };
80 
81 static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
82 	u16 value, u16 index, u8 *data, u16 len)
83 {
84 	int ret;
85 
86 	ret = usb_control_msg(gspca_dev->dev,
87 			      usb_sndctrlpipe(gspca_dev->dev, 0),
88 			      request,
89 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
90 			      value, index, data, len, 1000);
91 	if (ret < 0)
92 		pr_err("control msg req %02X error %d\n", request, ret);
93 
94 	return ret;
95 }
96 
97 static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
98 {
99 	int ret;
100 
101 	ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
102 	if (ret < 0)
103 		return ret;
104 
105 	if (state)
106 		ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
107 
108 	return ret;
109 }
110 
111 /*
112  *  request and read a block of data
113  */
114 static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
115 {
116 	int ret, unscaled_height, act_len = 0;
117 	u8 *req_data = gspca_dev->usb_buf;
118 	s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
119 	s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
120 
121 	memset(req_data, 0, 16);
122 	req_data[0] = gain;
123 	if (gspca_dev->pixfmt.width == 256)
124 		req_data[1] |= 0x01; /* low nibble x-scale */
125 	if (gspca_dev->pixfmt.height <= 122) {
126 		req_data[1] |= 0x10; /* high nibble y-scale */
127 		unscaled_height = gspca_dev->pixfmt.height * 2;
128 	} else
129 		unscaled_height = gspca_dev->pixfmt.height;
130 	req_data[2] = 0x90; /* unknown, does not seem to do anything */
131 	if (unscaled_height <= 200)
132 		req_data[3] = 0x06; /* vend? */
133 	else if (unscaled_height <= 242) /* Yes 242 not 240 */
134 		req_data[3] = 0x07; /* vend? */
135 	else /* Up to 244 lines with req_data[3] == 0x08 */
136 		req_data[3] = 0x08; /* vend? */
137 
138 	if (expo < 256) {
139 		/* Frame rate maxed out, use partial frame expo time */
140 		req_data[4] = 255 - expo;
141 		req_data[5] = 0x00;
142 		req_data[6] = 0x00;
143 		req_data[7] = 0x01;
144 	} else {
145 		/* Modify frame rate */
146 		req_data[4] = 0x00;
147 		req_data[5] = 0x00;
148 		req_data[6] = expo & 0xFF;
149 		req_data[7] = expo >> 8;
150 	}
151 	req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
152 	/* bytes 9-15 do not seem to affect exposure or image quality */
153 
154 	mutex_lock(&gspca_dev->usb_lock);
155 	ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
156 	mutex_unlock(&gspca_dev->usb_lock);
157 	if (ret < 0)
158 		return ret;
159 
160 	ret = usb_bulk_msg(gspca_dev->dev,
161 			   usb_rcvbulkpipe(gspca_dev->dev, 0x81),
162 			   data, size, &act_len, 10000);
163 	/* successful, it returns 0, otherwise  negative */
164 	if (ret < 0 || act_len != size) {
165 		pr_err("bulk read fail (%d) len %d/%d\n",
166 		       ret, act_len, size);
167 		return -EIO;
168 	}
169 	return 0;
170 }
171 
172 /*
173  * This function is called as a workqueue function and runs whenever the camera
174  * is streaming data. Because it is a workqueue function it is allowed to sleep
175  * so we can use synchronous USB calls. To avoid possible collisions with other
176  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
177  * performing USB operations using it. In practice we don't really need this
178  * as the cameras controls are only written from the workqueue.
179  */
180 static void vicam_dostream(struct work_struct *work)
181 {
182 	struct sd *sd = container_of(work, struct sd, work_struct);
183 	struct gspca_dev *gspca_dev = &sd->gspca_dev;
184 	int ret, frame_sz;
185 	u8 *buffer;
186 
187 	frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
188 		   HEADER_SIZE;
189 	buffer = kmalloc(frame_sz, GFP_KERNEL | GFP_DMA);
190 	if (!buffer) {
191 		pr_err("Couldn't allocate USB buffer\n");
192 		goto exit;
193 	}
194 
195 	while (gspca_dev->present && gspca_dev->streaming) {
196 #ifdef CONFIG_PM
197 		if (gspca_dev->frozen)
198 			break;
199 #endif
200 		ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
201 		if (ret < 0)
202 			break;
203 
204 		/* Note the frame header contents seem to be completely
205 		   constant, they do not change with either image, or
206 		   settings. So we simply discard it. The frames have
207 		   a very similar 64 byte footer, which we don't even
208 		   bother reading from the cam */
209 		gspca_frame_add(gspca_dev, FIRST_PACKET,
210 				buffer + HEADER_SIZE,
211 				frame_sz - HEADER_SIZE);
212 		gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
213 	}
214 exit:
215 	kfree(buffer);
216 }
217 
218 /* This function is called at probe time just before sd_init */
219 static int sd_config(struct gspca_dev *gspca_dev,
220 		const struct usb_device_id *id)
221 {
222 	struct cam *cam = &gspca_dev->cam;
223 	struct sd *sd = (struct sd *)gspca_dev;
224 
225 	/* We don't use the buffer gspca allocates so make it small. */
226 	cam->bulk = 1;
227 	cam->bulk_size = 64;
228 	cam->cam_mode = vicam_mode;
229 	cam->nmodes = ARRAY_SIZE(vicam_mode);
230 
231 	INIT_WORK(&sd->work_struct, vicam_dostream);
232 
233 	return 0;
234 }
235 
236 /* this function is called at probe and resume time */
237 static int sd_init(struct gspca_dev *gspca_dev)
238 {
239 	int ret;
240 	const struct ihex_binrec *rec;
241 	const struct firmware *uninitialized_var(fw);
242 	u8 *firmware_buf;
243 
244 	ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
245 				    &gspca_dev->dev->dev);
246 	if (ret) {
247 		pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
248 		return ret;
249 	}
250 
251 	firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
252 	if (!firmware_buf) {
253 		ret = -ENOMEM;
254 		goto exit;
255 	}
256 	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
257 		memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
258 		ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
259 					be16_to_cpu(rec->len));
260 		if (ret < 0)
261 			break;
262 	}
263 
264 	kfree(firmware_buf);
265 exit:
266 	release_firmware(fw);
267 	return ret;
268 }
269 
270 /* Set up for getting frames. */
271 static int sd_start(struct gspca_dev *gspca_dev)
272 {
273 	struct sd *sd = (struct sd *)gspca_dev;
274 	int ret;
275 
276 	ret = vicam_set_camera_power(gspca_dev, 1);
277 	if (ret < 0)
278 		return ret;
279 
280 	schedule_work(&sd->work_struct);
281 
282 	return 0;
283 }
284 
285 /* called on streamoff with alt==0 and on disconnect */
286 /* the usb_lock is held at entry - restore on exit */
287 static void sd_stop0(struct gspca_dev *gspca_dev)
288 {
289 	struct sd *dev = (struct sd *)gspca_dev;
290 
291 	/* wait for the work queue to terminate */
292 	mutex_unlock(&gspca_dev->usb_lock);
293 	/* This waits for vicam_dostream to finish */
294 	flush_work(&dev->work_struct);
295 	mutex_lock(&gspca_dev->usb_lock);
296 
297 	if (gspca_dev->present)
298 		vicam_set_camera_power(gspca_dev, 0);
299 }
300 
301 static int sd_init_controls(struct gspca_dev *gspca_dev)
302 {
303 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
304 
305 	gspca_dev->vdev.ctrl_handler = hdl;
306 	v4l2_ctrl_handler_init(hdl, 2);
307 	gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
308 			V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
309 	gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
310 			V4L2_CID_GAIN, 0, 255, 1, 200);
311 
312 	if (hdl->error) {
313 		pr_err("Could not initialize controls\n");
314 		return hdl->error;
315 	}
316 	return 0;
317 }
318 
319 /* Table of supported USB devices */
320 static const struct usb_device_id device_table[] = {
321 	{USB_DEVICE(0x04c1, 0x009d)},
322 	{USB_DEVICE(0x0602, 0x1001)},
323 	{}
324 };
325 
326 MODULE_DEVICE_TABLE(usb, device_table);
327 
328 /* sub-driver description */
329 static const struct sd_desc sd_desc = {
330 	.name   = MODULE_NAME,
331 	.config = sd_config,
332 	.init   = sd_init,
333 	.init_controls = sd_init_controls,
334 	.start  = sd_start,
335 	.stop0  = sd_stop0,
336 };
337 
338 /* -- device connect -- */
339 static int sd_probe(struct usb_interface *intf,
340 		const struct usb_device_id *id)
341 {
342 	return gspca_dev_probe(intf, id,
343 			&sd_desc,
344 			sizeof(struct sd),
345 			THIS_MODULE);
346 }
347 
348 static struct usb_driver sd_driver = {
349 	.name       = MODULE_NAME,
350 	.id_table   = device_table,
351 	.probe      = sd_probe,
352 	.disconnect = gspca_disconnect,
353 #ifdef CONFIG_PM
354 	.suspend = gspca_suspend,
355 	.resume  = gspca_resume,
356 	.reset_resume = gspca_resume,
357 #endif
358 };
359 
360 module_usb_driver(sd_driver);
361