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")) {
82 info->mfr = kstrdup(sep, GFP_KERNEL);
83 } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
85 info->model = kstrdup(sep, GFP_KERNEL);
86 } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
89 kfree(info->class_name);
90 info->class_name = kstrdup(sep, GFP_KERNEL);
91 for (u = sep; *u; u++)
93 for (i = 0; classes[i].token; i++) {
94 if (!strcmp(classes[i].token, sep)) {
99 printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
100 info->class = PARPORT_CLASS_OTHER;
101 } else if (!strcmp(p, "CMD") ||
102 !strcmp(p, "COMMAND SET")) {
104 info->cmdset = kstrdup(sep, GFP_KERNEL);
105 /* if it speaks printer language, it's
106 probably a printer */
107 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
108 guessed_class = PARPORT_CLASS_PRINTER;
109 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
110 kfree(info->description);
111 info->description = kstrdup(sep, GFP_KERNEL);
121 /* If the device didn't tell us its class, maybe we have managed to
122 guess one from the things it did say. */
123 if (info->class == PARPORT_CLASS_UNSPEC)
124 info->class = guessed_class;
126 pretty_print (port, device);
131 /* Read up to count-1 bytes of device id. Terminate buffer with
132 * '\0'. Buffer begins with two Device ID length bytes as given by
134 static ssize_t parport_read_device_id (struct parport *port, char *buffer,
137 unsigned char length[2];
138 unsigned lelen, belen;
141 unsigned current_idlen;
145 /* First two bytes are MSB,LSB of inclusive length. */
146 retval = parport_read (port, length, 2);
155 memcpy(buffer, length, 2);
158 /* Some devices wrongly send LE length, and some send it two
159 * bytes short. Construct a sorted array of lengths to try. */
160 belen = (length[0] << 8) + length[1];
161 lelen = (length[1] << 8) + length[0];
162 idlens[0] = min(belen, lelen);
163 idlens[1] = idlens[0]+2;
164 if (belen != lelen) {
166 /* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
169 idlens[off] = max(belen, lelen);
170 idlens[off+1] = idlens[off]+2;
174 /* Some devices don't truly implement Device ID, but
175 * just return constant nibble forever. This catches
176 * also those cases. */
177 if (idlens[0] == 0 || idlens[0] > 0xFFF) {
178 printk (KERN_DEBUG "%s: reported broken Device ID"
179 " length of %#zX bytes\n",
180 port->name, idlens[0]);
186 /* Try to respect the given ID length despite all the bugs in
187 * the ID length. Read according to shortest possible ID
189 for (current_idlen = 0; current_idlen < numidlens; ++current_idlen) {
190 size_t idlen = idlens[current_idlen];
191 if (idlen+1 >= count)
194 retval = parport_read (port, buffer+len, idlen-len);
200 if (port->physport->ieee1284.phase != IEEE1284_PH_HBUSY_DAVAIL) {
202 printk (KERN_DEBUG "%s: Device ID was %zd bytes"
203 " while device told it would be %d"
205 port->name, len, belen);
210 /* This might end reading the Device ID too
211 * soon. Hopefully the needed fields were already in
212 * the first 256 bytes or so that we must have read so
214 if (buffer[len-1] == ';') {
215 printk (KERN_DEBUG "%s: Device ID reading stopped"
216 " before device told data not available. "
217 "Current idlen %u of %u, len bytes %02X %02X\n",
218 port->name, current_idlen, numidlens,
219 length[0], length[1]);
223 if (current_idlen < numidlens) {
224 /* Buffer not large enough, read to end of buffer. */
227 retval = parport_read (port, buffer+len, count-len-1);
232 /* Read the whole ID since some devices would not
233 * otherwise give back the Device ID from beginning
234 * next time when asked. */
235 idlen = idlens[current_idlen];
237 while(len2 < idlen && retval > 0) {
239 retval = parport_read (port, tmp,
240 min(sizeof tmp, idlen-len2));
246 /* In addition, there are broken devices out there that don't
247 even finish off with a semi-colon. We do not need to care
248 about those at this time. */
254 /* Get Std 1284 Device ID. */
255 ssize_t parport_device_id (int devnum, char *buffer, size_t count)
257 ssize_t retval = -ENXIO;
258 struct pardevice *dev = parport_open (devnum, "Device ID probe");
262 parport_claim_or_block (dev);
264 /* Negotiate to compatibility mode, and then to device ID
265 * mode. (This so that we start form beginning of device ID if
266 * already in device ID mode.) */
267 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
268 retval = parport_negotiate (dev->port,
269 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
272 retval = parport_read_device_id (dev->port, buffer, count);
273 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
275 parse_data (dev->port, dev->daisy, buffer+2);
278 parport_release (dev);