2 * STV0680 USB Camera Driver
4 * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
6 * This module is adapted from the in kernel v4l1 stv680 driver:
8 * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
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.
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.
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.
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
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #define MODULE_NAME "stv0680"
36 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
37 MODULE_DESCRIPTION("STV0680 USB Camera Driver");
38 MODULE_LICENSE("GPL");
40 /* specific webcam descriptor */
42 struct gspca_dev gspca_dev
; /* !! must be the first item */
43 struct v4l2_pix_format mode
;
49 static int stv_sndctrl(struct gspca_dev
*gspca_dev
, int set
, u8 req
, u16 val
,
54 unsigned int pipe
= 0;
58 req_type
= USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
59 pipe
= usb_rcvctrlpipe(gspca_dev
->dev
, 0);
62 req_type
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
63 pipe
= usb_sndctrlpipe(gspca_dev
->dev
, 0);
66 req_type
= USB_DIR_IN
| USB_RECIP_DEVICE
;
67 pipe
= usb_rcvctrlpipe(gspca_dev
->dev
, 0);
70 req_type
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
;
71 pipe
= usb_sndctrlpipe(gspca_dev
->dev
, 0);
75 ret
= usb_control_msg(gspca_dev
->dev
, pipe
,
77 val
, 0, gspca_dev
->usb_buf
, size
, 500);
79 if ((ret
< 0) && (req
!= 0x0a))
80 pr_err("usb_control_msg error %i, request = 0x%x, error = %i\n",
86 static int stv0680_handle_error(struct gspca_dev
*gspca_dev
, int ret
)
88 stv_sndctrl(gspca_dev
, 0, 0x80, 0, 0x02); /* Get Last Error */
89 PERR("last error: %i, command = 0x%x",
90 gspca_dev
->usb_buf
[0], gspca_dev
->usb_buf
[1]);
94 static int stv0680_get_video_mode(struct gspca_dev
*gspca_dev
)
96 /* Note not sure if this init of usb_buf is really necessary */
97 memset(gspca_dev
->usb_buf
, 0, 8);
98 gspca_dev
->usb_buf
[0] = 0x0f;
100 if (stv_sndctrl(gspca_dev
, 0, 0x87, 0, 0x08) != 0x08) {
101 PERR("Get_Camera_Mode failed");
102 return stv0680_handle_error(gspca_dev
, -EIO
);
105 return gspca_dev
->usb_buf
[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
108 static int stv0680_set_video_mode(struct gspca_dev
*gspca_dev
, u8 mode
)
110 struct sd
*sd
= (struct sd
*) gspca_dev
;
112 if (sd
->current_mode
== mode
)
115 memset(gspca_dev
->usb_buf
, 0, 8);
116 gspca_dev
->usb_buf
[0] = mode
;
118 if (stv_sndctrl(gspca_dev
, 3, 0x07, 0x0100, 0x08) != 0x08) {
119 PERR("Set_Camera_Mode failed");
120 return stv0680_handle_error(gspca_dev
, -EIO
);
123 /* Verify we got what we've asked for */
124 if (stv0680_get_video_mode(gspca_dev
) != mode
) {
125 PERR("Error setting camera video mode!");
129 sd
->current_mode
= mode
;
134 /* this function is called at probe time */
135 static int sd_config(struct gspca_dev
*gspca_dev
,
136 const struct usb_device_id
*id
)
139 struct sd
*sd
= (struct sd
*) gspca_dev
;
140 struct cam
*cam
= &gspca_dev
->cam
;
142 /* Give the camera some time to settle, otherwise initialization will
143 fail on hotplug, and yes it really needs a full second. */
146 /* ping camera to be sure STV0680 is present */
147 if (stv_sndctrl(gspca_dev
, 0, 0x88, 0x5678, 0x02) != 0x02 ||
148 gspca_dev
->usb_buf
[0] != 0x56 || gspca_dev
->usb_buf
[1] != 0x78) {
149 PERR("STV(e): camera ping failed!!");
150 return stv0680_handle_error(gspca_dev
, -ENODEV
);
153 /* get camera descriptor */
154 if (stv_sndctrl(gspca_dev
, 2, 0x06, 0x0200, 0x09) != 0x09)
155 return stv0680_handle_error(gspca_dev
, -ENODEV
);
157 if (stv_sndctrl(gspca_dev
, 2, 0x06, 0x0200, 0x22) != 0x22 ||
158 gspca_dev
->usb_buf
[7] != 0xa0 || gspca_dev
->usb_buf
[8] != 0x23) {
159 PERR("Could not get descriptor 0200.");
160 return stv0680_handle_error(gspca_dev
, -ENODEV
);
162 if (stv_sndctrl(gspca_dev
, 0, 0x8a, 0, 0x02) != 0x02)
163 return stv0680_handle_error(gspca_dev
, -ENODEV
);
164 if (stv_sndctrl(gspca_dev
, 0, 0x8b, 0, 0x24) != 0x24)
165 return stv0680_handle_error(gspca_dev
, -ENODEV
);
166 if (stv_sndctrl(gspca_dev
, 0, 0x85, 0, 0x10) != 0x10)
167 return stv0680_handle_error(gspca_dev
, -ENODEV
);
169 if (!(gspca_dev
->usb_buf
[7] & 0x09)) {
170 PERR("Camera supports neither CIF nor QVGA mode");
173 if (gspca_dev
->usb_buf
[7] & 0x01)
174 PDEBUG(D_PROBE
, "Camera supports CIF mode");
175 if (gspca_dev
->usb_buf
[7] & 0x02)
176 PDEBUG(D_PROBE
, "Camera supports VGA mode");
177 if (gspca_dev
->usb_buf
[7] & 0x04)
178 PDEBUG(D_PROBE
, "Camera supports QCIF mode");
179 if (gspca_dev
->usb_buf
[7] & 0x08)
180 PDEBUG(D_PROBE
, "Camera supports QVGA mode");
182 if (gspca_dev
->usb_buf
[7] & 0x01)
183 sd
->video_mode
= 0x00; /* CIF */
185 sd
->video_mode
= 0x03; /* QVGA */
187 /* FW rev, ASIC rev, sensor ID */
188 PDEBUG(D_PROBE
, "Firmware rev is %i.%i",
189 gspca_dev
->usb_buf
[0], gspca_dev
->usb_buf
[1]);
190 PDEBUG(D_PROBE
, "ASIC rev is %i.%i",
191 gspca_dev
->usb_buf
[2], gspca_dev
->usb_buf
[3]);
192 PDEBUG(D_PROBE
, "Sensor ID is %i",
193 (gspca_dev
->usb_buf
[4]*16) + (gspca_dev
->usb_buf
[5]>>4));
196 ret
= stv0680_get_video_mode(gspca_dev
);
199 sd
->current_mode
= sd
->orig_mode
= ret
;
201 ret
= stv0680_set_video_mode(gspca_dev
, sd
->video_mode
);
205 /* Get mode details */
206 if (stv_sndctrl(gspca_dev
, 0, 0x8f, 0, 0x10) != 0x10)
207 return stv0680_handle_error(gspca_dev
, -EIO
);
210 cam
->bulk_nurbs
= 1; /* The cam cannot handle more */
211 cam
->bulk_size
= (gspca_dev
->usb_buf
[0] << 24) |
212 (gspca_dev
->usb_buf
[1] << 16) |
213 (gspca_dev
->usb_buf
[2] << 8) |
214 (gspca_dev
->usb_buf
[3]);
215 sd
->mode
.width
= (gspca_dev
->usb_buf
[4] << 8) |
216 (gspca_dev
->usb_buf
[5]); /* 322, 356, 644 */
217 sd
->mode
.height
= (gspca_dev
->usb_buf
[6] << 8) |
218 (gspca_dev
->usb_buf
[7]); /* 242, 292, 484 */
219 sd
->mode
.pixelformat
= V4L2_PIX_FMT_STV0680
;
220 sd
->mode
.field
= V4L2_FIELD_NONE
;
221 sd
->mode
.bytesperline
= sd
->mode
.width
;
222 sd
->mode
.sizeimage
= cam
->bulk_size
;
223 sd
->mode
.colorspace
= V4L2_COLORSPACE_SRGB
;
225 /* origGain = gspca_dev->usb_buf[12]; */
227 cam
->cam_mode
= &sd
->mode
;
231 ret
= stv0680_set_video_mode(gspca_dev
, sd
->orig_mode
);
235 if (stv_sndctrl(gspca_dev
, 2, 0x06, 0x0100, 0x12) != 0x12 ||
236 gspca_dev
->usb_buf
[8] != 0x53 || gspca_dev
->usb_buf
[9] != 0x05) {
237 pr_err("Could not get descriptor 0100\n");
238 return stv0680_handle_error(gspca_dev
, -EIO
);
244 /* this function is called at probe and resume time */
245 static int sd_init(struct gspca_dev
*gspca_dev
)
250 /* -- start the camera -- */
251 static int sd_start(struct gspca_dev
*gspca_dev
)
254 struct sd
*sd
= (struct sd
*) gspca_dev
;
256 ret
= stv0680_set_video_mode(gspca_dev
, sd
->video_mode
);
260 if (stv_sndctrl(gspca_dev
, 0, 0x85, 0, 0x10) != 0x10)
261 return stv0680_handle_error(gspca_dev
, -EIO
);
264 0x0000 = CIF (352x288)
265 0x0100 = VGA (640x480)
266 0x0300 = QVGA (320x240) */
267 if (stv_sndctrl(gspca_dev
, 1, 0x09, sd
->video_mode
<< 8, 0x0) != 0x0)
268 return stv0680_handle_error(gspca_dev
, -EIO
);
273 static void sd_stopN(struct gspca_dev
*gspca_dev
)
275 /* This is a high priority command; it stops all lower order cmds */
276 if (stv_sndctrl(gspca_dev
, 1, 0x04, 0x0000, 0x0) != 0x0)
277 stv0680_handle_error(gspca_dev
, -EIO
);
280 static void sd_stop0(struct gspca_dev
*gspca_dev
)
282 struct sd
*sd
= (struct sd
*) gspca_dev
;
284 if (!sd
->gspca_dev
.present
)
287 stv0680_set_video_mode(gspca_dev
, sd
->orig_mode
);
290 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
294 struct sd
*sd
= (struct sd
*) gspca_dev
;
296 /* Every now and then the camera sends a 16 byte packet, no idea
297 what it contains, but it is not image data, when this
298 happens the frame received before this packet is corrupt,
300 if (len
!= sd
->mode
.sizeimage
) {
301 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
305 /* Finish the previous frame, we do this upon reception of the next
306 packet, even though it is already complete so that the strange 16
307 byte packets send after a corrupt frame can discard it. */
308 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
310 /* Store the just received frame */
311 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
314 /* sub-driver description */
315 static const struct sd_desc sd_desc
= {
322 .pkt_scan
= sd_pkt_scan
,
325 /* -- module initialisation -- */
326 static const struct usb_device_id device_table
[] = {
327 {USB_DEVICE(0x0553, 0x0202)},
328 {USB_DEVICE(0x041e, 0x4007)},
331 MODULE_DEVICE_TABLE(usb
, device_table
);
333 /* -- device connect -- */
334 static int sd_probe(struct usb_interface
*intf
,
335 const struct usb_device_id
*id
)
337 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
341 static struct usb_driver sd_driver
= {
343 .id_table
= device_table
,
345 .disconnect
= gspca_disconnect
,
347 .suspend
= gspca_suspend
,
348 .resume
= gspca_resume
,
349 .reset_resume
= gspca_resume
,
353 module_usb_driver(sd_driver
);