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