synaptics.c 43.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (C) 2019 Synaptics Inc
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#define FP_COMPONENT "synaptics"

#include "drivers_api.h"

#include "fpi-byte-reader.h"

#include "synaptics.h"
#include "bmkt_message.h"

28
G_DEFINE_TYPE (FpiDeviceSynaptics, fpi_device_synaptics, FP_TYPE_DEVICE)
29

30 31 32
static void init_identify_msg (FpDevice *device);
static void compose_and_send_identify_msg (FpDevice *device);

33 34
static const FpIdEntry id_table[] = {
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xBD,  },
35 36
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xE9,  },
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xDF,  },
37 38 39
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xF9,  },
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xFC,  },
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xC2,  },
40
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xC9,  },
Aris Lin's avatar
Aris Lin committed
41
  { .vid = SYNAPTICS_VENDOR_ID,  .pid = 0xE7,  },
42
  { .vid = 0,  .pid = 0,  .driver_data = 0 },   /* terminating entry */
43 44 45 46
};


static void
Bastien Nocera's avatar
Bastien Nocera committed
47
cmd_receive_cb (FpiUsbTransfer *transfer,
48 49 50
                FpDevice       *device,
                gpointer        user_data,
                GError         *error)
51
{
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  SynCmdMsgCallback callback = user_data;
  int res;
  bmkt_msg_resp_t msg_resp;
  bmkt_response_t resp;

  if (error)
    {
      /* NOTE: assumes timeout should never happen for receiving. */
      fpi_ssm_mark_failed (transfer->ssm, error);
      return;
    }

  res = bmkt_parse_message_header (&transfer->buffer[SENSOR_FW_REPLY_HEADER_LEN],
                                   transfer->actual_length - SENSOR_FW_REPLY_HEADER_LEN,
                                   &msg_resp);
  if (res != BMKT_SUCCESS)
    {
      g_warning ("Corrupted message received");
      fpi_ssm_mark_failed (transfer->ssm,
                           fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
      return;
    }

  /* Special case events */
  if (msg_resp.msg_id == BMKT_EVT_FINGER_REPORT)
    {
      if (msg_resp.payload_len != 1)
        {
          g_warning ("Corrupted finger report received");
          fpi_ssm_mark_failed (transfer->ssm,
                               fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
          return;
        }

      if (msg_resp.payload[0] == 0x01)
        {
          self->finger_on_sensor = TRUE;
90 91 92
          fpi_device_report_finger_status_changes (device,
                                                   FP_FINGER_STATUS_PRESENT,
                                                   FP_FINGER_STATUS_NONE);
93 94 95 96
        }
      else
        {
          self->finger_on_sensor = FALSE;
97 98 99 100
          fpi_device_report_finger_status_changes (device,
                                                   FP_FINGER_STATUS_NONE,
                                                   FP_FINGER_STATUS_PRESENT);

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
          if (self->cmd_complete_on_removal)
            {
              fpi_ssm_mark_completed (transfer->ssm);
              return;
            }
        }

      fp_dbg ("Finger is now %s the sensor", self->finger_on_sensor ? "on" : "off");

      /* XXX: Call callback!?! */
    }

  res = bmkt_parse_message_payload (&msg_resp, &resp);
  if (res != BMKT_SUCCESS)
    {
      g_warning ("Could not parse message payload: %i", res);
      fpi_ssm_mark_failed (transfer->ssm,
                           fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
      return;
    }

  /* Special cancellation handling */
  if (resp.response_id == BMKT_RSP_CANCEL_OP_OK || resp.response_id == BMKT_RSP_CANCEL_OP_FAIL)
    {
      if (resp.response_id == BMKT_RSP_CANCEL_OP_OK)
        {
Torstein Husebø's avatar
Torstein Husebø committed
127
          fp_dbg ("Received cancellation success response");
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
          fpi_ssm_mark_failed (transfer->ssm,
                               g_error_new_literal (G_IO_ERROR,
                                                    G_IO_ERROR_CANCELLED,
                                                    "Device reported cancellation of operation"));
        }
      else
        {
          fp_dbg ("Cancellation failed, this should not happen");
          fpi_ssm_mark_failed (transfer->ssm,
                               fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
        }
      return;
    }

  if (msg_resp.seq_num == 0)
    {
      /* XXX: Should we really abort the command on general error?
       *      The original code did not! */
      if (msg_resp.msg_id == BMKT_RSP_GENERAL_ERROR)
        {
          guint16 err;

          /* XXX: It is weird that this is big endian. */
          err = FP_READ_UINT16_BE (msg_resp.payload);

          fp_warn ("Received General Error %d from the sensor", (guint) err);
          fpi_ssm_mark_failed (transfer->ssm,
                               fpi_device_error_new_msg (FP_DEVICE_ERROR_PROTO,
156 157
                                                         "Received general error %u from device",
                                                         (guint) err));
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
          //fpi_ssm_jump_to_state (transfer->ssm, fpi_ssm_get_cur_state (transfer->ssm));
          return;
        }
      else
        {
          fp_dbg ("Received message with 0 sequence number 0x%02x, ignoring!",
                  msg_resp.msg_id);
          fpi_ssm_next_state (transfer->ssm);
          return;
        }
    }

  /* We should only ever have one command running, and the sequence num needs
   * to match. */
  if (msg_resp.seq_num != self->cmd_seq_num)
    {
      fp_warn ("Got unexpected sequence number from device, %d instead of %d",
               msg_resp.seq_num,
               self->cmd_seq_num);
    }

  if (callback)
    callback (self, &resp, NULL);

  /* Callback may have queued a follow up command, then we need
   * to restart the SSM. If not, we'll finish/wait for interrupt
   * depending on resp.complete. */
  if (self->cmd_pending_transfer)
    fpi_ssm_jump_to_state (transfer->ssm, SYNAPTICS_CMD_SEND_PENDING);
187
  else if (!resp.complete || self->cmd_complete_on_removal)
188 189 190
    fpi_ssm_next_state (transfer->ssm);             /* SYNAPTICS_CMD_WAIT_INTERRUPT */
  else
    fpi_ssm_mark_completed (transfer->ssm);
191 192 193 194
}

static void
cmd_interrupt_cb (FpiUsbTransfer *transfer,
195 196 197
                  FpDevice       *device,
                  gpointer        user_data,
                  GError         *error)
198
{
199 200 201 202 203 204 205 206 207 208 209 210 211 212
  g_debug ("interrupt transfer done");
  if (error)
    {
      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        {
          g_error_free (error);
          fpi_ssm_jump_to_state (transfer->ssm, SYNAPTICS_CMD_GET_RESP);
          return;
        }

      fpi_ssm_mark_failed (transfer->ssm, error);
      return;
    }

Benjamin Berg's avatar
Benjamin Berg committed
213
  if (transfer->buffer[0] & USB_ASYNC_MESSAGE_PENDING)
214 215 216
    {
      fpi_ssm_next_state (transfer->ssm);
    }
217
  else
218 219
    {
      fpi_usb_transfer_submit (fpi_usb_transfer_ref (transfer),
220
                               0,
221 222 223 224
                               NULL,
                               cmd_interrupt_cb,
                               NULL);
    }
225 226 227
}

static void
228
synaptics_cmd_run_state (FpiSsm   *ssm,
229
                         FpDevice *dev)
230
{
231
  FpiUsbTransfer *transfer;
232 233 234 235 236 237 238 239 240 241 242 243 244
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (dev);

  switch (fpi_ssm_get_cur_state (ssm))
    {
    case SYNAPTICS_CMD_SEND_PENDING:
      if (self->cmd_pending_transfer)
        {
          self->cmd_pending_transfer->ssm = ssm;
          fpi_usb_transfer_submit (self->cmd_pending_transfer,
                                   1000,
                                   NULL,
                                   fpi_ssm_usb_transfer_cb,
                                   NULL);
245
          self->cmd_pending_transfer = NULL;
246 247 248 249 250 251 252 253 254 255 256 257 258 259
        }
      else
        {
          fpi_ssm_next_state (ssm);
        }
      break;

    case SYNAPTICS_CMD_GET_RESP:
      transfer = fpi_usb_transfer_new (dev);
      transfer->ssm = ssm;
      fpi_usb_transfer_fill_bulk (transfer, USB_EP_REPLY, MAX_TRANSFER_LEN);
      fpi_usb_transfer_submit (transfer,
                               5000,
                               NULL,
Bastien Nocera's avatar
Bastien Nocera committed
260
                               cmd_receive_cb,
261
                               fpi_ssm_get_data (ssm));
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

      break;

    case SYNAPTICS_CMD_WAIT_INTERRUPT:
      transfer = fpi_usb_transfer_new (dev);
      transfer->ssm = ssm;
      fpi_usb_transfer_fill_interrupt (transfer, USB_EP_INTERRUPT, USB_INTERRUPT_DATA_SIZE);
      fpi_usb_transfer_submit (transfer,
                               0,
                               self->interrupt_cancellable,
                               cmd_interrupt_cb,
                               NULL);

      break;

    case SYNAPTICS_CMD_SEND_ASYNC:
      transfer = fpi_usb_transfer_new (dev);
      transfer->ssm = ssm;
      fpi_usb_transfer_fill_bulk (transfer, USB_EP_REQUEST, SENSOR_FW_CMD_HEADER_LEN);
      transfer->buffer[0] = SENSOR_CMD_ASYNCMSG_READ;
      fpi_usb_transfer_submit (transfer,
                               1000,
                               NULL,
                               fpi_ssm_usb_transfer_cb,
                               NULL);

      break;

    case SYNAPTICS_CMD_RESTART:
      fpi_ssm_jump_to_state (ssm, SYNAPTICS_CMD_SEND_PENDING);
      break;
    }
294 295 296
}

static void
297
cmd_ssm_done (FpiSsm *ssm, FpDevice *dev, GError *error)
298
{
299
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (dev);
300
  SynCmdMsgCallback callback = fpi_ssm_get_data (ssm);
301 302 303 304

  self->cmd_ssm = NULL;

  /* Notify about the SSM failure from here instead. */
305 306 307
  if (error || self->cmd_complete_on_removal)
    callback (self, NULL, error);

308
  self->cmd_complete_on_removal = FALSE;
309 310 311 312
}

static void
cmd_forget_cb (FpiUsbTransfer *transfer,
313 314 315
               FpDevice       *device,
               gpointer        user_data,
               GError         *error)
316
{
317 318 319 320 321 322 323 324 325
  if (error)
    {
      g_warning ("Async command sending failed: %s", error->message);
      g_error_free (error);
    }
  else
    {
      g_debug ("Async command sent successfully");
    }
326 327 328 329
}

static void
synaptics_sensor_cmd (FpiDeviceSynaptics *self,
330 331 332 333 334
                      gint                seq_num,
                      guint8              msg_id,
                      const guint8      * payload,
                      gssize              payload_len,
                      SynCmdMsgCallback   callback)
335
{
336
  FpiUsbTransfer *transfer;
337 338 339 340 341 342 343 344 345 346
  guint8 real_seq_num;
  gint msg_len;
  gint res;

  /* callback may be NULL in two cases:
   *  - seq_num == -1
   *  - a state machine is already running, continued command */
  g_assert (payload || payload_len == 0);

  /* seq_num of 0 means a normal command, -1 means the current commands
Bastien Nocera's avatar
Bastien Nocera committed
347
   * sequence number should not be updated (i.e. second async command which
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
   * may only be a cancellation currently). */
  if (seq_num <= 0)
    {
      self->last_seq_num = MAX (1, self->last_seq_num + 1);
      real_seq_num = self->last_seq_num;
      if (seq_num == 0)
        self->cmd_seq_num = self->last_seq_num;
    }
  else
    {
      real_seq_num = seq_num;
      self->last_seq_num = real_seq_num;
    }
  g_debug ("sequence number is %d", real_seq_num);

  /* We calculate the exact length here (we could also just create a larger
   * buffer instead and check the result of bmkt_compose_message. */
  msg_len = BMKT_MESSAGE_HEADER_LEN + payload_len;

  /* Send out the command */
  transfer = fpi_usb_transfer_new (FP_DEVICE (self));
  transfer->short_is_error = TRUE;
  fpi_usb_transfer_fill_bulk (transfer,
                              USB_EP_REQUEST,
                              msg_len + SENSOR_FW_CMD_HEADER_LEN);

  /* MIS sensors send ACE commands encapsulated in FW commands*/
  transfer->buffer[0] = SENSOR_CMD_ACE_COMMAND;
  res = bmkt_compose_message (&transfer->buffer[1],
                              &msg_len, msg_id,
                              real_seq_num,
                              payload_len,
                              payload);
  g_assert (res == BMKT_SUCCESS);
  g_assert (msg_len + SENSOR_FW_CMD_HEADER_LEN == transfer->length);

  /* Special case for async command sending (should only be used for
   * cancellation). */
  if (seq_num == -1)
    {
      g_assert (callback == NULL);

      /* We just send and forget here. */
      fpi_usb_transfer_submit (transfer, 1000, NULL, cmd_forget_cb, NULL);
    }
  else
    {
      /* Command should be send using the state machine. */
      g_assert (self->cmd_pending_transfer == NULL);

      self->cmd_pending_transfer = g_steal_pointer (&transfer);

      if (self->cmd_ssm)
        {
          /* Continued command, we already have an SSM with a callback.
           * There is nothing to do in this case, the command will be
           * sent automatically. */
          g_assert (callback == NULL);
        }
      else
        {
          /* Start of a new command, create the state machine. */
          g_assert (callback != NULL);

          self->cmd_ssm = fpi_ssm_new (FP_DEVICE (self),
                                       synaptics_cmd_run_state,
414 415
                                       SYNAPTICS_CMD_NUM_STATES);
          fpi_ssm_set_data (self->cmd_ssm, callback, NULL);
416 417 418 419

          fpi_ssm_start (self->cmd_ssm, cmd_ssm_done);
        }
    }
420 421 422 423 424 425
}

static gboolean
parse_print_data (GVariant      *data,
                  guint8        *finger,
                  const guint8 **user_id,
426
                  gsize         *user_id_len)
427
{
428
  g_autoptr(GVariant) user_id_var = NULL;
429

430 431 432 433
  g_return_val_if_fail (data != NULL, FALSE);
  g_return_val_if_fail (finger != NULL, FALSE);
  g_return_val_if_fail (user_id != NULL, FALSE);
  g_return_val_if_fail (user_id_len != NULL, FALSE);
434

435 436
  *user_id = NULL;
  *user_id_len = 0;
437

438 439
  if (!g_variant_check_format_string (data, "(y@ay)", FALSE))
    return FALSE;
440

441 442 443 444
  g_variant_get (data,
                 "(y@ay)",
                 finger,
                 &user_id_var);
445

446
  *user_id = g_variant_get_fixed_array (user_id_var, user_id_len, 1);
447

448 449
  if (*user_id_len == 0 || *user_id_len > BMKT_MAX_USER_ID_LEN)
    return FALSE;
450

451 452
  if (*user_id_len <= 0 || *user_id[0] == ' ')
    return FALSE;
453

454
  return TRUE;
455 456
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
static FpPrint *
create_print (FpiDeviceSynaptics *self,
              guint8             *user_id,
              guint8              finger_id)
{
  FpPrint *print;
  g_autofree gchar *user_id_safe;
  GVariant *data = NULL;
  GVariant *uid = NULL;

  user_id_safe = g_strndup ((char *) user_id, BMKT_MAX_USER_ID_LEN);

  print = fp_print_new (FP_DEVICE (self));
  uid = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
                                   user_id_safe,
                                   strlen (user_id_safe),
                                   1);
  data = g_variant_new ("(y@ay)",
                        finger_id,
                        uid);

  fpi_print_set_type (print, FPI_PRINT_RAW);
  fpi_print_set_device_stored (print, TRUE);
  g_object_set (print, "fpi-data", data, NULL);
  g_object_set (print, "description", user_id_safe, NULL);

  fpi_print_fill_from_user_id (print, user_id_safe);

  return print;
}

488
static void
489 490 491
list_msg_cb (FpiDeviceSynaptics *self,
             bmkt_response_t    *resp,
             GError             *error)
492
{
493 494 495 496
  bmkt_enroll_templates_resp_t *get_enroll_templates_resp;

  if (error)
    {
497
      g_clear_pointer (&self->list_result, g_ptr_array_unref);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
      fpi_device_list_complete (FP_DEVICE (self), NULL, error);
      return;
    }

  get_enroll_templates_resp = &resp->response.enroll_templates_resp;

  switch (resp->response_id)
    {
    case BMKT_RSP_QUERY_FAIL:
      if (resp->result == BMKT_FP_DATABASE_EMPTY)
        {
          fp_info ("Database is empty");

          fpi_device_list_complete (FP_DEVICE (self),
                                    g_steal_pointer (&self->list_result),
                                    NULL);
        }
      else
        {
          fp_info ("Failed to query enrolled users: %d", resp->result);
518
          g_clear_pointer (&self->list_result, g_ptr_array_unref);
519 520 521
          fpi_device_list_complete (FP_DEVICE (self),
                                    NULL,
                                    fpi_device_error_new_msg (FP_DEVICE_ERROR_GENERAL,
522 523
                                                              "Failed to query enrolled users: %d",
                                                              resp->result));
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        }
      break;

    case BMKT_RSP_QUERY_RESPONSE_COMPLETE:
      fp_info ("Query complete!");

      fpi_device_list_complete (FP_DEVICE (self),
                                g_steal_pointer (&self->list_result),
                                NULL);

      break;

    case BMKT_RSP_TEMPLATE_RECORDS_REPORT:

      for (int n = 0; n < BMKT_MAX_NUM_TEMPLATES_INTERNAL_FLASH; n++)
        {
          FpPrint *print;

          if (get_enroll_templates_resp->templates[n].user_id_len == 0)
            continue;

          fp_info ("![query %d of %d] template %d: status=0x%x, userId=%s, fingerId=%d",
                   get_enroll_templates_resp->query_sequence,
                   get_enroll_templates_resp->total_query_messages,
                   n,
                   get_enroll_templates_resp->templates[n].template_status,
                   get_enroll_templates_resp->templates[n].user_id,
                   get_enroll_templates_resp->templates[n].finger_id);

553 554 555
          print = create_print (self,
                                get_enroll_templates_resp->templates[n].user_id,
                                get_enroll_templates_resp->templates[n].finger_id);
556 557 558 559 560 561 562 563 564 565 566 567 568

          g_ptr_array_add (self->list_result, g_object_ref_sink (print));
        }

      synaptics_sensor_cmd (self,
                            self->cmd_seq_num,
                            BMKT_CMD_GET_NEXT_QUERY_RESPONSE,
                            NULL,
                            0,
                            NULL);

      break;
    }
569 570 571
}

static void
572
list (FpDevice *device)
573
{
574
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
575

576
  G_DEBUG_HERE ();
577

578 579
  self->list_result = g_ptr_array_new_with_free_func (g_object_unref);
  synaptics_sensor_cmd (self, 0, BMKT_CMD_GET_TEMPLATE_RECORDS, NULL, 0, list_msg_cb);
580 581
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
static void
verify_complete_after_finger_removal (FpiDeviceSynaptics *self)
{
  FpDevice *device = FP_DEVICE (self);

  if (self->finger_on_sensor)
    {
      fp_dbg ("delaying verify report until after finger removal!");
      self->cmd_complete_on_removal = TRUE;
    }
  else
    {
      fpi_device_verify_complete (device, NULL);
    }
}

598
static void
599 600 601
verify_msg_cb (FpiDeviceSynaptics *self,
               bmkt_response_t    *resp,
               GError             *error)
602
{
603 604 605 606 607
  FpDevice *device = FP_DEVICE (self);
  bmkt_verify_resp_t *verify_resp;

  if (error)
    {
608
      fpi_device_verify_complete (device, error);
609 610 611 612 613
      return;
    }

  if (resp == NULL && self->cmd_complete_on_removal)
    {
614
      fpi_device_verify_complete (device, NULL);
615 616 617
      return;
    }

618 619
  g_assert (resp != NULL);

620 621 622 623 624
  verify_resp = &resp->response.verify_resp;

  switch (resp->response_id)
    {
    case BMKT_RSP_VERIFY_READY:
625 626 627
      fpi_device_report_finger_status_changes (device,
                                               FP_FINGER_STATUS_NEEDED,
                                               FP_FINGER_STATUS_NONE);
628 629 630 631 632 633 634 635
      fp_info ("Place Finger on the Sensor!");
      break;

    case BMKT_RSP_CAPTURE_COMPLETE:
      fp_info ("Fingerprint image capture complete!");
      break;

    case BMKT_RSP_VERIFY_FAIL:
636
      if (resp->result == BMKT_SENSOR_STIMULUS_ERROR)
637
        {
638
          fp_info ("Match error occurred");
639 640
          fpi_device_verify_report (device, FPI_MATCH_ERROR, NULL,
                                    fpi_device_retry_new (FP_DEVICE_RETRY_GENERAL));
641
          verify_complete_after_finger_removal (self);
642 643 644
        }
      else if (resp->result == BMKT_FP_NO_MATCH)
        {
645
          fp_info ("Print didn't match");
646
          fpi_device_verify_report (device, FPI_MATCH_FAIL, NULL, NULL);
647
          verify_complete_after_finger_removal (self);
648
        }
649
      else if (resp->result == BMKT_FP_DATABASE_NO_RECORD_EXISTS)
650 651 652 653 654 655 656 657
        {
          fp_info ("Print is not in database");
          fpi_device_verify_complete (device,
                                      fpi_device_error_new (FP_DEVICE_ERROR_DATA_NOT_FOUND));
        }
      else
        {
          fp_warn ("Verify has failed: %d", resp->result);
658 659 660 661
          fpi_device_verify_complete (device,
                                      fpi_device_error_new_msg (FP_DEVICE_ERROR_PROTO,
                                                                "Unexpected result from device %d",
                                                                resp->result));
662 663 664 665 666 667
        }
      break;

    case BMKT_RSP_VERIFY_OK:
      fp_info ("Verify was successful! for user: %s finger: %d score: %f",
               verify_resp->user_id, verify_resp->finger_id, verify_resp->match_result);
668
      fpi_device_verify_report (device, FPI_MATCH_SUCCESS, NULL, NULL);
669
      verify_complete_after_finger_removal (self);
670 671
      break;
    }
672 673 674
}

static void
675
verify (FpDevice *device)
676
{
677 678 679 680 681 682 683 684 685 686
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  FpPrint *print = NULL;

  g_autoptr(GVariant) data = NULL;
  guint8 finger;
  const guint8 *user_id;
  gsize user_id_len = 0;

  fpi_device_get_verify_data (device, &print);

687
  g_object_get (print, "fpi-data", &data, NULL);
688 689 690 691 692 693 694 695 696 697 698
  g_debug ("data is %p", data);
  if (!parse_print_data (data, &finger, &user_id, &user_id_len))
    {
      fpi_device_verify_complete (device,
                                  fpi_device_error_new (FP_DEVICE_ERROR_DATA_INVALID));
      return;
    }

  G_DEBUG_HERE ();

  synaptics_sensor_cmd (self, 0, BMKT_CMD_VERIFY_USER, user_id, user_id_len, verify_msg_cb);
699 700
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
static void
identify_complete_after_finger_removal (FpiDeviceSynaptics *self)
{
  FpDevice *device = FP_DEVICE (self);

  if (self->finger_on_sensor)
    {
      fp_dbg ("delaying identify report until after finger removal!");
      self->cmd_complete_on_removal = TRUE;
    }
  else
    {
      fpi_device_identify_complete (device, NULL);
    }
}


static void
identify_msg_cb (FpiDeviceSynaptics *self,
                 bmkt_response_t    *resp,
                 GError             *error)
{
  FpDevice *device = FP_DEVICE (self);

  if (error)
    {
      fpi_device_identify_complete (device, error);
      return;
    }

  if (resp == NULL && self->cmd_complete_on_removal)
    {
      fpi_device_identify_complete (device, NULL);
      return;
    }

  g_assert (resp != NULL);

  switch (resp->response_id)
    {
    case BMKT_RSP_ID_READY:
      fp_info ("Place Finger on the Sensor!");
      break;

745 746 747 748 749 750
    case BMKT_RSP_SEND_NEXT_USER_ID:
      {
        compose_and_send_identify_msg (device);
        break;
      }

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    case BMKT_RSP_ID_FAIL:
      if (resp->result == BMKT_SENSOR_STIMULUS_ERROR)
        {
          fp_info ("Match error occurred");
          fpi_device_identify_report (device, NULL, NULL,
                                      fpi_device_retry_new (FP_DEVICE_RETRY_GENERAL));
          identify_complete_after_finger_removal (self);
        }
      else if (resp->result == BMKT_FP_NO_MATCH)
        {
          fp_info ("Print didn't match");
          fpi_device_identify_report (device, NULL, NULL, NULL);
          identify_complete_after_finger_removal (self);
        }
      else if (resp->result == BMKT_FP_DATABASE_NO_RECORD_EXISTS)
        {
          fp_info ("Print is not in database");
          fpi_device_identify_complete (device,
                                        fpi_device_error_new (FP_DEVICE_ERROR_DATA_NOT_FOUND));
        }
      else
        {
          fp_warn ("identify has failed: %d", resp->result);
          fpi_device_identify_complete (device,
                                        fpi_device_error_new_msg (FP_DEVICE_ERROR_PROTO,
                                                                  "Unexpected result from device %d",
                                                                  resp->result));
        }
      break;

    case BMKT_RSP_ID_OK:
      {
        FpPrint *print = NULL;
        GPtrArray *prints = NULL;
        g_autoptr(GVariant) data = NULL;
786 787 788 789 790 791
        gboolean found = FALSE;
        guint index;

        print = create_print (self,
                              resp->response.id_resp.user_id,
                              resp->response.id_resp.finger_id);
792 793 794

        fpi_device_get_identify_data (device, &prints);

795 796 797 798 799 800
        found = g_ptr_array_find_with_equal_func (prints,
                                                  print,
                                                  (GEqualFunc) fp_print_equal,
                                                  &index);

        if (found)
801
          fpi_device_identify_report (device, g_ptr_array_index (prints, index), print, NULL);
802
        else
803 804 805
          fpi_device_identify_report (device, NULL, print, NULL);

        identify_complete_after_finger_removal (self);
806 807 808 809 810 811
      }
    }
}

static void
identify (FpDevice *device)
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
{
  init_identify_msg (device);
  compose_and_send_identify_msg (device);
}

static void
init_identify_msg (FpDevice *device)
{
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);

  self->id_idx = 0;
}

static void
compose_and_send_identify_msg (FpDevice *device)
827 828 829 830 831 832 833 834 835
{
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  FpPrint *print = NULL;
  GPtrArray *prints = NULL;

  g_autoptr(GVariant) data = NULL;
  guint8 finger;
  const guint8 *user_id;
  gsize user_id_len = 0;
836 837 838
  g_autofree guint8 *payload = NULL;
  guint8 payload_len = 0;
  guint8 payloadOffset = 0;
839 840

  fpi_device_get_identify_data (device, &prints);
841
  if (prints->len > UINT8_MAX)
842
    {
843 844 845
      fpi_device_identify_complete (device,
                                    fpi_device_error_new (FP_DEVICE_ERROR_DATA_INVALID));
      return;
846
    }
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  if(self->id_idx >= prints->len)
    {
      fp_warn ("Device asked for more prints than we are providing.");
      fpi_device_identify_complete (device,
                                    fpi_device_error_new_msg (FP_DEVICE_ERROR_PROTO,
                                                              "Unexpected index"));
      return;
    }
  print = g_ptr_array_index (prints, self->id_idx);
  g_object_get (print, "fpi-data", &data, NULL);
  g_debug ("data is %p", data);
  if (!parse_print_data (data, &finger, &user_id, &user_id_len))
    {
      fpi_device_identify_complete (device,
                                    fpi_device_error_new (FP_DEVICE_ERROR_DATA_INVALID));
      return;
    }
  if(self->id_idx == 0)
    {
      /*
       * Construct payload.
       * 1st byte is total number of IDs in list.
       * 2nd byte is number of IDs in list.
       * 1 byte for each ID length, maximum id length is 100.
       * user_id_len bytes of each ID
       */
      payload_len = 2 + 1 + user_id_len;
      payload = g_malloc0 (payload_len);
      payload[payloadOffset] = prints->len;
      payloadOffset += 1;
      payload[payloadOffset] = 1; /* send one id per message */
      payloadOffset += 1;
      payload[payloadOffset] = user_id_len;
      payloadOffset += 1;
      memcpy (&payload[payloadOffset], user_id, user_id_len);
      payloadOffset += user_id_len;

      G_DEBUG_HERE ();

      synaptics_sensor_cmd (self, 0, BMKT_CMD_ID_USER_IN_ORDER, payload, payloadOffset, identify_msg_cb);
    }
  else
    {
      /*
       * 1st byte is the number of IDs
       * 1 byte for each ID length
       * id_length bytes for each ID
       */
      payload_len = 1 + 1 + user_id_len;
      payload = g_malloc0 (payload_len);
      payload[payloadOffset] = 1; /* send one id per message */
      payloadOffset += 1;
      payload[payloadOffset] = user_id_len;
      payloadOffset += 1;
      memcpy (&payload[payloadOffset], user_id, user_id_len);
      payloadOffset += user_id_len;
      synaptics_sensor_cmd (self, self->cmd_seq_num, BMKT_CMD_ID_NEXT_USER, payload, payloadOffset, NULL);
    }
  self->id_idx++;
906
}
907
static void
908 909 910
enroll_msg_cb (FpiDeviceSynaptics *self,
               bmkt_response_t    *resp,
               GError             *error)
911
{
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
  FpDevice *device = FP_DEVICE (self);
  bmkt_enroll_resp_t *enroll_resp;

  if (error)
    {
      fpi_device_enroll_complete (device, NULL, error);
      return;
    }

  enroll_resp = &resp->response.enroll_resp;

  switch (resp->response_id)
    {
    case BMKT_RSP_ENROLL_READY:
      {
        self->enroll_stage = 0;
928 929 930
        fpi_device_report_finger_status_changes (device,
                                                 FP_FINGER_STATUS_NEEDED,
                                                 FP_FINGER_STATUS_NONE);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
        fp_info ("Place Finger on the Sensor!");
        break;
      }

    case BMKT_RSP_CAPTURE_COMPLETE:
      {
        fp_info ("Fingerprint image capture complete!");
        break;
      }

    case BMKT_RSP_ENROLL_REPORT:
      {
        gint done_stages;
        fp_info ("Enrollment is %d %% ", enroll_resp->progress);

        done_stages = (enroll_resp->progress * ENROLL_SAMPLES + 99) / 100;
        if (enroll_resp->progress < 100)
          done_stages = MIN (done_stages, ENROLL_SAMPLES - 1);

Bastien Nocera's avatar
Bastien Nocera committed
950
        /* Emit a retry error if there has been no discernible
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
         * progress. Some firmware revisions report more required
         * touches. */
        if (self->enroll_stage == done_stages)
          {
            fpi_device_enroll_progress (device,
                                        done_stages,
                                        NULL,
                                        fpi_device_retry_new (FP_DEVICE_RETRY_GENERAL));
          }

        while (self->enroll_stage < done_stages)
          {
            self->enroll_stage += 1;
            fpi_device_enroll_progress (device, self->enroll_stage, NULL, NULL);
          }
        break;
      }

    case BMKT_RSP_ENROLL_PAUSED:
      {
        fp_info ("Enrollment has been paused!");
        break;
      }

    case BMKT_RSP_ENROLL_RESUMED:
      {
        fp_info ("Enrollment has been resumed!");
        break;
      }

    case BMKT_RSP_ENROLL_FAIL:
      {
        fp_info ("Enrollment has failed!: %d", resp->result);
        if (resp->result == BMKT_FP_DATABASE_FULL)
          {
            fpi_device_enroll_complete (device,
                                        NULL,
                                        fpi_device_error_new (FP_DEVICE_ERROR_DATA_FULL));
          }
        else
          {
            fpi_device_enroll_complete (device,
                                        NULL,
                                        fpi_device_error_new_msg (FP_DEVICE_ERROR_GENERAL,
995 996
                                                                  "Enrollment failed (%d)",
                                                                  resp->result));
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
          }
        break;
      }

    case BMKT_RSP_ENROLL_OK:
      {
        FpPrint *print = NULL;

        fp_info ("Enrollment was successful!");

        fpi_device_get_enroll_data (device, &print);

        fpi_device_enroll_complete (device, g_object_ref (print), NULL);
        break;
      }
    }
1013 1014 1015
}

static void
1016
enroll (FpDevice *device)
1017
{
1018 1019 1020 1021 1022
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  FpPrint *print = NULL;
  GVariant *data = NULL;
  GVariant *uid = NULL;
  guint finger;
1023
  g_autofree gchar *user_id = NULL;
1024 1025 1026 1027 1028 1029 1030
  gssize user_id_len;
  g_autofree guint8 *payload = NULL;

  fpi_device_get_enroll_data (device, &print);

  G_DEBUG_HERE ();

1031
  user_id = fpi_print_generate_user_id (print);
1032 1033 1034 1035

  user_id_len = strlen (user_id);
  user_id_len = MIN (BMKT_MAX_USER_ID_LEN, user_id_len);

Bastien Nocera's avatar
Bastien Nocera committed
1036
  /* We currently always use finger 1 from the devices point of view */
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
  finger = 1;

  uid = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
                                   user_id,
                                   user_id_len,
                                   1);
  data = g_variant_new ("(y@ay)",
                        finger,
                        uid);

1047
  fpi_print_set_type (print, FPI_PRINT_RAW);
1048
  fpi_print_set_device_stored (print, TRUE);
1049
  g_object_set (print, "fpi-data", data, NULL);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  g_object_set (print, "description", user_id, NULL);

  g_debug ("user_id: %s, finger: %d", user_id, finger);

  payload = g_malloc0 (user_id_len + 2);

  /* Backup options are not supported for Prometheus */
  payload[0] = 0;
  payload[1] = finger;
  memcpy (payload + 2, user_id, user_id_len);

  synaptics_sensor_cmd (self, 0, BMKT_CMD_ENROLL_USER, payload, user_id_len + 2, enroll_msg_cb);
1062 1063 1064
}

static void
1065 1066 1067
delete_msg_cb (FpiDeviceSynaptics *self,
               bmkt_response_t    *resp,
               GError             *error)
1068
{
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
  FpDevice *device = FP_DEVICE (self);
  bmkt_del_user_resp_t *del_user_resp;

  if (error)
    {
      fpi_device_delete_complete (device, error);
      return;
    }

  del_user_resp = &resp->response.del_user_resp;

  switch (resp->response_id)
    {
    case BMKT_RSP_DELETE_PROGRESS:
      fp_info ("Deleting Enrolled Users is %d%% complete",
               del_user_resp->progress);
      break;

    case BMKT_RSP_DEL_USER_FP_FAIL:
      fp_info ("Failed to delete enrolled user: %d", resp->result);
      if (resp->result == BMKT_FP_DATABASE_NO_RECORD_EXISTS)
        fpi_device_delete_complete (device,
                                    fpi_device_error_new (FP_DEVICE_ERROR_DATA_NOT_FOUND));
      else
        fpi_device_delete_complete (device,
                                    fpi_device_error_new (FP_DEVICE_ERROR_GENERAL));
      break;

    case BMKT_RSP_DEL_USER_FP_OK:
      fp_info ("Successfully deleted enrolled user");
      fpi_device_delete_complete (device, NULL);
      break;
    }
1102 1103 1104
}

static void
1105
delete_print (FpDevice *device)
1106
{
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  FpPrint *print = NULL;

  g_autoptr(GVariant) data = NULL;
  guint8 finger;
  const guint8 *user_id;
  gsize user_id_len = 0;
  g_autofree guint8 *payload = NULL;

  fpi_device_get_delete_data (device, &print);

1118
  g_object_get (print, "fpi-data", &data, NULL);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
  g_debug ("data is %p", data);
  if (!parse_print_data (data, &finger, &user_id, &user_id_len))
    {
      fpi_device_delete_complete (device,
                                  fpi_device_error_new (FP_DEVICE_ERROR_DATA_INVALID));
      return;
    }

  G_DEBUG_HERE ();

  payload = g_malloc0 (1 + user_id_len);
  payload[0] = finger;
  memcpy (payload + 1, user_id, user_id_len);

  synaptics_sensor_cmd (self, 0, BMKT_CMD_DEL_USER_FP, payload, user_id_len + 1, delete_msg_cb);
1134 1135 1136
}

static void
1137
dev_probe (FpDevice *device)
1138
{
1139 1140
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  GUsbDevice *usb_dev;
1141 1142

  g_autoptr(FpiUsbTransfer) transfer = NULL;
1143 1144 1145 1146 1147 1148
  FpiByteReader reader;
  GError *error = NULL;
  guint16 status;
  const guint8 *data;
  gboolean read_ok = TRUE;
  g_autofree gchar *serial = NULL;
1149
  gboolean retry = TRUE;
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

  G_DEBUG_HERE ();

  /* Claim usb interface */
  usb_dev = fpi_device_get_usb_device (device);
  if (!g_usb_device_open (usb_dev, &error))
    {
      fpi_device_probe_complete (device, NULL, NULL, error);
      return;
    }

1161
  if (!g_usb_device_reset (usb_dev, &error))
1162
    goto err_close;
1163

1164
  if (!g_usb_device_claim_interface (usb_dev, 0, 0, &error))
1165 1166
    goto err_close;

1167 1168 1169 1170 1171 1172 1173 1174 1175
  while(1)
    {
      /* TODO: Do not do this synchronous. */
      transfer = fpi_usb_transfer_new (device);
      fpi_usb_transfer_fill_bulk (transfer, USB_EP_REQUEST, SENSOR_FW_CMD_HEADER_LEN);
      transfer->short_is_error = TRUE;
      transfer->buffer[0] = SENSOR_CMD_GET_VERSION;
      if (!fpi_usb_transfer_submit_sync (transfer, 1000, &error))
        goto err_close;
1176

1177 1178 1179 1180 1181
      g_clear_pointer (&transfer, fpi_usb_transfer_unref);
      transfer = fpi_usb_transfer_new (device);
      fpi_usb_transfer_fill_bulk (transfer, USB_EP_REPLY, 40);
      if (!fpi_usb_transfer_submit_sync (transfer, 1000, &error))
        goto err_close;
1182

1183
      fpi_byte_reader_init (&reader, transfer->buffer, transfer->actual_length);
1184

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
      if (!fpi_byte_reader_get_uint16_le (&reader, &status))
        {
          g_warning ("Transfer in response to version query was too short");
          error = fpi_device_error_new (FP_DEVICE_ERROR_PROTO);
          goto err_close;
        }
      if (status != 0)
        {
          g_warning ("Device responded with error: %d retry: %d", status, retry);
          if(retry)
            {
              retry = FALSE;
              continue;
            }
          error = fpi_device_error_new (FP_DEVICE_ERROR_PROTO);
          goto err_close;
        }
      break;
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    }
  read_ok &= fpi_byte_reader_get_uint32_le (&reader, &self->mis_version.build_time);
  read_ok &= fpi_byte_reader_get_uint32_le (&reader, &self->mis_version.build_num);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.version_major);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.version_minor);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.target);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.product);

  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.silicon_rev);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.formal_release);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.platform);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.patch);
  if (fpi_byte_reader_get_data (&reader, sizeof (self->mis_version.serial_number), &data))
    memcpy (self->mis_version.serial_number, data, sizeof (self->mis_version.serial_number));
  else
    read_ok = FALSE;
  read_ok &= fpi_byte_reader_get_uint16_le (&reader, &self->mis_version.security);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.iface);
  read_ok &= fpi_byte_reader_get_uint8 (&reader, &self->mis_version.device_type);

  if (!read_ok)
    {
Bastien Nocera's avatar
Bastien Nocera committed
1225
      g_warning ("Transfer in response to version query was too short");
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
      error = fpi_device_error_new (FP_DEVICE_ERROR_PROTO);
      goto err_close;
    }

  fp_dbg ("Build Time: %d", self->mis_version.build_time);
  fp_dbg ("Build Num: %d", self->mis_version.build_num);
  fp_dbg ("Version: %d.%d", self->mis_version.version_major, self->mis_version.version_minor);
  fp_dbg ("Target: %d", self->mis_version.target);
  fp_dbg ("Product: %d", self->mis_version.product);


  /* We need at least firmware version 10.1, and for 10.1 build 2989158 */
  if (self->mis_version.version_major < 10 ||
      self->mis_version.version_minor < 1 ||
      (self->mis_version.version_major == 10 &&
       self->mis_version.version_minor == 1 &&
       self->mis_version.build_num < 2989158))
    {
      fp_warn ("Firmware version %d.%d with build number %d is unsupported",
               self->mis_version.version_major,
               self->mis_version.version_minor,
               self->mis_version.build_num);

      error = fpi_device_error_new_msg (FP_DEVICE_ERROR_GENERAL,
1250 1251 1252 1253 1254
                                        "Unsupported firmware version "
                                        "(%d.%d with build number %d)",
                                        self->mis_version.version_major,
                                        self->mis_version.version_minor,
                                        self->mis_version.build_num);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
      goto err_close;
    }

  /* This is the same as the serial_number from above, hex encoded and somewhat reordered */
  /* Should we add in more, e.g. the chip revision? */
  if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
    serial = g_strdup ("emulated-device");
  else
    serial = g_usb_device_get_string_descriptor (usb_dev,
                                                 g_usb_device_get_serial_number_index (usb_dev),
                                                 &error);

  g_usb_device_close (usb_dev, NULL);

  fpi_device_probe_complete (device, serial, NULL, error);

  return;
