Re: How to read a file from a device driver?

2000-03-20 Thread Gary T. Corcoran


Darrell,

> > I'm trying to initialize a network device, and I'm trying to download
> > code *into* my device from some binary system files.  There is no "user
> > space" or user process, for that matter, to deal with at this point. I
> > just want to (at this step) open a file(s) directly from my device
> > driver, read the file(s), and download the relevant parts to my device.
> 
> sysctl might also do the trick.  to answer your question, here's some code:
> 
> struct vnode *
> vp_open(char *fname)
> {
> struct vnode *vp;
> struct nameidata nd;
> int error;
> 
> NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, curproc);

< rest of good code sample snipped - see previous message>

This is great - thanks!

One question though (since I'm new to FreeBSD drivers):
Is "curproc" a global variable that will be set to the "right thing"
while my device driver is executing?   Presuming that it is, it looks
like I can just take your sample code and run with it...  :)

Thanks,
Gary


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-20 Thread Darrell Anderson

On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
>
> I'm trying to initialize a network device, and I'm trying to download
> code *into* my device from some binary system files.  There is no "user
> space" or user process, for that matter, to deal with at this point. I
> just want to (at this step) open a file(s) directly from my device
> driver, read the file(s), and download the relevant parts to my device.

sysctl might also do the trick.  to answer your question, here's some code:

