1// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB Debug cable driver
4 *
5 * Copyright (C) 2006 Greg Kroah-Hartman <greg@kroah.com>
6 */
7
8#include <linux/gfp.h>
9#include <linux/kernel.h>
10#include <linux/tty.h>
11#include <linux/module.h>
12#include <linux/usb.h>
13#include <linux/usb/serial.h>
14
15#define USB_DEBUG_MAX_PACKET_SIZE	8
16#define USB_DEBUG_BRK_SIZE		8
17static const char USB_DEBUG_BRK[USB_DEBUG_BRK_SIZE] = {
18	0x00,
19	0xff,
20	0x01,
21	0xfe,
22	0x00,
23	0xfe,
24	0x01,
25	0xff,
26};
27
28static const struct usb_device_id id_table[] = {
29	{ USB_DEVICE(0x0525, 0x127a) },
30	{ },
31};
32
33static const struct usb_device_id dbc_id_table[] = {
34	{ USB_DEVICE(0x1d6b, 0x0010) },
35	{ USB_DEVICE(0x1d6b, 0x0011) },
36	{ },
37};
38
39static const struct usb_device_id id_table_combined[] = {
40	{ USB_DEVICE(0x0525, 0x127a) },
41	{ USB_DEVICE(0x1d6b, 0x0010) },
42	{ USB_DEVICE(0x1d6b, 0x0011) },
43	{ },
44};
45MODULE_DEVICE_TABLE(usb, id_table_combined);
46
47/* This HW really does not support a serial break, so one will be
48 * emulated when ever the break state is set to true.
49 */
50static int usb_debug_break_ctl(struct tty_struct *tty, int break_state)
51{
52	struct usb_serial_port *port = tty->driver_data;
53	int ret;
54
55	if (!break_state)
56		return 0;
57
58	ret = usb_serial_generic_write(tty, port, USB_DEBUG_BRK, USB_DEBUG_BRK_SIZE);
59	if (ret < 0)
60		return ret;
61
62	return 0;
63}
64
65static void usb_debug_process_read_urb(struct urb *urb)
66{
67	struct usb_serial_port *port = urb->context;
68
69	if (urb->actual_length == USB_DEBUG_BRK_SIZE &&
70		memcmp(urb->transfer_buffer, USB_DEBUG_BRK,
71						USB_DEBUG_BRK_SIZE) == 0) {
72		usb_serial_handle_break(port);
73		return;
74	}
75
76	usb_serial_generic_process_read_urb(urb);
77}
78
79static struct usb_serial_driver debug_device = {
80	.driver = {
81		.owner =	THIS_MODULE,
82		.name =		"debug",
83	},
84	.id_table =		id_table,
85	.num_ports =		1,
86	.bulk_out_size =	USB_DEBUG_MAX_PACKET_SIZE,
87	.break_ctl =		usb_debug_break_ctl,
88	.process_read_urb =	usb_debug_process_read_urb,
89};
90
91static struct usb_serial_driver dbc_device = {
92	.driver = {
93		.owner =	THIS_MODULE,
94		.name =		"xhci_dbc",
95	},
96	.id_table =		dbc_id_table,
97	.num_ports =		1,
98	.break_ctl =		usb_debug_break_ctl,
99	.process_read_urb =	usb_debug_process_read_urb,
100};
101
102static struct usb_serial_driver * const serial_drivers[] = {
103	&debug_device, &dbc_device, NULL
104};
105
106module_usb_serial_driver(serial_drivers, id_table_combined);
107MODULE_LICENSE("GPL v2");
108