1 /* sane-find-scanner.c
2
3 Copyright (C) 1997-2013 Oliver Rauch, Henning Meier-Geinitz, and others.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 */
19
20 #include "../include/sane/config.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <dirent.h>
29 #include <errno.h>
30
31 #if defined (HAVE_DDK_NTDDSCSI_H) || defined (HAVE_NTDDSCSI_H)
32 # define WIN32_SCSI
33 # include <windows.h>
34 # if defined (HAVE_DDK_NTDDSCSI_H)
35 # include <ddk/scsi.h>
36 # include <ddk/ntddscsi.h>
37 # elif defined (HAVE_NTDDSCSI_H)
38 # include <ntddscsi.h>
39 # endif
40 #endif
41
42 #include "../include/sane/sanei.h"
43 #include "../include/sane/sanei_scsi.h"
44 #include "../include/sane/sanei_pa4s2.h"
45 #include "../include/sane/sanei_config.h"
46
47 #ifdef HAVE_LIBUSB_LEGACY
48 #ifdef HAVE_LUSB0_USB_H
49 #include <lusb0_usb.h>
50 #else
51 #include <usb.h>
52 #endif
53 extern char * check_usb_chip (struct usb_device *dev, int verbosity, SANE_Bool from_file);
54 #endif
55
56 #ifdef HAVE_LIBUSB
57 #include <libusb.h>
58 extern char * check_usb_chip (int verbosity,
59 struct libusb_device_descriptor desc,
60 libusb_device_handle *hdl,
61 struct libusb_config_descriptor *config0);
62 #endif
63
64 #include "../include/sane/sanei_usb.h"
65
66 #ifndef PATH_MAX
67 # define PATH_MAX 1024
68 #endif
69
70 static const char *prog_name;
71 static int verbose = 1;
72 static SANE_Bool force = SANE_FALSE;
73 static SANE_Bool device_found = SANE_FALSE;
74 static SANE_Bool libusb_device_found = SANE_FALSE;
75 static SANE_Bool unknown_found = SANE_FALSE;
76
77 #ifdef HAVE_LIBUSB
78 libusb_context *sfs_usb_ctx;
79 #endif
80
81 typedef struct
82 {
83 unsigned char *cmd;
84 size_t size;
85 }
86 scsiblk;
87
88 #define INQUIRY 0x12
89 #define set_inquiry_return_size(icb,val) icb[0x04]=val
90 #define IN_periph_devtype_cpu 0x03
91 #define IN_periph_devtype_scanner 0x06
92 #define get_scsi_inquiry_vendor(in, buf) snprintf(buf, 0x08 + 1, "%.*s", \
93 0x08, in + 0x08)
94 #define get_scsi_inquiry_product(in, buf) snprintf(buf, 0x10 + 1, "%.*s", \
95 0x10, in + 0x10)
96 #define get_scsi_inquiry_version(in, buf) snprintf(buf, 0x04 + 1, "%.*s", \
97 0x04, in + 0x20)
98 #define get_scsi_inquiry_periph_devtype(in) (in[0] & 0x1f)
99 #define get_scsi_inquiry_additional_length(in) in[0x04]
100 #define set_scsi_inquiry_length(out,n) out[0x04]=n-5
101
102 static unsigned char inquiryC[] = {
103 INQUIRY, 0x00, 0x00, 0x00, 0xff, 0x00
104 };
105 static scsiblk inquiry = {
106 inquiryC, sizeof (inquiryC)
107 };
108
109 static void
usage(char * msg)110 usage (char *msg)
111 {
112 fprintf (stderr, "Usage: %s [-hvqf] [devname ...]\n", prog_name);
113 fprintf (stderr, "\t-h: print this help message\n");
114 fprintf (stderr, "\t-v: be more verbose (can be used multiple times)\n");
115 fprintf (stderr, "\t-q: be quiet (print only devices, no comments)\n");
116 fprintf (stderr, "\t-f: force opening devname as SCSI even if it looks "
117 "like USB\n");
118 fprintf (stderr, "\t-p: enable scanning for parallel port devices\n");
119 #ifdef HAVE_LIBUSB_LEGACY
120 fprintf (stderr, "\t-F file: try to detect chipset from given "
121 "/proc/bus/usb/devices file\n");
122 #endif
123 if (msg)
124 fprintf (stderr, "\t%s\n", msg);
125 }
126
127 /* if SCSI generic is optional on your OS, and there is a software test
128 which will determine if it is included, add it here. If you are sure
129 that SCSI generic was found, return 1. If SCSI generic is always
130 available in your OS, return 1 */
131
132 static int
check_sg(void)133 check_sg (void)
134 {
135 #if defined(__linux__)
136 /* Assumption: /proc/devices lines are shorter than 256 characters */
137 char line[256], driver[256] = "";
138 FILE *stream;
139
140 stream = fopen ("/proc/devices", "r");
141 /* Likely reason for failure, no /proc => probably no SG either */
142 if (stream == NULL)
143 return 0;
144
145 while (fgets (line, sizeof (line) - 1, stream))
146 {
147 if (sscanf (line, " %*d %s\n", driver) > 0 && !strcmp (driver, "sg"))
148 break;
149 }
150 fclose (stream);
151
152 if (strcmp (driver, "sg"))
153 {
154 return 0;
155 }
156 else
157 {
158 return 1;
159 }
160 #endif
161 return 1; /* Give up, and assume yes to avoid false negatives */
162 }
163
164 /* Display a buffer in the log. Display by lines of 16 bytes. */
165 static void
hexdump(const char * comment,unsigned char * buf,const int length)166 hexdump (const char *comment, unsigned char *buf, const int length)
167 {
168 int i;
169 char line[128];
170 char *ptr;
171 char asc_buf[17];
172 char *asc_ptr;
173
174 printf (" %s\n", comment);
175
176 i = 0;
177 goto start;
178
179 do
180 {
181 if (i < length)
182 {
183 ptr += sprintf (ptr, " %2.2x", *buf);
184
185 if (*buf >= 32 && *buf <= 127)
186 {
187 asc_ptr += sprintf (asc_ptr, "%c", *buf);
188 }
189 else
190 {
191 asc_ptr += sprintf (asc_ptr, ".");
192 }
193 }
194 else
195 {
196 /* After the length; do nothing. */
197 ptr += sprintf (ptr, " ");
198 }
199
200 i++;
201 buf++;
202
203 if ((i % 16) == 0)
204 {
205 /* It's a new line */
206 printf (" %s %s\n", line, asc_buf);
207
208 start:
209 ptr = line;
210 *ptr = '\0';
211 asc_ptr = asc_buf;
212 *asc_ptr = '\0';
213
214 ptr += sprintf (ptr, " %3.3d:", i);
215 }
216
217 }
218 while (i < ((length + 15) & ~15));
219 }
220
221 static SANE_Status
scanner_do_scsi_inquiry(unsigned char * buffer,int sfd)222 scanner_do_scsi_inquiry (unsigned char *buffer, int sfd)
223 {
224 size_t size;
225 SANE_Status status;
226
227 memset (buffer, '\0', 256); /* clear buffer */
228
229 size = 5; /* first get only 5 bytes to get size of
230 inquiry_return_block */
231 set_inquiry_return_size (inquiry.cmd, size);
232 status = sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
233
234 if (status != SANE_STATUS_GOOD)
235 return (status);
236
237 size = get_scsi_inquiry_additional_length (buffer) + 5;
238
239 /* then get inquiry with actual size */
240 set_inquiry_return_size (inquiry.cmd, size);
241 status = sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
242
243 return (status);
244 }
245
246 static void
scanner_identify_scsi_scanner(unsigned char * buffer,int sfd,char * devicename)247 scanner_identify_scsi_scanner (unsigned char *buffer, int sfd,
248 char *devicename)
249 {
250 unsigned char vendor[9];
251 unsigned char product[17];
252 unsigned char version[5];
253 unsigned char *pp;
254 unsigned int devtype;
255 SANE_Status status;
256 static char *devtypes[] = {
257 "disk", "tape", "printer", "processor", "CD-writer",
258 "CD-drive", "scanner", "optical-drive", "jukebox",
259 "communicator"
260 };
261 status = scanner_do_scsi_inquiry (buffer, sfd);
262 if (status != SANE_STATUS_GOOD)
263 {
264 if (verbose > 1)
265 printf ("inquiry for device %s failed (%s)\n",
266 devicename, sane_strstatus (status));
267 return;
268 }
269
270 if (verbose > 2)
271 hexdump ("Inquiry for device:", buffer,
272 get_scsi_inquiry_additional_length (buffer) + 5);
273
274 devtype = get_scsi_inquiry_periph_devtype (buffer);
275 if (verbose <= 1 && devtype != IN_periph_devtype_scanner
276 /* old HP scanners use the CPU id ... */
277 && devtype != IN_periph_devtype_cpu)
278 return; /* no, continue searching */
279
280 get_scsi_inquiry_vendor ((char *) buffer, (char *) vendor);
281 get_scsi_inquiry_product ((char *) buffer, (char *) product);
282 get_scsi_inquiry_version ((char *) buffer, (char *) version);
283
284 pp = &vendor[7];
285 vendor[8] = '\0';
286 while (pp >= vendor && (*pp == ' ' || *pp >= 127))
287 *pp-- = '\0';
288
289 pp = &product[15];
290 product[16] = '\0';
291 while (pp >= product && (*pp == ' ' || *pp >= 127))
292 *pp-- = '\0';
293
294 pp = &version[3];
295 version[4] = '\0';
296 while (pp >= version && (*pp == ' ' || *(pp - 1) >= 127))
297 *pp-- = '\0';
298
299 device_found = SANE_TRUE;
300 printf ("found SCSI %s \"%s %s %s\" at %s\n",
301 devtype < NELEMS (devtypes) ? devtypes[devtype] : "unknown device",
302 vendor, product, version, devicename);
303 return;
304 }
305
306 static void
check_scsi_file(char * file_name)307 check_scsi_file (char *file_name)
308 {
309 int result;
310 int sfd;
311 unsigned char buffer[16384];
312
313 if (strstr (file_name, "usb")
314 || strstr (file_name, "uscanner") || strstr (file_name, "ugen"))
315 {
316 if (force)
317 {
318 if (verbose > 1)
319 printf ("checking %s even though it looks like a USB device...",
320 file_name);
321 }
322 else
323 {
324 if (verbose > 1)
325 printf ("ignored %s (not a SCSI device)\n", file_name);
326 return;
327 }
328 }
329 else if (verbose > 1)
330 printf ("checking %s...", file_name);
331
332 result = sanei_scsi_open (file_name, &sfd, NULL, NULL);
333
334 if (verbose > 1)
335 {
336 if (result != 0)
337 printf (" failed to open (%s)\n", sane_strstatus (result));
338 else
339 printf (" open ok\n");
340 }
341
342 if (result == SANE_STATUS_GOOD)
343 {
344 scanner_identify_scsi_scanner (buffer, sfd, file_name);
345 sanei_scsi_close (sfd);
346 }
347 return;
348 }
349
350 static void
check_usb_file(char * file_name)351 check_usb_file (char *file_name)
352 {
353 SANE_Status result;
354 SANE_Word vendor, product;
355 SANE_Int fd;
356
357 if (!strstr (file_name, "usb")
358 && !strstr (file_name, "uscanner") && !strstr (file_name, "ugen"))
359 {
360 if (force)
361 {
362 if (verbose > 1)
363 printf ("checking %s even though doesn't look like a "
364 "USB device...", file_name);
365 }
366 else
367 {
368 if (verbose > 1)
369 printf ("ignored %s (not a USB device)\n", file_name);
370 return;
371 }
372 }
373 else if (verbose > 1)
374 printf ("checking %s...", file_name);
375
376 result = sanei_usb_open (file_name, &fd);
377
378 if (result != SANE_STATUS_GOOD)
379 {
380 if (verbose > 1)
381 printf (" failed to open (%s)\n", sane_strstatus (result));
382 }
383 else
384 {
385 result = sanei_usb_get_vendor_product (fd, &vendor, &product);
386 if (result == SANE_STATUS_GOOD)
387 {
388 if (verbose > 1)
389 printf (" open ok, vendor and product ids were identified\n");
390 printf ("found possible USB scanner (vendor=0x%04x, "
391 "product=0x%04x) at %s\n", vendor, product, file_name);
392 }
393 else
394 {
395 if (verbose > 1)
396 printf (" open ok, but vendor and product could NOT be "
397 "identified\n");
398 printf ("found possible USB scanner (UNKNOWN vendor and "
399 "product) at %s\n", file_name);
400 unknown_found = SANE_TRUE;
401 }
402 device_found = SANE_TRUE;
403 sanei_usb_close (fd);
404 }
405 }
406
407 #ifdef HAVE_LIBUSB_LEGACY
408
409 static char *
get_libusb_string_descriptor(struct usb_device * dev,int index)410 get_libusb_string_descriptor (struct usb_device *dev, int index)
411 {
412 usb_dev_handle *handle;
413 char *buffer, short_buffer[2];
414 struct usb_string_descriptor *sd;
415 int size = 2;
416 int i;
417
418 if (!index)
419 return 0;
420
421 handle = usb_open (dev);
422 if (!handle)
423 return 0;
424
425 sd = (struct usb_string_descriptor *) short_buffer;
426
427 if (usb_control_msg (handle,
428 USB_ENDPOINT_IN + USB_TYPE_STANDARD + USB_RECIP_DEVICE,
429 USB_REQ_GET_DESCRIPTOR,
430 (USB_DT_STRING << 8) + index, 0, short_buffer,
431 size, 1000) < 0)
432 {
433 usb_close (handle);
434 return 0;
435 }
436
437 if (sd->bLength < 2
438 || sd->bDescriptorType != USB_DT_STRING)
439 {
440 usb_close (handle);
441 return 0;
442 }
443
444 size = sd->bLength;
445
446 buffer = calloc (1, size + 1);
447 if (!buffer)
448 return 0;
449
450 sd = (struct usb_string_descriptor *) buffer;
451
452 if (usb_control_msg (handle,
453 USB_ENDPOINT_IN + USB_TYPE_STANDARD + USB_RECIP_DEVICE,
454 USB_REQ_GET_DESCRIPTOR,
455 (USB_DT_STRING << 8) + index, 0, buffer,
456 size, 1000) < 0)
457 {
458 usb_close (handle);
459 free (buffer);
460 return 0;
461 }
462
463 if (sd->bLength < 2 || sd->bLength > size
464 || sd->bDescriptorType != USB_DT_STRING)
465 {
466 usb_close (handle);
467 free (buffer);
468 return 0;
469 }
470 size = sd->bLength - 2;
471 for (i = 0; i < (size / 2); i++)
472 buffer[i] = buffer[2 + 2 * i];
473 buffer[i] = 0;
474
475 usb_close (handle);
476 return buffer;
477 }
478
479 static char *
get_libusb_vendor(struct usb_device * dev)480 get_libusb_vendor (struct usb_device *dev)
481 {
482 return get_libusb_string_descriptor (dev, dev->descriptor.iManufacturer);
483 }
484
485 static char *
get_libusb_product(struct usb_device * dev)486 get_libusb_product (struct usb_device *dev)
487 {
488 return get_libusb_string_descriptor (dev, dev->descriptor.iProduct);
489 }
490
491 static void
check_libusb_device(struct usb_device * dev,SANE_Bool from_file)492 check_libusb_device (struct usb_device *dev, SANE_Bool from_file)
493 {
494 int is_scanner = 0;
495 char *vendor;
496 char *product;
497 int interface_nr;
498
499 if (!dev->config)
500 {
501 if (verbose > 1)
502 printf ("device 0x%04x/0x%04x is not configured\n",
503 dev->descriptor.idVendor, dev->descriptor.idProduct);
504 return;
505 }
506
507 vendor = get_libusb_vendor (dev);
508 product = get_libusb_product (dev);
509
510 if (verbose > 2)
511 {
512 /* print everything we know about the device */
513 char *buf;
514 int config_nr;
515 struct usb_device_descriptor *d = &dev->descriptor;
516
517 printf ("\n");
518 printf ("<device descriptor of 0x%04x/0x%04x at %s:%s",
519 d->idVendor, d->idProduct, dev->bus->dirname, dev->filename);
520 if (vendor || product)
521 {
522 printf (" (%s%s%s)", vendor ? vendor : "",
523 (vendor && product) ? " " : "", product ? product : "");
524 }
525 printf (">\n");
526 printf ("bLength %d\n", d->bLength);
527 printf ("bDescriptorType %d\n", d->bDescriptorType);
528 printf ("bcdUSB %d.%d%d\n", d->bcdUSB >> 8,
529 (d->bcdUSB >> 4) & 15, d->bcdUSB & 15);
530 printf ("bDeviceClass %d\n", d->bDeviceClass);
531 printf ("bDeviceSubClass %d\n", d->bDeviceSubClass);
532 printf ("bDeviceProtocol %d\n", d->bDeviceProtocol);
533 printf ("bMaxPacketSize0 %d\n", d->bMaxPacketSize0);
534 printf ("idVendor 0x%04X\n", d->idVendor);
535 printf ("idProduct 0x%04X\n", d->idProduct);
536 printf ("bcdDevice %d.%d%d\n", d->bcdDevice >> 8,
537 (d->bcdDevice >> 4) & 15, d->bcdDevice & 15);
538 printf ("iManufacturer %d (%s)\n", d->iManufacturer,
539 (vendor) ? vendor : "");
540 printf ("iProduct %d (%s)\n", d->iProduct,
541 (product) ? product : "");
542
543 buf = get_libusb_string_descriptor (dev, d->iSerialNumber);
544 printf ("iSerialNumber %d (%s)\n", d->iSerialNumber,
545 (buf) ? buf : "");
546 if (buf)
547 free (buf);
548 printf ("bNumConfigurations %d\n", d->bNumConfigurations);
549
550 for (config_nr = 0; config_nr < d->bNumConfigurations; config_nr++)
551 {
552 struct usb_config_descriptor *c = &dev->config[config_nr];
553 int interface_nr;
554
555 printf (" <configuration %d>\n", config_nr);
556 printf (" bLength %d\n", c->bLength);
557 printf (" bDescriptorType %d\n", c->bDescriptorType);
558 printf (" wTotalLength %d\n", c->wTotalLength);
559 printf (" bNumInterfaces %d\n", c->bNumInterfaces);
560 printf (" bConfigurationValue %d\n", c->bConfigurationValue);
561 buf = get_libusb_string_descriptor (dev, c->iConfiguration);
562 printf (" iConfiguration %d (%s)\n", c->iConfiguration,
563 (buf) ? buf : "");
564 if (buf)
565 free (buf);
566 printf (" bmAttributes %d (%s%s)\n", c->bmAttributes,
567 c->bmAttributes & 64 ? "Self-powered" : "",
568 c->bmAttributes & 32 ? "Remote Wakeup" : "");
569 printf (" MaxPower %d mA\n", c->MaxPower * 2);
570
571 for (interface_nr = 0; interface_nr < c->bNumInterfaces;
572 interface_nr++)
573 {
574 struct usb_interface *interface = &c->interface[interface_nr];
575 int alt_setting_nr;
576
577 printf (" <interface %d>\n", interface_nr);
578 for (alt_setting_nr = 0;
579 alt_setting_nr < interface->num_altsetting;
580 alt_setting_nr++)
581 {
582 struct usb_interface_descriptor *i
583 = &interface->altsetting[alt_setting_nr];
584 int ep_nr;
585 printf (" <altsetting %d>\n", alt_setting_nr);
586 printf (" bLength %d\n", i->bLength);
587 printf (" bDescriptorType %d\n", i->bDescriptorType);
588 printf (" bInterfaceNumber %d\n", i->bInterfaceNumber);
589 printf (" bAlternateSetting %d\n", i->bAlternateSetting);
590 printf (" bNumEndpoints %d\n", i->bNumEndpoints);
591 printf (" bInterfaceClass %d\n", i->bInterfaceClass);
592 printf (" bInterfaceSubClass %d\n",
593 i->bInterfaceSubClass);
594 printf (" bInterfaceProtocol %d\n",
595 i->bInterfaceProtocol);
596 buf = get_libusb_string_descriptor (dev, i->iInterface);
597 printf (" iInterface %d (%s)\n", i->iInterface,
598 (buf) ? buf : "");
599 if (buf)
600 free (buf);
601
602 for (ep_nr = 0; ep_nr < i->bNumEndpoints; ep_nr++)
603 {
604 struct usb_endpoint_descriptor *e = &i->endpoint[ep_nr];
605 char *ep_type;
606
607 switch (e->bmAttributes & USB_ENDPOINT_TYPE_MASK)
608 {
609 case USB_ENDPOINT_TYPE_CONTROL:
610 ep_type = "control";
611 break;
612 case USB_ENDPOINT_TYPE_ISOCHRONOUS:
613 ep_type = "isochronous";
614 break;
615 case USB_ENDPOINT_TYPE_BULK:
616 ep_type = "bulk";
617 break;
618 case USB_ENDPOINT_TYPE_INTERRUPT:
619 ep_type = "interrupt";
620 break;
621 default:
622 ep_type = "unknown";
623 break;
624 }
625 printf (" <endpoint %d>\n", ep_nr);
626 printf (" bLength %d\n", e->bLength);
627 printf (" bDescriptorType %d\n",
628 e->bDescriptorType);
629 printf (" bEndpointAddress 0x%02X (%s 0x%02X)\n",
630 e->bEndpointAddress,
631 e->bEndpointAddress & USB_ENDPOINT_DIR_MASK ?
632 "in" : "out",
633 e->
634 bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK);
635 printf (" bmAttributes %d (%s)\n",
636 e->bmAttributes, ep_type);
637 printf (" wMaxPacketSize %d\n",
638 e->wMaxPacketSize);
639 printf (" bInterval %d ms\n", e->bInterval);
640 printf (" bRefresh %d\n", e->bRefresh);
641 printf (" bSynchAddress %d\n", e->bSynchAddress);
642 }
643 }
644 }
645 }
646
647 }
648
649 /* Some heuristics, which device may be a scanner */
650 if (dev->descriptor.idVendor == 0) /* hub */
651 --is_scanner;
652 if (dev->descriptor.idProduct == 0) /* hub */
653 --is_scanner;
654
655 for (interface_nr = 0; interface_nr < dev->config[0].bNumInterfaces && is_scanner <= 0; interface_nr++)
656 {
657 switch (dev->descriptor.bDeviceClass)
658 {
659 case USB_CLASS_VENDOR_SPEC:
660 ++is_scanner;
661 break;
662 case USB_CLASS_PER_INTERFACE:
663 if (dev->config[0].interface[interface_nr].num_altsetting == 0 ||
664 !dev->config[0].interface[interface_nr].altsetting)
665 break;
666 switch (dev->config[0].interface[interface_nr].altsetting[0].bInterfaceClass)
667 {
668 case USB_CLASS_VENDOR_SPEC:
669 case USB_CLASS_PER_INTERFACE:
670 case 16: /* data? */
671 ++is_scanner;
672 break;
673 }
674 break;
675 }
676 }
677
678 if (is_scanner > 0)
679 {
680 char * chipset = check_usb_chip (dev, verbose, from_file);
681
682 printf ("found possible USB scanner (vendor=0x%04x",
683 dev->descriptor.idVendor);
684 if (vendor)
685 printf (" [%s]", vendor);
686 printf (", product=0x%04x", dev->descriptor.idProduct);
687 if (product)
688 printf (" [%s]", product);
689 if (chipset)
690 printf (", chip=%s", chipset);
691 if (from_file)
692 printf (")\n");
693 else
694 printf (") at libusb:%s:%s\n", dev->bus->dirname, dev->filename);
695
696 libusb_device_found = SANE_TRUE;
697 device_found = SANE_TRUE;
698 }
699
700 if (vendor)
701 free (vendor);
702
703 if (product)
704 free (product);
705 }
706 #endif /* HAVE_LIBUSB_LEGACY */
707
708
709 #ifdef HAVE_LIBUSB
710 static char *
sfs_libusb_strerror(int errcode)711 sfs_libusb_strerror (int errcode)
712 {
713 /* Error codes & descriptions from the libusb-1.0 documentation */
714
715 switch (errcode)
716 {
717 case LIBUSB_SUCCESS:
718 return "Success (no error)";
719
720 case LIBUSB_ERROR_IO:
721 return "Input/output error";
722
723 case LIBUSB_ERROR_INVALID_PARAM:
724 return "Invalid parameter";
725
726 case LIBUSB_ERROR_ACCESS:
727 return "Access denied (insufficient permissions)";
728
729 case LIBUSB_ERROR_NO_DEVICE:
730 return "No such device (it may have been disconnected)";
731
732 case LIBUSB_ERROR_NOT_FOUND:
733 return "Entity not found";
734
735 case LIBUSB_ERROR_BUSY:
736 return "Resource busy";
737
738 case LIBUSB_ERROR_TIMEOUT:
739 return "Operation timed out";
740
741 case LIBUSB_ERROR_OVERFLOW:
742 return "Overflow";
743
744 case LIBUSB_ERROR_PIPE:
745 return "Pipe error";
746
747 case LIBUSB_ERROR_INTERRUPTED:
748 return "System call interrupted (perhaps due to signal)";
749
750 case LIBUSB_ERROR_NO_MEM:
751 return "Insufficient memory";
752
753 case LIBUSB_ERROR_NOT_SUPPORTED:
754 return "Operation not supported or unimplemented on this platform";
755
756 case LIBUSB_ERROR_OTHER:
757 return "Other error";
758
759 default:
760 return "Unknown libusb-1.0 error code";
761 }
762 }
763
764 static char *
get_libusb_string_descriptor(libusb_device_handle * hdl,int index)765 get_libusb_string_descriptor (libusb_device_handle *hdl, int index)
766 {
767 unsigned char *buffer, short_buffer[2];
768 int size;
769 int ret;
770 int i;
771
772 if (!index)
773 return NULL;
774
775 ret = libusb_get_descriptor (hdl, LIBUSB_DT_STRING, index,
776 short_buffer, sizeof (short_buffer));
777 if (ret < 0)
778 {
779 printf ("could not fetch string descriptor: %s\n",
780 sfs_libusb_strerror (ret));
781 return NULL;
782 }
783
784 if ((short_buffer[0] < 2) /* descriptor length */
785 || (short_buffer[1] != LIBUSB_DT_STRING)) /* descriptor type */
786 return NULL;
787
788 size = short_buffer[0];
789
790 buffer = calloc (1, size + 1);
791 if (!buffer)
792 return NULL;
793
794 ret = libusb_get_descriptor (hdl, LIBUSB_DT_STRING, index,
795 buffer, size);
796 if (ret < 0)
797 {
798 printf ("could not fetch string descriptor (again): %s\n",
799 sfs_libusb_strerror (ret));
800 free (buffer);
801 return NULL;
802 }
803
804 if ((buffer[0] < 2) || (buffer[0] > size) /* descriptor length */
805 || (buffer[1] != LIBUSB_DT_STRING)) /* descriptor type */
806 {
807 free (buffer);
808 return NULL;
809 }
810
811 size = buffer[0] - 2;
812 for (i = 0; i < (size / 2); i++)
813 buffer[i] = buffer[2 + 2 * i];
814 buffer[i] = '\0';
815
816 return (char *) buffer;
817 }
818
819 static void
check_libusb_device(libusb_device * dev,SANE_Bool from_file)820 check_libusb_device (libusb_device *dev, SANE_Bool from_file)
821 {
822 libusb_device_handle *hdl;
823 struct libusb_device_descriptor desc;
824 struct libusb_config_descriptor *config0;
825
826 int is_scanner = 0;
827 char *vendor;
828 char *product;
829 unsigned short vid, pid;
830 unsigned char busno, address;
831 int config;
832 int intf;
833 int ret;
834
835 busno = libusb_get_bus_number (dev);
836 address = libusb_get_device_address (dev);
837
838 ret = libusb_get_device_descriptor (dev, &desc);
839 if (ret < 0)
840 {
841 printf ("could not get device descriptor for device at %03d:%03d: %s\n",
842 busno, address, sfs_libusb_strerror (ret));
843 return;
844 }
845
846 vid = desc.idVendor;
847 pid = desc.idProduct;
848
849 ret = libusb_open (dev, &hdl);
850 if (ret < 0)
851 {
852 printf ("could not open USB device 0x%04x/0x%04x at %03d:%03d: %s\n",
853 vid, pid, busno, address, sfs_libusb_strerror (ret));
854 return;
855 }
856
857 ret = libusb_get_configuration (hdl, &config);
858 if (ret < 0)
859 {
860 printf ("could not get configuration for device 0x%04x/0x%04x at %03d:%03d: %s\n",
861 vid, pid, busno, address, sfs_libusb_strerror (ret));
862 libusb_close (hdl);
863 return;
864 }
865
866 if (config == 0)
867 {
868 if (verbose > 1)
869 printf ("device 0x%04x/0x%04x at %03d:%03d is not configured\n",
870 vid, pid, busno, address);
871 libusb_close (hdl);
872 return;
873 }
874
875 vendor = get_libusb_string_descriptor (hdl, desc.iManufacturer);
876 product = get_libusb_string_descriptor (hdl, desc.iProduct);
877
878 if (verbose > 2)
879 {
880 /* print everything we know about the device */
881 char *buf;
882 int config_nr;
883
884 printf ("\n");
885 printf ("<device descriptor of 0x%04x/0x%04x at %03d:%03d",
886 vid, pid, busno, address);
887 if (vendor || product)
888 {
889 printf (" (%s%s%s)", (vendor) ? vendor : "",
890 (vendor && product) ? " " : "", (product) ? product : "");
891 }
892 printf (">\n");
893 printf ("bLength %d\n", desc.bLength);
894 printf ("bDescriptorType %d\n", desc.bDescriptorType);
895 printf ("bcdUSB %d.%d%d\n", desc.bcdUSB >> 8,
896 (desc.bcdUSB >> 4) & 15, desc.bcdUSB & 15);
897 printf ("bDeviceClass %d\n", desc.bDeviceClass);
898 printf ("bDeviceSubClass %d\n", desc.bDeviceSubClass);
899 printf ("bDeviceProtocol %d\n", desc.bDeviceProtocol);
900 printf ("bMaxPacketSize0 %d\n", desc.bMaxPacketSize0);
901 printf ("idVendor 0x%04X\n", desc.idVendor);
902 printf ("idProduct 0x%04X\n", desc.idProduct);
903 printf ("bcdDevice %d.%d%d\n", desc.bcdDevice >> 8,
904 (desc.bcdDevice >> 4) & 15, desc.bcdDevice & 15);
905 printf ("iManufacturer %d (%s)\n", desc.iManufacturer,
906 (vendor) ? vendor : "");
907 printf ("iProduct %d (%s)\n", desc.iProduct,
908 (product) ? product : "");
909 buf = get_libusb_string_descriptor (hdl, desc.iSerialNumber);
910 printf ("iSerialNumber %d (%s)\n", desc.iSerialNumber,
911 (buf) ? buf : "");
912 if (buf)
913 free (buf);
914 printf ("bNumConfigurations %d\n", desc.bNumConfigurations);
915
916 for (config_nr = 0; config_nr < desc.bNumConfigurations; config_nr++)
917 {
918 struct libusb_config_descriptor *c;
919
920 ret = libusb_get_config_descriptor (dev, config_nr, &c);
921 if (ret < 0)
922 {
923 printf ("could not get configuration descriptor %d: %s\n",
924 config_nr, sfs_libusb_strerror (ret));
925 continue;
926 }
927
928 printf (" <configuration %d>\n", config_nr);
929 printf (" bLength %d\n", c->bLength);
930 printf (" bDescriptorType %d\n", c->bDescriptorType);
931 printf (" wTotalLength %d\n", c->wTotalLength);
932 printf (" bNumInterfaces %d\n", c->bNumInterfaces);
933 printf (" bConfigurationValue %d\n", c->bConfigurationValue);
934
935 buf = get_libusb_string_descriptor (hdl, c->iConfiguration);
936 printf (" iConfiguration %d (%s)\n", c->iConfiguration,
937 (buf) ? buf : "");
938 free (buf);
939
940 printf (" bmAttributes %d (%s%s)\n", c->bmAttributes,
941 c->bmAttributes & 64 ? "Self-powered" : "",
942 c->bmAttributes & 32 ? "Remote Wakeup" : "");
943 printf (" MaxPower %d mA\n", c->MaxPower * 2);
944
945 for (intf = 0; intf < c->bNumInterfaces; intf++)
946 {
947 const struct libusb_interface *interface;
948 int alt_setting_nr;
949
950 interface = &c->interface[intf];
951
952 printf (" <interface %d>\n", intf);
953 for (alt_setting_nr = 0;
954 alt_setting_nr < interface->num_altsetting;
955 alt_setting_nr++)
956 {
957 const struct libusb_interface_descriptor *i;
958 int ep_nr;
959
960 i = &interface->altsetting[alt_setting_nr];
961
962 printf (" <altsetting %d>\n", alt_setting_nr);
963 printf (" bLength %d\n", i->bLength);
964 printf (" bDescriptorType %d\n", i->bDescriptorType);
965 printf (" bInterfaceNumber %d\n", i->bInterfaceNumber);
966 printf (" bAlternateSetting %d\n", i->bAlternateSetting);
967 printf (" bNumEndpoints %d\n", i->bNumEndpoints);
968 printf (" bInterfaceClass %d\n", i->bInterfaceClass);
969 printf (" bInterfaceSubClass %d\n",
970 i->bInterfaceSubClass);
971 printf (" bInterfaceProtocol %d\n",
972 i->bInterfaceProtocol);
973
974 buf = NULL;
975 buf = get_libusb_string_descriptor (hdl, i->iInterface);
976 printf (" iInterface %d (%s)\n", i->iInterface,
977 (buf) ? buf : "");
978 free (buf);
979 for (ep_nr = 0; ep_nr < i->bNumEndpoints; ep_nr++)
980 {
981 const struct libusb_endpoint_descriptor *e;
982 char *ep_type;
983
984 e = &i->endpoint[ep_nr];
985
986 switch (e->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK)
987 {
988 case LIBUSB_TRANSFER_TYPE_CONTROL:
989 ep_type = "control";
990 break;
991 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
992 ep_type = "isochronous";
993 break;
994 case LIBUSB_TRANSFER_TYPE_BULK:
995 ep_type = "bulk";
996 break;
997 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
998 ep_type = "interrupt";
999 break;
1000 default:
1001 ep_type = "unknown";
1002 break;
1003 }
1004 printf (" <endpoint %d>\n", ep_nr);
1005 printf (" bLength %d\n", e->bLength);
1006 printf (" bDescriptorType %d\n",
1007 e->bDescriptorType);
1008 printf (" bEndpointAddress 0x%02X (%s 0x%02X)\n",
1009 e->bEndpointAddress,
1010 e->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK ?
1011 "in" : "out",
1012 e->
1013 bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK);
1014 printf (" bmAttributes %d (%s)\n",
1015 e->bmAttributes, ep_type);
1016 printf (" wMaxPacketSize %d\n",
1017 e->wMaxPacketSize);
1018 printf (" bInterval %d ms\n", e->bInterval);
1019 printf (" bRefresh %d\n", e->bRefresh);
1020 printf (" bSynchAddress %d\n", e->bSynchAddress);
1021 }
1022 }
1023 }
1024 }
1025 }
1026
1027
1028 /* Some heuristics, which device may be a scanner */
1029 if (desc.idVendor == 0) /* hub */
1030 --is_scanner;
1031 if (desc.idProduct == 0) /* hub */
1032 --is_scanner;
1033
1034 ret = libusb_get_config_descriptor (dev, 0, &config0);
1035 if (ret < 0)
1036 {
1037 printf ("could not get config[0] descriptor: %s\n",
1038 sfs_libusb_strerror (ret));
1039
1040 goto out_free;
1041 }
1042
1043 for (intf = 0; (intf < config0->bNumInterfaces) && (is_scanner <= 0); intf++)
1044 {
1045 switch (desc.bDeviceClass)
1046 {
1047 case USB_CLASS_VENDOR_SPEC:
1048 ++is_scanner;
1049 break;
1050 case USB_CLASS_PER_INTERFACE:
1051 if ((config0->interface[intf].num_altsetting == 0)
1052 || !config0->interface[intf].altsetting)
1053 break;
1054 switch (config0->interface[intf].altsetting[0].bInterfaceClass)
1055 {
1056 case USB_CLASS_VENDOR_SPEC:
1057 case USB_CLASS_PER_INTERFACE:
1058 case 16: /* data? */
1059 ++is_scanner;
1060 break;
1061 }
1062 break;
1063 }
1064 }
1065
1066 if (is_scanner > 0)
1067 {
1068 char *chipset = NULL;
1069
1070 if(!from_file)
1071 chipset = check_usb_chip (verbose, desc, hdl, config0);
1072
1073 printf ("found possible USB scanner (vendor=0x%04x", vid);
1074 if (vendor)
1075 printf (" [%s]", vendor);
1076 printf (", product=0x%04x", pid);
1077 if (product)
1078 printf (" [%s]", product);
1079 if (chipset)
1080 printf (", chip=%s", chipset);
1081 if (from_file)
1082 printf (")\n");
1083 else
1084 printf (") at libusb:%03d:%03d\n", busno, address);
1085
1086 libusb_device_found = SANE_TRUE;
1087 device_found = SANE_TRUE;
1088 }
1089
1090 libusb_free_config_descriptor (config0);
1091
1092 out_free:
1093 libusb_close (hdl);
1094 if (vendor)
1095 free (vendor);
1096
1097 if (product)
1098 free (product);
1099 }
1100 #endif /* HAVE_LIBUSB */
1101
1102
1103 static DIR *
scan_directory(char * dir_name)1104 scan_directory (char *dir_name)
1105 {
1106 struct stat stat_buf;
1107 DIR *dir;
1108
1109 if (verbose > 2)
1110 printf ("scanning directory %s\n", dir_name);
1111
1112 if (stat (dir_name, &stat_buf) < 0)
1113 {
1114 if (verbose > 1)
1115 printf ("cannot stat `%s' (%s)\n", dir_name, strerror (errno));
1116 return 0;
1117 }
1118 if (!S_ISDIR (stat_buf.st_mode))
1119 {
1120 if (verbose > 1)
1121 printf ("`%s' is not a directory\n", dir_name);
1122 return 0;
1123 }
1124 if ((dir = opendir (dir_name)) == 0)
1125 {
1126 if (verbose > 1)
1127 printf ("cannot read directory `%s' (%s)\n", dir_name,
1128 strerror (errno));
1129 return 0;
1130 }
1131 return dir;
1132 }
1133
1134 static char *
get_next_file(char * dir_name,DIR * dir)1135 get_next_file (char *dir_name, DIR * dir)
1136 {
1137 struct dirent *dir_entry;
1138 static char file_name[PATH_MAX];
1139
1140 do
1141 {
1142 dir_entry = readdir (dir);
1143 if (!dir_entry)
1144 return 0;
1145 }
1146 while (strcmp (dir_entry->d_name, ".") == 0
1147 || strcmp (dir_entry->d_name, "..") == 0);
1148
1149 if (strlen (dir_name) + strlen (dir_entry->d_name) + 1 > PATH_MAX)
1150 {
1151 if (verbose > 1)
1152 printf ("filename too long\n");
1153 return 0;
1154 }
1155 sprintf (file_name, "%s%s", dir_name, dir_entry->d_name);
1156 return file_name;
1157 }
1158
1159 #if defined(WIN32_SCSI)
1160 /* Return a list of potential scanners. There's a lot of hardcoded values here that might break on a system with lots of scsi devices. */
build_scsi_dev_list(void)1161 static char **build_scsi_dev_list(void)
1162 {
1163 char **dev_list;
1164 int dev_list_index;
1165 int hca;
1166 HANDLE fd;
1167 char scsi_hca_name[20];
1168 char buffer[4096];
1169 DWORD BytesReturned;
1170 BOOL ret;
1171 size_t dev_list_size;
1172 PSCSI_ADAPTER_BUS_INFO adapter;
1173 PSCSI_INQUIRY_DATA inquiry;
1174 int i;
1175
1176 /* Allocate room for about 100 scanners. That should be enough. */
1177 dev_list_size = 100;
1178 dev_list_index = 0;
1179 dev_list = calloc(1, dev_list_size * sizeof(char *));
1180
1181 hca = 0;
1182
1183 for(hca = 0; ; hca++) {
1184
1185 /* Open the adapter */
1186 snprintf(scsi_hca_name, 20, "\\\\.\\Scsi%d:", hca);
1187 fd = CreateFile(scsi_hca_name, GENERIC_READ | GENERIC_WRITE,
1188 FILE_SHARE_READ | FILE_SHARE_WRITE,
1189 NULL, OPEN_EXISTING,
1190 FILE_FLAG_RANDOM_ACCESS, NULL );
1191
1192 if (fd == INVALID_HANDLE_VALUE) {
1193 /* Assume there is no more adapter. This is wrong in the case
1194 * of hot-plug stuff, but I have yet to see it on a user
1195 * machine. */
1196 break;
1197 }
1198
1199 /* Get the inquiry info for the devices on that hca. */
1200 ret = DeviceIoControl(fd,
1201 IOCTL_SCSI_GET_INQUIRY_DATA,
1202 NULL,
1203 0,
1204 buffer,
1205 sizeof(buffer),
1206 &BytesReturned,
1207 FALSE);
1208
1209 if(ret == 0)
1210 {
1211 CloseHandle(fd);
1212 continue;
1213 }
1214
1215 adapter = (PSCSI_ADAPTER_BUS_INFO)buffer;
1216
1217 for(i = 0; i < adapter->NumberOfBuses; i++) {
1218
1219 if (adapter->BusData[i].InquiryDataOffset == 0) {
1220 /* No device here */
1221 continue;
1222 }
1223
1224 inquiry = (PSCSI_INQUIRY_DATA) (buffer +
1225 adapter->BusData[i].InquiryDataOffset);
1226 while(1) {
1227 /* Check if it is a scanner or a processor
1228 * device. Ignore the other
1229 * device types. */
1230 if (inquiry->InquiryDataLength >= 5 &&
1231 ((inquiry->InquiryData[0] & 0x1f) == 3 ||
1232 (inquiry->InquiryData[0] & 0x1f) == 6)) {
1233 char device_name[20];
1234 sprintf(device_name, "h%db%dt%dl%d", hca, inquiry->PathId, inquiry->TargetId, inquiry->Lun);
1235 dev_list[dev_list_index] = strdup(device_name);
1236 dev_list_index++;
1237 }
1238
1239 if (inquiry->NextInquiryDataOffset == 0) {
1240 /* No device here */
1241 break;
1242 } else {
1243 inquiry = (PSCSI_INQUIRY_DATA) (buffer +
1244 inquiry->NextInquiryDataOffset);
1245 }
1246 }
1247 }
1248
1249 CloseHandle(fd);
1250
1251 }
1252
1253 return dev_list;
1254
1255 }
1256 #endif
1257
1258 #if defined (HAVE_IOKIT_CDB_IOSCSILIB_H) || \
1259 defined (HAVE_IOKIT_SCSI_SCSICOMMANDOPERATIONCODES_H) || \
1260 defined (HAVE_IOKIT_SCSI_COMMANDS_SCSICOMMANDOPERATIONCODES_H)
1261 char **scsi_dev_list;
1262 int scsi_dev_list_index;
1263
AddToSCSIDeviceList(const char * dev)1264 static SANE_Status AddToSCSIDeviceList (const char *dev) {
1265 if (scsi_dev_list_index < 99) {
1266 scsi_dev_list [scsi_dev_list_index] = strdup (dev);
1267 scsi_dev_list_index++;
1268 return SANE_STATUS_GOOD;
1269 }
1270 else
1271 return SANE_STATUS_NO_MEM;
1272 }
1273
build_scsi_dev_list(void)1274 static char **build_scsi_dev_list(void)
1275 {
1276 scsi_dev_list_index = 0;
1277 scsi_dev_list = malloc (100 * sizeof(char *));
1278 sanei_scsi_find_devices (NULL, NULL, NULL, -1, -1, -1, -1,
1279 AddToSCSIDeviceList);
1280 scsi_dev_list [scsi_dev_list_index] = NULL;
1281 return scsi_dev_list;
1282 }
1283 #endif
1284
1285 static int
check_mustek_pp_device(void)1286 check_mustek_pp_device (void)
1287 {
1288 const char **devices;
1289 int ctr = 0, found = 0, scsi = 0;
1290
1291 if (verbose > 1)
1292 printf ("searching for Mustek parallel port scanners:\n");
1293
1294 devices = sanei_pa4s2_devices ();
1295
1296 while (devices[ctr] != NULL) {
1297 int fd;
1298 SANE_Status result;
1299
1300 /* ordinary parallel port scanner type */
1301 if (verbose > 1)
1302 printf ("checking %s...", devices[ctr]);
1303
1304 result = sanei_pa4s2_open (devices[ctr], &fd);
1305
1306 if (verbose > 1)
1307 {
1308 if (result != 0)
1309 printf (" failed to open (%s)\n", sane_strstatus (result));
1310 else
1311 printf (" open ok\n");
1312 }
1313
1314 if (result == 0) {
1315 printf ("found possible Mustek parallel port scanner at \"%s\"\n",
1316 devices[ctr]);
1317 found++;
1318 sanei_pa4s2_close(fd);
1319 }
1320
1321 /* trying scsi over pp devices */
1322 if (verbose > 1)
1323 printf ("checking %s (SCSI emulation)...", devices[ctr]);
1324
1325 result = sanei_pa4s2_scsi_pp_open (devices[ctr], &fd);
1326
1327 if (verbose > 1)
1328 {
1329 if (result != 0)
1330 printf (" failed to open (%s)\n", sane_strstatus (result));
1331 else
1332 printf (" open ok\n");
1333 }
1334
1335 if (result == 0) {
1336 printf ("found possible Mustek SCSI over PP scanner at \"%s\"\n",
1337 devices[ctr]);
1338 scsi++;
1339 sanei_pa4s2_close(fd);
1340 }
1341
1342 ctr++;
1343 }
1344
1345 free(devices);
1346
1347 if (found > 0 && verbose > 0)
1348 printf("\n # Your Mustek parallel port scanner was detected. It may or\n"
1349 " # may not be supported by SANE. Please read the sane-mustek_pp\n"
1350 " # man-page for setup instructions.\n");
1351
1352 if (scsi > 0 && verbose > 0)
1353 printf("\n # Your Mustek parallel port scanner was detected. It may or\n"
1354 " # may not be supported by SANE. Please read the sane-mustek_pp\n"
1355 " # man-page for setup instructions.\n");
1356
1357 return (found > 0 || scsi > 0);
1358 }
1359
1360 #ifdef HAVE_LIBUSB_LEGACY
1361 static SANE_Bool
parse_num(char * search,const char * line,int base,long int * number)1362 parse_num (char* search, const char* line, int base, long int * number)
1363 {
1364 char* start_number;
1365
1366 start_number = strstr (line, search);
1367 if (start_number == NULL)
1368 return SANE_FALSE;
1369 start_number += strlen (search);
1370
1371 *number = strtol (start_number, NULL, base);
1372 if (verbose > 2)
1373 printf ("Found %s%ld\n", search, *number);
1374 return SANE_TRUE;
1375 }
1376
1377 static SANE_Bool
parse_bcd(char * search,const char * line,long int * number)1378 parse_bcd (char* search, const char* line, long int * number)
1379 {
1380 char* start_number;
1381 char* end_number;
1382 int first_part;
1383 int second_part;
1384
1385 start_number = strstr (line, search);
1386 if (start_number == NULL)
1387 return SANE_FALSE;
1388 start_number += strlen (search);
1389
1390 first_part = strtol (start_number, &end_number, 10);
1391 start_number = end_number + 1; /* skip colon */
1392 second_part = strtol (start_number, NULL, 10);
1393 *number = ((first_part / 10) << 12) + ((first_part % 10) << 8)
1394 + ((second_part / 10) << 4) + (second_part % 10);
1395 if (verbose > 2)
1396 printf ("Found %s%ld\n", search, *number);
1397 return SANE_TRUE;
1398 }
1399
1400 static void
parse_file(char * filename)1401 parse_file (char *filename)
1402 {
1403 FILE * parsefile;
1404 char line [PATH_MAX], *token;
1405 const char * p;
1406 struct usb_device *dev = 0;
1407 long int number = 0;
1408 int current_config = 1;
1409 int current_if = -1;
1410 int current_as = -1;
1411 int current_ep = -1;
1412
1413 if (verbose > 1)
1414 printf ("trying to open %s\n", filename);
1415 parsefile = fopen (filename, "r");
1416
1417 if (parsefile == NULL)
1418 {
1419 if (verbose > 0)
1420 printf ("opening %s failed: %s\n", filename, strerror (errno));
1421 return;
1422 }
1423
1424 while (sanei_config_read (line, PATH_MAX, parsefile))
1425 {
1426 if (verbose > 2)
1427 printf ("parsing line: `%s'\n", line);
1428 p = sanei_config_get_string (line, &token);
1429 if (!token || !p || token[0] == '\0')
1430 continue;
1431 if (token[1] != ':')
1432 {
1433 if (verbose > 2)
1434 printf ("missing `:'?\n");
1435 continue;
1436 }
1437 switch (token[0])
1438 {
1439 case 'T':
1440 if (dev)
1441 check_libusb_device (dev, SANE_TRUE);
1442 dev = calloc (1, sizeof (struct usb_device));
1443 dev->bus = calloc (1, sizeof (struct usb_bus));
1444 current_config = 1;
1445 current_if = -1;
1446 current_as = -1;
1447 current_ep = -1;
1448 break;
1449 case 'D':
1450 if (parse_bcd ("Ver=", line, &number))
1451 dev->descriptor.bcdUSB = number;
1452 if (parse_num ("Cls=", line, 16, &number))
1453 dev->descriptor.bDeviceClass = number;
1454 if (parse_num ("Sub=", line, 16, &number))
1455 dev->descriptor.bDeviceSubClass = number;
1456 if (parse_num ("Prot=", line, 16, &number))
1457 dev->descriptor.bDeviceProtocol = number;
1458 if (parse_num ("MxPS=", line, 10, &number))
1459 dev->descriptor.bMaxPacketSize0 = number;
1460 if (parse_num ("#Cfgs=", line, 10, &number))
1461 dev->descriptor.bNumConfigurations = number;
1462 dev->config = calloc (number, sizeof (struct usb_config_descriptor));
1463 break;
1464 case 'P':
1465 if (parse_num ("Vendor=", line, 16, &number))
1466 dev->descriptor.idVendor = number;
1467 if (parse_num ("ProdID=", line, 16, &number))
1468 dev->descriptor.idProduct = number;
1469 if (parse_bcd ("Rev=", line, &number))
1470 dev->descriptor.bcdDevice = number;
1471 break;
1472 case 'C':
1473 current_if = -1;
1474 current_as = -1;
1475 current_ep = -1;
1476 if (parse_num ("Cfg#=", line, 10, &number))
1477 {
1478 current_config = number - 1;
1479 dev->config[current_config].bConfigurationValue = number;
1480 }
1481 if (parse_num ("Ifs=", line, 10, &number))
1482 dev->config[current_config].bNumInterfaces = number;
1483 dev->config[current_config].interface
1484 = calloc (number, sizeof (struct usb_interface));
1485 if (parse_num ("Atr=", line, 16, &number))
1486 dev->config[current_config].bmAttributes = number;
1487 if (parse_num ("MxPwr=", line, 10, &number))
1488 dev->config[current_config].MaxPower = number / 2;
1489 break;
1490 case 'I':
1491 current_ep = -1;
1492 if (parse_num ("If#=", line, 10, &number))
1493 {
1494 if (current_if != number)
1495 {
1496 current_if = number;
1497 current_as = -1;
1498 dev->config[current_config].interface[current_if].altsetting
1499 = calloc (20, sizeof (struct usb_interface_descriptor));
1500 /* Can't read number of altsettings */
1501 dev->config[current_config].interface[current_if].num_altsetting = 1;
1502 }
1503 else
1504 dev->config[current_config].interface[current_if].num_altsetting++;
1505 }
1506 if (parse_num ("Alt=", line, 10, &number))
1507 {
1508 current_as = number;
1509 dev->config[current_config].interface[current_if].altsetting[current_as].bInterfaceNumber
1510 = current_if;
1511 dev->config[current_config].interface[current_if].altsetting[current_as].bAlternateSetting
1512 = current_as;
1513 }
1514 if (parse_num ("#EPs=", line, 10, &number))
1515 dev->config[current_config].interface[current_if].altsetting[current_as].bNumEndpoints
1516 = number;
1517 dev->config[current_config].interface[current_if].altsetting[current_as].endpoint
1518 = calloc (number, sizeof (struct usb_endpoint_descriptor));
1519 if (parse_num ("Cls=", line, 16, &number))
1520 dev->config[current_config].interface[current_if].altsetting[current_as].bInterfaceClass
1521 = number;
1522 if (parse_num ("Sub=", line, 16, &number))
1523 dev->config[current_config].interface[current_if].altsetting[current_as].bInterfaceSubClass
1524 = number;
1525 if (parse_num ("Prot=", line, 16, &number))
1526 dev->config[current_config].interface[current_if].altsetting[current_as].bInterfaceProtocol
1527 = number;
1528 break;
1529 case 'E':
1530 current_ep++;
1531 if (parse_num ("Ad=", line, 16, &number))
1532 dev->config[current_config].interface[current_if].altsetting[current_as]
1533 .endpoint[current_ep].bEndpointAddress = number;
1534 if (parse_num ("Atr=", line, 16, &number))
1535 dev->config[current_config].interface[current_if].altsetting[current_as]
1536 .endpoint[current_ep].bmAttributes = number;
1537 if (parse_num ("MxPS=", line, 10, &number))
1538 dev->config[current_config].interface[current_if].altsetting[current_as]
1539 .endpoint[current_ep].wMaxPacketSize = number;
1540 if (parse_num ("Ivl=", line, 10, &number))
1541 dev->config[current_config].interface[current_if].altsetting[current_as]
1542 .endpoint[current_ep].bInterval = number;
1543 break;
1544 case 'S':
1545 case 'B':
1546 continue;
1547 default:
1548 if (verbose > 1)
1549 printf ("ignoring unknown line identifier: %c\n", token[0]);
1550 continue;
1551 }
1552 free (token);
1553 }
1554 if (dev)
1555 check_libusb_device (dev, SANE_TRUE);
1556 fclose (parsefile);
1557 return;
1558 }
1559 #endif
1560
1561 int
main(int argc,char ** argv)1562 main (int argc, char **argv)
1563 {
1564 char **dev_list, **usb_dev_list, *dev_name, **ap;
1565 int enable_pp_checks = SANE_FALSE;
1566
1567 prog_name = strrchr (argv[0], '/');
1568 if (prog_name)
1569 ++prog_name;
1570 else
1571 prog_name = argv[0];
1572
1573 for (ap = argv + 1; ap < argv + argc; ++ap)
1574 {
1575 if ((*ap)[0] != '-')
1576 break;
1577 switch ((*ap)[1])
1578 {
1579 case '?':
1580 case 'h':
1581 usage (0);
1582 exit (0);
1583
1584 case 'v':
1585 ++verbose;
1586 break;
1587
1588 case 'q':
1589 --verbose;
1590 break;
1591
1592 case 'f':
1593 force = SANE_TRUE;
1594 break;
1595
1596 case 'p':
1597 enable_pp_checks = SANE_TRUE;
1598 break;
1599
1600 case 'F':
1601 #ifdef HAVE_LIBUSB_LEGACY
1602 parse_file ((char *) (*(++ap)));
1603 #elif defined(HAVE_LIBUSB)
1604 printf ("option -F not implemented with libusb-1.0\n");
1605 #else
1606 printf ("libusb not available: option -F can't be used\n");
1607 #endif
1608 exit (0);
1609
1610 case '-':
1611 if (!strcmp((*ap), "--help"))
1612 {
1613 usage (0);
1614 exit (0);
1615 }
1616 // fall through
1617 default:
1618 printf ("unknown option: -%c, try -h for help\n", (*ap)[1]);
1619 exit (0);
1620 }
1621 }
1622 if (ap < argv + argc)
1623 {
1624 dev_list = ap;
1625 usb_dev_list = ap;
1626 }
1627 else
1628 {
1629 static char *default_dev_list[] = {
1630 #if defined(__sgi)
1631 "/dev/scsi/sc0d1l0", "/dev/scsi/sc0d2l0",
1632 "/dev/scsi/sc0d3l0", "/dev/scsi/sc0d4l0",
1633 "/dev/scsi/sc0d5l0", "/dev/scsi/sc0d6l0",
1634 "/dev/scsi/sc0d7l0", "/dev/scsi/sc0d8l0",
1635 "/dev/scsi/sc0d9l0", "/dev/scsi/sc0d10l0",
1636 "/dev/scsi/sc0d11l0", "/dev/scsi/sc0d12l0",
1637 "/dev/scsi/sc0d13l0", "/dev/scsi/sc0d14l0",
1638 "/dev/scsi/sc0d15l0",
1639 "/dev/scsi/sc1d1l0", "/dev/scsi/sc1d2l0",
1640 "/dev/scsi/sc1d3l0", "/dev/scsi/sc1d4l0",
1641 "/dev/scsi/sc1d5l0", "/dev/scsi/sc1d6l0",
1642 "/dev/scsi/sc1d7l0", "/dev/scsi/sc1d8l0",
1643 "/dev/scsi/sc1d9l0", "/dev/scsi/sc1d10l0",
1644 "/dev/scsi/sc1d11l0", "/dev/scsi/sc1d12l0",
1645 "/dev/scsi/sc1d13l0", "/dev/scsi/sc1d14l0",
1646 "/dev/scsi/sc1d15l0",
1647 "/dev/scsi/sc2d1l0", "/dev/scsi/sc2d2l0",
1648 "/dev/scsi/sc2d3l0", "/dev/scsi/sc2d4l0",
1649 "/dev/scsi/sc2d5l0", "/dev/scsi/sc2d6l0",
1650 "/dev/scsi/sc2d7l0", "/dev/scsi/sc2d8l0",
1651 "/dev/scsi/sc2d9l0", "/dev/scsi/sc2d10l0",
1652 "/dev/scsi/sc2d11l0", "/dev/scsi/sc2d12l0",
1653 "/dev/scsi/sc2d13l0", "/dev/scsi/sc2d14l0",
1654 "/dev/scsi/sc2d15l0",
1655 "/dev/scsi/sc3d1l0", "/dev/scsi/sc3d2l0",
1656 "/dev/scsi/sc3d3l0", "/dev/scsi/sc3d4l0",
1657 "/dev/scsi/sc3d5l0", "/dev/scsi/sc3d6l0",
1658 "/dev/scsi/sc3d7l0", "/dev/scsi/sc3d8l0",
1659 "/dev/scsi/sc3d9l0", "/dev/scsi/sc3d10l0",
1660 "/dev/scsi/sc3d11l0", "/dev/scsi/sc3d12l0",
1661 "/dev/scsi/sc3d13l0", "/dev/scsi/sc3d14l0",
1662 "/dev/scsi/sc3d15l0",
1663 "/dev/scsi/sc4d1l0", "/dev/scsi/sc4d2l0",
1664 "/dev/scsi/sc4d3l0", "/dev/scsi/sc4d4l0",
1665 "/dev/scsi/sc4d5l0", "/dev/scsi/sc4d6l0",
1666 "/dev/scsi/sc4d7l0", "/dev/scsi/sc4d8l0",
1667 "/dev/scsi/sc4d9l0", "/dev/scsi/sc4d10l0",
1668 "/dev/scsi/sc4d11l0", "/dev/scsi/sc4d12l0",
1669 "/dev/scsi/sc4d13l0", "/dev/scsi/sc4d14l0",
1670 "/dev/scsi/sc4d15l0",
1671 "/dev/scsi/sc5d1l0", "/dev/scsi/sc5d2l0",
1672 "/dev/scsi/sc5d3l0", "/dev/scsi/sc5d4l0",
1673 "/dev/scsi/sc5d5l0", "/dev/scsi/sc5d6l0",
1674 "/dev/scsi/sc5d7l0", "/dev/scsi/sc5d8l0",
1675 "/dev/scsi/sc5d9l0", "/dev/scsi/sc5d10l0",
1676 "/dev/scsi/sc5d11l0", "/dev/scsi/sc5d12l0",
1677 "/dev/scsi/sc5d13l0", "/dev/scsi/sc5d14l0",
1678 "/dev/scsi/sc5d15l0",
1679 "/dev/scsi/sc6d1l0", "/dev/scsi/sc6d2l0",
1680 "/dev/scsi/sc6d3l0", "/dev/scsi/sc6d4l0",
1681 "/dev/scsi/sc6d5l0", "/dev/scsi/sc6d6l0",
1682 "/dev/scsi/sc6d7l0", "/dev/scsi/sc6d8l0",
1683 "/dev/scsi/sc6d9l0", "/dev/scsi/sc6d10l0",
1684 "/dev/scsi/sc6d11l0", "/dev/scsi/sc6d12l0",
1685 "/dev/scsi/sc6d13l0", "/dev/scsi/sc6d14l0",
1686 "/dev/scsi/sc6d15l0",
1687 "/dev/scsi/sc7d1l0", "/dev/scsi/sc7d2l0",
1688 "/dev/scsi/sc7d3l0", "/dev/scsi/sc7d4l0",
1689 "/dev/scsi/sc7d5l0", "/dev/scsi/sc7d6l0",
1690 "/dev/scsi/sc7d7l0", "/dev/scsi/sc7d8l0",
1691 "/dev/scsi/sc7d9l0", "/dev/scsi/sc7d10l0",
1692 "/dev/scsi/sc7d11l0", "/dev/scsi/sc7d12l0",
1693 "/dev/scsi/sc7d13l0", "/dev/scsi/sc7d14l0",
1694 "/dev/scsi/sc7d15l0",
1695 "/dev/scsi/sc8d1l0", "/dev/scsi/sc8d2l0",
1696 "/dev/scsi/sc8d3l0", "/dev/scsi/sc8d4l0",
1697 "/dev/scsi/sc8d5l0", "/dev/scsi/sc8d6l0",
1698 "/dev/scsi/sc8d7l0", "/dev/scsi/sc8d8l0",
1699 "/dev/scsi/sc8d9l0", "/dev/scsi/sc8d10l0",
1700 "/dev/scsi/sc8d11l0", "/dev/scsi/sc8d12l0",
1701 "/dev/scsi/sc8d13l0", "/dev/scsi/sc8d14l0",
1702 "/dev/scsi/sc8d15l0",
1703 "/dev/scsi/sc9d1l0", "/dev/scsi/sc9d2l0",
1704 "/dev/scsi/sc9d3l0", "/dev/scsi/sc9d4l0",
1705 "/dev/scsi/sc9d5l0", "/dev/scsi/sc9d6l0",
1706 "/dev/scsi/sc9d7l0", "/dev/scsi/sc9d8l0",
1707 "/dev/scsi/sc9d9l0", "/dev/scsi/sc9d10l0",
1708 "/dev/scsi/sc9d11l0", "/dev/scsi/sc9d12l0",
1709 "/dev/scsi/sc9d13l0", "/dev/scsi/sc9d14l0",
1710 "/dev/scsi/sc9d15l0",
1711 "/dev/scsi/sc10d1l0", "/dev/scsi/sc10d2l0",
1712 "/dev/scsi/sc10d3l0", "/dev/scsi/sc10d4l0",
1713 "/dev/scsi/sc10d5l0", "/dev/scsi/sc10d6l0",
1714 "/dev/scsi/sc10d7l0", "/dev/scsi/sc10d8l0",
1715 "/dev/scsi/sc10d9l0", "/dev/scsi/sc10d10l0",
1716 "/dev/scsi/sc10d11l0", "/dev/scsi/sc10d12l0",
1717 "/dev/scsi/sc10d13l0", "/dev/scsi/sc10d14l0",
1718 "/dev/scsi/sc10d15l0",
1719 "/dev/scsi/sc11d1l0", "/dev/scsi/sc11d2l0",
1720 "/dev/scsi/sc11d3l0", "/dev/scsi/sc11d4l0",
1721 "/dev/scsi/sc11d5l0", "/dev/scsi/sc11d6l0",
1722 "/dev/scsi/sc11d7l0", "/dev/scsi/sc11d8l0",
1723 "/dev/scsi/sc11d9l0", "/dev/scsi/sc11d10l0",
1724 "/dev/scsi/sc11d11l0", "/dev/scsi/sc11d12l0",
1725 "/dev/scsi/sc11d13l0", "/dev/scsi/sc11d14l0",
1726 "/dev/scsi/sc11d15l0",
1727 "/dev/scsi/sc12d1l0", "/dev/scsi/sc12d2l0",
1728 "/dev/scsi/sc12d3l0", "/dev/scsi/sc12d4l0",
1729 "/dev/scsi/sc12d5l0", "/dev/scsi/sc12d6l0",
1730 "/dev/scsi/sc12d7l0", "/dev/scsi/sc12d8l0",
1731 "/dev/scsi/sc12d9l0", "/dev/scsi/sc12d10l0",
1732 "/dev/scsi/sc12d11l0", "/dev/scsi/sc12d12l0",
1733 "/dev/scsi/sc12d13l0", "/dev/scsi/sc12d14l0",
1734 "/dev/scsi/sc12d15l0",
1735 "/dev/scsi/sc13d1l0", "/dev/scsi/sc13d2l0",
1736 "/dev/scsi/sc13d3l0", "/dev/scsi/sc13d4l0",
1737 "/dev/scsi/sc13d5l0", "/dev/scsi/sc13d6l0",
1738 "/dev/scsi/sc13d7l0", "/dev/scsi/sc13d8l0",
1739 "/dev/scsi/sc13d9l0", "/dev/scsi/sc13d10l0",
1740 "/dev/scsi/sc13d11l0", "/dev/scsi/sc13d12l0",
1741 "/dev/scsi/sc13d13l0", "/dev/scsi/sc13d14l0",
1742 "/dev/scsi/sc13d15l0",
1743 "/dev/scsi/sc14d1l0", "/dev/scsi/sc14d2l0",
1744 "/dev/scsi/sc14d3l0", "/dev/scsi/sc14d4l0",
1745 "/dev/scsi/sc14d5l0", "/dev/scsi/sc14d6l0",
1746 "/dev/scsi/sc14d7l0", "/dev/scsi/sc14d8l0",
1747 "/dev/scsi/sc14d9l0", "/dev/scsi/sc14d10l0",
1748 "/dev/scsi/sc14d11l0", "/dev/scsi/sc14d12l0",
1749 "/dev/scsi/sc14d13l0", "/dev/scsi/sc14d14l0",
1750 "/dev/scsi/sc14d15l0",
1751 "/dev/scsi/sc15d1l0", "/dev/scsi/sc15d2l0",
1752 "/dev/scsi/sc15d3l0", "/dev/scsi/sc15d4l0",
1753 "/dev/scsi/sc15d5l0", "/dev/scsi/sc15d6l0",
1754 "/dev/scsi/sc15d7l0", "/dev/scsi/sc15d8l0",
1755 "/dev/scsi/sc15d9l0", "/dev/scsi/sc15d10l0",
1756 "/dev/scsi/sc15d11l0", "/dev/scsi/sc15d12l0",
1757 "/dev/scsi/sc15d13l0", "/dev/scsi/sc15d14l0",
1758 "/dev/scsi/sc15d15l0",
1759 #elif defined(__EMX__)
1760 "b0t0l0", "b0t1l0", "b0t2l0", "b0t3l0",
1761 "b0t4l0", "b0t5l0", "b0t6l0", "b0t7l0",
1762 "b1t0l0", "b1t1l0", "b1t2l0", "b1t3l0",
1763 "b1t4l0", "b1t5l0", "b1t6l0", "b1t7l0",
1764 "b2t0l0", "b2t1l0", "b2t2l0", "b2t3l0",
1765 "b2t4l0", "b2t5l0", "b2t6l0", "b2t7l0",
1766 "b3t0l0", "b3t1l0", "b3t2l0", "b3t3l0",
1767 "b3t4l0", "b3t5l0", "b3t6l0", "b3t7l0",
1768 #elif defined(__linux__)
1769 "/dev/scanner",
1770 "/dev/sg0", "/dev/sg1", "/dev/sg2", "/dev/sg3",
1771 "/dev/sg4", "/dev/sg5", "/dev/sg6", "/dev/sg7",
1772 "/dev/sg8", "/dev/sg9", "/dev/sg10", "/dev/sg11",
1773 "/dev/sg12", "/dev/sg13", "/dev/sg14", "/dev/sg15",
1774 "/dev/sg16", "/dev/sg17", "/dev/sg18", "/dev/sg19",
1775 "/dev/sg20", "/dev/sg21", "/dev/sg22", "/dev/sg23",
1776 "/dev/sg24", "/dev/sg25", "/dev/sg26", "/dev/sg27",
1777 "/dev/sg28", "/dev/sg29", "/dev/sg30", "/dev/sg31",
1778 "/dev/sga", "/dev/sgb", "/dev/sgc", "/dev/sgd",
1779 "/dev/sge", "/dev/sgf", "/dev/sgg", "/dev/sgh",
1780 "/dev/sgi", "/dev/sgj", "/dev/sgk", "/dev/sgl",
1781 "/dev/sgm", "/dev/sgn", "/dev/sgo", "/dev/sgp",
1782 "/dev/sgq", "/dev/sgr", "/dev/sgs", "/dev/sgt",
1783 "/dev/sgu", "/dev/sgv", "/dev/sgw", "/dev/sgx",
1784 "/dev/sgy", "/dev/sgz",
1785 #elif defined(__NeXT__)
1786 "/dev/sg0a", "/dev/sg0b", "/dev/sg0c", "/dev/sg0d",
1787 "/dev/sg0e", "/dev/sg0f", "/dev/sg0g", "/dev/sg0h",
1788 "/dev/sg1a", "/dev/sg1b", "/dev/sg1c", "/dev/sg1d",
1789 "/dev/sg1e", "/dev/sg1f", "/dev/sg1g", "/dev/sg1h",
1790 "/dev/sg2a", "/dev/sg2b", "/dev/sg2c", "/dev/sg2d",
1791 "/dev/sg2e", "/dev/sg2f", "/dev/sg2g", "/dev/sg2h",
1792 "/dev/sg3a", "/dev/sg3b", "/dev/sg3c", "/dev/sg3d",
1793 "/dev/sg3e", "/dev/sg3f", "/dev/sg3g", "/dev/sg3h",
1794 #elif defined(_AIX)
1795 "/dev/scanner",
1796 "/dev/gsc0", "/dev/gsc1", "/dev/gsc2", "/dev/gsc3",
1797 "/dev/gsc4", "/dev/gsc5", "/dev/gsc6", "/dev/gsc7",
1798 "/dev/gsc8", "/dev/gsc9", "/dev/gsc10", "/dev/gsc11",
1799 "/dev/gsc12", "/dev/gsc13", "/dev/gsc14", "/dev/gsc15",
1800 #elif defined(__sun)
1801 "/dev/scg0a", "/dev/scg0b", "/dev/scg0c", "/dev/scg0d",
1802 "/dev/scg0e", "/dev/scg0f", "/dev/scg0g",
1803 "/dev/scg1a", "/dev/scg1b", "/dev/scg1c", "/dev/scg1d",
1804 "/dev/scg1e", "/dev/scg1f", "/dev/scg1g",
1805 "/dev/scg2a", "/dev/scg2b", "/dev/scg2c", "/dev/scg2d",
1806 "/dev/scg2e", "/dev/scg2f", "/dev/scg2g",
1807 "/dev/sg/0", "/dev/sg/1", "/dev/sg/2", "/dev/sg/3",
1808 "/dev/sg/4", "/dev/sg/5", "/dev/sg/6",
1809 "/dev/scsi/scanner/", "/dev/scsi/processor/",
1810 #elif defined(HAVE_CAMLIB_H)
1811 "/dev/scanner", "/dev/scanner0", "/dev/scanner1",
1812 "/dev/pass0", "/dev/pass1", "/dev/pass2", "/dev/pass3",
1813 "/dev/pass4", "/dev/pass5", "/dev/pass6", "/dev/pass7",
1814 #elif defined(__FreeBSD__) || defined(__DragonFly__)
1815 "/dev/uk0", "/dev/uk1", "/dev/uk2", "/dev/uk3", "/dev/uk4",
1816 "/dev/uk5", "/dev/uk6",
1817 #elif defined(__NetBSD__)
1818 "/dev/uk0", "/dev/uk1", "/dev/uk2", "/dev/uk3", "/dev/uk4",
1819 "/dev/uk5", "/dev/uk6",
1820 "/dev/ss0",
1821 #elif defined(__OpenBSD__)
1822 "/dev/uk0", "/dev/uk1", "/dev/uk2", "/dev/uk3", "/dev/uk4",
1823 "/dev/uk5", "/dev/uk6",
1824 #elif defined(__hpux)
1825 "/dev/rscsi/",
1826 #endif
1827 0
1828 };
1829 static char *usb_default_dev_list[] = {
1830 #if defined(__linux__)
1831 "/dev/usb/scanner",
1832 "/dev/usb/scanner0", "/dev/usb/scanner1",
1833 "/dev/usb/scanner2", "/dev/usb/scanner3",
1834 "/dev/usb/scanner4", "/dev/usb/scanner5",
1835 "/dev/usb/scanner5", "/dev/usb/scanner7",
1836 "/dev/usb/scanner8", "/dev/usb/scanner9",
1837 "/dev/usb/scanner10", "/dev/usb/scanner11",
1838 "/dev/usb/scanner12", "/dev/usb/scanner13",
1839 "/dev/usb/scanner14", "/dev/usb/scanner15",
1840 "/dev/usbscanner",
1841 "/dev/usbscanner0", "/dev/usbscanner1",
1842 "/dev/usbscanner2", "/dev/usbscanner3",
1843 "/dev/usbscanner4", "/dev/usbscanner5",
1844 "/dev/usbscanner6", "/dev/usbscanner7",
1845 "/dev/usbscanner8", "/dev/usbscanner9",
1846 "/dev/usbscanner10", "/dev/usbscanner11",
1847 "/dev/usbscanner12", "/dev/usbscanner13",
1848 "/dev/usbscanner14", "/dev/usbscanner15",
1849 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
1850 "/dev/uscanner",
1851 "/dev/uscanner0", "/dev/uscanner1",
1852 "/dev/uscanner2", "/dev/uscanner3",
1853 "/dev/uscanner4", "/dev/uscanner5",
1854 "/dev/uscanner6", "/dev/uscanner7",
1855 "/dev/uscanner8", "/dev/uscanner9",
1856 "/dev/uscanner10", "/dev/uscanner11",
1857 "/dev/uscanner12", "/dev/uscanner13",
1858 "/dev/uscanner14", "/dev/uscanner15",
1859 #endif
1860 0
1861 };
1862
1863 #if defined (WIN32_SCSI) || \
1864 defined (HAVE_IOKIT_CDB_IOSCSILIB_H) || \
1865 defined (HAVE_IOKIT_SCSI_SCSICOMMANDOPERATIONCODES_H) || \
1866 defined (HAVE_IOKIT_SCSI_COMMANDS_SCSICOMMANDOPERATIONCODES_H)
1867 /* Build a list of valid of possible scanners found */
1868 dev_list = build_scsi_dev_list();
1869 #else
1870 dev_list = default_dev_list;
1871 #endif
1872
1873 usb_dev_list = usb_default_dev_list;
1874 }
1875 if (verbose > 1)
1876 printf ("This is sane-find-scanner from %s\n", PACKAGE_STRING);
1877
1878 if (verbose > 0)
1879 printf ("\n # sane-find-scanner will now attempt to detect your scanner. If the"
1880 "\n # result is different from what you expected, first make sure your"
1881 "\n # scanner is powered up and properly connected to your computer.\n\n");
1882
1883 if (verbose > 1)
1884 printf ("searching for SCSI scanners:\n");
1885
1886 while ((dev_name = *dev_list++))
1887 {
1888 if (strlen (dev_name) == 0)
1889 continue; /* Empty device names ... */
1890
1891 if (dev_name[strlen (dev_name) - 1] == '/')
1892 {
1893 /* check whole directories */
1894 DIR *dir;
1895 char *file_name;
1896
1897 dir = scan_directory (dev_name);
1898 if (!dir)
1899 continue;
1900
1901 while ((file_name = get_next_file (dev_name, dir)))
1902 check_scsi_file (file_name);
1903 }
1904 else
1905 {
1906 /* check device files */
1907 check_scsi_file (dev_name);
1908 }
1909 }
1910 if (device_found)
1911 {
1912 if (verbose > 0)
1913 printf
1914 (" # Your SCSI scanner was detected. It may or may not be "
1915 "supported by SANE. Try\n # scanimage -L and read the backend's "
1916 "manpage.\n");
1917 }
1918 else
1919 {
1920 if (verbose > 0)
1921 printf
1922 (" # No SCSI scanners found. If you expected something different, "
1923 "make sure that\n # you have loaded a kernel SCSI driver for your SCSI "
1924 "adapter.\n");
1925 if (!check_sg ())
1926 {
1927 if (verbose > 0)
1928 printf
1929 (" # Also you need support for SCSI Generic (sg) in your "
1930 "operating system.\n # If using Linux, try \"modprobe "
1931 "sg\".\n");
1932 }
1933 }
1934 if (verbose > 0)
1935 printf ("\n");
1936 device_found = SANE_FALSE;
1937 sanei_usb_init ();
1938 if (verbose > 1)
1939 printf ("searching for USB scanners:\n");
1940
1941 while ((dev_name = *usb_dev_list++))
1942 {
1943 if (strlen (dev_name) == 0)
1944 continue; /* Empty device names ... */
1945
1946 if (dev_name[strlen (dev_name) - 1] == '/')
1947 {
1948 /* check whole directories */
1949 DIR *dir;
1950 char *file_name;
1951
1952 dir = scan_directory (dev_name);
1953 if (!dir)
1954 continue;
1955
1956 while ((file_name = get_next_file (dev_name, dir)))
1957 check_usb_file (file_name);
1958 }
1959 else
1960 {
1961 /* check device files */
1962 check_usb_file (dev_name);
1963 }
1964 }
1965 #ifdef HAVE_LIBUSB_LEGACY
1966 /* Now the libusb devices */
1967 {
1968 struct usb_bus *bus;
1969 struct usb_device *dev;
1970
1971 if (ap < argv + argc)
1972 {
1973 /* user-specified devices not useful for libusb */
1974 if (verbose > 1)
1975 printf ("ignoring libusb devices\n");
1976 }
1977 else
1978 {
1979 if (verbose > 2)
1980 printf ("trying libusb:\n");
1981 for (bus = usb_get_busses (); bus; bus = bus->next)
1982 {
1983 for (dev = bus->devices; dev; dev = dev->next)
1984 {
1985 check_libusb_device (dev, SANE_FALSE);
1986 } /* for (dev) */
1987 } /* for (bus) */
1988 }
1989 }
1990 #elif defined(HAVE_LIBUSB)
1991 /* Now the libusb-1.0 devices */
1992 {
1993 if (ap < argv + argc)
1994 {
1995 /* user-specified devices not useful for libusb */
1996 if (verbose > 1)
1997 printf ("ignoring libusb devices\n");
1998 }
1999 else
2000 {
2001 libusb_device **devlist;
2002 ssize_t devcnt;
2003 int i;
2004 int ret;
2005
2006 if (verbose > 2)
2007 printf ("trying libusb:\n");
2008
2009 ret = libusb_init (&sfs_usb_ctx);
2010 if (ret < 0)
2011 {
2012 printf ("# Could not initialize libusb-1.0, error %d\n", ret);
2013 printf ("# Skipping libusb devices\n");
2014
2015 goto failed_libusb_1_0;
2016 }
2017
2018 if (verbose > 3)
2019 #if LIBUSB_API_VERSION >= 0x01000106
2020 libusb_set_option (sfs_usb_ctx, LIBUSB_OPTION_LOG_LEVEL,
2021 LIBUSB_LOG_LEVEL_INFO);
2022 #else
2023 libusb_set_debug (sfs_usb_ctx, 3);
2024 #endif
2025
2026 devcnt = libusb_get_device_list (sfs_usb_ctx, &devlist);
2027 if (devcnt < 0)
2028 {
2029 printf ("# Could not get device list, error %d\n", ret);
2030
2031 goto deinit_libusb_1_0;
2032 }
2033
2034 for (i = 0; i < devcnt; i++)
2035 {
2036 check_libusb_device (devlist[i], SANE_FALSE);
2037 }
2038
2039 libusb_free_device_list (devlist, 1);
2040
2041 deinit_libusb_1_0:
2042 libusb_exit (sfs_usb_ctx);
2043
2044 failed_libusb_1_0:
2045 ; /* init failed, jumping here */
2046 }
2047 }
2048 #else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */
2049 if (verbose > 1)
2050 printf ("libusb not available\n");
2051 #endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */
2052
2053 if (device_found)
2054 {
2055 if (libusb_device_found)
2056 {
2057 if (verbose > 0)
2058 printf
2059 (" # Your USB scanner was (probably) detected. It "
2060 "may or may not be supported by\n # SANE. Try scanimage "
2061 "-L and read the backend's manpage.\n");
2062 }
2063 else if (verbose > 0)
2064 printf
2065 (" # Your USB scanner was detected. It may or may not "
2066 "be supported by\n # SANE. Try scanimage -L and read the "
2067 "backend's manpage.\n");
2068 if (unknown_found && verbose > 0)
2069 printf
2070 (" # `UNKNOWN vendor and product' means that there seems to be a "
2071 "scanner at this\n # device file but the vendor and product ids "
2072 "couldn't be identified.\n # Currently identification only works "
2073 "with Linux versions >= 2.4.8. You may\n # need to configure your "
2074 "backend manually, see the backend's manpage.\n");
2075 }
2076 else
2077 {
2078 if (verbose > 0)
2079 printf
2080 (" # No USB scanners found. If you expected something different, "
2081 "make sure that\n # you have loaded a kernel driver for your USB host "
2082 "controller and have setup\n # the USB system correctly. "
2083 "See man sane-usb for details.\n");
2084 #if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB)
2085 if (verbose > 0)
2086 printf (" # SANE has been built without libusb support. This may be a "
2087 "reason\n # for not detecting USB scanners. Read README for "
2088 "more details.\n");
2089 #endif
2090 }
2091 if (enable_pp_checks == SANE_TRUE)
2092 {
2093 if (!check_mustek_pp_device() && verbose > 0)
2094 printf ("\n # No Mustek parallel port scanners found. If you expected"
2095 " something\n # different, make sure the scanner is correctly"
2096 " connected to your computer\n # and you have appropriate"
2097 " access rights.\n");
2098 }
2099 else if (verbose > 0)
2100 printf ("\n # Not checking for parallel port scanners.\n");
2101 if (verbose > 0)
2102 printf ("\n # Most Scanners connected to the parallel port or other "
2103 "proprietary ports\n # can't be detected by this program.\n");
2104 #ifdef HAVE_GETUID
2105 if (getuid ())
2106 if (verbose > 0)
2107 printf
2108 ("\n # You may want to run this program as root to find all devices. "
2109 "Once you\n # found the scanner devices, be sure to adjust access "
2110 "permissions as\n # necessary.\n");
2111 #endif
2112
2113 if (verbose > 1)
2114 printf ("done\n");
2115
2116 return 0;
2117 }
2118