~ubuntu-branches/ubuntu/lucid/hal/lucid-proposed

« back to all changes in this revision

Viewing changes to hald/freebsd/probing/probe-usb2-device.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-12-03 01:21:24 UTC
  • mto: (1.1.18 squeeze)
  • mto: This revision was merged to the branch mainline in revision 165.
  • Revision ID: james.westby@ubuntu.com-20091203012124-3573qknop973uvc2
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 * CVSID: $Id$
 
3
 *
 
4
 * probe-usb2-device.c : USB2 Device poller
 
5
 *
 
6
 * Copyright (C) 2009 Joe Marcus Clarke <marcus@FreeBSD.org>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 **************************************************************************/
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#  include <config.h>
 
26
#endif
 
27
 
 
28
#include <sys/param.h>
 
29
#include <sys/types.h>
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
#include <unistd.h>
 
34
 
 
35
#include <libusb20_desc.h>
 
36
#include <libusb20.h>
 
37
#if __FreeBSD_version >= 800064
 
38
#include <dev/usb/usb_ioctl.h>
 
39
#else
 
40
#include <dev/usb2/include/usb2_standard.h>
 
41
#include <dev/usb2/include/usb2_ioctl.h>
 
42
#endif
 
43
 
 
44
#include "../libprobe/hfp.h"
 
45
 
 
46
int
 
47
main(int argc, char **argv)
 
48
{
 
49
  struct libusb20_backend *pbe = NULL;
 
50
  struct libusb20_device *pdev = NULL;
 
51
  char *busstr, *addrstr;
 
52
  int bus, addr;
 
53
 
 
54
  if (! hfp_init(argc, argv))
 
55
    goto end;
 
56
 
 
57
  pbe = libusb20_be_alloc_default();
 
58
  if (pbe == NULL)
 
59
    goto end;
 
60
 
 
61
  busstr = getenv("HAL_PROP_USB_DEVICE_BUS_NUMBER");
 
62
  if (! busstr)
 
63
    goto end;
 
64
 
 
65
  addrstr = getenv("HAL_PROP_USB_DEVICE_PORT_NUMBER");
 
66
  if (! addrstr)
 
67
    goto end;
 
68
 
 
69
  bus = atoi(busstr);
 
70
  addr = atoi(addrstr);
 
71
 
 
72
  while ((pdev = libusb20_be_device_foreach(pbe, pdev)))
 
73
    {
 
74
      struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
 
75
      struct LIBUSB20_CONFIG_DESC_DECODED *cdesc;
 
76
#if __FreeBSD_version >= 800092
 
77
      struct usb_device_info di;
 
78
#else
 
79
      struct usb2_device_info di;
 
80
#endif
 
81
      struct libusb20_config *pcfg = NULL;
 
82
      int curr_config;
 
83
      int bcdspeed = 0;
 
84
      uint8_t temp_string[256];
 
85
      double speed = 0.0;
 
86
      double version = 1.0;
 
87
 
 
88
      if (libusb20_dev_get_bus_number(pdev) != bus ||
 
89
          libusb20_dev_get_address(pdev) != addr)
 
90
        continue;
 
91
 
 
92
      if (libusb20_dev_open(pdev, 0))
 
93
        continue;
 
94
 
 
95
      ddesc = libusb20_dev_get_device_desc(pdev);
 
96
      curr_config = libusb20_dev_get_config_index(pdev);
 
97
      pcfg = libusb20_dev_alloc_config(pdev, curr_config);
 
98
      cdesc = &(pcfg->desc);
 
99
 
 
100
      if (pcfg == NULL || libusb20_dev_get_info(pdev, &di))
 
101
        {
 
102
          free(pcfg);
 
103
          continue;
 
104
        }
 
105
 
 
106
      memset(temp_string, 0, sizeof(temp_string));
 
107
 
 
108
      if (cdesc->iConfiguration !=0)
 
109
        libusb20_dev_req_string_simple_sync(pdev, cdesc->iConfiguration,
 
110
          temp_string, sizeof(temp_string));
 
111
 
 
112
      libhal_device_set_property_string(hfp_ctx, hfp_udi,
 
113
        "usb_device.configuration", (char *) temp_string, &hfp_error);
 
114
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
115
        "usb_device.configuration_value", cdesc->bConfigurationValue,
 
116
        &hfp_error);
 
117
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
118
        "usb_device.num_configurations", ddesc->bNumConfigurations,
 
119
        &hfp_error);
 
