1 /* $Id: probe.c,v 1.1.1.1 2005/04/11 02:50:33 jack 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(KERN_WARNING "%s probe: memory squeeze\n", port->name);
77 /* Get rid of trailing blanks */
78 u = sep + strlen (sep) - 1;
79 while (u >= p && *u == ' ')
86 if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
89 info->mfr = strdup(sep);
90 } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
93 info->model = strdup(sep);
94 } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
97 kfree (info->class_name);
98 info->class_name = strdup(sep);
99 for (u = sep; *u; u++)
101 for (i = 0; classes[i].token; i++) {
102 if (!strcmp(classes[i].token, sep)) {
107 printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
108 info->class = PARPORT_CLASS_OTHER;
109 } else if (!strcmp(p, "CMD") ||
110 !strcmp(p, "COMMAND SET")) {
112 kfree (info->cmdset);
113 info->cmdset = strdup(sep);
114 /* if it speaks printer language, it's
115 probably a printer */
116 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
117 guessed_class = PARPORT_CLASS_PRINTER;
118 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
119 if (info->description)
120 kfree (info->description);
121 info->description = strdup(sep);
125 if (q) p = q+1; else p=NULL;
128 /* If the device didn't tell us its class, maybe we have managed to
129 guess one from the things it did say. */
130 if (info->class == PARPORT_CLASS_UNSPEC)
131 info->class = guessed_class;
133 pretty_print (port, device);
138 /* Get Std 1284 Device ID. */
139 ssize_t parport_device_id (int devnum, char *buffer, size_t len)
141 ssize_t retval = -ENXIO;
142 struct pardevice *dev = parport_open (devnum, "Device ID probe",
143 NULL, NULL, NULL, 0, NULL);
147 parport_claim_or_block (dev);
149 /* Negotiate to compatibility mode, and then to device ID mode.
150 * (This is in case we are already in device ID mode.) */
151 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
152 retval = parport_negotiate (dev->port,
153 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
157 unsigned char length[2];
159 /* First two bytes are MSB,LSB of inclusive length. */
160 retval = parport_read (dev->port, length, 2);
162 if (retval != 2) goto end_id;
164 idlen = (length[0] << 8) + length[1] - 2;
167 retval = parport_read (dev->port, buffer, len);
170 printk (KERN_DEBUG "%s: only read %Zd of %Zd ID bytes\n",
171 dev->port->name, retval,
174 /* Some printer manufacturers mistakenly believe that
175 the length field is supposed to be _exclusive_.
176 In addition, there are broken devices out there
177 that don't even finish off with a semi-colon. */
178 if (buffer[len - 1] != ';') {
180 diff = parport_read (dev->port, buffer + len, 2);
185 "%s: device reported incorrect "
186 "length field (%d, should be %Zd)\n",
187 dev->port->name, idlen, retval);
189 /* One semi-colon short of a device ID. */
191 printk (KERN_DEBUG "%s: faking semi-colon\n",
194 /* If we get here, I don't think we
195 need to worry about the possible
196 standard violation of having read
197 more than we were told to. The
198 device is non-compliant anyhow. */
204 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
206 parport_release (dev);
209 parse_data (dev->port, dev->daisy, buffer);