1 | /* $NetBSD: uplcom.c,v 1.79 2016/07/07 06:55:42 msaitoh 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 Ichiro FUKUHARA (ichiro@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 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
19 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
20 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | /* |
32 | * General information: http://www.prolific.com.tw/fr_pl2303.htm |
33 | * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf |
34 | */ |
35 | |
36 | #include <sys/cdefs.h> |
37 | __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.79 2016/07/07 06:55:42 msaitoh Exp $" ); |
38 | |
39 | #ifdef _KERNEL_OPT |
40 | #include "opt_usb.h" |
41 | #endif |
42 | |
43 | #include <sys/param.h> |
44 | #include <sys/systm.h> |
45 | #include <sys/kernel.h> |
46 | #include <sys/kmem.h> |
47 | #include <sys/ioctl.h> |
48 | #include <sys/conf.h> |
49 | #include <sys/tty.h> |
50 | #include <sys/file.h> |
51 | #include <sys/select.h> |
52 | #include <sys/proc.h> |
53 | #include <sys/device.h> |
54 | #include <sys/poll.h> |
55 | |
56 | #include <dev/usb/usb.h> |
57 | #include <dev/usb/usbcdc.h> |
58 | |
59 | #include <dev/usb/usbdi.h> |
60 | #include <dev/usb/usbdi_util.h> |
61 | #include <dev/usb/usbdevs.h> |
62 | #include <dev/usb/usb_quirks.h> |
63 | |
64 | #include <dev/usb/ucomvar.h> |
65 | |
66 | #ifdef UPLCOM_DEBUG |
67 | #define DPRINTFN(n, x) if (uplcomdebug > (n)) printf x |
68 | int uplcomdebug = 0; |
69 | #else |
70 | #define DPRINTFN(n, x) |
71 | #endif |
72 | #define DPRINTF(x) DPRINTFN(0, x) |
73 | |
74 | #define UPLCOM_CONFIG_INDEX 0 |
75 | #define UPLCOM_IFACE_INDEX 0 |
76 | #define UPLCOM_SECOND_IFACE_INDEX 1 |
77 | |
78 | #define UPLCOM_SET_REQUEST 0x01 |
79 | #define UPLCOM_SET_CRTSCTS_0 0x41 |
80 | #define UPLCOM_SET_CRTSCTS_HX 0x61 |
81 | |
82 | #define UPLCOM_N_SERIAL_CTS 0x80 |
83 | |
84 | enum pl2303_type { |
85 | UPLCOM_TYPE_0, /* we use this for all non-HX variants */ |
86 | UPLCOM_TYPE_HX, |
87 | }; |
88 | |
89 | struct uplcom_softc { |
90 | device_t sc_dev; /* base device */ |
91 | struct usbd_device * sc_udev; /* USB device */ |
92 | struct usbd_interface * sc_iface; /* interface */ |
93 | int sc_iface_number; /* interface number */ |
94 | |
95 | struct usbd_interface * sc_intr_iface; /* interrupt interface */ |
96 | int sc_intr_number; /* interrupt number */ |
97 | struct usbd_pipe * sc_intr_pipe; /* interrupt pipe */ |
98 | u_char *sc_intr_buf; /* interrupt buffer */ |
99 | int sc_isize; |
100 | |
101 | usb_cdc_line_state_t sc_line_state; /* current line state */ |
102 | int sc_dtr; /* current DTR state */ |
103 | int sc_rts; /* current RTS state */ |
104 | |
105 | device_t sc_subdev; /* ucom device */ |
106 | |
107 | u_char sc_dying; /* disconnecting */ |
108 | |
109 | u_char sc_lsr; /* Local status register */ |
110 | u_char sc_msr; /* uplcom status register */ |
111 | |
112 | enum pl2303_type sc_type; /* PL2303 chip type */ |
113 | }; |
114 | |
115 | /* |
116 | * These are the maximum number of bytes transferred per frame. |
117 | * The output buffer size cannot be increased due to the size encoding. |
118 | */ |
119 | #define UPLCOMIBUFSIZE 256 |
120 | #define UPLCOMOBUFSIZE 256 |
121 | |
122 | Static usbd_status uplcom_reset(struct uplcom_softc *); |
123 | Static usbd_status uplcom_set_line_coding(struct uplcom_softc *, |
124 | usb_cdc_line_state_t *); |
125 | Static usbd_status uplcom_set_crtscts(struct uplcom_softc *); |
126 | Static void uplcom_intr(struct usbd_xfer *, void *, usbd_status); |
127 | |
128 | Static void uplcom_set(void *, int, int, int); |
129 | Static void uplcom_dtr(struct uplcom_softc *, int); |
130 | Static void uplcom_rts(struct uplcom_softc *, int); |
131 | Static void uplcom_break(struct uplcom_softc *, int); |
132 | Static void uplcom_set_line_state(struct uplcom_softc *); |
133 | Static void uplcom_get_status(void *, int, u_char *, u_char *); |
134 | #if TODO |
135 | Static int uplcom_ioctl(void *, int, u_long, void *, int, proc_t *); |
136 | #endif |
137 | Static int uplcom_param(void *, int, struct termios *); |
138 | Static int uplcom_open(void *, int); |
139 | Static void uplcom_close(void *, int); |
140 | Static usbd_status uplcom_vendor_control_write(struct usbd_device *, uint16_t, uint16_t); |
141 | |
142 | struct ucom_methods uplcom_methods = { |
143 | .ucom_get_status = uplcom_get_status, |
144 | .ucom_set = uplcom_set, |
145 | .ucom_param = uplcom_param, |
146 | .ucom_ioctl = NULL, /* TODO */ |
147 | .ucom_open = uplcom_open, |
148 | .ucom_close = uplcom_close, |
149 | .ucom_read = NULL, |
150 | .ucom_write = NULL, |
151 | }; |
152 | |
153 | static const struct usb_devno uplcom_devs[] = { |
154 | /* I/O DATA USB-RSAQ2 */ |
155 | { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 }, |
156 | /* I/O DATA USB-RSAQ3 */ |
157 | { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 }, |
158 | /* I/O DATA USB-RSAQ */ |
159 | { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ }, |
160 | /* I/O DATA USB-RSAQ5 */ |
161 | { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 }, |
162 | /* PLANEX USB-RS232 URS-03 */ |
163 | { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A }, |
164 | /* various */ |
165 | { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 }, |
166 | /* SMART Technologies USB to serial */ |
167 | { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 }, |
168 | /* IOGEAR/ATENTRIPPLITE */ |
169 | { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 }, |
170 | /* ELECOM UC-SGT */ |
171 | { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT }, |
172 | /* ELECOM UC-SGT0 */ |
173 | { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 }, |
174 | /* Panasonic 50" Touch Panel */ |
175 | { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S }, |
176 | /* RATOC REX-USB60 */ |
177 | { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 }, |
178 | /* TDK USB-PHS Adapter UHA6400 */ |
179 | { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 }, |
180 | /* TDK USB-PDC Adapter UPA9664 */ |
181 | { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 }, |
182 | /* Sony Ericsson USB Cable */ |
183 | { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 }, |
184 | /* SOURCENEXT KeikaiDenwa 8 */ |
185 | { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 }, |
186 | /* SOURCENEXT KeikaiDenwa 8 with charger */ |
187 | { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG }, |
188 | /* HAL Corporation Crossam2+USB */ |
189 | { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 }, |
190 | /* Sitecom USB to serial cable */ |
191 | { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 }, |
192 | /* Pharos USB GPS - Microsoft version */ |
193 | { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X }, |
194 | /* Willcom WS002IN (DD) */ |
195 | { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN }, |
196 | /* COREGA CG-USBRS232R */ |
197 | { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGUSBRS232R }, |
198 | /* Sharp CE-175TU (USB to Zaurus option port 15 adapter) */ |
199 | { USB_VENDOR_SHARP, USB_PRODUCT_SHARP_CE175TU }, |
200 | }; |
201 | #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p) |
202 | |
203 | int uplcom_match(device_t, cfdata_t, void *); |
204 | void uplcom_attach(device_t, device_t, void *); |
205 | void uplcom_childdet(device_t, device_t); |
206 | int uplcom_detach(device_t, int); |
207 | int uplcom_activate(device_t, enum devact); |
208 | extern struct cfdriver uplcom_cd; |
209 | CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match, |
210 | uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet); |
211 | |
212 | int |
213 | uplcom_match(device_t parent, cfdata_t match, void *aux) |
214 | { |
215 | struct usb_attach_arg *uaa = aux; |
216 | |
217 | return uplcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? |
218 | UMATCH_VENDOR_PRODUCT : UMATCH_NONE; |
219 | } |
220 | |
221 | void |
222 | uplcom_attach(device_t parent, device_t self, void *aux) |
223 | { |
224 | struct uplcom_softc *sc = device_private(self); |
225 | struct usb_attach_arg *uaa = aux; |
226 | struct usbd_device *dev = uaa->uaa_device; |
227 | usb_device_descriptor_t *ddesc; |
228 | usb_config_descriptor_t *cdesc; |
229 | usb_interface_descriptor_t *id; |
230 | usb_endpoint_descriptor_t *ed; |
231 | char *devinfop; |
232 | const char *devname = device_xname(self); |
233 | usbd_status err; |
234 | int i; |
235 | struct ucom_attach_args ucaa; |
236 | |
237 | sc->sc_dev = self; |
238 | |
239 | aprint_naive("\n" ); |
240 | aprint_normal("\n" ); |
241 | |
242 | devinfop = usbd_devinfo_alloc(dev, 0); |
243 | aprint_normal_dev(self, "%s\n" , devinfop); |
244 | usbd_devinfo_free(devinfop); |
245 | |
246 | sc->sc_udev = dev; |
247 | |
248 | DPRINTF(("\n\nuplcom attach: sc=%p\n" , sc)); |
249 | |
250 | /* initialize endpoints */ |
251 | ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; |
252 | sc->sc_intr_number = -1; |
253 | sc->sc_intr_pipe = NULL; |
254 | |
255 | /* Move the device into the configured state. */ |
256 | err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1); |
257 | if (err) { |
258 | aprint_error("\n%s: failed to set configuration, err=%s\n" , |
259 | devname, usbd_errstr(err)); |
260 | sc->sc_dying = 1; |
261 | return; |
262 | } |
263 | |
264 | /* determine chip type */ |
265 | ddesc = usbd_get_device_descriptor(dev); |
266 | if (ddesc->bDeviceClass != UDCLASS_COMM && |
267 | ddesc->bMaxPacketSize == 0x40) |
268 | sc->sc_type = UPLCOM_TYPE_HX; |
269 | |
270 | #ifdef UPLCOM_DEBUG |
271 | /* print the chip type */ |
272 | if (sc->sc_type == UPLCOM_TYPE_HX) { |
273 | DPRINTF(("uplcom_attach: chiptype HX\n" )); |
274 | } else { |
275 | DPRINTF(("uplcom_attach: chiptype 0\n" )); |
276 | } |
277 | #endif |
278 | |
279 | /* Move the device into the configured state. */ |
280 | err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1); |
281 | if (err) { |
282 | aprint_error_dev(self, "failed to set configuration: %s\n" , |
283 | usbd_errstr(err)); |
284 | sc->sc_dying = 1; |
285 | return; |
286 | } |
287 | |
288 | /* get the config descriptor */ |
289 | cdesc = usbd_get_config_descriptor(sc->sc_udev); |
290 | |
291 | if (cdesc == NULL) { |
292 | aprint_error_dev(self, |
293 | "failed to get configuration descriptor\n" ); |
294 | sc->sc_dying = 1; |
295 | return; |
296 | } |
297 | |
298 | /* get the (first/common) interface */ |
299 | err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, |
300 | &sc->sc_iface); |
301 | if (err) { |
302 | aprint_error("\n%s: failed to get interface, err=%s\n" , |
303 | devname, usbd_errstr(err)); |
304 | sc->sc_dying = 1; |
305 | return; |
306 | } |
307 | |
308 | /* Find the interrupt endpoints */ |
309 | |
310 | id = usbd_get_interface_descriptor(sc->sc_iface); |
311 | sc->sc_iface_number = id->bInterfaceNumber; |
312 | |
313 | for (i = 0; i < id->bNumEndpoints; i++) { |
314 | ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); |
315 | if (ed == NULL) { |
316 | aprint_error_dev(self, |
317 | "no endpoint descriptor for %d\n" , i); |
318 | sc->sc_dying = 1; |
319 | return; |
320 | } |
321 | |
322 | if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
323 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { |
324 | sc->sc_intr_number = ed->bEndpointAddress; |
325 | sc->sc_isize = UGETW(ed->wMaxPacketSize); |
326 | } |
327 | } |
328 | |
329 | if (sc->sc_intr_number== -1) { |
330 | aprint_error_dev(self, "Could not find interrupt in\n" ); |
331 | sc->sc_dying = 1; |
332 | return; |
333 | } |
334 | |
335 | /* keep interface for interrupt */ |
336 | sc->sc_intr_iface = sc->sc_iface; |
337 | |
338 | /* |
339 | * USB-RSAQ1 has two interface |
340 | * |
341 | * USB-RSAQ1 | USB-RSAQ2 |
342 | * -----------------+----------------- |
343 | * Interface 0 |Interface 0 |
344 | * Interrupt(0x81) | Interrupt(0x81) |
345 | * -----------------+ BulkIN(0x02) |
346 | * Interface 1 | BulkOUT(0x83) |
347 | * BulkIN(0x02) | |
348 | * BulkOUT(0x83) | |
349 | */ |
350 | if (cdesc->bNumInterface == 2) { |
351 | err = usbd_device2interface_handle(dev, |
352 | UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface); |
353 | if (err) { |
354 | aprint_error("\n%s: failed to get second interface, " |
355 | "err=%s\n" , devname, usbd_errstr(err)); |
356 | sc->sc_dying = 1; |
357 | return; |
358 | } |
359 | } |
360 | |
361 | /* Find the bulk{in,out} endpoints */ |
362 | |
363 | id = usbd_get_interface_descriptor(sc->sc_iface); |
364 | sc->sc_iface_number = id->bInterfaceNumber; |
365 | |
366 | for (i = 0; i < id->bNumEndpoints; i++) { |
367 | ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); |
368 | if (ed == NULL) { |
369 | aprint_error_dev(self, |
370 | "no endpoint descriptor for %d\n" , i); |
371 | sc->sc_dying = 1; |
372 | return; |
373 | } |
374 | |
375 | if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
376 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
377 | ucaa.ucaa_bulkin = ed->bEndpointAddress; |
378 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && |
379 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
380 | ucaa.ucaa_bulkout = ed->bEndpointAddress; |
381 | } |
382 | } |
383 | |
384 | if (ucaa.ucaa_bulkin == -1) { |
385 | aprint_error_dev(self, "Could not find data bulk in\n" ); |
386 | sc->sc_dying = 1; |
387 | return; |
388 | } |
389 | |
390 | if (ucaa.ucaa_bulkout == -1) { |
391 | aprint_error_dev(self, "Could not find data bulk out\n" ); |
392 | sc->sc_dying = 1; |
393 | return; |
394 | } |
395 | |
396 | sc->sc_dtr = sc->sc_rts = -1; |
397 | ucaa.ucaa_portno = UCOM_UNK_PORTNO; |
398 | /* ucaa_bulkin, ucaa_bulkout set above */ |
399 | ucaa.ucaa_ibufsize = UPLCOMIBUFSIZE; |
400 | ucaa.ucaa_obufsize = UPLCOMOBUFSIZE; |
401 | ucaa.ucaa_ibufsizepad = UPLCOMIBUFSIZE; |
402 | ucaa.ucaa_opkthdrlen = 0; |
403 | ucaa.ucaa_device = dev; |
404 | ucaa.ucaa_iface = sc->sc_iface; |
405 | ucaa.ucaa_methods = &uplcom_methods; |
406 | ucaa.ucaa_arg = sc; |
407 | ucaa.ucaa_info = NULL; |
408 | |
409 | err = uplcom_reset(sc); |
410 | |
411 | if (err) { |
412 | aprint_error_dev(self, "reset failed, %s\n" , usbd_errstr(err)); |
413 | sc->sc_dying = 1; |
414 | return; |
415 | } |
416 | |
417 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); |
418 | |
419 | DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n" , |
420 | ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number )); |
421 | sc->sc_subdev = config_found_sm_loc(self, "ucombus" , NULL, &ucaa, |
422 | ucomprint, ucomsubmatch); |
423 | |
424 | if (!pmf_device_register(self, NULL, NULL)) |
425 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
426 | |
427 | return; |
428 | } |
429 | |
430 | void |
431 | uplcom_childdet(device_t self, device_t child) |
432 | { |
433 | struct uplcom_softc *sc = device_private(self); |
434 | |
435 | KASSERT(sc->sc_subdev == child); |
436 | sc->sc_subdev = NULL; |
437 | } |
438 | |
439 | int |
440 | uplcom_detach(device_t self, int flags) |
441 | { |
442 | struct uplcom_softc *sc = device_private(self); |
443 | int rv = 0; |
444 | |
445 | DPRINTF(("uplcom_detach: sc=%p flags=%d\n" , sc, flags)); |
446 | |
447 | if (sc->sc_intr_pipe != NULL) { |
448 | usbd_abort_pipe(sc->sc_intr_pipe); |
449 | usbd_close_pipe(sc->sc_intr_pipe); |
450 | kmem_free(sc->sc_intr_buf, sc->sc_isize); |
451 | sc->sc_intr_pipe = NULL; |
452 | } |
453 | |
454 | sc->sc_dying = 1; |
455 | if (sc->sc_subdev != NULL) |
456 | rv = config_detach(sc->sc_subdev, flags); |
457 | |
458 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); |
459 | |
460 | if (rv == 0) |
461 | pmf_device_deregister(self); |
462 | |
463 | return rv; |
464 | } |
465 | |
466 | int |
467 | uplcom_activate(device_t self, enum devact act) |
468 | { |
469 | struct uplcom_softc *sc = device_private(self); |
470 | |
471 | switch (act) { |
472 | case DVACT_DEACTIVATE: |
473 | sc->sc_dying = 1; |
474 | return 0; |
475 | default: |
476 | return EOPNOTSUPP; |
477 | } |
478 | } |
479 | |
480 | usbd_status |
481 | uplcom_reset(struct uplcom_softc *sc) |
482 | { |
483 | usb_device_request_t req; |
484 | usbd_status err; |
485 | |
486 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
487 | req.bRequest = UPLCOM_SET_REQUEST; |
488 | USETW(req.wValue, 0); |
489 | USETW(req.wIndex, sc->sc_iface_number); |
490 | USETW(req.wLength, 0); |
491 | |
492 | err = usbd_do_request(sc->sc_udev, &req, 0); |
493 | if (err) |
494 | return EIO; |
495 | |
496 | return 0; |
497 | } |
498 | |
499 | struct pl2303x_init { |
500 | uint8_t req_type; |
501 | uint8_t request; |
502 | uint16_t value; |
503 | uint16_t index; |
504 | }; |
505 | |
506 | static const struct pl2303x_init pl2303x[] = { |
507 | { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, |
508 | { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0 }, |
509 | { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, |
510 | { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0 }, |
511 | { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, |
512 | { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1 }, |
513 | { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, |
514 | { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0 }, |
515 | { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1 }, |
516 | { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0 }, |
517 | { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44 } |
518 | }; |
519 | #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0])) |
520 | |
521 | static usbd_status |
522 | uplcom_pl2303x_init(struct uplcom_softc *sc) |
523 | { |
524 | usb_device_request_t req; |
525 | usbd_status err; |
526 | int i; |
527 | |
528 | for (i = 0; i < N_PL2302X_INIT; i++) { |
529 | char buf[1]; |
530 | void *b; |
531 | |
532 | req.bmRequestType = pl2303x[i].req_type; |
533 | req.bRequest = pl2303x[i].request; |
534 | USETW(req.wValue, pl2303x[i].value); |
535 | USETW(req.wIndex, pl2303x[i].index); |
536 | if (UT_GET_DIR(req.bmRequestType) == UT_READ) { |
537 | b = buf; |
538 | USETW(req.wLength, sizeof(buf)); |
539 | } else { |
540 | b = NULL; |
541 | USETW(req.wLength, 0); |
542 | } |
543 | |
544 | err = usbd_do_request(sc->sc_udev, &req, b); |
545 | if (err) { |
546 | aprint_error_dev(sc->sc_dev, |
547 | "uplcom_pl2303x_init failed: %s\n" , |
548 | usbd_errstr(err)); |
549 | return EIO; |
550 | } |
551 | } |
552 | |
553 | return 0; |
554 | } |
555 | |
556 | void |
557 | uplcom_set_line_state(struct uplcom_softc *sc) |
558 | { |
559 | usb_device_request_t req; |
560 | int ls; |
561 | |
562 | /* make sure we have initialized state for sc_dtr and sc_rts */ |
563 | if (sc->sc_dtr == -1) |
564 | sc->sc_dtr = 0; |
565 | if (sc->sc_rts == -1) |
566 | sc->sc_rts = 0; |
567 | |
568 | ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) | |
569 | (sc->sc_rts ? UCDC_LINE_RTS : 0); |
570 | |
571 | req.bmRequestType = UT_WRITE_CLASS_INTERFACE; |
572 | req.bRequest = UCDC_SET_CONTROL_LINE_STATE; |
573 | USETW(req.wValue, ls); |
574 | USETW(req.wIndex, sc->sc_iface_number); |
575 | USETW(req.wLength, 0); |
576 | |
577 | (void)usbd_do_request(sc->sc_udev, &req, 0); |
578 | } |
579 | |
580 | void |
581 | uplcom_set(void *addr, int portno, int reg, int onoff) |
582 | { |
583 | struct uplcom_softc *sc = addr; |
584 | |
585 | switch (reg) { |
586 | case UCOM_SET_DTR: |
587 | uplcom_dtr(sc, onoff); |
588 | break; |
589 | case UCOM_SET_RTS: |
590 | uplcom_rts(sc, onoff); |
591 | break; |
592 | case UCOM_SET_BREAK: |
593 | uplcom_break(sc, onoff); |
594 | break; |
595 | default: |
596 | break; |
597 | } |
598 | } |
599 | |
600 | void |
601 | uplcom_dtr(struct uplcom_softc *sc, int onoff) |
602 | { |
603 | |
604 | DPRINTF(("uplcom_dtr: onoff=%d\n" , onoff)); |
605 | |
606 | if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff) |
607 | return; |
608 | |
609 | sc->sc_dtr = !!onoff; |
610 | |
611 | uplcom_set_line_state(sc); |
612 | } |
613 | |
614 | void |
615 | uplcom_rts(struct uplcom_softc *sc, int onoff) |
616 | { |
617 | DPRINTF(("uplcom_rts: onoff=%d\n" , onoff)); |
618 | |
619 | if (sc->sc_rts != -1 && !sc->sc_rts == !onoff) |
620 | return; |
621 | |
622 | sc->sc_rts = !!onoff; |
623 | |
624 | uplcom_set_line_state(sc); |
625 | } |
626 | |
627 | void |
628 | uplcom_break(struct uplcom_softc *sc, int onoff) |
629 | { |
630 | usb_device_request_t req; |
631 | |
632 | DPRINTF(("uplcom_break: onoff=%d\n" , onoff)); |
633 | |
634 | req.bmRequestType = UT_WRITE_CLASS_INTERFACE; |
635 | req.bRequest = UCDC_SEND_BREAK; |
636 | USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); |
637 | USETW(req.wIndex, sc->sc_iface_number); |
638 | USETW(req.wLength, 0); |
639 | |
640 | (void)usbd_do_request(sc->sc_udev, &req, 0); |
641 | } |
642 | |
643 | usbd_status |
644 | uplcom_set_crtscts(struct uplcom_softc *sc) |
645 | { |
646 | usb_device_request_t req; |
647 | usbd_status err; |
648 | |
649 | DPRINTF(("uplcom_set_crtscts: on\n" )); |
650 | |
651 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
652 | req.bRequest = UPLCOM_SET_REQUEST; |
653 | USETW(req.wValue, 0); |
654 | if (sc->sc_type == UPLCOM_TYPE_HX) |
655 | USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX); |
656 | else |
657 | USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0); |
658 | USETW(req.wLength, 0); |
659 | |
660 | err = usbd_do_request(sc->sc_udev, &req, 0); |
661 | if (err) { |
662 | DPRINTF(("uplcom_set_crtscts: failed, err=%s\n" , |
663 | usbd_errstr(err))); |
664 | return err; |
665 | } |
666 | |
667 | return USBD_NORMAL_COMPLETION; |
668 | } |
669 | |
670 | usbd_status |
671 | uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state) |
672 | { |
673 | usb_device_request_t req; |
674 | usbd_status err; |
675 | |
676 | DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n" , |
677 | UGETDW(state->dwDTERate), state->bCharFormat, |
678 | state->bParityType, state->bDataBits)); |
679 | |
680 | if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) { |
681 | DPRINTF(("uplcom_set_line_coding: already set\n" )); |
682 | return USBD_NORMAL_COMPLETION; |
683 | } |
684 | |
685 | req.bmRequestType = UT_WRITE_CLASS_INTERFACE; |
686 | req.bRequest = UCDC_SET_LINE_CODING; |
687 | USETW(req.wValue, 0); |
688 | USETW(req.wIndex, sc->sc_iface_number); |
689 | USETW(req.wLength, UCDC_LINE_STATE_LENGTH); |
690 | |
691 | err = usbd_do_request(sc->sc_udev, &req, state); |
692 | if (err) { |
693 | DPRINTF(("uplcom_set_line_coding: failed, err=%s\n" , |
694 | usbd_errstr(err))); |
695 | return err; |
696 | } |
697 | |
698 | sc->sc_line_state = *state; |
699 | |
700 | return USBD_NORMAL_COMPLETION; |
701 | } |
702 | |
703 | int |
704 | uplcom_param(void *addr, int portno, struct termios *t) |
705 | { |
706 | struct uplcom_softc *sc = addr; |
707 | usbd_status err; |
708 | usb_cdc_line_state_t ls; |
709 | |
710 | DPRINTF(("uplcom_param: sc=%p\n" , sc)); |
711 | |
712 | USETDW(ls.dwDTERate, t->c_ospeed); |
713 | if (ISSET(t->c_cflag, CSTOPB)) |
714 | ls.bCharFormat = UCDC_STOP_BIT_2; |
715 | else |
716 | ls.bCharFormat = UCDC_STOP_BIT_1; |
717 | if (ISSET(t->c_cflag, PARENB)) { |
718 | if (ISSET(t->c_cflag, PARODD)) |
719 | ls.bParityType = UCDC_PARITY_ODD; |
720 | else |
721 | ls.bParityType = UCDC_PARITY_EVEN; |
722 | } else |
723 | ls.bParityType = UCDC_PARITY_NONE; |
724 | switch (ISSET(t->c_cflag, CSIZE)) { |
725 | case CS5: |
726 | ls.bDataBits = 5; |
727 | break; |
728 | case CS6: |
729 | ls.bDataBits = 6; |
730 | break; |
731 | case CS7: |
732 | ls.bDataBits = 7; |
733 | break; |
734 | case CS8: |
735 | ls.bDataBits = 8; |
736 | break; |
737 | } |
738 | |
739 | err = uplcom_set_line_coding(sc, &ls); |
740 | if (err) { |
741 | DPRINTF(("uplcom_param: err=%s\n" , usbd_errstr(err))); |
742 | return EIO; |
743 | } |
744 | |
745 | if (ISSET(t->c_cflag, CRTSCTS)) |
746 | uplcom_set_crtscts(sc); |
747 | |
748 | if (sc->sc_rts == -1 || sc->sc_dtr == -1) |
749 | uplcom_set_line_state(sc); |
750 | |
751 | if (err) { |
752 | DPRINTF(("uplcom_param: err=%s\n" , usbd_errstr(err))); |
753 | return EIO; |
754 | } |
755 | |
756 | return 0; |
757 | } |
758 | |
759 | Static usbd_status |
760 | uplcom_vendor_control_write(struct usbd_device *dev, uint16_t value, |
761 | uint16_t index) |
762 | { |
763 | usb_device_request_t req; |
764 | usbd_status err; |
765 | |
766 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
767 | req.bRequest = UPLCOM_SET_REQUEST; |
768 | USETW(req.wValue, value); |
769 | USETW(req.wIndex, index); |
770 | USETW(req.wLength, 0); |
771 | |
772 | err = usbd_do_request(dev, &req, NULL); |
773 | |
774 | if (err) { |
775 | DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n" , |
776 | usbd_errstr(err), err)); |
777 | } |
778 | |
779 | return err; |
780 | } |
781 | |
782 | int |
783 | uplcom_open(void *addr, int portno) |
784 | { |
785 | struct uplcom_softc *sc = addr; |
786 | usbd_status err; |
787 | |
788 | if (sc->sc_dying) |
789 | return EIO; |
790 | |
791 | DPRINTF(("uplcom_open: sc=%p\n" , sc)); |
792 | |
793 | /* Some unknown device frobbing. */ |
794 | if (sc->sc_type == UPLCOM_TYPE_HX) |
795 | uplcom_vendor_control_write(sc->sc_udev, 2, 0x44); |
796 | else |
797 | uplcom_vendor_control_write(sc->sc_udev, 2, 0x24); |
798 | |
799 | if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { |
800 | sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP); |
801 | err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number, |
802 | USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, |
803 | sc->sc_intr_buf, sc->sc_isize, |
804 | uplcom_intr, USBD_DEFAULT_INTERVAL); |
805 | if (err) { |
806 | DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n" , |
807 | device_xname(sc->sc_dev), sc->sc_intr_number)); |
808 | return EIO; |
809 | } |
810 | } |
811 | |
812 | if (sc->sc_type == UPLCOM_TYPE_HX) |
813 | return uplcom_pl2303x_init(sc); |
814 | |
815 | return 0; |
816 | } |
817 | |
818 | void |
819 | uplcom_close(void *addr, int portno) |
820 | { |
821 | struct uplcom_softc *sc = addr; |
822 | int err; |
823 | |
824 | if (sc->sc_dying) |
825 | return; |
826 | |
827 | DPRINTF(("uplcom_close: close\n" )); |
828 | |
829 | if (sc->sc_intr_pipe != NULL) { |
830 | err = usbd_abort_pipe(sc->sc_intr_pipe); |
831 | if (err) |
832 | printf("%s: abort interrupt pipe failed: %s\n" , |
833 | device_xname(sc->sc_dev), usbd_errstr(err)); |
834 | err = usbd_close_pipe(sc->sc_intr_pipe); |
835 | if (err) |
836 | printf("%s: close interrupt pipe failed: %s\n" , |
837 | device_xname(sc->sc_dev), usbd_errstr(err)); |
838 | kmem_free(sc->sc_intr_buf, sc->sc_isize); |
839 | sc->sc_intr_pipe = NULL; |
840 | } |
841 | } |
842 | |
843 | void |
844 | uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) |
845 | { |
846 | struct uplcom_softc *sc = priv; |
847 | u_char *buf = sc->sc_intr_buf; |
848 | u_char pstatus; |
849 | |
850 | if (sc->sc_dying) |
851 | return; |
852 | |
853 | if (status != USBD_NORMAL_COMPLETION) { |
854 | if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) |
855 | return; |
856 | |
857 | DPRINTF(("%s: abnormal status: %s\n" , device_xname(sc->sc_dev), |
858 | usbd_errstr(status))); |
859 | usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); |
860 | return; |
861 | } |
862 | |
863 | DPRINTF(("%s: uplcom status = %02x\n" , device_xname(sc->sc_dev), |
864 | buf[8])); |
865 | |
866 | sc->sc_lsr = sc->sc_msr = 0; |
867 | pstatus = buf[8]; |
868 | if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS)) |
869 | sc->sc_msr |= UMSR_CTS; |
870 | if (ISSET(pstatus, UCDC_N_SERIAL_RI)) |
871 | sc->sc_msr |= UMSR_RI; |
872 | if (ISSET(pstatus, UCDC_N_SERIAL_DSR)) |
873 | sc->sc_msr |= UMSR_DSR; |
874 | if (ISSET(pstatus, UCDC_N_SERIAL_DCD)) |
875 | sc->sc_msr |= UMSR_DCD; |
876 | ucom_status_change(device_private(sc->sc_subdev)); |
877 | } |
878 | |
879 | void |
880 | uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) |
881 | { |
882 | struct uplcom_softc *sc = addr; |
883 | |
884 | DPRINTF(("uplcom_get_status:\n" )); |
885 | |
886 | if (lsr != NULL) |
887 | *lsr = sc->sc_lsr; |
888 | if (msr != NULL) |
889 | *msr = sc->sc_msr; |
890 | } |
891 | |
892 | #if TODO |
893 | int |
894 | uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag, |
895 | proc_t *p) |
896 | { |
897 | struct uplcom_softc *sc = addr; |
898 | int error = 0; |
899 | |
900 | if (sc->sc_dying) |
901 | return EIO; |
902 | |
903 | DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n" , cmd)); |
904 | |
905 | switch (cmd) { |
906 | case TIOCNOTTY: |
907 | case TIOCMGET: |
908 | case TIOCMSET: |
909 | case USB_GET_CM_OVER_DATA: |
910 | case USB_SET_CM_OVER_DATA: |
911 | break; |
912 | |
913 | default: |
914 | DPRINTF(("uplcom_ioctl: unknown\n" )); |
915 | error = ENOTTY; |
916 | break; |
917 | } |
918 | |
919 | return error; |
920 | } |
921 | #endif |
922 | |