Skip to content

Commit 530a353

Browse files
committed
Add register debug and sense ID commands to channel tool
1 parent 1111911 commit 530a353

File tree

9 files changed

+385
-197
lines changed

9 files changed

+385
-197
lines changed

cxip/cxip.c

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct state {
5656
struct client clients[CLIENTS_MAX];
5757
};
5858

59+
static void usage(char *cmd);
5960
static bool serve(int listen_sock, struct chan *chan, struct dev *dev);
6061

6162
static bool handle_connect(int sock, struct client **client, struct state *state);
@@ -69,7 +70,7 @@ static struct dev *find_dev(struct state *state, int num);
6970
static struct client *find_client(struct state *state, int sock);
7071
static bool close_client(struct client *client, struct state *state);
7172

72-
static void usage(char *cmd);
73+
static bool parse_dev_num(char *str, uint16_t *num);
7374
static bool parse_dev_config(char *config, uint16_t *num, uint8_t *addr);
7475

7576
int main(int argc, char **argv)
@@ -112,7 +113,7 @@ int main(int argc, char **argv)
112113
uint8_t dev_addr;
113114

114115
if (!parse_dev_config(dev_config, &dev_num, &dev_addr)) {
115-
printf("Device config invalid: %s\n", argv[optind]);
116+
printf("Invalid device config: %s\n", argv[optind]);
116117
return EXIT_FAILURE;
117118
}
118119

@@ -160,13 +161,13 @@ int main(int argc, char **argv)
160161

161162
struct chan chan;
162163

163-
int result = chan_out_open(&chan.out, mem_fd, "udmabuf0", frontend_enable);
164+
int result = chan_out_open(&chan.out, mem_fd, "udmabuf0", false);
164165

165166
if (result == -1) {
166167
perror("chan_open");
167168
return EXIT_FAILURE;
168169
} else if (result < -1) {
169-
printf("chan_open error: %d\n", result);
170+
printf("chan_out_open error: %d\n", result);
170171
return EXIT_FAILURE;
171172
}
172173

@@ -184,7 +185,7 @@ int main(int argc, char **argv)
184185

185186
printf("Device number %.4X configured for address %.2X\n", dev.num, dev.addr);
186187

187-
chan_out_enable(&chan.out);
188+
chan_out_config(&chan.out, true, frontend_enable);
188189

189190
struct mock_cu mock_cu;
190191

@@ -208,7 +209,7 @@ int main(int argc, char **argv)
208209
mock_cu_close(&mock_cu);
209210
}
210211

211-
chan_out_close(&chan.out);
212+
chan_out_close(&chan.out, true);
212213

213214
free(chan.recv_buf);
214215

@@ -218,6 +219,11 @@ int main(int argc, char **argv)
218219
return (success ? EXIT_SUCCESS : EXIT_FAILURE);
219220
}
220221

222+
void usage(char *cmd)
223+
{
224+
printf("Usage: %s [-lm] nnnn[:aa]\n", cmd);
225+
}
226+
221227
volatile bool stop = false;
222228

223229
static void signal_handler(int signum)
@@ -481,7 +487,7 @@ bool handle_msg(struct client *client, uint8_t *msg, size_t msg_len, struct stat
481487
return cxip_send_error(client->sock, 0, "Device already open");
482488
}
483489

484-
chan_out_config(&dev->chan->out, dev->addr, true);
490+
chan_out_dev_config(&dev->chan->out, dev->addr, true);
485491

486492
printf("%.4X | %.2X | Open |\n", dev->num, dev->addr);
487493

@@ -501,7 +507,7 @@ bool handle_msg(struct client *client, uint8_t *msg, size_t msg_len, struct stat
501507
return cxip_send_error(client->sock, 0, "Device not open");
502508
}
503509

504-
chan_out_config(&dev->chan->out, dev->addr, false);
510+
chan_out_dev_config(&dev->chan->out, dev->addr, false);
505511

506512
printf("%.4X | %.2X | Close |\n", dev->num, dev->addr);
507513

@@ -654,7 +660,7 @@ bool close_client(struct client *client, struct state *state)
654660
if (state->dev != NULL && state->dev->client == client) {
655661
struct dev *dev = state->dev;
656662

657-
chan_out_config(&dev->chan->out, dev->addr, false);
663+
chan_out_dev_config(&dev->chan->out, dev->addr, false);
658664

659665
dev->client = NULL;
660666
}
@@ -674,9 +680,41 @@ bool close_client(struct client *client, struct state *state)
674680
return true;
675681
}
676682

