VirtualBox

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

Last change on this file since 59176 was 59176, checked in by vboxsync, 9 years ago

Solaris/USB: Take the easy way out for now.

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