Date:2010-09-05 23:59:42 (8 years 11 months ago)
Author:Werner Almesberger
Commit:e2b2df2e315c6b0338aed53dffd1dce9cae8196e
Message:Made the libatspi API driver-agnostic.

- tools/include/atspi.h: API is now driver-independent
- tools/lib/driver.h: API between driver and frontend
- tools/lib/atspi.c: renamed to atusb.c and updated for driver API
- tools/lib/atspi.c: frontend functions for the driver API
- tools/lib/Makefile: added atusb.o
Files: tools/include/atspi.h (1 diff)
tools/lib/Makefile (1 diff)
tools/lib/atspi.c (1 diff)
tools/lib/atusb.c (1 diff)
tools/lib/driver.h (1 diff)

Change Details

tools/include/atspi.h
1414#define ATSPI_H
1515
1616#include <stdint.h>
17#include <usb.h>
17
18
19struct atspi_dsc;
1820
1921
2022int atspi_error(void);
2123int atspi_clear_error(void);
2224
23usb_dev_handle *atspi_open(void);
24void atspi_close(usb_dev_handle *dev);
25struct atspi_dsc *atspi_open(void);
26void atspi_close(struct atspi_dsc *dsc);
2527
26void atspi_reset(usb_dev_handle *dev);
27void atspi_reset_rf(usb_dev_handle *dev);
28void atspi_reset(struct atspi_dsc *dsc);
29void atspi_reset_rf(struct atspi_dsc *dsc);
2830
29void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value);
30uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg);
31void atspi_reg_write(struct atspi_dsc *dsc, uint8_t reg, uint8_t value);
32uint8_t atspi_reg_read(struct atspi_dsc *dsc, uint8_t reg);
3133
32void atspi_buf_write(usb_dev_handle *dev, const void *buf, int size);
33int atspi_buf_read(usb_dev_handle *dev, void *buf, int size);
34void atspi_buf_write(struct atspi_dsc *dsc, const void *buf, int size);
35int atspi_buf_read(struct atspi_dsc *dsc, void *buf, int size);
3436
3537#endif /* !ATSPI_H */
tools/lib/Makefile
1616LIB = libatspi.a
1717
1818CFLAGS = -Wall -I$(F32XBASE)/include -I../../atrf/fw/include -I../include
19OBJS = $(F32XBASE)/lib/usb.o atspi.o
19OBJS = $(F32XBASE)/lib/usb.o atspi.o atusb.o
2020
2121.PHONY: all clean spotless
2222
tools/lib/atspi.c
1111 */
1212
1313
14#include <stdlib.h>
1415#include <stdio.h>
15#include <usb.h>
16
17#include "f32xbase/usb.h"
18#include "atspi/ep0.h"
19#include "atspi/usb-ids.h"
2016
17#include "driver.h"
2118#include "atspi.h"
2219
2320
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28/* ----- error handling ---------------------------------------------------- */
29
30
31static int error;
32
33
34int atspi_error(void)
35{
36    return error;
37}
38
39
40int atspi_clear_error(void)
41{
42    int ret;
21extern struct atspi_driver atusb_driver;
4322
44    ret = error;
45    error = 0;
46    return ret;
47}
4823
24struct atspi_dsc {
25    struct atspi_driver *driver;
26    void *handle;
27};
4928
50/* ----- open/close -------------------------------------------------------- */
5129
52
53usb_dev_handle *atspi_open(void)
30struct atspi_dsc *atspi_open(void)
5431{
55    usb_dev_handle *dev;
56
57    dev = open_usb(USB_VENDOR, USB_PRODUCT);
58    if (dev) {
59        error = 0;
60    } else {
61        fprintf(stderr, ":-(\n");
62        error = 1;
32    struct atspi_dsc *dsc;
33    struct atspi_driver *driver;
34    void *handle;
35
36    driver = &atusb_driver;
37    handle = driver->open();
38    if (!handle)
39        return NULL;
40    dsc = malloc(sizeof(*dsc));
41    if (!dsc) {
42        perror("malloc");
43        exit(1);
6344    }
64    return dev;
45    dsc->driver = driver;
46    dsc->handle = handle;
47    return dsc;
6548}
6649
6750
68void atspi_close(usb_dev_handle *dev)
51void atspi_close(struct atspi_dsc *dsc)
6952{
70    /* to do */
53    if (dsc->driver->close)
54        dsc->driver->close(dsc->handle);
55    free(dsc);
7156}
7257
7358
74/* ----- device mode ------------------------------------------------------- */
75
76
77void atspi_reset(usb_dev_handle *dev)
59void atspi_reset(struct atspi_dsc *dsc)
7860{
79    int res;
80
81    if (error)
82        return;
83
84    res =
85        usb_control_msg(dev, TO_DEV, ATSPI_RESET, 0, 0, NULL, 0, 1000);
86    if (res < 0) {
87        fprintf(stderr, "ATSPI_RESET: %d\n", res);
88        error = 1;
89    }
61    if (dsc->driver->reset)
62        dsc->driver->reset(dsc->handle);
9063}
9164
9265
93void atspi_reset_rf(usb_dev_handle *dev)
66void atspi_reset_rf(struct atspi_dsc *dsc)
9467{
95    int res;
96
97    if (error)
98        return;
99
100    res =
101        usb_control_msg(dev, TO_DEV, ATSPI_RF_RESET, 0, 0, NULL, 0, 1000);
102    if (res < 0) {
103        fprintf(stderr, "ATSPI_RF_RESET: %d\n", res);
104        error = 1;
105    }
68    dsc->driver->reset_rf(dsc->handle);
10669}
10770
10871
109/* ----- register access --------------------------------------------------- */
110
111
112void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value)
72void atspi_reg_write(struct atspi_dsc *dsc, uint8_t reg, uint8_t value)
11373{
114    int res;
115
116    if (error)
117        return;
118
119    res = usb_control_msg(dev, TO_DEV, ATSPI_REG_WRITE, value, reg,
120        NULL, 0, 1000);
121    if (res < 0) {
122        fprintf(stderr, "ATSPI_REG_WRITE: %d\n", res);
123        error = 1;
124    }
74    dsc->driver->reg_write(dsc->handle, reg, value);
12575}
12676
12777
128uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg)
78uint8_t atspi_reg_read(struct atspi_dsc *dsc, uint8_t reg)
12979{
130    uint8_t value = 0;
131    int res;
132
133    if (error)
134        return 0;
135
136    res = usb_control_msg(dev, FROM_DEV, ATSPI_REG_READ, 0, reg,
137        (void *) &value, 1, 1000);
138    if (res < 0) {
139        fprintf(stderr, "ATSPI_REG_READ: %d\n", res);
140        error = 1;
141    }
142    return value;
80    return dsc->driver->reg_read(dsc->handle, reg);
14381}
14482
14583
146/* ----- frame buffer access ----------------------------------------------- */
147
148
149void atspi_buf_write(usb_dev_handle *dev, const void *buf, int size)
84void atspi_buf_write(struct atspi_dsc *dsc, const void *buf, int size)
15085{
151    int res;
152
153    if (error)
154        return;
155
156    res = usb_control_msg(dev, TO_DEV, ATSPI_BUF_WRITE, 0, 0,
157        (void *) buf, size, 1000);
158    if (res < 0) {
159        fprintf(stderr, "ATSPI_BUF_WRITE: %d\n", res);
160        error = 1;
161    }
162
86    dsc->driver->buf_write(dsc->handle, buf, size);
16387}
16488
16589
166int atspi_buf_read(usb_dev_handle *dev, void *buf, int size)
90int atspi_buf_read(struct atspi_dsc *dsc, void *buf, int size)
16791{
168    int res;
169
170    if (error)
171        return -1;
172
173    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUF_READ, 0, 0,
174        buf, size, 1000);
175    if (res < 0) {
176        fprintf(stderr, "ATSPI_BUF_READ: %d\n", res);
177        error = 1;
178    }
179
180    return res;
92    return dsc->driver->buf_read(dsc->handle, buf, size);
18193}
tools/lib/atusb.c
1/*
2 * lib/atusb.c - ATSPI access functions library (USB version)
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdio.h>
15#include <usb.h>
16
17#include "f32xbase/usb.h"
18#include "atspi/ep0.h"
19#include "atspi/usb-ids.h"
20
21#include "driver.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28/* ----- error handling ---------------------------------------------------- */
29
30
31static int error;
32
33
34int atusb_error(void)
35{
36    return error;
37}
38
39
40int atusb_clear_error(void)
41{
42    int ret;
43
44    ret = error;
45    error = 0;
46    return ret;
47}
48
49
50/* ----- open/close -------------------------------------------------------- */
51
52
53static void *atusb_open(void)
54{
55    usb_dev_handle *dev;
56
57    dev = open_usb(USB_VENDOR, USB_PRODUCT);
58    if (dev) {
59        error = 0;
60    } else {
61        fprintf(stderr, ":-(\n");
62        error = 1;
63    }
64    return dev;
65}
66
67
68static void atusb_close(void *dsc)
69{
70    /* to do */
71}
72
73
74/* ----- device mode ------------------------------------------------------- */
75
76
77static void atusb_reset(void *dsc)
78{
79    usb_dev_handle *dev = dsc;
80    int res;
81
82    if (error)
83        return;
84
85    res =
86        usb_control_msg(dev, TO_DEV, ATSPI_RESET, 0, 0, NULL, 0, 1000);
87    if (res < 0) {
88        fprintf(stderr, "ATSPI_RESET: %d\n", res);
89        error = 1;
90    }
91}
92
93
94static void atusb_reset_rf(void *dsc)
95{
96    usb_dev_handle *dev = dsc;
97    int res;
98
99    if (error)
100        return;
101
102    res =
103        usb_control_msg(dev, TO_DEV, ATSPI_RF_RESET, 0, 0, NULL, 0, 1000);
104    if (res < 0) {
105        fprintf(stderr, "ATSPI_RF_RESET: %d\n", res);
106        error = 1;
107    }
108}
109
110
111/* ----- register access --------------------------------------------------- */
112
113
114static void atusb_reg_write(void *dsc, uint8_t reg, uint8_t value)
115{
116    usb_dev_handle *dev = dsc;
117    int res;
118
119    if (error)
120        return;
121
122    res = usb_control_msg(dev, TO_DEV, ATSPI_REG_WRITE, value, reg,
123        NULL, 0, 1000);
124    if (res < 0) {
125        fprintf(stderr, "ATSPI_REG_WRITE: %d\n", res);
126        error = 1;
127    }
128}
129
130
131static uint8_t atusb_reg_read(void *dsc, uint8_t reg)
132{
133    usb_dev_handle *dev = dsc;
134    uint8_t value = 0;
135    int res;
136
137    if (error)
138        return 0;
139
140    res = usb_control_msg(dev, FROM_DEV, ATSPI_REG_READ, 0, reg,
141        (void *) &value, 1, 1000);
142    if (res < 0) {
143        fprintf(stderr, "ATSPI_REG_READ: %d\n", res);
144        error = 1;
145    }
146    return value;
147}
148
149
150/* ----- frame buffer access ----------------------------------------------- */
151
152
153static void atusb_buf_write(void *dsc, const void *buf, int size)
154{
155    usb_dev_handle *dev = dsc;
156    int res;
157
158    if (error)
159        return;
160
161    res = usb_control_msg(dev, TO_DEV, ATSPI_BUF_WRITE, 0, 0,
162        (void *) buf, size, 1000);
163    if (res < 0) {
164        fprintf(stderr, "ATSPI_BUF_WRITE: %d\n", res);
165        error = 1;
166    }
167
168}
169
170
171static int atusb_buf_read(void *dsc, void *buf, int size)
172{
173    usb_dev_handle *dev = dsc;
174    int res;
175
176    if (error)
177        return -1;
178
179    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUF_READ, 0, 0,
180        buf, size, 1000);
181    if (res < 0) {
182        fprintf(stderr, "ATSPI_BUF_READ: %d\n", res);
183        error = 1;
184    }
185
186    return res;
187}
188
189
190/* ----- driver interface -------------------------------------------------- */
191
192
193struct atspi_driver atusb_driver = {
194    .name = "atusb",
195    .open = atusb_open,
196    .close = atusb_close,
197    .reset = atusb_reset,
198    .reset_rf = atusb_reset_rf,
199    .reg_write = atusb_reg_write,
200    .reg_read = atusb_reg_read,
201    .buf_write = atusb_buf_write,
202    .buf_read = atusb_buf_read,
203};
tools/lib/driver.h
1/*
2 * lib/driver.h - ATSPI driver API
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#ifndef DRIVER_H
15#define DRIVER_H
16
17#include <stdint.h>
18
19
20struct atspi_driver {
21    const char *name;
22    void *(*open)(void);
23    void (*close)(void *dsc);
24    void (*reset)(void *dsc);
25    void (*reset_rf)(void *dsc);
26    void (*reg_write)(void *dsc, uint8_t reg, uint8_t value);
27    uint8_t (*reg_read)(void *dsc, uint8_t reg);
28    void (*buf_write)(void *dsc, const void *buf, int size);
29    int (*buf_read)(void *dsc, void *buf, int size);
30};
31
32#endif /* !DRIVER_H */

Archive Download the corresponding diff file



interactive