ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: Anton Farygin <rider@altlinux.com>
To: devel@altlinux.ru
Subject: [devel] [gmane.linux.hotplug.devel] [code] simple udev helper for firmware loading
Date: Thu, 21 Jul 2005 09:55:15 +0400
Message-ID: <pan.2005.07.21.05.55.14.97934@altlinux.com> (raw)

А нам такое надо ?

Мне проверят не на чем... кто-то у нас firmware использует ?

Rgds,
Rider


On Wed, 20 Jul 2005 23:33:05 -0400, Bill Nottingham wrote:

> As part of the move to newer udev releases, we've been getting rid of
> various hotplug agents instead of udev rules. While some (pci, scsi, usb)
> are simple transforms, firmware loading still needs a helper.
> 
> Kay asked me to pass this along, so here it is.
> 
> Compile is simply:
> 	gcc -options_of_choice -o firmware_helper firmware_helper.c
> 
> It's invoked via a udev rule as such:
> 
> ACTION=="add", SUBSYSTEM=="firmware", FIRMWARE="*", \
>         RUN+="/sbin/firmware_helper"
> 
> Potential improvements:
> - better logging of errors
> - support for sysfs mounted somewhere other than /sys (port to libsysfs?)
> 
> But it should work fine as-is. Well, barring the inevitable bugs.
> 
> Bill/*
>  * A simple firmware helper program.
>  *
>  * Copyright 2005 Red Hat, Inc.
>  *
>  * This software may be freely redistributed under the terms of the GNU *
>  public license.
>  *
>  * 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., 675 Mass Ave, Cambridge, MA 02139, USA. *
>  */
> 
> #include <errno.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <syslog.h>
> #include <unistd.h>
> 
> #include <sys/stat.h>
> 
> #define FIRMWARE_PATH	"/lib/firmware"
> 
> 
> void log_err(char *firmware, int err) {
> 	char *driver;
> 	
> 	driver = getenv("PHYSDEVDRIVER");
> 	if (!driver)
> 		driver = "(unknown)";
> 	openlog("firmware_helper",LOG_PID,LOG_USER); syslog(LOG_ERR,"Loading of
> 	%s for %s driver failed: %s",firmware, driver, strerror(err));
> 	closelog();
> }
> }
> /* Set the 'loading' attribute for a firmware device.
>  * 1 == currently loading
>  * 0 == done loading
>  * -1 == error
>  */
> int set_loading(char *device, int value) {
> 	char *path;
> 	int rc;
> 	FILE *f;
> 	
> 	path = alloca(strlen(device)+15);
> 	sprintf(path,"/sys/%s/loading", device); f = fopen(path, "w");
> 	if (!f)
> 		return 1;
> 	rc = fprintf(f, "%d", value);
> 	fclose(f);
> 	if (rc < 0)
> 		return rc;
> 	return 0;
> }
> }
> 	
> int main(int argc, char **argv) {
> 	char *devpath, *firmware, *action;
> 	struct stat sbuf;
> 	int fw_fd, count;
> 	int rc;
> 	char *fw_path, *data_path;
> 	char *fw_buffer;
> 	
> 	devpath = getenv("DEVPATH");
> 	if (!devpath) return 0;
> 	
> 	firmware = getenv("FIRMWARE");
> 	if (!firmware) return 0;
> 	
> 	action = getenv("ACTION");
> 	if (strcmp(action,"add"))
> 		return 0;
> 
> 	set_loading(devpath, 1);
> 	
> 	fw_path = malloc(strlen(FIRMWARE_PATH) + strlen(firmware) + 2); if
> 	(!fw_path) {
> 		rc = errno;
> 		goto out_err;
> 	}
> 	}
> 	sprintf(fw_path,"%s/%s", FIRMWARE_PATH, firmware); fw_fd = open(fw_path,
> 	O_RDONLY);
> 	if (fw_fd == -1) {
> 		rc = errno;
> 		goto out_err;
> 	}
> 	}
> 	fstat(fw_fd, &sbuf);
> 	fw_buffer = malloc(sbuf.st_size);
> 	if (!fw_buffer) {
> 		rc = errno;
> 		goto out_err;
> 	}
> 	if (read(fw_fd, fw_buffer, sbuf.st_size) != sbuf.st_size) {
> 		rc = errno;
> 		goto out_err;
> 	}
> 	close(fw_fd);
> 	
> 	data_path = malloc(strlen(devpath) + 12); if (!data_path) {
> 		rc = errno;
> 		goto out_err;
> 	}
> 	}
> 	sprintf(data_path,"/sys/%s/data", devpath);
> 	
> 	fw_fd = open(data_path, O_RDWR);
> 	if (fw_fd == -1) {
> 		rc = errno;
> 		goto out_err;
> 	}
> 	}
> 	count = 0;
> 	while (count < sbuf.st_size) {
> 		int c;
> 		
> 		c = write(fw_fd,fw_buffer+count,sbuf.st_size-count); if (c <= 0) {
> 			rc = errno;
> 			goto out_err;
> 		}
> 		count += c;
> 	}
> 	close(fw_fd);
> 	set_loading(devpath, 0);
> 	return 0;
> out_err:
> 	close(fw_fd);
> 	set_loading(devpath, -1);
> 	log_err(fw_path, rc);
> 	return rc;
> }



             reply	other threads:[~2005-07-21  5:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-21  5:55 Anton Farygin [this message]
2005-07-21  6:41 ` Alexander Bokovoy
2005-07-21 15:54 ` Dmitry (Mothlike) Marochko
2005-07-22  6:07   ` [devel] " Anton Farygin
2005-07-22 16:38     ` Dmitry Marochko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pan.2005.07.21.05.55.14.97934@altlinux.com \
    --to=rider@altlinux.com \
    --cc=devel@altlinux.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git