项目

一般

简介

VE问题 #2510 » ch343.c

PM 洪丰, 2023-11-13 11:34

 
1
/*
2
 * USB serial driver for USB to UART(s) chip ch342/ch343/ch344/ch347/ch9101/ch9102/ch9103/ch9104, etc.
3
 *
4
 * Copyright (C) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
5
 * Web: http://wch.cn
6
 * Author: WCH <tech@wch.cn>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * System required:
14
 * Kernel version beyond 3.4.x
15
 * Update Log:
16
 * V1.0 - initial version
17
 * V1.1 - add support of chip ch344, ch9101 and ch9103
18
 * V1.2 - add gpio support of chip ch344
19
 * V1.3 - add support of chip ch347
20
 * V1.4 - add support of chip ch9104
21
 * V1.5 - add gpio character device
22
 *      - add supports for kernel version beyond 5.14.x
23
 */
24

    
25
#define DEBUG
26
#define VERBOSE_DEBUG
27

    
28
#undef DEBUG
29
#undef VERBOSE_DEBUG
30

    
31
#include <linux/errno.h>
32
#include <linux/idr.h>
33
#include <linux/init.h>
34
#include <linux/kernel.h>
35
#include <linux/list.h>
36
#include <linux/module.h>
37
#include <linux/mutex.h>
38
#include <linux/serial.h>
39
#include <linux/slab.h>
40
#include <linux/tty.h>
41
#include <linux/tty_driver.h>
42
#include <linux/tty_flip.h>
43
#include <linux/uaccess.h>
44
#include <linux/usb.h>
45
#include <linux/usb/cdc.h>
46
#include <linux/version.h>
47
#include <asm/byteorder.h>
48
#include <asm/unaligned.h>
49

    
50
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
51
#include <linux/sched/signal.h>
52
#endif
53

    
54
#include "ch343.h"
55

    
56
#define DRIVER_AUTHOR "WCH"
57
#define DRIVER_DESC   "USB serial driver for ch342/ch343/ch344/ch347/ch9101/ch9102/ch9103/ch9104, etc."
58
#define VERSION_DESC  "V1.5 On 2023.03"
59

    
60
#define IOCTL_MAGIC	      'W'
61
#define IOCTL_CMD_GETCHIPTYPE _IOR(IOCTL_MAGIC, 0x84, u16)
62
#define IOCTL_CMD_CTRLIN      _IOWR(IOCTL_MAGIC, 0x90, u16)
63
#define IOCTL_CMD_CTRLOUT     _IOW(IOCTL_MAGIC, 0x91, u16)
64

    
65
static struct usb_driver ch343_driver;
66
static struct tty_driver *ch343_tty_driver;
67
static struct usb_interface *g_intf;
68

    
69
static DEFINE_IDR(ch343_minors);
70
static DEFINE_MUTEX(ch343_minors_lock);
71

    
72
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
73
static void ch343_tty_set_termios(struct tty_struct *tty, const struct ktermios *termios_old);
74
#else
75
static void ch343_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old);
76
#endif
77

    
78
/*
79
 * Look up an ch343 structure by minor. If found and not disconnected, increment
80
 * its refcount and return it with its mutex held.
81
 */
82
static struct ch343 *ch343_get_by_minor(unsigned int minor)
83
{
84
	struct ch343 *ch343;
85

    
86
	mutex_lock(&ch343_minors_lock);
87
	ch343 = idr_find(&ch343_minors, minor);
88
	if (ch343) {
89
		mutex_lock(&ch343->mutex);
90
		if (ch343->disconnected) {
91
			mutex_unlock(&ch343->mutex);
92
			ch343 = NULL;
93
		} else {
94
			tty_port_get(&ch343->port);
95
			mutex_unlock(&ch343->mutex);
96
		}
97
	}
98
	mutex_unlock(&ch343_minors_lock);
99
	return ch343;
100
}
101

    
102
static int ch343_alloc_minor(struct ch343 *ch343)
103
{
104
	int minor;
105

    
106
	mutex_lock(&ch343_minors_lock);
107
	minor = idr_alloc(&ch343_minors, ch343, 0, CH343_TTY_MINORS, GFP_KERNEL);
108
	mutex_unlock(&ch343_minors_lock);
109

    
110
	return minor;
111
}
112

    
113
/* Release the minor number associated with 'ch343'. */
114
static void ch343_release_minor(struct ch343 *ch343)
115
{
116
	mutex_lock(&ch343_minors_lock);
117
	idr_remove(&ch343_minors, ch343->minor);
118
	mutex_unlock(&ch343_minors_lock);
119
}
120

    
121
static int ch343_control_out(struct ch343 *ch343, u8 request, u16 value, u16 index)
122
{
123
	int retval;
124

    
125
	retval = usb_autopm_get_interface(ch343->control);
126
	if (retval)
127
		return retval;
128
	retval = usb_control_msg(ch343->dev, usb_sndctrlpipe(ch343->dev, 0), request,
129
				 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, value, index, NULL, 0,
130
				 DEFAULT_TIMEOUT);
131
	usb_autopm_put_interface(ch343->control);
132

    
133
	return retval;
134
}
135

    
136
static int ch343_control_in(struct ch343 *ch343, u8 request, u16 value, u16 index, char *buf, unsigned bufsize)
137
{
138
	int retval;
139

    
140
	retval = usb_autopm_get_interface(ch343->control);
141
	if (retval)
142
		return retval;
143
	retval = usb_control_msg(ch343->dev, usb_rcvctrlpipe(ch343->dev, 0), request,
144
				 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, value, index, buf, bufsize,
145
				 DEFAULT_TIMEOUT);
146
	usb_autopm_put_interface(ch343->control);
147

    
148
	return retval;
149
}
150

    
151
static int ch343_control_msg_out(struct ch343 *ch343, u8 request, u8 requesttype, u16 value, u16 index, void *buf,
152
				 unsigned bufsize)
153
{
154
	int retval;
155
	char *buffer;
156

    
157
	buffer = kmalloc(bufsize, GFP_KERNEL);
158
	if (!buffer)
159
		return -ENOMEM;
160

    
161
	retval = copy_from_user(buffer, (char __user *)buf, bufsize);
162
	if (retval)
163
		goto out;
164

    
165
	retval = usb_autopm_get_interface(ch343->control);
166
	if (retval)
167
		goto out;
168
	retval = usb_control_msg(ch343->dev, usb_sndctrlpipe(ch343->dev, 0), request, requesttype, value, index, buf,
169
				 bufsize, DEFAULT_TIMEOUT);
170
	usb_autopm_put_interface(ch343->control);
171

    
172
out:
173
	kfree(buffer);
174
	return retval;
175
}
176

    
177
static int ch343_control_msg_in(struct ch343 *ch343, u8 request, u8 requesttype, u16 value, u16 index, void *buf,
178
				unsigned bufsize)
