00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00080
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
00091 control(handle, 0xc0, 0xa9, 0, 1, buffer, 2, 100);
00092 }
00093 else {
00094
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
00102
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
00119
00120
00121
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
00129
00130
00131
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
00146 bool process(struct usb_device *dev, bool is_pearl)
00147 {
00148 bool apply = false;
00149 printf("Found device #%s...", dev->filename);
00150
00151
00152 usb_dev_handle *handle = usb_open(dev);
00153 if( !handle ) {
00154 printf("unable to open device\n");
00155 return false;
00156 }
00157
00158
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
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
00190 if( apply ) {
00191 if( usb_set_configuration(handle, BLACKBERRY_CONFIGURATION) < 0 )
00192 driver_conflict(handle);
00193
00194
00195
00196 if( is_pearl || force_dual ) {
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
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
00222 usb_close(handle);
00223 return apply;
00224 }
00225
00226 bool power_write(const std::string &file, const std::string &value)
00227 {
00228
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
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 void resume()
00309 {
00310 if( udev_devpath.size() == 0 )
00311 return;
00312
00313
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
00330
00331
00332
00333
00334
00335
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':
00344 force_dual = true;
00345 old_style_pearl = false;
00346 break;
00347
00348 case 'o':
00349 force_dual = false;
00350 old_style_pearl = true;
00351 break;
00352
00353 case 'p':
00354 udev_devpath = optarg;
00355 break;
00356
00357 case 's':
00358 sysfs_path = optarg;
00359 break;
00360
00361 case 'h':
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
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