+/*
+ * Get a reference to our struct, taking into account locks/refcounts.
+ * This is to ensure ordering in case we are shutting down, and to ensure
+ * there is a refcount at all (because tty->disc_data may be NULL).
+ */
+static struct elmcan* get_elm(struct tty_struct *tty)
+{
+ struct elmcan *elm;
+ bool got_ref;
+
+ /* Lock all elmcan TTYs, so tty->disc_data can't become NULL
+ * the moment before we increase the reference counter.
+ */
+ spin_lock_bh(&elmcan_discdata_lock);
+ elm = (struct elmcan *) tty->disc_data;
+
+ if (!elm) {
+ spin_unlock_bh(&elmcan_discdata_lock);
+ return NULL;
+ }
+
+ got_ref = atomic_inc_not_zero(&elm->refcount);
+ spin_unlock_bh(&elmcan_discdata_lock);
+
+ if (!got_ref) {
+ return NULL;
+ }
+
+ return elm;
+}
+
+static void put_elm(struct elmcan *elm)
+{
+ atomic_dec(&elm->refcount);
+}
+
+
+