179
{
180
	int retval;
181
	char *buffer;
182

    
183
	buffer = kmalloc(bufsize, GFP_KERNEL);
184
	if (!buffer)
185
		return -ENOMEM;
186

    
187
	retval = usb_autopm_get_interface(ch343->control);
188
	if (retval)
189
		goto out;
190
	retval = usb_control_msg(ch343->dev, usb_rcvctrlpipe(ch343->dev, 0), request, requesttype, value, index, buffer,
191
				 bufsize, DEFAULT_TIMEOUT);
192
	if (retval > 0) {
193
		if (copy_to_user((char __user *)buf, buffer, retval)) {
194
			retval = -EFAULT;
195
		}
196
	}
197
	usb_autopm_put_interface(ch343->control);
198

    
199
out:
200
	kfree(buffer);
201
	return retval;
202
}
203

    
204
static inline int ch343_set_control(struct ch343 *ch343, int control)
205
{
206
	if (ch343->iface <= 1)
207
		return ch343_control_out(ch343, CMD_C2 + ch343->iface, ~control, 0x0000);
208
	else if (ch343->iface <= 3)
209
		return ch343_control_out(ch343, CMD_C2 + 0x10 + (ch343->iface - 2), ~control, 0x0000);
210
	else
211
		return -1;
212
}
213

    
214
static inline int ch343_set_line(struct ch343 *ch343, struct usb_cdc_line_coding *line)
215
{
216
	return 0;
217
}
218

    
219
static int ch343_get_status(struct ch343 *ch343)
220
{
221
	char *buffer;
222
	int retval;
223
	const unsigned size = 2;
224
	unsigned long flags;
225

    
226
	buffer = kmalloc(size, GFP_KERNEL);
227
	if (!buffer)
228
		return -ENOMEM;
229

    
230
	retval = ch343_control_in(ch343, CMD_R, CMD_C3 + ch343->iface, 0, buffer, size);
231
	if (retval != size)
232
		goto out;
233

    
234
	spin_lock_irqsave(&ch343->read_lock, flags);
235
	ch343->ctrlin = (~(*buffer)) & CH343_CTI_ST;
236
	spin_unlock_irqrestore(&ch343->read_lock, flags);
237

    
238
out:
239
	kfree(buffer);
240
	return retval;
241
}
242

    
243
static int ch343_configure(struct ch343 *ch343)
244
{
245
	char *buffer;
246
	int r;
247
	const unsigned size = 2;
248
	u8 chiptype;
249

    
250
	buffer = kmalloc(size, GFP_KERNEL);
251
	if (!buffer)
252
		return -ENOMEM;
253

    
254
	r = ch343_control_in(ch343, CMD_C6, 0, 0, buffer, size);
255
	if (r != size)
256
		goto out;
257

    
258
	chiptype = buffer[1];
259

    
260
	switch (ch343->idProduct) {
261
	case 0x55D2:
262
		if (chiptype == 0x48)
263
			ch343->chiptype = CHIP_CH342F;
264
		else if (chiptype == 0x41)
265
			ch343->chiptype = CHIP_CH342K;
266
		break;
267
	case 0x55D3:
268
		if (chiptype == 0x08)
269
			ch343->chiptype = CHIP_CH343GP;
270
		else if (chiptype == 0x02)
271
			ch343->chiptype = CHIP_CH343J;
272
		else if (chiptype == 0x01)
273
			ch343->chiptype = CHIP_CH343K;
274
		else if (chiptype == 0x18)
275
			ch343->chiptype = CHIP_CH343G_AUTOBAUD;
276
		break;
277
	case 0x55D4:
278
		if (chiptype == 0x08)
279
			ch343->chiptype = CHIP_CH9102F;
280
		else if (chiptype == 0x09)
281
			ch343->chiptype = CHIP_CH9102X;
282
		break;
283
	case 0x55D5:
284
		if (chiptype == 0xC0) {
285
			if ((buffer[0] & 0xF0) == 0x40)
286
				ch343->chiptype = CHIP_CH344L;
287
			else
288
				ch343->chiptype = CHIP_CH344L_V2;
289
		} else
290
			ch343->chiptype = CHIP_CH344Q;
291
		break;
292
	case 0x55D7:
293
		if (chiptype == 0x4B)
294
			ch343->chiptype = CHIP_CH9103M;
295
		break;
296
	case 0x55D8:
297
		if (chiptype == 0x08)
298
			ch343->chiptype = CHIP_CH9101UH;
299
		else if (chiptype == 0x0A)
300
			ch343->chiptype = CHIP_CH9101RY;
301
		break;
302
	case 0x55DA:
303
	case 0x55DB:
304
	case 0x55DD:
305
		ch343->chiptype = CHIP_CH347T;
306
		break;
307
	case 0x55DF:
308
		ch343->chiptype = CHIP_CH9104L;
309
		break;
310
	default:
311
		break;
312
	}
313

    
314
	if (ch343->chiptype != CHIP_CH344L && ch343->chiptype != CHIP_CH344L_V2 && ch343->chiptype != CHIP_CH9104L) {
315
		r = ch343_get_status(ch343);
316
		if (r < 0)
317
			goto out;
318
	}
319
out:
320
	kfree(buffer);
321
	return r < 0 ? r : 0;
322
}
323

    
324
static int ch343_wb_alloc(struct ch343 *ch343)
325
{
326
	int i, wbn;
327
	struct ch343_wb *wb;
328

    
329
	wbn = 0;
330
	i = 0;
331
	for (;;) {
332
		wb = &ch343->wb[wbn];
333
		if (!wb->use) {
334
			wb->use = 1;
335
			return wbn;
336
		}
337
		wbn = (wbn + 1) % CH343_NW;
338
		if (++i >= CH343_NW)
339
			return -1;
340
	}
341
}
342

    
343
static int ch343_wb_is_avail(struct ch343 *ch343)
344
{
345
	int i, n;
346
	unsigned long flags;
347

    
348
	n = CH343_NW;
349
	spin_lock_irqsave(&ch343->write_lock, flags);
350
	for (i = 0; i < CH343_NW; i++)
351
		n -= ch343->wb[i].use;
352
	spin_unlock_irqrestore(&ch343->write_lock, flags);
353
	return n;
354
}
355

    
356
static void ch343_write_done(struct ch343 *ch343, struct ch343_wb *wb)
357
{
358
	wb->use = 0;
359
	ch343->transmitting--;
360
	usb_autopm_put_interface_async(ch343->control);
361
}
362

    
363
static int ch343_start_wb(struct ch343 *ch343, struct ch343_wb *wb)
364
{
365
	int rc;
366

    
367
	ch343->transmitting++;
368

    
369
	wb->urb->transfer_buffer = wb->buf;
370
	wb->urb->transfer_dma = wb->dmah;
371
	wb->urb->transfer_buffer_length = wb->len;
372
	wb->urb->dev = ch343->dev;
373

    
374
	rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
375
	if (rc < 0) {
376
		dev_err(&ch343->data->dev, "%s - usb_submit_urb(write bulk) failed: %d\n", __func__, rc);
377
		ch343_write_done(ch343, wb);
378
	}
379
	return rc;
380
}
381

    
382
static void ch343_update_status(struct ch343 *ch343, unsigned char *data, size_t len)
383
{
384
	unsigned long flags;
385
	u8 status;
386
	u8 difference;
387
	u8 type = data[0];
388
	u8 handled = 0;
389

    
390
	if (len < 4)
391
		return;
392

    
393
	if (ch343->chiptype == CHIP_CH344L) {
394
		if (data[0] != 0x00)
395
			return;
396
		type = data[1];
397
	} else if (ch343->chiptype == CHIP_CH344Q || ch343->chiptype == CHIP_CH344L_V2 ||
398
		   ch343->chiptype == CHIP_CH9104L) {
399
		type = data[1];
400
	}
401

    
402
	if (type & CH343_CTT_M) {
403
		status = ~data[len - 1] & CH343_CTI_ST;
404
		if (ch343->chiptype == CHIP_CH344L || ch343->chiptype == CHIP_CH344L_V2)
405
			status &= CH343_CTI_C;
406

    
407
		if (!ch343->clocal && (ch343->ctrlin & status & CH343_CTI_DC)) {
408
			tty_port_tty_hangup(&ch343->port, false);
409
		}
410

    
411
		spin_lock_irqsave(&ch343->read_lock, flags);
412
		difference = status ^ ch343->ctrlin;
413
		ch343->ctrlin = status;
414
		ch343->oldcount = ch343->iocount;
415

    
416
		if (difference) {
417
			if (difference & CH343_CTI_C) {
418
				ch343->iocount.cts++;
419
			}
420
			if (difference & CH343_CTI_DS) {
421
				ch343->iocount.dsr++;
422
			}
423
			if (difference & CH343_CTI_R) {
424
				ch343->iocount.rng++;
425
			}
426
			if (difference & CH343_CTI_DC) {
427
				ch343->iocount.dcd++;
428
			}
429
			spin_unlock_irqrestore(&ch343->read_lock, flags);
430
			wake_up_interruptible(&ch343->wioctl);
431
		} else
432
			spin_unlock_irqrestore(&ch343->read_lock, flags);
433
		handled = 1;
434
	}
435
	if (type & CH343_CTT_O) {
436
		spin_lock_irqsave(&ch343->read_lock, flags);
437
		ch343->oldcount = ch343->iocount;
438
		ch343->iocount.overrun++;
439
		spin_unlock_irqrestore(&ch343->read_lock, flags);
440
		handled = 1;
441
	}
442
	if ((type & CH343_CTT_F) == CH343_CTT_F) {
443
		spin_lock_irqsave(&ch343->read_lock, flags);
444
		ch343->oldcount = ch343->iocount;
445
		ch343->iocount.frame++;
446
		spin_unlock_irqrestore(&ch343->read_lock, flags);
447
		handled = 1;
448
	} else if (type & CH343_CTT_P) {
449
		spin_lock_irqsave(&ch343->read_lock, flags);
450
		ch343->oldcount = ch343->iocount;
451
		ch343->iocount.parity++;
452
		spin_unlock_irqrestore(&ch343->read_lock, flags);
453
		handled = 1;
454
	}
455
	if (!handled)
456
		dev_err(&ch343->control->dev,
457
			"%s - unknown status received:"
458
			"len:%d, data0:0x%x, data1:0x%x\n",
459
			__func__, (int)len, data[0], data[1]);
460
}
461

    
462
static void ch343_ctrl_irq(struct urb *urb)
463
{
464
	struct ch343 *ch343 = urb->context;
465
	unsigned char *data = urb->transfer_buffer;
466
	unsigned int len = urb->actual_length;
467
	int status = urb->status;
468
	int retval;
469

    
470
	switch (status) {
471
	case 0:
472
		/* success */
473
		break;
474
	case -ECONNRESET:
475
	case -ENOENT:
476
	case -ESHUTDOWN:
477
		/* this urb is terminated, clean up */
478
		dev_dbg(&ch343->control->dev, "%s - urb shutting down with status: %d\n", __func__, status);
479
		return;
480
	default:
481
		dev_dbg(&ch343->control->dev, "%s - nonzero urb status received: %d\n", __func__, status);
482
		goto exit;
483
	}
484

    
485
	usb_mark_last_busy(ch343->dev);
486
	ch343_update_status(ch343, data, len);
487
exit:
488
	retval = usb_submit_urb(urb, GFP_ATOMIC);
489
	if (retval && retval != -EPERM)
490
		dev_err(&ch343->control->dev, "%s - usb_submit_urb failed: %d\n", __func__, retval);
491
}
492

    
493
static int ch343_submit_read_urb(struct ch343 *ch343, int index, gfp_t mem_flags)
494
{
495
	int res;
496

    
497
	if (!test_and_clear_bit(index, &ch343->read_urbs_free))
498
		return 0;
499

    
500
	res = usb_submit_urb(ch343->read_urbs[index], mem_flags);
501
	if (res) {
502
		if (res != -EPERM) {
503
			dev_err(&ch343->data->dev, "%s - usb_submit_urb failed: %d\n", __func__, res);
504
		}
505
		set_bit(index, &ch343->read_urbs_free);
506
		return res;
507
	}
508
	return 0;
509
}
510

    
511
static int ch343_submit_read_urbs(struct ch343 *ch343, gfp_t mem_flags)
512
{
513
	int res;
514
	int i;
515

    
516
	for (i = 0; i < ch343->rx_buflimit; ++i) {
517
		res = ch343_submit_read_urb(ch343, i, mem_flags);
518
		if (res)
519
			return res;
520
	}
521
	return 0;
522
}
523

    
524
static void ch343_process_read_urb(struct ch343 *ch343, struct urb *urb)
525
{
526
	if (!urb->actual_length)
527
		return;
528

    
529
	ch343->iocount.rx += urb->actual_length;
530
	tty_insert_flip_string(&ch343->port, urb->transfer_buffer, urb->actual_length);
531
	tty_flip_buffer_push(&ch343->port);
532
}
533

    
534
static void ch343_read_bulk_callback(struct urb *urb)
535
{
536
	struct ch343_rb *rb = urb->context;
537
	struct ch343 *ch343 = rb->instance;
538
	int status = urb->status;
539

    
540
	if (!ch343->dev) {
541
		set_bit(rb->index, &ch343->read_urbs_free);
542
		dev_dbg(&ch343->data->dev, "%s - disconnected\n", __func__);
543
		return;
544
	}
545

    
546
	if (status) {
547
		set_bit(rb->index, &ch343->read_urbs_free);
548
		dev_dbg(&ch343->data->dev, "%s - non-zero urb status: %d\n", __func__, status);
549
		return;
550
	}
551

    
552
	usb_mark_last_busy(ch343->dev);
553
	ch343_process_read_urb(ch343, urb);
554
	set_bit(rb->index, &ch343->read_urbs_free);
555
	ch343_submit_read_urb(ch343, rb->index, GFP_ATOMIC);
556
}
557

    
558
static void ch343_write_bulk(struct urb *urb)
559
{
560
	struct ch343_wb *wb = urb->context;
561
	struct ch343 *ch343 = wb->instance;
562
	unsigned long flags;
563
	int status = urb->status;
564

    
565
	if (status || (urb->actual_length != urb->transfer_buffer_length))
566
		dev_vdbg(&ch343->data->dev, "%s - len %d/%d, status %d\n", __func__, urb->actual_length,
567
			 urb->transfer_buffer_length, status);
568

    
569
	ch343->iocount.tx += urb->actual_length;
570
	spin_lock_irqsave(&ch343->write_lock, flags);
571
	ch343_write_done(ch343, wb);
572
	spin_unlock_irqrestore(&ch343->write_lock, flags);
573
	schedule_work(&ch343->work);
574
}
575

    
576
static void ch343_softint(struct work_struct *work)
577
{
578
	struct ch343 *ch343 = container_of(work, struct ch343, work);
579

    
580
	tty_port_tty_wakeup(&ch343->port);
581
}
582

    
583
static int ch343_tty_install(struct tty_driver *driver, struct tty_struct *tty)
584
{
585
	struct ch343 *ch343;
586
	int retval;
587

    
588
	ch343 = ch343_get_by_minor(tty->index);
589
	if (!ch343)
590
		return -ENODEV;
591

    
592
	retval = tty_standard_install(driver, tty);
593
	if (retval)
594
		goto error_init_termios;
595

    
596
	tty->driver_data = ch343;
597

    
598
	return 0;
599

    
600
error_init_termios:
601
	tty_port_put(&ch343->port);
602
	return retval;
603
}
604

    
605
static int ch343_tty_open(struct tty_struct *tty, struct file *filp)
606
{
607
	struct ch343 *ch343 = tty->driver_data;
608

    
609
	return tty_port_open(&ch343->port, tty, filp);
610
}
611

    
612
static void ch343_port_dtr_rts(struct tty_port *port, int raise)
613
{
614
	struct ch343 *ch343 = container_of(port, struct ch343, port);
615
	int res;
616

    
617
	if (raise)
618
		ch343->ctrlout |= CH343_CTO_D | CH343_CTO_R;
619
	else
620
		ch343->ctrlout &= ~(CH343_CTO_D | CH343_CTO_R);
621

    
622
	res = ch343_set_control(ch343, ch343->ctrlout);
623
	if (res)
624
		dev_err(&ch343->control->dev, "failed to set dtr/rts\n");
625
}
626

    
627
static int ch343_port_activate(struct tty_port *port, struct tty_struct *tty)
628
{
629
	struct ch343 *ch343 = container_of(port, struct ch343, port);
630
	int retval = -ENODEV;
631
	int i;
632

    
633
	mutex_lock(&ch343->mutex);
634
	if (ch343->disconnected)
635
		goto disconnected;
636

    
637
	retval = usb_autopm_get_interface(ch343->control);
638
	if (retval)
639
		goto error_get_interface;
640

    
641
	set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
642
	ch343->control->needs_remote_wakeup = 1;
643

    
644
	ch343_tty_set_termios(tty, NULL);
645

    
646
	retval = usb_submit_urb(ch343->ctrlurb, GFP_KERNEL);
647
	if (retval) {
648
		dev_err(&ch343->control->dev, "%s - usb_submit_urb(ctrl cmd) failed\n", __func__);
649
		goto error_submit_urb;
650
	}
651
	retval = ch343_submit_read_urbs(ch343, GFP_KERNEL);
652
	if (retval)
653
		goto error_submit_read_urbs;
654

    
655
	usb_autopm_put_interface(ch343->control);
656
	mutex_unlock(&ch343->mutex);
657

    
658
	return 0;
659

    
660
error_submit_read_urbs:
661
	for (i = 0; i < ch343->rx_buflimit; i++)
662
		usb_kill_urb(ch343->read_urbs[i]);
663
error_submit_urb:
664
	usb_kill_urb(ch343->ctrlurb);
665
	usb_autopm_put_interface(ch343->control);
666
error_get_interface:
667
disconnected:
668
	mutex_unlock(&ch343->mutex);
669
	return usb_translate_errors(retval);
670
}
671

    
672
static void ch343_port_destruct(struct tty_port *port)
673
{
674
	struct ch343 *ch343 = container_of(port, struct ch343, port);
675

    
676
	ch343_release_minor(ch343);
677
	usb_put_intf(ch343->control);
678
	kfree(ch343);
679
}
680

    
681
static void ch343_port_shutdown(struct tty_port *port)
682
{
683
	struct ch343 *ch343 = container_of(port, struct ch343, port);
684
	struct urb *urb;
685
	struct ch343_wb *wb;
686
	int i;
687

    
688
	usb_autopm_get_interface_no_resume(ch343->control);
689
	ch343->control->needs_remote_wakeup = 0;
690
	usb_autopm_put_interface(ch343->control);
691

    
692
	for (;;) {
693
		urb = usb_get_from_anchor(&ch343->delayed);
694
		if (!urb)
695
			break;
696
		wb = urb->context;
697
		wb->use = 0;
698
		usb_autopm_put_interface_async(ch343->control);
699
	}
700

    
701
	usb_kill_urb(ch343->ctrlurb);
702
	for (i = 0; i < CH343_NW; i++)
703
		usb_kill_urb(ch343->wb[i].urb);
704
	for (i = 0; i < ch343->rx_buflimit; i++)
705
		usb_kill_urb(ch343->read_urbs[i]);
706
}
707

    
708
static void ch343_tty_cleanup(struct tty_struct *tty)
709
{
710
	struct ch343 *ch343 = tty->driver_data;
711
	tty_port_put(&ch343->port);
712
}
713

    
714
static void ch343_tty_hangup(struct tty_struct *tty)
715
{
716
	struct ch343 *ch343 = tty->driver_data;
717
	tty_port_hangup(&ch343->port);
718
}
719

    
720
static void ch343_tty_close(struct tty_struct *tty, struct file *filp)
721
{
722
	struct ch343 *ch343 = tty->driver_data;
723
	tty_port_close(&ch343->port, tty, filp);
724
}
725

    
726
static int ch343_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
727
{
728
	struct ch343 *ch343 = tty->driver_data;
729
	int stat;
730
	unsigned long flags;
731
	int wbn;
732
	struct ch343_wb *wb;
733

    
734
	if (!count)
735
		return 0;
736

    
737
	spin_lock_irqsave(&ch343->write_lock, flags);
738
	wbn = ch343_wb_alloc(ch343);
739
	if (wbn < 0) {
740
		spin_unlock_irqrestore(&ch343->write_lock, flags);
741
		return 0;
742
	}
743
	wb = &ch343->wb[wbn];
744

    
745
	if (!ch343->dev) {
746
		wb->use = 0;
747
		spin_unlock_irqrestore(&ch343->write_lock, flags);
748
		return -ENODEV;
749
	}
750

    
751
	count = (count > ch343->writesize) ? ch343->writesize : count;
752

    
753
	memcpy(wb->buf, buf, count);
754
	wb->len = count;
755

    
756
	stat = usb_autopm_get_interface_async(ch343->control);
757
	if (stat) {
758
		wb->use = 0;
759
		spin_unlock_irqrestore(&ch343->write_lock, flags);
760
		return stat;
761
	}
762

    
763
	if (ch343->susp_count) {
764
		usb_anchor_urb(wb->urb, &ch343->delayed);
765
		spin_unlock_irqrestore(&ch343->write_lock, flags);
766
		return count;
767
	}
768

    
769
	stat = ch343_start_wb(ch343, wb);
770
	spin_unlock_irqrestore(&ch343->write_lock, flags);
771

    
772
	if (stat < 0)
773
		return stat;
774
	return count;
775
}
776

    
777
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0))
778
static unsigned int ch343_tty_write_room(struct tty_struct *tty)
779
#else
780
static int ch343_tty_write_room(struct tty_struct *tty)
781
#endif
782
{
783
	struct ch343 *ch343 = tty->driver_data;
784

    
785
	return ch343_wb_is_avail(ch343) ? ch343->writesize : 0;
786
}
787

    
788
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0))
789
static unsigned int ch343_tty_chars_in_buffer(struct tty_struct *tty)
790
#else
791
static int ch343_tty_chars_in_buffer(struct tty_struct *tty)
792
#endif
793
{
794
	struct ch343 *ch343 = tty->driver_data;
795

    
796
	if (ch343->disconnected)
797
		return 0;
798

    
799
	return (CH343_NW - ch343_wb_is_avail(ch343)) * ch343->writesize;
800
}
801

    
802
static int ch343_tty_break_ctl(struct tty_struct *tty, int state)
803
{
804
	struct ch343 *ch343 = tty->driver_data;
805
	int retval;
806
	uint16_t reg_contents;
807
	uint8_t *regbuf;
808

    
809
	regbuf = kmalloc(2, GFP_KERNEL);
810
	if (!regbuf)
811
		return -1;
812

    
813
	if (state != 0) {
814
		if ((ch343->chiptype == CHIP_CH344L) || (ch343->chiptype == CHIP_CH344Q) ||
815
		    (ch343->chiptype == CHIP_CH344L_V2) || (ch343->chiptype == CHIP_CH9104L)) {
816
			regbuf[0] = ch343->iface;
817
			regbuf[1] = 0x01;
818
		} else {
819
			regbuf[0] = CH343_N_B;
820
			regbuf[1] = 0x00;
821
		}
822
	} else {
823
		if ((ch343->chiptype == CHIP_CH344L) || (ch343->chiptype == CHIP_CH344Q) ||
824
		    (ch343->chiptype == CHIP_CH344L_V2) || (ch343->chiptype == CHIP_CH9104L)) {
825
			regbuf[0] = ch343->iface;
826
			regbuf[1] = 0x00;
827
		} else {
828
			regbuf[0] = CH343_N_B | CH343_N_AB;
829
			regbuf[1] = 0x00;
830
		}
831
	}
832
	reg_contents = get_unaligned_le16(regbuf);
833

    
834
	if ((ch343->chiptype == CHIP_CH344L) || (ch343->chiptype == CHIP_CH344Q) ||
835
	    (ch343->chiptype == CHIP_CH344L_V2) || (ch343->chiptype == CHIP_CH9104L)) {
836
		retval = ch343_control_out(ch343, CMD_C4, reg_contents, 0x00);
837
	} else {
838
		if (ch343->iface)
839
			retval = ch343_control_out(ch343, CMD_C4, 0x00, reg_contents);
840
		else
841
			retval = ch343_control_out(ch343, CMD_C4, reg_contents, 0x00);
842
	}
843

    
844
	if (retval < 0)
845
		dev_err(&ch343->control->dev, "%s - USB control write error (%d)\n", __func__, retval);
846

    
847
	kfree(regbuf);
848
	return retval;
849
}
850

    
851
static int ch343_tty_tiocmget(struct tty_struct *tty)
852
{
853
	struct ch343 *ch343 = tty->driver_data;
854
	unsigned long flags;
855
	unsigned int result;
856

    
857
	spin_lock_irqsave(&ch343->read_lock, flags);
858
	result = (ch343->ctrlout & CH343_CTO_D ? TIOCM_DTR : 0) | (ch343->ctrlout & CH343_CTO_R ? TIOCM_RTS : 0) |
859
		 (ch343->ctrlin & CH343_CTI_C ? TIOCM_CTS : 0) | (ch343->ctrlin & CH343_CTI_DS ? TIOCM_DSR : 0) |
860
		 (ch343->ctrlin & CH343_CTI_R ? TIOCM_RI : 0) | (ch343->ctrlin & CH343_CTI_DC ? TIOCM_CD : 0);
861
	spin_unlock_irqrestore(&ch343->read_lock, flags);
862

    
863
	return result;
864
}
865

    
866
static int ch343_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
867
{
868
	struct ch343 *ch343 = tty->driver_data;
869
	unsigned int newctrl;
870

    
871
	newctrl = ch343->ctrlout;
872
	set = (set & TIOCM_DTR ? CH343_CTO_D : 0) | (set & TIOCM_RTS ? CH343_CTO_R : 0);
873
	clear = (clear & TIOCM_DTR ? CH343_CTO_D : 0) | (clear & TIOCM_RTS ? CH343_CTO_R : 0);
874

    
875
	newctrl = (newctrl & ~clear) | set;
876

    
877
	if (ch343->ctrlout == newctrl) {
878
		return 0;
879
	}
880

    
881
	return ch343_set_control(ch343, ch343->ctrlout = newctrl);
882
}
883

    
884
static int ch343_get_icount(struct tty_struct *tty, struct serial_icounter_struct *icount)
885
{
886
	struct ch343 *ch343 = tty->driver_data;
887
	struct async_icount cnow;
888
	unsigned long flags;
889

    
890
	spin_lock_irqsave(&ch343->read_lock, flags);
891
	cnow = ch343->iocount;
892
	spin_unlock_irqrestore(&ch343->read_lock, flags);
893

    
894
	icount->cts = cnow.cts;
895
	icount->dsr = cnow.dsr;
896
	icount->rng = cnow.rng;
897
	icount->dcd = cnow.dcd;
898
	icount->tx = cnow.tx;
899
	icount->rx = cnow.rx;
900
	icount->frame = cnow.frame;
901
	icount->parity = cnow.parity;
902
	icount->overrun = cnow.overrun;
903
	icount->brk = cnow.brk;
904
	icount->buf_overrun = cnow.buf_overrun;
905

    
906
	return 0;
907
}
908

    
909
static int ch343_get_serial_info(struct ch343 *ch343, struct serial_struct __user *info)
910
{
911
	struct serial_struct tmp;
912

    
913
	if (!info)
914
		return -EINVAL;
915

    
916
	memset(&tmp, 0, sizeof(tmp));
917
	tmp.flags = ASYNC_LOW_LATENCY;
918
	tmp.xmit_fifo_size = ch343->writesize;
919
	tmp.baud_base = le32_to_cpu(ch343->line.dwDTERate);
920
	tmp.close_delay = ch343->port.close_delay / 10;
921
	tmp.closing_wait = ch343->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? ASYNC_CLOSING_WAIT_NONE :
922
										 ch343->port.closing_wait / 10;
923

    
924
	if (copy_to_user(info, &tmp, sizeof(tmp)))
925
		return -EFAULT;
926
	else
927
		return 0;
928
}
929

    
930
static int ch343_set_serial_info(struct ch343 *ch343, struct serial_struct __user *newinfo)
931
{
932
	struct serial_struct new_serial;
933
	unsigned int closing_wait, close_delay;
934
	int retval = 0;
935

    
936
	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
937
		return -EFAULT;
938

    
939
	close_delay = new_serial.close_delay * 10;
940
	closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? ASYNC_CLOSING_WAIT_NONE :
941
									    new_serial.closing_wait * 10;
942

    
943
	mutex_lock(&ch343->port.mutex);
944

    
945
	if (!capable(CAP_SYS_ADMIN)) {
946
		if ((close_delay != ch343->port.close_delay) || (closing_wait != ch343->port.closing_wait))
947
			retval = -EPERM;
948
		else
949
			retval = -EOPNOTSUPP;
950
	} else {
951
		ch343->port.close_delay = close_delay;
952
		ch343->port.closing_wait = closing_wait;
953
	}
954

    
955
	mutex_unlock(&ch343->port.mutex);
956
	return retval;
957
}
958

    
959
static int ch343_wait_serial_change(struct ch343 *ch343, unsigned long arg)
960
{
961
	int rv = 0;
962
	DECLARE_WAITQUEUE(wait, current);
963
	struct async_icount old, new;
964

    
965
	do {
966
		spin_lock_irq(&ch343->read_lock);
967
		old = ch343->oldcount;
968
		new = ch343->iocount;
969
		ch343->oldcount = new;
970
		spin_unlock_irq(&ch343->read_lock);
971

    
972
		if ((arg & TIOCM_CTS) && old.cts != new.cts)
973
			break;
974
		if ((arg & TIOCM_DSR) && old.dsr != new.dsr)
975
			break;
976
		if ((arg & TIOCM_RI) && old.rng != new.rng)
977
			break;
978
		if ((arg & TIOCM_CD) && old.dcd != new.dcd)
979
			break;
980

    
981
		add_wait_queue(&ch343->wioctl, &wait);
982
		set_current_state(TASK_INTERRUPTIBLE);
983
		schedule();
984
		remove_wait_queue(&ch343->wioctl, &wait);
985
		if (ch343->disconnected) {
986
			if (arg & TIOCM_CD)
987
				break;
988
			else
989
				rv = -ENODEV;
990
		} else {
991
			if (signal_pending(current))
992
				rv = -ERESTARTSYS;
993
		}
994
	} while (!rv);
995

    
996
	return rv;
997
}
998

    
999
static int ch343_get_serial_usage(struct ch343 *ch343, struct serial_icounter_struct __user *count)
1000
{
1001
	struct serial_icounter_struct icount;
1002
	int rv = 0;
1003

    
1004
	memset(&icount, 0, sizeof(icount));
1005
	icount.cts = ch343->iocount.cts;
1006
	icount.dsr = ch343->iocount.dsr;
1007
	icount.rng = ch343->iocount.rng;
1008
	icount.dcd = ch343->iocount.dcd;
1009
	icount.tx = ch343->iocount.tx;
1010
	icount.rx = ch343->iocount.rx;
1011
	icount.frame = ch343->iocount.frame;
1012
	icount.overrun = ch343->iocount.overrun;
1013
	icount.parity = ch343->iocount.parity;
1014
	icount.brk = ch343->iocount.brk;
1015
	icount.buf_overrun = ch343->iocount.buf_overrun;
1016

    
1017
	if (copy_to_user(count, &icount, sizeof(icount)) > 0)
1018
		rv = -EFAULT;
1019

    
1020
	return rv;
1021
}
1022

    
1023
static int ch343_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1024
{
1025
	struct ch343 *ch343 = tty->driver_data;
1026
	int rv = 0;
1027
	unsigned long arg1, arg2, arg3, arg4, arg5, arg6;
1028
	u32 __user *argval = (u32 __user *)arg;
1029
	u8 *buffer;
1030

    
1031
	buffer = kmalloc(512, GFP_KERNEL);
1032
	if (!buffer)
1033
		return -ENOMEM;
1034

    
1035
	switch (cmd) {
1036
	case TIOCGSERIAL: /* gets serial port data */
1037
		rv = ch343_get_serial_info(ch343, (struct serial_struct __user *)arg);
1038
		break;
1039
	case TIOCSSERIAL:
1040
		rv = ch343_set_serial_info(ch343, (struct serial_struct __user *)arg);
1041
		break;
1042
	case TIOCMIWAIT:
1043
		rv = usb_autopm_get_interface(ch343->control);
1044
		if (rv < 0) {
1045
			rv = -EIO;
1046
			break;
1047
		}
1048
		rv = ch343_wait_serial_change(ch343, arg);
1049
		usb_autopm_put_interface(ch343->control);
1050
		break;
1051
	case TIOCGICOUNT:
1052
		rv = ch343_get_serial_usage(ch343, (struct serial_icounter_struct __user *)arg);
1053
		break;
1054
	case IOCTL_CMD_GETCHIPTYPE:
1055
		if (put_user(ch343->chiptype, argval)) {
1056
			rv = -EFAULT;
1057
			goto out;
1058
		}
1059
		break;
1060
	case IOCTL_CMD_CTRLIN:
1061
		get_user(arg1, (u8 __user *)arg);
1062
		get_user(arg2, ((u8 __user *)arg + 1));
1063
		get_user(arg3, (u16 __user *)((u8 *)arg + 2));
1064
		get_user(arg4, (u16 __user *)((u8 *)arg + 4));
1065
		get_user(arg5, (u16 __user *)((u8 *)arg + 6));
1066
		arg6 = (unsigned long)((u8 __user *)arg + 8);
1067
		rv = ch343_control_msg_in(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6,
1068
					  (u16)arg5);
1069
		break;
1070
	case IOCTL_CMD_CTRLOUT:
1071
		get_user(arg1, (u8 __user *)arg);
1072
		get_user(arg2, ((u8 __user *)arg + 1));
1073
		get_user(arg3, (u16 __user *)((u8 *)arg + 2));
1074
		get_user(arg4, (u16 __user *)((u8 *)arg + 4));
1075
		get_user(arg5, (u16 __user *)((u8 *)arg + 6));
1076
		arg6 = (unsigned long)((u8 __user *)arg + 8);
1077
		rv = ch343_control_msg_out(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6,
1078
					   (u16)arg5);
1079
		if (rv != (u16)arg5) {
1080
			rv = -EINVAL;
1081
			goto out;
1082
		}
1083
		break;
1084
	default:
1085
		rv = -ENOIOCTLCMD;
1086
		break;
1087
	}
1088
out:
1089
	kfree(buffer);
1090
	return rv < 0 ? rv : 0;
1091
}
1092

    
1093
static int ch343_get(CHIPTYPE chiptype, unsigned int bval, unsigned char *fct, unsigned char *dvs)
1094
{
1095
	unsigned char a;
1096
	unsigned char b;
1097
	unsigned long c;
1098

    
1099
	if (((chiptype == CHIP_CH347T) || (chiptype == CHIP_CH344Q) || (chiptype == CHIP_CH9104L)) && bval >= 2000000) {
1100
		*fct = (unsigned char)(bval / 200);
1101
		*dvs = (unsigned char)((bval / 200) >> 8);
1102
	}
1103

    
1104
	switch (bval) {
1105
	case 6000000:
1106
	case 4000000:
1107
	case 2400000:
1108
	case 921600:
1109
	case 307200:
1110
	case 256000:
1111
		b = 7;
1112
		c = 12000000;
1113
		break;
1114
	default:
1115
		if (bval > 6000000 / 255) {
1116
			b = 3;
1117
			c = 6000000;
1118
		} else if (bval > 750000 / 255) {
1119
			b = 2;
1120
			c = 750000;
1121
		} else if (bval > 93750 / 255) {
1122
			b = 1;
1123
			c = 93750;
1124
		} else {
1125
			b = 0;
1126
			c = 11719;
1127
		}
1128
		break;
1129
	}
1130
	a = (unsigned char)(c / bval);
1131
	if (a == 0 || a == 0xFF)
1132
		return -EINVAL;
1133
	if ((c / a - bval) > (bval - c / (a + 1)))
1134
		a++;
1135
	a = 256 - a;
1136

    
1137
	*fct = a;
1138
	*dvs = b;
1139

    
1140
	return 0;
1141
}
1142

    
1143
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
1144
static void ch343_tty_set_termios(struct tty_struct *tty, const struct ktermios *termios_old)
1145
#else
1146
static void ch343_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
1147
#endif
1148
{
1149
	struct ch343 *ch343 = tty->driver_data;
1150
	struct ktermios *termios = &tty->termios;
1151
	struct usb_ch343_line_coding newline;
1152
	int newctrl = ch343->ctrlout;
1153

    
1154
	unsigned char dvs = 0;
1155
	unsigned char reg_count = 0;
1156
	unsigned char fct = 0;
1157
	unsigned char reg_value = 0;
1158
	unsigned short value = 0;
1159
	unsigned short index = 0;
1160

    
1161
	if (termios_old && !tty_termios_hw_change(&tty->termios, termios_old)) {
1162
		return;
1163
	}
1164

    
1165
	newline.dwDTERate = tty_get_baud_rate(tty);
1166

    
1167
	if (newline.dwDTERate == 0)
1168
		newline.dwDTERate = 9600;
1169
	ch343_get(ch343->chiptype, newline.dwDTERate, &fct, &dvs);
1170

    
1171
	newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 1;
1172
	if (newline.bCharFormat == 2)
1173
		reg_value |= CH343_L_SB;
1174

    
1175
	newline.bParityType = termios->c_cflag & PARENB ?
1176
				      (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) :
1177
				      0;
1178

    
1179
	switch (newline.bParityType) {
1180
	case 0x01:
1181
		reg_value |= CH343_L_P_O;
1182
		break;
1183
	case 0x02:
1184
		reg_value |= CH343_L_P_E;
1185
		break;
1186
	case 0x03:
1187
		reg_value |= CH343_L_P_M;
1188
		break;
1189
	case 0x04:
1190
		reg_value |= CH343_L_P_S;
1191
		break;
1192
	default:
1193
		break;
1194
	}
1195

    
1196
	switch (termios->c_cflag & CSIZE) {
1197
	case CS5:
1198
		newline.bDataBits = 5;
1199
		reg_value |= CH343_L_C5;
1200
		break;
1201
	case CS6:
1202
		newline.bDataBits = 6;
1203
		reg_value |= CH343_L_C6;
1204
		break;
1205
	case CS7:
1206
		newline.bDataBits = 7;
1207
		reg_value |= CH343_L_C7;
1208
		break;
1209
	case CS8:
1210
	default:
1211
		newline.bDataBits = 8;
1212
		reg_value |= CH343_L_C8;
1213
		break;
1214
	}
1215

    
1216
	ch343->clocal = ((termios->c_cflag & CLOCAL) != 0);
1217

    
1218
	if (C_BAUD(tty) == B0) {
1219
		newline.dwDTERate = ch343->line.dwDTERate;
1220
		newctrl &= ~CH343_CTO_D;
1221
	} else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
1222
		newctrl |= CH343_CTO_D;
1223
	}
