Huawei UMTS/HSDPA adapetrs are already handled by umsm(4).
[dragonfly.git] / sys / dev / usbmisc / uplcom / uplcom.c
CommitLineData
62ade751 1/*
1550dfd9 2 * $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $
4de415d2 3 * $FreeBSD: src/sys/dev/usb/uplcom.c,v 1.39 2006/09/07 00:06:42 imp Exp $
652c597b 4 * $DragonFly: src/sys/dev/usbmisc/uplcom/uplcom.c,v 1.26 2008/01/13 16:04:02 hasso Exp $
62ade751 5 */
984263bc
MD
6
7/*-
4de415d2 8 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
984263bc
MD
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 2001 The NetBSD Foundation, Inc.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Ichiro FUKUHARA (ichiro@ichiro.org).
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69/*
57785fef
HT
70 * This driver supports several devices devices driven by Prolific PL-2303
71 * (known also as PL-2303H), PL-2303X and PL-2303HX USB-to-RS232 bridge chip.
72 * The devices are sold under many different brand names.
984263bc 73 *
57785fef
HT
74 * Datasheets are available at Prolific www site at http://www.prolific.com.tw
75 * The datasheets don't contain full programming information for the chip.
4de415d2 76 *
57785fef
HT
77 * PL-2303HX has the same features as PL-2303X (at least from the point of
78 * view of device driver) but is pin-to-pin compatible with PL-2303.
4de415d2
SW
79 *
80 * There are several differences between PL-2303 and PL-2303(H)X.
81 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
82 * different command for controlling CRTSCTS and needs special
83 * sequence of commands for initialization which aren't also
84 * documented in the datasheet.
984263bc
MD
85 */
86
87#include <sys/param.h>
88#include <sys/systm.h>
89#include <sys/kernel.h>
90#include <sys/malloc.h>
91#include <sys/bus.h>
92#include <sys/ioccom.h>
93#include <sys/fcntl.h>
94#include <sys/conf.h>
95#include <sys/tty.h>
96#include <sys/file.h>
984263bc 97#include <sys/select.h>
984263bc 98#include <sys/proc.h>
984263bc
MD
99#include <sys/poll.h>
100#include <sys/sysctl.h>
4de415d2 101#include <sys/taskqueue.h>
984263bc 102
1f2de5d4
MD
103#include <bus/usb/usb.h>
104#include <bus/usb/usbcdc.h>
984263bc 105
1f2de5d4
MD
106#include <bus/usb/usbdi.h>
107#include <bus/usb/usbdi_util.h>
1f2de5d4 108#include <bus/usb/usb_quirks.h>
984263bc 109
1f2de5d4 110#include "../ucom/ucomvar.h"
984263bc 111
62ade751 112SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
984263bc
MD
113#ifdef USB_DEBUG
114static int uplcomdebug = 0;
984263bc
MD
115SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
116 &uplcomdebug, 0, "uplcom debug level");
117
118#define DPRINTFN(n, x) do { \
119 if (uplcomdebug > (n)) \
fc1ef497 120 kprintf x; \
984263bc
MD
121 } while (0)
122#else
123#define DPRINTFN(n, x)
124#endif
125#define DPRINTF(x) DPRINTFN(0, x)
126
127#define UPLCOM_MODVER 1 /* module version */
128
129#define UPLCOM_CONFIG_INDEX 0
130#define UPLCOM_IFACE_INDEX 0
131#define UPLCOM_SECOND_IFACE_INDEX 1
132
62ade751 133#ifndef UPLCOM_INTR_INTERVAL
984263bc 134#define UPLCOM_INTR_INTERVAL 100 /* ms */
62ade751 135#endif
984263bc
MD
136
137#define UPLCOM_SET_REQUEST 0x01
138#define UPLCOM_SET_CRTSCTS 0x41
4de415d2
SW
139#define UPLCOM_SET_CRTSCTS_PL2303X 0x61
140#define RSAQ_STATUS_CTS 0x80
984263bc
MD
141#define RSAQ_STATUS_DSR 0x02
142#define RSAQ_STATUS_DCD 0x01
143
4de415d2
SW
144#define TYPE_PL2303 0
145#define TYPE_PL2303X 1
146
984263bc
MD
147struct uplcom_softc {
148 struct ucom_softc sc_ucom;
149
150 int sc_iface_number; /* interface number */
151
152 usbd_interface_handle sc_intr_iface; /* interrupt interface */
153 int sc_intr_number; /* interrupt number */
154 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
155 u_char *sc_intr_buf; /* interrupt buffer */
156 int sc_isize;
157
158 usb_cdc_line_state_t sc_line_state; /* current line state */
159 u_char sc_dtr; /* current DTR state */
160 u_char sc_rts; /* current RTS state */
161 u_char sc_status;
162
163 u_char sc_lsr; /* Local status register */
164 u_char sc_msr; /* uplcom status register */
4de415d2
SW
165
166 int sc_chiptype; /* Type of chip */
167
168 struct task sc_task;
984263bc
MD
169};
170
171/*
172 * These are the maximum number of bytes transferred per frame.
173 * The output buffer size cannot be increased due to the size encoding.
174 */
175#define UPLCOMIBUFSIZE 256
176#define UPLCOMOBUFSIZE 256
177
6ed427ca
HT
178static usbd_status uplcom_reset(struct uplcom_softc *);
179static usbd_status uplcom_set_line_coding(struct uplcom_softc *,
984263bc 180 usb_cdc_line_state_t *);
6ed427ca
HT
181static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
182static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
183
184static void uplcom_set(void *, int, int, int);
185static void uplcom_dtr(struct uplcom_softc *, int);
186static void uplcom_rts(struct uplcom_softc *, int);
187static void uplcom_break(struct uplcom_softc *, int);
188static void uplcom_set_line_state(struct uplcom_softc *);
189static void uplcom_get_status(void *, int, u_char *, u_char *);
4de415d2 190#if 0 /* TODO */
375a9af6 191static int uplcom_ioctl(void *, int, u_long, caddr_t, int, struct thread *);
984263bc 192#endif
6ed427ca
HT
193static int uplcom_param(void *, int, struct termios *);
194static int uplcom_open(void *, int);
195static void uplcom_close(void *, int);
196static void uplcom_notify(void *, int);
984263bc
MD
197
198struct ucom_callback uplcom_callback = {
199 uplcom_get_status,
200 uplcom_set,
201 uplcom_param,
202 NULL, /* uplcom_ioctl, TODO */
203 uplcom_open,
204 uplcom_close,
205 NULL,
206 NULL
207};
208
57785fef 209static const struct usb_devno uplcom_devs[] = {
3f56796e
HT
210 { USB_DEVICE(0x0413, 0x2101) }, /* Leadtek 9531 GPS */
211 { USB_DEVICE(0x0421, 0x1234) }, /* Nokia CA-42 USB data cable clones*/
212 { USB_DEVICE(0x04bb, 0x0a03) }, /* I/O DATA USB-RSAQ USB to serial */
213 { USB_DEVICE(0x04bb, 0x0a0e) }, /* I/O DATA USB-RSAQ5 USB to serial */
214 { USB_DEVICE(0x04bf, 0x0115) }, /* TDK USB-PDC adapter UPA9664 */
215 { USB_DEVICE(0x04bf, 0x0117) }, /* TDK USB-PHS adapter UHA6400 */
216 { USB_DEVICE(0x04da, 0x3900) }, /* Panasonic 50" touch panel */
217 { USB_DEVICE(0x04e8, 0x8001) }, /* Samsung I330 smartphone cradle */
218 { USB_DEVICE(0x050d, 0x0257) }, /* Belkin F5U257 USB to serial */
219 { USB_DEVICE(0x0547, 0x2008) }, /* Anchor serial */
220 { USB_DEVICE(0x0557, 0x2008) }, /* Aten UC232A USB to serial */
221 { USB_DEVICE(0x056e, 0x5003) }, /* ELECOM UC-SGT USB to serial */
222 { USB_DEVICE(0x056e, 0x5004) }, /* ELECOM UC-SGT USB to serial */
223 { USB_DEVICE(0x0584, 0xb000) }, /* RATOC REX-USB60 USB to serial */
224 { USB_DEVICE(0x058f, 0x9720) }, /* Alcor AU9720 USB to serial */
225 { USB_DEVICE(0x067b, 0x04bb) }, /* I/O DATA USB-RSAQ2 USB to serial */
226 { USB_DEVICE(0x067b, 0x1234) }, /* Unbranded DCU-11 clone */
227 { USB_DEVICE(0x067b, 0x2303) }, /* IOGEAR/ATEN UC-232A, ST Lab
228 USB-SERIAL-X etc */
229 { USB_DEVICE(0x067b, 0xaaa0) }, /* Microsoft OEM Pharos 360 GPS */
230 { USB_DEVICE(0x067b, 0xaaa2) }, /* I/O DATA USB-RSAQ3 USB to serial */
231 { USB_DEVICE(0x0731, 0x0528) }, /* Sony-Ericsson DCU-10/DCU-11 */
232 { USB_DEVICE(0x0731, 0x2003) }, /* Susteen Datapilot Universal-2 Phone
233 Cable */
234 { USB_DEVICE(0x0745, 0x0001) }, /* Syntech CPT-8001C barcode scanner
235 USB cradle */
236 { USB_DEVICE(0x079b, 0x0027) }, /* Sagem USB data cables */
237 { USB_DEVICE(0x0833, 0x012e) }, /* SOURCENEXT KeikaiDenwa 8 with
238 charger */
239 { USB_DEVICE(0x0833, 0x039f) }, /* SOURCENEXT KeikaiDenwa 8 */
240 { USB_DEVICE(0x0b41, 0x0011) }, /* HAL Corporation Crossam2+USB */
241 { USB_DEVICE(0x0b8c, 0x2303) }, /* Smart Technologies USB to serial */
242 { USB_DEVICE(0x0df7, 0x0620) }, /* Mobile Action MA-620 IrDA */
243 { USB_DEVICE(0x0e55, 0x110b) }, /* Speed Dragon MS3303H */
244 { USB_DEVICE(0x0eba, 0x1080) }, /* Itegno GSM/GPRS modem */
245 { USB_DEVICE(0x0eba, 0x2080) }, /* Itegno CDMA 1x card */
246 { USB_DEVICE(0x10b5, 0xac70) }, /* PLX CA-42 USB data cable clone */
247 { USB_DEVICE(0x11f5, 0x0001) }, /* Siemens SX1 cellphone */
248 { USB_DEVICE(0x11f5, 0x0003) }, /* Siemens x65 series cellphones */
249 { USB_DEVICE(0x11f5, 0x0004) }, /* Siemens x75 series cellphones */
250 { USB_DEVICE(0x11f6, 0x2001) }, /* Willcom W-SIM */
251 { USB_DEVICE(0x11f7, 0x02df) }, /* Alcatel One Touch 535/735 phones */
3f56796e
HT
252 { USB_DEVICE(0x1453, 0x4026) }, /* RADIOSHACK USB cable */
253 { USB_DEVICE(0x2478, 0x2008) }, /* Tripp-Lite U209-000-R */
254 { USB_DEVICE(0x6189, 0x2068) }, /* Sitecom USB to serial cable */
984263bc
MD
255};
256
6ed427ca
HT
257static device_probe_t uplcom_match;
258static device_attach_t uplcom_attach;
259static device_detach_t uplcom_detach;
984263bc 260
6ed427ca 261static device_method_t uplcom_methods[] = {
984263bc
MD
262 /* Device interface */
263 DEVMETHOD(device_probe, uplcom_match),
264 DEVMETHOD(device_attach, uplcom_attach),
265 DEVMETHOD(device_detach, uplcom_detach),
266 { 0, 0 }
267};
268
6ed427ca 269static driver_t uplcom_driver = {
62ade751 270 "ucom",
984263bc
MD
271 uplcom_methods,
272 sizeof (struct uplcom_softc)
273};
274
275DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, 0);
276MODULE_DEPEND(uplcom, usb, 1, 1, 1);
277MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
278MODULE_VERSION(uplcom, UPLCOM_MODVER);
279
4de415d2 280static int uplcominterval = UPLCOM_INTR_INTERVAL;
62ade751
MD
281
282static int
283sysctl_hw_usb_uplcom_interval(SYSCTL_HANDLER_ARGS)
284{
285 int err, val;
286
287 val = uplcominterval;
288 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
289 if (err != 0 || req->newptr == NULL)
290 return (err);
291 if (0 < val && val <= 1000)
292 uplcominterval = val;
293 else
294 err = EINVAL;
295
296 return (err);
297}
298
299SYSCTL_PROC(_hw_usb_uplcom, OID_AUTO, interval, CTLTYPE_INT | CTLFLAG_RW,
4de415d2
SW
300 0, sizeof(int), sysctl_hw_usb_uplcom_interval,
301 "I", "uplcom interrupt pipe interval");
62ade751 302
e785a5d9
HT
303static int
304uplcom_match(device_t self)
984263bc 305{
e785a5d9 306 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
307
308 if (uaa->iface != NULL)
309 return (UMATCH_NONE);
310
57785fef
HT
311 return (usb_lookup(uplcom_devs, uaa->vendor, uaa->product) != NULL ?
312 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
984263bc
MD
313}
314
e785a5d9
HT
315static int
316uplcom_attach(device_t self)
984263bc 317{
e785a5d9
HT
318 struct uplcom_softc *sc = device_get_softc(self);
319 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 320 usbd_device_handle dev = uaa->device;
57785fef 321 usb_device_descriptor_t *dd;
984263bc
MD
322 struct ucom_softc *ucom;
323 usb_config_descriptor_t *cdesc;
324 usb_interface_descriptor_t *id;
325 usb_endpoint_descriptor_t *ed;
984263bc
MD
326 usbd_status err;
327 int i;
328
984263bc 329 ucom = &sc->sc_ucom;
984263bc
MD
330 bzero(sc, sizeof (struct uplcom_softc));
331
984263bc 332 ucom->sc_dev = self;
984263bc
MD
333 ucom->sc_udev = dev;
334 ucom->sc_iface = uaa->iface;
335
984263bc
MD
336 DPRINTF(("uplcom attach: sc = %p\n", sc));
337
57785fef
HT
338 dd = usbd_get_device_descriptor(uaa->device);
339
340 if (!dd)
341 goto error;
4de415d2
SW
342
343 /*
57785fef
HT
344 * Determine chip type with algorithm sequence taken from the
345 * Linux driver as I'm not aware of any better method. Device
346 * release number in chip could be (and in fact is in many cases)
347 * replaced by the contents of external EEPROM etc.
4de415d2 348 */
57785fef
HT
349 else if (dd->bDeviceClass == 0x02)
350 sc->sc_chiptype = TYPE_PL2303;
351 else if (dd->bMaxPacketSize == 0x40)
352 sc->sc_chiptype = TYPE_PL2303X;
353 else
354 sc->sc_chiptype = TYPE_PL2303;
4de415d2
SW
355
356#ifdef USB_DEBUG
357 /* print the chip type */
358 if (sc->sc_chiptype == TYPE_PL2303X) {
359 DPRINTF(("uplcom_attach: chiptype 2303X\n"));
360 } else {
361 DPRINTF(("uplcom_attach: chiptype 2303\n"));
362 }
363#endif
364
1550dfd9 365 /* initialize endpoints */
984263bc
MD
366 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
367 sc->sc_intr_number = -1;
368 sc->sc_intr_pipe = NULL;
369
370 /* Move the device into the configured state. */
371 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
372 if (err) {
71f2bf84
HT
373 device_printf(ucom->sc_dev, "failed to set configuration: %s\n",
374 usbd_errstr(err));
984263bc
MD
375 ucom->sc_dying = 1;
376 goto error;
377 }
378
379 /* get the config descriptor */
380 cdesc = usbd_get_config_descriptor(ucom->sc_udev);
381
382 if (cdesc == NULL) {
71f2bf84
HT
383 device_printf(ucom->sc_dev, "failed to get configuration "
384 "descriptor\n");
984263bc
MD
385 ucom->sc_dying = 1;
386 goto error;
387 }
388
389 /* get the (first/common) interface */
1550dfd9 390 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
984263bc
MD
391 &ucom->sc_iface);
392 if (err) {
71f2bf84
HT
393 device_printf(ucom->sc_dev, "failed to get interface: %s\n",
394 usbd_errstr(err));
984263bc
MD
395 ucom->sc_dying = 1;
396 goto error;
397 }
398
399 /* Find the interrupt endpoints */
400
401 id = usbd_get_interface_descriptor(ucom->sc_iface);
402 sc->sc_iface_number = id->bInterfaceNumber;
403
404 for (i = 0; i < id->bNumEndpoints; i++) {
405 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
406 if (ed == NULL) {
71f2bf84
HT
407 device_printf(ucom->sc_dev, "no endpoint descriptor "
408 "for %d\n", i);
984263bc
MD
409 ucom->sc_dying = 1;
410 goto error;
411 }
412
413 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
414 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
415 sc->sc_intr_number = ed->bEndpointAddress;
416 sc->sc_isize = UGETW(ed->wMaxPacketSize);
1550dfd9 417 }
984263bc
MD
418 }
419
420 if (sc->sc_intr_number == -1) {
71f2bf84 421 device_printf(ucom->sc_dev, "could not find interrupt in\n");
984263bc
MD
422 ucom->sc_dying = 1;
423 goto error;
424 }
425
426 /* keep interface for interrupt */
427 sc->sc_intr_iface = ucom->sc_iface;
428
429 /*
430 * USB-RSAQ1 has two interface
431 *
432 * USB-RSAQ1 | USB-RSAQ2
433 * -----------------+-----------------
434 * Interface 0 |Interface 0
435 * Interrupt(0x81) | Interrupt(0x81)
436 * -----------------+ BulkIN(0x02)
437 * Interface 1 | BulkOUT(0x83)
1550dfd9 438 * BulkIN(0x02) |
984263bc
MD
439 * BulkOUT(0x83) |
440 */
441 if (cdesc->bNumInterface == 2) {
1550dfd9 442 err = usbd_device2interface_handle(dev,
984263bc
MD
443 UPLCOM_SECOND_IFACE_INDEX,
444 &ucom->sc_iface);
445 if (err) {
71f2bf84
HT
446 device_printf(ucom->sc_dev, "failed to get second "
447 "interface: %s\n", usbd_errstr(err));
984263bc
MD
448 ucom->sc_dying = 1;
449 goto error;
450 }
1550dfd9 451 }
984263bc
MD
452
453 /* Find the bulk{in,out} endpoints */
454
455 id = usbd_get_interface_descriptor(ucom->sc_iface);
456 sc->sc_iface_number = id->bInterfaceNumber;
457
458 for (i = 0; i < id->bNumEndpoints; i++) {
459 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
460 if (ed == NULL) {
71f2bf84
HT
461 device_printf(ucom->sc_dev, "no endpoint descriptor "
462 "for %d\n", i);
984263bc
MD
463 ucom->sc_dying = 1;
464 goto error;
465 }
466
467 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
468 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
469 ucom->sc_bulkin_no = ed->bEndpointAddress;
470 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
471 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
472 ucom->sc_bulkout_no = ed->bEndpointAddress;
473 }
474 }
475
476 if (ucom->sc_bulkin_no == -1) {
71f2bf84 477 device_printf(ucom->sc_dev, "could not find data bulk in\n");
984263bc
MD
478 ucom->sc_dying = 1;
479 goto error;
480 }
481
482 if (ucom->sc_bulkout_no == -1) {
71f2bf84 483 device_printf(ucom->sc_dev, "could not find data bulk out\n");
984263bc
MD
484 ucom->sc_dying = 1;
485 goto error;
486 }
487
488 sc->sc_dtr = sc->sc_rts = -1;
489 ucom->sc_parent = sc;
490 ucom->sc_portno = UCOM_UNK_PORTNO;
491 /* bulkin, bulkout set above */
492 ucom->sc_ibufsize = UPLCOMIBUFSIZE;
493 ucom->sc_obufsize = UPLCOMOBUFSIZE;
494 ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
495 ucom->sc_opkthdrlen = 0;
496 ucom->sc_callback = &uplcom_callback;
497
498 err = uplcom_reset(sc);
499
500 if (err) {
71f2bf84
HT
501 device_printf(ucom->sc_dev, "reset failed: %s\n",
502 usbd_errstr(err));
984263bc
MD
503 ucom->sc_dying = 1;
504 goto error;
505 }
506
507 DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
508 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
509
4de415d2 510 TASK_INIT(&sc->sc_task, 0, uplcom_notify, sc);
984263bc
MD
511 ucom_attach(&sc->sc_ucom);
512
e785a5d9 513 return 0;
984263bc
MD
514
515error:
e785a5d9 516 return ENXIO;
984263bc
MD
517}
518
e785a5d9
HT
519static int
520uplcom_detach(device_t self)
984263bc 521{
e785a5d9 522 struct uplcom_softc *sc = device_get_softc(self);
984263bc
MD
523 int rv = 0;
524
525 DPRINTF(("uplcom_detach: sc = %p\n", sc));
526
527 if (sc->sc_intr_pipe != NULL) {
528 usbd_abort_pipe(sc->sc_intr_pipe);
529 usbd_close_pipe(sc->sc_intr_pipe);
efda3bd0 530 kfree(sc->sc_intr_buf, M_USBDEV);
984263bc
MD
531 sc->sc_intr_pipe = NULL;
532 }
533
534 sc->sc_ucom.sc_dying = 1;
535
536 rv = ucom_detach(&sc->sc_ucom);
537
538 return (rv);
539}
540
6ed427ca 541static usbd_status
984263bc
MD
542uplcom_reset(struct uplcom_softc *sc)
543{
544 usb_device_request_t req;
545 usbd_status err;
546
547 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
548 req.bRequest = UPLCOM_SET_REQUEST;
549 USETW(req.wValue, 0);
550 USETW(req.wIndex, sc->sc_iface_number);
1550dfd9 551 USETW(req.wLength, 0);
984263bc 552
1550dfd9 553 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
984263bc 554 if (err) {
71f2bf84
HT
555 device_printf(sc->sc_ucom.sc_dev, "uplcom_reset: %s\n",
556 usbd_errstr(err));
984263bc
MD
557 return (EIO);
558 }
559
560 return (0);
561}
562
4de415d2
SW
563struct pl2303x_init {
564 uint8_t req_type;
565 uint8_t request;
566 uint16_t value;
567 uint16_t index;
568 uint16_t length;
569};
570
571static const struct pl2303x_init pl2303x[] = {
572 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
573 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
574 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
575 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
576 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
577 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
578 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
579 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
580 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
581 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
582 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
583};
584#define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
585
586static usbd_status
587uplcom_pl2303x_init(struct uplcom_softc *sc)
588{
589 usb_device_request_t req;
590 usbd_status err;
591 int i;
592
593 for (i = 0; i < N_PL2302X_INIT; i++) {
594 req.bmRequestType = pl2303x[i].req_type;
595 req.bRequest = pl2303x[i].request;
596 USETW(req.wValue, pl2303x[i].value);
597 USETW(req.wIndex, pl2303x[i].index);
598 USETW(req.wLength, pl2303x[i].length);
599
600 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
601 if (err) {
71f2bf84
HT
602 device_printf(sc->sc_ucom.sc_dev,
603 "uplcom_pl2303x_init: %d: %s\n", i,
604 usbd_errstr(err));
4de415d2
SW
605 return (EIO);
606 }
607 }
608
609 return (0);
610}
611
612static void
984263bc
MD
613uplcom_set_line_state(struct uplcom_softc *sc)
614{
615 usb_device_request_t req;
616 int ls;
617 usbd_status err;
618
619 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
620 (sc->sc_rts ? UCDC_LINE_RTS : 0);
621 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
622 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
623 USETW(req.wValue, ls);
624 USETW(req.wIndex, sc->sc_iface_number);
625 USETW(req.wLength, 0);
626
627 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
628 if (err)
71f2bf84
HT
629 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_line_status: "
630 "%s\n", usbd_errstr(err));
984263bc
MD
631}
632
6ed427ca 633static void
984263bc
MD
634uplcom_set(void *addr, int portno, int reg, int onoff)
635{
636 struct uplcom_softc *sc = addr;
637
638 switch (reg) {
639 case UCOM_SET_DTR:
640 uplcom_dtr(sc, onoff);
641 break;
642 case UCOM_SET_RTS:
643 uplcom_rts(sc, onoff);
644 break;
645 case UCOM_SET_BREAK:
646 uplcom_break(sc, onoff);
647 break;
648 default:
649 break;
650 }
651}
652
6ed427ca 653static void
984263bc
MD
654uplcom_dtr(struct uplcom_softc *sc, int onoff)
655{
656 DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
657
658 if (sc->sc_dtr == onoff)
659 return;
660 sc->sc_dtr = onoff;
661
662 uplcom_set_line_state(sc);
663}
664
6ed427ca 665static void
984263bc
MD
666uplcom_rts(struct uplcom_softc *sc, int onoff)
667{
668 DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
669
670 if (sc->sc_rts == onoff)
671 return;
672 sc->sc_rts = onoff;
673
674 uplcom_set_line_state(sc);
675}
676
6ed427ca 677static void
984263bc
MD
678uplcom_break(struct uplcom_softc *sc, int onoff)
679{
680 usb_device_request_t req;
681 usbd_status err;
682
683 DPRINTF(("uplcom_break: onoff = %d\n", onoff));
684
685 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
686 req.bRequest = UCDC_SEND_BREAK;
687 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
688 USETW(req.wIndex, sc->sc_iface_number);
689 USETW(req.wLength, 0);
690
691 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
692 if (err)
71f2bf84
HT
693 device_printf(sc->sc_ucom.sc_dev, "uplcom_break: %s\n",
694 usbd_errstr(err));
984263bc
MD
695}
696
6ed427ca 697static usbd_status
984263bc
MD
698uplcom_set_crtscts(struct uplcom_softc *sc)
699{
700 usb_device_request_t req;
701 usbd_status err;
702
703 DPRINTF(("uplcom_set_crtscts: on\n"));
704
705 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
706 req.bRequest = UPLCOM_SET_REQUEST;
707 USETW(req.wValue, 0);
4de415d2
SW
708 if (sc->sc_chiptype == TYPE_PL2303X)
709 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
710 else
711 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
984263bc
MD
712 USETW(req.wLength, 0);
713
714 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
715 if (err) {
71f2bf84
HT
716 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_crtscts: %s\n",
717 usbd_errstr(err));
984263bc
MD
718 return (err);
719 }
720
721 return (USBD_NORMAL_COMPLETION);
722}
723
6ed427ca 724static usbd_status
984263bc
MD
725uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
726{
727 usb_device_request_t req;
728 usbd_status err;
729
730 DPRINTF((
731"uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
732 UGETDW(state->dwDTERate), state->bCharFormat,
733 state->bParityType, state->bDataBits));
734
735 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
736 DPRINTF(("uplcom_set_line_coding: already set\n"));
737 return (USBD_NORMAL_COMPLETION);
738 }
739
740 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
741 req.bRequest = UCDC_SET_LINE_CODING;
742 USETW(req.wValue, 0);
743 USETW(req.wIndex, sc->sc_iface_number);
744 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
745
746 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
747 if (err) {
71f2bf84
HT
748 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_line_coding: "
749 "%s\n", usbd_errstr(err));
984263bc
MD
750 return (err);
751 }
752
753 sc->sc_line_state = *state;
754
755 return (USBD_NORMAL_COMPLETION);
756}
757
4de415d2
SW
758static const int uplcom_rates[] = {
759 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
760 19200, 28800, 38400, 57600, 115200,
761 /*
762 * Higher speeds are probably possible. PL2303X supports up to
763 * 6Mb and can set any rate
764 */
765 230400, 460800, 614400, 921600, 1228800
766};
767#define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
768
769static int
984263bc
MD
770uplcom_param(void *addr, int portno, struct termios *t)
771{
772 struct uplcom_softc *sc = addr;
773 usbd_status err;
774 usb_cdc_line_state_t ls;
4de415d2 775 int i;
984263bc
MD
776
777 DPRINTF(("uplcom_param: sc = %p\n", sc));
778
4de415d2
SW
779 /* Check requested baud rate */
780 for (i = 0; i < N_UPLCOM_RATES; i++)
781 if (uplcom_rates[i] == t->c_ospeed)
782 break;
783 if (i == N_UPLCOM_RATES) {
784 DPRINTF(("uplcom_param: bad baud rate (%d)\n", t->c_ospeed));
785 return (EIO);
786 }
787
984263bc
MD
788 USETDW(ls.dwDTERate, t->c_ospeed);
789 if (ISSET(t->c_cflag, CSTOPB))
790 ls.bCharFormat = UCDC_STOP_BIT_2;
791 else
792 ls.bCharFormat = UCDC_STOP_BIT_1;
793 if (ISSET(t->c_cflag, PARENB)) {
794 if (ISSET(t->c_cflag, PARODD))
795 ls.bParityType = UCDC_PARITY_ODD;
796 else
797 ls.bParityType = UCDC_PARITY_EVEN;
798 } else
799 ls.bParityType = UCDC_PARITY_NONE;
800 switch (ISSET(t->c_cflag, CSIZE)) {
801 case CS5:
802 ls.bDataBits = 5;
803 break;
804 case CS6:
805 ls.bDataBits = 6;
806 break;
807 case CS7:
808 ls.bDataBits = 7;
809 break;
810 case CS8:
811 ls.bDataBits = 8;
812 break;
813 }
814
815 err = uplcom_set_line_coding(sc, &ls);
816 if (err)
817 return (EIO);
818
819 if (ISSET(t->c_cflag, CRTSCTS)) {
820 err = uplcom_set_crtscts(sc);
821 if (err)
822 return (EIO);
823 }
824
825 return (0);
826}
827
6ed427ca 828static int
984263bc
MD
829uplcom_open(void *addr, int portno)
830{
831 struct uplcom_softc *sc = addr;
832 int err;
1550dfd9 833
984263bc
MD
834 if (sc->sc_ucom.sc_dying)
835 return (ENXIO);
836
837 DPRINTF(("uplcom_open: sc = %p\n", sc));
838
839 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
840 sc->sc_status = 0; /* clear status bit */
efda3bd0 841 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
984263bc
MD
842 err = usbd_open_pipe_intr(sc->sc_intr_iface,
843 sc->sc_intr_number,
844 USBD_SHORT_XFER_OK,
845 &sc->sc_intr_pipe,
846 sc,
847 sc->sc_intr_buf,
848 sc->sc_isize,
849 uplcom_intr,
62ade751 850 uplcominterval);
984263bc 851 if (err) {
71f2bf84
HT
852 device_printf(sc->sc_ucom.sc_dev, "cannot open "
853 "interrupt pipe (addr %d)\n",
854 sc->sc_intr_number);
984263bc
MD
855 return (EIO);
856 }
857 }
858
4de415d2
SW
859 if (sc->sc_chiptype == TYPE_PL2303X)
860 return (uplcom_pl2303x_init(sc));
861
984263bc
MD
862 return (0);
863}
864
6ed427ca 865static void
1550dfd9 866uplcom_close(void *addr, int portno)
984263bc
MD
867{
868 struct uplcom_softc *sc = addr;
869 int err;
870
871 if (sc->sc_ucom.sc_dying)
872 return;
873
874 DPRINTF(("uplcom_close: close\n"));
875
876 if (sc->sc_intr_pipe != NULL) {
877 err = usbd_abort_pipe(sc->sc_intr_pipe);
878 if (err)
71f2bf84
HT
879 device_printf(sc->sc_ucom.sc_dev, "abort interrupt "
880 "pipe failed: %s\n", usbd_errstr(err));
984263bc
MD
881 err = usbd_close_pipe(sc->sc_intr_pipe);
882 if (err)
71f2bf84
HT
883 device_printf(sc->sc_ucom.sc_dev, "close interrupt "
884 "pipe failed: %s\n", usbd_errstr(err));
efda3bd0 885 kfree(sc->sc_intr_buf, M_USBDEV);
984263bc
MD
886 sc->sc_intr_pipe = NULL;
887 }
888}
889
6ed427ca 890static void
984263bc
MD
891uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
892{
893 struct uplcom_softc *sc = priv;
894 u_char *buf = sc->sc_intr_buf;
895 u_char pstatus;
896
897 if (sc->sc_ucom.sc_dying)
898 return;
899
900 if (status != USBD_NORMAL_COMPLETION) {
901 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
902 return;
903
904 DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
6ed427ca 905 device_get_nameunit(sc->sc_ucom.sc_dev),
984263bc
MD
906 usbd_errstr(status)));
907 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
908 return;
909 }
910
911 DPRINTF(("%s: uplcom status = %02x\n",
6ed427ca 912 device_get_nameunit(sc->sc_ucom.sc_dev), buf[8]));
984263bc
MD
913
914 sc->sc_lsr = sc->sc_msr = 0;
915 pstatus = buf[8];
4de415d2
SW
916 if (ISSET(pstatus, RSAQ_STATUS_CTS))
917 sc->sc_msr |= UMSR_CTS;
918 else
919 sc->sc_msr &= ~UMSR_CTS;
984263bc
MD
920 if (ISSET(pstatus, RSAQ_STATUS_DSR))
921 sc->sc_msr |= UMSR_DSR;
4de415d2
SW
922 else
923 sc->sc_msr &= ~UMSR_DSR;
984263bc
MD
924 if (ISSET(pstatus, RSAQ_STATUS_DCD))
925 sc->sc_msr |= UMSR_DCD;
4de415d2
SW
926 else
927 sc->sc_msr &= ~UMSR_DCD;
928
929 /* Deferred notifying to the ucom layer */
930 taskqueue_enqueue(taskqueue_swi, &sc->sc_task);
931}
932
933static void
934uplcom_notify(void *arg, int count)
935{
936 struct uplcom_softc *sc;
937
938 sc = (struct uplcom_softc *)arg;
939 if (sc->sc_ucom.sc_dying)
940 return;
984263bc
MD
941 ucom_status_change(&sc->sc_ucom);
942}
943
6ed427ca 944static void
984263bc
MD
945uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
946{
947 struct uplcom_softc *sc = addr;
948
949 DPRINTF(("uplcom_get_status:\n"));
950
951 if (lsr != NULL)
952 *lsr = sc->sc_lsr;
953 if (msr != NULL)
954 *msr = sc->sc_msr;
955}
956
4de415d2 957#if 0 /* TODO */
6ed427ca 958static int
984263bc 959uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
375a9af6 960 struct thread *p)
984263bc
MD
961{
962 struct uplcom_softc *sc = addr;
963 int error = 0;
964
965 if (sc->sc_ucom.sc_dying)
966 return (EIO);
967
968 DPRINTF(("uplcom_ioctl: cmd = 0x%08lx\n", cmd));
969
970 switch (cmd) {
971 case TIOCNOTTY:
972 case TIOCMGET:
973 case TIOCMSET:
974 case USB_GET_CM_OVER_DATA:
975 case USB_SET_CM_OVER_DATA:
976 break;
977
978 default:
979 DPRINTF(("uplcom_ioctl: unknown\n"));
980 error = ENOTTY;
981 break;
982 }
983
984 return (error);
985}
986#endif