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