<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf8">
<title>/usr/web/sources/contrib/cross/devgpio.c - Plan 9 from Bell Labs</title>
<!-- THIS FILE IS AUTOMATICALLY GENERATED. -->
<!-- EDIT sources.tr INSTEAD. -->
</meta>
</head>
<body>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"><a href="/plan9/">Plan 9 from Bell Labs</a>&rsquo;s /usr/web/sources/contrib/cross/devgpio.c</span></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
Copyright © 2009 Alcatel-Lucent.<br />
Distributed under the
<a href="/plan9/license.html">Lucent Public License version 1.02</a>.
<br />
<a href="/plan9/download.html">Download the Plan 9 distribution.</a>
</font>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<table width="100%" cellspacing=0 border=0><tr><td align="center">
<table cellspacing=0 cellpadding=5 bgcolor="#eeeeff"><tr><td align="left">
<pre>
<!-- END HEADER -->
/*
 * access to GPIO pins.
 */

#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

enum{
	Qrootdir = 0,
	Qgpiodir,
	Q17,
	Q18,
};

static Dirtab gpiorootdir[]={
	".",	{Qrootdir, 0, QTDIR},	0,	0555,
	"gpio",	{Qgpiodir, 0, QTDIR},	0,	0555,
};

static Dirtab gpiodir[]={
	".",	{Qgpiodir, 0, QTDIR},	0,	0555,
	"17",		{Q17, 0},	0,	0664,
	"18",		{Q18, 0},	0,	0664,
};

enum {
	Pin17 = 17,
	Pin18 = 18,
	Npins,

	Output = 0x01,

	Off = 0,
	On,
};

static int gpiostate[Npins];

extern void gpioout(uint pin, int set);
extern void gpiosel(uint pin, int func);

static void
gpioinit(void)
{
	gpiosel(Pin17, Output);
	gpiosel(Pin18, Output);
}

static int
gpiogen(Chan *c, char *name, Dirtab *, int, int s, Dir *dp)
{
	if ((ulong)c-&gt;qid.path == Qrootdir) {
		if (s == DEVDOTDOT){
			devdir(c, (Qid){Qrootdir, 0, QTDIR}, "#G", 0, eve, 0555, dp);
			return 1;
		}
		return devgen(c, name, gpiorootdir, nelem(gpiorootdir), s, dp);
	}
	if ((ulong)c-&gt;qid.path == Qgpiodir &amp;&amp; s == DEVDOTDOT){
		devdir(c, (Qid){Qrootdir, 0, QTDIR}, "#G", 0, eve, 0555, dp);
		return 1;
	}
	return devgen(c, name, gpiodir, nelem(gpiodir), s, dp);
}

static long
gpioread(Chan *c, void *a, long n, vlong offset)
{
	int pin;
	char *status;

	if(c-&gt;qid.type &amp; QTDIR)
		return devdirread(c, a, n, nil, 0, gpiogen);

	pin = 0;
	switch((ulong)c-&gt;qid.path){
	case Q17:
		pin = Pin17;
		break;
	case Q18:
		pin = Pin18;
		break;
	default:
		error(Ebadarg);
		break;
	}
	status = gpiostate[pin] ? "on" : "off";
	return readstr(offset, a, n, status);
}

static long
gpiowrite(Chan*c, void *a, long n, vlong)
{
	int state;
	char *ch, buf[20];

	if (n &gt;= sizeof(buf))
		error(Ebadarg);
	strncpy(buf, a, sizeof(buf));
	buf[sizeof(buf)-1] = '\0';
	ch = strrchr(buf, '\n');
	if (ch != nil)
		*ch = '\0';
	state = (strcmp(buf, "on") == 0);
	switch((ulong)c-&gt;qid.path){
	case Q17:
		gpiostate[Pin17] = state;
		gpioout(Pin17, state);
		break;
	case Q18:
		gpiostate[Pin18] = state;
		gpioout(Pin18, state);
		break;
	default:
		error(Ebadarg);
		break;
	}
	return n;
}

static Chan*
gpioattach(char* spec)
{
	return devattach('G', spec);
}

static Walkqid*	 
gpiowalk(Chan* c, Chan *nc, char** name, int nname)
{
	return devwalk(c, nc, name, nname, nil, 0, gpiogen);
}

static int	 
gpiostat(Chan* c, uchar* dp, int n)
{
	return devstat(c, dp, n, nil, 0, gpiogen);
}

static Chan*
gpioopen(Chan* c, int omode)
{
	return devopen(c, omode, nil, 0, gpiogen);
}

static void	 
gpioclose(Chan*)
{
}

Dev gpiodevtab = {
	'G',
	"gpio",

	devreset,
	gpioinit,
	devshutdown,
	gpioattach,
	gpiowalk,
	gpiostat,
	gpioopen,
	devcreate,
	gpioclose,
	gpioread,
	devbread,
	gpiowrite,
	devbwrite,
	devremove,
	devwstat,
};
<!-- BEGIN TAIL -->
</pre>
</td></tr></table>
</td></tr></table>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"></span></p>
<p style="margin-top: 0; margin-bottom: 0.50in"></p>
<p style="margin-top: 0; margin-bottom: 0.33in"></p>
<center><table border="0"><tr>
<td valign="middle"><a href="http://www.alcatel-lucent.com/"><img border="0" src="/plan9/img/logo_ft.gif" alt="Bell Labs" />
</a></td>
<td valign="middle"><a href="http://www.opensource.org"><img border="0" alt="OSI certified" src="/plan9/img/osi-certified-60x50.gif" />
</a></td>
<td><img style="padding-right: 45px;" alt="Powered by Plan 9" src="/plan9/img/power36.gif" />
</td>
</tr></table></center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center>
<span style="font-size: 10pt">(<a href="/plan9/">Return to Plan 9 Home Page</a>)</span>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
<span style="font-size: 10pt"><a href="http://www.lucent.com/copyright.html">Copyright</a></span>
<span style="font-size: 10pt">© 2009 Alcatel-Lucent.</span>
<span style="font-size: 10pt">All Rights Reserved.</span>
<br />
<span style="font-size: 10pt">Comments to</span>
<span style="font-size: 10pt"><a href="mailto:webmaster@plan9.bell-labs.com">webmaster@plan9.bell-labs.com</a>.</span>
</font></center>
</body>
</html>