1 /* $Id: parport_probe.c,v 1.3 1997/10/19 18:18:46 phil Exp $
2 * Parallel port device probing code
4 * Authors: Carsten Gross, carsten@sol.wohnheim.uni-ulm.de
5 * Philip Blundell <Philip.Blundell@pobox.com>
8 #include <linux/tasks.h>
9 #include <linux/parport.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/ioport.h>
14 #include <linux/kernel.h>
15 #include <linux/malloc.h>
16 #include <linux/ctype.h>
17 #include <linux/module.h>
23 #include <asm/uaccess.h>
27 static inline int read_nibble(struct parport
*port
)
30 i
= parport_read_status(port
)>>3;
32 if ((i
& 0x10) == 0) i
|= 8;
36 static void read_terminate(struct parport
*port
) {
37 parport_write_control(port
, (parport_read_control(port
) & ~2) | 8);
38 /* SelectIN high, AutoFeed low */
39 if (parport_wait_peripheral(port
, 0x80, 0))
40 /* timeout, SelectIN high, Autofeed low */
42 parport_write_control(port
, parport_read_control(port
) | 2);
44 parport_wait_peripheral(port
, 0x80, 0x80);
45 /* no timeout possible, Autofeed low, SelectIN high */
46 parport_write_control(port
, (parport_read_control(port
) & ~2) | 8);
49 static long read_polled(struct parport
*port
, char *buf
,
54 unsigned int count
= 0;
57 unsigned long igiveupat
=jiffies
+5*HZ
;
59 for (i
=0; time_before(jiffies
, igiveupat
); i
++) {
60 /* if(current->need_resched) schedule(); */
61 parport_write_control(port
, parport_read_control(port
) | 2); /* AutoFeed high */
62 if (parport_wait_peripheral(port
, 0x40, 0)) {
64 /* Some peripherals just time out when they've sent
66 printk("%s: read1 timeout.\n", port
->name
);
68 parport_write_control(port
, parport_read_control(port
) & ~2);
71 z
= read_nibble(port
);
72 parport_write_control(port
, parport_read_control(port
) & ~2); /* AutoFeed low */
73 if (parport_wait_peripheral(port
, 0x40, 0x40)) {
74 printk("%s: read2 timeout.\n", port
->name
);
81 if (++count
== length
)
83 /* Does the error line indicate end of data? */
84 if ((parport_read_status(port
) & LP_PERRORP
) ==
94 int parport_probe(struct parport
*port
, char *buffer
, int len
)
96 struct pardevice
*dev
= parport_register_device(port
, "IEEE 1284 probe", NULL
, NULL
, NULL
, 0, &dev
);
101 printk("%s: unable to register for probe.\n", port
->name
);
105 parport_claim_or_block(dev
);
107 switch (parport_ieee1284_nibble_mode_ok(port
, 4)) {
109 current
->state
=TASK_INTERRUPTIBLE
;
110 /* HACK: wait 10ms because printer seems to ack wrong */
111 schedule_timeout((HZ
+99)/100);
112 result
= read_polled(port
, buffer
, len
);
119 parport_release(dev
);
120 parport_unregister_device(dev
);
129 { "", "Legacy device" },
130 { "PRINTER", "Printer" },
131 { "MODEM", "Modem" },
132 { "NET", "Network device" },
133 { "HDC", "Hard disk" },
134 { "PCMCIA", "PCMCIA" },
135 { "MEDIA", "Multimedia device" },
136 { "FDC", "Floppy disk" },
137 { "PORTS", "Ports" },
138 { "SCANNER", "Scanner" },
139 { "DIGICAM", "Digital camera" },
140 { "", "Unknown device" },
141 { "", "Unspecified" },
145 static char *strdup(char *str
)
147 int n
= strlen(str
)+1;
148 char *s
= kmalloc(n
, GFP_KERNEL
);
150 return strcpy(s
, str
);
153 static void parse_data(struct parport
*port
, char *str
)
155 char *txt
= kmalloc(strlen(str
)+1, GFP_KERNEL
);
157 int guessed_class
= PARPORT_CLASS_UNSPEC
;
160 printk("%s probe: memory squeeze\n", port
->name
);
168 sep
= strchr(p
, ':');
176 if (!strcmp(p
, "MFG") || !strcmp(p
, "MANUFACTURER")) {
177 if (port
->probe_info
.mfr
)
178 kfree (port
->probe_info
.mfr
);
179 port
->probe_info
.mfr
= strdup(sep
);
180 } else if (!strcmp(p
, "MDL") || !strcmp(p
, "MODEL")) {
181 if (port
->probe_info
.model
)
182 kfree (port
->probe_info
.model
);
183 port
->probe_info
.model
= strdup(sep
);
184 } else if (!strcmp(p
, "CLS") || !strcmp(p
, "CLASS")) {
186 if (port
->probe_info
.class_name
)
187 kfree (port
->probe_info
.class_name
);
188 port
->probe_info
.class_name
= strdup(sep
);
189 for (u
= sep
; *u
; u
++)
191 for (i
= 0; classes
[i
].token
; i
++) {
192 if (!strcmp(classes
[i
].token
, sep
)) {
193 port
->probe_info
.class = i
;
197 printk(KERN_WARNING
"%s probe: warning, class '%s' not understood.\n", port
->name
, sep
);
198 port
->probe_info
.class = PARPORT_CLASS_OTHER
;
199 } else if (!strcmp(p
, "CMD") || !strcmp(p
, "COMMAND SET")) {
200 if (port
->probe_info
.cmdset
)
201 kfree (port
->probe_info
.cmdset
);
202 port
->probe_info
.cmdset
= strdup(sep
);
203 /* if it speaks printer language, it's
204 probably a printer */
205 if (strstr(sep
, "PJL") || strstr(sep
, "PCL"))
206 guessed_class
= PARPORT_CLASS_PRINTER
;
207 } else if (!strcmp(p
, "DES") || !strcmp(p
, "DESCRIPTION")) {
208 if (port
->probe_info
.description
)
209 kfree (port
->probe_info
.description
);
210 port
->probe_info
.description
= strdup(sep
);
214 if (q
) p
= q
+1; else p
=NULL
;
217 /* If the device didn't tell us its class, maybe we have managed to
218 guess one from the things it did say. */
219 if (port
->probe_info
.class == PARPORT_CLASS_UNSPEC
)
220 port
->probe_info
.class = guessed_class
;
225 static void pretty_print(struct parport
*port
)
227 printk(KERN_INFO
"%s: %s", port
->name
,
228 classes
[port
->probe_info
.class].descr
);
229 if (port
->probe_info
.class) {
230 printk(", %s %s", port
->probe_info
.mfr
,
231 port
->probe_info
.model
);
236 void parport_probe_one(struct parport
*port
)
238 char *buffer
= kmalloc(2048, GFP_KERNEL
);
242 port
->probe_info
.model
= strdup ("Unknown device");
243 port
->probe_info
.mfr
= strdup ("Unknown vendor");
244 port
->probe_info
.description
= port
->probe_info
.cmdset
= NULL
;
245 port
->probe_info
.class = PARPORT_CLASS_UNSPEC
;
246 port
->probe_info
.class_name
= NULL
;
249 printk(KERN_ERR
"%s probe: Memory squeeze.\n", port
->name
);
253 r
= parport_probe(port
, buffer
, 2047);
256 printk(KERN_INFO
"%s: no IEEE-1284 device present.\n",
258 port
->probe_info
.class = PARPORT_CLASS_LEGACY
;
260 printk(KERN_INFO
"%s: no ID data returned by device.\n",
265 printk("%s id: %s\n", port
->name
, buffer
+2);
267 parse_data(port
, buffer
+2);
275 int init_module(void)
278 for (p
= parport_enumerate(); p
; p
= p
->next
)
279 parport_probe_one(p
);
280 parport_probe_hook
= &parport_probe_one
;
284 void cleanup_module(void)
286 parport_probe_hook
= NULL
;