kernel - AHCI - enable AHCI device initiated power management
[dragonfly.git] / sys / dev / usbmisc / uark / uark.c
blobbb746b6349972b238c42d0a51f768dfed5c51ce5
1 /* $DragonFly: src/sys/dev/usbmisc/uark/uark.c,v 1.6 2007/11/06 07:37:01 hasso Exp $ */
2 /* $OpenBSD: uark.c,v 1.9 2007/06/13 06:25:03 mbalmer Exp $ */
4 /*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/conf.h>
24 #include <sys/tty.h>
25 #include <sys/device.h>
26 #include <sys/types.h>
27 #include <sys/bus.h>
28 #include <sys/module.h>
30 #include <bus/usb/usb.h>
31 #include <bus/usb/usbdi.h>
32 #include <bus/usb/usbdi_util.h>
34 #include <dev/usbmisc/ucom/ucomvar.h>
36 #ifdef UARK_DEBUG
37 #define DPRINTFN(n, x) do { if (uarkdebug > (n)) kprintf x; } while (0)
38 int uarkebug = 0;
39 #else
40 #define DPRINTFN(n, x)
41 #endif
42 #define DPRINTF(x) DPRINTFN(0, x)
44 #define UARKBUFSZ 256
45 #define UARK_CONFIG_NO 0
46 #define UARK_IFACE_NO 0
48 #define UARK_SET_DATA_BITS(x) (x - 5)
50 #define UARK_PARITY_NONE 0x00
51 #define UARK_PARITY_ODD 0x08
52 #define UARK_PARITY_EVEN 0x18
54 #define UARK_STOP_BITS_1 0x00
55 #define UARK_STOP_BITS_2 0x04
57 #define UARK_BAUD_REF 3000000
59 #define UARK_WRITE 0x40
60 #define UARK_READ 0xc0
62 #define UARK_REQUEST 0xfe
64 struct uark_softc {
65 struct ucom_softc sc_ucom;
66 u_char sc_msr;
67 u_char sc_lsr;
70 static void uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
71 static void uark_set(void *, int, int, int);
72 static int uark_param(void *, int, struct termios *);
73 static void uark_break(void *, int, int);
74 static int uark_cmd(struct uark_softc *, uint16_t, uint16_t);
76 struct ucom_callback uark_callback = {
77 uark_get_status,
78 uark_set,
79 uark_param,
80 NULL,
81 NULL,
82 NULL,
83 NULL,
84 NULL,
87 static const struct usb_devno uark_devs[] = {
88 { USB_DEVICE(0x6547, 0x0232) } /* Arkmicro Technologies ARK3116 */
91 static device_probe_t uark_match;
92 static device_attach_t uark_attach;
93 static device_detach_t uark_detach;
95 static device_method_t uark_methods[] = {
96 /* Device interface */
97 DEVMETHOD(device_probe, uark_match),
98 DEVMETHOD(device_attach, uark_attach),
99 DEVMETHOD(device_detach, uark_detach),
100 { 0, 0 }
103 static driver_t uark_driver = {
104 "ucom",
105 uark_methods,
106 sizeof (struct uark_softc)
109 DRIVER_MODULE(uark, uhub, uark_driver, ucom_devclass, usbd_driver_load, 0);
110 MODULE_DEPEND(uark, usb, 1, 1, 1);
111 MODULE_DEPEND(uark, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
112 MODULE_VERSION(uark, 1);
114 static int
115 uark_match(device_t self)
117 struct usb_attach_arg *uaa = device_get_ivars(self);
119 if (uaa->iface != NULL)
120 return UMATCH_NONE;
122 return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
123 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
126 static int
127 uark_attach(device_t self)
129 struct uark_softc *sc = device_get_softc(self);
130 struct usb_attach_arg *uaa = device_get_ivars(self);
131 struct ucom_softc *ucom;
132 usb_interface_descriptor_t *id;
133 usb_endpoint_descriptor_t *ed;
134 usbd_status error;
135 int i;
137 ucom = &sc->sc_ucom;
138 bzero(sc, sizeof (struct uark_softc));
140 ucom->sc_dev = self;
141 ucom->sc_udev = uaa->device;
142 ucom->sc_iface = uaa->iface;
144 if (usbd_set_config_index(ucom->sc_udev, UARK_CONFIG_NO, 1) != 0) {
145 device_printf(ucom->sc_dev, "could not set configuration no\n");
146 goto error;
149 /* get the first interface handle */
150 error = usbd_device2interface_handle(ucom->sc_udev, UARK_IFACE_NO,
151 &ucom->sc_iface);
152 if (error != 0) {
153 device_printf(ucom->sc_dev, "could not get interface handle\n");
154 goto error;
157 id = usbd_get_interface_descriptor(ucom->sc_iface);
159 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
160 for (i = 0; i < id->bNumEndpoints; i++) {
161 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
162 if (ed == NULL) {
163 device_printf(ucom->sc_dev, "no endpoint descriptor "
164 "found for %d\n", i);
165 goto error;
168 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
169 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
170 ucom->sc_bulkin_no = ed->bEndpointAddress;
171 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
172 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
173 ucom->sc_bulkout_no = ed->bEndpointAddress;
176 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
177 device_printf(ucom->sc_dev, "missing endpoint\n");
178 goto error;
181 ucom->sc_parent = sc;
182 ucom->sc_portno = UCOM_UNK_PORTNO;
183 ucom->sc_ibufsize = UARKBUFSZ;
184 ucom->sc_obufsize = UARKBUFSZ;
185 ucom->sc_ibufsizepad = UARKBUFSZ;
186 ucom->sc_opkthdrlen = 0;
187 ucom->sc_callback = &uark_callback;
189 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
190 ucom->sc_dev);
192 DPRINTF(("uark: in = 0x%x, out = 0x%x, intr = 0x%x\n",
193 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
195 ucom_attach(&sc->sc_ucom);
197 return 0;
199 error:
200 ucom->sc_dying = 1;
201 return ENXIO;
204 static int
205 uark_detach(device_t self)
207 struct uark_softc *sc = device_get_softc(self);
208 int rv = 0;
210 DPRINTF(("uark_detach: sc=%p\n", sc));
211 sc->sc_ucom.sc_dying = 1;
212 rv = ucom_detach(&sc->sc_ucom);
213 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
214 sc->sc_ucom.sc_dev);
216 return (rv);
219 #if 0 /* not yet */
221 uark_activate(struct device *self, enum devact act)
223 struct uark_softc *sc = (struct uark_softc *)self;
224 int rv = 0;
226 switch (act) {
227 case DVACT_ACTIVATE:
228 break;
230 case DVACT_DEACTIVATE:
231 if (sc->sc_subdev != NULL)
232 rv = config_deactivate(sc->sc_subdev);
233 sc->sc_dying = 1;
234 break;
236 return (rv);
238 #endif
240 static void
241 uark_set(void *vsc, int portno, int reg, int onoff)
243 struct uark_softc *sc = vsc;
245 switch (reg) {
246 case UCOM_SET_BREAK:
247 uark_break(sc, portno, onoff);
248 return;
249 case UCOM_SET_DTR:
250 case UCOM_SET_RTS:
251 default:
252 return;
256 static int
257 uark_param(void *vsc, int portno, struct termios *t)
259 struct uark_softc *sc = (struct uark_softc *)vsc;
260 int data;
262 switch (t->c_ospeed) {
263 case 300:
264 case 600:
265 case 1200:
266 case 1800:
267 case 2400:
268 case 4800:
269 case 9600:
270 case 19200:
271 case 38400:
272 case 57600:
273 case 115200:
274 uark_cmd(sc, 3, 0x83);
275 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
276 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
277 uark_cmd(sc, 3, 0x03);
278 break;
279 default:
280 return (EINVAL);
283 if (ISSET(t->c_cflag, CSTOPB))
284 data = UARK_STOP_BITS_2;
285 else
286 data = UARK_STOP_BITS_1;
288 if (ISSET(t->c_cflag, PARENB)) {
289 if (ISSET(t->c_cflag, PARODD))
290 data |= UARK_PARITY_ODD;
291 else
292 data |= UARK_PARITY_EVEN;
293 } else
294 data |= UARK_PARITY_NONE;
296 switch (ISSET(t->c_cflag, CSIZE)) {
297 case CS5:
298 data |= UARK_SET_DATA_BITS(5);
299 break;
300 case CS6:
301 data |= UARK_SET_DATA_BITS(6);
302 break;
303 case CS7:
304 data |= UARK_SET_DATA_BITS(7);
305 break;
306 case CS8:
307 data |= UARK_SET_DATA_BITS(8);
308 break;
311 uark_cmd(sc, 3, 0x00);
312 uark_cmd(sc, 3, data);
314 #if 0
315 /* XXX flow control */
316 if (ISSET(t->c_cflag, CRTSCTS))
317 /* rts/cts flow ctl */
318 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
319 /* xon/xoff flow ctl */
320 } else {
321 /* disable flow ctl */
323 #endif
325 return (0);
328 static void
329 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
331 struct uark_softc *sc = vsc;
333 if (msr != NULL)
334 *msr = sc->sc_msr;
335 if (lsr != NULL)
336 *lsr = sc->sc_lsr;
339 static void
340 uark_break(void *vsc, int portno, int onoff)
342 #ifdef UARK_DEBUG
343 struct uark_softc *sc = vsc;
345 device_printf(sc->sc_ucom.sc_dev, "break %s!\n", onoff ? "on" : "off");
347 if (onoff)
348 /* break on */
349 uark_cmd(sc, 4, 0x01);
350 else
351 uark_cmd(sc, 4, 0x00);
352 #endif
355 static int
356 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
358 usb_device_request_t req;
359 usbd_status err;
361 req.bmRequestType = UARK_WRITE;
362 req.bRequest = UARK_REQUEST;
363 USETW(req.wValue, value);
364 USETW(req.wIndex, index);
365 USETW(req.wLength, 0);
366 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
368 if (err)
369 return (EIO);
371 return (0);