xref: /linux-6.15/drivers/usb/serial/usb_debug.c (revision 98fcb5f7)
1958e8741SGreg Kroah-Hartman /*
2958e8741SGreg Kroah-Hartman  * USB Debug cable driver
3958e8741SGreg Kroah-Hartman  *
4958e8741SGreg Kroah-Hartman  * Copyright (C) 2006 Greg Kroah-Hartman <[email protected]>
5958e8741SGreg Kroah-Hartman  *
6958e8741SGreg Kroah-Hartman  *	This program is free software; you can redistribute it and/or
7958e8741SGreg Kroah-Hartman  *	modify it under the terms of the GNU General Public License version
8958e8741SGreg Kroah-Hartman  *	2 as published by the Free Software Foundation.
9958e8741SGreg Kroah-Hartman  */
10958e8741SGreg Kroah-Hartman 
11958e8741SGreg Kroah-Hartman #include <linux/kernel.h>
12958e8741SGreg Kroah-Hartman #include <linux/init.h>
13958e8741SGreg Kroah-Hartman #include <linux/tty.h>
14958e8741SGreg Kroah-Hartman #include <linux/module.h>
15958e8741SGreg Kroah-Hartman #include <linux/usb.h>
16958e8741SGreg Kroah-Hartman #include <linux/usb/serial.h>
17958e8741SGreg Kroah-Hartman 
18715b1dc0SJason Wessel #define URB_DEBUG_MAX_IN_FLIGHT_URBS	4000
1971be4f81SAleksey Gorelov #define USB_DEBUG_MAX_PACKET_SIZE	8
20*98fcb5f7SJason Wessel #define USB_DEBUG_BRK_SIZE		8
21*98fcb5f7SJason Wessel static char USB_DEBUG_BRK[USB_DEBUG_BRK_SIZE] = {
22*98fcb5f7SJason Wessel 	0x00,
23*98fcb5f7SJason Wessel 	0xff,
24*98fcb5f7SJason Wessel 	0x01,
25*98fcb5f7SJason Wessel 	0xfe,
26*98fcb5f7SJason Wessel 	0x00,
27*98fcb5f7SJason Wessel 	0xfe,
28*98fcb5f7SJason Wessel 	0x01,
29*98fcb5f7SJason Wessel 	0xff,
30*98fcb5f7SJason Wessel };
3171be4f81SAleksey Gorelov 
32958e8741SGreg Kroah-Hartman static struct usb_device_id id_table [] = {
33958e8741SGreg Kroah-Hartman 	{ USB_DEVICE(0x0525, 0x127a) },
34958e8741SGreg Kroah-Hartman 	{ },
35958e8741SGreg Kroah-Hartman };
36958e8741SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
37958e8741SGreg Kroah-Hartman 
38958e8741SGreg Kroah-Hartman static struct usb_driver debug_driver = {
39958e8741SGreg Kroah-Hartman 	.name =		"debug",
40958e8741SGreg Kroah-Hartman 	.probe =	usb_serial_probe,
41958e8741SGreg Kroah-Hartman 	.disconnect =	usb_serial_disconnect,
42958e8741SGreg Kroah-Hartman 	.id_table =	id_table,
43958e8741SGreg Kroah-Hartman 	.no_dynamic_id = 	1,
44958e8741SGreg Kroah-Hartman };
45958e8741SGreg Kroah-Hartman 
46619b572aSRoel Kluin static int usb_debug_open(struct tty_struct *tty, struct usb_serial_port *port,
4795da310eSAlan Cox 							struct file *filp)
4871be4f81SAleksey Gorelov {
4971be4f81SAleksey Gorelov 	port->bulk_out_size = USB_DEBUG_MAX_PACKET_SIZE;
5095da310eSAlan Cox 	return usb_serial_generic_open(tty, port, filp);
5171be4f81SAleksey Gorelov }
5271be4f81SAleksey Gorelov 
53*98fcb5f7SJason Wessel /* This HW really does not support a serial break, so one will be
54*98fcb5f7SJason Wessel  * emulated when ever the break state is set to true.
55*98fcb5f7SJason Wessel  */
56*98fcb5f7SJason Wessel static void usb_debug_break_ctl(struct tty_struct *tty, int break_state)
57*98fcb5f7SJason Wessel {
58*98fcb5f7SJason Wessel 	struct usb_serial_port *port = tty->driver_data;
59*98fcb5f7SJason Wessel 	if (!break_state)
60*98fcb5f7SJason Wessel 		return;
61*98fcb5f7SJason Wessel 	usb_serial_generic_write(tty, port, USB_DEBUG_BRK, USB_DEBUG_BRK_SIZE);
62*98fcb5f7SJason Wessel }
63*98fcb5f7SJason Wessel 
64*98fcb5f7SJason Wessel static void usb_debug_read_bulk_callback(struct urb *urb)
65*98fcb5f7SJason Wessel {
66*98fcb5f7SJason Wessel 	struct usb_serial_port *port = urb->context;
67*98fcb5f7SJason Wessel 
68*98fcb5f7SJason Wessel 	if (urb->actual_length == USB_DEBUG_BRK_SIZE &&
69*98fcb5f7SJason Wessel 	    memcmp(urb->transfer_buffer, USB_DEBUG_BRK,
70*98fcb5f7SJason Wessel 		   USB_DEBUG_BRK_SIZE) == 0) {
71*98fcb5f7SJason Wessel 		usb_serial_handle_break(port);
72*98fcb5f7SJason Wessel 		usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
73*98fcb5f7SJason Wessel 		return;
74*98fcb5f7SJason Wessel 	}
75*98fcb5f7SJason Wessel 
76*98fcb5f7SJason Wessel 	usb_serial_generic_read_bulk_callback(urb);
77*98fcb5f7SJason Wessel }
78*98fcb5f7SJason Wessel 
79958e8741SGreg Kroah-Hartman static struct usb_serial_driver debug_device = {
80958e8741SGreg Kroah-Hartman 	.driver = {
81958e8741SGreg Kroah-Hartman 		.owner =	THIS_MODULE,
82958e8741SGreg Kroah-Hartman 		.name =		"debug",
83958e8741SGreg Kroah-Hartman 	},
84958e8741SGreg Kroah-Hartman 	.id_table =		id_table,
85958e8741SGreg Kroah-Hartman 	.num_ports =		1,
8671be4f81SAleksey Gorelov 	.open =			usb_debug_open,
87715b1dc0SJason Wessel 	.max_in_flight_urbs =	URB_DEBUG_MAX_IN_FLIGHT_URBS,
88*98fcb5f7SJason Wessel 	.break_ctl =		usb_debug_break_ctl,
89*98fcb5f7SJason Wessel 	.read_bulk_callback =	usb_debug_read_bulk_callback,
90958e8741SGreg Kroah-Hartman };
91958e8741SGreg Kroah-Hartman 
92958e8741SGreg Kroah-Hartman static int __init debug_init(void)
93958e8741SGreg Kroah-Hartman {
94958e8741SGreg Kroah-Hartman 	int retval;
95958e8741SGreg Kroah-Hartman 
96958e8741SGreg Kroah-Hartman 	retval = usb_serial_register(&debug_device);
97958e8741SGreg Kroah-Hartman 	if (retval)
98958e8741SGreg Kroah-Hartman 		return retval;
99958e8741SGreg Kroah-Hartman 	retval = usb_register(&debug_driver);
100958e8741SGreg Kroah-Hartman 	if (retval)
101958e8741SGreg Kroah-Hartman 		usb_serial_deregister(&debug_device);
102958e8741SGreg Kroah-Hartman 	return retval;
103958e8741SGreg Kroah-Hartman }
104958e8741SGreg Kroah-Hartman 
105958e8741SGreg Kroah-Hartman static void __exit debug_exit(void)
106958e8741SGreg Kroah-Hartman {
107958e8741SGreg Kroah-Hartman 	usb_deregister(&debug_driver);
108958e8741SGreg Kroah-Hartman 	usb_serial_deregister(&debug_device);
109958e8741SGreg Kroah-Hartman }
110958e8741SGreg Kroah-Hartman 
111958e8741SGreg Kroah-Hartman module_init(debug_init);
112958e8741SGreg Kroah-Hartman module_exit(debug_exit);
113958e8741SGreg Kroah-Hartman MODULE_LICENSE("GPL");
114