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