GNU Linux-libre 4.4.284-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  * 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
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #define MODULE_NAME "spca1528"
24
25 #include "gspca.h"
26 #include "jpeg.h"
27
28 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
30 MODULE_LICENSE("GPL");
31
32 /* specific webcam descriptor */
33 struct sd {
34         struct gspca_dev gspca_dev;     /* !! must be the first item */
35
36         u8 pkt_seq;
37
38         u8 jpeg_hdr[JPEG_HDR_SZ];
39 };
40
41 static const struct v4l2_pix_format vga_mode[] = {
42 /*              (does not work correctly)
43         {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
44                 .bytesperline = 176,
45                 .sizeimage = 176 * 144 * 5 / 8 + 590,
46                 .colorspace = V4L2_COLORSPACE_JPEG,
47                 .priv = 3},
48 */
49         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
50                 .bytesperline = 320,
51                 .sizeimage = 320 * 240 * 4 / 8 + 590,
52                 .colorspace = V4L2_COLORSPACE_JPEG,
53                 .priv = 2},
54         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
55                 .bytesperline = 640,
56                 .sizeimage = 640 * 480 * 3 / 8 + 590,
57                 .colorspace = V4L2_COLORSPACE_JPEG,
58                 .priv = 1},
59 };
60
61 /* read <len> bytes to gspca usb_buf */
62 static void reg_r(struct gspca_dev *gspca_dev,
63                         u8 req,
64                         u16 index,
65                         int len)
66 {
67 #if USB_BUF_SZ < 64
68 #error "USB buffer too small"
69 #endif
70         struct usb_device *dev = gspca_dev->dev;
71         int ret;
72
73         if (gspca_dev->usb_err < 0)
74                 return;
75         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
76                         req,
77                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78                         0x0000,                 /* value */
79                         index,
80                         gspca_dev->usb_buf, len,
81                         500);
82         PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index,
83                          gspca_dev->usb_buf[0]);
84         if (ret < 0) {
85                 pr_err("reg_r err %d\n", ret);
86                 gspca_dev->usb_err = ret;
87                 /*
88                  * Make sure the buffer is zeroed to avoid uninitialized
89                  * values.
90                  */
91                 memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
92         }
93 }
94
95 static void reg_w(struct gspca_dev *gspca_dev,
96                         u8 req,
97                         u16 value,
98                         u16 index)
99 {
100         struct usb_device *dev = gspca_dev->dev;
101         int ret;
102
103         if (gspca_dev->usb_err < 0)
104                 return;
105         PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index);
106         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
107                         req,
108                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
109                         value, index,
110                         NULL, 0, 500);
111         if (ret < 0) {
112                 pr_err("reg_w err %d\n", ret);
113                 gspca_dev->usb_err = ret;
114         }
115 }
116
117 static void reg_wb(struct gspca_dev *gspca_dev,
118                         u8 req,
119                         u16 value,
120                         u16 index,
121                         u8 byte)
122 {
123         struct usb_device *dev = gspca_dev->dev;
124         int ret;
125
126         if (gspca_dev->usb_err < 0)
127                 return;
128         PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
129         gspca_dev->usb_buf[0] = byte;
130         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
131                         req,
132                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
133                         value, index,
134                         gspca_dev->usb_buf, 1, 500);
135         if (ret < 0) {
136                 pr_err("reg_w err %d\n", ret);
137                 gspca_dev->usb_err = ret;
138         }
139 }
140
141 static void wait_status_0(struct gspca_dev *gspca_dev)
142 {
143         int i, w;
144
145         i = 16;
146         w = 0;
147         do {
148                 reg_r(gspca_dev, 0x21, 0x0000, 1);
149                 if (gspca_dev->usb_buf[0] == 0)
150                         return;
151                 w += 15;
152                 msleep(w);
153         } while (--i > 0);
154         PERR("wait_status_0 timeout");
155         gspca_dev->usb_err = -ETIME;
156 }
157
158 static void wait_status_1(struct gspca_dev *gspca_dev)
159 {
160         int i;
161
162         i = 10;
163         do {
164                 reg_r(gspca_dev, 0x21, 0x0001, 1);
165                 msleep(10);
166                 if (gspca_dev->usb_buf[0] == 1) {
167                         reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
168                         reg_r(gspca_dev, 0x21, 0x0001, 1);
169                         return;
170                 }
171         } while (--i > 0);
172         PERR("wait_status_1 timeout");
173         gspca_dev->usb_err = -ETIME;
174 }
175
176 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
177 {
178         reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
179 }
180
181 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
182 {
183         reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
184 }
185
186 static void sethue(struct gspca_dev *gspca_dev, s32 val)
187 {
188         reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
189 }
190
191 static void setcolor(struct gspca_dev *gspca_dev, s32 val)
192 {
193         reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
194 }
195
196 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
197 {
198         reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
199 }
200
201 /* this function is called at probe time */
202 static int sd_config(struct gspca_dev *gspca_dev,
203                         const struct usb_device_id *id)
204 {
205         gspca_dev->cam.cam_mode = vga_mode;
206         gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
207         gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
208                         /*fixme: 256 in ms-win traces*/
209
210         return 0;
211 }
212
213 /* this function is called at probe and resume time */
214 static int sd_init(struct gspca_dev *gspca_dev)
215 {
216         reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
217         reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
218         reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
219         reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
220         msleep(8);
221         reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
222         reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
223         reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
224
225         reg_r(gspca_dev, 0x20, 0x0000, 1);
226         reg_r(gspca_dev, 0x20, 0x0000, 5);
227         reg_r(gspca_dev, 0x23, 0x0000, 64);
228         PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
229                                 &gspca_dev->usb_buf[0x30]);
230         reg_r(gspca_dev, 0x23, 0x0001, 64);
231         return gspca_dev->usb_err;
232 }
233
234 /* function called at start time before URB creation */
235 static int sd_isoc_init(struct gspca_dev *gspca_dev)
236 {
237         u8 mode;
238
239         reg_r(gspca_dev, 0x00, 0x2520, 1);
240         wait_status_0(gspca_dev);
241         reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
242         wait_status_1(gspca_dev);
243
244         wait_status_0(gspca_dev);
245         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
246         reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
247         reg_r(gspca_dev, 0x25, 0x0004, 1);
248         reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06);  /* 420 */
249         reg_r(gspca_dev, 0x27, 0x0000, 1);
250
251 /* not useful..
252         gspca_dev->alt = 4;             * use alternate setting 3 */
253
254         return gspca_dev->usb_err;
255 }
256
257 /* -- start the camera -- */
258 static int sd_start(struct gspca_dev *gspca_dev)
259 {
260         struct sd *sd = (struct sd *) gspca_dev;
261
262         /* initialize the JPEG header */
263         jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
264                         gspca_dev->pixfmt.width,
265                         0x22);          /* JPEG 411 */
266
267         /* the JPEG quality shall be 85% */
268         jpeg_set_qual(sd->jpeg_hdr, 85);
269
270         reg_r(gspca_dev, 0x00, 0x2520, 1);
271         msleep(8);
272
273         /* start the capture */
274         wait_status_0(gspca_dev);
275         reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
276         wait_status_1(gspca_dev);
277         wait_status_0(gspca_dev);
278         msleep(200);
279
280         sd->pkt_seq = 0;
281         return gspca_dev->usb_err;
282 }
283
284 static void sd_stopN(struct gspca_dev *gspca_dev)
285 {
286         /* stop the capture */
287         wait_status_0(gspca_dev);
288         reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
289         wait_status_1(gspca_dev);
290         wait_status_0(gspca_dev);
291 }
292
293 /* move a packet adding 0x00 after 0xff */
294 static void add_packet(struct gspca_dev *gspca_dev,
295                         u8 *data,
296                         int len)
297 {
298         int i;
299
300         i = 0;
301         do {
302                 if (data[i] == 0xff) {
303                         gspca_frame_add(gspca_dev, INTER_PACKET,
304                                         data, i + 1);
305                         len -= i;
306                         data += i;
307                         *data = 0x00;
308                         i = 0;
309                 }
310         } while (++i < len);
311         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
312 }
313
314 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
315                         u8 *data,                       /* isoc packet */
316                         int len)                        /* iso packet length */
317 {
318         struct sd *sd = (struct sd *) gspca_dev;
319         static const u8 ffd9[] = {0xff, 0xd9};
320
321         /* image packets start with:
322          *      02 8n
323          * with <n> bit:
324          *      0x01: even (0) / odd (1) image
325          *      0x02: end of image when set
326          */
327         if (len < 3)
328                 return;                         /* empty packet */
329         if (*data == 0x02) {
330                 if (data[1] & 0x02) {
331                         sd->pkt_seq = !(data[1] & 1);
332                         add_packet(gspca_dev, data + 2, len - 2);
333                         gspca_frame_add(gspca_dev, LAST_PACKET,
334                                         ffd9, 2);
335                         return;
336                 }
337                 if ((data[1] & 1) != sd->pkt_seq)
338                         goto err;
339                 if (gspca_dev->last_packet_type == LAST_PACKET)
340                         gspca_frame_add(gspca_dev, FIRST_PACKET,
341                                         sd->jpeg_hdr, JPEG_HDR_SZ);
342                 add_packet(gspca_dev, data + 2, len - 2);
343                 return;
344         }
345 err:
346         gspca_dev->last_packet_type = DISCARD_PACKET;
347 }
348
349 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
350 {
351         struct gspca_dev *gspca_dev =
352                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
353
354         gspca_dev->usb_err = 0;
355
356         if (!gspca_dev->streaming)
357                 return 0;
358
359         switch (ctrl->id) {
360         case V4L2_CID_BRIGHTNESS:
361                 setbrightness(gspca_dev, ctrl->val);
362                 break;
363         case V4L2_CID_CONTRAST:
364                 setcontrast(gspca_dev, ctrl->val);
365                 break;
366         case V4L2_CID_HUE:
367                 sethue(gspca_dev, ctrl->val);
368                 break;
369         case V4L2_CID_SATURATION:
370                 setcolor(gspca_dev, ctrl->val);
371                 break;
372         case V4L2_CID_SHARPNESS:
373                 setsharpness(gspca_dev, ctrl->val);
374                 break;
375         }
376         return gspca_dev->usb_err;
377 }
378
379 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
380         .s_ctrl = sd_s_ctrl,
381 };
382
383 static int sd_init_controls(struct gspca_dev *gspca_dev)
384 {
385         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
386
387         gspca_dev->vdev.ctrl_handler = hdl;
388         v4l2_ctrl_handler_init(hdl, 5);
389         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
390                         V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
391         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
392                         V4L2_CID_CONTRAST, 0, 8, 1, 1);
393         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
394                         V4L2_CID_HUE, 0, 255, 1, 0);
395         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
396                         V4L2_CID_SATURATION, 0, 8, 1, 1);
397         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
398                         V4L2_CID_SHARPNESS, 0, 255, 1, 0);
399
400         if (hdl->error) {
401                 pr_err("Could not initialize controls\n");
402                 return hdl->error;
403         }
404         return 0;
405 }
406
407 /* sub-driver description */
408 static const struct sd_desc sd_desc = {
409         .name = MODULE_NAME,
410         .config = sd_config,
411         .init = sd_init,
412         .init_controls = sd_init_controls,
413         .isoc_init = sd_isoc_init,
414         .start = sd_start,
415         .stopN = sd_stopN,
416         .pkt_scan = sd_pkt_scan,
417 };
418
419 /* -- module initialisation -- */
420 static const struct usb_device_id device_table[] = {
421         {USB_DEVICE(0x04fc, 0x1528)},
422         {}
423 };
424 MODULE_DEVICE_TABLE(usb, device_table);
425
426 /* -- device connect -- */
427 static int sd_probe(struct usb_interface *intf,
428                         const struct usb_device_id *id)
429 {
430         /* the video interface for isochronous transfer is 1 */
431         if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
432                 return -ENODEV;
433
434         return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
435                                 THIS_MODULE);
436 }
437
438 static struct usb_driver sd_driver = {
439         .name = MODULE_NAME,
440         .id_table = device_table,
441         .probe = sd_probe,
442         .disconnect = gspca_disconnect,
443 #ifdef CONFIG_PM
444         .suspend = gspca_suspend,
445         .resume = gspca_resume,
446         .reset_resume = gspca_resume,
447 #endif
448 };
449
450 module_usb_driver(sd_driver);