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 */
38 u8 jpeg_hdr[JPEG_HDR_SZ];
41 static const struct v4l2_pix_format vga_mode[] = {
42 /* (does not work correctly)
43 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
45 .sizeimage = 176 * 144 * 5 / 8 + 590,
46 .colorspace = V4L2_COLORSPACE_JPEG,
49 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51 .sizeimage = 320 * 240 * 4 / 8 + 590,
52 .colorspace = V4L2_COLORSPACE_JPEG,
54 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
56 .sizeimage = 640 * 480 * 3 / 8 + 590,
57 .colorspace = V4L2_COLORSPACE_JPEG,
61 /* read <len> bytes to gspca usb_buf */
62 static void reg_r(struct gspca_dev *gspca_dev,
68 #error "USB buffer too small"
70 struct usb_device *dev = gspca_dev->dev;
73 if (gspca_dev->usb_err < 0)
75 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
77 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
80 gspca_dev->usb_buf, len,
82 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index,
83 gspca_dev->usb_buf[0]);
85 pr_err("reg_r err %d\n", ret);
86 gspca_dev->usb_err = ret;
90 static void reg_w(struct gspca_dev *gspca_dev,
95 struct usb_device *dev = gspca_dev->dev;
98 if (gspca_dev->usb_err < 0)
100 PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index);
101 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
103 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
107 pr_err("reg_w err %d\n", ret);
108 gspca_dev->usb_err = ret;
112 static void reg_wb(struct gspca_dev *gspca_dev,
118 struct usb_device *dev = gspca_dev->dev;
121 if (gspca_dev->usb_err < 0)
123 PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
124 gspca_dev->usb_buf[0] = byte;
125 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
127 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
129 gspca_dev->usb_buf, 1, 500);
131 pr_err("reg_w err %d\n", ret);
132 gspca_dev->usb_err = ret;
136 static void wait_status_0(struct gspca_dev *gspca_dev)
143 reg_r(gspca_dev, 0x21, 0x0000, 1);
144 if (gspca_dev->usb_buf[0] == 0)
149 PERR("wait_status_0 timeout");
150 gspca_dev->usb_err = -ETIME;
153 static void wait_status_1(struct gspca_dev *gspca_dev)
159 reg_r(gspca_dev, 0x21, 0x0001, 1);
161 if (gspca_dev->usb_buf[0] == 1) {
162 reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
163 reg_r(gspca_dev, 0x21, 0x0001, 1);
167 PERR("wait_status_1 timeout");
168 gspca_dev->usb_err = -ETIME;
171 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
173 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
176 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
178 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
181 static void sethue(struct gspca_dev *gspca_dev, s32 val)
183 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
186 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
188 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
191 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
193 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
196 /* this function is called at probe time */
197 static int sd_config(struct gspca_dev *gspca_dev,
198 const struct usb_device_id *id)
200 gspca_dev->cam.cam_mode = vga_mode;
201 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
202 gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
203 /*fixme: 256 in ms-win traces*/
208 /* this function is called at probe and resume time */
209 static int sd_init(struct gspca_dev *gspca_dev)
211 reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
212 reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
213 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
214 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
216 reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
217 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
218 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
220 reg_r(gspca_dev, 0x20, 0x0000, 1);
221 reg_r(gspca_dev, 0x20, 0x0000, 5);
222 reg_r(gspca_dev, 0x23, 0x0000, 64);
223 PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
224 &gspca_dev->usb_buf[0x30]);
225 reg_r(gspca_dev, 0x23, 0x0001, 64);
226 return gspca_dev->usb_err;
229 /* function called at start time before URB creation */
230 static int sd_isoc_init(struct gspca_dev *gspca_dev)
234 reg_r(gspca_dev, 0x00, 0x2520, 1);
235 wait_status_0(gspca_dev);
236 reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
237 wait_status_1(gspca_dev);
239 wait_status_0(gspca_dev);
240 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
241 reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
242 reg_r(gspca_dev, 0x25, 0x0004, 1);
243 reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
244 reg_r(gspca_dev, 0x27, 0x0000, 1);
247 gspca_dev->alt = 4; * use alternate setting 3 */
249 return gspca_dev->usb_err;
252 /* -- start the camera -- */
253 static int sd_start(struct gspca_dev *gspca_dev)
255 struct sd *sd = (struct sd *) gspca_dev;
257 /* initialize the JPEG header */
258 jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
259 gspca_dev->pixfmt.width,
260 0x22); /* JPEG 411 */
262 /* the JPEG quality shall be 85% */
263 jpeg_set_qual(sd->jpeg_hdr, 85);
265 reg_r(gspca_dev, 0x00, 0x2520, 1);
268 /* start the capture */
269 wait_status_0(gspca_dev);
270 reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
271 wait_status_1(gspca_dev);
272 wait_status_0(gspca_dev);
276 return gspca_dev->usb_err;
279 static void sd_stopN(struct gspca_dev *gspca_dev)
281 /* stop the capture */
282 wait_status_0(gspca_dev);
283 reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
284 wait_status_1(gspca_dev);
285 wait_status_0(gspca_dev);
288 /* move a packet adding 0x00 after 0xff */
289 static void add_packet(struct gspca_dev *gspca_dev,
297 if (data[i] == 0xff) {
298 gspca_frame_add(gspca_dev, INTER_PACKET,
306 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
309 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
310 u8 *data, /* isoc packet */
311 int len) /* iso packet length */
313 struct sd *sd = (struct sd *) gspca_dev;
314 static const u8 ffd9[] = {0xff, 0xd9};
316 /* image packets start with:
319 * 0x01: even (0) / odd (1) image
320 * 0x02: end of image when set
323 return; /* empty packet */
325 if (data[1] & 0x02) {
326 sd->pkt_seq = !(data[1] & 1);
327 add_packet(gspca_dev, data + 2, len - 2);
328 gspca_frame_add(gspca_dev, LAST_PACKET,
332 if ((data[1] & 1) != sd->pkt_seq)
334 if (gspca_dev->last_packet_type == LAST_PACKET)
335 gspca_frame_add(gspca_dev, FIRST_PACKET,
336 sd->jpeg_hdr, JPEG_HDR_SZ);
337 add_packet(gspca_dev, data + 2, len - 2);
341 gspca_dev->last_packet_type = DISCARD_PACKET;
344 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
346 struct gspca_dev *gspca_dev =
347 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
349 gspca_dev->usb_err = 0;
351 if (!gspca_dev->streaming)
355 case V4L2_CID_BRIGHTNESS:
356 setbrightness(gspca_dev, ctrl->val);
358 case V4L2_CID_CONTRAST:
359 setcontrast(gspca_dev, ctrl->val);
362 sethue(gspca_dev, ctrl->val);
364 case V4L2_CID_SATURATION:
365 setcolor(gspca_dev, ctrl->val);
367 case V4L2_CID_SHARPNESS:
368 setsharpness(gspca_dev, ctrl->val);
371 return gspca_dev->usb_err;
374 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
378 static int sd_init_controls(struct gspca_dev *gspca_dev)
380 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
382 gspca_dev->vdev.ctrl_handler = hdl;
383 v4l2_ctrl_handler_init(hdl, 5);
384 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
385 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
386 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
387 V4L2_CID_CONTRAST, 0, 8, 1, 1);
388 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
389 V4L2_CID_HUE, 0, 255, 1, 0);
390 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
391 V4L2_CID_SATURATION, 0, 8, 1, 1);
392 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
393 V4L2_CID_SHARPNESS, 0, 255, 1, 0);
396 pr_err("Could not initialize controls\n");
402 /* sub-driver description */
403 static const struct sd_desc sd_desc = {
407 .init_controls = sd_init_controls,
408 .isoc_init = sd_isoc_init,
411 .pkt_scan = sd_pkt_scan,
414 /* -- module initialisation -- */
415 static const struct usb_device_id device_table[] = {
416 {USB_DEVICE(0x04fc, 0x1528)},
419 MODULE_DEVICE_TABLE(usb, device_table);
421 /* -- device connect -- */
422 static int sd_probe(struct usb_interface *intf,
423 const struct usb_device_id *id)
425 /* the video interface for isochronous transfer is 1 */
426 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
429 return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
433 static struct usb_driver sd_driver = {
435 .id_table = device_table,
437 .disconnect = gspca_disconnect,
439 .suspend = gspca_suspend,
440 .resume = gspca_resume,
441 .reset_resume = gspca_resume,
445 module_usb_driver(sd_driver);