summaryrefslogtreecommitdiff
path: root/src/hal/simdrivers/uparport.h
blob: 7df55666e6e236e969b4d0f841f35cdaa8fce22d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//    Copyright (C) 2009 Jeff Epler
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#ifndef HAL_PARPORT_COMMON_H
#define HAL_PARPORT_COMMON_H

#ifdef SIM

// drop root privilegs after init
// this is a stopgap measure until rtapi_app knows how to handle this
#define DROP_PRIV 1 

#define MAX_PARPORT 10 // a tad on the high side
struct ppres  {
    unsigned int state;
    unsigned int reg_start;
    unsigned int reg_end;
    unsigned int irq;
};
#else
#include <linux/parport.h>
#endif
typedef struct hal_parport_t
{
    unsigned short base;
    unsigned short base_hi;
#ifndef SIM
    struct pardevice *linux_dev;
#else
    struct ppres pp_res; // retrieved from sysfs
    int parport_fd;  // for ioctl()
#endif
    void *region;
    void *region_hi;
} hal_parport_t;

#ifdef SIM
float cpu_MHz(void) 
{
    char *path = "/proc/cpuinfo",  *s, line[1024];
    float freq;
    char *cpu_line = "cpu MHz";

    // parse /proc/cpuinfo for the line:
    // cpu MHz		: 2378.041
    FILE *f = fopen(path,"r");
    if (!f) {
	perror(path);
	return -1.0;
    }
    while (fgets(line, sizeof(line), f)) {
	if (!strncmp(line, cpu_line, strlen(cpu_line))) {
	    s = strchr(line, ':');
	    if (s && 1 == sscanf(s, ":%g", &freq)) {
		fclose(f);
		return freq;
	    }
	}
    }
    fclose(f);
    return -1.0;
}

int get_ppdev_res(int dev, struct ppres *ppres)
{
    FILE *f;
    char path[1024], value[100],line[1024], *s;
    unsigned from, to;

    // parse this:
    // $ cat /sys/class/ppdev/parport0/device/resources 
    // state = active
    // io 0x378-0x37f
    // irq 7
    ppres->state = 0;
    ppres->reg_start = 0;
    ppres->reg_end = 0;
    ppres->irq = 0;

    sprintf(path,"/sys/class/ppdev/parport%d/device/resources",
	    dev);

    f = fopen(path, "r");
    if (!f) {
	return -1;
    }
    while (fgets(line, sizeof(line), f)) {
	if (!strncmp(line, "state", 5)) {
	    s = strchr(line, '=');
	    if (s && 1 == sscanf(s, "= %s", value)) {
		ppres->state = !strcmp(value,"active");
	    } else  {
		rtapi_print_msg(RTAPI_MSG_ERR, 
				"get_ppdev_res: cant parse '%s'\n",
				line);
		fclose(f);
		return -1;
	    }
	}
	if (!strncmp(line, "irq", 3)) {
	    if (1 != sscanf(line+4, " %d", &ppres->irq)) {
		rtapi_print_msg(RTAPI_MSG_ERR, 
				"get_ppdev_res: cant parse '%s'\n",
				line);
		fclose(f);
		return -1;
	    }
	}
	if (!strncmp(line, "io", 2)) {
	    if (2 != sscanf(line+2, " 0x%x-0x%x", &from, &to)) {
		rtapi_print_msg(RTAPI_MSG_ERR,
				"get_ppdev_res: cant parse '%s'\n",
				line);
		fclose(f);
		return -1;
		    
	    } else {
		ppres->reg_start = from;
		ppres->reg_end = to;
	    }
	}
    }
    fclose(f);
    return 0;
}
#endif

static int
hal_parport_get(int comp_id, hal_parport_t *port,
        unsigned short base, unsigned short base_hi, unsigned int modes)
{
    int i, direction;
    struct ppres pp_res;
    char path[PATH_MAX];
#ifndef SIM
    int retval = 0;
    struct parport *linux_port = 0;
#endif
    memset(port, 0, sizeof(hal_parport_t));

    // investigate sysfs for ppdev-created parport%d devices
    for (i = 0; i < MAX_PARPORT; i++) {
	if (!get_ppdev_res(i, &pp_res)) {
	    if (base == i)
		break;
	    if (pp_res.reg_start == base)
		break;
	} 
    }
    if (i == MAX_PARPORT) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: cant find port %d (0x%x)\n",
			base, base);
	return -ENODEV;
    }
    rtapi_print_msg(RTAPI_MSG_ERR,
		    "PARPORT: using base=0x%x\n",
		    pp_res.reg_start);

    snprintf(path, sizeof(path),"/dev/parport%d", i);
    if ((port->parport_fd = open(path,O_RDWR)) < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: cant open %s: %s\n", 
			path, strerror(errno));
	return -EPERM;
    }
    if (ioctl(port->parport_fd, PPCLAIM) < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: cant reserve %s: %s\n", 
			path, strerror(errno));
	return -EPERM;
    }
    if (ioperm(pp_res.reg_start, 3, 1) < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: cant get access to 0x%x (not running as root?)\n",
			pp_res.reg_start);
	return -EPERM;
    }
    port->base = pp_res.reg_start;
    port->base_hi = port->base + 0x400; // dubious..

    direction = modes == PARPORT_MODE_TRISTATE;
    if (ioctl(port->parport_fd, PPDATADIR, &direction) < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: cant set direction (PPDATADIR) on 0x%x\n",
			pp_res.reg_start);
	return -EPERM;
    }

