v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / bluetooth / hci_uart.c
1 /* 
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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 version 2 as
9    published by the Free Software Foundation;
10
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
22    SOFTWARE IS DISCLAIMED.
23 */
24
25 /*
26  * BlueZ HCI UART driver.
27  *
28  * $Id: hci_uart.c,v 1.1 2001/06/01 08:12:10 davem Exp $    
29  */
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33
34 #include <linux/version.h>
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/poll.h>
44
45 #include <linux/slab.h>
46 #include <linux/tty.h>
47 #include <linux/errno.h>
48 #include <linux/string.h>
49 #include <linux/signal.h>
50 #include <linux/ioctl.h>
51 #include <linux/skbuff.h>
52
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/bluez.h>
55 #include <net/bluetooth/hci_core.h>
56 #include <net/bluetooth/hci_uart.h>
57
58 #ifndef HCI_UART_DEBUG
59 #undef  DBG
60 #define DBG( A... )
61 #undef  DMP
62 #define DMP( A... )
63 #endif
64
65 /* ------- Interface to HCI layer ------ */
66 /* Initialize device */
67 int n_hci_open(struct hci_dev *hdev)
68 {
69         DBG("%s %p", hdev->name, hdev);
70
71         /* Nothing to do for UART driver */
72
73         hdev->flags |= HCI_RUNNING;
74
75         return 0;
76 }
77
78 /* Reset device */
79 int n_hci_flush(struct hci_dev *hdev)
80 {
81         struct n_hci *n_hci  = (struct n_hci *) hdev->driver_data;
82         struct tty_struct *tty = n_hci->tty;
83
84         DBG("hdev %p tty %p", hdev, tty);
85
86         /* Drop TX queue */
87         bluez_skb_queue_purge(&n_hci->txq);
88
89         /* Flush any pending characters in the driver and discipline. */
90         if (tty->ldisc.flush_buffer)
91                 tty->ldisc.flush_buffer(tty);
92
93         if (tty->driver.flush_buffer)
94                 tty->driver.flush_buffer(tty);
95
96         return 0;
97 }
98
99 /* Close device */
100 int n_hci_close(struct hci_dev *hdev)
101 {
102         DBG("hdev %p", hdev);
103
104         hdev->flags &= ~HCI_RUNNING;
105
106         n_hci_flush(hdev);
107
108         return 0;
109 }
110
111 int n_hci_tx_wakeup(struct n_hci *n_hci)
112 {
113         register struct tty_struct *tty = n_hci->tty;
114
115         if (test_and_set_bit(TRANS_SENDING, &n_hci->tx_state)) {
116                 set_bit(TRANS_WAKEUP, &n_hci->tx_state);
117                 return 0;
118         }
119
120         DBG("");
121         do {
122                 register struct sk_buff *skb;
123                 register int len;
124
125                 clear_bit(TRANS_WAKEUP, &n_hci->tx_state);
126
127                 if (!(skb = skb_dequeue(&n_hci->txq)))
128                         break;
129
130                 DMP(skb->data, skb->len);
131
132                 /* Send frame to TTY driver */
133                 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
134                 len = tty->driver.write(tty, 0, skb->data, skb->len);
135
136                 n_hci->hdev.stat.byte_tx += len;
137
138                 DBG("sent %d", len);
139
140                 if (len == skb->len) {
141                         /* Full frame was sent */
142                         bluez_skb_free(skb);
143                 } else {
144                         /* Subtract sent part and requeue  */
145                         skb_pull(skb, len);
146                         skb_queue_head(&n_hci->txq, skb);
147                 }
148         } while (test_bit(TRANS_WAKEUP, &n_hci->tx_state));
149         clear_bit(TRANS_SENDING, &n_hci->tx_state);
150
151         return 0;
152 }
153
154 /* Send frames from HCI layer */
155 int n_hci_send_frame(struct sk_buff *skb)
156 {
157         struct hci_dev* hdev = (struct hci_dev *) skb->dev;
158         struct tty_struct *tty;
159         struct n_hci *n_hci;
160
161         if (!hdev) {
162                 ERR("Frame for uknown device (hdev=NULL)");
163                 return -ENODEV;
164         }
165
166         if (!(hdev->flags & HCI_RUNNING))
167                 return -EBUSY;
168
169         n_hci = (struct n_hci *) hdev->driver_data;
170         tty = n_hci2tty(n_hci);
171
172         DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len);
173
174         switch (skb->pkt_type) {
175                 case HCI_COMMAND_PKT:
176                         hdev->stat.cmd_tx++;
177                         break;
178
179                 case HCI_ACLDATA_PKT:
180                         hdev->stat.acl_tx++;
181                         break;
182
183                 case HCI_SCODATA_PKT:
184                         hdev->stat.cmd_tx++;
185                         break;
186         };
187
188         /* Prepend skb with frame type and queue */
189         memcpy(skb_push(skb, 1), &skb->pkt_type, 1);
190         skb_queue_tail(&n_hci->txq, skb);
191
192         n_hci_tx_wakeup(n_hci);
193
194         return 0;
195 }
196
197 /* ------ LDISC part ------ */
198
199 /* n_hci_tty_open
200  * 
201  *     Called when line discipline changed to N_HCI.
202  *     
203  * Arguments:    
204  *     tty    pointer to tty info structure
205  * Return Value:    
206  *     0 if success, otherwise error code
207  */
208 static int n_hci_tty_open(struct tty_struct *tty)
209 {
210         struct n_hci *n_hci = tty2n_hci(tty);
211         struct hci_dev *hdev;
212
213         DBG("tty %p", tty);
214
215         if (n_hci)
216                 return -EEXIST;
217
218         if (!(n_hci = kmalloc(sizeof(struct n_hci), GFP_KERNEL))) {
219                 ERR("Can't allocate controll structure");
220                 return -ENFILE;
221         }
222         memset(n_hci, 0, sizeof(struct n_hci));
223
224         /* Initialize and register HCI device */
225         hdev = &n_hci->hdev;
226
227         hdev->type = HCI_UART;
228         hdev->driver_data = n_hci;
229
230         hdev->open  = n_hci_open;
231         hdev->close = n_hci_close;
232         hdev->flush = n_hci_flush;
233         hdev->send  = n_hci_send_frame;
234
235         if (hci_register_dev(hdev) < 0) {
236                 ERR("Can't register HCI device %s", hdev->name);
237                 kfree(n_hci);
238                 return -ENODEV;
239         }
240
241         tty->disc_data = n_hci;
242         n_hci->tty = tty;
243
244         spin_lock_init(&n_hci->rx_lock);
245         n_hci->rx_state = WAIT_PACKET_TYPE;
246
247         skb_queue_head_init(&n_hci->txq);
248
249         MOD_INC_USE_COUNT;
250
251         /* Flush any pending characters in the driver and discipline. */
252         if (tty->ldisc.flush_buffer)
253                 tty->ldisc.flush_buffer(tty);
254
255         if (tty->driver.flush_buffer)
256                 tty->driver.flush_buffer(tty);
257
258         return 0;
259 }
260
261 /* n_hci_tty_close()
262  *
263  *    Called when the line discipline is changed to something
264  *    else, the tty is closed, or the tty detects a hangup.
265  */
266 static void n_hci_tty_close(struct tty_struct *tty)
267 {
268         struct n_hci *n_hci = tty2n_hci(tty);
269         struct hci_dev *hdev = &n_hci->hdev;
270
271         DBG("tty %p hdev %p", tty, hdev);
272
273         if (n_hci != NULL) {
274                 n_hci_close(hdev);
275
276                 if (hci_unregister_dev(hdev) < 0) {
277                         ERR("Can't unregister HCI device %s",hdev->name);
278                 }
279
280                 hdev->driver_data = NULL;
281                 tty->disc_data = NULL;
282                 kfree(n_hci);
283
284                 MOD_DEC_USE_COUNT;
285         }
286 }
287
288 /* n_hci_tty_wakeup()
289  *
290  *    Callback for transmit wakeup. Called when low level
291  *    device driver can accept more send data.
292  *
293  * Arguments:        tty    pointer to associated tty instance data
294  * Return Value:    None
295  */
296 static void n_hci_tty_wakeup( struct tty_struct *tty )
297 {
298         struct n_hci *n_hci = tty2n_hci(tty);
299
300         DBG("");
301
302         if (!n_hci)
303                 return;
304
305         tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
306
307         if (tty != n_hci->tty)
308                 return;
309
310         n_hci_tx_wakeup(n_hci);
311 }
312
313 /* n_hci_tty_room()
314  * 
315  *    Callback function from tty driver. Return the amount of 
316  *    space left in the receiver's buffer to decide if remote
317  *    transmitter is to be throttled.
318  *
319  * Arguments:        tty    pointer to associated tty instance data
320  * Return Value:    number of bytes left in receive buffer
321  */
322 static int n_hci_tty_room (struct tty_struct *tty)
323 {
324         return 65536;
325 }
326
327 static inline int n_hci_check_data_len(struct n_hci *n_hci, int len)
328 {
329         register int room = skb_tailroom(n_hci->rx_skb);
330
331         DBG("len %d room %d", len, room);
332         if (!len) {
333                 DMP(n_hci->rx_skb->data, n_hci->rx_skb->len);
334                 hci_recv_frame(n_hci->rx_skb);
335         } else if (len > room) {
336                 ERR("Data length is to large");
337                 bluez_skb_free(n_hci->rx_skb);
338                 n_hci->hdev.stat.err_rx++;
339         } else {
340                 n_hci->rx_state = WAIT_DATA;
341                 n_hci->rx_count = len;
342                 return len;
343         }
344
345         n_hci->rx_state = WAIT_PACKET_TYPE;
346         n_hci->rx_skb   = NULL;
347         n_hci->rx_count = 0;
348         return 0;
349 }
350
351 static inline void n_hci_rx(struct n_hci *n_hci, const __u8 * data, char *flags, int count)
352 {
353         register const char *ptr;
354         hci_event_hdr *eh;
355         hci_acl_hdr   *ah;
356         hci_sco_hdr   *sh;
357         register int len, type, dlen;
358
359         DBG("count %d state %d rx_count %d", count, n_hci->rx_state, n_hci->rx_count);
360
361         n_hci->hdev.stat.byte_rx += count;
362
363         ptr = data;
364         while (count) {
365                 if (n_hci->rx_count) {
366                         len = MIN(n_hci->rx_count, count);
367                         memcpy(skb_put(n_hci->rx_skb, len), ptr, len);
368                         n_hci->rx_count -= len; count -= len; ptr += len;
369
370                         if (n_hci->rx_count)
371                                 continue;
372
373                         switch (n_hci->rx_state) {
374                                 case WAIT_DATA:
375                                         DBG("Complete data");
376
377                                         DMP(n_hci->rx_skb->data, n_hci->rx_skb->len);
378
379                                         hci_recv_frame(n_hci->rx_skb);
380
381                                         n_hci->rx_state = WAIT_PACKET_TYPE;
382                                         n_hci->rx_skb = NULL;
383                                         continue;
384
385                                 case WAIT_EVENT_HDR:
386                                         eh = (hci_event_hdr *) n_hci->rx_skb->data;
387
388                                         DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
389
390                                         n_hci_check_data_len(n_hci, eh->plen);
391                                         continue;
392
393                                 case WAIT_ACL_HDR:
394                                         ah = (hci_acl_hdr *) n_hci->rx_skb->data;
395                                         dlen = __le16_to_cpu(ah->dlen);
396
397                                         DBG("ACL header: dlen %d", dlen);
398
399                                         n_hci_check_data_len(n_hci, dlen);
400                                         continue;
401
402                                 case WAIT_SCO_HDR:
403                                         sh = (hci_sco_hdr *) n_hci->rx_skb->data;
404
405                                         DBG("SCO header: dlen %d", sh->dlen);
406
407                                         n_hci_check_data_len(n_hci, sh->dlen);
408                                         continue;
409                         };
410                 }
411
412                 /* WAIT_PACKET_TYPE */
413                 switch (*ptr) {
414                         case HCI_EVENT_PKT:
415                                 DBG("Event packet");
416                                 n_hci->rx_state = WAIT_EVENT_HDR;
417                                 n_hci->rx_count = HCI_EVENT_HDR_SIZE;
418                                 type = HCI_EVENT_PKT;
419                                 break;
420
421                         case HCI_ACLDATA_PKT:
422                                 DBG("ACL packet");
423                                 n_hci->rx_state = WAIT_ACL_HDR;
424                                 n_hci->rx_count = HCI_ACL_HDR_SIZE;
425                                 type = HCI_ACLDATA_PKT;
426                                 break;
427
428                         case HCI_SCODATA_PKT:
429                                 DBG("SCO packet");
430                                 n_hci->rx_state = WAIT_SCO_HDR;
431                                 n_hci->rx_count = HCI_SCO_HDR_SIZE;
432                                 type = HCI_SCODATA_PKT;
433                                 break;
434
435                         default:
436                                 ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
437                                 n_hci->hdev.stat.err_rx++;
438                                 ptr++; count--;
439                                 continue;
440                 };
441                 ptr++; count--;
442
443                 /* Allocate packet */
444                 if (!(n_hci->rx_skb = bluez_skb_alloc(HCI_MAX_READ, GFP_ATOMIC))) {
445                         ERR("Can't allocate mem for new packet");
446
447                         n_hci->rx_state = WAIT_PACKET_TYPE;
448                         n_hci->rx_count = 0;
449                         return;
450                 }
451                 n_hci->rx_skb->dev = (void *) &n_hci->hdev;
452                 n_hci->rx_skb->pkt_type = type;
453         }
454 }
455
456 /* n_hci_tty_receive()
457  * 
458  *     Called by tty low level driver when receive data is
459  *     available.
460  *     
461  * Arguments:  tty          pointer to tty isntance data
462  *             data         pointer to received data
463  *             flags        pointer to flags for data
464  *             count        count of received data in bytes
465  *     
466  * Return Value:    None
467  */
468 static void n_hci_tty_receive(struct tty_struct *tty, const __u8 * data, char *flags, int count)
469 {
470         struct n_hci *n_hci = tty2n_hci(tty);
471
472         if (!n_hci || tty != n_hci->tty)
473                 return;
474
475         spin_lock(&n_hci->rx_lock);
476         n_hci_rx(n_hci, data, flags, count);
477         spin_unlock(&n_hci->rx_lock);
478
479         if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver.unthrottle)
480                 tty->driver.unthrottle(tty);
481 }
482
483 /* n_hci_tty_ioctl()
484  *
485  *    Process IOCTL system call for the tty device.
486  *
487  * Arguments:
488  *
489  *    tty        pointer to tty instance data
490  *    file       pointer to open file object for device
491  *    cmd        IOCTL command code
492  *    arg        argument for IOCTL call (cmd dependent)
493  *
494  * Return Value:    Command dependent
495  */
496 static int n_hci_tty_ioctl (struct tty_struct *tty, struct file * file,
497                             unsigned int cmd, unsigned long arg)
498 {
499         struct n_hci *n_hci = tty2n_hci(tty);
500         int error = 0;
501
502         DBG("");
503
504         /* Verify the status of the device */
505         if (!n_hci)
506                 return -EBADF;
507
508         switch (cmd) {
509                 default:
510                         error = n_tty_ioctl(tty, file, cmd, arg);
511                         break;
512         };
513
514         return error;
515 }
516
517 /*
518  * We don't provide read/write/poll interface for user space.
519  */
520 static ssize_t n_hci_tty_read(struct tty_struct *tty, struct file *file, unsigned char *buf, size_t nr)
521 {
522         return 0;
523 }
524 static ssize_t n_hci_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
525 {
526         return 0;
527 }
528 static unsigned int n_hci_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
529 {
530         return 0;
531 }
532
533 int __init n_hci_init(void)
534 {
535         static struct tty_ldisc n_hci_ldisc;
536         int err;
537
538         INF("BlueZ HCI UART driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", 
539                 BLUEZ_VER);
540         INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
541
542         /* Register the tty discipline */
543
544         memset(&n_hci_ldisc, 0, sizeof (n_hci_ldisc));
545         n_hci_ldisc.magic       = TTY_LDISC_MAGIC;
546         n_hci_ldisc.name        = "n_hci";
547         n_hci_ldisc.open        = n_hci_tty_open;
548         n_hci_ldisc.close       = n_hci_tty_close;
549         n_hci_ldisc.read        = n_hci_tty_read;
550         n_hci_ldisc.write       = n_hci_tty_write;
551         n_hci_ldisc.ioctl       = n_hci_tty_ioctl;
552         n_hci_ldisc.poll        = n_hci_tty_poll;
553         n_hci_ldisc.receive_room= n_hci_tty_room;
554         n_hci_ldisc.receive_buf = n_hci_tty_receive;
555         n_hci_ldisc.write_wakeup= n_hci_tty_wakeup;
556
557         if ((err = tty_register_ldisc(N_HCI, &n_hci_ldisc))) {
558                 ERR("Can't register HCI line discipline (%d)", err);
559                 return err;
560         }
561
562         return 0;
563 }
564
565 void n_hci_cleanup(void)
566 {
567         int err;
568
569         /* Release tty registration of line discipline */
570         if ((err = tty_register_ldisc(N_HCI, NULL)))
571                 ERR("Can't unregister HCI line discipline (%d)", err);
572 }
573
574 module_init(n_hci_init);
575 module_exit(n_hci_cleanup);