comparison drivers/sis_vid.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children dc0424327084
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
1 /*
2 *
3 * sis_vid.c
4 *
5 * Copyright (C) 2000 Aaron Holtzman
6 *
7 * This software has been released under the terms of the GNU Public
8 * license. See http://www.gnu.org/copyleft/gpl.html for details.
9 */
10
11 // video4linux interface disabled by A'rpi/ESP-team
12
13
14 //It's entirely possible this major conflicts with something else
15 /* mknod /dev/mga_vid c 178 0 */
16
17 #include <linux/config.h>
18 #include <linux/version.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/malloc.h>
27 #include <linux/pci.h>
28 #include <linux/init.h>
29 //#include <linux/videodev.h>
30
31 #include "sis_vid.h"
32
33 #ifdef CONFIG_MTRR
34 #include <asm/mtrr.h>
35 #endif
36
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
40
41 #define TRUE 1
42 #define FALSE 0
43
44 #define MGA_VID_MAJOR 178
45
46
47 #ifndef PCI_DEVICE_ID_SI_6323
48 #define PCI_DEVICE_ID_SI_6323 0x6326
49 #endif
50
51
52 MODULE_AUTHOR("Aaron Holtzman <aholtzma@engr.uvic.ca>");
53
54
55 typedef struct bes_registers_s
56 {
57 //base address of yuv framebuffer
58 uint32_t yuv_base;
59 uint32_t u_base;
60 uint32_t v_base;
61 uint32_t fb_end;;
62
63 //frame buffer pitch
64 uint32_t pitch;
65
66 //window boundaries
67 uint32_t left;
68 uint32_t right;
69 uint32_t top;
70 uint32_t bottom;
71
72 //control registers
73 uint32_t misc_0;
74 uint32_t misc_1;
75 uint32_t misc_3;
76 uint32_t misc_4;
77
78 //key overlay mode
79 uint32_t key_mode;
80
81 } bes_registers_t;
82
83 static bes_registers_t regs;
84 static uint32_t mga_vid_in_use = 0;
85 static uint32_t vid_src_ready = 0;
86 static uint32_t vid_overlay_on = 0;
87
88 static uint8_t *mga_mmio_base = 0;
89 static uint32_t mga_mem_base = 0;
90 static uint32_t mga_src_base = 0;
91
92 static uint32_t mga_ram_size = 0;
93
94 static struct pci_dev *pci_dev;
95
96 //static struct video_window mga_win;
97 static mga_vid_config_t mga_config;
98
99
100
101 // Backend Scaler registers
102
103 #define MISC_0 0x98
104 #define MISC_1 0x99
105 #define MISC_3 0x9d
106 #define MISC_4 0xb6
107
108
109
110
111 static void mga_vid_frame_sel(int frame)
112 {
113 //we don't need the vcount protection as we're only hitting
114 //one register (and it doesn't seem to be double buffered)
115 //regs.besctl = (regs.besctl & ~0x07000000) + (frame << 25);
116 //writel( regs.besctl, mga_mmio_base + BESCTL );
117 }
118
119
120 #define WRITE_REG(x,y,z) {outb((y),(x));outb((z),(x+1));}
121 #define READ_REG(x,y) (outb((y),(x)),inb(x+1))
122 #define VIDEO_ACCEL 0x3d4
123
124 static void mga_vid_write_regs(void)
125 {
126 uint32_t foo;
127
128 //unlock the video accel registers
129 WRITE_REG(VIDEO_ACCEL,0x80,0x86);
130 foo = READ_REG(VIDEO_ACCEL,0x80);
131
132 if(foo != 0xa1)
133 return; //something bad happened
134
135 //setup the horizontal window bounds
136 WRITE_REG(VIDEO_ACCEL,0x81,regs.left & 0xff);
137 WRITE_REG(VIDEO_ACCEL,0x82,regs.right & 0xff);
138 WRITE_REG(VIDEO_ACCEL,0x83,(regs.left >> 8) | ((regs.right>>4) & 0x70));
139
140 //setup the vertical window bounds
141 WRITE_REG(VIDEO_ACCEL,0x84,regs.top & 0xff);
142 WRITE_REG(VIDEO_ACCEL,0x85,regs.bottom & 0xff);
143 WRITE_REG(VIDEO_ACCEL,0x86,(regs.top >> 8) | ((regs.bottom>>4) & 0x70));
144
145 //setup the framebuffer base addresses
146 WRITE_REG(VIDEO_ACCEL,0x8a,regs.yuv_base & 0xff);
147 WRITE_REG(VIDEO_ACCEL,0x8b,(regs.yuv_base >> 8) & 0xff);
148 WRITE_REG(VIDEO_ACCEL,0x89,(regs.yuv_base >> 12) & 0xf0);
149
150 WRITE_REG(VIDEO_ACCEL,0xb7,regs.u_base & 0xff);
151 WRITE_REG(VIDEO_ACCEL,0xb8,(regs.u_base >> 8) & 0xff);
152
153 WRITE_REG(VIDEO_ACCEL,0xba,regs.v_base & 0xff);
154 WRITE_REG(VIDEO_ACCEL,0xbb,(regs.v_base >> 8) & 0xff);
155 WRITE_REG(VIDEO_ACCEL,0xb9,((regs.v_base >> 12) & 0xf0) + ((regs.u_base >> 16) & 0xf));
156
157 WRITE_REG(VIDEO_ACCEL,0x8d,regs.fb_end);
158
159 //setup framebuffer pitch
160 WRITE_REG(VIDEO_ACCEL,0x8c,regs.pitch & 0xff);
161 WRITE_REG(VIDEO_ACCEL,0x8e,(regs.pitch >> 8) & 0x0f);
162 WRITE_REG(VIDEO_ACCEL,0xbc,(regs.pitch) & 0xff);
163 WRITE_REG(VIDEO_ACCEL,0xbd,((regs.pitch) >> 8) & 0x0f);
164
165
166 //write key overlay register
167 WRITE_REG(VIDEO_ACCEL,0xa9,regs.key_mode);
168
169 WRITE_REG(VIDEO_ACCEL,0x93,0x40);
170 WRITE_REG(VIDEO_ACCEL,0x94,1);
171 WRITE_REG(VIDEO_ACCEL,0x9e,0);
172 WRITE_REG(VIDEO_ACCEL,0x9f,0);
173
174 //write config registers
175 WRITE_REG(VIDEO_ACCEL,MISC_0,regs.misc_0);
176 WRITE_REG(VIDEO_ACCEL,MISC_1,regs.misc_1);
177 WRITE_REG(VIDEO_ACCEL,MISC_3,regs.misc_3);
178 WRITE_REG(VIDEO_ACCEL,MISC_4,regs.misc_4);
179
180 //setup the video line buffer
181 WRITE_REG(VIDEO_ACCEL,0xa0,(regs.right - regs.left)/ 8);
182
183 }
184
185 static int mga_vid_set_config(mga_vid_config_t *config)
186 {
187 uint32_t x, y, frame_size;
188
189 x = config->x_org;
190 y = config->y_org;
191
192 regs.left = x;
193 regs.right= config->src_width + x;
194 regs.top = y;
195 regs.bottom = config->src_height + y;
196
197 printk("mga_vid: Setting up a %dx%d+%d+%d video window (src %dx%d)\n",
198 config->dest_width, config->dest_height, config->x_org, config->y_org,
199 config->src_width, config->src_height);
200
201
202 regs.pitch = ((config->src_width + 31) & ~31) / 4 ;
203
204 //frame size in pixels
205 frame_size = regs.pitch * config->src_height * 4;
206
207 regs.yuv_base = (mga_src_base) >> 2;
208 regs.u_base = (mga_src_base + frame_size) >> 2;
209 regs.v_base = (mga_src_base + frame_size/4) >> 2;
210 regs.fb_end = (mga_src_base + (3*frame_size)/2) >> 14;
211
212 //disable video capture, enable video display, enable graphics display,
213 //select yuv format, odd parity
214 regs.misc_0 = (1 << 1) + (1<<6) + (1<<4);
215
216 //disable dithering, no filtering, no interrupts
217 regs.misc_1 = 0;
218
219 //select 2's complement format YUV for playback
220 regs.misc_3 = (1<<1);
221
222 //select 4:2:0 video format, + yuv4:2:2 cpu writes
223 regs.misc_4 = (1<<2);
224
225 //disable keying
226 regs.key_mode = 0xf;
227
228 mga_vid_write_regs();
229 return 0;
230 }
231
232
233 static int mga_vid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
234 {
235 int frame;
236
237 switch(cmd)
238 {
239 case MGA_VID_CONFIG:
240 //FIXME remove
241 printk("mga_mmio_base = %p\n",mga_mmio_base);
242 printk("mga_mem_base = %08x\n",mga_mem_base);
243 //FIXME remove
244
245 printk("mga_vid: Received configuration\n");
246
247 if(copy_from_user(&mga_config,(mga_vid_config_t*) arg,sizeof(mga_vid_config_t)))
248 {
249 printk("mga_vid: failed copy from userspace\n");
250 return(-EFAULT);
251 }
252
253 mga_config.ram_size = mga_ram_size;
254 //XXX make it look like a g400
255 mga_config.card_type = MGA_G400;;
256
257 if (copy_to_user((mga_vid_config_t *) arg, &mga_config, sizeof(mga_vid_config_t)))
258 {
259 printk("mga_vid: failed copy to userspace\n");
260 return(-EFAULT);
261 }
262 return mga_vid_set_config(&mga_config);
263 break;
264
265 case MGA_VID_ON:
266 printk("mga_vid: Video ON\n");
267 vid_src_ready = 1;
268 if(vid_overlay_on)
269 {
270 //regs.besctl |= 1;
271 mga_vid_write_regs();
272 }
273 break;
274
275 case MGA_VID_OFF:
276 printk("mga_vid: Video OFF\n");
277 vid_src_ready = 0;
278 //regs.besctl &= ~1;
279 mga_vid_write_regs();
280 break;
281
282 case MGA_VID_FSEL:
283 if(copy_from_user(&frame,(int *) arg,sizeof(int)))
284 {
285 printk("mga_vid: FSEL failed copy from userspace\n");
286 return(-EFAULT);
287 }
288
289 mga_vid_frame_sel(frame);
290 break;
291
292 default:
293 printk("mga_vid: Invalid ioctl\n");
294 return (-EINVAL);
295 }
296
297 return 0;
298 }
299
300
301 static int mga_vid_find_card(void)
302 {
303 struct pci_dev *dev = NULL;
304
305 if((dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_6323, NULL)))
306 {
307 printk("sis_vid: Found SiS 6326\n");
308 }
309 else
310 {
311 printk("sis_vid: No supported cards found\n");
312 return FALSE;
313 }
314
315 pci_dev = dev;
316
317 #if LINUX_VERSION_CODE >= 0x020300
318 mga_mmio_base = ioremap_nocache(dev->resource[1].start,0x10000);
319 mga_mem_base = dev->resource[0].start;
320 #else
321 mga_mmio_base = ioremap_nocache(dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK,0x10000);
322 mga_mem_base = dev->base_address[0] & PCI_BASE_ADDRESS_MEM_MASK;
323 #endif
324 printk("mga_vid: MMIO at 0x%p\n", mga_mmio_base);
325 printk("mga_vid: Frame Buffer at 0x%08x\n", mga_mem_base);
326
327 //FIXME set ram size properly
328 mga_ram_size = 4;
329 mga_src_base = (mga_ram_size - 1) * 0x100000;
330
331 //FIXME remove
332 if(1)
333 {
334 mga_vid_config_t config ={0,0,256,256,256,256,10,10,0,0,0,0};
335
336 mga_vid_set_config(&config);
337 mga_vid_write_regs();
338 //regs.misc_0 ^= 2;
339 //mga_vid_write_regs();
340 }
341 //FIXME remove
342
343 return TRUE;
344 }
345
346
347 static ssize_t mga_vid_read(struct file *file, char *buf, size_t count, loff_t *ppos)
348 {
349 return -EINVAL;
350 }
351
352 static ssize_t mga_vid_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
353 {
354 return -EINVAL;
355 }
356
357 static int mga_vid_mmap(struct file *file, struct vm_area_struct *vma)
358 {
359
360 printk("mga_vid: mapping video memory into userspace\n");
361 if(remap_page_range(vma->vm_start, mga_mem_base + mga_src_base,
362 vma->vm_end - vma->vm_start, vma->vm_page_prot))
363 {
364 printk("mga_vid: error mapping video memory\n");
365 return(-EAGAIN);
366 }
367
368 return(0);
369 }
370
371 static int mga_vid_release(struct inode *inode, struct file *file)
372 {
373 //Close the window just in case
374 vid_src_ready = 0;
375 regs.misc_0 &= 0xed;
376 mga_vid_write_regs();
377 mga_vid_in_use = 0;
378
379 //FIXME put back in!
380 //MOD_DEC_USE_COUNT;
381 return 0;
382 }
383
384 static long long mga_vid_lseek(struct file *file, long long offset, int origin)
385 {
386 return -ESPIPE;
387 }
388
389 static int mga_vid_open(struct inode *inode, struct file *file)
390 {
391 int minor = MINOR(inode->i_rdev);
392
393 if(minor != 0)
394 return(-ENXIO);
395
396 if(mga_vid_in_use == 1)
397 return(-EBUSY);
398
399 mga_vid_in_use = 1;
400 //FIXME turn me back on!
401 //MOD_INC_USE_COUNT;
402 return(0);
403 }
404
405 #if LINUX_VERSION_CODE >= 0x020400
406 static struct file_operations mga_vid_fops =
407 {
408 llseek: mga_vid_lseek,
409 read: mga_vid_read,
410 write: mga_vid_write,
411 ioctl: mga_vid_ioctl,
412 mmap: mga_vid_mmap,
413 open: mga_vid_open,
414 release: mga_vid_release
415 };
416 #else
417 static struct file_operations mga_vid_fops =
418 {
419 mga_vid_lseek,
420 mga_vid_read,
421 mga_vid_write,
422 NULL,
423 NULL,
424 mga_vid_ioctl,
425 mga_vid_mmap,
426 mga_vid_open,
427 NULL,
428 mga_vid_release
429 };
430 #endif
431
432 #if 0
433 static long mga_v4l_read(struct video_device *v, char *buf, unsigned long count,
434 int noblock)
435 {
436 return -EINVAL;
437 }
438
439 static long mga_v4l_write(struct video_device *v, const char *buf, unsigned long count, int noblock)
440 {
441 return -EINVAL;
442 }
443
444 static int mga_v4l_open(struct video_device *dev, int mode)
445 {
446 MOD_INC_USE_COUNT;
447 return 0;
448 }
449
450 static void mga_v4l_close(struct video_device *dev)
451 {
452 //regs.besctl &= ~1;
453 mga_vid_write_regs();
454 vid_overlay_on = 0;
455 MOD_DEC_USE_COUNT;
456 return;
457 }
458
459 static int mga_v4l_init_done(struct video_device *dev)
460 {
461 return 0;
462 }
463
464 static int mga_v4l_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
465 {
466 switch(cmd)
467 {
468 case VIDIOCGCAP:
469 {
470 struct video_capability b;
471 strcpy(b.name, "Matrox G200/400");
472 b.type = VID_TYPE_SCALES|VID_TYPE_OVERLAY|VID_TYPE_CHROMAKEY;
473 b.channels = 0;
474 b.audios = 0;
475 b.maxwidth = 1024; /* GUESS ?? */
476 b.maxheight = 768;
477 b.minwidth = 32;
478 b.minheight = 16; /* GUESS ?? */
479 if(copy_to_user(arg,&b,sizeof(b)))
480 return -EFAULT;
481 return 0;
482 }
483 case VIDIOCGPICT:
484 {
485 /*
486 * Default values.. if we can change this we
487 * can add the feature later
488 */
489 struct video_picture vp;
490 vp.brightness = 0x8000;
491 vp.hue = 0x8000;
492 vp.colour = 0x8000;
493 vp.whiteness = 0x8000;
494 vp.depth = 8;
495 /* Format is a guess */
496 vp.palette = VIDEO_PALETTE_YUV420P;
497 if(copy_to_user(arg, &vp, sizeof(vp)))
498 return -EFAULT;
499 return 0;
500 }
501 case VIDIOCSPICT:
502 {
503 return -EINVAL;
504 }
505 case VIDIOCSWIN:
506 {
507 struct video_window vw;
508 if(copy_from_user(&vw, arg, sizeof(vw)))
509 return -EFAULT;
510 if(vw.x <0 || vw.y <0 || vw.width < 32
511 || vw.height < 16)
512 return -EINVAL;
513 memcpy(&mga_win, &vw, sizeof(mga_win));
514
515 mga_config.x_org = vw.x;
516 mga_config.y_org = vw.y;
517 mga_config.dest_width = vw.width;
518 mga_config.dest_height = vw.height;
519
520 /*
521 * May have to add
522 *
523 * #define VIDEO_WINDOW_CHROMAKEY 16
524 *
525 * to <linux/videodev.h>
526 */
527
528 //add it here for now
529 #define VIDEO_WINDOW_CHROMAKEY 16
530
531 if (vw.flags & VIDEO_WINDOW_CHROMAKEY)
532 mga_config.colkey_on = 1;
533 else
534 mga_config.colkey_on = 0;
535
536 mga_config.colkey_red = (vw.chromakey >> 24) & 0xFF;
537 mga_config.colkey_green = (vw.chromakey >> 16) & 0xFF;
538 mga_config.colkey_blue = (vw.chromakey >> 8) & 0xFF;
539 mga_vid_set_config(&mga_config);
540 return 0;
541
542 }
543 case VIDIOCGWIN:
544 {
545 if(copy_to_user(arg, &mga_win, sizeof(mga_win)))
546 return -EFAULT;
547 return 0;
548 }
549 case VIDIOCCAPTURE:
550 {
551 int v;
552 if(copy_from_user(&v, arg, sizeof(v)))
553 return -EFAULT;
554 vid_overlay_on = v;
555 if(vid_overlay_on && vid_src_ready)
556 {
557 //regs.besctl |= 1;
558 mga_vid_write_regs();
559 }
560 else
561 {
562 //regs.besctl &= ~1;
563 mga_vid_write_regs();
564 }
565 return 0;
566 }
567 default:
568 return -ENOIOCTLCMD;
569 }
570 }
571
572 static struct video_device mga_v4l_dev =
573 {
574 "Matrox G200/G400",
575 VID_TYPE_CAPTURE,
576 VID_HARDWARE_BT848, /* This is a lie for now */
577 mga_v4l_open,
578 mga_v4l_close,
579 mga_v4l_read,
580 mga_v4l_write,
581 NULL,
582 mga_v4l_ioctl,
583 NULL,
584 mga_v4l_init_done,
585 NULL,
586 0,
587 0
588 };
589
590 #endif
591
592 /*
593 * Main Initialization Function
594 */
595
596
597 static int mga_vid_initialize(void)
598 {
599 mga_vid_in_use = 0;
600
601 printk( "SiS 6326 YUV Video interface v0.01 (c) Aaron Holtzman \n");
602 if(register_chrdev(MGA_VID_MAJOR, "mga_vid", &mga_vid_fops))
603 {
604 printk("sis_vid: unable to get major: %d\n", MGA_VID_MAJOR);
605 return -EIO;
606 }
607
608 if (!mga_vid_find_card())
609 {
610 printk("sis_vid: no supported devices found\n");
611 unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
612 return -EINVAL;
613 }
614
615 #if 0
616 if (video_register_device(&mga_v4l_dev, VFL_TYPE_GRABBER)<0)
617 {
618 printk("sis_vid: unable to register.\n");
619 unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
620 if(mga_mmio_base)
621 iounmap(mga_mmio_base);
622 mga_mmio_base = 0;
623 return -EINVAL;
624 }
625 #endif
626
627 return(0);
628 }
629
630 int init_module(void)
631 {
632 return mga_vid_initialize();
633 }
634
635 void cleanup_module(void)
636 {
637 // video_unregister_device(&mga_v4l_dev);
638 if(mga_mmio_base)
639 iounmap(mga_mmio_base);
640
641 //FIXME turn off BES
642 printk("mga_vid: Cleaning up module\n");
643 unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
644 }
645