| 1 | /*
|
|---|
| 2 | Mode switcher for Option Icon 3G+ USB box
|
|---|
| 3 |
|
|---|
| 4 | Copyright (C) 2006 Josua Dietze (digidietze nospam t-online spamno de)
|
|---|
| 5 |
|
|---|
| 6 | Triggers the switching of the box from storage device mode
|
|---|
| 7 | to modem (serial) device mode.
|
|---|
| 8 |
|
|---|
| 9 | Created with help from usbsnoop2libusb.pl (http://iki.fi/lindi/usb/usbsnoop2libusb.pl)
|
|---|
| 10 |
|
|---|
| 11 | Version 0.2, 2006/09/25
|
|---|
| 12 | Code cleaning, more messages
|
|---|
| 13 | Version 0.1, 2006/09/24
|
|---|
| 14 | Just very basic functionality ...
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | This program is free software; you can redistribute it and/or modify
|
|---|
| 18 | it under the terms of the GNU General Public License as published by
|
|---|
| 19 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 20 | (at your option) any later version.
|
|---|
| 21 |
|
|---|
| 22 | This program is distributed in the hope that it will be useful,
|
|---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 25 | GNU General Public License for more details:
|
|---|
| 26 |
|
|---|
| 27 | http://www.gnu.org/licenses/gpl.txt
|
|---|
| 28 |
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | #include <stdio.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <string.h>
|
|---|
| 34 | #include <assert.h>
|
|---|
| 35 | #include <signal.h>
|
|---|
| 36 | #include <ctype.h>
|
|---|
| 37 | #include <usb.h>
|
|---|
| 38 |
|
|---|
| 39 | struct usb_dev_handle *devh;
|
|---|
| 40 |
|
|---|
| 41 | void release_usb_device(int dummy) {
|
|---|
| 42 | int ret;
|
|---|
| 43 | printf("Program cancelled by system. Bye\n\n");
|
|---|
| 44 | ret = usb_release_interface(devh, 0);
|
|---|
| 45 | if (!ret)
|
|---|
| 46 | printf(" Oops, failed to release interface: %d\n", ret);
|
|---|
| 47 | usb_close(devh);
|
|---|
| 48 | if (!ret)
|
|---|
| 49 | printf(" Oops, failed to close interface: %d\n", ret);
|
|---|
| 50 | printf("\n");
|
|---|
| 51 | exit(1);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | struct usb_device *find_device(int vendor, int product) {
|
|---|
| 55 | struct usb_bus *bus;
|
|---|
| 56 | struct usb_device *right_dev;
|
|---|
| 57 | right_dev = NULL;
|
|---|
| 58 |
|
|---|
| 59 | printf("Looking for Option Icon USB 3G+ box ...\n");
|
|---|
| 60 | for (bus = usb_get_busses(); bus; bus = bus->next) {
|
|---|
| 61 | struct usb_device *dev;
|
|---|
| 62 |
|
|---|
| 63 | for (dev = bus->devices; dev; dev = dev->next) {
|
|---|
| 64 | if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
|
|---|
| 65 | right_dev = dev;
|
|---|
| 66 | }
|
|---|
| 67 | if (dev->descriptor.idVendor == 0x0af0 && dev->descriptor.idProduct == 0x6600) {
|
|---|
| 68 | printf("Found box in modem mode. Switching not necessary. Bye\n\n");
|
|---|
| 69 | exit(0);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | if (right_dev != NULL)
|
|---|
| 74 | printf("Found box in storage mode. Preparing for switching ...\n");
|
|---|
| 75 | else {
|
|---|
| 76 | printf("No Option Icon box found. Is it connected? Bye\n\n");
|
|---|
| 77 | exit(0);
|
|---|
| 78 | }
|
|---|
| 79 | return right_dev;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | int main(int argc, char **argv) {
|
|---|
| 83 | int ret;
|
|---|
| 84 | int vendor = 0x05c6;
|
|---|
| 85 | int product = 0x1000;
|
|---|
| 86 |
|
|---|
| 87 | struct usb_device *dev;
|
|---|
| 88 | char buf[65535];
|
|---|
| 89 |
|
|---|
| 90 | printf("\n * icon_switch: tool for changing USB mode of Option Icon 3G+ box\n");
|
|---|
| 91 | printf(" * (C) Josua Dietze 2006\n");
|
|---|
| 92 | printf(" * Works with libusb 0.1.12 and probably other versions\n\n");
|
|---|
| 93 |
|
|---|
| 94 | usb_init();
|
|---|
| 95 | usb_find_busses();
|
|---|
| 96 | usb_find_devices();
|
|---|
| 97 |
|
|---|
| 98 | dev = find_device(vendor, product);
|
|---|
| 99 | assert(dev);
|
|---|
| 100 |
|
|---|
| 101 | devh = usb_open(dev);
|
|---|
| 102 | assert(devh);
|
|---|
| 103 |
|
|---|
| 104 | signal(SIGTERM, release_usb_device);
|
|---|
| 105 |
|
|---|
| 106 | printf("Looking for active storage driver ...\n");
|
|---|
| 107 | ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
|
|---|
| 108 | if (ret == 0) {
|
|---|
| 109 | printf(" OK, driver found (\"%s\"), attempting to detach it ...\n", buf);
|
|---|
| 110 | ret = usb_detach_kernel_driver_np(devh, 0);
|
|---|
| 111 | if (ret == 0)
|
|---|
| 112 | printf(" OK, driver \"%s\" successfully detached\n", buf);
|
|---|
| 113 | else
|
|---|
| 114 | printf(" Oops, driver \"%s\" detach failed with error %d. Trying to continue ...\n", buf, ret);
|
|---|
| 115 | } else {
|
|---|
| 116 | printf("No driver found. Box was not initialized. Can't communicate. Bye\n\n");
|
|---|
| 117 | exit(1);
|
|---|
| 118 | }
|
|---|
| 119 | ret = usb_claim_interface(devh, 0);
|
|---|
| 120 | if (ret != 0) {
|
|---|
| 121 | printf("Could not claim interface (error %d). Can't communicate. Bye\n\n", ret);
|
|---|
| 122 | exit(1);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | ret = usb_set_altinterface(devh, 0);
|
|---|
| 126 | assert(ret >= 0);
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | memcpy(buf, "\x55\x53\x42\x43\x70\x6e\xde\x86\x00\x00\x00\x00\x00\x00\x06\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x000001f);
|
|---|
| 130 | ret = usb_bulk_write(devh, 0x00000005, buf, 0x000001f, 1000);
|
|---|
| 131 | if (ret >= 0 )
|
|---|
| 132 | printf("Device change command successfully sent. Box probably switched.\nLook at /var/log/syslog for result ... Bye\n\n");
|
|---|
| 133 | else
|
|---|
| 134 | printf("Device change command returned error %d", ret);
|
|---|
| 135 |
|
|---|
| 136 | ret = usb_release_interface(devh, 0);
|
|---|
| 137 | assert(ret == 0);
|
|---|
| 138 |
|
|---|
| 139 | ret = usb_close(devh);
|
|---|
| 140 | assert(ret == 0);
|
|---|
| 141 |
|
|---|
| 142 | return 0;
|
|---|
| 143 | }
|
|---|