1*fd9871f7SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab * Benq DC E300 subdriver
40c0d06caSMauro Carvalho Chehab *
50c0d06caSMauro Carvalho Chehab * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
60c0d06caSMauro Carvalho Chehab */
70c0d06caSMauro Carvalho Chehab
80c0d06caSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
90c0d06caSMauro Carvalho Chehab
100c0d06caSMauro Carvalho Chehab #define MODULE_NAME "benq"
110c0d06caSMauro Carvalho Chehab
120c0d06caSMauro Carvalho Chehab #include "gspca.h"
130c0d06caSMauro Carvalho Chehab
140c0d06caSMauro Carvalho Chehab MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
150c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
160c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
170c0d06caSMauro Carvalho Chehab
180c0d06caSMauro Carvalho Chehab /* specific webcam descriptor */
190c0d06caSMauro Carvalho Chehab struct sd {
200c0d06caSMauro Carvalho Chehab struct gspca_dev gspca_dev; /* !! must be the first item */
210c0d06caSMauro Carvalho Chehab };
220c0d06caSMauro Carvalho Chehab
230c0d06caSMauro Carvalho Chehab static const struct v4l2_pix_format vga_mode[] = {
240c0d06caSMauro Carvalho Chehab {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
250c0d06caSMauro Carvalho Chehab .bytesperline = 320,
260c0d06caSMauro Carvalho Chehab .sizeimage = 320 * 240 * 3 / 8 + 590,
270c0d06caSMauro Carvalho Chehab .colorspace = V4L2_COLORSPACE_JPEG},
280c0d06caSMauro Carvalho Chehab };
290c0d06caSMauro Carvalho Chehab
300c0d06caSMauro Carvalho Chehab static void sd_isoc_irq(struct urb *urb);
310c0d06caSMauro Carvalho Chehab
320c0d06caSMauro Carvalho Chehab /* -- write a register -- */
reg_w(struct gspca_dev * gspca_dev,u16 value,u16 index)330c0d06caSMauro Carvalho Chehab static void reg_w(struct gspca_dev *gspca_dev,
340c0d06caSMauro Carvalho Chehab u16 value, u16 index)
350c0d06caSMauro Carvalho Chehab {
360c0d06caSMauro Carvalho Chehab struct usb_device *dev = gspca_dev->dev;
370c0d06caSMauro Carvalho Chehab int ret;
380c0d06caSMauro Carvalho Chehab
390c0d06caSMauro Carvalho Chehab if (gspca_dev->usb_err < 0)
400c0d06caSMauro Carvalho Chehab return;
410c0d06caSMauro Carvalho Chehab ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
420c0d06caSMauro Carvalho Chehab 0x02,
430c0d06caSMauro Carvalho Chehab USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
440c0d06caSMauro Carvalho Chehab value,
450c0d06caSMauro Carvalho Chehab index,
460c0d06caSMauro Carvalho Chehab NULL,
470c0d06caSMauro Carvalho Chehab 0,
480c0d06caSMauro Carvalho Chehab 500);
490c0d06caSMauro Carvalho Chehab if (ret < 0) {
500c0d06caSMauro Carvalho Chehab pr_err("reg_w err %d\n", ret);
510c0d06caSMauro Carvalho Chehab gspca_dev->usb_err = ret;
520c0d06caSMauro Carvalho Chehab }
530c0d06caSMauro Carvalho Chehab }
540c0d06caSMauro Carvalho Chehab
550c0d06caSMauro Carvalho Chehab /* this function is called at probe time */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)560c0d06caSMauro Carvalho Chehab static int sd_config(struct gspca_dev *gspca_dev,
570c0d06caSMauro Carvalho Chehab const struct usb_device_id *id)
580c0d06caSMauro Carvalho Chehab {
590c0d06caSMauro Carvalho Chehab gspca_dev->cam.cam_mode = vga_mode;
600c0d06caSMauro Carvalho Chehab gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
610c0d06caSMauro Carvalho Chehab gspca_dev->cam.no_urb_create = 1;
620c0d06caSMauro Carvalho Chehab return 0;
630c0d06caSMauro Carvalho Chehab }
640c0d06caSMauro Carvalho Chehab
650c0d06caSMauro Carvalho Chehab /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)660c0d06caSMauro Carvalho Chehab static int sd_init(struct gspca_dev *gspca_dev)
670c0d06caSMauro Carvalho Chehab {
680c0d06caSMauro Carvalho Chehab return 0;
690c0d06caSMauro Carvalho Chehab }
700c0d06caSMauro Carvalho Chehab
710c0d06caSMauro Carvalho Chehab /* -- start the camera -- */
sd_start(struct gspca_dev * gspca_dev)720c0d06caSMauro Carvalho Chehab static int sd_start(struct gspca_dev *gspca_dev)
730c0d06caSMauro Carvalho Chehab {
740c0d06caSMauro Carvalho Chehab struct urb *urb;
750c0d06caSMauro Carvalho Chehab int i, n;
760c0d06caSMauro Carvalho Chehab
770c0d06caSMauro Carvalho Chehab /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
780c0d06caSMauro Carvalho Chehab #if MAX_NURBS < 4
790c0d06caSMauro Carvalho Chehab #error "Not enough URBs in the gspca table"
800c0d06caSMauro Carvalho Chehab #endif
810c0d06caSMauro Carvalho Chehab #define SD_PKT_SZ 64
820c0d06caSMauro Carvalho Chehab #define SD_NPKT 32
830c0d06caSMauro Carvalho Chehab for (n = 0; n < 4; n++) {
840c0d06caSMauro Carvalho Chehab urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
8560e36999SWolfram Sang if (!urb)
860c0d06caSMauro Carvalho Chehab return -ENOMEM;
870c0d06caSMauro Carvalho Chehab gspca_dev->urb[n] = urb;
880c0d06caSMauro Carvalho Chehab urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
890c0d06caSMauro Carvalho Chehab SD_PKT_SZ * SD_NPKT,
900c0d06caSMauro Carvalho Chehab GFP_KERNEL,
910c0d06caSMauro Carvalho Chehab &urb->transfer_dma);
920c0d06caSMauro Carvalho Chehab
930c0d06caSMauro Carvalho Chehab if (urb->transfer_buffer == NULL) {
940c0d06caSMauro Carvalho Chehab pr_err("usb_alloc_coherent failed\n");
950c0d06caSMauro Carvalho Chehab return -ENOMEM;
960c0d06caSMauro Carvalho Chehab }
970c0d06caSMauro Carvalho Chehab urb->dev = gspca_dev->dev;
980c0d06caSMauro Carvalho Chehab urb->context = gspca_dev;
990c0d06caSMauro Carvalho Chehab urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
1000c0d06caSMauro Carvalho Chehab urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
1010c0d06caSMauro Carvalho Chehab n & 1 ? 0x82 : 0x83);
1020c0d06caSMauro Carvalho Chehab urb->transfer_flags = URB_ISO_ASAP
1030c0d06caSMauro Carvalho Chehab | URB_NO_TRANSFER_DMA_MAP;
1040c0d06caSMauro Carvalho Chehab urb->interval = 1;
1050c0d06caSMauro Carvalho Chehab urb->complete = sd_isoc_irq;
1060c0d06caSMauro Carvalho Chehab urb->number_of_packets = SD_NPKT;
1070c0d06caSMauro Carvalho Chehab for (i = 0; i < SD_NPKT; i++) {
1080c0d06caSMauro Carvalho Chehab urb->iso_frame_desc[i].length = SD_PKT_SZ;
1090c0d06caSMauro Carvalho Chehab urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
1100c0d06caSMauro Carvalho Chehab }
1110c0d06caSMauro Carvalho Chehab }
1120c0d06caSMauro Carvalho Chehab
1130c0d06caSMauro Carvalho Chehab return gspca_dev->usb_err;
1140c0d06caSMauro Carvalho Chehab }
1150c0d06caSMauro Carvalho Chehab
sd_stopN(struct gspca_dev * gspca_dev)1160c0d06caSMauro Carvalho Chehab static void sd_stopN(struct gspca_dev *gspca_dev)
1170c0d06caSMauro Carvalho Chehab {
1180c0d06caSMauro Carvalho Chehab struct usb_interface *intf;
1190c0d06caSMauro Carvalho Chehab
1200c0d06caSMauro Carvalho Chehab reg_w(gspca_dev, 0x003c, 0x0003);
1210c0d06caSMauro Carvalho Chehab reg_w(gspca_dev, 0x003c, 0x0004);
1220c0d06caSMauro Carvalho Chehab reg_w(gspca_dev, 0x003c, 0x0005);
1230c0d06caSMauro Carvalho Chehab reg_w(gspca_dev, 0x003c, 0x0006);
1240c0d06caSMauro Carvalho Chehab reg_w(gspca_dev, 0x003c, 0x0007);
1250c0d06caSMauro Carvalho Chehab
1260c0d06caSMauro Carvalho Chehab intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
1270c0d06caSMauro Carvalho Chehab usb_set_interface(gspca_dev->dev, gspca_dev->iface,
1280c0d06caSMauro Carvalho Chehab intf->num_altsetting - 1);
1290c0d06caSMauro Carvalho Chehab }
1300c0d06caSMauro Carvalho Chehab
sd_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)1310c0d06caSMauro Carvalho Chehab static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1320c0d06caSMauro Carvalho Chehab u8 *data, /* isoc packet */
1330c0d06caSMauro Carvalho Chehab int len) /* iso packet length */
1340c0d06caSMauro Carvalho Chehab {
1350c0d06caSMauro Carvalho Chehab /* unused */
1360c0d06caSMauro Carvalho Chehab }
1370c0d06caSMauro Carvalho Chehab
1380c0d06caSMauro Carvalho Chehab /* reception of an URB */
sd_isoc_irq(struct urb * urb)1390c0d06caSMauro Carvalho Chehab static void sd_isoc_irq(struct urb *urb)
1400c0d06caSMauro Carvalho Chehab {
1410c0d06caSMauro Carvalho Chehab struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
1420c0d06caSMauro Carvalho Chehab struct urb *urb0;
1430c0d06caSMauro Carvalho Chehab u8 *data;
1440c0d06caSMauro Carvalho Chehab int i, st;
1450c0d06caSMauro Carvalho Chehab
14637d5efb0SJoe Perches gspca_dbg(gspca_dev, D_PACK, "sd isoc irq\n");
1470c0d06caSMauro Carvalho Chehab if (!gspca_dev->streaming)
1480c0d06caSMauro Carvalho Chehab return;
1490c0d06caSMauro Carvalho Chehab if (urb->status != 0) {
1500c0d06caSMauro Carvalho Chehab if (urb->status == -ESHUTDOWN)
1510c0d06caSMauro Carvalho Chehab return; /* disconnection */
1520c0d06caSMauro Carvalho Chehab #ifdef CONFIG_PM
1530c0d06caSMauro Carvalho Chehab if (gspca_dev->frozen)
1540c0d06caSMauro Carvalho Chehab return;
1550c0d06caSMauro Carvalho Chehab #endif
1560c0d06caSMauro Carvalho Chehab pr_err("urb status: %d\n", urb->status);
1570c0d06caSMauro Carvalho Chehab return;
1580c0d06caSMauro Carvalho Chehab }
1590c0d06caSMauro Carvalho Chehab
1600c0d06caSMauro Carvalho Chehab /* if this is a control URN (ep 0x83), wait */
1610c0d06caSMauro Carvalho Chehab if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
1620c0d06caSMauro Carvalho Chehab return;
1630c0d06caSMauro Carvalho Chehab
1640c0d06caSMauro Carvalho Chehab /* scan both received URBs */
1650c0d06caSMauro Carvalho Chehab if (urb == gspca_dev->urb[1])
1660c0d06caSMauro Carvalho Chehab urb0 = gspca_dev->urb[0];
1670c0d06caSMauro Carvalho Chehab else
1680c0d06caSMauro Carvalho Chehab urb0 = gspca_dev->urb[2];
1690c0d06caSMauro Carvalho Chehab for (i = 0; i < urb->number_of_packets; i++) {
1700c0d06caSMauro Carvalho Chehab
1710c0d06caSMauro Carvalho Chehab /* check the packet status and length */
1720c0d06caSMauro Carvalho Chehab if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
1730c0d06caSMauro Carvalho Chehab || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
17452173c5fSJoe Perches gspca_err(gspca_dev, "ISOC bad lengths %d / %d\n",
1750c0d06caSMauro Carvalho Chehab urb0->iso_frame_desc[i].actual_length,
1760c0d06caSMauro Carvalho Chehab urb->iso_frame_desc[i].actual_length);
1770c0d06caSMauro Carvalho Chehab gspca_dev->last_packet_type = DISCARD_PACKET;
1780c0d06caSMauro Carvalho Chehab continue;
1790c0d06caSMauro Carvalho Chehab }
1800c0d06caSMauro Carvalho Chehab st = urb0->iso_frame_desc[i].status;
1810c0d06caSMauro Carvalho Chehab if (st == 0)
1820c0d06caSMauro Carvalho Chehab st = urb->iso_frame_desc[i].status;
1830c0d06caSMauro Carvalho Chehab if (st) {
1840c0d06caSMauro Carvalho Chehab pr_err("ISOC data error: [%d] status=%d\n",
1850c0d06caSMauro Carvalho Chehab i, st);
1860c0d06caSMauro Carvalho Chehab gspca_dev->last_packet_type = DISCARD_PACKET;
1870c0d06caSMauro Carvalho Chehab continue;
1880c0d06caSMauro Carvalho Chehab }
1890c0d06caSMauro Carvalho Chehab
1900c0d06caSMauro Carvalho Chehab /*
1910c0d06caSMauro Carvalho Chehab * The images are received in URBs of different endpoints
1920c0d06caSMauro Carvalho Chehab * (0x83 and 0x82).
1930c0d06caSMauro Carvalho Chehab * Image pieces in URBs of ep 0x83 are continuated in URBs of
1940c0d06caSMauro Carvalho Chehab * ep 0x82 of the same index.
1950c0d06caSMauro Carvalho Chehab * The packets in the URBs of endpoint 0x83 start with:
1960c0d06caSMauro Carvalho Chehab * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
1970c0d06caSMauro Carvalho Chehab * - 04 ba/bb oo oo = image piece
1980c0d06caSMauro Carvalho Chehab * where 'oo oo' is the image offset
1993e4d8f48SMauro Carvalho Chehab (not checked)
2000c0d06caSMauro Carvalho Chehab * - (other -> bad frame)
2010c0d06caSMauro Carvalho Chehab * The images are JPEG encoded with full header and
2020c0d06caSMauro Carvalho Chehab * normal ff escape.
2030c0d06caSMauro Carvalho Chehab * The end of image ('ff d9') may occur in any URB.
2043e4d8f48SMauro Carvalho Chehab * (not checked)
2050c0d06caSMauro Carvalho Chehab */
2060c0d06caSMauro Carvalho Chehab data = (u8 *) urb0->transfer_buffer
2070c0d06caSMauro Carvalho Chehab + urb0->iso_frame_desc[i].offset;
2080c0d06caSMauro Carvalho Chehab if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
2090c0d06caSMauro Carvalho Chehab
2100c0d06caSMauro Carvalho Chehab /* new image */
2110c0d06caSMauro Carvalho Chehab gspca_frame_add(gspca_dev, LAST_PACKET,
2120c0d06caSMauro Carvalho Chehab NULL, 0);
2130c0d06caSMauro Carvalho Chehab gspca_frame_add(gspca_dev, FIRST_PACKET,
2140c0d06caSMauro Carvalho Chehab data + 4, SD_PKT_SZ - 4);
2150c0d06caSMauro Carvalho Chehab } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
2160c0d06caSMauro Carvalho Chehab gspca_frame_add(gspca_dev, INTER_PACKET,
2170c0d06caSMauro Carvalho Chehab data + 4, SD_PKT_SZ - 4);
2180c0d06caSMauro Carvalho Chehab } else {
2190c0d06caSMauro Carvalho Chehab gspca_dev->last_packet_type = DISCARD_PACKET;
2200c0d06caSMauro Carvalho Chehab continue;
2210c0d06caSMauro Carvalho Chehab }
2220c0d06caSMauro Carvalho Chehab data = (u8 *) urb->transfer_buffer
2230c0d06caSMauro Carvalho Chehab + urb->iso_frame_desc[i].offset;
2240c0d06caSMauro Carvalho Chehab gspca_frame_add(gspca_dev, INTER_PACKET,
2250c0d06caSMauro Carvalho Chehab data, SD_PKT_SZ);
2260c0d06caSMauro Carvalho Chehab }
2270c0d06caSMauro Carvalho Chehab
2280c0d06caSMauro Carvalho Chehab /* resubmit the URBs */
2290c0d06caSMauro Carvalho Chehab st = usb_submit_urb(urb0, GFP_ATOMIC);
2300c0d06caSMauro Carvalho Chehab if (st < 0)
2310c0d06caSMauro Carvalho Chehab pr_err("usb_submit_urb(0) ret %d\n", st);
2320c0d06caSMauro Carvalho Chehab st = usb_submit_urb(urb, GFP_ATOMIC);
2330c0d06caSMauro Carvalho Chehab if (st < 0)
2340c0d06caSMauro Carvalho Chehab pr_err("usb_submit_urb() ret %d\n", st);
2350c0d06caSMauro Carvalho Chehab }
2360c0d06caSMauro Carvalho Chehab
2370c0d06caSMauro Carvalho Chehab /* sub-driver description */
2380c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc = {
2390c0d06caSMauro Carvalho Chehab .name = MODULE_NAME,
2400c0d06caSMauro Carvalho Chehab .config = sd_config,
2410c0d06caSMauro Carvalho Chehab .init = sd_init,
2420c0d06caSMauro Carvalho Chehab .start = sd_start,
2430c0d06caSMauro Carvalho Chehab .stopN = sd_stopN,
2440c0d06caSMauro Carvalho Chehab .pkt_scan = sd_pkt_scan,
2450c0d06caSMauro Carvalho Chehab };
2460c0d06caSMauro Carvalho Chehab
2470c0d06caSMauro Carvalho Chehab /* -- module initialisation -- */
2480c0d06caSMauro Carvalho Chehab static const struct usb_device_id device_table[] = {
2490c0d06caSMauro Carvalho Chehab {USB_DEVICE(0x04a5, 0x3035)},
2500c0d06caSMauro Carvalho Chehab {}
2510c0d06caSMauro Carvalho Chehab };
2520c0d06caSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, device_table);
2530c0d06caSMauro Carvalho Chehab
2540c0d06caSMauro Carvalho Chehab /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)2550c0d06caSMauro Carvalho Chehab static int sd_probe(struct usb_interface *intf,
2560c0d06caSMauro Carvalho Chehab const struct usb_device_id *id)
2570c0d06caSMauro Carvalho Chehab {
2580c0d06caSMauro Carvalho Chehab return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2590c0d06caSMauro Carvalho Chehab THIS_MODULE);
2600c0d06caSMauro Carvalho Chehab }
2610c0d06caSMauro Carvalho Chehab
2620c0d06caSMauro Carvalho Chehab static struct usb_driver sd_driver = {
2630c0d06caSMauro Carvalho Chehab .name = MODULE_NAME,
2640c0d06caSMauro Carvalho Chehab .id_table = device_table,
2650c0d06caSMauro Carvalho Chehab .probe = sd_probe,
2660c0d06caSMauro Carvalho Chehab .disconnect = gspca_disconnect,
2670c0d06caSMauro Carvalho Chehab #ifdef CONFIG_PM
2680c0d06caSMauro Carvalho Chehab .suspend = gspca_suspend,
2690c0d06caSMauro Carvalho Chehab .resume = gspca_resume,
2700c0d06caSMauro Carvalho Chehab .reset_resume = gspca_resume,
2710c0d06caSMauro Carvalho Chehab #endif
2720c0d06caSMauro Carvalho Chehab };
2730c0d06caSMauro Carvalho Chehab
2740c0d06caSMauro Carvalho Chehab module_usb_driver(sd_driver);
275