| 1520 | */ |
| 1521 | |
| 1522 | static int /* O - 0 on success, -1 on error */ |
| 1523 | open_device(usb_printer_t *printer, /* I - Printer */ |
| 1524 | int verbose) /* I - Update connecting-to-device state? */ |
| 1525 | { |
| 1526 | struct libusb_device_descriptor devdesc; |
| 1527 | /* Current device descriptor */ |
| 1528 | struct libusb_config_descriptor *confptr = NULL; |
| 1529 | /* Pointer to current configuration */ |
| 1530 | int number1 = -1, /* Configuration/interface/altset */ |
| 1531 | number2 = -1, /* numbers */ |
| 1532 | errcode = 0; |
| 1533 | unsigned char current; /* Current configuration */ |
| 1534 | |
| 1535 | |
| 1536 | /* |
| 1537 | * Return immediately if we are already connected... |
| 1538 | */ |
| 1539 | |
| 1540 | if (printer->handle) |
| 1541 | return (0); |
| 1542 | |
| 1543 | /* |
| 1544 | * Try opening the printer... |
| 1545 | */ |
| 1546 | |
| 1547 | if ((errcode = libusb_open(printer->device, &printer->handle)) < 0) |
| 1548 | { |
| 1549 | fprintf(stderr, "DEBUG: Failed to open device, code: %d\n", |
| 1550 | errcode); |
| 1551 | return (-1); |
| 1552 | } |
| 1553 | |
| 1554 | printer->usblp_attached = 0; |
| 1555 | printer->reset_after_job = 0; |
| 1556 | |
| 1557 | if (verbose) |
| 1558 | fputs("STATE: +connecting-to-device\n", stderr); |
| 1559 | |
| 1560 | if ((errcode = libusb_get_device_descriptor(printer->device, &devdesc)) < 0) |
| 1561 | { |
| 1562 | fprintf(stderr, "DEBUG: Failed to get device descriptor, code: %d\n", |
| 1563 | errcode); |
| 1564 | goto error; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * Get the "usblp" kernel module out of the way. This backend only |
| 1569 | * works without the module attached. |
| 1570 | */ |
| 1571 | |
| 1572 | errcode = libusb_kernel_driver_active(printer->handle, printer->iface); |
| 1573 | if (errcode == 0) |
| 1574 | printer->usblp_attached = 0; |
| 1575 | else if (errcode == 1) |
| 1576 | { |
| 1577 | printer->usblp_attached = 1; |
| 1578 | if ((errcode = |
| 1579 | libusb_detach_kernel_driver(printer->handle, printer->iface)) < 0) |