Skip to content

Commit 335f851

Browse files
Alan-Coxtorvalds
authored andcommitted
tty: Bring the usb tty port structure into more use
This allows us to clean stuff up, but is probably also going to cause some app breakage with buggy apps as we now implement proper POSIX behaviour for USB ports matching all the other ports. This does also mean other apps that break on USB will now work properly. Signed-off-by: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1ec739b commit 335f851

39 files changed

Lines changed: 467 additions & 661 deletions

‎drivers/usb/serial/belkin_sa.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ static int belkin_sa_startup(struct usb_serial *serial);
9393
static void belkin_sa_shutdown(struct usb_serial *serial);
9494
static int belkin_sa_open(struct tty_struct *tty,
9595
struct usb_serial_port *port, struct file *filp);
96-
static void belkin_sa_close(struct tty_struct *tty,
97-
struct usb_serial_port *port, struct file *filp);
96+
static void belkin_sa_close(struct usb_serial_port *port);
9897
static void belkin_sa_read_int_callback(struct urb *urb);
9998
static void belkin_sa_set_termios(struct tty_struct *tty,
10099
struct usb_serial_port *port, struct ktermios * old);
@@ -244,8 +243,7 @@ static int belkin_sa_open(struct tty_struct *tty,
244243
} /* belkin_sa_open */
245244

246245

247-
static void belkin_sa_close(struct tty_struct *tty,
248-
struct usb_serial_port *port, struct file *filp)
246+
static void belkin_sa_close(struct usb_serial_port *port)
249247
{
250248
dbg("%s port %d", __func__, port->number);
251249

‎drivers/usb/serial/ch341.c‎

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,40 @@ error: kfree(priv);
262262
return r;
263263
}
264264

265-
static void ch341_close(struct tty_struct *tty, struct usb_serial_port *port,
266-
struct file *filp)
265+
static int ch341_carrier_raised(struct usb_serial_port *port)
266+
{
267+
struct ch341_private *priv = usb_get_serial_port_data(port);
268+
if (priv->line_status & CH341_BIT_DCD)
269+
return 1;
270+
return 0;
271+
}
272+
273+
static void ch341_dtr_rts(struct usb_serial_port *port, int on)
267274
{
268275
struct ch341_private *priv = usb_get_serial_port_data(port);
269276
unsigned long flags;
270-
unsigned int c_cflag;
271277

278+
dbg("%s - port %d", __func__, port->number);
279+
/* drop DTR and RTS */
280+
spin_lock_irqsave(&priv->lock, flags);
281+
if (on)
282+
priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
283+
else
284+
priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
285+
spin_unlock_irqrestore(&priv->lock, flags);
286+
ch341_set_handshake(port->serial->dev, priv->line_control);
287+
wake_up_interruptible(&priv->delta_msr_wait);
288+
}
289+
290+
static void ch341_close(struct usb_serial_port *port)
291+
{
272292
dbg("%s - port %d", __func__, port->number);
273293

274294
/* shutdown our urbs */
275295
dbg("%s - shutting down urbs", __func__);
276296
usb_kill_urb(port->write_urb);
277297
usb_kill_urb(port->read_urb);
278298
usb_kill_urb(port->interrupt_in_urb);
279-
280-
if (tty) {
281-
c_cflag = tty->termios->c_cflag;
282-
if (c_cflag & HUPCL) {
283-
/* drop DTR and RTS */
284-
spin_lock_irqsave(&priv->lock, flags);
285-
priv->line_control = 0;
286-
spin_unlock_irqrestore(&priv->lock, flags);
287-
ch341_set_handshake(port->serial->dev, 0);
288-
}
289-
}
290-
wake_up_interruptible(&priv->delta_msr_wait);
291299
}
292300

293301

@@ -302,7 +310,6 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
302310
dbg("ch341_open()");
303311

304312
priv->baud_rate = DEFAULT_BAUD_RATE;
305-
priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
306313

307314
r = ch341_configure(serial->dev, priv);
308315
if (r)
@@ -322,7 +329,7 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
322329
if (r) {
323330
dev_err(&port->dev, "%s - failed submitting interrupt urb,"
324331
" error %d\n", __func__, r);
325-
ch341_close(tty, port, NULL);
332+
ch341_close(port);
326333
return -EPROTO;
327334
}
328335

