1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#define MODULE_NAME "spca1528"
20
21#include "gspca.h"
22#include "jpeg.h"
23
24MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
25MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
26MODULE_LICENSE("GPL");
27
28
29struct sd {
30 struct gspca_dev gspca_dev;
31
32 u8 pkt_seq;
33
34 u8 jpeg_hdr[JPEG_HDR_SZ];
35};
36
37static const struct v4l2_pix_format vga_mode[] = {
38
39
40
41
42
43
44
45 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
46 .bytesperline = 320,
47 .sizeimage = 320 * 240 * 4 / 8 + 590,
48 .colorspace = V4L2_COLORSPACE_JPEG,
49 .priv = 2},
50 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51 .bytesperline = 640,
52 .sizeimage = 640 * 480 * 3 / 8 + 590,
53 .colorspace = V4L2_COLORSPACE_JPEG,
54 .priv = 1},
55};
56
57
58static void reg_r(struct gspca_dev *gspca_dev,
59 u8 req,
60 u16 index,
61 int len)
62{
63#if USB_BUF_SZ < 64
64#error "USB buffer too small"
65#endif
66 struct usb_device *dev = gspca_dev->dev;
67 int ret;
68
69 if (gspca_dev->usb_err < 0)
70 return;
71 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
72 req,
73 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
74 0x0000,
75 index,
76 gspca_dev->usb_buf, len,
77 500);
78 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index,
79 gspca_dev->usb_buf[0]);
80 if (ret < 0) {
81 pr_err("reg_r err %d\n", ret);
82 gspca_dev->usb_err = ret;
83 }
84}
85
86static void reg_w(struct gspca_dev *gspca_dev,
87 u8 req,
88 u16 value,
89 u16 index)
90{
91 struct usb_device *dev = gspca_dev->dev;
92 int ret;
93
94 if (gspca_dev->usb_err < 0)
95 return;
96 PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index);
97 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
98 req,
99 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
100 value, index,
101 NULL, 0, 500);
102 if (ret < 0) {
103 pr_err("reg_w err %d\n", ret);
104 gspca_dev->usb_err = ret;
105 }
106}
107
108static void reg_wb(struct gspca_dev *gspca_dev,
109 u8 req,
110 u16 value,
111 u16 index,
112 u8 byte)
113{
114 struct usb_device *dev = gspca_dev->dev;
115 int ret;
116
117 if (gspca_dev->usb_err < 0)
118 return;
119 PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
120 gspca_dev->usb_buf[0] = byte;
121 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
122 req,
123 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124 value, index,
125 gspca_dev->usb_buf, 1, 500);
126 if (ret < 0) {
127 pr_err("reg_w err %d\n", ret);
128 gspca_dev->usb_err = ret;
129 }
130}
131
132static void wait_status_0(struct gspca_dev *gspca_dev)
133{
134 int i, w;
135
136 i = 16;
137 w = 0;
138 do {
139 reg_r(gspca_dev, 0x21, 0x0000, 1);
140 if (gspca_dev->usb_buf[0] == 0)
141 return;
142 w += 15;
143 msleep(w);
144 } while (--i > 0);
145 PERR("wait_status_0 timeout");
146 gspca_dev->usb_err = -ETIME;
147}
148
149static void wait_status_1(struct gspca_dev *gspca_dev)
150{
151 int i;
152
153 i = 10;
154 do {
155 reg_r(gspca_dev, 0x21, 0x0001, 1);
156 msleep(10);
157 if (gspca_dev->usb_buf[0] == 1) {
158 reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
159 reg_r(gspca_dev, 0x21, 0x0001, 1);
160 return;
161 }
162 } while (--i > 0);
163 PERR("wait_status_1 timeout");
164 gspca_dev->usb_err = -ETIME;
165}
166
167static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
168{
169 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
170}
171
172static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
173{
174 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
175}
176
177static void sethue(struct gspca_dev *gspca_dev, s32 val)
178{
179 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
180}
181
182static void setcolor(struct gspca_dev *gspca_dev, s32 val)
183{
184 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
185}
186
187static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
188{
189 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
190}
191
192
193static int sd_config(struct gspca_dev *gspca_dev,
194 const struct usb_device_id *id)
195{
196 gspca_dev->cam.cam_mode = vga_mode;
197 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
198 gspca_dev->cam.npkt = 128;
199
200
201 return 0;
202}
203
204
205static int sd_init(struct gspca_dev *gspca_dev)
206{
207 reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
208 reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
209 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
210 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
211 msleep(8);
212 reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
213 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
214 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
215
216 reg_r(gspca_dev, 0x20, 0x0000, 1);
217 reg_r(gspca_dev, 0x20, 0x0000, 5);
218 reg_r(gspca_dev, 0x23, 0x0000, 64);
219 PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
220 &gspca_dev->usb_buf[0x30]);
221 reg_r(gspca_dev, 0x23, 0x0001, 64);
222 return gspca_dev->usb_err;
223}
224
225
226static int sd_isoc_init(struct gspca_dev *gspca_dev)
227{
228 u8 mode;
229
230 reg_r(gspca_dev, 0x00, 0x2520, 1);
231 wait_status_0(gspca_dev);
232 reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
233 wait_status_1(gspca_dev);
234
235 wait_status_0(gspca_dev);
236 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
237 reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
238 reg_r(gspca_dev, 0x25, 0x0004, 1);
239 reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06);
240 reg_r(gspca_dev, 0x27, 0x0000, 1);
241
242
243
244
245 return gspca_dev->usb_err;
246}
247
248
249static int sd_start(struct gspca_dev *gspca_dev)
250{
251 struct sd *sd = (struct sd *) gspca_dev;
252
253
254 jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
255 gspca_dev->pixfmt.width,
256 0x22);
257
258
259 jpeg_set_qual(sd->jpeg_hdr, 85);
260
261 reg_r(gspca_dev, 0x00, 0x2520, 1);
262 msleep(8);
263
264
265 wait_status_0(gspca_dev);
266 reg_w(gspca_dev, 0x31, 0x0000, 0x0004);
267 wait_status_1(gspca_dev);
268 wait_status_0(gspca_dev);
269 msleep(200);
270
271 sd->pkt_seq = 0;
272 return gspca_dev->usb_err;
273}
274
275static void sd_stopN(struct gspca_dev *gspca_dev)
276{
277
278 wait_status_0(gspca_dev);
279 reg_w(gspca_dev, 0x31, 0x0000, 0x0000);
280 wait_status_1(gspca_dev);
281 wait_status_0(gspca_dev);
282}
283
284
285static void add_packet(struct gspca_dev *gspca_dev,
286 u8 *data,
287 int len)
288{
289 int i;
290
291 i = 0;
292 do {
293 if (data[i] == 0xff) {
294 gspca_frame_add(gspca_dev, INTER_PACKET,
295 data, i + 1);
296 len -= i;
297 data += i;
298 *data = 0x00;
299 i = 0;
300 }
301 } while (++i < len);
302 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
303}
304
305static void sd_pkt_scan(struct gspca_dev *gspca_dev,
306 u8 *data,
307 int len)
308{
309 struct sd *sd = (struct sd *) gspca_dev;
310 static const u8 ffd9[] = {0xff, 0xd9};
311
312
313
314
315
316
317
318 if (len < 3)
319 return;
320 if (*data == 0x02) {
321 if (data[1] & 0x02) {
322 sd->pkt_seq = !(data[1] & 1);
323 add_packet(gspca_dev, data + 2, len - 2);
324 gspca_frame_add(gspca_dev, LAST_PACKET,
325 ffd9, 2);
326 return;
327 }
328 if ((data[1] & 1) != sd->pkt_seq)
329 goto err;
330 if (gspca_dev->last_packet_type == LAST_PACKET)
331 gspca_frame_add(gspca_dev, FIRST_PACKET,
332 sd->jpeg_hdr, JPEG_HDR_SZ);
333 add_packet(gspca_dev, data + 2, len - 2);
334 return;
335 }
336err:
337 gspca_dev->last_packet_type = DISCARD_PACKET;
338}
339
340static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
341{
342 struct gspca_dev *gspca_dev =
343 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
344
345 gspca_dev->usb_err = 0;
346
347 if (!gspca_dev->streaming)
348 return 0;
349
350 switch (ctrl->id) {
351 case V4L2_CID_BRIGHTNESS:
352 setbrightness(gspca_dev, ctrl->val);
353 break;
354 case V4L2_CID_CONTRAST:
355 setcontrast(gspca_dev, ctrl->val);
356 break;
357 case V4L2_CID_HUE:
358 sethue(gspca_dev, ctrl->val);
359 break;
360 case V4L2_CID_SATURATION:
361 setcolor(gspca_dev, ctrl->val);
362 break;
363 case V4L2_CID_SHARPNESS:
364 setsharpness(gspca_dev, ctrl->val);
365 break;
366 }
367 return gspca_dev->usb_err;
368}
369
370static const struct v4l2_ctrl_ops sd_ctrl_ops = {
371 .s_ctrl = sd_s_ctrl,
372};
373
374static int sd_init_controls(struct gspca_dev *gspca_dev)
375{
376 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
377
378 gspca_dev->vdev.ctrl_handler = hdl;
379 v4l2_ctrl_handler_init(hdl, 5);
380 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
381 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
382 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
383 V4L2_CID_CONTRAST, 0, 8, 1, 1);
384 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
385 V4L2_CID_HUE, 0, 255, 1, 0);
386 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
387 V4L2_CID_SATURATION, 0, 8, 1, 1);
388 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
389 V4L2_CID_SHARPNESS, 0, 255, 1, 0);
390
391 if (hdl->error) {
392 pr_err("Could not initialize controls\n");
393 return hdl->error;
394 }
395 return 0;
396}
397
398
399static const struct sd_desc sd_desc = {
400 .name = MODULE_NAME,
401 .config = sd_config,
402 .init = sd_init,
403 .init_controls = sd_init_controls,
404 .isoc_init = sd_isoc_init,
405 .start = sd_start,
406 .stopN = sd_stopN,
407 .pkt_scan = sd_pkt_scan,
408};
409
410
411static const struct usb_device_id device_table[] = {
412 {USB_DEVICE(0x04fc, 0x1528)},
413 {}
414};
415MODULE_DEVICE_TABLE(usb, device_table);
416
417
418static int sd_probe(struct usb_interface *intf,
419 const struct usb_device_id *id)
420{
421
422 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
423 return -ENODEV;
424
425 return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
426 THIS_MODULE);
427}
428
429static struct usb_driver sd_driver = {
430 .name = MODULE_NAME,
431 .id_table = device_table,
432 .probe = sd_probe,
433 .disconnect = gspca_disconnect,
434#ifdef CONFIG_PM
435 .suspend = gspca_suspend,
436 .resume = gspca_resume,
437 .reset_resume = gspca_resume,
438#endif
439};
440
441module_usb_driver(sd_driver);
442