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