libsigrok
hwdriver.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2010-2012 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 "sigrok.h"
00027 #include "sigrok-internal.h"
00028 
00029 /*
00030  * This enumerates which driver capabilities correspond to user-settable
00031  * options.
00032  */
00033 /* TODO: This shouldn't be a global. */
00034 SR_API struct sr_hwcap_option sr_hwcap_options[] = {
00035         {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
00036         {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
00037         {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "pattern"},
00038         {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
00039         {0, 0, NULL, NULL},
00040 };
00041 
00042 #ifdef HAVE_LA_DEMO
00043 extern SR_PRIV struct sr_dev_driver demo_driver_info;
00044 #endif
00045 #ifdef HAVE_LA_OLS
00046 extern SR_PRIV struct sr_dev_driver ols_driver_info;
00047 #endif
00048 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
00049 extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
00050 #endif
00051 #ifdef HAVE_LA_ASIX_SIGMA
00052 extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
00053 #endif
00054 #ifdef HAVE_LA_CHRONOVU_LA8
00055 extern SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
00056 #endif
00057 #ifdef HAVE_LA_LINK_MSO19
00058 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
00059 #endif
00060 #ifdef HAVE_LA_ALSA
00061 extern SR_PRIV struct sr_dev_driver alsa_driver_info;
00062 #endif
00063 #ifdef HAVE_LA_FX2LAFW
00064 extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
00065 #endif
00066 
00067 static struct sr_dev_driver *drivers_list[] = {
00068 #ifdef HAVE_LA_DEMO
00069         &demo_driver_info,
00070 #endif
00071 #ifdef HAVE_LA_OLS
00072         &ols_driver_info,
00073 #endif
00074 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
00075         &zeroplus_logic_cube_driver_info,
00076 #endif
00077 #ifdef HAVE_LA_ASIX_SIGMA
00078         &asix_sigma_driver_info,
00079 #endif
00080 #ifdef HAVE_LA_CHRONOVU_LA8
00081         &chronovu_la8_driver_info,
00082 #endif
00083 #ifdef HAVE_LA_LINK_MSO19
00084         &link_mso19_driver_info,
00085 #endif
00086 #ifdef HAVE_LA_ALSA
00087         &alsa_driver_info,
00088 #endif
00089 #ifdef HAVE_LA_FX2LAFW
00090         &fx2lafw_driver_info,
00091 #endif
00092         NULL,
00093 };
00094 
00095 /**
00096  * Return the list of supported hardware drivers.
00097  *
00098  * @return Pointer to the NULL-terminated list of hardware driver pointers.
00099  */
00100 SR_API struct sr_dev_driver **sr_driver_list(void)
00101 {
00102         return drivers_list;
00103 }
00104 
00105 /**
00106  * Initialize a hardware driver.
00107  *
00108  * The specified driver is initialized, and all devices discovered by the
00109  * driver are instantiated.
00110  *
00111  * @param driver The driver to initialize.
00112  *
00113  * @return The number of devices found and instantiated by the driver.
00114  */
00115 SR_API int sr_driver_init(struct sr_dev_driver *driver)
00116 {
00117         int num_devs, num_probes, i, j;
00118         int num_initialized_devs = 0;
00119         struct sr_dev *dev;
00120         char **probe_names;
00121 
00122         sr_dbg("initializing %s driver", driver->name);
00123         num_devs = driver->init(NULL);
00124         for (i = 0; i < num_devs; i++) {
00125                 num_probes = GPOINTER_TO_INT(
00126                                 driver->dev_info_get(i, SR_DI_NUM_PROBES));
00127                 probe_names = (char **)driver->dev_info_get(i,
00128                                                         SR_DI_PROBE_NAMES);
00129 
00130                 if (!probe_names) {
00131                         sr_warn("hwdriver: %s: driver %s does not return a "
00132                                 "list of probe names", __func__, driver->name);
00133                         continue;
00134                 }
00135 
00136                 dev = sr_dev_new(driver, i);
00137                 for (j = 0; j < num_probes; j++)
00138                         sr_dev_probe_add(dev, probe_names[j]);
00139                 num_initialized_devs++;
00140         }
00141 
00142         return num_initialized_devs;
00143 }
00144 
00145 SR_PRIV void sr_hw_cleanup_all(void)
00146 {
00147         int i;
00148         struct sr_dev_driver **drivers;
00149 
00150         drivers = sr_driver_list();
00151         for (i = 0; drivers[i]; i++) {
00152                 if (drivers[i]->cleanup)
00153                         drivers[i]->cleanup();
00154         }
00155 }
00156 
00157 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
00158                 const char *vendor, const char *model, const char *version)
00159 {
00160         struct sr_dev_inst *sdi;
00161 
00162         if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
00163                 sr_err("hwdriver: %s: sdi malloc failed", __func__);
00164                 return NULL;
00165         }
00166 
00167         sdi->index = index;
00168         sdi->status = status;
00169         sdi->inst_type = -1;
00170         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
00171         sdi->model = model ? g_strdup(model) : NULL;
00172         sdi->version = version ? g_strdup(version) : NULL;
00173         sdi->priv = NULL;
00174 
00175         return sdi;
00176 }
00177 
00178 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
00179 {
00180         struct sr_dev_inst *sdi;
00181         GSList *l;
00182 
00183         for (l = dev_insts; l; l = l->next) {
00184                 sdi = (struct sr_dev_inst *)(l->data);
00185                 if (sdi->index == dev_index)
00186                         return sdi;
00187         }
00188         sr_warn("could not find device index %d instance", dev_index);
00189 
00190         return NULL;
00191 }
00192 
00193 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
00194 {
00195         g_free(sdi->priv);
00196         g_free(sdi->vendor);
00197         g_free(sdi->model);
00198         g_free(sdi->version);
00199         g_free(sdi);
00200 }
00201 
00202 #ifdef HAVE_LIBUSB_1_0
00203 
00204 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
00205                         uint8_t address, struct libusb_device_handle *hdl)
00206 {
00207         struct sr_usb_dev_inst *udi;
00208 
00209         if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
00210                 sr_err("hwdriver: %s: udi malloc failed", __func__);
00211                 return NULL;
00212         }
00213 
00214         udi->bus = bus;
00215         udi->address = address;
00216         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
00217 
00218         return udi;
00219 }
00220 
00221 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
00222 {
00223         /* Avoid compiler warnings. */
00224         (void)usb;
00225 
00226         /* Nothing to do for this device instance type. */
00227 }
00228 
00229 #endif
00230 
00231 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
00232                                                           int fd)
00233 {
00234         struct sr_serial_dev_inst *serial;
00235 
00236         if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
00237                 sr_err("hwdriver: %s: serial malloc failed", __func__);
00238                 return NULL;
00239         }
00240 
00241         serial->port = g_strdup(port);
00242         serial->fd = fd;
00243 
00244         return serial;
00245 }
00246 
00247 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
00248 {
00249         g_free(serial->port);
00250 }
00251 
00252 /**
00253  * Find out if a hardware driver has a specific capability.
00254  *
00255  * @param driver The hardware driver in which to search for the capability.
00256  * @param hwcap The capability to find in the list.
00257  *
00258  * @return TRUE if the specified capability exists in the specified driver,
00259  *         FALSE otherwise. Also, if 'driver' is NULL or the respective driver
00260  *         returns an invalid capability list, FALSE is returned.
00261  */
00262 SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
00263 {
00264         int *hwcaps, i;
00265 
00266         if (!driver) {
00267                 sr_err("hwdriver: %s: driver was NULL", __func__);
00268                 return FALSE;
00269         }
00270 
00271         if (!(hwcaps = driver->hwcap_get_all())) {
00272                 sr_err("hwdriver: %s: hwcap_get_all() returned NULL", __func__);
00273                 return FALSE;
00274         }
00275 
00276         for (i = 0; hwcaps[i]; i++) {
00277                 if (hwcaps[i] == hwcap)
00278                         return TRUE;
00279         }
00280 
00281         return FALSE;
00282 }
00283 
00284 /**
00285  * Get a hardware driver capability option.
00286  *
00287  * @param hwcap The capability to get.
00288  *
00289  * @return A pointer to a struct with information about the parameter, or NULL
00290  *         if the capability was not found.
00291  */
00292 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
00293 {
00294         int i;
00295 
00296         for (i = 0; sr_hwcap_options[i].hwcap; i++) {
00297                 if (sr_hwcap_options[i].hwcap == hwcap)
00298                         return &sr_hwcap_options[i];
00299         }
00300 
00301         return NULL;
00302 }
00303 
00304 /* Unnecessary level of indirection follows. */
00305 
00306 SR_PRIV int sr_source_remove(int fd)
00307 {
00308         return sr_session_source_remove(fd);
00309 }
00310 
00311 SR_PRIV int sr_source_add(int fd, int events, int timeout,
00312                           sr_receive_data_callback_t cb, void *cb_data)
00313 {
00314         return sr_session_source_add(fd, events, timeout, cb, cb_data);
00315 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines