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.
7 /// This file is part of the Barry project:
9 /// http://www.netdirect.ca/software/packages/barry
10 /// http://sourceforge.net/projects/barry
12 /// Compile with the following command (needs libusb):
14 /// g++ -o bcharge bcharge.cc -lusb
18 Copyright (C) 2006-2010, 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.
38 #include <sys/types.h>
42 #include <barry/common.h>
47 PEARL_CLASSIC_MODE_0001
, // force classic mode
48 PEARL_DUAL_MODE_0004
, // force dual mode
49 CONDITIONAL_DUAL_MODE
, // set dual mode if no class 255
50 // interface is found (database iface)
53 std::string udev_devpath
;
54 std::string sysfs_path
= "/sys";
59 "bcharge - Adjust Blackberry charging modes\n"
60 " Copyright 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
62 " -d Set to dual mode (0004)\n"
63 " -o Set a Pearl to old Blackberry mode (0001)\n"
64 " -g Set dual mode only if database interface class 255\n"
67 " -h This help message\n"
68 " -p devpath The devpath argument from udev. If specified, will attempt\n"
69 " to adjust USB suspend settings to keep the device charging.\n"
70 " -s path The path where sysfs is mounted. Defaults to '/sys'\n"
75 void control(usb_dev_handle
*dev
, int requesttype
, int request
, int value
,
76 int index
, char *bytes
, int size
, int timeout
)
78 int result
= usb_control_msg(dev
, requesttype
, request
, value
, index
,
79 bytes
, size
, timeout
);
81 printf("\nusb_control_msg failed: code: %d, %s\n", result
,
86 void charge(struct usb_dev_handle
*handle
)
88 // the special sauce... these steps seem to do the trick
89 // for the 7750 series... needs testing on others
91 control(handle
, 0xc0, 0xa5, 0, 1, buffer
, 2, 100);
92 control(handle
, 0x40, 0xa2, 0, 1, buffer
, 0, 100);
95 void pearl_classic_mode(struct usb_dev_handle
*handle
)
98 // use this for "old style" interface: product ID 0001
99 control(handle
, 0xc0, 0xa9, 0, 1, buffer
, 2, 100);
102 void pearl_dual_mode(struct usb_dev_handle
*handle
)
106 control(handle
, 0xc0, 0xa9, 1, 1, buffer
, 2, 100);
109 int find_interface(struct usb_dev_handle
*handle
, int iface_class
)
111 // search the configuration descriptor for the given class ID
112 struct usb_device
*dev
= usb_device(handle
);
113 struct usb_config_descriptor
*cfg
= dev
? dev
->config
: 0;
117 for( unsigned i
= 0; cfg
->interface
&& i
< cfg
->bNumInterfaces
; i
++ ) {
118 struct usb_interface
*iface
= &cfg
->interface
[i
];
119 for( int a
= 0; iface
->altsetting
&& a
< iface
->num_altsetting
; a
++ ) {
120 struct usb_interface_descriptor
*id
= &iface
->altsetting
[a
];
121 if( id
->bInterfaceClass
== iface_class
)
122 return id
->bInterfaceNumber
;
130 int find_mass_storage_interface(struct usb_dev_handle
*handle
)
132 int iface
= find_interface(handle
, USB_CLASS_MASS_STORAGE
);
135 // if we get here, then we didn't find the Mass Storage
136 // interface ... this should never happen, but if it does,
137 // assume the device is showing product ID 0006, and the
138 // Mass Storage interface is interface #0
139 printf("Can't find Mass Storage interface, assuming 0.\n");
147 void driver_conflict(struct usb_dev_handle
*handle
)
149 // this is called if the first usb_set_configuration()
150 // failed... this most probably means that usb_storage
151 // has already claimed the Mass Storage interface,
152 // in which case we politely tell it to go away.
154 #if LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
155 printf("Detecting possible kernel driver conflict, trying to resolve...\n");
157 int iface
= find_mass_storage_interface(handle
);
158 if( usb_detach_kernel_driver_np(handle
, iface
) < 0 )
159 printf("usb_detach_kernel_driver_np() failed: %s\n", usb_strerror());
161 if( usb_set_configuration(handle
, BLACKBERRY_CONFIGURATION
) < 0 )
162 printf("usb_set_configuration() failed: %s\n", usb_strerror());
166 // returns true if device mode was modified, false otherwise
167 bool process(struct usb_device
*dev
, ModeType mode
)
170 printf("Found device #%s...", dev
->filename
);
173 usb_dev_handle
*handle
= usb_open(dev
);
175 printf("unable to open device\n");
181 dev
->descriptor
.bNumConfigurations
>= 1 &&
182 dev
->config
[0].MaxPower
< 250 ) {
183 printf("adjusting charge setting");
188 printf("already at 500mA");
196 printf("...no Pearl mode adjustment");
199 case PEARL_CLASSIC_MODE_0001
:
200 if( dev
->descriptor
.idProduct
!= PRODUCT_RIM_BLACKBERRY
) {
201 printf("...adjusting Pearl mode to single");
202 pearl_classic_mode(handle
);
206 printf("...already in classic/single mode");
210 case PEARL_DUAL_MODE_0004
:
211 if( dev
->descriptor
.idProduct
!= PRODUCT_RIM_PEARL_DUAL
) {
212 printf("...adjusting Pearl mode to dual");
213 pearl_dual_mode(handle
);
217 printf("...already in dual mode");
221 case CONDITIONAL_DUAL_MODE
:
222 if( find_interface(handle
, 255) == -1 ) {
223 printf("...no database iface found, setting dual mode");
224 pearl_dual_mode(handle
);
228 printf("...found database iface, no change");
233 printf("Bug: default case");
240 if( usb_set_configuration(handle
, BLACKBERRY_CONFIGURATION
) < 0 )
241 driver_conflict(handle
);
243 // the Blackberry Pearl doesn't reset itself after the above,
244 // so do it ourselves
245 if( mode
== PEARL_DUAL_MODE_0004
) {
247 // It has been observed that the 8830 behaves both like
248 // a Pearl device (in that it has mass storage +
249 // database modes) as well as a Classic device in
250 // that it resets itself and doesn't need an explicit
253 // In order to deal with this, we insert a brief sleep.
254 // If it is an 8830, it will reset itself and the
255 // following reset call will fail. If it is a Pearl,
256 // the reset will work as normal.
259 if( usb_reset(handle
) < 0 ) {
260 printf("\nusb_reset failed: %s\n", usb_strerror());
267 printf("...no change");
276 bool power_write(const std::string
&file
, const std::string
&value
)
278 // attempt to open the state file
279 int fd
= open(file
.c_str(), O_RDWR
);
281 printf("autosuspend adjustment failure: (file: %s): %s\n",
287 int written
= write(fd
, value
.data(), value
.size());
291 if( written
< 0 || (size_t)written
!= value
.size() ) {
292 printf("autosuspend adjustment failure (write): (file: %s): %s\n",
298 printf("autosuspend adjustment: wrote %s to %s\n",
299 value
.c_str(), file
.c_str());
304 // Checks for USB suspend, and enables the device if suspended.
306 // Kernel 2.6.21 behaves with autosuspend=0 meaning off, while 2.6.22
307 // and higher needs autosuspend=-1 to turn it off. In 2.6.22, a value
308 // of 0 means "immediate" instead of "never".
310 // Version 2.6.22 adds variables internal to the system called
311 // autosuspend_disabled and autoresume_disabled. These are controlled by the
312 // /sys/class/usb_device/*/device/power/level file. (See below)
314 // Here's a summary of files under device/power. These may or may not exist
315 // depending on your kernel version and configuration.
319 // -1 or 0 means off, depending on kernel,
320 // otherwise it is the number of seconds to
324 // with the settings:
326 // on - suspend is disabled, device is fully powered
327 // auto - suspend is controlled by the kernel (default)
328 // suspend - suspend is enabled permanently
330 // You can write these strings to the file to control
331 // behaviour on a per-device basis.
333 // echo on > /sys/usb_device/.../device/power/level
336 // current state of device
340 // You can write these numbers to control behaviour, but
341 // any change you make here might change automatically
342 // if autosuspend is on.
344 // echo -n 0 > /sys/usb_device/.../device/power/state
350 // Given the above facts, use the following heuristics to try to disable
351 // autosuspend for the Blackberry:
353 // - if level exists, write "on" to it
354 // - if autosuspend exists, write -1 to it
355 // - if error, write 0 to it
356 // - if neither of the above work, and state exists, write 0 to it
360 if( udev_devpath
.size() == 0 )
361 return; // nothing to do
363 // let sysfs settle a bit
366 std::string power_path
= sysfs_path
+ "/" + udev_devpath
+ "/device/power/";
368 if( !power_write(power_path
+ "level", "on\n") )
369 if( !power_write(power_path
+ "autosuspend", "-1\n") )
370 if( !power_write(power_path
+ "autosuspend", "0\n") )
371 power_write(power_path
+ "state", "0");
374 int main(int argc
, char *argv
[])
376 struct usb_bus
*busses
;
377 ModeType mode
= NO_CHANGE
;
382 // allow -o command line switch to choose which mode to use for
383 // Blackberry Pearls:
385 // With switch: 0001 -o
388 // process command line options
390 int cmd
= getopt(argc
, argv
, "dogp:s:h");
397 mode
= PEARL_DUAL_MODE_0004
;
400 case 'o': // Classic style pearl
401 mode
= PEARL_CLASSIC_MODE_0001
;
404 case 'g': // Guess whether dual is needed
405 mode
= CONDITIONAL_DUAL_MODE
;
408 case 'p': // udev devpath
409 udev_devpath
= optarg
;
412 case 's': // where sysfs is mounted
424 if( usb_find_busses() < 0 || usb_find_devices() < 0 ) {
425 printf("\nUnable to scan devices: %s\n", usb_strerror());
428 busses
= usb_get_busses();
430 printf("Scanning for Blackberry devices...\n");
433 for( bus
= busses
; bus
; bus
= bus
->next
) {
434 struct usb_device
*dev
;
436 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
437 // Is this a blackberry?
438 if( dev
->descriptor
.idVendor
== VENDOR_RIM
) {
439 if( !process(dev
, mode
) )