GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / media / usb / gspca / spca1528.c
1 /*
2  * spca1528 subdriver
3  *
4  * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
5  *
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.
10  *
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.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #define MODULE_NAME "spca1528"
20
21 #include "gspca.h"
22 #include "jpeg.h"
23
24 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
25 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
26 MODULE_LICENSE("GPL");
27
28 /* specific webcam descriptor */
29 struct sd {
30         struct gspca_dev gspca_dev;     /* !! must be the first item */
31
32         u8 pkt_seq;
33
34         u8 jpeg_hdr[JPEG_HDR_SZ];
35 };
36
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},
44 */
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},
55 };
56
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)
62 {
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;
68
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;
83                 /*
84                  * Make sure the buffer is zeroed to avoid uninitialized
85                  * values.
86                  */
87                 memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
88         }
89 }
90
91 static void reg_w(struct gspca_dev *gspca_dev,
92                         u8 req,
93                         u16 value,
94                         u16 index)
95 {
96         struct usb_device *dev = gspca_dev->dev;
97         int ret;
98
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;
110         }
111 }
112
113 static void reg_wb(struct gspca_dev *gspca_dev,
114                         u8 req,
115                         u16 value,
116                         u16 index,
117                         u8 byte)
118 {
119         struct usb_device *dev = gspca_dev->dev;
120         int ret;
121
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;
135         }
136 }
137
138 static void wait_status_0(struct gspca_dev *gspca_dev)
139 {
140         int i, w;
141
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;
153 }
154
155 static void wait_status_1(struct gspca_dev *gspca_dev)
156 {
157         int i;
158
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;
167                 }
168         } while (--i > 0);
169         gspca_err(gspca_dev, "wait_status_1 timeout\n");
170         gspca_dev->usb_err = -ETIME;
171 }
172
173 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
174 {
175         reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
176 }
177
178 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
179 {
180         reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
181 }
182
183 static void sethue(struct gspca_dev *gspca_dev, s32 val)
184 {
185         reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
186 }
187
188 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
189 {
190         reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
191 }
192
193 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
194 {
195         reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
196 }
197
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)
201 {
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*/
206
207         return 0;
208 }
209
210 /* this function is called at probe and resume time */
211 static int sd_init(struct gspca_dev *gspca_dev)
212 {
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);
221
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;
229 }
230
231 /* function called at start time before URB creation */
232 static int sd_isoc_init(struct gspca_dev *gspca_dev)
233 {
234         u8 mode;
235
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);
240
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);
247
248 /* not useful..
249         gspca_dev->alt = 4;             * use alternate setting 3 */
250
251         return gspca_dev->usb_err;
252 }
253
254 /* -- start the camera -- */
255 static int sd_start(struct gspca_dev *gspca_dev)
256 {
257         struct sd *sd = (struct sd *) gspca_dev;
258
259         /* initialize the JPEG header */
260         jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
261                         gspca_dev->pixfmt.width,
262                         0x22);          /* JPEG 411 */
263
264         /* the JPEG quality shall be 85% */
265         jpeg_set_qual(sd->jpeg_hdr, 85);
266
267         reg_r(gspca_dev, 0x00, 0x2520, 1);
268         msleep(8);
269
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);
276
277         sd->pkt_seq = 0;
278         return gspca_dev->usb_err;
279 }
280
281 static void sd_stopN(struct gspca_dev *gspca_dev)
282 {
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);
288 }
289
290 /* move a packet adding 0x00 after 0xff */
291 static void add_packet(struct gspca_dev *gspca_dev,
292                         u8 *data,
293                         int len)
294 {
295         int i;
296
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;
306                 }
307         } while (++i < len);
308         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
309 }
310
311 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
312                         u8 *data,                       /* isoc packet */
313                         int len)                        /* iso packet length */
314 {
315         struct sd *sd = (struct sd *) gspca_dev;
316         static const u8 ffd9[] = {0xff, 0xd9};
317
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
323          */
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;
333                 }
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;
341         }
342 err:
343         gspca_dev->last_packet_type = DISCARD_PACKET;
344 }
345
346 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
347 {
348         struct gspca_dev *gspca_dev =
349                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
350
351         gspca_dev->usb_err = 0;
352
353         if (!gspca_dev->streaming)
354                 return 0;
355
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;
372         }
373         return gspca_dev->usb_err;
374 }
375
376 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
377         .s_ctrl = sd_s_ctrl,
378 };
379
380 static int sd_init_controls(struct gspca_dev *gspca_dev)
381 {
382         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
383
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);
396
397         if (hdl->error) {
398                 pr_err("Could not initialize controls\n");
399                 return hdl->error;
400         }
401         return 0;
402 }
403
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,
414 };
415
416 /* -- module initialisation -- */
417 static const struct usb_device_id device_table[] = {
418         {USB_DEVICE(0x04fc, 0x1528)},
419         {}
420 };
421 MODULE_DEVICE_TABLE(usb, device_table);
422
423 /* -- device connect -- */
424 static int sd_probe(struct usb_interface *intf,
425                         const struct usb_device_id *id)
426 {
427         /* the video interface for isochronous transfer is 1 */
428         if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
429                 return -ENODEV;
430
431         return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
432                                 THIS_MODULE);
433 }
434
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
445 };
446
447 module_usb_driver(sd_driver);