^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * USB Wishbone-Serial adapter driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2013 Wesley W. Terpstra <w.terpstra@gsi.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2013 GSI Helmholtz Centre for Heavy Ion Research GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define GSI_VENDOR_OPENCLOSE 0xB0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) { USB_DEVICE_AND_INTERFACE_INFO(0x1D50, 0x6062, 0xFF, 0xFF, 0xFF) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Etherbone must be told that a new stream has begun before data arrives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * This is necessary to restart the negotiation of Wishbone bus parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Similarly, when the stream ends, Etherbone must be told so that the cycle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * line can be driven low in the case that userspace failed to do so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int usb_gsi_openclose(struct usb_serial_port *port, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct usb_device *dev = port->serial->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return usb_control_msg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) usb_sndctrlpipe(dev, 0), /* Send to EP0OUT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) GSI_VENDOR_OPENCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) value, /* wValue = device is open(1) or closed(0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) port->serial->interface->cur_altsetting->desc.bInterfaceNumber,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) NULL, 0, /* There is no data stage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 5000); /* Timeout till operation fails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int wishbone_serial_open(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) retval = usb_gsi_openclose(port, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dev_err(&port->serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "Could not mark device as open (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) retval = usb_serial_generic_open(tty, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) usb_gsi_openclose(port, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void wishbone_serial_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) usb_serial_generic_close(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) usb_gsi_openclose(port, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static struct usb_serial_driver wishbone_serial_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .name = "wishbone_serial",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .id_table = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .num_ports = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .open = &wishbone_serial_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .close = &wishbone_serial_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) &wishbone_serial_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) MODULE_AUTHOR("Wesley W. Terpstra <w.terpstra@gsi.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) MODULE_DESCRIPTION("USB Wishbone-Serial adapter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) MODULE_LICENSE("GPL");