1 /* $Id: parport_probe.c,v 1.1 1999/07/03 08:56:17 davem Exp $
2 * Parallel port device probing code
4 * Authors: Carsten Gross, carsten@sol.wohnheim.uni-ulm.de
5 * Philip Blundell <philb@gnu.org>
8 #include <linux/module.h>
9 #include <linux/parport.h>
10 #include <linux/ctype.h>
11 #include <linux/string.h>
12 #include <asm/uaccess.h>
18 { "", "Legacy device" },
19 { "PRINTER", "Printer" },
21 { "NET", "Network device" },
22 { "HDC", "Hard disk" },
23 { "PCMCIA", "PCMCIA" },
24 { "MEDIA", "Multimedia device" },
25 { "FDC", "Floppy disk" },
27 { "SCANNER", "Scanner" },
28 { "DIGICAM", "Digital camera" },
29 { "", "Unknown device" },
30 { "", "Unspecified" },
31 { "SCSIADAPTER", "SCSI adapter" },
35 static void pretty_print(struct parport
*port
, int device
)
37 struct parport_device_info
*info
= &port
->probe_info
[device
+ 1];
39 printk(KERN_INFO
"%s", port
->name
);
42 printk (" (addr %d)", device
);
44 printk (": %s", classes
[info
->class].descr
);
46 printk(", %s %s", info
->mfr
, info
->model
);
51 static char *strdup(char *str
)
53 int n
= strlen(str
)+1;
54 char *s
= kmalloc(n
, GFP_KERNEL
);
56 return strcpy(s
, str
);
59 static void parse_data(struct parport
*port
, int device
, char *str
)
61 char *txt
= kmalloc(strlen(str
)+1, GFP_KERNEL
);
63 int guessed_class
= PARPORT_CLASS_UNSPEC
;
64 struct parport_device_info
*info
= &port
->probe_info
[device
+ 1];
67 printk(KERN_WARNING
"%s probe: memory squeeze\n", port
->name
);
79 /* Get rid of trailing blanks */
80 u
= sep
+ strlen (sep
) - 1;
81 while (u
>= p
&& *u
== ' ')
88 if (!strcmp(p
, "MFG") || !strcmp(p
, "MANUFACTURER")) {
91 info
->mfr
= strdup(sep
);
92 } else if (!strcmp(p
, "MDL") || !strcmp(p
, "MODEL")) {
95 info
->model
= strdup(sep
);
96 } else if (!strcmp(p
, "CLS") || !strcmp(p
, "CLASS")) {
99 kfree (info
->class_name
);
100 info
->class_name
= strdup(sep
);
101 for (u
= sep
; *u
; u
++)
103 for (i
= 0; classes
[i
].token
; i
++) {
104 if (!strcmp(classes
[i
].token
, sep
)) {
109 printk(KERN_WARNING
"%s probe: warning, class '%s' not understood.\n", port
->name
, sep
);
110 info
->class = PARPORT_CLASS_OTHER
;
111 } else if (!strcmp(p
, "CMD") ||
112 !strcmp(p
, "COMMAND SET")) {
114 kfree (info
->cmdset
);
115 info
->cmdset
= strdup(sep
);
116 /* if it speaks printer language, it's
117 probably a printer */
118 if (strstr(sep
, "PJL") || strstr(sep
, "PCL"))
119 guessed_class
= PARPORT_CLASS_PRINTER
;
120 } else if (!strcmp(p
, "DES") || !strcmp(p
, "DESCRIPTION")) {
121 if (info
->description
)
122 kfree (info
->description
);
123 info
->description
= strdup(sep
);
127 if (q
) p
= q
+1; else p
=NULL
;
130 /* If the device didn't tell us its class, maybe we have managed to
131 guess one from the things it did say. */
132 if (info
->class == PARPORT_CLASS_UNSPEC
)
133 info
->class = guessed_class
;
135 pretty_print (port
, device
);
140 /* Get Std 1284 Device ID. */
141 ssize_t
parport_device_id (int devnum
, char *buffer
, size_t len
)
143 ssize_t retval
= -ENXIO
;
144 struct pardevice
*dev
= parport_open (devnum
, "Device ID probe",
145 NULL
, NULL
, NULL
, 0, NULL
);
149 parport_claim_or_block (dev
);
151 /* Negotiate to compatibility mode, and then to device ID mode.
152 * (This is in case we are already in device ID mode.) */
153 parport_negotiate (dev
->port
, IEEE1284_MODE_COMPAT
);
154 retval
= parport_negotiate (dev
->port
,
155 IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
);
159 unsigned char length
[2];
161 /* First two bytes are MSB,LSB of inclusive length. */
162 retval
= parport_read (dev
->port
, length
, 2);
164 if (retval
!= 2) goto end_id
;
166 idlen
= (length
[0] << 8) + length
[1] - 2;
168 * Check if the caller-allocated buffer is large enough
169 * otherwise bail out or there will be an at least off by one.
177 retval
= parport_read (dev
->port
, buffer
, len
);
180 printk (KERN_DEBUG
"%s: only read %Zd of %Zd ID bytes\n",
181 dev
->port
->name
, retval
,
184 /* Some printer manufacturers mistakenly believe that
185 the length field is supposed to be _exclusive_.
186 In addition, there are broken devices out there
187 that don't even finish off with a semi-colon. */
188 if (buffer
[len
- 1] != ';') {
190 diff
= parport_read (dev
->port
, buffer
+ len
, 2);
195 "%s: device reported incorrect "
196 "length field (%d, should be %Zd)\n",
197 dev
->port
->name
, idlen
, retval
);
199 /* One semi-colon short of a device ID. */
201 printk (KERN_DEBUG
"%s: faking semi-colon\n",
204 /* If we get here, I don't think we
205 need to worry about the possible
206 standard violation of having read
207 more than we were told to. The
208 device is non-compliant anyhow. */
214 parport_negotiate (dev
->port
, IEEE1284_MODE_COMPAT
);
218 parse_data (dev
->port
, dev
->daisy
, buffer
);
221 parport_release (dev
);