File: geniusvp2-parport.c

package info (click to toggle)
sane-backends-extras 1.0.22.4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,080 kB
  • ctags: 2,710
  • sloc: ansic: 27,680; sh: 10,433; makefile: 448
file content (252 lines) | stat: -rw-r--r-- 6,396 bytes parent folder | download | duplicates (4)
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
/* parport.c - Parallel port I/O functions
 * 
 * Copyright (C) 2004, 2005 Anderson Lizardo
 *
 * 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., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#ifdef __linux__
#  include <linux/parport.h>
#  include <linux/ppdev.h>
#elif defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
#  include <dev/ppbus/ppi.h>
#  include <dev/ppbus/ppbconf.h>
#  define PPRDATA	PPIGDATA
#  define PPWDATA	PPISDATA
#  define PPRSTATUS	PPIGSTATUS
#  define PPWSTATUS	PPISSTATUS
#  define PPRCONTROL	PPIGCTRL
#  define PPWCONTROL	PPISCTRL
/* Not available anymore in userland in FreeBSD 8, for no good reason */
#  define PPB_COMPATIBLE 0x0
#  define PPB_EPP        0x4
#endif

#define DEBUG_DECLARE_ONLY
#include "geniusvp2.h"
#include "geniusvp2-parport.h"

static int port_fd;

int
sane_geniusvp2_parport_open (const char *pp_device)
{
    int retval;
    unsigned int port_feature;

    port_fd = open (pp_device, O_RDWR);
    if (port_fd == -1)
    {
        DBG (1, "sane_geniusvp2_parport_open: could not open %s (%s)\n",
             pp_device, strerror (errno));
        return 1;
    }
#ifdef __linux__
    retval = ioctl (port_fd, PPCLAIM);
    if (retval == -1)
    {
        DBG (1, "sane_geniusvp2_parport_open: could not claim port access (%s)\n",
             strerror (errno));
        retval = close (port_fd);
        if (retval == -1)
            DBG (1, "sane_geniusvp2_parport_open: could not close port (%s)\n",
                 strerror (errno));
        return 1;
    }
    retval = ioctl (port_fd, PPGETMODES, &port_feature);
    if (retval == -1)
    {
        DBG (1, "sane_geniusvp2_parport_open: warning: could not get port capabilities "
             "(%s), assuming EPP is present\n", strerror (errno));
    }
    else if (!(port_feature & PARPORT_MODE_EPP))
    {
        DBG (1, "sane_geniusvp2_parport_open: this port does not support EPP mode, "
             "please enable EPP or EPP+ECP mode\n");
	retval = ioctl (port_fd, PPRELEASE);
        if (retval == -1)
            DBG (1, "sane_geniusvp2_parport_open: could not release port (%s)\n",
                 strerror (errno));
        retval = close (port_fd);
        if (retval == -1)
            DBG (1, "sane_geniusvp2_parport_open: could not close port (%s)\n",
                 strerror (errno));
        return 1;
    }
#elif defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
    port_feature = PPB_EPP;
    retval = ioctl (port_fd, PPISECR, &port_feature);
    if (retval == -1)
    {
        DBG (1, "sane_geniusvp2_parport_open: could not switch port to EPP mode, " 
	     "please enable EPP or EPP+ECP mode (%s)\n", strerror (errno));
	return 1;
    }
#endif

    return 0;
}

int
sane_geniusvp2_parport_close (void)
{
    int retval;
#ifdef __linux__
    const int compat = IEEE1284_MODE_COMPAT;

    retval = ioctl (port_fd, PPNEGOT, &compat);
#elif defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
    const int compat = PPB_COMPATIBLE;

    retval = ioctl (port_fd, PPISECR, &compat);
    if (retval == -1)
        DBG (1, "sane_geniusvp2_parport_close: warning: could not negotiate "
             "compatibility mode (%s)\n", strerror (errno));
#endif
#ifdef __linux__
    retval = ioctl (port_fd, PPRELEASE);
    if (retval == -1)
        DBG (1, "sane_geniusvp2_parport_close: could not release port (%s)\n",
             strerror (errno));
#endif    
    retval = close (port_fd);
    if (retval == -1)
    {
        DBG (1, "sane_geniusvp2_parport_close: could not close port (%s)\n",
             strerror (errno));
        return 1;
    }

    return 0;
}

int
sane_geniusvp2_epp_read (unsigned char *data, int size)
{
    int returned;

    while (size > 0)
    {
        returned = read (port_fd, data, size);
        if (returned < 0)
        {
            DBG (1, "sane_geniusvp2_epp_read: %s\n", strerror (errno));
            return 1;
        }
        data += returned;
        size -= returned;
    }

    return 0;
}

int
sane_geniusvp2_epp_write (unsigned char *data, int size)
{
    int written;

    while (size > 0)
    {
        written = write (port_fd, data, size);
        if (written < 0)
        {
            DBG (1, "sane_geniusvp2_epp_write: %s\n", strerror (errno));
            return 1;
        }
        data += written;
        size -= written;
    }

    return 0;
}

#ifdef __linux__
int
sane_geniusvp2_epp_set_mode (int mode)
{
    int retval;

    mode |= IEEE1284_MODE_EPP;
    retval = ioctl (port_fd, PPSETMODE, &mode);
    if (retval == -1)
    {
        DBG (1, "sane_geniusvp2_epp_set_mode: %s\n", strerror (errno));
        return 1;
    }

    return 0;
}
#elif defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
int sane_geniusvp2_epp_write_addr (unsigned char addr)
{
    int retval;

    retval = ioctl (port_fd, PPISEPPA, &addr);
    if (retval == -1)
    {
        DBG (1, "sane_geniusvp2_epp_write_addr: %s\n", strerror (errno));
        return 1;
    }

    return 0;

}
#endif

int
sane_geniusvp2_pp_write (unsigned long int line, unsigned char data)
{
    switch (line)
    {
    case PP_DATA:
        return ioctl (port_fd, PPWDATA, &data);
        break;

    case PP_CONTROL:
        return ioctl (port_fd, PPWCONTROL, &data);
        break;
    }

    return 0;
}

int
sane_geniusvp2_pp_read (unsigned long int line, unsigned char *data)
{
    switch (line)
    {
    case PP_DATA:
        return ioctl (port_fd, PPRDATA, data);
        break;

    case PP_STATUS:
        return ioctl (port_fd, PPRSTATUS, data);
        break;

    case PP_CONTROL:
        return ioctl (port_fd, PPRCONTROL, data);
        break;
    }

    return 0;
}