1 #ifndef ARTEC48U_H 2 #define ARTEC48U_H 3 4 #include "../include/sane/sane.h" 5 #include "../include/sane/sanei.h" 6 #include "../include/sane/saneopts.h" 7 #include <sys/types.h> 8 #ifdef HAVE_SYS_IPC_H 9 #include <sys/ipc.h> 10 #endif 11 #include <unistd.h> 12 #include <fcntl.h> 13 #include <errno.h> 14 15 #include "../include/sane/sanei_usb.h" 16 #include "../include/sane/sanei_thread.h" 17 18 #define _MAX_ID_LEN 20 19 20 /*Uncomment next line for button support. This 21 actually isn't supported by the frontends. */ 22 /*#define ARTEC48U_USE_BUTTONS 1*/ 23 24 #define ARTEC48U_PACKET_SIZE 64 25 #define DECLARE_FUNCTION_NAME(name) \ 26 IF_DBG ( static const char function_name[] = name; ) 27 28 typedef SANE_Byte Artec48U_Packet[ARTEC48U_PACKET_SIZE]; 29 #define XDBG(args) do { IF_DBG ( DBG args ); } while (0) 30 31 /* calculate the minimum/maximum values */ 32 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 33 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 34 35 /* return the lower/upper 8 bits of a 16 bit word */ 36 #define HIBYTE(w) ((SANE_Byte)(((SANE_Word)(w) >> 8) & 0xFF)) 37 #define LOBYTE(w) ((SANE_Byte)(w)) 38 39 40 #define CHECK_DEV_NOT_NULL(dev, func_name) \ 41 do { \ 42 if (!(dev)) \ 43 { \ 44 XDBG ((3, "%s: BUG: NULL device\n", (func_name))); \ 45 return SANE_STATUS_INVAL; \ 46 } \ 47 } while (SANE_FALSE) 48 49 /** Check that the device is open. 50 * 51 * @param dev Pointer to the device object (Artec48U_Device). 52 * @param func_name Function name (for use in debug messages). 53 */ 54 #define CHECK_DEV_OPEN(dev, func_name) \ 55 do { \ 56 CHECK_DEV_NOT_NULL ((dev), (func_name)); \ 57 if ((dev)->fd == -1) \ 58 { \ 59 XDBG ((3, "%s: BUG: device %p not open\n", (func_name), (void*)(dev)));\ 60 return SANE_STATUS_INVAL; \ 61 } \ 62 } while (SANE_FALSE) 63 64 #define CHECK_DEV_ACTIVE(dev,func_name) \ 65 do { \ 66 CHECK_DEV_OPEN ((dev), (func_name)); \ 67 if (!(dev)->active) \ 68 { \ 69 XDBG ((3, "%s: BUG: device %p not active\n", \ 70 (func_name), (void*)(dev))); \ 71 return SANE_STATUS_INVAL; \ 72 } \ 73 } while (SANE_FALSE) 74 75 typedef struct Artec48U_Device Artec48U_Device; 76 typedef struct Artec48U_Scan_Request Artec48U_Scan_Request; 77 typedef struct Artec48U_Scanner Artec48U_Scanner; 78 typedef struct Artec48U_Scan_Parameters Artec48U_Scan_Parameters; 79 typedef struct Artec48U_AFE_Parameters Artec48U_AFE_Parameters; 80 typedef struct Artec48U_Exposure_Parameters Artec48U_Exposure_Parameters; 81 typedef struct Artec48U_Line_Reader Artec48U_Line_Reader; 82 typedef struct Artec48U_Delay_Buffer Artec48U_Delay_Buffer; 83 84 enum artec_options 85 { 86 OPT_NUM_OPTS = 0, 87 OPT_MODE_GROUP, 88 OPT_SCAN_MODE, 89 OPT_BIT_DEPTH, 90 OPT_BLACK_LEVEL, 91 OPT_RESOLUTION, 92 OPT_ENHANCEMENT_GROUP, 93 OPT_BRIGHTNESS, 94 OPT_CONTRAST, 95 OPT_GAMMA, 96 OPT_GAMMA_R, 97 OPT_GAMMA_G, 98 OPT_GAMMA_B, 99 OPT_DEFAULT_ENHANCEMENTS, 100 OPT_GEOMETRY_GROUP, 101 OPT_TL_X, 102 OPT_TL_Y, 103 OPT_BR_X, 104 OPT_BR_Y, 105 OPT_CALIBRATION_GROUP, 106 OPT_CALIBRATE, 107 OPT_CALIBRATE_SHADING, 108 #ifdef ARTEC48U_USE_BUTTONS 109 OPT_BUTTON_STATE, 110 #endif 111 /* must come last: */ 112 NUM_OPTIONS 113 }; 114 115 /** Artec48U analog front-end (AFE) parameters. 116 */ 117 struct Artec48U_AFE_Parameters 118 { 119 SANE_Byte r_offset; /**< Red channel offset */ 120 SANE_Byte r_pga; /**< Red channel PGA gain */ 121 SANE_Byte g_offset; /**< Green channel offset (also used for mono) */ 122 SANE_Byte g_pga; /**< Green channel PGA gain (also used for mono) */ 123 SANE_Byte b_offset; /**< Blue channel offset */ 124 SANE_Byte b_pga; /**< Blue channel PGA gain */ 125 }; 126 127 /** TV9693 exposure time parameters. 128 */ 129 struct Artec48U_Exposure_Parameters 130 { 131 SANE_Int r_time; /**< Red exposure time */ 132 SANE_Int g_time; /**< Red exposure time */ 133 SANE_Int b_time; /**< Red exposure time */ 134 }; 135 136 struct Artec48U_Device 137 { 138 Artec48U_Device *next; 139 /** Device file descriptor. */ 140 int fd; 141 /** Device activation flag. */ 142 SANE_Bool active; 143 SANE_String_Const name; 144 SANE_Device sane; /** Scanner model data. */ 145 SANE_String_Const firmware_path; 146 double gamma_master; 147 double gamma_r; 148 double gamma_g; 149 double gamma_b; 150 Artec48U_Exposure_Parameters exp_params; 151 Artec48U_AFE_Parameters afe_params; 152 Artec48U_AFE_Parameters artec_48u_afe_params; 153 Artec48U_Exposure_Parameters artec_48u_exposure_params; 154 155 SANE_Int optical_xdpi; 156 SANE_Int optical_ydpi; 157 SANE_Int base_ydpi; 158 SANE_Int xdpi_offset; /* in optical_xdpi units */ 159 SANE_Int ydpi_offset; /* in optical_ydpi units */ 160 SANE_Int x_size; /* in optical_xdpi units */ 161 SANE_Int y_size; /* in optical_ydpi units */ 162 /* the number of lines, that we move forward before we start reading the 163 shading lines */ 164 int shading_offset; 165 /* the number of lines we read for the black shading buffer */ 166 int shading_lines_b; 167 /* the number of lines we read for the white shading buffer */ 168 int shading_lines_w; 169 170 SANE_Fixed x_offset, y_offset; 171 SANE_Bool read_active; 172 SANE_Byte *read_buffer; 173 size_t requested_buffer_size; 174 size_t read_pos; 175 size_t read_bytes_in_buffer; 176 size_t read_bytes_left; 177 unsigned int is_epro; 178 unsigned int epro_mult; 179 }; 180 181 /** Scan parameters for artec48u_device_setup_scan(). 182 * 183 * These parameters describe a low-level scan request; many such requests are 184 * executed during calibration, and they need to have parameters separate from 185 * the main request (Artec48U_Scan_Request). E.g., on the BearPaw 2400 TA the 186 * scan to find the home position is always done at 300dpi 8-bit mono with 187 * fixed width and height, regardless of the high-level scan parameters. 188 */ 189 struct Artec48U_Scan_Parameters 190 { 191 SANE_Int xdpi; /**< Horizontal resolution */ 192 SANE_Int ydpi; /**< Vertical resolution */ 193 SANE_Int depth; /**< Number of bits per channel */ 194 SANE_Bool color; /**< Color mode flag */ 195 196 SANE_Int pixel_xs; /**< Logical width in pixels */ 197 SANE_Int pixel_ys; /**< Logical height in pixels */ 198 SANE_Int scan_xs; /**< Physical width in pixels */ 199 SANE_Int scan_ys; /**< Physical height in pixels */ 200 SANE_Int scan_bpl; /**< Number of bytes per scan line */ 201 SANE_Bool lineart; /**<Lineart is not really supported by device*/ 202 }; 203 204 205 /** Parameters for the high-level scan request. 206 * 207 * These parameters describe the scan request sent by the SANE frontend. 208 */ 209 struct Artec48U_Scan_Request 210 { 211 SANE_Fixed x0; /**< Left boundary */ 212 SANE_Fixed y0; /**< Top boundary */ 213 SANE_Fixed xs; /**< Width */ 214 SANE_Fixed ys; /**< Height */ 215 SANE_Int xdpi; /**< Horizontal resolution */ 216 SANE_Int ydpi; /**< Vertical resolution */ 217 SANE_Int depth; /**< Number of bits per channel */ 218 SANE_Bool color; /**< Color mode flag */ 219 }; 220 /** Scan action code (purpose of the scan). 221 * 222 * The scan action code affects various scanning mode fields in the setup 223 * command. 224 */ 225 typedef enum Artec48U_Scan_Action 226 { 227 SA_CALIBRATE_SCAN_WHITE, /**< Scan white shading buffer */ 228 SA_CALIBRATE_SCAN_BLACK, /**< Scan black shading buffer */ 229 SA_CALIBRATE_SCAN_OFFSET_1, /**< First scan to determine offset */ 230 SA_CALIBRATE_SCAN_OFFSET_2, /**< Second scan to determine offset */ 231 SA_CALIBRATE_SCAN_EXPOSURE_1, /**< First scan to determine offset */ 232 SA_CALIBRATE_SCAN_EXPOSURE_2, /**< Second scan to determine offset */ 233 SA_SCAN /**< Normal scan */ 234 } 235 Artec48U_Scan_Action; 236 237 238 struct Artec48U_Delay_Buffer 239 { 240 SANE_Int line_count; 241 SANE_Int read_index; 242 SANE_Int write_index; 243 unsigned int **lines; 244 SANE_Byte *mem_block; 245 }; 246 247 struct Artec48U_Line_Reader 248 { 249 Artec48U_Device *dev; /**< Low-level interface object */ 250 Artec48U_Scan_Parameters params; /**< Scan parameters */ 251 252 /** Number of pixels in the returned scanlines */ 253 SANE_Int pixels_per_line; 254 255 SANE_Byte *pixel_buffer; 256 257 Artec48U_Delay_Buffer r_delay; 258 Artec48U_Delay_Buffer g_delay; 259 Artec48U_Delay_Buffer b_delay; 260 SANE_Bool delays_initialized; 261 262 SANE_Status (*read) (Artec48U_Line_Reader * reader, 263 unsigned int **buffer_pointers_return); 264 }; 265 266 #ifndef SANE_OPTION 267 typedef union 268 { 269 SANE_Word w; 270 SANE_Word *wa; /* word array */ 271 SANE_String s; 272 } 273 Option_Value; 274 #endif 275 276 struct Artec48U_Scanner 277 { 278 Artec48U_Scanner *next; 279 Artec48U_Scan_Parameters params; 280 Artec48U_Scan_Request request; 281 Artec48U_Device *dev; 282 Artec48U_Line_Reader *reader; 283 FILE *pipe_handle; 284 SANE_Pid reader_pid; 285 int pipe; 286 int reader_pipe; 287 SANE_Option_Descriptor opt[NUM_OPTIONS]; 288 Option_Value val[NUM_OPTIONS]; 289 SANE_Status exit_code; 290 SANE_Parameters sane_params; 291 SANE_Bool scanning; 292 SANE_Bool eof; 293 SANE_Bool calibrated; 294 SANE_Word gamma_array[4][65536]; 295 SANE_Word contrast_array[65536]; 296 SANE_Word brightness_array[65536]; 297 SANE_Byte *line_buffer; 298 SANE_Byte *lineart_buffer; 299 SANE_Word lines_to_read; 300 unsigned int temp_shading_buffer[3][10240]; /*epro*/ 301 unsigned int *buffer_pointers[3]; 302 unsigned char *shading_buffer_w; 303 unsigned char *shading_buffer_b; 304 unsigned int *shading_buffer_white[3]; 305 unsigned int *shading_buffer_black[3]; 306 unsigned long byte_cnt; 307 }; 308 309 310 /** Create a new Artec48U_Device object. 311 * 312 * The newly created device object is in the closed state. 313 * 314 * @param dev_return Returned pointer to the created device object. 315 * 316 * @return 317 * - #SANE_STATUS_GOOD - the device object was created. 318 * - #SANE_STATUS_NO_MEM - not enough system resources to create the object. 319 */ 320 static SANE_Status artec48u_device_new (Artec48U_Device ** dev_return); 321 322 /** Destroy the device object and release all associated resources. 323 * 324 * If the device was active, it will be deactivated; if the device was open, it 325 * will be closed. 326 * 327 * @param dev Device object. 328 * 329 * @return 330 * - #SANE_STATUS_GOOD - success. 331 */ 332 static SANE_Status artec48u_device_free (Artec48U_Device * dev); 333 334 /** Open the scanner device. 335 * 336 * This function opens the device special file @a dev_name and tries to detect 337 * the device model by its USB ID. 338 * 339 * If the device is detected successfully (its USB ID is found in the supported 340 * device list), this function sets the appropriate model parameters. 341 * 342 * If the USB ID is not recognized, the device remains unconfigured; an attempt 343 * to activate it will fail unless artec48u_device_set_model() is used to force 344 * the parameter set. Note that the open is considered to be successful in 345 * this case. 346 * 347 * @param dev Device object. 348 * @param dev_name Scanner device name. 349 * 350 * @return 351 * - #SANE_STATUS_GOOD - the device was opened successfully (it still may be 352 * unconfigured). 353 */ 354 static SANE_Status artec48u_device_open (Artec48U_Device * dev); 355 356 /** Close the scanner device. 357 * 358 * @param dev Device object. 359 */ 360 static SANE_Status artec48u_device_close (Artec48U_Device * dev); 361 362 /** Activate the device. 363 * 364 * The device must be activated before performing any I/O operations with it. 365 * All device model parameters must be configured before activation; it is 366 * impossible to change them after the device is active. 367 * 368 * This function might need to acquire resources (it calls 369 * Artec48U_Command_Set::activate). These resources will be released when 370 * artec48u_device_deactivate() is called. 371 * 372 * @param dev Device object. 373 * 374 * @return 375 * - #SANE_STATUS_GOOD - device activated successfully. 376 * - #SANE_STATUS_INVAL - invalid request (attempt to activate a closed or 377 * unconfigured device). 378 */ 379 static SANE_Status artec48u_device_activate (Artec48U_Device * dev); 380 381 /** Deactivate the device. 382 * 383 * This function reverses the action of artec48u_device_activate(). 384 * 385 * @param dev Device object. 386 * 387 * @return 388 * - #SANE_STATUS_GOOD - device deactivated successfully. 389 * - #SANE_STATUS_INVAL - invalid request (the device was not activated). 390 */ 391 static SANE_Status artec48u_device_deactivate (Artec48U_Device * dev); 392 393 /** Write a data block to the TV9693 memory. 394 * 395 * @param dev Device object. 396 * @param addr Start address in the TV9693 memory. 397 * @param size Size of the data block in bytes. 398 * @param data Data block to write. 399 * 400 * @return 401 * - #SANE_STATUS_GOOD - success. 402 * - #SANE_STATUS_IO_ERROR - a communication error occurred. 403 * 404 * @warning 405 * @a size must be a multiple of 64 (at least with TV9693), otherwise the 406 * scanner (and possibly the entire USB bus) will lock up. 407 */ 408 static SANE_Status 409 artec48u_device_memory_write (Artec48U_Device * dev, SANE_Word addr, 410 SANE_Word size, SANE_Byte * data); 411 412 /** Read a data block from the TV9693 memory. 413 * 414 * @param dev Device object. 415 * @param addr Start address in the TV9693 memory. 416 * @param size Size of the data block in bytes. 417 * @param data Buffer for the read data. 418 * 419 * @return 420 * - #SANE_STATUS_GOOD - success. 421 * - #SANE_STATUS_IO_ERROR - a communication error occurred. 422 * 423 * @warning 424 * @a size must be a multiple of 64 (at least with TV9693), otherwise the 425 * scanner (and possibly the entire USB bus) will lock up. 426 */ 427 static SANE_Status 428 artec48u_device_memory_read (Artec48U_Device * dev, SANE_Word addr, 429 SANE_Word size, SANE_Byte * data); 430 431 /** Execute a control command. 432 * 433 * @param dev Device object. 434 * @param cmd Command packet. 435 * @param res Result packet (may point to the same buffer as @a cmd). 436 * 437 * @return 438 * - #SANE_STATUS_GOOD - success. 439 * - #SANE_STATUS_IO_ERROR - a communication error occurred. 440 */ 441 static SANE_Status 442 artec48u_device_req (Artec48U_Device * dev, Artec48U_Packet cmd, 443 Artec48U_Packet res); 444 445 /** Execute a "small" control command. 446 * 447 * @param dev Device object. 448 * @param cmd Command packet; only first 8 bytes are used. 449 * @param res Result packet (may point to the same buffer as @a cmd). 450 * 451 * @return 452 * - #SANE_STATUS_GOOD - success. 453 * - #SANE_STATUS_IO_ERROR - a communication error occurred. 454 */ 455 static SANE_Status 456 artec48u_device_small_req (Artec48U_Device * dev, Artec48U_Packet cmd, 457 Artec48U_Packet res); 458 459 /** Read raw data from the bulk-in scanner pipe. 460 * 461 * @param dev Device object. 462 * @param buffer Buffer for the read data. 463 * @param size Pointer to the variable which must be set to the requested data 464 * size before call. After completion this variable will hold the number of 465 * bytes actually read. 466 * 467 * @return 468 * - #SANE_STATUS_GOOD - success. 469 * - #SANE_STATUS_IO_ERROR - a communication error occurred. 470 */ 471 static SANE_Status 472 artec48u_device_read_raw (Artec48U_Device * dev, SANE_Byte * buffer, 473 size_t * size); 474 475 static SANE_Status 476 artec48u_device_set_read_buffer_size (Artec48U_Device * dev, 477 size_t buffer_size); 478 479 static SANE_Status 480 artec48u_device_read_prepare (Artec48U_Device * dev, size_t expected_count); 481 482 static SANE_Status 483 artec48u_device_read (Artec48U_Device * dev, SANE_Byte * buffer, 484 size_t * size); 485 486 static SANE_Status artec48u_device_read_finish (Artec48U_Device * dev); 487 488 489 /** 490 * Create a new Artec48U_Line_Reader object. 491 * 492 * @param dev The low-level scanner interface object. 493 * @param params Scan parameters prepared by artec48u_device_setup_scan(). 494 * @param reader_return Location for the returned object. 495 * 496 * @return 497 * - SANE_STATUS_GOOD - on success 498 * - SANE_STATUS_NO_MEM - cannot allocate memory for object or buffers 499 * - other error values - failure of some internal functions 500 */ 501 static SANE_Status 502 artec48u_line_reader_new (Artec48U_Device * dev, 503 Artec48U_Scan_Parameters * params, 504 Artec48U_Line_Reader ** reader_return); 505 506 /** 507 * Destroy the Artec48U_Line_Reader object. 508 * 509 * @param reader The Artec48U_Line_Reader object to destroy. 510 */ 511 static SANE_Status artec48u_line_reader_free (Artec48U_Line_Reader * reader); 512 513 /** 514 * Read a scanline from the Artec48U_Line_Reader object. 515 * 516 * @param reader The Artec48U_Line_Reader object. 517 * @param buffer_pointers_return Array of pointers to image lines (1 or 3 518 * elements) 519 * 520 * This function reads a full scanline from the device, unpacks it to internal 521 * buffers and returns pointer to these buffers in @a 522 * buffer_pointers_return[i]. For monochrome scan, only @a 523 * buffer_pointers_return[0] is filled; for color scan, elements 0, 1, 2 are 524 * filled with pointers to red, green, and blue data. The returned pointers 525 * are valid until the next call to artec48u_line_reader_read(), or until @a 526 * reader is destroyed. 527 * 528 * @return 529 * - SANE_STATUS_GOOD - read completed successfully 530 * - other error value - an error occurred 531 */ 532 static SANE_Status 533 artec48u_line_reader_read (Artec48U_Line_Reader * reader, 534 unsigned int **buffer_pointers_return); 535 536 static SANE_Status 537 artec48u_download_firmware (Artec48U_Device * dev, 538 SANE_Byte * data, SANE_Word size); 539 540 static SANE_Status 541 artec48u_is_moving (Artec48U_Device * dev, SANE_Bool * moving); 542 543 static SANE_Status artec48u_carriage_home (Artec48U_Device * dev); 544 545 static SANE_Status artec48u_stop_scan (Artec48U_Device * dev); 546 547 static SANE_Status 548 artec48u_setup_scan (Artec48U_Scanner * s, 549 Artec48U_Scan_Request * request, 550 Artec48U_Scan_Action action, 551 SANE_Bool calculate_only, 552 Artec48U_Scan_Parameters * params); 553 554 static SANE_Status 555 artec48u_scanner_new (Artec48U_Device * dev, 556 Artec48U_Scanner ** scanner_return); 557 558 static SANE_Status artec48u_scanner_free (Artec48U_Scanner * scanner); 559 560 static SANE_Status 561 artec48u_scanner_start_scan (Artec48U_Scanner * scanner, 562 Artec48U_Scan_Request * request, 563 Artec48U_Scan_Parameters * params); 564 565 static SANE_Status 566 artec48u_scanner_read_line (Artec48U_Scanner * scanner, 567 unsigned int **buffer_pointers, 568 SANE_Bool shading); 569 570 static SANE_Status artec48u_scanner_stop_scan (Artec48U_Scanner * scanner); 571 572 static SANE_Status 573 artec48u_calculate_shading_buffer (Artec48U_Scanner * s, int start, int end, 574 int resolution, SANE_Bool color); 575 576 static SANE_Status download_firmware_file (Artec48U_Device * chip); 577 578 static SANE_Status 579 artec48u_generic_set_exposure_time (Artec48U_Device * dev, 580 Artec48U_Exposure_Parameters * params); 581 582 static SANE_Status 583 artec48u_generic_set_afe (Artec48U_Device * dev, 584 Artec48U_AFE_Parameters * params); 585 586 static SANE_Status artec48u_generic_start_scan (Artec48U_Device * dev); 587 588 static SANE_Status 589 artec48u_generic_read_scanned_data (Artec48U_Device * dev, SANE_Bool * ready); 590 591 static SANE_Status init_options (Artec48U_Scanner * s); 592 593 static SANE_Status load_calibration_data (Artec48U_Scanner * s); 594 595 static SANE_Status save_calibration_data (Artec48U_Scanner * s); 596 #endif 597