1315 lines · 33.4 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/idr.h>#include <linux/jiffies.h>#include <linux/kobject.h>#include <linux/list.h>#include <linux/mod_devicetable.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" 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_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 after * the immediate entry with @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(struct fw_unit *unit, int *id){	get_ids(&fw_parent_device(unit)->config_rom[5], id);	get_ids(unit->directory, 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,						   struct device_driver *drv){	const struct ieee1394_device_id *id_table =			container_of(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(struct device *dev); static int fw_unit_match(struct device *dev, 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 int fw_unit_remove(struct device *dev){	struct fw_driver *driver =			container_of(dev->driver, struct fw_driver, driver); 	return driver->remove(fw_unit(dev)), 0;} static int get_modalias(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(struct device *dev, struct kobj_uevent_env *env){	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;} 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 *dir;	int key, value, ret = -ENOENT; 	down_read(&fw_device_rwsem); 	if (is_fw_unit(dev))		dir = fw_unit(dev)->directory;	else		dir = fw_device(dev)->config_rom + 5; 	fw_csr_iterator_init(&ci, dir);	while (fw_csr_iterator_next(&ci, &key, &value))		if (attr->key == key) {			ret = snprintf(buf, buf ? PAGE_SIZE : 0,				       "0x%06x\n", value);			break;		} 	up_read(&fw_device_rwsem); 	return ret;} #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 *dir;	size_t bufsize;	char dummy_buf[2];	int ret; 	down_read(&fw_device_rwsem); 	if (is_fw_unit(dev))		dir = fw_unit(dev)->directory;	else		dir = fw_device(dev)->config_rom + 5; 	if (buf) {		bufsize = PAGE_SIZE - 1;	} else {		buf = dummy_buf;		bufsize = 1;	} 	ret = fw_csr_string(dir, attr->key, buf, bufsize); 	if (ret >= 0) {		/* Strip trailing whitespace and add newline. */		while (ret > 0 && isspace(buf[ret - 1]))			ret--;		strcpy(buf + ret, "\n");		ret++;	} 	up_read(&fw_device_rwsem); 	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 snprintf(buf, PAGE_SIZE, "%d\n",			(int)(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; 	down_read(&fw_device_rwsem);	length = device->config_rom_length * 4;	memcpy(buf, device->config_rom, length);	up_read(&fw_device_rwsem); 	return length;} static ssize_t guid_show(struct device *dev,			 struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev);	int ret; 	down_read(&fw_device_rwsem);	ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",		       device->config_rom[3], device->config_rom[4]);	up_read(&fw_device_rwsem); 	return ret;} static ssize_t is_local_show(struct device *dev,			     struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev); 	return sprintf(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:			specifier_id = value;			break;		case CSR_VERSION:			version = value;			break;		}	} 	return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);} static ssize_t units_show(struct device *dev,			  struct device_attribute *attr, char *buf){	struct fw_device *device = fw_device(dev);	struct fw_csr_iterator ci;	int key, value, i = 0; 	down_read(&fw_device_rwsem);	fw_csr_iterator_init(&ci, &device->config_rom[5]);	while (fw_csr_iterator_next(&ci, &key, &value)) {		if (key != (CSR_UNIT | CSR_DIRECTORY))			continue;		i += units_sprintf(&buf[i], ci.p + value - 1);		if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))			break;	}	up_read(&fw_device_rwsem); 	if (i)		buf[i - 1] = '\n'; 	return i;} static struct device_attribute fw_device_attributes[] = {	__ATTR_RO(config_rom),	__ATTR_RO(guid),	__ATTR_RO(is_local),	__ATTR_RO(units),	__ATTR_NULL,}; static int read_rom(struct fw_device *device,		    int generation, int index, u32 *data){	u64 offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;	int i, rcode; 	/* device->node_id, accessed below, must not be older than generation */	smp_rmb(); 	for (i = 10; i < 100; i += 10) {		rcode = fw_run_transaction(device->card,				TCODE_READ_QUADLET_REQUEST, device->node_id,				generation, device->max_speed, offset, data, 4);		if (rcode != RCODE_BUSY)			break;		msleep(i);	}	be32_to_cpus(data); 	return rcode;} #define MAX_CONFIG_ROM_SIZE 256 /* * Read the bus info block, perform a speed probe, and read all of the rest of

Lines 1–500 of 1315.