f7679a59dee2480a060c9d5c2087538ce83fc598
[openwrt/svn-archive/archive.git] / utils / huaweiaktbbo / src / huaweiAktBbo.c
1 /* HUAWEI E220 3G HSDPA modem - Aktivator modemu = aktivuje ttyUSB0 tty USB1
2 bobovsky 11.12.2006
3 dalej sa uz pouzije usbserial a usb-storage
4 cc huaweiAktBbo.c -lusb (resp -I. -L.)
5 armeb-linux-gcc huaweiAktBbo.c -L. -I. -lusb
6 Copyright (C) 2006 bobovsky bobovsky@kanoistika.sk GPL
7 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License2.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <signal.h>
14 #include <ctype.h>
15 #include <usb.h>
16 #if 0
17 #include <linux/usbdevice_fs.h>
18 #define LIBUSB_AUGMENT
19 #include "libusb_augment.h"
20 #endif
21
22 struct usb_dev_handle *devh;
23
24 void release_usb_device(int dummy) {
25 int ret;
26 ret = usb_release_interface(devh, 0);
27 if (!ret)
28 printf("failed to release interface: %d\n", ret);
29 usb_close(devh);
30 if (!ret)
31 printf("failed to close interface: %d\n", ret);
32 exit(1);
33 }
34
35 void list_devices() {
36 struct usb_bus *bus;
37 for (bus = usb_get_busses(); bus; bus = bus->next) {
38 struct usb_device *dev;
39
40 for (dev = bus->devices; dev; dev = dev->next)
41 printf("0x%04x 0x%04x\n",
42 dev->descriptor.idVendor,
43 dev->descriptor.idProduct);
44 }
45 }
46
47 struct usb_device *find_device(int vendor, int product) {
48 struct usb_bus *bus;
49
50 for (bus = usb_get_busses(); bus; bus = bus->next) {
51 struct usb_device *dev;
52
53 for (dev = bus->devices; dev; dev = dev->next) {
54 if (dev->descriptor.idVendor == vendor
55 && dev->descriptor.idProduct == product)
56 return dev;
57 }
58 }
59 return NULL;
60 }
61
62 void print_bytes(char *bytes, int len) {
63 int i;
64 if (len > 0) {
65 for (i=0; i<len; i++) {
66 printf("%02x ", (int)((unsigned char)bytes[i]));
67 }
68 printf("\"");
69 for (i=0; i<len; i++) {
70 printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
71 }
72 printf("\"");
73 }
74 }
75
76
77 int main(int argc, char **argv) {
78 int ret, vendor, product;
79 struct usb_device *dev;
80 char buf[65535], *endptr;
81 #if 0
82 usb_urb *isourb;
83 struct timeval isotv;
84 char isobuf[32768];
85 #endif
86
87 usb_init();
88 // usb_set_debug(255);
89 usb_find_busses();
90 usb_find_devices();
91 /*
92 if (argc!=3) {
93 printf("usage: %s vendorID productID\n", argv[0]);
94 printf("ID numbers of currently attached devices:\n");
95 list_devices();
96 exit(1);
97 }
98 vendor = strtol(argv[1], &endptr, 16);
99 if (*endptr != '\0') {
100 printf("invalid vendor id\n");
101 exit(1);
102 }
103 product = strtol(argv[2], &endptr, 16);
104 if (*endptr != '\0') {
105 printf("invalid product id\n");
106 exit(1);
107 }
108 */
109 printf("Hladam HUAWEI E220 a prepnem na modem - bbo 06\n");
110 vendor = 0x12d1;
111 product = 0x1003;
112 dev = find_device(vendor, product);
113 assert(dev);
114
115 devh = usb_open(dev);
116 assert(devh);
117
118 signal(SIGTERM, release_usb_device);
119
120 /*
121 ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
122 printf("usb_get_driver_np returned %d\n", ret);
123 if (ret == 0) {
124 printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
125 ret = usb_detach_kernel_driver_np(devh, 0);
126 printf("usb_detach_kernel_driver_np returned %d\n", ret);
127 }
128 ret = usb_claim_interface(devh, 0);
129 if (ret != 0) {
130 printf("claim failed with error %d\n", ret);
131 exit(1);
132 }
133
134 ret = usb_set_altinterface(devh, 0);
135 assert(ret >= 0);
136 */
137 // BBO typ 1 = DEVICE
138 ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
139 //printf("1 get descriptor returned %d, bytes: ", ret);
140 //print_bytes(buf, ret);
141 //printf("\n");
142 usleep(1*1000);
143 // BBO typ 2 = CONFIGURATION
144 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
145 //printf("2 get descriptor returned %d, bytes: ", ret);
146 //print_bytes(buf, ret);
147 //printf("\n");
148 usleep(1*1000);
149 // BBO typ 2 = CONFIGURATION
150 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000020);
151 //printf("3 get descriptor returned %d, bytes: ", ret);
152 //print_bytes(buf, ret);
153 //printf("\n");
154 usleep(1*1000);
155 /*
156 ret = usb_release_interface(devh, 0);
157 if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
158 ret = usb_set_configuration(devh, 0x0000001);
159 printf("4 set configuration returned %d\n", ret);
160 ret = usb_claim_interface(devh, 0);
161 if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
162 ret = usb_set_altinterface(devh, 0);
163 printf("4 set alternate setting returned %d\n", ret);
164 usleep(50*1000);
165 ret = usb_set_altinterface(devh, 0);
166 printf("5 set alternate setting returned %d\n", ret);
167 usleep(62*1000);
168 */
169 ret = usb_control_msg(devh, USB_TYPE_STANDARD + USB_RECIP_DEVICE, USB_REQ_SET_FEATURE, 00000001, 0, buf, 0, 1000);
170 printf("4 set feature request returned %d\n", ret);
171 /*
172 ret = usb_release_interface(devh, 0);
173 assert(ret == 0);
174 */
175 ret = usb_close(devh);
176 assert(ret == 0);
177 printf("Prepnute-OK, Mas ttyUSB0 ttyUSB1 (cez usbserial vendor=0x12d1 product=0x1003)\n");
178 printf("pozri /proc/bus/usb/devices\n");
179 return 0;
180 }