4 * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
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
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.
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
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #define MODULE_NAME "spca1528"
28 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
30 MODULE_LICENSE("GPL");
32 /* specific webcam descriptor */
34 struct gspca_dev gspca_dev
; /* !! must be the first item */
44 u8 jpeg_hdr
[JPEG_HDR_SZ
];
47 /* V4L2 controls supported by the driver */
48 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
);
49 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
50 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
);
51 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
);
52 static int sd_sethue(struct gspca_dev
*gspca_dev
, __s32 val
);
53 static int sd_gethue(struct gspca_dev
*gspca_dev
, __s32
*val
);
54 static int sd_setcolor(struct gspca_dev
*gspca_dev
, __s32 val
);
55 static int sd_getcolor(struct gspca_dev
*gspca_dev
, __s32
*val
);
56 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
);
57 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
);
59 static const struct ctrl sd_ctrls
[] = {
62 .id
= V4L2_CID_BRIGHTNESS
,
63 .type
= V4L2_CTRL_TYPE_INTEGER
,
68 #define BRIGHTNESS_DEF 128
69 .default_value
= BRIGHTNESS_DEF
,
71 .set
= sd_setbrightness
,
72 .get
= sd_getbrightness
,
76 .id
= V4L2_CID_CONTRAST
,
77 .type
= V4L2_CTRL_TYPE_INTEGER
,
82 #define CONTRAST_DEF 1
83 .default_value
= CONTRAST_DEF
,
85 .set
= sd_setcontrast
,
86 .get
= sd_getcontrast
,
91 .type
= V4L2_CTRL_TYPE_INTEGER
,
97 .default_value
= HUE_DEF
,
104 .id
= V4L2_CID_SATURATION
,
105 .type
= V4L2_CTRL_TYPE_INTEGER
,
106 .name
= "Saturation",
111 .default_value
= COLOR_DEF
,
118 .id
= V4L2_CID_SHARPNESS
,
119 .type
= V4L2_CTRL_TYPE_INTEGER
,
124 #define SHARPNESS_DEF 0
125 .default_value
= SHARPNESS_DEF
,
127 .set
= sd_setsharpness
,
128 .get
= sd_getsharpness
,
132 static const struct v4l2_pix_format vga_mode
[] = {
133 /* (does not work correctly)
134 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
136 .sizeimage = 176 * 144 * 5 / 8 + 590,
137 .colorspace = V4L2_COLORSPACE_JPEG,
140 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
142 .sizeimage
= 320 * 240 * 4 / 8 + 590,
143 .colorspace
= V4L2_COLORSPACE_JPEG
,
145 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
147 .sizeimage
= 640 * 480 * 3 / 8 + 590,
148 .colorspace
= V4L2_COLORSPACE_JPEG
,
152 /* read <len> bytes to gspca usb_buf */
153 static void reg_r(struct gspca_dev
*gspca_dev
,
159 #error "USB buffer too small"
161 struct usb_device
*dev
= gspca_dev
->dev
;
164 if (gspca_dev
->usb_err
< 0)
166 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
168 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
171 gspca_dev
->usb_buf
, len
,
173 PDEBUG(D_USBI
, "GET %02x 0000 %04x %02x", req
, index
,
174 gspca_dev
->usb_buf
[0]);
176 pr_err("reg_r err %d\n", ret
);
177 gspca_dev
->usb_err
= ret
;
181 static void reg_w(struct gspca_dev
*gspca_dev
,
186 struct usb_device
*dev
= gspca_dev
->dev
;
189 if (gspca_dev
->usb_err
< 0)
191 PDEBUG(D_USBO
, "SET %02x %04x %04x", req
, value
, index
);
192 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
194 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
198 pr_err("reg_w err %d\n", ret
);
199 gspca_dev
->usb_err
= ret
;
203 static void reg_wb(struct gspca_dev
*gspca_dev
,
209 struct usb_device
*dev
= gspca_dev
->dev
;
212 if (gspca_dev
->usb_err
< 0)
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),
218 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
220 gspca_dev
->usb_buf
, 1, 500);
222 pr_err("reg_w err %d\n", ret
);
223 gspca_dev
->usb_err
= ret
;
227 static void wait_status_0(struct gspca_dev
*gspca_dev
)
234 reg_r(gspca_dev
, 0x21, 0x0000, 1);
235 if (gspca_dev
->usb_buf
[0] == 0)
240 PDEBUG(D_ERR
, "wait_status_0 timeout");
241 gspca_dev
->usb_err
= -ETIME
;
244 static void wait_status_1(struct gspca_dev
*gspca_dev
)
250 reg_r(gspca_dev
, 0x21, 0x0001, 1);
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);
258 PDEBUG(D_ERR
, "wait_status_1 timeout");
259 gspca_dev
->usb_err
= -ETIME
;
262 static void setbrightness(struct gspca_dev
*gspca_dev
)
264 struct sd
*sd
= (struct sd
*) gspca_dev
;
266 reg_wb(gspca_dev
, 0xc0, 0x0000, 0x00c0, sd
->brightness
);
269 static void setcontrast(struct gspca_dev
*gspca_dev
)
271 struct sd
*sd
= (struct sd
*) gspca_dev
;
273 reg_wb(gspca_dev
, 0xc1, 0x0000, 0x00c1, sd
->contrast
);
276 static void sethue(struct gspca_dev
*gspca_dev
)
278 struct sd
*sd
= (struct sd
*) gspca_dev
;
280 reg_wb(gspca_dev
, 0xc2, 0x0000, 0x0000, sd
->hue
);
283 static void setcolor(struct gspca_dev
*gspca_dev
)
285 struct sd
*sd
= (struct sd
*) gspca_dev
;
287 reg_wb(gspca_dev
, 0xc3, 0x0000, 0x00c3, sd
->color
);
290 static void setsharpness(struct gspca_dev
*gspca_dev
)
292 struct sd
*sd
= (struct sd
*) gspca_dev
;
294 reg_wb(gspca_dev
, 0xc4, 0x0000, 0x00c4, sd
->sharpness
);
297 /* this function is called at probe time */
298 static int sd_config(struct gspca_dev
*gspca_dev
,
299 const struct usb_device_id
*id
)
301 struct sd
*sd
= (struct sd
*) gspca_dev
;
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*/
308 sd
->brightness
= BRIGHTNESS_DEF
;
309 sd
->contrast
= CONTRAST_DEF
;
311 sd
->color
= COLOR_DEF
;
312 sd
->sharpness
= SHARPNESS_DEF
;
317 /* this function is called at probe and resume time */
318 static int sd_init(struct gspca_dev
*gspca_dev
)
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);
325 reg_w(gspca_dev
, 0x00, 0x00c0, 0x206b);
326 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
327 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
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
;
338 /* function called at start time before URB creation */
339 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
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
);
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);
352 reg_wb(gspca_dev
, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
353 reg_r(gspca_dev
, 0x27, 0x0000, 1);
356 gspca_dev->alt = 4; * use alternate setting 3 */
358 return gspca_dev
->usb_err
;
361 /* -- start the camera -- */
362 static int sd_start(struct gspca_dev
*gspca_dev
)
364 struct sd
*sd
= (struct sd
*) gspca_dev
;
366 /* initialize the JPEG header */
367 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
368 0x22); /* JPEG 411 */
370 /* the JPEG quality shall be 85% */
371 jpeg_set_qual(sd
->jpeg_hdr
, 85);
373 /* set the controls */
374 setbrightness(gspca_dev
);
375 setcontrast(gspca_dev
);
378 setsharpness(gspca_dev
);
381 reg_r(gspca_dev
, 0x00, 0x2520, 1);
384 /* start the capture */
385 wait_status_0(gspca_dev
);
386 reg_w(gspca_dev
, 0x31, 0x0000, 0x0004); /* start request */
387 wait_status_1(gspca_dev
);
388 wait_status_0(gspca_dev
);
392 return gspca_dev
->usb_err
;
395 static void sd_stopN(struct gspca_dev
*gspca_dev
)
397 /* stop the capture */
398 wait_status_0(gspca_dev
);
399 reg_w(gspca_dev
, 0x31, 0x0000, 0x0000); /* stop request */
400 wait_status_1(gspca_dev
);
401 wait_status_0(gspca_dev
);
404 /* move a packet adding 0x00 after 0xff */
405 static void add_packet(struct gspca_dev
*gspca_dev
,
413 if (data
[i
] == 0xff) {
414 gspca_frame_add(gspca_dev
, INTER_PACKET
,
422 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
425 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
426 u8
*data
, /* isoc packet */
427 int len
) /* iso packet length */
429 struct sd
*sd
= (struct sd
*) gspca_dev
;
430 static const u8 ffd9
[] = {0xff, 0xd9};
432 /* image packets start with:
435 * 0x01: even (0) / odd (1) image
436 * 0x02: end of image when set
439 return; /* empty packet */
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
,
448 if ((data
[1] & 1) != sd
->pkt_seq
)
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);
457 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
460 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
)
462 struct sd
*sd
= (struct sd
*) gspca_dev
;
464 sd
->brightness
= val
;
465 if (gspca_dev
->streaming
)
466 setbrightness(gspca_dev
);
467 return gspca_dev
->usb_err
;
470 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
472 struct sd
*sd
= (struct sd
*) gspca_dev
;
474 *val
= sd
->brightness
;
478 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
)
480 struct sd
*sd
= (struct sd
*) gspca_dev
;
483 if (gspca_dev
->streaming
)
484 setcontrast(gspca_dev
);
485 return gspca_dev
->usb_err
;
488 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
)
490 struct sd
*sd
= (struct sd
*) gspca_dev
;
496 static int sd_sethue(struct gspca_dev
*gspca_dev
, __s32 val
)
498 struct sd
*sd
= (struct sd
*) gspca_dev
;
501 if (gspca_dev
->streaming
)
503 return gspca_dev
->usb_err
;
506 static int sd_gethue(struct gspca_dev
*gspca_dev
, __s32
*val
)
508 struct sd
*sd
= (struct sd
*) gspca_dev
;
514 static int sd_setcolor(struct gspca_dev
*gspca_dev
, __s32 val
)
516 struct sd
*sd
= (struct sd
*) gspca_dev
;
519 if (gspca_dev
->streaming
)
521 return gspca_dev
->usb_err
;
524 static int sd_getcolor(struct gspca_dev
*gspca_dev
, __s32
*val
)
526 struct sd
*sd
= (struct sd
*) gspca_dev
;
532 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
)
534 struct sd
*sd
= (struct sd
*) gspca_dev
;
537 if (gspca_dev
->streaming
)
538 setsharpness(gspca_dev
);
539 return gspca_dev
->usb_err
;
542 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
)
544 struct sd
*sd
= (struct sd
*) gspca_dev
;
546 *val
= sd
->sharpness
;
550 /* sub-driver description */
551 static const struct sd_desc sd_desc
= {
554 .nctrls
= ARRAY_SIZE(sd_ctrls
),
557 .isoc_init
= sd_isoc_init
,
560 .pkt_scan
= sd_pkt_scan
,
563 /* -- module initialisation -- */
564 static const struct usb_device_id device_table
[] = {
565 {USB_DEVICE(0x04fc, 0x1528)},
568 MODULE_DEVICE_TABLE(usb
, device_table
);
570 /* -- device connect -- */
571 static int sd_probe(struct usb_interface
*intf
,
572 const struct usb_device_id
*id
)
574 /* the video interface for isochronous transfer is 1 */
575 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
578 return gspca_dev_probe2(intf
, id
, &sd_desc
, sizeof(struct sd
),
582 static struct usb_driver sd_driver
= {
584 .id_table
= device_table
,
586 .disconnect
= gspca_disconnect
,
588 .suspend
= gspca_suspend
,
589 .resume
= gspca_resume
,
593 module_usb_driver(sd_driver
);