]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/bluetooth/brf6150.c
Manual merge to make things compile after updating to 2.6.18-rc4
[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/config.h>
23 #include <linux/module.h>
24
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/errno.h>
28 #include <linux/delay.h>
29 #include <linux/spinlock.h>
30 #include <linux/serial_reg.h>
31 #include <linux/skbuff.h>
32 #include <linux/firmware.h>
33 #include <linux/irq.h>
34 #include <linux/timer.h>
35 #include <linux/clk.h>
36 #include <linux/platform_device.h>
37
38 #include <asm/arch/hardware.h>
39 #include <asm/arch/gpio.h>
40 #include <asm/arch/board.h>
41 #include <asm/arch/irqs.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                 omap_set_gpio_dataout(info->btinfo->bt_wakeup_gpio, 1);
186         }
187         if (omap_get_gpio_datain(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                 omap_set_gpio_dataout(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, struct pt_regs *regs)
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                                             struct pt_regs *regs)
623 {
624         struct brf6150_info *info = dev_inst;
625         int should_wakeup;
626         unsigned long flags;
627
628         spin_lock_irqsave(&info->lock, flags);
629         should_wakeup = omap_get_gpio_datain(info->btinfo->host_wakeup_gpio);
630         NBT_DBG_POWER("gpio interrupt %d\n", should_wakeup);
631         if (should_wakeup) {
632                 clk_enable(info->uart_ck);
633                 brf6150_set_auto_ctsrts(info, 1);
634                 brf6150_rx(info);
635                 tasklet_schedule(&info->tx_task);
636         } else {
637                 brf6150_set_auto_ctsrts(info, 0);
638                 brf6150_set_rts(info, 0);
639                 clk_disable(info->uart_ck);
640         }
641
642         spin_unlock_irqrestore(&info->lock, flags);
643         return IRQ_HANDLED;
644 }
645
646 static int brf6150_init_uart(struct brf6150_info *info)
647 {
648         int count = 0;
649
650         /* Reset the  UART */
651         brf6150_outb(info, UART_OMAP_SYSC, UART_SYSC_OMAP_RESET);
652         while (!(brf6150_inb(info, UART_OMAP_SYSS) & UART_SYSS_RESETDONE)) {
653                 if (count++ > 100) {
654                         printk(KERN_ERR "brf6150: UART reset timeout\n");
655                         return -1;
656                 }
657                 udelay(1);
658         }
659
660         /* Enable and setup FIFO */
661         brf6150_outb(info, UART_LCR, UART_LCR_WLEN8);
662         brf6150_outb(info, UART_OMAP_MDR1, 0x00); /* Make sure UART mode is enabled */
663         brf6150_outb(info, UART_OMAP_SCR, 0x00);
664         brf6150_outb(info, UART_EFR, brf6150_inb(info, UART_EFR) | UART_EFR_ECB);
665         brf6150_outb(info, UART_MCR, brf6150_inb(info, UART_MCR) | UART_MCR_TCRTLR);
666         brf6150_outb(info, UART_TI752_TLR, 0xff);
667         brf6150_outb(info, UART_TI752_TCR, 0x1f);
668         brf6150_outb(info, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
669         brf6150_outb(info, UART_IER, UART_IER_RDI);
670
671         return 0;
672 }
673
674 static int brf6150_reset(struct brf6150_info *info)
675 {
676         omap_set_gpio_dataout(info->btinfo->bt_wakeup_gpio, 0);
677         omap_set_gpio_dataout(info->btinfo->reset_gpio, 0);
678         current->state = TASK_UNINTERRUPTIBLE;
679         schedule_timeout(msecs_to_jiffies(10));
680         omap_set_gpio_dataout(info->btinfo->bt_wakeup_gpio, 1);
681         current->state = TASK_UNINTERRUPTIBLE;
682         schedule_timeout(msecs_to_jiffies(100));
683         omap_set_gpio_dataout(info->btinfo->reset_gpio, 1);
684         current->state = TASK_UNINTERRUPTIBLE;
685         schedule_timeout(msecs_to_jiffies(100));
686
687         return 0;
688 }
689
690 static int brf6150_send_firmware(struct brf6150_info *info)
691 {
692         struct sk_buff *skb;
693
694         init_completion(&info->fw_completion);
695         info->fw_error = 0;
696
697         while ((skb = brf6150_read_fw_cmd(info, GFP_KERNEL)) != NULL) {
698                 clk_enable(info->uart_ck);
699                 skb_queue_tail(&info->txq, skb);
700                 tasklet_schedule(&info->tx_task);
701
702                 if (!wait_for_completion_timeout(&info->fw_completion, HZ)) {
703                         return -1;
704                 }
705
706                 if (info->fw_error) {
707                         return -1;
708                 }
709         }
710         NBT_DBG_FW("Firmware sent\n");
711
712         return 0;
713
714 }
715
716 /* hci callback functions */
717 static int brf6150_hci_flush(struct hci_dev *hdev)
718 {
719         struct brf6150_info *info;
720         info = hdev->driver_data;
721
722         skb_queue_purge(&info->txq);
723
724         return 0;
725 }
726
727 static int brf6150_hci_open(struct hci_dev *hdev)
728 {
729         struct brf6150_info *info;
730         int err;
731
732         info = hdev->driver_data;
733
734         if (test_bit(HCI_RUNNING, &hdev->flags))
735                 return 0;
736
737         if (brf6150_open_firmware(info) < 0) {
738                 printk("Cannot open firmware\n");
739                 return -1;
740         }
741
742         info->rx_state = WAIT_FOR_PKT_TYPE;
743         info->rx_count = 0;
744         info->garbage_bytes = 0;
745         info->rx_skb = NULL;
746         info->pm_enabled = 0;
747         set_irq_type(OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio), IRQT_NOEDGE);
748         init_completion(&info->fw_completion);
749
750         clk_enable(info->uart_ck);
751
752         brf6150_init_uart(info);
753         brf6150_set_auto_ctsrts(info, 0);
754         brf6150_set_rts(info, 0);
755         brf6150_reset(info);
756         brf6150_wait_for_cts(info, 1, 10);
757         brf6150_set_rts(info, 1);
758         if (brf6150_send_negotiation(info)) {
759                 brf6150_close_firmware(info);
760                 return -1;
761         }
762
763         if (!wait_for_completion_interruptible_timeout(&info->init_completion, HZ)) {
764                 brf6150_close_firmware(info);
765                 clk_disable(info->uart_ck);
766                 clear_bit(HCI_RUNNING, &hdev->flags);
767                 return -1;
768         }
769         brf6150_set_auto_ctsrts(info, 1);
770
771         err = brf6150_send_firmware(info);
772         brf6150_close_firmware(info);
773         if (err < 0)
774                 printk(KERN_ERR "brf6150: Sending firmware failed. Bluetooth won't work properly\n");
775
776         set_irq_type(OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio), IRQT_BOTHEDGE);
777         info->pm_enabled = 1;
778         set_bit(HCI_RUNNING, &hdev->flags);
779         return 0;
780 }
781
782 static int brf6150_hci_close(struct hci_dev *hdev)
783 {
784         struct brf6150_info *info = hdev->driver_data;
785         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
786                 return 0;
787
788         brf6150_hci_flush(hdev);
789         clk_disable(info->uart_ck);
790         del_timer_sync(&info->pm_timer);
791         omap_set_gpio_dataout(info->btinfo->bt_wakeup_gpio, 0);
792         set_irq_type(OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio), IRQT_NOEDGE);
793
794         return 0;
795 }
796
797 static void brf6150_hci_destruct(struct hci_dev *hdev)
798 {
799 }
800
801 static int brf6150_hci_send_frame(struct sk_buff *skb)
802 {
803         struct brf6150_info *info;
804         struct hci_dev *hdev = (struct hci_dev *)skb->dev;
805
806         if (!hdev) {
807                 printk(KERN_WARNING "brf6150: Frame for unknown device\n");
808                 return -ENODEV;
809         }
810
811         if (!test_bit(HCI_RUNNING, &hdev->flags)) {
812                 printk(KERN_WARNING "brf6150: Frame for non-running device\n");
813                 return -EIO;
814         }
815
816         info = hdev->driver_data;
817
818         switch (bt_cb(skb)->pkt_type) {
819                 case HCI_COMMAND_PKT:
820                         hdev->stat.cmd_tx++;
821                         break;
822                 case HCI_ACLDATA_PKT:
823                         hdev->stat.acl_tx++;
824                         break;
825                 case HCI_SCODATA_PKT:
826                         hdev->stat.sco_tx++;
827                         break;
828         };
829
830         /* Push frame type to skb */
831         clk_enable(info->uart_ck);
832         *skb_push(skb, 1) = bt_cb(skb)->pkt_type;
833         skb_queue_tail(&info->txq, skb);
834
835         brf6150_disable_pm_tx(info);
836
837         return 0;
838 }
839
840 static int brf6150_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
841 {
842         return -ENOIOCTLCMD;
843 }
844
845 static void brf6150_device_release(struct device *dev)
846 {
847 }
848
849 static int brf6150_register_hdev(struct brf6150_info *info)
850 {
851         struct hci_dev *hdev;
852
853         /* Initialize and register HCI device */
854
855         hdev = hci_alloc_dev();
856         if (!hdev) {
857                 printk(KERN_WARNING "brf6150: Can't allocate memory for device\n");
858                 return -ENOMEM;
859         }
860         info->hdev = hdev;
861
862         hdev->type = HCI_UART;
863         hdev->driver_data = info;
864
865         hdev->open = brf6150_hci_open;
866         hdev->close = brf6150_hci_close;
867         hdev->destruct = brf6150_hci_destruct;
868         hdev->flush = brf6150_hci_flush;
869         hdev->send = brf6150_hci_send_frame;
870         hdev->destruct = brf6150_hci_destruct;
871         hdev->ioctl = brf6150_hci_ioctl;
872
873         hdev->owner = THIS_MODULE;
874
875         if (hci_register_dev(hdev) < 0) {
876                 printk(KERN_WARNING "brf6150: Can't register HCI device %s.\n", hdev->name);
877                 return -ENODEV;
878         }
879
880         return 0;
881 }
882
883 static int __init brf6150_init(void)
884 {
885         struct brf6150_info *info;
886         int irq, err;
887
888         info = kmalloc(sizeof(struct brf6150_info), GFP_KERNEL);
889         if (!info)
890                 return -ENOMEM;
891         memset(info, 0, sizeof(struct brf6150_info));
892
893         brf6150_device.dev.driver_data = info;
894         init_completion(&info->init_completion);
895         init_completion(&info->fw_completion);
896         info->pm_enabled = 0;
897         info->rx_pm_enabled = 0;
898         info->tx_pm_enabled = 0;
899         info->garbage_bytes = 0;
900         tasklet_init(&info->tx_task, brf6150_tx_tasklet, (unsigned long)info);
901         spin_lock_init(&info->lock);
902         skb_queue_head_init(&info->txq);
903         init_timer(&info->pm_timer);
904         info->pm_timer.function = brf6150_pm_timer;
905         info->pm_timer.data = (unsigned long)info;
906         exit_info = NULL;
907
908         info->btinfo = omap_get_config(OMAP_TAG_NOKIA_BT, struct omap_bluetooth_config);
909         if (info->btinfo == NULL)
910                 return -1;
911
912         NBT_DBG("RESET gpio: %d\n", info->btinfo->reset_gpio);
913         NBT_DBG("BTWU gpio: %d\n", info->btinfo->bt_wakeup_gpio);
914         NBT_DBG("HOSTWU gpio: %d\n", info->btinfo->host_wakeup_gpio);
915         NBT_DBG("Uart: %d\n", info->btinfo->bt_uart);
916         NBT_DBG("sysclk: %d\n", info->btinfo->bt_sysclk);
917
918         err = omap_request_gpio(info->btinfo->reset_gpio);
919         if (err < 0)
920         {
921                 printk(KERN_WARNING "Cannot get GPIO line %d", 
922                        info->btinfo->reset_gpio);
923                 kfree(info);
924                 return err;
925         }
926
927         err = omap_request_gpio(info->btinfo->bt_wakeup_gpio);
928         if (err < 0)
929         {
930                 printk(KERN_WARNING "Cannot get GPIO line 0x%d",
931                        info->btinfo->bt_wakeup_gpio);
932                 omap_free_gpio(info->btinfo->reset_gpio);
933                 kfree(info);
934                 return err;
935         }
936
937         err = omap_request_gpio(info->btinfo->host_wakeup_gpio);
938         if (err < 0)
939         {
940                 printk(KERN_WARNING "Cannot get GPIO line %d",
941                        info->btinfo->host_wakeup_gpio);
942                 omap_free_gpio(info->btinfo->reset_gpio);
943                 omap_free_gpio(info->btinfo->bt_wakeup_gpio);
944                 kfree(info);
945                 return err;
946         }
947
948         omap_set_gpio_direction(info->btinfo->reset_gpio, 0);
949         omap_set_gpio_direction(info->btinfo->bt_wakeup_gpio, 0);
950         omap_set_gpio_direction(info->btinfo->host_wakeup_gpio, 1);
951         set_irq_type(OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio), IRQT_NOEDGE);
952
953         switch (info->btinfo->bt_uart) {
954         case 1:
955                 irq = INT_UART1;
956                 info->uart_ck = clk_get(NULL, "uart1_ck");
957                 info->uart_base = io_p2v((unsigned long)OMAP_UART1_BASE);
958                 break;
959         case 2:
960                 irq = INT_UART2;
961                 info->uart_ck = clk_get(NULL, "uart2_ck");
962                 info->uart_base = io_p2v((unsigned long)OMAP_UART2_BASE);
963                 break;
964         case 3:
965                 irq = INT_UART3;
966                 info->uart_ck = clk_get(NULL, "uart3_ck");
967                 info->uart_base = io_p2v((unsigned long)OMAP_UART3_BASE);
968                 break;
969         default:
970                 printk(KERN_ERR "No uart defined\n");
971                 goto cleanup;
972         }
973
974         info->irq = irq;
975         err = request_irq(irq, brf6150_interrupt, 0, "brf6150", (void *)info);
976         if (err < 0) {
977                 printk(KERN_ERR "brf6150: unable to get IRQ %d\n", irq);
978                 goto cleanup;
979         }
980
981         err = request_irq(OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio),
982                         brf6150_wakeup_interrupt, 0, "brf6150_wkup", (void *)info);
983         if (err < 0) {
984                 printk(KERN_ERR "brf6150: unable to get wakeup IRQ %d\n",
985                                 OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio));
986                 free_irq(irq, (void *)info);
987                 goto cleanup;
988         }
989
990         /* Register with LDM */
991         if (platform_device_register(&brf6150_device)) {
992                 printk(KERN_ERR "failed to register brf6150 device\n");
993                 err = -ENODEV;
994                 goto cleanup_irq;
995         }
996         /* Register the driver with LDM */
997         if (driver_register(&brf6150_driver)) {
998                 printk(KERN_WARNING "failed to register brf6150 driver\n");
999                 platform_device_unregister(&brf6150_device);
1000                 err = -ENODEV;
1001                 goto cleanup_irq;
1002         }
1003
1004         if (brf6150_register_hdev(info) < 0) {
1005                 printk(KERN_WARNING "failed to register brf6150 hci device\n");
1006                 platform_device_unregister(&brf6150_device);
1007                 driver_unregister(&brf6150_driver);
1008                 goto cleanup_irq;
1009         }
1010
1011         exit_info = info;
1012         return 0;
1013
1014 cleanup_irq:
1015         free_irq(irq, (void *)info);
1016         free_irq(OMAP_GPIO_IRQ(info->btinfo->host_wakeup_gpio), (void *)info);
1017 cleanup:
1018         omap_free_gpio(info->btinfo->reset_gpio);
1019         omap_free_gpio(info->btinfo->bt_wakeup_gpio);
1020         omap_free_gpio(info->btinfo->host_wakeup_gpio);
1021         kfree(info);
1022
1023         return err;
1024 }
1025
1026 static void __exit brf6150_exit(void)
1027 {
1028         brf6150_hci_close(exit_info->hdev);
1029         hci_free_dev(exit_info->hdev);
1030         omap_free_gpio(exit_info->btinfo->reset_gpio);
1031         omap_free_gpio(exit_info->btinfo->bt_wakeup_gpio);
1032         omap_free_gpio(exit_info->btinfo->host_wakeup_gpio);
1033         free_irq(exit_info->irq, (void *)exit_info);
1034         free_irq(OMAP_GPIO_IRQ(exit_info->btinfo->host_wakeup_gpio), (void *)exit_info);
1035         kfree(exit_info);
1036 }
1037
1038 module_init(brf6150_init);
1039 module_exit(brf6150_exit);
1040
1041 MODULE_DESCRIPTION("brf6150 hci driver");
1042 MODULE_LICENSE("GPL");
1043 MODULE_AUTHOR("Ville Tervo <ville.tervo@nokia.com>");