xref: /openbmc/linux/drivers/media/usb/gspca/spca1528.c (revision d3964221)
1 /*
2  * spca1528 subdriver
3  *
4  * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #define MODULE_NAME "spca1528"
20 
21 #include "gspca.h"
22 #include "jpeg.h"
23 
24 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
25 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
26 MODULE_LICENSE("GPL");
27 
28 /* specific webcam descriptor */
29 struct sd {
30 	struct gspca_dev gspca_dev;	/* !! must be the first item */
31 
32 	u8 pkt_seq;
33 
34 	u8 jpeg_hdr[JPEG_HDR_SZ];
35 };
36 
37 static const struct v4l2_pix_format vga_mode[] = {
38 /*		(does not work correctly)
39 	{176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
40 		.bytesperline = 176,
41 		.sizeimage = 176 * 144 * 5 / 8 + 590,
42 		.colorspace = V4L2_COLORSPACE_JPEG,
43 		.priv = 3},
44 */
45 	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
46 		.bytesperline = 320,
47 		.sizeimage = 320 * 240 * 4 / 8 + 590,
48 		.colorspace = V4L2_COLORSPACE_JPEG,
49 		.priv = 2},
50 	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51 		.bytesperline = 640,
52 		.sizeimage = 640 * 480 * 3 / 8 + 590,
53 		.colorspace = V4L2_COLORSPACE_JPEG,
54 		.priv = 1},
55 };
56 
57 /* read <len> bytes to gspca usb_buf */
58 static void reg_r(struct gspca_dev *gspca_dev,
59 			u8 req,
60 			u16 index,
61 			int len)
62 {
63 #if USB_BUF_SZ < 64
64 #error "USB buffer too small"
65 #endif
66 	struct usb_device *dev = gspca_dev->dev;
67 	int ret;
68 
69 	if (gspca_dev->usb_err < 0)
70 		return;
71 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
72 			req,
73 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
74 			0x0000,			/* value */
75 			index,
76 			gspca_dev->usb_buf, len,
77 			500);
78 	PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index,
79 			 gspca_dev->usb_buf[0]);
80 	if (ret < 0) {
81 		pr_err("reg_r err %d\n", ret);
82 		gspca_dev->usb_err = ret;
83 	}
84 }
85 
86 static void reg_w(struct gspca_dev *gspca_dev,
87 			u8 req,
88 			u16 value,
89 			u16 index)
90 {
91 	struct usb_device *dev = gspca_dev->dev;
92 	int ret;
93 
94 	if (gspca_dev->usb_err < 0)
95 		return;
96 	PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index);
97 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
98 			req,
99 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
100 			value, index,
101 			NULL, 0, 500);
102 	if (ret < 0) {
103 		pr_err("reg_w err %d\n", ret);
104 		gspca_dev->usb_err = ret;
105 	}
106 }
107 
108 static void reg_wb(struct gspca_dev *gspca_dev,
109 			u8 req,
110 			u16 value,
111 			u16 index,
112 			u8 byte)
113 {
114 	struct usb_device *dev = gspca_dev->dev;
115 	int ret;
116 
117 	if (gspca_dev->usb_err < 0)
118 		return;
119 	PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
120 	gspca_dev->usb_buf[0] = byte;
121 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
122 			req,
123 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124 			value, index,
125 			gspca_dev->usb_buf, 1, 500);
126 	if (ret < 0) {
127 		pr_err("reg_w err %d\n", ret);
128 		gspca_dev->usb_err = ret;
129 	}
130 }
131 
132 static void wait_status_0(struct gspca_dev *gspca_dev)
133 {
134 	int i, w;
135 
136 	i = 16;
137 	w = 0;
138 	do {
139 		reg_r(gspca_dev, 0x21, 0x0000, 1);
140 		if (gspca_dev->usb_buf[0] == 0)
141 			return;
142 		w += 15;
143 		msleep(w);
144 	} while (--i > 0);
145 	PERR("wait_status_0 timeout");
146 	gspca_dev->usb_err = -ETIME;
147 }
148 
149 static void wait_status_1(struct gspca_dev *gspca_dev)
150 {
151 	int i;
152 
153 	i = 10;
154 	do {
155 		reg_r(gspca_dev, 0x21, 0x0001, 1);
156 		msleep(10);
157 		if (gspca_dev->usb_buf[0] == 1) {
158 			reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
159 			reg_r(gspca_dev, 0x21, 0x0001, 1);
160 			return;
161 		}
162 	} while (--i > 0);
163 	PERR("wait_status_1 timeout");
164 	gspca_dev->usb_err = -ETIME;
165 }
166 
167 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
168 {
169 	reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
170 }
171 
172 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
173 {
174 	reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
175 }
176 
177 static void sethue(struct gspca_dev *gspca_dev, s32 val)
178 {
179 	reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
180 }
181 
182 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
183 {
184 	reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
185 }
186 
187 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
188 {
189 	reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
190 }
191 
192 /* this function is called at probe time */
193 static int sd_config(struct gspca_dev *gspca_dev,
194 			const struct usb_device_id *id)
195 {
196 	gspca_dev->cam.cam_mode = vga_mode;
197 	gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
198 	gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
199 			/*fixme: 256 in ms-win traces*/
200 
201 	return 0;
202 }
203 
204 /* this function is called at probe and resume time */
205 static int sd_init(struct gspca_dev *gspca_dev)
206 {
207 	reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
208 	reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
209 	reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
210 	reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
211 	msleep(8);
212 	reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
213 	reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
214 	reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
215 
216 	reg_r(gspca_dev, 0x20, 0x0000, 1);
217 	reg_r(gspca_dev, 0x20, 0x0000, 5);
218 	reg_r(gspca_dev, 0x23, 0x0000, 64);
219 	PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
220 				&gspca_dev->usb_buf[0x30]);
221 	reg_r(gspca_dev, 0x23, 0x0001, 64);
222 	return gspca_dev->usb_err;
223 }
224 
225 /* function called at start time before URB creation */
226 static int sd_isoc_init(struct gspca_dev *gspca_dev)
227 {
228 	u8 mode;
229 
230 	reg_r(gspca_dev, 0x00, 0x2520, 1);
231 	wait_status_0(gspca_dev);
232 	reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
233 	wait_status_1(gspca_dev);
234 
235 	wait_status_0(gspca_dev);
236 	mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
237 	reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
238 	reg_r(gspca_dev, 0x25, 0x0004, 1);
239 	reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06);	/* 420 */
240 	reg_r(gspca_dev, 0x27, 0x0000, 1);
241 
242 /* not useful..
243 	gspca_dev->alt = 4;		* use alternate setting 3 */
244 
245 	return gspca_dev->usb_err;
246 }
247 
248 /* -- start the camera -- */
249 static int sd_start(struct gspca_dev *gspca_dev)
250 {
251 	struct sd *sd = (struct sd *) gspca_dev;
252 
253 	/* initialize the JPEG header */
254 	jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
255 			gspca_dev->pixfmt.width,
256 			0x22);		/* JPEG 411 */
257 
258 	/* the JPEG quality shall be 85% */
259 	jpeg_set_qual(sd->jpeg_hdr, 85);
260 
261 	reg_r(gspca_dev, 0x00, 0x2520, 1);
262 	msleep(8);
263 
264 	/* start the capture */
265 	wait_status_0(gspca_dev);
266 	reg_w(gspca_dev, 0x31, 0x0000, 0x0004);	/* start request */
267 	wait_status_1(gspca_dev);
268 	wait_status_0(gspca_dev);
269 	msleep(200);
270 
271 	sd->pkt_seq = 0;
272 	return gspca_dev->usb_err;
273 }
274 
275 static void sd_stopN(struct gspca_dev *gspca_dev)
276 {
277 	/* stop the capture */
278 	wait_status_0(gspca_dev);
279 	reg_w(gspca_dev, 0x31, 0x0000, 0x0000);	/* stop request */
280 	wait_status_1(gspca_dev);
281 	wait_status_0(gspca_dev);
282 }
283 
284 /* move a packet adding 0x00 after 0xff */
285 static void add_packet(struct gspca_dev *gspca_dev,
286 			u8 *data,
287 			int len)
288 {
289 	int i;
290 
291 	i = 0;
292 	do {
293 		if (data[i] == 0xff) {
294 			gspca_frame_add(gspca_dev, INTER_PACKET,
295 					data, i + 1);
296 			len -= i;
297 			data += i;
298 			*data = 0x00;
299 			i = 0;
300 		}
301 	} while (++i < len);
302 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
303 }
304 
305 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
306 			u8 *data,			/* isoc packet */
307 			int len)			/* iso packet length */
308 {
309 	struct sd *sd = (struct sd *) gspca_dev;
310 	static const u8 ffd9[] = {0xff, 0xd9};
311 
312 	/* image packets start with:
313 	 *	02 8n
314 	 * with <n> bit:
315 	 *	0x01: even (0) / odd (1) image
316 	 *	0x02: end of image when set
317 	 */
318 	if (len < 3)
319 		return;				/* empty packet */
320 	if (*data == 0x02) {
321 		if (data[1] & 0x02) {
322 			sd->pkt_seq = !(data[1] & 1);
323 			add_packet(gspca_dev, data + 2, len - 2);
324 			gspca_frame_add(gspca_dev, LAST_PACKET,
325 					ffd9, 2);
326 			return;
327 		}
328 		if ((data[1] & 1) != sd->pkt_seq)
329 			goto err;
330 		if (gspca_dev->last_packet_type == LAST_PACKET)
331 			gspca_frame_add(gspca_dev, FIRST_PACKET,
332 					sd->jpeg_hdr, JPEG_HDR_SZ);
333 		add_packet(gspca_dev, data + 2, len - 2);
334 		return;
335 	}
336 err:
337 	gspca_dev->last_packet_type = DISCARD_PACKET;
338 }
339 
340 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
341 {
342 	struct gspca_dev *gspca_dev =
343 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
344 
345 	gspca_dev->usb_err = 0;
346 
347 	if (!gspca_dev->streaming)
348 		return 0;
349 
350 	switch (ctrl->id) {
351 	case V4L2_CID_BRIGHTNESS:
352 		setbrightness(gspca_dev, ctrl->val);
353 		break;
354 	case V4L2_CID_CONTRAST:
355 		setcontrast(gspca_dev, ctrl->val);
356 		break;
357 	case V4L2_CID_HUE:
358 		sethue(gspca_dev, ctrl->val);
359 		break;
360 	case V4L2_CID_SATURATION:
361 		setcolor(gspca_dev, ctrl->val);
362 		break;
363 	case V4L2_CID_SHARPNESS:
364 		setsharpness(gspca_dev, ctrl->val);
365 		break;
366 	}
367 	return gspca_dev->usb_err;
368 }
369 
370 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
371 	.s_ctrl = sd_s_ctrl,
372 };
373 
374 static int sd_init_controls(struct gspca_dev *gspca_dev)
375 {
376 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
377 
378 	gspca_dev->vdev.ctrl_handler = hdl;
379 	v4l2_ctrl_handler_init(hdl, 5);
380 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
381 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
382 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
383 			V4L2_CID_CONTRAST, 0, 8, 1, 1);
384 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
385 			V4L2_CID_HUE, 0, 255, 1, 0);
386 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
387 			V4L2_CID_SATURATION, 0, 8, 1, 1);
388 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
389 			V4L2_CID_SHARPNESS, 0, 255, 1, 0);
390 
391 	if (hdl->error) {
392 		pr_err("Could not initialize controls\n");
393 		return hdl->error;
394 	}
395 	return 0;
396 }
397 
398 /* sub-driver description */
399 static const struct sd_desc sd_desc = {
400 	.name = MODULE_NAME,
401 	.config = sd_config,
402 	.init = sd_init,
403 	.init_controls = sd_init_controls,
404 	.isoc_init = sd_isoc_init,
405 	.start = sd_start,
406 	.stopN = sd_stopN,
407 	.pkt_scan = sd_pkt_scan,
408 };
409 
410 /* -- module initialisation -- */
411 static const struct usb_device_id device_table[] = {
412 	{USB_DEVICE(0x04fc, 0x1528)},
413 	{}
414 };
415 MODULE_DEVICE_TABLE(usb, device_table);
416 
417 /* -- device connect -- */
418 static int sd_probe(struct usb_interface *intf,
419 			const struct usb_device_id *id)
420 {
421 	/* the video interface for isochronous transfer is 1 */
422 	if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
423 		return -ENODEV;
424 
425 	return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
426 				THIS_MODULE);
427 }
428 
429 static struct usb_driver sd_driver = {
430 	.name = MODULE_NAME,
431 	.id_table = device_table,
432 	.probe = sd_probe,
433 	.disconnect = gspca_disconnect,
434 #ifdef CONFIG_PM
435 	.suspend = gspca_suspend,
436 	.resume = gspca_resume,
437 	.reset_resume = gspca_resume,
438 #endif
439 };
440 
441 module_usb_driver(sd_driver);
442