uplcom.c revision 1.5 1 /* $NetBSD: uplcom.c,v 1.5 2001/01/23 10:10:22 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.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
41 * (english)
42 *
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/ioctl.h>
49 #include <sys/conf.h>
50 #include <sys/tty.h>
51 #include <sys/file.h>
52 #include <sys/select.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/device.h>
56 #include <sys/poll.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbcdc.h>
60
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdevs.h>
64 #include <dev/usb/usb_quirks.h>
65
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/ucomvar.h>
68
69 #ifdef UPLCOM_DEBUG
70 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
71 int uplcomdebug = 0xff;
72 #else
73 #define DPRINTFN(n, x)
74 #endif
75 #define DPRINTF(x) DPRINTFN(0, x)
76
77 #define UPLCOM_CONFIG_INDEX 0
78 #define UPLCOM_IFACE_INDEX 0
79 #define UPLCOM_RESET 0
80
81 struct uplcom_softc {
82 USBBASEDEVICE sc_dev; /* base device */
83 usbd_device_handle sc_udev; /* USB device */
84 usbd_interface_handle sc_iface; /* interface */
85 int sc_iface_number; /* interface number */
86
87 usb_cdc_line_state_t sc_line_state; /* current line state */
88 u_char sc_dtr; /* current DTR state */
89 u_char sc_rts; /* current RTS state */
90
91 device_ptr_t sc_subdev;
92
93 u_char sc_dying;
94 };
95
96 /*
97 * These are the maximum number of bytes transferred per frame.
98 * The output buffer size cannot be increased due to the size encoding.
99 */
100 #define UPLCOMIBUFSIZE 256;
101 #define UPLCOMOBUFSIZE 256;
102
103 Static usbd_status uplcom_init(struct uplcom_softc *);
104 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
105 usb_cdc_line_state_t *state);
106
107 Static void uplcom_set(void *, int, int, int);
108 Static void uplcom_dtr(struct uplcom_softc *, int);
109 Static void uplcom_rts(struct uplcom_softc *, int);
110 Static void uplcom_break(struct uplcom_softc *, int);
111 Static void uplcom_set_line_state(struct uplcom_softc *);
112 Static int uplcom_param(void *, int, struct termios *);
113
114 struct ucom_methods uplcom_methods = {
115 NULL,
116 uplcom_set,
117 uplcom_param,
118 NULL,
119 NULL,
120 NULL,
121 NULL,
122 NULL,
123 };
124
125 static const struct uplcom_product {
126 uint16_t vendor;
127 uint16_t product;
128 } uplcom_products [] = {
129 /* I/O DATA USB-RSAQ2 */
130 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
131
132 { 0, 0 }
133 };
134
135 USB_DECLARE_DRIVER(uplcom);
136
137 USB_MATCH(uplcom)
138 {
139 USB_MATCH_START(uplcom, uaa);
140 int i;
141
142
143 if (uaa->iface == NULL)
144 return (UMATCH_NONE);
145
146 for (i = 0; uplcom_products[i].vendor != 0; i++) {
147 if (uplcom_products[i].vendor == uaa->vendor &&
148 uplcom_products[i].product == uaa->product) {
149 return (UMATCH_VENDOR_PRODUCT);
150 }
151 }
152 return (UMATCH_NONE);
153 }
154
155 USB_ATTACH(uplcom)
156 {
157 USB_ATTACH_START(uplcom, sc, uaa);
158 usbd_device_handle dev = uaa->device;
159 usbd_interface_handle iface;
160 usb_interface_descriptor_t *id;
161 usb_endpoint_descriptor_t *ed;
162
163 char devinfo[1024];
164 char *devname = USBDEVNAME(sc->sc_dev);
165 usbd_status err;
166 int i;
167 struct ucom_attach_args uca;
168
169 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
170
171 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
172 if (err) {
173 printf("\n%s: failed to set configuration, err=%s\n",
174 devname, usbd_errstr(err));
175 USB_ATTACH_ERROR_RETURN;
176 }
177
178 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, &iface);
179 if (err) {
180 printf("\n%s: failed to get interface, err=%s\n",
181 devname, usbd_errstr(err));
182 USB_ATTACH_ERROR_RETURN;
183 }
184
185 usbd_devinfo(dev, 0, devinfo);
186 USB_ATTACH_SETUP;
187 printf("%s: %s\n", devname, devinfo);
188
189 id = usbd_get_interface_descriptor(iface);
190
191 sc->sc_udev = dev;
192 sc->sc_iface = iface;
193
194 sc->sc_iface_number = id->bInterfaceNumber;
195
196 uca.bulkin = uca.bulkout = -1;
197 for (i = 0; i < id->bNumEndpoints; i++) {
198 ed = usbd_interface2endpoint_descriptor(iface, i);
199 if (ed == NULL) {
200 printf("%s: no endpoint descriptor for %d\n",
201 USBDEVNAME(sc->sc_dev), i);
202 USB_ATTACH_ERROR_RETURN;
203 }
204
205 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
206 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
207 uca.bulkin = ed->bEndpointAddress;
208 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
209 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
210 DPRINTF(("interrupt endpoint addr = 0x%x\n",
211 ed->bEndpointAddress));
212 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
213 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
214 uca.bulkout = ed->bEndpointAddress;
215 }
216 }
217
218 if (uca.bulkin == -1) {
219 printf("%s: Could not find data bulk in\n",
220 USBDEVNAME(sc->sc_dev));
221 USB_ATTACH_ERROR_RETURN;
222 }
223
224 if (uca.bulkout == -1) {
225 printf("%s: Could not find data bulk out\n",
226 USBDEVNAME(sc->sc_dev));
227 USB_ATTACH_ERROR_RETURN;
228 }
229
230 sc->sc_dtr = -1;
231 uca.portno = UCOM_UNK_PORTNO;
232 /* bulkin, bulkout set above */
233 uca.ibufsize = UPLCOMIBUFSIZE;
234 uca.obufsize = UPLCOMOBUFSIZE;
235 uca.ibufsizepad = UPLCOMIBUFSIZE;
236 uca.opkthdrlen = 0;
237 uca.device = dev;
238 uca.iface = iface;
239 uca.methods = &uplcom_methods;
240 uca.arg = sc;
241
242 err = uplcom_init(sc);
243 if (err) {
244 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
245 usbd_errstr(err));
246 USB_ATTACH_ERROR_RETURN;
247 }
248
249 DPRINTF(("uplcom: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
250 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
251
252 USB_ATTACH_SUCCESS_RETURN;
253 }
254
255 USB_DETACH(uplcom)
256 {
257 USB_DETACH_START(uplcom, sc);
258 int rv = 0;
259
260 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
261 sc->sc_dying = 1;
262 if (sc->sc_subdev != NULL)
263 rv = config_detach(sc->sc_subdev, flags);
264
265 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
266 USBDEV(sc->sc_dev));
267
268 return (rv);
269 }
270
271 int
272 uplcom_activate(device_ptr_t self, enum devact act)
273 {
274 struct uplcom_softc *sc = (struct uplcom_softc *)self;
275 int rv = 0;
276
277 switch (act) {
278 case DVACT_ACTIVATE:
279 return (EOPNOTSUPP);
280 break;
281
282 case DVACT_DEACTIVATE:
283 if (sc->sc_subdev != NULL)
284 rv = config_deactivate(sc->sc_subdev);
285 sc->sc_dying = 1;
286 break;
287 }
288 return (rv);
289 }
290
291
292 usbd_status
293 uplcom_init(struct uplcom_softc *sc)
294 {
295 usb_device_request_t req;
296 usbd_status err;
297
298 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
299 req.bRequest = UPLCOM_RESET;
300 USETW(req.wValue, UPLCOM_RESET);
301 USETW(req.wIndex, sc->sc_iface_number);
302 USETW(req.wLength, 0);
303
304 err = usbd_do_request(sc->sc_udev, &req, 0);
305 if (err)
306 return (EIO);
307
308 return (0);
309 }
310
311 void
312 uplcom_set_line_state(struct uplcom_softc *sc)
313 {
314 usb_device_request_t req;
315 int ls;
316
317 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
318 (sc->sc_rts ? UCDC_LINE_RTS : 0);
319 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
320 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
321 USETW(req.wValue, ls);
322 USETW(req.wIndex, sc->sc_iface_number);
323 USETW(req.wLength, 0);
324
325 (void)usbd_do_request(sc->sc_udev, &req, 0);
326
327 }
328
329 void
330 uplcom_set(void *addr, int portno, int reg, int onoff)
331 {
332 struct uplcom_softc *sc = addr;
333
334 switch (reg) {
335 case UCOM_SET_DTR:
336 uplcom_dtr(sc, onoff);
337 break;
338 case UCOM_SET_RTS:
339 uplcom_rts(sc, onoff);
340 break;
341 case UCOM_SET_BREAK:
342 uplcom_break(sc, onoff);
343 break;
344 default:
345 break;
346 }
347 }
348
349 void
350 uplcom_dtr(struct uplcom_softc *sc, int onoff)
351 {
352
353 DPRINTF(("uplcom: onoff=%d\n", onoff));
354
355 if (sc->sc_dtr == onoff)
356 return;
357 sc->sc_dtr = onoff;
358
359 uplcom_set_line_state(sc);
360 }
361
362 void
363 uplcom_rts(struct uplcom_softc *sc, int onoff)
364 {
365 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
366
367 if (sc->sc_rts == onoff)
368 return;
369 sc->sc_rts = onoff;
370
371 uplcom_set_line_state(sc);
372 }
373
374 void
375 uplcom_break(struct uplcom_softc *sc, int onoff)
376 {
377 usb_device_request_t req;
378
379 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
380
381 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
382 req.bRequest = UCDC_SEND_BREAK;
383 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
384 USETW(req.wIndex, sc->sc_iface_number);
385 USETW(req.wLength, 0);
386
387 (void)usbd_do_request(sc->sc_udev, &req, 0);
388 }
389
390 usbd_status
391 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
392 {
393 usb_device_request_t req;
394 usbd_status err;
395
396 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
397 UGETDW(state->dwDTERate), state->bCharFormat,
398 state->bParityType, state->bDataBits));
399
400 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
401 DPRINTF(("uplcom_set_line_coding: already set\n"));
402 return (USBD_NORMAL_COMPLETION);
403 }
404
405 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
406 req.bRequest = UCDC_SET_LINE_CODING;
407 USETW(req.wValue, 0);
408 USETW(req.wIndex, sc->sc_iface_number);
409 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
410
411 err = usbd_do_request(sc->sc_udev, &req, state);
412 if (err) {
413 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
414 usbd_errstr(err)));
415 return (err);
416 }
417
418 sc->sc_line_state = *state;
419
420 return (USBD_NORMAL_COMPLETION);
421 }
422
423 int
424 uplcom_param(void *addr, int portno, struct termios *t)
425 {
426 struct uplcom_softc *sc = addr;
427 usbd_status err;
428 usb_cdc_line_state_t ls;
429
430 DPRINTF(("uplcom_param: sc=%p\n", sc));
431
432 USETDW(ls.dwDTERate, t->c_ospeed);
433 if (ISSET(t->c_cflag, CSTOPB))
434 ls.bCharFormat = UCDC_STOP_BIT_2;
435 else
436 ls.bCharFormat = UCDC_STOP_BIT_1;
437 if (ISSET(t->c_cflag, PARENB)) {
438 if (ISSET(t->c_cflag, PARODD))
439 ls.bParityType = UCDC_PARITY_ODD;
440 else
441 ls.bParityType = UCDC_PARITY_EVEN;
442 } else
443 ls.bParityType = UCDC_PARITY_NONE;
444 switch (ISSET(t->c_cflag, CSIZE)) {
445 case CS5:
446 ls.bDataBits = 5;
447 break;
448 case CS6:
449 ls.bDataBits = 6;
450 break;
451 case CS7:
452 ls.bDataBits = 7;
453 break;
454 case CS8:
455 ls.bDataBits = 8;
456 break;
457 }
458
459 err = uplcom_set_line_coding(sc, &ls);
460 if (err) {
461 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
462 return (EIO);
463 }
464 return (0);
465 }
466