#ifdef DROP_PRIV
    // not a good place, but good until simdriver #2..
    rtapi_print_msg(RTAPI_MSG_ERR,
		    "PARPORT: dropping root permissions to uid %d\n",
		    getuid());
    setuid(getuid()); 
#endif

#ifndef SIM
    // I/O addresses 1..16 are assumed to be linux parport numbers
    if(base < 16) {
        linux_port = parport_find_number(base);
        if(!linux_port)
        {
            rtapi_print_msg(RTAPI_MSG_ERR,
                    "PARPORT: ERROR: linux parport %d not found\n",
                    base);
            return -ENODEV;
        }
    } else {
        linux_port = parport_find_base(base);
    }

    if(linux_port)
    {
        if((modes & linux_port->modes) != modes)
        {
            rtapi_print_msg(RTAPI_MSG_WARN,
                "PARPORT: linux parport %s does not support mode %x.\n"
                "PARPORT: continuing anyway.\n",
                linux_port->name, modes);
        }
        rtapi_print_msg(RTAPI_MSG_INFO,
                  "PARPORT: Using Linux parport %s at ioaddr=0x%lx:0x%lx\n",
                  linux_port->name, linux_port->base, linux_port->base_hi);
        port->linux_dev = parport_register_device(linux_port,
                hal_comp_name(comp_id), NULL, NULL, NULL, 0, NULL);

        if(!port->linux_dev)
        {
            parport_put_port(linux_port);
            rtapi_print_msg(RTAPI_MSG_ERR,
                "PARPORT: ERROR: port %s register failed\n", linux_port->name);
            return -EIO;
        }

        retval = parport_claim(port->linux_dev);
        if(retval < 0)
        {
            parport_put_port(linux_port);
            parport_unregister_device(port->linux_dev);
            rtapi_print_msg(RTAPI_MSG_ERR,
                "PARPORT: ERROR: port %s claim failed\n", linux_port->name);
            return retval;
        }

        port->base = linux_port->base;
        if(linux_port->base_hi > 0) {
            port->base_hi = linux_port->base_hi;
        } else if(base_hi != (unsigned short)-1) {
            if(base_hi == 0) base_hi = port->base + 0x400;
            rtapi_print_msg(RTAPI_MSG_DBG,
                "PARPORT: DEBUG: linux reports no ioaddr_hi, using 0x%x",
                base_hi);
            port->region_hi =
                rtapi_request_region(base_hi, 3, hal_comp_name(comp_id));
            if(port->region_hi) {
                rtapi_print_msg(RTAPI_MSG_DBG,
                    "PARPORT: DEBUG: got requested region starting at 0x%x",
                    base_hi);
                port->base_hi = base_hi;
            } else {
                rtapi_print_msg(RTAPI_MSG_DBG,
                    "PARPORT: DEBUG: did not get requested region starting at 0x%x",
                    base_hi);
            }
        }
        parport_put_port(linux_port);
    } else {
        if(base_hi == 0) base_hi = base + 0x400;

        port->base = base;
        rtapi_print_msg(RTAPI_MSG_INFO,
                  "Using direct parport at ioaddr=0x%x:0x%x\n", base, base_hi);

        // SPP access needs only 3 bytes, but EPP needs 8.  Allocating 8
        // is likely to always be OK, and it simplifies things (since the
        // exact allocation size is also needed at deallocation).
        port->region = rtapi_request_region(base, 8, hal_comp_name(comp_id));
        if(!port->region)
        {
            rtapi_print_msg(RTAPI_MSG_ERR,
                "PARPORT: ERROR: request_region(0x%x) failed\n", base);
            return -EBUSY;
        }

        if(base_hi != (unsigned short)-1)
        {
            port->base_hi = base_hi;
            port->region_hi =
                rtapi_request_region(base_hi, 3, hal_comp_name(comp_id));
            if(!port->region_hi)
            {
                rtapi_print_msg(RTAPI_MSG_ERR,
                    "PARPORT: ERROR: request_region(0x%x) failed\n", base_hi);
                rtapi_release_region(port->base, 8);
                return -EBUSY;
            }
        }
    }
#endif
    return 0;
}

void hal_parport_release(hal_parport_t *port)
{
#ifndef SIM
    if(port->linux_dev)
	{
	    rtapi_print_msg(RTAPI_MSG_INFO,
			    "PARPORT: Releasing Linux parport at ioaddr=0x%lx:0x%lx\n",
			    port->linux_dev->port->base, port->linux_dev->port->base_hi);
	    parport_release(port->linux_dev);
	    parport_unregister_device(port->linux_dev);
	}
    if(port->region) {
        rtapi_print_msg(RTAPI_MSG_INFO,
			"PARPORT: Releasing I/O region ioaddr=0x%x\n", port->base);
	rtapi_release_region(port->base, 8);
    }
    if(port->region_hi) {
        rtapi_print_msg(RTAPI_MSG_INFO,
			"PARPORT: Releasing high I/O region ioaddr=0x%x\n",
			port->base_hi);
	rtapi_release_region(port->base_hi, 3);
    }
    memset(port, 0, sizeof(hal_parport_t));
#else
    if (ioperm(port->pp_res.reg_start, 3, 0) < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: releasing ioperm(0x%x)\n",
			port->pp_res.reg_start);
    }
    if (ioctl(port->parport_fd, PPRELEASE) < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
			"PARPORT: ERROR: releasing parport 0x%x: %s\n", 
			port->pp_res.reg_start, strerror(errno));
    }
#endif

}
#endif