677-
void usage(char *cmd)
683+
bool parse_dev_num(char *str, uint16_t *num)
678684
{
679-
printf("Usage: %s [-lm] nnnn[:aa]\n", cmd);
685+
if (str == NULL) {
686+
return false;
687+
}
688+
689+
size_t digits = 0;
690+
691+
for (size_t index = 0; index < strlen(str); index++) {
692+
if (isblank(str[index])) {
693+
continue;
694+
}
695+
696+
if (!isxdigit(str[index])) {
697+
return false;
698+
}
699+
700+
if (++digits > 4) {
701+
return false;
702+
}
703+
}
704+
705+
char *end;
706+
707+
errno = 0;
708+
709+
unsigned long value = strtol(str, &end, 16);
710+
711+
if (errno != 0 || *end != '\0') {
712+
return false;
713+
}
714+
715+
*num = (uint16_t) value;
716+
717+
return true;
680718
}
681719

682720
bool parse_dev_config(char *config, uint16_t *num, uint8_t *addr)
@@ -692,38 +730,16 @@ bool parse_dev_config(char *config, uint16_t *num, uint8_t *addr)
692730
return false;
693731
}
694732

695-
for (int element = 0; element < 2; element++) {
696-
for (size_t index = 0; index < strlen(token); index++) {
697-
if (!(isxdigit(token[index]) || isblank(token[index]))) {
698-
return false;
699-
}
700-
}
701-
702-
char *end;
703-
704-
errno = 0;
705-
706-
unsigned long value = strtol(token, &end, 16);
707-
708-
if (errno != 0 || *end != '\0') {
709-
return false;
710-
}
711-
712-
if (element == 0 && value <= 0xffff) {
713-
*num = (uint16_t) value;
714-
*addr = (uint8_t) (value & 0xff);
715-
} else if (element == 1 && value <= 0xff) {
716-
*addr = (uint8_t) (value & 0xff);
717-
} else {
718-
return false;
719-
}
720-
721-
if (strlen(rest) == 0) {
722-
break;
723-
}
733+
if (!parse_dev_num(token, num)) {
734+
return false;
735+
}
724736

725-
token = rest;
737+
if (strlen(rest) == 0) {
738+
*addr = (uint8_t) (*num & 0xff);
739+
return true;
726740
}
727741

728-
return true;
742+
token = rest;
743+
744+
return chan_parse_dev_addr(token, addr);
729745
}

libchan/chan.c

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include <stdio.h>
22
#include <stdint.h>
3+
#include <string.h>
34
#include <stddef.h>
5+
#include <stdlib.h>
46
#include <sys/types.h>
57
#include <unistd.h>
8+
#include <ctype.h>
9+
#include <errno.h>
610

711
#include "chan.h"
812

@@ -47,6 +51,10 @@ ssize_t chan_exec(struct chan_out *out, uint8_t addr, uint8_t cmd, uint8_t flags
4751
*status = cumulative_status;
4852
}
4953

54+
if (cumulative_status != (CHAN_STATUS_CE | CHAN_STATUS_DE)) {
55+
return CHAN_ERR_EXEC_STATUS;
56+
}
57+
5058
return chan_out_complete(out, cmd, buf, count);
5159
}
5260

@@ -60,7 +68,17 @@ ssize_t chan_exec_basic_sense(struct chan_out *out, uint8_t addr, void *buf, siz
6068
return CHAN_ERR_ARGS;
6169
}
6270

63-
return chan_exec(out, addr, CHAN_CMD_BASIC_SENSE, 0, buf, count, status);
71+
ssize_t result = chan_exec(out, addr, CHAN_CMD_BASIC_SENSE, 0, buf, count, status);
72+
73+
if (result < 0) {
74+
return result;
75+
}
76+
77+
if (result < 1) {
78+
return CHAN_ERR_EXEC_COUNT;
79+
}
80+
81+
return result;
6482
}
6583

6684
ssize_t chan_exec_sense_id(struct chan_out *out, uint8_t addr, void *buf, size_t count, uint8_t *status)
@@ -80,13 +98,13 @@ ssize_t chan_exec_sense_id(struct chan_out *out, uint8_t addr, void *buf, size_t
8098
}
8199

