<!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>libftdi: ftdi.c 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.7.4 --> <div id="top"> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">libftdi <span id="projectnumber">0.19</span></div> </td> </tr> </tbody> </table> </div> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</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> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <div class="title">ftdi.c</div> </div> </div> <div class="contents"> <a href="ftdi_8c.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"> ftdi.c - description</span> <a name="l00003"></a>00003 <span class="comment"> -------------------</span> <a name="l00004"></a>00004 <span class="comment"> begin : Fri Apr 4 2003</span> <a name="l00005"></a>00005 <span class="comment"> copyright : (C) 2003-2010 by Intra2net AG</span> <a name="l00006"></a>00006 <span class="comment"> email : opensource@intra2net.com</span> <a name="l00007"></a>00007 <span class="comment"> ***************************************************************************/</span> <a name="l00008"></a>00008 <a name="l00009"></a>00009 <span class="comment">/***************************************************************************</span> <a name="l00010"></a>00010 <span class="comment"> * *</span> <a name="l00011"></a>00011 <span class="comment"> * This program is free software; you can redistribute it and/or modify *</span> <a name="l00012"></a>00012 <span class="comment"> * it under the terms of the GNU Lesser General Public License *</span> <a name="l00013"></a>00013 <span class="comment"> * version 2.1 as published by the Free Software Foundation; *</span> <a name="l00014"></a>00014 <span class="comment"> * *</span> <a name="l00015"></a>00015 <span class="comment"> ***************************************************************************/</span> <a name="l00016"></a>00016 <a name="l00029"></a>00029 <span class="comment">/* @{ */</span> <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="preprocessor">#include <usb.h></span> <a name="l00032"></a>00032 <span class="preprocessor">#include <string.h></span> <a name="l00033"></a>00033 <span class="preprocessor">#include <errno.h></span> <a name="l00034"></a>00034 <span class="preprocessor">#include <stdio.h></span> <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="preprocessor">#include "<a class="code" href="ftdi_8h.html">ftdi.h</a>"</span> <a name="l00037"></a>00037 <a name="l00038"></a>00038 <span class="comment">/* stuff needed for async write */</span> <a name="l00039"></a>00039 <span class="preprocessor">#ifdef LIBFTDI_LINUX_ASYNC_MODE</span> <a name="l00040"></a>00040 <span class="preprocessor"></span><span class="preprocessor">#include <sys/ioctl.h></span> <a name="l00041"></a>00041 <span class="preprocessor">#include <sys/select.h></span> <a name="l00042"></a>00042 <span class="preprocessor">#include <sys/types.h></span> <a name="l00043"></a>00043 <span class="preprocessor">#include <unistd.h></span> <a name="l00044"></a>00044 <span class="preprocessor">#include <linux/usbdevice_fs.h></span> <a name="l00045"></a>00045 <span class="preprocessor">#endif</span> <a name="l00046"></a>00046 <span class="preprocessor"></span> <a name="l00047"></a><a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">00047</a> <span class="preprocessor">#define ftdi_error_return(code, str) do { \</span> <a name="l00048"></a>00048 <span class="preprocessor"> ftdi->error_str = str; \</span> <a name="l00049"></a>00049 <span class="preprocessor"> return code; \</span> <a name="l00050"></a>00050 <span class="preprocessor"> } while(0);</span> <a name="l00051"></a>00051 <span class="preprocessor"></span> <a name="l00052"></a>00052 <a name="l00062"></a>00062 <span class="keyword">static</span> <span class="keywordtype">int</span> ftdi_usb_close_internal (<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00063"></a>00063 { <a name="l00064"></a>00064 <span class="keywordtype">int</span> ret = 0; <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keywordflow">if</span> (ftdi && ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>) <a name="l00067"></a>00067 { <a name="l00068"></a>00068 ret = usb_close (ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>); <a name="l00069"></a>00069 ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> = NULL; <a name="l00070"></a>00070 } <a name="l00071"></a>00071 <a name="l00072"></a>00072 <span class="keywordflow">return</span> ret; <a name="l00073"></a>00073 } <a name="l00074"></a>00074 <a name="l00085"></a><a class="code" href="group__libftdi.html#ga21e2ed53cad4fa8242022afe1e12d2ee">00085</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga21e2ed53cad4fa8242022afe1e12d2ee">ftdi_init</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00086"></a>00086 { <a name="l00087"></a>00087 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; <a name="l00088"></a>00088 <a name="l00089"></a>00089 ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> = NULL; <a name="l00090"></a>00090 ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a> = 5000; <a name="l00091"></a>00091 ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a> = 5000; <a name="l00092"></a>00092 <a name="l00093"></a>00093 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea3865c3e28f68be594baec18ca30d2068">TYPE_BM</a>; <span class="comment">/* chip type */</span> <a name="l00094"></a>00094 ftdi-><a class="code" href="structftdi__context.html#aa688eb2312af3a9fb6b56a8c59178e34">baudrate</a> = -1; <a name="l00095"></a>00095 ftdi-><a class="code" href="structftdi__context.html#a164c17ee7bddaa0d76da8da7138f602f">bitbang_enabled</a> = 0; <span class="comment">/* 0: normal mode 1: any of the bitbang modes enabled */</span> <a name="l00096"></a>00096 <a name="l00097"></a>00097 ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a> = NULL; <a name="l00098"></a>00098 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> = 0; <a name="l00099"></a>00099 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> = 0; <a name="l00100"></a>00100 ftdi-><a class="code" href="structftdi__context.html#a127d66a8b7602bd767fb01ce90e2a6be">writebuffer_chunksize</a> = 4096; <a name="l00101"></a>00101 ftdi-><a class="code" href="structftdi__context.html#a7ca2f7e68cd28e1c3079d029fd6129c0">max_packet_size</a> = 0; <a name="l00102"></a>00102 <a name="l00103"></a>00103 ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a> = 0; <a name="l00104"></a>00104 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a> = 0; <a name="l00105"></a>00105 ftdi-><a class="code" href="structftdi__context.html#a9b6cfd3763a3ca80c76bc52c89dad968">in_ep</a> = 0x02; <a name="l00106"></a>00106 ftdi-><a class="code" href="structftdi__context.html#a079427d8f2c09102bcadb9118e60c0ec">out_ep</a> = 0x81; <a name="l00107"></a>00107 ftdi-><a class="code" href="structftdi__context.html#a017557435f4377c2353d9d3b7fbcff01">bitbang_mode</a> = 1; <span class="comment">/* when bitbang is enabled this holds the number of the mode */</span> <a name="l00108"></a>00108 <a name="l00109"></a>00109 ftdi-><a class="code" href="structftdi__context.html#accc4dc90cea120d545faf82c6034d445">error_str</a> = NULL; <a name="l00110"></a>00110 <a name="l00111"></a>00111 <span class="preprocessor">#ifdef LIBFTDI_LINUX_ASYNC_MODE</span> <a name="l00112"></a>00112 <span class="preprocessor"></span> ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>=10; <a name="l00113"></a>00113 <span class="keywordflow">if</span> ((ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a>=malloc(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> usbdevfs_urb)*ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>)) == NULL) <a name="l00114"></a>00114 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"out of memory for async usb buffer"</span>); <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="comment">/* initialize async usb buffer with unused-marker */</span> <a name="l00117"></a>00117 <span class="keywordflow">for</span> (i=0; i < ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>; i++) <a name="l00118"></a>00118 ((<span class="keyword">struct</span> usbdevfs_urb*)ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a>)[i].usercontext = <a class="code" href="ftdi_8h.html#a40b81fba21b92de527c99f43a0f8b254">FTDI_URB_USERCONTEXT_COOKIE</a>; <a name="l00119"></a>00119 <span class="preprocessor">#else</span> <a name="l00120"></a>00120 <span class="preprocessor"></span> ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>=0; <a name="l00121"></a>00121 ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a> = NULL; <a name="l00122"></a>00122 <span class="preprocessor">#endif</span> <a name="l00123"></a>00123 <span class="preprocessor"></span> <a name="l00124"></a>00124 ftdi-><a class="code" href="structftdi__context.html#ad53e310b3c70a57d36d34b85d7c67550">eeprom_size</a> = <a class="code" href="ftdi_8h.html#a1d8794aafb35cdc72bf0d2f01d4063b9">FTDI_DEFAULT_EEPROM_SIZE</a>; <a name="l00125"></a>00125 <a name="l00126"></a>00126 ftdi-><a class="code" href="structftdi__context.html#a06072f2484298ae90fcffe450a133250">module_detach_mode</a> = <a class="code" href="ftdi_8h.html#adb5c9021fa85791df4a0b9dac671109aa3fde2d289c2051df02332045e58f4a76">AUTO_DETACH_SIO_MODULE</a>; <a name="l00127"></a>00127 <a name="l00128"></a>00128 <span class="comment">/* All fine. Now allocate the readbuffer */</span> <a name="l00129"></a>00129 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#ga65783703baf0828d519ef597bb2855c1">ftdi_read_data_set_chunksize</a>(ftdi, 4096); <a name="l00130"></a>00130 } <a name="l00131"></a>00131 <a name="l00137"></a><a class="code" href="group__libftdi.html#gafd46c6784367fd95c5f852d3b38949a8">00137</a> <span class="keyword">struct </span><a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *<a class="code" href="group__libftdi.html#gafd46c6784367fd95c5f852d3b38949a8">ftdi_new</a>(<span class="keywordtype">void</span>) <a name="l00138"></a>00138 { <a name="l00139"></a>00139 <span class="keyword">struct </span><a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> * ftdi = (<span class="keyword">struct </span><a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *)malloc(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a>)); <a name="l00140"></a>00140 <a name="l00141"></a>00141 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00142"></a>00142 { <a name="l00143"></a>00143 <span class="keywordflow">return</span> NULL; <a name="l00144"></a>00144 } <a name="l00145"></a>00145 <a name="l00146"></a>00146 <span class="keywordflow">if</span> (<a class="code" href="group__libftdi.html#ga21e2ed53cad4fa8242022afe1e12d2ee">ftdi_init</a>(ftdi) != 0) <a name="l00147"></a>00147 { <a name="l00148"></a>00148 free(ftdi); <a name="l00149"></a>00149 <span class="keywordflow">return</span> NULL; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <a name="l00152"></a>00152 <span class="keywordflow">return</span> ftdi; <a name="l00153"></a>00153 } <a name="l00154"></a>00154 <a name="l00165"></a><a class="code" href="group__libftdi.html#ga6dc886d709eced377a33dc975fa6e771">00165</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga6dc886d709eced377a33dc975fa6e771">ftdi_set_interface</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265">ftdi_interface</a> <a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a>) <a name="l00166"></a>00166 { <a name="l00167"></a>00167 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00168"></a>00168 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l00169"></a>00169 <a name="l00170"></a>00170 <span class="keywordflow">switch</span> (interface) <a name="l00171"></a>00171 { <a name="l00172"></a>00172 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a794b6a029883152ff98ea1f6c896b02d">INTERFACE_ANY</a>: <a name="l00173"></a>00173 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265afb60d9e73330c16ba970b3dd169fe4d1">INTERFACE_A</a>: <a name="l00174"></a>00174 <span class="comment">/* ftdi_usb_open_desc cares to set the right index, depending on the found chip */</span> <a name="l00175"></a>00175 <span class="keywordflow">break</span>; <a name="l00176"></a>00176 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a5326a506445adc927025ee5c55452380">INTERFACE_B</a>: <a name="l00177"></a>00177 ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a> = 1; <a name="l00178"></a>00178 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a> = <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a5326a506445adc927025ee5c55452380">INTERFACE_B</a>; <a name="l00179"></a>00179 ftdi-><a class="code" href="structftdi__context.html#a9b6cfd3763a3ca80c76bc52c89dad968">in_ep</a> = 0x04; <a name="l00180"></a>00180 ftdi-><a class="code" href="structftdi__context.html#a079427d8f2c09102bcadb9118e60c0ec">out_ep</a> = 0x83; <a name="l00181"></a>00181 <span class="keywordflow">break</span>; <a name="l00182"></a>00182 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a1eb34784ca745a5d76480c190a92d68c">INTERFACE_C</a>: <a name="l00183"></a>00183 ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a> = 2; <a name="l00184"></a>00184 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a> = <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a1eb34784ca745a5d76480c190a92d68c">INTERFACE_C</a>; <a name="l00185"></a>00185 ftdi-><a class="code" href="structftdi__context.html#a9b6cfd3763a3ca80c76bc52c89dad968">in_ep</a> = 0x06; <a name="l00186"></a>00186 ftdi-><a class="code" href="structftdi__context.html#a079427d8f2c09102bcadb9118e60c0ec">out_ep</a> = 0x85; <a name="l00187"></a>00187 <span class="keywordflow">break</span>; <a name="l00188"></a>00188 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a3b672d2aef77ffdf9d59d827db194021">INTERFACE_D</a>: <a name="l00189"></a>00189 ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a> = 3; <a name="l00190"></a>00190 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a> = <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265a3b672d2aef77ffdf9d59d827db194021">INTERFACE_D</a>; <a name="l00191"></a>00191 ftdi-><a class="code" href="structftdi__context.html#a9b6cfd3763a3ca80c76bc52c89dad968">in_ep</a> = 0x08; <a name="l00192"></a>00192 ftdi-><a class="code" href="structftdi__context.html#a079427d8f2c09102bcadb9118e60c0ec">out_ep</a> = 0x87; <a name="l00193"></a>00193 <span class="keywordflow">break</span>; <a name="l00194"></a>00194 <span class="keywordflow">default</span>: <a name="l00195"></a>00195 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"Unknown interface"</span>); <a name="l00196"></a>00196 } <a name="l00197"></a>00197 <span class="keywordflow">return</span> 0; <a name="l00198"></a>00198 } <a name="l00199"></a>00199 <a name="l00205"></a><a class="code" href="group__libftdi.html#ga72838200be98d88737d532f9eee1e7be">00205</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#ga72838200be98d88737d532f9eee1e7be">ftdi_deinit</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00206"></a>00206 { <a name="l00207"></a>00207 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00208"></a>00208 <span class="keywordflow">return</span>; <a name="l00209"></a>00209 <a name="l00210"></a>00210 ftdi_usb_close_internal (ftdi); <a name="l00211"></a>00211 <a name="l00212"></a>00212 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a> != NULL) <a name="l00213"></a>00213 { <a name="l00214"></a>00214 free(ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a>); <a name="l00215"></a>00215 ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a> = NULL; <a name="l00216"></a>00216 } <a name="l00217"></a>00217 <a name="l00218"></a>00218 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a> != NULL) <a name="l00219"></a>00219 { <a name="l00220"></a>00220 free(ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>); <a name="l00221"></a>00221 ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a> = NULL; <a name="l00222"></a>00222 } <a name="l00223"></a>00223 } <a name="l00224"></a>00224 <a name="l00230"></a><a class="code" href="group__libftdi.html#ga3cc28e5b7f1f50246861fcb39e7dd291">00230</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#ga3cc28e5b7f1f50246861fcb39e7dd291">ftdi_free</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00231"></a>00231 { <a name="l00232"></a>00232 <a class="code" href="group__libftdi.html#ga72838200be98d88737d532f9eee1e7be">ftdi_deinit</a>(ftdi); <a name="l00233"></a>00233 free(ftdi); <a name="l00234"></a>00234 } <a name="l00235"></a>00235 <a name="l00242"></a><a class="code" href="group__libftdi.html#ga1b66cfb146ff0732322c49585d2459f6">00242</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#ga1b66cfb146ff0732322c49585d2459f6">ftdi_set_usbdev</a> (<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <a class="code" href="structusb__dev__handle.html">usb_dev_handle</a> *usb) <a name="l00243"></a>00243 { <a name="l00244"></a>00244 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00245"></a>00245 <span class="keywordflow">return</span>; <a name="l00246"></a>00246 <a name="l00247"></a>00247 ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> = usb; <a name="l00248"></a>00248 } <a name="l00249"></a>00249 <a name="l00250"></a>00250 <a name="l00265"></a><a class="code" href="group__libftdi.html#ga2845a0c63809747c9c7b1ea404453bc4">00265</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga2845a0c63809747c9c7b1ea404453bc4">ftdi_usb_find_all</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">struct</span> <a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a> **devlist, <span class="keywordtype">int</span> vendor, <span class="keywordtype">int</span> product) <a name="l00266"></a>00266 { <a name="l00267"></a>00267 <span class="keyword">struct </span><a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a> **curdev; <a name="l00268"></a>00268 <span class="keyword">struct </span>usb_bus *bus; <a name="l00269"></a>00269 <span class="keyword">struct </span>usb_device *dev; <a name="l00270"></a>00270 <span class="keywordtype">int</span> count = 0; <a name="l00271"></a>00271 <a name="l00272"></a>00272 usb_init(); <a name="l00273"></a>00273 <span class="keywordflow">if</span> (usb_find_busses() < 0) <a name="l00274"></a>00274 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"usb_find_busses() failed"</span>); <a name="l00275"></a>00275 <span class="keywordflow">if</span> (usb_find_devices() < 0) <a name="l00276"></a>00276 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"usb_find_devices() failed"</span>); <a name="l00277"></a>00277 <a name="l00278"></a>00278 curdev = devlist; <a name="l00279"></a>00279 *curdev = NULL; <a name="l00280"></a>00280 <span class="keywordflow">for</span> (bus = usb_get_busses(); bus; bus = bus->next) <a name="l00281"></a>00281 { <a name="l00282"></a>00282 <span class="keywordflow">for</span> (dev = bus->devices; dev; dev = dev->next) <a name="l00283"></a>00283 { <a name="l00284"></a>00284 <span class="keywordflow">if</span> (dev->descriptor.idVendor == vendor <a name="l00285"></a>00285 && dev->descriptor.idProduct == product) <a name="l00286"></a>00286 { <a name="l00287"></a>00287 *curdev = (<span class="keyword">struct </span><a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a>*)malloc(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a>)); <a name="l00288"></a>00288 <span class="keywordflow">if</span> (!*curdev) <a name="l00289"></a>00289 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"out of memory"</span>); <a name="l00290"></a>00290 <a name="l00291"></a>00291 (*curdev)->next = NULL; <a name="l00292"></a>00292 (*curdev)->dev = <a class="code" href="structftdi__device__list.html#a9067b0b6efaa1c0e2be4f4658c3e3e6e">dev</a>; <a name="l00293"></a>00293 <a name="l00294"></a>00294 curdev = &(*curdev)-><a class="code" href="structftdi__device__list.html#aa80d0234b1a36e0e9a91605a7c74df37">next</a>; <a name="l00295"></a>00295 count++; <a name="l00296"></a>00296 } <a name="l00297"></a>00297 } <a name="l00298"></a>00298 } <a name="l00299"></a>00299 <a name="l00300"></a>00300 <span class="keywordflow">return</span> count; <a name="l00301"></a>00301 } <a name="l00302"></a>00302 <a name="l00308"></a><a class="code" href="group__libftdi.html#ga35358ea578644aa309300db782415c9f">00308</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#ga35358ea578644aa309300db782415c9f">ftdi_list_free</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a> **devlist) <a name="l00309"></a>00309 { <a name="l00310"></a>00310 <span class="keyword">struct </span><a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a> *curdev, *<a class="code" href="structftdi__device__list.html#aa80d0234b1a36e0e9a91605a7c74df37">next</a>; <a name="l00311"></a>00311 <a name="l00312"></a>00312 <span class="keywordflow">for</span> (curdev = *devlist; curdev != NULL;) <a name="l00313"></a>00313 { <a name="l00314"></a>00314 next = curdev-><a class="code" href="structftdi__device__list.html#aa80d0234b1a36e0e9a91605a7c74df37">next</a>; <a name="l00315"></a>00315 free(curdev); <a name="l00316"></a>00316 curdev = <a class="code" href="structftdi__device__list.html#aa80d0234b1a36e0e9a91605a7c74df37">next</a>; <a name="l00317"></a>00317 } <a name="l00318"></a>00318 <a name="l00319"></a>00319 *devlist = NULL; <a name="l00320"></a>00320 } <a name="l00321"></a>00321 <a name="l00327"></a><a class="code" href="group__libftdi.html#gaa4838bebb2024ae9bfdcc81adbda3493">00327</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#gaa4838bebb2024ae9bfdcc81adbda3493">ftdi_list_free2</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__device__list.html" title="list of usb devices created by ftdi_usb_find_all()">ftdi_device_list</a> *devlist) <a name="l00328"></a>00328 { <a name="l00329"></a>00329 <a class="code" href="group__libftdi.html#ga35358ea578644aa309300db782415c9f">ftdi_list_free</a>(&devlist); <a name="l00330"></a>00330 } <a name="l00331"></a>00331 <a name="l00358"></a><a class="code" href="group__libftdi.html#gabeca5642c95141e11b6936175ce08b3b">00358</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gabeca5642c95141e11b6936175ce08b3b">ftdi_usb_get_strings</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> * ftdi, <span class="keyword">struct</span> usb_device * <a class="code" href="structftdi__device__list.html#a9067b0b6efaa1c0e2be4f4658c3e3e6e">dev</a>, <a name="l00359"></a>00359 <span class="keywordtype">char</span> * manufacturer, <span class="keywordtype">int</span> mnf_len, <span class="keywordtype">char</span> * description, <span class="keywordtype">int</span> desc_len, <span class="keywordtype">char</span> * serial, <span class="keywordtype">int</span> serial_len) <a name="l00360"></a>00360 { <a name="l00361"></a>00361 <span class="keywordflow">if</span> ((ftdi==NULL) || (dev==NULL)) <a name="l00362"></a>00362 <span class="keywordflow">return</span> -1; <a name="l00363"></a>00363 <a name="l00364"></a>00364 <span class="keywordflow">if</span> (!(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> = usb_open(dev))) <a name="l00365"></a>00365 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-4, usb_strerror()); <a name="l00366"></a>00366 <a name="l00367"></a>00367 <span class="keywordflow">if</span> (manufacturer != NULL) <a name="l00368"></a>00368 { <a name="l00369"></a>00369 <span class="keywordflow">if</span> (usb_get_string_simple(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) <a name="l00370"></a>00370 { <a name="l00371"></a>00371 ftdi_usb_close_internal (ftdi); <a name="l00372"></a>00372 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-7, usb_strerror()); <a name="l00373"></a>00373 } <a name="l00374"></a>00374 } <a name="l00375"></a>00375 <a name="l00376"></a>00376 <span class="keywordflow">if</span> (description != NULL) <a name="l00377"></a>00377 { <a name="l00378"></a>00378 <span class="keywordflow">if</span> (usb_get_string_simple(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, dev->descriptor.iProduct, description, desc_len) <= 0) <a name="l00379"></a>00379 { <a name="l00380"></a>00380 ftdi_usb_close_internal (ftdi); <a name="l00381"></a>00381 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-8, usb_strerror()); <a name="l00382"></a>00382 } <a name="l00383"></a>00383 } <a name="l00384"></a>00384 <a name="l00385"></a>00385 <span class="keywordflow">if</span> (serial != NULL) <a name="l00386"></a>00386 { <a name="l00387"></a>00387 <span class="keywordflow">if</span> (usb_get_string_simple(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) <a name="l00388"></a>00388 { <a name="l00389"></a>00389 ftdi_usb_close_internal (ftdi); <a name="l00390"></a>00390 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-9, usb_strerror()); <a name="l00391"></a>00391 } <a name="l00392"></a>00392 } <a name="l00393"></a>00393 <a name="l00394"></a>00394 <span class="keywordflow">if</span> (ftdi_usb_close_internal (ftdi) != 0) <a name="l00395"></a>00395 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-10, usb_strerror()); <a name="l00396"></a>00396 <a name="l00397"></a>00397 <span class="keywordflow">return</span> 0; <a name="l00398"></a>00398 } <a name="l00399"></a>00399 <a name="l00406"></a>00406 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _ftdi_determine_max_packet_size(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">struct</span> usb_device *<a class="code" href="structftdi__device__list.html#a9067b0b6efaa1c0e2be4f4658c3e3e6e">dev</a>) <a name="l00407"></a>00407 { <a name="l00408"></a>00408 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packet_size; <a name="l00409"></a>00409 <a name="l00410"></a>00410 <span class="comment">// Sanity check</span> <a name="l00411"></a>00411 <span class="keywordflow">if</span> (ftdi == NULL || dev == NULL) <a name="l00412"></a>00412 <span class="keywordflow">return</span> 64; <a name="l00413"></a>00413 <a name="l00414"></a>00414 <span class="comment">// Determine maximum packet size. Init with default value.</span> <a name="l00415"></a>00415 <span class="comment">// New hi-speed devices from FTDI use a packet size of 512 bytes</span> <a name="l00416"></a>00416 <span class="comment">// but could be connected to a normal speed USB hub -> 64 bytes packet size.</span> <a name="l00417"></a>00417 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea1beb33ef9cca96d6a05f8fd7f1fdb92f">TYPE_2232H</a> || ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceab327f0179af90c5e1b2f447b6ebb426b">TYPE_4232H</a>) <a name="l00418"></a>00418 packet_size = 512; <a name="l00419"></a>00419 <span class="keywordflow">else</span> <a name="l00420"></a>00420 packet_size = 64; <a name="l00421"></a>00421 <a name="l00422"></a>00422 <span class="keywordflow">if</span> (dev->descriptor.bNumConfigurations > 0 && dev->config) <a name="l00423"></a>00423 { <a name="l00424"></a>00424 <span class="keyword">struct </span>usb_config_descriptor config = dev->config[0]; <a name="l00425"></a>00425 <a name="l00426"></a>00426 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a> < config.bNumInterfaces) <a name="l00427"></a>00427 { <a name="l00428"></a>00428 <span class="keyword">struct </span>usb_interface interface = config.interface[ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a>]; <a name="l00429"></a>00429 <span class="keywordflow">if</span> (interface.num_altsetting > 0) <a name="l00430"></a>00430 { <a name="l00431"></a>00431 <span class="keyword">struct </span>usb_interface_descriptor descriptor = interface.altsetting[0]; <a name="l00432"></a>00432 <span class="keywordflow">if</span> (descriptor.bNumEndpoints > 0) <a name="l00433"></a>00433 { <a name="l00434"></a>00434 packet_size = descriptor.endpoint[0].wMaxPacketSize; <a name="l00435"></a>00435 } <a name="l00436"></a>00436 } <a name="l00437"></a>00437 } <a name="l00438"></a>00438 } <a name="l00439"></a>00439 <a name="l00440"></a>00440 <span class="keywordflow">return</span> packet_size; <a name="l00441"></a>00441 } <a name="l00442"></a>00442 <a name="l00457"></a><a class="code" href="group__libftdi.html#gaa03eb28a2ec02db4e923e3949854a30d">00457</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gaa03eb28a2ec02db4e923e3949854a30d">ftdi_usb_open_dev</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">struct</span> usb_device *dev) <a name="l00458"></a>00458 { <a name="l00459"></a>00459 <span class="keywordtype">int</span> detach_errno = 0; <a name="l00460"></a>00460 <span class="keywordtype">int</span> config_val = 1; <a name="l00461"></a>00461 <a name="l00462"></a>00462 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00463"></a>00463 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-8, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l00464"></a>00464 <a name="l00465"></a>00465 <span class="keywordflow">if</span> (!(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> = usb_open(dev))) <a name="l00466"></a>00466 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-4, <span class="stringliteral">"usb_open() failed"</span>); <a name="l00467"></a>00467 <a name="l00468"></a>00468 <span class="preprocessor">#ifdef LIBUSB_HAS_GET_DRIVER_NP</span> <a name="l00469"></a>00469 <span class="preprocessor"></span> <span class="comment">// Try to detach ftdi_sio kernel module.</span> <a name="l00470"></a>00470 <span class="comment">// Returns ENODATA if driver is not loaded.</span> <a name="l00471"></a>00471 <span class="comment">//</span> <a name="l00472"></a>00472 <span class="comment">// The return code is kept in a separate variable and only parsed</span> <a name="l00473"></a>00473 <span class="comment">// if usb_set_configuration() or usb_claim_interface() fails as the</span> <a name="l00474"></a>00474 <span class="comment">// detach operation might be denied and everything still works fine.</span> <a name="l00475"></a>00475 <span class="comment">// Likely scenario is a static ftdi_sio kernel module.</span> <a name="l00476"></a>00476 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a06072f2484298ae90fcffe450a133250">module_detach_mode</a> == <a class="code" href="ftdi_8h.html#adb5c9021fa85791df4a0b9dac671109aa3fde2d289c2051df02332045e58f4a76">AUTO_DETACH_SIO_MODULE</a>) <a name="l00477"></a>00477 { <a name="l00478"></a>00478 <span class="keywordflow">if</span> (usb_detach_kernel_driver_np(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a>) != 0 && errno != ENODATA) <a name="l00479"></a>00479 detach_errno = errno; <a name="l00480"></a>00480 } <a name="l00481"></a>00481 <span class="preprocessor">#endif</span> <a name="l00482"></a>00482 <span class="preprocessor"></span> <a name="l00483"></a>00483 <span class="preprocessor">#ifdef __WIN32__</span> <a name="l00484"></a>00484 <span class="preprocessor"></span> <span class="comment">// set configuration (needed especially for windows)</span> <a name="l00485"></a>00485 <span class="comment">// tolerate EBUSY: one device with one configuration, but two interfaces</span> <a name="l00486"></a>00486 <span class="comment">// and libftdi sessions to both interfaces (e.g. FT2232)</span> <a name="l00487"></a>00487 <a name="l00488"></a>00488 <span class="keywordflow">if</span> (dev->descriptor.bNumConfigurations > 0) <a name="l00489"></a>00489 { <a name="l00490"></a>00490 <span class="comment">// libusb-win32 on Windows 64 can return a null pointer for a valid device</span> <a name="l00491"></a>00491 <span class="keywordflow">if</span> (dev->config) <a name="l00492"></a>00492 config_val = dev->config[0].bConfigurationValue; <a name="l00493"></a>00493 <a name="l00494"></a>00494 <span class="keywordflow">if</span> (usb_set_configuration(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, config_val) && <a name="l00495"></a>00495 errno != EBUSY) <a name="l00496"></a>00496 { <a name="l00497"></a>00497 ftdi_usb_close_internal (ftdi); <a name="l00498"></a>00498 <span class="keywordflow">if</span> (detach_errno == EPERM) <a name="l00499"></a>00499 { <a name="l00500"></a>00500 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-8, <span class="stringliteral">"inappropriate permissions on device!"</span>); <a name="l00501"></a>00501 } <a name="l00502"></a>00502 <span class="keywordflow">else</span> <a name="l00503"></a>00503 { <a name="l00504"></a>00504 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"unable to set usb configuration. Make sure the default FTDI driver is not in use"</span>); <a name="l00505"></a>00505 } <a name="l00506"></a>00506 } <a name="l00507"></a>00507 } <a name="l00508"></a>00508 <span class="preprocessor">#endif</span> <a name="l00509"></a>00509 <span class="preprocessor"></span> <a name="l00510"></a>00510 <span class="keywordflow">if</span> (usb_claim_interface(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a>) != 0) <a name="l00511"></a>00511 { <a name="l00512"></a>00512 ftdi_usb_close_internal (ftdi); <a name="l00513"></a>00513 <span class="keywordflow">if</span> (detach_errno == EPERM) <a name="l00514"></a>00514 { <a name="l00515"></a>00515 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-8, <span class="stringliteral">"inappropriate permissions on device!"</span>); <a name="l00516"></a>00516 } <a name="l00517"></a>00517 <span class="keywordflow">else</span> <a name="l00518"></a>00518 { <a name="l00519"></a>00519 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-5, <span class="stringliteral">"unable to claim usb device. Make sure the default FTDI driver is not in use"</span>); <a name="l00520"></a>00520 } <a name="l00521"></a>00521 } <a name="l00522"></a>00522 <a name="l00523"></a>00523 <span class="keywordflow">if</span> (<a class="code" href="group__libftdi.html#ga266ccc351ea31289022063a8ae29edf4">ftdi_usb_reset</a> (ftdi) != 0) <a name="l00524"></a>00524 { <a name="l00525"></a>00525 ftdi_usb_close_internal (ftdi); <a name="l00526"></a>00526 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-6, <span class="stringliteral">"ftdi_usb_reset failed"</span>); <a name="l00527"></a>00527 } <a name="l00528"></a>00528 <a name="l00529"></a>00529 <span class="comment">// Try to guess chip type</span> <a name="l00530"></a>00530 <span class="comment">// Bug in the BM type chips: bcdDevice is 0x200 for serial == 0</span> <a name="l00531"></a>00531 <span class="keywordflow">if</span> (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200 <a name="l00532"></a>00532 && dev->descriptor.iSerialNumber == 0)) <a name="l00533"></a>00533 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea3865c3e28f68be594baec18ca30d2068">TYPE_BM</a>; <a name="l00534"></a>00534 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (dev->descriptor.bcdDevice == 0x200) <a name="l00535"></a>00535 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceac52d52869d9a67db6d6caaeaf6520ba7">TYPE_AM</a>; <a name="l00536"></a>00536 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (dev->descriptor.bcdDevice == 0x500) <a name="l00537"></a>00537 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceaac65bc41b2313c45ee680cefacc84687">TYPE_2232C</a>; <a name="l00538"></a>00538 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (dev->descriptor.bcdDevice == 0x600) <a name="l00539"></a>00539 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>; <a name="l00540"></a>00540 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (dev->descriptor.bcdDevice == 0x700) <a name="l00541"></a>00541 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea1beb33ef9cca96d6a05f8fd7f1fdb92f">TYPE_2232H</a>; <a name="l00542"></a>00542 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (dev->descriptor.bcdDevice == 0x800) <a name="l00543"></a>00543 ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceab327f0179af90c5e1b2f447b6ebb426b">TYPE_4232H</a>; <a name="l00544"></a>00544 <a name="l00545"></a>00545 <span class="comment">// Set default interface on dual/quad type chips</span> <a name="l00546"></a>00546 <span class="keywordflow">switch</span>(ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a>) <a name="l00547"></a>00547 { <a name="l00548"></a>00548 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceaac65bc41b2313c45ee680cefacc84687">TYPE_2232C</a>: <a name="l00549"></a>00549 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea1beb33ef9cca96d6a05f8fd7f1fdb92f">TYPE_2232H</a>: <a name="l00550"></a>00550 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceab327f0179af90c5e1b2f447b6ebb426b">TYPE_4232H</a>: <a name="l00551"></a>00551 <span class="keywordflow">if</span> (!ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>) <a name="l00552"></a>00552 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a> = <a class="code" href="ftdi_8h.html#a9c55e66c45a0e4ec5548ee7d6e1a9265afb60d9e73330c16ba970b3dd169fe4d1">INTERFACE_A</a>; <a name="l00553"></a>00553 <span class="keywordflow">break</span>; <a name="l00554"></a>00554 <span class="keywordflow">default</span>: <a name="l00555"></a>00555 <span class="keywordflow">break</span>; <a name="l00556"></a>00556 } <a name="l00557"></a>00557 <a name="l00558"></a>00558 <span class="comment">// Determine maximum packet size</span> <a name="l00559"></a>00559 ftdi-><a class="code" href="structftdi__context.html#a7ca2f7e68cd28e1c3079d029fd6129c0">max_packet_size</a> = _ftdi_determine_max_packet_size(ftdi, dev); <a name="l00560"></a>00560 <a name="l00561"></a>00561 <span class="keywordflow">if</span> (<a class="code" href="group__libftdi.html#gae80f1dc38f5c80946a56ec34a329e7f5">ftdi_set_baudrate</a> (ftdi, 9600) != 0) <a name="l00562"></a>00562 { <a name="l00563"></a>00563 ftdi_usb_close_internal (ftdi); <a name="l00564"></a>00564 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-7, <span class="stringliteral">"set baudrate failed"</span>); <a name="l00565"></a>00565 } <a name="l00566"></a>00566 <a name="l00567"></a>00567 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(0, <span class="stringliteral">"all fine"</span>); <a name="l00568"></a>00568 } <a name="l00569"></a>00569 <a name="l00579"></a><a class="code" href="group__libftdi.html#ga95d4689db18fc1fd70011f6504ab8c40">00579</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga95d4689db18fc1fd70011f6504ab8c40">ftdi_usb_open</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> vendor, <span class="keywordtype">int</span> product) <a name="l00580"></a>00580 { <a name="l00581"></a>00581 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#ga93c2c5f546f5968ae497c099d319ff32">ftdi_usb_open_desc</a>(ftdi, vendor, product, NULL, NULL); <a name="l00582"></a>00582 } <a name="l00583"></a>00583 <a name="l00606"></a><a class="code" href="group__libftdi.html#ga93c2c5f546f5968ae497c099d319ff32">00606</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga93c2c5f546f5968ae497c099d319ff32">ftdi_usb_open_desc</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> vendor, <span class="keywordtype">int</span> product, <a name="l00607"></a>00607 <span class="keyword">const</span> <span class="keywordtype">char</span>* description, <span class="keyword">const</span> <span class="keywordtype">char</span>* serial) <a name="l00608"></a>00608 { <a name="l00609"></a>00609 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#ga3c5ccb0678b0e268b4081ea978e8eb78">ftdi_usb_open_desc_index</a>(ftdi,vendor,product,description,serial,0); <a name="l00610"></a>00610 } <a name="l00611"></a>00611 <a name="l00636"></a><a class="code" href="group__libftdi.html#ga3c5ccb0678b0e268b4081ea978e8eb78">00636</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga3c5ccb0678b0e268b4081ea978e8eb78">ftdi_usb_open_desc_index</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> vendor, <span class="keywordtype">int</span> product, <a name="l00637"></a>00637 <span class="keyword">const</span> <span class="keywordtype">char</span>* description, <span class="keyword">const</span> <span class="keywordtype">char</span>* serial, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> index) <a name="l00638"></a>00638 { <a name="l00639"></a>00639 <span class="keyword">struct </span>usb_bus *bus; <a name="l00640"></a>00640 <span class="keyword">struct </span>usb_device *dev; <a name="l00641"></a>00641 <span class="keywordtype">char</span> <span class="keywordtype">string</span>[256]; <a name="l00642"></a>00642 <a name="l00643"></a>00643 usb_init(); <a name="l00644"></a>00644 <a name="l00645"></a>00645 <span class="keywordflow">if</span> (usb_find_busses() < 0) <a name="l00646"></a>00646 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"usb_find_busses() failed"</span>); <a name="l00647"></a>00647 <span class="keywordflow">if</span> (usb_find_devices() < 0) <a name="l00648"></a>00648 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"usb_find_devices() failed"</span>); <a name="l00649"></a>00649 <a name="l00650"></a>00650 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00651"></a>00651 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l00652"></a>00652 <a name="l00653"></a>00653 <span class="keywordflow">for</span> (bus = usb_get_busses(); bus; bus = bus->next) <a name="l00654"></a>00654 { <a name="l00655"></a>00655 <span class="keywordflow">for</span> (dev = bus->devices; dev; dev = dev->next) <a name="l00656"></a>00656 { <a name="l00657"></a>00657 <span class="keywordflow">if</span> (dev->descriptor.idVendor == vendor <a name="l00658"></a>00658 && dev->descriptor.idProduct == product) <a name="l00659"></a>00659 { <a name="l00660"></a>00660 <span class="keywordflow">if</span> (!(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> = usb_open(dev))) <a name="l00661"></a>00661 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-4, <span class="stringliteral">"usb_open() failed"</span>); <a name="l00662"></a>00662 <a name="l00663"></a>00663 <span class="keywordflow">if</span> (description != NULL) <a name="l00664"></a>00664 { <a name="l00665"></a>00665 <span class="keywordflow">if</span> (usb_get_string_simple(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, dev->descriptor.iProduct, <span class="keywordtype">string</span>, <span class="keyword">sizeof</span>(<span class="keywordtype">string</span>)) <= 0) <a name="l00666"></a>00666 { <a name="l00667"></a>00667 ftdi_usb_close_internal (ftdi); <a name="l00668"></a>00668 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-8, <span class="stringliteral">"unable to fetch product description"</span>); <a name="l00669"></a>00669 } <a name="l00670"></a>00670 <span class="keywordflow">if</span> (strncmp(<span class="keywordtype">string</span>, description, <span class="keyword">sizeof</span>(<span class="keywordtype">string</span>)) != 0) <a name="l00671"></a>00671 { <a name="l00672"></a>00672 <span class="keywordflow">if</span> (ftdi_usb_close_internal (ftdi) != 0) <a name="l00673"></a>00673 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-10, <span class="stringliteral">"unable to close device"</span>); <a name="l00674"></a>00674 <span class="keywordflow">continue</span>; <a name="l00675"></a>00675 } <a name="l00676"></a>00676 } <a name="l00677"></a>00677 <span class="keywordflow">if</span> (serial != NULL) <a name="l00678"></a>00678 { <a name="l00679"></a>00679 <span class="keywordflow">if</span> (usb_get_string_simple(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, dev->descriptor.iSerialNumber, <span class="keywordtype">string</span>, <span class="keyword">sizeof</span>(<span class="keywordtype">string</span>)) <= 0) <a name="l00680"></a>00680 { <a name="l00681"></a>00681 ftdi_usb_close_internal (ftdi); <a name="l00682"></a>00682 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-9, <span class="stringliteral">"unable to fetch serial number"</span>); <a name="l00683"></a>00683 } <a name="l00684"></a>00684 <span class="keywordflow">if</span> (strncmp(<span class="keywordtype">string</span>, serial, <span class="keyword">sizeof</span>(<span class="keywordtype">string</span>)) != 0) <a name="l00685"></a>00685 { <a name="l00686"></a>00686 <span class="keywordflow">if</span> (ftdi_usb_close_internal (ftdi) != 0) <a name="l00687"></a>00687 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-10, <span class="stringliteral">"unable to close device"</span>); <a name="l00688"></a>00688 <span class="keywordflow">continue</span>; <a name="l00689"></a>00689 } <a name="l00690"></a>00690 } <a name="l00691"></a>00691 <a name="l00692"></a>00692 <span class="keywordflow">if</span> (ftdi_usb_close_internal (ftdi) != 0) <a name="l00693"></a>00693 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-10, <span class="stringliteral">"unable to close device"</span>); <a name="l00694"></a>00694 <a name="l00695"></a>00695 <span class="keywordflow">if</span> (index > 0) <a name="l00696"></a>00696 { <a name="l00697"></a>00697 index--; <a name="l00698"></a>00698 <span class="keywordflow">continue</span>; <a name="l00699"></a>00699 } <a name="l00700"></a>00700 <a name="l00701"></a>00701 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#gaa03eb28a2ec02db4e923e3949854a30d">ftdi_usb_open_dev</a>(ftdi, dev); <a name="l00702"></a>00702 } <a name="l00703"></a>00703 } <a name="l00704"></a>00704 } <a name="l00705"></a>00705 <a name="l00706"></a>00706 <span class="comment">// device not found</span> <a name="l00707"></a>00707 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"device not found"</span>); <a name="l00708"></a>00708 } <a name="l00709"></a>00709 <a name="l00737"></a><a class="code" href="group__libftdi.html#gaae805b82251a61dae46b98345cd84d5c">00737</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gaae805b82251a61dae46b98345cd84d5c">ftdi_usb_open_string</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">const</span> <span class="keywordtype">char</span>* description) <a name="l00738"></a>00738 { <a name="l00739"></a>00739 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00740"></a>00740 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-12, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l00741"></a>00741 <a name="l00742"></a>00742 <span class="keywordflow">if</span> (description[0] == 0 || description[1] != <span class="charliteral">':'</span>) <a name="l00743"></a>00743 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00744"></a>00744 <a name="l00745"></a>00745 <span class="keywordflow">if</span> (description[0] == <span class="charliteral">'d'</span>) <a name="l00746"></a>00746 { <a name="l00747"></a>00747 <span class="keyword">struct </span>usb_bus *bus; <a name="l00748"></a>00748 <span class="keyword">struct </span>usb_device *dev; <a name="l00749"></a>00749 <a name="l00750"></a>00750 usb_init(); <a name="l00751"></a>00751 <a name="l00752"></a>00752 <span class="keywordflow">if</span> (usb_find_busses() < 0) <a name="l00753"></a>00753 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"usb_find_busses() failed"</span>); <a name="l00754"></a>00754 <span class="keywordflow">if</span> (usb_find_devices() < 0) <a name="l00755"></a>00755 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"usb_find_devices() failed"</span>); <a name="l00756"></a>00756 <a name="l00757"></a>00757 <span class="keywordflow">for</span> (bus = usb_get_busses(); bus; bus = bus->next) <a name="l00758"></a>00758 { <a name="l00759"></a>00759 <span class="keywordflow">for</span> (dev = bus->devices; dev; dev = dev->next) <a name="l00760"></a>00760 { <a name="l00761"></a>00761 <span class="comment">/* XXX: This doesn't handle symlinks/odd paths/etc... */</span> <a name="l00762"></a>00762 <span class="keyword">const</span> <span class="keywordtype">char</span> *desc = description + 2; <a name="l00763"></a>00763 <span class="keywordtype">size_t</span> len = strlen(bus->dirname); <a name="l00764"></a>00764 <span class="keywordflow">if</span> (strncmp(desc, bus->dirname, len)) <a name="l00765"></a>00765 <span class="keywordflow">continue</span>; <a name="l00766"></a>00766 desc += len; <a name="l00767"></a>00767 <span class="keywordflow">if</span> (desc[0] != <span class="charliteral">'/'</span>) <a name="l00768"></a>00768 <span class="keywordflow">continue</span>; <a name="l00769"></a>00769 ++desc; <a name="l00770"></a>00770 <span class="keywordflow">if</span> (strcmp(desc, dev->filename)) <a name="l00771"></a>00771 <span class="keywordflow">continue</span>; <a name="l00772"></a>00772 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#gaa03eb28a2ec02db4e923e3949854a30d">ftdi_usb_open_dev</a>(ftdi, dev); <a name="l00773"></a>00773 } <a name="l00774"></a>00774 } <a name="l00775"></a>00775 <a name="l00776"></a>00776 <span class="comment">// device not found</span> <a name="l00777"></a>00777 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"device not found"</span>); <a name="l00778"></a>00778 } <a name="l00779"></a>00779 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (description[0] == <span class="charliteral">'i'</span> || description[0] == <span class="charliteral">'s'</span>) <a name="l00780"></a>00780 { <a name="l00781"></a>00781 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> vendor; <a name="l00782"></a>00782 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> product; <a name="l00783"></a>00783 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> index=0; <a name="l00784"></a>00784 <span class="keyword">const</span> <span class="keywordtype">char</span> *serial=NULL; <a name="l00785"></a>00785 <span class="keyword">const</span> <span class="keywordtype">char</span> *startp, *endp; <a name="l00786"></a>00786 <a name="l00787"></a>00787 errno=0; <a name="l00788"></a>00788 startp=description+2; <a name="l00789"></a>00789 vendor=strtoul((<span class="keywordtype">char</span>*)startp,(<span class="keywordtype">char</span>**)&endp,0); <a name="l00790"></a>00790 <span class="keywordflow">if</span> (*endp != <span class="charliteral">':'</span> || endp == startp || errno != 0) <a name="l00791"></a>00791 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00792"></a>00792 <a name="l00793"></a>00793 startp=endp+1; <a name="l00794"></a>00794 product=strtoul((<span class="keywordtype">char</span>*)startp,(<span class="keywordtype">char</span>**)&endp,0); <a name="l00795"></a>00795 <span class="keywordflow">if</span> (endp == startp || errno != 0) <a name="l00796"></a>00796 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00797"></a>00797 <a name="l00798"></a>00798 <span class="keywordflow">if</span> (description[0] == <span class="charliteral">'i'</span> && *endp != 0) <a name="l00799"></a>00799 { <a name="l00800"></a>00800 <span class="comment">/* optional index field in i-mode */</span> <a name="l00801"></a>00801 <span class="keywordflow">if</span> (*endp != <span class="charliteral">':'</span>) <a name="l00802"></a>00802 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00803"></a>00803 <a name="l00804"></a>00804 startp=endp+1; <a name="l00805"></a>00805 index=strtoul((<span class="keywordtype">char</span>*)startp,(<span class="keywordtype">char</span>**)&endp,0); <a name="l00806"></a>00806 <span class="keywordflow">if</span> (*endp != 0 || endp == startp || errno != 0) <a name="l00807"></a>00807 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00808"></a>00808 } <a name="l00809"></a>00809 <span class="keywordflow">if</span> (description[0] == <span class="charliteral">'s'</span>) <a name="l00810"></a>00810 { <a name="l00811"></a>00811 <span class="keywordflow">if</span> (*endp != <span class="charliteral">':'</span>) <a name="l00812"></a>00812 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00813"></a>00813 <a name="l00814"></a>00814 <span class="comment">/* rest of the description is the serial */</span> <a name="l00815"></a>00815 serial=endp+1; <a name="l00816"></a>00816 } <a name="l00817"></a>00817 <a name="l00818"></a>00818 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#ga3c5ccb0678b0e268b4081ea978e8eb78">ftdi_usb_open_desc_index</a>(ftdi, vendor, product, NULL, serial, index); <a name="l00819"></a>00819 } <a name="l00820"></a>00820 <span class="keywordflow">else</span> <a name="l00821"></a>00821 { <a name="l00822"></a>00822 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-11, <span class="stringliteral">"illegal description format"</span>); <a name="l00823"></a>00823 } <a name="l00824"></a>00824 } <a name="l00825"></a>00825 <a name="l00835"></a><a class="code" href="group__libftdi.html#ga266ccc351ea31289022063a8ae29edf4">00835</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga266ccc351ea31289022063a8ae29edf4">ftdi_usb_reset</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00836"></a>00836 { <a name="l00837"></a>00837 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l00838"></a>00838 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l00839"></a>00839 <a name="l00840"></a>00840 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l00841"></a>00841 <a class="code" href="ftdi_8h.html#a5950bd59bc692e59e8cc410667c21efb">SIO_RESET_REQUEST</a>, <a class="code" href="ftdi_8h.html#ac0a9f9965ee40db86a2e2a0415558ede">SIO_RESET_SIO</a>, <a name="l00842"></a>00842 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l00843"></a>00843 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1,<span class="stringliteral">"FTDI reset failed"</span>); <a name="l00844"></a>00844 <a name="l00845"></a>00845 <span class="comment">// Invalidate data in the readbuffer</span> <a name="l00846"></a>00846 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> = 0; <a name="l00847"></a>00847 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> = 0; <a name="l00848"></a>00848 <a name="l00849"></a>00849 <span class="keywordflow">return</span> 0; <a name="l00850"></a>00850 } <a name="l00851"></a>00851 <a name="l00861"></a><a class="code" href="group__libftdi.html#ga25a6d4766e30bd47602ed0c0ecf53476">00861</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga25a6d4766e30bd47602ed0c0ecf53476">ftdi_usb_purge_rx_buffer</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00862"></a>00862 { <a name="l00863"></a>00863 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l00864"></a>00864 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l00865"></a>00865 <a name="l00866"></a>00866 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l00867"></a>00867 <a class="code" href="ftdi_8h.html#a5950bd59bc692e59e8cc410667c21efb">SIO_RESET_REQUEST</a>, <a class="code" href="ftdi_8h.html#ae61a2c543abcb5d1423e1a4580c785c5">SIO_RESET_PURGE_RX</a>, <a name="l00868"></a>00868 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l00869"></a>00869 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"FTDI purge of RX buffer failed"</span>); <a name="l00870"></a>00870 <a name="l00871"></a>00871 <span class="comment">// Invalidate data in the readbuffer</span> <a name="l00872"></a>00872 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> = 0; <a name="l00873"></a>00873 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> = 0; <a name="l00874"></a>00874 <a name="l00875"></a>00875 <span class="keywordflow">return</span> 0; <a name="l00876"></a>00876 } <a name="l00877"></a>00877 <a name="l00887"></a><a class="code" href="group__libftdi.html#ga1eab1dfd17068c46775a86496fee8bec">00887</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga1eab1dfd17068c46775a86496fee8bec">ftdi_usb_purge_tx_buffer</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00888"></a>00888 { <a name="l00889"></a>00889 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l00890"></a>00890 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l00891"></a>00891 <a name="l00892"></a>00892 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l00893"></a>00893 <a class="code" href="ftdi_8h.html#a5950bd59bc692e59e8cc410667c21efb">SIO_RESET_REQUEST</a>, <a class="code" href="ftdi_8h.html#a318e8a6398bf99f58c1e7fdbaa31f3fb">SIO_RESET_PURGE_TX</a>, <a name="l00894"></a>00894 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l00895"></a>00895 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"FTDI purge of TX buffer failed"</span>); <a name="l00896"></a>00896 <a name="l00897"></a>00897 <span class="keywordflow">return</span> 0; <a name="l00898"></a>00898 } <a name="l00899"></a>00899 <a name="l00910"></a><a class="code" href="group__libftdi.html#ga3089d7ceafad896c007111b8e5c02906">00910</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga3089d7ceafad896c007111b8e5c02906">ftdi_usb_purge_buffers</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00911"></a>00911 { <a name="l00912"></a>00912 <span class="keywordtype">int</span> result; <a name="l00913"></a>00913 <a name="l00914"></a>00914 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l00915"></a>00915 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"USB device unavailable"</span>); <a name="l00916"></a>00916 <a name="l00917"></a>00917 result = <a class="code" href="group__libftdi.html#ga25a6d4766e30bd47602ed0c0ecf53476">ftdi_usb_purge_rx_buffer</a>(ftdi); <a name="l00918"></a>00918 <span class="keywordflow">if</span> (result < 0) <a name="l00919"></a>00919 <span class="keywordflow">return</span> -1; <a name="l00920"></a>00920 <a name="l00921"></a>00921 result = <a class="code" href="group__libftdi.html#ga1eab1dfd17068c46775a86496fee8bec">ftdi_usb_purge_tx_buffer</a>(ftdi); <a name="l00922"></a>00922 <span class="keywordflow">if</span> (result < 0) <a name="l00923"></a>00923 <span class="keywordflow">return</span> -2; <a name="l00924"></a>00924 <a name="l00925"></a>00925 <span class="keywordflow">return</span> 0; <a name="l00926"></a>00926 } <a name="l00927"></a>00927 <a name="l00928"></a>00928 <a name="l00929"></a>00929 <a name="l00940"></a><a class="code" href="group__libftdi.html#ga9536eff48c7ae9313b2a97320743ee3b">00940</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga9536eff48c7ae9313b2a97320743ee3b">ftdi_usb_close</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l00941"></a>00941 { <a name="l00942"></a>00942 <span class="keywordtype">int</span> rtn = 0; <a name="l00943"></a>00943 <a name="l00944"></a>00944 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l00945"></a>00945 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l00946"></a>00946 <a name="l00947"></a>00947 <span class="preprocessor">#ifdef LIBFTDI_LINUX_ASYNC_MODE</span> <a name="l00948"></a>00948 <span class="preprocessor"></span> <span class="comment">/* try to release some kernel resources */</span> <a name="l00949"></a>00949 <a class="code" href="group__libftdi.html#ga76f20e683a65c0c2b368e983b315eb04">ftdi_async_complete</a>(ftdi,1); <a name="l00950"></a>00950 <span class="preprocessor">#endif</span> <a name="l00951"></a>00951 <span class="preprocessor"></span> <a name="l00952"></a>00952 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> != NULL) <a name="l00953"></a>00953 <span class="keywordflow">if</span> (usb_release_interface(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, ftdi-><a class="code" href="structftdi__context.html#ab67b17bd137b3b010544cdc3e6149aa7">interface</a>) != 0) <a name="l00954"></a>00954 rtn = -1; <a name="l00955"></a>00955 <a name="l00956"></a>00956 <span class="keywordflow">if</span> (ftdi_usb_close_internal (ftdi) != 0) <a name="l00957"></a>00957 rtn = -2; <a name="l00958"></a>00958 <a name="l00959"></a>00959 <span class="keywordflow">return</span> rtn; <a name="l00960"></a>00960 } <a name="l00961"></a>00961 <a name="l00967"></a>00967 <span class="keyword">static</span> <span class="keywordtype">int</span> ftdi_convert_baudrate(<span class="keywordtype">int</span> baudrate, <span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <a name="l00968"></a>00968 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> *value, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> *index) <a name="l00969"></a>00969 { <a name="l00970"></a>00970 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; <a name="l00971"></a>00971 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; <a name="l00972"></a>00972 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; <a name="l00973"></a>00973 <span class="keywordtype">int</span> divisor, best_divisor, best_baud, best_baud_diff; <a name="l00974"></a>00974 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> encoded_divisor; <a name="l00975"></a>00975 <span class="keywordtype">int</span> i; <a name="l00976"></a>00976 <a name="l00977"></a>00977 <span class="keywordflow">if</span> (baudrate <= 0) <a name="l00978"></a>00978 { <a name="l00979"></a>00979 <span class="comment">// Return error</span> <a name="l00980"></a>00980 <span class="keywordflow">return</span> -1; <a name="l00981"></a>00981 } <a name="l00982"></a>00982 <a name="l00983"></a>00983 divisor = 24000000 / baudrate; <a name="l00984"></a>00984 <a name="l00985"></a>00985 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceac52d52869d9a67db6d6caaeaf6520ba7">TYPE_AM</a>) <a name="l00986"></a>00986 { <a name="l00987"></a>00987 <span class="comment">// Round down to supported fraction (AM only)</span> <a name="l00988"></a>00988 divisor -= am_adjust_dn[divisor & 7]; <a name="l00989"></a>00989 } <a name="l00990"></a>00990 <a name="l00991"></a>00991 <span class="comment">// Try this divisor and the one above it (because division rounds down)</span> <a name="l00992"></a>00992 best_divisor = 0; <a name="l00993"></a>00993 best_baud = 0; <a name="l00994"></a>00994 best_baud_diff = 0; <a name="l00995"></a>00995 <span class="keywordflow">for</span> (i = 0; i < 2; i++) <a name="l00996"></a>00996 { <a name="l00997"></a>00997 <span class="keywordtype">int</span> try_divisor = divisor + i; <a name="l00998"></a>00998 <span class="keywordtype">int</span> baud_estimate; <a name="l00999"></a>00999 <span class="keywordtype">int</span> baud_diff; <a name="l01000"></a>01000 <a name="l01001"></a>01001 <span class="comment">// Round up to supported divisor value</span> <a name="l01002"></a>01002 <span class="keywordflow">if</span> (try_divisor <= 8) <a name="l01003"></a>01003 { <a name="l01004"></a>01004 <span class="comment">// Round up to minimum supported divisor</span> <a name="l01005"></a>01005 try_divisor = 8; <a name="l01006"></a>01006 } <a name="l01007"></a>01007 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> != <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceac52d52869d9a67db6d6caaeaf6520ba7">TYPE_AM</a> && try_divisor < 12) <a name="l01008"></a>01008 { <a name="l01009"></a>01009 <span class="comment">// BM doesn't support divisors 9 through 11 inclusive</span> <a name="l01010"></a>01010 try_divisor = 12; <a name="l01011"></a>01011 } <a name="l01012"></a>01012 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (divisor < 16) <a name="l01013"></a>01013 { <a name="l01014"></a>01014 <span class="comment">// AM doesn't support divisors 9 through 15 inclusive</span> <a name="l01015"></a>01015 try_divisor = 16; <a name="l01016"></a>01016 } <a name="l01017"></a>01017 <span class="keywordflow">else</span> <a name="l01018"></a>01018 { <a name="l01019"></a>01019 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceac52d52869d9a67db6d6caaeaf6520ba7">TYPE_AM</a>) <a name="l01020"></a>01020 { <a name="l01021"></a>01021 <span class="comment">// Round up to supported fraction (AM only)</span> <a name="l01022"></a>01022 try_divisor += am_adjust_up[try_divisor & 7]; <a name="l01023"></a>01023 <span class="keywordflow">if</span> (try_divisor > 0x1FFF8) <a name="l01024"></a>01024 { <a name="l01025"></a>01025 <span class="comment">// Round down to maximum supported divisor value (for AM)</span> <a name="l01026"></a>01026 try_divisor = 0x1FFF8; <a name="l01027"></a>01027 } <a name="l01028"></a>01028 } <a name="l01029"></a>01029 <span class="keywordflow">else</span> <a name="l01030"></a>01030 { <a name="l01031"></a>01031 <span class="keywordflow">if</span> (try_divisor > 0x1FFFF) <a name="l01032"></a>01032 { <a name="l01033"></a>01033 <span class="comment">// Round down to maximum supported divisor value (for BM)</span> <a name="l01034"></a>01034 try_divisor = 0x1FFFF; <a name="l01035"></a>01035 } <a name="l01036"></a>01036 } <a name="l01037"></a>01037 } <a name="l01038"></a>01038 <span class="comment">// Get estimated baud rate (to nearest integer)</span> <a name="l01039"></a>01039 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; <a name="l01040"></a>01040 <span class="comment">// Get absolute difference from requested baud rate</span> <a name="l01041"></a>01041 <span class="keywordflow">if</span> (baud_estimate < baudrate) <a name="l01042"></a>01042 { <a name="l01043"></a>01043 baud_diff = baudrate - baud_estimate; <a name="l01044"></a>01044 } <a name="l01045"></a>01045 <span class="keywordflow">else</span> <a name="l01046"></a>01046 { <a name="l01047"></a>01047 baud_diff = baud_estimate - baudrate; <a name="l01048"></a>01048 } <a name="l01049"></a>01049 <span class="keywordflow">if</span> (i == 0 || baud_diff < best_baud_diff) <a name="l01050"></a>01050 { <a name="l01051"></a>01051 <span class="comment">// Closest to requested baud rate so far</span> <a name="l01052"></a>01052 best_divisor = try_divisor; <a name="l01053"></a>01053 best_baud = baud_estimate; <a name="l01054"></a>01054 best_baud_diff = baud_diff; <a name="l01055"></a>01055 <span class="keywordflow">if</span> (baud_diff == 0) <a name="l01056"></a>01056 { <a name="l01057"></a>01057 <span class="comment">// Spot on! No point trying</span> <a name="l01058"></a>01058 <span class="keywordflow">break</span>; <a name="l01059"></a>01059 } <a name="l01060"></a>01060 } <a name="l01061"></a>01061 } <a name="l01062"></a>01062 <span class="comment">// Encode the best divisor value</span> <a name="l01063"></a>01063 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); <a name="l01064"></a>01064 <span class="comment">// Deal with special cases for encoded value</span> <a name="l01065"></a>01065 <span class="keywordflow">if</span> (encoded_divisor == 1) <a name="l01066"></a>01066 { <a name="l01067"></a>01067 encoded_divisor = 0; <span class="comment">// 3000000 baud</span> <a name="l01068"></a>01068 } <a name="l01069"></a>01069 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (encoded_divisor == 0x4001) <a name="l01070"></a>01070 { <a name="l01071"></a>01071 encoded_divisor = 1; <span class="comment">// 2000000 baud (BM only)</span> <a name="l01072"></a>01072 } <a name="l01073"></a>01073 <span class="comment">// Split into "value" and "index" values</span> <a name="l01074"></a>01074 *value = (<span class="keywordtype">unsigned</span> short)(encoded_divisor & 0xFFFF); <a name="l01075"></a>01075 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceaac65bc41b2313c45ee680cefacc84687">TYPE_2232C</a> || ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea1beb33ef9cca96d6a05f8fd7f1fdb92f">TYPE_2232H</a> || ftdi-><a class="code" href="structftdi__context.html#a2e565f92e6a559d75cf4828ed46d0109">type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceab327f0179af90c5e1b2f447b6ebb426b">TYPE_4232H</a>) <a name="l01076"></a>01076 { <a name="l01077"></a>01077 *index = (<span class="keywordtype">unsigned</span> short)(encoded_divisor >> 8); <a name="l01078"></a>01078 *index &= 0xFF00; <a name="l01079"></a>01079 *index |= ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>; <a name="l01080"></a>01080 } <a name="l01081"></a>01081 <span class="keywordflow">else</span> <a name="l01082"></a>01082 *index = (<span class="keywordtype">unsigned</span> short)(encoded_divisor >> 16); <a name="l01083"></a>01083 <a name="l01084"></a>01084 <span class="comment">// Return the nearest baud rate</span> <a name="l01085"></a>01085 <span class="keywordflow">return</span> best_baud; <a name="l01086"></a>01086 } <a name="l01087"></a>01087 <a name="l01099"></a><a class="code" href="group__libftdi.html#gae80f1dc38f5c80946a56ec34a329e7f5">01099</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gae80f1dc38f5c80946a56ec34a329e7f5">ftdi_set_baudrate</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> baudrate) <a name="l01100"></a>01100 { <a name="l01101"></a>01101 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> value, index; <a name="l01102"></a>01102 <span class="keywordtype">int</span> actual_baudrate; <a name="l01103"></a>01103 <a name="l01104"></a>01104 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01105"></a>01105 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01106"></a>01106 <a name="l01107"></a>01107 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#a164c17ee7bddaa0d76da8da7138f602f">bitbang_enabled</a>) <a name="l01108"></a>01108 { <a name="l01109"></a>01109 baudrate = baudrate*4; <a name="l01110"></a>01110 } <a name="l01111"></a>01111 <a name="l01112"></a>01112 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); <a name="l01113"></a>01113 <span class="keywordflow">if</span> (actual_baudrate <= 0) <a name="l01114"></a>01114 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a> (-1, <span class="stringliteral">"Silly baudrate <= 0."</span>); <a name="l01115"></a>01115 <a name="l01116"></a>01116 <span class="comment">// Check within tolerance (about 5%)</span> <a name="l01117"></a>01117 <span class="keywordflow">if</span> ((actual_baudrate * 2 < baudrate <span class="comment">/* Catch overflows */</span> ) <a name="l01118"></a>01118 || ((actual_baudrate < baudrate) <a name="l01119"></a>01119 ? (actual_baudrate * 21 < baudrate * 20) <a name="l01120"></a>01120 : (baudrate * 21 < actual_baudrate * 20))) <a name="l01121"></a>01121 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a> (-1, <span class="stringliteral">"Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"</span>); <a name="l01122"></a>01122 <a name="l01123"></a>01123 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l01124"></a>01124 <a class="code" href="ftdi_8h.html#a92906a42c77f2165392071ae422fc068">SIO_SET_BAUDRATE_REQUEST</a>, value, <a name="l01125"></a>01125 index, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01126"></a>01126 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a> (-2, <span class="stringliteral">"Setting new baudrate failed"</span>); <a name="l01127"></a>01127 <a name="l01128"></a>01128 ftdi-><a class="code" href="structftdi__context.html#aa688eb2312af3a9fb6b56a8c59178e34">baudrate</a> = baudrate; <a name="l01129"></a>01129 <span class="keywordflow">return</span> 0; <a name="l01130"></a>01130 } <a name="l01131"></a>01131 <a name="l01145"></a><a class="code" href="group__libftdi.html#ga044fdde91eaa3f557b545172622ee460">01145</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga044fdde91eaa3f557b545172622ee460">ftdi_set_line_property</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#a8007a7d5e37b76307c341051769e5c6f">ftdi_bits_type</a> bits, <a name="l01146"></a>01146 <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#a3977a883c99069e02f97e99d059dc8b8">ftdi_stopbits_type</a> sbit, <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1d">ftdi_parity_type</a> parity) <a name="l01147"></a>01147 { <a name="l01148"></a>01148 <span class="keywordflow">return</span> <a class="code" href="group__libftdi.html#ga481d055a35dd9042c1c23e2214d2655c">ftdi_set_line_property2</a>(ftdi, bits, sbit, parity, <a class="code" href="ftdi_8h.html#ae1ca6c6246df33380443c4ba6f192025a17b671576ab0f99edd941019303ce7a4">BREAK_OFF</a>); <a name="l01149"></a>01149 } <a name="l01150"></a>01150 <a name="l01164"></a><a class="code" href="group__libftdi.html#ga481d055a35dd9042c1c23e2214d2655c">01164</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga481d055a35dd9042c1c23e2214d2655c">ftdi_set_line_property2</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#a8007a7d5e37b76307c341051769e5c6f">ftdi_bits_type</a> bits, <a name="l01165"></a>01165 <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#a3977a883c99069e02f97e99d059dc8b8">ftdi_stopbits_type</a> sbit, <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1d">ftdi_parity_type</a> parity, <a name="l01166"></a>01166 <span class="keyword">enum</span> <a class="code" href="ftdi_8h.html#ae1ca6c6246df33380443c4ba6f192025">ftdi_break_type</a> break_type) <a name="l01167"></a>01167 { <a name="l01168"></a>01168 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> value = bits; <a name="l01169"></a>01169 <a name="l01170"></a>01170 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01171"></a>01171 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01172"></a>01172 <a name="l01173"></a>01173 <span class="keywordflow">switch</span> (parity) <a name="l01174"></a>01174 { <a name="l01175"></a>01175 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1dac157bdf0b85a40d2619cbc8bc1ae5fe2">NONE</a>: <a name="l01176"></a>01176 value |= (0x00 << 8); <a name="l01177"></a>01177 <span class="keywordflow">break</span>; <a name="l01178"></a>01178 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1daa29cedab858353a26006af9db7cd1ed8">ODD</a>: <a name="l01179"></a>01179 value |= (0x01 << 8); <a name="l01180"></a>01180 <span class="keywordflow">break</span>; <a name="l01181"></a>01181 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1da8487756fbc720579906f0ae1738f0fcc">EVEN</a>: <a name="l01182"></a>01182 value |= (0x02 << 8); <a name="l01183"></a>01183 <span class="keywordflow">break</span>; <a name="l01184"></a>01184 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1dab2a979177fd509f5398ec69341f7058f">MARK</a>: <a name="l01185"></a>01185 value |= (0x03 << 8); <a name="l01186"></a>01186 <span class="keywordflow">break</span>; <a name="l01187"></a>01187 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ab0a93b5355c5155dd4fbd5beaf5d5e1dac08dae7edcb5c5bb959fee5971fbad95">SPACE</a>: <a name="l01188"></a>01188 value |= (0x04 << 8); <a name="l01189"></a>01189 <span class="keywordflow">break</span>; <a name="l01190"></a>01190 } <a name="l01191"></a>01191 <a name="l01192"></a>01192 <span class="keywordflow">switch</span> (sbit) <a name="l01193"></a>01193 { <a name="l01194"></a>01194 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a3977a883c99069e02f97e99d059dc8b8a706ed2f39ef4e69314ec3cda847d47f9">STOP_BIT_1</a>: <a name="l01195"></a>01195 value |= (0x00 << 11); <a name="l01196"></a>01196 <span class="keywordflow">break</span>; <a name="l01197"></a>01197 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a3977a883c99069e02f97e99d059dc8b8abe0eab9d50a866c950d3cae314e772ba">STOP_BIT_15</a>: <a name="l01198"></a>01198 value |= (0x01 << 11); <a name="l01199"></a>01199 <span class="keywordflow">break</span>; <a name="l01200"></a>01200 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a3977a883c99069e02f97e99d059dc8b8a9f707b03d4db34dd2d5f346f5ca590c6">STOP_BIT_2</a>: <a name="l01201"></a>01201 value |= (0x02 << 11); <a name="l01202"></a>01202 <span class="keywordflow">break</span>; <a name="l01203"></a>01203 } <a name="l01204"></a>01204 <a name="l01205"></a>01205 <span class="keywordflow">switch</span> (break_type) <a name="l01206"></a>01206 { <a name="l01207"></a>01207 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ae1ca6c6246df33380443c4ba6f192025a17b671576ab0f99edd941019303ce7a4">BREAK_OFF</a>: <a name="l01208"></a>01208 value |= (0x00 << 14); <a name="l01209"></a>01209 <span class="keywordflow">break</span>; <a name="l01210"></a>01210 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#ae1ca6c6246df33380443c4ba6f192025acf4ae3922d90ced9c3c6c4d48c3ef2e3">BREAK_ON</a>: <a name="l01211"></a>01211 value |= (0x01 << 14); <a name="l01212"></a>01212 <span class="keywordflow">break</span>; <a name="l01213"></a>01213 } <a name="l01214"></a>01214 <a name="l01215"></a>01215 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l01216"></a>01216 <a class="code" href="ftdi_8h.html#a3060d361931e004cae4880164192c4ae">SIO_SET_DATA_REQUEST</a>, value, <a name="l01217"></a>01217 ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01218"></a>01218 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a> (-1, <span class="stringliteral">"Setting new line property failed"</span>); <a name="l01219"></a>01219 <a name="l01220"></a>01220 <span class="keywordflow">return</span> 0; <a name="l01221"></a>01221 } <a name="l01222"></a>01222 <a name="l01234"></a><a class="code" href="group__libftdi.html#gaa79550f209ce3327a5df03f2bd1e80f0">01234</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gaa79550f209ce3327a5df03f2bd1e80f0">ftdi_write_data</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> size) <a name="l01235"></a>01235 { <a name="l01236"></a>01236 <span class="keywordtype">int</span> ret; <a name="l01237"></a>01237 <span class="keywordtype">int</span> offset = 0; <a name="l01238"></a>01238 <span class="keywordtype">int</span> total_written = 0; <a name="l01239"></a>01239 <a name="l01240"></a>01240 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01241"></a>01241 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-666, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01242"></a>01242 <a name="l01243"></a>01243 <span class="keywordflow">while</span> (offset < size) <a name="l01244"></a>01244 { <a name="l01245"></a>01245 <span class="keywordtype">int</span> write_size = ftdi-><a class="code" href="structftdi__context.html#a127d66a8b7602bd767fb01ce90e2a6be">writebuffer_chunksize</a>; <a name="l01246"></a>01246 <a name="l01247"></a>01247 <span class="keywordflow">if</span> (offset+write_size > size) <a name="l01248"></a>01248 write_size = size-offset; <a name="l01249"></a>01249 <a name="l01250"></a>01250 ret = usb_bulk_write(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, ftdi-><a class="code" href="structftdi__context.html#a9b6cfd3763a3ca80c76bc52c89dad968">in_ep</a>, buf+offset, write_size, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>); <a name="l01251"></a>01251 <span class="keywordflow">if</span> (ret < 0) <a name="l01252"></a>01252 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(ret, <span class="stringliteral">"usb bulk write failed"</span>); <a name="l01253"></a>01253 <a name="l01254"></a>01254 total_written += ret; <a name="l01255"></a>01255 offset += write_size; <a name="l01256"></a>01256 } <a name="l01257"></a>01257 <a name="l01258"></a>01258 <span class="keywordflow">return</span> total_written; <a name="l01259"></a>01259 } <a name="l01260"></a>01260 <a name="l01261"></a>01261 <span class="preprocessor">#ifdef LIBFTDI_LINUX_ASYNC_MODE</span> <a name="l01262"></a>01262 <span class="preprocessor"></span><span class="preprocessor">#ifdef USB_CLASS_PTP</span> <a name="l01263"></a>01263 <span class="preprocessor"></span><span class="preprocessor">#error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!</span> <a name="l01264"></a>01264 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l01265"></a>01265 <span class="preprocessor"></span><span class="comment">/* this is strongly dependent on libusb using the same struct layout. If libusb</span> <a name="l01266"></a>01266 <span class="comment"> changes in some later version this may break horribly (this is for libusb 0.1.12) */</span> <a name="l01267"></a><a class="code" href="structusb__dev__handle.html">01267</a> <span class="keyword">struct </span><a class="code" href="structusb__dev__handle.html">usb_dev_handle</a> <a name="l01268"></a>01268 { <a name="l01269"></a><a class="code" href="structusb__dev__handle.html#a950bc8d46963caa443fb27cf2ee2b811">01269</a> <span class="keywordtype">int</span> <a class="code" href="structusb__dev__handle.html#a950bc8d46963caa443fb27cf2ee2b811">fd</a>; <a name="l01270"></a>01270 <span class="comment">// some other stuff coming here we don't need</span> <a name="l01271"></a>01271 }; <a name="l01272"></a>01272 <a name="l01277"></a>01277 <span class="keyword">static</span> <span class="keywordtype">int</span> _usb_get_async_urbs_pending(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l01278"></a>01278 { <a name="l01279"></a>01279 <span class="keyword">struct </span>usbdevfs_urb *urb; <a name="l01280"></a>01280 <span class="keywordtype">int</span> pending=0; <a name="l01281"></a>01281 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; <a name="l01282"></a>01282 <a name="l01283"></a>01283 <span class="keywordflow">for</span> (i=0; i < ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>; i++) <a name="l01284"></a>01284 { <a name="l01285"></a>01285 urb=&((<span class="keyword">struct </span>usbdevfs_urb *)(ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a>))[i]; <a name="l01286"></a>01286 <span class="keywordflow">if</span> (urb->usercontext != <a class="code" href="ftdi_8h.html#a40b81fba21b92de527c99f43a0f8b254">FTDI_URB_USERCONTEXT_COOKIE</a>) <a name="l01287"></a>01287 pending++; <a name="l01288"></a>01288 } <a name="l01289"></a>01289 <a name="l01290"></a>01290 <span class="keywordflow">return</span> pending; <a name="l01291"></a>01291 } <a name="l01292"></a>01292 <a name="l01303"></a>01303 <span class="keyword">static</span> <span class="keywordtype">void</span> _usb_async_cleanup(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> wait_for_more, <span class="keywordtype">int</span> timeout_msec) <a name="l01304"></a>01304 { <a name="l01305"></a>01305 <span class="keyword">struct </span>timeval tv; <a name="l01306"></a>01306 <span class="keyword">struct </span>usbdevfs_urb *urb; <a name="l01307"></a>01307 <span class="keywordtype">int</span> ret; <a name="l01308"></a>01308 fd_set writefds; <a name="l01309"></a>01309 <span class="keywordtype">int</span> keep_going=0; <a name="l01310"></a>01310 <a name="l01311"></a>01311 FD_ZERO(&writefds); <a name="l01312"></a>01312 FD_SET(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>-><a class="code" href="structusb__dev__handle.html#a950bc8d46963caa443fb27cf2ee2b811">fd</a>, &writefds); <a name="l01313"></a>01313 <a name="l01314"></a>01314 <span class="comment">/* init timeout only once, select writes time left after call */</span> <a name="l01315"></a>01315 tv.tv_sec = timeout_msec / 1000; <a name="l01316"></a>01316 tv.tv_usec = (timeout_msec % 1000) * 1000; <a name="l01317"></a>01317 <a name="l01318"></a>01318 <span class="keywordflow">do</span> <a name="l01319"></a>01319 { <a name="l01320"></a>01320 ret = -1; <a name="l01321"></a>01321 urb = NULL; <a name="l01322"></a>01322 <a name="l01323"></a>01323 <span class="keywordflow">while</span> (_usb_get_async_urbs_pending(ftdi) <a name="l01324"></a>01324 && (ret = ioctl(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>-><a class="code" href="structusb__dev__handle.html#a950bc8d46963caa443fb27cf2ee2b811">fd</a>, USBDEVFS_REAPURBNDELAY, &urb)) == -1 <a name="l01325"></a>01325 && errno == EAGAIN) <a name="l01326"></a>01326 { <a name="l01327"></a>01327 <span class="keywordflow">if</span> (keep_going && !wait_for_more) <a name="l01328"></a>01328 { <a name="l01329"></a>01329 <span class="comment">/* don't wait if repeating only for keep_going */</span> <a name="l01330"></a>01330 keep_going=0; <a name="l01331"></a>01331 <span class="keywordflow">break</span>; <a name="l01332"></a>01332 } <a name="l01333"></a>01333 <a name="l01334"></a>01334 <span class="comment">/* wait for timeout msec or something written ready */</span> <a name="l01335"></a>01335 select(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>-><a class="code" href="structusb__dev__handle.html#a950bc8d46963caa443fb27cf2ee2b811">fd</a>+1, NULL, &writefds, NULL, &tv); <a name="l01336"></a>01336 } <a name="l01337"></a>01337 <a name="l01338"></a>01338 <span class="keywordflow">if</span> (ret == 0 && urb != NULL) <a name="l01339"></a>01339 { <a name="l01340"></a>01340 <span class="comment">/* got a free urb, mark it */</span> <a name="l01341"></a>01341 urb->usercontext = <a class="code" href="ftdi_8h.html#a40b81fba21b92de527c99f43a0f8b254">FTDI_URB_USERCONTEXT_COOKIE</a>; <a name="l01342"></a>01342 <a name="l01343"></a>01343 <span class="comment">/* try to get more urbs that are ready now, but don't wait anymore */</span> <a name="l01344"></a>01344 keep_going=1; <a name="l01345"></a>01345 } <a name="l01346"></a>01346 <span class="keywordflow">else</span> <a name="l01347"></a>01347 { <a name="l01348"></a>01348 <span class="comment">/* no more urbs waiting */</span> <a name="l01349"></a>01349 keep_going=0; <a name="l01350"></a>01350 } <a name="l01351"></a>01351 } <a name="l01352"></a>01352 <span class="keywordflow">while</span> (keep_going); <a name="l01353"></a>01353 } <a name="l01354"></a>01354 <a name="l01362"></a><a class="code" href="group__libftdi.html#ga76f20e683a65c0c2b368e983b315eb04">01362</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#ga76f20e683a65c0c2b368e983b315eb04">ftdi_async_complete</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> wait_for_more) <a name="l01363"></a>01363 { <a name="l01364"></a>01364 _usb_async_cleanup(ftdi,wait_for_more,ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>); <a name="l01365"></a>01365 } <a name="l01366"></a>01366 <a name="l01372"></a>01372 <span class="keyword">static</span> <span class="keywordtype">int</span> _usb_bulk_write_async(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> ep, <span class="keywordtype">char</span> *bytes, <span class="keywordtype">int</span> size) <a name="l01373"></a>01373 { <a name="l01374"></a>01374 <span class="keyword">struct </span>usbdevfs_urb *urb; <a name="l01375"></a>01375 <span class="keywordtype">int</span> bytesdone = 0, requested; <a name="l01376"></a>01376 <span class="keywordtype">int</span> ret, cleanup_count; <a name="l01377"></a>01377 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; <a name="l01378"></a>01378 <a name="l01379"></a>01379 <span class="keywordflow">do</span> <a name="l01380"></a>01380 { <a name="l01381"></a>01381 <span class="comment">/* find a free urb buffer we can use */</span> <a name="l01382"></a>01382 i = 0; <a name="l01383"></a>01383 urb=NULL; <a name="l01384"></a>01384 <span class="keywordflow">for</span> (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++) <a name="l01385"></a>01385 { <a name="l01386"></a>01386 <span class="keywordflow">if</span> (i==ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>) <a name="l01387"></a>01387 { <a name="l01388"></a>01388 <span class="comment">/* wait until some buffers are free */</span> <a name="l01389"></a>01389 _usb_async_cleanup(ftdi,0,ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>); <a name="l01390"></a>01390 } <a name="l01391"></a>01391 <a name="l01392"></a>01392 <span class="keywordflow">for</span> (i=0; i < ftdi-><a class="code" href="structftdi__context.html#a3e77c45d1ec2faf3242f5a144216d3af">async_usb_buffer_size</a>; i++) <a name="l01393"></a>01393 { <a name="l01394"></a>01394 urb=&((<span class="keyword">struct </span>usbdevfs_urb *)(ftdi-><a class="code" href="structftdi__context.html#a469d9abb16547e15f3bce21e9f7fade6">async_usb_buffer</a>))[i]; <a name="l01395"></a>01395 <span class="keywordflow">if</span> (urb->usercontext == <a class="code" href="ftdi_8h.html#a40b81fba21b92de527c99f43a0f8b254">FTDI_URB_USERCONTEXT_COOKIE</a>) <a name="l01396"></a>01396 <span class="keywordflow">break</span>; <span class="comment">/* found a free urb position */</span> <a name="l01397"></a>01397 urb=NULL; <a name="l01398"></a>01398 } <a name="l01399"></a>01399 } <a name="l01400"></a>01400 <a name="l01401"></a>01401 <span class="comment">/* no free urb position found */</span> <a name="l01402"></a>01402 <span class="keywordflow">if</span> (urb==NULL) <a name="l01403"></a>01403 <span class="keywordflow">return</span> -1; <a name="l01404"></a>01404 <a name="l01405"></a>01405 requested = size - bytesdone; <a name="l01406"></a>01406 <span class="keywordflow">if</span> (requested > 4096) <a name="l01407"></a>01407 requested = 4096; <a name="l01408"></a>01408 <a name="l01409"></a>01409 memset(urb,0,<span class="keyword">sizeof</span>(urb)); <a name="l01410"></a>01410 <a name="l01411"></a>01411 urb->type = USBDEVFS_URB_TYPE_BULK; <a name="l01412"></a>01412 urb->endpoint = ep; <a name="l01413"></a>01413 urb->flags = 0; <a name="l01414"></a>01414 urb->buffer = bytes + bytesdone; <a name="l01415"></a>01415 urb->buffer_length = requested; <a name="l01416"></a>01416 urb->signr = 0; <a name="l01417"></a>01417 urb->actual_length = 0; <a name="l01418"></a>01418 urb->number_of_packets = 0; <a name="l01419"></a>01419 urb->usercontext = 0; <a name="l01420"></a>01420 <a name="l01421"></a>01421 <span class="keywordflow">do</span> <a name="l01422"></a>01422 { <a name="l01423"></a>01423 ret = ioctl(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>-><a class="code" href="structusb__dev__handle.html#a950bc8d46963caa443fb27cf2ee2b811">fd</a>, USBDEVFS_SUBMITURB, urb); <a name="l01424"></a>01424 } <a name="l01425"></a>01425 <span class="keywordflow">while</span> (ret < 0 && errno == EINTR); <a name="l01426"></a>01426 <span class="keywordflow">if</span> (ret < 0) <a name="l01427"></a>01427 <span class="keywordflow">return</span> ret; <span class="comment">/* the caller can read errno to get more info */</span> <a name="l01428"></a>01428 <a name="l01429"></a>01429 bytesdone += requested; <a name="l01430"></a>01430 } <a name="l01431"></a>01431 <span class="keywordflow">while</span> (bytesdone < size); <a name="l01432"></a>01432 <span class="keywordflow">return</span> bytesdone; <a name="l01433"></a>01433 } <a name="l01434"></a>01434 <a name="l01454"></a><a class="code" href="group__libftdi.html#gae08ef89d23158590fa2d9e397b686d4b">01454</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gae08ef89d23158590fa2d9e397b686d4b">ftdi_write_data_async</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> size) <a name="l01455"></a>01455 { <a name="l01456"></a>01456 <span class="keywordtype">int</span> ret; <a name="l01457"></a>01457 <span class="keywordtype">int</span> offset = 0; <a name="l01458"></a>01458 <span class="keywordtype">int</span> total_written = 0; <a name="l01459"></a>01459 <a name="l01460"></a>01460 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01461"></a>01461 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-666, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01462"></a>01462 <a name="l01463"></a>01463 <span class="keywordflow">while</span> (offset < size) <a name="l01464"></a>01464 { <a name="l01465"></a>01465 <span class="keywordtype">int</span> write_size = ftdi-><a class="code" href="structftdi__context.html#a127d66a8b7602bd767fb01ce90e2a6be">writebuffer_chunksize</a>; <a name="l01466"></a>01466 <a name="l01467"></a>01467 <span class="keywordflow">if</span> (offset+write_size > size) <a name="l01468"></a>01468 write_size = size-offset; <a name="l01469"></a>01469 <a name="l01470"></a>01470 ret = _usb_bulk_write_async(ftdi, ftdi-><a class="code" href="structftdi__context.html#a9b6cfd3763a3ca80c76bc52c89dad968">in_ep</a>, buf+offset, write_size); <a name="l01471"></a>01471 <span class="keywordflow">if</span> (ret < 0) <a name="l01472"></a>01472 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(ret, <span class="stringliteral">"usb bulk write async failed"</span>); <a name="l01473"></a>01473 <a name="l01474"></a>01474 total_written += ret; <a name="l01475"></a>01475 offset += write_size; <a name="l01476"></a>01476 } <a name="l01477"></a>01477 <a name="l01478"></a>01478 <span class="keywordflow">return</span> total_written; <a name="l01479"></a>01479 } <a name="l01480"></a>01480 <span class="preprocessor">#endif // LIBFTDI_LINUX_ASYNC_MODE</span> <a name="l01481"></a>01481 <span class="preprocessor"></span> <a name="l01492"></a><a class="code" href="group__libftdi.html#ga85e22717c3315a09219b03a1862c0c46">01492</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga85e22717c3315a09219b03a1862c0c46">ftdi_write_data_set_chunksize</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> chunksize) <a name="l01493"></a>01493 { <a name="l01494"></a>01494 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l01495"></a>01495 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l01496"></a>01496 <a name="l01497"></a>01497 ftdi-><a class="code" href="structftdi__context.html#a127d66a8b7602bd767fb01ce90e2a6be">writebuffer_chunksize</a> = chunksize; <a name="l01498"></a>01498 <span class="keywordflow">return</span> 0; <a name="l01499"></a>01499 } <a name="l01500"></a>01500 <a name="l01510"></a><a class="code" href="group__libftdi.html#ga5944f14ffd11faa113515ff29dc769ed">01510</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga5944f14ffd11faa113515ff29dc769ed">ftdi_write_data_get_chunksize</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> *chunksize) <a name="l01511"></a>01511 { <a name="l01512"></a>01512 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l01513"></a>01513 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l01514"></a>01514 <a name="l01515"></a>01515 *chunksize = ftdi-><a class="code" href="structftdi__context.html#a127d66a8b7602bd767fb01ce90e2a6be">writebuffer_chunksize</a>; <a name="l01516"></a>01516 <span class="keywordflow">return</span> 0; <a name="l01517"></a>01517 } <a name="l01518"></a>01518 <a name="l01535"></a><a class="code" href="group__libftdi.html#ga72d87e30015c98bd0be22e7c8c873345">01535</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga72d87e30015c98bd0be22e7c8c873345">ftdi_read_data</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> size) <a name="l01536"></a>01536 { <a name="l01537"></a>01537 <span class="keywordtype">int</span> offset = 0, ret = 1, i, num_of_chunks, chunk_remains; <a name="l01538"></a>01538 <span class="keywordtype">int</span> packet_size; <a name="l01539"></a>01539 <a name="l01540"></a>01540 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01541"></a>01541 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-666, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01542"></a>01542 <a name="l01543"></a>01543 packet_size = ftdi-><a class="code" href="structftdi__context.html#a7ca2f7e68cd28e1c3079d029fd6129c0">max_packet_size</a>; <a name="l01544"></a>01544 <span class="comment">// Packet size sanity check (avoid division by zero)</span> <a name="l01545"></a>01545 <span class="keywordflow">if</span> (packet_size == 0) <a name="l01546"></a>01546 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"max_packet_size is bogus (zero)"</span>); <a name="l01547"></a>01547 <a name="l01548"></a>01548 <span class="comment">// everything we want is still in the readbuffer?</span> <a name="l01549"></a>01549 <span class="keywordflow">if</span> (size <= ftdi->readbuffer_remaining) <a name="l01550"></a>01550 { <a name="l01551"></a>01551 memcpy (buf, ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>, size); <a name="l01552"></a>01552 <a name="l01553"></a>01553 <span class="comment">// Fix offsets</span> <a name="l01554"></a>01554 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> -= size; <a name="l01555"></a>01555 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> += size; <a name="l01556"></a>01556 <a name="l01557"></a>01557 <span class="comment">/* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */</span> <a name="l01558"></a>01558 <a name="l01559"></a>01559 <span class="keywordflow">return</span> size; <a name="l01560"></a>01560 } <a name="l01561"></a>01561 <span class="comment">// something still in the readbuffer, but not enough to satisfy 'size'?</span> <a name="l01562"></a>01562 <span class="keywordflow">if</span> (ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> != 0) <a name="l01563"></a>01563 { <a name="l01564"></a>01564 memcpy (buf, ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>, ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a>); <a name="l01565"></a>01565 <a name="l01566"></a>01566 <span class="comment">// Fix offset</span> <a name="l01567"></a>01567 offset += ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a>; <a name="l01568"></a>01568 } <a name="l01569"></a>01569 <span class="comment">// do the actual USB read</span> <a name="l01570"></a>01570 <span class="keywordflow">while</span> (offset < size && ret > 0) <a name="l01571"></a>01571 { <a name="l01572"></a>01572 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> = 0; <a name="l01573"></a>01573 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> = 0; <a name="l01574"></a>01574 <span class="comment">/* returns how much received */</span> <a name="l01575"></a>01575 ret = usb_bulk_read (ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, ftdi-><a class="code" href="structftdi__context.html#a079427d8f2c09102bcadb9118e60c0ec">out_ep</a>, ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>, ftdi-><a class="code" href="structftdi__context.html#a8bd3ab7267621b4a8ec3f08ae18b1c00">readbuffer_chunksize</a>, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>); <a name="l01576"></a>01576 <span class="keywordflow">if</span> (ret < 0) <a name="l01577"></a>01577 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(ret, <span class="stringliteral">"usb bulk read failed"</span>); <a name="l01578"></a>01578 <a name="l01579"></a>01579 <span class="keywordflow">if</span> (ret > 2) <a name="l01580"></a>01580 { <a name="l01581"></a>01581 <span class="comment">// skip FTDI status bytes.</span> <a name="l01582"></a>01582 <span class="comment">// Maybe stored in the future to enable modem use</span> <a name="l01583"></a>01583 num_of_chunks = ret / packet_size; <a name="l01584"></a>01584 chunk_remains = ret % packet_size; <a name="l01585"></a>01585 <span class="comment">//printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);</span> <a name="l01586"></a>01586 <a name="l01587"></a>01587 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> += 2; <a name="l01588"></a>01588 ret -= 2; <a name="l01589"></a>01589 <a name="l01590"></a>01590 <span class="keywordflow">if</span> (ret > packet_size - 2) <a name="l01591"></a>01591 { <a name="l01592"></a>01592 <span class="keywordflow">for</span> (i = 1; i < num_of_chunks; i++) <a name="l01593"></a>01593 memmove (ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>+(packet_size - 2)*i, <a name="l01594"></a>01594 ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>+packet_size*i, <a name="l01595"></a>01595 packet_size - 2); <a name="l01596"></a>01596 <span class="keywordflow">if</span> (chunk_remains > 2) <a name="l01597"></a>01597 { <a name="l01598"></a>01598 memmove (ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>+(packet_size - 2)*i, <a name="l01599"></a>01599 ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>+packet_size*i, <a name="l01600"></a>01600 chunk_remains-2); <a name="l01601"></a>01601 ret -= 2*num_of_chunks; <a name="l01602"></a>01602 } <a name="l01603"></a>01603 <span class="keywordflow">else</span> <a name="l01604"></a>01604 ret -= 2*(num_of_chunks-1)+chunk_remains; <a name="l01605"></a>01605 } <a name="l01606"></a>01606 } <a name="l01607"></a>01607 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret <= 2) <a name="l01608"></a>01608 { <a name="l01609"></a>01609 <span class="comment">// no more data to read?</span> <a name="l01610"></a>01610 <span class="keywordflow">return</span> offset; <a name="l01611"></a>01611 } <a name="l01612"></a>01612 <span class="keywordflow">if</span> (ret > 0) <a name="l01613"></a>01613 { <a name="l01614"></a>01614 <span class="comment">// data still fits in buf?</span> <a name="l01615"></a>01615 <span class="keywordflow">if</span> (offset+ret <= size) <a name="l01616"></a>01616 { <a name="l01617"></a>01617 memcpy (buf+offset, ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>, ret); <a name="l01618"></a>01618 <span class="comment">//printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);</span> <a name="l01619"></a>01619 offset += ret; <a name="l01620"></a>01620 <a name="l01621"></a>01621 <span class="comment">/* Did we read exactly the right amount of bytes? */</span> <a name="l01622"></a>01622 <span class="keywordflow">if</span> (offset == size) <a name="l01623"></a>01623 <span class="comment">//printf("read_data exact rem %d offset %d\n",</span> <a name="l01624"></a>01624 <span class="comment">//ftdi->readbuffer_remaining, offset);</span> <a name="l01625"></a>01625 <span class="keywordflow">return</span> offset; <a name="l01626"></a>01626 } <a name="l01627"></a>01627 <span class="keywordflow">else</span> <a name="l01628"></a>01628 { <a name="l01629"></a>01629 <span class="comment">// only copy part of the data or size <= readbuffer_chunksize</span> <a name="l01630"></a>01630 <span class="keywordtype">int</span> part_size = size-offset; <a name="l01631"></a>01631 memcpy (buf+offset, ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>+ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a>, part_size); <a name="l01632"></a>01632 <a name="l01633"></a>01633 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> += part_size; <a name="l01634"></a>01634 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> = ret-part_size; <a name="l01635"></a>01635 offset += part_size; <a name="l01636"></a>01636 <a name="l01637"></a>01637 <span class="comment">/* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",</span> <a name="l01638"></a>01638 <span class="comment"> part_size, size, offset, ret, ftdi->readbuffer_remaining); */</span> <a name="l01639"></a>01639 <a name="l01640"></a>01640 <span class="keywordflow">return</span> offset; <a name="l01641"></a>01641 } <a name="l01642"></a>01642 } <a name="l01643"></a>01643 } <a name="l01644"></a>01644 <span class="comment">// never reached</span> <a name="l01645"></a>01645 <span class="keywordflow">return</span> -127; <a name="l01646"></a>01646 } <a name="l01647"></a>01647 <a name="l01660"></a><a class="code" href="group__libftdi.html#ga65783703baf0828d519ef597bb2855c1">01660</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga65783703baf0828d519ef597bb2855c1">ftdi_read_data_set_chunksize</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> chunksize) <a name="l01661"></a>01661 { <a name="l01662"></a>01662 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *new_buf; <a name="l01663"></a>01663 <a name="l01664"></a>01664 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l01665"></a>01665 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"ftdi context invalid"</span>); <a name="l01666"></a>01666 <a name="l01667"></a>01667 <span class="comment">// Invalidate all remaining data</span> <a name="l01668"></a>01668 ftdi-><a class="code" href="structftdi__context.html#aca6046c8ee0175acf84334afab5a3392">readbuffer_offset</a> = 0; <a name="l01669"></a>01669 ftdi-><a class="code" href="structftdi__context.html#aad05935e91af93e8dbe36781217632bf">readbuffer_remaining</a> = 0; <a name="l01670"></a>01670 <a name="l01671"></a>01671 <span class="keywordflow">if</span> ((new_buf = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)realloc(ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a>, chunksize)) == NULL) <a name="l01672"></a>01672 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"out of memory for readbuffer"</span>); <a name="l01673"></a>01673 <a name="l01674"></a>01674 ftdi-><a class="code" href="structftdi__context.html#a891754b08145eee8669598ab28c985a4">readbuffer</a> = new_buf; <a name="l01675"></a>01675 ftdi-><a class="code" href="structftdi__context.html#a8bd3ab7267621b4a8ec3f08ae18b1c00">readbuffer_chunksize</a> = chunksize; <a name="l01676"></a>01676 <a name="l01677"></a>01677 <span class="keywordflow">return</span> 0; <a name="l01678"></a>01678 } <a name="l01679"></a>01679 <a name="l01689"></a><a class="code" href="group__libftdi.html#gab42b05f26b6a6545ae3ae1b4edc28e5c">01689</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gab42b05f26b6a6545ae3ae1b4edc28e5c">ftdi_read_data_get_chunksize</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> *chunksize) <a name="l01690"></a>01690 { <a name="l01691"></a>01691 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l01692"></a>01692 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"FTDI context invalid"</span>); <a name="l01693"></a>01693 <a name="l01694"></a>01694 *chunksize = ftdi-><a class="code" href="structftdi__context.html#a8bd3ab7267621b4a8ec3f08ae18b1c00">readbuffer_chunksize</a>; <a name="l01695"></a>01695 <span class="keywordflow">return</span> 0; <a name="l01696"></a>01696 } <a name="l01697"></a>01697 <a name="l01698"></a>01698 <a name="l01712"></a><a class="code" href="group__libftdi.html#ga73127091e1d4d50b2b7c0e005c4900f7">01712</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga73127091e1d4d50b2b7c0e005c4900f7">ftdi_enable_bitbang</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> bitmask) <a name="l01713"></a>01713 { <a name="l01714"></a>01714 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l01715"></a>01715 <a name="l01716"></a>01716 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01717"></a>01717 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01718"></a>01718 <a name="l01719"></a>01719 usb_val = bitmask; <span class="comment">// low byte: bitmask</span> <a name="l01720"></a>01720 <span class="comment">/* FT2232C: Set bitbang_mode to 2 to enable SPI */</span> <a name="l01721"></a>01721 usb_val |= (ftdi-><a class="code" href="structftdi__context.html#a017557435f4377c2353d9d3b7fbcff01">bitbang_mode</a> << 8); <a name="l01722"></a>01722 <a name="l01723"></a>01723 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l01724"></a>01724 <a class="code" href="ftdi_8h.html#afe6cb4f5f53d7e21f50479c8847d4d26">SIO_SET_BITMODE_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, <a name="l01725"></a>01725 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01726"></a>01726 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"unable to enter bitbang mode. Perhaps not a BM type chip?"</span>); <a name="l01727"></a>01727 <a name="l01728"></a>01728 ftdi-><a class="code" href="structftdi__context.html#a164c17ee7bddaa0d76da8da7138f602f">bitbang_enabled</a> = 1; <a name="l01729"></a>01729 <span class="keywordflow">return</span> 0; <a name="l01730"></a>01730 } <a name="l01731"></a>01731 <a name="l01741"></a><a class="code" href="group__libftdi.html#ga42b4776b91c6b14d2d1008b160b78efa">01741</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga42b4776b91c6b14d2d1008b160b78efa">ftdi_disable_bitbang</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l01742"></a>01742 { <a name="l01743"></a>01743 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01744"></a>01744 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01745"></a>01745 <a name="l01746"></a>01746 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a class="code" href="ftdi_8h.html#afe6cb4f5f53d7e21f50479c8847d4d26">SIO_SET_BITMODE_REQUEST</a>, 0, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01747"></a>01747 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"unable to leave bitbang mode. Perhaps not a BM type chip?"</span>); <a name="l01748"></a>01748 <a name="l01749"></a>01749 ftdi-><a class="code" href="structftdi__context.html#a164c17ee7bddaa0d76da8da7138f602f">bitbang_enabled</a> = 0; <a name="l01750"></a>01750 <span class="keywordflow">return</span> 0; <a name="l01751"></a>01751 } <a name="l01752"></a>01752 <a name="l01765"></a><a class="code" href="group__libftdi.html#ga83b41e900a1faf2ecc0546ce48ec7e70">01765</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga83b41e900a1faf2ecc0546ce48ec7e70">ftdi_set_bitmode</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> bitmask, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> mode) <a name="l01766"></a>01766 { <a name="l01767"></a>01767 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l01768"></a>01768 <a name="l01769"></a>01769 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01770"></a>01770 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01771"></a>01771 <a name="l01772"></a>01772 usb_val = bitmask; <span class="comment">// low byte: bitmask</span> <a name="l01773"></a>01773 usb_val |= (mode << 8); <a name="l01774"></a>01774 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a class="code" href="ftdi_8h.html#afe6cb4f5f53d7e21f50479c8847d4d26">SIO_SET_BITMODE_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01775"></a>01775 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"unable to configure bitbang mode. Perhaps selected mode not supported on your chip?"</span>); <a name="l01776"></a>01776 <a name="l01777"></a>01777 ftdi-><a class="code" href="structftdi__context.html#a017557435f4377c2353d9d3b7fbcff01">bitbang_mode</a> = mode; <a name="l01778"></a>01778 ftdi-><a class="code" href="structftdi__context.html#a164c17ee7bddaa0d76da8da7138f602f">bitbang_enabled</a> = (mode == <a class="code" href="ftdi_8h.html#a2250f7812c87a96ada7fd4f19b82f666a9a39449cbe4c114e1e6bfa7d699470d0">BITMODE_RESET</a>) ? 0 : 1; <a name="l01779"></a>01779 <span class="keywordflow">return</span> 0; <a name="l01780"></a>01780 } <a name="l01781"></a>01781 <a name="l01792"></a><a class="code" href="group__libftdi.html#gab823dd90f2359612b1546ef0ceebdc0e">01792</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gab823dd90f2359612b1546ef0ceebdc0e">ftdi_read_pins</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *pins) <a name="l01793"></a>01793 { <a name="l01794"></a>01794 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01795"></a>01795 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01796"></a>01796 <a name="l01797"></a>01797 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a6891bba67d5536ca8108a3c5bed01866">SIO_READ_PINS_REQUEST</a>, 0, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, (<span class="keywordtype">char</span> *)pins, 1, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) != 1) <a name="l01798"></a>01798 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"read pins failed"</span>); <a name="l01799"></a>01799 <a name="l01800"></a>01800 <span class="keywordflow">return</span> 0; <a name="l01801"></a>01801 } <a name="l01802"></a>01802 <a name="l01818"></a><a class="code" href="group__libftdi.html#ga3e0227b0c7aa859d2d2afb5ffa4c8466">01818</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga3e0227b0c7aa859d2d2afb5ffa4c8466">ftdi_set_latency_timer</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> latency) <a name="l01819"></a>01819 { <a name="l01820"></a>01820 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l01821"></a>01821 <a name="l01822"></a>01822 <span class="keywordflow">if</span> (latency < 1) <a name="l01823"></a>01823 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"latency out of range. Only valid for 1-255"</span>); <a name="l01824"></a>01824 <a name="l01825"></a>01825 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01826"></a>01826 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-3, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01827"></a>01827 <a name="l01828"></a>01828 usb_val = latency; <a name="l01829"></a>01829 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a6a69ccd99fa8f1d77ba9475a69a3256d">SIO_SET_LATENCY_TIMER_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01830"></a>01830 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"unable to set latency timer"</span>); <a name="l01831"></a>01831 <a name="l01832"></a>01832 <span class="keywordflow">return</span> 0; <a name="l01833"></a>01833 } <a name="l01834"></a>01834 <a name="l01845"></a><a class="code" href="group__libftdi.html#gacf7b6dcc4de33db2263ce8a421e18bfc">01845</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gacf7b6dcc4de33db2263ce8a421e18bfc">ftdi_get_latency_timer</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *latency) <a name="l01846"></a>01846 { <a name="l01847"></a>01847 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l01848"></a>01848 <a name="l01849"></a>01849 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01850"></a>01850 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01851"></a>01851 <a name="l01852"></a>01852 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#ad4c92540084ee84c0eead313a7bbe3fa">SIO_GET_LATENCY_TIMER_REQUEST</a>, 0, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, (<span class="keywordtype">char</span> *)&usb_val, 1, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) != 1) <a name="l01853"></a>01853 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"reading latency timer failed"</span>); <a name="l01854"></a>01854 <a name="l01855"></a>01855 *latency = (<span class="keywordtype">unsigned</span> char)usb_val; <a name="l01856"></a>01856 <span class="keywordflow">return</span> 0; <a name="l01857"></a>01857 } <a name="l01858"></a>01858 <a name="l01899"></a><a class="code" href="group__libftdi.html#ga396c3bf1435fb3317755868e28c47414">01899</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga396c3bf1435fb3317755868e28c47414">ftdi_poll_modem_status</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> *status) <a name="l01900"></a>01900 { <a name="l01901"></a>01901 <span class="keywordtype">char</span> usb_val[2]; <a name="l01902"></a>01902 <a name="l01903"></a>01903 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01904"></a>01904 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01905"></a>01905 <a name="l01906"></a>01906 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#aadec0bf6ad89f4fd94d7a5f5a212aa36">SIO_POLL_MODEM_STATUS_REQUEST</a>, 0, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, usb_val, 2, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) != 2) <a name="l01907"></a>01907 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"getting modem status failed"</span>); <a name="l01908"></a>01908 <a name="l01909"></a>01909 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF); <a name="l01910"></a>01910 <a name="l01911"></a>01911 <span class="keywordflow">return</span> 0; <a name="l01912"></a>01912 } <a name="l01913"></a>01913 <a name="l01925"></a><a class="code" href="group__libftdi.html#gac3b3d2cc69edf567e653340f434c91e0">01925</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gac3b3d2cc69edf567e653340f434c91e0">ftdi_setflowctrl</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> flowctrl) <a name="l01926"></a>01926 { <a name="l01927"></a>01927 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01928"></a>01928 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01929"></a>01929 <a name="l01930"></a>01930 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l01931"></a>01931 <a class="code" href="ftdi_8h.html#ac1f7891448c392bf8b59a99dd9f0cc8b">SIO_SET_FLOW_CTRL_REQUEST</a>, 0, (flowctrl | ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>), <a name="l01932"></a>01932 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01933"></a>01933 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"set flow control failed"</span>); <a name="l01934"></a>01934 <a name="l01935"></a>01935 <span class="keywordflow">return</span> 0; <a name="l01936"></a>01936 } <a name="l01937"></a>01937 <a name="l01948"></a><a class="code" href="group__libftdi.html#ga47761f16762363288c55f5fff24620d5">01948</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga47761f16762363288c55f5fff24620d5">ftdi_setdtr</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> state) <a name="l01949"></a>01949 { <a name="l01950"></a>01950 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l01951"></a>01951 <a name="l01952"></a>01952 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01953"></a>01953 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01954"></a>01954 <a name="l01955"></a>01955 <span class="keywordflow">if</span> (state) <a name="l01956"></a>01956 usb_val = <a class="code" href="ftdi_8h.html#ab93aac18cc98c8601ee233089b1e1874">SIO_SET_DTR_HIGH</a>; <a name="l01957"></a>01957 <span class="keywordflow">else</span> <a name="l01958"></a>01958 usb_val = <a class="code" href="ftdi_8h.html#a0e7b24544b495e4701640d4ea181e29a">SIO_SET_DTR_LOW</a>; <a name="l01959"></a>01959 <a name="l01960"></a>01960 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l01961"></a>01961 <a class="code" href="ftdi_8h.html#a634bc73352c2a79fdea9ded7e19cd7db">SIO_SET_MODEM_CTRL_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, <a name="l01962"></a>01962 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01963"></a>01963 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"set dtr failed"</span>); <a name="l01964"></a>01964 <a name="l01965"></a>01965 <span class="keywordflow">return</span> 0; <a name="l01966"></a>01966 } <a name="l01967"></a>01967 <a name="l01978"></a><a class="code" href="group__libftdi.html#ga5713ba73288b3e1ac1d45096c917c1ab">01978</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga5713ba73288b3e1ac1d45096c917c1ab">ftdi_setrts</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> state) <a name="l01979"></a>01979 { <a name="l01980"></a>01980 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l01981"></a>01981 <a name="l01982"></a>01982 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l01983"></a>01983 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l01984"></a>01984 <a name="l01985"></a>01985 <span class="keywordflow">if</span> (state) <a name="l01986"></a>01986 usb_val = <a class="code" href="ftdi_8h.html#af2a5be61d404ae9d5283420c8146048d">SIO_SET_RTS_HIGH</a>; <a name="l01987"></a>01987 <span class="keywordflow">else</span> <a name="l01988"></a>01988 usb_val = <a class="code" href="ftdi_8h.html#aca30d9bfc0334b28fcc0ab639c4a6cc5">SIO_SET_RTS_LOW</a>; <a name="l01989"></a>01989 <a name="l01990"></a>01990 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l01991"></a>01991 <a class="code" href="ftdi_8h.html#a634bc73352c2a79fdea9ded7e19cd7db">SIO_SET_MODEM_CTRL_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, <a name="l01992"></a>01992 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l01993"></a>01993 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"set of rts failed"</span>); <a name="l01994"></a>01994 <a name="l01995"></a>01995 <span class="keywordflow">return</span> 0; <a name="l01996"></a>01996 } <a name="l01997"></a>01997 <a name="l02009"></a><a class="code" href="group__libftdi.html#ga5a8f23c2e77c8b58c1f44a11e2f52aa9">02009</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga5a8f23c2e77c8b58c1f44a11e2f52aa9">ftdi_setdtr_rts</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> dtr, <span class="keywordtype">int</span> rts) <a name="l02010"></a>02010 { <a name="l02011"></a>02011 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l02012"></a>02012 <a name="l02013"></a>02013 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02014"></a>02014 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02015"></a>02015 <a name="l02016"></a>02016 <span class="keywordflow">if</span> (dtr) <a name="l02017"></a>02017 usb_val = <a class="code" href="ftdi_8h.html#ab93aac18cc98c8601ee233089b1e1874">SIO_SET_DTR_HIGH</a>; <a name="l02018"></a>02018 <span class="keywordflow">else</span> <a name="l02019"></a>02019 usb_val = <a class="code" href="ftdi_8h.html#a0e7b24544b495e4701640d4ea181e29a">SIO_SET_DTR_LOW</a>; <a name="l02020"></a>02020 <a name="l02021"></a>02021 <span class="keywordflow">if</span> (rts) <a name="l02022"></a>02022 usb_val |= <a class="code" href="ftdi_8h.html#af2a5be61d404ae9d5283420c8146048d">SIO_SET_RTS_HIGH</a>; <a name="l02023"></a>02023 <span class="keywordflow">else</span> <a name="l02024"></a>02024 usb_val |= <a class="code" href="ftdi_8h.html#aca30d9bfc0334b28fcc0ab639c4a6cc5">SIO_SET_RTS_LOW</a>; <a name="l02025"></a>02025 <a name="l02026"></a>02026 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l02027"></a>02027 <a class="code" href="ftdi_8h.html#a634bc73352c2a79fdea9ded7e19cd7db">SIO_SET_MODEM_CTRL_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, <a name="l02028"></a>02028 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l02029"></a>02029 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"set of rts/dtr failed"</span>); <a name="l02030"></a>02030 <a name="l02031"></a>02031 <span class="keywordflow">return</span> 0; <a name="l02032"></a>02032 } <a name="l02033"></a>02033 <a name="l02045"></a><a class="code" href="group__libftdi.html#ga71eeabe2ee5474d5c0ceddc2e08766cd">02045</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga71eeabe2ee5474d5c0ceddc2e08766cd">ftdi_set_event_char</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <a name="l02046"></a>02046 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> eventch, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> enable) <a name="l02047"></a>02047 { <a name="l02048"></a>02048 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l02049"></a>02049 <a name="l02050"></a>02050 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02051"></a>02051 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02052"></a>02052 <a name="l02053"></a>02053 usb_val = eventch; <a name="l02054"></a>02054 <span class="keywordflow">if</span> (enable) <a name="l02055"></a>02055 usb_val |= 1 << 8; <a name="l02056"></a>02056 <a name="l02057"></a>02057 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a50f11e536f110d96eb90687d07e481d5">SIO_SET_EVENT_CHAR_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l02058"></a>02058 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"setting event character failed"</span>); <a name="l02059"></a>02059 <a name="l02060"></a>02060 <span class="keywordflow">return</span> 0; <a name="l02061"></a>02061 } <a name="l02062"></a>02062 <a name="l02074"></a><a class="code" href="group__libftdi.html#gaeb9510a61f6648eb1bd57f0f99a390e1">02074</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gaeb9510a61f6648eb1bd57f0f99a390e1">ftdi_set_error_char</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <a name="l02075"></a>02075 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> errorch, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> enable) <a name="l02076"></a>02076 { <a name="l02077"></a>02077 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val; <a name="l02078"></a>02078 <a name="l02079"></a>02079 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02080"></a>02080 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02081"></a>02081 <a name="l02082"></a>02082 usb_val = errorch; <a name="l02083"></a>02083 <span class="keywordflow">if</span> (enable) <a name="l02084"></a>02084 usb_val |= 1 << 8; <a name="l02085"></a>02085 <a name="l02086"></a>02086 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a5c01e46988dff16f8e0754198443d1f1">SIO_SET_ERROR_CHAR_REQUEST</a>, usb_val, ftdi-><a class="code" href="structftdi__context.html#aa38ced8c782ed6844895cd4d685def64">index</a>, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l02087"></a>02087 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"setting error character failed"</span>); <a name="l02088"></a>02088 <a name="l02089"></a>02089 <span class="keywordflow">return</span> 0; <a name="l02090"></a>02090 } <a name="l02091"></a>02091 <a name="l02100"></a><a class="code" href="group__libftdi.html#gaf2ce1365f476463d01d02d5aba5fa818">02100</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#gaf2ce1365f476463d01d02d5aba5fa818">ftdi_eeprom_setsize</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keyword">struct</span> <a class="code" href="structftdi__eeprom.html" title="FTDI eeprom structure.">ftdi_eeprom</a> *eeprom, <span class="keywordtype">int</span> size) <a name="l02101"></a>02101 { <a name="l02102"></a>02102 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l02103"></a>02103 <span class="keywordflow">return</span>; <a name="l02104"></a>02104 <a name="l02105"></a>02105 ftdi-><a class="code" href="structftdi__context.html#ad53e310b3c70a57d36d34b85d7c67550">eeprom_size</a>=size; <a name="l02106"></a>02106 eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>=size; <a name="l02107"></a>02107 } <a name="l02108"></a>02108 <a name="l02114"></a><a class="code" href="group__libftdi.html#ga3302c00605356730893c0b11ef0dba03">02114</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#ga3302c00605356730893c0b11ef0dba03">ftdi_eeprom_initdefaults</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__eeprom.html" title="FTDI eeprom structure.">ftdi_eeprom</a> *eeprom) <a name="l02115"></a>02115 { <a name="l02116"></a>02116 <span class="keywordtype">int</span> i; <a name="l02117"></a>02117 <a name="l02118"></a>02118 <span class="keywordflow">if</span> (eeprom == NULL) <a name="l02119"></a>02119 <span class="keywordflow">return</span>; <a name="l02120"></a>02120 <a name="l02121"></a>02121 eeprom-><a class="code" href="structftdi__eeprom.html#af591e992b9bd12a484ea9d4d36e92251">vendor_id</a> = 0x0403; <a name="l02122"></a>02122 eeprom-><a class="code" href="structftdi__eeprom.html#ab273d6fe056b3871824486ad0bfa3a90">product_id</a> = 0x6001; <a name="l02123"></a>02123 <a name="l02124"></a>02124 eeprom-><a class="code" href="structftdi__eeprom.html#a055d6bef2328557e6ac4d2909e42d21b">self_powered</a> = 1; <a name="l02125"></a>02125 eeprom-><a class="code" href="structftdi__eeprom.html#a9b82f88497b432bebfa4dd92c304cb02">remote_wakeup</a> = 1; <a name="l02126"></a>02126 eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea3865c3e28f68be594baec18ca30d2068">TYPE_BM</a>; <a name="l02127"></a>02127 <a name="l02128"></a>02128 eeprom-><a class="code" href="structftdi__eeprom.html#a5117b50c00668aff73b5df385b0fc5f8">in_is_isochronous</a> = 0; <a name="l02129"></a>02129 eeprom-><a class="code" href="structftdi__eeprom.html#ab9885ee665c318a8d19c28a9c75b999a">out_is_isochronous</a> = 0; <a name="l02130"></a>02130 eeprom-><a class="code" href="structftdi__eeprom.html#a722a630d370318cefa94de1365f98786">suspend_pull_downs</a> = 0; <a name="l02131"></a>02131 <a name="l02132"></a>02132 eeprom-><a class="code" href="structftdi__eeprom.html#abe250275595d020fd8de3aa61d093d38">use_serial</a> = 0; <a name="l02133"></a>02133 eeprom-><a class="code" href="structftdi__eeprom.html#a335d8b4733288ee270cd01c5b31eb1b7">change_usb_version</a> = 0; <a name="l02134"></a>02134 eeprom-><a class="code" href="structftdi__eeprom.html#a7c0093a9bfbdf7a3c89e18228fcaba84">usb_version</a> = 0x0200; <a name="l02135"></a>02135 eeprom-><a class="code" href="structftdi__eeprom.html#a8aa39af43a6ac5c05b8682d8b3904e3c">max_power</a> = 0; <a name="l02136"></a>02136 <a name="l02137"></a>02137 eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a> = NULL; <a name="l02138"></a>02138 eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a> = NULL; <a name="l02139"></a>02139 eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a> = NULL; <a name="l02140"></a>02140 <span class="keywordflow">for</span> (i=0; i < 5; i++) <a name="l02141"></a>02141 { <a name="l02142"></a>02142 eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[i] = 0; <a name="l02143"></a>02143 } <a name="l02144"></a>02144 eeprom-><a class="code" href="structftdi__eeprom.html#ad377261e985c768c2ca5731e9690efd9">high_current</a> = 0; <a name="l02145"></a>02145 eeprom-><a class="code" href="structftdi__eeprom.html#a4876d3d01242d7c3b2961b1a42b35509">invert</a> = 0; <a name="l02146"></a>02146 <a name="l02147"></a>02147 eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a> = <a class="code" href="ftdi_8h.html#a1d8794aafb35cdc72bf0d2f01d4063b9">FTDI_DEFAULT_EEPROM_SIZE</a>; <a name="l02148"></a>02148 } <a name="l02149"></a>02149 <a name="l02155"></a><a class="code" href="group__libftdi.html#gaae86ddcceb7f95ac9d974fd20e10ccdf">02155</a> <span class="keywordtype">void</span> <a class="code" href="group__libftdi.html#gaae86ddcceb7f95ac9d974fd20e10ccdf">ftdi_eeprom_free</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__eeprom.html" title="FTDI eeprom structure.">ftdi_eeprom</a> *eeprom) <a name="l02156"></a>02156 { <a name="l02157"></a>02157 <span class="keywordflow">if</span> (!eeprom) <a name="l02158"></a>02158 <span class="keywordflow">return</span>; <a name="l02159"></a>02159 <a name="l02160"></a>02160 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a> != 0) { <a name="l02161"></a>02161 free(eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a>); <a name="l02162"></a>02162 eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a> = 0; <a name="l02163"></a>02163 } <a name="l02164"></a>02164 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a> != 0) { <a name="l02165"></a>02165 free(eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a>); <a name="l02166"></a>02166 eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a> = 0; <a name="l02167"></a>02167 } <a name="l02168"></a>02168 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a> != 0) { <a name="l02169"></a>02169 free(eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a>); <a name="l02170"></a>02170 eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a> = 0; <a name="l02171"></a>02171 } <a name="l02172"></a>02172 } <a name="l02173"></a>02173 <a name="l02189"></a><a class="code" href="group__libftdi.html#ga6e80677242c262c58169ea5565ae9163">02189</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga6e80677242c262c58169ea5565ae9163">ftdi_eeprom_build</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__eeprom.html" title="FTDI eeprom structure.">ftdi_eeprom</a> *eeprom, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *output) <a name="l02190"></a>02190 { <a name="l02191"></a>02191 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> i, j; <a name="l02192"></a>02192 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> checksum, value; <a name="l02193"></a>02193 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> manufacturer_size = 0, product_size = 0, serial_size = 0; <a name="l02194"></a>02194 <span class="keywordtype">int</span> size_check; <a name="l02195"></a>02195 <span class="keyword">const</span> <span class="keywordtype">int</span> cbus_max[5] = {13, 13, 13, 13, 9}; <a name="l02196"></a>02196 <a name="l02197"></a>02197 <span class="keywordflow">if</span> (eeprom == NULL) <a name="l02198"></a>02198 <span class="keywordflow">return</span> -2; <a name="l02199"></a>02199 <a name="l02200"></a>02200 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a> != NULL) <a name="l02201"></a>02201 manufacturer_size = strlen(eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a>); <a name="l02202"></a>02202 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a> != NULL) <a name="l02203"></a>02203 product_size = strlen(eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a>); <a name="l02204"></a>02204 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a> != NULL) <a name="l02205"></a>02205 serial_size = strlen(eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a>); <a name="l02206"></a>02206 <a name="l02207"></a>02207 <span class="comment">// highest allowed cbus value</span> <a name="l02208"></a>02208 <span class="keywordflow">for</span> (i = 0; i < 5; i++) <a name="l02209"></a>02209 { <a name="l02210"></a>02210 <span class="keywordflow">if</span> ((eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[i] > cbus_max[i]) || <a name="l02211"></a>02211 (eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[i] && eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> != <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>)) <span class="keywordflow">return</span> -3; <a name="l02212"></a>02212 } <a name="l02213"></a>02213 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> != <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>) <a name="l02214"></a>02214 { <a name="l02215"></a>02215 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a4876d3d01242d7c3b2961b1a42b35509">invert</a>) <span class="keywordflow">return</span> -4; <a name="l02216"></a>02216 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#ad377261e985c768c2ca5731e9690efd9">high_current</a>) <span class="keywordflow">return</span> -5; <a name="l02217"></a>02217 } <a name="l02218"></a>02218 <a name="l02219"></a>02219 size_check = eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>; <a name="l02220"></a>02220 size_check -= 28; <span class="comment">// 28 are always in use (fixed)</span> <a name="l02221"></a>02221 <a name="l02222"></a>02222 <span class="comment">// Top half of a 256byte eeprom is used just for strings and checksum</span> <a name="l02223"></a>02223 <span class="comment">// it seems that the FTDI chip will not read these strings from the lower half</span> <a name="l02224"></a>02224 <span class="comment">// Each string starts with two bytes; offset and type (0x03 for string)</span> <a name="l02225"></a>02225 <span class="comment">// the checksum needs two bytes, so without the string data that 8 bytes from the top half</span> <a name="l02226"></a>02226 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>>=256) size_check = 120; <a name="l02227"></a>02227 size_check -= manufacturer_size*2; <a name="l02228"></a>02228 size_check -= product_size*2; <a name="l02229"></a>02229 size_check -= serial_size*2; <a name="l02230"></a>02230 <a name="l02231"></a>02231 <span class="comment">// eeprom size exceeded?</span> <a name="l02232"></a>02232 <span class="keywordflow">if</span> (size_check < 0) <a name="l02233"></a>02233 <span class="keywordflow">return</span> (-1); <a name="l02234"></a>02234 <a name="l02235"></a>02235 <span class="comment">// empty eeprom</span> <a name="l02236"></a>02236 memset (output, 0, eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>); <a name="l02237"></a>02237 <a name="l02238"></a>02238 <span class="comment">// Addr 00: High current IO</span> <a name="l02239"></a>02239 output[0x00] = eeprom-><a class="code" href="structftdi__eeprom.html#ad377261e985c768c2ca5731e9690efd9">high_current</a> ? <a class="code" href="ftdi_8h.html#a0b41f0434b3f10219bfbfc0ae02d60a0">HIGH_CURRENT_DRIVE</a> : 0; <a name="l02240"></a>02240 <span class="comment">// Addr 01: IN endpoint size (for R type devices, different for FT2232)</span> <a name="l02241"></a>02241 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>) { <a name="l02242"></a>02242 output[0x01] = 0x40; <a name="l02243"></a>02243 } <a name="l02244"></a>02244 <span class="comment">// Addr 02: Vendor ID</span> <a name="l02245"></a>02245 output[0x02] = eeprom-><a class="code" href="structftdi__eeprom.html#af591e992b9bd12a484ea9d4d36e92251">vendor_id</a>; <a name="l02246"></a>02246 output[0x03] = eeprom-><a class="code" href="structftdi__eeprom.html#af591e992b9bd12a484ea9d4d36e92251">vendor_id</a> >> 8; <a name="l02247"></a>02247 <a name="l02248"></a>02248 <span class="comment">// Addr 04: Product ID</span> <a name="l02249"></a>02249 output[0x04] = eeprom-><a class="code" href="structftdi__eeprom.html#ab273d6fe056b3871824486ad0bfa3a90">product_id</a>; <a name="l02250"></a>02250 output[0x05] = eeprom-><a class="code" href="structftdi__eeprom.html#ab273d6fe056b3871824486ad0bfa3a90">product_id</a> >> 8; <a name="l02251"></a>02251 <a name="l02252"></a>02252 <span class="comment">// Addr 06: Device release number (0400h for BM features)</span> <a name="l02253"></a>02253 output[0x06] = 0x00; <a name="l02254"></a>02254 <span class="keywordflow">switch</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a>) { <a name="l02255"></a>02255 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceac52d52869d9a67db6d6caaeaf6520ba7">TYPE_AM</a>: <a name="l02256"></a>02256 output[0x07] = 0x02; <a name="l02257"></a>02257 <span class="keywordflow">break</span>; <a name="l02258"></a>02258 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea3865c3e28f68be594baec18ca30d2068">TYPE_BM</a>: <a name="l02259"></a>02259 output[0x07] = 0x04; <a name="l02260"></a>02260 <span class="keywordflow">break</span>; <a name="l02261"></a>02261 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceaac65bc41b2313c45ee680cefacc84687">TYPE_2232C</a>: <a name="l02262"></a>02262 output[0x07] = 0x05; <a name="l02263"></a>02263 <span class="keywordflow">break</span>; <a name="l02264"></a>02264 <span class="keywordflow">case</span> <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>: <a name="l02265"></a>02265 output[0x07] = 0x06; <a name="l02266"></a>02266 <span class="keywordflow">break</span>; <a name="l02267"></a>02267 <span class="keywordflow">default</span>: <a name="l02268"></a>02268 output[0x07] = 0x00; <a name="l02269"></a>02269 } <a name="l02270"></a>02270 <a name="l02271"></a>02271 <span class="comment">// Addr 08: Config descriptor</span> <a name="l02272"></a>02272 <span class="comment">// Bit 7: always 1</span> <a name="l02273"></a>02273 <span class="comment">// Bit 6: 1 if this device is self powered, 0 if bus powered</span> <a name="l02274"></a>02274 <span class="comment">// Bit 5: 1 if this device uses remote wakeup</span> <a name="l02275"></a>02275 <span class="comment">// Bit 4: 1 if this device is battery powered</span> <a name="l02276"></a>02276 j = 0x80; <a name="l02277"></a>02277 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a055d6bef2328557e6ac4d2909e42d21b">self_powered</a> == 1) <a name="l02278"></a>02278 j |= 0x40; <a name="l02279"></a>02279 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a9b82f88497b432bebfa4dd92c304cb02">remote_wakeup</a> == 1) <a name="l02280"></a>02280 j |= 0x20; <a name="l02281"></a>02281 output[0x08] = j; <a name="l02282"></a>02282 <a name="l02283"></a>02283 <span class="comment">// Addr 09: Max power consumption: max power = value * 2 mA</span> <a name="l02284"></a>02284 output[0x09] = eeprom-><a class="code" href="structftdi__eeprom.html#a8aa39af43a6ac5c05b8682d8b3904e3c">max_power</a>; <a name="l02285"></a>02285 <a name="l02286"></a>02286 <span class="comment">// Addr 0A: Chip configuration</span> <a name="l02287"></a>02287 <span class="comment">// Bit 7: 0 - reserved</span> <a name="l02288"></a>02288 <span class="comment">// Bit 6: 0 - reserved</span> <a name="l02289"></a>02289 <span class="comment">// Bit 5: 0 - reserved</span> <a name="l02290"></a>02290 <span class="comment">// Bit 4: 1 - Change USB version</span> <a name="l02291"></a>02291 <span class="comment">// Bit 3: 1 - Use the serial number string</span> <a name="l02292"></a>02292 <span class="comment">// Bit 2: 1 - Enable suspend pull downs for lower power</span> <a name="l02293"></a>02293 <span class="comment">// Bit 1: 1 - Out EndPoint is Isochronous</span> <a name="l02294"></a>02294 <span class="comment">// Bit 0: 1 - In EndPoint is Isochronous</span> <a name="l02295"></a>02295 <span class="comment">//</span> <a name="l02296"></a>02296 j = 0; <a name="l02297"></a>02297 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a5117b50c00668aff73b5df385b0fc5f8">in_is_isochronous</a> == 1) <a name="l02298"></a>02298 j = j | 1; <a name="l02299"></a>02299 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#ab9885ee665c318a8d19c28a9c75b999a">out_is_isochronous</a> == 1) <a name="l02300"></a>02300 j = j | 2; <a name="l02301"></a>02301 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a722a630d370318cefa94de1365f98786">suspend_pull_downs</a> == 1) <a name="l02302"></a>02302 j = j | 4; <a name="l02303"></a>02303 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#abe250275595d020fd8de3aa61d093d38">use_serial</a> == 1) <a name="l02304"></a>02304 j = j | 8; <a name="l02305"></a>02305 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a335d8b4733288ee270cd01c5b31eb1b7">change_usb_version</a> == 1) <a name="l02306"></a>02306 j = j | 16; <a name="l02307"></a>02307 output[0x0A] = j; <a name="l02308"></a>02308 <a name="l02309"></a>02309 <span class="comment">// Addr 0B: Invert data lines</span> <a name="l02310"></a>02310 output[0x0B] = eeprom-><a class="code" href="structftdi__eeprom.html#a4876d3d01242d7c3b2961b1a42b35509">invert</a> & 0xff; <a name="l02311"></a>02311 <a name="l02312"></a>02312 <span class="comment">// Addr 0C: USB version low byte when 0x0A bit 4 is set</span> <a name="l02313"></a>02313 <span class="comment">// Addr 0D: USB version high byte when 0x0A bit 4 is set</span> <a name="l02314"></a>02314 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a335d8b4733288ee270cd01c5b31eb1b7">change_usb_version</a> == 1) <a name="l02315"></a>02315 { <a name="l02316"></a>02316 output[0x0C] = eeprom-><a class="code" href="structftdi__eeprom.html#a7c0093a9bfbdf7a3c89e18228fcaba84">usb_version</a>; <a name="l02317"></a>02317 output[0x0D] = eeprom-><a class="code" href="structftdi__eeprom.html#a7c0093a9bfbdf7a3c89e18228fcaba84">usb_version</a> >> 8; <a name="l02318"></a>02318 } <a name="l02319"></a>02319 <a name="l02320"></a>02320 <a name="l02321"></a>02321 <span class="comment">// Addr 0E: Offset of the manufacturer string + 0x80, calculated later</span> <a name="l02322"></a>02322 <span class="comment">// Addr 0F: Length of manufacturer string</span> <a name="l02323"></a>02323 output[0x0F] = manufacturer_size*2 + 2; <a name="l02324"></a>02324 <a name="l02325"></a>02325 <span class="comment">// Addr 10: Offset of the product string + 0x80, calculated later</span> <a name="l02326"></a>02326 <span class="comment">// Addr 11: Length of product string</span> <a name="l02327"></a>02327 output[0x11] = product_size*2 + 2; <a name="l02328"></a>02328 <a name="l02329"></a>02329 <span class="comment">// Addr 12: Offset of the serial string + 0x80, calculated later</span> <a name="l02330"></a>02330 <span class="comment">// Addr 13: Length of serial string</span> <a name="l02331"></a>02331 output[0x13] = serial_size*2 + 2; <a name="l02332"></a>02332 <a name="l02333"></a>02333 <span class="comment">// Addr 14: CBUS function: CBUS0, CBUS1</span> <a name="l02334"></a>02334 <span class="comment">// Addr 15: CBUS function: CBUS2, CBUS3</span> <a name="l02335"></a>02335 <span class="comment">// Addr 16: CBUS function: CBUS5</span> <a name="l02336"></a>02336 output[0x14] = eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[0] | (eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[1] << 4); <a name="l02337"></a>02337 output[0x15] = eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[2] | (eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[3] << 4); <a name="l02338"></a>02338 output[0x16] = eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[4]; <a name="l02339"></a>02339 <span class="comment">// Addr 17: Unknown</span> <a name="l02340"></a>02340 <a name="l02341"></a>02341 <span class="comment">// Dynamic content</span> <a name="l02342"></a>02342 <span class="comment">// In images produced by FTDI's FT_Prog for FT232R strings start at 0x18</span> <a name="l02343"></a>02343 <span class="comment">// Space till 0x18 should be considered as reserved.</span> <a name="l02344"></a>02344 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> >= <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>) { <a name="l02345"></a>02345 i = 0x18; <a name="l02346"></a>02346 } <span class="keywordflow">else</span> { <a name="l02347"></a>02347 i = 0x14; <a name="l02348"></a>02348 } <a name="l02349"></a>02349 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a> >= 256) i = 0x80; <a name="l02350"></a>02350 <a name="l02351"></a>02351 <a name="l02352"></a>02352 <span class="comment">// Output manufacturer</span> <a name="l02353"></a>02353 output[0x0E] = i | 0x80; <span class="comment">// calculate offset</span> <a name="l02354"></a>02354 output[i++] = manufacturer_size*2 + 2; <a name="l02355"></a>02355 output[i++] = 0x03; <span class="comment">// type: string</span> <a name="l02356"></a>02356 <span class="keywordflow">for</span> (j = 0; j < manufacturer_size; j++) <a name="l02357"></a>02357 { <a name="l02358"></a>02358 output[i] = eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a>[j], i++; <a name="l02359"></a>02359 output[i] = 0x00, i++; <a name="l02360"></a>02360 } <a name="l02361"></a>02361 <a name="l02362"></a>02362 <span class="comment">// Output product name</span> <a name="l02363"></a>02363 output[0x10] = i | 0x80; <span class="comment">// calculate offset</span> <a name="l02364"></a>02364 output[i] = product_size*2 + 2, i++; <a name="l02365"></a>02365 output[i] = 0x03, i++; <a name="l02366"></a>02366 <span class="keywordflow">for</span> (j = 0; j < product_size; j++) <a name="l02367"></a>02367 { <a name="l02368"></a>02368 output[i] = eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a>[j], i++; <a name="l02369"></a>02369 output[i] = 0x00, i++; <a name="l02370"></a>02370 } <a name="l02371"></a>02371 <a name="l02372"></a>02372 <span class="comment">// Output serial</span> <a name="l02373"></a>02373 output[0x12] = i | 0x80; <span class="comment">// calculate offset</span> <a name="l02374"></a>02374 output[i] = serial_size*2 + 2, i++; <a name="l02375"></a>02375 output[i] = 0x03, i++; <a name="l02376"></a>02376 <span class="keywordflow">for</span> (j = 0; j < serial_size; j++) <a name="l02377"></a>02377 { <a name="l02378"></a>02378 output[i] = eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a>[j], i++; <a name="l02379"></a>02379 output[i] = 0x00, i++; <a name="l02380"></a>02380 } <a name="l02381"></a>02381 <a name="l02382"></a>02382 <span class="comment">// calculate checksum</span> <a name="l02383"></a>02383 checksum = 0xAAAA; <a name="l02384"></a>02384 <a name="l02385"></a>02385 <span class="keywordflow">for</span> (i = 0; i < eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>/2-1; i++) <a name="l02386"></a>02386 { <a name="l02387"></a>02387 value = output[i*2]; <a name="l02388"></a>02388 value += output[(i*2)+1] << 8; <a name="l02389"></a>02389 <a name="l02390"></a>02390 checksum = value^checksum; <a name="l02391"></a>02391 checksum = (checksum << 1) | (checksum >> 15); <a name="l02392"></a>02392 } <a name="l02393"></a>02393 <a name="l02394"></a>02394 output[eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>-2] = checksum; <a name="l02395"></a>02395 output[eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>-1] = checksum >> 8; <a name="l02396"></a>02396 <a name="l02397"></a>02397 <span class="keywordflow">return</span> size_check; <a name="l02398"></a>02398 } <a name="l02399"></a>02399 <a name="l02413"></a><a class="code" href="group__libftdi.html#ga03beffe9080d466d70c90ce75fec26c9">02413</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga03beffe9080d466d70c90ce75fec26c9">ftdi_eeprom_decode</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__eeprom.html" title="FTDI eeprom structure.">ftdi_eeprom</a> *eeprom, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> size) <a name="l02414"></a>02414 { <a name="l02415"></a>02415 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> i, j; <a name="l02416"></a>02416 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> checksum, eeprom_checksum, value; <a name="l02417"></a>02417 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> manufacturer_size = 0, product_size = 0, serial_size = 0; <a name="l02418"></a>02418 <span class="keywordtype">int</span> size_check; <a name="l02419"></a>02419 <span class="keywordtype">int</span> eeprom_size = 128; <a name="l02420"></a>02420 <a name="l02421"></a>02421 <span class="keywordflow">if</span> (eeprom == NULL) <a name="l02422"></a>02422 <span class="keywordflow">return</span> -1; <a name="l02423"></a>02423 <span class="preprocessor">#if 0</span> <a name="l02424"></a>02424 <span class="preprocessor"></span> size_check = eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>; <a name="l02425"></a>02425 size_check -= 28; <span class="comment">// 28 are always in use (fixed)</span> <a name="l02426"></a>02426 <a name="l02427"></a>02427 <span class="comment">// Top half of a 256byte eeprom is used just for strings and checksum</span> <a name="l02428"></a>02428 <span class="comment">// it seems that the FTDI chip will not read these strings from the lower half</span> <a name="l02429"></a>02429 <span class="comment">// Each string starts with two bytes; offset and type (0x03 for string)</span> <a name="l02430"></a>02430 <span class="comment">// the checksum needs two bytes, so without the string data that 8 bytes from the top half</span> <a name="l02431"></a>02431 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a6d45e714bee4069f7f44e184d6f5ce60">size</a>>=256)size_check = 120; <a name="l02432"></a>02432 size_check -= manufacturer_size*2; <a name="l02433"></a>02433 size_check -= product_size*2; <a name="l02434"></a>02434 size_check -= serial_size*2; <a name="l02435"></a>02435 <a name="l02436"></a>02436 <span class="comment">// eeprom size exceeded?</span> <a name="l02437"></a>02437 <span class="keywordflow">if</span> (size_check < 0) <a name="l02438"></a>02438 <span class="keywordflow">return</span> (-1); <a name="l02439"></a>02439 <span class="preprocessor">#endif</span> <a name="l02440"></a>02440 <span class="preprocessor"></span> <a name="l02441"></a>02441 <span class="comment">// empty eeprom struct</span> <a name="l02442"></a>02442 memset(eeprom, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structftdi__eeprom.html" title="FTDI eeprom structure.">ftdi_eeprom</a>)); <a name="l02443"></a>02443 <a name="l02444"></a>02444 <span class="comment">// Addr 00: High current IO</span> <a name="l02445"></a>02445 eeprom-><a class="code" href="structftdi__eeprom.html#ad377261e985c768c2ca5731e9690efd9">high_current</a> = (buf[0x02] & <a class="code" href="ftdi_8h.html#a0b41f0434b3f10219bfbfc0ae02d60a0">HIGH_CURRENT_DRIVE</a>); <a name="l02446"></a>02446 <a name="l02447"></a>02447 <span class="comment">// Addr 02: Vendor ID</span> <a name="l02448"></a>02448 eeprom-><a class="code" href="structftdi__eeprom.html#af591e992b9bd12a484ea9d4d36e92251">vendor_id</a> = buf[0x02] + (buf[0x03] << 8); <a name="l02449"></a>02449 <a name="l02450"></a>02450 <span class="comment">// Addr 04: Product ID</span> <a name="l02451"></a>02451 eeprom-><a class="code" href="structftdi__eeprom.html#ab273d6fe056b3871824486ad0bfa3a90">product_id</a> = buf[0x04] + (buf[0x05] << 8); <a name="l02452"></a>02452 <a name="l02453"></a>02453 value = buf[0x06] + (buf[0x07]<<8); <a name="l02454"></a>02454 <span class="keywordflow">switch</span> (value) <a name="l02455"></a>02455 { <a name="l02456"></a>02456 <span class="keywordflow">case</span> 0x0600: <a name="l02457"></a>02457 eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>; <a name="l02458"></a>02458 <span class="keywordflow">break</span>; <a name="l02459"></a>02459 <span class="keywordflow">case</span> 0x0400: <a name="l02460"></a>02460 eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea3865c3e28f68be594baec18ca30d2068">TYPE_BM</a>; <a name="l02461"></a>02461 <span class="keywordflow">break</span>; <a name="l02462"></a>02462 <span class="keywordflow">case</span> 0x0200: <a name="l02463"></a>02463 eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> = <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385ceac52d52869d9a67db6d6caaeaf6520ba7">TYPE_AM</a>; <a name="l02464"></a>02464 <span class="keywordflow">break</span>; <a name="l02465"></a>02465 <span class="keywordflow">default</span>: <span class="comment">// Unknown device</span> <a name="l02466"></a>02466 eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> = 0; <a name="l02467"></a>02467 <span class="keywordflow">break</span>; <a name="l02468"></a>02468 } <a name="l02469"></a>02469 <a name="l02470"></a>02470 <span class="comment">// Addr 08: Config descriptor</span> <a name="l02471"></a>02471 <span class="comment">// Bit 7: always 1</span> <a name="l02472"></a>02472 <span class="comment">// Bit 6: 1 if this device is self powered, 0 if bus powered</span> <a name="l02473"></a>02473 <span class="comment">// Bit 5: 1 if this device uses remote wakeup</span> <a name="l02474"></a>02474 <span class="comment">// Bit 4: 1 if this device is battery powered</span> <a name="l02475"></a>02475 j = buf[0x08]; <a name="l02476"></a>02476 <span class="keywordflow">if</span> (j&0x40) eeprom-><a class="code" href="structftdi__eeprom.html#a055d6bef2328557e6ac4d2909e42d21b">self_powered</a> = 1; <a name="l02477"></a>02477 <span class="keywordflow">if</span> (j&0x20) eeprom-><a class="code" href="structftdi__eeprom.html#a9b82f88497b432bebfa4dd92c304cb02">remote_wakeup</a> = 1; <a name="l02478"></a>02478 <a name="l02479"></a>02479 <span class="comment">// Addr 09: Max power consumption: max power = value * 2 mA</span> <a name="l02480"></a>02480 eeprom-><a class="code" href="structftdi__eeprom.html#a8aa39af43a6ac5c05b8682d8b3904e3c">max_power</a> = buf[0x09]; <a name="l02481"></a>02481 <a name="l02482"></a>02482 <span class="comment">// Addr 0A: Chip configuration</span> <a name="l02483"></a>02483 <span class="comment">// Bit 7: 0 - reserved</span> <a name="l02484"></a>02484 <span class="comment">// Bit 6: 0 - reserved</span> <a name="l02485"></a>02485 <span class="comment">// Bit 5: 0 - reserved</span> <a name="l02486"></a>02486 <span class="comment">// Bit 4: 1 - Change USB version</span> <a name="l02487"></a>02487 <span class="comment">// Bit 3: 1 - Use the serial number string</span> <a name="l02488"></a>02488 <span class="comment">// Bit 2: 1 - Enable suspend pull downs for lower power</span> <a name="l02489"></a>02489 <span class="comment">// Bit 1: 1 - Out EndPoint is Isochronous</span> <a name="l02490"></a>02490 <span class="comment">// Bit 0: 1 - In EndPoint is Isochronous</span> <a name="l02491"></a>02491 <span class="comment">//</span> <a name="l02492"></a>02492 j = buf[0x0A]; <a name="l02493"></a>02493 <span class="keywordflow">if</span> (j&0x01) eeprom-><a class="code" href="structftdi__eeprom.html#a5117b50c00668aff73b5df385b0fc5f8">in_is_isochronous</a> = 1; <a name="l02494"></a>02494 <span class="keywordflow">if</span> (j&0x02) eeprom-><a class="code" href="structftdi__eeprom.html#ab9885ee665c318a8d19c28a9c75b999a">out_is_isochronous</a> = 1; <a name="l02495"></a>02495 <span class="keywordflow">if</span> (j&0x04) eeprom-><a class="code" href="structftdi__eeprom.html#a722a630d370318cefa94de1365f98786">suspend_pull_downs</a> = 1; <a name="l02496"></a>02496 <span class="keywordflow">if</span> (j&0x08) eeprom-><a class="code" href="structftdi__eeprom.html#abe250275595d020fd8de3aa61d093d38">use_serial</a> = 1; <a name="l02497"></a>02497 <span class="keywordflow">if</span> (j&0x10) eeprom-><a class="code" href="structftdi__eeprom.html#a335d8b4733288ee270cd01c5b31eb1b7">change_usb_version</a> = 1; <a name="l02498"></a>02498 <a name="l02499"></a>02499 <span class="comment">// Addr 0B: Invert data lines</span> <a name="l02500"></a>02500 eeprom-><a class="code" href="structftdi__eeprom.html#a4876d3d01242d7c3b2961b1a42b35509">invert</a> = buf[0x0B]; <a name="l02501"></a>02501 <a name="l02502"></a>02502 <span class="comment">// Addr 0C: USB version low byte when 0x0A bit 4 is set</span> <a name="l02503"></a>02503 <span class="comment">// Addr 0D: USB version high byte when 0x0A bit 4 is set</span> <a name="l02504"></a>02504 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a335d8b4733288ee270cd01c5b31eb1b7">change_usb_version</a> == 1) <a name="l02505"></a>02505 { <a name="l02506"></a>02506 eeprom-><a class="code" href="structftdi__eeprom.html#a7c0093a9bfbdf7a3c89e18228fcaba84">usb_version</a> = buf[0x0C] + (buf[0x0D] << 8); <a name="l02507"></a>02507 } <a name="l02508"></a>02508 <a name="l02509"></a>02509 <span class="comment">// Addr 0E: Offset of the manufacturer string + 0x80, calculated later</span> <a name="l02510"></a>02510 <span class="comment">// Addr 0F: Length of manufacturer string</span> <a name="l02511"></a>02511 manufacturer_size = buf[0x0F]/2; <a name="l02512"></a>02512 <span class="keywordflow">if</span> (manufacturer_size > 0) eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a> = malloc(manufacturer_size); <a name="l02513"></a>02513 <span class="keywordflow">else</span> eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a> = NULL; <a name="l02514"></a>02514 <a name="l02515"></a>02515 <span class="comment">// Addr 10: Offset of the product string + 0x80, calculated later</span> <a name="l02516"></a>02516 <span class="comment">// Addr 11: Length of product string</span> <a name="l02517"></a>02517 product_size = buf[0x11]/2; <a name="l02518"></a>02518 <span class="keywordflow">if</span> (product_size > 0) eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a> = malloc(product_size); <a name="l02519"></a>02519 <span class="keywordflow">else</span> eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a> = NULL; <a name="l02520"></a>02520 <a name="l02521"></a>02521 <span class="comment">// Addr 12: Offset of the serial string + 0x80, calculated later</span> <a name="l02522"></a>02522 <span class="comment">// Addr 13: Length of serial string</span> <a name="l02523"></a>02523 serial_size = buf[0x13]/2; <a name="l02524"></a>02524 <span class="keywordflow">if</span> (serial_size > 0) eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a> = malloc(serial_size); <a name="l02525"></a>02525 <span class="keywordflow">else</span> eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a> = NULL; <a name="l02526"></a>02526 <a name="l02527"></a>02527 <span class="comment">// Addr 14: CBUS function: CBUS0, CBUS1</span> <a name="l02528"></a>02528 <span class="comment">// Addr 15: CBUS function: CBUS2, CBUS3</span> <a name="l02529"></a>02529 <span class="comment">// Addr 16: CBUS function: CBUS5</span> <a name="l02530"></a>02530 <span class="keywordflow">if</span> (eeprom-><a class="code" href="structftdi__eeprom.html#a0eeba93de1cafe9e4547b5ec7106ec75">chip_type</a> == <a class="code" href="ftdi_8h.html#a026dc3f26fbc7f5ef67c75f0739385cea60949f421ca5df446c9fb4322093bc8a">TYPE_R</a>) { <a name="l02531"></a>02531 eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[0] = buf[0x14] & 0x0f; <a name="l02532"></a>02532 eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[1] = (buf[0x14] >> 4) & 0x0f; <a name="l02533"></a>02533 eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[2] = buf[0x15] & 0x0f; <a name="l02534"></a>02534 eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[3] = (buf[0x15] >> 4) & 0x0f; <a name="l02535"></a>02535 eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[4] = buf[0x16] & 0x0f; <a name="l02536"></a>02536 } <span class="keywordflow">else</span> { <a name="l02537"></a>02537 <span class="keywordflow">for</span> (j=0; j<5; j++) eeprom-><a class="code" href="structftdi__eeprom.html#a22818e44822d0664206469e99d78c644">cbus_function</a>[j] = 0; <a name="l02538"></a>02538 } <a name="l02539"></a>02539 <a name="l02540"></a>02540 <span class="comment">// Decode manufacturer</span> <a name="l02541"></a>02541 i = buf[0x0E] & 0x7f; <span class="comment">// offset</span> <a name="l02542"></a>02542 <span class="keywordflow">for</span> (j=0;j<manufacturer_size-1;j++) <a name="l02543"></a>02543 { <a name="l02544"></a>02544 eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a>[j] = buf[2*j+i+2]; <a name="l02545"></a>02545 } <a name="l02546"></a>02546 eeprom-><a class="code" href="structftdi__eeprom.html#ac4656922906039cab49d4c3c52d2916b">manufacturer</a>[j] = <span class="charliteral">'\0'</span>; <a name="l02547"></a>02547 <a name="l02548"></a>02548 <span class="comment">// Decode product name</span> <a name="l02549"></a>02549 i = buf[0x10] & 0x7f; <span class="comment">// offset</span> <a name="l02550"></a>02550 <span class="keywordflow">for</span> (j=0;j<product_size-1;j++) <a name="l02551"></a>02551 { <a name="l02552"></a>02552 eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a>[j] = buf[2*j+i+2]; <a name="l02553"></a>02553 } <a name="l02554"></a>02554 eeprom-><a class="code" href="structftdi__eeprom.html#aa24461721904d41198603dda94e0e8ec">product</a>[j] = <span class="charliteral">'\0'</span>; <a name="l02555"></a>02555 <a name="l02556"></a>02556 <span class="comment">// Decode serial</span> <a name="l02557"></a>02557 i = buf[0x12] & 0x7f; <span class="comment">// offset</span> <a name="l02558"></a>02558 <span class="keywordflow">for</span> (j=0;j<serial_size-1;j++) <a name="l02559"></a>02559 { <a name="l02560"></a>02560 eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a>[j] = buf[2*j+i+2]; <a name="l02561"></a>02561 } <a name="l02562"></a>02562 eeprom-><a class="code" href="structftdi__eeprom.html#aadcbc36d5b0569821b66dc0e0b0c61d1">serial</a>[j] = <span class="charliteral">'\0'</span>; <a name="l02563"></a>02563 <a name="l02564"></a>02564 <span class="comment">// verify checksum</span> <a name="l02565"></a>02565 checksum = 0xAAAA; <a name="l02566"></a>02566 <a name="l02567"></a>02567 <span class="keywordflow">for</span> (i = 0; i < eeprom_size/2-1; i++) <a name="l02568"></a>02568 { <a name="l02569"></a>02569 value = buf[i*2]; <a name="l02570"></a>02570 value += buf[(i*2)+1] << 8; <a name="l02571"></a>02571 <a name="l02572"></a>02572 checksum = value^checksum; <a name="l02573"></a>02573 checksum = (checksum << 1) | (checksum >> 15); <a name="l02574"></a>02574 } <a name="l02575"></a>02575 <a name="l02576"></a>02576 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8); <a name="l02577"></a>02577 <a name="l02578"></a>02578 <span class="keywordflow">if</span> (eeprom_checksum != checksum) <a name="l02579"></a>02579 { <a name="l02580"></a>02580 fprintf(stderr, <span class="stringliteral">"Checksum Error: %04x %04x\n"</span>, checksum, eeprom_checksum); <a name="l02581"></a>02581 <span class="keywordflow">return</span> -1; <a name="l02582"></a>02582 } <a name="l02583"></a>02583 <a name="l02584"></a>02584 <span class="keywordflow">return</span> 0; <a name="l02585"></a>02585 } <a name="l02586"></a>02586 <a name="l02598"></a><a class="code" href="group__libftdi.html#ga650df1dc49ec78f0ee58b1c4f2644fd9">02598</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga650df1dc49ec78f0ee58b1c4f2644fd9">ftdi_read_eeprom_location</a> (<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> eeprom_addr, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> *eeprom_val) <a name="l02599"></a>02599 { <a name="l02600"></a>02600 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02601"></a>02601 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02602"></a>02602 <a name="l02603"></a>02603 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a9942db0e999f3b8549488d2346b55ad5">SIO_READ_EEPROM_REQUEST</a>, 0, eeprom_addr, (<span class="keywordtype">char</span> *)eeprom_val, 2, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) != 2) <a name="l02604"></a>02604 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"reading eeprom failed"</span>); <a name="l02605"></a>02605 <a name="l02606"></a>02606 <span class="keywordflow">return</span> 0; <a name="l02607"></a>02607 } <a name="l02608"></a>02608 <a name="l02619"></a><a class="code" href="group__libftdi.html#gac2607b0a26dd8d78431d30a3fd01c052">02619</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gac2607b0a26dd8d78431d30a3fd01c052">ftdi_read_eeprom</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *eeprom) <a name="l02620"></a>02620 { <a name="l02621"></a>02621 <span class="keywordtype">int</span> i; <a name="l02622"></a>02622 <a name="l02623"></a>02623 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02624"></a>02624 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02625"></a>02625 <a name="l02626"></a>02626 <span class="keywordflow">for</span> (i = 0; i < ftdi-><a class="code" href="structftdi__context.html#ad53e310b3c70a57d36d34b85d7c67550">eeprom_size</a>/2; i++) <a name="l02627"></a>02627 { <a name="l02628"></a>02628 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a9942db0e999f3b8549488d2346b55ad5">SIO_READ_EEPROM_REQUEST</a>, 0, i, eeprom+(i*2), 2, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) != 2) <a name="l02629"></a>02629 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"reading eeprom failed"</span>); <a name="l02630"></a>02630 } <a name="l02631"></a>02631 <a name="l02632"></a>02632 <span class="keywordflow">return</span> 0; <a name="l02633"></a>02633 } <a name="l02634"></a>02634 <a name="l02635"></a>02635 <span class="comment">/*</span> <a name="l02636"></a>02636 <span class="comment"> ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID</span> <a name="l02637"></a>02637 <span class="comment"> Function is only used internally</span> <a name="l02638"></a>02638 <span class="comment"> \internal</span> <a name="l02639"></a>02639 <span class="comment">*/</span> <a name="l02640"></a>02640 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> ftdi_read_chipid_shift(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> value) <a name="l02641"></a>02641 { <a name="l02642"></a>02642 <span class="keywordflow">return</span> ((value & 1) << 1) | <a name="l02643"></a>02643 ((value & 2) << 5) | <a name="l02644"></a>02644 ((value & 4) >> 2) | <a name="l02645"></a>02645 ((value & 8) << 4) | <a name="l02646"></a>02646 ((value & 16) >> 1) | <a name="l02647"></a>02647 ((value & 32) >> 1) | <a name="l02648"></a>02648 ((value & 64) >> 4) | <a name="l02649"></a>02649 ((value & 128) >> 2); <a name="l02650"></a>02650 } <a name="l02651"></a>02651 <a name="l02662"></a><a class="code" href="group__libftdi.html#gab978a76c2cc0dbfbc1fd6350db848300">02662</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gab978a76c2cc0dbfbc1fd6350db848300">ftdi_read_chipid</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> *chipid) <a name="l02663"></a>02663 { <a name="l02664"></a>02664 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> a = 0, b = 0; <a name="l02665"></a>02665 <a name="l02666"></a>02666 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02667"></a>02667 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02668"></a>02668 <a name="l02669"></a>02669 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a9942db0e999f3b8549488d2346b55ad5">SIO_READ_EEPROM_REQUEST</a>, 0, 0x43, (<span class="keywordtype">char</span> *)&a, 2, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) == 2) <a name="l02670"></a>02670 { <a name="l02671"></a>02671 a = a << 8 | a >> 8; <a name="l02672"></a>02672 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a9942db0e999f3b8549488d2346b55ad5">SIO_READ_EEPROM_REQUEST</a>, 0, 0x44, (<span class="keywordtype">char</span> *)&b, 2, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) == 2) <a name="l02673"></a>02673 { <a name="l02674"></a>02674 b = b << 8 | b >> 8; <a name="l02675"></a>02675 a = (a << 16) | (b & 0xFFFF); <a name="l02676"></a>02676 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8 <a name="l02677"></a>02677 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24; <a name="l02678"></a>02678 *chipid = a ^ 0xa5f0f7d1; <a name="l02679"></a>02679 <span class="keywordflow">return</span> 0; <a name="l02680"></a>02680 } <a name="l02681"></a>02681 } <a name="l02682"></a>02682 <a name="l02683"></a>02683 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"read of FTDIChip-ID failed"</span>); <a name="l02684"></a>02684 } <a name="l02685"></a>02685 <a name="l02698"></a><a class="code" href="group__libftdi.html#gaf9fc6da50167f7160024aca768c8dccc">02698</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#gaf9fc6da50167f7160024aca768c8dccc">ftdi_read_eeprom_getsize</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *eeprom, <span class="keywordtype">int</span> maxsize) <a name="l02699"></a>02699 { <a name="l02700"></a>02700 <span class="keywordtype">int</span> i=0,j,minsize=32; <a name="l02701"></a>02701 <span class="keywordtype">int</span> size=minsize; <a name="l02702"></a>02702 <a name="l02703"></a>02703 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02704"></a>02704 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02705"></a>02705 <a name="l02706"></a>02706 <span class="keywordflow">do</span> <a name="l02707"></a>02707 { <a name="l02708"></a>02708 <span class="keywordflow">for</span> (j = 0; i < maxsize/2 && j<size; j++) <a name="l02709"></a>02709 { <a name="l02710"></a>02710 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#ac07e5b0c33c6c87470b991700b5f49dd">FTDI_DEVICE_IN_REQTYPE</a>, <a name="l02711"></a>02711 <a class="code" href="ftdi_8h.html#a9942db0e999f3b8549488d2346b55ad5">SIO_READ_EEPROM_REQUEST</a>, 0, i, <a name="l02712"></a>02712 eeprom+(i*2), 2, ftdi-><a class="code" href="structftdi__context.html#afa159483bb1153259ee7ceb3b45a2c9f">usb_read_timeout</a>) != 2) <a name="l02713"></a>02713 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"eeprom read failed"</span>); <a name="l02714"></a>02714 i++; <a name="l02715"></a>02715 } <a name="l02716"></a>02716 size*=2; <a name="l02717"></a>02717 } <a name="l02718"></a>02718 <span class="keywordflow">while</span> (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0); <a name="l02719"></a>02719 <a name="l02720"></a>02720 <span class="keywordflow">return</span> size/2; <a name="l02721"></a>02721 } <a name="l02722"></a>02722 <a name="l02734"></a><a class="code" href="group__libftdi.html#ga5f2f678fc05cb8f92708fb1e5eb75100">02734</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga5f2f678fc05cb8f92708fb1e5eb75100">ftdi_write_eeprom_location</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">int</span> eeprom_addr, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> eeprom_val) <a name="l02735"></a>02735 { <a name="l02736"></a>02736 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02737"></a>02737 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02738"></a>02738 <a name="l02739"></a>02739 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l02740"></a>02740 <a class="code" href="ftdi_8h.html#aaea5180ed6c97e8ab5c3d50c16b04076">SIO_WRITE_EEPROM_REQUEST</a>, eeprom_val, eeprom_addr, <a name="l02741"></a>02741 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l02742"></a>02742 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"unable to write eeprom"</span>); <a name="l02743"></a>02743 <a name="l02744"></a>02744 <span class="keywordflow">return</span> 0; <a name="l02745"></a>02745 } <a name="l02746"></a>02746 <a name="l02757"></a><a class="code" href="group__libftdi.html#ga28e530f10b046bcc353aa75938bddb31">02757</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga28e530f10b046bcc353aa75938bddb31">ftdi_write_eeprom</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *eeprom) <a name="l02758"></a>02758 { <a name="l02759"></a>02759 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> usb_val, status; <a name="l02760"></a>02760 <span class="keywordtype">int</span> i, ret; <a name="l02761"></a>02761 <a name="l02762"></a>02762 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02763"></a>02763 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02764"></a>02764 <a name="l02765"></a>02765 <span class="comment">/* These commands were traced while running MProg */</span> <a name="l02766"></a>02766 <span class="keywordflow">if</span> ((ret = <a class="code" href="group__libftdi.html#ga266ccc351ea31289022063a8ae29edf4">ftdi_usb_reset</a>(ftdi)) != 0) <a name="l02767"></a>02767 <span class="keywordflow">return</span> ret; <a name="l02768"></a>02768 <span class="keywordflow">if</span> ((ret = <a class="code" href="group__libftdi.html#ga396c3bf1435fb3317755868e28c47414">ftdi_poll_modem_status</a>(ftdi, &status)) != 0) <a name="l02769"></a>02769 <span class="keywordflow">return</span> ret; <a name="l02770"></a>02770 <span class="keywordflow">if</span> ((ret = <a class="code" href="group__libftdi.html#ga3e0227b0c7aa859d2d2afb5ffa4c8466">ftdi_set_latency_timer</a>(ftdi, 0x77)) != 0) <a name="l02771"></a>02771 <span class="keywordflow">return</span> ret; <a name="l02772"></a>02772 <a name="l02773"></a>02773 <span class="keywordflow">for</span> (i = 0; i < ftdi-><a class="code" href="structftdi__context.html#ad53e310b3c70a57d36d34b85d7c67550">eeprom_size</a>/2; i++) <a name="l02774"></a>02774 { <a name="l02775"></a>02775 usb_val = eeprom[i*2]; <a name="l02776"></a>02776 usb_val += eeprom[(i*2)+1] << 8; <a name="l02777"></a>02777 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a name="l02778"></a>02778 <a class="code" href="ftdi_8h.html#aaea5180ed6c97e8ab5c3d50c16b04076">SIO_WRITE_EEPROM_REQUEST</a>, usb_val, i, <a name="l02779"></a>02779 NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l02780"></a>02780 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"unable to write eeprom"</span>); <a name="l02781"></a>02781 } <a name="l02782"></a>02782 <a name="l02783"></a>02783 <span class="keywordflow">return</span> 0; <a name="l02784"></a>02784 } <a name="l02785"></a>02785 <a name="l02797"></a><a class="code" href="group__libftdi.html#ga25eb9a83a925065b6c3b61b8f0565951">02797</a> <span class="keywordtype">int</span> <a class="code" href="group__libftdi.html#ga25eb9a83a925065b6c3b61b8f0565951">ftdi_erase_eeprom</a>(<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l02798"></a>02798 { <a name="l02799"></a>02799 <span class="keywordflow">if</span> (ftdi == NULL || ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a> == NULL) <a name="l02800"></a>02800 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-2, <span class="stringliteral">"USB device unavailable"</span>); <a name="l02801"></a>02801 <a name="l02802"></a>02802 <span class="keywordflow">if</span> (usb_control_msg(ftdi-><a class="code" href="structftdi__context.html#a190e410f4ed24583d7727d10d463ef4a">usb_dev</a>, <a class="code" href="ftdi_8h.html#aff6baace3506ed18cfed6fd4695543fa">FTDI_DEVICE_OUT_REQTYPE</a>, <a class="code" href="ftdi_8h.html#a8150b0280c2dff228abba700c4cec491">SIO_ERASE_EEPROM_REQUEST</a>, 0, 0, NULL, 0, ftdi-><a class="code" href="structftdi__context.html#ae98125557fc25c90953053a9992767cb">usb_write_timeout</a>) != 0) <a name="l02803"></a>02803 <a class="code" href="group__libftdi.html#ga885116a371f8fe4f6d03d1f943d8529b">ftdi_error_return</a>(-1, <span class="stringliteral">"unable to erase eeprom"</span>); <a name="l02804"></a>02804 <a name="l02805"></a>02805 <span class="keywordflow">return</span> 0; <a name="l02806"></a>02806 } <a name="l02807"></a>02807 <a name="l02815"></a><a class="code" href="group__libftdi.html#ga28c84d963760d7396e5a2992e63a9354">02815</a> <span class="keywordtype">char</span> *<a class="code" href="group__libftdi.html#ga28c84d963760d7396e5a2992e63a9354">ftdi_get_error_string</a> (<span class="keyword">struct</span> <a class="code" href="structftdi__context.html" title="Main context structure for all libftdi functions.">ftdi_context</a> *ftdi) <a name="l02816"></a>02816 { <a name="l02817"></a>02817 <span class="keywordflow">if</span> (ftdi == NULL) <a name="l02818"></a>02818 <span class="keywordflow">return</span> <span class="stringliteral">""</span>; <a name="l02819"></a>02819 <a name="l02820"></a>02820 <span class="keywordflow">return</span> ftdi-><a class="code" href="structftdi__context.html#accc4dc90cea120d545faf82c6034d445">error_str</a>; <a name="l02821"></a>02821 } <a name="l02822"></a>02822 <a name="l02823"></a>02823 <span class="comment">/* @} end of doxygen libftdi group */</span> </pre></div></div> </div> <hr size="1"><address style="text-align: right;"><small> Generated for libftdi by <a href="http://www.doxygen.org/ index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.7.4</small></address> </body> </html>