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