1224

    
1225
	reg_value |= CH343_L_E_R | CH343_L_E_T;
1226
	reg_count |= CH343_L_R_CT | CH343_L_R_CL | CH343_L_R_T;
1227

    
1228
	value |= reg_count;
1229
	value |= (unsigned short)reg_value << 8;
1230

    
1231
	index |= 0x00 | dvs;
1232
	index |= (unsigned short)fct << 8;
1233
	if (ch343->iface <= 1)
1234
		ch343_control_out(ch343, CMD_C1 + ch343->iface, value, index);
1235
	else if (ch343->iface <= 3)
1236
		ch343_control_out(ch343, CMD_C1 + 0x10 + (ch343->iface - 2), value, index);
1237

    
1238
	if (memcmp(&ch343->line, &newline, sizeof newline))
1239
		memcpy(&ch343->line, &newline, sizeof newline);
1240

    
1241
	if (C_CRTSCTS(tty)) {
1242
		newctrl |= CH343_CTO_A | CH343_CTO_R;
1243
	} else
1244
		newctrl &= ~CH343_CTO_A;
1245

    
1246
	if (newctrl != ch343->ctrlout)
1247
		ch343_set_control(ch343, ch343->ctrlout = newctrl);
1248
}
1249

    
1250
static const struct tty_port_operations ch343_port_ops = {
1251
	.dtr_rts = ch343_port_dtr_rts,
1252
	.shutdown = ch343_port_shutdown,
1253
	.activate = ch343_port_activate,
1254
	.destruct = ch343_port_destruct,
1255
};
1256

    
1257
static void ch343_write_buffers_free(struct ch343 *ch343)
1258
{
1259
	int i;
1260
	struct ch343_wb *wb;
1261
	struct usb_device *usb_dev = interface_to_usbdev(ch343->control);
1262

    
1263
	for (wb = &ch343->wb[0], i = 0; i < CH343_NW; i++, wb++)
1264
		usb_free_coherent(usb_dev, ch343->writesize, wb->buf, wb->dmah);
1265
}
1266

    
1267
static void ch343_read_buffers_free(struct ch343 *ch343)
1268
{
1269
	struct usb_device *usb_dev = interface_to_usbdev(ch343->control);
1270
	int i;
1271

    
1272
	for (i = 0; i < ch343->rx_buflimit; i++)
1273
		usb_free_coherent(usb_dev, ch343->readsize, ch343->read_buffers[i].base, ch343->read_buffers[i].dma);
1274
}
1275

    
1276
static int ch343_write_buffers_alloc(struct ch343 *ch343)
1277
{
1278
	int i;
1279
	struct ch343_wb *wb;
1280

    
1281
	for (wb = &ch343->wb[0], i = 0; i < CH343_NW; i++, wb++) {
1282
		wb->buf = usb_alloc_coherent(ch343->dev, ch343->writesize, GFP_KERNEL, &wb->dmah);
1283
		if (!wb->buf) {
1284
			while (i != 0) {
1285
				--i;
1286
				--wb;
1287
				usb_free_coherent(ch343->dev, ch343->writesize, wb->buf, wb->dmah);
1288
			}
1289
			return -ENOMEM;
1290
		}
1291
	}
1292
	return 0;
1293
}
1294

    
1295
static int ch343_open(struct inode *inode, struct file *file)
1296
{
1297
	struct ch343 *ch343;
1298
	struct usb_interface *interface;
1299
	int subminor;
1300
	int retval = 0;
1301

    
1302
	subminor = iminor(inode);
1303

    
1304
	interface = usb_find_interface(&ch343_driver, subminor);
1305
	if (!interface) {
1306
		retval = -ENODEV;
1307
		goto exit;
1308
	}
1309

    
1310
	ch343 = usb_get_intfdata(interface);
1311
	if (!ch343) {
1312
		retval = -ENODEV;
1313
		goto exit;
1314
	}
1315

    
1316
	file->private_data = ch343;
1317

    
1318
exit:
1319
	return retval;
1320
}
1321

    
1322
static int ch343_release(struct inode *inode, struct file *file)
1323
{
1324
	struct ch343 *ch343;
1325

    
1326
	ch343 = file->private_data;
1327
	if (ch343 == NULL)
1328
		return -ENODEV;
1329

    
1330
	return 0;
1331
}
1332

    
1333
static long ch343_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1334
{
1335
	struct ch343 *ch343;
1336
	int rv = 0;
1337
	u8 *buffer;
1338
	unsigned long arg1, arg2, arg3, arg4, arg5, arg6;
1339
	u32 __user *argval = (u32 __user *)arg;
1340

    
1341
	ch343 = file->private_data;
1342
	if (ch343 == NULL)
1343
		return -ENODEV;
1344

    
1345
	buffer = kmalloc(512, GFP_KERNEL);
1346
	if (!buffer)
1347
		return -ENOMEM;
1348

    
1349
	switch (cmd) {
1350
	case IOCTL_CMD_GETCHIPTYPE:
1351
		if (put_user(ch343->chiptype, argval)) {
1352
			rv = -EFAULT;
1353
			goto out;
1354
		}
1355
		break;
1356
	case IOCTL_CMD_CTRLIN:
1357
		get_user(arg1, (u8 __user *)arg);
1358
		get_user(arg2, ((u8 __user *)arg + 1));
1359
		get_user(arg3, (u16 __user *)((u8 *)arg + 2));
1360
		get_user(arg4, (u16 __user *)((u8 *)arg + 4));
1361
		get_user(arg5, (u16 __user *)((u8 *)arg + 6));
1362
		arg6 = (unsigned long)((u8 __user *)arg + 8);
1363
		rv = ch343_control_msg_in(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6,
1364
					  (u16)arg5);
1365
		break;
1366
	case IOCTL_CMD_CTRLOUT:
1367
		get_user(arg1, (u8 __user *)arg);
1368
		get_user(arg2, ((u8 __user *)arg + 1));
1369
		get_user(arg3, (u16 __user *)((u8 *)arg + 2));
1370
		get_user(arg4, (u16 __user *)((u8 *)arg + 4));
1371
		get_user(arg5, (u16 __user *)((u8 *)arg + 6));
1372
		arg6 = (unsigned long)((u8 __user *)arg + 8);
1373
		rv = ch343_control_msg_out(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6,
1374
					   (u16)arg5);
1375
		if (rv != (u16)arg5) {
1376
			rv = -EINVAL;
1377
			goto out;
1378
		}
1379
		break;
1380
	default:
1381
		rv = -ENOIOCTLCMD;
1382
		break;
1383
	}
1384

    
1385
out:
1386
	kfree(buffer);
1387
	return rv;
1388
}
1389

    
1390
static const struct file_operations ch343_fops = {
1391
	.owner = THIS_MODULE,
1392
	.open = ch343_open,
1393
	.unlocked_ioctl = ch343_ioctl,
1394
	.release = ch343_release,
1395
};
1396

    
1397
/*
1398
 * usb class driver info in order to get a minor number from the usb core,
1399
 * and to have the device registered with the driver core
1400
 */
