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
|
// epson.c: Epson ESC/I device backend
// This file is part of scanbuttond.
// Copyleft )c( 2004-2006 by Bernhard Stiftner
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// Thanks to:
// - James Gilliland (Epson CX3200 support)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include "scanbuttond/scanbuttond.h"
#include "scanbuttond/libusbi.h"
#include "epson.h"
#define ESC 0x1B /* ASCII value for ESC */
static char* backend_name = "Epson USB";
#define NUM_SUPPORTED_USB_DEVICES 15
static int supported_usb_devices[NUM_SUPPORTED_USB_DEVICES][3] = {
// vendor, product, num_buttons
{ 0x04B8, 0x0107, 1 }, // Epson Expression 1600
{ 0x04B8, 0x010E, 1 }, // Epson Expression 1680
{ 0x04B8, 0x0103, 1 }, // Epson Perfection 610
{ 0x04B8, 0x0101, 3 }, // Epson Perfection 636U
{ 0x04B8, 0x010C, 3 }, // Epson Perfection 640
{ 0x04B8, 0x0104, 1 }, // Epson Perfection 1200U
{ 0x04B8, 0x010B, 3 }, // Epson Perfection 1240
{ 0x04B8, 0x010A, 1 }, // Epson Perfection 1640
{ 0x04B8, 0x0110, 4 }, // Epson Perfection 1650
{ 0x04B8, 0x011E, 4 }, // Epson Perfection 1660
{ 0x04B8, 0x011B, 4 }, // Epson Perfection 2400
{ 0x04B8, 0x0112, 1 }, // Epson Perfection 2450
{ 0x04B8, 0x011C, 1 }, // Epson Perfection 3200
{ 0x04B8, 0x0802, 1 }, // Epson CX3200 (note: is the button number
// really correct?)
{ 0x04B8, 0x0106, 1 } // Epson Stylus Scan 2500
};
static char* usb_device_descriptions[NUM_SUPPORTED_USB_DEVICES][2] = {
{ "Epson", "Expression 1600"},
{ "Epson", "Expression 1680"},
{ "Epson", "Perfection 610"},
{ "Epson", "Perfection 636U"},
{ "Epson", "Perfection 640"},
{ "Epson", "Perfection 1200U"},
{ "Epson", "Perfection 1240"},
{ "Epson", "Perfection 1640"},
{ "Epson", "Perfection 1650"},
{ "Epson", "Perfection 1660"},
{ "Epson", "Perfection 2400"},
{ "Epson", "Perfection 2450"},
{ "Epson", "Perfection 3200"},
{ "Epson", "Stylus CX3200"},
{ "Epson", "Stylus Scan 2500"}
};
static libusb_handle_t* libusb_handle;
static scanner_t* epson_scanners = NULL;
// returns -1 if the scanner is unsupported, or the index of the
// corresponding vendor-product pair in the supported_usb_devices array.
int epson_match_libusb_scanner(libusb_device_t* device)
{
int index;
for (index = 0; index < NUM_SUPPORTED_USB_DEVICES; index++) {
if (supported_usb_devices[index][0] == device->vendorID &&
supported_usb_devices[index][1] == device->productID) {
break;
}
}
if (index >= NUM_SUPPORTED_USB_DEVICES) return -1;
return index;
}
void epson_attach_libusb_scanner(libusb_device_t* device)
{
const char* descriptor_prefix = "epson:libusb:";
int index = epson_match_libusb_scanner(device);
if (index < 0) return; // unsupported
scanner_t* scanner = (scanner_t*)malloc(sizeof(scanner_t));
scanner->vendor = usb_device_descriptions[index][0];
scanner->product = usb_device_descriptions[index][1];
scanner->connection = CONNECTION_LIBUSB;
scanner->internal_dev_ptr = (void*)device;
scanner->lastbutton = 0;
scanner->sane_device = (char*)malloc(strlen(device->location) +
strlen(descriptor_prefix) + 1);
strcpy(scanner->sane_device, descriptor_prefix);
strcat(scanner->sane_device, device->location);
scanner->num_buttons = supported_usb_devices[index][2];
scanner->is_open = 0;
scanner->next = epson_scanners;
epson_scanners = scanner;
}
void epson_detach_scanners(void)
{
scanner_t* next;
while (epson_scanners != NULL) {
next = epson_scanners->next;
free(epson_scanners->sane_device);
free(epson_scanners);
epson_scanners = next;
}
}
void epson_scan_devices(libusb_device_t* devices)
{
int index;
libusb_device_t* device = devices;
while (device != NULL) {
index = epson_match_libusb_scanner(device);
if (index >= 0)
epson_attach_libusb_scanner(device);
device = device->next;
}
}
int epson_init_libusb(void)
{
libusb_device_t* devices;
libusb_handle = libusb_init();
devices = libusb_get_devices(libusb_handle);
epson_scan_devices(devices);
return 0;
}
const char* scanbtnd_get_backend_name(void)
{
return backend_name;
}
int scanbtnd_init(void)
{
epson_scanners = NULL;
syslog(LOG_INFO, "epson-backend: init");
return epson_init_libusb();
}
int scanbtnd_rescan(void)
{
libusb_device_t *devices;
epson_detach_scanners();
epson_scanners = NULL;
libusb_rescan(libusb_handle);
devices = libusb_get_devices(libusb_handle);
epson_scan_devices(devices);
return 0;
}
const scanner_t* scanbtnd_get_supported_devices(void)
{
return epson_scanners;
}
int scanbtnd_open(scanner_t* scanner)
{
int result = -ENOSYS;
if (scanner->is_open)
return -EINVAL;
switch (scanner->connection) {
case CONNECTION_LIBUSB:
// if devices have been added/removed, return -ENODEV to
// make scanbuttond update its device list
if (libusb_get_changed_device_count() != 0)
return -ENODEV;
result = libusb_open((libusb_device_t*)scanner->internal_dev_ptr);
break;
}
if (result == 0)
scanner->is_open = 1;
return result;
}
int scanbtnd_close(scanner_t* scanner)
{
int result = -ENOSYS;
if (!scanner->is_open)
return -EINVAL;
switch (scanner->connection) {
case CONNECTION_LIBUSB:
result = libusb_close((libusb_device_t*)scanner->internal_dev_ptr);
break;
}
if (result == 0)
scanner->is_open = 0;
return result;
}
int epson_read(scanner_t* scanner, void* buffer, int bytecount)
{
switch (scanner->connection) {
case CONNECTION_LIBUSB:
return libusb_read((libusb_device_t*)scanner->internal_dev_ptr,
buffer, bytecount);
break;
}
return -1;
}
int epson_write(scanner_t* scanner, void* buffer, int bytecount)
{
switch (scanner->connection) {
case CONNECTION_LIBUSB:
return libusb_write((libusb_device_t*)scanner->internal_dev_ptr,
buffer, bytecount);
break;
}
return -1;
}
void epson_flush(scanner_t* scanner)
{
switch (scanner->connection) {
case CONNECTION_LIBUSB:
libusb_flush((libusb_device_t*)scanner->internal_dev_ptr);
break;
}
}
int scanbtnd_get_button(scanner_t* scanner)
{
#define BUFFER_SIZE 16
unsigned char bytes[BUFFER_SIZE];
int rcv_len;
int num_bytes;
bytes[0] = ESC;
bytes[1] = '!';
bytes[2] = '\0';
if (!scanner->is_open)
return -EINVAL;
num_bytes = epson_write(scanner, (void*)bytes, 2);
if (num_bytes != 2) {
syslog(LOG_WARNING, "epson-backend: communication error: "
"write length:%d (expected:%d)", num_bytes, 2);
epson_flush(scanner);
return 0;
}
num_bytes = epson_read(scanner, (void*)bytes, 4);
if (num_bytes != 4) {
syslog(LOG_WARNING, "epson-backend: communication error: "
"read length:%d (expected:%d)", num_bytes, 4);
epson_flush(scanner);
return 0;
}
rcv_len = bytes[3] << 8 | bytes[2];
num_bytes = epson_read(scanner, (void*)bytes, MAX(rcv_len, BUFFER_SIZE));
if (num_bytes != rcv_len) {
syslog(LOG_WARNING, "epson-backend: communication error: "
"read length:%d (expected:%d)", num_bytes, MAX(rcv_len, BUFFER_SIZE));
epson_flush(scanner);
return 0;
}
return bytes[0];
}
const char* scanbtnd_get_sane_device_descriptor(scanner_t* scanner)
{
return scanner->sane_device;
}
int scanbtnd_exit(void)
{
syslog(LOG_INFO, "epson-backend: exit");
epson_detach_scanners();
libusb_exit(libusb_handle);
return 0;
}
|