summaryrefslogtreecommitdiff
path: root/sys/dev/usb/uslhcom.c
blob: 976bedba2c5c6182b83884007fb4d6daee59238f (plain)
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
/*	$OpenBSD: uslhcom.c,v 1.4 2016/01/09 04:14:42 jcs Exp $	*/

/*
 * Copyright (c) 2015 SASANO Takayoshi <uaa@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Device driver for Silicon Labs CP2110 USB HID-UART bridge.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/tty.h>
#include <sys/device.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>

#include <dev/usb/usbhid.h>
#include <dev/usb/uhidev.h>

#include <dev/usb/ucomvar.h>
#include <dev/usb/uslhcomreg.h>

#ifdef USLHCOM_DEBUG
#define	DPRINTF(x)	if (uslhcomdebug) printf x
#else
#define	DPRINTF(x)
#endif

struct uslhcom_softc {
	struct uhidev		 sc_hdev;
	struct usbd_device	*sc_udev;

	u_char			*sc_ibuf;
	u_int			 sc_icnt;

	u_char			 sc_lsr;
	u_char			 sc_msr;

	struct device		*sc_subdev;
};

void		uslhcom_get_status(void *, int, u_char *, u_char *);
void		uslhcom_set(void *, int, int, int);
int		uslhcom_param(void *, int, struct termios *);
int		uslhcom_open(void *, int);
void		uslhcom_close(void *, int);
void		uslhcom_write(void *, int, u_char *, u_char *, u_int32_t *);
void		uslhcom_read(void *, int, u_char **, u_int32_t *);
void		uslhcom_intr(struct uhidev *, void *, u_int);

int		uslhcom_match(struct device *, void *, void *);
void		uslhcom_attach(struct device *, struct device *, void *);
int		uslhcom_detach(struct device *, int);

int		uslhcom_uart_endis(struct uslhcom_softc *, int);
int		uslhcom_clear_fifo(struct uslhcom_softc *, int);
int		uslhcom_get_version(struct uslhcom_softc *, struct uslhcom_version_info *);
int		uslhcom_get_uart_status(struct uslhcom_softc *, struct uslhcom_uart_status *);
int		uslhcom_set_break(struct uslhcom_softc *, int);
int		uslhcom_set_config(struct uslhcom_softc *, struct uslhcom_uart_config *);
void		uslhcom_set_baud_rate(struct uslhcom_uart_config *, u_int32_t);
int		uslhcom_create_config(struct uslhcom_uart_config *, struct termios *);
int		uslhcom_setup(struct uslhcom_softc *, struct uslhcom_uart_config *);

struct ucom_methods uslhcom_methods = {
	uslhcom_get_status,
	uslhcom_set,
	uslhcom_param,
	NULL,
	uslhcom_open,
	uslhcom_close,
	uslhcom_read,
	uslhcom_write,
};

static const struct usb_devno uslhcom_devs[] = {
	{ USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2110 },
};

struct cfdriver uslhcom_cd = {
	NULL, "uslhcom", DV_DULL
};

const struct cfattach uslhcom_ca = {
	sizeof(struct uslhcom_softc),
	uslhcom_match, uslhcom_attach, uslhcom_detach
};

/* ----------------------------------------------------------------------
 * driver entry points
 */

