20 #include <linux/kernel.h>
21 #include <linux/sched.h>
23 #include <linux/device.h>
25 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/export.h>
58 #define PREFIX "ttyGS"
82 #define WRITE_BUF_SIZE 8192
124 static struct portmaster {
128 static unsigned n_ports;
130 #define GS_CLOSE_TIMEOUT 15
136 #define pr_vdebug(fmt, arg...) \
141 #define pr_vdebug(fmt, arg...) \
142 ({ if (0) pr_debug(fmt, ##arg); })
155 static int gs_buf_alloc(
struct gs_buf *gb,
unsigned size)
173 static void gs_buf_free(
struct gs_buf *gb)
184 static void gs_buf_clear(
struct gs_buf *gb)
196 static unsigned gs_buf_data_avail(
struct gs_buf *gb)
207 static unsigned gs_buf_space_avail(
struct gs_buf *gb)
225 len = gs_buf_space_avail(gb);
257 gs_buf_get(
struct gs_buf *gb,
char *buf,
unsigned count)
261 len = gs_buf_data_avail(gb);
299 req = usb_ep_alloc_request(ep, kmalloc_flags);
305 usb_ep_free_request(ep, req);
321 usb_ep_free_request(ep, req);
357 static int gs_start_tx(
struct gs_port *port)
366 bool do_tty_wake =
false;
368 while (!list_empty(pool)) {
404 __func__,
"queue", in->
name, status);
405 list_add(&req->
list, pool);
416 if (do_tty_wake && port->
port.tty)
424 static unsigned gs_start_rx(
struct gs_port *port)
433 while (!list_empty(pool)) {
439 tty = port->
port.tty;
459 __func__,
"queue", out->
name, status);
460 list_add(&req->
list, pool);
482 static void gs_rx_push(
unsigned long _port)
484 struct gs_port *port = (
void *)_port;
488 bool do_push =
false;
492 tty = port->
port.tty;
493 while (!list_empty(queue)) {
524 char *packet = req->
buf;
525 unsigned size = req->
actual;
536 count = tty_insert_flip_string(tty, packet, size);
569 if (!list_empty(queue) && tty) {
572 tasklet_schedule(&port->
push);
593 tasklet_schedule(&port->
push);
630 while (!list_empty(head)) {
639 static int gs_alloc_requests(
struct usb_ep *ep,
struct list_head *head,
651 for (i = 0; i <
n; i++) {
654 return list_empty(head) ? -
ENOMEM : 0;
672 static int gs_start_io(
struct gs_port *port)
685 status = gs_alloc_requests(ep, head, gs_read_complete,
699 started = gs_start_rx(port);
738 if (port->
port.count) {
770 }
while (status != -
EAGAIN);
783 pr_debug(
"gs_open: ttyGS%d (%p,%p) no buffer\n",
786 goto exit_unlock_port;
797 port->
port.tty = tty;
799 port->
port.count = 1;
822 static int gs_writes_finished(
struct gs_port *
p)
834 static void gs_close(
struct tty_struct *tty,
struct file *file)
841 if (port->
port.count != 1) {
842 if (port->
port.count == 0)
855 port->
port.count = 0;
867 gs_writes_finished(port),
887 pr_debug(
"gs_close: ttyGS%d (%p,%p) done!\n",
895 static int gs_write(
struct tty_struct *tty,
const unsigned char *buf,
int count)
901 pr_vdebug(
"gs_write: ttyGS%d (%p) writing %d bytes\n",
909 status = gs_start_tx(port);
910 spin_unlock_irqrestore(&port->
port_lock, flags);
915 static int gs_put_char(
struct tty_struct *tty,
unsigned char ch)
921 pr_vdebug(
"gs_put_char: (%d,%p) char=0x%x, called from %pf\n",
922 port->
port_num, tty, ch, __builtin_return_address(0));
926 spin_unlock_irqrestore(&port->
port_lock, flags);
931 static void gs_flush_chars(
struct tty_struct *tty)
941 spin_unlock_irqrestore(&port->
port_lock, flags);
944 static int gs_write_room(
struct tty_struct *tty)
953 spin_unlock_irqrestore(&port->
port_lock, flags);
955 pr_vdebug(
"gs_write_room: (%d,%p) room=%d\n",
961 static int gs_chars_in_buffer(
struct tty_struct *tty)
969 spin_unlock_irqrestore(&port->
port_lock, flags);
971 pr_vdebug(
"gs_chars_in_buffer: (%d,%p) chars=%d\n",
978 static void gs_unthrottle(
struct tty_struct *tty)
989 tasklet_schedule(&port->
push);
992 spin_unlock_irqrestore(&port->
port_lock, flags);
1001 pr_vdebug(
"gs_break_ctl: ttyGS%d, send break (%d) \n",
1017 .put_char = gs_put_char,
1018 .flush_chars = gs_flush_chars,
1019 .write_room = gs_write_room,
1020 .chars_in_buffer = gs_chars_in_buffer,
1021 .unthrottle = gs_unthrottle,
1022 .break_ctl = gs_break_ctl,
1081 if (count == 0 || count >
N_PORTS)
1084 gs_tty_driver = alloc_tty_driver(count);
1114 for (i = 0; i <
count; i++) {
1116 status = gs_port_alloc(i, &coding);
1127 pr_err(
"%s: cannot register, err %d\n",
1133 for (i = 0; i <
count; i++) {
1137 gs_tty_driver, i, &g->
dev);
1138 if (IS_ERR(tty_dev))
1139 pr_warning(
"%s: no classdev for port %d, err %ld\n",
1140 __func__, i, PTR_ERR(tty_dev));
1143 pr_debug(
"%s: registered %d ttyGS* device%s\n", __func__,
1144 count, (count == 1) ?
"" :
"s");
1151 gs_tty_driver =
NULL;
1155 static int gs_closed(
struct gs_port *port)
1186 for (i = 0; i < n_ports; i++)
1189 for (i = 0; i < n_ports; i++) {
1209 gs_tty_driver =
NULL;
1211 pr_debug(
"%s: cleaned up ttyGS* support\n", __func__);
1238 unsigned long flags;
1241 if (!gs_tty_driver || port_num >= n_ports)
1248 status = usb_ep_enable(gser->
in);
1251 gser->
in->driver_data =
port;
1253 status = usb_ep_enable(gser->
out);
1256 gser->
out->driver_data =
port;
1273 if (port->
port.count) {
1283 spin_unlock_irqrestore(&port->
port_lock, flags);
1288 usb_ep_disable(gser->
in);
1289 gser->
in->driver_data =
NULL;
1307 unsigned long flags;
1325 spin_unlock_irqrestore(&port->
port_lock, flags);
1328 usb_ep_disable(gser->
out);
1329 gser->
out->driver_data =
NULL;
1331 usb_ep_disable(gser->
in);
1332 gser->
in->driver_data =
NULL;
1345 spin_unlock_irqrestore(&port->
port_lock, flags);