1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * STV0680 USB Camera Driver
4  *
5  * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
6  *
7  * This module is adapted from the in kernel v4l1 stv680 driver:
8  *
9  *  STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
10  *
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.
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #define MODULE_NAME "stv0680"
19 
20 #include "gspca.h"
21 
22 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
23 MODULE_DESCRIPTION("STV0680 USB Camera Driver");
24 MODULE_LICENSE("GPL");
25 
26 /* specific webcam descriptor */
27 struct sd {
28 	struct gspca_dev gspca_dev;		/* !! must be the first item */
29 	struct v4l2_pix_format mode;
30 	u8 orig_mode;
31 	u8 video_mode;
32 	u8 current_mode;
33 };
34 
stv_sndctrl(struct gspca_dev * gspca_dev,int set,u8 req,u16 val,int size)35 static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val,
36 		       int size)
37 {
38 	int ret;
39 	u8 req_type = 0;
40 	unsigned int pipe = 0;
41 
42 	switch (set) {
43 	case 0: /*  0xc1  */
44 		req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
45 		pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
46 		break;
47 	case 1: /*  0x41  */
48 		req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
49 		pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
50 		break;
51 	case 2:	/*  0x80  */
52 		req_type = USB_DIR_IN | USB_RECIP_DEVICE;
53 		pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
54 		break;
55 	case 3:	/*  0x40  */
56 		req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
57 		pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
58 		break;
59 	}
60 
61 	ret = usb_control_msg(gspca_dev->dev, pipe,
62 			      req, req_type,
63 			      val, 0, gspca_dev->usb_buf, size, 500);
64 
65 	if ((ret < 0) && (req != 0x0a))
66 		pr_err("usb_control_msg error %i, request = 0x%x, error = %i\n",
67 		       set, req, ret);
68 
69 	return ret;
70 }
71 
stv0680_handle_error(struct gspca_dev * gspca_dev,int ret)72 static int stv0680_handle_error(struct gspca_dev *gspca_dev, int ret)
73 {
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]);
77 	return ret;
78 }
79 
stv0680_get_video_mode(struct gspca_dev * gspca_dev)80 static int stv0680_get_video_mode(struct gspca_dev *gspca_dev)
81 {
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;
85 
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);
89 	}
90 
91 	return gspca_dev->usb_buf[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
92 }
93 
stv0680_set_video_mode(struct gspca_dev * gspca_dev,u8 mode)94 static int stv0680_set_video_mode(struct gspca_dev *gspca_dev, u8 mode)
95 {
96 	struct sd *sd = (struct sd *) gspca_dev;
97 
98 	if (sd->current_mode == mode)
99 		return 0;
100 
101 	memset(gspca_dev->usb_buf, 0, 8);
102 	gspca_dev->usb_buf[0] = mode;
103 
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);
107 	}
108 
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");
112 		return -EIO;
113 	}
114 
115 	sd->current_mode = mode;
116 
117 	return 0;
118 }
119 
120 /* this function is called at probe time */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)121 static int sd_config(struct gspca_dev *gspca_dev,
122 			const struct usb_device_id *id)
123 {
124 	int ret;
125 	struct sd *sd = (struct sd *) gspca_dev;
126 	struct cam *cam = &gspca_dev->cam;
127 
128 	/* Give the camera some time to settle, otherwise initialization will
129 	   fail on hotplug, and yes it really needs a full second. */
130 	msleep(1000);
131 
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);
137 	}
138 
139 	/* get camera descriptor */
140 	if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x09) != 0x09)
141 		return stv0680_handle_error(gspca_dev, -ENODEV);
142 
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);
147 	}
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);
154 
155 	if (!(gspca_dev->usb_buf[7] & 0x09)) {
156 		gspca_err(gspca_dev, "Camera supports neither CIF nor QVGA mode\n");
157 		return -ENODEV;
158 	}
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");
167 
168 	if (gspca_dev->usb_buf[7] & 0x01)
169 		sd->video_mode = 0x00; /* CIF */
170 	else
171 		sd->video_mode = 0x03; /* QVGA */
172 
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));
180 
181 
182 	ret = stv0680_get_video_mode(gspca_dev);
183 	if (ret < 0)
184 		return ret;
185 	sd->current_mode = sd->orig_mode = ret;
186 
187 	ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
188 	if (ret < 0)
189 		return ret;
190 
191 	/* Get mode details */
192 	if (stv_sndctrl(gspca_dev, 0, 0x8f, 0, 0x10) != 0x10)
193 		return stv0680_handle_error(gspca_dev, -EIO);
194 
195 	cam->bulk = 1;
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;
210 
211 	/* origGain = gspca_dev->usb_buf[12]; */
212 
213 	cam->cam_mode = &sd->mode;
214 	cam->nmodes = 1;
215 
216 
217 	ret = stv0680_set_video_mode(gspca_dev, sd->orig_mode);
218 	if (ret < 0)
219 		return ret;
220 
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);
225 	}
226 
227 	return 0;
228 }
229 
230 /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)231 static int sd_init(struct gspca_dev *gspca_dev)
232 {
233 	return 0;
234 }
235 
236 /* -- start the camera -- */
sd_start(struct gspca_dev * gspca_dev)237 static int sd_start(struct gspca_dev *gspca_dev)
238 {
239 	int ret;
240 	struct sd *sd = (struct sd *) gspca_dev;
241 
242 	ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
243 	if (ret < 0)
244 		return ret;
245 
246 	if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
247 		return stv0680_handle_error(gspca_dev, -EIO);
248 
249 	/* Start stream at:
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);
255 
256 	return 0;
257 }
258 
sd_stopN(struct gspca_dev * gspca_dev)259 static void sd_stopN(struct gspca_dev *gspca_dev)
260 {
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);
264 }
265 
sd_stop0(struct gspca_dev * gspca_dev)266 static void sd_stop0(struct gspca_dev *gspca_dev)
267 {
268 	struct sd *sd = (struct sd *) gspca_dev;
269 
270 	if (!sd->gspca_dev.present)
271 		return;
272 
273 	stv0680_set_video_mode(gspca_dev, sd->orig_mode);
274 }
275 
sd_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)276 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
277 			u8 *data,
278 			int len)
279 {
280 	struct sd *sd = (struct sd *) gspca_dev;
281 
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,
285 	   so discard it. */
286 	if (len != sd->mode.sizeimage) {
287 		gspca_dev->last_packet_type = DISCARD_PACKET;
288 		return;
289 	}
290 
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);
295 
296 	/* Store the just received frame */
297 	gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
298 }
299 
300 /* sub-driver description */
301 static const struct sd_desc sd_desc = {
302 	.name = MODULE_NAME,
303 	.config = sd_config,
304 	.init = sd_init,
305 	.start = sd_start,
306 	.stopN = sd_stopN,
307 	.stop0 = sd_stop0,
308 	.pkt_scan = sd_pkt_scan,
309 };
310 
311 /* -- module initialisation -- */
312 static const struct usb_device_id device_table[] = {
313 	{USB_DEVICE(0x0553, 0x0202)},
314 	{USB_DEVICE(0x041e, 0x4007)},
315 	{}
316 };
317 MODULE_DEVICE_TABLE(usb, device_table);
318 
319 /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)320 static int sd_probe(struct usb_interface *intf,
321 			const struct usb_device_id *id)
322 {
323 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
324 				THIS_MODULE);
325 }
326 
327 static struct usb_driver sd_driver = {
328 	.name = MODULE_NAME,
329 	.id_table = device_table,
330 	.probe = sd_probe,
331 	.disconnect = gspca_disconnect,
332 #ifdef CONFIG_PM
333 	.suspend = gspca_suspend,
334 	.resume = gspca_resume,
335 	.reset_resume = gspca_resume,
336 #endif
337 };
338 
339 module_usb_driver(sd_driver);
340