1 /* SANE - Scanner Access Now Easy.
2 * For limitations, see function sanei_usb_get_vendor_product().
3
4 Copyright (C) 2011-2020 Rolf Bensch <rolf at bensch hyphen online dot de>
5 Copyright (C) 2006-2007 Wittawat Yamwong <wittawat@web.de>
6
7 This file is part of the SANE package.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22 As a special exception, the authors of SANE give permission for
23 additional uses of the libraries contained in this release of SANE.
24
25 The exception is that, if you link a SANE library with other files
26 to produce an executable, this does not by itself cause the
27 resulting executable to be covered by the GNU General Public
28 License. Your use of that executable is in no way restricted on
29 account of linking the SANE library code into it.
30
31 This exception does not, however, invalidate any other reasons why
32 the executable file might be covered by the GNU General Public
33 License.
34
35 If you submit changes to SANE to the maintainers to be included in
36 a subsequent release, you agree by submitting the changes that
37 those changes may be distributed with this exception intact.
38
39 If you write modifications of your own for SANE, it is your choice
40 whether to permit this exception to apply to your modifications.
41 If you do not wish that, delete this exception notice.
42 */
43 #include "../include/sane/config.h"
44
45 #include <string.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <limits.h> /* INT_MAX */
49
50 #include "pixma_rename.h"
51 #include "pixma_common.h"
52 #include "pixma_io.h"
53 #include "pixma_bjnp.h"
54
55 #include "../include/sane/sanei_usb.h"
56 #include "../include/sane/sane.h"
57
58
59 #ifdef __GNUC__
60 # define UNUSED(v) (void) v
61 #else
62 # define UNUSED(v)
63 #endif
64
65
66 struct pixma_io_t
67 {
68 pixma_io_t *next;
69 int interface;
70 SANE_Int dev;
71 };
72
73 typedef struct scanner_info_t
74 {
75 struct scanner_info_t *next;
76 char *devname;
77 int interface;
78 const pixma_config_t *cfg;
79 char serial[PIXMA_MAX_ID_LEN + 1]; /* "xxxxyyyy_zzzzzzz..."
80 x = vid, y = pid, z = serial */
81 } scanner_info_t;
82
83 #define INT_USB 0
84 #define INT_BJNP 1
85
86 static scanner_info_t *first_scanner = NULL;
87 static pixma_io_t *first_io = NULL;
88 static unsigned nscanners;
89
90
91 static scanner_info_t *
get_scanner_info(unsigned devnr)92 get_scanner_info (unsigned devnr)
93 {
94 scanner_info_t *si;
95 for (si = first_scanner; si && devnr != 0; --devnr, si = si->next)
96 {
97 }
98 return si;
99 }
100
101 static SANE_Status
attach(SANE_String_Const devname)102 attach (SANE_String_Const devname)
103 {
104 scanner_info_t *si;
105
106 si = (scanner_info_t *) calloc (1, sizeof (*si));
107 if (!si)
108 return SANE_STATUS_NO_MEM;
109 si->devname = strdup (devname);
110 if (!si->devname)
111 return SANE_STATUS_NO_MEM;
112 si -> interface = INT_USB;
113 si->next = first_scanner;
114 first_scanner = si;
115 nscanners++;
116 return SANE_STATUS_GOOD;
117 }
118
119
120 static SANE_Status
attach_bjnp(SANE_String_Const devname,SANE_String_Const serial,const struct pixma_config_t * cfg)121 attach_bjnp (SANE_String_Const devname,
122 SANE_String_Const serial,
123 const struct pixma_config_t *cfg)
124 {
125 scanner_info_t *si;
126
127 si = (scanner_info_t *) calloc (1, sizeof (*si));
128 if (!si)
129 return SANE_STATUS_NO_MEM;
130 si->devname = strdup (devname);
131 if (!si->devname)
132 return SANE_STATUS_NO_MEM;
133
134 si->cfg = cfg;
135 sprintf(si->serial, "%s_%s", cfg->model, serial);
136 si -> interface = INT_BJNP;
137 si->next = first_scanner;
138 first_scanner = si;
139 nscanners++;
140 return SANE_STATUS_GOOD;
141 }
142
143 static void
clear_scanner_list(void)144 clear_scanner_list (void)
145 {
146 scanner_info_t *si = first_scanner;
147 while (si)
148 {
149 scanner_info_t *temp = si;
150 free (si->devname);
151 si = si->next;
152 free (temp);
153 }
154 nscanners = 0;
155 first_scanner = NULL;
156 }
157
158 static SANE_Status
get_descriptor(SANE_Int dn,SANE_Int type,SANE_Int descidx,SANE_Int index,SANE_Int length,SANE_Byte * data)159 get_descriptor (SANE_Int dn, SANE_Int type, SANE_Int descidx,
160 SANE_Int index, SANE_Int length, SANE_Byte * data)
161 {
162 return sanei_usb_control_msg (dn, 0x80, USB_REQ_GET_DESCRIPTOR,
163 ((type & 0xff) << 8) | (descidx & 0xff),
164 index, length, data);
165 }
166
167 static SANE_Status
get_string_descriptor(SANE_Int dn,SANE_Int index,SANE_Int lang,SANE_Int length,SANE_Byte * data)168 get_string_descriptor (SANE_Int dn, SANE_Int index, SANE_Int lang,
169 SANE_Int length, SANE_Byte * data)
170 {
171 return get_descriptor (dn, USB_DT_STRING, index, lang, length, data);
172 }
173
174 static void
u16tohex(uint16_t x,char * str)175 u16tohex (uint16_t x, char *str)
176 {
177 static const char hdigit[16] =
178 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
179 'E', 'F'
180 };
181 str[0] = hdigit[(x >> 12) & 0xf];
182 str[1] = hdigit[(x >> 8) & 0xf];
183 str[2] = hdigit[(x >> 4) & 0xf];
184 str[3] = hdigit[x & 0xf];
185 str[4] = '\0';
186 }
187
188 static void
read_serial_number(scanner_info_t * si)189 read_serial_number (scanner_info_t * si)
190 {
191 uint8_t unicode[2 * (PIXMA_MAX_ID_LEN - 9) + 2];
192 uint8_t ddesc[18];
193 int iSerialNumber;
194 SANE_Int usb;
195 char *serial = si->serial;
196
197 u16tohex (si->cfg->vid, serial);
198 u16tohex (si->cfg->pid, serial + 4);
199
200 if (SANE_STATUS_GOOD != sanei_usb_open (si->devname, &usb))
201 return;
202 if (get_descriptor (usb, USB_DT_DEVICE, 0, 0, 18, ddesc)
203 != SANE_STATUS_GOOD)
204 goto done;
205 iSerialNumber = ddesc[16];
206 if (iSerialNumber != 0)
207 {
208 int i, len;
209 SANE_Status status;
210
211 /*int iSerialNumber = ddesc[16];*/
212 /* Read the first language code. Assumed that there is at least one. */
213 if (get_string_descriptor (usb, 0, 0, 4, unicode) != SANE_STATUS_GOOD)
214 goto done;
215 /* Read the serial number string. */
216 status = get_string_descriptor (usb, iSerialNumber,
217 unicode[3] * 256 + unicode[2],
218 sizeof (unicode), unicode);
219 if (status != SANE_STATUS_GOOD)
220 goto done;
221 /* Assumed charset: Latin1 */
222 len = unicode[0];
223 if (len > (int) sizeof (unicode))
224 {
225 len = sizeof (unicode);
226 PDBG (pixma_dbg (1, "WARNING:Truncated serial number\n"));
227 }
228 serial[8] = '_';
229 for (i = 2; i < len; i += 2)
230 {
231 serial[9 + i / 2 - 1] = unicode[i];
232 }
233 serial[9 + i / 2 - 1] = '\0';
234 }
235 else
236 {
237 PDBG (pixma_dbg (1, "WARNING:No serial number\n"));
238 }
239 done:
240 sanei_usb_close (usb);
241 }
242
243 static int
map_error(SANE_Status ss)244 map_error (SANE_Status ss)
245 {
246 switch (ss)
247 {
248 case SANE_STATUS_GOOD:
249 return 0;
250 case SANE_STATUS_UNSUPPORTED:
251 return PIXMA_ENODEV;
252 case SANE_STATUS_DEVICE_BUSY:
253 return PIXMA_EBUSY;
254 case SANE_STATUS_INVAL:
255 return PIXMA_EINVAL;
256 case SANE_STATUS_IO_ERROR:
257 return PIXMA_EIO;
258 case SANE_STATUS_NO_MEM:
259 return PIXMA_ENOMEM;
260 case SANE_STATUS_ACCESS_DENIED:
261 return PIXMA_EACCES;
262 case SANE_STATUS_CANCELLED:
263 return PIXMA_ECANCELED;
264 case SANE_STATUS_JAMMED:
265 return PIXMA_EPAPER_JAMMED;
266 case SANE_STATUS_COVER_OPEN:
267 return PIXMA_ECOVER_OPEN;
268 case SANE_STATUS_NO_DOCS:
269 return PIXMA_ENO_PAPER;
270 case SANE_STATUS_EOF:
271 return PIXMA_EOF;
272 #ifdef SANE_STATUS_HW_LOCKED
273 case SANE_STATUS_HW_LOCKED: /* unused by pixma */
274 #endif
275 #ifdef SANE_STATUS_WARMING_UP
276 case SANE_STATUS_WARMING_UP: /* unused by pixma */
277 #endif
278 break;
279 }
280 PDBG (pixma_dbg (1, "BUG:Unmapped SANE Status code %d\n", ss));
281 return PIXMA_EIO; /* should not happen */
282 }
283
284
285 int
pixma_io_init(void)286 pixma_io_init (void)
287 {
288 sanei_usb_init ();
289 sanei_bjnp_init();
290 nscanners = 0;
291 return 0;
292 }
293
294 void
pixma_io_cleanup(void)295 pixma_io_cleanup (void)
296 {
297 while (first_io)
298 pixma_disconnect (first_io);
299 clear_scanner_list ();
300 }
301
302 unsigned
pixma_collect_devices(const char ** conf_devices,const struct pixma_config_t * const pixma_devices[],SANE_Bool local_only)303 pixma_collect_devices (const char **conf_devices,
304 const struct pixma_config_t *const pixma_devices[], SANE_Bool local_only)
305 {
306 unsigned i, j;
307 struct scanner_info_t *si;
308 const struct pixma_config_t *cfg;
309
310 clear_scanner_list ();
311 j = 0;
312 for (i = 0; pixma_devices[i]; i++)
313 {
314 for (cfg = pixma_devices[i]; cfg->name; cfg++)
315 {
316 sanei_usb_find_devices (cfg->vid, cfg->pid, attach);
317 si = first_scanner;
318 while (j < nscanners)
319 {
320 PDBG (pixma_dbg (3, "pixma_collect_devices() found %s at %s\n",
321 cfg->name, si->devname));
322 si->cfg = cfg;
323 read_serial_number (si);
324 si = si->next;
325 j++;
326 }
327 }
328 }
329 if (! local_only)
330 sanei_bjnp_find_devices(conf_devices, attach_bjnp, pixma_devices);
331
332 si = first_scanner;
333 while (j < nscanners)
334 {
335 PDBG (pixma_dbg (3, "pixma_collect_devices() found %s at %s\n",
336 si->cfg->name, si->devname));
337 si = si->next;
338 j++;
339
340 }
341 return nscanners;
342 }
343
344 const pixma_config_t *
pixma_get_device_config(unsigned devnr)345 pixma_get_device_config (unsigned devnr)
346 {
347 const scanner_info_t *si = get_scanner_info (devnr);
348 return (si) ? si->cfg : NULL;
349 }
350
351 const char *
pixma_get_device_id(unsigned devnr)352 pixma_get_device_id (unsigned devnr)
353 {
354 const scanner_info_t *si = get_scanner_info (devnr);
355 return (si) ? si->serial : NULL;
356 }
357
358 int
pixma_connect(unsigned devnr,pixma_io_t ** handle)359 pixma_connect (unsigned devnr, pixma_io_t ** handle)
360 {
361 pixma_io_t *io;
362 SANE_Int dev;
363 const scanner_info_t *si;
364 int error;
365
366 *handle = NULL;
367 si = get_scanner_info (devnr);
368 if (!si)
369 return PIXMA_EINVAL;
370 if (si-> interface == INT_BJNP)
371 error = map_error (sanei_bjnp_open (si->devname, &dev));
372 else
373 error = map_error (sanei_usb_open (si->devname, &dev));
374
375 if (error < 0)
376 return error;
377 io = (pixma_io_t *) calloc (1, sizeof (*io));
378 if (!io)
379 {
380 if (si -> interface == INT_BJNP)
381 sanei_bjnp_close (dev);
382 else
383 sanei_usb_close (dev);
384 return PIXMA_ENOMEM;
385 }
386 io->next = first_io;
387 first_io = io;
388 io->dev = dev;
389 io->interface = si->interface;
390 *handle = io;
391 return 0;
392 }
393
394
395 void
pixma_disconnect(pixma_io_t * io)396 pixma_disconnect (pixma_io_t * io)
397 {
398 pixma_io_t **p;
399
400 if (!io)
401 return;
402 for (p = &first_io; *p && *p != io; p = &((*p)->next))
403 {
404 }
405 PASSERT (*p);
406 if (!(*p))
407 return;
408 if (io-> interface == INT_BJNP)
409 sanei_bjnp_close (io->dev);
410 else
411 sanei_usb_close (io->dev);
412 *p = io->next;
413 free (io);
414 }
415
pixma_activate(pixma_io_t * io)416 int pixma_activate (pixma_io_t * io)
417 {
418 int error;
419 if (io->interface == INT_BJNP)
420 {
421 error = map_error(sanei_bjnp_activate (io->dev));
422 }
423 else
424 /* noop for USB interface */
425 error = 0;
426 return error;
427 }
428
pixma_deactivate(pixma_io_t * io)429 int pixma_deactivate (pixma_io_t * io)
430 {
431 int error;
432 if (io->interface == INT_BJNP)
433 {
434 error = map_error(sanei_bjnp_deactivate (io->dev));
435 }
436 else
437 /* noop for USB interface */
438 error = 0;
439 return error;
440
441 }
442
443 int
pixma_reset_device(pixma_io_t * io)444 pixma_reset_device (pixma_io_t * io)
445 {
446 UNUSED (io);
447 return PIXMA_ENOTSUP;
448 }
449
450 int
pixma_write(pixma_io_t * io,const void * cmd,unsigned len)451 pixma_write (pixma_io_t * io, const void *cmd, unsigned len)
452 {
453 size_t count = len;
454 int error;
455
456 if (io->interface == INT_BJNP)
457 {
458 sanei_bjnp_set_timeout (io->dev, PIXMA_BULKOUT_TIMEOUT);
459 error = map_error (sanei_bjnp_write_bulk (io->dev, cmd, &count));
460 }
461 else
462 {
463 #ifdef HAVE_SANEI_USB_SET_TIMEOUT
464 sanei_usb_set_timeout (PIXMA_BULKOUT_TIMEOUT);
465 #endif
466 error = map_error (sanei_usb_write_bulk (io->dev, cmd, &count));
467 }
468 if (error == PIXMA_EIO)
469 error = PIXMA_ETIMEDOUT; /* FIXME: SANE doesn't have ETIMEDOUT!! */
470 if (count != len)
471 {
472 PDBG (pixma_dbg (1, "WARNING:pixma_write(): count(%u) != len(%u)\n",
473 (unsigned) count, len));
474 error = PIXMA_EIO;
475 }
476 if (error >= 0)
477 error = count;
478 PDBG (pixma_dump (10, "OUT ", cmd, error, len, 128));
479 return error;
480 }
481
482 int
pixma_read(pixma_io_t * io,void * buf,unsigned size)483 pixma_read (pixma_io_t * io, void *buf, unsigned size)
484 {
485 size_t count = size;
486 int error;
487
488 if (io-> interface == INT_BJNP)
489 {
490 sanei_bjnp_set_timeout (io->dev, PIXMA_BULKIN_TIMEOUT);
491 error = map_error (sanei_bjnp_read_bulk (io->dev, buf, &count));
492 }
493 else
494 {
495 #ifdef HAVE_SANEI_USB_SET_TIMEOUT
496 sanei_usb_set_timeout (PIXMA_BULKIN_TIMEOUT);
497 #endif
498 error = map_error (sanei_usb_read_bulk (io->dev, buf, &count));
499 }
500
501 if (error == PIXMA_EIO)
502 error = PIXMA_ETIMEDOUT; /* FIXME: SANE doesn't have ETIMEDOUT!! */
503 if (error >= 0)
504 error = count;
505 PDBG (pixma_dump (10, "IN ", buf, error, -1, 128));
506 return error;
507 }
508
509 int
pixma_wait_interrupt(pixma_io_t * io,void * buf,unsigned size,int timeout)510 pixma_wait_interrupt (pixma_io_t * io, void *buf, unsigned size, int timeout)
511 {
512 size_t count = size;
513 int error;
514
515 /* FIXME: What is the meaning of "timeout" in sanei_usb? */
516 if (timeout < 0)
517 timeout = INT_MAX;
518 else if (timeout < 100)
519 timeout = 100;
520 if (io-> interface == INT_BJNP)
521 {
522 sanei_bjnp_set_timeout (io->dev, timeout);
523 error = map_error (sanei_bjnp_read_int (io->dev, buf, &count));
524 }
525 else
526 {
527 #ifdef HAVE_SANEI_USB_SET_TIMEOUT
528 sanei_usb_set_timeout (timeout);
529 #endif
530 error = map_error (sanei_usb_read_int (io->dev, buf, &count));
531 }
532 if (error == PIXMA_EIO ||
533 (io->interface == INT_BJNP && error == PIXMA_EOF)) /* EOF is a bjnp timeout error! */
534 error = PIXMA_ETIMEDOUT; /* FIXME: SANE doesn't have ETIMEDOUT!! */
535 if (error == 0)
536 error = count;
537 if (error != PIXMA_ETIMEDOUT)
538 PDBG (pixma_dump (10, "INTR", buf, error, -1, -1));
539 return error;
540 }
541
542 int
pixma_set_interrupt_mode(pixma_io_t * s,int background)543 pixma_set_interrupt_mode (pixma_io_t * s, int background)
544 {
545 UNUSED (s);
546 return (background) ? PIXMA_ENOTSUP : 0;
547 }
548