1401
static struct usb_class_driver ch343_class = {
1402
	.name = "ch343_iodev%d",
1403
	.fops = &ch343_fops,
1404
	.minor_base = USB_MINOR_BASE,
1405
};
1406

    
1407
/*
1408
 * USB probe and disconnect routines.
1409
 */
1410
static int ch343_probe(struct usb_interface *intf, const struct usb_device_id *id)
1411
{
1412
	struct usb_cdc_union_desc *union_header = NULL;
1413
	unsigned char *buffer = intf->altsetting->extra;
1414
	int buflen = intf->altsetting->extralen;
1415
	struct usb_interface *control_interface;
1416
	struct usb_interface *data_interface;
1417
	struct usb_endpoint_descriptor *epctrl = NULL;
1418
	struct usb_endpoint_descriptor *epread = NULL;
1419
	struct usb_endpoint_descriptor *epwrite = NULL;
1420
	struct usb_device *usb_dev = interface_to_usbdev(intf);
1421
	struct ch343 *ch343;
1422
	int minor;
1423
	int ctrlsize, readsize;
1424
	u8 *buf;
1425
	unsigned long quirks;
1426
	int num_rx_buf = CH343_NR;
1427
	int i;
1428
	unsigned int elength = 0;
1429
	struct device *tty_dev;
1430
	int rv = -ENOMEM;
1431

    
1432
	quirks = (unsigned long)id->driver_info;
1433
	if (!buffer) {
1434
		dev_err(&intf->dev, "Weird descriptor references\n");
1435
		return -EINVAL;
1436
	}
1437

    
1438
	while (buflen > 0) {
1439
		elength = buffer[0];
1440
		if (!elength) {
1441
			dev_err(&intf->dev, "skipping garbage byte\n");
1442
			elength = 1;
1443
			goto next_desc;
1444
		}
1445
		if (buffer[1] != USB_DT_CS_INTERFACE) {
1446
			dev_err(&intf->dev, "skipping garbage\n");
1447
			goto next_desc;
1448
		}
1449

    
1450
		switch (buffer[2]) {
1451
		case USB_CDC_UNION_TYPE:
1452
			if (elength < sizeof(struct usb_cdc_union_desc))
1453
				goto next_desc;
1454
			if (union_header) {
1455
				dev_err(&intf->dev, "More than one "
1456
						    "union descriptor, skipping ...\n");
1457
				goto next_desc;
1458
			}
1459
			union_header = (struct usb_cdc_union_desc *)buffer;
1460
			break;
1461
		default:
1462
			/*
1463
             * there are LOTS more CDC descriptors that
1464
             * could legitimately be found here.
1465
             */
1466
			break;
1467
		}
1468
next_desc:
1469
		buflen -= elength;
1470
		buffer += elength;
1471
	}
1472

    
1473
	control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1474
	data_interface = usb_ifnum_to_if(usb_dev, union_header->bSlaveInterface0);
1475

    
1476
	if (intf != control_interface)
1477
		return -ENODEV;
1478

    
1479
	if (usb_interface_claimed(data_interface)) {
1480
		dev_err(&intf->dev, "The data interface isn't available\n");
1481
		return -EBUSY;
1482
	}
1483

    
1484
	if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1485
	    control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1486
		return -EINVAL;
1487

    
1488
	epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1489
	epwrite = &data_interface->cur_altsetting->endpoint[0].desc;
1490
	epread = &data_interface->cur_altsetting->endpoint[1].desc;
1491

    
1492
	if (!usb_endpoint_dir_in(epread))
1493
		swap(epread, epwrite);
1494

    
1495
	ch343 = kzalloc(sizeof(struct ch343), GFP_KERNEL);
1496
	if (ch343 == NULL)
1497
		goto alloc_fail;
1498

    
1499
	ch343->idVendor = id->idVendor;
1500
	ch343->idProduct = id->idProduct;
1501
	ch343->iface = control_interface->cur_altsetting->desc.bInterfaceNumber / 2;
1502

    
1503
	minor = ch343_alloc_minor(ch343);
1504
	if (minor < 0) {
1505
		dev_err(&intf->dev, "no more free ch343 devices\n");
1506
		kfree(ch343);
1507
		return -ENODEV;
1508
	}
1509

    
1510
	ctrlsize = usb_endpoint_maxp(epctrl);
1511
	readsize = usb_endpoint_maxp(epread);
1512
	ch343->writesize = usb_endpoint_maxp(epwrite) * 20;
1513
	ch343->control = control_interface;
1514
	ch343->data = data_interface;
1515
	ch343->minor = minor;
1516
	ch343->dev = usb_dev;
1517
	ch343->ctrlsize = ctrlsize;
1518
	ch343->readsize = readsize;
1519
	ch343->rx_buflimit = num_rx_buf;
1520

    
1521
	INIT_WORK(&ch343->work, ch343_softint);
1522
	init_waitqueue_head(&ch343->wioctl);
1523
	spin_lock_init(&ch343->write_lock);
1524
	spin_lock_init(&ch343->read_lock);
1525
	mutex_init(&ch343->mutex);
1526
	ch343->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1527
	tty_port_init(&ch343->port);
1528
	ch343->port.ops = &ch343_port_ops;
1529
	init_usb_anchor(&ch343->delayed);
1530
	ch343->quirks = quirks;
1531

    
1532
	buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &ch343->ctrl_dma);
