linux/drivers/media/usb/stkwebcam/stk-webcam.c
<<
>>
Prefs
   1/*
   2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
   3 *
   4 * Copyright (C) 2006 Nicolas VIVIEN
   5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
   6 *
   7 * Some parts are inspired from cafe_ccic.c
   8 * Copyright 2006-2007 Jonathan Corbet
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or
  13 * any later version.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18 * GNU General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23 */
  24
  25#include <linux/module.h>
  26#include <linux/init.h>
  27#include <linux/kernel.h>
  28#include <linux/errno.h>
  29#include <linux/slab.h>
  30
  31#include <linux/dmi.h>
  32#include <linux/usb.h>
  33#include <linux/mm.h>
  34#include <linux/vmalloc.h>
  35#include <linux/videodev2.h>
  36#include <media/v4l2-common.h>
  37#include <media/v4l2-ioctl.h>
  38#include <media/v4l2-event.h>
  39
  40#include "stk-webcam.h"
  41
  42
  43static int hflip = -1;
  44module_param(hflip, int, 0444);
  45MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
  46
  47static int vflip = -1;
  48module_param(vflip, int, 0444);
  49MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
  50
  51static int debug;
  52module_param(debug, int, 0444);
  53MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  54
  55MODULE_LICENSE("GPL");
  56MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  57MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  58
  59/* Some cameras have audio interfaces, we aren't interested in those */
  60static struct usb_device_id stkwebcam_table[] = {
  61        { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  62        { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  63        { }
  64};
  65MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  66
  67/*
  68 * The stk webcam laptop module is mounted upside down in some laptops :(
  69 *
  70 * Some background information (thanks to Hans de Goede for providing this):
  71 *
  72 * 1) Once upon a time the stkwebcam driver was written
  73 *
  74 * 2) The webcam in question was used mostly in Asus laptop models, including
  75 * the laptop of the original author of the driver, and in these models, in
  76 * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
  77 * they mounted the webcam-module the wrong way up. So the hflip and vflip
  78 * module options were given a default value of 1 (the correct value for
  79 * upside down mounted models)
  80 *
  81 * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
  82 * where the module was actually mounted the right way up, and thus showed
  83 * upside down under Linux. So now I was facing the choice of 2 options:
  84 *
  85 * a) Add a not-upside-down list to stkwebcam, which overrules the default.
  86 *
  87 * b) Do it like all the other drivers do, and make the default right for
  88 *    cams mounted the proper way and add an upside-down model list, with
  89 *    models where we need to flip-by-default.
  90 *
  91 * Despite knowing that going b) would cause a period of pain where we were
  92 * building the table I opted to go for option b), since a) is just too ugly,
  93 * and worse different from how every other driver does it leading to
  94 * confusion in the long run. This change was made in kernel 3.6.
  95 *
  96 * So for any user report about upside-down images since kernel 3.6 ask them
  97 * to provide the output of 'sudo dmidecode' so the laptop can be added in
  98 * the table below.
  99 */
 100static const struct dmi_system_id stk_upside_down_dmi_table[] = {
 101        {
 102                .ident = "ASUS G1",
 103                .matches = {
 104                        DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
 105                        DMI_MATCH(DMI_PRODUCT_NAME, "G1")
 106                }
 107        }, {
 108                .ident = "ASUS F3JC",
 109                .matches = {
 110                        DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
 111                        DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
 112                }
 113        },
 114        {}
 115};
 116
 117
 118/*
 119 * Basic stuff
 120 */
 121int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
 122{
 123        struct usb_device *udev = dev->udev;
 124        int ret;
 125
 126        ret =  usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 127                        0x01,
 128                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 129                        value,
 130                        index,
 131                        NULL,
 132                        0,
 133                        500);
 134        if (ret < 0)
 135                return ret;
 136        else
 137                return 0;
 138}
 139
 140int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
 141{
 142        struct usb_device *udev = dev->udev;
 143        int ret;
 144
 145        ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
 146                        0x00,
 147                        USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 148                        0x00,
 149                        index,
 150                        (u8 *) value,
 151                        sizeof(u8),
 152                        500);
 153        if (ret < 0)
 154                return ret;
 155        else
 156                return 0;
 157}
 158
 159static int stk_start_stream(struct stk_camera *dev)
 160{
 161        int value;
 162        int i, ret;
 163        int value_116, value_117;
 164
 165        if (!is_present(dev))
 166                return -ENODEV;
 167        if (!is_memallocd(dev) || !is_initialised(dev)) {
 168                STK_ERROR("FIXME: Buffers are not allocated\n");
 169                return -EFAULT;
 170        }
 171        ret = usb_set_interface(dev->udev, 0, 5);
 172
 173        if (ret < 0)
 174                STK_ERROR("usb_set_interface failed !\n");
 175        if (stk_sensor_wakeup(dev))
 176                STK_ERROR("error awaking the sensor\n");
 177
 178        stk_camera_read_reg(dev, 0x0116, &value_116);
 179        stk_camera_read_reg(dev, 0x0117, &value_117);
 180
 181        stk_camera_write_reg(dev, 0x0116, 0x0000);
 182        stk_camera_write_reg(dev, 0x0117, 0x0000);
 183
 184        stk_camera_read_reg(dev, 0x0100, &value);
 185        stk_camera_write_reg(dev, 0x0100, value | 0x80);
 186
 187        stk_camera_write_reg(dev, 0x0116, value_116);
 188        stk_camera_write_reg(dev, 0x0117, value_117);
 189        for (i = 0; i < MAX_ISO_BUFS; i++) {
 190                if (dev->isobufs[i].urb) {
 191                        ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
 192                        atomic_inc(&dev->urbs_used);
 193                        if (ret)
 194                                return ret;
 195                }
 196        }
 197        set_streaming(dev);
 198        return 0;
 199}
 200
 201static int stk_stop_stream(struct stk_camera *dev)
 202{
 203        int value;
 204        int i;
 205        if (is_present(dev)) {
 206                stk_camera_read_reg(dev, 0x0100, &value);
 207                stk_camera_write_reg(dev, 0x0100, value & ~0x80);
 208                if (dev->isobufs != NULL) {
 209                        for (i = 0; i < MAX_ISO_BUFS; i++) {
 210                                if (dev->isobufs[i].urb)
 211                                        usb_kill_urb(dev->isobufs[i].urb);
 212                        }
 213                }
 214                unset_streaming(dev);
 215
 216                if (usb_set_interface(dev->udev, 0, 0))
 217                        STK_ERROR("usb_set_interface failed !\n");
 218                if (stk_sensor_sleep(dev))
 219                        STK_ERROR("error suspending the sensor\n");
 220        }
 221        return 0;
 222}
 223
 224/*
 225 * This seems to be the shortest init sequence we
 226 * must do in order to find the sensor
 227 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
 228 * is also reset. Maybe powers down it?
 229 * Rest of values don't make a difference
 230 */
 231
 232static struct regval stk1125_initvals[] = {
 233        /*TODO: What means this sequence? */
 234        {0x0000, 0x24},
 235        {0x0100, 0x21},
 236        {0x0002, 0x68},
 237        {0x0003, 0x80},
 238        {0x0005, 0x00},
 239        {0x0007, 0x03},
 240        {0x000d, 0x00},
 241        {0x000f, 0x02},
 242        {0x0300, 0x12},
 243        {0x0350, 0x41},
 244        {0x0351, 0x00},
 245        {0x0352, 0x00},
 246        {0x0353, 0x00},
 247        {0x0018, 0x10},
 248        {0x0019, 0x00},
 249        {0x001b, 0x0e},
 250        {0x001c, 0x46},
 251        {0x0300, 0x80},
 252        {0x001a, 0x04},
 253        {0x0110, 0x00},
 254        {0x0111, 0x00},
 255        {0x0112, 0x00},
 256        {0x0113, 0x00},
 257
 258        {0xffff, 0xff},
 259};
 260
 261
 262static int stk_initialise(struct stk_camera *dev)
 263{
 264        struct regval *rv;
 265        int ret;
 266        if (!is_present(dev))
 267                return -ENODEV;
 268        if (is_initialised(dev))
 269                return 0;
 270        rv = stk1125_initvals;
 271        while (rv->reg != 0xffff) {
 272                ret = stk_camera_write_reg(dev, rv->reg, rv->val);
 273                if (ret)
 274                        return ret;
 275                rv++;
 276        }
 277        if (stk_sensor_init(dev) == 0) {
 278                set_initialised(dev);
 279                return 0;
 280        } else
 281                return -1;
 282}
 283
 284/* *********************************************** */
 285/*
 286 * This function is called as an URB transfert is complete (Isochronous pipe).
 287 * So, the traitement is done in interrupt time, so it has be fast, not crash,
 288 * and not stall. Neat.
 289 */
 290static void stk_isoc_handler(struct urb *urb)
 291{
 292        int i;
 293        int ret;
 294        int framelen;
 295        unsigned long flags;
 296
 297        unsigned char *fill = NULL;
 298        unsigned char *iso_buf = NULL;
 299
 300        struct stk_camera *dev;
 301        struct stk_sio_buffer *fb;
 302
 303        dev = (struct stk_camera *) urb->context;
 304
 305        if (dev == NULL) {
 306                STK_ERROR("isoc_handler called with NULL device !\n");
 307                return;
 308        }
 309
 310        if (urb->status == -ENOENT || urb->status == -ECONNRESET
 311                || urb->status == -ESHUTDOWN) {
 312                atomic_dec(&dev->urbs_used);
 313                return;
 314        }
 315
 316        spin_lock_irqsave(&dev->spinlock, flags);
 317
 318        if (urb->status != -EINPROGRESS && urb->status != 0) {
 319                STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
 320                goto resubmit;
 321        }
 322
 323        if (list_empty(&dev->sio_avail)) {
 324                /*FIXME Stop streaming after a while */
 325                (void) (printk_ratelimit() &&
 326                STK_ERROR("isoc_handler without available buffer!\n"));
 327                goto resubmit;
 328        }
 329        fb = list_first_entry(&dev->sio_avail,
 330                        struct stk_sio_buffer, list);
 331        fill = fb->buffer + fb->v4lbuf.bytesused;
 332
 333        for (i = 0; i < urb->number_of_packets; i++) {
 334                if (urb->iso_frame_desc[i].status != 0) {
 335                        if (urb->iso_frame_desc[i].status != -EXDEV)
 336                                STK_ERROR("Frame %d has error %d\n", i,
 337                                        urb->iso_frame_desc[i].status);
 338                        continue;
 339                }
 340                framelen = urb->iso_frame_desc[i].actual_length;
 341                iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
 342
 343                if (framelen <= 4)
 344                        continue; /* no data */
 345
 346                /*
 347                 * we found something informational from there
 348                 * the isoc frames have to type of headers
 349                 * type1: 00 xx 00 00 or 20 xx 00 00
 350                 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
 351                 * xx is a sequencer which has never been seen over 0x3f
 352                 * imho data written down looks like bayer, i see similarities
 353                 * after every 640 bytes
 354                 */
 355                if (*iso_buf & 0x80) {
 356                        framelen -= 8;
 357                        iso_buf += 8;
 358                        /* This marks a new frame */
 359                        if (fb->v4lbuf.bytesused != 0
 360                                && fb->v4lbuf.bytesused != dev->frame_size) {
 361                                (void) (printk_ratelimit() &&
 362                                STK_ERROR("frame %d, "
 363                                        "bytesused=%d, skipping\n",
 364                                        i, fb->v4lbuf.bytesused));
 365                                fb->v4lbuf.bytesused = 0;
 366                                fill = fb->buffer;
 367                        } else if (fb->v4lbuf.bytesused == dev->frame_size) {
 368                                if (list_is_singular(&dev->sio_avail)) {
 369                                        /* Always reuse the last buffer */
 370                                        fb->v4lbuf.bytesused = 0;
 371                                        fill = fb->buffer;
 372                                } else {
 373                                        list_move_tail(dev->sio_avail.next,
 374                                                &dev->sio_full);
 375                                        wake_up(&dev->wait_frame);
 376                                        fb = list_first_entry(&dev->sio_avail,
 377                                                struct stk_sio_buffer, list);
 378                                        fb->v4lbuf.bytesused = 0;
 379                                        fill = fb->buffer;
 380                                }
 381                        }
 382                } else {
 383                        framelen -= 4;
 384                        iso_buf += 4;
 385                }
 386
 387                /* Our buffer is full !!! */
 388                if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
 389                        (void) (printk_ratelimit() &&
 390                        STK_ERROR("Frame buffer overflow, lost sync\n"));
 391                        /*FIXME Do something here? */
 392                        continue;
 393                }
 394                spin_unlock_irqrestore(&dev->spinlock, flags);
 395                memcpy(fill, iso_buf, framelen);
 396                spin_lock_irqsave(&dev->spinlock, flags);
 397                fill += framelen;
 398
 399                /* New size of our buffer */
 400                fb->v4lbuf.bytesused += framelen;
 401        }
 402
 403resubmit:
 404        spin_unlock_irqrestore(&dev->spinlock, flags);
 405        urb->dev = dev->udev;
 406        ret = usb_submit_urb(urb, GFP_ATOMIC);
 407        if (ret != 0) {
 408                STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
 409                        ret);
 410        }
 411}
 412
 413/* -------------------------------------------- */
 414
 415static int stk_prepare_iso(struct stk_camera *dev)
 416{
 417        void *kbuf;
 418        int i, j;
 419        struct urb *urb;
 420        struct usb_device *udev;
 421
 422        if (dev == NULL)
 423                return -ENXIO;
 424        udev = dev->udev;
 425
 426        if (dev->isobufs)
 427                STK_ERROR("isobufs already allocated. Bad\n");
 428        else
 429                dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
 430                                       GFP_KERNEL);
 431        if (dev->isobufs == NULL) {
 432                STK_ERROR("Unable to allocate iso buffers\n");
 433                return -ENOMEM;
 434        }
 435        for (i = 0; i < MAX_ISO_BUFS; i++) {
 436                if (dev->isobufs[i].data == NULL) {
 437                        kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
 438                        if (kbuf == NULL) {
 439                                STK_ERROR("Failed to allocate iso buffer %d\n",
 440                                        i);
 441                                goto isobufs_out;
 442                        }
 443                        dev->isobufs[i].data = kbuf;
 444                } else
 445                        STK_ERROR("isobuf data already allocated\n");
 446                if (dev->isobufs[i].urb == NULL) {
 447                        urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
 448                        if (urb == NULL) {
 449                                STK_ERROR("Failed to allocate URB %d\n", i);
 450                                goto isobufs_out;
 451                        }
 452                        dev->isobufs[i].urb = urb;
 453                } else {
 454                        STK_ERROR("Killing URB\n");
 455                        usb_kill_urb(dev->isobufs[i].urb);
 456                        urb = dev->isobufs[i].urb;
 457                }
 458                urb->interval = 1;
 459                urb->dev = udev;
 460                urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
 461                urb->transfer_flags = URB_ISO_ASAP;
 462                urb->transfer_buffer = dev->isobufs[i].data;
 463                urb->transfer_buffer_length = ISO_BUFFER_SIZE;
 464                urb->complete = stk_isoc_handler;
 465                urb->context = dev;
 466                urb->start_frame = 0;
 467                urb->number_of_packets = ISO_FRAMES_PER_DESC;
 468
 469                for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
 470                        urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
 471                        urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
 472                }
 473        }
 474        set_memallocd(dev);
 475        return 0;
 476
 477isobufs_out:
 478        for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
 479                kfree(dev->isobufs[i].data);
 480        for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
 481                usb_free_urb(dev->isobufs[i].urb);
 482        kfree(dev->isobufs);
 483        dev->isobufs = NULL;
 484        return -ENOMEM;
 485}
 486
 487static void stk_clean_iso(struct stk_camera *dev)
 488{
 489        int i;
 490
 491        if (dev == NULL || dev->isobufs == NULL)
 492                return;
 493
 494        for (i = 0; i < MAX_ISO_BUFS; i++) {
 495                struct urb *urb;
 496
 497                urb = dev->isobufs[i].urb;
 498                if (urb) {
 499                        if (atomic_read(&dev->urbs_used) && is_present(dev))
 500                                usb_kill_urb(urb);
 501                        usb_free_urb(urb);
 502                }
 503                kfree(dev->isobufs[i].data);
 504        }
 505        kfree(dev->isobufs);
 506        dev->isobufs = NULL;
 507        unset_memallocd(dev);
 508}
 509
 510static int stk_setup_siobuf(struct stk_camera *dev, int index)
 511{
 512        struct stk_sio_buffer *buf = dev->sio_bufs + index;
 513        INIT_LIST_HEAD(&buf->list);
 514        buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
 515        buf->buffer = vmalloc_user(buf->v4lbuf.length);
 516        if (buf->buffer == NULL)
 517                return -ENOMEM;
 518        buf->mapcount = 0;
 519        buf->dev = dev;
 520        buf->v4lbuf.index = index;
 521        buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 522        buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 523        buf->v4lbuf.field = V4L2_FIELD_NONE;
 524        buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
 525        buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
 526        return 0;
 527}
 528
 529static int stk_free_sio_buffers(struct stk_camera *dev)
 530{
 531        int i;
 532        int nbufs;
 533        unsigned long flags;
 534        if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
 535                return 0;
 536        /*
 537        * If any buffers are mapped, we cannot free them at all.
 538        */
 539        for (i = 0; i < dev->n_sbufs; i++) {
 540                if (dev->sio_bufs[i].mapcount > 0)
 541                        return -EBUSY;
 542        }
 543        /*
 544        * OK, let's do it.
 545        */
 546        spin_lock_irqsave(&dev->spinlock, flags);
 547        INIT_LIST_HEAD(&dev->sio_avail);
 548        INIT_LIST_HEAD(&dev->sio_full);
 549        nbufs = dev->n_sbufs;
 550        dev->n_sbufs = 0;
 551        spin_unlock_irqrestore(&dev->spinlock, flags);
 552        for (i = 0; i < nbufs; i++) {
 553                if (dev->sio_bufs[i].buffer != NULL)
 554                        vfree(dev->sio_bufs[i].buffer);
 555        }
 556        kfree(dev->sio_bufs);
 557        dev->sio_bufs = NULL;
 558        return 0;
 559}
 560
 561static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
 562{
 563        int i;
 564        if (dev->sio_bufs != NULL)
 565                STK_ERROR("sio_bufs already allocated\n");
 566        else {
 567                dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
 568                                GFP_KERNEL);
 569                if (dev->sio_bufs == NULL)
 570                        return -ENOMEM;
 571                for (i = 0; i < n_sbufs; i++) {
 572                        if (stk_setup_siobuf(dev, i))
 573                                return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
 574                        dev->n_sbufs = i+1;
 575                }
 576        }
 577        return 0;
 578}
 579
 580static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
 581{
 582        int err;
 583        err = stk_prepare_iso(dev);
 584        if (err) {
 585                stk_clean_iso(dev);
 586                return err;
 587        }
 588        err = stk_prepare_sio_buffers(dev, n_sbufs);
 589        if (err) {
 590                stk_free_sio_buffers(dev);
 591                return err;
 592        }
 593        return 0;
 594}
 595
 596static void stk_free_buffers(struct stk_camera *dev)
 597{
 598        stk_clean_iso(dev);
 599        stk_free_sio_buffers(dev);
 600}
 601/* -------------------------------------------- */
 602
 603/* v4l file operations */
 604
 605static int v4l_stk_open(struct file *fp)
 606{
 607        struct stk_camera *dev = video_drvdata(fp);
 608        int err;
 609
 610        if (dev == NULL || !is_present(dev))
 611                return -ENXIO;
 612
 613        if (mutex_lock_interruptible(&dev->lock))
 614                return -ERESTARTSYS;
 615        if (!dev->first_init)
 616                stk_camera_write_reg(dev, 0x0, 0x24);
 617        else
 618                dev->first_init = 0;
 619
 620        err = v4l2_fh_open(fp);
 621        if (!err)
 622                usb_autopm_get_interface(dev->interface);
 623        mutex_unlock(&dev->lock);
 624        return err;
 625}
 626
 627static int v4l_stk_release(struct file *fp)
 628{
 629        struct stk_camera *dev = video_drvdata(fp);
 630
 631        mutex_lock(&dev->lock);
 632        if (dev->owner == fp) {
 633                stk_stop_stream(dev);
 634                stk_free_buffers(dev);
 635                stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
 636                unset_initialised(dev);
 637                dev->owner = NULL;
 638        }
 639
 640        if (is_present(dev))
 641                usb_autopm_put_interface(dev->interface);
 642        mutex_unlock(&dev->lock);
 643        return v4l2_fh_release(fp);
 644}
 645
 646static ssize_t stk_read(struct file *fp, char __user *buf,
 647                size_t count, loff_t *f_pos)
 648{
 649        int i;
 650        int ret;
 651        unsigned long flags;
 652        struct stk_sio_buffer *sbuf;
 653        struct stk_camera *dev = video_drvdata(fp);
 654
 655        if (!is_present(dev))
 656                return -EIO;
 657        if (dev->owner && (!dev->reading || dev->owner != fp))
 658                return -EBUSY;
 659        dev->owner = fp;
 660        if (!is_streaming(dev)) {
 661                if (stk_initialise(dev)
 662                        || stk_allocate_buffers(dev, 3)
 663                        || stk_start_stream(dev))
 664                        return -ENOMEM;
 665                dev->reading = 1;
 666                spin_lock_irqsave(&dev->spinlock, flags);
 667                for (i = 0; i < dev->n_sbufs; i++) {
 668                        list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
 669                        dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
 670                }
 671                spin_unlock_irqrestore(&dev->spinlock, flags);
 672        }
 673        if (*f_pos == 0) {
 674                if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
 675                        return -EWOULDBLOCK;
 676                ret = wait_event_interruptible(dev->wait_frame,
 677                        !list_empty(&dev->sio_full) || !is_present(dev));
 678                if (ret)
 679                        return ret;
 680                if (!is_present(dev))
 681                        return -EIO;
 682        }
 683        if (count + *f_pos > dev->frame_size)
 684                count = dev->frame_size - *f_pos;
 685        spin_lock_irqsave(&dev->spinlock, flags);
 686        if (list_empty(&dev->sio_full)) {
 687                spin_unlock_irqrestore(&dev->spinlock, flags);
 688                STK_ERROR("BUG: No siobufs ready\n");
 689                return 0;
 690        }
 691        sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
 692        spin_unlock_irqrestore(&dev->spinlock, flags);
 693
 694        if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
 695                return -EFAULT;
 696
 697        *f_pos += count;
 698
 699        if (*f_pos >= dev->frame_size) {
 700                *f_pos = 0;
 701                spin_lock_irqsave(&dev->spinlock, flags);
 702                list_move_tail(&sbuf->list, &dev->sio_avail);
 703                spin_unlock_irqrestore(&dev->spinlock, flags);
 704        }
 705        return count;
 706}
 707
 708static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
 709                size_t count, loff_t *f_pos)
 710{
 711        struct stk_camera *dev = video_drvdata(fp);
 712        int ret;
 713
 714        if (mutex_lock_interruptible(&dev->lock))
 715                return -ERESTARTSYS;
 716        ret = stk_read(fp, buf, count, f_pos);
 717        mutex_unlock(&dev->lock);
 718        return ret;
 719}
 720
 721static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
 722{
 723        struct stk_camera *dev = video_drvdata(fp);
 724        unsigned res = v4l2_ctrl_poll(fp, wait);
 725
 726        poll_wait(fp, &dev->wait_frame, wait);
 727
 728        if (!is_present(dev))
 729                return POLLERR;
 730
 731        if (!list_empty(&dev->sio_full))
 732                return res | POLLIN | POLLRDNORM;
 733
 734        return res;
 735}
 736
 737
 738static void stk_v4l_vm_open(struct vm_area_struct *vma)
 739{
 740        struct stk_sio_buffer *sbuf = vma->vm_private_data;
 741        sbuf->mapcount++;
 742}
 743static void stk_v4l_vm_close(struct vm_area_struct *vma)
 744{
 745        struct stk_sio_buffer *sbuf = vma->vm_private_data;
 746        sbuf->mapcount--;
 747        if (sbuf->mapcount == 0)
 748                sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
 749}
 750static const struct vm_operations_struct stk_v4l_vm_ops = {
 751        .open = stk_v4l_vm_open,
 752        .close = stk_v4l_vm_close
 753};
 754
 755static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
 756{
 757        unsigned int i;
 758        int ret;
 759        unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
 760        struct stk_camera *dev = video_drvdata(fp);
 761        struct stk_sio_buffer *sbuf = NULL;
 762
 763        if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
 764                return -EINVAL;
 765
 766        for (i = 0; i < dev->n_sbufs; i++) {
 767                if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
 768                        sbuf = dev->sio_bufs + i;
 769                        break;
 770                }
 771        }
 772        if (sbuf == NULL)
 773                return -EINVAL;
 774        ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
 775        if (ret)
 776                return ret;
 777        vma->vm_flags |= VM_DONTEXPAND;
 778        vma->vm_private_data = sbuf;
 779        vma->vm_ops = &stk_v4l_vm_ops;
 780        sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
 781        stk_v4l_vm_open(vma);
 782        return 0;
 783}
 784
 785/* v4l ioctl handlers */
 786
 787static int stk_vidioc_querycap(struct file *filp,
 788                void *priv, struct v4l2_capability *cap)
 789{
 790        struct stk_camera *dev = video_drvdata(filp);
 791
 792        strcpy(cap->driver, "stk");
 793        strcpy(cap->card, "stk");
 794        usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
 795
 796        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
 797                | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
 798        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 799        return 0;
 800}
 801
 802static int stk_vidioc_enum_input(struct file *filp,
 803                void *priv, struct v4l2_input *input)
 804{
 805        if (input->index != 0)
 806                return -EINVAL;
 807
 808        strcpy(input->name, "Syntek USB Camera");
 809        input->type = V4L2_INPUT_TYPE_CAMERA;
 810        return 0;
 811}
 812
 813
 814static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
 815{
 816        *i = 0;
 817        return 0;
 818}
 819
 820static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
 821{
 822        return i ? -EINVAL : 0;
 823}
 824
 825static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
 826{
 827        struct stk_camera *dev =
 828                container_of(ctrl->handler, struct stk_camera, hdl);
 829
 830        switch (ctrl->id) {
 831        case V4L2_CID_BRIGHTNESS:
 832                return stk_sensor_set_brightness(dev, ctrl->val);
 833        case V4L2_CID_HFLIP:
 834                if (dmi_check_system(stk_upside_down_dmi_table))
 835                        dev->vsettings.hflip = !ctrl->val;
 836                else
 837                        dev->vsettings.hflip = ctrl->val;
 838                return 0;
 839        case V4L2_CID_VFLIP:
 840                if (dmi_check_system(stk_upside_down_dmi_table))
 841                        dev->vsettings.vflip = !ctrl->val;
 842                else
 843                        dev->vsettings.vflip = ctrl->val;
 844                return 0;
 845        default:
 846                return -EINVAL;
 847        }
 848        return 0;
 849}
 850
 851
 852static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
 853                void *priv, struct v4l2_fmtdesc *fmtd)
 854{
 855        switch (fmtd->index) {
 856        case 0:
 857                fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
 858                strcpy(fmtd->description, "r5g6b5");
 859                break;
 860        case 1:
 861                fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
 862                strcpy(fmtd->description, "r5g6b5BE");
 863                break;
 864        case 2:
 865                fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
 866                strcpy(fmtd->description, "yuv4:2:2");
 867                break;
 868        case 3:
 869                fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
 870                strcpy(fmtd->description, "Raw bayer");
 871                break;
 872        case 4:
 873                fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
 874                strcpy(fmtd->description, "yuv4:2:2");
 875                break;
 876        default:
 877                return -EINVAL;
 878        }
 879        return 0;
 880}
 881
 882static struct stk_size {
 883        unsigned w;
 884        unsigned h;
 885        enum stk_mode m;
 886} stk_sizes[] = {
 887        { .w = 1280, .h = 1024, .m = MODE_SXGA, },
 888        { .w = 640,  .h = 480,  .m = MODE_VGA,  },
 889        { .w = 352,  .h = 288,  .m = MODE_CIF,  },
 890        { .w = 320,  .h = 240,  .m = MODE_QVGA, },
 891        { .w = 176,  .h = 144,  .m = MODE_QCIF, },
 892};
 893
 894static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
 895                void *priv, struct v4l2_format *f)
 896{
 897        struct v4l2_pix_format *pix_format = &f->fmt.pix;
 898        struct stk_camera *dev = video_drvdata(filp);
 899        int i;
 900
 901        for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
 902                        stk_sizes[i].m != dev->vsettings.mode; i++)
 903                ;
 904        if (i == ARRAY_SIZE(stk_sizes)) {
 905                STK_ERROR("ERROR: mode invalid\n");
 906                return -EINVAL;
 907        }
 908        pix_format->width = stk_sizes[i].w;
 909        pix_format->height = stk_sizes[i].h;
 910        pix_format->field = V4L2_FIELD_NONE;
 911        pix_format->colorspace = V4L2_COLORSPACE_SRGB;
 912        pix_format->pixelformat = dev->vsettings.palette;
 913        if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
 914                pix_format->bytesperline = pix_format->width;
 915        else
 916                pix_format->bytesperline = 2 * pix_format->width;
 917        pix_format->sizeimage = pix_format->bytesperline
 918                                * pix_format->height;
 919        pix_format->priv = 0;
 920        return 0;
 921}
 922
 923static int stk_try_fmt_vid_cap(struct file *filp,
 924                struct v4l2_format *fmtd, int *idx)
 925{
 926        int i;
 927        switch (fmtd->fmt.pix.pixelformat) {
 928        case V4L2_PIX_FMT_RGB565:
 929        case V4L2_PIX_FMT_RGB565X:
 930        case V4L2_PIX_FMT_UYVY:
 931        case V4L2_PIX_FMT_YUYV:
 932        case V4L2_PIX_FMT_SBGGR8:
 933                break;
 934        default:
 935                return -EINVAL;
 936        }
 937        for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
 938                if (fmtd->fmt.pix.width > stk_sizes[i].w)
 939                        break;
 940        }
 941        if (i == ARRAY_SIZE(stk_sizes)
 942                || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
 943                        < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
 944                fmtd->fmt.pix.height = stk_sizes[i-1].h;
 945                fmtd->fmt.pix.width = stk_sizes[i-1].w;
 946                if (idx)
 947                        *idx = i - 1;
 948        } else {
 949                fmtd->fmt.pix.height = stk_sizes[i].h;
 950                fmtd->fmt.pix.width = stk_sizes[i].w;
 951                if (idx)
 952                        *idx = i;
 953        }
 954
 955        fmtd->fmt.pix.field = V4L2_FIELD_NONE;
 956        fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
 957        if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
 958                fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
 959        else
 960                fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
 961        fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
 962                * fmtd->fmt.pix.height;
 963        fmtd->fmt.pix.priv = 0;
 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                STK_ERROR("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 heigth 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        v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
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 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                STK_ERROR("We are leaking memory\n");
1242        usb_put_intf(dev->interface);
1243        kfree(dev);
1244}
1245
1246static struct video_device stk_v4l_data = {
1247        .name = "stkwebcam",
1248        .fops = &v4l_stk_fops,
1249        .ioctl_ops = &v4l_stk_ioctl_ops,
1250        .release = stk_v4l_dev_release,
1251};
1252
1253
1254static int stk_register_video_device(struct stk_camera *dev)
1255{
1256        int err;
1257
1258        dev->vdev = stk_v4l_data;
1259        dev->vdev.lock = &dev->lock;
1260        dev->vdev.debug = debug;
1261        dev->vdev.v4l2_dev = &dev->v4l2_dev;
1262        set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
1263        video_set_drvdata(&dev->vdev, dev);
1264        err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1265        if (err)
1266                STK_ERROR("v4l registration failed\n");
1267        else
1268                STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1269                         video_device_node_name(&dev->vdev));
1270        return err;
1271}
1272
1273
1274/* USB Stuff */
1275
1276static int stk_camera_probe(struct usb_interface *interface,
1277                const struct usb_device_id *id)
1278{
1279        struct v4l2_ctrl_handler *hdl;
1280        int err = 0;
1281        int i;
1282
1283        struct stk_camera *dev = NULL;
1284        struct usb_device *udev = interface_to_usbdev(interface);
1285        struct usb_host_interface *iface_desc;
1286        struct usb_endpoint_descriptor *endpoint;
1287
1288        dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1289        if (dev == NULL) {
1290                STK_ERROR("Out of memory !\n");
1291                return -ENOMEM;
1292        }
1293        err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1294        if (err < 0) {
1295                dev_err(&udev->dev, "couldn't register v4l2_device\n");
1296                kfree(dev);
1297                return err;
1298        }
1299        hdl = &dev->hdl;
1300        v4l2_ctrl_handler_init(hdl, 3);
1301        v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1302                          V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1303        v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1304                          V4L2_CID_HFLIP, 0, 1, 1, 1);
1305        v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1306                          V4L2_CID_VFLIP, 0, 1, 1, 1);
1307        if (hdl->error) {
1308                err = hdl->error;
1309                dev_err(&udev->dev, "couldn't register control\n");
1310                goto error;
1311        }
1312        dev->v4l2_dev.ctrl_handler = hdl;
1313
1314        spin_lock_init(&dev->spinlock);
1315        mutex_init(&dev->lock);
1316        init_waitqueue_head(&dev->wait_frame);
1317        dev->first_init = 1; /* webcam LED management */
1318
1319        dev->udev = udev;
1320        dev->interface = interface;
1321        usb_get_intf(interface);
1322
1323        if (hflip != -1)
1324                dev->vsettings.hflip = hflip;
1325        else if (dmi_check_system(stk_upside_down_dmi_table))
1326                dev->vsettings.hflip = 1;
1327        else
1328                dev->vsettings.hflip = 0;
1329        if (vflip != -1)
1330                dev->vsettings.vflip = vflip;
1331        else if (dmi_check_system(stk_upside_down_dmi_table))
1332                dev->vsettings.vflip = 1;
1333        else
1334                dev->vsettings.vflip = 0;
1335        dev->n_sbufs = 0;
1336        set_present(dev);
1337
1338        /* Set up the endpoint information
1339         * use only the first isoc-in endpoint
1340         * for the current alternate setting */
1341        iface_desc = interface->cur_altsetting;
1342
1343        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1344                endpoint = &iface_desc->endpoint[i].desc;
1345
1346                if (!dev->isoc_ep
1347                        && usb_endpoint_is_isoc_in(endpoint)) {
1348                        /* we found an isoc in endpoint */
1349                        dev->isoc_ep = usb_endpoint_num(endpoint);
1350                        break;
1351                }
1352        }
1353        if (!dev->isoc_ep) {
1354                STK_ERROR("Could not find isoc-in endpoint");
1355                err = -ENODEV;
1356                goto error;
1357        }
1358        dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1359        dev->vsettings.mode = MODE_VGA;
1360        dev->frame_size = 640 * 480 * 2;
1361
1362        INIT_LIST_HEAD(&dev->sio_avail);
1363        INIT_LIST_HEAD(&dev->sio_full);
1364
1365        usb_set_intfdata(interface, dev);
1366
1367        err = stk_register_video_device(dev);
1368        if (err)
1369                goto error;
1370
1371        return 0;
1372
1373error:
1374        v4l2_ctrl_handler_free(hdl);
1375        v4l2_device_unregister(&dev->v4l2_dev);
1376        kfree(dev);
1377        return err;
1378}
1379
1380static void stk_camera_disconnect(struct usb_interface *interface)
1381{
1382        struct stk_camera *dev = usb_get_intfdata(interface);
1383
1384        usb_set_intfdata(interface, NULL);
1385        unset_present(dev);
1386
1387        wake_up_interruptible(&dev->wait_frame);
1388
1389        STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1390                 video_device_node_name(&dev->vdev));
1391
1392        video_unregister_device(&dev->vdev);
1393        v4l2_ctrl_handler_free(&dev->hdl);
1394        v4l2_device_unregister(&dev->v4l2_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