1 /* $OpenBSD: uark.c,v 1.26 2024/05/23 03:21:09 jsg Exp $ */
2
3 /*
4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/tty.h>
22 #include <sys/device.h>
23
24 #include <dev/usb/usb.h>
25 #include <dev/usb/usbdi.h>
26 #include <dev/usb/usbdevs.h>
27
28 #include <dev/usb/ucomvar.h>
29
30 #ifdef UARK_DEBUG
31 #define DPRINTFN(n, x) do { if (uarkdebug > (n)) printf x; } while (0)
32 int uarkebug = 0;
33 #else
34 #define DPRINTFN(n, x)
35 #endif
36 #define DPRINTF(x) DPRINTFN(0, x)
37
38 #define UARKBUFSZ 256
39 #define UARK_IFACE_NO 0
40
41 #define UARK_SET_DATA_BITS(x) (x - 5)
42
43 #define UARK_PARITY_NONE 0x00
44 #define UARK_PARITY_ODD 0x08
45 #define UARK_PARITY_EVEN 0x18
46
47 #define UARK_STOP_BITS_1 0x00
48 #define UARK_STOP_BITS_2 0x04
49
50 #define UARK_BAUD_REF 3000000
51
52 #define UARK_WRITE 0x40
53 #define UARK_READ 0xc0
54
55 #define UARK_REQUEST 0xfe
56
57 struct uark_softc {
58 struct device sc_dev;
59 struct usbd_device *sc_udev;
60 struct usbd_interface *sc_iface;
61 struct device *sc_subdev;
62
63 u_char sc_msr;
64 u_char sc_lsr;
65 };
66
67 void uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
68 void uark_set(void *, int, int, int);
69 int uark_param(void *, int, struct termios *);
70 void uark_break(void *, int, int);
71 int uark_cmd(struct uark_softc *, uint16_t, uint16_t);
72
73 const struct ucom_methods uark_methods = {
74 uark_get_status,
75 uark_set,
76 uark_param,
77 NULL,
78 NULL,
79 NULL,
80 NULL,
81 NULL,
82 };
83
84 static const struct usb_devno uark_devs[] = {
85 { USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116 }
86 };
87
88 int uark_match(struct device *, void *, void *);
89 void uark_attach(struct device *, struct device *, void *);
90 int uark_detach(struct device *, int);
91
92 struct cfdriver uark_cd = {
93 NULL, "uark", DV_DULL
94 };
95
96 const struct cfattach uark_ca = {
97 sizeof(struct uark_softc), uark_match, uark_attach, uark_detach
98 };
99
100 int
uark_match(struct device * parent,void * match,void * aux)101 uark_match(struct device *parent, void *match, void *aux)
102 {
103 struct usb_attach_arg *uaa = aux;
104
105 if (uaa->iface == NULL)
106 return UMATCH_NONE;
107
108 return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
109 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
110 }
111
112 void
uark_attach(struct device * parent,struct device * self,void * aux)113 uark_attach(struct device *parent, struct device *self, void *aux)
114 {
115 struct uark_softc *sc = (struct uark_softc *)self;
116 struct usb_attach_arg *uaa = aux;
117 struct ucom_attach_args uca;
118 usb_interface_descriptor_t *id;
119 usb_endpoint_descriptor_t *ed;
120 usbd_status error;
121 int i;
122
123 bzero(&uca, sizeof(uca));
124 sc->sc_udev = uaa->device;
125
126 /* get the first interface handle */
127 error = usbd_device2interface_handle(sc->sc_udev, UARK_IFACE_NO,
128 &sc->sc_iface);
129 if (error != 0) {
130 printf("%s: could not get interface handle\n",
131 sc->sc_dev.dv_xname);
132 usbd_deactivate(sc->sc_udev);
133 return;
134 }
135
136 id = usbd_get_interface_descriptor(sc->sc_iface);
137
138 uca.bulkin = uca.bulkout = -1;
139 for (i = 0; i < id->bNumEndpoints; i++) {
140 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
141 if (ed == NULL) {
142 printf("%s: no endpoint descriptor found for %d\n",
143 sc->sc_dev.dv_xname, i);
144 usbd_deactivate(sc->sc_udev);
145 return;
146 }
147
148 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
149 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
150 uca.bulkin = ed->bEndpointAddress;
151 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
152 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
153 uca.bulkout = ed->bEndpointAddress;
154 }
155
156 if (uca.bulkin == -1 || uca.bulkout == -1) {
157 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
158 usbd_deactivate(sc->sc_udev);
159 return;
160 }
161
162 uca.ibufsize = UARKBUFSZ;
163 uca.obufsize = UARKBUFSZ;
164 uca.ibufsizepad = UARKBUFSZ;
165 uca.opkthdrlen = 0;
166 uca.device = sc->sc_udev;
167 uca.iface = sc->sc_iface;
168 uca.methods = &uark_methods;
169 uca.arg = sc;
170 uca.info = NULL;
171
172 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
173 }
174
175 int
uark_detach(struct device * self,int flags)176 uark_detach(struct device *self, int flags)
177 {
178 struct uark_softc *sc = (struct uark_softc *)self;
179 int rv = 0;
180
181 if (sc->sc_subdev != NULL) {
182 rv = config_detach(sc->sc_subdev, flags);
183 sc->sc_subdev = NULL;
184 }
185
186 return (rv);
187 }
188
189 void
uark_set(void * vsc,int portno,int reg,int onoff)190 uark_set(void *vsc, int portno, int reg, int onoff)
191 {
192 struct uark_softc *sc = vsc;
193
194 switch (reg) {
195 case UCOM_SET_BREAK:
196 uark_break(sc, portno, onoff);
197 return;
198 case UCOM_SET_DTR:
199 case UCOM_SET_RTS:
200 default:
201 return;
202 }
203 }
204
205 int
uark_param(void * vsc,int portno,struct termios * t)206 uark_param(void *vsc, int portno, struct termios *t)
207 {
208 struct uark_softc *sc = (struct uark_softc *)vsc;
209 int data;
210
211 switch (t->c_ospeed) {
212 case 300:
213 case 600:
214 case 1200:
215 case 1800:
216 case 2400:
217 case 4800:
218 case 9600:
219 case 19200:
220 case 38400:
221 case 57600:
222 case 115200:
223 uark_cmd(sc, 3, 0x83);
224 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
225 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
226 uark_cmd(sc, 3, 0x03);
227 break;
228 default:
229 return (EINVAL);
230 }
231
232 if (ISSET(t->c_cflag, CSTOPB))
233 data = UARK_STOP_BITS_2;
234 else
235 data = UARK_STOP_BITS_1;
236
237 if (ISSET(t->c_cflag, PARENB)) {
238 if (ISSET(t->c_cflag, PARODD))
239 data |= UARK_PARITY_ODD;
240 else
241 data |= UARK_PARITY_EVEN;
242 } else
243 data |= UARK_PARITY_NONE;
244
245 switch (ISSET(t->c_cflag, CSIZE)) {
246 case CS5:
247 data |= UARK_SET_DATA_BITS(5);
248 break;
249 case CS6:
250 data |= UARK_SET_DATA_BITS(6);
251 break;
252 case CS7:
253 data |= UARK_SET_DATA_BITS(7);
254 break;
255 case CS8:
256 data |= UARK_SET_DATA_BITS(8);
257 break;
258 }
259
260 uark_cmd(sc, 3, 0x00);
261 uark_cmd(sc, 3, data);
262
263 #if 0
264 /* XXX flow control */
265 if (ISSET(t->c_cflag, CRTSCTS))
266 /* rts/cts flow ctl */
267 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
268 /* xon/xoff flow ctl */
269 } else {
270 /* disable flow ctl */
271 }
272 #endif
273
274 return (0);
275 }
276
277 void
uark_get_status(void * vsc,int portno,u_char * lsr,u_char * msr)278 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
279 {
280 struct uark_softc *sc = vsc;
281
282 if (msr != NULL)
283 *msr = sc->sc_msr;
284 if (lsr != NULL)
285 *lsr = sc->sc_lsr;
286 }
287
288 void
uark_break(void * vsc,int portno,int onoff)289 uark_break(void *vsc, int portno, int onoff)
290 {
291 #ifdef UARK_DEBUG
292 struct uark_softc *sc = vsc;
293
294 printf("%s: break %s!\n", sc->sc_dev.dv_xname,
295 onoff ? "on" : "off");
296
297 if (onoff)
298 /* break on */
299 uark_cmd(sc, 4, 0x01);
300 else
301 uark_cmd(sc, 4, 0x00);
302 #endif
303 }
304
305 int
uark_cmd(struct uark_softc * sc,uint16_t index,uint16_t value)306 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
307 {
308 usb_device_request_t req;
309 usbd_status err;
310
311 req.bmRequestType = UARK_WRITE;
312 req.bRequest = UARK_REQUEST;
313 USETW(req.wValue, value);
314 USETW(req.wIndex, index);
315 USETW(req.wLength, 0);
316 err = usbd_do_request(sc->sc_udev, &req, NULL);
317
318 if (err)
319 return (EIO);
320
321 return (0);
322 }
323