1272 1273

err_close:
1274 1275
  g_usb_device_close (usb_dev, NULL);
  fpi_device_probe_complete (device, NULL, NULL, error);
1276 1277 1278
}

static void
1279 1280 1281
fps_init_msg_cb (FpiDeviceSynaptics *self,
                 bmkt_response_t    *resp,
                 GError             *error)
1282
{
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
  if (error)
    {
      fpi_device_open_complete (FP_DEVICE (self), error);
      return;
    }

  /* BMKT_OPERATION_DENIED is returned if the sensor is already initialized */
  if (resp->result == BMKT_SUCCESS || resp->result == BMKT_OPERATION_DENIED)
    {
      fpi_device_open_complete (FP_DEVICE (self), NULL);
    }
  else
    {
      g_warning ("Initializing fingerprint sensor failed with %d!", resp->result);
      fpi_device_open_complete (FP_DEVICE (self),
                                fpi_device_error_new (FP_DEVICE_ERROR_GENERAL));
    }
1300
}
1301
static void
1302 1303 1304
fps_deinit_cb (FpiDeviceSynaptics *self,
               bmkt_response_t    *resp,
               GError             *error)
1305
{
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
  /* Release usb interface */
  g_usb_device_release_interface (fpi_device_get_usb_device (FP_DEVICE (self)), 0, 0, &error);

  g_clear_object (&self->interrupt_cancellable);

  if (!error)
    {
      switch (resp->response_id)
        {
        case BMKT_RSP_POWER_DOWN_READY:
          fp_info ("Fingerprint sensor ready to be powered down");
          break;

        case BMKT_RSP_POWER_DOWN_FAIL:
          fp_info ("Failed to go to power down mode: %d", resp->result);
          error = fpi_device_error_new_msg (FP_DEVICE_ERROR_GENERAL,
1322
                                            "Power down failed: %d", resp->result);
1323 1324 1325 1326 1327

          break;
        }
    }
  fpi_device_close_complete (FP_DEVICE (self), error);
1328
}
1329 1330 1331 1332

static void
dev_init (FpDevice *device)
{
1333 1334
  FpiDeviceSynaptics *self = FPI_DEVICE_SYNAPTICS (device);
  GError *error = NULL;
1335