|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Qualcomm Technologies, Inc. and/or its subsidiaries |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +#include <err.h> |
| 9 | +#include <stdarg.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <sys/types.h> |
| 14 | +#include <termios.h> |
| 15 | +#include <unistd.h> |
| 16 | + |
| 17 | +#include "device.h" |
| 18 | +#include "tty.h" |
| 19 | + |
| 20 | +struct pic32cx { |
| 21 | + int fd; |
| 22 | + struct termios tios; |
| 23 | + bool fastboot_pressed; |
| 24 | +}; |
| 25 | + |
| 26 | +static void pic32cx_device_write(struct pic32cx *pic32cx, const char *fmt, ...) |
| 27 | +{ |
| 28 | + char buf[32]; |
| 29 | + va_list va; |
| 30 | + int count; |
| 31 | + va_start(va, fmt); |
| 32 | + count = vsnprintf(buf, sizeof(buf), fmt, va); |
| 33 | + va_end(va); |
| 34 | + |
| 35 | + write(pic32cx->fd, buf, count); |
| 36 | +} |
| 37 | + |
| 38 | +static void pic32cx_device_power(struct pic32cx *pic32cx, int on) |
| 39 | +{ |
| 40 | + pic32cx_device_write(pic32cx, "PWR_OFF %d\r", !on); |
| 41 | +} |
| 42 | + |
| 43 | +static void *pic32cx_open(struct device *dev) |
| 44 | +{ |
| 45 | + struct pic32cx *pic32cx; |
| 46 | + |
| 47 | + dev->has_power_key = true; |
| 48 | + |
| 49 | + pic32cx = calloc(1, sizeof(*pic32cx)); |
| 50 | + |
| 51 | + pic32cx->fd = tty_open(dev->control_dev, &pic32cx->tios); |
| 52 | + if (pic32cx->fd < 0) |
| 53 | + err(1, "failed to open %s", dev->control_dev); |
| 54 | + |
| 55 | + pic32cx_device_power(pic32cx, 1); |
| 56 | + |
| 57 | + sleep(5); |
| 58 | + |
| 59 | + return pic32cx; |
| 60 | +} |
| 61 | + |
| 62 | +static int pic32cx_power(struct device *dev, bool on) |
| 63 | +{ |
| 64 | + pic32cx_device_power(dev->cdb, on); |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +static void pic32cx_key(struct device *dev, int key, bool asserted) |
| 70 | +{ |
| 71 | + struct pic32cx *pic32cx = dev->cdb; |
| 72 | + |
| 73 | + switch (key) { |
| 74 | + case DEVICE_KEY_FASTBOOT: |
| 75 | + if (asserted) |
| 76 | + pic32cx->fastboot_pressed = true; |
| 77 | + if (!asserted && pic32cx->fastboot_pressed) { |
| 78 | + pic32cx_device_write(pic32cx, "MD_FASTBOOT\r"); |
| 79 | + pic32cx->fastboot_pressed = false; |
| 80 | + } |
| 81 | + break; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const struct control_ops pic32cx_ops = { |
| 86 | + .open = pic32cx_open, |
| 87 | + .power = pic32cx_power, |
| 88 | + .key = pic32cx_key, |
| 89 | +}; |
0 commit comments