blob: 179b2ec3df57c97c8072ee3e3f353889171b485f [file] [log] [blame]
Thomas Gleixnerfd9871f2019-05-19 15:51:54 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans de Goede49b61ec2011-02-21 11:06:29 -03002/*
3 * gspca ViCam subdriver
4 *
5 * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
6 *
7 * Based on the usbvideo vicam driver, which is:
8 *
9 * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
Chris Cheneyccb7cc02013-08-04 22:37:42 -050010 * Chris Cheney (chris.cheney@gmail.com),
Hans de Goede49b61ec2011-02-21 11:06:29 -030011 * Pavel Machek (pavel@ucw.cz),
12 * John Tyner (jtyner@cs.ucr.edu),
13 * Monroe Williams (monroe@pobox.com)
Hans de Goede49b61ec2011-02-21 11:06:29 -030014 */
15
Joe Perches133a9fe2011-08-21 19:56:57 -030016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Hans de Goede49b61ec2011-02-21 11:06:29 -030018#define MODULE_NAME "vicam"
19#define HEADER_SIZE 64
20
21#include <linux/workqueue.h>
22#include <linux/slab.h>
23#include <linux/firmware.h>
24#include <linux/ihex.h>
25#include "gspca.h"
26
Tim Gardnerd74185b2012-04-12 17:23:21 -030027#define VICAM_FIRMWARE "vicam/firmware.fw"
28
Hans de Goede49b61ec2011-02-21 11:06:29 -030029MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
30MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
31MODULE_LICENSE("GPL");
Tim Gardnerd74185b2012-04-12 17:23:21 -030032MODULE_FIRMWARE(VICAM_FIRMWARE);
Hans de Goede49b61ec2011-02-21 11:06:29 -030033
Hans de Goede49b61ec2011-02-21 11:06:29 -030034struct sd {
35 struct gspca_dev gspca_dev; /* !! must be the first item */
36 struct work_struct work_struct;
Hans de Goede49b61ec2011-02-21 11:06:29 -030037};
38
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 :( */
42static struct v4l2_pix_format vicam_mode[] = {
43 { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
44 .bytesperline = 256,
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,
49 .bytesperline = 256,
50 .sizeimage = 256 * 200,
51 .colorspace = V4L2_COLORSPACE_SRGB,},
52 { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
53 .bytesperline = 256,
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,
58 .bytesperline = 512,
59 .sizeimage = 512 * 122,
60 .colorspace = V4L2_COLORSPACE_SRGB,},
61#endif
62 { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
63 .bytesperline = 512,
64 .sizeimage = 512 * 244,
65 .colorspace = V4L2_COLORSPACE_SRGB,},
66};
67
Hans de Goede49b61ec2011-02-21 11:06:29 -030068static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
69 u16 value, u16 index, u8 *data, u16 len)
70{
71 int ret;
72
73 ret = usb_control_msg(gspca_dev->dev,
74 usb_sndctrlpipe(gspca_dev->dev, 0),
75 request,
76 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
77 value, index, data, len, 1000);
78 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -030079 pr_err("control msg req %02X error %d\n", request, ret);
Hans de Goede49b61ec2011-02-21 11:06:29 -030080
81 return ret;
82}
83
84static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
85{
86 int ret;
87
88 ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
89 if (ret < 0)
90 return ret;
91
92 if (state)
93 ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
94
95 return ret;
96}
97
98/*
Hans de Goede844db452012-09-09 07:30:02 -030099 * request and read a block of data
Hans de Goede49b61ec2011-02-21 11:06:29 -0300100 */
101static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
102{
Hans de Goede49b61ec2011-02-21 11:06:29 -0300103 int ret, unscaled_height, act_len = 0;
104 u8 *req_data = gspca_dev->usb_buf;
Hans Verkuil4910adf2012-05-18 05:22:38 -0300105 s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
106 s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300107
108 memset(req_data, 0, 16);
Hans Verkuil4910adf2012-05-18 05:22:38 -0300109 req_data[0] = gain;
Ondrej Zary1966bc22013-08-30 17:54:23 -0300110 if (gspca_dev->pixfmt.width == 256)
Hans de Goede49b61ec2011-02-21 11:06:29 -0300111 req_data[1] |= 0x01; /* low nibble x-scale */
Ondrej Zary1966bc22013-08-30 17:54:23 -0300112 if (gspca_dev->pixfmt.height <= 122) {
Hans de Goede49b61ec2011-02-21 11:06:29 -0300113 req_data[1] |= 0x10; /* high nibble y-scale */
Ondrej Zary1966bc22013-08-30 17:54:23 -0300114 unscaled_height = gspca_dev->pixfmt.height * 2;
Hans de Goede49b61ec2011-02-21 11:06:29 -0300115 } else
Ondrej Zary1966bc22013-08-30 17:54:23 -0300116 unscaled_height = gspca_dev->pixfmt.height;
Hans de Goede49b61ec2011-02-21 11:06:29 -0300117 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? */
124
Hans Verkuil4910adf2012-05-18 05:22:38 -0300125 if (expo < 256) {
Hans de Goede49b61ec2011-02-21 11:06:29 -0300126 /* Frame rate maxed out, use partial frame expo time */
Hans Verkuil4910adf2012-05-18 05:22:38 -0300127 req_data[4] = 255 - expo;
Hans de Goede49b61ec2011-02-21 11:06:29 -0300128 req_data[5] = 0x00;
129 req_data[6] = 0x00;
130 req_data[7] = 0x01;
131 } else {
132 /* Modify frame rate */
133 req_data[4] = 0x00;
134 req_data[5] = 0x00;
Hans Verkuil4910adf2012-05-18 05:22:38 -0300135 req_data[6] = expo & 0xFF;
136 req_data[7] = expo >> 8;
Hans de Goede49b61ec2011-02-21 11:06:29 -0300137 }
138 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
139 /* bytes 9-15 do not seem to affect exposure or image quality */
140
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);
144 if (ret < 0)
145 return ret;
146
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) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300152 pr_err("bulk read fail (%d) len %d/%d\n",
153 ret, act_len, size);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300154 return -EIO;
155 }
156 return 0;
157}
158
Hans de Goede844db452012-09-09 07:30:02 -0300159/*
160 * This function is called as a workqueue function and runs whenever the camera
Hans de Goede49b61ec2011-02-21 11:06:29 -0300161 * 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
Hans de Goede844db452012-09-09 07:30:02 -0300163 * 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.
Hans de Goede49b61ec2011-02-21 11:06:29 -0300166 */
167static void vicam_dostream(struct work_struct *work)
168{
169 struct sd *sd = container_of(work, struct sd, work_struct);
170 struct gspca_dev *gspca_dev = &sd->gspca_dev;
171 int ret, frame_sz;
172 u8 *buffer;
173
174 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
175 HEADER_SIZE;
Hans de Goede5334b342018-05-05 04:22:08 -0400176 buffer = kmalloc(frame_sz, GFP_KERNEL);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300177 if (!buffer) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300178 pr_err("Couldn't allocate USB buffer\n");
Hans de Goede49b61ec2011-02-21 11:06:29 -0300179 goto exit;
180 }
181
Hans de Goede345321d2012-09-09 06:30:02 -0300182 while (gspca_dev->present && gspca_dev->streaming) {
Hans Verkuil4ad34da2012-05-18 08:40:42 -0300183#ifdef CONFIG_PM
184 if (gspca_dev->frozen)
185 break;
186#endif
Hans de Goede49b61ec2011-02-21 11:06:29 -0300187 ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
188 if (ret < 0)
189 break;
190
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);
200 }
201exit:
202 kfree(buffer);
203}
204
205/* This function is called at probe time just before sd_init */
206static int sd_config(struct gspca_dev *gspca_dev,
207 const struct usb_device_id *id)
208{
209 struct cam *cam = &gspca_dev->cam;
210 struct sd *sd = (struct sd *)gspca_dev;
211
212 /* We don't use the buffer gspca allocates so make it small. */
213 cam->bulk = 1;
214 cam->bulk_size = 64;
215 cam->cam_mode = vicam_mode;
216 cam->nmodes = ARRAY_SIZE(vicam_mode);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300217
218 INIT_WORK(&sd->work_struct, vicam_dostream);
219
220 return 0;
221}
222
223/* this function is called at probe and resume time */
224static int sd_init(struct gspca_dev *gspca_dev)
225{
226 int ret;
227 const struct ihex_binrec *rec;
228 const struct firmware *uninitialized_var(fw);
229 u8 *firmware_buf;
230
Tim Gardnerd74185b2012-04-12 17:23:21 -0300231 ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
Hans de Goede49b61ec2011-02-21 11:06:29 -0300232 &gspca_dev->dev->dev);
233 if (ret) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300234 pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300235 return ret;
236 }
237
238 firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
239 if (!firmware_buf) {
240 ret = -ENOMEM;
241 goto exit;
242 }
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));
247 if (ret < 0)
248 break;
249 }
250
251 kfree(firmware_buf);
252exit:
253 release_firmware(fw);
254 return ret;
255}
256
257/* Set up for getting frames. */
258static int sd_start(struct gspca_dev *gspca_dev)
259{
260 struct sd *sd = (struct sd *)gspca_dev;
261 int ret;
262
263 ret = vicam_set_camera_power(gspca_dev, 1);
264 if (ret < 0)
265 return ret;
266
Bhaktipriya Shridhar95705082016-07-16 05:52:19 -0300267 schedule_work(&sd->work_struct);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300268
269 return 0;
270}
271
272/* called on streamoff with alt==0 and on disconnect */
273/* the usb_lock is held at entry - restore on exit */
274static void sd_stop0(struct gspca_dev *gspca_dev)
275{
276 struct sd *dev = (struct sd *)gspca_dev;
277
278 /* wait for the work queue to terminate */
279 mutex_unlock(&gspca_dev->usb_lock);
280 /* This waits for vicam_dostream to finish */
Bhaktipriya Shridhar95705082016-07-16 05:52:19 -0300281 flush_work(&dev->work_struct);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300282 mutex_lock(&gspca_dev->usb_lock);
283
Hans de Goede345321d2012-09-09 06:30:02 -0300284 if (gspca_dev->present)
Hans de Goede27d3e362011-12-29 16:50:57 -0300285 vicam_set_camera_power(gspca_dev, 0);
Hans de Goede49b61ec2011-02-21 11:06:29 -0300286}
287
Hans Verkuil4910adf2012-05-18 05:22:38 -0300288static int sd_init_controls(struct gspca_dev *gspca_dev)
289{
290 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
291
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);
298
299 if (hdl->error) {
300 pr_err("Could not initialize controls\n");
301 return hdl->error;
302 }
303 return 0;
304}
305
Hans de Goede49b61ec2011-02-21 11:06:29 -0300306/* Table of supported USB devices */
307static const struct usb_device_id device_table[] = {
308 {USB_DEVICE(0x04c1, 0x009d)},
309 {USB_DEVICE(0x0602, 0x1001)},
310 {}
311};
312
313MODULE_DEVICE_TABLE(usb, device_table);
314
315/* sub-driver description */
316static const struct sd_desc sd_desc = {
317 .name = MODULE_NAME,
Hans de Goede49b61ec2011-02-21 11:06:29 -0300318 .config = sd_config,
319 .init = sd_init,
Hans Verkuil4910adf2012-05-18 05:22:38 -0300320 .init_controls = sd_init_controls,
Hans de Goede49b61ec2011-02-21 11:06:29 -0300321 .start = sd_start,
322 .stop0 = sd_stop0,
323};
324
325/* -- device connect -- */
326static int sd_probe(struct usb_interface *intf,
327 const struct usb_device_id *id)
328{
329 return gspca_dev_probe(intf, id,
330 &sd_desc,
331 sizeof(struct sd),
332 THIS_MODULE);
333}
334
335static struct usb_driver sd_driver = {
336 .name = MODULE_NAME,
337 .id_table = device_table,
338 .probe = sd_probe,
339 .disconnect = gspca_disconnect,
340#ifdef CONFIG_PM
341 .suspend = gspca_suspend,
342 .resume = gspca_resume,
Hans de Goede8bb58962012-06-30 06:44:47 -0300343 .reset_resume = gspca_resume,
Hans de Goede49b61ec2011-02-21 11:06:29 -0300344#endif
345};
346
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800347module_usb_driver(sd_driver);