xref: /openbmc/linux/drivers/parport/probe.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2612de10dSAdrian Bunk /*
31da177e4SLinus Torvalds  * Parallel port device probing code
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:    Carsten Gross, carsten@sol.wohnheim.uni-ulm.de
61da177e4SLinus Torvalds  *             Philip Blundell <philb@gnu.org>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/parport.h>
111da177e4SLinus Torvalds #include <linux/string.h>
12*8c55a99dSAndy Shevchenko #include <linux/string_helpers.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
147c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
151da177e4SLinus Torvalds 
16a6767b7cSMarko Kohtala static const struct {
17a6767b7cSMarko Kohtala 	const char *token;
18a6767b7cSMarko Kohtala 	const char *descr;
191da177e4SLinus Torvalds } classes[] = {
201da177e4SLinus Torvalds 	{ "",            "Legacy device" },
211da177e4SLinus Torvalds 	{ "PRINTER",     "Printer" },
221da177e4SLinus Torvalds 	{ "MODEM",       "Modem" },
231da177e4SLinus Torvalds 	{ "NET",         "Network device" },
241da177e4SLinus Torvalds 	{ "HDC",       	 "Hard disk" },
251da177e4SLinus Torvalds 	{ "PCMCIA",      "PCMCIA" },
261da177e4SLinus Torvalds 	{ "MEDIA",       "Multimedia device" },
271da177e4SLinus Torvalds 	{ "FDC",         "Floppy disk" },
281da177e4SLinus Torvalds 	{ "PORTS",       "Ports" },
291da177e4SLinus Torvalds 	{ "SCANNER",     "Scanner" },
301da177e4SLinus Torvalds 	{ "DIGICAM",     "Digital camera" },
311da177e4SLinus Torvalds 	{ "",            "Unknown device" },
321da177e4SLinus Torvalds 	{ "",            "Unspecified" },
331da177e4SLinus Torvalds 	{ "SCSIADAPTER", "SCSI adapter" },
341da177e4SLinus Torvalds 	{ NULL,          NULL }
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
pretty_print(struct parport * port,int device)371da177e4SLinus Torvalds static void pretty_print(struct parport *port, int device)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds 	struct parport_device_info *info = &port->probe_info[device + 1];
401da177e4SLinus Torvalds 
41decf26f6SJoe Perches 	pr_info("%s", port->name);
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	if (device >= 0)
44aa3d6e7cSJoe Perches 		pr_cont(" (addr %d)", device);
451da177e4SLinus Torvalds 
46aa3d6e7cSJoe Perches 	pr_cont(": %s", classes[info->class].descr);
471da177e4SLinus Torvalds 	if (info->class)
48aa3d6e7cSJoe Perches 		pr_cont(", %s %s", info->mfr, info->model);
491da177e4SLinus Torvalds 
50aa3d6e7cSJoe Perches 	pr_cont("\n");
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
parse_data(struct parport * port,int device,char * str)531da177e4SLinus Torvalds static void parse_data(struct parport *port, int device, char *str)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
561da177e4SLinus Torvalds 	char *p = txt, *q;
571da177e4SLinus Torvalds 	int guessed_class = PARPORT_CLASS_UNSPEC;
581da177e4SLinus Torvalds 	struct parport_device_info *info = &port->probe_info[device + 1];
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	if (!txt) {
61decf26f6SJoe Perches 		pr_warn("%s probe: memory squeeze\n", port->name);
621da177e4SLinus Torvalds 		return;
631da177e4SLinus Torvalds 	}
641da177e4SLinus Torvalds 	strcpy(txt, str);
651da177e4SLinus Torvalds 	while (p) {
661da177e4SLinus Torvalds 		char *sep;
671da177e4SLinus Torvalds 		q = strchr(p, ';');
681da177e4SLinus Torvalds 		if (q) *q = 0;
691da177e4SLinus Torvalds 		sep = strchr(p, ':');
701da177e4SLinus Torvalds 		if (sep) {
711da177e4SLinus Torvalds 			char *u;
721da177e4SLinus Torvalds 			*(sep++) = 0;
731da177e4SLinus Torvalds 			/* Get rid of trailing blanks */
741da177e4SLinus Torvalds 			u = sep + strlen (sep) - 1;
751da177e4SLinus Torvalds 			while (u >= p && *u == ' ')
761da177e4SLinus Torvalds 				*u-- = '\0';
77*8c55a99dSAndy Shevchenko 			string_upper(p, p);
781da177e4SLinus Torvalds 			if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
791da177e4SLinus Torvalds 				kfree(info->mfr);
80543537bdSPaulo Marques 				info->mfr = kstrdup(sep, GFP_KERNEL);
811da177e4SLinus Torvalds 			} else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
821da177e4SLinus Torvalds 				kfree(info->model);
83543537bdSPaulo Marques 				info->model = kstrdup(sep, GFP_KERNEL);
841da177e4SLinus Torvalds 			} else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
851da177e4SLinus Torvalds 				int i;
866044ec88SJesper Juhl 
871da177e4SLinus Torvalds 				kfree(info->class_name);
88543537bdSPaulo Marques 				info->class_name = kstrdup(sep, GFP_KERNEL);
89*8c55a99dSAndy Shevchenko 				string_upper(sep, sep);
901da177e4SLinus Torvalds 				for (i = 0; classes[i].token; i++) {
911da177e4SLinus Torvalds 					if (!strcmp(classes[i].token, sep)) {
921da177e4SLinus Torvalds 						info->class = i;
931da177e4SLinus Torvalds 						goto rock_on;
941da177e4SLinus Torvalds 					}
951da177e4SLinus Torvalds 				}
96decf26f6SJoe Perches 				pr_warn("%s probe: warning, class '%s' not understood\n",
97decf26f6SJoe Perches 					port->name, sep);
981da177e4SLinus Torvalds 				info->class = PARPORT_CLASS_OTHER;
991da177e4SLinus Torvalds 			} else if (!strcmp(p, "CMD") ||
1001da177e4SLinus Torvalds 				   !strcmp(p, "COMMAND SET")) {
1011da177e4SLinus Torvalds 				kfree(info->cmdset);
102543537bdSPaulo Marques 				info->cmdset = kstrdup(sep, GFP_KERNEL);
1031da177e4SLinus Torvalds 				/* if it speaks printer language, it's
1041da177e4SLinus Torvalds 				   probably a printer */
1051da177e4SLinus Torvalds 				if (strstr(sep, "PJL") || strstr(sep, "PCL"))
1061da177e4SLinus Torvalds 					guessed_class = PARPORT_CLASS_PRINTER;
1071da177e4SLinus Torvalds 			} else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
1081da177e4SLinus Torvalds 				kfree(info->description);
109543537bdSPaulo Marques 				info->description = kstrdup(sep, GFP_KERNEL);
1101da177e4SLinus Torvalds 			}
1111da177e4SLinus Torvalds 		}
1121da177e4SLinus Torvalds 	rock_on:
1136044ec88SJesper Juhl 		if (q)
1146044ec88SJesper Juhl 			p = q + 1;
1156044ec88SJesper Juhl 		else
1166044ec88SJesper Juhl 			p = NULL;
1171da177e4SLinus Torvalds 	}
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	/* If the device didn't tell us its class, maybe we have managed to
1201da177e4SLinus Torvalds 	   guess one from the things it did say. */
1211da177e4SLinus Torvalds 	if (info->class == PARPORT_CLASS_UNSPEC)
1221da177e4SLinus Torvalds 		info->class = guessed_class;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	pretty_print (port, device);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	kfree(txt);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129c6606290SMarko Kohtala /* Read up to count-1 bytes of device id. Terminate buffer with
130c6606290SMarko Kohtala  * '\0'. Buffer begins with two Device ID length bytes as given by
131c6606290SMarko Kohtala  * device. */
parport_read_device_id(struct parport * port,char * buffer,size_t count)132c6606290SMarko Kohtala static ssize_t parport_read_device_id (struct parport *port, char *buffer,
133c6606290SMarko Kohtala 				       size_t count)
134c6606290SMarko Kohtala {
135c6606290SMarko Kohtala 	unsigned char length[2];
136c6606290SMarko Kohtala 	unsigned lelen, belen;
137c6606290SMarko Kohtala 	size_t idlens[4];
138c6606290SMarko Kohtala 	unsigned numidlens;
139c6606290SMarko Kohtala 	unsigned current_idlen;
140c6606290SMarko Kohtala 	ssize_t retval;
141c6606290SMarko Kohtala 	size_t len;
142c6606290SMarko Kohtala 
143c6606290SMarko Kohtala 	/* First two bytes are MSB,LSB of inclusive length. */
144c6606290SMarko Kohtala 	retval = parport_read (port, length, 2);
145c6606290SMarko Kohtala 
146c6606290SMarko Kohtala 	if (retval < 0)
147c6606290SMarko Kohtala 		return retval;
148c6606290SMarko Kohtala 	if (retval != 2)
149c6606290SMarko Kohtala 		return -EIO;
150c6606290SMarko Kohtala 
151c6606290SMarko Kohtala 	if (count < 2)
152c6606290SMarko Kohtala 		return 0;
153c6606290SMarko Kohtala 	memcpy(buffer, length, 2);
154c6606290SMarko Kohtala 	len = 2;
155c6606290SMarko Kohtala 
156c6606290SMarko Kohtala 	/* Some devices wrongly send LE length, and some send it two
157c6606290SMarko Kohtala 	 * bytes short. Construct a sorted array of lengths to try. */
158c6606290SMarko Kohtala 	belen = (length[0] << 8) + length[1];
159c6606290SMarko Kohtala 	lelen = (length[1] << 8) + length[0];
160c6606290SMarko Kohtala 	idlens[0] = min(belen, lelen);
161c6606290SMarko Kohtala 	idlens[1] = idlens[0]+2;
162c6606290SMarko Kohtala 	if (belen != lelen) {
163c6606290SMarko Kohtala 		int off = 2;
164efad798bSPaulius Zaleckas 		/* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
165c6606290SMarko Kohtala 		if (idlens[0] <= 2)
166c6606290SMarko Kohtala 			off = 0;
167c6606290SMarko Kohtala 		idlens[off] = max(belen, lelen);
168c6606290SMarko Kohtala 		idlens[off+1] = idlens[off]+2;
169c6606290SMarko Kohtala 		numidlens = off+2;
170c6606290SMarko Kohtala 	}
171c6606290SMarko Kohtala 	else {
172c6606290SMarko Kohtala 		/* Some devices don't truly implement Device ID, but
173c6606290SMarko Kohtala 		 * just return constant nibble forever. This catches
174c6606290SMarko Kohtala 		 * also those cases. */
175c6606290SMarko Kohtala 		if (idlens[0] == 0 || idlens[0] > 0xFFF) {
176aa3d6e7cSJoe Perches 			printk(KERN_DEBUG "%s: reported broken Device ID length of %#zX bytes\n",
177c6606290SMarko Kohtala 			       port->name, idlens[0]);
178c6606290SMarko Kohtala 			return -EIO;
179c6606290SMarko Kohtala 		}
180c6606290SMarko Kohtala 		numidlens = 2;
181c6606290SMarko Kohtala 	}
182c6606290SMarko Kohtala 
183c6606290SMarko Kohtala 	/* Try to respect the given ID length despite all the bugs in
184c6606290SMarko Kohtala 	 * the ID length. Read according to shortest possible ID
185c6606290SMarko Kohtala 	 * first. */
186c6606290SMarko Kohtala 	for (current_idlen = 0; current_idlen < numidlens; ++current_idlen) {
187c6606290SMarko Kohtala 		size_t idlen = idlens[current_idlen];
188c6606290SMarko Kohtala 		if (idlen+1 >= count)
189c6606290SMarko Kohtala 			break;
190c6606290SMarko Kohtala 
191c6606290SMarko Kohtala 		retval = parport_read (port, buffer+len, idlen-len);
192c6606290SMarko Kohtala 
193c6606290SMarko Kohtala 		if (retval < 0)
194c6606290SMarko Kohtala 			return retval;
195c6606290SMarko Kohtala 		len += retval;
196c6606290SMarko Kohtala 
197c6606290SMarko Kohtala 		if (port->physport->ieee1284.phase != IEEE1284_PH_HBUSY_DAVAIL) {
198c6606290SMarko Kohtala 			if (belen != len) {
199aa3d6e7cSJoe Perches 				printk(KERN_DEBUG "%s: Device ID was %zd bytes while device told it would be %d bytes\n",
200c6606290SMarko Kohtala 				       port->name, len, belen);
201c6606290SMarko Kohtala 			}
202c6606290SMarko Kohtala 			goto done;
203c6606290SMarko Kohtala 		}
204c6606290SMarko Kohtala 
205c6606290SMarko Kohtala 		/* This might end reading the Device ID too
206c6606290SMarko Kohtala 		 * soon. Hopefully the needed fields were already in
207c6606290SMarko Kohtala 		 * the first 256 bytes or so that we must have read so
208c6606290SMarko Kohtala 		 * far. */
209c6606290SMarko Kohtala 		if (buffer[len-1] == ';') {
210aa3d6e7cSJoe Perches 			printk(KERN_DEBUG "%s: Device ID reading stopped before device told data not available. Current idlen %u of %u, len bytes %02X %02X\n",
211c6606290SMarko Kohtala 			       port->name, current_idlen, numidlens,
212c6606290SMarko Kohtala 			       length[0], length[1]);
213c6606290SMarko Kohtala 			goto done;
214c6606290SMarko Kohtala 		}
215c6606290SMarko Kohtala 	}
216c6606290SMarko Kohtala 	if (current_idlen < numidlens) {
217c6606290SMarko Kohtala 		/* Buffer not large enough, read to end of buffer. */
218c6606290SMarko Kohtala 		size_t idlen, len2;
219c6606290SMarko Kohtala 		if (len+1 < count) {
220c6606290SMarko Kohtala 			retval = parport_read (port, buffer+len, count-len-1);
221c6606290SMarko Kohtala 			if (retval < 0)
222c6606290SMarko Kohtala 				return retval;
223c6606290SMarko Kohtala 			len += retval;
224c6606290SMarko Kohtala 		}
225c6606290SMarko Kohtala 		/* Read the whole ID since some devices would not
226c6606290SMarko Kohtala 		 * otherwise give back the Device ID from beginning
227c6606290SMarko Kohtala 		 * next time when asked. */
228c6606290SMarko Kohtala 		idlen = idlens[current_idlen];
229c6606290SMarko Kohtala 		len2 = len;
230c6606290SMarko Kohtala 		while(len2 < idlen && retval > 0) {
231c6606290SMarko Kohtala 			char tmp[4];
232c6606290SMarko Kohtala 			retval = parport_read (port, tmp,
233c6606290SMarko Kohtala 					       min(sizeof tmp, idlen-len2));
234c6606290SMarko Kohtala 			if (retval < 0)
235c6606290SMarko Kohtala 				return retval;
236c6606290SMarko Kohtala 			len2 += retval;
237c6606290SMarko Kohtala 		}
238c6606290SMarko Kohtala 	}
239c6606290SMarko Kohtala 	/* In addition, there are broken devices out there that don't
240c6606290SMarko Kohtala 	   even finish off with a semi-colon. We do not need to care
241c6606290SMarko Kohtala 	   about those at this time. */
242c6606290SMarko Kohtala  done:
243c6606290SMarko Kohtala 	buffer[len] = '\0';
244c6606290SMarko Kohtala 	return len;
245c6606290SMarko Kohtala }
246c6606290SMarko Kohtala 
2471da177e4SLinus Torvalds /* Get Std 1284 Device ID. */
parport_device_id(int devnum,char * buffer,size_t count)248c6606290SMarko Kohtala ssize_t parport_device_id (int devnum, char *buffer, size_t count)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	ssize_t retval = -ENXIO;
251c059d579SSudip Mukherjee 	struct pardevice *dev = parport_open(devnum, daisy_dev_name);
2521da177e4SLinus Torvalds 	if (!dev)
2531da177e4SLinus Torvalds 		return -ENXIO;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	parport_claim_or_block (dev);
2561da177e4SLinus Torvalds 
257c6606290SMarko Kohtala 	/* Negotiate to compatibility mode, and then to device ID
258c6606290SMarko Kohtala 	 * mode. (This so that we start form beginning of device ID if
259c6606290SMarko Kohtala 	 * already in device ID mode.) */
2601da177e4SLinus Torvalds 	parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
2611da177e4SLinus Torvalds 	retval = parport_negotiate (dev->port,
2621da177e4SLinus Torvalds 				    IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if (!retval) {
265c6606290SMarko Kohtala 		retval = parport_read_device_id (dev->port, buffer, count);
2661da177e4SLinus Torvalds 		parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
267c6606290SMarko Kohtala 		if (retval > 2)
268c6606290SMarko Kohtala 			parse_data (dev->port, dev->daisy, buffer+2);
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	parport_release (dev);
2721da177e4SLinus Torvalds 	parport_close (dev);
2731da177e4SLinus Torvalds 	return retval;
2741da177e4SLinus Torvalds }
275