VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/solaris/VBoxUSB-solaris.c

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 140.9 KB
Line 
1/* $Id: VBoxUSB-solaris.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VirtualBox USB Client Driver, Solaris Hosts.
4 */
5
6/*
7 * Copyright (C) 2008-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP LOG_GROUP_USB_DRV
42#include <VBox/version.h>
43#include <VBox/log.h>
44#include <VBox/err.h>
45#include <VBox/cdefs.h>
46#include <VBox/sup.h>
47#include <VBox/usblib-solaris.h>
48
49#include <iprt/assert.h>
50#include <iprt/initterm.h>
51#include <iprt/semaphore.h>
52#include <iprt/mem.h>
53#include <iprt/process.h>
54#include <iprt/string.h>
55#include <iprt/path.h>
56#include <iprt/thread.h>
57#include <iprt/dbg.h>
58
59#define USBDRV_MAJOR_VER 2
60#define USBDRV_MINOR_VER 0
61#include <sys/usb/usba.h>
62#include <sys/strsun.h>
63#include "usbai_private.h"
64
65
66/*********************************************************************************************************************************
67* Defined Constants And Macros *
68*********************************************************************************************************************************/
69/** The module name. */
70#define DEVICE_NAME "vboxusb"
71/** The module description as seen in 'modinfo'. */
72#define DEVICE_DESC_DRV "VirtualBox USB"
73
74/** -=-=-=-=-=-=- Standard Specifics -=-=-=-=-=-=- */
75/** Max. supported endpoints. */
76#define VBOXUSB_MAX_ENDPOINTS 32
77/** Size of USB Ctrl Xfer Header in bytes. */
78#define VBOXUSB_CTRL_XFER_SIZE 8
79/**
80 * USB2.0 (Sec. 9-13) Bits 10..0 is the max packet size; for high speed Isoc/Intr, bits 12..11 is
81 * number of additional transaction opportunities per microframe.
82 */
83#define VBOXUSB_PKT_SIZE(pkt) (pkt & 0x07FF) * (1 + ((pkt >> 11) & 3))
84/** Endpoint Xfer Type. */
85#define VBOXUSB_XFER_TYPE(endp) ((endp)->EpDesc.bmAttributes & USB_EP_ATTR_MASK)
86/** Endpoint Xfer Direction. */
87#define VBOXUSB_XFER_DIR(endp) ((endp)->EpDesc.bEndpointAddress & USB_EP_DIR_IN)
88/** Create an Endpoint index from an Endpoint address. */
89#define VBOXUSB_GET_EP_INDEX(epaddr) (((epaddr) & USB_EP_NUM_MASK) + \
90 (((epaddr) & USB_EP_DIR_MASK) ? 16 : 0))
91
92
93/** -=-=-=-=-=-=- Tunable Parameters -=-=-=-=-=-=- */
94/** Time to wait while draining inflight UBRs on suspend, in seconds. */
95#define VBOXUSB_DRAIN_TIME 20
96/** Ctrl Xfer timeout in seconds. */
97#define VBOXUSB_CTRL_XFER_TIMEOUT 15
98/** Maximum URB queue length. */
99#define VBOXUSB_URB_QUEUE_SIZE 512
100/** Maximum asynchronous requests per pipe. */
101#define VBOXUSB_MAX_PIPE_ASYNC_REQS 2
102
103/** For enabling global symbols while debugging. **/
104#if defined(DEBUG_ramshankar)
105# define LOCAL
106#else
107# define LOCAL static
108#endif
109
110
111/*********************************************************************************************************************************
112* Kernel Entry Hooks *
113*********************************************************************************************************************************/
114int VBoxUSBSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
115int VBoxUSBSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
116int VBoxUSBSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
117int VBoxUSBSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
118int VBoxUSBSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal);
119int VBoxUSBSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead);
120int VBoxUSBSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
121int VBoxUSBSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
122int VBoxUSBSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
123int VBoxUSBSolarisPower(dev_info_t *pDip, int Component, int Level);
124
125
126/*********************************************************************************************************************************
127* Structures and Typedefs *
128*********************************************************************************************************************************/
129/**
130 * cb_ops: for drivers that support char/block entry points
131 */
132static struct cb_ops g_VBoxUSBSolarisCbOps =
133{
134 VBoxUSBSolarisOpen,
135 VBoxUSBSolarisClose,
136 nodev, /* b strategy */
137 nodev, /* b dump */
138 nodev, /* b print */
139 VBoxUSBSolarisRead,
140 VBoxUSBSolarisWrite,
141 VBoxUSBSolarisIOCtl,
142 nodev, /* c devmap */
143 nodev, /* c mmap */
144 nodev, /* c segmap */
145 VBoxUSBSolarisPoll,
146 ddi_prop_op, /* property ops */
147 NULL, /* streamtab */
148 D_NEW | D_MP, /* compat. flag */
149 CB_REV, /* revision */
150 nodev, /* c aread */
151 nodev /* c awrite */
152};
153
154/**
155 * dev_ops: for driver device operations
156 */
157static struct dev_ops g_VBoxUSBSolarisDevOps =
158{
159 DEVO_REV, /* driver build revision */
160 0, /* ref count */
161 VBoxUSBSolarisGetInfo,
162 nulldev, /* identify */
163 nulldev, /* probe */
164 VBoxUSBSolarisAttach,
165 VBoxUSBSolarisDetach,
166 nodev, /* reset */
167 &g_VBoxUSBSolarisCbOps,
168 NULL, /* bus ops */
169 VBoxUSBSolarisPower,
170 ddi_quiesce_not_needed
171};
172
173/**
174 * modldrv: export driver specifics to the kernel
175 */
176static struct modldrv g_VBoxUSBSolarisModule =
177{
178 &mod_driverops, /* extern from kernel */
179 DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
180 &g_VBoxUSBSolarisDevOps
181};
182
183/**
184 * modlinkage: export install/remove/info to the kernel
185 */
186static struct modlinkage g_VBoxUSBSolarisModLinkage =
187{
188 MODREV_1,
189 &g_VBoxUSBSolarisModule,
190 NULL,
191};
192
193/**
194 * vboxusb_ep_t: Endpoint structure with info. for managing an endpoint.
195 */
196typedef struct vboxusb_ep_t
197{
198 bool fInitialized; /* Whether this Endpoint is initialized */
199 usb_ep_descr_t EpDesc; /* Endpoint descriptor */
200 usb_pipe_handle_t pPipe; /* Endpoint pipe handle */
201 usb_pipe_policy_t PipePolicy; /* Endpoint policy */
202 bool fIsocPolling; /* Whether Isoc. IN polling is enabled */
203 list_t hIsocInUrbs; /* Isoc. IN inflight URBs */
204 uint16_t cIsocInUrbs; /* Number of Isoc. IN inflight URBs */
205 list_t hIsocInLandedReqs; /* Isoc. IN landed requests */
206 uint16_t cbIsocInLandedReqs; /* Cumulative size of landed Isoc. IN requests */
207 size_t cbMaxIsocData; /* Maximum size of Isoc. IN landed buffer */
208} vboxusb_ep_t;
209
210/**
211 * vboxusb_isoc_req_t: Isoc IN. requests queued from device till they are reaped.
212 */
213typedef struct vboxusb_isoc_req_t
214{
215 mblk_t *pMsg; /* Pointer to the data buffer */
216 uint32_t cIsocPkts; /* Number of Isoc pkts */
217 VUSBISOC_PKT_DESC aIsocPkts[8]; /* Array of Isoc pkt descriptors */
218 list_node_t hListLink;
219} vboxusb_isoc_req_t;
220
221/**
222 * VBOXUSB_URB_STATE: Internal USB URB state.
223 */
224typedef enum VBOXUSB_URB_STATE
225{
226 VBOXUSB_URB_STATE_FREE = 0x00,
227 VBOXUSB_URB_STATE_INFLIGHT = 0x04,
228 VBOXUSB_URB_STATE_LANDED = 0x08
229} VBOXUSB_URB_STATE;
230
231/**
232 * vboxusb_urb_t: kernel URB representation.
233 */
234typedef struct vboxusb_urb_t
235{
236 void *pvUrbR3; /* Userspace URB address (untouched, returned while reaping) */
237 uint8_t bEndpoint; /* Endpoint address */
238 VUSBXFERTYPE enmType; /* Xfer type */
239 VUSBDIRECTION enmDir; /* Xfer direction */
240 VUSBSTATUS enmStatus; /* URB status */
241 bool fShortOk; /* Whether receiving less data than requested is acceptable */
242 RTR3PTR pvDataR3; /* Userspace address of the original data buffer */
243 size_t cbDataR3; /* Size of the data buffer */
244 mblk_t *pMsg; /* Pointer to the data buffer */
245 uint32_t cIsocPkts; /* Number of Isoc pkts */
246 VUSBISOC_PKT_DESC aIsocPkts[8]; /* Array of Isoc pkt descriptors */
247 VBOXUSB_URB_STATE enmState; /* URB state (free/in-flight/landed). */
248 struct vboxusb_state_t *pState; /* Pointer to the device instance */
249 list_node_t hListLink; /* List node link handle */
250} vboxusb_urb_t;
251
252/**
253 * vboxusb_power_t: Per Device Power Management info.
254 */
255typedef struct vboxusb_power_t
256{
257 uint_t PowerStates; /* Bit mask of the power states */
258 int PowerBusy; /* Busy reference counter */
259 bool fPowerWakeup; /* Whether remote power wakeup is enabled */
260 bool fPowerRaise; /* Whether to raise the power level */
261 uint8_t PowerLevel; /* Current power level */
262} vboxusb_power_t;
263
264/**
265 * vboxusb_state_t: Per Device instance state info.
266 */
267typedef struct vboxusb_state_t
268{
269 dev_info_t *pDip; /* Per instance device info. */
270 usb_client_dev_data_t *pDevDesc; /* Parsed & complete device descriptor */
271 uint8_t DevState; /* Current USB Device state */
272 bool fDefaultPipeOpen; /* Whether the device (default control pipe) is closed */
273 bool fPollPending; /* Whether the userland process' poll is pending */
274 kmutex_t Mtx; /* Mutex state protection */
275 usb_serialization_t StateMulti; /* State serialization */
276 size_t cbMaxBulkXfer; /* Maximum bulk xfer size */
277 vboxusb_ep_t aEps[VBOXUSB_MAX_ENDPOINTS]; /* Array of all endpoints structures */
278 list_t hFreeUrbs; /* List of free URBs */
279 list_t hInflightUrbs; /* List of inflight URBs */
280 list_t hLandedUrbs; /* List of landed URBs */
281 uint32_t cFreeUrbs; /* Number of free URBs */
282 uint32_t cInflightUrbs; /* Number of inflight URBs */
283 uint32_t cLandedUrbs; /* Number of landed URBs */
284 pollhead_t PollHead; /* Handle to pollhead for waking polling processes */
285 RTPROCESS Process; /* The process (pid) of the user session */
286 VBOXUSBREQ_CLIENT_INFO ClientInfo; /* Registration data */
287 vboxusb_power_t *pPower; /* Power Management */
288 char szMfg[255]; /* Parsed manufacturer string */
289 char szProduct[255]; /* Parsed product string */
290} vboxusb_state_t;
291AssertCompileMemberSize(vboxusb_state_t, szMfg, USB_MAXSTRINGLEN);
292AssertCompileMemberSize(vboxusb_state_t, szProduct, USB_MAXSTRINGLEN);
293
294
295/*********************************************************************************************************************************
296* Internal Functions *
297*********************************************************************************************************************************/
298LOCAL int vboxUsbSolarisInitEp(vboxusb_state_t *pState, usb_ep_data_t *pEpData);
299LOCAL int vboxUsbSolarisInitEpsForCfg(vboxusb_state_t *pState);
300LOCAL int vboxUsbSolarisInitEpsForIfAlt(vboxusb_state_t *pState, uint8_t bIf, uint8_t bAlt);
301LOCAL void vboxUsbSolarisDestroyAllEps(vboxusb_state_t *pState);
302LOCAL void vboxUsbSolarisDestroyEp(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
303LOCAL void vboxUsbSolarisCloseAllPipes(vboxusb_state_t *pState, bool fControlPipe);
304LOCAL int vboxUsbSolarisOpenPipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
305LOCAL void vboxUsbSolarisClosePipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
306LOCAL int vboxUsbSolarisCtrlXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
307LOCAL void vboxUsbSolarisCtrlXferCompleted(usb_pipe_handle_t pPipe, usb_ctrl_req_t *pReq);
308LOCAL int vboxUsbSolarisBulkXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *purb);
309LOCAL void vboxUsbSolarisBulkXferCompleted(usb_pipe_handle_t pPipe, usb_bulk_req_t *pReq);
310LOCAL int vboxUsbSolarisIntrXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
311LOCAL void vboxUsbSolarisIntrXferCompleted(usb_pipe_handle_t pPipe, usb_intr_req_t *pReq);
312LOCAL int vboxUsbSolarisIsocXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
313LOCAL void vboxUsbSolarisIsocInXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
314LOCAL void vboxUsbSolarisIsocInXferError(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
315LOCAL void vboxUsbSolarisIsocOutXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
316LOCAL vboxusb_urb_t *vboxUsbSolarisGetIsocInUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq);
317LOCAL vboxusb_urb_t *vboxUsbSolarisQueueUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, mblk_t *pMsg);
318LOCAL VUSBSTATUS vboxUsbSolarisGetUrbStatus(usb_cr_t Status);
319LOCAL void vboxUsbSolarisConcatMsg(vboxusb_urb_t *pUrb);
320LOCAL void vboxUsbSolarisDeQueueUrb(vboxusb_urb_t *pUrb, int URBStatus);
321LOCAL void vboxUsbSolarisNotifyComplete(vboxusb_state_t *pState);
322LOCAL int vboxUsbSolarisProcessIOCtl(int iFunction, void *pvState, int Mode, PVBOXUSBREQ pUSBReq, void *pvBuf,
323 size_t *pcbDataOut);
324LOCAL bool vboxUsbSolarisIsUSBDevice(dev_info_t *pDip);
325
326/** @name Device Operation Hooks
327 * @{ */
328LOCAL int vboxUsbSolarisSendUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode);
329LOCAL int vboxUsbSolarisReapUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode);
330LOCAL int vboxUsbSolarisClearEndPoint(vboxusb_state_t *pState, uint8_t bEndpoint);
331LOCAL int vboxUsbSolarisSetConfig(vboxusb_state_t *pState, uint8_t bCfgValue);
332LOCAL int vboxUsbSolarisGetConfig(vboxusb_state_t *pState, uint8_t *pCfgValue);
333LOCAL int vboxUsbSolarisSetInterface(vboxusb_state_t *pState, uint8_t bIf, uint8_t bAlt);
334LOCAL int vboxUsbSolarisCloseDevice(vboxusb_state_t *pState, VBOXUSB_RESET_LEVEL enmReset);
335LOCAL int vboxUsbSolarisAbortPipe(vboxusb_state_t *pState, uint8_t bEndpoint);
336LOCAL int vboxUsbSolarisGetConfigIndex(vboxusb_state_t *pState, uint_t bCfgValue);
337/** @} */
338
339/** @name Hotplug & Power Management Hooks
340 * @{ */
341LOCAL void vboxUsbSolarisNotifyUnplug(vboxusb_state_t *pState);
342LOCAL int vboxUsbSolarisDeviceDisconnected(dev_info_t *pDip);
343LOCAL int vboxUsbSolarisDeviceReconnected(dev_info_t *pDip);
344
345LOCAL int vboxUsbSolarisInitPower(vboxusb_state_t *pState);
346LOCAL void vboxUsbSolarisDestroyPower(vboxusb_state_t *pState);
347LOCAL int vboxUsbSolarisDeviceSuspend(vboxusb_state_t *pState);
348LOCAL void vboxUsbSolarisDeviceResume(vboxusb_state_t *pState);
349LOCAL void vboxUsbSolarisDeviceRestore(vboxusb_state_t *pState);
350LOCAL void vboxUsbSolarisPowerBusy(vboxusb_state_t *pState);
351LOCAL void vboxUsbSolarisPowerIdle(vboxusb_state_t *pState);
352/** @} */
353
354/** @name Monitor Hooks
355 * @{ */
356int VBoxUSBMonSolarisRegisterClient(dev_info_t *pClientDip, PVBOXUSB_CLIENT_INFO pClientInfo);
357int VBoxUSBMonSolarisUnregisterClient(dev_info_t *pClientDip);
358/** @} */
359
360/** @name Callbacks from Monitor
361 * @{ */
362LOCAL int vboxUsbSolarisSetConsumerCredentials(RTPROCESS Process, int Instance, void *pvReserved);
363/** @} */
364
365
366/*********************************************************************************************************************************
367* Global Variables *
368*********************************************************************************************************************************/
369/** Global list of all device instances. */
370static void *g_pVBoxUSBSolarisState;
371
372/** The default endpoint descriptor */
373static usb_ep_descr_t g_VBoxUSBSolarisDefaultEpDesc = { 7, 5, 0, USB_EP_ATTR_CONTROL, 8, 0 };
374
375/** Size of the usb_ep_data_t struct (used to index into data). */
376static size_t g_cbUsbEpData = ~0UL;
377
378/** The offset of usb_ep_data_t::ep_desc. */
379static size_t g_offUsbEpDataDescr = ~0UL;
380
381
382#ifdef LOG_ENABLED
383/**
384 * Gets the description of an Endpoint's transfer type.
385 *
386 * @param pEp The Endpoint.
387 * @returns The type of the Endpoint.
388 */
389static const char *vboxUsbSolarisEpType(vboxusb_ep_t *pEp)
390{
391 uint8_t uType = VBOXUSB_XFER_TYPE(pEp);
392 switch (uType)
393 {
394 case 0: return "CTRL";
395 case 1: return "ISOC";
396 case 2: return "BULK";
397 default: return "INTR";
398 }
399}
400
401
402/**
403 * Gets the description of an Endpoint's direction.
404 *
405 * @param pEp The Endpoint.
406 * @returns The direction of the Endpoint.
407 */
408static const char *vboxUsbSolarisEpDir(vboxusb_ep_t *pEp)
409{
410 return VBOXUSB_XFER_DIR(pEp) == USB_EP_DIR_IN ? "IN " : "OUT";
411}
412#endif
413
414
415/**
416 * Caches device strings from the parsed device descriptors.
417 *
418 * @param pState The USB device instance.
419 *
420 * @remarks Must only be called after usb_get_dev_data().
421 */
422static void vboxUsbSolarisGetDeviceStrings(vboxusb_state_t *pState)
423{
424 AssertReturnVoid(pState);
425 AssertReturnVoid(pState->pDevDesc);
426
427 if (pState->pDevDesc->dev_product)
428 strlcpy(&pState->szMfg[0], pState->pDevDesc->dev_mfg, sizeof(pState->szMfg));
429 else
430 strlcpy(&pState->szMfg[0], "<Unknown Manufacturer>", sizeof(pState->szMfg));
431
432 if (pState->pDevDesc->dev_product)
433 strlcpy(&pState->szProduct[0], pState->pDevDesc->dev_product, sizeof(pState->szProduct));
434 else
435 strlcpy(&pState->szProduct[0], "<Unnamed USB device>", sizeof(pState->szProduct));
436}
437
438
439/**
440 * Queries the necessary symbols at runtime.
441 *
442 * @returns VBox status code.
443 */
444LOCAL int vboxUsbSolarisQuerySymbols(void)
445{
446 RTDBGKRNLINFO hKrnlDbgInfo;
447 int rc = RTR0DbgKrnlInfoOpen(&hKrnlDbgInfo, 0 /* fFlags */);
448 if (RT_SUCCESS(rc))
449 {
450 /*
451 * Query and sanitize the size of usb_ep_data_t struct.
452 */
453 size_t cbPrevUsbEpData = g_cbUsbEpData;
454 rc = RTR0DbgKrnlInfoQuerySize(hKrnlDbgInfo, "usba", "usb_ep_data_t", &g_cbUsbEpData);
455 if (RT_FAILURE(rc))
456 {
457 LogRel(("Failed to query size of \"usb_ep_data_t\" in the \"usba\" module, rc=%Rrc\n", rc));
458 return rc;
459 }
460 if (g_cbUsbEpData > _4K)
461 {
462 LogRel(("Size of \"usb_ep_data_t\" (%u bytes) seems implausible, too paranoid to continue\n", g_cbUsbEpData));
463 return VERR_MISMATCH;
464 }
465
466 /*
467 * Query and sanitizie the offset of usb_ep_data_t::ep_descr.
468 */
469 size_t offPrevUsbEpDataDescr = g_offUsbEpDataDescr;
470 rc = RTR0DbgKrnlInfoQueryMember(hKrnlDbgInfo, "usba", "usb_ep_data_t", "ep_descr", &g_offUsbEpDataDescr);
471 if (RT_FAILURE(rc))
472 {
473 LogRel(("Failed to query offset of usb_ep_data_t::ep_descr, rc=%Rrc\n", rc));
474 return rc;
475 }
476 if (g_offUsbEpDataDescr > _4K - sizeof(usb_ep_descr_t))
477 {
478 LogRel(("Offset of \"ep_desrc\" (%u) seems implausible, too paranoid to continue\n", g_offUsbEpDataDescr));
479 return VERR_MISMATCH;
480 }
481
482 /*
483 * Log only when it changes / first time, since _init() seems to be called often (e.g. on failed attaches).
484 * cmn_err, CE_CONT and '!' is used to not show the message on console during boot each time.
485 */
486 if ( cbPrevUsbEpData != g_cbUsbEpData
487 || offPrevUsbEpDataDescr != g_offUsbEpDataDescr)
488 {
489 cmn_err(CE_CONT, "!usba_ep_data_t is %lu bytes\n", g_cbUsbEpData);
490 cmn_err(CE_CONT, "!usba_ep_data_t::ep_descr @ 0x%lx (%ld)\n", g_offUsbEpDataDescr, g_offUsbEpDataDescr);
491 }
492
493 RTR0DbgKrnlInfoRelease(hKrnlDbgInfo);
494 }
495
496 return rc;
497}
498
499
500/**
501 * Kernel entry points
502 */
503int _init(void)
504{
505 LogFunc((DEVICE_NAME ": _init\n"));
506
507 /*
508 * Prevent module autounloading.
509 */
510 modctl_t *pModCtl = mod_getctl(&g_VBoxUSBSolarisModLinkage);
511 if (pModCtl)
512 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
513 else
514 LogRel((DEVICE_NAME ": _init: failed to disable autounloading!\n"));
515
516 /*
517 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
518 */
519 int rc = RTR0Init(0);
520 if (RT_SUCCESS(rc))
521 {
522 rc = vboxUsbSolarisQuerySymbols();
523 if (RT_FAILURE(rc))
524 {
525 RTR0Term();
526 return EINVAL;
527 }
528
529 rc = ddi_soft_state_init(&g_pVBoxUSBSolarisState, sizeof(vboxusb_state_t), 4 /* pre-alloc */);
530 if (!rc)
531 {
532 rc = mod_install(&g_VBoxUSBSolarisModLinkage);
533 if (!rc)
534 return rc;
535
536 LogRel((DEVICE_NAME ": _init: mod_install failed! rc=%d\n", rc));
537 ddi_soft_state_fini(&g_pVBoxUSBSolarisState);
538 }
539 else
540 LogRel((DEVICE_NAME ": _init: failed to initialize soft state\n"));
541
542 RTR0Term();
543 }
544 else
545 LogRel((DEVICE_NAME ": _init: RTR0Init failed! rc=%d\n", rc));
546 return RTErrConvertToErrno(rc);
547}
548
549
550int _fini(void)
551{
552 int rc;
553
554 LogFunc((DEVICE_NAME ": _fini\n"));
555
556 rc = mod_remove(&g_VBoxUSBSolarisModLinkage);
557 if (!rc)
558 {
559 ddi_soft_state_fini(&g_pVBoxUSBSolarisState);
560 RTR0Term();
561 }
562
563 return rc;
564}
565
566
567int _info(struct modinfo *pModInfo)
568{
569 LogFunc((DEVICE_NAME ": _info\n"));
570
571 return mod_info(&g_VBoxUSBSolarisModLinkage, pModInfo);
572}
573
574
575/**
576 * Attach entry point, to attach a device to the system or resume it.
577 *
578 * @param pDip The module structure instance.
579 * @param enmCmd Attach type (ddi_attach_cmd_t)
580 *
581 * @returns Solaris error code.
582 */
583int VBoxUSBSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
584{
585 LogFunc((DEVICE_NAME ": VBoxUSBSolarisAttach: pDip=%p enmCmd=%d\n", pDip, enmCmd));
586
587 int rc;
588 int instance = ddi_get_instance(pDip);
589 vboxusb_state_t *pState = NULL;
590
591 switch (enmCmd)
592 {
593 case DDI_ATTACH:
594 {
595 rc = ddi_soft_state_zalloc(g_pVBoxUSBSolarisState, instance);
596 if (rc == DDI_SUCCESS)
597 {
598 pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
599 if (RT_LIKELY(pState))
600 {
601 pState->pDip = pDip;
602 pState->pDevDesc = NULL;
603 pState->fPollPending = false;
604 pState->cInflightUrbs = 0;
605 pState->cFreeUrbs = 0;
606 pState->cLandedUrbs = 0;
607 pState->Process = NIL_RTPROCESS;
608 pState->pPower = NULL;
609 bzero(pState->aEps, sizeof(pState->aEps));
610 list_create(&pState->hFreeUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
611 list_create(&pState->hInflightUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
612 list_create(&pState->hLandedUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
613
614 /*
615 * There is a bug in usb_client_attach() as of Nevada 120 which panics when we bind to
616 * a non-USB device. So check if we are really binding to a USB device or not.
617 */
618 if (vboxUsbSolarisIsUSBDevice(pState->pDip))
619 {
620 /*
621 * Here starts the USB specifics.
622 */
623 rc = usb_client_attach(pState->pDip, USBDRV_VERSION, 0);
624 if (rc == USB_SUCCESS)
625 {
626 pState->fDefaultPipeOpen = true;
627
628 /*
629 * Parse out the entire descriptor.
630 */
631 rc = usb_get_dev_data(pState->pDip, &pState->pDevDesc, USB_PARSE_LVL_ALL, 0 /* Unused */);
632 if (rc == USB_SUCCESS)
633 {
634 /*
635 * Cache some device descriptor strings.
636 */
637 vboxUsbSolarisGetDeviceStrings(pState);
638#ifdef DEBUG_ramshankar
639 usb_print_descr_tree(pState->pDip, pState->pDevDesc);
640#endif
641
642 /*
643 * Initialize state locks.
644 */
645 mutex_init(&pState->Mtx, NULL, MUTEX_DRIVER, pState->pDevDesc->dev_iblock_cookie);
646 pState->StateMulti = usb_init_serialization(pState->pDip, USB_INIT_SER_CHECK_SAME_THREAD);
647
648 /*
649 * Get maximum bulk transfer size supported by the HCD.
650 */
651 rc = usb_pipe_get_max_bulk_transfer_size(pState->pDip, &pState->cbMaxBulkXfer);
652 if (rc == USB_SUCCESS)
653 {
654 Log((DEVICE_NAME ": VBoxUSBSolarisAttach: cbMaxBulkXfer=%d\n", pState->cbMaxBulkXfer));
655
656 /*
657 * Initialize the default endpoint.
658 */
659 rc = vboxUsbSolarisInitEp(pState, NULL /* pEp */);
660 if (RT_SUCCESS(rc))
661 {
662 /*
663 * Set the device state.
664 */
665 pState->DevState = USB_DEV_ONLINE;
666
667 /*
668 * Initialize power management for the device.
669 */
670 rc = vboxUsbSolarisInitPower(pState);
671 if (RT_SUCCESS(rc))
672 {
673 /*
674 * Initialize endpoints for the current config.
675 */
676 rc = vboxUsbSolarisInitEpsForCfg(pState);
677 AssertRC(rc);
678
679 /*
680 * Publish the minor node.
681 */
682 rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0,
683 "none", "none", 0666);
684 if (RT_LIKELY(rc == DDI_SUCCESS))
685 {
686 /*
687 * Register hotplug callbacks.
688 */
689 rc = usb_register_hotplug_cbs(pState->pDip, &vboxUsbSolarisDeviceDisconnected,
690 &vboxUsbSolarisDeviceReconnected);
691 if (RT_LIKELY(rc == USB_SUCCESS))
692 {
693 /*
694 * Register with our monitor driver.
695 */
696 bzero(&pState->ClientInfo, sizeof(pState->ClientInfo));
697 char szDevicePath[MAXPATHLEN];
698 ddi_pathname(pState->pDip, szDevicePath);
699 RTStrPrintf(pState->ClientInfo.szClientPath,
700 sizeof(pState->ClientInfo.szClientPath),
701 "/devices%s:%s", szDevicePath, DEVICE_NAME);
702 RTStrPrintf(pState->ClientInfo.szDeviceIdent,
703 sizeof(pState->ClientInfo.szDeviceIdent),
704 "%#x:%#x:%d:%s",
705 pState->pDevDesc->dev_descr->idVendor,
706 pState->pDevDesc->dev_descr->idProduct,
707 pState->pDevDesc->dev_descr->bcdDevice, szDevicePath);
708 pState->ClientInfo.Instance = instance;
709 pState->ClientInfo.pfnSetConsumerCredentials = &vboxUsbSolarisSetConsumerCredentials;
710 rc = VBoxUSBMonSolarisRegisterClient(pState->pDip, &pState->ClientInfo);
711 if (RT_SUCCESS(rc))
712 {
713#if 0
714 LogRel((DEVICE_NAME ": Captured %s %s (Ident=%s)\n", pState->szMfg,
715 pState->szProduct, pState->ClientInfo.szDeviceIdent));
716#else
717 /* Until IPRT R0 logging is fixed. See @bugref{6657#c7} */
718 cmn_err(CE_CONT, "Captured %s %s (Ident=%s)\n", pState->szMfg,
719 pState->szProduct, pState->ClientInfo.szDeviceIdent);
720#endif
721 return DDI_SUCCESS;
722 }
723
724 LogRel((DEVICE_NAME ": VBoxUSBMonSolarisRegisterClient failed! rc=%d "
725 "path=%s instance=%d\n", rc, pState->ClientInfo.szClientPath,
726 instance));
727
728 usb_unregister_hotplug_cbs(pState->pDip);
729 }
730 else
731 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: Failed to register hotplug callbacks! rc=%d\n", rc));
732
733 ddi_remove_minor_node(pState->pDip, NULL);
734 }
735 else
736 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: ddi_create_minor_node failed! rc=%d\n", rc));
737
738 mutex_enter(&pState->Mtx);
739 vboxUsbSolarisDestroyPower(pState);
740 mutex_exit(&pState->Mtx);
741 }
742 else
743 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: Failed to init power management! rc=%d\n", rc));
744 }
745 else
746 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: vboxUsbSolarisInitEp failed! rc=%d\n", rc));
747 }
748 else
749 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: usb_pipe_get_max_bulk_transfer_size failed! rc=%d\n", rc));
750
751 usb_fini_serialization(pState->StateMulti);
752 mutex_destroy(&pState->Mtx);
753 usb_free_dev_data(pState->pDip, pState->pDevDesc);
754 }
755 else
756 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: Failed to get device descriptor. rc=%d\n", rc));
757
758 usb_client_detach(pState->pDip, NULL);
759 }
760 else
761 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: usb_client_attach failed! rc=%d\n", rc));
762 }
763 else
764 {
765 /* This would appear on every boot if it were LogRel() */
766 Log((DEVICE_NAME ": VBoxUSBSolarisAttach: Not a USB device\n"));
767 }
768 }
769 else
770 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: Failed to get soft state\n", sizeof(*pState)));
771
772 ddi_soft_state_free(g_pVBoxUSBSolarisState, instance);
773 }
774 else
775 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: Failed to alloc soft state. rc=%d\n", rc));
776
777 return DDI_FAILURE;
778 }
779
780 case DDI_RESUME:
781 {
782 pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
783 if (RT_UNLIKELY(!pState))
784 {
785 LogRel((DEVICE_NAME ": VBoxUSBSolarisAttach: DDI_RESUME failed to get soft state on detach\n"));
786 return DDI_FAILURE;
787 }
788
789 vboxUsbSolarisDeviceResume(pState);
790 return DDI_SUCCESS;
791 }
792
793 default:
794 return DDI_FAILURE;
795 }
796}
797
798
799/**
800 * Detach entry point, to detach a device to the system or suspend it.
801 *
802 * @param pDip The module structure instance.
803 * @param enmCmd Attach type (ddi_attach_cmd_t)
804 *
805 * @returns Solaris error code.
806 */
807int VBoxUSBSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
808{
809 LogFunc((DEVICE_NAME ": VBoxUSBSolarisDetach: pDip=%p enmCmd=%d\n", pDip, enmCmd));
810
811 int instance = ddi_get_instance(pDip);
812 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
813 if (RT_UNLIKELY(!pState))
814 {
815 LogRel((DEVICE_NAME ": VBoxUSBSolarisDetach: Failed to get soft state on detach\n"));
816 return DDI_FAILURE;
817 }
818
819 switch (enmCmd)
820 {
821 case DDI_DETACH:
822 {
823 /*
824 * At this point it must be assumed that the default control pipe has
825 * already been closed by userland (via VBoxUSBSolarisClose() entry point).
826 * Once it's closed we can no longer open or reference the device here.
827 */
828
829 /*
830 * Notify userland if any that we're gone (while resetting device held by us).
831 */
832 mutex_enter(&pState->Mtx);
833 pState->DevState = USB_DEV_DISCONNECTED;
834 vboxUsbSolarisNotifyUnplug(pState);
835 mutex_exit(&pState->Mtx);
836
837
838 /*
839 * Unregister hotplug callback events first without holding the mutex as the callbacks
840 * would otherwise block on the mutex.
841 */
842 usb_unregister_hotplug_cbs(pDip);
843
844 /*
845 * Serialize: paranoid; drain other driver activity.
846 */
847 usb_serialize_access(pState->StateMulti, USB_WAIT, 0 /* timeout */);
848 usb_release_access(pState->StateMulti);
849 mutex_enter(&pState->Mtx);
850
851 /*
852 * Close all pipes.
853 */
854 vboxUsbSolarisCloseAllPipes(pState, true /* ControlPipe */);
855 Assert(!pState->fDefaultPipeOpen);
856
857 /*
858 * Deinitialize power, destroy all endpoints.
859 */
860 vboxUsbSolarisDestroyPower(pState);
861 vboxUsbSolarisDestroyAllEps(pState);
862
863 /*
864 * Free up all URB lists.
865 */
866 vboxusb_urb_t *pUrb = NULL;
867 while ((pUrb = list_remove_head(&pState->hFreeUrbs)) != NULL)
868 {
869 if (pUrb->pMsg)
870 freemsg(pUrb->pMsg);
871 RTMemFree(pUrb);
872 }
873 while ((pUrb = list_remove_head(&pState->hInflightUrbs)) != NULL)
874 {
875 if (pUrb->pMsg)
876 freemsg(pUrb->pMsg);
877 RTMemFree(pUrb);
878 }
879 while ((pUrb = list_remove_head(&pState->hLandedUrbs)) != NULL)
880 {
881 if (pUrb->pMsg)
882 freemsg(pUrb->pMsg);
883 RTMemFree(pUrb);
884 }
885 pState->cFreeUrbs = 0;
886 pState->cLandedUrbs = 0;
887 pState->cInflightUrbs = 0;
888 list_destroy(&pState->hFreeUrbs);
889 list_destroy(&pState->hInflightUrbs);
890 list_destroy(&pState->hLandedUrbs);
891
892 /*
893 * Destroy locks, free up descriptor and detach from USBA.
894 */
895 mutex_exit(&pState->Mtx);
896 usb_fini_serialization(pState->StateMulti);
897 mutex_destroy(&pState->Mtx);
898
899 usb_free_dev_data(pState->pDip, pState->pDevDesc);
900 usb_client_detach(pState->pDip, NULL);
901
902 /*
903 * Deregister with our Monitor driver.
904 */
905 VBoxUSBMonSolarisUnregisterClient(pState->pDip);
906
907 ddi_remove_minor_node(pState->pDip, NULL);
908
909#if 0
910 LogRel((DEVICE_NAME ": Released %s %s (Ident=%s)\n", pState->szMfg, pState->szProduct,
911 pState->ClientInfo.szDeviceIdent));
912#else
913 /* Until IPRT R0 logging is fixed. See @bugref{6657#c7} */
914 cmn_err(CE_CONT, "Released %s %s (Ident=%s)\n", pState->szMfg, pState->szProduct, pState->ClientInfo.szDeviceIdent);
915#endif
916
917 ddi_soft_state_free(g_pVBoxUSBSolarisState, instance);
918 pState = NULL;
919 return DDI_SUCCESS;
920 }
921
922 case DDI_SUSPEND:
923 {
924 int rc = vboxUsbSolarisDeviceSuspend(pState);
925 if (RT_SUCCESS(rc))
926 return DDI_SUCCESS;
927
928 return DDI_FAILURE;
929 }
930
931 default:
932 return DDI_FAILURE;
933 }
934}
935
936
937/**
938 * Info entry point, called by solaris kernel for obtaining driver info.
939 *
940 * @param pDip The module structure instance (do not use).
941 * @param enmCmd Information request type.
942 * @param pvArg Type specific argument.
943 * @param ppvResult Where to store the requested info.
944 *
945 * @returns Solaris error code.
946 */
947int VBoxUSBSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
948{
949 LogFunc((DEVICE_NAME ": VBoxUSBSolarisGetInfo\n"));
950
951 vboxusb_state_t *pState = NULL;
952 int instance = getminor((dev_t)pvArg);
953
954 switch (enmCmd)
955 {
956 case DDI_INFO_DEVT2DEVINFO:
957 {
958 /*
959 * One is to one mapping of instance & minor number as we publish only one minor node per device.
960 */
961 pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
962 if (pState)
963 {
964 *ppvResult = (void *)pState->pDip;
965 return DDI_SUCCESS;
966 }
967 else
968 LogRel((DEVICE_NAME ": VBoxUSBSolarisGetInfo: Failed to get device state\n"));
969 return DDI_FAILURE;
970 }
971
972 case DDI_INFO_DEVT2INSTANCE:
973 {
974 *ppvResult = (void *)(uintptr_t)instance;
975 return DDI_SUCCESS;
976 }
977
978 default:
979 return DDI_FAILURE;
980 }
981}
982
983
984/**
985 * Callback invoked from the VirtualBox USB Monitor driver when a VM process
986 * tries to access this USB client instance.
987 *
988 * This determines which VM process will be allowed to open and access this USB
989 * device.
990 *
991 * @returns VBox status code.
992 *
993 * @param Process The VM process performing the client info. query.
994 * @param Instance This client instance (the one set while we register
995 * ourselves to the Monitor driver)
996 * @param pvReserved Reserved for future, unused.
997 */
998LOCAL int vboxUsbSolarisSetConsumerCredentials(RTPROCESS Process, int Instance, void *pvReserved)
999{
1000 LogFunc((DEVICE_NAME ": vboxUsbSolarisSetConsumerCredentials: Process=%u Instance=%d\n", Process, Instance));
1001 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, Instance);
1002 if (!pState)
1003 {
1004 LogRel((DEVICE_NAME ": vboxUsbSolarisSetConsumerCredentials: Failed to get device state for instance %d\n", Instance));
1005 return VERR_INVALID_STATE;
1006 }
1007
1008 int rc = VINF_SUCCESS;
1009 mutex_enter(&pState->Mtx);
1010
1011 if (pState->Process == NIL_RTPROCESS)
1012 pState->Process = Process;
1013 else
1014 {
1015 LogRel((DEVICE_NAME ": vboxUsbSolarisSetConsumerCredentials: Failed! Process %u already has client open\n",
1016 pState->Process));
1017 rc = VERR_RESOURCE_BUSY;
1018 }
1019
1020 mutex_exit(&pState->Mtx);
1021
1022 return rc;
1023}
1024
1025
1026int VBoxUSBSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
1027{
1028 LogFunc((DEVICE_NAME ": VBoxUSBSolarisOpen: pDev=%p fFlag=%d fType=%d pCred=%p\n", pDev, fFlag, fType, pCred));
1029
1030 /*
1031 * Verify we are being opened as a character device
1032 */
1033 if (fType != OTYP_CHR)
1034 return EINVAL;
1035
1036 /*
1037 * One is to one mapping. (Minor<=>Instance).
1038 */
1039 int instance = getminor((dev_t)*pDev);
1040 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1041 if (!pState)
1042 {
1043 LogRel((DEVICE_NAME ": VBoxUSBSolarisOpen: Failed to get device state for instance %d\n", instance));
1044 return ENXIO;
1045 }
1046
1047 mutex_enter(&pState->Mtx);
1048
1049 /*
1050 * Only one user process can open a device instance at a time.
1051 */
1052 if (pState->Process != RTProcSelf())
1053 {
1054 if (pState->Process == NIL_RTPROCESS)
1055 LogRel((DEVICE_NAME ": VBoxUSBSolarisOpen: No prior information about authorized process\n"));
1056 else
1057 LogRel((DEVICE_NAME ": VBoxUSBSolarisOpen: Process %u is already using this device instance\n", pState->Process));
1058
1059 mutex_exit(&pState->Mtx);
1060 return EPERM;
1061 }
1062
1063 mutex_exit(&pState->Mtx);
1064
1065 NOREF(fFlag);
1066 NOREF(pCred);
1067
1068 return 0;
1069}
1070
1071
1072int VBoxUSBSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred)
1073{
1074 LogFunc((DEVICE_NAME ": VBoxUSBSolarisClose: Dev=%d fFlag=%d fType=%d pCred=%p\n", Dev, fFlag, fType, pCred));
1075
1076 int instance = getminor((dev_t)Dev);
1077 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1078 if (RT_UNLIKELY(!pState))
1079 {
1080 LogRel((DEVICE_NAME ": VBoxUSBSolarisClose: Failed to get device state for instance %d\n", instance));
1081 return ENXIO;
1082 }
1083
1084 mutex_enter(&pState->Mtx);
1085 pState->fPollPending = false;
1086 pState->Process = NIL_RTPROCESS;
1087 mutex_exit(&pState->Mtx);
1088
1089 return 0;
1090}
1091
1092
1093int VBoxUSBSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
1094{
1095 LogFunc((DEVICE_NAME ": VBoxUSBSolarisRead\n"));
1096 return ENOTSUP;
1097}
1098
1099
1100int VBoxUSBSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
1101{
1102 LogFunc((DEVICE_NAME ": VBoxUSBSolarisWrite\n"));
1103 return ENOTSUP;
1104}
1105
1106
1107int VBoxUSBSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
1108{
1109 LogFunc((DEVICE_NAME ": VBoxUSBSolarisPoll: Dev=%d fEvents=%d fAnyYet=%d pReqEvents=%p\n", Dev, fEvents, fAnyYet, pReqEvents));
1110
1111 /*
1112 * Get the device state (one to one mapping).
1113 */
1114 int instance = getminor((dev_t)Dev);
1115 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1116 if (RT_UNLIKELY(!pState))
1117 {
1118 LogRel((DEVICE_NAME ": VBoxUSBSolarisPoll: No state data for %d\n", instance));
1119 return ENXIO;
1120 }
1121
1122 mutex_enter(&pState->Mtx);
1123
1124 /*
1125 * Disconnect event (POLLHUP) is invalid in "fEvents".
1126 */
1127 if (pState->DevState == USB_DEV_DISCONNECTED)
1128 *pReqEvents |= POLLHUP;
1129 else if (pState->cLandedUrbs)
1130 *pReqEvents |= POLLIN;
1131 else
1132 {
1133 *pReqEvents = 0;
1134 if (!fAnyYet)
1135 {
1136 *ppPollHead = &pState->PollHead;
1137 pState->fPollPending = true;
1138 }
1139 }
1140
1141 mutex_exit(&pState->Mtx);
1142
1143 return 0;
1144}
1145
1146
1147int VBoxUSBSolarisPower(dev_info_t *pDip, int Component, int Level)
1148{
1149 LogFunc((DEVICE_NAME ": VBoxUSBSolarisPower: pDip=%p Component=%d Level=%d\n", pDip, Component, Level));
1150
1151 int instance = ddi_get_instance(pDip);
1152 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1153 if (RT_UNLIKELY(!pState))
1154 {
1155 LogRel((DEVICE_NAME ": VBoxUSBSolarisPower: Failed! State Gone\n"));
1156 return DDI_FAILURE;
1157 }
1158
1159 if (!pState->pPower)
1160 return DDI_SUCCESS;
1161
1162 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
1163 mutex_enter(&pState->Mtx);
1164
1165 int rc = USB_FAILURE;
1166 if (pState->DevState == USB_DEV_ONLINE)
1167 {
1168 /*
1169 * Check if we are transitioning to a valid power state.
1170 */
1171 if (!USB_DEV_PWRSTATE_OK(pState->pPower->PowerStates, Level))
1172 {
1173 switch (Level)
1174 {
1175 case USB_DEV_OS_PWR_OFF:
1176 {
1177 if (pState->pPower->PowerBusy)
1178 break;
1179
1180 /*
1181 * USB D3 command.
1182 */
1183 pState->pPower->PowerLevel = USB_DEV_OS_PWR_OFF;
1184 mutex_exit(&pState->Mtx);
1185 rc = USB_SUCCESS; /* usb_set_device_pwrlvl3(pDip); */
1186 mutex_enter(&pState->Mtx);
1187 break;
1188 }
1189
1190 case USB_DEV_OS_FULL_PWR:
1191 {
1192 /*
1193 * Can happen during shutdown of the OS.
1194 */
1195 pState->pPower->PowerLevel = USB_DEV_OS_FULL_PWR;
1196 mutex_exit(&pState->Mtx);
1197 rc = USB_SUCCESS; /* usb_set_device_pwrlvl0(pDip); */
1198 mutex_enter(&pState->Mtx);
1199 break;
1200 }
1201
1202 default: /* Power levels 1, 2 not implemented */
1203 break;
1204 }
1205 }
1206 else
1207 Log((DEVICE_NAME ": VBoxUSBSolarisPower: USB_DEV_PWRSTATE_OK failed\n"));
1208 }
1209 else
1210 rc = USB_SUCCESS;
1211
1212 mutex_exit(&pState->Mtx);
1213 usb_release_access(pState->StateMulti);
1214 return rc == USB_SUCCESS ? DDI_SUCCESS : DDI_FAILURE;
1215}
1216
1217
1218/** @def IOCPARM_LEN
1219 * Gets the length from the ioctl number.
1220 * This is normally defined by sys/ioccom.h on BSD systems...
1221 */
1222#ifndef IOCPARM_LEN
1223# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
1224#endif
1225
1226int VBoxUSBSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
1227{
1228 /* LogFunc((DEVICE_NAME ": VBoxUSBSolarisIOCtl: Dev=%d Cmd=%d pArg=%p Mode=%d\n", Dev, Cmd, pArg)); */
1229
1230 /*
1231 * Get the device state (one to one mapping).
1232 */
1233 int instance = getminor((dev_t)Dev);
1234 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1235 if (RT_UNLIKELY(!pState))
1236 {
1237 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: No state data for %d\n", instance));
1238 return EINVAL;
1239 }
1240
1241 /*
1242 * Read the request wrapper.
1243 */
1244 VBOXUSBREQ ReqWrap;
1245 if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
1246 {
1247 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: Bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd),
1248 sizeof(ReqWrap)));
1249 return ENOTTY;
1250 }
1251
1252 int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
1253 if (RT_UNLIKELY(rc))
1254 {
1255 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%d\n", pArg, Cmd, rc));
1256 return EINVAL;
1257 }
1258
1259 if (ReqWrap.u32Magic != VBOXUSB_MAGIC)
1260 {
1261 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: Bad magic %#x; pArg=%p Cmd=%d\n", ReqWrap.u32Magic, pArg, Cmd));
1262 return EINVAL;
1263 }
1264 if (RT_UNLIKELY( ReqWrap.cbData == 0
1265 || ReqWrap.cbData > _1M*16))
1266 {
1267 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: Bad size %#x; pArg=%p Cmd=%d\n", ReqWrap.cbData, pArg, Cmd));
1268 return EINVAL;
1269 }
1270
1271 /*
1272 * Read the request.
1273 */
1274 void *pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
1275 if (RT_UNLIKELY(!pvBuf))
1276 {
1277 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: RTMemTmpAlloc failed to alloc %d bytes\n", ReqWrap.cbData));
1278 return ENOMEM;
1279 }
1280
1281 rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
1282 if (RT_UNLIKELY(rc))
1283 {
1284 RTMemTmpFree(pvBuf);
1285 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: ddi_copyin failed! pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
1286 return EFAULT;
1287 }
1288 if (RT_UNLIKELY( ReqWrap.cbData == 0
1289 || pvBuf == NULL))
1290 {
1291 RTMemTmpFree(pvBuf);
1292 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: Invalid request! pvBuf=%p cbData=%d\n", pvBuf, ReqWrap.cbData));
1293 return EINVAL;
1294 }
1295
1296 /*
1297 * Process the IOCtl.
1298 */
1299 size_t cbDataOut = 0;
1300 rc = vboxUsbSolarisProcessIOCtl(Cmd, pState, Mode, &ReqWrap, pvBuf, &cbDataOut);
1301 ReqWrap.rc = rc;
1302 rc = 0;
1303
1304 if (RT_UNLIKELY(cbDataOut > ReqWrap.cbData))
1305 {
1306 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: Too much output data %d expected %d Truncating!\n", cbDataOut,
1307 ReqWrap.cbData));
1308 cbDataOut = ReqWrap.cbData;
1309 }
1310
1311 ReqWrap.cbData = cbDataOut;
1312
1313 /*
1314 * Copy VBOXUSBREQ back to userspace (which contains rc for USB operation).
1315 */
1316 rc = ddi_copyout(&ReqWrap, (void *)pArg, sizeof(ReqWrap), Mode);
1317 if (RT_LIKELY(!rc))
1318 {
1319 /*
1320 * Copy payload (if any) back to userspace.
1321 */
1322 if (cbDataOut > 0)
1323 {
1324 rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataOut, Mode);
1325 if (RT_UNLIKELY(rc))
1326 {
1327 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: ddi_copyout failed! pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg,
1328 Cmd, rc));
1329 rc = EFAULT;
1330 }
1331 }
1332 }
1333 else
1334 {
1335 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: ddi_copyout(1)failed! pReqWrap=%p pArg=%p Cmd=%d. rc=%d\n", &ReqWrap, pArg,
1336 Cmd, rc));
1337 rc = EFAULT;
1338 }
1339
1340 *pVal = rc;
1341 RTMemTmpFree(pvBuf);
1342 return rc;
1343}
1344
1345
1346/**
1347 * IOCtl processor for user to kernel and kernel to kernel communication.
1348 *
1349 * @returns VBox status code.
1350 *
1351 * @param iFunction The requested function.
1352 * @param pvState The USB device instance.
1353 * @param Mode The IOCtl mode.
1354 * @param pUSBReq Pointer to the VBOXUSB request.
1355 * @param pvBuf Pointer to the ring-3 URB.
1356 * @param pcbDataOut Where to store the IOCtl OUT data size.
1357 */
1358LOCAL int vboxUsbSolarisProcessIOCtl(int iFunction, void *pvState, int Mode, PVBOXUSBREQ pUSBReq, void *pvBuf,
1359 size_t *pcbDataOut)
1360{
1361 /* LogFunc((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: iFunction=%d pvState=%p pUSBReq=%p\n", iFunction, pvState, pUSBReq)); */
1362
1363 AssertPtrReturn(pvState, VERR_INVALID_PARAMETER);
1364 vboxusb_state_t *pState = (vboxusb_state_t *)pvState;
1365 size_t cbData = pUSBReq->cbData;
1366 int rc;
1367
1368#define CHECKRET_MIN_SIZE(mnemonic, cbMin) \
1369 do { \
1370 if (RT_UNLIKELY(cbData < (cbMin))) \
1371 { \
1372 LogRel((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: " mnemonic ": cbData=%#zx (%zu) min is %#zx (%zu)\n", \
1373 cbData, cbData, (size_t)(cbMin), (size_t)(cbMin))); \
1374 return VERR_BUFFER_OVERFLOW; \
1375 } \
1376 if (RT_UNLIKELY((cbMin) != 0 && !RT_VALID_PTR(pvBuf))) \
1377 { \
1378 LogRel((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: " mnemonic ": Invalid pointer %p\n", pvBuf)); \
1379 return VERR_INVALID_PARAMETER; \
1380 } \
1381 } while (0)
1382
1383 switch (iFunction)
1384 {
1385 case VBOXUSB_IOCTL_SEND_URB:
1386 {
1387 CHECKRET_MIN_SIZE("SEND_URB", sizeof(VBOXUSBREQ_URB));
1388
1389 PVBOXUSBREQ_URB pUrbReq = (PVBOXUSBREQ_URB)pvBuf;
1390 rc = vboxUsbSolarisSendUrb(pState, pUrbReq, Mode);
1391 *pcbDataOut = 0;
1392 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: SEND_URB returned %d\n", rc));
1393 break;
1394 }
1395
1396 case VBOXUSB_IOCTL_REAP_URB:
1397 {
1398 CHECKRET_MIN_SIZE("REAP_URB", sizeof(VBOXUSBREQ_URB));
1399
1400 PVBOXUSBREQ_URB pUrbReq = (PVBOXUSBREQ_URB)pvBuf;
1401 rc = vboxUsbSolarisReapUrb(pState, pUrbReq, Mode);
1402 *pcbDataOut = sizeof(VBOXUSBREQ_URB);
1403 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: REAP_URB returned %d\n", rc));
1404 break;
1405 }
1406
1407 case VBOXUSB_IOCTL_CLEAR_EP:
1408 {
1409 CHECKRET_MIN_SIZE("CLEAR_EP", sizeof(VBOXUSBREQ_CLEAR_EP));
1410
1411 PVBOXUSBREQ_CLEAR_EP pClearEpReq = (PVBOXUSBREQ_CLEAR_EP)pvBuf;
1412 rc = vboxUsbSolarisClearEndPoint(pState, pClearEpReq->bEndpoint);
1413 *pcbDataOut = 0;
1414 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: CLEAR_EP returned %d\n", rc));
1415 break;
1416 }
1417
1418 case VBOXUSB_IOCTL_SET_CONFIG:
1419 {
1420 CHECKRET_MIN_SIZE("SET_CONFIG", sizeof(VBOXUSBREQ_SET_CONFIG));
1421
1422 PVBOXUSBREQ_SET_CONFIG pSetCfgReq = (PVBOXUSBREQ_SET_CONFIG)pvBuf;
1423 rc = vboxUsbSolarisSetConfig(pState, pSetCfgReq->bConfigValue);
1424 *pcbDataOut = 0;
1425 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: SET_CONFIG returned %d\n", rc));
1426 break;
1427 }
1428
1429 case VBOXUSB_IOCTL_SET_INTERFACE:
1430 {
1431 CHECKRET_MIN_SIZE("SET_INTERFACE", sizeof(VBOXUSBREQ_SET_INTERFACE));
1432
1433 PVBOXUSBREQ_SET_INTERFACE pSetInterfaceReq = (PVBOXUSBREQ_SET_INTERFACE)pvBuf;
1434 rc = vboxUsbSolarisSetInterface(pState, pSetInterfaceReq->bInterface, pSetInterfaceReq->bAlternate);
1435 *pcbDataOut = 0;
1436 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: SET_INTERFACE returned %d\n", rc));
1437 break;
1438 }
1439
1440 case VBOXUSB_IOCTL_CLOSE_DEVICE:
1441 {
1442 CHECKRET_MIN_SIZE("CLOSE_DEVICE", sizeof(VBOXUSBREQ_CLOSE_DEVICE));
1443
1444 PVBOXUSBREQ_CLOSE_DEVICE pCloseDeviceReq = (PVBOXUSBREQ_CLOSE_DEVICE)pvBuf;
1445 if ( pCloseDeviceReq->ResetLevel != VBOXUSB_RESET_LEVEL_REATTACH
1446 || (Mode & FKIOCTL))
1447 {
1448 rc = vboxUsbSolarisCloseDevice(pState, pCloseDeviceReq->ResetLevel);
1449 }
1450 else
1451 {
1452 /* Userland IOCtls are not allowed to perform a reattach of the device. */
1453 rc = VERR_NOT_SUPPORTED;
1454 }
1455 *pcbDataOut = 0;
1456 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: CLOSE_DEVICE returned %d\n", rc));
1457 break;
1458 }
1459
1460 case VBOXUSB_IOCTL_ABORT_PIPE:
1461 {
1462 CHECKRET_MIN_SIZE("ABORT_PIPE", sizeof(VBOXUSBREQ_ABORT_PIPE));
1463
1464 PVBOXUSBREQ_ABORT_PIPE pAbortPipeReq = (PVBOXUSBREQ_ABORT_PIPE)pvBuf;
1465 rc = vboxUsbSolarisAbortPipe(pState, pAbortPipeReq->bEndpoint);
1466 *pcbDataOut = 0;
1467 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: ABORT_PIPE returned %d\n", rc));
1468 break;
1469 }
1470
1471 case VBOXUSB_IOCTL_GET_CONFIG:
1472 {
1473 CHECKRET_MIN_SIZE("GET_CONFIG", sizeof(VBOXUSBREQ_GET_CONFIG));
1474
1475 PVBOXUSBREQ_GET_CONFIG pGetCfgReq = (PVBOXUSBREQ_GET_CONFIG)pvBuf;
1476 rc = vboxUsbSolarisGetConfig(pState, &pGetCfgReq->bConfigValue);
1477 *pcbDataOut = sizeof(VBOXUSBREQ_GET_CONFIG);
1478 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: GET_CONFIG returned %d\n", rc));
1479 break;
1480 }
1481
1482 case VBOXUSB_IOCTL_GET_VERSION:
1483 {
1484 CHECKRET_MIN_SIZE("GET_VERSION", sizeof(VBOXUSBREQ_GET_VERSION));
1485
1486 PVBOXUSBREQ_GET_VERSION pGetVersionReq = (PVBOXUSBREQ_GET_VERSION)pvBuf;
1487 pGetVersionReq->u32Major = VBOXUSB_VERSION_MAJOR;
1488 pGetVersionReq->u32Minor = VBOXUSB_VERSION_MINOR;
1489 *pcbDataOut = sizeof(VBOXUSBREQ_GET_VERSION);
1490 rc = VINF_SUCCESS;
1491 Log((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: GET_VERSION returned %d\n", rc));
1492 break;
1493 }
1494
1495 default:
1496 {
1497 LogRel((DEVICE_NAME ": vboxUsbSolarisProcessIOCtl: Unknown request %#x\n", iFunction));
1498 rc = VERR_NOT_SUPPORTED;
1499 *pcbDataOut = 0;
1500 break;
1501 }
1502 }
1503
1504 pUSBReq->cbData = *pcbDataOut;
1505 return rc;
1506}
1507
1508
1509/**
1510 * Initializes device power management.
1511 *
1512 * @param pState The USB device instance.
1513 *
1514 * @returns VBox status code.
1515 */
1516LOCAL int vboxUsbSolarisInitPower(vboxusb_state_t *pState)
1517{
1518 LogFunc((DEVICE_NAME ": vboxUsbSolarisInitPower: pState=%p\n", pState));
1519
1520 int rc = usb_handle_remote_wakeup(pState->pDip, USB_REMOTE_WAKEUP_ENABLE);
1521 if (rc == USB_SUCCESS)
1522 {
1523 vboxusb_power_t *pPower = RTMemAllocZ(sizeof(vboxusb_power_t));
1524 if (RT_LIKELY(pPower))
1525 {
1526 mutex_enter(&pState->Mtx);
1527 pState->pPower = pPower;
1528 pState->pPower->fPowerWakeup = false;
1529 mutex_exit(&pState->Mtx);
1530
1531 uint_t PowerStates;
1532 rc = usb_create_pm_components(pState->pDip, &PowerStates);
1533 if (rc == USB_SUCCESS)
1534 {
1535 pState->pPower->fPowerWakeup = true;
1536 pState->pPower->PowerLevel = USB_DEV_OS_FULL_PWR;
1537 pState->pPower->PowerStates = PowerStates;
1538
1539 rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
1540
1541 if (rc != DDI_SUCCESS)
1542 {
1543 LogRel((DEVICE_NAME ": vboxUsbSolarisInitPower: Failed to raise power level usb(%#x,%#x)\n",
1544 pState->pDevDesc->dev_descr->idVendor, pState->pDevDesc->dev_descr->idProduct));
1545 }
1546 }
1547 else
1548 Log((DEVICE_NAME ": vboxUsbSolarisInitPower: Failed to create power components\n"));
1549
1550 return VINF_SUCCESS;
1551 }
1552 else
1553 rc = VERR_NO_MEMORY;
1554 }
1555 else
1556 {
1557 Log((DEVICE_NAME ": vboxUsbSolarisInitPower: Failed to enable remote wakeup, No PM!\n"));
1558 rc = VINF_SUCCESS;
1559 }
1560
1561 return rc;
1562}
1563
1564
1565/**
1566 * Destroys device power management.
1567 *
1568 * @param pState The USB device instance.
1569 * @remarks Requires the device state mutex to be held.
1570 */
1571LOCAL void vboxUsbSolarisDestroyPower(vboxusb_state_t *pState)
1572{
1573 LogFunc((DEVICE_NAME ": vboxUsbSolarisDestroyPower: pState=%p\n", pState));
1574
1575 if (pState->pPower)
1576 {
1577 mutex_exit(&pState->Mtx);
1578 vboxUsbSolarisPowerBusy(pState);
1579 mutex_enter(&pState->Mtx);
1580
1581 int rc = -1;
1582 if ( pState->pPower->fPowerWakeup
1583 && pState->DevState != USB_DEV_DISCONNECTED)
1584 {
1585 mutex_exit(&pState->Mtx);
1586 rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
1587 if (rc != DDI_SUCCESS)
1588 Log((DEVICE_NAME ": vboxUsbSolarisDestroyPower: Raising power failed! rc=%d\n", rc));
1589
1590 rc = usb_handle_remote_wakeup(pState->pDip, USB_REMOTE_WAKEUP_DISABLE);
1591 if (rc != DDI_SUCCESS)
1592 Log((DEVICE_NAME ": vboxUsbSolarisDestroyPower: Failed to disable remote wakeup\n"));
1593 }
1594 else
1595 mutex_exit(&pState->Mtx);
1596
1597 rc = pm_lower_power(pState->pDip, 0 /* component */, USB_DEV_OS_PWR_OFF);
1598 if (rc != DDI_SUCCESS)
1599 Log((DEVICE_NAME ": vboxUsbSolarisDestroyPower: Lowering power failed! rc=%d\n", rc));
1600
1601 vboxUsbSolarisPowerIdle(pState);
1602 mutex_enter(&pState->Mtx);
1603 RTMemFree(pState->pPower);
1604 pState->pPower = NULL;
1605 }
1606}
1607
1608
1609/**
1610 * Converts Solaris' USBA URB status to VBox's USB URB status.
1611 *
1612 * @param Status Solaris USBA USB URB status.
1613 *
1614 * @returns VBox USB URB status.
1615 */
1616LOCAL VUSBSTATUS vboxUsbSolarisGetUrbStatus(usb_cr_t Status)
1617{
1618 switch (Status)
1619 {
1620 case USB_CR_OK: return VUSBSTATUS_OK;
1621 case USB_CR_CRC: return VUSBSTATUS_CRC;
1622 case USB_CR_DEV_NOT_RESP: return VUSBSTATUS_DNR;
1623 case USB_CR_DATA_UNDERRUN: return VUSBSTATUS_DATA_UNDERRUN;
1624 case USB_CR_DATA_OVERRUN: return VUSBSTATUS_DATA_OVERRUN;
1625 case USB_CR_STALL: return VUSBSTATUS_STALL;
1626 /*
1627 case USB_CR_BITSTUFFING:
1628 case USB_CR_DATA_TOGGLE_MM:
1629 case USB_CR_PID_CHECKFAILURE:
1630 case USB_CR_UNEXP_PID:
1631 case USB_CR_BUFFER_OVERRUN:
1632 case USB_CR_BUFFER_UNDERRUN:
1633 case USB_CR_TIMEOUT:
1634 case USB_CR_NOT_ACCESSED:
1635 case USB_CR_NO_RESOURCES:
1636 case USB_CR_UNSPECIFIED_ERR:
1637 case USB_CR_STOPPED_POLLING:
1638 case USB_CR_PIPE_CLOSING:
1639 case USB_CR_PIPE_RESET:
1640 case USB_CR_NOT_SUPPORTED:
1641 case USB_CR_FLUSHED:
1642 case USB_CR_HC_HARDWARE_ERR:
1643 */
1644 default: return VUSBSTATUS_INVALID;
1645 }
1646}
1647
1648
1649/**
1650 * Converts Solaris' USBA error code to VBox's error code.
1651 *
1652 * @param UsbRc Solaris USBA error code.
1653 *
1654 * @returns VBox error code.
1655 */
1656static int vboxUsbSolarisToVBoxRC(int UsbRc)
1657{
1658 switch (UsbRc)
1659 {
1660 case USB_SUCCESS: return VINF_SUCCESS;
1661 case USB_INVALID_ARGS: return VERR_INVALID_PARAMETER;
1662 case USB_INVALID_PIPE: return VERR_BAD_PIPE;
1663 case USB_INVALID_CONTEXT: return VERR_INVALID_CONTEXT;
1664 case USB_BUSY: return VERR_PIPE_BUSY;
1665 case USB_PIPE_ERROR: return VERR_PIPE_IO_ERROR;
1666 /*
1667 case USB_FAILURE:
1668 case USB_NO_RESOURCES:
1669 case USB_NO_BANDWIDTH:
1670 case USB_NOT_SUPPORTED:
1671 case USB_PIPE_ERROR:
1672 case USB_NO_FRAME_NUMBER:
1673 case USB_INVALID_START_FRAME:
1674 case USB_HC_HARDWARE_ERROR:
1675 case USB_INVALID_REQUEST:
1676 case USB_INVALID_VERSION:
1677 case USB_INVALID_PERM:
1678 */
1679 default: return VERR_GENERAL_FAILURE;
1680 }
1681}
1682
1683
1684/**
1685 * Converts Solaris' USBA device state to VBox's error code.
1686 *
1687 * @param uDeviceState The USB device state to convert.
1688 *
1689 * @returns VBox error code.
1690 */
1691static int vboxUsbSolarisDeviceState(uint8_t uDeviceState)
1692{
1693 switch (uDeviceState)
1694 {
1695 case USB_DEV_ONLINE: return VINF_SUCCESS;
1696 case USB_DEV_SUSPENDED: return VERR_VUSB_DEVICE_IS_SUSPENDED;
1697 case USB_DEV_DISCONNECTED:
1698 case USB_DEV_PWRED_DOWN: return VERR_VUSB_DEVICE_NOT_ATTACHED;
1699 default: return VERR_GENERAL_FAILURE;
1700 }
1701}
1702
1703
1704/**
1705 * Checks if the device is a USB device.
1706 *
1707 * @param pDip Pointer to this device info. structure.
1708 *
1709 * @returns If this is really a USB device returns true, otherwise false.
1710 */
1711LOCAL bool vboxUsbSolarisIsUSBDevice(dev_info_t *pDip)
1712{
1713 int rc = DDI_FAILURE;
1714
1715 /*
1716 * Check device for "usb" compatible property, root hubs->device would likely mean parent has no "usb" property.
1717 */
1718 char **ppszCompatible = NULL;
1719 uint_t cCompatible;
1720 rc = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, pDip, DDI_PROP_DONTPASS, "compatible", &ppszCompatible, &cCompatible);
1721 if (RT_LIKELY(rc == DDI_PROP_SUCCESS))
1722 {
1723 while (cCompatible--)
1724 {
1725 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: Compatible[%d]=%s\n", cCompatible, ppszCompatible[cCompatible]));
1726 if (!strncmp(ppszCompatible[cCompatible], "usb", 3))
1727 {
1728 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: Verified device as USB. pszCompatible=%s\n",
1729 ppszCompatible[cCompatible]));
1730 ddi_prop_free(ppszCompatible);
1731 return true;
1732 }
1733 }
1734
1735 ddi_prop_free(ppszCompatible);
1736 ppszCompatible = NULL;
1737 }
1738 else
1739 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: USB property lookup failed, rc=%d\n", rc));
1740
1741 /*
1742 * Check parent for "usb" compatible property.
1743 */
1744 dev_info_t *pParentDip = ddi_get_parent(pDip);
1745 if (pParentDip)
1746 {
1747 rc = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, pParentDip, DDI_PROP_DONTPASS, "compatible", &ppszCompatible,
1748 &cCompatible);
1749 if (RT_LIKELY(rc == DDI_PROP_SUCCESS))
1750 {
1751 while (cCompatible--)
1752 {
1753 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: Parent compatible[%d]=%s\n", cCompatible,
1754 ppszCompatible[cCompatible]));
1755 if (!strncmp(ppszCompatible[cCompatible], "usb", 3))
1756 {
1757 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: Verified device as USB. parent pszCompatible=%s\n",
1758 ppszCompatible[cCompatible]));
1759 ddi_prop_free(ppszCompatible);
1760 return true;
1761 }
1762 }
1763
1764 ddi_prop_free(ppszCompatible);
1765 ppszCompatible = NULL;
1766 }
1767 else
1768 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: USB parent property lookup failed. rc=%d\n", rc));
1769 }
1770 else
1771 Log((DEVICE_NAME ": vboxUsbSolarisIsUSBDevice: Failed to obtain parent device for property lookup\n"));
1772
1773 return false;
1774}
1775
1776
1777/**
1778 * Submits a URB.
1779 *
1780 * @param pState The USB device instance.
1781 * @param pUrbReq Pointer to the VBox USB URB.
1782 * @param Mode The IOCtl mode.
1783 *
1784 * @returns VBox error code.
1785 */
1786LOCAL int vboxUsbSolarisSendUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode)
1787{
1788 int iEpIndex = VBOXUSB_GET_EP_INDEX(pUrbReq->bEndpoint);
1789 Assert(iEpIndex >= 0 && iEpIndex < RT_ELEMENTS(pState->aEps));
1790 vboxusb_ep_t *pEp = &pState->aEps[iEpIndex];
1791 AssertPtrReturn(pEp, VERR_INVALID_POINTER);
1792 Assert(pUrbReq);
1793
1794#if 0
1795 LogFunc((DEVICE_NAME ": vboxUsbSolarisSendUrb: pState=%p pUrbReq=%p bEndpoint=%#x[%d] enmDir=%#x enmType=%#x "
1796 "cbData=%d pvData=%p\n", pState, pUrbReq, pUrbReq->bEndpoint, iEpIndex, pUrbReq->enmDir,
1797 pUrbReq->enmType, pUrbReq->cbData, pUrbReq->pvData));
1798#endif
1799
1800 if (RT_UNLIKELY(!pUrbReq->pvData))
1801 {
1802 LogRel((DEVICE_NAME ": vboxUsbSolarisSendUrb: Invalid request - No data\n"));
1803 return VERR_INVALID_POINTER;
1804 }
1805
1806 /*
1807 * Allocate message block & copy userspace buffer for host to device Xfers and for
1808 * Control Xfers (since input has Setup header that needs copying).
1809 */
1810 mblk_t *pMsg = NULL;
1811 int rc = VINF_SUCCESS;
1812 if ( pUrbReq->enmDir == VUSBDIRECTION_OUT
1813 || pUrbReq->enmType == VUSBXFERTYPE_MSG)
1814 {
1815 pMsg = allocb(pUrbReq->cbData, BPRI_HI);
1816 if (RT_UNLIKELY(!pMsg))
1817 {
1818 LogRel((DEVICE_NAME ": vboxUsbSolarisSendUrb: Failed to allocate %u bytes\n", pUrbReq->cbData));
1819 return VERR_NO_MEMORY;
1820 }
1821
1822 rc = ddi_copyin(pUrbReq->pvData, pMsg->b_wptr, pUrbReq->cbData, Mode);
1823 if (RT_UNLIKELY(rc))
1824 {
1825 LogRel((DEVICE_NAME ": vboxUsbSolarisSendUrb: ddi_copyin failed! rc=%d\n", rc));
1826 freemsg(pMsg);
1827 return VERR_NO_MEMORY;
1828 }
1829
1830 pMsg->b_wptr += pUrbReq->cbData;
1831 }
1832
1833 mutex_enter(&pState->Mtx);
1834 rc = vboxUsbSolarisDeviceState(pState->DevState);
1835 if (!pState->fDefaultPipeOpen) /* Required for Isoc. IN Xfers which don't Xfer through the pipe after polling starts */
1836 rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
1837 if (RT_SUCCESS(rc))
1838 {
1839 /*
1840 * Open the pipe if needed.
1841 */
1842 rc = vboxUsbSolarisOpenPipe(pState, pEp);
1843 if (RT_UNLIKELY(RT_FAILURE(rc)))
1844 {
1845 mutex_exit(&pState->Mtx);
1846 freemsg(pMsg);
1847 LogRel((DEVICE_NAME ": vboxUsbSolarisSendUrb: OpenPipe failed! pState=%p pUrbReq=%p bEndpoint=%#x enmDir=%#x "
1848 "enmType=%#x cbData=%d pvData=%p rc=%d\n", pState, pUrbReq, pUrbReq->bEndpoint, pUrbReq->enmDir,
1849 pUrbReq->enmType, pUrbReq->cbData, pUrbReq->pvData, rc));
1850 return VERR_BAD_PIPE;
1851 }
1852
1853 mutex_exit(&pState->Mtx);
1854
1855 vboxusb_urb_t *pUrb = NULL;
1856 if ( pUrbReq->enmType == VUSBXFERTYPE_ISOC
1857 && pUrbReq->enmDir == VUSBDIRECTION_IN)
1858 pUrb = vboxUsbSolarisGetIsocInUrb(pState, pUrbReq);
1859 else
1860 pUrb = vboxUsbSolarisQueueUrb(pState, pUrbReq, pMsg);
1861
1862 if (RT_LIKELY(pUrb))
1863 {
1864 switch (pUrb->enmType)
1865 {
1866 case VUSBXFERTYPE_MSG:
1867 {
1868 rc = vboxUsbSolarisCtrlXfer(pState, pEp, pUrb);
1869 break;
1870 }
1871
1872 case VUSBXFERTYPE_BULK:
1873 {
1874 rc = vboxUsbSolarisBulkXfer(pState, pEp, pUrb);
1875 break;
1876 }
1877
1878 case VUSBXFERTYPE_INTR:
1879 {
1880 rc = vboxUsbSolarisIntrXfer(pState, pEp, pUrb);
1881 break;
1882 }
1883
1884 case VUSBXFERTYPE_ISOC:
1885 {
1886 rc = vboxUsbSolarisIsocXfer(pState, pEp, pUrb);
1887 break;
1888 }
1889
1890 default:
1891 {
1892 LogRelMax(5, (DEVICE_NAME ": vboxUsbSolarisSendUrb: URB type unsupported %d\n", pUrb->enmType));
1893 rc = VERR_NOT_SUPPORTED;
1894 break;
1895 }
1896 }
1897
1898 if (RT_FAILURE(rc))
1899 {
1900 mutex_enter(&pState->Mtx);
1901 freemsg(pUrb->pMsg);
1902 pUrb->pMsg = NULL;
1903 pMsg = NULL;
1904
1905 if ( pUrb->enmType == VUSBXFERTYPE_ISOC
1906 && pUrb->enmDir == VUSBDIRECTION_IN)
1907 {
1908 RTMemFree(pUrb);
1909 pUrb = NULL;
1910 }
1911 else
1912 {
1913 /*
1914 * Xfer failed, move URB back to the free list.
1915 */
1916 list_remove(&pState->hInflightUrbs, pUrb);
1917 Assert(pState->cInflightUrbs > 0);
1918 --pState->cInflightUrbs;
1919
1920 pUrb->enmState = VBOXUSB_URB_STATE_FREE;
1921 Assert(!pUrb->pMsg);
1922 list_insert_head(&pState->hFreeUrbs, pUrb);
1923 ++pState->cFreeUrbs;
1924 }
1925 mutex_exit(&pState->Mtx);
1926 }
1927 }
1928 else
1929 {
1930 LogRel((DEVICE_NAME ": vboxUsbSolarisSendUrb: Failed to queue URB\n"));
1931 rc = VERR_NO_MEMORY;
1932 freemsg(pMsg);
1933 }
1934 }
1935 else
1936 {
1937 mutex_exit(&pState->Mtx);
1938 freemsg(pMsg);
1939 }
1940
1941 return rc;
1942}
1943
1944
1945/**
1946 * Reaps a completed URB.
1947 *
1948 * @param pState The USB device instance.
1949 * @param pUrbReq Pointer to the VBox USB URB.
1950 * @param Mode The IOCtl mode.
1951 *
1952 * @returns VBox error code.
1953 */
1954LOCAL int vboxUsbSolarisReapUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode)
1955{
1956 /* LogFunc((DEVICE_NAME ": vboxUsbSolarisReapUrb: pState=%p pUrbReq=%p\n", pState, pUrbReq)); */
1957
1958 AssertPtrReturn(pUrbReq, VERR_INVALID_POINTER);
1959
1960 int rc = VINF_SUCCESS;
1961 mutex_enter(&pState->Mtx);
1962 rc = vboxUsbSolarisDeviceState(pState->DevState);
1963 if (!pState->fDefaultPipeOpen)
1964 rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
1965 if (RT_SUCCESS(rc))
1966 {
1967 vboxusb_urb_t *pUrb = list_remove_head(&pState->hLandedUrbs);
1968 if (pUrb)
1969 {
1970 Assert(pState->cLandedUrbs > 0);
1971 --pState->cLandedUrbs;
1972 }
1973
1974 /*
1975 * It is safe to access pUrb->pMsg outside the state mutex because this is from the landed URB list
1976 * and not the inflight URB list.
1977 */
1978 mutex_exit(&pState->Mtx);
1979 if (pUrb)
1980 {
1981 /*
1982 * Copy the URB which will then be copied to user-space.
1983 */
1984 pUrbReq->pvUrbR3 = pUrb->pvUrbR3;
1985 pUrbReq->bEndpoint = pUrb->bEndpoint;
1986 pUrbReq->enmType = pUrb->enmType;
1987 pUrbReq->enmDir = pUrb->enmDir;
1988 pUrbReq->enmStatus = pUrb->enmStatus;
1989 pUrbReq->pvData = (void *)pUrb->pvDataR3;
1990 pUrbReq->cbData = pUrb->cbDataR3;
1991
1992 if (RT_LIKELY(pUrb->pMsg))
1993 {
1994 /*
1995 * Copy the message back into the user buffer.
1996 */
1997 if (RT_LIKELY(pUrb->pvDataR3 != NIL_RTR3PTR))
1998 {
1999 Assert(!pUrb->pMsg->b_cont); /* We really should have a single message block always. */
2000 size_t cbData = RT_MIN(MBLKL(pUrb->pMsg), pUrb->cbDataR3);
2001 pUrbReq->cbData = cbData;
2002
2003 if (RT_LIKELY(cbData))
2004 {
2005 rc = ddi_copyout(pUrb->pMsg->b_rptr, (void *)pUrbReq->pvData, cbData, Mode);
2006 if (RT_UNLIKELY(rc))
2007 {
2008 LogRel((DEVICE_NAME ": vboxUsbSolarisReapUrb: ddi_copyout failed! rc=%d\n", rc));
2009 pUrbReq->enmStatus = VUSBSTATUS_INVALID;
2010 }
2011 }
2012
2013 Log((DEVICE_NAME ": vboxUsbSolarisReapUrb: pvUrbR3=%p pvDataR3=%p cbData=%d\n", pUrbReq->pvUrbR3,
2014 pUrbReq->pvData, pUrbReq->cbData));
2015 }
2016 else
2017 {
2018 pUrbReq->cbData = 0;
2019 rc = VERR_INVALID_POINTER;
2020 LogRel((DEVICE_NAME ": vboxUsbSolarisReapUrb: Missing pvDataR3!!\n"));
2021 }
2022
2023 /*
2024 * Free buffer allocated in vboxUsbSolarisSendUrb or vboxUsbSolaris[Ctrl|Bulk|Intr]Xfer().
2025 */
2026 freemsg(pUrb->pMsg);
2027 pUrb->pMsg = NULL;
2028 }
2029 else
2030 {
2031 if ( pUrb->enmType == VUSBXFERTYPE_ISOC
2032 && pUrb->enmDir == VUSBDIRECTION_IN)
2033 {
2034 pUrbReq->enmStatus = VUSBSTATUS_INVALID;
2035 pUrbReq->cbData = 0;
2036 }
2037 }
2038
2039 /*
2040 * Copy Isoc packet descriptors.
2041 */
2042 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
2043 {
2044 AssertCompile(sizeof(pUrbReq->aIsocPkts) == sizeof(pUrb->aIsocPkts));
2045 pUrbReq->cIsocPkts = pUrb->cIsocPkts;
2046
2047 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
2048 {
2049 pUrbReq->aIsocPkts[i].cbPkt = pUrb->aIsocPkts[i].cbPkt;
2050 pUrbReq->aIsocPkts[i].cbActPkt = pUrb->aIsocPkts[i].cbActPkt;
2051 pUrbReq->aIsocPkts[i].enmStatus = pUrb->aIsocPkts[i].enmStatus;
2052 }
2053
2054 if (pUrb->enmDir == VUSBDIRECTION_IN)
2055 {
2056 RTMemFree(pUrb);
2057 pUrb = NULL;
2058 }
2059 }
2060
2061 if (pUrb)
2062 {
2063 /*
2064 * Add URB back to the free list.
2065 */
2066 Assert(!pUrb->pMsg);
2067 pUrb->cbDataR3 = 0;
2068 pUrb->pvDataR3 = NIL_RTR3PTR;
2069 pUrb->enmState = VBOXUSB_URB_STATE_FREE;
2070 mutex_enter(&pState->Mtx);
2071 list_insert_head(&pState->hFreeUrbs, pUrb);
2072 ++pState->cFreeUrbs;
2073 mutex_exit(&pState->Mtx);
2074 }
2075 }
2076 else
2077 {
2078 pUrbReq->pvUrbR3 = NULL;
2079 pUrbReq->cbData = 0;
2080 pUrbReq->pvData = NULL;
2081 pUrbReq->enmStatus = VUSBSTATUS_INVALID;
2082 }
2083 }
2084 else
2085 mutex_exit(&pState->Mtx);
2086
2087 return rc;
2088}
2089
2090
2091/**
2092 * Clears a pipe (CLEAR_FEATURE).
2093 *
2094 * @param pState The USB device instance.
2095 * @param bEndpoint The Endpoint address.
2096 *
2097 * @returns VBox error code.
2098 */
2099LOCAL int vboxUsbSolarisClearEndPoint(vboxusb_state_t *pState, uint8_t bEndpoint)
2100{
2101 LogFunc((DEVICE_NAME ": vboxUsbSolarisClearEndPoint: pState=%p bEndpoint=%#x\n", pState, bEndpoint));
2102
2103 mutex_enter(&pState->Mtx);
2104 int rc = vboxUsbSolarisDeviceState(pState->DevState);
2105 if (RT_SUCCESS(rc))
2106 {
2107 int iEpIndex = VBOXUSB_GET_EP_INDEX(bEndpoint);
2108 Assert(iEpIndex >= 0 && iEpIndex < RT_ELEMENTS(pState->aEps));
2109 vboxusb_ep_t *pEp = &pState->aEps[iEpIndex];
2110 if (RT_LIKELY(pEp))
2111 {
2112 /*
2113 * Check if the endpoint is open to be cleared.
2114 */
2115 if (pEp->pPipe)
2116 {
2117 mutex_exit(&pState->Mtx);
2118
2119 /*
2120 * Synchronous reset pipe.
2121 */
2122 usb_pipe_reset(pState->pDip, pEp->pPipe,
2123 USB_FLAGS_SLEEP, /* Synchronous */
2124 NULL, /* Completion callback */
2125 NULL); /* Exception callback */
2126
2127 mutex_enter(&pState->Mtx);
2128
2129 Log((DEVICE_NAME ": vboxUsbSolarisClearEndPoint: bEndpoint=%#x[%d] returns %d\n", bEndpoint, iEpIndex, rc));
2130
2131 rc = VINF_SUCCESS;
2132 }
2133 else
2134 {
2135 Log((DEVICE_NAME ": vboxUsbSolarisClearEndPoint: Not opened to be cleared. Faking success. bEndpoint=%#x\n",
2136 bEndpoint));
2137 rc = VINF_SUCCESS;
2138 }
2139 }
2140 else
2141 {
2142 LogRel((DEVICE_NAME ": vboxUsbSolarisClearEndPoint: Endpoint missing! bEndpoint=%#x[%d]\n", bEndpoint, iEpIndex));
2143 rc = VERR_GENERAL_FAILURE;
2144 }
2145 }
2146 else
2147 Log((DEVICE_NAME ": vboxUsbSolarisClearEndPoint: Device not online, state=%d\n", pState->DevState));
2148
2149 mutex_exit(&pState->Mtx);
2150 return rc;
2151}
2152
2153
2154/**
2155 * Sets configuration (SET_CONFIGURATION)
2156 *
2157 * @param pState The USB device instance.
2158 * @param bConfig The Configuration.
2159 *
2160 * @returns VBox error code.
2161 */
2162LOCAL int vboxUsbSolarisSetConfig(vboxusb_state_t *pState, uint8_t bConfig)
2163{
2164 LogFunc((DEVICE_NAME ": vboxUsbSolarisSetConfig: pState=%p bConfig=%u\n", pState, bConfig));
2165
2166 mutex_enter(&pState->Mtx);
2167 int rc = vboxUsbSolarisDeviceState(pState->DevState);
2168 if (RT_SUCCESS(rc))
2169 {
2170 vboxUsbSolarisCloseAllPipes(pState, false /* ControlPipe */);
2171 int iCfgIndex = vboxUsbSolarisGetConfigIndex(pState, bConfig);
2172
2173 if ( iCfgIndex >= 0
2174 && iCfgIndex < pState->pDevDesc->dev_n_cfg)
2175 {
2176 /*
2177 * Switch Config synchronously.
2178 */
2179 mutex_exit(&pState->Mtx);
2180 rc = usb_set_cfg(pState->pDip, (uint_t)iCfgIndex, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback data */);
2181 mutex_enter(&pState->Mtx);
2182
2183 if (rc == USB_SUCCESS)
2184 {
2185 int rc2 = vboxUsbSolarisInitEpsForCfg(pState);
2186 AssertRC(rc2); NOREF(rc2);
2187 rc = VINF_SUCCESS;
2188 }
2189 else
2190 {
2191 LogRel((DEVICE_NAME ": vboxUsbSolarisSetConfig: usb_set_cfg failed for iCfgIndex=%#x bConfig=%u rc=%d\n",
2192 iCfgIndex, bConfig, rc));
2193 rc = vboxUsbSolarisToVBoxRC(rc);
2194 }
2195 }
2196 else
2197 {
2198 LogRel((DEVICE_NAME ": vboxUsbSolarisSetConfig: Invalid iCfgIndex=%d bConfig=%u\n", iCfgIndex, bConfig));
2199 rc = VERR_OUT_OF_RANGE;
2200 }
2201 }
2202
2203 mutex_exit(&pState->Mtx);
2204
2205 return rc;
2206}
2207
2208
2209/**
2210 * Gets configuration (GET_CONFIGURATION)
2211 *
2212 * @param pState The USB device instance.
2213 * @param pbConfig Where to store the Configuration.
2214 *
2215 * @returns VBox error code.
2216 */
2217LOCAL int vboxUsbSolarisGetConfig(vboxusb_state_t *pState, uint8_t *pbConfig)
2218{
2219 LogFunc((DEVICE_NAME ": vboxUsbSolarisGetConfig: pState=%p pbConfig=%p\n", pState, pbConfig));
2220 AssertPtrReturn(pbConfig, VERR_INVALID_POINTER);
2221
2222 /*
2223 * Get Config synchronously.
2224 */
2225 uint_t bConfig;
2226 int rc = usb_get_cfg(pState->pDip, &bConfig, USB_FLAGS_SLEEP);
2227 if (RT_LIKELY(rc == USB_SUCCESS))
2228 {
2229 *pbConfig = bConfig;
2230 rc = VINF_SUCCESS;
2231 }
2232 else
2233 {
2234 LogRel((DEVICE_NAME ": vboxUsbSolarisGetConfig: Failed, rc=%d\n", rc));
2235 rc = vboxUsbSolarisToVBoxRC(rc);
2236 }
2237
2238 Log((DEVICE_NAME ": vboxUsbSolarisGetConfig: Returns %d bConfig=%u\n", rc, *pbConfig));
2239 return rc;
2240}
2241
2242
2243/**
2244 * Sets interface (SET_INTERFACE) and alternate.
2245 *
2246 * @param pState The USB device instance.
2247 * @param bIf The Interface.
2248 * @param bAlt The Alternate setting.
2249 *
2250 * @returns VBox error code.
2251 */
2252LOCAL int vboxUsbSolarisSetInterface(vboxusb_state_t *pState, uint8_t bIf, uint8_t bAlt)
2253{
2254 LogFunc((DEVICE_NAME ": vboxUsbSolarisSetInterface: pState=%p bIf=%#x bAlt=%#x\n", pState, bIf, bAlt));
2255
2256 mutex_enter(&pState->Mtx);
2257 int rc = vboxUsbSolarisDeviceState(pState->DevState);
2258 if (RT_SUCCESS(rc))
2259 {
2260 /*
2261 * Set Interface & Alt setting synchronously.
2262 */
2263 mutex_exit(&pState->Mtx);
2264 rc = usb_set_alt_if(pState->pDip, bIf, bAlt, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback data */);
2265 mutex_enter(&pState->Mtx);
2266
2267 if (rc == USB_SUCCESS)
2268 {
2269 Log((DEVICE_NAME ": vboxUsbSolarisSetInterface: Success, bIf=%#x bAlt=%#x\n", bIf, bAlt, rc));
2270 int rc2 = vboxUsbSolarisInitEpsForIfAlt(pState, bIf, bAlt);
2271 AssertRC(rc2); NOREF(rc2);
2272 rc = VINF_SUCCESS;
2273 }
2274 else
2275 {
2276 LogRel((DEVICE_NAME ": vboxUsbSolarisSetInterface: usb_set_alt_if failed for bIf=%#x bAlt=%#x rc=%d\n", bIf, bAlt, rc));
2277 rc = vboxUsbSolarisToVBoxRC(rc);
2278 }
2279 }
2280
2281 mutex_exit(&pState->Mtx);
2282
2283 return rc;
2284}
2285
2286
2287/**
2288 * Closes the USB device and optionally resets it.
2289 *
2290 * @param pState The USB device instance.
2291 * @param enmReset The reset level.
2292 *
2293 * @returns VBox error code.
2294 */
2295LOCAL int vboxUsbSolarisCloseDevice(vboxusb_state_t *pState, VBOXUSB_RESET_LEVEL enmReset)
2296{
2297 LogFunc((DEVICE_NAME ": vboxUsbSolarisCloseDevice: pState=%p enmReset=%d\n", pState, enmReset));
2298
2299 mutex_enter(&pState->Mtx);
2300 int rc = vboxUsbSolarisDeviceState(pState->DevState);
2301
2302 if (enmReset == VBOXUSB_RESET_LEVEL_CLOSE)
2303 vboxUsbSolarisCloseAllPipes(pState, true /* ControlPipe */);
2304 else
2305 vboxUsbSolarisCloseAllPipes(pState, false /* ControlPipe */);
2306
2307 mutex_exit(&pState->Mtx);
2308
2309 if (RT_SUCCESS(rc))
2310 {
2311 switch (enmReset)
2312 {
2313 case VBOXUSB_RESET_LEVEL_REATTACH:
2314 rc = usb_reset_device(pState->pDip, USB_RESET_LVL_REATTACH);
2315 break;
2316
2317 case VBOXUSB_RESET_LEVEL_SOFT:
2318 rc = usb_reset_device(pState->pDip, USB_RESET_LVL_DEFAULT);
2319 break;
2320
2321 default:
2322 rc = USB_SUCCESS;
2323 break;
2324 }
2325
2326 rc = vboxUsbSolarisToVBoxRC(rc);
2327 }
2328
2329 Log((DEVICE_NAME ": vboxUsbSolarisCloseDevice: Returns %d\n", rc));
2330 return rc;
2331}
2332
2333
2334/**
2335 * Aborts pending requests and reset the pipe.
2336 *
2337 * @param pState The USB device instance.
2338 * @param bEndpoint The Endpoint address.
2339 *
2340 * @returns VBox error code.
2341 */
2342LOCAL int vboxUsbSolarisAbortPipe(vboxusb_state_t *pState, uint8_t bEndpoint)
2343{
2344 LogFunc((DEVICE_NAME ": vboxUsbSolarisAbortPipe: pState=%p bEndpoint=%#x\n", pState, bEndpoint));
2345
2346 mutex_enter(&pState->Mtx);
2347 int rc = vboxUsbSolarisDeviceState(pState->DevState);
2348 if (RT_SUCCESS(rc))
2349 {
2350 int iEpIndex = VBOXUSB_GET_EP_INDEX(bEndpoint);
2351 Assert(iEpIndex >= 0 && iEpIndex < RT_ELEMENTS(pState->aEps));
2352 vboxusb_ep_t *pEp = &pState->aEps[iEpIndex];
2353 if (RT_LIKELY(pEp))
2354 {
2355 if (pEp->pPipe)
2356 {
2357 /*
2358 * Aborting requests not supported for the default control pipe.
2359 */
2360 if ((pEp->EpDesc.bEndpointAddress & USB_EP_NUM_MASK) == 0)
2361 {
2362 mutex_exit(&pState->Mtx);
2363 LogRel((DEVICE_NAME ": vboxUsbSolarisAbortPipe: Cannot reset default control pipe\n"));
2364 return VERR_NOT_SUPPORTED;
2365 }
2366
2367 mutex_exit(&pState->Mtx);
2368 usb_pipe_reset(pState->pDip, pEp->pPipe,
2369 USB_FLAGS_SLEEP, /* Synchronous */
2370 NULL, /* Completion callback */
2371 NULL); /* Callback's parameter */
2372
2373 /*
2374 * Allow pending async requests to complete.
2375 */
2376 /** @todo this is most likely not required. */
2377 rc = usb_pipe_drain_reqs(pState->pDip, pEp->pPipe,
2378 USB_FLAGS_SLEEP, /* Synchronous */
2379 5, /* Timeout (seconds) */
2380 NULL, /* Completion callback */
2381 NULL); /* Callback's parameter */
2382
2383 mutex_enter(&pState->Mtx);
2384
2385 Log((DEVICE_NAME ": vboxUsbSolarisAbortPipe: usb_pipe_drain_reqs returns %d\n", rc));
2386 rc = vboxUsbSolarisToVBoxRC(rc);
2387 }
2388 else
2389 {
2390 LogRel((DEVICE_NAME ": vboxUsbSolarisAbortPipe: pipe not open. bEndpoint=%#x\n", bEndpoint));
2391 rc = VERR_PIPE_IO_ERROR;
2392 }
2393 }
2394 else
2395 {
2396 LogRel((DEVICE_NAME ": vboxUsbSolarisAbortPipe: Invalid pipe bEndpoint=%#x[%d]\n", bEndpoint, iEpIndex));
2397 rc = VERR_INVALID_HANDLE;
2398 }
2399 }
2400
2401 mutex_exit(&pState->Mtx);
2402
2403 LogFunc((DEVICE_NAME ": vboxUsbSolarisAbortPipe: Returns %d\n", rc));
2404 return rc;
2405}
2406
2407
2408/**
2409 * Initializes an Endpoint.
2410 *
2411 * @param pState The USB device instance.
2412 * @param pEpData The Endpoint data (NULL implies the default
2413 * endpoint).
2414 *
2415 * @returns VBox error code.
2416 */
2417LOCAL int vboxUsbSolarisInitEp(vboxusb_state_t *pState, usb_ep_data_t *pEpData)
2418{
2419 LogFunc((DEVICE_NAME ": vboxUsbSolarisInitEp: pState=%p pEpData=%p", pState, pEpData));
2420
2421 /*
2422 * Is this the default endpoint?
2423 */
2424 usb_ep_descr_t *pEpDesc = NULL;
2425 vboxusb_ep_t *pEp = NULL;
2426 int iEpIndex;
2427 if (!pEpData)
2428 {
2429 iEpIndex = 0;
2430 pEpDesc = &g_VBoxUSBSolarisDefaultEpDesc;
2431 }
2432 else
2433 {
2434 iEpIndex = VBOXUSB_GET_EP_INDEX(pEpData->ep_descr.bEndpointAddress);
2435 pEpDesc = (usb_ep_descr_t *)((uint8_t *)pEpData + g_offUsbEpDataDescr);
2436 }
2437
2438 Assert(iEpIndex >= 0 && iEpIndex < RT_ELEMENTS(pState->aEps));
2439 pEp = &pState->aEps[iEpIndex];
2440
2441 /*
2442 * Initialize the endpoint.
2443 */
2444 pEp->EpDesc = *pEpDesc;
2445 if (!pEp->fInitialized)
2446 {
2447 pEp->pPipe = NULL;
2448 bzero(&pEp->PipePolicy, sizeof(pEp->PipePolicy));
2449 pEp->PipePolicy.pp_max_async_reqs = VBOXUSB_MAX_PIPE_ASYNC_REQS;
2450 pEp->fIsocPolling = false;
2451 list_create(&pEp->hIsocInUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
2452 pEp->cIsocInUrbs = 0;
2453 list_create(&pEp->hIsocInLandedReqs, sizeof(vboxusb_isoc_req_t), offsetof(vboxusb_isoc_req_t, hListLink));
2454 pEp->cbIsocInLandedReqs = 0;
2455 pEp->cbMaxIsocData = 0;
2456 pEp->fInitialized = true;
2457 }
2458
2459 Log((DEVICE_NAME ": vboxUsbSolarisInitEp: Success, %s[%2d] %s %s bEndpoint=%#x\n", !pEpData ? "Default " : "Endpoint",
2460 iEpIndex, vboxUsbSolarisEpType(pEp), vboxUsbSolarisEpDir(pEp), pEp->EpDesc.bEndpointAddress));
2461 return VINF_SUCCESS;
2462}
2463
2464
2465/**
2466 * Initializes Endpoints for the current configuration, all interfaces and
2467 * alternate setting 0 for each interface.
2468 *
2469 * @param pState The USB device instance.
2470 *
2471 * @returns VBox status code.
2472 */
2473LOCAL int vboxUsbSolarisInitEpsForCfg(vboxusb_state_t *pState)
2474{
2475 uint_t uCfgIndex = usb_get_current_cfgidx(pState->pDip);
2476 if (uCfgIndex >= pState->pDevDesc->dev_n_cfg)
2477 {
2478 LogRel((DEVICE_NAME ": vboxUsbSolarisInitEpsForCfg: Invalid current config index %u\n", uCfgIndex));
2479 return VERR_OUT_OF_RANGE;
2480 }
2481
2482 usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[uCfgIndex];
2483 uchar_t bConfig = pConfig->cfg_descr.bConfigurationValue;
2484
2485 LogFunc((DEVICE_NAME ": vboxUsbSolarisInitEpsForCfg: pState=%p bConfig=%u uCfgIndex=%u\n", pState, bConfig, uCfgIndex));
2486
2487 const uint_t cIfs = pConfig->cfg_n_if;
2488 for (uchar_t uIf = 0; uIf < cIfs; uIf++)
2489 {
2490 usb_if_data_t *pIf = &pConfig->cfg_if[uIf];
2491 const uint_t cAlts = pIf->if_n_alt;
2492 for (uchar_t uAlt = 0; uAlt < cAlts; uAlt++)
2493 {
2494 usb_alt_if_data_t *pAlt = &pIf->if_alt[uAlt];
2495 if (pAlt->altif_descr.bAlternateSetting == 0) /* Refer USB 2.0 spec 9.6.5 "Interface" */
2496 {
2497 const uint_t cEps = pAlt->altif_n_ep;
2498 for (uchar_t uEp = 0; uEp < cEps; uEp++)
2499 {
2500 uint8_t *pbEpData = (uint8_t *)&pAlt->altif_ep[0];
2501 usb_ep_data_t *pEpData = (usb_ep_data_t *)(pbEpData + uEp * g_cbUsbEpData);
2502 int rc = vboxUsbSolarisInitEp(pState, pEpData);
2503 if (RT_FAILURE(rc))
2504 {
2505 LogRel((DEVICE_NAME ": vboxUsbSolarisInitEpsForCfg: Failed to init endpoint! "
2506 "bConfig=%u bIf=%#x bAlt=%#x\n", bConfig, pAlt->altif_descr.bInterfaceNumber,
2507 pAlt->altif_descr.bAlternateSetting));
2508 return rc;
2509 }
2510 }
2511 break; /* move on to next interface. */
2512 }
2513 }
2514 }
2515 return VINF_SUCCESS;
2516}
2517
2518
2519/**
2520 * Initializes Endpoints for the given Interface & Alternate setting.
2521 *
2522 * @param pState The USB device instance.
2523 * @param bIf The Interface.
2524 * @param bAlt The Alterate.
2525 *
2526 * @returns VBox status code.
2527 */
2528LOCAL int vboxUsbSolarisInitEpsForIfAlt(vboxusb_state_t *pState, uint8_t bIf, uint8_t bAlt)
2529{
2530 LogFunc((DEVICE_NAME ": vboxUsbSolarisInitEpsForIfAlt: pState=%p bIf=%d uAlt=%d\n", pState, bIf, bAlt));
2531
2532 /* Doesn't hurt to be paranoid */
2533 uint_t uCfgIndex = usb_get_current_cfgidx(pState->pDip);
2534 if (uCfgIndex >= pState->pDevDesc->dev_n_cfg)
2535 {
2536 LogRel((DEVICE_NAME ": vboxUsbSolarisInitEpsForIfAlt: Invalid current config index %d\n", uCfgIndex));
2537 return VERR_OUT_OF_RANGE;
2538 }
2539
2540 usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[uCfgIndex];
2541 for (uchar_t uIf = 0; uIf < pConfig->cfg_n_if; uIf++)
2542 {
2543 usb_if_data_t *pInterface = &pConfig->cfg_if[uIf];
2544 const uint_t cAlts = pInterface->if_n_alt;
2545 for (uchar_t uAlt = 0; uAlt < cAlts; uAlt++)
2546 {
2547 usb_alt_if_data_t *pAlt = &pInterface->if_alt[uAlt];
2548 if ( pAlt->altif_descr.bInterfaceNumber == bIf
2549 && pAlt->altif_descr.bAlternateSetting == bAlt)
2550 {
2551 const uint_t cEps = pAlt->altif_n_ep;
2552 for (uchar_t uEp = 0; uEp < cEps; uEp++)
2553 {
2554 uint8_t *pbEpData = (uint8_t *)&pAlt->altif_ep[0];
2555 usb_ep_data_t *pEpData = (usb_ep_data_t *)(pbEpData + uEp * g_cbUsbEpData);
2556 int rc = vboxUsbSolarisInitEp(pState, pEpData);
2557 if (RT_FAILURE(rc))
2558 {
2559 uint8_t bCfgValue = pConfig->cfg_descr.bConfigurationValue;
2560 LogRel((DEVICE_NAME ": vboxUsbSolarisInitEpsForIfAlt: Failed to init endpoint! "
2561 "bCfgValue=%u bIf=%#x bAlt=%#x\n", bCfgValue, bIf, bAlt));
2562 return rc;
2563 }
2564 }
2565 return VINF_SUCCESS;
2566 }
2567 }
2568 }
2569 return VERR_NOT_FOUND;
2570}
2571
2572
2573/**
2574 * Destroys all Endpoints.
2575 *
2576 * @param pState The USB device instance.
2577 *
2578 * @remarks Requires the state mutex to be held.
2579 * Call only from Detach() or similar as callbacks
2580 */
2581LOCAL void vboxUsbSolarisDestroyAllEps(vboxusb_state_t *pState)
2582{
2583 LogFunc((DEVICE_NAME ": vboxUsbSolarisDestroyAllEps: pState=%p\n", pState));
2584
2585 Assert(mutex_owned(&pState->Mtx));
2586 for (unsigned i = 0; i < VBOXUSB_MAX_ENDPOINTS; i++)
2587 {
2588 vboxusb_ep_t *pEp = &pState->aEps[i];
2589 if (pEp->fInitialized)
2590 vboxUsbSolarisDestroyEp(pState, pEp);
2591 }
2592}
2593
2594
2595/**
2596 * Destroys an Endpoint.
2597 *
2598 * @param pState The USB device instance.
2599 * @param pEp The Endpoint.
2600 *
2601 * @remarks Requires the state mutex to be held.
2602 */
2603LOCAL void vboxUsbSolarisDestroyEp(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2604{
2605 LogFunc((DEVICE_NAME ": vboxUsbSolarisDestroyEp: pState=%p pEp=%p\n", pState, pEp));
2606
2607 Assert(pEp->fInitialized);
2608 Assert(mutex_owned(&pState->Mtx));
2609 vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
2610 while (pUrb)
2611 {
2612 if (pUrb->pMsg)
2613 freemsg(pUrb->pMsg);
2614 RTMemFree(pUrb);
2615 pUrb = list_remove_head(&pEp->hIsocInUrbs);
2616 }
2617 pEp->cIsocInUrbs = 0;
2618 list_destroy(&pEp->hIsocInUrbs);
2619
2620 vboxusb_isoc_req_t *pIsocReq = list_remove_head(&pEp->hIsocInLandedReqs);
2621 while (pIsocReq)
2622 {
2623 kmem_free(pIsocReq, sizeof(vboxusb_isoc_req_t));
2624 pIsocReq = list_remove_head(&pEp->hIsocInLandedReqs);
2625 }
2626 pEp->cbIsocInLandedReqs = 0;
2627 list_destroy(&pEp->hIsocInLandedReqs);
2628
2629 pEp->fInitialized = false;
2630}
2631
2632
2633/**
2634 * Closes all non-default pipes and drains the default pipe.
2635 *
2636 * @param pState The USB device instance.
2637 * @param fDefault Whether to close the default control pipe.
2638 *
2639 * @remarks Requires the device state mutex to be held.
2640 */
2641LOCAL void vboxUsbSolarisCloseAllPipes(vboxusb_state_t *pState, bool fDefault)
2642{
2643 LogFunc((DEVICE_NAME ": vboxUsbSolarisCloseAllPipes: pState=%p\n", pState));
2644
2645 for (int i = 1; i < VBOXUSB_MAX_ENDPOINTS; i++)
2646 {
2647 vboxusb_ep_t *pEp = &pState->aEps[i];
2648 if ( pEp
2649 && pEp->pPipe)
2650 {
2651 Log((DEVICE_NAME ": vboxUsbSolarisCloseAllPipes: Closing[%d]\n", i));
2652 vboxUsbSolarisClosePipe(pState, pEp);
2653 }
2654 }
2655
2656 if (fDefault)
2657 {
2658 vboxusb_ep_t *pEp = &pState->aEps[0];
2659 if ( pEp
2660 && pEp->pPipe)
2661 {
2662 vboxUsbSolarisClosePipe(pState, pEp);
2663 Log((DEVICE_NAME ": vboxUsbSolarisCloseAllPipes: Closed default pipe\n"));
2664 }
2665 }
2666}
2667
2668
2669/**
2670 * Opens the pipe associated with an Endpoint.
2671 *
2672 * @param pState The USB device instance.
2673 * @param pEp The Endpoint.
2674 * @remarks Requires the device state mutex to be held.
2675 *
2676 * @returns VBox status code.
2677 */
2678LOCAL int vboxUsbSolarisOpenPipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2679{
2680 Assert(mutex_owned(&pState->Mtx));
2681
2682 /*
2683 * Make sure the Endpoint isn't open already.
2684 */
2685 if (pEp->pPipe)
2686 return VINF_SUCCESS;
2687
2688 /*
2689 * Default Endpoint; already opened just copy the pipe handle.
2690 */
2691 if ((pEp->EpDesc.bEndpointAddress & USB_EP_NUM_MASK) == 0)
2692 {
2693 pEp->pPipe = pState->pDevDesc->dev_default_ph;
2694 Log((DEVICE_NAME ": vboxUsbSolarisOpenPipe: Default pipe opened\n"));
2695 return VINF_SUCCESS;
2696 }
2697
2698 /*
2699 * Open the non-default pipe for the Endpoint.
2700 */
2701 mutex_exit(&pState->Mtx);
2702 int rc = usb_pipe_open(pState->pDip, &pEp->EpDesc, &pEp->PipePolicy, USB_FLAGS_NOSLEEP, &pEp->pPipe);
2703 mutex_enter(&pState->Mtx);
2704 if (rc == USB_SUCCESS)
2705 {
2706 LogFunc((DEVICE_NAME ": vboxUsbSolarisOpenPipe: Opened pipe, pState=%p pEp=%p\n", pState, pEp));
2707 usb_pipe_set_private(pEp->pPipe, (usb_opaque_t)pEp);
2708
2709 /*
2710 * Determine input buffer size for Isoc. IN transfers.
2711 */
2712 if ( VBOXUSB_XFER_TYPE(pEp) == VUSBXFERTYPE_ISOC
2713 && VBOXUSB_XFER_DIR(pEp) == VUSB_DIR_TO_HOST)
2714 {
2715 /*
2716 * wMaxPacketSize bits 10..0 specifies maximum packet size which can hold 1024 bytes.
2717 * If bits 12..11 is non-zero, cbMax will be more than 1024 and thus the Endpoint is a
2718 * high-bandwidth Endpoint.
2719 */
2720 uint16_t cbMax = VBOXUSB_PKT_SIZE(pEp->EpDesc.wMaxPacketSize);
2721 if (cbMax <= 1024)
2722 {
2723 /* Buffer 1 second for highspeed and 8 seconds for fullspeed Endpoints. */
2724 pEp->cbMaxIsocData = 1000 * cbMax * 8;
2725 }
2726 else
2727 {
2728 /* Buffer about 400 milliseconds of data for highspeed high-bandwidth endpoints. */
2729 pEp->cbMaxIsocData = 400 * cbMax * 8;
2730 }
2731 Log((DEVICE_NAME ": vboxUsbSolarisOpenPipe: bEndpoint=%#x cbMaxIsocData=%u\n", pEp->EpDesc.bEndpointAddress,
2732 pEp->cbMaxIsocData));
2733 }
2734
2735 rc = VINF_SUCCESS;
2736 }
2737 else
2738 {
2739 LogRel((DEVICE_NAME ": vboxUsbSolarisOpenPipe: Failed! rc=%d pState=%p pEp=%p\n", rc, pState, pEp));
2740 rc = VERR_BAD_PIPE;
2741 }
2742
2743 return rc;
2744}
2745
2746
2747/**
2748 * Closes the pipe associated with an Endpoint.
2749 *
2750 * @param pState The USB device instance.
2751 * @param pEp The Endpoint.
2752 *
2753 * @remarks Requires the device state mutex to be held.
2754 */
2755LOCAL void vboxUsbSolarisClosePipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2756{
2757 LogFunc((DEVICE_NAME ": vboxUsbSolarisClosePipe: pState=%p pEp=%p\n", pState, pEp));
2758 AssertPtr(pEp);
2759
2760 if (pEp->pPipe)
2761 {
2762 /*
2763 * Default pipe: allow completion of pending requests.
2764 */
2765 if (pEp->pPipe == pState->pDevDesc->dev_default_ph)
2766 {
2767 mutex_exit(&pState->Mtx);
2768 usb_pipe_drain_reqs(pState->pDip, pEp->pPipe, 0, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback arg. */);
2769 mutex_enter(&pState->Mtx);
2770 Log((DEVICE_NAME ": vboxUsbSolarisClosePipe: Closed default pipe\n"));
2771 pState->fDefaultPipeOpen = false;
2772 }
2773 else
2774 {
2775 /*
2776 * Stop Isoc. IN polling if required.
2777 */
2778 if (pEp->fIsocPolling)
2779 {
2780 pEp->fIsocPolling = false;
2781 mutex_exit(&pState->Mtx);
2782 usb_pipe_stop_isoc_polling(pEp->pPipe, USB_FLAGS_NOSLEEP);
2783 mutex_enter(&pState->Mtx);
2784 }
2785
2786 /*
2787 * Non-default pipe: close it.
2788 */
2789 Log((DEVICE_NAME ": vboxUsbSolarisClosePipe: Pipe bmAttributes=%#x bEndpoint=%#x\n", pEp->EpDesc.bmAttributes,
2790 pEp->EpDesc.bEndpointAddress));
2791 mutex_exit(&pState->Mtx);
2792 usb_pipe_close(pState->pDip, pEp->pPipe, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback arg. */);
2793 mutex_enter(&pState->Mtx);
2794 }
2795
2796 /*
2797 * Free the Endpoint data message block and reset pipe handle.
2798 */
2799 pEp->pPipe = NULL;
2800
2801 Log((DEVICE_NAME ": vboxUsbSolarisClosePipe: Success, bEndpoint=%#x\n", pEp->EpDesc.bEndpointAddress));
2802 }
2803
2804 Assert(pEp->pPipe == NULL);
2805}
2806
2807
2808/**
2809 * Finds the Configuration index for the passed in Configuration value.
2810 *
2811 * @param pState The USB device instance.
2812 * @param bConfig The Configuration.
2813 *
2814 * @returns The configuration index if found, otherwise -1.
2815 */
2816LOCAL int vboxUsbSolarisGetConfigIndex(vboxusb_state_t *pState, uint_t bConfig)
2817{
2818 for (int CfgIndex = 0; CfgIndex < pState->pDevDesc->dev_n_cfg; CfgIndex++)
2819 {
2820 usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[CfgIndex];
2821 if (pConfig->cfg_descr.bConfigurationValue == bConfig)
2822 return CfgIndex;
2823 }
2824
2825 return -1;
2826}
2827
2828
2829/**
2830 * Allocates and initializes an Isoc. In URB from the ring-3 equivalent.
2831 *
2832 * @param pState The USB device instance.
2833 * @param pUrbReq Opaque pointer to the complete request.
2834 *
2835 * @returns The allocated Isoc. In URB to be used.
2836 */
2837LOCAL vboxusb_urb_t *vboxUsbSolarisGetIsocInUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq)
2838{
2839 /*
2840 * Isoc. In URBs are not queued into the Inflight list like every other URBs.
2841 * For now we allocate each URB which gets queued into the respective Endpoint during Xfer.
2842 */
2843 vboxusb_urb_t *pUrb = RTMemAllocZ(sizeof(vboxusb_urb_t));
2844 if (RT_LIKELY(pUrb))
2845 {
2846 pUrb->enmState = VBOXUSB_URB_STATE_INFLIGHT;
2847 pUrb->pState = pState;
2848
2849 if (RT_LIKELY(pUrbReq))
2850 {
2851 pUrb->pvUrbR3 = pUrbReq->pvUrbR3;
2852 pUrb->bEndpoint = pUrbReq->bEndpoint;
2853 pUrb->enmType = pUrbReq->enmType;
2854 pUrb->enmDir = pUrbReq->enmDir;
2855 pUrb->enmStatus = pUrbReq->enmStatus;
2856 pUrb->cbDataR3 = pUrbReq->cbData;
2857 pUrb->pvDataR3 = (RTR3PTR)pUrbReq->pvData;
2858 pUrb->cIsocPkts = pUrbReq->cIsocPkts;
2859
2860 for (unsigned i = 0; i < pUrbReq->cIsocPkts; i++)
2861 pUrb->aIsocPkts[i].cbPkt = pUrbReq->aIsocPkts[i].cbPkt;
2862
2863 pUrb->pMsg = NULL;
2864 }
2865 }
2866 else
2867 LogRel((DEVICE_NAME ": vboxUsbSolarisGetIsocInUrb: Failed to alloc %d bytes\n", sizeof(vboxusb_urb_t)));
2868 return pUrb;
2869}
2870
2871
2872/**
2873 * Queues a URB reusing previously allocated URBs as required.
2874 *
2875 * @param pState The USB device instance.
2876 * @param pUrbReq Opaque pointer to the complete request.
2877 * @param pMsg Pointer to the allocated request data.
2878 *
2879 * @returns The allocated URB to be used, or NULL upon failure.
2880 */
2881LOCAL vboxusb_urb_t *vboxUsbSolarisQueueUrb(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, mblk_t *pMsg)
2882{
2883 Assert(pUrbReq);
2884 LogFunc((DEVICE_NAME ": vboxUsbSolarisQueueUrb: pState=%p pUrbReq=%p\n", pState, pUrbReq));
2885
2886 mutex_enter(&pState->Mtx);
2887
2888 /*
2889 * Grab a URB from the free list.
2890 */
2891 vboxusb_urb_t *pUrb = list_remove_head(&pState->hFreeUrbs);
2892 if (pUrb)
2893 {
2894 Assert(pUrb->enmState == VBOXUSB_URB_STATE_FREE);
2895 Assert(!pUrb->pMsg);
2896 Assert(pState->cFreeUrbs > 0);
2897 --pState->cFreeUrbs;
2898 }
2899 else
2900 {
2901 /*
2902 * We can't discard "old" URBs. For instance, INTR IN URBs that don't complete as
2903 * they don't have a timeout can essentially take arbitrarily long to complete depending
2904 * on the device and it's not safe to discard them in case they -do- complete. However,
2905 * we also have to reasonably assume a device doesn't have too many pending URBs always.
2906 *
2907 * Thus we just use a large queue and simply refuse further transfers. This is not
2908 * a situation which normally ever happens as usually there are at most than 4 or 5 URBs
2909 * in-flight until we reap them.
2910 */
2911 uint32_t const cTotalUrbs = pState->cInflightUrbs + pState->cFreeUrbs + pState->cLandedUrbs;
2912 if (cTotalUrbs >= VBOXUSB_URB_QUEUE_SIZE)
2913 {
2914 mutex_exit(&pState->Mtx);
2915 LogRelMax(5, (DEVICE_NAME ": vboxUsbSolarisQueueUrb: Max queue size %u reached, refusing further transfers",
2916 cTotalUrbs));
2917 return NULL;
2918 }
2919
2920 /*
2921 * Allocate a new URB as we have no free URBs.
2922 */
2923 mutex_exit(&pState->Mtx);
2924 pUrb = RTMemAllocZ(sizeof(vboxusb_urb_t));
2925 if (RT_UNLIKELY(!pUrb))
2926 {
2927 LogRel((DEVICE_NAME ": vboxUsbSolarisQueueUrb: Failed to alloc %d bytes\n", sizeof(vboxusb_urb_t)));
2928 return NULL;
2929 }
2930 mutex_enter(&pState->Mtx);
2931 }
2932
2933 /*
2934 * Add the URB to the inflight list.
2935 */
2936 list_insert_tail(&pState->hInflightUrbs, pUrb);
2937 ++pState->cInflightUrbs;
2938
2939 Assert(!pUrb->pMsg);
2940 pUrb->pMsg = pMsg;
2941 pUrb->pState = pState;
2942 pUrb->enmState = VBOXUSB_URB_STATE_INFLIGHT;
2943 pUrb->pvUrbR3 = pUrbReq->pvUrbR3;
2944 pUrb->bEndpoint = pUrbReq->bEndpoint;
2945 pUrb->enmType = pUrbReq->enmType;
2946 pUrb->enmDir = pUrbReq->enmDir;
2947 pUrb->enmStatus = pUrbReq->enmStatus;
2948 pUrb->fShortOk = pUrbReq->fShortOk;
2949 pUrb->pvDataR3 = (RTR3PTR)pUrbReq->pvData;
2950 pUrb->cbDataR3 = pUrbReq->cbData;
2951 pUrb->cIsocPkts = pUrbReq->cIsocPkts;
2952 if (pUrbReq->enmType == VUSBXFERTYPE_ISOC)
2953 {
2954 for (unsigned i = 0; i < pUrbReq->cIsocPkts; i++)
2955 pUrb->aIsocPkts[i].cbPkt = pUrbReq->aIsocPkts[i].cbPkt;
2956 }
2957
2958 mutex_exit(&pState->Mtx);
2959 return pUrb;
2960}
2961
2962
2963/**
2964 * Dequeues a completed URB into the landed list and informs user-land.
2965 *
2966 * @param pUrb The URB to move.
2967 * @param URBStatus The Solaris URB completion code.
2968 *
2969 * @remarks All pipes could be closed at this point (e.g. Device disconnected during inflight URBs)
2970 */
2971LOCAL void vboxUsbSolarisDeQueueUrb(vboxusb_urb_t *pUrb, int URBStatus)
2972{
2973 LogFunc((DEVICE_NAME ": vboxUsbSolarisDeQueue: pUrb=%p\n", pUrb));
2974 AssertPtrReturnVoid(pUrb);
2975
2976 pUrb->enmStatus = vboxUsbSolarisGetUrbStatus(URBStatus);
2977 if (pUrb->enmStatus != VUSBSTATUS_OK)
2978 Log((DEVICE_NAME ": vboxUsbSolarisDeQueueUrb: URB failed! URBStatus=%d bEndpoint=%#x\n", URBStatus, pUrb->bEndpoint));
2979
2980 vboxusb_state_t *pState = pUrb->pState;
2981 if (RT_LIKELY(pState))
2982 {
2983 mutex_enter(&pState->Mtx);
2984 pUrb->enmState = VBOXUSB_URB_STATE_LANDED;
2985
2986 /*
2987 * Remove it from the inflight list & move it to the landed list.
2988 */
2989 list_remove(&pState->hInflightUrbs, pUrb);
2990 Assert(pState->cInflightUrbs > 0);
2991 --pState->cInflightUrbs;
2992
2993 list_insert_tail(&pState->hLandedUrbs, pUrb);
2994 ++pState->cLandedUrbs;
2995
2996 vboxUsbSolarisNotifyComplete(pUrb->pState);
2997 mutex_exit(&pState->Mtx);
2998 return;
2999 }
3000
3001 /* Well, let's at least not leak memory... */
3002 freemsg(pUrb->pMsg);
3003 pUrb->pMsg = NULL;
3004 pUrb->enmStatus = VUSBSTATUS_INVALID;
3005
3006 LogRel((DEVICE_NAME ": vboxUsbSolarisDeQueue: State Gone\n"));
3007}
3008
3009
3010/**
3011 * Concatenates a chain message block into a single message block if possible.
3012 *
3013 * @param pUrb The URB to move.
3014 */
3015LOCAL void vboxUsbSolarisConcatMsg(vboxusb_urb_t *pUrb)
3016{
3017 /*
3018 * Concatenate the whole message rather than doing a chained copy while reaping.
3019 */
3020 if ( pUrb->pMsg
3021 && pUrb->pMsg->b_cont)
3022 {
3023 mblk_t *pFullMsg = msgpullup(pUrb->pMsg, -1 /* all data */);
3024 if (RT_LIKELY(pFullMsg))
3025 {
3026 freemsg(pUrb->pMsg);
3027 pUrb->pMsg = pFullMsg;
3028 }
3029 else
3030 LogRel((DEVICE_NAME ": vboxUsbSolarisConcatMsg: Failed. Expect glitches due to truncated data!\n"));
3031 }
3032}
3033
3034
3035/**
3036 * Wakes up a user process signalling URB completion.
3037 *
3038 * @param pState The USB device instance.
3039 * @remarks Requires the device state mutex to be held.
3040 */
3041LOCAL void vboxUsbSolarisNotifyComplete(vboxusb_state_t *pState)
3042{
3043 if (pState->fPollPending)
3044 {
3045 pollhead_t *pPollHead = &pState->PollHead;
3046 pState->fPollPending = false;
3047 mutex_exit(&pState->Mtx);
3048 pollwakeup(pPollHead, POLLIN);
3049 mutex_enter(&pState->Mtx);
3050 }
3051}
3052
3053
3054/**
3055 * Wakes up a user process signalling a device unplug events.
3056 *
3057 * @param pState The USB device instance.
3058 * @remarks Requires the device state mutex to be held.
3059 */
3060LOCAL void vboxUsbSolarisNotifyUnplug(vboxusb_state_t *pState)
3061{
3062 if (pState->fPollPending)
3063 {
3064 pollhead_t *pPollHead = &pState->PollHead;
3065 pState->fPollPending = false;
3066 mutex_exit(&pState->Mtx);
3067 pollwakeup(pPollHead, POLLHUP);
3068 mutex_enter(&pState->Mtx);
3069 }
3070}
3071
3072
3073/**
3074 * Performs a Control Xfer.
3075 *
3076 * @param pState The USB device instance.
3077 * @param pEp The Endpoint for the Xfer.
3078 * @param pUrb The VBox USB URB.
3079 *
3080 * @returns VBox status code.
3081 */
3082LOCAL int vboxUsbSolarisCtrlXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3083{
3084 LogFunc((DEVICE_NAME ": vboxUsbSolarisCtrlXfer: pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb,
3085 pUrb->enmDir, pUrb->cbDataR3));
3086
3087 AssertPtrReturn(pUrb->pMsg, VERR_INVALID_PARAMETER);
3088 const size_t cbData = pUrb->cbDataR3 > VBOXUSB_CTRL_XFER_SIZE ? pUrb->cbDataR3 - VBOXUSB_CTRL_XFER_SIZE : 0;
3089
3090 /*
3091 * Allocate a wrapper request.
3092 */
3093 usb_ctrl_req_t *pReq = usb_alloc_ctrl_req(pState->pDip, cbData, USB_FLAGS_SLEEP);
3094 if (RT_LIKELY(pReq))
3095 {
3096 uchar_t *pSetupData = pUrb->pMsg->b_rptr;
3097
3098 /*
3099 * Initialize the Ctrl Xfer Header.
3100 */
3101 pReq->ctrl_bmRequestType = pSetupData[0];
3102 pReq->ctrl_bRequest = pSetupData[1];
3103 pReq->ctrl_wValue = (pSetupData[3] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[2];
3104 pReq->ctrl_wIndex = (pSetupData[5] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[4];
3105 pReq->ctrl_wLength = (pSetupData[7] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[6];
3106
3107 if ( pUrb->enmDir == VUSBDIRECTION_OUT
3108 && cbData)
3109 {
3110 bcopy(pSetupData + VBOXUSB_CTRL_XFER_SIZE, pReq->ctrl_data->b_wptr, cbData);
3111 pReq->ctrl_data->b_wptr += cbData;
3112 }
3113
3114 freemsg(pUrb->pMsg);
3115 pUrb->pMsg = NULL;
3116
3117 /*
3118 * Initialize callbacks and timeouts.
3119 */
3120 pReq->ctrl_cb = vboxUsbSolarisCtrlXferCompleted;
3121 pReq->ctrl_exc_cb = vboxUsbSolarisCtrlXferCompleted;
3122 pReq->ctrl_timeout = VBOXUSB_CTRL_XFER_TIMEOUT;
3123 pReq->ctrl_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_SHORT_XFER_OK;
3124 pReq->ctrl_client_private = (usb_opaque_t)pUrb;
3125
3126 /*
3127 * Submit the request.
3128 */
3129 int rc = usb_pipe_ctrl_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3130 if (RT_LIKELY(rc == USB_SUCCESS))
3131 return VINF_SUCCESS;
3132
3133 LogRel((DEVICE_NAME ": vboxUsbSolarisCtrlXfer: Request failed! bEndpoint=%#x rc=%d\n", pUrb->bEndpoint, rc));
3134
3135 usb_free_ctrl_req(pReq);
3136 return VERR_PIPE_IO_ERROR;
3137 }
3138
3139 LogRel((DEVICE_NAME ": vboxUsbSolarisCtrlXfer: Failed to alloc request for %u bytes\n", cbData));
3140 return VERR_NO_MEMORY;
3141}
3142
3143
3144/**
3145 * Completion/Exception callback for Control Xfers.
3146 *
3147 * @param pPipe The Ctrl pipe handle.
3148 * @param pReq The Ctrl request.
3149 */
3150LOCAL void vboxUsbSolarisCtrlXferCompleted(usb_pipe_handle_t pPipe, usb_ctrl_req_t *pReq)
3151{
3152 LogFunc((DEVICE_NAME ": vboxUsbSolarisCtrlXferCompleted: pPipe=%p pReq=%p\n", pPipe, pReq));
3153 Assert(pReq);
3154 Assert(!(pReq->ctrl_cb_flags & USB_CB_INTR_CONTEXT));
3155
3156 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->ctrl_client_private;
3157 if (RT_LIKELY(pUrb))
3158 {
3159 /*
3160 * Funky stuff: We need to reconstruct the header for control transfers.
3161 * Let us chain along the data and concatenate the entire message.
3162 */
3163 mblk_t *pSetupMsg = allocb(sizeof(VUSBSETUP), BPRI_MED);
3164 if (RT_LIKELY(pSetupMsg))
3165 {
3166 VUSBSETUP SetupData;
3167 SetupData.bmRequestType = pReq->ctrl_bmRequestType;
3168 SetupData.bRequest = pReq->ctrl_bRequest;
3169 SetupData.wValue = pReq->ctrl_wValue;
3170 SetupData.wIndex = pReq->ctrl_wIndex;
3171 SetupData.wLength = pReq->ctrl_wLength;
3172
3173 bcopy(&SetupData, pSetupMsg->b_wptr, sizeof(VUSBSETUP));
3174 pSetupMsg->b_wptr += sizeof(VUSBSETUP);
3175
3176 /*
3177 * Should be safe to update pMsg here without the state mutex as typically nobody else
3178 * touches this URB in the inflight list.
3179 *
3180 * The reason we choose to use vboxUsbSolarisConcatMsg here is that we don't assume the
3181 * message returned by Solaris is one contiguous chunk in 'pMsg->b_rptr'.
3182 */
3183 Assert(!pUrb->pMsg);
3184 pUrb->pMsg = pSetupMsg;
3185 pUrb->pMsg->b_cont = pReq->ctrl_data;
3186 pReq->ctrl_data = NULL;
3187 vboxUsbSolarisConcatMsg(pUrb);
3188 }
3189 else
3190 LogRel((DEVICE_NAME ": vboxUsbSolarisCtrlXferCompleted: Failed to alloc %u bytes for header\n", sizeof(VUSBSETUP)));
3191
3192 /*
3193 * Update the URB and move to landed list for reaping.
3194 */
3195 vboxUsbSolarisDeQueueUrb(pUrb, pReq->ctrl_completion_reason);
3196 }
3197 else
3198 LogRel((DEVICE_NAME ": vboxUsbSolarisCtrlXferCompleted: Extreme error! missing private data\n"));
3199
3200 usb_free_ctrl_req(pReq);
3201}
3202
3203
3204/**
3205 * Performs a Bulk Xfer.
3206 *
3207 * @param pState The USB device instance.
3208 * @param pEp The Endpoint for the Xfer.
3209 * @param pUrb The VBox USB URB.
3210 *
3211 * @returns VBox status code.
3212 * @remarks Any errors, the caller should free pUrb->pMsg.
3213 */
3214LOCAL int vboxUsbSolarisBulkXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3215{
3216 LogFunc((DEVICE_NAME ": vboxUsbSolarisBulkXfer: pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb,
3217 pUrb->enmDir, pUrb->cbDataR3));
3218
3219 /*
3220 * Allocate a wrapper request.
3221 */
3222 size_t const cbAlloc = pUrb->enmDir == VUSBDIRECTION_IN ? pUrb->cbDataR3 : 0;
3223 usb_bulk_req_t *pReq = usb_alloc_bulk_req(pState->pDip, cbAlloc, USB_FLAGS_SLEEP);
3224 if (RT_LIKELY(pReq))
3225 {
3226 /*
3227 * Initialize Bulk Xfer, callbacks and timeouts.
3228 */
3229 usb_req_attrs_t fAttributes = USB_ATTRS_AUTOCLEARING;
3230 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3231 {
3232 pReq->bulk_data = pUrb->pMsg;
3233 pUrb->pMsg = NULL;
3234 }
3235 else if ( pUrb->enmDir == VUSBDIRECTION_IN
3236 && pUrb->fShortOk)
3237 {
3238 fAttributes |= USB_ATTRS_SHORT_XFER_OK;
3239 }
3240
3241 Assert(!pUrb->pMsg);
3242 pReq->bulk_len = pUrb->cbDataR3;
3243 pReq->bulk_cb = vboxUsbSolarisBulkXferCompleted;
3244 pReq->bulk_exc_cb = vboxUsbSolarisBulkXferCompleted;
3245 pReq->bulk_timeout = 0;
3246 pReq->bulk_attributes = fAttributes;
3247 pReq->bulk_client_private = (usb_opaque_t)pUrb;
3248
3249 /* Don't obtain state lock here, we're just reading unchanging data... */
3250 if (RT_UNLIKELY(pUrb->cbDataR3 > pState->cbMaxBulkXfer))
3251 {
3252 LogRel((DEVICE_NAME ": vboxUsbSolarisBulkXfer: Requesting %d bytes when only %d bytes supported by device\n",
3253 pUrb->cbDataR3, pState->cbMaxBulkXfer));
3254 }
3255
3256 /*
3257 * Submit the request.
3258 */
3259 int rc = usb_pipe_bulk_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3260 if (RT_LIKELY(rc == USB_SUCCESS))
3261 return VINF_SUCCESS;
3262
3263 LogRel((DEVICE_NAME ": vboxUsbSolarisBulkXfer: Request failed! Ep=%#x rc=%d cbData=%u\n", pUrb->bEndpoint, rc,
3264 pReq->bulk_len));
3265
3266 usb_free_bulk_req(pReq);
3267 return VERR_PIPE_IO_ERROR;
3268 }
3269
3270 LogRel((DEVICE_NAME ": vboxUsbSolarisBulkXfer: Failed to alloc bulk request\n"));
3271 return VERR_NO_MEMORY;
3272}
3273
3274
3275/**
3276 * Completion/Exception callback for Bulk Xfers.
3277 *
3278 * @param pPipe The Bulk pipe handle.
3279 * @param pReq The Bulk request.
3280 */
3281LOCAL void vboxUsbSolarisBulkXferCompleted(usb_pipe_handle_t pPipe, usb_bulk_req_t *pReq)
3282{
3283 LogFunc((DEVICE_NAME ": vboxUsbSolarisBulkXferCompleted: pPipe=%p pReq=%p\n", pPipe, pReq));
3284
3285 Assert(pReq);
3286 Assert(!(pReq->bulk_cb_flags & USB_CB_INTR_CONTEXT));
3287
3288 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3289 if (RT_LIKELY(pEp))
3290 {
3291 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->bulk_client_private;
3292 if (RT_LIKELY(pUrb))
3293 {
3294 Assert(!pUrb->pMsg);
3295 if ( pUrb->enmDir == VUSBDIRECTION_IN
3296 && pReq->bulk_data)
3297 {
3298 pUrb->pMsg = pReq->bulk_data;
3299 pReq->bulk_data = NULL;
3300 vboxUsbSolarisConcatMsg(pUrb);
3301 }
3302
3303 /*
3304 * Update the URB and move to tail for reaping.
3305 */
3306 vboxUsbSolarisDeQueueUrb(pUrb, pReq->bulk_completion_reason);
3307 }
3308 else
3309 LogRel((DEVICE_NAME ": vboxUsbSolarisBulkXferCompleted: Extreme error! private request data missing!\n"));
3310 }
3311 else
3312 Log((DEVICE_NAME ": vboxUsbSolarisBulkXferCompleted: Pipe Gone!\n"));
3313
3314 usb_free_bulk_req(pReq);
3315}
3316
3317
3318/**
3319 * Performs an Interrupt Xfer.
3320 *
3321 * @param pState The USB device instance.
3322 * @param pEp The Endpoint for the Xfer.
3323 * @param pUrb The VBox USB URB.
3324 *
3325 * @returns VBox status code.
3326 * @remarks Any errors, the caller should free pUrb->pMsg.
3327 */
3328LOCAL int vboxUsbSolarisIntrXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3329{
3330 LogFunc((DEVICE_NAME ": vboxUsbSolarisIntrXfer: pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb,
3331 pUrb->enmDir, pUrb->cbDataR3));
3332
3333 usb_intr_req_t *pReq = usb_alloc_intr_req(pState->pDip, 0 /* length */, USB_FLAGS_SLEEP);
3334 if (RT_LIKELY(pReq))
3335 {
3336 /*
3337 * Initialize Intr Xfer, callbacks & timeouts.
3338 */
3339 usb_req_attrs_t fAttributes = USB_ATTRS_AUTOCLEARING;
3340 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3341 {
3342 pReq->intr_data = pUrb->pMsg;
3343 pUrb->pMsg = NULL;
3344 }
3345 else
3346 {
3347 Assert(pUrb->enmDir == VUSBDIRECTION_IN);
3348 fAttributes |= USB_ATTRS_ONE_XFER;
3349 if (pUrb->fShortOk)
3350 fAttributes |= USB_ATTRS_SHORT_XFER_OK;
3351 }
3352
3353 Assert(!pUrb->pMsg);
3354 pReq->intr_len = pUrb->cbDataR3; /* Not pEp->EpDesc.wMaxPacketSize */
3355 pReq->intr_cb = vboxUsbSolarisIntrXferCompleted;
3356 pReq->intr_exc_cb = vboxUsbSolarisIntrXferCompleted;
3357 pReq->intr_timeout = 0;
3358 pReq->intr_attributes = fAttributes;
3359 pReq->intr_client_private = (usb_opaque_t)pUrb;
3360
3361 /*
3362 * Submit the request.
3363 */
3364 int rc = usb_pipe_intr_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3365 if (RT_LIKELY(rc == USB_SUCCESS))
3366 return VINF_SUCCESS;
3367
3368 LogRel((DEVICE_NAME ": vboxUsbSolarisIntrXfer: usb_pipe_intr_xfer failed! rc=%d bEndpoint=%#x\n", rc, pUrb->bEndpoint));
3369
3370 usb_free_intr_req(pReq);
3371 return VERR_PIPE_IO_ERROR;
3372 }
3373
3374 LogRel((DEVICE_NAME ": vboxUsbSolarisIntrXfer: Failed to alloc intr request\n"));
3375 return VERR_NO_MEMORY;
3376}
3377
3378
3379/**
3380 * Completion/Exception callback for Intr Xfers.
3381 *
3382 * @param pPipe The Intr pipe handle.
3383 * @param pReq The Intr request.
3384 */
3385LOCAL void vboxUsbSolarisIntrXferCompleted(usb_pipe_handle_t pPipe, usb_intr_req_t *pReq)
3386{
3387 LogFunc((DEVICE_NAME ": vboxUsbSolarisIntrXferCompleted: pPipe=%p pReq=%p\n", pPipe, pReq));
3388
3389 Assert(pReq);
3390 Assert(!(pReq->intr_cb_flags & USB_CB_INTR_CONTEXT));
3391
3392 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->intr_client_private;
3393 if (RT_LIKELY(pUrb))
3394 {
3395 if ( pUrb->enmDir == VUSBDIRECTION_IN
3396 && pReq->intr_data)
3397 {
3398 pUrb->pMsg = pReq->intr_data;
3399 pReq->intr_data = NULL;
3400 vboxUsbSolarisConcatMsg(pUrb);
3401 }
3402
3403 /*
3404 * Update the URB and move to landed list for reaping.
3405 */
3406 vboxUsbSolarisDeQueueUrb(pUrb, pReq->intr_completion_reason);
3407 }
3408 else
3409 LogRel((DEVICE_NAME ": vboxUsbSolarisIntrXferCompleted: Extreme error! private request data missing\n"));
3410
3411 usb_free_intr_req(pReq);
3412}
3413
3414
3415/**
3416 * Performs an Isochronous Xfer.
3417 *
3418 * @param pState The USB device instance.
3419 * @param pEp The Endpoint for the Xfer.
3420 * @param pUrb The VBox USB URB.
3421 *
3422 * @returns VBox status code.
3423 * @remarks Any errors, the caller should free pUrb->pMsg.
3424 */
3425LOCAL int vboxUsbSolarisIsocXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3426{
3427 /* LogFunc((DEVICE_NAME ": vboxUsbSolarisIsocXfer: pState=%p pEp=%p pUrb=%p\n", pState, pEp, pUrb)); */
3428
3429 /*
3430 * For Isoc. IN transfers we perform one request and USBA polls the device continuously
3431 * and supplies our Xfer callback with input data. We cannot perform one-shot Isoc. In transfers.
3432 */
3433 size_t cbData = (pUrb->enmDir == VUSBDIRECTION_IN ? pUrb->cIsocPkts * pUrb->aIsocPkts[0].cbPkt : 0);
3434 if (pUrb->enmDir == VUSBDIRECTION_IN)
3435 {
3436 Log((DEVICE_NAME ": vboxUsbSolarisIsocXfer: Isoc. IN - Queueing\n"));
3437
3438 mutex_enter(&pState->Mtx);
3439 if (pEp->fIsocPolling)
3440 {
3441 /*
3442 * Queue a maximum of cbMaxIsocData bytes, else fail.
3443 */
3444 if (pEp->cbIsocInLandedReqs + cbData > pEp->cbMaxIsocData)
3445 {
3446 mutex_exit(&pState->Mtx);
3447 Log((DEVICE_NAME ": vboxUsbSolarisIsocXfer: Max Isoc. data %d bytes queued\n", pEp->cbMaxIsocData));
3448 return VERR_TOO_MUCH_DATA;
3449 }
3450
3451 list_insert_tail(&pEp->hIsocInUrbs, pUrb);
3452 ++pEp->cIsocInUrbs;
3453
3454 mutex_exit(&pState->Mtx);
3455 return VINF_SUCCESS;
3456 }
3457 mutex_exit(&pState->Mtx);
3458 }
3459
3460 int rc = VINF_SUCCESS;
3461 usb_isoc_req_t *pReq = usb_alloc_isoc_req(pState->pDip, pUrb->cIsocPkts, cbData, USB_FLAGS_NOSLEEP);
3462 Log((DEVICE_NAME ": vboxUsbSolarisIsocXfer: enmDir=%#x cIsocPkts=%d aIsocPkts[0]=%d cbDataR3=%d\n", pUrb->enmDir,
3463 pUrb->cIsocPkts, pUrb->aIsocPkts[0].cbPkt, pUrb->cbDataR3));
3464 if (RT_LIKELY(pReq))
3465 {
3466 /*
3467 * Initialize Isoc Xfer, callbacks & timeouts.
3468 */
3469 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
3470 pReq->isoc_pkt_descr[i].isoc_pkt_length = pUrb->aIsocPkts[i].cbPkt;
3471
3472 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3473 {
3474 pReq->isoc_data = pUrb->pMsg;
3475 pReq->isoc_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ISOC_XFER_ASAP;
3476 pReq->isoc_cb = vboxUsbSolarisIsocOutXferCompleted;
3477 pReq->isoc_exc_cb = vboxUsbSolarisIsocOutXferCompleted;
3478 pReq->isoc_client_private = (usb_opaque_t)pUrb;
3479 }
3480 else
3481 {
3482 pReq->isoc_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ISOC_XFER_ASAP | USB_ATTRS_SHORT_XFER_OK;
3483 pReq->isoc_cb = vboxUsbSolarisIsocInXferCompleted;
3484 pReq->isoc_exc_cb = vboxUsbSolarisIsocInXferError;
3485 pReq->isoc_client_private = (usb_opaque_t)pState;
3486 }
3487 pReq->isoc_pkts_count = pUrb->cIsocPkts;
3488 pReq->isoc_pkts_length = 0; /* auto compute */
3489
3490 /*
3491 * Submit the request.
3492 */
3493 rc = usb_pipe_isoc_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3494 if (RT_LIKELY(rc == USB_SUCCESS))
3495 {
3496 if (pUrb->enmDir == VUSBDIRECTION_IN)
3497 {
3498 /*
3499 * Add the first Isoc. IN URB to the queue as well.
3500 */
3501 mutex_enter(&pState->Mtx);
3502 list_insert_tail(&pEp->hIsocInUrbs, pUrb);
3503 ++pEp->cIsocInUrbs;
3504 pEp->fIsocPolling = true;
3505 mutex_exit(&pState->Mtx);
3506 }
3507
3508 return VINF_SUCCESS;
3509 }
3510 else
3511 {
3512 LogRel((DEVICE_NAME ": vboxUsbSolarisIsocXfer: usb_pipe_isoc_xfer failed! rc=%d\n", rc));
3513 rc = VERR_PIPE_IO_ERROR;
3514
3515 if (pUrb->enmDir == VUSBDIRECTION_IN)
3516 {
3517 mutex_enter(&pState->Mtx);
3518 vboxusb_urb_t *pIsocFailedUrb = list_remove_tail(&pEp->hIsocInUrbs);
3519 if (pIsocFailedUrb)
3520 {
3521 RTMemFree(pIsocFailedUrb);
3522 --pEp->cIsocInUrbs;
3523 }
3524 pEp->fIsocPolling = false;
3525 mutex_exit(&pState->Mtx);
3526 }
3527 }
3528
3529 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3530 {
3531 freemsg(pUrb->pMsg);
3532 pUrb->pMsg = NULL;
3533 }
3534
3535 usb_free_isoc_req(pReq);
3536 }
3537 else
3538 {
3539 LogRel((DEVICE_NAME ": vboxUsbSolarisIsocXfer: Failed to alloc isoc req for %d packets\n", pUrb->cIsocPkts));
3540 rc = VERR_NO_MEMORY;
3541 }
3542
3543 return rc;
3544}
3545
3546
3547/**
3548 * Completion/Exception callback for Isoc IN Xfers.
3549 *
3550 * @param pPipe The Intr pipe handle.
3551 * @param pReq The Intr request.
3552 *
3553 * @remarks Completion callback executes in interrupt context!
3554 */
3555LOCAL void vboxUsbSolarisIsocInXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3556{
3557 /* LogFunc((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: pPipe=%p pReq=%p\n", pPipe, pReq)); */
3558
3559 vboxusb_state_t *pState = (vboxusb_state_t *)pReq->isoc_client_private;
3560 if (RT_LIKELY(pState))
3561 {
3562 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3563 if ( pEp
3564 && pEp->pPipe)
3565 {
3566#if 0
3567 /*
3568 * Stop polling if all packets failed.
3569 */
3570 if (pReq->isoc_error_count == pReq->isoc_pkts_count)
3571 {
3572 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: Stopping polling! Too many errors\n"));
3573 mutex_exit(&pState->Mtx);
3574 usb_pipe_stop_isoc_polling(pPipe, USB_FLAGS_NOSLEEP);
3575 mutex_enter(&pState->Mtx);
3576 pEp->fIsocPolling = false;
3577 }
3578#endif
3579
3580 /** @todo Query and verify this at runtime. */
3581 AssertCompile(sizeof(VUSBISOC_PKT_DESC) == sizeof(usb_isoc_pkt_descr_t));
3582 if (RT_LIKELY(pReq->isoc_data))
3583 {
3584 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: cIsocInUrbs=%d cbIsocInLandedReqs=%d\n", pEp->cIsocInUrbs,
3585 pEp->cbIsocInLandedReqs));
3586
3587 mutex_enter(&pState->Mtx);
3588
3589 /*
3590 * If there are waiting URBs, satisfy the oldest one.
3591 */
3592 if ( pEp->cIsocInUrbs > 0
3593 && pEp->cbIsocInLandedReqs == 0)
3594 {
3595 vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
3596 if (RT_LIKELY(pUrb))
3597 {
3598 --pEp->cIsocInUrbs;
3599 mutex_exit(&pState->Mtx);
3600
3601 for (unsigned i = 0; i < pReq->isoc_pkts_count; i++)
3602 {
3603 pUrb->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3604 pUrb->aIsocPkts[i].enmStatus = vboxUsbSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3605 }
3606
3607 pUrb->pMsg = pReq->isoc_data;
3608 pReq->isoc_data = NULL;
3609
3610 /*
3611 * Move to landed list
3612 */
3613 mutex_enter(&pState->Mtx);
3614 list_insert_tail(&pState->hLandedUrbs, pUrb);
3615 ++pState->cLandedUrbs;
3616 vboxUsbSolarisNotifyComplete(pState);
3617 }
3618 else
3619 {
3620 /* Huh!? cIsocInUrbs is wrong then! Should never happen unless we decide to decrement cIsocInUrbs in
3621 Reap time */
3622 pEp->cIsocInUrbs = 0;
3623 LogRel((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: Extreme error! Isoc. counter borked!\n"));
3624 }
3625
3626 mutex_exit(&pState->Mtx);
3627 usb_free_isoc_req(pReq);
3628 return;
3629 }
3630
3631 mutex_exit(&pState->Mtx);
3632 }
3633 else
3634 LogRel((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: Data missing\n"));
3635 }
3636 else
3637 LogRel((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: Pipe Gone\n"));
3638 }
3639 else
3640 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferCompleted: State Gone\n"));
3641
3642 usb_free_isoc_req(pReq);
3643}
3644
3645
3646/**
3647 * Exception callback for Isoc IN Xfers.
3648 *
3649 * @param pPipe The Intr pipe handle.
3650 * @param pReq The Intr request.
3651 * @remarks Completion callback executes in interrupt context!
3652 */
3653LOCAL void vboxUsbSolarisIsocInXferError(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3654{
3655 LogFunc((DEVICE_NAME ": vboxUsbSolarisIsocInXferError: pPipe=%p pReq=%p\n", pPipe, pReq));
3656
3657 vboxusb_state_t *pState = (vboxusb_state_t *)pReq->isoc_client_private;
3658 if (RT_UNLIKELY(!pState))
3659 {
3660 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferError: State Gone\n"));
3661 usb_free_isoc_req(pReq);
3662 return;
3663 }
3664
3665 mutex_enter(&pState->Mtx);
3666 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3667 if (RT_UNLIKELY(!pEp))
3668 {
3669 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferError: Pipe Gone\n"));
3670 mutex_exit(&pState->Mtx);
3671 usb_free_isoc_req(pReq);
3672 return;
3673 }
3674
3675 switch(pReq->isoc_completion_reason)
3676 {
3677 case USB_CR_NO_RESOURCES:
3678 {
3679 /*
3680 * Resubmit the request in case the original request did not complete due to
3681 * immediately unavailable requests
3682 */
3683 mutex_exit(&pState->Mtx);
3684 usb_pipe_isoc_xfer(pPipe, pReq, USB_FLAGS_NOSLEEP);
3685 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferError: Resubmitted Isoc. IN request due to unavailable resources\n"));
3686 return;
3687 }
3688
3689 case USB_CR_PIPE_CLOSING:
3690 case USB_CR_STOPPED_POLLING:
3691 case USB_CR_PIPE_RESET:
3692 {
3693 pEp->fIsocPolling = false;
3694 usb_free_isoc_req(pReq);
3695 break;
3696 }
3697
3698 default:
3699 {
3700 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferError: Stopping Isoc. IN polling due to rc=%d\n",
3701 pReq->isoc_completion_reason));
3702 pEp->fIsocPolling = false;
3703 mutex_exit(&pState->Mtx);
3704 usb_pipe_stop_isoc_polling(pPipe, USB_FLAGS_NOSLEEP);
3705 usb_free_isoc_req(pReq);
3706 mutex_enter(&pState->Mtx);
3707 break;
3708 }
3709 }
3710
3711 /*
3712 * Dequeue i.e. delete the last queued Isoc In. URB. as failed.
3713 */
3714 vboxusb_urb_t *pUrb = list_remove_tail(&pEp->hIsocInUrbs);
3715 if (pUrb)
3716 {
3717 --pEp->cIsocInUrbs;
3718 Log((DEVICE_NAME ": vboxUsbSolarisIsocInXferError: Deleting last queued URB as it failed\n"));
3719 freemsg(pUrb->pMsg);
3720 RTMemFree(pUrb);
3721 vboxUsbSolarisNotifyComplete(pState);
3722 }
3723
3724 mutex_exit(&pState->Mtx);
3725}
3726
3727
3728/**
3729 * Completion/Exception callback for Isoc OUT Xfers.
3730 *
3731 * @param pPipe The Intr pipe handle.
3732 * @param pReq The Intr request.
3733 * @remarks Completion callback executes in interrupt context!
3734 */
3735LOCAL void vboxUsbSolarisIsocOutXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3736{
3737 LogFunc((DEVICE_NAME ": vboxUsbSolarisIsocOutXferCompleted: pPipe=%p pReq=%p\n", pPipe, pReq));
3738
3739 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3740 if (RT_LIKELY(pEp))
3741 {
3742 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->isoc_client_private;
3743 if (RT_LIKELY(pUrb))
3744 {
3745 size_t cbActPkt = 0;
3746 for (int i = 0; i < pReq->isoc_pkts_count; i++)
3747 {
3748 cbActPkt += pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3749 pUrb->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3750 pUrb->aIsocPkts[i].enmStatus = vboxUsbSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3751 }
3752
3753 Log((DEVICE_NAME ": vboxUsbSolarisIsocOutXferCompleted: cIsocPkts=%d cbData=%d cbActPkt=%d\n", pUrb->cIsocPkts,
3754 pUrb->cbDataR3, cbActPkt));
3755
3756 if (pReq->isoc_completion_reason == USB_CR_OK)
3757 {
3758 if (RT_UNLIKELY(pUrb->pMsg != pReq->isoc_data)) /* Paranoia */
3759 {
3760 freemsg(pUrb->pMsg);
3761 pUrb->pMsg = pReq->isoc_data;
3762 }
3763 }
3764 pReq->isoc_data = NULL;
3765
3766 pUrb->cIsocPkts = pReq->isoc_pkts_count;
3767 pUrb->cbDataR3 = cbActPkt;
3768
3769 /*
3770 * Update the URB and move to landed list for reaping.
3771 */
3772 vboxUsbSolarisDeQueueUrb(pUrb, pReq->isoc_completion_reason);
3773 }
3774 else
3775 Log((DEVICE_NAME ": vboxUsbSolarisIsocOutXferCompleted: Missing private data!?! Dropping OUT pUrb\n"));
3776 }
3777 else
3778 Log((DEVICE_NAME ": vboxUsbSolarisIsocOutXferCompleted: Pipe Gone\n"));
3779
3780 usb_free_isoc_req(pReq);
3781}
3782
3783
3784/**
3785 * Callback when the device gets disconnected.
3786 *
3787 * @param pDip The module structure instance.
3788 *
3789 * @returns Solaris USB error code.
3790 */
3791LOCAL int vboxUsbSolarisDeviceDisconnected(dev_info_t *pDip)
3792{
3793 LogFunc((DEVICE_NAME ": vboxUsbSolarisDeviceDisconnected: pDip=%p\n", pDip));
3794
3795 int instance = ddi_get_instance(pDip);
3796 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
3797
3798 if (RT_LIKELY(pState))
3799 {
3800 /*
3801 * Serialize access: exclusive access to the state.
3802 */
3803 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3804 mutex_enter(&pState->Mtx);
3805
3806 pState->DevState = USB_DEV_DISCONNECTED;
3807
3808 vboxUsbSolarisCloseAllPipes(pState, true /* ControlPipe */);
3809 vboxUsbSolarisNotifyUnplug(pState);
3810
3811 mutex_exit(&pState->Mtx);
3812 usb_release_access(pState->StateMulti);
3813
3814 return USB_SUCCESS;
3815 }
3816
3817 LogRel((DEVICE_NAME ": vboxUsbSolarisDeviceDisconnected: Failed to get device state!\n"));
3818 return USB_FAILURE;
3819}
3820
3821
3822/**
3823 * Callback when the device gets reconnected.
3824 *
3825 * @param pDip The module structure instance.
3826 *
3827 * @returns Solaris USB error code.
3828 */
3829LOCAL int vboxUsbSolarisDeviceReconnected(dev_info_t *pDip)
3830{
3831 LogFunc((DEVICE_NAME ": vboxUsbSolarisDeviceReconnected: pDip=%p\n", pDip));
3832
3833 int instance = ddi_get_instance(pDip);
3834 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
3835
3836 if (RT_LIKELY(pState))
3837 {
3838 vboxUsbSolarisDeviceRestore(pState);
3839 return USB_SUCCESS;
3840 }
3841
3842 LogRel((DEVICE_NAME ": vboxUsbSolarisDeviceReconnected: Failed to get device state!\n"));
3843 return USB_FAILURE;
3844}
3845
3846
3847/**
3848 * Restores device state after a reconnect or resume.
3849 *
3850 * @param pState The USB device instance.
3851 */
3852LOCAL void vboxUsbSolarisDeviceRestore(vboxusb_state_t *pState)
3853{
3854 LogFunc((DEVICE_NAME ": vboxUsbSolarisDeviceRestore: pState=%p\n", pState));
3855 AssertPtrReturnVoid(pState);
3856
3857 /*
3858 * Raise device power.
3859 */
3860 vboxUsbSolarisPowerBusy(pState);
3861 int rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
3862
3863 /*
3864 * Check if the same device is resumed/reconnected.
3865 */
3866 rc = usb_check_same_device(pState->pDip,
3867 NULL, /* log handle */
3868 USB_LOG_L2, /* log level */
3869 -1, /* log mask */
3870 USB_CHK_ALL, /* check level */
3871 NULL); /* device string */
3872
3873 if (rc != USB_SUCCESS)
3874 {
3875 mutex_enter(&pState->Mtx);
3876 pState->DevState = USB_DEV_DISCONNECTED;
3877 mutex_exit(&pState->Mtx);
3878
3879 /* Do we need to inform userland here? */
3880 vboxUsbSolarisPowerIdle(pState);
3881 Log((DEVICE_NAME ": vboxUsbSolarisDeviceRestore: Not the same device\n"));
3882 return;
3883 }
3884
3885 /*
3886 * Serialize access to not race with other PM functions.
3887 */
3888 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3889
3890 mutex_enter(&pState->Mtx);
3891 if (pState->DevState == USB_DEV_DISCONNECTED)
3892 pState->DevState = USB_DEV_ONLINE;
3893 else if (pState->DevState == USB_DEV_SUSPENDED)
3894 pState->DevState = USB_DEV_ONLINE;
3895
3896 mutex_exit(&pState->Mtx);
3897 usb_release_access(pState->StateMulti);
3898
3899 vboxUsbSolarisPowerIdle(pState);
3900}
3901
3902
3903/**
3904 * Restores device state after a reconnect or resume.
3905 *
3906 * @param pState The USB device instance.
3907 *
3908 * @returns VBox status code.
3909 */
3910LOCAL int vboxUsbSolarisDeviceSuspend(vboxusb_state_t *pState)
3911{
3912 LogFunc((DEVICE_NAME ": vboxUsbSolarisDeviceSuspend: pState=%p\n", pState));
3913
3914 int rc = VERR_VUSB_DEVICE_IS_SUSPENDED;
3915 mutex_enter(&pState->Mtx);
3916
3917 switch (pState->DevState)
3918 {
3919 case USB_DEV_SUSPENDED:
3920 {
3921 LogRel((DEVICE_NAME ": vboxUsbSolarisDeviceSuspend: Invalid device state %d\n", pState->DevState));
3922 break;
3923 }
3924
3925 case USB_DEV_ONLINE:
3926 case USB_DEV_DISCONNECTED:
3927 case USB_DEV_PWRED_DOWN:
3928 {
3929 int PreviousState = pState->DevState;
3930 pState->DevState = USB_DEV_DISCONNECTED;
3931
3932 /** @todo this doesn't make sense when for e.g. an INTR IN URB with infinite
3933 * timeout is pending on the device. Fix suspend logic later. */
3934 /*
3935 * Drain pending URBs.
3936 */
3937 for (int i = 0; i < VBOXUSB_DRAIN_TIME; i++)
3938 {
3939 if (pState->cInflightUrbs < 1)
3940 break;
3941
3942 mutex_exit(&pState->Mtx);
3943 delay(drv_usectohz(100000));
3944 mutex_enter(&pState->Mtx);
3945 }
3946
3947 /*
3948 * Deny suspend if we still have pending URBs.
3949 */
3950 if (pState->cInflightUrbs > 0)
3951 {
3952 pState->DevState = PreviousState;
3953 LogRel((DEVICE_NAME ": Cannot suspend %s %s (Ident=%s), %d inflight URBs\n", pState->szMfg, pState->szProduct,
3954 pState->ClientInfo.szDeviceIdent, pState->cInflightUrbs));
3955
3956 mutex_exit(&pState->Mtx);
3957 return VERR_RESOURCE_BUSY;
3958 }
3959
3960 pState->cInflightUrbs = 0;
3961
3962 /*
3963 * Serialize access to not race with Open/Detach/Close and
3964 * Close all pipes including the default pipe.
3965 */
3966 mutex_exit(&pState->Mtx);
3967 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3968 mutex_enter(&pState->Mtx);
3969
3970 vboxUsbSolarisCloseAllPipes(pState, true /* default pipe */);
3971 vboxUsbSolarisNotifyUnplug(pState);
3972
3973 mutex_exit(&pState->Mtx);
3974 usb_release_access(pState->StateMulti);
3975
3976 LogRel((DEVICE_NAME ": Suspended %s %s (Ident=%s)\n", pState->szMfg, pState->szProduct,
3977 pState->ClientInfo.szDeviceIdent));
3978 return VINF_SUCCESS;
3979 }
3980 }
3981
3982 mutex_exit(&pState->Mtx);
3983 Log((DEVICE_NAME ": vboxUsbSolarisDeviceSuspend: Returns %d\n", rc));
3984 return rc;
3985}
3986
3987
3988/**
3989 * Restores device state after a reconnect or resume.
3990 *
3991 * @param pState The USB device instance.
3992 */
3993LOCAL void vboxUsbSolarisDeviceResume(vboxusb_state_t *pState)
3994{
3995 LogFunc((DEVICE_NAME ": vboxUsbSolarisDeviceResume: pState=%p\n", pState));
3996 return vboxUsbSolarisDeviceRestore(pState);
3997}
3998
3999
4000/**
4001 * Flags the PM component as busy so the system will not manage it's power.
4002 *
4003 * @param pState The USB device instance.
4004 */
4005LOCAL void vboxUsbSolarisPowerBusy(vboxusb_state_t *pState)
4006{
4007 LogFunc((DEVICE_NAME ": vboxUsbSolarisPowerBusy: pState=%p\n", pState));
4008 AssertPtrReturnVoid(pState);
4009
4010 mutex_enter(&pState->Mtx);
4011 if (pState->pPower)
4012 {
4013 pState->pPower->PowerBusy++;
4014 mutex_exit(&pState->Mtx);
4015
4016 int rc = pm_busy_component(pState->pDip, 0 /* component */);
4017 if (rc != DDI_SUCCESS)
4018 {
4019 Log((DEVICE_NAME ": vboxUsbSolarisPowerBusy: Busy component failed! rc=%d\n", rc));
4020 mutex_enter(&pState->Mtx);
4021 pState->pPower->PowerBusy--;
4022 mutex_exit(&pState->Mtx);
4023 }
4024 }
4025 else
4026 mutex_exit(&pState->Mtx);
4027}
4028
4029
4030/**
4031 * Flags the PM component as idle so its power managed by the system.
4032 *
4033 * @param pState The USB device instance.
4034 */
4035LOCAL void vboxUsbSolarisPowerIdle(vboxusb_state_t *pState)
4036{
4037 LogFunc((DEVICE_NAME ": vboxUsbSolarisPowerIdle: pState=%p\n", pState));
4038 AssertPtrReturnVoid(pState);
4039
4040 if (pState->pPower)
4041 {
4042 int rc = pm_idle_component(pState->pDip, 0 /* component */);
4043 if (rc == DDI_SUCCESS)
4044 {
4045 mutex_enter(&pState->Mtx);
4046 Assert(pState->pPower->PowerBusy > 0);
4047 pState->pPower->PowerBusy--;
4048 mutex_exit(&pState->Mtx);
4049 }
4050 else
4051 Log((DEVICE_NAME ": vboxUsbSolarisPowerIdle: Idle component failed! rc=%d\n", rc));
4052 }
4053}
4054
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette