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