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