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 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", 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 PDEBUG(D_USBO, "SET %02x %04x %04x", 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 PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
125 gspca_dev->usb_buf[0] = byte;
126 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
128 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
130 gspca_dev->usb_buf, 1, 500);
132 pr_err("reg_w err %d\n", ret);
133 gspca_dev->usb_err = ret;
137 static void wait_status_0(struct gspca_dev *gspca_dev)
144 reg_r(gspca_dev, 0x21, 0x0000, 1);
145 if (gspca_dev->usb_buf[0] == 0)
150 PERR("wait_status_0 timeout");
151 gspca_dev->usb_err = -ETIME;
154 static void wait_status_1(struct gspca_dev *gspca_dev)
160 reg_r(gspca_dev, 0x21, 0x0001, 1);
162 if (gspca_dev->usb_buf[0] == 1) {
163 reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
164 reg_r(gspca_dev, 0x21, 0x0001, 1);
168 PERR("wait_status_1 timeout");
169 gspca_dev->usb_err = -ETIME;
172 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
174 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
177 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
179 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
182 static void sethue(struct gspca_dev *gspca_dev, s32 val)
184 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
187 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
189 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
192 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
194 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
197 /* this function is called at probe time */
198 static int sd_config(struct gspca_dev *gspca_dev,
199 const struct usb_device_id *id)
201 gspca_dev->cam.cam_mode = vga_mode;
202 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
203 gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
204 /*fixme: 256 in ms-win traces*/
209 /* this function is called at probe and resume time */
210 static int sd_init(struct gspca_dev *gspca_dev)
212 reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
213 reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
214 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
215 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
217 reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
218 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
219 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
221 reg_r(gspca_dev, 0x20, 0x0000, 1);
222 reg_r(gspca_dev, 0x20, 0x0000, 5);
223 reg_r(gspca_dev, 0x23, 0x0000, 64);
224 PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
225 &gspca_dev->usb_buf[0x30]);
226 reg_r(gspca_dev, 0x23, 0x0001, 64);
227 return gspca_dev->usb_err;
230 /* function called at start time before URB creation */
231 static int sd_isoc_init(struct gspca_dev *gspca_dev)
235 reg_r(gspca_dev, 0x00, 0x2520, 1);
236 wait_status_0(gspca_dev);
237 reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
238 wait_status_1(gspca_dev);
240 wait_status_0(gspca_dev);
241 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
242 reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
243 reg_r(gspca_dev, 0x25, 0x0004, 1);
244 reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
245 reg_r(gspca_dev, 0x27, 0x0000, 1);
248 gspca_dev->alt = 4; * use alternate setting 3 */
250 return gspca_dev->usb_err;
253 /* -- start the camera -- */
254 static int sd_start(struct gspca_dev *gspca_dev)
256 struct sd *sd = (struct sd *) gspca_dev;
258 /* initialize the JPEG header */
259 jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
260 gspca_dev->pixfmt.width,
261 0x22); /* JPEG 411 */
263 /* the JPEG quality shall be 85% */
264 jpeg_set_qual(sd->jpeg_hdr, 85);
266 reg_r(gspca_dev, 0x00, 0x2520, 1);
269 /* start the capture */
270 wait_status_0(gspca_dev);
271 reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
272 wait_status_1(gspca_dev);
273 wait_status_0(gspca_dev);
277 return gspca_dev->usb_err;
280 static void sd_stopN(struct gspca_dev *gspca_dev)
282 /* stop the capture */
283 wait_status_0(gspca_dev);
284 reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
285 wait_status_1(gspca_dev);
286 wait_status_0(gspca_dev);
289 /* move a packet adding 0x00 after 0xff */
290 static void add_packet(struct gspca_dev *gspca_dev,
298 if (data[i] == 0xff) {
299 gspca_frame_add(gspca_dev, INTER_PACKET,
307 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
310 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
311 u8 *data, /* isoc packet */
312 int len) /* iso packet length */
314 struct sd *sd = (struct sd *) gspca_dev;
315 static const u8 ffd9[] = {0xff, 0xd9};
317 /* image packets start with:
320 * 0x01: even (0) / odd (1) image
321 * 0x02: end of image when set
324 return; /* empty packet */
326 if (data[1] & 0x02) {
327 sd->pkt_seq = !(data[1] & 1);
328 add_packet(gspca_dev, data + 2, len - 2);
329 gspca_frame_add(gspca_dev, LAST_PACKET,
333 if ((data[1] & 1) != sd->pkt_seq)
335 if (gspca_dev->last_packet_type == LAST_PACKET)
336 gspca_frame_add(gspca_dev, FIRST_PACKET,
337 sd->jpeg_hdr, JPEG_HDR_SZ);
338 add_packet(gspca_dev, data + 2, len - 2);
342 gspca_dev->last_packet_type = DISCARD_PACKET;
345 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
347 struct gspca_dev *gspca_dev =
348 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
350 gspca_dev->usb_err = 0;
352 if (!gspca_dev->streaming)
356 case V4L2_CID_BRIGHTNESS:
357 setbrightness(gspca_dev, ctrl->val);
359 case V4L2_CID_CONTRAST:
360 setcontrast(gspca_dev, ctrl->val);
363 sethue(gspca_dev, ctrl->val);
365 case V4L2_CID_SATURATION:
366 setcolor(gspca_dev, ctrl->val);
368 case V4L2_CID_SHARPNESS:
369 setsharpness(gspca_dev, ctrl->val);
372 return gspca_dev->usb_err;
375 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
379 static int sd_init_controls(struct gspca_dev *gspca_dev)
381 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
383 gspca_dev->vdev.ctrl_handler = hdl;
384 v4l2_ctrl_handler_init(hdl, 5);
385 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
386 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
387 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
388 V4L2_CID_CONTRAST, 0, 8, 1, 1);
389 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
390 V4L2_CID_HUE, 0, 255, 1, 0);
391 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
392 V4L2_CID_SATURATION, 0, 8, 1, 1);
393 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
394 V4L2_CID_SHARPNESS, 0, 255, 1, 0);
397 pr_err("Could not initialize controls\n");
403 /* sub-driver description */
404 static const struct sd_desc sd_desc = {
408 .init_controls = sd_init_controls,
409 .isoc_init = sd_isoc_init,
412 .pkt_scan = sd_pkt_scan,
415 /* -- module initialisation -- */
416 static const struct usb_device_id device_table[] = {
417 {USB_DEVICE(0x04fc, 0x1528)},
420 MODULE_DEVICE_TABLE(usb, device_table);
422 /* -- device connect -- */
423 static int sd_probe(struct usb_interface *intf,
424 const struct usb_device_id *id)
426 /* the video interface for isochronous transfer is 1 */
427 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
430 return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
434 static struct usb_driver sd_driver = {
436 .id_table = device_table,
438 .disconnect = gspca_disconnect,
440 .suspend = gspca_suspend,
441 .resume = gspca_resume,
442 .reset_resume = gspca_resume,
446 module_usb_driver(sd_driver);