static int amzn_sfp_probe()

in drivers/misc/sfp/amzn-sfp.c [324:388]


static int amzn_sfp_probe(struct i2c_client *client,
    const struct i2c_device_id *id)
{
	struct amzn_sfp_softc *sc;
	int error;

	/* Paranoia... */
	if (client == NULL || id == NULL)
		return -EINVAL;

	sc = devm_kzalloc(&client->dev, sizeof(*sc), GFP_KERNEL);
	if (sc == NULL)
		return -ENOMEM;

	sc->client = client;
	rt_mutex_init(&sc->lock);
	sc->sfp_type = id->driver_data;
	sc->cur_page = -1;	/* We don't know */
	i2c_set_clientdata(client, sc);

	sysfs_bin_attr_init(&sc->attr);
	sc->attr.attr.name = "eeprom";
	sc->attr.attr.mode = S_IWUSR | S_IRUGO;
	switch (sc->sfp_type) {
	case AMZN_SFP_TYPE_SFP_PLUS:
		/*
		 * SFP+ has DDI.  Which is a separate I2C EEPROM at address
		 * 0x51.  We expose this to user space as the second half
		 * of a double-sized EEPROM.  We use I2C address auto-
		 * increment to select the I2C device.
		 */
		sc->attr.size = 2 * AMZN_SFP_FULL_SIZE;
		break;
	case AMZN_SFP_TYPE_QSFP_PLUS:
	case AMZN_SFP_TYPE_QSFP28:
	case AMZN_SFP_TYPE_QSFP_DD:
		/*
		 * The EEPROM is divided into a lower and an upper half.
		 * The upper half is paged, with the 8-bit page number
		 * set in the lower half.  The paging is abstracted from
		 * user space by providing a single large EEPROM, that is
		 * the concatenation of all halves.  There are 257 halves:
		 * 1 lower half and 256 paged upper halves.
		 * The key objective is to guarantee atomicity of the page
		 * select and the register access on that page.
		 */
		sc->attr.size = 257 * AMZN_SFP_HALF_SIZE;
		break;
	default:
		dev_warn(&client->dev, "unknown SFP type %d; fix driver\n",
		    sc->sfp_type);
		sc->attr.size = AMZN_SFP_FULL_SIZE;
		break;
	}
	sc->attr.private = sc;
	sc->attr.read = amzn_sfp_read;
	sc->attr.write = amzn_sfp_write;
	error = sysfs_create_bin_file(&client->dev.kobj, &sc->attr);
	if (error)
		dev_err(&client->dev,
		    "unable to create 'eeprom' file in sysfs (error %d)\n",
		    error);

	return error;
}