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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #define MODULE_NAME "spca1528"
24 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
25 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
26 MODULE_LICENSE("GPL");
28 /* specific webcam descriptor */
30 struct gspca_dev gspca_dev
; /* !! must be the first item */
34 u8 jpeg_hdr
[JPEG_HDR_SZ
];
37 static const struct v4l2_pix_format vga_mode
[] = {
38 /* (does not work correctly)
39 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
41 .sizeimage = 176 * 144 * 5 / 8 + 590,
42 .colorspace = V4L2_COLORSPACE_JPEG,
45 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
47 .sizeimage
= 320 * 240 * 4 / 8 + 590,
48 .colorspace
= V4L2_COLORSPACE_JPEG
,
50 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
52 .sizeimage
= 640 * 480 * 3 / 8 + 590,
53 .colorspace
= V4L2_COLORSPACE_JPEG
,
57 /* read <len> bytes to gspca usb_buf */
58 static void reg_r(struct gspca_dev
*gspca_dev
,
64 #error "USB buffer too small"
66 struct usb_device
*dev
= gspca_dev
->dev
;
69 if (gspca_dev
->usb_err
< 0)
71 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
73 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
76 gspca_dev
->usb_buf
, len
,
78 gspca_dbg(gspca_dev
, D_USBI
, "GET %02x 0000 %04x %02x\n", req
, index
,
79 gspca_dev
->usb_buf
[0]);
81 pr_err("reg_r err %d\n", ret
);
82 gspca_dev
->usb_err
= ret
;
84 * Make sure the buffer is zeroed to avoid uninitialized
87 memset(gspca_dev
->usb_buf
, 0, USB_BUF_SZ
);
91 static void reg_w(struct gspca_dev
*gspca_dev
,
96 struct usb_device
*dev
= gspca_dev
->dev
;
99 if (gspca_dev
->usb_err
< 0)
101 gspca_dbg(gspca_dev
, D_USBO
, "SET %02x %04x %04x\n", req
, value
, index
);
102 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
104 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
108 pr_err("reg_w err %d\n", ret
);
109 gspca_dev
->usb_err
= ret
;
113 static void reg_wb(struct gspca_dev
*gspca_dev
,
119 struct usb_device
*dev
= gspca_dev
->dev
;
122 if (gspca_dev
->usb_err
< 0)
124 gspca_dbg(gspca_dev
, D_USBO
, "SET %02x %04x %04x %02x\n",
125 req
, value
, index
, byte
);
126 gspca_dev
->usb_buf
[0] = byte
;
127 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
129 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
131 gspca_dev
->usb_buf
, 1, 500);
133 pr_err("reg_w err %d\n", ret
);
134 gspca_dev
->usb_err
= ret
;
138 static void wait_status_0(struct gspca_dev
*gspca_dev
)
145 reg_r(gspca_dev
, 0x21, 0x0000, 1);
146 if (gspca_dev
->usb_buf
[0] == 0)
151 gspca_err(gspca_dev
, "wait_status_0 timeout\n");
152 gspca_dev
->usb_err
= -ETIME
;
155 static void wait_status_1(struct gspca_dev
*gspca_dev
)
161 reg_r(gspca_dev
, 0x21, 0x0001, 1);
163 if (gspca_dev
->usb_buf
[0] == 1) {
164 reg_wb(gspca_dev
, 0x21, 0x0000, 0x0001, 0x00);
165 reg_r(gspca_dev
, 0x21, 0x0001, 1);
169 gspca_err(gspca_dev
, "wait_status_1 timeout\n");
170 gspca_dev
->usb_err
= -ETIME
;
173 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
175 reg_wb(gspca_dev
, 0xc0, 0x0000, 0x00c0, val
);
178 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
180 reg_wb(gspca_dev
, 0xc1, 0x0000, 0x00c1, val
);
183 static void sethue(struct gspca_dev
*gspca_dev
, s32 val
)
185 reg_wb(gspca_dev
, 0xc2, 0x0000, 0x0000, val
);
188 static void setcolor(struct gspca_dev
*gspca_dev
, s32 val
)
190 reg_wb(gspca_dev
, 0xc3, 0x0000, 0x00c3, val
);
193 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
195 reg_wb(gspca_dev
, 0xc4, 0x0000, 0x00c4, val
);
198 /* this function is called at probe time */
199 static int sd_config(struct gspca_dev
*gspca_dev
,
200 const struct usb_device_id
*id
)
202 gspca_dev
->cam
.cam_mode
= vga_mode
;
203 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
204 gspca_dev
->cam
.npkt
= 128; /* number of packets per ISOC message */
205 /*fixme: 256 in ms-win traces*/
210 /* this function is called at probe and resume time */
211 static int sd_init(struct gspca_dev
*gspca_dev
)
213 reg_w(gspca_dev
, 0x00, 0x0001, 0x2067);
214 reg_w(gspca_dev
, 0x00, 0x00d0, 0x206b);
215 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
216 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
218 reg_w(gspca_dev
, 0x00, 0x00c0, 0x206b);
219 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
220 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
222 reg_r(gspca_dev
, 0x20, 0x0000, 1);
223 reg_r(gspca_dev
, 0x20, 0x0000, 5);
224 reg_r(gspca_dev
, 0x23, 0x0000, 64);
225 gspca_dbg(gspca_dev
, D_PROBE
, "%s%s\n", &gspca_dev
->usb_buf
[0x1c],
226 &gspca_dev
->usb_buf
[0x30]);
227 reg_r(gspca_dev
, 0x23, 0x0001, 64);
228 return gspca_dev
->usb_err
;
231 /* function called at start time before URB creation */
232 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
236 reg_r(gspca_dev
, 0x00, 0x2520, 1);
237 wait_status_0(gspca_dev
);
238 reg_w(gspca_dev
, 0xc5, 0x0003, 0x0000);
239 wait_status_1(gspca_dev
);
241 wait_status_0(gspca_dev
);
242 mode
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
243 reg_wb(gspca_dev
, 0x25, 0x0000, 0x0004, mode
);
244 reg_r(gspca_dev
, 0x25, 0x0004, 1);
245 reg_wb(gspca_dev
, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
246 reg_r(gspca_dev
, 0x27, 0x0000, 1);
249 gspca_dev->alt = 4; * use alternate setting 3 */
251 return gspca_dev
->usb_err
;
254 /* -- start the camera -- */
255 static int sd_start(struct gspca_dev
*gspca_dev
)
257 struct sd
*sd
= (struct sd
*) gspca_dev
;
259 /* initialize the JPEG header */
260 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
261 gspca_dev
->pixfmt
.width
,
262 0x22); /* JPEG 411 */
264 /* the JPEG quality shall be 85% */
265 jpeg_set_qual(sd
->jpeg_hdr
, 85);
267 reg_r(gspca_dev
, 0x00, 0x2520, 1);
270 /* start the capture */
271 wait_status_0(gspca_dev
);
272 reg_w(gspca_dev
, 0x31, 0x0000, 0x0004); /* start request */
273 wait_status_1(gspca_dev
);
274 wait_status_0(gspca_dev
);
278 return gspca_dev
->usb_err
;
281 static void sd_stopN(struct gspca_dev
*gspca_dev
)
283 /* stop the capture */
284 wait_status_0(gspca_dev
);
285 reg_w(gspca_dev
, 0x31, 0x0000, 0x0000); /* stop request */
286 wait_status_1(gspca_dev
);
287 wait_status_0(gspca_dev
);
290 /* move a packet adding 0x00 after 0xff */
291 static void add_packet(struct gspca_dev
*gspca_dev
,
299 if (data
[i
] == 0xff) {
300 gspca_frame_add(gspca_dev
, INTER_PACKET
,
308 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
311 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
312 u8
*data
, /* isoc packet */
313 int len
) /* iso packet length */
315 struct sd
*sd
= (struct sd
*) gspca_dev
;
316 static const u8 ffd9
[] = {0xff, 0xd9};
318 /* image packets start with:
321 * 0x01: even (0) / odd (1) image
322 * 0x02: end of image when set
325 return; /* empty packet */
327 if (data
[1] & 0x02) {
328 sd
->pkt_seq
= !(data
[1] & 1);
329 add_packet(gspca_dev
, data
+ 2, len
- 2);
330 gspca_frame_add(gspca_dev
, LAST_PACKET
,
334 if ((data
[1] & 1) != sd
->pkt_seq
)
336 if (gspca_dev
->last_packet_type
== LAST_PACKET
)
337 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
338 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
339 add_packet(gspca_dev
, data
+ 2, len
- 2);
343 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
346 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
348 struct gspca_dev
*gspca_dev
=
349 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
351 gspca_dev
->usb_err
= 0;
353 if (!gspca_dev
->streaming
)
357 case V4L2_CID_BRIGHTNESS
:
358 setbrightness(gspca_dev
, ctrl
->val
);
360 case V4L2_CID_CONTRAST
:
361 setcontrast(gspca_dev
, ctrl
->val
);
364 sethue(gspca_dev
, ctrl
->val
);
366 case V4L2_CID_SATURATION
:
367 setcolor(gspca_dev
, ctrl
->val
);
369 case V4L2_CID_SHARPNESS
:
370 setsharpness(gspca_dev
, ctrl
->val
);
373 return gspca_dev
->usb_err
;
376 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
380 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
382 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
384 gspca_dev
->vdev
.ctrl_handler
= hdl
;
385 v4l2_ctrl_handler_init(hdl
, 5);
386 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
387 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
388 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
389 V4L2_CID_CONTRAST
, 0, 8, 1, 1);
390 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
391 V4L2_CID_HUE
, 0, 255, 1, 0);
392 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
393 V4L2_CID_SATURATION
, 0, 8, 1, 1);
394 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
395 V4L2_CID_SHARPNESS
, 0, 255, 1, 0);
398 pr_err("Could not initialize controls\n");
404 /* sub-driver description */
405 static const struct sd_desc sd_desc
= {
409 .init_controls
= sd_init_controls
,
410 .isoc_init
= sd_isoc_init
,
413 .pkt_scan
= sd_pkt_scan
,
416 /* -- module initialisation -- */
417 static const struct usb_device_id device_table
[] = {
418 {USB_DEVICE(0x04fc, 0x1528)},
421 MODULE_DEVICE_TABLE(usb
, device_table
);
423 /* -- device connect -- */
424 static int sd_probe(struct usb_interface
*intf
,
425 const struct usb_device_id
*id
)
427 /* the video interface for isochronous transfer is 1 */
428 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
431 return gspca_dev_probe2(intf
, id
, &sd_desc
, sizeof(struct sd
),
435 static struct usb_driver sd_driver
= {
437 .id_table
= device_table
,
439 .disconnect
= gspca_disconnect
,
441 .suspend
= gspca_suspend
,
442 .resume
= gspca_resume
,
443 .reset_resume
= gspca_resume
,
447 module_usb_driver(sd_driver
);