1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STV0680 USB Camera Driver
5 * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
7 * This module is adapted from the in kernel v4l1 stv680 driver:
9 * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
11 * Thanks to STMicroelectronics for information on the usb commands, and
12 * to Steve Miller at STM for his help and encouragement while I was
13 * writing this driver.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #define MODULE_NAME "stv0680"
22 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
23 MODULE_DESCRIPTION("STV0680 USB Camera Driver");
24 MODULE_LICENSE("GPL");
26 /* specific webcam descriptor */
28 struct gspca_dev gspca_dev; /* !! must be the first item */
29 struct v4l2_pix_format mode;
35 static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val,
40 unsigned int pipe = 0;
44 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
45 pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
48 req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
49 pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
52 req_type = USB_DIR_IN | USB_RECIP_DEVICE;
53 pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
56 req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
57 pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
61 ret = usb_control_msg(gspca_dev->dev, pipe,
63 val, 0, gspca_dev->usb_buf, size, 500);
65 if ((ret < 0) && (req != 0x0a))
66 pr_err("usb_control_msg error %i, request = 0x%x, error = %i\n",
72 static int stv0680_handle_error(struct gspca_dev *gspca_dev, int ret)
74 stv_sndctrl(gspca_dev, 0, 0x80, 0, 0x02); /* Get Last Error */
75 gspca_err(gspca_dev, "last error: %i, command = 0x%x\n",
76 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
80 static int stv0680_get_video_mode(struct gspca_dev *gspca_dev)
82 /* Note not sure if this init of usb_buf is really necessary */
83 memset(gspca_dev->usb_buf, 0, 8);
84 gspca_dev->usb_buf[0] = 0x0f;
86 if (stv_sndctrl(gspca_dev, 0, 0x87, 0, 0x08) != 0x08) {
87 gspca_err(gspca_dev, "Get_Camera_Mode failed\n");
88 return stv0680_handle_error(gspca_dev, -EIO);
91 return gspca_dev->usb_buf[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
94 static int stv0680_set_video_mode(struct gspca_dev *gspca_dev, u8 mode)
96 struct sd *sd = (struct sd *) gspca_dev;
98 if (sd->current_mode == mode)
101 memset(gspca_dev->usb_buf, 0, 8);
102 gspca_dev->usb_buf[0] = mode;
104 if (stv_sndctrl(gspca_dev, 3, 0x07, 0x0100, 0x08) != 0x08) {
105 gspca_err(gspca_dev, "Set_Camera_Mode failed\n");
106 return stv0680_handle_error(gspca_dev, -EIO);
109 /* Verify we got what we've asked for */
110 if (stv0680_get_video_mode(gspca_dev) != mode) {
111 gspca_err(gspca_dev, "Error setting camera video mode!\n");
115 sd->current_mode = mode;
120 /* this function is called at probe time */
121 static int sd_config(struct gspca_dev *gspca_dev,
122 const struct usb_device_id *id)
125 struct sd *sd = (struct sd *) gspca_dev;
126 struct cam *cam = &gspca_dev->cam;
128 /* Give the camera some time to settle, otherwise initialization will
129 fail on hotplug, and yes it really needs a full second. */
132 /* ping camera to be sure STV0680 is present */
133 if (stv_sndctrl(gspca_dev, 0, 0x88, 0x5678, 0x02) != 0x02 ||
134 gspca_dev->usb_buf[0] != 0x56 || gspca_dev->usb_buf[1] != 0x78) {
135 gspca_err(gspca_dev, "STV(e): camera ping failed!!\n");
136 return stv0680_handle_error(gspca_dev, -ENODEV);
139 /* get camera descriptor */
140 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x09) != 0x09)
141 return stv0680_handle_error(gspca_dev, -ENODEV);
143 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x22) != 0x22 ||
144 gspca_dev->usb_buf[7] != 0xa0 || gspca_dev->usb_buf[8] != 0x23) {
145 gspca_err(gspca_dev, "Could not get descriptor 0200\n");
146 return stv0680_handle_error(gspca_dev, -ENODEV);
148 if (stv_sndctrl(gspca_dev, 0, 0x8a, 0, 0x02) != 0x02)
149 return stv0680_handle_error(gspca_dev, -ENODEV);
150 if (stv_sndctrl(gspca_dev, 0, 0x8b, 0, 0x24) != 0x24)
151 return stv0680_handle_error(gspca_dev, -ENODEV);
152 if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
153 return stv0680_handle_error(gspca_dev, -ENODEV);
155 if (!(gspca_dev->usb_buf[7] & 0x09)) {
156 gspca_err(gspca_dev, "Camera supports neither CIF nor QVGA mode\n");
159 if (gspca_dev->usb_buf[7] & 0x01)
160 gspca_dbg(gspca_dev, D_PROBE, "Camera supports CIF mode\n");
161 if (gspca_dev->usb_buf[7] & 0x02)
162 gspca_dbg(gspca_dev, D_PROBE, "Camera supports VGA mode\n");
163 if (gspca_dev->usb_buf[7] & 0x04)
164 gspca_dbg(gspca_dev, D_PROBE, "Camera supports QCIF mode\n");
165 if (gspca_dev->usb_buf[7] & 0x08)
166 gspca_dbg(gspca_dev, D_PROBE, "Camera supports QVGA mode\n");
168 if (gspca_dev->usb_buf[7] & 0x01)
169 sd->video_mode = 0x00; /* CIF */
171 sd->video_mode = 0x03; /* QVGA */
173 /* FW rev, ASIC rev, sensor ID */
174 gspca_dbg(gspca_dev, D_PROBE, "Firmware rev is %i.%i\n",
175 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
176 gspca_dbg(gspca_dev, D_PROBE, "ASIC rev is %i.%i",
177 gspca_dev->usb_buf[2], gspca_dev->usb_buf[3]);
178 gspca_dbg(gspca_dev, D_PROBE, "Sensor ID is %i",
179 (gspca_dev->usb_buf[4]*16) + (gspca_dev->usb_buf[5]>>4));
182 ret = stv0680_get_video_mode(gspca_dev);
185 sd->current_mode = sd->orig_mode = ret;
187 ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
191 /* Get mode details */
192 if (stv_sndctrl(gspca_dev, 0, 0x8f, 0, 0x10) != 0x10)
193 return stv0680_handle_error(gspca_dev, -EIO);
196 cam->bulk_nurbs = 1; /* The cam cannot handle more */
197 cam->bulk_size = (gspca_dev->usb_buf[0] << 24) |
198 (gspca_dev->usb_buf[1] << 16) |
199 (gspca_dev->usb_buf[2] << 8) |
200 (gspca_dev->usb_buf[3]);
201 sd->mode.width = (gspca_dev->usb_buf[4] << 8) |
202 (gspca_dev->usb_buf[5]); /* 322, 356, 644 */
203 sd->mode.height = (gspca_dev->usb_buf[6] << 8) |
204 (gspca_dev->usb_buf[7]); /* 242, 292, 484 */
205 sd->mode.pixelformat = V4L2_PIX_FMT_STV0680;
206 sd->mode.field = V4L2_FIELD_NONE;
207 sd->mode.bytesperline = sd->mode.width;
208 sd->mode.sizeimage = cam->bulk_size;
209 sd->mode.colorspace = V4L2_COLORSPACE_SRGB;
211 /* origGain = gspca_dev->usb_buf[12]; */
213 cam->cam_mode = &sd->mode;
217 ret = stv0680_set_video_mode(gspca_dev, sd->orig_mode);
221 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0100, 0x12) != 0x12 ||
222 gspca_dev->usb_buf[8] != 0x53 || gspca_dev->usb_buf[9] != 0x05) {
223 pr_err("Could not get descriptor 0100\n");
224 return stv0680_handle_error(gspca_dev, -EIO);
230 /* this function is called at probe and resume time */
231 static int sd_init(struct gspca_dev *gspca_dev)
236 /* -- start the camera -- */
237 static int sd_start(struct gspca_dev *gspca_dev)
240 struct sd *sd = (struct sd *) gspca_dev;
242 ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
246 if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
247 return stv0680_handle_error(gspca_dev, -EIO);
250 0x0000 = CIF (352x288)
251 0x0100 = VGA (640x480)
252 0x0300 = QVGA (320x240) */
253 if (stv_sndctrl(gspca_dev, 1, 0x09, sd->video_mode << 8, 0x0) != 0x0)
254 return stv0680_handle_error(gspca_dev, -EIO);
259 static void sd_stopN(struct gspca_dev *gspca_dev)
261 /* This is a high priority command; it stops all lower order cmds */
262 if (stv_sndctrl(gspca_dev, 1, 0x04, 0x0000, 0x0) != 0x0)
263 stv0680_handle_error(gspca_dev, -EIO);
266 static void sd_stop0(struct gspca_dev *gspca_dev)
268 struct sd *sd = (struct sd *) gspca_dev;
270 if (!sd->gspca_dev.present)
273 stv0680_set_video_mode(gspca_dev, sd->orig_mode);
276 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
280 struct sd *sd = (struct sd *) gspca_dev;
282 /* Every now and then the camera sends a 16 byte packet, no idea
283 what it contains, but it is not image data, when this
284 happens the frame received before this packet is corrupt,
286 if (len != sd->mode.sizeimage) {
287 gspca_dev->last_packet_type = DISCARD_PACKET;
291 /* Finish the previous frame, we do this upon reception of the next
292 packet, even though it is already complete so that the strange 16
293 byte packets send after a corrupt frame can discard it. */
294 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
296 /* Store the just received frame */
297 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
300 /* sub-driver description */
301 static const struct sd_desc sd_desc = {
308 .pkt_scan = sd_pkt_scan,
311 /* -- module initialisation -- */
312 static const struct usb_device_id device_table[] = {
313 {USB_DEVICE(0x0553, 0x0202)},
314 {USB_DEVICE(0x041e, 0x4007)},
317 MODULE_DEVICE_TABLE(usb, device_table);
319 /* -- device connect -- */
320 static int sd_probe(struct usb_interface *intf,
321 const struct usb_device_id *id)
323 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
327 static struct usb_driver sd_driver = {
329 .id_table = device_table,
331 .disconnect = gspca_disconnect,
333 .suspend = gspca_suspend,
334 .resume = gspca_resume,
335 .reset_resume = gspca_resume,
339 module_usb_driver(sd_driver);