Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/sbus/char/vfc_dev.c |
| 3 | * |
| 4 | * Driver for the Videopix Frame Grabber. |
| 5 | * |
| 6 | * In order to use the VFC you need to program the video controller |
| 7 | * chip. This chip is the Phillips SAA9051. You need to call their |
| 8 | * documentation ordering line to get the docs. |
| 9 | * |
| 10 | * There is very little documentation on the VFC itself. There is |
| 11 | * some useful info that can be found in the manuals that come with |
| 12 | * the card. I will hopefully write some better docs at a later date. |
| 13 | * |
| 14 | * Copyright (C) 1996 Manish Vachharajani (mvachhar@noc.rutgers.edu) |
| 15 | * */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/fs.h> |
| 23 | #include <linux/smp_lock.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/mm.h> |
| 27 | |
| 28 | #include <asm/openprom.h> |
| 29 | #include <asm/oplib.h> |
| 30 | #include <asm/io.h> |
| 31 | #include <asm/system.h> |
| 32 | #include <asm/sbus.h> |
| 33 | #include <asm/page.h> |
| 34 | #include <asm/pgtable.h> |
| 35 | #include <asm/uaccess.h> |
| 36 | |
| 37 | #define VFC_MAJOR (60) |
| 38 | |
| 39 | #if 0 |
| 40 | #define VFC_IOCTL_DEBUG |
| 41 | #endif |
| 42 | |
| 43 | #include "vfc.h" |
| 44 | #include <asm/vfc_ioctls.h> |
| 45 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 46 | static const struct file_operations vfc_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct vfc_dev **vfc_dev_lst; |
| 48 | static char vfcstr[]="vfc"; |
| 49 | static unsigned char saa9051_init_array[VFC_SAA9051_NR] = { |
| 50 | 0x00, 0x64, 0x72, 0x52, |
| 51 | 0x36, 0x18, 0xff, 0x20, |
| 52 | 0xfc, 0x77, 0xe3, 0x50, |
| 53 | 0x3e |
| 54 | }; |
| 55 | |
| 56 | void vfc_lock_device(struct vfc_dev *dev) |
| 57 | { |
| 58 | down(&dev->device_lock_sem); |
| 59 | } |
| 60 | |
| 61 | void vfc_unlock_device(struct vfc_dev *dev) |
| 62 | { |
| 63 | up(&dev->device_lock_sem); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void vfc_captstat_reset(struct vfc_dev *dev) |
| 68 | { |
| 69 | dev->control_reg |= VFC_CONTROL_CAPTRESET; |
| 70 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 71 | dev->control_reg &= ~VFC_CONTROL_CAPTRESET; |
| 72 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 73 | dev->control_reg |= VFC_CONTROL_CAPTRESET; |
| 74 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 75 | } |
| 76 | |
| 77 | void vfc_memptr_reset(struct vfc_dev *dev) |
| 78 | { |
| 79 | dev->control_reg |= VFC_CONTROL_MEMPTR; |
| 80 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 81 | dev->control_reg &= ~VFC_CONTROL_MEMPTR; |
| 82 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 83 | dev->control_reg |= VFC_CONTROL_MEMPTR; |
| 84 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 85 | } |
| 86 | |
| 87 | int vfc_csr_init(struct vfc_dev *dev) |
| 88 | { |
| 89 | dev->control_reg = 0x80000000; |
| 90 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 91 | udelay(200); |
| 92 | dev->control_reg &= ~0x80000000; |
| 93 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 94 | udelay(100); |
| 95 | sbus_writel(0x0f000000, &dev->regs->i2c_magic2); |
| 96 | |
| 97 | vfc_memptr_reset(dev); |
| 98 | |
| 99 | dev->control_reg &= ~VFC_CONTROL_DIAGMODE; |
| 100 | dev->control_reg &= ~VFC_CONTROL_CAPTURE; |
| 101 | dev->control_reg |= 0x40000000; |
| 102 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 103 | |
| 104 | vfc_captstat_reset(dev); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int vfc_saa9051_init(struct vfc_dev *dev) |
| 110 | { |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < VFC_SAA9051_NR; i++) |
| 114 | dev->saa9051_state_array[i] = saa9051_init_array[i]; |
| 115 | |
| 116 | vfc_i2c_sendbuf(dev,VFC_SAA9051_ADDR, |
| 117 | dev->saa9051_state_array, VFC_SAA9051_NR); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int init_vfc_hw(struct vfc_dev *dev) |
| 122 | { |
| 123 | vfc_lock_device(dev); |
| 124 | vfc_csr_init(dev); |
| 125 | |
| 126 | vfc_pcf8584_init(dev); |
| 127 | vfc_init_i2c_bus(dev); /* hopefully this doesn't undo the magic |
| 128 | sun code above*/ |
| 129 | vfc_saa9051_init(dev); |
| 130 | vfc_unlock_device(dev); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int init_vfc_devstruct(struct vfc_dev *dev, int instance) |
| 135 | { |
| 136 | dev->instance=instance; |
| 137 | init_MUTEX(&dev->device_lock_sem); |
| 138 | dev->control_reg=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | dev->busy=0; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance) |
| 144 | { |
| 145 | if(dev == NULL) { |
| 146 | printk(KERN_ERR "VFC: Bogus pointer passed\n"); |
| 147 | return -ENOMEM; |
| 148 | } |
| 149 | printk("Initializing vfc%d\n",instance); |
| 150 | dev->regs = NULL; |
Al Viro | b7c690b | 2005-12-06 05:50:56 -0500 | [diff] [blame] | 151 | dev->regs = (volatile struct vfc_regs __iomem *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | sbus_ioremap(&sdev->resource[0], 0, |
| 153 | sizeof(struct vfc_regs), vfcstr); |
| 154 | dev->which_io = sdev->reg_addrs[0].which_io; |
| 155 | dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr; |
| 156 | if (dev->regs == NULL) |
| 157 | return -EIO; |
| 158 | |
| 159 | printk("vfc%d: registers mapped at phys_addr: 0x%lx\n virt_addr: 0x%lx\n", |
| 160 | instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs); |
| 161 | |
| 162 | if (init_vfc_devstruct(dev, instance)) |
| 163 | return -EINVAL; |
| 164 | if (init_vfc_hw(dev)) |
| 165 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | struct vfc_dev *vfc_get_dev_ptr(int instance) |
| 171 | { |
| 172 | return vfc_dev_lst[instance]; |
| 173 | } |
| 174 | |
| 175 | static DEFINE_SPINLOCK(vfc_dev_lock); |
| 176 | |
| 177 | static int vfc_open(struct inode *inode, struct file *file) |
| 178 | { |
| 179 | struct vfc_dev *dev; |
| 180 | |
| 181 | spin_lock(&vfc_dev_lock); |
| 182 | dev = vfc_get_dev_ptr(iminor(inode)); |
| 183 | if (dev == NULL) { |
| 184 | spin_unlock(&vfc_dev_lock); |
| 185 | return -ENODEV; |
| 186 | } |
| 187 | if (dev->busy) { |
| 188 | spin_unlock(&vfc_dev_lock); |
| 189 | return -EBUSY; |
| 190 | } |
| 191 | |
| 192 | dev->busy = 1; |
| 193 | spin_unlock(&vfc_dev_lock); |
| 194 | |
| 195 | vfc_lock_device(dev); |
| 196 | |
| 197 | vfc_csr_init(dev); |
| 198 | vfc_pcf8584_init(dev); |
| 199 | vfc_init_i2c_bus(dev); |
| 200 | vfc_saa9051_init(dev); |
| 201 | vfc_memptr_reset(dev); |
| 202 | vfc_captstat_reset(dev); |
| 203 | |
| 204 | vfc_unlock_device(dev); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int vfc_release(struct inode *inode,struct file *file) |
| 209 | { |
| 210 | struct vfc_dev *dev; |
| 211 | |
| 212 | spin_lock(&vfc_dev_lock); |
| 213 | dev = vfc_get_dev_ptr(iminor(inode)); |
| 214 | if (!dev || !dev->busy) { |
| 215 | spin_unlock(&vfc_dev_lock); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | dev->busy = 0; |
| 219 | spin_unlock(&vfc_dev_lock); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int vfc_debug(struct vfc_dev *dev, int cmd, void __user *argp) |
| 224 | { |
| 225 | struct vfc_debug_inout inout; |
| 226 | unsigned char *buffer; |
| 227 | |
| 228 | if (!capable(CAP_SYS_ADMIN)) |
| 229 | return -EPERM; |
| 230 | |
| 231 | switch(cmd) { |
| 232 | case VFC_I2C_SEND: |
| 233 | if(copy_from_user(&inout, argp, sizeof(inout))) |
| 234 | return -EFAULT; |
| 235 | |
| 236 | buffer = kmalloc(inout.len, GFP_KERNEL); |
| 237 | if (buffer == NULL) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | if(copy_from_user(buffer, inout.buffer, inout.len)) { |
| 241 | kfree(buffer); |
| 242 | return -EFAULT; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | vfc_lock_device(dev); |
| 247 | inout.ret= |
| 248 | vfc_i2c_sendbuf(dev,inout.addr & 0xff, |
| 249 | buffer,inout.len); |
| 250 | |
| 251 | if (copy_to_user(argp,&inout,sizeof(inout))) { |
| 252 | kfree(buffer); |
| 253 | return -EFAULT; |
| 254 | } |
| 255 | vfc_unlock_device(dev); |
| 256 | |
| 257 | break; |
| 258 | case VFC_I2C_RECV: |
| 259 | if (copy_from_user(&inout, argp, sizeof(inout))) |
| 260 | return -EFAULT; |
| 261 | |
| 262 | buffer = kmalloc(inout.len, GFP_KERNEL); |
| 263 | if (buffer == NULL) |
| 264 | return -ENOMEM; |
| 265 | |
| 266 | memset(buffer,0,inout.len); |
| 267 | vfc_lock_device(dev); |
| 268 | inout.ret= |
| 269 | vfc_i2c_recvbuf(dev,inout.addr & 0xff |
| 270 | ,buffer,inout.len); |
| 271 | vfc_unlock_device(dev); |
| 272 | |
| 273 | if (copy_to_user(inout.buffer, buffer, inout.len)) { |
| 274 | kfree(buffer); |
| 275 | return -EFAULT; |
| 276 | } |
| 277 | if (copy_to_user(argp,&inout,sizeof(inout))) { |
| 278 | kfree(buffer); |
| 279 | return -EFAULT; |
| 280 | } |
| 281 | kfree(buffer); |
| 282 | break; |
| 283 | default: |
| 284 | return -EINVAL; |
| 285 | }; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | int vfc_capture_start(struct vfc_dev *dev) |
| 291 | { |
| 292 | vfc_captstat_reset(dev); |
| 293 | dev->control_reg = sbus_readl(&dev->regs->control); |
| 294 | if((dev->control_reg & VFC_STATUS_CAPTURE)) { |
| 295 | printk(KERN_ERR "vfc%d: vfc capture status not reset\n", |
| 296 | dev->instance); |
| 297 | return -EIO; |
| 298 | } |
| 299 | |
| 300 | vfc_lock_device(dev); |
| 301 | dev->control_reg &= ~VFC_CONTROL_CAPTURE; |
| 302 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 303 | dev->control_reg |= VFC_CONTROL_CAPTURE; |
| 304 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 305 | dev->control_reg &= ~VFC_CONTROL_CAPTURE; |
| 306 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 307 | vfc_unlock_device(dev); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int vfc_capture_poll(struct vfc_dev *dev) |
| 313 | { |
| 314 | int timeout = 1000; |
| 315 | |
| 316 | while (!timeout--) { |
Al Viro | b7c690b | 2005-12-06 05:50:56 -0500 | [diff] [blame] | 317 | if (sbus_readl(&dev->regs->control) & VFC_STATUS_CAPTURE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | break; |
| 319 | vfc_i2c_delay_no_busy(dev, 100); |
| 320 | } |
| 321 | if(!timeout) { |
| 322 | printk(KERN_WARNING "vfc%d: capture timed out\n", |
| 323 | dev->instance); |
| 324 | return -ETIMEDOUT; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | static int vfc_set_control_ioctl(struct inode *inode, struct file *file, |
| 332 | struct vfc_dev *dev, unsigned long arg) |
| 333 | { |
| 334 | int setcmd, ret = 0; |
| 335 | |
| 336 | if (copy_from_user(&setcmd,(void __user *)arg,sizeof(unsigned int))) |
| 337 | return -EFAULT; |
| 338 | |
| 339 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n", |
| 340 | dev->instance,setcmd)); |
| 341 | |
| 342 | switch(setcmd) { |
| 343 | case MEMPRST: |
| 344 | vfc_lock_device(dev); |
| 345 | vfc_memptr_reset(dev); |
| 346 | vfc_unlock_device(dev); |
| 347 | ret=0; |
| 348 | break; |
| 349 | case CAPTRCMD: |
| 350 | vfc_capture_start(dev); |
| 351 | vfc_capture_poll(dev); |
| 352 | break; |
| 353 | case DIAGMODE: |
| 354 | if(capable(CAP_SYS_ADMIN)) { |
| 355 | vfc_lock_device(dev); |
| 356 | dev->control_reg |= VFC_CONTROL_DIAGMODE; |
| 357 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 358 | vfc_unlock_device(dev); |
| 359 | ret = 0; |
| 360 | } else { |
| 361 | ret = -EPERM; |
| 362 | } |
| 363 | break; |
| 364 | case NORMMODE: |
| 365 | vfc_lock_device(dev); |
| 366 | dev->control_reg &= ~VFC_CONTROL_DIAGMODE; |
| 367 | sbus_writel(dev->control_reg, &dev->regs->control); |
| 368 | vfc_unlock_device(dev); |
| 369 | ret = 0; |
| 370 | break; |
| 371 | case CAPTRSTR: |
| 372 | vfc_capture_start(dev); |
| 373 | ret = 0; |
| 374 | break; |
| 375 | case CAPTRWAIT: |
| 376 | vfc_capture_poll(dev); |
| 377 | ret = 0; |
| 378 | break; |
| 379 | default: |
| 380 | ret = -EINVAL; |
| 381 | break; |
| 382 | }; |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | int vfc_port_change_ioctl(struct inode *inode, struct file *file, |
| 389 | struct vfc_dev *dev, unsigned long arg) |
| 390 | { |
| 391 | int ret = 0; |
| 392 | int cmd; |
| 393 | |
| 394 | if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) { |
| 395 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to " |
| 396 | "vfc_port_change_ioctl\n", |
| 397 | dev->instance)); |
| 398 | return -EFAULT; |
| 399 | } |
| 400 | |
| 401 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n", |
| 402 | dev->instance, cmd)); |
| 403 | |
| 404 | switch(cmd) { |
| 405 | case 1: |
| 406 | case 2: |
| 407 | VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72; |
| 408 | VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52; |
| 409 | VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36; |
| 410 | VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18; |
| 411 | VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2; |
| 412 | VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3; |
| 413 | VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e; |
| 414 | break; |
| 415 | case 3: |
| 416 | VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a; |
| 417 | VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17; |
| 418 | VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa; |
| 419 | VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde; |
| 420 | VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = |
| 421 | VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2; |
| 422 | VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC; |
| 423 | VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0; |
| 424 | VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &= |
| 425 | ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1); |
| 426 | break; |
| 427 | default: |
| 428 | ret = -EINVAL; |
| 429 | return ret; |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | switch(cmd) { |
| 434 | case 1: |
| 435 | VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= |
| 436 | (VFC_SAA9051_SS0 | VFC_SAA9051_SS1); |
| 437 | break; |
| 438 | case 2: |
| 439 | VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &= |
| 440 | ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1); |
| 441 | VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0; |
| 442 | break; |
| 443 | case 3: |
| 444 | break; |
| 445 | default: |
| 446 | ret = -EINVAL; |
| 447 | return ret; |
| 448 | break; |
| 449 | } |
| 450 | VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2); |
| 451 | ret=vfc_update_saa9051(dev); |
| 452 | udelay(500); |
| 453 | VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2); |
| 454 | ret=vfc_update_saa9051(dev); |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | int vfc_set_video_ioctl(struct inode *inode, struct file *file, |
| 459 | struct vfc_dev *dev, unsigned long arg) |
| 460 | { |
| 461 | int ret = 0; |
| 462 | int cmd; |
| 463 | |
| 464 | if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) { |
| 465 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to " |
| 466 | "vfc_set_video_ioctl\n", |
| 467 | dev->instance)); |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n", |
| 472 | dev->instance, cmd)); |
| 473 | switch(cmd) { |
| 474 | case STD_NTSC: |
| 475 | VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT; |
| 476 | VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN | |
| 477 | VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS; |
| 478 | ret = vfc_update_saa9051(dev); |
| 479 | break; |
| 480 | case STD_PAL: |
| 481 | VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN | |
| 482 | VFC_SAA9051_CCFR1 | |
| 483 | VFC_SAA9051_CCFR0 | |
| 484 | VFC_SAA9051_FS); |
| 485 | VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT; |
| 486 | ret = vfc_update_saa9051(dev); |
| 487 | break; |
| 488 | |
| 489 | case COLOR_ON: |
| 490 | VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO; |
| 491 | VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &= |
| 492 | ~(VFC_SAA9051_BY | VFC_SAA9051_PF); |
| 493 | ret = vfc_update_saa9051(dev); |
| 494 | break; |
| 495 | case MONO: |
| 496 | VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_CO); |
| 497 | VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) |= |
| 498 | (VFC_SAA9051_BY | VFC_SAA9051_PF); |
| 499 | ret = vfc_update_saa9051(dev); |
| 500 | break; |
| 501 | default: |
| 502 | ret = -EINVAL; |
| 503 | break; |
| 504 | }; |
| 505 | |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | int vfc_get_video_ioctl(struct inode *inode, struct file *file, |
| 510 | struct vfc_dev *dev, unsigned long arg) |
| 511 | { |
| 512 | int ret = 0; |
| 513 | unsigned int status = NO_LOCK; |
| 514 | unsigned char buf[1]; |
| 515 | |
| 516 | if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) { |
| 517 | printk(KERN_ERR "vfc%d: Unable to get status\n", |
| 518 | dev->instance); |
| 519 | return -EIO; |
| 520 | } |
| 521 | |
| 522 | if(buf[0] & VFC_SAA9051_HLOCK) { |
| 523 | status = NO_LOCK; |
| 524 | } else if(buf[0] & VFC_SAA9051_FD) { |
| 525 | if(buf[0] & VFC_SAA9051_CD) |
| 526 | status = NTSC_COLOR; |
| 527 | else |
| 528 | status = NTSC_NOCOLOR; |
| 529 | } else { |
| 530 | if(buf[0] & VFC_SAA9051_CD) |
| 531 | status = PAL_COLOR; |
| 532 | else |
| 533 | status = PAL_NOCOLOR; |
| 534 | } |
| 535 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; " |
| 536 | "buf[0]=%x\n", dev->instance, status, buf[0])); |
| 537 | |
| 538 | if (copy_to_user((void __user *)arg,&status,sizeof(unsigned int))) { |
| 539 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to " |
| 540 | "vfc_get_video_ioctl\n", |
| 541 | dev->instance)); |
| 542 | return ret; |
| 543 | } |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 548 | unsigned long arg) |
| 549 | { |
| 550 | int ret = 0; |
| 551 | unsigned int tmp; |
| 552 | struct vfc_dev *dev; |
| 553 | void __user *argp = (void __user *)arg; |
| 554 | |
| 555 | dev = vfc_get_dev_ptr(iminor(inode)); |
| 556 | if(dev == NULL) |
| 557 | return -ENODEV; |
| 558 | |
| 559 | switch(cmd & 0x0000ffff) { |
| 560 | case VFCGCTRL: |
| 561 | #if 0 |
| 562 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance)); |
| 563 | #endif |
| 564 | tmp = sbus_readl(&dev->regs->control); |
| 565 | if(copy_to_user(argp, &tmp, sizeof(unsigned int))) { |
| 566 | ret = -EFAULT; |
| 567 | break; |
| 568 | } |
| 569 | ret = 0; |
| 570 | break; |
| 571 | case VFCSCTRL: |
| 572 | ret = vfc_set_control_ioctl(inode, file, dev, arg); |
| 573 | break; |
| 574 | case VFCGVID: |
| 575 | ret = vfc_get_video_ioctl(inode, file, dev, arg); |
| 576 | break; |
| 577 | case VFCSVID: |
| 578 | ret = vfc_set_video_ioctl(inode, file, dev, arg); |
| 579 | break; |
| 580 | case VFCHUE: |
| 581 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance)); |
| 582 | if(copy_from_user(&tmp,argp,sizeof(unsigned int))) { |
| 583 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer " |
| 584 | "to IOCTL(VFCHUE)", dev->instance)); |
| 585 | ret = -EFAULT; |
| 586 | } else { |
| 587 | VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp; |
| 588 | vfc_update_saa9051(dev); |
| 589 | ret = 0; |
| 590 | } |
| 591 | break; |
| 592 | case VFCPORTCHG: |
| 593 | ret = vfc_port_change_ioctl(inode, file, dev, arg); |
| 594 | break; |
| 595 | case VFCRDINFO: |
| 596 | ret = -EINVAL; |
| 597 | VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance)); |
| 598 | break; |
| 599 | default: |
| 600 | ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)), cmd, argp); |
| 601 | break; |
| 602 | }; |
| 603 | |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | static int vfc_mmap(struct file *file, struct vm_area_struct *vma) |
| 608 | { |
| 609 | unsigned int map_size, ret, map_offset; |
| 610 | struct vfc_dev *dev; |
| 611 | |
Josef Sipek | 7fa95f7 | 2006-12-08 02:37:36 -0800 | [diff] [blame] | 612 | dev = vfc_get_dev_ptr(iminor(file->f_path.dentry->d_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | if(dev == NULL) |
| 614 | return -ENODEV; |
| 615 | |
| 616 | map_size = vma->vm_end - vma->vm_start; |
| 617 | if(map_size > sizeof(struct vfc_regs)) |
| 618 | map_size = sizeof(struct vfc_regs); |
| 619 | |
| 620 | vma->vm_flags |= |
Christoph Lameter | 68402dd | 2006-06-25 05:46:47 -0700 | [diff] [blame] | 621 | (VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | map_offset = (unsigned int) (long)dev->phys_regs; |
| 623 | ret = io_remap_pfn_range(vma, vma->vm_start, |
| 624 | MK_IOSPACE_PFN(dev->which_io, |
| 625 | map_offset >> PAGE_SHIFT), |
| 626 | map_size, vma->vm_page_prot); |
| 627 | |
| 628 | if(ret) |
| 629 | return -EAGAIN; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 635 | static const struct file_operations vfc_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | .owner = THIS_MODULE, |
| 637 | .llseek = no_llseek, |
| 638 | .ioctl = vfc_ioctl, |
| 639 | .mmap = vfc_mmap, |
| 640 | .open = vfc_open, |
| 641 | .release = vfc_release, |
| 642 | }; |
| 643 | |
| 644 | static int vfc_probe(void) |
| 645 | { |
| 646 | struct sbus_bus *sbus; |
| 647 | struct sbus_dev *sdev = NULL; |
| 648 | int ret; |
| 649 | int instance = 0, cards = 0; |
| 650 | |
| 651 | for_all_sbusdev(sdev, sbus) { |
| 652 | if (strcmp(sdev->prom_name, "vfc") == 0) { |
| 653 | cards++; |
| 654 | continue; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | if (!cards) |
| 659 | return -ENODEV; |
| 660 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 661 | vfc_dev_lst = kmalloc(sizeof(struct vfc_dev *) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | (cards+1), |
| 663 | GFP_KERNEL); |
| 664 | if (vfc_dev_lst == NULL) |
| 665 | return -ENOMEM; |
| 666 | memset(vfc_dev_lst, 0, sizeof(struct vfc_dev *) * (cards + 1)); |
| 667 | vfc_dev_lst[cards] = NULL; |
| 668 | |
| 669 | ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops); |
| 670 | if(ret) { |
| 671 | printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR); |
| 672 | kfree(vfc_dev_lst); |
| 673 | return -EIO; |
| 674 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | instance = 0; |
| 676 | for_all_sbusdev(sdev, sbus) { |
| 677 | if (strcmp(sdev->prom_name, "vfc") == 0) { |
| 678 | vfc_dev_lst[instance]=(struct vfc_dev *) |
| 679 | kmalloc(sizeof(struct vfc_dev), GFP_KERNEL); |
| 680 | if (vfc_dev_lst[instance] == NULL) |
| 681 | return -ENOMEM; |
| 682 | ret = init_vfc_device(sdev, |
| 683 | vfc_dev_lst[instance], |
| 684 | instance); |
| 685 | if(ret) { |
| 686 | printk(KERN_ERR "Unable to initialize" |
| 687 | " vfc%d device\n", |
| 688 | instance); |
| 689 | } else { |
| 690 | } |
| 691 | |
| 692 | instance++; |
| 693 | continue; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | #ifdef MODULE |
| 701 | int init_module(void) |
| 702 | #else |
| 703 | int vfc_init(void) |
| 704 | #endif |
| 705 | { |
| 706 | return vfc_probe(); |
| 707 | } |
| 708 | |
| 709 | #ifdef MODULE |
| 710 | static void deinit_vfc_device(struct vfc_dev *dev) |
| 711 | { |
| 712 | if(dev == NULL) |
| 713 | return; |
Al Viro | b7c690b | 2005-12-06 05:50:56 -0500 | [diff] [blame] | 714 | sbus_iounmap(dev->regs, sizeof(struct vfc_regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | kfree(dev); |
| 716 | } |
| 717 | |
| 718 | void cleanup_module(void) |
| 719 | { |
| 720 | struct vfc_dev **devp; |
| 721 | |
| 722 | unregister_chrdev(VFC_MAJOR,vfcstr); |
| 723 | |
| 724 | for (devp = vfc_dev_lst; *devp; devp++) |
| 725 | deinit_vfc_device(*devp); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | kfree(vfc_dev_lst); |
| 728 | return; |
| 729 | } |
| 730 | #endif |
| 731 | |
| 732 | MODULE_LICENSE("GPL"); |
| 733 | |