bcharge.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       bcharge.cc
00003 ///             Talk to the Blackberry just enough to change the Max Power
00004 ///             to 500mA.  Cycles through all devices attached to USB,
00005 ///             attempting to set all matching Blackberry devices to charge.
00006 ///
00007 ///             This file is part of the Barry project:
00008 ///
00009 ///             http://www.netdirect.ca/software/packages/barry/index.php
00010 ///             http://sourceforge.net/projects/barry
00011 ///
00012 ///             Compile with the following command (needs libusb):
00013 ///
00014 ///             g++ -o bcharge bcharge.cc -lusb
00015 ///
00016 
00017 /*
00018     Copyright (C) 2006-2009, Net Direct Inc. (http://www.netdirect.ca/)
00019 
00020     This program is free software; you can redistribute it and/or modify
00021     it under the terms of the GNU General Public License as published by
00022     the Free Software Foundation; either version 2 of the License, or
00023     (at your option) any later version.
00024 
00025     This program is distributed in the hope that it will be useful,
00026     but WITHOUT ANY WARRANTY; without even the implied warranty of
00027     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00028 
00029     See the GNU General Public License in the COPYING file at the
00030     root directory of this project for more details.
00031 */
00032 
00033 #include <usb.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <unistd.h>
00037 #include <errno.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include <fcntl.h>
00041 #include <string>
00042 #include <barry/common.h>
00043 
00044 bool old_style_pearl = false;
00045 bool force_dual = false;
00046 std::string udev_devpath;
00047 std::string sysfs_path = "/sys";
00048 
00049 void Usage()
00050 {
00051         printf(
00052         "bcharge - Adjust Blackberry charging modes\n"
00053         "          Copyright 2006-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
00054         "\n"
00055         "   -d          Dual mode (mode 0004) (default)\n"
00056         "   -o          Set a Pearl to old Blackberry mode (0001)\n"
00057         "\n"
00058         "   -h          This help message\n"
00059         "   -p devpath  The devpath argument from udev.  If specified, will attempt\n"
00060         "               to adjust USB suspend settings to keep the device charging.\n"
00061         "   -s path     The path where sysfs is mounted.  Defaults to '/sys'\n"
00062         "\n"
00063         );
00064 }
00065 
00066 void control(usb_dev_handle *dev, int requesttype, int request, int value,
00067         int index, char *bytes, int size, int timeout)
00068 {
00069         int result = usb_control_msg(dev, requesttype, request, value, index,
00070                 bytes, size, timeout);
00071         if( result < 0 ) {
00072                 printf("\nusb_control_msg failed: code: %d, %s\n", result,
00073                         usb_strerror());
00074         }
00075 }
00076 
00077 void charge(struct usb_dev_handle *handle)
00078 {
00079         // the special sauce... these steps seem to do the trick
00080         // for the 7750 series... needs testing on others
00081         char buffer[2];
00082         control(handle, 0xc0, 0xa5, 0, 1, buffer, 2, 100);
00083         control(handle, 0x40, 0xa2, 0, 1, buffer, 0, 100);
00084 }
00085 
00086 void pearl_mode(struct usb_dev_handle *handle)
00087 {
00088         char buffer[2];
00089         if( old_style_pearl ) {
00090                 // use this for "old style" interface: product ID 0001
00091                 control(handle, 0xc0, 0xa9, 0, 1, buffer, 2, 100);
00092         }
00093         else {
00094                 // Product ID 0004
00095                 control(handle, 0xc0, 0xa9, 1, 1, buffer, 2, 100);
00096         }
00097 }
00098 
00099 int find_mass_storage_interface(struct usb_dev_handle *handle)
00100 {
00101         // search the configuration descriptor for a Mass Storage
00102         // interface (class 8)
00103         struct usb_device *dev = usb_device(handle);
00104         struct usb_config_descriptor *cfg = dev ? dev->config : 0;
00105 
00106         if( cfg ) {
00107 
00108                 for( unsigned i = 0; cfg->interface && i < cfg->bNumInterfaces; i++ ) {
00109                         struct usb_interface *iface = &cfg->interface[i];
00110                         for( int a = 0; iface->altsetting && a < iface->num_altsetting; a++ ) {
00111                                 struct usb_interface_descriptor *id = &iface->altsetting[a];
00112                                 if( id->bInterfaceClass == USB_CLASS_MASS_STORAGE )
00113                                         return id->bInterfaceNumber;
00114                         }
00115                 }
00116         }
00117 
00118         // if we get here, then we didn't find the Mass Storage interface
00119         // ... this should never happen, but if it does, assume
00120         // the device is s showing product ID 0006, and the Mass Storage
00121         // interface is interface #0
00122         printf("Can't find Mass Storage interface, assuming 0.\n");
00123         return 0;
00124 }
00125 
00126 void driver_conflict(struct usb_dev_handle *handle)
00127 {
00128         // this is called if the first usb_set_configuration()
00129         // failed... this most probably means that usb_storage
00130         // has already claimed the Mass Storage interface,
00131         // in which case we politely tell it to away.
00132 
00133 #if LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
00134         printf("Detecting possible kernel driver conflict, trying to resolve...\n");
00135 
00136         int iface = find_mass_storage_interface(handle);
00137         if( usb_detach_kernel_driver_np(handle, iface) < 0 )
00138                 printf("usb_detach_kernel_driver_np() failed: %s\n", usb_strerror());
00139 
00140         if( usb_set_configuration(handle, BLACKBERRY_CONFIGURATION) < 0 )
00141                 printf("usb_set_configuration() failed: %s\n", usb_strerror());
00142 #endif
00143 }
00144 
00145 // returns true if device mode was modified, false otherwise
00146 bool process(struct usb_device *dev, bool is_pearl)
00147 {
00148         bool apply = false;
00149         printf("Found device #%s...", dev->filename);
00150 
00151         // open
00152         usb_dev_handle *handle = usb_open(dev);
00153         if( !handle ) {
00154                 printf("unable to open device\n");
00155                 return false;
00156         }
00157 
00158         // adjust power
00159         if( dev->config &&
00160             dev->descriptor.bNumConfigurations >= 1 &&
00161             dev->config[0].MaxPower < 250 ) {
00162                 printf("adjusting charge setting");
00163                 charge(handle);
00164                 apply = true;
00165         }
00166         else {
00167                 printf("already at 500mA");
00168         }
00169 
00170         // adjust Pearl mode
00171         if( is_pearl || force_dual ) {
00172                 int desired_mode = old_style_pearl
00173                         ? PRODUCT_RIM_BLACKBERRY : PRODUCT_RIM_PEARL_DUAL;
00174 
00175                 if( desired_mode != dev->descriptor.idProduct ) {
00176                         printf("...adjusting Pearl mode to %s",
00177                                 old_style_pearl ? "single" : "dual");
00178                         pearl_mode(handle);
00179                         apply = true;
00180                 }
00181                 else {
00182                         printf("...already in desired Pearl mode");
00183                 }
00184         }
00185         else {
00186                 printf("...no Pearl adjustment");
00187         }
00188 
00189         // apply changes
00190         if( apply ) {
00191                 if( usb_set_configuration(handle, BLACKBERRY_CONFIGURATION) < 0 )
00192                         driver_conflict(handle);
00193 
00194                 // the Blackberry Pearl doesn't reset itself after the above,
00195                 // so do it ourselves
00196                 if( is_pearl || force_dual ) {
00197                         //
00198                         // It has been observed that the 8830 behaves both like
00199                         // a Pearl device (in that it has mass storage +
00200                         // database modes) as well as a Classic device in
00201                         // that it resets itself and doesn't need an explicit
00202                         // reset call.
00203                         //
00204                         // In order to deal with this, we insert a brief sleep.
00205                         // If it is an 8830, it will reset itself and the
00206                         // following reset call will fail.  If it is a Pearl,
00207                         // the reset will work as normal.
00208                         //
00209                         sleep(1);
00210                         if( usb_reset(handle) < 0 ) {
00211                                 printf("\nusb_reset failed: %s\n", usb_strerror());
00212                         }
00213                 }
00214 
00215                 printf("...done\n");
00216         }
00217         else {
00218                 printf("...no change\n");
00219         }
00220 
00221         // cleanup
00222         usb_close(handle);
00223         return apply;
00224 }
00225 
00226 bool power_write(const std::string &file, const std::string &value)
00227 {
00228         // attempt to open the state file
00229         int fd = open(file.c_str(), O_RDWR);
00230         if( fd == -1 ) {
00231                 printf("autosuspend adjustment failure: (file: %s): %s\n",
00232                         file.c_str(),
00233                         strerror(errno));
00234                 return false;
00235         }
00236 
00237         int written = write(fd, value.data(), value.size());
00238         int error = errno;
00239         close(fd);
00240 
00241         if( written < 0 || (size_t)written != value.size() ) {
00242                 printf("autosuspend adjustment failure (write): (file: %s): %s\n",
00243                         file.c_str(),
00244                         strerror(error));
00245                 return false;
00246         }
00247 
00248         printf("autosuspend adjustment: wrote %s to %s\n",
00249                 value.c_str(), file.c_str());
00250         return true;
00251 }
00252 
00253 //
00254 // Checks for USB suspend, and enables the device if suspended.
00255 //
00256 // Kernel 2.6.21 behaves with autosuspend=0 meaning off, while 2.6.22
00257 // and higher needs autosuspend=-1 to turn it off.  In 2.6.22, a value
00258 // of 0 means "immediate" instead of "never".
00259 //
00260 // Version 2.6.22 adds variables internal to the system called
00261 // autosuspend_disabled and autoresume_disabled.  These are controlled by the
00262 // /sys/class/usb_device/*/device/power/level file.  (See below)
00263 //
00264 // Here's a summary of files under device/power.  These may or may not exist
00265 // depending on your kernel version and configuration.
00266 //
00267 //
00268 //        autosuspend
00269 //                -1 or 0 means off, depending on kernel,
00270 //                otherwise it is the number of seconds to
00271 //                autosuspend
00272 //
00273 //        level
00274 //                with the settings:
00275 //
00276 //                on      - suspend is disabled, device is fully powered
00277 //                auto    - suspend is controlled by the kernel (default)
00278 //                suspend - suspend is enabled permanently
00279 //
00280 //                You can write these strings to the file to control
00281 //                behaviour on a per-device basis.
00282 //
00283 //                echo on > /sys/usb_device/.../device/power/level
00284 //
00285 //        state
00286 //                current state of device
00287 //                0 - fully powered
00288 //                2 - suspended
00289 //
00290 //                You can write these numbers to control behaviour, but
00291 //                any change you make here might change automatically
00292 //                if autosuspend is on.
00293 //
00294 //                echo -n 0 > /sys/usb_device/.../device/power/state
00295 //
00296 //        wakeup
00297 //                unknown
00298 //
00299 //
00300 // Given the above facts, use the following heuristics to try to disable
00301 // autosuspend for the Blackberry:
00302 //
00303 //      - if level exists, write "on" to it
00304 //      - if autosuspend exists, write -1 to it
00305 //              - if error, write 0 to it
00306 //      - if neither of the above work, and state exists, write 0 to it
00307 //
00308 void resume()
00309 {
00310         if( udev_devpath.size() == 0 )
00311                 return; // nothing to do
00312 
00313         // let sysfs settle a bit
00314         sleep(5);
00315 
00316         std::string power_path = sysfs_path + "/" + udev_devpath + "/device/power/";
00317 
00318         if( !power_write(power_path + "level", "on\n") )
00319                 if( !power_write(power_path + "autosuspend", "-1\n") )
00320                         if( !power_write(power_path + "autosuspend", "0\n") )
00321                                 power_write(power_path + "state", "0");
00322 }
00323 
00324 int main(int argc, char *argv[])
00325 {
00326         struct usb_bus *busses;
00327 
00328         //
00329         // allow -o command line switch to choose which mode to use for
00330         // Blackberry Pearls:
00331         //      Dual(default):  0004    -d
00332         //      With switch:    0001    -o
00333         //
00334 
00335         // process command line options
00336         for(;;) {
00337                 int cmd = getopt(argc, argv, "dop:s:h");
00338                 if( cmd == -1 )
00339                         break;
00340 
00341                 switch( cmd )
00342                 {
00343                 case 'd':       // Dual (default)
00344                         force_dual = true;
00345                         old_style_pearl = false;
00346                         break;
00347 
00348                 case 'o':       // Old style pearl
00349                         force_dual = false;
00350                         old_style_pearl = true;
00351                         break;
00352 
00353                 case 'p':       // udev devpath
00354                         udev_devpath = optarg;
00355                         break;
00356 
00357                 case 's':       // where sysfs is mounted
00358                         sysfs_path = optarg;
00359                         break;
00360 
00361                 case 'h':       // help!
00362                 default:
00363                         Usage();
00364                         return 0;
00365                 }
00366         }
00367 
00368         usb_init();
00369         if( usb_find_busses() < 0 || usb_find_devices() < 0 ) {
00370                 printf("\nUnable to scan devices: %s\n", usb_strerror());
00371                 return 1;
00372         }
00373         busses = usb_get_busses();
00374 
00375         printf("Scanning for Blackberry devices...\n");
00376 
00377         struct usb_bus *bus;
00378         for( bus = busses; bus; bus = bus->next ) {
00379                 struct usb_device *dev;
00380 
00381                 for (dev = bus->devices; dev; dev = dev->next) {
00382                         // Is this a blackberry?
00383                         if( dev->descriptor.idVendor == VENDOR_RIM ) {
00384                                 switch(dev->descriptor.idProduct)
00385                                 {
00386                                 case PRODUCT_RIM_BLACKBERRY:
00387                                         if( !process(dev, false) )
00388                                                 resume();
00389                                         break;
00390 
00391                                 case PRODUCT_RIM_PEARL_DUAL:
00392                                 case PRODUCT_RIM_PEARL:
00393                                 case PRODUCT_RIM_PEARL_8120:
00394                                 case PRODUCT_RIM_PEARL_FLIP:
00395                                 case PRODUCT_RIM_STORM:
00396                                         if( !process(dev, true) )
00397                                                 resume();
00398                                         break;
00399                                 }
00400                         }
00401                 }
00402         }
00403 }
00404 

Generated on Tue Jun 30 16:08:13 2009 for Barry by  doxygen 1.5.8