3 * A driver for Nokia Connectivity Card DTL-1 devices
5 * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org>
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;
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.
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.
23 #include <linux/config.h>
24 #include <linux/module.h>
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>
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>
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>
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/hci_core.h>
58 /* ======================== Module parameters ======================== */
61 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
62 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
63 MODULE_LICENSE("GPL");
67 /* ======================== Local structures ======================== */
70 typedef struct dtl1_info_t
{
76 spinlock_t lock
; /* For serializing operations */
78 unsigned long flowmask
; /* HCI flow mask */
81 struct sk_buff_head txq
;
82 unsigned long tx_state
;
84 unsigned long rx_state
;
85 unsigned long rx_count
;
86 struct sk_buff
*rx_skb
;
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
);
94 static dev_info_t dev_info
= "dtl1_cs";
96 static dev_link_t
*dtl1_attach(void);
97 static void dtl1_detach(struct pcmcia_device
*p_dev
);
100 /* Transmit states */
101 #define XMIT_SENDING 1
102 #define XMIT_WAKEUP 2
103 #define XMIT_WAITING 8
105 /* Receiver States */
106 #define RECV_WAIT_NSH 0
107 #define RECV_WAIT_DATA 1
114 } __attribute__ ((packed
)) nsh_t
; /* Nokia Specific Header */
116 #define NSHL 4 /* Nokia Specific Header Length */
120 /* ======================== Interrupt handling ======================== */
123 static int dtl1_write(unsigned int iobase
, int fifo_size
, __u8
*buf
, int len
)
127 /* Tx FIFO should be empty */
128 if (!(inb(iobase
+ UART_LSR
) & UART_LSR_THRE
))
131 /* Fill FIFO with current frame */
132 while ((fifo_size
-- > 0) && (actual
< len
)) {
133 /* Transmit next byte */
134 outb(buf
[actual
], iobase
+ UART_TX
);
142 static void dtl1_write_wakeup(dtl1_info_t
*info
)
145 BT_ERR("Unknown device");
149 if (test_bit(XMIT_WAITING
, &(info
->tx_state
))) {
150 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
154 if (test_and_set_bit(XMIT_SENDING
, &(info
->tx_state
))) {
155 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
160 register unsigned int iobase
= info
->link
.io
.BasePort1
;
161 register struct sk_buff
*skb
;
164 clear_bit(XMIT_WAKEUP
, &(info
->tx_state
));
166 if (!(info
->link
.state
& DEV_PRESENT
))
169 if (!(skb
= skb_dequeue(&(info
->txq
))))
173 len
= dtl1_write(iobase
, 32, skb
->data
, skb
->len
);
175 if (len
== skb
->len
) {
176 set_bit(XMIT_WAITING
, &(info
->tx_state
));
180 skb_queue_head(&(info
->txq
), skb
);
183 info
->hdev
->stat
.byte_tx
+= len
;
185 } while (test_bit(XMIT_WAKEUP
, &(info
->tx_state
)));
187 clear_bit(XMIT_SENDING
, &(info
->tx_state
));
191 static void dtl1_control(dtl1_info_t
*info
, struct sk_buff
*skb
)
193 u8 flowmask
= *(u8
*)skb
->data
;
196 printk(KERN_INFO
"Bluetooth: Nokia control data =");
197 for (i
= 0; i
< skb
->len
; i
++) {
198 printk(" %02x", skb
->data
[i
]);
202 /* transition to active state */
203 if (((info
->flowmask
& 0x07) == 0) && ((flowmask
& 0x07) != 0)) {
204 clear_bit(XMIT_WAITING
, &(info
->tx_state
));
205 dtl1_write_wakeup(info
);
208 info
->flowmask
= flowmask
;
214 static void dtl1_receive(dtl1_info_t
*info
)
221 BT_ERR("Unknown device");
225 iobase
= info
->link
.io
.BasePort1
;
228 info
->hdev
->stat
.byte_rx
++;
230 /* Allocate packet */
231 if (info
->rx_skb
== NULL
)
232 if (!(info
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
))) {
233 BT_ERR("Can't allocate mem for new packet");
234 info
->rx_state
= RECV_WAIT_NSH
;
235 info
->rx_count
= NSHL
;
239 *skb_put(info
->rx_skb
, 1) = inb(iobase
+ UART_RX
);
240 nsh
= (nsh_t
*)info
->rx_skb
->data
;
244 if (info
->rx_count
== 0) {
246 switch (info
->rx_state
) {
248 info
->rx_state
= RECV_WAIT_DATA
;
249 info
->rx_count
= nsh
->len
+ (nsh
->len
& 0x0001);
252 bt_cb(info
->rx_skb
)->pkt_type
= nsh
->type
;
254 /* remove PAD byte if it exists */
255 if (nsh
->len
& 0x0001) {
256 info
->rx_skb
->tail
--;
261 skb_pull(info
->rx_skb
, NSHL
);
263 switch (bt_cb(info
->rx_skb
)->pkt_type
) {
265 /* control data for the Nokia Card */
266 dtl1_control(info
, info
->rx_skb
);
271 /* send frame to the HCI layer */
272 info
->rx_skb
->dev
= (void *) info
->hdev
;
273 bt_cb(info
->rx_skb
)->pkt_type
&= 0x0f;
274 hci_recv_frame(info
->rx_skb
);
278 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info
->rx_skb
)->pkt_type
);
279 kfree_skb(info
->rx_skb
);
283 info
->rx_state
= RECV_WAIT_NSH
;
284 info
->rx_count
= NSHL
;
291 /* Make sure we don't stay here too long */
292 if (boguscount
++ > 32)
295 } while (inb(iobase
+ UART_LSR
) & UART_LSR_DR
);
299 static irqreturn_t
dtl1_interrupt(int irq
, void *dev_inst
, struct pt_regs
*regs
)
301 dtl1_info_t
*info
= dev_inst
;
307 if (!info
|| !info
->hdev
) {
308 BT_ERR("Call of irq %d for unknown device", irq
);
312 iobase
= info
->link
.io
.BasePort1
;
314 spin_lock(&(info
->lock
));
316 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
319 /* Clear interrupt */
320 lsr
= inb(iobase
+ UART_LSR
);
327 /* Receive interrupt */
331 if (lsr
& UART_LSR_THRE
) {
332 /* Transmitter ready for data */
333 dtl1_write_wakeup(info
);
337 BT_ERR("Unhandled IIR=%#x", iir
);
341 /* Make sure we don't stay here too long */
342 if (boguscount
++ > 100)
345 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
349 msr
= inb(iobase
+ UART_MSR
);
351 if (info
->ri_latch
^ (msr
& UART_MSR_RI
)) {
352 info
->ri_latch
= msr
& UART_MSR_RI
;
353 clear_bit(XMIT_WAITING
, &(info
->tx_state
));
354 dtl1_write_wakeup(info
);
357 spin_unlock(&(info
->lock
));
364 /* ======================== HCI interface ======================== */
367 static int dtl1_hci_open(struct hci_dev
*hdev
)
369 set_bit(HCI_RUNNING
, &(hdev
->flags
));
375 static int dtl1_hci_flush(struct hci_dev
*hdev
)
377 dtl1_info_t
*info
= (dtl1_info_t
*)(hdev
->driver_data
);
380 skb_queue_purge(&(info
->txq
));
386 static int dtl1_hci_close(struct hci_dev
*hdev
)
388 if (!test_and_clear_bit(HCI_RUNNING
, &(hdev
->flags
)))
391 dtl1_hci_flush(hdev
);
397 static int dtl1_hci_send_frame(struct sk_buff
*skb
)
400 struct hci_dev
*hdev
= (struct hci_dev
*)(skb
->dev
);
405 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
409 info
= (dtl1_info_t
*)(hdev
->driver_data
);
411 switch (bt_cb(skb
)->pkt_type
) {
412 case HCI_COMMAND_PKT
:
416 case HCI_ACLDATA_PKT
:
420 case HCI_SCODATA_PKT
:
429 s
= bt_skb_alloc(NSHL
+ skb
->len
+ 1, GFP_ATOMIC
);
430 skb_reserve(s
, NSHL
);
431 memcpy(skb_put(s
, skb
->len
), skb
->data
, skb
->len
);
432 if (skb
->len
& 0x0001)
433 *skb_put(s
, 1) = 0; /* PAD */
435 /* Prepend skb with Nokia frame header and queue */
436 memcpy(skb_push(s
, NSHL
), &nsh
, NSHL
);
437 skb_queue_tail(&(info
->txq
), s
);
439 dtl1_write_wakeup(info
);
447 static void dtl1_hci_destruct(struct hci_dev
*hdev
)
452 static int dtl1_hci_ioctl(struct hci_dev
*hdev
, unsigned int cmd
, unsigned long arg
)
459 /* ======================== Card services HCI interaction ======================== */
462 static int dtl1_open(dtl1_info_t
*info
)
465 unsigned int iobase
= info
->link
.io
.BasePort1
;
466 struct hci_dev
*hdev
;
468 spin_lock_init(&(info
->lock
));
470 skb_queue_head_init(&(info
->txq
));
472 info
->rx_state
= RECV_WAIT_NSH
;
473 info
->rx_count
= NSHL
;
476 set_bit(XMIT_WAITING
, &(info
->tx_state
));
478 /* Initialize HCI device */
479 hdev
= hci_alloc_dev();
481 BT_ERR("Can't allocate HCI device");
487 hdev
->type
= HCI_PCCARD
;
488 hdev
->driver_data
= info
;
490 hdev
->open
= dtl1_hci_open
;
491 hdev
->close
= dtl1_hci_close
;
492 hdev
->flush
= dtl1_hci_flush
;
493 hdev
->send
= dtl1_hci_send_frame
;
494 hdev
->destruct
= dtl1_hci_destruct
;
495 hdev
->ioctl
= dtl1_hci_ioctl
;
497 hdev
->owner
= THIS_MODULE
;
499 spin_lock_irqsave(&(info
->lock
), flags
);
502 outb(0, iobase
+ UART_MCR
);
504 /* Turn off interrupts */
505 outb(0, iobase
+ UART_IER
);
507 /* Initialize UART */
508 outb(UART_LCR_WLEN8
, iobase
+ UART_LCR
); /* Reset DLAB */
509 outb((UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
), iobase
+ UART_MCR
);
511 info
->ri_latch
= inb(info
->link
.io
.BasePort1
+ UART_MSR
) & UART_MSR_RI
;
513 /* Turn on interrupts */
514 outb(UART_IER_RLSI
| UART_IER_RDI
| UART_IER_THRI
, iobase
+ UART_IER
);
516 spin_unlock_irqrestore(&(info
->lock
), flags
);
518 /* Timeout before it is safe to send the first HCI packet */
521 /* Register HCI device */
522 if (hci_register_dev(hdev
) < 0) {
523 BT_ERR("Can't register HCI device");
533 static int dtl1_close(dtl1_info_t
*info
)
536 unsigned int iobase
= info
->link
.io
.BasePort1
;
537 struct hci_dev
*hdev
= info
->hdev
;
542 dtl1_hci_close(hdev
);
544 spin_lock_irqsave(&(info
->lock
), flags
);
547 outb(0, iobase
+ UART_MCR
);
549 /* Turn off interrupts */
550 outb(0, iobase
+ UART_IER
);
552 spin_unlock_irqrestore(&(info
->lock
), flags
);
554 if (hci_unregister_dev(hdev
) < 0)
555 BT_ERR("Can't unregister HCI device %s", hdev
->name
);
562 static dev_link_t
*dtl1_attach(void)
565 client_reg_t client_reg
;
569 /* Create new info device */
570 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
577 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_8
;
578 link
->io
.NumPorts1
= 8;
579 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
| IRQ_HANDLE_PRESENT
;
580 link
->irq
.IRQInfo1
= IRQ_LEVEL_ID
;
582 link
->irq
.Handler
= dtl1_interrupt
;
583 link
->irq
.Instance
= info
;
585 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
587 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
589 /* Register with Card Services */
591 client_reg
.dev_info
= &dev_info
;
592 client_reg
.Version
= 0x0210;
593 client_reg
.event_callback_args
.client_data
= link
;
595 ret
= pcmcia_register_client(&link
->handle
, &client_reg
);
596 if (ret
!= CS_SUCCESS
) {
597 cs_error(link
->handle
, RegisterClient
, ret
);
598 dtl1_detach(link
->handle
);
606 static void dtl1_detach(struct pcmcia_device
*p_dev
)
608 dev_link_t
*link
= dev_to_instance(p_dev
);
609 dtl1_info_t
*info
= link
->priv
;
611 if (link
->state
& DEV_CONFIG
)
617 static int get_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
621 i
= pcmcia_get_tuple_data(handle
, tuple
);
625 return pcmcia_parse_tuple(handle
, tuple
, parse
);
628 static int first_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
630 if (pcmcia_get_first_tuple(handle
, tuple
) != CS_SUCCESS
)
631 return CS_NO_MORE_ITEMS
;
632 return get_tuple(handle
, tuple
, parse
);
635 static int next_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
637 if (pcmcia_get_next_tuple(handle
, tuple
) != CS_SUCCESS
)
638 return CS_NO_MORE_ITEMS
;
639 return get_tuple(handle
, tuple
, parse
);
642 static void dtl1_config(dev_link_t
*link
)
644 client_handle_t handle
= link
->handle
;
645 dtl1_info_t
*info
= link
->priv
;
649 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
650 config_info_t config
;
651 int i
, last_ret
, last_fn
;
653 tuple
.TupleData
= (cisdata_t
*)buf
;
654 tuple
.TupleOffset
= 0;
655 tuple
.TupleDataMax
= 255;
656 tuple
.Attributes
= 0;
658 /* Get configuration register information */
659 tuple
.DesiredTuple
= CISTPL_CONFIG
;
660 last_ret
= first_tuple(handle
, &tuple
, &parse
);
661 if (last_ret
!= CS_SUCCESS
) {
662 last_fn
= ParseTuple
;
665 link
->conf
.ConfigBase
= parse
.config
.base
;
666 link
->conf
.Present
= parse
.config
.rmask
[0];
669 link
->state
|= DEV_CONFIG
;
670 i
= pcmcia_get_configuration_info(handle
, &config
);
671 link
->conf
.Vcc
= config
.Vcc
;
673 tuple
.TupleData
= (cisdata_t
*)buf
;
674 tuple
.TupleOffset
= 0;
675 tuple
.TupleDataMax
= 255;
676 tuple
.Attributes
= 0;
677 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
679 /* Look for a generic full-sized window */
680 link
->io
.NumPorts1
= 8;
681 i
= first_tuple(handle
, &tuple
, &parse
);
682 while (i
!= CS_NO_MORE_ITEMS
) {
683 if ((i
== CS_SUCCESS
) && (cf
->io
.nwin
== 1) && (cf
->io
.win
[0].len
> 8)) {
684 link
->conf
.ConfigIndex
= cf
->index
;
685 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
686 link
->io
.NumPorts1
= cf
->io
.win
[0].len
; /*yo */
687 link
->io
.IOAddrLines
= cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
688 i
= pcmcia_request_io(link
->handle
, &link
->io
);
692 i
= next_tuple(handle
, &tuple
, &parse
);
695 if (i
!= CS_SUCCESS
) {
696 cs_error(link
->handle
, RequestIO
, i
);
700 i
= pcmcia_request_irq(link
->handle
, &link
->irq
);
701 if (i
!= CS_SUCCESS
) {
702 cs_error(link
->handle
, RequestIRQ
, i
);
703 link
->irq
.AssignedIRQ
= 0;
706 i
= pcmcia_request_configuration(link
->handle
, &link
->conf
);
707 if (i
!= CS_SUCCESS
) {
708 cs_error(link
->handle
, RequestConfiguration
, i
);
712 if (dtl1_open(info
) != 0)
715 strcpy(info
->node
.dev_name
, info
->hdev
->name
);
716 link
->dev
= &info
->node
;
717 link
->state
&= ~DEV_CONFIG_PENDING
;
722 cs_error(link
->handle
, last_fn
, last_ret
);
729 static void dtl1_release(dev_link_t
*link
)
731 dtl1_info_t
*info
= link
->priv
;
733 if (link
->state
& DEV_PRESENT
)
738 pcmcia_release_configuration(link
->handle
);
739 pcmcia_release_io(link
->handle
, &link
->io
);
740 pcmcia_release_irq(link
->handle
, &link
->irq
);
742 link
->state
&= ~DEV_CONFIG
;
745 static int dtl1_suspend(struct pcmcia_device
*dev
)
747 dev_link_t
*link
= dev_to_instance(dev
);
749 link
->state
|= DEV_SUSPEND
;
750 if (link
->state
& DEV_CONFIG
)
751 pcmcia_release_configuration(link
->handle
);
756 static int dtl1_resume(struct pcmcia_device
*dev
)
758 dev_link_t
*link
= dev_to_instance(dev
);
760 link
->state
&= ~DEV_SUSPEND
;
762 pcmcia_request_configuration(link
->handle
, &link
->conf
);
767 static int dtl1_event(event_t event
, int priority
, event_callback_args_t
*args
)
769 dev_link_t
*link
= args
->client_data
;
772 case CS_EVENT_CARD_INSERTION
:
773 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
781 static struct pcmcia_device_id dtl1_ids
[] = {
782 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
783 PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
786 MODULE_DEVICE_TABLE(pcmcia
, dtl1_ids
);
788 static struct pcmcia_driver dtl1_driver
= {
789 .owner
= THIS_MODULE
,
793 .attach
= dtl1_attach
,
795 .remove
= dtl1_detach
,
796 .id_table
= dtl1_ids
,
797 .suspend
= dtl1_suspend
,
798 .resume
= dtl1_resume
,
801 static int __init
init_dtl1_cs(void)
803 return pcmcia_register_driver(&dtl1_driver
);
807 static void __exit
exit_dtl1_cs(void)
809 pcmcia_unregister_driver(&dtl1_driver
);
812 module_init(init_dtl1_cs
);
813 module_exit(exit_dtl1_cs
);