Skip to content

Commit cad348a

Browse files
funnydogGustavo Padovan
authored andcommitted
Bluetooth: Implement .activate, .shutdown and .carrier_raised methods
Implement .activate, .shutdown and .carrier_raised methods of tty_port to manage the dlc, moving the code from rfcomm_tty_install() and rfcomm_tty_cleanup() functions. At the same time the tty .open()/.close() and .hangup() methods are changed to use the tty_port helpers that properly call the aforementioned tty_port methods. Signed-off-by: Gianluca Anzolin <[email protected]> Reviewed-by: Peter Hurley <[email protected]> Signed-off-by: Gustavo Padovan <[email protected]>
1 parent 54b926a commit cad348a

File tree

1 file changed

+47
-70
lines changed
  • net/bluetooth/rfcomm

1 file changed

+47
-70
lines changed

net/bluetooth/rfcomm/tty.c

Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ struct rfcomm_dev {
5858
uint modem_status;
5959

6060
struct rfcomm_dlc *dlc;
61-
wait_queue_head_t wait;
6261

6362
struct device *tty_dev;
6463

@@ -104,8 +103,39 @@ static void rfcomm_dev_destruct(struct tty_port *port)
104103
module_put(THIS_MODULE);
105104
}
106105

106+
/* device-specific initialization: open the dlc */
107+
static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
108+
{
109+
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
110+
111+
return rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
112+
}
113+
114+
/* we block the open until the dlc->state becomes BT_CONNECTED */
115+
static int rfcomm_dev_carrier_raised(struct tty_port *port)
116+
{
117+
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
118+
119+
return (dev->dlc->state == BT_CONNECTED);
120+
}
121+
122+
/* device-specific cleanup: close the dlc */
123+
static void rfcomm_dev_shutdown(struct tty_port *port)
124+
{
125+
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
126+
127+
if (dev->tty_dev->parent)
128+
device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
129+
130+
/* close the dlc */
131+
rfcomm_dlc_close(dev->dlc, 0);
132+
}
133+
107134
static const struct tty_port_operations rfcomm_port_ops = {
108135
.destruct = rfcomm_dev_destruct,
136+
.activate = rfcomm_dev_activate,
137+
.shutdown = rfcomm_dev_shutdown,
138+
.carrier_raised = rfcomm_dev_carrier_raised,
109139
};
110140

111141
static struct rfcomm_dev *__rfcomm_dev_get(int id)
@@ -228,7 +258,6 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
228258

229259
tty_port_init(&dev->port);
230260
dev->port.ops = &rfcomm_port_ops;
231-
init_waitqueue_head(&dev->wait);
232261

233262
skb_queue_head_init(&dev->pending);
234263

@@ -563,9 +592,12 @@ static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
563592
BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
564593

