ppbconf.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/dev/ppbus/ppbconf.c 330897 2018-03-14 03:19:51Z eadler $");
33#include "opt_ppb_1284.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/lock.h>
39#include <sys/module.h>
40#include <sys/mutex.h>
41#include <sys/bus.h>
42#include <sys/malloc.h>
43#include <sys/rman.h>
44
45#include <machine/resource.h>
46
47#include <dev/ppbus/ppbconf.h>
48#include <dev/ppbus/ppb_1284.h>
49
50#include "ppbus_if.h"
51
52#define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
53
54static MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
55
56
57static int	ppbus_intr(void *arg);
58
59/*
60 * Device methods
61 */
62
63static int
64ppbus_print_child(device_t bus, device_t dev)
65{
66	struct ppb_device *ppbdev;
67	int retval;
68
69	retval = bus_print_child_header(bus, dev);
70
71	ppbdev = (struct ppb_device *)device_get_ivars(dev);
72
73	if (ppbdev->flags != 0)
74		retval += printf(" flags 0x%x", ppbdev->flags);
75
76	retval += bus_print_child_footer(bus, dev);
77
78	return (retval);
79}
80
81static int
82ppbus_probe(device_t dev)
83{
84	device_set_desc(dev, "Parallel port bus");
85
86	return (0);
87}
88
89/*
90 * ppbus_add_child()
91 *
92 * Add a ppbus device, allocate/initialize the ivars
93 */
94static device_t
95ppbus_add_child(device_t dev, u_int order, const char *name, int unit)
96{
97	struct ppb_device *ppbdev;
98	device_t child;
99
100	/* allocate ivars for the new ppbus child */
101	ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV,
102		M_NOWAIT | M_ZERO);
103	if (!ppbdev)
104		return (NULL);
105
106	/* initialize the ivars */
107	ppbdev->name = name;
108
109	/* add the device as a child to the ppbus bus with the allocated
110	 * ivars */
111	child = device_add_child_ordered(dev, order, name, unit);
112	device_set_ivars(child, ppbdev);
113
114	return (child);
115}
116
117static int
118ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
119{
120
121	switch (index) {
122	case PPBUS_IVAR_MODE:
123		/* XXX yet device mode = ppbus mode = chipset mode */
124		*val = (u_long)ppb_get_mode(bus);
125		break;
126	default:
127		return (ENOENT);
128	}
129
130	return (0);
131}
132
133static int
134ppbus_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
135{
136
137	switch (index) {
138	case PPBUS_IVAR_MODE:
139		/* XXX yet device mode = ppbus mode = chipset mode */
140		ppb_set_mode(bus, val);
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 occurrence 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 = NULL;
212	int i, len, error;
213	int class_id = -1;
214	char str[PPB_PnP_STRING_SIZE+1];
215
216	device_printf(bus, "Probing for PnP devices:\n");
217
218	if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
219					PPB_PnP_STRING_SIZE, &len)))
220		goto end_detect;
221
222#ifdef DEBUG_1284
223	device_printf(bus, "<PnP> %d characters: ", len);
224	for (i = 0; i < len; i++)
225		printf("%c(0x%x) ", str[i], str[i]);
226	printf("\n");
227#endif
228
229	/* replace ';' characters by '\0' */
230	for (i = 0; i < len; i++)
231		str[i] = (str[i] == ';') ? '\0' : str[i];
232
233	if ((token = search_token(str, len, "MFG")) != NULL ||
234		(token = search_token(str, len, "MANUFACTURER")) != NULL)
235		device_printf(bus, "<%s",
236			search_token(token, UNKNOWN_LENGTH, ":") + 1);
237	else
238		device_printf(bus, "<unknown");
239
240	if ((token = search_token(str, len, "MDL")) != NULL ||
241		(token = search_token(str, len, "MODEL")) != NULL)
242		printf(" %s",
243			search_token(token, UNKNOWN_LENGTH, ":") + 1);
244	else
245		printf(" unknown");
246
247	if ((token = search_token(str, len, "VER")) != NULL)
248		printf("/%s",
249			search_token(token, UNKNOWN_LENGTH, ":") + 1);
250
251	if ((token = search_token(str, len, "REV")) != NULL)
252		printf(".%s",
253			search_token(token, UNKNOWN_LENGTH, ":") + 1);
254
255	printf(">");
256
257	if ((token = search_token(str, len, "CLS")) != NULL) {
258		class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
259		printf(" %s", class);
260	}
261
262	if ((token = search_token(str, len, "CMD")) != NULL ||
263		(token = search_token(str, len, "COMMAND")) != NULL)
264		printf(" %s",
265			search_token(token, UNKNOWN_LENGTH, ":") + 1);
266
267	printf("\n");
268
269	if (class)
270		/* identify class ident */
271		for (i = 0; pnp_tokens[i] != NULL; i++) {
272			if (search_token(class, len, pnp_tokens[i]) != NULL) {
273				class_id = i;
274				goto end_detect;
275			}
276		}
277
278	class_id = PPB_PnP_UNKNOWN;
279
280end_detect:
281	return (class_id);
282}
283
284/*
285 * ppb_scan_bus()
286 *
287 * Scan the ppbus for IEEE1284 compliant devices
288 */
289static int
290ppb_scan_bus(device_t bus)
291{
292	struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
293	int error = 0;
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	device_printf(bus, "IEEE1284 device found ");
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
375end_scan:
376	return (error);
377}
378
379#endif /* !DONTPROBE_1284 */
380
381static int
382ppbus_attach(device_t dev)
383{
384	struct ppb_data *ppb = device_get_softc(dev);
385	int error, rid;
386
387	error = BUS_READ_IVAR(device_get_parent(dev), dev, PPC_IVAR_LOCK,
388	    (uintptr_t *)&ppb->ppc_lock);
389	if (error) {
390		device_printf(dev, "Unable to fetch parent's lock\n");
391		return (error);
392	}
393
394	rid = 0;
395	ppb->ppc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
396	    RF_SHAREABLE);
397	if (ppb->ppc_irq_res != NULL) {
398		mtx_lock(ppb->ppc_lock);
399		error = BUS_WRITE_IVAR(device_get_parent(dev), dev,
400		    PPC_IVAR_INTR_HANDLER, (uintptr_t)&ppbus_intr);
401		mtx_unlock(ppb->ppc_lock);
402		if (error) {
403			device_printf(dev, "Unable to set interrupt handler\n");
404			return (error);
405		}
406	}
407
408	/* Locate our children */
409	bus_generic_probe(dev);
410
411#ifndef DONTPROBE_1284
412	/* detect IEEE1284 compliant devices */
413	mtx_lock(ppb->ppc_lock);
414	ppb_scan_bus(dev);
415	mtx_unlock(ppb->ppc_lock);
416#endif /* !DONTPROBE_1284 */
417
418	/* launch attachment of the added children */
419	bus_generic_attach(dev);
420
421	return (0);
422}
423
424static int
425ppbus_detach(device_t dev)
426{
427	int error;
428
429	error = bus_generic_detach(dev);
430	if (error)
431		return (error);
432
433	/* detach & delete all children */
434	device_delete_children(dev);
435
436	return (0);
437}
438
439static int
440ppbus_intr(void *arg)
441{
442	struct ppb_device *ppbdev;
443	struct ppb_data *ppb = arg;
444
445	mtx_assert(ppb->ppc_lock, MA_OWNED);
446	if (ppb->ppb_owner == NULL)
447		return (ENOENT);
448
449	ppbdev = device_get_ivars(ppb->ppb_owner);
450	if (ppbdev->intr_hook == NULL)
451		return (ENOENT);
452
453	ppbdev->intr_hook(ppbdev->intr_arg);
454	return (0);
455}
456
457static int
458ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
459    driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
460{
461	struct ppb_device *ppbdev = device_get_ivars(child);
462	struct ppb_data *ppb = DEVTOSOFTC(bus);
463
464	/* We do not support filters. */
465	if (filt != NULL || ihand == NULL)
466		return (EINVAL);
467
468	/* Can only attach handlers to the parent device's resource. */
469	if (ppb->ppc_irq_res != r)
470		return (EINVAL);
471
472	mtx_lock(ppb->ppc_lock);
473	ppbdev->intr_hook = ihand;
474	ppbdev->intr_arg = arg;
475	*cookiep = ppbdev;
476	mtx_unlock(ppb->ppc_lock);
477
478	return (0);
479}
480
481static int
482ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
483{
484	struct ppb_device *ppbdev = device_get_ivars(child);
485	struct ppb_data *ppb = DEVTOSOFTC(bus);
486
487	mtx_lock(ppb->ppc_lock);
488	if (ppbdev != ih || ppb->ppc_irq_res != r) {
489		mtx_unlock(ppb->ppc_lock);
490		return (EINVAL);
491	}
492
493	ppbdev->intr_hook = NULL;
494	mtx_unlock(ppb->ppc_lock);
495
496	return (0);
497}
498
499/*
500 * ppb_request_bus()
501 *
502 * Allocate the device to perform transfers.
503 *
504 * how	: PPB_WAIT or PPB_DONTWAIT
505 */
506int
507ppb_request_bus(device_t bus, device_t dev, int how)
508{
509	struct ppb_data *ppb = DEVTOSOFTC(bus);
510	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
511	int error = 0;
512
513	mtx_assert(ppb->ppc_lock, MA_OWNED);
514	while (!error) {
515		if (ppb->ppb_owner) {
516			switch (how) {
517			case PPB_WAIT | PPB_INTR:
518				error = mtx_sleep(ppb, ppb->ppc_lock,
519				    PPBPRI | PCATCH, "ppbreq", 0);
520				break;
521
522			case PPB_WAIT | PPB_NOINTR:
523				error = mtx_sleep(ppb, ppb->ppc_lock, PPBPRI,
524				    "ppbreq", 0);
525				break;
526
527			default:
528				return (EWOULDBLOCK);
529			}
530
531		} else {
532			ppb->ppb_owner = dev;
533
534			/* restore the context of the device
535			 * The first time, ctx.valid is certainly false
536			 * then do not change anything. This is useful for
537			 * drivers that do not set there operating mode
538			 * during attachement
539			 */
540			if (ppbdev->ctx.valid)
541				ppb_set_mode(bus, ppbdev->ctx.mode);
542
543			return (0);
544		}
545	}
546
547	return (error);
548}
549
550/*
551 * ppb_release_bus()
552 *
553 * Release the device allocated with ppb_request_bus()
554 */
555int
556ppb_release_bus(device_t bus, device_t dev)
557{
558	struct ppb_data *ppb = DEVTOSOFTC(bus);
559	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
560
561	mtx_assert(ppb->ppc_lock, MA_OWNED);
562	if (ppb->ppb_owner != dev)
563		return (EACCES);
564
565	/* save the context of the device */
566	ppbdev->ctx.mode = ppb_get_mode(bus);
567
568	/* ok, now the context of the device is valid */
569	ppbdev->ctx.valid = 1;
570
571	ppb->ppb_owner = 0;
572
573	/* wakeup waiting processes */
574	wakeup(ppb);
575
576	return (0);
577}
578
579static devclass_t ppbus_devclass;
580
581static device_method_t ppbus_methods[] = {
582	/* device interface */
583	DEVMETHOD(device_probe,		ppbus_probe),
584	DEVMETHOD(device_attach,	ppbus_attach),
585	DEVMETHOD(device_detach,	ppbus_detach),
586
587	/* bus interface */
588	DEVMETHOD(bus_add_child,	ppbus_add_child),
589	DEVMETHOD(bus_print_child,	ppbus_print_child),
590	DEVMETHOD(bus_read_ivar,	ppbus_read_ivar),
591	DEVMETHOD(bus_write_ivar,	ppbus_write_ivar),
592	DEVMETHOD(bus_setup_intr,	ppbus_setup_intr),
593	DEVMETHOD(bus_teardown_intr,	ppbus_teardown_intr),
594	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
595	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
596
597	{ 0, 0 }
598};
599
600static driver_t ppbus_driver = {
601	"ppbus",
602	ppbus_methods,
603	sizeof(struct ppb_data),
604};
605DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);
606