1/*
2 * Stuff used by all variants of the driver
3 *
4 * Copyright (c) 2001 by Stefan Eilers,
5 *                       Hansjoerg Lipp <hjlipp@web.de>,
6 *                       Tilman Schmidt <tilman@imap.cc>.
7 *
8 * =====================================================================
9 *	This program is free software; you can redistribute it and/or
10 *	modify it under the terms of the GNU General Public License as
11 *	published by the Free Software Foundation; either version 2 of
12 *	the License, or (at your option) any later version.
13 * =====================================================================
14 */
15
16#include "gigaset.h"
17#include <linux/ctype.h>
18
19static ssize_t show_cidmode(struct device *dev,
20			    struct device_attribute *attr, char *buf)
21{
22	int ret;
23	unsigned long flags;
24	struct cardstate *cs = dev_get_drvdata(dev);
25
26	spin_lock_irqsave(&cs->lock, flags);
27	ret = sprintf(buf, "%u\n", cs->cidmode);
28	spin_unlock_irqrestore(&cs->lock, flags);
29
30	return ret;
31}
32
33static ssize_t set_cidmode(struct device *dev, struct device_attribute *attr,
34			   const char *buf, size_t count)
35{
36	struct cardstate *cs = dev_get_drvdata(dev);
37	long int value;
38	char *end;
39
40	value = simple_strtol(buf, &end, 0);
41	while (*end)
42		if (!isspace(*end++))
43			return -EINVAL;
44	if (value < 0 || value > 1)
45			return -EINVAL;
46
47	if (mutex_lock_interruptible(&cs->mutex))
48		return -ERESTARTSYS;
49
50	cs->waiting = 1;
51	if (!gigaset_add_event(cs, &cs->at_state, EV_PROC_CIDMODE,
52			       NULL, value, NULL)) {
53		cs->waiting = 0;
54		mutex_unlock(&cs->mutex);
55		return -ENOMEM;
56	}
57
58	gig_dbg(DEBUG_CMD, "scheduling PROC_CIDMODE");
59	gigaset_schedule_event(cs);
60
61	wait_event(cs->waitqueue, !cs->waiting);
62
63	mutex_unlock(&cs->mutex);
64
65	return count;
66}
67
68static DEVICE_ATTR(cidmode, S_IRUGO|S_IWUSR, show_cidmode, set_cidmode);
69
70/* free sysfs for device */
71void gigaset_free_dev_sysfs(struct cardstate *cs)
72{
73	if (!cs->tty_dev)
74		return;
75
76	gig_dbg(DEBUG_INIT, "removing sysfs entries");
77	device_remove_file(cs->tty_dev, &dev_attr_cidmode);
78}
79
80/* initialize sysfs for device */
81void gigaset_init_dev_sysfs(struct cardstate *cs)
82{
83	if (!cs->tty_dev)
84		return;
85
86	gig_dbg(DEBUG_INIT, "setting up sysfs");
87	if (device_create_file(cs->tty_dev, &dev_attr_cidmode))
88		dev_err(cs->dev, "could not create sysfs attribute\n");
89}
90