1444 lines · 38.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * Device probing and sysfs code. * * Copyright (C) 2005-2006  Kristian Hoegsberg <krh@bitplanet.net> */ #include <linux/bug.h>#include <linux/ctype.h>#include <linux/delay.h>#include <linux/device.h>#include <linux/errno.h>#include <linux/firewire.h>#include <linux/firewire-constants.h>#include <linux/jiffies.h>#include <linux/kobject.h>#include <linux/list.h>#include <linux/module.h>#include <linux/mutex.h>#include <linux/random.h>#include <linux/rwsem.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/string.h>#include <linux/workqueue.h> #include <linux/atomic.h>#include <asm/byteorder.h> #include "core.h" #define ROOT_DIR_OFFSET	5 void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p){	ci->p = p + 1;	ci->end = ci->p + (p[0] >> 16);}EXPORT_SYMBOL(fw_csr_iterator_init); int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value){	*key = *ci->p >> 24;	*value = *ci->p & 0xffffff; 	return ci->p++ < ci->end;}EXPORT_SYMBOL(fw_csr_iterator_next); static const u32 *search_directory(const u32 *directory, int search_key){	struct fw_csr_iterator ci;	int key, value; 	search_key |= CSR_DIRECTORY; 	fw_csr_iterator_init(&ci, directory);	while (fw_csr_iterator_next(&ci, &key, &value)) {		if (key == search_key)			return ci.p - 1 + value;	} 	return NULL;} static const u32 *search_leaf(const u32 *directory, int search_key){	struct fw_csr_iterator ci;	int last_key = 0, key, value; 	fw_csr_iterator_init(&ci, directory);	while (fw_csr_iterator_next(&ci, &key, &value)) {		if (last_key == search_key &&		    key == (CSR_DESCRIPTOR | CSR_LEAF))			return ci.p - 1 + value; 		last_key = key;	} 	return NULL;} static int textual_leaf_to_string(const u32 *block, char *buf, size_t size){	unsigned int quadlets, i;	char c; 	if (!size || !buf)		return -EINVAL; 	quadlets = min(block[0] >> 16, 256U);	if (quadlets < 2)		return -ENODATA; 	if (block[1] != 0 || block[2] != 0)		/* unknown language/character set */		return -ENODATA; 	block += 3;	quadlets -= 2;	for (i = 0; i < quadlets * 4 && i < size - 1; i++) {		c = block[i / 4] >> (24 - 8 * (i % 4));		if (c == '\0')			break;		buf[i] = c;	}	buf[i] = '\0'; 	return i;} /** * fw_csr_string() - reads a string from the configuration ROM * @directory:	e.g. root directory or unit directory * @key:	the key of the preceding directory entry * @buf:	where to put the string * @size:	size of @buf, in bytes * * The string is taken from a minimal ASCII text descriptor leaf just after the entry with the * @key. The string is zero-terminated. An overlong string is silently truncated such that it * and the zero byte fit into @size. * * Returns strlen(buf) or a negative error code. */int fw_csr_string(const u32 *directory, int key, char *buf, size_t size){	const u32 *leaf = search_leaf(directory, key);	if (!leaf)		return -ENOENT; 	return textual_leaf_to_string(leaf, buf, size);}EXPORT_SYMBOL(fw_csr_string); static void get_ids(const u32 *directory, int *id){	struct fw_csr_iterator ci;	int key, value; 	fw_csr_iterator_init(&ci, directory);	while (fw_csr_iterator_next(&ci, &key, &value)) {		switch (key) {		case CSR_VENDOR:	id[0] = value; break;		case CSR_MODEL:		id[1] = value; break;		case CSR_SPECIFIER_ID:	id[2] = value; break;		case CSR_VERSION:	id[3] = value; break;		}	}} static void get_modalias_ids(const struct fw_unit *unit, int *id){	const u32 *root_directory = &fw_parent_device(unit)->config_rom[ROOT_DIR_OFFSET];	const u32 *directories[] = {NULL, NULL, NULL};	const u32 *vendor_directory;	int i; 	directories[0] = root_directory; 	// Legacy layout of configuration ROM described in Annex 1 of 'Configuration ROM for AV/C	// Devices 1.0 (December 12, 2000, 1394 Trading Association, TA Document 1999027)'.	vendor_directory = search_directory(root_directory, CSR_VENDOR);	if (!vendor_directory) {		directories[1] = unit->directory;	} else {		directories[1] = vendor_directory;		directories[2] = unit->directory;	} 	for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i)		get_ids(directories[i], id);} static bool match_ids(const struct ieee1394_device_id *id_table, int *id){	int match = 0; 	if (id[0] == id_table->vendor_id)		match |= IEEE1394_MATCH_VENDOR_ID;	if (id[1] == id_table->model_id)		match |= IEEE1394_MATCH_MODEL_ID;	if (id[2] == id_table->specifier_id)		match |= IEEE1394_MATCH_SPECIFIER_ID;	if (id[3] == id_table->version)		match |= IEEE1394_MATCH_VERSION; 	return (match & id_table->match_flags) == id_table->match_flags;} static const struct ieee1394_device_id *unit_match(struct device *dev,						   const struct device_driver *drv){	const struct ieee1394_device_id *id_table =			container_of_const(drv, struct fw_driver, driver)->id_table;	int id[] = {0, 0, 0, 0}; 	get_modalias_ids(fw_unit(dev), id); 	for (; id_table->match_flags != 0; id_table++)		if (match_ids(id_table, id))			return id_table; 	return NULL;} static bool is_fw_unit(const struct device *dev); static int fw_unit_match(struct device *dev, const struct device_driver *drv){	/* We only allow binding to fw_units. */	return is_fw_unit(dev) && unit_match(dev, drv) != NULL;} static int fw_unit_probe(struct device *dev){	struct fw_driver *driver =			container_of(dev->driver, struct fw_driver, driver); 	return driver->probe(fw_unit(dev), unit_match(dev, dev->driver));} static void fw_unit_remove(struct device *dev){	struct fw_driver *driver =			container_of(dev->driver, struct fw_driver, driver); 	driver->remove(fw_unit(dev));} static int get_modalias(const struct fw_unit *unit, char *buffer, size_t buffer_size){	int id[] = {0, 0, 0, 0}; 	get_modalias_ids(unit, id); 	return snprintf(buffer, buffer_size,			"ieee1394:ven%08Xmo%08Xsp%08Xver%08X",			id[0], id[1], id[2], id[3]);} static int fw_unit_uevent(const struct device *dev, struct kobj_uevent_env *env){	const struct fw_unit *unit = fw_unit(dev);	char modalias[64]; 	get_modalias(unit, modalias, sizeof(modalias)); 	if (add_uevent_var(env, "MODALIAS=%s", modalias))		return -ENOMEM; 	return 0;} const struct bus_type fw_bus_type = {	.name = "firewire",	.match = fw_unit_match,	.probe = fw_unit_probe,	.remove = fw_unit_remove,};EXPORT_SYMBOL(fw_bus_type); int fw_device_enable_phys_dma(struct fw_device *device){	int generation = device->generation; 	/* device->node_id, accessed below, must not be older than generation */	smp_rmb(); 	return device->card->driver->enable_phys_dma(device->card,						     device->node_id,						     generation);}EXPORT_SYMBOL(fw_device_enable_phys_dma); struct config_rom_attribute {	struct device_attribute attr;	u32 key;}; static ssize_t show_immediate(struct device *dev,			      struct device_attribute *dattr, char *buf){	struct config_rom_attribute *attr =		container_of(dattr, struct config_rom_attribute, attr);	struct fw_csr_iterator ci;	const u32 *directories[] = {NULL, NULL};	int i, value = -1; 	guard(rwsem_read)(&fw_device_rwsem); 	if (is_fw_unit(dev)) {		directories[0] = fw_unit(dev)->directory;	} else {		const u32 *root_directory = fw_device(dev)->config_rom + ROOT_DIR_OFFSET;		const u32 *vendor_directory = search_directory(root_directory, CSR_VENDOR); 		if (!vendor_directory) {			directories[0] = root_directory;		} else {			// Legacy layout of configuration ROM described in Annex 1 of			// 'Configuration ROM for AV/C Devices 1.0 (December 12, 2000, 1394 Trading			// Association, TA Document 1999027)'.			directories[0] = vendor_directory;			directories[1] = root_directory;		}	} 	for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i) {		int key, val; 		fw_csr_iterator_init(&ci, directories[i]);		while (fw_csr_iterator_next(&ci, &key, &val)) {			if (attr->key == key)				value = val;		}	} 	if (value < 0)		return -ENOENT; 	// Note that this function is also called by init_fw_attribute_group() with NULL pointer.	return buf ? sysfs_emit(buf, "0x%06x\n", value) : 0;} #define IMMEDIATE_ATTR(name, key)				\	{ __ATTR(name, S_IRUGO, show_immediate, NULL), key } static ssize_t show_text_leaf(struct device *dev,			      struct device_attribute *dattr, char *buf){	struct config_rom_attribute *attr =		container_of(dattr, struct config_rom_attribute, attr);	const u32 *directories[] = {NULL, NULL};	size_t bufsize;	char dummy_buf[2];	int i, ret = -ENOENT; 	guard(rwsem_read)(&fw_device_rwsem); 	if (is_fw_unit(dev)) {		directories[0] = fw_unit(dev)->directory;	} else {		const u32 *root_directory = fw_device(dev)->config_rom + ROOT_DIR_OFFSET;		const u32 *vendor_directory = search_directory(root_directory, CSR_VENDOR); 		if (!vendor_directory) {			directories[0] = root_directory;		} else {			// Legacy layout of configuration ROM described in Annex 1 of			// 'Configuration ROM for AV/C Devices 1.0 (December 12, 2000, 1394			// Trading Association, TA Document 1999027)'.			directories[0] = root_directory;			directories[1] = vendor_directory;		}	} 	// Note that this function is also called by init_fw_attribute_group() with NULL pointer.	if (buf) {		bufsize = PAGE_SIZE - 1;	} else {		buf = dummy_buf;		bufsize = 1;	} 	for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i) {		int result = fw_csr_string(directories[i], attr->key, buf, bufsize);		// Detected.		if (result >= 0) {			ret = result;		} else if (i == 0 && attr->key == CSR_VENDOR) {			// Sony DVMC-DA1 has configuration ROM such that the descriptor leaf entry			// in the root directory follows to the directory entry for vendor ID			// instead of the immediate value for vendor ID.			result = fw_csr_string(directories[i], CSR_DIRECTORY | attr->key, buf,					       bufsize);			if (result >= 0)				ret = result;		}	} 	if (ret < 0)		return ret; 	// Strip trailing whitespace and add newline.	while (ret > 0 && isspace(buf[ret - 1]))		ret--;	strcpy(buf + ret, "\n");	ret++; 	return ret;} #define TEXT_LEAF_ATTR(name, key)				\	{ __ATTR(name, S_IRUGO, show_text_leaf, NULL), key } static struct config_rom_attribute config_rom_attributes[] = {	IMMEDIATE_ATTR(vendor, CSR_VENDOR),	IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),	IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),	IMMEDIATE_ATTR(version, CSR_VERSION),	IMMEDIATE_ATTR(model, CSR_MODEL),	TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),	TEXT_LEAF_ATTR(model_name, CSR_MODEL),	TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),}; static void init_fw_attribute_group(struct device *dev,				    struct device_attribute *attrs,				    struct fw_attribute_group *group){	struct device_attribute *attr;	int i, j; 	for (j = 0; attrs[j].attr.name != NULL; j++)		group->attrs[j] = &attrs[j].attr; 	for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {		attr = &config_rom_attributes[i].attr;		if (attr->show(dev, attr, NULL) < 0)			continue;		group->attrs[j++] = &attr->attr;	} 	group->attrs[j] = NULL;	group->groups[0] = &group->group;	group->groups[1] = NULL;	group->group.attrs = group->attrs;	dev->groups = (const struct attribute_group **) group->groups;} static ssize_t modalias_show(struct device *dev,			     struct device_attribute *attr, char *buf){	struct fw_unit *unit = fw_unit(dev);	int length; 	length = get_modalias(unit, buf, PAGE_SIZE);	strcpy(buf + length, "\n"); 	return length + 1;} static ssize_t rom_index_show(struct device *dev,			      struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev->parent);	struct fw_unit *unit = fw_unit(dev); 	return sysfs_emit(buf, "%td\n", unit->directory - device->config_rom);} static struct device_attribute fw_unit_attributes[] = {	__ATTR_RO(modalias),	__ATTR_RO(rom_index),	__ATTR_NULL,}; static ssize_t config_rom_show(struct device *dev,			       struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev);	size_t length; 	guard(rwsem_read)(&fw_device_rwsem); 	length = device->config_rom_length * 4;	memcpy(buf, device->config_rom, length); 	return length;} static ssize_t guid_show(struct device *dev,			 struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev); 	guard(rwsem_read)(&fw_device_rwsem); 	return sysfs_emit(buf, "0x%08x%08x\n", device->config_rom[3], device->config_rom[4]);} static ssize_t is_local_show(struct device *dev,			     struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev); 	return sysfs_emit(buf, "%u\n", device->is_local);} static int units_sprintf(char *buf, const u32 *directory){	struct fw_csr_iterator ci;	int key, value;	int specifier_id = 0;	int version = 0; 	fw_csr_iterator_init(&ci, directory);	while (fw_csr_iterator_next(&ci, &key, &value)) {		switch (key) {		case CSR_SPECIFIER_ID:

Lines 1–500 of 1444.