Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / gspca / spca1528.c
bloba20eb8580db2ea944100ecfd32ecafc9825f49eb
1 /*
2 * spca1528 subdriver
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
9 * any later version.
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"
21 #include "gspca.h"
22 #include "jpeg.h"
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 */
29 struct sd {
30 struct gspca_dev gspca_dev; /* !! must be the first item */
32 u8 pkt_seq;
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,
40 .bytesperline = 176,
41 .sizeimage = 176 * 144 * 5 / 8 + 590,
42 .colorspace = V4L2_COLORSPACE_JPEG,
43 .priv = 3},
45 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
46 .bytesperline = 320,
47 .sizeimage = 320 * 240 * 4 / 8 + 590,
48 .colorspace = V4L2_COLORSPACE_JPEG,
49 .priv = 2},
50 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51 .bytesperline = 640,
52 .sizeimage = 640 * 480 * 3 / 8 + 590,
53 .colorspace = V4L2_COLORSPACE_JPEG,
54 .priv = 1},
57 /* read <len> bytes to gspca usb_buf */
58 static void reg_r(struct gspca_dev *gspca_dev,
59 u8 req,
60 u16 index,
61 int len)
63 #if USB_BUF_SZ < 64
64 #error "USB buffer too small"
65 #endif
66 struct usb_device *dev = gspca_dev->dev;
67 int ret;
69 if (gspca_dev->usb_err < 0)
70 return;
71 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
72 req,
73 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
74 0x0000, /* value */
75 index,
76 gspca_dev->usb_buf, len,
77 500);
78 gspca_dbg(gspca_dev, D_USBI, "GET %02x 0000 %04x %02x\n", req, index,
79 gspca_dev->usb_buf[0]);
80 if (ret < 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
85 * values.
87 memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
91 static void reg_w(struct gspca_dev *gspca_dev,
92 u8 req,
93 u16 value,
94 u16 index)
96 struct usb_device *dev = gspca_dev->dev;
97 int ret;
99 if (gspca_dev->usb_err < 0)
100 return;
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),
103 req,
104 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
105 value, index,
106 NULL, 0, 500);
107 if (ret < 0) {
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,
114 u8 req,
115 u16 value,
116 u16 index,
117 u8 byte)
119 struct usb_device *dev = gspca_dev->dev;
120 int ret;
122 if (gspca_dev->usb_err < 0)
123 return;
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),
128 req,
129 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
130 value, index,
131 gspca_dev->usb_buf, 1, 500);
132 if (ret < 0) {
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)
140 int i, w;
142 i = 16;
143 w = 0;
144 do {
145 reg_r(gspca_dev, 0x21, 0x0000, 1);
146 if (gspca_dev->usb_buf[0] == 0)
147 return;
148 w += 15;
149 msleep(w);
150 } while (--i > 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)
157 int i;
159 i = 10;
160 do {
161 reg_r(gspca_dev, 0x21, 0x0001, 1);
162 msleep(10);
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);
166 return;
168 } while (--i > 0);
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*/
207 return 0;
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);
217 msleep(8);
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)
234 u8 mode;
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);
248 /* not useful..
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);
268 msleep(8);
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);
275 msleep(200);
277 sd->pkt_seq = 0;
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,
292 u8 *data,
293 int len)
295 int i;
297 i = 0;
298 do {
299 if (data[i] == 0xff) {
300 gspca_frame_add(gspca_dev, INTER_PACKET,
301 data, i + 1);
302 len -= i;
303 data += i;
304 *data = 0x00;
305 i = 0;
307 } while (++i < len);
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:
319 * 02 8n
320 * with <n> bit:
321 * 0x01: even (0) / odd (1) image
322 * 0x02: end of image when set
324 if (len < 3)
325 return; /* empty packet */
326 if (*data == 0x02) {
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,
331 ffd9, 2);
332 return;
334 if ((data[1] & 1) != sd->pkt_seq)
335 goto err;
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);
340 return;
342 err:
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)
354 return 0;
356 switch (ctrl->id) {
357 case V4L2_CID_BRIGHTNESS:
358 setbrightness(gspca_dev, ctrl->val);
359 break;
360 case V4L2_CID_CONTRAST:
361 setcontrast(gspca_dev, ctrl->val);
362 break;
363 case V4L2_CID_HUE:
364 sethue(gspca_dev, ctrl->val);
365 break;
366 case V4L2_CID_SATURATION:
367 setcolor(gspca_dev, ctrl->val);
368 break;
369 case V4L2_CID_SHARPNESS:
370 setsharpness(gspca_dev, ctrl->val);
371 break;
373 return gspca_dev->usb_err;
376 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
377 .s_ctrl = sd_s_ctrl,
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);
397 if (hdl->error) {
398 pr_err("Could not initialize controls\n");
399 return hdl->error;
401 return 0;
404 /* sub-driver description */
405 static const struct sd_desc sd_desc = {
406 .name = MODULE_NAME,
407 .config = sd_config,
408 .init = sd_init,
409 .init_controls = sd_init_controls,
410 .isoc_init = sd_isoc_init,
411 .start = sd_start,
412 .stopN = sd_stopN,
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)
429 return -ENODEV;
431 return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
432 THIS_MODULE);
435 static struct usb_driver sd_driver = {
436 .name = MODULE_NAME,
437 .id_table = device_table,
438 .probe = sd_probe,
439 .disconnect = gspca_disconnect,
440 #ifdef CONFIG_PM
441 .suspend = gspca_suspend,
442 .resume = gspca_resume,
443 .reset_resume = gspca_resume,
444 #endif
447 module_usb_driver(sd_driver);