Merge branch 'for-arm-soc-next' of git://git.linaro.org/people/ljones/linux-3.0-ux500...
[linux-3.10.git] / drivers / usb / gadget / u_serial.c
index 3a4a664..f173952 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/tty.h>
 #include <linux/tty_flip.h>
 #include <linux/slab.h>
+#include <linux/export.h>
 
 #include "u_serial.h"
 
@@ -93,17 +94,14 @@ struct gs_buf {
  * (and thus for each /dev/ node).
  */
 struct gs_port {
+       struct tty_port         port;
        spinlock_t              port_lock;      /* guard port_* access */
 
        struct gserial          *port_usb;
-       struct tty_struct       *port_tty;
 
-       unsigned                open_count;
        bool                    openclose;      /* open/close in progress */
        u8                      port_num;
 
-       wait_queue_head_t       close_wait;     /* wait for last close */
-
        struct list_head        read_pool;
        int read_started;
        int read_allocated;
@@ -134,11 +132,15 @@ static unsigned   n_ports;
 
 
 #ifdef VERBOSE_DEBUG
+#ifndef pr_vdebug
 #define pr_vdebug(fmt, arg...) \
        pr_debug(fmt, ##arg)
+#endif /* pr_vdebug */
 #else
+#ifndef pr_vdebig
 #define pr_vdebug(fmt, arg...) \
        ({ if (0) pr_debug(fmt, ##arg); })
+#endif /* pr_vdebug */
 #endif
 
 /*-------------------------------------------------------------------------*/
@@ -411,8 +413,8 @@ __acquires(&port->port_lock)
                        break;
        }
 
-       if (do_tty_wake && port->port_tty)
-               tty_wakeup(port->port_tty);
+       if (do_tty_wake && port->port.tty)
+               tty_wakeup(port->port.tty);
        return status;
 }
 
@@ -434,7 +436,7 @@ __acquires(&port->port_lock)
                struct tty_struct       *tty;
 
                /* no more rx if closed */
-               tty = port->port_tty;
+               tty = port->port.tty;
                if (!tty)
                        break;
 
@@ -487,7 +489,7 @@ static void gs_rx_push(unsigned long _port)
 
        /* hand any queued data to the tty */
        spin_lock_irq(&port->port_lock);
-       tty = port->port_tty;
+       tty = port->port.tty;
        while (!list_empty(queue)) {
                struct usb_request      *req;
 
@@ -698,7 +700,7 @@ static int gs_start_io(struct gs_port *port)
 
        /* unblock any pending writes into our circular buffer */
        if (started) {
-               tty_wakeup(port->port_tty);
+               tty_wakeup(port->port.tty);
        } else {
                gs_free_requests(ep, head, &port->read_allocated);
                gs_free_requests(port->port_usb->in, &port->write_pool,
@@ -724,9 +726,6 @@ static int gs_open(struct tty_struct *tty, struct file *file)
        struct gs_port  *port;
        int             status;
 
-       if (port_num < 0 || port_num >= n_ports)
-               return -ENXIO;
-
        do {
                mutex_lock(&ports[port_num].lock);
                port = ports[port_num].port;
@@ -736,9 +735,9 @@ static int gs_open(struct tty_struct *tty, struct file *file)
                        spin_lock_irq(&port->port_lock);
 
                        /* already open?  Great. */
-                       if (port->open_count) {
+                       if (port->port.count) {
                                status = 0;
-                               port->open_count++;
+                               port->port.count++;
 
                        /* currently opening/closing? wait ... */
                        } else if (port->openclose) {
@@ -795,9 +794,9 @@ static int gs_open(struct tty_struct *tty, struct file *file)
        /* REVISIT maybe wait for "carrier detect" */
 
        tty->driver_data = port;
-       port->port_tty = tty;
+       port->port.tty = tty;
 
-       port->open_count = 1;
+       port->port.count = 1;
        port->openclose = false;
 
        /* if connected, start the I/O stream */
@@ -839,11 +838,11 @@ static void gs_close(struct tty_struct *tty, struct file *file)
 
        spin_lock_irq(&port->port_lock);
 
-       if (port->open_count != 1) {
-               if (port->open_count == 0)
+       if (port->port.count != 1) {
+               if (port->port.count == 0)
                        WARN_ON(1);
                else
-                       --port->open_count;
+                       --port->port.count;
                goto exit;
        }
 
@@ -853,7 +852,7 @@ static void gs_close(struct tty_struct *tty, struct file *file)
         * and sleep if necessary
         */
        port->openclose = true;
-       port->open_count = 0;
+       port->port.count = 0;
 
        gser = port->port_usb;
        if (gser && gser->disconnect)
@@ -881,14 +880,14 @@ static void gs_close(struct tty_struct *tty, struct file *file)
                gs_buf_clear(&port->port_write_buf);
 
        tty->driver_data = NULL;
-       port->port_tty = NULL;
+       port->port.tty = NULL;
 
        port->openclose = false;
 
        pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
                        port->port_num, tty, file);
 
-       wake_up_interruptible(&port->close_wait);
+       wake_up_interruptible(&port->port.close_wait);
 exit:
        spin_unlock_irq(&port->port_lock);
 }
@@ -919,7 +918,7 @@ static int gs_put_char(struct tty_struct *tty, unsigned char ch)
        unsigned long   flags;
        int             status;
 
-       pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
+       pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %pf\n",
                port->port_num, tty, ch, __builtin_return_address(0));
 
        spin_lock_irqsave(&port->port_lock, flags);
@@ -1027,7 +1026,7 @@ static const struct tty_operations gs_tty_ops = {
 
 static struct tty_driver *gs_tty_driver;
 
-static int __init
+static int
 gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
 {
        struct gs_port  *port;
@@ -1036,8 +1035,8 @@ gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
        if (port == NULL)
                return -ENOMEM;
 
+       tty_port_init(&port->port);
        spin_lock_init(&port->port_lock);
-       init_waitqueue_head(&port->close_wait);
        init_waitqueue_head(&port->drain_wait);
 
        tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
@@ -1073,7 +1072,7 @@ gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
  *
  * Returns negative errno or zero.
  */
-int __init gserial_setup(struct usb_gadget *g, unsigned count)
+int gserial_setup(struct usb_gadget *g, unsigned count)
 {
        unsigned                        i;
        struct usb_cdc_line_coding      coding;
@@ -1086,7 +1085,6 @@ int __init gserial_setup(struct usb_gadget *g, unsigned count)
        if (!gs_tty_driver)
                return -ENOMEM;
 
-       gs_tty_driver->owner = THIS_MODULE;
        gs_tty_driver->driver_name = "g_serial";
        gs_tty_driver->name = PREFIX;
        /* uses dynamically assigned dev_t values */
@@ -1135,7 +1133,8 @@ int __init gserial_setup(struct usb_gadget *g, unsigned count)
        for (i = 0; i < count; i++) {
                struct device   *tty_dev;
 
-               tty_dev = tty_register_device(gs_tty_driver, i, &g->dev);
+               tty_dev = tty_port_register_device(&ports[i].port->port,
+                               gs_tty_driver, i, &g->dev);
                if (IS_ERR(tty_dev))
                        pr_warning("%s: no classdev for port %d, err %ld\n",
                                __func__, i, PTR_ERR(tty_dev));
@@ -1158,7 +1157,7 @@ static int gs_closed(struct gs_port *port)
        int cond;
 
        spin_lock_irq(&port->port_lock);
-       cond = (port->open_count == 0) && !port->openclose;
+       cond = (port->port.count == 0) && !port->openclose;
        spin_unlock_irq(&port->port_lock);
        return cond;
 }
@@ -1197,7 +1196,7 @@ void gserial_cleanup(void)
                tasklet_kill(&port->push);
 
                /* wait for old opens to finish */
-               wait_event(port->close_wait, gs_closed(port));
+               wait_event(port->port.close_wait, gs_closed(port));
 
                WARN_ON(port->port_usb != NULL);
 
@@ -1271,7 +1270,7 @@ int gserial_connect(struct gserial *gser, u8 port_num)
        /* if it's already open, start I/O ... and notify the serial
         * protocol about open/close status (connect/disconnect).
         */
-       if (port->open_count) {
+       if (port->port.count) {
                pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
                gs_start_io(port);
                if (gser->connect)
@@ -1318,10 +1317,10 @@ void gserial_disconnect(struct gserial *gser)
 
        port->port_usb = NULL;
        gser->ioport = NULL;
-       if (port->open_count > 0 || port->openclose) {
+       if (port->port.count > 0 || port->openclose) {
                wake_up_interruptible(&port->drain_wait);
-               if (port->port_tty)
-                       tty_hangup(port->port_tty);
+               if (port->port.tty)
+                       tty_hangup(port->port.tty);
        }
        spin_unlock_irqrestore(&port->port_lock, flags);
 
@@ -1334,7 +1333,7 @@ void gserial_disconnect(struct gserial *gser)
 
        /* finally, free any unused/unusable I/O buffers */
        spin_lock_irqsave(&port->port_lock, flags);
-       if (port->open_count == 0 && !port->openclose)
+       if (port->port.count == 0 && !port->openclose)
                gs_buf_free(&port->port_write_buf);
        gs_free_requests(gser->out, &port->read_pool, NULL);
        gs_free_requests(gser->out, &port->read_queue, NULL);