xref: /netbsd-src/sys/dev/ppbus/ppbus_base.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: ppbus_base.c,v 1.14 2007/12/05 07:58:31 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * FreeBSD: src/sys/dev/ppbus/ppb_base.c,v 1.10.2.1 2000/08/01 23:26:26 n_hibma Exp
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ppbus_base.c,v 1.14 2007/12/05 07:58:31 ad Exp $");
34 
35 #include "opt_ppbus_1284.h"
36 #include "opt_ppbus.h"
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/lock.h>
43 #include <sys/systm.h>
44 
45 #include <dev/ppbus/ppbus_1284.h>
46 #include <dev/ppbus/ppbus_conf.h>
47 #include <dev/ppbus/ppbus_base.h>
48 #include <dev/ppbus/ppbus_device.h>
49 #include <dev/ppbus/ppbus_io.h>
50 #include <dev/ppbus/ppbus_var.h>
51 
52 #ifndef DONTPROBE_1284
53 /* Utility functions */
54 static char * search_token(char *, int, const char *);
55 #endif
56 
57 /* Perform general ppbus I/O request */
58 int
59 ppbus_io(struct device * dev, int iop, u_char * addr, int cnt, u_char byte)
60 {
61 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
62 	return (bus->ppbus_io(device_parent(dev), iop, addr, cnt, byte));
63 }
64 
65 /* Execute microsequence */
66 int
67 ppbus_exec_microseq(struct device * dev, struct ppbus_microseq ** sequence)
68 {
69 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
70 	return (bus->ppbus_exec_microseq(device_parent(dev), sequence));
71 }
72 
73 /* Read instance variables of ppbus */
74 int
75 ppbus_read_ivar(struct device * dev, int index, unsigned int * val)
76 
77 {
78 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
79 
80 	switch (index) {
81 	case PPBUS_IVAR_INTR:
82 	case PPBUS_IVAR_EPP_PROTO:
83 	case PPBUS_IVAR_DMA:
84 		return (bus->ppbus_read_ivar(device_parent(dev), index, val));
85 
86 	case PPBUS_IVAR_IEEE:
87 		*val = (bus->sc_use_ieee == PPBUS_ENABLE_IEEE) ? 1 : 0;
88 		break;
89 
90 	default:
91 		return (ENOENT);
92 	}
93 
94 	return 0;
95 }
96 
97 /* Write an instance variable */
98 int
99 ppbus_write_ivar(struct device * dev, int index, unsigned int * val)
100 {
101 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
102 
103 	switch (index) {
104 	case PPBUS_IVAR_INTR:
105 	case PPBUS_IVAR_EPP_PROTO:
106 	case PPBUS_IVAR_DMA:
107 		return (bus->ppbus_write_ivar(device_parent(dev), index, val));
108 
109 	case PPBUS_IVAR_IEEE:
110 		bus->sc_use_ieee = ((*val != 0) ? PPBUS_ENABLE_IEEE :
111 			PPBUS_DISABLE_IEEE);
112 		break;
113 
114 	default:
115 		return (ENOENT);
116 	}
117 
118 	return 0;
119 }
120 
121 /* Polls the bus for a max of 10-milliseconds */
122 int
123 ppbus_poll_bus(struct device * dev, int maxp, char mask, char status,
124 	int how)
125 {
126 	int i, j, error;
127 	char r;
128 
129 	/* try at least up to 10ms */
130 	for (j = 0; j < ((how & PPBUS_POLL) ? maxp : 1); j++) {
131 		for (i = 0; i < 10000; i++) {
132 			r = ppbus_rstr(dev);
133 			DELAY(1);
134 			if ((r & mask) == status)
135 				return (0);
136 		}
137 	}
138 
139 	if (!(how & PPBUS_POLL)) {
140 	   for (i = 0; maxp == PPBUS_FOREVER || i < maxp-1; i++) {
141 		if ((ppbus_rstr(dev) & mask) == status)
142 			return (0);
143 
144 		switch (how) {
145 		case PPBUS_NOINTR:
146 			/* wait 10 ms */
147 			tsleep((void *)dev, PPBUSPRI, "ppbuspoll", hz/100);
148 			break;
149 
150 		case PPBUS_INTR:
151 		default:
152 			/* wait 10 ms */
153 			if (((error = tsleep((void *)dev, PPBUSPRI | PCATCH,
154 			    "ppbuspoll", hz/100)) != EWOULDBLOCK) != 0) {
155 				return (error);
156 			}
157 			break;
158 		}
159 	   }
160 	}
161 
162 	return (EWOULDBLOCK);
163 }
164 
165 /* Get operating mode of the chipset */
166 int
167 ppbus_get_mode(struct device * dev)
168 {
169 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
170 
171 	return (bus->sc_mode);
172 }
173 
174 /* Set the operating mode of the chipset, return 0 on success. */
175 int
176 ppbus_set_mode(struct device * dev, int mode, int options)
177 {
178 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
179 	int error = 0;
180 
181 	/* If no mode change, do nothing */
182 	if(bus->sc_mode == mode)
183 		return error;
184 
185 	/* Do necessary negotiations */
186 	if(bus->sc_use_ieee == PPBUS_ENABLE_IEEE) {
187 		/* Cannot negotiate standard mode */
188 		if(!(mode & (PPBUS_FAST | PPBUS_COMPATIBLE))) {
189  			error = ppbus_1284_negotiate(dev, mode, options);
190 		}
191 		/* Termination is unnecessary for standard<->fast */
192 		else if(!(bus->sc_mode & (PPBUS_FAST | PPBUS_COMPATIBLE))) {
193 			ppbus_1284_terminate(dev);
194 		}
195 	}
196 
197 	if(!error) {
198 		/* Set mode and update mode of ppbus to actual mode */
199 		error = bus->ppbus_setmode(device_parent(dev), mode);
200 		bus->sc_mode = bus->ppbus_getmode(device_parent(dev));
201 	}
202 
203 	/* Update host state if necessary */
204 	if(!(error) && (bus->sc_use_ieee == PPBUS_ENABLE_IEEE)) {
205 		switch(mode) {
206 		case PPBUS_COMPATIBLE:
207 		case PPBUS_FAST:
208 		case PPBUS_EPP:
209 		case PPBUS_ECP:
210 			ppbus_1284_set_state(dev, PPBUS_FORWARD_IDLE);
211 			break;
212 
213 		case PPBUS_NIBBLE:
214 		case PPBUS_PS2:
215 			ppbus_1284_set_state(dev, PPBUS_REVERSE_IDLE);
216 			break;
217 		}
218 	}
219 
220 	return error;
221 }
222 
223 /* Write charaters to the port */
224 int
225 ppbus_write(struct device * dev, char * buf, int len, int how, size_t * cnt)
226 {
227 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
228 
229 	if(bus->sc_use_ieee == PPBUS_ENABLE_IEEE) {
230 		if(bus->sc_1284_state != PPBUS_FORWARD_IDLE) {
231 			printf("%s(%s): bus not in forward idle mode.\n",
232 				__func__, dev->dv_xname);
233 			return ENODEV;
234 		}
235 	}
236 
237 	return (bus->ppbus_write(device_parent(&bus->sc_dev), buf, len, how, cnt));
238 }
239 
240 /* Read charaters from the port */
241 int
242 ppbus_read(struct device * dev, char * buf, int len, int how, size_t * cnt)
243 {
244 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
245 
246 	if(bus->sc_use_ieee == PPBUS_ENABLE_IEEE) {
247 		if(bus->sc_1284_state != PPBUS_REVERSE_IDLE) {
248 			printf("%s(%s): bus not in reverse idle mode.\n",
249 				__func__, dev->dv_xname);
250 			return ENODEV;
251 		}
252 	}
253 
254 	return (bus->ppbus_read(device_parent(dev), buf, len, how, cnt));
255 }
256 
257 /* Reset the EPP timeout bit in the status register */
258 int
259 ppbus_reset_epp_timeout(struct device * dev)
260 {
261 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
262 
263 	if(bus->sc_capabilities & PPBUS_HAS_EPP) {
264 		bus->ppbus_reset_epp_timeout(device_parent(dev));
265 		return 0;
266 	}
267 	else {
268 		return ENODEV;
269 	}
270 }
271 
272 /* Wait for the ECP FIFO to be empty */
273 int
274 ppbus_ecp_sync(struct device * dev)
275 {
276 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
277 
278 	if(bus->sc_capabilities & PPBUS_HAS_ECP) {
279 		bus->ppbus_ecp_sync(device_parent(dev));
280 		return 0;
281 	}
282 	else {
283 		return ENODEV;
284 	}
285 }
286 
287 /* Allocate DMA for use with ppbus */
288 int
289 ppbus_dma_malloc(struct device * dev, void ** buf, bus_addr_t * addr,
290 	bus_size_t size)
291 {
292 	struct ppbus_softc * ppbus = (struct ppbus_softc *) dev;
293 
294 	if(ppbus->sc_capabilities & PPBUS_HAS_DMA)
295 		return (ppbus->ppbus_dma_malloc(device_parent(dev), buf, addr,
296 			size));
297 	else
298 		return ENODEV;
299 }
300 
301 /* Free memory allocated with ppbus_dma_malloc() */
302 int
303 ppbus_dma_free(struct device * dev, void ** buf, bus_addr_t * addr,
304 	bus_size_t size)
305 {
306 	struct ppbus_softc * ppbus = (struct ppbus_softc *) dev;
307 
308 	if(ppbus->sc_capabilities & PPBUS_HAS_DMA) {
309 		ppbus->ppbus_dma_free(device_parent(dev), buf, addr, size);
310 		return 0;
311 	}
312 	else {
313 		return ENODEV;
314 	}
315 }
316 
317 /* Install a handler to be called by hardware interrupt handler */
318 int ppbus_add_handler(struct device * dev, void (*func)(void *), void *arg)
319 {
320 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
321 
322 	if(bus->sc_capabilities & PPBUS_HAS_INTR)
323 		return bus->ppbus_add_handler(device_parent(dev), func, arg);
324 	else
325 		return ENODEV;
326 }
327 
328 /* Remove a handler registered with ppbus_add_handler() */
329 int ppbus_remove_handler(struct device * dev, void (*func)(void *))
330 {
331 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
332 
333 	if(bus->sc_capabilities & PPBUS_HAS_INTR)
334 		return bus->ppbus_remove_handler(device_parent(dev), func);
335 	else
336 		return ENODEV;
337 }
338 
339 /*
340  * ppbus_get_status()
341  *
342  * Read the status register and update the status info
343  */
344 int
345 ppbus_get_status(struct device * dev, struct ppbus_status * status)
346 {
347 	register char r = status->status = ppbus_rstr(dev);
348 
349 	status->timeout	= r & TIMEOUT;
350 	status->error	= !(r & nFAULT);
351 	status->select	= r & SELECT;
352 	status->paper_end = r & PERROR;
353 	status->ack	= !(r & nACK);
354 	status->busy	= !(r & nBUSY);
355 
356 	return (0);
357 }
358 
359 /* Allocate the device to perform transfers */
360 int
361 ppbus_request_bus(struct device * dev, struct device * busdev, int how,
362 	unsigned int timeout)
363 {
364 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
365 	unsigned int counter = timeout;
366 	int priority = PPBUSPRI;
367 	int error;
368 
369 	if(how & PPBUS_INTR)
370 		priority |= PCATCH;
371 
372 	/* Loop until lock acquired (if PPBUS_WAIT) or an error occurs */
373 	for(;;) {
374 		if (mutex_tryenter(&(bus->sc_lock)))
375 			break;
376 
377 		if(how & PPBUS_WAIT) {
378 			error = ltsleep(bus, priority, __func__, hz/2, NULL);
379 			counter -= (hz/2);
380 			if(!(error))
381 				continue;
382 			else if(error != EWOULDBLOCK)
383 				goto end;
384 			if(counter == 0) {
385 				error = ETIMEDOUT;
386 				goto end;
387 			}
388 		}
389 		else {
390 			error = EWOULDBLOCK;
391 			goto end;
392 		}
393 	}
394 
395 	/* Set bus owner or return error if bus is taken */
396 	if(bus->ppbus_owner == NULL) {
397 		bus->ppbus_owner = busdev;
398 		error = 0;
399 	}
400 	else {
401 		error = EBUSY;
402 	}
403 
404 	/* Release lock */
405 	mutex_exit(&(bus->sc_lock));
406 
407 end:
408 	return error;
409 }
410 
411 /* Release the device allocated with ppbus_request_bus() */
412 int
413 ppbus_release_bus(struct device * dev, struct device * busdev, int how,
414 	unsigned int timeout)
415 {
416 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
417 	unsigned int counter = timeout;
418 	int priority = PPBUSPRI;
419 	int error;
420 
421 	if(how & PPBUS_INTR)
422 		priority |= PCATCH;
423 
424 	/* Loop until lock acquired (if PPBUS_WAIT) or an error occurs */
425 	for(;;) {
426 		if (mutex_tryenter(&(bus->sc_lock)))
427 			break;
428 
429 		if(how & PPBUS_WAIT) {
430 			error = ltsleep(bus, priority, __func__, hz/2, NULL);
431 			counter -= (hz/2);
432 			if(!(error))
433 				continue;
434 			else if(error != EWOULDBLOCK)
435 				goto end;
436 			if(counter == 0) {
437 				error = ETIMEDOUT;
438 				goto end;
439 			}
440 		}
441 		else {
442 			error = EWOULDBLOCK;
443 			goto end;
444 		}
445 	}
446 
447 	/* If the device is the owner, release bus */
448 	if(bus->ppbus_owner != busdev) {
449 		error = EINVAL;
450 	}
451 	else {
452 		bus->ppbus_owner = NULL;
453 		error = 0;
454 	}
455 
456 	/* Release lock */
457 	mutex_exit(&(bus->sc_lock));
458 
459 end:
460 	return error;
461 }
462 
463 
464 /* IEEE 1284-based probes */
465 
466 #ifndef DONTPROBE_1284
467 
468 static const char *pnp_tokens[] = {
469 	"PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
470 	"FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
471 
472 /* ??? */
473 #if 0
474 static char *pnp_classes[] = {
475 	"printer", "modem", "network device",
476 	"hard disk", "PCMCIA", "multimedia device",
477 	"floppy disk", "ports", "scanner",
478 	"digital camera", "unknown device", NULL };
479 #endif
480 
481 /*
482  * Search the first occurence of a token within a string
483  * XXX should use strxxx() calls
484  */
485 static char *
486 search_token(char *str, int slen, const char *token)
487 {
488 	const char *p;
489 	int tlen, i, j;
490 
491 #define UNKNOWN_LENGTH  -1
492 
493 	if (slen == UNKNOWN_LENGTH)
494 	       /* get string's length */
495 	       for (slen = 0, p = str; *p != '\0'; p++)
496 		       slen++;
497 
498        /* get token's length */
499        for (tlen = 0, p = token; *p != '\0'; p++)
500 	       tlen++;
501 
502        if (tlen == 0)
503 	       return (str);
504 
505        for (i = 0; i <= slen-tlen; i++) {
506 	       for (j = 0; j < tlen; j++)
507 		       if (str[i+j] != token[j])
508 			       break;
509 	       if (j == tlen)
510 		       return (&str[i]);
511        }
512 
513 	return (NULL);
514 }
515 
516 /* Stores the class ID of the peripherial in soft config data */
517 void
518 ppbus_pnp_detect(struct device * dev)
519 {
520 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
521 	int i;
522 	int error;
523 	size_t len = 0;
524 	size_t str_sz = 0;
525 	char * str = NULL;
526 	char * class = NULL;
527 	char * token;
528 
529 #ifdef PPBUS_VERBOSE
530 	printf("%s: Probing for PnP devices.\n", dev->dv_xname);
531 #endif
532 
533 	error = ppbus_1284_read_id(dev, PPBUS_NIBBLE, &str, &str_sz, &len);
534 	if(str_sz != len) {
535 #ifdef DEBUG_1284
536 		printf("%s(%s): device returned less characters than expected "
537 			"in device ID.\n", __func__, dev->dv_xname);
538 #endif
539 	}
540 	if(error) {
541 		printf("%s: Error getting device ID (errno = %d)\n",
542 			dev->dv_xname, error);
543 		goto end_detect;
544 	}
545 
546 #ifdef DEBUG_1284
547 	printf("%s: <PnP> %d characters: ", dev->dv_xname, len);
548 	for (i = 0; i < len; i++)
549 		printf("%c(0x%x) ", str[i], str[i]);
550 	printf("\n");
551 #endif
552 
553 	/* replace ';' characters by '\0' */
554 	for (i = 0; i < len; i++)
555 		if(str[i] == ';') str[i] = '\0';
556 		/* str[i] = (str[i] == ';') ? '\0' : str[i]; */
557 
558 	if ((token = search_token(str, len, "MFG")) != NULL ||
559 		(token = search_token(str, len, "MANUFACTURER")) != NULL)
560 		printf("%s: <%s", dev->dv_xname,
561 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
562 	else
563 		printf("%s: <unknown", dev->dv_xname);
564 
565 	if ((token = search_token(str, len, "MDL")) != NULL ||
566 		(token = search_token(str, len, "MODEL")) != NULL)
567 		printf(" %s",
568 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
569 
570 	if ((token = search_token(str, len, "REV")) != NULL)
571 		printf(".%s",
572 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
573 
574 	printf(">");
575 
576 	if ((token = search_token(str, len, "CLS")) != NULL) {
577 		class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
578 		printf(" %s", class);
579 	}
580 
581 	if ((token = search_token(str, len, "CMD")) != NULL ||
582 		(token = search_token(str, len, "COMMAND")) != NULL)
583 		printf(" %s",
584 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
585 
586 	printf("\n");
587 
588 	if (class) {
589 		/* identify class ident */
590 		for (i = 0; pnp_tokens[i] != NULL; i++) {
591 			if (search_token(class, len, pnp_tokens[i]) != NULL) {
592 				bus->sc_class_id = i;
593 				goto end_detect;
594 			}
595 		}
596 	}
597 	bus->sc_class_id = PPBUS_PNP_UNKNOWN;
598 
599 end_detect:
600 	if(str)
601 		free(str, M_DEVBUF);
602         return;
603 }
604 
605 /* Scan the ppbus for IEEE1284 compliant devices */
606 int
607 ppbus_scan_bus(struct device * dev)
608 {
609 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
610 	int error;
611 
612 	/* Try IEEE1284 modes, one device only (no IEEE1284.3 support) */
613 
614 	error = ppbus_1284_negotiate(dev, PPBUS_NIBBLE, 0);
615 	if (error && bus->sc_1284_state == PPBUS_ERROR
616 	    && bus->sc_1284_error == PPBUS_NOT_IEEE1284)
617 		return (error);
618 	ppbus_1284_terminate(dev);
619 
620 #if defined(PPBUS_VERBOSE) || defined(PPBUS_DEBUG)
621 	/* IEEE1284 supported, print info */
622 	printf("%s: IEEE1284 negotiation: modes %s",
623 	    dev->dv_xname, "NIBBLE");
624 
625 	error = ppbus_1284_negotiate(dev, PPBUS_PS2, 0);
626 	if (!error)
627 		printf("/PS2");
628 	ppbus_1284_terminate(dev);
629 
630 	error = ppbus_1284_negotiate(dev, PPBUS_ECP, 0);
631 	if (!error)
632 		printf("/ECP");
633 	ppbus_1284_terminate(dev);
634 
635 	error = ppbus_1284_negotiate(dev, PPBUS_ECP, PPBUS_USE_RLE);
636 	if (!error)
637 		printf("/ECP_RLE");
638 	ppbus_1284_terminate(dev);
639 
640 	error = ppbus_1284_negotiate(dev, PPBUS_EPP, 0);
641 	if (!error)
642 		printf("/EPP");
643 	ppbus_1284_terminate(dev);
644 
645 	printf("\n");
646 #endif /* PPBUS_VERBOSE || PPBUS_DEBUG */
647 
648 	return 0;
649 }
650 
651 #endif /* !DONTPROBE_1284 */
652 
653