uplcom.c revision 1.10 1 /* $NetBSD: uplcom.c,v 1.10 2001/01/28 03:44:46 ichiro Exp $ */
2 /*
3 * Copyright (c) 2001 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by FUKUHARA ichiro (ichiro (at) ichiro.org).
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Simple datasheet
40 * http://www.prolific.com.tw/download/DataSheet/pl2303_ds11.PDF
41 * http://www.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
42 * (english)
43 *
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/ioctl.h>
50 #include <sys/conf.h>
51 #include <sys/tty.h>
52 #include <sys/file.h>
53 #include <sys/select.h>
54 #include <sys/proc.h>
55 #include <sys/vnode.h>
56 #include <sys/device.h>
57 #include <sys/poll.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbcdc.h>
61
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 #include <dev/usb/usb_quirks.h>
66
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/ucomvar.h>
69
70 #ifdef UPLCOM_DEBUG
71 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
72 int uplcomdebug = 0xff;
73 #else
74 #define DPRINTFN(n, x)
75 #endif
76 #define DPRINTF(x) DPRINTFN(0, x)
77
78 #define UPLCOM_CONFIG_INDEX 0
79 #define UPLCOM_IFACE_INDEX 0
80 #define UPLCOM_SECOND_IFACE_INDEX 1
81 #define UPLCOM_RESET 0
82
83 struct uplcom_softc {
84 USBBASEDEVICE sc_dev; /* base device */
85 usbd_device_handle sc_udev; /* USB device */
86 usbd_interface_handle sc_iface; /* first interface */
87 usbd_interface_handle sc_sec_iface; /* second interface */
88 int sc_iface_number; /* interface number */
89
90 usb_cdc_line_state_t sc_line_state; /* current line state */
91 u_char sc_dtr; /* current DTR state */
92 u_char sc_rts; /* current RTS state */
93
94 device_ptr_t sc_subdev;
95
96 u_char sc_dying;
97 };
98
99 /*
100 * These are the maximum number of bytes transferred per frame.
101 * The output buffer size cannot be increased due to the size encoding.
102 */
103 #define UPLCOMIBUFSIZE 256
104 #define UPLCOMOBUFSIZE 256
105
106 Static usbd_status uplcom_init(struct uplcom_softc *);
107 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
108 usb_cdc_line_state_t *state);
109
110 Static void uplcom_set(void *, int, int, int);
111 Static void uplcom_dtr(struct uplcom_softc *, int);
112 Static void uplcom_rts(struct uplcom_softc *, int);
113 Static void uplcom_break(struct uplcom_softc *, int);
114 Static void uplcom_set_line_state(struct uplcom_softc *);
115 Static int uplcom_param(void *, int, struct termios *);
116
117 struct ucom_methods uplcom_methods = {
118 NULL,
119 uplcom_set,
120 uplcom_param,
121 NULL,
122 NULL,
123 NULL,
124 NULL,
125 NULL,
126 };
127
128 static const struct uplcom_product {
129 uint16_t vendor;
130 uint16_t product;
131 } uplcom_products [] = {
132 /* I/O DATA USB-RSAQ2 */
133 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
134 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
135
136 { 0, 0 }
137 };
138
139 USB_DECLARE_DRIVER(uplcom);
140
141 USB_MATCH(uplcom)
142 {
143 USB_MATCH_START(uplcom, uaa);
144 int i;
145
146 if (uaa->iface != NULL)
147 return (UMATCH_NONE);
148
149 for (i = 0; uplcom_products[i].vendor != 0; i++) {
150 if (uplcom_products[i].vendor == uaa->vendor &&
151 uplcom_products[i].product == uaa->product) {
152 return (UMATCH_VENDOR_PRODUCT);
153 }
154 }
155 return (UMATCH_NONE);
156 }
157
158 USB_ATTACH(uplcom)
159 {
160 USB_ATTACH_START(uplcom, sc, uaa);
161 usbd_device_handle dev = uaa->device;
162 usbd_interface_handle iface;
163 usb_config_descriptor_t *cdesc;
164 usb_interface_descriptor_t *id;
165 usb_endpoint_descriptor_t *ed;
166
167 char devinfo[1024];
168 char *devname = USBDEVNAME(sc->sc_dev);
169 usbd_status err;
170 int i;
171 struct ucom_attach_args uca;
172
173 usbd_devinfo(dev, 0, devinfo);
174 USB_ATTACH_SETUP;
175 printf("%s: %s\n", devname, devinfo);
176
177 sc->sc_udev = dev;
178
179 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
180
181 /* Move the device into the configured state. */
182 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
183 if (err) {
184 printf("\n%s: failed to set configuration, err=%s\n",
185 devname, usbd_errstr(err));
186 USB_ATTACH_ERROR_RETURN;
187 }
188
189 /* get the config descriptor */
190 cdesc = usbd_get_config_descriptor(sc->sc_udev);
191
192 if (cdesc == NULL) {
193 printf("%s: failed to get configuration descriptor\n",
194 USBDEVNAME(sc->sc_dev));
195 USB_ATTACH_ERROR_RETURN;
196 }
197
198 /* get the (first) interface */
199 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
200 &sc->sc_iface);
201 if (err) {
202 printf("\n%s: failed to get interface, err=%s\n",
203 devname, usbd_errstr(err));
204 USB_ATTACH_ERROR_RETURN;
205 }
206 /*
207 * check second of interface
208 * USB-RSAQ has two interface
209 */
210 if (cdesc->bNumInterface == 2) {
211 err = usbd_device2interface_handle(dev,
212 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_sec_iface);
213 if (err) {
214 printf("\n%s: failed to get second interface, err=%s\n",
215 devname, usbd_errstr(err));
216 USB_ATTACH_ERROR_RETURN;
217 }
218 iface = sc->sc_sec_iface;
219 } else
220 iface = sc->sc_iface;
221
222 /* Find the bulk endpoints */
223
224 id = usbd_get_interface_descriptor(iface);
225 sc->sc_iface_number = id->bInterfaceNumber;
226
227 uca.bulkin = uca.bulkout = -1;
228 for (i = 0; i < id->bNumEndpoints; i++) {
229 ed = usbd_interface2endpoint_descriptor(iface, i);
230 if (ed == NULL) {
231 printf("%s: no endpoint descriptor for %d\n",
232 USBDEVNAME(sc->sc_dev), i);
233 USB_ATTACH_ERROR_RETURN;
234 }
235
236 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
237 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
238 uca.bulkin = ed->bEndpointAddress;
239 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
240 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
241 DPRINTF(("interrupt endpoint addr = 0x%x\n",
242 ed->bEndpointAddress));
243 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
244 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
245 uca.bulkout = ed->bEndpointAddress;
246 }
247 }
248
249 if (uca.bulkin == -1) {
250 printf("%s: Could not find data bulk in\n",
251 USBDEVNAME(sc->sc_dev));
252 USB_ATTACH_ERROR_RETURN;
253 }
254
255 if (uca.bulkout == -1) {
256 printf("%s: Could not find data bulk out\n",
257 USBDEVNAME(sc->sc_dev));
258 USB_ATTACH_ERROR_RETURN;
259 }
260
261 sc->sc_dtr = -1;
262 uca.portno = UCOM_UNK_PORTNO;
263 /* bulkin, bulkout set above */
264 uca.ibufsize = UPLCOMIBUFSIZE;
265 uca.obufsize = UPLCOMOBUFSIZE;
266 uca.ibufsizepad = UPLCOMIBUFSIZE;
267 uca.opkthdrlen = 0;
268 uca.device = dev;
269 uca.iface = iface;
270 uca.methods = &uplcom_methods;
271 uca.arg = sc;
272 uca.info = NULL;
273
274 err = uplcom_init(sc);
275 if (err) {
276 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
277 usbd_errstr(err));
278 USB_ATTACH_ERROR_RETURN;
279 }
280
281 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
282 USBDEV(sc->sc_dev));
283
284 DPRINTF(("uplcom: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
285 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
286
287 USB_ATTACH_SUCCESS_RETURN;
288 }
289
290 USB_DETACH(uplcom)
291 {
292 USB_DETACH_START(uplcom, sc);
293 int rv = 0;
294
295 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
296 sc->sc_dying = 1;
297 if (sc->sc_subdev != NULL) {
298 rv = config_detach(sc->sc_subdev, flags);
299 sc->sc_subdev = NULL;
300 }
301
302 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
303 USBDEV(sc->sc_dev));
304
305 return (rv);
306 }
307
308 int
309 uplcom_activate(device_ptr_t self, enum devact act)
310 {
311 struct uplcom_softc *sc = (struct uplcom_softc *)self;
312 int rv = 0;
313
314 switch (act) {
315 case DVACT_ACTIVATE:
316 return (EOPNOTSUPP);
317 break;
318
319 case DVACT_DEACTIVATE:
320 if (sc->sc_subdev != NULL)
321 rv = config_deactivate(sc->sc_subdev);
322 sc->sc_dying = 1;
323 break;
324 }
325 return (rv);
326 }
327
328
329 usbd_status
330 uplcom_init(struct uplcom_softc *sc)
331 {
332 usb_device_request_t req;
333 usbd_status err;
334
335 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
336 req.bRequest = UPLCOM_RESET;
337 USETW(req.wValue, UPLCOM_RESET);
338 USETW(req.wIndex, sc->sc_iface_number);
339 USETW(req.wLength, 0);
340
341 err = usbd_do_request(sc->sc_udev, &req, 0);
342 if (err)
343 return (EIO);
344
345 return (0);
346 }
347
348 void
349 uplcom_set_line_state(struct uplcom_softc *sc)
350 {
351 usb_device_request_t req;
352 int ls;
353
354 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
355 (sc->sc_rts ? UCDC_LINE_RTS : 0);
356 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
357 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
358 USETW(req.wValue, ls);
359 USETW(req.wIndex, sc->sc_iface_number);
360 USETW(req.wLength, 0);
361
362 (void)usbd_do_request(sc->sc_udev, &req, 0);
363
364 }
365
366 void
367 uplcom_set(void *addr, int portno, int reg, int onoff)
368 {
369 struct uplcom_softc *sc = addr;
370
371 switch (reg) {
372 case UCOM_SET_DTR:
373 uplcom_dtr(sc, onoff);
374 break;
375 case UCOM_SET_RTS:
376 uplcom_rts(sc, onoff);
377 break;
378 case UCOM_SET_BREAK:
379 uplcom_break(sc, onoff);
380 break;
381 default:
382 break;
383 }
384 }
385
386 void
387 uplcom_dtr(struct uplcom_softc *sc, int onoff)
388 {
389
390 DPRINTF(("uplcom: onoff=%d\n", onoff));
391
392 if (sc->sc_dtr == onoff)
393 return;
394 sc->sc_dtr = onoff;
395
396 uplcom_set_line_state(sc);
397 }
398
399 void
400 uplcom_rts(struct uplcom_softc *sc, int onoff)
401 {
402 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
403
404 if (sc->sc_rts == onoff)
405 return;
406 sc->sc_rts = onoff;
407
408 uplcom_set_line_state(sc);
409 }
410
411 void
412 uplcom_break(struct uplcom_softc *sc, int onoff)
413 {
414 usb_device_request_t req;
415
416 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
417
418 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
419 req.bRequest = UCDC_SEND_BREAK;
420 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
421 USETW(req.wIndex, sc->sc_iface_number);
422 USETW(req.wLength, 0);
423
424 (void)usbd_do_request(sc->sc_udev, &req, 0);
425 }
426
427 usbd_status
428 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
429 {
430 usb_device_request_t req;
431 usbd_status err;
432
433 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
434 UGETDW(state->dwDTERate), state->bCharFormat,
435 state->bParityType, state->bDataBits));
436
437 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
438 DPRINTF(("uplcom_set_line_coding: already set\n"));
439 return (USBD_NORMAL_COMPLETION);
440 }
441
442 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
443 req.bRequest = UCDC_SET_LINE_CODING;
444 USETW(req.wValue, 0);
445 USETW(req.wIndex, sc->sc_iface_number);
446 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
447
448 err = usbd_do_request(sc->sc_udev, &req, state);
449 if (err) {
450 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
451 usbd_errstr(err)));
452 return (err);
453 }
454
455 sc->sc_line_state = *state;
456
457 return (USBD_NORMAL_COMPLETION);
458 }
459
460 int
461 uplcom_param(void *addr, int portno, struct termios *t)
462 {
463 struct uplcom_softc *sc = addr;
464 usbd_status err;
465 usb_cdc_line_state_t ls;
466
467 DPRINTF(("uplcom_param: sc=%p\n", sc));
468
469 USETDW(ls.dwDTERate, t->c_ospeed);
470 if (ISSET(t->c_cflag, CSTOPB))
471 ls.bCharFormat = UCDC_STOP_BIT_2;
472 else
473 ls.bCharFormat = UCDC_STOP_BIT_1;
474 if (ISSET(t->c_cflag, PARENB)) {
475 if (ISSET(t->c_cflag, PARODD))
476 ls.bParityType = UCDC_PARITY_ODD;
477 else
478 ls.bParityType = UCDC_PARITY_EVEN;
479 } else
480 ls.bParityType = UCDC_PARITY_NONE;
481 switch (ISSET(t->c_cflag, CSIZE)) {
482 case CS5:
483 ls.bDataBits = 5;
484 break;
485 case CS6:
486 ls.bDataBits = 6;
487 break;
488 case CS7:
489 ls.bDataBits = 7;
490 break;
491 case CS8:
492 ls.bDataBits = 8;
493 break;
494 }
495
496 err = uplcom_set_line_coding(sc, &ls);
497 if (err) {
498 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
499 return (EIO);
500 }
501 return (0);
502 }
503