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_SALEAE_LOGIC
47 #endif
48 #ifdef HAVE_LA_OLS
50 #endif
51 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
53 #endif
54 #ifdef HAVE_LA_ASIX_SIGMA
56 #endif
57 #ifdef HAVE_LA_CHRONOVU_LA8
59 #endif
60 #ifdef HAVE_LA_LINK_MSO19
62 #endif
63 #ifdef HAVE_LA_ALSA
65 #endif
66 #ifdef HAVE_LA_FX2LAFW
68 #endif
69 
70 static struct sr_dev_driver *drivers_list[] = {
71 #ifdef HAVE_LA_DEMO
73 #endif
74 #ifdef HAVE_LA_SALEAE_LOGIC
76 #endif
77 #ifdef HAVE_LA_OLS
79 #endif
80 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
82 #endif
83 #ifdef HAVE_LA_ASIX_SIGMA
85 #endif
86 #ifdef HAVE_LA_CHRONOVU_LA8
88 #endif
89 #ifdef HAVE_LA_LINK_MSO19
91 #endif
92 #ifdef HAVE_LA_ALSA
94 #endif
95 #ifdef HAVE_LA_FX2LAFW
97 #endif
98  NULL,
99 };
100 
101 /**
102  * Return the list of supported hardware drivers.
103  *
104  * @return Pointer to the NULL-terminated list of hardware driver pointers.
105  */
107 {
108  return drivers_list;
109 }
110 
111 /**
112  * Initialize a hardware driver.
113  *
114  * The specified driver is initialized, and all devices discovered by the
115  * driver are instantiated.
116  *
117  * @param driver The driver to initialize.
118  *
119  * @return The number of devices found and instantiated by the driver.
120  */
122 {
123  int num_devs, num_probes, i, j;
124  int num_initialized_devs = 0;
125  struct sr_dev *dev;
126  char **probe_names;
127 
128  sr_dbg("initializing %s driver", driver->name);
129  num_devs = driver->init(NULL);
130  for (i = 0; i < num_devs; i++) {
131  num_probes = GPOINTER_TO_INT(
132  driver->dev_info_get(i, SR_DI_NUM_PROBES));
133  probe_names = (char **)driver->dev_info_get(i,
135 
136  if (!probe_names) {
137  sr_warn("hwdriver: %s: driver %s does not return a "
138  "list of probe names", __func__, driver->name);
139  continue;
140  }
141 
142  dev = sr_dev_new(driver, i);
143  for (j = 0; j < num_probes; j++)
144  sr_dev_probe_add(dev, probe_names[j]);
145  num_initialized_devs++;
146  }
147 
148  return num_initialized_devs;
149 }
150 
152 {
153  int i;
154  struct sr_dev_driver **drivers;
155 
156  drivers = sr_driver_list();
157  for (i = 0; drivers[i]; i++) {
158  if (drivers[i]->cleanup)
159  drivers[i]->cleanup();
160  }
161 }
162 
164  const char *vendor, const char *model, const char *version)
165 {
166  struct sr_dev_inst *sdi;
167 
168  if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
169  sr_err("hwdriver: %s: sdi malloc failed", __func__);
170  return NULL;
171  }
172 
173  sdi->index = index;
174  sdi->status = status;
175  sdi->inst_type = -1;
176  sdi->vendor = vendor ? g_strdup(vendor) : NULL;
177  sdi->model = model ? g_strdup(model) : NULL;
178  sdi->version = version ? g_strdup(version) : NULL;
179  sdi->priv = NULL;
180 
181  return sdi;
182 }
183 
184 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
185 {
186  struct sr_dev_inst *sdi;
187  GSList *l;
188 
189  for (l = dev_insts; l; l = l->next) {
190  sdi = (struct sr_dev_inst *)(l->data);
191  if (sdi->index == dev_index)
192  return sdi;
193  }
194  sr_warn("could not find device index %d instance", dev_index);
195 
196  return NULL;
197 }
198 
200 {
201  g_free(sdi->priv);
202  g_free(sdi->vendor);
203  g_free(sdi->model);
204  g_free(sdi->version);
205  g_free(sdi);
206 }
207 
208 #ifdef HAVE_LIBUSB_1_0
209 
210 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
211  uint8_t address, struct libusb_device_handle *hdl)
212 {
213  struct sr_usb_dev_inst *udi;
214 
215  if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
216  sr_err("hwdriver: %s: udi malloc failed", __func__);
217  return NULL;
218  }
219 
220  udi->bus = bus;
221  udi->address = address;
222  udi->devhdl = hdl; /* TODO: Check if this is NULL? */
223 
224  return udi;
225 }
226 
227 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
228 {
229  /* Avoid compiler warnings. */
230  (void)usb;
231 
232  /* Nothing to do for this device instance type. */
233 }
234 
235 #endif
236 
237 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
238  int fd)
239 {
240  struct sr_serial_dev_inst *serial;
241 
242  if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
243  sr_err("hwdriver: %s: serial malloc failed", __func__);
244  return NULL;
245  }
246 
247  serial->port = g_strdup(port);
248  serial->fd = fd;
249 
250  return serial;
251 }
252 
253 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
254 {
255  g_free(serial->port);
256 }
257 
258 /**
259  * Find out if a hardware driver has a specific capability.
260  *
261  * @param driver The hardware driver in which to search for the capability.
262  * @param hwcap The capability to find in the list.
263  *
264  * @return TRUE if the specified capability exists in the specified driver,
265  * FALSE otherwise. Also, if 'driver' is NULL or the respective driver
266  * returns an invalid capability list, FALSE is returned.
267  */
268 SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
269 {
270  int *hwcaps, i;
271 
272  if (!driver) {
273  sr_err("hwdriver: %s: driver was NULL", __func__);
274  return FALSE;
275  }
276 
277  if (!(hwcaps = driver->hwcap_get_all())) {
278  sr_err("hwdriver: %s: hwcap_get_all() returned NULL", __func__);
279  return FALSE;
280  }
281 
282  for (i = 0; hwcaps[i]; i++) {
283  if (hwcaps[i] == hwcap)
284  return TRUE;
285  }
286 
287  return FALSE;
288 }
289 
290 /**
291  * Get a hardware driver capability option.
292  *
293  * @param hwcap The capability to get.
294  *
295  * @return A pointer to a struct with information about the parameter, or NULL
296  * if the capability was not found.
297  */
299 {
300  int i;
301 
302  for (i = 0; sr_hwcap_options[i].hwcap; i++) {
303  if (sr_hwcap_options[i].hwcap == hwcap)
304  return &sr_hwcap_options[i];
305  }
306 
307  return NULL;
308 }
309 
310 /* Unnecessary level of indirection follows. */
311 
313 {
314  return sr_session_source_remove(fd);
315 }
316 
317 SR_PRIV int sr_source_add(int fd, int events, int timeout,
318  sr_receive_data_callback_t cb, void *cb_data)
319 {
320  return sr_session_source_add(fd, events, timeout, cb, cb_data);
321 }
char * name
Definition: sigrok.h:389
int *(* hwcap_get_all)(void)
Definition: sigrok.h:400
SR_PRIV struct sr_dev_driver chronovu_la8_driver_info
SR_PRIV int sr_source_remove(int fd)
Definition: hwdriver.c:312
SR_PRIV void sr_hw_cleanup_all(void)
Definition: hwdriver.c:151
SR_API struct sr_hwcap_option * sr_hw_hwcap_get(int hwcap)
Get a hardware driver capability option.
Definition: hwdriver.c:298
char * version
Definition: sigrok.h:329
SR_PRIV struct sr_dev_driver ols_driver_info
Definition: ols.c:1042
int inst_type
Definition: sigrok.h:326
SR_API struct sr_hwcap_option sr_hwcap_options[]
Definition: hwdriver.c:34
int(* init)(const char *devinfo)
Definition: sigrok.h:392
The device supports Run Length Encoding.
Definition: sigrok.h:276
void * priv
Definition: sigrok.h:330
Definition: sigrok.h:220
SR_PRIV int sr_source_add(int fd, int events, int timeout, sr_receive_data_callback_t cb, void *cb_data)
Definition: hwdriver.c:317
SR_API int sr_session_source_add(int fd, int events, int timeout, sr_receive_data_callback_t cb, void *cb_data)
TODO.
Definition: session.c:476
SR_PRIV int sr_warn(const char *format,...)
Definition: log.c:241
char * vendor
Definition: sigrok.h:327
SR_API int sr_dev_probe_add(struct sr_dev *dev, const char *name)
Add a probe with the specified name to the specified device.
Definition: device.c:161
SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
Definition: hwdriver.c:253
#define SR_PRIV
Definition: sigrok.h:133
int status
Definition: sigrok.h:325
SR_API int sr_session_source_remove(int fd)
Remove the source belonging to the specified file descriptor.
Definition: session.c:526
int(* cleanup)(void)
Definition: sigrok.h:393
The device supports setting a pattern (pattern generator mode).
Definition: sigrok.h:273
SR_PRIV struct sr_dev_inst * sr_dev_inst_get(GSList *dev_insts, int dev_index)
Definition: hwdriver.c:184
void *(* dev_info_get)(int dev_index, int dev_info_id)
Definition: sigrok.h:398
The device supports setting a pre/post-trigger capture ratio.
Definition: sigrok.h:269
SR_PRIV struct sr_usb_dev_inst * sr_usb_dev_inst_new(uint8_t bus, uint8_t address, struct libusb_device_handle *hdl)
Definition: hwdriver.c:210
SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
Definition: hwdriver.c:227
SR_PRIV struct sr_dev_inst * sr_dev_inst_new(int index, int status, const char *vendor, const char *model, const char *version)
Definition: hwdriver.c:163
#define SR_API
Definition: sigrok.h:126
The device supports setting/changing its samplerate.
Definition: sigrok.h:262
SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
Definition: hwdriver.c:199
SR_PRIV struct sr_dev_driver alsa_driver_info
Definition: alsa.c:399
int(* sr_receive_data_callback_t)(int fd, int revents, void *cb_data)
Definition: sigrok.h:138
SR_PRIV int sr_dbg(const char *format,...)
Definition: log.c:217
SR_API int sr_driver_init(struct sr_dev_driver *driver)
Initialize a hardware driver.
Definition: hwdriver.c:121
SR_PRIV struct sr_dev_driver asix_sigma_driver_info
Definition: asix-sigma.c:1413
SR_PRIV struct sr_dev_driver demo_driver_info
Definition: demo.c:498
SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info
Definition: zeroplus.c:717
int index
Definition: sigrok.h:324
SR_PRIV struct sr_dev_driver saleae_logic_driver_info
Definition: saleae-logic.c:919
SR_API struct sr_dev * sr_dev_new(const struct sr_dev_driver *driver, int driver_index)
Create a new device.
Definition: device.c:120
char * model
Definition: sigrok.h:328
SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
Find out if a hardware driver has a specific capability.
Definition: hwdriver.c:268
SR_API struct sr_dev_driver ** sr_driver_list(void)
Return the list of supported hardware drivers.
Definition: hwdriver.c:106
SR_PRIV struct sr_dev_driver fx2lafw_driver_info
Definition: fx2lafw.c:852
SR_PRIV int sr_err(const char *format,...)
Definition: log.c:253
SR_PRIV struct sr_serial_dev_inst * sr_serial_dev_inst_new(const char *port, int fd)
Definition: hwdriver.c:237