Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Driver for the 3Com Bluetooth PCMCIA card |
| 4 | * |
| 5 | * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org> |
| 6 | * Jose Orlando Pereira <jop@di.uminho.pt> |
| 7 | * |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation; |
| 12 | * |
| 13 | * Software distributed under the License is distributed on an "AS |
| 14 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 15 | * implied. See the License for the specific language governing |
| 16 | * rights and limitations under the License. |
| 17 | * |
| 18 | * The initial developer of the original code is David A. Hinds |
| 19 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds |
| 20 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/config.h> |
| 25 | #include <linux/module.h> |
| 26 | |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/errno.h> |
| 34 | #include <linux/ptrace.h> |
| 35 | #include <linux/ioport.h> |
| 36 | #include <linux/spinlock.h> |
| 37 | #include <linux/moduleparam.h> |
| 38 | |
| 39 | #include <linux/skbuff.h> |
| 40 | #include <linux/string.h> |
| 41 | #include <linux/serial.h> |
| 42 | #include <linux/serial_reg.h> |
| 43 | #include <linux/bitops.h> |
| 44 | #include <asm/system.h> |
| 45 | #include <asm/io.h> |
| 46 | |
| 47 | #include <linux/device.h> |
| 48 | #include <linux/firmware.h> |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <pcmcia/cs_types.h> |
| 51 | #include <pcmcia/cs.h> |
| 52 | #include <pcmcia/cistpl.h> |
| 53 | #include <pcmcia/ciscode.h> |
| 54 | #include <pcmcia/ds.h> |
| 55 | #include <pcmcia/cisreg.h> |
| 56 | |
| 57 | #include <net/bluetooth/bluetooth.h> |
| 58 | #include <net/bluetooth/hci_core.h> |
| 59 | |
| 60 | |
| 61 | |
| 62 | /* ======================== Module parameters ======================== */ |
| 63 | |
| 64 | |
| 65 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>"); |
| 66 | MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card"); |
| 67 | MODULE_LICENSE("GPL"); |
| 68 | |
| 69 | |
| 70 | |
| 71 | /* ======================== Local structures ======================== */ |
| 72 | |
| 73 | |
| 74 | typedef struct bt3c_info_t { |
| 75 | dev_link_t link; |
| 76 | dev_node_t node; |
| 77 | |
| 78 | struct hci_dev *hdev; |
| 79 | |
| 80 | spinlock_t lock; /* For serializing operations */ |
| 81 | |
| 82 | struct sk_buff_head txq; |
| 83 | unsigned long tx_state; |
| 84 | |
| 85 | unsigned long rx_state; |
| 86 | unsigned long rx_count; |
| 87 | struct sk_buff *rx_skb; |
| 88 | } bt3c_info_t; |
| 89 | |
| 90 | |
| 91 | static void bt3c_config(dev_link_t *link); |
| 92 | static void bt3c_release(dev_link_t *link); |
| 93 | static int bt3c_event(event_t event, int priority, event_callback_args_t *args); |
| 94 | |
| 95 | static dev_info_t dev_info = "bt3c_cs"; |
| 96 | |
| 97 | static dev_link_t *bt3c_attach(void); |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame^] | 98 | static void bt3c_detach(struct pcmcia_device *p_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | static dev_link_t *dev_list = NULL; |
| 101 | |
| 102 | |
| 103 | /* Transmit states */ |
| 104 | #define XMIT_SENDING 1 |
| 105 | #define XMIT_WAKEUP 2 |
| 106 | #define XMIT_WAITING 8 |
| 107 | |
| 108 | /* Receiver states */ |
| 109 | #define RECV_WAIT_PACKET_TYPE 0 |
| 110 | #define RECV_WAIT_EVENT_HEADER 1 |
| 111 | #define RECV_WAIT_ACL_HEADER 2 |
| 112 | #define RECV_WAIT_SCO_HEADER 3 |
| 113 | #define RECV_WAIT_DATA 4 |
| 114 | |
| 115 | |
| 116 | |
| 117 | /* ======================== Special I/O functions ======================== */ |
| 118 | |
| 119 | |
| 120 | #define DATA_L 0 |
| 121 | #define DATA_H 1 |
| 122 | #define ADDR_L 2 |
| 123 | #define ADDR_H 3 |
| 124 | #define CONTROL 4 |
| 125 | |
| 126 | |
| 127 | static inline void bt3c_address(unsigned int iobase, unsigned short addr) |
| 128 | { |
| 129 | outb(addr & 0xff, iobase + ADDR_L); |
| 130 | outb((addr >> 8) & 0xff, iobase + ADDR_H); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static inline void bt3c_put(unsigned int iobase, unsigned short value) |
| 135 | { |
| 136 | outb(value & 0xff, iobase + DATA_L); |
| 137 | outb((value >> 8) & 0xff, iobase + DATA_H); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value) |
| 142 | { |
| 143 | bt3c_address(iobase, addr); |
| 144 | bt3c_put(iobase, value); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | static inline unsigned short bt3c_get(unsigned int iobase) |
| 149 | { |
| 150 | unsigned short value = inb(iobase + DATA_L); |
| 151 | |
| 152 | value |= inb(iobase + DATA_H) << 8; |
| 153 | |
| 154 | return value; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr) |
| 159 | { |
| 160 | bt3c_address(iobase, addr); |
| 161 | |
| 162 | return bt3c_get(iobase); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | /* ======================== Interrupt handling ======================== */ |
| 168 | |
| 169 | |
| 170 | static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len) |
| 171 | { |
| 172 | int actual = 0; |
| 173 | |
| 174 | bt3c_address(iobase, 0x7080); |
| 175 | |
| 176 | /* Fill FIFO with current frame */ |
| 177 | while (actual < len) { |
| 178 | /* Transmit next byte */ |
| 179 | bt3c_put(iobase, buf[actual]); |
| 180 | actual++; |
| 181 | } |
| 182 | |
| 183 | bt3c_io_write(iobase, 0x7005, actual); |
| 184 | |
| 185 | return actual; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | static void bt3c_write_wakeup(bt3c_info_t *info) |
| 190 | { |
| 191 | if (!info) { |
| 192 | BT_ERR("Unknown device"); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) |
| 197 | return; |
| 198 | |
| 199 | do { |
| 200 | register unsigned int iobase = info->link.io.BasePort1; |
| 201 | register struct sk_buff *skb; |
| 202 | register int len; |
| 203 | |
| 204 | if (!(info->link.state & DEV_PRESENT)) |
| 205 | break; |
| 206 | |
| 207 | |
| 208 | if (!(skb = skb_dequeue(&(info->txq)))) { |
| 209 | clear_bit(XMIT_SENDING, &(info->tx_state)); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | /* Send frame */ |
| 214 | len = bt3c_write(iobase, 256, skb->data, skb->len); |
| 215 | |
| 216 | if (len != skb->len) { |
| 217 | BT_ERR("Very strange"); |
| 218 | } |
| 219 | |
| 220 | kfree_skb(skb); |
| 221 | |
| 222 | info->hdev->stat.byte_tx += len; |
| 223 | |
| 224 | } while (0); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | static void bt3c_receive(bt3c_info_t *info) |
| 229 | { |
| 230 | unsigned int iobase; |
| 231 | int size = 0, avail; |
| 232 | |
| 233 | if (!info) { |
| 234 | BT_ERR("Unknown device"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | iobase = info->link.io.BasePort1; |
| 239 | |
| 240 | avail = bt3c_read(iobase, 0x7006); |
| 241 | //printk("bt3c_cs: receiving %d bytes\n", avail); |
| 242 | |
| 243 | bt3c_address(iobase, 0x7480); |
| 244 | while (size < avail) { |
| 245 | size++; |
| 246 | info->hdev->stat.byte_rx++; |
| 247 | |
| 248 | /* Allocate packet */ |
| 249 | if (info->rx_skb == NULL) { |
| 250 | info->rx_state = RECV_WAIT_PACKET_TYPE; |
| 251 | info->rx_count = 0; |
| 252 | if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) { |
| 253 | BT_ERR("Can't allocate mem for new packet"); |
| 254 | return; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | if (info->rx_state == RECV_WAIT_PACKET_TYPE) { |
| 260 | |
| 261 | info->rx_skb->dev = (void *) info->hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 262 | bt_cb(info->rx_skb)->pkt_type = inb(iobase + DATA_L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | inb(iobase + DATA_H); |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 264 | //printk("bt3c: PACKET_TYPE=%02x\n", bt_cb(info->rx_skb)->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 266 | switch (bt_cb(info->rx_skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | case HCI_EVENT_PKT: |
| 269 | info->rx_state = RECV_WAIT_EVENT_HEADER; |
| 270 | info->rx_count = HCI_EVENT_HDR_SIZE; |
| 271 | break; |
| 272 | |
| 273 | case HCI_ACLDATA_PKT: |
| 274 | info->rx_state = RECV_WAIT_ACL_HEADER; |
| 275 | info->rx_count = HCI_ACL_HDR_SIZE; |
| 276 | break; |
| 277 | |
| 278 | case HCI_SCODATA_PKT: |
| 279 | info->rx_state = RECV_WAIT_SCO_HEADER; |
| 280 | info->rx_count = HCI_SCO_HDR_SIZE; |
| 281 | break; |
| 282 | |
| 283 | default: |
| 284 | /* Unknown packet */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 285 | 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] | 286 | info->hdev->stat.err_rx++; |
| 287 | clear_bit(HCI_RUNNING, &(info->hdev->flags)); |
| 288 | |
| 289 | kfree_skb(info->rx_skb); |
| 290 | info->rx_skb = NULL; |
| 291 | break; |
| 292 | |
| 293 | } |
| 294 | |
| 295 | } else { |
| 296 | |
| 297 | __u8 x = inb(iobase + DATA_L); |
| 298 | |
| 299 | *skb_put(info->rx_skb, 1) = x; |
| 300 | inb(iobase + DATA_H); |
| 301 | info->rx_count--; |
| 302 | |
| 303 | if (info->rx_count == 0) { |
| 304 | |
| 305 | int dlen; |
| 306 | struct hci_event_hdr *eh; |
| 307 | struct hci_acl_hdr *ah; |
| 308 | struct hci_sco_hdr *sh; |
| 309 | |
| 310 | switch (info->rx_state) { |
| 311 | |
| 312 | case RECV_WAIT_EVENT_HEADER: |
| 313 | eh = (struct hci_event_hdr *)(info->rx_skb->data); |
| 314 | info->rx_state = RECV_WAIT_DATA; |
| 315 | info->rx_count = eh->plen; |
| 316 | break; |
| 317 | |
| 318 | case RECV_WAIT_ACL_HEADER: |
| 319 | ah = (struct hci_acl_hdr *)(info->rx_skb->data); |
| 320 | dlen = __le16_to_cpu(ah->dlen); |
| 321 | info->rx_state = RECV_WAIT_DATA; |
| 322 | info->rx_count = dlen; |
| 323 | break; |
| 324 | |
| 325 | case RECV_WAIT_SCO_HEADER: |
| 326 | sh = (struct hci_sco_hdr *)(info->rx_skb->data); |
| 327 | info->rx_state = RECV_WAIT_DATA; |
| 328 | info->rx_count = sh->dlen; |
| 329 | break; |
| 330 | |
| 331 | case RECV_WAIT_DATA: |
| 332 | hci_recv_frame(info->rx_skb); |
| 333 | info->rx_skb = NULL; |
| 334 | break; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | } |
| 343 | |
| 344 | bt3c_io_write(iobase, 0x7006, 0x0000); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs) |
| 349 | { |
| 350 | bt3c_info_t *info = dev_inst; |
| 351 | unsigned int iobase; |
| 352 | int iir; |
| 353 | |
| 354 | if (!info || !info->hdev) { |
| 355 | BT_ERR("Call of irq %d for unknown device", irq); |
| 356 | return IRQ_NONE; |
| 357 | } |
| 358 | |
| 359 | iobase = info->link.io.BasePort1; |
| 360 | |
| 361 | spin_lock(&(info->lock)); |
| 362 | |
| 363 | iir = inb(iobase + CONTROL); |
| 364 | if (iir & 0x80) { |
| 365 | int stat = bt3c_read(iobase, 0x7001); |
| 366 | |
| 367 | if ((stat & 0xff) == 0x7f) { |
| 368 | BT_ERR("Very strange (stat=0x%04x)", stat); |
| 369 | } else if ((stat & 0xff) != 0xff) { |
| 370 | if (stat & 0x0020) { |
| 371 | int stat = bt3c_read(iobase, 0x7002) & 0x10; |
| 372 | BT_INFO("%s: Antenna %s", info->hdev->name, |
| 373 | stat ? "out" : "in"); |
| 374 | } |
| 375 | if (stat & 0x0001) |
| 376 | bt3c_receive(info); |
| 377 | if (stat & 0x0002) { |
| 378 | //BT_ERR("Ack (stat=0x%04x)", stat); |
| 379 | clear_bit(XMIT_SENDING, &(info->tx_state)); |
| 380 | bt3c_write_wakeup(info); |
| 381 | } |
| 382 | |
| 383 | bt3c_io_write(iobase, 0x7001, 0x0000); |
| 384 | |
| 385 | outb(iir, iobase + CONTROL); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | spin_unlock(&(info->lock)); |
| 390 | |
| 391 | return IRQ_HANDLED; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | /* ======================== HCI interface ======================== */ |
| 397 | |
| 398 | |
| 399 | static int bt3c_hci_flush(struct hci_dev *hdev) |
| 400 | { |
| 401 | bt3c_info_t *info = (bt3c_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 bt3c_hci_open(struct hci_dev *hdev) |
| 411 | { |
| 412 | set_bit(HCI_RUNNING, &(hdev->flags)); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | static int bt3c_hci_close(struct hci_dev *hdev) |
| 419 | { |
| 420 | if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags))) |
| 421 | return 0; |
| 422 | |
| 423 | bt3c_hci_flush(hdev); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | static int bt3c_hci_send_frame(struct sk_buff *skb) |
| 430 | { |
| 431 | bt3c_info_t *info; |
| 432 | struct hci_dev *hdev = (struct hci_dev *)(skb->dev); |
| 433 | unsigned long flags; |
| 434 | |
| 435 | if (!hdev) { |
| 436 | BT_ERR("Frame for unknown HCI device (hdev=NULL)"); |
| 437 | return -ENODEV; |
| 438 | } |
| 439 | |
| 440 | info = (bt3c_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 | spin_lock_irqsave(&(info->lock), flags); |
| 459 | |
| 460 | bt3c_write_wakeup(info); |
| 461 | |
| 462 | spin_unlock_irqrestore(&(info->lock), flags); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | static void bt3c_hci_destruct(struct hci_dev *hdev) |
| 469 | { |
| 470 | } |
| 471 | |
| 472 | |
| 473 | static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg) |
| 474 | { |
| 475 | return -ENOIOCTLCMD; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | |
| 480 | /* ======================== Card services HCI interaction ======================== */ |
| 481 | |
| 482 | |
| 483 | static struct device *bt3c_device(void) |
| 484 | { |
| 485 | static struct device dev = { |
| 486 | .bus_id = "pcmcia", |
| 487 | }; |
| 488 | kobject_set_name(&dev.kobj, "bt3c"); |
| 489 | kobject_init(&dev.kobj); |
| 490 | |
| 491 | return &dev; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count) |
| 496 | { |
| 497 | char *ptr = (char *) firmware; |
| 498 | char b[9]; |
| 499 | unsigned int iobase, size, addr, fcs, tmp; |
| 500 | int i, err = 0; |
| 501 | |
| 502 | iobase = info->link.io.BasePort1; |
| 503 | |
| 504 | /* Reset */ |
| 505 | bt3c_io_write(iobase, 0x8040, 0x0404); |
| 506 | bt3c_io_write(iobase, 0x8040, 0x0400); |
| 507 | |
| 508 | udelay(1); |
| 509 | |
| 510 | bt3c_io_write(iobase, 0x8040, 0x0404); |
| 511 | |
| 512 | udelay(17); |
| 513 | |
| 514 | /* Load */ |
| 515 | while (count) { |
| 516 | if (ptr[0] != 'S') { |
| 517 | BT_ERR("Bad address in firmware"); |
| 518 | err = -EFAULT; |
| 519 | goto error; |
| 520 | } |
| 521 | |
| 522 | memset(b, 0, sizeof(b)); |
| 523 | memcpy(b, ptr + 2, 2); |
| 524 | size = simple_strtol(b, NULL, 16); |
| 525 | |
| 526 | memset(b, 0, sizeof(b)); |
| 527 | memcpy(b, ptr + 4, 8); |
| 528 | addr = simple_strtol(b, NULL, 16); |
| 529 | |
| 530 | memset(b, 0, sizeof(b)); |
| 531 | memcpy(b, ptr + (size * 2) + 2, 2); |
| 532 | fcs = simple_strtol(b, NULL, 16); |
| 533 | |
| 534 | memset(b, 0, sizeof(b)); |
| 535 | for (tmp = 0, i = 0; i < size; i++) { |
| 536 | memcpy(b, ptr + (i * 2) + 2, 2); |
| 537 | tmp += simple_strtol(b, NULL, 16); |
| 538 | } |
| 539 | |
| 540 | if (((tmp + fcs) & 0xff) != 0xff) { |
| 541 | BT_ERR("Checksum error in firmware"); |
| 542 | err = -EILSEQ; |
| 543 | goto error; |
| 544 | } |
| 545 | |
| 546 | if (ptr[1] == '3') { |
| 547 | bt3c_address(iobase, addr); |
| 548 | |
| 549 | memset(b, 0, sizeof(b)); |
| 550 | for (i = 0; i < (size - 4) / 2; i++) { |
| 551 | memcpy(b, ptr + (i * 4) + 12, 4); |
| 552 | tmp = simple_strtol(b, NULL, 16); |
| 553 | bt3c_put(iobase, tmp); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | ptr += (size * 2) + 6; |
| 558 | count -= (size * 2) + 6; |
| 559 | } |
| 560 | |
| 561 | udelay(17); |
| 562 | |
| 563 | /* Boot */ |
| 564 | bt3c_address(iobase, 0x3000); |
| 565 | outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL); |
| 566 | |
| 567 | error: |
| 568 | udelay(17); |
| 569 | |
| 570 | /* Clear */ |
| 571 | bt3c_io_write(iobase, 0x7006, 0x0000); |
| 572 | bt3c_io_write(iobase, 0x7005, 0x0000); |
| 573 | bt3c_io_write(iobase, 0x7001, 0x0000); |
| 574 | |
| 575 | return err; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | static int bt3c_open(bt3c_info_t *info) |
| 580 | { |
| 581 | const struct firmware *firmware; |
| 582 | struct hci_dev *hdev; |
| 583 | int err; |
| 584 | |
| 585 | spin_lock_init(&(info->lock)); |
| 586 | |
| 587 | skb_queue_head_init(&(info->txq)); |
| 588 | |
| 589 | info->rx_state = RECV_WAIT_PACKET_TYPE; |
| 590 | info->rx_count = 0; |
| 591 | info->rx_skb = NULL; |
| 592 | |
| 593 | /* Initialize HCI device */ |
| 594 | hdev = hci_alloc_dev(); |
| 595 | if (!hdev) { |
| 596 | BT_ERR("Can't allocate HCI device"); |
| 597 | return -ENOMEM; |
| 598 | } |
| 599 | |
| 600 | info->hdev = hdev; |
| 601 | |
| 602 | hdev->type = HCI_PCCARD; |
| 603 | hdev->driver_data = info; |
| 604 | |
| 605 | hdev->open = bt3c_hci_open; |
| 606 | hdev->close = bt3c_hci_close; |
| 607 | hdev->flush = bt3c_hci_flush; |
| 608 | hdev->send = bt3c_hci_send_frame; |
| 609 | hdev->destruct = bt3c_hci_destruct; |
| 610 | hdev->ioctl = bt3c_hci_ioctl; |
| 611 | |
| 612 | hdev->owner = THIS_MODULE; |
| 613 | |
| 614 | /* Load firmware */ |
| 615 | err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device()); |
| 616 | if (err < 0) { |
| 617 | BT_ERR("Firmware request failed"); |
| 618 | goto error; |
| 619 | } |
| 620 | |
| 621 | err = bt3c_load_firmware(info, firmware->data, firmware->size); |
| 622 | |
| 623 | release_firmware(firmware); |
| 624 | |
| 625 | if (err < 0) { |
| 626 | BT_ERR("Firmware loading failed"); |
| 627 | goto error; |
| 628 | } |
| 629 | |
| 630 | /* Timeout before it is safe to send the first HCI packet */ |
| 631 | msleep(1000); |
| 632 | |
| 633 | /* Register HCI device */ |
| 634 | err = hci_register_dev(hdev); |
| 635 | if (err < 0) { |
| 636 | BT_ERR("Can't register HCI device"); |
| 637 | goto error; |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | |
| 642 | error: |
| 643 | info->hdev = NULL; |
| 644 | hci_free_dev(hdev); |
| 645 | return err; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | static int bt3c_close(bt3c_info_t *info) |
| 650 | { |
| 651 | struct hci_dev *hdev = info->hdev; |
| 652 | |
| 653 | if (!hdev) |
| 654 | return -ENODEV; |
| 655 | |
| 656 | bt3c_hci_close(hdev); |
| 657 | |
| 658 | if (hci_unregister_dev(hdev) < 0) |
| 659 | BT_ERR("Can't unregister HCI device %s", hdev->name); |
| 660 | |
| 661 | hci_free_dev(hdev); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static dev_link_t *bt3c_attach(void) |
| 667 | { |
| 668 | bt3c_info_t *info; |
| 669 | client_reg_t client_reg; |
| 670 | dev_link_t *link; |
| 671 | int ret; |
| 672 | |
| 673 | /* Create new info device */ |
Deepak Saxena | 089b1db | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 674 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (!info) |
| 676 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | link = &info->link; |
| 679 | link->priv = info; |
| 680 | |
| 681 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_8; |
| 682 | link->io.NumPorts1 = 8; |
| 683 | link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT; |
| 684 | link->irq.IRQInfo1 = IRQ_LEVEL_ID; |
| 685 | |
| 686 | link->irq.Handler = bt3c_interrupt; |
| 687 | link->irq.Instance = info; |
| 688 | |
| 689 | link->conf.Attributes = CONF_ENABLE_IRQ; |
| 690 | link->conf.Vcc = 50; |
| 691 | link->conf.IntType = INT_MEMORY_AND_IO; |
| 692 | |
| 693 | /* Register with Card Services */ |
| 694 | link->next = dev_list; |
| 695 | dev_list = link; |
| 696 | client_reg.dev_info = &dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | client_reg.Version = 0x0210; |
| 698 | client_reg.event_callback_args.client_data = link; |
| 699 | |
| 700 | ret = pcmcia_register_client(&link->handle, &client_reg); |
| 701 | if (ret != CS_SUCCESS) { |
| 702 | cs_error(link->handle, RegisterClient, ret); |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame^] | 703 | bt3c_detach(link->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | return link; |
| 708 | } |
| 709 | |
| 710 | |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame^] | 711 | static void bt3c_detach(struct pcmcia_device *p_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame^] | 713 | dev_link_t *link = dev_to_instance(p_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | bt3c_info_t *info = link->priv; |
| 715 | dev_link_t **linkp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | /* Locate device structure */ |
| 718 | for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) |
| 719 | if (*linkp == link) |
| 720 | break; |
| 721 | |
| 722 | if (*linkp == NULL) |
| 723 | return; |
| 724 | |
| 725 | if (link->state & DEV_CONFIG) |
| 726 | bt3c_release(link); |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* Unlink device structure, free bits */ |
| 729 | *linkp = link->next; |
| 730 | |
| 731 | kfree(info); |
| 732 | } |
| 733 | |
| 734 | static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse) |
| 735 | { |
| 736 | int i; |
| 737 | |
| 738 | i = pcmcia_get_tuple_data(handle, tuple); |
| 739 | if (i != CS_SUCCESS) |
| 740 | return i; |
| 741 | |
| 742 | return pcmcia_parse_tuple(handle, tuple, parse); |
| 743 | } |
| 744 | |
| 745 | static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse) |
| 746 | { |
| 747 | if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS) |
| 748 | return CS_NO_MORE_ITEMS; |
| 749 | return get_tuple(handle, tuple, parse); |
| 750 | } |
| 751 | |
| 752 | static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse) |
| 753 | { |
| 754 | if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS) |
| 755 | return CS_NO_MORE_ITEMS; |
| 756 | return get_tuple(handle, tuple, parse); |
| 757 | } |
| 758 | |
| 759 | static void bt3c_config(dev_link_t *link) |
| 760 | { |
| 761 | static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 }; |
| 762 | client_handle_t handle = link->handle; |
| 763 | bt3c_info_t *info = link->priv; |
| 764 | tuple_t tuple; |
| 765 | u_short buf[256]; |
| 766 | cisparse_t parse; |
| 767 | cistpl_cftable_entry_t *cf = &parse.cftable_entry; |
| 768 | config_info_t config; |
| 769 | int i, j, try, last_ret, last_fn; |
| 770 | |
| 771 | tuple.TupleData = (cisdata_t *)buf; |
| 772 | tuple.TupleOffset = 0; |
| 773 | tuple.TupleDataMax = 255; |
| 774 | tuple.Attributes = 0; |
| 775 | |
| 776 | /* Get configuration register information */ |
| 777 | tuple.DesiredTuple = CISTPL_CONFIG; |
| 778 | last_ret = first_tuple(handle, &tuple, &parse); |
| 779 | if (last_ret != CS_SUCCESS) { |
| 780 | last_fn = ParseTuple; |
| 781 | goto cs_failed; |
| 782 | } |
| 783 | link->conf.ConfigBase = parse.config.base; |
| 784 | link->conf.Present = parse.config.rmask[0]; |
| 785 | |
| 786 | /* Configure card */ |
| 787 | link->state |= DEV_CONFIG; |
| 788 | i = pcmcia_get_configuration_info(handle, &config); |
| 789 | link->conf.Vcc = config.Vcc; |
| 790 | |
| 791 | /* First pass: look for a config entry that looks normal. */ |
| 792 | tuple.TupleData = (cisdata_t *)buf; |
| 793 | tuple.TupleOffset = 0; |
| 794 | tuple.TupleDataMax = 255; |
| 795 | tuple.Attributes = 0; |
| 796 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; |
| 797 | /* Two tries: without IO aliases, then with aliases */ |
| 798 | for (try = 0; try < 2; try++) { |
| 799 | i = first_tuple(handle, &tuple, &parse); |
| 800 | while (i != CS_NO_MORE_ITEMS) { |
| 801 | if (i != CS_SUCCESS) |
| 802 | goto next_entry; |
| 803 | if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM)) |
| 804 | link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000; |
| 805 | if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) { |
| 806 | link->conf.ConfigIndex = cf->index; |
| 807 | link->io.BasePort1 = cf->io.win[0].base; |
| 808 | link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK; |
| 809 | i = pcmcia_request_io(link->handle, &link->io); |
| 810 | if (i == CS_SUCCESS) |
| 811 | goto found_port; |
| 812 | } |
| 813 | next_entry: |
| 814 | i = next_tuple(handle, &tuple, &parse); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* Second pass: try to find an entry that isn't picky about |
| 819 | its base address, then try to grab any standard serial port |
| 820 | address, and finally try to get any free port. */ |
| 821 | i = first_tuple(handle, &tuple, &parse); |
| 822 | while (i != CS_NO_MORE_ITEMS) { |
| 823 | if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) { |
| 824 | link->conf.ConfigIndex = cf->index; |
| 825 | for (j = 0; j < 5; j++) { |
| 826 | link->io.BasePort1 = base[j]; |
| 827 | link->io.IOAddrLines = base[j] ? 16 : 3; |
| 828 | i = pcmcia_request_io(link->handle, &link->io); |
| 829 | if (i == CS_SUCCESS) |
| 830 | goto found_port; |
| 831 | } |
| 832 | } |
| 833 | i = next_tuple(handle, &tuple, &parse); |
| 834 | } |
| 835 | |
| 836 | found_port: |
| 837 | if (i != CS_SUCCESS) { |
| 838 | BT_ERR("No usable port range found"); |
| 839 | cs_error(link->handle, RequestIO, i); |
| 840 | goto failed; |
| 841 | } |
| 842 | |
| 843 | i = pcmcia_request_irq(link->handle, &link->irq); |
| 844 | if (i != CS_SUCCESS) { |
| 845 | cs_error(link->handle, RequestIRQ, i); |
| 846 | link->irq.AssignedIRQ = 0; |
| 847 | } |
| 848 | |
| 849 | i = pcmcia_request_configuration(link->handle, &link->conf); |
| 850 | if (i != CS_SUCCESS) { |
| 851 | cs_error(link->handle, RequestConfiguration, i); |
| 852 | goto failed; |
| 853 | } |
| 854 | |
| 855 | if (bt3c_open(info) != 0) |
| 856 | goto failed; |
| 857 | |
| 858 | strcpy(info->node.dev_name, info->hdev->name); |
| 859 | link->dev = &info->node; |
| 860 | link->state &= ~DEV_CONFIG_PENDING; |
| 861 | |
| 862 | return; |
| 863 | |
| 864 | cs_failed: |
| 865 | cs_error(link->handle, last_fn, last_ret); |
| 866 | |
| 867 | failed: |
| 868 | bt3c_release(link); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | static void bt3c_release(dev_link_t *link) |
| 873 | { |
| 874 | bt3c_info_t *info = link->priv; |
| 875 | |
| 876 | if (link->state & DEV_PRESENT) |
| 877 | bt3c_close(info); |
| 878 | |
| 879 | link->dev = NULL; |
| 880 | |
| 881 | pcmcia_release_configuration(link->handle); |
| 882 | pcmcia_release_io(link->handle, &link->io); |
| 883 | pcmcia_release_irq(link->handle, &link->irq); |
| 884 | |
| 885 | link->state &= ~DEV_CONFIG; |
| 886 | } |
| 887 | |
Dominik Brodowski | 98e4c28 | 2005-11-14 21:21:18 +0100 | [diff] [blame] | 888 | static int bt3c_suspend(struct pcmcia_device *dev) |
| 889 | { |
| 890 | dev_link_t *link = dev_to_instance(dev); |
| 891 | |
| 892 | link->state |= DEV_SUSPEND; |
| 893 | if (link->state & DEV_CONFIG) |
| 894 | pcmcia_release_configuration(link->handle); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static int bt3c_resume(struct pcmcia_device *dev) |
| 900 | { |
| 901 | dev_link_t *link = dev_to_instance(dev); |
| 902 | |
| 903 | link->state &= ~DEV_SUSPEND; |
| 904 | if (DEV_OK(link)) |
| 905 | pcmcia_request_configuration(link->handle, &link->conf); |
| 906 | |
| 907 | return 0; |
| 908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
| 910 | static int bt3c_event(event_t event, int priority, event_callback_args_t *args) |
| 911 | { |
| 912 | dev_link_t *link = args->client_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | switch (event) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | case CS_EVENT_CARD_INSERTION: |
| 916 | link->state |= DEV_PRESENT | DEV_CONFIG_PENDING; |
| 917 | bt3c_config(link); |
| 918 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
Dominik Brodowski | a01c3ed | 2005-06-27 16:28:37 -0700 | [diff] [blame] | 924 | static struct pcmcia_device_id bt3c_ids[] = { |
| 925 | PCMCIA_DEVICE_PROD_ID13("3COM", "Bluetooth PC Card", 0xefce0a31, 0xd4ce9b02), |
| 926 | PCMCIA_DEVICE_NULL |
| 927 | }; |
| 928 | MODULE_DEVICE_TABLE(pcmcia, bt3c_ids); |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | static struct pcmcia_driver bt3c_driver = { |
| 931 | .owner = THIS_MODULE, |
| 932 | .drv = { |
| 933 | .name = "bt3c_cs", |
| 934 | }, |
| 935 | .attach = bt3c_attach, |
Dominik Brodowski | 1e212f3 | 2005-07-07 17:59:00 -0700 | [diff] [blame] | 936 | .event = bt3c_event, |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame^] | 937 | .remove = bt3c_detach, |
Dominik Brodowski | a01c3ed | 2005-06-27 16:28:37 -0700 | [diff] [blame] | 938 | .id_table = bt3c_ids, |
Dominik Brodowski | 98e4c28 | 2005-11-14 21:21:18 +0100 | [diff] [blame] | 939 | .suspend = bt3c_suspend, |
| 940 | .resume = bt3c_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | }; |
| 942 | |
| 943 | static int __init init_bt3c_cs(void) |
| 944 | { |
| 945 | return pcmcia_register_driver(&bt3c_driver); |
| 946 | } |
| 947 | |
| 948 | |
| 949 | static void __exit exit_bt3c_cs(void) |
| 950 | { |
| 951 | pcmcia_unregister_driver(&bt3c_driver); |
| 952 | BUG_ON(dev_list != NULL); |
| 953 | } |
| 954 | |
| 955 | module_init(init_bt3c_cs); |
| 956 | module_exit(exit_bt3c_cs); |