Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * A driver for Nokia Connectivity Card DTL-1 devices |
| 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 Nokia Connectivity Card DTL-1"); |
| 63 | MODULE_LICENSE("GPL"); |
| 64 | |
| 65 | |
| 66 | |
| 67 | /* ======================== Local structures ======================== */ |
| 68 | |
| 69 | |
| 70 | typedef struct dtl1_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 | unsigned long flowmask; /* HCI flow mask */ |
| 79 | int ri_latch; |
| 80 | |
| 81 | struct sk_buff_head txq; |
| 82 | unsigned long tx_state; |
| 83 | |
| 84 | unsigned long rx_state; |
| 85 | unsigned long rx_count; |
| 86 | struct sk_buff *rx_skb; |
| 87 | } dtl1_info_t; |
| 88 | |
| 89 | |
| 90 | static void dtl1_config(dev_link_t *link); |
| 91 | static void dtl1_release(dev_link_t *link); |
| 92 | static int dtl1_event(event_t event, int priority, event_callback_args_t *args); |
| 93 | |
| 94 | static dev_info_t dev_info = "dtl1_cs"; |
| 95 | |
| 96 | static dev_link_t *dtl1_attach(void); |
| 97 | static void dtl1_detach(dev_link_t *); |
| 98 | |
| 99 | static dev_link_t *dev_list = NULL; |
| 100 | |
| 101 | |
| 102 | /* Transmit states */ |
| 103 | #define XMIT_SENDING 1 |
| 104 | #define XMIT_WAKEUP 2 |
| 105 | #define XMIT_WAITING 8 |
| 106 | |
| 107 | /* Receiver States */ |
| 108 | #define RECV_WAIT_NSH 0 |
| 109 | #define RECV_WAIT_DATA 1 |
| 110 | |
| 111 | |
| 112 | typedef struct { |
| 113 | u8 type; |
| 114 | u8 zero; |
| 115 | u16 len; |
| 116 | } __attribute__ ((packed)) nsh_t; /* Nokia Specific Header */ |
| 117 | |
| 118 | #define NSHL 4 /* Nokia Specific Header Length */ |
| 119 | |
| 120 | |
| 121 | |
| 122 | /* ======================== Interrupt handling ======================== */ |
| 123 | |
| 124 | |
| 125 | static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len) |
| 126 | { |
| 127 | int actual = 0; |
| 128 | |
| 129 | /* Tx FIFO should be empty */ |
| 130 | if (!(inb(iobase + UART_LSR) & UART_LSR_THRE)) |
| 131 | return 0; |
| 132 | |
| 133 | /* Fill FIFO with current frame */ |
| 134 | while ((fifo_size-- > 0) && (actual < len)) { |
| 135 | /* Transmit next byte */ |
| 136 | outb(buf[actual], iobase + UART_TX); |
| 137 | actual++; |
| 138 | } |
| 139 | |
| 140 | return actual; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static void dtl1_write_wakeup(dtl1_info_t *info) |
| 145 | { |
| 146 | if (!info) { |
| 147 | BT_ERR("Unknown device"); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if (test_bit(XMIT_WAITING, &(info->tx_state))) { |
| 152 | set_bit(XMIT_WAKEUP, &(info->tx_state)); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) { |
| 157 | set_bit(XMIT_WAKEUP, &(info->tx_state)); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | do { |
| 162 | register unsigned int iobase = info->link.io.BasePort1; |
| 163 | register struct sk_buff *skb; |
| 164 | register int len; |
| 165 | |
| 166 | clear_bit(XMIT_WAKEUP, &(info->tx_state)); |
| 167 | |
| 168 | if (!(info->link.state & DEV_PRESENT)) |
| 169 | return; |
| 170 | |
| 171 | if (!(skb = skb_dequeue(&(info->txq)))) |
| 172 | break; |
| 173 | |
| 174 | /* Send frame */ |
| 175 | len = dtl1_write(iobase, 32, skb->data, skb->len); |
| 176 | |
| 177 | if (len == skb->len) { |
| 178 | set_bit(XMIT_WAITING, &(info->tx_state)); |
| 179 | kfree_skb(skb); |
| 180 | } else { |
| 181 | skb_pull(skb, len); |
| 182 | skb_queue_head(&(info->txq), skb); |
| 183 | } |
| 184 | |
| 185 | info->hdev->stat.byte_tx += len; |
| 186 | |
| 187 | } while (test_bit(XMIT_WAKEUP, &(info->tx_state))); |
| 188 | |
| 189 | clear_bit(XMIT_SENDING, &(info->tx_state)); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb) |
| 194 | { |
| 195 | u8 flowmask = *(u8 *)skb->data; |
| 196 | int i; |
| 197 | |
| 198 | printk(KERN_INFO "Bluetooth: Nokia control data ="); |
| 199 | for (i = 0; i < skb->len; i++) { |
| 200 | printk(" %02x", skb->data[i]); |
| 201 | } |
| 202 | printk("\n"); |
| 203 | |
| 204 | /* transition to active state */ |
| 205 | if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) { |
| 206 | clear_bit(XMIT_WAITING, &(info->tx_state)); |
| 207 | dtl1_write_wakeup(info); |
| 208 | } |
| 209 | |
| 210 | info->flowmask = flowmask; |
| 211 | |
| 212 | kfree_skb(skb); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static void dtl1_receive(dtl1_info_t *info) |
| 217 | { |
| 218 | unsigned int iobase; |
| 219 | nsh_t *nsh; |
| 220 | int boguscount = 0; |
| 221 | |
| 222 | if (!info) { |
| 223 | BT_ERR("Unknown device"); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | iobase = info->link.io.BasePort1; |
| 228 | |
| 229 | do { |
| 230 | info->hdev->stat.byte_rx++; |
| 231 | |
| 232 | /* Allocate packet */ |
| 233 | if (info->rx_skb == NULL) |
| 234 | if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) { |
| 235 | BT_ERR("Can't allocate mem for new packet"); |
| 236 | info->rx_state = RECV_WAIT_NSH; |
| 237 | info->rx_count = NSHL; |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX); |
| 242 | nsh = (nsh_t *)info->rx_skb->data; |
| 243 | |
| 244 | info->rx_count--; |
| 245 | |
| 246 | if (info->rx_count == 0) { |
| 247 | |
| 248 | switch (info->rx_state) { |
| 249 | case RECV_WAIT_NSH: |
| 250 | info->rx_state = RECV_WAIT_DATA; |
| 251 | info->rx_count = nsh->len + (nsh->len & 0x0001); |
| 252 | break; |
| 253 | case RECV_WAIT_DATA: |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 254 | bt_cb(info->rx_skb)->pkt_type = nsh->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | /* remove PAD byte if it exists */ |
| 257 | if (nsh->len & 0x0001) { |
| 258 | info->rx_skb->tail--; |
| 259 | info->rx_skb->len--; |
| 260 | } |
| 261 | |
| 262 | /* remove NSH */ |
| 263 | skb_pull(info->rx_skb, NSHL); |
| 264 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 265 | switch (bt_cb(info->rx_skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | case 0x80: |
| 267 | /* control data for the Nokia Card */ |
| 268 | dtl1_control(info, info->rx_skb); |
| 269 | break; |
| 270 | case 0x82: |
| 271 | case 0x83: |
| 272 | case 0x84: |
| 273 | /* send frame to the HCI layer */ |
| 274 | info->rx_skb->dev = (void *) info->hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 275 | bt_cb(info->rx_skb)->pkt_type &= 0x0f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | hci_recv_frame(info->rx_skb); |
| 277 | break; |
| 278 | default: |
| 279 | /* unknown packet */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 280 | 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] | 281 | kfree_skb(info->rx_skb); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | info->rx_state = RECV_WAIT_NSH; |
| 286 | info->rx_count = NSHL; |
| 287 | info->rx_skb = NULL; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | } |
| 292 | |
| 293 | /* Make sure we don't stay here too long */ |
| 294 | if (boguscount++ > 32) |
| 295 | break; |
| 296 | |
| 297 | } while (inb(iobase + UART_LSR) & UART_LSR_DR); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs) |
| 302 | { |
| 303 | dtl1_info_t *info = dev_inst; |
| 304 | unsigned int iobase; |
| 305 | unsigned char msr; |
| 306 | int boguscount = 0; |
| 307 | int iir, lsr; |
| 308 | |
| 309 | if (!info || !info->hdev) { |
| 310 | BT_ERR("Call of irq %d for unknown device", irq); |
| 311 | return IRQ_NONE; |
| 312 | } |
| 313 | |
| 314 | iobase = info->link.io.BasePort1; |
| 315 | |
| 316 | spin_lock(&(info->lock)); |
| 317 | |
| 318 | iir = inb(iobase + UART_IIR) & UART_IIR_ID; |
| 319 | while (iir) { |
| 320 | |
| 321 | /* Clear interrupt */ |
| 322 | lsr = inb(iobase + UART_LSR); |
| 323 | |
| 324 | switch (iir) { |
| 325 | case UART_IIR_RLSI: |
| 326 | BT_ERR("RLSI"); |
| 327 | break; |
| 328 | case UART_IIR_RDI: |
| 329 | /* Receive interrupt */ |
| 330 | dtl1_receive(info); |
| 331 | break; |
| 332 | case UART_IIR_THRI: |
| 333 | if (lsr & UART_LSR_THRE) { |
| 334 | /* Transmitter ready for data */ |
| 335 | dtl1_write_wakeup(info); |
| 336 | } |
| 337 | break; |
| 338 | default: |
| 339 | BT_ERR("Unhandled IIR=%#x", iir); |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | /* Make sure we don't stay here too long */ |
| 344 | if (boguscount++ > 100) |
| 345 | break; |
| 346 | |
| 347 | iir = inb(iobase + UART_IIR) & UART_IIR_ID; |
| 348 | |
| 349 | } |
| 350 | |
| 351 | msr = inb(iobase + UART_MSR); |
| 352 | |
| 353 | if (info->ri_latch ^ (msr & UART_MSR_RI)) { |
| 354 | info->ri_latch = msr & UART_MSR_RI; |
| 355 | clear_bit(XMIT_WAITING, &(info->tx_state)); |
| 356 | dtl1_write_wakeup(info); |
| 357 | } |
| 358 | |
| 359 | spin_unlock(&(info->lock)); |
| 360 | |
| 361 | return IRQ_HANDLED; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | /* ======================== HCI interface ======================== */ |
| 367 | |
| 368 | |
| 369 | static int dtl1_hci_open(struct hci_dev *hdev) |
| 370 | { |
| 371 | set_bit(HCI_RUNNING, &(hdev->flags)); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | static int dtl1_hci_flush(struct hci_dev *hdev) |
| 378 | { |
| 379 | dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data); |
| 380 | |
| 381 | /* Drop TX queue */ |
| 382 | skb_queue_purge(&(info->txq)); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | static int dtl1_hci_close(struct hci_dev *hdev) |
| 389 | { |
| 390 | if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags))) |
| 391 | return 0; |
| 392 | |
| 393 | dtl1_hci_flush(hdev); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static int dtl1_hci_send_frame(struct sk_buff *skb) |
| 400 | { |
| 401 | dtl1_info_t *info; |
| 402 | struct hci_dev *hdev = (struct hci_dev *)(skb->dev); |
| 403 | struct sk_buff *s; |
| 404 | nsh_t nsh; |
| 405 | |
| 406 | if (!hdev) { |
| 407 | BT_ERR("Frame for unknown HCI device (hdev=NULL)"); |
| 408 | return -ENODEV; |
| 409 | } |
| 410 | |
| 411 | info = (dtl1_info_t *)(hdev->driver_data); |
| 412 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 413 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | case HCI_COMMAND_PKT: |
| 415 | hdev->stat.cmd_tx++; |
| 416 | nsh.type = 0x81; |
| 417 | break; |
| 418 | case HCI_ACLDATA_PKT: |
| 419 | hdev->stat.acl_tx++; |
| 420 | nsh.type = 0x82; |
| 421 | break; |
| 422 | case HCI_SCODATA_PKT: |
| 423 | hdev->stat.sco_tx++; |
| 424 | nsh.type = 0x83; |
| 425 | break; |
| 426 | }; |
| 427 | |
| 428 | nsh.zero = 0; |
| 429 | nsh.len = skb->len; |
| 430 | |
| 431 | s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC); |
| 432 | skb_reserve(s, NSHL); |
| 433 | memcpy(skb_put(s, skb->len), skb->data, skb->len); |
| 434 | if (skb->len & 0x0001) |
| 435 | *skb_put(s, 1) = 0; /* PAD */ |
| 436 | |
| 437 | /* Prepend skb with Nokia frame header and queue */ |
| 438 | memcpy(skb_push(s, NSHL), &nsh, NSHL); |
| 439 | skb_queue_tail(&(info->txq), s); |
| 440 | |
| 441 | dtl1_write_wakeup(info); |
| 442 | |
| 443 | kfree_skb(skb); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | static void dtl1_hci_destruct(struct hci_dev *hdev) |
| 450 | { |
| 451 | } |
| 452 | |
| 453 | |
| 454 | static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg) |
| 455 | { |
| 456 | return -ENOIOCTLCMD; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | |
| 461 | /* ======================== Card services HCI interaction ======================== */ |
| 462 | |
| 463 | |
| 464 | static int dtl1_open(dtl1_info_t *info) |
| 465 | { |
| 466 | unsigned long flags; |
| 467 | unsigned int iobase = info->link.io.BasePort1; |
| 468 | struct hci_dev *hdev; |
| 469 | |
| 470 | spin_lock_init(&(info->lock)); |
| 471 | |
| 472 | skb_queue_head_init(&(info->txq)); |
| 473 | |
| 474 | info->rx_state = RECV_WAIT_NSH; |
| 475 | info->rx_count = NSHL; |
| 476 | info->rx_skb = NULL; |
| 477 | |
| 478 | set_bit(XMIT_WAITING, &(info->tx_state)); |
| 479 | |
| 480 | /* Initialize HCI device */ |
| 481 | hdev = hci_alloc_dev(); |
| 482 | if (!hdev) { |
| 483 | BT_ERR("Can't allocate HCI device"); |
| 484 | return -ENOMEM; |
| 485 | } |
| 486 | |
| 487 | info->hdev = hdev; |
| 488 | |
| 489 | hdev->type = HCI_PCCARD; |
| 490 | hdev->driver_data = info; |
| 491 | |
| 492 | hdev->open = dtl1_hci_open; |
| 493 | hdev->close = dtl1_hci_close; |
| 494 | hdev->flush = dtl1_hci_flush; |
| 495 | hdev->send = dtl1_hci_send_frame; |
| 496 | hdev->destruct = dtl1_hci_destruct; |
| 497 | hdev->ioctl = dtl1_hci_ioctl; |
| 498 | |
| 499 | hdev->owner = THIS_MODULE; |
| 500 | |
| 501 | spin_lock_irqsave(&(info->lock), flags); |
| 502 | |
| 503 | /* Reset UART */ |
| 504 | outb(0, iobase + UART_MCR); |
| 505 | |
| 506 | /* Turn off interrupts */ |
| 507 | outb(0, iobase + UART_IER); |
| 508 | |
| 509 | /* Initialize UART */ |
| 510 | outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */ |
| 511 | outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR); |
| 512 | |
| 513 | info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI; |
| 514 | |
| 515 | /* Turn on interrupts */ |
| 516 | outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER); |
| 517 | |
| 518 | spin_unlock_irqrestore(&(info->lock), flags); |
| 519 | |
| 520 | /* Timeout before it is safe to send the first HCI packet */ |
| 521 | msleep(2000); |
| 522 | |
| 523 | /* Register HCI device */ |
| 524 | if (hci_register_dev(hdev) < 0) { |
| 525 | BT_ERR("Can't register HCI device"); |
| 526 | info->hdev = NULL; |
| 527 | hci_free_dev(hdev); |
| 528 | return -ENODEV; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | static int dtl1_close(dtl1_info_t *info) |
| 536 | { |
| 537 | unsigned long flags; |
| 538 | unsigned int iobase = info->link.io.BasePort1; |
| 539 | struct hci_dev *hdev = info->hdev; |
| 540 | |
| 541 | if (!hdev) |
| 542 | return -ENODEV; |
| 543 | |
| 544 | dtl1_hci_close(hdev); |
| 545 | |
| 546 | spin_lock_irqsave(&(info->lock), flags); |
| 547 | |
| 548 | /* Reset UART */ |
| 549 | outb(0, iobase + UART_MCR); |
| 550 | |
| 551 | /* Turn off interrupts */ |
| 552 | outb(0, iobase + UART_IER); |
| 553 | |
| 554 | spin_unlock_irqrestore(&(info->lock), flags); |
| 555 | |
| 556 | if (hci_unregister_dev(hdev) < 0) |
| 557 | BT_ERR("Can't unregister HCI device %s", hdev->name); |
| 558 | |
| 559 | hci_free_dev(hdev); |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static dev_link_t *dtl1_attach(void) |
| 565 | { |
| 566 | dtl1_info_t *info; |
| 567 | client_reg_t client_reg; |
| 568 | dev_link_t *link; |
| 569 | int ret; |
| 570 | |
| 571 | /* Create new info device */ |
Deepak Saxena | 089b1db | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 572 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (!info) |
| 574 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | link = &info->link; |
| 577 | link->priv = info; |
| 578 | |
| 579 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_8; |
| 580 | link->io.NumPorts1 = 8; |
| 581 | link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT; |
| 582 | link->irq.IRQInfo1 = IRQ_LEVEL_ID; |
| 583 | |
| 584 | link->irq.Handler = dtl1_interrupt; |
| 585 | link->irq.Instance = info; |
| 586 | |
| 587 | link->conf.Attributes = CONF_ENABLE_IRQ; |
| 588 | link->conf.Vcc = 50; |
| 589 | link->conf.IntType = INT_MEMORY_AND_IO; |
| 590 | |
| 591 | /* Register with Card Services */ |
| 592 | link->next = dev_list; |
| 593 | dev_list = link; |
| 594 | client_reg.dev_info = &dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | client_reg.Version = 0x0210; |
| 596 | client_reg.event_callback_args.client_data = link; |
| 597 | |
| 598 | ret = pcmcia_register_client(&link->handle, &client_reg); |
| 599 | if (ret != CS_SUCCESS) { |
| 600 | cs_error(link->handle, RegisterClient, ret); |
| 601 | dtl1_detach(link); |
| 602 | return NULL; |
| 603 | } |
| 604 | |
| 605 | return link; |
| 606 | } |
| 607 | |
| 608 | |
| 609 | static void dtl1_detach(dev_link_t *link) |
| 610 | { |
| 611 | dtl1_info_t *info = link->priv; |
| 612 | dev_link_t **linkp; |
| 613 | int ret; |
| 614 | |
| 615 | /* Locate device structure */ |
| 616 | for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) |
| 617 | if (*linkp == link) |
| 618 | break; |
| 619 | |
| 620 | if (*linkp == NULL) |
| 621 | return; |
| 622 | |
| 623 | if (link->state & DEV_CONFIG) |
| 624 | dtl1_release(link); |
| 625 | |
| 626 | if (link->handle) { |
| 627 | ret = pcmcia_deregister_client(link->handle); |
| 628 | if (ret != CS_SUCCESS) |
| 629 | cs_error(link->handle, DeregisterClient, ret); |
| 630 | } |
| 631 | |
| 632 | /* Unlink device structure, free bits */ |
| 633 | *linkp = link->next; |
| 634 | |
| 635 | kfree(info); |
| 636 | } |
| 637 | |
| 638 | static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse) |
| 639 | { |
| 640 | int i; |
| 641 | |
| 642 | i = pcmcia_get_tuple_data(handle, tuple); |
| 643 | if (i != CS_SUCCESS) |
| 644 | return i; |
| 645 | |
| 646 | return pcmcia_parse_tuple(handle, tuple, parse); |
| 647 | } |
| 648 | |
| 649 | static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse) |
| 650 | { |
| 651 | if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS) |
| 652 | return CS_NO_MORE_ITEMS; |
| 653 | return get_tuple(handle, tuple, parse); |
| 654 | } |
| 655 | |
| 656 | static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse) |
| 657 | { |
| 658 | if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS) |
| 659 | return CS_NO_MORE_ITEMS; |
| 660 | return get_tuple(handle, tuple, parse); |
| 661 | } |
| 662 | |
| 663 | static void dtl1_config(dev_link_t *link) |
| 664 | { |
| 665 | client_handle_t handle = link->handle; |
| 666 | dtl1_info_t *info = link->priv; |
| 667 | tuple_t tuple; |
| 668 | u_short buf[256]; |
| 669 | cisparse_t parse; |
| 670 | cistpl_cftable_entry_t *cf = &parse.cftable_entry; |
| 671 | config_info_t config; |
| 672 | int i, last_ret, last_fn; |
| 673 | |
| 674 | tuple.TupleData = (cisdata_t *)buf; |
| 675 | tuple.TupleOffset = 0; |
| 676 | tuple.TupleDataMax = 255; |
| 677 | tuple.Attributes = 0; |
| 678 | |
| 679 | /* Get configuration register information */ |
| 680 | tuple.DesiredTuple = CISTPL_CONFIG; |
| 681 | last_ret = first_tuple(handle, &tuple, &parse); |
| 682 | if (last_ret != CS_SUCCESS) { |
| 683 | last_fn = ParseTuple; |
| 684 | goto cs_failed; |
| 685 | } |
| 686 | link->conf.ConfigBase = parse.config.base; |
| 687 | link->conf.Present = parse.config.rmask[0]; |
| 688 | |
| 689 | /* Configure card */ |
| 690 | link->state |= DEV_CONFIG; |
| 691 | i = pcmcia_get_configuration_info(handle, &config); |
| 692 | link->conf.Vcc = config.Vcc; |
| 693 | |
| 694 | tuple.TupleData = (cisdata_t *)buf; |
| 695 | tuple.TupleOffset = 0; |
| 696 | tuple.TupleDataMax = 255; |
| 697 | tuple.Attributes = 0; |
| 698 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; |
| 699 | |
| 700 | /* Look for a generic full-sized window */ |
| 701 | link->io.NumPorts1 = 8; |
| 702 | i = first_tuple(handle, &tuple, &parse); |
| 703 | while (i != CS_NO_MORE_ITEMS) { |
| 704 | if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) { |
| 705 | link->conf.ConfigIndex = cf->index; |
| 706 | link->io.BasePort1 = cf->io.win[0].base; |
| 707 | link->io.NumPorts1 = cf->io.win[0].len; /*yo */ |
| 708 | link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK; |
| 709 | i = pcmcia_request_io(link->handle, &link->io); |
| 710 | if (i == CS_SUCCESS) |
| 711 | break; |
| 712 | } |
| 713 | i = next_tuple(handle, &tuple, &parse); |
| 714 | } |
| 715 | |
| 716 | if (i != CS_SUCCESS) { |
| 717 | cs_error(link->handle, RequestIO, i); |
| 718 | goto failed; |
| 719 | } |
| 720 | |
| 721 | i = pcmcia_request_irq(link->handle, &link->irq); |
| 722 | if (i != CS_SUCCESS) { |
| 723 | cs_error(link->handle, RequestIRQ, i); |
| 724 | link->irq.AssignedIRQ = 0; |
| 725 | } |
| 726 | |
| 727 | i = pcmcia_request_configuration(link->handle, &link->conf); |
| 728 | if (i != CS_SUCCESS) { |
| 729 | cs_error(link->handle, RequestConfiguration, i); |
| 730 | goto failed; |
| 731 | } |
| 732 | |
| 733 | if (dtl1_open(info) != 0) |
| 734 | goto failed; |
| 735 | |
| 736 | strcpy(info->node.dev_name, info->hdev->name); |
| 737 | link->dev = &info->node; |
| 738 | link->state &= ~DEV_CONFIG_PENDING; |
| 739 | |
| 740 | return; |
| 741 | |
| 742 | cs_failed: |
| 743 | cs_error(link->handle, last_fn, last_ret); |
| 744 | |
| 745 | failed: |
| 746 | dtl1_release(link); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | static void dtl1_release(dev_link_t *link) |
| 751 | { |
| 752 | dtl1_info_t *info = link->priv; |
| 753 | |
| 754 | if (link->state & DEV_PRESENT) |
| 755 | dtl1_close(info); |
| 756 | |
| 757 | link->dev = NULL; |
| 758 | |
| 759 | pcmcia_release_configuration(link->handle); |
| 760 | pcmcia_release_io(link->handle, &link->io); |
| 761 | pcmcia_release_irq(link->handle, &link->irq); |
| 762 | |
| 763 | link->state &= ~DEV_CONFIG; |
| 764 | } |
| 765 | |
Dominik Brodowski | 98e4c28 | 2005-11-14 21:21:18 +0100 | [diff] [blame^] | 766 | static int dtl1_suspend(struct pcmcia_device *dev) |
| 767 | { |
| 768 | dev_link_t *link = dev_to_instance(dev); |
| 769 | |
| 770 | link->state |= DEV_SUSPEND; |
| 771 | if (link->state & DEV_CONFIG) |
| 772 | pcmcia_release_configuration(link->handle); |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static int dtl1_resume(struct pcmcia_device *dev) |
| 778 | { |
| 779 | dev_link_t *link = dev_to_instance(dev); |
| 780 | |
| 781 | link->state &= ~DEV_SUSPEND; |
| 782 | if (DEV_OK(link)) |
| 783 | pcmcia_request_configuration(link->handle, &link->conf); |
| 784 | |
| 785 | return 0; |
| 786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
| 788 | static int dtl1_event(event_t event, int priority, event_callback_args_t *args) |
| 789 | { |
| 790 | dev_link_t *link = args->client_data; |
| 791 | dtl1_info_t *info = link->priv; |
| 792 | |
| 793 | switch (event) { |
| 794 | case CS_EVENT_CARD_REMOVAL: |
| 795 | link->state &= ~DEV_PRESENT; |
| 796 | if (link->state & DEV_CONFIG) { |
| 797 | dtl1_close(info); |
| 798 | dtl1_release(link); |
| 799 | } |
| 800 | break; |
| 801 | case CS_EVENT_CARD_INSERTION: |
| 802 | link->state |= DEV_PRESENT | DEV_CONFIG_PENDING; |
| 803 | dtl1_config(link); |
| 804 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Dominik Brodowski | 88eca2e | 2005-06-27 16:28:39 -0700 | [diff] [blame] | 810 | static struct pcmcia_device_id dtl1_ids[] = { |
| 811 | PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d), |
| 812 | PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863), |
| 813 | PCMCIA_DEVICE_NULL |
| 814 | }; |
| 815 | MODULE_DEVICE_TABLE(pcmcia, dtl1_ids); |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | static struct pcmcia_driver dtl1_driver = { |
| 818 | .owner = THIS_MODULE, |
| 819 | .drv = { |
| 820 | .name = "dtl1_cs", |
| 821 | }, |
| 822 | .attach = dtl1_attach, |
Dominik Brodowski | 1e212f3 | 2005-07-07 17:59:00 -0700 | [diff] [blame] | 823 | .event = dtl1_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | .detach = dtl1_detach, |
Dominik Brodowski | 88eca2e | 2005-06-27 16:28:39 -0700 | [diff] [blame] | 825 | .id_table = dtl1_ids, |
Dominik Brodowski | 98e4c28 | 2005-11-14 21:21:18 +0100 | [diff] [blame^] | 826 | .suspend = dtl1_suspend, |
| 827 | .resume = dtl1_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | }; |
| 829 | |
| 830 | static int __init init_dtl1_cs(void) |
| 831 | { |
| 832 | return pcmcia_register_driver(&dtl1_driver); |
| 833 | } |
| 834 | |
| 835 | |
| 836 | static void __exit exit_dtl1_cs(void) |
| 837 | { |
| 838 | pcmcia_unregister_driver(&dtl1_driver); |
| 839 | BUG_ON(dev_list != NULL); |
| 840 | } |
| 841 | |
| 842 | module_init(init_dtl1_cs); |
| 843 | module_exit(exit_dtl1_cs); |