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 <linux/slab.h>
13 #include <asm/uaccess.h>
19 { "", "Legacy device" },
20 { "PRINTER", "Printer" },
22 { "NET", "Network device" },
23 { "HDC", "Hard disk" },
24 { "PCMCIA", "PCMCIA" },
25 { "MEDIA", "Multimedia device" },
26 { "FDC", "Floppy disk" },
28 { "SCANNER", "Scanner" },
29 { "DIGICAM", "Digital camera" },
30 { "", "Unknown device" },
31 { "", "Unspecified" },
32 { "SCSIADAPTER", "SCSI adapter" },
36 static void pretty_print(struct parport *port, int device)
38 struct parport_device_info *info = &port->probe_info[device + 1];
40 printk(KERN_INFO "%s", port->name);
43 printk (" (addr %d)", device);
45 printk (": %s", classes[info->class].descr);
47 printk(", %s %s", info->mfr, info->model);
52 static void parse_data(struct parport *port, int device, char *str)
54 char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
56 int guessed_class = PARPORT_CLASS_UNSPEC;
57 struct parport_device_info *info = &port->probe_info[device + 1];
60 printk(KERN_WARNING "%s probe: memory squeeze\n", port->name);
72 /* Get rid of trailing blanks */
73 u = sep + strlen (sep) - 1;
74 while (u >= p && *u == ' ')
81 if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
83 info->mfr = kstrdup(sep, GFP_KERNEL);
84 } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
86 info->model = kstrdup(sep, GFP_KERNEL);
87 } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
90 kfree(info->class_name);
91 info->class_name = kstrdup(sep, GFP_KERNEL);
92 for (u = sep; *u; u++)
94 for (i = 0; classes[i].token; i++) {
95 if (!strcmp(classes[i].token, sep)) {
100 printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
101 info->class = PARPORT_CLASS_OTHER;
102 } else if (!strcmp(p, "CMD") ||
103 !strcmp(p, "COMMAND SET")) {
105 info->cmdset = kstrdup(sep, GFP_KERNEL);
106 /* if it speaks printer language, it's
107 probably a printer */
108 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
109 guessed_class = PARPORT_CLASS_PRINTER;
110 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
111 kfree(info->description);
112 info->description = kstrdup(sep, GFP_KERNEL);
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 /* Read up to count-1 bytes of device id. Terminate buffer with
133 * '\0'. Buffer begins with two Device ID length bytes as given by
135 static ssize_t parport_read_device_id (struct parport *port, char *buffer,
138 unsigned char length[2];
139 unsigned lelen, belen;
142 unsigned current_idlen;
146 /* First two bytes are MSB,LSB of inclusive length. */
147 retval = parport_read (port, length, 2);
156 memcpy(buffer, length, 2);
159 /* Some devices wrongly send LE length, and some send it two
160 * bytes short. Construct a sorted array of lengths to try. */
161 belen = (length[0] << 8) + length[1];
162 lelen = (length[1] << 8) + length[0];
163 idlens[0] = min(belen, lelen);
164 idlens[1] = idlens[0]+2;
165 if (belen != lelen) {
167 /* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
170 idlens[off] = max(belen, lelen);
171 idlens[off+1] = idlens[off]+2;
175 /* Some devices don't truly implement Device ID, but
176 * just return constant nibble forever. This catches
177 * also those cases. */
178 if (idlens[0] == 0 || idlens[0] > 0xFFF) {
179 printk (KERN_DEBUG "%s: reported broken Device ID"
180 " length of %#zX bytes\n",
181 port->name, idlens[0]);
187 /* Try to respect the given ID length despite all the bugs in
188 * the ID length. Read according to shortest possible ID
190 for (current_idlen = 0; current_idlen < numidlens; ++current_idlen) {
191 size_t idlen = idlens[current_idlen];
192 if (idlen+1 >= count)
195 retval = parport_read (port, buffer+len, idlen-len);
201 if (port->physport->ieee1284.phase != IEEE1284_PH_HBUSY_DAVAIL) {
203 printk (KERN_DEBUG "%s: Device ID was %zd bytes"
204 " while device told it would be %d"
206 port->name, len, belen);
211 /* This might end reading the Device ID too
212 * soon. Hopefully the needed fields were already in
213 * the first 256 bytes or so that we must have read so
215 if (buffer[len-1] == ';') {
216 printk (KERN_DEBUG "%s: Device ID reading stopped"
217 " before device told data not available. "
218 "Current idlen %u of %u, len bytes %02X %02X\n",
219 port->name, current_idlen, numidlens,
220 length[0], length[1]);
224 if (current_idlen < numidlens) {
225 /* Buffer not large enough, read to end of buffer. */
228 retval = parport_read (port, buffer+len, count-len-1);
233 /* Read the whole ID since some devices would not
234 * otherwise give back the Device ID from beginning
235 * next time when asked. */
236 idlen = idlens[current_idlen];
238 while(len2 < idlen && retval > 0) {
240 retval = parport_read (port, tmp,
241 min(sizeof tmp, idlen-len2));
247 /* In addition, there are broken devices out there that don't
248 even finish off with a semi-colon. We do not need to care
249 about those at this time. */
255 /* Get Std 1284 Device ID. */
256 ssize_t parport_device_id (int devnum, char *buffer, size_t count)
258 ssize_t retval = -ENXIO;
259 struct pardevice *dev = parport_open (devnum, "Device ID probe");
263 parport_claim_or_block (dev);
265 /* Negotiate to compatibility mode, and then to device ID
266 * mode. (This so that we start form beginning of device ID if
267 * already in device ID mode.) */
268 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
269 retval = parport_negotiate (dev->port,
270 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
273 retval = parport_read_device_id (dev->port, buffer, count);
274 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
276 parse_data (dev->port, dev->daisy, buffer+2);
279 parport_release (dev);