"Fossies" - the Fresh Open Source Software Archive

Member "pulseaudio-14.2/src/modules/module-card-restore.c" (11 Jan 2021, 19403 Bytes) of package /linux/misc/pulseaudio-14.2.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "module-card-restore.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 14.0_vs_14.2.

    1 /***
    2   This file is part of PulseAudio.
    3 
    4   Copyright 2006-2008 Lennart Poettering
    5 
    6   PulseAudio is free software; you can redistribute it and/or modify
    7   it under the terms of the GNU Lesser General Public License as published
    8   by the Free Software Foundation; either version 2.1 of the License,
    9   or (at your option) any later version.
   10 
   11   PulseAudio is distributed in the hope that it will be useful, but
   12   WITHOUT ANY WARRANTY; without even the implied warranty of
   13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   14   General Public License for more details.
   15 
   16   You should have received a copy of the GNU Lesser General Public License
   17   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
   18 ***/
   19 
   20 #ifdef HAVE_CONFIG_H
   21 #include <config.h>
   22 #endif
   23 
   24 #include <unistd.h>
   25 #include <string.h>
   26 #include <errno.h>
   27 #include <sys/types.h>
   28 #include <stdio.h>
   29 #include <stdlib.h>
   30 
   31 #include <pulse/gccmacro.h>
   32 #include <pulse/xmalloc.h>
   33 #include <pulse/timeval.h>
   34 #include <pulse/rtclock.h>
   35 
   36 #include <pulsecore/core-error.h>
   37 #include <pulsecore/module.h>
   38 #include <pulsecore/core-util.h>
   39 #include <pulsecore/modargs.h>
   40 #include <pulsecore/log.h>
   41 #include <pulsecore/core-subscribe.h>
   42 #include <pulsecore/card.h>
   43 #include <pulsecore/namereg.h>
   44 #include <pulsecore/database.h>
   45 #include <pulsecore/tagstruct.h>
   46 
   47 PA_MODULE_AUTHOR("Lennart Poettering");
   48 PA_MODULE_DESCRIPTION("Automatically restore profile of cards");
   49 PA_MODULE_VERSION(PACKAGE_VERSION);
   50 PA_MODULE_LOAD_ONCE(true);
   51 PA_MODULE_USAGE(
   52     "restore_bluetooth_profile=<boolean>"
   53 );
   54 
   55 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
   56 
   57 static const char* const valid_modargs[] = {
   58     "restore_bluetooth_profile",
   59     NULL
   60 };
   61 
   62 struct userdata {
   63     pa_core *core;
   64     pa_module *module;
   65     pa_time_event *save_time_event;
   66     pa_database *database;
   67     bool restore_bluetooth_profile;
   68 };
   69 
   70 #define ENTRY_VERSION 4
   71 
   72 struct port_info {
   73     char *name;
   74     int64_t offset;
   75     char *profile;
   76 };
   77 
   78 struct entry {
   79     char *profile;
   80     pa_hashmap *ports; /* Port name -> struct port_info */
   81     char *preferred_input_port;
   82     char *preferred_output_port;
   83 };
   84 
   85 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
   86     struct userdata *u = userdata;
   87 
   88     pa_assert(a);
   89     pa_assert(e);
   90     pa_assert(u);
   91 
   92     pa_assert(e == u->save_time_event);
   93     u->core->mainloop->time_free(u->save_time_event);
   94     u->save_time_event = NULL;
   95 
   96     pa_database_sync(u->database);
   97     pa_log_info("Synced.");
   98 }
   99 
  100 static void trigger_save(struct userdata *u) {
  101     if (u->save_time_event)
  102         return;
  103 
  104     u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
  105 }
  106 
  107 static void port_info_free(struct port_info *p_info) {
  108     pa_assert(p_info);
  109 
  110     pa_xfree(p_info->profile);
  111     pa_xfree(p_info->name);
  112     pa_xfree(p_info);
  113 }
  114 
  115 static struct entry* entry_new(void) {
  116     struct entry *r = pa_xnew0(struct entry, 1);
  117     r->ports = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) port_info_free);
  118     return r;
  119 }
  120 
  121 static struct port_info *port_info_new(pa_device_port *port) {
  122     struct port_info *p_info;
  123 
  124     if (port) {
  125         p_info = pa_xnew0(struct port_info, 1);
  126         p_info->name = pa_xstrdup(port->name);
  127         p_info->offset = port->latency_offset;
  128         if (port->preferred_profile)
  129             p_info->profile = pa_xstrdup(port->preferred_profile);
  130     } else
  131         p_info = pa_xnew0(struct port_info, 1);
  132 
  133     return p_info;
  134 }
  135 
  136 static void entry_free(struct entry* e) {
  137     pa_assert(e);
  138 
  139     pa_xfree(e->preferred_output_port);
  140     pa_xfree(e->preferred_input_port);
  141     pa_xfree(e->profile);
  142     pa_hashmap_free(e->ports);
  143 
  144     pa_xfree(e);
  145 }
  146 
  147 static struct entry *entry_from_card(pa_card *card) {
  148     struct port_info *p_info;
  149     struct entry *entry;
  150     pa_device_port *port;
  151     void *state;
  152 
  153     pa_assert(card);
  154 
  155     entry = entry_new();
  156     if (card->save_profile)
  157         entry->profile = pa_xstrdup(card->active_profile->name);
  158 
  159     PA_HASHMAP_FOREACH(port, card->ports, state) {
  160         p_info = port_info_new(port);
  161         pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
  162     }
  163 
  164     return entry;
  165 }
  166 
  167 static bool entrys_equal(struct entry *a, struct entry *b) {
  168     struct port_info *Ap_info, *Bp_info;
  169     void *state;
  170 
  171     pa_assert(a);
  172     pa_assert(b);
  173 
  174     if (!pa_streq(a->profile, b->profile) ||
  175             pa_hashmap_size(a->ports) != pa_hashmap_size(b->ports))
  176         return false;
  177 
  178     PA_HASHMAP_FOREACH(Ap_info, a->ports, state) {
  179         if ((Bp_info = pa_hashmap_get(b->ports, Ap_info->name))) {
  180             if (Ap_info->offset != Bp_info->offset)
  181                 return false;
  182         } else
  183             return false;
  184     }
  185 
  186     if (!pa_safe_streq(a->preferred_input_port, b->preferred_input_port))
  187         return false;
  188 
  189     if (!pa_safe_streq(a->preferred_output_port, b->preferred_output_port))
  190         return false;
  191 
  192     return true;
  193 }
  194 
  195 static bool entry_write(struct userdata *u, const char *name, const struct entry *e) {
  196     pa_tagstruct *t;
  197     pa_datum key, data;
  198     bool r;
  199     void *state;
  200     struct port_info *p_info;
  201 
  202     pa_assert(u);
  203     pa_assert(name);
  204     pa_assert(e);
  205 
  206     t = pa_tagstruct_new();
  207     pa_tagstruct_putu8(t, ENTRY_VERSION);
  208     pa_tagstruct_puts(t, e->profile);
  209     pa_tagstruct_putu32(t, pa_hashmap_size(e->ports));
  210 
  211     PA_HASHMAP_FOREACH(p_info, e->ports, state) {
  212         pa_tagstruct_puts(t, p_info->name);
  213         pa_tagstruct_puts64(t, p_info->offset);
  214         pa_tagstruct_puts(t, p_info->profile);
  215     }
  216 
  217     pa_tagstruct_puts(t, e->preferred_input_port);
  218     pa_tagstruct_puts(t, e->preferred_output_port);
  219 
  220     key.data = (char *) name;
  221     key.size = strlen(name);
  222 
  223     data.data = (void*)pa_tagstruct_data(t, &data.size);
  224 
  225     r = (pa_database_set(u->database, &key, &data, true) == 0);
  226 
  227     pa_tagstruct_free(t);
  228 
  229     return r;
  230 }
  231 
  232 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
  233 
  234 #define LEGACY_ENTRY_VERSION 1
  235 static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
  236     struct legacy_entry {
  237         uint8_t version;
  238         char profile[PA_NAME_MAX];
  239     } PA_GCC_PACKED ;
  240     struct legacy_entry *le;
  241     struct entry *e;
  242 
  243     pa_assert(u);
  244     pa_assert(data);
  245 
  246     if (data->size != sizeof(struct legacy_entry)) {
  247         pa_log_debug("Size does not match.");
  248         return NULL;
  249     }
  250 
  251     le = (struct legacy_entry*)data->data;
  252 
  253     if (le->version != LEGACY_ENTRY_VERSION) {
  254         pa_log_debug("Version mismatch.");
  255         return NULL;
  256     }
  257 
  258     if (!memchr(le->profile, 0, sizeof(le->profile))) {
  259         pa_log_warn("Profile has missing NUL byte.");
  260         return NULL;
  261     }
  262 
  263     e = entry_new();
  264     e->profile = pa_xstrdup(le->profile);
  265     return e;
  266 }
  267 #endif
  268 
  269 static struct entry* entry_read(struct userdata *u, const char *name) {
  270     pa_datum key, data;
  271     struct entry *e = NULL;
  272     pa_tagstruct *t = NULL;
  273     const char* profile;
  274     uint8_t version;
  275 
  276     pa_assert(u);
  277     pa_assert(name);
  278 
  279     key.data = (char*) name;
  280     key.size = strlen(name);
  281 
  282     pa_zero(data);
  283 
  284     if (!pa_database_get(u->database, &key, &data)) {
  285         pa_log_debug("Database contains no data for key: %s", name);
  286         return NULL;
  287     }
  288 
  289     t = pa_tagstruct_new_fixed(data.data, data.size);
  290     e = entry_new();
  291 
  292     if (pa_tagstruct_getu8(t, &version) < 0 ||
  293         version > ENTRY_VERSION ||
  294         pa_tagstruct_gets(t, &profile) < 0) {
  295 
  296         goto fail;
  297     }
  298 
  299     if (!profile)
  300         profile = "";
  301 
  302     e->profile = pa_xstrdup(profile);
  303 
  304     if (version >= 2) {
  305         uint32_t port_count = 0;
  306         const char *port_name = NULL, *profile_name = NULL;
  307         int64_t port_offset = 0;
  308         struct port_info *p_info;
  309         unsigned i;
  310 
  311         if (pa_tagstruct_getu32(t, &port_count) < 0)
  312             goto fail;
  313 
  314         for (i = 0; i < port_count; i++) {
  315             if (pa_tagstruct_gets(t, &port_name) < 0 ||
  316                 !port_name ||
  317                 pa_hashmap_get(e->ports, port_name) ||
  318                 pa_tagstruct_gets64(t, &port_offset) < 0)
  319                 goto fail;
  320             if (version >= 3 && pa_tagstruct_gets(t, &profile_name) < 0)
  321                 goto fail;
  322 
  323             p_info = port_info_new(NULL);
  324             p_info->name = pa_xstrdup(port_name);
  325             p_info->offset = port_offset;
  326             if (profile_name)
  327                 p_info->profile = pa_xstrdup(profile_name);
  328 
  329             pa_assert_se(pa_hashmap_put(e->ports, p_info->name, p_info) >= 0);
  330         }
  331     }
  332 
  333     if (version >= 4) {
  334         const char *preferred_input_port;
  335         const char *preferred_output_port;
  336 
  337         if (pa_tagstruct_gets(t, &preferred_input_port) < 0
  338                 || pa_tagstruct_gets(t, &preferred_output_port) < 0)
  339             goto fail;
  340 
  341         e->preferred_input_port = pa_xstrdup(preferred_input_port);
  342         e->preferred_output_port = pa_xstrdup(preferred_output_port);
  343     }
  344 
  345     if (!pa_tagstruct_eof(t))
  346         goto fail;
  347 
  348     pa_tagstruct_free(t);
  349     pa_datum_free(&data);
  350 
  351     return e;
  352 
  353 fail:
  354 
  355     pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
  356 
  357     if (e)
  358         entry_free(e);
  359     if (t)
  360         pa_tagstruct_free(t);
  361 
  362 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
  363     pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
  364     if ((e = legacy_entry_read(u, &data))) {
  365         pa_log_debug("Success. Saving new format for key: %s", name);
  366         if (entry_write(u, name, e))
  367             trigger_save(u);
  368         pa_datum_free(&data);
  369         return e;
  370     } else
  371         pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
  372 #endif
  373 
  374     pa_datum_free(&data);
  375     return NULL;
  376 }
  377 
  378 static void show_full_info(pa_card *card) {
  379     pa_assert(card);
  380 
  381     if (card->save_profile)
  382         pa_log_info("Storing profile and port latency offsets for card %s.", card->name);
  383     else
  384         pa_log_info("Storing port latency offsets for card %s.", card->name);
  385 }
  386 
  387 static pa_hook_result_t card_put_hook_callback(pa_core *c, pa_card *card, struct userdata *u) {
  388     struct entry *entry, *old;
  389 
  390     pa_assert(card);
  391 
  392     entry = entry_from_card(card);
  393 
  394     if ((old = entry_read(u, card->name))) {
  395         if (!card->save_profile)
  396             entry->profile = pa_xstrdup(old->profile);
  397         if (entrys_equal(entry, old))
  398             goto finish;
  399     }
  400 
  401     show_full_info(card);
  402 
  403     if (entry_write(u, card->name, entry))
  404         trigger_save(u);
  405 
  406 finish:
  407     entry_free(entry);
  408     if (old)
  409         entry_free(old);
  410 
  411     return PA_HOOK_OK;
  412 }
  413 
  414 static void update_profile_for_port(struct entry *entry, pa_card *card, pa_device_port *p) {
  415     struct port_info *p_info;
  416 
  417     if (p == NULL)
  418         return;
  419 
  420     if (!(p_info = pa_hashmap_get(entry->ports, p->name))) {
  421         p_info = port_info_new(p);
  422         pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
  423     }
  424 
  425     if (!pa_safe_streq(p_info->profile, p->preferred_profile)) {
  426         pa_xfree(p_info->profile);
  427         p_info->profile = pa_xstrdup(p->preferred_profile);
  428         pa_log_info("Storing profile %s for port %s on card %s.", p_info->profile, p->name, card->name);
  429     }
  430 }
  431 
  432 static pa_hook_result_t card_profile_changed_callback(pa_core *c, pa_card *card, struct userdata *u) {
  433     struct entry *entry;
  434     pa_sink *sink;
  435     pa_source *source;
  436     uint32_t state;
  437 
  438     pa_assert(card);
  439 
  440     if (!card->save_profile)
  441         return PA_HOOK_OK;
  442 
  443     if ((entry = entry_read(u, card->name))) {
  444         pa_xfree(entry->profile);
  445         entry->profile = pa_xstrdup(card->active_profile->name);
  446         pa_log_info("Storing card profile for card %s.", card->name);
  447     } else {
  448         entry = entry_from_card(card);
  449         show_full_info(card);
  450     }
  451 
  452     PA_IDXSET_FOREACH(sink, card->sinks, state)
  453         update_profile_for_port(entry, card, sink->active_port);
  454     PA_IDXSET_FOREACH(source, card->sources, state)
  455         update_profile_for_port(entry, card, source->active_port);
  456 
  457     if (entry_write(u, card->name, entry))
  458         trigger_save(u);
  459 
  460     entry_free(entry);
  461     return PA_HOOK_OK;
  462 }
  463 
  464 static pa_hook_result_t card_profile_added_callback(pa_core *c, pa_card_profile *profile, struct userdata *u) {
  465     struct entry *entry;
  466 
  467     pa_assert(profile);
  468 
  469     if (profile->available == PA_AVAILABLE_NO)
  470         return PA_HOOK_OK;
  471 
  472     if (!(entry = entry_read(u, profile->card->name)))
  473         return PA_HOOK_OK;
  474 
  475     if (pa_safe_streq(entry->profile, profile->name)) {
  476         if (pa_card_set_profile(profile->card, profile, true) >= 0)
  477             pa_log_info("Restored profile '%s' for card %s.", profile->name, profile->card->name);
  478     }
  479 
  480     entry_free(entry);
  481 
  482     return PA_HOOK_OK;
  483 }
  484 
  485 static pa_hook_result_t port_offset_change_callback(pa_core *c, pa_device_port *port, struct userdata *u) {
  486     struct entry *entry;
  487     pa_card *card;
  488 
  489     pa_assert(port);
  490     card = port->card;
  491 
  492     if ((entry = entry_read(u, card->name))) {
  493         struct port_info *p_info;
  494 
  495         if ((p_info = pa_hashmap_get(entry->ports, port->name)))
  496             p_info->offset = port->latency_offset;
  497         else {
  498             p_info = port_info_new(port);
  499             pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
  500         }
  501 
  502         pa_log_info("Storing latency offset for port %s on card %s.", port->name, card->name);
  503 
  504     } else {
  505         entry = entry_from_card(card);
  506         show_full_info(card);
  507     }
  508 
  509     if (entry_write(u, card->name, entry))
  510         trigger_save(u);
  511 
  512     entry_free(entry);
  513     return PA_HOOK_OK;
  514 }
  515 
  516 static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
  517     struct entry *e;
  518     void *state;
  519     pa_device_port *p;
  520     struct port_info *p_info;
  521 
  522     pa_assert(new_data);
  523 
  524     if (!(e = entry_read(u, new_data->name)))
  525         return PA_HOOK_OK;
  526 
  527     /* Always restore the latency offsets because their
  528      * initial value is always 0 */
  529 
  530     pa_log_info("Restoring port latency offsets for card %s.", new_data->name);
  531 
  532     PA_HASHMAP_FOREACH(p_info, e->ports, state)
  533         if ((p = pa_hashmap_get(new_data->ports, p_info->name))) {
  534             p->latency_offset = p_info->offset;
  535             if (!p->preferred_profile && p_info->profile)
  536                 pa_device_port_set_preferred_profile(p, p_info->profile);
  537         }
  538 
  539     if (e->preferred_input_port) {
  540         p = pa_hashmap_get(new_data->ports, e->preferred_input_port);
  541         if (p)
  542             pa_card_new_data_set_preferred_port(new_data, PA_DIRECTION_INPUT, p);
  543     }
  544 
  545     if (e->preferred_output_port) {
  546         p = pa_hashmap_get(new_data->ports, e->preferred_output_port);
  547         if (p)
  548             pa_card_new_data_set_preferred_port(new_data, PA_DIRECTION_OUTPUT, p);
  549     }
  550 
  551     entry_free(e);
  552 
  553     return PA_HOOK_OK;
  554 }
  555 
  556 static pa_hook_result_t card_choose_initial_profile_callback(pa_core *core, pa_card *card, struct userdata *u) {
  557     struct entry *e;
  558 
  559     if (!(e = entry_read(u, card->name)))
  560         return PA_HOOK_OK;
  561 
  562     if (!u->restore_bluetooth_profile) {
  563         const char *s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS);
  564         if (pa_safe_streq(s, "bluetooth"))
  565             goto finish;
  566     }
  567 
  568     if (e->profile[0]) {
  569         pa_card_profile *profile;
  570 
  571         profile = pa_hashmap_get(card->profiles, e->profile);
  572         if (profile) {
  573             if (profile->available != PA_AVAILABLE_NO) {
  574                 pa_log_info("Restoring profile '%s' for card %s.", profile->name, card->name);
  575                 pa_card_set_profile(card, profile, true);
  576             } else
  577                 pa_log_debug("Not restoring profile %s for card %s, because the profile is currently unavailable.",
  578                              profile->name, card->name);
  579         } else {
  580             pa_log_debug("Tried to restore profile %s for card %s, but the card doesn't have such profile.",
  581                          e->profile, card->name);
  582         }
  583     }
  584 
  585 finish:
  586     entry_free(e);
  587 
  588     return PA_HOOK_OK;
  589 }
  590 
  591 static pa_hook_result_t card_preferred_port_changed_callback(pa_core *core, pa_card_preferred_port_changed_hook_data *data,
  592                                                              struct userdata *u) {
  593     struct entry *e;
  594     pa_card *card;
  595 
  596     card = data->card;
  597 
  598     e = entry_read(u, card->name);
  599     if (!e)
  600         e = entry_from_card(card);
  601 
  602     if (data->direction == PA_DIRECTION_INPUT) {
  603         pa_xfree(e->preferred_input_port);
  604         e->preferred_input_port = pa_xstrdup(card->preferred_input_port ? card->preferred_input_port->name : NULL);
  605     } else {
  606         pa_xfree(e->preferred_output_port);
  607         e->preferred_output_port = pa_xstrdup(card->preferred_output_port ? card->preferred_output_port->name : NULL);
  608     }
  609 
  610     if (entry_write(u, card->name, e))
  611         trigger_save(u);
  612 
  613     entry_free(e);
  614 
  615     return PA_HOOK_OK;
  616 }
  617 
  618 int pa__init(pa_module*m) {
  619     pa_modargs *ma = NULL;
  620     struct userdata *u;
  621     char *state_path;
  622     bool restore_bluetooth_profile;
  623 
  624     pa_assert(m);
  625 
  626     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  627         pa_log("Failed to parse module arguments");
  628         goto fail;
  629     }
  630 
  631     restore_bluetooth_profile = false;
  632     if (pa_modargs_get_value_boolean(ma, "restore_bluetooth_profile", &restore_bluetooth_profile) < 0) {
  633         pa_log("Invalid boolean value for restore_bluetooth_profile parameter");
  634         goto fail;
  635     }
  636 
  637     m->userdata = u = pa_xnew0(struct userdata, 1);
  638     u->core = m->core;
  639     u->module = m;
  640     u->restore_bluetooth_profile = restore_bluetooth_profile;
  641 
  642     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u);
  643     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_CHOOSE_INITIAL_PROFILE], PA_HOOK_NORMAL,
  644                            (pa_hook_cb_t) card_choose_initial_profile_callback, u);
  645     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) card_put_hook_callback, u);
  646     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PREFERRED_PORT_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) card_preferred_port_changed_callback, u);
  647     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) card_profile_changed_callback, u);
  648     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], PA_HOOK_NORMAL, (pa_hook_cb_t) card_profile_added_callback, u);
  649     pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) port_offset_change_callback, u);
  650 
  651     if (!(state_path = pa_state_path(NULL, true)))
  652         goto fail;
  653 
  654     if (!(u->database = pa_database_open(state_path, "card-database", true, true))) {
  655         pa_xfree(state_path);
  656         goto fail;
  657     }
  658 
  659     pa_xfree(state_path);
  660 
  661     pa_modargs_free(ma);
  662     return 0;
  663 
  664 fail:
  665     pa__done(m);
  666 
  667     if (ma)
  668         pa_modargs_free(ma);
  669 
  670     return -1;
  671 }
  672 
  673 void pa__done(pa_module*m) {
  674     struct userdata* u;
  675 
  676     pa_assert(m);
  677 
  678     if (!(u = m->userdata))
  679         return;
  680 
  681     if (u->save_time_event) {
  682         u->core->mainloop->time_free(u->save_time_event);
  683         pa_database_sync(u->database);
  684     }
  685 
  686     if (u->database)
  687         pa_database_close(u->database);
  688 
  689     pa_xfree(u);
  690 }