xref: /freebsd/sys/dev/ppbus/ppbconf.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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$
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 static 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,
90 		M_NOWAIT | M_ZERO);
91 	if (!ppbdev)
92 		return NULL;
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 static char *
178 search_token(char *str, int slen, char *token)
179 {
180 	int tlen, i;
181 
182 #define UNKNOWN_LENGTH	-1
183 
184 	if (slen == UNKNOWN_LENGTH)
185 		/* get string's length */
186 		slen = strlen(str);
187 
188 	/* get token's length */
189 	tlen = strlen(token);
190 	if (tlen == 0)
191 		return (str);
192 
193 	for (i = 0; i <= slen-tlen; i++) {
194 		if (strncmp(str + i, token, tlen) == 0)
195 			return (&str[i]);
196 	}
197 
198 	return (NULL);
199 }
200 
201 /*
202  * ppb_pnp_detect()
203  *
204  * Returns the class id. of the peripherial, -1 otherwise
205  */
206 static int
207 ppb_pnp_detect(device_t bus)
208 {
209 	char *token, *class = 0;
210 	int i, len, error;
211 	int class_id = -1;
212 	char str[PPB_PnP_STRING_SIZE+1];
213 	int unit = device_get_unit(bus);
214 
215 	printf("Probing for PnP devices on ppbus%d:\n", unit);
216 
217 	if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
218 					PPB_PnP_STRING_SIZE, &len)))
219 		goto end_detect;
220 
221 #ifdef DEBUG_1284
222 	printf("ppb: <PnP> %d characters: ", len);
223 	for (i = 0; i < len; i++)
224 		printf("%c(0x%x) ", str[i], str[i]);
225 	printf("\n");
226 #endif
227 
228 	/* replace ';' characters by '\0' */
229 	for (i = 0; i < len; i++)
230 		str[i] = (str[i] == ';') ? '\0' : str[i];
231 
232 	if ((token = search_token(str, len, "MFG")) != NULL ||
233 		(token = search_token(str, len, "MANUFACTURER")) != NULL)
234 		printf("ppbus%d: <%s", unit,
235 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
236 	else
237 		printf("ppbus%d: <unknown", unit);
238 
239 	if ((token = search_token(str, len, "MDL")) != NULL ||
240 		(token = search_token(str, len, "MODEL")) != NULL)
241 		printf(" %s",
242 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
243 	else
244 		printf(" unknown");
245 
246 	if ((token = search_token(str, len, "VER")) != NULL)
247 		printf("/%s",
248 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
249 
250 	if ((token = search_token(str, len, "REV")) != NULL)
251 		printf(".%s",
252 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
253 
254 	printf(">");
255 
256 	if ((token = search_token(str, len, "CLS")) != NULL) {
257 		class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
258 		printf(" %s", class);
259 	}
260 
261 	if ((token = search_token(str, len, "CMD")) != NULL ||
262 		(token = search_token(str, len, "COMMAND")) != NULL)
263 		printf(" %s",
264 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
265 
266 	printf("\n");
267 
268 	if (class)
269 		/* identify class ident */
270 		for (i = 0; pnp_tokens[i] != NULL; i++) {
271 			if (search_token(class, len, pnp_tokens[i]) != NULL) {
272 				class_id = i;
273 				goto end_detect;
274 			}
275 		}
276 
277 	class_id = PPB_PnP_UNKNOWN;
278 
279 end_detect:
280 	return (class_id);
281 }
282 
283 /*
284  * ppb_scan_bus()
285  *
286  * Scan the ppbus for IEEE1284 compliant devices
287  */
288 static int
289 ppb_scan_bus(device_t bus)
290 {
291 	struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
292 	int error = 0;
293 	int unit = device_get_unit(bus);
294 
295 	/* try all IEEE1284 modes, for one device only
296 	 *
297 	 * XXX We should implement the IEEE1284.3 standard to detect
298 	 * daisy chained devices
299 	 */
300 
301 	error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
302 
303 	if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
304 		goto end_scan;
305 
306 	ppb_1284_terminate(bus);
307 
308 	printf("ppbus%d: IEEE1284 device found ", unit);
309 
310 	if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
311 		printf("/NIBBLE");
312 		ppb_1284_terminate(bus);
313 	}
314 
315 	if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
316 		printf("/PS2");
317 		ppb_1284_terminate(bus);
318 	}
319 
320 	if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
321 		printf("/ECP");
322 		ppb_1284_terminate(bus);
323 	}
324 
325 	if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
326 		printf("/ECP_RLE");
327 		ppb_1284_terminate(bus);
328 	}
329 
330 	if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
331 		printf("/EPP");
332 		ppb_1284_terminate(bus);
333 	}
334 
335 	/* try more IEEE1284 modes */
336 	if (bootverbose) {
337 		if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
338 				PPB_REQUEST_ID))) {
339 			printf("/NIBBLE_ID");
340 			ppb_1284_terminate(bus);
341 		}
342 
343 		if (!(error = ppb_1284_negociate(bus, PPB_PS2,
344 				PPB_REQUEST_ID))) {
345 			printf("/PS2_ID");
346 			ppb_1284_terminate(bus);
347 		}
348 
349 		if (!(error = ppb_1284_negociate(bus, PPB_ECP,
350 				PPB_REQUEST_ID))) {
351 			printf("/ECP_ID");
352 			ppb_1284_terminate(bus);
353 		}
354 
355 		if (!(error = ppb_1284_negociate(bus, PPB_ECP,
356 				PPB_REQUEST_ID | PPB_USE_RLE))) {
357 			printf("/ECP_RLE_ID");
358 			ppb_1284_terminate(bus);
359 		}
360 
361 		if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
362 				PPB_EXTENSIBILITY_LINK))) {
363 			printf("/Extensibility Link");
364 			ppb_1284_terminate(bus);
365 		}
366 	}
367 
368 	printf("\n");
369 
370 	/* detect PnP devices */
371 	ppb->class_id = ppb_pnp_detect(bus);
372 
373 	return (0);
374 
375 end_scan:
376 	return (error);
377 }
378 
379 #endif /* !DONTPROBE_1284 */
380 
381 static int
382 ppbus_attach(device_t dev)
383 {
384 
385 	/* Locate our children */
386 	bus_generic_probe(dev);
387 
388 #ifndef DONTPROBE_1284
389 	/* detect IEEE1284 compliant devices */
390 	ppb_scan_bus(dev);
391 #endif /* !DONTPROBE_1284 */
392 
393 	/* launch attachement of the added children */
394 	bus_generic_attach(dev);
395 
396 	return 0;
397 }
398 
399 static int
400 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
401 			void (*ihand)(void *), void *arg, void **cookiep)
402 {
403 	int error;
404 	struct ppb_data *ppb = DEVTOSOFTC(bus);
405 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
406 
407 	/* a device driver must own the bus to register an interrupt */
408 	if (ppb->ppb_owner != child)
409 		return (EINVAL);
410 
411 	if ((error = BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
412 					ihand, arg, cookiep)))
413 		return (error);
414 
415 	/* store the resource and the cookie for eventually forcing
416 	 * handler unregistration
417 	 */
418 	ppbdev->intr_cookie = *cookiep;
419 	ppbdev->intr_resource = r;
420 
421 	return (0);
422 }
423 
424 static int
425 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
426 {
427 	struct ppb_data *ppb = DEVTOSOFTC(bus);
428 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
429 
430 	/* a device driver must own the bus to unregister an interrupt */
431 	if ((ppb->ppb_owner != child) || (ppbdev->intr_cookie != ih) ||
432 			(ppbdev->intr_resource != r))
433 		return (EINVAL);
434 
435 	ppbdev->intr_cookie = 0;
436 	ppbdev->intr_resource = 0;
437 
438 	/* pass unregistration to the upper layer */
439 	return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, ih));
440 }
441 
442 /*
443  * ppb_request_bus()
444  *
445  * Allocate the device to perform transfers.
446  *
447  * how	: PPB_WAIT or PPB_DONTWAIT
448  */
449 int
450 ppb_request_bus(device_t bus, device_t dev, int how)
451 {
452 	int s, error = 0;
453 	struct ppb_data *ppb = DEVTOSOFTC(bus);
454 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
455 
456 	while (!error) {
457 		s = splhigh();
458 		if (ppb->ppb_owner) {
459 			splx(s);
460 
461 			switch (how) {
462 			case (PPB_WAIT | PPB_INTR):
463 				error = tsleep(ppb, PPBPRI|PCATCH, "ppbreq", 0);
464 				break;
465 
466 			case (PPB_WAIT | PPB_NOINTR):
467 				error = tsleep(ppb, PPBPRI, "ppbreq", 0);
468 				break;
469 
470 			default:
471 				return (EWOULDBLOCK);
472 				break;
473 			}
474 
475 		} else {
476 			ppb->ppb_owner = dev;
477 
478 			/* restore the context of the device
479 			 * The first time, ctx.valid is certainly false
480 			 * then do not change anything. This is usefull for
481 			 * drivers that do not set there operating mode
482 			 * during attachement
483 			 */
484 			if (ppbdev->ctx.valid)
485 				ppb_set_mode(bus, ppbdev->ctx.mode);
486 
487 			splx(s);
488 			return (0);
489 		}
490 	}
491 
492 	return (error);
493 }
494 
495 /*
496  * ppb_release_bus()
497  *
498  * Release the device allocated with ppb_request_bus()
499  */
500 int
501 ppb_release_bus(device_t bus, device_t dev)
502 {
503 	int s, error;
504 	struct ppb_data *ppb = DEVTOSOFTC(bus);
505 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
506 
507 	if (ppbdev->intr_resource != 0)
508 		/* force interrupt handler unregistration when the ppbus is released */
509 		if ((error = BUS_TEARDOWN_INTR(bus, dev, ppbdev->intr_resource,
510 					       ppbdev->intr_cookie)))
511 			return (error);
512 
513 	s = splhigh();
514 	if (ppb->ppb_owner != dev) {
515 		splx(s);
516 		return (EACCES);
517 	}
518 
519 	ppb->ppb_owner = 0;
520 	splx(s);
521 
522 	/* save the context of the device */
523 	ppbdev->ctx.mode = ppb_get_mode(bus);
524 
525 	/* ok, now the context of the device is valid */
526 	ppbdev->ctx.valid = 1;
527 
528 	/* wakeup waiting processes */
529 	wakeup(ppb);
530 
531 	return (0);
532 }
533 
534 static devclass_t ppbus_devclass;
535 
536 static device_method_t ppbus_methods[] = {
537         /* device interface */
538 	DEVMETHOD(device_probe,         ppbus_probe),
539 	DEVMETHOD(device_attach,        ppbus_attach),
540 
541         /* bus interface */
542 	DEVMETHOD(bus_add_child,	ppbus_add_child),
543 	DEVMETHOD(bus_print_child,	ppbus_print_child),
544 	DEVMETHOD(bus_read_ivar,        ppbus_read_ivar),
545 	DEVMETHOD(bus_write_ivar,       ppbus_write_ivar),
546 	DEVMETHOD(bus_setup_intr,	ppbus_setup_intr),
547 	DEVMETHOD(bus_teardown_intr,	ppbus_teardown_intr),
548 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
549 
550         { 0, 0 }
551 };
552 
553 static driver_t ppbus_driver = {
554         "ppbus",
555         ppbus_methods,
556         sizeof(struct ppb_data),
557 };
558 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);
559