Remove unused cctools code paths for old libusb and cp2103 ioctls.
[fw/altos] / cctools / lib / ccdbg-io.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ccdbg.h"
20 #include <time.h>
21 #include "cc-usb.h"
22 #include "cc-bitbang.h"
23
24 struct ccdbg *
25 ccdbg_open(void)
26 {
27         struct ccdbg *dbg;
28         char *tty;
29
30         dbg = calloc(sizeof (struct ccdbg), 1);
31         if (!dbg) {
32                 perror("calloc");
33                 return NULL;
34         }
35         tty = getenv("CCDBG_TTY");
36         if (!tty || tty[0] == '/')
37                 dbg->usb = cc_usb_open(tty);
38         if (!dbg->usb) {
39                 dbg->bb = cc_bitbang_open();
40                 if (!dbg->bb) {
41                         free(dbg);
42                         return NULL;
43                 }
44         }
45         return dbg;
46 }
47
48 void
49 ccdbg_close(struct ccdbg *dbg)
50 {
51         if (dbg->usb)
52                 cc_usb_close(dbg->usb);
53         if (dbg->bb)
54                 cc_bitbang_close(dbg->bb);
55         free (dbg);
56 }
57
58 void
59 ccdbg_debug_mode(struct ccdbg *dbg)
60 {
61         if (dbg->usb)
62                 cc_usb_debug_mode(dbg->usb);
63         else if (dbg->bb)
64                 cc_bitbang_debug_mode(dbg->bb);
65 }
66
67 void
68 ccdbg_reset(struct ccdbg *dbg)
69 {
70         if (dbg->usb)
71                 cc_usb_reset(dbg->usb);
72         else if (dbg->bb)
73                 cc_bitbang_reset(dbg->bb);
74 }
75
76 void
77 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
78 {
79         if (dbg->usb)
80                 cc_usb_send_bytes(dbg->usb, bytes, nbytes);
81         else if (dbg->bb)
82                 cc_bitbang_send_bytes(dbg->bb, bytes, nbytes);
83 }
84
85 void
86 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
87 {
88         if (dbg->usb)
89                 cc_usb_recv_bytes(dbg->usb, bytes, nbytes);
90         else if (dbg->bb)
91                 cc_bitbang_recv_bytes(dbg->bb, bytes, nbytes);
92 }
93
94 void
95 ccdbg_sync(struct ccdbg *dbg)
96 {
97         if (dbg->usb)
98                 cc_usb_sync(dbg->usb);
99         else if (dbg->bb)
100                 cc_bitbang_sync(dbg->bb);
101 }
102
103 void
104 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
105 {
106         ccdbg_send_bytes(dbg, &cmd, 1);
107         ccdbg_send_bytes(dbg, data, len);
108 }
109
110 uint8_t
111 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
112 {
113         uint8_t byte[1];
114         ccdbg_cmd_write(dbg, cmd, data, len);
115         ccdbg_recv_bytes(dbg, byte, 1);
116         ccdbg_sync(dbg);
117         return byte[0];
118 }
119
120 uint16_t
121 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
122 {
123         uint8_t byte[2];
124         ccdbg_cmd_write(dbg, cmd, data, len);
125         ccdbg_recv_bytes(dbg, byte, 2);
126         ccdbg_sync(dbg);
127         return (byte[0] << 8) | byte[1];
128 }
129
130 void
131 ccdbg_cmd_write_queue8(struct ccdbg *dbg, uint8_t cmd,
132                        uint8_t *data, int len,
133                        uint8_t *reply)
134 {
135         ccdbg_cmd_write(dbg, cmd, data, len);
136         ccdbg_recv_bytes(dbg, reply, 1);
137 }