Release tarball for barry-0.6
[barry.git] / tools / bcharge.cc
blob16caddf1cd1b2e0af97c952d921511954158eefa
1 ///
2 /// \file bcharge.cc
3 /// Talk to the Blackberry just enough to change the Max Power
4 /// to 500mA. Cycles through all devices attached to USB,
5 /// attempting to set all matching Blackberry devices to charge.
6 ///
7 /// This file is part of the Barry project:
8 ///
9 /// http://www.netdirect.ca/software/packages/barry/index.php
10 /// http://sourceforge.net/projects/barry
11 ///
12 /// Compile with the following command (needs libusb):
13 ///
14 /// g++ -o bcharge bcharge.cc -lusb
15 ///
18 Copyright (C) 2006-2007, Net Direct Inc. (http://www.netdirect.ca/)
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29 See the GNU General Public License in the COPYING file at the
30 root directory of this project for more details.
33 #include <usb.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
38 #define VENDOR_RIM 0x0fca
39 #define PRODUCT_RIM_BLACKBERRY 0x0001
40 #define PRODUCT_RIM_PEARL 0x0006
42 #define BLACKBERRY_INTERFACE 0
43 #define BLACKBERRY_CONFIGURATION 1
45 void charge(struct usb_device *dev, bool old_style_pearl)
47 usb_dev_handle *handle = usb_open(dev);
48 if( !handle )
49 return;
51 // the special sauce... these 3 steps seem to do the trick
52 // for the 7750 series... needs testing on others
53 char buffer[2];
54 usb_control_msg(handle, 0xc0, 0xa5, 0, 1, buffer, 2, 100);
55 usb_control_msg(handle, 0x40, 0xa2, 0, 1, buffer, 0, 100);
56 if( dev->descriptor.idProduct == PRODUCT_RIM_PEARL) {
57 if( old_style_pearl ) {
58 // use this for "old style" interface: product ID 0001
59 usb_control_msg(handle, 0xc0, 0xa9, 0, 1, buffer, 2, 100);
61 else {
62 // Product ID 0004
63 usb_control_msg(handle, 0xc0, 0xa9, 1, 1, buffer, 2, 100);
67 usb_set_configuration(handle, BLACKBERRY_CONFIGURATION);
69 // the Blackberry Pearl doesn't reset itself after the above,
70 // so do it ourselves
71 if( dev->descriptor.idProduct == PRODUCT_RIM_PEARL) {
72 usb_reset(handle);
75 // let it reset itself
76 sleep(3);
78 // cleanup
79 usb_close(handle);
82 int main(int argc, char *argv[])
84 struct usb_bus *busses;
87 // allow -o command line switch to choose which mode to use for
88 // Blackberry Pearls:
89 // Default: 0004
90 // With switch: 0001
92 bool old_style_pearl = (argc > 1 && strcmp(argv[1], "-o") == 0);
94 usb_init();
95 usb_find_busses();
96 usb_find_devices();
97 busses = usb_get_busses();
99 printf("Scanning for Blackberry devices...\n");
100 int found = 0;
102 struct usb_bus *bus;
103 for( bus = busses; bus; bus = bus->next ) {
104 struct usb_device *dev;
106 for (dev = bus->devices; dev; dev = dev->next) {
107 // Is this a blackberry?
108 if( dev->descriptor.idVendor == VENDOR_RIM &&
109 (dev->descriptor.idProduct == PRODUCT_RIM_BLACKBERRY ||
110 dev->descriptor.idProduct == PRODUCT_RIM_PEARL) ) {
111 printf("Found...");
112 if( dev->config &&
113 dev->descriptor.bNumConfigurations >= 1 &&
114 dev->config[0].MaxPower < 250 ) {
115 printf("attempting to adjust charge setting.\n");
116 charge(dev, old_style_pearl);
117 found++;
119 else {
120 printf("already at 500mA\n");
126 printf("%d device%s adjusted.\n", found, found > 1 ? "s" : "");