libsigrok  0.3.0
sigrok hardware access and backend library
hwdriver.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the libsigrok project.
00003  *
00004  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
00005  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <sys/types.h>
00023 #include <dirent.h>
00024 #include <string.h>
00025 #include <glib.h>
00026 #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
00027 #include "libsigrok.h"
00028 #include "libsigrok-internal.h"
00029 
00030 /** @cond PRIVATE */
00031 #define LOG_PREFIX "hwdriver"
00032 /** @endcond */
00033 
00034 /**
00035  * @file
00036  *
00037  * Hardware driver handling in libsigrok.
00038  */
00039 
00040 /**
00041  * @defgroup grp_driver Hardware drivers
00042  *
00043  * Hardware driver handling in libsigrok.
00044  *
00045  * @{
00046  */
00047 
00048 static struct sr_config_info sr_config_info_data[] = {
00049         {SR_CONF_CONN, SR_T_STRING, "conn",
00050                 "Connection", NULL},
00051         {SR_CONF_SERIALCOMM, SR_T_STRING, "serialcomm",
00052                 "Serial communication", NULL},
00053         {SR_CONF_SAMPLERATE, SR_T_UINT64, "samplerate",
00054                 "Sample rate", NULL},
00055         {SR_CONF_CAPTURE_RATIO, SR_T_UINT64, "captureratio",
00056                 "Pre-trigger capture ratio", NULL},
00057         {SR_CONF_PATTERN_MODE, SR_T_STRING, "pattern",
00058                 "Pattern", NULL},
00059         {SR_CONF_TRIGGER_TYPE, SR_T_STRING, "triggertype",
00060                 "Trigger types", NULL},
00061         {SR_CONF_EXTERNAL_CLOCK, SR_T_BOOL, "external_clock",
00062                 "External clock mode", NULL},
00063         {SR_CONF_SWAP, SR_T_BOOL, "swap",
00064                 "Swap channel order", NULL},
00065         {SR_CONF_RLE, SR_T_BOOL, "rle",
00066                 "Run Length Encoding", NULL},
00067         {SR_CONF_TRIGGER_SLOPE, SR_T_STRING, "triggerslope",
00068                 "Trigger slope", NULL},
00069         {SR_CONF_TRIGGER_SOURCE, SR_T_STRING, "triggersource",
00070                 "Trigger source", NULL},
00071         {SR_CONF_HORIZ_TRIGGERPOS, SR_T_FLOAT, "horiz_triggerpos",
00072                 "Horizontal trigger position", NULL},
00073         {SR_CONF_BUFFERSIZE, SR_T_UINT64, "buffersize",
00074                 "Buffer size", NULL},
00075         {SR_CONF_TIMEBASE, SR_T_RATIONAL_PERIOD, "timebase",
00076                 "Time base", NULL},
00077         {SR_CONF_FILTER, SR_T_STRING, "filter",
00078                 "Filter targets", NULL},
00079         {SR_CONF_VDIV, SR_T_RATIONAL_VOLT, "vdiv",
00080                 "Volts/div", NULL},
00081         {SR_CONF_COUPLING, SR_T_STRING, "coupling",
00082                 "Coupling", NULL},
00083         {SR_CONF_DATALOG, SR_T_BOOL, "datalog",
00084                 "Datalog", NULL},
00085         {SR_CONF_SPL_WEIGHT_FREQ, SR_T_STRING, "spl_weight_freq",
00086                 "Sound pressure level frequency weighting", NULL},
00087         {SR_CONF_SPL_WEIGHT_TIME, SR_T_STRING, "spl_weight_time",
00088                 "Sound pressure level time weighting", NULL},
00089         {SR_CONF_HOLD_MAX, SR_T_BOOL, "hold_max",
00090                 "Hold max", NULL},
00091         {SR_CONF_HOLD_MIN, SR_T_BOOL, "hold_min",
00092                 "Hold min", NULL},
00093         {SR_CONF_SPL_MEASUREMENT_RANGE, SR_T_UINT64_RANGE, "spl_meas_range",
00094                 "Sound pressure level measurement range", NULL},
00095         {SR_CONF_VOLTAGE_THRESHOLD, SR_T_DOUBLE_RANGE, "voltage_threshold",
00096                 "Voltage threshold", NULL },
00097         {SR_CONF_POWER_OFF, SR_T_BOOL, "power_off",
00098                 "Power off", NULL},
00099         {SR_CONF_DATA_SOURCE, SR_T_STRING, "data_source",
00100                 "Data source", NULL},
00101         {SR_CONF_NUM_LOGIC_CHANNELS, SR_T_INT32, "logic_channels",
00102                 "Number of logic channels", NULL},
00103         {SR_CONF_NUM_ANALOG_CHANNELS, SR_T_INT32, "analog_channels",
00104                 "Number of analog channels", NULL},
00105         {SR_CONF_OUTPUT_VOLTAGE, SR_T_FLOAT, "output_voltage",
00106                 "Current output voltage", NULL},
00107         {SR_CONF_OUTPUT_VOLTAGE_MAX, SR_T_FLOAT, "output_voltage_max",
00108                 "Maximum output voltage", NULL},
00109         {SR_CONF_OUTPUT_CURRENT, SR_T_FLOAT, "output_current",
00110                 "Current output current", NULL},
00111         {SR_CONF_OUTPUT_CURRENT_MAX, SR_T_FLOAT, "output_current_max",
00112                 "Maximum output current", NULL},
00113         {SR_CONF_OUTPUT_ENABLED, SR_T_BOOL, "output_enabled",
00114                 "Output enabled", NULL},
00115         {SR_CONF_OUTPUT_CHANNEL, SR_T_STRING, "output_channel",
00116                 "Output channel modes", NULL},
00117         {SR_CONF_OVER_VOLTAGE_PROTECTION, SR_T_BOOL, "ovp",
00118                 "Over-voltage protection", NULL},
00119         {SR_CONF_OVER_CURRENT_PROTECTION, SR_T_BOOL, "ocp",
00120                 "Over-current protection", NULL},
00121         {SR_CONF_LIMIT_SAMPLES, SR_T_UINT64, "limit_samples",
00122                 "Sample limit", NULL},
00123         {SR_CONF_CLOCK_EDGE, SR_T_STRING, "clock_edge",
00124                 "Clock edge", NULL},
00125         {0, 0, NULL, NULL, NULL},
00126 };
00127 
00128 /** @cond PRIVATE */
00129 #ifdef HAVE_HW_APPA_55II
00130 extern SR_PRIV struct sr_dev_driver appa_55ii_driver_info;
00131 #endif
00132 #ifdef HAVE_HW_ATTEN_PPS3XXX
00133 extern SR_PRIV struct sr_dev_driver atten_pps3203_driver_info;
00134 #endif
00135 #ifdef HAVE_HW_BRYMEN_BM86X
00136 extern SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info;
00137 #endif
00138 #ifdef HAVE_HW_BRYMEN_DMM
00139 extern SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
00140 #endif
00141 #ifdef HAVE_HW_CEM_DT_885X
00142 extern SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
00143 #endif
00144 #ifdef HAVE_HW_CENTER_3XX
00145 extern SR_PRIV struct sr_dev_driver center_309_driver_info;
00146 extern SR_PRIV struct sr_dev_driver voltcraft_k204_driver_info;
00147 #endif
00148 #ifdef HAVE_HW_COLEAD_SLM
00149 extern SR_PRIV struct sr_dev_driver colead_slm_driver_info;
00150 #endif
00151 #ifdef HAVE_HW_CONRAD_DIGI_35_CPU
00152 extern SR_PRIV struct sr_dev_driver conrad_digi_35_cpu_driver_info;
00153 #endif
00154 #ifdef HAVE_HW_DEMO
00155 extern SR_PRIV struct sr_dev_driver demo_driver_info;
00156 #endif
00157 #ifdef HAVE_HW_GMC_MH_1X_2X
00158 extern SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
00159 extern SR_PRIV struct sr_dev_driver gmc_mh_2x_bd232_driver_info;
00160 #endif
00161 #ifdef HAVE_HW_HAMEG_HMO
00162 extern SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
00163 #endif
00164 #ifdef HAVE_HW_IKALOGIC_SCANALOGIC2
00165 extern SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
00166 #endif
00167 #ifdef HAVE_HW_IKALOGIC_SCANAPLUS
00168 extern SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info;
00169 #endif
00170 #ifdef HAVE_HW_KECHENG_KC_330B
00171 extern SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info;
00172 #endif
00173 #ifdef HAVE_HW_LASCAR_EL_USB
00174 extern SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info;
00175 #endif
00176 #ifdef HAVE_HW_MIC_985XX
00177 extern SR_PRIV struct sr_dev_driver mic_98581_driver_info;
00178 extern SR_PRIV struct sr_dev_driver mic_98583_driver_info;
00179 #endif
00180 #ifdef HAVE_HW_NORMA_DMM
00181 extern SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
00182 #endif
00183 #ifdef HAVE_HW_OLS
00184 extern SR_PRIV struct sr_dev_driver ols_driver_info;
00185 #endif
00186 #ifdef HAVE_HW_RIGOL_DS
00187 extern SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
00188 #endif
00189 #ifdef HAVE_HW_SALEAE_LOGIC16
00190 extern SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
00191 #endif
00192 #ifdef HAVE_HW_SYSCLK_LWLA
00193 extern SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
00194 #endif
00195 #ifdef HAVE_HW_TELEINFO
00196 extern SR_PRIV struct sr_dev_driver teleinfo_driver_info;
00197 #endif
00198 #ifdef HAVE_HW_TONDAJ_SL_814
00199 extern SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info;
00200 #endif
00201 #ifdef HAVE_HW_UNI_T_UT32X
00202 extern SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info;
00203 #endif
00204 #ifdef HAVE_HW_VICTOR_DMM
00205 extern SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
00206 #endif
00207 #ifdef HAVE_HW_ZEROPLUS_LOGIC_CUBE
00208 extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
00209 #endif
00210 #ifdef HAVE_HW_ASIX_SIGMA
00211 extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
00212 #endif
00213 #ifdef HAVE_HW_CHRONOVU_LA
00214 extern SR_PRIV struct sr_dev_driver chronovu_la_driver_info;
00215 #endif
00216 #ifdef HAVE_HW_LINK_MSO19
00217 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
00218 #endif
00219 #ifdef HAVE_HW_FX2LAFW
00220 extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
00221 #endif
00222 #ifdef HAVE_HW_HANTEK_DSO
00223 extern SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
00224 #endif
00225 #ifdef HAVE_HW_AGILENT_DMM
00226 extern SR_PRIV struct sr_dev_driver agdmm_driver_info;
00227 #endif
00228 #ifdef HAVE_HW_FLUKE_DMM
00229 extern SR_PRIV struct sr_dev_driver flukedmm_driver_info;
00230 #endif
00231 #ifdef HAVE_HW_SERIAL_DMM
00232 extern SR_PRIV struct sr_dev_driver bbcgm_m2110_driver_info;
00233 extern SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info;
00234 extern SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
00235 extern SR_PRIV struct sr_dev_driver metex_me31_driver_info;
00236 extern SR_PRIV struct sr_dev_driver peaktech_3410_driver_info;
00237 extern SR_PRIV struct sr_dev_driver mastech_mas345_driver_info;
00238 extern SR_PRIV struct sr_dev_driver va_va18b_driver_info;
00239 extern SR_PRIV struct sr_dev_driver va_va40b_driver_info;
00240 extern SR_PRIV struct sr_dev_driver metex_m3640d_driver_info;
00241 extern SR_PRIV struct sr_dev_driver metex_m4650cr_driver_info;
00242 extern SR_PRIV struct sr_dev_driver peaktech_4370_driver_info;
00243 extern SR_PRIV struct sr_dev_driver pce_pce_dm32_driver_info;
00244 extern SR_PRIV struct sr_dev_driver radioshack_22_168_driver_info;
00245 extern SR_PRIV struct sr_dev_driver radioshack_22_805_driver_info;
00246 extern SR_PRIV struct sr_dev_driver radioshack_22_812_driver_info;
00247 extern SR_PRIV struct sr_dev_driver tecpel_dmm_8061_ser_driver_info;
00248 extern SR_PRIV struct sr_dev_driver voltcraft_m3650cr_driver_info;
00249 extern SR_PRIV struct sr_dev_driver voltcraft_m3650d_driver_info;
00250 extern SR_PRIV struct sr_dev_driver voltcraft_m4650cr_driver_info;
00251 extern SR_PRIV struct sr_dev_driver voltcraft_me42_driver_info;
00252 extern SR_PRIV struct sr_dev_driver voltcraft_vc820_ser_driver_info;
00253 extern SR_PRIV struct sr_dev_driver voltcraft_vc830_ser_driver_info;
00254 extern SR_PRIV struct sr_dev_driver voltcraft_vc840_ser_driver_info;
00255 extern SR_PRIV struct sr_dev_driver uni_t_ut60a_ser_driver_info;
00256 extern SR_PRIV struct sr_dev_driver uni_t_ut60e_ser_driver_info;
00257 extern SR_PRIV struct sr_dev_driver uni_t_ut60g_ser_driver_info;
00258 extern SR_PRIV struct sr_dev_driver uni_t_ut61b_ser_driver_info;
00259 extern SR_PRIV struct sr_dev_driver uni_t_ut61c_ser_driver_info;
00260 extern SR_PRIV struct sr_dev_driver uni_t_ut61d_ser_driver_info;
00261 extern SR_PRIV struct sr_dev_driver uni_t_ut61e_ser_driver_info;
00262 extern SR_PRIV struct sr_dev_driver iso_tech_idm103n_driver_info;
00263 extern SR_PRIV struct sr_dev_driver tenma_72_7745_ser_driver_info;
00264 extern SR_PRIV struct sr_dev_driver tenma_72_7750_ser_driver_info;
00265 #endif
00266 #ifdef HAVE_HW_UNI_T_DMM
00267 extern SR_PRIV struct sr_dev_driver tecpel_dmm_8061_driver_info;
00268 extern SR_PRIV struct sr_dev_driver uni_t_ut60a_driver_info;
00269 extern SR_PRIV struct sr_dev_driver uni_t_ut60e_driver_info;
00270 extern SR_PRIV struct sr_dev_driver uni_t_ut60g_driver_info;
00271 extern SR_PRIV struct sr_dev_driver uni_t_ut61b_driver_info;
00272 extern SR_PRIV struct sr_dev_driver uni_t_ut61c_driver_info;
00273 extern SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
00274 extern SR_PRIV struct sr_dev_driver uni_t_ut61e_driver_info;
00275 extern SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
00276 extern SR_PRIV struct sr_dev_driver voltcraft_vc830_driver_info;
00277 extern SR_PRIV struct sr_dev_driver voltcraft_vc840_driver_info;
00278 extern SR_PRIV struct sr_dev_driver tenma_72_7745_driver_info;
00279 extern SR_PRIV struct sr_dev_driver tenma_72_7750_driver_info;
00280 #endif
00281 /** @endcond */
00282 
00283 static struct sr_dev_driver *drivers_list[] = {
00284 #ifdef HAVE_HW_APPA_55II
00285         &appa_55ii_driver_info,
00286 #endif
00287 #ifdef HAVE_HW_ATTEN_PPS3XXX
00288         &atten_pps3203_driver_info,
00289 #endif
00290 #ifdef HAVE_HW_BRYMEN_BM86X
00291         &brymen_bm86x_driver_info,
00292 #endif
00293 #ifdef HAVE_HW_BRYMEN_DMM
00294         &brymen_bm857_driver_info,
00295 #endif
00296 #ifdef HAVE_HW_CEM_DT_885X
00297         &cem_dt_885x_driver_info,
00298 #endif
00299 #ifdef HAVE_HW_CENTER_3XX
00300         &center_309_driver_info,
00301         &voltcraft_k204_driver_info,
00302 #endif
00303 #ifdef HAVE_HW_COLEAD_SLM
00304         &colead_slm_driver_info,
00305 #endif
00306 #ifdef HAVE_HW_CONRAD_DIGI_35_CPU
00307         &conrad_digi_35_cpu_driver_info,
00308 #endif
00309 #ifdef HAVE_HW_DEMO
00310         &demo_driver_info,
00311 #endif
00312 #ifdef HAVE_HW_GMC_MH_1X_2X
00313         &gmc_mh_1x_2x_rs232_driver_info,
00314         &gmc_mh_2x_bd232_driver_info,
00315 #endif
00316 #ifdef HAVE_HW_HAMEG_HMO
00317         &hameg_hmo_driver_info,
00318 #endif
00319 #ifdef HAVE_HW_IKALOGIC_SCANALOGIC2
00320         &ikalogic_scanalogic2_driver_info,
00321 #endif
00322 #ifdef HAVE_HW_IKALOGIC_SCANAPLUS
00323         &ikalogic_scanaplus_driver_info,
00324 #endif
00325 #ifdef HAVE_HW_KECHENG_KC_330B
00326         &kecheng_kc_330b_driver_info,
00327 #endif
00328 #ifdef HAVE_HW_LASCAR_EL_USB
00329         &lascar_el_usb_driver_info,
00330 #endif
00331 #ifdef HAVE_HW_MIC_985XX
00332         &mic_98581_driver_info,
00333         &mic_98583_driver_info,
00334 #endif
00335 #ifdef HAVE_HW_NORMA_DMM
00336         &norma_dmm_driver_info,
00337 #endif
00338 #ifdef HAVE_HW_OLS
00339         &ols_driver_info,
00340 #endif
00341 #ifdef HAVE_HW_RIGOL_DS
00342         &rigol_ds_driver_info,
00343 #endif
00344 #ifdef HAVE_HW_SALEAE_LOGIC16
00345         &saleae_logic16_driver_info,
00346 #endif
00347 #ifdef HAVE_HW_SYSCLK_LWLA
00348         &sysclk_lwla_driver_info,
00349 #endif
00350 #ifdef HAVE_HW_TELEINFO
00351         &teleinfo_driver_info,
00352 #endif
00353 #ifdef HAVE_HW_TONDAJ_SL_814
00354         &tondaj_sl_814_driver_info,
00355 #endif
00356 #ifdef HAVE_HW_UNI_T_UT32X
00357         &uni_t_ut32x_driver_info,
00358 #endif
00359 #ifdef HAVE_HW_VICTOR_DMM
00360         &victor_dmm_driver_info,
00361 #endif
00362 #ifdef HAVE_HW_ZEROPLUS_LOGIC_CUBE
00363         &zeroplus_logic_cube_driver_info,
00364 #endif
00365 #ifdef HAVE_HW_ASIX_SIGMA
00366         &asix_sigma_driver_info,
00367 #endif
00368 #ifdef HAVE_HW_CHRONOVU_LA
00369         &chronovu_la_driver_info,
00370 #endif
00371 #ifdef HAVE_HW_LINK_MSO19
00372         &link_mso19_driver_info,
00373 #endif
00374 #ifdef HAVE_HW_FX2LAFW
00375         &fx2lafw_driver_info,
00376 #endif
00377 #ifdef HAVE_HW_HANTEK_DSO
00378         &hantek_dso_driver_info,
00379 #endif
00380 #ifdef HAVE_HW_AGILENT_DMM
00381         &agdmm_driver_info,
00382 #endif
00383 #ifdef HAVE_HW_FLUKE_DMM
00384         &flukedmm_driver_info,
00385 #endif
00386 #ifdef HAVE_HW_SERIAL_DMM
00387         &bbcgm_m2110_driver_info,
00388         &digitek_dt4000zc_driver_info,
00389         &tekpower_tp4000zc_driver_info,
00390         &metex_me31_driver_info,
00391         &peaktech_3410_driver_info,
00392         &mastech_mas345_driver_info,
00393         &va_va18b_driver_info,
00394         &va_va40b_driver_info,
00395         &metex_m3640d_driver_info,
00396         &metex_m4650cr_driver_info,
00397         &peaktech_4370_driver_info,
00398         &pce_pce_dm32_driver_info,
00399         &radioshack_22_168_driver_info,
00400         &radioshack_22_805_driver_info,
00401         &radioshack_22_812_driver_info,
00402         &tecpel_dmm_8061_ser_driver_info,
00403         &voltcraft_m3650cr_driver_info,
00404         &voltcraft_m3650d_driver_info,
00405         &voltcraft_m4650cr_driver_info,
00406         &voltcraft_me42_driver_info,
00407         &voltcraft_vc820_ser_driver_info,
00408         &voltcraft_vc830_ser_driver_info,
00409         &voltcraft_vc840_ser_driver_info,
00410         &uni_t_ut60a_ser_driver_info,
00411         &uni_t_ut60e_ser_driver_info,
00412         &uni_t_ut60g_ser_driver_info,
00413         &uni_t_ut61b_ser_driver_info,
00414         &uni_t_ut61c_ser_driver_info,
00415         &uni_t_ut61d_ser_driver_info,
00416         &uni_t_ut61e_ser_driver_info,
00417         &iso_tech_idm103n_driver_info,
00418         &tenma_72_7745_ser_driver_info,
00419         &tenma_72_7750_ser_driver_info,
00420 #endif
00421 #ifdef HAVE_HW_UNI_T_DMM
00422         &tecpel_dmm_8061_driver_info,
00423         &uni_t_ut60a_driver_info,
00424         &uni_t_ut60e_driver_info,
00425         &uni_t_ut60g_driver_info,
00426         &uni_t_ut61b_driver_info,
00427         &uni_t_ut61c_driver_info,
00428         &uni_t_ut61d_driver_info,
00429         &uni_t_ut61e_driver_info,
00430         &voltcraft_vc820_driver_info,
00431         &voltcraft_vc830_driver_info,
00432         &voltcraft_vc840_driver_info,
00433         &tenma_72_7745_driver_info,
00434         &tenma_72_7750_driver_info,
00435 #endif
00436         NULL,
00437 };
00438 
00439 /**
00440  * Return the list of supported hardware drivers.
00441  *
00442  * @return Pointer to the NULL-terminated list of hardware driver pointers.
00443  *
00444  * @since 0.1.0
00445  */
00446 SR_API struct sr_dev_driver **sr_driver_list(void)
00447 {
00448 
00449         return drivers_list;
00450 }
00451 
00452 /**
00453  * Initialize a hardware driver.
00454  *
00455  * This usually involves memory allocations and variable initializations
00456  * within the driver, but _not_ scanning for attached devices.
00457  * The API call sr_driver_scan() is used for that.
00458  *
00459  * @param ctx A libsigrok context object allocated by a previous call to
00460  *            sr_init(). Must not be NULL.
00461  * @param driver The driver to initialize. This must be a pointer to one of
00462  *               the entries returned by sr_driver_list(). Must not be NULL.
00463  *
00464  * @retval SR_OK Success
00465  * @retval SR_ERR_ARG Invalid parameter(s).
00466  * @retval SR_ERR_BUG Internal errors.
00467  * @retval other Another negative error code upon other errors.
00468  *
00469  * @since 0.2.0
00470  */
00471 SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
00472 {
00473         int ret;
00474 
00475         if (!ctx) {
00476                 sr_err("Invalid libsigrok context, can't initialize.");
00477                 return SR_ERR_ARG;
00478         }
00479 
00480         if (!driver) {
00481                 sr_err("Invalid driver, can't initialize.");
00482                 return SR_ERR_ARG;
00483         }
00484 
00485         sr_spew("Initializing driver '%s'.", driver->name);
00486         if ((ret = driver->init(ctx)) < 0)
00487                 sr_err("Failed to initialize the driver: %d.", ret);
00488 
00489         return ret;
00490 }
00491 
00492 /**
00493  * Tell a hardware driver to scan for devices.
00494  *
00495  * In addition to the detection, the devices that are found are also
00496  * initialized automatically. On some devices, this involves a firmware upload,
00497  * or other such measures.
00498  *
00499  * The order in which the system is scanned for devices is not specified. The
00500  * caller should not assume or rely on any specific order.
00501  *
00502  * Before calling sr_driver_scan(), the user must have previously initialized
00503  * the driver by calling sr_driver_init().
00504  *
00505  * @param driver The driver that should scan. This must be a pointer to one of
00506  *               the entries returned by sr_driver_list(). Must not be NULL.
00507  * @param options A list of 'struct sr_hwopt' options to pass to the driver's
00508  *                scanner. Can be NULL/empty.
00509  *
00510  * @return A GSList * of 'struct sr_dev_inst', or NULL if no devices were
00511  *         found (or errors were encountered). This list must be freed by the
00512  *         caller using g_slist_free(), but without freeing the data pointed
00513  *         to in the list.
00514  *
00515  * @since 0.2.0
00516  */
00517 SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
00518 {
00519         GSList *l;
00520 
00521         if (!driver) {
00522                 sr_err("Invalid driver, can't scan for devices.");
00523                 return NULL;
00524         }
00525 
00526         if (!driver->priv) {
00527                 sr_err("Driver not initialized, can't scan for devices.");
00528                 return NULL;
00529         }
00530 
00531         l = driver->scan(options);
00532 
00533         sr_spew("Scan of '%s' found %d devices.", driver->name,
00534                 g_slist_length(l));
00535 
00536         return l;
00537 }
00538 
00539 /** Call driver cleanup function for all drivers.
00540  *  @private */
00541 SR_PRIV void sr_hw_cleanup_all(void)
00542 {
00543         int i;
00544         struct sr_dev_driver **drivers;
00545 
00546         drivers = sr_driver_list();
00547         for (i = 0; drivers[i]; i++) {
00548                 if (drivers[i]->cleanup)
00549                         drivers[i]->cleanup();
00550         }
00551 }
00552 
00553 /** Allocate struct sr_config.
00554  *  A floating reference can be passed in for data.
00555  *  @private
00556  */
00557 SR_PRIV struct sr_config *sr_config_new(int key, GVariant *data)
00558 {
00559         struct sr_config *src;
00560 
00561         if (!(src = g_try_malloc(sizeof(struct sr_config))))
00562                 return NULL;
00563         src->key = key;
00564         src->data = g_variant_ref_sink(data);
00565 
00566         return src;
00567 }
00568 
00569 /** Free struct sr_config.
00570  *  @private
00571  */
00572 SR_PRIV void sr_config_free(struct sr_config *src)
00573 {
00574 
00575         if (!src || !src->data) {
00576                 sr_err("%s: invalid data!", __func__);
00577                 return;
00578         }
00579 
00580         g_variant_unref(src->data);
00581         g_free(src);
00582 
00583 }
00584 
00585 /**
00586  * Query value of a configuration key at the given driver or device instance.
00587  *
00588  * @param[in] driver The sr_dev_driver struct to query.
00589  * @param[in] sdi (optional) If the key is specific to a device, this must
00590  *            contain a pointer to the struct sr_dev_inst to be checked.
00591  *            Otherwise it must be NULL.
00592  * @param[in] cg The channel group on the device for which to list the
00593  *                    values, or NULL.
00594  * @param[in] key The configuration key (SR_CONF_*).
00595  * @param[in,out] data Pointer to a GVariant where the value will be stored.
00596  *             Must not be NULL. The caller is given ownership of the GVariant
00597  *             and must thus decrease the refcount after use. However if
00598  *             this function returns an error code, the field should be
00599  *             considered unused, and should not be unreferenced.
00600  *
00601  * @retval SR_OK Success.
00602  * @retval SR_ERR Error.
00603  * @retval SR_ERR_ARG The driver doesn't know that key, but this is not to be
00604  *          interpreted as an error by the caller; merely as an indication
00605  *          that it's not applicable.
00606  *
00607  * @since 0.3.0
00608  */
00609 SR_API int sr_config_get(const struct sr_dev_driver *driver,
00610                 const struct sr_dev_inst *sdi,
00611                 const struct sr_channel_group *cg,
00612                 int key, GVariant **data)
00613 {
00614         int ret;
00615 
00616         if (!driver || !data)
00617                 return SR_ERR;
00618 
00619         if (!driver->config_get)
00620                 return SR_ERR_ARG;
00621 
00622         if ((ret = driver->config_get(key, data, sdi, cg)) == SR_OK) {
00623                 /* Got a floating reference from the driver. Sink it here,
00624                  * caller will need to unref when done with it. */
00625                 g_variant_ref_sink(*data);
00626         }
00627 
00628         return ret;
00629 }
00630 
00631 /**
00632  * Set value of a configuration key in a device instance.
00633  *
00634  * @param[in] sdi The device instance.
00635  * @param[in] cg The channel group on the device for which to list the
00636  *                    values, or NULL.
00637  * @param[in] key The configuration key (SR_CONF_*).
00638  * @param data The new value for the key, as a GVariant with GVariantType
00639  *        appropriate to that key. A floating reference can be passed
00640  *        in; its refcount will be sunk and unreferenced after use.
00641  *
00642  * @retval SR_OK Success.
00643  * @retval SR_ERR Error.
00644  * @retval SR_ERR_ARG The driver doesn't know that key, but this is not to be
00645  *          interpreted as an error by the caller; merely as an indication
00646  *          that it's not applicable.
00647  *
00648  * @since 0.3.0
00649  */
00650 SR_API int sr_config_set(const struct sr_dev_inst *sdi,
00651                 const struct sr_channel_group *cg,
00652                 int key, GVariant *data)
00653 {
00654         int ret;
00655 
00656         g_variant_ref_sink(data);
00657 
00658         if (!sdi || !sdi->driver || !data)
00659                 ret = SR_ERR;
00660         else if (!sdi->driver->config_set)
00661                 ret = SR_ERR_ARG;
00662         else
00663                 ret = sdi->driver->config_set(key, data, sdi, cg);
00664 
00665         g_variant_unref(data);
00666 
00667         return ret;
00668 }
00669 
00670 /**
00671  * Apply configuration settings to the device hardware.
00672  *
00673  * @param sdi The device instance.
00674  *
00675  * @return SR_OK upon success or SR_ERR in case of error.
00676  *
00677  * @since 0.3.0
00678  */
00679 SR_API int sr_config_commit(const struct sr_dev_inst *sdi)
00680 {
00681         int ret;
00682 
00683         if (!sdi || !sdi->driver)
00684                 ret = SR_ERR;
00685         else if (!sdi->driver->config_commit)
00686                 ret = SR_OK;
00687         else
00688                 ret = sdi->driver->config_commit(sdi);
00689 
00690         return ret;
00691 }
00692 
00693 /**
00694  * List all possible values for a configuration key.
00695  *
00696  * @param[in] driver The sr_dev_driver struct to query.
00697  * @param[in] sdi (optional) If the key is specific to a device, this must
00698  *            contain a pointer to the struct sr_dev_inst to be checked.
00699  * @param[in] cg The channel group on the device for which to list the
00700  *                    values, or NULL.
00701  * @param[in] key The configuration key (SR_CONF_*).
00702  * @param[in,out] data A pointer to a GVariant where the list will be stored.
00703  *             The caller is given ownership of the GVariant and must thus
00704  *             unref the GVariant after use. However if this function
00705  *             returns an error code, the field should be considered
00706  *             unused, and should not be unreferenced.
00707  *
00708  * @retval SR_OK Success.
00709  * @retval SR_ERR Error.
00710  * @retval SR_ERR_ARG The driver doesn't know that key, but this is not to be
00711  *          interpreted as an error by the caller; merely as an indication
00712  *          that it's not applicable.
00713  *
00714  * @since 0.3.0
00715  */
00716 SR_API int sr_config_list(const struct sr_dev_driver *driver,
00717                 const struct sr_dev_inst *sdi,
00718                 const struct sr_channel_group *cg,
00719                 int key, GVariant **data)
00720 {
00721         int ret;
00722 
00723         if (!driver || !data)
00724                 ret = SR_ERR;
00725         else if (!driver->config_list)
00726                 ret = SR_ERR_ARG;
00727         else if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK)
00728                 g_variant_ref_sink(*data);
00729 
00730         return ret;
00731 }
00732 
00733 /**
00734  * Get information about a configuration key, by key.
00735  *
00736  * @param[in] key The configuration key.
00737  *
00738  * @return A pointer to a struct sr_config_info, or NULL if the key
00739  *         was not found.
00740  *
00741  * @since 0.2.0
00742  */
00743 SR_API const struct sr_config_info *sr_config_info_get(int key)
00744 {
00745         int i;
00746 
00747         for (i = 0; sr_config_info_data[i].key; i++) {
00748                 if (sr_config_info_data[i].key == key)
00749                         return &sr_config_info_data[i];
00750         }
00751 
00752         return NULL;
00753 }
00754 
00755 /**
00756  * Get information about a configuration key, by name.
00757  *
00758  * @param[in] optname The configuration key.
00759  *
00760  * @return A pointer to a struct sr_config_info, or NULL if the key
00761  *         was not found.
00762  *
00763  * @since 0.2.0
00764  */
00765 SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname)
00766 {
00767         int i;
00768 
00769         for (i = 0; sr_config_info_data[i].key; i++) {
00770                 if (!strcmp(sr_config_info_data[i].id, optname))
00771                         return &sr_config_info_data[i];
00772         }
00773 
00774         return NULL;
00775 }
00776 
00777 /* Unnecessary level of indirection follows. */
00778 
00779 /** @private
00780  *  @see sr_session_source_remove()
00781  */
00782 SR_PRIV int sr_source_remove(int fd)
00783 {
00784         return sr_session_source_remove(fd);
00785 }
00786 
00787 /** @private
00788  *  @see sr_session_source_add()
00789  */
00790 SR_PRIV int sr_source_add(int fd, int events, int timeout,
00791                           sr_receive_data_callback cb, void *cb_data)
00792 {
00793         return sr_session_source_add(fd, events, timeout, cb, cb_data);
00794 }
00795 
00796 /** @} */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines