1
/* $Id: VBoxUSB-solaris.c 33595 2010-10-29 10:35:00Z vboxsync $ */
3
* VirtualBox USB Client Driver, Solaris Hosts.
7
* Copyright (C) 2008 Oracle Corporation
9
* This file is part of VirtualBox Open Source Edition (OSE), as
10
* available from http://www.virtualbox.org. This file is free software;
11
* you can redistribute it and/or modify it under the terms of the GNU
12
* General Public License (GPL) as published by the Free Software
13
* Foundation, in version 2 as it comes in the "COPYING" file of the
14
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18
/*******************************************************************************
20
*******************************************************************************/
21
#include <VBox/version.h>
24
#include <VBox/cdefs.h>
26
#include <VBox/usblib-solaris.h>
28
#include <iprt/assert.h>
29
#include <iprt/initterm.h>
30
#include <iprt/semaphore.h>
32
#include <iprt/process.h>
33
#include <iprt/string.h>
34
#include <iprt/path.h>
35
#include <iprt/thread.h>
37
#define USBDRV_MAJOR_VER 2
38
#define USBDRV_MINOR_VER 0
39
#include <sys/usb/usba.h>
40
#include <sys/strsun.h>
41
#include "usbai_private.h"
42
#include <sys/archsystm.h>
46
/*******************************************************************************
47
* Defined Constants And Macros *
48
*******************************************************************************/
49
/** The module name. */
50
#define DEVICE_NAME "vboxusb"
51
/** The module description as seen in 'modinfo'. */
52
#define DEVICE_DESC_DRV "VirtualBox USB"
54
/** Endpoint states */
55
#define VBOXUSB_EP_INITIALIZED 0xa1fa1fa
56
#define VBOXUSB_EP_STATE_NONE RT_BIT(0)
57
#define VBOXUSB_EP_STATE_CLOSED RT_BIT(1)
58
#define VBOXUSB_EP_STATE_OPENED RT_BIT(2)
60
#define VBOXUSB_POLL_OFF RT_BIT(0)
61
#define VBOXUSB_POLL_ON RT_BIT(1)
62
#define VBOXUSB_POLL_REAP_PENDING RT_BIT(2)
63
#define VBOXUSB_POLL_DEV_UNPLUGGED RT_BIT(3)
65
/** -=-=-=-=-=-=- Standard Specifics -=-=-=-=-=-=- */
66
/** Max. supported endpoints */
67
#define VBOXUSB_MAX_ENDPOINTS 32
68
/** Size of USB Ctrl Xfer Header */
69
#define VBOXUSB_CTRL_XFER_SIZE 0x08
71
* USB2.0 (Sec. 9-13) Bits 10..0 is the max packet size; for high speed Isoc/Intr, bits 12..11 is
72
* number of additional transaction opportunities per microframe.
74
#define VBOXUSB_PKT_SIZE(pkt) (pkt & 0x07FF) * (1 + ((pkt >> 11) & 3))
75
/** Endpoint Xfer Type */
76
#define VBOXUSB_XFER_TYPE(endp) ((endp)->EpDesc.bmAttributes & USB_EP_ATTR_MASK)
77
/** Endpoint Xfer Direction */
78
#define VBOXUSB_XFER_DIR(endp) ((endp)->EpDesc.bEndpointAddress & USB_EP_DIR_IN)
80
/** -=-=-=-=-=-=- Tunable Parameters -=-=-=-=-=-=- */
81
/** Time to wait while draining inflight UBRs on suspend, in seconds. */
82
#define VBOXUSB_DRAIN_TIME 30
83
/** Ctrl Xfer timeout in seconds. */
84
#define VBOXUSB_CTRL_XFER_TIMEOUT 10
85
/** Bulk Xfer timeout in seconds. */
86
#define VBOXUSB_BULK_XFER_TIMEOUT 10
87
/** Intr Xfer timeout in seconds. */
88
#define VBOXUSB_INTR_XFER_TIMEOUT 10
89
/** Maximum URB queue length. */
90
#define VBOXUSB_URB_QUEUE_SIZE 64
91
/** Maximum asynchronous requests per pipe */
92
#define VBOXUSB_MAX_PIPE_ASYNC_REQS 2
94
#if defined(DEBUG_ramshankar)
98
# define LogFlow LogRel
100
# define LogFlowFunc LogRel
103
/** For enabling global symbols while debugging **/
104
#if defined(DEBUG_ramshankar)
107
# define LOCAL static
111
/*******************************************************************************
112
* Kernel Entry Hook *
113
*******************************************************************************/
114
int VBoxUSBSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
115
int VBoxUSBSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
116
int VBoxUSBSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
117
int VBoxUSBSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
118
int VBoxUSBSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal);
119
int VBoxUSBSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead);
120
int VBoxUSBSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
121
int VBoxUSBSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
122
int VBoxUSBSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
123
int VBoxUSBSolarisPower(dev_info_t *pDip, int Component, int Level);
126
/*******************************************************************************
127
* Structures and Typedefs *
128
*******************************************************************************/
130
* cb_ops: for drivers that support char/block entry points
132
static struct cb_ops g_VBoxUSBSolarisCbOps =
136
nodev, /* b strategy */
142
nodev, /* c devmap */
144
nodev, /* c segmap */
146
ddi_prop_op, /* property ops */
147
NULL, /* streamtab */
148
D_NEW | D_MP, /* compat. flag */
149
CB_REV, /* revision */
155
* dev_ops: for driver device operations
157
static struct dev_ops g_VBoxUSBSolarisDevOps =
159
DEVO_REV, /* driver build revision */
161
VBoxUSBSolarisGetInfo,
162
nulldev, /* identify */
164
VBoxUSBSolarisAttach,
165
VBoxUSBSolarisDetach,
167
&g_VBoxUSBSolarisCbOps,
170
ddi_quiesce_not_needed
174
* modldrv: export driver specifics to the kernel
176
static struct modldrv g_VBoxUSBSolarisModule =
178
&mod_driverops, /* extern from kernel */
179
DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
180
&g_VBoxUSBSolarisDevOps
184
* modlinkage: export install/remove/info to the kernel
186
static struct modlinkage g_VBoxUSBSolarisModLinkage =
189
&g_VBoxUSBSolarisModule,
194
* vboxusb_ep_t: Endpoint structure with info. for managing an endpoint.
196
typedef struct vboxusb_ep_t
198
uint_t fInitialized; /* Whether this Endpoint is initialized */
199
uint_t EpState; /* Endpoint state */
200
usb_ep_descr_t EpDesc; /* Endpoint descriptor */
201
uchar_t uCfgValue; /* Configuration value */
202
uchar_t uInterface; /* Interface number */
203
uchar_t uAlt; /* Alternate number */
204
usb_pipe_handle_t pPipe; /* Endpoint pipe handle */
205
usb_pipe_policy_t PipePolicy; /* Endpoint policy */
206
bool fIsocPolling; /* Whether Isoc. IN polling is enabled */
207
list_t hIsocInUrbs; /* Isoc. IN inflight URBs */
208
uint16_t cIsocInUrbs; /* Number of Isoc. IN inflight URBs */
209
list_t hIsocInLandedReqs; /* Isoc. IN landed requests */
210
uint16_t cbIsocInLandedReqs; /* Cumulative size of landed Isoc. IN requests */
211
size_t cbMaxIsocData; /* Maximum size of Isoc. IN landed buffer */
215
* vboxusb_isoc_req_t: Isoc IN. requests queued from device till they are reaped.
217
typedef struct vboxusb_isoc_req_t
219
mblk_t *pMsg; /* Pointer to the data buffer */
220
uint32_t cIsocPkts; /* Number of Isoc pkts */
221
VUSBISOC_PKT_DESC aIsocPkts[8]; /* Array of Isoc pkt descriptors */
222
list_node_t hListLink;
223
} vboxusb_isoc_req_t;
226
* VBOXUSB_URB_STATE: Internal USB URB state.
228
typedef enum VBOXUSB_URB_STATE
230
VBOXUSB_URB_STATE_FREE = 0x00,
231
VBOXUSB_URB_STATE_INFLIGHT = 0x04,
232
VBOXUSB_URB_STATE_LANDED = 0x08
236
* vboxusb_urb_t: kernel URB representation.
238
typedef struct vboxusb_urb_t
240
void *pvUrbR3; /* Userspace URB address (untouched, returned while reaping) */
241
uint8_t bEndpoint; /* Endpoint address */
242
VUSBXFERTYPE enmType; /* Xfer type */
243
VUSBDIRECTION enmDir; /* Xfer direction */
244
VUSBSTATUS enmStatus; /* URB status */
245
RTR3PTR pvDataR3; /* Userspace address of the original data buffer */
246
size_t cbDataR3; /* Size of the data buffer */
247
mblk_t *pMsg; /* Pointer to the data buffer */
248
uint32_t cIsocPkts; /* Number of Isoc pkts */
249
VUSBISOC_PKT_DESC aIsocPkts[8]; /* Array of Isoc pkt descriptors */
250
VBOXUSB_URB_STATE enmState; /* Whether free/in-flight etc. */
251
struct vboxusb_state_t *pState; /* Pointer to the device instance */
252
list_node_t hListLink; /* List node link handle */
256
* vboxusb_power_t: Per Device Power Management info.
258
typedef struct vboxusb_power_t
260
uint_t PowerStates; /* Bit mask of the power states */
261
int PowerBusy; /* Busy counter */
262
bool fPowerWakeup; /* Whether remote power wakeup is enabled */
263
bool fPowerRaise; /* Whether to raise the power level */
264
uint8_t PowerLevel; /* Current power level */
268
* vboxusb_state_t: Per Device instance state info.
270
typedef struct vboxusb_state_t
272
dev_info_t *pDip; /* Per instance device info. */
273
usb_client_dev_data_t *pDevDesc; /* Parsed & complete device descriptor */
274
uint8_t DevState; /* Current USB Device state */
275
bool fClosed; /* Whether the device (default control pipe) is closed */
276
bool fRestoreCfg; /* Whether we changed configs to restore while tearing down */
277
bool fGetCfgReqDone; /* First GET_CONFIG request has been circumvented */
278
kmutex_t Mtx; /* Mutex state protection */
279
usb_serialization_t StateMulti; /* State serialization */
280
size_t cbMaxBulkXfer; /* Maximum bulk xfer size */
281
vboxusb_ep_t aEps[VBOXUSB_MAX_ENDPOINTS]; /* All endpoints structures */
282
list_t hUrbs; /* Handle to list of free/inflight URBs */
283
list_t hLandedUrbs; /* Handle to list of landed URBs */
284
uint16_t cInflightUrbs; /* Number of inflight URBs. */
285
pollhead_t PollHead; /* Handle to pollhead for waking polling processes */
286
int fPoll; /* Polling status flag */
287
RTPROCESS Process; /* The process (id) of the session */
288
VBOXUSBREQ_CLIENT_INFO ClientInfo; /* Registration data */
289
vboxusb_power_t *pPower; /* Power Management */
293
/*******************************************************************************
294
* Internal Functions *
295
*******************************************************************************/
296
LOCAL int vboxUSBSolarisInitEndPoint(vboxusb_state_t *pState, usb_ep_data_t *pEpData, uchar_t uCfgValue,
297
uchar_t uInterface, uchar_t uAlt);
298
LOCAL int vboxUSBSolarisInitAllEndPoints(vboxusb_state_t *pState);
299
LOCAL int vboxUSBSolarisInitEndPointsForConfig(vboxusb_state_t *pState, uint8_t uCfgIndex);
300
LOCAL int vboxUSBSolarisInitEndPointsForInterfaceAlt(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt);
301
LOCAL void vboxUSBSolarisDestroyAllEndPoints(vboxusb_state_t *pState);
302
LOCAL void vboxUSBSolarisDestroyEndPoint(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
303
LOCAL void vboxUSBSolarisCloseAllPipes(vboxusb_state_t *pState, bool fControlPipe);
304
LOCAL void vboxUSBSolarisCloseInterface(vboxusb_state_t *pState, uint8_t bInterface);
305
LOCAL int vboxUSBSolarisOpenPipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
306
LOCAL void vboxUSBSolarisClosePipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
307
LOCAL int vboxUSBSolarisCtrlXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
308
LOCAL void vboxUSBSolarisCtrlXferCompleted(usb_pipe_handle_t pPipe, usb_ctrl_req_t *pReq);
309
LOCAL int vboxUSBSolarisBulkXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *purb);
310
LOCAL void vboxUSBSolarisBulkXferCompleted(usb_pipe_handle_t pPipe, usb_bulk_req_t *pReq);
311
LOCAL int vboxUSBSolarisIntrXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
312
LOCAL void vboxUSBSolarisIntrXferCompleted(usb_pipe_handle_t pPipe, usb_intr_req_t *pReq);
313
LOCAL int vboxUSBSolarisIsocXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
314
LOCAL void vboxUSBSolarisIsocInXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
315
LOCAL void vboxUSBSolarisIsocInXferError(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
316
LOCAL void vboxUSBSolarisIsocOutXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
317
LOCAL vboxusb_urb_t *vboxUSBSolarisGetIsocInURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq);
318
LOCAL vboxusb_urb_t *vboxUSBSolarisQueueURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, mblk_t *pMsg);
319
LOCAL inline void vboxUSBSolarisConcatMsg(vboxusb_urb_t *pUrb);
320
LOCAL inline void vboxUSBSolarisDeQueueURB(vboxusb_urb_t *pUrb, int URBStatus);
321
LOCAL inline void vboxUSBSolarisNotifyComplete(vboxusb_state_t *pState);
322
LOCAL int vboxUSBSolarisProcessIOCtl(int iFunction, void *pvState, int Mode, PVBOXUSBREQ pUSBReq, void *pvBuf, size_t *pcbDataOut);
323
LOCAL bool vboxUSBSolarisIsUSBDevice(dev_info_t *pDip);
325
/** Device Operation Hooks */
326
LOCAL int vboxUSBSolarisSendURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode);
327
LOCAL int vboxUSBSolarisReapURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode);
328
LOCAL int vboxUSBSolarisClearEndPoint(vboxusb_state_t *pState, uint8_t bEndpoint);
329
LOCAL int vboxUSBSolarisSetConfig(vboxusb_state_t *pState, uint8_t bCfgValue);
330
LOCAL int vboxUSBSolarisGetConfig(vboxusb_state_t *pState, uint8_t *pCfgValue);
331
LOCAL int vboxUSBSolarisSetInterface(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt);
332
LOCAL int vboxUSBSolarisCloseDevice(vboxusb_state_t *pState, VBOXUSB_RESET_LEVEL enmReset);
333
LOCAL int vboxUSBSolarisAbortPipe(vboxusb_state_t *pState, uint8_t bEndpoint);
334
LOCAL bool vboxUSBSolarisIsAnyPipeOpen(vboxusb_state_t *pState);
335
LOCAL int vboxUSBSolarisGetConfigIndex(vboxusb_state_t *pState, uint_t uCfgValue);
337
/** Hotplug & Power Management Hooks */
338
LOCAL inline void vboxUSBSolarisNotifyHotplug(vboxusb_state_t *pState);
339
LOCAL int vboxUSBSolarisDeviceDisconnected(dev_info_t *pDip);
340
LOCAL int vboxUSBSolarisDeviceReconnected(dev_info_t *pDip);
342
LOCAL int vboxUSBSolarisInitPower(vboxusb_state_t *pState);
343
LOCAL void vboxUSBSolarisDestroyPower(vboxusb_state_t *pState);
344
LOCAL int vboxUSBSolarisDeviceSuspend(vboxusb_state_t *pState);
345
LOCAL void vboxUSBSolarisDeviceResume(vboxusb_state_t *pState);
346
LOCAL void vboxUSBSolarisDeviceRestore(vboxusb_state_t *pState);
347
LOCAL void vboxUSBSolarisPowerBusy(vboxusb_state_t *pState);
348
LOCAL void vboxUSBSolarisPowerIdle(vboxusb_state_t *pState);
351
int VBoxUSBMonSolarisRegisterClient(dev_info_t *pClientDip, PVBOXUSB_CLIENT_INFO pClientInfo);
352
int VBoxUSBMonSolarisUnregisterClient(dev_info_t *pClientDip);
355
/*******************************************************************************
357
*******************************************************************************/
358
/** Global list of all device instances. */
359
static void *g_pVBoxUSBSolarisState;
361
/** The default endpoint descriptor */
362
static usb_ep_descr_t g_VBoxUSBSolarisDefaultEpDesc = {7, 5, 0, USB_EP_ATTR_CONTROL, 8, 0};
364
/** Hotplug events */
365
static usb_event_t g_VBoxUSBSolarisEvents =
367
vboxUSBSolarisDeviceDisconnected,
368
vboxUSBSolarisDeviceReconnected,
369
NULL, /* presuspend */
370
NULL /* postresume */
375
* Kernel entry points
379
LogFlowFunc((DEVICE_NAME ":_init\n"));
382
* Prevent module autounloading.
384
modctl_t *pModCtl = mod_getctl(&g_VBoxUSBSolarisModLinkage);
386
pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
388
LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
391
* Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
393
int rc = RTR0Init(0);
396
rc = ddi_soft_state_init(&g_pVBoxUSBSolarisState, sizeof(vboxusb_state_t), 4 /* pre-alloc */);
399
rc = mod_install(&g_VBoxUSBSolarisModLinkage);
403
LogRel((DEVICE_NAME ":mod_install failed! rc=%d\n", rc));
404
ddi_soft_state_fini(&g_pVBoxUSBSolarisState);
407
LogRel((DEVICE_NAME ":failed to initialize soft state.\n"));
412
LogRel((DEVICE_NAME ":RTR0Init failed! rc=%d\n", rc));
413
return RTErrConvertToErrno(rc);
421
LogFlowFunc((DEVICE_NAME ":_fini\n"));
423
rc = mod_remove(&g_VBoxUSBSolarisModLinkage);
426
ddi_soft_state_fini(&g_pVBoxUSBSolarisState);
434
int _info(struct modinfo *pModInfo)
436
LogFlowFunc((DEVICE_NAME ":_info\n"));
438
return mod_info(&g_VBoxUSBSolarisModLinkage, pModInfo);
443
* Attach entry point, to attach a device to the system or resume it.
445
* @param pDip The module structure instance.
446
* @param enmCmd Attach type (ddi_attach_cmd_t)
448
* @returns corresponding solaris error code.
450
int VBoxUSBSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
452
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisAttach pDip=%p enmCmd=%d\n", pDip, enmCmd));
455
int instance = ddi_get_instance(pDip);
456
vboxusb_state_t *pState = NULL;
462
rc = ddi_soft_state_zalloc(g_pVBoxUSBSolarisState, instance);
463
if (rc == DDI_SUCCESS)
465
pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
469
pState->pDevDesc = NULL;
470
pState->fClosed = false;
471
pState->fRestoreCfg = false;
472
pState->fGetCfgReqDone = false;
473
bzero(pState->aEps, sizeof(pState->aEps));
474
list_create(&pState->hUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
475
list_create(&pState->hLandedUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
476
pState->cInflightUrbs = 0;
477
pState->fPoll = VBOXUSB_POLL_OFF;
478
pState->Process = NIL_RTPROCESS;
479
pState->pPower = NULL;
482
* There is a bug in usb_client_attach() as of Nevada 120 which panics when we bind to
483
* a non-USB device. So check if we are really binding to a USB device or not.
485
if (vboxUSBSolarisIsUSBDevice(pState->pDip))
488
* Here starts the USB specifics.
490
rc = usb_client_attach(pState->pDip, USBDRV_VERSION, 0);
491
if (rc == USB_SUCCESS)
494
* Parse out the entire descriptor.
496
rc = usb_get_dev_data(pState->pDip, &pState->pDevDesc, USB_PARSE_LVL_ALL, 0 /* Unused */);
497
if (rc == USB_SUCCESS)
499
#ifdef DEBUG_ramshankar
500
usb_print_descr_tree(pState->pDip, pState->pDevDesc);
504
* Initialize state locks.
506
mutex_init(&pState->Mtx, NULL, MUTEX_DRIVER, pState->pDevDesc->dev_iblock_cookie);
507
pState->StateMulti = usb_init_serialization(pState->pDip, USB_INIT_SER_CHECK_SAME_THREAD);
510
* Get maximum bulk transfer size supported by the HCD.
512
rc = usb_pipe_get_max_bulk_transfer_size(pState->pDip, &pState->cbMaxBulkXfer);
513
if (rc == USB_SUCCESS)
515
LogFlow((DEVICE_NAME ":VBoxUSBSolarisAttach cbMaxBulkXfer=%d\n", pState->cbMaxBulkXfer));
518
* Initialize all endpoints.
520
rc = vboxUSBSolarisInitAllEndPoints(pState);
524
* Set the device state.
526
pState->DevState = USB_DEV_ONLINE;
529
* Initialize power management for the device.
531
rc = vboxUSBSolarisInitPower(pState);
535
* Update endpoints (descriptors) for the current config.
537
vboxUSBSolarisInitEndPointsForConfig(pState, usb_get_current_cfgidx(pState->pDip));
540
* Publish the minor node.
542
rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0,
543
"none", "none", 0666);
544
if (RT_LIKELY(rc == DDI_SUCCESS))
547
* Register hotplug callbacks.
549
rc = usb_register_event_cbs(pState->pDip, &g_VBoxUSBSolarisEvents, 0 /* flags */);
550
if (RT_LIKELY(rc == USB_SUCCESS))
553
* Register with our monitor driver.
555
bzero(&pState->ClientInfo, sizeof(pState->ClientInfo));
556
char szDevicePath[MAXPATHLEN];
557
ddi_pathname(pState->pDip, szDevicePath);
558
RTStrPrintf(pState->ClientInfo.achClientPath, sizeof(pState->ClientInfo.achClientPath),
562
RTPathStripFilename(szDevicePath);
563
RTStrPrintf(pState->ClientInfo.achDeviceIdent, sizeof(pState->ClientInfo.achDeviceIdent),
565
pState->pDevDesc->dev_descr->idVendor,
566
pState->pDevDesc->dev_descr->idProduct,
567
pState->pDevDesc->dev_descr->bcdDevice,
569
pState->ClientInfo.Instance = instance;
570
rc = VBoxUSBMonSolarisRegisterClient(pState->pDip, &pState->ClientInfo);
575
LogRel((DEVICE_NAME ":VBoxUSBMonSolarisRegisterClient failed! rc=%d path=%s instance=%d\n",
576
rc, pState->ClientInfo.achClientPath, instance));
579
usb_unregister_event_cbs(pState->pDip, &g_VBoxUSBSolarisEvents);
582
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to register hotplug callbacks! rc=%d\n", rc));
584
ddi_remove_minor_node(pState->pDip, NULL);
587
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach ddi_create_minor_node failed! rc=%d\n", rc));
590
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to initialize power management! rc=%d\n", rc));
593
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach vboxUSBSolarisInitAllEndPoints failed! rc=%d\n"));
596
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach usb_pipe_get_max_bulk_transfer_size failed! rc=%d\n", rc));
598
usb_fini_serialization(pState->StateMulti);
599
mutex_destroy(&pState->Mtx);
600
usb_free_dev_data(pState->pDip, pState->pDevDesc);
603
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to get device descriptor. rc=%d\n", rc));
605
usb_client_detach(pState->pDip, NULL);
608
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach usb_client_attach failed! rc=%d\n", rc));
611
LogFlow((DEVICE_NAME ":VBoxUSBSolarisAttach not a USB device.\n")); /* This would appear on every boot if it were Rel */
614
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to get soft state\n", sizeof(*pState)));
616
ddi_soft_state_free(g_pVBoxUSBSolarisState, instance);
619
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to alloc soft state. rc=%d\n", rc));
626
pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
627
if (RT_UNLIKELY(!pState))
629
LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach DDI_RESUME: failed to get soft state on detach.\n"));
633
vboxUSBSolarisDeviceResume(pState);
644
* Detach entry point, to detach a device to the system or suspend it.
646
* @param pDip The module structure instance.
647
* @param enmCmd Attach type (ddi_attach_cmd_t)
649
* @returns corresponding solaris error code.
651
int VBoxUSBSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
653
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisDetach pDip=%p enmCmd=%d\n", pDip, enmCmd));
655
int instance = ddi_get_instance(pDip);
656
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
657
if (RT_UNLIKELY(!pState))
659
LogRel((DEVICE_NAME ":VBoxUSBSolarisDetach failed to get soft state on detach.\n"));
668
* At this point it must be assumed that the default control pipe has
669
* already been closed by userland (via VBoxUSBSolarisClose() entry point).
670
* Once it's closed we can no longer open or reference the device here.
674
* Notify userland if any that we're gone (while resetting device held by us).
676
vboxUSBSolarisNotifyHotplug(pState);
679
* Unregister hotplug callback events first without holding the mutex as the callbacks
680
* would otherwise block on the mutex.
682
usb_unregister_event_cbs(pDip, &g_VBoxUSBSolarisEvents);
686
* Serialize: paranoid; drain other driver activity.
688
usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
689
usb_release_access(pState->StateMulti);
690
mutex_enter(&pState->Mtx);
693
* Close all endpoints.
695
vboxUSBSolarisCloseAllPipes(pState, true /* ControlPipe */);
698
* Deinitialize power, destroy endpoints.
700
vboxUSBSolarisDestroyPower(pState);
701
vboxUSBSolarisDestroyAllEndPoints(pState);
706
vboxusb_urb_t *pUrb = NULL;
707
while ((pUrb = list_remove_head(&pState->hUrbs)) != NULL)
714
while ((pUrb = list_remove_head(&pState->hLandedUrbs)) != NULL)
720
pState->cInflightUrbs = 0;
721
list_destroy(&pState->hUrbs);
722
list_destroy(&pState->hLandedUrbs);
725
* Destroy locks, free up descriptor and detach from USBA.
727
mutex_exit(&pState->Mtx);
728
usb_fini_serialization(pState->StateMulti);
729
mutex_destroy(&pState->Mtx);
731
usb_free_dev_data(pState->pDip, pState->pDevDesc);
732
usb_client_detach(pState->pDip, NULL);
735
* Deregister with our Monitor driver.
737
VBoxUSBMonSolarisUnregisterClient(pState->pDip);
739
ddi_remove_minor_node(pState->pDip, NULL);
741
ddi_soft_state_free(g_pVBoxUSBSolarisState, instance);
749
int rc = vboxUSBSolarisDeviceSuspend(pState);
763
* Info entry point, called by solaris kernel for obtaining driver info.
765
* @param pDip The module structure instance (do not use).
766
* @param enmCmd Information request type.
767
* @param pvArg Type specific argument.
768
* @param ppvResult Where to store the requested info.
770
* @returns corresponding solaris error code.
772
int VBoxUSBSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
774
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisGetInfo\n"));
776
vboxusb_state_t *pState = NULL;
777
int instance = getminor((dev_t)pvArg);
781
case DDI_INFO_DEVT2DEVINFO:
784
* One is to one mapping of instance & minor number as we publish only one minor node per device.
786
pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
789
*ppvResult = (void *)pState->pDip;
793
LogRel((DEVICE_NAME ":VBoxUSBSolarisGetInfo failed to get device state.\n"));
797
case DDI_INFO_DEVT2INSTANCE:
799
*ppvResult = (void *)(uintptr_t)instance;
809
int VBoxUSBSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
811
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisOpen pDev=%p fFlag=%d fType=%d pCred=%p\n", pDev, fFlag, fType, pCred));
814
* Verify we are being opened as a character device
816
if (fType != OTYP_CHR)
820
* One is to one mapping. (Minor<=>Instance).
822
int instance = getminor((dev_t)*pDev);
823
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
826
LogRel((DEVICE_NAME ":VBoxUSBSolarisOpen failed to get device state for instance %d\n", instance));
831
* Only one user process can open a device instance at a time.
833
if (pState->Process != NIL_RTPROCESS)
835
LogRel((DEVICE_NAME ":VBoxUSBSolarisOpen a process is already using this device instance.\n"));
839
pState->Process = RTProcSelf();
840
pState->fPoll = VBOXUSB_POLL_ON;
849
int VBoxUSBSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred)
851
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisClose Dev=%d fFlag=%d fType=%d pCred=%p\n", Dev, fFlag, fType, pCred));
853
int instance = getminor((dev_t)Dev);
854
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
855
if (RT_UNLIKELY(!pState))
857
LogRel((DEVICE_NAME ":VBoxUSBSolarisClose failed to get device state for instance %d\n", instance));
861
mutex_enter(&pState->Mtx);
862
pState->fPoll = VBOXUSB_POLL_OFF;
863
pState->Process = NIL_RTPROCESS;
864
mutex_exit(&pState->Mtx);
870
int VBoxUSBSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
872
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisRead\n"));
877
int VBoxUSBSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
879
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisWrite\n"));
884
int VBoxUSBSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
886
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisPoll Dev=%d fEvents=%d fAnyYet=%d pReqEvents=%p\n", Dev, fEvents, fAnyYet, pReqEvents));
889
* Get the device state (one to one mapping).
891
int instance = getminor((dev_t)Dev);
892
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
893
if (RT_UNLIKELY(!pState))
895
LogRel((DEVICE_NAME ":VBoxUSBSolarisPoll: no state data for %d\n", instance));
899
mutex_enter(&pState->Mtx);
902
* "fEvents" HAS to be POLLIN. We won't bother to test it. The caller
903
* must always requests input events. Disconnect event (POLLHUP) is invalid in "fEvents".
906
if (pState->fPoll & VBOXUSB_POLL_DEV_UNPLUGGED)
909
pState->fPoll &= ~VBOXUSB_POLL_DEV_UNPLUGGED;
912
if (pState->fPoll & VBOXUSB_POLL_REAP_PENDING)
915
pState->fPoll &= ~VBOXUSB_POLL_REAP_PENDING;
921
*ppPollHead = &pState->PollHead;
924
*pReqEvents = fEvents;
926
mutex_exit(&pState->Mtx);
932
int VBoxUSBSolarisPower(dev_info_t *pDip, int Component, int Level)
934
LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisPower pDip=%p Component=%d Level=%d\n", pDip, Component, Level));
936
int instance = ddi_get_instance(pDip);
937
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
938
if (RT_UNLIKELY(!pState))
940
LogRel((DEVICE_NAME ":VBoxUSBSolarisPower Failed! missing state.\n"));
947
usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
948
mutex_enter(&pState->Mtx);
950
int rc = USB_FAILURE;
951
if (pState->DevState == USB_DEV_ONLINE)
954
* Check if we are transitioning to a valid power state.
956
if (!USB_DEV_PWRSTATE_OK(pState->pPower->PowerStates, Level))
960
case USB_DEV_OS_PWR_OFF:
962
if (pState->pPower->PowerBusy)
968
pState->pPower->PowerLevel = USB_DEV_OS_PWR_OFF;
969
mutex_exit(&pState->Mtx);
970
rc = usb_set_device_pwrlvl3(pDip);
971
mutex_enter(&pState->Mtx);
975
case USB_DEV_OS_FULL_PWR:
978
* Can happen during shutdown of the OS.
980
pState->pPower->PowerLevel = USB_DEV_OS_FULL_PWR;
981
mutex_exit(&pState->Mtx);
982
rc = usb_set_device_pwrlvl0(pDip);
983
mutex_enter(&pState->Mtx);
987
default: /* Power levels 1, 2 not implemented */
992
LogFlow((DEVICE_NAME ":USB_DEV_PWRSTATE_OK failed.\n"));
997
mutex_exit(&pState->Mtx);
998
usb_release_access(pState->StateMulti);
999
return rc == USB_SUCCESS ? DDI_SUCCESS : DDI_FAILURE;
1003
/** @def IOCPARM_LEN
1004
* Gets the length from the ioctl number.
1005
* This is normally defined by sys/ioccom.h on BSD systems...
1008
# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
1011
int VBoxUSBSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
1013
/* LogFlowFunc((DEVICE_NAME ":VBoxUSBSolarisIOCtl Dev=%d Cmd=%d pArg=%p Mode=%d\n", Dev, Cmd, pArg)); */
1016
* Get the device state (one to one mapping).
1018
int instance = getminor((dev_t)Dev);
1019
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1020
if (RT_UNLIKELY(!pState))
1022
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: no state data for %d\n", instance));
1027
* Read the request wrapper.
1030
if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
1032
LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd), sizeof(ReqWrap)));
1036
int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
1037
if (RT_UNLIKELY(rc))
1039
LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%d.\n", pArg, Cmd, rc));
1043
if (ReqWrap.u32Magic != VBOXUSB_MAGIC)
1045
LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: bad magic %#x; pArg=%p Cmd=%d.\n", ReqWrap.u32Magic, pArg, Cmd));
1048
if (RT_UNLIKELY( ReqWrap.cbData == 0
1049
|| ReqWrap.cbData > _1M*16))
1051
LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: bad size %#x; pArg=%p Cmd=%d.\n", ReqWrap.cbData, pArg, Cmd));
1058
void *pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
1059
if (RT_UNLIKELY(!pvBuf))
1061
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap.cbData));
1065
rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
1066
if (RT_UNLIKELY(rc))
1068
RTMemTmpFree(pvBuf);
1069
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: ddi_copyin failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
1072
if (RT_UNLIKELY( ReqWrap.cbData == 0
1075
RTMemTmpFree(pvBuf);
1076
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: invalid request pvBuf=%p cbData=%d\n", pvBuf, ReqWrap.cbData));
1081
* Process the IOCtl.
1084
rc = vboxUSBSolarisProcessIOCtl(Cmd, pState, Mode, &ReqWrap, pvBuf, &cbDataOut);
1088
if (RT_UNLIKELY(cbDataOut > ReqWrap.cbData))
1090
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: too much output data %d expected %d Truncating!\n", cbDataOut, ReqWrap.cbData));
1091
cbDataOut = ReqWrap.cbData;
1094
ReqWrap.cbData = cbDataOut;
1097
* Copy VBOXUSBREQ back to userspace (which contains rc for USB operation).
1099
rc = ddi_copyout(&ReqWrap, (void *)pArg, sizeof(ReqWrap), Mode);
1103
* Copy payload (if any) back to userspace.
1107
rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataOut, Mode);
1108
if (RT_UNLIKELY(rc))
1110
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: ddi_copyout failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
1117
LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: ddi_copyout(1)failed; pReqWrap=%p pArg=%p Cmd=%d. rc=%d\n", &ReqWrap, pArg, Cmd, rc));
1122
RTMemTmpFree(pvBuf);
1128
* IOCtl processor for user to kernel and kernel to kernel communication.
1130
* @returns VBox status code.
1132
* @param iFunction The requested function.
1133
* @param pvState The USB device instance.
1134
* @param Mode The IOCtl mode.
1135
* @param pUSBReq Pointer to the VBOXUSB request.
1136
* @param pvBuf Pointer to the ring-3 URB.
1137
* @param pcbDataOut Where to store the IOCtl OUT data size.
1139
LOCAL int vboxUSBSolarisProcessIOCtl(int iFunction, void *pvState, int Mode, PVBOXUSBREQ pUSBReq, void *pvBuf, size_t *pcbDataOut)
1141
// LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl iFunction=%d pvState=%p pUSBReq=%p\n", iFunction, pvState, pUSBReq));
1143
AssertPtrReturn(pvState, VERR_INVALID_PARAMETER);
1144
vboxusb_state_t *pState = (vboxusb_state_t *)pvState;
1145
size_t cbData = pUSBReq->cbData;
1148
#define CHECKRET_MIN_SIZE(mnemonic, cbMin) \
1150
if (cbData < (cbMin)) \
1152
LogRel((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: " mnemonic ": cbData=%#zx (%zu) min is %#zx (%zu)\n", \
1153
cbData, cbData, (size_t)(cbMin), (size_t)(cbMin))); \
1154
return VERR_BUFFER_OVERFLOW; \
1156
if ((cbMin) != 0 && !VALID_PTR(pvBuf)) \
1158
LogRel((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: " mnemonic ": Invalid pointer %p\n", pvBuf)); \
1159
return VERR_INVALID_PARAMETER; \
1165
case VBOXUSB_IOCTL_SEND_URB:
1167
CHECKRET_MIN_SIZE("SEND_URB", sizeof(VBOXUSBREQ_URB));
1169
PVBOXUSBREQ_URB pUrbReq = (PVBOXUSBREQ_URB)pvBuf;
1170
rc = vboxUSBSolarisSendURB(pState, pUrbReq, Mode);
1172
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: SEND_URB returned %d\n", rc));
1176
case VBOXUSB_IOCTL_REAP_URB:
1178
CHECKRET_MIN_SIZE("REAP_URB", sizeof(VBOXUSBREQ_URB));
1180
PVBOXUSBREQ_URB pUrbReq = (PVBOXUSBREQ_URB)pvBuf;
1181
rc = vboxUSBSolarisReapURB(pState, pUrbReq, Mode);
1182
*pcbDataOut = sizeof(VBOXUSBREQ_URB);
1183
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: REAP_URB returned %d\n", rc));
1187
case VBOXUSB_IOCTL_CLEAR_EP:
1189
CHECKRET_MIN_SIZE("CLEAR_EP", sizeof(VBOXUSBREQ_CLEAR_EP));
1191
PVBOXUSBREQ_CLEAR_EP pClearEpReq = (PVBOXUSBREQ_CLEAR_EP)pvBuf;
1192
rc = vboxUSBSolarisClearEndPoint(pState, pClearEpReq->bEndpoint);
1194
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: CLEAR_EP returned %d\n", rc));
1198
case VBOXUSB_IOCTL_SET_CONFIG:
1200
CHECKRET_MIN_SIZE("SET_CONFIG", sizeof(VBOXUSBREQ_SET_CONFIG));
1202
PVBOXUSBREQ_SET_CONFIG pSetCfgReq = (PVBOXUSBREQ_SET_CONFIG)pvBuf;
1203
rc = vboxUSBSolarisSetConfig(pState, pSetCfgReq->bConfigValue);
1205
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: SET_CONFIG returned %d\n", rc));
1209
case VBOXUSB_IOCTL_SET_INTERFACE:
1211
CHECKRET_MIN_SIZE("SET_INTERFACE", sizeof(VBOXUSBREQ_SET_INTERFACE));
1213
PVBOXUSBREQ_SET_INTERFACE pSetInterfaceReq = (PVBOXUSBREQ_SET_INTERFACE)pvBuf;
1214
rc = vboxUSBSolarisSetInterface(pState, pSetInterfaceReq->bInterface, pSetInterfaceReq->bAlternate);
1216
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: SET_INTERFACE returned %d\n", rc));
1220
case VBOXUSB_IOCTL_CLOSE_DEVICE:
1222
CHECKRET_MIN_SIZE("CLOSE_DEVICE", sizeof(VBOXUSBREQ_CLOSE_DEVICE));
1224
PVBOXUSBREQ_CLOSE_DEVICE pCloseDeviceReq = (PVBOXUSBREQ_CLOSE_DEVICE)pvBuf;
1225
rc = vboxUSBSolarisCloseDevice(pState, pCloseDeviceReq->ResetLevel);
1227
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: CLOSE_DEVICE returned %d\n", rc));
1231
case VBOXUSB_IOCTL_ABORT_PIPE:
1233
CHECKRET_MIN_SIZE("ABORT_PIPE", sizeof(VBOXUSBREQ_ABORT_PIPE));
1235
PVBOXUSBREQ_ABORT_PIPE pAbortPipeReq = (PVBOXUSBREQ_ABORT_PIPE)pvBuf;
1236
rc = vboxUSBSolarisAbortPipe(pState, pAbortPipeReq->bEndpoint);
1238
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: ABORT_PIPE returned %d\n", rc));
1242
case VBOXUSB_IOCTL_GET_CONFIG:
1244
CHECKRET_MIN_SIZE("GET_CONFIG", sizeof(VBOXUSBREQ_GET_CONFIG));
1246
PVBOXUSBREQ_GET_CONFIG pGetCfgReq = (PVBOXUSBREQ_GET_CONFIG)pvBuf;
1247
rc = vboxUSBSolarisGetConfig(pState, &pGetCfgReq->bConfigValue);
1248
*pcbDataOut = sizeof(VBOXUSBREQ_GET_CONFIG);
1249
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: GET_CONFIG returned %d\n", rc));
1253
case VBOXUSB_IOCTL_GET_VERSION:
1255
CHECKRET_MIN_SIZE("GET_VERSION", sizeof(VBOXUSBREQ_GET_VERSION));
1257
PVBOXUSBREQ_GET_VERSION pGetVersionReq = (PVBOXUSBREQ_GET_VERSION)pvBuf;
1258
pGetVersionReq->u32Major = VBOXUSB_VERSION_MAJOR;
1259
pGetVersionReq->u32Minor = VBOXUSB_VERSION_MINOR;
1260
*pcbDataOut = sizeof(VBOXUSBREQ_GET_VERSION);
1262
LogFlow((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: GET_VERSION returned %d\n", rc));
1268
LogRel((DEVICE_NAME ":solarisUSBProcessIOCtl: Unknown request %#x\n", iFunction));
1269
rc = VERR_NOT_SUPPORTED;
1275
pUSBReq->cbData = *pcbDataOut;
1281
* Initialize device power management functions.
1283
* @param pState The USB device instance.
1285
* @returns VBox status code.
1287
LOCAL int vboxUSBSolarisInitPower(vboxusb_state_t *pState)
1289
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisInitPower pState=%p\n", pState));
1291
int rc = usb_handle_remote_wakeup(pState->pDip, USB_REMOTE_WAKEUP_ENABLE);
1292
if (rc == USB_SUCCESS)
1294
vboxusb_power_t *pPower = RTMemAlloc(sizeof(vboxusb_power_t));
1295
if (RT_LIKELY(pPower))
1297
mutex_enter(&pState->Mtx);
1298
pState->pPower = pPower;
1299
pState->pPower->fPowerWakeup = false;
1300
mutex_exit(&pState->Mtx);
1303
rc = usb_create_pm_components(pState->pDip, &PowerStates);
1304
if (rc == USB_SUCCESS)
1306
pState->pPower->fPowerWakeup = true;
1307
pState->pPower->PowerLevel = USB_DEV_OS_FULL_PWR;
1308
pState->pPower->PowerStates = PowerStates;
1310
rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
1312
if (rc != DDI_SUCCESS)
1314
LogRel((DEVICE_NAME ":vboxUSBSolarisInitPower failed to raise power level usb(%#x,%#x).\n",
1315
pState->pDevDesc->dev_descr->idVendor, pState->pDevDesc->dev_descr->idProduct));
1319
LogFlow((DEVICE_NAME ":vboxUSBSolarisInitPower failed to create power components.\n"));
1321
return VINF_SUCCESS;
1324
rc = VERR_NO_MEMORY;
1328
LogFlow((DEVICE_NAME ":vboxUSBSolarisInitPower failed to enable remote wakeup. No PM.\n"));
1337
* Destroy device power management functions.
1339
* @param pState The USB device instance.
1340
* @remarks Requires the device state mutex to be held.
1342
* @returns VBox status code.
1344
LOCAL void vboxUSBSolarisDestroyPower(vboxusb_state_t *pState)
1346
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDestroyPower pState=%p\n", pState));
1350
mutex_exit(&pState->Mtx);
1351
vboxUSBSolarisPowerBusy(pState);
1352
mutex_enter(&pState->Mtx);
1355
if ( pState->pPower->fPowerWakeup
1356
&& pState->DevState != USB_DEV_DISCONNECTED)
1358
mutex_exit(&pState->Mtx);
1359
rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
1360
if (rc != DDI_SUCCESS)
1361
LogFlow((DEVICE_NAME ":vboxUSBSolarisDestroyPower raising power failed! rc=%d\n", rc));
1363
rc = usb_handle_remote_wakeup(pState->pDip, USB_REMOTE_WAKEUP_DISABLE);
1364
if (rc != DDI_SUCCESS)
1365
LogFlow((DEVICE_NAME ":vboxUSBSolarisDestroyPower failed to disable remote wakeup.\n"));
1368
mutex_exit(&pState->Mtx);
1370
rc = pm_lower_power(pState->pDip, 0 /* component */, USB_DEV_OS_PWR_OFF);
1371
if (rc != DDI_SUCCESS)
1372
LogFlow((DEVICE_NAME ":vboxUSBSolarisDestroyPower lowering power failed! rc=%d\n", rc));
1374
vboxUSBSolarisPowerIdle(pState);
1375
mutex_enter(&pState->Mtx);
1376
RTMemFree(pState->pPower);
1377
pState->pPower = NULL;
1383
* Convert Solaris' USBA URB status to VBox's USB URB status.
1385
* @param Status Solaris USBA USB URB status.
1387
* @returns VBox USB URB status.
1389
static inline VUSBSTATUS vboxUSBSolarisGetUrbStatus(usb_cr_t Status)
1393
case USB_CR_OK: return VUSBSTATUS_OK;
1394
case USB_CR_CRC: return VUSBSTATUS_CRC;
1395
case USB_CR_DEV_NOT_RESP: return VUSBSTATUS_DNR;
1396
case USB_CR_DATA_UNDERRUN: return VUSBSTATUS_DATA_UNDERRUN;
1397
case USB_CR_DATA_OVERRUN: return VUSBSTATUS_DATA_OVERRUN;
1398
case USB_CR_STALL: return VUSBSTATUS_STALL;
1400
case USB_CR_BITSTUFFING:
1401
case USB_CR_DATA_TOGGLE_MM:
1402
case USB_CR_PID_CHECKFAILURE:
1403
case USB_CR_UNEXP_PID:
1404
case USB_CR_BUFFER_OVERRUN:
1405
case USB_CR_BUFFER_UNDERRUN:
1406
case USB_CR_TIMEOUT:
1407
case USB_CR_NOT_ACCESSED:
1408
case USB_CR_NO_RESOURCES:
1409
case USB_CR_UNSPECIFIED_ERR:
1410
case USB_CR_STOPPED_POLLING:
1411
case USB_CR_PIPE_CLOSING:
1412
case USB_CR_PIPE_RESET:
1413
case USB_CR_NOT_SUPPORTED:
1414
case USB_CR_FLUSHED:
1415
case USB_CR_HC_HARDWARE_ERR:
1417
default: return VUSBSTATUS_INVALID;
1423
* Convert Solaris' USBA error code to VBox's error code.
1425
* @param UsbRc Solaris USBA error code.
1427
* @returns VBox error code.
1429
static inline int vboxUSBSolarisToVBoxRC(int UsbRc)
1433
case USB_SUCCESS: return VINF_SUCCESS;
1434
case USB_INVALID_ARGS: return VERR_INVALID_PARAMETER;
1435
case USB_INVALID_PIPE: return VERR_BAD_PIPE;
1436
case USB_INVALID_CONTEXT: return VERR_INVALID_CONTEXT;
1437
case USB_BUSY: return VERR_PIPE_BUSY;
1438
case USB_PIPE_ERROR: return VERR_PIPE_IO_ERROR;
1441
case USB_NO_RESOURCES:
1442
case USB_NO_BANDWIDTH:
1443
case USB_NOT_SUPPORTED:
1444
case USB_PIPE_ERROR:
1445
case USB_NO_FRAME_NUMBER:
1446
case USB_INVALID_START_FRAME:
1447
case USB_HC_HARDWARE_ERROR:
1448
case USB_INVALID_REQUEST:
1449
case USB_INVALID_VERSION:
1450
case USB_INVALID_PERM:
1452
default: return VERR_GENERAL_FAILURE;
1458
* Convert Solaris' USBA device state to VBox's error code.
1460
* @param UsbRc Solaris USBA error code.
1462
* @returns VBox error code.
1464
static inline int vboxUSBSolarisDeviceState(uint8_t uDeviceState)
1466
switch (uDeviceState)
1468
case USB_DEV_ONLINE: return VINF_SUCCESS;
1469
case USB_DEV_SUSPENDED: return VERR_VUSB_DEVICE_IS_SUSPENDED;
1470
case USB_DEV_DISCONNECTED:
1471
case USB_DEV_PWRED_DOWN: return VERR_VUSB_DEVICE_NOT_ATTACHED;
1472
default: return VERR_GENERAL_FAILURE;
1478
* Check if the device is a USB device.
1480
* @param pDip Pointer to this device info. structure.
1482
* @returns If this is really a USB device returns true, otherwise false.
1484
LOCAL bool vboxUSBSolarisIsUSBDevice(dev_info_t *pDip)
1486
int rc = DDI_FAILURE;
1489
* Check device for "usb" compatible property, root hubs->device would likely mean parent has no "usb" property.
1491
char **ppszCompatible = NULL;
1493
rc = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, pDip, DDI_PROP_DONTPASS, "compatible", &ppszCompatible, &cCompatible);
1494
if (RT_LIKELY(rc == DDI_PROP_SUCCESS))
1496
while (cCompatible--)
1498
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice compatible[%d]=%s\n", cCompatible, ppszCompatible[cCompatible]));
1499
if (!strncmp(ppszCompatible[cCompatible], "usb", 3))
1501
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice verified device as USB. pszCompatible=%s\n", ppszCompatible[cCompatible]));
1502
ddi_prop_free(ppszCompatible);
1507
ddi_prop_free(ppszCompatible);
1508
ppszCompatible = NULL;
1511
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice USB property lookup failed. rc=%d\n", rc));
1514
* Check parent for "usb" compatible property.
1516
dev_info_t *pParentDip = ddi_get_parent(pDip);
1519
rc = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, pParentDip, DDI_PROP_DONTPASS, "compatible", &ppszCompatible, &cCompatible);
1520
if (RT_LIKELY(rc == DDI_PROP_SUCCESS))
1522
while (cCompatible--)
1524
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice parent compatible[%d]=%s\n", cCompatible, ppszCompatible[cCompatible]));
1525
if (!strncmp(ppszCompatible[cCompatible], "usb", 3))
1527
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice verified device as USB. parent pszCompatible=%s\n",
1528
ppszCompatible[cCompatible]));
1529
ddi_prop_free(ppszCompatible);
1534
ddi_prop_free(ppszCompatible);
1535
ppszCompatible = NULL;
1538
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice USB parent property lookup failed. rc=%d\n", rc));
1541
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice failed to obtain parent device for property lookup.\n"));
1550
* @param pState The USB device instance.
1551
* @param pUrb Pointer to the VBox USB URB.
1552
* @param Mode The IOCtl mode.
1554
* @returns VBox error code.
1556
LOCAL int vboxUSBSolarisSendURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode)
1558
uchar_t EndPtIndex = usb_get_ep_index(pUrbReq->bEndpoint);
1559
vboxusb_ep_t *pEp = &pState->aEps[EndPtIndex];
1560
AssertPtrReturn(pEp, VERR_INVALID_POINTER);
1562
// LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisSendUrb pState=%p pUrbReq=%p bEndpoint=%#x[%d] enmDir=%#x enmType=%#x cbData=%d pvData=%p\n",
1563
// pState, pUrbReq, pUrbReq->bEndpoint, EndPtIndex, pUrbReq->enmDir, pUrbReq->enmType, pUrbReq->cbData, pUrbReq->pvData));
1565
if (RT_UNLIKELY(!pUrbReq->pvData))
1567
LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb Invalid request. No data.\n"));
1568
return VERR_INVALID_POINTER;
1572
* Allocate message block & copy userspace buffer for host to device Xfers and for
1573
* Control Xfers (since input has Setup header that needs copying).
1575
mblk_t *pMsg = NULL;
1576
int rc = VINF_SUCCESS;
1577
if ( pUrbReq->enmDir == VUSBDIRECTION_OUT
1578
|| pUrbReq->enmType == VUSBXFERTYPE_MSG)
1580
pMsg = allocb(pUrbReq->cbData, BPRI_HI);
1581
if (RT_UNLIKELY(!pMsg))
1583
LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb: failed to allocate %d bytes\n", pUrbReq->cbData));
1584
return VERR_NO_MEMORY;
1587
rc = ddi_copyin(pUrbReq->pvData, pMsg->b_wptr, pUrbReq->cbData, Mode);
1588
if (RT_UNLIKELY(rc))
1590
LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb: ddi_copyin failed! rc=%d\n", rc));
1592
return VERR_NO_MEMORY;
1595
pMsg->b_wptr += pUrbReq->cbData;
1598
mutex_enter(&pState->Mtx);
1599
rc = vboxUSBSolarisDeviceState(pState->DevState);
1601
if (pState->fClosed) /* Required for Isoc. IN Xfers which don't Xfer through the pipe after polling starts */
1602
rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
1607
* Open the pipe if needed.
1609
rc = vboxUSBSolarisOpenPipe(pState, pEp);
1610
if (RT_UNLIKELY(RT_FAILURE(rc)))
1612
mutex_exit(&pState->Mtx);
1614
LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb OpenPipe failed. pState=%p pUrbReq=%p bEndpoint=%#x enmDir=%#x enmType=%#x cbData=%d pvData=%p rc=%d\n",
1615
pState, pUrbReq, pUrbReq->bEndpoint, pUrbReq->enmDir, pUrbReq->enmType, pUrbReq->cbData, pUrbReq->pvData, rc));
1616
return VERR_BAD_PIPE;
1619
mutex_exit(&pState->Mtx);
1621
vboxusb_urb_t *pUrb = NULL;
1622
if ( pUrbReq->enmType == VUSBXFERTYPE_ISOC
1623
&& pUrbReq->enmDir == VUSBDIRECTION_IN)
1624
pUrb = vboxUSBSolarisGetIsocInURB(pState, pUrbReq);
1626
pUrb = vboxUSBSolarisQueueURB(pState, pUrbReq, pMsg);
1628
if (RT_LIKELY(pUrb))
1630
switch (pUrb->enmType)
1632
case VUSBXFERTYPE_MSG:
1634
rc = vboxUSBSolarisCtrlXfer(pState, pEp, pUrb);
1638
case VUSBXFERTYPE_BULK:
1640
rc = vboxUSBSolarisBulkXfer(pState, pEp, pUrb);
1644
case VUSBXFERTYPE_INTR:
1646
rc = vboxUSBSolarisIntrXfer(pState, pEp, pUrb);
1650
case VUSBXFERTYPE_ISOC:
1652
rc = vboxUSBSolarisIsocXfer(pState, pEp, pUrb);
1658
rc = VERR_NOT_SUPPORTED;
1665
freemsg(pUrb->pMsg);
1667
if ( pUrb->enmType == VUSBXFERTYPE_ISOC
1668
&& pUrb->enmDir == VUSBDIRECTION_IN)
1676
pUrb->enmState = VBOXUSB_URB_STATE_FREE;
1682
LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb failed to queue URB.\n"));
1683
rc = VERR_NO_MEMORY;
1689
if ( pUrb->enmType != VUSBXFERTYPE_ISOC
1690
|| pUrb->enmDir != VUSBDIRECTION_IN)
1692
mutex_enter(&pState->Mtx);
1693
pState->cInflightUrbs--;
1694
mutex_exit(&pState->Mtx);
1700
mutex_exit(&pState->Mtx);
1709
* Reap a completed/error'd URB.
1711
* @param pState The USB device instance.
1712
* @param pUrbReq Pointer to the VBox USB URB.
1713
* @param Mode The IOCtl mode.
1715
* @returns VBox error code.
1717
LOCAL int vboxUSBSolarisReapURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode)
1719
// LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisReapUrb pState=%p pUrbReq=%p\n", pState, pUrbReq));
1721
AssertPtrReturn(pUrbReq, VERR_INVALID_POINTER);
1723
int rc = VINF_SUCCESS;
1724
mutex_enter(&pState->Mtx);
1725
rc = vboxUSBSolarisDeviceState(pState->DevState);
1726
if (pState->fClosed)
1727
rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
1730
vboxusb_urb_t *pUrb = list_remove_head(&pState->hLandedUrbs);
1731
mutex_exit(&pState->Mtx);
1735
* Copy the URB which will then be copied to user-space.
1737
pUrbReq->pvUrbR3 = pUrb->pvUrbR3;
1738
pUrbReq->bEndpoint = pUrb->bEndpoint;
1739
pUrbReq->enmType = pUrb->enmType;
1740
pUrbReq->enmDir = pUrb->enmDir;
1741
pUrbReq->enmStatus = pUrb->enmStatus;
1743
if (RT_LIKELY(pUrb->pMsg))
1746
* Chain copy the message back into the user buffer.
1748
if (RT_LIKELY(pUrb->pvDataR3 != NIL_RTR3PTR))
1750
size_t cbData = RT_MIN(msgdsize(pUrb->pMsg), pUrb->cbDataR3);
1751
pUrbReq->cbData = cbData;
1752
pUrbReq->pvData = (void *)pUrb->pvDataR3;
1755
* Paranoia: we should have a single message block almost always.
1757
if (RT_LIKELY(!pUrb->pMsg->b_cont && cbData > 0))
1759
rc = ddi_copyout(pUrb->pMsg->b_rptr, (void *)pUrbReq->pvData, cbData, Mode);
1760
if (RT_UNLIKELY(rc != 0))
1762
LogRel((DEVICE_NAME ":vboxUSBSolarisReapUrb ddi_copyout failed! rc=%d\n", rc));
1763
pUrbReq->enmStatus = VUSBSTATUS_INVALID;
1768
RTR3PTR pvDataR3 = pUrb->pvDataR3;
1769
mblk_t *pMsg = pUrb->pMsg;
1772
size_t cbMsg = MBLKL(pMsg);
1775
rc = ddi_copyout(pMsg->b_rptr, (void *)pvDataR3, cbMsg, Mode);
1776
if (RT_UNLIKELY(rc != 0))
1778
LogRel((DEVICE_NAME ":vboxUSBSolarisReapUrb ddi_copyout (2) failed! rc=%d\n", rc));
1779
pUrbReq->enmStatus = VUSBSTATUS_INVALID;
1784
pMsg = pMsg->b_cont;
1786
if ((pvDataR3 - pUrb->pvDataR3) >= cbData)
1791
LogFlow((DEVICE_NAME ":vboxUSBSolarisReapUrb pvUrbR3=%p pvDataR3=%p cbData=%d\n", pUrbReq->pvUrbR3, pUrbReq->pvData, pUrbReq->cbData));
1795
pUrbReq->cbData = 0;
1796
rc = VERR_INVALID_POINTER;
1797
LogFlow((DEVICE_NAME ":vboxUSBSolarisReapUrb missing pvDataR3!!\n"));
1801
* Free buffer allocated in VBOXUSB_IOCTL_SEND_URB.
1803
freemsg(pUrb->pMsg);
1808
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
1810
if (pUrb->enmDir == VUSBDIRECTION_OUT)
1811
pUrbReq->cbData = pUrb->cbDataR3;
1814
pUrbReq->enmStatus = VUSBSTATUS_INVALID;
1815
pUrbReq->cbData = 0;
1820
LogFlow((DEVICE_NAME ":vboxUSBSolarisReapUrb missing message.\n"));
1821
pUrbReq->cbData = 0;
1826
* Copy Isoc packet descriptors.
1828
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
1830
AssertCompile(sizeof(pUrbReq->aIsocPkts) == sizeof(pUrb->aIsocPkts));
1831
pUrbReq->cIsocPkts = pUrb->cIsocPkts;
1833
for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
1835
pUrbReq->aIsocPkts[i].cbPkt = pUrb->aIsocPkts[i].cbPkt;
1836
pUrbReq->aIsocPkts[i].cbActPkt = pUrb->aIsocPkts[i].cbActPkt;
1837
pUrbReq->aIsocPkts[i].enmStatus = pUrb->aIsocPkts[i].enmStatus;
1840
bcopy(pUrb->aIsocPkts, pUrbReq->aIsocPkts, pUrb->cIsocPkts * sizeof(VUSBISOC_PKT_DESC));
1843
if (pUrb->enmDir == VUSBDIRECTION_IN)
1853
* Add URB back to the head of the free/inflight list.
1856
pUrb->pvDataR3 = NIL_RTR3PTR;
1857
pUrb->enmState = VBOXUSB_URB_STATE_FREE;
1858
mutex_enter(&pState->Mtx);
1859
list_insert_head(&pState->hUrbs, pUrb);
1860
mutex_exit(&pState->Mtx);
1864
pUrbReq->pvUrbR3 = NULL;
1867
mutex_exit(&pState->Mtx);
1869
LogFlow((DEVICE_NAME ":vboxUSBSolarisReapUrb returns %d\n", rc));
1875
* Clear a pipe (CLEAR_FEATURE).
1877
* @param pState The USB device instance.
1878
* @param bEndpoint The Endpoint address.
1880
* @returns VBox error code.
1882
LOCAL int vboxUSBSolarisClearEndPoint(vboxusb_state_t *pState, uint8_t bEndpoint)
1884
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisClearEndPoint pState=%p bEndpoint=%#x\n", pState, bEndpoint));
1887
* Serialize access: single threaded per Endpoint, one request at a time.
1889
mutex_enter(&pState->Mtx);
1890
int rc = vboxUSBSolarisDeviceState(pState->DevState);
1893
uchar_t EndPtIndex = usb_get_ep_index(bEndpoint);
1894
vboxusb_ep_t *pEp = &pState->aEps[EndPtIndex];
1898
* Check if the endpoint is open to be cleared.
1902
mutex_exit(&pState->Mtx);
1905
* Asynchronous clear pipe.
1907
rc = usb_clr_feature(pState->pDip, USB_DEV_REQ_RCPT_EP, USB_EP_HALT, bEndpoint,
1908
USB_FLAGS_NOSLEEP, /* Asynchronous */
1909
NULL, /* Completion callback */
1910
NULL); /* Exception callback */
1913
* Synchronous reset pipe.
1915
usb_pipe_reset(pState->pDip, pEp->pPipe,
1916
USB_FLAGS_SLEEP, /* Synchronous */
1917
NULL, /* Completion callback */
1918
NULL); /* Exception callback */
1920
mutex_enter(&pState->Mtx);
1922
LogFlow((DEVICE_NAME ":vboxUSBSolarisClearEndPoint bEndpoint=%#x[%d] returns %d\n", bEndpoint, EndPtIndex, rc));
1928
LogFlow((DEVICE_NAME ":vboxUSBSolarisClearEndPoint not opened to be cleared. Faking success. bEndpoint=%#x.\n", bEndpoint));
1934
LogRel((DEVICE_NAME ":vboxUSBSolarisClearEndPoint Endpoint missing!! bEndpoint=%#x EndPtIndex=%d.\n", bEndpoint, EndPtIndex));
1935
rc = VERR_GENERAL_FAILURE;
1939
LogFlow((DEVICE_NAME ":vboxUSBSolarisClearEndPoint device state=%d not online.\n", pState->DevState));
1941
mutex_exit(&pState->Mtx);
1947
* Set configuration (SET_CONFIGURATION)
1949
* @param pState The USB device instance.
1950
* @param uCfgValue The Configuration value.
1952
* @returns VBox error code.
1954
LOCAL int vboxUSBSolarisSetConfig(vboxusb_state_t *pState, uint8_t bCfgValue)
1956
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisSetConfig pState=%p bCfgValue=%#x\n", pState, bCfgValue));
1959
* Serialize access: single threaded per Endpoint, one request at a time.
1961
mutex_enter(&pState->Mtx);
1962
int rc = vboxUSBSolarisDeviceState(pState->DevState);
1965
vboxUSBSolarisCloseAllPipes(pState, false /* ControlPipe */);
1966
int iCfgIndex = vboxUSBSolarisGetConfigIndex(pState, bCfgValue);
1971
* Switch Config synchronously.
1973
mutex_exit(&pState->Mtx);
1974
rc = usb_set_cfg(pState->pDip, (uint_t)iCfgIndex, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback data */);
1975
mutex_enter(&pState->Mtx);
1977
if (rc == USB_SUCCESS)
1979
pState->fRestoreCfg = true;
1980
vboxUSBSolarisInitEndPointsForConfig(pState, iCfgIndex);
1985
LogRel((DEVICE_NAME ":vboxUSBSolarisSetConfig usb_set_cfg failed for iCfgIndex=%#x bCfgValue=%#x rc=%d\n",
1986
iCfgIndex, bCfgValue, rc));
1987
rc = vboxUSBSolarisToVBoxRC(rc);
1992
LogRel((DEVICE_NAME ":vboxUSBSolarisSetConfig invalid iCfgIndex=%d bCfgValue=%#x\n", iCfgIndex, bCfgValue));
1993
rc = VERR_INVALID_HANDLE;
1997
mutex_exit(&pState->Mtx);
2004
* Get configuration (GET_CONFIGURATION)
2006
* @param pState The USB device instance.
2007
* @param pCfgValue Where to store the configuration value.
2009
* @returns VBox error code.
2011
LOCAL int vboxUSBSolarisGetConfig(vboxusb_state_t *pState, uint8_t *pCfgValue)
2013
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisGetConfig pState=%p pCfgValue=%p\n", pState, pCfgValue));
2014
AssertPtrReturn(pCfgValue, VERR_INVALID_POINTER);
2017
* Solaris keeps the currently active configuration for the first time. Thus for the first request
2018
* we simply pass the cached configuration back to the user.
2020
if (!pState->fGetCfgReqDone)
2022
pState->fGetCfgReqDone = true;
2023
AssertPtrReturn(pState->pDevDesc, VERR_GENERAL_FAILURE);
2024
usb_cfg_data_t *pCurrCfg = pState->pDevDesc->dev_curr_cfg;
2027
*pCfgValue = pCurrCfg->cfg_descr.bConfigurationValue;
2028
LogFlow((DEVICE_NAME ":vboxUSBSolarisGetConfig cached config returned. CfgValue=%d\n", *pCfgValue));
2029
return VINF_SUCCESS;
2034
* Get Config synchronously.
2037
int rc = usb_get_cfg(pState->pDip, &bCfgValue, USB_FLAGS_SLEEP);
2038
if (RT_LIKELY(rc == USB_SUCCESS))
2040
*pCfgValue = bCfgValue;
2045
LogRel((DEVICE_NAME ":vboxUSBSolarisGetConfig failed. rc=%d\n", rc));
2046
rc = vboxUSBSolarisToVBoxRC(rc);
2049
LogFlow((DEVICE_NAME ":vboxUSBSolarisGetConfig returns %d CfgValue=%d\n", rc, *pCfgValue));
2055
* Set interface (SET_INTERFACE)
2057
* @param pState The USB device instance.
2058
* @param uInterface The Interface number.
2059
* @param uAlt The Alternate setting number.
2061
* @returns VBox error code.
2063
LOCAL int vboxUSBSolarisSetInterface(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt)
2065
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisSetInterface pState=%p uInterface=%#x uAlt=%#x\n", pState, uInterface, uAlt));
2068
* Serialize access: single threaded per Endpoint, one request at a time.
2070
mutex_enter(&pState->Mtx);
2071
int rc = vboxUSBSolarisDeviceState(pState->DevState);
2074
vboxUSBSolarisCloseAllPipes(pState, false /* ControlPipe */);
2077
* Set Interface & Alt setting synchronously.
2079
mutex_exit(&pState->Mtx);
2080
rc = usb_set_alt_if(pState->pDip, uInterface, uAlt, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback data */);
2081
mutex_enter(&pState->Mtx);
2083
if (rc == USB_SUCCESS)
2085
vboxUSBSolarisInitEndPointsForInterfaceAlt(pState, uInterface, uAlt);
2090
LogRel((DEVICE_NAME ":vboxUSBSolarisSetInterface usb_set_alt_if failed for uInterface=%#x bAlt=%#x rc=%d\n",
2091
uInterface, uAlt, rc));
2092
rc = vboxUSBSolarisToVBoxRC(rc);
2096
mutex_exit(&pState->Mtx);
2103
* Close the USB device and reset it if required.
2105
* @param pState The USB device instance.
2106
* @param ResetLevel The reset level.
2108
* @returns VBox error code.
2110
LOCAL int vboxUSBSolarisCloseDevice(vboxusb_state_t *pState, VBOXUSB_RESET_LEVEL enmReset)
2112
LogFlow((DEVICE_NAME ":vboxUSBSolarisCloseDevice pState=%p enmReset=%d\n", pState, enmReset));
2115
* Serialize access: single threaded per Endpoint, one request at a time.
2117
mutex_enter(&pState->Mtx);
2118
int rc = vboxUSBSolarisDeviceState(pState->DevState);
2120
if (enmReset == VBOXUSB_RESET_LEVEL_NONE)
2122
vboxUSBSolarisCloseAllPipes(pState, true /* ControlPipe */);
2123
pState->fClosed = true;
2126
vboxUSBSolarisCloseAllPipes(pState, false /* ControlPipe */);
2129
mutex_exit(&pState->Mtx);
2135
case VBOXUSB_RESET_LEVEL_REATTACH:
2136
rc = usb_reset_device(pState->pDip, USB_RESET_LVL_REATTACH);
2139
case VBOXUSB_RESET_LEVEL_SOFT:
2140
rc = usb_reset_device(pState->pDip, USB_RESET_LVL_DEFAULT);
2148
rc = vboxUSBSolarisToVBoxRC(rc);
2151
LogFlow((DEVICE_NAME ":vboxUSBSolarisCloseDevice returns %d\n", rc));
2157
* Abort pending requests and reset the pipe.
2159
* @param pState The USB device instance.
2160
* @param bEndpoint The Endpoint address.
2162
* @returns VBox error code.
2164
LOCAL int vboxUSBSolarisAbortPipe(vboxusb_state_t *pState, uint8_t bEndpoint)
2166
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisAbortPipe pState=%p bEndpoint=%#x\n", pState, bEndpoint));
2169
* Serialize access: single threaded per Endpoint, one request at a time.
2171
mutex_enter(&pState->Mtx);
2172
int rc = vboxUSBSolarisDeviceState(pState->DevState);
2175
uchar_t EndPtIndex = usb_get_ep_index(bEndpoint);
2176
vboxusb_ep_t *pEp = &pState->aEps[EndPtIndex];
2182
* Default Endpoint; aborting requests not supported, fake success.
2184
if ((pEp->EpDesc.bEndpointAddress & USB_EP_NUM_MASK) == 0)
2186
mutex_exit(&pState->Mtx);
2187
LogRel((DEVICE_NAME ":vboxUSBSolarisAbortPipe Cannot reset control pipe.\n"));
2188
return VERR_NOT_SUPPORTED;
2192
* Serialize access: single threaded per Endpoint, one request at a time.
2194
mutex_exit(&pState->Mtx);
2195
usb_pipe_reset(pState->pDip, pEp->pPipe,
2196
USB_FLAGS_SLEEP, /* Synchronous */
2197
NULL, /* Completion callback */
2198
NULL); /* Callback data */
2201
* Allow pending async requests to complete.
2203
rc = usb_pipe_drain_reqs(pState->pDip, pEp->pPipe,
2204
USB_FLAGS_SLEEP, /* Synchronous */
2205
5, /* Timeout (seconds) */
2206
NULL, /* Completion callback */
2207
NULL); /* Callback data*/
2209
mutex_enter(&pState->Mtx);
2211
LogFlow((DEVICE_NAME ":usb_pipe_drain_reqs returns %d\n", rc));
2212
rc = vboxUSBSolarisToVBoxRC(rc);
2216
LogRel((DEVICE_NAME ":vboxUSBSolarisAbortPipe pipe not open. bEndpoint=%#x\n", bEndpoint));
2217
rc = VERR_PIPE_IO_ERROR;
2222
LogRel((DEVICE_NAME ":vboxUSBSolarisAbortPipe invalid pipe index %d bEndpoint=%#x\n", EndPtIndex, bEndpoint));
2223
rc = VERR_INVALID_HANDLE;
2227
mutex_exit(&pState->Mtx);
2229
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisAbortPipe returns %d\n", rc));
2235
* Initialize an endpoint.
2237
* @param pState The USB device instance.
2238
* @param pEpData The Endpoint data.
2239
* @param uCfgValue The Configuration value.
2240
* @param uCfgIndex The Configuration index.
2241
* @param uInterface The Interface.
2242
* @param uAlt The Alternate setting.
2244
* @returns VBox error code.
2246
LOCAL int vboxUSBSolarisInitEndPoint(vboxusb_state_t *pState, usb_ep_data_t *pEpData, uchar_t uCfgValue,
2247
uchar_t uInterface, uchar_t uAlt)
2249
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisInitEndPoint pState=%p pEpData=%p CfgVal=%d Iface=%d Alt=%d", pState,
2250
pEpData, uCfgValue, uInterface, uAlt));
2253
* Is this the default endpoint?
2255
usb_ep_descr_t *pEpDesc = NULL;
2256
vboxusb_ep_t *pEp = NULL;
2261
pEpDesc = &g_VBoxUSBSolarisDefaultEpDesc;
2265
EpIndex = usb_get_ep_index(pEpData->ep_descr.bEndpointAddress);
2266
pEpDesc = &pEpData->ep_descr;
2269
pEp = &pState->aEps[EpIndex];
2273
* Initialize the endpoint data structure.
2275
pEp->EpDesc = *pEpDesc;
2276
pEp->uCfgValue = uCfgValue;
2277
pEp->uInterface = uInterface;
2279
if (pEp->fInitialized != VBOXUSB_EP_INITIALIZED)
2282
pEp->EpState = VBOXUSB_EP_STATE_CLOSED;
2283
bzero(&pEp->PipePolicy, sizeof(pEp->PipePolicy));
2284
pEp->PipePolicy.pp_max_async_reqs = VBOXUSB_MAX_PIPE_ASYNC_REQS;
2285
pEp->fIsocPolling = false;
2286
list_create(&pEp->hIsocInUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
2287
pEp->cIsocInUrbs = 0;
2288
list_create(&pEp->hIsocInLandedReqs, sizeof(vboxusb_isoc_req_t), offsetof(vboxusb_isoc_req_t, hListLink));
2289
pEp->cbIsocInLandedReqs = 0;
2290
pEp->cbMaxIsocData = 0;
2291
pEp->fInitialized = VBOXUSB_EP_INITIALIZED;
2293
LogFlow((DEVICE_NAME ":vboxUSBSolarisInitEndPoint done. %s:[%d] bEndpoint=%#x\n", !pEpData ? "Default " : "Endpoint",
2294
EpIndex, pEp->EpDesc.bEndpointAddress));
2295
return VINF_SUCCESS;
2300
* Initialize all Endpoint structures.
2302
* @param pState The USB device instance.
2304
* @returns VBox status code.
2306
LOCAL int vboxUSBSolarisInitAllEndPoints(vboxusb_state_t *pState)
2308
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisInitAllEndPoints pState=%p\n", pState));
2311
* Initialize all Endpoints for all Alternate settings of all Interfaces of all Configs.
2313
int rc = vboxUSBSolarisInitEndPoint(pState, NULL /* pEp */, 0 /* uCfgValue */, 0 /* uInterface */, 0 /* uAlt */);
2318
* Initialize all Endpoints for all Alternate settings of all Interfaces of all Configs.
2320
for (uchar_t uCfgIndex = 0; uCfgIndex < pState->pDevDesc->dev_n_cfg; uCfgIndex++)
2322
rc = vboxUSBSolarisInitEndPointsForConfig(pState, uCfgIndex);
2325
LogRel((DEVICE_NAME ":vboxUSBSolarisInitAllEndPoints: vboxUSBSolarisInitEndPoints uCfgIndex=%d failed. rc=%d\n", uCfgIndex, rc));
2331
LogRel((DEVICE_NAME ":vboxUSBSolarisInitAllEndPoints default Endpoint initialization failed!\n"));
2338
* Initialize Endpoints structures for the given Config.
2340
* @param pState The USB device instance.
2341
* @param uCfgIndex The current Config. index.
2343
* @returns VBox status code.
2345
LOCAL int vboxUSBSolarisInitEndPointsForConfig(vboxusb_state_t *pState, uint8_t uCfgIndex)
2347
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForConfig pState=%p uCfgIndex=%d\n", pState, uCfgIndex));
2348
usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[uCfgIndex];
2349
uchar_t uCfgValue = pConfig->cfg_descr.bConfigurationValue;
2351
for (uchar_t uInterface = 0; uInterface < pConfig->cfg_n_if; uInterface++)
2353
usb_if_data_t *pInterface = &pConfig->cfg_if[uInterface];
2355
for (uchar_t uAlt = 0; uAlt < pInterface->if_n_alt; uAlt++)
2357
usb_alt_if_data_t *pAlt = &pInterface->if_alt[uAlt];
2359
for (uchar_t uEp = 0; uEp < pAlt->altif_n_ep; uEp++)
2361
usb_ep_data_t *pEpData = &pAlt->altif_ep[uEp];
2363
int rc = vboxUSBSolarisInitEndPoint(pState, pEpData, uCfgValue, uInterface, uAlt);
2366
LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForConfig: vboxUSBSolarisInitEndPoint failed! pEp=%p uCfgValue=%u uCfgIndex=%u uInterface=%u, uAlt=%u\n",
2367
uCfgValue, uCfgIndex, uInterface, uAlt));
2373
return VINF_SUCCESS;
2378
* Initialize Endpoints structures for the given Interface & Alternate setting.
2380
* @param pState The USB device instance.
2381
* @param uInterface The interface being switched to.
2382
* @param uAlt The alt being switched to.
2384
* @returns VBox status code.
2386
LOCAL int vboxUSBSolarisInitEndPointsForInterfaceAlt(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt)
2388
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt pState=%p uInterface=%d uAlt=%d\n", pState, uInterface, uAlt));
2390
/* Doesn't hurt to be paranoid */
2391
uint_t uCfgIndex = usb_get_current_cfgidx(pState->pDip);
2392
if (RT_UNLIKELY(uCfgIndex >= pState->pDevDesc->dev_n_cfg))
2394
LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt invalid current config index %d\n", uCfgIndex));
2395
return VERR_GENERAL_FAILURE;
2398
usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[uCfgIndex];
2399
uchar_t uCfgValue = pConfig->cfg_descr.bConfigurationValue;
2400
usb_if_data_t *pInterface = &pConfig->cfg_if[uInterface];
2402
int rc = VINF_SUCCESS;
2403
if (RT_LIKELY(pInterface))
2405
usb_alt_if_data_t *pAlt = &pInterface->if_alt[uAlt];
2406
if (RT_LIKELY(pAlt))
2408
for (uchar_t uEp = 0; uEp < pAlt->altif_n_ep; uEp++)
2410
usb_ep_data_t *pEpData = &pAlt->altif_ep[uEp];
2411
rc = vboxUSBSolarisInitEndPoint(pState, pEpData, uCfgValue, uInterface, uAlt);
2414
LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt: vboxUSBSolarisInitEndPoint failed! pEp=%p uCfgValue=%u uCfgIndex=%u uInterface=%u, uAlt=%u\n",
2415
uCfgValue, uCfgIndex, uInterface, uAlt));
2422
LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt missing alternate.\n"));
2423
rc = VERR_INVALID_POINTER;
2428
LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt missing interface.\n"));
2429
rc = VERR_INVALID_POINTER;
2432
LogFlow((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt returns %d\n", rc));
2438
* Destroy all Endpoint Xfer structures.
2440
* @param pState The USB device instance.
2441
* @remarks Requires the state mutex to be held!!
2442
* Call only from Detach() or similar as callbacks
2444
LOCAL void vboxUSBSolarisDestroyAllEndPoints(vboxusb_state_t *pState)
2446
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDestroyAllEndPoints pState=%p\n", pState));
2447
for (unsigned i = 0; i < VBOXUSB_MAX_ENDPOINTS; i++)
2449
vboxusb_ep_t *pEp = &pState->aEps[i];
2452
vboxUSBSolarisDestroyEndPoint(pState, pEp);
2460
* Destroy an Endpoint.
2462
* @param pState The USB device instance.
2463
* @param pEp The Endpoint.
2465
LOCAL void vboxUSBSolarisDestroyEndPoint(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2467
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDestroyEndPoint pState=%p pEp=%p\n", pState, pEp));
2469
if (pEp->fInitialized == VBOXUSB_EP_INITIALIZED)
2471
vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
2475
freemsg(pUrb->pMsg);
2477
pUrb = list_remove_head(&pEp->hIsocInUrbs);
2479
pEp->cIsocInUrbs = 0;
2480
list_destroy(&pEp->hIsocInUrbs);
2482
vboxusb_isoc_req_t *pIsocReq = list_remove_head(&pEp->hIsocInLandedReqs);
2485
kmem_free(pIsocReq, sizeof(vboxusb_isoc_req_t));
2486
pIsocReq = list_remove_head(&pEp->hIsocInLandedReqs);
2488
pEp->cbIsocInLandedReqs = 0;
2489
list_destroy(&pEp->hIsocInLandedReqs);
2491
pEp->fInitialized = 0;
2497
* Close all non-default Endpoints and drains the default pipe.
2499
* @param pState The USB device instance.
2500
* @param fDefault Whether to close the default control pipe.
2502
* @remarks Requires the device state mutex to be held.
2504
LOCAL void vboxUSBSolarisCloseAllPipes(vboxusb_state_t *pState, bool fDefault)
2506
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisCloseAllPipes pState=%p\n", pState));
2508
for (int i = 1; i < VBOXUSB_MAX_ENDPOINTS; i++)
2510
vboxusb_ep_t *pEp = &pState->aEps[i];
2514
LogFlow((DEVICE_NAME ":vboxUSBSolarisCloseAllPipes closing[%d]\n", i));
2515
vboxUSBSolarisClosePipe(pState, pEp);
2521
vboxusb_ep_t *pEp = &pState->aEps[0];
2525
vboxUSBSolarisClosePipe(pState, pEp);
2526
LogFlow((DEVICE_NAME ":vboxUSBSolarisCloseAllPipes closed default pipe.\n"));
2533
* Closes all pipes for a given interface.
2535
* @param pState The USB device instance.
2536
* @param bInterface The Interface.
2538
LOCAL void vboxUSBSolarisCloseInterface(vboxusb_state_t *pState, uint8_t bInterface)
2540
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisCloseInterface pState=%p bInterface=%#x\n", pState, bInterface));
2542
for (int i = 1; i < VBOXUSB_MAX_ENDPOINTS; i++)
2544
vboxusb_ep_t *pEp = &pState->aEps[i];
2547
&& pEp->uInterface == bInterface)
2549
LogFlow((DEVICE_NAME ":vboxUSBSolarisCloseInterface closing[%d]\n", i));
2550
vboxUSBSolarisClosePipe(pState, pEp);
2557
* Open the pipe for an Endpoint.
2559
* @param pState The USB device instance.
2560
* @param pEp The Endpoint.
2562
* @returns VBox status code.
2564
LOCAL int vboxUSBSolarisOpenPipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2566
// LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisOpenPipe pState=%p pEp=%p\n", pState, pEp));
2569
* Make sure the Endpoint isn't open already.
2572
return VINF_SUCCESS;
2575
* Default Endpoint; already opened just copy the pipe handle.
2577
if ((pEp->EpDesc.bEndpointAddress & USB_EP_NUM_MASK) == 0)
2579
pEp->pPipe = pState->pDevDesc->dev_default_ph;
2580
pEp->EpState |= VBOXUSB_EP_STATE_OPENED;
2581
LogFlow((DEVICE_NAME ":vboxUSBSolarisOpenPipe default pipe opened.\n"));
2582
return VINF_SUCCESS;
2586
* Open the non-default pipe for the Endpoint.
2588
int rc = usb_pipe_open(pState->pDip, &pEp->EpDesc, &pEp->PipePolicy, USB_FLAGS_NOSLEEP, &pEp->pPipe);
2589
if (rc == USB_SUCCESS)
2591
usb_pipe_set_private(pEp->pPipe, (usb_opaque_t)pEp);
2594
* Determine input buffer size for Isoc. IN transfers.
2596
if ( VBOXUSB_XFER_TYPE(pEp) == VUSBXFERTYPE_ISOC
2597
&& VBOXUSB_XFER_DIR(pEp) == VUSB_DIR_TO_HOST)
2600
* wMaxPacketSize bits 10..0 specifies maximum packet size which can hold 1024 bytes.
2601
* If bits 12..11 is non-zero, cbMax will be more than 1024 and thus the Endpoint is a
2602
* high-bandwidth Endpoint.
2604
uint16_t cbMax = VBOXUSB_PKT_SIZE(pEp->EpDesc.wMaxPacketSize);
2607
/* Buffer 1 second for highspeed and 8 seconds for fullspeed Endpoints. */
2608
pEp->cbMaxIsocData = 1000 * cbMax * 8;
2612
/* Buffer about 400 milliseconds of data for highspeed high-bandwidth endpoints. */
2613
pEp->cbMaxIsocData = 400 * cbMax * 8;
2615
LogFlow((DEVICE_NAME ":vboxUSBSolarisOpenPipe pEp=%p cbMaxIsocData=%u\n", pEp->cbMaxIsocData));
2618
pEp->EpState |= VBOXUSB_EP_STATE_OPENED;
2623
LogRel((DEVICE_NAME ":vboxUSBSolarisOpenPipe failed! rc=%d pState=%p pEp=%p\n", rc, pState, pEp));
2632
* Close the pipe of the Endpoint.
2634
* @param pState The USB device instance.
2635
* @param pEp The Endpoint.
2637
* @remarks Requires the device state mutex to be held.
2639
LOCAL void vboxUSBSolarisClosePipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2641
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisClosePipe pState=%p pEp=%p\n", pState, pEp));
2646
pEp->EpState &= ~(VBOXUSB_EP_STATE_OPENED);
2649
* Default pipe: allow completion of pending requests.
2651
if (pEp->pPipe == pState->pDevDesc->dev_default_ph)
2653
mutex_exit(&pState->Mtx);
2654
usb_pipe_drain_reqs(pState->pDip, pEp->pPipe, 0, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback arg. */);
2655
mutex_enter(&pState->Mtx);
2656
LogFlow((DEVICE_NAME ":vboxUSBSolarisClosePipe closed default pipe\n"));
2661
* Stop Isoc. IN polling if required.
2663
if (pEp->fIsocPolling)
2665
pEp->fIsocPolling = false;
2666
mutex_exit(&pState->Mtx);
2667
usb_pipe_stop_isoc_polling(pEp->pPipe, USB_FLAGS_NOSLEEP);
2668
mutex_enter(&pState->Mtx);
2672
* Non-default pipe: close it.
2674
LogFlow((DEVICE_NAME ":vboxUSBSolarisClosePipe pipe bmAttributes=%#x bEndpointAddress=%#x\n", pEp->EpDesc.bmAttributes, pEp->EpDesc.bEndpointAddress));
2675
mutex_exit(&pState->Mtx);
2676
usb_pipe_close(pState->pDip, pEp->pPipe, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback arg. */);
2677
mutex_enter(&pState->Mtx);
2681
* Free the Endpoint data message block and reset pipe handle.
2685
LogFlow((DEVICE_NAME ":vboxUSBSolarisClosePipe successful. pEp=%p\n", pEp));
2688
Assert(pEp->pPipe == NULL);
2693
* Check if any non-default Endpoints are open.
2695
* @param pState The USB device instance.
2697
* @returns Returns true if any non-default Endpoint is open, otherwise false.
2699
LOCAL bool vboxUSBSolarisIsAnyPipeOpen(vboxusb_state_t *pState)
2701
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIsAnyPipeOpen pState=%p\n", pState));
2703
for (int i = 1; i < VBOXUSB_MAX_ENDPOINTS; i++)
2705
vboxusb_ep_t *pEp = &pState->aEps[i];
2708
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsAnyPipeOpen pState=%p pEp=%p returns true.\n", pState, pEp));
2713
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsAnyPipeOpen pState=%p returns false.\n", pState));
2719
* Find the Configuration index for the passed in Configuration value.
2721
* @param pState The USB device instance.
2722
* @param uCfgValue The Configuration value.
2724
* @returns The configuration index if found, otherwise -1.
2726
LOCAL int vboxUSBSolarisGetConfigIndex(vboxusb_state_t *pState, uint_t uCfgValue)
2728
for (int CfgIndex = 0; CfgIndex < pState->pDevDesc->dev_n_cfg; CfgIndex++)
2730
usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[CfgIndex];
2731
if (pConfig->cfg_descr.bConfigurationValue == uCfgValue)
2740
* Allocates and initializes an Isoc. In URB from the ring-3 equivalent.
2742
* @param pState The USB device instance.
2743
* @param pUrb The URB to initialize.
2744
* @param pUrbReq Opaque pointer to the complete request.
2745
* @param pMsg Pointer to the allocated request data.
2747
* @returns The allocated Isoc. In URB to be used.
2749
LOCAL vboxusb_urb_t *vboxUSBSolarisGetIsocInURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq)
2752
* Isoc. In URBs are not queued into the Inflight list like every other URBs.
2753
* For now we allocate each URB which gets queued into the respective Endpoint during Xfer.
2755
vboxusb_urb_t *pUrb = RTMemAllocZ(sizeof(vboxusb_urb_t));
2756
if (RT_LIKELY(pUrb))
2758
pUrb->enmState = VBOXUSB_URB_STATE_INFLIGHT;
2759
pUrb->pState = pState;
2761
if (RT_LIKELY(pUrbReq))
2763
pUrb->pvUrbR3 = pUrbReq->pvUrbR3;
2764
pUrb->bEndpoint = pUrbReq->bEndpoint;
2765
pUrb->enmType = pUrbReq->enmType;
2766
pUrb->enmDir = pUrbReq->enmDir;
2767
pUrb->enmStatus = pUrbReq->enmStatus;
2768
pUrb->cbDataR3 = pUrbReq->cbData;
2769
pUrb->pvDataR3 = (RTR3PTR)pUrbReq->pvData;
2770
pUrb->cIsocPkts = pUrbReq->cIsocPkts;
2772
for (unsigned i = 0; i < pUrbReq->cIsocPkts; i++)
2773
pUrb->aIsocPkts[i].cbPkt = pUrbReq->aIsocPkts[i].cbPkt;
2779
LogRel((DEVICE_NAME ":vboxUSBSolarisGetIsocInURB failed to alloc %d bytes.\n", sizeof(vboxusb_urb_t)));
2785
* Queues a URB reusing previously allocated URBs as required.
2787
* @param pState The USB device instance.
2788
* @param pUrbReq Opaque pointer to the complete request.
2789
* @param pMsg Pointer to the allocated request data.
2791
* @returns The allocated URB to be used.
2793
LOCAL vboxusb_urb_t *vboxUSBSolarisQueueURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, mblk_t *pMsg)
2795
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisQueueURB pState=%p pUrbReq=%p\n", pState, pUrbReq));
2797
mutex_enter(&pState->Mtx);
2800
* Discard oldest queued URB if we've queued max URBs and none of them have completed.
2802
if (pState->cInflightUrbs >= VBOXUSB_URB_QUEUE_SIZE)
2804
vboxusb_urb_t *pUrb = list_head(&pState->hUrbs);
2805
if (RT_LIKELY(pUrb))
2809
freemsg(pUrb->pMsg);
2812
pUrb->enmState = VBOXUSB_URB_STATE_FREE;
2816
vboxusb_urb_t *pUrb = list_head(&pState->hUrbs);
2819
&& pUrb->enmState != VBOXUSB_URB_STATE_FREE))
2821
mutex_exit(&pState->Mtx);
2822
pUrb = RTMemAllocZ(sizeof(vboxusb_urb_t));
2823
if (RT_UNLIKELY(!pUrb))
2825
LogRel((DEVICE_NAME ":vboxUSBSolarisQueueURB failed to alloc %d bytes.\n", sizeof(vboxusb_urb_t)));
2828
mutex_enter(&pState->Mtx);
2833
* Remove from head and move to tail so that when several URBs are reaped continuously we get to use
2834
* up each one free 'head'.
2836
list_remove_head(&pState->hUrbs);
2839
list_insert_tail(&pState->hUrbs, pUrb);
2840
++pState->cInflightUrbs;
2842
pUrb->enmState = VBOXUSB_URB_STATE_INFLIGHT;
2844
mutex_exit(&pState->Mtx);
2846
Assert(pUrb->pMsg == NULL);
2847
pUrb->pState = pState;
2848
LogFlow((DEVICE_NAME ":vboxUSBSolarisQueueURB cInflightUrbs=%d\n", pState->cInflightUrbs));
2850
if (RT_LIKELY(pUrbReq))
2852
pUrb->pvUrbR3 = pUrbReq->pvUrbR3;
2853
pUrb->bEndpoint = pUrbReq->bEndpoint;
2854
pUrb->enmType = pUrbReq->enmType;
2855
pUrb->enmDir = pUrbReq->enmDir;
2856
pUrb->enmStatus = pUrbReq->enmStatus;
2857
pUrb->cbDataR3 = pUrbReq->cbData;
2858
pUrb->pvDataR3 = (RTR3PTR)pUrbReq->pvData;
2859
pUrb->cIsocPkts = pUrbReq->cIsocPkts;
2860
if (pUrbReq->enmType == VUSBXFERTYPE_ISOC)
2862
for (unsigned i = 0; i < pUrbReq->cIsocPkts; i++)
2863
pUrb->aIsocPkts[i].cbPkt = pUrbReq->aIsocPkts[i].cbPkt;
2874
* Dequeues a completed URB into the landed list and informs user-land.
2876
* @param pUrb The URB to move.
2877
* @param URBStatus The Solaris URB completion code.
2879
* @remarks All pipes could be closed at this point (e.g. Device disconnected during inflight URBs)
2881
LOCAL inline void vboxUSBSolarisDeQueueURB(vboxusb_urb_t *pUrb, int URBStatus)
2883
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDeQueue pUrb=%p\n", pUrb));
2884
AssertPtrReturnVoid(pUrb);
2886
pUrb->enmStatus = vboxUSBSolarisGetUrbStatus(URBStatus);
2888
vboxusb_state_t *pState = pUrb->pState;
2889
if (RT_LIKELY(pState))
2891
mutex_enter(&pState->Mtx);
2892
pUrb->enmState = VBOXUSB_URB_STATE_LANDED;
2895
* Remove it from the inflight list & move it to landed list.
2897
list_remove(&pState->hUrbs, pUrb);
2898
--pState->cInflightUrbs;
2899
list_insert_tail(&pState->hLandedUrbs, pUrb);
2901
vboxUSBSolarisNotifyComplete(pUrb->pState);
2902
mutex_exit(&pState->Mtx);
2906
LogFlow((DEVICE_NAME ":vboxUSBSolarisDeQueue State Gone.\n"));
2907
freemsg(pUrb->pMsg);
2909
pUrb->enmStatus = VUSBSTATUS_INVALID;
2915
* Concatenates a chain message block into a single message block if possible.
2917
* @param pUrb The URB to move.
2919
LOCAL inline void vboxUSBSolarisConcatMsg(vboxusb_urb_t *pUrb)
2922
* Concatenate the whole message rather than doing a chained copy while reaping.
2925
&& pUrb->pMsg->b_cont)
2927
mblk_t *pFullMsg = msgpullup(pUrb->pMsg, -1 /* all data */);
2928
if (RT_LIKELY(pFullMsg))
2930
freemsg(pUrb->pMsg);
2931
pUrb->pMsg = pFullMsg;
2938
* User process poll wake up wrapper for asynchronous URB completion.
2940
* @param pState The USB device instance.
2941
* @remarks Requires the device state mutex to be held!!
2943
LOCAL inline void vboxUSBSolarisNotifyComplete(vboxusb_state_t *pState)
2945
if (pState->fPoll & VBOXUSB_POLL_ON)
2947
pollhead_t *pPollHead = &pState->PollHead;
2948
pState->fPoll |= VBOXUSB_POLL_REAP_PENDING;
2949
mutex_exit(&pState->Mtx);
2950
pollwakeup(pPollHead, POLLIN);
2951
mutex_enter(&pState->Mtx);
2957
* User process poll wake up wrapper for hotplug events.
2959
* @param pState The USB device instance.
2960
* @remarks Requires the device state mutex to be held.
2962
LOCAL inline void vboxUSBSolarisNotifyHotplug(vboxusb_state_t *pState)
2964
if (pState->fPoll & VBOXUSB_POLL_ON)
2966
pollhead_t *pPollHead = &pState->PollHead;
2967
pState->fPoll |= VBOXUSB_POLL_DEV_UNPLUGGED;
2968
mutex_exit(&pState->Mtx);
2969
pollwakeup(pPollHead, POLLHUP);
2970
mutex_enter(&pState->Mtx);
2976
* Perform a Control Xfer.
2978
* @param pState The USB device instance.
2979
* @param pEp The Endpoint for the Xfer.
2980
* @param pUrb The VBox USB URB.
2982
* @returns VBox status code.
2983
* @remarks Any errors, the caller should free pUrb->pMsg.
2985
LOCAL int vboxUSBSolarisCtrlXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
2987
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisCtrlXfer pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb, pUrb->enmDir, pUrb->cbDataR3));
2989
AssertPtrReturn(pUrb->pMsg, VERR_INVALID_PARAMETER);
2990
uchar_t *pSetupData = pUrb->pMsg->b_rptr;
2991
size_t cbData = pUrb->cbDataR3 - VBOXUSB_CTRL_XFER_SIZE;
2994
* Solaris USBA gives us garbage and incorrect message lengths making it impossible to use
2995
* pre-allocated control messages. The allocation of "ctrl_data" is not documented well.
2999
* Allocate a wrapper request.
3001
int rc = VINF_SUCCESS;
3002
usb_ctrl_req_t *pReq = usb_alloc_ctrl_req(pState->pDip, cbData, USB_FLAGS_NOSLEEP);
3003
if (RT_LIKELY(pReq))
3006
* Initialize the Ctrl Xfer Header.
3008
pReq->ctrl_bmRequestType = pSetupData[0];
3009
pReq->ctrl_bRequest = pSetupData[1];
3010
pReq->ctrl_wValue = (pSetupData[3] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[2];
3011
pReq->ctrl_wIndex = (pSetupData[5] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[4];
3012
pReq->ctrl_wLength = (pSetupData[7] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[6];
3014
if ( pUrb->enmDir == VUSBDIRECTION_OUT
3017
pUrb->pMsg->b_rptr += VBOXUSB_CTRL_XFER_SIZE;
3018
bcopy(pUrb->pMsg->b_rptr, pReq->ctrl_data->b_wptr, cbData);
3019
pReq->ctrl_data->b_wptr += cbData;
3022
freemsg(pUrb->pMsg);
3026
* Initialize callbacks and timeouts.
3028
pReq->ctrl_cb = vboxUSBSolarisCtrlXferCompleted;
3029
pReq->ctrl_exc_cb = vboxUSBSolarisCtrlXferCompleted;
3030
pReq->ctrl_timeout = VBOXUSB_CTRL_XFER_TIMEOUT;
3031
pReq->ctrl_attributes = USB_ATTRS_AUTOCLEARING | (pUrb->enmDir == VUSBDIRECTION_IN ? USB_ATTRS_SHORT_XFER_OK : 0);
3033
pReq->ctrl_client_private = (usb_opaque_t)pUrb;
3035
LogFlow((DEVICE_NAME ":vboxUSBSolarisCtrlXfer %.*Rhxd\n", VBOXUSB_CTRL_XFER_SIZE, pSetupData));
3038
* Submit the request.
3040
rc = usb_pipe_ctrl_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3042
if (RT_LIKELY(rc == USB_SUCCESS))
3043
return VINF_SUCCESS;
3046
LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXfer usb_pipe_ctrl_xfer failed! rc=%d\n", rc));
3047
rc = VERR_PIPE_IO_ERROR;
3050
usb_free_ctrl_req(pReq);
3054
LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXfer failed to alloc request.\n"));
3055
rc = VERR_NO_MEMORY;
3063
* Completion/Exception callback for Control Xfers.
3065
* @param pPipe The Ctrl pipe handle.
3066
* @param pReq The Ctrl request.
3068
LOCAL void vboxUSBSolarisCtrlXferCompleted(usb_pipe_handle_t pPipe, usb_ctrl_req_t *pReq)
3070
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3072
vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->ctrl_client_private;
3073
if (RT_LIKELY(pUrb))
3076
* Funky stuff: We need to reconstruct the header for control transfers.
3077
* Let us chain along the data and while we dequeue the URB we attempt to
3078
* concatenate the entire message there.
3080
mblk_t *pSetupMsg = allocb(sizeof(VUSBSETUP), BPRI_MED);
3081
if (RT_LIKELY(pSetupMsg))
3083
VUSBSETUP SetupData;
3084
SetupData.bmRequestType = pReq->ctrl_bmRequestType;
3085
SetupData.bRequest = pReq->ctrl_bRequest;
3086
SetupData.wValue = pReq->ctrl_wValue;
3087
SetupData.wIndex = pReq->ctrl_wIndex;
3088
SetupData.wLength = pReq->ctrl_wLength;
3089
bcopy(&SetupData, pSetupMsg->b_wptr, sizeof(VUSBSETUP));
3090
pSetupMsg->b_wptr += sizeof(VUSBSETUP);
3092
pUrb->pMsg = pSetupMsg;
3093
pUrb->pMsg->b_cont = pReq->ctrl_data;
3094
pReq->ctrl_data = NULL;
3095
vboxUSBSolarisConcatMsg(pUrb);
3097
#if defined(DEBUG_ramshankar)
3099
&& pUrb->pMsg->b_cont == NULL) /* Concat succeeded */
3101
LogFlow((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted prepended header rc=%d cbData=%d.\n", pReq->ctrl_completion_reason,
3102
MBLKL(pUrb->pMsg)));
3103
LogFlow((DEVICE_NAME ":%.*Rhxd\n", MBLKL(pUrb->pMsg), pUrb->pMsg->b_rptr));
3108
* Update the URB and move to landed list for reaping.
3110
vboxUSBSolarisDeQueueURB(pUrb, pReq->ctrl_completion_reason);
3111
usb_free_ctrl_req(pReq);
3115
LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted failed to alloc %d bytes for Setup Header.\n", sizeof(VUSBSETUP)));
3118
LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted Extreme error! missing private data.\n"));
3120
usb_free_ctrl_req(pReq);
3125
* Perform a Bulk Xfer.
3127
* @param pState The USB device instance.
3128
* @param pEp The Endpoint for the Xfer.
3129
* @param pUrb The VBox USB URB.
3131
* @returns VBox status code.
3132
* @remarks Any errors, the caller should free pUrb->pMsg.
3134
LOCAL int vboxUSBSolarisBulkXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3136
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisBulkXfer pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb, pUrb->enmDir, pUrb->cbDataR3));
3139
* Allocate a wrapper request.
3141
int rc = VINF_SUCCESS;
3142
usb_bulk_req_t *pReq = usb_alloc_bulk_req(pState->pDip, pUrb->enmDir == VUSBDIRECTION_IN ? pUrb->cbDataR3 : 0, USB_FLAGS_NOSLEEP);
3143
if (RT_LIKELY(pReq))
3146
* Initialize Bulk Xfer, callbacks and timeouts.
3148
if (pUrb->enmDir == VUSBDIRECTION_OUT)
3149
pReq->bulk_data = pUrb->pMsg;
3151
pReq->bulk_len = pUrb->cbDataR3;
3152
pReq->bulk_cb = vboxUSBSolarisBulkXferCompleted;
3153
pReq->bulk_exc_cb = vboxUSBSolarisBulkXferCompleted;
3154
pReq->bulk_timeout = VBOXUSB_BULK_XFER_TIMEOUT;
3155
pReq->bulk_attributes = USB_ATTRS_AUTOCLEARING | (pUrb->enmDir == VUSBDIRECTION_IN ? USB_ATTRS_SHORT_XFER_OK : 0);
3156
pReq->bulk_client_private = (usb_opaque_t)pUrb;
3158
/* Don't obtain state lock here, we're just reading unchanging data... */
3159
if (RT_UNLIKELY(pUrb->cbDataR3 > pState->cbMaxBulkXfer))
3161
LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXfer requesting %d bytes when only %d bytes supported by device\n",
3162
pUrb->cbDataR3, pState->cbMaxBulkXfer));
3166
* Submit the request.
3168
rc = usb_pipe_bulk_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3170
if (RT_LIKELY(rc == USB_SUCCESS))
3171
return VINF_SUCCESS;
3174
LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXfer usb_pipe_bulk_xfer enmDir=%#x Ep=%#x failed! rc=%d\n", pUrb->enmDir, pUrb->bEndpoint, rc));
3175
rc = VERR_PIPE_IO_ERROR;
3178
if (pUrb->enmDir == VUSBDIRECTION_OUT) /* pUrb->pMsg freed by caller */
3179
pReq->bulk_data = NULL;
3181
usb_free_bulk_req(pReq);
3185
LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXfer failed to alloc bulk request.\n"));
3186
rc = VERR_NO_MEMORY;
3194
* Completion/Exception callback for Bulk Xfers.
3196
* @param pPipe The Bulk pipe handle.
3197
* @param pReq The Bulk request.
3199
LOCAL void vboxUSBSolarisBulkXferCompleted(usb_pipe_handle_t pPipe, usb_bulk_req_t *pReq)
3201
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3203
vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3206
vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->bulk_client_private;
3207
if (RT_LIKELY(pUrb))
3209
if (pUrb->enmDir == VUSBDIRECTION_OUT)
3210
pReq->bulk_data = NULL;
3213
if (pReq->bulk_completion_reason == USB_CR_OK)
3215
pUrb->pMsg = pReq->bulk_data;
3216
pReq->bulk_data = NULL;
3217
vboxUSBSolarisConcatMsg(pUrb);
3221
LogFlow((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted %s. rc=%d cbData=%d\n",
3222
pReq->bulk_completion_reason != USB_CR_OK ? "failed URB" : "success",
3223
pReq->bulk_completion_reason, pUrb->pMsg ? MBLKL(pUrb->pMsg) : 0));
3226
* Update the URB and move to tail for reaping.
3228
vboxUSBSolarisDeQueueURB(pUrb, pReq->bulk_completion_reason);
3229
usb_free_bulk_req(pReq);
3233
LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted Extreme error! private request data missing.\n"));
3236
LogFlow((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted Pipe Gone.\n"));
3238
usb_free_bulk_req(pReq);
3243
* Perform an Interrupt Xfer.
3245
* @param pState The USB device instance.
3246
* @param pEp The Endpoint for the Xfer.
3247
* @param pUrb The VBox USB URB.
3249
* @returns VBox status code.
3250
* @remarks Any errors, the caller should free pUrb->pMsg.
3252
LOCAL int vboxUSBSolarisIntrXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3254
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIntrXfer pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb, pUrb->enmDir, pUrb->cbDataR3));
3256
int rc = VINF_SUCCESS;
3257
usb_intr_req_t *pReq = usb_alloc_intr_req(pState->pDip, 0 /* length */, USB_FLAGS_NOSLEEP);
3258
if (RT_LIKELY(pReq))
3261
* Initialize Intr Xfer, callbacks & timeouts.
3263
if (pUrb->enmDir == VUSBDIRECTION_OUT)
3265
pReq->intr_data = pUrb->pMsg;
3266
pReq->intr_attributes = USB_ATTRS_AUTOCLEARING;
3270
pReq->intr_data = NULL;
3271
pReq->intr_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ONE_XFER | USB_ATTRS_SHORT_XFER_OK;
3274
pReq->intr_len = pUrb->cbDataR3; /* Not pEp->EpDesc.wMaxPacketSize */
3275
pReq->intr_cb = vboxUSBSolarisIntrXferCompleted;
3276
pReq->intr_exc_cb = vboxUSBSolarisIntrXferCompleted;
3277
pReq->intr_timeout = VBOXUSB_INTR_XFER_TIMEOUT;
3278
pReq->intr_client_private = (usb_opaque_t)pUrb;
3281
* Submit the request.
3283
rc = usb_pipe_intr_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3285
if (RT_LIKELY(rc == USB_SUCCESS))
3286
return VINF_SUCCESS;
3289
LogRel((DEVICE_NAME ":vboxUSBSolarisIntrXfer usb_pipe_intr_xfer failed! rc=%d\n", rc));
3290
rc = VERR_PIPE_IO_ERROR;
3293
pReq->intr_data = NULL;
3294
usb_free_intr_req(pReq);
3298
LogRel((DEVICE_NAME ":vboxUSBSolarisIntrXfer failed to alloc intr request.\n"));
3299
rc = VERR_NO_MEMORY;
3307
* Completion/Exception callback for Intr Xfers.
3309
* @param pPipe The Intr pipe handle.
3310
* @param pReq The Intr request.
3312
LOCAL void vboxUSBSolarisIntrXferCompleted(usb_pipe_handle_t pPipe, usb_intr_req_t *pReq)
3314
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3316
vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3319
vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->intr_client_private;
3320
if (RT_LIKELY(pUrb))
3322
if (pUrb->enmDir == VUSBDIRECTION_OUT)
3323
pReq->intr_data = NULL;
3326
if (pReq->intr_completion_reason == USB_CR_OK)
3328
pUrb->pMsg = pReq->intr_data;
3329
pReq->intr_data = NULL;
3333
LogFlow((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted rc=%d pMsg=%p enmDir=%#x\n", pReq->intr_completion_reason, pUrb->pMsg,
3337
* Update the URB and move to landed list for reaping.
3339
vboxUSBSolarisDeQueueURB(pUrb, pReq->intr_completion_reason);
3340
usb_free_intr_req(pReq);
3344
LogRel((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted Extreme error! private request data missing.\n"));
3347
LogFlow((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted Pipe Gone.\n"));
3349
usb_free_intr_req(pReq);
3354
* Perform an Isochronous Xfer.
3356
* @param pState The USB device instance.
3357
* @param pEp The Endpoint for the Xfer.
3358
* @param pUrb The VBox USB URB.
3360
* @returns VBox status code.
3361
* @remarks Any errors, the caller should free pUrb->pMsg.
3363
LOCAL int vboxUSBSolarisIsocXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3365
// LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIsocXfer pState=%p pEp=%p pUrb=%p\n", pState, pEp, pUrb));
3368
* For Isoc. IN transfers we perform one request and USBA polls the device continuously
3369
* and supplies our Xfer callback with input data. We cannot perform one-shot Isoc. In transfers.
3371
size_t cbData = (pUrb->enmDir == VUSBDIRECTION_IN ? pUrb->cIsocPkts * pUrb->aIsocPkts[0].cbPkt : 0);
3372
if (pUrb->enmDir == VUSBDIRECTION_IN)
3374
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocXfer Isoc. In queueing.\n"));
3376
mutex_enter(&pState->Mtx);
3377
if (pEp->fIsocPolling)
3380
* Queue a maximum of cbMaxIsocData bytes, else fail.
3382
if (pEp->cbIsocInLandedReqs + cbData > pEp->cbMaxIsocData)
3384
mutex_exit(&pState->Mtx);
3385
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocXfer Max Isoc. data %d bytes queued\n", pEp->cbMaxIsocData));
3386
return VERR_TOO_MUCH_DATA;
3389
list_insert_tail(&pEp->hIsocInUrbs, pUrb);
3392
mutex_exit(&pState->Mtx);
3393
return VINF_SUCCESS;
3395
mutex_exit(&pState->Mtx);
3398
int rc = VINF_SUCCESS;
3399
usb_isoc_req_t *pReq = usb_alloc_isoc_req(pState->pDip, pUrb->cIsocPkts, cbData, USB_FLAGS_NOSLEEP);
3400
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocXfer enmDir=%#x cIsocPkts=%d aIsocPkts[0]=%d cbDataR3=%d\n", pUrb->enmDir,
3401
pUrb->cIsocPkts, pUrb->aIsocPkts[0].cbPkt, pUrb->cbDataR3));
3402
if (RT_LIKELY(pReq))
3405
* Initialize Isoc Xfer, callbacks & timeouts.
3407
for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
3408
pReq->isoc_pkt_descr[i].isoc_pkt_length = pUrb->aIsocPkts[i].cbPkt;
3410
if (pUrb->enmDir == VUSBDIRECTION_OUT)
3412
pReq->isoc_data = pUrb->pMsg;
3413
pReq->isoc_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ISOC_XFER_ASAP;
3414
pReq->isoc_cb = vboxUSBSolarisIsocOutXferCompleted;
3415
pReq->isoc_exc_cb = vboxUSBSolarisIsocOutXferCompleted;
3416
pReq->isoc_client_private = (usb_opaque_t)pUrb;
3420
pReq->isoc_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ISOC_XFER_ASAP | USB_ATTRS_SHORT_XFER_OK;
3421
pReq->isoc_cb = vboxUSBSolarisIsocInXferCompleted;
3422
pReq->isoc_exc_cb = vboxUSBSolarisIsocInXferError;
3423
pReq->isoc_client_private = (usb_opaque_t)pState;
3425
pReq->isoc_pkts_count = pUrb->cIsocPkts;
3426
pReq->isoc_pkts_length = 0; /* auto compute */
3429
* Submit the request.
3431
rc = usb_pipe_isoc_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3432
if (RT_LIKELY(rc == USB_SUCCESS))
3434
if (pUrb->enmDir == VUSBDIRECTION_IN)
3437
* Add the first Isoc. IN URB to the queue as well.
3439
mutex_enter(&pState->Mtx);
3440
list_insert_tail(&pEp->hIsocInUrbs, pUrb);
3442
pEp->fIsocPolling = true;
3443
mutex_exit(&pState->Mtx);
3446
return VINF_SUCCESS;
3450
LogRel((DEVICE_NAME ":vboxUSBSolarisIsocXfer usb_pipe_isoc_xfer failed! rc=%d\n", rc));
3451
rc = VERR_PIPE_IO_ERROR;
3453
if (pUrb->enmDir == VUSBDIRECTION_IN)
3455
mutex_enter(&pState->Mtx);
3456
vboxusb_urb_t *pIsocFailedUrb = list_remove_tail(&pEp->hIsocInUrbs);
3459
RTMemFree(pIsocFailedUrb);
3462
pEp->fIsocPolling = false;
3463
mutex_exit(&pState->Mtx);
3467
if (pUrb->enmDir == VUSBDIRECTION_OUT) /* pUrb->pMsg freed by caller */
3468
pReq->isoc_data = NULL;
3470
usb_free_isoc_req(pReq);
3474
LogRel((DEVICE_NAME ":vboxUSBSolarisIsocXfer failed to alloc isoc req for %d packets\n", pUrb->cIsocPkts));
3475
rc = VERR_NO_MEMORY;
3483
* Completion/Exception callback for Isoc IN Xfers.
3485
* @param pPipe The Intr pipe handle.
3486
* @param pReq The Intr request.
3488
* @remarks Completion callback executes in interrupt context!
3490
LOCAL void vboxUSBSolarisIsocInXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3492
// LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3494
vboxusb_state_t *pState = (vboxusb_state_t *)pReq->isoc_client_private;
3495
if (RT_LIKELY(pState))
3497
vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3503
* Stop polling if all packets failed.
3505
if (pReq->isoc_error_count == pReq->isoc_pkts_count)
3507
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted stopping polling! Too many errors.\n"));
3508
mutex_exit(&pState->Mtx);
3509
usb_pipe_stop_isoc_polling(pPipe, USB_FLAGS_NOSLEEP);
3510
mutex_enter(&pState->Mtx);
3511
pEp->fIsocPolling = false;
3515
AssertCompile(sizeof(VUSBISOC_PKT_DESC) == sizeof(usb_isoc_pkt_descr_t));
3517
if (RT_LIKELY(pReq->isoc_data))
3519
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted cIsocInUrbs=%d cbIsocInLandedReqs=%d\n", pEp->cIsocInUrbs, pEp->cbIsocInLandedReqs));
3521
mutex_enter(&pState->Mtx);
3524
* If there are waiting URBs, satisfy the oldest one.
3526
if ( pEp->cIsocInUrbs > 0
3527
&& pEp->cbIsocInLandedReqs == 0)
3529
vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
3530
if (RT_LIKELY(pUrb))
3533
mutex_exit(&pState->Mtx);
3535
for (unsigned i = 0; i < pReq->isoc_pkts_count; i++)
3537
pUrb->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3538
pUrb->aIsocPkts[i].enmStatus = vboxUSBSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3541
bcopy(pReq->isoc_pkt_descr, pUrb->aIsocPkts, sizeof(VUSBISOC_PKT_DESC) * pReq->isoc_pkts_count);
3543
pUrb->pMsg = pReq->isoc_data;
3544
pReq->isoc_data = NULL;
3547
* Move to landed list
3549
mutex_enter(&pState->Mtx);
3550
list_insert_tail(&pState->hLandedUrbs, pUrb);
3551
vboxUSBSolarisNotifyComplete(pState);
3555
/* Huh!? cIsocInUrbs is wrong then! Should never happen unless we decide to decrement cIsocInUrbs in Reap time */
3556
pEp->cIsocInUrbs = 0;
3557
LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted Extreme error! Isoc. counter b0rked!\n"));
3560
mutex_exit(&pState->Mtx);
3561
usb_free_isoc_req(pReq);
3567
* If the maximum buffer size is reached, discard the oldest data.
3569
if (pEp->cbIsocInLandedReqs + MBLKL(pReq->isoc_data) > pEp->cbMaxIsocData)
3571
vboxusb_isoc_req_t *pOldReq = list_remove_head(&pEp->hIsocInLandedReqs);
3572
if (RT_LIKELY(pOldReq))
3574
pEp->cbIsocInLandedReqs -= MBLKL(pOldReq->pMsg);
3575
kmem_free(pOldReq, sizeof(vboxusb_isoc_req_t));
3579
mutex_exit(&pState->Mtx);
3582
* Buffer incoming data if the guest has not yet queued any Input URBs.
3584
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted Buffering\n"));
3585
vboxusb_isoc_req_t *pIsocReq = kmem_alloc(sizeof(vboxusb_isoc_req_t), KM_NOSLEEP);
3586
if (RT_LIKELY(pIsocReq))
3588
pIsocReq->pMsg = pReq->isoc_data;
3589
pReq->isoc_data = NULL;
3590
pIsocReq->cIsocPkts = pReq->isoc_pkts_count;
3592
for (unsigned i = 0; i < pReq->isoc_pkts_count; i++)
3594
pIsocReq->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3595
pIsocReq->aIsocPkts[i].enmStatus = vboxUSBSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3598
bcopy(pReq->isoc_pkt_descr, pIsocReq->aIsocPkts, pReq->isoc_pkts_count * sizeof(VUSBISOC_PKT_DESC));
3601
mutex_enter(&pState->Mtx);
3602
list_insert_tail(&pEp->hIsocInLandedReqs, pIsocReq);
3603
pEp->cbIsocInLandedReqs += MBLKL(pIsocReq->pMsg);
3604
mutex_exit(&pState->Mtx);
3608
LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted failed to alloc %d bytes for Isoc. queueing\n",
3609
sizeof(vboxusb_isoc_req_t)));
3613
* Drain the input URB buffer with the device buffer, queueing them with the landed URBs.
3615
mutex_enter(&pState->Mtx);
3616
while (pEp->cIsocInUrbs)
3618
vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
3619
if (RT_UNLIKELY(!pUrb))
3622
vboxusb_isoc_req_t *pBuffReq = list_remove_head(&pEp->hIsocInLandedReqs);
3625
list_insert_head(&pEp->hIsocInUrbs, pUrb);
3630
pEp->cbIsocInLandedReqs -= MBLKL(pBuffReq->pMsg);
3631
mutex_exit(&pState->Mtx);
3634
for (unsigned i = 0; i < pBuffReq->cIsocPkts; i++)
3636
pUrb->aIsocPkts[i].cbActPkt = pBuffReq->aIsocPkts[i].cbActPkt;
3637
pUrb->aIsocPkts[i].enmStatus = pBuffReq->aIsocPkts[i].enmStatus;
3640
bcopy(pBuffReq->aIsocPkts, pUrb->aIsocPkts, pBuffReq->cIsocPkts * sizeof(VUSBISOC_PKT_DESC));
3642
pUrb->pMsg = pBuffReq->pMsg;
3643
pBuffReq->pMsg = NULL;
3644
kmem_free(pBuffReq, sizeof(vboxusb_isoc_req_t));
3647
* Move to landed list
3649
mutex_enter(&pState->Mtx);
3650
list_insert_tail(&pState->hLandedUrbs, pUrb);
3651
vboxUSBSolarisNotifyComplete(pState);
3655
mutex_exit(&pState->Mtx);
3656
usb_free_isoc_req(pReq);
3660
LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted data missing.\n"));
3663
LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted Pipe Gone.\n"));
3666
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted State Gone.\n"));
3668
usb_free_isoc_req(pReq);
3673
* Exception callback for Isoc IN Xfers.
3675
* @param pPipe The Intr pipe handle.
3676
* @param pReq The Intr request.
3677
* @remarks Completion callback executes in interrupt context!
3679
LOCAL void vboxUSBSolarisIsocInXferError(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3681
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIsocInXferError pPipe=%p pReq=%p\n", pPipe, pReq));
3683
vboxusb_state_t *pState = (vboxusb_state_t *)pReq->isoc_client_private;
3684
if (RT_UNLIKELY(!pState))
3686
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferError State Gone.\n"));
3687
usb_free_isoc_req(pReq);
3691
mutex_enter(&pState->Mtx);
3692
vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3693
if (RT_UNLIKELY(!pEp))
3695
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferError Pipe Gone.\n"));
3696
mutex_exit(&pState->Mtx);
3697
usb_free_isoc_req(pReq);
3701
switch(pReq->isoc_completion_reason)
3703
case USB_CR_NO_RESOURCES:
3706
* Resubmit the request in case the original request did not complete due to
3707
* immediately unavailable requests
3709
mutex_exit(&pState->Mtx);
3710
usb_pipe_isoc_xfer(pPipe, pReq, USB_FLAGS_NOSLEEP);
3711
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferError resubmitted Isoc. IN request due to immediately unavailable resources.\n"));
3716
case USB_CR_PIPE_CLOSING:
3717
case USB_CR_STOPPED_POLLING:
3718
case USB_CR_PIPE_RESET:
3720
pEp->fIsocPolling = false;
3721
usb_free_isoc_req(pReq);
3727
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferError stopping Isoc. In. polling due to rc=%d\n", pReq->isoc_completion_reason));
3728
pEp->fIsocPolling = false;
3729
mutex_exit(&pState->Mtx);
3730
usb_pipe_stop_isoc_polling(pPipe, USB_FLAGS_NOSLEEP);
3731
usb_free_isoc_req(pReq);
3732
mutex_enter(&pState->Mtx);
3738
* Dequeue i.e. delete the last queued Isoc In. URB. as failed.
3740
vboxusb_urb_t *pUrb = list_remove_tail(&pEp->hIsocInUrbs);
3744
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocInXferError Deleting last queued URB as it failed.\n"));
3745
freemsg(pUrb->pMsg);
3747
vboxUSBSolarisNotifyComplete(pState);
3750
mutex_exit(&pState->Mtx);
3755
* Completion/Exception callback for Isoc OUT Xfers.
3757
* @param pPipe The Intr pipe handle.
3758
* @param pReq The Intr request.
3759
* @remarks Completion callback executes in interrupt context!
3761
LOCAL void vboxUSBSolarisIsocOutXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3763
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3765
vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3768
vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->isoc_client_private;
3769
if (RT_LIKELY(pUrb))
3771
size_t cbActPkt = 0;
3772
for (int i = 0; i < pReq->isoc_pkts_count; i++)
3774
cbActPkt += pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3775
pUrb->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3776
pUrb->aIsocPkts[i].enmStatus = vboxUSBSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3779
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted cIsocPkts=%d cbData=%d cbActPkt=%d\n", pUrb->cIsocPkts, pUrb->cbDataR3, cbActPkt));
3781
if (pReq->isoc_completion_reason == USB_CR_OK)
3783
if (RT_UNLIKELY(pUrb->pMsg != pReq->isoc_data)) /* Paranoia */
3785
freemsg(pUrb->pMsg);
3786
pUrb->pMsg = pReq->isoc_data;
3789
pReq->isoc_data = NULL;
3791
pUrb->cIsocPkts = pReq->isoc_pkts_count;
3792
pUrb->cbDataR3 = cbActPkt;
3795
* Update the URB and move to landed list for reaping.
3797
vboxUSBSolarisDeQueueURB(pUrb, pReq->isoc_completion_reason);
3798
usb_free_isoc_req(pReq);
3802
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted missing private data!?! Dropping OUT pUrb.\n"));
3805
LogFlow((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted Pipe Gone.\n"));
3807
usb_free_isoc_req(pReq);
3812
* Callback when the device gets disconnected.
3814
* @param pDip The module structure instance.
3816
* @returns Solaris USB error code.
3818
LOCAL int vboxUSBSolarisDeviceDisconnected(dev_info_t *pDip)
3820
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDeviceDisconnected pDip=%p\n", pDip));
3822
int instance = ddi_get_instance(pDip);
3823
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
3825
if (RT_LIKELY(pState))
3828
* Serialize access: exclusive access to the state.
3830
usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3831
mutex_enter(&pState->Mtx);
3833
pState->DevState = USB_DEV_DISCONNECTED;
3835
vboxUSBSolarisCloseAllPipes(pState, true /* ControlPipe */);
3836
vboxUSBSolarisNotifyHotplug(pState);
3838
mutex_exit(&pState->Mtx);
3839
usb_release_access(pState->StateMulti);
3844
LogRel((DEVICE_NAME ":vboxUSBSolarisDeviceDisconnected failed to get device state!\n"));
3850
* Callback when the device gets reconnected.
3852
* @param pDip The module structure instance.
3854
* @returns Solaris USB error code.
3856
LOCAL int vboxUSBSolarisDeviceReconnected(dev_info_t *pDip)
3858
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDeviceReconnected pDip=%p\n", pDip));
3860
int instance = ddi_get_instance(pDip);
3861
vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
3863
if (RT_LIKELY(pState))
3865
vboxUSBSolarisDeviceRestore(pState);
3869
LogRel((DEVICE_NAME ":vboxUSBSolarisDeviceReconnected failed to get device state!\n"));
3875
* Restore device state after a reconnect or resume.
3877
* @param pState The USB device instance.
3879
LOCAL void vboxUSBSolarisDeviceRestore(vboxusb_state_t *pState)
3881
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDeviceRestore pState=%p\n", pState));
3882
AssertPtrReturnVoid(pState);
3885
* Raise device power.
3887
vboxUSBSolarisPowerBusy(pState);
3888
int rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
3891
* Check if the same device is resumed/reconnected.
3893
rc = usb_check_same_device(pState->pDip,
3894
NULL, /* log handle */
3895
USB_LOG_L2, /* log level */
3897
USB_CHK_ALL, /* check level */
3898
NULL); /* device string */
3900
if (rc != USB_SUCCESS)
3902
mutex_enter(&pState->Mtx);
3903
pState->DevState = USB_DEV_DISCONNECTED;
3904
mutex_exit(&pState->Mtx);
3906
/* Do we need to inform userland here? */
3907
vboxUSBSolarisPowerIdle(pState);
3908
LogFlow((DEVICE_NAME ":vboxUSBSolarisDeviceRestore not the same device.\n"));
3913
* Serialize access to not race with other PM functions.
3915
usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3917
mutex_enter(&pState->Mtx);
3918
if (pState->DevState == USB_DEV_DISCONNECTED)
3919
pState->DevState = USB_DEV_ONLINE;
3920
else if (pState->DevState == USB_DEV_SUSPENDED)
3921
pState->DevState = USB_DEV_ONLINE;
3923
mutex_exit(&pState->Mtx);
3924
usb_release_access(pState->StateMulti);
3926
vboxUSBSolarisPowerIdle(pState);
3931
* Restore device state after a reconnect or resume.
3933
* @param pState The USB device instance.
3935
* @returns VBox status code.
3937
LOCAL int vboxUSBSolarisDeviceSuspend(vboxusb_state_t *pState)
3939
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDeviceSuspend pState=%p\n", pState));
3941
int rc = VERR_VUSB_DEVICE_IS_SUSPENDED;
3942
mutex_enter(&pState->Mtx);
3944
switch (pState->DevState)
3946
case USB_DEV_SUSPENDED:
3948
LogRel((DEVICE_NAME ":vboxUSBSolarisDeviceSuspend: Invalid device state %d\n", pState->DevState));
3952
case USB_DEV_ONLINE:
3953
case USB_DEV_DISCONNECTED:
3954
case USB_DEV_PWRED_DOWN:
3956
int PreviousState = pState->DevState;
3957
pState->DevState = USB_DEV_DISCONNECTED;
3960
* Drain pending URBs.
3962
for (int i = 0; i < VBOXUSB_DRAIN_TIME; i++)
3964
if (pState->cInflightUrbs < 1)
3967
mutex_exit(&pState->Mtx);
3968
delay(drv_usectohz(100000));
3969
mutex_enter(&pState->Mtx);
3973
* Deny suspend if we still have pending URBs.
3975
if (pState->cInflightUrbs > 0)
3977
pState->DevState = PreviousState;
3978
LogRel((DEVICE_NAME ":Cannot suspend, still have %d inflight URBs.\n", pState->cInflightUrbs));
3980
mutex_exit(&pState->Mtx);
3981
return VERR_RESOURCE_BUSY;
3984
pState->cInflightUrbs = 0;
3987
* Serialize access to not race with Open/Detach/Close and
3988
* Close all pipes including the default pipe.
3990
mutex_exit(&pState->Mtx);
3991
usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3992
mutex_enter(&pState->Mtx);
3994
vboxUSBSolarisCloseAllPipes(pState, true /* default pipe */);
3995
vboxUSBSolarisNotifyHotplug(pState);
3997
mutex_exit(&pState->Mtx);
3998
usb_release_access(pState->StateMulti);
3999
return VINF_SUCCESS;
4003
mutex_exit(&pState->Mtx);
4004
LogFlow((DEVICE_NAME ":vboxUSBSolarisDeviceSuspend returns %d\n", rc));
4010
* Restore device state after a reconnect or resume.
4012
* @param pState The USB device instance.
4014
LOCAL void vboxUSBSolarisDeviceResume(vboxusb_state_t *pState)
4016
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisDeviceResume pState=%p\n", pState));
4017
return vboxUSBSolarisDeviceRestore(pState);
4022
* Flag the PM component as busy so the system will not manage it's power.
4024
* @param pState The USB device instance.
4026
LOCAL void vboxUSBSolarisPowerBusy(vboxusb_state_t *pState)
4028
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisPowerBusy pState=%p\n", pState));
4029
AssertPtrReturnVoid(pState);
4031
mutex_enter(&pState->Mtx);
4034
pState->pPower->PowerBusy++;
4035
mutex_exit(&pState->Mtx);
4037
int rc = pm_busy_component(pState->pDip, 0 /* component */);
4038
if (rc != DDI_SUCCESS)
4040
LogFlow((DEVICE_NAME ":vboxUSBSolarisPowerBusy busy component failed! rc=%d\n", rc));
4041
mutex_enter(&pState->Mtx);
4042
pState->pPower->PowerBusy--;
4043
mutex_exit(&pState->Mtx);
4047
mutex_exit(&pState->Mtx);
4052
* Flag the PM component as idle so its power managed by the system.
4054
* @param pState The USB device instance.
4056
LOCAL void vboxUSBSolarisPowerIdle(vboxusb_state_t *pState)
4058
LogFlowFunc((DEVICE_NAME ":vboxUSBSolarisPowerIdle pState=%p\n", pState));
4059
AssertPtrReturnVoid(pState);
4063
int rc = pm_idle_component(pState->pDip, 0 /* component */);
4064
if (rc == DDI_SUCCESS)
4066
mutex_enter(&pState->Mtx);
4067
Assert(pState->pPower->PowerBusy > 0);
4068
pState->pPower->PowerBusy--;
4069
mutex_exit(&pState->Mtx);
4072
LogFlow((DEVICE_NAME ":vboxUSBSolarisPowerIdle idle component failed! rc=%d\n", rc));