4 * Copyright (C) 2010 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 MODULE_NAME "spca1528"
26 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
27 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
28 MODULE_LICENSE("GPL");
30 /* specific webcam descriptor */
32 struct gspca_dev gspca_dev
; /* !! must be the first item */
42 u8 jpeg_hdr
[JPEG_HDR_SZ
];
45 /* V4L2 controls supported by the driver */
46 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
);
47 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
48 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
);
49 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
);
50 static int sd_sethue(struct gspca_dev
*gspca_dev
, __s32 val
);
51 static int sd_gethue(struct gspca_dev
*gspca_dev
, __s32
*val
);
52 static int sd_setcolor(struct gspca_dev
*gspca_dev
, __s32 val
);
53 static int sd_getcolor(struct gspca_dev
*gspca_dev
, __s32
*val
);
54 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
);
55 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
);
57 static const struct ctrl sd_ctrls
[] = {
60 .id
= V4L2_CID_BRIGHTNESS
,
61 .type
= V4L2_CTRL_TYPE_INTEGER
,
66 #define BRIGHTNESS_DEF 128
67 .default_value
= BRIGHTNESS_DEF
,
69 .set
= sd_setbrightness
,
70 .get
= sd_getbrightness
,
74 .id
= V4L2_CID_CONTRAST
,
75 .type
= V4L2_CTRL_TYPE_INTEGER
,
80 #define CONTRAST_DEF 1
81 .default_value
= CONTRAST_DEF
,
83 .set
= sd_setcontrast
,
84 .get
= sd_getcontrast
,
89 .type
= V4L2_CTRL_TYPE_INTEGER
,
95 .default_value
= HUE_DEF
,
102 .id
= V4L2_CID_SATURATION
,
103 .type
= V4L2_CTRL_TYPE_INTEGER
,
104 .name
= "Saturation",
109 .default_value
= COLOR_DEF
,
116 .id
= V4L2_CID_SHARPNESS
,
117 .type
= V4L2_CTRL_TYPE_INTEGER
,
122 #define SHARPNESS_DEF 0
123 .default_value
= SHARPNESS_DEF
,
125 .set
= sd_setsharpness
,
126 .get
= sd_getsharpness
,
130 static const struct v4l2_pix_format vga_mode
[] = {
131 /* (does not work correctly)
132 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
134 .sizeimage = 176 * 144 * 5 / 8 + 590,
135 .colorspace = V4L2_COLORSPACE_JPEG,
138 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
140 .sizeimage
= 320 * 240 * 4 / 8 + 590,
141 .colorspace
= V4L2_COLORSPACE_JPEG
,
143 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
145 .sizeimage
= 640 * 480 * 3 / 8 + 590,
146 .colorspace
= V4L2_COLORSPACE_JPEG
,
150 /* read <len> bytes to gspca usb_buf */
151 static void reg_r(struct gspca_dev
*gspca_dev
,
157 #error "USB buffer too small"
159 struct usb_device
*dev
= gspca_dev
->dev
;
162 if (gspca_dev
->usb_err
< 0)
164 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
166 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
169 gspca_dev
->usb_buf
, len
,
171 PDEBUG(D_USBI
, "GET %02x 0000 %04x %02x", req
, index
,
172 gspca_dev
->usb_buf
[0]);
174 err("reg_r err %d", ret
);
175 gspca_dev
->usb_err
= ret
;
179 static void reg_w(struct gspca_dev
*gspca_dev
,
184 struct usb_device
*dev
= gspca_dev
->dev
;
187 if (gspca_dev
->usb_err
< 0)
189 PDEBUG(D_USBO
, "SET %02x %04x %04x", req
, value
, index
);
190 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
192 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
196 err("reg_w err %d", ret
);
197 gspca_dev
->usb_err
= ret
;
201 static void reg_wb(struct gspca_dev
*gspca_dev
,
207 struct usb_device
*dev
= gspca_dev
->dev
;
210 if (gspca_dev
->usb_err
< 0)
212 PDEBUG(D_USBO
, "SET %02x %04x %04x %02x", req
, value
, index
, byte
);
213 gspca_dev
->usb_buf
[0] = byte
;
214 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
216 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
218 gspca_dev
->usb_buf
, 1, 500);
220 err("reg_w err %d", ret
);
221 gspca_dev
->usb_err
= ret
;
225 static void wait_status_0(struct gspca_dev
*gspca_dev
)
231 reg_r(gspca_dev
, 0x21, 0x0000, 1);
232 if (gspca_dev
->usb_buf
[0] == 0)
236 PDEBUG(D_ERR
, "wait_status_0 timeout");
237 gspca_dev
->usb_err
= -ETIME
;
240 static void wait_status_1(struct gspca_dev
*gspca_dev
)
246 reg_r(gspca_dev
, 0x21, 0x0001, 1);
248 if (gspca_dev
->usb_buf
[0] == 1) {
249 reg_wb(gspca_dev
, 0x21, 0x0000, 0x0001, 0x00);
250 reg_r(gspca_dev
, 0x21, 0x0001, 1);
254 PDEBUG(D_ERR
, "wait_status_1 timeout");
255 gspca_dev
->usb_err
= -ETIME
;
258 static void setbrightness(struct gspca_dev
*gspca_dev
)
260 struct sd
*sd
= (struct sd
*) gspca_dev
;
262 reg_wb(gspca_dev
, 0xc0, 0x0000, 0x00c0, sd
->brightness
);
265 static void setcontrast(struct gspca_dev
*gspca_dev
)
267 struct sd
*sd
= (struct sd
*) gspca_dev
;
269 reg_wb(gspca_dev
, 0xc1, 0x0000, 0x00c1, sd
->contrast
);
272 static void sethue(struct gspca_dev
*gspca_dev
)
274 struct sd
*sd
= (struct sd
*) gspca_dev
;
276 reg_wb(gspca_dev
, 0xc2, 0x0000, 0x0000, sd
->hue
);
279 static void setcolor(struct gspca_dev
*gspca_dev
)
281 struct sd
*sd
= (struct sd
*) gspca_dev
;
283 reg_wb(gspca_dev
, 0xc3, 0x0000, 0x00c3, sd
->color
);
286 static void setsharpness(struct gspca_dev
*gspca_dev
)
288 struct sd
*sd
= (struct sd
*) gspca_dev
;
290 reg_wb(gspca_dev
, 0xc4, 0x0000, 0x00c4, sd
->sharpness
);
293 /* this function is called at probe time */
294 static int sd_config(struct gspca_dev
*gspca_dev
,
295 const struct usb_device_id
*id
)
297 struct sd
*sd
= (struct sd
*) gspca_dev
;
299 gspca_dev
->cam
.cam_mode
= vga_mode
;
300 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
301 gspca_dev
->cam
.npkt
= 128; /* number of packets per ISOC message */
302 /*fixme: 256 in ms-win traces*/
304 sd
->brightness
= BRIGHTNESS_DEF
;
305 sd
->contrast
= CONTRAST_DEF
;
307 sd
->color
= COLOR_DEF
;
308 sd
->sharpness
= SHARPNESS_DEF
;
310 gspca_dev
->nbalt
= 4; /* use alternate setting 3 */
315 /* this function is called at probe and resume time */
316 static int sd_init(struct gspca_dev
*gspca_dev
)
318 reg_w(gspca_dev
, 0x00, 0x0001, 0x2067);
319 reg_w(gspca_dev
, 0x00, 0x00d0, 0x206b);
320 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
321 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
323 reg_w(gspca_dev
, 0x00, 0x00c0, 0x206b);
324 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
325 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
327 reg_r(gspca_dev
, 0x20, 0x0000, 1);
328 reg_r(gspca_dev
, 0x20, 0x0000, 5);
329 reg_r(gspca_dev
, 0x23, 0x0000, 64);
330 PDEBUG(D_PROBE
, "%s%s", &gspca_dev
->usb_buf
[0x1c],
331 &gspca_dev
->usb_buf
[0x30]);
332 reg_r(gspca_dev
, 0x23, 0x0001, 64);
333 return gspca_dev
->usb_err
;
336 /* function called at start time before URB creation */
337 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
341 reg_r(gspca_dev
, 0x00, 0x2520, 1);
342 wait_status_0(gspca_dev
);
343 reg_w(gspca_dev
, 0xc5, 0x0003, 0x0000);
344 wait_status_1(gspca_dev
);
346 wait_status_0(gspca_dev
);
347 mode
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
348 reg_wb(gspca_dev
, 0x25, 0x0000, 0x0004, mode
);
349 reg_r(gspca_dev
, 0x25, 0x0004, 1);
350 reg_wb(gspca_dev
, 0x27, 0x0000, 0x0000, 0x06);
351 reg_r(gspca_dev
, 0x27, 0x0000, 1);
352 return gspca_dev
->usb_err
;
355 /* -- start the camera -- */
356 static int sd_start(struct gspca_dev
*gspca_dev
)
358 struct sd
*sd
= (struct sd
*) gspca_dev
;
360 /* initialize the JPEG header */
361 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
362 0x22); /* JPEG 411 */
364 /* the JPEG quality seems to be 82% */
365 jpeg_set_qual(sd
->jpeg_hdr
, 82);
367 /* set the controls */
368 setbrightness(gspca_dev
);
369 setcontrast(gspca_dev
);
372 setsharpness(gspca_dev
);
375 reg_r(gspca_dev
, 0x00, 0x2520, 1);
378 /* start the capture */
379 wait_status_0(gspca_dev
);
380 reg_w(gspca_dev
, 0x31, 0x0000, 0x0004);
381 wait_status_1(gspca_dev
);
382 wait_status_0(gspca_dev
);
386 return gspca_dev
->usb_err
;
389 static void sd_stopN(struct gspca_dev
*gspca_dev
)
391 /* stop the capture */
392 wait_status_0(gspca_dev
);
393 reg_w(gspca_dev
, 0x31, 0x0000, 0x0000);
394 wait_status_1(gspca_dev
);
395 wait_status_0(gspca_dev
);
398 /* move a packet adding 0x00 after 0xff */
399 static void add_packet(struct gspca_dev
*gspca_dev
,
407 if (data
[i
] == 0xff) {
408 gspca_frame_add(gspca_dev
, INTER_PACKET
,
416 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
419 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
420 u8
*data
, /* isoc packet */
421 int len
) /* iso packet length */
423 struct sd
*sd
= (struct sd
*) gspca_dev
;
424 static const u8 ffd9
[] = {0xff, 0xd9};
426 /* image packets start with:
429 * 0x01: even (0) / odd (1) image
430 * 0x02: end of image when set
433 return; /* empty packet */
435 if (data
[1] & 0x02) {
436 sd
->pkt_seq
= !(data
[1] & 1);
437 add_packet(gspca_dev
, data
+ 2, len
- 2);
438 gspca_frame_add(gspca_dev
, LAST_PACKET
,
442 if ((data
[1] & 1) != sd
->pkt_seq
)
444 if (gspca_dev
->last_packet_type
== LAST_PACKET
)
445 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
446 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
447 add_packet(gspca_dev
, data
+ 2, len
- 2);
451 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
454 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
)
456 struct sd
*sd
= (struct sd
*) gspca_dev
;
458 sd
->brightness
= val
;
459 if (gspca_dev
->streaming
)
460 setbrightness(gspca_dev
);
461 return gspca_dev
->usb_err
;
464 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
466 struct sd
*sd
= (struct sd
*) gspca_dev
;
468 *val
= sd
->brightness
;
472 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
)
474 struct sd
*sd
= (struct sd
*) gspca_dev
;
477 if (gspca_dev
->streaming
)
478 setcontrast(gspca_dev
);
479 return gspca_dev
->usb_err
;
482 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
)
484 struct sd
*sd
= (struct sd
*) gspca_dev
;
490 static int sd_sethue(struct gspca_dev
*gspca_dev
, __s32 val
)
492 struct sd
*sd
= (struct sd
*) gspca_dev
;
495 if (gspca_dev
->streaming
)
497 return gspca_dev
->usb_err
;
500 static int sd_gethue(struct gspca_dev
*gspca_dev
, __s32
*val
)
502 struct sd
*sd
= (struct sd
*) gspca_dev
;
508 static int sd_setcolor(struct gspca_dev
*gspca_dev
, __s32 val
)
510 struct sd
*sd
= (struct sd
*) gspca_dev
;
513 if (gspca_dev
->streaming
)
515 return gspca_dev
->usb_err
;
518 static int sd_getcolor(struct gspca_dev
*gspca_dev
, __s32
*val
)
520 struct sd
*sd
= (struct sd
*) gspca_dev
;
526 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
)
528 struct sd
*sd
= (struct sd
*) gspca_dev
;
531 if (gspca_dev
->streaming
)
532 setsharpness(gspca_dev
);
533 return gspca_dev
->usb_err
;
536 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
)
538 struct sd
*sd
= (struct sd
*) gspca_dev
;
540 *val
= sd
->sharpness
;
544 /* sub-driver description */
545 static const struct sd_desc sd_desc
= {
548 .nctrls
= ARRAY_SIZE(sd_ctrls
),
551 .isoc_init
= sd_isoc_init
,
554 .pkt_scan
= sd_pkt_scan
,
557 /* -- module initialisation -- */
558 static const struct usb_device_id device_table
[] = {
559 {USB_DEVICE(0x04fc, 0x1528)},
562 MODULE_DEVICE_TABLE(usb
, device_table
);
564 /* -- device connect -- */
565 static int sd_probe(struct usb_interface
*intf
,
566 const struct usb_device_id
*id
)
568 /* the video interface for isochronous transfer is 1 */
569 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
572 return gspca_dev_probe2(intf
, id
, &sd_desc
, sizeof(struct sd
),
576 static struct usb_driver sd_driver
= {
578 .id_table
= device_table
,
580 .disconnect
= gspca_disconnect
,
582 .suspend
= gspca_suspend
,
583 .resume
= gspca_resume
,
587 /* -- module insert / remove -- */
588 static int __init
sd_mod_init(void)
590 return usb_register(&sd_driver
);
592 static void __exit
sd_mod_exit(void)
594 usb_deregister(&sd_driver
);
597 module_init(sd_mod_init
);
598 module_exit(sd_mod_exit
);