]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/bluetooth/hci_h4p/core.c
Merge branch 'omap-fixes'
[linux-2.6-omap-h63xx.git] / drivers / bluetooth / hci_h4p / core.c
1 /*
2  * This file is part of hci_h4p bluetooth driver
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation.
5  *
6  * Contact: Ville Tervo <ville.tervo@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * 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., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/module.h>
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/errno.h>
29 #include <linux/delay.h>
30 #include <linux/spinlock.h>
31 #include <linux/serial_reg.h>
32 #include <linux/skbuff.h>
33 #include <linux/timer.h>
34 #include <linux/device.h>
35 #include <linux/platform_device.h>
36 #include <linux/clk.h>
37 #include <linux/gpio.h>
38
39 #include <mach/hardware.h>
40 #include <mach/board.h>
41 #include <mach/irqs.h>
42 #include <mach/pm.h>
43
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
46 #include <net/bluetooth/hci.h>
47
48 #include "hci_h4p.h"
49
50 #define PM_TIMEOUT 200
51
52 /* This should be used in function that cannot release clocks */
53 static void hci_h4p_set_clk(struct hci_h4p_info *info, int *clock, int enable)
54 {
55         unsigned long flags;
56
57         spin_lock_irqsave(&info->clocks_lock, flags);
58         if (enable && !*clock) {
59                 NBT_DBG_POWER("Enabling %p\n", clock);
60                 clk_enable(info->uart_fclk);
61 #ifdef CONFIG_ARCH_OMAP2
62                 if (cpu_is_omap24xx()) {
63                         clk_enable(info->uart_iclk);
64                         omap2_block_sleep();
65                 }
66 #endif
67         }
68         if (!enable && *clock) {
69                 NBT_DBG_POWER("Disabling %p\n", clock);
70                 clk_disable(info->uart_fclk);
71 #ifdef CONFIG_ARCH_OMAP2
72                 if (cpu_is_omap24xx()) {
73                         clk_disable(info->uart_iclk);
74                         omap2_allow_sleep();
75                 }
76 #endif
77         }
78
79         *clock = enable;
80         spin_unlock_irqrestore(&info->clocks_lock, flags);
81 }
82
83 /* Power management functions */
84 static void hci_h4p_disable_tx(struct hci_h4p_info *info)
85 {
86         NBT_DBG_POWER("\n");
87
88         if (!info->pm_enabled)
89                 return;
90
91         mod_timer(&info->tx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
92 }
93
94 static void hci_h4p_enable_tx(struct hci_h4p_info *info)
95 {
96         NBT_DBG_POWER("\n");
97
98         if (!info->pm_enabled)
99                 return;
100
101         del_timer_sync(&info->tx_pm_timer);
102         if (info->tx_pm_enabled) {
103                 info->tx_pm_enabled = 0;
104                 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
105                 gpio_set_value(info->bt_wakeup_gpio, 1);
106         }
107 }
108
109 static void hci_h4p_tx_pm_timer(unsigned long data)
110 {
111         struct hci_h4p_info *info;
112
113         NBT_DBG_POWER("\n");
114
115         info = (struct hci_h4p_info *)data;
116
117         if (hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT) {
118                 gpio_set_value(info->bt_wakeup_gpio, 0);
119                 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
120                 info->tx_pm_enabled = 1;
121         }
122         else {
123                 mod_timer(&info->tx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
124         }
125 }
126
127 static void hci_h4p_disable_rx(struct hci_h4p_info *info)
128 {
129         if (!info->pm_enabled)
130                 return;
131
132         mod_timer(&info->rx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
133 }
134
135 static void hci_h4p_enable_rx(struct hci_h4p_info *info)
136 {
137         unsigned long flags;
138
139         if (!info->pm_enabled)
140                 return;
141
142         del_timer_sync(&info->rx_pm_timer);
143         spin_lock_irqsave(&info->lock, flags);
144         if (info->rx_pm_enabled) {
145                 hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
146                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_RDI);
147                 __hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_RTS);
148                 info->rx_pm_enabled = 0;
149         }
150         spin_unlock_irqrestore(&info->lock, flags);
151 }
152
153 static void hci_h4p_rx_pm_timer(unsigned long data)
154 {
155         unsigned long flags;
156         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
157
158         spin_lock_irqsave(&info->lock, flags);
159         if (!(hci_h4p_inb(info, UART_LSR) & UART_LSR_DR)) {
160                 __hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_RTS);
161                 hci_h4p_set_rts(info, 0);
162                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_RDI);
163                 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
164                 info->rx_pm_enabled = 1;
165         }
166         else {
167                 mod_timer(&info->rx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
168         }
169         spin_unlock_irqrestore(&info->lock, flags);
170 }
171
172 /* Negotiation functions */
173 int hci_h4p_send_alive_packet(struct hci_h4p_info *info)
174 {
175         NBT_DBG("Sending alive packet\n");
176
177         if (!info->alive_cmd_skb)
178                 return -EINVAL;
179
180         /* Keep reference to buffer so we can reuse it */
181         info->alive_cmd_skb = skb_get(info->alive_cmd_skb);
182
183         skb_queue_tail(&info->txq, info->alive_cmd_skb);
184         tasklet_schedule(&info->tx_task);
185
186         NBT_DBG("Alive packet sent\n");
187
188         return 0;
189 }
190
191 static void hci_h4p_alive_packet(struct hci_h4p_info *info, struct sk_buff *skb)
192 {
193         NBT_DBG("Received alive packet\n");
194         if (skb->data[1] == 0xCC) {
195                 complete(&info->init_completion);
196         }
197
198         kfree_skb(skb);
199 }
200
201 static int hci_h4p_send_negotiation(struct hci_h4p_info *info, struct sk_buff *skb)
202 {
203         NBT_DBG("Sending negotiation..\n");
204
205         hci_h4p_change_speed(info, INIT_SPEED);
206
207         info->init_error = 0;
208         init_completion(&info->init_completion);
209         skb_queue_tail(&info->txq, skb);
210         tasklet_schedule(&info->tx_task);
211
212         if (!wait_for_completion_interruptible_timeout(&info->init_completion,
213                                 msecs_to_jiffies(1000))) 
214                 return -ETIMEDOUT;
215
216         NBT_DBG("Negotiation sent\n");
217         return info->init_error;
218 }
219
220 static void hci_h4p_negotiation_packet(struct hci_h4p_info *info,
221                                        struct sk_buff *skb)
222 {
223         int err = 0;
224
225         if (skb->data[1] == 0x20) {
226                 /* Change to operational settings */
227                 hci_h4p_set_rts(info, 0);
228
229                 err = hci_h4p_wait_for_cts(info, 0, 100);
230                 if (err < 0)
231                         goto neg_ret;
232
233                 hci_h4p_change_speed(info, MAX_BAUD_RATE);
234
235                 err = hci_h4p_wait_for_cts(info, 1, 100);
236                 if (err < 0)
237                         goto neg_ret;
238
239                 hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_CTS | UART_EFR_RTS);
240
241                 err = hci_h4p_send_alive_packet(info);
242                 if (err < 0)
243                         goto neg_ret;
244         } else {
245                 dev_err(info->dev, "Could not negotiate hci_h4p settings\n");
246                 err = -EINVAL;
247                 goto neg_ret;
248         }
249
250         kfree_skb(skb);
251         return;
252
253 neg_ret:
254         info->init_error = err;
255         complete(&info->init_completion);
256         kfree_skb(skb);
257 }
258
259 /* H4 packet handling functions */
260 static int hci_h4p_get_hdr_len(struct hci_h4p_info *info, u8 pkt_type)
261 {
262         long retval;
263
264         switch (pkt_type) {
265         case H4_EVT_PKT:
266                 retval = HCI_EVENT_HDR_SIZE;
267                 break;
268         case H4_ACL_PKT:
269                 retval = HCI_ACL_HDR_SIZE;
270                 break;
271         case H4_SCO_PKT:
272                 retval = HCI_SCO_HDR_SIZE;
273                 break;
274         case H4_NEG_PKT:
275                 retval = 11;
276                 break;
277         case H4_ALIVE_PKT:
278                 retval = 3;
279                 break;
280         default:
281                 dev_err(info->dev, "Unknown H4 packet type 0x%.2x\n", pkt_type);
282                 retval = -1;
283                 break;
284         }
285
286         return retval;
287 }
288
289 static unsigned int hci_h4p_get_data_len(struct hci_h4p_info *info,
290                                          struct sk_buff *skb)
291 {
292         long retval = -1;
293         struct hci_event_hdr *evt_hdr;
294         struct hci_acl_hdr *acl_hdr;
295         struct hci_sco_hdr *sco_hdr;
296
297         switch (bt_cb(skb)->pkt_type) {
298         case H4_EVT_PKT:
299                 evt_hdr = (struct hci_event_hdr *)skb->data;
300                 retval = evt_hdr->plen;
301                 break;
302         case H4_ACL_PKT:
303                 acl_hdr = (struct hci_acl_hdr *)skb->data;
304                 retval = le16_to_cpu(acl_hdr->dlen);
305                 break;
306         case H4_SCO_PKT:
307                 sco_hdr = (struct hci_sco_hdr *)skb->data;
308                 retval = sco_hdr->dlen;
309                 break;
310         case H4_NEG_PKT:
311                 retval = 0;
312                 break;
313         case H4_ALIVE_PKT:
314                 retval = 0;
315                 break;
316         }
317
318         return retval;
319 }
320
321 static inline void hci_h4p_recv_frame(struct hci_h4p_info *info,
322                                       struct sk_buff *skb)
323 {
324
325         if (unlikely(!test_bit(HCI_RUNNING, &info->hdev->flags))) {
326                 NBT_DBG("fw_event\n");
327                 hci_h4p_parse_fw_event(info, skb);
328         } else {
329                 hci_recv_frame(skb);
330                 NBT_DBG("Frame sent to upper layer\n");
331         }
332 }
333
334 static void hci_h4p_rx_tasklet(unsigned long data)
335 {
336         u8 byte;
337         unsigned long flags;
338         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
339
340         NBT_DBG("tasklet woke up\n");
341         NBT_DBG_TRANSFER("rx_tasklet woke up\ndata ");
342
343         while (hci_h4p_inb(info, UART_LSR) & UART_LSR_DR) {
344                 byte = hci_h4p_inb(info, UART_RX);
345                 if (info->garbage_bytes) {
346                         info->garbage_bytes--;
347                         continue;
348                 }
349                 if (info->rx_skb == NULL) {
350                         info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC | GFP_DMA);
351                         if (!info->rx_skb) {
352                                 dev_err(info->dev, "Can't allocate memory for new packet\n");
353                                 goto finish_task;
354                         }
355                         info->rx_state = WAIT_FOR_PKT_TYPE;
356                         info->rx_skb->dev = (void *)info->hdev;
357                 }
358                 info->hdev->stat.byte_rx++;
359                 NBT_DBG_TRANSFER_NF("0x%.2x  ", byte);
360                 switch (info->rx_state) {
361                 case WAIT_FOR_PKT_TYPE:
362                         bt_cb(info->rx_skb)->pkt_type = byte;
363                         info->rx_count = hci_h4p_get_hdr_len(info, byte);
364                         if (info->rx_count < 0) {
365                                 info->hdev->stat.err_rx++;
366                                 kfree_skb(info->rx_skb);
367                                 info->rx_skb = NULL;
368                         } else {
369                                 info->rx_state = WAIT_FOR_HEADER;
370                         }
371                         break;
372                 case WAIT_FOR_HEADER:
373                         info->rx_count--;
374                         *skb_put(info->rx_skb, 1) = byte;
375                         if (info->rx_count == 0) {
376                                 info->rx_count = hci_h4p_get_data_len(info, info->rx_skb);
377                                 if (info->rx_count > skb_tailroom(info->rx_skb)) {
378                                         dev_err(info->dev, "Frame is %ld bytes too long.\n",
379                                                info->rx_count - skb_tailroom(info->rx_skb));
380                                         kfree_skb(info->rx_skb);
381                                         info->rx_skb = NULL;
382                                         info->garbage_bytes = info->rx_count - skb_tailroom(info->rx_skb);
383                                         break;
384                                 }
385                                 info->rx_state = WAIT_FOR_DATA;
386
387                                 if (bt_cb(info->rx_skb)->pkt_type == H4_NEG_PKT) {
388                                         hci_h4p_negotiation_packet(info, info->rx_skb);
389                                         info->rx_skb = NULL;
390                                         info->rx_state = WAIT_FOR_PKT_TYPE;
391                                         goto finish_task;
392                                 }
393                                 if (bt_cb(info->rx_skb)->pkt_type == H4_ALIVE_PKT) {
394                                         hci_h4p_alive_packet(info, info->rx_skb);
395                                         info->rx_skb = NULL;
396                                         info->rx_state = WAIT_FOR_PKT_TYPE;
397                                         goto finish_task;
398                                 }
399                         }
400                         break;
401                 case WAIT_FOR_DATA:
402                         info->rx_count--;
403                         *skb_put(info->rx_skb, 1) = byte;
404                         if (info->rx_count == 0) {
405                                 /* H4+ devices should allways send word aligned packets */
406                                 if (!(info->rx_skb->len % 2)) {
407                                         info->garbage_bytes++;
408                                 }
409                                 hci_h4p_recv_frame(info, info->rx_skb);
410                                 info->rx_skb = NULL;
411                         }
412                         break;
413                 default:
414                         WARN_ON(1);
415                         break;
416                 }
417         }
418
419 finish_task:
420         spin_lock_irqsave(&info->lock, flags);
421         hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_RDI);
422         spin_unlock_irqrestore(&info->lock, flags);
423
424         NBT_DBG_TRANSFER_NF("\n");
425         NBT_DBG("rx_ended\n");
426 }
427
428 static void hci_h4p_tx_tasklet(unsigned long data)
429 {
430         unsigned int sent = 0;
431         unsigned long flags;
432         struct sk_buff *skb;
433         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
434
435         NBT_DBG("tasklet woke up\n");
436         NBT_DBG_TRANSFER("tx_tasklet woke up\n data ");
437
438         skb = skb_dequeue(&info->txq);
439         if (!skb) {
440                 /* No data in buffer */
441                 NBT_DBG("skb ready\n");
442                 hci_h4p_disable_tx(info);
443                 return;
444         }
445
446         /* Copy data to tx fifo */
447         while (!(hci_h4p_inb(info, UART_OMAP_SSR) & UART_OMAP_SSR_TXFULL) &&
448                (sent < skb->len)) {
449                 NBT_DBG_TRANSFER_NF("0x%.2x ", skb->data[sent]);
450                 hci_h4p_outb(info, UART_TX, skb->data[sent]);
451                 sent++;
452         }
453
454         info->hdev->stat.byte_tx += sent;
455         NBT_DBG_TRANSFER_NF("\n");
456         if (skb->len == sent) {
457                 kfree_skb(skb);
458         } else {
459                 skb_pull(skb, sent);
460                 skb_queue_head(&info->txq, skb);
461         }
462
463         spin_lock_irqsave(&info->lock, flags);
464         hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_THRI);
465         spin_unlock_irqrestore(&info->lock, flags);
466 }
467
468 static irqreturn_t hci_h4p_interrupt(int irq, void *data)
469 {
470         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
471         u8 iir, msr;
472         int ret;
473         unsigned long flags;
474
475         ret = IRQ_NONE;
476
477         iir = hci_h4p_inb(info, UART_IIR);
478         if (iir & UART_IIR_NO_INT) {
479                 dev_err(info->dev, "Interrupt but no reason irq 0x%.2x\n", iir);
480                 return IRQ_HANDLED;
481         }
482
483         NBT_DBG("In interrupt handler iir 0x%.2x\n", iir);
484
485         iir &= UART_IIR_ID;
486
487         if (iir == UART_IIR_MSI) {
488                 msr = hci_h4p_inb(info, UART_MSR);
489                 ret = IRQ_HANDLED;
490         }
491         if (iir == UART_IIR_RLSI) {
492                 hci_h4p_inb(info, UART_RX);
493                 hci_h4p_inb(info, UART_LSR);
494                 ret = IRQ_HANDLED;
495         }
496
497         if (iir == UART_IIR_RDI) {
498                 spin_lock_irqsave(&info->lock, flags);
499                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_RDI);
500                 spin_unlock_irqrestore(&info->lock, flags);
501                 tasklet_schedule(&info->rx_task);
502                 ret = IRQ_HANDLED;
503         }
504
505         if (iir == UART_IIR_THRI) {
506                 spin_lock_irqsave(&info->lock, flags);
507                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_THRI);
508                 spin_unlock_irqrestore(&info->lock, flags);
509                 tasklet_schedule(&info->tx_task);
510                 ret = IRQ_HANDLED;
511         }
512
513         return ret;
514 }
515
516 static irqreturn_t hci_h4p_wakeup_interrupt(int irq, void *dev_inst)
517 {
518         struct hci_h4p_info *info = dev_inst;
519         int should_wakeup;
520         struct hci_dev *hdev;
521
522         if (!info->hdev)
523                 return IRQ_HANDLED;
524
525         hdev = info->hdev;
526
527         if (!test_bit(HCI_RUNNING, &hdev->flags))
528                 return IRQ_HANDLED;
529
530         should_wakeup = gpio_get_value(info->host_wakeup_gpio);
531         NBT_DBG_POWER("gpio interrupt %d\n", should_wakeup);
532         if (should_wakeup) {
533                 hci_h4p_enable_rx(info);
534         } else {
535                 hci_h4p_disable_rx(info);
536         }
537
538         return IRQ_HANDLED;
539 }
540
541 static int hci_h4p_reset(struct hci_h4p_info *info)
542 {
543         int err;
544
545         hci_h4p_init_uart(info);
546         hci_h4p_set_rts(info, 0);
547
548         gpio_set_value(info->reset_gpio, 0);
549         msleep(100);
550         gpio_set_value(info->bt_wakeup_gpio, 1);
551         gpio_set_value(info->reset_gpio, 1);
552         msleep(100);
553
554         err = hci_h4p_wait_for_cts(info, 1, 10);
555         if (err < 0) {
556                 dev_err(info->dev, "No cts from bt chip\n");
557                 return err;
558         }
559
560         hci_h4p_set_rts(info, 1);
561
562         return 0;
563 }
564
565 /* hci callback functions */
566 static int hci_h4p_hci_flush(struct hci_dev *hdev)
567 {
568         struct hci_h4p_info *info;
569         info = hdev->driver_data;
570
571         skb_queue_purge(&info->txq);
572
573         return 0;
574 }
575
576 static int hci_h4p_hci_open(struct hci_dev *hdev)
577 {
578         struct hci_h4p_info *info;
579         int err;
580         struct sk_buff *neg_cmd_skb;
581         struct sk_buff_head fw_queue;
582
583         info = hdev->driver_data;
584
585         if (test_bit(HCI_RUNNING, &hdev->flags))
586                 return 0;
587
588         skb_queue_head_init(&fw_queue);
589         err = hci_h4p_read_fw(info, &fw_queue);
590         if (err < 0) {
591                 dev_err(info->dev, "Cannot read firmware\n");
592                 return err;
593         }
594         neg_cmd_skb = skb_dequeue(&fw_queue);
595         if (!neg_cmd_skb) {
596                 err = -EPROTO;
597                 goto err_clean;
598         }
599         info->alive_cmd_skb = skb_dequeue(&fw_queue);
600         if (!info->alive_cmd_skb) {
601                 err = -EPROTO;
602                 goto err_clean;
603         }
604
605         hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
606         hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
607
608         tasklet_enable(&info->tx_task);
609         tasklet_enable(&info->rx_task);
610         info->rx_state = WAIT_FOR_PKT_TYPE;
611         info->rx_count = 0;
612         info->garbage_bytes = 0;
613         info->rx_skb = NULL;
614         info->pm_enabled = 0;
615         init_completion(&info->fw_completion);
616
617         err = hci_h4p_reset(info);
618         if (err < 0)
619                 goto err_clean;
620
621         err = hci_h4p_send_negotiation(info, neg_cmd_skb);
622         neg_cmd_skb = NULL;
623         if (err < 0)
624                 goto err_clean;
625
626         err = hci_h4p_send_fw(info, &fw_queue);
627         if (err < 0) {
628                 dev_err(info->dev, "Sending firmware failed.\n");
629                 goto err_clean;
630         }
631
632         kfree_skb(info->alive_cmd_skb);
633         info->alive_cmd_skb = NULL;
634         info->pm_enabled = 1;
635         info->tx_pm_enabled = 1;
636         info->rx_pm_enabled = 0;
637         set_bit(HCI_RUNNING, &hdev->flags);
638
639         NBT_DBG("hci up and running\n");
640         return 0;
641
642 err_clean:
643         hci_h4p_hci_flush(hdev);
644         tasklet_disable(&info->tx_task);
645         tasklet_disable(&info->rx_task);
646         hci_h4p_reset_uart(info);
647         hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
648         hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
649         gpio_set_value(info->reset_gpio, 0);
650         gpio_set_value(info->bt_wakeup_gpio, 0);
651         skb_queue_purge(&fw_queue);
652         kfree_skb(neg_cmd_skb);
653         neg_cmd_skb = NULL;
654         kfree_skb(info->alive_cmd_skb);
655         info->alive_cmd_skb = NULL;
656         kfree_skb(info->rx_skb);
657
658         return err;
659 }
660
661 static int hci_h4p_hci_close(struct hci_dev *hdev)
662 {
663         struct hci_h4p_info *info = hdev->driver_data;
664
665         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
666                 return 0;
667
668         hci_h4p_hci_flush(hdev);
669         del_timer_sync(&info->tx_pm_timer);
670         del_timer_sync(&info->rx_pm_timer);
671         tasklet_disable(&info->tx_task);
672         tasklet_disable(&info->rx_task);
673         hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
674         hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
675         hci_h4p_reset_uart(info);
676         hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
677         hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
678         gpio_set_value(info->reset_gpio, 0);
679         gpio_set_value(info->bt_wakeup_gpio, 0);
680         kfree_skb(info->rx_skb);
681
682         return 0;
683 }
684
685 static void hci_h4p_hci_destruct(struct hci_dev *hdev)
686 {
687 }
688
689 static int hci_h4p_hci_send_frame(struct sk_buff *skb)
690 {
691         struct hci_h4p_info *info;
692         struct hci_dev *hdev = (struct hci_dev *)skb->dev;
693         int err = 0;
694
695         if (!hdev) {
696                 printk(KERN_WARNING "hci_h4p: Frame for unknown device\n");
697                 return -ENODEV;
698         }
699
700         NBT_DBG("dev %p, skb %p\n", hdev, skb);
701
702         info = hdev->driver_data;
703
704         if (!test_bit(HCI_RUNNING, &hdev->flags)) {
705                 dev_warn(info->dev, "Frame for non-running device\n");
706                 return -EIO;
707         }
708
709         switch (bt_cb(skb)->pkt_type) {
710         case HCI_COMMAND_PKT:
711                 hdev->stat.cmd_tx++;
712                 break;
713         case HCI_ACLDATA_PKT:
714                 hdev->stat.acl_tx++;
715                 break;
716         case HCI_SCODATA_PKT:
717                 hdev->stat.sco_tx++;
718                 break;
719         }
720
721         /* Push frame type to skb */
722         *skb_push(skb, 1) = (bt_cb(skb)->pkt_type);
723         /* We should allways send word aligned data to h4+ devices */
724         if (skb->len % 2) {
725                 err = skb_pad(skb, 1);
726         }
727         if (err)
728                 return err;
729
730         hci_h4p_enable_tx(info);
731         skb_queue_tail(&info->txq, skb);
732         tasklet_schedule(&info->tx_task);
733
734         return 0;
735 }
736
737 static int hci_h4p_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
738 {
739         return -ENOIOCTLCMD;
740 }
741
742 static int hci_h4p_register_hdev(struct hci_h4p_info *info)
743 {
744         struct hci_dev *hdev;
745
746         /* Initialize and register HCI device */
747
748         hdev = hci_alloc_dev();
749         if (!hdev) {
750                 dev_err(info->dev, "Can't allocate memory for device\n");
751                 return -ENOMEM;
752         }
753         info->hdev = hdev;
754
755         hdev->type = HCI_UART;
756         hdev->driver_data = info;
757
758         hdev->open = hci_h4p_hci_open;
759         hdev->close = hci_h4p_hci_close;
760         hdev->flush = hci_h4p_hci_flush;
761         hdev->send = hci_h4p_hci_send_frame;
762         hdev->destruct = hci_h4p_hci_destruct;
763         hdev->ioctl = hci_h4p_hci_ioctl;
764
765         hdev->owner = THIS_MODULE;
766
767         if (hci_register_dev(hdev) < 0) {
768                 dev_err(info->dev, "hci_h4p: Can't register HCI device %s.\n", hdev->name);
769                 return -ENODEV;
770         }
771
772         return 0;
773 }
774
775 static int hci_h4p_probe(struct platform_device *pdev)
776 {
777         struct omap_bluetooth_config *bt_config;
778         struct hci_h4p_info *info;
779         int irq, err;
780
781         dev_info(&pdev->dev, "Registering HCI H4P device\n");
782         info = kzalloc(sizeof(struct hci_h4p_info), GFP_KERNEL);
783         if (!info)
784                 return -ENOMEM;
785
786         info->dev = &pdev->dev;
787         info->pm_enabled = 0;
788         info->tx_pm_enabled = 0;
789         info->rx_pm_enabled = 0;
790         info->garbage_bytes = 0;
791         info->tx_clocks_en = 0;
792         info->rx_clocks_en = 0;
793         tasklet_init(&info->tx_task, hci_h4p_tx_tasklet, (unsigned long)info);
794         tasklet_init(&info->rx_task, hci_h4p_rx_tasklet, (unsigned long)info);
795         /* hci_h4p_hci_open assumes that tasklet is disabled in startup */
796         tasklet_disable(&info->tx_task);
797         tasklet_disable(&info->rx_task);
798         spin_lock_init(&info->lock);
799         spin_lock_init(&info->clocks_lock);
800         skb_queue_head_init(&info->txq);
801         init_timer(&info->tx_pm_timer);
802         info->tx_pm_timer.function = hci_h4p_tx_pm_timer;
803         info->tx_pm_timer.data = (unsigned long)info;
804         init_timer(&info->rx_pm_timer);
805         info->rx_pm_timer.function = hci_h4p_rx_pm_timer;
806         info->rx_pm_timer.data = (unsigned long)info;
807
808         if (pdev->dev.platform_data == NULL) {
809                 dev_err(&pdev->dev, "Could not get Bluetooth config data\n");
810                 return -ENODATA;
811         }
812
813         bt_config = pdev->dev.platform_data;
814         info->chip_type = bt_config->chip_type;
815         info->bt_wakeup_gpio = bt_config->bt_wakeup_gpio;
816         info->host_wakeup_gpio = bt_config->host_wakeup_gpio;
817         info->reset_gpio = bt_config->reset_gpio;
818         info->bt_sysclk = bt_config->bt_sysclk;
819
820         NBT_DBG("RESET gpio: %d\n", info->reset_gpio);
821         NBT_DBG("BTWU gpio: %d\n", info->bt_wakeup_gpio);
822         NBT_DBG("HOSTWU gpio: %d\n", info->host_wakeup_gpio);
823         NBT_DBG("Uart: %d\n", bt_config->bt_uart);
824         NBT_DBG("sysclk: %d\n", info->bt_sysclk);
825
826         err = gpio_request(info->reset_gpio, "BT reset");
827         if (err < 0) {
828                 dev_err(&pdev->dev, "Cannot get GPIO line %d\n",
829                         info->reset_gpio);
830                 kfree(info);
831                 goto cleanup;
832         }
833
834         err = gpio_request(info->bt_wakeup_gpio, "BT wakeup");
835         if (err < 0)
836         {
837                 dev_err(info->dev, "Cannot get GPIO line 0x%d",
838                         info->bt_wakeup_gpio);
839                 gpio_free(info->reset_gpio);
840                 kfree(info);
841                 goto cleanup;
842         }
843
844         err = gpio_request(info->host_wakeup_gpio, "BT host wakeup");
845         if (err < 0)
846         {
847                 dev_err(info->dev, "Cannot get GPIO line %d",
848                        info->host_wakeup_gpio);
849                 gpio_free(info->reset_gpio);
850                 gpio_free(info->bt_wakeup_gpio);
851                 kfree(info);
852                 goto cleanup;
853         }
854
855         gpio_direction_output(info->reset_gpio, 0);
856         gpio_direction_output(info->bt_wakeup_gpio, 0);
857         gpio_direction_input(info->host_wakeup_gpio);
858
859         switch (bt_config->bt_uart) {
860         case 1:
861                 if (cpu_is_omap16xx()) {
862                         irq = INT_UART1;
863                         info->uart_fclk = clk_get(NULL, "uart1_ck");
864                 } else if (cpu_is_omap24xx()) {
865                         irq = INT_24XX_UART1_IRQ;
866                         info->uart_iclk = clk_get(NULL, "uart1_ick");
867                         info->uart_fclk = clk_get(NULL, "uart1_fck");
868                 }
869                 /* FIXME: Use platform_get_resource for the port */
870                 info->uart_base = ioremap(OMAP_UART1_BASE, 0x16);
871                 if (!info->uart_base)
872                         goto cleanup;
873                 break;
874         case 2:
875                 if (cpu_is_omap16xx()) {
876                         irq = INT_UART2;
877                         info->uart_fclk = clk_get(NULL, "uart2_ck");
878                 } else {
879                         irq = INT_24XX_UART2_IRQ;
880                         info->uart_iclk = clk_get(NULL, "uart2_ick");
881                         info->uart_fclk = clk_get(NULL, "uart2_fck");
882                 }
883                 /* FIXME: Use platform_get_resource for the port */
884                 info->uart_base = ioremap(OMAP_UART2_BASE, 0x16);
885                 if (!info->uart_base)
886                         goto cleanup;
887                 break;
888         case 3:
889                 if (cpu_is_omap16xx()) {
890                         irq = INT_UART3;
891                         info->uart_fclk = clk_get(NULL, "uart3_ck");
892                 } else {
893                         irq = INT_24XX_UART3_IRQ;
894                         info->uart_iclk = clk_get(NULL, "uart3_ick");
895                         info->uart_fclk = clk_get(NULL, "uart3_fck");
896                 }
897                 /* FIXME: Use platform_get_resource for the port */
898                 info->uart_base = ioremap(OMAP_UART3_BASE, 0x16);
899                 if (!info->uart_base)
900                         goto cleanup;
901                 break;
902         default:
903                 dev_err(info->dev, "No uart defined\n");
904                 goto cleanup;
905         }
906
907         info->irq = irq;
908         err = request_irq(irq, hci_h4p_interrupt, 0, "hci_h4p", (void *)info);
909         if (err < 0) {
910                 dev_err(info->dev, "hci_h4p: unable to get IRQ %d\n", irq);
911                 goto cleanup;
912         }
913
914         err = request_irq(gpio_to_irq(info->host_wakeup_gpio),
915                           hci_h4p_wakeup_interrupt,
916                                 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
917                           "hci_h4p_wkup", (void *)info);
918         if (err < 0) {
919                 dev_err(info->dev, "hci_h4p: unable to get wakeup IRQ %d\n",
920                           gpio_to_irq(info->host_wakeup_gpio));
921                 free_irq(irq, (void *)info);
922                 goto cleanup;
923         }
924
925         hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
926         hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_CTS | UART_EFR_RTS);
927         err = hci_h4p_init_uart(info);
928         if (err < 0)
929                 goto cleanup_irq;
930         err = hci_h4p_reset(info);
931         if (err < 0)
932                 goto cleanup_irq;
933         err = hci_h4p_wait_for_cts(info, 1, 10);
934         if (err < 0)
935                 goto cleanup_irq;
936         hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
937
938         platform_set_drvdata(pdev, info);
939         err = hci_h4p_sysfs_create_files(info->dev);
940         if (err < 0)
941                 goto cleanup_irq;
942
943         if (hci_h4p_register_hdev(info) < 0) {
944                 dev_err(info->dev, "failed to register hci_h4p hci device\n");
945                 goto cleanup_irq;
946         }
947         gpio_set_value(info->reset_gpio, 0);
948
949         return 0;
950
951 cleanup_irq:
952         free_irq(irq, (void *)info);
953         free_irq(gpio_to_irq(info->host_wakeup_gpio), (void *)info);
954 cleanup:
955         gpio_set_value(info->reset_gpio, 0);
956         gpio_free(info->reset_gpio);
957         gpio_free(info->bt_wakeup_gpio);
958         gpio_free(info->host_wakeup_gpio);
959         kfree(info);
960
961         return err;
962
963 }
964
965 static int hci_h4p_remove(struct platform_device *dev)
966 {
967         struct hci_h4p_info *info;
968
969         info = platform_get_drvdata(dev);
970
971         hci_h4p_hci_close(info->hdev);
972         free_irq(gpio_to_irq(info->host_wakeup_gpio), (void *) info);
973         hci_free_dev(info->hdev);
974         gpio_free(info->reset_gpio);
975         gpio_free(info->bt_wakeup_gpio);
976         gpio_free(info->host_wakeup_gpio);
977         free_irq(info->irq, (void *) info);
978         kfree(info);
979
980         return 0;
981 }
982
983 static struct platform_driver hci_h4p_driver = {
984         .probe          = hci_h4p_probe,
985         .remove         = hci_h4p_remove,
986         .driver         = {
987                 .name   = "hci_h4p",
988         },
989 };
990
991 static int __init hci_h4p_init(void)
992 {
993         int err = 0;
994
995         /* Register the driver with LDM */
996         err = platform_driver_register(&hci_h4p_driver);
997         if (err < 0)
998                 printk(KERN_WARNING "failed to register hci_h4p driver\n");
999
1000         return err;
1001 }
1002
1003 static void __exit hci_h4p_exit(void)
1004 {
1005         platform_driver_unregister(&hci_h4p_driver);
1006 }
1007
1008 module_init(hci_h4p_init);
1009 module_exit(hci_h4p_exit);
1010
1011 MODULE_DESCRIPTION("h4 driver with nokia extensions");
1012 MODULE_LICENSE("GPL");
1013 MODULE_AUTHOR("Ville Tervo");