int
uslhcom_match(struct device *parent, void *match, void *aux)
{
	struct uhidev_attach_arg *uha = aux;

	/* use all report IDs */
	if (uha->reportid != UHIDEV_CLAIM_ALLREPORTID)
		return UMATCH_NONE;

	return (usb_lookup(uslhcom_devs,
			   uha->uaa->vendor, uha->uaa->product) != NULL ?
		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
}

void
uslhcom_attach(struct device *parent, struct device *self, void *aux)
{
	struct uslhcom_softc *sc = (struct uslhcom_softc *)self;
	struct uhidev_attach_arg *uha = aux;
	struct usbd_device *dev = uha->parent->sc_udev;
	struct ucom_attach_args uca;
	struct uslhcom_version_info version;
	int err, repid, size, rsize;
	void *desc;

	sc->sc_udev = dev;
	sc->sc_lsr = sc->sc_msr = 0;
	sc->sc_hdev.sc_intr = uslhcom_intr;
	sc->sc_hdev.sc_parent = uha->parent;
	sc->sc_hdev.sc_report_id = uha->reportid;
	sc->sc_hdev.sc_isize = sc->sc_hdev.sc_osize = sc->sc_hdev.sc_fsize = 0;

	uhidev_get_report_desc(uha->parent, &desc, &size);
	for (repid = 0; repid < uha->parent->sc_nrepid; repid++) {
		rsize = hid_report_size(desc, size, hid_input, repid);
		if (sc->sc_hdev.sc_isize < rsize) sc->sc_hdev.sc_isize = rsize;
		rsize = hid_report_size(desc, size, hid_output, repid);
		if (sc->sc_hdev.sc_osize < rsize) sc->sc_hdev.sc_osize = rsize;
		rsize = hid_report_size(desc, size, hid_feature, repid);
		if (sc->sc_hdev.sc_fsize < rsize) sc->sc_hdev.sc_fsize = rsize;
	}

	printf("\n");

	err = uhidev_open(&sc->sc_hdev);
	if (err) {
		DPRINTF(("uslhcom_attach: uhidev_open %d\n", err));
		return;
	}

	DPRINTF(("uslhcom_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));
	DPRINTF(("uslhcom_attach: isize %d osize %d fsize %d\n",
		 sc->sc_hdev.sc_isize, sc->sc_hdev.sc_osize,
		 sc->sc_hdev.sc_fsize));

	uslhcom_uart_endis(sc, UART_DISABLE);
	uslhcom_get_version(sc, &version);
	printf("%s: pid %#x rev %#x\n", sc->sc_hdev.sc_dev.dv_xname,
	       version.product_id, version.product_revision);

	/* setup ucom layer */
	uca.portno = UCOM_UNK_PORTNO;
	uca.bulkin = uca.bulkout = -1;
	uca.ibufsize = uca.ibufsizepad = 0;
	uca.obufsize = sc->sc_hdev.sc_osize;
	uca.opkthdrlen = USLHCOM_TX_HEADER_SIZE;
	uca.uhidev = sc->sc_hdev.sc_parent;
	uca.device = uha->uaa->device;
	uca.iface = uha->uaa->iface;
	uca.methods = &uslhcom_methods;
	uca.arg = sc;
	uca.info = NULL;

	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
}

int
uslhcom_detach(struct device *self, int flags)
{
	struct uslhcom_softc *sc = (struct uslhcom_softc *)self;

	DPRINTF(("uslhcom_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;
}

/* ----------------------------------------------------------------------
 * low level I/O
 */

int
uslhcom_uart_endis(struct uslhcom_softc *sc, int enable)
{
	int len;
	u_char val;

	len = sizeof(val);
	val = enable;

	return uhidev_set_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
				 GET_SET_UART_ENABLE, &val, len) != len;
}

int
uslhcom_clear_fifo(struct uslhcom_softc *sc, int fifo)
{
	int len;
	u_char val;

	len = sizeof(val);
	val = fifo;

	return uhidev_set_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
				 SET_CLEAR_FIFOS, &val, len) != len;
}

int
uslhcom_get_version(struct uslhcom_softc *sc, struct uslhcom_version_info *version)
{
	int len;

	len = sizeof(*version);

	return uhidev_get_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
				 GET_VERSION, version, len) < len;
}

int
uslhcom_get_uart_status(struct uslhcom_softc *sc, struct uslhcom_uart_status *status)
{
	int len;

	len = sizeof(*status);

	return uhidev_get_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
				 GET_UART_STATUS, status, len) < len;
}

int
uslhcom_set_break(struct uslhcom_softc *sc, int onoff)
{
	int len, reportid;
	u_char val;

	len = sizeof(val);

	if (onoff) {
		val = 0; /* send break until SET_STOP_LINE_BREAK */
		reportid = SET_TRANSMIT_LINE_BREAK;
	} else {
		val = 0; /* any value can be accepted */
		reportid = SET_STOP_LINE_BREAK;
	}

	return uhidev_set_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
				 reportid, &val, len) != len;
}

int
uslhcom_set_config(struct uslhcom_softc *sc, struct uslhcom_uart_config *config)
{
	int len;

	len = sizeof(*config);

	return uhidev_set_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
				 GET_SET_UART_CONFIG, config, len) != len;
}

void
uslhcom_set_baud_rate(struct uslhcom_uart_config *config, u_int32_t baud_rate)
{
	config->baud_rate[0] = baud_rate >> 24;
	config->baud_rate[1] = baud_rate >> 16;
	config->baud_rate[2] = baud_rate >> 8;
	config->baud_rate[3] = baud_rate >> 0;
}

int
uslhcom_create_config(struct uslhcom_uart_config *config, struct termios *t)
{
	if (t->c_ospeed < UART_CONFIG_BAUD_RATE_MIN ||
	    t->c_ospeed > UART_CONFIG_BAUD_RATE_MAX)
		return EINVAL;

	uslhcom_set_baud_rate(config, t->c_ospeed);

	if (ISSET(t->c_cflag, PARENB)) {
		if (ISSET(t->c_cflag, PARODD))
			config->parity = UART_CONFIG_PARITY_ODD;
		else
			config->parity = UART_CONFIG_PARITY_EVEN;
	} else
		config->parity = UART_CONFIG_PARITY_NONE;

	if (ISSET(t->c_cflag, CRTSCTS))
		config->data_control = UART_CONFIG_DATA_CONTROL_HARD;
	else
		config->data_control = UART_CONFIG_DATA_CONTROL_NONE;

	switch (ISSET(t->c_cflag, CSIZE)) {
	case CS5:
		config->data_bits = UART_CONFIG_DATA_BITS_5;
		break;
	case CS6:
		config->data_bits = UART_CONFIG_DATA_BITS_6;
		break;
	case CS7:
		config->data_bits = UART_CONFIG_DATA_BITS_7;
		break;
	case CS8:
		config->data_bits = UART_CONFIG_DATA_BITS_8;
		break;
	default:
		return EINVAL;
	}

	if (ISSET(t->c_cflag, CSTOPB))
		config->stop_bits = UART_CONFIG_STOP_BITS_2;
	else
		config->stop_bits = UART_CONFIG_STOP_BITS_1;

	return 0;
}

