Import 2.3.18pre1
[davej-history.git] / drivers / parport / probe.c
blob9cfb0079479061de827afacc144c096abd05633f
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 <Philip.Blundell@pobox.com>
6 */
8 #include <linux/parport.h>
9 #include <linux/ctype.h>
10 #include <asm/uaccess.h>
12 static struct {
13 char *token;
14 char *descr;
15 } classes[] = {
16 { "", "Legacy device" },
17 { "PRINTER", "Printer" },
18 { "MODEM", "Modem" },
19 { "NET", "Network device" },
20 { "HDC", "Hard disk" },
21 { "PCMCIA", "PCMCIA" },
22 { "MEDIA", "Multimedia device" },
23 { "FDC", "Floppy disk" },
24 { "PORTS", "Ports" },
25 { "SCANNER", "Scanner" },
26 { "DIGICAM", "Digital camera" },
27 { "", "Unknown device" },
28 { "", "Unspecified" },
29 { "SCSIADAPTER", "SCSI adapter" },
30 { NULL, NULL }
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);
39 if (device >= 0)
40 printk (" (addr %d)", device);
42 printk (": %s", classes[info->class].descr);
43 if (info->class)
44 printk(", %s %s", info->mfr, info->model);
46 printk("\n");
49 static char *strdup(char *str)
51 int n = strlen(str)+1;
52 char *s = kmalloc(n, GFP_KERNEL);
53 if (!s) return NULL;
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);
60 char *p = txt, *q;
61 int guessed_class = PARPORT_CLASS_UNSPEC;
62 struct parport_device_info *info = &port->probe_info[device + 1];
64 if (!txt) {
65 printk("%s probe: memory squeeze\n", port->name);
66 return;
68 strcpy(txt, str);
69 while (p) {
70 char *sep;
71 q = strchr(p, ';');
72 if (q) *q = 0;
73 sep = strchr(p, ':');
74 if (sep) {
75 char *u = p;
76 *(sep++) = 0;
77 while (*u) {
78 *u = toupper(*u);
79 u++;
81 if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
82 if (info->mfr)
83 kfree (info->mfr);
84 info->mfr = strdup(sep);
85 } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
86 if (info->model)
87 kfree (info->model);
88 info->model = strdup(sep);
89 } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
90 int i;
91 if (info->class_name)
92 kfree (info->class_name);
93 info->class_name = strdup(sep);
94 for (u = sep; *u; u++)
95 *u = toupper(*u);
96 for (i = 0; classes[i].token; i++) {
97 if (!strcmp(classes[i].token, sep)) {
98 info->class = i;
99 goto rock_on;
102 printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
103 info->class = PARPORT_CLASS_OTHER;
104 } else if (!strcmp(p, "CMD") ||
105 !strcmp(p, "COMMAND SET")) {
106 if (info->cmdset)
107 kfree (info->cmdset);
108 info->cmdset = strdup(sep);
109 /* if it speaks printer language, it's
110 probably a printer */
111 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
112 guessed_class = PARPORT_CLASS_PRINTER;
113 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
114 if (info->description)
115 kfree (info->description);
116 info->description = strdup(sep);
119 rock_on:
120 if (q) p = q+1; else p=NULL;
123 /* If the device didn't tell us its class, maybe we have managed to
124 guess one from the things it did say. */
125 if (info->class == PARPORT_CLASS_UNSPEC)
126 info->class = guessed_class;
128 pretty_print (port, device);
130 kfree(txt);
133 /* Get Std 1284 Device ID. */
134 ssize_t parport_device_id (int devnum, char *buffer, size_t len)
136 ssize_t retval = -ENXIO;
137 struct pardevice *dev = parport_open (devnum, "Device ID probe",
138 NULL, NULL, NULL, 0, NULL);
139 if (!dev)
140 return -ENXIO;
142 parport_claim_or_block (dev);
144 /* Negotiate to compatibility mode, and then to device ID mode.
145 * (This is in case we are already in device ID mode.) */
146 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
147 retval = parport_negotiate (dev->port,
148 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
150 if (!retval) {
151 int idlen;
152 unsigned char length[2];
153 mm_segment_t oldfs = get_fs ();
154 set_fs (get_ds ());
156 /* First two bytes are MSB,LSB of inclusive length. */
157 retval = parport_read (dev->port, length, 2);
159 if (retval != 2) goto restore_fs;
161 idlen = (length[0] << 8) + length[1] - 2;
162 if (idlen < len)
163 len = idlen;
164 retval = parport_read (dev->port, buffer, len);
166 if (retval != len)
167 printk (KERN_DEBUG "%s: only read %d of %d ID bytes\n",
168 dev->port->name, retval, len);
170 /* Some printer manufacturers mistakenly believe that
171 the length field is supposed to be _exclusive_. */
172 /* In addition, there are broken devices out there
173 that don't even finish off with a semi-colon. */
174 if (buffer[len - 1] != ';') {
175 ssize_t diff;
176 diff = parport_read (dev->port, buffer + len, 2);
177 retval += diff;
179 if (diff)
180 printk (KERN_DEBUG
181 "%s: device reported incorrect "
182 "length field (%d, should be %d)\n",
183 dev->port->name, idlen, retval);
184 else {
185 /* One semi-colon short of a device ID. */
186 buffer[len++] = ';';
187 printk (KERN_DEBUG "%s: faking semi-colon\n",
188 dev->port->name);
190 /* If we get here, I don't think we
191 need to worry about the possible
192 standard violation of having read
193 more than we were told to. The
194 device is non-compliant anyhow. */
198 restore_fs:
199 buffer[len] = '\0';
200 set_fs (oldfs);
201 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
203 parport_release (dev);
205 if (retval > 2)
206 parse_data (dev->port, dev->daisy, buffer);
208 parport_close (dev);
209 return retval;