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