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.
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"
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
);
48 struct gspca_dev gspca_dev
; /* !! must be the first item */
49 struct work_struct work_struct
;
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
,
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
,
63 .sizeimage
= 256 * 200,
64 .colorspace
= V4L2_COLORSPACE_SRGB
,},
65 { 256, 240, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
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
,
72 .sizeimage
= 512 * 122,
73 .colorspace
= V4L2_COLORSPACE_SRGB
,},
75 { 512, 244, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
77 .sizeimage
= 512 * 244,
78 .colorspace
= V4L2_COLORSPACE_SRGB
,},
81 static int vicam_control_msg(struct gspca_dev
*gspca_dev
, u8 request
,
82 u16 value
, u16 index
, u8
*data
, u16 len
)
86 ret
= usb_control_msg(gspca_dev
->dev
,
87 usb_sndctrlpipe(gspca_dev
->dev
, 0),
89 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
90 value
, index
, data
, len
, 1000);
92 pr_err("control msg req %02X error %d\n", request
, ret
);
97 static int vicam_set_camera_power(struct gspca_dev
*gspca_dev
, int state
)
101 ret
= vicam_control_msg(gspca_dev
, 0x50, state
, 0, NULL
, 0);
106 ret
= vicam_control_msg(gspca_dev
, 0x55, 1, 0, NULL
, 0);
112 * request and read a block of data
114 static int vicam_read_frame(struct gspca_dev
*gspca_dev
, u8
*data
, int size
)
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
);
121 memset(req_data
, 0, 16);
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;
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? */
139 /* Frame rate maxed out, use partial frame expo time */
140 req_data
[4] = 255 - expo
;
145 /* Modify frame rate */
148 req_data
[6] = expo
& 0xFF;
149 req_data
[7] = expo
>> 8;
151 req_data
[8] = ((244 - unscaled_height
) / 2) & ~0x01; /* vstart */
152 /* bytes 9-15 do not seem to affect exposure or image quality */
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
);
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",
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.
180 static void vicam_dostream(struct work_struct
*work
)
182 struct sd
*sd
= container_of(work
, struct sd
, work_struct
);
183 struct gspca_dev
*gspca_dev
= &sd
->gspca_dev
;
187 frame_sz
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].sizeimage
+
189 buffer
= kmalloc(frame_sz
, GFP_KERNEL
| GFP_DMA
);
191 pr_err("Couldn't allocate USB buffer\n");
195 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
197 if (gspca_dev
->frozen
)
200 ret
= vicam_read_frame(gspca_dev
, buffer
, frame_sz
);
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);
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
)
222 struct cam
*cam
= &gspca_dev
->cam
;
223 struct sd
*sd
= (struct sd
*)gspca_dev
;
225 /* We don't use the buffer gspca allocates so make it small. */
228 cam
->cam_mode
= vicam_mode
;
229 cam
->nmodes
= ARRAY_SIZE(vicam_mode
);
231 INIT_WORK(&sd
->work_struct
, vicam_dostream
);
236 /* this function is called at probe and resume time */
237 static int sd_init(struct gspca_dev
*gspca_dev
)
240 const struct ihex_binrec
*rec
;
241 const struct firmware
*uninitialized_var(fw
);
244 ret
= request_ihex_firmware(&fw
, VICAM_FIRMWARE
,
245 &gspca_dev
->dev
->dev
);
247 pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret
);
251 firmware_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
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
));
266 release_firmware(fw
);
270 /* Set up for getting frames. */
271 static int sd_start(struct gspca_dev
*gspca_dev
)
273 struct sd
*sd
= (struct sd
*)gspca_dev
;
276 ret
= vicam_set_camera_power(gspca_dev
, 1);
280 schedule_work(&sd
->work_struct
);
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
)
289 struct sd
*dev
= (struct sd
*)gspca_dev
;
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
);
297 if (gspca_dev
->present
)
298 vicam_set_camera_power(gspca_dev
, 0);
301 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
303 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
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);
313 pr_err("Could not initialize controls\n");
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)},
326 MODULE_DEVICE_TABLE(usb
, device_table
);
328 /* sub-driver description */
329 static const struct sd_desc sd_desc
= {
333 .init_controls
= sd_init_controls
,
338 /* -- device connect -- */
339 static int sd_probe(struct usb_interface
*intf
,
340 const struct usb_device_id
*id
)
342 return gspca_dev_probe(intf
, id
,
348 static struct usb_driver sd_driver
= {
350 .id_table
= device_table
,
352 .disconnect
= gspca_disconnect
,
354 .suspend
= gspca_suspend
,
355 .resume
= gspca_resume
,
356 .reset_resume
= gspca_resume
,
360 module_usb_driver(sd_driver
);