Revert "Remove support for xpp drivers."
[dahdi/linux.git] / drivers / dahdi / xpp / xbus-sysfs.c
1 /*
2  * Written by Oron Peled <oron@actcom.co.il>
3  * Copyright (C) 2004-2006, Xorcom
4  *
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #include <linux/version.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/proc_fs.h>
27 #ifdef  PROTOCOL_DEBUG
28 #include <linux/ctype.h>
29 #endif
30 #include <linux/workqueue.h>
31 #include <linux/device.h>
32 #include <linux/delay.h>        /* for msleep() to debug */
33 #include <linux/sched.h>
34 #include "xpd.h"
35 #include "xpp_dahdi.h"
36 #include "xbus-core.h"
37 #include "dahdi_debug.h"
38
39 static const char rcsid[] = "$Id$";
40
41 /* Command line parameters */
42 extern int debug;
43
44 /*--------- xpp driver attributes -*/
45 static ssize_t sync_show(struct device_driver *driver, char *buf)
46 {
47         DBG(SYNC, "\n");
48         return fill_sync_string(buf, PAGE_SIZE);
49 }
50
51 static ssize_t sync_store(struct device_driver *driver, const char *buf,
52                           size_t count)
53 {
54         /* DBG(SYNC, "%s\n", buf); */
55         return exec_sync_command(buf, count);
56 }
57
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
59 static struct driver_attribute xpp_attrs[] = {
60         __ATTR(sync, S_IRUGO | S_IWUSR, sync_show, sync_store),
61         __ATTR_NULL,
62 };
63 #else
64 static DRIVER_ATTR_RW(sync);
65 static struct attribute *xpp_attrs[] = {
66         &driver_attr_sync.attr,
67         NULL,
68 };
69 ATTRIBUTE_GROUPS(xpp);
70 #endif
71
72 /*--------- Sysfs Bus handling ----*/
73 static DEVICE_ATTR_READER(xbus_state_show, dev, buf)
74 {
75         xbus_t *xbus;
76         int ret;
77
78         xbus = dev_to_xbus(dev);
79         ret = XBUS_STATE(xbus);
80         ret = snprintf(buf, PAGE_SIZE, "%s (%d)\n", xbus_statename(ret), ret);
81         return ret;
82 }
83
84 static DEVICE_ATTR_WRITER(xbus_state_store, dev, buf, count)
85 {
86         xbus_t *xbus;
87
88         xbus = dev_to_xbus(dev);
89         XBUS_DBG(GENERAL, xbus, "%s\n", buf);
90         if (strncmp(buf, "stop", 4) == 0)
91                 xbus_deactivate(xbus);
92         else if (XBUS_IS(xbus, IDLE) && strncmp(buf, "start", 5) == 0)
93                 xbus_activate(xbus);
94         else {
95                 XBUS_NOTICE(xbus,
96                             "%s: Illegal action %s in state %s. Ignored.\n",
97                             __func__, buf, xbus_statename(XBUS_STATE(xbus)));
98                 return -EINVAL;
99         }
100         return count;
101 }
102
103 static DEVICE_ATTR_READER(status_show, dev, buf)
104 {
105         xbus_t *xbus;
106         int ret;
107
108         xbus = dev_to_xbus(dev);
109         ret =
110             snprintf(buf, PAGE_SIZE, "%s\n",
111                      (XBUS_FLAGS(xbus, CONNECTED)) ? "connected" : "missing");
112         return ret;
113 }
114
115 static DEVICE_ATTR_READER(timing_show, dev, buf)
116 {
117         xbus_t *xbus;
118         struct xpp_drift *driftinfo;
119         int len = 0;
120         struct timeval now;
121
122         do_gettimeofday(&now);
123         xbus = dev_to_xbus(dev);
124         driftinfo = &xbus->drift;
125         len +=
126             snprintf(buf + len, PAGE_SIZE - len, "%-3s",
127                      sync_mode_name(xbus->sync_mode));
128         if (xbus->sync_mode == SYNC_MODE_PLL) {
129                 len +=
130                     snprintf(buf + len, PAGE_SIZE - len,
131                              " %5d: lost (%4d,%4d) : ", xbus->ticker.cycle,
132                              driftinfo->lost_ticks, driftinfo->lost_tick_count);
133                 len +=
134                     snprintf(buf + len, PAGE_SIZE - len,
135                              "DRIFT %3d %ld sec ago", xbus->sync_adjustment,
136                              (xbus->pll_updated_at ==
137                               0) ? 0 : now.tv_sec - xbus->pll_updated_at);
138         }
139         len += snprintf(buf + len, PAGE_SIZE - len, "\n");
140         return len;
141 }
142
143 #ifdef  SAMPLE_TICKS
144 /*
145  * tick sampling: Measure offset from reference ticker:
146  *   - Recording start when writing to:
147  *       /sys/bus/astribanks/devices/xbus-??/samples
148  *   - Recording ends when filling SAMPLE_SIZE ticks
149  *   - Results are read from the same sysfs file.
150  *   - Trying to read/write during recording, returns -EBUSY.
151  */
152 static DEVICE_ATTR_READER(samples_show, dev, buf)
153 {
154         xbus_t *xbus;
155         int len = 0;
156         int i;
157
158         xbus = dev_to_xbus(dev);
159         if (xbus->sample_running)
160                 return -EBUSY;
161         for (i = 0; i < SAMPLE_SIZE; i++) {
162                 if (len > PAGE_SIZE - 20)
163                         break;
164                 len +=
165                     snprintf(buf + len, PAGE_SIZE - len, "%d\n",
166                              xbus->sample_ticks[i]);
167         }
168         return len;
169 }
170
171 static DEVICE_ATTR_WRITER(samples_store, dev, buf, count)
172 {
173         xbus_t *xbus;
174
175         xbus = dev_to_xbus(dev);
176         if (xbus->sample_running)
177                 return -EBUSY;
178         memset(xbus->sample_ticks, 0, sizeof(*xbus->sample_ticks));
179         xbus->sample_pos = 0;
180         xbus->sample_running = 1;
181         return count;
182 }
183 #endif
184
185 /*
186  * Clear statistics
187  */
188 static DEVICE_ATTR_WRITER(cls_store, dev, buf, count)
189 {
190         xbus_t *xbus;
191         struct xpp_drift *driftinfo;
192
193         xbus = dev_to_xbus(dev);
194         driftinfo = &xbus->drift;
195         driftinfo->lost_ticks = 0;
196         driftinfo->lost_tick_count = 0;
197         xbus->min_tx_sync = INT_MAX;
198         xbus->max_tx_sync = 0;
199         xbus->min_rx_sync = INT_MAX;
200         xbus->max_rx_sync = 0;
201 #ifdef  SAMPLE_TICKS
202         memset(xbus->sample_ticks, 0, sizeof(*xbus->sample_ticks));
203 #endif
204         return count;
205 }
206
207 static DEVICE_ATTR_READER(waitfor_xpds_show, dev, buf)
208 {
209         xbus_t *xbus;
210         int len;
211
212         xbus = dev_to_xbus(dev);
213         len = waitfor_xpds(xbus, buf);
214         return len;
215 }
216
217 static DEVICE_ATTR_READER(refcount_xbus_show, dev, buf)
218 {
219         xbus_t *xbus;
220         int len;
221
222         xbus = dev_to_xbus(dev);
223         len = sprintf(buf, "%d\n", refcount_xbus(xbus));
224         return len;
225 }
226
227 static DEVICE_ATTR_READER(driftinfo_show, dev, buf)
228 {
229         xbus_t *xbus;
230         struct xpp_drift *di;
231         struct xpp_ticker *ticker;
232         struct timeval now;
233         int len = 0;
234         int hours;
235         int minutes;
236         int seconds;
237         int speed_range;
238         int uframes_inaccuracy;
239         int i;
240
241         xbus = dev_to_xbus(dev);
242         di = &xbus->drift;
243         ticker = &xbus->ticker;
244         /*
245          * Calculate lost ticks time
246          */
247         do_gettimeofday(&now);
248         seconds = now.tv_sec - di->last_lost_tick.tv.tv_sec;
249         minutes = seconds / 60;
250         seconds = seconds % 60;
251         hours = minutes / 60;
252         minutes = minutes % 60;
253         len += snprintf(buf + len, PAGE_SIZE - len,
254                 "%-15s: %8d (was %d:%02d:%02d ago)\n", "lost_ticks",
255                 di->lost_ticks, hours, minutes, seconds);
256         speed_range = abs(di->max_speed - di->min_speed);
257         uframes_inaccuracy = di->sync_inaccuracy / 125;
258         len += snprintf(buf + len, PAGE_SIZE - len,
259                 "%-15s: %8d ", "instability",
260                 speed_range + uframes_inaccuracy);
261         if (xbus->sync_mode == SYNC_MODE_AB) {
262                 buf[len++] = '-';
263         } else {
264                 for (i = 0;
265                      len < PAGE_SIZE - 1
266                      && i < speed_range + uframes_inaccuracy; i++)
267                         buf[len++] = '#';
268         }
269         buf[len++] = '\n';
270         len += snprintf(buf + len, PAGE_SIZE - len, "%-15s: %8d (uframes)\n",
271                 "inaccuracy", uframes_inaccuracy);
272         len += snprintf(buf + len, PAGE_SIZE - len, "%-15s: %8d\n",
273                 "speed_range", speed_range);
274 #define SHOW(ptr, item) \
275         do { \
276                 len += snprintf(buf + len, PAGE_SIZE - len, \
277                 "%-15s: %8d\n", #item, (ptr)->item); \
278         } while (0)
279         SHOW(xbus, sync_adjustment);
280         len += snprintf(buf + len, PAGE_SIZE - len, "%-15s: %8d\n",
281                 "offset (usec)", di->offset_prev);
282         SHOW(di, offset_range);
283         len += snprintf(buf + len, PAGE_SIZE - len, "%-15s: %8d\n",
284                 "best_speed", (di->max_speed + di->min_speed) / 2);
285         SHOW(di, min_speed);
286         SHOW(di, max_speed);
287         SHOW(ticker, cycle);
288         SHOW(ticker, tick_period);
289         SHOW(ticker, count);
290 #undef  SHOW
291         return len;
292 }
293
294 #define xbus_attr(field, format_string)                                    \
295 static ssize_t                                                             \
296 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
297 {                                                                          \
298         xbus_t  *xbus;                                                     \
299         xbus = dev_to_xbus(dev);                                           \
300         return sprintf(buf, format_string, xbus->field);                   \
301 }
302
303 xbus_attr(connector, "%s\n");
304 xbus_attr(label, "%s\n");
305
306 static DEVICE_ATTR_WRITER(dahdi_registration_store, dev, buf, count)
307 {
308         xbus_t *xbus;
309         int dahdi_reg;
310         int ret;
311
312         xbus = dev_to_xbus(dev);
313         if (!xbus)
314                 return -ENODEV;
315         ret = sscanf(buf, "%d", &dahdi_reg);
316         if (ret != 1)
317                 return -EINVAL;
318         if (dahdi_reg) {
319                 ret = xbus_register_dahdi_device(xbus);
320                 if (ret < 0) {
321                         XBUS_ERR(xbus,
322                                 "xbus_register_dahdi_device() failed (ret = %d)\n",
323                                 ret);
324                         return ret;
325                 }
326         } else {
327                 xbus_unregister_dahdi_device(xbus);
328         }
329         return count;
330 }
331
332 static DEVICE_ATTR_READER(dahdi_registration_show, dev, buf)
333 {
334         xbus_t *xbus;
335         int len;
336
337         xbus = dev_to_xbus(dev);
338         len = sprintf(buf, "%d\n", xbus_is_registered(xbus));
339         return len;
340 }
341
342 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
343 static struct device_attribute xbus_dev_attrs[] = {
344         __ATTR_RO(connector),
345         __ATTR_RO(label),
346         __ATTR_RO(status),
347         __ATTR_RO(timing),
348         __ATTR_RO(refcount_xbus),
349         __ATTR_RO(waitfor_xpds),
350         __ATTR_RO(driftinfo),
351         __ATTR(cls, S_IWUSR, NULL, cls_store),
352         __ATTR(xbus_state, S_IRUGO | S_IWUSR, xbus_state_show,
353                xbus_state_store),
354 #ifdef  SAMPLE_TICKS
355         __ATTR(samples, S_IWUSR | S_IRUGO, samples_show, samples_store),
356 #endif
357         __ATTR(dahdi_registration, S_IRUGO | S_IWUSR,
358                 dahdi_registration_show,
359                 dahdi_registration_store),
360         __ATTR_NULL,
361 };
362 #else
363 static DEVICE_ATTR_RO(connector);
364 static DEVICE_ATTR_RO(label);
365 static DEVICE_ATTR_RO(status);
366 static DEVICE_ATTR_RO(timing);
367 static DEVICE_ATTR_RO(refcount_xbus);
368 static DEVICE_ATTR_RO(waitfor_xpds);
369 static DEVICE_ATTR_RO(driftinfo);
370 static DEVICE_ATTR_WO(cls);
371 static DEVICE_ATTR_RW(xbus_state);
372 #ifdef  SAMPLE_TICKS
373 static DEVICE_ATTR_RO(samples);
374 #endif
375 static DEVICE_ATTR_RW(dahdi_registration);
376
377 static struct attribute *xbus_dev_attrs[] = {
378    &dev_attr_connector.attr,
379    &dev_attr_label.attr,
380    &dev_attr_status.attr,
381    &dev_attr_timing.attr,
382    &dev_attr_refcount_xbus.attr,
383    &dev_attr_waitfor_xpds.attr,
384    &dev_attr_driftinfo.attr,
385    &dev_attr_cls.attr,
386    &dev_attr_xbus_state.attr,
387 #ifdef  SAMPLE_TICKS
388    &dev_attr_samples.attr,
389 #endif
390    &dev_attr_dahdi_registration.attr,
391    NULL,
392 };
393 ATTRIBUTE_GROUPS(xbus_dev);
394 #endif
395
396 static int astribank_match(struct device *dev, struct device_driver *driver)
397 {
398         DBG(DEVICES, "SYSFS MATCH: dev->bus_id = %s, driver->name = %s\n",
399             dev_name(dev), driver->name);
400         return 1;
401 }
402
403 #define XBUS_VAR_BLOCK  \
404         do {            \
405                 XBUS_ADD_UEVENT_VAR("XPP_INIT_DIR=%s", initdir);        \
406                 XBUS_ADD_UEVENT_VAR("XBUS_NUM=%02d", xbus->num);        \
407                 XBUS_ADD_UEVENT_VAR("XBUS_NAME=%s", xbus->busname);     \
408         } while (0)
409
410 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
411 #define XBUS_ADD_UEVENT_VAR(fmt, val...)                        \
412         do {                                                    \
413                 int err = add_uevent_var(envp, num_envp, &i,    \
414                                 buffer, buffer_size, &len,      \
415                                 fmt, val);                      \
416                 if (err)                                        \
417                         return err;                             \
418         } while (0)
419
420 static int astribank_uevent(struct device *dev, char **envp, int num_envp,
421                             char *buffer, int buffer_size)
422 {
423         xbus_t *xbus;
424         int i = 0;
425         int len = 0;
426         extern char *initdir;
427
428         if (!dev)
429                 return -ENODEV;
430         xbus = dev_to_xbus(dev);
431         DBG(GENERAL, "SYFS bus_id=%s xbus=%s\n", dev_name(dev), xbus->busname);
432         XBUS_VAR_BLOCK;
433         envp[i] = NULL;
434         return 0;
435 }
436
437 #else
438 #define XBUS_ADD_UEVENT_VAR(fmt, val...)                        \
439         do {                                                    \
440                 int err = add_uevent_var(kenv, fmt, val);       \
441                 if (err)                                        \
442                         return err;                             \
443         } while (0)
444
445 static int astribank_uevent(struct device *dev, struct kobj_uevent_env *kenv)
446 {
447         xbus_t *xbus;
448         extern char *initdir;
449
450         if (!dev)
451                 return -ENODEV;
452         xbus = dev_to_xbus(dev);
453         DBG(GENERAL, "SYFS bus_id=%s xbus=%s\n", dev_name(dev), xbus->busname);
454         XBUS_VAR_BLOCK;
455         return 0;
456 }
457
458 #endif
459
460 void astribank_uevent_send(xbus_t *xbus, enum kobject_action act)
461 {
462         struct kobject *kobj;
463
464         kobj = &xbus->astribank.kobj;
465         XBUS_DBG(DEVICES, xbus, "SYFS bus_id=%s action=%d\n",
466                  dev_name(&xbus->astribank), act);
467         kobject_uevent(kobj, act);
468 }
469
470 static void astribank_release(struct device *dev)
471 {
472         xbus_t *xbus;
473
474         BUG_ON(!dev);
475         xbus = dev_to_xbus(dev);
476         if (XBUS_FLAGS(xbus, CONNECTED)) {
477                 XBUS_ERR(xbus, "Try to release CONNECTED device.\n");
478                 BUG();
479         }
480         if (!XBUS_IS(xbus, IDLE) && !XBUS_IS(xbus, FAIL)
481             && !XBUS_IS(xbus, DEACTIVATED)) {
482                 XBUS_ERR(xbus, "Try to release in state %s\n",
483                          xbus_statename(XBUS_STATE(xbus)));
484                 BUG();
485         }
486         XBUS_INFO(xbus, "[%s] Astribank Release\n", xbus->label);
487         xbus_free(xbus);
488 }
489
490 static struct bus_type toplevel_bus_type = {
491         .name = "astribanks",
492         .match = astribank_match,
493         .uevent = astribank_uevent,
494 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
495         .dev_attrs = xbus_dev_attrs,
496 #else
497         .dev_groups = xbus_dev_groups,
498 #endif
499 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
500         .drv_attrs = xpp_attrs,
501 #else
502         .drv_groups = xpp_groups,
503 #endif
504 };
505
506 static int astribank_probe(struct device *dev)
507 {
508         xbus_t *xbus;
509
510         xbus = dev_to_xbus(dev);
511         XBUS_DBG(DEVICES, xbus, "SYSFS\n");
512         return 0;
513 }
514
515 static int astribank_remove(struct device *dev)
516 {
517         xbus_t *xbus;
518
519         xbus = dev_to_xbus(dev);
520         XBUS_INFO(xbus, "[%s] Atribank Remove\n", xbus->label);
521         return 0;
522 }
523
524 static struct device_driver xpp_driver = {
525         .name = "xppdrv",
526         .bus = &toplevel_bus_type,
527         .probe = astribank_probe,
528         .remove = astribank_remove,
529         .owner = THIS_MODULE
530 };
531
532 /*--------- Sysfs XPD handling ----*/
533
534 static DEVICE_ATTR_READER(chipregs_show, dev, buf)
535 {
536         xpd_t *xpd;
537         unsigned long flags;
538         reg_cmd_t *regs;
539         bool do_datah;
540         char datah_str[50];
541         int len = 0;
542
543         BUG_ON(!dev);
544         xpd = dev_to_xpd(dev);
545         if (!xpd)
546                 return -ENODEV;
547         spin_lock_irqsave(&xpd->lock, flags);
548         regs = &xpd->last_reply;
549         len += sprintf(buf + len,
550                 "# Writing bad data to this file may damage your hardware!\n");
551         len += sprintf(buf + len, "# Consult firmware docs first\n");
552         len += sprintf(buf + len, "#\n");
553         do_datah = REG_FIELD(regs, do_datah) ? 1 : 0;
554         if (do_datah) {
555                 snprintf(datah_str, ARRAY_SIZE(datah_str), "\t%02X",
556                          REG_FIELD(regs, data_high));
557         } else
558                 datah_str[0] = '\0';
559         if (regs->h.bytes ==  REG_CMD_SIZE(RAM)) {
560                 len +=
561                     sprintf(buf + len, "#CH\tOP\tAL\tAH\tD0\tD1\tD2\tD3\n");
562                 len +=
563                     sprintf(buf + len, "%2d\tRR\t%02X\t%02X\t%02X\t%02X\t%02X\t%02X\n",
564                             regs->h.portnum,
565                             REG_FIELD_RAM(regs, addr_low),
566                             REG_FIELD_RAM(regs, addr_high),
567                             REG_FIELD_RAM(regs, data_0),
568                             REG_FIELD_RAM(regs, data_1),
569                             REG_FIELD_RAM(regs, data_2),
570                             REG_FIELD_RAM(regs, data_3));
571         } else if (REG_FIELD(regs, do_subreg)) {
572                 len +=
573                     sprintf(buf + len, "#CH\tOP\tReg.\tSub\tDL%s\n",
574                             (do_datah) ? "\tDH" : "");
575                 len +=
576                     sprintf(buf + len, "%2d\tRS\t%02X\t%02X\t%02X%s\n",
577                             regs->h.portnum, REG_FIELD(regs, regnum),
578                             REG_FIELD(regs, subreg), REG_FIELD(regs, data_low),
579                             datah_str);
580         } else {
581                 len +=
582                     sprintf(buf + len, "#CH\tOP\tReg.\tDL%s\n",
583                             (do_datah) ? "\tDH" : "");
584                 len +=
585                     sprintf(buf + len, "%2d\tRD\t%02X\t%02X%s\n", regs->h.portnum,
586                             REG_FIELD(regs, regnum), REG_FIELD(regs, data_low),
587                             datah_str);
588         }
589         spin_unlock_irqrestore(&xpd->lock, flags);
590         return len;
591 }
592
593 static DEVICE_ATTR_WRITER(chipregs_store, dev, buf, count)
594 {
595         xpd_t *xpd;
596         const char *p;
597         char tmp[MAX_PROC_WRITE];
598         int i;
599         int ret;
600
601         BUG_ON(!dev);
602         xpd = dev_to_xpd(dev);
603         //XPD_DBG(GENERAL, xpd, "%s\n", buf);
604         if (!xpd)
605                 return -ENODEV;
606         p = buf;
607         while ((p - buf) < count) {
608                 i = strcspn(p, "\r\n");
609                 if (i > 0) {
610                         if (i >= MAX_PROC_WRITE) {
611                                 XPD_NOTICE(xpd, "Command too long (%d chars)\n",
612                                            i);
613                                 return -E2BIG;
614                         }
615                         memcpy(tmp, p, i);
616                         tmp[i] = '\0';
617                         ret = parse_chip_command(xpd, tmp);
618                         if (ret < 0) {
619                                 XPD_NOTICE(xpd,
620                                            "Failed writing command: '%s'\n",
621                                            tmp);
622                                 return ret;
623                         }
624                 }
625                 p += i + 1;
626                 /* Don't flood command_queue */
627                 if (xframe_queue_count(&xpd->xbus->command_queue) > 5)
628                         msleep(6);
629         }
630         return count;
631 }
632
633 static DEVICE_ATTR_READER(blink_show, dev, buf)
634 {
635         xpd_t *xpd;
636         unsigned long flags;
637         int len = 0;
638
639         BUG_ON(!dev);
640         xpd = dev_to_xpd(dev);
641         if (!xpd)
642                 return -ENODEV;
643         spin_lock_irqsave(&xpd->lock, flags);
644         len += sprintf(buf, "0x%lX\n", xpd->blink_mode);
645         spin_unlock_irqrestore(&xpd->lock, flags);
646         return len;
647 }
648
649 static DEVICE_ATTR_WRITER(blink_store, dev, buf, count)
650 {
651         xpd_t *xpd;
652         char *endp;
653         unsigned long blink;
654
655         BUG_ON(!dev);
656         xpd = dev_to_xpd(dev);
657         //XPD_DBG(GENERAL, xpd, "%s\n", buf);
658         if (!xpd)
659                 return -ENODEV;
660         blink = simple_strtoul(buf, &endp, 0);
661         if (*endp != '\0' && *endp != '\n' && *endp != '\r')
662                 return -EINVAL;
663         if (blink > 0xFFFF)
664                 return -EINVAL;
665         XPD_DBG(GENERAL, xpd, "BLINK channels: 0x%lX\n", blink);
666         xpd->blink_mode = blink;
667         return count;
668 }
669
670 static DEVICE_ATTR_READER(span_show, dev, buf)
671 {
672         xpd_t *xpd;
673         unsigned long flags;
674         int len = 0;
675
676         BUG_ON(!dev);
677         xpd = dev_to_xpd(dev);
678         if (!xpd)
679                 return -ENODEV;
680         spin_lock_irqsave(&xpd->lock, flags);
681         len +=
682             sprintf(buf, "%d\n",
683                     SPAN_REGISTERED(xpd) ? PHONEDEV(xpd).span.spanno : 0);
684         spin_unlock_irqrestore(&xpd->lock, flags);
685         return len;
686 }
687
688 /*
689  * For backward compatibility with old dahdi-tools
690  * Remove after dahdi_registration is upgraded
691  */
692 static DEVICE_ATTR_WRITER(span_store, dev, buf, count)
693 {
694         xpd_t *xpd;
695         int dahdi_reg;
696         int ret;
697
698         BUG_ON(!dev);
699         xpd = dev_to_xpd(dev);
700         if (!xpd)
701                 return -ENODEV;
702         ret = sscanf(buf, "%d", &dahdi_reg);
703         if (ret != 1)
704                 return -EINVAL;
705         if (!XBUS_IS(xpd->xbus, READY))
706                 return -ENODEV;
707         XPD_DBG(DEVICES, xpd, "%s -- deprecated (should use assigned-spans)\n",
708                 (dahdi_reg) ? "register" : "unregister");
709         if (dahdi_reg)
710                 xbus_register_dahdi_device(xpd->xbus);
711           else
712                 xbus_unregister_dahdi_device(xpd->xbus);
713         return count;
714 }
715
716 static DEVICE_ATTR_READER(type_show, dev, buf)
717 {
718         xpd_t *xpd;
719         int len = 0;
720
721         BUG_ON(!dev);
722         xpd = dev_to_xpd(dev);
723         if (!xpd)
724                 return -ENODEV;
725         len += sprintf(buf, "%s\n", xpd->type_name);
726         return len;
727 }
728
729 static DEVICE_ATTR_READER(hwid_show, dev, buf)
730 {
731         xpd_t *xpd;
732         int len = 0;
733
734         BUG_ON(!dev);
735         xpd = dev_to_xpd(dev);
736         if (!xpd)
737                 return -ENODEV;
738         len += sprintf(buf, "%d.%d\n", XPD_HW(xpd).type, XPD_HW(xpd).subtype);
739         return len;
740 }
741
742 static DEVICE_ATTR_READER(offhook_show, dev, buf)
743 {
744         xpd_t *xpd;
745         int len = 0;
746         int i;
747
748         BUG_ON(!dev);
749         xpd = dev_to_xpd(dev);
750         if (!xpd)
751                 return -ENODEV;
752         for_each_line(xpd, i) {
753                 len += sprintf(buf + len, "%d ", IS_OFFHOOK(xpd, i));
754         }
755         if (len)
756                 len--;          /* backout last space */
757         len += sprintf(buf + len, "\n");
758         return len;
759 }
760
761 static DEVICE_ATTR_READER(timing_priority_show, dev, buf)
762 {
763         xpd_t *xpd;
764         unsigned long flags;
765         int len = 0;
766
767         BUG_ON(!dev);
768         xpd = dev_to_xpd(dev);
769         if (!xpd)
770                 return -ENODEV;
771         spin_lock_irqsave(&xpd->lock, flags);
772         len += sprintf(buf + len, "%d\n", PHONEDEV(xpd).timing_priority);
773         spin_unlock_irqrestore(&xpd->lock, flags);
774         return len;
775 }
776
777 static DEVICE_ATTR_READER(refcount_xpd_show, dev, buf)
778 {
779         xpd_t *xpd;
780         int len = 0;
781
782         BUG_ON(!dev);
783         xpd = dev_to_xpd(dev);
784         if (!xpd)
785                 return -ENODEV;
786         len += sprintf(buf + len, "%d\n", refcount_xpd(xpd));
787         return len;
788 }
789
790 static int xpd_match(struct device *dev, struct device_driver *driver)
791 {
792         struct xpd_driver *xpd_driver;
793         xpd_t *xpd;
794
795         xpd_driver = driver_to_xpd_driver(driver);
796         xpd = dev_to_xpd(dev);
797         if (xpd_driver->xpd_type != xpd->xpd_type) {
798                 XPD_DBG(DEVICES, xpd,
799                         "SYSFS match fail: xpd->xpd_type = %d, "
800                         "xpd_driver->xpd_type = %d\n",
801                         xpd->xpd_type, xpd_driver->xpd_type);
802                 return 0;
803         }
804         XPD_DBG(DEVICES, xpd,
805                 "SYSFS MATCH: xpd_type=%d dev->bus_id = %s, driver->name = %s\n",
806                 xpd->xpd_type, dev_name(dev), driver->name);
807         return 1;
808 }
809
810 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
811 static struct device_attribute xpd_dev_attrs[] = {
812         __ATTR(chipregs, S_IRUGO | S_IWUSR, chipregs_show, chipregs_store),
813         __ATTR(blink, S_IRUGO | S_IWUSR, blink_show, blink_store),
814         __ATTR(span, S_IRUGO | S_IWUSR, span_show, span_store),
815         __ATTR_RO(type),
816         __ATTR_RO(hwid),
817         __ATTR_RO(offhook),
818         __ATTR_RO(timing_priority),
819         __ATTR_RO(refcount_xpd),
820         __ATTR_NULL,
821 };
822 #else
823 static DEVICE_ATTR_RW(chipregs);
824 static DEVICE_ATTR_RW(blink);
825 static DEVICE_ATTR_RW(span);
826 static DEVICE_ATTR_RO(type);
827 static DEVICE_ATTR_RO(hwid);
828 static DEVICE_ATTR_RO(offhook);
829 static DEVICE_ATTR_RO(timing_priority);
830 static DEVICE_ATTR_RO(refcount_xpd);
831
832 static struct attribute *xpd_dev_attrs[] = {
833    &dev_attr_chipregs.attr,
834    &dev_attr_blink.attr,
835    &dev_attr_span.attr,
836    &dev_attr_type.attr,
837    &dev_attr_hwid.attr,
838    &dev_attr_offhook.attr,
839    &dev_attr_timing_priority.attr,
840    &dev_attr_refcount_xpd.attr,
841    NULL,
842 };
843 ATTRIBUTE_GROUPS(xpd_dev);
844 #endif
845
846 static struct bus_type xpd_type = {
847         .name = "xpds",
848         .match = xpd_match,
849 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
850         .dev_attrs = xpd_dev_attrs,
851 #else
852         .dev_groups = xpd_dev_groups,
853 #endif
854 };
855
856 int xpd_driver_register(struct device_driver *driver)
857 {
858         int ret;
859
860         DBG(DEVICES, "%s\n", driver->name);
861         driver->bus = &xpd_type;
862         if ((ret = driver_register(driver)) < 0) {
863                 ERR("%s: driver_register(%s) failed. Error number %d", __func__,
864                     driver->name, ret);
865         }
866         return ret;
867 }
868 EXPORT_SYMBOL(xpd_driver_register);
869
870 void xpd_driver_unregister(struct device_driver *driver)
871 {
872         DBG(DEVICES, "%s\n", driver->name);
873         driver_unregister(driver);
874 }
875 EXPORT_SYMBOL(xpd_driver_unregister);
876
877 static void xpd_release(struct device *dev)
878 {
879         xpd_t *xpd;
880
881         BUG_ON(!dev);
882         xpd = dev_to_xpd(dev);
883         XPD_DBG(DEVICES, xpd, "SYSFS\n");
884         xpd_remove(xpd);
885 }
886
887 int xpd_device_register(xbus_t *xbus, xpd_t *xpd)
888 {
889         struct device *dev = &xpd->xpd_dev;
890         int ret;
891
892         XPD_DBG(DEVICES, xpd, "SYSFS\n");
893         dev->bus = &xpd_type;
894         dev->parent = &xbus->astribank;
895         dev_set_name(dev, "%02d:%1x:%1x", xbus->num, xpd->addr.unit,
896                      xpd->addr.subunit);
897         dev_set_drvdata(dev, xpd);
898         dev->release = xpd_release;
899         ret = device_register(dev);
900         if (ret) {
901                 XPD_ERR(xpd, "%s: device_register failed: %d\n", __func__, ret);
902                 return ret;
903         }
904         return 0;
905 }
906
907 void xpd_device_unregister(xpd_t *xpd)
908 {
909         xbus_t *xbus;
910         struct device *dev;
911
912         xbus = xpd->xbus;
913         BUG_ON(!xbus);
914         XPD_DBG(DEVICES, xpd, "SYSFS\n");
915         dev = &xpd->xpd_dev;
916         if (!dev_get_drvdata(dev))
917                 return;
918         BUG_ON(dev_get_drvdata(dev) != xpd);
919         device_unregister(dev);
920         dev_set_drvdata(dev, NULL);
921 }
922
923 static DEVICE_ATTR_READER(echocancel_show, dev, buf)
924 {
925         xpd_t *xpd;
926         unsigned long flags;
927         int len = 0;
928         xpp_line_t ec_mask = 0;
929         int i;
930         int ret;
931
932         BUG_ON(!dev);
933         xpd = dev_to_xpd(dev);
934         if (!xpd)
935                 return -ENODEV;
936         if (!ECHOOPS(xpd->xbus))
937                 return -ENODEV;
938         spin_lock_irqsave(&xpd->lock, flags);
939         for (i = 0; i < PHONEDEV(xpd).channels; i++) {
940                 ret = CALL_EC_METHOD(ec_get, xpd->xbus, xpd, i);
941                 if (ret < 0) {
942                         LINE_ERR(xpd, i, "ec_get failed\n");
943                         len = -ENODEV;
944                         goto out;
945                 }
946                 if (ret)
947                         ec_mask |= (1 << i);
948         }
949         len += sprintf(buf, "0x%08X\n", ec_mask);
950 out:
951         spin_unlock_irqrestore(&xpd->lock, flags);
952         return len;
953 }
954
955 static DEVICE_ATTR_WRITER(echocancel_store, dev, buf, count)
956 {
957         xpd_t *xpd;
958         char *endp;
959         unsigned long mask;
960         int channels;
961         int ret;
962
963         BUG_ON(!dev);
964         xpd = dev_to_xpd(dev);
965         XPD_DBG(GENERAL, xpd, "%s\n", buf);
966         if (!xpd)
967                 return -ENODEV;
968         if (!ECHOOPS(xpd->xbus)) {
969                 XPD_ERR(xpd, "No echo canceller in this XBUS\n");
970                 return -ENODEV;
971         }
972         if (!IS_PHONEDEV(xpd)) {
973                 XPD_ERR(xpd, "Not a phone device\n");
974                 return -ENODEV;
975         }
976         channels = PHONEDEV(xpd).channels;
977         mask = simple_strtoul(buf, &endp, 0);
978         if (*endp != '\0' && *endp != '\n' && *endp != '\r') {
979                 XPD_ERR(xpd, "Too many channels: %d\n", channels);
980                 return -EINVAL;
981         }
982         if (mask != 0 && __ffs(mask) > channels) {
983                 XPD_ERR(xpd,
984                         "Channel mask (0x%lX) larger than "
985                         "available channels (%d)\n",
986                         mask, channels);
987                 return -EINVAL;
988         }
989         XPD_DBG(GENERAL, xpd, "ECHOCANCEL channels: 0x%lX\n", mask);
990         ret = CALL_PHONE_METHOD(echocancel_setmask, xpd, mask);
991         if (ret < 0) {
992                 XPD_ERR(xpd, "echocancel_setmask failed\n");
993                 return ret;
994         }
995         return count;
996 }
997
998 static DEVICE_ATTR(echocancel, S_IRUGO | S_IWUSR, echocancel_show,
999                    echocancel_store);
1000
1001 int echocancel_xpd(xpd_t *xpd, int on)
1002 {
1003         int ret;
1004
1005         XPD_DBG(GENERAL, xpd, "echocancel_xpd(%s)\n", (on) ? "on" : "off");
1006         if (!on) {
1007                 device_remove_file(xpd->echocancel, &dev_attr_echocancel);
1008                 return 0;
1009         }
1010
1011         ret = device_create_file(&xpd->xpd_dev, &dev_attr_echocancel);
1012         if (ret)
1013                 XPD_ERR(xpd, "%s: device_create_file(echocancel) failed: %d\n",
1014                         __func__, ret);
1015
1016         return ret;
1017 }
1018 EXPORT_SYMBOL(echocancel_xpd);
1019
1020 /*--------- Sysfs Device handling ----*/
1021
1022 void xbus_sysfs_transport_remove(xbus_t *xbus)
1023 {
1024         struct device *astribank;
1025
1026         BUG_ON(!xbus);
1027         XBUS_DBG(DEVICES, xbus, "\n");
1028         astribank = &xbus->astribank;
1029         sysfs_remove_link(&astribank->kobj, "transport");
1030 }
1031
1032 int xbus_sysfs_transport_create(xbus_t *xbus)
1033 {
1034         struct device *astribank;
1035         struct device *transport_device;
1036         int ret = 0;
1037
1038         BUG_ON(!xbus);
1039         XBUS_DBG(DEVICES, xbus, "\n");
1040         astribank = &xbus->astribank;
1041         BUG_ON(!astribank);
1042         transport_device = xbus->transport.transport_device;
1043         if (!transport_device) {
1044                 XBUS_ERR(xbus, "%s: Missing transport_device\n", __func__);
1045                 return -ENODEV;
1046         }
1047         ret =
1048             sysfs_create_link(&astribank->kobj, &transport_device->kobj,
1049                               "transport");
1050         if (ret < 0) {
1051                 XBUS_ERR(xbus, "%s: sysfs_create_link failed: %d\n", __func__,
1052                          ret);
1053                 dev_set_drvdata(astribank, NULL);
1054         }
1055         return ret;
1056 }
1057
1058 void xbus_sysfs_remove(xbus_t *xbus)
1059 {
1060         struct device *astribank;
1061
1062         BUG_ON(!xbus);
1063         astribank = &xbus->astribank;
1064         if (!dev_get_drvdata(astribank)) {
1065                 XBUS_NOTICE(xbus, "%s: already removed\n", __func__);
1066                 return;
1067         }
1068         XBUS_DBG(DEVICES, xbus, "going to unregister: refcount=%d\n",
1069                 refcount_read(&astribank->kobj.kref.refcount));
1070         BUG_ON(dev_get_drvdata(astribank) != xbus);
1071         device_unregister(astribank);
1072         dev_set_drvdata(astribank, NULL);
1073 }
1074
1075 int xbus_sysfs_create(xbus_t *xbus)
1076 {
1077         struct device *astribank;
1078         int ret = 0;
1079
1080         BUG_ON(!xbus);
1081         astribank = &xbus->astribank;
1082         XBUS_DBG(DEVICES, xbus, "\n");
1083         astribank->bus = &toplevel_bus_type;
1084         astribank->parent = xbus->transport.transport_device;
1085         dev_set_name(astribank, "xbus-%02d", xbus->num);
1086         dev_set_drvdata(astribank, xbus);
1087         astribank->release = astribank_release;
1088         ret = device_register(astribank);
1089         if (ret) {
1090                 XBUS_ERR(xbus, "%s: device_register failed: %d\n", __func__,
1091                          ret);
1092                 dev_set_drvdata(astribank, NULL);
1093         }
1094         return ret;
1095 }
1096
1097 int __init xpp_driver_init(void)
1098 {
1099         int ret;
1100
1101         DBG(DEVICES, "SYSFS\n");
1102         if ((ret = bus_register(&toplevel_bus_type)) < 0) {
1103                 ERR("%s: bus_register(%s) failed. Error number %d", __func__,
1104                     toplevel_bus_type.name, ret);
1105                 goto failed_toplevel;
1106         }
1107         if ((ret = driver_register(&xpp_driver)) < 0) {
1108                 ERR("%s: driver_register(%s) failed. Error number %d", __func__,
1109                     xpp_driver.name, ret);
1110                 goto failed_xpp_driver;
1111         }
1112         if ((ret = bus_register(&xpd_type)) < 0) {
1113                 ERR("%s: bus_register(%s) failed. Error number %d", __func__,
1114                     xpd_type.name, ret);
1115                 goto failed_xpd_bus;
1116         }
1117         return 0;
1118 failed_xpd_bus:
1119         driver_unregister(&xpp_driver);
1120 failed_xpp_driver:
1121         bus_unregister(&toplevel_bus_type);
1122 failed_toplevel:
1123         return ret;
1124 }
1125
1126 void xpp_driver_exit(void)
1127 {
1128         DBG(DEVICES, "SYSFS\n");
1129         bus_unregister(&xpd_type);
1130         driver_unregister(&xpp_driver);
1131         bus_unregister(&toplevel_bus_type);
1132 }