xref: /illumos-gate/usr/src/uts/sparc/os/polled_io.c (revision 43d18f1c320355e93c47399bea0b2e022fe06364)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This code sets up the callbacks(vx_handlers) so that the firmware may call
31  * into the kernel for console input and/or output while in the debugger.
32  * The callbacks that execute in debug mode must be careful to not
33  * allocate memory, access mutexes, etc. because most kernel services are
34  * not available during this mode.
35  *
36  * This code, and the underlying code that supports the polled input, is very
37  * hard to debug.  In order to get the code to execute, polled input must
38  * provide input to the debugger.  If anything goes wrong with the code, then
39  * it is hard to debug the debugger.  If there are any problems to debug,
40  * the following is useful:
41  *
42  * set the polled_debug variable in /etc/system
43  *	set polled_debug=1
44  *
45  * This variable will register the callbacks but will not throw the switch
46  * in the firmware.  The callbacks can be executed by hand from the firmware.
47  * Boot the system and drop down to the firmware.
48  *
49  *	ok " /os-io" select-dev
50  *
51  * The following will cause the polled_give_input to execute:
52  *	ok take
53  *
54  * The following will cause the polled_take_input to execute:
55  *	ok give
56  *
57  * The following will cause polled_read to execute:
58  *	ok read
59  */
60 
61 #include <sys/stropts.h>
62 #include <v9/sys/prom_isa.h>
63 #include <sys/devops.h>
64 #include <sys/modctl.h>
65 #include <sys/ddi.h>
66 #include <sys/sunddi.h>
67 #include <sys/promif.h>
68 #include <sys/note.h>
69 #include <sys/consdev.h>
70 #include <sys/polled_io.h>
71 #include <sys/kdi.h>
72 
73 /*
74  * Internal Functions
75  */
76 static void	polled_give_input(cell_t *cif);
77 static void	polled_read(cell_t *cif);
78 static void	polled_take_input(cell_t *cif);
79 static void	polled_give_output(cell_t *cif);
80 static void	polled_write(cell_t *cif);
81 static void	polled_take_output(cell_t *cif);
82 static void	polled_io_register(cons_polledio_t *,
83 			polled_io_console_type_t, int);
84 static void	polled_io_unregister(polled_io_console_type_t, int);
85 static int	polled_io_take_console(polled_io_console_type_t, int);
86 static int	polled_io_release_console(polled_io_console_type_t, int);
87 
88 /*
89  * State information regarding the input/output device
90  */
91 static polled_device_t	polled_input_device;
92 static polled_device_t	polled_output_device;
93 static int polled_vx_handlers_init = 0;
94 
95 extern void	add_vx_handler(char *name, int flag, void (*func)(cell_t *));
96 extern void	remove_vx_handler(char *name);
97 
98 /*
99  * This is a useful flag for debugging the entry points.   This flag
100  * allows us to exercise the entry points from the firmware without
101  * switching the firmware's notion of the input device.
102  */
103 int	polled_debug = 0;
104 
105 /*
106  * This routine is called to initialize polled I/O.  We insert our entry
107  * points so that the firmware will call into this code
108  * when the switch is thrown in polled_io_take_console().
109  */
110 void
111 polled_io_init(void)
112 {
113 
114 	/*
115 	 * Only do the initialization once
116 	 */
117 	if (polled_vx_handlers_init != 0)
118 		return;
119 
120 	/*
121 	 * Add the vx_handlers for the different functions that
122 	 * need to be accessed from firmware.
123 	 */
124 	add_vx_handler("enter-input", 1, polled_give_input);
125 
126 	add_vx_handler("read", 1, polled_read);
127 
128 	add_vx_handler("exit-input", 1, polled_take_input);
129 
130 	add_vx_handler("give-output", 1, polled_give_output);
131 
132 	add_vx_handler("write", 1, polled_write);
133 
134 	add_vx_handler("take-output", 1, polled_take_output);
135 
136 	/*
137 	 * Initialize lock to protect multiple thread access to the
138 	 * polled_input_device structure.  This does not protect
139 	 * us from access in debug mode.
140 	 */
141 	mutex_init(&polled_input_device.polled_device_lock,
142 		NULL, MUTEX_DRIVER, NULL);
143 
144 	/*
145 	 * Initialize lock to protect multiple thread access to the
146 	 * polled_output_device structure.  This does not protect
147 	 * us from access in debug mode.
148 	 */
149 	mutex_init(&polled_output_device.polled_device_lock,
150 		NULL, MUTEX_DRIVER, NULL);
151 
152 	polled_vx_handlers_init = 1;
153 }
154 
155 /*
156  * Register a device for input or output.  The polled_io structure
157  * will be filled in with the callbacks that are appropriate for
158  * that device.
159  */
160 int
161 polled_io_register_callbacks(
162 cons_polledio_t			*polled_io,
163 int				flags
164 )
165 {
166 	/*
167 	 * If the input structure entries aren't filled in, then register this
168 	 * structure as an input device.
169 	 */
170 	if ((polled_io->cons_polledio_getchar != NULL) &&
171 		(polled_io->cons_polledio_ischar != NULL)) {
172 
173 		polled_io_register(polled_io,
174 			POLLED_IO_CONSOLE_INPUT, flags);
175 	}
176 
177 	/*
178 	 * If the output structure entries aren't filled in, then register this
179 	 * structure as an output device.
180 	 */
181 	if (polled_io->cons_polledio_putchar != NULL) {
182 
183 		polled_io_register(polled_io,
184 			POLLED_IO_CONSOLE_OUTPUT, flags);
185 	}
186 
187 	cons_polledio = polled_io;
188 
189 	return (DDI_SUCCESS);
190 }
191 
192 /*
193  * Unregister a device for console input/output.
194  */
195 int
196 polled_io_unregister_callbacks(
197 cons_polledio_t			*polled_io,
198 int				flags
199 )
200 {
201 	/*
202 	 * If polled_io is being used for input, then unregister it.
203 	 */
204 	if (polled_io == polled_input_device.polled_io) {
205 
206 		polled_io_unregister(
207 			POLLED_IO_CONSOLE_INPUT, flags);
208 	}
209 
210 	/*
211 	 * If polled_io is being used for output, then unregister it.
212 	 */
213 	if (polled_io == polled_output_device.polled_io) {
214 
215 		polled_io_unregister(
216 			POLLED_IO_CONSOLE_OUTPUT, flags);
217 	}
218 
219 	return (DDI_SUCCESS);
220 }
221 
222 /*
223  * This routine is called when we are done handling polled io.  We will
224  * remove all of our handlers and destroy any memory that we have allocated.
225  */
226 void
227 polled_io_fini()
228 {
229 	/*
230 	 * Remove the vx_handlers so that our functions will nolonger be
231 	 * accessible.
232 	 */
233 	remove_vx_handler("enter-input");
234 
235 	remove_vx_handler("read");
236 
237 	remove_vx_handler("exit-input");
238 
239 	remove_vx_handler("give-output");
240 
241 	remove_vx_handler("write");
242 
243 	remove_vx_handler("take-output");
244 
245 	/*
246 	 * Destroy the mutexes, we will not need them anymore.
247 	 */
248 	mutex_destroy(&polled_input_device.polled_device_lock);
249 
250 	mutex_destroy(&polled_output_device.polled_device_lock);
251 
252 	polled_vx_handlers_init = 0;
253 }
254 
255 /*
256  * Generic internal routine for registering a polled input or output device.
257  */
258 /* ARGSUSED */
259 static void
260 polled_io_register(
261 cons_polledio_t			*polled_io,
262 polled_io_console_type_t	type,
263 int				flags
264 )
265 {
266 	switch (type) {
267 	case POLLED_IO_CONSOLE_INPUT:
268 		/*
269 		 * Grab the device lock, because we are going to access
270 		 * protected structure entries.  We do this before the
271 		 * POLLED_IO_CONSOLE_OPEN_INPUT so that we serialize
272 		 * registration.
273 		 */
274 		mutex_enter(&polled_input_device.polled_device_lock);
275 
276 		/*
277 		 * Save the polled_io pointers so that we can access
278 		 * them later.
279 		 */
280 		polled_input_device.polled_io = polled_io;
281 
282 		mutex_exit(&polled_input_device.polled_device_lock);
283 
284 
285 		if (!polled_debug) {
286 			/*
287 			 * Tell the generic console framework to
288 			 * repoint firmware's stdin to this keyboard device.
289 			 */
290 			(void) polled_io_take_console(type, 0);
291 		}
292 
293 		break;
294 
295 	case POLLED_IO_CONSOLE_OUTPUT:
296 		/*
297 		 * Grab the device lock, because we are going to access
298 		 * protected structure entries. We do this before the
299 		 * POLLED_IO_CONSOLE_OPEN_OUTPUT so that we serialize
300 		 * registration.
301 		 */
302 		mutex_enter(&polled_output_device.polled_device_lock);
303 
304 		/*
305 		 * Save the polled_io pointers so that we can access
306 		 * them later.
307 		 */
308 		polled_input_device.polled_io = polled_io;
309 
310 		mutex_exit(&polled_output_device.polled_device_lock);
311 
312 		break;
313 	}
314 }
315 
316 /*
317  * Generic internal routine for unregistering a polled input or output device.
318  */
319 /* ARGSUSED */
320 static void
321 polled_io_unregister(
322 polled_io_console_type_t	type,
323 int				flags
324 )
325 {
326 	switch (type) {
327 	case POLLED_IO_CONSOLE_INPUT:
328 		/*
329 		 * Tell the generic console framework to restore
330 		 * the firmware's old stdin pointers.
331 		 */
332 		(void) polled_io_release_console(type, 0);
333 
334 		/*
335 		 * Grab the device lock, because we are going to access
336 		 * protected structure entries.
337 		 */
338 		mutex_enter(&polled_input_device.polled_device_lock);
339 
340 		polled_input_device.polled_io = NULL;
341 
342 		mutex_exit(&polled_input_device.polled_device_lock);
343 
344 		break;
345 
346 	case POLLED_IO_CONSOLE_OUTPUT:
347 		/*
348 		 * Grab the device lock, because we are going to access
349 		 * protected structure entries.
350 		 */
351 		mutex_enter(&polled_output_device.polled_device_lock);
352 
353 		polled_output_device.polled_io = NULL;
354 
355 		mutex_exit(&polled_output_device.polled_device_lock);
356 
357 		break;
358 	}
359 }
360 
361 /*
362  * This is the routine that is called to throw the switch from the
363  * firmware's ownership of stdout/stdin to the kernel.
364  */
365 /* ARGSUSED */
366 static int
367 polled_io_take_console(
368 polled_io_console_type_t	type,
369 int				flags
370 )
371 {
372 
373 	switch (type) {
374 	case POLLED_IO_CONSOLE_INPUT:
375 		/*
376 		 * Call into firmware to switch to the kernel I/O handling.
377 		 * We will save the old value of stdin so that we can
378 		 * restore it if the device is released.
379 		 */
380 #ifdef DEBUG_OBP
381 		/*
382 		 * This code is useful to trace through
383 		 * what the prom is doing
384 		 */
385 		prom_interpret(
386 			"stdin @ swap ! trace-on \" /os-io\" input trace-off",
387 			(uintptr_t)&polled_input_device.polled_old_handle,
388 				0, 0, 0, 0);
389 #endif
390 
391 		prom_interpret(
392 			"stdin @ swap ! \" /os-io\" open-dev stdin !",
393 			(uintptr_t)&polled_input_device.polled_old_handle,
394 				0, 0, 0, 0);
395 
396 		break;
397 
398 	case POLLED_IO_CONSOLE_OUTPUT:
399 		/*
400 		 * Call into firmware to switch to the kernel I/O handling.
401 		 * We will save the old value of stdout so that we can
402 		 * restore it if the device is released.
403 		 */
404 		prom_interpret(
405 			"stdout @ swap ! \" /os-io\" output",
406 			(uintptr_t)&polled_output_device.polled_old_handle,
407 				0, 0, 0, 0);
408 
409 		break;
410 	}
411 
412 	return (DDI_SUCCESS);
413 }
414 
415 /*
416  * This routine gives control of console input/output back to firmware.
417  */
418 /* ARGSUSED */
419 static int
420 polled_io_release_console(
421 polled_io_console_type_t	type,
422 int				flags
423 )
424 {
425 	switch (type) {
426 	case POLLED_IO_CONSOLE_INPUT:
427 		/*
428 		 * Restore the stdin handle
429 		 */
430 		prom_interpret("to stdin",
431 			(uintptr_t)polled_input_device.
432 				polled_old_handle,
433 				0, 0, 0, 0);
434 
435 		break;
436 
437 	case POLLED_IO_CONSOLE_OUTPUT:
438 		/*
439 		 * Restore the stdout handle
440 		 */
441 		prom_interpret("to stdout",
442 			(uintptr_t)polled_output_device.
443 				polled_old_handle,
444 				0, 0, 0, 0);
445 
446 		break;
447 	}
448 
449 	return (DDI_SUCCESS);
450 }
451 
452 
453 /*
454  * This is the routine that the firmware calls to save any state information
455  * before using the input device.  This routine, and all of the
456  * routines that it calls, are responsible for saving any state
457  * information so that it can be restored when debug mode is over.
458  *
459  * WARNING: This routine runs in debug mode.
460  */
461 static void
462 polled_give_input(cell_t *cif)
463 {
464 	cons_polledio_t		*polled_io;
465 	uint_t			out_args;
466 
467 	/*
468 	 * Calculate the offset of the return arguments
469 	 */
470 	out_args = CIF_MIN_SIZE +
471 		p1275_cell2uint(cif[CIF_NUMBER_IN_ARGS]);
472 
473 	/*
474 	 * There is one argument being passed back to firmware.
475 	 */
476 	cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
477 	cif[out_args] = p1275_uint2cell(CIF_SUCCESS);
478 
479 	/*
480 	 * We check to see if there is an
481 	 * input device that has been registered.
482 	 */
483 	polled_io = polled_input_device.polled_io;
484 
485 	if (polled_io == NULL) {
486 		return;
487 	}
488 
489 	/*
490 	 * Call down to the lower layers to save the state.
491 	 */
492 	polled_io->cons_polledio_enter(
493 		polled_io->cons_polledio_argument);
494 }
495 
496 /*
497  * This is the routine that the firmware calls
498  * when it wants to read a character.
499  * We will call to the lower layers to see if there is any input data
500  * available.
501  *
502  * WARNING: This routine runs in debug mode.
503  */
504 static void
505 polled_read(cell_t *cif)
506 {
507 	uint_t				actual;
508 	cons_polledio_t			*polled_io;
509 	uint_t				in_args;
510 	uint_t				out_args;
511 	uchar_t				*buffer;
512 	uint_t				buflen;
513 	uchar_t				key;
514 
515 	/*
516 	 * The number of arguments passed in by the firmware
517 	 */
518 	in_args = p1275_cell2uint(cif[CIF_NUMBER_IN_ARGS]);
519 
520 	/*
521 	 * Calculate the location of the first out arg.  This location is
522 	 * CIF_MIN_SIZE plus the in argument locations.
523 	 */
524 	out_args = CIF_MIN_SIZE + in_args;
525 
526 	/*
527 	 * The firmware should pass in a pointer to a buffer, and the
528 	 * number of characters it expects or expects to write.
529 	 * If 2 arguments are not passed in, then return an error.
530 	 */
531 	if (in_args != 2) {
532 
533 		/*
534 		 * Tell firmware how many arguments we are passing back.
535 		 */
536 		cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
537 
538 		/*
539 		 * Tell the firmware that we cannot give it any characters.
540 		 */
541 		cif[out_args] = p1275_uint2cell(CIF_FAILURE);
542 
543 		return;
544 	}
545 
546 	/*
547 	 * Get the address of where to copy the characters into.
548 	 */
549 	buffer = (uchar_t *)(uintptr_t)p1275_cell2uint(cif[CIF_MIN_SIZE+0]);
550 
551 	/*
552 	 * Get the length of the buffer that we can copy characters into.
553 	 */
554 	buflen = p1275_cell2uint(cif[CIF_MIN_SIZE+1]);
555 
556 	/*
557 	 * Make sure there is enough room in the buffer to copy the
558 	 * characters into.
559 	 */
560 	if (buflen == 0) {
561 
562 		/*
563 		 * Tell the OBP that we cannot give it any characters.
564 		 */
565 		cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
566 
567 		/*
568 		 * Tell the firmware that we cannot give it any characters.
569 		 */
570 		cif[out_args] = p1275_uint2cell(CIF_FAILURE);
571 
572 		return;
573 	}
574 
575 	/*
576 	 * Pass back whether or not the operation was a success or
577 	 * failure plus the actual number of bytes in the buffer.
578 	 * Tell firmware how many arguments we are passing back.
579 	 */
580 	cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)2);
581 
582 	/*
583 	 * Initialize the cif to be "no characters"
584 	 */
585 	cif[out_args+0] = p1275_uint2cell(CIF_SUCCESS);
586 	cif[out_args+1] = p1275_uint2cell(CIF_NO_CHARACTERS);
587 
588 	/*
589 	 * We check to see if there is an
590 	 * input device that has been registered.
591 	 */
592 	polled_io = polled_input_device.polled_io;
593 
594 	if (polled_io == NULL) {
595 
596 		/*
597 		 * The cif structure is already set up to return
598 		 * no characters.
599 		 */
600 
601 		return;
602 	}
603 
604 	actual = 0;
605 
606 	/*
607 	 * Obtain the characters
608 	 */
609 	while (polled_io->cons_polledio_ischar(
610 		polled_io->cons_polledio_argument) == B_TRUE) {
611 
612 		/*
613 		 * Make sure that we don't overrun the buffer.
614 		 */
615 		if (actual == buflen) {
616 
617 			break;
618 		}
619 
620 		/*
621 		 * Call down to the device to copy the input data into the
622 		 * buffer.
623 		 */
624 		key = polled_io->cons_polledio_getchar(
625 			polled_io->cons_polledio_argument);
626 
627 		*(buffer + actual) = key;
628 
629 		actual++;
630 	}
631 
632 	/*
633 	 * There is a special return code when there is no data.
634 	 */
635 	if (actual == 0) {
636 
637 		/*
638 		 * The cif structure is already set up to return
639 		 * no characters.
640 		 */
641 
642 		return;
643 	}
644 
645 	/*
646 	 * Tell firmware how many characters we are sending it.
647 	 */
648 	cif[out_args+0] = p1275_uint2cell((uint_t)CIF_SUCCESS);
649 	cif[out_args+1] = p1275_uint2cell((uint_t)actual);
650 }
651 
652 /*
653  * This is the routine that firmware calls when it is giving up control of the
654  * input device.  This routine, and the lower layer routines that it calls,
655  * are responsible for restoring the controller state to the state it was
656  * in before firmware took control.
657  *
658  * WARNING: This routine runs in debug mode.
659  */
660 static void
661 polled_take_input(cell_t *cif)
662 {
663 	cons_polledio_t		*polled_io;
664 	uint_t			out_args;
665 
666 	/*
667 	 * Calculate the offset of the return arguments
668 	 */
669 	out_args = CIF_MIN_SIZE +
670 		p1275_cell2uint(cif[CIF_NUMBER_IN_ARGS]);
671 
672 	/*
673 	 * There is one argument being passed back to firmware.
674 	 */
675 	cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
676 	cif[out_args] = p1275_uint2cell(CIF_SUCCESS);
677 
678 	/*
679 	 * We check the pointer to see if there is an
680 	 * input device that has been registered.
681 	 */
682 	polled_io = polled_input_device.polled_io;
683 
684 	if (polled_io == NULL) {
685 		return;
686 	}
687 
688 	/*
689 	 * Call down to the lower layers to save the state.
690 	 */
691 	polled_io->cons_polledio_exit(
692 		polled_io->cons_polledio_argument);
693 }
694 
695 /*
696  * This is the routine that the firmware calls to save any state information
697  * before using the output device.  This routine, and all of the
698  * routines that it calls, are responsible for saving any state
699  * information so that it can be restored when the debug  is over.
700  *
701  * WARNING:  This routine runs in debug mode.
702  */
703 static void
704 polled_give_output(cell_t *cif)
705 {
706 	cons_polledio_t		*polled_io;
707 
708 	uint_t			out_args;
709 
710 	/*
711 	 * Calculate the offset of the return arguments
712 	 */
713 	out_args = CIF_MIN_SIZE +
714 		p1275_cell2uint(cif[CIF_NUMBER_IN_ARGS]);
715 
716 	/*
717 	 * There is one argument being passed back to the firmware .
718 	 */
719 	cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
720 	cif[out_args] = p1275_uint2cell(CIF_SUCCESS);
721 
722 	/*
723 	 * We check to see if there is an
724 	 * output device that has been registered.
725 	 */
726 	polled_io = polled_output_device.polled_io;
727 
728 	if (polled_io == NULL) {
729 		return;
730 	}
731 
732 	/*
733 	 * Call down to the lower layers to save the state.
734 	 */
735 	polled_io->cons_polledio_enter(
736 		polled_io->cons_polledio_argument);
737 }
738 
739 /*
740  * This is the routine that the firmware calls when
741  * it wants to write a character.
742  *
743  * WARNING: This routine runs in debug mode.
744  */
745 static void
746 polled_write(cell_t *cif)
747 {
748 	cons_polledio_t			*polled_io;
749 	uint_t				in_args;
750 	uint_t				out_args;
751 	uchar_t				*buffer;
752 	uint_t				buflen;
753 	uint_t				i;
754 
755 	/*
756 	 * The number of arguments passed in by the firmware
757 	 */
758 	in_args = p1275_cell2uint(cif[CIF_NUMBER_IN_ARGS]);
759 
760 	/*
761 	 * Calculate the location of the first out arg.  This location is
762 	 * CIF_MIN_SIZE (name + no. in args + no. out args) plus the
763 	 * in argument locations.
764 	 */
765 	out_args = CIF_MIN_SIZE + in_args;
766 
767 	/*
768 	 * The firmware should pass in a pointer to a buffer, and the
769 	 * number of characters it expects or expects to write.
770 	 * If 2 arguments are not passed in, then return an error.
771 	 */
772 	if (in_args != 2) {
773 
774 		/*
775 		 * Tell firmware how many arguments we are passing back.
776 		 */
777 		cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
778 
779 
780 		/*
781 		 * Tell the firmware that we cannot give it any characters.
782 		 */
783 		cif[out_args] = p1275_uint2cell(CIF_FAILURE);
784 
785 		return;
786 	}
787 
788 	/*
789 	 * Get the address of where to copy the characters into.
790 	 */
791 	buffer = (uchar_t *)(uintptr_t)p1275_cell2uint(cif[CIF_MIN_SIZE+0]);
792 
793 	/*
794 	 * Get the length of the buffer that we can copy characters into.
795 	 */
796 	buflen = p1275_cell2uint(cif[CIF_MIN_SIZE+1]);
797 
798 	/*
799 	 * Make sure there is enough room in the buffer to copy the
800 	 * characters into.
801 	 */
802 	if (buflen == 0) {
803 
804 		/*
805 		 * Tell the OBP that we cannot give it any characters.
806 		 */
807 		cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
808 
809 		/*
810 		 * Tell the firmware that we cannot give it any characters.
811 		 */
812 		cif[out_args] = p1275_uint2cell(CIF_FAILURE);
813 
814 		return;
815 	}
816 
817 
818 	/*
819 	 * Tell the firmware how many arguments we are passing back.
820 	 */
821 	cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)2);
822 
823 	/*
824 	 * Initialize the cif to success
825 	 */
826 	cif[out_args+0] = p1275_uint2cell(CIF_SUCCESS);
827 	cif[out_args+1] = p1275_uint2cell(0);
828 
829 	/*
830 	 * We check the pointer to see if there is an
831 	 * input device that has been registered.
832 	 */
833 	polled_io = polled_output_device.polled_io;
834 
835 	if (polled_io == NULL) {
836 
837 		/*
838 		 * The cif is already initialized
839 		 */
840 		return;
841 	}
842 
843 	for (i = 0; i < buflen; i++) {
844 
845 		polled_io->cons_polledio_putchar(
846 			polled_io->cons_polledio_argument, *(buffer + i));
847 	}
848 
849 	/*
850 	 * Tell the firmware how many characters we are sending it.
851 	 */
852 	cif[out_args+0] = p1275_uint2cell((uint_t)CIF_SUCCESS);
853 	cif[out_args+1] = p1275_uint2cell((uint_t)buflen);
854 }
855 
856 /*
857  * This is the routine that the firmware calls
858  *  when it is giving up control of the
859  * output device.  This routine, and the lower layer routines that it calls,
860  * are responsible for restoring the controller state to the state it was
861  * in before the firmware took control.
862  *
863  * WARNING: This routine runs in debug mode.
864  */
865 static void
866 polled_take_output(cell_t *cif)
867 {
868 	cons_polledio_t		*polled_io;
869 	uint_t			out_args;
870 
871 	/*
872 	 * Calculate the offset of the return arguments
873 	 */
874 	out_args = CIF_MIN_SIZE +
875 		p1275_cell2uint(cif[CIF_NUMBER_IN_ARGS]);
876 
877 	/*
878 	 * There is one argument being passed back to the firmware.
879 	 */
880 	cif[CIF_NUMBER_OUT_ARGS] = p1275_uint2cell((uint_t)1);
881 	cif[out_args] = p1275_uint2cell(CIF_SUCCESS);
882 
883 	/*
884 	 * We check the pointer to see if there is an
885 	 * output device that has been registered.
886 	 */
887 	polled_io = polled_output_device.polled_io;
888 
889 	if (polled_io == NULL) {
890 		return;
891 	}
892 
893 	/*
894 	 * Call down to the lower layers to save the state.
895 	 */
896 	polled_io->cons_polledio_exit(
897 		polled_io->cons_polledio_argument);
898 }
899