2 * gspca ViCam subdriver
4 * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
6 * Based on the usbvideo vicam driver, which is:
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)
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
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.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #define MODULE_NAME "vicam"
28 #define HEADER_SIZE 64
30 #include <linux/workqueue.h>
31 #include <linux/slab.h>
32 #include <linux/firmware.h>
33 #include <linux/ihex.h>
36 #define VICAM_FIRMWARE "/*(DEBLOBBED)*/"
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
40 MODULE_LICENSE("GPL");
44 struct gspca_dev gspca_dev; /* !! must be the first item */
45 struct work_struct work_struct;
48 /* The vicam sensor has a resolution of 512 x 244, with I believe square
49 pixels, but this is forced to a 4:3 ratio by optics. So it has
50 non square pixels :( */
51 static struct v4l2_pix_format vicam_mode[] = {
52 { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
54 .sizeimage = 256 * 122,
55 .colorspace = V4L2_COLORSPACE_SRGB,},
56 /* 2 modes with somewhat more square pixels */
57 { 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
59 .sizeimage = 256 * 200,
60 .colorspace = V4L2_COLORSPACE_SRGB,},
61 { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
63 .sizeimage = 256 * 240,
64 .colorspace = V4L2_COLORSPACE_SRGB,},
65 #if 0 /* This mode has extremely non square pixels, testing use only */
66 { 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
68 .sizeimage = 512 * 122,
69 .colorspace = V4L2_COLORSPACE_SRGB,},
71 { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
73 .sizeimage = 512 * 244,
74 .colorspace = V4L2_COLORSPACE_SRGB,},
77 static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
78 u16 value, u16 index, u8 *data, u16 len)
82 ret = usb_control_msg(gspca_dev->dev,
83 usb_sndctrlpipe(gspca_dev->dev, 0),
85 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
86 value, index, data, len, 1000);
88 pr_err("control msg req %02X error %d\n", request, ret);
93 static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
97 ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
102 ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
108 * request and read a block of data
110 static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
112 int ret, unscaled_height, act_len = 0;
113 u8 *req_data = gspca_dev->usb_buf;
114 s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
115 s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
117 memset(req_data, 0, 16);
119 if (gspca_dev->pixfmt.width == 256)
120 req_data[1] |= 0x01; /* low nibble x-scale */
121 if (gspca_dev->pixfmt.height <= 122) {
122 req_data[1] |= 0x10; /* high nibble y-scale */
123 unscaled_height = gspca_dev->pixfmt.height * 2;
125 unscaled_height = gspca_dev->pixfmt.height;
126 req_data[2] = 0x90; /* unknown, does not seem to do anything */
127 if (unscaled_height <= 200)
128 req_data[3] = 0x06; /* vend? */
129 else if (unscaled_height <= 242) /* Yes 242 not 240 */
130 req_data[3] = 0x07; /* vend? */
131 else /* Up to 244 lines with req_data[3] == 0x08 */
132 req_data[3] = 0x08; /* vend? */
135 /* Frame rate maxed out, use partial frame expo time */
136 req_data[4] = 255 - expo;
141 /* Modify frame rate */
144 req_data[6] = expo & 0xFF;
145 req_data[7] = expo >> 8;
147 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
148 /* bytes 9-15 do not seem to affect exposure or image quality */
150 mutex_lock(&gspca_dev->usb_lock);
151 ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
152 mutex_unlock(&gspca_dev->usb_lock);
156 ret = usb_bulk_msg(gspca_dev->dev,
157 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
158 data, size, &act_len, 10000);
159 /* successful, it returns 0, otherwise negative */
160 if (ret < 0 || act_len != size) {
161 pr_err("bulk read fail (%d) len %d/%d\n",
169 * This function is called as a workqueue function and runs whenever the camera
170 * is streaming data. Because it is a workqueue function it is allowed to sleep
171 * so we can use synchronous USB calls. To avoid possible collisions with other
172 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
173 * performing USB operations using it. In practice we don't really need this
174 * as the cameras controls are only written from the workqueue.
176 static void vicam_dostream(struct work_struct *work)
178 struct sd *sd = container_of(work, struct sd, work_struct);
179 struct gspca_dev *gspca_dev = &sd->gspca_dev;
183 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
185 buffer = kmalloc(frame_sz, GFP_KERNEL);
187 pr_err("Couldn't allocate USB buffer\n");
191 while (gspca_dev->present && gspca_dev->streaming) {
193 if (gspca_dev->frozen)
196 ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
200 /* Note the frame header contents seem to be completely
201 constant, they do not change with either image, or
202 settings. So we simply discard it. The frames have
203 a very similar 64 byte footer, which we don't even
204 bother reading from the cam */
205 gspca_frame_add(gspca_dev, FIRST_PACKET,
206 buffer + HEADER_SIZE,
207 frame_sz - HEADER_SIZE);
208 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
214 /* This function is called at probe time just before sd_init */
215 static int sd_config(struct gspca_dev *gspca_dev,
216 const struct usb_device_id *id)
218 struct cam *cam = &gspca_dev->cam;
219 struct sd *sd = (struct sd *)gspca_dev;
221 /* We don't use the buffer gspca allocates so make it small. */
224 cam->cam_mode = vicam_mode;
225 cam->nmodes = ARRAY_SIZE(vicam_mode);
227 INIT_WORK(&sd->work_struct, vicam_dostream);
232 /* this function is called at probe and resume time */
233 static int sd_init(struct gspca_dev *gspca_dev)
236 const struct ihex_binrec *rec;
237 const struct firmware *fw;
240 ret = reject_firmware(&fw, VICAM_FIRMWARE,
241 &gspca_dev->dev->dev);
243 pr_err("Failed to load \"/*(DEBLOBBED)*/\": %d\n", ret);
247 firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
252 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
253 memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
254 ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
255 be16_to_cpu(rec->len));
262 release_firmware(fw);
266 /* Set up for getting frames. */
267 static int sd_start(struct gspca_dev *gspca_dev)
269 struct sd *sd = (struct sd *)gspca_dev;
272 ret = vicam_set_camera_power(gspca_dev, 1);
276 schedule_work(&sd->work_struct);
281 /* called on streamoff with alt==0 and on disconnect */
282 /* the usb_lock is held at entry - restore on exit */
283 static void sd_stop0(struct gspca_dev *gspca_dev)
285 struct sd *dev = (struct sd *)gspca_dev;
287 /* wait for the work queue to terminate */
288 mutex_unlock(&gspca_dev->usb_lock);
289 /* This waits for vicam_dostream to finish */
290 flush_work(&dev->work_struct);
291 mutex_lock(&gspca_dev->usb_lock);
293 if (gspca_dev->present)
294 vicam_set_camera_power(gspca_dev, 0);
297 static int sd_init_controls(struct gspca_dev *gspca_dev)
299 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
301 gspca_dev->vdev.ctrl_handler = hdl;
302 v4l2_ctrl_handler_init(hdl, 2);
303 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
304 V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
305 gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
306 V4L2_CID_GAIN, 0, 255, 1, 200);
309 pr_err("Could not initialize controls\n");
315 /* Table of supported USB devices */
316 static const struct usb_device_id device_table[] = {
317 {USB_DEVICE(0x04c1, 0x009d)},
318 {USB_DEVICE(0x0602, 0x1001)},
322 MODULE_DEVICE_TABLE(usb, device_table);
324 /* sub-driver description */
325 static const struct sd_desc sd_desc = {
329 .init_controls = sd_init_controls,
334 /* -- device connect -- */
335 static int sd_probe(struct usb_interface *intf,
336 const struct usb_device_id *id)
338 return gspca_dev_probe(intf, id,
344 static struct usb_driver sd_driver = {
346 .id_table = device_table,
348 .disconnect = gspca_disconnect,
350 .suspend = gspca_suspend,
351 .resume = gspca_resume,
352 .reset_resume = gspca_resume,
356 module_usb_driver(sd_driver);