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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
/* hp5590.c: HP4570/5550/5590/7650 backend
* This file is part of scanbuttond.
* Copyleft )c( 2008 by Ilia Sotnikov <hostcc@gmail.com>
*
* 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.
*/
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <netinet/in.h> /* For htons() */
#include "scanbuttond/scanbuttond.h"
#include "scanbuttond/libusbi.h"
#include "hp5590.h"
static char* backend_name = "HP5590 USB";
#define NUM_SUPPORTED_USB_DEVICES 4
static int supported_usb_devices[NUM_SUPPORTED_USB_DEVICES][3] =
{
/* vendor, product, num_buttons */
{ 0x03f0, 0x1305, 5 }, /* HP Scanjet 4570 */
{ 0x03f0, 0x1305, 5 }, /* HP Scanjet 5550 */
{ 0x03f0, 0x1705, 5 }, /* HP Scanjet 5590 */
{ 0x03f0, 0x1805, 5 }, /* HP Scanjet 7650 */
};
static char* usb_device_descriptions[NUM_SUPPORTED_USB_DEVICES][2] =
{
{ "Hewlett-Packard", "ScanJet 4570" },
{ "Hewlett-Packard", "ScanJet 5550" },
{ "Hewlett-Packard", "Scanjet 5590" },
{ "Hewlett-Packard", "Scanjet 7650" },
};
static libusb_handle_t* libusb_handle;
static scanner_t* hp5590_scanners = NULL;
/* returns -1 if the scanner is unsupported, or the index of the
* corresponding vendor-product pair in the supported_usb_devices array.
*/
static int
hp5590_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;
}
static void
hp5590_attach_libusb_scanner (libusb_device_t* device)
{
const char* descriptor_prefix = "hp5590:libusb:";
int index;
index = hp5590_match_libusb_scanner(device);
/* Unsupported */
if (index < 0)
return;
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 = hp5590_scanners;
hp5590_scanners = scanner;
}
static void
hp5590_detach_scanners (void)
{
scanner_t* next;
while (hp5590_scanners != NULL)
{
next = hp5590_scanners->next;
free (hp5590_scanners->sane_device);
free (hp5590_scanners);
hp5590_scanners = next;
}
}
static void
hp5590_scan_devices (libusb_device_t* devices)
{
int index;
libusb_device_t* device = devices;
while (device != NULL)
{
index = hp5590_match_libusb_scanner (device);
if (index >= 0)
hp5590_attach_libusb_scanner (device);
device = device->next;
}
}
static int
hp5590_init_libusb (void)
{
libusb_device_t* devices;
libusb_handle = libusb_init ();
devices = libusb_get_devices (libusb_handle);
hp5590_scan_devices (devices);
return 0;
}
static void
hp5590_flush (scanner_t* scanner)
{
switch (scanner->connection)
{
case CONNECTION_LIBUSB:
libusb_flush ((libusb_device_t*) scanner->internal_dev_ptr);
break;
}
}
const char*
scanbtnd_get_backend_name (void)
{
return backend_name;
}
int
scanbtnd_init (void)
{
hp5590_scanners = NULL;
syslog (LOG_INFO, "hp5590-backend: init");
return hp5590_init_libusb ();
}
int
scanbtnd_rescan (void)
{
libusb_device_t* devices;
hp5590_detach_scanners ();
hp5590_scanners = NULL;
libusb_rescan (libusb_handle);
devices = libusb_get_devices (libusb_handle);
hp5590_scan_devices (devices);
return 0;
}
const scanner_t*
scanbtnd_get_supported_devices (void)
{
return hp5590_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;
}
/* Taken from sane-backends/include/sane/sanei_usb.h */
#define USB_DIR_OUT 0x00
#define USB_DIR_IN 0x80
/* Taken from sane-backends/backends/hp5590_cmds.c */
/* Button flags */
#define BUTTON_FLAG_EMAIL 1 << 15
#define BUTTON_FLAG_COPY 1 << 14
#define BUTTON_FLAG_DOWN 1 << 13
#define BUTTON_FLAG_MODE 1 << 12
#define BUTTON_FLAG_UP 1 << 11
#define BUTTON_FLAG_FILE 1 << 9
#define BUTTON_FLAG_POWER 1 << 5
#define BUTTON_FLAG_SCAN 1 << 2
#define BUTTON_FLAG_COLLECT 1 << 1
#define BUTTON_FLAG_CANCEL 1 << 0
#define CMD_BUTTON_STATUS 0x0020
/* Taken from sane-backends/backends/hp5590_low.c */
/* Flags for hp5590_cmd() */
#define CMD_IN 1 << 0 /* Indicates IN direction, otherwise - OUT */
#define CMD_VERIFY 1 << 1 /* Requests last command verification */
/* Core flags for hp5590_cmd() - they indicate so called CORE commands */
#define CORE_NONE 0 /* No CORE operation */
#define CORE_DATA 1 << 0 /* Operate on CORE data */
/* CORE status flag - ready or not */
#define CORE_FLAG_NOT_READY 1 << 1
/* Structure describing control URB */
struct usb_in_usb_ctrl_setup
{
u_int8_t bRequestType;
u_int8_t bRequest;
u_int16_t wValue;
u_int16_t wIndex;
u_int16_t wLength;
} __attribute__ ((packed));
static int
hp5590_get_ack (scanner_t *scanner)
{
u_int8_t status;
int ret;
/* Check if USB-in-USB operation was accepted */
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_IN | USB_TYPE_VENDOR,
0x0c, 0x8e, 0x20,
&status, sizeof (status));
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error getting acknowledge");
return -1;
}
/* Check if we received correct acknowledgement */
if (status != 0x01)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: not accepted (status %u)", status);
return -1;
}
return 0;
}
static int
hp5590_control_msg (scanner_t *scanner,
int requesttype, int request,
int value, int index, unsigned char *bytes,
int size, int core_flags)
{
struct usb_in_usb_ctrl_setup ctrl;
int ret;
unsigned int len;
unsigned char *ptr;
u_int8_t ack;
u_int8_t response;
/* IN (read) operation will be performed */
if (requesttype & USB_DIR_IN)
{
/* Prepare USB-in-USB control message */
memset (&ctrl, 0, sizeof (ctrl));
ctrl.bRequestType = 0xc0;
ctrl.bRequest = request;
ctrl.wValue = htons (value);
ctrl.wIndex = htons (index);
ctrl.wLength = size;
/* Send USB-in-USB control message */
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_OUT | USB_TYPE_VENDOR,
0x04, 0x8f, 0x00,
(unsigned char *) &ctrl, sizeof (ctrl));
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error sending control message");
return -1;
}
ret = hp5590_get_ack (scanner);
if (ret != 0)
return ret;
len = size;
ptr = bytes;
/* Data is read in 8 byte portions */
while (len)
{
unsigned int next_packet_size;
next_packet_size = 8;
if (len < 8)
next_packet_size = len;
/* Read USB-in-USB data */
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_IN | USB_TYPE_VENDOR,
core_flags & CORE_DATA ? 0x0c : 0x04,
0x90, 0x00,
ptr, next_packet_size);
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error reading data");
return -1;
}
ptr += next_packet_size;
len -= next_packet_size;
}
/* Confirm data reception */
ack = 0;
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_OUT | USB_TYPE_VENDOR,
0x0c, 0x8f, 0x00,
&ack, sizeof (ack));
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error confirming data reception");
return -1;
}
ret = hp5590_get_ack (scanner);
if (ret != 0)
return ret;
}
/* OUT (write) operation will be performed */
if (!(requesttype & USB_DIR_IN))
{
/* Prepare USB-in-USB control message */
memset (&ctrl, 0, sizeof (ctrl));
ctrl.bRequestType = 0x40;
ctrl.bRequest = request;
ctrl.wValue = htons (value);
ctrl.wIndex = htons (index);
ctrl.wLength = size;
/* Send USB-in-USB control message */
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_OUT | USB_TYPE_VENDOR,
0x04, 0x8f, 0x00,
(unsigned char *) &ctrl, sizeof (ctrl));
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error sending control message");
return -1;
}
ret = hp5590_get_ack (scanner);
if (ret != 0)
return ret;
len = size;
ptr = bytes;
/* Data is sent in 8 byte portions */
while (len)
{
unsigned int next_packet_size;
next_packet_size = 8;
if (len < 8)
next_packet_size = len;
/* Send USB-in-USB data */
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_OUT | USB_TYPE_VENDOR,
core_flags & CORE_DATA ? 0x04 : 0x0c,
0x8f, 0x00, ptr, next_packet_size);
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error sending data");
return -1;
}
/* CORE data is acknowledged packet by packet */
if (core_flags & CORE_DATA)
{
ret = hp5590_get_ack (scanner);
if (ret != 0)
return ret;
}
ptr += next_packet_size;
len -= next_packet_size;
}
/* Normal (non-CORE) data is acknowledged after its full transmission */
if (!(core_flags & CORE_DATA))
{
ret = hp5590_get_ack (scanner);
if (ret != 0)
return ret;
}
/* Getting response after data transmission */
ret = libusb_control_msg ((libusb_device_t *) scanner->internal_dev_ptr,
USB_DIR_IN | USB_TYPE_VENDOR,
0x0c, 0x90, 0x00,
&response, sizeof (response));
if (ret <= 0)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: error getting response");
return -1;
}
/* Necessary response after normal (non-CORE) data is 0x00 */
if (response != 0x00)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: invalid response received "
"(needed 0x00, got %04x)", response);
return -1;
}
}
return 0;
}
static int
hp5590_verify_last_cmd (scanner_t *scanner, unsigned int cmd)
{
u_int16_t verify_cmd;
unsigned int last_cmd;
unsigned int core_status;
int ret;
/* Read last command along with CORE status */
ret = hp5590_control_msg (scanner,
USB_DIR_IN,
0x04, 0xc5, 0x00,
(unsigned char *) &verify_cmd,
sizeof (verify_cmd), CORE_NONE);
if (ret != 0)
return ret;
/* Last command - minor byte */
last_cmd = verify_cmd & 0xff;
/* CORE status - major byte */
core_status = (verify_cmd & 0xff00) >> 8;
/* Verify last command */
if ((cmd & 0x00ff) != last_cmd)
{
syslog (LOG_ERR, "hp5590-backend: USB-in-USB: command verification failed: "
"expected 0x%04x, got 0x%04x", cmd, last_cmd);
return -1;
}
/* Return value depends on CORE status */
return core_status & CORE_FLAG_NOT_READY ? -1 : 0;
}
static int
hp5590_cmd (scanner_t *scanner, unsigned int flags,
unsigned int cmd, unsigned char *data, unsigned int size,
unsigned int core_flags)
{
int ret;
ret = hp5590_control_msg (scanner,
flags & CMD_IN ? USB_DIR_IN : USB_DIR_OUT,
0x04, cmd, 0x00, data, size, core_flags);
if (ret != 0)
return ret;
ret = 0;
/* Verify last command if requested */
if (flags & CMD_VERIFY)
ret = hp5590_verify_last_cmd (scanner, cmd);
return ret;
}
int
scanbtnd_get_button(scanner_t* scanner)
{
int button = 0;
u_int16_t button_status;
int ret;
if (!scanner->is_open)
return -EINVAL;
ret = hp5590_cmd (scanner, CMD_IN | CMD_VERIFY,
CMD_BUTTON_STATUS,
(unsigned char *) &button_status,
sizeof (button_status), CORE_NONE);
if (ret != 0) {
hp5590_flush (scanner);
return 0;
}
/* Network order */
button_status = ntohs (button_status);
if (button_status & BUTTON_FLAG_SCAN)
button = 1;
if (button_status & BUTTON_FLAG_COLLECT)
button = 2;
if (button_status & BUTTON_FLAG_FILE)
button = 3;
if (button_status & BUTTON_FLAG_EMAIL)
button = 4;
if (button_status & BUTTON_FLAG_COPY)
button = 5;
return button;
}
const char*
scanbtnd_get_sane_device_descriptor (scanner_t* scanner)
{
return scanner->sane_device;
}
int
scanbtnd_exit (void)
{
syslog (LOG_INFO, "hp5590-backend: exit");
hp5590_detach_scanners ();
libusb_exit (libusb_handle);
return 0;
}
|