Root/drivers/ieee802154/serial.c

1/*
2 * ZigBee TTY line discipline.
3 *
4 * Provides interface between ZigBee stack and IEEE 802.15.4 compatible
5 * firmware over serial line. Communication protocol is described below.
6 *
7 * Copyright (C) 2007, 2008, 2009 Siemens AG
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Written by:
23 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
24 * Maxim Osipov <maxim.osipov@siemens.com>
25 * Sergey Lapin <slapin@ossfans.org>
26 */
27
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/completion.h>
31#include <linux/tty.h>
32#include <linux/skbuff.h>
33#include <linux/sched.h>
34#include <net/mac802154.h>
35#include <net/wpan-phy.h>
36
37
38/* NOTE: be sure to use here the same values as in the firmware */
39#define START_BYTE1 'z'
40#define START_BYTE2 'b'
41#define MAX_DATA_SIZE 127
42
43#define IDLE_MODE 0x00
44#define RX_MODE 0x02
45#define TX_MODE 0x03
46#define FORCE_TRX_OFF 0xF0
47
48#define STATUS_SUCCESS 0
49#define STATUS_RX_ON 1
50#define STATUS_TX_ON 2
51#define STATUS_TRX_OFF 3
52#define STATUS_IDLE 4
53#define STATUS_BUSY 5
54#define STATUS_BUSY_RX 6
55#define STATUS_BUSY_TX 7
56#define STATUS_ERR 8
57
58#define STATUS_WAIT ((u8) -1) /* waiting for the answer */
59
60/* We re-use PPP ioctl for our purposes */
61#define PPPIOCGUNIT _IOR('t', 86, int) /* get ppp unit number */
62
63/*
64 * The following messages are used to control ZigBee firmware.
65 * All communication has request/response format,
66 * except of asynchronous incoming data stream (DATA_RECV_* messages).
67 */
68enum {
69    NO_ID = 0, /* means no pending id */
70
71    /* Driver to Firmware */
72    CMD_OPEN = 0x01, /* u8 id */
73    CMD_CLOSE = 0x02, /* u8 id */
74    CMD_SET_CHANNEL = 0x04, /* u8 id, u8 channel */
75    CMD_ED = 0x05, /* u8 id */
76    CMD_CCA = 0x06, /* u8 id */
77    CMD_SET_STATE = 0x07, /* u8 id, u8 flag */
78    DATA_XMIT_BLOCK = 0x09, /* u8 id, u8 len, u8 data[len] */
79    RESP_RECV_BLOCK = 0x0b, /* u8 id, u8 status */
80    CMD_ADDRESS = 0x0d, /* u8 id */
81
82    /* Firmware to Driver */
83    RESP_OPEN = 0x81, /* u8 id, u8 status */
84    RESP_CLOSE = 0x82, /* u8 id, u8 status */
85    RESP_SET_CHANNEL = 0x84, /* u8 id, u8 status */
86    RESP_ED = 0x85, /* u8 id, u8 status, u8 level */
87    RESP_CCA = 0x86, /* u8 id, u8 status */
88    RESP_SET_STATE = 0x87, /* u8 id, u8 status */
89    RESP_XMIT_BLOCK = 0x89, /* u8 id, u8 status */
90    DATA_RECV_BLOCK = 0x8b, /* u8 id, u8 lq, u8 len, u8 data[len] */
91    RESP_ADDRESS = 0x8d, /* u8 id, u8 status, u8 u8 u8 u8 u8 u8 u8 u8 address */
92};
93
94enum {
95    STATE_WAIT_START1,
96    STATE_WAIT_START2,
97    STATE_WAIT_COMMAND,
98    STATE_WAIT_PARAM1,
99    STATE_WAIT_PARAM2,
100    STATE_WAIT_DATA
101};
102
103struct zb_device {
104    /* Relative devices */
105    struct tty_struct *tty;
106    struct ieee802154_dev *dev;
107
108    /* locks the ldisc for the command */
109    struct mutex mutex;
110
111    /* command completition */
112    wait_queue_head_t wq;
113    u8 status;
114    u8 ed;
115
116    /* Internal state */
117    struct completion open_done;
118    unsigned char opened;
119    u8 pending_id;
120    unsigned int pending_size;
121    u8 *pending_data;
122    /* FIXME: WE NEED LOCKING!!! */
123
124    /* Command (rx) processing */
125    int state;
126    unsigned char id;
127    unsigned char param1;
128    unsigned char param2;
129    unsigned char index;
130    unsigned char data[MAX_DATA_SIZE];
131};
132
133/*****************************************************************************
134 * ZigBee serial device protocol handling
135 *****************************************************************************/
136static int _open_dev(struct zb_device *zbdev);
137
138static int
139_send_pending_data(struct zb_device *zbdev)
140{
141    struct tty_struct *tty;
142
143    BUG_ON(!zbdev);
144    tty = zbdev->tty;
145    if (!tty)
146        return -ENODEV;
147
148    zbdev->status = STATUS_WAIT;
149
150    /* Debug info */
151    printk(KERN_INFO "%s, %d bytes\n", __func__,
152            zbdev->pending_size);
153#ifdef DEBUG
154    print_hex_dump_bytes("send_pending_data ", DUMP_PREFIX_NONE,
155            zbdev->pending_data, zbdev->pending_size);
156#endif
157
158    if (tty->driver->ops->write(tty, zbdev->pending_data,
159                zbdev->pending_size) != zbdev->pending_size) {
160        printk(KERN_ERR "%s: device write failed\n", __func__);
161        return -1;
162    }
163
164    return 0;
165}
166
167static int
168send_cmd(struct zb_device *zbdev, u8 id)
169{
170    u8 len = 0;
171    /* 4 because of 2 start bytes, id and optional extra */
172    u8 buf[4];
173
174    /* Check arguments */
175    BUG_ON(!zbdev);
176
177    if (!zbdev->opened) {
178        if (!_open_dev(zbdev))
179            return -EAGAIN;
180    }
181
182    pr_debug("%s(): id = %u\n", __func__, id);
183    if (zbdev->pending_size) {
184        printk(KERN_ERR "%s(): cmd is already pending, id = %u\n",
185            __func__, zbdev->pending_id);
186        BUG();
187    }
188
189    /* Prepare a message */
190    buf[len++] = START_BYTE1;
191    buf[len++] = START_BYTE2;
192    buf[len++] = id;
193
194    zbdev->pending_id = id;
195    zbdev->pending_size = len;
196    zbdev->pending_data = kzalloc(zbdev->pending_size, GFP_KERNEL);
197    if (!zbdev->pending_data) {
198        printk(KERN_ERR "%s(): unable to allocate memory\n", __func__);
199        zbdev->pending_id = 0;
200        zbdev->pending_size = 0;
201        return -ENOMEM;
202    }
203    memcpy(zbdev->pending_data, buf, len);
204
205    return _send_pending_data(zbdev);
206}
207
208static int
209send_cmd2(struct zb_device *zbdev, u8 id, u8 extra)
210{
211    u8 len = 0;
212    /* 4 because of 2 start bytes, id and optional extra */
213    u8 buf[4];
214
215    /* Check arguments */
216    BUG_ON(!zbdev);
217
218    if (!zbdev->opened) {
219        if (!_open_dev(zbdev))
220            return -EAGAIN;
221    }
222
223    pr_debug("%s(): id = %u\n", __func__, id);
224    if (zbdev->pending_size) {
225        printk(KERN_ERR "%s(): cmd is already pending, id = %u\n",
226            __func__, zbdev->pending_id);
227        BUG();
228    }
229
230    /* Prepare a message */
231    buf[len++] = START_BYTE1;
232    buf[len++] = START_BYTE2;
233    buf[len++] = id;
234    buf[len++] = extra;
235
236    zbdev->pending_id = id;
237    zbdev->pending_size = len;
238    zbdev->pending_data = kzalloc(zbdev->pending_size, GFP_KERNEL);
239    if (!zbdev->pending_data) {
240        printk(KERN_ERR "%s(): unable to allocate memory\n", __func__);
241        zbdev->pending_id = 0;
242        zbdev->pending_size = 0;
243        return -ENOMEM;
244    }
245    memcpy(zbdev->pending_data, buf, len);
246
247    return _send_pending_data(zbdev);
248}
249
250static int
251send_block(struct zb_device *zbdev, u8 len, u8 *data)
252{
253    u8 i = 0, buf[4]; /* 4 because of 2 start bytes, id and len */
254
255    /* Check arguments */
256    BUG_ON(!zbdev);
257
258    if (!zbdev->opened) {
259        if (!_open_dev(zbdev))
260            return -EAGAIN;
261    }
262
263    pr_debug("%s(): id = %u\n", __func__, DATA_XMIT_BLOCK);
264    if (zbdev->pending_size) {
265        printk(KERN_ERR "%s(): cmd is already pending, id = %u\n",
266            __func__, zbdev->pending_id);
267        BUG();
268    }
269
270    /* Prepare a message */
271    buf[i++] = START_BYTE1;
272    buf[i++] = START_BYTE2;
273    buf[i++] = DATA_XMIT_BLOCK;
274    buf[i++] = len;
275
276    zbdev->pending_id = DATA_XMIT_BLOCK;
277    zbdev->pending_size = i + len;
278    zbdev->pending_data = kzalloc(zbdev->pending_size, GFP_KERNEL);
279    if (!zbdev->pending_data) {
280        printk(KERN_ERR "%s(): unable to allocate memory\n", __func__);
281        zbdev->pending_id = 0;
282        zbdev->pending_size = 0;
283        return -ENOMEM;
284    }
285    memcpy(zbdev->pending_data, buf, i);
286    memcpy(zbdev->pending_data + i, data, len);
287
288    return _send_pending_data(zbdev);
289}
290
291static void
292cleanup(struct zb_device *zbdev)
293{
294    zbdev->state = STATE_WAIT_START1;
295    zbdev->id = 0;
296    zbdev->param1 = 0;
297    zbdev->param2 = 0;
298    zbdev->index = 0;
299}
300
301static int
302is_command(unsigned char c)
303{
304    switch (c) {
305    /* ids we can get here: */
306    case RESP_OPEN:
307    case RESP_CLOSE:
308    case RESP_SET_CHANNEL:
309    case RESP_ED:
310    case RESP_CCA:
311    case RESP_SET_STATE:
312    case RESP_XMIT_BLOCK:
313    case DATA_RECV_BLOCK:
314    case RESP_ADDRESS:
315        return 1;
316    }
317    return 0;
318}
319
320static int
321_match_pending_id(struct zb_device *zbdev)
322{
323    return ((CMD_OPEN == zbdev->pending_id &&
324            RESP_OPEN == zbdev->id) ||
325        (CMD_CLOSE == zbdev->pending_id &&
326            RESP_CLOSE == zbdev->id) ||
327        (CMD_SET_CHANNEL == zbdev->pending_id &&
328            RESP_SET_CHANNEL == zbdev->id) ||
329        (CMD_ED == zbdev->pending_id &&
330            RESP_ED == zbdev->id) ||
331        (CMD_CCA == zbdev->pending_id &&
332            RESP_CCA == zbdev->id) ||
333        (CMD_SET_STATE == zbdev->pending_id &&
334            RESP_SET_STATE == zbdev->id) ||
335        (DATA_XMIT_BLOCK == zbdev->pending_id &&
336            RESP_XMIT_BLOCK == zbdev->id) ||
337        (DATA_RECV_BLOCK == zbdev->id) ||
338        (CMD_ADDRESS == zbdev->pending_id &&
339            RESP_ADDRESS == zbdev->id));
340}
341
342static void serial_net_rx(struct zb_device *zbdev)
343{
344    /* zbdev->param1 is LQI
345     * zbdev->param2 is length of data
346     * zbdev->data is data itself
347     */
348    struct sk_buff *skb;
349    skb = alloc_skb(zbdev->param2, GFP_ATOMIC);
350    skb_put(skb, zbdev->param2);
351    skb_copy_to_linear_data(skb, zbdev->data, zbdev->param2);
352    ieee802154_rx_irqsafe(zbdev->dev, skb, zbdev->param1);
353}
354
355static void
356process_command(struct zb_device *zbdev)
357{
358    /* Command processing */
359    if (!_match_pending_id(zbdev))
360        return;
361
362    if (RESP_OPEN == zbdev->id && STATUS_SUCCESS == zbdev->param1) {
363        zbdev->opened = 1;
364        pr_debug("Opened device\n");
365        complete(&zbdev->open_done);
366        /* Input is not processed during output, so
367         * using completion is not possible during output.
368         * so we need to handle open as any other command
369         * and hope for best
370         */
371        return;
372    }
373
374    if (!zbdev->opened)
375        return;
376
377    zbdev->pending_id = 0;
378    kfree(zbdev->pending_data);
379    zbdev->pending_data = NULL;
380    zbdev->pending_size = 0;
381    if (zbdev->id != DATA_RECV_BLOCK) {
382        /* XXX: w/around for old FW, REMOVE */
383        if (zbdev->param1 == STATUS_IDLE)
384            zbdev->status = STATUS_SUCCESS;
385        else
386            zbdev->status = zbdev->param1;
387    }
388
389    switch (zbdev->id) {
390    case RESP_ED:
391        zbdev->ed = zbdev->param2;
392        break;
393    case DATA_RECV_BLOCK:
394        pr_debug("Received block, lqi %02x, len %02x\n",
395                zbdev->param1, zbdev->param2);
396        /* zbdev->param1 is LQ, zbdev->param2 is length */
397        serial_net_rx(zbdev);
398        break;
399    case RESP_ADDRESS:
400        pr_debug("Received address, %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
401            zbdev->data[0], zbdev->data[1], zbdev->data[2], zbdev->data[3],
402            zbdev->data[4], zbdev->data[5], zbdev->data[6], zbdev->data[7]);
403        break;
404    }
405
406    wake_up(&zbdev->wq);
407}
408
409static void
410process_char(struct zb_device *zbdev, unsigned char c)
411{
412    /* Data processing */
413    switch (zbdev->state) {
414    case STATE_WAIT_START1:
415        if (START_BYTE1 == c)
416            zbdev->state = STATE_WAIT_START2;
417        break;
418
419    case STATE_WAIT_START2:
420        if (START_BYTE2 == c)
421            zbdev->state = STATE_WAIT_COMMAND;
422        else
423            cleanup(zbdev);
424        break;
425
426    case STATE_WAIT_COMMAND:
427        if (is_command(c)) {
428            zbdev->id = c;
429            zbdev->state = STATE_WAIT_PARAM1;
430        } else {
431            cleanup(zbdev);
432            printk(KERN_ERR "%s, unexpected command id: %x\n",
433                    __func__, c);
434        }
435        break;
436
437    case STATE_WAIT_PARAM1:
438        zbdev->param1 = c;
439        if ((RESP_ED == zbdev->id) || (DATA_RECV_BLOCK == zbdev->id))
440            zbdev->state = STATE_WAIT_PARAM2;
441        else if (RESP_ADDRESS == zbdev->id) {
442            zbdev->param2 = 8;
443            zbdev->state = STATE_WAIT_DATA;
444        } else {
445            process_command(zbdev);
446            cleanup(zbdev);
447        }
448        break;
449
450    case STATE_WAIT_PARAM2:
451        zbdev->param2 = c;
452        if (RESP_ED == zbdev->id) {
453            process_command(zbdev);
454            cleanup(zbdev);
455        } else if (DATA_RECV_BLOCK == zbdev->id)
456            zbdev->state = STATE_WAIT_DATA;
457        else
458            cleanup(zbdev);
459        break;
460
461    case STATE_WAIT_DATA:
462        if (zbdev->index < sizeof(zbdev->data)) {
463            zbdev->data[zbdev->index] = c;
464            zbdev->index++;
465            /*
466             * Pending data is received,
467             * param2 is length for DATA_RECV_BLOCK
468             */
469            if (zbdev->index == zbdev->param2) {
470                process_command(zbdev);
471                cleanup(zbdev);
472            }
473        } else {
474            printk(KERN_ERR "%s(): data size is greater "
475                "than buffer available\n", __func__);
476            cleanup(zbdev);
477        }
478        break;
479
480    default:
481        cleanup(zbdev);
482    }
483}
484
485/*****************************************************************************
486 * Device operations for IEEE 802.15.4 PHY side interface ZigBee stack
487 *****************************************************************************/
488
489static int _open_dev(struct zb_device *zbdev)
490{
491    int retries;
492    u8 len = 0;
493    /* 4 because of 2 start bytes, id and optional extra */
494    u8 buf[4];
495
496    /* Check arguments */
497    BUG_ON(!zbdev);
498    if (zbdev->opened)
499        return 1;
500
501    pr_debug("%s()\n", __func__);
502    if (zbdev->pending_size) {
503        printk(KERN_ERR "%s(): cmd is already pending, id = %u\n",
504            __func__, zbdev->pending_id);
505        BUG();
506    }
507
508    /* Prepare a message */
509    buf[len++] = START_BYTE1;
510    buf[len++] = START_BYTE2;
511    buf[len++] = CMD_OPEN;
512
513    zbdev->pending_id = CMD_OPEN;
514    zbdev->pending_size = len;
515    zbdev->pending_data = kzalloc(zbdev->pending_size, GFP_KERNEL);
516    if (!zbdev->pending_data) {
517        printk(KERN_ERR "%s(): unable to allocate memory\n", __func__);
518        zbdev->pending_id = 0;
519        zbdev->pending_size = 0;
520        return -ENOMEM;
521    }
522    memcpy(zbdev->pending_data, buf, len);
523
524    retries = 5;
525    while (!zbdev->opened && retries) {
526        if (_send_pending_data(zbdev) != 0)
527            return 0;
528
529        /* 3 second before retransmission */
530        wait_for_completion_interruptible_timeout(
531                &zbdev->open_done, msecs_to_jiffies(1000));
532        --retries;
533    }
534
535    zbdev->pending_id = 0;
536    kfree(zbdev->pending_data);
537    zbdev->pending_data = NULL;
538    zbdev->pending_size = 0;
539
540    if (zbdev->opened) {
541        printk(KERN_INFO "Opened connection to device\n");
542        return 1;
543    }
544
545    return 0;
546}
547
548/* Valid channels: 1-16 */
549static int
550ieee802154_serial_set_channel(struct ieee802154_dev *dev, int page, int channel)
551{
552    struct zb_device *zbdev;
553    int ret = 0;
554
555    pr_debug("%s %d\n", __func__, channel);
556
557    zbdev = dev->priv;
558    if (NULL == zbdev) {
559        printk(KERN_ERR "%s: wrong phy\n", __func__);
560        return -EINVAL;
561    }
562
563    BUG_ON(page != 0);
564    /* Our channels are actually from 11 to 26
565     * We have IEEE802.15.4 channel no from 0 to 26.
566     * channels 0-10 are not valid for us */
567    BUG_ON(channel < 11 || channel > 26);
568    /* ... but our crappy firmware numbers channels from 1 to 16
569     * which is a mystery. We suould enforce that using PIB API
570     * but additional checking here won't kill, and gcc will
571     * optimize this stuff anyway. */
572    BUG_ON((channel - 10) < 1 && (channel - 10) > 16);
573    if (mutex_lock_interruptible(&zbdev->mutex))
574        return -EINTR;
575    ret = send_cmd2(zbdev, CMD_SET_CHANNEL, channel - 10);
576    if (ret)
577        goto out;
578
579    if (wait_event_interruptible_timeout(zbdev->wq,
580                zbdev->status != STATUS_WAIT,
581                msecs_to_jiffies(1000)) > 0) {
582        if (zbdev->status != STATUS_SUCCESS)
583            ret = -EBUSY;
584    } else
585        ret = -EINTR;
586
587    if (!ret)
588        zbdev->dev->phy->current_channel = channel;
589out:
590    mutex_unlock(&zbdev->mutex);
591    pr_debug("%s end\n", __func__);
592    return ret;
593}
594
595static int
596ieee802154_serial_ed(struct ieee802154_dev *dev, u8 *level)
597{
598    struct zb_device *zbdev;
599    int ret = 0;
600
601    pr_debug("%s\n", __func__);
602
603    zbdev = dev->priv;
604    if (NULL == zbdev) {
605        printk(KERN_ERR "%s: wrong phy\n", __func__);
606        return -EINVAL;
607    }
608
609    if (mutex_lock_interruptible(&zbdev->mutex))
610        return -EINTR;
611
612    ret = send_cmd(zbdev, CMD_ED);
613    if (ret)
614        goto out;
615
616    if (wait_event_interruptible_timeout(zbdev->wq,
617                zbdev->status != STATUS_WAIT,
618                msecs_to_jiffies(1000)) > 0) {
619        *level = zbdev->ed;
620        if (zbdev->status != STATUS_SUCCESS)
621            ret = -EBUSY;
622    } else
623        ret = -ETIMEDOUT;
624out:
625
626    mutex_unlock(&zbdev->mutex);
627    pr_debug("%s end\n", __func__);
628    return ret;
629}
630
631static int
632ieee802154_serial_address(struct ieee802154_dev *dev, u8 addr[IEEE802154_ALEN])
633{
634    struct zb_device *zbdev;
635    int ret = 0;
636
637    pr_debug("%s\n", __func__);
638
639    zbdev = dev->priv;
640    if (NULL == zbdev) {
641        printk(KERN_ERR "%s: wrong phy\n", __func__);
642        return -EINVAL;
643    }
644
645    if (mutex_lock_interruptible(&zbdev->mutex))
646        return -EINTR;
647
648    ret = send_cmd(zbdev, CMD_ADDRESS);
649    if (ret)
650        goto out;
651
652    if (wait_event_interruptible_timeout(zbdev->wq,
653                zbdev->status != STATUS_WAIT,
654                msecs_to_jiffies(1000)) > 0) {
655        memcpy(addr, zbdev->data, sizeof addr);
656        if (zbdev->status != STATUS_SUCCESS)
657            ret = -EBUSY;
658    } else
659        ret = -ETIMEDOUT;
660out:
661
662    mutex_unlock(&zbdev->mutex);
663    pr_debug("%s end\n", __func__);
664    return ret;
665}
666
667static int
668ieee802154_serial_start(struct ieee802154_dev *dev)
669{
670    struct zb_device *zbdev;
671    int ret = 0;
672
673    pr_debug("%s\n", __func__);
674
675    zbdev = dev->priv;
676    if (NULL == zbdev) {
677        printk(KERN_ERR "%s: wrong phy\n", __func__);
678        return -EINVAL;
679    }
680
681    if (mutex_lock_interruptible(&zbdev->mutex))
682        return -EINTR;
683
684    ret = send_cmd2(zbdev, CMD_SET_STATE, RX_MODE);
685    if (ret)
686        goto out;
687
688    if (wait_event_interruptible_timeout(zbdev->wq,
689                zbdev->status != STATUS_WAIT,
690                msecs_to_jiffies(1000)) > 0) {
691        if (zbdev->status != STATUS_SUCCESS)
692            ret = -EBUSY;
693    } else
694        ret = -ETIMEDOUT;
695out:
696    mutex_unlock(&zbdev->mutex);
697    pr_debug("%s end\n", __func__);
698    return ret;
699}
700
701static void
702ieee802154_serial_stop(struct ieee802154_dev *dev)
703{
704    struct zb_device *zbdev;
705    pr_debug("%s\n", __func__);
706
707    zbdev = dev->priv;
708    if (NULL == zbdev) {
709        printk(KERN_ERR "%s: wrong phy\n", __func__);
710        return;
711    }
712
713    if (mutex_lock_interruptible(&zbdev->mutex))
714        return;
715
716
717    if (send_cmd2(zbdev, CMD_SET_STATE, FORCE_TRX_OFF) != 0)
718        goto out;
719
720    wait_event_interruptible_timeout(zbdev->wq,
721                zbdev->status != STATUS_WAIT,
722                msecs_to_jiffies(1000));
723out:
724    mutex_unlock(&zbdev->mutex);
725    pr_debug("%s end\n", __func__);
726}
727
728static int
729ieee802154_serial_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
730{
731    struct zb_device *zbdev;
732    int ret;
733
734    pr_debug("%s\n", __func__);
735
736    zbdev = dev->priv;
737    if (NULL == zbdev) {
738        printk(KERN_ERR "%s: wrong phy\n", __func__);
739        return -EINVAL;
740    }
741
742    if (mutex_lock_interruptible(&zbdev->mutex))
743        return -EINTR;
744
745    ret = send_cmd(zbdev, CMD_CCA);
746    if (ret)
747        goto out;
748
749    if (wait_event_interruptible_timeout(zbdev->wq,
750                zbdev->status != STATUS_WAIT,
751                msecs_to_jiffies(1000)) > 0) {
752        if (zbdev->status != STATUS_SUCCESS) {
753            ret = -EBUSY;
754            goto out;
755        }
756    } else {
757        ret = -ETIMEDOUT;
758        goto out;
759    }
760
761    ret = send_cmd2(zbdev, CMD_SET_STATE, TX_MODE);
762    if (ret)
763        goto out;
764
765    if (wait_event_interruptible_timeout(zbdev->wq,
766                zbdev->status != STATUS_WAIT,
767                msecs_to_jiffies(1000)) > 0) {
768        if (zbdev->status != STATUS_SUCCESS) {
769            ret = -EBUSY;
770            goto out;
771        }
772    } else {
773        ret = -ETIMEDOUT;
774        goto out;
775    }
776
777    ret = send_block(zbdev, skb->len, skb->data);
778    if (ret)
779        goto out;
780
781    if (wait_event_interruptible_timeout(zbdev->wq,
782                zbdev->status != STATUS_WAIT,
783                msecs_to_jiffies(1000)) > 0) {
784        if (zbdev->status != STATUS_SUCCESS) {
785            ret = -EBUSY;
786            goto out;
787        }
788    } else {
789        ret = -ETIMEDOUT;
790        goto out;
791    }
792
793    ret = send_cmd2(zbdev, CMD_SET_STATE, RX_MODE);
794    if (ret)
795        goto out;
796
797    if (wait_event_interruptible_timeout(zbdev->wq,
798                zbdev->status != STATUS_WAIT,
799                msecs_to_jiffies(1000)) > 0) {
800        if (zbdev->status != STATUS_SUCCESS) {
801            ret = -EBUSY;
802            goto out;
803        }
804    } else {
805        ret = -ETIMEDOUT;
806        goto out;
807    }
808
809out:
810
811    mutex_unlock(&zbdev->mutex);
812    pr_debug("%s end\n", __func__);
813    return ret;
814}
815
816/*****************************************************************************
817 * Line discipline interface for IEEE 802.15.4 serial device
818 *****************************************************************************/
819
820static struct ieee802154_ops serial_ops = {
821    .owner = THIS_MODULE,
822    .xmit = ieee802154_serial_xmit,
823    .ed = ieee802154_serial_ed,
824    .set_channel = ieee802154_serial_set_channel,
825    .start = ieee802154_serial_start,
826    .stop = ieee802154_serial_stop,
827    .ieee_addr = ieee802154_serial_address,
828};
829
830/*
831 * Called when a tty is put into ZB line discipline. Called in process context.
832 * Returns 0 on success.
833 */
834static int
835ieee802154_tty_open(struct tty_struct *tty)
836{
837    struct zb_device *zbdev = tty->disc_data;
838    struct ieee802154_dev *dev;
839    int err;
840
841    pr_debug("Openning ldisc\n");
842    if (!capable(CAP_NET_ADMIN))
843        return -EPERM;
844
845    if (tty->disc_data)
846        return -EBUSY;
847
848    dev = ieee802154_alloc_device(sizeof(*zbdev), &serial_ops);
849    if (!dev)
850        return -ENOMEM;
851
852    zbdev = dev->priv;
853    zbdev->dev = dev;
854
855    mutex_init(&zbdev->mutex);
856    init_completion(&zbdev->open_done);
857    init_waitqueue_head(&zbdev->wq);
858
859    dev->extra_tx_headroom = 0;
860    /* only 2.4 GHz band */
861    dev->phy->channels_supported[0] = 0x7fff800;
862
863    dev->flags = IEEE802154_HW_OMIT_CKSUM;
864
865    dev->parent = tty->dev;
866
867    zbdev->tty = tty_kref_get(tty);
868
869    cleanup(zbdev);
870
871    tty->disc_data = zbdev;
872    tty->receive_room = MAX_DATA_SIZE;
873
874    /* FIXME: why is this needed. Note don't use ldisc_ref here as the
875       open path is before the ldisc is referencable */
876
877    if (tty->ldisc->ops->flush_buffer)
878        tty->ldisc->ops->flush_buffer(tty);
879    tty_driver_flush_buffer(tty);
880
881    err = ieee802154_register_device(dev);
882    if (err) {
883        printk(KERN_ERR "%s: device register failed\n", __func__);
884        goto out_free;
885    }
886
887    return 0;
888
889    ieee802154_unregister_device(zbdev->dev);
890
891out_free:
892    tty->disc_data = NULL;
893    tty_kref_put(tty);
894    zbdev->tty = NULL;
895
896    ieee802154_free_device(zbdev->dev);
897
898    return err;
899}
900
901/*
902 * Called when the tty is put into another line discipline or it hangs up. We
903 * have to wait for any cpu currently executing in any of the other zb_tty_*
904 * routines to finish before we can call zb_tty_close and free the
905 * zb_serial_dev struct. This routine must be called from process context, not
906 * interrupt or softirq context.
907 */
908static void
909ieee802154_tty_close(struct tty_struct *tty)
910{
911    struct zb_device *zbdev;
912
913    zbdev = tty->disc_data;
914    if (NULL == zbdev) {
915        printk(KERN_WARNING "%s: match is not found\n", __func__);
916        return;
917    }
918
919    tty->disc_data = NULL;
920    tty_kref_put(tty);
921    zbdev->tty = NULL;
922
923    ieee802154_unregister_device(zbdev->dev);
924
925    tty_ldisc_flush(tty);
926    tty_driver_flush_buffer(tty);
927
928    ieee802154_free_device(zbdev->dev);
929}
930
931/*
932 * Called on tty hangup in process context.
933 */
934static int
935ieee802154_tty_hangup(struct tty_struct *tty)
936{
937    ieee802154_tty_close(tty);
938    return 0;
939}
940
941/*
942 * Called in process context only. May be re-entered
943 * by multiple ioctl calling threads.
944 */
945static int
946ieee802154_tty_ioctl(struct tty_struct *tty, struct file *file,
947        unsigned int cmd, unsigned long arg)
948{
949    struct zb_device *zbdev;
950
951    pr_debug("cmd = 0x%x\n", cmd);
952
953    zbdev = tty->disc_data;
954    if (NULL == zbdev) {
955        pr_debug("match is not found\n");
956        return -EINVAL;
957    }
958
959    switch (cmd) {
960    case TCFLSH:
961        return tty_perform_flush(tty, arg);
962    default:
963        /* Try the mode commands */
964        return tty_mode_ioctl(tty, file, cmd, arg);
965    }
966}
967
968
969/*
970 * This can now be called from hard interrupt level as well
971 * as soft interrupt level or mainline.
972 */
973static void
974ieee802154_tty_receive(struct tty_struct *tty, const unsigned char *buf,
975        char *cflags, int count)
976{
977    struct zb_device *zbdev;
978    int i;
979
980    /* Debug info */
981    printk(KERN_INFO "%s, received %d bytes\n", __func__,
982            count);
983#ifdef DEBUG
984    print_hex_dump_bytes("ieee802154_tty_receive ", DUMP_PREFIX_NONE,
985            buf, count);
986#endif
987
988    /* Actual processing */
989    zbdev = tty->disc_data;
990    if (NULL == zbdev) {
991        printk(KERN_ERR "%s(): record for tty is not found\n",
992                __func__);
993        return;
994    }
995    for (i = 0; i < count; ++i)
996        process_char(zbdev, buf[i]);
997#if 0
998    if (tty->driver->flush_chars)
999        tty->driver->flush_chars(tty);
1000#endif
1001    tty_unthrottle(tty);
1002}
1003
1004/*
1005 * Line discipline device structure
1006 */
1007static struct tty_ldisc_ops ieee802154_ldisc = {
1008    .owner = THIS_MODULE,
1009    .magic = TTY_LDISC_MAGIC,
1010    .name = "ieee802154-ldisc",
1011    .open = ieee802154_tty_open,
1012    .close = ieee802154_tty_close,
1013    .hangup = ieee802154_tty_hangup,
1014    .receive_buf = ieee802154_tty_receive,
1015    .ioctl = ieee802154_tty_ioctl,
1016};
1017
1018/*****************************************************************************
1019 * Module service routinues
1020 *****************************************************************************/
1021
1022static int __init ieee802154_serial_init(void)
1023{
1024    printk(KERN_INFO "Initializing ZigBee TTY interface\n");
1025
1026    if (tty_register_ldisc(N_IEEE802154, &ieee802154_ldisc) != 0) {
1027        printk(KERN_ERR "%s: line discipline register failed\n",
1028                __func__);
1029        return -EINVAL;
1030    }
1031
1032    return 0;
1033}
1034
1035static void __exit ieee802154_serial_cleanup(void)
1036{
1037    if (tty_unregister_ldisc(N_IEEE802154) != 0)
1038        printk(KERN_CRIT
1039            "failed to unregister ZigBee line discipline.\n");
1040}
1041
1042module_init(ieee802154_serial_init);
1043module_exit(ieee802154_serial_cleanup);
1044
1045MODULE_LICENSE("GPL");
1046MODULE_ALIAS_LDISC(N_IEEE802154);
1047
1048

Archive Download this file



interactive