2 * gspca ViCam subdriver
6 * Based on the usbvideo vicam driver, which is:
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.
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
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #define MODULE_NAME "vicam"
32 #define HEADER_SIZE 64
34 #include <linux/workqueue.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <linux/ihex.h>
40 #define VICAM_FIRMWARE "vicam/firmware.fw"
43 MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
44 MODULE_LICENSE("GPL");
45 MODULE_FIRMWARE(VICAM_FIRMWARE);
48 struct gspca_dev gspca_dev; /* !! must be the first item */
49 struct work_struct work_struct;
50 struct workqueue_struct *work_thread;
53 /* The vicam sensor has a resolution of 512 x 244, with I believe square
54 pixels, but this is forced to a 4:3 ratio by optics. So it has
55 non square pixels :( */
56 static struct v4l2_pix_format vicam_mode[] = {
57 { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
59 .sizeimage = 256 * 122,
60 .colorspace = V4L2_COLORSPACE_SRGB,},
61 /* 2 modes with somewhat more square pixels */
62 { 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
64 .sizeimage = 256 * 200,
65 .colorspace = V4L2_COLORSPACE_SRGB,},
66 { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
68 .sizeimage = 256 * 240,
69 .colorspace = V4L2_COLORSPACE_SRGB,},
70 #if 0 /* This mode has extremely non square pixels, testing use only */
71 { 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
73 .sizeimage = 512 * 122,
74 .colorspace = V4L2_COLORSPACE_SRGB,},
76 { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
78 .sizeimage = 512 * 244,
79 .colorspace = V4L2_COLORSPACE_SRGB,},
82 static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
83 u16 value, u16 index, u8 *data, u16 len)
87 ret = usb_control_msg(gspca_dev->dev,
88 usb_sndctrlpipe(gspca_dev->dev, 0),
90 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
91 value, index, data, len, 1000);
93 pr_err("control msg req %02X error %d\n", request, ret);
98 static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
102 ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
107 ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
113 * request and read a block of data
115 static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
117 int ret, unscaled_height, act_len = 0;
118 u8 *req_data = gspca_dev->usb_buf;
119 s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
120 s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
122 memset(req_data, 0, 16);
124 if (gspca_dev->width == 256)
125 req_data[1] |= 0x01; /* low nibble x-scale */
126 if (gspca_dev->height <= 122) {
127 req_data[1] |= 0x10; /* high nibble y-scale */
128 unscaled_height = gspca_dev->height * 2;
130 unscaled_height = gspca_dev->height;
131 req_data[2] = 0x90; /* unknown, does not seem to do anything */
132 if (unscaled_height <= 200)
133 req_data[3] = 0x06; /* vend? */
134 else if (unscaled_height <= 242) /* Yes 242 not 240 */
135 req_data[3] = 0x07; /* vend? */
136 else /* Up to 244 lines with req_data[3] == 0x08 */
137 req_data[3] = 0x08; /* vend? */
140 /* Frame rate maxed out, use partial frame expo time */
141 req_data[4] = 255 - expo;
146 /* Modify frame rate */
149 req_data[6] = expo & 0xFF;
150 req_data[7] = expo >> 8;
152 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
153 /* bytes 9-15 do not seem to affect exposure or image quality */
155 mutex_lock(&gspca_dev->usb_lock);
156 ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
157 mutex_unlock(&gspca_dev->usb_lock);
161 ret = usb_bulk_msg(gspca_dev->dev,
162 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
163 data, size, &act_len, 10000);
164 /* successful, it returns 0, otherwise negative */
165 if (ret < 0 || act_len != size) {
166 pr_err("bulk read fail (%d) len %d/%d\n",
174 * This function is called as a workqueue function and runs whenever the camera
175 * is streaming data. Because it is a workqueue function it is allowed to sleep
176 * so we can use synchronous USB calls. To avoid possible collisions with other
177 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
178 * performing USB operations using it. In practice we don't really need this
179 * as the cameras controls are only written from the workqueue.
181 static void vicam_dostream(struct work_struct *work)
183 struct sd *sd = container_of(work, struct sd, work_struct);
184 struct gspca_dev *gspca_dev = &sd->gspca_dev;
188 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
190 buffer = kmalloc(frame_sz, GFP_KERNEL | GFP_DMA);
192 pr_err("Couldn't allocate USB buffer\n");
196 while (gspca_dev->present && gspca_dev->streaming) {
198 if (gspca_dev->frozen)
201 ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
205 /* Note the frame header contents seem to be completely
206 constant, they do not change with either image, or
207 settings. So we simply discard it. The frames have
208 a very similar 64 byte footer, which we don't even
209 bother reading from the cam */
210 gspca_frame_add(gspca_dev, FIRST_PACKET,
211 buffer + HEADER_SIZE,
212 frame_sz - HEADER_SIZE);
213 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
219 /* This function is called at probe time just before sd_init */
220 static int sd_config(struct gspca_dev *gspca_dev,
221 const struct usb_device_id *id)
223 struct cam *cam = &gspca_dev->cam;
224 struct sd *sd = (struct sd *)gspca_dev;
226 /* We don't use the buffer gspca allocates so make it small. */
229 cam->cam_mode = vicam_mode;
230 cam->nmodes = ARRAY_SIZE(vicam_mode);
232 INIT_WORK(&sd->work_struct, vicam_dostream);
237 /* this function is called at probe and resume time */
238 static int sd_init(struct gspca_dev *gspca_dev)
241 const struct ihex_binrec *rec;
242 const struct firmware *uninitialized_var(fw);
245 ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
246 &gspca_dev->dev->dev);
248 pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
252 firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
257 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
258 memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
259 ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
260 be16_to_cpu(rec->len));
267 release_firmware(fw);
271 /* Set up for getting frames. */
272 static int sd_start(struct gspca_dev *gspca_dev)
274 struct sd *sd = (struct sd *)gspca_dev;
277 ret = vicam_set_camera_power(gspca_dev, 1);
281 /* Start the workqueue function to do the streaming */
282 sd->work_thread = create_singlethread_workqueue(MODULE_NAME);
283 queue_work(sd->work_thread, &sd->work_struct);
288 /* called on streamoff with alt==0 and on disconnect */
289 /* the usb_lock is held at entry - restore on exit */
290 static void sd_stop0(struct gspca_dev *gspca_dev)
292 struct sd *dev = (struct sd *)gspca_dev;
294 /* wait for the work queue to terminate */
295 mutex_unlock(&gspca_dev->usb_lock);
296 /* This waits for vicam_dostream to finish */
297 destroy_workqueue(dev->work_thread);
298 dev->work_thread = NULL;
299 mutex_lock(&gspca_dev->usb_lock);
301 if (gspca_dev->present)
302 vicam_set_camera_power(gspca_dev, 0);
305 static int sd_init_controls(struct gspca_dev *gspca_dev)
307 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
309 gspca_dev->vdev.ctrl_handler = hdl;
310 v4l2_ctrl_handler_init(hdl, 2);
311 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
312 V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
313 gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
314 V4L2_CID_GAIN, 0, 255, 1, 200);
317 pr_err("Could not initialize controls\n");
323 /* Table of supported USB devices */
324 static const struct usb_device_id device_table[] = {
325 {USB_DEVICE(0x04c1, 0x009d)},
326 {USB_DEVICE(0x0602, 0x1001)},
330 MODULE_DEVICE_TABLE(usb, device_table);
332 /* sub-driver description */
333 static const struct sd_desc sd_desc = {
337 .init_controls = sd_init_controls,
342 /* -- device connect -- */
343 static int sd_probe(struct usb_interface *intf,
344 const struct usb_device_id *id)
346 return gspca_dev_probe(intf, id,
352 static struct usb_driver sd_driver = {
354 .id_table = device_table,
356 .disconnect = gspca_disconnect,
358 .suspend = gspca_suspend,
359 .resume = gspca_resume,
360 .reset_resume = gspca_resume,
364 module_usb_driver(sd_driver);