Debian Codegraph @

src/cups/backend_common.h

raw symbols SCIP
1 /*
2 * CUPS Backend common code
3 *
4 * (c) 2013-2026 Solomon Peachy <[email protected]>
5 *
6 * The latest version of this program can be found at:
7 *
8 * https://git.shaftnet.org/gitea/slp/selphy_print.git
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 *
23 * SPDX-License-Identifier: GPL-2.0+
24 *
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <time.h>
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37
38 #include <libusb.h>
39
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wcast-qual"
42
43 /* For Integration into gutenprint */
44 #if defined(HAVE_CONFIG_H)
45 #include <config.h>
46 #endif
47
48 #ifndef __BACKEND_COMMON_H
49 #define __BACKEND_COMMON_H
50
51 #ifdef ERROR
52 #undef ERROR
53 #endif
54
55 #define STR_LEN_MAX 64
56 #define STATE( ... ) do { if (!quiet) fprintf(logger, "STATE: " __VA_ARGS__ ); } while(0)
57 #define ATTR( ... ) do { if (!quiet) fprintf(logger, "ATTR: " __VA_ARGS__ ); } while(0)
58 #define PAGE( ... ) do { if (!quiet) fprintf(logger, "PAGE: " __VA_ARGS__ ); } while(0)
59 #define DEBUG( ... ) do { if (!quiet) fprintf(logger, "DEBUG: " __VA_ARGS__ ); } while(0)
60 #define DEBUG2( ... ) do { if (!quiet) fprintf(logger, __VA_ARGS__ ); } while(0)
61 #define INFO( ... ) do { if (!quiet) fprintf(logger, "INFO: " __VA_ARGS__ ); } while(0)
62 #define WARNING( ... ) do { if (!quiet) fprintf(logger, "WARNING: " __VA_ARGS__ ); } while(0)
63 #define ERROR( ... ) do { fprintf(logger, "ERROR: " __VA_ARGS__ ); sleep(1); } while (0)
64 #define PPD( ... ) do { fprintf(logger, "PPD: " __VA_ARGS__ ); } while (0)
65
66 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
67 #define le16_to_cpu(__x) __x
68 #define le32_to_cpu(__x) __x
69 #define be16_to_cpu(__x) __builtin_bswap16(__x)
70 #define be32_to_cpu(__x) __builtin_bswap32(__x)
71 #else
72 #define le16_to_cpu(__x) __builtin_bswap16(__x)
73 #define le32_to_cpu(__x) __builtin_bswap32(__x)
74 #define be32_to_cpu(__x) __x
75 #define be16_to_cpu(__x) __x
76 #endif
77
78 #define cpu_to_le16 le16_to_cpu
79 #define cpu_to_le32 le32_to_cpu
80 #define cpu_to_be16 be16_to_cpu
81 #define cpu_to_be32 be32_to_cpu
82
83 /* To cheat the compiler */
84 #define UNUSED(expr) do { (void)(expr); } while (0)
85
86 /* Compile-time assertions */
87 #define STATIC_ASSERT(test_for_true) _Static_assert((test_for_true), "(" #test_for_true ") failed")
88
89 /* IEEE1284 ID processing */
90 struct deviceid_dict {
91 char *key;
92 char *val;
93 };
94 #define MAX_DICT 32
95 int parse1284_data(const char *device_id, struct deviceid_dict* dict);
96 char *dict_find(const char *key, int dlen, struct deviceid_dict* dict);
97 char *get_device_id(struct libusb_device_handle *dev, int iface);
98
99 /* To enumerate supported devices */
100 enum {
101 P_UNKNOWN = 0,
102 P_CITIZEN_CW01,
103 P_CITIZEN_OP900II,
104 P_CPGENERIC,
105 P_CP10,
106 P_CP790,
107 P_CP900,
108 P_CP910,
109 P_DNP_DS40,
110 P_DNP_DS80,
111 P_DNP_DS80D,
112 P_DNP_DS620,
113 P_DNP_DS820,
114 P_DNP_DSRX1,
115 P_DNP_QW410,
116 P_DNP_DSX80,
117 P_ES1,
118 P_ES2_20,
119 P_ES3_30,
120 P_ES40,
121 P_FUJI_ASK300,
122 P_FUJI_ASK500,
123 P_HITI_CS2XX,
124 P_HITI_310,
125 P_HITI_320,
126 P_HITI_461,
127 P_HITI_51X,
128 P_HITI_520,
129 P_HITI_525,
130 P_HITI_530,
131 P_HITI_630,
132 P_HITI_720,
133 P_HITI_750,
134 P_HITI_826,
135 P_HITI_910,
136 P_SWIFTFOTO_KSF10,
137 P_KODAK_1400_805,
138 P_KODAK_305,
139 P_KODAK_605,
140 P_KODAK_6800,
141 P_KODAK_6850,
142 P_KODAK_6900,
143 P_KODAK_7000,
144 P_KODAK_701X,
145 P_KODAK_8800,
146 P_KODAK_8810,
147 P_MAGICARD,
148 P_NDC,
149 P_MITSU_9500S,
150 P_MITSU_9550,
151 P_MITSU_9550S,
152 P_MITSU_9600,
153 P_MITSU_9600S,
154 P_MITSU_9800,
155 P_MITSU_9800S,
156 P_MITSU_9810,
157 P_MITSU_9820S,
158 P_MITSU_D70X,
159 P_MITSU_D80,
160 P_MITSU_D90,
161 P_MITSU_K60,
162 P_MITSU_M1,
163 P_MITSU_W5000,
164 P_MITSU_P93D,
165 P_MITSU_P95D,
166 P_MITSU_CP30D,
167 P_SHINKO_S1245,
168 P_SHINKO_S2145,
169 P_SHINKO_S2245,
170 P_SHINKO_S6145,
171 P_SHINKO_S6145D,
172 P_SHINKO_S6245,
173 P_SONY_UPCR10,
174 P_SONY_UPCR20L,
175 P_SONY_UPD711,
176 P_SONY_UPD895,
177 P_SONY_UPD897,
178 P_SONY_UPD898,
179 P_SONY_UP9x1,
180 P_SONY_UPDR150,
181 P_SONY_UPDR80,
182 P_END,
183 };
184
185 struct device_id {
186 uint16_t vid;
187 uint16_t pid;
188 int type; /* P_** */
189 const char *manuf_str;
190 const char *make;
191 };
192
193 struct marker {
194 const char *color; /* Eg "#00FFFF" */
195 const char *name; /* Eg "CK9015 (4x6)" */
196 int levelmax; /* Max media count, eg '600', or '-1' */
197 int levelnow; /* Remaining media, -3, -2, -1, 0..N. See CUPS. */
198 int numtype; /* Numerical type, (-1 for unknown) */
199 };
200
201 #define DECKS_MAX 2
202 struct printerstats {
203 time_t timestamp;
204 const char *mfg; /* Manufacturer */
205 const char *model; /* Model */
206 const char *serial; /* Serial Number */
207 const char *fwver; /* Firmware Version */
208 uint8_t decks; /* Number of "decks" (1 or 2) */
209
210 const char *name[DECKS_MAX]; /* Name */
211 char *status[DECKS_MAX]; /* Status (dynamic) */
212 const char *mediatype[DECKS_MAX]; /* Media Type */
213 int32_t levelmax[DECKS_MAX]; /* Max media count (-1 if unknown) */
214 int32_t levelnow[DECKS_MAX]; /* Remaining media count (-1 if unknown) */
215 int32_t cnt_life[DECKS_MAX]; /* Lifetime prints */
216 };
217
218 struct dyesub_connection {
219 struct libusb_device_handle *dev;
220 uint8_t endp_up;
221 uint8_t endp_down;
222 uint8_t iface;
223 uint8_t altset;
224
225 /* to make our lives easier later */
226 uint8_t bus_num;
227 uint8_t port_num;
228 uint16_t usb_vid;
229 uint16_t usb_pid;
230
231 // TODO: mutex/lock
232
233 int type; /* P_XXXX */
234 };
235
236 #define DYESUB_MAX_JOB_ENTRIES 3
237
238 struct dyesub_joblist {
239 // TODO: mutex/lock
240 const struct dyesub_backend *backend;
241 void *ctx;
242 int num_entries;
243 int copies;
244 const void *entries[DYESUB_MAX_JOB_ENTRIES];
245 };
246 #define MAX_JOBS_FROM_READ_PARSE 3
247
248 /* This MUST be the start of every per-printer job struct! */
249 struct dyesub_job_common {
250 size_t jobsize;
251 int copies;
252 int can_combine;
253 };
254
255 /* Panorama stuff */
256 struct panodata_row {
257 uint16_t start_row;
258 double rhYMC[3];
259 double lhYMC[3];
260 };
261
262 #define DNP_PANO_MAXROWS 64
263 struct dnp_panodata {
264 uint16_t elements;
265 struct panodata_row rows[DNP_PANO_MAXROWS];
266 };
267
268 /* Exported functions */
269 int send_data(struct dyesub_connection *conn, const uint8_t *buf, int len);
270 int read_data(struct dyesub_connection *conn,
271 uint8_t *buf, int buflen, int *readlen);
272
273 void dump_markers(const struct marker *markers, int marker_count, int full);
274
275 void print_license_blurb(void);
276 void print_help(const char *argv0, const struct dyesub_backend *backend);
277
278 int dyesub_read_file2(const char *filename, void *databuf, int datalen,
279 int *actual_len, int fail_ok);
280
281 #define dyesub_read_file(__fname, __databuf, __datalen, __actual_len) \
282 dyesub_read_file2(__fname, __databuf, __datalen, __actual_len, 0)
283
284 uint16_t uint16_to_packed_bcd(uint16_t val);
285 uint32_t packed_bcd_to_uint32(const char *in, int len);
286
287 void generic_teardown(void *vctx);
288
289 /* USB enumeration and attachment */
290 #define NUM_CLAIM_ATTEMPTS 10
291 int backend_claim_interface(struct libusb_device_handle *dev, int iface,
292 int num_claim_attempts);
293
294 /* Job list manipulation */
295 struct dyesub_joblist *dyesub_joblist_create(const struct dyesub_backend *backend, void *ctx);
296 int dyesub_joblist_appendjob(struct dyesub_joblist *list, const void *job);
297 void dyesub_joblist_cleanup(const struct dyesub_joblist *list);
298 int dyesub_joblist_print(const struct dyesub_joblist *list, int *pagenum);
299 const void *dyesub_joblist_popjob(struct dyesub_joblist *list);
300 int dyesub_joblist_canwait(struct dyesub_joblist *list);
301
302 #define BACKEND_FLAG_BADISERIAL 0x00000001
303 #define BACKEND_FLAG_DUMMYPRINT 0x00000002
304
305 void dyesub_pano_split_rgb8(const uint8_t *src, uint16_t cols, uint8_t numpanels,
306 uint16_t overlap_rows, uint16_t pad_rows,
307 uint16_t *panel_rows, uint8_t **panels);
308
309 /* Backend Functions */
310 struct dyesub_backend {
311 /* Required properties */
312 const char *name;
313 const char *version;
314 const char **uri_prefixes;
315 const struct device_id *devices;
316
317 /* Optional properties */
318 const uint32_t flags;
319
320 /* Required functions */
321 void *(*init)(void);
322 int (*attach)(void *ctx, struct dyesub_connection *conn, uint8_t jobid);
323 int (*cmdline_arg)(void *ctx, int argc, char **argv);
324 int (*read_parse)(void *ctx, const void **job, int data_fd, int copies);
325 int (*main_loop)(void *ctx, const void *job, int wait_on_return);
326 void (*cleanup_job)(const void *job);
327
328 /* Required if backend implemnets job combining */
329 void *(*combine_jobs)(const void *job1, const void *job2);
330 int (*job_polarity)(void *ctx);
331
332 /* Optional stuff */
333 void (*teardown)(void *ctx);
334 void (*cmdline_usage)(void);
335 int (*query_serno)(struct dyesub_connection *conn, char *buf, int buf_len);
336 int (*query_markers)(void *ctx, struct marker **markers, int *count);
337 int (*query_stats)(void *ctx, struct printerstats *stats);
338 };
339
340 /* Global data */
341 extern int terminate;
342 extern int dyesub_debug;
343 extern int fast_return;
344 extern int extra_vid;
345 extern int extra_pid;
346 extern int extra_type;
347 extern int ncopies;
348 extern int collate;
349 extern int test_mode;
350 extern int quiet;
351 extern const char *corrtable_path;
352 extern FILE *logger;
353 extern int stats_only;
354
355 enum {
356 TEST_MODE_NONE = 0,
357 TEST_MODE_NOPRINT,
358 TEST_MODE_NOATTACH,
359 TEST_MODE_MAX,
360 };
361
362 #if defined(BACKEND)
363 extern const struct dyesub_backend BACKEND;
364 #endif
365
366 /* CUPS compatibility */
367 #define CUPS_BACKEND_OK 0 /* Success */
368 #define CUPS_BACKEND_FAILED 1 /* Failed to print use CUPS policy */
369 #define CUPS_BACKEND_AUTH_REQUIRED 2 /* Auth required */
370 #define CUPS_BACKEND_HOLD 3 /* Hold this job only */
371 #define CUPS_BACKEND_STOP 4 /* Stop the entire queue */
372 #define CUPS_BACKEND_CANCEL 5 /* Cancel print job */
373 #define CUPS_BACKEND_RETRY 6 /* Retry later */
374 #define CUPS_BACKEND_RETRY_CURRENT 7 /* Retry immediately */
375
376 #define CUPS_MARKER_UNAVAILABLE -1
377 #define CUPS_MARKER_UNKNOWN -2
378 #define CUPS_MARKER_UNKNOWN_OK -3
379
380 /* Argument processing */
381 #define GETOPT_LIST_GLOBAL "d:DfGhv"
382 #define GETOPT_PROCESS_GLOBAL \
383 case 'd': \
384 ncopies = atoi(optarg); \
385 break; \
386 case 'D': \
387 dyesub_debug++; \
388 break; \
389 case 'f': \
390 fast_return++; \
391 break; \
392 case 'G': \
393 print_license_blurb(); \
394 exit(0); \
395 case 'h': \
396 print_help(argv[0], &BACKEND); \
397 exit(0); \
398 case 'v': \
399 quiet++; \
400 break;
401
402 /* Dynamic library loading */
403 #if defined(USE_DLOPEN)
404 #define WITH_DYNAMIC
405 #include <dlfcn.h>
406 #define DL_INIT() do {} while(0)
407 #define DL_OPEN(__x) dlopen(__x, RTLD_NOW)
408 #define DL_SYM(__x, __y) dlsym(__x, __y)
409 #define DL_CLOSE(__x) dlclose(__x)
410 #define DL_EXIT() do {} while(0)
411 #elif defined(USE_LTDL)
412 #define WITH_DYNAMIC
413 #include <ltdl.h>
414 #define DL_INIT() lt_dlinit()
415 #define DL_OPEN(__x) lt_dlopen(__x)
416 #define DL_SYM(__x, __y) lt_dlsym(__x, __y)
417 #define DL_CLOSE(__x) do {} while(0)
418 #define DL_EXIT() lt_dlexit()
419 #else
420 #define DL_INIT() do {} while(0)
421 #define DL_CLOSE(__x) do {} while(0)
422 #define DL_EXIT() do {} while(0)
423 #endif
424 #if defined(_WIN32)
425 #define DLL_SUFFIX ".dll"
426 #elif defined(__APPLE__)
427 #define DLL_SUFFIX ".dylib"
428 #else
429 #define DLL_SUFFIX ".so"
430 #endif
431
432 #endif /* __BACKEND_COMMON_H */