1 // SPDX-License-Identifier: GPL-2.0
3 * Parallel port device probing code
5 * Authors: Carsten Gross, carsten@sol.wohnheim.uni-ulm.de
6 * Philip Blundell <philb@gnu.org>
9 #include <linux/module.h>
10 #include <linux/parport.h>
11 #include <linux/ctype.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/uaccess.h>
20 { "", "Legacy device" },
21 { "PRINTER", "Printer" },
23 { "NET", "Network device" },
24 { "HDC", "Hard disk" },
25 { "PCMCIA", "PCMCIA" },
26 { "MEDIA", "Multimedia device" },
27 { "FDC", "Floppy disk" },
29 { "SCANNER", "Scanner" },
30 { "DIGICAM", "Digital camera" },
31 { "", "Unknown device" },
32 { "", "Unspecified" },
33 { "SCSIADAPTER", "SCSI adapter" },
37 static void pretty_print(struct parport
*port
, int device
)
39 struct parport_device_info
*info
= &port
->probe_info
[device
+ 1];
41 printk(KERN_INFO
"%s", port
->name
);
44 printk (" (addr %d)", device
);
46 printk (": %s", classes
[info
->class].descr
);
48 printk(", %s %s", info
->mfr
, info
->model
);
53 static void parse_data(struct parport
*port
, int device
, char *str
)
55 char *txt
= kmalloc(strlen(str
)+1, GFP_KERNEL
);
57 int guessed_class
= PARPORT_CLASS_UNSPEC
;
58 struct parport_device_info
*info
= &port
->probe_info
[device
+ 1];
61 printk(KERN_WARNING
"%s probe: memory squeeze\n", port
->name
);
73 /* Get rid of trailing blanks */
74 u
= sep
+ strlen (sep
) - 1;
75 while (u
>= p
&& *u
== ' ')
82 if (!strcmp(p
, "MFG") || !strcmp(p
, "MANUFACTURER")) {
84 info
->mfr
= kstrdup(sep
, GFP_KERNEL
);
85 } else if (!strcmp(p
, "MDL") || !strcmp(p
, "MODEL")) {
87 info
->model
= kstrdup(sep
, GFP_KERNEL
);
88 } else if (!strcmp(p
, "CLS") || !strcmp(p
, "CLASS")) {
91 kfree(info
->class_name
);
92 info
->class_name
= kstrdup(sep
, GFP_KERNEL
);
93 for (u
= sep
; *u
; u
++)
95 for (i
= 0; classes
[i
].token
; i
++) {
96 if (!strcmp(classes
[i
].token
, sep
)) {
101 printk(KERN_WARNING
"%s probe: warning, class '%s' not understood.\n", port
->name
, sep
);
102 info
->class = PARPORT_CLASS_OTHER
;
103 } else if (!strcmp(p
, "CMD") ||
104 !strcmp(p
, "COMMAND SET")) {
106 info
->cmdset
= kstrdup(sep
, GFP_KERNEL
);
107 /* if it speaks printer language, it's
108 probably a printer */
109 if (strstr(sep
, "PJL") || strstr(sep
, "PCL"))
110 guessed_class
= PARPORT_CLASS_PRINTER
;
111 } else if (!strcmp(p
, "DES") || !strcmp(p
, "DESCRIPTION")) {
112 kfree(info
->description
);
113 info
->description
= kstrdup(sep
, GFP_KERNEL
);
123 /* If the device didn't tell us its class, maybe we have managed to
124 guess one from the things it did say. */
125 if (info
->class == PARPORT_CLASS_UNSPEC
)
126 info
->class = guessed_class
;
128 pretty_print (port
, device
);
133 /* Read up to count-1 bytes of device id. Terminate buffer with
134 * '\0'. Buffer begins with two Device ID length bytes as given by
136 static ssize_t
parport_read_device_id (struct parport
*port
, char *buffer
,
139 unsigned char length
[2];
140 unsigned lelen
, belen
;
143 unsigned current_idlen
;
147 /* First two bytes are MSB,LSB of inclusive length. */
148 retval
= parport_read (port
, length
, 2);
157 memcpy(buffer
, length
, 2);
160 /* Some devices wrongly send LE length, and some send it two
161 * bytes short. Construct a sorted array of lengths to try. */
162 belen
= (length
[0] << 8) + length
[1];
163 lelen
= (length
[1] << 8) + length
[0];
164 idlens
[0] = min(belen
, lelen
);
165 idlens
[1] = idlens
[0]+2;
166 if (belen
!= lelen
) {
168 /* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
171 idlens
[off
] = max(belen
, lelen
);
172 idlens
[off
+1] = idlens
[off
]+2;
176 /* Some devices don't truly implement Device ID, but
177 * just return constant nibble forever. This catches
178 * also those cases. */
179 if (idlens
[0] == 0 || idlens
[0] > 0xFFF) {
180 printk (KERN_DEBUG
"%s: reported broken Device ID"
181 " length of %#zX bytes\n",
182 port
->name
, idlens
[0]);
188 /* Try to respect the given ID length despite all the bugs in
189 * the ID length. Read according to shortest possible ID
191 for (current_idlen
= 0; current_idlen
< numidlens
; ++current_idlen
) {
192 size_t idlen
= idlens
[current_idlen
];
193 if (idlen
+1 >= count
)
196 retval
= parport_read (port
, buffer
+len
, idlen
-len
);
202 if (port
->physport
->ieee1284
.phase
!= IEEE1284_PH_HBUSY_DAVAIL
) {
204 printk (KERN_DEBUG
"%s: Device ID was %zd bytes"
205 " while device told it would be %d"
207 port
->name
, len
, belen
);
212 /* This might end reading the Device ID too
213 * soon. Hopefully the needed fields were already in
214 * the first 256 bytes or so that we must have read so
216 if (buffer
[len
-1] == ';') {
217 printk (KERN_DEBUG
"%s: Device ID reading stopped"
218 " before device told data not available. "
219 "Current idlen %u of %u, len bytes %02X %02X\n",
220 port
->name
, current_idlen
, numidlens
,
221 length
[0], length
[1]);
225 if (current_idlen
< numidlens
) {
226 /* Buffer not large enough, read to end of buffer. */
229 retval
= parport_read (port
, buffer
+len
, count
-len
-1);
234 /* Read the whole ID since some devices would not
235 * otherwise give back the Device ID from beginning
236 * next time when asked. */
237 idlen
= idlens
[current_idlen
];
239 while(len2
< idlen
&& retval
> 0) {
241 retval
= parport_read (port
, tmp
,
242 min(sizeof tmp
, idlen
-len2
));
248 /* In addition, there are broken devices out there that don't
249 even finish off with a semi-colon. We do not need to care
250 about those at this time. */
256 /* Get Std 1284 Device ID. */
257 ssize_t
parport_device_id (int devnum
, char *buffer
, size_t count
)
259 ssize_t retval
= -ENXIO
;
260 struct pardevice
*dev
= parport_open (devnum
, "Device ID probe");
264 parport_claim_or_block (dev
);
266 /* Negotiate to compatibility mode, and then to device ID
267 * mode. (This so that we start form beginning of device ID if
268 * already in device ID mode.) */
269 parport_negotiate (dev
->port
, IEEE1284_MODE_COMPAT
);
270 retval
= parport_negotiate (dev
->port
,
271 IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
);
274 retval
= parport_read_device_id (dev
->port
, buffer
, count
);
275 parport_negotiate (dev
->port
, IEEE1284_MODE_COMPAT
);
277 parse_data (dev
->port
, dev
->daisy
, buffer
+2);
280 parport_release (dev
);