--- /dev/null
+/*
+ * lsdev.c
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <../include/libhusb.h>
+
+static void print_devs(libhusb_device **devs, libhusb_context *ctx)
+{
+ libhusb_device *dev;
+ int i = 0, j = 0;
+ uint8_t path[8];
+ int r;
+
+ int data1;
+ libhusb_device_handle *dev_handle;
+ libhusb_device *dev_vp;
+ char str1[256];
+ struct libhusb_cfg_descriptor *config;
+ struct libhusb_device_descriptor desc;
+ int conf;
+
+ printf("Getting device list: \n");
+
+ while ((dev = devs[i++]) != NULL) {
+ r = libhusb_get_device_descriptor(dev, &desc);
+ if (r < 0) {
+ fprintf(stderr, "failed to get device descriptor");
+ return;
+ }
+
+ printf("%04x:%04x (bus %d, device %d)",
+ desc.idVendor, desc.idProduct,
+ libhusb_get_bus_number(dev), libhusb_get_address(dev));
+
+ r = libhusb_get_port_numbers(dev, path, sizeof(path));
+ if (r < 0)
+ printf("cant get port numbers\n");
+ else if (r > 0) {
+ printf(" path: %d", path[0]);
+ for (j = 1; j < r; j++)
+ printf(".%d", path[j]);
+ }
+ printf("\n");
+ }
+
+ int vid, pid;
+ printf("provide vid and pid of device which you want to operate on\n");
+ printf("\n enter device vid and hit enter(with 0x):");
+ scanf("%x", &vid);
+ printf("\n enter device pid and hit enter(with 0x):");
+ scanf("%x", &pid);
+
+ printf("\nOpening device vid:0x%x pid:0x%x\n", vid, pid);
+
+ dev_handle = libhusb_device_open_with_vid_pid(ctx, vid, pid);
+ if (dev_handle == NULL) {
+ printf("cant get dev with vid pid\n");
+ return;
+ }
+ if (dev_handle != NULL) printf("opened\n");
+
+ dev_vp = libhusb_get_device(dev_handle);
+ if (dev_vp == NULL) {
+ printf("ERROR\n");
+ return;
+ }
+
+ data1 = libhusb_get_max_packet_size(dev_vp, 1);
+ if (data1 < 0) {
+ printf("ERROR: %s", libhusb_error_name(data1));
+ return;
+ }
+
+ printf("\nmax packet size: %d\n", data1);
+
+ r = libhusb_get_active_config(dev_handle, &conf);
+ if (r < 0) {
+ printf("ERROR: %s", libhusb_error_name(r));
+ return;
+ }
+
+ printf("\nbconfig: %d\n", conf);
+
+ printf("\ntrying to set 2nd config\n");
+ r = libhusb_set_config(dev_handle, 1);
+ if (r < 0) printf("failure\n");
+
+ printf("claiming iface\n");
+ r = libhusb_claim_interface(dev_handle, 0, 0);
+ if (r < 0) printf("failure\n");
+
+ printf("releasing iface\n");
+ r = libhusb_release_interface(dev_handle, 0);
+ if (r < 0) printf("failure\n");
+
+/*
+ commented because sometime after reset device require hardware restart
+ printf("reseting device \n");
+ r = libhusb_reset_device(dev_handle);
+ if (r < 0) printf("failure\n");
+*/
+
+ printf("is kernel driver active on iface 1? \n");
+ r = libhusb_kernel_driver_active(dev_handle, 0);
+ if (r == 0)
+ printf("no active driver\n");
+ else if (r == 1)
+ printf("kernel driver is active\n");
+ else if (r != 0 || r != 1)
+ printf("ERROR\n");
+
+ printf("detaching kernel driver\n");
+ r = libhusb_detach_kernel_driver(dev_handle, 0);
+ if (r < 0) printf("failure\n");
+
+ printf("attaching kernel driver\n");
+ r = libhusb_attach_kernel_driver(dev_handle, 0);
+ if (r < 0) printf("failure\n");
+
+ r = libhusb_get_string_descriptor_ascii(dev_handle, desc.iProduct,
+ (unsigned char *) str1, sizeof(str1));
+ if (r < 0) printf("failure\n");
+ else printf("Product name: %s\n", str1);
+
+ r = libhusb_get_config_descriptor(dev_vp, 0, &config);
+ if (r < 0) printf("failure\n");
+
+ libhusb_free_config_descriptor(config);
+ libhusb_close(dev_handle);
+}
+
+int main(void)
+{
+ struct libhusb_context *ctx;
+ struct libhusb_device **devs;
+ int r;
+ ssize_t cnt;
+
+ r = libhusb_init(&ctx);
+ if (r < 0) {
+ printf("ERROR: %s", libhusb_error_name(r));
+ return r;
+ }
+
+ cnt = libhusb_get_devices(ctx, &devs);
+ if (cnt < 0)
+ return (int) cnt;
+
+ print_devs(devs, ctx);
+ libhusb_free_devices(devs, 1);
+
+ libhusb_exit(ctx);
+
+ return 0;
+}