82100
if (result < 4) {
83-
return -1;
101+
return CHAN_ERR_EXEC_COUNT;
84102
}
85103

86104
uint8_t *p = buf;
87105

88106
if (p[0] != 0xff) {
89-
return -1;
107+
return CHAN_ERR_EXEC_DATA;
90108
}
91109

92110
return result;
@@ -101,12 +119,49 @@ int chan_exec_nop(struct chan_out *out, uint8_t addr, uint8_t *status)
101119
}
102120

103121
if (result != 0) {
104-
return -1;
122+
return CHAN_ERR_EXEC_COUNT;
105123
}
106124

107125
return 0;
108126
}
109127

128+
bool chan_parse_dev_addr(char *str, uint8_t *addr)
129+
{
130+
if (str == NULL) {
131+
return false;
132+
}
133+
134+
size_t digits = 0;
135+
136+
for (size_t index = 0; index < strlen(str); index++) {
137+
if (isblank(str[index])) {
138+
continue;
139+
}
140+
141+
if (!isxdigit(str[index])) {
142+
return false;
143+
}
144+
145+
if (++digits > 2) {
146+
return false;
147+
}
148+
}
149+
150+
char *end;
151+
152+
errno = 0;
153+
154+
unsigned long value = strtol(str, &end, 16);
155+
156+
if (errno != 0 || *end != '\0') {
157+
return false;
158+
}
159+
160+
*addr = (uint8_t) value;
161+
162+
return true;
163+
}
164+
110165
char *chan_fmt_status(uint8_t status, char *buf, size_t size)
111166
{
112167
if (size < CHAN_FMT_STATUS_BUF_SIZE) {
@@ -223,7 +278,7 @@ char *chan_fmt_cmd(uint8_t cmd, char *buf, size_t size)
223278
p += sprintf(p, "*");
224279
}
225280

226-
p += sprintf(p, ">");
281+
sprintf(p, ">");
227282

228283
return buf;
229284
}

libchan/chan.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#define CHAN_ERR_STATUS_PENDING -8 // Status pending
3737
#define CHAN_ERR_CMD_RESERVED -9 // Reserved command
3838
#define CHAN_ERR_DMA_SIZE -10 // DMA buffer limit
39+
#define CHAN_ERR_EXEC_STATUS -11 // Unexpected status
40+
#define CHAN_ERR_EXEC_COUNT -12 // Unexpected count
41+
#define CHAN_ERR_EXEC_DATA -13 // Unexpected data
3942

4043
struct chan_out {
4144
uintptr_t base_addr;
@@ -44,15 +47,15 @@ struct chan_out {
4447
struct udmabuf udmabuf;
4548
};
4649

47-
int chan_out_open(struct chan_out *out, int mem_fd, char *udmabuf_path, bool frontend_enable);
50+
int chan_out_open(struct chan_out *out, int mem_fd, char *udmabuf_path, bool ignore_state);
4851

49-
int chan_out_close(struct chan_out *out);
52+
int chan_out_close(struct chan_out *out, bool reset);
5053

51-
int chan_out_enable(struct chan_out *out);
54+
bool chan_out_is_enabled(struct chan_out *out);
5255

53-
int chan_out_disable(struct chan_out *out);
56+
int chan_out_config(struct chan_out *out, bool enable, bool frontend_enable);
5457

55-
int chan_out_config(struct chan_out *out, uint8_t addr, bool enable);
58+
int chan_out_dev_config(struct chan_out *out, uint8_t addr, bool enable);
5659

5760
int chan_out_test(struct chan_out *out, uint8_t addr, uint8_t *status);
5861

@@ -72,6 +75,8 @@ ssize_t chan_exec_sense_id(struct chan_out *out, uint8_t addr, void *buf, size_t
7275

7376
int chan_exec_nop(struct chan_out *out, uint8_t addr, uint8_t *status);
7477

78+
bool chan_parse_dev_addr(char *str, uint8_t *addr);
79+
7580
char *chan_fmt_status(uint8_t status, char *buf, size_t size);
7681

7782
char *chan_fmt_cmd(uint8_t cmd, char *buf, size_t size);

0 commit comments

Comments
 (0)