GNU Linux-libre 4.14.266-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         PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", 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         PDEBUG(D_USBO, "SET %02x %04x %04x", 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         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),
127                         req,
128                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
129                         value, index,
130                         gspca_dev->usb_buf, 1, 500);
131         if (ret < 0) {
132                 pr_err("reg_w err %d\n", ret);
133                 gspca_dev->usb_err = ret;
134         }
135 }
136
137 static void wait_status_0(struct gspca_dev *gspca_dev)
138 {
139         int i, w;
140
141         i = 16;
142         w = 0;
143         do {
144                 reg_r(gspca_dev, 0x21, 0x0000, 1);
145                 if (gspca_dev->usb_buf[0] == 0)
146                         return;
147                 w += 15;
148                 msleep(w);
149         } while (--i > 0);
150         PERR("wait_status_0 timeout");
151         gspca_dev->usb_err = -ETIME;
152 }
153
154 static void wait_status_1(struct gspca_dev *gspca_dev)
155 {
156         int i;
157
158         i = 10;
159         do {
160                 reg_r(gspca_dev, 0x21, 0x0001, 1);
161                 msleep(10);
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);
165                         return;
166                 }
167         } while (--i > 0);
168         PERR("wait_status_1 timeout");
169         gspca_dev->usb_err = -ETIME;
170 }
171
172 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
173 {
174         reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
175 }
176
177 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
178 {
179         reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
180 }
181
182 static void sethue(struct gspca_dev *gspca_dev, s32 val)
183 {
184         reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
185 }
186
187 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
188 {
189         reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
190 }
191
192 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
193 {
194         reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
195 }
196
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)
200 {
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*/
205
206         return 0;
207 }
208
209 /* this function is called at probe and resume time */
210 static int sd_init(struct gspca_dev *gspca_dev)
211 {
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);
216         msleep(8);
217         reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
218         reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
219         reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
220
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;
228 }
229
230 /* function called at start time before URB creation */
231 static int sd_isoc_init(struct gspca_dev *gspca_dev)
232 {
233         u8 mode;
234
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);
239
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);
246
247 /* not useful..
248         gspca_dev->alt = 4;             * use alternate setting 3 */
249
250         return gspca_dev->usb_err;
251 }
252
253 /* -- start the camera -- */
254 static int sd_start(struct gspca_dev *gspca_dev)
255 {
256         struct sd *sd = (struct sd *) gspca_dev;
257
258         /* initialize the JPEG header */
259         jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
260                         gspca_dev->pixfmt.width,
261                         0x22);          /* JPEG 411 */
262
263         /* the JPEG quality shall be 85% */
264         jpeg_set_qual(sd->jpeg_hdr, 85);
265
266         reg_r(gspca_dev, 0x00, 0x2520, 1);
267         msleep(8);
268
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);
274         msleep(200);
275
276         sd->pkt_seq = 0;
277         return gspca_dev->usb_err;
278 }
279
280 static void sd_stopN(struct gspca_dev *gspca_dev)
281 {
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);
287 }
288
289 /* move a packet adding 0x00 after 0xff */
290 static void add_packet(struct gspca_dev *gspca_dev,
291                         u8 *data,
292                         int len)
293 {
294         int i;
295
296         i = 0;
297         do {
298                 if (data[i] == 0xff) {
299                         gspca_frame_add(gspca_dev, INTER_PACKET,
300                                         data, i + 1);
301                         len -= i;
302                         data += i;
303                         *data = 0x00;
304                         i = 0;
305                 }
306         } while (++i < len);
307         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
308 }
309
310 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
311                         u8 *data,                       /* isoc packet */
312                         int len)                        /* iso packet length */
313 {
314         struct sd *sd = (struct sd *) gspca_dev;
315         static const u8 ffd9[] = {0xff, 0xd9};
316
317         /* image packets start with:
318          *      02 8n
319          * with <n> bit:
320          *      0x01: even (0) / odd (1) image
321          *      0x02: end of image when set
322          */
323         if (len < 3)
324                 return;                         /* empty packet */
325         if (*data == 0x02) {
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,
330                                         ffd9, 2);
331                         return;
332                 }
333                 if ((data[1] & 1) != sd->pkt_seq)
334                         goto err;
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);
339                 return;
340         }
341 err:
342         gspca_dev->last_packet_type = DISCARD_PACKET;
343 }
344
345 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
346 {
347         struct gspca_dev *gspca_dev =
348                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
349
350         gspca_dev->usb_err = 0;
351
352         if (!gspca_dev->streaming)
353                 return 0;
354
355         switch (ctrl->id) {
356         case V4L2_CID_BRIGHTNESS:
357                 setbrightness(gspca_dev, ctrl->val);
358                 break;
359         case V4L2_CID_CONTRAST:
360                 setcontrast(gspca_dev, ctrl->val);
361                 break;
362         case V4L2_CID_HUE:
363                 sethue(gspca_dev, ctrl->val);
364                 break;
365         case V4L2_CID_SATURATION:
366                 setcolor(gspca_dev, ctrl->val);
367                 break;
368         case V4L2_CID_SHARPNESS:
369                 setsharpness(gspca_dev, ctrl->val);
370                 break;
371         }
372         return gspca_dev->usb_err;
373 }
374
375 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
376         .s_ctrl = sd_s_ctrl,
377 };
378
379 static int sd_init_controls(struct gspca_dev *gspca_dev)
380 {
381         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
382
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);
395
396         if (hdl->error) {
397                 pr_err("Could not initialize controls\n");
398                 return hdl->error;
399         }
400         return 0;
401 }
402
403 /* sub-driver description */
404 static const struct sd_desc sd_desc = {
405         .name = MODULE_NAME,
406         .config = sd_config,
407         .init = sd_init,
408         .init_controls = sd_init_controls,
409         .isoc_init = sd_isoc_init,
410         .start = sd_start,
411         .stopN = sd_stopN,
412         .pkt_scan = sd_pkt_scan,
413 };
414
415 /* -- module initialisation -- */
416 static const struct usb_device_id device_table[] = {
417         {USB_DEVICE(0x04fc, 0x1528)},
418         {}
419 };
420 MODULE_DEVICE_TABLE(usb, device_table);
421
422 /* -- device connect -- */
423 static int sd_probe(struct usb_interface *intf,
424                         const struct usb_device_id *id)
425 {
426         /* the video interface for isochronous transfer is 1 */
427         if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
428                 return -ENODEV;
429
430         return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
431                                 THIS_MODULE);
432 }
433
434 static struct usb_driver sd_driver = {
435         .name = MODULE_NAME,
436         .id_table = device_table,
437         .probe = sd_probe,
438         .disconnect = gspca_disconnect,
439 #ifdef CONFIG_PM
440         .suspend = gspca_suspend,
441         .resume = gspca_resume,
442         .reset_resume = gspca_resume,
443 #endif
444 };
445
446 module_usb_driver(sd_driver);