struct vnode *
vp_open(char *fname)
{
struct vnode *vp;
struct nameidata nd;
int error;

NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, curproc);
if ((error = vn_open(&nd, FREAD|FWRITE|O_CREAT, 
 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != 0) {
printf("vp_open: failed for %s with error %d\n", fname, error);
return NULL;
}
vp = nd.ni_vp;
NDFREE(&nd, NDF_ONLY_PNBUF);
VOP_UNLOCK(vp, 0, curproc);
if (vp->v_type != VREG) {
printf("vp_open: %s not a regular file\n", fname);
vn_close(vp, FREAD|FWRITE, curproc->p_ucred, curproc);
return NULL;
}
return vp;  
}

int
vp_close(struct vnode *vp)
{
int error;

if (vp == NULL) {
printf("vp_close: caller supplied NULL vp\n");
return ENOENT;
}
if ((error = vn_close(vp, FREAD|FWRITE, curproc->p_ucred, curproc)) {
printf("vp_close: vn_close error %d\n", error);
}
return error;
}

int
vp_read(struct vnode *vp, off_t offset, int len, caddr_t data)
{
struct iovec iov;
struct uio uio;
int error;

iov.iov_base = data;
iov.iov_len = len;
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_offset = offset;
uio.uio_resid = iov.iov_len;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_rw = UIO_READ;
uio.uio_procp = NULL;

if ((error = vn_lock(vp, LK_SHARED|LK_RETRY, curproc)) == 0) {
error = VOP_READ(vp, &uio, IO_SYNC, curproc->p_ucred);
VOP_UNLOCK(vp, 0, curproc);
if (uio.uio_resid) {
bzero(data + (len - uio.uio_resid), uio.uio_resid);
}
}
return error;
}

-Darrell
-- 
Department of Computer Science, Duke University, Durham, NC 27708-0129
Darrell Anderson, [EMAIL PROTECTED], http://www.cs.duke.edu/~anderson


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-19 Thread Andrzej Bialecki

On Fri, 17 Mar 2000, Alfred Perlstein wrote:

> * Matthew N. Dodd <[EMAIL PROTECTED]> [000317 21:22] wrote:
> > On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
> > > I'm trying to initialize a network device, and I'm trying to download
> > > code *into* my device from some binary system files.  There is no
> > > "user space" or user process, for that matter, to deal with at this
> > > point. I just want to (at this step) open a file(s) directly from my
> > > device driver, read the file(s), and download the relevant parts to my
> > > device.
> > 
> > There isn't really any clean way of doing this so most drivers that need
> > to load firmware usually compile them in.  :/
> 
> Now that I think about it, with FreeBSD's ability to dynamically load
> and unload modules it would seem like using anything else would be
> pretty annoying unless there's something else we don't understand here.

I think if you combine the ability to load arbitrary chunks of data (from
within bootloader) as modules, with similar auto-loading as in the vfs
case, you'll have a good solution.

Andrzej Bialecki

//  <[EMAIL PROTECTED]> WebGiro AB, Sweden (http://www.webgiro.com)
// ---
// -- FreeBSD: The Power to Serve. http://www.freebsd.org 
// --- Small & Embedded FreeBSD: http://www.freebsd.org/~picobsd/ 




To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-18 Thread Wes Peters

"Daniel C. Sobral" wrote:
> 
> Wes Peters wrote:
> >
> > Can you repackage the binary as a data-only kld, with a couple of public
> > symbols wrapping the beginning and end of the binary, or a couple of
> > symbols with start and length?  You could then have the loader pre-
> > load the .ko at boot time.
> 
> As a matter of fact, there is no need to repackage it. Loader can load
> the file with a specific -t type, in which case the file is loaded as
> is. See, for instance, the splash screen code.

Coolness.  You know, I think this FreeBSD thing is really going to take
off one of these days.  ;^)

-- 
"Where am I, and what am I doing in this handbasket?"

Wes Peters Softweyr LLC
[EMAIL PROTECTED]   http://softweyr.com/


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-18 Thread Daniel C. Sobral

Wes Peters wrote:
> 
> Can you repackage the binary as a data-only kld, with a couple of public
> symbols wrapping the beginning and end of the binary, or a couple of
> symbols with start and length?  You could then have the loader pre-
> load the .ko at boot time.

As a matter of fact, there is no need to repackage it. Loader can load
the file with a specific -t type, in which case the file is loaded as
is. See, for instance, the splash screen code.

--
Daniel C. Sobral(8-DCS)
[EMAIL PROTECTED]
[EMAIL PROTECTED]

One Unix to rule them all, One Resolver to find them,
One IP to bring them all and in the zone bind them.



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Mark Newton

On Fri, Mar 17, 2000 at 11:00:08PM -0500, Gary T. Corcoran wrote:

 > Can someone please tell me how I can read a file from a device driver
 > in FreeBSD?  I need to download 2 or 3 relatively-large code files to
 > my device, choosing from amongst several different files depending on
 > which mode I'm operating in.  Therefore compiling-in the code is not
 > a reasonable choice.
 
Defer the initialization of the device until a user-mode process opens
it and performs an ioctl() on it.  The ioctl should take a (void *) to
a buffer containing a structure which says how long the code is, followed
by the code itself.  That avoids the whole problem of reading a file from
your driver, you can do it with a user-mode helper process.

 > If you can either tell me how to be able to read a file from my driver,
 > or point me to an example driver which does this, I would appreciate it.

I think the Stallion serial port drivers do something kinda similar.

- mark

-- 
Mark Newton   Email:  [EMAIL PROTECTED] (W)
Network Engineer  Email:  [EMAIL PROTECTED]  (H)
Internode Systems Pty Ltd Desk:   +61-8-82232999
"Network Man" - Anagram of "Mark Newton"  Mobile: +61-416-202-223


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Matthew N. Dodd

On Sat, 18 Mar 2000, Gary T. Corcoran wrote:
> Ugh.  This isn't the answer I was looking for...  :-( ;-) I can do
> this in Windows (the original driver), I can do this in Linux (our new
> port) via a slight kluge which temporarily fiddles with the segment
> pointers (via standard system routines) to make it seem as if our
> driver's buffer is in user space so that the standard system read()
> can be called.

Again, your best bet is to look at how the QUOTA stuff works; it reads and
writes to the quota files.

Heres the problem though;

If you compile your kernel in, the root filesystem isn't mounted until
after your drivers probe/attach routines have been called.

It would be better if you made your firmware a KLD so that it can be
loaded by the loader or demand loaded by the kernel linker when your
driver is loaded.

-- 
| Matthew N. Dodd  | '78 Datsun 280Z | '75 Volvo 164E | FreeBSD/NetBSD  |
| [EMAIL PROTECTED] |   2 x '84 Volvo 245DL| ix86,sparc,pmax |
| http://www.jurai.net/~winter | This Space For Rent  | ISO8802.5 4ever |



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Alfred Perlstein

* Gary T. Corcoran <[EMAIL PROTECTED]> [000317 21:46] wrote:
> 
> "Matthew N. Dodd" wrote:
> > 
> > On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
> > > I'm trying to initialize a network device, and I'm trying to download
> > > code *into* my device from some binary system files.  There is no
> > > "user space" or user process, for that matter, to deal with at this
> > > point. I just want to (at this step) open a file(s) directly from my
> > > device driver, read the file(s), and download the relevant parts to my
> > > device.
> > 
> > There isn't really any clean way of doing this so most drivers that need
> > to load firmware usually compile them in.  :/
> 
> Ugh.  This isn't the answer I was looking for...  :-(   ;-)
> I can do this in Windows (the original driver), I can do this in Linux
> (our new port) via a slight kluge which temporarily fiddles with the segment
> pointers (via standard system routines) to make it seem as if our driver's
> buffer is in user space so that the standard system read() can be called.
> 
> You mean I really can't do a relatively simple thing like read a file
> (which the _kernel_ really does, if you think about it) in my favorite
> operating system??  There's gotta be a way for a driver to do a read(),
> isn't there? :)  BTW, I did a search on the email archives, and Mike Smith
> implied that there *is* a way to read a file from a driver, but he only
> made a vague reference to a driver that did same, and I wasn't able to
> find any example code to give me a clue...

You're just going to have to grovel through the code somewhat,
sendfile is sort of useful but you must have an already open file
handle.  There's other code you can get to do that, like the quota
system (i'm pretty sure).

-- 
-Alfred Perlstein - [[EMAIL PROTECTED]|[EMAIL PROTECTED]]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Gary T. Corcoran


"Matthew N. Dodd" wrote:
> 
> On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
> > I'm trying to initialize a network device, and I'm trying to download
> > code *into* my device from some binary system files.  There is no
> > "user space" or user process, for that matter, to deal with at this
> > point. I just want to (at this step) open a file(s) directly from my
> > device driver, read the file(s), and download the relevant parts to my
> > device.
> 
> There isn't really any clean way of doing this so most drivers that need
> to load firmware usually compile them in.  :/

Ugh.  This isn't the answer I was looking for...  :-(   ;-)
I can do this in Windows (the original driver), I can do this in Linux
(our new port) via a slight kluge which temporarily fiddles with the segment
pointers (via standard system routines) to make it seem as if our driver's
buffer is in user space so that the standard system read() can be called.

You mean I really can't do a relatively simple thing like read a file
(which the _kernel_ really does, if you think about it) in my favorite
operating system??  There's gotta be a way for a driver to do a read(),
isn't there? :)  BTW, I did a search on the email archives, and Mike Smith
implied that there *is* a way to read a file from a driver, but he only
made a vague reference to a driver that did same, and I wasn't able to
find any example code to give me a clue...

Thanks,
Gary


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Wes Peters

"Gary T. Corcoran" wrote:
> 
> I'm trying to initialize a network device, and I'm trying to download
> code *into* my device from some binary system files.  There is no
> "user space" or user process, for that matter, to deal with at this point.
> I just want to (at this step) open a file(s) directly from my device
> driver, read the file(s), and download the relevant parts to my device.

Can you repackage the binary as a data-only kld, with a couple of public
symbols wrapping the beginning and end of the binary, or a couple of
symbols with start and length?  You could then have the loader pre-
load the .ko at boot time.

The only other way is to wait until the system is up and load the code
into your driver through an ioctl.

-- 
"Where am I, and what am I doing in this handbasket?"

Wes Peters Softweyr LLC
[EMAIL PROTECTED]   http://softweyr.com/


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Alfred Perlstein

* Matthew N. Dodd <[EMAIL PROTECTED]> [000317 21:22] wrote:
> On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
> > I'm trying to initialize a network device, and I'm trying to download
> > code *into* my device from some binary system files.  There is no
> > "user space" or user process, for that matter, to deal with at this
> > point. I just want to (at this step) open a file(s) directly from my
> > device driver, read the file(s), and download the relevant parts to my
> > device.
> 
> There isn't really any clean way of doing this so most drivers that need
> to load firmware usually compile them in.  :/

Now that I think about it, with FreeBSD's ability to dynamically load
and unload modules it would seem like using anything else would be
pretty annoying unless there's something else we don't understand here.

-- 
-Alfred Perlstein - [[EMAIL PROTECTED]|[EMAIL PROTECTED]]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Kenneth D. Merry

On Fri, Mar 17, 2000 at 23:59:28 -0500, Matthew N. Dodd wrote:
> On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
> > I'm trying to initialize a network device, and I'm trying to download
> > code *into* my device from some binary system files.  There is no
> > "user space" or user process, for that matter, to deal with at this
> > point. I just want to (at this step) open a file(s) directly from my
> > device driver, read the file(s), and download the relevant parts to my
> > device.
> 
> There isn't really any clean way of doing this so most drivers that need
> to load firmware usually compile them in.  :/

True enough.  The Alteon Tigon driver (sys/pci/if_ti.c) decides which one
of its firmware images (sys/pci/{ti_fw,ti_fw2}.h) based on the chip
revision.

So that's one possible example to look at.

Ken
-- 
Kenneth Merry
[EMAIL PROTECTED]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Matthew N. Dodd

On Fri, 17 Mar 2000, Gary T. Corcoran wrote:
> I'm trying to initialize a network device, and I'm trying to download
> code *into* my device from some binary system files.  There is no
> "user space" or user process, for that matter, to deal with at this
> point. I just want to (at this step) open a file(s) directly from my
> device driver, read the file(s), and download the relevant parts to my
> device.

There isn't really any clean way of doing this so most drivers that need
to load firmware usually compile them in.  :/

-- 
| Matthew N. Dodd  | '78 Datsun 280Z | '75 Volvo 164E | FreeBSD/NetBSD  |
| [EMAIL PROTECTED] |   2 x '84 Volvo 245DL| ix86,sparc,pmax |
| http://www.jurai.net/~winter | This Space For Rent  | ISO8802.5 4ever |



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Alfred Perlstein

* Gary T. Corcoran <[EMAIL PROTECTED]> [000317 20:54] wrote:
> 
> Alfred,
> 
> > > Can someone please tell me how I can read a file from a device driver
> > > in FreeBSD?  I need to download 2 or 3 relatively-large code files to
> > > my device, choosing from amongst several different files depending on
> > > which mode I'm operating in.  Therefore compiling-in the code is not
> > > a reasonable choice.
> > >
> > > If you can either tell me how to be able to read a file from my driver,
> > > or point me to an example driver which does this, I would appreciate it.
> > >
> > > I'm running FreeBSD 3.4.
> > >
> > > (not subscribed to this list, please always CC: me on replies)
> > 
> > I hope i'm not advocating abusing an interface here, but here's what
> > comes to mind...
> > 
> > Use an ioctl in your driver to pass in a pointer to your user
> > address space which depending on the ioctl request the size of the
> > file be written to the pointer, or that the pointer is where the
> > device should copyout() or use one of the functions from STORE(9)
> > to dump into the user address space.
> 
> Sorry, but either I'm not understanding what you're suggesting, or
> I didn't explain my need clearly...  ;-)

I had an inverted sense of what you wanted to accomplish, i thought
you had several different chunks of data that needed to be downloaded
from the driver.

> 
> I'm trying to initialize a network device, and I'm trying to download
> code *into* my device from some binary system files.  There is no
> "user space" or user process, for that matter, to deal with at this point.
> I just want to (at this step) open a file(s) directly from my device
> driver, read the file(s), and download the relevant parts to my device.
> 
> Does that change your answer?  :-)

Yes, in a couple of ways:

a) you can use a userland program to copy the data in like I suggested
   above, but you'll obviously be using 'fubyte' or 'copyin' instead of
   subyte or copyout (easiest)