int
uslhcom_setup(struct uslhcom_softc *sc, struct uslhcom_uart_config *config)
{
	struct uslhcom_uart_status status;

	if (uslhcom_uart_endis(sc, UART_DISABLE))
		return EIO;

	if (uslhcom_set_config(sc, config))
		return EIO;

	if (uslhcom_clear_fifo(sc, CLEAR_TX_FIFO | CLEAR_RX_FIFO))
		return EIO;

	if (uslhcom_get_uart_status(sc, &status))
		return EIO;

	if (uslhcom_uart_endis(sc, UART_ENABLE))
		return EIO;

	return 0;
}

/* ----------------------------------------------------------------------
 * methods for ucom
 */

void
uslhcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
{
	struct uslhcom_softc *sc = arg;

	if (usbd_is_dying(sc->sc_udev))
		return;

	*rlsr = sc->sc_lsr;
	*rmsr = sc->sc_msr;
}

void
uslhcom_set(void *arg, int portno, int reg, int onoff)
{
	struct uslhcom_softc *sc = arg;

	if (usbd_is_dying(sc->sc_udev))
		return;

	switch (reg) {
	case UCOM_SET_DTR:
	case UCOM_SET_RTS:
		/* no support, do nothing */
		break;
	case UCOM_SET_BREAK:
		uslhcom_set_break(sc, onoff);
		break;
	}
}

int
uslhcom_param(void *arg, int portno, struct termios *t)
{
	struct uslhcom_softc *sc = arg;
	struct uslhcom_uart_config config;
	int ret;

	if (usbd_is_dying(sc->sc_udev))
		return 0;

	ret = uslhcom_create_config(&config, t);
	if (ret)
		return ret;

	ret = uslhcom_setup(sc, &config);
	if (ret)
		return ret;

	return 0;
}

int
uslhcom_open(void *arg, int portno)
{
	struct uslhcom_softc *sc = arg;
	struct uslhcom_uart_config config;
	int ret;

	if (usbd_is_dying(sc->sc_udev))
		return EIO;

	sc->sc_ibuf = malloc(sc->sc_hdev.sc_osize + sizeof(u_char),
			     M_USBDEV, M_WAITOK);

	uslhcom_set_baud_rate(&config, 9600);
	config.parity = UART_CONFIG_PARITY_NONE;
	config.data_control = UART_CONFIG_DATA_CONTROL_NONE;
	config.data_bits = UART_CONFIG_DATA_BITS_8;
	config.stop_bits = UART_CONFIG_STOP_BITS_1;

	ret = uslhcom_set_config(sc, &config);
	if (ret)
		return ret;

	return 0;
}

void
uslhcom_close(void *arg, int portno)
{
	struct uslhcom_softc *sc = arg;
	int s;

	if (usbd_is_dying(sc->sc_udev))
		return;

	uslhcom_uart_endis(sc, UART_DISABLE);

	s = splusb();
	if (sc->sc_ibuf != NULL) {
		free(sc->sc_ibuf, M_USBDEV, 0);
		sc->sc_ibuf = NULL;
	}
	splx(s);
}

void
uslhcom_read(void *arg, int portno, u_char **ptr, u_int32_t *cnt)
{
	struct uslhcom_softc *sc = arg;

	*ptr = sc->sc_ibuf;
	*cnt = sc->sc_icnt;
}

void
uslhcom_write(void *arg, int portno, u_char *to, u_char *data, u_int32_t *cnt)
{
	bcopy(data, &to[USLHCOM_TX_HEADER_SIZE], *cnt);
	to[0] = *cnt;	/* add Report ID (= transmit length) */
	*cnt += USLHCOM_TX_HEADER_SIZE;
}

void
uslhcom_intr(struct uhidev *addr, void *ibuf, u_int len)
{
	extern void ucomreadcb(struct usbd_xfer *, void *, usbd_status);
	struct uslhcom_softc *sc = (struct uslhcom_softc *)addr;
	int s;

	if (sc->sc_ibuf == NULL)
		return;

	s = spltty();
	sc->sc_icnt = len;	/* Report ID is already stripped */
	bcopy(ibuf, sc->sc_ibuf, len);
	ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev,
		   USBD_NORMAL_COMPLETION);
	splx(s);
}