3 * \brief HwDep Interface (hardware dependent)
4 * \author Jaroslav Kysela <perex@perex.cz>
7 * HwDep (hardware dependent) Interface is designed for individual hardware
8 * access. This interface does not cover any API specification.
11 * Hardware dependent Interface - main file
12 * Copyright (c) 2000 by Jaroslav Kysela <perex@perex.cz>
15 * This library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as
17 * published by the Free Software Foundation; either version 2.1 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/ioctl.h>
37 #include "hwdep_local.h"
39 static int snd_hwdep_open_conf(snd_hwdep_t **hwdep,
40 const char *name, snd_config_t *hwdep_root,
41 snd_config_t *hwdep_conf, int mode)
46 snd_config_t *conf, *type_conf = NULL;
47 snd_config_iterator_t i, next;
49 const char *lib = NULL, *open_name = NULL;
50 int (*open_func)(snd_hwdep_t **, const char *, snd_config_t *, snd_config_t *, int) = NULL;
52 extern void *snd_hwdep_open_symbols(void);
55 if (snd_config_get_type(hwdep_conf) != SND_CONFIG_TYPE_COMPOUND) {
57 SNDERR("Invalid type for HWDEP %s definition", name);
59 SNDERR("Invalid type for HWDEP definition");
62 err = snd_config_search(hwdep_conf, "type", &conf);
64 SNDERR("type is not defined");
67 err = snd_config_get_id(conf, &id);
69 SNDERR("unable to get id");
72 err = snd_config_get_string(conf, &str);
74 SNDERR("Invalid type for %s", id);
77 err = snd_config_search_definition(hwdep_root, "hwdep_type", str, &type_conf);
79 if (snd_config_get_type(type_conf) != SND_CONFIG_TYPE_COMPOUND) {
80 SNDERR("Invalid type for HWDEP type %s definition", str);
83 snd_config_for_each(i, next, type_conf) {
84 snd_config_t *n = snd_config_iterator_entry(i);
86 if (snd_config_get_id(n, &id) < 0)
88 if (strcmp(id, "comment") == 0)
90 if (strcmp(id, "lib") == 0) {
91 err = snd_config_get_string(n, &lib);
93 SNDERR("Invalid type for %s", id);
98 if (strcmp(id, "open") == 0) {
99 err = snd_config_get_string(n, &open_name);
101 SNDERR("Invalid type for %s", id);
106 SNDERR("Unknown field %s", id);
113 snprintf(buf, sizeof(buf), "_snd_hwdep_%s_open", str);
116 snd_hwdep_open_symbols();
118 h = snd_dlopen(lib, RTLD_NOW);
120 open_func = snd_dlsym(h, open_name, SND_DLSYM_VERSION(SND_HWDEP_DLSYM_VERSION));
123 SNDERR("Cannot open shared library %s", lib);
125 } else if (!open_func) {
126 SNDERR("symbol %s is not defined inside %s", open_name, lib);
132 snd_config_delete(type_conf);
134 err = open_func(hwdep, name, hwdep_root, hwdep_conf, mode);
136 (*hwdep)->dl_handle = h;
144 static int snd_hwdep_open_noupdate(snd_hwdep_t **hwdep, snd_config_t *root, const char *name, int mode)
147 snd_config_t *hwdep_conf;
148 err = snd_config_search_definition(root, "hwdep", name, &hwdep_conf);
150 SNDERR("Unknown HwDep %s", name);
153 err = snd_hwdep_open_conf(hwdep, name, root, hwdep_conf, mode);
154 snd_config_delete(hwdep_conf);
159 * \brief Opens a new connection to the HwDep interface.
160 * \param hwdep Returned handle (NULL if not wanted)
161 * \param name ASCII identifier of the HwDep handle
162 * \param mode Open mode
163 * \return 0 on success otherwise a negative error code
165 * Opens a new connection to the HwDep interface specified with
166 * an ASCII identifier and mode.
168 int snd_hwdep_open(snd_hwdep_t **hwdep, const char *name, int mode)
171 assert(hwdep && name);
172 err = snd_config_update();
175 return snd_hwdep_open_noupdate(hwdep, snd_config, name, mode);
179 * \brief Opens a new connection to the HwDep interface using local configuration
180 * \param hwdep Returned handle (NULL if not wanted)
181 * \param name ASCII identifier of the HwDep handle
182 * \param mode Open mode
183 * \param lconf The local configuration tree
184 * \return 0 on success otherwise a negative error code
186 * Opens a new connection to the HwDep interface specified with
187 * an ASCII identifier and mode.
189 int snd_hwdep_open_lconf(snd_hwdep_t **hwdep, const char *name,
190 int mode, snd_config_t *lconf)
192 assert(hwdep && name && lconf);
193 return snd_hwdep_open_noupdate(hwdep, lconf, name, mode);
197 * \brief close HwDep handle
198 * \param hwdep HwDep handle
199 * \return 0 on success otherwise a negative error code
201 * Closes the specified HwDep handle and frees all associated
204 int snd_hwdep_close(snd_hwdep_t *hwdep)
208 err = hwdep->ops->close(hwdep);
209 if (hwdep->dl_handle)
210 snd_dlclose(hwdep->dl_handle);
217 * \brief get identifier of HwDep handle
218 * \param hwdep a Hwdep handle
219 * \return ascii identifier of HwDep handle
221 * Returns the ASCII identifier of given HwDep handle. It's the same
222 * identifier specified in snd_hwdep_open().
224 const char *snd_hwdep_name(snd_hwdep_t *hwdep)
231 * \brief get type of HwDep handle
232 * \param hwdep a HwDep handle
233 * \return type of HwDep handle
235 * Returns the type #snd_hwdep_type_t of given HwDep handle.
237 snd_hwdep_type_t snd_hwdep_type(snd_hwdep_t *hwdep)
244 * \brief get count of poll descriptors for HwDep handle
245 * \param hwdep HwDep handle
246 * \return count of poll descriptors
248 int snd_hwdep_poll_descriptors_count(snd_hwdep_t *hwdep)
255 * \brief get poll descriptors
256 * \param hwdep HwDep handle
257 * \param pfds array of poll descriptors
258 * \param space space in the poll descriptor array
259 * \return count of filled descriptors
261 int snd_hwdep_poll_descriptors(snd_hwdep_t *hwdep, struct pollfd *pfds, unsigned int space)
265 pfds->fd = hwdep->poll_fd;
266 switch (hwdep->mode & O_ACCMODE) {
268 pfds->events = POLLOUT|POLLERR|POLLNVAL;
271 pfds->events = POLLIN|POLLERR|POLLNVAL;
274 pfds->events = POLLOUT|POLLIN|POLLERR|POLLNVAL;
285 * \brief get returned events from poll descriptors
286 * \param hwdep HwDep handle
287 * \param pfds array of poll descriptors
288 * \param nfds count of poll descriptors
289 * \param revents returned events
290 * \return zero if success, otherwise a negative error code
292 int snd_hwdep_poll_descriptors_revents(snd_hwdep_t *hwdep, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
294 assert(hwdep && pfds && revents);
296 *revents = pfds->revents;
303 * \brief set nonblock mode
304 * \param hwdep HwDep handle
305 * \param nonblock 0 = block, 1 = nonblock mode
306 * \return 0 on success otherwise a negative error code
308 int snd_hwdep_nonblock(snd_hwdep_t *hwdep, int nonblock)
312 if ((err = hwdep->ops->nonblock(hwdep, nonblock)) < 0)
315 hwdep->mode |= SND_HWDEP_OPEN_NONBLOCK;
317 hwdep->mode &= ~SND_HWDEP_OPEN_NONBLOCK;
322 * \brief get size of the snd_hwdep_info_t structure in bytes
323 * \return size of the snd_hwdep_info_t structure in bytes
325 size_t snd_hwdep_info_sizeof()
327 return sizeof(snd_hwdep_info_t);
331 * \brief allocate a new snd_hwdep_info_t structure
332 * \param info returned pointer
333 * \return 0 on success otherwise a negative error code if fails
335 * Allocates a new snd_hwdep_info_t structure using the standard
336 * malloc C library function.
338 int snd_hwdep_info_malloc(snd_hwdep_info_t **info)
341 *info = calloc(1, sizeof(snd_hwdep_info_t));
348 * \brief frees the snd_hwdep_info_t structure
349 * \param info pointer to the snd_hwdep_info_t structure to free
351 * Frees the given snd_hwdep_info_t structure using the standard
352 * free C library function.
354 void snd_hwdep_info_free(snd_hwdep_info_t *info)
361 * \brief copy one snd_hwdep_info_t structure to another
362 * \param dst destination snd_hwdep_info_t structure
363 * \param src source snd_hwdep_info_t structure
365 void snd_hwdep_info_copy(snd_hwdep_info_t *dst, const snd_hwdep_info_t *src)
372 * \brief get hwdep card number
373 * \param obj pointer to a snd_hwdep_info_t structure
374 * \return hwdep card number
376 int snd_hwdep_info_get_card(const snd_hwdep_info_t *obj)
383 * \brief get hwdep device number
384 * \param info pointer to a snd_hwdep_info_t structure
385 * \return hwdep device number
387 unsigned int snd_hwdep_info_get_device(const snd_hwdep_info_t *info)
394 * \brief get hwdep driver identifier
395 * \param obj pointer to a snd_hwdep_info_t structure
396 * \return hwdep driver identifier
398 const char *snd_hwdep_info_get_id(const snd_hwdep_info_t *obj)
401 return (const char *)obj->id;
405 * \brief get hwdep driver name
406 * \param obj pointer to a snd_hwdep_info_t structure
407 * \return hwdep driver name
409 const char *snd_hwdep_info_get_name(const snd_hwdep_info_t *obj)
412 return (const char *)obj->name;
416 * \brief get hwdep protocol interface
417 * \param obj pointer to a snd_hwdep_info_t structure
418 * \return hwdep protocol interface
420 snd_hwdep_iface_t snd_hwdep_info_get_iface(const snd_hwdep_info_t *obj)
427 * \brief set hwdep device number
428 * \param obj pointer to a snd_hwdep_info_t structure
429 * \param val hwdep device
431 void snd_hwdep_info_set_device(snd_hwdep_info_t *obj, unsigned int val)
438 * \brief get information about HwDep handle
439 * \param hwdep HwDep handle
440 * \param info pointer to a snd_hwdep_info_t structure to be filled
441 * \return 0 on success otherwise a negative error code
443 int snd_hwdep_info(snd_hwdep_t *hwdep, snd_hwdep_info_t * info)
447 return hwdep->ops->info(hwdep, info);
451 * \brief do hardware dependent ioctl
452 * \param hwdep HwDep handle
453 * \param request ioctl command
454 * \param arg ioctl argument
455 * \return 0 on success otherwise a negative error code
457 int snd_hwdep_ioctl(snd_hwdep_t *hwdep, unsigned int request, void * arg)
460 return hwdep->ops->ioctl(hwdep, request, arg);
464 * \brief write bytes using HwDep handle
465 * \param hwdep HwDep handle
466 * \param buffer buffer containing bytes to write
467 * \param size output buffer size in bytes
469 ssize_t snd_hwdep_write(snd_hwdep_t *hwdep, const void *buffer, size_t size)
472 assert(((hwdep->mode & O_ACCMODE) == O_WRONLY) || ((hwdep->mode & O_ACCMODE) == O_RDWR));
473 assert(buffer || size == 0);
474 return hwdep->ops->write(hwdep, buffer, size);
478 * \brief read bytes using HwDep handle
479 * \param hwdep HwDep handle
480 * \param buffer buffer to store the input bytes
481 * \param size input buffer size in bytes
483 ssize_t snd_hwdep_read(snd_hwdep_t *hwdep, void *buffer, size_t size)
486 assert(((hwdep->mode & O_ACCMODE) == O_RDONLY) || ((hwdep->mode & O_ACCMODE) == O_RDWR));
487 assert(buffer || size == 0);
488 return (hwdep->ops->read)(hwdep, buffer, size);
492 * \brief get the DSP status information
493 * \param hwdep HwDep handle
494 * \param info pointer to a snd_hwdep_dsp_status_t structure to be filled
495 * \return 0 on success otherwise a negative error code
497 int snd_hwdep_dsp_status(snd_hwdep_t *hwdep, snd_hwdep_dsp_status_t *info)
501 return hwdep->ops->ioctl(hwdep, SNDRV_HWDEP_IOCTL_DSP_STATUS, (void*)info);
505 * \brief load the DSP block
506 * \param hwdep HwDep handle
507 * \param block pointer to a snd_hwdep_dsp_image_t structure to transfer
508 * \return 0 on success otherwise a negative error code
510 int snd_hwdep_dsp_load(snd_hwdep_t *hwdep, snd_hwdep_dsp_image_t *block)
514 return hwdep->ops->ioctl(hwdep, SNDRV_HWDEP_IOCTL_DSP_LOAD, (void*)block);
518 * \brief get size of the snd_hwdep_dsp_status_t structure in bytes
519 * \return size of the snd_hwdep_dsp_status_t structure in bytes
521 size_t snd_hwdep_dsp_status_sizeof()
523 return sizeof(snd_hwdep_dsp_status_t);
527 * \brief allocate a new snd_hwdep_dsp_status_t structure
528 * \param info returned pointer
529 * \return 0 on success otherwise a negative error code if fails
531 * Allocates a new snd_hwdep_dsp_status_t structure using the standard
532 * malloc C library function.
534 int snd_hwdep_dsp_status_malloc(snd_hwdep_dsp_status_t **info)
537 *info = calloc(1, sizeof(snd_hwdep_dsp_status_t));
544 * \brief frees the snd_hwdep_dsp_status_t structure
545 * \param info pointer to the snd_hwdep_dsp_status_t structure to free
547 * Frees the given snd_hwdep_dsp_status_t structure using the standard
548 * free C library function.
550 void snd_hwdep_dsp_status_free(snd_hwdep_dsp_status_t *info)
557 * \brief copy one snd_hwdep_dsp_status_t structure to another
558 * \param dst destination snd_hwdep_dsp_status_t structure
559 * \param src source snd_hwdep_dsp_status_t structure
561 void snd_hwdep_dsp_status_copy(snd_hwdep_dsp_status_t *dst, const snd_hwdep_dsp_status_t *src)
568 * \brief get the driver version of dsp loader
569 * \param obj pointer to a snd_hwdep_dsp_status_t structure
570 * \return the driver version
572 unsigned int snd_hwdep_dsp_status_get_version(const snd_hwdep_dsp_status_t *obj)
579 * \brief get the driver id of dsp loader
580 * \param obj pointer to a snd_hwdep_dsp_status_t structure
581 * \return the driver id string
583 const char *snd_hwdep_dsp_status_get_id(const snd_hwdep_dsp_status_t *obj)
586 return (const char *)obj->id;
590 * \brief get number of dsp blocks
591 * \param obj pointer to a snd_hwdep_dsp_status_t structure
592 * \return number of dsp blocks
594 unsigned int snd_hwdep_dsp_status_get_num_dsps(const snd_hwdep_dsp_status_t *obj)
597 return obj->num_dsps;
601 * \brief get the bit flags of the loaded dsp blocks
602 * \param info pointer to a snd_hwdep_dsp_status_t structure
603 * \return the big flags of the loaded dsp blocks
605 unsigned int snd_hwdep_dsp_status_get_dsp_loaded(const snd_hwdep_dsp_status_t *info)
608 return info->dsp_loaded;
612 * \brief get the chip status of dsp loader
613 * \param obj pointer to a snd_hwdep_dsp_status_t structure
614 * \return non-zero if all DSP blocks are loaded and the chip is ready
616 unsigned int snd_hwdep_dsp_status_get_chip_ready(const snd_hwdep_dsp_status_t *obj)
619 return obj->chip_ready;
623 * \brief get size of the snd_hwdep_dsp_image_t structure in bytes
624 * \return size of the snd_hwdep_dsp_image_t structure in bytes
626 size_t snd_hwdep_dsp_image_sizeof()
628 return sizeof(snd_hwdep_dsp_image_t);
632 * \brief allocate a new snd_hwdep_dsp_image_t structure
633 * \param info returned pointer
634 * \return 0 on success otherwise a negative error code if fails
636 * Allocates a new snd_hwdep_dsp_image_t structure using the standard
637 * malloc C library function.
639 int snd_hwdep_dsp_image_malloc(snd_hwdep_dsp_image_t **info)
642 *info = calloc(1, sizeof(snd_hwdep_dsp_image_t));
649 * \brief frees the snd_hwdep_dsp_image_t structure
650 * \param info pointer to the snd_hwdep_dsp_image_t structure to free
652 * Frees the given snd_hwdep_dsp_image_t structure using the standard
653 * free C library function.
655 void snd_hwdep_dsp_image_free(snd_hwdep_dsp_image_t *info)
662 * \brief copy one snd_hwdep_dsp_image_t structure to another
663 * \param dst destination snd_hwdep_dsp_image_t structure
664 * \param src source snd_hwdep_dsp_image_t structure
666 void snd_hwdep_dsp_image_copy(snd_hwdep_dsp_image_t *dst, const snd_hwdep_dsp_image_t *src)
673 * \brief get the DSP block index
674 * \param obj pointer to a snd_hwdep_dsp_image_t structure
675 * \return the index of the DSP block
677 unsigned int snd_hwdep_dsp_image_get_index(const snd_hwdep_dsp_image_t *obj)
684 * \brief get the name of the DSP block
685 * \param obj pointer to a snd_hwdep_dsp_image_t structure
686 * \return the name string of the DSP block
688 const char *snd_hwdep_dsp_image_get_name(const snd_hwdep_dsp_image_t *obj)
691 return (const char *)obj->name;
695 * \brief get the length of the DSP block
696 * \param obj pointer to a snd_hwdep_dsp_image_t structure
697 * \return the length of the DSP block in bytes
699 size_t snd_hwdep_dsp_image_get_length(const snd_hwdep_dsp_image_t *obj)
706 * \brief get the image pointer of the DSP block
707 * \param obj pointer to a snd_hwdep_dsp_image_t structure
708 * \return the image pointer of the DSP block
710 const void *snd_hwdep_dsp_image_get_image(const snd_hwdep_dsp_image_t *obj)
717 * \brief set the DSP block index
718 * \param obj pointer to a snd_hwdep_dsp_image_t structure
719 * \param index the index value to set
721 void snd_hwdep_dsp_image_set_index(snd_hwdep_dsp_image_t *obj, unsigned int index)
728 * \brief set the name of the DSP block
729 * \param obj pointer to a snd_hwdep_dsp_image_t structure
730 * \param name the name string
732 void snd_hwdep_dsp_image_set_name(snd_hwdep_dsp_image_t *obj, const char *name)
735 strncpy((char *)obj->name, name, sizeof(obj->name));
736 obj->name[sizeof(obj->name)-1] = 0;
740 * \brief set the DSP block length
741 * \param obj pointer to a snd_hwdep_dsp_image_t structure
742 * \param length the length of the DSP block
744 void snd_hwdep_dsp_image_set_length(snd_hwdep_dsp_image_t *obj, size_t length)
747 obj->length = length;
751 * \brief set the DSP block image pointer
752 * \param obj pointer to a snd_hwdep_dsp_image_t structure
753 * \param image the DSP image pointer
755 void snd_hwdep_dsp_image_set_image(snd_hwdep_dsp_image_t *obj, void *image)