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/version.h>
47 #include <pcmcia/cs_types.h>
48 #include <pcmcia/cs.h>
49 #include <pcmcia/cistpl.h>
50 #include <pcmcia/ciscode.h>
51 #include <pcmcia/ds.h>
52 #include <pcmcia/cisreg.h>
54 #include <net/bluetooth/bluetooth.h>
55 #include <net/bluetooth/hci_core.h>
59 /* ======================== Module parameters ======================== */
62 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
63 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
64 MODULE_LICENSE("GPL");
68 /* ======================== Local structures ======================== */
71 typedef struct dtl1_info_t
{
77 spinlock_t lock
; /* For serializing operations */
79 unsigned long flowmask
; /* HCI flow mask */
82 struct sk_buff_head txq
;
83 unsigned long tx_state
;
85 unsigned long rx_state
;
86 unsigned long rx_count
;
87 struct sk_buff
*rx_skb
;
91 static void dtl1_config(dev_link_t
*link
);
92 static void dtl1_release(dev_link_t
*link
);
93 static int dtl1_event(event_t event
, int priority
, event_callback_args_t
*args
);
95 static dev_info_t dev_info
= "dtl1_cs";
97 static dev_link_t
*dtl1_attach(void);
98 static void dtl1_detach(dev_link_t
*);
100 static dev_link_t
*dev_list
= NULL
;
103 /* Transmit states */
104 #define XMIT_SENDING 1
105 #define XMIT_WAKEUP 2
106 #define XMIT_WAITING 8
108 /* Receiver States */
109 #define RECV_WAIT_NSH 0
110 #define RECV_WAIT_DATA 1
117 } __attribute__ ((packed
)) nsh_t
; /* Nokia Specific Header */
119 #define NSHL 4 /* Nokia Specific Header Length */
123 /* ======================== Interrupt handling ======================== */
126 static int dtl1_write(unsigned int iobase
, int fifo_size
, __u8
*buf
, int len
)
130 /* Tx FIFO should be empty */
131 if (!(inb(iobase
+ UART_LSR
) & UART_LSR_THRE
))
134 /* Fill FIFO with current frame */
135 while ((fifo_size
-- > 0) && (actual
< len
)) {
136 /* Transmit next byte */
137 outb(buf
[actual
], iobase
+ UART_TX
);
145 static void dtl1_write_wakeup(dtl1_info_t
*info
)
148 BT_ERR("Unknown device");
152 if (test_bit(XMIT_WAITING
, &(info
->tx_state
))) {
153 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
157 if (test_and_set_bit(XMIT_SENDING
, &(info
->tx_state
))) {
158 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
163 register unsigned int iobase
= info
->link
.io
.BasePort1
;
164 register struct sk_buff
*skb
;
167 clear_bit(XMIT_WAKEUP
, &(info
->tx_state
));
169 if (!(info
->link
.state
& DEV_PRESENT
))
172 if (!(skb
= skb_dequeue(&(info
->txq
))))
176 len
= dtl1_write(iobase
, 32, skb
->data
, skb
->len
);
178 if (len
== skb
->len
) {
179 set_bit(XMIT_WAITING
, &(info
->tx_state
));
183 skb_queue_head(&(info
->txq
), skb
);
186 info
->hdev
->stat
.byte_tx
+= len
;
188 } while (test_bit(XMIT_WAKEUP
, &(info
->tx_state
)));
190 clear_bit(XMIT_SENDING
, &(info
->tx_state
));
194 static void dtl1_control(dtl1_info_t
*info
, struct sk_buff
*skb
)
196 u8 flowmask
= *(u8
*)skb
->data
;
199 printk(KERN_INFO
"Bluetooth: Nokia control data =");
200 for (i
= 0; i
< skb
->len
; i
++) {
201 printk(" %02x", skb
->data
[i
]);
205 /* transition to active state */
206 if (((info
->flowmask
& 0x07) == 0) && ((flowmask
& 0x07) != 0)) {
207 clear_bit(XMIT_WAITING
, &(info
->tx_state
));
208 dtl1_write_wakeup(info
);
211 info
->flowmask
= flowmask
;
217 static void dtl1_receive(dtl1_info_t
*info
)
224 BT_ERR("Unknown device");
228 iobase
= info
->link
.io
.BasePort1
;
231 info
->hdev
->stat
.byte_rx
++;
233 /* Allocate packet */
234 if (info
->rx_skb
== NULL
)
235 if (!(info
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
))) {
236 BT_ERR("Can't allocate mem for new packet");
237 info
->rx_state
= RECV_WAIT_NSH
;
238 info
->rx_count
= NSHL
;
242 *skb_put(info
->rx_skb
, 1) = inb(iobase
+ UART_RX
);
243 nsh
= (nsh_t
*)info
->rx_skb
->data
;
247 if (info
->rx_count
== 0) {
249 switch (info
->rx_state
) {
251 info
->rx_state
= RECV_WAIT_DATA
;
252 info
->rx_count
= nsh
->len
+ (nsh
->len
& 0x0001);
255 info
->rx_skb
->pkt_type
= nsh
->type
;
257 /* remove PAD byte if it exists */
258 if (nsh
->len
& 0x0001) {
259 info
->rx_skb
->tail
--;
264 skb_pull(info
->rx_skb
, NSHL
);
266 switch (info
->rx_skb
->pkt_type
) {
268 /* control data for the Nokia Card */
269 dtl1_control(info
, info
->rx_skb
);
274 /* send frame to the HCI layer */
275 info
->rx_skb
->dev
= (void *) info
->hdev
;
276 info
->rx_skb
->pkt_type
&= 0x0f;
277 hci_recv_frame(info
->rx_skb
);
281 BT_ERR("Unknown HCI packet with type 0x%02x received", info
->rx_skb
->pkt_type
);
282 kfree_skb(info
->rx_skb
);
286 info
->rx_state
= RECV_WAIT_NSH
;
287 info
->rx_count
= NSHL
;
294 /* Make sure we don't stay here too long */
295 if (boguscount
++ > 32)
298 } while (inb(iobase
+ UART_LSR
) & UART_LSR_DR
);
302 static irqreturn_t
dtl1_interrupt(int irq
, void *dev_inst
, struct pt_regs
*regs
)
304 dtl1_info_t
*info
= dev_inst
;
310 if (!info
|| !info
->hdev
) {
311 BT_ERR("Call of irq %d for unknown device", irq
);
315 iobase
= info
->link
.io
.BasePort1
;
317 spin_lock(&(info
->lock
));
319 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
322 /* Clear interrupt */
323 lsr
= inb(iobase
+ UART_LSR
);
330 /* Receive interrupt */
334 if (lsr
& UART_LSR_THRE
) {
335 /* Transmitter ready for data */
336 dtl1_write_wakeup(info
);
340 BT_ERR("Unhandled IIR=%#x", iir
);
344 /* Make sure we don't stay here too long */
345 if (boguscount
++ > 100)
348 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
352 msr
= inb(iobase
+ UART_MSR
);
354 if (info
->ri_latch
^ (msr
& UART_MSR_RI
)) {
355 info
->ri_latch
= msr
& UART_MSR_RI
;
356 clear_bit(XMIT_WAITING
, &(info
->tx_state
));
357 dtl1_write_wakeup(info
);
360 spin_unlock(&(info
->lock
));
367 /* ======================== HCI interface ======================== */
370 static int dtl1_hci_open(struct hci_dev
*hdev
)
372 set_bit(HCI_RUNNING
, &(hdev
->flags
));
378 static int dtl1_hci_flush(struct hci_dev
*hdev
)
380 dtl1_info_t
*info
= (dtl1_info_t
*)(hdev
->driver_data
);
383 skb_queue_purge(&(info
->txq
));
389 static int dtl1_hci_close(struct hci_dev
*hdev
)
391 if (!test_and_clear_bit(HCI_RUNNING
, &(hdev
->flags
)))
394 dtl1_hci_flush(hdev
);
400 static int dtl1_hci_send_frame(struct sk_buff
*skb
)
403 struct hci_dev
*hdev
= (struct hci_dev
*)(skb
->dev
);
408 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
412 info
= (dtl1_info_t
*)(hdev
->driver_data
);
414 switch (skb
->pkt_type
) {
415 case HCI_COMMAND_PKT
:
419 case HCI_ACLDATA_PKT
:
423 case HCI_SCODATA_PKT
:
432 s
= bt_skb_alloc(NSHL
+ skb
->len
+ 1, GFP_ATOMIC
);
433 skb_reserve(s
, NSHL
);
434 memcpy(skb_put(s
, skb
->len
), skb
->data
, skb
->len
);
435 if (skb
->len
& 0x0001)
436 *skb_put(s
, 1) = 0; /* PAD */
438 /* Prepend skb with Nokia frame header and queue */
439 memcpy(skb_push(s
, NSHL
), &nsh
, NSHL
);
440 skb_queue_tail(&(info
->txq
), s
);
442 dtl1_write_wakeup(info
);
450 static void dtl1_hci_destruct(struct hci_dev
*hdev
)
455 static int dtl1_hci_ioctl(struct hci_dev
*hdev
, unsigned int cmd
, unsigned long arg
)
462 /* ======================== Card services HCI interaction ======================== */
465 static int dtl1_open(dtl1_info_t
*info
)
468 unsigned int iobase
= info
->link
.io
.BasePort1
;
469 struct hci_dev
*hdev
;
471 spin_lock_init(&(info
->lock
));
473 skb_queue_head_init(&(info
->txq
));
475 info
->rx_state
= RECV_WAIT_NSH
;
476 info
->rx_count
= NSHL
;
479 set_bit(XMIT_WAITING
, &(info
->tx_state
));
481 /* Initialize HCI device */
482 hdev
= hci_alloc_dev();
484 BT_ERR("Can't allocate HCI device");
490 hdev
->type
= HCI_PCCARD
;
491 hdev
->driver_data
= info
;
493 hdev
->open
= dtl1_hci_open
;
494 hdev
->close
= dtl1_hci_close
;
495 hdev
->flush
= dtl1_hci_flush
;
496 hdev
->send
= dtl1_hci_send_frame
;
497 hdev
->destruct
= dtl1_hci_destruct
;
498 hdev
->ioctl
= dtl1_hci_ioctl
;
500 hdev
->owner
= THIS_MODULE
;
502 spin_lock_irqsave(&(info
->lock
), flags
);
505 outb(0, iobase
+ UART_MCR
);
507 /* Turn off interrupts */
508 outb(0, iobase
+ UART_IER
);
510 /* Initialize UART */
511 outb(UART_LCR_WLEN8
, iobase
+ UART_LCR
); /* Reset DLAB */
512 outb((UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
), iobase
+ UART_MCR
);
514 info
->ri_latch
= inb(info
->link
.io
.BasePort1
+ UART_MSR
) & UART_MSR_RI
;
516 /* Turn on interrupts */
517 outb(UART_IER_RLSI
| UART_IER_RDI
| UART_IER_THRI
, iobase
+ UART_IER
);
519 spin_unlock_irqrestore(&(info
->lock
), flags
);
521 /* Timeout before it is safe to send the first HCI packet */
524 /* Register HCI device */
525 if (hci_register_dev(hdev
) < 0) {
526 BT_ERR("Can't register HCI device");
536 static int dtl1_close(dtl1_info_t
*info
)
539 unsigned int iobase
= info
->link
.io
.BasePort1
;
540 struct hci_dev
*hdev
= info
->hdev
;
545 dtl1_hci_close(hdev
);
547 spin_lock_irqsave(&(info
->lock
), flags
);
550 outb(0, iobase
+ UART_MCR
);
552 /* Turn off interrupts */
553 outb(0, iobase
+ UART_IER
);
555 spin_unlock_irqrestore(&(info
->lock
), flags
);
557 if (hci_unregister_dev(hdev
) < 0)
558 BT_ERR("Can't unregister HCI device %s", hdev
->name
);
565 static dev_link_t
*dtl1_attach(void)
568 client_reg_t client_reg
;
572 /* Create new info device */
573 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
576 memset(info
, 0, sizeof(*info
));
581 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_8
;
582 link
->io
.NumPorts1
= 8;
583 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
| IRQ_HANDLE_PRESENT
;
584 link
->irq
.IRQInfo1
= IRQ_LEVEL_ID
;
586 link
->irq
.Handler
= dtl1_interrupt
;
587 link
->irq
.Instance
= info
;
589 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
591 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
593 /* Register with Card Services */
594 link
->next
= dev_list
;
596 client_reg
.dev_info
= &dev_info
;
597 client_reg
.EventMask
=
598 CS_EVENT_CARD_INSERTION
| CS_EVENT_CARD_REMOVAL
|
599 CS_EVENT_RESET_PHYSICAL
| CS_EVENT_CARD_RESET
|
600 CS_EVENT_PM_SUSPEND
| CS_EVENT_PM_RESUME
;
601 client_reg
.event_handler
= &dtl1_event
;
602 client_reg
.Version
= 0x0210;
603 client_reg
.event_callback_args
.client_data
= link
;
605 ret
= pcmcia_register_client(&link
->handle
, &client_reg
);
606 if (ret
!= CS_SUCCESS
) {
607 cs_error(link
->handle
, RegisterClient
, ret
);
616 static void dtl1_detach(dev_link_t
*link
)
618 dtl1_info_t
*info
= link
->priv
;
622 /* Locate device structure */
623 for (linkp
= &dev_list
; *linkp
; linkp
= &(*linkp
)->next
)
630 if (link
->state
& DEV_CONFIG
)
634 ret
= pcmcia_deregister_client(link
->handle
);
635 if (ret
!= CS_SUCCESS
)
636 cs_error(link
->handle
, DeregisterClient
, ret
);
639 /* Unlink device structure, free bits */
645 static int get_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
649 i
= pcmcia_get_tuple_data(handle
, tuple
);
653 return pcmcia_parse_tuple(handle
, tuple
, parse
);
656 static int first_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
658 if (pcmcia_get_first_tuple(handle
, tuple
) != CS_SUCCESS
)
659 return CS_NO_MORE_ITEMS
;
660 return get_tuple(handle
, tuple
, parse
);
663 static int next_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
665 if (pcmcia_get_next_tuple(handle
, tuple
) != CS_SUCCESS
)
666 return CS_NO_MORE_ITEMS
;
667 return get_tuple(handle
, tuple
, parse
);
670 static void dtl1_config(dev_link_t
*link
)
672 client_handle_t handle
= link
->handle
;
673 dtl1_info_t
*info
= link
->priv
;
677 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
678 config_info_t config
;
679 int i
, last_ret
, last_fn
;
681 tuple
.TupleData
= (cisdata_t
*)buf
;
682 tuple
.TupleOffset
= 0;
683 tuple
.TupleDataMax
= 255;
684 tuple
.Attributes
= 0;
686 /* Get configuration register information */
687 tuple
.DesiredTuple
= CISTPL_CONFIG
;
688 last_ret
= first_tuple(handle
, &tuple
, &parse
);
689 if (last_ret
!= CS_SUCCESS
) {
690 last_fn
= ParseTuple
;
693 link
->conf
.ConfigBase
= parse
.config
.base
;
694 link
->conf
.Present
= parse
.config
.rmask
[0];
697 link
->state
|= DEV_CONFIG
;
698 i
= pcmcia_get_configuration_info(handle
, &config
);
699 link
->conf
.Vcc
= config
.Vcc
;
701 tuple
.TupleData
= (cisdata_t
*)buf
;
702 tuple
.TupleOffset
= 0;
703 tuple
.TupleDataMax
= 255;
704 tuple
.Attributes
= 0;
705 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
707 /* Look for a generic full-sized window */
708 link
->io
.NumPorts1
= 8;
709 i
= first_tuple(handle
, &tuple
, &parse
);
710 while (i
!= CS_NO_MORE_ITEMS
) {
711 if ((i
== CS_SUCCESS
) && (cf
->io
.nwin
== 1) && (cf
->io
.win
[0].len
> 8)) {
712 link
->conf
.ConfigIndex
= cf
->index
;
713 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
714 link
->io
.NumPorts1
= cf
->io
.win
[0].len
; /*yo */
715 link
->io
.IOAddrLines
= cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
716 i
= pcmcia_request_io(link
->handle
, &link
->io
);
720 i
= next_tuple(handle
, &tuple
, &parse
);
723 if (i
!= CS_SUCCESS
) {
724 cs_error(link
->handle
, RequestIO
, i
);
728 i
= pcmcia_request_irq(link
->handle
, &link
->irq
);
729 if (i
!= CS_SUCCESS
) {
730 cs_error(link
->handle
, RequestIRQ
, i
);
731 link
->irq
.AssignedIRQ
= 0;
734 i
= pcmcia_request_configuration(link
->handle
, &link
->conf
);
735 if (i
!= CS_SUCCESS
) {
736 cs_error(link
->handle
, RequestConfiguration
, i
);
740 if (dtl1_open(info
) != 0)
743 strcpy(info
->node
.dev_name
, info
->hdev
->name
);
744 link
->dev
= &info
->node
;
745 link
->state
&= ~DEV_CONFIG_PENDING
;
750 cs_error(link
->handle
, last_fn
, last_ret
);
757 static void dtl1_release(dev_link_t
*link
)
759 dtl1_info_t
*info
= link
->priv
;
761 if (link
->state
& DEV_PRESENT
)
766 pcmcia_release_configuration(link
->handle
);
767 pcmcia_release_io(link
->handle
, &link
->io
);
768 pcmcia_release_irq(link
->handle
, &link
->irq
);
770 link
->state
&= ~DEV_CONFIG
;
774 static int dtl1_event(event_t event
, int priority
, event_callback_args_t
*args
)
776 dev_link_t
*link
= args
->client_data
;
777 dtl1_info_t
*info
= link
->priv
;
780 case CS_EVENT_CARD_REMOVAL
:
781 link
->state
&= ~DEV_PRESENT
;
782 if (link
->state
& DEV_CONFIG
) {
787 case CS_EVENT_CARD_INSERTION
:
788 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
791 case CS_EVENT_PM_SUSPEND
:
792 link
->state
|= DEV_SUSPEND
;
793 /* Fall through... */
794 case CS_EVENT_RESET_PHYSICAL
:
795 if (link
->state
& DEV_CONFIG
)
796 pcmcia_release_configuration(link
->handle
);
798 case CS_EVENT_PM_RESUME
:
799 link
->state
&= ~DEV_SUSPEND
;
800 /* Fall through... */
801 case CS_EVENT_CARD_RESET
:
803 pcmcia_request_configuration(link
->handle
, &link
->conf
);
810 static struct pcmcia_driver dtl1_driver
= {
811 .owner
= THIS_MODULE
,
815 .attach
= dtl1_attach
,
816 .detach
= dtl1_detach
,
819 static int __init
init_dtl1_cs(void)
821 return pcmcia_register_driver(&dtl1_driver
);
825 static void __exit
exit_dtl1_cs(void)
827 pcmcia_unregister_driver(&dtl1_driver
);
828 BUG_ON(dev_list
!= NULL
);
831 module_init(init_dtl1_cs
);
832 module_exit(exit_dtl1_cs
);