Add the basic sudoku subsystem and sudoku_type

This commit is contained in:
Sameer Rahmani 2020-03-31 22:08:11 +01:00
parent d9fcf6d8d7
commit 4508f3bdf8
2 changed files with 79 additions and 6 deletions

View File

@ -8,6 +8,49 @@ static MATRIX_ATTR_RO(is_valid);
static MATRIX_ATTR_RO(is_solved);
/**
* sudoku_create_file - create sysfs attribute file for the given sudoku.
* @sudoku: sudoku
* @attr: sudoku attribute descriptor.
*/
int sudoku_create_file(struct sudoku *s,
const struct matrix_attribute *attr)
{
int error = 0;
if (s) {
WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
"Attribute %s: write permission without 'store'\n",
attr->attr.name);
WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
"Attribute %s: read permission without 'show'\n",
attr->attr.name);
error = sysfs_create_file(&s->kobj, &attr->attr);
}
return error;
}
EXPORT_SYMBOL_GPL(sudoku_create_file);
static ssize_t is_valid_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct sudoku_attribute *s_attr = to_sudoku_attr(attr);
struct sudoku *s = to_sudoku(kobj);
ssize_t ret = -EIO;
if (s_attr->show)
ret = s_attr->show(s, s_attr, buf);
if (ret >= (ssize_t)PAGE_SIZE) {
printk("is_valid_show: %pS returned bad count\n",
s_attr->show);
}
return ret;
}
static int kstudy_init(void)
{
printk(KERN_ALERT "Init ksudoku.\n");

View File

@ -4,21 +4,49 @@
#include <linux/module.h>
#include <linux/sysfs.h>
ssize_t is_valid_show(void);
ssize_t is_valid_store(void);
#define to_sudoku(obj) container_of(obj, struct sudoku, kobj)
#define to_sudoku_attr(_attr) container_of(_attr, struct sudoku_attribute, attr)
struct sudoku;
static ssize_t is_valid_show(struct kobject *kobj, struct attribute *attr,
char *buf)
static ssize_t is_valid_store(void);
static ssize_t is_solved_show(struct kobject *kobj, struct attribute *attr,
char *buf);
static ssize_t is_solved_store(void);
ssize_t is_solved_show(void);
ssize_t is_solved_store(void);
struct matrix_attribute {
struct attribute attr;
ssize_t (*show)(void);
ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
char *buf);
ssize_t (*store)(void);
bool value;
};
struct matrix {
/*
* The type of sudoku, "struct sudoku" is embedded in. A class
* or bus can only contain one sudoku type.
* This identifies the sudoku type and carries type-specific
* information, equivalent to the kobj_type of a kobject.
* Since we have only one type it always will be the same button
* we're designing a subsystem so we have to make sysfs happy.
* If "name" is specified, the uevent will contain it in
* the DEVTYPE variable.
*/
struct sudoku_type {
const char *name;
char matrix[81];
const struct attribute_group **groups;
int (*uevent)(struct sudoku *s, struct kobj_uevent_env *env);
char *(*devnode)(struct sudoku *s, umode_t *mode,
kuid_t *uid, kgid_t *gid);
void (*release)(struct sudoku *s);
const struct sysfs_ops *sysfs_ops;
};
#define MATRIX_ATTR_RO(_name) \
@ -27,4 +55,6 @@ struct matrix {
#define MATRIX_ATTR_WO(_name) \
struct matrix_attribute matrix_##_name = __ATTR_WO(_name)
extern int sudoku_create_file(struct sudoku *s,
const struct matrix_attribute *attr)
#endif