linux/drivers/mmc/card/sdio_uart.c
<<
>>
Prefs
   1/*
   2 * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
   3 *
   4 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
   5 * by Russell King.
   6 *
   7 * Author:      Nicolas Pitre
   8 * Created:     June 15, 2007
   9 * Copyright:   MontaVista Software, Inc.
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or (at
  14 * your option) any later version.
  15 */
  16
  17/*
  18 * Note: Although this driver assumes a 16550A-like UART implementation,
  19 * it is not possible to leverage the common 8250/16550 driver, nor the
  20 * core UART infrastructure, as they assumes direct access to the hardware
  21 * registers, often under a spinlock.  This is not possible in the SDIO
  22 * context as SDIO access functions must be able to sleep.
  23 *
  24 * Because we need to lock the SDIO host to ensure an exclusive access to
  25 * the card, we simply rely on that lock to also prevent and serialize
  26 * concurrent access to the same port.
  27 */
  28
  29#include <linux/module.h>
  30#include <linux/init.h>
  31#include <linux/kernel.h>
  32#include <linux/mutex.h>
  33#include <linux/seq_file.h>
  34#include <linux/serial_reg.h>
  35#include <linux/circ_buf.h>
  36#include <linux/gfp.h>
  37#include <linux/tty.h>
  38#include <linux/tty_flip.h>
  39
  40#include <linux/mmc/core.h>
  41#include <linux/mmc/card.h>
  42#include <linux/mmc/sdio_func.h>
  43#include <linux/mmc/sdio_ids.h>
  44
  45
  46#define UART_NR         8       /* Number of UARTs this driver can handle */
  47
  48
  49#define UART_XMIT_SIZE  PAGE_SIZE
  50#define WAKEUP_CHARS    256
  51
  52#define circ_empty(circ)        ((circ)->head == (circ)->tail)
  53#define circ_clear(circ)        ((circ)->head = (circ)->tail = 0)
  54
  55#define circ_chars_pending(circ) \
  56                (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
  57
  58#define circ_chars_free(circ) \
  59                (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
  60
  61
  62struct uart_icount {
  63        __u32   cts;
  64        __u32   dsr;
  65        __u32   rng;
  66        __u32   dcd;
  67        __u32   rx;
  68        __u32   tx;
  69        __u32   frame;
  70        __u32   overrun;
  71        __u32   parity;
  72        __u32   brk;
  73};
  74
  75struct sdio_uart_port {
  76        struct kref             kref;
  77        struct tty_struct       *tty;
  78        unsigned int            index;
  79        unsigned int            opened;
  80        struct mutex            open_lock;
  81        struct sdio_func        *func;
  82        struct mutex            func_lock;
  83        struct task_struct      *in_sdio_uart_irq;
  84        unsigned int            regs_offset;
  85        struct circ_buf         xmit;
  86        spinlock_t              write_lock;
  87        struct uart_icount      icount;
  88        unsigned int            uartclk;
  89        unsigned int            mctrl;
  90        unsigned int            read_status_mask;
  91        unsigned int            ignore_status_mask;
  92        unsigned char           x_char;
  93        unsigned char           ier;
  94        unsigned char           lcr;
  95};
  96
  97static struct sdio_uart_port *sdio_uart_table[UART_NR];
  98static DEFINE_SPINLOCK(sdio_uart_table_lock);
  99
 100static int sdio_uart_add_port(struct sdio_uart_port *port)
 101{
 102        int index, ret = -EBUSY;
 103
 104        kref_init(&port->kref);
 105        mutex_init(&port->open_lock);
 106        mutex_init(&port->func_lock);
 107        spin_lock_init(&port->write_lock);
 108
 109        spin_lock(&sdio_uart_table_lock);
 110        for (index = 0; index < UART_NR; index++) {
 111                if (!sdio_uart_table[index]) {
 112                        port->index = index;
 113                        sdio_uart_table[index] = port;
 114                        ret = 0;
 115                        break;
 116                }
 117        }
 118        spin_unlock(&sdio_uart_table_lock);
 119
 120        return ret;
 121}
 122
 123static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
 124{
 125        struct sdio_uart_port *port;
 126
 127        if (index >= UART_NR)
 128                return NULL;
 129
 130        spin_lock(&sdio_uart_table_lock);
 131        port = sdio_uart_table[index];
 132        if (port)
 133                kref_get(&port->kref);
 134        spin_unlock(&sdio_uart_table_lock);
 135
 136        return port;
 137}
 138
 139static void sdio_uart_port_destroy(struct kref *kref)
 140{
 141        struct sdio_uart_port *port =
 142                container_of(kref, struct sdio_uart_port, kref);
 143        kfree(port);
 144}
 145
 146static void sdio_uart_port_put(struct sdio_uart_port *port)
 147{
 148        kref_put(&port->kref, sdio_uart_port_destroy);
 149}
 150
 151static void sdio_uart_port_remove(struct sdio_uart_port *port)
 152{
 153        struct sdio_func *func;
 154
 155        BUG_ON(sdio_uart_table[port->index] != port);
 156
 157        spin_lock(&sdio_uart_table_lock);
 158        sdio_uart_table[port->index] = NULL;
 159        spin_unlock(&sdio_uart_table_lock);
 160
 161        /*
 162         * We're killing a port that potentially still is in use by
 163         * the tty layer. Be careful to prevent any further access
 164         * to the SDIO function and arrange for the tty layer to
 165         * give up on that port ASAP.
 166         * Beware: the lock ordering is critical.
 167         */
 168        mutex_lock(&port->open_lock);
 169        mutex_lock(&port->func_lock);
 170        func = port->func;
 171        sdio_claim_host(func);
 172        port->func = NULL;
 173        mutex_unlock(&port->func_lock);
 174        if (port->opened)
 175                tty_hangup(port->tty);
 176        mutex_unlock(&port->open_lock);
 177        sdio_release_irq(func);
 178        sdio_disable_func(func);
 179        sdio_release_host(func);
 180
 181        sdio_uart_port_put(port);
 182}
 183
 184static int sdio_uart_claim_func(struct sdio_uart_port *port)
 185{
 186        mutex_lock(&port->func_lock);
 187        if (unlikely(!port->func)) {
 188                mutex_unlock(&port->func_lock);
 189                return -ENODEV;
 190        }
 191        if (likely(port->in_sdio_uart_irq != current))
 192                sdio_claim_host(port->func);
 193        mutex_unlock(&port->func_lock);
 194        return 0;
 195}
 196
 197static inline void sdio_uart_release_func(struct sdio_uart_port *port)
 198{
 199        if (likely(port->in_sdio_uart_irq != current))
 200                sdio_release_host(port->func);
 201}
 202
 203static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
 204{
 205        unsigned char c;
 206        c = sdio_readb(port->func, port->regs_offset + offset, NULL);
 207        return c;
 208}
 209
 210static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
 211{
 212        sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
 213}
 214
 215static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
 216{
 217        unsigned char status;
 218        unsigned int ret;
 219
 220        status = sdio_in(port, UART_MSR);
 221
 222        ret = 0;
 223        if (status & UART_MSR_DCD)
 224                ret |= TIOCM_CAR;
 225        if (status & UART_MSR_RI)
 226                ret |= TIOCM_RNG;
 227        if (status & UART_MSR_DSR)
 228                ret |= TIOCM_DSR;
 229        if (status & UART_MSR_CTS)
 230                ret |= TIOCM_CTS;
 231        return ret;
 232}
 233
 234static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
 235{
 236        unsigned char mcr = 0;
 237
 238        if (mctrl & TIOCM_RTS)
 239                mcr |= UART_MCR_RTS;
 240        if (mctrl & TIOCM_DTR)
 241                mcr |= UART_MCR_DTR;
 242        if (mctrl & TIOCM_OUT1)
 243                mcr |= UART_MCR_OUT1;
 244        if (mctrl & TIOCM_OUT2)
 245                mcr |= UART_MCR_OUT2;
 246        if (mctrl & TIOCM_LOOP)
 247                mcr |= UART_MCR_LOOP;
 248
 249        sdio_out(port, UART_MCR, mcr);
 250}
 251
 252static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
 253                                          unsigned int set, unsigned int clear)
 254{
 255        unsigned int old;
 256
 257        old = port->mctrl;
 258        port->mctrl = (old & ~clear) | set;
 259        if (old != port->mctrl)
 260                sdio_uart_write_mctrl(port, port->mctrl);
 261}
 262
 263#define sdio_uart_set_mctrl(port, x)    sdio_uart_update_mctrl(port, x, 0)
 264#define sdio_uart_clear_mctrl(port, x)  sdio_uart_update_mctrl(port, 0, x)
 265
 266static void sdio_uart_change_speed(struct sdio_uart_port *port,
 267                                   struct ktermios *termios,
 268                                   struct ktermios *old)
 269{
 270        unsigned char cval, fcr = 0;
 271        unsigned int baud, quot;
 272
 273        switch (termios->c_cflag & CSIZE) {
 274        case CS5:
 275                cval = UART_LCR_WLEN5;
 276                break;
 277        case CS6:
 278                cval = UART_LCR_WLEN6;
 279                break;
 280        case CS7:
 281                cval = UART_LCR_WLEN7;
 282                break;
 283        default:
 284        case CS8:
 285                cval = UART_LCR_WLEN8;
 286                break;
 287        }
 288
 289        if (termios->c_cflag & CSTOPB)
 290                cval |= UART_LCR_STOP;
 291        if (termios->c_cflag & PARENB)
 292                cval |= UART_LCR_PARITY;
 293        if (!(termios->c_cflag & PARODD))
 294                cval |= UART_LCR_EPAR;
 295
 296        for (;;) {
 297                baud = tty_termios_baud_rate(termios);
 298                if (baud == 0)
 299                        baud = 9600;  /* Special case: B0 rate. */
 300                if (baud <= port->uartclk)
 301                        break;
 302                /*
 303                 * Oops, the quotient was zero.  Try again with the old
 304                 * baud rate if possible, otherwise default to 9600.
 305                 */
 306                termios->c_cflag &= ~CBAUD;
 307                if (old) {
 308                        termios->c_cflag |= old->c_cflag & CBAUD;
 309                        old = NULL;
 310                } else
 311                        termios->c_cflag |= B9600;
 312        }
 313        quot = (2 * port->uartclk + baud) / (2 * baud);
 314
 315        if (baud < 2400)
 316                fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
 317        else
 318                fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
 319
 320        port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
 321        if (termios->c_iflag & INPCK)
 322                port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
 323        if (termios->c_iflag & (BRKINT | PARMRK))
 324                port->read_status_mask |= UART_LSR_BI;
 325
 326        /*
 327         * Characters to ignore
 328         */
 329        port->ignore_status_mask = 0;
 330        if (termios->c_iflag & IGNPAR)
 331                port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
 332        if (termios->c_iflag & IGNBRK) {
 333                port->ignore_status_mask |= UART_LSR_BI;
 334                /*
 335                 * If we're ignoring parity and break indicators,
 336                 * ignore overruns too (for real raw support).
 337                 */
 338                if (termios->c_iflag & IGNPAR)
 339                        port->ignore_status_mask |= UART_LSR_OE;
 340        }
 341
 342        /*
 343         * ignore all characters if CREAD is not set
 344         */
 345        if ((termios->c_cflag & CREAD) == 0)
 346                port->ignore_status_mask |= UART_LSR_DR;
 347
 348        /*
 349         * CTS flow control flag and modem status interrupts
 350         */
 351        port->ier &= ~UART_IER_MSI;
 352        if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
 353                port->ier |= UART_IER_MSI;
 354
 355        port->lcr = cval;
 356
 357        sdio_out(port, UART_IER, port->ier);
 358        sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
 359        sdio_out(port, UART_DLL, quot & 0xff);
 360        sdio_out(port, UART_DLM, quot >> 8);
 361        sdio_out(port, UART_LCR, cval);
 362        sdio_out(port, UART_FCR, fcr);
 363
 364        sdio_uart_write_mctrl(port, port->mctrl);
 365}
 366
 367static void sdio_uart_start_tx(struct sdio_uart_port *port)
 368{
 369        if (!(port->ier & UART_IER_THRI)) {
 370                port->ier |= UART_IER_THRI;
 371                sdio_out(port, UART_IER, port->ier);
 372        }
 373}
 374
 375static void sdio_uart_stop_tx(struct sdio_uart_port *port)
 376{
 377        if (port->ier & UART_IER_THRI) {
 378                port->ier &= ~UART_IER_THRI;
 379                sdio_out(port, UART_IER, port->ier);
 380        }
 381}
 382
 383static void sdio_uart_stop_rx(struct sdio_uart_port *port)
 384{
 385        port->ier &= ~UART_IER_RLSI;
 386        port->read_status_mask &= ~UART_LSR_DR;
 387        sdio_out(port, UART_IER, port->ier);
 388}
 389
 390static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
 391{
 392        struct tty_struct *tty = port->tty;
 393        unsigned int ch, flag;
 394        int max_count = 256;
 395
 396        do {
 397                ch = sdio_in(port, UART_RX);
 398                flag = TTY_NORMAL;
 399                port->icount.rx++;
 400
 401                if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
 402                                        UART_LSR_FE | UART_LSR_OE))) {
 403                        /*
 404                         * For statistics only
 405                         */
 406                        if (*status & UART_LSR_BI) {
 407                                *status &= ~(UART_LSR_FE | UART_LSR_PE);
 408                                port->icount.brk++;
 409                        } else if (*status & UART_LSR_PE)
 410                                port->icount.parity++;
 411                        else if (*status & UART_LSR_FE)
 412                                port->icount.frame++;
 413                        if (*status & UART_LSR_OE)
 414                                port->icount.overrun++;
 415
 416                        /*
 417                         * Mask off conditions which should be ignored.
 418                         */
 419                        *status &= port->read_status_mask;
 420                        if (*status & UART_LSR_BI) {
 421                                flag = TTY_BREAK;
 422                        } else if (*status & UART_LSR_PE)
 423                                flag = TTY_PARITY;
 424                        else if (*status & UART_LSR_FE)
 425                                flag = TTY_FRAME;
 426                }
 427
 428                if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
 429                        tty_insert_flip_char(tty, ch, flag);
 430
 431                /*
 432                 * Overrun is special.  Since it's reported immediately,
 433                 * it doesn't affect the current character.
 434                 */
 435                if (*status & ~port->ignore_status_mask & UART_LSR_OE)
 436                        tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 437
 438                *status = sdio_in(port, UART_LSR);
 439        } while ((*status & UART_LSR_DR) && (max_count-- > 0));
 440        tty_flip_buffer_push(tty);
 441}
 442
 443static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
 444{
 445        struct circ_buf *xmit = &port->xmit;
 446        int count;
 447
 448        if (port->x_char) {
 449                sdio_out(port, UART_TX, port->x_char);
 450                port->icount.tx++;
 451                port->x_char = 0;
 452                return;
 453        }
 454        if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
 455                sdio_uart_stop_tx(port);
 456                return;
 457        }
 458
 459        count = 16;
 460        do {
 461                sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
 462                xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
 463                port->icount.tx++;
 464                if (circ_empty(xmit))
 465                        break;
 466        } while (--count > 0);
 467
 468        if (circ_chars_pending(xmit) < WAKEUP_CHARS)
 469                tty_wakeup(port->tty);
 470
 471        if (circ_empty(xmit))
 472                sdio_uart_stop_tx(port);
 473}
 474
 475static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
 476{
 477        int status;
 478
 479        status = sdio_in(port, UART_MSR);
 480
 481        if ((status & UART_MSR_ANY_DELTA) == 0)
 482                return;
 483
 484        if (status & UART_MSR_TERI)
 485                port->icount.rng++;
 486        if (status & UART_MSR_DDSR)
 487                port->icount.dsr++;
 488        if (status & UART_MSR_DDCD)
 489                port->icount.dcd++;
 490        if (status & UART_MSR_DCTS) {
 491                port->icount.cts++;
 492                if (port->tty->termios->c_cflag & CRTSCTS) {
 493                        int cts = (status & UART_MSR_CTS);
 494                        if (port->tty->hw_stopped) {
 495                                if (cts) {
 496                                        port->tty->hw_stopped = 0;
 497                                        sdio_uart_start_tx(port);
 498                                        tty_wakeup(port->tty);
 499                                }
 500                        } else {
 501                                if (!cts) {
 502                                        port->tty->hw_stopped = 1;
 503                                        sdio_uart_stop_tx(port);
 504                                }
 505                        }
 506                }
 507        }
 508}
 509
 510/*
 511 * This handles the interrupt from one port.
 512 */
 513static void sdio_uart_irq(struct sdio_func *func)
 514{
 515        struct sdio_uart_port *port = sdio_get_drvdata(func);
 516        unsigned int iir, lsr;
 517
 518        /*
 519         * In a few places sdio_uart_irq() is called directly instead of
 520         * waiting for the actual interrupt to be raised and the SDIO IRQ
 521         * thread scheduled in order to reduce latency.  However, some
 522         * interaction with the tty core may end up calling us back
 523         * (serial echo, flow control, etc.) through those same places
 524         * causing undesirable effects.  Let's stop the recursion here.
 525         */
 526        if (unlikely(port->in_sdio_uart_irq == current))
 527                return;
 528
 529        iir = sdio_in(port, UART_IIR);
 530        if (iir & UART_IIR_NO_INT)
 531                return;
 532
 533        port->in_sdio_uart_irq = current;
 534        lsr = sdio_in(port, UART_LSR);
 535        if (lsr & UART_LSR_DR)
 536                sdio_uart_receive_chars(port, &lsr);
 537        sdio_uart_check_modem_status(port);
 538        if (lsr & UART_LSR_THRE)
 539                sdio_uart_transmit_chars(port);
 540        port->in_sdio_uart_irq = NULL;
 541}
 542
 543static int sdio_uart_startup(struct sdio_uart_port *port)
 544{
 545        unsigned long page;
 546        int ret;
 547
 548        /*
 549         * Set the TTY IO error marker - we will only clear this
 550         * once we have successfully opened the port.
 551         */
 552        set_bit(TTY_IO_ERROR, &port->tty->flags);
 553
 554        /* Initialise and allocate the transmit buffer. */
 555        page = __get_free_page(GFP_KERNEL);
 556        if (!page)
 557                return -ENOMEM;
 558        port->xmit.buf = (unsigned char *)page;
 559        circ_clear(&port->xmit);
 560
 561        ret = sdio_uart_claim_func(port);
 562        if (ret)
 563                goto err1;
 564        ret = sdio_enable_func(port->func);
 565        if (ret)
 566                goto err2;
 567        ret = sdio_claim_irq(port->func, sdio_uart_irq);
 568        if (ret)
 569                goto err3;
 570
 571        /*
 572         * Clear the FIFO buffers and disable them.
 573         * (they will be reenabled in sdio_change_speed())
 574         */
 575        sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
 576        sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
 577                        UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
 578        sdio_out(port, UART_FCR, 0);
 579
 580        /*
 581         * Clear the interrupt registers.
 582         */
 583        (void) sdio_in(port, UART_LSR);
 584        (void) sdio_in(port, UART_RX);
 585        (void) sdio_in(port, UART_IIR);
 586        (void) sdio_in(port, UART_MSR);
 587
 588        /*
 589         * Now, initialize the UART
 590         */
 591        sdio_out(port, UART_LCR, UART_LCR_WLEN8);
 592
 593        port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
 594        port->mctrl = TIOCM_OUT2;
 595
 596        sdio_uart_change_speed(port, port->tty->termios, NULL);
 597
 598        if (port->tty->termios->c_cflag & CBAUD)
 599                sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
 600
 601        if (port->tty->termios->c_cflag & CRTSCTS)
 602                if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
 603                        port->tty->hw_stopped = 1;
 604
 605        clear_bit(TTY_IO_ERROR, &port->tty->flags);
 606
 607        /* Kick the IRQ handler once while we're still holding the host lock */
 608        sdio_uart_irq(port->func);
 609
 610        sdio_uart_release_func(port);
 611        return 0;
 612
 613err3:
 614        sdio_disable_func(port->func);
 615err2:
 616        sdio_uart_release_func(port);
 617err1:
 618        free_page((unsigned long)port->xmit.buf);
 619        return ret;
 620}
 621
 622static void sdio_uart_shutdown(struct sdio_uart_port *port)
 623{
 624        int ret;
 625
 626        ret = sdio_uart_claim_func(port);
 627        if (ret)
 628                goto skip;
 629
 630        sdio_uart_stop_rx(port);
 631
 632        /* TODO: wait here for TX FIFO to drain */
 633
 634        /* Turn off DTR and RTS early. */
 635        if (port->tty->termios->c_cflag & HUPCL)
 636                sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
 637
 638         /* Disable interrupts from this port */
 639        sdio_release_irq(port->func);
 640        port->ier = 0;
 641        sdio_out(port, UART_IER, 0);
 642
 643        sdio_uart_clear_mctrl(port, TIOCM_OUT2);
 644
 645        /* Disable break condition and FIFOs. */
 646        port->lcr &= ~UART_LCR_SBC;
 647        sdio_out(port, UART_LCR, port->lcr);
 648        sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
 649                                 UART_FCR_CLEAR_RCVR |
 650                                 UART_FCR_CLEAR_XMIT);
 651        sdio_out(port, UART_FCR, 0);
 652
 653        sdio_disable_func(port->func);
 654
 655        sdio_uart_release_func(port);
 656
 657skip:
 658        /* Free the transmit buffer page. */
 659        free_page((unsigned long)port->xmit.buf);
 660}
 661
 662static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
 663{
 664        struct sdio_uart_port *port;
 665        int ret;
 666
 667        port = sdio_uart_port_get(tty->index);
 668        if (!port)
 669                return -ENODEV;
 670
 671        mutex_lock(&port->open_lock);
 672
 673        /*
 674         * Make sure not to mess up with a dead port
 675         * which has not been closed yet.
 676         */
 677        if (tty->driver_data && tty->driver_data != port) {
 678                mutex_unlock(&port->open_lock);
 679                sdio_uart_port_put(port);
 680                return -EBUSY;
 681        }
 682
 683        if (!port->opened) {
 684                tty->driver_data = port;
 685                port->tty = tty;
 686                ret = sdio_uart_startup(port);
 687                if (ret) {
 688                        tty->driver_data = NULL;
 689                        port->tty = NULL;
 690                        mutex_unlock(&port->open_lock);
 691                        sdio_uart_port_put(port);
 692                        return ret;
 693                }
 694        }
 695        port->opened++;
 696        mutex_unlock(&port->open_lock);
 697        return 0;
 698}
 699
 700static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
 701{
 702        struct sdio_uart_port *port = tty->driver_data;
 703
 704        if (!port)
 705                return;
 706
 707        mutex_lock(&port->open_lock);
 708        BUG_ON(!port->opened);
 709
 710        /*
 711         * This is messy.  The tty layer calls us even when open()
 712         * returned an error.  Ignore this close request if tty->count
 713         * is larger than port->count.
 714         */
 715        if (tty->count > port->opened) {
 716                mutex_unlock(&port->open_lock);
 717                return;
 718        }
 719
 720        if (--port->opened == 0) {
 721                tty->closing = 1;
 722                sdio_uart_shutdown(port);
 723                tty_ldisc_flush(tty);
 724                port->tty = NULL;
 725                tty->driver_data = NULL;
 726                tty->closing = 0;
 727        }
 728        mutex_unlock(&port->open_lock);
 729        sdio_uart_port_put(port);
 730}
 731
 732static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
 733                           int count)
 734{
 735        struct sdio_uart_port *port = tty->driver_data;
 736        struct circ_buf *circ = &port->xmit;
 737        int c, ret = 0;
 738
 739        if (!port->func)
 740                return -ENODEV;
 741
 742        spin_lock(&port->write_lock);
 743        while (1) {
 744                c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
 745                if (count < c)
 746                        c = count;
 747                if (c <= 0)
 748                        break;
 749                memcpy(circ->buf + circ->head, buf, c);
 750                circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
 751                buf += c;
 752                count -= c;
 753                ret += c;
 754        }
 755        spin_unlock(&port->write_lock);
 756
 757        if ( !(port->ier & UART_IER_THRI)) {
 758                int err = sdio_uart_claim_func(port);
 759                if (!err) {
 760                        sdio_uart_start_tx(port);
 761                        sdio_uart_irq(port->func);
 762                        sdio_uart_release_func(port);
 763                } else
 764                        ret = err;
 765        }
 766
 767        return ret;
 768}
 769
 770static int sdio_uart_write_room(struct tty_struct *tty)
 771{
 772        struct sdio_uart_port *port = tty->driver_data;
 773        return port ? circ_chars_free(&port->xmit) : 0;
 774}
 775
 776static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
 777{
 778        struct sdio_uart_port *port = tty->driver_data;
 779        return port ? circ_chars_pending(&port->xmit) : 0;
 780}
 781
 782static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
 783{
 784        struct sdio_uart_port *port = tty->driver_data;
 785
 786        port->x_char = ch;
 787        if (ch && !(port->ier & UART_IER_THRI)) {
 788                if (sdio_uart_claim_func(port) != 0)
 789                        return;
 790                sdio_uart_start_tx(port);
 791                sdio_uart_irq(port->func);
 792                sdio_uart_release_func(port);
 793        }
 794}
 795
 796static void sdio_uart_throttle(struct tty_struct *tty)
 797{
 798        struct sdio_uart_port *port = tty->driver_data;
 799
 800        if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
 801                return;
 802
 803        if (sdio_uart_claim_func(port) != 0)
 804                return;
 805
 806        if (I_IXOFF(tty)) {
 807                port->x_char = STOP_CHAR(tty);
 808                sdio_uart_start_tx(port);
 809        }
 810
 811        if (tty->termios->c_cflag & CRTSCTS)
 812                sdio_uart_clear_mctrl(port, TIOCM_RTS);
 813
 814        sdio_uart_irq(port->func);
 815        sdio_uart_release_func(port);
 816}
 817
 818static void sdio_uart_unthrottle(struct tty_struct *tty)
 819{
 820        struct sdio_uart_port *port = tty->driver_data;
 821
 822        if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
 823                return;
 824
 825        if (sdio_uart_claim_func(port) != 0)
 826                return;
 827
 828        if (I_IXOFF(tty)) {
 829                if (port->x_char) {
 830                        port->x_char = 0;
 831                } else {
 832                        port->x_char = START_CHAR(tty);
 833                        sdio_uart_start_tx(port);
 834                }
 835        }
 836
 837        if (tty->termios->c_cflag & CRTSCTS)
 838                sdio_uart_set_mctrl(port, TIOCM_RTS);
 839
 840        sdio_uart_irq(port->func);
 841        sdio_uart_release_func(port);
 842}
 843
 844static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
 845{
 846        struct sdio_uart_port *port = tty->driver_data;
 847        unsigned int cflag = tty->termios->c_cflag;
 848
 849#define RELEVANT_IFLAG(iflag)   ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
 850
 851        if ((cflag ^ old_termios->c_cflag) == 0 &&
 852            RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
 853                return;
 854
 855        if (sdio_uart_claim_func(port) != 0)
 856                return;
 857
 858        sdio_uart_change_speed(port, tty->termios, old_termios);
 859
 860        /* Handle transition to B0 status */
 861        if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
 862                sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
 863
 864        /* Handle transition away from B0 status */
 865        if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
 866                unsigned int mask = TIOCM_DTR;
 867                if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
 868                        mask |= TIOCM_RTS;
 869                sdio_uart_set_mctrl(port, mask);
 870        }
 871
 872        /* Handle turning off CRTSCTS */
 873        if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
 874                tty->hw_stopped = 0;
 875                sdio_uart_start_tx(port);
 876        }
 877
 878        /* Handle turning on CRTSCTS */
 879        if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
 880                if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
 881                        tty->hw_stopped = 1;
 882                        sdio_uart_stop_tx(port);
 883                }
 884        }
 885
 886        sdio_uart_release_func(port);
 887}
 888
 889static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
 890{
 891        struct sdio_uart_port *port = tty->driver_data;
 892        int result;
 893
 894        result = sdio_uart_claim_func(port);
 895        if (result != 0)
 896                return result;
 897
 898        if (break_state == -1)
 899                port->lcr |= UART_LCR_SBC;
 900        else
 901                port->lcr &= ~UART_LCR_SBC;
 902        sdio_out(port, UART_LCR, port->lcr);
 903
 904        sdio_uart_release_func(port);
 905        return 0;
 906}
 907
 908static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
 909{
 910        struct sdio_uart_port *port = tty->driver_data;
 911        int result;
 912
 913        result = sdio_uart_claim_func(port);
 914        if (!result) {
 915                result = port->mctrl | sdio_uart_get_mctrl(port);
 916                sdio_uart_release_func(port);
 917        }
 918
 919        return result;
 920}
 921
 922static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
 923                              unsigned int set, unsigned int clear)
 924{
 925        struct sdio_uart_port *port = tty->driver_data;
 926        int result;
 927
 928        result =sdio_uart_claim_func(port);
 929        if(!result) {
 930                sdio_uart_update_mctrl(port, set, clear);
 931                sdio_uart_release_func(port);
 932        }
 933
 934        return result;
 935}
 936
 937static int sdio_uart_proc_show(struct seq_file *m, void *v)
 938{
 939        int i;
 940
 941        seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
 942                       "", "", "");
 943        for (i = 0; i < UART_NR; i++) {
 944                struct sdio_uart_port *port = sdio_uart_port_get(i);
 945                if (port) {
 946                        seq_printf(m, "%d: uart:SDIO", i);
 947                        if(capable(CAP_SYS_ADMIN)) {
 948                                seq_printf(m, " tx:%d rx:%d",
 949                                               port->icount.tx, port->icount.rx);
 950                                if (port->icount.frame)
 951                                        seq_printf(m, " fe:%d",
 952                                                       port->icount.frame);
 953                                if (port->icount.parity)
 954                                        seq_printf(m, " pe:%d",
 955                                                       port->icount.parity);
 956                                if (port->icount.brk)
 957                                        seq_printf(m, " brk:%d",
 958                                                       port->icount.brk);
 959                                if (port->icount.overrun)
 960                                        seq_printf(m, " oe:%d",
 961                                                       port->icount.overrun);
 962                                if (port->icount.cts)
 963                                        seq_printf(m, " cts:%d",
 964                                                       port->icount.cts);
 965                                if (port->icount.dsr)
 966                                        seq_printf(m, " dsr:%d",
 967                                                       port->icount.dsr);
 968                                if (port->icount.rng)
 969                                        seq_printf(m, " rng:%d",
 970                                                       port->icount.rng);
 971                                if (port->icount.dcd)
 972                                        seq_printf(m, " dcd:%d",
 973                                                       port->icount.dcd);
 974                        }
 975                        sdio_uart_port_put(port);
 976                        seq_putc(m, '\n');
 977                }
 978        }
 979        return 0;
 980}
 981
 982static int sdio_uart_proc_open(struct inode *inode, struct file *file)
 983{
 984        return single_open(file, sdio_uart_proc_show, NULL);
 985}
 986
 987static const struct file_operations sdio_uart_proc_fops = {
 988        .owner          = THIS_MODULE,
 989        .open           = sdio_uart_proc_open,
 990        .read           = seq_read,
 991        .llseek         = seq_lseek,
 992        .release        = single_release,
 993};
 994
 995static const struct tty_operations sdio_uart_ops = {
 996        .open                   = sdio_uart_open,
 997        .close                  = sdio_uart_close,
 998        .write                  = sdio_uart_write,
 999        .write_room             = sdio_uart_write_room,
1000        .chars_in_buffer        = sdio_uart_chars_in_buffer,
1001        .send_xchar             = sdio_uart_send_xchar,
1002        .throttle               = sdio_uart_throttle,
1003        .unthrottle             = sdio_uart_unthrottle,
1004        .set_termios            = sdio_uart_set_termios,
1005        .break_ctl              = sdio_uart_break_ctl,
1006        .tiocmget               = sdio_uart_tiocmget,
1007        .tiocmset               = sdio_uart_tiocmset,
1008        .proc_fops              = &sdio_uart_proc_fops,
1009};
1010
1011static struct tty_driver *sdio_uart_tty_driver;
1012
1013static int sdio_uart_probe(struct sdio_func *func,
1014                           const struct sdio_device_id *id)
1015{
1016        struct sdio_uart_port *port;
1017        int ret;
1018
1019        port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1020        if (!port)
1021                return -ENOMEM;
1022
1023        if (func->class == SDIO_CLASS_UART) {
1024                printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1025                       sdio_func_id(func));
1026                kfree(port);
1027                return -ENOSYS;
1028        } else if (func->class == SDIO_CLASS_GPS) {
1029                /*
1030                 * We need tuple 0x91.  It contains SUBTPL_SIOREG
1031                 * and SUBTPL_RCVCAPS.
1032                 */
1033                struct sdio_func_tuple *tpl;
1034                for (tpl = func->tuples; tpl; tpl = tpl->next) {
1035                        if (tpl->code != 0x91)
1036                                continue;
1037                        if (tpl->size < 10)
1038                                continue;
1039                        if (tpl->data[1] == 0)  /* SUBTPL_SIOREG */
1040                                break;
1041                }
1042                if (!tpl) {
1043                        printk(KERN_WARNING
1044                               "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1045                               sdio_func_id(func));
1046                        kfree(port);
1047                        return -EINVAL;
1048                }
1049                printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1050                       sdio_func_id(func), tpl->data[2], tpl->data[3]);
1051                port->regs_offset = (tpl->data[4] << 0) |
1052                                    (tpl->data[5] << 8) |
1053                                    (tpl->data[6] << 16);
1054                printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1055                       sdio_func_id(func), port->regs_offset);
1056                port->uartclk = tpl->data[7] * 115200;
1057                if (port->uartclk == 0)
1058                        port->uartclk = 115200;
1059                printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1060                       sdio_func_id(func), port->uartclk,
1061                       tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1062        } else {
1063                kfree(port);
1064                return -EINVAL;
1065        }
1066
1067        port->func = func;
1068        sdio_set_drvdata(func, port);
1069
1070        ret = sdio_uart_add_port(port);
1071        if (ret) {
1072                kfree(port);
1073        } else {
1074                struct device *dev;
1075                dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1076                if (IS_ERR(dev)) {
1077                        sdio_uart_port_remove(port);
1078                        ret = PTR_ERR(dev);
1079                }
1080        }
1081
1082        return ret;
1083}
1084
1085static void sdio_uart_remove(struct sdio_func *func)
1086{
1087        struct sdio_uart_port *port = sdio_get_drvdata(func);
1088
1089        tty_unregister_device(sdio_uart_tty_driver, port->index);
1090        sdio_uart_port_remove(port);
1091}
1092
1093static const struct sdio_device_id sdio_uart_ids[] = {
1094        { SDIO_DEVICE_CLASS(SDIO_CLASS_UART)            },
1095        { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS)             },
1096        { /* end: all zeroes */                         },
1097};
1098
1099MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1100
1101static struct sdio_driver sdio_uart_driver = {
1102        .probe          = sdio_uart_probe,
1103        .remove         = sdio_uart_remove,
1104        .name           = "sdio_uart",
1105        .id_table       = sdio_uart_ids,
1106};
1107
1108static int __init sdio_uart_init(void)
1109{
1110        int ret;
1111        struct tty_driver *tty_drv;
1112
1113        sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1114        if (!tty_drv)
1115                return -ENOMEM;
1116
1117        tty_drv->owner = THIS_MODULE;
1118        tty_drv->driver_name = "sdio_uart";
1119        tty_drv->name =   "ttySDIO";
1120        tty_drv->major = 0;  /* dynamically allocated */
1121        tty_drv->minor_start = 0;
1122        tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1123        tty_drv->subtype = SERIAL_TYPE_NORMAL;
1124        tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1125        tty_drv->init_termios = tty_std_termios;
1126        tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1127        tty_drv->init_termios.c_ispeed = 4800;
1128        tty_drv->init_termios.c_ospeed = 4800;
1129        tty_set_operations(tty_drv, &sdio_uart_ops);
1130
1131        ret = tty_register_driver(tty_drv);
1132        if (ret)
1133                goto err1;
1134
1135        ret = sdio_register_driver(&sdio_uart_driver);
1136        if (ret)
1137                goto err2;
1138
1139        return 0;
1140
1141err2:
1142        tty_unregister_driver(tty_drv);
1143err1:
1144        put_tty_driver(tty_drv);
1145        return ret;
1146}
1147
1148static void __exit sdio_uart_exit(void)
1149{
1150        sdio_unregister_driver(&sdio_uart_driver);
1151        tty_unregister_driver(sdio_uart_tty_driver);
1152        put_tty_driver(sdio_uart_tty_driver);
1153}
1154
1155module_init(sdio_uart_init);
1156module_exit(sdio_uart_exit);
1157
1158MODULE_AUTHOR("Nicolas Pitre");
1159MODULE_LICENSE("GPL");
1160