18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * USB Wishbone-Serial adapter driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Wesley W. Terpstra <w.terpstra@gsi.de>
68c2ecf20Sopenharmony_ci * Copyright (C) 2013 GSI Helmholtz Centre for Heavy Ion Research GmbH
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/tty.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/usb.h>
138c2ecf20Sopenharmony_ci#include <linux/usb/serial.h>
148c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define GSI_VENDOR_OPENCLOSE 0xB0
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table[] = {
198c2ecf20Sopenharmony_ci	{ USB_DEVICE_AND_INTERFACE_INFO(0x1D50, 0x6062, 0xFF, 0xFF, 0xFF) },
208c2ecf20Sopenharmony_ci	{ },
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * Etherbone must be told that a new stream has begun before data arrives.
268c2ecf20Sopenharmony_ci * This is necessary to restart the negotiation of Wishbone bus parameters.
278c2ecf20Sopenharmony_ci * Similarly, when the stream ends, Etherbone must be told so that the cycle
288c2ecf20Sopenharmony_ci * line can be driven low in the case that userspace failed to do so.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_cistatic int usb_gsi_openclose(struct usb_serial_port *port, int value)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct usb_device *dev = port->serial->dev;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	return usb_control_msg(
358c2ecf20Sopenharmony_ci		dev,
368c2ecf20Sopenharmony_ci		usb_sndctrlpipe(dev, 0), /* Send to EP0OUT */
378c2ecf20Sopenharmony_ci		GSI_VENDOR_OPENCLOSE,
388c2ecf20Sopenharmony_ci		USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
398c2ecf20Sopenharmony_ci		value, /* wValue = device is open(1) or closed(0) */
408c2ecf20Sopenharmony_ci		port->serial->interface->cur_altsetting->desc.bInterfaceNumber,
418c2ecf20Sopenharmony_ci		NULL, 0,  /* There is no data stage */
428c2ecf20Sopenharmony_ci		5000); /* Timeout till operation fails */
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int wishbone_serial_open(struct tty_struct *tty,
468c2ecf20Sopenharmony_ci				struct usb_serial_port *port)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	int retval;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	retval = usb_gsi_openclose(port, 1);
518c2ecf20Sopenharmony_ci	if (retval) {
528c2ecf20Sopenharmony_ci		dev_err(&port->serial->dev->dev,
538c2ecf20Sopenharmony_ci		       "Could not mark device as open (%d)\n",
548c2ecf20Sopenharmony_ci		       retval);
558c2ecf20Sopenharmony_ci		return retval;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	retval = usb_serial_generic_open(tty, port);
598c2ecf20Sopenharmony_ci	if (retval)
608c2ecf20Sopenharmony_ci		usb_gsi_openclose(port, 0);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return retval;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void wishbone_serial_close(struct usb_serial_port *port)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	usb_serial_generic_close(port);
688c2ecf20Sopenharmony_ci	usb_gsi_openclose(port, 0);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic struct usb_serial_driver wishbone_serial_device = {
728c2ecf20Sopenharmony_ci	.driver = {
738c2ecf20Sopenharmony_ci		.owner =	THIS_MODULE,
748c2ecf20Sopenharmony_ci		.name =		"wishbone_serial",
758c2ecf20Sopenharmony_ci	},
768c2ecf20Sopenharmony_ci	.id_table =		id_table,
778c2ecf20Sopenharmony_ci	.num_ports =		1,
788c2ecf20Sopenharmony_ci	.open =			&wishbone_serial_open,
798c2ecf20Sopenharmony_ci	.close =		&wishbone_serial_close,
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
838c2ecf20Sopenharmony_ci	&wishbone_serial_device, NULL
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Wesley W. Terpstra <w.terpstra@gsi.de>");
898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("USB Wishbone-Serial adapter");
908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
91