~ubuntu-branches/ubuntu/trusty/argyll/trusty-proposed

« back to all changes in this revision

Viewing changes to libusbw/src/driver/get_descriptor.c

  • Committer: Package Import Robot
  • Author(s): Artur Rona
  • Date: 2014-02-12 00:35:39 UTC
  • mfrom: (13.1.24 sid)
  • Revision ID: package-import@ubuntu.com-20140212003539-24tautzlitsiz61w
Tags: 1.5.1-5ubuntu1
* Merge from Debian unstable. (LP: #1275572) Remaining changes:
  - debian/control:
    + Build-depend on libtiff-dev rather than libtiff4-dev.
  - debian/control, debian/patches/06_fix_udev_rule.patch:
    + Fix udev rules to actually work; ENV{ACL_MANAGE} has
      stopped working ages ago, and with logind it's now the
      "uaccess" tag. Dropping also consolekit from Recommends.
  - debian/patches/drop-usb-db.patch:
    + Use hwdb builtin, instead of the obsolete usb-db
      in the udev rules.
* debian/patches/05_ftbfs-underlinkage.diff:
  - Dropped change, no needed anymore.
* Refresh the patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LIBUSB-WIN32, Generic Windows USB Library
2
 
 * Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
 
20
 
#include "libusb_driver.h"
21
 
 
22
 
 
23
 
 
24
 
NTSTATUS get_descriptor(libusb_device_t *dev,
25
 
                        void *buffer, int size, int type, int recipient,
26
 
                        int index, int language_id, int *received, int timeout)
27
 
{
28
 
  NTSTATUS status = STATUS_SUCCESS;
29
 
  URB urb;
30
 
 
31
 
  DEBUG_PRINT_NL();
32
 
  DEBUG_MESSAGE("get_descriptor(): buffer size %d", size);
33
 
  DEBUG_MESSAGE("get_descriptor(): type %04d", type);
34
 
  DEBUG_MESSAGE("get_descriptor(): recipient %04d", recipient);
35
 
  DEBUG_MESSAGE("get_descriptor(): index %04d", index);
36
 
  DEBUG_MESSAGE("get_descriptor(): language id %04d", language_id);
37
 
  DEBUG_MESSAGE("get_descriptor(): timeout %d", timeout);
38
 
  
39
 
 
40
 
  memset(&urb, 0, sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST));
41
 
 
42
 
  switch(recipient)
43
 
    {
44
 
    case USB_RECIP_DEVICE:
45
 
      urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE;
46
 
      break;
47
 
    case USB_RECIP_INTERFACE:
48
 
      urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE;
49
 
      break;
50
 
    case USB_RECIP_ENDPOINT:
51
 
      urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_ENDPOINT;
52
 
      break;
53
 
    default:
54
 
      DEBUG_ERROR("get_descriptor(): invalid recipient");
55
 
      return STATUS_INVALID_PARAMETER;
56
 
    }
57
 
 
58
 
 
59
 
  urb.UrbHeader.Length = sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST);
60
 
  urb.UrbControlDescriptorRequest.TransferBufferLength = size;
61
 
  urb.UrbControlDescriptorRequest.TransferBuffer = buffer;
62
 
  urb.UrbControlDescriptorRequest.DescriptorType = (UCHAR)type;
63
 
  urb.UrbControlDescriptorRequest.Index = (UCHAR)index;
64
 
  urb.UrbControlDescriptorRequest.LanguageId = (USHORT)language_id;
65
 
  
66
 
 
67
 
  status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout);
68
 
 
69
 
      
70
 
  if(!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status))
71
 
    {
72
 
      DEBUG_ERROR("get_descriptor(): getting descriptor "
73
 
                  "failed: status: 0x%x, urb-status: 0x%x", 
74
 
                  status, urb.UrbHeader.Status);
75
 
      *received = 0;
76
 
    }
77
 
  else
78
 
    {
79
 
      *received = urb.UrbControlDescriptorRequest.TransferBufferLength;
80
 
    }
81
 
 
82
 
  return status;
83
 
}
84
 
 
85
 
USB_CONFIGURATION_DESCRIPTOR *
86
 
get_config_descriptor(libusb_device_t *dev, int value, int *size)
87
 
{
88
 
  NTSTATUS status;
89
 
  USB_CONFIGURATION_DESCRIPTOR *desc = NULL;
90
 
  USB_DEVICE_DESCRIPTOR device_descriptor;
91
 
  int i;
92
 
  volatile int desc_size;
93
 
 
94
 
  status = get_descriptor(dev, &device_descriptor,
95
 
                          sizeof(USB_DEVICE_DESCRIPTOR), 
96
 
                          USB_DEVICE_DESCRIPTOR_TYPE,
97
 
                          USB_RECIP_DEVICE,
98
 
                          0, 0, size, LIBUSB_DEFAULT_TIMEOUT);  
99
 
 
100
 
  if(!NT_SUCCESS(status) || *size != sizeof(USB_DEVICE_DESCRIPTOR))
101
 
    {
102
 
      DEBUG_ERROR("get_config_descriptor(): getting device descriptor failed");
103
 
      return NULL;
104
 
    }
105
 
 
106
 
  if(!(desc = ExAllocatePool(NonPagedPool, 
107
 
                             sizeof(USB_CONFIGURATION_DESCRIPTOR))))
108
 
    {
109
 
      DEBUG_ERROR("get_config_descriptor(): memory allocation error");
110
 
      return NULL;
111
 
    }
112
 
 
113
 
  for(i = 0; i < device_descriptor.bNumConfigurations; i++)
114
 
    {
115
 
 
116
 
      if(!NT_SUCCESS(get_descriptor(dev, desc, 
117
 
                                    sizeof(USB_CONFIGURATION_DESCRIPTOR), 
118
 
                                    USB_CONFIGURATION_DESCRIPTOR_TYPE,
119
 
                                    USB_RECIP_DEVICE,
120
 
                                    i, 0, size, LIBUSB_DEFAULT_TIMEOUT)))
121
 
        {
122
 
          DEBUG_ERROR("get_config_descriptor(): getting configuration "
123
 
                      "descriptor failed");
124
 
          break;
125
 
        }
126
 
 
127
 
      if(desc->bConfigurationValue == value)
128
 
        {
129
 
          desc_size = desc->wTotalLength;
130
 
          ExFreePool(desc);
131
 
 
132
 
          if(!(desc = ExAllocatePool(NonPagedPool, desc_size)))
133
 
            {
134
 
              DEBUG_ERROR("get_config_descriptor(): memory allocation error");
135
 
              break;
136
 
            }
137
 
          
138
 
          if(!NT_SUCCESS(get_descriptor(dev, desc, desc_size,
139
 
                                        USB_CONFIGURATION_DESCRIPTOR_TYPE,
140
 
                                        USB_RECIP_DEVICE,
141
 
                                        i, 0, size, LIBUSB_DEFAULT_TIMEOUT)))
142
 
            {
143
 
              DEBUG_ERROR("get_config_descriptor(): getting configuration "
144
 
                          "descriptor failed");
145
 
              break;
146
 
            }
147
 
          
148
 
          return desc;
149
 
        }
150
 
    }
151
 
 
152
 
  if(desc)
153
 
    {
154
 
      ExFreePool(desc);
155
 
    }
156
 
 
157
 
  return NULL;
158
 
}