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 /* MAC OS X does not support timeouts in darwin/libusb interrupt reads
66  * This is a very basic turnaround for MAC OS X
67  * Button scan will not work with this wrapper */
68 #ifdef __APPLE__
69 # define sanei_usb_read_int sanei_usb_read_bulk
70 #endif
71 
72 
73 struct pixma_io_t
74 {
75   pixma_io_t *next;
76   int interface;
77   SANE_Int dev;
78 };
79 
80 typedef struct scanner_info_t
81 {
82   struct scanner_info_t *next;
83   char *devname;
84   int interface;
85   const pixma_config_t *cfg;
86   char serial[PIXMA_MAX_ID_LEN + 1];	/* "xxxxyyyy_zzzzzzz..."
87 					   x = vid, y = pid, z = serial */
88 } scanner_info_t;
89 
90 #define INT_USB 0
91 #define INT_BJNP 1
92 
93 static scanner_info_t *first_scanner = NULL;
94 static pixma_io_t *first_io = NULL;
95 static unsigned nscanners;
96 
97 
98 static scanner_info_t *
get_scanner_info(unsigned devnr)99 get_scanner_info (unsigned devnr)
100 {
101   scanner_info_t *si;
102   for (si = first_scanner; si && devnr != 0; --devnr, si = si->next)
103     {
104     }
105   return si;
106 }
107 
108 static SANE_Status
attach(SANE_String_Const devname)109 attach (SANE_String_Const devname)
110 {
111   scanner_info_t *si;
112 
113   si = (scanner_info_t *) calloc (1, sizeof (*si));
114   if (!si)
115     return SANE_STATUS_NO_MEM;
116   si->devname = strdup (devname);
117   if (!si->devname)
118     return SANE_STATUS_NO_MEM;
119   si -> interface = INT_USB;
120   si->next = first_scanner;
121   first_scanner = si;
122   nscanners++;
123   return SANE_STATUS_GOOD;
124 }
125 
126 
127 static SANE_Status
attach_bjnp(SANE_String_Const devname,SANE_String_Const serial,const struct pixma_config_t * cfg)128 attach_bjnp (SANE_String_Const devname,
129              SANE_String_Const serial,
130              const struct pixma_config_t *cfg)
131 {
132   scanner_info_t *si;
133 
134   si = (scanner_info_t *) calloc (1, sizeof (*si));
135   if (!si)
136     return SANE_STATUS_NO_MEM;
137   si->devname = strdup (devname);
138   if (!si->devname)
139     return SANE_STATUS_NO_MEM;
140 
141   si->cfg = cfg;
142   sprintf(si->serial, "%s_%s", cfg->model, serial);
143   si -> interface = INT_BJNP;
144   si->next = first_scanner;
145   first_scanner = si;
146   nscanners++;
147   return SANE_STATUS_GOOD;
148 }
149 
150 static void
clear_scanner_list(void)151 clear_scanner_list (void)
152 {
153   scanner_info_t *si = first_scanner;
154   while (si)
155     {
156       scanner_info_t *temp = si;
157       free (si->devname);
158       si = si->next;
159       free (temp);
160     }
161   nscanners = 0;
162   first_scanner = NULL;
163 }
164 
165 static SANE_Status
get_descriptor(SANE_Int dn,SANE_Int type,SANE_Int descidx,SANE_Int index,SANE_Int length,SANE_Byte * data)166 get_descriptor (SANE_Int dn, SANE_Int type, SANE_Int descidx,
167 		SANE_Int index, SANE_Int length, SANE_Byte * data)
168 {
169   return sanei_usb_control_msg (dn, 0x80, USB_REQ_GET_DESCRIPTOR,
170 				((type & 0xff) << 8) | (descidx & 0xff),
171 				index, length, data);
172 }
173 
174 static SANE_Status
get_string_descriptor(SANE_Int dn,SANE_Int index,SANE_Int lang,SANE_Int length,SANE_Byte * data)175 get_string_descriptor (SANE_Int dn, SANE_Int index, SANE_Int lang,
176 		       SANE_Int length, SANE_Byte * data)
177 {
178   return get_descriptor (dn, USB_DT_STRING, index, lang, length, data);
179 }
180 
181 static void
u16tohex(uint16_t x,char * str)182 u16tohex (uint16_t x, char *str)
183 {
184   static const char hdigit[16] =
185     { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
186     'E', 'F'
187     };
188   str[0] = hdigit[(x >> 12) & 0xf];
189   str[1] = hdigit[(x >> 8) & 0xf];
190   str[2] = hdigit[(x >> 4) & 0xf];
191   str[3] = hdigit[x & 0xf];
192   str[4] = '\0';
193 }
194 
195 static void
read_serial_number(scanner_info_t * si)196 read_serial_number (scanner_info_t * si)
197 {
198   uint8_t unicode[2 * (PIXMA_MAX_ID_LEN - 9) + 2];
199   uint8_t ddesc[18];
200   int iSerialNumber;
201   SANE_Int usb;
202   char *serial = si->serial;
203 
204   u16tohex (si->cfg->vid, serial);
205   u16tohex (si->cfg->pid, serial + 4);
206 
207   if (SANE_STATUS_GOOD != sanei_usb_open (si->devname, &usb))
208     return;
209   if (get_descriptor (usb, USB_DT_DEVICE, 0, 0, 18, ddesc)
210       != SANE_STATUS_GOOD)
211     goto done;
212   iSerialNumber = ddesc[16];
213   if (iSerialNumber != 0)
214     {
215       int i, len;
216       SANE_Status status;
217 
218       /*int iSerialNumber = ddesc[16];*/
219       /* Read the first language code. Assumed that there is at least one. */
220       if (get_string_descriptor (usb, 0, 0, 4, unicode) != SANE_STATUS_GOOD)
221         goto done;
222       /* Read the serial number string. */
223       status = get_string_descriptor (usb, iSerialNumber,
224                                       unicode[3] * 256 + unicode[2],
225                                       sizeof (unicode), unicode);
226       if (status != SANE_STATUS_GOOD)
227         goto done;
228       /* Assumed charset: Latin1 */
229       len = unicode[0];
230       if (len > (int) sizeof (unicode))
231         {
232           len = sizeof (unicode);
233           PDBG (pixma_dbg (1, "WARNING:Truncated serial number\n"));
234         }
235             serial[8] = '_';
236             for (i = 2; i < len; i += 2)
237         {
238           serial[9 + i / 2 - 1] = unicode[i];
239         }
240       serial[9 + i / 2 - 1] = '\0';
241     }
242   else
243     {
244       PDBG (pixma_dbg (1, "WARNING:No serial number\n"));
245     }
246 done:
247   sanei_usb_close (usb);
248 }
249 
250 static int
map_error(SANE_Status ss)251 map_error (SANE_Status ss)
252 {
253   switch (ss)
254     {
255     case SANE_STATUS_GOOD:
256       return 0;
257     case SANE_STATUS_UNSUPPORTED:
258       return PIXMA_ENODEV;
259     case SANE_STATUS_DEVICE_BUSY:
260       return PIXMA_EBUSY;
261     case SANE_STATUS_INVAL:
262       return PIXMA_EINVAL;
263     case SANE_STATUS_IO_ERROR:
264       return PIXMA_EIO;
265     case SANE_STATUS_NO_MEM:
266       return PIXMA_ENOMEM;
267     case SANE_STATUS_ACCESS_DENIED:
268       return PIXMA_EACCES;
269     case SANE_STATUS_CANCELLED:
270       return PIXMA_ECANCELED;
271     case SANE_STATUS_JAMMED:
272        return PIXMA_EPAPER_JAMMED;
273     case SANE_STATUS_COVER_OPEN:
274        return PIXMA_ECOVER_OPEN;
275     case SANE_STATUS_NO_DOCS:
276        return PIXMA_ENO_PAPER;
277     case SANE_STATUS_EOF:
278        return PIXMA_EOF;
279 #ifdef SANE_STATUS_HW_LOCKED
280     case SANE_STATUS_HW_LOCKED:       /* unused by pixma */
281 #endif
282 #ifdef SANE_STATUS_WARMING_UP
283     case SANE_STATUS_WARMING_UP:      /* unused by pixma */
284 #endif
285       break;
286     }
287   PDBG (pixma_dbg (1, "BUG:Unmapped SANE Status code %d\n", ss));
288   return PIXMA_EIO;		/* should not happen */
289 }
290 
291 
292 int
pixma_io_init(void)293 pixma_io_init (void)
294 {
295   sanei_usb_init ();
296   sanei_bjnp_init();
297   nscanners = 0;
298   return 0;
299 }
300 
301 void
pixma_io_cleanup(void)302 pixma_io_cleanup (void)
303 {
304   while (first_io)
305     pixma_disconnect (first_io);
306   clear_scanner_list ();
307 }
308 
309 unsigned
pixma_collect_devices(const char ** conf_devices,const struct pixma_config_t * const pixma_devices[],SANE_Bool local_only)310 pixma_collect_devices (const char **conf_devices,
311                        const struct pixma_config_t *const pixma_devices[], SANE_Bool local_only)
312 {
313   unsigned i, j;
314   struct scanner_info_t *si;
315   const struct pixma_config_t *cfg;
316 
317   clear_scanner_list ();
318   j = 0;
319   for (i = 0; pixma_devices[i]; i++)
320     {
321       for (cfg = pixma_devices[i]; cfg->name; cfg++)
322         {
323           sanei_usb_find_devices (cfg->vid, cfg->pid, attach);
324           si = first_scanner;
325           while (j < nscanners)
326             {
327               PDBG (pixma_dbg (3, "pixma_collect_devices() found %s at %s\n",
328                    cfg->name, si->devname));
329               si->cfg = cfg;
330               read_serial_number (si);
331               si = si->next;
332               j++;
333             }
334         }
335     }
336   if (! local_only)
337     sanei_bjnp_find_devices(conf_devices, attach_bjnp, pixma_devices);
338 
339   si = first_scanner;
340   while (j < nscanners)
341     {
342       PDBG (pixma_dbg (3, "pixma_collect_devices() found %s at %s\n",
343                si->cfg->name, si->devname));
344       si = si->next;
345       j++;
346 
347     }
348   return nscanners;
349 }
350 
351 const pixma_config_t *
pixma_get_device_config(unsigned devnr)352 pixma_get_device_config (unsigned devnr)
353 {
354   const scanner_info_t *si = get_scanner_info (devnr);
355   return (si) ? si->cfg : NULL;
356 }
357 
358 const char *
pixma_get_device_id(unsigned devnr)359 pixma_get_device_id (unsigned devnr)
360 {
361   const scanner_info_t *si = get_scanner_info (devnr);
362   return (si) ? si->serial : NULL;
363 }
364 
365 int
pixma_connect(unsigned devnr,pixma_io_t ** handle)366 pixma_connect (unsigned devnr, pixma_io_t ** handle)
367 {
368   pixma_io_t *io;
369   SANE_Int dev;
370   const scanner_info_t *si;
371   int error;
372 
373   *handle = NULL;
374   si = get_scanner_info (devnr);
375   if (!si)
376     return PIXMA_EINVAL;
377   if (si-> interface == INT_BJNP)
378     error = map_error (sanei_bjnp_open (si->devname, &dev));
379   else
380     error = map_error (sanei_usb_open (si->devname, &dev));
381 
382   if (error < 0)
383     return error;
384   io = (pixma_io_t *) calloc (1, sizeof (*io));
385   if (!io)
386     {
387       if (si -> interface == INT_BJNP)
388         sanei_bjnp_close (dev);
389       else
390         sanei_usb_close (dev);
391       return PIXMA_ENOMEM;
392     }
393   io->next = first_io;
394   first_io = io;
395   io->dev = dev;
396   io->interface = si->interface;
397   *handle = io;
398   return 0;
399 }
400 
401 
402 void
pixma_disconnect(pixma_io_t * io)403 pixma_disconnect (pixma_io_t * io)
404 {
405   pixma_io_t **p;
406 
407   if (!io)
408     return;
409   for (p = &first_io; *p && *p != io; p = &((*p)->next))
410     {
411     }
412   PASSERT (*p);
413   if (!(*p))
414     return;
415   if (io-> interface == INT_BJNP)
416     sanei_bjnp_close (io->dev);
417   else
418     sanei_usb_close (io->dev);
419   *p = io->next;
420   free (io);
421 }
422 
pixma_activate(pixma_io_t * io)423 int pixma_activate (pixma_io_t * io)
424 {
425   int error;
426   if (io->interface == INT_BJNP)
427     {
428       error = map_error(sanei_bjnp_activate (io->dev));
429     }
430   else
431     /* noop for USB interface */
432     error = 0;
433   return error;
434 }
435 
pixma_deactivate(pixma_io_t * io)436 int pixma_deactivate (pixma_io_t * io)
437 {
438   int error;
439   if (io->interface == INT_BJNP)
440     {
441       error = map_error(sanei_bjnp_deactivate (io->dev));
442     }
443   else
444     /* noop for USB interface */
445     error = 0;
446   return error;
447 
448 }
449 
450 int
pixma_reset_device(pixma_io_t * io)451 pixma_reset_device (pixma_io_t * io)
452 {
453   UNUSED (io);
454   return PIXMA_ENOTSUP;
455 }
456 
457 int
pixma_write(pixma_io_t * io,const void * cmd,unsigned len)458 pixma_write (pixma_io_t * io, const void *cmd, unsigned len)
459 {
460   size_t count = len;
461   int error;
462 
463   if (io->interface == INT_BJNP)
464     {
465     sanei_bjnp_set_timeout (io->dev, PIXMA_BULKOUT_TIMEOUT);
466     error = map_error (sanei_bjnp_write_bulk (io->dev, cmd, &count));
467     }
468   else
469     {
470 #ifdef HAVE_SANEI_USB_SET_TIMEOUT
471     sanei_usb_set_timeout (PIXMA_BULKOUT_TIMEOUT);
472 #endif
473     error = map_error (sanei_usb_write_bulk (io->dev, cmd, &count));
474     }
475   if (error == PIXMA_EIO)
476     error = PIXMA_ETIMEDOUT;	/* FIXME: SANE doesn't have ETIMEDOUT!! */
477   if (count != len)
478     {
479       PDBG (pixma_dbg (1, "WARNING:pixma_write(): count(%u) != len(%u)\n",
480 		       (unsigned) count, len));
481       error = PIXMA_EIO;
482     }
483   if (error >= 0)
484     error = count;
485   PDBG (pixma_dump (10, "OUT ", cmd, error, len, 128));
486   return error;
487 }
488 
489 int
pixma_read(pixma_io_t * io,void * buf,unsigned size)490 pixma_read (pixma_io_t * io, void *buf, unsigned size)
491 {
492   size_t count = size;
493   int error;
494 
495   if (io-> interface == INT_BJNP)
496     {
497     sanei_bjnp_set_timeout (io->dev, PIXMA_BULKIN_TIMEOUT);
498     error = map_error (sanei_bjnp_read_bulk (io->dev, buf, &count));
499     }
500   else
501     {
502 #ifdef HAVE_SANEI_USB_SET_TIMEOUT
503       sanei_usb_set_timeout (PIXMA_BULKIN_TIMEOUT);
504 #endif
505       error = map_error (sanei_usb_read_bulk (io->dev, buf, &count));
506     }
507 
508   if (error == PIXMA_EIO)
509     error = PIXMA_ETIMEDOUT;	/* FIXME: SANE doesn't have ETIMEDOUT!! */
510   if (error >= 0)
511     error = count;
512   PDBG (pixma_dump (10, "IN  ", buf, error, -1, 128));
513   return error;
514 }
515 
516 int
pixma_wait_interrupt(pixma_io_t * io,void * buf,unsigned size,int timeout)517 pixma_wait_interrupt (pixma_io_t * io, void *buf, unsigned size, int timeout)
518 {
519   size_t count = size;
520   int error;
521 
522   /* FIXME: What is the meaning of "timeout" in sanei_usb? */
523   if (timeout < 0)
524     timeout = INT_MAX;
525   else if (timeout < 100)
526     timeout = 100;
527   if (io-> interface == INT_BJNP)
528     {
529       sanei_bjnp_set_timeout (io->dev, timeout);
530       error = map_error (sanei_bjnp_read_int (io->dev, buf, &count));
531     }
532   else
533     {
534 #ifdef HAVE_SANEI_USB_SET_TIMEOUT
535       sanei_usb_set_timeout (timeout);
536 #endif
537       error = map_error (sanei_usb_read_int (io->dev, buf, &count));
538     }
539   if (error == PIXMA_EIO ||
540       (io->interface == INT_BJNP && error == PIXMA_EOF))     /* EOF is a bjnp timeout error! */
541     error = PIXMA_ETIMEDOUT;	/* FIXME: SANE doesn't have ETIMEDOUT!! */
542   if (error == 0)
543     error = count;
544   if (error != PIXMA_ETIMEDOUT)
545     PDBG (pixma_dump (10, "INTR", buf, error, -1, -1));
546   return error;
547 }
548 
549 int
pixma_set_interrupt_mode(pixma_io_t * s,int background)550 pixma_set_interrupt_mode (pixma_io_t * s, int background)
551 {
552   UNUSED (s);
553   return (background) ? PIXMA_ENOTSUP : 0;
554 }
555