@@ -343,9 +350,6 @@ static void ch341_set_termios(struct tty_struct *tty,
343350

344351
dbg("ch341_set_termios()");
345352

346-
if (!tty || !tty->termios)
347-
return;
348-
349353
baud_rate = tty_get_baud_rate(tty);
350354

351355
priv->baud_rate = baud_rate;
@@ -568,6 +572,8 @@ static struct usb_serial_driver ch341_device = {
568572
.usb_driver = &ch341_driver,
569573
.num_ports = 1,
570574
.open = ch341_open,
575+
.dtr_rts = ch341_dtr_rts,
576+
.carrier_raised = ch341_carrier_raised,
571577
.close = ch341_close,
572578
.ioctl = ch341_ioctl,
573579
.set_termios = ch341_set_termios,

‎drivers/usb/serial/console.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ static int usb_console_setup(struct console *co, char *options)
169169
kfree(tty);
170170
}
171171
}
172-
172+
/* So we know not to kill the hardware on a hangup on this
173+
port. We have also bumped the use count by one so it won't go
174+
idle */
173175
port->console = 1;
174176
retval = 0;
175177

@@ -182,7 +184,7 @@ static int usb_console_setup(struct console *co, char *options)
182184
kfree(tty);
183185
reset_open_count:
184186
port->port.count = 0;
185-
goto out;
187+
goto out;
186188
}
187189

188190
static void usb_console_write(struct console *co,

‎drivers/usb/serial/cp210x.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
static int cp2101_open(struct tty_struct *, struct usb_serial_port *,
3737
struct file *);
3838
static void cp2101_cleanup(struct usb_serial_port *);
39-
static void cp2101_close(struct tty_struct *, struct usb_serial_port *,
40-
struct file*);
39+
static void cp2101_close(struct usb_serial_port *);
4140
static void cp2101_get_termios(struct tty_struct *,
4241
struct usb_serial_port *port);
4342
static void cp2101_get_termios_port(struct usb_serial_port *port,
@@ -398,8 +397,7 @@ static void cp2101_cleanup(struct usb_serial_port *port)
398397
}
399398
}
400399

