File: linux.c

package info (click to toggle)
libinklevel 0.6.6~rc5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 208 kB
  • ctags: 197
  • sloc: ansic: 2,709; makefile: 73; sh: 34
file content (159 lines) | stat: -rw-r--r-- 3,728 bytes parent folder | download
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
/* linux.c
 *
 * (c) 2003, 2004, 2005, 2006 Markus Heinz
 *
 * This software is licensed under the terms of the GPL.
 * For details see file COPYING.
 */

#include "inklevel.h"

#include <ieee1284.h>

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>

#define IOCNR_GET_DEVICE_ID 1
#define LPIOC_GET_DEVICE_ID _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, BUFLEN)

int get_device_id(int port, int portnumber, char *device_id);
int open_printer_device(int port, int portnumber);

/* This function retrieves the device id of the specified port */

int get_device_id(int port, int portnumber, char *device_id) {
  struct parport_list parports;
  char tmp[BUFLEN];
  char device_file[256];
  char device_file2[256];
  int size;
  int fd;
  char *c;
  int realsize;

  if (port == PARPORT ) {
    /* check if we have appropiate permissions */

    sprintf(device_file, "/dev/parport%d", portnumber);
    if ((fd = open(device_file, O_RDWR)) < 0) {
      return DEV_PARPORT_INACCESSIBLE;
    }
    close(fd);

    sprintf(device_file, "/dev/lp%d", portnumber);
    if ((fd = open(device_file, O_RDWR)) < 0) {
      return DEV_LP_INACCESSIBLE;
    }
    close(fd);

    if (ieee1284_find_ports(&parports, 0) == E1284_OK) {
      if (portnumber < parports.portc) {
	size = ieee1284_get_deviceid(parports.portv[portnumber], -1, 
                                     F1284_FRESH, tmp, BUFLEN);
	if (size >= 2) {
	  strncpy(device_id, tmp + 2, size - 2);
	  return OK;
	}
      }
    }
    return COULD_NOT_GET_DEVICE_ID;
  } else if (port == USB) {
    sprintf(device_file, "/dev/usb/lp%d", portnumber);
    sprintf(device_file2, "/dev/usblp%d", portnumber);
    fd = open(device_file, O_RDONLY);
    if (fd == -1) {
      fd = open(device_file2, O_RDONLY);
    }
    if (fd == -1) {
      return DEV_USB_LP_INACCESSIBLE;
    }
    if (ioctl(fd, LPIOC_GET_DEVICE_ID, tmp) < 0) {
      close(fd);
      return COULD_NOT_GET_DEVICE_ID;
    }
    close(fd);
    size = ((unsigned char) tmp[0] << 8) | ((unsigned char) tmp[1]);

    /* Some printers report the size of the device id incorrectly. */

    realsize = 2;
    c = &tmp[2];
    while (*c++ != '\0') {
      realsize++;
    }

#ifdef DEBUG
    printf("Printer reported size %d, real size is %d\n", size, realsize);
#endif

    size = (realsize < size) ? realsize : size;
    size = (size < BUFLEN - 1) ? size : BUFLEN - 1;
    tmp[size] = '\0';
    if (size >= 2) {
      strncpy(device_id, tmp + 2, size - 2);
      return OK;
    } else {
      return COULD_NOT_GET_DEVICE_ID;
    }
  } else {
    return UNKNOWN_PORT_SPECIFIED;
  }
}

int open_printer_device(int port, int portnumber) {
  char device_file[256];
  char device_file2[256];
  int fd;

  if (port == USB) {
    sprintf(device_file, "/dev/usb/lp%d", portnumber);
    sprintf(device_file2, "/dev/usblp%d", portnumber);
  } else if (port == PARPORT) {
    sprintf(device_file, "/dev/lp%d", portnumber);
  } else {
    return UNKNOWN_PORT_SPECIFIED;
  }

#ifdef DEBUG
  printf("Device file: %s\n", device_file);
#endif

  fd = open(device_file, O_RDWR);

  if (fd == -1 && port == USB) {

#ifdef DEBUG
    printf("Could not open %s\n", device_file);
    printf("Device file: %s\n", device_file2);
#endif

    fd = open(device_file2, O_RDWR);
    
    if (fd == -1) {
#ifdef DEBUG
      printf("Could not open %s\n", device_file2);
#endif
    }
  } else if (fd == -1 && port == PARPORT) {

#ifdef DEBUG
    printf("Could not open %s\n", device_file);
#endif

  }

  if (fd == -1) {
    if (port == USB) {
      return DEV_USB_LP_INACCESSIBLE;
    } else { 
      return DEV_LP_INACCESSIBLE;
    }
  } else {
    return fd;
  }
}