GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / media / usb / gspca / konica.c
1 /*
2  * Driver for USB webcams based on Konica chipset. This
3  * chipset is used in Intel YC76 camera.
4  *
5  * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
6  *
7  * Based on the usbvideo v4l1 konicawc driver which is:
8  *
9  * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
10  *
11  * The code for making gspca work with a webcam with 2 isoc endpoints was
12  * taken from the benq gspca subdriver which is:
13  *
14  * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 #define MODULE_NAME "konica"
34
35 #include <linux/input.h>
36 #include "gspca.h"
37
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
40 MODULE_LICENSE("GPL");
41
42 #define WHITEBAL_REG   0x01
43 #define BRIGHTNESS_REG 0x02
44 #define SHARPNESS_REG  0x03
45 #define CONTRAST_REG   0x04
46 #define SATURATION_REG 0x05
47
48 /* specific webcam descriptor */
49 struct sd {
50         struct gspca_dev gspca_dev;     /* !! must be the first item */
51         struct urb *last_data_urb;
52         u8 snapshot_pressed;
53 };
54
55
56 /* .priv is what goes to register 8 for this mode, known working values:
57    0x00 -> 176x144, cropped
58    0x01 -> 176x144, cropped
59    0x02 -> 176x144, cropped
60    0x03 -> 176x144, cropped
61    0x04 -> 176x144, binned
62    0x05 -> 320x240
63    0x06 -> 320x240
64    0x07 -> 160x120, cropped
65    0x08 -> 160x120, cropped
66    0x09 -> 160x120, binned (note has 136 lines)
67    0x0a -> 160x120, binned (note has 136 lines)
68    0x0b -> 160x120, cropped
69 */
70 static const struct v4l2_pix_format vga_mode[] = {
71         {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
72                 .bytesperline = 160,
73                 .sizeimage = 160 * 136 * 3 / 2 + 960,
74                 .colorspace = V4L2_COLORSPACE_SRGB,
75                 .priv = 0x0a},
76         {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
77                 .bytesperline = 176,
78                 .sizeimage = 176 * 144 * 3 / 2 + 960,
79                 .colorspace = V4L2_COLORSPACE_SRGB,
80                 .priv = 0x04},
81         {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
82                 .bytesperline = 320,
83                 .sizeimage = 320 * 240 * 3 / 2 + 960,
84                 .colorspace = V4L2_COLORSPACE_SRGB,
85                 .priv = 0x05},
86 };
87
88 static void sd_isoc_irq(struct urb *urb);
89
90 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
91 {
92         struct usb_device *dev = gspca_dev->dev;
93         int ret;
94
95         if (gspca_dev->usb_err < 0)
96                 return;
97         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
98                         0x02,
99                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
100                         value,
101                         index,
102                         NULL,
103                         0,
104                         1000);
105         if (ret < 0) {
106                 pr_err("reg_w err writing %02x to %02x: %d\n",
107                        value, index, ret);
108                 gspca_dev->usb_err = ret;
109         }
110 }
111
112 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
113 {
114         struct usb_device *dev = gspca_dev->dev;
115         int ret;
116
117         if (gspca_dev->usb_err < 0)
118                 return;
119         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
120                         0x03,
121                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122                         value,
123                         index,
124                         gspca_dev->usb_buf,
125                         2,
126                         1000);
127         if (ret < 0) {
128                 pr_err("reg_r err %d\n", ret);
129                 gspca_dev->usb_err = ret;
130                 /*
131                  * Make sure the buffer is zeroed to avoid uninitialized
132                  * values.
133                  */
134                 memset(gspca_dev->usb_buf, 0, 2);
135         }
136 }
137
138 static void konica_stream_on(struct gspca_dev *gspca_dev)
139 {
140         reg_w(gspca_dev, 1, 0x0b);
141 }
142
143 static void konica_stream_off(struct gspca_dev *gspca_dev)
144 {
145         reg_w(gspca_dev, 0, 0x0b);
146 }
147
148 /* this function is called at probe time */
149 static int sd_config(struct gspca_dev *gspca_dev,
150                         const struct usb_device_id *id)
151 {
152         gspca_dev->cam.cam_mode = vga_mode;
153         gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
154         gspca_dev->cam.no_urb_create = 1;
155
156         return 0;
157 }
158
159 /* this function is called at probe and resume time */
160 static int sd_init(struct gspca_dev *gspca_dev)
161 {
162         int i;
163
164         /*
165          * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
166          * and does not want to be bothered while doing so :|
167          * Register 0x10 counts from 1 - 3, with 3 being "ready"
168          */
169         msleep(6000);
170         for (i = 0; i < 20; i++) {
171                 reg_r(gspca_dev, 0, 0x10);
172                 if (gspca_dev->usb_buf[0] == 3)
173                         break;
174                 msleep(100);
175         }
176         reg_w(gspca_dev, 0, 0x0d);
177
178         return gspca_dev->usb_err;
179 }
180
181 static int sd_start(struct gspca_dev *gspca_dev)
182 {
183         struct sd *sd = (struct sd *) gspca_dev;
184         struct urb *urb;
185         int i, n, packet_size;
186         struct usb_host_interface *alt;
187         struct usb_interface *intf;
188
189         intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
190         alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
191         if (!alt) {
192                 pr_err("Couldn't get altsetting\n");
193                 return -EIO;
194         }
195
196         if (alt->desc.bNumEndpoints < 2)
197                 return -ENODEV;
198
199         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
200
201         n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
202         reg_w(gspca_dev, n, 0x08);
203
204         konica_stream_on(gspca_dev);
205
206         if (gspca_dev->usb_err)
207                 return gspca_dev->usb_err;
208
209         /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
210 #if MAX_NURBS < 4
211 #error "Not enough URBs in the gspca table"
212 #endif
213 #define SD_NPKT 32
214         for (n = 0; n < 4; n++) {
215                 i = n & 1 ? 0 : 1;
216                 packet_size =
217                         le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
218                 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
219                 if (!urb) {
220                         pr_err("usb_alloc_urb failed\n");
221                         return -ENOMEM;
222                 }
223                 gspca_dev->urb[n] = urb;
224                 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
225                                                 packet_size * SD_NPKT,
226                                                 GFP_KERNEL,
227                                                 &urb->transfer_dma);
228                 if (urb->transfer_buffer == NULL) {
229                         pr_err("usb_buffer_alloc failed\n");
230                         return -ENOMEM;
231                 }
232
233                 urb->dev = gspca_dev->dev;
234                 urb->context = gspca_dev;
235                 urb->transfer_buffer_length = packet_size * SD_NPKT;
236                 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
237                                         n & 1 ? 0x81 : 0x82);
238                 urb->transfer_flags = URB_ISO_ASAP
239                                         | URB_NO_TRANSFER_DMA_MAP;
240                 urb->interval = 1;
241                 urb->complete = sd_isoc_irq;
242                 urb->number_of_packets = SD_NPKT;
243                 for (i = 0; i < SD_NPKT; i++) {
244                         urb->iso_frame_desc[i].length = packet_size;
245                         urb->iso_frame_desc[i].offset = packet_size * i;
246                 }
247         }
248
249         return 0;
250 }
251
252 static void sd_stopN(struct gspca_dev *gspca_dev)
253 {
254         struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
255
256         konica_stream_off(gspca_dev);
257 #if IS_ENABLED(CONFIG_INPUT)
258         /* Don't keep the button in the pressed state "forever" if it was
259            pressed when streaming is stopped */
260         if (sd->snapshot_pressed) {
261                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
262                 input_sync(gspca_dev->input_dev);
263                 sd->snapshot_pressed = 0;
264         }
265 #endif
266 }
267
268 /* reception of an URB */
269 static void sd_isoc_irq(struct urb *urb)
270 {
271         struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
272         struct sd *sd = (struct sd *) gspca_dev;
273         struct urb *data_urb, *status_urb;
274         u8 *data;
275         int i, st;
276
277         PDEBUG(D_PACK, "sd isoc irq");
278         if (!gspca_dev->streaming)
279                 return;
280
281         if (urb->status != 0) {
282                 if (urb->status == -ESHUTDOWN)
283                         return;         /* disconnection */
284 #ifdef CONFIG_PM
285                 if (gspca_dev->frozen)
286                         return;
287 #endif
288                 PERR("urb status: %d", urb->status);
289                 st = usb_submit_urb(urb, GFP_ATOMIC);
290                 if (st < 0)
291                         pr_err("resubmit urb error %d\n", st);
292                 return;
293         }
294
295         /* if this is a data URB (ep 0x82), wait */
296         if (urb->transfer_buffer_length > 32) {
297                 sd->last_data_urb = urb;
298                 return;
299         }
300
301         status_urb = urb;
302         data_urb   = sd->last_data_urb;
303         sd->last_data_urb = NULL;
304
305         if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
306                 PERR("lost sync on frames");
307                 goto resubmit;
308         }
309
310         if (data_urb->number_of_packets != status_urb->number_of_packets) {
311                 PERR("no packets does not match, data: %d, status: %d",
312                      data_urb->number_of_packets,
313                      status_urb->number_of_packets);
314                 goto resubmit;
315         }
316
317         for (i = 0; i < status_urb->number_of_packets; i++) {
318                 if (data_urb->iso_frame_desc[i].status ||
319                     status_urb->iso_frame_desc[i].status) {
320                         PERR("pkt %d data-status %d, status-status %d", i,
321                              data_urb->iso_frame_desc[i].status,
322                              status_urb->iso_frame_desc[i].status);
323                         gspca_dev->last_packet_type = DISCARD_PACKET;
324                         continue;
325                 }
326
327                 if (status_urb->iso_frame_desc[i].actual_length != 1) {
328                         PERR("bad status packet length %d",
329                              status_urb->iso_frame_desc[i].actual_length);
330                         gspca_dev->last_packet_type = DISCARD_PACKET;
331                         continue;
332                 }
333
334                 st = *((u8 *)status_urb->transfer_buffer
335                                 + status_urb->iso_frame_desc[i].offset);
336
337                 data = (u8 *)data_urb->transfer_buffer
338                                 + data_urb->iso_frame_desc[i].offset;
339
340                 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
341                  * otherwise:
342                  * bit 0 0: keep packet
343                  *       1: drop packet (padding data)
344                  *
345                  * bit 4 0 button not clicked
346                  *       1 button clicked
347                  * button is used to `take a picture' (in software)
348                  */
349                 if (st & 0x80) {
350                         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
351                         gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
352                 } else {
353 #if IS_ENABLED(CONFIG_INPUT)
354                         u8 button_state = st & 0x40 ? 1 : 0;
355                         if (sd->snapshot_pressed != button_state) {
356                                 input_report_key(gspca_dev->input_dev,
357                                                  KEY_CAMERA,
358                                                  button_state);
359                                 input_sync(gspca_dev->input_dev);
360                                 sd->snapshot_pressed = button_state;
361                         }
362 #endif
363                         if (st & 0x01)
364                                 continue;
365                 }
366                 gspca_frame_add(gspca_dev, INTER_PACKET, data,
367                                 data_urb->iso_frame_desc[i].actual_length);
368         }
369
370 resubmit:
371         if (data_urb) {
372                 st = usb_submit_urb(data_urb, GFP_ATOMIC);
373                 if (st < 0)
374                         PERR("usb_submit_urb(data_urb) ret %d", st);
375         }
376         st = usb_submit_urb(status_urb, GFP_ATOMIC);
377         if (st < 0)
378                 PERR("usb_submit_urb(status_urb) ret %d\n", st);
379 }
380
381 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
382 {
383         struct gspca_dev *gspca_dev =
384                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
385
386         gspca_dev->usb_err = 0;
387
388         if (!gspca_dev->streaming)
389                 return 0;
390
391         switch (ctrl->id) {
392         case V4L2_CID_BRIGHTNESS:
393                 konica_stream_off(gspca_dev);
394                 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
395                 konica_stream_on(gspca_dev);
396                 break;
397         case V4L2_CID_CONTRAST:
398                 konica_stream_off(gspca_dev);
399                 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
400                 konica_stream_on(gspca_dev);
401                 break;
402         case V4L2_CID_SATURATION:
403                 konica_stream_off(gspca_dev);
404                 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
405                 konica_stream_on(gspca_dev);
406                 break;
407         case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
408                 konica_stream_off(gspca_dev);
409                 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
410                 konica_stream_on(gspca_dev);
411                 break;
412         case V4L2_CID_SHARPNESS:
413                 konica_stream_off(gspca_dev);
414                 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
415                 konica_stream_on(gspca_dev);
416                 break;
417         }
418         return gspca_dev->usb_err;
419 }
420
421 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
422         .s_ctrl = sd_s_ctrl,
423 };
424
425 static int sd_init_controls(struct gspca_dev *gspca_dev)
426 {
427         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
428
429         gspca_dev->vdev.ctrl_handler = hdl;
430         v4l2_ctrl_handler_init(hdl, 5);
431         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
432                         V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
433         /* Needs to be verified */
434         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
435                         V4L2_CID_CONTRAST, 0, 9, 1, 4);
436         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
437                         V4L2_CID_SATURATION, 0, 9, 1, 4);
438         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
439                         V4L2_CID_WHITE_BALANCE_TEMPERATURE,
440                         0, 33, 1, 25);
441         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
442                         V4L2_CID_SHARPNESS, 0, 9, 1, 4);
443
444         if (hdl->error) {
445                 pr_err("Could not initialize controls\n");
446                 return hdl->error;
447         }
448         return 0;
449 }
450
451 /* sub-driver description */
452 static const struct sd_desc sd_desc = {
453         .name = MODULE_NAME,
454         .config = sd_config,
455         .init = sd_init,
456         .init_controls = sd_init_controls,
457         .start = sd_start,
458         .stopN = sd_stopN,
459 #if IS_ENABLED(CONFIG_INPUT)
460         .other_input = 1,
461 #endif
462 };
463
464 /* -- module initialisation -- */
465 static const struct usb_device_id device_table[] = {
466         {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
467         {}
468 };
469 MODULE_DEVICE_TABLE(usb, device_table);
470
471 /* -- device connect -- */
472 static int sd_probe(struct usb_interface *intf,
473                         const struct usb_device_id *id)
474 {
475         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
476                                 THIS_MODULE);
477 }
478
479 static struct usb_driver sd_driver = {
480         .name = MODULE_NAME,
481         .id_table = device_table,
482         .probe = sd_probe,
483         .disconnect = gspca_disconnect,
484 #ifdef CONFIG_PM
485         .suspend = gspca_suspend,
486         .resume = gspca_resume,
487         .reset_resume = gspca_resume,
488 #endif
489 };
490
491 module_usb_driver(sd_driver);