1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * gspca ViCam subdriver
5 * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
7 * Based on the usbvideo vicam driver, which is:
9 * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
10 * Chris Cheney (chris.cheney@gmail.com),
11 * Pavel Machek (pavel@ucw.cz),
12 * John Tyner (jtyner@cs.ucr.edu),
13 * Monroe Williams (monroe@pobox.com)
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #define MODULE_NAME "vicam"
19 #define HEADER_SIZE 64
21 #include <linux/workqueue.h>
22 #include <linux/slab.h>
23 #include <linux/firmware.h>
24 #include <linux/ihex.h>
27 #define VICAM_FIRMWARE "vicam/firmware.fw"
29 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
30 MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
31 MODULE_LICENSE("GPL");
32 MODULE_FIRMWARE(VICAM_FIRMWARE
);
35 struct gspca_dev gspca_dev
; /* !! must be the first item */
36 struct work_struct work_struct
;
39 /* The vicam sensor has a resolution of 512 x 244, with I believe square
40 pixels, but this is forced to a 4:3 ratio by optics. So it has
41 non square pixels :( */
42 static struct v4l2_pix_format vicam_mode
[] = {
43 { 256, 122, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
45 .sizeimage
= 256 * 122,
46 .colorspace
= V4L2_COLORSPACE_SRGB
,},
47 /* 2 modes with somewhat more square pixels */
48 { 256, 200, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
50 .sizeimage
= 256 * 200,
51 .colorspace
= V4L2_COLORSPACE_SRGB
,},
52 { 256, 240, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
54 .sizeimage
= 256 * 240,
55 .colorspace
= V4L2_COLORSPACE_SRGB
,},
56 #if 0 /* This mode has extremely non square pixels, testing use only */
57 { 512, 122, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
59 .sizeimage
= 512 * 122,
60 .colorspace
= V4L2_COLORSPACE_SRGB
,},
62 { 512, 244, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
64 .sizeimage
= 512 * 244,
65 .colorspace
= V4L2_COLORSPACE_SRGB
,},
68 static int vicam_control_msg(struct gspca_dev
*gspca_dev
, u8 request
,
69 u16 value
, u16 index
, u8
*data
, u16 len
)
73 ret
= usb_control_msg(gspca_dev
->dev
,
74 usb_sndctrlpipe(gspca_dev
->dev
, 0),
76 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
77 value
, index
, data
, len
, 1000);
79 pr_err("control msg req %02X error %d\n", request
, ret
);
84 static int vicam_set_camera_power(struct gspca_dev
*gspca_dev
, int state
)
88 ret
= vicam_control_msg(gspca_dev
, 0x50, state
, 0, NULL
, 0);
93 ret
= vicam_control_msg(gspca_dev
, 0x55, 1, 0, NULL
, 0);
99 * request and read a block of data
101 static int vicam_read_frame(struct gspca_dev
*gspca_dev
, u8
*data
, int size
)
103 int ret
, unscaled_height
, act_len
= 0;
104 u8
*req_data
= gspca_dev
->usb_buf
;
105 s32 expo
= v4l2_ctrl_g_ctrl(gspca_dev
->exposure
);
106 s32 gain
= v4l2_ctrl_g_ctrl(gspca_dev
->gain
);
108 memset(req_data
, 0, 16);
110 if (gspca_dev
->pixfmt
.width
== 256)
111 req_data
[1] |= 0x01; /* low nibble x-scale */
112 if (gspca_dev
->pixfmt
.height
<= 122) {
113 req_data
[1] |= 0x10; /* high nibble y-scale */
114 unscaled_height
= gspca_dev
->pixfmt
.height
* 2;
116 unscaled_height
= gspca_dev
->pixfmt
.height
;
117 req_data
[2] = 0x90; /* unknown, does not seem to do anything */
118 if (unscaled_height
<= 200)
119 req_data
[3] = 0x06; /* vend? */
120 else if (unscaled_height
<= 242) /* Yes 242 not 240 */
121 req_data
[3] = 0x07; /* vend? */
122 else /* Up to 244 lines with req_data[3] == 0x08 */
123 req_data
[3] = 0x08; /* vend? */
126 /* Frame rate maxed out, use partial frame expo time */
127 req_data
[4] = 255 - expo
;
132 /* Modify frame rate */
135 req_data
[6] = expo
& 0xFF;
136 req_data
[7] = expo
>> 8;
138 req_data
[8] = ((244 - unscaled_height
) / 2) & ~0x01; /* vstart */
139 /* bytes 9-15 do not seem to affect exposure or image quality */
141 mutex_lock(&gspca_dev
->usb_lock
);
142 ret
= vicam_control_msg(gspca_dev
, 0x51, 0x80, 0, req_data
, 16);
143 mutex_unlock(&gspca_dev
->usb_lock
);
147 ret
= usb_bulk_msg(gspca_dev
->dev
,
148 usb_rcvbulkpipe(gspca_dev
->dev
, 0x81),
149 data
, size
, &act_len
, 10000);
150 /* successful, it returns 0, otherwise negative */
151 if (ret
< 0 || act_len
!= size
) {
152 pr_err("bulk read fail (%d) len %d/%d\n",
160 * This function is called as a workqueue function and runs whenever the camera
161 * is streaming data. Because it is a workqueue function it is allowed to sleep
162 * so we can use synchronous USB calls. To avoid possible collisions with other
163 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
164 * performing USB operations using it. In practice we don't really need this
165 * as the cameras controls are only written from the workqueue.
167 static void vicam_dostream(struct work_struct
*work
)
169 struct sd
*sd
= container_of(work
, struct sd
, work_struct
);
170 struct gspca_dev
*gspca_dev
= &sd
->gspca_dev
;
174 frame_sz
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].sizeimage
+
176 buffer
= kmalloc(frame_sz
, GFP_KERNEL
);
178 pr_err("Couldn't allocate USB buffer\n");
182 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
184 if (gspca_dev
->frozen
)
187 ret
= vicam_read_frame(gspca_dev
, buffer
, frame_sz
);
191 /* Note the frame header contents seem to be completely
192 constant, they do not change with either image, or
193 settings. So we simply discard it. The frames have
194 a very similar 64 byte footer, which we don't even
195 bother reading from the cam */
196 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
197 buffer
+ HEADER_SIZE
,
198 frame_sz
- HEADER_SIZE
);
199 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
205 /* This function is called at probe time just before sd_init */
206 static int sd_config(struct gspca_dev
*gspca_dev
,
207 const struct usb_device_id
*id
)
209 struct cam
*cam
= &gspca_dev
->cam
;
210 struct sd
*sd
= (struct sd
*)gspca_dev
;
212 /* We don't use the buffer gspca allocates so make it small. */
215 cam
->cam_mode
= vicam_mode
;
216 cam
->nmodes
= ARRAY_SIZE(vicam_mode
);
218 INIT_WORK(&sd
->work_struct
, vicam_dostream
);
223 /* this function is called at probe and resume time */
224 static int sd_init(struct gspca_dev
*gspca_dev
)
227 const struct ihex_binrec
*rec
;
228 const struct firmware
*fw
;
231 ret
= request_ihex_firmware(&fw
, VICAM_FIRMWARE
,
232 &gspca_dev
->dev
->dev
);
234 pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret
);
238 firmware_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
243 for (rec
= (void *)fw
->data
; rec
; rec
= ihex_next_binrec(rec
)) {
244 memcpy(firmware_buf
, rec
->data
, be16_to_cpu(rec
->len
));
245 ret
= vicam_control_msg(gspca_dev
, 0xff, 0, 0, firmware_buf
,
246 be16_to_cpu(rec
->len
));
253 release_firmware(fw
);
257 /* Set up for getting frames. */
258 static int sd_start(struct gspca_dev
*gspca_dev
)
260 struct sd
*sd
= (struct sd
*)gspca_dev
;
263 ret
= vicam_set_camera_power(gspca_dev
, 1);
267 schedule_work(&sd
->work_struct
);
272 /* called on streamoff with alt==0 and on disconnect */
273 /* the usb_lock is held at entry - restore on exit */
274 static void sd_stop0(struct gspca_dev
*gspca_dev
)
276 struct sd
*dev
= (struct sd
*)gspca_dev
;
278 /* wait for the work queue to terminate */
279 mutex_unlock(&gspca_dev
->usb_lock
);
280 /* This waits for vicam_dostream to finish */
281 flush_work(&dev
->work_struct
);
282 mutex_lock(&gspca_dev
->usb_lock
);
284 if (gspca_dev
->present
)
285 vicam_set_camera_power(gspca_dev
, 0);
288 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
290 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
292 gspca_dev
->vdev
.ctrl_handler
= hdl
;
293 v4l2_ctrl_handler_init(hdl
, 2);
294 gspca_dev
->exposure
= v4l2_ctrl_new_std(hdl
, NULL
,
295 V4L2_CID_EXPOSURE
, 0, 2047, 1, 256);
296 gspca_dev
->gain
= v4l2_ctrl_new_std(hdl
, NULL
,
297 V4L2_CID_GAIN
, 0, 255, 1, 200);
300 pr_err("Could not initialize controls\n");
306 /* Table of supported USB devices */
307 static const struct usb_device_id device_table
[] = {
308 {USB_DEVICE(0x04c1, 0x009d)},
309 {USB_DEVICE(0x0602, 0x1001)},
313 MODULE_DEVICE_TABLE(usb
, device_table
);
315 /* sub-driver description */
316 static const struct sd_desc sd_desc
= {
320 .init_controls
= sd_init_controls
,
325 /* -- device connect -- */
326 static int sd_probe(struct usb_interface
*intf
,
327 const struct usb_device_id
*id
)
329 return gspca_dev_probe(intf
, id
,
335 static struct usb_driver sd_driver
= {
337 .id_table
= device_table
,
339 .disconnect
= gspca_disconnect
,
341 .suspend
= gspca_suspend
,
342 .resume
= gspca_resume
,
343 .reset_resume
= gspca_resume
,
347 module_usb_driver(sd_driver
);