libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
hwdriver.c
Go to the documentation of this file.
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <string.h>
25 #include <glib.h>
26 #include "sigrok.h"
27 #include "sigrok-internal.h"
28 
29 /*
30  * This enumerates which driver capabilities correspond to user-settable
31  * options.
32  */
33 /* TODO: This shouldn't be a global. */
35  {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
36  {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
37  {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "pattern"},
38  {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
39  {0, 0, NULL, NULL},
40 };
41 
42 #ifdef HAVE_LA_DEMO
44 #endif
45 #ifdef HAVE_LA_OLS
47 #endif
48 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
50 #endif
51 #ifdef HAVE_LA_ASIX_SIGMA
53 #endif
54 #ifdef HAVE_LA_CHRONOVU_LA8
56 #endif
57 #ifdef HAVE_LA_LINK_MSO19
59 #endif
60 #ifdef HAVE_LA_ALSA
62 #endif
63 #ifdef HAVE_LA_FX2LAFW
65 #endif
66 
67 static struct sr_dev_driver *drivers_list[] = {
68 #ifdef HAVE_LA_DEMO
70 #endif
71 #ifdef HAVE_LA_OLS
73 #endif
74 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
76 #endif
77 #ifdef HAVE_LA_ASIX_SIGMA
79 #endif
80 #ifdef HAVE_LA_CHRONOVU_LA8
82 #endif
83 #ifdef HAVE_LA_LINK_MSO19
85 #endif
86 #ifdef HAVE_LA_ALSA
88 #endif
89 #ifdef HAVE_LA_FX2LAFW
91 #endif
92  NULL,
93 };
94 
95 /**
96  * Return the list of supported hardware drivers.
97  *
98  * @return Pointer to the NULL-terminated list of hardware driver pointers.
99  */
101 {
102  return drivers_list;
103 }
104 
105 /**
106  * Initialize a hardware driver.
107  *
108  * The specified driver is initialized, and all devices discovered by the
109  * driver are instantiated.
110  *
111  * @param driver The driver to initialize.
112  *
113  * @return The number of devices found and instantiated by the driver.
114  */
116 {
117  int num_devs, num_probes, i, j;
118  int num_initialized_devs = 0;
119  struct sr_dev *dev;
120  char **probe_names;
121 
122  sr_dbg("initializing %s driver", driver->name);
123  num_devs = driver->init(NULL);
124  for (i = 0; i < num_devs; i++) {
125  num_probes = GPOINTER_TO_INT(
126  driver->dev_info_get(i, SR_DI_NUM_PROBES));
127  probe_names = (char **)driver->dev_info_get(i,
129 
130  if (!probe_names) {
131  sr_warn("hwdriver: %s: driver %s does not return a "
132  "list of probe names", __func__, driver->name);
133  continue;
134  }
135 
136  dev = sr_dev_new(driver, i);
137  for (j = 0; j < num_probes; j++)
138  sr_dev_probe_add(dev, probe_names[j]);
139  num_initialized_devs++;
140  }
141 
142  return num_initialized_devs;
143 }
144 
146 {
147  int i;
148  struct sr_dev_driver **drivers;
149 
150  drivers = sr_driver_list();
151  for (i = 0; drivers[i]; i++) {
152  if (drivers[i]->cleanup)
153  drivers[i]->cleanup();
154  }
155 }
156 
158  const char *vendor, const char *model, const char *version)
159 {
160  struct sr_dev_inst *sdi;
161 
162  if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
163  sr_err("hwdriver: %s: sdi malloc failed", __func__);
164  return NULL;
165  }
166 
167  sdi->index = index;
168  sdi->status = status;
169  sdi->inst_type = -1;
170  sdi->vendor = vendor ? g_strdup(vendor) : NULL;
171  sdi->model = model ? g_strdup(model) : NULL;
172  sdi->version = version ? g_strdup(version) : NULL;
173  sdi->priv = NULL;
174 
175  return sdi;
176 }
177 
178 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
179 {
180  struct sr_dev_inst *sdi;
181  GSList *l;
182 
183  for (l = dev_insts; l; l = l->next) {
184  sdi = (struct sr_dev_inst *)(l->data);
185  if (sdi->index == dev_index)
186  return sdi;
187  }
188  sr_warn("could not find device index %d instance", dev_index);
189 
190  return NULL;
191 }
192 
194 {
195  g_free(sdi->priv);
196  g_free(sdi->vendor);
197  g_free(sdi->model);
198  g_free(sdi->version);
199  g_free(sdi);
200 }
201 
202 #ifdef HAVE_LIBUSB_1_0
203 
204 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
205  uint8_t address, struct libusb_device_handle *hdl)
206 {
207  struct sr_usb_dev_inst *udi;
208 
209  if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
210  sr_err("hwdriver: %s: udi malloc failed", __func__);
211  return NULL;
212  }
213 
214  udi->bus = bus;
215  udi->address = address;
216  udi->devhdl = hdl; /* TODO: Check if this is NULL? */
217 
218  return udi;
219 }
220 
221 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
222 {
223  /* Avoid compiler warnings. */
224  (void)usb;
225 
226  /* Nothing to do for this device instance type. */
227 }
228 
229 #endif
230 
231 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
232  int fd)
233 {
234  struct sr_serial_dev_inst *serial;
235 
236  if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
237  sr_err("hwdriver: %s: serial malloc failed", __func__);
238  return NULL;
239  }
240 
241  serial->port = g_strdup(port);
242  serial->fd = fd;
243 
244  return serial;
245 }
246 
247 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
248 {
249  g_free(serial->port);
250 }
251 
252 /**
253  * Find out if a hardware driver has a specific capability.
254  *
255  * @param driver The hardware driver in which to search for the capability.
256  * @param hwcap The capability to find in the list.
257  *
258  * @return TRUE if the specified capability exists in the specified driver,
259  * FALSE otherwise. Also, if 'driver' is NULL or the respective driver
260  * returns an invalid capability list, FALSE is returned.
261  */
262 SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
263 {
264  int *hwcaps, i;
265 
266  if (!driver) {
267  sr_err("hwdriver: %s: driver was NULL", __func__);
268  return FALSE;
269  }
270 
271  if (!(hwcaps = driver->hwcap_get_all())) {
272  sr_err("hwdriver: %s: hwcap_get_all() returned NULL", __func__);
273  return FALSE;
274  }
275 
276  for (i = 0; hwcaps[i]; i++) {
277  if (hwcaps[i] == hwcap)
278  return TRUE;
279  }
280 
281  return FALSE;
282 }
283 
284 /**
285  * Get a hardware driver capability option.
286  *
287  * @param hwcap The capability to get.
288  *
289  * @return A pointer to a struct with information about the parameter, or NULL
290  * if the capability was not found.
291  */
293 {
294  int i;
295 
296  for (i = 0; sr_hwcap_options[i].hwcap; i++) {
297  if (sr_hwcap_options[i].hwcap == hwcap)
298  return &sr_hwcap_options[i];
299  }
300 
301  return NULL;
302 }
303 
304 /* Unnecessary level of indirection follows. */
305 
307 {
308  return sr_session_source_remove(fd);
309 }
310 
311 SR_PRIV int sr_source_add(int fd, int events, int timeout,
312  sr_receive_data_callback_t cb, void *cb_data)
313 {
314  return sr_session_source_add(fd, events, timeout, cb, cb_data);
315 }