blob: 070b9c33b5177a3f917633721f29f372740e0f70 [file] [log] [blame]
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -03001/*
2 * spca1528 subdriver
3 *
Jean-François Moine0dd82692011-09-23 04:19:03 -03004 * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
Joe Perches133a9fe2011-08-21 19:56:57 -030021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -030023#define MODULE_NAME "spca1528"
24
25#include "gspca.h"
26#include "jpeg.h"
27
28MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
30MODULE_LICENSE("GPL");
31
32/* specific webcam descriptor */
33struct sd {
34 struct gspca_dev gspca_dev; /* !! must be the first item */
35
36 u8 brightness;
37 u8 contrast;
38 u8 hue;
39 u8 color;
40 u8 sharpness;
41
42 u8 pkt_seq;
43
44 u8 jpeg_hdr[JPEG_HDR_SZ];
45};
46
47/* V4L2 controls supported by the driver */
48static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
49static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
50static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
51static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
52static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
53static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
54static int sd_setcolor(struct gspca_dev *gspca_dev, __s32 val);
55static int sd_getcolor(struct gspca_dev *gspca_dev, __s32 *val);
56static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
57static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
58
59static const struct ctrl sd_ctrls[] = {
60 {
61 {
62 .id = V4L2_CID_BRIGHTNESS,
63 .type = V4L2_CTRL_TYPE_INTEGER,
64 .name = "Brightness",
65 .minimum = 0,
66 .maximum = 255,
67 .step = 1,
68#define BRIGHTNESS_DEF 128
69 .default_value = BRIGHTNESS_DEF,
70 },
71 .set = sd_setbrightness,
72 .get = sd_getbrightness,
73 },
74 {
75 {
76 .id = V4L2_CID_CONTRAST,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Contrast",
79 .minimum = 0,
80 .maximum = 8,
81 .step = 1,
82#define CONTRAST_DEF 1
83 .default_value = CONTRAST_DEF,
84 },
85 .set = sd_setcontrast,
86 .get = sd_getcontrast,
87 },
88 {
89 {
90 .id = V4L2_CID_HUE,
91 .type = V4L2_CTRL_TYPE_INTEGER,
92 .name = "Hue",
93 .minimum = 0,
94 .maximum = 255,
95 .step = 1,
96#define HUE_DEF 0
97 .default_value = HUE_DEF,
98 },
99 .set = sd_sethue,
100 .get = sd_gethue,
101 },
102 {
103 {
104 .id = V4L2_CID_SATURATION,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Saturation",
107 .minimum = 0,
108 .maximum = 8,
109 .step = 1,
110#define COLOR_DEF 1
111 .default_value = COLOR_DEF,
112 },
113 .set = sd_setcolor,
114 .get = sd_getcolor,
115 },
116 {
117 {
118 .id = V4L2_CID_SHARPNESS,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 .name = "Sharpness",
121 .minimum = 0,
122 .maximum = 255,
123 .step = 1,
124#define SHARPNESS_DEF 0
125 .default_value = SHARPNESS_DEF,
126 },
127 .set = sd_setsharpness,
128 .get = sd_getsharpness,
129 },
130};
131
132static const struct v4l2_pix_format vga_mode[] = {
133/* (does not work correctly)
134 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
135 .bytesperline = 176,
136 .sizeimage = 176 * 144 * 5 / 8 + 590,
137 .colorspace = V4L2_COLORSPACE_JPEG,
138 .priv = 3},
139*/
140 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
141 .bytesperline = 320,
142 .sizeimage = 320 * 240 * 4 / 8 + 590,
143 .colorspace = V4L2_COLORSPACE_JPEG,
144 .priv = 2},
145 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
146 .bytesperline = 640,
147 .sizeimage = 640 * 480 * 3 / 8 + 590,
148 .colorspace = V4L2_COLORSPACE_JPEG,
149 .priv = 1},
150};
151
152/* read <len> bytes to gspca usb_buf */
153static void reg_r(struct gspca_dev *gspca_dev,
154 u8 req,
155 u16 index,
156 int len)
157{
158#if USB_BUF_SZ < 64
159#error "USB buffer too small"
160#endif
161 struct usb_device *dev = gspca_dev->dev;
162 int ret;
163
164 if (gspca_dev->usb_err < 0)
165 return;
166 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
167 req,
168 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
169 0x0000, /* value */
170 index,
171 gspca_dev->usb_buf, len,
172 500);
173 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index,
174 gspca_dev->usb_buf[0]);
175 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300176 pr_err("reg_r err %d\n", ret);
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300177 gspca_dev->usb_err = ret;
178 }
179}
180
181static void reg_w(struct gspca_dev *gspca_dev,
182 u8 req,
183 u16 value,
184 u16 index)
185{
186 struct usb_device *dev = gspca_dev->dev;
187 int ret;
188
189 if (gspca_dev->usb_err < 0)
190 return;
191 PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index);
192 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
193 req,
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
195 value, index,
196 NULL, 0, 500);
197 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300198 pr_err("reg_w err %d\n", ret);
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300199 gspca_dev->usb_err = ret;
200 }
201}
202
203static void reg_wb(struct gspca_dev *gspca_dev,
204 u8 req,
205 u16 value,
206 u16 index,
207 u8 byte)
208{
209 struct usb_device *dev = gspca_dev->dev;
210 int ret;
211
212 if (gspca_dev->usb_err < 0)
213 return;
214 PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
215 gspca_dev->usb_buf[0] = byte;
216 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
217 req,
218 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
219 value, index,
220 gspca_dev->usb_buf, 1, 500);
221 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300222 pr_err("reg_w err %d\n", ret);
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300223 gspca_dev->usb_err = ret;
224 }
225}
226
227static void wait_status_0(struct gspca_dev *gspca_dev)
228{
Jean-François Moine9ba4a4b2011-09-23 04:15:50 -0300229 int i, w;
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300230
Jean-François Moine9ba4a4b2011-09-23 04:15:50 -0300231 i = 16;
232 w = 0;
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300233 do {
234 reg_r(gspca_dev, 0x21, 0x0000, 1);
235 if (gspca_dev->usb_buf[0] == 0)
236 return;
Jean-François Moine9ba4a4b2011-09-23 04:15:50 -0300237 w += 15;
238 msleep(w);
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300239 } while (--i > 0);
240 PDEBUG(D_ERR, "wait_status_0 timeout");
241 gspca_dev->usb_err = -ETIME;
242}
243
244static void wait_status_1(struct gspca_dev *gspca_dev)
245{
246 int i;
247
248 i = 10;
249 do {
250 reg_r(gspca_dev, 0x21, 0x0001, 1);
251 msleep(10);
252 if (gspca_dev->usb_buf[0] == 1) {
253 reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
254 reg_r(gspca_dev, 0x21, 0x0001, 1);
255 return;
256 }
257 } while (--i > 0);
258 PDEBUG(D_ERR, "wait_status_1 timeout");
259 gspca_dev->usb_err = -ETIME;
260}
261
262static void setbrightness(struct gspca_dev *gspca_dev)
263{
264 struct sd *sd = (struct sd *) gspca_dev;
265
266 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, sd->brightness);
267}
268
269static void setcontrast(struct gspca_dev *gspca_dev)
270{
271 struct sd *sd = (struct sd *) gspca_dev;
272
273 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, sd->contrast);
274}
275
276static void sethue(struct gspca_dev *gspca_dev)
277{
278 struct sd *sd = (struct sd *) gspca_dev;
279
280 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, sd->hue);
281}
282
283static void setcolor(struct gspca_dev *gspca_dev)
284{
285 struct sd *sd = (struct sd *) gspca_dev;
286
287 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, sd->color);
288}
289
290static void setsharpness(struct gspca_dev *gspca_dev)
291{
292 struct sd *sd = (struct sd *) gspca_dev;
293
294 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, sd->sharpness);
295}
296
297/* this function is called at probe time */
298static int sd_config(struct gspca_dev *gspca_dev,
299 const struct usb_device_id *id)
300{
301 struct sd *sd = (struct sd *) gspca_dev;
302
303 gspca_dev->cam.cam_mode = vga_mode;
304 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
305 gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
306 /*fixme: 256 in ms-win traces*/
307
308 sd->brightness = BRIGHTNESS_DEF;
309 sd->contrast = CONTRAST_DEF;
310 sd->hue = HUE_DEF;
311 sd->color = COLOR_DEF;
312 sd->sharpness = SHARPNESS_DEF;
313
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300314 return 0;
315}
316
317/* this function is called at probe and resume time */
318static int sd_init(struct gspca_dev *gspca_dev)
319{
320 reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
321 reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
322 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
323 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
324 msleep(8);
325 reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
326 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
327 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
328
329 reg_r(gspca_dev, 0x20, 0x0000, 1);
330 reg_r(gspca_dev, 0x20, 0x0000, 5);
331 reg_r(gspca_dev, 0x23, 0x0000, 64);
332 PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
333 &gspca_dev->usb_buf[0x30]);
334 reg_r(gspca_dev, 0x23, 0x0001, 64);
335 return gspca_dev->usb_err;
336}
337
338/* function called at start time before URB creation */
339static int sd_isoc_init(struct gspca_dev *gspca_dev)
340{
341 u8 mode;
342
343 reg_r(gspca_dev, 0x00, 0x2520, 1);
344 wait_status_0(gspca_dev);
345 reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
346 wait_status_1(gspca_dev);
347
348 wait_status_0(gspca_dev);
349 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
350 reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
351 reg_r(gspca_dev, 0x25, 0x0004, 1);
Jean-François Moine0dd82692011-09-23 04:19:03 -0300352 reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300353 reg_r(gspca_dev, 0x27, 0x0000, 1);
Jean-François Moine6507b422011-09-23 04:23:52 -0300354
355/* not useful..
356 gspca_dev->alt = 4; * use alternate setting 3 */
357
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300358 return gspca_dev->usb_err;
359}
360
361/* -- start the camera -- */
362static int sd_start(struct gspca_dev *gspca_dev)
363{
364 struct sd *sd = (struct sd *) gspca_dev;
365
366 /* initialize the JPEG header */
367 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
368 0x22); /* JPEG 411 */
369
Jean-François Moine3e129502011-09-23 04:20:29 -0300370 /* the JPEG quality shall be 85% */
371 jpeg_set_qual(sd->jpeg_hdr, 85);
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300372
373 /* set the controls */
374 setbrightness(gspca_dev);
375 setcontrast(gspca_dev);
376 sethue(gspca_dev);
377 setcolor(gspca_dev);
378 setsharpness(gspca_dev);
379
380 msleep(5);
381 reg_r(gspca_dev, 0x00, 0x2520, 1);
382 msleep(8);
383
384 /* start the capture */
385 wait_status_0(gspca_dev);
Jean-François Moine0dd82692011-09-23 04:19:03 -0300386 reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300387 wait_status_1(gspca_dev);
388 wait_status_0(gspca_dev);
389 msleep(200);
390
391 sd->pkt_seq = 0;
392 return gspca_dev->usb_err;
393}
394
395static void sd_stopN(struct gspca_dev *gspca_dev)
396{
397 /* stop the capture */
398 wait_status_0(gspca_dev);
Jean-François Moine0dd82692011-09-23 04:19:03 -0300399 reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300400 wait_status_1(gspca_dev);
401 wait_status_0(gspca_dev);
402}
403
404/* move a packet adding 0x00 after 0xff */
405static void add_packet(struct gspca_dev *gspca_dev,
406 u8 *data,
407 int len)
408{
409 int i;
410
411 i = 0;
412 do {
413 if (data[i] == 0xff) {
414 gspca_frame_add(gspca_dev, INTER_PACKET,
415 data, i + 1);
416 len -= i;
417 data += i;
418 *data = 0x00;
419 i = 0;
420 }
421 } while (++i < len);
422 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
423}
424
425static void sd_pkt_scan(struct gspca_dev *gspca_dev,
426 u8 *data, /* isoc packet */
427 int len) /* iso packet length */
428{
429 struct sd *sd = (struct sd *) gspca_dev;
430 static const u8 ffd9[] = {0xff, 0xd9};
431
432 /* image packets start with:
433 * 02 8n
434 * with <n> bit:
435 * 0x01: even (0) / odd (1) image
436 * 0x02: end of image when set
437 */
438 if (len < 3)
439 return; /* empty packet */
440 if (*data == 0x02) {
441 if (data[1] & 0x02) {
442 sd->pkt_seq = !(data[1] & 1);
443 add_packet(gspca_dev, data + 2, len - 2);
444 gspca_frame_add(gspca_dev, LAST_PACKET,
445 ffd9, 2);
446 return;
447 }
448 if ((data[1] & 1) != sd->pkt_seq)
449 goto err;
450 if (gspca_dev->last_packet_type == LAST_PACKET)
451 gspca_frame_add(gspca_dev, FIRST_PACKET,
452 sd->jpeg_hdr, JPEG_HDR_SZ);
453 add_packet(gspca_dev, data + 2, len - 2);
454 return;
455 }
456err:
457 gspca_dev->last_packet_type = DISCARD_PACKET;
458}
459
460static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
461{
462 struct sd *sd = (struct sd *) gspca_dev;
463
464 sd->brightness = val;
465 if (gspca_dev->streaming)
466 setbrightness(gspca_dev);
467 return gspca_dev->usb_err;
468}
469
470static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
471{
472 struct sd *sd = (struct sd *) gspca_dev;
473
474 *val = sd->brightness;
475 return 0;
476}
477
478static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
479{
480 struct sd *sd = (struct sd *) gspca_dev;
481
482 sd->contrast = val;
483 if (gspca_dev->streaming)
484 setcontrast(gspca_dev);
485 return gspca_dev->usb_err;
486}
487
488static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
489{
490 struct sd *sd = (struct sd *) gspca_dev;
491
492 *val = sd->contrast;
493 return 0;
494}
495
496static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
497{
498 struct sd *sd = (struct sd *) gspca_dev;
499
500 sd->hue = val;
501 if (gspca_dev->streaming)
502 sethue(gspca_dev);
503 return gspca_dev->usb_err;
504}
505
506static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
507{
508 struct sd *sd = (struct sd *) gspca_dev;
509
510 *val = sd->hue;
511 return 0;
512}
513
514static int sd_setcolor(struct gspca_dev *gspca_dev, __s32 val)
515{
516 struct sd *sd = (struct sd *) gspca_dev;
517
518 sd->color = val;
519 if (gspca_dev->streaming)
520 setcolor(gspca_dev);
521 return gspca_dev->usb_err;
522}
523
524static int sd_getcolor(struct gspca_dev *gspca_dev, __s32 *val)
525{
526 struct sd *sd = (struct sd *) gspca_dev;
527
528 *val = sd->color;
529 return 0;
530}
531
532static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
533{
534 struct sd *sd = (struct sd *) gspca_dev;
535
536 sd->sharpness = val;
537 if (gspca_dev->streaming)
538 setsharpness(gspca_dev);
539 return gspca_dev->usb_err;
540}
541
542static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
543{
544 struct sd *sd = (struct sd *) gspca_dev;
545
546 *val = sd->sharpness;
547 return 0;
548}
549
550/* sub-driver description */
551static const struct sd_desc sd_desc = {
552 .name = MODULE_NAME,
553 .ctrls = sd_ctrls,
554 .nctrls = ARRAY_SIZE(sd_ctrls),
555 .config = sd_config,
556 .init = sd_init,
557 .isoc_init = sd_isoc_init,
558 .start = sd_start,
559 .stopN = sd_stopN,
560 .pkt_scan = sd_pkt_scan,
561};
562
563/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300564static const struct usb_device_id device_table[] = {
Jean-François Moine5b0ff8c2010-06-05 07:57:56 -0300565 {USB_DEVICE(0x04fc, 0x1528)},
566 {}
567};
568MODULE_DEVICE_TABLE(usb, device_table);
569
570/* -- device connect -- */
571static int sd_probe(struct usb_interface *intf,
572 const struct usb_device_id *id)
573{
574 /* the video interface for isochronous transfer is 1 */
575 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
576 return -ENODEV;
577
578 return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
579 THIS_MODULE);
580}
581
582static struct usb_driver sd_driver = {
583 .name = MODULE_NAME,
584 .id_table = device_table,
585 .probe = sd_probe,
586 .disconnect = gspca_disconnect,
587#ifdef CONFIG_PM
588 .suspend = gspca_suspend,
589 .resume = gspca_resume,
590#endif
591};
592
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800593module_usb_driver(sd_driver);