18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Parallel port device probing code
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:    Carsten Gross, carsten@sol.wohnheim.uni-ulm.de
68c2ecf20Sopenharmony_ci *             Philip Blundell <philb@gnu.org>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/parport.h>
118c2ecf20Sopenharmony_ci#include <linux/ctype.h>
128c2ecf20Sopenharmony_ci#include <linux/string.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic const struct {
178c2ecf20Sopenharmony_ci	const char *token;
188c2ecf20Sopenharmony_ci	const char *descr;
198c2ecf20Sopenharmony_ci} classes[] = {
208c2ecf20Sopenharmony_ci	{ "",            "Legacy device" },
218c2ecf20Sopenharmony_ci	{ "PRINTER",     "Printer" },
228c2ecf20Sopenharmony_ci	{ "MODEM",       "Modem" },
238c2ecf20Sopenharmony_ci	{ "NET",         "Network device" },
248c2ecf20Sopenharmony_ci	{ "HDC",       	 "Hard disk" },
258c2ecf20Sopenharmony_ci	{ "PCMCIA",      "PCMCIA" },
268c2ecf20Sopenharmony_ci	{ "MEDIA",       "Multimedia device" },
278c2ecf20Sopenharmony_ci	{ "FDC",         "Floppy disk" },
288c2ecf20Sopenharmony_ci	{ "PORTS",       "Ports" },
298c2ecf20Sopenharmony_ci	{ "SCANNER",     "Scanner" },
308c2ecf20Sopenharmony_ci	{ "DIGICAM",     "Digital camera" },
318c2ecf20Sopenharmony_ci	{ "",            "Unknown device" },
328c2ecf20Sopenharmony_ci	{ "",            "Unspecified" },
338c2ecf20Sopenharmony_ci	{ "SCSIADAPTER", "SCSI adapter" },
348c2ecf20Sopenharmony_ci	{ NULL,          NULL }
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic void pretty_print(struct parport *port, int device)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct parport_device_info *info = &port->probe_info[device + 1];
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	pr_info("%s", port->name);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (device >= 0)
448c2ecf20Sopenharmony_ci		pr_cont(" (addr %d)", device);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	pr_cont(": %s", classes[info->class].descr);
478c2ecf20Sopenharmony_ci	if (info->class)
488c2ecf20Sopenharmony_ci		pr_cont(", %s %s", info->mfr, info->model);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	pr_cont("\n");
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void parse_data(struct parport *port, int device, char *str)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
568c2ecf20Sopenharmony_ci	char *p = txt, *q;
578c2ecf20Sopenharmony_ci	int guessed_class = PARPORT_CLASS_UNSPEC;
588c2ecf20Sopenharmony_ci	struct parport_device_info *info = &port->probe_info[device + 1];
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (!txt) {
618c2ecf20Sopenharmony_ci		pr_warn("%s probe: memory squeeze\n", port->name);
628c2ecf20Sopenharmony_ci		return;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci	strcpy(txt, str);
658c2ecf20Sopenharmony_ci	while (p) {
668c2ecf20Sopenharmony_ci		char *sep;
678c2ecf20Sopenharmony_ci		q = strchr(p, ';');
688c2ecf20Sopenharmony_ci		if (q) *q = 0;
698c2ecf20Sopenharmony_ci		sep = strchr(p, ':');
708c2ecf20Sopenharmony_ci		if (sep) {
718c2ecf20Sopenharmony_ci			char *u;
728c2ecf20Sopenharmony_ci			*(sep++) = 0;
738c2ecf20Sopenharmony_ci			/* Get rid of trailing blanks */
748c2ecf20Sopenharmony_ci			u = sep + strlen (sep) - 1;
758c2ecf20Sopenharmony_ci			while (u >= p && *u == ' ')
768c2ecf20Sopenharmony_ci				*u-- = '\0';
778c2ecf20Sopenharmony_ci			u = p;
788c2ecf20Sopenharmony_ci			while (*u) {
798c2ecf20Sopenharmony_ci				*u = toupper(*u);
808c2ecf20Sopenharmony_ci				u++;
818c2ecf20Sopenharmony_ci			}
828c2ecf20Sopenharmony_ci			if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
838c2ecf20Sopenharmony_ci				kfree(info->mfr);
848c2ecf20Sopenharmony_ci				info->mfr = kstrdup(sep, GFP_KERNEL);
858c2ecf20Sopenharmony_ci			} else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
868c2ecf20Sopenharmony_ci				kfree(info->model);
878c2ecf20Sopenharmony_ci				info->model = kstrdup(sep, GFP_KERNEL);
888c2ecf20Sopenharmony_ci			} else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
898c2ecf20Sopenharmony_ci				int i;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci				kfree(info->class_name);
928c2ecf20Sopenharmony_ci				info->class_name = kstrdup(sep, GFP_KERNEL);
938c2ecf20Sopenharmony_ci				for (u = sep; *u; u++)
948c2ecf20Sopenharmony_ci					*u = toupper(*u);
958c2ecf20Sopenharmony_ci				for (i = 0; classes[i].token; i++) {
968c2ecf20Sopenharmony_ci					if (!strcmp(classes[i].token, sep)) {
978c2ecf20Sopenharmony_ci						info->class = i;
988c2ecf20Sopenharmony_ci						goto rock_on;
998c2ecf20Sopenharmony_ci					}
1008c2ecf20Sopenharmony_ci				}
1018c2ecf20Sopenharmony_ci				pr_warn("%s probe: warning, class '%s' not understood\n",
1028c2ecf20Sopenharmony_ci					port->name, sep);
1038c2ecf20Sopenharmony_ci				info->class = PARPORT_CLASS_OTHER;
1048c2ecf20Sopenharmony_ci			} else if (!strcmp(p, "CMD") ||
1058c2ecf20Sopenharmony_ci				   !strcmp(p, "COMMAND SET")) {
1068c2ecf20Sopenharmony_ci				kfree(info->cmdset);
1078c2ecf20Sopenharmony_ci				info->cmdset = kstrdup(sep, GFP_KERNEL);
1088c2ecf20Sopenharmony_ci				/* if it speaks printer language, it's
1098c2ecf20Sopenharmony_ci				   probably a printer */
1108c2ecf20Sopenharmony_ci				if (strstr(sep, "PJL") || strstr(sep, "PCL"))
1118c2ecf20Sopenharmony_ci					guessed_class = PARPORT_CLASS_PRINTER;
1128c2ecf20Sopenharmony_ci			} else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
1138c2ecf20Sopenharmony_ci				kfree(info->description);
1148c2ecf20Sopenharmony_ci				info->description = kstrdup(sep, GFP_KERNEL);
1158c2ecf20Sopenharmony_ci			}
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	rock_on:
1188c2ecf20Sopenharmony_ci		if (q)
1198c2ecf20Sopenharmony_ci			p = q + 1;
1208c2ecf20Sopenharmony_ci		else
1218c2ecf20Sopenharmony_ci			p = NULL;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* If the device didn't tell us its class, maybe we have managed to
1258c2ecf20Sopenharmony_ci	   guess one from the things it did say. */
1268c2ecf20Sopenharmony_ci	if (info->class == PARPORT_CLASS_UNSPEC)
1278c2ecf20Sopenharmony_ci		info->class = guessed_class;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	pretty_print (port, device);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	kfree(txt);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* Read up to count-1 bytes of device id. Terminate buffer with
1358c2ecf20Sopenharmony_ci * '\0'. Buffer begins with two Device ID length bytes as given by
1368c2ecf20Sopenharmony_ci * device. */
1378c2ecf20Sopenharmony_cistatic ssize_t parport_read_device_id (struct parport *port, char *buffer,
1388c2ecf20Sopenharmony_ci				       size_t count)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	unsigned char length[2];
1418c2ecf20Sopenharmony_ci	unsigned lelen, belen;
1428c2ecf20Sopenharmony_ci	size_t idlens[4];
1438c2ecf20Sopenharmony_ci	unsigned numidlens;
1448c2ecf20Sopenharmony_ci	unsigned current_idlen;
1458c2ecf20Sopenharmony_ci	ssize_t retval;
1468c2ecf20Sopenharmony_ci	size_t len;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* First two bytes are MSB,LSB of inclusive length. */
1498c2ecf20Sopenharmony_ci	retval = parport_read (port, length, 2);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (retval < 0)
1528c2ecf20Sopenharmony_ci		return retval;
1538c2ecf20Sopenharmony_ci	if (retval != 2)
1548c2ecf20Sopenharmony_ci		return -EIO;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (count < 2)
1578c2ecf20Sopenharmony_ci		return 0;
1588c2ecf20Sopenharmony_ci	memcpy(buffer, length, 2);
1598c2ecf20Sopenharmony_ci	len = 2;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* Some devices wrongly send LE length, and some send it two
1628c2ecf20Sopenharmony_ci	 * bytes short. Construct a sorted array of lengths to try. */
1638c2ecf20Sopenharmony_ci	belen = (length[0] << 8) + length[1];
1648c2ecf20Sopenharmony_ci	lelen = (length[1] << 8) + length[0];
1658c2ecf20Sopenharmony_ci	idlens[0] = min(belen, lelen);
1668c2ecf20Sopenharmony_ci	idlens[1] = idlens[0]+2;
1678c2ecf20Sopenharmony_ci	if (belen != lelen) {
1688c2ecf20Sopenharmony_ci		int off = 2;
1698c2ecf20Sopenharmony_ci		/* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
1708c2ecf20Sopenharmony_ci		if (idlens[0] <= 2)
1718c2ecf20Sopenharmony_ci			off = 0;
1728c2ecf20Sopenharmony_ci		idlens[off] = max(belen, lelen);
1738c2ecf20Sopenharmony_ci		idlens[off+1] = idlens[off]+2;
1748c2ecf20Sopenharmony_ci		numidlens = off+2;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci	else {
1778c2ecf20Sopenharmony_ci		/* Some devices don't truly implement Device ID, but
1788c2ecf20Sopenharmony_ci		 * just return constant nibble forever. This catches
1798c2ecf20Sopenharmony_ci		 * also those cases. */
1808c2ecf20Sopenharmony_ci		if (idlens[0] == 0 || idlens[0] > 0xFFF) {
1818c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "%s: reported broken Device ID length of %#zX bytes\n",
1828c2ecf20Sopenharmony_ci			       port->name, idlens[0]);
1838c2ecf20Sopenharmony_ci			return -EIO;
1848c2ecf20Sopenharmony_ci		}
1858c2ecf20Sopenharmony_ci		numidlens = 2;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* Try to respect the given ID length despite all the bugs in
1898c2ecf20Sopenharmony_ci	 * the ID length. Read according to shortest possible ID
1908c2ecf20Sopenharmony_ci	 * first. */
1918c2ecf20Sopenharmony_ci	for (current_idlen = 0; current_idlen < numidlens; ++current_idlen) {
1928c2ecf20Sopenharmony_ci		size_t idlen = idlens[current_idlen];
1938c2ecf20Sopenharmony_ci		if (idlen+1 >= count)
1948c2ecf20Sopenharmony_ci			break;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		retval = parport_read (port, buffer+len, idlen-len);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		if (retval < 0)
1998c2ecf20Sopenharmony_ci			return retval;
2008c2ecf20Sopenharmony_ci		len += retval;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci		if (port->physport->ieee1284.phase != IEEE1284_PH_HBUSY_DAVAIL) {
2038c2ecf20Sopenharmony_ci			if (belen != len) {
2048c2ecf20Sopenharmony_ci				printk(KERN_DEBUG "%s: Device ID was %zd bytes while device told it would be %d bytes\n",
2058c2ecf20Sopenharmony_ci				       port->name, len, belen);
2068c2ecf20Sopenharmony_ci			}
2078c2ecf20Sopenharmony_ci			goto done;
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		/* This might end reading the Device ID too
2118c2ecf20Sopenharmony_ci		 * soon. Hopefully the needed fields were already in
2128c2ecf20Sopenharmony_ci		 * the first 256 bytes or so that we must have read so
2138c2ecf20Sopenharmony_ci		 * far. */
2148c2ecf20Sopenharmony_ci		if (buffer[len-1] == ';') {
2158c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "%s: Device ID reading stopped before device told data not available. Current idlen %u of %u, len bytes %02X %02X\n",
2168c2ecf20Sopenharmony_ci			       port->name, current_idlen, numidlens,
2178c2ecf20Sopenharmony_ci			       length[0], length[1]);
2188c2ecf20Sopenharmony_ci			goto done;
2198c2ecf20Sopenharmony_ci		}
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci	if (current_idlen < numidlens) {
2228c2ecf20Sopenharmony_ci		/* Buffer not large enough, read to end of buffer. */
2238c2ecf20Sopenharmony_ci		size_t idlen, len2;
2248c2ecf20Sopenharmony_ci		if (len+1 < count) {
2258c2ecf20Sopenharmony_ci			retval = parport_read (port, buffer+len, count-len-1);
2268c2ecf20Sopenharmony_ci			if (retval < 0)
2278c2ecf20Sopenharmony_ci				return retval;
2288c2ecf20Sopenharmony_ci			len += retval;
2298c2ecf20Sopenharmony_ci		}
2308c2ecf20Sopenharmony_ci		/* Read the whole ID since some devices would not
2318c2ecf20Sopenharmony_ci		 * otherwise give back the Device ID from beginning
2328c2ecf20Sopenharmony_ci		 * next time when asked. */
2338c2ecf20Sopenharmony_ci		idlen = idlens[current_idlen];
2348c2ecf20Sopenharmony_ci		len2 = len;
2358c2ecf20Sopenharmony_ci		while(len2 < idlen && retval > 0) {
2368c2ecf20Sopenharmony_ci			char tmp[4];
2378c2ecf20Sopenharmony_ci			retval = parport_read (port, tmp,
2388c2ecf20Sopenharmony_ci					       min(sizeof tmp, idlen-len2));
2398c2ecf20Sopenharmony_ci			if (retval < 0)
2408c2ecf20Sopenharmony_ci				return retval;
2418c2ecf20Sopenharmony_ci			len2 += retval;
2428c2ecf20Sopenharmony_ci		}
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci	/* In addition, there are broken devices out there that don't
2458c2ecf20Sopenharmony_ci	   even finish off with a semi-colon. We do not need to care
2468c2ecf20Sopenharmony_ci	   about those at this time. */
2478c2ecf20Sopenharmony_ci done:
2488c2ecf20Sopenharmony_ci	buffer[len] = '\0';
2498c2ecf20Sopenharmony_ci	return len;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* Get Std 1284 Device ID. */
2538c2ecf20Sopenharmony_cissize_t parport_device_id (int devnum, char *buffer, size_t count)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	ssize_t retval = -ENXIO;
2568c2ecf20Sopenharmony_ci	struct pardevice *dev = parport_open(devnum, daisy_dev_name);
2578c2ecf20Sopenharmony_ci	if (!dev)
2588c2ecf20Sopenharmony_ci		return -ENXIO;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	parport_claim_or_block (dev);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* Negotiate to compatibility mode, and then to device ID
2638c2ecf20Sopenharmony_ci	 * mode. (This so that we start form beginning of device ID if
2648c2ecf20Sopenharmony_ci	 * already in device ID mode.) */
2658c2ecf20Sopenharmony_ci	parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
2668c2ecf20Sopenharmony_ci	retval = parport_negotiate (dev->port,
2678c2ecf20Sopenharmony_ci				    IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if (!retval) {
2708c2ecf20Sopenharmony_ci		retval = parport_read_device_id (dev->port, buffer, count);
2718c2ecf20Sopenharmony_ci		parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
2728c2ecf20Sopenharmony_ci		if (retval > 2)
2738c2ecf20Sopenharmony_ci			parse_data (dev->port, dev->daisy, buffer+2);
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	parport_release (dev);
2778c2ecf20Sopenharmony_ci	parport_close (dev);
2788c2ecf20Sopenharmony_ci	return retval;
2798c2ecf20Sopenharmony_ci}
280