120
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
121
        "usb_device.device_class", di.udi_class, &hfp_error);
 
122
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
123
        "usb_device.device_subclass", di.udi_subclass, &hfp_error);
 
124
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
125
        "usb_device.device_protocol", di.udi_protocol, &hfp_error);
 
126
      libhal_device_set_property_bool(hfp_ctx, hfp_udi,
 
127
        "usb_device.is_self_powered",
 
128
        di.udi_power == 0 ? TRUE : FALSE, &hfp_error);
 
129
      libhal_device_set_property_bool(hfp_ctx, hfp_udi,
 
130
        "usb_device.can_wake_up",
 
131
        (cdesc->bmAttributes & UC_REMOTE_WAKEUP) != 0 ? TRUE : FALSE,
 
132
        &hfp_error);
 
133
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
134
        "usb_device.max_power", di.udi_power, &hfp_error);
 
135
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
136
        "usb_device.num_interfaces", pcfg->num_interface, &hfp_error);
 
137
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
138
        "usb_device.num_ports", di.udi_nports, &hfp_error);
 
139
 
 
140
      switch (libusb20_dev_get_speed(pdev))
 
141
        {
 
142
          case LIBUSB20_SPEED_LOW:
 
143
            speed = 1.5;
 
144
            bcdspeed = 0x00150;
 
145
            break;
 
146
          case LIBUSB20_SPEED_FULL:
 
147
            speed = 12.0;
 
148
            bcdspeed = 0x01200;
 
149
            break;
 
150
          case LIBUSB20_SPEED_HIGH:
 
151
            speed = 480.0;
 
152
            bcdspeed = 0x48000;
 
153
            break;
 
154
          case LIBUSB20_SPEED_SUPER:
 
155
            speed = 4800.0;
 
156
            bcdspeed = 0x480000;
 
157
            break;
 
158
          default:
 
159
            ;
 
160
        }
 
161
 
 
162
      libhal_device_set_property_double(hfp_ctx, hfp_udi, "usb_device.speed",
 
163
        speed, &hfp_error);
 
164
      libhal_device_set_property_int(hfp_ctx, hfp_udi, "usb_device.speed_bcd",
 
165
        bcdspeed, &hfp_error);
 
166
 
 
167
      switch (ddesc->bcdUSB)
 
168
        {
 
169
          case UD_USB_2_0:
 
170
            version = 2.0;
 
171
            break;
 
172
          case UD_USB_3_0:
 
173
            version = 3.0;
 
174
            break;
 
175
          default:
 
176
            version = 1.0;
 
177
            break;
 
178
        }
 
179
 
 
180
      libhal_device_set_property_double(hfp_ctx, hfp_udi,
 
181
        "usb_device.version", version, &hfp_error);
 
182
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
183
        "usb_device.product_id", di.udi_productNo, &hfp_error);
 
184
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
185
        "usb_device.vendor_id", di.udi_vendorNo, &hfp_error);
 
186
      libhal_device_set_property_int(hfp_ctx, hfp_udi,
 
187
        "usb_device.device_revision_bcd", ddesc->bcdUSB, &hfp_error);
 
188
      libhal_device_set_property_string(hfp_ctx, hfp_udi,
 
189
        "usb_device.serial", di.udi_serial, &hfp_error);
 
190
      libhal_device_set_property_string(hfp_ctx, hfp_udi,
 
191
        "usb_device.product", di.udi_product, &hfp_error);
 
192
      libhal_device_set_property_string(hfp_ctx, hfp_udi,
 
193
        "usb_device.vendor", di.udi_vendor, &hfp_error);
 
194
 
 
195
      libhal_device_set_property_string(hfp_ctx, hfp_udi,
 
196
        "info.product", di.udi_product, &hfp_error);
 
197
      libhal_device_set_property_string(hfp_ctx, hfp_udi,
 
198
        "info.vendor", di.udi_vendor, &hfp_error);
 
199
 
 
200
      libusb20_dev_close(pdev);
 
201
      free(pcfg);
 
202
    }
 
203
end:
 
204
  if (pbe)
 
205
    libusb20_be_free(pbe);
 
206
 
 
207
  return 0;
 
208
}