1533
	if (!buf)
1534
		goto alloc_fail2;
1535
	ch343->ctrl_buffer = buf;
1536

    
1537
	if (ch343_write_buffers_alloc(ch343) < 0)
1538
		goto alloc_fail4;
1539

    
1540
	ch343->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1541
	if (!ch343->ctrlurb)
1542
		goto alloc_fail5;
1543

    
1544
	for (i = 0; i < num_rx_buf; i++) {
1545
		struct ch343_rb *rb = &(ch343->read_buffers[i]);
1546
		struct urb *urb;
1547

    
1548
		rb->base = usb_alloc_coherent(ch343->dev, readsize, GFP_KERNEL, &rb->dma);
1549
		if (!rb->base)
1550
			goto alloc_fail6;
1551
		rb->index = i;
1552
		rb->instance = ch343;
1553

    
1554
		urb = usb_alloc_urb(0, GFP_KERNEL);
1555
		if (!urb)
1556
			goto alloc_fail6;
1557

    
1558
		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1559
		urb->transfer_dma = rb->dma;
1560
		usb_fill_bulk_urb(urb, ch343->dev, ch343->rx_endpoint, rb->base, ch343->readsize,
1561
				  ch343_read_bulk_callback, rb);
1562

    
1563
		ch343->read_urbs[i] = urb;
1564
		__set_bit(i, &ch343->read_urbs_free);
1565
	}
