forked from adrianomarto/soft_uart
-
Notifications
You must be signed in to change notification settings - Fork 5
/
module.c
executable file
·401 lines (343 loc) · 10.5 KB
/
module.c
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "raspberry_soft_uart.h"
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/version.h>
#include <linux/gpio.h>
#define SOFT_UART_MAJOR 0
#define N_PORTS 1
#define NONE 0
#define TX_BUFFER_FLUSH_TIMEOUT 4000 // milliseconds
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Adriano Marto Reis");
MODULE_DESCRIPTION("Software-UART for Raspberry Pi");
MODULE_VERSION("0.2");
static int gpio_tx = 17;
module_param(gpio_tx, int, 0);
static int gpio_rx = 27;
module_param(gpio_rx, int, 0);
// Module prototypes.
static int soft_uart_open(struct tty_struct*, struct file*);
static void soft_uart_close(struct tty_struct*, struct file*);
static int soft_uart_write(struct tty_struct*, const unsigned char*, int);
static unsigned int soft_uart_write_room(struct tty_struct*);
static void soft_uart_flush_buffer(struct tty_struct*);
static unsigned int soft_uart_chars_in_buffer(struct tty_struct*);
static void soft_uart_set_termios(struct tty_struct*, const struct ktermios*);
static void soft_uart_stop(struct tty_struct*);
static void soft_uart_start(struct tty_struct*);
static void soft_uart_hangup(struct tty_struct*);
static int soft_uart_tiocmget(struct tty_struct*);
static int soft_uart_tiocmset(struct tty_struct*, unsigned int, unsigned int);
static int soft_uart_ioctl(struct tty_struct*, unsigned int, unsigned int long);
static void soft_uart_throttle(struct tty_struct*);
static void soft_uart_unthrottle(struct tty_struct*);
// Module operations.
static const struct tty_operations soft_uart_operations = {
.open = soft_uart_open,
.close = soft_uart_close,
.write = soft_uart_write,
.write_room = soft_uart_write_room,
.flush_buffer = soft_uart_flush_buffer,
.chars_in_buffer = soft_uart_chars_in_buffer,
.ioctl = soft_uart_ioctl,
.set_termios = soft_uart_set_termios,
.stop = soft_uart_stop,
.start = soft_uart_start,
.hangup = soft_uart_hangup,
.tiocmget = soft_uart_tiocmget,
.tiocmset = soft_uart_tiocmset,
.throttle = soft_uart_throttle,
.unthrottle = soft_uart_unthrottle
};
// Driver instance.
static struct tty_driver* soft_uart_driver = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
static struct tty_port port;
#endif
/**
* Module initialization.
*/
static int __init soft_uart_init(void)
{
bool success = true;
printk(KERN_INFO "soft_uart: Initializing module...\n");
success &= gpio_request(gpio_tx, "soft_uart_tx") == 0;
success &= gpio_direction_output(gpio_tx, 1) == 0;
success &= gpio_request(gpio_rx, "soft_uart_rx") == 0;
success &= gpio_direction_input(gpio_rx) == 0;
if (!success || !raspberry_soft_uart_init(gpio_to_desc(gpio_tx), gpio_to_desc(gpio_rx)))
{
printk(KERN_ALERT "soft_uart: Failed initialize GPIO.\n");
return -ENOMEM;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
printk(KERN_INFO "soft_uart: LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0).\n");
// Initializes the port.
tty_port_init(&port);
// Allocates the driver.
soft_uart_driver = tty_alloc_driver(N_PORTS, TTY_DRIVER_REAL_RAW);
// Returns if the allocation fails.
if (IS_ERR(soft_uart_driver))
{
printk(KERN_ALERT "soft_uart: Failed to allocate the driver.\n");
return -ENOMEM;
}
#else
printk(KERN_INFO "soft_uart: LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0).\n");
// Allocates the driver.
soft_uart_driver = alloc_tty_driver(N_PORTS);
// Returns if the allocation fails.
if (!soft_uart_driver)
{
printk(KERN_ALERT "soft_uart: Failed to allocate the driver.\n");
return -ENOMEM;
}
#endif
// Initializes the driver.
soft_uart_driver->owner = THIS_MODULE;
soft_uart_driver->driver_name = "soft_uart";
soft_uart_driver->name = "ttySOFT";
soft_uart_driver->major = SOFT_UART_MAJOR;
soft_uart_driver->minor_start = 0;
soft_uart_driver->flags = TTY_DRIVER_REAL_RAW;
soft_uart_driver->type = TTY_DRIVER_TYPE_SERIAL;
soft_uart_driver->subtype = SERIAL_TYPE_NORMAL;
soft_uart_driver->init_termios = tty_std_termios;
soft_uart_driver->init_termios.c_ispeed = 4800;
soft_uart_driver->init_termios.c_ospeed = 4800;
soft_uart_driver->init_termios.c_cflag = B4800 | CREAD | CS8 | CLOCAL;
// Sets the callbacks for the driver.
tty_set_operations(soft_uart_driver, &soft_uart_operations);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
// Link the port with the driver.
tty_port_link_device(&port, soft_uart_driver, 0);
#endif
// Registers the TTY driver.
if (tty_register_driver(soft_uart_driver))
{
printk(KERN_ALERT "soft_uart: Failed to register the driver.\n");
tty_driver_kref_put(soft_uart_driver);
return -1; // return if registration fails
}
printk(KERN_INFO "soft_uart: Module initialized.\n");
return 0;
}
/**
* Cleanup function that gets called when the module is unloaded.
*/
static void __exit soft_uart_exit(void)
{
printk(KERN_INFO "soft_uart: Finalizing the module...\n");
// Finalizes the soft UART.
if (!raspberry_soft_uart_finalize())
{
printk(KERN_ALERT "soft_uart: Something went wrong whilst finalizing the soft UART.\n");
}
// Unregisters the driver.
tty_unregister_driver(soft_uart_driver);
tty_driver_kref_put(soft_uart_driver);
printk(KERN_INFO "soft_uart: Module finalized.\n");
}
/**
* Opens a given TTY device.
* @param tty given TTY device
* @param file
* @return error code.
*/
static int soft_uart_open(struct tty_struct* tty, struct file* file)
{
int error = NONE;
if (raspberry_soft_uart_open(tty))
{
printk(KERN_INFO "soft_uart: Device opened.\n");
}
else
{
printk(KERN_ALERT "soft_uart: Device busy.\n");
error = -ENODEV;
}
return error;
}
/**
* Closes a given TTY device.
* @param tty
* @param file
*/
static void soft_uart_close(struct tty_struct* tty, struct file* file)
{
// Waits for the TX buffer to be empty before closing the UART.
int wait_time = 0;
while ((raspberry_soft_uart_get_tx_queue_size() > 0)
&& (wait_time < TX_BUFFER_FLUSH_TIMEOUT))
{
msleep(100);
wait_time += 100;
}
if (raspberry_soft_uart_close())
{
printk(KERN_INFO "soft_uart: Device closed.\n");
}
else
{
printk(KERN_ALERT "soft_uart: Could not close the device.\n");
}
}
/**
* Writes the contents of a given buffer into a given TTY device.
* @param tty given TTY device
* @param buffer given buffer
* @param buffer_size number of bytes contained in the given buffer
* @return number of bytes successfuly written into the TTY device
*/
static int soft_uart_write(struct tty_struct* tty, const unsigned char* buffer, int buffer_size)
{
return raspberry_soft_uart_send_string(buffer, buffer_size);
}
/**
* Tells the kernel the number of bytes that can be written to a given TTY.
* @param tty given TTY
* @return number of bytes
*/
static unsigned int soft_uart_write_room(struct tty_struct* tty)
{
return raspberry_soft_uart_get_tx_queue_room();
}
/**
* Does nothing.
* @param tty
*/
static void soft_uart_flush_buffer(struct tty_struct* tty)
{
}
/**
* Tells the kernel the number of bytes contained in the buffer of a given TTY.
* @param tty given TTY
* @return number of bytes
*/
static unsigned int soft_uart_chars_in_buffer(struct tty_struct* tty)
{
return raspberry_soft_uart_get_tx_queue_size();
}
/**
* Sets the UART parameters for a given TTY (only the baudrate is taken into account).
* @param tty given TTY
* @param termios parameters
*/
static void soft_uart_set_termios(struct tty_struct* tty, const struct ktermios* termios)
{
int cflag = 0;
speed_t baudrate = tty_get_baud_rate(tty);
printk(KERN_INFO "soft_uart: soft_uart_set_termios: baudrate = %d.\n", baudrate);
// Gets the cflag.
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
cflag = tty->termios.c_cflag;
#else
cflag = tty->termios->c_cflag;
#endif
// Verifies the number of data bits (it must be 8).
if ((cflag & CSIZE) != CS8)
{
printk(KERN_ALERT "soft_uart: Invalid number of data bits.\n");
}
// Verifies the number of stop bits (it must be 1).
if (cflag & CSTOPB)
{
printk(KERN_ALERT "soft_uart: Invalid number of stop bits.\n");
}
// Verifies the parity (it must be none).
if (cflag & PARENB)
{
printk(KERN_ALERT "soft_uart: Invalid parity.\n");
}
// Configure the baudrate.
if (!raspberry_soft_uart_set_baudrate(baudrate))
{
printk(KERN_ALERT "soft_uart: Invalid baudrate.\n");
}
}
/**
* Does nothing.
* @param tty
*/
static void soft_uart_stop(struct tty_struct* tty)
{
printk(KERN_DEBUG "soft_uart: soft_uart_stop.\n");
}
/**
* Does nothing.
* @param tty
*/
static void soft_uart_start(struct tty_struct* tty)
{
printk(KERN_DEBUG "soft_uart: soft_uart_start.\n");
}
/**
* Does nothing.
* @param tty
*/
static void soft_uart_hangup(struct tty_struct* tty)
{
printk(KERN_DEBUG "soft_uart: soft_uart_hangup.\n");
}
/**
* Does nothing.
* @param tty
*/
static int soft_uart_tiocmget(struct tty_struct* tty)
{
return 0;
}
/**
* Does nothing.
* @param tty
* @param set
* @param clear
*/
static int soft_uart_tiocmset(struct tty_struct* tty, unsigned int set, unsigned int clear)
{
return 0;
}
/**
* Does nothing.
* @param tty
* @param command
* @param parameter
*/
static int soft_uart_ioctl(struct tty_struct* tty, unsigned int command, unsigned int long parameter)
{
int error = NONE;
switch (command)
{
case TIOCMSET:
error = NONE;
break;
case TIOCMGET:
error = NONE;
break;
default:
error = -ENOIOCTLCMD;
break;
}
return error;
}
/**
* Does nothing.
* @param tty
*/
static void soft_uart_throttle(struct tty_struct* tty)
{
printk(KERN_DEBUG "soft_uart: soft_uart_throttle.\n");
}
/**
* Does nothing.
* @param tty
*/
static void soft_uart_unthrottle(struct tty_struct* tty)
{
printk(KERN_DEBUG "soft_uart: soft_uart_unthrottle.\n");
}
// Module entry points.
module_init(soft_uart_init);
module_exit(soft_uart_exit);