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