1566
	for (i = 0; i < CH343_NW; i++) {
1567
		struct ch343_wb *snd = &(ch343->wb[i]);
1568

    
1569
		snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1570
		if (snd->urb == NULL)
1571
			goto alloc_fail7;
1572

    
1573
		usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), NULL,
1574
				  ch343->writesize, ch343_write_bulk, snd);
1575
		snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1576
		snd->instance = ch343;
1577
	}
1578

    
1579
	usb_set_intfdata(intf, ch343);
1580

    
1581
	usb_fill_int_urb(ch343->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), ch343->ctrl_buffer,
1582
			 ctrlsize, ch343_ctrl_irq, ch343, epctrl->bInterval ? epctrl->bInterval : 16);
1583
	ch343->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1584
	ch343->ctrlurb->transfer_dma = ch343->ctrl_dma;
1585

    
1586
	dev_info(&intf->dev, "ttyCH343USB%d: usb to uart device\n", minor);
1587

    
1588
	usb_driver_claim_interface(&ch343_driver, data_interface, ch343);
1589
	usb_set_intfdata(data_interface, ch343);
1590
	usb_get_intf(control_interface);
1591

    
1592
	rv = ch343_configure(ch343);
1593
	if (rv)
1594
		goto alloc_fail7;
1595

    
1596
	if (ch343->iface == 0) {
1597
		/* register the device now, as it is ready */
1598
		rv = usb_register_dev(intf, &ch343_class);
1599
		if (rv) {
1600
			/* error when registering this driver */
1601
			dev_err(&intf->dev, "Not able to get a minor for this device.\n");
1602
		} else {
1603
			g_intf = intf;
1604
		}
1605
	}
