Merge tag 'ntb-3.15' of git://github.com/jonmason/ntb
[cascardo/linux.git] / drivers / staging / nokia_h4p / nokia_fw-csr.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  */
21
22 #include <linux/skbuff.h>
23 #include <linux/delay.h>
24 #include <linux/serial_reg.h>
25
26 #include "hci_h4p.h"
27
28 void hci_h4p_bc4_parse_fw_event(struct hci_h4p_info *info, struct sk_buff *skb)
29 {
30         /* Check if this is fw packet */
31         if (skb->data[0] != 0xff) {
32                 hci_recv_frame(info->hdev, skb);
33                 return;
34         }
35
36         if (skb->data[11] || skb->data[12]) {
37                 dev_err(info->dev, "Firmware sending command failed\n");
38                 info->fw_error = -EPROTO;
39         }
40
41         kfree_skb(skb);
42         complete(&info->fw_completion);
43 }
44
45 int hci_h4p_bc4_send_fw(struct hci_h4p_info *info,
46                         struct sk_buff_head *fw_queue)
47 {
48         static const u8 nokia_oui[3] = {0x00, 0x19, 0x4F};
49         struct sk_buff *skb;
50         unsigned int offset;
51         int retries, count, i, not_valid;
52         unsigned long flags;
53
54         info->fw_error = 0;
55
56         BT_DBG("Sending firmware");
57         skb = skb_dequeue(fw_queue);
58
59         if (!skb)
60                 return -ENOMSG;
61
62         /* Check if this is bd_address packet */
63         if (skb->data[15] == 0x01 && skb->data[16] == 0x00) {
64                 offset = 21;
65                 skb->data[offset + 1] = 0x00;
66                 skb->data[offset + 5] = 0x00;
67
68                 not_valid = 1;
69                 for (i = 0; i < 6; i++) {
70                         if (info->bd_addr[i] != 0x00) {
71                                 not_valid = 0;
72                                 break;
73                         }
74                 }
75
76                 if (not_valid) {
77                         dev_info(info->dev, "Valid bluetooth address not found, setting some random\n");
78                         /* When address is not valid, use some random */
79                         memcpy(info->bd_addr, nokia_oui, 3);
80                         get_random_bytes(info->bd_addr + 3, 3);
81                 }
82
83                 skb->data[offset + 7] = info->bd_addr[0];
84                 skb->data[offset + 6] = info->bd_addr[1];
85                 skb->data[offset + 4] = info->bd_addr[2];
86                 skb->data[offset + 0] = info->bd_addr[3];
87                 skb->data[offset + 3] = info->bd_addr[4];
88                 skb->data[offset + 2] = info->bd_addr[5];
89         }
90
91         for (count = 1; ; count++) {
92                 BT_DBG("Sending firmware command %d", count);
93                 init_completion(&info->fw_completion);
94                 skb_queue_tail(&info->txq, skb);
95                 spin_lock_irqsave(&info->lock, flags);
96                 hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) |
97                                                          UART_IER_THRI);
98                 spin_unlock_irqrestore(&info->lock, flags);
99
100                 skb = skb_dequeue(fw_queue);
101                 if (!skb)
102                         break;
103
104                 if (!wait_for_completion_timeout(&info->fw_completion,
105                                                  msecs_to_jiffies(1000))) {
106                         dev_err(info->dev, "No reply to fw command\n");
107                         return -ETIMEDOUT;
108                 }
109
110                 if (info->fw_error) {
111                         dev_err(info->dev, "FW error\n");
112                         return -EPROTO;
113                 }
114         };
115
116         /* Wait for chip warm reset */
117         retries = 100;
118         while ((!skb_queue_empty(&info->txq) ||
119                !(hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT)) &&
120                retries--) {
121                 msleep(10);
122         }
123         if (!retries) {
124                 dev_err(info->dev, "Transmitter not empty\n");
125                 return -ETIMEDOUT;
126         }
127
128         hci_h4p_change_speed(info, BC4_MAX_BAUD_RATE);
129
130         if (hci_h4p_wait_for_cts(info, 1, 100)) {
131                 dev_err(info->dev, "cts didn't deassert after final speed\n");
132                 return -ETIMEDOUT;
133         }
134
135         retries = 100;
136         do {
137                 init_completion(&info->init_completion);
138                 hci_h4p_send_alive_packet(info);
139                 retries--;
140         } while (!wait_for_completion_timeout(&info->init_completion, 100) &&
141                  retries > 0);
142
143         if (!retries) {
144                 dev_err(info->dev, "No alive reply after speed change\n");
145                 return -ETIMEDOUT;
146         }
147
148         return 0;
149 }