]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/linux/openzaurus-pxa-2.4.18-rmk7-pxa3-embedix20031107/bt950_cs.patch
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / linux / openzaurus-pxa-2.4.18-rmk7-pxa3-embedix20031107 / bt950_cs.patch
1 diff -Nur linux-orig/drivers/bluetooth/bt950_cs.c linux/drivers/bluetooth/bt950_cs.c
2 --- linux-orig/drivers/bluetooth/bt950_cs.c     1970-01-01 03:00:00.000000000 +0300
3 +++ linux/drivers/bluetooth/bt950_cs.c  2004-02-04 09:55:04.000000000 +0300
4 @@ -0,0 +1,1133 @@
5 +/*
6 + *
7 + *  Driver for Bluetooth cards with OXCF950 UART interface
8 + *
9 + *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
10 + *                           Albert Rybalkin <albertr@iral.com>
11 + *
12 + *
13 + *  This program is free software; you can redistribute it and/or modify
14 + *  it under the terms of the GNU General Public License version 2 as
15 + *  published by the Free Software Foundation;
16 + *
17 + *  Software distributed under the License is distributed on an "AS
18 + *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
19 + *  implied. See the License for the specific language governing
20 + *  rights and limitations under the License.
21 + *
22 + *  The initial developer of the original code is David A. Hinds
23 + *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
24 + *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
25 + *
26 + */
27 +
28 +#include <linux/config.h>
29 +
30 +#ifdef CONFIG_MODVERSIONS
31 +#ifndef MODVERSIONS
32 +#define MODVERSIONS
33 +#endif
34 +#include <linux/modversions.h>
35 +#endif
36 +
37 +#include <linux/module.h>
38 +#include <linux/kernel.h>
39 +#include <linux/init.h>
40 +#include <linux/slab.h>
41 +#include <linux/types.h>
42 +#include <linux/sched.h>
43 +#include <linux/timer.h>
44 +#include <linux/errno.h>
45 +#include <linux/ptrace.h>
46 +#include <linux/ioport.h>
47 +#include <linux/spinlock.h>
48 +#include <linux/delay.h>
49 +
50 +#include <linux/skbuff.h>
51 +#include <linux/string.h>
52 +#include <linux/serial.h>
53 +#include <linux/serial_reg.h>
54 +#include <asm/system.h>
55 +#include <asm/bitops.h>
56 +#include <asm/io.h>
57 +
58 +#include <pcmcia/version.h>
59 +#include <pcmcia/cs_types.h>
60 +#include <pcmcia/cs.h>
61 +#include <pcmcia/cistpl.h>
62 +#include <pcmcia/ciscode.h>
63 +#include <pcmcia/ds.h>
64 +#include <pcmcia/cisreg.h>
65 +
66 +#include <net/bluetooth/bluetooth.h>
67 +#include <net/bluetooth/hci_core.h>
68 +
69 +/* Default baud rate: 57600, 115200, 230400 or 460800 */
70 +#define DEFAULT_BAUD_RATE 460800
71 +
72 +
73 +/* ======================== Module parameters ======================== */
74 +
75 +
76 +/* Bit map of interrupts to choose from */
77 +static u_int irq_mask = 0x86bc;
78 +static int irq_list[4] = { -1 };
79 +static long baud_rate = DEFAULT_BAUD_RATE;
80 +
81 +MODULE_PARM(irq_mask, "i");
82 +MODULE_PARM(irq_list, "1-4i");
83 +MODULE_PARM(baud_rate, "l");
84 +
85 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Albert Rybalkin <albertr@iral.com>");
86 +MODULE_DESCRIPTION("BlueZ driver for Bluetooth cards with OXCF950 UART interface");
87 +MODULE_LICENSE("GPL");
88 +
89 +
90 +
91 +/* ======================== Local structures ======================== */
92 +
93 +
94 +typedef struct bt950_info_t {
95 +       dev_link_t link;
96 +       dev_node_t node;
97 +
98 +       struct hci_dev hdev;
99 +
100 +       spinlock_t lock;        /* For serializing operations */
101 +
102 +       struct sk_buff_head txq;
103 +       unsigned long tx_state;
104 +
105 +       unsigned long rx_state;
106 +       unsigned long rx_count;
107 +       struct sk_buff *rx_skb;
108 +} bt950_info_t;
109 +
110 +
111 +static void bt950_config(dev_link_t *link);
112 +static void bt950_release(u_long arg);
113 +static int bt950_event(event_t event, int priority, event_callback_args_t *args);
114 +
115 +static dev_info_t dev_info = "bt950_cs";
116 +
117 +static dev_link_t *bt950_attach(void);
118 +static void bt950_detach(dev_link_t *);
119 +
120 +static dev_link_t *dev_list = NULL;
121 +
122 +/* Transmit states  */
123 +#define XMIT_SENDING   1
124 +#define XMIT_WAKEUP    2
125 +#define XMIT_WAITING   8
126 +
127 +/* Receiver states */
128 +#define RECV_WAIT_PACKET_TYPE  0
129 +#define RECV_WAIT_EVENT_HEADER 1
130 +#define RECV_WAIT_ACL_HEADER   2
131 +#define RECV_WAIT_SCO_HEADER   3
132 +#define RECV_WAIT_DATA         4
133 +
134 +/* Special packet types */
135 +#define PKT_BAUD_RATE_57600   0x80
136 +#define PKT_BAUD_RATE_115200  0x81
137 +#define PKT_BAUD_RATE_230400  0x82
138 +#define PKT_BAUD_RATE_460800  0x83
139 +
140 +/* 950-specific stuff */
141 +#define MAX_WAIT       0xFFFF
142 +#define FIFO_SIZE      128
143 +
144 +#define TR_TX_INT      0x10    /* TTL: TX interrupt trigger level (0-127) */
145 +#define TR_RX_INT      0x40    /* RTL: RX interrupt trigger level (1-127) */
146 +#define TR_CTL_LO      0x08    /* FCL: auto flow control LOWER trigger level (0-127) */
147 +#define TR_CTL_HI      0x60    /* FCH: auto flow control HIGH trigger level (1-127) */
148 +
149 +/* 950-specific registers and values we use. It should
150 + * eventually go to include/linux/serial_reg.h */
151 +#define UART_IER_CTS   0x80    /* enable CTS interrupt */
152 +#define UART_IER_RTS   0x40    /* enable RTS interrupt */
153 +#define UART_IER_SLP   0x10    /* enable sleep mode */
154 +#define UART_LCR_650   0xBF    /* enable 650-compatible registers access */
155 +#define UART_LSR_DE    0x80    /* data error */
156 +#define UART_LSR_ERR   (UART_LSR_OE|UART_LSR_PE|UART_LSR_FE|UART_LSR_BI|UART_LSR_DE)
157 +#define UART_IIR_RXTOUT        0x0C    /* RX timeout interrupt */
158 +#define UART_IIR_CTSRTS        0x20    /* CTS or RTS change interrupt */
159 +#define UART_IIR_RTS   0x40
160 +#define UART_IIR_CTS   0x80
161 +#define UART_IIR_MASK  0x3E    /* interrupt mask */
162 +#define UART_SRT       0x0D    /* soft reset register */
163 +
164 +
165 +
166 +/* ======================== Interrupt handling ======================== */
167 +
168 +
169 +static int bt950_write(unsigned int iobase, int fifo_size, const unsigned char *buf, int len)
170 +{
171 +       int i, actual = 0;
172 +
173 +       /* Activate DTR and RTS */
174 +       outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2, iobase + UART_MCR);
175 +
176 +       /* Wait for CTS flow control */
177 +       for (i = MAX_WAIT; i; i--)
178 +               if (inb(iobase + UART_MSR) & UART_MSR_CTS)
179 +                       break;
180 +
181 +       if (!i) {
182 +               printk(KERN_WARNING "bt950_cs: Timeout waiting for CTS on write.\n");
183 +               return 0;
184 +       }
185 +
186 +       /* The TX FIFO should be empty */
187 +       for (i = MAX_WAIT; i; i--)
188 +               if (inb(iobase + UART_LSR) & UART_LSR_THRE)
189 +                       break;
190 +
191 +       if (!i) {
192 +               printk(KERN_WARNING "bt950_cs: Timeout waiting for empty TX FIFO on write.\n");
193 +               return 0;
194 +       }
195 +
196 +       /* Fill FIFO with current frame */
197 +       while ((fifo_size-- > 0) && (actual < len)) {
198 +               /* Transmit next byte */
199 +               outb(buf[actual], iobase + UART_TX);
200 +               actual++;
201 +       }
202 +
203 +       return actual;
204 +}
205 +
206 +
207 +static void bt950_write_wakeup(bt950_info_t *info)
208 +{
209 +       unsigned char lcr;
210 +       unsigned int divisor;
211 +
212 +       if (!info) {
213 +               printk(KERN_WARNING "bt950_cs: Call of write_wakeup for unknown device.\n");
214 +               return;
215 +       }
216 +
217 +       if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
218 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
219 +               return;
220 +       }
221 +
222 +       do {
223 +               register unsigned int iobase = info->link.io.BasePort1;
224 +               register struct sk_buff *skb;
225 +               register int len;
226 +
227 +               clear_bit(XMIT_WAKEUP, &(info->tx_state));
228 +
229 +               if (!(info->link.state & DEV_PRESENT))
230 +                       return;
231 +
232 +               if (!(skb = skb_dequeue(&(info->txq))))
233 +                       break;
234 +
235 +               if (skb->pkt_type & 0x80) {
236 +                       /* Disable RTS */
237 +                       outb((inb(iobase + UART_MCR) & ~UART_MCR_RTS), iobase + UART_MCR);
238 +               }
239 +
240 +               /* Send frame */
241 +               len = bt950_write(iobase, FIFO_SIZE, skb->data, skb->len);
242 +
243 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
244 +
245 +               if (skb->pkt_type & 0x80) {
246 +
247 +                       wait_queue_head_t wait;
248 +
249 +                       switch (skb->pkt_type) {
250 +
251 +                       case PKT_BAUD_RATE_460800:
252 +                               divisor = 1;
253 +                               break;
254 +
255 +                       case PKT_BAUD_RATE_230400:
256 +                               divisor = 2;
257 +                               break;
258 +
259 +                       case PKT_BAUD_RATE_115200:
260 +                               divisor = 4;
261 +                               break;
262 +
263 +                       case PKT_BAUD_RATE_57600:
264 +                               /* Fall through... */
265 +
266 +                       default:
267 +                               divisor = 8;
268 +                               break;
269 +                       }
270 +
271 +                       /* Wait until the command reaches the baseband */
272 +                       init_waitqueue_head(&wait);
273 +                       interruptible_sleep_on_timeout(&wait, HZ / 10);
274 +
275 +                       /* Set baud on baseband */
276 +                       /* Enable divisor latch access */
277 +                       lcr = inb(iobase + UART_LCR) & 0x3F;
278 +                       outb(lcr | UART_LCR_DLAB, iobase + UART_LCR);
279 +
280 +                       /* Setup divisor latch */
281 +                       outb(divisor & 0x00FF, iobase + UART_DLL);              /* divisor latch LOW byte */
282 +                       outb((divisor & 0xFF00) >> 8, iobase + UART_DLM);       /* divisor latch HI byte */
283 +
284 +                       /* Disable divisor latch access */
285 +                       outb(lcr, iobase + UART_LCR);
286 +
287 +                       /* Enable RTS */
288 +                       outb((inb(iobase + UART_MCR) | UART_MCR_RTS), iobase + UART_MCR);
289 +
290 +                       /* Wait before the next HCI packet can be send */
291 +                       interruptible_sleep_on_timeout(&wait, HZ);
292 +
293 +               }
294 +
295 +               if (len == skb->len) {
296 +                       kfree_skb(skb);
297 +               } else {
298 +                       skb_pull(skb, len);
299 +                       skb_queue_head(&(info->txq), skb);
300 +               }
301 +
302 +               info->hdev.stat.byte_tx += len;
303 +
304 +       } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
305 +
306 +       clear_bit(XMIT_SENDING, &(info->tx_state));
307 +}
308 +
309 +
310 +static inline void bt950_receive(bt950_info_t *info)
311 +{
312 +       unsigned int iobase;
313 +       int boguscount = 0;
314 +
315 +       if (!info) {
316 +               printk(KERN_ERR "bt950_cs: Call of receive for unknown device.\n");
317 +               return;
318 +       }
319 +
320 +       iobase = info->link.io.BasePort1;
321 +
322 +       /* Fixme: BUG? */
323 +       inb(iobase + UART_MCR);
324 +
325 +       do {
326 +               info->hdev.stat.byte_rx++;
327 +
328 +               /* Allocate packet */
329 +               if (info->rx_skb == NULL) {
330 +                       info->rx_state = RECV_WAIT_PACKET_TYPE;
331 +                       info->rx_count = 0;
332 +                       if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
333 +                               printk(KERN_ERR "bt950_cs: Can't allocate mem for new packet.\n");
334 +                               return;
335 +                       }
336 +               }
337 +
338 +               if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
339 +
340 +                       info->rx_skb->dev = (void *)&(info->hdev);
341 +                       info->rx_skb->pkt_type = inb(iobase + UART_RX);
342 +
343 +                       switch (info->rx_skb->pkt_type) {
344 +
345 +                       case HCI_EVENT_PKT:
346 +                               info->rx_state = RECV_WAIT_EVENT_HEADER;
347 +                               info->rx_count = HCI_EVENT_HDR_SIZE;
348 +                               break;
349 +
350 +                       case HCI_ACLDATA_PKT:
351 +                               info->rx_state = RECV_WAIT_ACL_HEADER;
352 +                               info->rx_count = HCI_ACL_HDR_SIZE;
353 +                               break;
354 +
355 +                       case HCI_SCODATA_PKT:
356 +                               info->rx_state = RECV_WAIT_SCO_HEADER;
357 +                               info->rx_count = HCI_SCO_HDR_SIZE;
358 +                               break;
359 +
360 +                       default:
361 +                               /* Unknown packet */
362 +                               printk(KERN_WARNING "bt950_cs: Unknown HCI packet with type 0x%02X received.\n", info->rx_skb->pkt_type);
363 +                               info->hdev.stat.err_rx++;
364 +
365 +                               kfree_skb(info->rx_skb);
366 +                               info->rx_skb = NULL;
367 +                               break;
368 +
369 +                       }
370 +
371 +               } else {
372 +
373 +                       *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
374 +                       info->rx_count--;
375 +
376 +                       if (info->rx_count == 0) {
377 +
378 +                               int dlen;
379 +                               hci_event_hdr *eh;
380 +                               hci_acl_hdr *ah;
381 +                               hci_sco_hdr *sh;
382 +
383 +
384 +                               switch (info->rx_state) {
385 +
386 +                               case RECV_WAIT_EVENT_HEADER:
387 +                                       eh = (hci_event_hdr *)(info->rx_skb->data);
388 +                                       info->rx_state = RECV_WAIT_DATA;
389 +                                       info->rx_count = eh->plen;
390 +                                       break;
391 +
392 +                               case RECV_WAIT_ACL_HEADER:
393 +                                       ah = (hci_acl_hdr *)(info->rx_skb->data);
394 +                                       dlen = __le16_to_cpu(ah->dlen);
395 +                                       info->rx_state = RECV_WAIT_DATA;
396 +                                       info->rx_count = dlen;
397 +                                       break;
398 +
399 +                               case RECV_WAIT_SCO_HEADER:
400 +                                       sh = (hci_sco_hdr *)(info->rx_skb->data);
401 +                                       info->rx_state = RECV_WAIT_DATA;
402 +                                       info->rx_count = sh->dlen;
403 +                                       break;
404 +
405 +                               case RECV_WAIT_DATA:
406 +                                       hci_recv_frame(info->rx_skb);
407 +                                       info->rx_skb = NULL;
408 +                                       break;
409 +
410 +                               }
411 +
412 +                       }
413 +
414 +               }
415 +
416 +               /* Make sure we don't stay here too long */
417 +               if (boguscount++ > 16)
418 +                       break;
419 +
420 +       } while (inb(iobase + UART_LSR) & UART_LSR_DR);
421 +}
422 +
423 +
424 +static void bt950_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
425 +{
426 +       bt950_info_t *info = dev_inst;
427 +       unsigned int iobase;
428 +       int boguscount = 0;
429 +       int iir, lsr;
430 +
431 +       if (!info) {
432 +               printk(KERN_ERR "bt950_cs: Call of irq %d for unknown device.\n", irq);
433 +               return;
434 +       }
435 +
436 +       iobase = info->link.io.BasePort1;
437 +
438 +       spin_lock(&(info->lock));
439 +
440 +       iir = inb(iobase + UART_IIR);
441 +
442 +       while (!(iir & UART_IIR_NO_INT)) {
443 +
444 +               switch (iir & UART_IIR_ID) {
445 +               case UART_IIR_RLSI:
446 +                       /* Clear RLSI interrupt */
447 +                       lsr = inb(iobase + UART_LSR);
448 +                       printk(KERN_NOTICE "bt950_cs: RLSI interrupt, LSR=%#x\n", lsr);
449 +                       /* Fixme: we need to process errors ... */
450 +                       break;
451 +               case UART_IIR_RDI:
452 +                       /* Receive interrupt */
453 +                       bt950_receive(info);
454 +                       break;
455 +               case UART_IIR_THRI:
456 +                       /* Transmitter ready for data */
457 +                       bt950_write_wakeup(info);
458 +                       break;
459 +               default:
460 +                       printk(KERN_NOTICE "bt950_cs: Unhandled IIR=%#x\n", iir);
461 +                       break;
462 +               }
463 +
464 +               /* Make sure we don't stay here too long */
465 +               if (boguscount++ > 100)
466 +                       break;
467 +
468 +               iir = inb(iobase + UART_IIR);
469 +
470 +       }
471 +
472 +       spin_unlock(&(info->lock));
473 +}
474 +
475 +/* ======================== Device specific HCI commands ======================== */
476 +
477 +
478 +static int bt950_hci_set_baud_rate(struct hci_dev *hdev, int baud)
479 +{
480 +       bt950_info_t *info = (bt950_info_t *)(hdev->driver_data);
481 +       struct sk_buff *skb;
482 +
483 +       /* Ericsson baud rate command */
484 +       unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
485 +
486 +       if (!(skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
487 +               printk(KERN_WARNING "bt950_cs: Can't allocate mem for new packet.\n");
488 +               return -1;
489 +       }
490 +
491 +       switch (baud) {
492 +       case 460800:
493 +               cmd[4] = 0x00;
494 +               skb->pkt_type = PKT_BAUD_RATE_460800;
495 +               break;
496 +       case 230400:
497 +               cmd[4] = 0x01;
498 +               skb->pkt_type = PKT_BAUD_RATE_230400;
499 +               break;
500 +       case 115200:
501 +               cmd[4] = 0x02;
502 +               skb->pkt_type = PKT_BAUD_RATE_115200;
503 +               break;
504 +       case 57600:
505 +               /* Fall through... */
506 +       default:
507 +           baud = 57600;
508 +               cmd[4] = 0x03;
509 +               skb->pkt_type = PKT_BAUD_RATE_57600;
510 +               break;
511 +       }
512 +
513 +       memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
514 +
515 +       skb_queue_tail(&(info->txq), skb);
516 +
517 +    printk(KERN_WARNING "bt950_cs: setting baud rate: %d.\n", baud);
518 +    
519 +       bt950_write_wakeup(info);
520 +
521 +       return 0;
522 +}
523 +
524 +
525 +/* ======================== HCI interface ======================== */
526 +
527 +
528 +static int bt950_hci_flush(struct hci_dev *hdev)
529 +{
530 +       bt950_info_t *info = (bt950_info_t *)(hdev->driver_data);
531 +
532 +       /* Drop TX queue */
533 +       skb_queue_purge(&(info->txq));
534 +
535 +       return 0;
536 +}
537 +
538 +
539 +static int bt950_hci_open(struct hci_dev *hdev)
540 +{
541 +       bt950_hci_set_baud_rate(hdev, baud_rate);
542 +       set_bit(HCI_RUNNING, &(hdev->flags));
543 +
544 +       return 0;
545 +}
546 +
547 +
548 +static int bt950_hci_close(struct hci_dev *hdev)
549 +{
550 +       if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
551 +               return 0;
552 +
553 +       bt950_hci_flush(hdev);
554 +
555 +       return 0;
556 +}
557 +
558 +
559 +static int bt950_hci_send_frame(struct sk_buff *skb)
560 +{
561 +       bt950_info_t *info;
562 +       struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
563 +
564 +       if (!hdev) {
565 +               printk(KERN_ERR "bt950_cs: Frame for unknown HCI device (hdev=NULL).");
566 +               return -ENODEV;
567 +       }
568 +
569 +       info = (bt950_info_t *)(hdev->driver_data);
570 +
571 +       switch (skb->pkt_type) {
572 +       case HCI_COMMAND_PKT:
573 +               hdev->stat.cmd_tx++;
574 +               break;
575 +       case HCI_ACLDATA_PKT:
576 +               hdev->stat.acl_tx++;
577 +               break;
578 +       case HCI_SCODATA_PKT:
579 +               hdev->stat.sco_tx++;
580 +               break;
581 +       };
582 +
583 +       /* Prepend skb with frame type */
584 +       memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
585 +       skb_queue_tail(&(info->txq), skb);
586 +
587 +       bt950_write_wakeup(info);
588 +
589 +       return 0;
590 +}
591 +
592 +
593 +static void bt950_hci_destruct(struct hci_dev *hdev)
594 +{
595 +}
596 +
597 +
598 +static int bt950_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
599 +{
600 +       return -ENOIOCTLCMD;
601 +}
602 +
603 +
604 +
605 +/* ======================== Card services HCI interaction ======================== */
606 +
607 +
608 +static int bt950_setup_uart(bt950_info_t *info)
609 +{
610 +       unsigned long flags;
611 +       unsigned int iobase = info->link.io.BasePort1;
612 +       unsigned char lcr, ier = UART_IER_RDI | UART_IER_RLSI | UART_IER_SLP;
613 +       unsigned int divisor = 8;       /* Fixme: divisor == 0x0c ??? */
614 +       unsigned char id1, id2, id3, rev;
615 +       register int i;
616 +
617 +       spin_lock_irqsave(&(info->lock), flags);
618 +
619 +       /* Disable interrupts */
620 +       outb(0, iobase + UART_IER);
621 +
622 +       /* Activate RTS and OUT2 */
623 +       /* Fixme: is OUT2 used to enable interrupts? */
624 +       outb(UART_MCR_RTS | UART_MCR_OUT2, iobase + UART_MCR);
625 +
626 +       /* Setup the FIFO's */
627 +       outb(0, iobase + UART_FCR);
628 +       inb(iobase + UART_RX);
629 +       outb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
630 +            UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_14, iobase + UART_FCR);
631 +
632 +       /* Disable divisor latch access */
633 +       lcr = inb(iobase + UART_LCR) & 0x3F;    /* mask out UART_LCR_DLAB and UART_LCR_SBC */
634 +       outb(lcr, iobase + UART_LCR);
635 +
636 +       /* Read up to 4 bytes from RX FIFO */
637 +       for (i = 0; i < 4; i++) {
638 +               inb(iobase + UART_RX);
639 +               if (!(inb(iobase + UART_LSR) & UART_LSR_DR))
640 +                       break;
641 +       }
642 +
643 +       /* Wait if CTS/DSR/DCD changing */
644 +       for (i = 1; i < 0x3E8; i++) {
645 +               if (!(inb(iobase + UART_MSR) & UART_MSR_ANY_DELTA))
646 +                       break;
647 +       }
648 +
649 +       /* Enable divisor latch access */
650 +       outb(lcr | UART_LCR_DLAB, iobase + UART_LCR);
651 +
652 +       /* Setup divisor latch */
653 +       outb(divisor & 0x00FF, iobase + UART_DLL);              /* divisor latch LOW byte */
654 +       outb((divisor & 0xFF00) >> 8, iobase + UART_DLM);       /* divisor latch HIGH byte */
655 +
656 +       /* Disable divisor latch access */
657 +       outb(lcr, iobase + UART_LCR);
658 +
659 +       /* Setup interrupts, enable sleep mode */
660 +       outb(ier, iobase + UART_IER);   /* we don't want to handle TX interrupts */
661 +
662 +       /* Skip pending interrupts */
663 +       for (i = 0; i < 4; i++) {
664 +               if (inb(iobase + UART_IIR) & UART_IIR_NO_INT)
665 +                       break;
666 +       }
667 +
668 +       /* 8N1 */
669 +       lcr = UART_LCR_WLEN8;
670 +       outb(lcr, iobase + UART_LCR);
671 +
672 +       /* Setup CTS/RTS flow control and 950 enhanced mode */
673 +       outb(UART_LCR_650, iobase + UART_LCR);
674 +       outb(UART_EFR_CTS | UART_EFR_RTS | UART_EFR_ECB,
675 +            iobase + UART_EFR);
676 +       outb(lcr, iobase + UART_LCR);
677 +
678 +       /* Read core id and revision */
679 +       outb(UART_ACR, iobase + UART_EMSR);
680 +       outb(UART_ACR_ICRRD, iobase + UART_LSR);        /* enable ICR read access, we don't need to save the old value of ACR */
681 +
682 +       outb(UART_ID1, iobase + UART_EMSR);
683 +       id1 = inb(iobase + UART_LSR);
684 +
685 +       outb(UART_ID2, iobase + UART_EMSR);
686 +       id2 = inb(iobase + UART_LSR);
687 +
688 +       outb(UART_ID3, iobase + UART_EMSR);
689 +       id3 = inb(iobase + UART_LSR);
690 +
691 +       outb(UART_REV, iobase + UART_EMSR);
692 +       rev = inb(iobase + UART_LSR);
693 +
694 +       if (id1 != 0x16 || id2 != 0xC9 || id3 != 0x50) {
695 +               printk(KERN_ERR "bt950_cs: Unknown UART core %02X%02X%02X found.\n", id1, id2, id3);
696 +               spin_unlock_irqrestore(&(info->lock), flags);
697 +               return -ENODEV;
698 +       }
699 +
700 +
701 +       /* Init ICR registers */
702 +       outb(UART_TTL, iobase + UART_EMSR);
703 +       outb(TR_TX_INT, iobase + UART_LSR);     /* TX interrupt trigger level (0-127) */
704 +
705 +       outb(UART_RTL, iobase + UART_EMSR);
706 +       outb(TR_RX_INT, iobase + UART_LSR);     /* RX interrupt trigger level (1-127) */
707 +
708 +       outb(UART_FCL, iobase + UART_EMSR);
709 +       outb(TR_CTL_LO, iobase + UART_LSR);     /* auto flow control LOWER trigger level (0-127) */
710 +
711 +       outb(UART_FCH, iobase + UART_EMSR);
712 +       outb(TR_CTL_HI, iobase + UART_LSR);     /* auto flow control HIGH trigger level (1-127) */
713 +
714 +       outb(UART_ACR, iobase + UART_EMSR);
715 +       outb(UART_ACR_TLENB, iobase + UART_LSR);        /* disable ICR read access, enable trigger levels */
716 +
717 +       spin_unlock_irqrestore(&(info->lock), flags);
718 +
719 +       return 0;
720 +}
721 +
722 +
723 +static void bt950_stop_uart(bt950_info_t *info)
724 +{
725 +       unsigned long flags;
726 +       unsigned int iobase = info->link.io.BasePort1;
727 +
728 +       spin_lock_irqsave(&(info->lock), flags);
729 +
730 +       /* Disable interrupts */
731 +       outb(0, iobase + UART_IER);
732 +
733 +       /* Set RTS and OUT2 low */
734 +       outb(0, iobase + UART_MCR);
735 +
736 +       spin_unlock_irqrestore(&(info->lock), flags);
737 +}
738 +
739 +
740 +static int bt950_open(bt950_info_t *info)
741 +{
742 +       struct hci_dev *hdev;
743 +       int err;
744 +
745 +       spin_lock_init(&(info->lock));
746 +
747 +       skb_queue_head_init(&(info->txq));
748 +
749 +       info->rx_state = RECV_WAIT_PACKET_TYPE;
750 +       info->rx_count = 0;
751 +       info->rx_skb = NULL;
752 +
753 +       /* Setup hardware */
754 +       if ((err = bt950_setup_uart(info)) < 0)
755 +               return err;
756 +
757 +       /* Timeout before it is safe to send the first HCI packet */
758 +       set_current_state(TASK_INTERRUPTIBLE);
759 +       schedule_timeout(HZ);
760 +
761 +
762 +       /* Initialize and register HCI device */
763 +
764 +       hdev = &(info->hdev);
765 +
766 +       hdev->type = HCI_PCCARD;
767 +       hdev->driver_data = info;
768 +
769 +       hdev->open = bt950_hci_open;
770 +       hdev->close = bt950_hci_close;
771 +       hdev->flush = bt950_hci_flush;
772 +       hdev->send = bt950_hci_send_frame;
773 +       hdev->destruct = bt950_hci_destruct;
774 +       hdev->ioctl = bt950_hci_ioctl;
775 +
776 +       if (hci_register_dev(hdev) < 0) {
777 +               printk(KERN_ERR "bt950_cs: Can't register HCI device %s.\n", hdev->name);
778 +               return -ENODEV;
779 +       }
780 +
781 +       return 0;
782 +}
783 +
784 +
785 +static int bt950_close(bt950_info_t *info)
786 +{
787 +       struct hci_dev *hdev = &(info->hdev);
788 +
789 +       bt950_hci_close(hdev);
790 +
791 +       /* Stop hardware */
792 +       bt950_stop_uart(info);
793 +
794 +       if (hci_unregister_dev(hdev) < 0)
795 +               printk(KERN_ERR "bt950_cs: Can't unregister HCI device %s.\n", hdev->name);
796 +
797 +       return 0;
798 +}
799 +
800 +
801 +
802 +/* ======================== Card services ======================== */
803 +
804 +
805 +static void cs_error(client_handle_t handle, int func, int ret)
806 +{
807 +       error_info_t err = { func, ret };
808 +
809 +       CardServices(ReportError, handle, &err);
810 +}
811 +
812 +
813 +static dev_link_t *bt950_attach(void)
814 +{
815 +       bt950_info_t *info;
816 +       client_reg_t client_reg;
817 +       dev_link_t *link;
818 +       int i, ret;
819 +
820 +       /* Create new info device */
821 +       info = kmalloc(sizeof(*info), GFP_KERNEL);
822 +       if (!info)
823 +               return NULL;
824 +       memset(info, 0, sizeof(*info));
825 +
826 +       link = &info->link;
827 +       link->priv = info;
828 +
829 +       link->release.function = &bt950_release;
830 +       link->release.data = (u_long)link;
831 +       link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
832 +       link->io.NumPorts1 = 8;
833 +       link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
834 +       link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
835 +
836 +       if (irq_list[0] == -1)
837 +               link->irq.IRQInfo2 = irq_mask;
838 +       else
839 +               for (i = 0; i < 4; i++)
840 +                       link->irq.IRQInfo2 |= 1 << irq_list[i];
841 +
842 +       link->irq.Handler = bt950_interrupt;
843 +       link->irq.Instance = info;
844 +
845 +       link->conf.Attributes = CONF_ENABLE_IRQ;
846 +       link->conf.IntType = INT_MEMORY_AND_IO;
847 +       link->conf.Present =
848 +               PRESENT_OPTION | PRESENT_STATUS | PRESENT_PIN_REPLACE |
849 +               PRESENT_COPY;
850 +
851 +       /* Register with Card Services */
852 +       link->next = dev_list;
853 +       dev_list = link;
854 +       client_reg.dev_info = &dev_info;
855 +       client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
856 +       client_reg.EventMask =
857 +               CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
858 +               CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
859 +               CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
860 +       client_reg.event_handler = &bt950_event;
861 +       client_reg.Version = 0x0210;
862 +       client_reg.event_callback_args.client_data = link;
863 +
864 +       ret = CardServices(RegisterClient, &link->handle, &client_reg);
865 +       if (ret != CS_SUCCESS) {
866 +               cs_error(link->handle, RegisterClient, ret);
867 +               bt950_detach(link);
868 +               return NULL;
869 +       }
870 +
871 +       return link;
872 +}
873 +
874 +
875 +static void bt950_detach(dev_link_t *link)
876 +{
877 +       bt950_info_t *info = link->priv;
878 +       dev_link_t **linkp;
879 +       int ret;
880 +
881 +       /* Locate device structure */
882 +       for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
883 +               if (*linkp == link)
884 +                       break;
885 +
886 +       if (*linkp == NULL)
887 +               return;
888 +
889 +       del_timer(&link->release);
890 +       if (link->state & DEV_CONFIG)
891 +               bt950_release((u_long) link);
892 +
893 +       if (link->handle) {
894 +               ret = CardServices(DeregisterClient, link->handle);
895 +               if (ret != CS_SUCCESS)
896 +                       cs_error(link->handle, DeregisterClient, ret);
897 +       }
898 +
899 +       /* Unlink device structure, free bits */
900 +       *linkp = link->next;
901 +
902 +       kfree(info);
903 +}
904 +
905 +
906 +static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
907 +{
908 +       int i;
909 +
910 +       i = CardServices(fn, handle, tuple);
911 +       if (i != CS_SUCCESS)
912 +               return CS_NO_MORE_ITEMS;
913 +
914 +       i = CardServices(GetTupleData, handle, tuple);
915 +       if (i != CS_SUCCESS)
916 +               return i;
917 +
918 +       return CardServices(ParseTuple, handle, tuple, parse);
919 +}
920 +
921 +
922 +#define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
923 +#define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
924 +
925 +static void bt950_config(dev_link_t *link)
926 +{
927 +       static ioaddr_t base[4] = { 0x2f8, 0x3e8, 0x2e8, 0x0 };
928 +       client_handle_t handle = link->handle;
929 +       bt950_info_t *info = link->priv;
930 +       tuple_t tuple;
931 +       u_short buf[256];
932 +       cisparse_t parse;
933 +       cistpl_cftable_entry_t *cf = &parse.cftable_entry;
934 +       config_info_t config;
935 +       int i, j, try, last_ret, last_fn;
936 +
937 +       tuple.TupleData = (cisdata_t *)buf;
938 +       tuple.TupleOffset = 0;
939 +       tuple.TupleDataMax = 255;
940 +       tuple.Attributes = 0;
941 +
942 +       /* Get configuration register information */
943 +       tuple.DesiredTuple = CISTPL_CONFIG;
944 +       last_ret = first_tuple(handle, &tuple, &parse);
945 +       if (last_ret != CS_SUCCESS) {
946 +               last_fn = ParseTuple;
947 +               goto cs_failed;
948 +       }
949 +       link->conf.ConfigBase = parse.config.base;
950 +       link->conf.Present = parse.config.rmask[0];
951 +
952 +       /* Configure card */
953 +       link->state |= DEV_CONFIG;
954 +       i = CardServices(GetConfigurationInfo, handle, &config);
955 +       link->conf.Vcc = config.Vcc;
956 +
957 +       /* First pass: look for a config entry that looks normal. */
958 +       tuple.TupleData = (cisdata_t *) buf;
959 +       tuple.TupleOffset = 0;
960 +       tuple.TupleDataMax = 255;
961 +       tuple.Attributes = 0;
962 +       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
963 +       /* Two tries: without IO aliases, then with aliases */
964 +       for (try = 0; try < 2; try++) {
965 +               i = first_tuple(handle, &tuple, &parse);
966 +               while (i != CS_NO_MORE_ITEMS) {
967 +                       if (i != CS_SUCCESS)
968 +                               goto next_entry;
969 +                       if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
970 +                               link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
971 +                       if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
972 +                               link->conf.ConfigIndex = cf->index;
973 +                               link->io.BasePort1 = cf->io.win[0].base;
974 +                               link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
975 +                               i = CardServices(RequestIO, link->handle, &link->io);
976 +                               if (i == CS_SUCCESS)
977 +                                       goto found_port;
978 +                       }
979 +next_entry:
980 +                       i = next_tuple(handle, &tuple, &parse);
981 +               }
982 +       }
983 +
984 +       /* Second pass: try to find an entry that isn't picky about
985 +          its base address, then try to grab any standard serial port
986 +          address, and finally try to get any free port. */
987 +       i = first_tuple(handle, &tuple, &parse);
988 +       while (i != CS_NO_MORE_ITEMS) {
989 +               if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
990 +                   && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
991 +                       link->conf.ConfigIndex = cf->index;
992 +                       for (j = 0; j < 5; j++) {
993 +                               link->io.BasePort1 = base[j];
994 +                               link->io.IOAddrLines = base[j] ? 16 : 3;
995 +                               i = CardServices(RequestIO, link->handle, &link->io);
996 +                               if (i == CS_SUCCESS)
997 +                                       goto found_port;
998 +                       }
999 +               }
1000 +               i = next_tuple(handle, &tuple, &parse);
1001 +       }
1002 +
1003 +found_port:
1004 +       if (i != CS_SUCCESS) {
1005 +               printk(KERN_ERR "bt950_cs: No usable port range found. Giving up.\n");
1006 +               cs_error(link->handle, RequestIO, i);
1007 +               goto failed;
1008 +       }
1009 +
1010 +       i = CardServices(RequestIRQ, link->handle, &link->irq);
1011 +       if (i != CS_SUCCESS) {
1012 +               cs_error(link->handle, RequestIRQ, i);
1013 +               link->irq.AssignedIRQ = 0;
1014 +       }
1015 +
1016 +       i = CardServices(RequestConfiguration, link->handle, &link->conf);
1017 +       if (i != CS_SUCCESS) {
1018 +               cs_error(link->handle, RequestConfiguration, i);
1019 +               goto failed;
1020 +       }
1021 +
1022 +       MOD_INC_USE_COUNT;
1023 +
1024 +       if (bt950_open(info) != 0)
1025 +               goto failed;
1026 +
1027 +       strcpy(info->node.dev_name, info->hdev.name);
1028 +       link->dev = &info->node;
1029 +       link->state &= ~DEV_CONFIG_PENDING;
1030 +
1031 +       return;
1032 +
1033 +cs_failed:
1034 +       cs_error(link->handle, last_fn, last_ret);
1035 +
1036 +failed:
1037 +       bt950_release((u_long)link);
1038 +       link->state &= ~DEV_CONFIG_PENDING;
1039 +}
1040 +
1041 +
1042 +static void bt950_release(u_long arg)
1043 +{
1044 +       dev_link_t *link = (dev_link_t *) arg;
1045 +       bt950_info_t *info = link->priv;
1046 +
1047 +       if (link->state & DEV_PRESENT)
1048 +               bt950_close(info);
1049 +
1050 +       MOD_DEC_USE_COUNT;
1051 +
1052 +       link->dev = NULL;
1053 +
1054 +       CardServices(ReleaseConfiguration, link->handle);
1055 +       CardServices(ReleaseIO, link->handle, &link->io);
1056 +       CardServices(ReleaseIRQ, link->handle, &link->irq);
1057 +
1058 +       link->state &= ~DEV_CONFIG;
1059 +}
1060 +
1061 +
1062 +static int bt950_event(event_t event, int priority, event_callback_args_t *args)
1063 +{
1064 +       dev_link_t *link = args->client_data;
1065 +       bt950_info_t *info = link->priv;
1066 +
1067 +       switch (event) {
1068 +       case CS_EVENT_CARD_REMOVAL:
1069 +               link->state &= ~DEV_PRESENT;
1070 +               if (link->state & DEV_CONFIG) {
1071 +                       bt950_close(info);
1072 +                       link->state |= DEV_RELEASE_PENDING;
1073 +                       mod_timer(&link->release, jiffies + HZ / 20);
1074 +               }
1075 +               break;
1076 +       case CS_EVENT_CARD_INSERTION:
1077 +               link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1078 +               bt950_config(link);
1079 +               break;
1080 +       case CS_EVENT_PM_SUSPEND:
1081 +               link->state |= DEV_SUSPEND;
1082 +               /* Fall through... */
1083 +       case CS_EVENT_RESET_PHYSICAL:
1084 +               if (link->state & DEV_CONFIG) {
1085 +                       bt950_stop_uart(info);
1086 +                       CardServices(ReleaseConfiguration, link->handle);
1087 +               }
1088 +               break;
1089 +       case CS_EVENT_PM_RESUME:
1090 +               link->state &= ~DEV_SUSPEND;
1091 +               /* Fall through... */
1092 +       case CS_EVENT_CARD_RESET:
1093 +               if (link->state & DEV_CONFIG) {
1094 +                       CardServices(RequestConfiguration, link->handle, &link->conf);
1095 +                       bt950_setup_uart(info);
1096 +               }
1097 +               break;
1098 +       }
1099 +
1100 +       return 0;
1101 +}
1102 +
1103 +
1104 +
1105 +/* ======================== Module initialization ======================== */
1106 +
1107 +
1108 +int __init init_bt950_cs(void)
1109 +{
1110 +       servinfo_t serv;
1111 +       int err;
1112 +
1113 +       CardServices(GetCardServicesInfo, &serv);
1114 +       if (serv.Revision != CS_RELEASE_CODE) {
1115 +               printk(KERN_NOTICE "bt950_cs: Card Services release does not match!\n");
1116 +//             return -1;
1117 +       }
1118 +
1119 +       err = register_pccard_driver(&dev_info, &bt950_attach, &bt950_detach);
1120 +
1121 +       return err;
1122 +}
1123 +
1124 +
1125 +void __exit exit_bt950_cs(void)
1126 +{
1127 +       unregister_pccard_driver(&dev_info);
1128 +
1129 +       while (dev_list != NULL)
1130 +               bt950_detach(dev_list);
1131 +}
1132 +
1133 +
1134 +module_init(init_bt950_cs);
1135 +module_exit(exit_bt950_cs);
1136 +
1137 +EXPORT_NO_SYMBOLS;
1138 diff -Nur linux-orig/drivers/bluetooth/Config.in linux/drivers/bluetooth/Config.in
1139 --- linux-orig/drivers/bluetooth/Config.in      2004-02-16 08:45:33.000000000 +0300
1140 +++ linux/drivers/bluetooth/Config.in   2004-02-16 08:50:36.000000000 +0300
1141 @@ -21,7 +21,7 @@
1142  
1143  dep_tristate 'HCI DTL1 (PC Card) driver' CONFIG_BLUEZ_HCIDTL1 $CONFIG_PCMCIA $CONFIG_BLUEZ
1144  
1145 -dep_tristate 'HCI BT3C (PC Card) driver' CONFIG_BLUEZ_HCIBT3C $CONFIG_PCMCIA $CONFIG_BLUEZ
1146 +# dep_tristate 'HCI BT3C (PC Card) driver' CONFIG_BLUEZ_HCIBT3C $CONFIG_PCMCIA $CONFIG_BLUEZ
1147  
1148  dep_tristate 'HCI BlueCard (PC Card) driver' CONFIG_BLUEZ_HCIBLUECARD $CONFIG_PCMCIA $CONFIG_BLUEZ
1149  
1150 @@ -29,5 +29,7 @@
1151  
1152  dep_tristate 'HCI VHCI (Virtual HCI device) driver' CONFIG_BLUEZ_HCIVHCI $CONFIG_BLUEZ
1153  
1154 +dep_tristate 'HCI BT950 (BT950 device) driver' CONFIG_BLUEZ_BT950 $CONFIG_BLUEZ
1155 +
1156  endmenu
1157  
1158 diff -Nur linux-orig/drivers/bluetooth/Makefile linux/drivers/bluetooth/Makefile
1159 --- linux-orig/drivers/bluetooth/Makefile       2004-02-16 08:45:33.000000000 +0300
1160 +++ linux/drivers/bluetooth/Makefile    2004-02-16 08:50:47.000000000 +0300
1161 @@ -17,10 +17,12 @@
1162  obj-$(CONFIG_BLUEZ_HCIBFUSB)   += bfusb.o
1163  
1164  obj-$(CONFIG_BLUEZ_HCIDTL1)    += dtl1_cs.o
1165 -obj-$(CONFIG_BLUEZ_HCIBT3C)    += bt3c_cs.o
1166 +# obj-$(CONFIG_BLUEZ_HCIBT3C)  += bt3c_cs.o
1167  obj-$(CONFIG_BLUEZ_HCIBLUECARD)        += bluecard_cs.o
1168  obj-$(CONFIG_BLUEZ_HCIBTUART)  += btuart_cs.o
1169  
1170 +obj-$(CONFIG_BLUEZ_BT950)      += bt950_cs.o
1171 +
1172  include $(TOPDIR)/Rules.make
1173  
1174  hci_uart.o: $(uart-y)