]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/bluetooth/hci_h4p/core.c
Merge current mainline tree into linux-omap tree
[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
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 #include <asm/arch/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                 omap_set_gpio_dataout(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                 omap_set_gpio_dataout(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                 hci_h4p_set_rts(info, 1);
235
236                 err = hci_h4p_wait_for_cts(info, 1, 100);
237                 if (err < 0)
238                         goto neg_ret;
239
240                 hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_CTS | UART_EFR_RTS);
241
242                 err = hci_h4p_send_alive_packet(info);
243                 if (err < 0)
244                         goto neg_ret;
245         } else {
246                 dev_err(info->dev, "Could not negotiate hci_h4p settings\n");
247                 err = -EINVAL;
248                 goto neg_ret;
249         }
250
251         kfree_skb(skb);
252         return;
253
254 neg_ret:
255         complete(&info->init_completion);
256         info->init_error = err;
257         kfree_skb(skb);
258 }
259
260 /* H4 packet handling functions */
261 static int hci_h4p_get_hdr_len(struct hci_h4p_info *info, u8 pkt_type)
262 {
263         long retval;
264
265         switch (pkt_type) {
266         case H4_EVT_PKT:
267                 retval = HCI_EVENT_HDR_SIZE;
268                 break;
269         case H4_ACL_PKT:
270                 retval = HCI_ACL_HDR_SIZE;
271                 break;
272         case H4_SCO_PKT:
273                 retval = HCI_SCO_HDR_SIZE;
274                 break;
275         case H4_NEG_PKT:
276                 retval = 11;
277                 break;
278         case H4_ALIVE_PKT:
279                 retval = 3;
280                 break;
281         default:
282                 dev_err(info->dev, "Unknown H4 packet type 0x%.2x\n", pkt_type);
283                 retval = -1;
284                 break;
285         }
286
287         return retval;
288 }
289
290 static unsigned int hci_h4p_get_data_len(struct hci_h4p_info *info,
291                                          struct sk_buff *skb)
292 {
293         long retval = -1;
294         struct hci_event_hdr *evt_hdr;
295         struct hci_acl_hdr *acl_hdr;
296         struct hci_sco_hdr *sco_hdr;
297
298         switch (bt_cb(skb)->pkt_type) {
299         case H4_EVT_PKT:
300                 evt_hdr = (struct hci_event_hdr *)skb->data;
301                 retval = evt_hdr->plen;
302                 break;
303         case H4_ACL_PKT:
304                 acl_hdr = (struct hci_acl_hdr *)skb->data;
305                 retval = le16_to_cpu(acl_hdr->dlen);
306                 break;
307         case H4_SCO_PKT:
308                 sco_hdr = (struct hci_sco_hdr *)skb->data;
309                 retval = sco_hdr->dlen;
310                 break;
311         case H4_NEG_PKT:
312                 retval = 0;
313                 break;
314         case H4_ALIVE_PKT:
315                 retval = 0;
316                 break;
317         }
318
319         return retval;
320 }
321
322 static inline void hci_h4p_recv_frame(struct hci_h4p_info *info,
323                                       struct sk_buff *skb)
324 {
325
326         if (unlikely(!test_bit(HCI_RUNNING, &info->hdev->flags))) {
327                 NBT_DBG("fw_event\n");
328                 hci_h4p_parse_fw_event(info, skb);
329         } else {
330                 hci_recv_frame(skb);
331                 NBT_DBG("Frame sent to upper layer\n");
332         }
333 }
334
335 static void hci_h4p_rx_tasklet(unsigned long data)
336 {
337         u8 byte;
338         unsigned long flags;
339         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
340
341         NBT_DBG("tasklet woke up\n");
342         NBT_DBG_TRANSFER("rx_tasklet woke up\ndata ");
343
344         while (hci_h4p_inb(info, UART_LSR) & UART_LSR_DR) {
345                 byte = hci_h4p_inb(info, UART_RX);
346                 if (info->garbage_bytes) {
347                         info->garbage_bytes--;
348                         continue;
349                 }
350                 if (info->rx_skb == NULL) {
351                         info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC | GFP_DMA);
352                         if (!info->rx_skb) {
353                                 dev_err(info->dev, "Can't allocate memory for new packet\n");
354                                 goto finish_task;
355                         }
356                         info->rx_state = WAIT_FOR_PKT_TYPE;
357                         info->rx_skb->dev = (void *)info->hdev;
358                 }
359                 info->hdev->stat.byte_rx++;
360                 NBT_DBG_TRANSFER_NF("0x%.2x  ", byte);
361                 switch (info->rx_state) {
362                 case WAIT_FOR_PKT_TYPE:
363                         bt_cb(info->rx_skb)->pkt_type = byte;
364                         info->rx_count = hci_h4p_get_hdr_len(info, byte);
365                         if (info->rx_count < 0) {
366                                 info->hdev->stat.err_rx++;
367                                 kfree_skb(info->rx_skb);
368                                 info->rx_skb = NULL;
369                         } else {
370                                 info->rx_state = WAIT_FOR_HEADER;
371                         }
372                         break;
373                 case WAIT_FOR_HEADER:
374                         info->rx_count--;
375                         *skb_put(info->rx_skb, 1) = byte;
376                         if (info->rx_count == 0) {
377                                 info->rx_count = hci_h4p_get_data_len(info, info->rx_skb);
378                                 if (info->rx_count > skb_tailroom(info->rx_skb)) {
379                                         dev_err(info->dev, "Frame is %ld bytes too long.\n",
380                                                info->rx_count - skb_tailroom(info->rx_skb));
381                                         kfree_skb(info->rx_skb);
382                                         info->rx_skb = NULL;
383                                         info->garbage_bytes = info->rx_count - skb_tailroom(info->rx_skb);
384                                         break;
385                                 }
386                                 info->rx_state = WAIT_FOR_DATA;
387
388                                 if (bt_cb(info->rx_skb)->pkt_type == H4_NEG_PKT) {
389                                         hci_h4p_negotiation_packet(info, info->rx_skb);
390                                         info->rx_skb = NULL;
391                                         info->rx_state = WAIT_FOR_PKT_TYPE;
392                                         goto finish_task;
393                                 }
394                                 if (bt_cb(info->rx_skb)->pkt_type == H4_ALIVE_PKT) {
395                                         hci_h4p_alive_packet(info, info->rx_skb);
396                                         info->rx_skb = NULL;
397                                         info->rx_state = WAIT_FOR_PKT_TYPE;
398                                         goto finish_task;
399                                 }
400                         }
401                         break;
402                 case WAIT_FOR_DATA:
403                         info->rx_count--;
404                         *skb_put(info->rx_skb, 1) = byte;
405                         if (info->rx_count == 0) {
406                                 /* H4+ devices should allways send word aligned packets */
407                                 if (!(info->rx_skb->len % 2)) {
408                                         info->garbage_bytes++;
409                                 }
410                                 hci_h4p_recv_frame(info, info->rx_skb);
411                                 info->rx_skb = NULL;
412                         }
413                         break;
414                 default:
415                         WARN_ON(1);
416                         break;
417                 }
418         }
419
420 finish_task:
421         spin_lock_irqsave(&info->lock, flags);
422         hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_RDI);
423         spin_unlock_irqrestore(&info->lock, flags);
424
425         NBT_DBG_TRANSFER_NF("\n");
426         NBT_DBG("rx_ended\n");
427 }
428
429 static void hci_h4p_tx_tasklet(unsigned long data)
430 {
431         unsigned int sent = 0;
432         unsigned long flags;
433         struct sk_buff *skb;
434         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
435
436         NBT_DBG("tasklet woke up\n");
437         NBT_DBG_TRANSFER("tx_tasklet woke up\n data ");
438
439         skb = skb_dequeue(&info->txq);
440         if (!skb) {
441                 /* No data in buffer */
442                 NBT_DBG("skb ready\n");
443                 hci_h4p_disable_tx(info);
444                 return;
445         }
446
447         /* Copy data to tx fifo */
448         while (!(hci_h4p_inb(info, UART_OMAP_SSR) & UART_OMAP_SSR_TXFULL) &&
449                (sent < skb->len)) {
450                 NBT_DBG_TRANSFER_NF("0x%.2x ", skb->data[sent]);
451                 hci_h4p_outb(info, UART_TX, skb->data[sent]);
452                 sent++;
453         }
454
455         info->hdev->stat.byte_tx += sent;
456         NBT_DBG_TRANSFER_NF("\n");
457         if (skb->len == sent) {
458                 kfree_skb(skb);
459         } else {
460                 skb_pull(skb, sent);
461                 skb_queue_head(&info->txq, skb);
462         }
463
464         spin_lock_irqsave(&info->lock, flags);
465         hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_THRI);
466         spin_unlock_irqrestore(&info->lock, flags);
467 }
468
469 static irqreturn_t hci_h4p_interrupt(int irq, void *data)
470 {
471         struct hci_h4p_info *info = (struct hci_h4p_info *)data;
472         u8 iir, msr;
473         int ret;
474         unsigned long flags;
475
476         ret = IRQ_NONE;
477
478         iir = hci_h4p_inb(info, UART_IIR);
479         if (iir & UART_IIR_NO_INT) {
480                 dev_err(info->dev, "Interrupt but no reason irq 0x%.2x\n", iir);
481                 return IRQ_HANDLED;
482         }
483
484         NBT_DBG("In interrupt handler iir 0x%.2x\n", iir);
485
486         iir &= UART_IIR_ID;
487
488         if (iir == UART_IIR_MSI) {
489                 msr = hci_h4p_inb(info, UART_MSR);
490                 ret = IRQ_HANDLED;
491         }
492         if (iir == UART_IIR_RLSI) {
493                 hci_h4p_inb(info, UART_RX);
494                 hci_h4p_inb(info, UART_LSR);
495                 ret = IRQ_HANDLED;
496         }
497
498         if (iir == UART_IIR_RDI) {
499                 spin_lock_irqsave(&info->lock, flags);
500                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_RDI);
501                 spin_unlock_irqrestore(&info->lock, flags);
502                 tasklet_schedule(&info->rx_task);
503                 ret = IRQ_HANDLED;
504         }
505
506         if (iir == UART_IIR_THRI) {
507                 spin_lock_irqsave(&info->lock, flags);
508                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_THRI);
509                 spin_unlock_irqrestore(&info->lock, flags);
510                 tasklet_schedule(&info->tx_task);
511                 ret = IRQ_HANDLED;
512         }
513
514
515         return ret;
516 }
517
518 static irqreturn_t hci_h4p_wakeup_interrupt(int irq, void *dev_inst)
519 {
520         struct hci_h4p_info *info = dev_inst;
521         int should_wakeup;
522         struct hci_dev *hdev;
523
524         if (!info->hdev)
525                 return IRQ_HANDLED;
526
527         hdev = info->hdev;
528
529         if (!test_bit(HCI_RUNNING, &hdev->flags))
530                 return IRQ_HANDLED;
531
532         should_wakeup = omap_get_gpio_datain(info->host_wakeup_gpio);
533         NBT_DBG_POWER("gpio interrupt %d\n", should_wakeup);
534         if (should_wakeup) {
535                 hci_h4p_enable_rx(info);
536         } else {
537                 hci_h4p_disable_rx(info);
538         }
539
540         return IRQ_HANDLED;
541 }
542
543 static int hci_h4p_reset(struct hci_h4p_info *info)
544 {
545         int err;
546
547         hci_h4p_init_uart(info);
548         hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_CTS | UART_EFR_RTS);
549         hci_h4p_set_rts(info, 0);
550
551         omap_set_gpio_dataout(info->reset_gpio, 0);
552         msleep(100);
553         omap_set_gpio_dataout(info->bt_wakeup_gpio, 1);
554         omap_set_gpio_dataout(info->reset_gpio, 1);
555         msleep(100);
556
557         err = hci_h4p_wait_for_cts(info, 1, 10);
558         if (err < 0) {
559                 dev_err(info->dev, "No cts from bt chip\n");
560                 return err;
561         }
562
563         hci_h4p_set_rts(info, 1);
564
565         return 0;
566 }
567
568 /* hci callback functions */
569 static int hci_h4p_hci_flush(struct hci_dev *hdev)
570 {
571         struct hci_h4p_info *info;
572         info = hdev->driver_data;
573
574         skb_queue_purge(&info->txq);
575
576         return 0;
577 }
578
579 static int hci_h4p_hci_open(struct hci_dev *hdev)
580 {
581         struct hci_h4p_info *info;
582         int err;
583         struct sk_buff *neg_cmd_skb;
584         struct sk_buff_head fw_queue;
585
586         info = hdev->driver_data;
587
588         if (test_bit(HCI_RUNNING, &hdev->flags))
589                 return 0;
590
591         skb_queue_head_init(&fw_queue);
592         err = hci_h4p_read_fw(info, &fw_queue);
593         if (err < 0) {
594                 dev_err(info->dev, "Cannot read firmware\n");
595                 return err;
596         }
597         neg_cmd_skb = skb_dequeue(&fw_queue);
598         if (!neg_cmd_skb) {
599                 err = -EPROTO;
600                 goto err_clean;
601         }
602         info->alive_cmd_skb = skb_dequeue(&fw_queue);
603         if (!info->alive_cmd_skb) {
604                 err = -EPROTO;
605                 goto err_clean;
606         }
607
608         hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
609         hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
610
611         tasklet_enable(&info->tx_task);
612         tasklet_enable(&info->rx_task);
613         info->rx_state = WAIT_FOR_PKT_TYPE;
614         info->rx_count = 0;
615         info->garbage_bytes = 0;
616         info->rx_skb = NULL;
617         info->pm_enabled = 0;
618         init_completion(&info->fw_completion);
619
620         err = hci_h4p_reset(info);
621         if (err < 0)
622                 goto err_clean;
623
624         err = hci_h4p_send_negotiation(info, neg_cmd_skb);
625         neg_cmd_skb = NULL;
626         if (err < 0)
627                 goto err_clean;
628
629         hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_CTS | UART_EFR_RTS);
630
631         err = hci_h4p_send_fw(info, &fw_queue);
632         if (err < 0) {
633                 dev_err(info->dev, "Sending firmware failed.\n");
634                 goto err_clean;
635         }
636
637         kfree_skb(info->alive_cmd_skb);
638         info->alive_cmd_skb = NULL;
639         info->pm_enabled = 1;
640         info->tx_pm_enabled = 1;
641         info->rx_pm_enabled = 0;
642         set_bit(HCI_RUNNING, &hdev->flags);
643
644         NBT_DBG("hci up and running\n");
645         return 0;
646
647 err_clean:
648         hci_h4p_hci_flush(hdev);
649         tasklet_disable(&info->tx_task);
650         tasklet_disable(&info->rx_task);
651         hci_h4p_reset_uart(info);
652         hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
653         hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
654         omap_set_gpio_dataout(info->reset_gpio, 0);
655         omap_set_gpio_dataout(info->bt_wakeup_gpio, 0);
656         skb_queue_purge(&fw_queue);
657         kfree_skb(neg_cmd_skb);
658         neg_cmd_skb = NULL;
659         kfree_skb(info->alive_cmd_skb);
660         info->alive_cmd_skb = NULL;
661         kfree_skb(info->rx_skb);
662
663         return err;
664 }
665
666 static int hci_h4p_hci_close(struct hci_dev *hdev)
667 {
668         struct hci_h4p_info *info = hdev->driver_data;
669
670         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
671                 return 0;
672
673         hci_h4p_hci_flush(hdev);
674         del_timer_sync(&info->tx_pm_timer);
675         del_timer_sync(&info->rx_pm_timer);
676         tasklet_disable(&info->tx_task);
677         tasklet_disable(&info->rx_task);
678         hci_h4p_reset_uart(info);
679         hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
680         hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
681         omap_set_gpio_dataout(info->reset_gpio, 0);
682         omap_set_gpio_dataout(info->bt_wakeup_gpio, 0);
683         kfree_skb(info->rx_skb);
684
685         return 0;
686 }
687
688 static void hci_h4p_hci_destruct(struct hci_dev *hdev)
689 {
690 }
691
692 static int hci_h4p_hci_send_frame(struct sk_buff *skb)
693 {
694         struct hci_h4p_info *info;
695         struct hci_dev *hdev = (struct hci_dev *)skb->dev;
696         int err = 0;
697
698         if (!hdev) {
699                 printk(KERN_WARNING "hci_h4p: Frame for unknown device\n");
700                 return -ENODEV;
701         }
702
703         NBT_DBG("dev %p, skb %p\n", hdev, skb);
704
705         info = hdev->driver_data;
706
707         if (!test_bit(HCI_RUNNING, &hdev->flags)) {
708                 dev_warn(info->dev, "Frame for non-running device\n");
709                 return -EIO;
710         }
711
712         switch (bt_cb(skb)->pkt_type) {
713         case HCI_COMMAND_PKT:
714                 hdev->stat.cmd_tx++;
715                 break;
716         case HCI_ACLDATA_PKT:
717                 hdev->stat.acl_tx++;
718                 break;
719         case HCI_SCODATA_PKT:
720                 hdev->stat.sco_tx++;
721                 break;
722         }
723
724         /* Push frame type to skb */
725         *skb_push(skb, 1) = (bt_cb(skb)->pkt_type);
726         /* We should allways send word aligned data to h4+ devices */
727         if (skb->len % 2) {
728                 err = skb_pad(skb, 1);
729         }
730         if (err)
731                 return err;
732
733         hci_h4p_enable_tx(info);
734         skb_queue_tail(&info->txq, skb);
735         tasklet_schedule(&info->tx_task);
736
737         return 0;
738 }
739
740 static int hci_h4p_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
741 {
742         return -ENOIOCTLCMD;
743 }
744
745 static int hci_h4p_register_hdev(struct hci_h4p_info *info)
746 {
747         struct hci_dev *hdev;
748
749         /* Initialize and register HCI device */
750
751         hdev = hci_alloc_dev();
752         if (!hdev) {
753                 dev_err(info->dev, "Can't allocate memory for device\n");
754                 return -ENOMEM;
755         }
756         info->hdev = hdev;
757
758         hdev->type = HCI_UART;
759         hdev->driver_data = info;
760
761         hdev->open = hci_h4p_hci_open;
762         hdev->close = hci_h4p_hci_close;
763         hdev->flush = hci_h4p_hci_flush;
764         hdev->send = hci_h4p_hci_send_frame;
765         hdev->destruct = hci_h4p_hci_destruct;
766         hdev->ioctl = hci_h4p_hci_ioctl;
767
768         hdev->owner = THIS_MODULE;
769
770         if (hci_register_dev(hdev) < 0) {
771                 dev_err(info->dev, "hci_h4p: Can't register HCI device %s.\n", hdev->name);
772                 return -ENODEV;
773         }
774
775         return 0;
776 }
777
778 static int hci_h4p_probe(struct platform_device *pdev)
779 {
780         struct omap_bluetooth_config *bt_config;
781         struct hci_h4p_info *info;
782         int irq, err;
783
784         dev_info(&pdev->dev, "Registering HCI H4P device\n");
785         info = kzalloc(sizeof(struct hci_h4p_info), GFP_KERNEL);
786         if (!info)
787                 return -ENOMEM;
788
789         info->dev = &pdev->dev;
790         info->pm_enabled = 0;
791         info->tx_pm_enabled = 0;
792         info->rx_pm_enabled = 0;
793         info->garbage_bytes = 0;
794         info->tx_clocks_en = 0;
795         info->rx_clocks_en = 0;
796         tasklet_init(&info->tx_task, hci_h4p_tx_tasklet, (unsigned long)info);
797         tasklet_init(&info->rx_task, hci_h4p_rx_tasklet, (unsigned long)info);
798         /* hci_h4p_hci_open assumes that tasklet is disabled in startup */
799         tasklet_disable(&info->tx_task);
800         tasklet_disable(&info->rx_task);
801         spin_lock_init(&info->lock);
802         spin_lock_init(&info->clocks_lock);
803         skb_queue_head_init(&info->txq);
804         init_timer(&info->tx_pm_timer);
805         info->tx_pm_timer.function = hci_h4p_tx_pm_timer;
806         info->tx_pm_timer.data = (unsigned long)info;
807         init_timer(&info->rx_pm_timer);
808         info->rx_pm_timer.function = hci_h4p_rx_pm_timer;
809         info->rx_pm_timer.data = (unsigned long)info;
810
811         if (pdev->dev.platform_data == NULL) {
812                 dev_err(&pdev->dev, "Could not get Bluetooth config data\n");
813                 return -ENODATA;
814         }
815
816         bt_config = pdev->dev.platform_data;
817         info->chip_type = bt_config->chip_type;
818         info->bt_wakeup_gpio = bt_config->bt_wakeup_gpio;
819         info->host_wakeup_gpio = bt_config->host_wakeup_gpio;
820         info->reset_gpio = bt_config->reset_gpio;
821         info->bt_sysclk = bt_config->bt_sysclk;
822
823         NBT_DBG("RESET gpio: %d\n", info->reset_gpio);
824         NBT_DBG("BTWU gpio: %d\n", info->bt_wakeup_gpio);
825         NBT_DBG("HOSTWU gpio: %d\n", info->host_wakeup_gpio);
826         NBT_DBG("Uart: %d\n", bt_config->bt_uart);
827         NBT_DBG("sysclk: %d\n", info->bt_sysclk);
828
829         err = omap_request_gpio(info->reset_gpio);
830         if (err < 0) {
831                 dev_err(&pdev->dev, "Cannot get GPIO line %d\n",
832                         info->reset_gpio);
833                 kfree(info);
834                 goto cleanup;
835         }
836
837         err = omap_request_gpio(info->bt_wakeup_gpio);
838         if (err < 0)
839         {
840                 dev_err(info->dev, "Cannot get GPIO line 0x%d",
841                         info->bt_wakeup_gpio);
842                 omap_free_gpio(info->reset_gpio);
843                 kfree(info);
844                 goto cleanup;
845         }
846
847         err = omap_request_gpio(info->host_wakeup_gpio);
848         if (err < 0)
849         {
850                 dev_err(info->dev, "Cannot get GPIO line %d",
851                        info->host_wakeup_gpio);
852                 omap_free_gpio(info->reset_gpio);
853                 omap_free_gpio(info->bt_wakeup_gpio);
854                 kfree(info);
855                 goto cleanup;
856         }
857
858         omap_set_gpio_direction(info->reset_gpio, 0);
859         omap_set_gpio_direction(info->bt_wakeup_gpio, 0);
860         omap_set_gpio_direction(info->host_wakeup_gpio, 1);
861
862         switch (bt_config->bt_uart) {
863         case 1:
864                 if (cpu_is_omap16xx()) {
865                         irq = INT_UART1;
866                         info->uart_fclk = clk_get(NULL, "uart1_ck");
867                 } else if (cpu_is_omap24xx()) {
868                         irq = INT_24XX_UART1_IRQ;
869                         info->uart_iclk = clk_get(NULL, "uart1_ick");
870                         info->uart_fclk = clk_get(NULL, "uart1_fck");
871                 }
872                 info->uart_base = (void *)io_p2v(OMAP_UART1_BASE);
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                 info->uart_base = (void *)io_p2v(OMAP_UART2_BASE);
884                 break;
885         case 3:
886                 if (cpu_is_omap16xx()) {
887                         irq = INT_UART3;
888                         info->uart_fclk = clk_get(NULL, "uart3_ck");
889                 } else {
890                         irq = INT_24XX_UART3_IRQ;
891                         info->uart_iclk = clk_get(NULL, "uart3_ick");
892                         info->uart_fclk = clk_get(NULL, "uart3_fck");
893                 }
894                 info->uart_base = (void *)io_p2v(OMAP_UART3_BASE);
895                 break;
896         default:
897                 dev_err(info->dev, "No uart defined\n");
898                 goto cleanup;
899         }
900
901         info->irq = irq;
902         err = request_irq(irq, hci_h4p_interrupt, 0, "hci_h4p", (void *)info);
903         if (err < 0) {
904                 dev_err(info->dev, "hci_h4p: unable to get IRQ %d\n", irq);
905                 goto cleanup;
906         }
907
908         err = request_irq(OMAP_GPIO_IRQ(info->host_wakeup_gpio),
909                           hci_h4p_wakeup_interrupt, SA_TRIGGER_FALLING | SA_TRIGGER_RISING,
910                           "hci_h4p_wkup", (void *)info);
911         if (err < 0) {
912                 dev_err(info->dev, "hci_h4p: unable to get wakeup IRQ %d\n",
913                           OMAP_GPIO_IRQ(info->host_wakeup_gpio));
914                 free_irq(irq, (void *)info);
915                 goto cleanup;
916         }
917
918         hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
919         err = hci_h4p_init_uart(info);
920         if (err < 0)
921                 goto cleanup_irq;
922         hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_CTS | UART_EFR_RTS);
923         hci_h4p_set_rts(info, 0);
924         err = hci_h4p_reset(info);
925         if (err < 0)
926                 goto cleanup_irq;
927         err = hci_h4p_wait_for_cts(info, 1, 10);
928         if (err < 0)
929                 goto cleanup_irq;
930         hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
931
932         platform_set_drvdata(pdev, info);
933         err = hci_h4p_sysfs_create_files(info->dev);
934         if (err < 0)
935                 goto cleanup_irq;
936
937         if (hci_h4p_register_hdev(info) < 0) {
938                 dev_err(info->dev, "failed to register hci_h4p hci device\n");
939                 goto cleanup_irq;
940         }
941         omap_set_gpio_dataout(info->reset_gpio, 0);
942
943         return 0;
944
945 cleanup_irq:
946         free_irq(irq, (void *)info);
947         free_irq(OMAP_GPIO_IRQ(info->host_wakeup_gpio), (void *)info);
948 cleanup:
949         omap_set_gpio_dataout(info->reset_gpio, 0);
950         omap_free_gpio(info->reset_gpio);
951         omap_free_gpio(info->bt_wakeup_gpio);
952         omap_free_gpio(info->host_wakeup_gpio);
953         kfree(info);
954
955         return err;
956
957 }
958
959 static int hci_h4p_remove(struct platform_device *dev)
960 {
961         struct hci_h4p_info *info;
962
963         info = platform_get_drvdata(dev);
964
965         hci_h4p_hci_close(info->hdev);
966         free_irq(OMAP_GPIO_IRQ(info->host_wakeup_gpio), (void *) info);
967         hci_free_dev(info->hdev);
968         omap_free_gpio(info->reset_gpio);
969         omap_free_gpio(info->bt_wakeup_gpio);
970         omap_free_gpio(info->host_wakeup_gpio);
971         free_irq(info->irq, (void *) info);
972         kfree(info);
973
974         return 0;
975 }
976
977 static struct platform_driver hci_h4p_driver = {
978         .probe          = hci_h4p_probe,
979         .remove         = hci_h4p_remove,
980         .driver         = {
981                 .name   = "hci_h4p",
982         },
983 };
984
985 static int __init hci_h4p_init(void)
986 {
987         int err = 0;
988
989         /* Register the driver with LDM */
990         err = platform_driver_register(&hci_h4p_driver);
991         if (err < 0)
992                 printk(KERN_WARNING "failed to register hci_h4p driver\n");
993
994         return err;
995 }
996
997 static void __exit hci_h4p_exit(void)
998 {
999         platform_driver_unregister(&hci_h4p_driver);
1000 }
1001
1002 module_init(hci_h4p_init);
1003 module_exit(hci_h4p_exit);
1004
1005 MODULE_DESCRIPTION("h4 driver with nokia extensions");
1006 MODULE_LICENSE("GPL");
1007 MODULE_AUTHOR("Ville Tervo");