Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Driver for Bluetooth PCMCIA cards with HCI UART interface |
| 4 | * |
| 5 | * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org> |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation; |
| 11 | * |
| 12 | * Software distributed under the License is distributed on an "AS |
| 13 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 14 | * implied. See the License for the specific language governing |
| 15 | * rights and limitations under the License. |
| 16 | * |
| 17 | * The initial developer of the original code is David A. Hinds |
| 18 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds |
| 19 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. |
| 20 | * |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/delay.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/ptrace.h> |
| 32 | #include <linux/ioport.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | #include <linux/moduleparam.h> |
| 35 | |
| 36 | #include <linux/skbuff.h> |
| 37 | #include <linux/string.h> |
| 38 | #include <linux/serial.h> |
| 39 | #include <linux/serial_reg.h> |
| 40 | #include <linux/bitops.h> |
| 41 | #include <asm/system.h> |
| 42 | #include <asm/io.h> |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <pcmcia/cs.h> |
| 45 | #include <pcmcia/cistpl.h> |
| 46 | #include <pcmcia/ciscode.h> |
| 47 | #include <pcmcia/ds.h> |
| 48 | #include <pcmcia/cisreg.h> |
| 49 | |
| 50 | #include <net/bluetooth/bluetooth.h> |
| 51 | #include <net/bluetooth/hci_core.h> |
| 52 | |
| 53 | |
| 54 | |
| 55 | /* ======================== Module parameters ======================== */ |
| 56 | |
| 57 | |
| 58 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 59 | MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
| 62 | |
| 63 | |
| 64 | /* ======================== Local structures ======================== */ |
| 65 | |
| 66 | |
| 67 | typedef struct btuart_info_t { |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 68 | struct pcmcia_device *p_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | struct hci_dev *hdev; |
| 71 | |
| 72 | spinlock_t lock; /* For serializing operations */ |
| 73 | |
| 74 | struct sk_buff_head txq; |
| 75 | unsigned long tx_state; |
| 76 | |
| 77 | unsigned long rx_state; |
| 78 | unsigned long rx_count; |
| 79 | struct sk_buff *rx_skb; |
| 80 | } btuart_info_t; |
| 81 | |
| 82 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 83 | static int btuart_config(struct pcmcia_device *link); |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 84 | static void btuart_release(struct pcmcia_device *link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 86 | static void btuart_detach(struct pcmcia_device *p_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | /* Maximum baud rate */ |
| 90 | #define SPEED_MAX 115200 |
| 91 | |
| 92 | /* Default baud rate: 57600, 115200, 230400 or 460800 */ |
| 93 | #define DEFAULT_BAUD_RATE 115200 |
| 94 | |
| 95 | |
| 96 | /* Transmit states */ |
| 97 | #define XMIT_SENDING 1 |
| 98 | #define XMIT_WAKEUP 2 |
| 99 | #define XMIT_WAITING 8 |
| 100 | |
| 101 | /* Receiver states */ |
| 102 | #define RECV_WAIT_PACKET_TYPE 0 |
| 103 | #define RECV_WAIT_EVENT_HEADER 1 |
| 104 | #define RECV_WAIT_ACL_HEADER 2 |
| 105 | #define RECV_WAIT_SCO_HEADER 3 |
| 106 | #define RECV_WAIT_DATA 4 |
| 107 | |
| 108 | |
| 109 | |
| 110 | /* ======================== Interrupt handling ======================== */ |
| 111 | |
| 112 | |
| 113 | static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len) |
| 114 | { |
| 115 | int actual = 0; |
| 116 | |
| 117 | /* Tx FIFO should be empty */ |
| 118 | if (!(inb(iobase + UART_LSR) & UART_LSR_THRE)) |
| 119 | return 0; |
| 120 | |
| 121 | /* Fill FIFO with current frame */ |
| 122 | while ((fifo_size-- > 0) && (actual < len)) { |
| 123 | /* Transmit next byte */ |
| 124 | outb(buf[actual], iobase + UART_TX); |
| 125 | actual++; |
| 126 | } |
| 127 | |
| 128 | return actual; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void btuart_write_wakeup(btuart_info_t *info) |
| 133 | { |
| 134 | if (!info) { |
| 135 | BT_ERR("Unknown device"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) { |
| 140 | set_bit(XMIT_WAKEUP, &(info->tx_state)); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | do { |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 145 | register unsigned int iobase = info->p_dev->resource[0]->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | register struct sk_buff *skb; |
| 147 | register int len; |
| 148 | |
| 149 | clear_bit(XMIT_WAKEUP, &(info->tx_state)); |
| 150 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 151 | if (!pcmcia_dev_present(info->p_dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return; |
| 153 | |
| 154 | if (!(skb = skb_dequeue(&(info->txq)))) |
| 155 | break; |
| 156 | |
| 157 | /* Send frame */ |
| 158 | len = btuart_write(iobase, 16, skb->data, skb->len); |
| 159 | set_bit(XMIT_WAKEUP, &(info->tx_state)); |
| 160 | |
| 161 | if (len == skb->len) { |
| 162 | kfree_skb(skb); |
| 163 | } else { |
| 164 | skb_pull(skb, len); |
| 165 | skb_queue_head(&(info->txq), skb); |
| 166 | } |
| 167 | |
| 168 | info->hdev->stat.byte_tx += len; |
| 169 | |
| 170 | } while (test_bit(XMIT_WAKEUP, &(info->tx_state))); |
| 171 | |
| 172 | clear_bit(XMIT_SENDING, &(info->tx_state)); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static void btuart_receive(btuart_info_t *info) |
| 177 | { |
| 178 | unsigned int iobase; |
| 179 | int boguscount = 0; |
| 180 | |
| 181 | if (!info) { |
| 182 | BT_ERR("Unknown device"); |
| 183 | return; |
| 184 | } |
| 185 | |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 186 | iobase = info->p_dev->resource[0]->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | do { |
| 189 | info->hdev->stat.byte_rx++; |
| 190 | |
| 191 | /* Allocate packet */ |
| 192 | if (info->rx_skb == NULL) { |
| 193 | info->rx_state = RECV_WAIT_PACKET_TYPE; |
| 194 | info->rx_count = 0; |
| 195 | if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) { |
| 196 | BT_ERR("Can't allocate mem for new packet"); |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (info->rx_state == RECV_WAIT_PACKET_TYPE) { |
| 202 | |
| 203 | info->rx_skb->dev = (void *) info->hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 204 | bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 206 | switch (bt_cb(info->rx_skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | case HCI_EVENT_PKT: |
| 209 | info->rx_state = RECV_WAIT_EVENT_HEADER; |
| 210 | info->rx_count = HCI_EVENT_HDR_SIZE; |
| 211 | break; |
| 212 | |
| 213 | case HCI_ACLDATA_PKT: |
| 214 | info->rx_state = RECV_WAIT_ACL_HEADER; |
| 215 | info->rx_count = HCI_ACL_HDR_SIZE; |
| 216 | break; |
| 217 | |
| 218 | case HCI_SCODATA_PKT: |
| 219 | info->rx_state = RECV_WAIT_SCO_HEADER; |
| 220 | info->rx_count = HCI_SCO_HDR_SIZE; |
| 221 | break; |
| 222 | |
| 223 | default: |
| 224 | /* Unknown packet */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 225 | BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | info->hdev->stat.err_rx++; |
| 227 | clear_bit(HCI_RUNNING, &(info->hdev->flags)); |
| 228 | |
| 229 | kfree_skb(info->rx_skb); |
| 230 | info->rx_skb = NULL; |
| 231 | break; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | } else { |
| 236 | |
| 237 | *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX); |
| 238 | info->rx_count--; |
| 239 | |
| 240 | if (info->rx_count == 0) { |
| 241 | |
| 242 | int dlen; |
| 243 | struct hci_event_hdr *eh; |
| 244 | struct hci_acl_hdr *ah; |
| 245 | struct hci_sco_hdr *sh; |
| 246 | |
| 247 | |
| 248 | switch (info->rx_state) { |
| 249 | |
| 250 | case RECV_WAIT_EVENT_HEADER: |
Arnaldo Carvalho de Melo | 2a123b8 | 2007-03-27 18:38:07 -0300 | [diff] [blame] | 251 | eh = hci_event_hdr(info->rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | info->rx_state = RECV_WAIT_DATA; |
| 253 | info->rx_count = eh->plen; |
| 254 | break; |
| 255 | |
| 256 | case RECV_WAIT_ACL_HEADER: |
Arnaldo Carvalho de Melo | 2a123b8 | 2007-03-27 18:38:07 -0300 | [diff] [blame] | 257 | ah = hci_acl_hdr(info->rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | dlen = __le16_to_cpu(ah->dlen); |
| 259 | info->rx_state = RECV_WAIT_DATA; |
| 260 | info->rx_count = dlen; |
| 261 | break; |
| 262 | |
| 263 | case RECV_WAIT_SCO_HEADER: |
Arnaldo Carvalho de Melo | 2a123b8 | 2007-03-27 18:38:07 -0300 | [diff] [blame] | 264 | sh = hci_sco_hdr(info->rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | info->rx_state = RECV_WAIT_DATA; |
| 266 | info->rx_count = sh->dlen; |
| 267 | break; |
| 268 | |
| 269 | case RECV_WAIT_DATA: |
| 270 | hci_recv_frame(info->rx_skb); |
| 271 | info->rx_skb = NULL; |
| 272 | break; |
| 273 | |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | /* Make sure we don't stay here too long */ |
| 281 | if (boguscount++ > 16) |
| 282 | break; |
| 283 | |
| 284 | } while (inb(iobase + UART_LSR) & UART_LSR_DR); |
| 285 | } |
| 286 | |
| 287 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 288 | static irqreturn_t btuart_interrupt(int irq, void *dev_inst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | btuart_info_t *info = dev_inst; |
| 291 | unsigned int iobase; |
| 292 | int boguscount = 0; |
| 293 | int iir, lsr; |
Alan Cox | aafcf99 | 2008-10-05 17:35:41 +0100 | [diff] [blame] | 294 | irqreturn_t r = IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Mike Frysinger | 7427847 | 2009-09-14 13:43:49 -0400 | [diff] [blame] | 296 | if (!info || !info->hdev) |
| 297 | /* our irq handler is shared */ |
| 298 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 300 | iobase = info->p_dev->resource[0]->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | spin_lock(&(info->lock)); |
| 303 | |
| 304 | iir = inb(iobase + UART_IIR) & UART_IIR_ID; |
| 305 | while (iir) { |
Alan Cox | aafcf99 | 2008-10-05 17:35:41 +0100 | [diff] [blame] | 306 | r = IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | /* Clear interrupt */ |
| 309 | lsr = inb(iobase + UART_LSR); |
| 310 | |
| 311 | switch (iir) { |
| 312 | case UART_IIR_RLSI: |
| 313 | BT_ERR("RLSI"); |
| 314 | break; |
| 315 | case UART_IIR_RDI: |
| 316 | /* Receive interrupt */ |
| 317 | btuart_receive(info); |
| 318 | break; |
| 319 | case UART_IIR_THRI: |
| 320 | if (lsr & UART_LSR_THRE) { |
| 321 | /* Transmitter ready for data */ |
| 322 | btuart_write_wakeup(info); |
| 323 | } |
| 324 | break; |
| 325 | default: |
| 326 | BT_ERR("Unhandled IIR=%#x", iir); |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | /* Make sure we don't stay here too long */ |
| 331 | if (boguscount++ > 100) |
| 332 | break; |
| 333 | |
| 334 | iir = inb(iobase + UART_IIR) & UART_IIR_ID; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | spin_unlock(&(info->lock)); |
| 339 | |
Alan Cox | aafcf99 | 2008-10-05 17:35:41 +0100 | [diff] [blame] | 340 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | static void btuart_change_speed(btuart_info_t *info, unsigned int speed) |
| 345 | { |
| 346 | unsigned long flags; |
| 347 | unsigned int iobase; |
| 348 | int fcr; /* FIFO control reg */ |
| 349 | int lcr; /* Line control reg */ |
| 350 | int divisor; |
| 351 | |
| 352 | if (!info) { |
| 353 | BT_ERR("Unknown device"); |
| 354 | return; |
| 355 | } |
| 356 | |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 357 | iobase = info->p_dev->resource[0]->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | spin_lock_irqsave(&(info->lock), flags); |
| 360 | |
| 361 | /* Turn off interrupts */ |
| 362 | outb(0, iobase + UART_IER); |
| 363 | |
| 364 | divisor = SPEED_MAX / speed; |
| 365 | |
| 366 | fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT; |
| 367 | |
| 368 | /* |
| 369 | * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and |
| 370 | * almost 1,7 ms at 19200 bps. At speeds above that we can just forget |
| 371 | * about this timeout since it will always be fast enough. |
| 372 | */ |
| 373 | |
| 374 | if (speed < 38400) |
| 375 | fcr |= UART_FCR_TRIGGER_1; |
| 376 | else |
| 377 | fcr |= UART_FCR_TRIGGER_14; |
| 378 | |
| 379 | /* Bluetooth cards use 8N1 */ |
| 380 | lcr = UART_LCR_WLEN8; |
| 381 | |
| 382 | outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */ |
| 383 | outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */ |
| 384 | outb(divisor >> 8, iobase + UART_DLM); |
| 385 | outb(lcr, iobase + UART_LCR); /* Set 8N1 */ |
| 386 | outb(fcr, iobase + UART_FCR); /* Enable FIFO's */ |
| 387 | |
Joe Perches | b92b1c5 | 2008-02-03 17:10:31 +0200 | [diff] [blame] | 388 | /* Turn on interrupts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER); |
| 390 | |
| 391 | spin_unlock_irqrestore(&(info->lock), flags); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | /* ======================== HCI interface ======================== */ |
| 397 | |
| 398 | |
| 399 | static int btuart_hci_flush(struct hci_dev *hdev) |
| 400 | { |
| 401 | btuart_info_t *info = (btuart_info_t *)(hdev->driver_data); |
| 402 | |
| 403 | /* Drop TX queue */ |
| 404 | skb_queue_purge(&(info->txq)); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | static int btuart_hci_open(struct hci_dev *hdev) |
| 411 | { |
| 412 | set_bit(HCI_RUNNING, &(hdev->flags)); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | static int btuart_hci_close(struct hci_dev *hdev) |
| 419 | { |
| 420 | if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags))) |
| 421 | return 0; |
| 422 | |
| 423 | btuart_hci_flush(hdev); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | static int btuart_hci_send_frame(struct sk_buff *skb) |
| 430 | { |
| 431 | btuart_info_t *info; |
| 432 | struct hci_dev *hdev = (struct hci_dev *)(skb->dev); |
| 433 | |
| 434 | if (!hdev) { |
| 435 | BT_ERR("Frame for unknown HCI device (hdev=NULL)"); |
| 436 | return -ENODEV; |
| 437 | } |
| 438 | |
| 439 | info = (btuart_info_t *)(hdev->driver_data); |
| 440 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 441 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | case HCI_COMMAND_PKT: |
| 443 | hdev->stat.cmd_tx++; |
| 444 | break; |
| 445 | case HCI_ACLDATA_PKT: |
| 446 | hdev->stat.acl_tx++; |
| 447 | break; |
| 448 | case HCI_SCODATA_PKT: |
| 449 | hdev->stat.sco_tx++; |
| 450 | break; |
| 451 | }; |
| 452 | |
| 453 | /* Prepend skb with frame type */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 454 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | skb_queue_tail(&(info->txq), skb); |
| 456 | |
| 457 | btuart_write_wakeup(info); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | static void btuart_hci_destruct(struct hci_dev *hdev) |
| 464 | { |
| 465 | } |
| 466 | |
| 467 | |
| 468 | static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg) |
| 469 | { |
| 470 | return -ENOIOCTLCMD; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | |
| 475 | /* ======================== Card services HCI interaction ======================== */ |
| 476 | |
| 477 | |
| 478 | static int btuart_open(btuart_info_t *info) |
| 479 | { |
| 480 | unsigned long flags; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 481 | unsigned int iobase = info->p_dev->resource[0]->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | struct hci_dev *hdev; |
| 483 | |
| 484 | spin_lock_init(&(info->lock)); |
| 485 | |
| 486 | skb_queue_head_init(&(info->txq)); |
| 487 | |
| 488 | info->rx_state = RECV_WAIT_PACKET_TYPE; |
| 489 | info->rx_count = 0; |
| 490 | info->rx_skb = NULL; |
| 491 | |
| 492 | /* Initialize HCI device */ |
| 493 | hdev = hci_alloc_dev(); |
| 494 | if (!hdev) { |
| 495 | BT_ERR("Can't allocate HCI device"); |
| 496 | return -ENOMEM; |
| 497 | } |
| 498 | |
| 499 | info->hdev = hdev; |
| 500 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 501 | hdev->bus = HCI_PCCARD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | hdev->driver_data = info; |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 503 | SET_HCIDEV_DEV(hdev, &info->p_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | hdev->open = btuart_hci_open; |
| 506 | hdev->close = btuart_hci_close; |
| 507 | hdev->flush = btuart_hci_flush; |
| 508 | hdev->send = btuart_hci_send_frame; |
| 509 | hdev->destruct = btuart_hci_destruct; |
| 510 | hdev->ioctl = btuart_hci_ioctl; |
| 511 | |
| 512 | hdev->owner = THIS_MODULE; |
| 513 | |
| 514 | spin_lock_irqsave(&(info->lock), flags); |
| 515 | |
| 516 | /* Reset UART */ |
| 517 | outb(0, iobase + UART_MCR); |
| 518 | |
| 519 | /* Turn off interrupts */ |
| 520 | outb(0, iobase + UART_IER); |
| 521 | |
| 522 | /* Initialize UART */ |
| 523 | outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */ |
| 524 | outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR); |
| 525 | |
| 526 | /* Turn on interrupts */ |
| 527 | // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER); |
| 528 | |
| 529 | spin_unlock_irqrestore(&(info->lock), flags); |
| 530 | |
| 531 | btuart_change_speed(info, DEFAULT_BAUD_RATE); |
| 532 | |
| 533 | /* Timeout before it is safe to send the first HCI packet */ |
| 534 | msleep(1000); |
| 535 | |
| 536 | /* Register HCI device */ |
| 537 | if (hci_register_dev(hdev) < 0) { |
| 538 | BT_ERR("Can't register HCI device"); |
| 539 | info->hdev = NULL; |
| 540 | hci_free_dev(hdev); |
| 541 | return -ENODEV; |
| 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | static int btuart_close(btuart_info_t *info) |
| 549 | { |
| 550 | unsigned long flags; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 551 | unsigned int iobase = info->p_dev->resource[0]->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | struct hci_dev *hdev = info->hdev; |
| 553 | |
| 554 | if (!hdev) |
| 555 | return -ENODEV; |
| 556 | |
| 557 | btuart_hci_close(hdev); |
| 558 | |
| 559 | spin_lock_irqsave(&(info->lock), flags); |
| 560 | |
| 561 | /* Reset UART */ |
| 562 | outb(0, iobase + UART_MCR); |
| 563 | |
| 564 | /* Turn off interrupts */ |
| 565 | outb(0, iobase + UART_IER); |
| 566 | |
| 567 | spin_unlock_irqrestore(&(info->lock), flags); |
| 568 | |
| 569 | if (hci_unregister_dev(hdev) < 0) |
| 570 | BT_ERR("Can't unregister HCI device %s", hdev->name); |
| 571 | |
| 572 | hci_free_dev(hdev); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 577 | static int btuart_probe(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | btuart_info_t *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | /* Create new info device */ |
Deepak Saxena | 089b1db | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 582 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | if (!info) |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 584 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 586 | info->p_dev = link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | link->priv = info; |
| 588 | |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 589 | link->resource[0]->flags |= IO_DATA_PATH_WIDTH_8; |
| 590 | link->resource[0]->end = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | link->conf.Attributes = CONF_ENABLE_IRQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | link->conf.IntType = INT_MEMORY_AND_IO; |
| 594 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 595 | return btuart_config(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 599 | static void btuart_detach(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | { |
| 601 | btuart_info_t *info = link->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 603 | btuart_release(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | kfree(info); |
| 605 | } |
| 606 | |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 607 | static int btuart_check_config(struct pcmcia_device *p_dev, |
| 608 | cistpl_cftable_entry_t *cf, |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 609 | cistpl_cftable_entry_t *dflt, |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 610 | unsigned int vcc, |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 611 | void *priv_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 613 | int *try = priv_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 615 | p_dev->io_lines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK; |
| 616 | |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 617 | if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM)) |
| 618 | p_dev->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000; |
| 619 | if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && |
| 620 | (cf->io.win[0].base != 0)) { |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 621 | p_dev->resource[0]->start = cf->io.win[0].base; |
| 622 | if (!pcmcia_request_io(p_dev)) |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 628 | static int btuart_check_config_notpicky(struct pcmcia_device *p_dev, |
| 629 | cistpl_cftable_entry_t *cf, |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 630 | cistpl_cftable_entry_t *dflt, |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 631 | unsigned int vcc, |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 632 | void *priv_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 634 | static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 }; |
| 635 | int j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 637 | if ((cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) { |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 638 | for (j = 0; j < 5; j++) { |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 639 | p_dev->resource[0]->start = base[j]; |
| 640 | p_dev->io_lines = base[j] ? 16 : 3; |
| 641 | if (!pcmcia_request_io(p_dev)) |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | } |
| 645 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 648 | static int btuart_config(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | btuart_info_t *info = link->priv; |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 651 | int i; |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 652 | int try; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 654 | /* First pass: look for a config entry that looks normal. |
| 655 | Two tries: without IO aliases, then with aliases */ |
| 656 | for (try = 0; try < 2; try++) |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 657 | if (!pcmcia_loop_config(link, btuart_check_config, &try)) |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 658 | goto found_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | /* Second pass: try to find an entry that isn't picky about |
| 661 | its base address, then try to grab any standard serial port |
| 662 | address, and finally try to get any free port. */ |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 663 | if (!pcmcia_loop_config(link, btuart_check_config_notpicky, NULL)) |
| 664 | goto found_port; |
| 665 | |
| 666 | BT_ERR("No usable port range found"); |
Dominik Brodowski | ed58872 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 667 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | found_port: |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 670 | i = pcmcia_request_irq(link, btuart_interrupt); |
Dominik Brodowski | 9ac3e58 | 2009-10-24 15:45:06 +0200 | [diff] [blame] | 671 | if (i != 0) |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 672 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 674 | i = pcmcia_request_configuration(link, &link->conf); |
Dominik Brodowski | 9ac3e58 | 2009-10-24 15:45:06 +0200 | [diff] [blame] | 675 | if (i != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | if (btuart_open(info) != 0) |
| 679 | goto failed; |
| 680 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 681 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | failed: |
| 684 | btuart_release(link); |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 685 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 689 | static void btuart_release(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
| 691 | btuart_info_t *info = link->priv; |
| 692 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 693 | btuart_close(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 695 | pcmcia_disable_device(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Dominik Brodowski | 279c936 | 2005-06-27 16:28:38 -0700 | [diff] [blame] | 698 | static struct pcmcia_device_id btuart_ids[] = { |
| 699 | /* don't use this driver. Use serial_cs + hci_uart instead */ |
| 700 | PCMCIA_DEVICE_NULL |
| 701 | }; |
| 702 | MODULE_DEVICE_TABLE(pcmcia, btuart_ids); |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | static struct pcmcia_driver btuart_driver = { |
| 705 | .owner = THIS_MODULE, |
| 706 | .drv = { |
| 707 | .name = "btuart_cs", |
| 708 | }, |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 709 | .probe = btuart_probe, |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 710 | .remove = btuart_detach, |
Dominik Brodowski | 279c936 | 2005-06-27 16:28:38 -0700 | [diff] [blame] | 711 | .id_table = btuart_ids, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | }; |
| 713 | |
| 714 | static int __init init_btuart_cs(void) |
| 715 | { |
| 716 | return pcmcia_register_driver(&btuart_driver); |
| 717 | } |
| 718 | |
| 719 | |
| 720 | static void __exit exit_btuart_cs(void) |
| 721 | { |
| 722 | pcmcia_unregister_driver(&btuart_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | module_init(init_btuart_cs); |
| 726 | module_exit(exit_btuart_cs); |