b) you can make the datafiles into some sort of kernel loadable module (so so)
   you can load your module via the loader or once the system is up.
c) you can look at NAMEI and the fs code to figure out how this is done,
   one subsystem that manipulates files from kernel space is the quota
   system.
d) ? :)

good luck,
-- 
-Alfred Perlstein - [[EMAIL PROTECTED]|[EMAIL PROTECTED]]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Gary T. Corcoran


Alfred,

> > Can someone please tell me how I can read a file from a device driver
> > in FreeBSD?  I need to download 2 or 3 relatively-large code files to
> > my device, choosing from amongst several different files depending on
> > which mode I'm operating in.  Therefore compiling-in the code is not
> > a reasonable choice.
> >
> > If you can either tell me how to be able to read a file from my driver,
> > or point me to an example driver which does this, I would appreciate it.
> >
> > I'm running FreeBSD 3.4.
> >
> > (not subscribed to this list, please always CC: me on replies)
> 
> I hope i'm not advocating abusing an interface here, but here's what
> comes to mind...
> 
> Use an ioctl in your driver to pass in a pointer to your user
> address space which depending on the ioctl request the size of the
> file be written to the pointer, or that the pointer is where the
> device should copyout() or use one of the functions from STORE(9)
> to dump into the user address space.

Sorry, but either I'm not understanding what you're suggesting, or
I didn't explain my need clearly...  ;-)

