PageRenderTime 93ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/backend/avision.c

https://bitbucket.org/fhunleth/sane-backends-pl68xx
C | 8384 lines | 6077 code | 1415 blank | 892 comment | 1112 complexity | 5ce31aefcf994cdcea16ab127327f55a MD5 | raw file
Possible License(s): GPL-2.0, Unlicense
  1. /*******************************************************************************
  2. * SANE - Scanner Access Now Easy.
  3. avision.c
  4. This file is part of the SANE package.
  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. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16. MA 02111-1307, USA.
  17. As a special exception, the authors of SANE give permission for
  18. additional uses of the libraries contained in this release of SANE.
  19. The exception is that, if you link a SANE library with other files
  20. to produce an executable, this does not by itself cause the
  21. resulting executable to be covered by the GNU General Public
  22. License. Your use of that executable is in no way restricted on
  23. account of linking the SANE library code into it.
  24. This exception does not, however, invalidate any other reasons why
  25. the executable file might be covered by the GNU General Public
  26. License.
  27. If you submit changes to SANE to the maintainers to be included in
  28. a subsequent release, you agree by submitting the changes that
  29. those changes may be distributed with this exception intact.
  30. *****************************************************************************
  31. This backend is based upon the Tamarack backend and adapted to the Avision
  32. scanners by René Rebe and Meino Cramer.
  33. This file implements a SANE backend for the Avision SCSI Scanners (like the
  34. AV 630 / 620 (CS) ...) and some Avision (OEM) USB scanners (like the HP 53xx,
  35. 74xx, Minolta FS-V1 ...) or Fujitsu ScanPartner with the AVISION SCSI-2/3
  36. or USB command set.
  37. Copyright 1999, 2000, 2001 by
  38. "René Rebe" <rene@exactcode.de>
  39. "Meino Christian Cramer" <mccramer@s.netic.de>
  40. Copyright 2002 by
  41. "René Rebe" <rene@exactcode.de>
  42. "Jose Paulo Moitinho de Almeida" <moitinho@civil.ist.utl.pt>
  43. Copyright 2003, 2004, 2005, 2006, 2007 by
  44. "René Rebe" <rene@exactcode.de>
  45. Additional Contributers:
  46. "Gunter Wagner"
  47. (some fixes and the transparency option)
  48. "Martin Jelínek" <mates@sirrah.troja.mff.cuni.cz>
  49. nice attach debug output
  50. "Marcin Siennicki" <m.siennicki@cloos.pl>
  51. found some typos and contributed fixes for the HP 7400
  52. "Frank Zago" <fzago@greshamstorage.com>
  53. Mitsubishi IDs and report
  54. Avision INC
  55. example code to handle calibration and C5 ASIC specifics
  56. "Franz Bakan" <fbakan@gmx.net>
  57. OS/2 threading support
  58. "Falk Rohsiepe"
  59. Spelling and whitespace as well as HP5370 quirks
  60. Many additinoal special thanks to:
  61. Avision INC for providing protocol documentation.
  62. Avision INC for sponsoring an AV 8000S with ADF.
  63. Avision Europe and BHS Binkert for sponsoring several more scanners.
  64. Archivista GmbH, Switzerland, for sponsoring several features
  65. Roberto Di Cosmo who sponsored a HP 5370 scanner.
  66. Oliver Neukum who sponsored a HP 5300 USB scanner.
  67. Matthias Wiedemann for lending his HP 7450C for some weeks.
  68. Compusoft, C.A. Caracas / Venezuela for sponsoring a
  69. HP 7450 scanner and so enhanced ADF support.
  70. Chris Komatsu for the nice ADF scanning observartion.
  71. All the many other beta-tester and debug-log sender!
  72. Thanks to all the people and companies above. Without you
  73. the Avision backend would not be in the shape it is today! ;-)
  74. ********************************************************************************/
  75. /* SANE-FLOW-DIAGRAMM (from umax.c)
  76. *
  77. * - sane_init() : initialize backend, attach scanners(devicename,0)
  78. * . - sane_get_devices() : query list of scanner-devices
  79. * . - sane_open() : open a particular scanner-device and attach_scanner(devicename,&dev)
  80. * . . - sane_set_io_mode : set blocking-mode
  81. * . . - sane_get_select_fd : get scanner-fd
  82. * . . - sane_get_option_descriptor() : get option information
  83. * . . - sane_control_option() : change option values
  84. * . .
  85. * . . - sane_start() : start image aquisition
  86. * . . - sane_get_parameters() : returns actual scan-parameters
  87. * . . - sane_read() : read image-data (from pipe)
  88. *
  89. * in ADF mode this is done often:
  90. * . . - sane_start() : start image aquisition
  91. * . . - sane_get_parameters() : returns actual scan-parameters
  92. * . . - sane_read() : read image-data (from pipe)
  93. *
  94. * . . - sane_cancel() : cancel operation, kill reader_process
  95. *
  96. * . - sane_close() : close opened scanner-device, do_cancel, free buffer and handle
  97. * - sane_exit() : terminate use of backend, free devicename and device-struture
  98. */
  99. #include "../include/sane/config.h"
  100. #include <errno.h>
  101. #include <fcntl.h>
  102. #include <limits.h>
  103. #include <signal.h>
  104. #include <stdio.h>
  105. #include <stdlib.h>
  106. #include <string.h>
  107. #include <sys/types.h>
  108. #include <unistd.h>
  109. #include <sys/time.h>
  110. #include <math.h>
  111. #define BACKEND_NAME avision
  112. #define BACKEND_BUILD 290 /* avision backend BUILD version */
  113. #include <sane/sane.h>
  114. #include <sane/sanei.h>
  115. #include <sane/saneopts.h>
  116. #include <sane/sanei_thread.h>
  117. #include <sane/sanei_scsi.h>
  118. #include <sane/sanei_usb.h>
  119. #include <sane/sanei_config.h>
  120. #include <sane/sanei_backend.h>
  121. #include <avision.h>
  122. /* For timeval... */
  123. #ifdef DEBUG
  124. #include <sys/time.h>
  125. #endif
  126. /* Attention: The comments must stay as they are - they are automatially parsed
  127. to generate the SANE avision.desc file, as well as HTML online content! */
  128. /* Attention2: This device table is part of the source code and as such
  129. licensed under the terms of the license as listed above (GPL2+). By
  130. using this data you obviously create derived work! -ReneR */
  131. static Avision_HWEntry Avision_Device_List [] =
  132. {
  133. { "AVISION", "AV100CS",
  134. 0, 0,
  135. "Avision", "AV100CS",
  136. 0},
  137. /* status="untested" */
  138. { "AVISION", "AV100IIICS",
  139. 0, 0,
  140. "Avision", "AV100IIICS",
  141. 0},
  142. /* status="untested" */
  143. { "AVISION", "AV100S",
  144. 0, 0,
  145. "Avision", "AV100S",
  146. 0},
  147. /* status="untested" */
  148. { NULL, NULL,
  149. 0x0638, 0x0A27,
  150. "Avision", "AV120",
  151. AV_INT_STATUS},
  152. /* comment="sheetfed scanner" */
  153. /* status="complete" */
  154. { NULL, NULL,
  155. 0x0638, 0x0A3C,
  156. "Avision", "AV121",
  157. AV_INT_BUTTON | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA},
  158. /* comment="sheetfed scanner" */
  159. /* status="good" */
  160. { NULL, NULL,
  161. 0x0638, 0x0A33,
  162. "Avision", "AV122",
  163. AV_INT_BUTTON | AV_2ND_LINE_INTERLACED | AV_NO_REAR | AV_SOFT_SCALE | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA | AV_REAR_OFFSET},
  164. /* comment="sheetfed duplex scanner" */
  165. /* status="good" */
  166. { NULL, NULL,
  167. 0x0638, 0x0A93,
  168. "Avision", "AV122 C2",
  169. AV_INT_BUTTON | AV_2ND_LINE_INTERLACED | AV_NO_REAR | AV_SOFT_SCALE | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA | AV_REAR_OFFSET},
  170. /* comment="sheetfed duplex scanner" */
  171. /* status="good" */
  172. { NULL, NULL,
  173. 0x0638, 0x0A24,
  174. "Avision", "AV210",
  175. AV_INT_BUTTON | AV_ACCEL_TABLE},
  176. /* comment="sheetfed scanner" */
  177. /* status="complete" */
  178. { NULL, NULL,
  179. 0x0638, 0x0A25,
  180. "Avision", "AV210",
  181. AV_INT_BUTTON | AV_ACCEL_TABLE | AV_NO_64BYTE_ALIGN},
  182. /* comment="sheetfed scanner" */
  183. /* status="complete" */
  184. { NULL, NULL,
  185. 0x0638, 0x0A3A,
  186. "Avision", "AV210C2",
  187. AV_INT_BUTTON | AV_GRAY_MODES},
  188. /* comment="sheetfed scanner" */
  189. /* status="complete" */
  190. { NULL, NULL,
  191. 0x0638, 0x0A2F,
  192. "Avision", "AV210C2-G",
  193. AV_INT_BUTTON | AV_GRAY_MODES},
  194. /* comment="sheetfed scanner" */
  195. /* status="complete" */
  196. { NULL, NULL,
  197. 0x0638, 0x0A3A,
  198. "Avision", "AV210C2",
  199. AV_INT_BUTTON | AV_GRAY_MODES},
  200. /* comment="sheetfed scanner" */
  201. /* status="complete" */
  202. { NULL, NULL,
  203. 0x0638, 0x0A23,
  204. "Avision", "AV220",
  205. AV_INT_BUTTON},
  206. /* comment="duplex! sheetfed scanner" */
  207. /* status="complete" */
  208. { NULL, NULL,
  209. 0x0638, 0x0A2A,
  210. "Avision", "AV220C2",
  211. AV_INT_BUTTON | AV_CANCEL_BUTTON},
  212. /* comment="duplex! sheetfed scanner" */
  213. /* status="complete" */
  214. { NULL, NULL,
  215. 0x0638, 0x0A2B,
  216. "Avision", "AV220D2",
  217. AV_INT_BUTTON | AV_CANCEL_BUTTON},
  218. /* comment="duplex! sheetfed scanner" */
  219. /* status="complete" */
  220. { NULL, NULL,
  221. 0x0638, 0x0A2C,
  222. "Avision", "AV220+",
  223. AV_INT_BUTTON | AV_CANCEL_BUTTON},
  224. /* comment="duplex! sheetfed scanner" */
  225. /* status="complete" */
  226. { NULL, NULL,
  227. 0x0638, 0x0A2D,
  228. "Avision", "AV220C2-G",
  229. AV_INT_BUTTON | AV_CANCEL_BUTTON},
  230. /* comment="duplex! sheetfed scanner" */
  231. /* status="complete" */
  232. { NULL, NULL,
  233. 0x0638, 0x0A2E,
  234. "Avision", "AV220C2-B",
  235. AV_INT_BUTTON | AV_CANCEL_BUTTON},
  236. /* comment="duplex! sheetfed scanner" */
  237. /* status="complete" */
  238. { NULL, NULL,
  239. 0x0638, 0x0A94,
  240. "Avision", "AV220-G",
  241. AV_INT_BUTTON},
  242. /* comment="duplex! sheetfed scanner" */
  243. /* status="complete" */
  244. { "AVISION", "AV240SC",
  245. 0, 0,
  246. "Avision", "AV240SC",
  247. 0},
  248. /* status="untested" */
  249. { "AVISION", "AV260CS",
  250. 0, 0,
  251. "Avision", "AV260CS",
  252. 0},
  253. /* status="untested" */
  254. { "AVISION", "AV360CS",
  255. 0, 0,
  256. "Avision", "AV360CS",
  257. 0},
  258. /* status="untested" */
  259. { "AVISION", "AV363CS",
  260. 0, 0,
  261. "Avision", "AV363CS",
  262. 0},
  263. /* status="untested" */
  264. { "AVISION", "AV420CS",
  265. 0, 0,
  266. "Avision", "AV420CS",
  267. 0},
  268. /* status="untested" */
  269. { "AVISION", "AV6120",
  270. 0, 0,
  271. "Avision", "AV6120",
  272. 0},
  273. /* status="untested" */
  274. { NULL, "AV610",
  275. 0x0638, 0x0a18,
  276. "Avision", "AV610",
  277. AV_GRAY_CALIB_BLUE | AV_ACCEL_TABLE | AV_NO_64BYTE_ALIGN | AV_INT_STATUS | AV_INT_BUTTON},
  278. /* status="good" */
  279. { NULL, NULL,
  280. 0x0638, 0x0a18,
  281. "Avision", "AV600U Plus",
  282. AV_GRAY_CALIB_BLUE | AV_ACCEL_TABLE | AV_NO_64BYTE_ALIGN | AV_INT_STATUS | AV_INT_BUTTON},
  283. /* status="good" */
  284. { NULL, NULL,
  285. 0x0638, 0x0a5e,
  286. "Avision", "AV610C2",
  287. AV_NO_BACKGROUND | AV_INT_BUTTON}, /* cancel button -> sense abort! */
  288. /* status="good" */
  289. { NULL, NULL,
  290. 0x0638, 0x0a41,
  291. "Avision", "AM3000 Series",
  292. 0},
  293. /* comment="MFD" */
  294. /* status="basic" */
  295. { NULL, NULL,
  296. 0x0638, 0x0a16,
  297. "Avision", "DS610CU Scancopier",
  298. AV_INT_STATUS},
  299. /* comment="1 pass, 600 dpi, A4" */
  300. /* status="good" */
  301. { "AVISION", "AV620CS",
  302. 0, 0,
  303. "Avision", "AV620CS",
  304. 0},
  305. /* comment="1 pass, 600 dpi" */
  306. /* status="complete" */
  307. { "AVISION", "AV620CS Plus",
  308. 0, 0,
  309. "Avision", "AV620CS Plus",
  310. 0},
  311. /* comment="1 pass, 1200 dpi" */
  312. /* status="complete" */
  313. { "AVISION", "AV630CS",
  314. 0, 0,
  315. "Avision", "AV630CS",
  316. 0},
  317. /* comment="1 pass, 1200 dpi - regularly tested" */
  318. /* status="complete" */
  319. { "AVISION", "AV630CSL",
  320. 0, 0,
  321. "Avision", "AV630CSL",
  322. 0},
  323. /* comment="1 pass, 1200 dpi" */
  324. /* status="untested" */
  325. { "AVISION", "AV6240",
  326. 0, 0,
  327. "Avision", "AV6240",
  328. 0},
  329. /* comment="1 pass, ??? dpi" */
  330. /* status="complete" */
  331. { NULL, NULL,
  332. 0x0638, 0x0A13,
  333. "Avision", "AV600U",
  334. AV_MULTI_CALIB_CMD | AV_ADF_BGR_ORDER_INVERT | AV_SOFT_SCALE | AV_INT_STATUS | AV_NO_BUTTON},
  335. /* comment="1 pass, 600 dpi" */
  336. /* status="good" */
  337. { "AVISION", "AV660S",
  338. 0, 0,
  339. "Avision", "AV660S",
  340. 0},
  341. /* comment="1 pass, ??? dpi" */
  342. /* status="untested" */
  343. { "AVISION", "AV680S",
  344. 0, 0,
  345. "Avision", "AV680S",
  346. 0},
  347. /* comment="1 pass, ??? dpi" */
  348. /* status="untested" */
  349. { "AVISION", "AV690U",
  350. 0, 0,
  351. "Avision", "AV690U",
  352. 0},
  353. /* comment="1 pass, 2400 dpi" */
  354. /* status="untested" */
  355. { "AVISION", "AV800S",
  356. 0, 0,
  357. "Avision", "AV800S",
  358. 0},
  359. /* comment="1 pass, ??? dpi" */
  360. /* status="untested" */
  361. { "AVISION", "AV810C",
  362. 0, 0,
  363. "Avision", "AV810C",
  364. 0},
  365. /* comment="1 pass, ??? dpi" */
  366. /* status="untested" */
  367. { "AVISION", "AV820",
  368. 0, 0,
  369. "Avision", "AV820",
  370. 0},
  371. /* comment="1 pass, ??? dpi" */
  372. /* status="untested" */
  373. { "AVISION", "AV820C",
  374. 0, 0,
  375. "Avision", "AV820C",
  376. 0},
  377. /* comment="1 pass, ??? dpi" */
  378. /* status="complete" */
  379. { "AVISION", "AV820C Plus",
  380. 0, 0,
  381. "Avision", "AV820C Plus",
  382. 0},
  383. /* comment="1 pass, ??? dpi" */
  384. /* status="complete" */
  385. { "AVISION", "AV830C",
  386. 0, 0,
  387. "Avision", "AV830C",
  388. 0},
  389. /* comment="1 pass, ??? dpi" */
  390. /* status="complete" */
  391. { "AVISION", "AV830C Plus",
  392. 0, 0,
  393. "Avision", "AV830C Plus",
  394. 0},
  395. /* comment="1 pass, ??? dpi" */
  396. /* status="untested" */
  397. { "AVISION", "AV880",
  398. 0, 0,
  399. "Avision", "AV880",
  400. 0},
  401. /* comment="1 pass, ??? dpi" */
  402. /* status="untested" */
  403. { "AVISION", "AV880C",
  404. 0, 0,
  405. "Avision", "AV880C",
  406. 0},
  407. /* comment="1 pass, ??? dpi" */
  408. /* status="untested" */
  409. { "AVISION", "AV3200C",
  410. 0, 0,
  411. "Avision", "AV3200C",
  412. AV_NON_INTERLACED_DUPLEX_300 | AV_FASTER_WITH_FILTER},
  413. /* comment="1 pass, ??? dpi" */
  414. /* status="complete" */
  415. { "AVISION", "AV3200SU",
  416. 0x0638, 0x0A4E,
  417. "Avision", "AV3200SU",
  418. 0},
  419. /* comment="1 pass, ??? dpi" */
  420. /* status="complete" */
  421. { "AVISION", "AV3730SU",
  422. 0x0638, 0x0A4F,
  423. "Avision", "AV3730SU",
  424. 0},
  425. /* comment="1 pass, ??? dpi" */
  426. /* status="complete" */
  427. { "AVISION", "AV3750SU",
  428. 0x0638, 0x0A65,
  429. "Avision", "AV3750SU",
  430. 0},
  431. /* comment="1 pass, ??? dpi" */
  432. /* status="complete" */
  433. { "AVISION", "AV3800C",
  434. 0, 0,
  435. "Avision", "AV3800C",
  436. 0},
  437. /* comment="1 pass, ??? dpi" */
  438. /* status="complete" */
  439. { "AVISION", "AV3850SU",
  440. 0x0638, 0x0a66,
  441. "Avision", "AV3850SU",
  442. 0},
  443. /* comment="1 pass, ??? dpi" */
  444. /* status="complete" */
  445. { "AVISION", "FB6000E",
  446. 0, 0,
  447. "Avision", "FB6000E",
  448. AV_NON_INTERLACED_DUPLEX_300},
  449. /* comment="1 pass, 1200 dpi, A3 - duplex! - zero edge!" */
  450. /* status="complete" */
  451. { NULL, NULL,
  452. 0x0638, 0x0a82,
  453. "Avision", "FB6080E",
  454. AV_NON_INTERLACED_DUPLEX_300},
  455. /* comment="1 pass, 1200 dpi, A3 - duplex! - zero edge!" */
  456. /* status="complete" */
  457. { NULL, NULL,
  458. 0x0638, 0xa84,
  459. "Avision", "FB2080E",
  460. 0},
  461. /* comment="1 pass, 600 dpi, zero-edge" ASIC 7 */
  462. /* status="basic" */
  463. { "AVISION", "AV8000S",
  464. 0, 0,
  465. "Avision", "AV8000S",
  466. AV_DOES_NOT_KEEP_WINDOW},
  467. /* comment="1 pass, 1200 dpi, A3 - regularly tested" */
  468. /* status="complete" */
  469. { NULL, NULL,
  470. 0x0638, 0x0a4d,
  471. "Avision", "AV8050U",
  472. AV_NON_INTERLACED_DUPLEX_300 | AV_DOES_NOT_KEEP_GAMMA},
  473. /* comment="1 pass, 1200 dpi, A3 - duplex!" */
  474. /* status="complete" */
  475. { "AVISION", "AV8300",
  476. 0x0638, 0x0A40,
  477. "Avision", "AV8300",
  478. AV_NON_INTERLACED_DUPLEX_300 | AV_DOES_NOT_KEEP_GAMMA},
  479. /* comment="1 pass, 1200 dpi, A3 - duplex!" */
  480. /* status="complete" */
  481. { "AVISION", "AV8350",
  482. 0x0638, 0x0A68,
  483. "Avision", "AV8350",
  484. AV_NON_INTERLACED_DUPLEX_300 | AV_DOES_NOT_KEEP_GAMMA},
  485. /* comment="1 pass, 1200 dpi, A3 - duplex!" */
  486. /* status="complete" */
  487. { NULL, NULL,
  488. 0x0638, 0x0A61,
  489. "Avision", "IT8300",
  490. AV_NON_INTERLACED_DUPLEX_300 | AV_ACCEL_TABLE},
  491. /* comment="1 pass, 1200 dpi, A3 - duplex!, LCD screen, paper sensors" */
  492. /* status="good" */
  493. { NULL, NULL,
  494. 0x0638, 0x0AA1,
  495. "Avision", "@V2500",
  496. 0},
  497. /* comment="" */
  498. /* status="untested" */
  499. { NULL, NULL,
  500. 0x0638, 0x0A45,
  501. "Avision", "@V5100",
  502. 0},
  503. /* comment="" */
  504. /* status="good" */
  505. { "AVISION", "AVA3",
  506. 0, 0,
  507. "Avision", "AVA3",
  508. AV_FORCE_A3},
  509. /* comment="1 pass, 600 dpi, A3" */
  510. /* status="basic" */
  511. /* and possibly more avisions ;-) */
  512. { "HP", "ScanJet 5300C",
  513. 0x03f0, 0x0701,
  514. "Hewlett-Packard", "ScanJet 5300C",
  515. AV_INT_STATUS},
  516. /* comment="1 pass, 2400 dpi - regularly tested - some FW revisions have x-axis image scaling problems over 1200 dpi" */
  517. /* status="complete" */
  518. { "HP", "ScanJet 5370C",
  519. 0x03f0, 0x0701,
  520. "Hewlett-Packard", "ScanJet 5370C",
  521. AV_MULTI_CALIB_CMD | AV_INT_STATUS},
  522. /* comment="1 pass, 2400 dpi - some FW revisions have x-axis image scaling problems over 1200 dpi" */
  523. /* status="good" */
  524. { "hp", "scanjet 7400c",
  525. 0x03f0, 0x0801,
  526. "Hewlett-Packard", "ScanJet 7400c",
  527. AV_LIGHT_CHECK_BOGUS | AV_NO_64BYTE_ALIGN | AV_INT_STATUS},
  528. /* comment="1 pass, 2400 dpi - dual USB/SCSI interface" */
  529. /* status="good" */
  530. #ifdef FAKE_ENTRIES_FOR_DESC_GENERATION
  531. { "hp", "scanjet 7450c",
  532. 0x03f0, 0x0801,
  533. "Hewlett-Packard", "ScanJet 7450c",
  534. AV_NO_64BYTE_ALIGN | AV_INT_STATUS},
  535. /* comment="1 pass, 2400 dpi - dual USB/SCSI interface - regularly tested" */
  536. /* status="good" */
  537. { "hp", "scanjet 7490c",
  538. 0x03f0, 0x0801,
  539. "Hewlett-Packard", "ScanJet 7490c",
  540. AV_NO_64BYTE_ALIGN | AV_INT_STATUS},
  541. /* comment="1 pass, 1200 dpi - dual USB/SCSI interface" */
  542. /* status="good" */
  543. #endif
  544. { "HP", "C9930A",
  545. 0x03f0, 0x0b01,
  546. "Hewlett-Packard", "ScanJet 8200",
  547. 0},
  548. /* comment="1 pass, 4800 (?) dpi - USB 2.0" */
  549. /* status="good" */
  550. #ifdef FAKE_ENTRIES_FOR_DESC_GENERATION
  551. { "HP", "C9930A",
  552. 0x03f0, 0x0b01,
  553. "Hewlett-Packard", "ScanJet 8250",
  554. 0},
  555. /* comment="1 pass, 4800 (?) dpi - USB 2.0" */
  556. /* status="good" */
  557. #endif
  558. { "HP", "C9930A",
  559. 0x03f0, 0x3905,
  560. "Hewlett-Packard", "ScanJet 8270",
  561. 0},
  562. /* comment="1 pass, 4800 (?) dpi - USB 2.0" */
  563. /* status="good" */
  564. #ifdef FAKE_ENTRIES_FOR_DESC_GENERATION
  565. { "HP", "C9930A",
  566. 0x03f0, 0x0b01,
  567. "Hewlett-Packard", "ScanJet 8290",
  568. 0},
  569. /* comment="1 pass, 4800 (?) dpi - USB 2.0 and SCSI - only SCSI tested so far" */
  570. /* status="good" */
  571. #endif
  572. { "HP", "C9930A",
  573. 0x03f0, 0x3805,
  574. "Hewlett-Packard", "ScanJet 8390",
  575. 0},
  576. /* comment="1 pass, 4800 (?) dpi - USB 2.0" */
  577. /* status="good" */
  578. { "Minolta", "#2882",
  579. 0, 0,
  580. "Minolta", "Dimage Scan Dual I",
  581. AV_FORCE_FILM | AV_NO_START_SCAN}, /* not AV_FILMSCANNER (no frame control) */
  582. /* status="basic" */
  583. { "Minolta", "#2887",
  584. 0, 0,
  585. "Minolta", "Scan Multi Pro",
  586. AV_FORCE_FILM | AV_NO_START_SCAN}, /* AV_FILMSCANNER (rame control)? */
  587. /* status="untested" */
  588. { "MINOLTA", "FS-V1",
  589. 0x0638, 0x026a,
  590. "Minolta", "Dimage Scan Dual II",
  591. AV_FILMSCANNER | AV_ONE_CALIB_CMD | AV_12_BIT_MODE},
  592. /* comment="1 pass, film-scanner" */
  593. /* status="good" */
  594. { "MINOLTA", "Elite II",
  595. 0x0686, 0x4004,
  596. "Minolta", "Elite II",
  597. AV_FILMSCANNER | AV_ONE_CALIB_CMD},
  598. /* comment="1 pass, film-scanner" */
  599. /* status="untested" */
  600. { "MINOLTA", "FS-V3",
  601. 0x0686, 0x400d,
  602. "Minolta", "Dimage Scan Dual III",
  603. AV_FILMSCANNER | AV_ONE_CALIB_CMD | AV_ACCEL_TABLE },
  604. /* comment="1 pass, film-scanner" */
  605. /* status="good" */
  606. { "MINOLTA", "FS-V4",
  607. 0x0686, 0x400e,
  608. "Minolta", "Dimage Scan Elite 5400",
  609. AV_FILMSCANNER | AV_ONE_CALIB_CMD | /*AV_ACCEL_TABLE |*/ AV_NO_START_SCAN},
  610. /* comment="1 pass, film-scanner" */
  611. /* status="good" */
  612. { "QMS", "SC-110",
  613. 0x0638, 0x0a15,
  614. "Minolta-QMS", "SC-110",
  615. 0},
  616. /* comment="" */
  617. /* status="untested" */
  618. { "QMS", "SC-215",
  619. 0x0638, 0x0a16,
  620. "Minolta-QMS", "SC-215",
  621. 0},
  622. /* comment="" */
  623. /* status="good" */
  624. { "MITSBISH", "MCA-ADFC",
  625. 0, 0,
  626. "Mitsubishi", "MCA-ADFC",
  627. 0},
  628. /* status="untested" */
  629. { "MITSBISH", "MCA-S1200C",
  630. 0, 0,
  631. "Mitsubishi", "S1200C",
  632. 0},
  633. /* status="untested" */
  634. { "MITSBISH", "MCA-S600C",
  635. 0, 0,
  636. "Mitsubishi", "S600C",
  637. 0},
  638. /* status="untested" */
  639. { "MITSBISH", "SS600",
  640. 0, 0,
  641. "Mitsubishi", "SS600",
  642. 0},
  643. /* status="good" */
  644. /* The next are all untested ... */
  645. { "FCPA", "ScanPartner",
  646. 0, 0,
  647. "Fujitsu", "ScanPartner",
  648. AV_FUJITSU},
  649. /* status="untested" */
  650. { "FCPA", "ScanPartner 10",
  651. 0, 0,
  652. "Fujitsu", "ScanPartner 10",
  653. AV_FUJITSU},
  654. /* status="untested" */
  655. { "FCPA", "ScanPartner 10C",
  656. 0, 0,
  657. "Fujitsu", "ScanPartner 10C",
  658. AV_FUJITSU},
  659. /* status="untested" */
  660. { "FCPA", "ScanPartner 15C",
  661. 0, 0,
  662. "Fujitsu", "ScanPartner 15C",
  663. AV_FUJITSU},
  664. /* status="untested" */
  665. { "FCPA", "ScanPartner 300C",
  666. 0, 0,
  667. "Fujitsu", "ScanPartner 300C",
  668. 0},
  669. /* status="untested" */
  670. { "FCPA", "ScanPartner 600C",
  671. 0, 0,
  672. "Fujitsu", "ScanPartner 600C",
  673. 0},
  674. /* status="untested" */
  675. { "FCPA", "ScanPartner 620C",
  676. 0, 0,
  677. "Fujitsu", "ScanPartner 620C",
  678. 0},
  679. /* status="good" */
  680. { "FCPA", "ScanPartner Jr",
  681. 0, 0,
  682. "Fujitsu", "ScanPartner Jr",
  683. 0},
  684. /* status="untested" */
  685. { "FCPA", "ScanStation",
  686. 0, 0,
  687. "Fujitsu", "ScanStation",
  688. 0},
  689. /* status="untested" */
  690. { NULL, NULL,
  691. 0x04c5, 0x1029,
  692. "Fujitsu", "fi-4010CU",
  693. 0},
  694. /* status="untested" */
  695. { NULL, NULL,
  696. 0x040a, 0x6001,
  697. "Kodak", "i30",
  698. AV_INT_BUTTON},
  699. /* status="untested" */
  700. { NULL, NULL,
  701. 0x040a, 0x6002,
  702. "Kodak", "i40",
  703. AV_INT_BUTTON},
  704. /* status="basic" */
  705. { NULL, NULL,
  706. 0x040a, 0x6003,
  707. "Kodak", "i50",
  708. AV_INT_BUTTON},
  709. /* status="untested" */
  710. { NULL, NULL,
  711. 0x040a, 0x6003,
  712. "Kodak", "i55",
  713. AV_INT_BUTTON},
  714. /* status="untested" */
  715. { NULL, NULL,
  716. 0x040a, 0x6004,
  717. "Kodak", "i60",
  718. AV_INT_BUTTON},
  719. /* status="untested" */
  720. { NULL, NULL,
  721. 0x040a, 0x6004,
  722. "Kodak", "i65",
  723. AV_INT_BUTTON},
  724. /* status="untested" */
  725. { NULL, NULL,
  726. 0x040a, 0x6005,
  727. "Kodak", "i80",
  728. AV_INT_BUTTON},
  729. /* status="good" */
  730. { "iVina", "1200U",
  731. 0x0638, 0x0268,
  732. "iVina", "1200U",
  733. 0},
  734. /* status="untested" */
  735. { NULL, NULL,
  736. 0x04a7, 0x0424,
  737. "Visioneer", "Strobe XP 450",
  738. AV_INT_BUTTON | AV_ACCEL_TABLE},
  739. /* comment="sheetfed scanner" */
  740. /* status="complete" */
  741. { NULL, NULL,
  742. 0x04a7, 0x0491,
  743. "Visioneer", "Strobe XP 450-G",
  744. AV_INT_BUTTON | AV_ACCEL_TABLE},
  745. /* comment="sheetfed scanner" */
  746. /* status="complete" */
  747. { NULL, NULL,
  748. 0x04a7, 0x0479,
  749. "Visioneer", "Strobe XP 470",
  750. AV_INT_BUTTON | AV_ACCEL_TABLE},
  751. /* comment="sheetfed scanner" */
  752. /* status="complete" */
  753. { NULL, NULL,
  754. 0x04a7, 0x048F,
  755. "Visioneer", "Strobe XP 470-G",
  756. AV_INT_BUTTON | AV_ACCEL_TABLE},
  757. /* comment="sheetfed scanner" */
  758. /* status="complete" */
  759. { NULL, NULL,
  760. 0x04a7, 0x0420,
  761. "Visioneer", "9320",
  762. 0},
  763. /* comment="sheetfed scanner" */
  764. /* status="complete" */
  765. { NULL, NULL,
  766. 0x04a7, 0x0421,
  767. "Visioneer", "9450",
  768. 0},
  769. /* comment="sheetfed scanner" */
  770. /* status="complete" */
  771. { NULL, NULL,
  772. 0x04a7, 0x047A,
  773. "Visioneer", "9450-G",
  774. 0},
  775. /* comment="sheetfed scanner" */
  776. /* status="complete" */
  777. { NULL, NULL,
  778. 0x04a7, 0x0422,
  779. "Visioneer", "9550",
  780. 0},
  781. /* comment="sheetfed scanner" */
  782. /* status="complete" */
  783. { NULL, NULL,
  784. 0x04a7, 0x0390,
  785. "Visioneer", "9650",
  786. 0},
  787. /* comment="sheetfed scanner" */
  788. /* status="complete" */
  789. { NULL, NULL,
  790. 0x04a7, 0x047B,
  791. "Visioneer", "9650-G",
  792. 0},
  793. /* comment="sheetfed scanner" */
  794. /* status="complete" */
  795. { NULL, NULL,
  796. 0x04a7, 0x0423,
  797. "Visioneer", "9750",
  798. AV_INT_BUTTON},
  799. /* comment="sheetfed scanner" */
  800. /* status="complete" */
  801. { NULL, NULL,
  802. 0x04a7, 0x0493,
  803. "Visioneer", "9750-G",
  804. AV_INT_BUTTON},
  805. /* comment="sheetfed scanner" */
  806. /* status="complete" */
  807. { NULL, NULL,
  808. 0x04a7, 0x0497,
  809. "Visioneer", "Patriot 430",
  810. AV_INT_BUTTON | AV_2ND_LINE_INTERLACED | AV_NO_REAR | AV_SOFT_SCALE | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA | AV_REAR_OFFSET},
  811. /* comment="sheetfed scanner" */
  812. /* status="complete" */
  813. { NULL, NULL,
  814. 0x04a7, 0x048F,
  815. "Visioneer", "Patriot 470",
  816. AV_INT_BUTTON},
  817. /* comment="sheetfed scanner" */
  818. /* status="complete" */
  819. { NULL, NULL,
  820. 0x04a7, 0x0498,
  821. "Visioneer", "Patriot 680",
  822. AV_INT_BUTTON},
  823. /* comment="sheetfed scanner" */
  824. /* status="complete" */
  825. { NULL, NULL,
  826. 0x04a7, 0x0499,
  827. "Visioneer", "Patriot 780",
  828. AV_INT_BUTTON},
  829. /* comment="sheetfed scanner" */
  830. /* status="complete" */
  831. { NULL, NULL,
  832. 0x04a7, 0x049C,
  833. "Xerox", "DocuMate150",
  834. AV_INT_BUTTON | AV_SOFT_SCALE | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA | AV_BACKGROUND_QUIRK},
  835. /* status="good" */
  836. { NULL, NULL,
  837. 0x04a7, 0x0477,
  838. "Xerox", "DocuMate152",
  839. AV_INT_BUTTON | AV_2ND_LINE_INTERLACED | AV_NO_REAR | AV_SOFT_SCALE | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA | AV_REAR_OFFSET | AV_BACKGROUND_QUIRK},
  840. /* status="good" */
  841. { NULL, NULL,
  842. 0x04a7, 0x049D,
  843. "Xerox", "DocuMate162",
  844. AV_INT_BUTTON | AV_2ND_LINE_INTERLACED | AV_NO_REAR | AV_SOFT_SCALE | AV_DOES_KEEP_WINDOW | AV_DOES_KEEP_GAMMA | AV_REAR_OFFSET | AV_BACKGROUND_QUIRK},
  845. /* status="good" */
  846. { NULL, NULL,
  847. 0x04a7, 0x0448,
  848. "Xerox", "DocuMate250",
  849. AV_INT_BUTTON},
  850. /* status="good" */
  851. { NULL, NULL,
  852. 0x04a7, 0x0490,
  853. "Xerox", "DocuMate250-G",
  854. AV_INT_BUTTON},
  855. /* status="good" */
  856. { NULL, NULL,
  857. 0x04a7, 0x0449,
  858. "Xerox", "DocuMate252",
  859. AV_INT_BUTTON},
  860. /* status="good" */
  861. { NULL, NULL,
  862. 0x04a7, 0x048C,
  863. "Xerox", "DocuMate252-G",
  864. AV_INT_BUTTON},
  865. /* status="good" */
  866. { NULL, NULL,
  867. 0x04a7, 0x044c,
  868. "Xerox", "DocuMate262",
  869. AV_INT_BUTTON},
  870. /* status="good" */
  871. { NULL, NULL,
  872. 0x04a7, 0x048D,
  873. "Xerox", "DocuMate262-G",
  874. AV_INT_BUTTON},
  875. /* status="good" */
  876. { NULL, NULL,
  877. 0x04a7, 0x0475,
  878. "Xerox", "DocuMate272",
  879. AV_INT_BUTTON},
  880. /* status="untested" */
  881. { NULL, NULL,
  882. 0x04a7, 0x048E,
  883. "Xerox", "DocuMate272-G",
  884. AV_INT_BUTTON},
  885. /* status="untested" */
  886. { NULL, NULL,
  887. 0x04a7, 0x0446,
  888. "Xerox", "DocuMate510",
  889. AV_INT_BUTTON},
  890. /* status="untested" */
  891. { NULL, NULL,
  892. 0x04a7, 0x0495,
  893. "Xerox", "DocuMate512",
  894. AV_INT_BUTTON},
  895. /* status="untested" */
  896. { NULL, NULL,
  897. 0x04a7, 0x047c,
  898. "Xerox", "DocuMate510-G",
  899. AV_INT_BUTTON},
  900. /* status="untested" */
  901. { NULL, NULL,
  902. 0x04a7, 0x0447,
  903. "Xerox", "DocuMate520",
  904. AV_INT_BUTTON},
  905. /* status="untested" */
  906. { NULL, NULL,
  907. 0x04a7, 0x0492,
  908. "Xerox", "DocuMate520-G",
  909. AV_INT_BUTTON},
  910. /* status="untested" */
  911. { NULL, NULL,
  912. 0x04a7, 0x0498,
  913. "Xerox", "DocuMate632",
  914. AV_INT_BUTTON},
  915. /* status="untested" */
  916. { NULL, NULL,
  917. 0x04a7, 0x0478,
  918. "Xerox", "DocuMate752",
  919. AV_INT_BUTTON},
  920. /* status="untested" */
  921. { NULL, NULL,
  922. 0x04a7, 0x049A,
  923. "Xerox", "DocuMate752",
  924. AV_INT_BUTTON},
  925. /* status="untested" */
  926. { NULL, NULL,
  927. 0x0638, 0x0a16,
  928. "OKI", "S700 Scancopier",
  929. 0},
  930. /* comment="1 pass, 600 dpi, A4" */
  931. /* status="good" */
  932. { "B+H", "2000F",
  933. 0, 0,
  934. "Bell+Howell", "2000F",
  935. 0},
  936. /* comment="1 pass, ??? dpi, A4" */
  937. /* status="basic" */
  938. { NULL, NULL,
  939. 0x0482, 0x0335,
  940. "Kyocera", "FS-1016MFP",
  941. 0},
  942. /* comment="1 pass, ??? dpi, A4" */
  943. /* status="untested" */
  944. /* More IDs from the Avision dll:
  945. ArtiScan ProA3
  946. FB1065
  947. FB1265
  948. PHI860S
  949. PSDC SCSI
  950. SCSI Scan 19200
  951. V6240 */
  952. /* Possibly:
  953. Lexmark 4600 MFP Option MFP Options
  954. Lexmark 4600 MFP Option (C772n) MFP Options
  955. Lexmark X215
  956. Lexmark Optra Image X242
  957. Lexmark X443
  958. Lexmark 3100
  959. Lexmark 3200
  960. Lexmark X340 MFP Multifunction
  961. Lexmark X342n MFP Multifunction
  962. Lexmark X522
  963. Lexmark X630
  964. Lexmark X632E
  965. Lexmark X642e MFP Multifunction
  966. Lexmark X644e MFP Multifunction
  967. Lexmark X646dte MFP Multifunction
  968. Lexmark X646e MFP Multifunction
  969. Lexmark X646ef MFP Multifunction
  970. Lexmark X772e Multifunction
  971. Lexmark X850e MFP Multifunction
  972. Lexmark X852e MFP Multifunction
  973. Lexmark X854e MFP Multifunction
  974. Lexmark X4500 MFP
  975. */
  976. /* last entry detection */
  977. { NULL, NULL,
  978. 0, 0,
  979. NULL, NULL,
  980. 0}
  981. };
  982. #if 0
  983. struct timeval tv;
  984. #define TIMING(txt) gettimeofday (&tv, NULL); \
  985. DBG (4, "%lu: " txt "\n", tv.tv_sec * 1000000 + tv.tv_usec)
  986. #else
  987. #define TIMING(txt)
  988. #endif
  989. /* used when scanner returns invalid range fields ... */
  990. #define A4_X_RANGE 8.5 /* or 8.25 ? */
  991. #define A4_Y_RANGE 11.8
  992. #define A3_X_RANGE 11.8
  993. #define A3_Y_RANGE 16.5 /* or 17 ? */
  994. #define FILM_X_RANGE 1.0 /* really ? */
  995. #define FILM_Y_RANGE 1.0
  996. #define SHEETFEED_Y_RANGE 14.0
  997. #define AVISION_CONFIG_FILE "avision.conf"
  998. #define STD_INQUIRY_SIZE 0x24
  999. #define AVISION_INQUIRY_SIZE_V1 0x60
  1000. #define AVISION_INQUIRY_SIZE_V2 0x88
  1001. #define AVISION_INQUIRY_SIZE_MAX AVISION_INQUIRY_SIZE_V2
  1002. #define MM_PER_INCH 25.4
  1003. #define AVISION_BASE_RES 300
  1004. /* calibration (shading) defines */
  1005. #define INVALID_WHITE_SHADING 0x0000
  1006. #define DEFAULT_WHITE_SHADING 0xFFF0
  1007. #define MAX_WHITE_SHADING 0xFFFF
  1008. /* originally the WHITE_MAP_RANGE was 0x4000 - but this always
  1009. * resulted in slightly too dark images - thus I have choosen
  1010. * 0x4FFF ... */
  1011. #define WHITE_MAP_RANGE 0x4FFF
  1012. #define INVALID_DARK_SHADING 0xFFFF
  1013. #define DEFAULT_DARK_SHADING 0x0000
  1014. #define read_constrains(s,var) {\
  1015. if (s->hw->hw->feature_type & AV_NO_64BYTE_ALIGN) {\
  1016. if (var % 64 == 0) var /= 2;\
  1017. if (var % 64 == 0) var += 2;\
  1018. }\
  1019. }\
  1020. static int num_devices;
  1021. static Avision_Device* first_dev;
  1022. static Avision_Scanner* first_handle;
  1023. static const SANE_Device** devlist = 0;
  1024. /* this is a bit hacky to get extra information in the attach callback */
  1025. static Avision_HWEntry* attaching_hw = 0;
  1026. /* disable the usage of a custom gamma-table */
  1027. static SANE_Bool disable_gamma_table = SANE_FALSE;
  1028. /* disable the calibration */
  1029. static SANE_Bool disable_calibration = SANE_FALSE;
  1030. static SANE_Bool force_calibration = SANE_FALSE;
  1031. /* force scanable areas to ISO(DIN) A4/A3 */
  1032. static SANE_Bool force_a4 = SANE_FALSE;
  1033. static SANE_Bool force_a3 = SANE_FALSE;
  1034. /* hardware resolutions to interpolate from */
  1035. static const int hw_res_list_c5[] =
  1036. {
  1037. /* tested on AV600U */
  1038. 75, 150, 300, 600, 1200, 2400, 4800, /* ... */ 0
  1039. };
  1040. static const int hw_res_list_generic[] =
  1041. {
  1042. 50, /* slower than 150 on the AV122/DM152, left for USB 1 host's preview */
  1043. 75, /* slower than 150 on the AV122/DM152, left for USB 1 host's */
  1044. 150, 200, 300,
  1045. /* 400,*/ /* AV122 simplex y-scaling and duplex interlacing corrupt */
  1046. 600, 1200, 2400, 4800,
  1047. /* ... */
  1048. 0
  1049. };
  1050. static SANE_Bool static_calib_list[3] =
  1051. {
  1052. SANE_FALSE, SANE_FALSE, SANE_FALSE
  1053. };
  1054. static const SANE_Range u8_range =
  1055. {
  1056. 0, /* minimum */
  1057. 255, /* maximum */
  1058. 0 /* quantization */
  1059. };
  1060. static const SANE_Range percentage_range =
  1061. {
  1062. SANE_FIX (-100), /* minimum */
  1063. SANE_FIX (100), /* maximum */
  1064. SANE_FIX (1) /* quantization */
  1065. };
  1066. static const SANE_Range abs_percentage_range =
  1067. {
  1068. SANE_FIX (0), /* minimum */
  1069. SANE_FIX (100), /* maximum */
  1070. SANE_FIX (1) /* quantization */
  1071. };
  1072. static const SANE_Range exposure_range =
  1073. {
  1074. 0, /* minimum */
  1075. 1000, /* maximum */
  1076. 1 /* quantization */
  1077. };
  1078. static const SANE_Range overscan_range =
  1079. {
  1080. SANE_FIX (0), /* minimum */
  1081. SANE_FIX (4), /* maximum */ /* 4mm, meassured on AV122, AV220C2, i40 */
  1082. 0 /* quantization */
  1083. };
  1084. /* The 0x32 is a random guess based on USB logs. Might need a
  1085. per-device value in the future - 0x32 was tested on the AV122,
  1086. DM152, AV220. */
  1087. static const SANE_Range background_range =
  1088. {
  1089. 0, /* minimum */
  1090. 0x32, /* maximum */
  1091. 0 /* quantization */
  1092. };
  1093. static const uint8_t test_unit_ready[] =
  1094. {
  1095. AVISION_SCSI_TEST_UNIT_READY, 0x00, 0x00, 0x00, 0x00, 0x00
  1096. };
  1097. static const uint8_t get_status[] =
  1098. {
  1099. AVISION_SCSI_GET_DATA_STATUS, 0x00, 0x00, 0x00, 0x00, 0x00,
  1100. 0x00, 0x00, 0x0c, 0x00
  1101. };
  1102. static size_t
  1103. max_string_size (const SANE_String_Const strings[])
  1104. {
  1105. size_t size, max_size = 0;
  1106. int i;
  1107. DBG (3, "max_string_size:\n");
  1108. for (i = 0; strings[i]; ++ i) {
  1109. size = strlen (strings[i]) + 1;
  1110. if (size > max_size)
  1111. max_size = size;
  1112. }
  1113. return max_size;
  1114. }
  1115. static SANE_Status
  1116. constrain_value (Avision_Scanner* s, SANE_Int option, void* value,
  1117. SANE_Int* info)
  1118. {
  1119. DBG (3, "constrain_value:\n");
  1120. return sanei_constrain_value (s->opt + option, value, info);
  1121. }
  1122. static void debug_print_raw (int dbg_level, char* info, const uint8_t* data,
  1123. size_t count)
  1124. {
  1125. size_t i;
  1126. DBG (dbg_level, info);
  1127. for (i = 0; i < count; ++ i) {
  1128. DBG (dbg_level, " [%lu] %1d%1d%1d%1d%1d%1d%1d%1db %3oo %3dd %2xx\n",
  1129. (u_long) i,
  1130. BIT(data[i],7), BIT(data[i],6), BIT(data[i],5), BIT(data[i],4),
  1131. BIT(data[i],3), BIT(data[i],2), BIT(data[i],1), BIT(data[i],0),
  1132. data[i], data[i], data[i]);
  1133. }
  1134. }
  1135. static void debug_print_hex_raw (int dbg_level, char* info, const uint8_t* data,
  1136. size_t count)
  1137. {
  1138. int address = 0;
  1139. char text [16*3+1];
  1140. DBG (dbg_level, info);
  1141. while (count) {
  1142. char* t = text;
  1143. int i = 0;
  1144. while (i < 16 && count) {
  1145. t += sprintf (t, "%02x ", *data++);
  1146. count--; i++;
  1147. }
  1148. *--t = 0;
  1149. DBG (dbg_level, " [%08x] %s\n", address, text);
  1150. address += 16;
  1151. }
  1152. }
  1153. static void debug_print_nvram_data (int dbg_level, char* func,
  1154. nvram_data* nvram)
  1155. {
  1156. DBG (dbg_level, "%s: pad scans: %d\n",
  1157. func, get_quad(nvram->pad_scans));
  1158. DBG (dbg_level, "%s: ADF simplex scans: %d\n",
  1159. func, get_quad(nvram->adf_simplex_scans));
  1160. DBG (dbg_level, "%s: ADF duplex scans: %d\n",
  1161. func, get_quad(nvram->adf_duplex_scans));
  1162. DBG (dbg_level, "%s: flatbed scans: %d\n",
  1163. func, get_quad(nvram->flatbed_scans));
  1164. DBG (dbg_level, "%s: flatbed leading edge: %d\n",
  1165. func, (int16_t)get_double(nvram->flatbed_leading_edge));
  1166. DBG (dbg_level, "%s: flatbed side edge: %d\n",
  1167. func, (int16_t)get_double(nvram->flatbed_side_edge));
  1168. DBG (dbg_level, "%s: ADF leading edge: %d\n",
  1169. func, (int16_t)get_double(nvram->adf_leading_edge));
  1170. DBG (dbg_level, "%s: ADF side edge: %d\n",
  1171. func, (int16_t)get_double(nvram->adf_side_edge));
  1172. DBG (dbg_level, "%s: ADF rear leading edge: %d\n",
  1173. func, (int16_t)get_double(nvram->adf_rear_leading_edge));
  1174. DBG (dbg_level, "%s: ADF rear side edge: %d\n",
  1175. func, (int16_t)get_double(nvram->adf_rear_side_edge));
  1176. DBG (dbg_level, "%s: born month: %d\n",
  1177. func, get_double(nvram->born_month));
  1178. DBG (dbg_level, "%s: born day: %d\n",
  1179. func, get_double(nvram->born_day));
  1180. DBG (dbg_level, "%s: born year: %d\n",
  1181. func, get_double(nvram->born_year));
  1182. DBG (dbg_level, "%s: first scan month: %d\n",
  1183. func, get_double(nvram->first_scan_month));
  1184. DBG (dbg_level, "%s: first scan day: %d\n",
  1185. func, get_double(nvram->first_scan_day));
  1186. DBG (dbg_level, "%s: first scan year: %d\n",
  1187. func, get_double(nvram->first_scan_year));
  1188. DBG (dbg_level, "%s: vert. magnification: %d\n",
  1189. func, get_double(nvram->vertical_magnification));
  1190. DBG (dbg_level, "%s: horiz. magnification: %d\n",
  1191. func, get_double(nvram->horizontal_magnification));
  1192. DBG (dbg_level, "%s: CCD type: %d\n",
  1193. func, nvram->ccd_type);
  1194. DBG (dbg_level, "%s: scan speed: %d\n",
  1195. func, nvram->scan_speed);
  1196. DBG (dbg_level, "%s: serial: '%.24s'\n", /* 24 chars max */
  1197. func, nvram->serial);
  1198. DBG (dbg_level, "%s: power saving time: %d\n",
  1199. func, get_double(nvram->power_saving_time));
  1200. DBG (dbg_level, "%s: auto feed: %d\n",
  1201. func, nvram->auto_feed);
  1202. DBG (dbg_level, "%s: roller count: %d\n",
  1203. func, get_quad(nvram->roller_count));
  1204. DBG (dbg_level, "%s: multifeed count: %d\n",
  1205. func, get_quad(nvram->multifeed_count));
  1206. DBG (dbg_level, "%s: jam count: %d\n",
  1207. func, get_quad(nvram->jam_count));
  1208. DBG (dbg_level, "%s: identify info: '%.16s'\n", /* 16 chars max */
  1209. func, nvram->identify_info);
  1210. DBG (dbg_level, "%s: formal_name: '%.16s'\n", /* 16 chars max */
  1211. func, nvram->formal_name);
  1212. }
  1213. static void debug_print_avdimen (int dbg_level, char* func,
  1214. Avision_Dimensions* avdimen)
  1215. {
  1216. DBG (dbg_level, "%s: hw_xres: %d, hw_yres: %d, line_difference: %d\n",
  1217. func, avdimen->hw_xres, avdimen->hw_yres, avdimen->line_difference);
  1218. DBG (dbg_level, "%s: tlx: %ld, tly: %ld, brx: %ld, bry: %ld\n",
  1219. func, avdimen->tlx, avdimen->tly,
  1220. avdimen->brx, avdimen->bry);
  1221. DBG (dbg_level, "%s: hw_pixel_per_line: %d, hw_lines: %d, hw_bytes_per_line: %d\n",
  1222. func, avdimen->hw_pixels_per_line, avdimen->hw_lines, avdimen->hw_bytes_per_line);
  1223. DBG (dbg_level, "%s: xres: %d, yres: %d\n",
  1224. func, avdimen->xres, avdimen->yres);
  1225. }
  1226. static void debug_print_params (int dbg_level, char* func, SANE_Parameters* params)
  1227. {
  1228. DBG (dbg_level, "%s: pixel_per_line: %d, lines: %d\n",
  1229. func, params->pixels_per_line, params->lines);
  1230. DBG (dbg_level, "%s: depth: %d, bytes_per_line: %d\n",
  1231. func, params->depth, params->bytes_per_line);
  1232. }
  1233. static void debug_print_calib_format (int dbg_level, char* func,
  1234. uint8_t* result)
  1235. {
  1236. debug_print_raw (dbg_level + 2, "debug_print_calib_format:\n", result, 32);
  1237. DBG (dbg_level, "%s: [0-1] pixels per line: %d\n",
  1238. func, get_double ( &(result[0]) ));
  1239. DBG (dbg_level, "%s: [2] bytes per channel: %d\n", func, result[2]);
  1240. DBG (dbg_level, "%s: [3] line count: %d\n", func, result[3]);
  1241. DBG (dbg_level, "%s: [4] FLAG:%s%s%s\n",
  1242. func,
  1243. result[4] == 1?" MUST_DO_CALIBRATION":"",
  1244. result[4] == 2?" SCAN_IMAGE_DOES_CALIBRATION":"",
  1245. result[4] == 3?" NEEDS_NO_CALIBRATION":"");
  1246. DBG (dbg_level, "%s: [5] Ability1:%s%s%s%s%s%s%s%s\n",
  1247. func,
  1248. BIT(result[5],7)?" NONE_PACKED":" PACKED",
  1249. BIT(result[5],6)?" INTERPOLATED":"",
  1250. BIT(result[5],5)?" SEND_REVERSED":"",
  1251. BIT(result[5],4)?" PACKED_DATA":"",
  1252. BIT(result[5],3)?" COLOR_CALIB":"",
  1253. BIT(result[5],2)?" DARK_CALIB":"",
  1254. BIT(result[5],1)?" NEEDS_WHITE_BLACK_SHADING_DATA":"",
  1255. BIT(result[5],0)?" NEEDS_CALIB_TABLE_CHANNEL_BY_CHANNEL":"");
  1256. DBG (dbg_level, "%s: [6] R gain: %d\n", func, result[6]);
  1257. DBG (dbg_level, "%s: [7] G gain: %d\n", func, result[7]);
  1258. DBG (dbg_level, "%s: [8] B gain: %d\n", func, result[8]);
  1259. DBG (dbg_level, "%s: [9-10] R shading target: %x\n",
  1260. func, get_double ( &(result[9]) ) );
  1261. DBG (dbg_level, "%s: [11-12] G shading target: %x\n",
  1262. func, get_double ( &(result[11]) ) );
  1263. DBG (dbg_level, "%s: [13-14] B shading target: %x\n",
  1264. func, get_double ( &(result[13]) ) );
  1265. DBG (dbg_level, "%s: [15-16] R dark shading target: %x\n",
  1266. func, get_double ( &(result[15]) ) );
  1267. DBG (dbg_level, "%s: [17-18] G dark shading target: %x\n",
  1268. func, get_double ( &(result[17]) ) );
  1269. DBG (dbg_level, "%s: [19-20] B dark shading target: %x\n",
  1270. func, get_double ( &(result[19]) ) );
  1271. DBG (dbg_level, "%s: [21] true-gray gain: %d\n", func, result[21]);
  1272. DBG (dbg_level, "%s: [22-23] true-gray shading target: %x\n",
  1273. func, get_double ( &(result[22]) ) );
  1274. DBG (dbg_level, "%s: [24-25] true-gray dark shading target: %x\n",
  1275. func, get_double ( &(result[24]) ) );
  1276. }
  1277. static void debug_print_accel_info (int dbg_level, char* func,
  1278. uint8_t* result)
  1279. {
  1280. debug_print_raw (dbg_level + 2, "debug_print_accel_info:\n", result, 24);
  1281. DBG (dbg_level, "%s: [0-1] acceleration step count: %d\n",
  1282. func, get_double ( &(result[0]) ));
  1283. DBG (dbg_level, "%s: [2-3] stable step count: %d\n",
  1284. func, get_double ( &(result[2]) ));
  1285. DBG (dbg_level, "%s: [4-7] table units: %d\n",
  1286. func, get_quad ( &(result[4]) ));
  1287. DBG (dbg_level, "%s: [8-11] base units: %d\n",
  1288. func, get_quad ( &(result[8]) ));
  1289. DBG (dbg_level, "%s: [12-13] start speed: %d\n",
  1290. func, get_double ( &(result[12]) ));
  1291. DBG (dbg_level, "%s: [14-15] target speed: %d\n",
  1292. func, get_double ( &(result[14]) ));
  1293. DBG (dbg_level, "%s: [16] ability:%s%s\n",
  1294. func,
  1295. BIT(result[16],0)?" TWO_BYTES_PER_ELEM":" SINGLE_BYTE_PER_ELEM",
  1296. BIT(result[16],1)?" LOW_HIGH_ORDER":" HIGH_LOW_ORDER");
  1297. DBG (dbg_level, "%s: [17] table count: %d\n", func, result[17]);
  1298. }
  1299. static void debug_print_window_descriptor (int dbg_level, char* func,
  1300. command_set_window_window* window)
  1301. {
  1302. debug_print_raw (dbg_level + 1, "window_data_header: \n",
  1303. (uint8_t*)(&window->header),
  1304. sizeof(window->header));
  1305. debug_print_raw (dbg_level + 1, "window_descriptor: \n",
  1306. (uint8_t*)(&window->descriptor),
  1307. sizeof(*window) -
  1308. sizeof(window->header));
  1309. DBG (dbg_level, "%s: [0] window_id: %d\n", func,
  1310. window->descriptor.winid);
  1311. DBG (dbg_level, "%s: [2-3] x-axis res: %d\n", func,
  1312. get_double (window->descriptor.xres));
  1313. DBG (dbg_level, "%s: [4-5] y-axis res: %d\n", func,
  1314. get_double (window->descriptor.yres));
  1315. DBG (dbg_level, "%s: [6-9] x-axis upper left: %d\n",
  1316. func, get_quad (window->descriptor.ulx));
  1317. DBG (dbg_level, "%s: [10-13] y-axis upper left: %d\n",
  1318. func, get_quad (window->descriptor.uly));
  1319. DBG (dbg_level, "%s: [14-17] window width: %d\n", func,
  1320. get_quad (window->descriptor.width));
  1321. DBG (dbg_level, "%s: [18-21] window length: %d\n", func,
  1322. get_quad (window->descriptor.length));
  1323. DBG (dbg_level, "%s: [22] brightness: %d\n", func,
  1324. window->descriptor.brightness);
  1325. DBG (dbg_level, "%s: [23] threshold: %d\n", func,
  1326. window->descriptor.threshold);
  1327. DBG (dbg_level, "%s: [24] contrast: %d\n", func,
  1328. window->descriptor.contrast);
  1329. DBG (dbg_level, "%s: [25] image composition: %x\n", func,
  1330. window->descriptor.image_comp);
  1331. DBG (dbg_level, "%s: [26] bits per channel: %d\n", func,
  1332. window->descriptor.bpc);
  1333. DBG (dbg_level, "%s: [27-28] halftone pattern: %x\n", func,
  1334. get_double (window->descriptor.halftone));
  1335. DBG (dbg_level, "%s: [29] padding_and_bitset: %x\n", func,
  1336. window->descriptor.padding_and_bitset);
  1337. DBG (dbg_level, "%s: [30-31] bit ordering: %x\n", func,
  1338. get_double (window->descriptor.bitordering));
  1339. DBG (dbg_level, "%s: [32] compression type: %x\n", func,
  1340. window->descriptor.compr_type);
  1341. DBG (dbg_level, "%s: [33] compression argument: %x\n", func,
  1342. window->descriptor.compr_arg);
  1343. DBG (dbg_level, "%s: [34-35] paper length: %x\n", func,
  1344. get_double (window->descriptor.paper_length) );
  1345. DBG (dbg_level, "%s: [40] vendor id: %x\n", func,
  1346. window->descriptor.vendor_specific);
  1347. DBG (dbg_level, "%s: [41] param lenght: %d\n", func,
  1348. window->descriptor.paralen);
  1349. DBG (dbg_level, "%s: [42] bitset1: %x\n", func,
  1350. window->avision.bitset1);
  1351. DBG (dbg_level, "%s: [43] highlight: %d\n", func,
  1352. window->avision.highlight);
  1353. DBG (dbg_level, "%s: [44] shadow: %d\n", func,
  1354. window->avision.shadow);
  1355. DBG (dbg_level, "%s: [45-46] line-width: %d\n", func,
  1356. get_double (window->avision.line_width));
  1357. DBG (dbg_level, "%s: [47-48] line-count: %d\n", func,
  1358. get_double (window->avision.line_count));
  1359. DBG (dbg_level, "%s: [49] bitset2: %x\n", func,
  1360. window->avision.type.normal.bitset2);
  1361. DBG (dbg_level, "%s: [50] ir exposure time: %x\n",
  1362. func, window->avision.type.normal.ir_exposure_time);
  1363. DBG (dbg_level, "%s: [51-52] r exposure: %x\n", func,
  1364. get_double (window->avision.type.normal.r_exposure_time));
  1365. DBG (dbg_level, "%s: [53-54] g exposure: %x\n", func,
  1366. get_double (window->avision.type.normal.g_exposure_time));
  1367. DBG (dbg_level, "%s: [55-56] b exposure: %x\n", func,
  1368. get_double (window->avision.type.normal.b_exposure_time));
  1369. DBG (dbg_level, "%s: [57] bitset3: %x\n", func,
  1370. window->avision.type.normal.bitset3);
  1371. DBG (dbg_level, "%s: [58] auto focus: %d\n", func,
  1372. window->avision.type.normal.auto_focus);
  1373. DBG (dbg_level, "%s: [59] line-width (MSB): %d\n",
  1374. func, window->avision.type.normal.line_width_msb);
  1375. DBG (dbg_level, "%s: [60] line-count (MSB): %d\n",
  1376. func, window->avision.type.normal.line_count_msb);
  1377. DBG (dbg_level, "%s: [61] background lines: %d\n",
  1378. func, window->avision.type.normal.background_lines);
  1379. }
  1380. static int write_pnm_header (FILE* f, color_mode m, int depth, int width, int height)
  1381. {
  1382. int maxval = (1 << depth) - 1;
  1383. const char* hdr_str = NULL;
  1384. /* construct PNM header */
  1385. switch (m) {
  1386. case AV_THRESHOLDED:
  1387. case AV_DITHERED:
  1388. hdr_str = "P4\n%d %d\n";
  1389. break;
  1390. case AV_GRAYSCALE:
  1391. case AV_GRAYSCALE12:
  1392. case AV_GRAYSCALE16:
  1393. hdr_str = "P5\n%d %d\n%d\n";
  1394. break;
  1395. case AV_TRUECOLOR:
  1396. case AV_TRUECOLOR12:
  1397. case AV_TRUECOLOR16:
  1398. hdr_str = "P6\n%d %d\n%d\n";
  1399. break;
  1400. case AV_COLOR_MODE_LAST:
  1401. ; /* silence compiler warning */
  1402. }
  1403. return fprintf (f, hdr_str, width, height, maxval);
  1404. }
  1405. static SANE_Status
  1406. sense_handler (int fd, u_char* sense, void* arg)
  1407. {
  1408. SANE_Status status = SANE_STATUS_IO_ERROR; /* default case */
  1409. char* text;
  1410. char textbuf[64];
  1411. uint8_t error_code = sense[0] & 0x7f;
  1412. uint8_t sense_key = sense[2] & 0xf;
  1413. uint8_t additional_sense = sense[7];
  1414. fd = fd; /* silence gcc */
  1415. arg = arg; /* silence gcc */
  1416. DBG (3, "sense_handler:\n");
  1417. switch (error_code)
  1418. {
  1419. case 0x70:
  1420. text = "standard sense";
  1421. break;
  1422. case 0x7f:
  1423. text = "Avision-specific sense";
  1424. break;
  1425. default:
  1426. text = "unknown sense";
  1427. }
  1428. debug_print_raw (1, "sense_handler: data:\n", sense, 8 + additional_sense);
  1429. /* request valid? */
  1430. if (! sense[0] & (1<<7)) {
  1431. DBG (1, "sense_handler: sense not vaild ...\n");
  1432. return status;
  1433. }
  1434. switch (sense_key)
  1435. {
  1436. case 0x00:
  1437. status = SANE_STATUS_GOOD;
  1438. text = "ok ?!?";
  1439. break;
  1440. case 0x02:
  1441. text = "NOT READY";
  1442. break;
  1443. case 0x03:
  1444. text = "MEDIUM ERROR (mostly ADF)";
  1445. status = SANE_STATUS_JAMMED;
  1446. break;
  1447. case 0x04:
  1448. text = "HARDWARE ERROR";
  1449. break;
  1450. case 0x05:
  1451. text = "ILLEGAL REQUEST";
  1452. break;
  1453. case 0x06:
  1454. text = "UNIT ATTENTION";
  1455. break;
  1456. case 0x09:
  1457. text = "VENDOR SPECIFIC";
  1458. break;
  1459. case 0x0b:
  1460. text = "ABORTED COMMAND";
  1461. status = SANE_STATUS_CANCELLED; /* AV610C2 cancel button */
  1462. break;
  1463. default:
  1464. sprintf (textbuf, "got unknown sense code 0x%02x", (int)sense_key);
  1465. text = textbuf;
  1466. }
  1467. DBG (1, "sense_handler: sense code: %s\n", text);
  1468. if (sense[2] & (1<<6))
  1469. DBG (1, "sense_handler: end of scan\n");
  1470. else
  1471. DBG (1, "sense_handler: scan has not yet been completed\n");
  1472. if (sense[2] & (1<<5))
  1473. DBG (1, "sense_handler: incorrect logical length\n");
  1474. else
  1475. DBG (1, "sense_handler: correct logical length\n");
  1476. {
  1477. uint8_t asc = sense[12];
  1478. uint8_t ascq = sense[13];
  1479. #define ADDITIONAL_SENSE(asc,ascq,txt) \
  1480. case ( (asc << 8) + ascq): text = txt; break
  1481. switch ( (asc << 8) + ascq )
  1482. {
  1483. /* normal */
  1484. ADDITIONAL_SENSE (0x00,0x00, "No additional sense information");
  1485. ADDITIONAL_SENSE (0x00,0x06, "I/O process terminated");
  1486. ADDITIONAL_SENSE (0x15,0x01, "Mechanical positioning error");
  1487. ADDITIONAL_SENSE (0x15,0x02, "Flatbed Home Sensor Error (OKI only");
  1488. ADDITIONAL_SENSE (0x15,0x03, "ADF Home Sensor Error (OKI only)");
  1489. ADDITIONAL_SENSE (0x15,0x04, "Lock Error (OKI only)");
  1490. ADDITIONAL_SENSE (0x1a,0x00, "parameter list lenght error");
  1491. ADDITIONAL_SENSE (0x20,0x00, "Invalid command");
  1492. ADDITIONAL_SENSE (0x24,0x00, "Invalid field in CDB");
  1493. ADDITIONAL_SENSE (0x25,0x00, "Logical unit not supported");
  1494. ADDITIONAL_SENSE (0x26,0x00, "Invalid field in parameter list");
  1495. ADDITIONAL_SENSE (0x26,0x01, "parameter not supported");
  1496. ADDITIONAL_SENSE (0x26,0x02, "parameter value invalid");
  1497. ADDITIONAL_SENSE (0x29,0x00, "Power-on, reset or bus device reset occurred");
  1498. ADDITIONAL_SENSE (0x2c,0x02, "Invaild combination of window specified");
  1499. ADDITIONAL_SENSE (0x2f,0x00, "Command cleared by another initiator");
  1500. ADDITIONAL_SENSE (0x3D,0x00, "Invalid Bit in Identify Message");
  1501. ADDITIONAL_SENSE (0x43,0x00, "Message error");
  1502. ADDITIONAL_SENSE (0x44,0x00, "Internal target failure");
  1503. ADDITIONAL_SENSE (0x44,0x01, "Flatbed DRAM Error(OKI only)");
  1504. ADDITIONAL_SENSE (0x44,0x02, "ADF DRAM Error(OKI only)");
  1505. ADDITIONAL_SENSE (0x44,0x03, "Write NVRAM Error");
  1506. ADDITIONAL_SENSE (0x47,0x00, "SCSI paritx error");
  1507. ADDITIONAL_SENSE (0x49,0x00, "Invalid message error");
  1508. ADDITIONAL_SENSE (0x60,0x00, "Lamp failure");
  1509. ADDITIONAL_SENSE (0x60,0x01, "Flatbed Lamp error (Oki only)");
  1510. ADDITIONAL_SENSE (0x60,0x02, "ADF lamp error (Oki only)");
  1511. ADDITIONAL_SENSE (0x62,0x00, "Scan head positioning error");
  1512. ADDITIONAL_SENSE (0x80,0x01, "ADF paper jam"; status = SANE_STATUS_JAMMED);
  1513. ADDITIONAL_SENSE (0x80,0x02, "ADF cover open"; status = SANE_STATUS_COVER_OPEN);
  1514. ADDITIONAL_SENSE (0x80,0x03, "ADF chute empty"; status = SANE_STATUS_NO_DOCS);
  1515. ADDITIONAL_SENSE (0x80,0x04, "ADF paper end"; status = SANE_STATUS_EOF);
  1516. ADDITIONAL_SENSE (0x80,0x05, "Multi-feed (AV220,Kodak)");
  1517. ADDITIONAL_SENSE (0x80,0x06, "ADF prefeeding (OKI only)");
  1518. ADDITIONAL_SENSE (0x80,0x07, "Flatbed cover open (OKI only)"; status = SANE_STATUS_COVER_OPEN);
  1519. ADDITIONAL_SENSE (0x80,0x08, "FW module doesn't match with scanner");
  1520. ADDITIONAL_SENSE (0x80,0x09, "Papers fed from multiple trays (DM272)");
  1521. ADDITIONAL_SENSE (0x80,0x0A, "ADF Paper Start");
  1522. ADDITIONAL_SENSE (0x80,0x0B, "Multiple ADF paper End and Start");
  1523. ADDITIONAL_SENSE (0x80,0x0C, "Multiple ADF paper End");
  1524. /* film scanner */
  1525. ADDITIONAL_SENSE (0x81,0x00, "ADF/MFP front door open"; status = SANE_STATUS_COVER_OPEN);
  1526. ADDITIONAL_SENSE (0x81,0x01, "ADF holder cartrige open"; status = SANE_STATUS_COVER_OPEN);
  1527. ADDITIONAL_SENSE (0x81,0x02, "ADF no film inside"; status = SANE_STATUS_NO_DOCS);
  1528. ADDITIONAL_SENSE (0x81,0x03, "ADF initial load fail");
  1529. ADDITIONAL_SENSE (0x81,0x04, "ADF film end"; status = SANE_STATUS_NO_DOCS);
  1530. ADDITIONAL_SENSE (0x81,0x05, "ADF forward feed error");
  1531. ADDITIONAL_SENSE (0x81,0x06, "ADF rewind error");
  1532. ADDITIONAL_SENSE (0x81,0x07, "ADF set unload");
  1533. ADDITIONAL_SENSE (0x81,0x08, "ADF adapter error");
  1534. ADDITIONAL_SENSE (0xA0,0x01, "Filter Positioning Error");
  1535. ADDITIONAL_SENSE (0x90,0x00, "Scanner busy (FW busy)");
  1536. default:
  1537. sprintf (textbuf, "Unknown sense code asc: 0x%02x, ascq: 0x%02x",
  1538. (int)asc, (int)ascq);
  1539. text = textbuf;
  1540. }
  1541. #undef ADDITIONAL_SENSE
  1542. DBG (1, "sense_handler: sense code: %s\n", text);
  1543. /* sense code specific for invalid request
  1544. * it is possible to get a detailed error location here ;-)*/
  1545. if (sense_key == 0x05) {
  1546. if (sense[15] & (1<<7) )
  1547. {
  1548. if (sense[15] & (1<<6) )
  1549. DBG (1, "sense_handler: error in command parameter\n");
  1550. else
  1551. DBG (1, "sense_handler: error in data parameter\n");
  1552. DBG (1, "sense_handler: error in parameter byte: %d, %x\n",
  1553. get_double(&(sense[16])), get_double(&(sense[16])));
  1554. /* bit pointer valid ?*/
  1555. if (sense[15] & (1<<3) )
  1556. DBG (1, "sense_handler: error in command parameter\n");
  1557. else
  1558. DBG (1, "sense_handler: bit pointer invalid\n");
  1559. }
  1560. }
  1561. }
  1562. return status;
  1563. }
  1564. /*
  1565. * Avision scsi/usb multiplexers - to keep the code clean:
  1566. */
  1567. static SANE_Status
  1568. avision_usb_status (Avision_Connection* av_con, int retry, int timeout)
  1569. {
  1570. SANE_Status status = 0;
  1571. uint8_t usb_status[1] = {0};
  1572. size_t count = 0;
  1573. int t_retry = retry;
  1574. #define valid_status(status,a) (status == SANE_STATUS_GOOD ? a : 0)
  1575. DBG (4, "avision_usb_status: timeout %d, %d retries\n", timeout, retry);
  1576. #ifndef HAVE_SANEI_USB_SET_TIMEOUT
  1577. #error "You must update include/sane/sanei_usb.h and sanei/sanei_usb.c accordingly!"
  1578. #endif
  1579. sanei_usb_set_timeout (timeout);
  1580. /* 1st try bulk transfers - they are more lightweight ... */
  1581. for (;
  1582. count == 0 &&
  1583. (av_con->usb_status == AVISION_USB_BULK_STATUS ||
  1584. av_con->usb_status == AVISION_USB_UNTESTED_STATUS) &&
  1585. retry > 0;
  1586. --retry)
  1587. {
  1588. count = sizeof (usb_status);
  1589. DBG (5, "==> (bulk read) going down ...\n");
  1590. status = sanei_usb_read_bulk (av_con->usb_dn, usb_status,
  1591. &count);
  1592. DBG (5, "<== (bulk read) got: %ld, status: %d\n",
  1593. (u_long)count, valid_status(status, usb_status[0]));
  1594. if (count > 0) {
  1595. av_con->usb_status = AVISION_USB_BULK_STATUS;
  1596. }
  1597. }
  1598. /* reset retry count ... */
  1599. retry = t_retry;
  1600. /* 2nd try interrupt status read - if not yet disabled */
  1601. for (;
  1602. count == 0 &&
  1603. (av_con->usb_status == AVISION_USB_INT_STATUS ||
  1604. av_con->usb_status == AVISION_USB_UNTESTED_STATUS) &&
  1605. retry > 0;
  1606. --retry)
  1607. {
  1608. count = sizeof (usb_status);
  1609. DBG (5, "==> (interrupt read) going down ...\n");
  1610. status = sanei_usb_read_int (av_con->usb_dn, usb_status,
  1611. &count);
  1612. DBG (5, "<== (interrupt read) got: %ld, status: %d\n",
  1613. (u_long)count, valid_status(status, usb_status[0]));
  1614. if (count > 0)
  1615. av_con->usb_status = AVISION_USB_INT_STATUS;
  1616. }
  1617. if (status != SANE_STATUS_GOOD)
  1618. return status;
  1619. if (count == 0)
  1620. return SANE_STATUS_IO_ERROR;
  1621. /* 0 = ok, 2 => request sense, 8 ==> busy, else error */
  1622. switch (usb_status[0])
  1623. {
  1624. case AVISION_USB_GOOD:
  1625. return SANE_STATUS_GOOD;
  1626. case AVISION_USB_REQUEST_SENSE:
  1627. DBG (2, "avision_usb_status: Needs to request sense!\n");
  1628. return SANE_STATUS_INVAL;
  1629. case AVISION_USB_BUSY:
  1630. DBG (2, "avision_usb_status: Busy!\n");
  1631. return SANE_STATUS_DEVICE_BUSY;
  1632. default:
  1633. DBG (1, "avision_usb_status: Unknown!\n");
  1634. return SANE_STATUS_INVAL;
  1635. }
  1636. }
  1637. static SANE_Status avision_open (const char* device_name,
  1638. Avision_Connection* av_con,
  1639. SANEI_SCSI_Sense_Handler sense_handler,
  1640. void *sense_arg)
  1641. {
  1642. if (av_con->connection_type == AV_SCSI) {
  1643. return sanei_scsi_open (device_name, &(av_con->scsi_fd),
  1644. sense_handler, sense_arg);
  1645. }
  1646. else {
  1647. SANE_Status status;
  1648. status = sanei_usb_open (device_name, &(av_con->usb_dn));
  1649. return status;
  1650. }
  1651. }
  1652. static SANE_Status avision_open_extended (const char* device_name,
  1653. Avision_Connection* av_con,
  1654. SANEI_SCSI_Sense_Handler sense_handler,
  1655. void *sense_arg, int *buffersize)
  1656. {
  1657. if (av_con->connection_type == AV_SCSI) {
  1658. return sanei_scsi_open_extended (device_name, &(av_con->scsi_fd),
  1659. sense_handler, sense_arg, buffersize);
  1660. }
  1661. else {
  1662. SANE_Status status;
  1663. status = sanei_usb_open (device_name, &(av_con->usb_dn));
  1664. return status;
  1665. }
  1666. }
  1667. static void avision_close (Avision_Connection* av_con)
  1668. {
  1669. if (av_con->connection_type == AV_SCSI) {
  1670. sanei_scsi_close (av_con->scsi_fd);
  1671. av_con->scsi_fd = -1;
  1672. }
  1673. else {
  1674. sanei_usb_close (av_con->usb_dn);
  1675. av_con->usb_dn = -1;
  1676. }
  1677. }
  1678. static SANE_Bool avision_is_open (Avision_Connection* av_con)
  1679. {
  1680. if (av_con->connection_type == AV_SCSI) {
  1681. return av_con->scsi_fd >= 0;
  1682. }
  1683. else {
  1684. return av_con->usb_dn >= 0;
  1685. }
  1686. }
  1687. static SANE_Status avision_cmd (Avision_Connection* av_con,
  1688. const void* cmd, size_t cmd_size,
  1689. const void* src, size_t src_size,
  1690. void* dst, size_t* dst_size)
  1691. {
  1692. if (av_con->connection_type == AV_SCSI) {
  1693. return sanei_scsi_cmd2 (av_con->scsi_fd, cmd, cmd_size,
  1694. src, src_size, dst, dst_size);
  1695. }
  1696. else {
  1697. SANE_Status status = SANE_STATUS_GOOD;
  1698. size_t i, count, out_count;
  1699. /* some commands on some devices need a rather long time to respond */
  1700. #define STD_TIMEOUT 30000
  1701. #define STD_STATUS_TIMEOUT 10000
  1702. int retry = 4;
  1703. int write_timeout = STD_TIMEOUT;
  1704. int read_timeout = STD_TIMEOUT;
  1705. int status_timeout = STD_STATUS_TIMEOUT;
  1706. /* simply to allow nicer code below */
  1707. const uint8_t* m_cmd = (const uint8_t*)cmd;
  1708. const uint8_t* m_src = (const uint8_t*)src;
  1709. uint8_t* m_dst = (uint8_t*)dst;
  1710. /* may I vote for the possibility to use C99 ... */
  1711. #define min_usb_size 10
  1712. #define max_usb_size 256 * 1024 /* or 0x10000, used by AV Windows driver during background raster read, ... ? */
  1713. /* 1st send command data - at least 10 Bytes for USB scanners */
  1714. uint8_t enlarged_cmd [min_usb_size];
  1715. if (cmd_size < min_usb_size) {
  1716. DBG (1, "filling command to have a length of 10, was: %lu\n", (u_long) cmd_size);
  1717. memcpy (enlarged_cmd, m_cmd, cmd_size);
  1718. memset (enlarged_cmd + cmd_size, 0, min_usb_size - cmd_size);
  1719. m_cmd = enlarged_cmd;
  1720. cmd_size = min_usb_size;
  1721. }
  1722. /* per command class timeout tweaks */
  1723. switch (m_cmd[0]) {
  1724. case AVISION_SCSI_INQUIRY:
  1725. read_timeout = 1000; /* quickly timeout on initial detection */
  1726. status_timeout = 1000;
  1727. break;
  1728. case AVISION_SCSI_TEST_UNIT_READY:
  1729. read_timeout = 15000; /* quickly timeout on initial detection */
  1730. status_timeout = 15000;
  1731. break;
  1732. }
  1733. DBG (7, "Timeouts: write: %d, read: %d, status: %d\n",
  1734. write_timeout, read_timeout, status_timeout);
  1735. write_usb_cmd:
  1736. if (--retry == 0) {
  1737. DBG (1, "Max retry count reached: I/O error\n");
  1738. return SANE_STATUS_IO_ERROR;
  1739. }
  1740. count = cmd_size;
  1741. sanei_usb_set_timeout (write_timeout);
  1742. DBG (8, "try to write cmd, count: %lu.\n", (u_long) count);
  1743. status = sanei_usb_write_bulk (av_con->usb_dn, m_cmd, &count);
  1744. DBG (8, "wrote %lu bytes\n", (u_long) count);
  1745. if (status != SANE_STATUS_GOOD || count != cmd_size) {
  1746. DBG (3, "=== Got error %d trying to write, wrote: %ld. ===\n",
  1747. status, (long)count);
  1748. if (status != SANE_STATUS_GOOD) /* == SANE_STATUS_EOF) */ {
  1749. DBG (3, "try to read status to clear the FIFO\n");
  1750. status = avision_usb_status (av_con, 1, 500);
  1751. if (status != SANE_STATUS_GOOD) {
  1752. DBG (3, "=== Got error %d trying to read status. ===\n", status);
  1753. return SANE_STATUS_IO_ERROR;
  1754. }
  1755. else
  1756. goto write_usb_cmd;
  1757. } else {
  1758. DBG (3, "Retrying to send command\n");
  1759. goto write_usb_cmd;
  1760. }
  1761. return SANE_STATUS_IO_ERROR;
  1762. }
  1763. /* 2nd send command data (if any) */
  1764. for (i = 0; i < src_size; ) {
  1765. count = src_size - i;
  1766. /* if (count > max_usb_size)
  1767. count = max_usb_size; */
  1768. DBG (8, "try to write src, count: %lu.\n", (u_long) count);
  1769. sanei_usb_set_timeout (write_timeout);
  1770. status = sanei_usb_write_bulk (av_con->usb_dn, &(m_src[i]), &count);
  1771. DBG (8, "wrote %lu bytes\n", (u_long) count);
  1772. if (status == SANE_STATUS_GOOD) {
  1773. i += count;
  1774. }
  1775. else {
  1776. goto write_usb_cmd;
  1777. }
  1778. }
  1779. /* 3rd: read the resuling data (payload) (if any) */
  1780. if (status == SANE_STATUS_GOOD && dst != NULL && *dst_size > 0) {
  1781. out_count = 0;
  1782. sanei_usb_set_timeout (read_timeout);
  1783. while (out_count < *dst_size) {
  1784. count = (*dst_size - out_count);
  1785. DBG (8, "try to read %lu bytes\n", (u_long) count);
  1786. status = sanei_usb_read_bulk(av_con->usb_dn, &(m_dst[out_count]),
  1787. &count);
  1788. DBG (8, "read %lu bytes\n", (u_long) count);
  1789. if (count == 1 && (*dst_size - out_count > 1)) {
  1790. DBG (1, "Got 1 byte - status? (%d) Resending.\n", m_dst[out_count]);
  1791. goto write_usb_cmd;
  1792. }
  1793. else if (count > 0) {
  1794. out_count += count;
  1795. }
  1796. else {
  1797. DBG (1, "No data arrived.\n");
  1798. goto write_usb_cmd;
  1799. }
  1800. }
  1801. }
  1802. /* last: read the device status via a pseudo interrupt transfer
  1803. * this is needed - otherwise the scanner will hang ... */
  1804. sanei_usb_set_timeout (status_timeout);
  1805. status = avision_usb_status (av_con, /*retry*/ 1, status_timeout);
  1806. /* next i/o hardening attempt - and yes this gets ugly ... */
  1807. if (status != SANE_STATUS_GOOD && status != SANE_STATUS_INVAL)
  1808. goto write_usb_cmd;
  1809. if (status == SANE_STATUS_INVAL) {
  1810. struct {
  1811. command_header header;
  1812. uint8_t pad[4];
  1813. } sense_cmd;
  1814. uint8_t sense_buffer[22];
  1815. DBG (3, "Error during status read!\n");
  1816. DBG (3, "=== Try to request sense ===\n");
  1817. /* we can not call avision_cmd recursively - we might ending in
  1818. an endless recursion requesting sense for failing request
  1819. sense transfers ...*/
  1820. memset (&sense_cmd, 0, sizeof (sense_cmd) );
  1821. memset (&sense_buffer, 0, sizeof (sense_buffer) );
  1822. sense_cmd.header.opc = AVISION_SCSI_REQUEST_SENSE;
  1823. sense_cmd.header.len = sizeof (sense_buffer);
  1824. count = sizeof(sense_cmd);
  1825. DBG (8, "try to write %lu bytes\n", (u_long) count);
  1826. sanei_usb_set_timeout (write_timeout);
  1827. status = sanei_usb_write_bulk (av_con->usb_dn,
  1828. (uint8_t*) &sense_cmd, &count);
  1829. DBG (8, "wrote %lu bytes\n", (u_long) count);
  1830. if (status != SANE_STATUS_GOOD) {
  1831. DBG (3, "=== Got error %d trying to request sense! ===\n", status);
  1832. }
  1833. else {
  1834. count = sizeof (sense_buffer);
  1835. DBG (8, "try to read %lu bytes sense data\n", (u_long) count);
  1836. sanei_usb_set_timeout (read_timeout);
  1837. status = sanei_usb_read_bulk(av_con->usb_dn, sense_buffer, &count);
  1838. DBG (8, "read %lu bytes sense data\n", (u_long) count);
  1839. /* we need to read out the staus from the scanner i/o buffer */
  1840. status = avision_usb_status (av_con, 1, status_timeout);
  1841. /* some scanner return NEED_SENSE even after reading it */
  1842. if (status != SANE_STATUS_GOOD && status != SANE_STATUS_INVAL)
  1843. DBG (3, "=== Got error %d trying to read sense! ===\n", status);
  1844. else {
  1845. /* read complete -> call our sense handler */
  1846. status = sense_handler (-1, sense_buffer, 0);
  1847. }
  1848. } /* end read sense data */
  1849. } /* end request sense */
  1850. return status;
  1851. } /* end cmd usb */
  1852. }
  1853. /* A bubble sort for the calibration. It only sorts the first third
  1854. * and returns an average of the top 2/3 values. The input data is
  1855. * 16bit big endian and the count is the count of the words - not
  1856. * bytes! */
  1857. static uint16_t
  1858. bubble_sort (uint8_t* sort_data, size_t count)
  1859. {
  1860. size_t i, j, limit, k;
  1861. double sum = 0.0;
  1862. limit = count / 3;
  1863. for (i = 0; i < limit; ++i)
  1864. {
  1865. uint16_t ti = 0;
  1866. uint16_t tj = 0;
  1867. for (j = (i + 1); j < count; ++j)
  1868. {
  1869. ti = get_double ((sort_data + i*2));
  1870. tj = get_double ((sort_data + j*2));
  1871. if (ti > tj) {
  1872. set_double ((sort_data + i*2), tj);
  1873. set_double ((sort_data + j*2), ti);
  1874. }
  1875. }
  1876. }
  1877. for (k = 0, i = limit; i < count; ++i) {
  1878. sum += get_double ((sort_data + i*2));
  1879. ++ k;
  1880. }
  1881. /* DBG (7, "bubble_sort: %d values for average\n", k); */
  1882. if (k > 0) /* if avg to compute */
  1883. return (uint16_t) (sum / k);
  1884. else
  1885. return (uint16_t) (sum); /* always zero? */
  1886. }
  1887. static SANE_Status
  1888. add_color_mode (Avision_Device* dev, color_mode mode, SANE_String name)
  1889. {
  1890. int i;
  1891. DBG (3, "add_color_mode: %d %s\n", mode, name);
  1892. for (i = 0; i < AV_COLOR_MODE_LAST; ++i)
  1893. {
  1894. if (dev->color_list [i] == 0) {
  1895. dev->color_list [i] = strdup (name);
  1896. dev->color_list_num [i] = mode;
  1897. return SANE_STATUS_GOOD;
  1898. }
  1899. }
  1900. DBG (3, "add_color_mode: failed\n");
  1901. return SANE_STATUS_NO_MEM;
  1902. }
  1903. static int
  1904. last_color_mode (Avision_Device* dev)
  1905. {
  1906. int i = 1;
  1907. while (dev->color_list [i] != 0 && i < AV_COLOR_MODE_LAST)
  1908. ++i;
  1909. /* we are off by one */
  1910. --i;
  1911. return i;
  1912. }
  1913. static color_mode
  1914. match_color_mode (Avision_Device* dev, SANE_String name)
  1915. {
  1916. int i;
  1917. DBG (3, "match_color_mode:\n");
  1918. for (i = 0; i < AV_COLOR_MODE_LAST; ++i)
  1919. {
  1920. if (dev->color_list [i] != 0 && strcmp (dev->color_list [i], name) == 0) {
  1921. DBG (3, "match_color_mode: found at %d mode: %d\n",
  1922. i, dev->color_list_num [i]);
  1923. return dev->color_list_num [i];
  1924. }
  1925. }
  1926. DBG (3, "match_color_mode: source mode invalid\n");
  1927. return AV_GRAYSCALE;
  1928. }
  1929. static SANE_Bool
  1930. color_mode_is_shaded (color_mode mode)
  1931. {
  1932. return mode >= AV_GRAYSCALE;
  1933. }
  1934. static SANE_Bool
  1935. color_mode_is_color (color_mode mode)
  1936. {
  1937. return mode >= AV_TRUECOLOR;
  1938. }
  1939. static SANE_Bool
  1940. is_adf_scan (Avision_Scanner* s)
  1941. {
  1942. return s->hw->scanner_type == AV_SHEETFEED || (s->hw->scanner_type == AV_FLATBED && s->source_mode_dim == AV_ADF_DIM);
  1943. }
  1944. static SANE_Status
  1945. add_source_mode (Avision_Device* dev, source_mode mode, SANE_String name)
  1946. {
  1947. int i;
  1948. for (i = 0; i < AV_SOURCE_MODE_LAST; ++i)
  1949. {
  1950. if (dev->source_list [i] == 0) {
  1951. dev->source_list [i] = strdup (name);
  1952. dev->source_list_num [i] = mode;
  1953. return SANE_STATUS_GOOD;
  1954. }
  1955. }
  1956. return SANE_STATUS_NO_MEM;
  1957. }
  1958. static source_mode
  1959. match_source_mode (Avision_Device* dev, SANE_String name)
  1960. {
  1961. int i;
  1962. DBG (3, "match_source_mode: \"%s\"\n", name);
  1963. for (i = 0; i < AV_SOURCE_MODE_LAST; ++i)
  1964. {
  1965. if (dev->source_list [i] != 0 && strcmp (dev->source_list [i], name) == 0) {
  1966. DBG (3, "match_source_mode: found at %d mode: %d\n",
  1967. i, dev->source_list_num [i]);
  1968. return dev->source_list_num [i];
  1969. }
  1970. }
  1971. DBG (3, "match_source_mode: source mode invalid\n");
  1972. return AV_NORMAL;
  1973. }
  1974. static source_mode_dim
  1975. match_source_mode_dim (source_mode sm)
  1976. {
  1977. DBG (3, "match_source_mode_dim: %d\n", sm);
  1978. switch (sm) {
  1979. case AV_NORMAL:
  1980. return AV_NORMAL_DIM;
  1981. case AV_TRANSPARENT:
  1982. return AV_TRANSPARENT_DIM;
  1983. case AV_ADF:
  1984. case AV_ADF_REAR:
  1985. case AV_ADF_DUPLEX:
  1986. return AV_ADF_DIM;
  1987. default:
  1988. DBG (3, "match_source_mode_dim: source mode invalid\n");
  1989. return AV_NORMAL_DIM;
  1990. }
  1991. }
  1992. static int
  1993. get_pixel_boundary (Avision_Scanner* s)
  1994. {
  1995. Avision_Device* dev = s->hw;
  1996. int boundary;
  1997. switch (s->c_mode) {
  1998. case AV_TRUECOLOR:
  1999. case AV_TRUECOLOR12:
  2000. case AV_TRUECOLOR16:
  2001. boundary = dev->inquiry_color_boundary;
  2002. break;
  2003. case AV_GRAYSCALE:
  2004. case AV_GRAYSCALE12:
  2005. case AV_GRAYSCALE16:
  2006. boundary = dev->inquiry_gray_boundary;
  2007. break;
  2008. case AV_DITHERED:
  2009. if (dev->inquiry_asic_type != AV_ASIC_C5)
  2010. boundary = 32;
  2011. else
  2012. boundary = dev->inquiry_dithered_boundary;
  2013. break;
  2014. case AV_THRESHOLDED:
  2015. if (dev->inquiry_asic_type != AV_ASIC_C5)
  2016. boundary = 32;
  2017. else
  2018. boundary = dev->inquiry_thresholded_boundary;
  2019. break;
  2020. default:
  2021. boundary = 8;
  2022. }
  2023. return boundary;
  2024. }
  2025. static SANE_Status
  2026. compute_parameters (Avision_Scanner* s)
  2027. {
  2028. Avision_Device* dev = s->hw;
  2029. int boundary = get_pixel_boundary (s);
  2030. SANE_Bool gray_mode = color_mode_is_shaded (s->c_mode);
  2031. /* interlaced duplex (higher end) or flipping paper (HP8xxx)? */
  2032. s->avdimen.interlaced_duplex = s->source_mode == AV_ADF_DUPLEX &&
  2033. dev->inquiry_duplex_interlaced;
  2034. /* for infra-red we use the same code path es for interlaced
  2035. duplex */
  2036. if (s->val[OPT_IR].w)
  2037. s->avdimen.interlaced_duplex = 1;
  2038. #ifdef AVISION_ENHANCED_SANE
  2039. /* quick fix for Microsoft Office Products ... */
  2040. switch (s->c_mode)
  2041. {
  2042. case AV_THRESHOLDED:
  2043. case AV_DITHERED:
  2044. /* our backend already has this restriction - so this line is for
  2045. documentation purposes only */
  2046. boundary = boundary > 32 ? boundary : 32;
  2047. break;
  2048. case AV_GRAYSCALE:
  2049. case AV_GRAYSCALE12:
  2050. case AV_GRAYSCALE16:
  2051. boundary = boundary > 4 ? boundary : 4;
  2052. break;
  2053. case AV_TRUECOLOR:
  2054. case AV_TRUECOLOR12:
  2055. case AV_TRUECOLOR16:
  2056. /* 12 bytes for 24bit color - 48bit is untested w/ Office */
  2057. boundary = boundary > 4 ? boundary : 4;
  2058. break;
  2059. }
  2060. #endif
  2061. DBG (3, "sane_compute_parameters:\n");
  2062. DBG (3, "sane_compute_parameters: boundary %d, gray_mode: %d, \n",
  2063. boundary, gray_mode);
  2064. /* TODO: Implement different x/y resolutions support */
  2065. s->avdimen.xres = s->val[OPT_RESOLUTION].w;
  2066. s->avdimen.yres = s->val[OPT_RESOLUTION].w;
  2067. /* soft scale ? */
  2068. if (dev->hw->feature_type & AV_SOFT_SCALE) {
  2069. /* find supported hardware resolution */
  2070. const int* hw_res;
  2071. const int* hw_res_list =
  2072. dev->inquiry_asic_type == AV_ASIC_C5 ? hw_res_list_c5 : hw_res_list_generic;
  2073. for (hw_res = hw_res_list; *hw_res && *hw_res < s->avdimen.xres; ++hw_res)
  2074. /* just iterate */;
  2075. s->avdimen.hw_xres = *hw_res;
  2076. for (hw_res = hw_res_list; *hw_res && *hw_res < s->avdimen.yres; ++hw_res)
  2077. /* just iterate */;
  2078. s->avdimen.hw_yres = *hw_res;
  2079. DBG (3, "sane_compute_parameters: soft scale, hw res: %dx%d\n",
  2080. s->avdimen.hw_xres,
  2081. s->avdimen.hw_yres);
  2082. if (!s->avdimen.hw_xres || ! s->avdimen.hw_yres) {
  2083. DBG (1, "sane_compute_parameters: no matching HW res for: %dx%d\n",
  2084. s->avdimen.xres,
  2085. s->avdimen.yres);
  2086. return SANE_STATUS_INVAL;
  2087. }
  2088. }
  2089. else {
  2090. s->avdimen.hw_xres = s->val[OPT_RESOLUTION].w;
  2091. s->avdimen.hw_yres = s->val[OPT_RESOLUTION].w;
  2092. }
  2093. DBG (3, "sane_compute_parameters: tlx: %f, tly: %f, brx: %f, bry: %f\n",
  2094. SANE_UNFIX (s->val[OPT_TL_X].w), SANE_UNFIX (s->val[OPT_TL_Y].w),
  2095. SANE_UNFIX (s->val[OPT_BR_X].w), SANE_UNFIX (s->val[OPT_BR_Y].w));
  2096. /* window parameter in pixel */
  2097. s->avdimen.tlx = s->avdimen.hw_xres * SANE_UNFIX (s->val[OPT_TL_X].w)
  2098. / MM_PER_INCH;
  2099. s->avdimen.tly = s->avdimen.hw_yres * SANE_UNFIX (s->val[OPT_TL_Y].w)
  2100. / MM_PER_INCH;
  2101. s->avdimen.brx = s->avdimen.hw_xres * SANE_UNFIX (s->val[OPT_BR_X].w)
  2102. / MM_PER_INCH;
  2103. s->avdimen.bry = s->avdimen.hw_yres * SANE_UNFIX (s->val[OPT_BR_Y].w)
  2104. / MM_PER_INCH;
  2105. /* line difference */
  2106. if (color_mode_is_color (s->c_mode) &&
  2107. dev->inquiry_needs_software_colorpack &&
  2108. dev->inquiry_line_difference)
  2109. {
  2110. s->avdimen.line_difference =
  2111. (dev->inquiry_line_difference * s->avdimen.hw_yres) / dev->inquiry_optical_res;
  2112. s->avdimen.bry += 2 * s->avdimen.line_difference;
  2113. /* limit bry + line_difference to real scan boundary */
  2114. {
  2115. long y_max = dev->inquiry_y_ranges[s->source_mode_dim] *
  2116. s->avdimen.hw_yres / MM_PER_INCH;
  2117. DBG (3, "sane_compute_parameters: y_max: %ld, bry: %ld, line_difference: %d\n",
  2118. y_max, s->avdimen.bry, s->avdimen.line_difference);
  2119. if (s->avdimen.bry + 2 * s->avdimen.line_difference > y_max) {
  2120. DBG (1, "sane_compute_parameters: bry limitted!\n");
  2121. s->avdimen.bry = y_max - 2 * s->avdimen.line_difference;
  2122. }
  2123. }
  2124. } /* end if needs software colorpack */
  2125. else {
  2126. s->avdimen.line_difference = 0;
  2127. }
  2128. /* add overscan */
  2129. if (dev->inquiry_tune_scan_length && is_adf_scan (s)) {
  2130. /* some extra efford for precise rounding ... */
  2131. int overscan = (s->avdimen.hw_yres *
  2132. (SANE_UNFIX (s->val[OPT_OVERSCAN_TOP].w) +
  2133. SANE_UNFIX (s->val[OPT_OVERSCAN_BOTTOM].w)) + (MM_PER_INCH - 1)
  2134. ) / MM_PER_INCH;
  2135. DBG (3, "sane_compute_parameters: overscan lines: %d\n", overscan);
  2136. s->avdimen.bry += overscan;
  2137. }
  2138. /* rear offset compensation */
  2139. if (s->avdimen.interlaced_duplex && dev->hw->feature_type & AV_REAR_OFFSET) {
  2140. const double offset = 0.5; /* in current affected models 1/2 inch */
  2141. s->avdimen.rear_offset = (int) (offset * s->avdimen.hw_yres);
  2142. DBG (1, "sane_compute_parameters: rear_offset: %d!\n", s->avdimen.rear_offset);
  2143. /* we do not limit against the buttom-y here, as rear offset always
  2144. applies to ADF scans, only */
  2145. }
  2146. else {
  2147. s->avdimen.rear_offset = 0;
  2148. }
  2149. memset (&s->params, 0, sizeof (s->params));
  2150. s->avdimen.hw_pixels_per_line = (s->avdimen.brx - s->avdimen.tlx);
  2151. s->avdimen.hw_pixels_per_line -= s->avdimen.hw_pixels_per_line % boundary;
  2152. s->avdimen.hw_lines = (s->avdimen.bry - s->avdimen.tly -
  2153. 2 * s->avdimen.line_difference);
  2154. if (s->avdimen.interlaced_duplex && dev->scanner_type != AV_FILM)
  2155. s->avdimen.hw_lines -= s->avdimen.hw_lines % dev->read_stripe_size;
  2156. s->params.pixels_per_line = s->avdimen.hw_pixels_per_line * s->avdimen.xres / s->avdimen.hw_xres;
  2157. s->params.lines = s->avdimen.hw_lines * s->avdimen.xres / s->avdimen.hw_xres;
  2158. if (s->c_mode == AV_THRESHOLDED || s->c_mode == AV_DITHERED)
  2159. s->params.pixels_per_line -= s->params.pixels_per_line % 8;
  2160. debug_print_avdimen (1, "sane_compute_parameters", &s->avdimen);
  2161. switch (s->c_mode)
  2162. {
  2163. case AV_THRESHOLDED:
  2164. s->params.format = SANE_FRAME_GRAY;
  2165. s->avdimen.hw_bytes_per_line = s->avdimen.hw_pixels_per_line / 8;
  2166. s->params.bytes_per_line = s->params.pixels_per_line / 8;
  2167. s->params.depth = 1;
  2168. break;
  2169. case AV_DITHERED:
  2170. s->params.format = SANE_FRAME_GRAY;
  2171. s->avdimen.hw_bytes_per_line = s->avdimen.hw_pixels_per_line / 8;
  2172. s->params.bytes_per_line = s->params.pixels_per_line / 8;
  2173. s->params.depth = 1;
  2174. break;
  2175. case AV_GRAYSCALE:
  2176. s->params.format = SANE_FRAME_GRAY;
  2177. s->avdimen.hw_bytes_per_line = s->avdimen.hw_pixels_per_line;
  2178. s->params.bytes_per_line = s->params.pixels_per_line;
  2179. s->params.depth = 8;
  2180. break;
  2181. case AV_GRAYSCALE12:
  2182. case AV_GRAYSCALE16:
  2183. s->params.format = SANE_FRAME_GRAY;
  2184. s->avdimen.hw_bytes_per_line = s->avdimen.hw_pixels_per_line * 2;
  2185. s->params.bytes_per_line = s->params.pixels_per_line * 2;
  2186. s->params.depth = 16;
  2187. break;
  2188. case AV_TRUECOLOR:
  2189. s->params.format = SANE_FRAME_RGB;
  2190. s->avdimen.hw_bytes_per_line = s->avdimen.hw_pixels_per_line * 3;
  2191. s->params.bytes_per_line = s->params.pixels_per_line * 3;
  2192. s->params.depth = 8;
  2193. break;
  2194. case AV_TRUECOLOR12:
  2195. case AV_TRUECOLOR16:
  2196. s->params.format = SANE_FRAME_RGB;
  2197. s->avdimen.hw_bytes_per_line = s->avdimen.hw_pixels_per_line * 3 * 2;
  2198. s->params.bytes_per_line = s->params.pixels_per_line * 3 * 2;
  2199. s->params.depth = 16;
  2200. break;
  2201. default:
  2202. DBG (1, "Invalid mode. %d\n", s->c_mode);
  2203. return SANE_STATUS_INVAL;
  2204. } /* end switch */
  2205. s->params.last_frame = SANE_TRUE;
  2206. debug_print_params (1, "sane_compute_parameters", &s->params);
  2207. return SANE_STATUS_GOOD;
  2208. }
  2209. static SANE_Status
  2210. inquiry (Avision_Connection av_con, uint8_t* data, size_t len)
  2211. {
  2212. SANE_Status status;
  2213. command_header inquiry;
  2214. int try = 2;
  2215. DBG (3, "inquiry: length: %ld\n", (long)len);
  2216. memset (&inquiry, 0, sizeof(inquiry));
  2217. inquiry.opc = AVISION_SCSI_INQUIRY;
  2218. inquiry.len = len;
  2219. do {
  2220. size_t size = inquiry.len;
  2221. DBG (3, "inquiry: inquiring ...\n");
  2222. status = avision_cmd (&av_con, &inquiry, sizeof (inquiry), 0, 0,
  2223. data, &size);
  2224. if (status == SANE_STATUS_GOOD && size == inquiry.len)
  2225. break;
  2226. DBG (1, "inquiry: inquiry failed (%s)\n", sane_strstatus (status));
  2227. --try;
  2228. } while (try > 0);
  2229. return status;
  2230. }
  2231. static SANE_Status
  2232. wait_ready (Avision_Connection* av_con, int delay)
  2233. {
  2234. SANE_Status status;
  2235. int try;
  2236. for (try = 0; try < 10; ++ try)
  2237. {
  2238. DBG (3, "wait_ready: sending TEST_UNIT_READY\n");
  2239. status = avision_cmd (av_con, test_unit_ready, sizeof (test_unit_ready),
  2240. 0, 0, 0, 0);
  2241. sleep (delay);
  2242. switch (status)
  2243. {
  2244. default:
  2245. /* Ignore errors while waiting for scanner to become ready.
  2246. Some SCSI drivers return EIO while the scanner is
  2247. returning to the home position. */
  2248. DBG (1, "wait_ready: test unit ready failed (%s)\n",
  2249. sane_strstatus (status));
  2250. /* fall through */
  2251. case SANE_STATUS_DEVICE_BUSY:
  2252. break;
  2253. case SANE_STATUS_GOOD:
  2254. return status;
  2255. }
  2256. }
  2257. DBG (1, "wait_ready: timed out after %d attempts\n", try);
  2258. return SANE_STATUS_INVAL;
  2259. }
  2260. static SANE_Status
  2261. wait_4_light (Avision_Scanner* s)
  2262. {
  2263. Avision_Device* dev = s->hw;
  2264. /* read stuff */
  2265. struct command_read rcmd;
  2266. char* light_status[] =
  2267. { "off", "on", "warming up", "needs warm up test",
  2268. "light check error", "RESERVED" };
  2269. SANE_Status status;
  2270. uint8_t result;
  2271. int try;
  2272. size_t size = 1;
  2273. DBG (3, "wait_4_light: getting light status.\n");
  2274. memset (&rcmd, 0, sizeof (rcmd));
  2275. rcmd.opc = AVISION_SCSI_READ;
  2276. rcmd.datatypecode = 0xa0; /* get light status */
  2277. set_double (rcmd.datatypequal, dev->data_dq);
  2278. set_triple (rcmd.transferlen, size);
  2279. for (try = 0; try < 90; ++ try) {
  2280. DBG (5, "wait_4_light: read bytes %lu\n", (u_long) size);
  2281. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, &result, &size);
  2282. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2283. DBG (1, "wait_4_light: read failed (%s)\n", sane_strstatus (status));
  2284. return status;
  2285. }
  2286. DBG (3, "wait_4_light: command is %d. Result is %s\n",
  2287. status, light_status[(result>4)?5:result]);
  2288. if (result == 1) {
  2289. return SANE_STATUS_GOOD;
  2290. }
  2291. else if (dev->hw->feature_type & AV_LIGHT_CHECK_BOGUS) {
  2292. DBG (3, "wait_4_light: scanner marked as returning bogus values in device-list!!\n");
  2293. return SANE_STATUS_GOOD;
  2294. }
  2295. else {
  2296. struct command_send scmd;
  2297. uint8_t light_on = 1;
  2298. /* turn on the light */
  2299. DBG (3, "wait_4_light: setting light status.\n");
  2300. memset (&scmd, 0, sizeof (scmd));
  2301. scmd.opc = AVISION_SCSI_SEND;
  2302. scmd.datatypecode = 0xa0; /* send light status */
  2303. set_double (scmd.datatypequal, dev->data_dq);
  2304. set_triple (scmd.transferlen, size);
  2305. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  2306. &light_on, sizeof (light_on), 0, 0);
  2307. if (status != SANE_STATUS_GOOD) {
  2308. DBG (1, "wait_4_light: send failed (%s)\n", sane_strstatus (status));
  2309. return status;
  2310. }
  2311. }
  2312. sleep (1);
  2313. }
  2314. DBG (1, "wait_4_light: timed out after %d attempts\n", try);
  2315. return SANE_STATUS_DEVICE_BUSY;
  2316. }
  2317. static SANE_Status
  2318. set_power_save_time (Avision_Scanner* s, int time)
  2319. {
  2320. struct {
  2321. struct command_send cmd;
  2322. uint8_t time[2];
  2323. } scmd;
  2324. Avision_Device* dev = s->hw;
  2325. SANE_Status status;
  2326. DBG (3, "set_power_save_time: time %d\n", time);
  2327. memset (&scmd, 0, sizeof (scmd));
  2328. scmd.cmd.opc = AVISION_SCSI_SEND;
  2329. scmd.cmd.datatypecode = 0xA2; /* power-saving timer */
  2330. set_double (scmd.cmd.datatypequal, dev->data_dq);
  2331. set_triple (scmd.cmd.transferlen, sizeof (scmd.time) );
  2332. set_double (scmd.time, time);
  2333. status = avision_cmd (&s->av_con, &scmd.cmd, sizeof (scmd.cmd),
  2334. &scmd.time, sizeof (scmd.time), 0, 0);
  2335. if (status != SANE_STATUS_GOOD)
  2336. DBG (1, "set_power_save_time: send_data (%s)\n", sane_strstatus (status));
  2337. return status;
  2338. }
  2339. static SANE_Status
  2340. get_firmware_status (Avision_Connection* av_con)
  2341. {
  2342. /* read stuff */
  2343. struct command_read rcmd;
  2344. size_t size;
  2345. SANE_Status status;
  2346. firmware_status result;
  2347. DBG (3, "get_firmware_status\n");
  2348. size = sizeof (result);
  2349. memset (&rcmd, 0, sizeof (rcmd));
  2350. rcmd.opc = AVISION_SCSI_READ;
  2351. rcmd.datatypecode = 0x90; /* firmware status */
  2352. set_double (rcmd.datatypequal, 0); /* dev->data_dq not available */
  2353. set_triple (rcmd.transferlen, size);
  2354. status = avision_cmd (av_con, &rcmd, sizeof (rcmd), 0, 0, &result, &size);
  2355. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2356. DBG (1, "get_firmware_status: read failed (%s)\n",
  2357. sane_strstatus (status));
  2358. return (status);
  2359. }
  2360. debug_print_raw (6, "get_firmware_status: raw data:\n", (uint8_t*)&result, size);
  2361. DBG (3, "get_firmware_status: [0] needs firmware %x\n", result.download_firmware);
  2362. DBG (3, "get_firmware_status: [1] side edge: %d\n", get_double ( result.first_effective_pixel_flatbed ));
  2363. DBG (3, "get_firmware_status: [3] side edge: %d\n", get_double ( result.first_effective_pixel_adf_front ));
  2364. DBG (3, "get_firmware_status: [5] side edge: %d\n", get_double ( result.first_effective_pixel_adf_rear ));
  2365. return SANE_STATUS_GOOD;
  2366. }
  2367. static SANE_Status
  2368. get_flash_ram_info (Avision_Connection* av_con)
  2369. {
  2370. /* read stuff */
  2371. struct command_read rcmd;
  2372. size_t size;
  2373. SANE_Status status;
  2374. uint8_t result[40];
  2375. DBG (3, "get_flash_ram_info\n");
  2376. size = sizeof (result);
  2377. memset (&rcmd, 0, sizeof (rcmd));
  2378. rcmd.opc = AVISION_SCSI_READ;
  2379. rcmd.datatypecode = 0x6a; /* flash ram information */
  2380. set_double (rcmd.datatypequal, 0); /* dev->data_dq not available */
  2381. set_triple (rcmd.transferlen, size);
  2382. status = avision_cmd (av_con, &rcmd, sizeof (rcmd), 0, 0, result, &size);
  2383. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2384. DBG (1, "get_flash_ram_info: read failed (%s)\n",
  2385. sane_strstatus (status));
  2386. return (status);
  2387. }
  2388. debug_print_raw (6, "get_flash_ram_info: raw data:\n", result, size);
  2389. DBG (3, "get_flash_ram_info: [0] data type %x\n", result [0]);
  2390. DBG (3, "get_flash_ram_info: [1] Ability1:%s%s%s%s%s%s%s%s\n",
  2391. BIT(result[1],7)?" RESERVED_BIT7":"",
  2392. BIT(result[1],6)?" RESERVED_BIT6":"",
  2393. BIT(result[1],5)?" FONT(r/w)":"",
  2394. BIT(result[1],4)?" FPGA(w)":"",
  2395. BIT(result[1],3)?" FMDBG(r)":"",
  2396. BIT(result[1],2)?" RAWLINE(r)":"",
  2397. BIT(result[1],1)?" FIRMWARE(r/w)":"",
  2398. BIT(result[1],0)?" CTAB(r/w)":"");
  2399. DBG (3, "get_flash_ram_info: [2-5] size CTAB: %d\n",
  2400. get_quad ( &(result[2]) ) );
  2401. DBG (3, "get_flash_ram_info: [6-9] size FIRMWARE: %d\n",
  2402. get_quad ( &(result[6]) ) );
  2403. DBG (3, "get_flash_ram_info: [10-13] size RAWLINE: %d\n",
  2404. get_quad ( &(result[10]) ) );
  2405. DBG (3, "get_flash_ram_info: [14-17] size FMDBG: %d\n",
  2406. get_quad ( &(result[14]) ) );
  2407. DBG (3, "get_flash_ram_info: [18-21] size FPGA: %d\n",
  2408. get_quad ( &(result[18]) ) );
  2409. DBG (3, "get_flash_ram_info: [22-25] size FONT: %d\n",
  2410. get_quad ( &(result[22]) ) );
  2411. DBG (3, "get_flash_ram_info: [26-29] size RESERVED: %d\n",
  2412. get_quad ( &(result[26]) ) );
  2413. DBG (3, "get_flash_ram_info: [30-33] size RESERVED: %d\n",
  2414. get_quad ( &(result[30]) ) );
  2415. return SANE_STATUS_GOOD;
  2416. }
  2417. static SANE_Status
  2418. get_nvram_data (Avision_Scanner* s, nvram_data* nvram)
  2419. {
  2420. /* read stuff */
  2421. struct command_send rcmd;
  2422. size_t size;
  2423. SANE_Status status;
  2424. DBG (3, "get_nvram_data\n");
  2425. size = sizeof (*nvram);
  2426. memset (&rcmd, 0, sizeof (rcmd));
  2427. memset (nvram, 0, size);
  2428. rcmd.opc = AVISION_SCSI_READ;
  2429. rcmd.datatypecode = 0x69; /* Read NVM RAM data */
  2430. set_double (rcmd.datatypequal, 0); /* dev->data_dq not available */
  2431. set_triple (rcmd.transferlen, size);
  2432. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0,
  2433. nvram, &size);
  2434. if (status != SANE_STATUS_GOOD) {
  2435. DBG (1, "get_nvram_data: read failed (%s)\n",
  2436. sane_strstatus (status));
  2437. return (status);
  2438. }
  2439. debug_print_nvram_data (5, "get_nvram_data", nvram);
  2440. return SANE_STATUS_GOOD;
  2441. }
  2442. static SANE_Status
  2443. get_and_parse_nvram (Avision_Scanner* s, char* str, int n)
  2444. {
  2445. SANE_Status status;
  2446. int i = 0;
  2447. int x;
  2448. nvram_data nvram;
  2449. uint8_t inquiry_result [AVISION_INQUIRY_SIZE_V1];
  2450. status = inquiry (s->av_con, inquiry_result, sizeof(inquiry_result));
  2451. if (status == SANE_STATUS_GOOD) {
  2452. i += snprintf (str+i, n-i, "Vendor: %.8s",
  2453. inquiry_result+8);
  2454. i += snprintf (str+i, n-i, "\nModel: %.16s",
  2455. inquiry_result+16);
  2456. i += snprintf (str+i, n-i, "\nFirmware: %.4s",
  2457. inquiry_result+32);
  2458. }
  2459. if (!s->hw->inquiry_nvram_read)
  2460. return SANE_STATUS_GOOD;
  2461. status = get_nvram_data (s, &nvram);
  2462. if (status == SANE_STATUS_GOOD)
  2463. {
  2464. if (nvram.serial[0])
  2465. i += snprintf (str+i, n-i, "\nSerial: %.24s",
  2466. nvram.serial);
  2467. if (nvram.born_year)
  2468. i += snprintf (str+i, n-i, "\nManufacturing date: %d-%d-%d",
  2469. get_double(nvram.born_year),
  2470. get_double(nvram.born_month),
  2471. get_double(nvram.born_day));
  2472. if (nvram.first_scan_year)
  2473. i += snprintf (str+i, n-i, "\nFirst scan date: %d-%d-%d",
  2474. get_double(nvram.first_scan_year),
  2475. get_double(nvram.first_scan_month),
  2476. get_double(nvram.first_scan_day));
  2477. x = get_quad (nvram.flatbed_scans);
  2478. if (x)
  2479. i += snprintf (str+i, n-i, "\nFlatbed scans: %d", x);
  2480. x = get_quad (nvram.pad_scans);
  2481. if (x)
  2482. i += snprintf (str+i, n-i, "\nPad scans: %d", x);
  2483. x = get_quad (nvram.adf_simplex_scans);
  2484. if (x)
  2485. i += snprintf (str+i, n-i, "\nADF simplex scans: %d", x);
  2486. x = get_quad (nvram.adf_duplex_scans);
  2487. if (x)
  2488. i += snprintf (str+i, n-i, "\nADF duplex scans: %d", x);
  2489. }
  2490. return status;
  2491. }
  2492. static SANE_Status
  2493. get_power_save_time (Avision_Scanner* s, SANE_Word* time)
  2494. {
  2495. SANE_Status status;
  2496. nvram_data nvram;
  2497. DBG (3, "get_power_save_time\n");
  2498. if (!s->hw->inquiry_nvram_read)
  2499. return SANE_STATUS_INVAL;
  2500. status = get_nvram_data (s, &nvram);
  2501. if (status != SANE_STATUS_GOOD) {
  2502. DBG (1, "get_power_save_time: read nvram failed (%s)\n", sane_strstatus (status));
  2503. return status;
  2504. }
  2505. *time = get_double (nvram.power_saving_time);
  2506. return SANE_STATUS_GOOD;
  2507. }
  2508. #ifdef NEEDED
  2509. static SANE_Status
  2510. send_nvram_data (Avision_Connection* av_con)
  2511. {
  2512. /* read stuff */
  2513. struct command_send scmd;
  2514. size_t size;
  2515. SANE_Status status;
  2516. DBG (3, "send_nvram_data\n");
  2517. size = sizeof (c7_nvram);
  2518. memset (&scmd, 0, sizeof (scmd));
  2519. scmd.opc = AVISION_SCSI_SEND;
  2520. scmd.datatypecode = 0x85; /* nvram data */
  2521. set_double (scmd.datatypequal, 0); /* dev->data_dq not available */
  2522. set_triple (scmd.transferlen, size);
  2523. status = avision_cmd (av_con, &scmd, sizeof (scmd), &c7_nvram, size,
  2524. 0, 0);
  2525. if (status != SANE_STATUS_GOOD) {
  2526. DBG (1, "send_nvram_data: send failed (%s)\n",
  2527. sane_strstatus (status));
  2528. return (status);
  2529. }
  2530. return SANE_STATUS_GOOD;
  2531. }
  2532. static SANE_Status
  2533. send_flash_ram_data (Avision_Connection* av_con)
  2534. {
  2535. /* read stuff */
  2536. struct command_send scmd;
  2537. size_t size;
  2538. SANE_Status status;
  2539. DBG (3, "send_flash_ram_data\n");
  2540. size = sizeof (c7_flash_ram);
  2541. memset (&scmd, 0, sizeof (scmd));
  2542. scmd.opc = AVISION_SCSI_SEND;
  2543. scmd.datatypecode = 0x86; /* flash data */
  2544. set_double (scmd.datatypequal, 0);
  2545. set_triple (scmd.transferlen, size);
  2546. status = avision_cmd (av_con, &scmd, sizeof (scmd), &c7_flash_ram, size,
  2547. 0, 0);
  2548. if (status != SANE_STATUS_GOOD) {
  2549. DBG (1, "send_flash_ram_data: send failed (%s)\n",
  2550. sane_strstatus (status));
  2551. return (status);
  2552. }
  2553. return SANE_STATUS_GOOD;
  2554. }
  2555. #endif
  2556. static SANE_Status
  2557. get_accessories_info (Avision_Scanner* s)
  2558. {
  2559. Avision_Device* dev = s->hw;
  2560. /* read stuff */
  2561. struct command_read rcmd;
  2562. size_t size;
  2563. SANE_Status status;
  2564. uint8_t result[8];
  2565. char* adf_model[] =
  2566. { "Origami", "Oodles", "HP9930", "unknown" };
  2567. const int adf_models = sizeof (adf_model) / sizeof(char*) - 1;
  2568. DBG (3, "get_accessories_info\n");
  2569. size = sizeof (result);
  2570. memset (&rcmd, 0, sizeof (rcmd));
  2571. rcmd.opc = AVISION_SCSI_READ;
  2572. rcmd.datatypecode = 0x64; /* detect accessories */
  2573. set_double (rcmd.datatypequal, dev->data_dq);
  2574. set_triple (rcmd.transferlen, size);
  2575. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, result, &size);
  2576. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2577. DBG (1, "get_accessories_info: read failed (%s)\n",
  2578. sane_strstatus (status));
  2579. return (status);
  2580. }
  2581. debug_print_raw (6, "get_accessories_info: raw data:\n", result, size);
  2582. DBG (3, "get_accessories_info: [0] ADF: %x\n", result[0]);
  2583. DBG (3, "get_accessories_info: [1] Light Box: %x\n", result[1]);
  2584. DBG (3, "get_accessories_info: [2] ADF model: %d (%s)\n",
  2585. result [2],
  2586. adf_model[ (result[2] < adf_models) ? result[2] : adf_models ]);
  2587. dev->inquiry_adf |= result [0];
  2588. if (result [2] == 2) /* HP */
  2589. {
  2590. dev->inquiry_duplex = 1;
  2591. dev->inquiry_duplex_interlaced = 0;
  2592. dev->inquiry_adf_need_mirror_rear = 1;
  2593. }
  2594. /* only honor a 1, some scanner without adapter set 0xff */
  2595. if (result[1] == 1)
  2596. dev->inquiry_light_box = 1;
  2597. return SANE_STATUS_GOOD;
  2598. }
  2599. /* Returns a pointer to static char* strings or NULL for cancel (we do
  2600. not want to start memcmp'ing for the cancel case). */
  2601. static const char*
  2602. string_for_button (Avision_Scanner* s, int button)
  2603. {
  2604. static char buffer [16];
  2605. Avision_Device* dev = s->hw;
  2606. /* dev->sane.model
  2607. dev->inquiry_asic_type */
  2608. if (dev->inquiry_buttons == 1)
  2609. goto return_scan;
  2610. /* simplex / duplex buttons */
  2611. if (strcmp (dev->sane.vendor, "Xerox") == 0 ||
  2612. strcmp (dev->sane.vendor, "Visioneer") == 0 ||
  2613. strcmp (dev->sane.model, "AV121") == 0 ||
  2614. strcmp (dev->sane.model, "AV122") == 0
  2615. )
  2616. {
  2617. switch (button)
  2618. {
  2619. case 1: return "simplex";
  2620. case 2: return "duplex";
  2621. }
  2622. }
  2623. if (strcmp (dev->sane.model, "AV210C2") == 0 ||
  2624. strcmp (dev->sane.model, "AV220C2") == 0 ||
  2625. strcmp (dev->sane.model, "AV610C2") == 0
  2626. )
  2627. {
  2628. if (button == 1)
  2629. return NULL; /* cancel */
  2630. else
  2631. goto return_scan;
  2632. }
  2633. /* those are uniqe, right now */
  2634. if (strcmp (dev->sane.model, "AV610") == 0)
  2635. {
  2636. switch (button)
  2637. {
  2638. case 0: return "email";
  2639. case 1: return "copy";
  2640. case 2: return "scan";
  2641. }
  2642. }
  2643. /* last resort */
  2644. snprintf (buffer, sizeof (buffer), "button%d", button);
  2645. return buffer;
  2646. return_scan:
  2647. return "scan";
  2648. }
  2649. static SANE_Status
  2650. get_button_status (Avision_Scanner* s)
  2651. {
  2652. Avision_Device* dev = s->hw;
  2653. /* read stuff */
  2654. struct command_read rcmd;
  2655. size_t size;
  2656. SANE_Status status;
  2657. /* was only 6 in an old SPEC - maybe we need a feature override :-( -ReneR */
  2658. struct {
  2659. uint8_t press_state;
  2660. uint8_t buttons[5];
  2661. uint8_t display; /* AV220 et.al. 7 segment LED display */
  2662. uint8_t reserved[9];
  2663. } result;
  2664. unsigned int i;
  2665. DBG (3, "get_button_status:\n");
  2666. size = sizeof (result);
  2667. /* AV220 et.al. */
  2668. if (! (dev->hw->feature_type & AV_INT_BUTTON))
  2669. {
  2670. memset (&rcmd, 0, sizeof (rcmd));
  2671. rcmd.opc = AVISION_SCSI_READ;
  2672. rcmd.datatypecode = 0xA1; /* button status */
  2673. set_double (rcmd.datatypequal, dev->data_dq);
  2674. set_triple (rcmd.transferlen, size);
  2675. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0,
  2676. (uint8_t*)&result, &size);
  2677. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2678. DBG (1, "get_button_status: read failed (%s)\n", sane_strstatus (status));
  2679. return status;
  2680. }
  2681. }
  2682. else
  2683. {
  2684. /* only try to read the first 8 bytes ...*/
  2685. size = 8;
  2686. /* no SCSI equivalent */
  2687. /* either there was a button press and this completes quickly
  2688. or there is no point waiting for a future press */
  2689. sanei_usb_set_timeout (100); /* 10th of a second */
  2690. DBG (5, "==> (interrupt read) going down ...\n");
  2691. status = sanei_usb_read_int (s->av_con.usb_dn, (uint8_t*)&result,
  2692. &size);
  2693. DBG (5, "==> (interrupt read) got: %ld\n", (long)size);
  2694. if (status != SANE_STATUS_GOOD) {
  2695. DBG (1, "get_button_status: interrupt read failed (%s)\n",
  2696. sane_strstatus (status));
  2697. return SANE_STATUS_GOOD;
  2698. }
  2699. if (size < sizeof (result))
  2700. memset ((char*)result.buttons + size, 0, sizeof (result) - size);
  2701. /* hack to fill in meaningful values for the AV 210 / 610 and
  2702. under some conditions the AV 220 */
  2703. if (size == 1) { /* AV 210, AV 610 */
  2704. DBG (1, "get_button_status: just one byte, filling the rest\n");
  2705. if (result.press_state > 0) {
  2706. debug_print_raw (6, "get_button_status: raw data\n",
  2707. (uint8_t*)&result, size);
  2708. result.buttons[0] = result.press_state;
  2709. result.press_state = 0x80 | 1;
  2710. size = 2;
  2711. }
  2712. else /* nothing pressed */
  2713. return SANE_STATUS_GOOD;
  2714. }
  2715. else if (size >= 8 && result.press_state == 0) { /* AV 220 */
  2716. debug_print_raw (6, "get_button_status: raw data\n",
  2717. (uint8_t*)&result, size);
  2718. DBG (1, "get_button_status: zero buttons - filling values ...\n");
  2719. /* simulate button press of the last button ... */
  2720. result.press_state = 0x80 | 1;
  2721. result.buttons[0] = dev->inquiry_buttons; /* 1 based */
  2722. }
  2723. }
  2724. debug_print_raw (6, "get_button_status: raw data\n",
  2725. (uint8_t*)&result, size);
  2726. DBG (3, "get_button_status: [0] Button status: %x\n", result.press_state);
  2727. for (i = 0; i < 5; ++i)
  2728. DBG (3, "get_button_status: [%d] Button number %d: %x\n", i+1, i,
  2729. result.buttons[i]);
  2730. DBG (3, "get_button_status: [7] Display: %d\n", result.display);
  2731. {
  2732. char* message_begin = s->val[OPT_MESSAGE].s;
  2733. char* message_end = s->val[OPT_MESSAGE].s + s->opt[OPT_MESSAGE].size;
  2734. char* message = message_begin;
  2735. #define add_token(format,value) do { \
  2736. int n = snprintf (message, message_end - message, "%s" format, \
  2737. message == message_begin ? "" : ":", value); \
  2738. message += n > 0 ? n : 0; \
  2739. } while (0)
  2740. if (result.display > 0)
  2741. add_token ("%d", result.display);
  2742. if (result.press_state >> 7) /* AV220 et.al. bit 6 is long/short press? */
  2743. {
  2744. const unsigned int buttons_pressed = result.press_state & 0x7F;
  2745. DBG (3, "get_button_status: %d button(s) pressed\n", buttons_pressed);
  2746. /* reset the hardware button status */
  2747. if (! (dev->hw->feature_type & AV_INT_BUTTON))
  2748. {
  2749. struct command_send scmd;
  2750. uint8_t button_reset = 1;
  2751. DBG (3, "get_button_status: resetting status\n");
  2752. memset (&scmd, 0, sizeof (scmd));
  2753. scmd.opc = AVISION_SCSI_SEND;
  2754. scmd.datatypecode = 0xA1; /* button control */
  2755. set_double (scmd.datatypequal, dev->data_dq);
  2756. set_triple (scmd.transferlen, size);
  2757. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  2758. &button_reset, sizeof (button_reset), 0, 0);
  2759. if (status != SANE_STATUS_GOOD) {
  2760. DBG (1, "get_button_status: send failed (%s)\n",
  2761. sane_strstatus (status));
  2762. return status;
  2763. }
  2764. }
  2765. for (i = 0; i < buttons_pressed; ++i) {
  2766. const unsigned int button = result.buttons[i] - 1; /* 1 based ... */
  2767. DBG (3, "get_button_status: button %d pressed\n", button);
  2768. if (button >= dev->inquiry_buttons) {
  2769. DBG (1, "get_button_status: button %d not allocated as not indicated in inquiry\n",
  2770. button);
  2771. }
  2772. else {
  2773. const char* label = string_for_button (s, button);
  2774. if (label)
  2775. add_token ("%s", label);
  2776. else
  2777. return SANE_STATUS_CANCELLED;
  2778. }
  2779. }
  2780. }
  2781. else
  2782. DBG (3, "get_button_status: no button pressed\n");
  2783. }
  2784. return SANE_STATUS_GOOD;
  2785. #undef add_token
  2786. }
  2787. static SANE_Status
  2788. get_frame_info (Avision_Scanner* s)
  2789. {
  2790. Avision_Device* dev = s->hw;
  2791. /* read stuff */
  2792. struct command_read rcmd;
  2793. size_t size;
  2794. SANE_Status status;
  2795. uint8_t result[8];
  2796. size_t i;
  2797. DBG (3, "get_frame_info:\n");
  2798. size = sizeof (result);
  2799. memset (&rcmd, 0, sizeof (rcmd));
  2800. rcmd.opc = AVISION_SCSI_READ;
  2801. rcmd.datatypecode = 0x87; /* film holder sense */
  2802. set_double (rcmd.datatypequal, dev->data_dq);
  2803. set_triple (rcmd.transferlen, size);
  2804. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, result, &size);
  2805. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2806. DBG (1, "get_frame_info: read failed (%s)\n", sane_strstatus (status));
  2807. return (status);
  2808. }
  2809. debug_print_raw (6, "get_frame_info: raw data\n", result, size);
  2810. DBG (3, "get_frame_info: [0] Holder type: %s\n",
  2811. (result[0]==1)?"APS":
  2812. (result[0]==2)?"Film holder (35mm)":
  2813. (result[0]==3)?"Slide holder":
  2814. (result[0]==0xff)?"Empty":"unknown");
  2815. DBG (3, "get_frame_info: [1] Current frame number: %d\n", result[1]);
  2816. DBG (3, "get_frame_info: [2] Frame ammount: %d\n", result[2]);
  2817. DBG (3, "get_frame_info: [3] Mode: %s\n", BIT(result[3],4)?"APS":"Not APS");
  2818. DBG (3, "get_frame_info: [3] Exposures (if APS): %s\n",
  2819. ((i=(BIT(result[3],3)<<1)+BIT(result[2],2))==0)?"Unknown":
  2820. (i==1)?"15":(i==2)?"25":"40");
  2821. DBG (3, "get_frame_info: [3] Film Type (if APS): %s\n",
  2822. ((i=(BIT(result[1],3)<<1)+BIT(result[0],2))==0)?"Unknown":
  2823. (i==1)?"B&W Negative":(i==2)?"Color slide":"Color Negative");
  2824. dev->holder_type = result[0];
  2825. dev->current_frame = result[1];
  2826. dev->frame_range.min = 1;
  2827. dev->frame_range.quant = 1;
  2828. if (result[0] != 0xff)
  2829. dev->frame_range.max = result[2];
  2830. else
  2831. dev->frame_range.max = 1;
  2832. return SANE_STATUS_GOOD;
  2833. }
  2834. static SANE_Status
  2835. get_duplex_info (Avision_Scanner* s)
  2836. {
  2837. Avision_Device* dev = s->hw;
  2838. /* read stuff */
  2839. struct command_read rcmd;
  2840. struct {
  2841. uint8_t mode;
  2842. uint8_t color_line_difference[2];
  2843. uint8_t gray_line_difference[2];
  2844. uint8_t lineart_line_difference[2];
  2845. uint8_t image_info;
  2846. } result;
  2847. size_t size;
  2848. SANE_Status status;
  2849. DBG (3, "get_duplex_info:\n");
  2850. size = sizeof (result);
  2851. memset (&rcmd, 0, sizeof (rcmd));
  2852. rcmd.opc = AVISION_SCSI_READ;
  2853. rcmd.datatypecode = 0xB1; /* read duplex info */
  2854. set_double (rcmd.datatypequal, dev->data_dq);
  2855. set_triple (rcmd.transferlen, size);
  2856. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0,
  2857. &result, &size);
  2858. if (status != SANE_STATUS_GOOD || size != sizeof (result)) {
  2859. DBG (1, "get_duplex_info: read failed (%s)\n", sane_strstatus (status));
  2860. return (status);
  2861. }
  2862. debug_print_raw (6, "get_duplex_info: raw data\n", (uint8_t*)&result, size);
  2863. DBG (3, "get_duplex_info: [0] Mode: %s%s\n",
  2864. BIT(result.mode,0)?"MERGED_PAGES":"",
  2865. BIT(result.mode,1)?"2ND_PAGE_FOLLOWS":"");
  2866. DBG (3, "get_duplex_info: [1-2] Color line difference: %d\n",
  2867. get_double(result.color_line_difference));
  2868. DBG (3, "get_duplex_info: [3-4] Gray line difference: %d\n",
  2869. get_double(result.gray_line_difference));
  2870. DBG (3, "get_duplex_info: [5-6] Lineart line difference: %d\n",
  2871. get_double(result.lineart_line_difference));
  2872. /* isn't this supposed to be result.info ?!? */
  2873. DBG (3, "get_duplex_info: [7] Mode: %s%s%s%s\n",
  2874. BIT(result.image_info,0)?" FLATBED_BGR":" FLATBED_RGB",
  2875. BIT(result.image_info,1)?" ADF_BGR":" ADF_RGB",
  2876. BIT(result.image_info,2)?" FLATBED_NEEDS_MIRROR_IMAGE":"",
  2877. BIT(result.image_info,3)?" ADF_NEEDS_MIRROR_IMAGE":"");
  2878. return SANE_STATUS_GOOD;
  2879. }
  2880. static SANE_Status
  2881. set_frame (Avision_Scanner* s, SANE_Word frame)
  2882. {
  2883. struct {
  2884. struct command_send cmd;
  2885. uint8_t data[8];
  2886. } scmd;
  2887. Avision_Device* dev = s->hw;
  2888. SANE_Status status;
  2889. DBG (3, "set_frame: request frame %d\n", frame);
  2890. /* Better check the current status of the film holder, because it
  2891. can be changed between scans. */
  2892. status = get_frame_info (s);
  2893. if (status != SANE_STATUS_GOOD)
  2894. return status;
  2895. /* No film holder? */
  2896. if (dev->holder_type == 0xff) {
  2897. DBG (1, "set_frame: No film holder!!\n");
  2898. return SANE_STATUS_INVAL;
  2899. }
  2900. /* Requesting frame 0xff indicates eject/rewind */
  2901. if (frame != 0xff && (frame < 1 || frame > dev->frame_range.max) ) {
  2902. DBG (1, "set_frame: Illegal frame (%d) requested (min=1, max=%d)\n",
  2903. frame, dev->frame_range.max);
  2904. return SANE_STATUS_INVAL;
  2905. }
  2906. memset (&scmd, 0, sizeof (scmd));
  2907. scmd.cmd.opc = AVISION_SCSI_SEND;
  2908. scmd.cmd.datatypecode = 0x87; /* send film holder "sense" */
  2909. set_double (scmd.cmd.datatypequal, dev->data_dq);
  2910. set_triple (scmd.cmd.transferlen, sizeof (scmd.data) );
  2911. scmd.data[0] = dev->holder_type;
  2912. scmd.data[1] = frame;
  2913. status = avision_cmd (&s->av_con, &scmd.cmd, sizeof (scmd.cmd),
  2914. &scmd.data, sizeof (scmd.data), 0, 0);
  2915. if (status != SANE_STATUS_GOOD) {
  2916. DBG (1, "set_frame: send_data (%s)\n", sane_strstatus (status));
  2917. }
  2918. return status;
  2919. }
  2920. static SANE_Status
  2921. attach (SANE_String_Const devname, Avision_ConnectionType con_type,
  2922. Avision_Device** devp)
  2923. {
  2924. uint8_t result [AVISION_INQUIRY_SIZE_MAX];
  2925. int model_num;
  2926. Avision_Device* dev;
  2927. SANE_Status status;
  2928. Avision_Connection av_con;
  2929. char mfg [9];
  2930. char model [17];
  2931. char rev [5];
  2932. unsigned int i;
  2933. char* s;
  2934. SANE_Bool found;
  2935. DBG (3, "attach:\n");
  2936. memset (result, 0, sizeof(result));
  2937. for (dev = first_dev; dev; dev = dev->next)
  2938. if (strcmp (dev->sane.name, devname) == 0) {
  2939. if (devp)
  2940. *devp = dev;
  2941. return SANE_STATUS_GOOD;
  2942. }
  2943. av_con.connection_type = con_type;
  2944. if (av_con.connection_type == AV_USB)
  2945. av_con.usb_status = AVISION_USB_UNTESTED_STATUS;
  2946. /* set known USB status type */
  2947. if (attaching_hw && attaching_hw->feature_type & AV_INT_STATUS)
  2948. av_con.usb_status = AVISION_USB_INT_STATUS;
  2949. DBG (3, "attach: opening %s\n", devname);
  2950. status = avision_open (devname, &av_con, sense_handler, 0);
  2951. if (status != SANE_STATUS_GOOD) {
  2952. DBG (1, "attach: open failed (%s)\n", sane_strstatus (status));
  2953. return SANE_STATUS_INVAL;
  2954. }
  2955. /* first: get the standard inquiry? */
  2956. status = inquiry (av_con, result, AVISION_INQUIRY_SIZE_V1);
  2957. if (status != SANE_STATUS_GOOD) {
  2958. DBG (1, "attach: 1st inquiry failed (%s)\n", sane_strstatus (status));
  2959. return status;
  2960. }
  2961. /* copy string information - and zero terminate them c-style */
  2962. memcpy (&mfg, result + 8, 8);
  2963. mfg [8] = 0;
  2964. memcpy (&model, result + 16, 16);
  2965. model [16] = 0;
  2966. memcpy (&rev, result + 32, 4);
  2967. rev [4] = 0;
  2968. /* shorten strings (-1 for last index
  2969. -1 for last 0; >0 because one char at least) */
  2970. for (i = sizeof (mfg) - 2; i > 0; i--) {
  2971. if (mfg[i] == 0x20)
  2972. mfg[i] = 0;
  2973. else
  2974. break;
  2975. }
  2976. for (i = sizeof (model) - 2; i > 0; i--) {
  2977. if (model[i] == 0x20)
  2978. model[i] = 0;
  2979. else
  2980. break;
  2981. }
  2982. DBG (1, "attach: Inquiry gives mfg=%s, model=%s, product revision=%s.\n",
  2983. mfg, model, rev);
  2984. model_num = 0;
  2985. found = 0;
  2986. /* while not at at end of list NULL terminator */
  2987. while (Avision_Device_List[model_num].real_mfg != NULL ||
  2988. Avision_Device_List[model_num].scsi_mfg != NULL)
  2989. {
  2990. int matches = 0, match_count = 0; /* count number of matches */
  2991. DBG (1, "attach: Checking model: %d\n", model_num);
  2992. if (Avision_Device_List[model_num].scsi_mfg) {
  2993. ++match_count;
  2994. if (strcmp(mfg, Avision_Device_List[model_num].scsi_mfg) == 0)
  2995. ++matches;
  2996. }
  2997. if (Avision_Device_List[model_num].scsi_model) {
  2998. ++match_count;
  2999. if (strcmp(model, Avision_Device_List[model_num].scsi_model) == 0)
  3000. ++matches;
  3001. }
  3002. /* we need 2 matches (mfg, model) for SCSI entries, or the ones available
  3003. for "we know what we are locking for" USB entries */
  3004. if ((attaching_hw == &(Avision_Device_List [model_num]) &&
  3005. matches == match_count) ||
  3006. matches == 2)
  3007. {
  3008. DBG (1, "attach: Scanner matched entry: %d: \"%s\", \"%s\", 0x%x, 0x%x\n",
  3009. model_num,
  3010. Avision_Device_List[model_num].scsi_mfg,
  3011. Avision_Device_List[model_num].scsi_model,
  3012. Avision_Device_List[model_num].usb_vendor,
  3013. Avision_Device_List[model_num].usb_product);
  3014. found = 1;
  3015. break;
  3016. }
  3017. ++model_num;
  3018. }
  3019. if (!found) {
  3020. DBG (0, "attach: \"%s\" - \"%s\" not yet in whitelist!\n", mfg, model);
  3021. DBG (0, "attach: You might want to report this output.\n");
  3022. DBG (0, "attach: To: rene@exactcode.de (the Avision backend author)\n");
  3023. status = SANE_STATUS_INVAL;
  3024. goto close_scanner_and_return;
  3025. }
  3026. /* second: maybe ask for the firmware status and flash ram info */
  3027. /* if (Avision_Device_List [model_num].feature_type & AV_FIRMWARE) */
  3028. if (0)
  3029. {
  3030. DBG (3, "attach: reading firmware status\n");
  3031. status = get_firmware_status (&av_con);
  3032. if (status != SANE_STATUS_GOOD) {
  3033. DBG (1, "attach: get firmware status failed (%s)\n",
  3034. sane_strstatus (status));
  3035. goto close_scanner_and_return;
  3036. }
  3037. DBG (3, "attach: reading flash ram info\n");
  3038. status = get_flash_ram_info (&av_con);
  3039. if (status != SANE_STATUS_GOOD) {
  3040. DBG (1, "attach: get flash ram info failed (%s)\n",
  3041. sane_strstatus (status));
  3042. goto close_scanner_and_return;
  3043. }
  3044. #ifdef FIRMWARE_DATABASE_INCLUDED
  3045. /* Send new NV-RAM (firmware) data */
  3046. status = send_nvram_data (&av_con);
  3047. if (status != SANE_STATUS_GOOD)
  3048. goto close_scanner_and_return;
  3049. #endif
  3050. }
  3051. /* third: get the extended Avision inquiry */
  3052. status = inquiry (av_con, result, AVISION_INQUIRY_SIZE_V1);
  3053. if (status != SANE_STATUS_GOOD) {
  3054. DBG (1, "attach: avision v1 inquiry failed (%s)\n", sane_strstatus (status));
  3055. goto close_scanner_and_return;
  3056. }
  3057. dev = malloc (sizeof (*dev));
  3058. if (!dev) {
  3059. status = SANE_STATUS_NO_MEM;
  3060. goto close_scanner_and_return;
  3061. }
  3062. memset (dev, 0, sizeof (*dev));
  3063. dev->hw = &Avision_Device_List[model_num];
  3064. dev->sane.name = strdup (devname);
  3065. dev->sane.vendor = dev->hw->real_mfg ? dev->hw->real_mfg : strdup (mfg);
  3066. dev->sane.model = dev->hw->real_model ? dev->hw->real_model : strdup (model);
  3067. dev->connection.connection_type = av_con.connection_type;
  3068. dev->connection.usb_status = av_con.usb_status;
  3069. /* and finally Avision even extended this one later on
  3070. the AV220C2 does not grok this */
  3071. dev->inquiry_asic_type = (int) result[91];
  3072. if (dev->inquiry_asic_type == AV_ASIC_C6)
  3073. {
  3074. status = inquiry (av_con, result, AVISION_INQUIRY_SIZE_V2);
  3075. if (status != SANE_STATUS_GOOD) {
  3076. DBG (1, "attach: avision v2 inquiry failed (%s)\n", sane_strstatus (status));
  3077. goto close_scanner_and_return;
  3078. }
  3079. }
  3080. debug_print_raw (6, "attach: raw data:\n", result, sizeof (result) );
  3081. DBG (3, "attach: [8-15] Vendor id.: '%8.8s'\n", result+8);
  3082. DBG (3, "attach: [16-31] Product id.: '%16.16s'\n", result+16);
  3083. DBG (3, "attach: [32-35] Product rev.: '%4.4s'\n", result+32);
  3084. i = (result[36] >> 4) & 0x7;
  3085. switch (result[36] & 0x07) {
  3086. case 0:
  3087. s = " RGB"; break;
  3088. case 1:
  3089. s = " BGR"; break;
  3090. default:
  3091. s = " unknown (RESERVERD)";
  3092. }
  3093. DBG (3, "attach: [36] Bitfield:%s%s%s%s%s%s%s color plane\n",
  3094. BIT(result[36],7)?" ADF":"",
  3095. (i==0)?" B&W only":"",
  3096. BIT(i, 1)?" 3-pass color":"",
  3097. BIT(i, 2)?" 1-pass color":"",
  3098. BIT(i, 2) && BIT(i, 0) ?" 1-pass color (ScanPartner only)":"",
  3099. BIT(result[36],3)?" IS_NOT_FLATBED":"",
  3100. s);
  3101. DBG (3, "attach: [37] Optical res.: %d00 dpi\n", result[37]);
  3102. DBG (3, "attach: [38] Maximum res.: %d00 dpi\n", result[38]);
  3103. DBG (3, "attach: [39] Bitfield1:%s%s%s%s%s%s\n",
  3104. BIT(result[39],7)?" TRANS":"",
  3105. BIT(result[39],6)?" Q_SCAN":"",
  3106. BIT(result[39],5)?" EXTENDED_RES":"",
  3107. BIT(result[39],4)?" SUPPORTS_CALIB":"",
  3108. BIT(result[39],2)?" NEW_PROTOCOL":"",
  3109. (result[39] & 0x03) == 0x03 ? " AVISION":" OEM");
  3110. DBG (3, "attach: [40-41] X res. in gray: %d dpi\n",
  3111. get_double ( &(result[40]) ));
  3112. DBG (3, "attach: [42-43] Y res. in gray: %d dpi\n",
  3113. get_double ( &(result[42]) ));
  3114. DBG (3, "attach: [44-45] X res. in color: %d dpi\n",
  3115. get_double ( &(result[44]) ));
  3116. DBG (3, "attach: [46-47] Y res. in color: %d dpi\n",
  3117. get_double ( &(result[46]) ));
  3118. DBG (3, "attach: [48-49] USB max read: %d\n",
  3119. get_double ( &(result[48] ) ));
  3120. DBG (3, "attach: [50] ESA1:%s%s%s%s%s%s%s%s\n",
  3121. BIT(result[50],7)?" LIGHT_CONTROL":"",
  3122. BIT(result[50],6)?" BUTTON_CONTROL":"",
  3123. BIT(result[50],5)?" NEED_SW_COLORPACK":"",
  3124. BIT(result[50],4)?" SW_CALIB":"",
  3125. BIT(result[50],3)?" NEED_SW_GAMMA":"",
  3126. BIT(result[50],2)?" KEEPS_GAMMA":"",
  3127. BIT(result[50],1)?" KEEPS_WINDOW_CMD":"",
  3128. BIT(result[50],0)?" XYRES_DIFFERENT":"");
  3129. DBG (3, "attach: [51] ESA2:%s%s%s%s%s%s%s%s\n",
  3130. BIT(result[51],7)?" EXPOSURE_CTRL":"",
  3131. BIT(result[51],6)?" NEED_SW_TRIGGER_CAL":"",
  3132. BIT(result[51],5)?" NEED_WHITE_PAPER_CALIB":"",
  3133. BIT(result[51],4)?" SUPPORTS_QUALITY_SPEED_CAL":"",
  3134. BIT(result[51],3)?" NEED_TRANSP_CAL":"",
  3135. BIT(result[51],2)?" HAS_PUSH_BUTTON":"",
  3136. BIT(result[51],1)?" NEW_CAL_METHOD_3x3_MATRIX_(NO_GAMMA_TABLE)":"",
  3137. BIT(result[51],0)?" ADF_MIRRORS_IMAGE":"");
  3138. DBG (3, "attach: [52] ESA3:%s%s%s%s%s%s%s%s\n",
  3139. BIT(result[52],7)?" GRAY_WHITE":"",
  3140. BIT(result[52],6)?" SUPPORTS_GAIN_CONTROL":"",
  3141. BIT(result[52],5)?" SUPPORTS_TET":"", /* "Text Enhanced Technology" */
  3142. BIT(result[52],4)?" 3x3COL_TABLE":"",
  3143. BIT(result[52],3)?" 1x3FILTER":"",
  3144. BIT(result[52],2)?" INDEX_COLOR":"",
  3145. BIT(result[52],1)?" POWER_SAVING_TIMER":"",
  3146. BIT(result[52],0)?" NVM_DATA_REC":"");
  3147. /* print some more scanner features/params */
  3148. DBG (3, "attach: [53] line difference (software color pack): %d\n", result[53]);
  3149. DBG (3, "attach: [54] color mode pixel boundary: %d\n", result[54]);
  3150. DBG (3, "attach: [55] gray mode pixel boundary: %d\n", result[55]);
  3151. DBG (3, "attach: [56] 4bit gray mode pixel boundary: %d\n", result[56]);
  3152. DBG (3, "attach: [57] lineart mode pixel boundary: %d\n", result[57]);
  3153. DBG (3, "attach: [58] halftone mode pixel boundary: %d\n", result[58]);
  3154. DBG (3, "attach: [59] error-diffusion mode pixel boundary: %d\n", result[59]);
  3155. DBG (3, "attach: [60] channels per pixel:%s%s%s\n",
  3156. BIT(result[60],7)?" 1":"",
  3157. BIT(result[60],6)?" 3":"",
  3158. (result[60] & 0x3F) != 0 ? " RESERVED":"");
  3159. DBG (3, "attach: [61] bits per channel:%s%s%s%s%s%s%s\n",
  3160. BIT(result[61],7)?" 1":"",
  3161. BIT(result[61],6)?" 4":"",
  3162. BIT(result[61],5)?" 6":"",
  3163. BIT(result[61],4)?" 8":"",
  3164. BIT(result[61],3)?" 10":"",
  3165. BIT(result[61],2)?" 12":"",
  3166. BIT(result[61],1)?" 16":"");
  3167. DBG (3, "attach: [62] scanner type:%s%s%s%s%s%s\n",
  3168. BIT(result[62],7)?" Flatbed":"",
  3169. BIT(result[62],6)?" Roller (ADF)":"",
  3170. BIT(result[62],5)?" Flatbed (ADF)":"",
  3171. BIT(result[62],4)?" Roller":"", /* does not feed multiple pages, AV25 */
  3172. BIT(result[62],3)?" Film scanner":"",
  3173. BIT(result[62],2)?" Duplex":"");
  3174. DBG (3, "attach: [75-76] Max shading target : %x\n",
  3175. get_double ( &(result[75]) ));
  3176. DBG (3, "attach: [77-78] Max X of transparency: %d dots * base_dpi\n",
  3177. get_double ( &(result[77]) ));
  3178. DBG (3, "attach: [79-80] Max Y of transparency: %d dots * base_dpi\n",
  3179. get_double ( &(result[79]) ));
  3180. DBG (3, "attach: [81-82] Max X of flatbed: %d dots * base_dpi\n",
  3181. get_double ( &(result[81]) ));
  3182. DBG (3, "attach: [83-84] Max Y of flatbed: %d dots * base_dpi\n",
  3183. get_double ( &(result[83]) ));
  3184. DBG (3, "attach: [85-86] Max X of ADF: %d dots * base_dpi\n",
  3185. get_double ( &(result[85]) ));
  3186. DBG (3, "attach: [87-88] Max Y of ADF: %d dots * base_dpi\n",
  3187. get_double ( &(result[87]) )); /* 0xFFFF means unlimited length */
  3188. DBG (3, "attach: [89-90] Res. in Ex. mode: %d dpi\n",
  3189. get_double ( &(result[89]) ));
  3190. DBG (3, "attach: [91] ASIC: %d\n", result[91]);
  3191. DBG (3, "attach: [92] Buttons: %d\n", result[92]);
  3192. DBG (3, "attach: [93] ESA4:%s%s%s%s%s%s%s%s\n",
  3193. BIT(result[93],7)?" SUPPORTS_ACCESSORIES_DETECT":"",
  3194. BIT(result[93],6)?" ADF_IS_BGR_ORDERED":"",
  3195. BIT(result[93],5)?" NO_SINGLE_CHANNEL_GRAY_MODE":"",
  3196. BIT(result[93],4)?" SUPPORTS_FLASH_UPDATE":"",
  3197. BIT(result[93],3)?" SUPPORTS_ASIC_UPDATE":"",
  3198. BIT(result[93],2)?" SUPPORTS_LIGHT_DETECT":"",
  3199. BIT(result[93],1)?" SUPPORTS_READ_PRNU_DATA":"",
  3200. BIT(result[93],0)?" FLATBED_MIRRORS_IMAGE":"");
  3201. DBG (3, "attach: [94] ESA5:%s%s%s%s%s%s%s%s\n",
  3202. BIT(result[94],7)?" IGNORE_LINE_DIFFERENCE_FOR_ADF":"",
  3203. BIT(result[94],6)?" NEEDS_SW_LINE_COLOR_PACK":"",
  3204. BIT(result[94],5)?" SUPPORTS_DUPLEX_SCAN":"",
  3205. BIT(result[94],4)?" INTERLACED_DUPLEX_SCAN":"",
  3206. BIT(result[94],3)?" SUPPORTS_TWO_MODE_ADF_SCANS":"",
  3207. BIT(result[94],2)?" SUPPORTS_TUNE_SCAN_LENGTH":"",
  3208. BIT(result[94],1)?" SUPPORTS_SWITCH_STRIP_FOR_DESKEW":"", /* Kodak i80 only */
  3209. BIT(result[94],0)?" SEARCHES_LEADING_SIDE_EDGE_BY_FIRMWARE":"");
  3210. DBG (3, "attach: [95] ESA6:%s%s%s%s%s%s%s%s\n",
  3211. BIT(result[95],7)?" SUPPORTS_PAPER_SIZE_AUTO_DETECTION":"",
  3212. BIT(result[95],6)?" SUPPORTS_DO_HOUSEKEEPING":"", /* Kodak i80 only */
  3213. BIT(result[95],5)?" SUPPORTS_PAPER_LENGTH_SETTING":"", /* AV220, Kodak */
  3214. BIT(result[95],4)?" SUPPORTS_PRE_GAMMA_LINEAR_CORRECTION":"",
  3215. BIT(result[95],3)?" SUPPORTS_PREFEEDING":"", /* OKI S9800 */
  3216. BIT(result[95],2)?" SUPPORTS_GET_BACKGROUND_RASTER":"", /* AV220 et.al. */
  3217. BIT(result[95],1)?" SUPPORTS_NVRAM_RESET":"",
  3218. BIT(result[95],0)?" SUPPORTS_BATCH_SCAN":"");
  3219. DBG (3, "attach: [128] ESA7:%s%s%s%s%s%s%s%s\n",
  3220. BIT(result[128],7)?" SUPPORTS_ADF_CONTINUOUS":"",
  3221. BIT(result[128],6)?" SUPPORTS_YCbCr_COLOR":"",
  3222. BIT(result[128],5)?" SUPPORTS_ADF_3PASS":"",
  3223. BIT(result[128],4)?" SUPPORTS_TUNE_SCAN_LENGTH_HORIZ":"",
  3224. BIT(result[128],3)?" SUPPORTS_READ_WRITE_ABILITY_PARAMETER":"",
  3225. BIT(result[128],2)?" SUPPORTS_JOB_CONTROL":"",
  3226. BIT(result[128],1)?" SUPPORTS_INF_LENGTH":"",
  3227. BIT(result[128],0)?" ULTRA_SONIC_DOUBLE_FEED_DETECTION":"");
  3228. DBG (3, "attach: [129] YCbCr:%s%s%s%s%s%s%s%s\n",
  3229. BIT(result[129],7)?" YCC4:2:0":"",
  3230. BIT(result[129],6)?" YCC(profile2)":"",
  3231. BIT(result[129],5)?" YCC(profile3)":"",
  3232. BIT(result[129],4)?" YCC(profile4)":"",
  3233. BIT(result[129],3)?" JPEG(profile1)":"",
  3234. BIT(result[129],2)?" JPEG(profile2)":"",
  3235. BIT(result[129],1)?" JPEG(profile3)":"",
  3236. BIT(result[129],0)?" JPEG(profile4)":"");
  3237. /* I have no idea how film scanner could reliably be detected -ReneR */
  3238. if (dev->hw->feature_type & AV_FILMSCANNER) {
  3239. dev->scanner_type = AV_FILM;
  3240. dev->sane.type = "film scanner";
  3241. }
  3242. else if ( BIT(result[62],6) || BIT(result[62],4) ) {
  3243. dev->scanner_type = AV_SHEETFEED;
  3244. dev->sane.type = "sheetfed scanner";
  3245. }
  3246. else {
  3247. dev->scanner_type = AV_FLATBED;
  3248. dev->sane.type = "flatbed scanner";
  3249. }
  3250. dev->inquiry_new_protocol = BIT (result[39],2);
  3251. dev->inquiry_asic_type = (int) result[91];
  3252. dev->inquiry_nvram_read = BIT(result[52],0);
  3253. dev->inquiry_power_save_time = BIT(result[52],1);
  3254. dev->inquiry_adf = BIT (result[62], 5);
  3255. dev->inquiry_duplex = BIT (result[62], 2) || BIT (result[94], 5);
  3256. dev->inquiry_duplex_interlaced = BIT(result[62],2) || BIT (result[94], 4);
  3257. /* the first avision scanners (AV3200) do not set the interlaced bit */
  3258. if (dev->inquiry_duplex && dev->inquiry_asic_type < AV_ASIC_C6)
  3259. dev->inquiry_duplex_interlaced = 1;
  3260. dev->inquiry_paper_length = BIT (result[95], 5);
  3261. dev->inquiry_batch_scan = BIT (result[95], 0); /* AV122, DM152 */
  3262. dev->inquiry_detect_accessories = BIT (result[93], 7);
  3263. dev->inquiry_needs_calibration = BIT (result[50], 4);
  3264. dev->inquiry_keeps_window = BIT (result[50], 1);
  3265. if (Avision_Device_List [model_num].feature_type & AV_DOES_NOT_KEEP_WINDOW)
  3266. dev->inquiry_keeps_window = 0;
  3267. if (Avision_Device_List [model_num].feature_type & AV_DOES_KEEP_WINDOW)
  3268. dev->inquiry_keeps_window = 1;
  3269. dev->inquiry_needs_gamma = BIT (result[50], 3);
  3270. dev->inquiry_keeps_gamma = BIT (result[50], 2);
  3271. if (Avision_Device_List [model_num].feature_type & AV_DOES_NOT_KEEP_GAMMA)
  3272. dev->inquiry_keeps_gamma = 0;
  3273. if (Avision_Device_List [model_num].feature_type & AV_DOES_KEEP_GAMMA)
  3274. dev->inquiry_keeps_gamma = 1;
  3275. dev->inquiry_3x3_matrix = BIT (result[51], 1);
  3276. dev->inquiry_needs_software_colorpack = BIT (result[50],5);
  3277. dev->inquiry_needs_line_pack = BIT (result[94], 6);
  3278. dev->inquiry_adf_need_mirror = BIT (result[51], 0);
  3279. dev->inquiry_adf_bgr_order = BIT (result[93], 6);
  3280. if (Avision_Device_List [model_num].feature_type & AV_ADF_BGR_ORDER_INVERT)
  3281. dev->inquiry_adf_bgr_order = ! dev->inquiry_adf_bgr_order;
  3282. dev->inquiry_light_detect = BIT (result[93], 2);
  3283. dev->inquiry_light_control = BIT (result[50], 7);
  3284. dev->inquiry_button_control = BIT (result[50], 6) | BIT (result[51],2);
  3285. dev->inquiry_exposure_control = BIT(result[51],7);
  3286. dev->inquiry_max_shading_target = get_double ( &(result[75]) );
  3287. dev->inquiry_color_boundary = result[54];
  3288. if (dev->inquiry_color_boundary == 0)
  3289. dev->inquiry_color_boundary = 8;
  3290. dev->inquiry_gray_boundary = result[55];
  3291. if (dev->inquiry_gray_boundary == 0)
  3292. dev->inquiry_gray_boundary = 8;
  3293. dev->inquiry_dithered_boundary = result[59];
  3294. if (dev->inquiry_dithered_boundary == 0)
  3295. dev->inquiry_dithered_boundary = 8;
  3296. dev->inquiry_thresholded_boundary = result[57];
  3297. if (dev->inquiry_thresholded_boundary == 0)
  3298. dev->inquiry_thresholded_boundary = 8;
  3299. dev->inquiry_line_difference = result[53];
  3300. /* compensation according to real world hardware */
  3301. switch (dev->inquiry_asic_type)
  3302. {
  3303. case AV_ASIC_C2: /* HP 5300 */
  3304. case AV_ASIC_C5: /* HP 53xx R2 */
  3305. dev->inquiry_line_difference /= 2; /* HP 5300 */
  3306. break;
  3307. case AV_ASIC_C7:
  3308. dev->inquiry_line_difference *= 2; /* AV610C2 */
  3309. break;
  3310. default:
  3311. ;
  3312. }
  3313. if (dev->inquiry_new_protocol) {
  3314. dev->inquiry_optical_res = get_double ( &(result[89]) );
  3315. dev->inquiry_max_res = get_double ( &(result[44]) );
  3316. }
  3317. else {
  3318. dev->inquiry_optical_res = result[37] * 100;
  3319. dev->inquiry_max_res = result[38] * 100;
  3320. }
  3321. /* fixup max res */
  3322. if (dev->inquiry_optical_res > dev->inquiry_max_res) {
  3323. DBG (1, "Inquiry optical resolution > max_resolution, adjusting!\n");
  3324. dev->inquiry_max_res = dev->inquiry_optical_res;
  3325. }
  3326. if (dev->inquiry_optical_res == 0)
  3327. {
  3328. DBG (1, "Inquiry optical resolution is invalid!\n");
  3329. if (dev->hw->feature_type & AV_FORCE_FILM)
  3330. dev->inquiry_optical_res = 2438; /* verify */
  3331. if (dev->scanner_type == AV_SHEETFEED)
  3332. dev->inquiry_optical_res = 300;
  3333. else
  3334. dev->inquiry_optical_res = 600;
  3335. }
  3336. if (dev->inquiry_max_res == 0) {
  3337. DBG (1, "Inquiry max resolution is invalid, using 1200 dpi!\n");
  3338. dev->inquiry_max_res = 1200;
  3339. }
  3340. DBG (1, "attach: optical resolution set to: %d dpi\n", dev->inquiry_optical_res);
  3341. DBG (1, "attach: max resolution set to: %d dpi\n", dev->inquiry_max_res);
  3342. if (BIT(result[60],6))
  3343. dev->inquiry_channels_per_pixel = 3;
  3344. else if (BIT(result[60],7))
  3345. dev->inquiry_channels_per_pixel = 1;
  3346. else if ( ((result[36] >> 4) & 0x7) > 0)
  3347. dev->inquiry_channels_per_pixel = 3;
  3348. else
  3349. dev->inquiry_channels_per_pixel = 1;
  3350. if (BIT(result[61],1))
  3351. dev->inquiry_bits_per_channel = 16;
  3352. else if (BIT(result[61],2))
  3353. dev->inquiry_bits_per_channel = 12;
  3354. else if (BIT(result[61],3))
  3355. dev->inquiry_bits_per_channel = 10;
  3356. else if (BIT(result[61],4))
  3357. dev->inquiry_bits_per_channel = 8;
  3358. else if (BIT(result[61],5))
  3359. dev->inquiry_bits_per_channel = 6;
  3360. else if (BIT(result[61],6))
  3361. dev->inquiry_bits_per_channel = 4;
  3362. else if (BIT(result[61],7))
  3363. dev->inquiry_bits_per_channel = 1;
  3364. else
  3365. dev->inquiry_bits_per_channel = 8; /* default for old scanners */
  3366. if (dev->hw->feature_type & AV_12_BIT_MODE)
  3367. dev->inquiry_bits_per_channel = 12;
  3368. if (!dev->hw->feature_type & AV_GRAY_MODES)
  3369. dev->inquiry_no_gray_modes = BIT(result[93],5);
  3370. DBG (1, "attach: max channels per pixel: %d, max bits per channel: %d\n",
  3371. dev->inquiry_channels_per_pixel, dev->inquiry_bits_per_channel);
  3372. if (! (dev->hw->feature_type & AV_NO_BUTTON))
  3373. dev->inquiry_buttons = result[92];
  3374. /* get max x/y ranges for the different modes */
  3375. {
  3376. double base_dpi; /* TODO: make int */
  3377. if (dev->scanner_type != AV_FILM) {
  3378. base_dpi = AVISION_BASE_RES;
  3379. } else {
  3380. /* ZP: The right number is 2820, whether it is 40-41, 42-43, 44-45,
  3381. * 46-47 or 89-90 I don't know but I would bet for the last !
  3382. * ReneR: OK. We use it via the optical_res which we need anyway ...
  3383. */
  3384. base_dpi = dev->inquiry_optical_res;
  3385. }
  3386. /* .1 to slightly increate the size to match the one of American standard paper
  3387. formats that would otherwise be .1 mm too large to scan ... */
  3388. dev->inquiry_x_ranges [AV_NORMAL_DIM] =
  3389. (double)get_double (&(result[81])) * MM_PER_INCH / base_dpi + .1;
  3390. dev->inquiry_y_ranges [AV_NORMAL_DIM] =
  3391. (double)get_double (&(result[83])) * MM_PER_INCH / base_dpi;
  3392. dev->inquiry_x_ranges [AV_TRANSPARENT_DIM] =
  3393. (double)get_double (&(result[77])) * MM_PER_INCH / base_dpi + .1;
  3394. dev->inquiry_y_ranges [AV_TRANSPARENT_DIM] =
  3395. (double)get_double (&(result[79])) * MM_PER_INCH / base_dpi;
  3396. dev->inquiry_x_ranges [AV_ADF_DIM] =
  3397. (double)get_double (&(result[85])) * MM_PER_INCH / base_dpi + .1;
  3398. dev->inquiry_y_ranges [AV_ADF_DIM] =
  3399. (double)get_double (&(result[87])) * MM_PER_INCH / base_dpi;
  3400. }
  3401. dev->inquiry_tune_scan_length = BIT(result[94],2);
  3402. dev->inquiry_background_raster = BIT(result[95],2);
  3403. if (dev->hw->feature_type & AV_NO_BACKGROUND)
  3404. dev->inquiry_background_raster = 0;
  3405. if (dev->inquiry_background_raster) {
  3406. dev->inquiry_background_raster_pixel =
  3407. get_double(&(result[85])) * dev->inquiry_optical_res / AVISION_BASE_RES;
  3408. }
  3409. /* check if x/y ranges are valid :-((( */
  3410. {
  3411. source_mode mode;
  3412. for (mode = AV_NORMAL_DIM; mode < AV_SOURCE_MODE_DIM_LAST; ++ mode)
  3413. {
  3414. if (dev->inquiry_x_ranges [mode] != 0 &&
  3415. dev->inquiry_y_ranges [mode] != 0)
  3416. {
  3417. DBG (3, "attach: x/y-range for mode %d is valid!\n", mode);
  3418. if (force_a4) {
  3419. DBG (1, "attach: \"force_a4\" found! Using defauld (ISO A4).\n");
  3420. dev->inquiry_x_ranges [mode] = A4_X_RANGE * MM_PER_INCH;
  3421. dev->inquiry_y_ranges [mode] = A4_Y_RANGE * MM_PER_INCH;
  3422. } else if (force_a3) {
  3423. DBG (1, "attach: \"force_a3\" found! Using defauld (ISO A3).\n");
  3424. dev->inquiry_x_ranges [mode] = A3_X_RANGE * MM_PER_INCH;
  3425. dev->inquiry_y_ranges [mode] = A3_Y_RANGE * MM_PER_INCH;
  3426. }
  3427. }
  3428. else /* mode is invaild */
  3429. {
  3430. DBG (1, "attach: x/y-range for mode %d is invalid! Using a default.\n", mode);
  3431. if (dev->hw->feature_type & AV_FORCE_A3) {
  3432. dev->inquiry_x_ranges [mode] = A3_X_RANGE * MM_PER_INCH;
  3433. dev->inquiry_y_ranges [mode] = A3_Y_RANGE * MM_PER_INCH;
  3434. }
  3435. else if (dev->hw->feature_type & AV_FORCE_FILM) {
  3436. dev->inquiry_x_ranges [mode] = FILM_X_RANGE * MM_PER_INCH;
  3437. dev->inquiry_y_ranges [mode] = FILM_Y_RANGE * MM_PER_INCH;
  3438. }
  3439. else {
  3440. dev->inquiry_x_ranges [mode] = A4_X_RANGE * MM_PER_INCH;
  3441. if (dev->scanner_type == AV_SHEETFEED)
  3442. dev->inquiry_y_ranges [mode] = SHEETFEED_Y_RANGE * MM_PER_INCH;
  3443. else
  3444. dev->inquiry_y_ranges [mode] = A4_Y_RANGE * MM_PER_INCH;
  3445. }
  3446. }
  3447. DBG (1, "attach: Mode %d range is now: %f x %f mm.\n",
  3448. mode,
  3449. dev->inquiry_x_ranges [mode], dev->inquiry_y_ranges [mode]);
  3450. } /* end for all modes */
  3451. }
  3452. /* We need a bigger buffer for USB devices, since they seem to have
  3453. a firmware bug and do not support reading the calibration data in
  3454. tiny chunks */
  3455. if (av_con.connection_type == AV_USB)
  3456. dev->scsi_buffer_size = 1024 * 1024; /* or 0x10000, used by AV Windows driver during background raster read, ... ? */
  3457. else
  3458. dev->scsi_buffer_size = sanei_scsi_max_request_size;
  3459. if (dev->inquiry_asic_type >= AV_ASIC_C5)
  3460. dev->read_stripe_size = 32;
  3461. else /* tested on AV3200 with it's max of 300dpi @color */
  3462. dev->read_stripe_size = 8; /* maybe made dynamic on scan res ... */
  3463. /* set the flag for an additional probe at sane_open() */
  3464. dev->additional_probe = SANE_TRUE;
  3465. /* normally the data_dq is 0x0a0d - but some newer scanner hang with it ... */
  3466. if (dev->inquiry_new_protocol) /* TODO: match on ASIC? which model hung? */
  3467. dev->data_dq = 0x0a0d;
  3468. else
  3469. dev->data_dq = 0;
  3470. avision_close (&av_con);
  3471. ++ num_devices;
  3472. dev->next = first_dev;
  3473. first_dev = dev;
  3474. if (devp)
  3475. *devp = dev;
  3476. return SANE_STATUS_GOOD;
  3477. close_scanner_and_return:
  3478. avision_close (&av_con);
  3479. return status;
  3480. }
  3481. static SANE_Status
  3482. get_tune_scan_length (Avision_Scanner* s)
  3483. {
  3484. SANE_Status status;
  3485. int i;
  3486. struct command_read rcmd;
  3487. size_t size;
  3488. struct max_value {
  3489. uint8_t max [2];
  3490. } payload;
  3491. /* turn on the light */
  3492. DBG (3, "get_tune_scan_length:\n");
  3493. memset (&rcmd, 0, sizeof (rcmd));
  3494. size = sizeof (payload);
  3495. rcmd.opc = AVISION_SCSI_READ;
  3496. rcmd.datatypecode = 0xD2; /* Read General Ability/Parameter */
  3497. for (i = 1; i <= 8; ++i) {
  3498. memset (&payload, 0, sizeof (payload));
  3499. set_double (rcmd.datatypequal, i); /* type */
  3500. set_triple (rcmd.transferlen, size);
  3501. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd),
  3502. 0, 0, &payload, &size);
  3503. if (status != SANE_STATUS_GOOD) {
  3504. DBG (1, "get_tune_scan_length: read %d failed (%s)\n", i, sane_strstatus (status));
  3505. return status;
  3506. }
  3507. DBG (1, "get_tune_scan_length: %d: %d\n", i, get_double (payload.max));
  3508. }
  3509. return SANE_STATUS_GOOD;
  3510. }
  3511. static SANE_Status
  3512. send_tune_scan_length (Avision_Scanner* s)
  3513. {
  3514. int top, bottom;
  3515. SANE_Status status;
  3516. size_t size;
  3517. struct command_send scmd;
  3518. struct truncate_attach {
  3519. uint8_t vertical [2];
  3520. /* uint8_t horizontal [2]; not send by the Windows driver, yet */
  3521. } payload;
  3522. DBG (3, "send_tune_scan_length:\n");
  3523. memset (&scmd, 0, sizeof (scmd));
  3524. size = sizeof (payload);
  3525. scmd.opc = AVISION_SCSI_SEND;
  3526. scmd.datatypecode = 0x96; /* Attach/Truncate head(left) of scan length */
  3527. set_triple (scmd.transferlen, size);
  3528. /* the SPEC says optical DPI, but real world meassuring suggests it is 1200
  3529. as in the window descriptor */
  3530. top = 1200 * SANE_UNFIX (s->val[OPT_OVERSCAN_TOP].w) / MM_PER_INCH;
  3531. DBG (3, "send_tune_scan_length: top: %d\n", top);
  3532. set_double (scmd.datatypequal, 0x0001); /* attach, 0x000 is shorten */
  3533. set_double (payload.vertical, top);
  3534. /* set_double (payload.horizontal, 0); */
  3535. /* we alway send it, even for 0 as the scanner keeps it in RAM and
  3536. previous runs could aleady have set something */
  3537. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  3538. &payload, sizeof (payload), 0, 0);
  3539. if (status != SANE_STATUS_GOOD) {
  3540. DBG (1, "send_tune_scan_length: send top/left failed (%s)\n", sane_strstatus (status));
  3541. return status;
  3542. }
  3543. scmd.datatypecode = 0x95; /* Attach/Truncate tail(right) of scan length */
  3544. bottom = 1200 * SANE_UNFIX (s->val[OPT_OVERSCAN_BOTTOM].w) / MM_PER_INCH;
  3545. DBG (3, "send_tune_scan_length: bottom: %d\n", bottom);
  3546. set_double (payload.vertical, bottom);
  3547. /*set_double (payload.horizontal, 0); */
  3548. size = sizeof (payload);
  3549. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  3550. &payload, sizeof (payload), 0, 0);
  3551. if (status != SANE_STATUS_GOOD) {
  3552. DBG (1, "send_tune_scan_length: send bottom/right failed (%s)\n", sane_strstatus (status));
  3553. return status;
  3554. }
  3555. return SANE_STATUS_GOOD;
  3556. }
  3557. static SANE_Status
  3558. additional_probe (Avision_Scanner* s)
  3559. {
  3560. Avision_Device* dev = s->hw;
  3561. /* we should wait until the scanner is ready before we
  3562. perform further actions */
  3563. SANE_Status status;
  3564. /* try to retrieve additional accessories information */
  3565. if (dev->inquiry_detect_accessories) {
  3566. status = get_accessories_info (s);
  3567. if (status != SANE_STATUS_GOOD)
  3568. return status;
  3569. }
  3570. /* for a film scanner try to retrieve additional frame information */
  3571. if (dev->scanner_type == AV_FILM) {
  3572. status = get_frame_info (s);
  3573. if (status != SANE_STATUS_GOOD)
  3574. return status;
  3575. }
  3576. /* no scanner did support this so far: tried on AV220, DM152 */
  3577. if (0 && dev->inquiry_duplex) {
  3578. status = get_duplex_info (s);
  3579. if (status != SANE_STATUS_GOOD)
  3580. return status;
  3581. }
  3582. /* get overscan ("head/tail tune") information: hangs AV220, zeros on AV122 */
  3583. if (0 && dev->inquiry_tune_scan_length) {
  3584. status = get_tune_scan_length (s);
  3585. if (status != SANE_STATUS_GOOD)
  3586. return status;
  3587. }
  3588. /* create dynamic *-mode entries */
  3589. if (!dev->inquiry_no_gray_modes)
  3590. {
  3591. if (dev->inquiry_bits_per_channel > 0) {
  3592. add_color_mode (dev, AV_THRESHOLDED, "Lineart");
  3593. add_color_mode (dev, AV_DITHERED, "Dithered");
  3594. }
  3595. if (dev->inquiry_bits_per_channel >= 8)
  3596. add_color_mode (dev, AV_GRAYSCALE, "Gray");
  3597. if (dev->inquiry_bits_per_channel == 12)
  3598. add_color_mode (dev, AV_GRAYSCALE12, "12bit Gray");
  3599. if (dev->inquiry_bits_per_channel >= 16)
  3600. add_color_mode (dev, AV_GRAYSCALE16, "16bit Gray");
  3601. }
  3602. if (dev->inquiry_channels_per_pixel > 1) {
  3603. add_color_mode (dev, AV_TRUECOLOR, "Color");
  3604. if (dev->inquiry_bits_per_channel == 12)
  3605. add_color_mode (dev, AV_TRUECOLOR12, "12bit Color");
  3606. if (dev->inquiry_bits_per_channel >= 16)
  3607. add_color_mode (dev, AV_TRUECOLOR16, "16bit Color");
  3608. }
  3609. /* now choose the default mode - avoiding the 12/16 bit modes */
  3610. dev->color_list_default = last_color_mode (dev);
  3611. if (dev->inquiry_bits_per_channel > 8 && dev->color_list_default > 0) {
  3612. dev->color_list_default--;
  3613. }
  3614. if (dev->scanner_type == AV_SHEETFEED)
  3615. {
  3616. add_source_mode (dev, AV_ADF, "ADF Front");
  3617. }
  3618. else
  3619. {
  3620. add_source_mode (dev, AV_NORMAL, "Normal");
  3621. if (dev->inquiry_light_box)
  3622. add_source_mode (dev, AV_TRANSPARENT, "Transparency");
  3623. if (dev->inquiry_adf)
  3624. add_source_mode (dev, AV_ADF, "ADF Front");
  3625. }
  3626. if (dev->inquiry_duplex) {
  3627. if (dev->inquiry_duplex_interlaced && !(dev->hw->feature_type & AV_NO_REAR))
  3628. add_source_mode (dev, AV_ADF_REAR, "ADF Back");
  3629. add_source_mode (dev, AV_ADF_DUPLEX, "ADF Duplex");
  3630. }
  3631. dev->additional_probe = SANE_FALSE;
  3632. return SANE_STATUS_GOOD;
  3633. }
  3634. static SANE_Status
  3635. get_calib_format (Avision_Scanner* s, struct calibration_format* format)
  3636. {
  3637. SANE_Status status;
  3638. struct command_read rcmd;
  3639. uint8_t result [32];
  3640. size_t size;
  3641. DBG (3, "get_calib_format:\n");
  3642. size = sizeof (result);
  3643. memset (&rcmd, 0, sizeof (rcmd));
  3644. rcmd.opc = AVISION_SCSI_READ;
  3645. rcmd.datatypecode = 0x60; /* get calibration format */
  3646. set_double (rcmd.datatypequal, s->hw->data_dq);
  3647. set_triple (rcmd.transferlen, size);
  3648. DBG (3, "get_calib_format: read_data: %lu bytes\n", (u_long) size);
  3649. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, result, &size);
  3650. if (status != SANE_STATUS_GOOD || size != sizeof (result) ) {
  3651. DBG (1, "get_calib_format: read calib. info failed (%s)\n",
  3652. sane_strstatus (status) );
  3653. return status;
  3654. }
  3655. debug_print_calib_format (3, "get_calib_format", result);
  3656. format->pixel_per_line = get_double (&(result[0]));
  3657. format->bytes_per_channel = result[2];
  3658. format->lines = result[3];
  3659. format->flags = result[4];
  3660. format->ability1 = result[5];
  3661. format->r_gain = result[6];
  3662. format->g_gain = result[7];
  3663. format->b_gain = result[8];
  3664. format->r_shading_target = get_double (&(result[9]));
  3665. format->g_shading_target = get_double (&(result[11]));
  3666. format->b_shading_target = get_double (&(result[13]));
  3667. format->r_dark_shading_target = get_double (&(result[15]));
  3668. format->g_dark_shading_target = get_double (&(result[17]));
  3669. format->b_dark_shading_target = get_double (&(result[19]));
  3670. /* now translate to normal! */
  3671. /* firmware return R--RG--GB--B with 3 line count */
  3672. /* software format it as 1 line if true color scan */
  3673. /* only line interleave format to be supported */
  3674. if (color_mode_is_color (s->c_mode) || BIT(format->ability1, 3)) {
  3675. format->channels = 3;
  3676. format->lines /= 3; /* line interleave */
  3677. }
  3678. else
  3679. format->channels = 1;
  3680. DBG (3, "get_calib_format: channels: %d\n", format->channels);
  3681. return SANE_STATUS_GOOD;
  3682. }
  3683. static SANE_Status
  3684. get_calib_data (Avision_Scanner* s, uint8_t data_type,
  3685. uint8_t* calib_data,
  3686. size_t calib_size)
  3687. {
  3688. SANE_Status status;
  3689. uint8_t *calib_ptr;
  3690. size_t get_size, data_size, chunk_size;
  3691. struct command_read rcmd;
  3692. chunk_size = calib_size;
  3693. DBG (3, "get_calib_data: type %x, size %lu, chunk_size: %lu\n",
  3694. data_type, (u_long) calib_size, (u_long) chunk_size);
  3695. memset (&rcmd, 0, sizeof (rcmd));
  3696. rcmd.opc = AVISION_SCSI_READ;
  3697. rcmd.datatypecode = data_type;
  3698. set_double (rcmd.datatypequal, s->hw->data_dq);
  3699. calib_ptr = calib_data;
  3700. get_size = chunk_size;
  3701. data_size = calib_size;
  3702. while (data_size) {
  3703. if (get_size > data_size)
  3704. get_size = data_size;
  3705. read_constrains(s, get_size);
  3706. set_triple (rcmd.transferlen, get_size);
  3707. DBG (3, "get_calib_data: Reading %ld bytes calibration data\n",
  3708. (long)get_size);
  3709. status = avision_cmd (&s->av_con, &rcmd,
  3710. sizeof (rcmd), 0, 0, calib_ptr, &get_size);
  3711. if (status != SANE_STATUS_GOOD) {
  3712. DBG (1, "get_calib_data: read data failed (%s)\n",
  3713. sane_strstatus (status));
  3714. return status;
  3715. }
  3716. DBG (3, "get_calib_data: Got %ld bytes calibration data\n", (long)get_size);
  3717. data_size -= get_size;
  3718. calib_ptr += get_size;
  3719. }
  3720. return SANE_STATUS_GOOD;
  3721. }
  3722. static SANE_Status
  3723. set_calib_data (Avision_Scanner* s, struct calibration_format* format,
  3724. uint8_t* dark_data, uint8_t* white_data)
  3725. {
  3726. Avision_Device* dev = s->hw;
  3727. const int elements_per_line = format->pixel_per_line * format->channels;
  3728. SANE_Status status;
  3729. uint8_t send_type;
  3730. uint16_t send_type_q;
  3731. struct command_send scmd;
  3732. int i;
  3733. size_t out_size;
  3734. DBG (3, "set_calib_data:\n");
  3735. send_type = 0x82; /* download calibration data */
  3736. /* do we use a color mode? */
  3737. if (format->channels > 1) {
  3738. send_type_q = 0x12; /* color calib data */
  3739. }
  3740. else {
  3741. if (dev->hw->feature_type & AV_GRAY_CALIB_BLUE)
  3742. send_type_q = 0x2; /* gray/bw calib data on the blue channel (AV610) */
  3743. else
  3744. send_type_q = 0x11; /* gray/bw calib data */
  3745. }
  3746. memset (&scmd, 0x00, sizeof (scmd));
  3747. scmd.opc = AVISION_SCSI_SEND;
  3748. scmd.datatypecode = send_type;
  3749. /* data corrections due to dark calibration data merge */
  3750. if (BIT (format->ability1, 2) ) {
  3751. DBG (3, "set_calib_data: merging dark calibration data\n");
  3752. for (i = 0; i < elements_per_line; ++i) {
  3753. uint16_t value_orig = get_double_le (white_data + i*2);
  3754. uint16_t value_new = value_orig;
  3755. value_new &= 0xffc0;
  3756. value_new |= (get_double_le (dark_data + i*2) >> 10) & 0x3f;
  3757. DBG (100, "set_calib_data: element %d, dark difference %d\n",
  3758. i, value_orig - value_new);
  3759. set_double_le ((white_data + i*2), value_new);
  3760. }
  3761. }
  3762. out_size = format->pixel_per_line * 2;
  3763. /* send data in one command? */
  3764. /* FR: HP5370 reports one-pass, but needs multi (or other format in single) */
  3765. if (format->channels == 1 ||
  3766. ( ( (dev->hw->feature_type & AV_ONE_CALIB_CMD) ||
  3767. ! BIT(format->ability1, 0) ) &&
  3768. ! (dev->hw->feature_type & AV_MULTI_CALIB_CMD) ) )
  3769. /* one command (most scanners) */
  3770. {
  3771. size_t send_size = elements_per_line * 2;
  3772. DBG (3, "set_calib_data: all channels in one command\n");
  3773. DBG (3, "set_calib_data: send_size: %lu\n", (u_long) send_size);
  3774. memset (&scmd, 0, sizeof (scmd) );
  3775. scmd.opc = AVISION_SCSI_SEND;
  3776. scmd.datatypecode = send_type;
  3777. set_double (scmd.datatypequal, send_type_q);
  3778. set_triple (scmd.transferlen, send_size);
  3779. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  3780. (char*) white_data, send_size, 0, 0);
  3781. /* not return immediately to free mem at the end */
  3782. }
  3783. else /* send data channel by channel (some USB ones) */
  3784. {
  3785. int conv_out_size = format->pixel_per_line * 2;
  3786. uint16_t* conv_out_data; /* here it is save to use 16bit data
  3787. since we only move wohle words around */
  3788. DBG (3, "set_calib_data: channels in single commands\n");
  3789. conv_out_data = (uint16_t*) malloc (conv_out_size);
  3790. if (!conv_out_data) {
  3791. status = SANE_STATUS_NO_MEM;
  3792. }
  3793. else {
  3794. int channel;
  3795. for (channel = 0; channel < 3; ++ channel)
  3796. {
  3797. int i;
  3798. /* no need for endianness handling since whole word copy */
  3799. uint16_t* casted_avg_data = (uint16_t*) white_data;
  3800. DBG (3, "set_calib_data_calibration: channel: %i\n", channel);
  3801. for (i = 0; i < format->pixel_per_line; ++ i)
  3802. conv_out_data [i] = casted_avg_data [i * 3 + channel];
  3803. DBG (3, "set_calib_data: sending %i bytes now\n",
  3804. conv_out_size);
  3805. memset (&scmd, 0, sizeof (scmd));
  3806. scmd.opc = AVISION_SCSI_SEND;
  3807. scmd.datatypecode = send_type; /* send calibration data */
  3808. set_double (scmd.datatypequal, channel);
  3809. set_triple (scmd.transferlen, conv_out_size);
  3810. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  3811. conv_out_data, conv_out_size, 0, 0);
  3812. if (status != SANE_STATUS_GOOD) {
  3813. DBG (3, "set_calib_data: send_data failed (%s)\n",
  3814. sane_strstatus (status));
  3815. /* not return immediately to free mem at the end */
  3816. }
  3817. } /* end for each channel */
  3818. free (conv_out_data);
  3819. } /* end else send calib data*/
  3820. }
  3821. return SANE_STATUS_GOOD;
  3822. }
  3823. /* Sort data pixel by pixel and average first 2/3 of the data.
  3824. The caller has to free return pointer. R,G,B pixels
  3825. interleave to R,G,B line interleave.
  3826. The input data data is in 16 bits little endian, always.
  3827. That is a = b[1] << 8 + b[0] in all system.
  3828. We convert it to SCSI high-endian (big-endian) since we use it all
  3829. over the place anyway .... - Sorry for this mess. */
  3830. static uint8_t*
  3831. sort_and_average (struct calibration_format* format, uint8_t* data)
  3832. {
  3833. const int elements_per_line = format->pixel_per_line * format->channels;
  3834. const int stride = format->bytes_per_channel * elements_per_line;
  3835. int i, line;
  3836. uint8_t *sort_data, *avg_data;
  3837. DBG (1, "sort_and_average:\n");
  3838. if (!format || !data)
  3839. return NULL;
  3840. sort_data = malloc (format->lines * 2);
  3841. if (!sort_data)
  3842. return NULL;
  3843. avg_data = malloc (elements_per_line * 2);
  3844. if (!avg_data) {
  3845. free (sort_data);
  3846. return NULL;
  3847. }
  3848. /* for each pixel */
  3849. for (i = 0; i < elements_per_line; ++ i)
  3850. {
  3851. uint8_t* ptr1 = data + i * format->bytes_per_channel;
  3852. uint16_t temp;
  3853. /* copy all lines for pixel i into the linear array sort_data */
  3854. for (line = 0; line < format->lines; ++ line) {
  3855. uint8_t* ptr2 = ptr1 + line * stride; /* pixel */
  3856. if (format->bytes_per_channel == 1)
  3857. temp = 0xffff * *ptr2 / 255;
  3858. else
  3859. temp = get_double_le (ptr2); /* little-endian! */
  3860. set_double ((sort_data + line*2), temp); /* store big-endian */
  3861. /* DBG (7, "ReneR to sort: %x\n", temp); */
  3862. }
  3863. temp = bubble_sort (sort_data, format->lines);
  3864. /* DBG (7, "ReneR averaged: %x\n", temp); */
  3865. set_double ((avg_data + i*2), temp); /* store big-endian */
  3866. }
  3867. free ((void *) sort_data);
  3868. return avg_data;
  3869. }
  3870. /* shading data is 16bits little endian format when send/read from firmware */
  3871. static void
  3872. compute_dark_shading_data (Avision_Scanner* s,
  3873. struct calibration_format* format, uint8_t* data)
  3874. {
  3875. uint16_t map_value = DEFAULT_DARK_SHADING;
  3876. uint16_t rgb_map_value[3];
  3877. int elements_per_line, i;
  3878. DBG (3, "compute_dark_shading_data:\n");
  3879. if (s->hw->inquiry_max_shading_target != INVALID_DARK_SHADING)
  3880. map_value = s->hw->inquiry_max_shading_target << 8;
  3881. rgb_map_value[0] = format->r_dark_shading_target;
  3882. rgb_map_value[1] = format->g_dark_shading_target;
  3883. rgb_map_value[2] = format->b_dark_shading_target;
  3884. for (i = 0; i < format->channels; ++i) {
  3885. if (rgb_map_value[i] == INVALID_DARK_SHADING)
  3886. rgb_map_value[i] = map_value;
  3887. }
  3888. if (format->channels == 1) {
  3889. /* set to green, TODO: should depend on color drop-out and true-gray -ReneR */
  3890. rgb_map_value[0] = rgb_map_value[1] = rgb_map_value[2] = rgb_map_value[1];
  3891. }
  3892. elements_per_line = format->pixel_per_line * format->channels;
  3893. /* Check line interleave or pixel interleave. */
  3894. /* It seems no ASIC use line interleave right now. */
  3895. /* Avision SCSI protocol document has bad description. */
  3896. for (i = 0; i < elements_per_line; ++i)
  3897. {
  3898. uint16_t tmp_data = get_double_le((data + i*2));
  3899. if (tmp_data > rgb_map_value[i % 3]) {
  3900. set_double ((data + i*2), tmp_data - rgb_map_value[i % 3]);
  3901. }
  3902. else {
  3903. set_double ((data + i*2), 0);
  3904. }
  3905. }
  3906. }
  3907. static void
  3908. compute_white_shading_data (Avision_Scanner* s,
  3909. struct calibration_format* format, uint8_t* data)
  3910. {
  3911. int i;
  3912. uint16_t inquiry_mst = DEFAULT_WHITE_SHADING;
  3913. uint16_t mst[3];
  3914. int elements_per_line = format->pixel_per_line * format->channels;
  3915. /* debug counter */
  3916. int values_invalid = 0;
  3917. int values_limitted = 0;
  3918. DBG (3, "compute_white_shading_data:\n");
  3919. if (s->hw->inquiry_max_shading_target != INVALID_WHITE_SHADING)
  3920. inquiry_mst = s->hw->inquiry_max_shading_target << 4;
  3921. mst[0] = format->r_shading_target;
  3922. mst[1] = format->g_shading_target;
  3923. mst[2] = format->b_shading_target;
  3924. for (i = 0; i < 3; ++i) {
  3925. if (mst[i] == INVALID_WHITE_SHADING) /* mst[i] > MAX_WHITE_SHADING) */ {
  3926. DBG (3, "compute_white_shading_data: target %d invaild (%x) using inquiry (%x)\n",
  3927. i, mst[i], inquiry_mst);
  3928. mst[i] = inquiry_mst;
  3929. }
  3930. /* some firmware versions seems to return the bytes swapped? */
  3931. else if (mst[i] < 0x110) {
  3932. uint8_t* swap_mst = (uint8_t*) &mst[i];
  3933. uint8_t low_nibble_mst = swap_mst [0];
  3934. swap_mst [0] = swap_mst[1];
  3935. swap_mst [1] = low_nibble_mst;
  3936. DBG (3, "compute_white_shading_data: target %d: bytes swapped.\n", i);
  3937. }
  3938. if (mst[i] < DEFAULT_WHITE_SHADING / 2) {
  3939. DBG (3, "compute_white_shading_data: target %d: too low (%d) usind default (%d).\n",
  3940. i, mst[i], DEFAULT_WHITE_SHADING);
  3941. mst[i] = DEFAULT_WHITE_SHADING;
  3942. }
  3943. else
  3944. DBG (3, "compute_white_shading_data: target %d: %x\n", i, mst[0]);
  3945. }
  3946. /* some Avision example code was present here until SANE/Avision
  3947. * BUILD 57. */
  3948. if (format->channels == 1) {
  3949. /* set to green, TODO: should depend on color drop-out and true-gray -ReneR */
  3950. mst[0] = mst[1] = mst[2] = mst[1];
  3951. }
  3952. /* calculate calibration data */
  3953. for (i = 0; i < elements_per_line; ++ i)
  3954. {
  3955. int result;
  3956. /* calculate calibration value for pixel i */
  3957. uint16_t tmp_data = get_double((data + i*2));
  3958. if (tmp_data == INVALID_WHITE_SHADING) {
  3959. tmp_data = DEFAULT_WHITE_SHADING;
  3960. ++ values_invalid;
  3961. }
  3962. result = ( (int)mst[i % 3] * WHITE_MAP_RANGE / (tmp_data + 0.5));
  3963. /* sanity check for over-amplification, clipping */
  3964. if (result > MAX_WHITE_SHADING) {
  3965. result = WHITE_MAP_RANGE;
  3966. ++ values_limitted;
  3967. }
  3968. /* for visual debugging ... */
  3969. if (static_calib_list [i % 3] == SANE_TRUE)
  3970. result = 0xA000;
  3971. /* the output to the scanner will be 16 bit little endian again */
  3972. set_double_le ((data + i*2), result);
  3973. }
  3974. DBG (3, "compute_white_shading_data: %d invalid, %d limitted\n",
  3975. values_invalid, values_limitted);
  3976. }
  3977. /* old_r_calibration was here until SANE/Avision BUILD 90 */
  3978. static SANE_Status
  3979. normal_calibration (Avision_Scanner* s)
  3980. {
  3981. SANE_Status status;
  3982. struct calibration_format calib_format;
  3983. int calib_data_size, calib_bytes_per_line;
  3984. uint8_t read_type;
  3985. uint8_t *calib_tmp_data;
  3986. DBG (1, "normal_calibration:\n");
  3987. /* get calibration format and data */
  3988. status = get_calib_format (s, &calib_format);
  3989. if (status != SANE_STATUS_GOOD)
  3990. return status;
  3991. /* check if need do calibration */
  3992. if (calib_format.flags != 1) {
  3993. DBG (1, "normal_calibration: Scanner claims no calibration needed -> skipped!\n");
  3994. return SANE_STATUS_GOOD;
  3995. }
  3996. /* calculate calibration data size for read from scanner */
  3997. /* size = lines * bytes_per_channel * pixels_per_line * channel */
  3998. calib_bytes_per_line = calib_format.bytes_per_channel *
  3999. calib_format.pixel_per_line * calib_format.channels;
  4000. calib_data_size = calib_format.lines * calib_bytes_per_line;
  4001. calib_tmp_data = malloc (calib_data_size);
  4002. if (!calib_tmp_data)
  4003. return SANE_STATUS_NO_MEM;
  4004. /* check if we need to do dark calibration (shading) */
  4005. if (BIT(calib_format.ability1, 3))
  4006. {
  4007. DBG (1, "normal_calibration: reading dark data\n");
  4008. /* read dark calib data */
  4009. status = get_calib_data (s, 0x66, calib_tmp_data, calib_data_size);
  4010. if (status != SANE_STATUS_GOOD) {
  4011. free (calib_tmp_data);
  4012. return status;
  4013. }
  4014. /* process dark data: sort and average. */
  4015. if (s->dark_avg_data) {
  4016. free (s->dark_avg_data);
  4017. s->dark_avg_data = 0;
  4018. }
  4019. s->dark_avg_data = sort_and_average (&calib_format, calib_tmp_data);
  4020. if (!s->dark_avg_data) {
  4021. free (calib_tmp_data);
  4022. return SANE_STATUS_NO_MEM;
  4023. }
  4024. compute_dark_shading_data (s, &calib_format, s->dark_avg_data);
  4025. }
  4026. /* do we use a color mode? */
  4027. if (calib_format.channels > 1) {
  4028. DBG (3, "normal_calibration: using color calibration\n");
  4029. read_type = 0x62; /* read color calib data */
  4030. }
  4031. else {
  4032. DBG (3, "normal_calibration: using gray calibration\n");
  4033. read_type = 0x61; /* gray calib data */
  4034. }
  4035. /* do white calibration: read gray or color data */
  4036. status = get_calib_data (s, read_type, calib_tmp_data, calib_data_size);
  4037. if (status != SANE_STATUS_GOOD) {
  4038. free (calib_tmp_data);
  4039. return status;
  4040. }
  4041. if (0) /* debug */
  4042. {
  4043. FILE* f = NULL;
  4044. f = fopen ("calibration-white.pnm", "w");
  4045. write_pnm_header (f, AV_GRAYSCALE, calib_format.bytes_per_channel * 8,
  4046. calib_format.pixel_per_line,
  4047. calib_format.lines * calib_format.channels);
  4048. fwrite (calib_tmp_data, 1, calib_data_size, f);
  4049. fclose (f);
  4050. }
  4051. if (s->white_avg_data) {
  4052. free (s->white_avg_data);
  4053. s->white_avg_data = 0;
  4054. }
  4055. s->white_avg_data = sort_and_average (&calib_format, calib_tmp_data);
  4056. if (!s->white_avg_data) {
  4057. free (calib_tmp_data);
  4058. return SANE_STATUS_NO_MEM;
  4059. }
  4060. /* decrease white average data (if dark average data is present) */
  4061. if (s->dark_avg_data) {
  4062. int elements_per_line = calib_format.pixel_per_line * calib_format.channels;
  4063. int i;
  4064. DBG (1, "normal_calibration: dark data present - decreasing white average data\n");
  4065. for (i = 0; i < elements_per_line; ++ i) {
  4066. s->white_avg_data[i] -= s->dark_avg_data[i];
  4067. }
  4068. }
  4069. compute_white_shading_data (s, &calib_format, s->white_avg_data);
  4070. status = set_calib_data (s, &calib_format,
  4071. s->dark_avg_data, s->white_avg_data);
  4072. free (calib_tmp_data);
  4073. return status;
  4074. }
  4075. /* next was taken from the GIMP and is a bit modifyed ... ;-)
  4076. * original Copyright (C) 1995 Spencer Kimball and Peter Mattis
  4077. */
  4078. static double
  4079. brightness_contrast_func (double brightness, double contrast, double value)
  4080. {
  4081. double nvalue;
  4082. double power;
  4083. /* apply brightness */
  4084. if (brightness < 0.0)
  4085. value = value * (1.0 + brightness);
  4086. else
  4087. value = value + ((1.0 - value) * brightness);
  4088. /* apply contrast */
  4089. if (contrast < 0.0)
  4090. {
  4091. if (value > 0.5)
  4092. nvalue = 1.0 - value;
  4093. else
  4094. nvalue = value;
  4095. if (nvalue < 0.0)
  4096. nvalue = 0.0;
  4097. nvalue = 0.5 * pow (nvalue * 2.0 , (double) (1.0 + contrast));
  4098. if (value > 0.5)
  4099. value = 1.0 - nvalue;
  4100. else
  4101. value = nvalue;
  4102. }
  4103. else
  4104. {
  4105. if (value > 0.5)
  4106. nvalue = 1.0 - value;
  4107. else
  4108. nvalue = value;
  4109. if (nvalue < 0.0)
  4110. nvalue = 0.0;
  4111. power = (contrast == 1.0) ? 127 : 1.0 / (1.0 - contrast);
  4112. nvalue = 0.5 * pow (2.0 * nvalue, power);
  4113. if (value > 0.5)
  4114. value = 1.0 - nvalue;
  4115. else
  4116. value = nvalue;
  4117. }
  4118. return value;
  4119. }
  4120. static SANE_Status
  4121. send_gamma (Avision_Scanner* s)
  4122. {
  4123. Avision_Device* dev = s->hw;
  4124. SANE_Status status = SANE_STATUS_GOOD;
  4125. int invert_table = 0;
  4126. size_t gamma_table_raw_size;
  4127. size_t gamma_table_size;
  4128. size_t gamma_values;
  4129. struct command_send scmd;
  4130. uint8_t *gamma_data;
  4131. int color; /* current color */
  4132. size_t i; /* big table index */
  4133. size_t j; /* little table index */
  4134. size_t k; /* big table sub index */
  4135. double v1, v2;
  4136. double brightness;
  4137. double contrast;
  4138. if (dev->inquiry_asic_type != AV_ASIC_OA980)
  4139. invert_table = (s->c_mode == AV_THRESHOLDED) || (s->c_mode == AV_DITHERED);
  4140. switch (dev->inquiry_asic_type)
  4141. {
  4142. case AV_ASIC_Cx:
  4143. case AV_ASIC_C1: /* from avision code */
  4144. gamma_table_raw_size = 4096;
  4145. gamma_table_size = 2048;
  4146. break;
  4147. case AV_ASIC_C5:
  4148. gamma_table_raw_size = 256;
  4149. gamma_table_size = 256;
  4150. break;
  4151. case AV_ASIC_C6: /* SPEC claims: 256 ... ? */
  4152. case AV_ASIC_C7:
  4153. gamma_table_raw_size = 512;
  4154. gamma_table_size = 512;
  4155. break;
  4156. case AV_ASIC_OA980:
  4157. gamma_table_raw_size = 4096;
  4158. gamma_table_size = 4096;
  4159. break;
  4160. case AV_ASIC_OA982:
  4161. gamma_table_raw_size = 256;
  4162. gamma_table_size = 256;
  4163. break;
  4164. default:
  4165. gamma_table_raw_size = gamma_table_size = 4096;
  4166. }
  4167. gamma_values = gamma_table_size / 256;
  4168. DBG (3, "send_gamma: table_raw_size: %lu, table_size: %lu\n",
  4169. (u_long) gamma_table_raw_size, (u_long) gamma_table_size);
  4170. DBG (3, "send_gamma: values: %lu, invert_table: %d\n",
  4171. (u_long) gamma_values, invert_table);
  4172. /* prepare for emulating contrast, brightness ... via the gamma-table */
  4173. brightness = SANE_UNFIX (s->val[OPT_BRIGHTNESS].w);
  4174. brightness /= 100;
  4175. contrast = SANE_UNFIX (s->val[OPT_CONTRAST].w);
  4176. contrast /= 100;
  4177. DBG (3, "send_gamma: brightness: %f, contrast: %f\n", brightness, contrast);
  4178. gamma_data = malloc (gamma_table_raw_size);
  4179. if (!gamma_data)
  4180. return SANE_STATUS_NO_MEM;
  4181. memset (&scmd, 0, sizeof (scmd) );
  4182. scmd.opc = AVISION_SCSI_SEND;
  4183. scmd.datatypecode = 0x81; /* 0x81 for download gamma table */
  4184. set_triple (scmd.transferlen, gamma_table_raw_size);
  4185. for (color = 0; color < 3 && status == SANE_STATUS_GOOD; ++ color)
  4186. {
  4187. /* color: 0=red; 1=green; 2=blue */
  4188. set_double (scmd.datatypequal, color);
  4189. i = 0; /* big table index */
  4190. for (j = 0; j < 256; ++ j) /* little table index */
  4191. {
  4192. /* calculate mode dependent values v1 and v2
  4193. * v1 <- current value for table
  4194. * v2 <- next value for table (for interpolation)
  4195. */
  4196. switch (s->c_mode)
  4197. {
  4198. case AV_TRUECOLOR:
  4199. case AV_TRUECOLOR12:
  4200. case AV_TRUECOLOR16:
  4201. {
  4202. v1 = (double) s->gamma_table [1 + color][j];
  4203. if (j == 255)
  4204. v2 = (double) v1;
  4205. else
  4206. v2 = (double) s->gamma_table [1 + color][j + 1];
  4207. }
  4208. break;
  4209. default:
  4210. /* for all other modes: */
  4211. {
  4212. v1 = (double) s->gamma_table [0][j];
  4213. if (j == 255)
  4214. v2 = (double) v1;
  4215. else
  4216. v2 = (double) s->gamma_table [0][j + 1];
  4217. }
  4218. } /*end switch */
  4219. /* Emulate brightness and contrast (at least the Avision AV6[2,3]0
  4220. * as well as many others do not have a hardware implementation,
  4221. * --$. The function was taken from the GIMP source - maybe I'll
  4222. * optimize it in the future (when I have spare time). */
  4223. v1 /= 255;
  4224. v2 /= 255;
  4225. v1 = (brightness_contrast_func (brightness, contrast, v1) );
  4226. v2 = (brightness_contrast_func (brightness, contrast, v2) );
  4227. v1 *= 255;
  4228. v2 *= 255;
  4229. if (invert_table) {
  4230. v1 = 255 - v1;
  4231. v2 = 255 - v2;
  4232. if (v1 <= 0)
  4233. v1 = 0;
  4234. if (v2 <= 0)
  4235. v2 = 0;
  4236. }
  4237. for (k = 0; k < gamma_values; ++ k, ++ i) {
  4238. gamma_data [i] = (uint8_t)
  4239. (((v1 * (gamma_values - k)) + (v2 * k) ) / (double) gamma_values);
  4240. }
  4241. }
  4242. /* fill the gamma table - (e.g.) if 11bit (old protocol) table */
  4243. {
  4244. size_t t_i = i-1;
  4245. if (i < gamma_table_raw_size) {
  4246. DBG (4, "send_gamma: (old protocol) - filling the table.\n");
  4247. for ( ; i < gamma_table_raw_size; ++ i)
  4248. gamma_data [i] = gamma_data [t_i];
  4249. }
  4250. }
  4251. DBG (4, "send_gamma: sending %lu bytes gamma table.\n",
  4252. (u_long) gamma_table_raw_size);
  4253. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  4254. gamma_data, gamma_table_raw_size, 0, 0);
  4255. if (status != SANE_STATUS_GOOD) {
  4256. DBG (1, "send_gamma: gamma table upload failed: %s\n",
  4257. sane_strstatus (status));
  4258. }
  4259. }
  4260. free (gamma_data);
  4261. return status;
  4262. }
  4263. static SANE_Status
  4264. send_3x3_matrix (Avision_Scanner* s)
  4265. {
  4266. SANE_Status status;
  4267. #define SIGN_BIT 0x1000
  4268. #define INT_PART 10
  4269. struct matrix_cmd
  4270. {
  4271. struct command_send scmd;
  4272. struct matrix_3x3 matrix;
  4273. } cmd;
  4274. /* 04 00 00 00 00 00
  4275. 00 00 04 00 00 00
  4276. 00 00 00 00 04 00 */
  4277. int i, a_i;
  4278. static const double c5_matrix[] =
  4279. { 1.000, 0.000, 0.000,
  4280. 0.000, 1.000, 0.000,
  4281. 0.000, 0.000, 1.000 };
  4282. double a_f, b_f;
  4283. uint16_t m;
  4284. DBG (3, "send_3x3_matrix:\n");
  4285. memset (&cmd, 0, sizeof (cmd));
  4286. for (i = 0; i < 9; i++)
  4287. {
  4288. m = 0;
  4289. a_f = c5_matrix[i];
  4290. if (a_f < 0) {
  4291. m |= SIGN_BIT;
  4292. a_f = -a_f;
  4293. }
  4294. a_i = (int) a_f; /* integer */
  4295. b_f = a_f - (double) a_i; /* float */
  4296. m |= ((a_i & 0x3) << INT_PART);
  4297. m |= (uint16_t) (b_f * 1024);
  4298. set_double (((uint8_t*)(&cmd.matrix.v[i])), m);
  4299. }
  4300. cmd.scmd.opc = AVISION_SCSI_SEND;
  4301. cmd.scmd.datatypecode = 0x83; /* 0x83 for 3x3 color matrix */
  4302. set_triple (cmd.scmd.transferlen, sizeof (struct matrix_3x3));
  4303. if (1) {
  4304. DBG (3, "send_3x3_matrix: sending matrix split into two commands\n");
  4305. status = avision_cmd (&s->av_con, &cmd.scmd, sizeof (cmd.scmd),
  4306. &cmd.matrix, sizeof(cmd.matrix), 0, 0);
  4307. }
  4308. else {
  4309. DBG (3, "send_3x3_matrix: sending matrix in one command\n");
  4310. status = avision_cmd (&s->av_con, &cmd, sizeof (cmd), 0, 0, 0, 0);
  4311. }
  4312. return status;
  4313. }
  4314. static SANE_Status
  4315. get_acceleration_info (Avision_Scanner* s, struct acceleration_info* info)
  4316. {
  4317. SANE_Status status;
  4318. struct command_read rcmd;
  4319. uint8_t result [24];
  4320. size_t size;
  4321. DBG (3, "get_acceleration_info:\n");
  4322. size = sizeof (result);
  4323. memset (&rcmd, 0, sizeof (rcmd));
  4324. rcmd.opc = AVISION_SCSI_READ;
  4325. rcmd.datatypecode = 0x6c; /* get acceleration information */
  4326. set_double (rcmd.datatypequal, s->hw->data_dq);
  4327. set_triple (rcmd.transferlen, size);
  4328. DBG (3, "get_acceleration_info: read_data: %lu bytes\n", (u_long) size);
  4329. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, result, &size);
  4330. if (status != SANE_STATUS_GOOD || size != sizeof (result) ) {
  4331. DBG (1, "get_acceleratoin_info: read accel. info failed (%s)\n",
  4332. sane_strstatus (status) );
  4333. return status;
  4334. }
  4335. debug_print_accel_info (3, "get_acceleration_info", result);
  4336. info->total_steps = get_double (&(result[0]));
  4337. info->stable_steps = get_double (&(result[2]));
  4338. info->table_units = get_quad (&(result[4]));
  4339. info->base_units = get_quad (&(result[8]));
  4340. info->start_speed = get_double (&(result[12]));
  4341. info->target_speed = get_double (&(result[14]));
  4342. info->ability = result[16];
  4343. info->table_count = result[17];
  4344. return SANE_STATUS_GOOD;
  4345. }
  4346. static SANE_Status
  4347. send_acceleration_table (Avision_Scanner* s)
  4348. {
  4349. SANE_Status status;
  4350. struct command_send scmd;
  4351. int table = 0;
  4352. int i;
  4353. struct acceleration_info accel_info = accel_info;
  4354. uint8_t* table_data;
  4355. DBG (3, "send_acceleration_table:\n");
  4356. do {
  4357. status = get_acceleration_info (s, &accel_info);
  4358. if (accel_info.table_count == 0) {
  4359. DBG (3, "send_acceleration_table: device does not need tables\n");
  4360. return SANE_STATUS_GOOD;
  4361. }
  4362. if (accel_info.target_speed > accel_info.start_speed ||
  4363. accel_info.target_speed == 0 ||
  4364. accel_info.total_steps <= accel_info.stable_steps) {
  4365. DBG (1, "send_acceleration_table: table does not look right.\n");
  4366. return SANE_STATUS_INVAL;
  4367. }
  4368. if (accel_info.ability != 0) {
  4369. DBG (1, "send_acceleration_table: ability non-zero - insert code\n");
  4370. return SANE_STATUS_INVAL;
  4371. }
  4372. /* so far I assume we have one byte tables as used in the C6 ASIC ... */
  4373. table_data = malloc (accel_info.total_steps + 1000);
  4374. memset (&scmd, 0x00, sizeof (scmd));
  4375. scmd.opc = AVISION_SCSI_SEND;
  4376. scmd.datatypecode = 0x6c; /* send acceleration table */
  4377. set_double (scmd.datatypequal, table);
  4378. set_triple (scmd.transferlen, accel_info.total_steps);
  4379. /* contruct the table - Warning: This code is derived from Avision
  4380. sample code and is a bit scarry! I have no idea why the scanner
  4381. needs such a dumb table and also do not know /why/ it has to be
  4382. constructed this way. "Works for me" -ReneR */
  4383. {
  4384. float low_lim = 0.001;
  4385. float up_lim = 1.0;
  4386. uint16_t accel_steps = accel_info.total_steps - accel_info.stable_steps + 1;
  4387. /* acceleration ramp */
  4388. while ((up_lim - low_lim) > 0.0001)
  4389. {
  4390. float mid = (up_lim + low_lim) / 2; /* accel rate */
  4391. uint16_t now_count = accel_info.start_speed;
  4392. uint16_t i = 0;
  4393. float now_count_f = now_count;
  4394. table_data [i++] = (uint8_t) accel_info.start_speed;
  4395. while (now_count != accel_info.target_speed)
  4396. {
  4397. now_count_f = now_count_f - (now_count_f -
  4398. accel_info.target_speed) * mid;
  4399. now_count = (uint16_t)(now_count_f + 0.5);
  4400. table_data[i++] = (uint8_t) now_count;
  4401. }
  4402. if (i == accel_steps)
  4403. break;
  4404. if (i > accel_steps)
  4405. low_lim = mid;
  4406. else
  4407. up_lim = mid;
  4408. }
  4409. /* fill stable steps */
  4410. for (i = accel_steps; i < accel_info.total_steps; i++)
  4411. table_data [i] = table_data [i-1];
  4412. debug_print_hex_raw (5, "send_acceleration_table: first pass:\n",
  4413. table_data, accel_info.total_steps);
  4414. /* maybe post fix-up */
  4415. {
  4416. int add_count;
  4417. /* count total steps in table */
  4418. int table_total = 0;
  4419. for (i = 0; i < accel_info.total_steps; i++)
  4420. table_total += table_data [i];
  4421. i = 0;
  4422. if (((table_total * accel_info.table_units) % accel_info.base_units) == 0)
  4423. add_count = 0;
  4424. else
  4425. add_count = (accel_info.base_units -
  4426. ((table_total*accel_info.table_units) % accel_info.base_units))
  4427. / accel_info.table_units;
  4428. /* add_count should not be bigger than 255 */
  4429. if (add_count > 255) {
  4430. DBG (1, "send_acceleration_table: add_count limitted, was: %d\n", add_count);
  4431. add_count = 255;
  4432. }
  4433. for (i = 0; i < accel_info.total_steps - 1 && add_count > 0; i++)
  4434. {
  4435. uint16_t temp_count = 255 - table_data [i];
  4436. temp_count = temp_count > add_count ? add_count : temp_count;
  4437. table_data [i] += (uint8_t) temp_count;
  4438. add_count -= temp_count;
  4439. }
  4440. }
  4441. }
  4442. debug_print_hex_raw (5, "send_acceleration_table: fixed up:\n",
  4443. table_data, accel_info.total_steps);
  4444. /* decrease all by one ... */
  4445. for (i = 0; i < accel_info.total_steps; i++) {
  4446. table_data[i]--;
  4447. }
  4448. DBG (1, "send_acceleration_table: sending table %d\n", table);
  4449. debug_print_hex_raw (5, "send_acceleration_table: final:\n",
  4450. table_data, accel_info.total_steps);
  4451. status = avision_cmd (&s->av_con, &scmd, sizeof (scmd),
  4452. (char*) table_data, accel_info.total_steps,
  4453. 0, 0);
  4454. if (status != SANE_STATUS_GOOD) {
  4455. DBG (3, "send_acceleration_table: send_data failed (%s)\n",
  4456. sane_strstatus (status));
  4457. }
  4458. free (table_data); table_data = 0;
  4459. table++;
  4460. } while (table < accel_info.table_count);
  4461. return status;
  4462. }
  4463. static SANE_Status
  4464. set_window (Avision_Scanner* s)
  4465. {
  4466. Avision_Device* dev = s->hw;
  4467. SANE_Status status;
  4468. int base_dpi_abs, base_dpi_rel;
  4469. int transferlen;
  4470. int paralen;
  4471. int bytes_per_line;
  4472. int line_count;
  4473. struct {
  4474. struct command_set_window cmd;
  4475. struct command_set_window_window window;
  4476. } cmd;
  4477. DBG (1, "set_window:\n");
  4478. /* plain old scanners, the C3 ASIC HP 53xx and the C6 ASIC HP 74xx
  4479. and up do use 1200 as base - only the C5 differs */
  4480. switch (dev->inquiry_asic_type) {
  4481. case AV_ASIC_C5:
  4482. base_dpi_abs = 1200;
  4483. /* round down to the next multiple of 300 */
  4484. base_dpi_rel = s->avdimen.hw_xres - s->avdimen.hw_xres % 300;
  4485. if (base_dpi_rel > dev->inquiry_optical_res)
  4486. base_dpi_rel = dev->inquiry_optical_res;
  4487. else if (s->avdimen.hw_xres <= 150)
  4488. base_dpi_rel = 150;
  4489. break;
  4490. default:
  4491. base_dpi_abs = 1200;
  4492. base_dpi_rel = 1200;
  4493. }
  4494. DBG (2, "set_window: base_dpi_abs: %d, base_dpi_rel: %d\n", base_dpi_abs, base_dpi_rel);
  4495. /* wipe out anything */
  4496. memset (&cmd, 0, sizeof (cmd) );
  4497. cmd.window.descriptor.winid = AV_WINID; /* normally defined to be zero */
  4498. /* optional parameter length to use */
  4499. paralen = sizeof (cmd.window.avision) - sizeof (cmd.window.avision.type);
  4500. DBG (2, "set_window: base paralen: %d\n", paralen);
  4501. if (dev->hw->feature_type & AV_FUJITSU)
  4502. paralen += sizeof (cmd.window.avision.type.fujitsu);
  4503. else if (!dev->inquiry_new_protocol)
  4504. paralen += sizeof (cmd.window.avision.type.old);
  4505. else
  4506. paralen += sizeof (cmd.window.avision.type.normal);
  4507. DBG (2, "set_window: final paralen: %d\n", paralen);
  4508. transferlen = sizeof (cmd.window)
  4509. - sizeof (cmd.window.avision) + paralen;
  4510. DBG (2, "set_window: transferlen: %d\n", transferlen);
  4511. /* command setup */
  4512. cmd.cmd.opc = AVISION_SCSI_SET_WINDOW;
  4513. set_triple (cmd.cmd.transferlen, transferlen);
  4514. set_double (cmd.window.header.desclen,
  4515. sizeof (cmd.window.descriptor) + paralen);
  4516. /* resolution parameters */
  4517. set_double (cmd.window.descriptor.xres, s->avdimen.hw_xres);
  4518. set_double (cmd.window.descriptor.yres, s->avdimen.hw_yres);
  4519. /* upper left corner x/y as well as width/length in inch * base_dpi
  4520. - avdimen are world pixels */
  4521. set_quad (cmd.window.descriptor.ulx, s->avdimen.tlx * base_dpi_abs / s->avdimen.hw_xres);
  4522. set_quad (cmd.window.descriptor.uly, s->avdimen.tly * base_dpi_abs / s->avdimen.hw_yres);
  4523. set_quad (cmd.window.descriptor.width,
  4524. s->avdimen.hw_pixels_per_line * base_dpi_rel / s->avdimen.hw_xres + 1);
  4525. line_count = s->avdimen.hw_lines + 2 * s->avdimen.line_difference + s->avdimen.rear_offset;
  4526. set_quad (cmd.window.descriptor.length,
  4527. line_count * base_dpi_rel / s->avdimen.hw_yres + 1);
  4528. /* interlaced duplex scans are twice as long */
  4529. if (s->avdimen.interlaced_duplex && dev->scanner_type != AV_FILM) {
  4530. DBG (2, "set_window: interlaced duplex scan, doubled line count\n");
  4531. line_count *= 2;
  4532. }
  4533. bytes_per_line = s->avdimen.hw_bytes_per_line;
  4534. set_double (cmd.window.avision.line_width, bytes_per_line);
  4535. set_double (cmd.window.avision.line_count, line_count);
  4536. /* here go the most significant bits if bigger than 16 bit */
  4537. if (dev->inquiry_new_protocol && !(dev->hw->feature_type & AV_FUJITSU) ) {
  4538. DBG (2, "set_window: large data-transfer support (>16bit)!\n");
  4539. cmd.window.avision.type.normal.line_width_msb =
  4540. bytes_per_line >> 16;
  4541. cmd.window.avision.type.normal.line_count_msb =
  4542. line_count >> 16;
  4543. }
  4544. if (dev->inquiry_background_raster)
  4545. cmd.window.avision.type.normal.background_lines = s->val[OPT_BACKGROUND].w;
  4546. /* scanner should use our line-width and count */
  4547. SET_BIT (cmd.window.avision.bitset1, 6);
  4548. /* set speed */
  4549. cmd.window.avision.bitset1 |= s->val[OPT_SPEED].w & 0x07; /* only 3 bit */
  4550. /* ADF scan? */
  4551. DBG (3, "set_window: source mode %d source mode dim %d\n",
  4552. s->source_mode, s->source_mode_dim);
  4553. {
  4554. int adf_mode = 0; /* offset by 1 to save a is_adf bool */
  4555. switch (s->source_mode) {
  4556. case AV_ADF:
  4557. adf_mode = 1;
  4558. break;
  4559. case AV_ADF_REAR:
  4560. adf_mode = 2;
  4561. break;
  4562. case AV_ADF_DUPLEX:
  4563. adf_mode = 3;
  4564. break;
  4565. default:
  4566. ; /* silence GCC */
  4567. }
  4568. if (adf_mode) {
  4569. DBG (3, "set_window: filling ADF bits\n");
  4570. SET_BIT (cmd.window.avision.bitset1, 7);
  4571. adf_mode--;
  4572. /* normal, interlaced duplex scanners */
  4573. if (dev->inquiry_duplex_interlaced) {
  4574. DBG (3, "set_window: interlaced duplex type\n");
  4575. cmd.window.avision.type.normal.bitset3 |= (adf_mode << 3);
  4576. }
  4577. else /* HP 2-pass duplex */
  4578. {
  4579. if (adf_mode) /* if duplex */
  4580. {
  4581. DBG (3, "set_window: non-interlaced duplex type (HP)\n");
  4582. /* MIRR 0x04 | FLIP 0x02 | DPLX 0x01 ... */
  4583. cmd.window.avision.type.normal.bitset3 |= 7;
  4584. }
  4585. }
  4586. }
  4587. }
  4588. /* Newer scanners can utilize this paper length to detect double feeds.
  4589. However some others (DM152) can get confused during media flush if it
  4590. is set? TODO: wire to an option, ... */
  4591. /* if (dev->inquiry_paper_length)
  4592. set_double (cmd.window.descriptor.paper_length, (int)((double)30.0*1200)); */
  4593. if ( !(dev->hw->feature_type & AV_FUJITSU) )
  4594. {
  4595. /* quality scan option switch */
  4596. if (s->val[OPT_QSCAN].w == SANE_TRUE) {
  4597. SET_BIT (cmd.window.avision.type.normal.bitset2, 4);
  4598. }
  4599. /* quality calibration option switch (inverted! if set == speed) */
  4600. if (s->val[OPT_QCALIB].w == SANE_FALSE) {
  4601. SET_BIT (cmd.window.avision.type.normal.bitset2, 3);
  4602. }
  4603. /* transparency option switch */
  4604. if (s->source_mode_dim == AV_TRANSPARENT_DIM) {
  4605. SET_BIT (cmd.window.avision.type.normal.bitset2, 7);
  4606. }
  4607. if (dev->scanner_type == AV_FILM) {
  4608. /* TODO: wire to IR exposure option? */
  4609. cmd.window.avision.type.normal.ir_exposure_time = 100;
  4610. set_double (cmd.window.avision.type.normal.r_exposure_time, s->val[OPT_EXPOSURE].w);
  4611. set_double (cmd.window.avision.type.normal.g_exposure_time, s->val[OPT_EXPOSURE].w);
  4612. set_double (cmd.window.avision.type.normal.b_exposure_time, s->val[OPT_EXPOSURE].w);
  4613. if (s->val[OPT_IR].w)
  4614. cmd.window.avision.type.normal.bitset3 |= (1 << 0);
  4615. if (s->val[OPT_MULTISAMPLE].w)
  4616. cmd.window.avision.type.normal.bitset3 |= (1 << 1);
  4617. }
  4618. }
  4619. /* fixed values */
  4620. cmd.window.descriptor.padding_and_bitset = 3;
  4621. cmd.window.descriptor.vendor_specific = 0xFF;
  4622. cmd.window.descriptor.paralen = paralen; /* R² was: 9, later 14 */
  4623. /* This is normaly unsupported by Avsion scanners, and we do this
  4624. via the gamma table - which works for all devices ... */
  4625. cmd.window.descriptor.threshold = 128;
  4626. cmd.window.descriptor.brightness = 128;
  4627. cmd.window.descriptor.contrast = 128;
  4628. cmd.window.avision.highlight = 0xFF;
  4629. cmd.window.avision.shadow = 0x00;
  4630. /* mode dependant settings */
  4631. switch (s->c_mode)
  4632. {
  4633. case AV_THRESHOLDED:
  4634. cmd.window.descriptor.bpc = 1;
  4635. cmd.window.descriptor.image_comp = 0;
  4636. break;
  4637. case AV_DITHERED:
  4638. cmd.window.descriptor.bpc = 1;
  4639. cmd.window.descriptor.image_comp = 1;
  4640. break;
  4641. case AV_GRAYSCALE:
  4642. cmd.window.descriptor.bpc = 8;
  4643. cmd.window.descriptor.image_comp = 2;
  4644. break;
  4645. case AV_GRAYSCALE12:
  4646. cmd.window.descriptor.bpc = 12;
  4647. cmd.window.descriptor.image_comp = 2;
  4648. break;
  4649. case AV_GRAYSCALE16:
  4650. cmd.window.descriptor.bpc = 16;
  4651. cmd.window.descriptor.image_comp = 2;
  4652. break;
  4653. case AV_TRUECOLOR:
  4654. cmd.window.descriptor.bpc = 8;
  4655. cmd.window.descriptor.image_comp = 5;
  4656. break;
  4657. case AV_TRUECOLOR12:
  4658. cmd.window.descriptor.bpc = 12;
  4659. cmd.window.descriptor.image_comp = 5;
  4660. break;
  4661. case AV_TRUECOLOR16:
  4662. cmd.window.descriptor.bpc = 16;
  4663. cmd.window.descriptor.image_comp = 5;
  4664. break;
  4665. default:
  4666. DBG (1, "Invalid mode. %d\n", s->c_mode);
  4667. return SANE_STATUS_INVAL;
  4668. }
  4669. if (color_mode_is_color (s->c_mode)) {
  4670. cmd.window.avision.bitset1 |= AVISION_FILTER_RGB << 3;
  4671. }
  4672. else {
  4673. if (dev->hw->feature_type & AV_FASTER_WITH_FILTER)
  4674. cmd.window.avision.bitset1 |= AVISION_FILTER_GREEN << 3;
  4675. else
  4676. cmd.window.avision.bitset1 |= AVISION_FILTER_NONE << 3;
  4677. }
  4678. debug_print_window_descriptor (5, "set_window", &(cmd.window));
  4679. DBG (3, "set_window: sending command. Bytes: %d\n", transferlen);
  4680. status = avision_cmd (&s->av_con, &cmd, sizeof (cmd.cmd),
  4681. &(cmd.window), transferlen, 0, 0);
  4682. return status;
  4683. }
  4684. static SANE_Status
  4685. get_background_raster (Avision_Scanner* s)
  4686. {
  4687. const int debug = 0;
  4688. Avision_Device* dev = s->hw;
  4689. SANE_Status status;
  4690. struct command_read rcmd;
  4691. size_t size;
  4692. int bytes_per_line, i;
  4693. const int bpp = color_mode_is_color (s->c_mode) ? 3 : 1;
  4694. const int lines = s->val[OPT_BACKGROUND].w * (s->avdimen.interlaced_duplex ? 2 : 1);
  4695. uint8_t* background = NULL;
  4696. DBG (1, "get_background_raster:\n");
  4697. if (lines == 0) {
  4698. DBG (1, "get_background_raster: no background requested\n");
  4699. return SANE_STATUS_GOOD;
  4700. }
  4701. /* full width, always :-(, duplex *2 for front and rear */
  4702. bytes_per_line = dev->inquiry_background_raster_pixel *
  4703. s->avdimen.hw_xres / dev->inquiry_optical_res;
  4704. bytes_per_line *= bpp;
  4705. DBG (3, "get_background_raster: native raster pixels: %d, raster bytes_per_line: %d\n",
  4706. dev->inquiry_background_raster_pixel, bytes_per_line);
  4707. /* according to spec only 8-bit gray or color, TODO: test for bi-level scans */
  4708. size = bytes_per_line * lines;
  4709. DBG (3, "get_background_raster: buffer size: %ld\n", (long)size);
  4710. background = s->background_raster = realloc (s->background_raster, size);
  4711. if (!background)
  4712. return SANE_STATUS_NO_MEM;
  4713. memset (&rcmd, 0, sizeof (rcmd));
  4714. rcmd.opc = AVISION_SCSI_READ;
  4715. rcmd.datatypecode = 0x9b; /* get background raster */
  4716. set_double (rcmd.datatypequal, s->hw->data_dq);
  4717. /* Ok, well - this part is very messy. The AV122 and DM152 appear to
  4718. contain differently buggy ASICs. The only combination I found to
  4719. at least get a correct front raster out of them is to read it
  4720. line by line and then every second line appears to be valid front
  4721. data, ... */
  4722. /* read the raster data */
  4723. for (i = 0; i < lines;)
  4724. {
  4725. uint8_t* dst_raster = background + bytes_per_line * i;
  4726. /* read stripe by stripe, or all in one chunk */
  4727. size_t this_read, read_size;
  4728. int this_lines;
  4729. if (dev->hw->feature_type & AV_2ND_LINE_INTERLACED) {
  4730. if (dev->hw->feature_type & AV_BACKGROUND_QUIRK)
  4731. this_lines = 1;
  4732. else
  4733. this_lines = lines;
  4734. }
  4735. else {
  4736. this_lines = s->val[OPT_BACKGROUND].w;
  4737. }
  4738. this_read = bytes_per_line * this_lines;
  4739. DBG (3, "get_background_raster: line: %d, lines: %d, %lu bytes\n",
  4740. i, this_lines, (u_long) this_read);
  4741. set_triple (rcmd.transferlen, this_read);
  4742. read_size = this_read;
  4743. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, dst_raster, &read_size);
  4744. if (status != SANE_STATUS_GOOD || read_size != this_read) {
  4745. DBG (1, "get_background_raster: read raster failed (%s)\n",
  4746. sane_strstatus (status) );
  4747. return status;
  4748. }
  4749. i += this_lines;
  4750. }
  4751. /* dump raw result while debugging */
  4752. if (debug)
  4753. {
  4754. FILE* f = NULL;
  4755. f = fopen ("background-raw.pnm", "w");
  4756. write_pnm_header (f, (color_mode_is_color (s->c_mode) ? AV_TRUECOLOR : AV_GRAYSCALE), 8,
  4757. bytes_per_line / bpp, lines);
  4758. fwrite (background, 1, bytes_per_line * lines, f);
  4759. fclose (f);
  4760. }
  4761. /* line-pack - move to unified processing flow, later */
  4762. if (dev->inquiry_needs_line_pack)
  4763. {
  4764. /* TODO: add 16bit per sample code? */
  4765. int l, p;
  4766. uint8_t* tmp_data = malloc (bytes_per_line);
  4767. for (l = 0; l < lines; ++l)
  4768. {
  4769. uint8_t* out_data = tmp_data;
  4770. uint8_t* r_ptr = background + (bytes_per_line * l);
  4771. uint8_t* g_ptr = r_ptr + bytes_per_line / bpp;
  4772. uint8_t* b_ptr = g_ptr + bytes_per_line / bpp;
  4773. for (p = 0; p < bytes_per_line;) {
  4774. out_data [p++] = *(r_ptr++);
  4775. out_data [p++] = *(g_ptr++);
  4776. out_data [p++] = *(b_ptr++);
  4777. }
  4778. memcpy (background + (bytes_per_line * l), tmp_data, bytes_per_line);
  4779. }
  4780. free (tmp_data);
  4781. } /* end line pack */
  4782. /* deinterlace? */
  4783. if (s->avdimen.interlaced_duplex && dev->hw->feature_type & AV_2ND_LINE_INTERLACED)
  4784. {
  4785. uint8_t* deinterlaced = malloc (size * 2);
  4786. if (!deinterlaced)
  4787. return SANE_STATUS_NO_MEM;
  4788. for (i = 0; i < lines; ++i)
  4789. {
  4790. int dst_i = i / 2 + (i % 2) * (lines / 2);
  4791. uint8_t* dst_raster; /* just no C99 in SANE :-( */
  4792. uint8_t* src_raster;
  4793. /* for the quirky devices and some resolutions the interlacing differs */
  4794. if (dev->hw->feature_type & AV_BACKGROUND_QUIRK && s->avdimen.hw_xres >= 150)
  4795. dst_i = i / 2 + ((i+1) % 2) * (lines / 2);
  4796. dst_raster = deinterlaced + bytes_per_line * dst_i;
  4797. src_raster = background + bytes_per_line * i;
  4798. DBG(3, "get_background_raster: deinterlaced %d -> %d\n", i, dst_i);
  4799. memcpy(dst_raster, src_raster, bytes_per_line);
  4800. }
  4801. free (background);
  4802. background = s->background_raster = deinterlaced;
  4803. }
  4804. /* dump raw result while debugging */
  4805. for (i = 0; debug && i < (s->avdimen.interlaced_duplex ? 2 : 1); ++i)
  4806. {
  4807. FILE* f = NULL;
  4808. uint8_t* raster = background;
  4809. if (i == 0) {
  4810. f = fopen ("background.pnm", "w");
  4811. }
  4812. else {
  4813. f = fopen ("background-rear.pnm", "w");
  4814. raster += bytes_per_line * s->val[OPT_BACKGROUND].w;
  4815. }
  4816. write_pnm_header (f, (color_mode_is_color (s->c_mode) ? AV_TRUECOLOR : AV_GRAYSCALE), 8,
  4817. bytes_per_line / bpp, s->val[OPT_BACKGROUND].w);
  4818. fwrite (raster, 1, bytes_per_line * s->val[OPT_BACKGROUND].w, f);
  4819. fclose (f);
  4820. }
  4821. /* crop from full-width scanlines to scan window */
  4822. {
  4823. uint8_t *dst_ptr, *src_ptr;
  4824. dst_ptr = background;
  4825. src_ptr = background + s->avdimen.tlx * bpp;
  4826. for (i = 0; i < lines; ++i)
  4827. {
  4828. memmove (dst_ptr, src_ptr, s->avdimen.hw_bytes_per_line);
  4829. dst_ptr += s->avdimen.hw_bytes_per_line;
  4830. src_ptr += bytes_per_line;
  4831. }
  4832. }
  4833. /* soft-scale - move to unified processing flow, later */
  4834. if (s->avdimen.hw_xres != s->avdimen.xres)
  4835. {
  4836. const uint8_t* out_data = background;
  4837. uint8_t* dst = background;
  4838. int l;
  4839. for (l = 0; l < lines; ++l)
  4840. {
  4841. const int hwbpl = s->avdimen.hw_bytes_per_line;
  4842. const int sy = l;
  4843. int x;
  4844. for (x = 0; x < s->params.pixels_per_line; ++x)
  4845. {
  4846. const double bx = (-1.0 + s->avdimen.hw_pixels_per_line) * x / s->params.pixels_per_line;
  4847. const int sx = (int)floor(bx);
  4848. const int xdist = (int) ((bx - sx) * 256);
  4849. const int sxx = sx + 1;
  4850. switch (bpp) {
  4851. case 1:
  4852. {
  4853. uint8_t v =
  4854. ( out_data [sy*hwbpl + sx ] * (256-xdist) +
  4855. out_data [sy*hwbpl + sxx] * xdist
  4856. ) / (256);
  4857. *dst++ = v;
  4858. }
  4859. break;
  4860. case 3:
  4861. {
  4862. int c;
  4863. for (c = 0; c < 3; ++c)
  4864. {
  4865. uint8_t v =
  4866. ( out_data [sy*hwbpl + sx*3 + c] * (256-xdist) +
  4867. out_data [sy*hwbpl + sxx*3 + c] * xdist
  4868. ) / (256);
  4869. *dst++ = v;
  4870. }
  4871. }
  4872. break;
  4873. }
  4874. }
  4875. }
  4876. }
  4877. /* dump final result while debugging */
  4878. if (debug) {
  4879. for (i = 0; i < (s->avdimen.interlaced_duplex ? 2 : 1); ++i)
  4880. {
  4881. FILE* f = NULL;
  4882. uint8_t* raster = background;
  4883. if (i == 0) {
  4884. f = fopen ("background-final.pnm", "w");
  4885. }
  4886. else {
  4887. f = fopen ("background-final-rear.pnm", "w");
  4888. raster += s->params.bytes_per_line * s->val[OPT_BACKGROUND].w;
  4889. }
  4890. write_pnm_header (f, (color_mode_is_color (s->c_mode) ? AV_TRUECOLOR : AV_GRAYSCALE), 8,
  4891. s->params.bytes_per_line / bpp, s->val[OPT_BACKGROUND].w);
  4892. fwrite (raster, 1, s->params.bytes_per_line * s->val[OPT_BACKGROUND].w, f);
  4893. fclose (f);
  4894. }
  4895. }
  4896. return SANE_STATUS_GOOD;
  4897. }
  4898. static SANE_Status
  4899. reserve_unit (Avision_Scanner* s)
  4900. {
  4901. char cmd[] =
  4902. {AVISION_SCSI_RESERVE_UNIT, 0, 0, 0, 0, 0};
  4903. SANE_Status status;
  4904. DBG (1, "reserve_unit:\n");
  4905. status = avision_cmd (&s->av_con, cmd, sizeof (cmd), 0, 0, 0, 0);
  4906. return status;
  4907. }
  4908. static SANE_Status
  4909. release_unit (Avision_Scanner* s, int type)
  4910. {
  4911. char cmd[] =
  4912. {AVISION_SCSI_RELEASE_UNIT, 0, 0, 0, 0, 0};
  4913. SANE_Status status;
  4914. DBG (1, "release unit: type: %d\n", type);
  4915. cmd[5] = type; /* latest scanners also allow 1: release paper and 2: end job */
  4916. status = avision_cmd (&s->av_con, cmd, sizeof (cmd), 0, 0, 0, 0);
  4917. return status;
  4918. }
  4919. /* Check if a sheet is present. */
  4920. static SANE_Status
  4921. media_check (Avision_Scanner* s)
  4922. {
  4923. char cmd[] = {AVISION_SCSI_MEDIA_CHECK, 0, 0, 0, 1, 0}; /* 1, 4 */
  4924. SANE_Status status;
  4925. uint8_t result[1]; /* 4 */
  4926. size_t size = sizeof(result);
  4927. status = avision_cmd (&s->av_con, cmd, sizeof (cmd),
  4928. 0, 0, result, &size);
  4929. debug_print_raw (5, "media_check: result\n", result, size);
  4930. if (status == SANE_STATUS_GOOD) {
  4931. if (!(result[0] & 0x1))
  4932. status = SANE_STATUS_NO_DOCS;
  4933. }
  4934. return status;
  4935. }
  4936. static SANE_Status
  4937. flush_media (Avision_Scanner* s)
  4938. {
  4939. Avision_Device* dev = s->hw;
  4940. SANE_Status status;
  4941. if (s->source_mode_dim == AV_ADF_DIM && dev->inquiry_batch_scan)
  4942. {
  4943. DBG (1, "flush_media: flushing pages out of batch scanner\n");
  4944. do {
  4945. status = media_check (s);
  4946. if (status == SANE_STATUS_GOOD) {
  4947. SANE_Status status2 = reserve_unit (s);
  4948. DBG (1, "flush_media: reserve status: %d\n", status2);
  4949. status2 = release_unit (s, 0);
  4950. DBG (1, "flush_media: release status: %d\n", status2);
  4951. }
  4952. } while (status == SANE_STATUS_GOOD);
  4953. }
  4954. return SANE_STATUS_GOOD;
  4955. }
  4956. static SANE_Status
  4957. object_position (Avision_Scanner* s, uint8_t position)
  4958. {
  4959. SANE_Status status;
  4960. uint8_t cmd [10];
  4961. memset (cmd, 0, sizeof (cmd));
  4962. cmd[0] = AVISION_SCSI_OBJECT_POSITION;
  4963. cmd[1] = position;
  4964. DBG (1, "object_position: %d\n", position);
  4965. status = avision_cmd (&s->av_con, cmd, sizeof(cmd), 0, 0, 0, 0);
  4966. return status;
  4967. }
  4968. static SANE_Status
  4969. start_scan (Avision_Scanner* s)
  4970. {
  4971. struct command_scan cmd;
  4972. size_t size = sizeof (cmd);
  4973. DBG (3, "start_scan:\n");
  4974. memset (&cmd, 0, sizeof (cmd));
  4975. cmd.opc = AVISION_SCSI_SCAN;
  4976. cmd.transferlen = 1;
  4977. /* AV610C2 in ADF preview mode does not detect the page end (...) */
  4978. if (s->val[OPT_PREVIEW].w == SANE_TRUE && s->hw->inquiry_asic_type != AV_ASIC_C7) {
  4979. SET_BIT(cmd.bitset1,6);
  4980. }
  4981. if (s->val[OPT_QSCAN].w == SANE_TRUE) {
  4982. SET_BIT(cmd.bitset1,7);
  4983. }
  4984. DBG (3, "start_scan: sending command. Bytes: %lu\n", (u_long) size);
  4985. return avision_cmd (&s->av_con, &cmd, size, 0, 0, 0, 0);
  4986. }
  4987. static SANE_Status
  4988. do_eof (Avision_Scanner *s)
  4989. {
  4990. int exit_status;
  4991. DBG (3, "do_eof:\n");
  4992. /* we do not scan anymore */
  4993. s->prepared = s->scanning = SANE_FALSE;
  4994. /* we can now mark the rear data as valid */
  4995. if (s->avdimen.interlaced_duplex) {
  4996. DBG (3, "do_eof: toggling duplex rear data valid\n");
  4997. s->duplex_rear_valid = !s->duplex_rear_valid;
  4998. DBG (3, "do_eof: duplex rear data valid: %x\n",
  4999. s->duplex_rear_valid);
  5000. }
  5001. if (s->read_fds >= 0) {
  5002. close (s->read_fds);
  5003. s->read_fds = -1;
  5004. }
  5005. /* join our processes - without a wait() you will produce defunct
  5006. childs */
  5007. sanei_thread_waitpid (s->reader_pid, &exit_status);
  5008. s->reader_pid = -1;
  5009. DBG (3, "do_eof: returning %d\n", exit_status);
  5010. return (SANE_Status)exit_status;
  5011. }
  5012. static SANE_Status
  5013. do_cancel (Avision_Scanner* s)
  5014. {
  5015. DBG (3, "do_cancel:\n");
  5016. s->prepared = s->scanning = SANE_FALSE;
  5017. s->duplex_rear_valid = SANE_FALSE;
  5018. s->page = 0;
  5019. if (s->reader_pid != -1) {
  5020. int exit_status;
  5021. /* ensure child knows it's time to stop: */
  5022. sanei_thread_kill (s->reader_pid);
  5023. sanei_thread_waitpid (s->reader_pid, &exit_status);
  5024. s->reader_pid = -1;
  5025. }
  5026. return SANE_STATUS_CANCELLED;
  5027. }
  5028. static SANE_Status
  5029. read_data (Avision_Scanner* s, SANE_Byte* buf, size_t* count)
  5030. {
  5031. struct command_read rcmd;
  5032. SANE_Status status;
  5033. DBG (9, "read_data: %lu\n", (u_long) *count);
  5034. memset (&rcmd, 0, sizeof (rcmd));
  5035. rcmd.opc = AVISION_SCSI_READ;
  5036. rcmd.datatypecode = 0x00; /* read image data */
  5037. set_double (rcmd.datatypequal, s->hw->data_dq);
  5038. set_triple (rcmd.transferlen, *count);
  5039. status = avision_cmd (&s->av_con, &rcmd, sizeof (rcmd), 0, 0, buf, count);
  5040. return status;
  5041. }
  5042. static SANE_Status
  5043. init_options (Avision_Scanner* s)
  5044. {
  5045. Avision_Device* dev = s->hw;
  5046. int i;
  5047. DBG (3, "init_options:\n");
  5048. memset (s->opt, 0, sizeof (s->opt));
  5049. memset (s->val, 0, sizeof (s->val));
  5050. for (i = 0; i < NUM_OPTIONS; ++ i) {
  5051. s->opt[i].size = sizeof (SANE_Word);
  5052. s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  5053. }
  5054. /* Init the SANE option from the scanner inquiry data */
  5055. dev->x_range.max = SANE_FIX ( (int)dev->inquiry_x_ranges[s->source_mode_dim]);
  5056. dev->x_range.quant = 0;
  5057. dev->y_range.max = SANE_FIX ( (int)dev->inquiry_y_ranges[s->source_mode_dim]);
  5058. dev->y_range.quant = 0;
  5059. switch (dev->inquiry_asic_type) {
  5060. case AV_ASIC_C2:
  5061. dev->dpi_range.min = 100;
  5062. break;
  5063. case AV_ASIC_C5:
  5064. dev->dpi_range.min = 80;
  5065. break;
  5066. case AV_ASIC_C6: /* TODO: AV610 in ADF mode does not scan less than 180 or so */
  5067. dev->dpi_range.min = 50;
  5068. break;
  5069. case AV_ASIC_C7: /* AV610C2 imperically tested out */
  5070. dev->dpi_range.min = 75;
  5071. break;
  5072. default:
  5073. dev->dpi_range.min = 50;
  5074. }
  5075. DBG (1, "init_options: dpi_range.min set to %d\n", dev->dpi_range.min);
  5076. dev->dpi_range.quant = 1; /* any, including 72, 144, etc. */
  5077. dev->dpi_range.max = dev->inquiry_max_res;
  5078. dev->speed_range.min = (SANE_Int)0;
  5079. dev->speed_range.max = (SANE_Int)4;
  5080. dev->speed_range.quant = (SANE_Int)1;
  5081. s->opt[OPT_NUM_OPTS].name = "";
  5082. s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
  5083. s->opt[OPT_NUM_OPTS].desc = "";
  5084. s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
  5085. s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
  5086. s->opt[OPT_NUM_OPTS].size = sizeof(SANE_TYPE_INT);
  5087. s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
  5088. /* "Mode" group: */
  5089. s->opt[OPT_MODE_GROUP].title = SANE_TITLE_SCAN_MODE;
  5090. s->opt[OPT_MODE_GROUP].desc = ""; /* for groups only title and type are valid */
  5091. s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
  5092. s->opt[OPT_MODE_GROUP].cap = 0;
  5093. s->opt[OPT_MODE_GROUP].size = 0;
  5094. s->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
  5095. /* color mode */
  5096. s->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
  5097. s->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
  5098. s->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
  5099. s->opt[OPT_MODE].type = SANE_TYPE_STRING;
  5100. s->opt[OPT_MODE].size = max_string_size (dev->color_list);
  5101. s->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
  5102. s->opt[OPT_MODE].constraint.string_list = dev->color_list;
  5103. s->val[OPT_MODE].s = strdup (dev->color_list[dev->color_list_default]);
  5104. s->c_mode = match_color_mode (dev, s->val[OPT_MODE].s);
  5105. /* source mode */
  5106. s->opt[OPT_SOURCE].name = SANE_NAME_SCAN_SOURCE;
  5107. s->opt[OPT_SOURCE].title = SANE_TITLE_SCAN_SOURCE;
  5108. s->opt[OPT_SOURCE].desc = SANE_DESC_SCAN_SOURCE;
  5109. s->opt[OPT_SOURCE].type = SANE_TYPE_STRING;
  5110. s->opt[OPT_SOURCE].size = max_string_size(dev->source_list);
  5111. s->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
  5112. s->opt[OPT_SOURCE].constraint.string_list = &dev->source_list[0];
  5113. s->val[OPT_SOURCE].s = strdup(dev->source_list[0]);
  5114. s->source_mode = match_source_mode (dev, s->val[OPT_SOURCE].s);
  5115. s->source_mode_dim = match_source_mode_dim (s->source_mode);
  5116. /* resolution */
  5117. s->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
  5118. s->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
  5119. s->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
  5120. s->opt[OPT_RESOLUTION].type = SANE_TYPE_INT;
  5121. s->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
  5122. s->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
  5123. s->opt[OPT_RESOLUTION].constraint.range = &dev->dpi_range;
  5124. s->val[OPT_RESOLUTION].w = OPT_RESOLUTION_DEFAULT;
  5125. /* preview */
  5126. s->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
  5127. s->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
  5128. s->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
  5129. s->opt[OPT_PREVIEW].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
  5130. s->val[OPT_PREVIEW].w = 0;
  5131. /* speed option */
  5132. s->opt[OPT_SPEED].name = SANE_NAME_SCAN_SPEED;
  5133. s->opt[OPT_SPEED].title = SANE_TITLE_SCAN_SPEED;
  5134. s->opt[OPT_SPEED].desc = SANE_DESC_SCAN_SPEED;
  5135. s->opt[OPT_SPEED].type = SANE_TYPE_INT;
  5136. s->opt[OPT_SPEED].constraint_type = SANE_CONSTRAINT_RANGE;
  5137. s->opt[OPT_SPEED].constraint.range = &dev->speed_range;
  5138. s->val[OPT_SPEED].w = 0;
  5139. if (dev->scanner_type == AV_SHEETFEED)
  5140. s->opt[OPT_SPEED].cap |= SANE_CAP_INACTIVE;
  5141. /* "Geometry" group: */
  5142. s->opt[OPT_GEOMETRY_GROUP].title = "Geometry";
  5143. s->opt[OPT_GEOMETRY_GROUP].desc = ""; /* for groups only title and type are valid */
  5144. s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
  5145. s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
  5146. s->opt[OPT_GEOMETRY_GROUP].size = 0;
  5147. s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
  5148. /* top-left x */
  5149. s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
  5150. s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
  5151. s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
  5152. s->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
  5153. s->opt[OPT_TL_X].unit = SANE_UNIT_MM;
  5154. s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
  5155. s->opt[OPT_TL_X].constraint.range = &dev->x_range;
  5156. s->val[OPT_TL_X].w = 0;
  5157. /* top-left y */
  5158. s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
  5159. s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
  5160. s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
  5161. s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
  5162. s->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
  5163. s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
  5164. s->opt[OPT_TL_Y].constraint.range = &dev->y_range;
  5165. s->val[OPT_TL_Y].w = 0;
  5166. /* bottom-right x */
  5167. s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
  5168. s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
  5169. s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
  5170. s->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
  5171. s->opt[OPT_BR_X].unit = SANE_UNIT_MM;
  5172. s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
  5173. s->opt[OPT_BR_X].constraint.range = &dev->x_range;
  5174. s->val[OPT_BR_X].w = dev->x_range.max;
  5175. /* bottom-right y */
  5176. s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
  5177. s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
  5178. s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
  5179. s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
  5180. s->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
  5181. s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
  5182. s->opt[OPT_BR_Y].constraint.range = &dev->y_range;
  5183. s->val[OPT_BR_Y].w = dev->y_range.max;
  5184. /* overscan top */
  5185. s->opt[OPT_OVERSCAN_TOP].name = "overscan-top";
  5186. s->opt[OPT_OVERSCAN_TOP].title = "Overscan top";
  5187. s->opt[OPT_OVERSCAN_TOP].desc = "The top overscan controls the additional area to scan before the paper is detected.";
  5188. s->opt[OPT_OVERSCAN_TOP].type = SANE_TYPE_FIXED;
  5189. s->opt[OPT_OVERSCAN_TOP].unit = SANE_UNIT_MM;
  5190. s->opt[OPT_OVERSCAN_TOP].constraint_type = SANE_CONSTRAINT_RANGE;
  5191. s->opt[OPT_OVERSCAN_TOP].constraint.range = &overscan_range;
  5192. s->val[OPT_OVERSCAN_TOP].w = SANE_FIX(0);
  5193. /* overscan bottom */
  5194. s->opt[OPT_OVERSCAN_BOTTOM].name = "overscan-bottom";
  5195. s->opt[OPT_OVERSCAN_BOTTOM].title = "Overscan bottom";
  5196. s->opt[OPT_OVERSCAN_BOTTOM].desc = "The bottom overscan controls the additional area to scan after the paper end was detected.";
  5197. s->opt[OPT_OVERSCAN_BOTTOM].type = SANE_TYPE_FIXED;
  5198. s->opt[OPT_OVERSCAN_BOTTOM].unit = SANE_UNIT_MM;
  5199. s->opt[OPT_OVERSCAN_BOTTOM].constraint_type = SANE_CONSTRAINT_RANGE;
  5200. s->opt[OPT_OVERSCAN_BOTTOM].constraint.range = &overscan_range;
  5201. s->val[OPT_OVERSCAN_BOTTOM].w = SANE_FIX(0);
  5202. if (!dev->inquiry_tune_scan_length)
  5203. s->opt[OPT_OVERSCAN_TOP].cap |= SANE_CAP_INACTIVE;
  5204. if (!dev->inquiry_tune_scan_length)
  5205. s->opt[OPT_OVERSCAN_BOTTOM].cap |= SANE_CAP_INACTIVE;
  5206. /* background raster */
  5207. s->opt[OPT_BACKGROUND].name = "background-lines";
  5208. s->opt[OPT_BACKGROUND].title = "Background raster lines";
  5209. s->opt[OPT_BACKGROUND].desc = "The background raster controls the additional background lines to scan before the paper is feed thru the scanner.";
  5210. s->opt[OPT_BACKGROUND].type = SANE_TYPE_INT;
  5211. s->opt[OPT_BACKGROUND].unit = SANE_UNIT_PIXEL;
  5212. s->opt[OPT_BACKGROUND].constraint_type = SANE_CONSTRAINT_RANGE;
  5213. s->opt[OPT_BACKGROUND].constraint.range = &background_range;
  5214. s->val[OPT_BACKGROUND].w = 0;
  5215. if (!dev->inquiry_background_raster) {
  5216. s->opt[OPT_BACKGROUND].cap |= SANE_CAP_INACTIVE;
  5217. }
  5218. /* "Enhancement" group: */
  5219. s->opt[OPT_ENHANCEMENT_GROUP].title = "Enhancement";
  5220. s->opt[OPT_ENHANCEMENT_GROUP].desc = ""; /* for groups only title and type are valid */
  5221. s->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
  5222. s->opt[OPT_ENHANCEMENT_GROUP].cap = 0;
  5223. s->opt[OPT_ENHANCEMENT_GROUP].size = 0;
  5224. s->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
  5225. /* brightness */
  5226. s->opt[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
  5227. s->opt[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
  5228. s->opt[OPT_BRIGHTNESS].desc = SANE_DESC_BRIGHTNESS;
  5229. s->opt[OPT_BRIGHTNESS].type = SANE_TYPE_FIXED;
  5230. if (disable_gamma_table)
  5231. s->opt[OPT_BRIGHTNESS].cap |= SANE_CAP_INACTIVE;
  5232. s->opt[OPT_BRIGHTNESS].unit = SANE_UNIT_PERCENT;
  5233. s->opt[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
  5234. s->opt[OPT_BRIGHTNESS].constraint.range = &percentage_range;
  5235. s->val[OPT_BRIGHTNESS].w = SANE_FIX(0);
  5236. /* contrast */
  5237. s->opt[OPT_CONTRAST].name = SANE_NAME_CONTRAST;
  5238. s->opt[OPT_CONTRAST].title = SANE_TITLE_CONTRAST;
  5239. s->opt[OPT_CONTRAST].desc = SANE_DESC_CONTRAST;
  5240. s->opt[OPT_CONTRAST].type = SANE_TYPE_FIXED;
  5241. if (disable_gamma_table)
  5242. s->opt[OPT_CONTRAST].cap |= SANE_CAP_INACTIVE;
  5243. s->opt[OPT_CONTRAST].unit = SANE_UNIT_PERCENT;
  5244. s->opt[OPT_CONTRAST].constraint_type = SANE_CONSTRAINT_RANGE;
  5245. s->opt[OPT_CONTRAST].constraint.range = &percentage_range;
  5246. s->val[OPT_CONTRAST].w = SANE_FIX(0);
  5247. /* Quality Scan */
  5248. s->opt[OPT_QSCAN].name = "quality-scan";
  5249. s->opt[OPT_QSCAN].title = "Quality scan";
  5250. s->opt[OPT_QSCAN].desc = "Turn on quality scanning (slower but better).";
  5251. s->opt[OPT_QSCAN].type = SANE_TYPE_BOOL;
  5252. s->opt[OPT_QSCAN].unit = SANE_UNIT_NONE;
  5253. s->val[OPT_QSCAN].w = SANE_TRUE;
  5254. /* Quality Calibration */
  5255. s->opt[OPT_QCALIB].name = SANE_NAME_QUALITY_CAL;
  5256. s->opt[OPT_QCALIB].title = SANE_TITLE_QUALITY_CAL;
  5257. s->opt[OPT_QCALIB].desc = SANE_DESC_QUALITY_CAL;
  5258. s->opt[OPT_QCALIB].type = SANE_TYPE_BOOL;
  5259. s->opt[OPT_QCALIB].unit = SANE_UNIT_NONE;
  5260. s->val[OPT_QCALIB].w = SANE_TRUE;
  5261. /* grayscale gamma vector */
  5262. s->opt[OPT_GAMMA_VECTOR].name = SANE_NAME_GAMMA_VECTOR;
  5263. s->opt[OPT_GAMMA_VECTOR].title = SANE_TITLE_GAMMA_VECTOR;
  5264. s->opt[OPT_GAMMA_VECTOR].desc = SANE_DESC_GAMMA_VECTOR;
  5265. s->opt[OPT_GAMMA_VECTOR].type = SANE_TYPE_INT;
  5266. s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
  5267. s->opt[OPT_GAMMA_VECTOR].unit = SANE_UNIT_NONE;
  5268. s->opt[OPT_GAMMA_VECTOR].size = 256 * sizeof (SANE_Word);
  5269. s->opt[OPT_GAMMA_VECTOR].constraint_type = SANE_CONSTRAINT_RANGE;
  5270. s->opt[OPT_GAMMA_VECTOR].constraint.range = &u8_range;
  5271. s->val[OPT_GAMMA_VECTOR].wa = &s->gamma_table[0][0];
  5272. /* red gamma vector */
  5273. s->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R;
  5274. s->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R;
  5275. s->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R;
  5276. s->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT;
  5277. s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
  5278. s->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE;
  5279. s->opt[OPT_GAMMA_VECTOR_R].size = 256 * sizeof (SANE_Word);
  5280. s->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE;
  5281. s->opt[OPT_GAMMA_VECTOR_R].constraint.range = &u8_range;
  5282. s->val[OPT_GAMMA_VECTOR_R].wa = &s->gamma_table[1][0];
  5283. /* green gamma vector */
  5284. s->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G;
  5285. s->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G;
  5286. s->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G;
  5287. s->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT;
  5288. s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
  5289. s->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE;
  5290. s->opt[OPT_GAMMA_VECTOR_G].size = 256 * sizeof (SANE_Word);
  5291. s->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE;
  5292. s->opt[OPT_GAMMA_VECTOR_G].constraint.range = &u8_range;
  5293. s->val[OPT_GAMMA_VECTOR_G].wa = &s->gamma_table[2][0];
  5294. /* blue gamma vector */
  5295. s->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B;
  5296. s->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B;
  5297. s->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B;
  5298. s->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT;
  5299. s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
  5300. s->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE;
  5301. s->opt[OPT_GAMMA_VECTOR_B].size = 256 * sizeof (SANE_Word);
  5302. s->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE;
  5303. s->opt[OPT_GAMMA_VECTOR_B].constraint.range = &u8_range;
  5304. s->val[OPT_GAMMA_VECTOR_B].wa = &s->gamma_table[3][0];
  5305. if (!disable_gamma_table)
  5306. {
  5307. if (color_mode_is_color (s->c_mode)) {
  5308. s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
  5309. s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
  5310. s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
  5311. }
  5312. else {
  5313. s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
  5314. }
  5315. }
  5316. /* exposure */
  5317. s->opt[OPT_EXPOSURE].name = "exposure";
  5318. s->opt[OPT_EXPOSURE].title = "Exposure";
  5319. s->opt[OPT_EXPOSURE].desc = "Manual exposure adjustment.";
  5320. s->opt[OPT_EXPOSURE].type = SANE_TYPE_INT;
  5321. s->opt[OPT_EXPOSURE].unit = SANE_UNIT_PERCENT;
  5322. s->opt[OPT_EXPOSURE].constraint_type = SANE_CONSTRAINT_RANGE;
  5323. s->opt[OPT_EXPOSURE].constraint.range = &exposure_range;
  5324. s->val[OPT_EXPOSURE].w = 100;
  5325. if (!dev->inquiry_exposure_control) {
  5326. s->opt[OPT_EXPOSURE].cap |= SANE_CAP_INACTIVE;
  5327. }
  5328. /* Multi sample */
  5329. s->opt[OPT_MULTISAMPLE].name = "multi-sample";
  5330. s->opt[OPT_MULTISAMPLE].title = "Multi-sample";
  5331. s->opt[OPT_MULTISAMPLE].desc = "Enable multi-sample scan mode.";
  5332. s->opt[OPT_MULTISAMPLE].type = SANE_TYPE_BOOL;
  5333. s->opt[OPT_MULTISAMPLE].unit = SANE_UNIT_NONE;
  5334. s->val[OPT_MULTISAMPLE].w = SANE_FALSE;
  5335. /* TODO: No idea how to detect, assume exosure control devices are
  5336. new enough to support this, for now. -ReneR */
  5337. if (!dev->inquiry_exposure_control) {
  5338. s->opt[OPT_MULTISAMPLE].cap |= SANE_CAP_INACTIVE;
  5339. }
  5340. /* Infra-red */
  5341. s->opt[OPT_IR].name = "infra-red";
  5342. s->opt[OPT_IR].title = "Infra-red";
  5343. s->opt[OPT_IR].desc = "Enable infra-red scan mode.";
  5344. s->opt[OPT_IR].type = SANE_TYPE_BOOL;
  5345. s->opt[OPT_IR].unit = SANE_UNIT_NONE;
  5346. s->val[OPT_IR].w = SANE_FALSE;
  5347. /* TODO: No idea how to detect, assume exosure control devices are
  5348. new enough to support this, for now. -ReneR */
  5349. if (!dev->inquiry_exposure_control) {
  5350. s->opt[OPT_IR].cap |= SANE_CAP_INACTIVE;
  5351. }
  5352. /* film holder control */
  5353. if (dev->scanner_type != AV_FILM)
  5354. s->opt[OPT_FRAME].cap |= SANE_CAP_INACTIVE;
  5355. s->opt[OPT_FRAME].name = SANE_NAME_FRAME;
  5356. s->opt[OPT_FRAME].title = SANE_TITLE_FRAME;
  5357. s->opt[OPT_FRAME].desc = SANE_DESC_FRAME;
  5358. s->opt[OPT_FRAME].type = SANE_TYPE_INT;
  5359. s->opt[OPT_FRAME].unit = SANE_UNIT_NONE;
  5360. s->opt[OPT_FRAME].constraint_type = SANE_CONSTRAINT_RANGE;
  5361. s->opt[OPT_FRAME].constraint.range = &dev->frame_range;
  5362. s->val[OPT_FRAME].w = dev->current_frame;
  5363. /* power save time */
  5364. if (!dev->inquiry_power_save_time)
  5365. s->opt[OPT_POWER_SAVE_TIME].cap |= SANE_CAP_INACTIVE;
  5366. s->opt[OPT_POWER_SAVE_TIME].name = "power-save-time";
  5367. s->opt[OPT_POWER_SAVE_TIME].title = "Power save timer control";
  5368. s->opt[OPT_POWER_SAVE_TIME].desc = "Allows control of the scanner's power save timer, dimming or turning off the light.";
  5369. s->opt[OPT_POWER_SAVE_TIME].type = SANE_TYPE_INT;
  5370. s->opt[OPT_POWER_SAVE_TIME].unit = SANE_UNIT_NONE;
  5371. s->opt[OPT_POWER_SAVE_TIME].constraint_type = SANE_CONSTRAINT_NONE;
  5372. s->val[OPT_POWER_SAVE_TIME].w = 0;
  5373. /* message, like options set on the scanner, LED no. & co */
  5374. s->opt[OPT_MESSAGE].name = "message";
  5375. s->opt[OPT_MESSAGE].title = "message text from the scanner";
  5376. s->opt[OPT_MESSAGE].desc = "This text contains device specific options controlled ny the user on the scanner hardware.";
  5377. s->opt[OPT_MESSAGE].type = SANE_TYPE_STRING;
  5378. s->opt[OPT_MESSAGE].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_ADVANCED;
  5379. s->opt[OPT_MESSAGE].size = 129;
  5380. s->opt[OPT_MESSAGE].constraint_type = SANE_CONSTRAINT_NONE;
  5381. s->val[OPT_MESSAGE].s = malloc(s->opt[OPT_MESSAGE].size);
  5382. s->val[OPT_MESSAGE].s[0] = 0;
  5383. /* NVRAM */
  5384. s->opt[OPT_NVRAM].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_ADVANCED;
  5385. if (!dev->inquiry_nvram_read)
  5386. s->opt[OPT_NVRAM].cap |= SANE_CAP_INACTIVE;
  5387. s->opt[OPT_NVRAM].name = "nvram-values";
  5388. s->opt[OPT_NVRAM].title = "Obtain NVRAM values";
  5389. s->opt[OPT_NVRAM].desc = "Allows access obtaining the scanner's NVRAM values as pretty printed text.";
  5390. s->opt[OPT_NVRAM].type = SANE_TYPE_STRING;
  5391. s->opt[OPT_NVRAM].unit = SANE_UNIT_NONE;
  5392. s->opt[OPT_NVRAM].size = 1024;
  5393. s->opt[OPT_NVRAM].constraint_type = SANE_CONSTRAINT_NONE;
  5394. s->val[OPT_NVRAM].s = malloc(s->opt[OPT_NVRAM].size);
  5395. s->val[OPT_NVRAM].s[0] = 0;
  5396. return SANE_STATUS_GOOD;
  5397. }
  5398. /* This function is executed as a child process. The reason this is
  5399. executed as a subprocess is because some (most?) generic SCSI
  5400. interfaces block a SCSI request until it has completed. With a
  5401. subprocess, we can let it block waiting for the request to finish
  5402. while the main process can go about to do more important things
  5403. (such as recognizing when the user presses a cancel button).
  5404. WARNING: Since this is executed as a subprocess, it's NOT possible
  5405. to update any of the variables in the main process (in particular
  5406. the scanner state cannot be updated). */
  5407. static int
  5408. reader_process (void *data)
  5409. {
  5410. struct Avision_Scanner *s = (struct Avision_Scanner *) data;
  5411. int fd = s->write_fds;
  5412. Avision_Device* dev = s->hw;
  5413. SANE_Status status;
  5414. SANE_Status exit_status = SANE_STATUS_GOOD;
  5415. sigset_t sigterm_set;
  5416. sigset_t ignore_set;
  5417. struct SIGACTION act;
  5418. FILE* fp;
  5419. FILE* rear_fp = 0; /* used to store the deinterlaced rear data */
  5420. FILE* raw_fp = 0; /* used to write the RAW image data for debugging */
  5421. /* the complex params */
  5422. unsigned int lines_per_stripe;
  5423. unsigned int lines_per_output;
  5424. unsigned int max_bytes_per_read;
  5425. SANE_Bool gray_mode;
  5426. /* the simple params for the data reader */
  5427. int hw_line = 0;
  5428. int line = 0;
  5429. unsigned int stripe_size;
  5430. unsigned int stripe_fill;
  5431. unsigned int out_size;
  5432. size_t total_size;
  5433. size_t processed_bytes;
  5434. enum {
  5435. NONE, /* do not de-interlace at all */
  5436. STRIPE, /* every 2nd stripe */
  5437. HALF, /* the 2nd half */
  5438. LINE /* every 2nd line */
  5439. } deinterlace = NONE;
  5440. /* the fat strip we currently puzzle together to perform software-colorpack
  5441. and more */
  5442. uint8_t* stripe_data;
  5443. /* the corrected output data */
  5444. uint8_t* out_data;
  5445. /* interpolation output data, one line */
  5446. uint8_t* ip_history = 0;
  5447. uint8_t* ip_data = 0;
  5448. DBG (3, "reader_process:\n");
  5449. if (sanei_thread_is_forked())
  5450. close (s->read_fds);
  5451. sigfillset (&ignore_set);
  5452. sigdelset (&ignore_set, SIGTERM);
  5453. #if defined (__APPLE__) && defined (__MACH__)
  5454. sigdelset (&ignore_set, SIGUSR2);
  5455. #endif
  5456. sigprocmask (SIG_SETMASK, &ignore_set, 0);
  5457. memset (&act, 0, sizeof (act));
  5458. sigaction (SIGTERM, &act, 0);
  5459. sigemptyset (&sigterm_set);
  5460. sigaddset (&sigterm_set, SIGTERM);
  5461. gray_mode = color_mode_is_shaded (s->c_mode);
  5462. if (s->avdimen.interlaced_duplex) {
  5463. deinterlace = STRIPE;
  5464. if ( (dev->hw->feature_type & AV_NON_INTERLACED_DUPLEX_300) &&
  5465. (s->avdimen.hw_xres <= 300 && s->avdimen.hw_yres <= 300) )
  5466. deinterlace = HALF;
  5467. if (dev->hw->feature_type & AV_2ND_LINE_INTERLACED)
  5468. deinterlace = LINE;
  5469. if (dev->scanner_type == AV_FILM)
  5470. deinterlace = LINE;
  5471. }
  5472. fp = fdopen (fd, "w");
  5473. if (!fp)
  5474. return SANE_STATUS_NO_MEM;
  5475. /* start scan ? */
  5476. if (deinterlace == NONE ||
  5477. (deinterlace != NONE && !s->duplex_rear_valid))
  5478. {
  5479. /* reserve unit - in the past we did this in open - but the
  5480. windows driver does reserve for each scan and some ADF
  5481. devices need a release for each sheet anyway ... */
  5482. status = reserve_unit (s);
  5483. if (status != SANE_STATUS_GOOD) {
  5484. DBG (1, "reader_process: reserve_unit failed: %s\n",
  5485. sane_strstatus (status));
  5486. return status;
  5487. }
  5488. if (dev->hw->feature_type & AV_NO_START_SCAN) {
  5489. DBG (1, "reader_process: start_scan skipped due to device-list!\n");
  5490. }
  5491. else {
  5492. status = start_scan (s);
  5493. if (status != SANE_STATUS_GOOD) {
  5494. DBG (1, "reader_process: start_scan faild: %s\n",
  5495. sane_strstatus (status));
  5496. return status;
  5497. }
  5498. }
  5499. if (dev->hw->feature_type & AV_ACCEL_TABLE)
  5500. /* (s->hw->inquiry_asic_type == AV_ASIC_C6) */ {
  5501. status = send_acceleration_table (s);
  5502. if (status != SANE_STATUS_GOOD) {
  5503. DBG (1, "reader_process: send_acceleration_table failed: %s\n",
  5504. sane_strstatus (status));
  5505. return status;
  5506. }
  5507. }
  5508. }
  5509. /* setup file i/o for deinterlacing scans */
  5510. if (deinterlace != NONE)
  5511. {
  5512. if (!s->duplex_rear_valid) { /* create new file for writing */
  5513. DBG (3, "reader_process: opening duplex rear file for writing.\n");
  5514. rear_fp = fopen (s->duplex_rear_fname, "w");
  5515. if (! rear_fp) {
  5516. fclose (fp);
  5517. return SANE_STATUS_NO_MEM;
  5518. }
  5519. }
  5520. else { /* open saved rear data */
  5521. DBG (3, "reader_process: opening duplex rear file for reading.\n");
  5522. rear_fp = fopen (s->duplex_rear_fname, "r");
  5523. if (! rear_fp) {
  5524. fclose (fp);
  5525. return SANE_STATUS_IO_ERROR;
  5526. }
  5527. }
  5528. }
  5529. /* always quite some lines to saturate the (USB) bus */
  5530. lines_per_stripe = dev->read_stripe_size;
  5531. if (s->avdimen.line_difference)
  5532. lines_per_stripe += 2 * s->avdimen.line_difference;
  5533. stripe_size = s->avdimen.hw_bytes_per_line * lines_per_stripe;
  5534. lines_per_output = lines_per_stripe - 2 * s->avdimen.line_difference;
  5535. if (s->av_con.connection_type == AV_SCSI)
  5536. /* maybe better not /2 ... */
  5537. max_bytes_per_read = dev->scsi_buffer_size / 2;
  5538. else
  5539. /* vast buffer size to saturate the bus */
  5540. max_bytes_per_read = 0x100000;
  5541. out_size = s->avdimen.hw_bytes_per_line * lines_per_output;
  5542. DBG (3, "dev->scsi_buffer_size / 2: %d\n",
  5543. dev->scsi_buffer_size / 2);
  5544. DBG (3, "bytes_per_line: %d, pixels_per_line: %d\n",
  5545. s->avdimen.hw_bytes_per_line, s->avdimen.hw_pixels_per_line);
  5546. DBG (3, "lines_per_stripe: %d, lines_per_output: %d\n",
  5547. lines_per_stripe, lines_per_output);
  5548. DBG (3, "max_bytes_per_read: %d, stripe_size: %d, out_size: %d\n",
  5549. max_bytes_per_read, stripe_size, out_size);
  5550. stripe_data = malloc (stripe_size);
  5551. /* for software scaling we need an aditional interpolation line buffer */
  5552. if (s->avdimen.hw_xres != s->avdimen.xres ||
  5553. s->avdimen.hw_yres != s->avdimen.yres)
  5554. {
  5555. /* layout out_data so that the interpolation history is exactly in front */
  5556. ip_history = malloc (s->avdimen.hw_bytes_per_line + out_size);
  5557. out_data = ip_history + s->avdimen.hw_bytes_per_line;
  5558. ip_data = malloc (s->params.bytes_per_line);
  5559. }
  5560. else {
  5561. out_data = malloc (out_size);
  5562. }
  5563. /* calculate params for the reading loop */
  5564. total_size = s->avdimen.hw_bytes_per_line * (s->avdimen.hw_lines +
  5565. 2 * s->avdimen.line_difference +
  5566. s->avdimen.rear_offset);
  5567. if (deinterlace != NONE && !s->duplex_rear_valid)
  5568. total_size *= 2;
  5569. DBG (3, "reader_process: total_size: %lu\n", (u_long) total_size);
  5570. /* write a RAW PNM file for debugging -ReneR */
  5571. if (0 /* DEBUG */ &&
  5572. (deinterlace == NONE || (deinterlace != NONE && !s->duplex_rear_valid)) )
  5573. {
  5574. raw_fp = fopen ("/tmp/sane-avision.raw", "w");
  5575. write_pnm_header (fp, s->c_mode, s->params.depth,
  5576. s->avdimen.hw_pixels_per_line, total_size / s->avdimen.hw_bytes_per_line);
  5577. }
  5578. processed_bytes = 0;
  5579. stripe_fill = 0;
  5580. /* First dump background raster, bypassing all the other processing
  5581. counters, ... */
  5582. if (dev->inquiry_background_raster && s->val[OPT_BACKGROUND].w)
  5583. {
  5584. uint8_t* background = s->background_raster;
  5585. if (s->duplex_rear_valid)
  5586. background += s->params.bytes_per_line * s->val[OPT_BACKGROUND].w;
  5587. DBG (5, "reader_process: dumping background raster\n");
  5588. fwrite (background, s->params.bytes_per_line, s->val[OPT_BACKGROUND].w, fp);
  5589. }
  5590. /* Data read loop until all data has been processed. Might exit
  5591. before all lines are transferred for ADF paper end. */
  5592. while (exit_status == SANE_STATUS_GOOD && processed_bytes < total_size)
  5593. {
  5594. unsigned int useful_bytes;
  5595. DBG (5, "reader_process: stripe filled: %d\n", stripe_fill);
  5596. /* fill the stripe buffer with real data */
  5597. while (!s->duplex_rear_valid &&
  5598. processed_bytes < total_size && stripe_fill < stripe_size &&
  5599. exit_status == SANE_STATUS_GOOD)
  5600. {
  5601. size_t this_read = stripe_size - stripe_fill;
  5602. /* Limit reads to max_bytes_per_read and global data
  5603. boundaries. Rounded to the next lower multiple of
  5604. byte_per_lines, otherwise some scanners freeze. */
  5605. if (this_read > max_bytes_per_read)
  5606. this_read = (max_bytes_per_read -
  5607. max_bytes_per_read % s->avdimen.hw_bytes_per_line);
  5608. if (processed_bytes + this_read > total_size)
  5609. this_read = total_size - processed_bytes;
  5610. read_constrains(s, this_read);
  5611. DBG (5, "reader_process: processed_bytes: %lu, total_size: %lu\n",
  5612. (u_long) processed_bytes, (u_long) total_size);
  5613. DBG (5, "reader_process: this_read: %lu\n", (u_long) this_read);
  5614. sigprocmask (SIG_BLOCK, &sigterm_set, 0);
  5615. status = read_data (s, stripe_data + stripe_fill, &this_read);
  5616. sigprocmask (SIG_UNBLOCK, &sigterm_set, 0);
  5617. /* only EOF on the second stripe, as otherwise the rear page
  5618. is shorter */
  5619. if (status == SANE_STATUS_EOF && deinterlace == STRIPE) {
  5620. static int already_eof = 0;
  5621. if (!already_eof) {
  5622. DBG (5, "reader_process: first EOF on stripe interlace: hiding.\n");
  5623. status = SANE_STATUS_GOOD;
  5624. already_eof = 1;
  5625. }
  5626. }
  5627. /* write RAW data to file for debugging */
  5628. if (raw_fp && this_read > 0)
  5629. fwrite (stripe_data + stripe_fill, this_read, 1, raw_fp);
  5630. if (status == SANE_STATUS_EOF || this_read == 0) {
  5631. DBG (1, "reader_process: read_data failed due to EOF\n");
  5632. exit_status = SANE_STATUS_EOF;
  5633. }
  5634. if (status != SANE_STATUS_GOOD) {
  5635. DBG (1, "reader_process: read_data failed with status: %d\n",
  5636. status);
  5637. exit_status = status;
  5638. }
  5639. stripe_fill += this_read;
  5640. processed_bytes += this_read;
  5641. }
  5642. /* fill the stripe buffer with stored, virtual data */
  5643. if (s->duplex_rear_valid)
  5644. {
  5645. size_t this_read = stripe_size - stripe_fill;
  5646. size_t got;
  5647. /* limit reads to max_read and global data boundaries */
  5648. if (this_read > max_bytes_per_read)
  5649. this_read = max_bytes_per_read;
  5650. if (processed_bytes + this_read > total_size)
  5651. this_read = total_size - processed_bytes;
  5652. DBG (5, "reader_process: virtual processed_bytes: %lu, total_size: %lu\n",
  5653. (u_long) processed_bytes, (u_long) total_size);
  5654. DBG (5, "reader_process: virtual this_read: %lu\n", (u_long) this_read);
  5655. got = fread (stripe_data + stripe_fill, 1, this_read, rear_fp);
  5656. stripe_fill += got;
  5657. processed_bytes += got;
  5658. if (got != this_read)
  5659. exit_status = SANE_STATUS_EOF;
  5660. }
  5661. DBG (5, "reader_process: stripe filled: %d\n", stripe_fill);
  5662. useful_bytes = stripe_fill;
  5663. if (color_mode_is_color (s->c_mode))
  5664. useful_bytes -= 2 * s->avdimen.line_difference * s->avdimen.hw_bytes_per_line;
  5665. DBG (3, "reader_process: useful_bytes %i\n", useful_bytes);
  5666. /* Deinterlace, save the rear stripes. For some scanners (AV220)
  5667. that is every 2nd stripe, the 2nd half of the transferred
  5668. data ((AV83xx) or every 2nd line (AV122)). */
  5669. if (deinterlace != NONE && !s->duplex_rear_valid)
  5670. {
  5671. /* for all lines we have in the buffer: */
  5672. unsigned int absline = (processed_bytes - stripe_fill) / s->avdimen.hw_bytes_per_line;
  5673. unsigned int abslines = absline + useful_bytes / s->avdimen.hw_bytes_per_line;
  5674. uint8_t* ptr = stripe_data;
  5675. for ( ; absline < abslines; ++absline)
  5676. {
  5677. DBG (9, "reader_process: deinterlacing line %d\n", absline);
  5678. /* interlaced? save the back data to the rear buffer */
  5679. if ( (deinterlace == STRIPE && absline % (lines_per_stripe*2) >= lines_per_stripe) ||
  5680. (deinterlace == HALF && absline >= total_size / s->avdimen.hw_bytes_per_line / 2) ||
  5681. (deinterlace == LINE && absline & 0x1) ) /* last bit equals % 2 */
  5682. {
  5683. DBG (9, "reader_process: saving rear line %d to temporary file.\n", absline);
  5684. fwrite (ptr, s->avdimen.hw_bytes_per_line, 1, rear_fp);
  5685. if (deinterlace == LINE)
  5686. memmove (ptr, ptr+s->avdimen.hw_bytes_per_line,
  5687. stripe_data + stripe_fill - ptr - s->avdimen.hw_bytes_per_line);
  5688. else
  5689. ptr += s->avdimen.hw_bytes_per_line;
  5690. useful_bytes -= s->avdimen.hw_bytes_per_line;
  5691. stripe_fill -= s->avdimen.hw_bytes_per_line;
  5692. }
  5693. else
  5694. ptr += s->avdimen.hw_bytes_per_line;
  5695. }
  5696. DBG (9, "reader_process: after deinterlacing: useful_bytes: %d, stripe_fill: %d\n",
  5697. useful_bytes, stripe_fill);
  5698. }
  5699. /*
  5700. * Perform needed data convertions (packing, ...) and/or copy the
  5701. * image data.
  5702. */
  5703. if (s->c_mode != AV_TRUECOLOR && s->c_mode != AV_TRUECOLOR16)
  5704. /* simple copy */
  5705. {
  5706. memcpy (out_data, stripe_data, useful_bytes);
  5707. }
  5708. else /* AV_TRUECOLOR* */
  5709. {
  5710. /* WARNING: DO NOT MODIFY MY (HOPEFULLY WELL) OPTIMIZED
  5711. ALGORITHMS BELOW, WITHOUT UNDERSTANDING THEM FULLY ! */
  5712. if (s->avdimen.line_difference > 0) /* color-pack */
  5713. {
  5714. /* TODO: add 16bit per sample code? */
  5715. unsigned int i;
  5716. int c_offset = s->avdimen.line_difference * s->avdimen.hw_bytes_per_line;
  5717. uint8_t* r_ptr = stripe_data;
  5718. uint8_t* g_ptr = stripe_data + c_offset + 1;
  5719. uint8_t* b_ptr = stripe_data + 2 * c_offset + 2;
  5720. for (i = 0; i < useful_bytes;) {
  5721. out_data [i++] = *r_ptr; r_ptr += 3;
  5722. out_data [i++] = *g_ptr; g_ptr += 3;
  5723. out_data [i++] = *b_ptr; b_ptr += 3;
  5724. }
  5725. } /* end color pack */
  5726. else if (dev->inquiry_needs_line_pack) /* line-pack */
  5727. {
  5728. /* TODO: add 16bit per sample code? */
  5729. int i = 0, l, p;
  5730. const int lines = useful_bytes / s->avdimen.hw_bytes_per_line;
  5731. for (l = 0; l < lines; ++l)
  5732. {
  5733. uint8_t* r_ptr = stripe_data + (s->avdimen.hw_bytes_per_line * l);
  5734. uint8_t* g_ptr = r_ptr + s->avdimen.hw_pixels_per_line;
  5735. uint8_t* b_ptr = g_ptr + s->avdimen.hw_pixels_per_line;
  5736. for (p = 0; p < s->avdimen.hw_pixels_per_line; ++p) {
  5737. out_data [i++] = *(r_ptr++);
  5738. out_data [i++] = *(g_ptr++);
  5739. out_data [i++] = *(b_ptr++);
  5740. }
  5741. }
  5742. } /* end line pack */
  5743. else /* else no packing was required -> simple copy */
  5744. {
  5745. memcpy (out_data, stripe_data, useful_bytes);
  5746. }
  5747. } /* end if AV_TRUECOLOR* */
  5748. /* FURTHER POST-PROCESSING ON THE FINAL OUTPUT DATA */
  5749. /* maybe mirroring in ADF mode */
  5750. if ((s->source_mode_dim == AV_ADF_DIM && dev->inquiry_adf_need_mirror) ||
  5751. (s->source_mode == AV_ADF_DUPLEX && dev->inquiry_adf_need_mirror_rear && s->page % 2 == 0))
  5752. {
  5753. if ( (s->c_mode != AV_TRUECOLOR) ||
  5754. (s->c_mode == AV_TRUECOLOR && dev->inquiry_adf_bgr_order) )
  5755. {
  5756. /* Mirroring with bgr -> rgb convertion: Just mirror the
  5757. * whole line */
  5758. int l;
  5759. int lines = useful_bytes / s->avdimen.hw_bytes_per_line;
  5760. for (l = 0; l < lines; ++l)
  5761. {
  5762. uint8_t* begin_ptr = out_data + (l * s->avdimen.hw_bytes_per_line);
  5763. uint8_t* end_ptr = begin_ptr + s->avdimen.hw_bytes_per_line;
  5764. while (begin_ptr < end_ptr) {
  5765. uint8_t tmp;
  5766. tmp = *begin_ptr;
  5767. *begin_ptr++ = *end_ptr;
  5768. *end_ptr-- = tmp;
  5769. }
  5770. }
  5771. }
  5772. else /* non trival mirroring */
  5773. {
  5774. /* Non-trival Mirroring with element swapping */
  5775. int l;
  5776. int lines = useful_bytes / s->avdimen.hw_bytes_per_line;
  5777. for (l = 0; l < lines; ++l)
  5778. {
  5779. uint8_t* begin_ptr = out_data + (l * s->avdimen.hw_bytes_per_line);
  5780. uint8_t* end_ptr = begin_ptr + s->avdimen.hw_bytes_per_line - 3;
  5781. while (begin_ptr < end_ptr) {
  5782. uint8_t tmp;
  5783. /* R */
  5784. tmp = *begin_ptr;
  5785. *begin_ptr++ = *end_ptr;
  5786. *end_ptr++ = tmp;
  5787. /* G */
  5788. tmp = *begin_ptr;
  5789. *begin_ptr++ = *end_ptr;
  5790. *end_ptr++ = tmp;
  5791. /* B */
  5792. tmp = *begin_ptr;
  5793. *begin_ptr++ = *end_ptr;
  5794. *end_ptr = tmp;
  5795. end_ptr -= 5;
  5796. }
  5797. }
  5798. }
  5799. } /* end if mirroring needed */
  5800. /* byte swapping and software calibration 16bit mode */
  5801. if (s->c_mode == AV_GRAYSCALE12 ||
  5802. s->c_mode == AV_GRAYSCALE16 ||
  5803. s->c_mode == AV_TRUECOLOR12 ||
  5804. s->c_mode == AV_TRUECOLOR16) {
  5805. int l;
  5806. int lines = useful_bytes / s->avdimen.hw_bytes_per_line;
  5807. uint8_t* dark_avg_data = s->dark_avg_data;
  5808. uint8_t* white_avg_data = s->white_avg_data;
  5809. uint8_t* begin_ptr = out_data;
  5810. uint8_t* end_ptr = begin_ptr + s->avdimen.hw_bytes_per_line;
  5811. uint8_t* line_ptr;
  5812. double scale = 1.0;
  5813. if (s->c_mode == AV_GRAYSCALE12 || s->c_mode == AV_TRUECOLOR12)
  5814. scale = (double) (1<<4);
  5815. while (begin_ptr < end_ptr) {
  5816. uint16_t dark_avg = 0;
  5817. uint16_t white_avg = WHITE_MAP_RANGE;
  5818. if (dark_avg_data)
  5819. dark_avg = get_double_le (dark_avg_data);
  5820. if (white_avg_data)
  5821. white_avg = get_double_le (white_avg_data);
  5822. line_ptr = begin_ptr;
  5823. for (l = 0; l < lines; ++ l)
  5824. {
  5825. double v = (double) get_double_le (line_ptr) * scale;
  5826. uint16_t v2;
  5827. if (0)
  5828. v = (v - dark_avg) * white_avg / WHITE_MAP_RANGE;
  5829. v2 = v < 0xFFFF ? v : 0xFFFF;
  5830. /* SANE Standard 3.2.1 "... bytes of each sample value are
  5831. transmitted in the machine's native byte order." */
  5832. *line_ptr = v2;
  5833. line_ptr += s->avdimen.hw_bytes_per_line;
  5834. }
  5835. begin_ptr += 2;
  5836. if (dark_avg_data)
  5837. dark_avg_data += 2;
  5838. if (white_avg_data)
  5839. white_avg_data += 2;
  5840. }
  5841. }
  5842. /* SOFTWARE SCALING WITH INTERPOLATION (IF NECESSARY) */
  5843. if (s->avdimen.hw_xres == s->avdimen.xres &&
  5844. s->avdimen.hw_yres == s->avdimen.yres) /* No scaling */
  5845. {
  5846. int lines, _hw_line = hw_line;
  5847. uint8_t* src = out_data;
  5848. /* we support cropping the begin and end due rear offset */
  5849. for (lines = useful_bytes / s->avdimen.hw_bytes_per_line;
  5850. lines > 0; --lines, ++_hw_line, src += s->avdimen.hw_bytes_per_line)
  5851. {
  5852. if (deinterlace != NONE) {
  5853. /* crop rear offset :-( */
  5854. if ( (!s->duplex_rear_valid && _hw_line >= s->avdimen.hw_lines) ||
  5855. (s->duplex_rear_valid && _hw_line < s->avdimen.rear_offset) )
  5856. {
  5857. DBG (7, "reader_process: skip due read offset line: %d\n", line);
  5858. continue;
  5859. }
  5860. }
  5861. fwrite (src, s->avdimen.hw_bytes_per_line, 1, fp);
  5862. ++line;
  5863. }
  5864. }
  5865. else /* Software scaling - watch out - this code bites back! */
  5866. {
  5867. int x;
  5868. /* for convinience in the 16bit code path */
  5869. uint16_t* out_data16 = (uint16_t*) out_data;
  5870. const int hw_line_end = hw_line + useful_bytes / s->avdimen.hw_bytes_per_line;
  5871. /* on-the-fly bi-linear interpolation */
  5872. while (1) {
  5873. double by = (-1.0 + s->avdimen.hw_lines) * line / s->params.lines;
  5874. int sy = (int)floor(by);
  5875. int ydist = (int) ((by - sy) * 256);
  5876. int syy = sy + 1;
  5877. const int hwbpl = s->avdimen.hw_bytes_per_line;
  5878. uint8_t* dst = ip_data;
  5879. uint16_t* dst16 = (uint16_t*) ip_data;
  5880. unsigned int v; /* accumulator */
  5881. /* Break out if we do not have the hw source line - yet,
  5882. or when we are past the end of wanted data (e.g. on the
  5883. front page due rear_offset). Also take the read_offset
  5884. into account on the rear side */
  5885. if (deinterlace != NONE) {
  5886. if (!s->duplex_rear_valid && syy >= s->avdimen.hw_lines) {
  5887. DBG (7, "reader_process: skip due past intended front page lines: %d\n", sy);
  5888. break;
  5889. }
  5890. else if (s->duplex_rear_valid) {
  5891. /* the beginning is to be skipped, accessed thru offset */
  5892. DBG (7, "reader_process: rear_offset adjusting source: %d\n", sy);
  5893. sy += s->avdimen.rear_offset;
  5894. syy += s->avdimen.rear_offset;
  5895. }
  5896. }
  5897. if (sy >= hw_line_end || syy >= hw_line_end) {
  5898. DBG (3, "reader_process: source line %d-%d not yet avail\n",
  5899. sy, syy);
  5900. break;
  5901. }
  5902. /* convert to offset in current stripe */
  5903. sy -= hw_line;
  5904. syy -= hw_line;
  5905. if (sy < -1) {
  5906. DBG (1, "reader_process: need more history: %d???\n", sy);
  5907. sy = -1;
  5908. }
  5909. DBG (8, "reader_process: out line: %d <- from: %d-%d\n",
  5910. line, sy, syy);
  5911. for (x = 0; x < s->params.pixels_per_line; ++x) {
  5912. const double bx = (-1.0 + s->avdimen.hw_pixels_per_line) * x / s->params.pixels_per_line;
  5913. const int sx = (int)floor(bx);
  5914. const int xdist = (int) ((bx - sx) * 256);
  5915. const int sxx = sx + 1;
  5916. if (x == 0 || x == s->params.pixels_per_line - 1)
  5917. DBG (8, "reader_process: x: %d <- from: %d-%d\n",
  5918. x, sx, sxx);
  5919. switch (s->c_mode) {
  5920. case AV_THRESHOLDED:
  5921. case AV_DITHERED:
  5922. {
  5923. /* Repeating this over and over again is not fast, but
  5924. as a seldom used code-path we want it readable.
  5925. x/8 is the byte, and x%8 the bit position. */
  5926. v =
  5927. ( ((out_data [sy*hwbpl + sx/8 ] >> (7-sx%8 )) & 1) * (256-xdist) * (256-ydist) +
  5928. ((out_data [sy*hwbpl + sxx/8] >> (7-sxx%8)) & 1) * xdist * (256-ydist) +
  5929. ((out_data [syy*hwbpl + sx/8 ] >> (7-sx%8 )) & 1) * (256-xdist) * ydist +
  5930. ((out_data [syy*hwbpl + sxx/8] >> (7-sxx%8)) & 1) * xdist * ydist
  5931. ) / (1 + 1 * 256);
  5932. /* Shift and or the result together and eventually
  5933. jump to the next byte. */
  5934. *dst = (*dst << 1) | ((v>>7)&1);
  5935. if (x % 8 == 7)
  5936. ++dst;
  5937. }
  5938. break;
  5939. case AV_GRAYSCALE:
  5940. {
  5941. v =
  5942. ( out_data [sy*hwbpl + sx ] * (256-xdist) * (256-ydist) +
  5943. out_data [sy*hwbpl + sxx] * xdist * (256-ydist) +
  5944. out_data [syy*hwbpl + sx ] * (256-xdist) * ydist +
  5945. out_data [syy*hwbpl + sxx] * xdist * ydist
  5946. ) / (256 * 256);
  5947. *dst++ = v;
  5948. }
  5949. break;
  5950. case AV_GRAYSCALE12:
  5951. case AV_GRAYSCALE16:
  5952. {
  5953. /* TODO: test! */
  5954. v =
  5955. ( out_data16 [sy*hwbpl + sx ] * (256-xdist) * (256-ydist) +
  5956. out_data16 [sy*hwbpl + sxx] * xdist * (256-ydist) +
  5957. out_data16 [syy*hwbpl + sx ] * (256-xdist) * ydist +
  5958. out_data16 [syy*hwbpl + sxx] * xdist * ydist
  5959. ) / (256 * 256);
  5960. *dst16++ = v;
  5961. }
  5962. break;
  5963. case AV_TRUECOLOR:
  5964. {
  5965. int c;
  5966. for (c = 0; c < 3; ++c)
  5967. {
  5968. v =
  5969. ( out_data [sy*hwbpl + sx*3 + c] * (256-xdist) * (256-ydist) +
  5970. out_data [sy*hwbpl + sxx*3 + c] * xdist * (256-ydist) +
  5971. out_data [syy*hwbpl + sx*3 + c] * (256-xdist) * ydist +
  5972. out_data [syy*hwbpl + sxx*3 + c] * xdist * ydist
  5973. ) / (256 * 256);
  5974. *dst++ = v;
  5975. }
  5976. }
  5977. break;
  5978. case AV_TRUECOLOR12:
  5979. case AV_TRUECOLOR16:
  5980. {
  5981. /* TODO: test! */
  5982. int c;
  5983. for (c = 0; c < 3; ++c)
  5984. {
  5985. v =
  5986. ( out_data16 [sy*hwbpl + sx*3 + c] * (256-xdist) * (256-ydist) +
  5987. out_data16 [sy*hwbpl + sxx*3 + c] * xdist * (256-ydist) +
  5988. out_data16 [syy*hwbpl + sx*3 + c] * (256-xdist) * ydist +
  5989. out_data16 [syy*hwbpl + sxx*3 + c] * xdist * ydist
  5990. ) / (256 * 256);
  5991. *dst16++ = v;
  5992. }
  5993. }
  5994. break;
  5995. case AV_COLOR_MODE_LAST:
  5996. ; /* silence compiler warning */
  5997. }
  5998. }
  5999. fwrite (ip_data, s->params.bytes_per_line, 1, fp);
  6000. ++line;
  6001. }
  6002. /* copy one line of history for the next pass */
  6003. memcpy (ip_history,
  6004. out_data + useful_bytes - s->avdimen.hw_bytes_per_line,
  6005. s->avdimen.hw_bytes_per_line);
  6006. }
  6007. /* save image date in stripe buffer for next next stripe */
  6008. stripe_fill -= useful_bytes;
  6009. if (stripe_fill > 0)
  6010. memcpy (stripe_data, stripe_data + useful_bytes, stripe_fill);
  6011. hw_line += useful_bytes / s->avdimen.hw_bytes_per_line;
  6012. DBG (3, "reader_process: end of iteration\n");
  6013. } /* end while not all lines or inf. mode */
  6014. DBG (3, "reader_process: i/o loop finished\n");
  6015. if (exit_status == SANE_STATUS_GOOD)
  6016. exit_status = SANE_STATUS_EOF;
  6017. if (raw_fp)
  6018. fclose (raw_fp);
  6019. /* maybe we need to fill in some white data */
  6020. if (exit_status == SANE_STATUS_EOF && line < s->params.lines) {
  6021. DBG (3, "reader_process: padding with white data\n");
  6022. memset (out_data, gray_mode ? 0xff : 0x00, s->params.bytes_per_line);
  6023. DBG (6, "reader_process: padding line %d - %d\n",
  6024. line, s->params.lines);
  6025. while (line < s->params.lines) {
  6026. fwrite (out_data, s->params.bytes_per_line, 1, fp);
  6027. ++line;
  6028. }
  6029. }
  6030. /* Eject film holder and/or release_unit - but only for
  6031. non-duplex-rear / non-virtual scans. */
  6032. if (deinterlace != NONE && s->duplex_rear_valid)
  6033. {
  6034. DBG (1, "reader_process: virtual duplex scan - no device cleanup!\n");
  6035. }
  6036. else
  6037. {
  6038. /* poll the cancel button if the scanner is marked to have one */
  6039. if (dev->hw->feature_type & AV_CANCEL_BUTTON) {
  6040. if (get_button_status (s) == SANE_STATUS_CANCELLED)
  6041. exit_status = SANE_STATUS_CANCELLED;
  6042. }
  6043. status = release_unit (s, 0);
  6044. if (status != SANE_STATUS_GOOD)
  6045. DBG (1, "reader_process: release_unit failed\n");
  6046. if (dev->inquiry_new_protocol && dev->scanner_type == AV_FILM) {
  6047. status = object_position (s, AVISION_SCSI_OP_GO_HOME);
  6048. if (status != SANE_STATUS_GOOD)
  6049. DBG (1, "reader_process: object position go-home failed!\n");
  6050. }
  6051. }
  6052. fclose (fp);
  6053. if (rear_fp)
  6054. fclose (rear_fp);
  6055. if (ip_data) free (ip_data);
  6056. if (ip_history)
  6057. free (ip_history);
  6058. else
  6059. free (out_data); /* if we have ip_history out_data is included there */
  6060. free (stripe_data);
  6061. DBG (3, "reader_process: returning success\n");
  6062. return exit_status;
  6063. }
  6064. /* SANE callback to attach a SCSI device */
  6065. static SANE_Status
  6066. attach_one_scsi (const char* dev)
  6067. {
  6068. attach (dev, AV_SCSI, 0);
  6069. return SANE_STATUS_GOOD;
  6070. }
  6071. /* SANE callback to attach an USB device */
  6072. static SANE_Status
  6073. attach_one_usb (const char* dev)
  6074. {
  6075. attach (dev, AV_USB, 0);
  6076. return SANE_STATUS_GOOD;
  6077. }
  6078. SANE_Status
  6079. sane_init (SANE_Int* version_code, SANE_Auth_Callback authorize)
  6080. {
  6081. FILE* fp;
  6082. char line[PATH_MAX];
  6083. const char* cp = 0;
  6084. char* word;
  6085. int linenumber = 0;
  6086. int model_num = 0;
  6087. authorize = authorize; /* silence gcc */
  6088. DBG_INIT();
  6089. #ifdef AVISION_STATIC_DEBUG_LEVEL
  6090. DBG_LEVEL = AVISION_STATIC_DEBUG_LEVEL;
  6091. #endif
  6092. DBG (3, "sane_init:(Version: %i.%i Build: %i)\n",
  6093. SANE_CURRENT_MAJOR, V_MINOR, BACKEND_BUILD);
  6094. /* must come first */
  6095. sanei_usb_init ();
  6096. sanei_thread_init ();
  6097. if (version_code)
  6098. *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, BACKEND_BUILD);
  6099. fp = sanei_config_open (AVISION_CONFIG_FILE);
  6100. if (fp <= (FILE*)0)
  6101. {
  6102. DBG (1, "sane_init: No config file present!\n");
  6103. }
  6104. else
  6105. {
  6106. /* first parse the config file */
  6107. while (sanei_config_read (line, sizeof (line), fp))
  6108. {
  6109. attaching_hw = 0;
  6110. word = NULL;
  6111. ++ linenumber;
  6112. DBG (5, "sane_init: parsing config line \"%s\"\n",
  6113. line);
  6114. cp = sanei_config_get_string (line, &word);
  6115. if (!word || cp == line) {
  6116. DBG (5, "sane_init: config file line %d: ignoring empty line\n",
  6117. linenumber);
  6118. if (word) {
  6119. free (word);
  6120. word = NULL;
  6121. }
  6122. continue;
  6123. }
  6124. if (!word) {
  6125. DBG (1, "sane_init: config file line %d: could not be parsed\n",
  6126. linenumber);
  6127. continue;
  6128. }
  6129. if (word[0] == '#') {
  6130. DBG (5, "sane_init: config file line %d: ignoring comment line\n",
  6131. linenumber);
  6132. free (word);
  6133. word = NULL;
  6134. continue;
  6135. }
  6136. if (strcmp (word, "option") == 0)
  6137. {
  6138. free (word);
  6139. word = NULL;
  6140. cp = sanei_config_get_string (cp, &word);
  6141. if (strcmp (word, "disable-gamma-table") == 0) {
  6142. DBG (3, "sane_init: config file line %d: disable-gamma-table\n",
  6143. linenumber);
  6144. disable_gamma_table = SANE_TRUE;
  6145. }
  6146. else if (strcmp (word, "disable-calibration") == 0) {
  6147. DBG (3, "sane_init: config file line %d: disable-calibration\n",
  6148. linenumber);
  6149. disable_calibration = SANE_TRUE;
  6150. }
  6151. else if (strcmp (word, "force-calibration") == 0) {
  6152. DBG (3, "sane_init: config file line %d: force-calibration\n",
  6153. linenumber);
  6154. force_calibration = SANE_TRUE;
  6155. }
  6156. else if (strcmp (word, "force-a4") == 0) {
  6157. DBG (3, "sane_init: config file line %d: enabling force-a4\n",
  6158. linenumber);
  6159. force_a4 = SANE_TRUE;
  6160. }
  6161. else if (strcmp (word, "force-a3") == 0) {
  6162. DBG (3, "sane_init: config file line %d: enabling force-a3\n",
  6163. linenumber);
  6164. force_a3 = SANE_TRUE;
  6165. }
  6166. else if (strcmp (word, "static-red-calib") == 0) {
  6167. DBG (3, "sane_init: config file line %d: static red calibration\n",
  6168. linenumber);
  6169. static_calib_list [0] = SANE_TRUE;
  6170. }
  6171. else if (strcmp (word, "static-green-calib") == 0) {
  6172. DBG (3, "sane_init: config file line %d: static green calibration\n",
  6173. linenumber);
  6174. static_calib_list [1] = SANE_TRUE;
  6175. }
  6176. else if (strcmp (word, "static-blue-calib") == 0) {
  6177. DBG (3, "sane_init: config file line %d: static blue calibration\n",
  6178. linenumber);
  6179. static_calib_list [2] = SANE_TRUE;
  6180. }
  6181. else
  6182. DBG (1, "sane_init: config file line %d: options unknown!\n",
  6183. linenumber);
  6184. }
  6185. else if (strcmp (word, "usb") == 0) {
  6186. DBG (2, "sane_init: config file line %d: trying to attach USB:`%s'\n",
  6187. linenumber, line);
  6188. /* try to attach USB device */
  6189. sanei_usb_attach_matching_devices (line, attach_one_usb);
  6190. }
  6191. else if (strcmp (word, "scsi") == 0) {
  6192. DBG (2, "sane_init: config file line %d: trying to attach SCSI: %s'\n",
  6193. linenumber, line);
  6194. /* the last time I verified (2003-03-18) this function
  6195. only matches SCSI devices ... */
  6196. sanei_config_attach_matching_devices (line, attach_one_scsi);
  6197. }
  6198. else {
  6199. DBG (1, "sane_init: config file line %d: OBSOLETE !! use the scsi keyword!\n",
  6200. linenumber);
  6201. DBG (1, "sane_init: (see man sane-avision for details): trying to attach SCSI: %s'\n",
  6202. line);
  6203. /* the last time I verified (2003-03-18) this function
  6204. only matched SCSI devices ... */
  6205. sanei_config_attach_matching_devices (line, attach_one_scsi);
  6206. }
  6207. free (word);
  6208. word = NULL;
  6209. } /* end while read */
  6210. fclose (fp);
  6211. if (word)
  6212. free (word);
  6213. } /* end if fp */
  6214. /* search for all supported SCSI/USB devices */
  6215. while (Avision_Device_List [model_num].scsi_mfg != NULL ||
  6216. Avision_Device_List [model_num].real_mfg != NULL)
  6217. {
  6218. /* also potentionally accessed in the attach_* callbacks */
  6219. attaching_hw = &(Avision_Device_List [model_num]);
  6220. if (attaching_hw->scsi_mfg != NULL)
  6221. sanei_scsi_find_devices (attaching_hw->scsi_mfg,
  6222. attaching_hw->scsi_model, NULL,
  6223. -1, -1, -1, -1,
  6224. attach_one_scsi);
  6225. if (attaching_hw->usb_vendor != 0 && attaching_hw->usb_product != 0 )
  6226. {
  6227. DBG (1, "sane_init: Trying to find USB device %x %x ...\n",
  6228. attaching_hw->usb_vendor,
  6229. attaching_hw->usb_product);
  6230. /* TODO: check return value */
  6231. if (sanei_usb_find_devices (attaching_hw->usb_vendor,
  6232. attaching_hw->usb_product,
  6233. attach_one_usb) != SANE_STATUS_GOOD) {
  6234. DBG (1, "sane_init: error during USB device detection!\n");
  6235. }
  6236. }
  6237. ++ model_num;
  6238. } /* end for all devices in supported list */
  6239. attaching_hw = 0;
  6240. return SANE_STATUS_GOOD;
  6241. }
  6242. void
  6243. sane_exit (void)
  6244. {
  6245. Avision_Device* dev;
  6246. Avision_Device* next;
  6247. DBG (3, "sane_exit:\n");
  6248. for (dev = first_dev; dev; dev = next) {
  6249. next = dev->next;
  6250. /* no warning for stripping const - C lacks a const_cast<> */
  6251. free ((void*)(size_t) dev->sane.name);
  6252. free (dev);
  6253. }
  6254. first_dev = NULL;
  6255. free(devlist);
  6256. devlist = NULL;
  6257. }
  6258. SANE_Status
  6259. sane_get_devices (const SANE_Device*** device_list, SANE_Bool local_only)
  6260. {
  6261. Avision_Device* dev;
  6262. int i;
  6263. local_only = local_only; /* silence gcc */
  6264. DBG (3, "sane_get_devices:\n");
  6265. if (devlist)
  6266. free (devlist);
  6267. devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
  6268. if (!devlist)
  6269. return SANE_STATUS_NO_MEM;
  6270. i = 0;
  6271. for (dev = first_dev; i < num_devices; dev = dev->next)
  6272. devlist[i++] = &dev->sane;
  6273. devlist[i++] = 0;
  6274. *device_list = devlist;
  6275. return SANE_STATUS_GOOD;
  6276. }
  6277. SANE_Status
  6278. sane_open (SANE_String_Const devicename, SANE_Handle *handle)
  6279. {
  6280. Avision_Device* dev;
  6281. SANE_Status status;
  6282. Avision_Scanner* s;
  6283. int i, j;
  6284. uint8_t inquiry_result[AVISION_INQUIRY_SIZE_V1];
  6285. DBG (3, "sane_open:\n");
  6286. if (devicename[0]) {
  6287. for (dev = first_dev; dev; dev = dev->next)
  6288. if (strcmp (dev->sane.name, devicename) == 0)
  6289. break;
  6290. if (dev) {
  6291. status = attach (devicename, dev->connection.connection_type, &dev);
  6292. if (status != SANE_STATUS_GOOD)
  6293. return status;
  6294. }
  6295. } else {
  6296. /* empty devicname -> use first device */
  6297. dev = first_dev;
  6298. }
  6299. if (!dev)
  6300. return SANE_STATUS_INVAL;
  6301. s = malloc (sizeof (*s));
  6302. if (!s)
  6303. return SANE_STATUS_NO_MEM;
  6304. /* initialize ... */
  6305. memset (s, 0, sizeof (*s));
  6306. /* initialize connection state */
  6307. s->av_con.connection_type = dev->connection.connection_type;
  6308. s->av_con.usb_status = dev->connection.usb_status;
  6309. s->av_con.scsi_fd = -1;
  6310. s->av_con.usb_dn = -1;
  6311. s->reader_pid = -1;
  6312. s->read_fds = -1;
  6313. s->hw = dev;
  6314. /* We initilize the table to a gamma value of 2.22, since this is what
  6315. papers about Colorimetry suggest.
  6316. http://www.poynton.com/GammaFAQ.html
  6317. Avision's driver does default to 2.2 though. */
  6318. {
  6319. const double gamma = 2.22;
  6320. const double one_over_gamma = 1. / gamma;
  6321. for (i = 0; i < 4; ++ i)
  6322. for (j = 0; j < 256; ++ j)
  6323. s->gamma_table[i][j] = pow( (double) j / 255, one_over_gamma) * 255;
  6324. }
  6325. /* the other states (scanning, ...) rely on the memset (0) above */
  6326. /* insert newly opened handle into list of open handles: */
  6327. s->next = first_handle;
  6328. first_handle = s;
  6329. *handle = s;
  6330. /* open the device */
  6331. if (! avision_is_open (&s->av_con) ) {
  6332. #ifdef HAVE_SANEI_SCSI_OPEN_EXTENDED
  6333. DBG (1, "sane_open: using open_extended\n");
  6334. status = avision_open_extended (s->hw->sane.name, &s->av_con, sense_handler, 0,
  6335. &(dev->scsi_buffer_size));
  6336. #else
  6337. status = avision_open (s->hw->sane.name, &s->av_con, sense_handler, 0);
  6338. #endif
  6339. if (status != SANE_STATUS_GOOD) {
  6340. DBG (1, "sane_open: open of %s failed: %s\n",
  6341. s->hw->sane.name, sane_strstatus (status));
  6342. return status;
  6343. }
  6344. DBG (1, "sane_open: got %d scsi_max_request_size\n", dev->scsi_buffer_size);
  6345. }
  6346. /* first: re-awake the device with an inquiry, some devices are flunk while initializing
  6347. the usb connection and like a inquiry to come first ... (AV610 et.al.) */
  6348. status = inquiry (s->av_con, inquiry_result, sizeof(inquiry_result));
  6349. if (status != SANE_STATUS_GOOD) {
  6350. DBG (1, "sane_open: awakening inquiry failed: %s\n", sane_strstatus (status));
  6351. return status;
  6352. }
  6353. /* maybe probe for additional information */
  6354. if (dev->additional_probe)
  6355. {
  6356. status = wait_ready (&s->av_con, 1);
  6357. if (status != SANE_STATUS_GOOD) {
  6358. DBG (1, "sane_open: wait_ready() failed: %s\n", sane_strstatus (status));
  6359. return status;
  6360. }
  6361. status = additional_probe (s);
  6362. if (status != SANE_STATUS_GOOD) {
  6363. DBG (1, "sane_open: additional probe failed: %s\n", sane_strstatus (status));
  6364. return status;
  6365. }
  6366. }
  6367. /* initilize the options */
  6368. init_options (s);
  6369. if (dev->inquiry_duplex_interlaced || dev->scanner_type == AV_FILM) {
  6370. /* Might need at least *DOS (Windows flavour and OS/2) portability fix
  6371. However, I was was told Cygwin (et al.) does keep care of it. */
  6372. strncpy(s->duplex_rear_fname, "/tmp/avision-rear-XXXXXX", PATH_MAX);
  6373. if (! mktemp(s->duplex_rear_fname) ) {
  6374. DBG (1, "sane_open: failed to generate temporary fname for duplex scans\n");
  6375. return SANE_STATUS_NO_MEM;
  6376. }
  6377. else {
  6378. DBG (1, "sane_open: temporary fname for duplex scans: %s\n",
  6379. s->duplex_rear_fname);
  6380. }
  6381. }
  6382. /* calibrate film scanners, as this this must be done without the
  6383. film holder and at the full resolution */
  6384. if (dev->scanner_type == AV_FILM)
  6385. {
  6386. int default_res = s->val[OPT_RESOLUTION].w;
  6387. s->val[OPT_RESOLUTION].w = dev->inquiry_optical_res;
  6388. DBG (1, "sane_open: early calibration for film scanner.\n");
  6389. compute_parameters (s);
  6390. status = set_window (s);
  6391. if (status != SANE_STATUS_GOOD) {
  6392. DBG (1, "sane_open: set scan window command failed: %s\n",
  6393. sane_strstatus (status));
  6394. return status;
  6395. }
  6396. if (!(dev->hw->feature_type & AV_NO_CALIB))
  6397. {
  6398. status = normal_calibration (s);
  6399. if (status != SANE_STATUS_GOOD) {
  6400. DBG (1, "sane_open: perform calibration failed: %s\n",
  6401. sane_strstatus (status));
  6402. return status;
  6403. }
  6404. }
  6405. if (dev->scanner_type == AV_FILM) {
  6406. status = object_position (s, AVISION_SCSI_OP_GO_HOME);
  6407. if (status != SANE_STATUS_GOOD)
  6408. DBG (1, "reader_open: object position go-home failed!\n");
  6409. }
  6410. s->val[OPT_RESOLUTION].w = default_res;
  6411. }
  6412. return SANE_STATUS_GOOD;
  6413. }
  6414. void
  6415. sane_close (SANE_Handle handle)
  6416. {
  6417. Avision_Scanner* prev;
  6418. Avision_Scanner* s = handle;
  6419. int i;
  6420. DBG (3, "sane_close:\n");
  6421. /* close the device */
  6422. if (avision_is_open (&s->av_con) ) {
  6423. avision_close (&s->av_con);
  6424. }
  6425. /* remove handle from list of open handles: */
  6426. prev = 0;
  6427. for (s = first_handle; s; s = s->next) {
  6428. if (s == handle)
  6429. break;
  6430. prev = s;
  6431. }
  6432. /* a handle we know about ? */
  6433. if (!s) {
  6434. DBG (1, "sane_close: invalid handle %p\n", handle);
  6435. return;
  6436. }
  6437. if (s->scanning)
  6438. do_cancel (handle);
  6439. if (prev)
  6440. prev->next = s->next;
  6441. else
  6442. first_handle = s->next;
  6443. for (i = 1; i < NUM_OPTIONS; ++ i) {
  6444. if (s->opt[i].type == SANE_TYPE_STRING && s->val[i].s) {
  6445. free (s->val[i].s);
  6446. }
  6447. }
  6448. if (s->white_avg_data)
  6449. free (s->white_avg_data);
  6450. if (s->dark_avg_data)
  6451. free (s->dark_avg_data);
  6452. if (s->background_raster)
  6453. free (s->background_raster);
  6454. if (*(s->duplex_rear_fname)) {
  6455. unlink (s->duplex_rear_fname);
  6456. *(s->duplex_rear_fname) = 0;
  6457. }
  6458. free (handle);
  6459. }
  6460. const SANE_Option_Descriptor*
  6461. sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
  6462. {
  6463. Avision_Scanner* s = handle;
  6464. DBG (3, "sane_get_option_descriptor:\n");
  6465. if ((unsigned) option >= NUM_OPTIONS)
  6466. return 0;
  6467. return s->opt + option;
  6468. }
  6469. SANE_Status
  6470. sane_control_option (SANE_Handle handle, SANE_Int option,
  6471. SANE_Action action, void* val, SANE_Int* info)
  6472. {
  6473. Avision_Scanner* s = handle;
  6474. Avision_Device* dev = s->hw;
  6475. SANE_Status status;
  6476. SANE_Word cap;
  6477. DBG (3, "sane_control_option: option=%d, action=%d\n",
  6478. (int)option, (int)action);
  6479. DBG (5, "sane_control_option: option=%s, action=%s\n",
  6480. s->opt[option].name,
  6481. action == SANE_ACTION_GET_VALUE ? "GET" :
  6482. (action == SANE_ACTION_SET_VALUE ? "SET" :
  6483. (action == SANE_ACTION_SET_AUTO ? "AUTO" : "UNKNOWN") ) );
  6484. if (info)
  6485. *info = 0;
  6486. if (s->scanning)
  6487. return SANE_STATUS_DEVICE_BUSY;
  6488. if (option >= NUM_OPTIONS)
  6489. return SANE_STATUS_INVAL;
  6490. cap = s->opt[option].cap;
  6491. if (!SANE_OPTION_IS_ACTIVE (cap))
  6492. return SANE_STATUS_INVAL;
  6493. if (action == SANE_ACTION_GET_VALUE)
  6494. {
  6495. switch (option)
  6496. {
  6497. /* word options: */
  6498. case OPT_PREVIEW:
  6499. case OPT_RESOLUTION:
  6500. case OPT_SPEED:
  6501. case OPT_TL_X:
  6502. case OPT_TL_Y:
  6503. case OPT_BR_X:
  6504. case OPT_BR_Y:
  6505. case OPT_OVERSCAN_TOP:
  6506. case OPT_OVERSCAN_BOTTOM:
  6507. case OPT_BACKGROUND:
  6508. case OPT_NUM_OPTS:
  6509. case OPT_BRIGHTNESS:
  6510. case OPT_CONTRAST:
  6511. case OPT_EXPOSURE:
  6512. case OPT_IR:
  6513. case OPT_MULTISAMPLE:
  6514. case OPT_QSCAN:
  6515. case OPT_QCALIB:
  6516. *(SANE_Word*) val = s->val[option].w;
  6517. return SANE_STATUS_GOOD;
  6518. /* specially treated word options */
  6519. case OPT_FRAME:
  6520. status = get_frame_info (s);
  6521. *(SANE_Word*) val = s->val[option].w;
  6522. return status;
  6523. case OPT_POWER_SAVE_TIME:
  6524. get_power_save_time (s, &(s->val[option].w));
  6525. *(SANE_Word*) val = s->val[option].w;
  6526. return SANE_STATUS_GOOD;
  6527. /* word-array options: */
  6528. case OPT_GAMMA_VECTOR:
  6529. case OPT_GAMMA_VECTOR_R:
  6530. case OPT_GAMMA_VECTOR_G:
  6531. case OPT_GAMMA_VECTOR_B:
  6532. memcpy (val, s->val[option].wa, s->opt[option].size);
  6533. return SANE_STATUS_GOOD;
  6534. /* string options: */
  6535. case OPT_MODE:
  6536. case OPT_SOURCE:
  6537. strcpy (val, s->val[option].s);
  6538. return SANE_STATUS_GOOD;
  6539. /* specially treated string options */
  6540. case OPT_MESSAGE:
  6541. if (dev->inquiry_button_control || dev->inquiry_buttons)
  6542. status = get_button_status (s);
  6543. strcpy (val, s->val[option].s);
  6544. s->val[option].s[0] = 0;
  6545. return SANE_STATUS_GOOD;
  6546. case OPT_NVRAM:
  6547. get_and_parse_nvram (s, s->val[option].s, 1024);
  6548. strcpy (val, s->val[option].s);
  6549. return SANE_STATUS_GOOD;
  6550. } /* end switch option */
  6551. } /* end if GET_ACTION_GET_VALUE */
  6552. else if (action == SANE_ACTION_SET_VALUE)
  6553. {
  6554. if (!SANE_OPTION_IS_SETTABLE (cap))
  6555. return SANE_STATUS_INVAL;
  6556. status = constrain_value (s, option, val, info);
  6557. if (status != SANE_STATUS_GOOD)
  6558. return status;
  6559. switch (option)
  6560. {
  6561. /* side-effect-free word options: */
  6562. case OPT_SPEED:
  6563. case OPT_PREVIEW:
  6564. case OPT_BRIGHTNESS:
  6565. case OPT_CONTRAST:
  6566. case OPT_EXPOSURE:
  6567. case OPT_IR:
  6568. case OPT_MULTISAMPLE:
  6569. case OPT_QSCAN:
  6570. case OPT_QCALIB:
  6571. case OPT_OVERSCAN_TOP:
  6572. case OPT_OVERSCAN_BOTTOM:
  6573. case OPT_BACKGROUND:
  6574. s->val[option].w = *(SANE_Word*) val;
  6575. return SANE_STATUS_GOOD;
  6576. /* side-effect-free word-array options: */
  6577. case OPT_GAMMA_VECTOR:
  6578. case OPT_GAMMA_VECTOR_R:
  6579. case OPT_GAMMA_VECTOR_G:
  6580. case OPT_GAMMA_VECTOR_B:
  6581. memcpy (s->val[option].wa, val, s->opt[option].size);
  6582. return SANE_STATUS_GOOD;
  6583. /* options with side-effects: */
  6584. case OPT_RESOLUTION:
  6585. case OPT_TL_X:
  6586. case OPT_TL_Y:
  6587. case OPT_BR_X:
  6588. case OPT_BR_Y:
  6589. s->val[option].w = *(SANE_Word*) val;
  6590. if (info)
  6591. *info |= SANE_INFO_RELOAD_PARAMS;
  6592. return SANE_STATUS_GOOD;
  6593. /* string options with side-effects: */
  6594. case OPT_SOURCE:
  6595. if (s->val[option].s) {
  6596. free(s->val[option].s);
  6597. }
  6598. s->val[option].s = strdup(val);
  6599. s->source_mode = match_source_mode (dev, s->val[option].s);
  6600. s->source_mode_dim = match_source_mode_dim (s->source_mode);
  6601. /* set side-effects */
  6602. dev->x_range.max =
  6603. SANE_FIX ( dev->inquiry_x_ranges[s->source_mode_dim]);
  6604. dev->y_range.max =
  6605. SANE_FIX ( dev->inquiry_y_ranges[s->source_mode_dim]);
  6606. if (info)
  6607. *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
  6608. return SANE_STATUS_GOOD;
  6609. case OPT_MODE:
  6610. {
  6611. if (s->val[option].s)
  6612. free (s->val[option].s);
  6613. s->val[option].s = strdup (val);
  6614. s->c_mode = match_color_mode (dev, s->val[OPT_MODE].s);
  6615. /* set to mode specific values */
  6616. /* the gamma table related */
  6617. if (!disable_gamma_table)
  6618. {
  6619. if (color_mode_is_color (s->c_mode) ) {
  6620. s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
  6621. s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
  6622. s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
  6623. s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
  6624. }
  6625. else /* gray or mono */
  6626. {
  6627. s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
  6628. s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
  6629. s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
  6630. s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
  6631. }
  6632. }
  6633. if (info)
  6634. *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
  6635. return SANE_STATUS_GOOD;
  6636. }
  6637. case OPT_FRAME:
  6638. {
  6639. SANE_Word frame = *((SANE_Word *) val);
  6640. status = set_frame (s, frame);
  6641. if (status == SANE_STATUS_GOOD) {
  6642. s->val[OPT_FRAME].w = frame;
  6643. dev->current_frame = frame;
  6644. }
  6645. return status;
  6646. }
  6647. case OPT_POWER_SAVE_TIME:
  6648. {
  6649. SANE_Word time = *((SANE_Word *) val);
  6650. status = set_power_save_time (s, time);
  6651. if (status == SANE_STATUS_GOOD)
  6652. s->val[OPT_POWER_SAVE_TIME].w = time;
  6653. return status;
  6654. }
  6655. } /* end switch option */
  6656. } /* end else SET_VALUE */
  6657. return SANE_STATUS_INVAL;
  6658. }
  6659. SANE_Status
  6660. sane_get_parameters (SANE_Handle handle, SANE_Parameters* params)
  6661. {
  6662. Avision_Scanner* s = handle;
  6663. DBG (3, "sane_get_parameters:\n");
  6664. /* during an acutal scan the parameters are used and thus are not alllowed
  6665. to change ... if a scan is running the parameters have already been
  6666. computed from sane_start */
  6667. if (!s->scanning)
  6668. {
  6669. DBG (3, "sane_get_parameters: computing parameters\n");
  6670. compute_parameters (s);
  6671. }
  6672. if (params) {
  6673. *params = s->params;
  6674. /* add background raster lines */
  6675. params->lines += s->val[OPT_BACKGROUND].w;
  6676. }
  6677. return SANE_STATUS_GOOD;
  6678. }
  6679. SANE_Status
  6680. sane_start (SANE_Handle handle)
  6681. {
  6682. Avision_Scanner* s = handle;
  6683. Avision_Device* dev = s->hw;
  6684. SANE_Status status;
  6685. int fds [2];
  6686. DBG (1, "sane_start:\n");
  6687. /* Make sure there is no scan running!!! */
  6688. if (s->scanning)
  6689. return SANE_STATUS_DEVICE_BUSY;
  6690. /* Make sure we have a current parameter set. Some of the
  6691. parameters will be overwritten below, but that's OK. */
  6692. status = sane_get_parameters (s, &s->params);
  6693. if (status != SANE_STATUS_GOOD) {
  6694. return status;
  6695. }
  6696. compute_parameters (s);
  6697. /* for non ADF scans (e.g. scanimage --batch-prompt on a Flatbed
  6698. scannner) make sure we do not assume it's an ADF scan and
  6699. optimize something away*/
  6700. if (!is_adf_scan (s))
  6701. s->page = 0;
  6702. if (s->page > 0 && s->duplex_rear_valid) {
  6703. DBG (1, "sane_start: virtual duplex rear data valid.\n");
  6704. goto start_scan_end;
  6705. }
  6706. /* Check for paper during ADF scans and for sheetfeed scanners. */
  6707. if (is_adf_scan (s)) {
  6708. status = media_check (s);
  6709. if (status != SANE_STATUS_GOOD) {
  6710. DBG (1, "sane_start: media_check failed: %s\n",
  6711. sane_strstatus (status));
  6712. return status;
  6713. }
  6714. else
  6715. DBG (1, "sane_start: media_check ok\n");
  6716. }
  6717. /* Check the light early, to return to the GUI and notify the user. */
  6718. if (s->prepared == SANE_FALSE) {
  6719. if (dev->inquiry_light_control) {
  6720. status = wait_4_light (s);
  6721. if (status != SANE_STATUS_GOOD) {
  6722. return status;
  6723. }
  6724. }
  6725. }
  6726. if (s->page > 0 && dev->inquiry_keeps_window) {
  6727. DBG (1, "sane_start: Optimized set_window away.\n");
  6728. }
  6729. else
  6730. {
  6731. status = set_window (s);
  6732. if (status != SANE_STATUS_GOOD) {
  6733. DBG (1, "sane_start: set scan window command failed: %s\n",
  6734. sane_strstatus (status));
  6735. goto stop_scanner_and_return;
  6736. }
  6737. }
  6738. #ifdef DEBUG_TEST
  6739. /* debug window size test ... */
  6740. if (dev->inquiry_new_protocol)
  6741. {
  6742. size_t size = 16;
  6743. uint8_t result[16];
  6744. DBG (5, "sane_start: reading scanner window size\n");
  6745. status = simple_read (s, 0x80, 0, &size, result);
  6746. if (status != SANE_STATUS_GOOD) {
  6747. DBG (1, "sane_start: get pixel size command failed: %s\n",
  6748. sane_strstatus (status));
  6749. goto stop_scanner_and_return;
  6750. }
  6751. debug_print_raw (5, "sane_start: pixel_size:", result, size);
  6752. DBG (5, "sane_start: x-pixels: %d, y-pixels: %d\n",
  6753. get_quad (&(result[0])), get_quad (&(result[4])));
  6754. }
  6755. #endif
  6756. /* no calibration for ADF pages */
  6757. if (s->page > 0) {
  6758. DBG (1, "sane_start: optimized calibration away.\n");
  6759. goto calib_end;
  6760. }
  6761. /* check whether the user enforces calibration */
  6762. if (force_calibration) {
  6763. DBG (1, "sane_start: calibration enforced in config!\n");
  6764. goto calib;
  6765. }
  6766. /* Only perform the calibration for newer scanners - it is not needed
  6767. for my Avision AV 630 - and also does not even work ... */
  6768. if (!dev->inquiry_new_protocol) {
  6769. DBG (1, "sane_start: old protocol no calibration needed!\n");
  6770. goto calib_end;
  6771. }
  6772. if (!dev->inquiry_needs_calibration) {
  6773. DBG (1, "sane_start: due to inquiry no calibration needed!\n");
  6774. goto calib_end;
  6775. }
  6776. /* calibration allowed for this scanner? */
  6777. if (dev->hw->feature_type & AV_NO_CALIB) {
  6778. DBG (1, "sane_start: calibration disabled in device list!!\n");
  6779. goto calib_end;
  6780. }
  6781. /* Not for film scanners, ... */
  6782. if (dev->scanner_type == AV_FILM) {
  6783. DBG (1, "sane_start: no calibration for film scanner!\n");
  6784. goto calib_end;
  6785. }
  6786. /* check whether claibration is disabled by the user */
  6787. if (disable_calibration) {
  6788. DBG (1, "sane_start: calibration disabled in config - skipped!\n");
  6789. goto calib_end;
  6790. }
  6791. /* R² reminder: We must not skip the calibration for ADF scans, some
  6792. scanner (HP 53xx/74xx ASIC series) rely on a calibration data
  6793. read (and will hang otherwise) */
  6794. calib:
  6795. status = normal_calibration (s);
  6796. if (status != SANE_STATUS_GOOD) {
  6797. DBG (1, "sane_start: perform calibration failed: %s\n",
  6798. sane_strstatus (status));
  6799. goto stop_scanner_and_return;
  6800. }
  6801. calib_end:
  6802. if (dev->inquiry_3x3_matrix && dev->inquiry_asic_type >= AV_ASIC_C6 &&
  6803. s->page == 0)
  6804. {
  6805. status = send_3x3_matrix (s);
  6806. if (status != SANE_STATUS_GOOD) {
  6807. return status;
  6808. }
  6809. }
  6810. /* check whether gamma-table is disabled by the user? */
  6811. if (disable_gamma_table) {
  6812. DBG (1, "sane_start: gamma-table disabled in config - skipped!\n");
  6813. goto gamma_end;
  6814. }
  6815. if (dev->hw->feature_type & AV_NO_GAMMA) {
  6816. DBG (1, "sane_start: gamma table skipped due to device-list!!\n");
  6817. goto gamma_end;
  6818. }
  6819. if (s->page > 0 && dev->inquiry_keeps_gamma)
  6820. DBG (1, "sane_start: Optimized send_gamma away.\n");
  6821. else
  6822. {
  6823. status = send_gamma (s);
  6824. if (status != SANE_STATUS_GOOD) {
  6825. DBG (1, "sane_start: send gamma failed: %s\n",
  6826. sane_strstatus (status));
  6827. goto stop_scanner_and_return;
  6828. }
  6829. }
  6830. gamma_end:
  6831. if (dev->inquiry_tune_scan_length && is_adf_scan (s)) {
  6832. status = send_tune_scan_length (s);
  6833. if (status != SANE_STATUS_GOOD) {
  6834. DBG (1, "sane_start: tune_scan_length command failed: %s\n",
  6835. sane_strstatus (status));
  6836. goto stop_scanner_and_return;
  6837. }
  6838. }
  6839. /* if the device supports retrieving background raster data
  6840. inquire the data no matter if the user/applications asks for
  6841. it in order to use it for bottom padding */
  6842. if (s->page == 0 && dev->inquiry_background_raster) {
  6843. status = get_background_raster (s);
  6844. if (status != SANE_STATUS_GOOD) {
  6845. DBG (1, "sane_start: get background raster command failed: %s\n",
  6846. sane_strstatus (status));
  6847. goto stop_scanner_and_return;
  6848. }
  6849. }
  6850. /* check film holder */
  6851. if (dev->scanner_type == AV_FILM && dev->holder_type == 0xff) {
  6852. DBG (1, "sane_start: no film holder or APS cassette!\n");
  6853. /* Normally "go_home" is executed from the reader process,
  6854. but as it will not start we have to reset things here */
  6855. if (dev->inquiry_new_protocol) {
  6856. status = object_position (s, AVISION_SCSI_OP_GO_HOME);
  6857. if (status != SANE_STATUS_GOOD)
  6858. DBG (1, "sane_start: go home failed: %s\n",
  6859. sane_strstatus (status));
  6860. }
  6861. goto stop_scanner_and_return;
  6862. }
  6863. start_scan_end:
  6864. s->scanning = SANE_TRUE;
  6865. s->page += 1; /* procesing next page */
  6866. if (pipe (fds) < 0) {
  6867. return SANE_STATUS_IO_ERROR;
  6868. }
  6869. s->read_fds = fds[0];
  6870. s->write_fds = fds[1];
  6871. /* create reader routine as new process or thread */
  6872. DBG (3, "sane_start: starting thread\n");
  6873. s->reader_pid = sanei_thread_begin (reader_process, (void *) s);
  6874. if (sanei_thread_is_forked())
  6875. close (s->write_fds);
  6876. return SANE_STATUS_GOOD;
  6877. stop_scanner_and_return:
  6878. /* cancel the scan nicely */
  6879. do_cancel (s);
  6880. return status;
  6881. }
  6882. SANE_Status
  6883. sane_read (SANE_Handle handle, SANE_Byte* buf, SANE_Int max_len, SANE_Int* len)
  6884. {
  6885. Avision_Scanner* s = handle;
  6886. ssize_t nread;
  6887. *len = 0;
  6888. DBG (8, "sane_read: max_len: %d\n", max_len);
  6889. nread = read (s->read_fds, buf, max_len);
  6890. if (nread > 0) {
  6891. DBG (8, "sane_read: got %ld bytes\n", (long) nread);
  6892. }
  6893. else {
  6894. DBG (3, "sane_read: got %ld bytes, err: %d %s\n", (long) nread, errno, strerror(errno));
  6895. }
  6896. if (!s->scanning)
  6897. return SANE_STATUS_CANCELLED;
  6898. if (nread < 0) {
  6899. if (errno == EAGAIN) {
  6900. return SANE_STATUS_GOOD;
  6901. } else {
  6902. do_cancel (s);
  6903. return SANE_STATUS_IO_ERROR;
  6904. }
  6905. }
  6906. *len = nread;
  6907. /* if all data was passed through */
  6908. if (nread == 0)
  6909. return do_eof (s);
  6910. return SANE_STATUS_GOOD;
  6911. }
  6912. void
  6913. sane_cancel (SANE_Handle handle)
  6914. {
  6915. Avision_Scanner* s = handle;
  6916. DBG (3, "sane_cancel:\n");
  6917. /* always do the housekeeping, e.g. flush batch scanner pages */
  6918. do_cancel (s);
  6919. }
  6920. SANE_Status
  6921. sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
  6922. {
  6923. Avision_Scanner* s = handle;
  6924. DBG (3, "sane_set_io_mode:\n");
  6925. if (!s->scanning) {
  6926. DBG (3, "sane_set_io_mode: not yet scanning\n");
  6927. return SANE_STATUS_INVAL;
  6928. }
  6929. if (fcntl (s->read_fds, F_SETFL, non_blocking ? O_NONBLOCK : 0) < 0)
  6930. return SANE_STATUS_IO_ERROR;
  6931. return SANE_STATUS_GOOD;
  6932. }
  6933. SANE_Status
  6934. sane_get_select_fd (SANE_Handle handle, SANE_Int* fd)
  6935. {
  6936. Avision_Scanner* s = handle;
  6937. DBG (3, "sane_get_select_fd:\n");
  6938. if (!s->scanning) {
  6939. DBG (3, "sane_get_select_fd: not yet scanning\n");
  6940. return SANE_STATUS_INVAL;
  6941. }
  6942. *fd = s->read_fds;
  6943. return SANE_STATUS_GOOD;
  6944. }