aboutsummaryrefslogblamecommitdiffstats
path: root/Documentation/DocBook/procfs-guide.tmpl
blob: 9eba4b7af73de9dd5d6d83086c374c07efb5a1bc (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                    
                   
                                            


                      








                                                                                 



                  
                                  



                                                                      
                                  

















































                                                                       
                        






















                                                                                     












































































































                                                                                          
                                   






















                                                                     
                                     


















                                                                                   
                                  
































































                                                                                   
                             










                                                                      
                                                                  


                                                                  
                                                               





                                                                

                                                            


             

                                                                     

                                                   
                                              


             
                                       




                                                                       

                                                                          

             




















































                                                                                 








                                                                   
                             

































































































































                                                                                               
                        


















                                                                    
                                   
































                                                                     
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
	"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
<!ENTITY procfsexample SYSTEM "procfs_example.xml">
]>

<book id="LKProcfsGuide">
  <bookinfo>
    <title>Linux Kernel Procfs Guide</title>

    <authorgroup>
      <author>
	<firstname>Erik</firstname>
	<othername>(J.A.K.)</othername>
	<surname>Mouw</surname>
	<affiliation>
	  <address>
            <email>mouw@nl.linux.org</email>
          </address>
	</affiliation>
      </author>
      <othercredit>
	<contrib>
	This software and documentation were written while working on the
	LART computing board
	(<ulink url="http://www.lartmaker.nl/">http://www.lartmaker.nl/</ulink>),
	which was sponsored by the Delt University of Technology projects
	Mobile Multi-media Communications and Ubiquitous Communications.
	</contrib>
      </othercredit>
    </authorgroup>

    <revhistory>
      <revision>
	<revnumber>1.0</revnumber>
	<date>May 30, 2001</date>
	<revremark>Initial revision posted to linux-kernel</revremark>
      </revision>
      <revision>
	<revnumber>1.1</revnumber>
	<date>June 3, 2001</date>
	<revremark>Revised after comments from linux-kernel</revremark>
      </revision>
    </revhistory>

    <copyright>
      <year>2001</year>
      <holder>Erik Mouw</holder>
    </copyright>


    <legalnotice>
      <para>
        This documentation is free software; you can redistribute it
        and/or modify it under the terms of the GNU General Public
        License as published by the Free Software Foundation; either
        version 2 of the License, or (at your option) any later
        version.
      </para>
      
      <para>
        This documentation is distributed in the hope that it will be
        useful, but WITHOUT ANY WARRANTY; without even the implied
        warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
        PURPOSE.  See the GNU General Public License for more details.
      </para>
      
      <para>
        You should have received a copy of the GNU General Public
        License along with this program; if not, write to the Free
        Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
        MA 02111-1307 USA
      </para>
      
      <para>
        For more details see the file COPYING in the source
        distribution of Linux.
      </para>
    </legalnotice>
  </bookinfo>




  <toc>
  </toc>




  <preface id="Preface">
    <title>Preface</title>

    <para>
      This guide describes the use of the procfs file system from
      within the Linux kernel. The idea to write this guide came up on
      the #kernelnewbies IRC channel (see <ulink
      url="http://www.kernelnewbies.org/">http://www.kernelnewbies.org/</ulink>),
      when Jeff Garzik explained the use of procfs and forwarded me a
      message Alexander Viro wrote to the linux-kernel mailing list. I
      agreed to write it up nicely, so here it is.
    </para>

    <para>
      I'd like to thank Jeff Garzik
      <email>jgarzik@pobox.com</email> and Alexander Viro
      <email>viro@parcelfarce.linux.theplanet.co.uk</email> for their input,
      Tim Waugh <email>twaugh@redhat.com</email> for his <ulink
      url="http://people.redhat.com/twaugh/docbook/selfdocbook/">Selfdocbook</ulink>,
      and Marc Joosen <email>marcj@historia.et.tudelft.nl</email> for
      proofreading.
    </para>

    <para>
      Erik
    </para>
  </preface>




  <chapter id="intro">
    <title>Introduction</title>

    <para>
      The <filename class="directory">/proc</filename> file system
      (procfs) is a special file system in the linux kernel. It's a
      virtual file system: it is not associated with a block device
      but exists only in memory. The files in the procfs are there to
      allow userland programs access to certain information from the
      kernel (like process information in <filename
      class="directory">/proc/[0-9]+/</filename>), but also for debug
      purposes (like <filename>/proc/ksyms</filename>).
    </para>

    <para>
      This guide describes the use of the procfs file system from
      within the Linux kernel. It starts by introducing all relevant
      functions to manage the files within the file system. After that
      it shows how to communicate with userland, and some tips and
      tricks will be pointed out. Finally a complete example will be
      shown.
    </para>

    <para>
      Note that the files in <filename
      class="directory">/proc/sys</filename> are sysctl files: they
      don't belong to procfs and are governed by a completely
      different API described in the Kernel API book.
    </para>
  </chapter>




  <chapter id="managing">
    <title>Managing procfs entries</title>
    
    <para>
      This chapter describes the functions that various kernel
      components use to populate the procfs with files, symlinks,
      device nodes, and directories.
    </para>

    <para>
      A minor note before we start: if you want to use any of the
      procfs functions, be sure to include the correct header file! 
      This should be one of the first lines in your code:
    </para>

    <programlisting>
#include &lt;linux/proc_fs.h&gt;
    </programlisting>




    <sect1 id="regularfile">
      <title>Creating a regular file</title>
      
      <funcsynopsis>
	<funcprototype>
	  <funcdef>struct proc_dir_entry* <function>create_proc_entry</function></funcdef>
	  <paramdef>const char* <parameter>name</parameter></paramdef>
	  <paramdef>mode_t <parameter>mode</parameter></paramdef>
	  <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>
        This function creates a regular file with the name
        <parameter>name</parameter>, file mode
        <parameter>mode</parameter> in the directory
        <parameter>parent</parameter>. To create a file in the root of
        the procfs, use <constant>NULL</constant> as
        <parameter>parent</parameter> parameter. When successful, the
        function will return a pointer to the freshly created
        <structname>struct proc_dir_entry</structname>; otherwise it
        will return <constant>NULL</constant>. <xref
        linkend="userland"/> describes how to do something useful with
        regular files.
      </para>

      <para>
        Note that it is specifically supported that you can pass a
        path that spans multiple directories. For example
        <function>create_proc_entry</function>(<parameter>"drivers/via0/info"</parameter>)
        will create the <filename class="directory">via0</filename>
        directory if necessary, with standard
        <constant>0755</constant> permissions.
      </para>

    <para>
      If you only want to be able to read the file, the function
      <function>create_proc_read_entry</function> described in <xref
      linkend="convenience"/> may be used to create and initialise
      the procfs entry in one single call.
    </para>
    </sect1>




    <sect1 id="Creating_a_symlink">
      <title>Creating a symlink</title>

      <funcsynopsis>
	<funcprototype>
	  <funcdef>struct proc_dir_entry*
	  <function>proc_symlink</function></funcdef> <paramdef>const
	  char* <parameter>name</parameter></paramdef>
	  <paramdef>struct proc_dir_entry*
	  <parameter>parent</parameter></paramdef> <paramdef>const
	  char* <parameter>dest</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>
      
      <para>
        This creates a symlink in the procfs directory
        <parameter>parent</parameter> that points from
        <parameter>name</parameter> to
        <parameter>dest</parameter>. This translates in userland to
        <literal>ln -s</literal> <parameter>dest</parameter>
        <parameter>name</parameter>.
      </para>
    </sect1>

    <sect1 id="Creating_a_directory">
      <title>Creating a directory</title>
      
      <funcsynopsis>
	<funcprototype>
	  <funcdef>struct proc_dir_entry* <function>proc_mkdir</function></funcdef>
	  <paramdef>const char* <parameter>name</parameter></paramdef>
	  <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>
        Create a directory <parameter>name</parameter> in the procfs
        directory <parameter>parent</parameter>.
      </para>
    </sect1>




    <sect1 id="Removing_an_entry">
      <title>Removing an entry</title>
      
      <funcsynopsis>
	<funcprototype>
	  <funcdef>void <function>remove_proc_entry</function></funcdef>
	  <paramdef>const char* <parameter>name</parameter></paramdef>
	  <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>
        Removes the entry <parameter>name</parameter> in the directory
        <parameter>parent</parameter> from the procfs. Entries are
        removed by their <emphasis>name</emphasis>, not by the
        <structname>struct proc_dir_entry</structname> returned by the
        various create functions. Note that this function doesn't
        recursively remove entries.
      </para>

      <para>
        Be sure to free the <structfield>data</structfield> entry from
        the <structname>struct proc_dir_entry</structname> before
        <function>remove_proc_entry</function> is called (that is: if
        there was some <structfield>data</structfield> allocated, of
        course). See <xref linkend="usingdata"/> for more information
        on using the <structfield>data</structfield> entry.
      </para>
    </sect1>
  </chapter>




  <chapter id="userland">
    <title>Communicating with userland</title>
    
    <para>
       Instead of reading (or writing) information directly from
       kernel memory, procfs works with <emphasis>call back
       functions</emphasis> for files: functions that are called when
       a specific file is being read or written. Such functions have
       to be initialised after the procfs file is created by setting
       the <structfield>read_proc</structfield> and/or
       <structfield>write_proc</structfield> fields in the
       <structname>struct proc_dir_entry*</structname> that the
       function <function>create_proc_entry</function> returned:
    </para>

    <programlisting>
struct proc_dir_entry* entry;

entry->read_proc = read_proc_foo;
entry->write_proc = write_proc_foo;
    </programlisting>

    <para>
      If you only want to use a the
      <structfield>read_proc</structfield>, the function
      <function>create_proc_read_entry</function> described in <xref
      linkend="convenience"/> may be used to create and initialise the
      procfs entry in one single call.
    </para>



    <sect1 id="Reading_data">
      <title>Reading data</title>

      <para>
        The read function is a call back function that allows userland
        processes to read data from the kernel. The read function
        should have the following format:
      </para>

      <funcsynopsis>
	<funcprototype>
	  <funcdef>int <function>read_func</function></funcdef>
	  <paramdef>char* <parameter>buffer</parameter></paramdef>
	  <paramdef>char** <parameter>start</parameter></paramdef>
	  <paramdef>off_t <parameter>off</parameter></paramdef>
	  <paramdef>int <parameter>count</parameter></paramdef>
	  <paramdef>int* <parameter>peof</parameter></paramdef>
	  <paramdef>void* <parameter>data</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>
        The read function should write its information into the
        <parameter>buffer</parameter>, which will be exactly
        <literal>PAGE_SIZE</literal> bytes long.
      </para>

      <para>
        The parameter
        <parameter>peof</parameter> should be used to signal that the
        end of the file has been reached by writing
        <literal>1</literal> to the memory location
        <parameter>peof</parameter> points to.
      </para>

      <para>
        The <parameter>data</parameter>
        parameter can be used to create a single call back function for
        several files, see <xref linkend="usingdata"/>.
      </para>

      <para>
        The rest of the parameters and the return value are described
	by a comment in <filename>fs/proc/generic.c</filename> as follows:
      </para>

      <blockquote>
        <para>
	You have three ways to return data:
       	</para>
        <orderedlist>
          <listitem>
            <para>
	      Leave <literal>*start = NULL</literal>.  (This is the default.)
	      Put the data of the requested offset at that
	      offset within the buffer.  Return the number (<literal>n</literal>)
	      of bytes there are from the beginning of the
	      buffer up to the last byte of data.  If the
	      number of supplied bytes (<literal>= n - offset</literal>) is
	      greater than zero and you didn't signal eof
	      and the reader is prepared to take more data
	      you will be called again with the requested
	      offset advanced by the number of bytes
	      absorbed.  This interface is useful for files
	      no larger than the buffer.
	    </para>
	  </listitem>
	  <listitem>
            <para>
	      Set <literal>*start</literal> to an unsigned long value less than
	      the buffer address but greater than zero.
	      Put the data of the requested offset at the
	      beginning of the buffer.  Return the number of
	      bytes of data placed there.  If this number is
	      greater than zero and you didn't signal eof
	      and the reader is prepared to take more data
	      you will be called again with the requested
	      offset advanced by <literal>*start</literal>.  This interface is
	      useful when you have a large file consisting
	      of a series of blocks which you want to count
	      and return as wholes.
	      (Hack by Paul.Russell@rustcorp.com.au)
	    </para>
	  </listitem>
	  <listitem>
            <para>
	      Set <literal>*start</literal> to an address within the buffer.
	      Put the data of the requested offset at <literal>*start</literal>.
	      Return the number of bytes of data placed there.
	      If this number is greater than zero and you
	      didn't signal eof and the reader is prepared to
	      take more data you will be called again with the
	      requested offset advanced by the number of bytes
	      absorbed.
	    </para>
	  </listitem>
	</orderedlist>
      </blockquote>

      <para>
        <xref linkend="example"/> shows how to use a read call back
        function.
      </para>
    </sect1>




    <sect1 id="Writing_data">
      <title>Writing data</title>

      <para>
        The write call back function allows a userland process to write
        data to the kernel, so it has some kind of control over the
        kernel. The write function should have the following format:
      </para>

      <funcsynopsis>
	<funcprototype>
	  <funcdef>int <function>write_func</function></funcdef>
	  <paramdef>struct file* <parameter>file</parameter></paramdef>
	  <paramdef>const char* <parameter>buffer</parameter></paramdef>
	  <paramdef>unsigned long <parameter>count</parameter></paramdef>
	  <paramdef>void* <parameter>data</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>
        The write function should read <parameter>count</parameter>
        bytes at maximum from the <parameter>buffer</parameter>. Note
        that the <parameter>buffer</parameter> doesn't live in the
        kernel's memory space, so it should first be copied to kernel
        space with <function>copy_from_user</function>. The
        <parameter>file</parameter> parameter is usually
        ignored. <xref linkend="usingdata"/> shows how to use the
        <parameter>data</parameter> parameter.
      </para>

      <para>
        Again, <xref linkend="example"/> shows how to use this call back
        function.
      </para>
    </sect1>




    <sect1 id="usingdata">
      <title>A single call back for many files</title>

      <para>
         When a large number of almost identical files is used, it's
         quite inconvenient to use a separate call back function for
         each file. A better approach is to have a single call back
         function that distinguishes between the files by using the
         <structfield>data</structfield> field in <structname>struct
         proc_dir_entry</structname>. First of all, the
         <structfield>data</structfield> field has to be initialised:
      </para>

      <programlisting>
struct proc_dir_entry* entry;
struct my_file_data *file_data;

file_data = kmalloc(sizeof(struct my_file_data), GFP_KERNEL);
entry->data = file_data;
      </programlisting>
     
      <para>
          The <structfield>data</structfield> field is a <type>void
          *</type>, so it can be initialised with anything.
      </para>

      <para>
        Now that the <structfield>data</structfield> field is set, the
        <function>read_proc</function> and
        <function>write_proc</function> can use it to distinguish
        between files because they get it passed into their
        <parameter>data</parameter> parameter:
      </para>

      <programlisting>
int foo_read_func(char *page, char **start, off_t off,
                  int count, int *eof, void *data)
{
        int len;

        if(data == file_data) {
                /* special case for this file */
        } else {
                /* normal processing */
        }

        return len;
}
      </programlisting>

      <para>
        Be sure to free the <structfield>data</structfield> data field
        when removing the procfs entry.
      </para>
    </sect1>
  </chapter>




  <chapter id="tips">
    <title>Tips and tricks</title>




    <sect1 id="convenience">
      <title>Convenience functions</title>

      <funcsynopsis>
	<funcprototype>
	  <funcdef>struct proc_dir_entry* <function>create_proc_read_entry</function></funcdef>
	  <paramdef>const char* <parameter>name</parameter></paramdef>
	  <paramdef>mode_t <parameter>mode</parameter></paramdef>
	  <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
	  <paramdef>read_proc_t* <parameter>read_proc</parameter></paramdef>
	  <paramdef>void* <parameter>data</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>
      
      <para>
        This function creates a regular file in exactly the same way
        as <function>create_proc_entry</function> from <xref
        linkend="regularfile"/> does, but also allows to set the read
        function <parameter>read_proc</parameter> in one call. This
        function can set the <parameter>data</parameter> as well, like
        explained in <xref linkend="usingdata"/>.
      </para>
    </sect1>



    <sect1 id="Modules">
      <title>Modules</title>

      <para>
        If procfs is being used from within a module, be sure to set
        the <structfield>owner</structfield> field in the
        <structname>struct proc_dir_entry</structname> to
        <constant>THIS_MODULE</constant>.
      </para>

      <programlisting>
struct proc_dir_entry* entry;

entry->owner = THIS_MODULE;
      </programlisting>
    </sect1>




    <sect1 id="Mode_and_ownership">
      <title>Mode and ownership</title>

      <para>
        Sometimes it is useful to change the mode and/or ownership of
        a procfs entry. Here is an example that shows how to achieve
        that:
      </para>

      <programlisting>
struct proc_dir_entry* entry;

entry->mode =  S_IWUSR |S_IRUSR | S_IRGRP | S_IROTH;
entry->uid = 0;
entry->gid = 100;
      </programlisting>

    </sect1>
  </chapter>




  <chapter id="example">
    <title>Example</title>

    <!-- be careful with the example code: it shouldn't be wider than
    approx. 60 columns, or otherwise it won't fit properly on a page
    -->

&procfsexample;

  </chapter>
</book>
s="hl opt">) & RXOVER)) { port->icount.overrun++; tty_insert_flip_char(tport, 0, TTY_OVERRUN); } /* and now the main RX loop */ while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) { unsigned int c; char flag = TTY_NORMAL; c = readw(port->membase + VT8500_RXFIFO) & 0x3ff; /* Mask conditions we're ignorning. */ c &= ~port->read_status_mask; if (c & FER) { port->icount.frame++; flag = TTY_FRAME; } else if (c & PER) { port->icount.parity++; flag = TTY_PARITY; } port->icount.rx++; if (!uart_handle_sysrq_char(port, c)) tty_insert_flip_char(tport, c, flag); } spin_unlock(&port->lock); tty_flip_buffer_push(tport); spin_lock(&port->lock); } static void handle_tx(struct uart_port *port) { struct circ_buf *xmit = &port->state->xmit; if (port->x_char) { writeb(port->x_char, port->membase + VT8500_TXFIFO); port->icount.tx++; port->x_char = 0; } if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { vt8500_stop_tx(port); return; } while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) { if (uart_circ_empty(xmit)) break; writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO); xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); port->icount.tx++; } if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) uart_write_wakeup(port); if (uart_circ_empty(xmit)) vt8500_stop_tx(port); } static void vt8500_start_tx(struct uart_port *port) { struct vt8500_port *vt8500_port = container_of(port, struct vt8500_port, uart); vt8500_port->ier &= ~TX_FIFO_INTS; vt8500_write(port, vt8500_port->ier, VT8500_URIER); handle_tx(port); vt8500_port->ier |= TX_FIFO_INTS; vt8500_write(port, vt8500_port->ier, VT8500_URIER); } static void handle_delta_cts(struct uart_port *port) { port->icount.cts++; wake_up_interruptible(&port->state->port.delta_msr_wait); } static irqreturn_t vt8500_irq(int irq, void *dev_id) { struct uart_port *port = dev_id; unsigned long isr; spin_lock(&port->lock); isr = vt8500_read(port, VT8500_URISR); /* Acknowledge active status bits */ vt8500_write(port, isr, VT8500_URISR); if (isr & RX_FIFO_INTS) handle_rx(port); if (isr & TX_FIFO_INTS) handle_tx(port); if (isr & TCTS) handle_delta_cts(port); spin_unlock(&port->lock); return IRQ_HANDLED; } static unsigned int vt8500_tx_empty(struct uart_port *port) { return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ? TIOCSER_TEMT : 0; } static unsigned int vt8500_get_mctrl(struct uart_port *port) { unsigned int usr; usr = vt8500_read(port, VT8500_URUSR); if (usr & (1 << 4)) return TIOCM_CTS; else return 0; } static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl) { } static void vt8500_break_ctl(struct uart_port *port, int break_ctl) { if (break_ctl) vt8500_write(port, vt8500_read(port, VT8500_URLCR) | (1 << 9), VT8500_URLCR); } static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud) { unsigned long div; unsigned int loops = 1000; div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff); if (unlikely((baud < 900) || (baud > 921600))) div |= 7; else div |= (921600 / baud) - 1; while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops) cpu_relax(); vt8500_write(port, div, VT8500_URDIV); return baud; } static int vt8500_startup(struct uart_port *port) { struct vt8500_port *vt8500_port = container_of(port, struct vt8500_port, uart); int ret; snprintf(vt8500_port->name, sizeof(vt8500_port->name), "vt8500_serial%d", port->line); ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH, vt8500_port->name, port); if (unlikely(ret)) return ret; vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */ return 0; } static void vt8500_shutdown(struct uart_port *port) { struct vt8500_port *vt8500_port = container_of(port, struct vt8500_port, uart); vt8500_port->ier = 0; /* disable interrupts and FIFOs */ vt8500_write(&vt8500_port->uart, 0, VT8500_URIER); vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR); free_irq(port->irq, port); } static void vt8500_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old) { struct vt8500_port *vt8500_port = container_of(port, struct vt8500_port, uart); unsigned long flags; unsigned int baud, lcr; unsigned int loops = 1000; spin_lock_irqsave(&port->lock, flags); /* calculate and set baud rate */ baud = uart_get_baud_rate(port, termios, old, 900, 921600); baud = vt8500_set_baud_rate(port, baud); if (tty_termios_baud_rate(termios)) tty_termios_encode_baud_rate(termios, baud, baud); /* calculate parity */ lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR); lcr &= ~((1 << 5) | (1 << 4)); if (termios->c_cflag & PARENB) { lcr |= (1 << 4); termios->c_cflag &= ~CMSPAR; if (termios->c_cflag & PARODD) lcr |= (1 << 5); } /* calculate bits per char */ lcr &= ~(1 << 2); switch (termios->c_cflag & CSIZE) { case CS7: break; case CS8: default: lcr |= (1 << 2); termios->c_cflag &= ~CSIZE; termios->c_cflag |= CS8; break; } /* calculate stop bits */ lcr &= ~(1 << 3); if (termios->c_cflag & CSTOPB) lcr |= (1 << 3); /* set parity, bits per char, and stop bit */ vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR); /* Configure status bits to ignore based on termio flags. */ port->read_status_mask = 0; if (termios->c_iflag & IGNPAR) port->read_status_mask = FER | PER; uart_update_timeout(port, termios->c_cflag, baud); /* Reset FIFOs */ vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR); while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc) && --loops) cpu_relax(); /* Every possible FIFO-related interrupt */ vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS; /* * CTS flow control */ if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag)) vt8500_port->ier |= TCTS; vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR); vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER); spin_unlock_irqrestore(&port->lock, flags); } static const char *vt8500_type(struct uart_port *port) { struct vt8500_port *vt8500_port = container_of(port, struct vt8500_port, uart); return vt8500_port->name; } static void vt8500_release_port(struct uart_port *port) { } static int vt8500_request_port(struct uart_port *port) { return 0; } static void vt8500_config_port(struct uart_port *port, int flags) { port->type = PORT_VT8500; } static int vt8500_verify_port(struct uart_port *port, struct serial_struct *ser) { if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500)) return -EINVAL; if (unlikely(port->irq != ser->irq)) return -EINVAL; return 0; } static struct vt8500_port *vt8500_uart_ports[VT8500_MAX_PORTS]; static struct uart_driver vt8500_uart_driver; #ifdef CONFIG_SERIAL_VT8500_CONSOLE static inline void wait_for_xmitr(struct uart_port *port) { unsigned int status, tmout = 10000; /* Wait up to 10ms for the character(s) to be sent. */ do { status = vt8500_read(port, VT8500_URFIDX); if (--tmout == 0) break; udelay(1); } while (status & 0x10); } static void vt8500_console_putchar(struct uart_port *port, int c) { wait_for_xmitr(port); writeb(c, port->membase + VT8500_TXFIFO); } static void vt8500_console_write(struct console *co, const char *s, unsigned int count) { struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index]; unsigned long ier; BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr); ier = vt8500_read(&vt8500_port->uart, VT8500_URIER); vt8500_write(&vt8500_port->uart, VT8500_URIER, 0); uart_console_write(&vt8500_port->uart, s, count, vt8500_console_putchar); /* * Finally, wait for transmitter to become empty * and switch back to FIFO */ wait_for_xmitr(&vt8500_port->uart); vt8500_write(&vt8500_port->uart, VT8500_URIER, ier); } static int __init vt8500_console_setup(struct console *co, char *options) { struct vt8500_port *vt8500_port; int baud = 9600; int bits = 8; int parity = 'n'; int flow = 'n'; if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0)) return -ENXIO; vt8500_port = vt8500_uart_ports[co->index]; if (!vt8500_port) return -ENODEV; if (options) uart_parse_options(options, &baud, &parity, &bits, &flow); return uart_set_options(&vt8500_port->uart, co, baud, parity, bits, flow); } static struct console vt8500_console = { .name = "ttyWMT", .write = vt8500_console_write, .device = uart_console_device, .setup = vt8500_console_setup, .flags = CON_PRINTBUFFER, .index = -1, .data = &vt8500_uart_driver, }; #define VT8500_CONSOLE (&vt8500_console) #else #define VT8500_CONSOLE NULL #endif static struct uart_ops vt8500_uart_pops = { .tx_empty = vt8500_tx_empty, .set_mctrl = vt8500_set_mctrl, .get_mctrl = vt8500_get_mctrl, .stop_tx = vt8500_stop_tx, .start_tx = vt8500_start_tx, .stop_rx = vt8500_stop_rx, .enable_ms = vt8500_enable_ms, .break_ctl = vt8500_break_ctl, .startup = vt8500_startup, .shutdown = vt8500_shutdown, .set_termios = vt8500_set_termios, .type = vt8500_type, .release_port = vt8500_release_port, .request_port = vt8500_request_port, .config_port = vt8500_config_port, .verify_port = vt8500_verify_port, }; static struct uart_driver vt8500_uart_driver = { .owner = THIS_MODULE, .driver_name = "vt8500_serial", .dev_name = "ttyWMT", .nr = 6, .cons = VT8500_CONSOLE, }; static int vt8500_serial_probe(struct platform_device *pdev) { struct vt8500_port *vt8500_port; struct resource *mmres, *irqres; struct device_node *np = pdev->dev.of_node; int ret; int port; mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0); irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0); if (!mmres || !irqres) return -ENODEV; if (np) port = of_alias_get_id(np, "serial"); if (port >= VT8500_MAX_PORTS) port = -1; else port = -1; if (port < 0) { /* calculate the port id */ port = find_first_zero_bit(&vt8500_ports_in_use, sizeof(vt8500_ports_in_use)); } if (port >= VT8500_MAX_PORTS) return -ENODEV; /* reserve the port id */ if (test_and_set_bit(port, &vt8500_ports_in_use)) { /* port already in use - shouldn't really happen */ return -EBUSY; } vt8500_port = devm_kzalloc(&pdev->dev, sizeof(struct vt8500_port), GFP_KERNEL); if (!vt8500_port) return -ENOMEM; vt8500_port->uart.membase = devm_ioremap_resource(&pdev->dev, mmres); if (IS_ERR(vt8500_port->uart.membase)) return PTR_ERR(vt8500_port->uart.membase); vt8500_port->clk = of_clk_get(pdev->dev.of_node, 0); if (IS_ERR(vt8500_port->clk)) { dev_err(&pdev->dev, "failed to get clock\n"); return -EINVAL; } ret = clk_prepare_enable(vt8500_port->clk); if (ret) { dev_err(&pdev->dev, "failed to enable clock\n"); return ret; } vt8500_port->uart.type = PORT_VT8500; vt8500_port->uart.iotype = UPIO_MEM; vt8500_port->uart.mapbase = mmres->start; vt8500_port->uart.irq = irqres->start; vt8500_port->uart.fifosize = 16; vt8500_port->uart.ops = &vt8500_uart_pops; vt8500_port->uart.line = port; vt8500_port->uart.dev = &pdev->dev; vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF; vt8500_port->uart.uartclk = clk_get_rate(vt8500_port->clk); snprintf(vt8500_port->name, sizeof(vt8500_port->name), "VT8500 UART%d", pdev->id); vt8500_uart_ports[port] = vt8500_port; uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart); platform_set_drvdata(pdev, vt8500_port); return 0; } static int vt8500_serial_remove(struct platform_device *pdev) { struct vt8500_port *vt8500_port = platform_get_drvdata(pdev); clk_disable_unprepare(vt8500_port->clk); uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart); return 0; } static const struct of_device_id wmt_dt_ids[] = { { .compatible = "via,vt8500-uart", }, {} }; static struct platform_driver vt8500_platform_driver = { .probe = vt8500_serial_probe, .remove = vt8500_serial_remove, .driver = { .name = "vt8500_serial", .owner = THIS_MODULE, .of_match_table = wmt_dt_ids, }, }; static int __init vt8500_serial_init(void) { int ret; ret = uart_register_driver(&vt8500_uart_driver); if (unlikely(ret)) return ret; ret = platform_driver_register(&vt8500_platform_driver); if (unlikely(ret)) uart_unregister_driver(&vt8500_uart_driver); return ret; } static void __exit vt8500_serial_exit(void) { #ifdef CONFIG_SERIAL_VT8500_CONSOLE unregister_console(&vt8500_console); #endif platform_driver_unregister(&vt8500_platform_driver); uart_unregister_driver(&vt8500_uart_driver); } module_init(vt8500_serial_init); module_exit(vt8500_serial_exit); MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>"); MODULE_DESCRIPTION("Driver for vt8500 serial device"); MODULE_LICENSE("GPL v2");