]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/bluetooth/brf6150.c
Merge branch 'omap-fixes'
[linux-2.6-omap-h63xx.git] / drivers / bluetooth / brf6150.c
1 /*
2  *  linux/drivers/bluetooth/brf6150/brf6150.c
3  *
4  *  Copyright (C) 2005 Nokia Corporation
5  *  Written by Ville Tervo <ville.tervo@nokia.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 as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version. 
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/delay.h>
28 #include <linux/spinlock.h>
29 #include <linux/serial_reg.h>
30 #include <linux/skbuff.h>
31 #include <linux/firmware.h>
32 #include <linux/irq.h>
33 #include <linux/timer.h>
34 #include <linux/clk.h>
35 #include <linux/platform_device.h>
36 #include <linux/gpio.h>
37
38 #include <mach/hardware.h>
39 #include <mach/board.h>
40 #include <mach/irqs.h>
41 #include <mach/board-nokia.h>
42
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/hci_core.h>
45 #include <net/bluetooth/hci.h>
46
47 #include "brf6150.h"
48
49 #if 0
50 #define NBT_DBG(fmt, arg...)  printk("%s: " fmt "" , __FUNCTION__ , ## arg)
51 #else
52 #define NBT_DBG(...)
53 #endif
54
55 #if 0
56 #define NBT_DBG_FW(fmt, arg...)  printk("%s: " fmt "" , __FUNCTION__ , ## arg)
57 #else
58 #define NBT_DBG_FW(...)
59 #endif
60
61 #if 0
62 #define NBT_DBG_POWER(fmt, arg...)  printk("%s: " fmt "" , __FUNCTION__ , ## arg)
63 #else
64 #define NBT_DBG_POWER(...)
65 #endif
66
67 #if 0
68 #define NBT_DBG_TRANSFER(fmt, arg...)  printk("%s: " fmt "" , __FUNCTION__ , ## arg)
69 #else
70 #define NBT_DBG_TRANSFER(...)
71 #endif
72
73 #if 0
74 #define NBT_DBG_TRANSFER_NF(fmt, arg...)  printk(fmt "" , ## arg)
75 #else
76 #define NBT_DBG_TRANSFER_NF(...)
77 #endif
78
79 #define PM_TIMEOUT (2000)
80
81 static void brf6150_device_release(struct device *dev);
82 static struct brf6150_info *exit_info;
83
84 static struct platform_device brf6150_device = {
85         .name           = BT_DEVICE,
86         .id             = -1,
87         .num_resources  = 0,
88         .dev = {
89                 .release = brf6150_device_release,
90         }
91 };
92
93 static struct device_driver brf6150_driver = {
94         .name           = BT_DRIVER,
95         .bus            = &platform_bus_type,
96 };
97
98 static inline void brf6150_outb(struct brf6150_info *info, unsigned int offset, u8 val)
99 {
100         outb(val, info->uart_base + (offset << 2));
101 }
102
103 static inline u8 brf6150_inb(struct brf6150_info *info, unsigned int offset)
104 {
105         return inb(info->uart_base + (offset << 2));
106 }
107
108 static void brf6150_set_rts(struct brf6150_info *info, int active)
109 {
110         u8 b;
111
112         b = brf6150_inb(info, UART_MCR);
113         if (active)
114                 b |= UART_MCR_RTS;
115         else
116                 b &= ~UART_MCR_RTS;
117         brf6150_outb(info, UART_MCR, b);
118 }
119
120 static void brf6150_wait_for_cts(struct brf6150_info *info, int active,
121                                  int timeout_ms)
122 {
123         int okay;
124         unsigned long timeout;
125
126         okay = 0;
127         timeout = jiffies + msecs_to_jiffies(timeout_ms);
128         for (;;) {
129                 int state;
130
131                 state = brf6150_inb(info, UART_MSR) & UART_MSR_CTS;
132                 if (active) {
133                         if (state)
134                                 break;
135                 } else {
136                         if (!state)
137                                 break;
138                 }
139                 if (jiffies > timeout)
140                         break;
141         }
142 }
143
144 static inline void brf6150_set_auto_ctsrts(struct brf6150_info *info, int on)
145 {
146         u8 lcr, b;
147
148         lcr = brf6150_inb(info, UART_LCR);
149         brf6150_outb(info, UART_LCR, 0xbf);
150         b = brf6150_inb(info, UART_EFR);
151         if (on)
152                 b |= UART_EFR_CTS | UART_EFR_RTS;
153         else
154                 b &= ~(UART_EFR_CTS | UART_EFR_RTS);
155         brf6150_outb(info, UART_EFR, b);
156         brf6150_outb(info, UART_LCR, lcr);
157 }
158
159 static inline void brf6150_enable_pm_rx(struct brf6150_info *info)
160 {
161         if (info->pm_enabled) {
162                 info->rx_pm_enabled = 1;
163         }
164 }
165
166 static inline void brf6150_disable_pm_rx(struct brf6150_info *info)
167 {
168         if (info->pm_enabled) {
169                 info->rx_pm_enabled = 0;
170         }
171 }
172
173 static void brf6150_enable_pm_tx(struct brf6150_info *info)
174 {
175         if (info->pm_enabled) {
176                 mod_timer(&info->pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
177                 info->tx_pm_enabled = 1;
178         }
179 }
180
181 static void brf6150_disable_pm_tx(struct brf6150_info *info)
182 {
183         if (info->pm_enabled) {
184                 info->tx_pm_enabled = 0;
185                 gpio_set_value(info->btinfo->bt_wakeup_gpio, 1);
186         }
187         if (gpio_get_value(info->btinfo->host_wakeup_gpio))
188                 tasklet_schedule(&info->tx_task);
189 }
190
191 static void brf6150_pm_timer(unsigned long data)
192 {
193         struct brf6150_info *info;
194
195         info = (struct brf6150_info *)data;
196         if (info->tx_pm_enabled && info->rx_pm_enabled && !test_bit(HCI_INQUIRY, &info->hdev->flags))
197                 gpio_set_value(info->btinfo->bt_wakeup_gpio, 0);
198         else
199                 mod_timer(&info->pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
200 }
201
202 static int brf6150_change_speed(struct brf6150_info *info, unsigned long speed)
203 {
204         unsigned int divisor;
205         u8 lcr, mdr1;
206
207         NBT_DBG("Setting speed %lu\n", speed);
208
209         if (speed >= 460800) {
210                 divisor = UART_CLOCK / 13 / speed;
211                 mdr1 = 3;
212         } else {
213                 divisor = UART_CLOCK / 16 / speed;
214                 mdr1 = 0;
215         }
216
217         brf6150_outb(info, UART_OMAP_MDR1, 7); /* Make sure UART mode is disabled */
218         lcr = brf6150_inb(info, UART_LCR);
219         brf6150_outb(info, UART_LCR, UART_LCR_DLAB);     /* Set DLAB */
220         brf6150_outb(info, UART_DLL, divisor & 0xff);    /* Set speed */
221         brf6150_outb(info, UART_DLM, divisor >> 8);
222         brf6150_outb(info, UART_LCR, lcr);
223         brf6150_outb(info, UART_OMAP_MDR1, mdr1); /* Make sure UART mode is enabled */
224
225         return 0;
226 }
227
228 /* Firmware handling */
229 static int brf6150_open_firmware(struct brf6150_info *info)
230 {
231         int err;
232
233         info->fw_pos = 0;
234         err = request_firmware(&info->fw_entry, "brf6150fw.bin", &brf6150_device.dev);
235
236         return err;
237 }
238
239 static struct sk_buff *brf6150_read_fw_cmd(struct brf6150_info *info, int how)
240 {
241         struct sk_buff *skb;
242         unsigned int cmd_len;
243
244         if (info->fw_pos >= info->fw_entry->size) {
245                 return NULL;
246         }
247
248         cmd_len = info->fw_entry->data[info->fw_pos++];
249         if (!cmd_len)
250                 return NULL;
251
252         if (info->fw_pos + cmd_len > info->fw_entry->size) {
253                 printk(KERN_WARNING "Corrupted firmware image\n");
254                 return NULL;
255         }
256
257         skb = bt_skb_alloc(cmd_len, how);
258         if (!skb) {
259                 printk(KERN_WARNING "Cannot reserve memory for buffer\n");
260                 return NULL;
261         }
262         memcpy(skb_put(skb, cmd_len), &info->fw_entry->data[info->fw_pos], cmd_len);
263
264         info->fw_pos += cmd_len;
265
266         return skb;
267 }
268
269 static int brf6150_close_firmware(struct brf6150_info *info)
270 {
271         release_firmware(info->fw_entry);
272         return 0;
273 }
274
275 static int brf6150_send_alive_packet(struct brf6150_info *info)
276 {
277         struct sk_buff *skb;
278
279         NBT_DBG("Sending alive packet\n");
280         skb = brf6150_read_fw_cmd(info, GFP_ATOMIC);
281         if (!skb) {
282                 printk(KERN_WARNING "Cannot read alive command");
283                 return -1;
284         }
285
286         clk_enable(info->uart_ck);
287         skb_queue_tail(&info->txq, skb);
288         tasklet_schedule(&info->tx_task);
289
290         NBT_DBG("Alive packet sent\n");
291         return 0;
292 }
293
294 static void brf6150_alive_packet(struct brf6150_info *info, struct sk_buff *skb)
295 {
296         NBT_DBG("Received alive packet\n");
297         if (skb->data[1] == 0xCC) {
298                 complete(&info->init_completion);
299         }
300
301         kfree_skb(skb);
302 }
303
304 static int brf6150_send_negotiation(struct brf6150_info *info)
305 {
306         struct sk_buff *skb;
307         NBT_DBG("Sending negotiation..\n");
308
309         brf6150_change_speed(info, INIT_SPEED);
310
311         skb = brf6150_read_fw_cmd(info, GFP_KERNEL);
312
313         if (!skb) {
314                 printk(KERN_WARNING "Cannot read negoatiation message");
315                 return -1;
316         }
317
318         clk_enable(info->uart_ck);
319         skb_queue_tail(&info->txq, skb);
320         tasklet_schedule(&info->tx_task);
321
322
323         NBT_DBG("Negotiation sent\n");
324         return 0;
325 }
326
327 static void brf6150_negotiation_packet(struct brf6150_info *info,
328                                        struct sk_buff *skb)
329 {
330         if (skb->data[1] == 0x20) {
331                 /* Change to operational settings */
332                 brf6150_set_rts(info, 0);
333                 brf6150_wait_for_cts(info, 0, 100);
334                 brf6150_change_speed(info, MAX_BAUD_RATE);
335                 brf6150_set_rts(info, 1);
336                 brf6150_wait_for_cts(info, 1, 100);
337                 brf6150_set_auto_ctsrts(info, 1);
338                 brf6150_send_alive_packet(info);
339         } else {
340                 printk(KERN_WARNING "Could not negotiate brf6150 settings\n");
341         }
342         kfree_skb(skb);
343 }
344
345 static int brf6150_get_hdr_len(u8 pkt_type)
346 {
347         long retval;
348
349         switch (pkt_type) {
350         case H4_EVT_PKT:
351                 retval = HCI_EVENT_HDR_SIZE;
352                 break;
353         case H4_ACL_PKT:
354                 retval = HCI_ACL_HDR_SIZE;
355                 break;
356         case H4_SCO_PKT:
357                 retval = HCI_SCO_HDR_SIZE;
358                 break;
359         case H4_NEG_PKT:
360                 retval = 9;
361                 break;
362         case H4_ALIVE_PKT:
363                 retval = 3;
364                 break;
365         default:
366                 printk(KERN_ERR "brf6150: Unknown H4 packet");
367                 retval = -1;
368                 break;
369         }
370
371         return retval;
372 }
373
374 static unsigned int brf6150_get_data_len(struct brf6150_info *info,
375                                          struct sk_buff *skb)
376 {
377         long retval = -1;
378         struct hci_event_hdr *evt_hdr;
379         struct hci_acl_hdr *acl_hdr;
380         struct hci_sco_hdr *sco_hdr;
381
382         switch (bt_cb(skb)->pkt_type) {
383         case H4_EVT_PKT:
384                 evt_hdr = (struct hci_event_hdr *)skb->data;
385                 retval = evt_hdr->plen;
386                 break;
387         case H4_ACL_PKT:
388                 acl_hdr = (struct hci_acl_hdr *)skb->data;
389                 retval = le16_to_cpu(acl_hdr->dlen);
390                 break;
391         case H4_SCO_PKT:
392                 sco_hdr = (struct hci_sco_hdr *)skb->data;
393                 retval = sco_hdr->dlen;
394                 break;
395         case H4_NEG_PKT:
396                 retval = 0;
397                 break;
398         case H4_ALIVE_PKT:
399                 retval = 0;
400                 break;
401         }
402
403         return retval;
404 }
405
406 static void brf6150_parse_fw_event(struct brf6150_info *info)
407 {
408         struct hci_fw_event *ev;
409
410         if (bt_cb(info->rx_skb)->pkt_type != H4_EVT_PKT) {
411                 printk(KERN_WARNING "Got non event fw packet.\n");
412                 info->fw_error = 1;
413                 return;
414         }
415
416         ev = (struct hci_fw_event *)info->rx_skb->data;
417         if (ev->hev.evt != HCI_EV_CMD_COMPLETE) {
418                 printk(KERN_WARNING "Got non cmd complete fw event\n");
419                 info->fw_error = 1;
420                 return;
421         }
422
423         if (ev->status != 0) {
424                 printk(KERN_WARNING "Got error status from fw command\n");
425                 info->fw_error = 1;
426                 return;
427         }
428
429         complete(&info->fw_completion);
430 }
431
432 static inline void brf6150_recv_frame(struct brf6150_info *info,
433                                       struct sk_buff *skb)
434 {
435         if (unlikely(!test_bit(HCI_RUNNING, &info->hdev->flags))) {
436                 NBT_DBG("fw_event\n");
437                 brf6150_parse_fw_event(info);
438                 kfree_skb(skb);
439         } else {
440                 hci_recv_frame(skb);
441                 if (!(brf6150_inb(info, UART_LSR) & UART_LSR_DR))
442                         brf6150_enable_pm_rx(info);
443                 NBT_DBG("Frame sent to upper layer\n");
444         }
445
446 }
447
448 static inline void brf6150_rx(struct brf6150_info *info)
449 {
450         u8 byte;
451
452         NBT_DBG_TRANSFER("rx_tasklet woke up\ndata ");
453
454         while (brf6150_inb(info, UART_LSR) & UART_LSR_DR) {
455                 if (info->rx_skb == NULL) {
456                         info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
457                         if (!info->rx_skb) {
458                                 printk(KERN_WARNING "brf6150: Can't allocate memory for new packet\n");
459                                 return;
460                         }
461                         info->rx_state = WAIT_FOR_PKT_TYPE;
462                         info->rx_skb->dev = (void *)info->hdev;
463                         brf6150_disable_pm_rx(info);
464                         clk_enable(info->uart_ck);
465                 }
466
467                 byte = brf6150_inb(info, UART_RX);
468                 if (info->garbage_bytes) {
469                         info->garbage_bytes--;
470                         info->hdev->stat.err_rx++;
471                         continue;
472                 }
473                 info->hdev->stat.byte_rx++;
474                 NBT_DBG_TRANSFER_NF("0x%.2x  ", byte);
475                 switch (info->rx_state) {
476                 case WAIT_FOR_PKT_TYPE:
477                         bt_cb(info->rx_skb)->pkt_type = byte;
478                         info->rx_count = brf6150_get_hdr_len(byte);
479                         if (info->rx_count >= 0) {
480                                 info->rx_state = WAIT_FOR_HEADER;
481                         } else {
482                                 info->hdev->stat.err_rx++;
483                                 kfree_skb(info->rx_skb);
484                                 info->rx_skb = NULL;
485                                 clk_disable(info->uart_ck);
486                         }
487                         break;
488                 case WAIT_FOR_HEADER:
489                         info->rx_count--;
490                         *skb_put(info->rx_skb, 1) = byte;
491                         if (info->rx_count == 0) {
492                                 info->rx_count = brf6150_get_data_len(info, info->rx_skb);
493                                 if (info->rx_count > skb_tailroom(info->rx_skb)) {
494                                         printk(KERN_WARNING "brf6150: Frame is %ld bytes too long.\n",
495                                                info->rx_count - skb_tailroom(info->rx_skb));
496                                         info->rx_skb = NULL;
497                                         info->garbage_bytes = info->rx_count - skb_tailroom(info->rx_skb);
498                                         clk_disable(info->uart_ck);
499                                         break;
500                                 }
501                                 info->rx_state = WAIT_FOR_DATA;
502                                 if (bt_cb(info->rx_skb)->pkt_type == H4_NEG_PKT) {
503                                         brf6150_negotiation_packet(info, info->rx_skb);
504                                         info->rx_skb = NULL;
505                                         clk_disable(info->uart_ck);
506                                         return;
507                                 }
508                                 if (bt_cb(info->rx_skb)->pkt_type == H4_ALIVE_PKT) {
509                                         brf6150_alive_packet(info, info->rx_skb);
510                                         info->rx_skb = NULL;
511                                         clk_disable(info->uart_ck);
512                                         return;
513                                 }
514                         }
515                         break;
516                 case WAIT_FOR_DATA:
517                         info->rx_count--;
518                         *skb_put(info->rx_skb, 1) = byte;
519                         if (info->rx_count == 0) {
520                                 brf6150_recv_frame(info, info->rx_skb);
521                                 info->rx_skb = NULL;
522                                 clk_disable(info->uart_ck);
523                         }
524                         break;
525                 default:
526                         WARN_ON(1);
527                         break;
528                 }
529         }
530
531         NBT_DBG_TRANSFER_NF("\n");
532 }
533
534 static void brf6150_tx_tasklet(unsigned long data)
535 {
536         unsigned int sent = 0;
537         unsigned long flags;
538         struct sk_buff *skb;
539         struct brf6150_info *info = (struct brf6150_info *)data;
540
541         NBT_DBG_TRANSFER("tx_tasklet woke up\n data ");
542
543         skb = skb_dequeue(&info->txq);
544         if (!skb) {
545                 /* No data in buffer */
546                 brf6150_enable_pm_tx(info);
547                 return;
548         }
549
550         /* Copy data to tx fifo */
551         while (!(brf6150_inb(info, UART_OMAP_SSR) & UART_OMAP_SSR_TXFULL) &&
552                (sent < skb->len)) {
553                 NBT_DBG_TRANSFER_NF("0x%.2x ", skb->data[sent]);
554                 brf6150_outb(info, UART_TX, skb->data[sent]);
555                 sent++;
556         }
557
558         info->hdev->stat.byte_tx += sent;
559         NBT_DBG_TRANSFER_NF("\n");
560         if (skb->len == sent) {
561                 kfree_skb(skb);
562                 clk_disable(info->uart_ck);
563         } else {
564                 skb_pull(skb, sent);
565                 skb_queue_head(&info->txq, skb);
566         }
567
568         spin_lock_irqsave(&info->lock, flags);
569         brf6150_outb(info, UART_IER, brf6150_inb(info, UART_IER) | UART_IER_THRI);
570         spin_unlock_irqrestore(&info->lock, flags);
571 }
572
573 static irqreturn_t brf6150_interrupt(int irq, void *data)
574 {
575         struct brf6150_info *info = (struct brf6150_info *)data;
576         u8 iir, msr;
577         int ret;
578         unsigned long flags;
579
580         ret = IRQ_NONE;
581
582         clk_enable(info->uart_ck);
583         iir = brf6150_inb(info, UART_IIR);
584         if (iir & UART_IIR_NO_INT) {
585                 printk("Interrupt but no reason irq 0x%.2x\n", iir);
586                 clk_disable(info->uart_ck);
587                 return IRQ_HANDLED;
588         }
589
590         NBT_DBG("In interrupt handler iir 0x%.2x\n", iir);
591
592         iir &= UART_IIR_ID;
593
594         if (iir == UART_IIR_MSI) {
595                 msr = brf6150_inb(info, UART_MSR);
596                 ret = IRQ_HANDLED;
597         }
598         if (iir == UART_IIR_RLSI) {
599                 brf6150_inb(info, UART_RX);
600                 brf6150_inb(info, UART_LSR);
601                 ret = IRQ_HANDLED;
602         }
603
604         if (iir == UART_IIR_RDI) {
605                 brf6150_rx(info);
606                 ret = IRQ_HANDLED;
607         }
608
609         if (iir == UART_IIR_THRI) {
610                 spin_lock_irqsave(&info->lock, flags);
611                 brf6150_outb(info, UART_IER, brf6150_inb(info, UART_IER) & ~UART_IER_THRI);
612                 spin_unlock_irqrestore(&info->lock, flags);
613                 tasklet_schedule(&info->tx_task);
614                 ret = IRQ_HANDLED;
615         }
616
617         clk_disable(info->uart_ck);
618         return ret;
619 }
620
621 static irqreturn_t brf6150_wakeup_interrupt(int irq, void *dev_inst)
622 {
623         struct brf6150_info *info = dev_inst;
624         int should_wakeup;
625         unsigned long flags;
626
627         spin_lock_irqsave(&info->lock, flags);
628         should_wakeup = gpio_get_value(info->btinfo->host_wakeup_gpio);
629         NBT_DBG_POWER("gpio interrupt %d\n", should_wakeup);
630         if (should_wakeup) {
631                 clk_enable(info->uart_ck);
632                 brf6150_set_auto_ctsrts(info, 1);
633                 brf6150_rx(info);
634                 tasklet_schedule(&info->tx_task);
635         } else {
636                 brf6150_set_auto_ctsrts(info, 0);
637                 brf6150_set_rts(info, 0);
638                 clk_disable(info->uart_ck);
639         }
640
641         spin_unlock_irqrestore(&info->lock, flags);
642         return IRQ_HANDLED;
643 }
644
645 static int brf6150_init_uart(struct brf6150_info *info)
646 {
647         int count = 0;
648
649         /* Reset the  UART */
650         brf6150_outb(info, UART_OMAP_SYSC, UART_SYSC_OMAP_RESET);
651         while (!(brf6150_inb(info, UART_OMAP_SYSS) & UART_SYSS_RESETDONE)) {
652                 if (count++ > 100) {
653                         printk(KERN_ERR "brf6150: UART reset timeout\n");
654                         return -1;
655                 }
656                 udelay(1);
657         }
658
659         /* Enable and setup FIFO */
660         brf6150_outb(info, UART_LCR, UART_LCR_WLEN8);
661         brf6150_outb(info, UART_OMAP_MDR1, 0x00); /* Make sure UART mode is enabled */
662         brf6150_outb(info, UART_OMAP_SCR, 0x00);
663         brf6150_outb(info, UART_EFR, brf6150_inb(info, UART_EFR) | UART_EFR_ECB);
664         brf6150_outb(info, UART_MCR, brf6150_inb(info, UART_MCR) | UART_MCR_TCRTLR);
665         brf6150_outb(info, UART_TI752_TLR, 0xff);
666         brf6150_outb(info, UART_TI752_TCR, 0x1f);
667         brf6150_outb(info, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
668         brf6150_outb(info, UART_IER, UART_IER_RDI);
669
670         return 0;
671 }
672
673 static int brf6150_reset(struct brf6150_info *info)
674 {
675         gpio_set_value(info->btinfo->bt_wakeup_gpio, 0);
676         gpio_set_value(info->btinfo->reset_gpio, 0);
677         current->state = TASK_UNINTERRUPTIBLE;
678         schedule_timeout(msecs_to_jiffies(10));
679         gpio_set_value(info->btinfo->bt_wakeup_gpio, 1);
680         current->state = TASK_UNINTERRUPTIBLE;
681         schedule_timeout(msecs_to_jiffies(100));
682         gpio_set_value(info->btinfo->reset_gpio, 1);
683         current->state = TASK_UNINTERRUPTIBLE;
684         schedule_timeout(msecs_to_jiffies(100));
685
686         return 0;
687 }
688
689 static int brf6150_send_firmware(struct brf6150_info *info)
690 {
691         struct sk_buff *skb;
692
693         init_completion(&info->fw_completion);
694         info->fw_error = 0;
695
696         while ((skb = brf6150_read_fw_cmd(info, GFP_KERNEL)) != NULL) {
697                 clk_enable(info->uart_ck);
698                 skb_queue_tail(&info->txq, skb);
699                 tasklet_schedule(&info->tx_task);
700
701                 if (!wait_for_completion_timeout(&info->fw_completion, HZ)) {
702                         return -1;
703                 }
704
705                 if (info->fw_error) {
706                         return -1;
707                 }
708         }
709         NBT_DBG_FW("Firmware sent\n");
710
711         return 0;
712
713 }
714
715 /* hci callback functions */
716 static int brf6150_hci_flush(struct hci_dev *hdev)
717 {
718         struct brf6150_info *info;
719         info = hdev->driver_data;
720
721         skb_queue_purge(&info->txq);
722
723         return 0;
724 }
725
726 static int brf6150_hci_open(struct hci_dev *hdev)
727 {
728         struct brf6150_info *info;
729         int err;
730
731         info = hdev->driver_data;
732
733         if (test_bit(HCI_RUNNING, &hdev->flags))
734                 return 0;
735
736         if (brf6150_open_firmware(info) < 0) {
737                 printk("Cannot open firmware\n");
738                 return -1;
739         }
740
741         info->rx_state = WAIT_FOR_PKT_TYPE;
742         info->rx_count = 0;
743         info->garbage_bytes = 0;
744         info->rx_skb = NULL;
745         info->pm_enabled = 0;
746         set_irq_type(gpio_to_irq(info->btinfo->host_wakeup_gpio), IRQ_TYPE_NONE);
747         init_completion(&info->fw_completion);
748
749         clk_enable(info->uart_ck);
750
751         brf6150_init_uart(info);
752         brf6150_set_auto_ctsrts(info, 0);
753         brf6150_set_rts(info, 0);
754         brf6150_reset(info);
755         brf6150_wait_for_cts(info, 1, 10);
756         brf6150_set_rts(info, 1);
757         if (brf6150_send_negotiation(info)) {
758                 brf6150_close_firmware(info);
759                 return -1;
760         }
761
762         if (!wait_for_completion_interruptible_timeout(&info->init_completion, HZ)) {
763                 brf6150_close_firmware(info);
764                 clk_disable(info->uart_ck);
765                 clear_bit(HCI_RUNNING, &hdev->flags);
766                 return -1;
767         }
768         brf6150_set_auto_ctsrts(info, 1);
769
770         err = brf6150_send_firmware(info);
771         brf6150_close_firmware(info);
772         if (err < 0)
773                 printk(KERN_ERR "brf6150: Sending firmware failed. Bluetooth won't work properly\n");
774
775         set_irq_type(gpio_to_irq(info->btinfo->host_wakeup_gpio), IRQ_TYPE_EDGE_BOTH);
776         info->pm_enabled = 1;
777         set_bit(HCI_RUNNING, &hdev->flags);
778         return 0;
779 }
780
781 static int brf6150_hci_close(struct hci_dev *hdev)
782 {
783         struct brf6150_info *info = hdev->driver_data;
784         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
785                 return 0;
786
787         brf6150_hci_flush(hdev);
788         clk_disable(info->uart_ck);
789         del_timer_sync(&info->pm_timer);
790         gpio_set_value(info->btinfo->bt_wakeup_gpio, 0);
791         set_irq_type(gpio_to_irq(info->btinfo->host_wakeup_gpio), IRQ_TYPE_NONE);
792
793         return 0;
794 }
795
796 static void brf6150_hci_destruct(struct hci_dev *hdev)
797 {
798 }
799
800 static int brf6150_hci_send_frame(struct sk_buff *skb)
801 {
802         struct brf6150_info *info;
803         struct hci_dev *hdev = (struct hci_dev *)skb->dev;
804
805         if (!hdev) {
806                 printk(KERN_WARNING "brf6150: Frame for unknown device\n");
807                 return -ENODEV;
808         }
809
810         if (!test_bit(HCI_RUNNING, &hdev->flags)) {
811                 printk(KERN_WARNING "brf6150: Frame for non-running device\n");
812                 return -EIO;
813         }
814
815         info = hdev->driver_data;
816
817         switch (bt_cb(skb)->pkt_type) {
818                 case HCI_COMMAND_PKT:
819                         hdev->stat.cmd_tx++;
820                         break;
821                 case HCI_ACLDATA_PKT:
822                         hdev->stat.acl_tx++;
823                         break;
824                 case HCI_SCODATA_PKT:
825                         hdev->stat.sco_tx++;
826                         break;
827         };
828
829         /* Push frame type to skb */
830         clk_enable(info->uart_ck);
831         *skb_push(skb, 1) = bt_cb(skb)->pkt_type;
832         skb_queue_tail(&info->txq, skb);
833
834         brf6150_disable_pm_tx(info);
835
836         return 0;
837 }
838
839 static int brf6150_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
840 {
841         return -ENOIOCTLCMD;
842 }
843
844 static void brf6150_device_release(struct device *dev)
845 {
846 }
847
848 static int brf6150_register_hdev(struct brf6150_info *info)
849 {
850         struct hci_dev *hdev;
851
852         /* Initialize and register HCI device */
853
854         hdev = hci_alloc_dev();
855         if (!hdev) {
856                 printk(KERN_WARNING "brf6150: Can't allocate memory for device\n");
857                 return -ENOMEM;
858         }
859         info->hdev = hdev;
860
861         hdev->type = HCI_UART;
862         hdev->driver_data = info;
863
864         hdev->open = brf6150_hci_open;
865         hdev->close = brf6150_hci_close;
866         hdev->destruct = brf6150_hci_destruct;
867         hdev->flush = brf6150_hci_flush;
868         hdev->send = brf6150_hci_send_frame;
869         hdev->destruct = brf6150_hci_destruct;
870         hdev->ioctl = brf6150_hci_ioctl;
871
872         hdev->owner = THIS_MODULE;
873
874         if (hci_register_dev(hdev) < 0) {
875                 printk(KERN_WARNING "brf6150: Can't register HCI device %s.\n", hdev->name);
876                 return -ENODEV;
877         }
878
879         return 0;
880 }
881
882 static int __init brf6150_init(void)
883 {
884         struct brf6150_info *info;
885         int irq, err;
886
887         info = kmalloc(sizeof(struct brf6150_info), GFP_KERNEL);
888         if (!info)
889                 return -ENOMEM;
890         memset(info, 0, sizeof(struct brf6150_info));
891
892         brf6150_device.dev.driver_data = info;
893         init_completion(&info->init_completion);
894         init_completion(&info->fw_completion);
895         info->pm_enabled = 0;
896         info->rx_pm_enabled = 0;
897         info->tx_pm_enabled = 0;
898         info->garbage_bytes = 0;
899         tasklet_init(&info->tx_task, brf6150_tx_tasklet, (unsigned long)info);
900         spin_lock_init(&info->lock);
901         skb_queue_head_init(&info->txq);
902         init_timer(&info->pm_timer);
903         info->pm_timer.function = brf6150_pm_timer;
904         info->pm_timer.data = (unsigned long)info;
905         exit_info = NULL;
906
907         info->btinfo = omap_get_config(OMAP_TAG_NOKIA_BT, struct omap_bluetooth_config);
908         if (info->btinfo == NULL)
909                 return -1;
910
911         NBT_DBG("RESET gpio: %d\n", info->btinfo->reset_gpio);
912         NBT_DBG("BTWU gpio: %d\n", info->btinfo->bt_wakeup_gpio);
913         NBT_DBG("HOSTWU gpio: %d\n", info->btinfo->host_wakeup_gpio);
914         NBT_DBG("Uart: %d\n", info->btinfo->bt_uart);
915         NBT_DBG("sysclk: %d\n", info->btinfo->bt_sysclk);
916
917         err = gpio_request(info->btinfo->reset_gpio, "BT reset");
918         if (err < 0)
919         {
920                 printk(KERN_WARNING "Cannot get GPIO line %d", 
921                        info->btinfo->reset_gpio);
922                 kfree(info);
923                 return err;
924         }
925
926         err = gpio_request(info->btinfo->bt_wakeup_gpio, "BT wakeup");
927         if (err < 0)
928         {
929                 printk(KERN_WARNING "Cannot get GPIO line 0x%d",
930                        info->btinfo->bt_wakeup_gpio);
931                 gpio_free(info->btinfo->reset_gpio);
932                 kfree(info);
933                 return err;
934         }
935
936         err = gpio_request(info->btinfo->host_wakeup_gpio, "BT host wakeup");
937         if (err < 0)
938         {
939                 printk(KERN_WARNING "Cannot get GPIO line %d",
940                        info->btinfo->host_wakeup_gpio);
941                 gpio_free(info->btinfo->reset_gpio);
942                 gpio_free(info->btinfo->bt_wakeup_gpio);
943                 kfree(info);
944                 return err;
945         }
946
947         gpio_direction_output(info->btinfo->reset_gpio, 0);
948         gpio_direction_output(info->btinfo->bt_wakeup_gpio, 0);
949         gpio_direction_input(info->btinfo->host_wakeup_gpio);
950         set_irq_type(gpio_to_irq(info->btinfo->host_wakeup_gpio), IRQ_TYPE_NONE);
951
952         switch (info->btinfo->bt_uart) {
953         case 1:
954                 irq = INT_UART1;
955                 info->uart_ck = clk_get(NULL, "uart1_ck");
956                 /* FIXME: Use platform_get_resource for the port */
957                 info->uart_base = ioremap(OMAP_UART1_BASE, 0x16);
958                 if (!info->uart_base)
959                         goto cleanup;
960                 break;
961         case 2:
962                 irq = INT_UART2;
963                 info->uart_ck = clk_get(NULL, "uart2_ck");
964                 /* FIXME: Use platform_get_resource for the port */
965                 info->uart_base = ioremap(OMAP_UART2_BASE, 0x16);
966                 if (!info->uart_base)
967                         goto cleanup;
968                 break;
969         case 3:
970                 irq = INT_UART3;
971                 info->uart_ck = clk_get(NULL, "uart3_ck");
972                 /* FIXME: Use platform_get_resource for the port */
973                 info->uart_base = ioremap(OMAP_UART3_BASE, 0x16);
974                 if (!info->uart_base)
975                         goto cleanup;
976                 break;
977         default:
978                 printk(KERN_ERR "No uart defined\n");
979                 goto cleanup;
980         }
981
982         info->irq = irq;
983         err = request_irq(irq, brf6150_interrupt, 0, "brf6150", (void *)info);
984         if (err < 0) {
985                 printk(KERN_ERR "brf6150: unable to get IRQ %d\n", irq);
986                 goto cleanup;
987         }
988
989         err = request_irq(gpio_to_irq(info->btinfo->host_wakeup_gpio),
990                         brf6150_wakeup_interrupt, 0, "brf6150_wkup", (void *)info);
991         if (err < 0) {
992                 printk(KERN_ERR "brf6150: unable to get wakeup IRQ %d\n",
993                                 gpio_to_irq(info->btinfo->host_wakeup_gpio));
994                 free_irq(irq, (void *)info);
995                 goto cleanup;
996         }
997
998         /* Register with LDM */
999         if (platform_device_register(&brf6150_device)) {
1000                 printk(KERN_ERR "failed to register brf6150 device\n");
1001                 err = -ENODEV;
1002                 goto cleanup_irq;
1003         }
1004         /* Register the driver with LDM */
1005         if (driver_register(&brf6150_driver)) {
1006                 printk(KERN_WARNING "failed to register brf6150 driver\n");
1007                 platform_device_unregister(&brf6150_device);
1008                 err = -ENODEV;
1009                 goto cleanup_irq;
1010         }
1011
1012         if (brf6150_register_hdev(info) < 0) {
1013                 printk(KERN_WARNING "failed to register brf6150 hci device\n");
1014                 platform_device_unregister(&brf6150_device);
1015                 driver_unregister(&brf6150_driver);
1016                 goto cleanup_irq;
1017         }
1018
1019         exit_info = info;
1020         return 0;
1021
1022 cleanup_irq:
1023         free_irq(irq, (void *)info);
1024         free_irq(gpio_to_irq(info->btinfo->host_wakeup_gpio), (void *)info);
1025 cleanup:
1026         gpio_free(info->btinfo->reset_gpio);
1027         gpio_free(info->btinfo->bt_wakeup_gpio);
1028         gpio_free(info->btinfo->host_wakeup_gpio);
1029         kfree(info);
1030
1031         return err;
1032 }
1033
1034 static void __exit brf6150_exit(void)
1035 {
1036         brf6150_hci_close(exit_info->hdev);
1037         hci_free_dev(exit_info->hdev);
1038         gpio_free(exit_info->btinfo->reset_gpio);
1039         gpio_free(exit_info->btinfo->bt_wakeup_gpio);
1040         gpio_free(exit_info->btinfo->host_wakeup_gpio);
1041         free_irq(exit_info->irq, (void *)exit_info);
1042         free_irq(gpio_to_irq(exit_info->btinfo->host_wakeup_gpio), (void *)exit_info);
1043         kfree(exit_info);
1044 }
1045
1046 module_init(brf6150_init);
1047 module_exit(brf6150_exit);
1048
1049 MODULE_DESCRIPTION("brf6150 hci driver");
1050 MODULE_LICENSE("GPL");
1051 MODULE_AUTHOR("Ville Tervo <ville.tervo@nokia.com>");