/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2014 Garrett D'Amore <garrett@damore.org>
*/
/*
* USBA: Solaris USB Architecture support
*
* all functions exposed to client drivers have prefix usb_ while all USBA
* internal functions or functions exposed to HCD or hubd only have prefix
* usba_
*
* this file contains all USBAI pipe management
* usb_pipe_open()
* usb_pipe_close()
* usb_pipe_set_private()
* usb_pipe_get_private()
* usb_pipe_abort()
* usb_pipe_reset()
* usb_pipe_drain_reqs()
*/
#define USBA_FRAMEWORK
extern pri_t maxclsyspri;
extern pri_t minclsyspri;
/* function prototypes */
static void usba_pipe_do_async_func_thread(void *arg);
/* local tunables */
/* return the default pipe for this device */
{
if (dip) {
if (usba_device) {
}
}
return (pipe_handle);
}
/* return dip owner of pipe_handle */
{
if (ph_impl) {
}
return (dip);
}
{
if ((usba_device) &&
}
return (pipe_handle);
}
{
if (ph_impl) {
}
return (ph_data);
}
{
if (ph_data) {
}
return (ph);
}
/*
* opaque to pipe handle impl translation with incr of ref count. The caller
* must release ph_data when done. Increment the ref count ensures that
* the ph_data will not be freed underneath us.
*/
{
if (ph_impl) {
switch (ph_impl->usba_ph_state) {
case USB_PIPE_STATE_IDLE:
case USB_PIPE_STATE_ACTIVE:
case USB_PIPE_STATE_ERROR:
break;
case USB_PIPE_STATE_CLOSED:
case USB_PIPE_STATE_CLOSING:
default:
break;
}
"usba_hold_ph_data: ph_impl=0x%p state=%d ref=%d",
}
return (ph_data);
}
void
{
if (ph_impl) {
"usba_release_ph_data: "
"ph_impl=0x%p state=%d ref=%d",
#ifndef __lock_lint
if (ph_impl->usba_ph_data) {
"usba_release_ph_data: req_count=%d",
}
#endif
}
}
/*
* get pipe state from ph_data
*/
{
return (pipe_state);
}
/*
* get ref_count from ph_data
*/
int
{
int ref_count;
return (ref_count);
}
/*
* new pipe state
* We need to hold both pipe mutex and ph_impl mutex
*/
void
{
"usba_pipe_new_state: "
"ph_data=0x%p old=%s new=%s ref=%d req=%d",
switch (ph_impl->usba_ph_state) {
case USB_PIPE_STATE_IDLE:
case USB_PIPE_STATE_ACTIVE:
case USB_PIPE_STATE_ERROR:
case USB_PIPE_STATE_CLOSED:
break;
case USB_PIPE_STATE_CLOSING:
default:
break;
}
}
/*
* async function execution support
* Arguments:
* dip - devinfo pointer
* sync_func - function to be executed
* ph_impl - impl pipehandle
* arg - opaque arg
* usb_flags - none
* callback - function to be called on completion, may be NULL
* callback_arg - argument for callback function
*
* Note: The caller must do a hold on ph_data
* We sleep for memory resources and taskq_dispatch which will ensure
* that this function succeeds
*/
int
int (*sync_func)(dev_info_t *,
void (*callback)(usb_pipe_handle_t,
usb_opaque_t, int, usb_cb_flags_t),
{
"usba_pipe_setup_func_call: ph_impl=0x%p, func=0x%p",
"usba_pipe_setup_func_call: async request with "
"no callback");
return (USB_INVALID_ARGS);
}
/*
* OR in sleep flag. regardless of calling sync_func directly
* or in a new thread, we will always wait for completion
*/
if (usb_flags & USB_FLAGS_SLEEP) {
} else if (usba_async_ph_req(ph_data,
"usb_async_req failed: ph_impl=0x%p, func=0x%p",
if (callback) {
}
}
return (rval);
}
/*
* taskq thread function to execute function synchronously
* Note: caller must have done a hold on ph_data
*/
static void
{
int rval;
USB_SUCCESS) {
"sync func failed (%d)", rval);
}
cb_flags);
}
}
/*
* default endpoint descriptor and pipe policy
*/
/* set some meaningful defaults */
/*
* usb_get_ep_index: create an index from endpoint address that can
* be used to index into endpoint pipe lists
*/
{
return ((ep_addr & USB_EP_NUM_MASK) +
}
/*
* pipe management
* utility functions to init and destroy a pipehandle
*/
static int
{
static unsigned int anon_instance = 0;
"usba_init_pipe_handle: "
"usba_device=0x%p ep=0x%x", (void *)usba_device,
/* just to keep warlock happy, there is no contention yet */
if (instance != -1) {
"USB_%s_%x_pipehndl_tq_%d",
} else {
"USB_%s_%x_pipehndl_tq_%d_",
}
/*
* Create a shared taskq.
*/
if (iface < 0) {
/* we own the device, use first entry */
iface = 0;
}
if (instance != -1) {
"USB_%s_%x_shared_tq_%d",
instance);
} else {
"USB_%s_%x_shared_tq_%d_",
}
1, /* Number threads. */
1, /* minalloc */
}
}
}
/* fix up the MaxPacketSize if it is the default endpoint descr */
"adjusting max packet size from %d to %d",
}
/* now update usba_ph_impl structure */
return (USB_SUCCESS);
}
static void
{
}
static void
{
int timeout;
"usba_destroy_pipe_handle: ph_data=0x%p", (void *)ph_data);
/* check for all activity to drain */
(ph_data->p_req_count == 0)) {
break;
}
}
/*
* set state to closed here so any other thread
* that is waiting for the CLOSED state will
* continue. Otherwise, taskq_destroy might deadlock
*/
ph_impl->usba_ph_ref_count = 0;
/*
* use system taskq to destroy ph's taskq to avoid
* deadlock
*/
(void) taskq_dispatch(system_taskq,
} else {
}
} else {
}
if (iface < 0) {
/* we own the device, use the first entry */
iface = 0;
}
curthread)) {
(void) taskq_dispatch(
TQ_SLEEP);
} else {
}
}
}
"usba_destroy_pipe_handle: destroying ph_data=0x%p",
(void *)ph_data);
/* destroy mutexes */
}
/*
* usba_drain_cbs:
* Drain the request callbacks on the pipe handle
*/
int
{
int timeout;
"usba_drain_cbs: ph_data=0x%p ref=%d req=%d cb=0x%x cr=%d",
if (USBA_IS_DEFAULT_PIPE(ph_data)) {
"no flushing on default pipe!");
flush_requests = 0;
}
}
if (flush_requests) {
/* flush all requests in the pipehandle queue */
while ((req_wrp = (usba_req_wrapper_t *)
}
}
/*
* wait for any callbacks in progress but don't wait for
* for queued requests on the default pipe
*/
(ph_data->p_req_count >
timeout++) {
}
"usba_drain_cbs done: ph_data=0x%p ref=%d req=%d",
if (timeout == usba_drain_timeout) {
"draining callbacks timed out!");
rval = USB_FAILURE;
}
return (rval);
}
/*
* usb_pipe_open():
*
* Before using any pipe including the default pipe, it should be opened
* using usb_pipe_open(). On a successful open, a pipe handle is returned
* for use in other usb_pipe_*() functions
*
* The default pipe can only be opened by the hub driver
*
* The bandwidth has been allocated and guaranteed on successful
*
* Only the default pipe can be shared. all other control pipes
* are excusively opened by default.
* A pipe policy and endpoint descriptor must always be provided
* except for default pipe
*
* Arguments:
* dip - devinfo ptr
* ep - endpoint descriptor pointer
* pipe_policy - pointer to pipe policy which provides hints on how
* the pipe will be used.
* flags - USB_FLAGS_SLEEP wait for resources
* to become available
* pipe_handle - a pipe handle pointer. On a successful open,
* a pipe_handle is returned in this pointer.
*
* Return values:
* USB_SUCCESS - open succeeded
* USB_FAILURE - unspecified open failure or pipe is already open
* USB_NO_RESOURCES - no resources were available to complete the open
* USB_* - refer to usbai.h
*/
int
{
int rval;
int kmflag;
"usb_pipe_open:\n\t"
"dip=0x%p ep=0x%p pp=0x%p uf=0x%x ph=0x%p",
(void *)pipe_handle);
return (USB_INVALID_ARGS);
}
return (USB_INVALID_CONTEXT);
}
"usb_pipe_open: null pipe policy");
return (USB_INVALID_ARGS);
}
/* is the device still connected? */
"usb_pipe_open: device has been removed");
return (USB_FAILURE);
}
/*
* if a null endpoint pointer was passed, use the default
* endpoint descriptor
*/
if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
"usb_pipe_open: not allowed to open def pipe");
return (USB_INVALID_PERM);
}
}
if (usb_flags & USB_FLAGS_SERIALIZED_CB) {
"usb_pipe_open: shared taskq not allowed with "
"ctrl or isoch pipe");
return (USB_INVALID_ARGS);
}
}
size = sizeof (usba_pipe_handle_data_t);
return (USB_NO_RESOURCES);
}
/* check if pipe is already open and if so fail */
if (ph_impl->usba_ph_data) {
"usb_pipe_open: pipe to ep %d already open", ep_index);
return (USB_BUSY);
}
if (usb_flags & USB_FLAGS_SERIALIZED_CB) {
}
/*
* allocate and initialize the pipe handle
*/
"usb_pipe_open: pipe init failed (%d)", rval);
return (rval);
}
/*
* ask the hcd to open the pipe
*/
usb_flags)) != USB_SUCCESS) {
*pipe_handle = NULL;
} else {
/* set the pipe state after a successful hcd open */
}
"usb_pipe_open: ph_impl=0x%p (0x%p)",
return (rval);
}
/*
*
* Close a pipe and release all resources and free the pipe_handle.
* Automatic polling, if active, will be terminated
*
* Arguments:
* dip - devinfo ptr
* pipehandle - pointer to pipehandle. The pipehandle will be
* zeroed on successful completion
* flags - USB_FLAGS_SLEEP:
* wait for resources, pipe
* to become free, all callbacks completed
* callback - If USB_FLAGS_SLEEP has not been specified, a
* callback will be performed.
* callback_arg - the first argument of the callback. Note that
* the pipehandle will be zeroed and not passed
*
* Notes:
* Pipe close will always succeed regardless whether USB_FLAGS_SLEEP has been
* specified or not.
* An async close will always succeed if the hint in the pipe policy
* has been correct about the max number of async taskq requests required.
* If there are really no resources, the pipe handle will be linked into
* a garbage pipe list and periodically checked by USBA until it can be
* closed. This may cause a hang in the detach of the driver.
* USBA will prevent the client from submitting more requests to a pipe
* that is being closed
* Subsequent usb_pipe_close() requests on the same pipe to USBA will
* wait for the previous close(s) to finish.
*
* Note that once we start closing a pipe, we cannot go back anymore
* to a normal pipe state
*/
void
void (*callback)(
int rval,
{
"usb_pipe_close: ph=0x%p", (void *)pipe_handle);
if (callback) {
} else {
"usb_pipe_close: invalid arguments");
}
return;
}
if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
/*
* It is the client driver doing the pipe close,
* the pipe is no longer persistent then.
*/
}
if (callback) {
} else {
"usb_pipe_close: invalid context");
}
return;
}
/* hold pipehandle anyways since we will decrement later */
return;
}
if (USBA_IS_DEFAULT_PIPE(ph_data) &&
((usb_flags & USBA_FLAGS_PRIVILEGED) == 0)) {
"usb_pipe_close: not allowed to close def pipe");
if (callback) {
} else {
"usb_pipe_close: invalid pipe");
}
return;
}
}
/*ARGSUSED*/
static int
{
int attribute;
int timeout;
return (USB_SUCCESS);
}
"usba_pipe_sync_close: dip=0x%p ph_data=0x%p state=%d ref=%d",
/*
* if another thread opens the pipe again, this loop could
* be truly forever
*/
/* wait forever till really closed */
delay(1);
}
return (USB_SUCCESS);
}
/*
* For control and bulk, we will drain till ref_count <= 1 and
* req_count == 0 but for isoc and intr IN, we can only wait
* till the ref_count === 1 as the req_count will never go to 0
*/
switch (attribute) {
case USB_EP_ATTR_CONTROL:
case USB_EP_ATTR_BULK:
if ((ph_data->p_req_count == 0) &&
goto done;
}
break;
case USB_EP_ATTR_INTR:
case USB_EP_ATTR_ISOCH:
if (dir == USB_EP_DIR_IN) {
goto done;
}
} else if ((ph_data->p_req_count == 0) &&
goto done;
}
break;
}
}
done:
if (timeout >= usba_drain_timeout) {
int draining_succeeded;
"timeout on draining requests, resetting pipe 0x%p",
(void *)ph_impl);
/* this MUST have succeeded */
"draining requests done");
}
usb_flags) != USB_SUCCESS) {
"usba_pipe_sync_close: hcd close failed");
/* carry on regardless! */
}
return (USB_SUCCESS);
}
/*
* usb_pipe_set_private:
* set private client date in the pipe handle
*/
int
{
"usb_pipe_set_private: ");
return (USB_INVALID_PIPE);
}
if (USBA_IS_DEFAULT_PIPE(ph_data)) {
return (USB_INVALID_PERM);
}
return (USB_SUCCESS);
}
/*
* usb_pipe_get_private:
* get private client date from the pipe handle
*/
{
"usb_pipe_get_private:");
return (NULL);
}
return (data);
}
/*
* usb_pipe_reset
* Arguments:
* dip - devinfo pointer
* pipe_handle - opaque pipe handle
* Returns:
* USB_SUCCESS - pipe successfully reset or request queued
* USB_FAILURE - undetermined failure
* USB_INVALID_PIPE - pipe is invalid or already closed
*/
void
void (*callback)(
int rval,
{
"usb_pipe_reset: dip=0x%p ph=0x%p uf=0x%x",
if (callback) {
} else {
"usb_pipe_reset: invalid arguments");
}
return;
}
if (callback) {
} else {
"usb_pipe_reset: invalid context");
}
return;
}
/* is this the default pipe? */
if (USBA_IS_DEFAULT_PIPE(ph_data)) {
if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
"usb_pipe_reset: not allowed to reset def pipe");
if (callback) {
} else {
"usb_pipe_reset: invalid pipe");
}
return;
}
}
(void) usba_pipe_setup_func_call(dip,
}
/*ARGSUSED*/
int
{
ph_impl);
"usba_pipe_sync_reset: dip=0x%p ph_data=0x%p uf=0x%x",
/*
* The host controller has stopped polling of the endpoint.
*/
/* this MUST have succeeded */
/*
* if there are requests still queued on the default pipe,
* start them now
*/
return (rval);
}
/*
* usba_pipe_clear:
* call hcd to clear pipe but don't wait for draining
*/
void
{
"usba_pipe_clear: ph_data=0x%p", (void *)ph_data);
return;
}
return;
}
if (USBA_IS_DEFAULT_PIPE(ph_data)) {
"no flushing on default pipe!");
flush_requests = 0;
}
}
if (flush_requests) {
/* flush all requests in the pipehandle queue */
while ((req_wrp = (usba_req_wrapper_t *)
}
}
}
/*
*
* usb_pipe_drain_reqs
* this function blocks until there are no more requests
* owned by this dip on the pipe
*
* Arguments:
* dip - devinfo pointer
* pipe_handle - opaque pipe handle
* timeout - timeout in seconds
* flags - USB_FLAGS_SLEEP:
* wait for completion.
* cb - if USB_FLAGS_SLEEP has not been specified
* this callback function will be called on
* completion. This callback may be NULL
* and no notification of completion will then
* be provided.
* cb_arg - 2nd argument to callback function.
*
* callback and callback_arg should be NULL if USB_FLAGS_SLEEP has
* been specified
*
* Returns:
* USB_SUCCESS - pipe successfully reset or request queued
* USB_FAILURE - timeout
* USB_* - refer to usbai.h
*/
int
void (*cb)(
int rval,
{
"usb_pipe_drain_reqs: dip=0x%p ph_data=0x%p tm=%d uf=0x%x",
return (USB_INVALID_PIPE);
}
return (USB_INVALID_ARGS);
}
return (USB_INVALID_CONTEXT);
}
return (USB_SUCCESS);
}
/*
* usba_pipe_sync_drain_reqs
* this function blocks until there are no more requests
* owned by this dip on the pipe
*
* Arguments:
* dip - devinfo pointer
* ph_impl - pipe impl handle
* timeout - timeout in seconds
* Returns:
* USB_SUCCESS - pipe successfully reset or request queued
* USB_FAILURE - timeout
* USB_* - see usbai.h
*/
/*ARGSUSED*/
int
{
ph_impl);
int i;
/* delay will be 10 ms */
"usba_pipe_sync_drain_reqs: "
"dip=0x%p ph_data=0x%p timeout=%d ref=%d req=%d",
/*
* for default pipe, we need to check the active request
* and the queue
* Note that a pipe reset on the default pipe doesn't flush
* the queue
* for all other pipes we just check ref and req count since
* these pipes are unshared
*/
if (USBA_IS_DEFAULT_PIPE(ph_data)) {
int found = 0;
int count = 0;
/* active_req_wrp is only for control pipes */
/* walk the queue */
req_wrp = (usba_req_wrapper_t *)
if (found) {
break;
}
count++;
}
if (found == 0) {
break;
}
}
"usb_pipe_sync_drain_reqs: "
"cnt=%d active_req_wrp=0x%p",
}
} else {
if (ph_data->p_req_count ||
} else {
break;
}
}
}
"usb_pipe_sync_drain_reqs: timeout=%d active_req_wrp=0x%p req=%d",
}
/*
* usba_persistent_pipe_open
* Open all the pipes marked persistent for this device
*/
int
{
int i;
"usba_persistent_pipe_open: usba_device=0x%p", (void *)usba_device);
if (usba_device != NULL) {
/* default pipe is the first one to be opened */
for (i = 0; (rval == USB_SUCCESS) &&
(i < USBA_N_ENDPOINTS); i++) {
ph_impl->usba_ph_flags &=
&pipe_handle);
"usba_persistent_pipe_open: "
"ep_index=%d, rval=%d", i, rval);
}
}
}
return (rval);
}
/*
* usba_persistent_pipe_close
* Close all pipes of this device and mark them persistent
*/
void
{
int i;
"usba_persistent_pipe_close: usba_device=0x%p",
(void *)usba_device);
if (usba_device != NULL) {
/* default pipe is the last one to be closed */
for (i = (USBA_N_ENDPOINTS - 1); i >= 0; i--) {
ph_impl->usba_ph_flags |=
}
}
}
}