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 void parse_data(struct parport *port, int device, char *str)
53 char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
55 int guessed_class = PARPORT_CLASS_UNSPEC;
56 struct parport_device_info *info = &port->probe_info[device + 1];
59 printk(KERN_WARNING "%s probe: memory squeeze\n", port->name);
71 /* Get rid of trailing blanks */
72 u = sep + strlen (sep) - 1;
73 while (u >= p && *u == ' ')
80 if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
82 info->mfr = kstrdup(sep, GFP_KERNEL);
83 } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
85 info->model = kstrdup(sep, GFP_KERNEL);
86 } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
89 kfree(info->class_name);
90 info->class_name = kstrdup(sep, GFP_KERNEL);
91 for (u = sep; *u; u++)
93 for (i = 0; classes[i].token; i++) {
94 if (!strcmp(classes[i].token, sep)) {
99 printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
100 info->class = PARPORT_CLASS_OTHER;
101 } else if (!strcmp(p, "CMD") ||
102 !strcmp(p, "COMMAND SET")) {
104 info->cmdset = kstrdup(sep, GFP_KERNEL);
105 /* if it speaks printer language, it's
106 probably a printer */
107 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
108 guessed_class = PARPORT_CLASS_PRINTER;
109 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
110 kfree(info->description);
111 info->description = kstrdup(sep, GFP_KERNEL);
121 /* If the device didn't tell us its class, maybe we have managed to
122 guess one from the things it did say. */
123 if (info->class == PARPORT_CLASS_UNSPEC)
124 info->class = guessed_class;
126 pretty_print (port, device);
131 /* Get Std 1284 Device ID. */
132 ssize_t parport_device_id (int devnum, char *buffer, size_t len)
134 ssize_t retval = -ENXIO;
135 struct pardevice *dev = parport_open (devnum, "Device ID probe",
136 NULL, NULL, NULL, 0, NULL);
140 parport_claim_or_block (dev);
142 /* Negotiate to compatibility mode, and then to device ID mode.
143 * (This is in case we are already in device ID mode.) */
144 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
145 retval = parport_negotiate (dev->port,
146 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
150 unsigned char length[2];
152 /* First two bytes are MSB,LSB of inclusive length. */
153 retval = parport_read (dev->port, length, 2);
155 if (retval != 2) goto end_id;
157 idlen = (length[0] << 8) + length[1] - 2;
159 * Check if the caller-allocated buffer is large enough
160 * otherwise bail out or there will be an at least off by one.
168 retval = parport_read (dev->port, buffer, len);
171 printk (KERN_DEBUG "%s: only read %Zd of %Zd ID bytes\n",
172 dev->port->name, retval,
175 /* Some printer manufacturers mistakenly believe that
176 the length field is supposed to be _exclusive_.
177 In addition, there are broken devices out there
178 that don't even finish off with a semi-colon. */
179 if (buffer[len - 1] != ';') {
181 diff = parport_read (dev->port, buffer + len, 2);
186 "%s: device reported incorrect "
187 "length field (%d, should be %Zd)\n",
188 dev->port->name, idlen, retval);
190 /* One semi-colon short of a device ID. */
192 printk (KERN_DEBUG "%s: faking semi-colon\n",
195 /* If we get here, I don't think we
196 need to worry about the possible
197 standard violation of having read
198 more than we were told to. The
199 device is non-compliant anyhow. */
205 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
209 parse_data (dev->port, dev->daisy, buffer);
212 parport_release (dev);