Commit | Line | Data |
---|---|---|
9b27105b SB |
1 | /* |
2 | * Copyright (C) ST-Ericsson AB 2010 | |
3 | * Author: Sjur Brendeland / sjur.brandeland@stericsson.com | |
4 | * License terms: GNU General Public License (GPL) version 2 | |
5 | */ | |
6 | ||
7 | #include <linux/init.h> | |
8 | #include <linux/version.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/device.h> | |
11 | #include <linux/types.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/netdevice.h> | |
14 | #include <linux/rtnetlink.h> | |
15 | #include <linux/tty.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/if_arp.h> | |
18 | #include <net/caif/caif_device.h> | |
19 | #include <net/caif/cfcnfg.h> | |
20 | #include <linux/err.h> | |
21 | #include <linux/debugfs.h> | |
22 | ||
23 | MODULE_LICENSE("GPL"); | |
24 | MODULE_AUTHOR("Sjur Brendeland<sjur.brandeland@stericsson.com>"); | |
25 | MODULE_DESCRIPTION("CAIF serial device TTY line discipline"); | |
26 | MODULE_LICENSE("GPL"); | |
27 | MODULE_ALIAS_LDISC(N_CAIF); | |
28 | ||
29 | #define SEND_QUEUE_LOW 10 | |
30 | #define SEND_QUEUE_HIGH 100 | |
31 | #define CAIF_SENDING 1 /* Bit 1 = 0x02*/ | |
32 | #define CAIF_FLOW_OFF_SENT 4 /* Bit 4 = 0x10 */ | |
33 | #define MAX_WRITE_CHUNK 4096 | |
34 | #define ON 1 | |
35 | #define OFF 0 | |
36 | #define CAIF_MAX_MTU 4096 | |
37 | ||
38 | /*This list is protected by the rtnl lock. */ | |
39 | static LIST_HEAD(ser_list); | |
40 | ||
41 | static int ser_loop; | |
42 | module_param(ser_loop, bool, S_IRUGO); | |
43 | MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode."); | |
44 | ||
45 | static int ser_use_stx = 1; | |
46 | module_param(ser_use_stx, bool, S_IRUGO); | |
47 | MODULE_PARM_DESC(ser_use_stx, "STX enabled or not."); | |
48 | ||
49 | static int ser_use_fcs = 1; | |
50 | ||
51 | module_param(ser_use_fcs, bool, S_IRUGO); | |
52 | MODULE_PARM_DESC(ser_use_fcs, "FCS enabled or not."); | |
53 | ||
54 | static int ser_write_chunk = MAX_WRITE_CHUNK; | |
55 | module_param(ser_write_chunk, int, S_IRUGO); | |
56 | ||
57 | MODULE_PARM_DESC(ser_write_chunk, "Maximum size of data written to UART."); | |
58 | ||
59 | static struct dentry *debugfsdir; | |
60 | ||
61 | static int caif_net_open(struct net_device *dev); | |
62 | static int caif_net_close(struct net_device *dev); | |
63 | ||
64 | struct ser_device { | |
65 | struct caif_dev_common common; | |
66 | struct list_head node; | |
67 | struct net_device *dev; | |
68 | struct sk_buff_head head; | |
69 | struct tty_struct *tty; | |
70 | bool tx_started; | |
71 | unsigned long state; | |
72 | char *tty_name; | |
73 | #ifdef CONFIG_DEBUG_FS | |
74 | struct dentry *debugfs_tty_dir; | |
75 | struct debugfs_blob_wrapper tx_blob; | |
76 | struct debugfs_blob_wrapper rx_blob; | |
77 | u8 rx_data[128]; | |
78 | u8 tx_data[128]; | |
79 | u8 tty_status; | |
80 | ||
81 | #endif | |
82 | }; | |
83 | ||
84 | static void caifdev_setup(struct net_device *dev); | |
85 | static void ldisc_tx_wakeup(struct tty_struct *tty); | |
86 | #ifdef CONFIG_DEBUG_FS | |
87 | static inline void update_tty_status(struct ser_device *ser) | |
88 | { | |
89 | ser->tty_status = | |
90 | ser->tty->stopped << 5 | | |
91 | ser->tty->hw_stopped << 4 | | |
92 | ser->tty->flow_stopped << 3 | | |
93 | ser->tty->packet << 2 | | |
94 | ser->tty->low_latency << 1 | | |
95 | ser->tty->warned; | |
96 | } | |
97 | static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty) | |
98 | { | |
99 | ser->debugfs_tty_dir = | |
100 | debugfs_create_dir(tty->name, debugfsdir); | |
101 | if (!IS_ERR(ser->debugfs_tty_dir)) { | |
102 | debugfs_create_blob("last_tx_msg", S_IRUSR, | |
103 | ser->debugfs_tty_dir, | |
104 | &ser->tx_blob); | |
105 | ||
106 | debugfs_create_blob("last_rx_msg", S_IRUSR, | |
107 | ser->debugfs_tty_dir, | |
108 | &ser->rx_blob); | |
109 | ||
110 | debugfs_create_x32("ser_state", S_IRUSR, | |
111 | ser->debugfs_tty_dir, | |
112 | (u32 *)&ser->state); | |
113 | ||
114 | debugfs_create_x8("tty_status", S_IRUSR, | |
115 | ser->debugfs_tty_dir, | |
116 | &ser->tty_status); | |
117 | ||
118 | } | |
119 | ser->tx_blob.data = ser->tx_data; | |
120 | ser->tx_blob.size = 0; | |
121 | ser->rx_blob.data = ser->rx_data; | |
122 | ser->rx_blob.size = 0; | |
123 | } | |
124 | ||
125 | static inline void debugfs_deinit(struct ser_device *ser) | |
126 | { | |
127 | debugfs_remove_recursive(ser->debugfs_tty_dir); | |
128 | } | |
129 | ||
130 | static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size) | |
131 | { | |
132 | if (size > sizeof(ser->rx_data)) | |
133 | size = sizeof(ser->rx_data); | |
134 | memcpy(ser->rx_data, data, size); | |
135 | ser->rx_blob.data = ser->rx_data; | |
136 | ser->rx_blob.size = size; | |
137 | } | |
138 | ||
139 | static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size) | |
140 | { | |
141 | if (size > sizeof(ser->tx_data)) | |
142 | size = sizeof(ser->tx_data); | |
143 | memcpy(ser->tx_data, data, size); | |
144 | ser->tx_blob.data = ser->tx_data; | |
145 | ser->tx_blob.size = size; | |
146 | } | |
147 | #else | |
148 | static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty) | |
149 | { | |
150 | } | |
151 | ||
152 | static inline void debugfs_deinit(struct ser_device *ser) | |
153 | { | |
154 | } | |
155 | ||
156 | static inline void update_tty_status(struct ser_device *ser) | |
157 | { | |
158 | } | |
159 | ||
160 | static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size) | |
161 | { | |
162 | } | |
163 | ||
164 | static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size) | |
165 | { | |
166 | } | |
167 | ||
168 | #endif | |
169 | ||
170 | static void ldisc_receive(struct tty_struct *tty, const u8 *data, | |
171 | char *flags, int count) | |
172 | { | |
173 | struct sk_buff *skb = NULL; | |
174 | struct ser_device *ser; | |
175 | int ret; | |
176 | u8 *p; | |
177 | ser = tty->disc_data; | |
178 | ||
179 | /* | |
180 | * NOTE: flags may contain information about break or overrun. | |
181 | * This is not yet handled. | |
182 | */ | |
183 | ||
184 | ||
185 | /* | |
186 | * Workaround for garbage at start of transmission, | |
187 | * only enable if STX handling is not enabled. | |
188 | */ | |
189 | if (!ser->common.use_stx && !ser->tx_started) { | |
190 | dev_info(&ser->dev->dev, | |
191 | "Bytes received before initial transmission -" | |
192 | "bytes discarded.\n"); | |
193 | return; | |
194 | } | |
195 | ||
196 | BUG_ON(ser->dev == NULL); | |
197 | ||
198 | /* Get a suitable caif packet and copy in data. */ | |
199 | skb = netdev_alloc_skb(ser->dev, count+1); | |
200 | BUG_ON(skb == NULL); | |
201 | p = skb_put(skb, count); | |
202 | memcpy(p, data, count); | |
203 | ||
204 | skb->protocol = htons(ETH_P_CAIF); | |
205 | skb_reset_mac_header(skb); | |
206 | skb->dev = ser->dev; | |
207 | debugfs_rx(ser, data, count); | |
208 | /* Push received packet up the stack. */ | |
209 | ret = netif_rx_ni(skb); | |
210 | if (!ret) { | |
211 | ser->dev->stats.rx_packets++; | |
212 | ser->dev->stats.rx_bytes += count; | |
213 | } else | |
214 | ++ser->dev->stats.rx_dropped; | |
215 | update_tty_status(ser); | |
216 | } | |
217 | ||
218 | static int handle_tx(struct ser_device *ser) | |
219 | { | |
220 | struct tty_struct *tty; | |
221 | struct sk_buff *skb; | |
222 | int tty_wr, len, room; | |
223 | tty = ser->tty; | |
224 | ser->tx_started = true; | |
225 | ||
226 | /* Enter critical section */ | |
227 | if (test_and_set_bit(CAIF_SENDING, &ser->state)) | |
228 | return 0; | |
229 | ||
230 | /* skb_peek is safe because handle_tx is called after skb_queue_tail */ | |
231 | while ((skb = skb_peek(&ser->head)) != NULL) { | |
232 | ||
233 | /* Make sure you don't write too much */ | |
234 | len = skb->len; | |
235 | room = tty_write_room(tty); | |
236 | if (!room) | |
237 | break; | |
238 | if (room > ser_write_chunk) | |
239 | room = ser_write_chunk; | |
240 | if (len > room) | |
241 | len = room; | |
242 | ||
243 | /* Write to tty or loopback */ | |
244 | if (!ser_loop) { | |
245 | tty_wr = tty->ops->write(tty, skb->data, len); | |
246 | update_tty_status(ser); | |
247 | } else { | |
248 | tty_wr = len; | |
249 | ldisc_receive(tty, skb->data, NULL, len); | |
250 | } | |
251 | ser->dev->stats.tx_packets++; | |
252 | ser->dev->stats.tx_bytes += tty_wr; | |
253 | ||
254 | /* Error on TTY ?! */ | |
255 | if (tty_wr < 0) | |
256 | goto error; | |
257 | /* Reduce buffer written, and discard if empty */ | |
258 | skb_pull(skb, tty_wr); | |
259 | if (skb->len == 0) { | |
260 | struct sk_buff *tmp = skb_dequeue(&ser->head); | |
261 | BUG_ON(tmp != skb); | |
262 | if (in_interrupt()) | |
263 | dev_kfree_skb_irq(skb); | |
264 | else | |
265 | kfree_skb(skb); | |
266 | } | |
267 | } | |
268 | /* Send flow off if queue is empty */ | |
269 | if (ser->head.qlen <= SEND_QUEUE_LOW && | |
270 | test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) && | |
271 | ser->common.flowctrl != NULL) | |
272 | ser->common.flowctrl(ser->dev, ON); | |
273 | clear_bit(CAIF_SENDING, &ser->state); | |
274 | return 0; | |
275 | error: | |
276 | clear_bit(CAIF_SENDING, &ser->state); | |
277 | return tty_wr; | |
278 | } | |
279 | ||
280 | static int caif_xmit(struct sk_buff *skb, struct net_device *dev) | |
281 | { | |
282 | struct ser_device *ser; | |
283 | BUG_ON(dev == NULL); | |
284 | ser = netdev_priv(dev); | |
285 | ||
286 | /* Send flow off once, on high water mark */ | |
287 | if (ser->head.qlen > SEND_QUEUE_HIGH && | |
288 | !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) && | |
289 | ser->common.flowctrl != NULL) | |
290 | ||
291 | ser->common.flowctrl(ser->dev, OFF); | |
292 | ||
293 | skb_queue_tail(&ser->head, skb); | |
294 | return handle_tx(ser); | |
295 | } | |
296 | ||
297 | ||
298 | static void ldisc_tx_wakeup(struct tty_struct *tty) | |
299 | { | |
300 | struct ser_device *ser; | |
301 | ser = tty->disc_data; | |
302 | BUG_ON(ser == NULL); | |
303 | BUG_ON(ser->tty != tty); | |
304 | handle_tx(ser); | |
305 | } | |
306 | ||
307 | ||
308 | static int ldisc_open(struct tty_struct *tty) | |
309 | { | |
310 | struct ser_device *ser; | |
311 | struct net_device *dev; | |
312 | char name[64]; | |
313 | int result; | |
314 | ||
c93f0940 AC |
315 | /* No write no play */ |
316 | if (tty->ops->write == NULL) | |
317 | return -EOPNOTSUPP; | |
318 | ||
9b27105b SB |
319 | sprintf(name, "cf%s", tty->name); |
320 | dev = alloc_netdev(sizeof(*ser), name, caifdev_setup); | |
321 | ser = netdev_priv(dev); | |
322 | ser->tty = tty; | |
323 | ser->dev = dev; | |
324 | debugfs_init(ser, tty); | |
325 | tty->receive_room = N_TTY_BUF_SIZE; | |
326 | tty->disc_data = ser; | |
327 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | |
328 | rtnl_lock(); | |
329 | result = register_netdevice(dev); | |
330 | if (result) { | |
331 | rtnl_unlock(); | |
332 | free_netdev(dev); | |
333 | return -ENODEV; | |
334 | } | |
335 | ||
336 | list_add(&ser->node, &ser_list); | |
337 | rtnl_unlock(); | |
338 | netif_stop_queue(dev); | |
339 | update_tty_status(ser); | |
340 | return 0; | |
341 | } | |
342 | ||
343 | static void ldisc_close(struct tty_struct *tty) | |
344 | { | |
345 | struct ser_device *ser = tty->disc_data; | |
346 | /* Remove may be called inside or outside of rtnl_lock */ | |
347 | int islocked = rtnl_is_locked(); | |
348 | if (!islocked) | |
349 | rtnl_lock(); | |
350 | /* device is freed automagically by net-sysfs */ | |
351 | dev_close(ser->dev); | |
352 | unregister_netdevice(ser->dev); | |
353 | list_del(&ser->node); | |
354 | debugfs_deinit(ser); | |
355 | if (!islocked) | |
356 | rtnl_unlock(); | |
357 | } | |
358 | ||
359 | /* The line discipline structure. */ | |
360 | static struct tty_ldisc_ops caif_ldisc = { | |
361 | .owner = THIS_MODULE, | |
362 | .magic = TTY_LDISC_MAGIC, | |
363 | .name = "n_caif", | |
364 | .open = ldisc_open, | |
365 | .close = ldisc_close, | |
366 | .receive_buf = ldisc_receive, | |
367 | .write_wakeup = ldisc_tx_wakeup | |
368 | }; | |
369 | ||
370 | static int register_ldisc(void) | |
371 | { | |
372 | int result; | |
373 | result = tty_register_ldisc(N_CAIF, &caif_ldisc); | |
374 | if (result < 0) { | |
375 | pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF, | |
376 | result); | |
377 | return result; | |
378 | } | |
379 | return result; | |
380 | } | |
381 | static const struct net_device_ops netdev_ops = { | |
382 | .ndo_open = caif_net_open, | |
383 | .ndo_stop = caif_net_close, | |
384 | .ndo_start_xmit = caif_xmit | |
385 | }; | |
386 | ||
387 | static void caifdev_setup(struct net_device *dev) | |
388 | { | |
389 | struct ser_device *serdev = netdev_priv(dev); | |
390 | dev->features = 0; | |
391 | dev->netdev_ops = &netdev_ops; | |
392 | dev->type = ARPHRD_CAIF; | |
393 | dev->flags = IFF_POINTOPOINT | IFF_NOARP; | |
394 | dev->mtu = CAIF_MAX_MTU; | |
395 | dev->hard_header_len = CAIF_NEEDED_HEADROOM; | |
396 | dev->tx_queue_len = 0; | |
397 | dev->destructor = free_netdev; | |
398 | skb_queue_head_init(&serdev->head); | |
399 | serdev->common.link_select = CAIF_LINK_LOW_LATENCY; | |
400 | serdev->common.use_frag = true; | |
401 | serdev->common.use_stx = ser_use_stx; | |
402 | serdev->common.use_fcs = ser_use_fcs; | |
403 | serdev->dev = dev; | |
404 | } | |
405 | ||
406 | ||
407 | static int caif_net_open(struct net_device *dev) | |
408 | { | |
409 | struct ser_device *ser; | |
410 | ser = netdev_priv(dev); | |
411 | netif_wake_queue(dev); | |
412 | return 0; | |
413 | } | |
414 | ||
415 | static int caif_net_close(struct net_device *dev) | |
416 | { | |
417 | netif_stop_queue(dev); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | static int __init caif_ser_init(void) | |
422 | { | |
423 | int ret; | |
424 | ret = register_ldisc(); | |
425 | debugfsdir = debugfs_create_dir("caif_serial", NULL); | |
426 | return ret; | |
427 | } | |
428 | ||
429 | static void __exit caif_ser_exit(void) | |
430 | { | |
431 | struct ser_device *ser = NULL; | |
432 | struct list_head *node; | |
433 | struct list_head *_tmp; | |
434 | list_for_each_safe(node, _tmp, &ser_list) { | |
435 | ser = list_entry(node, struct ser_device, node); | |
436 | dev_close(ser->dev); | |
437 | unregister_netdevice(ser->dev); | |
438 | list_del(node); | |
439 | } | |
440 | tty_unregister_ldisc(N_CAIF); | |
441 | debugfs_remove_recursive(debugfsdir); | |
442 | } | |
443 | ||
444 | module_init(caif_ser_init); | |
445 | module_exit(caif_ser_exit); |