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