1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* $OpenBSD: ucycom.c,v 1.29 2014/07/12 20:26:33 mpi Exp $ */
/* $NetBSD: ucycom.c,v 1.3 2005/08/05 07:27:47 skrll Exp $ */
/*
* Copyright (c) 2005 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nick Hudson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code is based on the ucom driver.
*/
/*
* Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
* RS232 bridges.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uhidev.h>
#include <dev/usb/hid.h>
#include <dev/usb/ucomvar.h>
#ifdef UCYCOM_DEBUG
#define DPRINTF(x) if (ucycomdebug) printf x
#define DPRINTFN(n, x) if (ucycomdebug > (n)) printf x
int ucycomdebug = 200;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
/* Configuration Byte */
#define UCYCOM_RESET 0x80
#define UCYCOM_PARITY_TYPE_MASK 0x20
#define UCYCOM_PARITY_ODD 0x20
#define UCYCOM_PARITY_EVEN 0x00
#define UCYCOM_PARITY_MASK 0x10
#define UCYCOM_PARITY_ON 0x10
#define UCYCOM_PARITY_OFF 0x00
#define UCYCOM_STOP_MASK 0x08
#define UCYCOM_STOP_BITS_2 0x08
#define UCYCOM_STOP_BITS_1 0x00
#define UCYCOM_DATA_MASK 0x03
#define UCYCOM_DATA_BITS_8 0x03
#define UCYCOM_DATA_BITS_7 0x02
#define UCYCOM_DATA_BITS_6 0x01
#define UCYCOM_DATA_BITS_5 0x00
/* Modem (Input) status byte */
#define UCYCOM_RI 0x80
#define UCYCOM_DCD 0x40
#define UCYCOM_DSR 0x20
#define UCYCOM_CTS 0x10
#define UCYCOM_ERROR 0x08
#define UCYCOM_LMASK 0x07
/* Modem (Output) control byte */
#define UCYCOM_DTR 0x20
#define UCYCOM_RTS 0x10
#define UCYCOM_ORESET 0x08
struct ucycom_softc {
struct uhidev sc_hdev;
struct usbd_device *sc_udev;
/* uhidev parameters */
size_t sc_flen; /* feature report length */
size_t sc_ilen; /* input report length */
size_t sc_olen; /* output report length */
uint8_t *sc_obuf;
uint8_t *sc_ibuf;
uint32_t sc_icnt;
/* settings */
uint32_t sc_baud;
uint8_t sc_cfg; /* Data format */
uint8_t sc_mcr; /* Modem control */
uint8_t sc_msr; /* Modem status */
uint8_t sc_newmsr; /* from HID intr */
int sc_swflags;
struct device *sc_subdev;
};
/* Callback routines */
void ucycom_set(void *, int, int, int);
int ucycom_param(void *, int, struct termios *);
void ucycom_get_status(void *, int, u_char *, u_char *);
int ucycom_open(void *, int);
void ucycom_close(void *, int);
void ucycom_write(void *, int, u_char *, u_char *, u_int32_t *);
void ucycom_read(void *, int, u_char **, u_int32_t *);
struct ucom_methods ucycom_methods = {
NULL, /* ucycom_get_status, */
ucycom_set,
ucycom_param,
NULL,
ucycom_open,
ucycom_close,
ucycom_read,
ucycom_write,
};
void ucycom_intr(struct uhidev *, void *, u_int);
void ucycom_get_cfg(struct ucycom_softc *);
const struct usb_devno ucycom_devs[] = {
{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMUSB },
{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMLT20 },
};
int ucycom_match(struct device *, void *, void *);
void ucycom_attach(struct device *, struct device *, void *);
int ucycom_detach(struct device *, int);
struct cfdriver ucycom_cd = {
NULL, "ucycom", DV_DULL
};
const struct cfattach ucycom_ca = {
sizeof(struct ucycom_softc), ucycom_match, ucycom_attach, ucycom_detach
};
int
ucycom_match(struct device *parent, void *match, void *aux)
{
struct uhidev_attach_arg *uha = aux;
if (uha->reportid == UHIDEV_CLAIM_ALLREPORTID)
return (UMATCH_NONE);
return (usb_lookup(ucycom_devs, uha->uaa->vendor, uha->uaa->product) != NULL ?
UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
}
void
ucycom_attach(struct device *parent, struct device *self, void *aux)
{
struct ucycom_softc *sc = (struct ucycom_softc *)self;
struct usb_attach_arg *uaa = aux;
struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
struct usbd_device *dev = uha->parent->sc_udev;
struct ucom_attach_args uca;
int size, repid, err;
void *desc;
sc->sc_hdev.sc_intr = ucycom_intr;
sc->sc_hdev.sc_parent = uha->parent;
sc->sc_hdev.sc_report_id = uha->reportid;
uhidev_get_report_desc(uha->parent, &desc, &size);
repid = uha->reportid;
sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
DPRINTF(("ucycom_open: olen %d ilen %d flen %d\n", sc->sc_ilen,
sc->sc_olen, sc->sc_flen));
printf("\n");
sc->sc_udev = dev;
sc->sc_msr = sc->sc_mcr = 0;
err = uhidev_open(&sc->sc_hdev);
if (err) {
DPRINTF(("ucycom_open: uhidev_open %d\n", err));
return;
}
DPRINTF(("ucycom attach: sc %p opipe %p ipipe %p report_id %d\n",
sc, sc->sc_hdev.sc_parent->sc_opipe, sc->sc_hdev.sc_parent->sc_ipipe,
uha->reportid));
/* bulkin, bulkout set above */
bzero(&uca, sizeof uca);
uca.bulkin = uca.bulkout = -1;
uca.uhidev = sc->sc_hdev.sc_parent;
uca.ibufsize = sc->sc_ilen - 1;
uca.obufsize = sc->sc_olen - 1;
uca.ibufsizepad = 1;
uca.opkthdrlen = 0;
uca.device = uaa->device;
uca.iface = uaa->iface;
uca.methods = &ucycom_methods;
uca.arg = sc;
uca.info = NULL;
sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
DPRINTF(("ucycom_attach: complete %p\n", sc->sc_subdev));
}
void
ucycom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
{
struct ucycom_softc *sc = addr;
DPRINTF(("ucycom_get_status:\n"));
#if 0
if (lsr != NULL)
*lsr = sc->sc_lsr;
#endif
if (msr != NULL)
*msr = sc->sc_msr;
}
int
ucycom_open(void *addr, int portno)
{
struct ucycom_softc *sc = addr;
struct termios t;
int err;
DPRINTF(("ucycom_open: complete\n"));
if (usbd_is_dying(sc->sc_udev))
return (EIO);
/* Allocate an output report buffer */
sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK | M_ZERO);
/* Allocate an input report buffer */
sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK);
DPRINTF(("ucycom_open: sc->sc_ibuf=%p sc->sc_obuf=%p \n",
sc->sc_ibuf, sc->sc_obuf));
t.c_ospeed = 9600;
t.c_cflag = CSTOPB | CS8;
(void)ucycom_param(sc, portno, &t);
sc->sc_mcr = UCYCOM_DTR | UCYCOM_RTS;
sc->sc_obuf[0] = sc->sc_mcr;
err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
if (err) {
DPRINTF(("ucycom_open: set RTS err=%d\n", err));
return (EIO);
}
return (0);
}
void
ucycom_close(void *addr, int portno)
{
struct ucycom_softc *sc = addr;
int s;
if (usbd_is_dying(sc->sc_udev))
return;
s = splusb();
if (sc->sc_obuf != NULL) {
free(sc->sc_obuf, M_USBDEV, 0);
sc->sc_obuf = NULL;
}
if (sc->sc_ibuf != NULL) {
free(sc->sc_ibuf, M_USBDEV, 0);
sc->sc_ibuf = NULL;
}
splx(s);
}
void
ucycom_read(void *addr, int portno, u_char **ptr, u_int32_t *count)
{
struct ucycom_softc *sc = addr;
if (sc->sc_newmsr ^ sc->sc_msr) {
DPRINTF(("ucycom_read: msr %d new %d\n",
sc->sc_msr, sc->sc_newmsr));
sc->sc_msr = sc->sc_newmsr;
ucom_status_change((struct ucom_softc *)sc->sc_subdev);
}
DPRINTF(("ucycom_read: buf %p chars %d\n", sc->sc_ibuf, sc->sc_icnt));
*ptr = sc->sc_ibuf;
*count = sc->sc_icnt;
}
void
ucycom_write(void *addr, int portno, u_char *to, u_char *data, u_int32_t *cnt)
{
struct ucycom_softc *sc = addr;
u_int32_t len;
#ifdef UCYCOM_DEBUG
u_int32_t want = *cnt;
#endif
/*
* The 8 byte output report uses byte 0 for control and byte
* count.
*
* The 32 byte output report uses byte 0 for control. Byte 1
* is used for byte count.
*/
len = sc->sc_olen;
memset(to, 0, len);
switch (sc->sc_olen) {
case 8:
to[0] = *cnt | sc->sc_mcr;
memcpy(&to[1], data, *cnt);
DPRINTF(("ucycomstart(8): to[0] = %d | %d = %d\n",
*cnt, sc->sc_mcr, to[0]));
break;
case 32:
to[0] = sc->sc_mcr;
to[1] = *cnt;
memcpy(&to[2], data, *cnt);
DPRINTF(("ucycomstart(32): to[0] = %d\nto[1] = %d\n",
to[0], to[1]));
break;
}
#ifdef UCYCOM_DEBUG
if (ucycomdebug > 5) {
int i;
if (len != 0) {
DPRINTF(("ucycomstart: to[0..%d) =", len-1));
for (i = 0; i < len; i++)
DPRINTF((" %02x", to[i]));
DPRINTF(("\n"));
}
}
#endif
*cnt = len;
#if 0
ucycom_get_cfg(sc);
#endif
DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", want, len));
}
int
ucycom_param(void *addr, int portno, struct termios *t)
{
struct ucycom_softc *sc = addr;
uint8_t report[5];
uint32_t baud = 0;
uint8_t cfg;
int err;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
switch (t->c_ospeed) {
case 600:
case 1200:
case 2400:
case 4800:
case 9600:
case 19200:
case 38400:
case 57600:
#if 0
/*
* Stock chips only support standard baud rates in the 600 - 57600
* range, but higher rates can be achieved using custom firmware.
*/
case 115200:
case 153600:
case 192000:
#endif
baud = t->c_ospeed;
break;
default:
return (EINVAL);
}
if (t->c_cflag & CIGNORE) {
cfg = sc->sc_cfg;
} else {
cfg = 0;
switch (t->c_cflag & CSIZE) {
case CS8:
cfg |= UCYCOM_DATA_BITS_8;
break;
case CS7:
cfg |= UCYCOM_DATA_BITS_7;
break;
case CS6:
cfg |= UCYCOM_DATA_BITS_6;
break;
case CS5:
cfg |= UCYCOM_DATA_BITS_5;
break;
default:
return (EINVAL);
}
cfg |= ISSET(t->c_cflag, CSTOPB) ?
UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
cfg |= ISSET(t->c_cflag, PARENB) ?
UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
cfg |= ISSET(t->c_cflag, PARODD) ?
UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
}
DPRINTF(("ucycom_param: setting %d baud, %d-%c-%d (%d)\n", baud,
5 + (cfg & UCYCOM_DATA_MASK),
(cfg & UCYCOM_PARITY_MASK) ?
((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
(cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
report[0] = baud & 0xff;
report[1] = (baud >> 8) & 0xff;
report[2] = (baud >> 16) & 0xff;
report[3] = (baud >> 24) & 0xff;
report[4] = cfg;
err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
sc->sc_hdev.sc_report_id, report, sc->sc_flen);
if (err != 0) {
DPRINTF(("ucycom_param: uhidev_set_report %d %s\n",
err, usbd_errstr(err)));
return EIO;
}
sc->sc_baud = baud;
return (err);
}
void
ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
{
extern void ucomreadcb(struct usbd_xfer *, void *, usbd_status);
struct ucycom_softc *sc = (struct ucycom_softc *)addr;
uint8_t *cp = ibuf;
int n, st, s;
/* not accepting data anymore.. */
if (sc->sc_ibuf == NULL)
return;
/* We understand 8 byte and 32 byte input records */
switch (len) {
case 8:
n = cp[0] & UCYCOM_LMASK;
st = cp[0] & ~UCYCOM_LMASK;
cp++;
break;
case 32:
st = cp[0];
n = cp[1];
cp += 2;
break;
default:
DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
return;
}
#ifdef UCYCOM_DEBUG
if (ucycomdebug > 5) {
u_int32_t i;
if (n != 0) {
DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
for (i = 0; i < n; i++)
DPRINTF((" %02x", cp[i]));
DPRINTF(("\n"));
}
}
#endif
if (n > 0 || st != sc->sc_msr) {
s = spltty();
sc->sc_newmsr = st;
bcopy(cp, sc->sc_ibuf, n);
sc->sc_icnt = n;
ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev,
USBD_NORMAL_COMPLETION);
splx(s);
}
}
void
ucycom_set(void *addr, int portno, int reg, int onoff)
{
struct ucycom_softc *sc = addr;
int err;
switch (reg) {
case UCOM_SET_DTR:
if (onoff)
SET(sc->sc_mcr, UCYCOM_DTR);
else
CLR(sc->sc_mcr, UCYCOM_DTR);
break;
case UCOM_SET_RTS:
if (onoff)
SET(sc->sc_mcr, UCYCOM_RTS);
else
CLR(sc->sc_mcr, UCYCOM_RTS);
break;
case UCOM_SET_BREAK:
break;
}
memset(sc->sc_obuf, 0, sc->sc_olen);
sc->sc_obuf[0] = sc->sc_mcr;
err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
if (err)
DPRINTF(("ucycom_set_status: err=%d\n", err));
}
void
ucycom_get_cfg(struct ucycom_softc *sc)
{
int err, cfg, baud;
uint8_t report[5];
err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
sc->sc_hdev.sc_report_id, report, sc->sc_flen);
cfg = report[4];
baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0];
DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud,
5 + (cfg & UCYCOM_DATA_MASK),
(cfg & UCYCOM_PARITY_MASK) ?
((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
(cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
}
int
ucycom_detach(struct device *self, int flags)
{
struct ucycom_softc *sc = (struct ucycom_softc *)self;
DPRINTF(("ucycom_detach: sc=%p flags=%d\n", sc, flags));
if (sc->sc_subdev != NULL) {
config_detach(sc->sc_subdev, flags);
sc->sc_subdev = NULL;
}
if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
uhidev_close(&sc->sc_hdev);
return (0);
}
|