Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / parport / probe.c
blobc94963145e17991bbfeb233e8d7a63733da58698
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>
6 */
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>
14 static struct {
15 char *token;
16 char *descr;
17 } classes[] = {
18 { "", "Legacy device" },
19 { "PRINTER", "Printer" },
20 { "MODEM", "Modem" },
21 { "NET", "Network device" },
22 { "HDC", "Hard disk" },
23 { "PCMCIA", "PCMCIA" },
24 { "MEDIA", "Multimedia device" },
25 { "FDC", "Floppy disk" },
26 { "PORTS", "Ports" },
27 { "SCANNER", "Scanner" },
28 { "DIGICAM", "Digital camera" },
29 { "", "Unknown device" },
30 { "", "Unspecified" },
31 { "SCSIADAPTER", "SCSI adapter" },
32 { NULL, NULL }
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);
41 if (device >= 0)
42 printk (" (addr %d)", device);
44 printk (": %s", classes[info->class].descr);
45 if (info->class)
46 printk(", %s %s", info->mfr, info->model);
48 printk("\n");
51 static char *strdup(char *str)
53 int n = strlen(str)+1;
54 char *s = kmalloc(n, GFP_KERNEL);
55 if (!s) return NULL;
56 return strcpy(s, str);
59 static void parse_data(struct parport *port, int device, char *str)
61 char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
62 char *p = txt, *q;
63 int guessed_class = PARPORT_CLASS_UNSPEC;
64 struct parport_device_info *info = &port->probe_info[device + 1];
66 if (!txt) {
67 printk(KERN_WARNING "%s probe: memory squeeze\n", port->name);
68 return;
70 strcpy(txt, str);
71 while (p) {
72 char *sep;
73 q = strchr(p, ';');
74 if (q) *q = 0;
75 sep = strchr(p, ':');
76 if (sep) {
77 char *u;
78 *(sep++) = 0;
79 /* Get rid of trailing blanks */
80 u = sep + strlen (sep) - 1;
81 while (u >= p && *u == ' ')
82 *u-- = '\0';
83 u = p;
84 while (*u) {
85 *u = toupper(*u);
86 u++;
88 if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
89 if (info->mfr)
90 kfree (info->mfr);
91 info->mfr = strdup(sep);
92 } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
93 if (info->model)
94 kfree (info->model);
95 info->model = strdup(sep);
96 } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
97 int i;
98 if (info->class_name)
99 kfree (info->class_name);
100 info->class_name = strdup(sep);
101 for (u = sep; *u; u++)
102 *u = toupper(*u);
103 for (i = 0; classes[i].token; i++) {
104 if (!strcmp(classes[i].token, sep)) {
105 info->class = i;
106 goto rock_on;
109 printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
110 info->class = PARPORT_CLASS_OTHER;
111 } else if (!strcmp(p, "CMD") ||
112 !strcmp(p, "COMMAND SET")) {
113 if (info->cmdset)
114 kfree (info->cmdset);
115 info->cmdset = strdup(sep);
116 /* if it speaks printer language, it's
117 probably a printer */
118 if (strstr(sep, "PJL") || strstr(sep, "PCL"))
119 guessed_class = PARPORT_CLASS_PRINTER;
120 } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
121 if (info->description)
122 kfree (info->description);
123 info->description = strdup(sep);
126 rock_on:
127 if (q) p = q+1; else p=NULL;
130 /* If the device didn't tell us its class, maybe we have managed to
131 guess one from the things it did say. */
132 if (info->class == PARPORT_CLASS_UNSPEC)
133 info->class = guessed_class;
135 pretty_print (port, device);
137 kfree(txt);
140 /* Get Std 1284 Device ID. */
141 ssize_t parport_device_id (int devnum, char *buffer, size_t len)
143 ssize_t retval = -ENXIO;
144 struct pardevice *dev = parport_open (devnum, "Device ID probe",
145 NULL, NULL, NULL, 0, NULL);
146 if (!dev)
147 return -ENXIO;
149 parport_claim_or_block (dev);
151 /* Negotiate to compatibility mode, and then to device ID mode.
152 * (This is in case we are already in device ID mode.) */
153 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
154 retval = parport_negotiate (dev->port,
155 IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
157 if (!retval) {
158 int idlen;
159 unsigned char length[2];
161 /* First two bytes are MSB,LSB of inclusive length. */
162 retval = parport_read (dev->port, length, 2);
164 if (retval != 2) goto end_id;
166 idlen = (length[0] << 8) + length[1] - 2;
168 * Check if the caller-allocated buffer is large enough
169 * otherwise bail out or there will be an at least off by one.
171 if (idlen + 1 < len)
172 len = idlen;
173 else {
174 retval = -EINVAL;
175 goto out;
177 retval = parport_read (dev->port, buffer, len);
179 if (retval != len)
180 printk (KERN_DEBUG "%s: only read %Zd of %Zd ID bytes\n",
181 dev->port->name, retval,
182 len);
184 /* Some printer manufacturers mistakenly believe that
185 the length field is supposed to be _exclusive_.
186 In addition, there are broken devices out there
187 that don't even finish off with a semi-colon. */
188 if (buffer[len - 1] != ';') {
189 ssize_t diff;
190 diff = parport_read (dev->port, buffer + len, 2);
191 retval += diff;
193 if (diff)
194 printk (KERN_DEBUG
195 "%s: device reported incorrect "
196 "length field (%d, should be %Zd)\n",
197 dev->port->name, idlen, retval);
198 else {
199 /* One semi-colon short of a device ID. */
200 buffer[len++] = ';';
201 printk (KERN_DEBUG "%s: faking semi-colon\n",
202 dev->port->name);
204 /* If we get here, I don't think we
205 need to worry about the possible
206 standard violation of having read
207 more than we were told to. The
208 device is non-compliant anyhow. */
212 end_id:
213 buffer[len] = '\0';
214 parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
217 if (retval > 2)
218 parse_data (dev->port, dev->daisy, buffer);
220 out:
221 parport_release (dev);
222 parport_close (dev);
223 return retval;