linux/drivers/media/usb/gspca/vicam.c
<<
>>
Prefs
   1/*
   2 * gspca ViCam subdriver
   3 *
   4 * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
   5 *
   6 * Based on the usbvideo vicam driver, which is:
   7 *
   8 * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
   9 *                    Chris Cheney (chris.cheney@gmail.com),
  10 *                    Pavel Machek (pavel@ucw.cz),
  11 *                    John Tyner (jtyner@cs.ucr.edu),
  12 *                    Monroe Williams (monroe@pobox.com)
  13 *
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2 of the License, or
  17 * any later version.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22 * GNU General Public License for more details.
  23 */
  24
  25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26
  27#define MODULE_NAME "vicam"
  28#define HEADER_SIZE 64
  29
  30#include <linux/workqueue.h>
  31#include <linux/slab.h>
  32#include <linux/firmware.h>
  33#include <linux/ihex.h>
  34#include "gspca.h"
  35
  36#define VICAM_FIRMWARE "vicam/firmware.fw"
  37
  38MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  39MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
  40MODULE_LICENSE("GPL");
  41MODULE_FIRMWARE(VICAM_FIRMWARE);
  42
  43struct sd {
  44        struct gspca_dev gspca_dev;     /* !! must be the first item */
  45        struct work_struct work_struct;
  46};
  47
  48/* The vicam sensor has a resolution of 512 x 244, with I believe square
  49   pixels, but this is forced to a 4:3 ratio by optics. So it has
  50   non square pixels :( */
  51static struct v4l2_pix_format vicam_mode[] = {
  52        { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  53                .bytesperline = 256,
  54                .sizeimage = 256 * 122,
  55                .colorspace = V4L2_COLORSPACE_SRGB,},
  56        /* 2 modes with somewhat more square pixels */
  57        { 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  58                .bytesperline = 256,
  59                .sizeimage = 256 * 200,
  60                .colorspace = V4L2_COLORSPACE_SRGB,},
  61        { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  62                .bytesperline = 256,
  63                .sizeimage = 256 * 240,
  64                .colorspace = V4L2_COLORSPACE_SRGB,},
  65#if 0   /* This mode has extremely non square pixels, testing use only */
  66        { 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  67                .bytesperline = 512,
  68                .sizeimage = 512 * 122,
  69                .colorspace = V4L2_COLORSPACE_SRGB,},
  70#endif
  71        { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  72                .bytesperline = 512,
  73                .sizeimage = 512 * 244,
  74                .colorspace = V4L2_COLORSPACE_SRGB,},
  75};
  76
  77static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
  78        u16 value, u16 index, u8 *data, u16 len)
  79{
  80        int ret;
  81
  82        ret = usb_control_msg(gspca_dev->dev,
  83                              usb_sndctrlpipe(gspca_dev->dev, 0),
  84                              request,
  85                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  86                              value, index, data, len, 1000);
  87        if (ret < 0)
  88                pr_err("control msg req %02X error %d\n", request, ret);
  89
  90        return ret;
  91}
  92
  93static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
  94{
  95        int ret;
  96
  97        ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
  98        if (ret < 0)
  99                return ret;
 100
 101        if (state)
 102                ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
 103
 104        return ret;
 105}
 106
 107/*
 108 *  request and read a block of data
 109 */
 110static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
 111{
 112        int ret, unscaled_height, act_len = 0;
 113        u8 *req_data = gspca_dev->usb_buf;
 114        s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
 115        s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
 116
 117        memset(req_data, 0, 16);
 118        req_data[0] = gain;
 119        if (gspca_dev->pixfmt.width == 256)
 120                req_data[1] |= 0x01; /* low nibble x-scale */
 121        if (gspca_dev->pixfmt.height <= 122) {
 122                req_data[1] |= 0x10; /* high nibble y-scale */
 123                unscaled_height = gspca_dev->pixfmt.height * 2;
 124        } else
 125                unscaled_height = gspca_dev->pixfmt.height;
 126        req_data[2] = 0x90; /* unknown, does not seem to do anything */
 127        if (unscaled_height <= 200)
 128                req_data[3] = 0x06; /* vend? */
 129        else if (unscaled_height <= 242) /* Yes 242 not 240 */
 130                req_data[3] = 0x07; /* vend? */
 131        else /* Up to 244 lines with req_data[3] == 0x08 */
 132                req_data[3] = 0x08; /* vend? */
 133
 134        if (expo < 256) {
 135                /* Frame rate maxed out, use partial frame expo time */
 136                req_data[4] = 255 - expo;
 137                req_data[5] = 0x00;
 138                req_data[6] = 0x00;
 139                req_data[7] = 0x01;
 140        } else {
 141                /* Modify frame rate */
 142                req_data[4] = 0x00;
 143                req_data[5] = 0x00;
 144                req_data[6] = expo & 0xFF;
 145                req_data[7] = expo >> 8;
 146        }
 147        req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
 148        /* bytes 9-15 do not seem to affect exposure or image quality */
 149
 150        mutex_lock(&gspca_dev->usb_lock);
 151        ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
 152        mutex_unlock(&gspca_dev->usb_lock);
 153        if (ret < 0)
 154                return ret;
 155
 156        ret = usb_bulk_msg(gspca_dev->dev,
 157                           usb_rcvbulkpipe(gspca_dev->dev, 0x81),
 158                           data, size, &act_len, 10000);
 159        /* successful, it returns 0, otherwise  negative */
 160        if (ret < 0 || act_len != size) {
 161                pr_err("bulk read fail (%d) len %d/%d\n",
 162                       ret, act_len, size);
 163                return -EIO;
 164        }
 165        return 0;
 166}
 167
 168/*
 169 * This function is called as a workqueue function and runs whenever the camera
 170 * is streaming data. Because it is a workqueue function it is allowed to sleep
 171 * so we can use synchronous USB calls. To avoid possible collisions with other
 172 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
 173 * performing USB operations using it. In practice we don't really need this
 174 * as the cameras controls are only written from the workqueue.
 175 */
 176static void vicam_dostream(struct work_struct *work)
 177{
 178        struct sd *sd = container_of(work, struct sd, work_struct);
 179        struct gspca_dev *gspca_dev = &sd->gspca_dev;
 180        int ret, frame_sz;
 181        u8 *buffer;
 182
 183        frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
 184                   HEADER_SIZE;
 185        buffer = kmalloc(frame_sz, GFP_KERNEL | GFP_DMA);
 186        if (!buffer) {
 187                pr_err("Couldn't allocate USB buffer\n");
 188                goto exit;
 189        }
 190
 191        while (gspca_dev->present && gspca_dev->streaming) {
 192#ifdef CONFIG_PM
 193                if (gspca_dev->frozen)
 194                        break;
 195#endif
 196                ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
 197                if (ret < 0)
 198                        break;
 199
 200                /* Note the frame header contents seem to be completely
 201                   constant, they do not change with either image, or
 202                   settings. So we simply discard it. The frames have
 203                   a very similar 64 byte footer, which we don't even
 204                   bother reading from the cam */
 205                gspca_frame_add(gspca_dev, FIRST_PACKET,
 206                                buffer + HEADER_SIZE,
 207                                frame_sz - HEADER_SIZE);
 208                gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
 209        }
 210exit:
 211        kfree(buffer);
 212}
 213
 214/* This function is called at probe time just before sd_init */
 215static int sd_config(struct gspca_dev *gspca_dev,
 216                const struct usb_device_id *id)
 217{
 218        struct cam *cam = &gspca_dev->cam;
 219        struct sd *sd = (struct sd *)gspca_dev;
 220
 221        /* We don't use the buffer gspca allocates so make it small. */
 222        cam->bulk = 1;
 223        cam->bulk_size = 64;
 224        cam->cam_mode = vicam_mode;
 225        cam->nmodes = ARRAY_SIZE(vicam_mode);
 226
 227        INIT_WORK(&sd->work_struct, vicam_dostream);
 228
 229        return 0;
 230}
 231
 232/* this function is called at probe and resume time */
 233static int sd_init(struct gspca_dev *gspca_dev)
 234{
 235        int ret;
 236        const struct ihex_binrec *rec;
 237        const struct firmware *uninitialized_var(fw);
 238        u8 *firmware_buf;
 239
 240        ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
 241                                    &gspca_dev->dev->dev);
 242        if (ret) {
 243                pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
 244                return ret;
 245        }
 246
 247        firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 248        if (!firmware_buf) {
 249                ret = -ENOMEM;
 250                goto exit;
 251        }
 252        for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
 253                memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
 254                ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
 255                                        be16_to_cpu(rec->len));
 256                if (ret < 0)
 257                        break;
 258        }
 259
 260        kfree(firmware_buf);
 261exit:
 262        release_firmware(fw);
 263        return ret;
 264}
 265
 266/* Set up for getting frames. */
 267static int sd_start(struct gspca_dev *gspca_dev)
 268{
 269        struct sd *sd = (struct sd *)gspca_dev;
 270        int ret;
 271
 272        ret = vicam_set_camera_power(gspca_dev, 1);
 273        if (ret < 0)
 274                return ret;
 275
 276        schedule_work(&sd->work_struct);
 277
 278        return 0;
 279}
 280
 281/* called on streamoff with alt==0 and on disconnect */
 282/* the usb_lock is held at entry - restore on exit */
 283static void sd_stop0(struct gspca_dev *gspca_dev)
 284{
 285        struct sd *dev = (struct sd *)gspca_dev;
 286
 287        /* wait for the work queue to terminate */
 288        mutex_unlock(&gspca_dev->usb_lock);
 289        /* This waits for vicam_dostream to finish */
 290        flush_work(&dev->work_struct);
 291        mutex_lock(&gspca_dev->usb_lock);
 292
 293        if (gspca_dev->present)
 294                vicam_set_camera_power(gspca_dev, 0);
 295}
 296
 297static int sd_init_controls(struct gspca_dev *gspca_dev)
 298{
 299        struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
 300
 301        gspca_dev->vdev.ctrl_handler = hdl;
 302        v4l2_ctrl_handler_init(hdl, 2);
 303        gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
 304                        V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
 305        gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
 306                        V4L2_CID_GAIN, 0, 255, 1, 200);
 307
 308        if (hdl->error) {
 309                pr_err("Could not initialize controls\n");
 310                return hdl->error;
 311        }
 312        return 0;
 313}
 314
 315/* Table of supported USB devices */
 316static const struct usb_device_id device_table[] = {
 317        {USB_DEVICE(0x04c1, 0x009d)},
 318        {USB_DEVICE(0x0602, 0x1001)},
 319        {}
 320};
 321
 322MODULE_DEVICE_TABLE(usb, device_table);
 323
 324/* sub-driver description */
 325static const struct sd_desc sd_desc = {
 326        .name   = MODULE_NAME,
 327        .config = sd_config,
 328        .init   = sd_init,
 329        .init_controls = sd_init_controls,
 330        .start  = sd_start,
 331        .stop0  = sd_stop0,
 332};
 333
 334/* -- device connect -- */
 335static int sd_probe(struct usb_interface *intf,
 336                const struct usb_device_id *id)
 337{
 338        return gspca_dev_probe(intf, id,
 339                        &sd_desc,
 340                        sizeof(struct sd),
 341                        THIS_MODULE);
 342}
 343
 344static struct usb_driver sd_driver = {
 345        .name       = MODULE_NAME,
 346        .id_table   = device_table,
 347        .probe      = sd_probe,
 348        .disconnect = gspca_disconnect,
 349#ifdef CONFIG_PM
 350        .suspend = gspca_suspend,
 351        .resume  = gspca_resume,
 352        .reset_resume = gspca_resume,
 353#endif
 354};
 355
 356module_usb_driver(sd_driver);
 357