blob: 6d34f405a2f37424976a685c669217bbd17fbca9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/types.h>
29#include <linux/sched.h>
30#include <linux/delay.h>
31#include <linux/timer.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#include <linux/wait.h>
38
39#include <linux/skbuff.h>
Cody Resterc89827e2010-07-01 21:27:44 -070040#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <pcmcia/cs_types.h>
43#include <pcmcia/cs.h>
44#include <pcmcia/cistpl.h>
45#include <pcmcia/ciscode.h>
46#include <pcmcia/ds.h>
47#include <pcmcia/cisreg.h>
48
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51
52
53
54/* ======================== Module parameters ======================== */
55
56
57MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
58MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
59MODULE_LICENSE("GPL");
60
61
62
63/* ======================== Local structures ======================== */
64
65
66typedef struct bluecard_info_t {
Dominik Brodowskifd238232006-03-05 10:45:09 +010067 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 struct hci_dev *hdev;
70
71 spinlock_t lock; /* For serializing operations */
72 struct timer_list timer; /* For LED control */
73
74 struct sk_buff_head txq;
75 unsigned long tx_state;
76
77 unsigned long rx_state;
78 unsigned long rx_count;
79 struct sk_buff *rx_skb;
80
81 unsigned char ctrl_reg;
82 unsigned long hw_state; /* Status of the hardware and LED control */
83} bluecard_info_t;
84
85
Dominik Brodowski15b99ac2006-03-31 17:26:06 +020086static int bluecard_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +020087static void bluecard_release(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Dominik Brodowskicc3b4862005-11-14 21:23:14 +010089static void bluecard_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Default baud rate: 57600, 115200, 230400 or 460800 */
93#define DEFAULT_BAUD_RATE 230400
94
95
96/* Hardware states */
97#define CARD_READY 1
98#define CARD_HAS_PCCARD_ID 4
99#define CARD_HAS_POWER_LED 5
100#define CARD_HAS_ACTIVITY_LED 6
101
102/* Transmit states */
103#define XMIT_SENDING 1
104#define XMIT_WAKEUP 2
105#define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
106#define XMIT_BUF_ONE_READY 6
107#define XMIT_BUF_TWO_READY 7
108#define XMIT_SENDING_READY 8
109
110/* Receiver states */
111#define RECV_WAIT_PACKET_TYPE 0
112#define RECV_WAIT_EVENT_HEADER 1
113#define RECV_WAIT_ACL_HEADER 2
114#define RECV_WAIT_SCO_HEADER 3
115#define RECV_WAIT_DATA 4
116
117/* Special packet types */
118#define PKT_BAUD_RATE_57600 0x80
119#define PKT_BAUD_RATE_115200 0x81
120#define PKT_BAUD_RATE_230400 0x82
121#define PKT_BAUD_RATE_460800 0x83
122
123
124/* These are the register offsets */
125#define REG_COMMAND 0x20
126#define REG_INTERRUPT 0x21
127#define REG_CONTROL 0x22
128#define REG_RX_CONTROL 0x24
129#define REG_CARD_RESET 0x30
130#define REG_LED_CTRL 0x30
131
132/* REG_COMMAND */
133#define REG_COMMAND_TX_BUF_ONE 0x01
134#define REG_COMMAND_TX_BUF_TWO 0x02
135#define REG_COMMAND_RX_BUF_ONE 0x04
136#define REG_COMMAND_RX_BUF_TWO 0x08
137#define REG_COMMAND_RX_WIN_ONE 0x00
138#define REG_COMMAND_RX_WIN_TWO 0x10
139
140/* REG_CONTROL */
141#define REG_CONTROL_BAUD_RATE_57600 0x00
142#define REG_CONTROL_BAUD_RATE_115200 0x01
143#define REG_CONTROL_BAUD_RATE_230400 0x02
144#define REG_CONTROL_BAUD_RATE_460800 0x03
145#define REG_CONTROL_RTS 0x04
146#define REG_CONTROL_BT_ON 0x08
147#define REG_CONTROL_BT_RESET 0x10
148#define REG_CONTROL_BT_RES_PU 0x20
149#define REG_CONTROL_INTERRUPT 0x40
150#define REG_CONTROL_CARD_RESET 0x80
151
152/* REG_RX_CONTROL */
153#define RTS_LEVEL_SHIFT_BITS 0x02
154
155
156
157/* ======================== LED handling routines ======================== */
158
159
160static void bluecard_activity_led_timeout(u_long arg)
161{
162 bluecard_info_t *info = (bluecard_info_t *)arg;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100163 unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
166 return;
167
168 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
169 /* Disable activity LED */
170 outb(0x08 | 0x20, iobase + 0x30);
171 } else {
172 /* Disable power LED */
173 outb(0x00, iobase + 0x30);
174 }
175}
176
177
178static void bluecard_enable_activity_led(bluecard_info_t *info)
179{
Dominik Brodowskifd238232006-03-05 10:45:09 +0100180 unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
183 return;
184
185 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
186 /* Enable activity LED */
187 outb(0x10 | 0x40, iobase + 0x30);
188
189 /* Stop the LED after HZ/4 */
190 mod_timer(&(info->timer), jiffies + HZ / 4);
191 } else {
192 /* Enable power LED */
193 outb(0x08 | 0x20, iobase + 0x30);
194
195 /* Stop the LED after HZ/2 */
196 mod_timer(&(info->timer), jiffies + HZ / 2);
197 }
198}
199
200
201
202/* ======================== Interrupt handling ======================== */
203
204
205static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
206{
207 int i, actual;
208
209 actual = (len > 15) ? 15 : len;
210
211 outb_p(actual, iobase + offset);
212
213 for (i = 0; i < actual; i++)
214 outb_p(buf[i], iobase + offset + i + 1);
215
216 return actual;
217}
218
219
220static void bluecard_write_wakeup(bluecard_info_t *info)
221{
222 if (!info) {
223 BT_ERR("Unknown device");
224 return;
225 }
226
227 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
228 return;
229
230 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
231 set_bit(XMIT_WAKEUP, &(info->tx_state));
232 return;
233 }
234
235 do {
Dominik Brodowskifd238232006-03-05 10:45:09 +0100236 register unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 register unsigned int offset;
238 register unsigned char command;
239 register unsigned long ready_bit;
240 register struct sk_buff *skb;
241 register int len;
242
243 clear_bit(XMIT_WAKEUP, &(info->tx_state));
244
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100245 if (!pcmcia_dev_present(info->p_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return;
247
248 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
249 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
250 break;
251 offset = 0x10;
252 command = REG_COMMAND_TX_BUF_TWO;
253 ready_bit = XMIT_BUF_TWO_READY;
254 } else {
255 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
256 break;
257 offset = 0x00;
258 command = REG_COMMAND_TX_BUF_ONE;
259 ready_bit = XMIT_BUF_ONE_READY;
260 }
261
262 if (!(skb = skb_dequeue(&(info->txq))))
263 break;
264
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700265 if (bt_cb(skb)->pkt_type & 0x80) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* Disable RTS */
267 info->ctrl_reg |= REG_CONTROL_RTS;
268 outb(info->ctrl_reg, iobase + REG_CONTROL);
269 }
270
271 /* Activate LED */
272 bluecard_enable_activity_led(info);
273
274 /* Send frame */
275 len = bluecard_write(iobase, offset, skb->data, skb->len);
276
277 /* Tell the FPGA to send the data */
278 outb_p(command, iobase + REG_COMMAND);
279
280 /* Mark the buffer as dirty */
281 clear_bit(ready_bit, &(info->tx_state));
282
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700283 if (bt_cb(skb)->pkt_type & 0x80) {
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800284 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 DEFINE_WAIT(wait);
286
287 unsigned char baud_reg;
288
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700289 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case PKT_BAUD_RATE_460800:
291 baud_reg = REG_CONTROL_BAUD_RATE_460800;
292 break;
293 case PKT_BAUD_RATE_230400:
294 baud_reg = REG_CONTROL_BAUD_RATE_230400;
295 break;
296 case PKT_BAUD_RATE_115200:
297 baud_reg = REG_CONTROL_BAUD_RATE_115200;
298 break;
299 case PKT_BAUD_RATE_57600:
300 /* Fall through... */
301 default:
302 baud_reg = REG_CONTROL_BAUD_RATE_57600;
303 break;
304 }
305
306 /* Wait until the command reaches the baseband */
307 prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
308 schedule_timeout(HZ/10);
309 finish_wait(&wq, &wait);
310
311 /* Set baud on baseband */
312 info->ctrl_reg &= ~0x03;
313 info->ctrl_reg |= baud_reg;
314 outb(info->ctrl_reg, iobase + REG_CONTROL);
315
316 /* Enable RTS */
317 info->ctrl_reg &= ~REG_CONTROL_RTS;
318 outb(info->ctrl_reg, iobase + REG_CONTROL);
319
320 /* Wait before the next HCI packet can be send */
321 prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
322 schedule_timeout(HZ);
323 finish_wait(&wq, &wait);
324 }
325
326 if (len == skb->len) {
327 kfree_skb(skb);
328 } else {
329 skb_pull(skb, len);
330 skb_queue_head(&(info->txq), skb);
331 }
332
333 info->hdev->stat.byte_tx += len;
334
335 /* Change buffer */
336 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
337
338 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
339
340 clear_bit(XMIT_SENDING, &(info->tx_state));
341}
342
343
344static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
345{
346 int i, n, len;
347
348 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
349
350 len = inb(iobase + offset);
351 n = 0;
352 i = 1;
353
354 while (n < len) {
355
356 if (i == 16) {
357 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
358 i = 0;
359 }
360
361 buf[n] = inb(iobase + offset + i);
362
363 n++;
364 i++;
365
366 }
367
368 return len;
369}
370
371
372static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
373{
374 unsigned int iobase;
375 unsigned char buf[31];
376 int i, len;
377
378 if (!info) {
379 BT_ERR("Unknown device");
380 return;
381 }
382
Dominik Brodowskifd238232006-03-05 10:45:09 +0100383 iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
386 bluecard_enable_activity_led(info);
387
388 len = bluecard_read(iobase, offset, buf, sizeof(buf));
389
390 for (i = 0; i < len; i++) {
391
392 /* Allocate packet */
393 if (info->rx_skb == NULL) {
394 info->rx_state = RECV_WAIT_PACKET_TYPE;
395 info->rx_count = 0;
396 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
397 BT_ERR("Can't allocate mem for new packet");
398 return;
399 }
400 }
401
402 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
403
404 info->rx_skb->dev = (void *) info->hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700405 bt_cb(info->rx_skb)->pkt_type = buf[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700407 switch (bt_cb(info->rx_skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 case 0x00:
410 /* init packet */
411 if (offset != 0x00) {
412 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
413 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
414 set_bit(XMIT_SENDING_READY, &(info->tx_state));
415 bluecard_write_wakeup(info);
416 }
417
418 kfree_skb(info->rx_skb);
419 info->rx_skb = NULL;
420 break;
421
422 case HCI_EVENT_PKT:
423 info->rx_state = RECV_WAIT_EVENT_HEADER;
424 info->rx_count = HCI_EVENT_HDR_SIZE;
425 break;
426
427 case HCI_ACLDATA_PKT:
428 info->rx_state = RECV_WAIT_ACL_HEADER;
429 info->rx_count = HCI_ACL_HDR_SIZE;
430 break;
431
432 case HCI_SCODATA_PKT:
433 info->rx_state = RECV_WAIT_SCO_HEADER;
434 info->rx_count = HCI_SCO_HDR_SIZE;
435 break;
436
437 default:
438 /* unknown packet */
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700439 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 info->hdev->stat.err_rx++;
441
442 kfree_skb(info->rx_skb);
443 info->rx_skb = NULL;
444 break;
445
446 }
447
448 } else {
449
450 *skb_put(info->rx_skb, 1) = buf[i];
451 info->rx_count--;
452
453 if (info->rx_count == 0) {
454
455 int dlen;
456 struct hci_event_hdr *eh;
457 struct hci_acl_hdr *ah;
458 struct hci_sco_hdr *sh;
459
460 switch (info->rx_state) {
461
462 case RECV_WAIT_EVENT_HEADER:
Arnaldo Carvalho de Melo2a123b82007-03-27 18:38:07 -0300463 eh = hci_event_hdr(info->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 info->rx_state = RECV_WAIT_DATA;
465 info->rx_count = eh->plen;
466 break;
467
468 case RECV_WAIT_ACL_HEADER:
Arnaldo Carvalho de Melo2a123b82007-03-27 18:38:07 -0300469 ah = hci_acl_hdr(info->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dlen = __le16_to_cpu(ah->dlen);
471 info->rx_state = RECV_WAIT_DATA;
472 info->rx_count = dlen;
473 break;
474
475 case RECV_WAIT_SCO_HEADER:
Arnaldo Carvalho de Melo2a123b82007-03-27 18:38:07 -0300476 sh = hci_sco_hdr(info->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 info->rx_state = RECV_WAIT_DATA;
478 info->rx_count = sh->dlen;
479 break;
480
481 case RECV_WAIT_DATA:
482 hci_recv_frame(info->rx_skb);
483 info->rx_skb = NULL;
484 break;
485
486 }
487
488 }
489
490 }
491
492
493 }
494
495 info->hdev->stat.byte_rx += len;
496}
497
498
David Howells7d12e782006-10-05 14:55:46 +0100499static irqreturn_t bluecard_interrupt(int irq, void *dev_inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 bluecard_info_t *info = dev_inst;
502 unsigned int iobase;
503 unsigned char reg;
504
Mike Frysinger74278472009-09-14 13:43:49 -0400505 if (!info || !info->hdev)
506 /* our irq handler is shared */
507 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!test_bit(CARD_READY, &(info->hw_state)))
510 return IRQ_HANDLED;
511
Dominik Brodowskifd238232006-03-05 10:45:09 +0100512 iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 spin_lock(&(info->lock));
515
516 /* Disable interrupt */
517 info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
518 outb(info->ctrl_reg, iobase + REG_CONTROL);
519
520 reg = inb(iobase + REG_INTERRUPT);
521
522 if ((reg != 0x00) && (reg != 0xff)) {
523
524 if (reg & 0x04) {
525 bluecard_receive(info, 0x00);
526 outb(0x04, iobase + REG_INTERRUPT);
527 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
528 }
529
530 if (reg & 0x08) {
531 bluecard_receive(info, 0x10);
532 outb(0x08, iobase + REG_INTERRUPT);
533 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
534 }
535
536 if (reg & 0x01) {
537 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
538 outb(0x01, iobase + REG_INTERRUPT);
539 bluecard_write_wakeup(info);
540 }
541
542 if (reg & 0x02) {
543 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
544 outb(0x02, iobase + REG_INTERRUPT);
545 bluecard_write_wakeup(info);
546 }
547
548 }
549
550 /* Enable interrupt */
551 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
552 outb(info->ctrl_reg, iobase + REG_CONTROL);
553
554 spin_unlock(&(info->lock));
555
556 return IRQ_HANDLED;
557}
558
559
560
561/* ======================== Device specific HCI commands ======================== */
562
563
564static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
565{
566 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
567 struct sk_buff *skb;
568
569 /* Ericsson baud rate command */
570 unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
571
572 if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
573 BT_ERR("Can't allocate mem for new packet");
574 return -1;
575 }
576
577 switch (baud) {
578 case 460800:
579 cmd[4] = 0x00;
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700580 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 case 230400:
583 cmd[4] = 0x01;
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700584 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_230400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 case 115200:
587 cmd[4] = 0x02;
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700588 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 case 57600:
591 /* Fall through... */
592 default:
593 cmd[4] = 0x03;
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700594 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 }
597
598 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
599
600 skb_queue_tail(&(info->txq), skb);
601
602 bluecard_write_wakeup(info);
603
604 return 0;
605}
606
607
608
609/* ======================== HCI interface ======================== */
610
611
612static int bluecard_hci_flush(struct hci_dev *hdev)
613{
614 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
615
616 /* Drop TX queue */
617 skb_queue_purge(&(info->txq));
618
619 return 0;
620}
621
622
623static int bluecard_hci_open(struct hci_dev *hdev)
624{
625 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100626 unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
629 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
630
631 if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
632 return 0;
633
634 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
635 /* Enable LED */
636 outb(0x08 | 0x20, iobase + 0x30);
637 }
638
639 return 0;
640}
641
642
643static int bluecard_hci_close(struct hci_dev *hdev)
644{
645 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100646 unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
649 return 0;
650
651 bluecard_hci_flush(hdev);
652
653 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
654 /* Disable LED */
655 outb(0x00, iobase + 0x30);
656 }
657
658 return 0;
659}
660
661
662static int bluecard_hci_send_frame(struct sk_buff *skb)
663{
664 bluecard_info_t *info;
665 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
666
667 if (!hdev) {
668 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
669 return -ENODEV;
670 }
671
672 info = (bluecard_info_t *)(hdev->driver_data);
673
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700674 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 case HCI_COMMAND_PKT:
676 hdev->stat.cmd_tx++;
677 break;
678 case HCI_ACLDATA_PKT:
679 hdev->stat.acl_tx++;
680 break;
681 case HCI_SCODATA_PKT:
682 hdev->stat.sco_tx++;
683 break;
684 };
685
686 /* Prepend skb with frame type */
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700687 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 skb_queue_tail(&(info->txq), skb);
689
690 bluecard_write_wakeup(info);
691
692 return 0;
693}
694
695
696static void bluecard_hci_destruct(struct hci_dev *hdev)
697{
698}
699
700
701static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
702{
703 return -ENOIOCTLCMD;
704}
705
706
707
708/* ======================== Card services HCI interaction ======================== */
709
710
711static int bluecard_open(bluecard_info_t *info)
712{
Dominik Brodowskifd238232006-03-05 10:45:09 +0100713 unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct hci_dev *hdev;
715 unsigned char id;
716
717 spin_lock_init(&(info->lock));
718
719 init_timer(&(info->timer));
720 info->timer.function = &bluecard_activity_led_timeout;
721 info->timer.data = (u_long)info;
722
723 skb_queue_head_init(&(info->txq));
724
725 info->rx_state = RECV_WAIT_PACKET_TYPE;
726 info->rx_count = 0;
727 info->rx_skb = NULL;
728
729 /* Initialize HCI device */
730 hdev = hci_alloc_dev();
731 if (!hdev) {
732 BT_ERR("Can't allocate HCI device");
733 return -ENOMEM;
734 }
735
736 info->hdev = hdev;
737
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100738 hdev->bus = HCI_PCCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 hdev->driver_data = info;
Marcel Holtmann27d35282006-07-03 10:02:37 +0200740 SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 hdev->open = bluecard_hci_open;
743 hdev->close = bluecard_hci_close;
744 hdev->flush = bluecard_hci_flush;
745 hdev->send = bluecard_hci_send_frame;
746 hdev->destruct = bluecard_hci_destruct;
747 hdev->ioctl = bluecard_hci_ioctl;
748
749 hdev->owner = THIS_MODULE;
750
751 id = inb(iobase + 0x30);
752
753 if ((id & 0x0f) == 0x02)
754 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
755
756 if (id & 0x10)
757 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
758
759 if (id & 0x20)
760 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
761
762 /* Reset card */
763 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
764 outb(info->ctrl_reg, iobase + REG_CONTROL);
765
766 /* Turn FPGA off */
767 outb(0x80, iobase + 0x30);
768
769 /* Wait some time */
770 msleep(10);
771
772 /* Turn FPGA on */
773 outb(0x00, iobase + 0x30);
774
775 /* Activate card */
776 info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
777 outb(info->ctrl_reg, iobase + REG_CONTROL);
778
779 /* Enable interrupt */
780 outb(0xff, iobase + REG_INTERRUPT);
781 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
782 outb(info->ctrl_reg, iobase + REG_CONTROL);
783
784 if ((id & 0x0f) == 0x03) {
785 /* Disable RTS */
786 info->ctrl_reg |= REG_CONTROL_RTS;
787 outb(info->ctrl_reg, iobase + REG_CONTROL);
788
789 /* Set baud rate */
790 info->ctrl_reg |= 0x03;
791 outb(info->ctrl_reg, iobase + REG_CONTROL);
792
793 /* Enable RTS */
794 info->ctrl_reg &= ~REG_CONTROL_RTS;
795 outb(info->ctrl_reg, iobase + REG_CONTROL);
796
797 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
798 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
799 set_bit(XMIT_SENDING_READY, &(info->tx_state));
800 }
801
802 /* Start the RX buffers */
803 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
804 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
805
806 /* Signal that the hardware is ready */
807 set_bit(CARD_READY, &(info->hw_state));
808
809 /* Drop TX queue */
810 skb_queue_purge(&(info->txq));
811
812 /* Control the point at which RTS is enabled */
813 outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
814
815 /* Timeout before it is safe to send the first HCI packet */
816 msleep(1250);
817
818 /* Register HCI device */
819 if (hci_register_dev(hdev) < 0) {
820 BT_ERR("Can't register HCI device");
821 info->hdev = NULL;
822 hci_free_dev(hdev);
823 return -ENODEV;
824 }
825
826 return 0;
827}
828
829
830static int bluecard_close(bluecard_info_t *info)
831{
Dominik Brodowskifd238232006-03-05 10:45:09 +0100832 unsigned int iobase = info->p_dev->io.BasePort1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct hci_dev *hdev = info->hdev;
834
835 if (!hdev)
836 return -ENODEV;
837
838 bluecard_hci_close(hdev);
839
840 clear_bit(CARD_READY, &(info->hw_state));
841
842 /* Reset card */
843 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
844 outb(info->ctrl_reg, iobase + REG_CONTROL);
845
846 /* Turn FPGA off */
847 outb(0x80, iobase + 0x30);
848
849 if (hci_unregister_dev(hdev) < 0)
850 BT_ERR("Can't unregister HCI device %s", hdev->name);
851
852 hci_free_dev(hdev);
853
854 return 0;
855}
856
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200857static int bluecard_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 bluecard_info_t *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* Create new info device */
Deepak Saxena089b1db2005-11-07 01:01:26 -0800862 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!info)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100864 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200866 info->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 link->priv = info;
868
869 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
870 link->io.NumPorts1 = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 link->conf.Attributes = CONF_ENABLE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 link->conf.IntType = INT_MEMORY_AND_IO;
874
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200875 return bluecard_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200879static void bluecard_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 bluecard_info_t *info = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100883 bluecard_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 kfree(info);
885}
886
887
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200888static int bluecard_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 bluecard_info_t *info = link->priv;
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400891 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 link->conf.ConfigIndex = 0x20;
894 link->io.NumPorts1 = 64;
895 link->io.IOAddrLines = 6;
896
897 for (n = 0; n < 0x400; n += 0x40) {
898 link->io.BasePort1 = n ^ 0x300;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200899 i = pcmcia_request_io(link, &link->io);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200900 if (i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
902 }
903
Dominik Brodowski9ac3e582009-10-24 15:45:06 +0200904 if (i != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Dominik Brodowskieb141202010-03-07 12:21:16 +0100907 i = pcmcia_request_irq(link, bluecard_interrupt);
Dominik Brodowski9ac3e582009-10-24 15:45:06 +0200908 if (i != 0)
Dominik Brodowskieb141202010-03-07 12:21:16 +0100909 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200911 i = pcmcia_request_configuration(link, &link->conf);
Dominik Brodowski9ac3e582009-10-24 15:45:06 +0200912 if (i != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (bluecard_open(info) != 0)
916 goto failed;
917
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920failed:
921 bluecard_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200922 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200926static void bluecard_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 bluecard_info_t *info = link->priv;
929
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100930 bluecard_close(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 del_timer(&(info->timer));
933
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200934 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Dominik Brodowski7f70cb62005-06-27 16:28:35 -0700937static struct pcmcia_device_id bluecard_ids[] = {
938 PCMCIA_DEVICE_PROD_ID12("BlueCard", "LSE041", 0xbaf16fbf, 0x657cc15e),
939 PCMCIA_DEVICE_PROD_ID12("BTCFCARD", "LSE139", 0xe3987764, 0x2524b59c),
940 PCMCIA_DEVICE_PROD_ID12("WSS", "LSE039", 0x0a0736ec, 0x24e6dfab),
941 PCMCIA_DEVICE_NULL
942};
943MODULE_DEVICE_TABLE(pcmcia, bluecard_ids);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static struct pcmcia_driver bluecard_driver = {
946 .owner = THIS_MODULE,
947 .drv = {
948 .name = "bluecard_cs",
949 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200950 .probe = bluecard_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100951 .remove = bluecard_detach,
Dominik Brodowski7f70cb62005-06-27 16:28:35 -0700952 .id_table = bluecard_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953};
954
955static int __init init_bluecard_cs(void)
956{
957 return pcmcia_register_driver(&bluecard_driver);
958}
959
960
961static void __exit exit_bluecard_cs(void)
962{
963 pcmcia_unregister_driver(&bluecard_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966module_init(init_bluecard_cs);
967module_exit(exit_bluecard_cs);