1606

    
1607
	tty_dev = tty_port_register_device(&ch343->port, ch343_tty_driver, minor, &control_interface->dev);
1608
	if (IS_ERR(tty_dev)) {
1609
		rv = PTR_ERR(tty_dev);
1610
		goto alloc_fail7;
1611
	}
1612

    
1613
	return 0;
1614

    
1615
alloc_fail7:
1616
	usb_set_intfdata(intf, NULL);
1617
	for (i = 0; i < CH343_NW; i++)
1618
		usb_free_urb(ch343->wb[i].urb);
1619
alloc_fail6:
1620
	for (i = 0; i < num_rx_buf; i++)
1621
		usb_free_urb(ch343->read_urbs[i]);
1622
	ch343_read_buffers_free(ch343);
1623
	usb_free_urb(ch343->ctrlurb);
1624
alloc_fail5:
1625
	ch343_write_buffers_free(ch343);
1626
alloc_fail4:
1627
	usb_free_coherent(usb_dev, ctrlsize, ch343->ctrl_buffer, ch343->ctrl_dma);
1628
alloc_fail2:
1629
	ch343_release_minor(ch343);
1630
	kfree(ch343);
1631
alloc_fail:
1632
	return rv;
1633
}
1634

    
1635
static void stop_data_traffic(struct ch343 *ch343)
1636
{
1637
	int i;
1638

    
1639
	usb_kill_urb(ch343->ctrlurb);
1640
	for (i = 0; i < CH343_NW; i++)
1641
		usb_kill_urb(ch343->wb[i].urb);
1642
	for (i = 0; i < ch343->rx_buflimit; i++)
1643
		usb_kill_urb(ch343->read_urbs[i]);
1644
	cancel_work_sync(&ch343->work);
1645
}
1646

    
1647
static void ch343_disconnect(struct usb_interface *intf)
1648
{
1649
	struct ch343 *ch343 = usb_get_intfdata(intf);
1650
	struct usb_device *usb_dev = interface_to_usbdev(intf);
1651
	struct tty_struct *tty;
1652
	int i;
1653

    
1654
	/* sibling interface is already cleaning up */
1655
	if (!ch343)
1656
		return;
1657

    
1658
	/* give back minor */
1659
	if ((ch343->iface == 0) && (g_intf != NULL)) {
1660
		usb_deregister_dev(g_intf, &ch343_class);
1661
	}
1662

    
1663
	mutex_lock(&ch343->mutex);
1664
	ch343->disconnected = true;
1665
	wake_up_all(&ch343->wioctl);
1666
	usb_set_intfdata(ch343->control, NULL);
1667
	usb_set_intfdata(ch343->data, NULL);
1668
	mutex_unlock(&ch343->mutex);
1669

    
1670
	tty = tty_port_tty_get(&ch343->port);
1671
	if (tty) {
1672
		tty_vhangup(tty);
1673
		tty_kref_put(tty);
1674
	}
1675

    
1676
	stop_data_traffic(ch343);
1677
	tty_unregister_device(ch343_tty_driver, ch343->minor);
1678

    
1679
	usb_free_urb(ch343->ctrlurb);
1680
	for (i = 0; i < CH343_NW; i++)
1681
		usb_free_urb(ch343->wb[i].urb);
1682
	for (i = 0; i < ch343->rx_buflimit; i++)
1683
		usb_free_urb(ch343->read_urbs[i]);
1684
	ch343_write_buffers_free(ch343);
1685
	usb_free_coherent(usb_dev, ch343->ctrlsize, ch343->ctrl_buffer, ch343->ctrl_dma);
1686
	ch343_read_buffers_free(ch343);
1687

    
1688
	usb_driver_release_interface(&ch343_driver, intf == ch343->control ? ch343->data : ch343->control);
1689
	tty_port_put(&ch343->port);
1690
	dev_info(&intf->dev, "%s\n", "ch343 usb device disconnect.");
1691
}
1692

    
1693
#ifdef CONFIG_PM
1694
static int ch343_suspend(struct usb_interface *intf, pm_message_t message)
1695
{
1696
	struct ch343 *ch343 = usb_get_intfdata(intf);
1697
	int cnt;
1698

    
1699
	spin_lock_irq(&ch343->write_lock);
1700
	if (PMSG_IS_AUTO(message)) {
1701
		if (ch343->transmitting) {
1702
			spin_unlock_irq(&ch343->write_lock);
1703
			return -EBUSY;
1704
		}
1705
	}
1706
	cnt = ch343->susp_count++;
1707
	spin_unlock_irq(&ch343->write_lock);
1708
	if (cnt)
1709
		return 0;
1710
	stop_data_traffic(ch343);
1711

    
1712
	return 0;
1713
}
1714

    
1715
static int ch343_resume(struct usb_interface *intf)
1716
{
1717
	struct ch343 *ch343 = usb_get_intfdata(intf);
1718
	struct urb *urb;
1719
	int rv = 0;
1720

    
1721
	spin_lock_irq(&ch343->write_lock);
1722
	if (--ch343->susp_count)
1723
		goto out;
1724

    
1725
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0))
1726
	if (tty_port_initialized(&ch343->port)) {
1727
#else
1728
	if (test_bit(ASYNCB_INITIALIZED, &ch343->port.flags)) {
1729
#endif
1730
		rv = usb_submit_urb(ch343->ctrlurb, GFP_ATOMIC);
1731
		for (;;) {
1732
			urb = usb_get_from_anchor(&ch343->delayed);
1733
			if (!urb)
1734
				break;
1735

    
1736
			ch343_start_wb(ch343, urb->context);
1737
		}
1738
		if (rv < 0)
1739
			goto out;
1740
		rv = ch343_submit_read_urbs(ch343, GFP_ATOMIC);
1741
	}
1742
out:
1743
	spin_unlock_irq(&ch343->write_lock);
1744
	return rv;
1745
}
1746

    
1747
static int ch343_reset_resume(struct usb_interface *intf)
1748
{
1749
	struct ch343 *ch343 = usb_get_intfdata(intf);
1750

    
1751
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0))
1752
	if (tty_port_initialized(&ch343->port))
