• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/media/video/gspca/
1/*
2 * STV0680 USB Camera Driver
3 *
4 * Copyright (C) 2009 Hans de Goede <hdgoede@redhat.com>
5 *
6 * This module is adapted from the in kernel v4l1 stv680 driver:
7 *
8 *  STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
9 *
10 * Thanks to STMicroelectronics for information on the usb commands, and
11 * to Steve Miller at STM for his help and encouragement while I was
12 * writing this driver.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 *
28 */
29
30#define MODULE_NAME "stv0680"
31
32#include "gspca.h"
33
34MODULE_AUTHOR("Hans de Goede <hdgoede@redhat.com>");
35MODULE_DESCRIPTION("STV0680 USB Camera Driver");
36MODULE_LICENSE("GPL");
37
38/* specific webcam descriptor */
39struct sd {
40	struct gspca_dev gspca_dev;		/* !! must be the first item */
41	struct v4l2_pix_format mode;
42	u8 orig_mode;
43	u8 video_mode;
44	u8 current_mode;
45};
46
47/* V4L2 controls supported by the driver */
48static const struct ctrl sd_ctrls[] = {
49};
50
51static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val,
52		       int size)
53{
54	int ret = -1;
55	u8 req_type = 0;
56	unsigned int pipe = 0;
57
58	switch (set) {
59	case 0: /*  0xc1  */
60		req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
61		pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
62		break;
63	case 1: /*  0x41  */
64		req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
65		pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
66		break;
67	case 2:	/*  0x80  */
68		req_type = USB_DIR_IN | USB_RECIP_DEVICE;
69		pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
70		break;
71	case 3:	/*  0x40  */
72		req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
73		pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
74		break;
75	}
76
77	ret = usb_control_msg(gspca_dev->dev, pipe,
78			      req, req_type,
79			      val, 0, gspca_dev->usb_buf, size, 500);
80
81	if ((ret < 0) && (req != 0x0a))
82		PDEBUG(D_ERR,
83		       "usb_control_msg error %i, request = 0x%x, error = %i",
84		       set, req, ret);
85
86	return ret;
87}
88
89static int stv0680_handle_error(struct gspca_dev *gspca_dev, int ret)
90{
91	stv_sndctrl(gspca_dev, 0, 0x80, 0, 0x02); /* Get Last Error */
92	PDEBUG(D_ERR, "last error: %i,  command = 0x%x",
93	       gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
94	return ret;
95}
96
97static int stv0680_get_video_mode(struct gspca_dev *gspca_dev)
98{
99	/* Note not sure if this init of usb_buf is really necessary */
100	memset(gspca_dev->usb_buf, 0, 8);
101	gspca_dev->usb_buf[0] = 0x0f;
102
103	if (stv_sndctrl(gspca_dev, 0, 0x87, 0, 0x08) != 0x08) {
104		PDEBUG(D_ERR, "Get_Camera_Mode failed");
105		return stv0680_handle_error(gspca_dev, -EIO);
106	}
107
108	return gspca_dev->usb_buf[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
109}
110
111static int stv0680_set_video_mode(struct gspca_dev *gspca_dev, u8 mode)
112{
113	struct sd *sd = (struct sd *) gspca_dev;
114
115	if (sd->current_mode == mode)
116		return 0;
117
118	memset(gspca_dev->usb_buf, 0, 8);
119	gspca_dev->usb_buf[0] = mode;
120
121	if (stv_sndctrl(gspca_dev, 3, 0x07, 0x0100, 0x08) != 0x08) {
122		PDEBUG(D_ERR, "Set_Camera_Mode failed");
123		return stv0680_handle_error(gspca_dev, -EIO);
124	}
125
126	/* Verify we got what we've asked for */
127	if (stv0680_get_video_mode(gspca_dev) != mode) {
128		PDEBUG(D_ERR, "Error setting camera video mode!");
129		return -EIO;
130	}
131
132	sd->current_mode = mode;
133
134	return 0;
135}
136
137/* this function is called at probe time */
138static int sd_config(struct gspca_dev *gspca_dev,
139			const struct usb_device_id *id)
140{
141	int ret;
142	struct sd *sd = (struct sd *) gspca_dev;
143	struct cam *cam = &gspca_dev->cam;
144
145	/* Give the camera some time to settle, otherwise initalization will
146	   fail on hotplug, and yes it really needs a full second. */
147	msleep(1000);
148
149	/* ping camera to be sure STV0680 is present */
150	if (stv_sndctrl(gspca_dev, 0, 0x88, 0x5678, 0x02) != 0x02 ||
151	    gspca_dev->usb_buf[0] != 0x56 || gspca_dev->usb_buf[1] != 0x78) {
152		PDEBUG(D_ERR, "STV(e): camera ping failed!!");
153		return stv0680_handle_error(gspca_dev, -ENODEV);
154	}
155
156	/* get camera descriptor */
157	if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x09) != 0x09)
158		return stv0680_handle_error(gspca_dev, -ENODEV);
159
160	if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x22) != 0x22 ||
161	    gspca_dev->usb_buf[7] != 0xa0 || gspca_dev->usb_buf[8] != 0x23) {
162		PDEBUG(D_ERR, "Could not get descriptor 0200.");
163		return stv0680_handle_error(gspca_dev, -ENODEV);
164	}
165	if (stv_sndctrl(gspca_dev, 0, 0x8a, 0, 0x02) != 0x02)
166		return stv0680_handle_error(gspca_dev, -ENODEV);
167	if (stv_sndctrl(gspca_dev, 0, 0x8b, 0, 0x24) != 0x24)
168		return stv0680_handle_error(gspca_dev, -ENODEV);
169	if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
170		return stv0680_handle_error(gspca_dev, -ENODEV);
171
172	if (!(gspca_dev->usb_buf[7] & 0x09)) {
173		PDEBUG(D_ERR, "Camera supports neither CIF nor QVGA mode");
174		return -ENODEV;
175	}
176	if (gspca_dev->usb_buf[7] & 0x01)
177		PDEBUG(D_PROBE, "Camera supports CIF mode");
178	if (gspca_dev->usb_buf[7] & 0x02)
179		PDEBUG(D_PROBE, "Camera supports VGA mode");
180	if (gspca_dev->usb_buf[7] & 0x04)
181		PDEBUG(D_PROBE, "Camera supports QCIF mode");
182	if (gspca_dev->usb_buf[7] & 0x08)
183		PDEBUG(D_PROBE, "Camera supports QVGA mode");
184
185	if (gspca_dev->usb_buf[7] & 0x01)
186		sd->video_mode = 0x00; /* CIF */
187	else
188		sd->video_mode = 0x03; /* QVGA */
189
190	/* FW rev, ASIC rev, sensor ID  */
191	PDEBUG(D_PROBE, "Firmware rev is %i.%i",
192	       gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
193	PDEBUG(D_PROBE, "ASIC rev is %i.%i",
194	       gspca_dev->usb_buf[2], gspca_dev->usb_buf[3]);
195	PDEBUG(D_PROBE, "Sensor ID is %i",
196	       (gspca_dev->usb_buf[4]*16) + (gspca_dev->usb_buf[5]>>4));
197
198
199	ret = stv0680_get_video_mode(gspca_dev);
200	if (ret < 0)
201		return ret;
202	sd->current_mode = sd->orig_mode = ret;
203
204	ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
205	if (ret < 0)
206		return ret;
207
208	/* Get mode details */
209	if (stv_sndctrl(gspca_dev, 0, 0x8f, 0, 0x10) != 0x10)
210		return stv0680_handle_error(gspca_dev, -EIO);
211
212	cam->bulk = 1;
213	cam->bulk_nurbs = 1; /* The cam cannot handle more */
214	cam->bulk_size = (gspca_dev->usb_buf[0] << 24) |
215			 (gspca_dev->usb_buf[1] << 16) |
216			 (gspca_dev->usb_buf[2] << 8) |
217			 (gspca_dev->usb_buf[3]);
218	sd->mode.width = (gspca_dev->usb_buf[4] << 8) |
219			 (gspca_dev->usb_buf[5]);  /* 322, 356, 644 */
220	sd->mode.height = (gspca_dev->usb_buf[6] << 8) |
221			  (gspca_dev->usb_buf[7]); /* 242, 292, 484 */
222	sd->mode.pixelformat = V4L2_PIX_FMT_STV0680;
223	sd->mode.field = V4L2_FIELD_NONE;
224	sd->mode.bytesperline = sd->mode.width;
225	sd->mode.sizeimage = cam->bulk_size;
226	sd->mode.colorspace = V4L2_COLORSPACE_SRGB;
227
228	/* origGain = gspca_dev->usb_buf[12]; */
229
230	cam->cam_mode = &sd->mode;
231	cam->nmodes = 1;
232
233
234	ret = stv0680_set_video_mode(gspca_dev, sd->orig_mode);
235	if (ret < 0)
236		return ret;
237
238	if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0100, 0x12) != 0x12 ||
239	    gspca_dev->usb_buf[8] != 0x53 || gspca_dev->usb_buf[9] != 0x05) {
240		PDEBUG(D_ERR, "Could not get descriptor 0100.");
241		return stv0680_handle_error(gspca_dev, -EIO);
242	}
243
244	return 0;
245}
246
247/* this function is called at probe and resume time */
248static int sd_init(struct gspca_dev *gspca_dev)
249{
250	return 0;
251}
252
253/* -- start the camera -- */
254static int sd_start(struct gspca_dev *gspca_dev)
255{
256	int ret;
257	struct sd *sd = (struct sd *) gspca_dev;
258
259	ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
260	if (ret < 0)
261		return ret;
262
263	if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
264		return stv0680_handle_error(gspca_dev, -EIO);
265
266	/* Start stream at:
267	   0x0000 = CIF (352x288)
268	   0x0100 = VGA (640x480)
269	   0x0300 = QVGA (320x240) */
270	if (stv_sndctrl(gspca_dev, 1, 0x09, sd->video_mode << 8, 0x0) != 0x0)
271		return stv0680_handle_error(gspca_dev, -EIO);
272
273	return 0;
274}
275
276static void sd_stopN(struct gspca_dev *gspca_dev)
277{
278	/* This is a high priority command; it stops all lower order cmds */
279	if (stv_sndctrl(gspca_dev, 1, 0x04, 0x0000, 0x0) != 0x0)
280		stv0680_handle_error(gspca_dev, -EIO);
281}
282
283static void sd_stop0(struct gspca_dev *gspca_dev)
284{
285	struct sd *sd = (struct sd *) gspca_dev;
286
287	if (!sd->gspca_dev.present)
288		return;
289
290	stv0680_set_video_mode(gspca_dev, sd->orig_mode);
291}
292
293static void sd_pkt_scan(struct gspca_dev *gspca_dev,
294			u8 *data,
295			int len)
296{
297	struct sd *sd = (struct sd *) gspca_dev;
298
299	/* Every now and then the camera sends a 16 byte packet, no idea
300	   what it contains, but it is not image data, when this
301	   happens the frame received before this packet is corrupt,
302	   so discard it. */
303	if (len != sd->mode.sizeimage) {
304		gspca_dev->last_packet_type = DISCARD_PACKET;
305		return;
306	}
307
308	/* Finish the previous frame, we do this upon reception of the next
309	   packet, even though it is already complete so that the strange 16
310	   byte packets send after a corrupt frame can discard it. */
311	gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
312
313	/* Store the just received frame */
314	gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
315}
316
317/* sub-driver description */
318static const struct sd_desc sd_desc = {
319	.name = MODULE_NAME,
320	.ctrls = sd_ctrls,
321	.nctrls = ARRAY_SIZE(sd_ctrls),
322	.config = sd_config,
323	.init = sd_init,
324	.start = sd_start,
325	.stopN = sd_stopN,
326	.stop0 = sd_stop0,
327	.pkt_scan = sd_pkt_scan,
328};
329
330/* -- module initialisation -- */
331static const __devinitdata struct usb_device_id device_table[] = {
332	{USB_DEVICE(0x0553, 0x0202)},
333	{USB_DEVICE(0x041e, 0x4007)},
334	{}
335};
336MODULE_DEVICE_TABLE(usb, device_table);
337
338/* -- device connect -- */
339static int sd_probe(struct usb_interface *intf,
340			const struct usb_device_id *id)
341{
342	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
343				THIS_MODULE);
344}
345
346static struct usb_driver sd_driver = {
347	.name = MODULE_NAME,
348	.id_table = device_table,
349	.probe = sd_probe,
350	.disconnect = gspca_disconnect,
351#ifdef CONFIG_PM
352	.suspend = gspca_suspend,
353	.resume = gspca_resume,
354#endif
355};
356
357/* -- module insert / remove -- */
358static int __init sd_mod_init(void)
359{
360	int ret;
361	ret = usb_register(&sd_driver);
362	if (ret < 0)
363		return ret;
364	PDEBUG(D_PROBE, "registered");
365	return 0;
366}
367static void __exit sd_mod_exit(void)
368{
369	usb_deregister(&sd_driver);
370	PDEBUG(D_PROBE, "deregistered");
371}
372
373module_init(sd_mod_init);
374module_exit(sd_mod_exit);
375