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