1bbecb07fSUwe Kleine-König // SPDX-License-Identifier: GPL-2.0
2bbecb07fSUwe Kleine-König /*
3bbecb07fSUwe Kleine-König * Copyright (C) 2015-2017 Pengutronix, Uwe Kleine-König <kernel@pengutronix.de>
4bbecb07fSUwe Kleine-König */
5bbecb07fSUwe Kleine-König #include <linux/kernel.h>
6bbecb07fSUwe Kleine-König #include <linux/device.h>
7bbecb07fSUwe Kleine-König #include <linux/module.h>
8bbecb07fSUwe Kleine-König #include <linux/slab.h>
9bbecb07fSUwe Kleine-König #include <linux/sysfs.h>
10bbecb07fSUwe Kleine-König
11bbecb07fSUwe Kleine-König #include "siox.h"
12bbecb07fSUwe Kleine-König
13bbecb07fSUwe Kleine-König /*
14bbecb07fSUwe Kleine-König * The lowest bit in the SIOX status word signals if the in-device watchdog is
15bbecb07fSUwe Kleine-König * ok. If the bit is set, the device is functional.
16bbecb07fSUwe Kleine-König *
17bbecb07fSUwe Kleine-König * On writing the watchdog timer is reset when this bit toggles.
18bbecb07fSUwe Kleine-König */
19bbecb07fSUwe Kleine-König #define SIOX_STATUS_WDG 0x01
20bbecb07fSUwe Kleine-König
21bbecb07fSUwe Kleine-König /*
22bbecb07fSUwe Kleine-König * Bits 1 to 3 of the status word read as the bitwise negation of what was
23bbecb07fSUwe Kleine-König * clocked in before. The value clocked in is changed in each cycle and so
24bbecb07fSUwe Kleine-König * allows to detect transmit/receive problems.
25bbecb07fSUwe Kleine-König */
26bbecb07fSUwe Kleine-König #define SIOX_STATUS_COUNTER 0x0e
27bbecb07fSUwe Kleine-König
28bbecb07fSUwe Kleine-König /*
29bbecb07fSUwe Kleine-König * Each Siox-Device has a 4 bit type number that is neither 0 nor 15. This is
30bbecb07fSUwe Kleine-König * available in the upper nibble of the read status.
31bbecb07fSUwe Kleine-König *
32bbecb07fSUwe Kleine-König * On write these bits are DC.
33bbecb07fSUwe Kleine-König */
34bbecb07fSUwe Kleine-König #define SIOX_STATUS_TYPE 0xf0
35bbecb07fSUwe Kleine-König
36297a344dSUwe Kleine-König #define CREATE_TRACE_POINTS
37297a344dSUwe Kleine-König #include <trace/events/siox.h>
38297a344dSUwe Kleine-König
39bbecb07fSUwe Kleine-König static bool siox_is_registered;
40bbecb07fSUwe Kleine-König
siox_master_lock(struct siox_master * smaster)41bbecb07fSUwe Kleine-König static void siox_master_lock(struct siox_master *smaster)
42bbecb07fSUwe Kleine-König {
43bbecb07fSUwe Kleine-König mutex_lock(&smaster->lock);
44bbecb07fSUwe Kleine-König }
45bbecb07fSUwe Kleine-König
siox_master_unlock(struct siox_master * smaster)46bbecb07fSUwe Kleine-König static void siox_master_unlock(struct siox_master *smaster)
47bbecb07fSUwe Kleine-König {
48bbecb07fSUwe Kleine-König mutex_unlock(&smaster->lock);
49bbecb07fSUwe Kleine-König }
50bbecb07fSUwe Kleine-König
siox_status_clean(u8 status_read,u8 status_written)51bbecb07fSUwe Kleine-König static inline u8 siox_status_clean(u8 status_read, u8 status_written)
52bbecb07fSUwe Kleine-König {
53bbecb07fSUwe Kleine-König /*
54bbecb07fSUwe Kleine-König * bits 3:1 of status sample the respective bit in the status
55bbecb07fSUwe Kleine-König * byte written in the previous cycle but inverted. So if you wrote the
56bbecb07fSUwe Kleine-König * status word as 0xa before (counter = 0b101), it is expected to get
57bbecb07fSUwe Kleine-König * back the counter bits as 0b010.
58bbecb07fSUwe Kleine-König *
59bbecb07fSUwe Kleine-König * So given the last status written this function toggles the there
60bbecb07fSUwe Kleine-König * unset counter bits in the read value such that the counter bits in
61bbecb07fSUwe Kleine-König * the return value are all zero iff the bits were read as expected to
62bbecb07fSUwe Kleine-König * simplify error detection.
63bbecb07fSUwe Kleine-König */
64bbecb07fSUwe Kleine-König
65bbecb07fSUwe Kleine-König return status_read ^ (~status_written & 0xe);
66bbecb07fSUwe Kleine-König }
67bbecb07fSUwe Kleine-König
siox_device_counter_error(struct siox_device * sdevice,u8 status_clean)68bbecb07fSUwe Kleine-König static bool siox_device_counter_error(struct siox_device *sdevice,
69bbecb07fSUwe Kleine-König u8 status_clean)
70bbecb07fSUwe Kleine-König {
71bbecb07fSUwe Kleine-König return (status_clean & SIOX_STATUS_COUNTER) != 0;
72bbecb07fSUwe Kleine-König }
73bbecb07fSUwe Kleine-König
siox_device_type_error(struct siox_device * sdevice,u8 status_clean)74bbecb07fSUwe Kleine-König static bool siox_device_type_error(struct siox_device *sdevice, u8 status_clean)
75bbecb07fSUwe Kleine-König {
76bbecb07fSUwe Kleine-König u8 statustype = (status_clean & SIOX_STATUS_TYPE) >> 4;
77bbecb07fSUwe Kleine-König
78bbecb07fSUwe Kleine-König /*
79bbecb07fSUwe Kleine-König * If the device knows which value the type bits should have, check
80bbecb07fSUwe Kleine-König * against this value otherwise just rule out the invalid values 0b0000
81bbecb07fSUwe Kleine-König * and 0b1111.
82bbecb07fSUwe Kleine-König */
83bbecb07fSUwe Kleine-König if (sdevice->statustype) {
84bbecb07fSUwe Kleine-König if (statustype != sdevice->statustype)
85bbecb07fSUwe Kleine-König return true;
86bbecb07fSUwe Kleine-König } else {
87bbecb07fSUwe Kleine-König switch (statustype) {
88bbecb07fSUwe Kleine-König case 0:
89bbecb07fSUwe Kleine-König case 0xf:
90bbecb07fSUwe Kleine-König return true;
91bbecb07fSUwe Kleine-König }
92bbecb07fSUwe Kleine-König }
93bbecb07fSUwe Kleine-König
94bbecb07fSUwe Kleine-König return false;
95bbecb07fSUwe Kleine-König }
96bbecb07fSUwe Kleine-König
siox_device_wdg_error(struct siox_device * sdevice,u8 status_clean)97bbecb07fSUwe Kleine-König static bool siox_device_wdg_error(struct siox_device *sdevice, u8 status_clean)
98bbecb07fSUwe Kleine-König {
99bbecb07fSUwe Kleine-König return (status_clean & SIOX_STATUS_WDG) == 0;
100bbecb07fSUwe Kleine-König }
101bbecb07fSUwe Kleine-König
102bbecb07fSUwe Kleine-König /*
103bbecb07fSUwe Kleine-König * If there is a type or counter error the device is called "unsynced".
104bbecb07fSUwe Kleine-König */
siox_device_synced(struct siox_device * sdevice)105bbecb07fSUwe Kleine-König bool siox_device_synced(struct siox_device *sdevice)
106bbecb07fSUwe Kleine-König {
107bbecb07fSUwe Kleine-König if (siox_device_type_error(sdevice, sdevice->status_read_clean))
108bbecb07fSUwe Kleine-König return false;
109bbecb07fSUwe Kleine-König
110bbecb07fSUwe Kleine-König return !siox_device_counter_error(sdevice, sdevice->status_read_clean);
111bbecb07fSUwe Kleine-König
112bbecb07fSUwe Kleine-König }
113bbecb07fSUwe Kleine-König EXPORT_SYMBOL_GPL(siox_device_synced);
114bbecb07fSUwe Kleine-König
115bbecb07fSUwe Kleine-König /*
116bbecb07fSUwe Kleine-König * A device is called "connected" if it is synced and the watchdog is not
117bbecb07fSUwe Kleine-König * asserted.
118bbecb07fSUwe Kleine-König */
siox_device_connected(struct siox_device * sdevice)119bbecb07fSUwe Kleine-König bool siox_device_connected(struct siox_device *sdevice)
120bbecb07fSUwe Kleine-König {
121bbecb07fSUwe Kleine-König if (!siox_device_synced(sdevice))
122bbecb07fSUwe Kleine-König return false;
123bbecb07fSUwe Kleine-König
124bbecb07fSUwe Kleine-König return !siox_device_wdg_error(sdevice, sdevice->status_read_clean);
125bbecb07fSUwe Kleine-König }
126bbecb07fSUwe Kleine-König EXPORT_SYMBOL_GPL(siox_device_connected);
127bbecb07fSUwe Kleine-König
siox_poll(struct siox_master * smaster)128bbecb07fSUwe Kleine-König static void siox_poll(struct siox_master *smaster)
129bbecb07fSUwe Kleine-König {
130bbecb07fSUwe Kleine-König struct siox_device *sdevice;
131bbecb07fSUwe Kleine-König size_t i = smaster->setbuf_len;
132297a344dSUwe Kleine-König unsigned int devno = 0;
133bbecb07fSUwe Kleine-König int unsync_error = 0;
134bbecb07fSUwe Kleine-König
135bbecb07fSUwe Kleine-König smaster->last_poll = jiffies;
136bbecb07fSUwe Kleine-König
137bbecb07fSUwe Kleine-König /*
138bbecb07fSUwe Kleine-König * The counter bits change in each second cycle, the watchdog bit
139bbecb07fSUwe Kleine-König * toggles each time.
140bbecb07fSUwe Kleine-König * The counter bits hold values from [0, 6]. 7 would be possible
141bbecb07fSUwe Kleine-König * theoretically but the protocol designer considered that a bad idea
142bbecb07fSUwe Kleine-König * for reasons unknown today. (Maybe that's because then the status read
143bbecb07fSUwe Kleine-König * back has only zeros in the counter bits then which might be confused
144bbecb07fSUwe Kleine-König * with a stuck-at-0 error. But for the same reason (with s/0/1/) 0
145bbecb07fSUwe Kleine-König * could be skipped.)
146bbecb07fSUwe Kleine-König */
147bbecb07fSUwe Kleine-König if (++smaster->status > 0x0d)
148bbecb07fSUwe Kleine-König smaster->status = 0;
149bbecb07fSUwe Kleine-König
150bbecb07fSUwe Kleine-König memset(smaster->buf, 0, smaster->setbuf_len);
151bbecb07fSUwe Kleine-König
152bbecb07fSUwe Kleine-König /* prepare data pushed out to devices in buf[0..setbuf_len) */
153bbecb07fSUwe Kleine-König list_for_each_entry(sdevice, &smaster->devices, node) {
154bbecb07fSUwe Kleine-König struct siox_driver *sdriver =
155bbecb07fSUwe Kleine-König to_siox_driver(sdevice->dev.driver);
156bbecb07fSUwe Kleine-König sdevice->status_written = smaster->status;
157bbecb07fSUwe Kleine-König
158bbecb07fSUwe Kleine-König i -= sdevice->inbytes;
159bbecb07fSUwe Kleine-König
160bbecb07fSUwe Kleine-König /*
161bbecb07fSUwe Kleine-König * If the device or a previous one is unsynced, don't pet the
162bbecb07fSUwe Kleine-König * watchdog. This is done to ensure that the device is kept in
163bbecb07fSUwe Kleine-König * reset when something is wrong.
164bbecb07fSUwe Kleine-König */
165bbecb07fSUwe Kleine-König if (!siox_device_synced(sdevice))
166bbecb07fSUwe Kleine-König unsync_error = 1;
167bbecb07fSUwe Kleine-König
168bbecb07fSUwe Kleine-König if (sdriver && !unsync_error)
169bbecb07fSUwe Kleine-König sdriver->set_data(sdevice, sdevice->status_written,
170bbecb07fSUwe Kleine-König &smaster->buf[i + 1]);
171bbecb07fSUwe Kleine-König else
172bbecb07fSUwe Kleine-König /*
173bbecb07fSUwe Kleine-König * Don't trigger watchdog if there is no driver or a
174bbecb07fSUwe Kleine-König * sync problem
175bbecb07fSUwe Kleine-König */
176bbecb07fSUwe Kleine-König sdevice->status_written &= ~SIOX_STATUS_WDG;
177bbecb07fSUwe Kleine-König
178bbecb07fSUwe Kleine-König smaster->buf[i] = sdevice->status_written;
179297a344dSUwe Kleine-König
180297a344dSUwe Kleine-König trace_siox_set_data(smaster, sdevice, devno, i);
181297a344dSUwe Kleine-König
182297a344dSUwe Kleine-König devno++;
183bbecb07fSUwe Kleine-König }
184bbecb07fSUwe Kleine-König
185bbecb07fSUwe Kleine-König smaster->pushpull(smaster, smaster->setbuf_len, smaster->buf,
186bbecb07fSUwe Kleine-König smaster->getbuf_len,
187bbecb07fSUwe Kleine-König smaster->buf + smaster->setbuf_len);
188bbecb07fSUwe Kleine-König
189bbecb07fSUwe Kleine-König unsync_error = 0;
190bbecb07fSUwe Kleine-König
191bbecb07fSUwe Kleine-König /* interpret data pulled in from devices in buf[setbuf_len..] */
192297a344dSUwe Kleine-König devno = 0;
193bbecb07fSUwe Kleine-König i = smaster->setbuf_len;
194bbecb07fSUwe Kleine-König list_for_each_entry(sdevice, &smaster->devices, node) {
195bbecb07fSUwe Kleine-König struct siox_driver *sdriver =
196bbecb07fSUwe Kleine-König to_siox_driver(sdevice->dev.driver);
197bbecb07fSUwe Kleine-König u8 status = smaster->buf[i + sdevice->outbytes - 1];
198bbecb07fSUwe Kleine-König u8 status_clean;
199bbecb07fSUwe Kleine-König u8 prev_status_clean = sdevice->status_read_clean;
200bbecb07fSUwe Kleine-König bool synced = true;
201bbecb07fSUwe Kleine-König bool connected = true;
202bbecb07fSUwe Kleine-König
203bbecb07fSUwe Kleine-König if (!siox_device_synced(sdevice))
204bbecb07fSUwe Kleine-König unsync_error = 1;
205bbecb07fSUwe Kleine-König
206bbecb07fSUwe Kleine-König /*
207bbecb07fSUwe Kleine-König * If the watchdog bit wasn't toggled in this cycle, report the
208bbecb07fSUwe Kleine-König * watchdog as active to give a consistent view for drivers and
209bbecb07fSUwe Kleine-König * sysfs consumers.
210bbecb07fSUwe Kleine-König */
211bbecb07fSUwe Kleine-König if (!sdriver || unsync_error)
212bbecb07fSUwe Kleine-König status &= ~SIOX_STATUS_WDG;
213bbecb07fSUwe Kleine-König
214bbecb07fSUwe Kleine-König status_clean =
215bbecb07fSUwe Kleine-König siox_status_clean(status,
216bbecb07fSUwe Kleine-König sdevice->status_written_lastcycle);
217bbecb07fSUwe Kleine-König
2187e6f7d24SUwe Kleine-König /* Check counter and type bits */
2197e6f7d24SUwe Kleine-König if (siox_device_counter_error(sdevice, status_clean) ||
2207e6f7d24SUwe Kleine-König siox_device_type_error(sdevice, status_clean)) {
2217e6f7d24SUwe Kleine-König bool prev_error;
222bbecb07fSUwe Kleine-König
223bbecb07fSUwe Kleine-König synced = false;
224bbecb07fSUwe Kleine-König
225bbecb07fSUwe Kleine-König /* only report a new error if the last cycle was ok */
2267e6f7d24SUwe Kleine-König prev_error =
227bbecb07fSUwe Kleine-König siox_device_counter_error(sdevice,
2287e6f7d24SUwe Kleine-König prev_status_clean) ||
2297e6f7d24SUwe Kleine-König siox_device_type_error(sdevice,
230bbecb07fSUwe Kleine-König prev_status_clean);
2317e6f7d24SUwe Kleine-König
2327e6f7d24SUwe Kleine-König if (!prev_error) {
233bbecb07fSUwe Kleine-König sdevice->status_errors++;
234bbecb07fSUwe Kleine-König sysfs_notify_dirent(sdevice->status_errors_kn);
235bbecb07fSUwe Kleine-König }
236bbecb07fSUwe Kleine-König }
237bbecb07fSUwe Kleine-König
238bbecb07fSUwe Kleine-König /* If the device is unsynced report the watchdog as active */
239bbecb07fSUwe Kleine-König if (!synced) {
240bbecb07fSUwe Kleine-König status &= ~SIOX_STATUS_WDG;
241bbecb07fSUwe Kleine-König status_clean &= ~SIOX_STATUS_WDG;
242bbecb07fSUwe Kleine-König }
243bbecb07fSUwe Kleine-König
244bbecb07fSUwe Kleine-König if (siox_device_wdg_error(sdevice, status_clean))
245bbecb07fSUwe Kleine-König connected = false;
246bbecb07fSUwe Kleine-König
247bbecb07fSUwe Kleine-König /* The watchdog state changed just now */
248bbecb07fSUwe Kleine-König if ((status_clean ^ prev_status_clean) & SIOX_STATUS_WDG) {
249bbecb07fSUwe Kleine-König sysfs_notify_dirent(sdevice->watchdog_kn);
250bbecb07fSUwe Kleine-König
251bbecb07fSUwe Kleine-König if (siox_device_wdg_error(sdevice, status_clean)) {
252bbecb07fSUwe Kleine-König struct kernfs_node *wd_errs =
253bbecb07fSUwe Kleine-König sdevice->watchdog_errors_kn;
254bbecb07fSUwe Kleine-König
255bbecb07fSUwe Kleine-König sdevice->watchdog_errors++;
256bbecb07fSUwe Kleine-König sysfs_notify_dirent(wd_errs);
257bbecb07fSUwe Kleine-König }
258bbecb07fSUwe Kleine-König }
259bbecb07fSUwe Kleine-König
260bbecb07fSUwe Kleine-König if (connected != sdevice->connected)
261bbecb07fSUwe Kleine-König sysfs_notify_dirent(sdevice->connected_kn);
262bbecb07fSUwe Kleine-König
263bbecb07fSUwe Kleine-König sdevice->status_read_clean = status_clean;
264bbecb07fSUwe Kleine-König sdevice->status_written_lastcycle = sdevice->status_written;
265bbecb07fSUwe Kleine-König sdevice->connected = connected;
266bbecb07fSUwe Kleine-König
267297a344dSUwe Kleine-König trace_siox_get_data(smaster, sdevice, devno, status_clean, i);
268297a344dSUwe Kleine-König
269bbecb07fSUwe Kleine-König /* only give data read to driver if the device is connected */
270bbecb07fSUwe Kleine-König if (sdriver && connected)
271bbecb07fSUwe Kleine-König sdriver->get_data(sdevice, &smaster->buf[i]);
272bbecb07fSUwe Kleine-König
273297a344dSUwe Kleine-König devno++;
274bbecb07fSUwe Kleine-König i += sdevice->outbytes;
275bbecb07fSUwe Kleine-König }
276bbecb07fSUwe Kleine-König }
277bbecb07fSUwe Kleine-König
siox_poll_thread(void * data)278bbecb07fSUwe Kleine-König static int siox_poll_thread(void *data)
279bbecb07fSUwe Kleine-König {
280bbecb07fSUwe Kleine-König struct siox_master *smaster = data;
281bbecb07fSUwe Kleine-König signed long timeout = 0;
282bbecb07fSUwe Kleine-König
283bbecb07fSUwe Kleine-König get_device(&smaster->dev);
284bbecb07fSUwe Kleine-König
285bbecb07fSUwe Kleine-König for (;;) {
286bbecb07fSUwe Kleine-König if (kthread_should_stop()) {
287bbecb07fSUwe Kleine-König put_device(&smaster->dev);
288bbecb07fSUwe Kleine-König return 0;
289bbecb07fSUwe Kleine-König }
290bbecb07fSUwe Kleine-König
291bbecb07fSUwe Kleine-König siox_master_lock(smaster);
292bbecb07fSUwe Kleine-König
293bbecb07fSUwe Kleine-König if (smaster->active) {
294bbecb07fSUwe Kleine-König unsigned long next_poll =
295bbecb07fSUwe Kleine-König smaster->last_poll + smaster->poll_interval;
296bbecb07fSUwe Kleine-König if (time_is_before_eq_jiffies(next_poll))
297bbecb07fSUwe Kleine-König siox_poll(smaster);
298bbecb07fSUwe Kleine-König
299bbecb07fSUwe Kleine-König timeout = smaster->poll_interval -
300bbecb07fSUwe Kleine-König (jiffies - smaster->last_poll);
301bbecb07fSUwe Kleine-König } else {
302bbecb07fSUwe Kleine-König timeout = MAX_SCHEDULE_TIMEOUT;
303bbecb07fSUwe Kleine-König }
304bbecb07fSUwe Kleine-König
305bbecb07fSUwe Kleine-König /*
306bbecb07fSUwe Kleine-König * Set the task to idle while holding the lock. This makes sure
307bbecb07fSUwe Kleine-König * that we don't sleep too long when the bus is reenabled before
308bbecb07fSUwe Kleine-König * schedule_timeout is reached.
309bbecb07fSUwe Kleine-König */
310bbecb07fSUwe Kleine-König if (timeout > 0)
311bbecb07fSUwe Kleine-König set_current_state(TASK_IDLE);
312bbecb07fSUwe Kleine-König
313bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
314bbecb07fSUwe Kleine-König
315bbecb07fSUwe Kleine-König if (timeout > 0)
316bbecb07fSUwe Kleine-König schedule_timeout(timeout);
317bbecb07fSUwe Kleine-König
318bbecb07fSUwe Kleine-König /*
319bbecb07fSUwe Kleine-König * I'm not clear if/why it is important to set the state to
320bbecb07fSUwe Kleine-König * RUNNING again, but it fixes a "do not call blocking ops when
321bbecb07fSUwe Kleine-König * !TASK_RUNNING;"-warning.
322bbecb07fSUwe Kleine-König */
323bbecb07fSUwe Kleine-König set_current_state(TASK_RUNNING);
324bbecb07fSUwe Kleine-König }
325bbecb07fSUwe Kleine-König }
326bbecb07fSUwe Kleine-König
__siox_start(struct siox_master * smaster)327bbecb07fSUwe Kleine-König static int __siox_start(struct siox_master *smaster)
328bbecb07fSUwe Kleine-König {
329bbecb07fSUwe Kleine-König if (!(smaster->setbuf_len + smaster->getbuf_len))
330bbecb07fSUwe Kleine-König return -ENODEV;
331bbecb07fSUwe Kleine-König
332bbecb07fSUwe Kleine-König if (!smaster->buf)
333bbecb07fSUwe Kleine-König return -ENOMEM;
334bbecb07fSUwe Kleine-König
335bbecb07fSUwe Kleine-König if (smaster->active)
336bbecb07fSUwe Kleine-König return 0;
337bbecb07fSUwe Kleine-König
338bbecb07fSUwe Kleine-König smaster->active = 1;
339bbecb07fSUwe Kleine-König wake_up_process(smaster->poll_thread);
340bbecb07fSUwe Kleine-König
341bbecb07fSUwe Kleine-König return 1;
342bbecb07fSUwe Kleine-König }
343bbecb07fSUwe Kleine-König
siox_start(struct siox_master * smaster)344bbecb07fSUwe Kleine-König static int siox_start(struct siox_master *smaster)
345bbecb07fSUwe Kleine-König {
346bbecb07fSUwe Kleine-König int ret;
347bbecb07fSUwe Kleine-König
348bbecb07fSUwe Kleine-König siox_master_lock(smaster);
349bbecb07fSUwe Kleine-König ret = __siox_start(smaster);
350bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
351bbecb07fSUwe Kleine-König
352bbecb07fSUwe Kleine-König return ret;
353bbecb07fSUwe Kleine-König }
354bbecb07fSUwe Kleine-König
__siox_stop(struct siox_master * smaster)355bbecb07fSUwe Kleine-König static int __siox_stop(struct siox_master *smaster)
356bbecb07fSUwe Kleine-König {
357bbecb07fSUwe Kleine-König if (smaster->active) {
358bbecb07fSUwe Kleine-König struct siox_device *sdevice;
359bbecb07fSUwe Kleine-König
360bbecb07fSUwe Kleine-König smaster->active = 0;
361bbecb07fSUwe Kleine-König
362bbecb07fSUwe Kleine-König list_for_each_entry(sdevice, &smaster->devices, node) {
363bbecb07fSUwe Kleine-König if (sdevice->connected)
364bbecb07fSUwe Kleine-König sysfs_notify_dirent(sdevice->connected_kn);
365bbecb07fSUwe Kleine-König sdevice->connected = false;
366bbecb07fSUwe Kleine-König }
367bbecb07fSUwe Kleine-König
368bbecb07fSUwe Kleine-König return 1;
369bbecb07fSUwe Kleine-König }
370bbecb07fSUwe Kleine-König return 0;
371bbecb07fSUwe Kleine-König }
372bbecb07fSUwe Kleine-König
siox_stop(struct siox_master * smaster)373bbecb07fSUwe Kleine-König static int siox_stop(struct siox_master *smaster)
374bbecb07fSUwe Kleine-König {
375bbecb07fSUwe Kleine-König int ret;
376bbecb07fSUwe Kleine-König
377bbecb07fSUwe Kleine-König siox_master_lock(smaster);
378bbecb07fSUwe Kleine-König ret = __siox_stop(smaster);
379bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
380bbecb07fSUwe Kleine-König
381bbecb07fSUwe Kleine-König return ret;
382bbecb07fSUwe Kleine-König }
383bbecb07fSUwe Kleine-König
type_show(struct device * dev,struct device_attribute * attr,char * buf)384bbecb07fSUwe Kleine-König static ssize_t type_show(struct device *dev,
385bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
386bbecb07fSUwe Kleine-König {
387bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
388bbecb07fSUwe Kleine-König
389bbecb07fSUwe Kleine-König return sprintf(buf, "%s\n", sdev->type);
390bbecb07fSUwe Kleine-König }
391bbecb07fSUwe Kleine-König
392bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(type);
393bbecb07fSUwe Kleine-König
inbytes_show(struct device * dev,struct device_attribute * attr,char * buf)394bbecb07fSUwe Kleine-König static ssize_t inbytes_show(struct device *dev,
395bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
396bbecb07fSUwe Kleine-König {
397bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
398bbecb07fSUwe Kleine-König
399bbecb07fSUwe Kleine-König return sprintf(buf, "%zu\n", sdev->inbytes);
400bbecb07fSUwe Kleine-König }
401bbecb07fSUwe Kleine-König
402bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(inbytes);
403bbecb07fSUwe Kleine-König
outbytes_show(struct device * dev,struct device_attribute * attr,char * buf)404bbecb07fSUwe Kleine-König static ssize_t outbytes_show(struct device *dev,
405bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
406bbecb07fSUwe Kleine-König {
407bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
408bbecb07fSUwe Kleine-König
409bbecb07fSUwe Kleine-König return sprintf(buf, "%zu\n", sdev->outbytes);
410bbecb07fSUwe Kleine-König }
411bbecb07fSUwe Kleine-König
412bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(outbytes);
413bbecb07fSUwe Kleine-König
status_errors_show(struct device * dev,struct device_attribute * attr,char * buf)414bbecb07fSUwe Kleine-König static ssize_t status_errors_show(struct device *dev,
415bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
416bbecb07fSUwe Kleine-König {
417bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
418bbecb07fSUwe Kleine-König unsigned int status_errors;
419bbecb07fSUwe Kleine-König
420bbecb07fSUwe Kleine-König siox_master_lock(sdev->smaster);
421bbecb07fSUwe Kleine-König
422bbecb07fSUwe Kleine-König status_errors = sdev->status_errors;
423bbecb07fSUwe Kleine-König
424bbecb07fSUwe Kleine-König siox_master_unlock(sdev->smaster);
425bbecb07fSUwe Kleine-König
426bbecb07fSUwe Kleine-König return sprintf(buf, "%u\n", status_errors);
427bbecb07fSUwe Kleine-König }
428bbecb07fSUwe Kleine-König
429bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(status_errors);
430bbecb07fSUwe Kleine-König
connected_show(struct device * dev,struct device_attribute * attr,char * buf)431bbecb07fSUwe Kleine-König static ssize_t connected_show(struct device *dev,
432bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
433bbecb07fSUwe Kleine-König {
434bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
435bbecb07fSUwe Kleine-König bool connected;
436bbecb07fSUwe Kleine-König
437bbecb07fSUwe Kleine-König siox_master_lock(sdev->smaster);
438bbecb07fSUwe Kleine-König
439bbecb07fSUwe Kleine-König connected = sdev->connected;
440bbecb07fSUwe Kleine-König
441bbecb07fSUwe Kleine-König siox_master_unlock(sdev->smaster);
442bbecb07fSUwe Kleine-König
443bbecb07fSUwe Kleine-König return sprintf(buf, "%u\n", connected);
444bbecb07fSUwe Kleine-König }
445bbecb07fSUwe Kleine-König
446bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(connected);
447bbecb07fSUwe Kleine-König
watchdog_show(struct device * dev,struct device_attribute * attr,char * buf)448bbecb07fSUwe Kleine-König static ssize_t watchdog_show(struct device *dev,
449bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
450bbecb07fSUwe Kleine-König {
451bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
452bbecb07fSUwe Kleine-König u8 status;
453bbecb07fSUwe Kleine-König
454bbecb07fSUwe Kleine-König siox_master_lock(sdev->smaster);
455bbecb07fSUwe Kleine-König
456bbecb07fSUwe Kleine-König status = sdev->status_read_clean;
457bbecb07fSUwe Kleine-König
458bbecb07fSUwe Kleine-König siox_master_unlock(sdev->smaster);
459bbecb07fSUwe Kleine-König
460bbecb07fSUwe Kleine-König return sprintf(buf, "%d\n", status & SIOX_STATUS_WDG);
461bbecb07fSUwe Kleine-König }
462bbecb07fSUwe Kleine-König
463bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(watchdog);
464bbecb07fSUwe Kleine-König
watchdog_errors_show(struct device * dev,struct device_attribute * attr,char * buf)465bbecb07fSUwe Kleine-König static ssize_t watchdog_errors_show(struct device *dev,
466bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
467bbecb07fSUwe Kleine-König {
468bbecb07fSUwe Kleine-König struct siox_device *sdev = to_siox_device(dev);
469bbecb07fSUwe Kleine-König unsigned int watchdog_errors;
470bbecb07fSUwe Kleine-König
471bbecb07fSUwe Kleine-König siox_master_lock(sdev->smaster);
472bbecb07fSUwe Kleine-König
473bbecb07fSUwe Kleine-König watchdog_errors = sdev->watchdog_errors;
474bbecb07fSUwe Kleine-König
475bbecb07fSUwe Kleine-König siox_master_unlock(sdev->smaster);
476bbecb07fSUwe Kleine-König
477bbecb07fSUwe Kleine-König return sprintf(buf, "%u\n", watchdog_errors);
478bbecb07fSUwe Kleine-König }
479bbecb07fSUwe Kleine-König
480bbecb07fSUwe Kleine-König static DEVICE_ATTR_RO(watchdog_errors);
481bbecb07fSUwe Kleine-König
482bbecb07fSUwe Kleine-König static struct attribute *siox_device_attrs[] = {
483bbecb07fSUwe Kleine-König &dev_attr_type.attr,
484bbecb07fSUwe Kleine-König &dev_attr_inbytes.attr,
485bbecb07fSUwe Kleine-König &dev_attr_outbytes.attr,
486bbecb07fSUwe Kleine-König &dev_attr_status_errors.attr,
487bbecb07fSUwe Kleine-König &dev_attr_connected.attr,
488bbecb07fSUwe Kleine-König &dev_attr_watchdog.attr,
489bbecb07fSUwe Kleine-König &dev_attr_watchdog_errors.attr,
490bbecb07fSUwe Kleine-König NULL
491bbecb07fSUwe Kleine-König };
492bbecb07fSUwe Kleine-König ATTRIBUTE_GROUPS(siox_device);
493bbecb07fSUwe Kleine-König
siox_device_release(struct device * dev)494bbecb07fSUwe Kleine-König static void siox_device_release(struct device *dev)
495bbecb07fSUwe Kleine-König {
496bbecb07fSUwe Kleine-König struct siox_device *sdevice = to_siox_device(dev);
497bbecb07fSUwe Kleine-König
498bbecb07fSUwe Kleine-König kfree(sdevice);
499bbecb07fSUwe Kleine-König }
500bbecb07fSUwe Kleine-König
501bbecb07fSUwe Kleine-König static struct device_type siox_device_type = {
502bbecb07fSUwe Kleine-König .groups = siox_device_groups,
503bbecb07fSUwe Kleine-König .release = siox_device_release,
504bbecb07fSUwe Kleine-König };
505bbecb07fSUwe Kleine-König
siox_match(struct device * dev,struct device_driver * drv)506bbecb07fSUwe Kleine-König static int siox_match(struct device *dev, struct device_driver *drv)
507bbecb07fSUwe Kleine-König {
508bbecb07fSUwe Kleine-König if (dev->type != &siox_device_type)
509bbecb07fSUwe Kleine-König return 0;
510bbecb07fSUwe Kleine-König
511bbecb07fSUwe Kleine-König /* up to now there is only a single driver so keeping this simple */
512bbecb07fSUwe Kleine-König return 1;
513bbecb07fSUwe Kleine-König }
514bbecb07fSUwe Kleine-König
siox_probe(struct device * dev)51507d9a767SUwe Kleine-König static int siox_probe(struct device *dev)
516bbecb07fSUwe Kleine-König {
517bbecb07fSUwe Kleine-König struct siox_driver *sdriver = to_siox_driver(dev->driver);
518bbecb07fSUwe Kleine-König struct siox_device *sdevice = to_siox_device(dev);
519bbecb07fSUwe Kleine-König
52007d9a767SUwe Kleine-König return sdriver->probe(sdevice);
521bbecb07fSUwe Kleine-König }
522bbecb07fSUwe Kleine-König
siox_remove(struct device * dev)523fc7a6209SUwe Kleine-König static void siox_remove(struct device *dev)
524bbecb07fSUwe Kleine-König {
525bbecb07fSUwe Kleine-König struct siox_driver *sdriver =
526bbecb07fSUwe Kleine-König container_of(dev->driver, struct siox_driver, driver);
527bbecb07fSUwe Kleine-König struct siox_device *sdevice = to_siox_device(dev);
528bbecb07fSUwe Kleine-König
52907d9a767SUwe Kleine-König if (sdriver->remove)
5301c12c270SUwe Kleine-König sdriver->remove(sdevice);
531bbecb07fSUwe Kleine-König }
532bbecb07fSUwe Kleine-König
siox_shutdown(struct device * dev)53307d9a767SUwe Kleine-König static void siox_shutdown(struct device *dev)
534bbecb07fSUwe Kleine-König {
535bbecb07fSUwe Kleine-König struct siox_device *sdevice = to_siox_device(dev);
53607d9a767SUwe Kleine-König struct siox_driver *sdriver;
537bbecb07fSUwe Kleine-König
53807d9a767SUwe Kleine-König if (!dev->driver)
53907d9a767SUwe Kleine-König return;
54007d9a767SUwe Kleine-König
54107d9a767SUwe Kleine-König sdriver = container_of(dev->driver, struct siox_driver, driver);
54207d9a767SUwe Kleine-König if (sdriver->shutdown)
543bbecb07fSUwe Kleine-König sdriver->shutdown(sdevice);
544bbecb07fSUwe Kleine-König }
545bbecb07fSUwe Kleine-König
54607d9a767SUwe Kleine-König static struct bus_type siox_bus_type = {
54707d9a767SUwe Kleine-König .name = "siox",
54807d9a767SUwe Kleine-König .match = siox_match,
54907d9a767SUwe Kleine-König .probe = siox_probe,
55007d9a767SUwe Kleine-König .remove = siox_remove,
55107d9a767SUwe Kleine-König .shutdown = siox_shutdown,
55207d9a767SUwe Kleine-König };
55307d9a767SUwe Kleine-König
active_show(struct device * dev,struct device_attribute * attr,char * buf)554bbecb07fSUwe Kleine-König static ssize_t active_show(struct device *dev,
555bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
556bbecb07fSUwe Kleine-König {
557bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
558bbecb07fSUwe Kleine-König
559bbecb07fSUwe Kleine-König return sprintf(buf, "%d\n", smaster->active);
560bbecb07fSUwe Kleine-König }
561bbecb07fSUwe Kleine-König
active_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)562bbecb07fSUwe Kleine-König static ssize_t active_store(struct device *dev,
563bbecb07fSUwe Kleine-König struct device_attribute *attr,
564bbecb07fSUwe Kleine-König const char *buf, size_t count)
565bbecb07fSUwe Kleine-König {
566bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
567bbecb07fSUwe Kleine-König int ret;
568bbecb07fSUwe Kleine-König int active;
569bbecb07fSUwe Kleine-König
570bbecb07fSUwe Kleine-König ret = kstrtoint(buf, 0, &active);
571bbecb07fSUwe Kleine-König if (ret < 0)
572bbecb07fSUwe Kleine-König return ret;
573bbecb07fSUwe Kleine-König
574bbecb07fSUwe Kleine-König if (active)
575bbecb07fSUwe Kleine-König ret = siox_start(smaster);
576bbecb07fSUwe Kleine-König else
577bbecb07fSUwe Kleine-König ret = siox_stop(smaster);
578bbecb07fSUwe Kleine-König
579bbecb07fSUwe Kleine-König if (ret < 0)
580bbecb07fSUwe Kleine-König return ret;
581bbecb07fSUwe Kleine-König
582bbecb07fSUwe Kleine-König return count;
583bbecb07fSUwe Kleine-König }
584bbecb07fSUwe Kleine-König
585bbecb07fSUwe Kleine-König static DEVICE_ATTR_RW(active);
586bbecb07fSUwe Kleine-König
587bbecb07fSUwe Kleine-König static struct siox_device *siox_device_add(struct siox_master *smaster,
588bbecb07fSUwe Kleine-König const char *type, size_t inbytes,
589bbecb07fSUwe Kleine-König size_t outbytes, u8 statustype);
590bbecb07fSUwe Kleine-König
device_add_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)591bbecb07fSUwe Kleine-König static ssize_t device_add_store(struct device *dev,
592bbecb07fSUwe Kleine-König struct device_attribute *attr,
593bbecb07fSUwe Kleine-König const char *buf, size_t count)
594bbecb07fSUwe Kleine-König {
595bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
596bbecb07fSUwe Kleine-König int ret;
597bbecb07fSUwe Kleine-König char type[20] = "";
598bbecb07fSUwe Kleine-König size_t inbytes = 0, outbytes = 0;
599bbecb07fSUwe Kleine-König u8 statustype = 0;
600bbecb07fSUwe Kleine-König
601f87deadaSGavin Schenk ret = sscanf(buf, "%19s %zu %zu %hhu", type, &inbytes,
602bbecb07fSUwe Kleine-König &outbytes, &statustype);
603bbecb07fSUwe Kleine-König if (ret != 3 && ret != 4)
604bbecb07fSUwe Kleine-König return -EINVAL;
605bbecb07fSUwe Kleine-König
606bbecb07fSUwe Kleine-König if (strcmp(type, "siox-12x8") || inbytes != 2 || outbytes != 4)
607bbecb07fSUwe Kleine-König return -EINVAL;
608bbecb07fSUwe Kleine-König
609bbecb07fSUwe Kleine-König siox_device_add(smaster, "siox-12x8", inbytes, outbytes, statustype);
610bbecb07fSUwe Kleine-König
611bbecb07fSUwe Kleine-König return count;
612bbecb07fSUwe Kleine-König }
613bbecb07fSUwe Kleine-König
614bbecb07fSUwe Kleine-König static DEVICE_ATTR_WO(device_add);
615bbecb07fSUwe Kleine-König
616bbecb07fSUwe Kleine-König static void siox_device_remove(struct siox_master *smaster);
617bbecb07fSUwe Kleine-König
device_remove_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)618bbecb07fSUwe Kleine-König static ssize_t device_remove_store(struct device *dev,
619bbecb07fSUwe Kleine-König struct device_attribute *attr,
620bbecb07fSUwe Kleine-König const char *buf, size_t count)
621bbecb07fSUwe Kleine-König {
622bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
623bbecb07fSUwe Kleine-König
624bbecb07fSUwe Kleine-König /* XXX? require to write <type> <inbytes> <outbytes> */
625bbecb07fSUwe Kleine-König siox_device_remove(smaster);
626bbecb07fSUwe Kleine-König
627bbecb07fSUwe Kleine-König return count;
628bbecb07fSUwe Kleine-König }
629bbecb07fSUwe Kleine-König
630bbecb07fSUwe Kleine-König static DEVICE_ATTR_WO(device_remove);
631bbecb07fSUwe Kleine-König
poll_interval_ns_show(struct device * dev,struct device_attribute * attr,char * buf)632bbecb07fSUwe Kleine-König static ssize_t poll_interval_ns_show(struct device *dev,
633bbecb07fSUwe Kleine-König struct device_attribute *attr, char *buf)
634bbecb07fSUwe Kleine-König {
635bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
636bbecb07fSUwe Kleine-König
637bbecb07fSUwe Kleine-König return sprintf(buf, "%lld\n", jiffies_to_nsecs(smaster->poll_interval));
638bbecb07fSUwe Kleine-König }
639bbecb07fSUwe Kleine-König
poll_interval_ns_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)640bbecb07fSUwe Kleine-König static ssize_t poll_interval_ns_store(struct device *dev,
641bbecb07fSUwe Kleine-König struct device_attribute *attr,
642bbecb07fSUwe Kleine-König const char *buf, size_t count)
643bbecb07fSUwe Kleine-König {
644bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
645bbecb07fSUwe Kleine-König int ret;
646bbecb07fSUwe Kleine-König u64 val;
647bbecb07fSUwe Kleine-König
648bbecb07fSUwe Kleine-König ret = kstrtou64(buf, 0, &val);
649bbecb07fSUwe Kleine-König if (ret < 0)
650bbecb07fSUwe Kleine-König return ret;
651bbecb07fSUwe Kleine-König
652bbecb07fSUwe Kleine-König siox_master_lock(smaster);
653bbecb07fSUwe Kleine-König
654bbecb07fSUwe Kleine-König smaster->poll_interval = nsecs_to_jiffies(val);
655bbecb07fSUwe Kleine-König
656bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
657bbecb07fSUwe Kleine-König
658bbecb07fSUwe Kleine-König return count;
659bbecb07fSUwe Kleine-König }
660bbecb07fSUwe Kleine-König
661bbecb07fSUwe Kleine-König static DEVICE_ATTR_RW(poll_interval_ns);
662bbecb07fSUwe Kleine-König
663bbecb07fSUwe Kleine-König static struct attribute *siox_master_attrs[] = {
664bbecb07fSUwe Kleine-König &dev_attr_active.attr,
665bbecb07fSUwe Kleine-König &dev_attr_device_add.attr,
666bbecb07fSUwe Kleine-König &dev_attr_device_remove.attr,
667bbecb07fSUwe Kleine-König &dev_attr_poll_interval_ns.attr,
668bbecb07fSUwe Kleine-König NULL
669bbecb07fSUwe Kleine-König };
670bbecb07fSUwe Kleine-König ATTRIBUTE_GROUPS(siox_master);
671bbecb07fSUwe Kleine-König
siox_master_release(struct device * dev)672bbecb07fSUwe Kleine-König static void siox_master_release(struct device *dev)
673bbecb07fSUwe Kleine-König {
674bbecb07fSUwe Kleine-König struct siox_master *smaster = to_siox_master(dev);
675bbecb07fSUwe Kleine-König
676bbecb07fSUwe Kleine-König kfree(smaster);
677bbecb07fSUwe Kleine-König }
678bbecb07fSUwe Kleine-König
679bbecb07fSUwe Kleine-König static struct device_type siox_master_type = {
680bbecb07fSUwe Kleine-König .groups = siox_master_groups,
681bbecb07fSUwe Kleine-König .release = siox_master_release,
682bbecb07fSUwe Kleine-König };
683bbecb07fSUwe Kleine-König
siox_master_alloc(struct device * dev,size_t size)684bbecb07fSUwe Kleine-König struct siox_master *siox_master_alloc(struct device *dev,
685bbecb07fSUwe Kleine-König size_t size)
686bbecb07fSUwe Kleine-König {
687bbecb07fSUwe Kleine-König struct siox_master *smaster;
688bbecb07fSUwe Kleine-König
689bbecb07fSUwe Kleine-König if (!dev)
690bbecb07fSUwe Kleine-König return NULL;
691bbecb07fSUwe Kleine-König
692bbecb07fSUwe Kleine-König smaster = kzalloc(sizeof(*smaster) + size, GFP_KERNEL);
693bbecb07fSUwe Kleine-König if (!smaster)
694bbecb07fSUwe Kleine-König return NULL;
695bbecb07fSUwe Kleine-König
696bbecb07fSUwe Kleine-König device_initialize(&smaster->dev);
697bbecb07fSUwe Kleine-König
698bbecb07fSUwe Kleine-König smaster->busno = -1;
699bbecb07fSUwe Kleine-König smaster->dev.bus = &siox_bus_type;
700bbecb07fSUwe Kleine-König smaster->dev.type = &siox_master_type;
701bbecb07fSUwe Kleine-König smaster->dev.parent = dev;
702bbecb07fSUwe Kleine-König smaster->poll_interval = DIV_ROUND_UP(HZ, 40);
703bbecb07fSUwe Kleine-König
704bbecb07fSUwe Kleine-König dev_set_drvdata(&smaster->dev, &smaster[1]);
705bbecb07fSUwe Kleine-König
706bbecb07fSUwe Kleine-König return smaster;
707bbecb07fSUwe Kleine-König }
708bbecb07fSUwe Kleine-König EXPORT_SYMBOL_GPL(siox_master_alloc);
709bbecb07fSUwe Kleine-König
siox_master_register(struct siox_master * smaster)710bbecb07fSUwe Kleine-König int siox_master_register(struct siox_master *smaster)
711bbecb07fSUwe Kleine-König {
712bbecb07fSUwe Kleine-König int ret;
713bbecb07fSUwe Kleine-König
714bbecb07fSUwe Kleine-König if (!siox_is_registered)
715bbecb07fSUwe Kleine-König return -EPROBE_DEFER;
716bbecb07fSUwe Kleine-König
717bbecb07fSUwe Kleine-König if (!smaster->pushpull)
718bbecb07fSUwe Kleine-König return -EINVAL;
719bbecb07fSUwe Kleine-König
720bbecb07fSUwe Kleine-König dev_set_name(&smaster->dev, "siox-%d", smaster->busno);
721bbecb07fSUwe Kleine-König
722e8905914SUwe Kleine-König mutex_init(&smaster->lock);
723e8905914SUwe Kleine-König INIT_LIST_HEAD(&smaster->devices);
724e8905914SUwe Kleine-König
725bbecb07fSUwe Kleine-König smaster->last_poll = jiffies;
726e8905914SUwe Kleine-König smaster->poll_thread = kthread_run(siox_poll_thread, smaster,
727bbecb07fSUwe Kleine-König "siox-%d", smaster->busno);
728bbecb07fSUwe Kleine-König if (IS_ERR(smaster->poll_thread)) {
729bbecb07fSUwe Kleine-König smaster->active = 0;
730bbecb07fSUwe Kleine-König return PTR_ERR(smaster->poll_thread);
731bbecb07fSUwe Kleine-König }
732bbecb07fSUwe Kleine-König
733bbecb07fSUwe Kleine-König ret = device_add(&smaster->dev);
734bbecb07fSUwe Kleine-König if (ret)
735bbecb07fSUwe Kleine-König kthread_stop(smaster->poll_thread);
736bbecb07fSUwe Kleine-König
737bbecb07fSUwe Kleine-König return ret;
738bbecb07fSUwe Kleine-König }
739bbecb07fSUwe Kleine-König EXPORT_SYMBOL_GPL(siox_master_register);
740bbecb07fSUwe Kleine-König
siox_master_unregister(struct siox_master * smaster)741bbecb07fSUwe Kleine-König void siox_master_unregister(struct siox_master *smaster)
742bbecb07fSUwe Kleine-König {
743bbecb07fSUwe Kleine-König /* remove device */
744bbecb07fSUwe Kleine-König device_del(&smaster->dev);
745bbecb07fSUwe Kleine-König
746bbecb07fSUwe Kleine-König siox_master_lock(smaster);
747bbecb07fSUwe Kleine-König
748bbecb07fSUwe Kleine-König __siox_stop(smaster);
749bbecb07fSUwe Kleine-König
750bbecb07fSUwe Kleine-König while (smaster->num_devices) {
751bbecb07fSUwe Kleine-König struct siox_device *sdevice;
752bbecb07fSUwe Kleine-König
753bbecb07fSUwe Kleine-König sdevice = container_of(smaster->devices.prev,
754bbecb07fSUwe Kleine-König struct siox_device, node);
755bbecb07fSUwe Kleine-König list_del(&sdevice->node);
756bbecb07fSUwe Kleine-König smaster->num_devices--;
757bbecb07fSUwe Kleine-König
758bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
759bbecb07fSUwe Kleine-König
760bbecb07fSUwe Kleine-König device_unregister(&sdevice->dev);
761bbecb07fSUwe Kleine-König
762bbecb07fSUwe Kleine-König siox_master_lock(smaster);
763bbecb07fSUwe Kleine-König }
764bbecb07fSUwe Kleine-König
765bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
766bbecb07fSUwe Kleine-König
767bbecb07fSUwe Kleine-König put_device(&smaster->dev);
768bbecb07fSUwe Kleine-König }
769bbecb07fSUwe Kleine-König EXPORT_SYMBOL_GPL(siox_master_unregister);
770bbecb07fSUwe Kleine-König
siox_device_add(struct siox_master * smaster,const char * type,size_t inbytes,size_t outbytes,u8 statustype)771bbecb07fSUwe Kleine-König static struct siox_device *siox_device_add(struct siox_master *smaster,
772bbecb07fSUwe Kleine-König const char *type, size_t inbytes,
773bbecb07fSUwe Kleine-König size_t outbytes, u8 statustype)
774bbecb07fSUwe Kleine-König {
775bbecb07fSUwe Kleine-König struct siox_device *sdevice;
776bbecb07fSUwe Kleine-König int ret;
777bbecb07fSUwe Kleine-König size_t buf_len;
778bbecb07fSUwe Kleine-König
779bbecb07fSUwe Kleine-König sdevice = kzalloc(sizeof(*sdevice), GFP_KERNEL);
780bbecb07fSUwe Kleine-König if (!sdevice)
781bbecb07fSUwe Kleine-König return ERR_PTR(-ENOMEM);
782bbecb07fSUwe Kleine-König
783bbecb07fSUwe Kleine-König sdevice->type = type;
784bbecb07fSUwe Kleine-König sdevice->inbytes = inbytes;
785bbecb07fSUwe Kleine-König sdevice->outbytes = outbytes;
786bbecb07fSUwe Kleine-König sdevice->statustype = statustype;
787bbecb07fSUwe Kleine-König
788bbecb07fSUwe Kleine-König sdevice->smaster = smaster;
789bbecb07fSUwe Kleine-König sdevice->dev.parent = &smaster->dev;
790bbecb07fSUwe Kleine-König sdevice->dev.bus = &siox_bus_type;
791bbecb07fSUwe Kleine-König sdevice->dev.type = &siox_device_type;
792bbecb07fSUwe Kleine-König
793bbecb07fSUwe Kleine-König siox_master_lock(smaster);
794bbecb07fSUwe Kleine-König
795bbecb07fSUwe Kleine-König dev_set_name(&sdevice->dev, "siox-%d-%d",
796bbecb07fSUwe Kleine-König smaster->busno, smaster->num_devices);
797bbecb07fSUwe Kleine-König
798bbecb07fSUwe Kleine-König buf_len = smaster->setbuf_len + inbytes +
799bbecb07fSUwe Kleine-König smaster->getbuf_len + outbytes;
800bbecb07fSUwe Kleine-König if (smaster->buf_len < buf_len) {
801bbecb07fSUwe Kleine-König u8 *buf = krealloc(smaster->buf, buf_len, GFP_KERNEL);
802bbecb07fSUwe Kleine-König
803bbecb07fSUwe Kleine-König if (!buf) {
804bbecb07fSUwe Kleine-König dev_err(&smaster->dev,
805bbecb07fSUwe Kleine-König "failed to realloc buffer to %zu\n", buf_len);
806bbecb07fSUwe Kleine-König ret = -ENOMEM;
807bbecb07fSUwe Kleine-König goto err_buf_alloc;
808bbecb07fSUwe Kleine-König }
809bbecb07fSUwe Kleine-König
810bbecb07fSUwe Kleine-König smaster->buf_len = buf_len;
811bbecb07fSUwe Kleine-König smaster->buf = buf;
812bbecb07fSUwe Kleine-König }
813bbecb07fSUwe Kleine-König
814bbecb07fSUwe Kleine-König ret = device_register(&sdevice->dev);
815bbecb07fSUwe Kleine-König if (ret) {
816bbecb07fSUwe Kleine-König dev_err(&smaster->dev, "failed to register device: %d\n", ret);
817bbecb07fSUwe Kleine-König
818bbecb07fSUwe Kleine-König goto err_device_register;
819bbecb07fSUwe Kleine-König }
820bbecb07fSUwe Kleine-König
821bbecb07fSUwe Kleine-König smaster->num_devices++;
822bbecb07fSUwe Kleine-König list_add_tail(&sdevice->node, &smaster->devices);
823bbecb07fSUwe Kleine-König
824bbecb07fSUwe Kleine-König smaster->setbuf_len += sdevice->inbytes;
825bbecb07fSUwe Kleine-König smaster->getbuf_len += sdevice->outbytes;
826bbecb07fSUwe Kleine-König
827bbecb07fSUwe Kleine-König sdevice->status_errors_kn = sysfs_get_dirent(sdevice->dev.kobj.sd,
828bbecb07fSUwe Kleine-König "status_errors");
829bbecb07fSUwe Kleine-König sdevice->watchdog_kn = sysfs_get_dirent(sdevice->dev.kobj.sd,
830bbecb07fSUwe Kleine-König "watchdog");
831bbecb07fSUwe Kleine-König sdevice->watchdog_errors_kn = sysfs_get_dirent(sdevice->dev.kobj.sd,
832bbecb07fSUwe Kleine-König "watchdog_errors");
833bbecb07fSUwe Kleine-König sdevice->connected_kn = sysfs_get_dirent(sdevice->dev.kobj.sd,
834bbecb07fSUwe Kleine-König "connected");
835bbecb07fSUwe Kleine-König
836bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
837bbecb07fSUwe Kleine-König
838bbecb07fSUwe Kleine-König return sdevice;
839bbecb07fSUwe Kleine-König
840bbecb07fSUwe Kleine-König err_device_register:
841bbecb07fSUwe Kleine-König /* don't care to make the buffer smaller again */
842*6e63153dSYang Yingliang put_device(&sdevice->dev);
843*6e63153dSYang Yingliang sdevice = NULL;
844bbecb07fSUwe Kleine-König
845bbecb07fSUwe Kleine-König err_buf_alloc:
846bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
847bbecb07fSUwe Kleine-König
848bbecb07fSUwe Kleine-König kfree(sdevice);
849bbecb07fSUwe Kleine-König
850bbecb07fSUwe Kleine-König return ERR_PTR(ret);
851bbecb07fSUwe Kleine-König }
852bbecb07fSUwe Kleine-König
siox_device_remove(struct siox_master * smaster)853bbecb07fSUwe Kleine-König static void siox_device_remove(struct siox_master *smaster)
854bbecb07fSUwe Kleine-König {
855bbecb07fSUwe Kleine-König struct siox_device *sdevice;
856bbecb07fSUwe Kleine-König
857bbecb07fSUwe Kleine-König siox_master_lock(smaster);
858bbecb07fSUwe Kleine-König
859bbecb07fSUwe Kleine-König if (!smaster->num_devices) {
860bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
861bbecb07fSUwe Kleine-König return;
862bbecb07fSUwe Kleine-König }
863bbecb07fSUwe Kleine-König
864bbecb07fSUwe Kleine-König sdevice = container_of(smaster->devices.prev, struct siox_device, node);
865bbecb07fSUwe Kleine-König list_del(&sdevice->node);
866bbecb07fSUwe Kleine-König smaster->num_devices--;
867bbecb07fSUwe Kleine-König
868bbecb07fSUwe Kleine-König smaster->setbuf_len -= sdevice->inbytes;
869bbecb07fSUwe Kleine-König smaster->getbuf_len -= sdevice->outbytes;
870bbecb07fSUwe Kleine-König
871bbecb07fSUwe Kleine-König if (!smaster->num_devices)
872bbecb07fSUwe Kleine-König __siox_stop(smaster);
873bbecb07fSUwe Kleine-König
874bbecb07fSUwe Kleine-König siox_master_unlock(smaster);
875bbecb07fSUwe Kleine-König
876bbecb07fSUwe Kleine-König /*
877bbecb07fSUwe Kleine-König * This must be done without holding the master lock because we're
878bbecb07fSUwe Kleine-König * called from device_remove_store which also holds a sysfs mutex.
879bbecb07fSUwe Kleine-König * device_unregister tries to aquire the same lock.
880bbecb07fSUwe Kleine-König */
881bbecb07fSUwe Kleine-König device_unregister(&sdevice->dev);
882bbecb07fSUwe Kleine-König }
883bbecb07fSUwe Kleine-König
__siox_driver_register(struct siox_driver * sdriver,struct module * owner)884bbecb07fSUwe Kleine-König int __siox_driver_register(struct siox_driver *sdriver, struct module *owner)
885bbecb07fSUwe Kleine-König {
886bbecb07fSUwe Kleine-König int ret;
887bbecb07fSUwe Kleine-König
888bbecb07fSUwe Kleine-König if (unlikely(!siox_is_registered))
889bbecb07fSUwe Kleine-König return -EPROBE_DEFER;
890bbecb07fSUwe Kleine-König
89107d9a767SUwe Kleine-König if (!sdriver->probe ||
89207d9a767SUwe Kleine-König (!sdriver->set_data && !sdriver->get_data)) {
893bbecb07fSUwe Kleine-König pr_err("Driver %s doesn't provide needed callbacks\n",
894bbecb07fSUwe Kleine-König sdriver->driver.name);
895bbecb07fSUwe Kleine-König return -EINVAL;
896bbecb07fSUwe Kleine-König }
897bbecb07fSUwe Kleine-König
898bbecb07fSUwe Kleine-König sdriver->driver.owner = owner;
899bbecb07fSUwe Kleine-König sdriver->driver.bus = &siox_bus_type;
900bbecb07fSUwe Kleine-König
901bbecb07fSUwe Kleine-König ret = driver_register(&sdriver->driver);
902bbecb07fSUwe Kleine-König if (ret)
903bbecb07fSUwe Kleine-König pr_err("Failed to register siox driver %s (%d)\n",
904bbecb07fSUwe Kleine-König sdriver->driver.name, ret);
905bbecb07fSUwe Kleine-König
906bbecb07fSUwe Kleine-König return ret;
907bbecb07fSUwe Kleine-König }
908bbecb07fSUwe Kleine-König EXPORT_SYMBOL_GPL(__siox_driver_register);
909bbecb07fSUwe Kleine-König
siox_init(void)910bbecb07fSUwe Kleine-König static int __init siox_init(void)
911bbecb07fSUwe Kleine-König {
912bbecb07fSUwe Kleine-König int ret;
913bbecb07fSUwe Kleine-König
914bbecb07fSUwe Kleine-König ret = bus_register(&siox_bus_type);
915bbecb07fSUwe Kleine-König if (ret) {
916bbecb07fSUwe Kleine-König pr_err("Registration of SIOX bus type failed: %d\n", ret);
917bbecb07fSUwe Kleine-König return ret;
918bbecb07fSUwe Kleine-König }
919bbecb07fSUwe Kleine-König
920bbecb07fSUwe Kleine-König siox_is_registered = true;
921bbecb07fSUwe Kleine-König
922bbecb07fSUwe Kleine-König return 0;
923bbecb07fSUwe Kleine-König }
924bbecb07fSUwe Kleine-König subsys_initcall(siox_init);
925bbecb07fSUwe Kleine-König
siox_exit(void)926bbecb07fSUwe Kleine-König static void __exit siox_exit(void)
927bbecb07fSUwe Kleine-König {
928bbecb07fSUwe Kleine-König bus_unregister(&siox_bus_type);
929bbecb07fSUwe Kleine-König }
930bbecb07fSUwe Kleine-König module_exit(siox_exit);
931bbecb07fSUwe Kleine-König
932bbecb07fSUwe Kleine-König MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
933bbecb07fSUwe Kleine-König MODULE_DESCRIPTION("Eckelmann SIOX driver core");
934bbecb07fSUwe Kleine-König MODULE_LICENSE("GPL v2");
935