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