1753
#else
1754
	if (test_bit(ASYNCB_INITIALIZED, &ch343->port.flags))
1755
#endif
1756
		tty_port_tty_hangup(&ch343->port, false);
1757

    
1758
	return ch343_resume(intf);
1759
}
1760
#endif /* CONFIG_PM */
1761

    
1762
static const struct usb_device_id ch343_ids[] = {
1763
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55d2, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch342 chip */
1764
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55d3, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch343 chip */
1765
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55d5, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch344 chip */
1766
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55da, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch347 chip mode0*/
1767
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55db, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch347 chip mode1*/
1768
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55dd, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch347 chip mode3*/
1769
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55d8, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch9101 chip */
1770
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55d4, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch9102 chip */
1771
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55d7, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch9103 chip */
1772
	{ USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55df, USB_CDC_ACM_PROTO_AT_V25TER) }, /* ch9104 chip */
1773
	{}
1774
};
1775

    
1776
MODULE_DEVICE_TABLE(usb, ch343_ids);
1777

    
1778
static struct usb_driver ch343_driver = {
1779
	.name = "usb_ch343",
1780
	.probe = ch343_probe,
1781
	.disconnect = ch343_disconnect,
1782
#ifdef CONFIG_PM
1783
	.suspend = ch343_suspend,
1784
	.resume = ch343_resume,
1785
	.reset_resume = ch343_reset_resume,
1786
#endif
1787
	.id_table = ch343_ids,
1788
#ifdef CONFIG_PM
1789
	.supports_autosuspend = 1,
1790
#endif
1791
	.disable_hub_initiated_lpm = 1,
1792
};
1793

    
1794
/*
1795
 * TTY driver structures.
1796
 */
1797
static const struct tty_operations ch343_ops = {
1798
	.install = ch343_tty_install,
1799
	.open = ch343_tty_open,
1800
	.close = ch343_tty_close,
1801
	.cleanup = ch343_tty_cleanup,
1802
	.hangup = ch343_tty_hangup,
1803
	.write = ch343_tty_write,
1804
	.write_room = ch343_tty_write_room,
1805
	.ioctl = ch343_tty_ioctl,
1806
	.chars_in_buffer = ch343_tty_chars_in_buffer,
1807
	.break_ctl = ch343_tty_break_ctl,
1808
	.set_termios = ch343_tty_set_termios,
1809
	.tiocmget = ch343_tty_tiocmget,
1810
	.tiocmset = ch343_tty_tiocmset,
1811
	.get_icount = ch343_get_icount,
1812
};
1813

    
1814
static int __init ch343_init(void)
1815
{
1816
	int retval;
1817
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
1818
	ch343_tty_driver = tty_alloc_driver(CH343_TTY_MINORS, TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
1819
	if (IS_ERR(ch343_tty_driver))
1820
		return PTR_ERR(ch343_tty_driver);
1821
#else
1822
	ch343_tty_driver = alloc_tty_driver(CH343_TTY_MINORS);
1823
	if (!ch343_tty_driver)
1824
		return -ENOMEM;
1825
#endif
1826
	ch343_tty_driver->driver_name = "usbch343", ch343_tty_driver->name = "ttyUSB",
1827
	ch343_tty_driver->major = CH343_TTY_MAJOR, ch343_tty_driver->minor_start = 0,
1828
	ch343_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, ch343_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1829
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
1830
	ch343_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1831
#endif
1832
	ch343_tty_driver->init_termios = tty_std_termios;
1833
	ch343_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1834
	tty_set_operations(ch343_tty_driver, &ch343_ops);
1835

    
1836
	retval = tty_register_driver(ch343_tty_driver);
1837
	if (retval) {
1838
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
1839
		tty_driver_kref_put(ch343_tty_driver);
1840
#else
1841
		put_tty_driver(ch343_tty_driver);
1842
#endif
1843
		return retval;
1844
	}
1845

    
1846
	retval = usb_register(&ch343_driver);
1847
	if (retval) {
1848
		tty_unregister_driver(ch343_tty_driver);
1849
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
1850
		tty_driver_kref_put(ch343_tty_driver);
1851
#else
1852
		put_tty_driver(ch343_tty_driver);
1853
#endif
1854
		return retval;
1855
	}
1856

    
1857
	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1858
	printk(KERN_INFO KBUILD_MODNAME ": " VERSION_DESC "\n");
1859

    
1860
	return 0;
1861
}
1862

    
1863
static void __exit ch343_exit(void)
1864
{
1865
	usb_deregister(&ch343_driver);
1866
	tty_unregister_driver(ch343_tty_driver);
1867
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
1868
	tty_driver_kref_put(ch343_tty_driver);
1869
#else
1870
	put_tty_driver(ch343_tty_driver);
1871
#endif
1872
	idr_destroy(&ch343_minors);
1873
	printk(KERN_INFO KBUILD_MODNAME ": "
1874
					"ch343 driver exit.\n");
1875
}
1876

    
1877
module_init(ch343_init);
1878
module_exit(ch343_exit);
1879

    
1880
MODULE_AUTHOR(DRIVER_AUTHOR);
1881
MODULE_DESCRIPTION(DRIVER_DESC);
1882
MODULE_VERSION(VERSION_DESC);
1883
MODULE_LICENSE("GPL");
1884
MODULE_ALIAS_CHARDEV_MAJOR(CH343_TTY_MAJOR);
(24-24/25)