xref: /openbsd-src/sys/dev/usb/uark.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: uark.c,v 1.11 2007/10/11 18:33:14 deraadt 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/kernel.h>
22 #include <sys/conf.h>
23 #include <sys/tty.h>
24 #include <sys/device.h>
25 
26 #include <dev/usb/usb.h>
27 #include <dev/usb/usbdi.h>
28 #include <dev/usb/usbdi_util.h>
29 #include <dev/usb/usbdevs.h>
30 
31 #include <dev/usb/usbdevs.h>
32 #include <dev/usb/ucomvar.h>
33 
34 #ifdef UARK_DEBUG
35 #define DPRINTFN(n, x)  do { if (uarkdebug > (n)) printf x; } while (0)
36 int	uarkebug = 0;
37 #else
38 #define DPRINTFN(n, x)
39 #endif
40 #define DPRINTF(x) DPRINTFN(0, x)
41 
42 #define UARKBUFSZ		256
43 #define UARK_CONFIG_NO	0
44 #define UARK_IFACE_NO		0
45 
46 #define UARK_SET_DATA_BITS(x)	(x - 5)
47 
48 #define UARK_PARITY_NONE	0x00
49 #define UARK_PARITY_ODD		0x08
50 #define UARK_PARITY_EVEN	0x18
51 
52 #define UARK_STOP_BITS_1	0x00
53 #define UARK_STOP_BITS_2	0x04
54 
55 #define UARK_BAUD_REF		3000000
56 
57 #define UARK_WRITE		0x40
58 #define UARK_READ		0xc0
59 
60 #define UARK_REQUEST		0xfe
61 
62 struct uark_softc {
63 	struct device		 sc_dev;
64 	usbd_device_handle	 sc_udev;
65 	usbd_interface_handle	 sc_iface;
66 	struct device 		*sc_subdev;
67 
68 	u_char			 sc_msr;
69 	u_char			 sc_lsr;
70 
71 	u_char			 sc_dying;
72 };
73 
74 void	uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
75 void	uark_set(void *, int, int, int);
76 int	uark_param(void *, int, struct termios *);
77 int	uark_open(void *sc, int);
78 void	uark_break(void *, int, int);
79 int	uark_cmd(struct uark_softc *, uint16_t, uint16_t);
80 
81 struct ucom_methods uark_methods = {
82 	uark_get_status,
83 	uark_set,
84 	uark_param,
85 	NULL,
86 	NULL,
87 	NULL,
88 	NULL,
89 	NULL,
90 };
91 
92 static const struct usb_devno uark_devs[] = {
93 	{ USB_VENDOR_ARKMICRO,		USB_PRODUCT_ARKMICRO_ARK3116 }
94 };
95 
96 int uark_match(struct device *, void *, void *);
97 void uark_attach(struct device *, struct device *, void *);
98 int uark_detach(struct device *, int);
99 int uark_activate(struct device *, enum devact);
100 
101 struct cfdriver uark_cd = {
102 	NULL, "uark", DV_DULL
103 };
104 
105 const struct cfattach uark_ca = {
106 	sizeof(struct uark_softc),
107 	uark_match,
108 	uark_attach,
109 	uark_detach,
110 	uark_activate,
111 };
112 
113 int
114 uark_match(struct device *parent, void *match, void *aux)
115 {
116 	struct usb_attach_arg *uaa = aux;
117 
118 	if (uaa->iface != NULL)
119 		return UMATCH_NONE;
120 
121 	return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
122 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
123 }
124 
125 void
126 uark_attach(struct device *parent, struct device *self, void *aux)
127 {
128 	struct uark_softc *sc = (struct uark_softc *)self;
129 	struct usb_attach_arg *uaa = aux;
130 	struct ucom_attach_args uca;
131 	usb_interface_descriptor_t *id;
132 	usb_endpoint_descriptor_t *ed;
133 	usbd_status error;
134 	int i;
135 
136 	bzero(&uca, sizeof(uca));
137 	sc->sc_udev = uaa->device;
138 
139 	if (usbd_set_config_index(sc->sc_udev, UARK_CONFIG_NO, 1) != 0) {
140 		printf("%s: could not set configuration no\n",
141 		    sc->sc_dev.dv_xname);
142 		sc->sc_dying = 1;
143 		return;
144 	}
145 
146 	/* get the first interface handle */
147 	error = usbd_device2interface_handle(sc->sc_udev, UARK_IFACE_NO,
148 	    &sc->sc_iface);
149 	if (error != 0) {
150 		printf("%s: could not get interface handle\n",
151 		    sc->sc_dev.dv_xname);
152 		sc->sc_dying = 1;
153 		return;
154 	}
155 
156 	id = usbd_get_interface_descriptor(sc->sc_iface);
157 
158 	uca.bulkin = uca.bulkout = -1;
159 	for (i = 0; i < id->bNumEndpoints; i++) {
160 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
161 		if (ed == NULL) {
162 			printf("%s: no endpoint descriptor found for %d\n",
163 			    sc->sc_dev.dv_xname, i);
164 			sc->sc_dying = 1;
165 			return;
166 		}
167 
168 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
169 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
170 			uca.bulkin = ed->bEndpointAddress;
171 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
172 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
173 			uca.bulkout = ed->bEndpointAddress;
174 	}
175 
176 	if (uca.bulkin == -1 || uca.bulkout == -1) {
177 		printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
178 		sc->sc_dying = 1;
179 		return;
180 	}
181 
182 	uca.ibufsize = UARKBUFSZ;
183 	uca.obufsize = UARKBUFSZ;
184 	uca.ibufsizepad = UARKBUFSZ;
185 	uca.opkthdrlen = 0;
186 	uca.device = sc->sc_udev;
187 	uca.iface = sc->sc_iface;
188 	uca.methods = &uark_methods;
189 	uca.arg = sc;
190 	uca.info = NULL;
191 
192 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
193 	    &sc->sc_dev);
194 
195 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
196 }
197 
198 int
199 uark_detach(struct device *self, int flags)
200 {
201 	struct uark_softc *sc = (struct uark_softc *)self;
202 	int rv = 0;
203 
204 	sc->sc_dying = 1;
205 	if (sc->sc_subdev != NULL) {
206 		rv = config_detach(sc->sc_subdev, flags);
207 		sc->sc_subdev = NULL;
208 	}
209 
210 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
211 			   &sc->sc_dev);
212 
213 	return (rv);
214 }
215 
216 int
217 uark_activate(struct device *self, enum devact act)
218 {
219 	struct uark_softc *sc = (struct uark_softc *)self;
220 	int rv = 0;
221 
222 	switch (act) {
223 	case DVACT_ACTIVATE:
224 		break;
225 
226 	case DVACT_DEACTIVATE:
227 		if (sc->sc_subdev != NULL)
228 			rv = config_deactivate(sc->sc_subdev);
229 		sc->sc_dying = 1;
230 		break;
231 	}
232 	return (rv);
233 }
234 
235 void
236 uark_set(void *vsc, int portno, int reg, int onoff)
237 {
238 	struct uark_softc *sc = vsc;
239 
240 	switch (reg) {
241 	case UCOM_SET_BREAK:
242 		uark_break(sc, portno, onoff);
243 		return;
244 	case UCOM_SET_DTR:
245 	case UCOM_SET_RTS:
246 	default:
247 		return;
248 	}
249 }
250 
251 int
252 uark_param(void *vsc, int portno, struct termios *t)
253 {
254 	struct uark_softc *sc = (struct uark_softc *)vsc;
255 	int data;
256 
257 	switch (t->c_ospeed) {
258 	case 300:
259 	case 600:
260 	case 1200:
261 	case 1800:
262 	case 2400:
263 	case 4800:
264 	case 9600:
265 	case 19200:
266 	case 38400:
267 	case 57600:
268 	case 115200:
269 		uark_cmd(sc, 3, 0x83);
270 		uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
271 		uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
272 		uark_cmd(sc, 3, 0x03);
273 		break;
274 	default:
275 		return (EINVAL);
276 	}
277 
278 	if (ISSET(t->c_cflag, CSTOPB))
279 		data = UARK_STOP_BITS_2;
280 	else
281 		data = UARK_STOP_BITS_1;
282 
283 	if (ISSET(t->c_cflag, PARENB)) {
284 		if (ISSET(t->c_cflag, PARODD))
285 			data |= UARK_PARITY_ODD;
286 		else
287 			data |= UARK_PARITY_EVEN;
288 	} else
289 		data |= UARK_PARITY_NONE;
290 
291 	switch (ISSET(t->c_cflag, CSIZE)) {
292 	case CS5:
293 		data |= UARK_SET_DATA_BITS(5);
294 		break;
295 	case CS6:
296 		data |= UARK_SET_DATA_BITS(6);
297 		break;
298 	case CS7:
299 		data |= UARK_SET_DATA_BITS(7);
300 		break;
301 	case CS8:
302 		data |= UARK_SET_DATA_BITS(8);
303 		break;
304 	}
305 
306 	uark_cmd(sc, 3, 0x00);
307 	uark_cmd(sc, 3, data);
308 
309 #if 0
310 	/* XXX flow control */
311 	if (ISSET(t->c_cflag, CRTSCTS))
312 		/*  rts/cts flow ctl */
313 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
314 		/*  xon/xoff flow ctl */
315 	} else {
316 		/* disable flow ctl */
317 	}
318 #endif
319 
320 	return (0);
321 }
322 
323 void
324 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
325 {
326 	struct uark_softc *sc = vsc;
327 
328 	if (msr != NULL)
329 		*msr = sc->sc_msr;
330 	if (lsr != NULL)
331 		*lsr = sc->sc_lsr;
332 }
333 
334 void
335 uark_break(void *vsc, int portno, int onoff)
336 {
337 #ifdef UARK_DEBUG
338 	struct uark_softc *sc = vsc;
339 
340 	printf("%s: break %s!\n", sc->sc_dev.dv_xname,
341 	    onoff ? "on" : "off");
342 
343 	if (onoff)
344 		/* break on */
345 		uark_cmd(sc, 4, 0x01);
346 	else
347 		uark_cmd(sc, 4, 0x00);
348 #endif
349 }
350 
351 int
352 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
353 {
354 	usb_device_request_t req;
355 	usbd_status err;
356 
357 	req.bmRequestType = UARK_WRITE;
358 	req.bRequest = UARK_REQUEST;
359 	USETW(req.wValue, value);
360 	USETW(req.wIndex, index);
361 	USETW(req.wLength, 0);
362 	err = usbd_do_request(sc->sc_udev, &req, NULL);
363 
364 	if (err)
365 		return (EIO);
366 
367 	return (0);
368 }
369