-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathreink.c
1553 lines (1264 loc) · 40.1 KB
/
reink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of ReInk.
* Copyright (C) 2008-2016 Alexey Osipov public@alexey.osipov.name
*
* ReInk is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* ReInk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReInk. If not, see <http://www.gnu.org/licenses/>.
*/
//////
// THIS UTILITY MAY DAMAGE YOUR PRINTER !!!
// USE CAREFULLY
//////
/*
############################################################################
This is EXPERIMENTAL utility to reset ink level in new chiped Epson
cartridges programmatically through regular printer interface (i.e. without
any additional hardware controllers).
This is supposed to be an Open Source analog to Windows program named
"SSC Service Utility".
############################################################################
############################################################################
Basic principle of operation (as I see it).
----------------------------------------------------------------------------
Information about ink levels is stored inside cartridge's chip's EEPROM.
The information stored in form of USED (not REMAINING) ink - so, zero value
means full cartridge.
On power-on, printer reads this info into <Printer's EEPROM> by means of
internal communication with CSIC. CSIC (Customer Specified Integrated Circuits)
is another chip located on printer's head and it "talks" to cartridge's chip.
So we have:
----------------------------------------------------------------------------
<PC>---<Printer's CPU>---<CSIC>---<Cartridge's chip>
| | |
<Printer's EEPROM> <CSIC EEPROM> <Cartridge EEPROM>
Fig.1
----------------------------------------------------------------------------
After each print-job, printer calculates approximate ink usage and writes
this value back to <Printer's EEPROM>.
On power-off, printer writes last-calculated value to <Cartridge EEPROM>
by means of CSIC.
The main idea is to tell printer not to write any values to it's EEPROM or
cartridge. Or tell it to write zero value to it (which means "full").
The possibility of this operation is prooved by "SSC Service Utility" program
for Windows.
Communication between <PC> and <Printer's CPU> can be done by several
protocols: ESCP/2, EJL, D4. As we see from "gutenprint"'s "escputil" sources
getting ink levels on modern printers is done by means of D4.
As I see from communication logs of "SSC Service Utility" with my printer
(Epson Stylus Photo 790) at moment of ink level reset, it uses D4 also. So
let's took d4lib written by Jean-Jacques and use it to reproduce the magic
sequence. :)
############################################################################
############################################################################
Usage
----------------------------------------------------------------------------
Basic usage is:
- to get current ink levels (as done by escputil):
./reink -i -r printer_raw_device
- to dump data from EEPROM
./reink -d <addr>[-<addr>] -r printer_raw_device
<addr> - two-byte address of EEPROM to read
Two addresses recognized as range.
Example: ./reink -d 0000-0FA0 -r /dev/usb/lp0
- to write to arbitary EEPROM address (CAUTION: THIS MAY DAMAGE YOUR PRINTER!)
./reink -w <addr>=<data> -r printer_raw_device
Writes <data> to <addr>
Example: ./reink -w 0206=00 -r /dev/usb/lp0
- to reset ink level for ink type ink_type:
./reink -z[ink_type] -r printer_raw_device
<ink_type> - is the number of ink how it print by -i
if <ink_type> is omitted, then - reset all known inks
Example: ./reink -z1 -r /dev/usb/lp0
- to reset waste ink counter:
./reink -s -r printer_raw_device
- to make an test report, containing some information about your printer
./reink -t -r printer_raw_device > testreport.log
You can set REINK_DEBUG environment variable to enable debug output to
stderr.
REINK_DEBUG=0 - no debug;
REINK_DEBUG=1 - debug only reink.c;
REINK_DEBUG=2 - debug reink.c and d4lib.c also.
############################################################################
*/
#include <stdlib.h> //getenv
#include <unistd.h> //getopt
#include <stdio.h> //printf, stdin, stderr, stdout
#include <string.h> //strdup
#include <sys/types.h> //fileIO
#include <sys/stat.h> //fileIO
#include <fcntl.h> //fileIO
#include <errno.h> //errno
#include <sys/utsname.h> //uname -a
#include "d4lib.h" //IEEE 1284.4
#include "printers.h" //printers defs
#define REINK_VERSION_MAJOR 0
#define REINK_VERSION_MINOR 6
#define REINK_VERSION_REV 0
#define CMD_NONE 0 //no command
#define CMD_GETINK 1 //command to display current ink levels
#define CMD_DUMPEEPROM 2 //command to read from printer's EEPROM
#define CMD_WRITEEEPROM 3 //command to write to printer's EEPROM
#define CMD_ZEROINK 4 //command to reset ink levels
#define CMD_REPORT 5 //command to make test report
#define CMD_ZEROWASTE 6 //command to reset waste ink counter
//EPSON factory commands classes and names
#define EFCMD_EEPROM_READ 0x41
#define EFCLS_EEPROM_READ 0x7c
#define EFCMD_EEPROM_WRITE 0x42
#define EFCLS_EEPROM_WRITE 0x7c
#define INPUT_BUF_LEN 1024
#define D(__c) if (ri_debug) {__c;};
#define D_OK D(fprintf(stderr, "OK\n"))
int ri_debug = 0;
void print_usage(const char* progname);
/* === protocol, channel initialization === */
/*
Tries to connect to raw_device and open it for RW.
Then tries to initialize IEEE 1284.4 packet mode.
On success returns filedescriptor of opened device.
On fail prints various error messages to stderr and returns -1.
*/
int printer_connect(const char* raw_device);
/*
fd - file descriptor for printer raw_device
initialized in IEEE 1284.4 mode.
Tries to carefully exit from IEEE 1284.4 mode
and close printer raw_device (fd).
On success returns 0.
On fail prints various error messages to stderr and returns -1.
*/
int printer_disconnect(int fd);
/*
fd - file descriptor for printer raw_device
initialized in IEEE 1284.4 mode.
Tries to get socket_id for service_name and
then open it.
On success returns positive socket_id.
On fail prints various error messages to stderr and returns -1.
*/
int open_channel(int fd, const char* service_name);
/*
fd - file descriptor for printer raw_device
initialized in IEEE 1284.4 mode.
socket_id - opened IEEE 1284.4 socket.
Tries to close socket_id.
On success returns 0.
On fail prints various error messages to stderr and returns -1.
*/
int close_channel(int fd, int socket_id);
/*
fd - file descriptor for printer raw_device
initialized in IEEE 1284.4 mode.
socket_id - opened IEEE 1284.4 socket.
buf_send - data to send.
send_len - bytes count to send.
buf_recv - buffer for recieved data.
recv_len - IN: maximum length of buf_recv,
OUT: actual bytes read.
Tries to write to printer channel socket_id data specified by
buf_send and read it's answer to buf_recv. Handles IEEE 1284.4
credits internally.
On success returns *recv_len.
On fail prints various error messges to stderr and returns -1.
*/
int printer_transact(int fd, int socket_id, const char* buf_send, int send_len, char* buf_recv, int* recv_len);
/* -------------------------------- */
/* === information === */
unsigned int printer_model(const char* raw_device); //return printer model (PM_*) or PM_UNKNOWN
/* ------------------- */
/* === EPSON factory commands === */
//epson factory command header
typedef struct _fcmd_header_t {
unsigned char cls1;
unsigned char cls2;
unsigned char lenL;
unsigned char lenH;
unsigned char mcode1;
unsigned char mcode2;
unsigned char cmd;
unsigned char cmd1;
unsigned char cmd2;
} fcmd_header_t;
/*
cmd - pointer to uninitialized epson factory command header.
model - printer model.
class - factory command class.
name - factory command name.
extra_length - length of command arguments.
Initialize given header based on passed in information.
If model is pm_unknown, than mcode1 and mcode2 fields is set to zero
and have to be initialized by caller.
*/
void init_command(fcmd_header_t* cmd, unsigned int model, unsigned char class, unsigned char name, unsigned short int extra_length);
/*
fd - file descriptor for printer raw_device
initialized in IEEE 1284.4 mode.
socket_id - opened IEEE 1284.4 socket for
"EPSON-CTRL" service.
Tries to read one byte form printer's EEPROM address <addr> to <data>.
On success returns 0.
On fail returns -1.
*/
int read_eeprom_address(int fd, int socket_id, unsigned int model, unsigned short int addr, unsigned char* data);
/*
fd - file descriptor for printer raw_device
initialized in IEEE 1284.4 mode.
socket_id - opened IEEE 1284.4 socket for
"EPSON-CTRL" service.
Tries to write one byte <data> to printer's EEPROM address <addr>.
On success returns 0.
On fail returns -1.
*/
int write_eeprom_address(int fd, int socket_id, unsigned int model, unsigned short int addr, unsigned char data);
/* ------------------------- */
/* === helpers === */
/*
Searches <source> for string: "<tag>*****;"
Returns ***** as null-terminated string in <value>.
<tag> must be null-terminated string.
On success returns 0.
If not found returns -1.
If found, but insufficient space in value, returns 1.
*/
int get_tag(const char* source, int source_len, const char* tag, char* value, int max_value_len);
/*
Parses buf and prints ink levels info to stdout.
On success returns 0.
On fail returns -1.
*/
int parse_ink_result(const char* buf, int len);
/* --------------- */
/* === main workers === */
int do_ink_levels(const char* raw_device, unsigned int pm);
int do_ink_reset(const char* raw_device, unsigned int pm, unsigned char ink_type);
int do_eeprom_dump(const char* raw_device, unsigned int pm, unsigned short int start_addr, unsigned short int end_addr);
int do_eeprom_write(const char* raw_device, unsigned int pm, unsigned short int addr, unsigned char data);
int do_make_report(const char* raw_device, unsigned char model_code[]);
int do_waste_reset(const char* raw_device, unsigned int pm);
/* -------------------- */
int main(int argc, char** argv)
{
int opt; //current option
int command = CMD_NONE; //command to do
unsigned int pmodel = PM_UNKNOWN; //printer model
char* raw_device = NULL; //-r option argument
char* addr_range = NULL; //-d option argument
unsigned short int addr_s; //start address for CMD_DUMPEEPROM
unsigned short int addr_e; //end address for CMD_DUMPEEPROM
char* write_data = NULL; //-w option argument
unsigned short int write_addr; //write address for CMD_WRITEEEPROM
unsigned short int write_byte; //data to write for CMD_WRITEEEPROM
char* str_ink_type = NULL; //-z option argument
unsigned char ink_type = 0; //ink_type for CMD_ZEROINK
char* str_model_code = NULL; //-t option argument
unsigned char model_code[2]; //model code for CMD_REPORT
char* inval_pos; //used in strtol to indicate conversion error
char onebyte[3]; //holds one-byte hex value ("0A" for example), used in conversion
char* str_reink_debug = NULL; //the value of REINK_DEBUG environmental variable
setDebug(0);
str_reink_debug = getenv("REINK_DEBUG");
if (str_reink_debug)
{
ri_debug = atoi(str_reink_debug);
if (ri_debug > 1)
setDebug(1);
}
onebyte[2] = '\0';
while ((opt = getopt(argc, argv, "sir:d:w:z::t::")) != -1)
{
switch (opt)
{
case 'i':
if (command != CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
command = CMD_GETINK;
break;
case 'r':
raw_device = optarg;
break;
case 'd':
if (command != CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
command = CMD_DUMPEEPROM;
addr_range = optarg;
break;
case 't':
if (command != CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
command = CMD_REPORT;
str_model_code = optarg;
break;
case 'w':
if (command != CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
command = CMD_WRITEEEPROM;
write_data = optarg;
break;
case 'z':
if (command != CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
command = CMD_ZEROINK;
str_ink_type = optarg;
break;
case 's':
if (command != CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
command = CMD_ZEROWASTE;
break;
default:
return 1;
}
}
//parameters checking...
if (command == CMD_NONE)
{
print_usage(argv[0]);
return 1;
}
if (raw_device == NULL)
{
print_usage(argv[0]);
return 1;
}
if (command == CMD_DUMPEEPROM)
{
//check the range parameter..
if (strlen(addr_range) == 4)
{
addr_s = strtol(addr_range, &inval_pos, 16);
if (*inval_pos != '\0')
{
//conversion failed
print_usage(argv[0]);
return 1;
}
addr_e = addr_s;
}
else if ((strlen(addr_range) == 9) && (addr_range[4] == '-'))
{
addr_s = strtol(addr_range, &inval_pos, 16);
if (*inval_pos != '-')
{
//conversion failed
print_usage(argv[0]);
return 1;
}
addr_e = strtol(addr_range+5, &inval_pos, 16);
if (*inval_pos != '\0')
{
//conversion failed
print_usage(argv[0]);
return 1;
}
}
else
{
print_usage(argv[0]);
return 1;
}
if (addr_s > addr_e)
{
print_usage(argv[0]);
return 1;
}
}
else if (command == CMD_WRITEEEPROM)
{
//check write_data parameter
if ((strlen(write_data) == 7) && (write_data[4] == '='))
{
write_addr = strtol(write_data, &inval_pos, 16); //address
if (*inval_pos != '=')
{
//conversion failed
print_usage(argv[0]);
return 1;
}
write_byte = strtol(write_data+5, &inval_pos, 16); //data to write
if (*inval_pos != '\0')
{
//conversion failed
print_usage(argv[0]);
return 1;
}
}
else
{
print_usage(argv[0]);
return 1;
}
}
else if (command == CMD_ZEROINK)
{
//check str_ink_type parameter
if (!str_ink_type)
{
ink_type = 0xFF; //all
}
else
{
ink_type = 1 << (strtol(str_ink_type, &inval_pos, 10) - 1); //one
if (*inval_pos != '\0')
{
//conversion failed
print_usage(argv[0]);
return 1;
}
}
}
else if (command == CMD_REPORT)
{
if (str_model_code)
{
if (strlen(str_model_code) != 4)
{
print_usage(argv[0]);
return 1;
}
model_code[1] = strtol(str_model_code + 2, &inval_pos, 16);
if (*inval_pos != '\0')
{
print_usage(argv[0]);
return 1;
}
str_model_code[2] = '\0';
model_code[0] = strtol(str_model_code, &inval_pos, 16);
if (*inval_pos != '\0')
{
print_usage(argv[0]);
return 1;
}
}
else
{
model_code[0] = 0x00;
model_code[1] = 0x00;
}
}
//end of options parsing
//CMD_REPORT is a special case
if (command == CMD_REPORT)
return do_make_report(raw_device, model_code);
//identifing printer
pmodel = printer_model(raw_device);
if (pmodel == PM_UNKNOWN)
{
fprintf(stderr, "Unknown printer. Wrong device file?\n");
return 1;
}
switch (command)
{
case CMD_GETINK:
return do_ink_levels(raw_device, pmodel);
break;
case CMD_DUMPEEPROM:
return do_eeprom_dump(raw_device, pmodel, addr_s, addr_e);
break;
case CMD_WRITEEEPROM:
return do_eeprom_write(raw_device, pmodel, write_addr, write_byte);
break;
case CMD_ZEROINK:
return do_ink_reset(raw_device, pmodel, ink_type);
break;
case CMD_ZEROWASTE:
return do_waste_reset(raw_device, pmodel);
break;
default:
fprintf(stderr, "Unknown command.\n");
return 1;
}
return 0;
}
void print_usage(const char* progname)
{
fprintf(stderr, "ReInk v%d.%d.%d (http://reink.lerlan.ru)\n\
Basic usage is:\n\
- to get current ink levels (as done by escputil):\n\
%s -i -r printer_raw_device\n\
\n\
- to dump data from EEPROM\n\
%s -d <addr>[-<addr>] -r printer_raw_device\n\
<addr> - two-byte address of EEPROM to read\n\
Two addresses recognized as range.\n\
Example: %s -d 0000-A000 -r /dev/usb/lp0\n\
\n\
- to write to arbitary EEPROM address (CAUTION: THIS MAY DAMAGE YOUR PRINTER!)\n\
%s -w <addr>=<data> -r printer_raw_device\n\
Writes <data> to <addr>\n\
Example: %s -w 0006=00 -r /dev/usb/lp0\n\
\n\
- to reset ink level for ink type ink_type:\n\
%s -z[ink_type] -r printer_raw_device\n\
<ink_type> - is the number of ink how it print by -i\n\
if <ink_type> is omitted, then - reset all known inks\n\
Example: %s -z1 -r /dev/usb/lp0\n\
\n\
- to reset waste ink counter:\n\
%s -s -r printer_raw_device\n\
\n\
- to make an test report, containing some information about your printer\n\
./reink -t -r printer_raw_device > testreport.log\n\
\n\
You can set REINK_DEBUG environment variable to enable debug output to\n\
stderr.\n\
REINK_DEBUG=0 - no debug;\n\
REINK_DEBUG=1 - debug only reink.c;\n\
REINK_DEBUG=2 - debug reink.c and d4lib.c also.\n",
REINK_VERSION_MAJOR, REINK_VERSION_MINOR, REINK_VERSION_REV,
progname, progname, progname, progname, progname, progname, progname, progname);
}
/////////////////////////////////////////////////////////////////////////////////
// MAIN WORKERS
/////////////////////////////////////////////////////////////////////////////////
//
int do_ink_levels(const char* raw_device, unsigned int pmodel)
{
int device; //file descriptor of the printer raw_device
int ctrl_socket; //IEEE 1284.4 socket identifier for "EPSON-CTRL" channel
char buf[INPUT_BUF_LEN]; //buffer for input data
int readed; //number of readed bytes
D(fprintf(stderr, "=== do_ink_levels ===\n"))
if ((device = printer_connect(raw_device)) < 0)
return 1;
if ((ctrl_socket = open_channel(device, "EPSON-CTRL")) < 0)
return 1;
D(fprintf(stderr, "Everything seems to be ready. :) Let's get ink level. Executing \"st\" command... "))
readed = INPUT_BUF_LEN;
if (printer_transact(device, ctrl_socket, "st\1\0\1", 5, buf, &readed))
return 1;
D_OK
D(fprintf(stderr, "Parsing result... "))
if (parse_ink_result(buf, readed))
{
fprintf(stderr, "FAIL.\n");
return 1;
}
D_OK
if (close_channel(device, ctrl_socket) < 0)
return 1;
if (printer_disconnect(device) < 0)
return 1;
D(fprintf(stderr, "^^^ do_ink_levels ^^^\n"))
return 0;
}
int do_ink_reset(const char* raw_device, unsigned int pm, unsigned char ink_type)
{
int i;
unsigned char cur_ink;
unsigned char* cur_addr;
int device; //file descriptor of the printer raw_device
int ctrl_socket; //IEEE 1284.4 socket identifier for "EPSON-CTRL" channel
D(fprintf(stderr, "=== do_ink_reset ===\n"))
if (pm == PM_UNKNOWN)
return 1;
if ((device = printer_connect(raw_device)) < 0)
return 1;
if ((ctrl_socket = open_channel(device, "EPSON-CTRL")) < 0)
return 1;
for(cur_ink = 1; cur_ink != 0x80; cur_ink <<= 1)
{
if (!(cur_ink & ink_type))
continue;
if (!(printers[pm].inkmap.mask & cur_ink))
{
if (ink_type == 0xFF) //reset all inks
continue;
fprintf(stderr, "Printer \"%s\" doesn't have ink bit %d.\n", printers[pm].name, cur_ink);
return 1;
}
switch(cur_ink)
{
case INK_BLACK:
cur_addr = printers[pm].inkmap.black;
break;
case INK_CYAN:
cur_addr = printers[pm].inkmap.cyan;
break;
case INK_MAGENTA:
cur_addr = printers[pm].inkmap.magenta;
break;
case INK_YELLOW:
cur_addr = printers[pm].inkmap.yellow;
break;
case INK_LIGHTCYAN:
cur_addr = printers[pm].inkmap.lightcyan;
break;
case INK_LIGHTMAGENTA:
cur_addr = printers[pm].inkmap.lightmagenta;
break;
default:
fprintf(stderr, "Unknown ink bit %d.\n", cur_ink);
return 1;
}
D(fprintf(stderr, "Resetting ink bit %d... ", cur_ink));
for (i=0;i<4;i++)
if (write_eeprom_address(device, ctrl_socket, pm, cur_addr[i], 0x00))
{
fprintf(stderr, "Can't write to eeprom.\n");
return 1;
}
D_OK
}
if (close_channel(device, ctrl_socket) < 0)
return 1;
if (printer_disconnect(device) < 0)
return 1;
D(fprintf(stderr, "^^^ do_ink_reset ^^^\n"))
return 0;
}
int do_eeprom_dump(const char* raw_device, unsigned int pm, unsigned short int start_addr, unsigned short int end_addr)
{
int device; //file descriptor of the printer raw_device
int ctrl_socket; //IEEE 1284.4 socket identifier for "EPSON-CTRL" channel
unsigned char data; //eeprom data (one byte)
unsigned short int cur_addr; //current address
int i;
D(fprintf(stderr, "=== do_eeprom_dump ===\n"))
if (pm == PM_UNKNOWN)
return 1;
if ((device = printer_connect(raw_device)) < 0)
return 1;
if ((ctrl_socket = open_channel(device, "EPSON-CTRL")) < 0)
return 1;
if (!printers[pm].twobyte_addresses && (end_addr & 0xFF00))
{
fprintf(stderr, "Printer \"%s\" doesn't support two-byte addresses, I will use lower byte only.\n", printers[pm].name);
start_addr &= 0xFF;
end_addr &= 0xFF;
}
D(fprintf(stderr, "Let's get the EEPROM dump (%x - %x)...\n", start_addr, end_addr))
for (cur_addr = start_addr; (cur_addr <= end_addr) && (cur_addr >= start_addr); cur_addr++)
{
if (read_eeprom_address(device, ctrl_socket, pm, cur_addr, &data))
{
fprintf(stderr, "Fail to read EEPROM data from address %x.\n", cur_addr);
return 1;
}
printf("0x%04X = 0x%02X\n", cur_addr, data);
if (cur_addr == end_addr)
break; //or there may be short int overflow and infinite loop
}
D_OK
if (close_channel(device, ctrl_socket) < 0)
return 1;
if (printer_disconnect(device) < 0)
return 1;
D(fprintf(stderr, "^^^ do_eeprom_dump ^^^\n"))
return 0;
}
int do_eeprom_write(const char* raw_device, unsigned int pm, unsigned short int addr, unsigned char data)
{
int device; //file descriptor of the printer raw_device
int ctrl_socket; //IEEE 1284.4 socket identifier for "EPSON-CTRL" channel
unsigned char readed_data; //verification data
D(fprintf(stderr, "=== do_eeprom_write ===\n"))
if (pm == PM_UNKNOWN)
return 1;
if ((device = printer_connect(raw_device)) < 0)
return 1;
if ((ctrl_socket = open_channel(device, "EPSON-CTRL")) < 0)
return 1;
D(fprintf(stderr, "Let's write %#x to EEPROM address %#x...\n", data, addr))
if (write_eeprom_address(device, ctrl_socket, pm, addr, data))
{
fprintf(stderr, "Fail to write EEPROM data to address %#x.\n", addr);
return 1;
}
D_OK
D(fprintf(stderr, "Verify by reading that byte... "))
if (read_eeprom_address(device, ctrl_socket, pm, addr, &readed_data))
{
fprintf(stderr, "Fail to subsequent read from EEPROM address %#x.\n", addr);
return 1;
}
if (readed_data != data)
{
fprintf(stderr, "Verification failed (readed byte = %#x).\n", readed_data);
return 1;
}
D_OK
if (close_channel(device, ctrl_socket) < 0)
return 1;
if (printer_disconnect(device) < 0)
return 1;
D(fprintf(stderr, "^^^ do_eeprom_write ^^^\n"))
return 0;
}
int do_waste_reset(const char* raw_device, unsigned int pm)
{
int i;
int device; //file descriptor of the printer raw_device
int ctrl_socket; //IEEE 1284.4 socket identifier for "EPSON-CTRL" channel
D(fprintf(stderr, "=== do_waste_reset ===\n"))
if (pm == PM_UNKNOWN)
return 1;
if ((device = printer_connect(raw_device)) < 0)
return 1;
if ((ctrl_socket = open_channel(device, "EPSON-CTRL")) < 0)
return 1;
D(fprintf(stderr, "Resetting... "));
for (i=0;i<printers[pm].wastemap.len;i++)
if (write_eeprom_address(device, ctrl_socket, pm, printers[pm].wastemap.addr[i], 0x00))
{
fprintf(stderr, "Can't write to eeprom.\n");
return 1;
}
D_OK
if (close_channel(device, ctrl_socket) < 0)
return 1;
if (printer_disconnect(device) < 0)
return 1;
D(fprintf(stderr, "^^^ do_waste_reset ^^^\n"))
return 0;
}
/*
What we need to know about unknown printer?
1) name
2) ability to use ieee1284.4
3) secret model code
4) cmds support: one-byte address read/write, two-byte address read/write
5) eeprom map (at least addresses with inks usage)
*/
int do_make_report(const char* raw_device, unsigned char model_code[])
{
struct utsname linux_info; //uname -a reply
int fd; //raw_device file descriptor
int socket2; //socket for EPSON-CTRL
int socket40; //socket for EPSON-DATA
int have_model_code = 0; //do we have model code?
char buf[INPUT_BUF_LEN]; //buffer for input data
int readed; //length of data in input buffer
unsigned short int caddr; //current address
unsigned char data; //one byte from eeprom
int original_stderr;
int original_debug = ri_debug; //original value of ri_debug
printf("ReInk v%d.%d test report.\n", REINK_VERSION_MAJOR, REINK_VERSION_MINOR);
fprintf(stderr, "Please, be patient.\nWait at least 10 minutes before force interrupt.\n");
//uname -a
if (!uname(&linux_info))
{
printf("sysname: %s\nrelease: %s\nvarsion: %s\narch: %s\n\n", linux_info.sysname,
linux_info.release,
linux_info.version,
linux_info.machine);
}
fflush(stdout);
//redirecting stderr to stdout (2>1)
original_stderr = dup(fileno(stderr));
dup2(fileno(stdout), fileno(stderr));
//enabling debug info
setDebug(1);
ri_debug=1;
//raw_device (r/w status)
//can enter in ieee1284.4 mode?
if ((fd = printer_connect(raw_device)) < 0)
return 0;
//opening EPSON-CTRL
if ((socket2 = open_channel(fd, "EPSON-CTRL")) < 0)
{
printer_disconnect(fd);
return 0;
}
//opening EPSON-DATA (just try to open - then close)
if ((socket40 = open_channel(fd, "EPSON-DATA")) >= 0)
close_channel(fd, socket40); //no need anymore
//reply to "di" command
readed = INPUT_BUF_LEN;
if (printer_transact(fd, socket2, "di\1\0\1", 5, buf, &readed) < 0)
{
close_channel(fd, socket2);
printer_disconnect(fd);
return 0;
}
//reply to "st" command
readed = INPUT_BUF_LEN;
if (printer_transact(fd, socket2, "st\1\0\1", 5, buf, &readed) < 0)
{
close_channel(fd, socket2);
printer_disconnect(fd);
return 0;
}
//init unknown printer
printers[PM_UNKNOWN].model_code[0] = model_code[0];
printers[PM_UNKNOWN].model_code[1] = model_code[1];
//assume first this is two-byte addresses printer
printers[PM_UNKNOWN].twobyte_addresses = 1;
if (model_code[0] != 0 || model_code[1] != 0)
have_model_code = 1;
else
{
printf("Searching printer secret model code with brute force.\n");
fflush(stdout);
}
//let's find out the model code
while (printers[PM_UNKNOWN].model_code[0] != 0xFF && !have_model_code)
{
//try to read from eeprom (address 00)
if (read_eeprom_address(fd, socket2, PM_UNKNOWN, 0x00, &data) == 0)
{
printf("We found model code: 0x%02X 0x%02X\n", printers[PM_UNKNOWN].model_code[0], printers[PM_UNKNOWN].model_code[1]);
have_model_code = 1;
break;
}