18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for USB webcams based on Konica chipset. This
48c2ecf20Sopenharmony_ci * chipset is used in Intel YC76 camera.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Based on the usbvideo v4l1 konicawc driver which is:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The code for making gspca work with a webcam with 2 isoc endpoints was
138c2ecf20Sopenharmony_ci * taken from the benq gspca subdriver which is:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define MODULE_NAME "konica"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/input.h>
238c2ecf20Sopenharmony_ci#include "gspca.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
268c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Konica chipset USB Camera Driver");
278c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define WHITEBAL_REG   0x01
308c2ecf20Sopenharmony_ci#define BRIGHTNESS_REG 0x02
318c2ecf20Sopenharmony_ci#define SHARPNESS_REG  0x03
328c2ecf20Sopenharmony_ci#define CONTRAST_REG   0x04
338c2ecf20Sopenharmony_ci#define SATURATION_REG 0x05
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* specific webcam descriptor */
368c2ecf20Sopenharmony_cistruct sd {
378c2ecf20Sopenharmony_ci	struct gspca_dev gspca_dev;	/* !! must be the first item */
388c2ecf20Sopenharmony_ci	struct urb *last_data_urb;
398c2ecf20Sopenharmony_ci	u8 snapshot_pressed;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* .priv is what goes to register 8 for this mode, known working values:
448c2ecf20Sopenharmony_ci   0x00 -> 176x144, cropped
458c2ecf20Sopenharmony_ci   0x01 -> 176x144, cropped
468c2ecf20Sopenharmony_ci   0x02 -> 176x144, cropped
478c2ecf20Sopenharmony_ci   0x03 -> 176x144, cropped
488c2ecf20Sopenharmony_ci   0x04 -> 176x144, binned
498c2ecf20Sopenharmony_ci   0x05 -> 320x240
508c2ecf20Sopenharmony_ci   0x06 -> 320x240
518c2ecf20Sopenharmony_ci   0x07 -> 160x120, cropped
528c2ecf20Sopenharmony_ci   0x08 -> 160x120, cropped
538c2ecf20Sopenharmony_ci   0x09 -> 160x120, binned (note has 136 lines)
548c2ecf20Sopenharmony_ci   0x0a -> 160x120, binned (note has 136 lines)
558c2ecf20Sopenharmony_ci   0x0b -> 160x120, cropped
568c2ecf20Sopenharmony_ci*/
578c2ecf20Sopenharmony_cistatic const struct v4l2_pix_format vga_mode[] = {
588c2ecf20Sopenharmony_ci	{160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
598c2ecf20Sopenharmony_ci		.bytesperline = 160,
608c2ecf20Sopenharmony_ci		.sizeimage = 160 * 136 * 3 / 2 + 960,
618c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
628c2ecf20Sopenharmony_ci		.priv = 0x0a},
638c2ecf20Sopenharmony_ci	{176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
648c2ecf20Sopenharmony_ci		.bytesperline = 176,
658c2ecf20Sopenharmony_ci		.sizeimage = 176 * 144 * 3 / 2 + 960,
668c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
678c2ecf20Sopenharmony_ci		.priv = 0x04},
688c2ecf20Sopenharmony_ci	{320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
698c2ecf20Sopenharmony_ci		.bytesperline = 320,
708c2ecf20Sopenharmony_ci		.sizeimage = 320 * 240 * 3 / 2 + 960,
718c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
728c2ecf20Sopenharmony_ci		.priv = 0x05},
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void sd_isoc_irq(struct urb *urb);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct usb_device *dev = gspca_dev->dev;
808c2ecf20Sopenharmony_ci	int ret;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (gspca_dev->usb_err < 0)
838c2ecf20Sopenharmony_ci		return;
848c2ecf20Sopenharmony_ci	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
858c2ecf20Sopenharmony_ci			0x02,
868c2ecf20Sopenharmony_ci			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
878c2ecf20Sopenharmony_ci			value,
888c2ecf20Sopenharmony_ci			index,
898c2ecf20Sopenharmony_ci			NULL,
908c2ecf20Sopenharmony_ci			0,
918c2ecf20Sopenharmony_ci			1000);
928c2ecf20Sopenharmony_ci	if (ret < 0) {
938c2ecf20Sopenharmony_ci		pr_err("reg_w err writing %02x to %02x: %d\n",
948c2ecf20Sopenharmony_ci		       value, index, ret);
958c2ecf20Sopenharmony_ci		gspca_dev->usb_err = ret;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct usb_device *dev = gspca_dev->dev;
1028c2ecf20Sopenharmony_ci	int ret;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (gspca_dev->usb_err < 0)
1058c2ecf20Sopenharmony_ci		return;
1068c2ecf20Sopenharmony_ci	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1078c2ecf20Sopenharmony_ci			0x03,
1088c2ecf20Sopenharmony_ci			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1098c2ecf20Sopenharmony_ci			value,
1108c2ecf20Sopenharmony_ci			index,
1118c2ecf20Sopenharmony_ci			gspca_dev->usb_buf,
1128c2ecf20Sopenharmony_ci			2,
1138c2ecf20Sopenharmony_ci			1000);
1148c2ecf20Sopenharmony_ci	if (ret < 0) {
1158c2ecf20Sopenharmony_ci		pr_err("reg_r err %d\n", ret);
1168c2ecf20Sopenharmony_ci		gspca_dev->usb_err = ret;
1178c2ecf20Sopenharmony_ci		/*
1188c2ecf20Sopenharmony_ci		 * Make sure the buffer is zeroed to avoid uninitialized
1198c2ecf20Sopenharmony_ci		 * values.
1208c2ecf20Sopenharmony_ci		 */
1218c2ecf20Sopenharmony_ci		memset(gspca_dev->usb_buf, 0, 2);
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic void konica_stream_on(struct gspca_dev *gspca_dev)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 1, 0x0b);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic void konica_stream_off(struct gspca_dev *gspca_dev)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0, 0x0b);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* this function is called at probe time */
1368c2ecf20Sopenharmony_cistatic int sd_config(struct gspca_dev *gspca_dev,
1378c2ecf20Sopenharmony_ci			const struct usb_device_id *id)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	gspca_dev->cam.cam_mode = vga_mode;
1408c2ecf20Sopenharmony_ci	gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
1418c2ecf20Sopenharmony_ci	gspca_dev->cam.no_urb_create = 1;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return 0;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/* this function is called at probe and resume time */
1478c2ecf20Sopenharmony_cistatic int sd_init(struct gspca_dev *gspca_dev)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int i;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/*
1528c2ecf20Sopenharmony_ci	 * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
1538c2ecf20Sopenharmony_ci	 * and does not want to be bothered while doing so :|
1548c2ecf20Sopenharmony_ci	 * Register 0x10 counts from 1 - 3, with 3 being "ready"
1558c2ecf20Sopenharmony_ci	 */
1568c2ecf20Sopenharmony_ci	msleep(6000);
1578c2ecf20Sopenharmony_ci	for (i = 0; i < 20; i++) {
1588c2ecf20Sopenharmony_ci		reg_r(gspca_dev, 0, 0x10);
1598c2ecf20Sopenharmony_ci		if (gspca_dev->usb_buf[0] == 3)
1608c2ecf20Sopenharmony_ci			break;
1618c2ecf20Sopenharmony_ci		msleep(100);
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0, 0x0d);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return gspca_dev->usb_err;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic int sd_start(struct gspca_dev *gspca_dev)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
1718c2ecf20Sopenharmony_ci	struct urb *urb;
1728c2ecf20Sopenharmony_ci	int i, n, packet_size;
1738c2ecf20Sopenharmony_ci	struct usb_host_interface *alt;
1748c2ecf20Sopenharmony_ci	struct usb_interface *intf;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
1778c2ecf20Sopenharmony_ci	alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
1788c2ecf20Sopenharmony_ci	if (!alt) {
1798c2ecf20Sopenharmony_ci		pr_err("Couldn't get altsetting\n");
1808c2ecf20Sopenharmony_ci		return -EIO;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (alt->desc.bNumEndpoints < 2)
1848c2ecf20Sopenharmony_ci		return -ENODEV;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
1898c2ecf20Sopenharmony_ci	reg_w(gspca_dev, n, 0x08);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	konica_stream_on(gspca_dev);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (gspca_dev->usb_err)
1948c2ecf20Sopenharmony_ci		return gspca_dev->usb_err;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
1978c2ecf20Sopenharmony_ci#if MAX_NURBS < 4
1988c2ecf20Sopenharmony_ci#error "Not enough URBs in the gspca table"
1998c2ecf20Sopenharmony_ci#endif
2008c2ecf20Sopenharmony_ci#define SD_NPKT 32
2018c2ecf20Sopenharmony_ci	for (n = 0; n < 4; n++) {
2028c2ecf20Sopenharmony_ci		i = n & 1 ? 0 : 1;
2038c2ecf20Sopenharmony_ci		packet_size =
2048c2ecf20Sopenharmony_ci			le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
2058c2ecf20Sopenharmony_ci		urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
2068c2ecf20Sopenharmony_ci		if (!urb)
2078c2ecf20Sopenharmony_ci			return -ENOMEM;
2088c2ecf20Sopenharmony_ci		gspca_dev->urb[n] = urb;
2098c2ecf20Sopenharmony_ci		urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
2108c2ecf20Sopenharmony_ci						packet_size * SD_NPKT,
2118c2ecf20Sopenharmony_ci						GFP_KERNEL,
2128c2ecf20Sopenharmony_ci						&urb->transfer_dma);
2138c2ecf20Sopenharmony_ci		if (urb->transfer_buffer == NULL) {
2148c2ecf20Sopenharmony_ci			pr_err("usb_buffer_alloc failed\n");
2158c2ecf20Sopenharmony_ci			return -ENOMEM;
2168c2ecf20Sopenharmony_ci		}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		urb->dev = gspca_dev->dev;
2198c2ecf20Sopenharmony_ci		urb->context = gspca_dev;
2208c2ecf20Sopenharmony_ci		urb->transfer_buffer_length = packet_size * SD_NPKT;
2218c2ecf20Sopenharmony_ci		urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
2228c2ecf20Sopenharmony_ci					n & 1 ? 0x81 : 0x82);
2238c2ecf20Sopenharmony_ci		urb->transfer_flags = URB_ISO_ASAP
2248c2ecf20Sopenharmony_ci					| URB_NO_TRANSFER_DMA_MAP;
2258c2ecf20Sopenharmony_ci		urb->interval = 1;
2268c2ecf20Sopenharmony_ci		urb->complete = sd_isoc_irq;
2278c2ecf20Sopenharmony_ci		urb->number_of_packets = SD_NPKT;
2288c2ecf20Sopenharmony_ci		for (i = 0; i < SD_NPKT; i++) {
2298c2ecf20Sopenharmony_ci			urb->iso_frame_desc[i].length = packet_size;
2308c2ecf20Sopenharmony_ci			urb->iso_frame_desc[i].offset = packet_size * i;
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	return 0;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic void sd_stopN(struct gspca_dev *gspca_dev)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	konica_stream_off(gspca_dev);
2428c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_INPUT)
2438c2ecf20Sopenharmony_ci	/* Don't keep the button in the pressed state "forever" if it was
2448c2ecf20Sopenharmony_ci	   pressed when streaming is stopped */
2458c2ecf20Sopenharmony_ci	if (sd->snapshot_pressed) {
2468c2ecf20Sopenharmony_ci		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
2478c2ecf20Sopenharmony_ci		input_sync(gspca_dev->input_dev);
2488c2ecf20Sopenharmony_ci		sd->snapshot_pressed = 0;
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci#endif
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/* reception of an URB */
2548c2ecf20Sopenharmony_cistatic void sd_isoc_irq(struct urb *urb)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
2578c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
2588c2ecf20Sopenharmony_ci	struct urb *data_urb, *status_urb;
2598c2ecf20Sopenharmony_ci	u8 *data;
2608c2ecf20Sopenharmony_ci	int i, st;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_PACK, "sd isoc irq\n");
2638c2ecf20Sopenharmony_ci	if (!gspca_dev->streaming)
2648c2ecf20Sopenharmony_ci		return;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (urb->status != 0) {
2678c2ecf20Sopenharmony_ci		if (urb->status == -ESHUTDOWN)
2688c2ecf20Sopenharmony_ci			return;		/* disconnection */
2698c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
2708c2ecf20Sopenharmony_ci		if (gspca_dev->frozen)
2718c2ecf20Sopenharmony_ci			return;
2728c2ecf20Sopenharmony_ci#endif
2738c2ecf20Sopenharmony_ci		gspca_err(gspca_dev, "urb status: %d\n", urb->status);
2748c2ecf20Sopenharmony_ci		st = usb_submit_urb(urb, GFP_ATOMIC);
2758c2ecf20Sopenharmony_ci		if (st < 0)
2768c2ecf20Sopenharmony_ci			pr_err("resubmit urb error %d\n", st);
2778c2ecf20Sopenharmony_ci		return;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* if this is a data URB (ep 0x82), wait */
2818c2ecf20Sopenharmony_ci	if (urb->transfer_buffer_length > 32) {
2828c2ecf20Sopenharmony_ci		sd->last_data_urb = urb;
2838c2ecf20Sopenharmony_ci		return;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	status_urb = urb;
2878c2ecf20Sopenharmony_ci	data_urb   = sd->last_data_urb;
2888c2ecf20Sopenharmony_ci	sd->last_data_urb = NULL;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
2918c2ecf20Sopenharmony_ci		gspca_err(gspca_dev, "lost sync on frames\n");
2928c2ecf20Sopenharmony_ci		goto resubmit;
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (data_urb->number_of_packets != status_urb->number_of_packets) {
2968c2ecf20Sopenharmony_ci		gspca_err(gspca_dev, "no packets does not match, data: %d, status: %d\n",
2978c2ecf20Sopenharmony_ci			  data_urb->number_of_packets,
2988c2ecf20Sopenharmony_ci			  status_urb->number_of_packets);
2998c2ecf20Sopenharmony_ci		goto resubmit;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	for (i = 0; i < status_urb->number_of_packets; i++) {
3038c2ecf20Sopenharmony_ci		if (data_urb->iso_frame_desc[i].status ||
3048c2ecf20Sopenharmony_ci		    status_urb->iso_frame_desc[i].status) {
3058c2ecf20Sopenharmony_ci			gspca_err(gspca_dev, "pkt %d data-status %d, status-status %d\n",
3068c2ecf20Sopenharmony_ci				  i,
3078c2ecf20Sopenharmony_ci				  data_urb->iso_frame_desc[i].status,
3088c2ecf20Sopenharmony_ci				  status_urb->iso_frame_desc[i].status);
3098c2ecf20Sopenharmony_ci			gspca_dev->last_packet_type = DISCARD_PACKET;
3108c2ecf20Sopenharmony_ci			continue;
3118c2ecf20Sopenharmony_ci		}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci		if (status_urb->iso_frame_desc[i].actual_length != 1) {
3148c2ecf20Sopenharmony_ci			gspca_err(gspca_dev, "bad status packet length %d\n",
3158c2ecf20Sopenharmony_ci				  status_urb->iso_frame_desc[i].actual_length);
3168c2ecf20Sopenharmony_ci			gspca_dev->last_packet_type = DISCARD_PACKET;
3178c2ecf20Sopenharmony_ci			continue;
3188c2ecf20Sopenharmony_ci		}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		st = *((u8 *)status_urb->transfer_buffer
3218c2ecf20Sopenharmony_ci				+ status_urb->iso_frame_desc[i].offset);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci		data = (u8 *)data_urb->transfer_buffer
3248c2ecf20Sopenharmony_ci				+ data_urb->iso_frame_desc[i].offset;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci		/* st: 0x80-0xff: frame start with frame number (ie 0-7f)
3278c2ecf20Sopenharmony_ci		 * otherwise:
3288c2ecf20Sopenharmony_ci		 * bit 0 0: keep packet
3298c2ecf20Sopenharmony_ci		 *	 1: drop packet (padding data)
3308c2ecf20Sopenharmony_ci		 *
3318c2ecf20Sopenharmony_ci		 * bit 4 0 button not clicked
3328c2ecf20Sopenharmony_ci		 *       1 button clicked
3338c2ecf20Sopenharmony_ci		 * button is used to `take a picture' (in software)
3348c2ecf20Sopenharmony_ci		 */
3358c2ecf20Sopenharmony_ci		if (st & 0x80) {
3368c2ecf20Sopenharmony_ci			gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
3378c2ecf20Sopenharmony_ci			gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
3388c2ecf20Sopenharmony_ci		} else {
3398c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_INPUT)
3408c2ecf20Sopenharmony_ci			u8 button_state = st & 0x40 ? 1 : 0;
3418c2ecf20Sopenharmony_ci			if (sd->snapshot_pressed != button_state) {
3428c2ecf20Sopenharmony_ci				input_report_key(gspca_dev->input_dev,
3438c2ecf20Sopenharmony_ci						 KEY_CAMERA,
3448c2ecf20Sopenharmony_ci						 button_state);
3458c2ecf20Sopenharmony_ci				input_sync(gspca_dev->input_dev);
3468c2ecf20Sopenharmony_ci				sd->snapshot_pressed = button_state;
3478c2ecf20Sopenharmony_ci			}
3488c2ecf20Sopenharmony_ci#endif
3498c2ecf20Sopenharmony_ci			if (st & 0x01)
3508c2ecf20Sopenharmony_ci				continue;
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci		gspca_frame_add(gspca_dev, INTER_PACKET, data,
3538c2ecf20Sopenharmony_ci				data_urb->iso_frame_desc[i].actual_length);
3548c2ecf20Sopenharmony_ci	}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ciresubmit:
3578c2ecf20Sopenharmony_ci	if (data_urb) {
3588c2ecf20Sopenharmony_ci		st = usb_submit_urb(data_urb, GFP_ATOMIC);
3598c2ecf20Sopenharmony_ci		if (st < 0)
3608c2ecf20Sopenharmony_ci			gspca_err(gspca_dev, "usb_submit_urb(data_urb) ret %d\n",
3618c2ecf20Sopenharmony_ci				  st);
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci	st = usb_submit_urb(status_urb, GFP_ATOMIC);
3648c2ecf20Sopenharmony_ci	if (st < 0)
3658c2ecf20Sopenharmony_ci		gspca_err(gspca_dev, "usb_submit_urb(status_urb) ret %d\n", st);
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic int sd_s_ctrl(struct v4l2_ctrl *ctrl)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev =
3718c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	gspca_dev->usb_err = 0;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (!gspca_dev->streaming)
3768c2ecf20Sopenharmony_ci		return 0;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	switch (ctrl->id) {
3798c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
3808c2ecf20Sopenharmony_ci		konica_stream_off(gspca_dev);
3818c2ecf20Sopenharmony_ci		reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
3828c2ecf20Sopenharmony_ci		konica_stream_on(gspca_dev);
3838c2ecf20Sopenharmony_ci		break;
3848c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
3858c2ecf20Sopenharmony_ci		konica_stream_off(gspca_dev);
3868c2ecf20Sopenharmony_ci		reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
3878c2ecf20Sopenharmony_ci		konica_stream_on(gspca_dev);
3888c2ecf20Sopenharmony_ci		break;
3898c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
3908c2ecf20Sopenharmony_ci		konica_stream_off(gspca_dev);
3918c2ecf20Sopenharmony_ci		reg_w(gspca_dev, ctrl->val, SATURATION_REG);
3928c2ecf20Sopenharmony_ci		konica_stream_on(gspca_dev);
3938c2ecf20Sopenharmony_ci		break;
3948c2ecf20Sopenharmony_ci	case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
3958c2ecf20Sopenharmony_ci		konica_stream_off(gspca_dev);
3968c2ecf20Sopenharmony_ci		reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
3978c2ecf20Sopenharmony_ci		konica_stream_on(gspca_dev);
3988c2ecf20Sopenharmony_ci		break;
3998c2ecf20Sopenharmony_ci	case V4L2_CID_SHARPNESS:
4008c2ecf20Sopenharmony_ci		konica_stream_off(gspca_dev);
4018c2ecf20Sopenharmony_ci		reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
4028c2ecf20Sopenharmony_ci		konica_stream_on(gspca_dev);
4038c2ecf20Sopenharmony_ci		break;
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	return gspca_dev->usb_err;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops sd_ctrl_ops = {
4098c2ecf20Sopenharmony_ci	.s_ctrl = sd_s_ctrl,
4108c2ecf20Sopenharmony_ci};
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic int sd_init_controls(struct gspca_dev *gspca_dev)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	gspca_dev->vdev.ctrl_handler = hdl;
4178c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 5);
4188c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4198c2ecf20Sopenharmony_ci			V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
4208c2ecf20Sopenharmony_ci	/* Needs to be verified */
4218c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4228c2ecf20Sopenharmony_ci			V4L2_CID_CONTRAST, 0, 9, 1, 4);
4238c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4248c2ecf20Sopenharmony_ci			V4L2_CID_SATURATION, 0, 9, 1, 4);
4258c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4268c2ecf20Sopenharmony_ci			V4L2_CID_WHITE_BALANCE_TEMPERATURE,
4278c2ecf20Sopenharmony_ci			0, 33, 1, 25);
4288c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
4298c2ecf20Sopenharmony_ci			V4L2_CID_SHARPNESS, 0, 9, 1, 4);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	if (hdl->error) {
4328c2ecf20Sopenharmony_ci		pr_err("Could not initialize controls\n");
4338c2ecf20Sopenharmony_ci		return hdl->error;
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci	return 0;
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci/* sub-driver description */
4398c2ecf20Sopenharmony_cistatic const struct sd_desc sd_desc = {
4408c2ecf20Sopenharmony_ci	.name = MODULE_NAME,
4418c2ecf20Sopenharmony_ci	.config = sd_config,
4428c2ecf20Sopenharmony_ci	.init = sd_init,
4438c2ecf20Sopenharmony_ci	.init_controls = sd_init_controls,
4448c2ecf20Sopenharmony_ci	.start = sd_start,
4458c2ecf20Sopenharmony_ci	.stopN = sd_stopN,
4468c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_INPUT)
4478c2ecf20Sopenharmony_ci	.other_input = 1,
4488c2ecf20Sopenharmony_ci#endif
4498c2ecf20Sopenharmony_ci};
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/* -- module initialisation -- */
4528c2ecf20Sopenharmony_cistatic const struct usb_device_id device_table[] = {
4538c2ecf20Sopenharmony_ci	{USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
4548c2ecf20Sopenharmony_ci	{}
4558c2ecf20Sopenharmony_ci};
4568c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, device_table);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci/* -- device connect -- */
4598c2ecf20Sopenharmony_cistatic int sd_probe(struct usb_interface *intf,
4608c2ecf20Sopenharmony_ci			const struct usb_device_id *id)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
4638c2ecf20Sopenharmony_ci				THIS_MODULE);
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic struct usb_driver sd_driver = {
4678c2ecf20Sopenharmony_ci	.name = MODULE_NAME,
4688c2ecf20Sopenharmony_ci	.id_table = device_table,
4698c2ecf20Sopenharmony_ci	.probe = sd_probe,
4708c2ecf20Sopenharmony_ci	.disconnect = gspca_disconnect,
4718c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4728c2ecf20Sopenharmony_ci	.suspend = gspca_suspend,
4738c2ecf20Sopenharmony_ci	.resume = gspca_resume,
4748c2ecf20Sopenharmony_ci	.reset_resume = gspca_resume,
4758c2ecf20Sopenharmony_ci#endif
4768c2ecf20Sopenharmony_ci};
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cimodule_usb_driver(sd_driver);
479