bluez/tools/hid2hci.c

352 lines
7.4 KiB
C
Raw Normal View History

2004-04-24 19:16:58 +08:00
/*
*
* BlueZ - Bluetooth protocol stack for Linux
2004-04-24 19:16:58 +08:00
*
2006-01-03 21:28:56 +08:00
* Copyright (C) 2003-2006 Marcel Holtmann <marcel@holtmann.org>
2004-04-24 19:16:58 +08:00
*
*
* This program is free software; you can redistribute it and/or modify
2005-10-30 06:36:31 +08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
2004-04-24 19:16:58 +08:00
*
2005-10-30 06:36:31 +08:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2004-04-24 19:16:58 +08:00
*
2005-10-30 06:36:31 +08:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2004-04-24 19:16:58 +08:00
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <sys/ioctl.h>
2004-07-27 04:57:01 +08:00
#include <usb.h>
2004-04-24 19:16:58 +08:00
#ifdef NEED_USB_GET_BUSSES
static inline struct usb_bus *usb_get_busses(void)
2004-07-27 04:57:01 +08:00
{
return usb_busses;
}
#endif
2004-04-24 19:16:58 +08:00
2004-07-27 04:57:01 +08:00
#ifndef USB_DIR_OUT
#define USB_DIR_OUT 0x00
#endif
2004-04-24 19:16:58 +08:00
static char devpath[PATH_MAX + 1] = "/dev";
struct hiddev_devinfo {
unsigned int bustype;
unsigned int busnum;
unsigned int devnum;
unsigned int ifnum;
short vendor;
short product;
short version;
unsigned num_applications;
};
struct hiddev_report_info {
unsigned report_type;
unsigned report_id;
unsigned num_fields;
};
typedef __signed__ int __s32;
struct hiddev_usage_ref {
unsigned report_type;
unsigned report_id;
unsigned field_index;
unsigned usage_index;
unsigned usage_code;
__s32 value;
};
#define HIDIOCGDEVINFO _IOR('H', 0x03, struct hiddev_devinfo)
#define HIDIOCINITREPORT _IO('H', 0x05)
#define HIDIOCSREPORT _IOW('H', 0x08, struct hiddev_report_info)
#define HIDIOCSUSAGE _IOW('H', 0x0C, struct hiddev_usage_ref)
#define HID_REPORT_TYPE_OUTPUT 2
#define HCI 0
#define HID 1
struct device_info;
struct device_id {
int mode;
uint16_t vendor;
uint16_t product;
int (*func)(struct device_info *dev);
};
struct device_info {
2004-07-27 04:57:01 +08:00
struct usb_device *dev;
2004-04-24 19:16:58 +08:00
struct device_id *id;
};
2004-07-27 04:57:01 +08:00
static int switch_hidproxy(struct device_info *devinfo)
2004-04-24 19:16:58 +08:00
{
2004-07-27 04:57:01 +08:00
struct usb_dev_handle *udev;
int err;
2004-04-24 19:16:58 +08:00
2004-07-27 04:57:01 +08:00
udev = usb_open(devinfo->dev);
if (!udev)
return -errno;
2004-04-24 19:16:58 +08:00
2004-07-27 04:57:01 +08:00
err = usb_control_msg(udev, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0, devinfo->id->mode, 0, NULL, 0, 10000);
2004-04-24 19:16:58 +08:00
if (err == 0) {
err = -1;
errno = EALREADY;
} else {
if (errno == ETIMEDOUT)
err = 0;
}
2004-07-27 04:57:01 +08:00
usb_close(udev);
2004-04-24 19:16:58 +08:00
return err;
}
2005-07-06 05:15:41 +08:00
static int send_report(int fd, const char *buf, size_t size)
2004-04-24 19:16:58 +08:00
{
struct hiddev_report_info rinfo;
struct hiddev_usage_ref uref;
int i, err;
for (i = 0; i < size; i++) {
memset(&uref, 0, sizeof(uref));
uref.report_type = HID_REPORT_TYPE_OUTPUT;
uref.report_id = 0x10;
uref.field_index = 0;
uref.usage_index = i;
uref.usage_code = 0xff000001;
uref.value = buf[i];
err = ioctl(fd, HIDIOCSUSAGE, &uref);
if (err < 0)
return err;
}
memset(&rinfo, 0, sizeof(rinfo));
rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
rinfo.report_id = 0x10;
rinfo.num_fields = 1;
err = ioctl(fd, HIDIOCSREPORT, &rinfo);
return err;
}
2004-07-27 04:57:01 +08:00
static int switch_logitech(struct device_info *devinfo)
2004-04-24 19:16:58 +08:00
{
char devname[PATH_MAX + 1];
int i, fd, err = -1;
for (i = 0; i < 16; i++) {
struct hiddev_devinfo dinfo;
char rep1[] = { 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 };
char rep2[] = { 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 };
char rep3[] = { 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 };
sprintf(devname, "%s/hiddev%d", devpath, i);
fd = open(devname, O_RDWR);
if (fd < 0) {
sprintf(devname, "%s/usb/hiddev%d", devpath, i);
fd = open(devname, O_RDWR);
if (fd < 0) {
sprintf(devname, "%s/usb/hid/hiddev%d", devpath, i);
fd = open(devname, O_RDWR);
if (fd < 0)
continue;
}
}
memset(&dinfo, 0, sizeof(dinfo));
err = ioctl(fd, HIDIOCGDEVINFO, &dinfo);
2004-07-27 04:57:01 +08:00
if (err < 0 || dinfo.busnum != atoi(devinfo->dev->bus->dirname) ||
dinfo.devnum != atoi(devinfo->dev->filename)) {
2004-04-24 19:16:58 +08:00
close(fd);
continue;
}
err = ioctl(fd, HIDIOCINITREPORT, 0);
if (err < 0) {
close(fd);
break;
}
err = send_report(fd, rep1, sizeof(rep1));
if (err < 0) {
close(fd);
break;
}
err = send_report(fd, rep2, sizeof(rep2));
if (err < 0) {
close(fd);
break;
}
err = send_report(fd, rep3, sizeof(rep3));
close(fd);
break;
}
return err;
}
static struct device_id device_list[] = {
{ HCI, 0x0a12, 0x1000, switch_hidproxy },
{ HID, 0x0a12, 0x0001, switch_hidproxy },
2004-11-29 23:02:25 +08:00
{ HCI, 0x0458, 0x1000, switch_hidproxy },
{ HID, 0x0458, 0x003f, switch_hidproxy },
2004-04-24 19:16:58 +08:00
{ HCI, 0x05ac, 0x1000, switch_hidproxy },
{ HID, 0x05ac, 0x8203, switch_hidproxy },
2006-04-13 06:03:51 +08:00
{ HID, 0x05ac, 0x8204, switch_hidproxy }, /* Apple Mac mini */
2004-04-24 19:16:58 +08:00
{ HCI, 0x046d, 0xc703, switch_logitech },
{ HCI, 0x046d, 0xc704, switch_logitech },
{ HCI, 0x046d, 0xc705, switch_logitech },
{ HCI, 0x046d, 0x0b02, switch_logitech }, /* Logitech diNovo Media Desktop Laser */
2004-04-24 19:16:58 +08:00
{ -1 }
};
static struct device_id *match_device(int mode, uint16_t vendor, uint16_t product)
{
int i;
for (i = 0; device_list[i].mode >= 0; i++) {
if (mode != device_list[i].mode)
continue;
if (vendor == device_list[i].vendor &&
product == device_list[i].product)
return &device_list[i];
}
return NULL;
}
2004-07-27 04:57:01 +08:00
static int find_devices(int mode, struct device_info *devinfo, size_t size)
2004-04-24 19:16:58 +08:00
{
2004-07-27 04:57:01 +08:00
struct usb_bus *bus;
struct usb_device *dev;
2004-04-24 19:16:58 +08:00
struct device_id *id;
2004-07-27 04:57:01 +08:00
int count = 0;
2004-04-24 19:16:58 +08:00
2004-07-27 04:57:01 +08:00
usb_find_busses();
usb_find_devices();
2004-04-24 19:16:58 +08:00
2004-07-27 04:57:01 +08:00
for (bus = usb_get_busses(); bus; bus = bus->next)
for (dev = bus->devices; dev; dev = dev->next) {
id = match_device(mode, dev->descriptor.idVendor,
dev->descriptor.idProduct);
2004-04-24 19:16:58 +08:00
if (!id)
continue;
if (count < size) {
2004-07-27 04:57:01 +08:00
devinfo[count].dev = dev;
devinfo[count].id = id;
2004-04-24 19:16:58 +08:00
count++;
}
}
return count;
}
static void usage(void)
{
printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
printf("Usage:\n"
"\thid2hci [options]\n"
"\n");
printf("Options:\n"
"\t-h, --help Display help\n"
2004-07-27 04:57:01 +08:00
"\t-q, --quiet Don't display any messages\n"
2004-04-24 19:16:58 +08:00
"\t-0, --tohci Switch to HCI mode (default)\n"
"\t-1, --tohid Switch to HID mode\n"
"\n");
}
static struct option main_options[] = {
{ "help", 0, 0, 'h' },
2004-07-27 04:57:01 +08:00
{ "quiet", 0, 0, 'q' },
2004-04-24 19:16:58 +08:00
{ "tohci", 0, 0, '0' },
{ "tohid", 0, 0, '1' },
{ 0, 0, 0, 0 }
};
int main(int argc, char *argv[])
{
struct device_info dev[16];
2004-07-27 04:57:01 +08:00
int i, opt, num, quiet = 0, mode = HCI;
2004-04-24 19:16:58 +08:00
2004-07-27 04:57:01 +08:00
while ((opt = getopt_long(argc, argv, "+01qh", main_options, NULL)) != -1) {
2004-04-24 19:16:58 +08:00
switch (opt) {
case '0':
mode = HCI;
break;
case '1':
mode = HID;
break;
2004-07-27 04:57:01 +08:00
case 'q':
quiet = 1;
break;
2004-04-24 19:16:58 +08:00
case 'h':
usage();
exit(0);
default:
exit(0);
}
}
argc -= optind;
argv += optind;
optind = 0;
2004-07-27 04:57:01 +08:00
usb_init();
2004-04-24 19:16:58 +08:00
num = find_devices(mode, dev, sizeof(dev) / sizeof(dev[0]));
if (num <= 0) {
2004-07-27 04:57:01 +08:00
if (!quiet)
fprintf(stderr, "No devices in %s mode found\n",
mode ? "HID" : "HCI");
2004-04-24 19:16:58 +08:00
exit(1);
}
for (i = 0; i < num; i++) {
struct device_id *id = dev[i].id;
2004-07-27 04:57:01 +08:00
if (!quiet)
printf("Switching device %04x:%04x to %s mode ",
id->vendor, id->product, mode ? "HID" : "HCI");
2004-04-24 19:16:58 +08:00
fflush(stdout);
2004-07-27 04:57:01 +08:00
if (id->func(&dev[i]) < 0) {
if (!quiet)
printf("failed (%s)\n", strerror(errno));
} else {
if (!quiet)
printf("was successful\n");
}
2004-04-24 19:16:58 +08:00
}
return 0;
}