sbUSBDeviceUtils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #ifndef __SB_USB_DEVICE_UTILS_H__
28 #define __SB_USB_DEVICE_UTILS_H__
29 
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 //
33 // Songbird USB device utilities defs.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
43 //------------------------------------------------------------------------------
44 //
45 // Songbird USB device utilities imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Mozilla imports.
50 #include <nsAutoPtr.h>
51 #include <nsTArray.h>
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // Songbird USB device utilities typedefs.
57 //
58 //------------------------------------------------------------------------------
59 
60 // List of USB descriptors.
62 typedef nsTArray<nsRefPtr<sbUSBDescriptor> > sbUSBDescriptorList;
63 
64 
65 //------------------------------------------------------------------------------
66 //
67 // Songbird USB standard defs and structures.
68 //
69 //------------------------------------------------------------------------------
70 
71 //
72 // USB descriptor type defs.
73 //
74 
75 #define SB_USB_DESCRIPTOR_TYPE_DEVICE 1
76 #define SB_USB_DESCRIPTOR_TYPE_CONFIGURATION 2
77 #define SB_USB_DESCRIPTOR_TYPE_STRING 3
78 #define SB_USB_DESCRIPTOR_TYPE_INTERFACE 4
79 #define SB_USB_DESCRIPTOR_TYPE_ENDPOINT 5
80 
81 //
82 // Apple's USB vendor ID is 05ac according to: http://www.linux-usb.org/usb.ids
83 //
84 #define SB_USB_APPLE_VENDOR_ID 0x05ac
85 
93 typedef struct
94 {
95  PRUint8 bLength;
96  PRUint8 bDescriptorType;
98 
99 
122 typedef struct
123 {
124  PRUint8 bLength;
126  PRUint16 bcdUSB;
127  PRUint8 bDeviceClass;
131  PRUint16 idVendor;
132  PRUint16 idProduct;
133  PRUint16 bcdDevice;
134  PRUint8 iManufacturer;
135  PRUint8 iProduct;
136  PRUint8 iSerialNumber;
139 
140 
161 typedef struct
162 {
163  PRUint8 bLength;
165  PRUint16 wTotalLength;
166  PRUint8 bNumInterfaces;
168  PRUint8 iConfiguration;
169  PRUint8 bmAttributes;
170  PRUint8 bMaxPower;
172 
173 
192 typedef struct
193 {
194  PRUint8 bLength;
198  PRUint8 bNumEndpoints;
202  PRUint8 iInterface;
204 
205 
214 typedef struct
215 {
216  PRUint8 bLength;
218  PRUnichar bString[1];
220 
221 
222 //------------------------------------------------------------------------------
223 //
224 // Songbird USB device utilities classes and structures.
225 //
226 //------------------------------------------------------------------------------
227 
228 //
229 // sbUSBDeviceRef Reference to a USB device. This class should be
230 // sub-classed by the USB device system services
231 // to contain whatever data is required to
232 // reference a USB device.
233 //
234 
235 class sbUSBDeviceRef {};
236 
237 
244 {
245 public:
246  //
247  // Inherited interfaces.
248  //
249 
251 
252 
253  //
254  // Constructors/destructors.
255  //
256 
257  sbUSBDescriptor(void* aDescriptor,
258  PRUint16 aDescriptorLength) {
259  mDescriptor = NS_Alloc(aDescriptorLength);
260  if (mDescriptor) {
261  mTotalLength = aDescriptorLength;
262  memcpy(mDescriptor, aDescriptor, mTotalLength);
263  } else {
264  mTotalLength = 0;
265  }
266  }
267 
269  {
270  if (aDescriptor && (aDescriptor->bLength > 0))
271  mDescriptor = NS_Alloc(aDescriptor->bLength);
272  else
273  mDescriptor = nsnull;
274  if (mDescriptor) {
275  mTotalLength = aDescriptor->bLength;
276  memcpy(mDescriptor, aDescriptor, mTotalLength);
277  } else {
278  mTotalLength = 0;
279  }
280  }
281 
283  {
284  if (mDescriptor)
285  NS_Free(mDescriptor);
286  }
287 
288 
293  void* Get()
294  {
295  return mDescriptor;
296  }
297 
299  {
300  return static_cast<sbUSBDescriptorHeader*>(mDescriptor);
301  }
302 
304  {
305  return static_cast<sbUSBConfigurationDescriptor*>(mDescriptor);
306  }
307 
309  {
310  return static_cast<sbUSBDeviceDescriptor*>(mDescriptor);
311  }
312 
314  {
315  return static_cast<sbUSBStringDescriptor*>(mDescriptor);
316  }
317 
318  PRUint16 GetTotalLength()
319  {
320  return mTotalLength;
321  }
322 
323 
324 private:
325  //
326  // mDescriptor USB descriptor.
327  // mTotalLength Total length of descriptor data.
328  //
329 
330  void* mDescriptor;
331  PRUint16 mTotalLength;
332 };
333 
334 
335 //------------------------------------------------------------------------------
336 //
337 // Songbird USB device services.
338 //
339 //------------------------------------------------------------------------------
340 
341 //
342 // Songbird USB device services.
343 //
344 
345 nsresult sbUSBDeviceImplementsClass(sbUSBDescriptorList& aDescriptorList,
346  PRUint8 aUSBClass,
347  PRBool* aImplementsClass);
348 
349 nsresult sbUSBDeviceGetDescriptor(sbUSBDeviceRef* aDeviceRef,
350  PRUint8 aDescriptorType,
351  PRUint8 aDescriptorIndex,
352  PRUint16 aDescriptorIndex2,
353  sbUSBDescriptor** aDescriptor);
354 
356  PRUint8 aDescriptorIndex,
357  PRUint16 aDescriptorIndex2,
358  nsAString& aString);
359 
360 nsresult sbUSBDeviceGetLanguageList(sbUSBDeviceRef* aDeviceRef,
361  nsTArray<PRUint16>& aLanguageList);
362 
364  (sbUSBDeviceRef* aDeviceRef,
365  PRUint8 aConfigurationIndex,
366  sbUSBDescriptorList& aDescriptorList);
367 
368 
369 //
370 // Songbird USB device system services.
371 //
372 // These services must be provided by a system dependent sources.
373 //
374 
375 nsresult sbUSBDeviceGetDescriptor(sbUSBDeviceRef* aDeviceRef,
376  PRUint8 aDescriptorType,
377  PRUint8 aDescriptorIndex,
378  PRUint16 aDescriptorIndex2,
379  PRUint16 aDescriptorLength,
380  sbUSBDescriptor** aDescriptor);
381 
382 
383 #endif // __SB_USB_DEVICE_UTILS_H__
384 
sbUSBDescriptor(sbUSBDescriptorHeader *aDescriptor)
nsTArray< nsRefPtr< sbUSBDescriptor > > sbUSBDescriptorList
sbUSBDeviceDescriptor * GetDevice()
nsresult sbUSBDeviceAddConfigurationDescriptors(sbUSBDeviceRef *aDeviceRef, PRUint8 aConfigurationIndex, sbUSBDescriptorList &aDescriptorList)
NS_DECL_ISUPPORTS sbUSBDescriptor(void *aDescriptor, PRUint16 aDescriptorLength)
sbUSBConfigurationDescriptor * GetConfiguration()
sbUSBDescriptorHeader * GetHeader()
nsresult sbUSBDeviceImplementsClass(sbUSBDescriptorList &aDescriptorList, PRUint8 aUSBClass, PRBool *aImplementsClass)
PRUint16 GetTotalLength()
sbUSBStringDescriptor * GetString()
nsresult sbUSBDeviceGetStringDescriptor(sbUSBDeviceRef *aDeviceRef, PRUint8 aDescriptorIndex, PRUint16 aDescriptorIndex2, nsAString &aString)
virtual ~sbUSBDescriptor()
nsresult sbUSBDeviceGetDescriptor(sbUSBDeviceRef *aDeviceRef, PRUint8 aDescriptorType, PRUint8 aDescriptorIndex, PRUint16 aDescriptorIndex2, sbUSBDescriptor **aDescriptor)
nsresult sbUSBDeviceGetLanguageList(sbUSBDeviceRef *aDeviceRef, nsTArray< PRUint16 > &aLanguageList)