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
;
86 static void reg_w(struct gspca_dev
*gspca_dev
,
91 struct usb_device
*dev
= gspca_dev
->dev
;
94 if (gspca_dev
->usb_err
< 0)
96 gspca_dbg(gspca_dev
, D_USBO
, "SET %02x %04x %04x\n", req
, value
, index
);
97 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
99 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
103 pr_err("reg_w err %d\n", ret
);
104 gspca_dev
->usb_err
= ret
;
108 static void reg_wb(struct gspca_dev
*gspca_dev
,
114 struct usb_device
*dev
= gspca_dev
->dev
;
117 if (gspca_dev
->usb_err
< 0)
119 gspca_dbg(gspca_dev
, D_USBO
, "SET %02x %04x %04x %02x\n",
120 req
, value
, index
, byte
);
121 gspca_dev
->usb_buf
[0] = byte
;
122 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
124 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
126 gspca_dev
->usb_buf
, 1, 500);
128 pr_err("reg_w err %d\n", ret
);
129 gspca_dev
->usb_err
= ret
;
133 static void wait_status_0(struct gspca_dev
*gspca_dev
)
140 reg_r(gspca_dev
, 0x21, 0x0000, 1);
141 if (gspca_dev
->usb_buf
[0] == 0)
146 gspca_err(gspca_dev
, "wait_status_0 timeout\n");
147 gspca_dev
->usb_err
= -ETIME
;
150 static void wait_status_1(struct gspca_dev
*gspca_dev
)
156 reg_r(gspca_dev
, 0x21, 0x0001, 1);
158 if (gspca_dev
->usb_buf
[0] == 1) {
159 reg_wb(gspca_dev
, 0x21, 0x0000, 0x0001, 0x00);
160 reg_r(gspca_dev
, 0x21, 0x0001, 1);
164 gspca_err(gspca_dev
, "wait_status_1 timeout\n");
165 gspca_dev
->usb_err
= -ETIME
;
168 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
170 reg_wb(gspca_dev
, 0xc0, 0x0000, 0x00c0, val
);
173 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
175 reg_wb(gspca_dev
, 0xc1, 0x0000, 0x00c1, val
);
178 static void sethue(struct gspca_dev
*gspca_dev
, s32 val
)
180 reg_wb(gspca_dev
, 0xc2, 0x0000, 0x0000, val
);
183 static void setcolor(struct gspca_dev
*gspca_dev
, s32 val
)
185 reg_wb(gspca_dev
, 0xc3, 0x0000, 0x00c3, val
);
188 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
190 reg_wb(gspca_dev
, 0xc4, 0x0000, 0x00c4, val
);
193 /* this function is called at probe time */
194 static int sd_config(struct gspca_dev
*gspca_dev
,
195 const struct usb_device_id
*id
)
197 gspca_dev
->cam
.cam_mode
= vga_mode
;
198 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
199 gspca_dev
->cam
.npkt
= 128; /* number of packets per ISOC message */
200 /*fixme: 256 in ms-win traces*/
205 /* this function is called at probe and resume time */
206 static int sd_init(struct gspca_dev
*gspca_dev
)
208 reg_w(gspca_dev
, 0x00, 0x0001, 0x2067);
209 reg_w(gspca_dev
, 0x00, 0x00d0, 0x206b);
210 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
211 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
213 reg_w(gspca_dev
, 0x00, 0x00c0, 0x206b);
214 reg_w(gspca_dev
, 0x00, 0x0000, 0x206c);
215 reg_w(gspca_dev
, 0x00, 0x0001, 0x2069);
217 reg_r(gspca_dev
, 0x20, 0x0000, 1);
218 reg_r(gspca_dev
, 0x20, 0x0000, 5);
219 reg_r(gspca_dev
, 0x23, 0x0000, 64);
220 gspca_dbg(gspca_dev
, D_PROBE
, "%s%s\n", &gspca_dev
->usb_buf
[0x1c],
221 &gspca_dev
->usb_buf
[0x30]);
222 reg_r(gspca_dev
, 0x23, 0x0001, 64);
223 return gspca_dev
->usb_err
;
226 /* function called at start time before URB creation */
227 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
231 reg_r(gspca_dev
, 0x00, 0x2520, 1);
232 wait_status_0(gspca_dev
);
233 reg_w(gspca_dev
, 0xc5, 0x0003, 0x0000);
234 wait_status_1(gspca_dev
);
236 wait_status_0(gspca_dev
);
237 mode
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
238 reg_wb(gspca_dev
, 0x25, 0x0000, 0x0004, mode
);
239 reg_r(gspca_dev
, 0x25, 0x0004, 1);
240 reg_wb(gspca_dev
, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
241 reg_r(gspca_dev
, 0x27, 0x0000, 1);
244 gspca_dev->alt = 4; * use alternate setting 3 */
246 return gspca_dev
->usb_err
;
249 /* -- start the camera -- */
250 static int sd_start(struct gspca_dev
*gspca_dev
)
252 struct sd
*sd
= (struct sd
*) gspca_dev
;
254 /* initialize the JPEG header */
255 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
256 gspca_dev
->pixfmt
.width
,
257 0x22); /* JPEG 411 */
259 /* the JPEG quality shall be 85% */
260 jpeg_set_qual(sd
->jpeg_hdr
, 85);
262 reg_r(gspca_dev
, 0x00, 0x2520, 1);
265 /* start the capture */
266 wait_status_0(gspca_dev
);
267 reg_w(gspca_dev
, 0x31, 0x0000, 0x0004); /* start request */
268 wait_status_1(gspca_dev
);
269 wait_status_0(gspca_dev
);
273 return gspca_dev
->usb_err
;
276 static void sd_stopN(struct gspca_dev
*gspca_dev
)
278 /* stop the capture */
279 wait_status_0(gspca_dev
);
280 reg_w(gspca_dev
, 0x31, 0x0000, 0x0000); /* stop request */
281 wait_status_1(gspca_dev
);
282 wait_status_0(gspca_dev
);
285 /* move a packet adding 0x00 after 0xff */
286 static void add_packet(struct gspca_dev
*gspca_dev
,
294 if (data
[i
] == 0xff) {
295 gspca_frame_add(gspca_dev
, INTER_PACKET
,
303 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
306 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
307 u8
*data
, /* isoc packet */
308 int len
) /* iso packet length */
310 struct sd
*sd
= (struct sd
*) gspca_dev
;
311 static const u8 ffd9
[] = {0xff, 0xd9};
313 /* image packets start with:
316 * 0x01: even (0) / odd (1) image
317 * 0x02: end of image when set
320 return; /* empty packet */
322 if (data
[1] & 0x02) {
323 sd
->pkt_seq
= !(data
[1] & 1);
324 add_packet(gspca_dev
, data
+ 2, len
- 2);
325 gspca_frame_add(gspca_dev
, LAST_PACKET
,
329 if ((data
[1] & 1) != sd
->pkt_seq
)
331 if (gspca_dev
->last_packet_type
== LAST_PACKET
)
332 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
333 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
334 add_packet(gspca_dev
, data
+ 2, len
- 2);
338 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
341 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
343 struct gspca_dev
*gspca_dev
=
344 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
346 gspca_dev
->usb_err
= 0;
348 if (!gspca_dev
->streaming
)
352 case V4L2_CID_BRIGHTNESS
:
353 setbrightness(gspca_dev
, ctrl
->val
);
355 case V4L2_CID_CONTRAST
:
356 setcontrast(gspca_dev
, ctrl
->val
);
359 sethue(gspca_dev
, ctrl
->val
);
361 case V4L2_CID_SATURATION
:
362 setcolor(gspca_dev
, ctrl
->val
);
364 case V4L2_CID_SHARPNESS
:
365 setsharpness(gspca_dev
, ctrl
->val
);
368 return gspca_dev
->usb_err
;
371 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
375 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
377 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
379 gspca_dev
->vdev
.ctrl_handler
= hdl
;
380 v4l2_ctrl_handler_init(hdl
, 5);
381 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
382 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
383 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
384 V4L2_CID_CONTRAST
, 0, 8, 1, 1);
385 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
386 V4L2_CID_HUE
, 0, 255, 1, 0);
387 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
388 V4L2_CID_SATURATION
, 0, 8, 1, 1);
389 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
390 V4L2_CID_SHARPNESS
, 0, 255, 1, 0);
393 pr_err("Could not initialize controls\n");
399 /* sub-driver description */
400 static const struct sd_desc sd_desc
= {
404 .init_controls
= sd_init_controls
,
405 .isoc_init
= sd_isoc_init
,
408 .pkt_scan
= sd_pkt_scan
,
411 /* -- module initialisation -- */
412 static const struct usb_device_id device_table
[] = {
413 {USB_DEVICE(0x04fc, 0x1528)},
416 MODULE_DEVICE_TABLE(usb
, device_table
);
418 /* -- device connect -- */
419 static int sd_probe(struct usb_interface
*intf
,
420 const struct usb_device_id
*id
)
422 /* the video interface for isochronous transfer is 1 */
423 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
426 return gspca_dev_probe2(intf
, id
, &sd_desc
, sizeof(struct sd
),
430 static struct usb_driver sd_driver
= {
432 .id_table
= device_table
,
434 .disconnect
= gspca_disconnect
,
436 .suspend
= gspca_suspend
,
437 .resume
= gspca_resume
,
438 .reset_resume
= gspca_resume
,
442 module_usb_driver(sd_driver
);