xref: /openbmc/linux/drivers/media/usb/gspca/spca1528.c (revision 4a075bd4)
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 	gspca_dbg(gspca_dev, D_USBI, "GET %02x 0000 %04x %02x\n", 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 	gspca_dbg(gspca_dev, D_USBO, "SET %02x %04x %04x\n", 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 	gspca_dbg(gspca_dev, D_USBO, "SET %02x %04x %04x %02x\n",
120 		  req, value, index, byte);
121 	gspca_dev->usb_buf[0] = byte;
122 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
123 			req,
124 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
125 			value, index,
126 			gspca_dev->usb_buf, 1, 500);
127 	if (ret < 0) {
128 		pr_err("reg_w err %d\n", ret);
129 		gspca_dev->usb_err = ret;
130 	}
131 }
132 
133 static void wait_status_0(struct gspca_dev *gspca_dev)
134 {
135 	int i, w;
136 
137 	i = 16;
138 	w = 0;
139 	do {
140 		reg_r(gspca_dev, 0x21, 0x0000, 1);
141 		if (gspca_dev->usb_buf[0] == 0)
142 			return;
143 		w += 15;
144 		msleep(w);
145 	} while (--i > 0);
146 	gspca_err(gspca_dev, "wait_status_0 timeout\n");
147 	gspca_dev->usb_err = -ETIME;
148 }
149 
150 static void wait_status_1(struct gspca_dev *gspca_dev)
151 {
152 	int i;
153 
154 	i = 10;
155 	do {
156 		reg_r(gspca_dev, 0x21, 0x0001, 1);
157 		msleep(10);
158 		if (gspca_dev->usb_buf[0] == 1) {
159 			reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
160 			reg_r(gspca_dev, 0x21, 0x0001, 1);
161 			return;
162 		}
163 	} while (--i > 0);
164 	gspca_err(gspca_dev, "wait_status_1 timeout\n");
165 	gspca_dev->usb_err = -ETIME;
166 }
167 
168 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
169 {
170 	reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
171 }
172 
173 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
174 {
175 	reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
176 }
177 
178 static void sethue(struct gspca_dev *gspca_dev, s32 val)
179 {
180 	reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
181 }
182 
183 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
184 {
185 	reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
186 }
187 
188 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
189 {
190 	reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
191 }
192 
193 /* this function is called at probe time */
194 static int sd_config(struct gspca_dev *gspca_dev,
195 			const struct usb_device_id *id)
196 {
197 	gspca_dev->cam.cam_mode = vga_mode;
198 	gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
199 	gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
200 			/*fixme: 256 in ms-win traces*/
201 
202 	return 0;
203 }
204 
205 /* this function is called at probe and resume time */
206 static int sd_init(struct gspca_dev *gspca_dev)
207 {
208 	reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
209 	reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
210 	reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
211 	reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
212 	msleep(8);
213 	reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
214 	reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
215 	reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
216 
217 	reg_r(gspca_dev, 0x20, 0x0000, 1);
218 	reg_r(gspca_dev, 0x20, 0x0000, 5);
219 	reg_r(gspca_dev, 0x23, 0x0000, 64);
220 	gspca_dbg(gspca_dev, D_PROBE, "%s%s\n", &gspca_dev->usb_buf[0x1c],
221 		  &gspca_dev->usb_buf[0x30]);
222 	reg_r(gspca_dev, 0x23, 0x0001, 64);
223 	return gspca_dev->usb_err;
224 }
225 
226 /* function called at start time before URB creation */
227 static int sd_isoc_init(struct gspca_dev *gspca_dev)
228 {
229 	u8 mode;
230 
231 	reg_r(gspca_dev, 0x00, 0x2520, 1);
232 	wait_status_0(gspca_dev);
233 	reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
234 	wait_status_1(gspca_dev);
235 
236 	wait_status_0(gspca_dev);
237 	mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
238 	reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
239 	reg_r(gspca_dev, 0x25, 0x0004, 1);
240 	reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06);	/* 420 */
241 	reg_r(gspca_dev, 0x27, 0x0000, 1);
242 
243 /* not useful..
244 	gspca_dev->alt = 4;		* use alternate setting 3 */
245 
246 	return gspca_dev->usb_err;
247 }
248 
249 /* -- start the camera -- */
250 static int sd_start(struct gspca_dev *gspca_dev)
251 {
252 	struct sd *sd = (struct sd *) gspca_dev;
253 
254 	/* initialize the JPEG header */
255 	jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
256 			gspca_dev->pixfmt.width,
257 			0x22);		/* JPEG 411 */
258 
259 	/* the JPEG quality shall be 85% */
260 	jpeg_set_qual(sd->jpeg_hdr, 85);
261 
262 	reg_r(gspca_dev, 0x00, 0x2520, 1);
263 	msleep(8);
264 
265 	/* start the capture */
266 	wait_status_0(gspca_dev);
267 	reg_w(gspca_dev, 0x31, 0x0000, 0x0004);	/* start request */
268 	wait_status_1(gspca_dev);
269 	wait_status_0(gspca_dev);
270 	msleep(200);
271 
272 	sd->pkt_seq = 0;
273 	return gspca_dev->usb_err;
274 }
275 
276 static void sd_stopN(struct gspca_dev *gspca_dev)
277 {
278 	/* stop the capture */
279 	wait_status_0(gspca_dev);
280 	reg_w(gspca_dev, 0x31, 0x0000, 0x0000);	/* stop request */
281 	wait_status_1(gspca_dev);
282 	wait_status_0(gspca_dev);
283 }
284 
285 /* move a packet adding 0x00 after 0xff */
286 static void add_packet(struct gspca_dev *gspca_dev,
287 			u8 *data,
288 			int len)
289 {
290 	int i;
291 
292 	i = 0;
293 	do {
294 		if (data[i] == 0xff) {
295 			gspca_frame_add(gspca_dev, INTER_PACKET,
296 					data, i + 1);
297 			len -= i;
298 			data += i;
299 			*data = 0x00;
300 			i = 0;
301 		}
302 	} while (++i < len);
303 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
304 }
305 
306 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
307 			u8 *data,			/* isoc packet */
308 			int len)			/* iso packet length */
309 {
310 	struct sd *sd = (struct sd *) gspca_dev;
311 	static const u8 ffd9[] = {0xff, 0xd9};
312 
313 	/* image packets start with:
314 	 *	02 8n
315 	 * with <n> bit:
316 	 *	0x01: even (0) / odd (1) image
317 	 *	0x02: end of image when set
318 	 */
319 	if (len < 3)
320 		return;				/* empty packet */
321 	if (*data == 0x02) {
322 		if (data[1] & 0x02) {
323 			sd->pkt_seq = !(data[1] & 1);
324 			add_packet(gspca_dev, data + 2, len - 2);
325 			gspca_frame_add(gspca_dev, LAST_PACKET,
326 					ffd9, 2);
327 			return;
328 		}
329 		if ((data[1] & 1) != sd->pkt_seq)
330 			goto err;
331 		if (gspca_dev->last_packet_type == LAST_PACKET)
332 			gspca_frame_add(gspca_dev, FIRST_PACKET,
333 					sd->jpeg_hdr, JPEG_HDR_SZ);
334 		add_packet(gspca_dev, data + 2, len - 2);
335 		return;
336 	}
337 err:
338 	gspca_dev->last_packet_type = DISCARD_PACKET;
339 }
340 
341 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
342 {
343 	struct gspca_dev *gspca_dev =
344 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
345 
346 	gspca_dev->usb_err = 0;
347 
348 	if (!gspca_dev->streaming)
349 		return 0;
350 
351 	switch (ctrl->id) {
352 	case V4L2_CID_BRIGHTNESS:
353 		setbrightness(gspca_dev, ctrl->val);
354 		break;
355 	case V4L2_CID_CONTRAST:
356 		setcontrast(gspca_dev, ctrl->val);
357 		break;
358 	case V4L2_CID_HUE:
359 		sethue(gspca_dev, ctrl->val);
360 		break;
361 	case V4L2_CID_SATURATION:
362 		setcolor(gspca_dev, ctrl->val);
363 		break;
364 	case V4L2_CID_SHARPNESS:
365 		setsharpness(gspca_dev, ctrl->val);
366 		break;
367 	}
368 	return gspca_dev->usb_err;
369 }
370 
371 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
372 	.s_ctrl = sd_s_ctrl,
373 };
374 
375 static int sd_init_controls(struct gspca_dev *gspca_dev)
376 {
377 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
378 
379 	gspca_dev->vdev.ctrl_handler = hdl;
380 	v4l2_ctrl_handler_init(hdl, 5);
381 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
382 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
383 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
384 			V4L2_CID_CONTRAST, 0, 8, 1, 1);
385 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
386 			V4L2_CID_HUE, 0, 255, 1, 0);
387 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
388 			V4L2_CID_SATURATION, 0, 8, 1, 1);
389 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
390 			V4L2_CID_SHARPNESS, 0, 255, 1, 0);
391 
392 	if (hdl->error) {
393 		pr_err("Could not initialize controls\n");
394 		return hdl->error;
395 	}
396 	return 0;
397 }
398 
399 /* sub-driver description */
400 static const struct sd_desc sd_desc = {
401 	.name = MODULE_NAME,
402 	.config = sd_config,
403 	.init = sd_init,
404 	.init_controls = sd_init_controls,
405 	.isoc_init = sd_isoc_init,
406 	.start = sd_start,
407 	.stopN = sd_stopN,
408 	.pkt_scan = sd_pkt_scan,
409 };
410 
411 /* -- module initialisation -- */
412 static const struct usb_device_id device_table[] = {
413 	{USB_DEVICE(0x04fc, 0x1528)},
414 	{}
415 };
416 MODULE_DEVICE_TABLE(usb, device_table);
417 
418 /* -- device connect -- */
419 static int sd_probe(struct usb_interface *intf,
420 			const struct usb_device_id *id)
421 {
422 	/* the video interface for isochronous transfer is 1 */
423 	if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
424 		return -ENODEV;
425 
426 	return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
427 				THIS_MODULE);
428 }
429 
430 static struct usb_driver sd_driver = {
431 	.name = MODULE_NAME,
432 	.id_table = device_table,
433 	.probe = sd_probe,
434 	.disconnect = gspca_disconnect,
435 #ifdef CONFIG_PM
436 	.suspend = gspca_suspend,
437 	.resume = gspca_resume,
438 	.reset_resume = gspca_resume,
439 #endif
440 };
441 
442 module_usb_driver(sd_driver);
443