401-
static void cp2101_close(struct tty_struct *tty, struct usb_serial_port *port,
402-
struct file *filp)
400+
static void cp2101_close(struct usb_serial_port *port)
403401
{
404402
dbg("%s - port %d", __func__, port->number);
405403

‎drivers/usb/serial/cyberjack.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ static int cyberjack_startup(struct usb_serial *serial);
6161
static void cyberjack_shutdown(struct usb_serial *serial);
6262
static int cyberjack_open(struct tty_struct *tty,
6363
struct usb_serial_port *port, struct file *filp);
64-
static void cyberjack_close(struct tty_struct *tty,
65-
struct usb_serial_port *port, struct file *filp);
64+
static void cyberjack_close(struct usb_serial_port *port);
6665
static int cyberjack_write(struct tty_struct *tty,
6766
struct usb_serial_port *port, const unsigned char *buf, int count);
6867
static int cyberjack_write_room(struct tty_struct *tty);
@@ -185,8 +184,7 @@ static int cyberjack_open(struct tty_struct *tty,
185184
return result;
186185
}
187186

188-
static void cyberjack_close(struct tty_struct *tty,
189-
struct usb_serial_port *port, struct file *filp)
187+
static void cyberjack_close(struct usb_serial_port *port)
190188
{
191189
dbg("%s - port %d", __func__, port->number);
192190

‎drivers/usb/serial/cypress_m8.c‎

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ static int cypress_ca42v2_startup(struct usb_serial *serial);
174174
static void cypress_shutdown(struct usb_serial *serial);
175175
static int cypress_open(struct tty_struct *tty,
176176
struct usb_serial_port *port, struct file *filp);
177-
static void cypress_close(struct tty_struct *tty,
178-
struct usb_serial_port *port, struct file *filp);
177+
static void cypress_close(struct usb_serial_port *port);
178+
static void cypress_dtr_rts(struct usb_serial_port *port, int on);
179179
static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
180180
const unsigned char *buf, int count);
181181
static void cypress_send(struct usb_serial_port *port);
@@ -218,6 +218,7 @@ static struct usb_serial_driver cypress_earthmate_device = {
218218
.shutdown = cypress_shutdown,
219219
.open = cypress_open,
220220
.close = cypress_close,
221+
.dtr_rts = cypress_dtr_rts,
221222
.write = cypress_write,
222223
.write_room = cypress_write_room,
223224
.ioctl = cypress_ioctl,
@@ -244,6 +245,7 @@ static struct usb_serial_driver cypress_hidcom_device = {
244245
.shutdown = cypress_shutdown,
245246
.open = cypress_open,
246247
.close = cypress_close,
248+
.dtr_rts = cypress_dtr_rts,
247249
.write = cypress_write,
248250
.write_room = cypress_write_room,
249251
.ioctl = cypress_ioctl,
@@ -270,6 +272,7 @@ static struct usb_serial_driver cypress_ca42v2_device = {
270272
.shutdown = cypress_shutdown,
271273
.open = cypress_open,
272274
.close = cypress_close,
275+
.dtr_rts = cypress_dtr_rts,
273276
.write = cypress_write,
274277
.write_room = cypress_write_room,
275278
.ioctl = cypress_ioctl,
@@ -656,11 +659,7 @@ static int cypress_open(struct tty_struct *tty,
656659
priv->rx_flags = 0;
657660
spin_unlock_irqrestore(&priv->lock, flags);
658661

659-
/* raise both lines and set termios */
660-
spin_lock_irqsave(&priv->lock, flags);
661-
priv->line_control = CONTROL_DTR | CONTROL_RTS;
662-
priv->cmd_ctrl = 1;
663-
spin_unlock_irqrestore(&priv->lock, flags);
662+
/* Set termios */
664663
result = cypress_write(tty, port, NULL, 0);
665664

666665
if (result) {
@@ -694,76 +693,42 @@ static int cypress_open(struct tty_struct *tty,
694693
__func__, result);
695694
cypress_set_dead(port);
696695
}
697-
696+
port->port.drain_delay = 256;
698697
return result;
699698
} /* cypress_open */
700699

700+
static void cypress_dtr_rts(struct usb_serial_port *port, int on)
701+
{
702+
struct cypress_private *priv = usb_get_serial_port_data(port);
703+
/* drop dtr and rts */
704+
priv = usb_get_serial_port_data(port);
705+
spin_lock_irq(&priv->lock);
706+
if (on == 0)
707+
priv->line_control = 0;
708+
else
709+
priv->line_control = CONTROL_DTR | CONTROL_RTS;
710+
priv->cmd_ctrl = 1;
711+
spin_unlock_irq(&priv->lock);
712+
cypress_write(NULL, port, NULL, 0);
713+
}
701714

702-
static void cypress_close(struct tty_struct *tty,
703-
struct usb_serial_port *port, struct file *filp)
715+
static void cypress_close(struct usb_serial_port *port)
704716
{
705717
struct cypress_private *priv = usb_get_serial_port_data(port);
706-
unsigned int c_cflag;
707-
int bps;
708-
long timeout;
709-
wait_queue_t wait;
710718

711719
dbg("%s - port %d", __func__, port->number);
712720

713-
/* wait for data to drain from buffer */
714-
spin_lock_irq(&priv->lock);
715-
timeout = CYPRESS_CLOSING_WAIT;
716-
init_waitqueue_entry(&wait, current);
717-
add_wait_queue(&tty->write_wait, &wait);
718-
for (;;) {
719-
set_current_state(TASK_INTERRUPTIBLE);
720-
if (cypress_buf_data_avail(priv->buf) == 0
721-
|| timeout == 0 || signal_pending(current)
722-
/* without mutex, allowed due to harmless failure mode */
723-
|| port->serial->disconnected)
724-
break;
725-
spin_unlock_irq(&priv->lock);
726-
timeout = schedule_timeout(timeout);
727-
spin_lock_irq(&priv->lock);
728-
}
729-
set_current_state(TASK_RUNNING);
730-
remove_wait_queue(&tty->write_wait, &wait);
731-
/* clear out any remaining data in the buffer */
732-
cypress_buf_clear(priv->buf);
733-
spin_unlock_irq(&priv->lock);
734-
735721
/* writing is potentially harmful, lock must be taken */
736722
mutex_lock(&port->serial->disc_mutex);
737723
if (port->serial->disconnected) {
738724
mutex_unlock(&port->serial->disc_mutex);
739725
return;
740726
}
741-
/* wait for characters to drain from device */
742-
if (tty) {
743-
bps = tty_get_baud_rate(tty);
744-
if (bps > 1200)
745-
timeout = max((HZ * 2560) / bps, HZ / 10);
746-
else
747-
timeout = 2 * HZ;
748-
schedule_timeout_interruptible(timeout);
749-
}
750-
727+
cypress_buf_clear(priv->buf);
751728
dbg("%s - stopping urbs", __func__);
752729
usb_kill_urb(port->interrupt_in_urb);
753730
usb_kill_urb(port->interrupt_out_urb);
754731

755-
if (tty) {
756-
c_cflag = tty->termios->c_cflag;
757-
if (c_cflag & HUPCL) {
758-
/* drop dtr and rts */
759-
priv = usb_get_serial_port_data(port);
760-
spin_lock_irq(&priv->lock);
761-
priv->line_control = 0;
762-
priv->cmd_ctrl = 1;
763-
spin_unlock_irq(&priv->lock);
764-
cypress_write(tty, port, NULL, 0);
765-
}
766-
}
767732

768733
if (stats)
769734
dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",

0 commit comments

Comments
 (0)