565594
dev->err = err;
566-
wake_up_interruptible(&dev->wait);
595+
if (dlc->state == BT_CONNECTED) {
596+
device_move(dev->tty_dev, rfcomm_get_device(dev),
597+
DPM_ORDER_DEV_AFTER_PARENT);
567598

568-
if (dlc->state == BT_CLOSED) {
599+
wake_up_interruptible(&dev->port.open_wait);
600+
} else if (dlc->state == BT_CLOSED) {
569601
tty = tty_port_tty_get(&dev->port);
570602
if (!tty) {
571603
if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
@@ -640,17 +672,10 @@ static void rfcomm_tty_cleanup(struct tty_struct *tty)
640672
{
641673
struct rfcomm_dev *dev = tty->driver_data;
642674

643-
if (dev->tty_dev->parent)
644-
device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
645-
646-
/* Close DLC and dettach TTY */
647-
rfcomm_dlc_close(dev->dlc, 0);
648-
649675
clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
650676

651677
rfcomm_dlc_lock(dev->dlc);
652678
tty->driver_data = NULL;
653-
dev->port.tty = NULL;
654679
rfcomm_dlc_unlock(dev->dlc);
655680

656681
tty_port_put(&dev->port);
@@ -662,7 +687,6 @@ static void rfcomm_tty_cleanup(struct tty_struct *tty)
662687
*/
663688
static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
664689
{
665-
DECLARE_WAITQUEUE(wait, current);
666690
struct rfcomm_dev *dev;
667691
struct rfcomm_dlc *dlc;
668692
int err;
@@ -676,72 +700,30 @@ static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
676700
/* Attach TTY and open DLC */
677701
rfcomm_dlc_lock(dlc);
678702
tty->driver_data = dev;
679-
dev->port.tty = tty;
680703
rfcomm_dlc_unlock(dlc);
681704
set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
682705

683706
/* install the tty_port */
684707
err = tty_port_install(&dev->port, driver, tty);
685-
if (err < 0)
686-
goto error_no_dlc;
687-
688-
err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
689-
if (err < 0)
690-
goto error_no_dlc;
708+
if (err)
709+
rfcomm_tty_cleanup(tty);
691710

692-
/* Wait for DLC to connect */
693-
add_wait_queue(&dev->wait, &wait);
694-
while (1) {
695-
set_current_state(TASK_INTERRUPTIBLE);
696-
697-
if (dlc->state == BT_CLOSED) {
698-
err = -dev->err;
699-
break;
700-
}
701-
702-
if (dlc->state == BT_CONNECTED)
703-
break;
704-
705-
if (signal_pending(current)) {
706-
err = -EINTR;
707-
break;
708-
}
709-
710-
tty_unlock(tty);
711-
schedule();
712-
tty_lock(tty);
713-
}
714-
set_current_state(TASK_RUNNING);
715-
remove_wait_queue(&dev->wait, &wait);
716-
717-
if (err < 0)
718-
goto error_no_connection;
719-
720-
device_move(dev->tty_dev, rfcomm_get_device(dev),
721-
DPM_ORDER_DEV_AFTER_PARENT);
722-
return 0;
723-
724-
error_no_connection:
725-
rfcomm_dlc_close(dlc, err);
726-
error_no_dlc:
727-
clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
728-
tty_port_put(&dev->port);
729711
return err;
730712
}
731713

732714
static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
733715
{
734716
struct rfcomm_dev *dev = tty->driver_data;
735-
unsigned long flags;
717+
int err;
736718

737719
BT_DBG("tty %p id %d", tty, tty->index);
738720

739721
BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
740722
dev->channel, dev->port.count);
741723

742-
spin_lock_irqsave(&dev->port.lock, flags);
743-
dev->port.count++;
744-
spin_unlock_irqrestore(&dev->port.lock, flags);
724+
err = tty_port_open(&dev->port, tty, filp);
725+
if (err)
726+
return err;
745727

746728
/*
747729
* FIXME: rfcomm should use proper flow control for
@@ -758,22 +740,17 @@ static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
758740
static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
759741
{
760742
struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
761-
unsigned long flags;
762743

763744
if (!dev)
764745
return;
765746

766747
BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
767748
dev->port.count);
768749

769-
spin_lock_irqsave(&dev->port.lock, flags);
770-
if (!--dev->port.count) {
771-
spin_unlock_irqrestore(&dev->port.lock, flags);
750+
tty_port_close(&dev->port, tty, filp);
772751

773-
if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
774-
tty_port_put(&dev->port);
775-
} else
776-
spin_unlock_irqrestore(&dev->port.lock, flags);
752+
if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
753+
tty_port_put(&dev->port);
777754
}
778755

779756
static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
@@ -1076,7 +1053,7 @@ static void rfcomm_tty_hangup(struct tty_struct *tty)
10761053
if (!dev)
10771054
return;
10781055

1079-
rfcomm_tty_flush_buffer(tty);
1056+
tty_port_hangup(&dev->port);
10801057

10811058
if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
10821059
if (rfcomm_dev_get(dev->id) == NULL)
@@ -1166,7 +1143,7 @@ int __init rfcomm_init_ttys(void)
11661143
rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
11671144
rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
11681145
rfcomm_tty_driver->init_termios = tty_std_termios;
1169-
rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1146+
rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
11701147
rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
11711148
tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
11721149

0 commit comments

Comments
 (0)