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