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.
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #define MODULE_NAME "stv0680"
32 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
33 MODULE_DESCRIPTION("STV0680 USB Camera Driver");
34 MODULE_LICENSE("GPL");
36 /* specific webcam descriptor */
38 struct gspca_dev gspca_dev
; /* !! must be the first item */
39 struct v4l2_pix_format mode
;
45 static int stv_sndctrl(struct gspca_dev
*gspca_dev
, int set
, u8 req
, u16 val
,
50 unsigned int pipe
= 0;
54 req_type
= USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
55 pipe
= usb_rcvctrlpipe(gspca_dev
->dev
, 0);
58 req_type
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
59 pipe
= usb_sndctrlpipe(gspca_dev
->dev
, 0);
62 req_type
= USB_DIR_IN
| USB_RECIP_DEVICE
;
63 pipe
= usb_rcvctrlpipe(gspca_dev
->dev
, 0);
66 req_type
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
;
67 pipe
= usb_sndctrlpipe(gspca_dev
->dev
, 0);
71 ret
= usb_control_msg(gspca_dev
->dev
, pipe
,
73 val
, 0, gspca_dev
->usb_buf
, size
, 500);
75 if ((ret
< 0) && (req
!= 0x0a))
76 pr_err("usb_control_msg error %i, request = 0x%x, error = %i\n",
82 static int stv0680_handle_error(struct gspca_dev
*gspca_dev
, int ret
)
84 stv_sndctrl(gspca_dev
, 0, 0x80, 0, 0x02); /* Get Last Error */
85 gspca_err(gspca_dev
, "last error: %i, command = 0x%x\n",
86 gspca_dev
->usb_buf
[0], gspca_dev
->usb_buf
[1]);
90 static int stv0680_get_video_mode(struct gspca_dev
*gspca_dev
)
92 /* Note not sure if this init of usb_buf is really necessary */
93 memset(gspca_dev
->usb_buf
, 0, 8);
94 gspca_dev
->usb_buf
[0] = 0x0f;
96 if (stv_sndctrl(gspca_dev
, 0, 0x87, 0, 0x08) != 0x08) {
97 gspca_err(gspca_dev
, "Get_Camera_Mode failed\n");
98 return stv0680_handle_error(gspca_dev
, -EIO
);
101 return gspca_dev
->usb_buf
[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
104 static int stv0680_set_video_mode(struct gspca_dev
*gspca_dev
, u8 mode
)
106 struct sd
*sd
= (struct sd
*) gspca_dev
;
108 if (sd
->current_mode
== mode
)
111 memset(gspca_dev
->usb_buf
, 0, 8);
112 gspca_dev
->usb_buf
[0] = mode
;
114 if (stv_sndctrl(gspca_dev
, 3, 0x07, 0x0100, 0x08) != 0x08) {
115 gspca_err(gspca_dev
, "Set_Camera_Mode failed\n");
116 return stv0680_handle_error(gspca_dev
, -EIO
);
119 /* Verify we got what we've asked for */
120 if (stv0680_get_video_mode(gspca_dev
) != mode
) {
121 gspca_err(gspca_dev
, "Error setting camera video mode!\n");
125 sd
->current_mode
= mode
;
130 /* this function is called at probe time */
131 static int sd_config(struct gspca_dev
*gspca_dev
,
132 const struct usb_device_id
*id
)
135 struct sd
*sd
= (struct sd
*) gspca_dev
;
136 struct cam
*cam
= &gspca_dev
->cam
;
138 /* Give the camera some time to settle, otherwise initialization will
139 fail on hotplug, and yes it really needs a full second. */
142 /* ping camera to be sure STV0680 is present */
143 if (stv_sndctrl(gspca_dev
, 0, 0x88, 0x5678, 0x02) != 0x02 ||
144 gspca_dev
->usb_buf
[0] != 0x56 || gspca_dev
->usb_buf
[1] != 0x78) {
145 gspca_err(gspca_dev
, "STV(e): camera ping failed!!\n");
146 return stv0680_handle_error(gspca_dev
, -ENODEV
);
149 /* get camera descriptor */
150 if (stv_sndctrl(gspca_dev
, 2, 0x06, 0x0200, 0x09) != 0x09)
151 return stv0680_handle_error(gspca_dev
, -ENODEV
);
153 if (stv_sndctrl(gspca_dev
, 2, 0x06, 0x0200, 0x22) != 0x22 ||
154 gspca_dev
->usb_buf
[7] != 0xa0 || gspca_dev
->usb_buf
[8] != 0x23) {
155 gspca_err(gspca_dev
, "Could not get descriptor 0200\n");
156 return stv0680_handle_error(gspca_dev
, -ENODEV
);
158 if (stv_sndctrl(gspca_dev
, 0, 0x8a, 0, 0x02) != 0x02)
159 return stv0680_handle_error(gspca_dev
, -ENODEV
);
160 if (stv_sndctrl(gspca_dev
, 0, 0x8b, 0, 0x24) != 0x24)
161 return stv0680_handle_error(gspca_dev
, -ENODEV
);
162 if (stv_sndctrl(gspca_dev
, 0, 0x85, 0, 0x10) != 0x10)
163 return stv0680_handle_error(gspca_dev
, -ENODEV
);
165 if (!(gspca_dev
->usb_buf
[7] & 0x09)) {
166 gspca_err(gspca_dev
, "Camera supports neither CIF nor QVGA mode\n");
169 if (gspca_dev
->usb_buf
[7] & 0x01)
170 gspca_dbg(gspca_dev
, D_PROBE
, "Camera supports CIF mode\n");
171 if (gspca_dev
->usb_buf
[7] & 0x02)
172 gspca_dbg(gspca_dev
, D_PROBE
, "Camera supports VGA mode\n");
173 if (gspca_dev
->usb_buf
[7] & 0x04)
174 gspca_dbg(gspca_dev
, D_PROBE
, "Camera supports QCIF mode\n");
175 if (gspca_dev
->usb_buf
[7] & 0x08)
176 gspca_dbg(gspca_dev
, D_PROBE
, "Camera supports QVGA mode\n");
178 if (gspca_dev
->usb_buf
[7] & 0x01)
179 sd
->video_mode
= 0x00; /* CIF */
181 sd
->video_mode
= 0x03; /* QVGA */
183 /* FW rev, ASIC rev, sensor ID */
184 gspca_dbg(gspca_dev
, D_PROBE
, "Firmware rev is %i.%i\n",
185 gspca_dev
->usb_buf
[0], gspca_dev
->usb_buf
[1]);
186 gspca_dbg(gspca_dev
, D_PROBE
, "ASIC rev is %i.%i",
187 gspca_dev
->usb_buf
[2], gspca_dev
->usb_buf
[3]);
188 gspca_dbg(gspca_dev
, D_PROBE
, "Sensor ID is %i",
189 (gspca_dev
->usb_buf
[4]*16) + (gspca_dev
->usb_buf
[5]>>4));
192 ret
= stv0680_get_video_mode(gspca_dev
);
195 sd
->current_mode
= sd
->orig_mode
= ret
;
197 ret
= stv0680_set_video_mode(gspca_dev
, sd
->video_mode
);
201 /* Get mode details */
202 if (stv_sndctrl(gspca_dev
, 0, 0x8f, 0, 0x10) != 0x10)
203 return stv0680_handle_error(gspca_dev
, -EIO
);
206 cam
->bulk_nurbs
= 1; /* The cam cannot handle more */
207 cam
->bulk_size
= (gspca_dev
->usb_buf
[0] << 24) |
208 (gspca_dev
->usb_buf
[1] << 16) |
209 (gspca_dev
->usb_buf
[2] << 8) |
210 (gspca_dev
->usb_buf
[3]);
211 sd
->mode
.width
= (gspca_dev
->usb_buf
[4] << 8) |
212 (gspca_dev
->usb_buf
[5]); /* 322, 356, 644 */
213 sd
->mode
.height
= (gspca_dev
->usb_buf
[6] << 8) |
214 (gspca_dev
->usb_buf
[7]); /* 242, 292, 484 */
215 sd
->mode
.pixelformat
= V4L2_PIX_FMT_STV0680
;
216 sd
->mode
.field
= V4L2_FIELD_NONE
;
217 sd
->mode
.bytesperline
= sd
->mode
.width
;
218 sd
->mode
.sizeimage
= cam
->bulk_size
;
219 sd
->mode
.colorspace
= V4L2_COLORSPACE_SRGB
;
221 /* origGain = gspca_dev->usb_buf[12]; */
223 cam
->cam_mode
= &sd
->mode
;
227 ret
= stv0680_set_video_mode(gspca_dev
, sd
->orig_mode
);
231 if (stv_sndctrl(gspca_dev
, 2, 0x06, 0x0100, 0x12) != 0x12 ||
232 gspca_dev
->usb_buf
[8] != 0x53 || gspca_dev
->usb_buf
[9] != 0x05) {
233 pr_err("Could not get descriptor 0100\n");
234 return stv0680_handle_error(gspca_dev
, -EIO
);
240 /* this function is called at probe and resume time */
241 static int sd_init(struct gspca_dev
*gspca_dev
)
246 /* -- start the camera -- */
247 static int sd_start(struct gspca_dev
*gspca_dev
)
250 struct sd
*sd
= (struct sd
*) gspca_dev
;
252 ret
= stv0680_set_video_mode(gspca_dev
, sd
->video_mode
);
256 if (stv_sndctrl(gspca_dev
, 0, 0x85, 0, 0x10) != 0x10)
257 return stv0680_handle_error(gspca_dev
, -EIO
);
260 0x0000 = CIF (352x288)
261 0x0100 = VGA (640x480)
262 0x0300 = QVGA (320x240) */
263 if (stv_sndctrl(gspca_dev
, 1, 0x09, sd
->video_mode
<< 8, 0x0) != 0x0)
264 return stv0680_handle_error(gspca_dev
, -EIO
);
269 static void sd_stopN(struct gspca_dev
*gspca_dev
)
271 /* This is a high priority command; it stops all lower order cmds */
272 if (stv_sndctrl(gspca_dev
, 1, 0x04, 0x0000, 0x0) != 0x0)
273 stv0680_handle_error(gspca_dev
, -EIO
);
276 static void sd_stop0(struct gspca_dev
*gspca_dev
)
278 struct sd
*sd
= (struct sd
*) gspca_dev
;
280 if (!sd
->gspca_dev
.present
)
283 stv0680_set_video_mode(gspca_dev
, sd
->orig_mode
);
286 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
290 struct sd
*sd
= (struct sd
*) gspca_dev
;
292 /* Every now and then the camera sends a 16 byte packet, no idea
293 what it contains, but it is not image data, when this
294 happens the frame received before this packet is corrupt,
296 if (len
!= sd
->mode
.sizeimage
) {
297 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
301 /* Finish the previous frame, we do this upon reception of the next
302 packet, even though it is already complete so that the strange 16
303 byte packets send after a corrupt frame can discard it. */
304 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
306 /* Store the just received frame */
307 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
310 /* sub-driver description */
311 static const struct sd_desc sd_desc
= {
318 .pkt_scan
= sd_pkt_scan
,
321 /* -- module initialisation -- */
322 static const struct usb_device_id device_table
[] = {
323 {USB_DEVICE(0x0553, 0x0202)},
324 {USB_DEVICE(0x041e, 0x4007)},
327 MODULE_DEVICE_TABLE(usb
, device_table
);
329 /* -- device connect -- */
330 static int sd_probe(struct usb_interface
*intf
,
331 const struct usb_device_id
*id
)
333 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
337 static struct usb_driver sd_driver
= {
339 .id_table
= device_table
,
341 .disconnect
= gspca_disconnect
,
343 .suspend
= gspca_suspend
,
344 .resume
= gspca_resume
,
345 .reset_resume
= gspca_resume
,
349 module_usb_driver(sd_driver
);