18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Benq DC E300 subdriver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define MODULE_NAME "benq"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "gspca.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
158c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
168c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* specific webcam descriptor */
198c2ecf20Sopenharmony_cistruct sd {
208c2ecf20Sopenharmony_ci	struct gspca_dev gspca_dev;	/* !! must be the first item */
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const struct v4l2_pix_format vga_mode[] = {
248c2ecf20Sopenharmony_ci	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
258c2ecf20Sopenharmony_ci		.bytesperline = 320,
268c2ecf20Sopenharmony_ci		.sizeimage = 320 * 240 * 3 / 8 + 590,
278c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_JPEG},
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic void sd_isoc_irq(struct urb *urb);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* -- write a register -- */
338c2ecf20Sopenharmony_cistatic void reg_w(struct gspca_dev *gspca_dev,
348c2ecf20Sopenharmony_ci			u16 value, u16 index)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct usb_device *dev = gspca_dev->dev;
378c2ecf20Sopenharmony_ci	int ret;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if (gspca_dev->usb_err < 0)
408c2ecf20Sopenharmony_ci		return;
418c2ecf20Sopenharmony_ci	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
428c2ecf20Sopenharmony_ci			0x02,
438c2ecf20Sopenharmony_ci			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
448c2ecf20Sopenharmony_ci			value,
458c2ecf20Sopenharmony_ci			index,
468c2ecf20Sopenharmony_ci			NULL,
478c2ecf20Sopenharmony_ci			0,
488c2ecf20Sopenharmony_ci			500);
498c2ecf20Sopenharmony_ci	if (ret < 0) {
508c2ecf20Sopenharmony_ci		pr_err("reg_w err %d\n", ret);
518c2ecf20Sopenharmony_ci		gspca_dev->usb_err = ret;
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* this function is called at probe time */
568c2ecf20Sopenharmony_cistatic int sd_config(struct gspca_dev *gspca_dev,
578c2ecf20Sopenharmony_ci			const struct usb_device_id *id)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	gspca_dev->cam.cam_mode = vga_mode;
608c2ecf20Sopenharmony_ci	gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
618c2ecf20Sopenharmony_ci	gspca_dev->cam.no_urb_create = 1;
628c2ecf20Sopenharmony_ci	return 0;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* this function is called at probe and resume time */
668c2ecf20Sopenharmony_cistatic int sd_init(struct gspca_dev *gspca_dev)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* -- start the camera -- */
728c2ecf20Sopenharmony_cistatic int sd_start(struct gspca_dev *gspca_dev)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct urb *urb;
758c2ecf20Sopenharmony_ci	int i, n;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
788c2ecf20Sopenharmony_ci#if MAX_NURBS < 4
798c2ecf20Sopenharmony_ci#error "Not enough URBs in the gspca table"
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci#define SD_PKT_SZ 64
828c2ecf20Sopenharmony_ci#define SD_NPKT 32
838c2ecf20Sopenharmony_ci	for (n = 0; n < 4; n++) {
848c2ecf20Sopenharmony_ci		urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
858c2ecf20Sopenharmony_ci		if (!urb)
868c2ecf20Sopenharmony_ci			return -ENOMEM;
878c2ecf20Sopenharmony_ci		gspca_dev->urb[n] = urb;
888c2ecf20Sopenharmony_ci		urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
898c2ecf20Sopenharmony_ci						SD_PKT_SZ * SD_NPKT,
908c2ecf20Sopenharmony_ci						GFP_KERNEL,
918c2ecf20Sopenharmony_ci						&urb->transfer_dma);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		if (urb->transfer_buffer == NULL) {
948c2ecf20Sopenharmony_ci			pr_err("usb_alloc_coherent failed\n");
958c2ecf20Sopenharmony_ci			return -ENOMEM;
968c2ecf20Sopenharmony_ci		}
978c2ecf20Sopenharmony_ci		urb->dev = gspca_dev->dev;
988c2ecf20Sopenharmony_ci		urb->context = gspca_dev;
998c2ecf20Sopenharmony_ci		urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
1008c2ecf20Sopenharmony_ci		urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
1018c2ecf20Sopenharmony_ci					n & 1 ? 0x82 : 0x83);
1028c2ecf20Sopenharmony_ci		urb->transfer_flags = URB_ISO_ASAP
1038c2ecf20Sopenharmony_ci					| URB_NO_TRANSFER_DMA_MAP;
1048c2ecf20Sopenharmony_ci		urb->interval = 1;
1058c2ecf20Sopenharmony_ci		urb->complete = sd_isoc_irq;
1068c2ecf20Sopenharmony_ci		urb->number_of_packets = SD_NPKT;
1078c2ecf20Sopenharmony_ci		for (i = 0; i < SD_NPKT; i++) {
1088c2ecf20Sopenharmony_ci			urb->iso_frame_desc[i].length = SD_PKT_SZ;
1098c2ecf20Sopenharmony_ci			urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
1108c2ecf20Sopenharmony_ci		}
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return gspca_dev->usb_err;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void sd_stopN(struct gspca_dev *gspca_dev)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct usb_interface *intf;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x003c, 0x0003);
1218c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x003c, 0x0004);
1228c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x003c, 0x0005);
1238c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x003c, 0x0006);
1248c2ecf20Sopenharmony_ci	reg_w(gspca_dev, 0x003c, 0x0007);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
1278c2ecf20Sopenharmony_ci	usb_set_interface(gspca_dev->dev, gspca_dev->iface,
1288c2ecf20Sopenharmony_ci					intf->num_altsetting - 1);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void sd_pkt_scan(struct gspca_dev *gspca_dev,
1328c2ecf20Sopenharmony_ci			u8 *data,		/* isoc packet */
1338c2ecf20Sopenharmony_ci			int len)		/* iso packet length */
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	/* unused */
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/* reception of an URB */
1398c2ecf20Sopenharmony_cistatic void sd_isoc_irq(struct urb *urb)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
1428c2ecf20Sopenharmony_ci	struct urb *urb0;
1438c2ecf20Sopenharmony_ci	u8 *data;
1448c2ecf20Sopenharmony_ci	int i, st;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_PACK, "sd isoc irq\n");
1478c2ecf20Sopenharmony_ci	if (!gspca_dev->streaming)
1488c2ecf20Sopenharmony_ci		return;
1498c2ecf20Sopenharmony_ci	if (urb->status != 0) {
1508c2ecf20Sopenharmony_ci		if (urb->status == -ESHUTDOWN)
1518c2ecf20Sopenharmony_ci			return;		/* disconnection */
1528c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
1538c2ecf20Sopenharmony_ci		if (gspca_dev->frozen)
1548c2ecf20Sopenharmony_ci			return;
1558c2ecf20Sopenharmony_ci#endif
1568c2ecf20Sopenharmony_ci		pr_err("urb status: %d\n", urb->status);
1578c2ecf20Sopenharmony_ci		return;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* if this is a control URN (ep 0x83), wait */
1618c2ecf20Sopenharmony_ci	if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
1628c2ecf20Sopenharmony_ci		return;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* scan both received URBs */
1658c2ecf20Sopenharmony_ci	if (urb == gspca_dev->urb[1])
1668c2ecf20Sopenharmony_ci		urb0 = gspca_dev->urb[0];
1678c2ecf20Sopenharmony_ci	else
1688c2ecf20Sopenharmony_ci		urb0 = gspca_dev->urb[2];
1698c2ecf20Sopenharmony_ci	for (i = 0; i < urb->number_of_packets; i++) {
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		/* check the packet status and length */
1728c2ecf20Sopenharmony_ci		if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
1738c2ecf20Sopenharmony_ci		    || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
1748c2ecf20Sopenharmony_ci			gspca_err(gspca_dev, "ISOC bad lengths %d / %d\n",
1758c2ecf20Sopenharmony_ci				  urb0->iso_frame_desc[i].actual_length,
1768c2ecf20Sopenharmony_ci				  urb->iso_frame_desc[i].actual_length);
1778c2ecf20Sopenharmony_ci			gspca_dev->last_packet_type = DISCARD_PACKET;
1788c2ecf20Sopenharmony_ci			continue;
1798c2ecf20Sopenharmony_ci		}
1808c2ecf20Sopenharmony_ci		st = urb0->iso_frame_desc[i].status;
1818c2ecf20Sopenharmony_ci		if (st == 0)
1828c2ecf20Sopenharmony_ci			st = urb->iso_frame_desc[i].status;
1838c2ecf20Sopenharmony_ci		if (st) {
1848c2ecf20Sopenharmony_ci			pr_err("ISOC data error: [%d] status=%d\n",
1858c2ecf20Sopenharmony_ci				i, st);
1868c2ecf20Sopenharmony_ci			gspca_dev->last_packet_type = DISCARD_PACKET;
1878c2ecf20Sopenharmony_ci			continue;
1888c2ecf20Sopenharmony_ci		}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		/*
1918c2ecf20Sopenharmony_ci		 * The images are received in URBs of different endpoints
1928c2ecf20Sopenharmony_ci		 * (0x83 and 0x82).
1938c2ecf20Sopenharmony_ci		 * Image pieces in URBs of ep 0x83 are continuated in URBs of
1948c2ecf20Sopenharmony_ci		 * ep 0x82 of the same index.
1958c2ecf20Sopenharmony_ci		 * The packets in the URBs of endpoint 0x83 start with:
1968c2ecf20Sopenharmony_ci		 *	- 80 ba/bb 00 00 = start of image followed by 'ff d8'
1978c2ecf20Sopenharmony_ci		 *	- 04 ba/bb oo oo = image piece
1988c2ecf20Sopenharmony_ci		 *		where 'oo oo' is the image offset
1998c2ecf20Sopenharmony_ci						(not checked)
2008c2ecf20Sopenharmony_ci		 *	- (other -> bad frame)
2018c2ecf20Sopenharmony_ci		 * The images are JPEG encoded with full header and
2028c2ecf20Sopenharmony_ci		 * normal ff escape.
2038c2ecf20Sopenharmony_ci		 * The end of image ('ff d9') may occur in any URB.
2048c2ecf20Sopenharmony_ci		 * (not checked)
2058c2ecf20Sopenharmony_ci		 */
2068c2ecf20Sopenharmony_ci		data = (u8 *) urb0->transfer_buffer
2078c2ecf20Sopenharmony_ci					+ urb0->iso_frame_desc[i].offset;
2088c2ecf20Sopenharmony_ci		if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci			/* new image */
2118c2ecf20Sopenharmony_ci			gspca_frame_add(gspca_dev, LAST_PACKET,
2128c2ecf20Sopenharmony_ci					NULL, 0);
2138c2ecf20Sopenharmony_ci			gspca_frame_add(gspca_dev, FIRST_PACKET,
2148c2ecf20Sopenharmony_ci					data + 4, SD_PKT_SZ - 4);
2158c2ecf20Sopenharmony_ci		} else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
2168c2ecf20Sopenharmony_ci			gspca_frame_add(gspca_dev, INTER_PACKET,
2178c2ecf20Sopenharmony_ci					data + 4, SD_PKT_SZ - 4);
2188c2ecf20Sopenharmony_ci		} else {
2198c2ecf20Sopenharmony_ci			gspca_dev->last_packet_type = DISCARD_PACKET;
2208c2ecf20Sopenharmony_ci			continue;
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci		data = (u8 *) urb->transfer_buffer
2238c2ecf20Sopenharmony_ci					+ urb->iso_frame_desc[i].offset;
2248c2ecf20Sopenharmony_ci		gspca_frame_add(gspca_dev, INTER_PACKET,
2258c2ecf20Sopenharmony_ci				data, SD_PKT_SZ);
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* resubmit the URBs */
2298c2ecf20Sopenharmony_ci	st = usb_submit_urb(urb0, GFP_ATOMIC);
2308c2ecf20Sopenharmony_ci	if (st < 0)
2318c2ecf20Sopenharmony_ci		pr_err("usb_submit_urb(0) ret %d\n", st);
2328c2ecf20Sopenharmony_ci	st = usb_submit_urb(urb, GFP_ATOMIC);
2338c2ecf20Sopenharmony_ci	if (st < 0)
2348c2ecf20Sopenharmony_ci		pr_err("usb_submit_urb() ret %d\n", st);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/* sub-driver description */
2388c2ecf20Sopenharmony_cistatic const struct sd_desc sd_desc = {
2398c2ecf20Sopenharmony_ci	.name = MODULE_NAME,
2408c2ecf20Sopenharmony_ci	.config = sd_config,
2418c2ecf20Sopenharmony_ci	.init = sd_init,
2428c2ecf20Sopenharmony_ci	.start = sd_start,
2438c2ecf20Sopenharmony_ci	.stopN = sd_stopN,
2448c2ecf20Sopenharmony_ci	.pkt_scan = sd_pkt_scan,
2458c2ecf20Sopenharmony_ci};
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* -- module initialisation -- */
2488c2ecf20Sopenharmony_cistatic const struct usb_device_id device_table[] = {
2498c2ecf20Sopenharmony_ci	{USB_DEVICE(0x04a5, 0x3035)},
2508c2ecf20Sopenharmony_ci	{}
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, device_table);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/* -- device connect -- */
2558c2ecf20Sopenharmony_cistatic int sd_probe(struct usb_interface *intf,
2568c2ecf20Sopenharmony_ci			const struct usb_device_id *id)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2598c2ecf20Sopenharmony_ci				THIS_MODULE);
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic struct usb_driver sd_driver = {
2638c2ecf20Sopenharmony_ci	.name = MODULE_NAME,
2648c2ecf20Sopenharmony_ci	.id_table = device_table,
2658c2ecf20Sopenharmony_ci	.probe = sd_probe,
2668c2ecf20Sopenharmony_ci	.disconnect = gspca_disconnect,
2678c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
2688c2ecf20Sopenharmony_ci	.suspend = gspca_suspend,
2698c2ecf20Sopenharmony_ci	.resume = gspca_resume,
2708c2ecf20Sopenharmony_ci	.reset_resume = gspca_resume,
2718c2ecf20Sopenharmony_ci#endif
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cimodule_usb_driver(sd_driver);
275