blob: 2a69d7ccb50dd07434c75cdbc3cf30860a4655a9 [file] [log] [blame]
Hans de Goede21f1b932009-10-23 06:50:12 -03001/*
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 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
57 switch (set) {
58 case 0: /* 0xc1 */
59 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
60 break;
61 case 1: /* 0x41 */
62 req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
63 break;
64 case 2: /* 0x80 */
65 req_type = USB_DIR_IN | USB_RECIP_DEVICE;
66 break;
67 case 3: /* 0x40 */
68 req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
69 break;
70 }
71
72 ret = usb_control_msg(gspca_dev->dev,
73 usb_rcvctrlpipe(gspca_dev->dev, 0),
74 req, req_type,
75 val, 0, gspca_dev->usb_buf, size, 500);
76
77 if ((ret < 0) && (req != 0x0a))
78 PDEBUG(D_ERR,
79 "usb_control_msg error %i, request = 0x%x, error = %i",
80 set, req, ret);
81
82 return ret;
83}
84
85static int stv0680_handle_error(struct gspca_dev *gspca_dev, int ret)
86{
87 stv_sndctrl(gspca_dev, 0, 0x80, 0, 0x02); /* Get Last Error */
88 PDEBUG(D_ERR, "last error: %i, command = 0x%x",
89 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
90 return ret;
91}
92
93static int stv0680_get_video_mode(struct gspca_dev *gspca_dev)
94{
95 /* Note not sure if this init of usb_buf is really necessary */
96 memset(gspca_dev->usb_buf, 0, 8);
97 gspca_dev->usb_buf[0] = 0x0f;
98
99 if (stv_sndctrl(gspca_dev, 0, 0x87, 0, 0x08) != 0x08) {
100 PDEBUG(D_ERR, "Get_Camera_Mode failed");
101 return stv0680_handle_error(gspca_dev, -EIO);
102 }
103
104 return gspca_dev->usb_buf[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
105}
106
107static int stv0680_set_video_mode(struct gspca_dev *gspca_dev, u8 mode)
108{
109 struct sd *sd = (struct sd *) gspca_dev;
110
111 if (sd->current_mode == mode)
112 return 0;
113
114 memset(gspca_dev->usb_buf, 0, 8);
115 gspca_dev->usb_buf[0] = mode;
116
117 if (stv_sndctrl(gspca_dev, 3, 0x07, 0x0100, 0x08) != 0x08) {
118 PDEBUG(D_ERR, "Set_Camera_Mode failed");
119 return stv0680_handle_error(gspca_dev, -EIO);
120 }
121
122 /* Verify we got what we've asked for */
123 if (stv0680_get_video_mode(gspca_dev) != mode) {
124 PDEBUG(D_ERR, "Error setting camera video mode!");
125 return -EIO;
126 }
127
128 sd->current_mode = mode;
129
130 return 0;
131}
132
133/* this function is called at probe time */
134static int sd_config(struct gspca_dev *gspca_dev,
135 const struct usb_device_id *id)
136{
137 int ret;
138 struct sd *sd = (struct sd *) gspca_dev;
139 struct cam *cam = &gspca_dev->cam;
140
141 /* ping camera to be sure STV0680 is present */
142 if (stv_sndctrl(gspca_dev, 0, 0x88, 0x5678, 0x02) != 0x02 ||
143 gspca_dev->usb_buf[0] != 0x56 || gspca_dev->usb_buf[1] != 0x78) {
144 PDEBUG(D_ERR, "STV(e): camera ping failed!!");
145 return stv0680_handle_error(gspca_dev, -ENODEV);
146 }
147
148 /* get camera descriptor */
149 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x09) != 0x09)
150 return stv0680_handle_error(gspca_dev, -ENODEV);
151
152 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x22) != 0x22 ||
153 gspca_dev->usb_buf[7] != 0xa0 || gspca_dev->usb_buf[8] != 0x23) {
154 PDEBUG(D_ERR, "Could not get descriptor 0200.");
155 return stv0680_handle_error(gspca_dev, -ENODEV);
156 }
157 if (stv_sndctrl(gspca_dev, 0, 0x8a, 0, 0x02) != 0x02)
158 return stv0680_handle_error(gspca_dev, -ENODEV);
159 if (stv_sndctrl(gspca_dev, 0, 0x8b, 0, 0x24) != 0x24)
160 return stv0680_handle_error(gspca_dev, -ENODEV);
161 if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
162 return stv0680_handle_error(gspca_dev, -ENODEV);
163
164 if (!(gspca_dev->usb_buf[7] & 0x09)) {
165 PDEBUG(D_ERR, "Camera supports neither CIF nor QVGA mode");
166 return -ENODEV;
167 }
168 if (gspca_dev->usb_buf[7] & 0x01)
169 PDEBUG(D_PROBE, "Camera supports CIF mode");
170 if (gspca_dev->usb_buf[7] & 0x02)
171 PDEBUG(D_PROBE, "Camera supports VGA mode");
172 if (gspca_dev->usb_buf[7] & 0x08)
173 PDEBUG(D_PROBE, "Camera supports QVGA mode");
174
175 if (gspca_dev->usb_buf[7] & 0x01)
176 sd->video_mode = 0x00; /* CIF */
177 else
178 sd->video_mode = 0x03; /* QVGA */
179
180 /* FW rev, ASIC rev, sensor ID */
181 PDEBUG(D_PROBE, "Firmware rev is %i.%i",
182 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
183 PDEBUG(D_PROBE, "ASIC rev is %i.%i",
184 gspca_dev->usb_buf[2], gspca_dev->usb_buf[3]);
185 PDEBUG(D_PROBE, "Sensor ID is %i",
186 (gspca_dev->usb_buf[4]*16) + (gspca_dev->usb_buf[5]>>4));
187
188
189 ret = stv0680_get_video_mode(gspca_dev);
190 if (ret < 0)
191 return ret;
192 sd->current_mode = sd->orig_mode = ret;
193
194 ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
195 if (ret < 0)
196 return ret;
197
198 /* Get mode details */
199 if (stv_sndctrl(gspca_dev, 0, 0x8f, 0, 0x10) != 0x10)
200 return stv0680_handle_error(gspca_dev, -EIO);
201
202 cam->bulk = 1;
203 cam->bulk_nurbs = 1; /* The cam cannot handle more */
204 cam->bulk_size = (gspca_dev->usb_buf[0] << 24) |
205 (gspca_dev->usb_buf[1] << 16) |
206 (gspca_dev->usb_buf[2] << 8) |
207 (gspca_dev->usb_buf[3]);
208 sd->mode.width = (gspca_dev->usb_buf[4] << 8) |
209 (gspca_dev->usb_buf[5]); /* 322, 356, 644 */
210 sd->mode.height = (gspca_dev->usb_buf[6] << 8) |
211 (gspca_dev->usb_buf[7]); /* 242, 292, 484 */
212 sd->mode.pixelformat = V4L2_PIX_FMT_STV0680;
213 sd->mode.field = V4L2_FIELD_NONE;
214 sd->mode.bytesperline = sd->mode.width;
215 sd->mode.sizeimage = cam->bulk_size;
216 sd->mode.colorspace = V4L2_COLORSPACE_SRGB;
217
218 /* origGain = gspca_dev->usb_buf[12]; */
219
220 cam->cam_mode = &sd->mode;
221 cam->nmodes = 1;
222
223
224 ret = stv0680_set_video_mode(gspca_dev, sd->orig_mode);
225 if (ret < 0)
226 return ret;
227
228 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0100, 0x12) != 0x12 ||
229 gspca_dev->usb_buf[8] != 0x53 || gspca_dev->usb_buf[9] != 0x05) {
230 PDEBUG(D_ERR, "Could not get descriptor 0100.");
231 return stv0680_handle_error(gspca_dev, -EIO);
232 }
233
234 return 0;
235}
236
237/* this function is called at probe and resume time */
238static int sd_init(struct gspca_dev *gspca_dev)
239{
240 return 0;
241}
242
243/* -- start the camera -- */
244static int sd_start(struct gspca_dev *gspca_dev)
245{
246 int ret;
247 struct sd *sd = (struct sd *) gspca_dev;
248
249 ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
250 if (ret < 0)
251 return ret;
252
253 if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
254 return stv0680_handle_error(gspca_dev, -EIO);
255
256 /* Start stream at:
257 0x0000 = CIF (352x288)
258 0x0100 = VGA (640x480)
259 0x0300 = QVGA (320x240) */
260 if (stv_sndctrl(gspca_dev, 1, 0x09, sd->video_mode << 8, 0x0) != 0x0)
261 return stv0680_handle_error(gspca_dev, -EIO);
262
263 return 0;
264}
265
266static void sd_stopN(struct gspca_dev *gspca_dev)
267{
268 /* This is a high priority command; it stops all lower order cmds */
269 if (stv_sndctrl(gspca_dev, 1, 0x04, 0x0000, 0x0) != 0x0)
270 stv0680_handle_error(gspca_dev, -EIO);
271}
272
273static void sd_stop0(struct gspca_dev *gspca_dev)
274{
275 struct sd *sd = (struct sd *) gspca_dev;
276
277 if (!sd->gspca_dev.present)
278 return;
279
280 stv0680_set_video_mode(gspca_dev, sd->orig_mode);
281}
282
283static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300284 u8 *data,
Hans de Goede21f1b932009-10-23 06:50:12 -0300285 int len)
286{
287 struct sd *sd = (struct sd *) gspca_dev;
288
289 /* Every now and then the camera sends a 16 byte packet, no idea
290 what it contains, but it is not image data, when this
291 happens the frame received before this packet is corrupt,
292 so discard it. */
293 if (len != sd->mode.sizeimage) {
294 gspca_dev->last_packet_type = DISCARD_PACKET;
295 return;
296 }
297
298 /* Finish the previous frame, we do this upon reception of the next
299 packet, even though it is already complete so that the strange 16
300 byte packets send after a corrupt frame can discard it. */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300301 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
Hans de Goede21f1b932009-10-23 06:50:12 -0300302
303 /* Store the just received frame */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300304 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
Hans de Goede21f1b932009-10-23 06:50:12 -0300305}
306
307/* sub-driver description */
308static const struct sd_desc sd_desc = {
309 .name = MODULE_NAME,
310 .ctrls = sd_ctrls,
311 .nctrls = ARRAY_SIZE(sd_ctrls),
312 .config = sd_config,
313 .init = sd_init,
314 .start = sd_start,
315 .stopN = sd_stopN,
316 .stop0 = sd_stop0,
317 .pkt_scan = sd_pkt_scan,
318};
319
320/* -- module initialisation -- */
321static const __devinitdata struct usb_device_id device_table[] = {
322 {USB_DEVICE(0x0553, 0x0202)},
323 {USB_DEVICE(0x041e, 0x4007)},
324 {}
325};
326MODULE_DEVICE_TABLE(usb, device_table);
327
328/* -- device connect -- */
329static int sd_probe(struct usb_interface *intf,
330 const struct usb_device_id *id)
331{
332 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
333 THIS_MODULE);
334}
335
336static struct usb_driver sd_driver = {
337 .name = MODULE_NAME,
338 .id_table = device_table,
339 .probe = sd_probe,
340 .disconnect = gspca_disconnect,
341#ifdef CONFIG_PM
342 .suspend = gspca_suspend,
343 .resume = gspca_resume,
344#endif
345};
346
347/* -- module insert / remove -- */
348static int __init sd_mod_init(void)
349{
350 int ret;
351 ret = usb_register(&sd_driver);
352 if (ret < 0)
353 return ret;
354 PDEBUG(D_PROBE, "registered");
355 return 0;
356}
357static void __exit sd_mod_exit(void)
358{
359 usb_deregister(&sd_driver);
360 PDEBUG(D_PROBE, "deregistered");
361}
362
363module_init(sd_mod_init);
364module_exit(sd_mod_exit);