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