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