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 <Philip.Blundell@pobox.com>
8 #include <linux/parport.h>
9 #include <linux/ctype.h>
10 #include <asm/uaccess.h>
16 { "", "Legacy device" },
17 { "PRINTER", "Printer" },
19 { "NET", "Network device" },
20 { "HDC", "Hard disk" },
21 { "PCMCIA", "PCMCIA" },
22 { "MEDIA", "Multimedia device" },
23 { "FDC", "Floppy disk" },
25 { "SCANNER", "Scanner" },
26 { "DIGICAM", "Digital camera" },
27 { "", "Unknown device" },
28 { "", "Unspecified" },
29 { "SCSIADAPTER", "SCSI adapter" },
33 static void pretty_print(struct parport
*port
, int device
)
35 struct parport_device_info
*info
= &port
->probe_info
[device
+ 1];
37 printk(KERN_INFO
"%s", port
->name
);
40 printk (" (addr %d)", device
);
42 printk (": %s", classes
[info
->class].descr
);
44 printk(", %s %s", info
->mfr
, info
->model
);
49 static char *strdup(char *str
)
51 int n
= strlen(str
)+1;
52 char *s
= kmalloc(n
, GFP_KERNEL
);
54 return strcpy(s
, str
);
57 static void parse_data(struct parport
*port
, int device
, char *str
)
59 char *txt
= kmalloc(strlen(str
)+1, GFP_KERNEL
);
61 int guessed_class
= PARPORT_CLASS_UNSPEC
;
62 struct parport_device_info
*info
= &port
->probe_info
[device
+ 1];
65 printk("%s probe: memory squeeze\n", port
->name
);
81 if (!strcmp(p
, "MFG") || !strcmp(p
, "MANUFACTURER")) {
84 info
->mfr
= strdup(sep
);
85 } else if (!strcmp(p
, "MDL") || !strcmp(p
, "MODEL")) {
88 info
->model
= strdup(sep
);
89 } else if (!strcmp(p
, "CLS") || !strcmp(p
, "CLASS")) {
92 kfree (info
->class_name
);
93 info
->class_name
= strdup(sep
);
94 for (u
= sep
; *u
; u
++)
96 for (i
= 0; classes
[i
].token
; i
++) {
97 if (!strcmp(classes
[i
].token
, sep
)) {
102 printk(KERN_WARNING
"%s probe: warning, class '%s' not understood.\n", port
->name
, sep
);
103 info
->class = PARPORT_CLASS_OTHER
;
104 } else if (!strcmp(p
, "CMD") ||
105 !strcmp(p
, "COMMAND SET")) {
107 kfree (info
->cmdset
);
108 info
->cmdset
= strdup(sep
);
109 /* if it speaks printer language, it's
110 probably a printer */
111 if (strstr(sep
, "PJL") || strstr(sep
, "PCL"))
112 guessed_class
= PARPORT_CLASS_PRINTER
;
113 } else if (!strcmp(p
, "DES") || !strcmp(p
, "DESCRIPTION")) {
114 if (info
->description
)
115 kfree (info
->description
);
116 info
->description
= strdup(sep
);
120 if (q
) p
= q
+1; else p
=NULL
;
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 /* Get Std 1284 Device ID. */
134 ssize_t
parport_device_id (int devnum
, char *buffer
, size_t len
)
136 ssize_t retval
= -ENXIO
;
137 struct pardevice
*dev
= parport_open (devnum
, "Device ID probe",
138 NULL
, NULL
, NULL
, 0, NULL
);
142 parport_claim_or_block (dev
);
144 /* Negotiate to compatibility mode, and then to device ID mode.
145 * (This is in case we are already in device ID mode.) */
146 parport_negotiate (dev
->port
, IEEE1284_MODE_COMPAT
);
147 retval
= parport_negotiate (dev
->port
,
148 IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
);
152 unsigned char length
[2];
154 /* First two bytes are MSB,LSB of inclusive length. */
155 retval
= parport_read (dev
->port
, length
, 2);
157 if (retval
!= 2) goto end_id
;
159 idlen
= (length
[0] << 8) + length
[1] - 2;
162 retval
= parport_read (dev
->port
, buffer
, len
);
165 printk (KERN_DEBUG
"%s: only read %Zd of %Zd ID bytes\n",
166 dev
->port
->name
, retval
,
169 /* Some printer manufacturers mistakenly believe that
170 the length field is supposed to be _exclusive_.
171 In addition, there are broken devices out there
172 that don't even finish off with a semi-colon. */
173 if (buffer
[len
- 1] != ';') {
175 diff
= parport_read (dev
->port
, buffer
+ len
, 2);
180 "%s: device reported incorrect "
181 "length field (%d, should be %Zd)\n",
182 dev
->port
->name
, idlen
, retval
);
184 /* One semi-colon short of a device ID. */
186 printk (KERN_DEBUG
"%s: faking semi-colon\n",
189 /* If we get here, I don't think we
190 need to worry about the possible
191 standard violation of having read
192 more than we were told to. The
193 device is non-compliant anyhow. */
199 parport_negotiate (dev
->port
, IEEE1284_MODE_COMPAT
);
201 parport_release (dev
);
204 parse_data (dev
->port
, dev
->daisy
, buffer
);