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")) {
83 info->mfr = kstrdup(sep, GFP_KERNEL);
84 } 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 kfree (info->cmdset);
107 info->cmdset = kstrdup(sep, GFP_KERNEL);
108 /* if it speaks printer language, it's
109 probably a printer */
110 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
111 guessed_class = PARPORT_CLASS_PRINTER;
112 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
113 if (info->description)
114 kfree (info->description);
115 info->description = kstrdup(sep, GFP_KERNEL);
119 if (q) p = q+1; else p=NULL;
122 /* If the device didn't tell us its class, maybe we have managed to
123 guess one from the things it did say. */
124 if (info->class == PARPORT_CLASS_UNSPEC)
125 info->class = guessed_class;
127 pretty_print (port, device);
132 /* Get Std 1284 Device ID. */
133 ssize_t parport_device_id (int devnum, char *buffer, size_t len)
135 ssize_t retval = -ENXIO;
136 struct pardevice *dev = parport_open (devnum, "Device ID probe",
137 NULL, NULL, NULL, 0, NULL);
141 parport_claim_or_block (dev);
143 /* Negotiate to compatibility mode, and then to device ID mode.
144 * (This is in case we are already in device ID mode.) */
145 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
146 retval = parport_negotiate (dev->port,
147 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
151 unsigned char length[2];
153 /* First two bytes are MSB,LSB of inclusive length. */
154 retval = parport_read (dev->port, length, 2);
156 if (retval != 2) goto end_id;
158 idlen = (length[0] << 8) + length[1] - 2;
160 * Check if the caller-allocated buffer is large enough
161 * otherwise bail out or there will be an at least off by one.
169 retval = parport_read (dev->port, buffer, len);
172 printk (KERN_DEBUG "%s: only read %Zd of %Zd ID bytes\n",
173 dev->port->name, retval,
176 /* Some printer manufacturers mistakenly believe that
177 the length field is supposed to be _exclusive_.
178 In addition, there are broken devices out there
179 that don't even finish off with a semi-colon. */
180 if (buffer[len - 1] != ';') {
182 diff = parport_read (dev->port, buffer + len, 2);
187 "%s: device reported incorrect "
188 "length field (%d, should be %Zd)\n",
189 dev->port->name, idlen, retval);
191 /* One semi-colon short of a device ID. */
193 printk (KERN_DEBUG "%s: faking semi-colon\n",
196 /* If we get here, I don't think we
197 need to worry about the possible
198 standard violation of having read
199 more than we were told to. The
200 device is non-compliant anyhow. */
206 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
210 parse_data (dev->port, dev->daisy, buffer);
213 parport_release (dev);