1 | #define _GNU_SOURCE
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <errno.h>
|
---|
4 | #include <sys/types.h>
|
---|
5 | #include <string.h>
|
---|
6 | #include <sys/ioctl.h>
|
---|
7 | #include <fcntl.h>
|
---|
8 | #include <sys/stat.h>
|
---|
9 | #include <sys/mman.h>
|
---|
10 | #include <sys/param.h>
|
---|
11 | #include "devopen.h"
|
---|
12 | #include "dev/pci/sis1100_var.h"
|
---|
13 |
|
---|
14 | static void
|
---|
15 | close_dev(struct devinfo* info)
|
---|
16 | {
|
---|
17 | if (info->base_ctrl) {
|
---|
18 | munmap((void*)(info->base_ctrl), info->size_ctrl);
|
---|
19 | info->base_ctrl=MAP_FAILED;
|
---|
20 | info->size_ctrl=0;
|
---|
21 | }
|
---|
22 | if (info->base_remote) {
|
---|
23 | munmap((void*)(info->base_remote), info->size_remote);
|
---|
24 | info->base_remote=MAP_FAILED;
|
---|
25 | info->size_remote=0;
|
---|
26 | }
|
---|
27 | close(info->p_ctrl); info->p_ctrl=-1;
|
---|
28 | close(info->p_remote); info->p_remote=-1;
|
---|
29 | close(info->p_ram); info->p_ram=-1;
|
---|
30 | close(info->p_dsp); info->p_dsp=-1;
|
---|
31 | }
|
---|
32 |
|
---|
33 | static volatile u_int32_t*
|
---|
34 | mmap_region(int p, u_int32_t* size)
|
---|
35 | {
|
---|
36 | volatile u_int32_t* base;
|
---|
37 |
|
---|
38 | if (ioctl(p, SIS1100_MAPSIZE, size)<0) {
|
---|
39 | fprintf(stderr, "ioctl(%d, SIS1100_MAPSIZE): %s\n",
|
---|
40 | p, strerror(errno));
|
---|
41 | return MAP_FAILED;
|
---|
42 | }
|
---|
43 | base=mmap(0, *size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
|
---|
44 | p, 0);
|
---|
45 | if (base==MAP_FAILED) {
|
---|
46 | fprintf(stderr, "mmap(%d, size=%d): %s\n", p, *size, strerror(errno));
|
---|
47 | }
|
---|
48 | return base;
|
---|
49 | }
|
---|
50 |
|
---|
51 | int
|
---|
52 | open_dev(char** pathes, struct devinfo* info)
|
---|
53 | {
|
---|
54 | const char* path;
|
---|
55 |
|
---|
56 | info->p_ctrl=-1;
|
---|
57 | info->p_remote=-1;
|
---|
58 | info->p_ram=-1;
|
---|
59 | info->p_dsp=-1;
|
---|
60 | info->base_ctrl=MAP_FAILED;
|
---|
61 | info->base_remote=MAP_FAILED;
|
---|
62 | info->size_ctrl=0;
|
---|
63 | info->size_remote=0;
|
---|
64 | info->close=0;
|
---|
65 |
|
---|
66 | path=*pathes++;
|
---|
67 | while (path) {
|
---|
68 | int p;
|
---|
69 | enum sis1100_subdev subdev;
|
---|
70 |
|
---|
71 | p=open(path, O_RDWR, 0);
|
---|
72 | if (p<0) {
|
---|
73 | fprintf(stderr, "open \"%s\": %s\n", path, strerror(errno));
|
---|
74 | goto error;
|
---|
75 | }
|
---|
76 | if (ioctl(p, SIS1100_DEVTYPE, &subdev)<0) {
|
---|
77 | fprintf(stderr, "ioctl(\"%s\", SIS1100_DEVTYPE): %s\n",
|
---|
78 | path, strerror(errno));
|
---|
79 | goto error;
|
---|
80 | }
|
---|
81 | switch (subdev) {
|
---|
82 | case sis1100_subdev_remote:
|
---|
83 | info->p_remote=p;
|
---|
84 | info->base_remote=mmap_region(p, &info->size_remote);
|
---|
85 | printf("base_remote=%p\n", info->base_remote);
|
---|
86 | break;
|
---|
87 | case sis1100_subdev_ram:
|
---|
88 | info->p_ram=p;
|
---|
89 | break;
|
---|
90 | case sis1100_subdev_ctrl:
|
---|
91 | info->p_ctrl=p;
|
---|
92 | info->base_ctrl=mmap_region(p, &info->size_ctrl);
|
---|
93 | printf("base_ctrl=%p\n", info->base_ctrl);
|
---|
94 | break;
|
---|
95 | case sis1100_subdev_dsp:
|
---|
96 | info->p_dsp=p;
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | path=*pathes++;
|
---|
100 | }
|
---|
101 |
|
---|
102 | info->close=close_dev;
|
---|
103 | return 0;
|
---|
104 |
|
---|
105 | error:
|
---|
106 | close_dev(info);
|
---|
107 | return -1;
|
---|
108 | }
|
---|