blob: 15a30f7a4b2ab73593da9d28988effee6f165287 [file] [log] [blame]
Hans de Goede49b61ec2011-02-21 11:06:29 -03001/*
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 * Christopher L Cheney (ccheney@cheney.cx),
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
Joe Perches133a9fe2011-08-21 19:56:57 -030029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Hans de Goede49b61ec2011-02-21 11:06:29 -030031#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
Tim Gardnerd74185b2012-04-12 17:23:21 -030040#define VICAM_FIRMWARE "vicam/firmware.fw"
41
Hans de Goede49b61ec2011-02-21 11:06:29 -030042MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
43MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
44MODULE_LICENSE("GPL");
Tim Gardnerd74185b2012-04-12 17:23:21 -030045MODULE_FIRMWARE(VICAM_FIRMWARE);
Hans de Goede49b61ec2011-02-21 11:06:29 -030046
47enum e_ctrl {
48 GAIN,
49 EXPOSURE,
50 NCTRL /* number of controls */
51};
52
53struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */
55 struct work_struct work_struct;
56 struct workqueue_struct *work_thread;
57 struct gspca_ctrl ctrls[NCTRL];
58};
59
60/* The vicam sensor has a resolution of 512 x 244, with I believe square
61 pixels, but this is forced to a 4:3 ratio by optics. So it has
62 non square pixels :( */
63static struct v4l2_pix_format vicam_mode[] = {
64 { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
65 .bytesperline = 256,
66 .sizeimage = 256 * 122,
67 .colorspace = V4L2_COLORSPACE_SRGB,},
68 /* 2 modes with somewhat more square pixels */
69 { 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
70 .bytesperline = 256,
71 .sizeimage = 256 * 200,
72 .colorspace = V4L2_COLORSPACE_SRGB,},
73 { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
74 .bytesperline = 256,
75 .sizeimage = 256 * 240,
76 .colorspace = V4L2_COLORSPACE_SRGB,},
77#if 0 /* This mode has extremely non square pixels, testing use only */
78 { 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
79 .bytesperline = 512,
80 .sizeimage = 512 * 122,
81 .colorspace = V4L2_COLORSPACE_SRGB,},
82#endif
83 { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
84 .bytesperline = 512,
85 .sizeimage = 512 * 244,
86 .colorspace = V4L2_COLORSPACE_SRGB,},
87};
88
89static const struct ctrl sd_ctrls[] = {
90[GAIN] = {
91 {
92 .id = V4L2_CID_GAIN,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "Gain",
95 .minimum = 0,
96 .maximum = 255,
97 .step = 1,
98 .default_value = 200,
99 },
100 },
101[EXPOSURE] = {
102 {
103 .id = V4L2_CID_EXPOSURE,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Exposure",
106 .minimum = 0,
107 .maximum = 2047,
108 .step = 1,
109 .default_value = 256,
110 },
111 },
112};
113
114static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
115 u16 value, u16 index, u8 *data, u16 len)
116{
117 int ret;
118
119 ret = usb_control_msg(gspca_dev->dev,
120 usb_sndctrlpipe(gspca_dev->dev, 0),
121 request,
122 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
123 value, index, data, len, 1000);
124 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300125 pr_err("control msg req %02X error %d\n", request, ret);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300126
127 return ret;
128}
129
130static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
131{
132 int ret;
133
134 ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
135 if (ret < 0)
136 return ret;
137
138 if (state)
139 ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
140
141 return ret;
142}
143
144/*
145 * request and read a block of data - see warning on vicam_command.
146 */
147static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
148{
149 struct sd *sd = (struct sd *)gspca_dev;
150 int ret, unscaled_height, act_len = 0;
151 u8 *req_data = gspca_dev->usb_buf;
152
153 memset(req_data, 0, 16);
154 req_data[0] = sd->ctrls[GAIN].val;
155 if (gspca_dev->width == 256)
156 req_data[1] |= 0x01; /* low nibble x-scale */
157 if (gspca_dev->height <= 122) {
158 req_data[1] |= 0x10; /* high nibble y-scale */
159 unscaled_height = gspca_dev->height * 2;
160 } else
161 unscaled_height = gspca_dev->height;
162 req_data[2] = 0x90; /* unknown, does not seem to do anything */
163 if (unscaled_height <= 200)
164 req_data[3] = 0x06; /* vend? */
165 else if (unscaled_height <= 242) /* Yes 242 not 240 */
166 req_data[3] = 0x07; /* vend? */
167 else /* Up to 244 lines with req_data[3] == 0x08 */
168 req_data[3] = 0x08; /* vend? */
169
170 if (sd->ctrls[EXPOSURE].val < 256) {
171 /* Frame rate maxed out, use partial frame expo time */
172 req_data[4] = 255 - sd->ctrls[EXPOSURE].val;
173 req_data[5] = 0x00;
174 req_data[6] = 0x00;
175 req_data[7] = 0x01;
176 } else {
177 /* Modify frame rate */
178 req_data[4] = 0x00;
179 req_data[5] = 0x00;
180 req_data[6] = sd->ctrls[EXPOSURE].val & 0xFF;
181 req_data[7] = sd->ctrls[EXPOSURE].val >> 8;
182 }
183 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
184 /* bytes 9-15 do not seem to affect exposure or image quality */
185
186 mutex_lock(&gspca_dev->usb_lock);
187 ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
188 mutex_unlock(&gspca_dev->usb_lock);
189 if (ret < 0)
190 return ret;
191
192 ret = usb_bulk_msg(gspca_dev->dev,
193 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
194 data, size, &act_len, 10000);
195 /* successful, it returns 0, otherwise negative */
196 if (ret < 0 || act_len != size) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300197 pr_err("bulk read fail (%d) len %d/%d\n",
198 ret, act_len, size);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300199 return -EIO;
200 }
201 return 0;
202}
203
204/* This function is called as a workqueue function and runs whenever the camera
205 * is streaming data. Because it is a workqueue function it is allowed to sleep
206 * so we can use synchronous USB calls. To avoid possible collisions with other
207 * threads attempting to use the camera's USB interface we take the gspca
208 * usb_lock when performing USB operations. In practice the only thing we need
209 * to protect against is the usb_set_interface call that gspca makes during
210 * stream_off as the camera doesn't provide any controls that the user could try
211 * to change.
212 */
213static void vicam_dostream(struct work_struct *work)
214{
215 struct sd *sd = container_of(work, struct sd, work_struct);
216 struct gspca_dev *gspca_dev = &sd->gspca_dev;
217 int ret, frame_sz;
218 u8 *buffer;
219
220 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
221 HEADER_SIZE;
222 buffer = kmalloc(frame_sz, GFP_KERNEL | GFP_DMA);
223 if (!buffer) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300224 pr_err("Couldn't allocate USB buffer\n");
Hans de Goede49b61ec2011-02-21 11:06:29 -0300225 goto exit;
226 }
227
Hans Verkuil4ad34da2012-05-18 08:40:42 -0300228 while (gspca_dev->dev && gspca_dev->streaming) {
229#ifdef CONFIG_PM
230 if (gspca_dev->frozen)
231 break;
232#endif
Hans de Goede49b61ec2011-02-21 11:06:29 -0300233 ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
234 if (ret < 0)
235 break;
236
237 /* Note the frame header contents seem to be completely
238 constant, they do not change with either image, or
239 settings. So we simply discard it. The frames have
240 a very similar 64 byte footer, which we don't even
241 bother reading from the cam */
242 gspca_frame_add(gspca_dev, FIRST_PACKET,
243 buffer + HEADER_SIZE,
244 frame_sz - HEADER_SIZE);
245 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
246 }
247exit:
248 kfree(buffer);
249}
250
251/* This function is called at probe time just before sd_init */
252static int sd_config(struct gspca_dev *gspca_dev,
253 const struct usb_device_id *id)
254{
255 struct cam *cam = &gspca_dev->cam;
256 struct sd *sd = (struct sd *)gspca_dev;
257
258 /* We don't use the buffer gspca allocates so make it small. */
259 cam->bulk = 1;
260 cam->bulk_size = 64;
261 cam->cam_mode = vicam_mode;
262 cam->nmodes = ARRAY_SIZE(vicam_mode);
263 cam->ctrls = sd->ctrls;
264
265 INIT_WORK(&sd->work_struct, vicam_dostream);
266
267 return 0;
268}
269
270/* this function is called at probe and resume time */
271static int sd_init(struct gspca_dev *gspca_dev)
272{
273 int ret;
274 const struct ihex_binrec *rec;
275 const struct firmware *uninitialized_var(fw);
276 u8 *firmware_buf;
277
Tim Gardnerd74185b2012-04-12 17:23:21 -0300278 ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
Hans de Goede49b61ec2011-02-21 11:06:29 -0300279 &gspca_dev->dev->dev);
280 if (ret) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300281 pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300282 return ret;
283 }
284
285 firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
286 if (!firmware_buf) {
287 ret = -ENOMEM;
288 goto exit;
289 }
290 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
291 memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
292 ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
293 be16_to_cpu(rec->len));
294 if (ret < 0)
295 break;
296 }
297
298 kfree(firmware_buf);
299exit:
300 release_firmware(fw);
301 return ret;
302}
303
304/* Set up for getting frames. */
305static int sd_start(struct gspca_dev *gspca_dev)
306{
307 struct sd *sd = (struct sd *)gspca_dev;
308 int ret;
309
310 ret = vicam_set_camera_power(gspca_dev, 1);
311 if (ret < 0)
312 return ret;
313
314 /* Start the workqueue function to do the streaming */
315 sd->work_thread = create_singlethread_workqueue(MODULE_NAME);
316 queue_work(sd->work_thread, &sd->work_struct);
317
318 return 0;
319}
320
321/* called on streamoff with alt==0 and on disconnect */
322/* the usb_lock is held at entry - restore on exit */
323static void sd_stop0(struct gspca_dev *gspca_dev)
324{
325 struct sd *dev = (struct sd *)gspca_dev;
326
327 /* wait for the work queue to terminate */
328 mutex_unlock(&gspca_dev->usb_lock);
329 /* This waits for vicam_dostream to finish */
330 destroy_workqueue(dev->work_thread);
331 dev->work_thread = NULL;
332 mutex_lock(&gspca_dev->usb_lock);
333
Hans Verkuil254902b2012-05-06 09:28:22 -0300334 if (gspca_dev->dev)
Hans de Goede27d3e362011-12-29 16:50:57 -0300335 vicam_set_camera_power(gspca_dev, 0);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300336}
337
338/* Table of supported USB devices */
339static const struct usb_device_id device_table[] = {
340 {USB_DEVICE(0x04c1, 0x009d)},
341 {USB_DEVICE(0x0602, 0x1001)},
342 {}
343};
344
345MODULE_DEVICE_TABLE(usb, device_table);
346
347/* sub-driver description */
348static const struct sd_desc sd_desc = {
349 .name = MODULE_NAME,
350 .ctrls = sd_ctrls,
351 .nctrls = ARRAY_SIZE(sd_ctrls),
352 .config = sd_config,
353 .init = sd_init,
354 .start = sd_start,
355 .stop0 = sd_stop0,
356};
357
358/* -- device connect -- */
359static int sd_probe(struct usb_interface *intf,
360 const struct usb_device_id *id)
361{
362 return gspca_dev_probe(intf, id,
363 &sd_desc,
364 sizeof(struct sd),
365 THIS_MODULE);
366}
367
368static struct usb_driver sd_driver = {
369 .name = MODULE_NAME,
370 .id_table = device_table,
371 .probe = sd_probe,
372 .disconnect = gspca_disconnect,
373#ifdef CONFIG_PM
374 .suspend = gspca_suspend,
375 .resume = gspca_resume,
376#endif
377};
378
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800379module_usb_driver(sd_driver);