xref: /dragonfly/sys/bus/ppbus/ppbconf.c (revision 984263bc)
1 /*-
2  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ppbus/ppbconf.c,v 1.17.2.1 2000/05/24 00:20:57 n_hibma Exp $
27  *
28  */
29 #include "opt_ppb_1284.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 
38 #include <dev/ppbus/ppbconf.h>
39 #include <dev/ppbus/ppb_1284.h>
40 
41 #include "ppbus_if.h"
42 
43 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
44 
45 MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
46 
47 
48 /*
49  * Device methods
50  */
51 
52 static void
53 ppbus_print_child(device_t bus, device_t dev)
54 {
55 	struct ppb_device *ppbdev;
56 
57 	bus_print_child_header(bus, dev);
58 
59 	ppbdev = (struct ppb_device *)device_get_ivars(dev);
60 
61 	if (ppbdev->flags != 0)
62 		printf(" flags 0x%x", ppbdev->flags);
63 
64 	printf(" on %s%d\n", device_get_name(bus), device_get_unit(bus));
65 
66 	return;
67 }
68 
69 static int
70 ppbus_probe(device_t dev)
71 {
72 	device_set_desc(dev, "Parallel port bus");
73 
74 	return (0);
75 }
76 
77 /*
78  * ppbus_add_child()
79  *
80  * Add a ppbus device, allocate/initialize the ivars
81  */
82 static device_t
83 ppbus_add_child(device_t dev, int order, const char *name, int unit)
84 {
85 	struct ppb_device *ppbdev;
86 	device_t child;
87 
88 	/* allocate ivars for the new ppbus child */
89 	ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV, M_NOWAIT);
90 	if (!ppbdev)
91 		return NULL;
92 	bzero(ppbdev, sizeof *ppbdev);
93 
94 	/* initialize the ivars */
95 	ppbdev->name = name;
96 
97 	/* add the device as a child to the ppbus bus with the allocated
98 	 * ivars */
99 	child = device_add_child_ordered(dev, order, name, unit);
100 	device_set_ivars(child, ppbdev);
101 
102 	return child;
103 }
104 
105 static int
106 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
107 {
108 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
109 
110 	switch (index) {
111 	case PPBUS_IVAR_MODE:
112 		/* XXX yet device mode = ppbus mode = chipset mode */
113 		*val = (u_long)ppb_get_mode(bus);
114 		ppbdev->mode = (u_short)*val;
115 		break;
116 	case PPBUS_IVAR_AVM:
117 		*val = (u_long)ppbdev->avm;
118 		break;
119 	case PPBUS_IVAR_IRQ:
120 		BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_IRQ, val);
121 		break;
122 	default:
123 		return (ENOENT);
124 	}
125 
126 	return (0);
127 }
128 
129 static int
130 ppbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
131 {
132 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
133 
134 	switch (index) {
135 	case PPBUS_IVAR_MODE:
136 		/* XXX yet device mode = ppbus mode = chipset mode */
137 		ppb_set_mode(bus,val);
138 		ppbdev->mode = ppb_get_mode(bus);
139 		break;
140 	default:
141 		return (ENOENT);
142   	}
143 
144 	return (0);
145   }
146 
147 #define PPB_PNP_PRINTER		0
148 #define PPB_PNP_MODEM		1
149 #define PPB_PNP_NET		2
150 #define PPB_PNP_HDC		3
151 #define PPB_PNP_PCMCIA		4
152 #define PPB_PNP_MEDIA		5
153 #define PPB_PNP_FDC		6
154 #define PPB_PNP_PORTS		7
155 #define PPB_PNP_SCANNER		8
156 #define PPB_PNP_DIGICAM		9
157 
158 #ifndef DONTPROBE_1284
159 
160 static char *pnp_tokens[] = {
161 	"PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
162 	"FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
163 
164 #if 0
165 static char *pnp_classes[] = {
166 	"printer", "modem", "network device",
167 	"hard disk", "PCMCIA", "multimedia device",
168 	"floppy disk", "ports", "scanner",
169 	"digital camera", "unknown device", NULL };
170 #endif
171 
172 /*
173  * search_token()
174  *
175  * Search the first occurence of a token within a string
176  *
177  * XXX should use strxxx() calls
178  */
179 static char *
180 search_token(char *str, int slen, char *token)
181 {
182 	char *p;
183 	int tlen, i, j;
184 
185 #define UNKNOWN_LENGTH	-1
186 
187 	if (slen == UNKNOWN_LENGTH)
188 		/* get string's length */
189 		for (slen = 0, p = str; *p != '\0'; p++)
190 			slen ++;
191 
192 	/* get token's length */
193 	for (tlen = 0, p = token; *p != '\0'; p++)
194 		tlen ++;
195 
196 	if (tlen == 0)
197 		return (str);
198 
199 	for (i = 0; i <= slen-tlen; i++) {
200 		for (j = 0; j < tlen; j++)
201 			if (str[i+j] != token[j])
202 				break;
203 		if (j == tlen)
204 			return (&str[i]);
205 	}
206 
207 	return (NULL);
208 }
209 
210 /*
211  * ppb_pnp_detect()
212  *
213  * Returns the class id. of the peripherial, -1 otherwise
214  */
215 static int
216 ppb_pnp_detect(device_t bus)
217 {
218 	char *token, *class = 0;
219 	int i, len, error;
220 	int class_id = -1;
221 	char str[PPB_PnP_STRING_SIZE+1];
222 	int unit = device_get_unit(bus);
223 
224 	printf("Probing for PnP devices on ppbus%d:\n", unit);
225 
226 	if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
227 					PPB_PnP_STRING_SIZE, &len)))
228 		goto end_detect;
229 
230 #ifdef DEBUG_1284
231 	printf("ppb: <PnP> %d characters: ", len);
232 	for (i = 0; i < len; i++)
233 		printf("%c(0x%x) ", str[i], str[i]);
234 	printf("\n");
235 #endif
236 
237 	/* replace ';' characters by '\0' */
238 	for (i = 0; i < len; i++)
239 		str[i] = (str[i] == ';') ? '\0' : str[i];
240 
241 	if ((token = search_token(str, len, "MFG")) != NULL ||
242 		(token = search_token(str, len, "MANUFACTURER")) != NULL)
243 		printf("ppbus%d: <%s", unit,
244 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
245 	else
246 		printf("ppbus%d: <unknown", unit);
247 
248 	if ((token = search_token(str, len, "MDL")) != NULL ||
249 		(token = search_token(str, len, "MODEL")) != NULL)
250 		printf(" %s",
251 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
252 	else
253 		printf(" unknown");
254 
255 	if ((token = search_token(str, len, "VER")) != NULL)
256 		printf("/%s",
257 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
258 
259 	if ((token = search_token(str, len, "REV")) != NULL)
260 		printf(".%s",
261 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
262 
263 	printf(">");
264 
265 	if ((token = search_token(str, len, "CLS")) != NULL) {
266 		class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
267 		printf(" %s", class);
268 	}
269 
270 	if ((token = search_token(str, len, "CMD")) != NULL ||
271 		(token = search_token(str, len, "COMMAND")) != NULL)
272 		printf(" %s",
273 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
274 
275 	printf("\n");
276 
277 	if (class)
278 		/* identify class ident */
279 		for (i = 0; pnp_tokens[i] != NULL; i++) {
280 			if (search_token(class, len, pnp_tokens[i]) != NULL) {
281 				class_id = i;
282 				goto end_detect;
283 			}
284 		}
285 
286 	class_id = PPB_PnP_UNKNOWN;
287 
288 end_detect:
289 	return (class_id);
290 }
291 
292 /*
293  * ppb_scan_bus()
294  *
295  * Scan the ppbus for IEEE1284 compliant devices
296  */
297 static int
298 ppb_scan_bus(device_t bus)
299 {
300 	struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
301 	int error = 0;
302 	int unit = device_get_unit(bus);
303 
304 	/* try all IEEE1284 modes, for one device only
305 	 *
306 	 * XXX We should implement the IEEE1284.3 standard to detect
307 	 * daisy chained devices
308 	 */
309 
310 	error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
311 
312 	if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
313 		goto end_scan;
314 
315 	ppb_1284_terminate(bus);
316 
317 	printf("ppbus%d: IEEE1284 device found ", unit);
318 
319 	if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
320 		printf("/NIBBLE");
321 		ppb_1284_terminate(bus);
322 	}
323 
324 	if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
325 		printf("/PS2");
326 		ppb_1284_terminate(bus);
327 	}
328 
329 	if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
330 		printf("/ECP");
331 		ppb_1284_terminate(bus);
332 	}
333 
334 	if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
335 		printf("/ECP_RLE");
336 		ppb_1284_terminate(bus);
337 	}
338 
339 	if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
340 		printf("/EPP");
341 		ppb_1284_terminate(bus);
342 	}
343 
344 	/* try more IEEE1284 modes */
345 	if (bootverbose) {
346 		if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
347 				PPB_REQUEST_ID))) {
348 			printf("/NIBBLE_ID");
349 			ppb_1284_terminate(bus);
350 		}
351 
352 		if (!(error = ppb_1284_negociate(bus, PPB_PS2,
353 				PPB_REQUEST_ID))) {
354 			printf("/PS2_ID");
355 			ppb_1284_terminate(bus);
356 		}
357 
358 		if (!(error = ppb_1284_negociate(bus, PPB_ECP,
359 				PPB_REQUEST_ID))) {
360 			printf("/ECP_ID");
361 			ppb_1284_terminate(bus);
362 		}
363 
364 		if (!(error = ppb_1284_negociate(bus, PPB_ECP,
365 				PPB_REQUEST_ID | PPB_USE_RLE))) {
366 			printf("/ECP_RLE_ID");
367 			ppb_1284_terminate(bus);
368 		}
369 
370 		if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
371 				PPB_EXTENSIBILITY_LINK))) {
372 			printf("/Extensibility Link");
373 			ppb_1284_terminate(bus);
374 		}
375 	}
376 
377 	printf("\n");
378 
379 	/* detect PnP devices */
380 	ppb->class_id = ppb_pnp_detect(bus);
381 
382 	return (0);
383 
384 end_scan:
385 	return (error);
386 }
387 
388 #endif /* !DONTPROBE_1284 */
389 
390 static int
391 ppbus_attach(device_t dev)
392 {
393 
394 	/* Locate our children */
395 	bus_generic_probe(dev);
396 
397 #ifndef DONTPROBE_1284
398 	/* detect IEEE1284 compliant devices */
399 	ppb_scan_bus(dev);
400 #endif /* !DONTPROBE_1284 */
401 
402 	/* launch attachement of the added children */
403 	bus_generic_attach(dev);
404 
405 	return 0;
406 }
407 
408 static int
409 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
410 			void (*ihand)(void *), void *arg, void **cookiep)
411 {
412 	int error;
413 	struct ppb_data *ppb = DEVTOSOFTC(bus);
414 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
415 
416 	/* a device driver must own the bus to register an interrupt */
417 	if (ppb->ppb_owner != child)
418 		return (EINVAL);
419 
420 	if ((error = BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
421 					ihand, arg, cookiep)))
422 		return (error);
423 
424 	/* store the resource and the cookie for eventually forcing
425 	 * handler unregistration
426 	 */
427 	ppbdev->intr_cookie = *cookiep;
428 	ppbdev->intr_resource = r;
429 
430 	return (0);
431 }
432 
433 static int
434 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
435 {
436 	struct ppb_data *ppb = DEVTOSOFTC(bus);
437 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
438 
439 	/* a device driver must own the bus to unregister an interrupt */
440 	if ((ppb->ppb_owner != child) || (ppbdev->intr_cookie != ih) ||
441 			(ppbdev->intr_resource != r))
442 		return (EINVAL);
443 
444 	ppbdev->intr_cookie = 0;
445 	ppbdev->intr_resource = 0;
446 
447 	/* pass unregistration to the upper layer */
448 	return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, ih));
449 }
450 
451 /*
452  * ppb_request_bus()
453  *
454  * Allocate the device to perform transfers.
455  *
456  * how	: PPB_WAIT or PPB_DONTWAIT
457  */
458 int
459 ppb_request_bus(device_t bus, device_t dev, int how)
460 {
461 	int s, error = 0;
462 	struct ppb_data *ppb = DEVTOSOFTC(bus);
463 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
464 
465 	while (!error) {
466 		s = splhigh();
467 		if (ppb->ppb_owner) {
468 			splx(s);
469 
470 			switch (how) {
471 			case (PPB_WAIT | PPB_INTR):
472 				error = tsleep(ppb, PPBPRI|PCATCH, "ppbreq", 0);
473 				break;
474 
475 			case (PPB_WAIT | PPB_NOINTR):
476 				error = tsleep(ppb, PPBPRI, "ppbreq", 0);
477 				break;
478 
479 			default:
480 				return (EWOULDBLOCK);
481 				break;
482 			}
483 
484 		} else {
485 			ppb->ppb_owner = dev;
486 
487 			/* restore the context of the device
488 			 * The first time, ctx.valid is certainly false
489 			 * then do not change anything. This is usefull for
490 			 * drivers that do not set there operating mode
491 			 * during attachement
492 			 */
493 			if (ppbdev->ctx.valid)
494 				ppb_set_mode(bus, ppbdev->ctx.mode);
495 
496 			splx(s);
497 			return (0);
498 		}
499 	}
500 
501 	return (error);
502 }
503 
504 /*
505  * ppb_release_bus()
506  *
507  * Release the device allocated with ppb_request_bus()
508  */
509 int
510 ppb_release_bus(device_t bus, device_t dev)
511 {
512 	int s, error;
513 	struct ppb_data *ppb = DEVTOSOFTC(bus);
514 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
515 
516 	if (ppbdev->intr_resource != 0)
517 		/* force interrupt handler unregistration when the ppbus is released */
518 		if ((error = BUS_TEARDOWN_INTR(bus, dev, ppbdev->intr_resource,
519 					       ppbdev->intr_cookie)))
520 			return (error);
521 
522 	s = splhigh();
523 	if (ppb->ppb_owner != dev) {
524 		splx(s);
525 		return (EACCES);
526 	}
527 
528 	ppb->ppb_owner = 0;
529 	splx(s);
530 
531 	/* save the context of the device */
532 	ppbdev->ctx.mode = ppb_get_mode(bus);
533 
534 	/* ok, now the context of the device is valid */
535 	ppbdev->ctx.valid = 1;
536 
537 	/* wakeup waiting processes */
538 	wakeup(ppb);
539 
540 	return (0);
541 }
542 
543 static devclass_t ppbus_devclass;
544 
545 static device_method_t ppbus_methods[] = {
546         /* device interface */
547 	DEVMETHOD(device_probe,         ppbus_probe),
548 	DEVMETHOD(device_attach,        ppbus_attach),
549 
550         /* bus interface */
551 	DEVMETHOD(bus_add_child,	ppbus_add_child),
552 	DEVMETHOD(bus_print_child,	ppbus_print_child),
553 	DEVMETHOD(bus_read_ivar,        ppbus_read_ivar),
554 	DEVMETHOD(bus_write_ivar,       ppbus_write_ivar),
555 	DEVMETHOD(bus_setup_intr,	ppbus_setup_intr),
556 	DEVMETHOD(bus_teardown_intr,	ppbus_teardown_intr),
557 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
558 
559         { 0, 0 }
560 };
561 
562 static driver_t ppbus_driver = {
563         "ppbus",
564         ppbus_methods,
565         sizeof(struct ppb_data),
566 };
567 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);
568