Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 2aa62c5bbb658df1fece777472a7bcf2 > files > 82

barry-devel-docs-0.17-0.3.20100730git.fc14.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Barry: bcharge.cc Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2-20100208 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="dir_cce6b820c61664c85c51a5136106e856.html">tools</a>
  </div>
</div>
<div class="contents">
<h1>bcharge.cc</h1><a href="bcharge_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">///</span>
<a name="l00002"></a>00002 <span class="comment"></span><span class="comment">/// \file       bcharge.cc</span>
<a name="l00003"></a>00003 <span class="comment"></span><span class="comment">///             Talk to the Blackberry just enough to change the Max Power</span>
<a name="l00004"></a>00004 <span class="comment"></span><span class="comment">///             to 500mA.  Cycles through all devices attached to USB,</span>
<a name="l00005"></a>00005 <span class="comment"></span><span class="comment">///             attempting to set all matching Blackberry devices to charge.</span>
<a name="l00006"></a>00006 <span class="comment"></span><span class="comment">///</span>
<a name="l00007"></a>00007 <span class="comment"></span><span class="comment">///             This file is part of the Barry project:</span>
<a name="l00008"></a>00008 <span class="comment"></span><span class="comment">///</span>
<a name="l00009"></a>00009 <span class="comment"></span><span class="comment">///             http://www.netdirect.ca/software/packages/barry</span>
<a name="l00010"></a>00010 <span class="comment"></span><span class="comment">///             http://sourceforge.net/projects/barry</span>
<a name="l00011"></a>00011 <span class="comment"></span><span class="comment">///</span>
<a name="l00012"></a>00012 <span class="comment"></span><span class="comment">///             Compile with the following command (needs libusb):</span>
<a name="l00013"></a>00013 <span class="comment"></span><span class="comment">///</span>
<a name="l00014"></a>00014 <span class="comment"></span><span class="comment">///             g++ -o bcharge bcharge.cc -lusb</span>
<a name="l00015"></a>00015 <span class="comment"></span><span class="comment">///</span>
<a name="l00016"></a>00016 <span class="comment"></span>
<a name="l00017"></a>00017 <span class="comment">/*</span>
<a name="l00018"></a>00018 <span class="comment">    Copyright (C) 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)</span>
<a name="l00019"></a>00019 <span class="comment"></span>
<a name="l00020"></a>00020 <span class="comment">    This program is free software; you can redistribute it and/or modify</span>
<a name="l00021"></a>00021 <span class="comment">    it under the terms of the GNU General Public License as published by</span>
<a name="l00022"></a>00022 <span class="comment">    the Free Software Foundation; either version 2 of the License, or</span>
<a name="l00023"></a>00023 <span class="comment">    (at your option) any later version.</span>
<a name="l00024"></a>00024 <span class="comment"></span>
<a name="l00025"></a>00025 <span class="comment">    This program is distributed in the hope that it will be useful,</span>
<a name="l00026"></a>00026 <span class="comment">    but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00027"></a>00027 <span class="comment">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</span>
<a name="l00028"></a>00028 <span class="comment"></span>
<a name="l00029"></a>00029 <span class="comment">    See the GNU General Public License in the COPYING file at the</span>
<a name="l00030"></a>00030 <span class="comment">    root directory of this project for more details.</span>
<a name="l00031"></a>00031 <span class="comment">*/</span>
<a name="l00032"></a>00032 
<a name="l00033"></a>00033 <span class="preprocessor">#include &lt;usb.h&gt;</span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00036"></a>00036 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00037"></a>00037 <span class="preprocessor">#include &lt;errno.h&gt;</span>
<a name="l00038"></a>00038 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;barry/common.h&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &quot;<a class="code" href="i18n_8h.html" title="Common internationalization defines, via gettext.">i18n.h</a>&quot;</span>
<a name="l00044"></a>00044 
<a name="l00045"></a>00045 <span class="keywordtype">bool</span> old_style_pearl = <span class="keyword">false</span>;
<a name="l00046"></a>00046 <span class="keywordtype">bool</span> force_dual = <span class="keyword">false</span>;
<a name="l00047"></a>00047 std::string udev_devpath;
<a name="l00048"></a>00048 std::string sysfs_path = <span class="stringliteral">&quot;/sys&quot;</span>;
<a name="l00049"></a>00049 
<a name="l00050"></a>00050 <span class="keywordtype">void</span> Usage()
<a name="l00051"></a>00051 {
<a name="l00052"></a>00052         printf(
<a name="l00053"></a>00053         <span class="stringliteral">&quot;bcharge - Adjust Blackberry charging modes\n&quot;</span>
<a name="l00054"></a>00054         <span class="stringliteral">&quot;          Copyright 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)\n&quot;</span>
<a name="l00055"></a>00055         <span class="stringliteral">&quot;\n&quot;</span>
<a name="l00056"></a>00056         <span class="stringliteral">&quot;   -d          Dual mode (mode 0004) (default)\n&quot;</span>
<a name="l00057"></a>00057         <span class="stringliteral">&quot;   -o          Set a Pearl to old Blackberry mode (0001)\n&quot;</span>
<a name="l00058"></a>00058         <span class="stringliteral">&quot;\n&quot;</span>
<a name="l00059"></a>00059         <span class="stringliteral">&quot;   -h          This help message\n&quot;</span>
<a name="l00060"></a>00060         <span class="stringliteral">&quot;   -p devpath  The devpath argument from udev.  If specified, will attempt\n&quot;</span>
<a name="l00061"></a>00061         <span class="stringliteral">&quot;               to adjust USB suspend settings to keep the device charging.\n&quot;</span>
<a name="l00062"></a>00062         <span class="stringliteral">&quot;   -s path     The path where sysfs is mounted.  Defaults to &#39;/sys&#39;\n&quot;</span>
<a name="l00063"></a>00063         <span class="stringliteral">&quot;\n&quot;</span>
<a name="l00064"></a>00064         );
<a name="l00065"></a>00065 }
<a name="l00066"></a>00066 
<a name="l00067"></a>00067 <span class="keywordtype">void</span> control(usb_dev_handle *dev, <span class="keywordtype">int</span> requesttype, <span class="keywordtype">int</span> request, <span class="keywordtype">int</span> value,
<a name="l00068"></a>00068         <span class="keywordtype">int</span> index, <span class="keywordtype">char</span> *bytes, <span class="keywordtype">int</span> size, <span class="keywordtype">int</span> timeout)
<a name="l00069"></a>00069 {
<a name="l00070"></a>00070         <span class="keywordtype">int</span> result = usb_control_msg(dev, requesttype, request, value, index,
<a name="l00071"></a>00071                 bytes, size, timeout);
<a name="l00072"></a>00072         <span class="keywordflow">if</span>( result &lt; 0 ) {
<a name="l00073"></a>00073                 printf(<span class="stringliteral">&quot;\nusb_control_msg failed: code: %d, %s\n&quot;</span>, result,
<a name="l00074"></a>00074                         usb_strerror());
<a name="l00075"></a>00075         }
<a name="l00076"></a>00076 }
<a name="l00077"></a>00077 
<a name="l00078"></a>00078 <span class="keywordtype">void</span> charge(<span class="keyword">struct</span> usb_dev_handle *handle)
<a name="l00079"></a>00079 {
<a name="l00080"></a>00080         <span class="comment">// the special sauce... these steps seem to do the trick</span>
<a name="l00081"></a>00081         <span class="comment">// for the 7750 series... needs testing on others</span>
<a name="l00082"></a>00082         <span class="keywordtype">char</span> buffer[2];
<a name="l00083"></a>00083         control(handle, 0xc0, 0xa5, 0, 1, buffer, 2, 100);
<a name="l00084"></a>00084         control(handle, 0x40, 0xa2, 0, 1, buffer, 0, 100);
<a name="l00085"></a>00085 }
<a name="l00086"></a>00086 
<a name="l00087"></a>00087 <span class="keywordtype">void</span> pearl_mode(<span class="keyword">struct</span> usb_dev_handle *handle)
<a name="l00088"></a>00088 {
<a name="l00089"></a>00089         <span class="keywordtype">char</span> buffer[2];
<a name="l00090"></a>00090         <span class="keywordflow">if</span>( old_style_pearl ) {
<a name="l00091"></a>00091                 <span class="comment">// use this for &quot;old style&quot; interface: product ID 0001</span>
<a name="l00092"></a>00092                 control(handle, 0xc0, 0xa9, 0, 1, buffer, 2, 100);
<a name="l00093"></a>00093         }
<a name="l00094"></a>00094         <span class="keywordflow">else</span> {
<a name="l00095"></a>00095                 <span class="comment">// Product ID 0004</span>
<a name="l00096"></a>00096                 control(handle, 0xc0, 0xa9, 1, 1, buffer, 2, 100);
<a name="l00097"></a>00097         }
<a name="l00098"></a>00098 }
<a name="l00099"></a>00099 
<a name="l00100"></a>00100 <span class="keywordtype">int</span> find_mass_storage_interface(<span class="keyword">struct</span> usb_dev_handle *handle)
<a name="l00101"></a>00101 {
<a name="l00102"></a>00102         <span class="comment">// search the configuration descriptor for a Mass Storage</span>
<a name="l00103"></a>00103         <span class="comment">// interface (class 8)</span>
<a name="l00104"></a>00104         <span class="keyword">struct </span>usb_device *dev = usb_device(handle);
<a name="l00105"></a>00105         <span class="keyword">struct </span>usb_config_descriptor *cfg = dev ? dev-&gt;config : 0;
<a name="l00106"></a>00106 
<a name="l00107"></a>00107         <span class="keywordflow">if</span>( cfg ) {
<a name="l00108"></a>00108 
<a name="l00109"></a>00109                 <span class="keywordflow">for</span>( <span class="keywordtype">unsigned</span> i = 0; cfg-&gt;interface &amp;&amp; i &lt; cfg-&gt;bNumInterfaces; i++ ) {
<a name="l00110"></a>00110                         <span class="keyword">struct </span>usb_interface *iface = &amp;cfg-&gt;interface[i];
<a name="l00111"></a>00111                         <span class="keywordflow">for</span>( <span class="keywordtype">int</span> a = 0; iface-&gt;altsetting &amp;&amp; a &lt; iface-&gt;num_altsetting; a++ ) {
<a name="l00112"></a>00112                                 <span class="keyword">struct </span>usb_interface_descriptor *<span class="keywordtype">id</span> = &amp;iface-&gt;altsetting[a];
<a name="l00113"></a>00113                                 <span class="keywordflow">if</span>( id-&gt;bInterfaceClass == USB_CLASS_MASS_STORAGE )
<a name="l00114"></a>00114                                         <span class="keywordflow">return</span> <span class="keywordtype">id</span>-&gt;bInterfaceNumber;
<a name="l00115"></a>00115                         }
<a name="l00116"></a>00116                 }
<a name="l00117"></a>00117         }
<a name="l00118"></a>00118 
<a name="l00119"></a>00119         <span class="comment">// if we get here, then we didn&#39;t find the Mass Storage interface</span>
<a name="l00120"></a>00120         <span class="comment">// ... this should never happen, but if it does, assume</span>
<a name="l00121"></a>00121         <span class="comment">// the device is s showing product ID 0006, and the Mass Storage</span>
<a name="l00122"></a>00122         <span class="comment">// interface is interface #0</span>
<a name="l00123"></a>00123         printf(<span class="stringliteral">&quot;Can&#39;t find Mass Storage interface, assuming 0.\n&quot;</span>);
<a name="l00124"></a>00124         <span class="keywordflow">return</span> 0;
<a name="l00125"></a>00125 }
<a name="l00126"></a>00126 
<a name="l00127"></a>00127 <span class="keywordtype">void</span> driver_conflict(<span class="keyword">struct</span> usb_dev_handle *handle)
<a name="l00128"></a>00128 {
<a name="l00129"></a>00129         <span class="comment">// this is called if the first usb_set_configuration()</span>
<a name="l00130"></a>00130         <span class="comment">// failed... this most probably means that usb_storage</span>
<a name="l00131"></a>00131         <span class="comment">// has already claimed the Mass Storage interface,</span>
<a name="l00132"></a>00132         <span class="comment">// in which case we politely tell it to away.</span>
<a name="l00133"></a>00133 
<a name="l00134"></a>00134 <span class="preprocessor">#if LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP</span>
<a name="l00135"></a>00135 <span class="preprocessor"></span>        printf(<span class="stringliteral">&quot;Detecting possible kernel driver conflict, trying to resolve...\n&quot;</span>);
<a name="l00136"></a>00136 
<a name="l00137"></a>00137         <span class="keywordtype">int</span> iface = find_mass_storage_interface(handle);
<a name="l00138"></a>00138         <span class="keywordflow">if</span>( usb_detach_kernel_driver_np(handle, iface) &lt; 0 )
<a name="l00139"></a>00139                 printf(<span class="stringliteral">&quot;usb_detach_kernel_driver_np() failed: %s\n&quot;</span>, usb_strerror());
<a name="l00140"></a>00140 
<a name="l00141"></a>00141         <span class="keywordflow">if</span>( usb_set_configuration(handle, BLACKBERRY_CONFIGURATION) &lt; 0 )
<a name="l00142"></a>00142                 printf(<span class="stringliteral">&quot;usb_set_configuration() failed: %s\n&quot;</span>, usb_strerror());
<a name="l00143"></a>00143 <span class="preprocessor">#endif</span>
<a name="l00144"></a>00144 <span class="preprocessor"></span>}
<a name="l00145"></a>00145 
<a name="l00146"></a>00146 <span class="comment">// returns true if device mode was modified, false otherwise</span>
<a name="l00147"></a>00147 <span class="keywordtype">bool</span> process(<span class="keyword">struct</span> usb_device *dev, <span class="keywordtype">bool</span> is_pearl)
<a name="l00148"></a>00148 {
<a name="l00149"></a>00149         <span class="keywordtype">bool</span> apply = <span class="keyword">false</span>;
<a name="l00150"></a>00150         printf(<span class="stringliteral">&quot;Found device #%s...&quot;</span>, dev-&gt;filename);
<a name="l00151"></a>00151 
<a name="l00152"></a>00152         <span class="comment">// open</span>
<a name="l00153"></a>00153         usb_dev_handle *handle = usb_open(dev);
<a name="l00154"></a>00154         <span class="keywordflow">if</span>( !handle ) {
<a name="l00155"></a>00155                 printf(<span class="stringliteral">&quot;unable to open device\n&quot;</span>);
<a name="l00156"></a>00156                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00157"></a>00157         }
<a name="l00158"></a>00158 
<a name="l00159"></a>00159         <span class="comment">// adjust power</span>
<a name="l00160"></a>00160         <span class="keywordflow">if</span>( dev-&gt;config &amp;&amp;
<a name="l00161"></a>00161             dev-&gt;descriptor.bNumConfigurations &gt;= 1 &amp;&amp;
<a name="l00162"></a>00162             dev-&gt;config[0].MaxPower &lt; 250 ) {
<a name="l00163"></a>00163                 printf(<span class="stringliteral">&quot;adjusting charge setting&quot;</span>);
<a name="l00164"></a>00164                 charge(handle);
<a name="l00165"></a>00165                 apply = <span class="keyword">true</span>;
<a name="l00166"></a>00166         }
<a name="l00167"></a>00167         <span class="keywordflow">else</span> {
<a name="l00168"></a>00168                 printf(<span class="stringliteral">&quot;already at 500mA&quot;</span>);
<a name="l00169"></a>00169         }
<a name="l00170"></a>00170 
<a name="l00171"></a>00171         <span class="comment">// adjust Pearl mode</span>
<a name="l00172"></a>00172         <span class="keywordflow">if</span>( is_pearl || force_dual ) {
<a name="l00173"></a>00173                 <span class="keywordtype">int</span> desired_mode = old_style_pearl
<a name="l00174"></a>00174                         ? PRODUCT_RIM_BLACKBERRY : PRODUCT_RIM_PEARL_DUAL;
<a name="l00175"></a>00175 
<a name="l00176"></a>00176                 <span class="keywordflow">if</span>( desired_mode != dev-&gt;descriptor.idProduct ) {
<a name="l00177"></a>00177                         printf(<span class="stringliteral">&quot;...adjusting Pearl mode to %s&quot;</span>,
<a name="l00178"></a>00178                                 old_style_pearl ? <span class="stringliteral">&quot;single&quot;</span> : <span class="stringliteral">&quot;dual&quot;</span>);
<a name="l00179"></a>00179                         pearl_mode(handle);
<a name="l00180"></a>00180                         apply = <span class="keyword">true</span>;
<a name="l00181"></a>00181                 }
<a name="l00182"></a>00182                 <span class="keywordflow">else</span> {
<a name="l00183"></a>00183                         printf(<span class="stringliteral">&quot;...already in desired Pearl mode&quot;</span>);
<a name="l00184"></a>00184                 }
<a name="l00185"></a>00185         }
<a name="l00186"></a>00186         <span class="keywordflow">else</span> {
<a name="l00187"></a>00187                 printf(<span class="stringliteral">&quot;...no Pearl adjustment&quot;</span>);
<a name="l00188"></a>00188         }
<a name="l00189"></a>00189 
<a name="l00190"></a>00190         <span class="comment">// apply changes</span>
<a name="l00191"></a>00191         <span class="keywordflow">if</span>( apply ) {
<a name="l00192"></a>00192                 <span class="keywordflow">if</span>( usb_set_configuration(handle, BLACKBERRY_CONFIGURATION) &lt; 0 )
<a name="l00193"></a>00193                         driver_conflict(handle);
<a name="l00194"></a>00194 
<a name="l00195"></a>00195                 <span class="comment">// the Blackberry Pearl doesn&#39;t reset itself after the above,</span>
<a name="l00196"></a>00196                 <span class="comment">// so do it ourselves</span>
<a name="l00197"></a>00197                 <span class="keywordflow">if</span>( is_pearl || force_dual ) {
<a name="l00198"></a>00198                         <span class="comment">//</span>
<a name="l00199"></a>00199                         <span class="comment">// It has been observed that the 8830 behaves both like</span>
<a name="l00200"></a>00200                         <span class="comment">// a Pearl device (in that it has mass storage +</span>
<a name="l00201"></a>00201                         <span class="comment">// database modes) as well as a Classic device in</span>
<a name="l00202"></a>00202                         <span class="comment">// that it resets itself and doesn&#39;t need an explicit</span>
<a name="l00203"></a>00203                         <span class="comment">// reset call.</span>
<a name="l00204"></a>00204                         <span class="comment">//</span>
<a name="l00205"></a>00205                         <span class="comment">// In order to deal with this, we insert a brief sleep.</span>
<a name="l00206"></a>00206                         <span class="comment">// If it is an 8830, it will reset itself and the</span>
<a name="l00207"></a>00207                         <span class="comment">// following reset call will fail.  If it is a Pearl,</span>
<a name="l00208"></a>00208                         <span class="comment">// the reset will work as normal.</span>
<a name="l00209"></a>00209                         <span class="comment">//</span>
<a name="l00210"></a>00210                         sleep(1);
<a name="l00211"></a>00211                         <span class="keywordflow">if</span>( usb_reset(handle) &lt; 0 ) {
<a name="l00212"></a>00212                                 printf(<span class="stringliteral">&quot;\nusb_reset failed: %s\n&quot;</span>, usb_strerror());
<a name="l00213"></a>00213                         }
<a name="l00214"></a>00214                 }
<a name="l00215"></a>00215 
<a name="l00216"></a>00216                 printf(<span class="stringliteral">&quot;...done\n&quot;</span>);
<a name="l00217"></a>00217         }
<a name="l00218"></a>00218         <span class="keywordflow">else</span> {
<a name="l00219"></a>00219                 printf(<span class="stringliteral">&quot;...no change\n&quot;</span>);
<a name="l00220"></a>00220         }
<a name="l00221"></a>00221 
<a name="l00222"></a>00222         <span class="comment">// cleanup</span>
<a name="l00223"></a>00223         usb_close(handle);
<a name="l00224"></a>00224         <span class="keywordflow">return</span> apply;
<a name="l00225"></a>00225 }
<a name="l00226"></a>00226 
<a name="l00227"></a>00227 <span class="keywordtype">bool</span> power_write(<span class="keyword">const</span> std::string &amp;file, <span class="keyword">const</span> std::string &amp;value)
<a name="l00228"></a>00228 {
<a name="l00229"></a>00229         <span class="comment">// attempt to open the state file</span>
<a name="l00230"></a>00230         <span class="keywordtype">int</span> fd = open(file.c_str(), O_RDWR);
<a name="l00231"></a>00231         <span class="keywordflow">if</span>( fd == -1 ) {
<a name="l00232"></a>00232                 printf(<span class="stringliteral">&quot;autosuspend adjustment failure: (file: %s): %s\n&quot;</span>,
<a name="l00233"></a>00233                         file.c_str(),
<a name="l00234"></a>00234                         strerror(errno));
<a name="l00235"></a>00235                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00236"></a>00236         }
<a name="l00237"></a>00237 
<a name="l00238"></a>00238         <span class="keywordtype">int</span> written = write(fd, value.data(), value.size());
<a name="l00239"></a>00239         <span class="keywordtype">int</span> error = errno;
<a name="l00240"></a>00240         close(fd);
<a name="l00241"></a>00241 
<a name="l00242"></a>00242         <span class="keywordflow">if</span>( written &lt; 0 || (<span class="keywordtype">size_t</span>)written != value.size() ) {
<a name="l00243"></a>00243                 printf(<span class="stringliteral">&quot;autosuspend adjustment failure (write): (file: %s): %s\n&quot;</span>,
<a name="l00244"></a>00244                         file.c_str(),
<a name="l00245"></a>00245                         strerror(error));
<a name="l00246"></a>00246                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00247"></a>00247         }
<a name="l00248"></a>00248 
<a name="l00249"></a>00249         printf(<span class="stringliteral">&quot;autosuspend adjustment: wrote %s to %s\n&quot;</span>,
<a name="l00250"></a>00250                 value.c_str(), file.c_str());
<a name="l00251"></a>00251         <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00252"></a>00252 }
<a name="l00253"></a>00253 
<a name="l00254"></a>00254 <span class="comment">//</span>
<a name="l00255"></a>00255 <span class="comment">// Checks for USB suspend, and enables the device if suspended.</span>
<a name="l00256"></a>00256 <span class="comment">//</span>
<a name="l00257"></a>00257 <span class="comment">// Kernel 2.6.21 behaves with autosuspend=0 meaning off, while 2.6.22</span>
<a name="l00258"></a>00258 <span class="comment">// and higher needs autosuspend=-1 to turn it off.  In 2.6.22, a value</span>
<a name="l00259"></a>00259 <span class="comment">// of 0 means &quot;immediate&quot; instead of &quot;never&quot;.</span>
<a name="l00260"></a>00260 <span class="comment">//</span>
<a name="l00261"></a>00261 <span class="comment">// Version 2.6.22 adds variables internal to the system called</span>
<a name="l00262"></a>00262 <span class="comment">// autosuspend_disabled and autoresume_disabled.  These are controlled by the</span>
<a name="l00263"></a>00263 <span class="comment">// /sys/class/usb_device/*/device/power/level file.  (See below)</span>
<a name="l00264"></a>00264 <span class="comment">//</span>
<a name="l00265"></a>00265 <span class="comment">// Here&#39;s a summary of files under device/power.  These may or may not exist</span>
<a name="l00266"></a>00266 <span class="comment">// depending on your kernel version and configuration.</span>
<a name="l00267"></a>00267 <span class="comment">//</span>
<a name="l00268"></a>00268 <span class="comment">//</span>
<a name="l00269"></a>00269 <span class="comment">//        autosuspend</span>
<a name="l00270"></a>00270 <span class="comment">//                -1 or 0 means off, depending on kernel,</span>
<a name="l00271"></a>00271 <span class="comment">//                otherwise it is the number of seconds to</span>
<a name="l00272"></a>00272 <span class="comment">//                autosuspend</span>
<a name="l00273"></a>00273 <span class="comment">//</span>
<a name="l00274"></a>00274 <span class="comment">//        level</span>
<a name="l00275"></a>00275 <span class="comment">//                with the settings:</span>
<a name="l00276"></a>00276 <span class="comment">//</span>
<a name="l00277"></a>00277 <span class="comment">//                on      - suspend is disabled, device is fully powered</span>
<a name="l00278"></a>00278 <span class="comment">//                auto    - suspend is controlled by the kernel (default)</span>
<a name="l00279"></a>00279 <span class="comment">//                suspend - suspend is enabled permanently</span>
<a name="l00280"></a>00280 <span class="comment">//</span>
<a name="l00281"></a>00281 <span class="comment">//                You can write these strings to the file to control</span>
<a name="l00282"></a>00282 <span class="comment">//                behaviour on a per-device basis.</span>
<a name="l00283"></a>00283 <span class="comment">//</span>
<a name="l00284"></a>00284 <span class="comment">//                echo on &gt; /sys/usb_device/.../device/power/level</span>
<a name="l00285"></a>00285 <span class="comment">//</span>
<a name="l00286"></a>00286 <span class="comment">//        state</span>
<a name="l00287"></a>00287 <span class="comment">//                current state of device</span>
<a name="l00288"></a>00288 <span class="comment">//                0 - fully powered</span>
<a name="l00289"></a>00289 <span class="comment">//                2 - suspended</span>
<a name="l00290"></a>00290 <span class="comment">//</span>
<a name="l00291"></a>00291 <span class="comment">//                You can write these numbers to control behaviour, but</span>
<a name="l00292"></a>00292 <span class="comment">//                any change you make here might change automatically</span>
<a name="l00293"></a>00293 <span class="comment">//                if autosuspend is on.</span>
<a name="l00294"></a>00294 <span class="comment">//</span>
<a name="l00295"></a>00295 <span class="comment">//                echo -n 0 &gt; /sys/usb_device/.../device/power/state</span>
<a name="l00296"></a>00296 <span class="comment">//</span>
<a name="l00297"></a>00297 <span class="comment">//        wakeup</span>
<a name="l00298"></a>00298 <span class="comment">//                unknown</span>
<a name="l00299"></a>00299 <span class="comment">//</span>
<a name="l00300"></a>00300 <span class="comment">//</span>
<a name="l00301"></a>00301 <span class="comment">// Given the above facts, use the following heuristics to try to disable</span>
<a name="l00302"></a>00302 <span class="comment">// autosuspend for the Blackberry:</span>
<a name="l00303"></a>00303 <span class="comment">//</span>
<a name="l00304"></a>00304 <span class="comment">//      - if level exists, write &quot;on&quot; to it</span>
<a name="l00305"></a>00305 <span class="comment">//      - if autosuspend exists, write -1 to it</span>
<a name="l00306"></a>00306 <span class="comment">//              - if error, write 0 to it</span>
<a name="l00307"></a>00307 <span class="comment">//      - if neither of the above work, and state exists, write 0 to it</span>
<a name="l00308"></a>00308 <span class="comment">//</span>
<a name="l00309"></a>00309 <span class="keywordtype">void</span> resume()
<a name="l00310"></a>00310 {
<a name="l00311"></a>00311         <span class="keywordflow">if</span>( udev_devpath.size() == 0 )
<a name="l00312"></a>00312                 <span class="keywordflow">return</span>; <span class="comment">// nothing to do</span>
<a name="l00313"></a>00313 
<a name="l00314"></a>00314         <span class="comment">// let sysfs settle a bit</span>
<a name="l00315"></a>00315         sleep(5);
<a name="l00316"></a>00316 
<a name="l00317"></a>00317         std::string power_path = sysfs_path + <span class="stringliteral">&quot;/&quot;</span> + udev_devpath + <span class="stringliteral">&quot;/device/power/&quot;</span>;
<a name="l00318"></a>00318 
<a name="l00319"></a>00319         <span class="keywordflow">if</span>( !power_write(power_path + <span class="stringliteral">&quot;level&quot;</span>, <span class="stringliteral">&quot;on\n&quot;</span>) )
<a name="l00320"></a>00320                 <span class="keywordflow">if</span>( !power_write(power_path + <span class="stringliteral">&quot;autosuspend&quot;</span>, <span class="stringliteral">&quot;-1\n&quot;</span>) )
<a name="l00321"></a>00321                         <span class="keywordflow">if</span>( !power_write(power_path + <span class="stringliteral">&quot;autosuspend&quot;</span>, <span class="stringliteral">&quot;0\n&quot;</span>) )
<a name="l00322"></a>00322                                 power_write(power_path + <span class="stringliteral">&quot;state&quot;</span>, <span class="stringliteral">&quot;0&quot;</span>);
<a name="l00323"></a>00323 }
<a name="l00324"></a>00324 
<a name="l00325"></a>00325 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
<a name="l00326"></a>00326 {
<a name="l00327"></a>00327         <span class="keyword">struct </span>usb_bus *busses;
<a name="l00328"></a>00328 
<a name="l00329"></a>00329         INIT_I18N(PACKAGE);
<a name="l00330"></a>00330 
<a name="l00331"></a>00331         <span class="comment">//</span>
<a name="l00332"></a>00332         <span class="comment">// allow -o command line switch to choose which mode to use for</span>
<a name="l00333"></a>00333         <span class="comment">// Blackberry Pearls:</span>
<a name="l00334"></a>00334         <span class="comment">//      Dual(default):  0004    -d</span>
<a name="l00335"></a>00335         <span class="comment">//      With switch:    0001    -o</span>
<a name="l00336"></a>00336         <span class="comment">//</span>
<a name="l00337"></a>00337 
<a name="l00338"></a>00338         <span class="comment">// process command line options</span>
<a name="l00339"></a>00339         <span class="keywordflow">for</span>(;;) {
<a name="l00340"></a>00340                 <span class="keywordtype">int</span> cmd = getopt(argc, argv, <span class="stringliteral">&quot;dop:s:h&quot;</span>);
<a name="l00341"></a>00341                 <span class="keywordflow">if</span>( cmd == -1 )
<a name="l00342"></a>00342                         <span class="keywordflow">break</span>;
<a name="l00343"></a>00343 
<a name="l00344"></a>00344                 <span class="keywordflow">switch</span>( cmd )
<a name="l00345"></a>00345                 {
<a name="l00346"></a>00346                 <span class="keywordflow">case</span> <span class="charliteral">&#39;d&#39;</span>:       <span class="comment">// Dual (default)</span>
<a name="l00347"></a>00347                         force_dual = <span class="keyword">true</span>;
<a name="l00348"></a>00348                         old_style_pearl = <span class="keyword">false</span>;
<a name="l00349"></a>00349                         <span class="keywordflow">break</span>;
<a name="l00350"></a>00350 
<a name="l00351"></a>00351                 <span class="keywordflow">case</span> <span class="charliteral">&#39;o&#39;</span>:       <span class="comment">// Old style pearl</span>
<a name="l00352"></a>00352                         force_dual = <span class="keyword">false</span>;
<a name="l00353"></a>00353                         old_style_pearl = <span class="keyword">true</span>;
<a name="l00354"></a>00354                         <span class="keywordflow">break</span>;
<a name="l00355"></a>00355 
<a name="l00356"></a>00356                 <span class="keywordflow">case</span> <span class="charliteral">&#39;p&#39;</span>:       <span class="comment">// udev devpath</span>
<a name="l00357"></a>00357                         udev_devpath = optarg;
<a name="l00358"></a>00358                         <span class="keywordflow">break</span>;
<a name="l00359"></a>00359 
<a name="l00360"></a>00360                 <span class="keywordflow">case</span> <span class="charliteral">&#39;s&#39;</span>:       <span class="comment">// where sysfs is mounted</span>
<a name="l00361"></a>00361                         sysfs_path = optarg;
<a name="l00362"></a>00362                         <span class="keywordflow">break</span>;
<a name="l00363"></a>00363 
<a name="l00364"></a>00364                 <span class="keywordflow">case</span> <span class="charliteral">&#39;h&#39;</span>:       <span class="comment">// help!</span>
<a name="l00365"></a>00365                 <span class="keywordflow">default</span>:
<a name="l00366"></a>00366                         Usage();
<a name="l00367"></a>00367                         <span class="keywordflow">return</span> 0;
<a name="l00368"></a>00368                 }
<a name="l00369"></a>00369         }
<a name="l00370"></a>00370 
<a name="l00371"></a>00371         usb_init();
<a name="l00372"></a>00372         <span class="keywordflow">if</span>( usb_find_busses() &lt; 0 || usb_find_devices() &lt; 0 ) {
<a name="l00373"></a>00373                 printf(<span class="stringliteral">&quot;\nUnable to scan devices: %s\n&quot;</span>, usb_strerror());
<a name="l00374"></a>00374                 <span class="keywordflow">return</span> 1;
<a name="l00375"></a>00375         }
<a name="l00376"></a>00376         busses = usb_get_busses();
<a name="l00377"></a>00377 
<a name="l00378"></a>00378         printf(<span class="stringliteral">&quot;Scanning for Blackberry devices...\n&quot;</span>);
<a name="l00379"></a>00379 
<a name="l00380"></a>00380         <span class="keyword">struct </span>usb_bus *bus;
<a name="l00381"></a>00381         <span class="keywordflow">for</span>( bus = busses; bus; bus = bus-&gt;next ) {
<a name="l00382"></a>00382                 <span class="keyword">struct </span>usb_device *dev;
<a name="l00383"></a>00383 
<a name="l00384"></a>00384                 <span class="keywordflow">for</span> (dev = bus-&gt;devices; dev; dev = dev-&gt;next) {
<a name="l00385"></a>00385                         <span class="comment">// Is this a blackberry?</span>
<a name="l00386"></a>00386                         <span class="keywordflow">if</span>( dev-&gt;descriptor.idVendor == VENDOR_RIM ) {
<a name="l00387"></a>00387                                 <span class="keywordflow">switch</span>(dev-&gt;descriptor.idProduct)
<a name="l00388"></a>00388                                 {
<a name="l00389"></a>00389                                 <span class="keywordflow">case</span> PRODUCT_RIM_BLACKBERRY:
<a name="l00390"></a>00390                                         <span class="keywordflow">if</span>( !process(dev, <span class="keyword">false</span>) )
<a name="l00391"></a>00391                                                 resume();
<a name="l00392"></a>00392                                         <span class="keywordflow">break</span>;
<a name="l00393"></a>00393 
<a name="l00394"></a>00394                                 <span class="keywordflow">case</span> PRODUCT_RIM_PEARL_DUAL:
<a name="l00395"></a>00395                                 <span class="keywordflow">case</span> PRODUCT_RIM_PEARL:
<a name="l00396"></a>00396                                 <span class="keywordflow">case</span> PRODUCT_RIM_PEARL_8120:
<a name="l00397"></a>00397                                 <span class="keywordflow">case</span> PRODUCT_RIM_PEARL_FLIP:
<a name="l00398"></a>00398                                 <span class="keywordflow">case</span> PRODUCT_RIM_STORM:
<a name="l00399"></a>00399                                         <span class="keywordflow">if</span>( !process(dev, <span class="keyword">true</span>) )
<a name="l00400"></a>00400                                                 resume();
<a name="l00401"></a>00401                                         <span class="keywordflow">break</span>;
<a name="l00402"></a>00402                                 }
<a name="l00403"></a>00403                         }
<a name="l00404"></a>00404                 }
<a name="l00405"></a>00405         }
<a name="l00406"></a>00406 }
<a name="l00407"></a>00407 
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address>
</body>
</html>