Merge branch 'x86-irq-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / staging / nokia_h4p / nokia_core.c
1 /*
2 * This file is part of Nokia H4P bluetooth driver
3 *
4 * Copyright (C) 2005-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 * Thanks to all the Nokia people that helped with this driver,
21 * including Ville Tervo and Roger Quadros.
22 *
23 * Power saving functionality was removed from this driver to make
24 * merging easier.
25 */
26
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/errno.h>
31 #include <linux/delay.h>
32 #include <linux/spinlock.h>
33 #include <linux/serial_reg.h>
34 #include <linux/skbuff.h>
35 #include <linux/device.h>
36 #include <linux/platform_device.h>
37 #include <linux/clk.h>
38 #include <linux/interrupt.h>
39 #include <linux/gpio.h>
40 #include <linux/timer.h>
41 #include <linux/kthread.h>
42 #include <linux/io.h>
43 #include <linux/completion.h>
44 #include <linux/sizes.h>
45
46 #include <net/bluetooth/bluetooth.h>
47 #include <net/bluetooth/hci_core.h>
48 #include <net/bluetooth/hci.h>
49
50 #include <linux/platform_data/bt-nokia-h4p.h>
51
52 #include "hci_h4p.h"
53
54 /* This should be used in function that cannot release clocks */
55 static void hci_h4p_set_clk(struct hci_h4p_info *info, int *clock, int enable)
56 {
57 unsigned long flags;
58
59 spin_lock_irqsave(&info->clocks_lock, flags);
60 if (enable && !*clock) {
61 BT_DBG("Enabling %p", clock);
62 clk_prepare_enable(info->uart_fclk);
63 clk_prepare_enable(info->uart_iclk);
64 if (atomic_read(&info->clk_users) == 0)
65 hci_h4p_restore_regs(info);
66 atomic_inc(&info->clk_users);
67 }
68
69 if (!enable && *clock) {
70 BT_DBG("Disabling %p", clock);
71 if (atomic_dec_and_test(&info->clk_users))
72 hci_h4p_store_regs(info);
73 clk_disable_unprepare(info->uart_fclk);
74 clk_disable_unprepare(info->uart_iclk);
75 }
76
77 *clock = enable;
78 spin_unlock_irqrestore(&info->clocks_lock, flags);
79 }
80
81 static void hci_h4p_lazy_clock_release(unsigned long data)
82 {
83 struct hci_h4p_info *info = (struct hci_h4p_info *)data;
84 unsigned long flags;
85
86 spin_lock_irqsave(&info->lock, flags);
87 if (!info->tx_enabled)
88 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
89 spin_unlock_irqrestore(&info->lock, flags);
90 }
91
92 /* Power management functions */
93 void hci_h4p_smart_idle(struct hci_h4p_info *info, bool enable)
94 {
95 u8 v;
96
97 v = hci_h4p_inb(info, UART_OMAP_SYSC);
98 v &= ~(UART_OMAP_SYSC_IDLEMASK);
99
100 if (enable)
101 v |= UART_OMAP_SYSC_SMART_IDLE;
102 else
103 v |= UART_OMAP_SYSC_NO_IDLE;
104
105 hci_h4p_outb(info, UART_OMAP_SYSC, v);
106 }
107
108 static inline void h4p_schedule_pm(struct hci_h4p_info *info)
109 {
110 }
111
112 static void hci_h4p_disable_tx(struct hci_h4p_info *info)
113 {
114 if (!info->pm_enabled)
115 return;
116
117 /* Re-enable smart-idle */
118 hci_h4p_smart_idle(info, 1);
119
120 gpio_set_value(info->bt_wakeup_gpio, 0);
121 mod_timer(&info->lazy_release, jiffies + msecs_to_jiffies(100));
122 info->tx_enabled = 0;
123 }
124
125 void hci_h4p_enable_tx(struct hci_h4p_info *info)
126 {
127 unsigned long flags;
128
129 if (!info->pm_enabled)
130 return;
131
132 h4p_schedule_pm(info);
133
134 spin_lock_irqsave(&info->lock, flags);
135 del_timer(&info->lazy_release);
136 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
137 info->tx_enabled = 1;
138 gpio_set_value(info->bt_wakeup_gpio, 1);
139 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) |
140 UART_IER_THRI);
141 /*
142 * Disable smart-idle as UART TX interrupts
143 * are not wake-up capable
144 */
145 hci_h4p_smart_idle(info, 0);
146
147 spin_unlock_irqrestore(&info->lock, flags);
148 }
149
150 static void hci_h4p_disable_rx(struct hci_h4p_info *info)
151 {
152 if (!info->pm_enabled)
153 return;
154
155 info->rx_enabled = 0;
156
157 if (hci_h4p_inb(info, UART_LSR) & UART_LSR_DR)
158 return;
159
160 if (!(hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT))
161 return;
162
163 __hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_RTS);
164 info->autorts = 0;
165 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
166 }
167
168 static void hci_h4p_enable_rx(struct hci_h4p_info *info)
169 {
170 if (!info->pm_enabled)
171 return;
172
173 h4p_schedule_pm(info);
174
175 hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
176 info->rx_enabled = 1;
177
178 if (!(hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT))
179 return;
180
181 __hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_RTS);
182 info->autorts = 1;
183 }
184
185 /* Negotiation functions */
186 int hci_h4p_send_alive_packet(struct hci_h4p_info *info)
187 {
188 struct hci_h4p_alive_hdr *hdr;
189 struct hci_h4p_alive_pkt *pkt;
190 struct sk_buff *skb;
191 unsigned long flags;
192 int len;
193
194 BT_DBG("Sending alive packet");
195
196 len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt);
197 skb = bt_skb_alloc(len, GFP_KERNEL);
198 if (!skb)
199 return -ENOMEM;
200
201 memset(skb->data, 0x00, len);
202 *skb_put(skb, 1) = H4_ALIVE_PKT;
203 hdr = (struct hci_h4p_alive_hdr *)skb_put(skb, sizeof(*hdr));
204 hdr->dlen = sizeof(*pkt);
205 pkt = (struct hci_h4p_alive_pkt *)skb_put(skb, sizeof(*pkt));
206 pkt->mid = H4P_ALIVE_REQ;
207
208 skb_queue_tail(&info->txq, skb);
209 spin_lock_irqsave(&info->lock, flags);
210 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) |
211 UART_IER_THRI);
212 spin_unlock_irqrestore(&info->lock, flags);
213
214 BT_DBG("Alive packet sent");
215
216 return 0;
217 }
218
219 static void hci_h4p_alive_packet(struct hci_h4p_info *info,
220 struct sk_buff *skb)
221 {
222 struct hci_h4p_alive_hdr *hdr;
223 struct hci_h4p_alive_pkt *pkt;
224
225 BT_DBG("Received alive packet");
226 hdr = (struct hci_h4p_alive_hdr *)skb->data;
227 if (hdr->dlen != sizeof(*pkt)) {
228 dev_err(info->dev, "Corrupted alive message\n");
229 info->init_error = -EIO;
230 goto finish_alive;
231 }
232
233 pkt = (struct hci_h4p_alive_pkt *)skb_pull(skb, sizeof(*hdr));
234 if (pkt->mid != H4P_ALIVE_RESP) {
235 dev_err(info->dev, "Could not negotiate hci_h4p settings\n");
236 info->init_error = -EINVAL;
237 }
238
239 finish_alive:
240 complete(&info->init_completion);
241 kfree_skb(skb);
242 }
243
244 static int hci_h4p_send_negotiation(struct hci_h4p_info *info)
245 {
246 struct hci_h4p_neg_cmd *neg_cmd;
247 struct hci_h4p_neg_hdr *neg_hdr;
248 struct sk_buff *skb;
249 unsigned long flags;
250 int err, len;
251 u16 sysclk;
252
253 BT_DBG("Sending negotiation..");
254
255 switch (info->bt_sysclk) {
256 case 1:
257 sysclk = 12000;
258 break;
259 case 2:
260 sysclk = 38400;
261 break;
262 default:
263 return -EINVAL;
264 }
265
266 len = sizeof(*neg_cmd) + sizeof(*neg_hdr) + H4_TYPE_SIZE;
267 skb = bt_skb_alloc(len, GFP_KERNEL);
268 if (!skb)
269 return -ENOMEM;
270
271 memset(skb->data, 0x00, len);
272 *skb_put(skb, 1) = H4_NEG_PKT;
273 neg_hdr = (struct hci_h4p_neg_hdr *)skb_put(skb, sizeof(*neg_hdr));
274 neg_cmd = (struct hci_h4p_neg_cmd *)skb_put(skb, sizeof(*neg_cmd));
275
276 neg_hdr->dlen = sizeof(*neg_cmd);
277 neg_cmd->ack = H4P_NEG_REQ;
278 neg_cmd->baud = cpu_to_le16(BT_BAUDRATE_DIVIDER/MAX_BAUD_RATE);
279 neg_cmd->proto = H4P_PROTO_BYTE;
280 neg_cmd->sys_clk = cpu_to_le16(sysclk);
281
282 hci_h4p_change_speed(info, INIT_SPEED);
283
284 hci_h4p_set_rts(info, 1);
285 info->init_error = 0;
286 init_completion(&info->init_completion);
287 skb_queue_tail(&info->txq, skb);
288 spin_lock_irqsave(&info->lock, flags);
289 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) |
290 UART_IER_THRI);
291 spin_unlock_irqrestore(&info->lock, flags);
292
293 if (!wait_for_completion_interruptible_timeout(&info->init_completion,
294 msecs_to_jiffies(1000)))
295 return -ETIMEDOUT;
296
297 if (info->init_error < 0)
298 return info->init_error;
299
300 /* Change to operational settings */
301 hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_RTS);
302 hci_h4p_set_rts(info, 0);
303 hci_h4p_change_speed(info, MAX_BAUD_RATE);
304
305 err = hci_h4p_wait_for_cts(info, 1, 100);
306 if (err < 0)
307 return err;
308
309 hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_RTS);
310 init_completion(&info->init_completion);
311 err = hci_h4p_send_alive_packet(info);
312
313 if (err < 0)
314 return err;
315
316 if (!wait_for_completion_interruptible_timeout(&info->init_completion,
317 msecs_to_jiffies(1000)))
318 return -ETIMEDOUT;
319
320 if (info->init_error < 0)
321 return info->init_error;
322
323 BT_DBG("Negotiation successful");
324 return 0;
325 }
326
327 static void hci_h4p_negotiation_packet(struct hci_h4p_info *info,
328 struct sk_buff *skb)
329 {
330 struct hci_h4p_neg_hdr *hdr;
331 struct hci_h4p_neg_evt *evt;
332
333 hdr = (struct hci_h4p_neg_hdr *)skb->data;
334 if (hdr->dlen != sizeof(*evt)) {
335 info->init_error = -EIO;
336 goto finish_neg;
337 }
338
339 evt = (struct hci_h4p_neg_evt *)skb_pull(skb, sizeof(*hdr));
340
341 if (evt->ack != H4P_NEG_ACK) {
342 dev_err(info->dev, "Could not negotiate hci_h4p settings\n");
343 info->init_error = -EINVAL;
344 }
345
346 info->man_id = evt->man_id;
347 info->ver_id = evt->ver_id;
348
349 finish_neg:
350
351 complete(&info->init_completion);
352 kfree_skb(skb);
353 }
354
355 /* H4 packet handling functions */
356 static int hci_h4p_get_hdr_len(struct hci_h4p_info *info, u8 pkt_type)
357 {
358 long retval;
359
360 switch (pkt_type) {
361 case H4_EVT_PKT:
362 retval = HCI_EVENT_HDR_SIZE;
363 break;
364 case H4_ACL_PKT:
365 retval = HCI_ACL_HDR_SIZE;
366 break;
367 case H4_SCO_PKT:
368 retval = HCI_SCO_HDR_SIZE;
369 break;
370 case H4_NEG_PKT:
371 retval = H4P_NEG_HDR_SIZE;
372 break;
373 case H4_ALIVE_PKT:
374 retval = H4P_ALIVE_HDR_SIZE;
375 break;
376 case H4_RADIO_PKT:
377 retval = H4_RADIO_HDR_SIZE;
378 break;
379 default:
380 dev_err(info->dev, "Unknown H4 packet type 0x%.2x\n", pkt_type);
381 retval = -1;
382 break;
383 }
384
385 return retval;
386 }
387
388 static unsigned int hci_h4p_get_data_len(struct hci_h4p_info *info,
389 struct sk_buff *skb)
390 {
391 long retval = -1;
392 struct hci_acl_hdr *acl_hdr;
393 struct hci_sco_hdr *sco_hdr;
394 struct hci_event_hdr *evt_hdr;
395 struct hci_h4p_neg_hdr *neg_hdr;
396 struct hci_h4p_alive_hdr *alive_hdr;
397 struct hci_h4p_radio_hdr *radio_hdr;
398
399 switch (bt_cb(skb)->pkt_type) {
400 case H4_EVT_PKT:
401 evt_hdr = (struct hci_event_hdr *)skb->data;
402 retval = evt_hdr->plen;
403 break;
404 case H4_ACL_PKT:
405 acl_hdr = (struct hci_acl_hdr *)skb->data;
406 retval = le16_to_cpu(acl_hdr->dlen);
407 break;
408 case H4_SCO_PKT:
409 sco_hdr = (struct hci_sco_hdr *)skb->data;
410 retval = sco_hdr->dlen;
411 break;
412 case H4_RADIO_PKT:
413 radio_hdr = (struct hci_h4p_radio_hdr *)skb->data;
414 retval = radio_hdr->dlen;
415 break;
416 case H4_NEG_PKT:
417 neg_hdr = (struct hci_h4p_neg_hdr *)skb->data;
418 retval = neg_hdr->dlen;
419 break;
420 case H4_ALIVE_PKT:
421 alive_hdr = (struct hci_h4p_alive_hdr *)skb->data;
422 retval = alive_hdr->dlen;
423 break;
424 }
425
426 return retval;
427 }
428
429 static inline void hci_h4p_recv_frame(struct hci_h4p_info *info,
430 struct sk_buff *skb)
431 {
432 if (unlikely(!test_bit(HCI_RUNNING, &info->hdev->flags))) {
433 switch (bt_cb(skb)->pkt_type) {
434 case H4_NEG_PKT:
435 hci_h4p_negotiation_packet(info, skb);
436 info->rx_state = WAIT_FOR_PKT_TYPE;
437 return;
438 case H4_ALIVE_PKT:
439 hci_h4p_alive_packet(info, skb);
440 info->rx_state = WAIT_FOR_PKT_TYPE;
441 return;
442 }
443
444 if (!test_bit(HCI_UP, &info->hdev->flags)) {
445 BT_DBG("fw_event");
446 hci_h4p_parse_fw_event(info, skb);
447 return;
448 }
449 }
450
451 hci_recv_frame(info->hdev, skb);
452 BT_DBG("Frame sent to upper layer");
453 }
454
455 static inline void hci_h4p_handle_byte(struct hci_h4p_info *info, u8 byte)
456 {
457 switch (info->rx_state) {
458 case WAIT_FOR_PKT_TYPE:
459 bt_cb(info->rx_skb)->pkt_type = byte;
460 info->rx_count = hci_h4p_get_hdr_len(info, byte);
461 if (info->rx_count < 0) {
462 info->hdev->stat.err_rx++;
463 kfree_skb(info->rx_skb);
464 info->rx_skb = NULL;
465 } else {
466 info->rx_state = WAIT_FOR_HEADER;
467 }
468 break;
469 case WAIT_FOR_HEADER:
470 info->rx_count--;
471 *skb_put(info->rx_skb, 1) = byte;
472 if (info->rx_count != 0)
473 break;
474 info->rx_count = hci_h4p_get_data_len(info, info->rx_skb);
475 if (info->rx_count > skb_tailroom(info->rx_skb)) {
476 dev_err(info->dev, "frame too long\n");
477 info->garbage_bytes = info->rx_count
478 - skb_tailroom(info->rx_skb);
479 kfree_skb(info->rx_skb);
480 info->rx_skb = NULL;
481 break;
482 }
483 info->rx_state = WAIT_FOR_DATA;
484 break;
485 case WAIT_FOR_DATA:
486 info->rx_count--;
487 *skb_put(info->rx_skb, 1) = byte;
488 break;
489 default:
490 WARN_ON(1);
491 break;
492 }
493
494 if (info->rx_count == 0) {
495 /* H4+ devices should always send word aligned packets */
496 if (!(info->rx_skb->len % 2))
497 info->garbage_bytes++;
498 hci_h4p_recv_frame(info, info->rx_skb);
499 info->rx_skb = NULL;
500 }
501 }
502
503 static void hci_h4p_rx_tasklet(unsigned long data)
504 {
505 u8 byte;
506 struct hci_h4p_info *info = (struct hci_h4p_info *)data;
507
508 BT_DBG("tasklet woke up");
509 BT_DBG("rx_tasklet woke up");
510
511 while (hci_h4p_inb(info, UART_LSR) & UART_LSR_DR) {
512 byte = hci_h4p_inb(info, UART_RX);
513 if (info->garbage_bytes) {
514 info->garbage_bytes--;
515 continue;
516 }
517 if (info->rx_skb == NULL) {
518 info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE,
519 GFP_ATOMIC | GFP_DMA);
520 if (!info->rx_skb) {
521 dev_err(info->dev,
522 "No memory for new packet\n");
523 goto finish_rx;
524 }
525 info->rx_state = WAIT_FOR_PKT_TYPE;
526 info->rx_skb->dev = (void *)info->hdev;
527 }
528 info->hdev->stat.byte_rx++;
529 hci_h4p_handle_byte(info, byte);
530 }
531
532 if (!info->rx_enabled) {
533 if (hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT &&
534 info->autorts) {
535 __hci_h4p_set_auto_ctsrts(info, 0 , UART_EFR_RTS);
536 info->autorts = 0;
537 }
538 /* Flush posted write to avoid spurious interrupts */
539 hci_h4p_inb(info, UART_OMAP_SCR);
540 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
541 }
542
543 finish_rx:
544 BT_DBG("rx_ended");
545 }
546
547 static void hci_h4p_tx_tasklet(unsigned long data)
548 {
549 unsigned int sent = 0;
550 struct sk_buff *skb;
551 struct hci_h4p_info *info = (struct hci_h4p_info *)data;
552
553 BT_DBG("tasklet woke up");
554 BT_DBG("tx_tasklet woke up");
555
556 if (info->autorts != info->rx_enabled) {
557 if (hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT) {
558 if (info->autorts && !info->rx_enabled) {
559 __hci_h4p_set_auto_ctsrts(info, 0,
560 UART_EFR_RTS);
561 info->autorts = 0;
562 }
563 if (!info->autorts && info->rx_enabled) {
564 __hci_h4p_set_auto_ctsrts(info, 1,
565 UART_EFR_RTS);
566 info->autorts = 1;
567 }
568 } else {
569 hci_h4p_outb(info, UART_OMAP_SCR,
570 hci_h4p_inb(info, UART_OMAP_SCR) |
571 UART_OMAP_SCR_EMPTY_THR);
572 goto finish_tx;
573 }
574 }
575
576 skb = skb_dequeue(&info->txq);
577 if (!skb) {
578 /* No data in buffer */
579 BT_DBG("skb ready");
580 if (hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT) {
581 hci_h4p_outb(info, UART_IER,
582 hci_h4p_inb(info, UART_IER) &
583 ~UART_IER_THRI);
584 hci_h4p_inb(info, UART_OMAP_SCR);
585 hci_h4p_disable_tx(info);
586 return;
587 }
588 hci_h4p_outb(info, UART_OMAP_SCR,
589 hci_h4p_inb(info, UART_OMAP_SCR) |
590 UART_OMAP_SCR_EMPTY_THR);
591 goto finish_tx;
592 }
593
594 /* Copy data to tx fifo */
595 while (!(hci_h4p_inb(info, UART_OMAP_SSR) & UART_OMAP_SSR_TXFULL) &&
596 (sent < skb->len)) {
597 hci_h4p_outb(info, UART_TX, skb->data[sent]);
598 sent++;
599 }
600
601 info->hdev->stat.byte_tx += sent;
602 if (skb->len == sent) {
603 kfree_skb(skb);
604 } else {
605 skb_pull(skb, sent);
606 skb_queue_head(&info->txq, skb);
607 }
608
609 hci_h4p_outb(info, UART_OMAP_SCR, hci_h4p_inb(info, UART_OMAP_SCR) &
610 ~UART_OMAP_SCR_EMPTY_THR);
611 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) |
612 UART_IER_THRI);
613
614 finish_tx:
615 /* Flush posted write to avoid spurious interrupts */
616 hci_h4p_inb(info, UART_OMAP_SCR);
617
618 }
619
620 static irqreturn_t hci_h4p_interrupt(int irq, void *data)
621 {
622 struct hci_h4p_info *info = (struct hci_h4p_info *)data;
623 u8 iir, msr;
624 int ret;
625
626 ret = IRQ_NONE;
627
628 iir = hci_h4p_inb(info, UART_IIR);
629 if (iir & UART_IIR_NO_INT)
630 return IRQ_HANDLED;
631
632 BT_DBG("In interrupt handler iir 0x%.2x", iir);
633
634 iir &= UART_IIR_ID;
635
636 if (iir == UART_IIR_MSI) {
637 msr = hci_h4p_inb(info, UART_MSR);
638 ret = IRQ_HANDLED;
639 }
640 if (iir == UART_IIR_RLSI) {
641 hci_h4p_inb(info, UART_RX);
642 hci_h4p_inb(info, UART_LSR);
643 ret = IRQ_HANDLED;
644 }
645
646 if (iir == UART_IIR_RDI) {
647 hci_h4p_rx_tasklet((unsigned long)data);
648 ret = IRQ_HANDLED;
649 }
650
651 if (iir == UART_IIR_THRI) {
652 hci_h4p_tx_tasklet((unsigned long)data);
653 ret = IRQ_HANDLED;
654 }
655
656 return ret;
657 }
658
659 static irqreturn_t hci_h4p_wakeup_interrupt(int irq, void *dev_inst)
660 {
661 struct hci_h4p_info *info = dev_inst;
662 int should_wakeup;
663 struct hci_dev *hdev;
664
665 if (!info->hdev)
666 return IRQ_HANDLED;
667
668 should_wakeup = gpio_get_value(info->host_wakeup_gpio);
669 hdev = info->hdev;
670
671 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
672 if (should_wakeup == 1)
673 complete_all(&info->test_completion);
674
675 return IRQ_HANDLED;
676 }
677
678 BT_DBG("gpio interrupt %d", should_wakeup);
679
680 /* Check if wee have missed some interrupts */
681 if (info->rx_enabled == should_wakeup)
682 return IRQ_HANDLED;
683
684 if (should_wakeup)
685 hci_h4p_enable_rx(info);
686 else
687 hci_h4p_disable_rx(info);
688
689 return IRQ_HANDLED;
690 }
691
692 static inline void hci_h4p_set_pm_limits(struct hci_h4p_info *info, bool set)
693 {
694 struct hci_h4p_platform_data *bt_plat_data = info->dev->platform_data;
695 const char *sset = set ? "set" : "clear";
696
697 if (unlikely(!bt_plat_data || !bt_plat_data->set_pm_limits))
698 return;
699
700 if (set != !!test_bit(H4P_ACTIVE_MODE, &info->pm_flags)) {
701 bt_plat_data->set_pm_limits(info->dev, set);
702 if (set)
703 set_bit(H4P_ACTIVE_MODE, &info->pm_flags);
704 else
705 clear_bit(H4P_ACTIVE_MODE, &info->pm_flags);
706 BT_DBG("Change pm constraints to: %s", sset);
707 return;
708 }
709
710 BT_DBG("pm constraints remains: %s", sset);
711 }
712
713 static int hci_h4p_reset(struct hci_h4p_info *info)
714 {
715 int err;
716
717 err = hci_h4p_reset_uart(info);
718 if (err < 0) {
719 dev_err(info->dev, "Uart reset failed\n");
720 return err;
721 }
722 hci_h4p_init_uart(info);
723 hci_h4p_set_rts(info, 0);
724
725 gpio_set_value(info->reset_gpio, 0);
726 gpio_set_value(info->bt_wakeup_gpio, 1);
727 msleep(10);
728
729 if (gpio_get_value(info->host_wakeup_gpio) == 1) {
730 dev_err(info->dev, "host_wakeup_gpio not low\n");
731 return -EPROTO;
732 }
733
734 init_completion(&info->test_completion);
735 gpio_set_value(info->reset_gpio, 1);
736
737 if (!wait_for_completion_interruptible_timeout(&info->test_completion,
738 msecs_to_jiffies(100))) {
739 dev_err(info->dev, "wakeup test timed out\n");
740 complete_all(&info->test_completion);
741 return -EPROTO;
742 }
743
744 err = hci_h4p_wait_for_cts(info, 1, 100);
745 if (err < 0) {
746 dev_err(info->dev, "No cts from bt chip\n");
747 return err;
748 }
749
750 hci_h4p_set_rts(info, 1);
751
752 return 0;
753 }
754
755 /* hci callback functions */
756 static int hci_h4p_hci_flush(struct hci_dev *hdev)
757 {
758 struct hci_h4p_info *info = hci_get_drvdata(hdev);
759 skb_queue_purge(&info->txq);
760
761 return 0;
762 }
763
764 static int hci_h4p_bt_wakeup_test(struct hci_h4p_info *info)
765 {
766 /*
767 * Test Sequence:
768 * Host de-asserts the BT_WAKE_UP line.
769 * Host polls the UART_CTS line, waiting for it to be de-asserted.
770 * Host asserts the BT_WAKE_UP line.
771 * Host polls the UART_CTS line, waiting for it to be asserted.
772 * Host de-asserts the BT_WAKE_UP line (allow the Bluetooth device to
773 * sleep).
774 * Host polls the UART_CTS line, waiting for it to be de-asserted.
775 */
776 int err;
777 int ret = -ECOMM;
778
779 if (!info)
780 return -EINVAL;
781
782 /* Disable wakeup interrupts */
783 disable_irq(gpio_to_irq(info->host_wakeup_gpio));
784
785 gpio_set_value(info->bt_wakeup_gpio, 0);
786 err = hci_h4p_wait_for_cts(info, 0, 100);
787 if (err) {
788 dev_warn(info->dev,
789 "bt_wakeup_test: fail: CTS low timed out: %d\n",
790 err);
791 goto out;
792 }
793
794 gpio_set_value(info->bt_wakeup_gpio, 1);
795 err = hci_h4p_wait_for_cts(info, 1, 100);
796 if (err) {
797 dev_warn(info->dev,
798 "bt_wakeup_test: fail: CTS high timed out: %d\n",
799 err);
800 goto out;
801 }
802
803 gpio_set_value(info->bt_wakeup_gpio, 0);
804 err = hci_h4p_wait_for_cts(info, 0, 100);
805 if (err) {
806 dev_warn(info->dev,
807 "bt_wakeup_test: fail: CTS re-low timed out: %d\n",
808 err);
809 goto out;
810 }
811
812 ret = 0;
813
814 out:
815
816 /* Re-enable wakeup interrupts */
817 enable_irq(gpio_to_irq(info->host_wakeup_gpio));
818
819 return ret;
820 }
821
822 static int hci_h4p_hci_open(struct hci_dev *hdev)
823 {
824 struct hci_h4p_info *info;
825 int err, retries = 0;
826 struct sk_buff_head fw_queue;
827 unsigned long flags;
828
829 info = hci_get_drvdata(hdev);
830
831 if (test_bit(HCI_RUNNING, &hdev->flags))
832 return 0;
833
834 /* TI1271 has HW bug and boot up might fail. Retry up to three times */
835 again:
836
837 info->rx_enabled = 1;
838 info->rx_state = WAIT_FOR_PKT_TYPE;
839 info->rx_count = 0;
840 info->garbage_bytes = 0;
841 info->rx_skb = NULL;
842 info->pm_enabled = 0;
843 init_completion(&info->fw_completion);
844 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
845 hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
846 skb_queue_head_init(&fw_queue);
847
848 err = hci_h4p_reset(info);
849 if (err < 0)
850 goto err_clean;
851
852 hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_CTS | UART_EFR_RTS);
853 info->autorts = 1;
854
855 err = hci_h4p_send_negotiation(info);
856
857 err = hci_h4p_read_fw(info, &fw_queue);
858 if (err < 0) {
859 dev_err(info->dev, "Cannot read firmware\n");
860 goto err_clean;
861 }
862
863 err = hci_h4p_send_fw(info, &fw_queue);
864 if (err < 0) {
865 dev_err(info->dev, "Sending firmware failed.\n");
866 goto err_clean;
867 }
868
869 info->pm_enabled = 1;
870
871 err = hci_h4p_bt_wakeup_test(info);
872 if (err < 0) {
873 dev_err(info->dev, "BT wakeup test failed.\n");
874 goto err_clean;
875 }
876
877 spin_lock_irqsave(&info->lock, flags);
878 info->rx_enabled = gpio_get_value(info->host_wakeup_gpio);
879 hci_h4p_set_clk(info, &info->rx_clocks_en, info->rx_enabled);
880 spin_unlock_irqrestore(&info->lock, flags);
881
882 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
883
884 kfree_skb(info->alive_cmd_skb);
885 info->alive_cmd_skb = NULL;
886 set_bit(HCI_RUNNING, &hdev->flags);
887
888 BT_DBG("hci up and running");
889 return 0;
890
891 err_clean:
892 hci_h4p_hci_flush(hdev);
893 hci_h4p_reset_uart(info);
894 del_timer_sync(&info->lazy_release);
895 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
896 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
897 gpio_set_value(info->reset_gpio, 0);
898 gpio_set_value(info->bt_wakeup_gpio, 0);
899 skb_queue_purge(&fw_queue);
900 kfree_skb(info->alive_cmd_skb);
901 info->alive_cmd_skb = NULL;
902 kfree_skb(info->rx_skb);
903 info->rx_skb = NULL;
904
905 if (retries++ < 3) {
906 dev_err(info->dev, "FW loading try %d fail. Retry.\n", retries);
907 goto again;
908 }
909
910 return err;
911 }
912
913 static int hci_h4p_hci_close(struct hci_dev *hdev)
914 {
915 struct hci_h4p_info *info = hci_get_drvdata(hdev);
916
917 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
918 return 0;
919
920 hci_h4p_hci_flush(hdev);
921 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
922 hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
923 hci_h4p_reset_uart(info);
924 del_timer_sync(&info->lazy_release);
925 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
926 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
927 gpio_set_value(info->reset_gpio, 0);
928 gpio_set_value(info->bt_wakeup_gpio, 0);
929 kfree_skb(info->rx_skb);
930
931 return 0;
932 }
933
934 static int hci_h4p_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
935 {
936 struct hci_h4p_info *info;
937 int err = 0;
938
939 BT_DBG("dev %p, skb %p", hdev, skb);
940
941 info = hci_get_drvdata(hdev);
942
943 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
944 dev_warn(info->dev, "Frame for non-running device\n");
945 return -EIO;
946 }
947
948 switch (bt_cb(skb)->pkt_type) {
949 case HCI_COMMAND_PKT:
950 hdev->stat.cmd_tx++;
951 break;
952 case HCI_ACLDATA_PKT:
953 hdev->stat.acl_tx++;
954 break;
955 case HCI_SCODATA_PKT:
956 hdev->stat.sco_tx++;
957 break;
958 }
959
960 /* Push frame type to skb */
961 *skb_push(skb, 1) = (bt_cb(skb)->pkt_type);
962 /* We should allways send word aligned data to h4+ devices */
963 if (skb->len % 2) {
964 err = skb_pad(skb, 1);
965 if (!err)
966 *skb_put(skb, 1) = 0x00;
967 }
968 if (err)
969 return err;
970
971 skb_queue_tail(&info->txq, skb);
972 hci_h4p_enable_tx(info);
973
974 return 0;
975 }
976
977 static ssize_t hci_h4p_store_bdaddr(struct device *dev,
978 struct device_attribute *attr,
979 const char *buf, size_t count)
980 {
981 struct hci_h4p_info *info = dev_get_drvdata(dev);
982 unsigned int bdaddr[6];
983 int ret, i;
984
985 ret = sscanf(buf, "%2x:%2x:%2x:%2x:%2x:%2x\n",
986 &bdaddr[0], &bdaddr[1], &bdaddr[2],
987 &bdaddr[3], &bdaddr[4], &bdaddr[5]);
988
989 if (ret != 6)
990 return -EINVAL;
991
992 for (i = 0; i < 6; i++) {
993 if (bdaddr[i] > 0xff)
994 return -EINVAL;
995 info->bd_addr[i] = bdaddr[i] & 0xff;
996 }
997
998 return count;
999 }
1000
1001 static ssize_t hci_h4p_show_bdaddr(struct device *dev,
1002 struct device_attribute *attr, char *buf)
1003 {
1004 struct hci_h4p_info *info = dev_get_drvdata(dev);
1005
1006 return sprintf(buf, "%pMR\n", info->bd_addr);
1007 }
1008
1009 static DEVICE_ATTR(bdaddr, S_IRUGO | S_IWUSR, hci_h4p_show_bdaddr,
1010 hci_h4p_store_bdaddr);
1011
1012 static int hci_h4p_sysfs_create_files(struct device *dev)
1013 {
1014 return device_create_file(dev, &dev_attr_bdaddr);
1015 }
1016
1017 static void hci_h4p_sysfs_remove_files(struct device *dev)
1018 {
1019 device_remove_file(dev, &dev_attr_bdaddr);
1020 }
1021
1022 static int hci_h4p_register_hdev(struct hci_h4p_info *info)
1023 {
1024 struct hci_dev *hdev;
1025
1026 /* Initialize and register HCI device */
1027
1028 hdev = hci_alloc_dev();
1029 if (!hdev) {
1030 dev_err(info->dev, "Can't allocate memory for device\n");
1031 return -ENOMEM;
1032 }
1033 info->hdev = hdev;
1034
1035 hdev->bus = HCI_UART;
1036 hci_set_drvdata(hdev, info);
1037
1038 hdev->open = hci_h4p_hci_open;
1039 hdev->close = hci_h4p_hci_close;
1040 hdev->flush = hci_h4p_hci_flush;
1041 hdev->send = hci_h4p_hci_send_frame;
1042 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1043
1044 SET_HCIDEV_DEV(hdev, info->dev);
1045
1046 if (hci_h4p_sysfs_create_files(info->dev) < 0) {
1047 dev_err(info->dev, "failed to create sysfs files\n");
1048 goto free;
1049 }
1050
1051 if (hci_register_dev(hdev) >= 0)
1052 return 0;
1053
1054 dev_err(info->dev, "hci_register failed %s.\n", hdev->name);
1055 hci_h4p_sysfs_remove_files(info->dev);
1056 free:
1057 hci_free_dev(info->hdev);
1058 return -ENODEV;
1059 }
1060
1061 static int hci_h4p_probe(struct platform_device *pdev)
1062 {
1063 struct hci_h4p_platform_data *bt_plat_data;
1064 struct hci_h4p_info *info;
1065 int err;
1066
1067 dev_info(&pdev->dev, "Registering HCI H4P device\n");
1068 info = devm_kzalloc(&pdev->dev, sizeof(struct hci_h4p_info),
1069 GFP_KERNEL);
1070 if (!info)
1071 return -ENOMEM;
1072
1073 info->dev = &pdev->dev;
1074 info->tx_enabled = 1;
1075 info->rx_enabled = 1;
1076 spin_lock_init(&info->lock);
1077 spin_lock_init(&info->clocks_lock);
1078 skb_queue_head_init(&info->txq);
1079
1080 if (pdev->dev.platform_data == NULL) {
1081 dev_err(&pdev->dev, "Could not get Bluetooth config data\n");
1082 return -ENODATA;
1083 }
1084
1085 bt_plat_data = pdev->dev.platform_data;
1086 info->chip_type = bt_plat_data->chip_type;
1087 info->bt_wakeup_gpio = bt_plat_data->bt_wakeup_gpio;
1088 info->host_wakeup_gpio = bt_plat_data->host_wakeup_gpio;
1089 info->reset_gpio = bt_plat_data->reset_gpio;
1090 info->reset_gpio_shared = bt_plat_data->reset_gpio_shared;
1091 info->bt_sysclk = bt_plat_data->bt_sysclk;
1092
1093 BT_DBG("RESET gpio: %d", info->reset_gpio);
1094 BT_DBG("BTWU gpio: %d", info->bt_wakeup_gpio);
1095 BT_DBG("HOSTWU gpio: %d", info->host_wakeup_gpio);
1096 BT_DBG("sysclk: %d", info->bt_sysclk);
1097
1098 init_completion(&info->test_completion);
1099 complete_all(&info->test_completion);
1100
1101 if (!info->reset_gpio_shared) {
1102 err = devm_gpio_request_one(&pdev->dev, info->reset_gpio,
1103 GPIOF_OUT_INIT_LOW, "bt_reset");
1104 if (err < 0) {
1105 dev_err(&pdev->dev, "Cannot get GPIO line %d\n",
1106 info->reset_gpio);
1107 return err;
1108 }
1109 }
1110
1111 err = devm_gpio_request_one(&pdev->dev, info->bt_wakeup_gpio,
1112 GPIOF_OUT_INIT_LOW, "bt_wakeup");
1113
1114 if (err < 0) {
1115 dev_err(info->dev, "Cannot get GPIO line 0x%d",
1116 info->bt_wakeup_gpio);
1117 return err;
1118 }
1119
1120 err = devm_gpio_request_one(&pdev->dev, info->host_wakeup_gpio,
1121 GPIOF_DIR_IN, "host_wakeup");
1122 if (err < 0) {
1123 dev_err(info->dev, "Cannot get GPIO line %d",
1124 info->host_wakeup_gpio);
1125 return err;
1126 }
1127
1128 info->irq = bt_plat_data->uart_irq;
1129 info->uart_base = devm_ioremap(&pdev->dev, bt_plat_data->uart_base,
1130 SZ_2K);
1131 info->uart_iclk = devm_clk_get(&pdev->dev, bt_plat_data->uart_iclk);
1132 info->uart_fclk = devm_clk_get(&pdev->dev, bt_plat_data->uart_fclk);
1133
1134 err = devm_request_irq(&pdev->dev, info->irq, hci_h4p_interrupt,
1135 IRQF_DISABLED, "hci_h4p", info);
1136 if (err < 0) {
1137 dev_err(info->dev, "hci_h4p: unable to get IRQ %d\n",
1138 info->irq);
1139 return err;
1140 }
1141
1142 err = devm_request_irq(&pdev->dev, gpio_to_irq(info->host_wakeup_gpio),
1143 hci_h4p_wakeup_interrupt, IRQF_TRIGGER_FALLING |
1144 IRQF_TRIGGER_RISING | IRQF_DISABLED,
1145 "hci_h4p_wkup", info);
1146 if (err < 0) {
1147 dev_err(info->dev, "hci_h4p: unable to get wakeup IRQ %d\n",
1148 gpio_to_irq(info->host_wakeup_gpio));
1149 return err;
1150 }
1151
1152 err = irq_set_irq_wake(gpio_to_irq(info->host_wakeup_gpio), 1);
1153 if (err < 0) {
1154 dev_err(info->dev, "hci_h4p: unable to set wakeup for IRQ %d\n",
1155 gpio_to_irq(info->host_wakeup_gpio));
1156 return err;
1157 }
1158
1159 init_timer_deferrable(&info->lazy_release);
1160 info->lazy_release.function = hci_h4p_lazy_clock_release;
1161 info->lazy_release.data = (unsigned long)info;
1162 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
1163 err = hci_h4p_reset_uart(info);
1164 if (err < 0)
1165 return err;
1166 gpio_set_value(info->reset_gpio, 0);
1167 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
1168
1169 platform_set_drvdata(pdev, info);
1170
1171 if (hci_h4p_register_hdev(info) < 0) {
1172 dev_err(info->dev, "failed to register hci_h4p hci device\n");
1173 return -EINVAL;
1174 }
1175
1176 return 0;
1177 }
1178
1179 static int hci_h4p_remove(struct platform_device *pdev)
1180 {
1181 struct hci_h4p_info *info;
1182
1183 info = platform_get_drvdata(pdev);
1184
1185 hci_h4p_sysfs_remove_files(info->dev);
1186 hci_h4p_hci_close(info->hdev);
1187 hci_unregister_dev(info->hdev);
1188 hci_free_dev(info->hdev);
1189
1190 return 0;
1191 }
1192
1193 static struct platform_driver hci_h4p_driver = {
1194 .probe = hci_h4p_probe,
1195 .remove = hci_h4p_remove,
1196 .driver = {
1197 .name = "hci_h4p",
1198 },
1199 };
1200
1201 module_platform_driver(hci_h4p_driver);
1202
1203 MODULE_ALIAS("platform:hci_h4p");
1204 MODULE_DESCRIPTION("Bluetooth h4 driver with nokia extensions");
1205 MODULE_LICENSE("GPL");
1206 MODULE_AUTHOR("Ville Tervo");
This page took 0.055686 seconds and 5 git commands to generate.