xref: /dragonfly/sys/bus/ppbus/ppbconf.c (revision f746689a)
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  * $DragonFly: src/sys/bus/ppbus/ppbconf.c,v 1.12 2008/05/18 03:02:53 pavalos Exp $
28  *
29  */
30 #include "opt_ppb_1284.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <sys/thread2.h>
39 
40 #include "ppbconf.h"
41 #include "ppb_1284.h"
42 
43 #include "ppbus_if.h"
44 
45 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
46 
47 MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
48 
49 
50 /*
51  * Device methods
52  */
53 
54 static void
55 ppbus_print_child(device_t bus, device_t dev)
56 {
57 	struct ppb_device *ppbdev;
58 
59 	bus_print_child_header(bus, dev);
60 
61 	ppbdev = (struct ppb_device *)device_get_ivars(dev);
62 
63 	if (ppbdev->flags != 0)
64 		kprintf(" flags 0x%x", ppbdev->flags);
65 
66 	kprintf(" on %s%d\n", device_get_name(bus), device_get_unit(bus));
67 
68 	return;
69 }
70 
71 static int
72 ppbus_probe(device_t dev)
73 {
74 	device_set_desc(dev, "Parallel port bus");
75 
76 	return (0);
77 }
78 
79 /*
80  * ppbus_add_child()
81  *
82  * Add a ppbus device, allocate/initialize the ivars
83  */
84 static device_t
85 ppbus_add_child(device_t bus, device_t parent, int order, const char *name, int unit)
86 {
87 	struct ppb_device *ppbdev;
88 	device_t child;
89 
90 	/* allocate ivars for the new ppbus child */
91 	ppbdev = kmalloc(sizeof(struct ppb_device), M_PPBUSDEV, M_WAITOK | M_ZERO);
92 
93 	/* initialize the ivars */
94 	ppbdev->name = name;
95 
96 	/* add the device as a child to the ppbus bus with the allocated
97 	 * ivars */
98 	child = device_add_child_ordered(parent, order, name, unit);
99 	device_set_ivars(child, ppbdev);
100 
101 	return child;
102 }
103 
104 static int
105 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
106 {
107 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
108 
109 	switch (index) {
110 	case PPBUS_IVAR_MODE:
111 		/* XXX yet device mode = ppbus mode = chipset mode */
112 		*val = (u_long)ppb_get_mode(bus);
113 		ppbdev->mode = (u_short)*val;
114 		break;
115 	case PPBUS_IVAR_AVM:
116 		*val = (u_long)ppbdev->avm;
117 		break;
118 	case PPBUS_IVAR_IRQ:
119 		BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_IRQ, val);
120 		break;
121 	default:
122 		return (ENOENT);
123 	}
124 
125 	return (0);
126 }
127 
128 static int
129 ppbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
130 {
131 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
132 
133 	switch (index) {
134 	case PPBUS_IVAR_MODE:
135 		/* XXX yet device mode = ppbus mode = chipset mode */
136 		ppb_set_mode(bus,val);
137 		ppbdev->mode = ppb_get_mode(bus);
138 		break;
139 	default:
140 		return (ENOENT);
141   	}
142 
143 	return (0);
144   }
145 
146 #define PPB_PNP_PRINTER		0
147 #define PPB_PNP_MODEM		1
148 #define PPB_PNP_NET		2
149 #define PPB_PNP_HDC		3
150 #define PPB_PNP_PCMCIA		4
151 #define PPB_PNP_MEDIA		5
152 #define PPB_PNP_FDC		6
153 #define PPB_PNP_PORTS		7
154 #define PPB_PNP_SCANNER		8
155 #define PPB_PNP_DIGICAM		9
156 
157 #ifndef DONTPROBE_1284
158 
159 static char *pnp_tokens[] = {
160 	"PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
161 	"FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
162 
163 #if 0
164 static char *pnp_classes[] = {
165 	"printer", "modem", "network device",
166 	"hard disk", "PCMCIA", "multimedia device",
167 	"floppy disk", "ports", "scanner",
168 	"digital camera", "unknown device", NULL };
169 #endif
170 
171 /*
172  * search_token()
173  *
174  * Search the first occurence of a token within a string
175  *
176  * XXX should use strxxx() calls
177  */
178 static char *
179 search_token(char *str, int slen, char *token)
180 {
181 	char *p;
182 	int tlen, i, j;
183 
184 #define UNKNOWN_LENGTH	-1
185 
186 	if (slen == UNKNOWN_LENGTH)
187 		/* get string's length */
188 		for (slen = 0, p = str; *p != '\0'; p++)
189 			slen ++;
190 
191 	/* get token's length */
192 	for (tlen = 0, p = token; *p != '\0'; p++)
193 		tlen ++;
194 
195 	if (tlen == 0)
196 		return (str);
197 
198 	for (i = 0; i <= slen-tlen; i++) {
199 		for (j = 0; j < tlen; j++)
200 			if (str[i+j] != token[j])
201 				break;
202 		if (j == tlen)
203 			return (&str[i]);
204 	}
205 
206 	return (NULL);
207 }
208 
209 /*
210  * ppb_pnp_detect()
211  *
212  * Returns the class id. of the peripherial, -1 otherwise
213  */
214 static int
215 ppb_pnp_detect(device_t bus)
216 {
217 	char *token, *class = 0;
218 	int i, len, error;
219 	int class_id = -1;
220 	char str[PPB_PnP_STRING_SIZE+1];
221 	int unit = device_get_unit(bus);
222 
223 	kprintf("Probing for PnP devices on ppbus%d:\n", unit);
224 
225 	if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
226 					PPB_PnP_STRING_SIZE, &len)))
227 		goto end_detect;
228 
229 #ifdef DEBUG_1284
230 	kprintf("ppb: <PnP> %d characters: ", len);
231 	for (i = 0; i < len; i++)
232 		kprintf("%c(0x%x) ", str[i], str[i]);
233 	kprintf("\n");
234 #endif
235 
236 	/* replace ';' characters by '\0' */
237 	for (i = 0; i < len; i++)
238 		str[i] = (str[i] == ';') ? '\0' : str[i];
239 
240 	if ((token = search_token(str, len, "MFG")) != NULL ||
241 		(token = search_token(str, len, "MANUFACTURER")) != NULL)
242 		kprintf("ppbus%d: <%s", unit,
243 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
244 	else
245 		kprintf("ppbus%d: <unknown", unit);
246 
247 	if ((token = search_token(str, len, "MDL")) != NULL ||
248 		(token = search_token(str, len, "MODEL")) != NULL)
249 		kprintf(" %s",
250 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
251 	else
252 		kprintf(" unknown");
253 
254 	if ((token = search_token(str, len, "VER")) != NULL)
255 		kprintf("/%s",
256 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
257 
258 	if ((token = search_token(str, len, "REV")) != NULL)
259 		kprintf(".%s",
260 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
261 
262 	kprintf(">");
263 
264 	if ((token = search_token(str, len, "CLS")) != NULL) {
265 		class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
266 		kprintf(" %s", class);
267 	}
268 
269 	if ((token = search_token(str, len, "CMD")) != NULL ||
270 		(token = search_token(str, len, "COMMAND")) != NULL)
271 		kprintf(" %s",
272 			search_token(token, UNKNOWN_LENGTH, ":") + 1);
273 
274 	kprintf("\n");
275 
276 	if (class)
277 		/* identify class ident */
278 		for (i = 0; pnp_tokens[i] != NULL; i++) {
279 			if (search_token(class, len, pnp_tokens[i]) != NULL) {
280 				class_id = i;
281 				goto end_detect;
282 			}
283 		}
284 
285 	class_id = PPB_PnP_UNKNOWN;
286 
287 end_detect:
288 	return (class_id);
289 }
290 
291 /*
292  * ppb_scan_bus()
293  *
294  * Scan the ppbus for IEEE1284 compliant devices
295  */
296 static int
297 ppb_scan_bus(device_t bus)
298 {
299 	struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
300 	int error = 0;
301 	int unit = device_get_unit(bus);
302 
303 	/* try all IEEE1284 modes, for one device only
304 	 *
305 	 * XXX We should implement the IEEE1284.3 standard to detect
306 	 * daisy chained devices
307 	 */
308 
309 	error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
310 
311 	if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
312 		goto end_scan;
313 
314 	ppb_1284_terminate(bus);
315 
316 	kprintf("ppbus%d: IEEE1284 device found ", unit);
317 
318 	if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
319 		kprintf("/NIBBLE");
320 		ppb_1284_terminate(bus);
321 	}
322 
323 	if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
324 		kprintf("/PS2");
325 		ppb_1284_terminate(bus);
326 	}
327 
328 	if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
329 		kprintf("/ECP");
330 		ppb_1284_terminate(bus);
331 	}
332 
333 	if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
334 		kprintf("/ECP_RLE");
335 		ppb_1284_terminate(bus);
336 	}
337 
338 	if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
339 		kprintf("/EPP");
340 		ppb_1284_terminate(bus);
341 	}
342 
343 	/* try more IEEE1284 modes */
344 	if (bootverbose) {
345 		if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
346 				PPB_REQUEST_ID))) {
347 			kprintf("/NIBBLE_ID");
348 			ppb_1284_terminate(bus);
349 		}
350 
351 		if (!(error = ppb_1284_negociate(bus, PPB_PS2,
352 				PPB_REQUEST_ID))) {
353 			kprintf("/PS2_ID");
354 			ppb_1284_terminate(bus);
355 		}
356 
357 		if (!(error = ppb_1284_negociate(bus, PPB_ECP,
358 				PPB_REQUEST_ID))) {
359 			kprintf("/ECP_ID");
360 			ppb_1284_terminate(bus);
361 		}
362 
363 		if (!(error = ppb_1284_negociate(bus, PPB_ECP,
364 				PPB_REQUEST_ID | PPB_USE_RLE))) {
365 			kprintf("/ECP_RLE_ID");
366 			ppb_1284_terminate(bus);
367 		}
368 
369 		if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
370 				PPB_EXTENSIBILITY_LINK))) {
371 			kprintf("/Extensibility Link");
372 			ppb_1284_terminate(bus);
373 		}
374 	}
375 
376 	kprintf("\n");
377 
378 	/* detect PnP devices */
379 	ppb->class_id = ppb_pnp_detect(bus);
380 
381 	return (0);
382 
383 end_scan:
384 	return (error);
385 }
386 
387 #endif /* !DONTPROBE_1284 */
388 
389 static int
390 ppbus_attach(device_t dev)
391 {
392 
393 	/* Locate our children */
394 	bus_generic_probe(dev);
395 
396 #ifndef DONTPROBE_1284
397 	/* detect IEEE1284 compliant devices */
398 	ppb_scan_bus(dev);
399 #endif /* !DONTPROBE_1284 */
400 
401 	/* launch attachement of the added children */
402 	bus_generic_attach(dev);
403 
404 	return 0;
405 }
406 
407 static int
408 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
409 			void (*ihand)(void *), void *arg,
410 			void **cookiep, lwkt_serialize_t serializer)
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, serializer)))
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 = NULL;
445 	ppbdev->intr_resource = NULL;
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 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 		crit_enter();
467 		if (ppb->ppb_owner) {
468 			crit_exit();
469 			switch (how) {
470 			case (PPB_WAIT | PPB_INTR):
471 				error = tsleep(ppb, PCATCH, "ppbreq", 0);
472 				break;
473 
474 			case (PPB_WAIT | PPB_NOINTR):
475 				error = tsleep(ppb, 0, "ppbreq", 0);
476 				break;
477 
478 			default:
479 				return (EWOULDBLOCK);
480 				break;
481 			}
482 
483 		} else {
484 			ppb->ppb_owner = dev;
485 
486 			/* restore the context of the device
487 			 * The first time, ctx.valid is certainly false
488 			 * then do not change anything. This is useful for
489 			 * drivers that do not set there operating mode
490 			 * during attachement
491 			 */
492 			if (ppbdev->ctx.valid)
493 				ppb_set_mode(bus, ppbdev->ctx.mode);
494 
495 			crit_exit();
496 			return (0);
497 		}
498 	}
499 
500 	return (error);
501 }
502 
503 /*
504  * ppb_release_bus()
505  *
506  * Release the device allocated with ppb_request_bus()
507  */
508 int
509 ppb_release_bus(device_t bus, device_t dev)
510 {
511 	int error;
512 	struct ppb_data *ppb = DEVTOSOFTC(bus);
513 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
514 
515 	if (ppbdev->intr_resource && ppbdev->intr_cookie) {
516 		/* force interrupt handler unregistration when the ppbus is released */
517 		if ((error = BUS_TEARDOWN_INTR(bus, dev, ppbdev->intr_resource,
518 					       ppbdev->intr_cookie)))
519 			return (error);
520 		ppbdev->intr_cookie = NULL;
521 		ppbdev->intr_resource = NULL;
522 	}
523 
524 	crit_enter();
525 	if (ppb->ppb_owner != dev) {
526 		crit_exit();
527 		return (EACCES);
528 	}
529 	ppb->ppb_owner = 0;
530 	crit_exit();
531 
532 	/* save the context of the device */
533 	ppbdev->ctx.mode = ppb_get_mode(bus);
534 
535 	/* ok, now the context of the device is valid */
536 	ppbdev->ctx.valid = 1;
537 
538 	/* wakeup waiting processes */
539 	wakeup(ppb);
540 
541 	return (0);
542 }
543 
544 static devclass_t ppbus_devclass;
545 
546 static device_method_t ppbus_methods[] = {
547         /* device interface */
548 	DEVMETHOD(device_probe,         ppbus_probe),
549 	DEVMETHOD(device_attach,        ppbus_attach),
550 
551         /* bus interface */
552 	DEVMETHOD(bus_add_child,	ppbus_add_child),
553 	DEVMETHOD(bus_print_child,	ppbus_print_child),
554 	DEVMETHOD(bus_read_ivar,        ppbus_read_ivar),
555 	DEVMETHOD(bus_write_ivar,       ppbus_write_ivar),
556 	DEVMETHOD(bus_setup_intr,	ppbus_setup_intr),
557 	DEVMETHOD(bus_teardown_intr,	ppbus_teardown_intr),
558 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
559 
560         { 0, 0 }
561 };
562 
563 static driver_t ppbus_driver = {
564         "ppbus",
565         ppbus_methods,
566         sizeof(struct ppb_data),
567 };
568 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);
569