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