akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 2 | em2820-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 3 | |
| 4 | Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com> |
| 5 | Ludovico Cavedon <cavedon@sssup.it> |
| 6 | Mauro Carvalho Chehab <mchehab@brturbo.com.br> |
| 7 | |
| 8 | Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de> |
| 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 | (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/usb.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 30 | #include <linux/i2c.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 31 | #include <linux/video_decoder.h> |
| 32 | |
| 33 | #include "em2820.h" |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 34 | #include <media/tuner.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 35 | |
| 36 | #define DRIVER_AUTHOR "Markus Rechberger <mrechberger@gmail.com>, " \ |
| 37 | "Ludovico Cavedon <cavedon@sssup.it>, " \ |
| 38 | "Mauro Carvalho Chehab <mchehab@brturbo.com.br>" |
| 39 | |
| 40 | #define DRIVER_NAME "em2820" |
| 41 | #define DRIVER_DESC "Empia em2820 based USB video device driver" |
| 42 | #define EM2820_VERSION_CODE KERNEL_VERSION(0, 0, 1) |
| 43 | |
| 44 | #define em2820_videodbg(fmt, arg...) do {\ |
| 45 | if (video_debug) \ |
| 46 | printk(KERN_INFO "%s %s :"fmt, \ |
| 47 | dev->name, __FUNCTION__ , ##arg); } while (0) |
| 48 | |
| 49 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 50 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 53 | static unsigned int card[] = {[0 ... (EM2820_MAXBOARDS - 1)] = UNSET }; |
| 54 | |
| 55 | module_param_array(card, int, NULL, 0444); |
| 56 | MODULE_PARM_DESC(card,"card type"); |
| 57 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 58 | static int tuner = -1; |
| 59 | module_param(tuner, int, 0444); |
| 60 | MODULE_PARM_DESC(tuner, "tuner type"); |
| 61 | |
| 62 | static unsigned int video_debug = 0; |
| 63 | module_param(video_debug,int,0644); |
| 64 | MODULE_PARM_DESC(video_debug,"enable debug messages [video]"); |
| 65 | |
| 66 | /* supported tv norms */ |
| 67 | static struct em2820_tvnorm tvnorms[] = { |
| 68 | { |
| 69 | .name = "PAL", |
| 70 | .id = V4L2_STD_PAL, |
| 71 | .mode = VIDEO_MODE_PAL, |
| 72 | }, { |
| 73 | .name = "NTSC", |
| 74 | .id = V4L2_STD_NTSC, |
| 75 | .mode = VIDEO_MODE_NTSC, |
| 76 | }, { |
| 77 | .name = "SECAM", |
| 78 | .id = V4L2_STD_SECAM, |
| 79 | .mode = VIDEO_MODE_SECAM, |
| 80 | }, { |
| 81 | .name = "PAL-M", |
| 82 | .id = V4L2_STD_PAL_M, |
| 83 | .mode = VIDEO_MODE_PAL, |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | #define TVNORMS ARRAY_SIZE(tvnorms) |
| 88 | |
| 89 | /* supported controls */ |
| 90 | static struct v4l2_queryctrl em2820_qctrl[] = { |
| 91 | { |
| 92 | .id = V4L2_CID_BRIGHTNESS, |
| 93 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 94 | .name = "Brightness", |
| 95 | .minimum = -128, |
| 96 | .maximum = 127, |
| 97 | .step = 1, |
| 98 | .default_value = 0, |
| 99 | .flags = 0, |
| 100 | },{ |
| 101 | .id = V4L2_CID_CONTRAST, |
| 102 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 103 | .name = "Contrast", |
| 104 | .minimum = 0x0, |
| 105 | .maximum = 0x1f, |
| 106 | .step = 0x1, |
| 107 | .default_value = 0x10, |
| 108 | .flags = 0, |
| 109 | },{ |
| 110 | .id = V4L2_CID_SATURATION, |
| 111 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 112 | .name = "Saturation", |
| 113 | .minimum = 0x0, |
| 114 | .maximum = 0x1f, |
| 115 | .step = 0x1, |
| 116 | .default_value = 0x10, |
| 117 | .flags = 0, |
| 118 | },{ |
| 119 | .id = V4L2_CID_AUDIO_VOLUME, |
| 120 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 121 | .name = "Volume", |
| 122 | .minimum = 0x0, |
| 123 | .maximum = 0x1f, |
| 124 | .step = 0x1, |
| 125 | .default_value = 0x1f, |
| 126 | .flags = 0, |
| 127 | },{ |
| 128 | .id = V4L2_CID_AUDIO_MUTE, |
| 129 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 130 | .name = "Mute", |
| 131 | .minimum = 0, |
| 132 | .maximum = 1, |
| 133 | .step = 1, |
| 134 | .default_value = 1, |
| 135 | .flags = 0, |
| 136 | },{ |
| 137 | .id = V4L2_CID_RED_BALANCE, |
| 138 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 139 | .name = "Red chroma balance", |
| 140 | .minimum = -128, |
| 141 | .maximum = 127, |
| 142 | .step = 1, |
| 143 | .default_value = 0, |
| 144 | .flags = 0, |
| 145 | },{ |
| 146 | .id = V4L2_CID_BLUE_BALANCE, |
| 147 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 148 | .name = "Blue chroma balance", |
| 149 | .minimum = -128, |
| 150 | .maximum = 127, |
| 151 | .step = 1, |
| 152 | .default_value = 0, |
| 153 | .flags = 0, |
| 154 | },{ |
| 155 | .id = V4L2_CID_GAMMA, |
| 156 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 157 | .name = "Gamma", |
| 158 | .minimum = 0x0, |
| 159 | .maximum = 0x3f, |
| 160 | .step = 0x1, |
| 161 | .default_value = 0x20, |
| 162 | .flags = 0, |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | static struct usb_driver em2820_usb_driver; |
| 167 | |
| 168 | static DECLARE_MUTEX(em2820_sysfs_lock); |
| 169 | static DECLARE_RWSEM(em2820_disconnect); |
| 170 | |
| 171 | /********************* v4l2 interface ******************************************/ |
| 172 | |
| 173 | static inline unsigned long kvirt_to_pa(unsigned long adr) |
| 174 | { |
| 175 | unsigned long kva, ret; |
| 176 | |
| 177 | kva = (unsigned long)page_address(vmalloc_to_page((void *)adr)); |
| 178 | kva |= adr & (PAGE_SIZE - 1); |
| 179 | ret = __pa(kva); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * em2820_config() |
| 185 | * inits registers with sane defaults |
| 186 | */ |
| 187 | static int em2820_config(struct em2820 *dev) |
| 188 | { |
| 189 | |
| 190 | /* Sets I2C speed to 100 KHz */ |
| 191 | em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1); |
| 192 | |
| 193 | /* enable vbi capturing */ |
| 194 | em2820_audio_usb_mute(dev, 1); |
| 195 | dev->mute = 1; /* maybe not the right place... */ |
| 196 | dev->volume = 0x1f; |
| 197 | em2820_audio_analog_set(dev); |
| 198 | em2820_audio_analog_setup(dev); |
| 199 | em2820_outfmt_set_yuv422(dev); |
| 200 | em2820_colorlevels_set_default(dev); |
| 201 | em2820_compression_disable(dev); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * em2820_config_i2c() |
| 208 | * configure i2c attached devices |
| 209 | */ |
| 210 | void em2820_config_i2c(struct em2820 *dev) |
| 211 | { |
| 212 | struct v4l2_frequency f; |
| 213 | struct video_decoder_init em2820_vdi = {.data = NULL }; |
| 214 | |
| 215 | |
| 216 | /* configure decoder */ |
| 217 | em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi); |
| 218 | em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input); |
| 219 | /* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */ |
| 220 | /* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */ |
| 221 | /* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */ |
| 222 | /* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */ |
| 223 | |
| 224 | /* configure tuner */ |
| 225 | f.tuner = 0; |
| 226 | f.type = V4L2_TUNER_ANALOG_TV; |
| 227 | f.frequency = 9076; /* FIXME:remove magic number */ |
| 228 | dev->ctl_freq = f.frequency; |
| 229 | em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f); |
| 230 | |
| 231 | /* configure tda9887 */ |
| 232 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 233 | |
| 234 | /* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */ |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * em2820_empty_framequeues() |
| 239 | * prepare queues for incoming and outgoing frames |
| 240 | */ |
| 241 | static void em2820_empty_framequeues(struct em2820 *dev) |
| 242 | { |
| 243 | u32 i; |
| 244 | |
| 245 | INIT_LIST_HEAD(&dev->inqueue); |
| 246 | INIT_LIST_HEAD(&dev->outqueue); |
| 247 | |
| 248 | for (i = 0; i < EM2820_NUM_FRAMES; i++) { |
| 249 | dev->frame[i].state = F_UNUSED; |
| 250 | dev->frame[i].buf.bytesused = 0; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * em2820_v4l2_open() |
| 256 | * inits the device and starts isoc transfer |
| 257 | */ |
| 258 | static int em2820_v4l2_open(struct inode *inode, struct file *filp) |
| 259 | { |
| 260 | struct video_device *vdev = video_devdata(filp); |
| 261 | int minor = iminor(inode); |
| 262 | struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev); |
| 263 | int errCode = 0; |
| 264 | |
| 265 | em2820_videodbg("users=%d", dev->users); |
| 266 | |
| 267 | if (!down_read_trylock(&em2820_disconnect)) |
| 268 | return -ERESTARTSYS; |
| 269 | |
| 270 | if (dev->users) { |
| 271 | em2820_warn("this driver can be opened only once\n"); |
| 272 | up_read(&em2820_disconnect); |
| 273 | return -EBUSY; |
| 274 | } |
| 275 | |
| 276 | /* if(dev->vbi_dev->minor == minor){ |
| 277 | dev->type=V4L2_BUF_TYPE_VBI_CAPTURE; |
| 278 | }*/ |
| 279 | if (dev->vdev->minor == minor) { |
| 280 | dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 281 | } |
| 282 | |
| 283 | init_MUTEX(&dev->fileop_lock); /* to 1 == available */ |
| 284 | spin_lock_init(&dev->queue_lock); |
| 285 | init_waitqueue_head(&dev->wait_frame); |
| 286 | init_waitqueue_head(&dev->wait_stream); |
| 287 | |
| 288 | down(&dev->lock); |
| 289 | |
| 290 | em2820_set_alternate(dev); |
| 291 | |
| 292 | dev->width = norm_maxw(dev); |
| 293 | dev->height = norm_maxh(dev); |
| 294 | dev->frame_size = dev->width * dev->height * 2; |
| 295 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ |
| 296 | dev->bytesperline = dev->width * 2; |
| 297 | dev->hscale = 0; |
| 298 | dev->vscale = 0; |
| 299 | |
| 300 | em2820_capture_start(dev, 1); |
| 301 | em2820_resolution_set(dev); |
| 302 | |
| 303 | /* start the transfer */ |
| 304 | errCode = em2820_init_isoc(dev); |
| 305 | if (errCode) |
| 306 | goto err; |
| 307 | |
| 308 | dev->users++; |
| 309 | filp->private_data = dev; |
| 310 | dev->io = IO_NONE; |
| 311 | dev->stream = STREAM_OFF; |
| 312 | dev->num_frames = 0; |
| 313 | |
| 314 | /* prepare queues */ |
| 315 | em2820_empty_framequeues(dev); |
| 316 | |
| 317 | dev->state |= DEV_INITIALIZED; |
| 318 | |
| 319 | err: |
| 320 | up(&dev->lock); |
| 321 | up_read(&em2820_disconnect); |
| 322 | return errCode; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * em2820_realease_resources() |
| 327 | * unregisters the v4l2,i2c and usb devices |
| 328 | * called when the device gets disconected or at module unload |
| 329 | */ |
| 330 | static void em2820_release_resources(struct em2820 *dev) |
| 331 | { |
| 332 | down(&em2820_sysfs_lock); |
| 333 | |
| 334 | em2820_info("V4L2 device /dev/video%d deregistered\n", |
| 335 | dev->vdev->minor); |
| 336 | video_set_drvdata(dev->vdev, NULL); |
| 337 | video_unregister_device(dev->vdev); |
| 338 | /* video_unregister_device(dev->vbi_dev); */ |
| 339 | em2820_i2c_unregister(dev); |
| 340 | usb_put_dev(dev->udev); |
| 341 | up(&em2820_sysfs_lock); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * em2820_v4l2_close() |
| 346 | * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls |
| 347 | */ |
| 348 | static int em2820_v4l2_close(struct inode *inode, struct file *file) |
| 349 | { |
| 350 | struct video_device *vdev = video_devdata(file); |
| 351 | struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev); |
| 352 | int errCode; |
| 353 | |
| 354 | em2820_videodbg("users=%d", dev->users); |
| 355 | |
| 356 | down(&dev->lock); |
| 357 | |
| 358 | em2820_uninit_isoc(dev); |
| 359 | |
| 360 | em2820_release_buffers(dev); |
| 361 | |
| 362 | /* the device is already disconnect, free the remaining resources */ |
| 363 | if (dev->state & DEV_DISCONNECTED) { |
| 364 | em2820_release_resources(dev); |
| 365 | up(&dev->lock); |
| 366 | kfree(dev); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /* set alternate 0 */ |
| 371 | dev->alt = 0; |
| 372 | em2820_videodbg("setting alternate 0"); |
| 373 | errCode = usb_set_interface(dev->udev, 0, 0); |
| 374 | if (errCode < 0) { |
| 375 | em2820_errdev ("cannot change alternate number to 0 (error=%i)\n", |
| 376 | errCode); |
| 377 | } |
| 378 | |
| 379 | dev->users--; |
| 380 | wake_up_interruptible_nr(&dev->open, 1); |
| 381 | up(&dev->lock); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * em2820_v4l2_read() |
| 387 | * will allocate buffers when called for the first time |
| 388 | */ |
| 389 | static ssize_t |
| 390 | em2820_v4l2_read(struct file *filp, char __user * buf, size_t count, |
| 391 | loff_t * f_pos) |
| 392 | { |
| 393 | struct em2820 *dev = video_get_drvdata(video_devdata(filp)); |
| 394 | struct em2820_frame_t *f, *i; |
| 395 | unsigned long lock_flags; |
| 396 | int ret = 0; |
| 397 | |
| 398 | if (down_interruptible(&dev->fileop_lock)) |
| 399 | return -ERESTARTSYS; |
| 400 | |
| 401 | if (dev->state & DEV_DISCONNECTED) { |
| 402 | em2820_videodbg("device not present"); |
| 403 | up(&dev->fileop_lock); |
| 404 | return -ENODEV; |
| 405 | } |
| 406 | |
| 407 | if (dev->state & DEV_MISCONFIGURED) { |
| 408 | em2820_videodbg("device misconfigured; close and open it again"); |
| 409 | up(&dev->fileop_lock); |
| 410 | return -EIO; |
| 411 | } |
| 412 | |
| 413 | if (dev->io == IO_MMAP) { |
| 414 | em2820_videodbg ("IO method is set to mmap; close and open" |
| 415 | " the device again to choose the read method"); |
| 416 | up(&dev->fileop_lock); |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | if (dev->io == IO_NONE) { |
| 421 | if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) { |
| 422 | em2820_errdev("read failed, not enough memory\n"); |
| 423 | up(&dev->fileop_lock); |
| 424 | return -ENOMEM; |
| 425 | } |
| 426 | dev->io = IO_READ; |
| 427 | dev->stream = STREAM_ON; |
| 428 | em2820_queue_unusedframes(dev); |
| 429 | } |
| 430 | |
| 431 | if (!count) { |
| 432 | up(&dev->fileop_lock); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | if (list_empty(&dev->outqueue)) { |
| 437 | if (filp->f_flags & O_NONBLOCK) { |
| 438 | up(&dev->fileop_lock); |
| 439 | return -EAGAIN; |
| 440 | } |
| 441 | ret = wait_event_interruptible |
| 442 | (dev->wait_frame, |
| 443 | (!list_empty(&dev->outqueue)) || |
| 444 | (dev->state & DEV_DISCONNECTED)); |
| 445 | if (ret) { |
| 446 | up(&dev->fileop_lock); |
| 447 | return ret; |
| 448 | } |
| 449 | if (dev->state & DEV_DISCONNECTED) { |
| 450 | up(&dev->fileop_lock); |
| 451 | return -ENODEV; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame); |
| 456 | |
| 457 | spin_lock_irqsave(&dev->queue_lock, lock_flags); |
| 458 | list_for_each_entry(i, &dev->outqueue, frame) |
| 459 | i->state = F_UNUSED; |
| 460 | INIT_LIST_HEAD(&dev->outqueue); |
| 461 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); |
| 462 | |
| 463 | em2820_queue_unusedframes(dev); |
| 464 | |
| 465 | if (count > f->buf.length) |
| 466 | count = f->buf.length; |
| 467 | |
| 468 | if (copy_to_user(buf, f->bufmem, count)) { |
| 469 | up(&dev->fileop_lock); |
| 470 | return -EFAULT; |
| 471 | } |
| 472 | *f_pos += count; |
| 473 | |
| 474 | up(&dev->fileop_lock); |
| 475 | |
| 476 | return count; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * em2820_v4l2_poll() |
| 481 | * will allocate buffers when called for the first time |
| 482 | */ |
| 483 | static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait) |
| 484 | { |
| 485 | struct em2820 *dev = video_get_drvdata(video_devdata(filp)); |
| 486 | unsigned int mask = 0; |
| 487 | |
| 488 | if (down_interruptible(&dev->fileop_lock)) |
| 489 | return POLLERR; |
| 490 | |
| 491 | if (dev->state & DEV_DISCONNECTED) { |
| 492 | em2820_videodbg("device not present"); |
| 493 | } else if (dev->state & DEV_MISCONFIGURED) { |
| 494 | em2820_videodbg("device is misconfigured; close and open it again"); |
| 495 | } else { |
| 496 | if (dev->io == IO_NONE) { |
| 497 | if (!em2820_request_buffers |
| 498 | (dev, EM2820_NUM_READ_FRAMES)) { |
| 499 | em2820_warn |
| 500 | ("poll() failed, not enough memory\n"); |
| 501 | } else { |
| 502 | dev->io = IO_READ; |
| 503 | dev->stream = STREAM_ON; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (dev->io == IO_READ) { |
| 508 | em2820_queue_unusedframes(dev); |
| 509 | poll_wait(filp, &dev->wait_frame, wait); |
| 510 | |
| 511 | if (!list_empty(&dev->outqueue)) |
| 512 | mask |= POLLIN | POLLRDNORM; |
| 513 | |
| 514 | up(&dev->fileop_lock); |
| 515 | |
| 516 | return mask; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | up(&dev->fileop_lock); |
| 521 | return POLLERR; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * em2820_vm_open() |
| 526 | */ |
| 527 | static void em2820_vm_open(struct vm_area_struct *vma) |
| 528 | { |
| 529 | struct em2820_frame_t *f = vma->vm_private_data; |
| 530 | f->vma_use_count++; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * em2820_vm_close() |
| 535 | */ |
| 536 | static void em2820_vm_close(struct vm_area_struct *vma) |
| 537 | { |
| 538 | /* NOTE: buffers are not freed here */ |
| 539 | struct em2820_frame_t *f = vma->vm_private_data; |
| 540 | f->vma_use_count--; |
| 541 | } |
| 542 | |
| 543 | static struct vm_operations_struct em2820_vm_ops = { |
| 544 | .open = em2820_vm_open, |
| 545 | .close = em2820_vm_close, |
| 546 | }; |
| 547 | |
| 548 | /* |
| 549 | * em2820_v4l2_mmap() |
| 550 | */ |
| 551 | static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) |
| 552 | { |
| 553 | struct em2820 *dev = video_get_drvdata(video_devdata(filp)); |
| 554 | unsigned long size = vma->vm_end - vma->vm_start, |
| 555 | start = vma->vm_start, pos, page; |
| 556 | u32 i; |
| 557 | if (down_interruptible(&dev->fileop_lock)) |
| 558 | return -ERESTARTSYS; |
| 559 | |
| 560 | if (dev->state & DEV_DISCONNECTED) { |
| 561 | em2820_videodbg("mmap: device not present"); |
| 562 | up(&dev->fileop_lock); |
| 563 | return -ENODEV; |
| 564 | } |
| 565 | |
| 566 | if (dev->state & DEV_MISCONFIGURED) { |
| 567 | em2820_videodbg ("mmap: Device is misconfigured; close and " |
| 568 | "open it again"); |
| 569 | up(&dev->fileop_lock); |
| 570 | return -EIO; |
| 571 | } |
| 572 | |
| 573 | if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || |
| 574 | size != PAGE_ALIGN(dev->frame[0].buf.length)) { |
| 575 | up(&dev->fileop_lock); |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | for (i = 0; i < dev->num_frames; i++) { |
| 580 | if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) |
| 581 | break; |
| 582 | } |
| 583 | if (i == dev->num_frames) { |
| 584 | em2820_videodbg("mmap: user supplied mapping address is out of range"); |
| 585 | up(&dev->fileop_lock); |
| 586 | return -EINVAL; |
| 587 | } |
| 588 | |
| 589 | /* VM_IO is eventually going to replace PageReserved altogether */ |
| 590 | vma->vm_flags |= VM_IO; |
| 591 | vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ |
| 592 | |
| 593 | pos = (unsigned long)dev->frame[i].bufmem; |
| 594 | while (size > 0) { /* size is page-aligned */ |
| 595 | page = vmalloc_to_pfn((void *)pos); |
| 596 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, |
| 597 | vma->vm_page_prot)) { |
| 598 | em2820_videodbg("mmap: rename page map failed"); |
| 599 | up(&dev->fileop_lock); |
| 600 | return -EAGAIN; |
| 601 | } |
| 602 | start += PAGE_SIZE; |
| 603 | pos += PAGE_SIZE; |
| 604 | size -= PAGE_SIZE; |
| 605 | } |
| 606 | |
| 607 | vma->vm_ops = &em2820_vm_ops; |
| 608 | vma->vm_private_data = &dev->frame[i]; |
| 609 | |
| 610 | em2820_vm_open(vma); |
| 611 | up(&dev->fileop_lock); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * em2820_get_ctrl() |
| 617 | * return the current saturation, brightness or contrast, mute state |
| 618 | */ |
| 619 | static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl) |
| 620 | { |
| 621 | s32 tmp; |
| 622 | switch (ctrl->id) { |
| 623 | case V4L2_CID_AUDIO_MUTE: |
| 624 | ctrl->value = dev->mute; |
| 625 | return 0; |
| 626 | case V4L2_CID_AUDIO_VOLUME: |
| 627 | ctrl->value = dev->volume; |
| 628 | return 0; |
| 629 | case V4L2_CID_BRIGHTNESS: |
| 630 | if ((tmp = em2820_brightness_get(dev)) < 0) |
| 631 | return -EIO; |
| 632 | ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */ |
| 633 | return 0; |
| 634 | case V4L2_CID_CONTRAST: |
| 635 | if ((ctrl->value = em2820_contrast_get(dev)) < 0) |
| 636 | return -EIO; |
| 637 | return 0; |
| 638 | case V4L2_CID_SATURATION: |
| 639 | if ((ctrl->value = em2820_saturation_get(dev)) < 0) |
| 640 | return -EIO; |
| 641 | return 0; |
| 642 | case V4L2_CID_RED_BALANCE: |
| 643 | if ((tmp = em2820_v_balance_get(dev)) < 0) |
| 644 | return -EIO; |
| 645 | ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */ |
| 646 | return 0; |
| 647 | case V4L2_CID_BLUE_BALANCE: |
| 648 | if ((tmp = em2820_u_balance_get(dev)) < 0) |
| 649 | return -EIO; |
| 650 | ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */ |
| 651 | return 0; |
| 652 | case V4L2_CID_GAMMA: |
| 653 | if ((ctrl->value = em2820_gamma_get(dev)) < 0) |
| 654 | return -EIO; |
| 655 | return 0; |
| 656 | default: |
| 657 | return -EINVAL; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * em2820_set_ctrl() |
| 663 | * mute or set new saturation, brightness or contrast |
| 664 | */ |
| 665 | static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl) |
| 666 | { |
| 667 | switch (ctrl->id) { |
| 668 | case V4L2_CID_AUDIO_MUTE: |
| 669 | if (ctrl->value != dev->mute) { |
| 670 | dev->mute = ctrl->value; |
| 671 | em2820_audio_usb_mute(dev, ctrl->value); |
| 672 | return em2820_audio_analog_set(dev); |
| 673 | } |
| 674 | return 0; |
| 675 | case V4L2_CID_AUDIO_VOLUME: |
| 676 | dev->volume = ctrl->value; |
| 677 | return em2820_audio_analog_set(dev); |
| 678 | case V4L2_CID_BRIGHTNESS: |
| 679 | return em2820_brightness_set(dev, ctrl->value); |
| 680 | case V4L2_CID_CONTRAST: |
| 681 | return em2820_contrast_set(dev, ctrl->value); |
| 682 | case V4L2_CID_SATURATION: |
| 683 | return em2820_saturation_set(dev, ctrl->value); |
| 684 | case V4L2_CID_RED_BALANCE: |
| 685 | return em2820_v_balance_set(dev, ctrl->value); |
| 686 | case V4L2_CID_BLUE_BALANCE: |
| 687 | return em2820_u_balance_set(dev, ctrl->value); |
| 688 | case V4L2_CID_GAMMA: |
| 689 | return em2820_gamma_set(dev, ctrl->value); |
| 690 | default: |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * em2820_stream_interrupt() |
| 697 | * stops streaming |
| 698 | */ |
| 699 | static int em2820_stream_interrupt(struct em2820 *dev) |
| 700 | { |
| 701 | int ret = 0; |
| 702 | |
| 703 | /* stop reading from the device */ |
| 704 | |
| 705 | dev->stream = STREAM_INTERRUPT; |
| 706 | ret = wait_event_timeout(dev->wait_stream, |
| 707 | (dev->stream == STREAM_OFF) || |
| 708 | (dev->state & DEV_DISCONNECTED), |
| 709 | EM2820_URB_TIMEOUT); |
| 710 | if (dev->state & DEV_DISCONNECTED) |
| 711 | return -ENODEV; |
| 712 | else if (ret) { |
| 713 | dev->state |= DEV_MISCONFIGURED; |
| 714 | em2820_videodbg("device is misconfigured; close and " |
| 715 | "open /dev/video%d again", dev->vdev->minor); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int em2820_set_norm(struct em2820 *dev, int width, int height) |
| 723 | { |
| 724 | unsigned int hscale, vscale; |
| 725 | unsigned int maxh, maxw; |
| 726 | |
| 727 | maxw = norm_maxw(dev); |
| 728 | maxh = norm_maxh(dev); |
| 729 | |
| 730 | /* width must even because of the YUYV format */ |
| 731 | /* height must be even because of interlacing */ |
| 732 | height &= 0xfffe; |
| 733 | width &= 0xfffe; |
| 734 | |
| 735 | if (height < 32) |
| 736 | height = 32; |
| 737 | if (height > maxh) |
| 738 | height = maxh; |
| 739 | if (width < 48) |
| 740 | width = 48; |
| 741 | if (width > maxw) |
| 742 | width = maxw; |
| 743 | |
| 744 | if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000) |
| 745 | hscale = 0x3fff; |
| 746 | width = (((unsigned long)maxw) << 12) / (hscale + 4096L); |
| 747 | |
| 748 | if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000) |
| 749 | vscale = 0x3fff; |
| 750 | height = (((unsigned long)maxh) << 12) / (vscale + 4096L); |
| 751 | |
| 752 | /* set new image size */ |
| 753 | dev->width = width; |
| 754 | dev->height = height; |
| 755 | dev->frame_size = dev->width * dev->height * 2; |
| 756 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ |
| 757 | dev->bytesperline = dev->width * 2; |
| 758 | dev->hscale = hscale; |
| 759 | dev->vscale = vscale; |
| 760 | |
| 761 | em2820_resolution_set(dev); |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static void video_mux(struct em2820 *dev, int index) |
| 767 | { |
| 768 | int input, ainput; |
| 769 | |
| 770 | input = INPUT(index)->vmux; |
| 771 | dev->ctl_input = index; |
| 772 | |
| 773 | em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input); |
| 774 | |
| 775 | dev->ctl_ainput = INPUT(index)->amux; |
| 776 | |
| 777 | switch (dev->ctl_ainput) { |
| 778 | case 0: |
| 779 | ainput = EM2820_AUDIO_SRC_TUNER; |
| 780 | break; |
| 781 | default: |
| 782 | ainput = EM2820_AUDIO_SRC_LINE; |
| 783 | } |
| 784 | |
| 785 | em2820_audio_source(dev, ainput); |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * em2820_v4l2_do_ioctl() |
| 790 | * This function is _not_ called directly, but from |
| 791 | * em2820_v4l2_ioctl. Userspace |
| 792 | * copying is done already, arg is a kernel pointer. |
| 793 | */ |
| 794 | static int em2820_do_ioctl(struct inode *inode, struct file *filp, |
| 795 | struct em2820 *dev, unsigned int cmd, void *arg, |
| 796 | v4l2_kioctl driver_ioctl) |
| 797 | { |
| 798 | int ret; |
| 799 | |
| 800 | switch (cmd) { |
| 801 | /* ---------- tv norms ---------- */ |
| 802 | case VIDIOC_ENUMSTD: |
| 803 | { |
| 804 | struct v4l2_standard *e = arg; |
| 805 | unsigned int i; |
| 806 | |
| 807 | i = e->index; |
| 808 | if (i >= TVNORMS) |
| 809 | return -EINVAL; |
| 810 | ret = v4l2_video_std_construct(e, tvnorms[e->index].id, |
| 811 | tvnorms[e->index].name); |
| 812 | e->index = i; |
| 813 | if (ret < 0) |
| 814 | return ret; |
| 815 | return 0; |
| 816 | } |
| 817 | case VIDIOC_G_STD: |
| 818 | { |
| 819 | v4l2_std_id *id = arg; |
| 820 | |
| 821 | *id = dev->tvnorm->id; |
| 822 | return 0; |
| 823 | } |
| 824 | case VIDIOC_S_STD: |
| 825 | { |
| 826 | v4l2_std_id *id = arg; |
| 827 | unsigned int i; |
| 828 | |
| 829 | for (i = 0; i < TVNORMS; i++) |
| 830 | if (*id == tvnorms[i].id) |
| 831 | break; |
| 832 | if (i == TVNORMS) |
| 833 | for (i = 0; i < TVNORMS; i++) |
| 834 | if (*id & tvnorms[i].id) |
| 835 | break; |
| 836 | if (i == TVNORMS) |
| 837 | return -EINVAL; |
| 838 | |
| 839 | down(&dev->lock); |
| 840 | dev->tvnorm = &tvnorms[i]; |
| 841 | |
| 842 | em2820_set_norm(dev, dev->width, dev->height); |
| 843 | |
| 844 | /* |
| 845 | dev->width=norm_maxw(dev); |
| 846 | dev->height=norm_maxh(dev); |
| 847 | dev->frame_size=dev->width*dev->height*2; |
| 848 | dev->field_size=dev->frame_size>>1; |
| 849 | dev->bytesperline=dev->width*2; |
| 850 | dev->hscale=0; |
| 851 | dev->vscale=0; |
| 852 | |
| 853 | em2820_resolution_set(dev); |
| 854 | */ |
| 855 | /* |
| 856 | em2820_uninit_isoc(dev); |
| 857 | em2820_set_alternate(dev); |
| 858 | em2820_capture_start(dev, 1); |
| 859 | em2820_resolution_set(dev); |
| 860 | em2820_init_isoc(dev); |
| 861 | */ |
| 862 | em2820_i2c_call_clients(dev, DECODER_SET_NORM, |
| 863 | &tvnorms[i].mode); |
| 864 | em2820_i2c_call_clients(dev, VIDIOC_S_STD, |
| 865 | &dev->tvnorm->id); |
| 866 | |
| 867 | up(&dev->lock); |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | /* ------ input switching ---------- */ |
| 873 | case VIDIOC_ENUMINPUT: |
| 874 | { |
| 875 | struct v4l2_input *i = arg; |
| 876 | unsigned int n; |
| 877 | static const char *iname[] = { |
| 878 | [EM2820_VMUX_COMPOSITE1] = "Composite1", |
| 879 | [EM2820_VMUX_COMPOSITE2] = "Composite2", |
| 880 | [EM2820_VMUX_COMPOSITE3] = "Composite3", |
| 881 | [EM2820_VMUX_COMPOSITE4] = "Composite4", |
| 882 | [EM2820_VMUX_SVIDEO] = "S-Video", |
| 883 | [EM2820_VMUX_TELEVISION] = "Television", |
| 884 | [EM2820_VMUX_CABLE] = "Cable TV", |
| 885 | [EM2820_VMUX_DVB] = "DVB", |
| 886 | [EM2820_VMUX_DEBUG] = "for debug only", |
| 887 | }; |
| 888 | |
| 889 | n = i->index; |
| 890 | if (n >= MAX_EM2820_INPUT) |
| 891 | return -EINVAL; |
| 892 | if (0 == INPUT(n)->type) |
| 893 | return -EINVAL; |
| 894 | memset(i, 0, sizeof(*i)); |
| 895 | i->index = n; |
| 896 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 897 | strcpy(i->name, iname[INPUT(n)->type]); |
| 898 | if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) || |
| 899 | (EM2820_VMUX_CABLE == INPUT(n)->type)) |
| 900 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 901 | for (n = 0; n < ARRAY_SIZE(tvnorms); n++) |
| 902 | i->std |= tvnorms[n].id; |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | case VIDIOC_G_INPUT: |
| 907 | { |
| 908 | int *i = arg; |
| 909 | *i = dev->ctl_input; |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | case VIDIOC_S_INPUT: |
| 915 | { |
| 916 | int *index = arg; |
| 917 | |
| 918 | if (*index >= MAX_EM2820_INPUT) |
| 919 | return -EINVAL; |
| 920 | if (0 == INPUT(*index)->type) |
| 921 | return -EINVAL; |
| 922 | |
| 923 | down(&dev->lock); |
| 924 | video_mux(dev, *index); |
| 925 | up(&dev->lock); |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | case VIDIOC_G_AUDIO: |
| 931 | { |
| 932 | struct v4l2_audio *a = arg; |
| 933 | unsigned int index = a->index; |
| 934 | |
| 935 | if (a->index > 1) |
| 936 | return -EINVAL; |
| 937 | memset(a, 0, sizeof(*a)); |
| 938 | index = dev->ctl_ainput; |
| 939 | |
| 940 | if (index == 0) { |
| 941 | strcpy(a->name, "Television"); |
| 942 | } else { |
| 943 | strcpy(a->name, "Line In"); |
| 944 | } |
| 945 | a->capability = V4L2_AUDCAP_STEREO; |
| 946 | a->index = index; |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | case VIDIOC_S_AUDIO: |
| 951 | { |
| 952 | struct v4l2_audio *a = arg; |
| 953 | if (a->index != dev->ctl_ainput) |
| 954 | return -EINVAL; |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | /* --- controls ---------------------------------------------- */ |
| 960 | case VIDIOC_QUERYCTRL: |
| 961 | { |
| 962 | struct v4l2_queryctrl *qc = arg; |
| 963 | u8 i, n; |
| 964 | n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]); |
| 965 | for (i = 0; i < n; i++) |
| 966 | if (qc->id && qc->id == em2820_qctrl[i].id) { |
| 967 | memcpy(qc, &(em2820_qctrl[i]), |
| 968 | sizeof(*qc)); |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | return -EINVAL; |
| 973 | } |
| 974 | |
| 975 | case VIDIOC_G_CTRL: |
| 976 | { |
| 977 | struct v4l2_control *ctrl = arg; |
| 978 | |
| 979 | |
| 980 | return em2820_get_ctrl(dev, ctrl); |
| 981 | } |
| 982 | |
| 983 | case VIDIOC_S_CTRL_OLD: /* ??? */ |
| 984 | case VIDIOC_S_CTRL: |
| 985 | { |
| 986 | struct v4l2_control *ctrl = arg; |
| 987 | u8 i, n; |
| 988 | |
| 989 | |
| 990 | n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]); |
| 991 | for (i = 0; i < n; i++) |
| 992 | if (ctrl->id == em2820_qctrl[i].id) { |
| 993 | if (ctrl->value < |
| 994 | em2820_qctrl[i].minimum |
| 995 | || ctrl->value > |
| 996 | em2820_qctrl[i].maximum) |
| 997 | return -ERANGE; |
| 998 | |
| 999 | return em2820_set_ctrl(dev, ctrl); |
| 1000 | } |
| 1001 | return -EINVAL; |
| 1002 | } |
| 1003 | |
| 1004 | /* --- tuner ioctls ------------------------------------------ */ |
| 1005 | case VIDIOC_G_TUNER: |
| 1006 | { |
| 1007 | struct v4l2_tuner *t = arg; |
| 1008 | int status = 0; |
| 1009 | |
| 1010 | if (0 != t->index) |
| 1011 | return -EINVAL; |
| 1012 | |
| 1013 | memset(t, 0, sizeof(*t)); |
| 1014 | strcpy(t->name, "Tuner"); |
| 1015 | t->type = V4L2_TUNER_ANALOG_TV; |
| 1016 | t->capability = V4L2_TUNER_CAP_NORM; |
| 1017 | t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */ |
| 1018 | /* t->signal = 0xffff;*/ |
| 1019 | /* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/ |
| 1020 | /* No way to get signal strength? */ |
| 1021 | down(&dev->lock); |
| 1022 | em2820_i2c_call_clients(dev, DECODER_GET_STATUS, |
| 1023 | &status); |
| 1024 | up(&dev->lock); |
| 1025 | t->signal = |
| 1026 | (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0; |
| 1027 | |
| 1028 | em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal, |
| 1029 | t->afc); |
| 1030 | return 0; |
| 1031 | } |
| 1032 | case VIDIOC_S_TUNER: |
| 1033 | { |
| 1034 | struct v4l2_tuner *t = arg; |
| 1035 | int status = 0; |
| 1036 | |
| 1037 | if (0 != t->index) |
| 1038 | return -EINVAL; |
| 1039 | memset(t, 0, sizeof(*t)); |
| 1040 | strcpy(t->name, "Tuner"); |
| 1041 | t->type = V4L2_TUNER_ANALOG_TV; |
| 1042 | t->capability = V4L2_TUNER_CAP_NORM; |
| 1043 | t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */ |
| 1044 | /* t->signal = 0xffff; */ |
| 1045 | /* No way to get signal strength? */ |
| 1046 | down(&dev->lock); |
| 1047 | em2820_i2c_call_clients(dev, DECODER_GET_STATUS, |
| 1048 | &status); |
| 1049 | up(&dev->lock); |
| 1050 | t->signal = |
| 1051 | (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0; |
| 1052 | |
| 1053 | em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n", |
| 1054 | t->signal, t->afc); |
| 1055 | return 0; |
| 1056 | } |
| 1057 | case VIDIOC_G_FREQUENCY: |
| 1058 | { |
| 1059 | struct v4l2_frequency *f = arg; |
| 1060 | |
| 1061 | memset(f, 0, sizeof(*f)); |
| 1062 | f->type = V4L2_TUNER_ANALOG_TV; |
| 1063 | f->frequency = dev->ctl_freq; |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | case VIDIOC_S_FREQUENCY: |
| 1068 | { |
| 1069 | struct v4l2_frequency *f = arg; |
| 1070 | |
| 1071 | if (0 != f->tuner) |
| 1072 | return -EINVAL; |
| 1073 | |
| 1074 | if (V4L2_TUNER_ANALOG_TV != f->type) |
| 1075 | return -EINVAL; |
| 1076 | |
| 1077 | down(&dev->lock); |
| 1078 | dev->ctl_freq = f->frequency; |
| 1079 | em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f); |
| 1080 | up(&dev->lock); |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | case VIDIOC_CROPCAP: |
| 1085 | { |
| 1086 | struct v4l2_cropcap *cc = arg; |
| 1087 | |
| 1088 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1089 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1090 | cc->bounds.left = 0; |
| 1091 | cc->bounds.top = 0; |
| 1092 | cc->bounds.width = dev->width; |
| 1093 | cc->bounds.height = dev->height; |
| 1094 | cc->defrect = cc->bounds; |
| 1095 | cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */ |
| 1096 | cc->pixelaspect.denominator = 59; |
| 1097 | return 0; |
| 1098 | } |
| 1099 | case VIDIOC_STREAMON: |
| 1100 | { |
| 1101 | int *type = arg; |
| 1102 | |
| 1103 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1104 | || dev->io != IO_MMAP) |
| 1105 | return -EINVAL; |
| 1106 | |
| 1107 | if (list_empty(&dev->inqueue)) |
| 1108 | return -EINVAL; |
| 1109 | |
| 1110 | dev->stream = STREAM_ON; /* FIXME: Start video capture here? */ |
| 1111 | |
| 1112 | em2820_videodbg("VIDIOC_STREAMON: starting stream"); |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | case VIDIOC_STREAMOFF: |
| 1117 | { |
| 1118 | int *type = arg; |
| 1119 | int ret; |
| 1120 | |
| 1121 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1122 | || dev->io != IO_MMAP) |
| 1123 | return -EINVAL; |
| 1124 | |
| 1125 | if (dev->stream == STREAM_ON) { |
| 1126 | em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream"); |
| 1127 | if ((ret = em2820_stream_interrupt(dev))) |
| 1128 | return ret; |
| 1129 | } |
| 1130 | em2820_empty_framequeues(dev); |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | default: |
| 1135 | return v4l_compat_translate_ioctl(inode, filp, cmd, arg, |
| 1136 | driver_ioctl); |
| 1137 | } |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * em2820_v4l2_do_ioctl() |
| 1143 | * This function is _not_ called directly, but from |
| 1144 | * em2820_v4l2_ioctl. Userspace |
| 1145 | * copying is done already, arg is a kernel pointer. |
| 1146 | */ |
| 1147 | static int em2820_video_do_ioctl(struct inode *inode, struct file *filp, |
| 1148 | unsigned int cmd, void *arg) |
| 1149 | { |
| 1150 | struct em2820 *dev = filp->private_data; |
| 1151 | |
| 1152 | if (!dev) |
| 1153 | return -ENODEV; |
| 1154 | |
| 1155 | if (video_debug > 1) |
| 1156 | em2820_print_ioctl(dev->name,cmd); |
| 1157 | |
| 1158 | switch (cmd) { |
| 1159 | |
| 1160 | /* --- capabilities ------------------------------------------ */ |
| 1161 | case VIDIOC_QUERYCAP: |
| 1162 | { |
| 1163 | struct v4l2_capability *cap = arg; |
| 1164 | |
| 1165 | memset(cap, 0, sizeof(*cap)); |
| 1166 | strlcpy(cap->driver, "em2820", sizeof(cap->driver)); |
| 1167 | strlcpy(cap->card, em2820_boards[dev->model].name, |
| 1168 | sizeof(cap->card)); |
| 1169 | strlcpy(cap->bus_info, dev->udev->dev.bus_id, |
| 1170 | sizeof(cap->bus_info)); |
| 1171 | cap->version = EM2820_VERSION_CODE; |
| 1172 | cap->capabilities = |
| 1173 | V4L2_CAP_VIDEO_CAPTURE | |
| 1174 | V4L2_CAP_AUDIO | |
| 1175 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 1176 | if (dev->has_tuner) |
| 1177 | cap->capabilities |= V4L2_CAP_TUNER; |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | /* --- capture ioctls ---------------------------------------- */ |
| 1182 | case VIDIOC_ENUM_FMT: |
| 1183 | { |
| 1184 | struct v4l2_fmtdesc *fmtd = arg; |
| 1185 | |
| 1186 | if (fmtd->index != 0) |
| 1187 | return -EINVAL; |
| 1188 | memset(fmtd, 0, sizeof(*fmtd)); |
| 1189 | fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1190 | strcpy(fmtd->description, "Packed YUY2"); |
| 1191 | fmtd->pixelformat = V4L2_PIX_FMT_YUYV; |
| 1192 | memset(fmtd->reserved, 0, sizeof(fmtd->reserved)); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | case VIDIOC_G_FMT: |
| 1197 | { |
| 1198 | struct v4l2_format *format = arg; |
| 1199 | |
| 1200 | em2820_videodbg("VIDIOC_G_FMT: type=%s", |
| 1201 | format->type == |
| 1202 | V4L2_BUF_TYPE_VIDEO_CAPTURE ? |
| 1203 | "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type == |
| 1204 | V4L2_BUF_TYPE_VBI_CAPTURE ? |
| 1205 | "V4L2_BUF_TYPE_VBI_CAPTURE " : |
| 1206 | "not supported"); |
| 1207 | |
| 1208 | if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1209 | return -EINVAL; |
| 1210 | |
| 1211 | format->fmt.pix.width = dev->width; |
| 1212 | format->fmt.pix.height = dev->height; |
| 1213 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 1214 | format->fmt.pix.bytesperline = dev->bytesperline; |
| 1215 | format->fmt.pix.sizeimage = dev->frame_size; |
| 1216 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 1217 | format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ |
| 1218 | |
| 1219 | em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width, |
| 1220 | dev->height); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | case VIDIOC_TRY_FMT: |
| 1225 | case VIDIOC_S_FMT: |
| 1226 | { |
| 1227 | struct v4l2_format *format = arg; |
| 1228 | u32 i; |
| 1229 | int ret = 0; |
| 1230 | int width = format->fmt.pix.width; |
| 1231 | int height = format->fmt.pix.height; |
| 1232 | unsigned int hscale, vscale; |
| 1233 | unsigned int maxh, maxw; |
| 1234 | |
| 1235 | maxw = norm_maxw(dev); |
| 1236 | maxh = norm_maxh(dev); |
| 1237 | |
| 1238 | /* int both_fields; */ |
| 1239 | |
| 1240 | em2820_videodbg("%s: type=%s", |
| 1241 | cmd == |
| 1242 | VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" : |
| 1243 | "VIDIOC_S_FMT", |
| 1244 | format->type == |
| 1245 | V4L2_BUF_TYPE_VIDEO_CAPTURE ? |
| 1246 | "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type == |
| 1247 | V4L2_BUF_TYPE_VBI_CAPTURE ? |
| 1248 | "V4L2_BUF_TYPE_VBI_CAPTURE " : |
| 1249 | "not supported"); |
| 1250 | |
| 1251 | if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1252 | return -EINVAL; |
| 1253 | |
| 1254 | em2820_videodbg("%s: requested %dx%d", |
| 1255 | cmd == |
| 1256 | VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" : |
| 1257 | "VIDIOC_S_FMT", format->fmt.pix.width, |
| 1258 | format->fmt.pix.height); |
| 1259 | |
| 1260 | /* FIXME: Move some code away from here */ |
| 1261 | /* width must even because of the YUYV format */ |
| 1262 | /* height must be even because of interlacing */ |
| 1263 | height &= 0xfffe; |
| 1264 | width &= 0xfffe; |
| 1265 | |
| 1266 | if (height < 32) |
| 1267 | height = 32; |
| 1268 | if (height > maxh) |
| 1269 | height = maxh; |
| 1270 | if (width < 48) |
| 1271 | width = 48; |
| 1272 | if (width > maxw) |
| 1273 | width = maxw; |
| 1274 | |
Sascha Sommer | 74458e6 | 2005-11-08 21:37:33 -0800 | [diff] [blame^] | 1275 | /* FIXME*/ |
| 1276 | if(dev->is_em2800){ |
| 1277 | /* we only know how to scale to 50% */ |
| 1278 | if(height % (maxh / 2)) |
| 1279 | height=maxh; |
| 1280 | if(width % (maxw / 2)) |
| 1281 | width=maxw; |
| 1282 | /* larger resoltion don't seem to work either */ |
| 1283 | if(width == maxw && height == maxh) |
| 1284 | width /= 2; |
| 1285 | } |
| 1286 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1287 | if ((hscale = |
| 1288 | (((unsigned long)maxw) << 12) / width - 4096L) >= |
| 1289 | 0x4000) |
| 1290 | hscale = 0x3fff; |
| 1291 | width = |
| 1292 | (((unsigned long)maxw) << 12) / (hscale + 4096L); |
| 1293 | |
| 1294 | if ((vscale = |
| 1295 | (((unsigned long)maxh) << 12) / height - 4096L) >= |
| 1296 | 0x4000) |
| 1297 | vscale = 0x3fff; |
| 1298 | height = |
| 1299 | (((unsigned long)maxh) << 12) / (vscale + 4096L); |
| 1300 | |
| 1301 | format->fmt.pix.width = width; |
| 1302 | format->fmt.pix.height = height; |
| 1303 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 1304 | format->fmt.pix.bytesperline = width * 2; |
| 1305 | format->fmt.pix.sizeimage = width * 2 * height; |
| 1306 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 1307 | format->fmt.pix.field = V4L2_FIELD_INTERLACED; |
| 1308 | |
| 1309 | em2820_videodbg("%s: returned %dx%d (%d, %d)", |
| 1310 | cmd == |
| 1311 | VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" : |
| 1312 | "VIDIOC_S_FMT", format->fmt.pix.width, |
| 1313 | format->fmt.pix.height, hscale, vscale); |
| 1314 | |
| 1315 | if (cmd == VIDIOC_TRY_FMT) |
| 1316 | return 0; |
| 1317 | |
| 1318 | for (i = 0; i < dev->num_frames; i++) |
| 1319 | if (dev->frame[i].vma_use_count) { |
| 1320 | em2820_videodbg("VIDIOC_S_FMT failed. " |
| 1321 | "Unmap the buffers first."); |
| 1322 | return -EINVAL; |
| 1323 | } |
| 1324 | |
| 1325 | /* stop io in case it is already in progress */ |
| 1326 | if (dev->stream == STREAM_ON) { |
| 1327 | em2820_videodbg("VIDIOC_SET_FMT: interupting stream"); |
| 1328 | if ((ret = em2820_stream_interrupt(dev))) |
| 1329 | return ret; |
| 1330 | } |
| 1331 | |
| 1332 | em2820_release_buffers(dev); |
| 1333 | dev->io = IO_NONE; |
| 1334 | |
| 1335 | /* set new image size */ |
| 1336 | dev->width = width; |
| 1337 | dev->height = height; |
| 1338 | dev->frame_size = dev->width * dev->height * 2; |
| 1339 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ |
| 1340 | dev->bytesperline = dev->width * 2; |
| 1341 | dev->hscale = hscale; |
| 1342 | dev->vscale = vscale; |
| 1343 | /* dev->both_fileds = both_fileds; */ |
| 1344 | em2820_uninit_isoc(dev); |
| 1345 | em2820_set_alternate(dev); |
| 1346 | em2820_capture_start(dev, 1); |
| 1347 | em2820_resolution_set(dev); |
| 1348 | em2820_init_isoc(dev); |
| 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | /* --- streaming capture ------------------------------------- */ |
| 1354 | case VIDIOC_REQBUFS: |
| 1355 | { |
| 1356 | struct v4l2_requestbuffers *rb = arg; |
| 1357 | u32 i; |
| 1358 | int ret; |
| 1359 | |
| 1360 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1361 | rb->memory != V4L2_MEMORY_MMAP) |
| 1362 | return -EINVAL; |
| 1363 | |
| 1364 | if (dev->io == IO_READ) { |
| 1365 | em2820_videodbg ("method is set to read;" |
| 1366 | " close and open the device again to" |
| 1367 | " choose the mmap I/O method"); |
| 1368 | return -EINVAL; |
| 1369 | } |
| 1370 | |
| 1371 | for (i = 0; i < dev->num_frames; i++) |
| 1372 | if (dev->frame[i].vma_use_count) { |
| 1373 | em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped"); |
| 1374 | return -EINVAL; |
| 1375 | } |
| 1376 | |
| 1377 | if (dev->stream == STREAM_ON) { |
| 1378 | em2820_videodbg("VIDIOC_REQBUFS: interrupting stream"); |
| 1379 | if ((ret = em2820_stream_interrupt(dev))) |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
| 1383 | em2820_empty_framequeues(dev); |
| 1384 | |
| 1385 | em2820_release_buffers(dev); |
| 1386 | if (rb->count) |
| 1387 | rb->count = |
| 1388 | em2820_request_buffers(dev, rb->count); |
| 1389 | |
| 1390 | dev->frame_current = NULL; |
| 1391 | |
| 1392 | em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i", |
| 1393 | rb->count); |
| 1394 | dev->io = rb->count ? IO_MMAP : IO_NONE; |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | case VIDIOC_QUERYBUF: |
| 1399 | { |
| 1400 | struct v4l2_buffer *b = arg; |
| 1401 | |
| 1402 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1403 | b->index >= dev->num_frames || dev->io != IO_MMAP) |
| 1404 | return -EINVAL; |
| 1405 | |
| 1406 | memcpy(b, &dev->frame[b->index].buf, sizeof(*b)); |
| 1407 | |
| 1408 | if (dev->frame[b->index].vma_use_count) { |
| 1409 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 1410 | } |
| 1411 | if (dev->frame[b->index].state == F_DONE) |
| 1412 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 1413 | else if (dev->frame[b->index].state != F_UNUSED) |
| 1414 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 1415 | return 0; |
| 1416 | } |
| 1417 | case VIDIOC_QBUF: |
| 1418 | { |
| 1419 | struct v4l2_buffer *b = arg; |
| 1420 | unsigned long lock_flags; |
| 1421 | |
| 1422 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1423 | b->index >= dev->num_frames || dev->io != IO_MMAP) { |
| 1424 | return -EINVAL; |
| 1425 | } |
| 1426 | |
| 1427 | if (dev->frame[b->index].state != F_UNUSED) { |
| 1428 | return -EAGAIN; |
| 1429 | } |
| 1430 | dev->frame[b->index].state = F_QUEUED; |
| 1431 | |
| 1432 | /* add frame to fifo */ |
| 1433 | spin_lock_irqsave(&dev->queue_lock, lock_flags); |
| 1434 | list_add_tail(&dev->frame[b->index].frame, |
| 1435 | &dev->inqueue); |
| 1436 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); |
| 1437 | |
| 1438 | return 0; |
| 1439 | } |
| 1440 | case VIDIOC_DQBUF: |
| 1441 | { |
| 1442 | struct v4l2_buffer *b = arg; |
| 1443 | struct em2820_frame_t *f; |
| 1444 | unsigned long lock_flags; |
| 1445 | int ret = 0; |
| 1446 | |
| 1447 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1448 | || dev->io != IO_MMAP) |
| 1449 | return -EINVAL; |
| 1450 | |
| 1451 | if (list_empty(&dev->outqueue)) { |
| 1452 | if (dev->stream == STREAM_OFF) |
| 1453 | return -EINVAL; |
| 1454 | if (filp->f_flags & O_NONBLOCK) |
| 1455 | return -EAGAIN; |
| 1456 | ret = wait_event_interruptible |
| 1457 | (dev->wait_frame, |
| 1458 | (!list_empty(&dev->outqueue)) || |
| 1459 | (dev->state & DEV_DISCONNECTED)); |
| 1460 | if (ret) |
| 1461 | return ret; |
| 1462 | if (dev->state & DEV_DISCONNECTED) |
| 1463 | return -ENODEV; |
| 1464 | } |
| 1465 | |
| 1466 | spin_lock_irqsave(&dev->queue_lock, lock_flags); |
| 1467 | f = list_entry(dev->outqueue.next, |
| 1468 | struct em2820_frame_t, frame); |
| 1469 | list_del(dev->outqueue.next); |
| 1470 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); |
| 1471 | |
| 1472 | f->state = F_UNUSED; |
| 1473 | memcpy(b, &f->buf, sizeof(*b)); |
| 1474 | |
| 1475 | if (f->vma_use_count) |
| 1476 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 1477 | |
| 1478 | return 0; |
| 1479 | } |
| 1480 | default: |
| 1481 | return em2820_do_ioctl(inode, filp, dev, cmd, arg, |
| 1482 | em2820_video_do_ioctl); |
| 1483 | } |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * em2820_v4l2_ioctl() |
| 1489 | * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl() |
| 1490 | */ |
| 1491 | static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp, |
| 1492 | unsigned int cmd, unsigned long arg) |
| 1493 | { |
| 1494 | struct em2820 *dev = video_get_drvdata(video_devdata(filp)); |
| 1495 | int ret = 0; |
| 1496 | |
| 1497 | if (down_interruptible(&dev->fileop_lock)) |
| 1498 | return -ERESTARTSYS; |
| 1499 | |
| 1500 | if (dev->state & DEV_DISCONNECTED) { |
| 1501 | em2820_errdev("v4l2 ioctl: device not present\n"); |
| 1502 | up(&dev->fileop_lock); |
| 1503 | return -ENODEV; |
| 1504 | } |
| 1505 | |
| 1506 | if (dev->state & DEV_MISCONFIGURED) { |
| 1507 | em2820_errdev |
| 1508 | ("v4l2 ioctl: device is misconfigured; close and open it again\n"); |
| 1509 | up(&dev->fileop_lock); |
| 1510 | return -EIO; |
| 1511 | } |
| 1512 | |
| 1513 | ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl); |
| 1514 | |
| 1515 | up(&dev->fileop_lock); |
| 1516 | |
| 1517 | return ret; |
| 1518 | } |
| 1519 | |
| 1520 | static struct file_operations em2820_v4l_fops = { |
| 1521 | .owner = THIS_MODULE, |
| 1522 | .open = em2820_v4l2_open, |
| 1523 | .release = em2820_v4l2_close, |
| 1524 | .ioctl = em2820_v4l2_ioctl, |
| 1525 | .read = em2820_v4l2_read, |
| 1526 | .poll = em2820_v4l2_poll, |
| 1527 | .mmap = em2820_v4l2_mmap, |
| 1528 | .llseek = no_llseek, |
| 1529 | }; |
| 1530 | |
| 1531 | /******************************** usb interface *****************************************/ |
| 1532 | |
| 1533 | /* |
| 1534 | * em2820_init_dev() |
| 1535 | * allocates and inits the device structs, registers i2c bus and v4l device |
| 1536 | */ |
| 1537 | static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev, |
| 1538 | int minor, int model) |
| 1539 | { |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1540 | struct em2820 *dev = *devhandle; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1541 | int retval = -ENOMEM; |
| 1542 | int errCode, i; |
| 1543 | unsigned int maxh, maxw; |
| 1544 | struct usb_interface *uif; |
| 1545 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1546 | dev->udev = udev; |
| 1547 | dev->model = model; |
| 1548 | init_MUTEX(&dev->lock); |
| 1549 | init_waitqueue_head(&dev->open); |
| 1550 | |
| 1551 | dev->em2820_write_regs = em2820_write_regs; |
| 1552 | dev->em2820_read_reg = em2820_read_reg; |
| 1553 | dev->em2820_read_reg_req_len = em2820_read_reg_req_len; |
| 1554 | dev->em2820_write_regs_req = em2820_write_regs_req; |
| 1555 | dev->em2820_read_reg_req = em2820_read_reg_req; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1556 | dev->is_em2800 = em2820_boards[model].is_em2800; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1557 | dev->has_tuner = em2820_boards[model].has_tuner; |
| 1558 | dev->has_msp34xx = em2820_boards[model].has_msp34xx; |
| 1559 | dev->tda9887_conf = em2820_boards[model].tda9887_conf; |
| 1560 | dev->decoder = em2820_boards[model].decoder; |
| 1561 | |
| 1562 | if (tuner >= 0) |
| 1563 | dev->tuner_type = tuner; |
| 1564 | else |
| 1565 | dev->tuner_type = em2820_boards[model].tuner_type; |
| 1566 | |
| 1567 | dev->video_inputs = em2820_boards[model].vchannels; |
| 1568 | |
| 1569 | for (i = 0; i < TVNORMS; i++) |
| 1570 | if (em2820_boards[model].norm == tvnorms[i].mode) |
| 1571 | break; |
| 1572 | if (i == TVNORMS) |
| 1573 | i = 0; |
| 1574 | |
| 1575 | dev->tvnorm = &tvnorms[i]; /* set default norm */ |
| 1576 | |
| 1577 | em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name); |
| 1578 | |
| 1579 | maxw = norm_maxw(dev); |
| 1580 | maxh = norm_maxh(dev); |
| 1581 | |
| 1582 | /* set default image size */ |
| 1583 | dev->width = maxw; |
| 1584 | dev->height = maxh; |
| 1585 | dev->interlaced = EM2820_INTERLACED_DEFAULT; |
| 1586 | dev->field_size = dev->width * dev->height; |
| 1587 | dev->frame_size = |
| 1588 | dev->interlaced ? dev->field_size << 1 : dev->field_size; |
| 1589 | dev->bytesperline = dev->width * 2; |
| 1590 | dev->hscale = 0; |
| 1591 | dev->vscale = 0; |
| 1592 | dev->ctl_input = 2; |
| 1593 | |
| 1594 | /* setup video picture settings for saa7113h */ |
| 1595 | memset(&dev->vpic, 0, sizeof(dev->vpic)); |
| 1596 | dev->vpic.colour = 128 << 8; |
| 1597 | dev->vpic.hue = 128 << 8; |
| 1598 | dev->vpic.brightness = 128 << 8; |
| 1599 | dev->vpic.contrast = 192 << 8; |
| 1600 | dev->vpic.whiteness = 128 << 8; /* This one isn't used */ |
| 1601 | dev->vpic.depth = 16; |
| 1602 | dev->vpic.palette = VIDEO_PALETTE_YUV422; |
| 1603 | |
| 1604 | /* compute alternate max packet sizes */ |
| 1605 | uif = dev->udev->actconfig->interface[0]; |
| 1606 | dev->alt_max_pkt_size[0] = 0; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1607 | for (i = 1; i <= EM2820_MAX_ALT && i < uif->num_altsetting ; i++) { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1608 | u16 tmp = |
| 1609 | le16_to_cpu(uif->altsetting[i].endpoint[1].desc. |
| 1610 | wMaxPacketSize); |
| 1611 | dev->alt_max_pkt_size[i] = |
| 1612 | (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1); |
| 1613 | } |
| 1614 | |
| 1615 | #ifdef CONFIG_MODULES |
| 1616 | /* request some modules */ |
| 1617 | if (dev->decoder == EM2820_SAA7113) |
| 1618 | request_module("saa7113"); |
| 1619 | if (dev->decoder == EM2820_SAA7114) |
| 1620 | request_module("saa7114"); |
| 1621 | if (dev->decoder == EM2820_TVP5150) |
| 1622 | request_module("tvp5150"); |
| 1623 | if (dev->has_tuner) |
| 1624 | request_module("tuner"); |
| 1625 | if (dev->tda9887_conf) |
| 1626 | request_module("tda9887"); |
| 1627 | #endif |
| 1628 | errCode = em2820_config(dev); |
| 1629 | if (errCode) { |
| 1630 | em2820_errdev("error configuring device\n"); |
| 1631 | kfree(dev); |
| 1632 | return -ENOMEM; |
| 1633 | } |
| 1634 | |
| 1635 | down(&dev->lock); |
| 1636 | /* register i2c bus */ |
| 1637 | em2820_i2c_register(dev); |
| 1638 | |
| 1639 | /* Do board specific init and eeprom reading */ |
| 1640 | em2820_card_setup(dev); |
| 1641 | |
| 1642 | /* configure the device */ |
| 1643 | em2820_config_i2c(dev); |
| 1644 | |
| 1645 | up(&dev->lock); |
| 1646 | |
| 1647 | errCode = em2820_config(dev); |
| 1648 | |
| 1649 | #ifdef CONFIG_MODULES |
| 1650 | if (dev->has_msp34xx) |
| 1651 | request_module("msp3400"); |
| 1652 | #endif |
| 1653 | /* allocate and fill v4l2 device struct */ |
| 1654 | dev->vdev = video_device_alloc(); |
| 1655 | if (NULL == dev->vdev) { |
| 1656 | em2820_errdev("cannot allocate video_device.\n"); |
| 1657 | kfree(dev); |
| 1658 | return -ENOMEM; |
| 1659 | } |
| 1660 | |
| 1661 | dev->vdev->owner = THIS_MODULE; |
| 1662 | dev->vdev->type = VID_TYPE_CAPTURE; |
| 1663 | if (dev->has_tuner) |
| 1664 | dev->vdev->type |= VID_TYPE_TUNER; |
| 1665 | dev->vdev->hardware = 0; |
| 1666 | dev->vdev->fops = &em2820_v4l_fops; |
| 1667 | dev->vdev->minor = -1; |
| 1668 | dev->vdev->dev = &dev->udev->dev; |
| 1669 | dev->vdev->release = video_device_release; |
| 1670 | snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s", |
| 1671 | "em2820 video"); |
| 1672 | video_set_drvdata(dev->vdev, dev); |
| 1673 | |
| 1674 | /* register v4l2 device */ |
| 1675 | down(&dev->lock); |
| 1676 | if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) { |
| 1677 | em2820_errdev("unable to register video device (error=%i).\n", |
| 1678 | retval); |
| 1679 | up(&dev->lock); |
| 1680 | video_set_drvdata(dev->vdev, NULL); |
| 1681 | video_device_release(dev->vdev); |
| 1682 | kfree(dev); |
| 1683 | return -ENODEV; |
| 1684 | } |
| 1685 | if (dev->has_msp34xx) { |
| 1686 | /* Send a reset to other chips via gpio */ |
| 1687 | em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1); |
| 1688 | udelay(2500); |
| 1689 | em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1); |
| 1690 | udelay(2500); |
| 1691 | |
| 1692 | } |
| 1693 | video_mux(dev, 0); |
| 1694 | |
| 1695 | up(&dev->lock); |
| 1696 | |
| 1697 | em2820_info("V4L2 device registered as /dev/video%d\n", |
| 1698 | dev->vdev->minor); |
| 1699 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
| 1704 | * em2820_usb_probe() |
| 1705 | * checks for supported devices |
| 1706 | */ |
| 1707 | static int em2820_usb_probe(struct usb_interface *interface, |
| 1708 | const struct usb_device_id *id) |
| 1709 | { |
| 1710 | const struct usb_endpoint_descriptor *endpoint; |
| 1711 | struct usb_device *udev; |
| 1712 | struct em2820 *dev = NULL; |
| 1713 | int retval = -ENODEV; |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1714 | int model,i,nr,ifnum; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1715 | |
| 1716 | udev = usb_get_dev(interface_to_usbdev(interface)); |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1717 | ifnum = interface->altsetting[0].desc.bInterfaceNumber; |
| 1718 | |
| 1719 | em2820_err(DRIVER_NAME " new device (%04x:%04x): interface %i, class %i\n", |
| 1720 | udev->descriptor.idVendor,udev->descriptor.idProduct, |
| 1721 | ifnum, |
| 1722 | interface->altsetting[0].desc.bInterfaceClass); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1723 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1724 | /* Don't register audio interfaces */ |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1725 | if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1726 | return -ENODEV; |
| 1727 | |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1728 | endpoint = &interface->cur_altsetting->endpoint[1].desc; |
| 1729 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1730 | /* check if the the device has the iso in endpoint at the correct place */ |
| 1731 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != |
| 1732 | USB_ENDPOINT_XFER_ISOC) { |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1733 | em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1734 | return -ENODEV; |
| 1735 | } |
| 1736 | if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1737 | em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1738 | return -ENODEV; |
| 1739 | } |
| 1740 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1741 | model=id->driver_info; |
| 1742 | nr=interface->minor; |
| 1743 | |
| 1744 | if (nr>EM2820_MAXBOARDS) { |
| 1745 | printk ("em2820: Supports only %i em28xx boards.\n",EM2820_MAXBOARDS); |
| 1746 | return -ENOMEM; |
| 1747 | } |
| 1748 | |
| 1749 | /* allocate memory for our device state and initialize it */ |
| 1750 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); |
| 1751 | if (dev == NULL) { |
| 1752 | em2820_err(DRIVER_NAME ": out of memory!\n"); |
| 1753 | return -ENOMEM; |
| 1754 | } |
| 1755 | memset(dev, 0, sizeof(*dev)); |
| 1756 | |
| 1757 | snprintf(dev->name, 29, "em2820 #%d", nr); |
| 1758 | |
| 1759 | if ((card[nr]>=0)&&(card[nr]<em2820_bcount)) |
| 1760 | model=card[nr]; |
| 1761 | |
| 1762 | if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) { |
| 1763 | printk( "%s: Your board has no eeprom inside it and thus can't\n" |
| 1764 | "%s: be autodetected. Please pass card=<n> insmod option to\n" |
| 1765 | "%s: workaround that. Redirect complaints to the vendor of\n" |
| 1766 | "%s: the TV card. Best regards,\n" |
| 1767 | "%s: -- tux\n", |
| 1768 | dev->name,dev->name,dev->name,dev->name,dev->name); |
| 1769 | printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n", |
| 1770 | dev->name); |
| 1771 | for (i = 0; i < em2820_bcount; i++) { |
| 1772 | printk("%s: card=%d -> %s\n", |
| 1773 | dev->name, i, em2820_boards[i].name); |
| 1774 | } |
| 1775 | } |
| 1776 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1777 | /* allocate device struct */ |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1778 | retval = em2820_init_dev(&dev, udev, nr, model); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1779 | if (retval) |
| 1780 | return retval; |
| 1781 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1782 | em2820_info("Found %s\n", em2820_boards[model].name); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1783 | |
| 1784 | /* save our data pointer in this interface device */ |
| 1785 | usb_set_intfdata(interface, dev); |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * em2820_usb_disconnect() |
| 1791 | * called when the device gets diconencted |
| 1792 | * video device will be unregistered on v4l2_close in case it is still open |
| 1793 | */ |
| 1794 | static void em2820_usb_disconnect(struct usb_interface *interface) |
| 1795 | { |
| 1796 | struct em2820 *dev = usb_get_intfdata(interface); |
| 1797 | usb_set_intfdata(interface, NULL); |
| 1798 | |
| 1799 | if (!dev) |
| 1800 | return; |
| 1801 | |
| 1802 | down_write(&em2820_disconnect); |
| 1803 | |
| 1804 | down(&dev->lock); |
| 1805 | |
| 1806 | em2820_info("disconnecting %s\n", dev->vdev->name); |
| 1807 | |
| 1808 | wake_up_interruptible_all(&dev->open); |
| 1809 | |
| 1810 | if (dev->users) { |
| 1811 | em2820_warn |
| 1812 | ("device /dev/video%d is open! Deregistration and memory " |
| 1813 | "deallocation are deferred on close.\n", dev->vdev->minor); |
| 1814 | dev->state |= DEV_MISCONFIGURED; |
| 1815 | em2820_uninit_isoc(dev); |
| 1816 | dev->state |= DEV_DISCONNECTED; |
| 1817 | wake_up_interruptible(&dev->wait_frame); |
| 1818 | wake_up_interruptible(&dev->wait_stream); |
| 1819 | } else { |
| 1820 | dev->state |= DEV_DISCONNECTED; |
| 1821 | em2820_release_resources(dev); |
| 1822 | } |
| 1823 | |
| 1824 | up(&dev->lock); |
| 1825 | |
| 1826 | if (!dev->users) |
| 1827 | kfree(dev); |
| 1828 | |
| 1829 | up_write(&em2820_disconnect); |
| 1830 | |
| 1831 | } |
| 1832 | |
| 1833 | static struct usb_driver em2820_usb_driver = { |
| 1834 | .owner = THIS_MODULE, |
| 1835 | .name = "em2820", |
| 1836 | .probe = em2820_usb_probe, |
| 1837 | .disconnect = em2820_usb_disconnect, |
| 1838 | .id_table = em2820_id_table, |
| 1839 | }; |
| 1840 | |
| 1841 | static int __init em2820_module_init(void) |
| 1842 | { |
| 1843 | int result; |
| 1844 | |
| 1845 | printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n", |
| 1846 | (EM2820_VERSION_CODE >> 16) & 0xff, |
| 1847 | (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff); |
| 1848 | #ifdef SNAPSHOT |
| 1849 | printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n", |
| 1850 | SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100); |
| 1851 | #endif |
| 1852 | |
| 1853 | /* register this driver with the USB subsystem */ |
| 1854 | result = usb_register(&em2820_usb_driver); |
| 1855 | if (result) |
| 1856 | em2820_err(DRIVER_NAME |
| 1857 | " usb_register failed. Error number %d.\n", result); |
| 1858 | |
| 1859 | return result; |
| 1860 | } |
| 1861 | |
| 1862 | static void __exit em2820_module_exit(void) |
| 1863 | { |
| 1864 | /* deregister this driver with the USB subsystem */ |
| 1865 | usb_deregister(&em2820_usb_driver); |
| 1866 | } |
| 1867 | |
| 1868 | module_init(em2820_module_init); |
| 1869 | module_exit(em2820_module_exit); |