Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for USB webcams based on Konica chipset. This
0004  * chipset is used in Intel YC76 camera.
0005  *
0006  * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
0007  *
0008  * Based on the usbvideo v4l1 konicawc driver which is:
0009  *
0010  * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
0011  *
0012  * The code for making gspca work with a webcam with 2 isoc endpoints was
0013  * taken from the benq gspca subdriver which is:
0014  *
0015  * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #define MODULE_NAME "konica"
0021 
0022 #include <linux/input.h>
0023 #include "gspca.h"
0024 
0025 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0026 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
0027 MODULE_LICENSE("GPL");
0028 
0029 #define WHITEBAL_REG   0x01
0030 #define BRIGHTNESS_REG 0x02
0031 #define SHARPNESS_REG  0x03
0032 #define CONTRAST_REG   0x04
0033 #define SATURATION_REG 0x05
0034 
0035 /* specific webcam descriptor */
0036 struct sd {
0037     struct gspca_dev gspca_dev; /* !! must be the first item */
0038     struct urb *last_data_urb;
0039     u8 snapshot_pressed;
0040 };
0041 
0042 
0043 /* .priv is what goes to register 8 for this mode, known working values:
0044    0x00 -> 176x144, cropped
0045    0x01 -> 176x144, cropped
0046    0x02 -> 176x144, cropped
0047    0x03 -> 176x144, cropped
0048    0x04 -> 176x144, binned
0049    0x05 -> 320x240
0050    0x06 -> 320x240
0051    0x07 -> 160x120, cropped
0052    0x08 -> 160x120, cropped
0053    0x09 -> 160x120, binned (note has 136 lines)
0054    0x0a -> 160x120, binned (note has 136 lines)
0055    0x0b -> 160x120, cropped
0056 */
0057 static const struct v4l2_pix_format vga_mode[] = {
0058     {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
0059         .bytesperline = 160,
0060         .sizeimage = 160 * 136 * 3 / 2 + 960,
0061         .colorspace = V4L2_COLORSPACE_SRGB,
0062         .priv = 0x0a},
0063     {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
0064         .bytesperline = 176,
0065         .sizeimage = 176 * 144 * 3 / 2 + 960,
0066         .colorspace = V4L2_COLORSPACE_SRGB,
0067         .priv = 0x04},
0068     {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
0069         .bytesperline = 320,
0070         .sizeimage = 320 * 240 * 3 / 2 + 960,
0071         .colorspace = V4L2_COLORSPACE_SRGB,
0072         .priv = 0x05},
0073 };
0074 
0075 static void sd_isoc_irq(struct urb *urb);
0076 
0077 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
0078 {
0079     struct usb_device *dev = gspca_dev->dev;
0080     int ret;
0081 
0082     if (gspca_dev->usb_err < 0)
0083         return;
0084     ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
0085             0x02,
0086             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0087             value,
0088             index,
0089             NULL,
0090             0,
0091             1000);
0092     if (ret < 0) {
0093         pr_err("reg_w err writing %02x to %02x: %d\n",
0094                value, index, ret);
0095         gspca_dev->usb_err = ret;
0096     }
0097 }
0098 
0099 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
0100 {
0101     struct usb_device *dev = gspca_dev->dev;
0102     int ret;
0103 
0104     if (gspca_dev->usb_err < 0)
0105         return;
0106     ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
0107             0x03,
0108             USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0109             value,
0110             index,
0111             gspca_dev->usb_buf,
0112             2,
0113             1000);
0114     if (ret < 0) {
0115         pr_err("reg_r err %d\n", ret);
0116         gspca_dev->usb_err = ret;
0117         /*
0118          * Make sure the buffer is zeroed to avoid uninitialized
0119          * values.
0120          */
0121         memset(gspca_dev->usb_buf, 0, 2);
0122     }
0123 }
0124 
0125 static void konica_stream_on(struct gspca_dev *gspca_dev)
0126 {
0127     reg_w(gspca_dev, 1, 0x0b);
0128 }
0129 
0130 static void konica_stream_off(struct gspca_dev *gspca_dev)
0131 {
0132     reg_w(gspca_dev, 0, 0x0b);
0133 }
0134 
0135 /* this function is called at probe time */
0136 static int sd_config(struct gspca_dev *gspca_dev,
0137             const struct usb_device_id *id)
0138 {
0139     gspca_dev->cam.cam_mode = vga_mode;
0140     gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
0141     gspca_dev->cam.no_urb_create = 1;
0142 
0143     return 0;
0144 }
0145 
0146 /* this function is called at probe and resume time */
0147 static int sd_init(struct gspca_dev *gspca_dev)
0148 {
0149     int i;
0150 
0151     /*
0152      * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
0153      * and does not want to be bothered while doing so :|
0154      * Register 0x10 counts from 1 - 3, with 3 being "ready"
0155      */
0156     msleep(6000);
0157     for (i = 0; i < 20; i++) {
0158         reg_r(gspca_dev, 0, 0x10);
0159         if (gspca_dev->usb_buf[0] == 3)
0160             break;
0161         msleep(100);
0162     }
0163     reg_w(gspca_dev, 0, 0x0d);
0164 
0165     return gspca_dev->usb_err;
0166 }
0167 
0168 static int sd_start(struct gspca_dev *gspca_dev)
0169 {
0170     struct sd *sd = (struct sd *) gspca_dev;
0171     struct urb *urb;
0172     int i, n, packet_size;
0173     struct usb_host_interface *alt;
0174     struct usb_interface *intf;
0175 
0176     intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
0177     alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
0178     if (!alt) {
0179         pr_err("Couldn't get altsetting\n");
0180         return -EIO;
0181     }
0182 
0183     if (alt->desc.bNumEndpoints < 2)
0184         return -ENODEV;
0185 
0186     packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
0187 
0188     n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
0189     reg_w(gspca_dev, n, 0x08);
0190 
0191     konica_stream_on(gspca_dev);
0192 
0193     if (gspca_dev->usb_err)
0194         return gspca_dev->usb_err;
0195 
0196     /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
0197 #if MAX_NURBS < 4
0198 #error "Not enough URBs in the gspca table"
0199 #endif
0200 #define SD_NPKT 32
0201     for (n = 0; n < 4; n++) {
0202         i = n & 1 ? 0 : 1;
0203         packet_size =
0204             le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
0205         urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
0206         if (!urb)
0207             return -ENOMEM;
0208         gspca_dev->urb[n] = urb;
0209         urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
0210                         packet_size * SD_NPKT,
0211                         GFP_KERNEL,
0212                         &urb->transfer_dma);
0213         if (urb->transfer_buffer == NULL) {
0214             pr_err("usb_buffer_alloc failed\n");
0215             return -ENOMEM;
0216         }
0217 
0218         urb->dev = gspca_dev->dev;
0219         urb->context = gspca_dev;
0220         urb->transfer_buffer_length = packet_size * SD_NPKT;
0221         urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
0222                     n & 1 ? 0x81 : 0x82);
0223         urb->transfer_flags = URB_ISO_ASAP
0224                     | URB_NO_TRANSFER_DMA_MAP;
0225         urb->interval = 1;
0226         urb->complete = sd_isoc_irq;
0227         urb->number_of_packets = SD_NPKT;
0228         for (i = 0; i < SD_NPKT; i++) {
0229             urb->iso_frame_desc[i].length = packet_size;
0230             urb->iso_frame_desc[i].offset = packet_size * i;
0231         }
0232     }
0233 
0234     return 0;
0235 }
0236 
0237 static void sd_stopN(struct gspca_dev *gspca_dev)
0238 {
0239     struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
0240 
0241     konica_stream_off(gspca_dev);
0242 #if IS_ENABLED(CONFIG_INPUT)
0243     /* Don't keep the button in the pressed state "forever" if it was
0244        pressed when streaming is stopped */
0245     if (sd->snapshot_pressed) {
0246         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
0247         input_sync(gspca_dev->input_dev);
0248         sd->snapshot_pressed = 0;
0249     }
0250 #endif
0251 }
0252 
0253 /* reception of an URB */
0254 static void sd_isoc_irq(struct urb *urb)
0255 {
0256     struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
0257     struct sd *sd = (struct sd *) gspca_dev;
0258     struct urb *data_urb, *status_urb;
0259     u8 *data;
0260     int i, st;
0261 
0262     gspca_dbg(gspca_dev, D_PACK, "sd isoc irq\n");
0263     if (!gspca_dev->streaming)
0264         return;
0265 
0266     if (urb->status != 0) {
0267         if (urb->status == -ESHUTDOWN)
0268             return;     /* disconnection */
0269 #ifdef CONFIG_PM
0270         if (gspca_dev->frozen)
0271             return;
0272 #endif
0273         gspca_err(gspca_dev, "urb status: %d\n", urb->status);
0274         st = usb_submit_urb(urb, GFP_ATOMIC);
0275         if (st < 0)
0276             pr_err("resubmit urb error %d\n", st);
0277         return;
0278     }
0279 
0280     /* if this is a data URB (ep 0x82), wait */
0281     if (urb->transfer_buffer_length > 32) {
0282         sd->last_data_urb = urb;
0283         return;
0284     }
0285 
0286     status_urb = urb;
0287     data_urb   = sd->last_data_urb;
0288     sd->last_data_urb = NULL;
0289 
0290     if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
0291         gspca_err(gspca_dev, "lost sync on frames\n");
0292         goto resubmit;
0293     }
0294 
0295     if (data_urb->number_of_packets != status_urb->number_of_packets) {
0296         gspca_err(gspca_dev, "no packets does not match, data: %d, status: %d\n",
0297               data_urb->number_of_packets,
0298               status_urb->number_of_packets);
0299         goto resubmit;
0300     }
0301 
0302     for (i = 0; i < status_urb->number_of_packets; i++) {
0303         if (data_urb->iso_frame_desc[i].status ||
0304             status_urb->iso_frame_desc[i].status) {
0305             gspca_err(gspca_dev, "pkt %d data-status %d, status-status %d\n",
0306                   i,
0307                   data_urb->iso_frame_desc[i].status,
0308                   status_urb->iso_frame_desc[i].status);
0309             gspca_dev->last_packet_type = DISCARD_PACKET;
0310             continue;
0311         }
0312 
0313         if (status_urb->iso_frame_desc[i].actual_length != 1) {
0314             gspca_err(gspca_dev, "bad status packet length %d\n",
0315                   status_urb->iso_frame_desc[i].actual_length);
0316             gspca_dev->last_packet_type = DISCARD_PACKET;
0317             continue;
0318         }
0319 
0320         st = *((u8 *)status_urb->transfer_buffer
0321                 + status_urb->iso_frame_desc[i].offset);
0322 
0323         data = (u8 *)data_urb->transfer_buffer
0324                 + data_urb->iso_frame_desc[i].offset;
0325 
0326         /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
0327          * otherwise:
0328          * bit 0 0: keep packet
0329          *   1: drop packet (padding data)
0330          *
0331          * bit 4 0 button not clicked
0332          *       1 button clicked
0333          * button is used to `take a picture' (in software)
0334          */
0335         if (st & 0x80) {
0336             gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
0337             gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
0338         } else {
0339 #if IS_ENABLED(CONFIG_INPUT)
0340             u8 button_state = st & 0x40 ? 1 : 0;
0341             if (sd->snapshot_pressed != button_state) {
0342                 input_report_key(gspca_dev->input_dev,
0343                          KEY_CAMERA,
0344                          button_state);
0345                 input_sync(gspca_dev->input_dev);
0346                 sd->snapshot_pressed = button_state;
0347             }
0348 #endif
0349             if (st & 0x01)
0350                 continue;
0351         }
0352         gspca_frame_add(gspca_dev, INTER_PACKET, data,
0353                 data_urb->iso_frame_desc[i].actual_length);
0354     }
0355 
0356 resubmit:
0357     if (data_urb) {
0358         st = usb_submit_urb(data_urb, GFP_ATOMIC);
0359         if (st < 0)
0360             gspca_err(gspca_dev, "usb_submit_urb(data_urb) ret %d\n",
0361                   st);
0362     }
0363     st = usb_submit_urb(status_urb, GFP_ATOMIC);
0364     if (st < 0)
0365         gspca_err(gspca_dev, "usb_submit_urb(status_urb) ret %d\n", st);
0366 }
0367 
0368 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
0369 {
0370     struct gspca_dev *gspca_dev =
0371         container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
0372 
0373     gspca_dev->usb_err = 0;
0374 
0375     if (!gspca_dev->streaming)
0376         return 0;
0377 
0378     switch (ctrl->id) {
0379     case V4L2_CID_BRIGHTNESS:
0380         konica_stream_off(gspca_dev);
0381         reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
0382         konica_stream_on(gspca_dev);
0383         break;
0384     case V4L2_CID_CONTRAST:
0385         konica_stream_off(gspca_dev);
0386         reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
0387         konica_stream_on(gspca_dev);
0388         break;
0389     case V4L2_CID_SATURATION:
0390         konica_stream_off(gspca_dev);
0391         reg_w(gspca_dev, ctrl->val, SATURATION_REG);
0392         konica_stream_on(gspca_dev);
0393         break;
0394     case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
0395         konica_stream_off(gspca_dev);
0396         reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
0397         konica_stream_on(gspca_dev);
0398         break;
0399     case V4L2_CID_SHARPNESS:
0400         konica_stream_off(gspca_dev);
0401         reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
0402         konica_stream_on(gspca_dev);
0403         break;
0404     }
0405     return gspca_dev->usb_err;
0406 }
0407 
0408 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
0409     .s_ctrl = sd_s_ctrl,
0410 };
0411 
0412 static int sd_init_controls(struct gspca_dev *gspca_dev)
0413 {
0414     struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
0415 
0416     gspca_dev->vdev.ctrl_handler = hdl;
0417     v4l2_ctrl_handler_init(hdl, 5);
0418     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0419             V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
0420     /* Needs to be verified */
0421     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0422             V4L2_CID_CONTRAST, 0, 9, 1, 4);
0423     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0424             V4L2_CID_SATURATION, 0, 9, 1, 4);
0425     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0426             V4L2_CID_WHITE_BALANCE_TEMPERATURE,
0427             0, 33, 1, 25);
0428     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0429             V4L2_CID_SHARPNESS, 0, 9, 1, 4);
0430 
0431     if (hdl->error) {
0432         pr_err("Could not initialize controls\n");
0433         return hdl->error;
0434     }
0435     return 0;
0436 }
0437 
0438 /* sub-driver description */
0439 static const struct sd_desc sd_desc = {
0440     .name = MODULE_NAME,
0441     .config = sd_config,
0442     .init = sd_init,
0443     .init_controls = sd_init_controls,
0444     .start = sd_start,
0445     .stopN = sd_stopN,
0446 #if IS_ENABLED(CONFIG_INPUT)
0447     .other_input = 1,
0448 #endif
0449 };
0450 
0451 /* -- module initialisation -- */
0452 static const struct usb_device_id device_table[] = {
0453     {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
0454     {}
0455 };
0456 MODULE_DEVICE_TABLE(usb, device_table);
0457 
0458 /* -- device connect -- */
0459 static int sd_probe(struct usb_interface *intf,
0460             const struct usb_device_id *id)
0461 {
0462     return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
0463                 THIS_MODULE);
0464 }
0465 
0466 static struct usb_driver sd_driver = {
0467     .name = MODULE_NAME,
0468     .id_table = device_table,
0469     .probe = sd_probe,
0470     .disconnect = gspca_disconnect,
0471 #ifdef CONFIG_PM
0472     .suspend = gspca_suspend,
0473     .resume = gspca_resume,
0474     .reset_resume = gspca_resume,
0475 #endif
0476 };
0477 
0478 module_usb_driver(sd_driver);