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