hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 2006-2007 Jan Kiszka <jan.kiszka@web.de>.
 * Copyright (C) 2011 Stefan Kisdaroczi <kisda@hispeed.ch>.
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#if defined(CONFIG_XENO_DRIVERS_16550A_PCI)
 
#include <linux/pci.h>
 
struct rt_16550_pci_board {
   char *name;
   resource_size_t resource_base_addr;
   unsigned int nports;
   unsigned int port_ofs;
   unsigned long irqtype;
   unsigned int baud_base;
   int tx_fifo;
};
 
#if defined(CONFIG_XENO_DRIVERS_16550A_PCI_MOXA)
 
#define PCI_DEVICE_ID_CP112UL        0x1120
#define PCI_DEVICE_ID_CP114UL        0x1143
#define PCI_DEVICE_ID_CP138U        0x1380
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,17,0)
#define PCI_DEVICE_ID_MOXA_CP102UL    0x1021
#define PCI_DEVICE_ID_MOXA_CP102U    0x1022
#define PCI_DEVICE_ID_MOXA_C104        0x1040
#define PCI_DEVICE_ID_MOXA_CP104U    0x1041
#define PCI_DEVICE_ID_MOXA_CP114    0x1141
#define PCI_DEVICE_ID_MOXA_CP118U    0x1180
#define PCI_DEVICE_ID_MOXA_CP132    0x1320
#define PCI_DEVICE_ID_MOXA_CP132U    0x1321
#define PCI_DEVICE_ID_MOXA_CP134U    0x1340
#define PCI_DEVICE_ID_MOXA_C168        0x1680
#define PCI_DEVICE_ID_MOXA_CP168U    0x1681
#endif
 
static const struct rt_16550_pci_board rt_16550_moxa_c104 = {
   .name = "Moxa C104H/PCI",
   .resource_base_addr = 2,
   .nports = 4,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_c168 = {
   .name = "Moxa C168H/PCI",
   .resource_base_addr = 2,
   .nports = 8,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp114 = {
   .name = "Moxa CP-114",
   .resource_base_addr = 2,
   .nports = 4,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp132 = {
   .name = "Moxa CP-132",
   .resource_base_addr = 2,
   .nports = 2,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp102u = {
   .name = "Moxa CP-102U",
   .resource_base_addr = 2,
   .nports = 2,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp102ul = {
   .name = "Moxa CP-102UL",
   .resource_base_addr = 2,
   .nports = 2,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp104u = {
   .name = "Moxa CP-104U",
   .resource_base_addr = 2,
   .nports = 4,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp112ul = {
   .name = "Moxa CP-112UL",
   .resource_base_addr = 2,
   .nports = 2,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp114ul = {
   .name = "Moxa CP-114UL",
   .resource_base_addr = 2,
   .nports = 4,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp118u = {
   .name = "Moxa CP-118U",
   .resource_base_addr = 2,
   .nports = 8,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp132u = {
   .name = "Moxa CP-132U",
   .resource_base_addr = 2,
   .nports = 2,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp134u = {
   .name = "Moxa CP-134U",
   .resource_base_addr = 2,
   .nports = 4,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp138u = {
   .name = "Moxa CP-138U",
   .resource_base_addr = 2,
   .nports = 8,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
 
static const struct rt_16550_pci_board rt_16550_moxa_cp168u = {
   .name = "Moxa CP-168U",
   .resource_base_addr = 2,
   .nports = 8,
   .port_ofs = 8,
   .baud_base = 921600,
   .tx_fifo = 16,
   .irqtype = RTDM_IRQTYPE_SHARED,
};
#endif
 
const struct pci_device_id rt_16550_pci_table[] = {
#if defined(CONFIG_XENO_DRIVERS_16550A_PCI_MOXA)
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104),
    .driver_data = (unsigned long)&rt_16550_moxa_c104},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168),
    .driver_data = (unsigned long)&rt_16550_moxa_c168},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114),
    .driver_data = (unsigned long)&rt_16550_moxa_cp114},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132),
    .driver_data = (unsigned long)&rt_16550_moxa_cp132},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp102u},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
    .driver_data = (unsigned long)&rt_16550_moxa_cp102ul},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp104u},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL),
    .driver_data = (unsigned long)&rt_16550_moxa_cp112ul},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL),
    .driver_data = (unsigned long)&rt_16550_moxa_cp114ul},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp118u},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp132u},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp134u},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp138u},
   {PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U),
    .driver_data = (unsigned long)&rt_16550_moxa_cp168u},
#endif
   { }
};
 
static int rt_16550_pci_probe(struct pci_dev *pdev,
                 const struct pci_device_id *ent)
{
   struct rt_16550_pci_board *board;
   int err;
   int i;
   int port = 0;
   int base_addr;
   int max_devices = 0;
 
   if (!ent->driver_data)
       return -ENODEV;
 
   board = (struct rt_16550_pci_board *)ent->driver_data;
 
   for (i = 0; i < MAX_DEVICES; i++)
       if (!rt_16550_addr_param(i))
           max_devices++;
 
   if (board->nports > max_devices)
       return -ENODEV;
 
   if ((err = pci_enable_device(pdev)))
       return err;
 
   base_addr = pci_resource_start(pdev, board->resource_base_addr);
 
   for (i = 0; i < MAX_DEVICES; i++) {
       if ((port < board->nports) && (!rt_16550_addr_param(i))) {
           io[i] = base_addr + port * board->port_ofs;
           irq[i] = pdev->irq;
           irqtype[i] = board->irqtype;
           baud_base[i] = board->baud_base;
           tx_fifo[i] = board->tx_fifo;
           port++;
       }
   }
 
   return 0;
}
 
static void rt_16550_pci_remove(struct pci_dev *pdev) {
   pci_disable_device( pdev );
};
 
static struct pci_driver rt_16550_pci_driver = {
   .name     = RT_16550_DRIVER_NAME,
   .id_table = rt_16550_pci_table,
   .probe    = rt_16550_pci_probe,
   .remove   = rt_16550_pci_remove
};
 
static int pci_registered;
 
static inline void rt_16550_pci_init(void)
{
   if (pci_register_driver(&rt_16550_pci_driver) == 0)
       pci_registered = 1;
}
 
static inline void rt_16550_pci_cleanup(void)
{
   if (pci_registered)
       pci_unregister_driver(&rt_16550_pci_driver);
}
 
#else /* Linux < 2.6.0 || !CONFIG_PCI || !(..._16550A_PCI */
 
#define rt_16550_pci_init()    do { } while (0)
#define rt_16550_pci_cleanup()    do { } while (0)
 
#endif /* Linux < 2.6.0 || !CONFIG_PCI || !(..._16550A_PCI */