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