1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28#define MODULE_NAME "stv0680"
29
30#include "gspca.h"
31
32MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
33MODULE_DESCRIPTION("STV0680 USB Camera Driver");
34MODULE_LICENSE("GPL");
35
36
37struct sd {
38 struct gspca_dev gspca_dev;
39 struct v4l2_pix_format mode;
40 u8 orig_mode;
41 u8 video_mode;
42 u8 current_mode;
43};
44
45static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val,
46 int size)
47{
48 int ret = -1;
49 u8 req_type = 0;
50 unsigned int pipe = 0;
51
52 switch (set) {
53 case 0:
54 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
55 pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
56 break;
57 case 1:
58 req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
59 pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
60 break;
61 case 2:
62 req_type = USB_DIR_IN | USB_RECIP_DEVICE;
63 pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
64 break;
65 case 3:
66 req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
67 pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
68 break;
69 }
70
71 ret = usb_control_msg(gspca_dev->dev, pipe,
72 req, req_type,
73 val, 0, gspca_dev->usb_buf, size, 500);
74
75 if ((ret < 0) && (req != 0x0a))
76 pr_err("usb_control_msg error %i, request = 0x%x, error = %i\n",
77 set, req, ret);
78
79 return ret;
80}
81
82static int stv0680_handle_error(struct gspca_dev *gspca_dev, int ret)
83{
84 stv_sndctrl(gspca_dev, 0, 0x80, 0, 0x02);
85 PERR("last error: %i, command = 0x%x",
86 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
87 return ret;
88}
89
90static int stv0680_get_video_mode(struct gspca_dev *gspca_dev)
91{
92
93 memset(gspca_dev->usb_buf, 0, 8);
94 gspca_dev->usb_buf[0] = 0x0f;
95
96 if (stv_sndctrl(gspca_dev, 0, 0x87, 0, 0x08) != 0x08) {
97 PERR("Get_Camera_Mode failed");
98 return stv0680_handle_error(gspca_dev, -EIO);
99 }
100
101 return gspca_dev->usb_buf[0];
102}
103
104static int stv0680_set_video_mode(struct gspca_dev *gspca_dev, u8 mode)
105{
106 struct sd *sd = (struct sd *) gspca_dev;
107
108 if (sd->current_mode == mode)
109 return 0;
110
111 memset(gspca_dev->usb_buf, 0, 8);
112 gspca_dev->usb_buf[0] = mode;
113
114 if (stv_sndctrl(gspca_dev, 3, 0x07, 0x0100, 0x08) != 0x08) {
115 PERR("Set_Camera_Mode failed");
116 return stv0680_handle_error(gspca_dev, -EIO);
117 }
118
119
120 if (stv0680_get_video_mode(gspca_dev) != mode) {
121 PERR("Error setting camera video mode!");
122 return -EIO;
123 }
124
125 sd->current_mode = mode;
126
127 return 0;
128}
129
130
131static int sd_config(struct gspca_dev *gspca_dev,
132 const struct usb_device_id *id)
133{
134 int ret;
135 struct sd *sd = (struct sd *) gspca_dev;
136 struct cam *cam = &gspca_dev->cam;
137
138
139
140 msleep(1000);
141
142
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 PERR("STV(e): camera ping failed!!");
146 return stv0680_handle_error(gspca_dev, -ENODEV);
147 }
148
149
150 if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x09) != 0x09)
151 return stv0680_handle_error(gspca_dev, -ENODEV);
152
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 PERR("Could not get descriptor 0200.");
156 return stv0680_handle_error(gspca_dev, -ENODEV);
157 }
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);
164
165 if (!(gspca_dev->usb_buf[7] & 0x09)) {
166 PERR("Camera supports neither CIF nor QVGA mode");
167 return -ENODEV;
168 }
169 if (gspca_dev->usb_buf[7] & 0x01)
170 PDEBUG(D_PROBE, "Camera supports CIF mode");
171 if (gspca_dev->usb_buf[7] & 0x02)
172 PDEBUG(D_PROBE, "Camera supports VGA mode");
173 if (gspca_dev->usb_buf[7] & 0x04)
174 PDEBUG(D_PROBE, "Camera supports QCIF mode");
175 if (gspca_dev->usb_buf[7] & 0x08)
176 PDEBUG(D_PROBE, "Camera supports QVGA mode");
177
178 if (gspca_dev->usb_buf[7] & 0x01)
179 sd->video_mode = 0x00;
180 else
181 sd->video_mode = 0x03;
182
183
184 PDEBUG(D_PROBE, "Firmware rev is %i.%i",
185 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
186 PDEBUG(D_PROBE, "ASIC rev is %i.%i",
187 gspca_dev->usb_buf[2], gspca_dev->usb_buf[3]);
188 PDEBUG(D_PROBE, "Sensor ID is %i",
189 (gspca_dev->usb_buf[4]*16) + (gspca_dev->usb_buf[5]>>4));
190
191
192 ret = stv0680_get_video_mode(gspca_dev);
193 if (ret < 0)
194 return ret;
195 sd->current_mode = sd->orig_mode = ret;
196
197 ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
198 if (ret < 0)
199 return ret;
200
201
202 if (stv_sndctrl(gspca_dev, 0, 0x8f, 0, 0x10) != 0x10)
203 return stv0680_handle_error(gspca_dev, -EIO);
204
205 cam->bulk = 1;
206 cam->bulk_nurbs = 1;
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]);
213 sd->mode.height = (gspca_dev->usb_buf[6] << 8) |
214 (gspca_dev->usb_buf[7]);
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;
220
221
222
223 cam->cam_mode = &sd->mode;
224 cam->nmodes = 1;
225
226
227 ret = stv0680_set_video_mode(gspca_dev, sd->orig_mode);
228 if (ret < 0)
229 return ret;
230
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);
235 }
236
237 return 0;
238}
239
240
241static int sd_init(struct gspca_dev *gspca_dev)
242{
243 return 0;
244}
245
246
247static int sd_start(struct gspca_dev *gspca_dev)
248{
249 int ret;
250 struct sd *sd = (struct sd *) gspca_dev;
251
252 ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
253 if (ret < 0)
254 return ret;
255
256 if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
257 return stv0680_handle_error(gspca_dev, -EIO);
258
259
260
261
262
263 if (stv_sndctrl(gspca_dev, 1, 0x09, sd->video_mode << 8, 0x0) != 0x0)
264 return stv0680_handle_error(gspca_dev, -EIO);
265
266 return 0;
267}
268
269static void sd_stopN(struct gspca_dev *gspca_dev)
270{
271
272 if (stv_sndctrl(gspca_dev, 1, 0x04, 0x0000, 0x0) != 0x0)
273 stv0680_handle_error(gspca_dev, -EIO);
274}
275
276static void sd_stop0(struct gspca_dev *gspca_dev)
277{
278 struct sd *sd = (struct sd *) gspca_dev;
279
280 if (!sd->gspca_dev.present)
281 return;
282
283 stv0680_set_video_mode(gspca_dev, sd->orig_mode);
284}
285
286static void sd_pkt_scan(struct gspca_dev *gspca_dev,
287 u8 *data,
288 int len)
289{
290 struct sd *sd = (struct sd *) gspca_dev;
291
292
293
294
295
296 if (len != sd->mode.sizeimage) {
297 gspca_dev->last_packet_type = DISCARD_PACKET;
298 return;
299 }
300
301
302
303
304 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
305
306
307 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
308}
309
310
311static const struct sd_desc sd_desc = {
312 .name = MODULE_NAME,
313 .config = sd_config,
314 .init = sd_init,
315 .start = sd_start,
316 .stopN = sd_stopN,
317 .stop0 = sd_stop0,
318 .pkt_scan = sd_pkt_scan,
319};
320
321
322static const struct usb_device_id device_table[] = {
323 {USB_DEVICE(0x0553, 0x0202)},
324 {USB_DEVICE(0x041e, 0x4007)},
325 {}
326};
327MODULE_DEVICE_TABLE(usb, device_table);
328
329
330static int sd_probe(struct usb_interface *intf,
331 const struct usb_device_id *id)
332{
333 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
334 THIS_MODULE);
335}
336
337static struct usb_driver sd_driver = {
338 .name = MODULE_NAME,
339 .id_table = device_table,
340 .probe = sd_probe,
341 .disconnect = gspca_disconnect,
342#ifdef CONFIG_PM
343 .suspend = gspca_suspend,
344 .resume = gspca_resume,
345 .reset_resume = gspca_resume,
346#endif
347};
348
349module_usb_driver(sd_driver);
350