sane-project-website/old-archive/1999-03/0017.html

257 wiersze
7.6 KiB
HTML

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

<!-- received="Tue Mar 2 08:11:07 1999 PST" -->
<!-- sent="Tue, 2 Mar 1999 17:08:34 +0100 (MET)" -->
<!-- name="Andreas Czechanowski" -->
<!-- email="andreas@inspc44b.ins.uni-stuttgart.de" -->
<!-- subject="off.c (for Mustek 600 II N)" -->
<!-- id="¨Õÿ" -->
<!-- inreplyto="" -->
<title>sane-devel: off.c (for Mustek 600 II N)</title>
<h1>off.c (for Mustek 600 II N)</h1>
<b>Andreas Czechanowski</b> (<a href="mailto:andreas@inspc44b.ins.uni-stuttgart.de"><i>andreas@inspc44b.ins.uni-stuttgart.de</i></a>)<br>
<i>Tue, 2 Mar 1999 17:08:34 +0100 (MET)</i>
<p>
<ul>
<li> <b>Messages sorted by:</b> <a href="date.html#17">[ date ]</a><a href="index.html#17">[ thread ]</a><a href="subject.html#17">[ subject ]</a><a href="author.html#17">[ author ]</a>
<!-- next="start" -->
<li> <b>Next message:</b> <a href="0018.html">Greg Franks: "Microtek x6el problems"</a>
<li> <b>Previous message:</b> <a href="0016.html">Torsten Evers: "Mustek 600CP"</a>
<!-- nextthread="start" -->
<!-- reply="end" -->
</ul>
<!-- body="start" -->
Hi !<br>
<p>
This is a utility for the tortured 600 II N user who wants to<br>
switch the scanner off after a program crash or if it gets not<br>
correctly switched off upon program exit.<br>
<p>
Could please somebody integrate this file into the source tree<br>
(with a Makefile if you want, but it is for a somewhat exotic<br>
scanner so only few feople will need it, and they probably will<br>
be able to compile it on their own...) ?<br>
<p>
Regards,<br>
<p>
Andreas.<br>
<p>
------------------------- cut here --------------------------<br>
/*<br>
off.c - Switch the Mustek 600 II N off<br>
<p>
This utility accesses the I/O-ports directly and must therefore be<br>
run with euid root, or must at least have access to /dev/port.<br>
Compile with:<br>
gcc -DHAVE_SYS_IO_H -O2 -Wall -s -o off off.c<br>
The -O2 optimization is needed to allow inline functions !<br>
Copyright (C) 1997-1999 Andreas Czechanowski, DL4SDC<br>
<p>
This program is free software; you can redistribute it and/or modify<br>
it under the terms of the GNU General Public License as published by<br>
the Free Software Foundation; either version 2 of the License, or<br>
(at your option) any later version.<br>
<p>
This program is distributed in the hope that it will be useful,<br>
but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>
GNU General Public License for more details.<br>
<p>
You should have received a copy of the GNU General Public License<br>
along with this program; if not, write to the Free Software Foundation,<br>
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.<br>
<p>
<a href="mailto:andreas.czechanowski@ins.uni-stuttgart.de">andreas.czechanowski@ins.uni-stuttgart.de</a><br>
*/<br>
<br>
#define MUSTEK_CONF "/usr/local/etc/sane.d/mustek.conf"<br>
#define PORT_DEV "/dev/port"<br>
<p>
#include &lt;stdio.h&gt;<br>
#include &lt;stdlib.h&gt;<br>
#include &lt;sys/types.h&gt;<br>
#include &lt;sys/stat.h&gt;<br>
#include &lt;fcntl.h&gt;<br>
#include &lt;string.h&gt;<br>
#include &lt;unistd.h&gt;<br>
<p>
#ifdef HAVE_SYS_IO_H<br>
# include &lt;sys/io.h&gt; /* use where available (glibc 2.x, for example) */<br>
#elif HAVE_ASM_IO_H<br>
# include &lt;asm/io.h&gt; /* ugly, but backwards compatible */<br>
#elif defined(__i386__) &amp;&amp; defined (__GNUC__)<br>
<p>
static __inline__ void<br>
outb (u_char value, u_long port)<br>
{<br>
__asm__ __volatile__ ("outb %0,%1" : : "a" (value), "d" ((u_short) port));<br>
}<br>
<p>
static __inline__ u_char<br>
inb (u_long port)<br>
{<br>
u_char value;<br>
<p>
__asm__ __volatile__ ("inb %1,%0" : "=a" (value) : "d" ((u_short)port));<br>
return value;<br>
}<br>
#endif<br>
<p>
char *Mustek_Conf = MUSTEK_CONF;<br>
<p>
int allowed_ports[] = {<br>
0x26b, 0x26c,<br>
0x2ab, 0x2ac,<br>
0x2eb, 0x2ec,<br>
0x22b, 0x22c,<br>
0x32b, 0x32c,<br>
0x36b, 0x36c,<br>
0x3ab, 0x3ac,<br>
0x3eb, 0x3ec,<br>
-1<br>
};<br>
<p>
void<br>
usage(void)<br>
{<br>
fprintf(stderr, "Usage: off [port]\n"<br>
" switches the Mustek 600 II N off that is connected to\n"<br>
" base address &lt;port&gt;. If address is not given, reads it\n"<br>
" from SANE config file &lt;%s&gt;.\n", Mustek_Conf);<br>
}<br>
<p>
void<br>
noaccess(int portaddr)<br>
{<br>
fprintf(stderr, "Access to port 0x%03x not allowed !\n", portaddr);<br>
}<br>
<p>
int<br>
check_port(int portaddr)<br>
{<br>
int i, j;<br>
<p>
for (i = 0; (j = allowed_ports[i]) != -1; i++) {<br>
if (j == portaddr)<br>
return j;<br>
}<br>
return -1;<br>
}<br>
<p>
int<br>
str2int(char *ch)<br>
{<br>
int i;<br>
<p>
i = strtol(ch, NULL, 0);<br>
return i;<br>
}<br>
<p>
int<br>
main(int argc, char **argv)<br>
{<br>
char *cp;<br>
int portaddr = 0;<br>
FILE *fp;<br>
int pfd;<br>
<p>
/* get config file name from environment if variable is set */<br>
if (NULL != (cp = getenv("MUSTEK_CONF")))<br>
{<br>
Mustek_Conf = cp;<br>
}<br>
<p>
/* if port is explicitly given, try this one */<br>
if (argc &gt; 1)<br>
{<br>
portaddr = str2int(argv[1]);<br>
}<br>
/* else try to look it up from SANE's mustek.conf file */<br>
else<br>
if (NULL != (fp = fopen(MUSTEK_CONF, "r")))<br>
{<br>
char line[256];<br>
<p>
while (NULL != fgets(line, 255, fp))<br>
{<br>
if ('#' == *line)<br>
continue;<br>
if (0 != (portaddr = str2int(line)))<br>
break;<br>
}<br>
fclose(fp);<br>
}<br>
else<br>
{<br>
fprintf(stderr, "Mustek config file &lt;%s&gt; not found\n", Mustek_Conf);<br>
usage();<br>
exit(1);<br>
}<br>
<p>
if (check_port(portaddr) &lt; 0 || check_port(portaddr + 1) &lt; 0)<br>
{<br>
fprintf(stderr, "invalid port address specified !\n");<br>
usage();<br>
exit(1);<br>
}<br>
<p>
/* we need the control port, not the data port, so... */<br>
portaddr++;<br>
<p>
fprintf(stderr, "using control port address 0x%03x\n", portaddr);<br>
/* try to get I/O permission from the kernel */<br>
if (ioperm(portaddr, 1, 1) == 0)<br>
{<br>
outb(0x00, portaddr);<br>
}<br>
/* else try to open /dev/port to access the I/O port */<br>
else<br>
if ((pfd = open(PORT_DEV, O_RDWR, 0666)) &gt;= 0)<br>
{<br>
char offcmd[] = { 0x00 };<br>
<p>
if ((lseek(pfd, portaddr, SEEK_SET) != portaddr)<br>
<i> || (write(pfd, offcmd, 1) != 1))</i><br>
{<br>
perror("error handling /dev/port");<br>
exit(1);<br>
}<br>
close(pfd);<br>
}<br>
else<br>
{<br>
fprintf(stderr, "Could not get port access:\n"<br>
"Neither via ioperm(), nor via /dev/port.\n"<br>
"This program must be run setuid root,\n"<br>
"or the user must have access to /dev/port.\n");<br>
exit(1);<br>
}<br>
printf("successfully sent OFF-command to control port at 0x%03x.\n",<br>
portaddr);<br>
<p>
exit(0);<br>
}<br>
<p>
------------------------- cut here --------------------------<br>
<p>
<pre>
--
Dipl.-Ing. Andreas Czechanowski
<a href="mailto:andreas.czechanowski@ins.uni-stuttgart.de">andreas.czechanowski@ins.uni-stuttgart.de</a>
Institut fuer Netzwerk- und Systemtheorie
Universitaet Stuttgart
<p>
<p>
<pre>
--
Source code, list archive, and docs: <a href="http://www.mostang.com/sane/">http://www.mostang.com/sane/</a>
To unsubscribe: echo unsubscribe sane-devel | mail <a href="mailto:majordomo@mostang.com">majordomo@mostang.com</a>
</pre>
<!-- body="end" -->
<p>
<ul>
<!-- next="start" -->
<li> <b>Next message:</b> <a href="0018.html">Greg Franks: "Microtek x6el problems"</a>
<li> <b>Previous message:</b> <a href="0016.html">Torsten Evers: "Mustek 600CP"</a>
<!-- nextthread="start" -->
<!-- reply="end" -->
</ul>