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