I'm trying to initialize a network device, and I'm trying to download
code *into* my device from some binary system files.  There is no
"user space" or user process, for that matter, to deal with at this point.
I just want to (at this step) open a file(s) directly from my device
driver, read the file(s), and download the relevant parts to my device.

Does that change your answer?  :-)

Thanks,
Gary


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: How to read a file from a device driver?

2000-03-17 Thread Alfred Perlstein

* Gary T. Corcoran <[EMAIL PROTECTED]> [000317 20:16] wrote:
> 
> Can someone please tell me how I can read a file from a device driver
> in FreeBSD?  I need to download 2 or 3 relatively-large code files to
> my device, choosing from amongst several different files depending on
> which mode I'm operating in.  Therefore compiling-in the code is not
> a reasonable choice.
> 
> If you can either tell me how to be able to read a file from my driver,
> or point me to an example driver which does this, I would appreciate it.
> 
> I'm running FreeBSD 3.4.
> 
> (not subscribed to this list, please always CC: me on replies)

I hope i'm not advocating abusing an interface here, but here's what
comes to mind...

Use an ioctl in your driver to pass in a pointer to your user
address space which depending on the ioctl request the size of the
file be written to the pointer, or that the pointer is where the
device should copyout() or use one of the functions from STORE(9)
to dump into the user address space.

-- 
-Alfred Perlstein - [[EMAIL PROTECTED]|[EMAIL PROTECTED]]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



How to read a file from a device driver?

2000-03-17 Thread Gary T. Corcoran


Can someone please tell me how I can read a file from a device driver
in FreeBSD?  I need to download 2 or 3 relatively-large code files to
my device, choosing from amongst several different files depending on
which mode I'm operating in.  Therefore compiling-in the code is not
a reasonable choice.

If you can either tell me how to be able to read a file from my driver,
or point me to an example driver which does this, I would appreciate it.

I'm running FreeBSD 3.4.

(not subscribed to this list, please always CC: me on replies)

Thanks,
Gary Corcoran


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message