xref: /onnv-gate/usr/src/uts/common/io/usb/usba/genconsole.c (revision 7425:e4dbffd35ebc)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52191Sszhou  * Common Development and Distribution License (the "License").
62191Sszhou  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7425SGongtian.Zhao@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * ISSUES:
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #define	USBA_FRAMEWORK
320Sstevel@tonic-gate #include <sys/usb/usba.h>
330Sstevel@tonic-gate #include <sys/usb/usba/hcdi.h>
340Sstevel@tonic-gate #include <sys/usb/usba/genconsole.h>
350Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
360Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
392191Sszhou  * Initialize USB polled support. This routine calls down to the lower
400Sstevel@tonic-gate  * layers to initialize any state information.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate int
usb_console_input_init(dev_info_t * dip,usb_pipe_handle_t pipe_handle,uchar_t ** state_buf,usb_console_info_t * console_input_info)430Sstevel@tonic-gate usb_console_input_init(dev_info_t		*dip,
440Sstevel@tonic-gate 			usb_pipe_handle_t	pipe_handle,
452191Sszhou 			uchar_t			**state_buf,
460Sstevel@tonic-gate 			usb_console_info_t	*console_input_info)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	int			ret;
490Sstevel@tonic-gate 	usba_device_t		*usba_device;
503943Ssl147100 	usba_pipe_handle_data_t	*ph_data;
513943Ssl147100 	usb_console_info_impl_t	*usb_console_input;
523943Ssl147100 
533943Ssl147100 	if (dip == NULL) {
543943Ssl147100 
553943Ssl147100 		return (USB_INVALID_ARGS);
563943Ssl147100 	}
573943Ssl147100 
583943Ssl147100 	if (DEVI_IS_DEVICE_REMOVED(dip)) {
593943Ssl147100 
603943Ssl147100 		return (USB_FAILURE);
613943Ssl147100 	}
623943Ssl147100 
633943Ssl147100 	usb_console_input = kmem_zalloc(
643943Ssl147100 	    sizeof (struct usb_console_info_impl), KM_SLEEP);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Save the dip
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	usb_console_input->uci_dip = dip;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * Translate the dip into a device.
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/*
773943Ssl147100 	 * Get ph_data from pipe handle and hold the data
783943Ssl147100 	 */
793943Ssl147100 	if ((ph_data = usba_hold_ph_data(pipe_handle)) == NULL) {
803943Ssl147100 		kmem_free(usb_console_input,
813943Ssl147100 		    sizeof (struct usb_console_info_impl));
823943Ssl147100 
833943Ssl147100 		return (USB_INVALID_PIPE);
843943Ssl147100 	}
853943Ssl147100 
863943Ssl147100 	/*
870Sstevel@tonic-gate 	 * Call the lower layer to initialize any state information
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	ret = usba_device->usb_hcdi_ops->usba_hcdi_console_input_init(
903943Ssl147100 	    ph_data, state_buf, usb_console_input);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if (ret != USB_SUCCESS) {
930Sstevel@tonic-gate 		kmem_free(usb_console_input,
943943Ssl147100 		    sizeof (struct usb_console_info_impl));
950Sstevel@tonic-gate 	} else {
960Sstevel@tonic-gate 		*console_input_info = (usb_console_info_t)usb_console_input;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
993943Ssl147100 	usba_release_ph_data((usba_ph_impl_t *)pipe_handle);
1003943Ssl147100 
1010Sstevel@tonic-gate 	return (ret);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Free up any resources that we allocated in the above initialization
1070Sstevel@tonic-gate  * routine.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate int
usb_console_input_fini(usb_console_info_t console_input_info)1100Sstevel@tonic-gate usb_console_input_fini(usb_console_info_t console_input_info)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	usb_console_info_impl_t		*usb_console_input;
1130Sstevel@tonic-gate 	usba_device_t			*usba_device;
1140Sstevel@tonic-gate 	int				ret;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	usb_console_input = (usb_console_info_impl_t *)console_input_info;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
1190Sstevel@tonic-gate 	 * Translate the dip into a device.
1200Sstevel@tonic-gate 	 */
1210Sstevel@tonic-gate 	usba_device = usba_get_usba_device(usb_console_input->uci_dip);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * Call the lower layer to free any state information.
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	ret = usba_device->usb_hcdi_ops->usba_hcdi_console_input_fini(
127*7425SGongtian.Zhao@Sun.COM 	    usb_console_input);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (ret == USB_FAILURE) {
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 		return (ret);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * We won't be needing this information anymore.
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	kmem_free(usb_console_input, sizeof (struct usb_console_info_impl));
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	return (USB_SUCCESS);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * This is the routine that OBP calls to save the USB state information
1450Sstevel@tonic-gate  * before using the USB keyboard as an input device.  This routine,
1460Sstevel@tonic-gate  * and all of the routines that it calls, are responsible for saving
1470Sstevel@tonic-gate  * any state information so that it can be restored when OBP mode is
1480Sstevel@tonic-gate  * over.  At this layer, this code is mainly just a pass through.
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  * Warning:  this code runs in polled mode.
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate int
usb_console_input_enter(usb_console_info_t console_input_info)1530Sstevel@tonic-gate usb_console_input_enter(usb_console_info_t console_input_info)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	usba_device_t				*usba_device;
1560Sstevel@tonic-gate 	usb_console_info_impl_t			*usb_console_input;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	usb_console_input = (usb_console_info_impl_t *)console_input_info;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Translate the dip into a device.
1620Sstevel@tonic-gate 	 * Do this by directly looking at the dip, do not call
1630Sstevel@tonic-gate 	 * usba_get_usba_device() because this function calls into the DDI.
1640Sstevel@tonic-gate 	 * The ddi then tries to acquire a mutex and the machine hard hangs.
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	usba_device = usba_polled_get_usba_device(usb_console_input->uci_dip);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * Call the lower layer to save state information.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	usba_device->usb_hcdi_ops->usba_hcdi_console_input_enter(
172*7425SGongtian.Zhao@Sun.COM 	    usb_console_input);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	return (USB_SUCCESS);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * This is the routine that OBP calls when it wants to read a character.
1800Sstevel@tonic-gate  * We will call to the lower layers to see if there is any input data
1810Sstevel@tonic-gate  * available.  At this layer, this code is mainly just a pass through.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * Warning: This code runs in polled mode.
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate int
usb_console_read(usb_console_info_t console_input_info,uint_t * num_characters)1860Sstevel@tonic-gate usb_console_read(usb_console_info_t console_input_info, uint_t *num_characters)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	usba_device_t				*usba_device;
1890Sstevel@tonic-gate 	usb_console_info_impl_t			*usb_console_input;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	usb_console_input = (usb_console_info_impl_t *)console_input_info;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Translate the dip into a device.
1950Sstevel@tonic-gate 	 * Do this by directly looking at the dip, do not call
1960Sstevel@tonic-gate 	 * usba_get_usba_device() because this function calls into the DDI.
1970Sstevel@tonic-gate 	 * The ddi then tries to acquire a mutex and the machine hard hangs.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	usba_device = usba_polled_get_usba_device(usb_console_input->uci_dip);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * Call the lower layer to get a a character.  Return the number
2030Sstevel@tonic-gate 	 * of characters read into the buffer.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	return (usba_device->usb_hcdi_ops->usba_hcdi_console_read(
206*7425SGongtian.Zhao@Sun.COM 	    usb_console_input, num_characters));
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * This is the routine that OBP calls when it is giving up control of the
2120Sstevel@tonic-gate  * USB keyboard.  This routine, and the lower layer routines that it calls,
2130Sstevel@tonic-gate  * are responsible for restoring the controller state to the state it was
2140Sstevel@tonic-gate  * in before OBP took control. At this layer, this code is mainly just a
2150Sstevel@tonic-gate  * pass through.
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * Warning: This code runs in polled mode.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate int
usb_console_input_exit(usb_console_info_t console_input_info)2200Sstevel@tonic-gate usb_console_input_exit(usb_console_info_t console_input_info)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	usba_device_t				*usba_device;
2230Sstevel@tonic-gate 	usb_console_info_impl_t			*usb_console_input;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	usb_console_input = (usb_console_info_impl_t *)console_input_info;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * Translate the dip into a device.
2290Sstevel@tonic-gate 	 * Do this by directly looking at the dip, do not call
2300Sstevel@tonic-gate 	 * usba_get_usba_device() because this function calls into the DDI.
2310Sstevel@tonic-gate 	 * The ddi then tries to acquire a mutex and the machine hard hangs.
2320Sstevel@tonic-gate 	 */
2330Sstevel@tonic-gate 	usba_device = usba_polled_get_usba_device(usb_console_input->uci_dip);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * Restore the state information.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	usba_device->usb_hcdi_ops->usba_hcdi_console_input_exit(
239*7425SGongtian.Zhao@Sun.COM 	    usb_console_input);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	return (USB_SUCCESS);
2420Sstevel@tonic-gate }
2432191Sszhou 
2442191Sszhou /*
245*7425SGongtian.Zhao@Sun.COM  * Initialize USB OBP support.	This routine calls down to the lower
2462191Sszhou  * layers to initialize any state information.
2472191Sszhou  */
2482191Sszhou int
usb_console_output_init(dev_info_t * dip,usb_pipe_handle_t pipe_handle,usb_console_info_t * console_output_info)2492191Sszhou usb_console_output_init(
2502191Sszhou 	dev_info_t		*dip,
2512191Sszhou 	usb_pipe_handle_t	pipe_handle,
2522191Sszhou 	usb_console_info_t	*console_output_info)
2532191Sszhou {
2542191Sszhou 	usba_device_t		*usb_device;
2552191Sszhou 	usb_console_info_impl_t	*usb_console_output;
2562191Sszhou 	int			ret;
2572191Sszhou 
2582191Sszhou 	/* Translate the dip into a device and check hcdi ops  */
2592191Sszhou 	usb_device = usba_get_usba_device(dip);
2602191Sszhou 	if (usb_device->usb_hcdi_ops->usba_hcdi_ops_version <
2612191Sszhou 	    HCDI_OPS_VERSION_1 ||
2622191Sszhou 	    usb_device->usb_hcdi_ops->usba_hcdi_console_output_init == NULL)
2632191Sszhou 
2642191Sszhou 		return (USB_FAILURE);
2652191Sszhou 
2662191Sszhou 	usb_console_output = kmem_zalloc(sizeof (struct usb_console_info_impl),
267*7425SGongtian.Zhao@Sun.COM 	    KM_SLEEP);
2682191Sszhou 	usb_console_output->uci_dip = dip;
2692191Sszhou 
2702191Sszhou 	/*
2712191Sszhou 	 * Call the lower layer to initialize any state information
2722191Sszhou 	 */
2732191Sszhou 	ret = usb_device->usb_hcdi_ops->usba_hcdi_console_output_init(
274*7425SGongtian.Zhao@Sun.COM 	    usba_get_ph_data(pipe_handle), usb_console_output);
2752191Sszhou 
2762191Sszhou 	if (ret == USB_FAILURE) {
2772191Sszhou 		kmem_free(usb_console_output,
278*7425SGongtian.Zhao@Sun.COM 		    sizeof (struct usb_console_info_impl));
2792191Sszhou 
2802191Sszhou 		return (ret);
2812191Sszhou 	}
2822191Sszhou 
2832191Sszhou 	*console_output_info = (usb_console_info_t)usb_console_output;
2842191Sszhou 
2852191Sszhou 	return (USB_SUCCESS);
2862191Sszhou }
2872191Sszhou 
2882191Sszhou /*
2892191Sszhou  * Free up any resources that we allocated in the above initialization
2902191Sszhou  * routine.
2912191Sszhou  */
2922191Sszhou int
usb_console_output_fini(usb_console_info_t console_output_info)2932191Sszhou usb_console_output_fini(usb_console_info_t console_output_info)
2942191Sszhou {
2952191Sszhou 	usb_console_info_impl_t	*usb_console_output;
2962191Sszhou 	usba_device_t		*usb_device;
2972191Sszhou 	int			ret;
2982191Sszhou 
2992191Sszhou 	usb_console_output = (usb_console_info_impl_t *)console_output_info;
3002191Sszhou 
3012191Sszhou 	/*
3022191Sszhou 	 * Translate the dip into a device.
3032191Sszhou 	 */
3042191Sszhou 	usb_device = usba_polled_get_usba_device(usb_console_output->uci_dip);
3052191Sszhou 
3062191Sszhou 	/*
3072191Sszhou 	 * Call the lower layer to free any state information.
3082191Sszhou 	 */
3092191Sszhou 	ret = usb_device->usb_hcdi_ops->usba_hcdi_console_output_fini(
310*7425SGongtian.Zhao@Sun.COM 	    usb_console_output);
3112191Sszhou 
3122191Sszhou 	if (ret == USB_FAILURE) {
3132191Sszhou 
3142191Sszhou 		return (ret);
3152191Sszhou 	}
3162191Sszhou 
3172191Sszhou 	/*
3182191Sszhou 	 * We won't be needing this information anymore.
3192191Sszhou 	 */
3202191Sszhou 	kmem_free(usb_console_output, sizeof (struct usb_console_info_impl));
3212191Sszhou 
3222191Sszhou 	return (USB_SUCCESS);
3232191Sszhou }
3242191Sszhou 
3252191Sszhou /*
3262191Sszhou  * This is the routine that OBP calls to save the USB state information
3272191Sszhou  * before using the USB device as an output device.  This routine,
3282191Sszhou  * and all of the routines that it calls, are responsible for saving
3292191Sszhou  * any state information so that it can be restored when OBP mode is
3302191Sszhou  * over.  At this layer, this code is mainly just a pass through.
3312191Sszhou  */
3322191Sszhou int
usb_console_output_enter(usb_console_info_t console_output_info)3332191Sszhou usb_console_output_enter(usb_console_info_t console_output_info)
3342191Sszhou {
3352191Sszhou 	usba_device_t			    *usb_device;
3362191Sszhou 	usb_console_info_impl_t		 *usb_console_output;
3372191Sszhou 
3382191Sszhou 	usb_console_output = (usb_console_info_impl_t *)console_output_info;
3392191Sszhou 
3402191Sszhou 	/*
3412191Sszhou 	 * Translate the dip into a device.
3422191Sszhou 	 */
3432191Sszhou 	usb_device = usba_polled_get_usba_device(usb_console_output->uci_dip);
3442191Sszhou 
3452191Sszhou 	/*
3462191Sszhou 	 * Call the lower layer to save state information.
3472191Sszhou 	 */
3482191Sszhou 	usb_device->usb_hcdi_ops->usba_hcdi_console_output_enter(
349*7425SGongtian.Zhao@Sun.COM 	    usb_console_output);
3502191Sszhou 
3512191Sszhou 	return (USB_SUCCESS);
3522191Sszhou }
3532191Sszhou 
3542191Sszhou /*
3552191Sszhou  * This is the routine that OBP calls when it wants to write a character.
3562191Sszhou  * We will call to the lower layers to write any data
3572191Sszhou  * At this layer, this code is mainly just a pass through.
3582191Sszhou  */
3592191Sszhou int
usb_console_write(usb_console_info_t console_output_info,uchar_t * buf,uint_t num_characters,uint_t * num_characters_written)3602191Sszhou usb_console_write(usb_console_info_t console_output_info,
3612191Sszhou 	uchar_t *buf, uint_t num_characters, uint_t *num_characters_written)
3622191Sszhou {
3632191Sszhou 	usba_device_t		*usb_device;
3642191Sszhou 	usb_console_info_impl_t	*usb_console_output;
3652191Sszhou 
3662191Sszhou 	usb_console_output = (usb_console_info_impl_t *)console_output_info;
3672191Sszhou 
3682191Sszhou 	/*
3692191Sszhou 	 * Translate the dip into a device.
3702191Sszhou 	 */
3712191Sszhou 	usb_device = usba_polled_get_usba_device(usb_console_output->uci_dip);
3722191Sszhou 
3732191Sszhou 	/*
3742191Sszhou 	 * Call the lower layer to get a a character.  Return the number
3752191Sszhou 	 * of characters read into the buffer.
3762191Sszhou 	 */
3772191Sszhou 	return (usb_device->usb_hcdi_ops->usba_hcdi_console_write(
378*7425SGongtian.Zhao@Sun.COM 	    usb_console_output, buf, num_characters,
379*7425SGongtian.Zhao@Sun.COM 	    num_characters_written));
3802191Sszhou }
3812191Sszhou 
3822191Sszhou /*
3832191Sszhou  * This is the routine that OBP calls when it is giving up control of the
3842191Sszhou  * USB output device.  This routine, and the lower layer routines that it
3852191Sszhou  * calls, are responsible for restoring the controller state to the state
3862191Sszhou  * it was in before OBP took control. At this layer, this code is mainly
3872191Sszhou  * just a pass through.
3882191Sszhou  */
3892191Sszhou int
usb_console_output_exit(usb_console_info_t console_output_info)3902191Sszhou usb_console_output_exit(usb_console_info_t console_output_info)
3912191Sszhou {
3922191Sszhou 	usba_device_t			 *usb_device;
3932191Sszhou 	usb_console_info_impl_t		 *usb_console_output;
3942191Sszhou 
3952191Sszhou 	usb_console_output = (usb_console_info_impl_t *)console_output_info;
3962191Sszhou 
3972191Sszhou 	/*
3982191Sszhou 	 * Translate the dip into a device.
3992191Sszhou 	 */
4002191Sszhou 	usb_device = usba_polled_get_usba_device(usb_console_output->uci_dip);
4012191Sszhou 
4022191Sszhou 	/*
4032191Sszhou 	 * Restore the state information.
4042191Sszhou 	 */
4052191Sszhou 	usb_device->usb_hcdi_ops->usba_hcdi_console_output_exit(
406*7425SGongtian.Zhao@Sun.COM 	    usb_console_output);
4072191Sszhou 
4082191Sszhou 	return (USB_SUCCESS);
4092191Sszhou }
410