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