linux/drivers/media/usb/stkwebcam/stk-webcam.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
   4 *
   5 * Copyright (C) 2006 Nicolas VIVIEN
   6 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
   7 *
   8 * Some parts are inspired from cafe_ccic.c
   9 * Copyright 2006-2007 Jonathan Corbet
  10 */
  11
  12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/kernel.h>
  17#include <linux/errno.h>
  18#include <linux/slab.h>
  19
  20#include <linux/dmi.h>
  21#include <linux/usb.h>
  22#include <linux/mm.h>
  23#include <linux/vmalloc.h>
  24#include <linux/videodev2.h>
  25#include <media/v4l2-common.h>
  26#include <media/v4l2-ioctl.h>
  27#include <media/v4l2-event.h>
  28
  29#include "stk-webcam.h"
  30
  31
  32static int hflip = -1;
  33module_param(hflip, int, 0444);
  34MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
  35
  36static int vflip = -1;
  37module_param(vflip, int, 0444);
  38MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
  39
  40static int debug;
  41module_param(debug, int, 0444);
  42MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  43
  44MODULE_LICENSE("GPL");
  45MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  46MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  47
  48/* Some cameras have audio interfaces, we aren't interested in those */
  49static const struct usb_device_id stkwebcam_table[] = {
  50        { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  51        { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  52        { }
  53};
  54MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  55
  56/*
  57 * The stk webcam laptop module is mounted upside down in some laptops :(
  58 *
  59 * Some background information (thanks to Hans de Goede for providing this):
  60 *
  61 * 1) Once upon a time the stkwebcam driver was written
  62 *
  63 * 2) The webcam in question was used mostly in Asus laptop models, including
  64 * the laptop of the original author of the driver, and in these models, in
  65 * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
  66 * they mounted the webcam-module the wrong way up. So the hflip and vflip
  67 * module options were given a default value of 1 (the correct value for
  68 * upside down mounted models)
  69 *
  70 * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
  71 * where the module was actually mounted the right way up, and thus showed
  72 * upside down under Linux. So now I was facing the choice of 2 options:
  73 *
  74 * a) Add a not-upside-down list to stkwebcam, which overrules the default.
  75 *
  76 * b) Do it like all the other drivers do, and make the default right for
  77 *    cams mounted the proper way and add an upside-down model list, with
  78 *    models where we need to flip-by-default.
  79 *
  80 * Despite knowing that going b) would cause a period of pain where we were
  81 * building the table I opted to go for option b), since a) is just too ugly,
  82 * and worse different from how every other driver does it leading to
  83 * confusion in the long run. This change was made in kernel 3.6.
  84 *
  85 * So for any user report about upside-down images since kernel 3.6 ask them
  86 * to provide the output of 'sudo dmidecode' so the laptop can be added in
  87 * the table below.
  88 */
  89static const struct dmi_system_id stk_upside_down_dmi_table[] = {
  90        {
  91                .ident = "ASUS G1",
  92                .matches = {
  93                        DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  94                        DMI_MATCH(DMI_PRODUCT_NAME, "G1")
  95                }
  96        }, {
  97                .ident = "ASUS F3JC",
  98                .matches = {
  99                        DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
 100                        DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
 101                }
 102        },
 103        {
 104                .ident = "T12Rg-H",
 105                .matches = {
 106                        DMI_MATCH(DMI_SYS_VENDOR, "HCL Infosystems Limited"),
 107                        DMI_MATCH(DMI_PRODUCT_NAME, "T12Rg-H")
 108                }
 109        },
 110        {
 111                .ident = "ASUS A6VM",
 112                .matches = {
 113                        DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
 114                        DMI_MATCH(DMI_PRODUCT_NAME, "A6VM")
 115                }
 116        },
 117        {}
 118};
 119
 120
 121/*
 122 * Basic stuff
 123 */
 124int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
 125{
 126        struct usb_device *udev = dev->udev;
 127        int ret;
 128
 129        ret =  usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 130                        0x01,
 131                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 132                        value,
 133                        index,
 134                        NULL,
 135                        0,
 136                        500);
 137        if (ret < 0)
 138                return ret;
 139        else
 140                return 0;
 141}
 142
 143int stk_camera_read_reg(struct stk_camera *dev, u16 index, u8 *value)
 144{
 145        struct usb_device *udev = dev->udev;
 146        unsigned char *buf;
 147        int ret;
 148
 149        buf = kmalloc(sizeof(u8), GFP_KERNEL);
 150        if (!buf)
 151                return -ENOMEM;
 152
 153        ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
 154                        0x00,
 155                        USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 156                        0x00,
 157                        index,
 158                        buf,
 159                        sizeof(u8),
 160                        500);
 161        if (ret >= 0)
 162                *value = *buf;
 163
 164        kfree(buf);
 165
 166        if (ret < 0)
 167                return ret;
 168        else
 169                return 0;
 170}
 171
 172static int stk_start_stream(struct stk_camera *dev)
 173{
 174        u8 value;
 175        int i, ret;
 176        u8 value_116, value_117;
 177
 178
 179        if (!is_present(dev))
 180                return -ENODEV;
 181        if (!is_memallocd(dev) || !is_initialised(dev)) {
 182                pr_err("FIXME: Buffers are not allocated\n");
 183                return -EFAULT;
 184        }
 185        ret = usb_set_interface(dev->udev, 0, 5);
 186
 187        if (ret < 0)
 188                pr_err("usb_set_interface failed !\n");
 189        if (stk_sensor_wakeup(dev))
 190                pr_err("error awaking the sensor\n");
 191
 192        stk_camera_read_reg(dev, 0x0116, &value_116);
 193        stk_camera_read_reg(dev, 0x0117, &value_117);
 194
 195        stk_camera_write_reg(dev, 0x0116, 0x0000);
 196        stk_camera_write_reg(dev, 0x0117, 0x0000);
 197
 198        stk_camera_read_reg(dev, 0x0100, &value);
 199        stk_camera_write_reg(dev, 0x0100, value | 0x80);
 200
 201        stk_camera_write_reg(dev, 0x0116, value_116);
 202        stk_camera_write_reg(dev, 0x0117, value_117);
 203        for (i = 0; i < MAX_ISO_BUFS; i++) {
 204                if (dev->isobufs[i].urb) {
 205                        ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
 206                        atomic_inc(&dev->urbs_used);
 207                        if (ret)
 208                                return ret;
 209                }
 210        }
 211        set_streaming(dev);
 212        return 0;
 213}
 214
 215static int stk_stop_stream(struct stk_camera *dev)
 216{
 217        u8 value;
 218        int i;
 219        if (is_present(dev)) {
 220                stk_camera_read_reg(dev, 0x0100, &value);
 221                stk_camera_write_reg(dev, 0x0100, value & ~0x80);
 222                if (dev->isobufs != NULL) {
 223                        for (i = 0; i < MAX_ISO_BUFS; i++) {
 224                                if (dev->isobufs[i].urb)
 225                                        usb_kill_urb(dev->isobufs[i].urb);
 226                        }
 227                }
 228                unset_streaming(dev);
 229
 230                if (usb_set_interface(dev->udev, 0, 0))
 231                        pr_err("usb_set_interface failed !\n");
 232                if (stk_sensor_sleep(dev))
 233                        pr_err("error suspending the sensor\n");
 234        }
 235        return 0;
 236}
 237
 238/*
 239 * This seems to be the shortest init sequence we
 240 * must do in order to find the sensor
 241 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
 242 * is also reset. Maybe powers down it?
 243 * Rest of values don't make a difference
 244 */
 245
 246static struct regval stk1125_initvals[] = {
 247        /*TODO: What means this sequence? */
 248        {0x0000, 0x24},
 249        {0x0100, 0x21},
 250        {0x0002, 0x68},
 251        {0x0003, 0x80},
 252        {0x0005, 0x00},
 253        {0x0007, 0x03},
 254        {0x000d, 0x00},
 255        {0x000f, 0x02},
 256        {0x0300, 0x12},
 257        {0x0350, 0x41},
 258        {0x0351, 0x00},
 259        {0x0352, 0x00},
 260        {0x0353, 0x00},
 261        {0x0018, 0x10},
 262        {0x0019, 0x00},
 263        {0x001b, 0x0e},
 264        {0x001c, 0x46},
 265        {0x0300, 0x80},
 266        {0x001a, 0x04},
 267        {0x0110, 0x00},
 268        {0x0111, 0x00},
 269        {0x0112, 0x00},
 270        {0x0113, 0x00},
 271
 272        {0xffff, 0xff},
 273};
 274
 275
 276static int stk_initialise(struct stk_camera *dev)
 277{
 278        struct regval *rv;
 279        int ret;
 280        if (!is_present(dev))
 281                return -ENODEV;
 282        if (is_initialised(dev))
 283                return 0;
 284        rv = stk1125_initvals;
 285        while (rv->reg != 0xffff) {
 286                ret = stk_camera_write_reg(dev, rv->reg, rv->val);
 287                if (ret)
 288                        return ret;
 289                rv++;
 290        }
 291        if (stk_sensor_init(dev) == 0) {
 292                set_initialised(dev);
 293                return 0;
 294        } else
 295                return -1;
 296}
 297
 298/* *********************************************** */
 299/*
 300 * This function is called as an URB transfert is complete (Isochronous pipe).
 301 * So, the traitement is done in interrupt time, so it has be fast, not crash,
 302 * and not stall. Neat.
 303 */
 304static void stk_isoc_handler(struct urb *urb)
 305{
 306        int i;
 307        int ret;
 308        int framelen;
 309        unsigned long flags;
 310
 311        unsigned char *fill = NULL;
 312        unsigned char *iso_buf = NULL;
 313
 314        struct stk_camera *dev;
 315        struct stk_sio_buffer *fb;
 316
 317        dev = (struct stk_camera *) urb->context;
 318
 319        if (dev == NULL) {
 320                pr_err("isoc_handler called with NULL device !\n");
 321                return;
 322        }
 323
 324        if (urb->status == -ENOENT || urb->status == -ECONNRESET
 325                || urb->status == -ESHUTDOWN) {
 326                atomic_dec(&dev->urbs_used);
 327                return;
 328        }
 329
 330        spin_lock_irqsave(&dev->spinlock, flags);
 331
 332        if (urb->status != -EINPROGRESS && urb->status != 0) {
 333                pr_err("isoc_handler: urb->status == %d\n", urb->status);
 334                goto resubmit;
 335        }
 336
 337        if (list_empty(&dev->sio_avail)) {
 338                /*FIXME Stop streaming after a while */
 339                pr_err_ratelimited("isoc_handler without available buffer!\n");
 340                goto resubmit;
 341        }
 342        fb = list_first_entry(&dev->sio_avail,
 343                        struct stk_sio_buffer, list);
 344        fill = fb->buffer + fb->v4lbuf.bytesused;
 345
 346        for (i = 0; i < urb->number_of_packets; i++) {
 347                if (urb->iso_frame_desc[i].status != 0) {
 348                        if (urb->iso_frame_desc[i].status != -EXDEV)
 349                                pr_err("Frame %d has error %d\n",
 350                                       i, urb->iso_frame_desc[i].status);
 351                        continue;
 352                }
 353                framelen = urb->iso_frame_desc[i].actual_length;
 354                iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
 355
 356                if (framelen <= 4)
 357                        continue; /* no data */
 358
 359                /*
 360                 * we found something informational from there
 361                 * the isoc frames have to type of headers
 362                 * type1: 00 xx 00 00 or 20 xx 00 00
 363                 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
 364                 * xx is a sequencer which has never been seen over 0x3f
 365                 * imho data written down looks like bayer, i see similarities
 366                 * after every 640 bytes
 367                 */
 368                if (*iso_buf & 0x80) {
 369                        framelen -= 8;
 370                        iso_buf += 8;
 371                        /* This marks a new frame */
 372                        if (fb->v4lbuf.bytesused != 0
 373                                && fb->v4lbuf.bytesused != dev->frame_size) {
 374                                pr_err_ratelimited("frame %d, bytesused=%d, skipping\n",
 375                                                   i, fb->v4lbuf.bytesused);
 376                                fb->v4lbuf.bytesused = 0;
 377                                fill = fb->buffer;
 378                        } else if (fb->v4lbuf.bytesused == dev->frame_size) {
 379                                if (list_is_singular(&dev->sio_avail)) {
 380                                        /* Always reuse the last buffer */
 381                                        fb->v4lbuf.bytesused = 0;
 382                                        fill = fb->buffer;
 383                                } else {
 384                                        list_move_tail(dev->sio_avail.next,
 385                                                &dev->sio_full);
 386                                        wake_up(&dev->wait_frame);
 387                                        fb = list_first_entry(&dev->sio_avail,
 388                                                struct stk_sio_buffer, list);
 389                                        fb->v4lbuf.bytesused = 0;
 390                                        fill = fb->buffer;
 391                                }
 392                        }
 393                } else {
 394                        framelen -= 4;
 395                        iso_buf += 4;
 396                }
 397
 398                /* Our buffer is full !!! */
 399                if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
 400                        pr_err_ratelimited("Frame buffer overflow, lost sync\n");
 401                        /*FIXME Do something here? */
 402                        continue;
 403                }
 404                spin_unlock_irqrestore(&dev->spinlock, flags);
 405                memcpy(fill, iso_buf, framelen);
 406                spin_lock_irqsave(&dev->spinlock, flags);
 407                fill += framelen;
 408
 409                /* New size of our buffer */
 410                fb->v4lbuf.bytesused += framelen;
 411        }
 412
 413resubmit:
 414        spin_unlock_irqrestore(&dev->spinlock, flags);
 415        urb->dev = dev->udev;
 416        ret = usb_submit_urb(urb, GFP_ATOMIC);
 417        if (ret != 0) {
 418                pr_err("Error (%d) re-submitting urb in stk_isoc_handler\n",
 419                       ret);
 420        }
 421}
 422
 423/* -------------------------------------------- */
 424
 425static int stk_prepare_iso(struct stk_camera *dev)
 426{
 427        void *kbuf;
 428        int i, j;
 429        struct urb *urb;
 430        struct usb_device *udev;
 431
 432        if (dev == NULL)
 433                return -ENXIO;
 434        udev = dev->udev;
 435
 436        if (dev->isobufs)
 437                pr_err("isobufs already allocated. Bad\n");
 438        else
 439                dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
 440                                       GFP_KERNEL);
 441        if (dev->isobufs == NULL) {
 442                pr_err("Unable to allocate iso buffers\n");
 443                return -ENOMEM;
 444        }
 445        for (i = 0; i < MAX_ISO_BUFS; i++) {
 446                if (dev->isobufs[i].data == NULL) {
 447                        kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
 448                        if (kbuf == NULL) {
 449                                pr_err("Failed to allocate iso buffer %d\n", i);
 450                                goto isobufs_out;
 451                        }
 452                        dev->isobufs[i].data = kbuf;
 453                } else
 454                        pr_err("isobuf data already allocated\n");
 455                if (dev->isobufs[i].urb == NULL) {
 456                        urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
 457                        if (urb == NULL)
 458                                goto isobufs_out;
 459                        dev->isobufs[i].urb = urb;
 460                } else {
 461                        pr_err("Killing URB\n");
 462                        usb_kill_urb(dev->isobufs[i].urb);
 463                        urb = dev->isobufs[i].urb;
 464                }
 465                urb->interval = 1;
 466                urb->dev = udev;
 467                urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
 468                urb->transfer_flags = URB_ISO_ASAP;
 469                urb->transfer_buffer = dev->isobufs[i].data;
 470                urb->transfer_buffer_length = ISO_BUFFER_SIZE;
 471                urb->complete = stk_isoc_handler;
 472                urb->context = dev;
 473                urb->start_frame = 0;
 474                urb->number_of_packets = ISO_FRAMES_PER_DESC;
 475
 476                for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
 477                        urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
 478                        urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
 479                }
 480        }
 481        set_memallocd(dev);
 482        return 0;
 483
 484isobufs_out:
 485        for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
 486                kfree(dev->isobufs[i].data);
 487        for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
 488                usb_free_urb(dev->isobufs[i].urb);
 489        kfree(dev->isobufs);
 490        dev->isobufs = NULL;
 491        return -ENOMEM;
 492}
 493
 494static void stk_clean_iso(struct stk_camera *dev)
 495{
 496        int i;
 497
 498        if (dev == NULL || dev->isobufs == NULL)
 499                return;
 500
 501        for (i = 0; i < MAX_ISO_BUFS; i++) {
 502                struct urb *urb;
 503
 504                urb = dev->isobufs[i].urb;
 505                if (urb) {
 506                        if (atomic_read(&dev->urbs_used) && is_present(dev))
 507                                usb_kill_urb(urb);
 508                        usb_free_urb(urb);
 509                }
 510                kfree(dev->isobufs[i].data);
 511        }
 512        kfree(dev->isobufs);
 513        dev->isobufs = NULL;
 514        unset_memallocd(dev);
 515}
 516
 517static int stk_setup_siobuf(struct stk_camera *dev, int index)
 518{
 519        struct stk_sio_buffer *buf = dev->sio_bufs + index;
 520        INIT_LIST_HEAD(&buf->list);
 521        buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
 522        buf->buffer = vmalloc_user(buf->v4lbuf.length);
 523        if (buf->buffer == NULL)
 524                return -ENOMEM;
 525        buf->mapcount = 0;
 526        buf->dev = dev;
 527        buf->v4lbuf.index = index;
 528        buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 529        buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 530        buf->v4lbuf.field = V4L2_FIELD_NONE;
 531        buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
 532        buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
 533        return 0;
 534}
 535
 536static int stk_free_sio_buffers(struct stk_camera *dev)
 537{
 538        int i;
 539        int nbufs;
 540        unsigned long flags;
 541        if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
 542                return 0;
 543        /*
 544        * If any buffers are mapped, we cannot free them at all.
 545        */
 546        for (i = 0; i < dev->n_sbufs; i++) {
 547                if (dev->sio_bufs[i].mapcount > 0)
 548                        return -EBUSY;
 549        }
 550        /*
 551        * OK, let's do it.
 552        */
 553        spin_lock_irqsave(&dev->spinlock, flags);
 554        INIT_LIST_HEAD(&dev->sio_avail);
 555        INIT_LIST_HEAD(&dev->sio_full);
 556        nbufs = dev->n_sbufs;
 557        dev->n_sbufs = 0;
 558        spin_unlock_irqrestore(&dev->spinlock, flags);
 559        for (i = 0; i < nbufs; i++)
 560                vfree(dev->sio_bufs[i].buffer);
 561        kfree(dev->sio_bufs);
 562        dev->sio_bufs = NULL;
 563        return 0;
 564}
 565
 566static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
 567{
 568        int i;
 569        if (dev->sio_bufs != NULL)
 570                pr_err("sio_bufs already allocated\n");
 571        else {
 572                dev->sio_bufs = kcalloc(n_sbufs,
 573                                        sizeof(struct stk_sio_buffer),
 574                                        GFP_KERNEL);
 575                if (dev->sio_bufs == NULL)
 576                        return -ENOMEM;
 577                for (i = 0; i < n_sbufs; i++) {
 578                        if (stk_setup_siobuf(dev, i))
 579                                return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
 580                        dev->n_sbufs = i+1;
 581                }
 582        }
 583        return 0;
 584}
 585
 586static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
 587{
 588        int err;
 589        err = stk_prepare_iso(dev);
 590        if (err) {
 591                stk_clean_iso(dev);
 592                return err;
 593        }
 594        err = stk_prepare_sio_buffers(dev, n_sbufs);
 595        if (err) {
 596                stk_free_sio_buffers(dev);
 597                return err;
 598        }
 599        return 0;
 600}
 601
 602static void stk_free_buffers(struct stk_camera *dev)
 603{
 604        stk_clean_iso(dev);
 605        stk_free_sio_buffers(dev);
 606}
 607/* -------------------------------------------- */
 608
 609/* v4l file operations */
 610
 611static int v4l_stk_open(struct file *fp)
 612{
 613        struct stk_camera *dev = video_drvdata(fp);
 614        int err;
 615
 616        if (dev == NULL || !is_present(dev))
 617                return -ENXIO;
 618
 619        if (mutex_lock_interruptible(&dev->lock))
 620                return -ERESTARTSYS;
 621        if (!dev->first_init)
 622                stk_camera_write_reg(dev, 0x0, 0x24);
 623        else
 624                dev->first_init = 0;
 625
 626        err = v4l2_fh_open(fp);
 627        if (!err)
 628                usb_autopm_get_interface(dev->interface);
 629        mutex_unlock(&dev->lock);
 630        return err;
 631}
 632
 633static int v4l_stk_release(struct file *fp)
 634{
 635        struct stk_camera *dev = video_drvdata(fp);
 636
 637        mutex_lock(&dev->lock);
 638        if (dev->owner == fp) {
 639                stk_stop_stream(dev);
 640                stk_free_buffers(dev);
 641                stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
 642                unset_initialised(dev);
 643                dev->owner = NULL;
 644        }
 645
 646        if (is_present(dev))
 647                usb_autopm_put_interface(dev->interface);
 648        mutex_unlock(&dev->lock);
 649        return v4l2_fh_release(fp);
 650}
 651
 652static ssize_t stk_read(struct file *fp, char __user *buf,
 653                size_t count, loff_t *f_pos)
 654{
 655        int i;
 656        int ret;
 657        unsigned long flags;
 658        struct stk_sio_buffer *sbuf;
 659        struct stk_camera *dev = video_drvdata(fp);
 660
 661        if (!is_present(dev))
 662                return -EIO;
 663        if (dev->owner && (!dev->reading || dev->owner != fp))
 664                return -EBUSY;
 665        dev->owner = fp;
 666        if (!is_streaming(dev)) {
 667                if (stk_initialise(dev)
 668                        || stk_allocate_buffers(dev, 3)
 669                        || stk_start_stream(dev))
 670                        return -ENOMEM;
 671                dev->reading = 1;
 672                spin_lock_irqsave(&dev->spinlock, flags);
 673                for (i = 0; i < dev->n_sbufs; i++) {
 674                        list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
 675                        dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
 676                }
 677                spin_unlock_irqrestore(&dev->spinlock, flags);
 678        }
 679        if (*f_pos == 0) {
 680                if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
 681                        return -EWOULDBLOCK;
 682                ret = wait_event_interruptible(dev->wait_frame,
 683                        !list_empty(&dev->sio_full) || !is_present(dev));
 684                if (ret)
 685                        return ret;
 686                if (!is_present(dev))
 687                        return -EIO;
 688        }
 689        if (count + *f_pos > dev->frame_size)
 690                count = dev->frame_size - *f_pos;
 691        spin_lock_irqsave(&dev->spinlock, flags);
 692        if (list_empty(&dev->sio_full)) {
 693                spin_unlock_irqrestore(&dev->spinlock, flags);
 694                pr_err("BUG: No siobufs ready\n");
 695                return 0;
 696        }
 697        sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
 698        spin_unlock_irqrestore(&dev->spinlock, flags);
 699
 700        if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
 701                return -EFAULT;
 702
 703        *f_pos += count;
 704
 705        if (*f_pos >= dev->frame_size) {
 706                *f_pos = 0;
 707                spin_lock_irqsave(&dev->spinlock, flags);
 708                list_move_tail(&sbuf->list, &dev->sio_avail);
 709                spin_unlock_irqrestore(&dev->spinlock, flags);
 710        }
 711        return count;
 712}
 713
 714static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
 715                size_t count, loff_t *f_pos)
 716{
 717        struct stk_camera *dev = video_drvdata(fp);
 718        int ret;
 719
 720        if (mutex_lock_interruptible(&dev->lock))
 721                return -ERESTARTSYS;
 722        ret = stk_read(fp, buf, count, f_pos);
 723        mutex_unlock(&dev->lock);
 724        return ret;
 725}
 726
 727static __poll_t v4l_stk_poll(struct file *fp, poll_table *wait)
 728{
 729        struct stk_camera *dev = video_drvdata(fp);
 730        __poll_t res = v4l2_ctrl_poll(fp, wait);
 731
 732        poll_wait(fp, &dev->wait_frame, wait);
 733
 734        if (!is_present(dev))
 735                return EPOLLERR;
 736
 737        if (!list_empty(&dev->sio_full))
 738                return res | EPOLLIN | EPOLLRDNORM;
 739
 740        return res;
 741}
 742
 743
 744static void stk_v4l_vm_open(struct vm_area_struct *vma)
 745{
 746        struct stk_sio_buffer *sbuf = vma->vm_private_data;
 747        sbuf->mapcount++;
 748}
 749static void stk_v4l_vm_close(struct vm_area_struct *vma)
 750{
 751        struct stk_sio_buffer *sbuf = vma->vm_private_data;
 752        sbuf->mapcount--;
 753        if (sbuf->mapcount == 0)
 754                sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
 755}
 756static const struct vm_operations_struct stk_v4l_vm_ops = {
 757        .open = stk_v4l_vm_open,
 758        .close = stk_v4l_vm_close
 759};
 760
 761static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
 762{
 763        unsigned int i;
 764        int ret;
 765        unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
 766        struct stk_camera *dev = video_drvdata(fp);
 767        struct stk_sio_buffer *sbuf = NULL;
 768
 769        if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
 770                return -EINVAL;
 771
 772        for (i = 0; i < dev->n_sbufs; i++) {
 773                if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
 774                        sbuf = dev->sio_bufs + i;
 775                        break;
 776                }
 777        }
 778        if (sbuf == NULL)
 779                return -EINVAL;
 780        ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
 781        if (ret)
 782                return ret;
 783        vma->vm_flags |= VM_DONTEXPAND;
 784        vma->vm_private_data = sbuf;
 785        vma->vm_ops = &stk_v4l_vm_ops;
 786        sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
 787        stk_v4l_vm_open(vma);
 788        return 0;
 789}
 790
 791/* v4l ioctl handlers */
 792
 793static int stk_vidioc_querycap(struct file *filp,
 794                void *priv, struct v4l2_capability *cap)
 795{
 796        struct stk_camera *dev = video_drvdata(filp);
 797
 798        strscpy(cap->driver, "stk", sizeof(cap->driver));
 799        strscpy(cap->card, "stk", sizeof(cap->card));
 800        usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
 801        return 0;
 802}
 803
 804static int stk_vidioc_enum_input(struct file *filp,
 805                void *priv, struct v4l2_input *input)
 806{
 807        if (input->index != 0)
 808                return -EINVAL;
 809
 810        strscpy(input->name, "Syntek USB Camera", sizeof(input->name));
 811        input->type = V4L2_INPUT_TYPE_CAMERA;
 812        return 0;
 813}
 814
 815
 816static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
 817{
 818        *i = 0;
 819        return 0;
 820}
 821
 822static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
 823{
 824        return i ? -EINVAL : 0;
 825}
 826
 827static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
 828{
 829        struct stk_camera *dev =
 830                container_of(ctrl->handler, struct stk_camera, hdl);
 831
 832        switch (ctrl->id) {
 833        case V4L2_CID_BRIGHTNESS:
 834                return stk_sensor_set_brightness(dev, ctrl->val);
 835        case V4L2_CID_HFLIP:
 836                if (dmi_check_system(stk_upside_down_dmi_table))
 837                        dev->vsettings.hflip = !ctrl->val;
 838                else
 839                        dev->vsettings.hflip = ctrl->val;
 840                return 0;
 841        case V4L2_CID_VFLIP:
 842                if (dmi_check_system(stk_upside_down_dmi_table))
 843                        dev->vsettings.vflip = !ctrl->val;
 844                else
 845                        dev->vsettings.vflip = ctrl->val;
 846                return 0;
 847        default:
 848                return -EINVAL;
 849        }
 850        return 0;
 851}
 852
 853
 854static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
 855                void *priv, struct v4l2_fmtdesc *fmtd)
 856{
 857        switch (fmtd->index) {
 858        case 0:
 859                fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
 860                strscpy(fmtd->description, "r5g6b5", sizeof(fmtd->description));
 861                break;
 862        case 1:
 863                fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
 864                strscpy(fmtd->description, "r5g6b5BE", sizeof(fmtd->description));
 865                break;
 866        case 2:
 867                fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
 868                strscpy(fmtd->description, "yuv4:2:2", sizeof(fmtd->description));
 869                break;
 870        case 3:
 871                fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
 872                strscpy(fmtd->description, "Raw bayer", sizeof(fmtd->description));
 873                break;
 874        case 4:
 875                fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
 876                strscpy(fmtd->description, "yuv4:2:2", sizeof(fmtd->description));
 877                break;
 878        default:
 879                return -EINVAL;
 880        }
 881        return 0;
 882}
 883
 884static struct stk_size {
 885        unsigned w;
 886        unsigned h;
 887        enum stk_mode m;
 888} stk_sizes[] = {
 889        { .w = 1280, .h = 1024, .m = MODE_SXGA, },
 890        { .w = 640,  .h = 480,  .m = MODE_VGA,  },
 891        { .w = 352,  .h = 288,  .m = MODE_CIF,  },
 892        { .w = 320,  .h = 240,  .m = MODE_QVGA, },
 893        { .w = 176,  .h = 144,  .m = MODE_QCIF, },
 894};
 895
 896static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
 897                void *priv, struct v4l2_format *f)
 898{
 899        struct v4l2_pix_format *pix_format = &f->fmt.pix;
 900        struct stk_camera *dev = video_drvdata(filp);
 901        int i;
 902
 903        for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
 904                        stk_sizes[i].m != dev->vsettings.mode; i++)
 905                ;
 906        if (i == ARRAY_SIZE(stk_sizes)) {
 907                pr_err("ERROR: mode invalid\n");
 908                return -EINVAL;
 909        }
 910        pix_format->width = stk_sizes[i].w;
 911        pix_format->height = stk_sizes[i].h;
 912        pix_format->field = V4L2_FIELD_NONE;
 913        pix_format->colorspace = V4L2_COLORSPACE_SRGB;
 914        pix_format->pixelformat = dev->vsettings.palette;
 915        if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
 916                pix_format->bytesperline = pix_format->width;
 917        else
 918                pix_format->bytesperline = 2 * pix_format->width;
 919        pix_format->sizeimage = pix_format->bytesperline
 920                                * pix_format->height;
 921        return 0;
 922}
 923
 924static int stk_try_fmt_vid_cap(struct file *filp,
 925                struct v4l2_format *fmtd, int *idx)
 926{
 927        int i;
 928        switch (fmtd->fmt.pix.pixelformat) {
 929        case V4L2_PIX_FMT_RGB565:
 930        case V4L2_PIX_FMT_RGB565X:
 931        case V4L2_PIX_FMT_UYVY:
 932        case V4L2_PIX_FMT_YUYV:
 933        case V4L2_PIX_FMT_SBGGR8:
 934                break;
 935        default:
 936                return -EINVAL;
 937        }
 938        for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
 939                if (fmtd->fmt.pix.width > stk_sizes[i].w)
 940                        break;
 941        }
 942        if (i == ARRAY_SIZE(stk_sizes)
 943                || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
 944                        < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
 945                fmtd->fmt.pix.height = stk_sizes[i-1].h;
 946                fmtd->fmt.pix.width = stk_sizes[i-1].w;
 947                if (idx)
 948                        *idx = i - 1;
 949        } else {
 950                fmtd->fmt.pix.height = stk_sizes[i].h;
 951                fmtd->fmt.pix.width = stk_sizes[i].w;
 952                if (idx)
 953                        *idx = i;
 954        }
 955
 956        fmtd->fmt.pix.field = V4L2_FIELD_NONE;
 957        fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
 958        if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
 959                fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
 960        else
 961                fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
 962        fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
 963                * fmtd->fmt.pix.height;
 964        return 0;
 965}
 966
 967static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
 968                void *priv, struct v4l2_format *fmtd)
 969{
 970        return stk_try_fmt_vid_cap(filp, fmtd, NULL);
 971}
 972
 973static int stk_setup_format(struct stk_camera *dev)
 974{
 975        int i = 0;
 976        int depth;
 977        if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
 978                depth = 1;
 979        else
 980                depth = 2;
 981        while (i < ARRAY_SIZE(stk_sizes) &&
 982                        stk_sizes[i].m != dev->vsettings.mode)
 983                i++;
 984        if (i == ARRAY_SIZE(stk_sizes)) {
 985                pr_err("Something is broken in %s\n", __func__);
 986                return -EFAULT;
 987        }
 988        /* This registers controls some timings, not sure of what. */
 989        stk_camera_write_reg(dev, 0x001b, 0x0e);
 990        if (dev->vsettings.mode == MODE_SXGA)
 991                stk_camera_write_reg(dev, 0x001c, 0x0e);
 992        else
 993                stk_camera_write_reg(dev, 0x001c, 0x46);
 994        /*
 995         * Registers 0x0115 0x0114 are the size of each line (bytes),
 996         * regs 0x0117 0x0116 are the height of the image.
 997         */
 998        stk_camera_write_reg(dev, 0x0115,
 999                ((stk_sizes[i].w * depth) >> 8) & 0xff);
1000        stk_camera_write_reg(dev, 0x0114,
1001                (stk_sizes[i].w * depth) & 0xff);
1002        stk_camera_write_reg(dev, 0x0117,
1003                (stk_sizes[i].h >> 8) & 0xff);
1004        stk_camera_write_reg(dev, 0x0116,
1005                stk_sizes[i].h & 0xff);
1006        return stk_sensor_configure(dev);
1007}
1008
1009static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1010                void *priv, struct v4l2_format *fmtd)
1011{
1012        int ret;
1013        int idx;
1014        struct stk_camera *dev = video_drvdata(filp);
1015
1016        if (dev == NULL)
1017                return -ENODEV;
1018        if (!is_present(dev))
1019                return -ENODEV;
1020        if (is_streaming(dev))
1021                return -EBUSY;
1022        if (dev->owner)
1023                return -EBUSY;
1024        ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
1025        if (ret)
1026                return ret;
1027
1028        dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1029        stk_free_buffers(dev);
1030        dev->frame_size = fmtd->fmt.pix.sizeimage;
1031        dev->vsettings.mode = stk_sizes[idx].m;
1032
1033        stk_initialise(dev);
1034        return stk_setup_format(dev);
1035}
1036
1037static int stk_vidioc_reqbufs(struct file *filp,
1038                void *priv, struct v4l2_requestbuffers *rb)
1039{
1040        struct stk_camera *dev = video_drvdata(filp);
1041
1042        if (dev == NULL)
1043                return -ENODEV;
1044        if (rb->memory != V4L2_MEMORY_MMAP)
1045                return -EINVAL;
1046        if (is_streaming(dev)
1047                || (dev->owner && dev->owner != filp))
1048                return -EBUSY;
1049        stk_free_buffers(dev);
1050        if (rb->count == 0) {
1051                stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
1052                unset_initialised(dev);
1053                dev->owner = NULL;
1054                return 0;
1055        }
1056        dev->owner = filp;
1057
1058        /*FIXME If they ask for zero, we must stop streaming and free */
1059        if (rb->count < 3)
1060                rb->count = 3;
1061        /* Arbitrary limit */
1062        else if (rb->count > 5)
1063                rb->count = 5;
1064
1065        stk_allocate_buffers(dev, rb->count);
1066        rb->count = dev->n_sbufs;
1067        return 0;
1068}
1069
1070static int stk_vidioc_querybuf(struct file *filp,
1071                void *priv, struct v4l2_buffer *buf)
1072{
1073        struct stk_camera *dev = video_drvdata(filp);
1074        struct stk_sio_buffer *sbuf;
1075
1076        if (buf->index >= dev->n_sbufs)
1077                return -EINVAL;
1078        sbuf = dev->sio_bufs + buf->index;
1079        *buf = sbuf->v4lbuf;
1080        return 0;
1081}
1082
1083static int stk_vidioc_qbuf(struct file *filp,
1084                void *priv, struct v4l2_buffer *buf)
1085{
1086        struct stk_camera *dev = video_drvdata(filp);
1087        struct stk_sio_buffer *sbuf;
1088        unsigned long flags;
1089
1090        if (buf->memory != V4L2_MEMORY_MMAP)
1091                return -EINVAL;
1092
1093        if (buf->index >= dev->n_sbufs)
1094                return -EINVAL;
1095        sbuf = dev->sio_bufs + buf->index;
1096        if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1097                return 0;
1098        sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1099        sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1100        spin_lock_irqsave(&dev->spinlock, flags);
1101        list_add_tail(&sbuf->list, &dev->sio_avail);
1102        *buf = sbuf->v4lbuf;
1103        spin_unlock_irqrestore(&dev->spinlock, flags);
1104        return 0;
1105}
1106
1107static int stk_vidioc_dqbuf(struct file *filp,
1108                void *priv, struct v4l2_buffer *buf)
1109{
1110        struct stk_camera *dev = video_drvdata(filp);
1111        struct stk_sio_buffer *sbuf;
1112        unsigned long flags;
1113        int ret;
1114
1115        if (!is_streaming(dev))
1116                return -EINVAL;
1117
1118        if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1119                return -EWOULDBLOCK;
1120        ret = wait_event_interruptible(dev->wait_frame,
1121                !list_empty(&dev->sio_full) || !is_present(dev));
1122        if (ret)
1123                return ret;
1124        if (!is_present(dev))
1125                return -EIO;
1126
1127        spin_lock_irqsave(&dev->spinlock, flags);
1128        sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1129        list_del_init(&sbuf->list);
1130        spin_unlock_irqrestore(&dev->spinlock, flags);
1131        sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1132        sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1133        sbuf->v4lbuf.sequence = ++dev->sequence;
1134        sbuf->v4lbuf.timestamp = ns_to_timeval(ktime_get_ns());
1135
1136        *buf = sbuf->v4lbuf;
1137        return 0;
1138}
1139
1140static int stk_vidioc_streamon(struct file *filp,
1141                void *priv, enum v4l2_buf_type type)
1142{
1143        struct stk_camera *dev = video_drvdata(filp);
1144        if (is_streaming(dev))
1145                return 0;
1146        if (dev->sio_bufs == NULL)
1147                return -EINVAL;
1148        dev->sequence = 0;
1149        return stk_start_stream(dev);
1150}
1151
1152static int stk_vidioc_streamoff(struct file *filp,
1153                void *priv, enum v4l2_buf_type type)
1154{
1155        struct stk_camera *dev = video_drvdata(filp);
1156        unsigned long flags;
1157        int i;
1158        stk_stop_stream(dev);
1159        spin_lock_irqsave(&dev->spinlock, flags);
1160        INIT_LIST_HEAD(&dev->sio_avail);
1161        INIT_LIST_HEAD(&dev->sio_full);
1162        for (i = 0; i < dev->n_sbufs; i++) {
1163                INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1164                dev->sio_bufs[i].v4lbuf.flags = 0;
1165        }
1166        spin_unlock_irqrestore(&dev->spinlock, flags);
1167        return 0;
1168}
1169
1170
1171static int stk_vidioc_g_parm(struct file *filp,
1172                void *priv, struct v4l2_streamparm *sp)
1173{
1174        /*FIXME This is not correct */
1175        sp->parm.capture.timeperframe.numerator = 1;
1176        sp->parm.capture.timeperframe.denominator = 30;
1177        sp->parm.capture.readbuffers = 2;
1178        return 0;
1179}
1180
1181static int stk_vidioc_enum_framesizes(struct file *filp,
1182                void *priv, struct v4l2_frmsizeenum *frms)
1183{
1184        if (frms->index >= ARRAY_SIZE(stk_sizes))
1185                return -EINVAL;
1186        switch (frms->pixel_format) {
1187        case V4L2_PIX_FMT_RGB565:
1188        case V4L2_PIX_FMT_RGB565X:
1189        case V4L2_PIX_FMT_UYVY:
1190        case V4L2_PIX_FMT_YUYV:
1191        case V4L2_PIX_FMT_SBGGR8:
1192                frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1193                frms->discrete.width = stk_sizes[frms->index].w;
1194                frms->discrete.height = stk_sizes[frms->index].h;
1195                return 0;
1196        default: return -EINVAL;
1197        }
1198}
1199
1200static const struct v4l2_ctrl_ops stk_ctrl_ops = {
1201        .s_ctrl = stk_s_ctrl,
1202};
1203
1204static const struct v4l2_file_operations v4l_stk_fops = {
1205        .owner = THIS_MODULE,
1206        .open = v4l_stk_open,
1207        .release = v4l_stk_release,
1208        .read = v4l_stk_read,
1209        .poll = v4l_stk_poll,
1210        .mmap = v4l_stk_mmap,
1211        .unlocked_ioctl = video_ioctl2,
1212};
1213
1214static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1215        .vidioc_querycap = stk_vidioc_querycap,
1216        .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1217        .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1218        .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1219        .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1220        .vidioc_enum_input = stk_vidioc_enum_input,
1221        .vidioc_s_input = stk_vidioc_s_input,
1222        .vidioc_g_input = stk_vidioc_g_input,
1223        .vidioc_reqbufs = stk_vidioc_reqbufs,
1224        .vidioc_querybuf = stk_vidioc_querybuf,
1225        .vidioc_qbuf = stk_vidioc_qbuf,
1226        .vidioc_dqbuf = stk_vidioc_dqbuf,
1227        .vidioc_streamon = stk_vidioc_streamon,
1228        .vidioc_streamoff = stk_vidioc_streamoff,
1229        .vidioc_g_parm = stk_vidioc_g_parm,
1230        .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1231        .vidioc_log_status = v4l2_ctrl_log_status,
1232        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1233        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1234};
1235
1236static void stk_v4l_dev_release(struct video_device *vd)
1237{
1238        struct stk_camera *dev = vdev_to_camera(vd);
1239
1240        if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1241                pr_err("We are leaking memory\n");
1242        usb_put_intf(dev->interface);
1243}
1244
1245static const struct video_device stk_v4l_data = {
1246        .name = "stkwebcam",
1247        .fops = &v4l_stk_fops,
1248        .ioctl_ops = &v4l_stk_ioctl_ops,
1249        .release = stk_v4l_dev_release,
1250};
1251
1252
1253static int stk_register_video_device(struct stk_camera *dev)
1254{
1255        int err;
1256
1257        dev->vdev = stk_v4l_data;
1258        dev->vdev.lock = &dev->lock;
1259        dev->vdev.v4l2_dev = &dev->v4l2_dev;
1260        dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1261                                V4L2_CAP_STREAMING;
1262        video_set_drvdata(&dev->vdev, dev);
1263        err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1264        if (err)
1265                pr_err("v4l registration failed\n");
1266        else
1267                pr_info("Syntek USB2.0 Camera is now controlling device %s\n",
1268                        video_device_node_name(&dev->vdev));
1269        return err;
1270}
1271
1272
1273/* USB Stuff */
1274
1275static int stk_camera_probe(struct usb_interface *interface,
1276                const struct usb_device_id *id)
1277{
1278        struct v4l2_ctrl_handler *hdl;
1279        int err = 0;
1280        int i;
1281
1282        struct stk_camera *dev = NULL;
1283        struct usb_device *udev = interface_to_usbdev(interface);
1284        struct usb_host_interface *iface_desc;
1285        struct usb_endpoint_descriptor *endpoint;
1286
1287        dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1288        if (dev == NULL) {
1289                pr_err("Out of memory !\n");
1290                return -ENOMEM;
1291        }
1292        err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1293        if (err < 0) {
1294                dev_err(&udev->dev, "couldn't register v4l2_device\n");
1295                kfree(dev);
1296                return err;
1297        }
1298        hdl = &dev->hdl;
1299        v4l2_ctrl_handler_init(hdl, 3);
1300        v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1301                          V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1302        v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1303                          V4L2_CID_HFLIP, 0, 1, 1, 1);
1304        v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1305                          V4L2_CID_VFLIP, 0, 1, 1, 1);
1306        if (hdl->error) {
1307                err = hdl->error;
1308                dev_err(&udev->dev, "couldn't register control\n");
1309                goto error;
1310        }
1311        dev->v4l2_dev.ctrl_handler = hdl;
1312
1313        spin_lock_init(&dev->spinlock);
1314        mutex_init(&dev->lock);
1315        init_waitqueue_head(&dev->wait_frame);
1316        dev->first_init = 1; /* webcam LED management */
1317
1318        dev->udev = udev;
1319        dev->interface = interface;
1320        usb_get_intf(interface);
1321
1322        if (hflip != -1)
1323                dev->vsettings.hflip = hflip;
1324        else if (dmi_check_system(stk_upside_down_dmi_table))
1325                dev->vsettings.hflip = 1;
1326        else
1327                dev->vsettings.hflip = 0;
1328        if (vflip != -1)
1329                dev->vsettings.vflip = vflip;
1330        else if (dmi_check_system(stk_upside_down_dmi_table))
1331                dev->vsettings.vflip = 1;
1332        else
1333                dev->vsettings.vflip = 0;
1334        dev->n_sbufs = 0;
1335        set_present(dev);
1336
1337        /* Set up the endpoint information
1338         * use only the first isoc-in endpoint
1339         * for the current alternate setting */
1340        iface_desc = interface->cur_altsetting;
1341
1342        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1343                endpoint = &iface_desc->endpoint[i].desc;
1344
1345                if (!dev->isoc_ep
1346                        && usb_endpoint_is_isoc_in(endpoint)) {
1347                        /* we found an isoc in endpoint */
1348                        dev->isoc_ep = usb_endpoint_num(endpoint);
1349                        break;
1350                }
1351        }
1352        if (!dev->isoc_ep) {
1353                pr_err("Could not find isoc-in endpoint\n");
1354                err = -ENODEV;
1355                goto error;
1356        }
1357        dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1358        dev->vsettings.mode = MODE_VGA;
1359        dev->frame_size = 640 * 480 * 2;
1360
1361        INIT_LIST_HEAD(&dev->sio_avail);
1362        INIT_LIST_HEAD(&dev->sio_full);
1363
1364        usb_set_intfdata(interface, dev);
1365
1366        err = stk_register_video_device(dev);
1367        if (err)
1368                goto error;
1369
1370        return 0;
1371
1372error:
1373        v4l2_ctrl_handler_free(hdl);
1374        v4l2_device_unregister(&dev->v4l2_dev);
1375        kfree(dev);
1376        return err;
1377}
1378
1379static void stk_camera_disconnect(struct usb_interface *interface)
1380{
1381        struct stk_camera *dev = usb_get_intfdata(interface);
1382
1383        usb_set_intfdata(interface, NULL);
1384        unset_present(dev);
1385
1386        wake_up_interruptible(&dev->wait_frame);
1387
1388        pr_info("Syntek USB2.0 Camera release resources device %s\n",
1389                video_device_node_name(&dev->vdev));
1390
1391        video_unregister_device(&dev->vdev);
1392        v4l2_ctrl_handler_free(&dev->hdl);
1393        v4l2_device_unregister(&dev->v4l2_dev);
1394        kfree(dev);
1395}
1396
1397#ifdef CONFIG_PM
1398static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1399{
1400        struct stk_camera *dev = usb_get_intfdata(intf);
1401        if (is_streaming(dev)) {
1402                stk_stop_stream(dev);
1403                /* yes, this is ugly */
1404                set_streaming(dev);
1405        }
1406        return 0;
1407}
1408
1409static int stk_camera_resume(struct usb_interface *intf)
1410{
1411        struct stk_camera *dev = usb_get_intfdata(intf);
1412        if (!is_initialised(dev))
1413                return 0;
1414        unset_initialised(dev);
1415        stk_initialise(dev);
1416        stk_camera_write_reg(dev, 0x0, 0x49);
1417        stk_setup_format(dev);
1418        if (is_streaming(dev))
1419                stk_start_stream(dev);
1420        return 0;
1421}
1422#endif
1423
1424static struct usb_driver stk_camera_driver = {
1425        .name = "stkwebcam",
1426        .probe = stk_camera_probe,
1427        .disconnect = stk_camera_disconnect,
1428        .id_table = stkwebcam_table,
1429#ifdef CONFIG_PM
1430        .suspend = stk_camera_suspend,
1431        .resume = stk_camera_resume,
1432#endif
1433};
1434
1435module_usb_driver(stk_camera_driver);
1436