bluecard_cs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:25k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  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. #include <linux/config.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <linux/sched.h>
  29. #include <linux/timer.h>
  30. #include <linux/errno.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/ioport.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/skbuff.h>
  35. #include <asm/io.h>
  36. #include <pcmcia/version.h>
  37. #include <pcmcia/cs_types.h>
  38. #include <pcmcia/cs.h>
  39. #include <pcmcia/cistpl.h>
  40. #include <pcmcia/ciscode.h>
  41. #include <pcmcia/ds.h>
  42. #include <pcmcia/cisreg.h>
  43. #include <net/bluetooth/bluetooth.h>
  44. #include <net/bluetooth/hci_core.h>
  45. /* ======================== Module parameters ======================== */
  46. /* Bit map of interrupts to choose from */
  47. static u_int irq_mask = 0x86bc;
  48. static int irq_list[4] = { -1 };
  49. MODULE_PARM(irq_mask, "i");
  50. MODULE_PARM(irq_list, "1-4i");
  51. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  52. MODULE_DESCRIPTION("BlueZ driver for the Anycom BlueCard (LSE039/LSE041)");
  53. MODULE_LICENSE("GPL");
  54. /* ======================== Local structures ======================== */
  55. typedef struct bluecard_info_t {
  56. dev_link_t link;
  57. dev_node_t node;
  58. struct hci_dev hdev;
  59. spinlock_t lock; /* For serializing operations */
  60. struct timer_list timer; /* For LED control */
  61. struct sk_buff_head txq;
  62. unsigned long tx_state;
  63. unsigned long rx_state;
  64. unsigned long rx_count;
  65. struct sk_buff *rx_skb;
  66. unsigned char ctrl_reg;
  67. unsigned long hw_state; /* Status of the hardware and LED control */
  68. } bluecard_info_t;
  69. void bluecard_config(dev_link_t *link);
  70. void bluecard_release(u_long arg);
  71. int bluecard_event(event_t event, int priority, event_callback_args_t *args);
  72. static dev_info_t dev_info = "bluecard_cs";
  73. dev_link_t *bluecard_attach(void);
  74. void bluecard_detach(dev_link_t *);
  75. static dev_link_t *dev_list = NULL;
  76. /* Default baud rate: 57600, 115200, 230400 or 460800 */
  77. #define DEFAULT_BAUD_RATE  230400
  78. /* Hardware states */
  79. #define CARD_READY             1
  80. #define CARD_HAS_PCCARD_ID     4
  81. #define CARD_HAS_POWER_LED     5
  82. #define CARD_HAS_ACTIVITY_LED  6
  83. /* Transmit states  */
  84. #define XMIT_SENDING         1
  85. #define XMIT_WAKEUP          2
  86. #define XMIT_BUFFER_NUMBER   5 /* unset = buffer one, set = buffer two */
  87. #define XMIT_BUF_ONE_READY   6
  88. #define XMIT_BUF_TWO_READY   7
  89. #define XMIT_SENDING_READY   8
  90. /* Receiver states */
  91. #define RECV_WAIT_PACKET_TYPE   0
  92. #define RECV_WAIT_EVENT_HEADER  1
  93. #define RECV_WAIT_ACL_HEADER    2
  94. #define RECV_WAIT_SCO_HEADER    3
  95. #define RECV_WAIT_DATA          4
  96. /* Special packet types */
  97. #define PKT_BAUD_RATE_57600   0x80
  98. #define PKT_BAUD_RATE_115200  0x81
  99. #define PKT_BAUD_RATE_230400  0x82
  100. #define PKT_BAUD_RATE_460800  0x83
  101. /* These are the register offsets */
  102. #define REG_COMMAND     0x20
  103. #define REG_INTERRUPT   0x21
  104. #define REG_CONTROL     0x22
  105. #define REG_RX_CONTROL  0x24
  106. #define REG_CARD_RESET  0x30
  107. #define REG_LED_CTRL    0x30
  108. /* REG_COMMAND */
  109. #define REG_COMMAND_TX_BUF_ONE  0x01
  110. #define REG_COMMAND_TX_BUF_TWO  0x02
  111. #define REG_COMMAND_RX_BUF_ONE  0x04
  112. #define REG_COMMAND_RX_BUF_TWO  0x08
  113. #define REG_COMMAND_RX_WIN_ONE  0x00
  114. #define REG_COMMAND_RX_WIN_TWO  0x10
  115. /* REG_CONTROL */
  116. #define REG_CONTROL_BAUD_RATE_57600   0x00
  117. #define REG_CONTROL_BAUD_RATE_115200  0x01
  118. #define REG_CONTROL_BAUD_RATE_230400  0x02
  119. #define REG_CONTROL_BAUD_RATE_460800  0x03
  120. #define REG_CONTROL_RTS               0x04
  121. #define REG_CONTROL_BT_ON             0x08
  122. #define REG_CONTROL_BT_RESET          0x10
  123. #define REG_CONTROL_BT_RES_PU         0x20
  124. #define REG_CONTROL_INTERRUPT         0x40
  125. #define REG_CONTROL_CARD_RESET        0x80
  126. /* REG_RX_CONTROL */
  127. #define RTS_LEVEL_SHIFT_BITS  0x02
  128. /* ======================== LED handling routines ======================== */
  129. void bluecard_activity_led_timeout(u_long arg)
  130. {
  131. bluecard_info_t *info = (bluecard_info_t *)arg;
  132. unsigned int iobase = info->link.io.BasePort1;
  133. if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
  134. /* Disable activity LED */
  135. outb(0x08 | 0x20, iobase + 0x30);
  136. } else {
  137. /* Disable power LED */
  138. outb(0x00, iobase + 0x30);
  139. }
  140. }
  141. static void bluecard_enable_activity_led(bluecard_info_t *info)
  142. {
  143. unsigned int iobase = info->link.io.BasePort1;
  144. if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
  145. /* Enable activity LED */
  146. outb(0x10 | 0x40, iobase + 0x30);
  147. /* Stop the LED after HZ/4 */
  148. mod_timer(&(info->timer), jiffies + HZ / 4);
  149. } else {
  150. /* Enable power LED */
  151. outb(0x08 | 0x20, iobase + 0x30);
  152. /* Stop the LED after HZ/2 */
  153. mod_timer(&(info->timer), jiffies + HZ / 2);
  154. }
  155. }
  156. /* ======================== Interrupt handling ======================== */
  157. static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
  158. {
  159. int i, actual;
  160. actual = (len > 15) ? 15 : len;
  161. outb_p(actual, iobase + offset);
  162. for (i = 0; i < actual; i++)
  163. outb_p(buf[i], iobase + offset + i + 1);
  164. return actual;
  165. }
  166. static void bluecard_write_wakeup(bluecard_info_t *info)
  167. {
  168. if (!info) {
  169. printk(KERN_WARNING "bluecard_cs: Call of write_wakeup for unknown device.n");
  170. return;
  171. }
  172. if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
  173. return;
  174. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  175. set_bit(XMIT_WAKEUP, &(info->tx_state));
  176. return;
  177. }
  178. do {
  179. register unsigned int iobase = info->link.io.BasePort1;
  180. register unsigned int offset;
  181. register unsigned char command;
  182. register unsigned long ready_bit;
  183. register struct sk_buff *skb;
  184. register int len;
  185. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  186. if (!(info->link.state & DEV_PRESENT))
  187. return;
  188. if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
  189. if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
  190. break;
  191. offset = 0x10;
  192. command = REG_COMMAND_TX_BUF_TWO;
  193. ready_bit = XMIT_BUF_TWO_READY;
  194. } else {
  195. if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
  196. break;
  197. offset = 0x00;
  198. command = REG_COMMAND_TX_BUF_ONE;
  199. ready_bit = XMIT_BUF_ONE_READY;
  200. }
  201. if (!(skb = skb_dequeue(&(info->txq))))
  202. break;
  203. if (skb->pkt_type & 0x80) {
  204. /* Disable RTS */
  205. info->ctrl_reg |= REG_CONTROL_RTS;
  206. outb(info->ctrl_reg, iobase + REG_CONTROL);
  207. }
  208. /* Activate LED */
  209. bluecard_enable_activity_led(info);
  210. /* Send frame */
  211. len = bluecard_write(iobase, offset, skb->data, skb->len);
  212. /* Tell the FPGA to send the data */
  213. outb_p(command, iobase + REG_COMMAND);
  214. /* Mark the buffer as dirty */
  215. clear_bit(ready_bit, &(info->tx_state));
  216. if (skb->pkt_type & 0x80) {
  217. wait_queue_head_t wait;
  218. unsigned char baud_reg;
  219. switch (skb->pkt_type) {
  220. case PKT_BAUD_RATE_460800:
  221. baud_reg = REG_CONTROL_BAUD_RATE_460800;
  222. break;
  223. case PKT_BAUD_RATE_230400:
  224. baud_reg = REG_CONTROL_BAUD_RATE_230400;
  225. break;
  226. case PKT_BAUD_RATE_115200:
  227. baud_reg = REG_CONTROL_BAUD_RATE_115200;
  228. break;
  229. case PKT_BAUD_RATE_57600:
  230. /* Fall through... */
  231. default:
  232. baud_reg = REG_CONTROL_BAUD_RATE_57600;
  233. break;
  234. }
  235. /* Wait until the command reaches the baseband */
  236. init_waitqueue_head(&wait);
  237. interruptible_sleep_on_timeout(&wait, HZ / 10);
  238. /* Set baud on baseband */
  239. info->ctrl_reg &= ~0x03;
  240. info->ctrl_reg |= baud_reg;
  241. outb(info->ctrl_reg, iobase + REG_CONTROL);
  242. /* Enable RTS */
  243. info->ctrl_reg &= ~REG_CONTROL_RTS;
  244. outb(info->ctrl_reg, iobase + REG_CONTROL);
  245. /* Wait before the next HCI packet can be send */
  246. interruptible_sleep_on_timeout(&wait, HZ);
  247. }
  248. if (len == skb->len) {
  249. kfree_skb(skb);
  250. } else {
  251. skb_pull(skb, len);
  252. skb_queue_head(&(info->txq), skb);
  253. }
  254. info->hdev.stat.byte_tx += len;
  255. /* Change buffer */
  256. change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
  257. } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
  258. clear_bit(XMIT_SENDING, &(info->tx_state));
  259. }
  260. static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
  261. {
  262. int i, n, len;
  263. outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
  264. len = inb(iobase + offset);
  265. n = 0;
  266. i = 1;
  267. while (n < len) {
  268. if (i == 16) {
  269. outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
  270. i = 0;
  271. }
  272. buf[n] = inb(iobase + offset + i);
  273. n++;
  274. i++;
  275. }
  276. return len;
  277. }
  278. static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
  279. {
  280. unsigned int iobase;
  281. unsigned char buf[31];
  282. int i, len;
  283. if (!info) {
  284. printk(KERN_WARNING "bluecard_cs: Call of receive for unknown device.n");
  285. return;
  286. }
  287. iobase = info->link.io.BasePort1;
  288. if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
  289. bluecard_enable_activity_led(info);
  290. len = bluecard_read(iobase, offset, buf, sizeof(buf));
  291. for (i = 0; i < len; i++) {
  292. /* Allocate packet */
  293. if (info->rx_skb == NULL) {
  294. info->rx_state = RECV_WAIT_PACKET_TYPE;
  295. info->rx_count = 0;
  296. if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  297. printk(KERN_WARNING "bluecard_cs: Can't allocate mem for new packet.n");
  298. return;
  299. }
  300. }
  301. if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
  302. info->rx_skb->dev = (void *)&(info->hdev);
  303. info->rx_skb->pkt_type = buf[i];
  304. switch (info->rx_skb->pkt_type) {
  305. case 0x00:
  306. /* init packet */
  307. if (offset != 0x00) {
  308. set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
  309. set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
  310. set_bit(XMIT_SENDING_READY, &(info->tx_state));
  311. bluecard_write_wakeup(info);
  312. }
  313. kfree_skb(info->rx_skb);
  314. info->rx_skb = NULL;
  315. break;
  316. case HCI_EVENT_PKT:
  317. info->rx_state = RECV_WAIT_EVENT_HEADER;
  318. info->rx_count = HCI_EVENT_HDR_SIZE;
  319. break;
  320. case HCI_ACLDATA_PKT:
  321. info->rx_state = RECV_WAIT_ACL_HEADER;
  322. info->rx_count = HCI_ACL_HDR_SIZE;
  323. break;
  324. case HCI_SCODATA_PKT:
  325. info->rx_state = RECV_WAIT_SCO_HEADER;
  326. info->rx_count = HCI_SCO_HDR_SIZE;
  327. break;
  328. default:
  329. /* unknown packet */
  330. printk(KERN_WARNING "bluecard_cs: Unknown HCI packet with type 0x%02x received.n", info->rx_skb->pkt_type);
  331. info->hdev.stat.err_rx++;
  332. kfree_skb(info->rx_skb);
  333. info->rx_skb = NULL;
  334. break;
  335. }
  336. } else {
  337. *skb_put(info->rx_skb, 1) = buf[i];
  338. info->rx_count--;
  339. if (info->rx_count == 0) {
  340. int dlen;
  341. hci_event_hdr *eh;
  342. hci_acl_hdr *ah;
  343. hci_sco_hdr *sh;
  344. switch (info->rx_state) {
  345. case RECV_WAIT_EVENT_HEADER:
  346. eh = (hci_event_hdr *)(info->rx_skb->data);
  347. info->rx_state = RECV_WAIT_DATA;
  348. info->rx_count = eh->plen;
  349. break;
  350. case RECV_WAIT_ACL_HEADER:
  351. ah = (hci_acl_hdr *)(info->rx_skb->data);
  352. dlen = __le16_to_cpu(ah->dlen);
  353. info->rx_state = RECV_WAIT_DATA;
  354. info->rx_count = dlen;
  355. break;
  356. case RECV_WAIT_SCO_HEADER:
  357. sh = (hci_sco_hdr *)(info->rx_skb->data);
  358. info->rx_state = RECV_WAIT_DATA;
  359. info->rx_count = sh->dlen;
  360. break;
  361. case RECV_WAIT_DATA:
  362. hci_recv_frame(info->rx_skb);
  363. info->rx_skb = NULL;
  364. break;
  365. }
  366. }
  367. }
  368. }
  369. info->hdev.stat.byte_rx += len;
  370. }
  371. void bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
  372. {
  373. bluecard_info_t *info = dev_inst;
  374. unsigned int iobase;
  375. unsigned char reg;
  376. if (!info) {
  377. printk(KERN_WARNING "bluecard_cs: Call of irq %d for unknown device.n", irq);
  378. return;
  379. }
  380. if (!test_bit(CARD_READY, &(info->hw_state)))
  381. return;
  382. iobase = info->link.io.BasePort1;
  383. spin_lock(&(info->lock));
  384. /* Disable interrupt */
  385. info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
  386. outb(info->ctrl_reg, iobase + REG_CONTROL);
  387. reg = inb(iobase + REG_INTERRUPT);
  388. if ((reg != 0x00) && (reg != 0xff)) {
  389. if (reg & 0x04) {
  390. bluecard_receive(info, 0x00);
  391. outb(0x04, iobase + REG_INTERRUPT);
  392. outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
  393. }
  394. if (reg & 0x08) {
  395. bluecard_receive(info, 0x10);
  396. outb(0x08, iobase + REG_INTERRUPT);
  397. outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
  398. }
  399. if (reg & 0x01) {
  400. set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
  401. outb(0x01, iobase + REG_INTERRUPT);
  402. bluecard_write_wakeup(info);
  403. }
  404. if (reg & 0x02) {
  405. set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
  406. outb(0x02, iobase + REG_INTERRUPT);
  407. bluecard_write_wakeup(info);
  408. }
  409. }
  410. /* Enable interrupt */
  411. info->ctrl_reg |= REG_CONTROL_INTERRUPT;
  412. outb(info->ctrl_reg, iobase + REG_CONTROL);
  413. spin_unlock(&(info->lock));
  414. }
  415. /* ======================== Device specific HCI commands ======================== */
  416. static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
  417. {
  418. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  419. struct sk_buff *skb;
  420. /* Ericsson baud rate command */
  421. unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
  422. if (!(skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  423. printk(KERN_WARNING "bluecard_cs: Can't allocate mem for new packet.n");
  424. return -1;
  425. }
  426. switch (baud) {
  427. case 460800:
  428. cmd[4] = 0x00;
  429. skb->pkt_type = PKT_BAUD_RATE_460800;
  430. break;
  431. case 230400:
  432. cmd[4] = 0x01;
  433. skb->pkt_type = PKT_BAUD_RATE_230400;
  434. break;
  435. case 115200:
  436. cmd[4] = 0x02;
  437. skb->pkt_type = PKT_BAUD_RATE_115200;
  438. break;
  439. case 57600:
  440. /* Fall through... */
  441. default:
  442. cmd[4] = 0x03;
  443. skb->pkt_type = PKT_BAUD_RATE_57600;
  444. break;
  445. }
  446. memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
  447. skb_queue_tail(&(info->txq), skb);
  448. bluecard_write_wakeup(info);
  449. return 0;
  450. }
  451. /* ======================== HCI interface ======================== */
  452. static int bluecard_hci_flush(struct hci_dev *hdev)
  453. {
  454. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  455. /* Drop TX queue */
  456. skb_queue_purge(&(info->txq));
  457. return 0;
  458. }
  459. static int bluecard_hci_open(struct hci_dev *hdev)
  460. {
  461. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  462. unsigned int iobase = info->link.io.BasePort1;
  463. bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
  464. if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
  465. return 0;
  466. /* Enable LED */
  467. outb(0x08 | 0x20, iobase + 0x30);
  468. return 0;
  469. }
  470. static int bluecard_hci_close(struct hci_dev *hdev)
  471. {
  472. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  473. unsigned int iobase = info->link.io.BasePort1;
  474. if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
  475. return 0;
  476. bluecard_hci_flush(hdev);
  477. /* Disable LED */
  478. outb(0x00, iobase + 0x30);
  479. return 0;
  480. }
  481. static int bluecard_hci_send_frame(struct sk_buff *skb)
  482. {
  483. bluecard_info_t *info;
  484. struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
  485. if (!hdev) {
  486. printk(KERN_WARNING "bluecard_cs: Frame for unknown HCI device (hdev=NULL).");
  487. return -ENODEV;
  488. }
  489. info = (bluecard_info_t *)(hdev->driver_data);
  490. switch (skb->pkt_type) {
  491. case HCI_COMMAND_PKT:
  492. hdev->stat.cmd_tx++;
  493. break;
  494. case HCI_ACLDATA_PKT:
  495. hdev->stat.acl_tx++;
  496. break;
  497. case HCI_SCODATA_PKT:
  498. hdev->stat.sco_tx++;
  499. break;
  500. };
  501. /* Prepend skb with frame type */
  502. memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
  503. skb_queue_tail(&(info->txq), skb);
  504. bluecard_write_wakeup(info);
  505. return 0;
  506. }
  507. static void bluecard_hci_destruct(struct hci_dev *hdev)
  508. {
  509. }
  510. static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
  511. {
  512. return -ENOIOCTLCMD;
  513. }
  514. /* ======================== Card services HCI interaction ======================== */
  515. int bluecard_open(bluecard_info_t *info)
  516. {
  517. unsigned int iobase = info->link.io.BasePort1;
  518. struct hci_dev *hdev;
  519. unsigned char id;
  520. spin_lock_init(&(info->lock));
  521. init_timer(&(info->timer));
  522. info->timer.function = &bluecard_activity_led_timeout;
  523. info->timer.data = (u_long)info;
  524. skb_queue_head_init(&(info->txq));
  525. info->rx_state = RECV_WAIT_PACKET_TYPE;
  526. info->rx_count = 0;
  527. info->rx_skb = NULL;
  528. id = inb(iobase + 0x30);
  529. if ((id & 0x0f) == 0x02)
  530. set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
  531. if (id & 0x10)
  532. set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
  533. if (id & 0x20)
  534. set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
  535. /* Reset card */
  536. info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
  537. outb(info->ctrl_reg, iobase + REG_CONTROL);
  538. /* Turn FPGA off */
  539. outb(0x80, iobase + 0x30);
  540. /* Wait some time */
  541. set_current_state(TASK_INTERRUPTIBLE);
  542. schedule_timeout(HZ / 100);
  543. /* Turn FPGA on */
  544. outb(0x00, iobase + 0x30);
  545. /* Activate card */
  546. info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
  547. outb(info->ctrl_reg, iobase + REG_CONTROL);
  548. /* Enable interrupt */
  549. outb(0xff, iobase + REG_INTERRUPT);
  550. info->ctrl_reg |= REG_CONTROL_INTERRUPT;
  551. outb(info->ctrl_reg, iobase + REG_CONTROL);
  552. /* Start the RX buffers */
  553. outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
  554. outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
  555. /* Signal that the hardware is ready */
  556. set_bit(CARD_READY, &(info->hw_state));
  557. /* Drop TX queue */
  558. skb_queue_purge(&(info->txq));
  559. /* Control the point at which RTS is enabled */
  560. outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
  561. /* Timeout before it is safe to send the first HCI packet */
  562. set_current_state(TASK_INTERRUPTIBLE);
  563. schedule_timeout((HZ * 5) / 4); // or set it to 3/2
  564. /* Initialize and register HCI device */
  565. hdev = &(info->hdev);
  566. hdev->type = HCI_PCCARD;
  567. hdev->driver_data = info;
  568. hdev->open = bluecard_hci_open;
  569. hdev->close = bluecard_hci_close;
  570. hdev->flush = bluecard_hci_flush;
  571. hdev->send = bluecard_hci_send_frame;
  572. hdev->destruct = bluecard_hci_destruct;
  573. hdev->ioctl = bluecard_hci_ioctl;
  574. if (hci_register_dev(hdev) < 0) {
  575. printk(KERN_WARNING "bluecard_cs: Can't register HCI device %s.n", hdev->name);
  576. return -ENODEV;
  577. }
  578. return 0;
  579. }
  580. int bluecard_close(bluecard_info_t *info)
  581. {
  582. unsigned int iobase = info->link.io.BasePort1;
  583. struct hci_dev *hdev = &(info->hdev);
  584. bluecard_hci_close(hdev);
  585. clear_bit(CARD_READY, &(info->hw_state));
  586. /* Reset card */
  587. info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
  588. outb(info->ctrl_reg, iobase + REG_CONTROL);
  589. /* Turn FPGA off */
  590. outb(0x80, iobase + 0x30);
  591. if (hci_unregister_dev(hdev) < 0)
  592. printk(KERN_WARNING "bluecard_cs: Can't unregister HCI device %s.n", hdev->name);
  593. return 0;
  594. }
  595. /* ======================== Card services ======================== */
  596. static void cs_error(client_handle_t handle, int func, int ret)
  597. {
  598. error_info_t err = { func, ret };
  599. CardServices(ReportError, handle, &err);
  600. }
  601. dev_link_t *bluecard_attach(void)
  602. {
  603. bluecard_info_t *info;
  604. client_reg_t client_reg;
  605. dev_link_t *link;
  606. int i, ret;
  607. /* Create new info device */
  608. info = kmalloc(sizeof(*info), GFP_KERNEL);
  609. if (!info)
  610. return NULL;
  611. memset(info, 0, sizeof(*info));
  612. link = &info->link;
  613. link->priv = info;
  614. link->release.function = &bluecard_release;
  615. link->release.data = (u_long)link;
  616. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  617. link->io.NumPorts1 = 8;
  618. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  619. link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
  620. if (irq_list[0] == -1)
  621. link->irq.IRQInfo2 = irq_mask;
  622. else
  623. for (i = 0; i < 4; i++)
  624. link->irq.IRQInfo2 |= 1 << irq_list[i];
  625. link->irq.Handler = bluecard_interrupt;
  626. link->irq.Instance = info;
  627. link->conf.Attributes = CONF_ENABLE_IRQ;
  628. link->conf.Vcc = 50;
  629. link->conf.IntType = INT_MEMORY_AND_IO;
  630. /* Register with Card Services */
  631. link->next = dev_list;
  632. dev_list = link;
  633. client_reg.dev_info = &dev_info;
  634. client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
  635. client_reg.EventMask =
  636. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  637. CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  638. CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  639. client_reg.event_handler = &bluecard_event;
  640. client_reg.Version = 0x0210;
  641. client_reg.event_callback_args.client_data = link;
  642. ret = CardServices(RegisterClient, &link->handle, &client_reg);
  643. if (ret != CS_SUCCESS) {
  644. cs_error(link->handle, RegisterClient, ret);
  645. bluecard_detach(link);
  646. return NULL;
  647. }
  648. return link;
  649. }
  650. void bluecard_detach(dev_link_t *link)
  651. {
  652. bluecard_info_t *info = link->priv;
  653. dev_link_t **linkp;
  654. int ret;
  655. /* Locate device structure */
  656. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  657. if (*linkp == link)
  658. break;
  659. if (*linkp == NULL)
  660. return;
  661. del_timer(&link->release);
  662. if (link->state & DEV_CONFIG)
  663. bluecard_release((u_long)link);
  664. if (link->handle) {
  665. ret = CardServices(DeregisterClient, link->handle);
  666. if (ret != CS_SUCCESS)
  667. cs_error(link->handle, DeregisterClient, ret);
  668. }
  669. /* Unlink device structure, free bits */
  670. *linkp = link->next;
  671. kfree(info);
  672. }
  673. static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
  674. {
  675. int i;
  676. i = CardServices(fn, handle, tuple);
  677. if (i != CS_SUCCESS)
  678. return CS_NO_MORE_ITEMS;
  679. i = CardServices(GetTupleData, handle, tuple);
  680. if (i != CS_SUCCESS)
  681. return i;
  682. return CardServices(ParseTuple, handle, tuple, parse);
  683. }
  684. #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
  685. #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
  686. void bluecard_config(dev_link_t *link)
  687. {
  688. client_handle_t handle = link->handle;
  689. bluecard_info_t *info = link->priv;
  690. tuple_t tuple;
  691. u_short buf[256];
  692. cisparse_t parse;
  693. config_info_t config;
  694. int i, n, last_ret, last_fn;
  695. tuple.TupleData = (cisdata_t *)buf;
  696. tuple.TupleOffset = 0;
  697. tuple.TupleDataMax = 255;
  698. tuple.Attributes = 0;
  699. /* Get configuration register information */
  700. tuple.DesiredTuple = CISTPL_CONFIG;
  701. last_ret = first_tuple(handle, &tuple, &parse);
  702. if (last_ret != CS_SUCCESS) {
  703. last_fn = ParseTuple;
  704. goto cs_failed;
  705. }
  706. link->conf.ConfigBase = parse.config.base;
  707. link->conf.Present = parse.config.rmask[0];
  708. /* Configure card */
  709. link->state |= DEV_CONFIG;
  710. i = CardServices(GetConfigurationInfo, handle, &config);
  711. link->conf.Vcc = config.Vcc;
  712. link->conf.ConfigIndex = 0x20;
  713. link->io.NumPorts1 = 64;
  714. link->io.IOAddrLines = 6;
  715. for (n = 0; n < 0x400; n += 0x40) {
  716. link->io.BasePort1 = n ^ 0x300;
  717. i = CardServices(RequestIO, link->handle, &link->io);
  718. if (i == CS_SUCCESS)
  719. break;
  720. }
  721. if (i != CS_SUCCESS) {
  722. cs_error(link->handle, RequestIO, i);
  723. goto failed;
  724. }
  725. i = CardServices(RequestIRQ, link->handle, &link->irq);
  726. if (i != CS_SUCCESS) {
  727. cs_error(link->handle, RequestIRQ, i);
  728. link->irq.AssignedIRQ = 0;
  729. }
  730. i = CardServices(RequestConfiguration, link->handle, &link->conf);
  731. if (i != CS_SUCCESS) {
  732. cs_error(link->handle, RequestConfiguration, i);
  733. goto failed;
  734. }
  735. MOD_INC_USE_COUNT;
  736. if (bluecard_open(info) != 0)
  737. goto failed;
  738. strcpy(info->node.dev_name, info->hdev.name);
  739. link->dev = &info->node;
  740. link->state &= ~DEV_CONFIG_PENDING;
  741. return;
  742. cs_failed:
  743. cs_error(link->handle, last_fn, last_ret);
  744. failed:
  745. bluecard_release((u_long)link);
  746. }
  747. void bluecard_release(u_long arg)
  748. {
  749. dev_link_t *link = (dev_link_t *)arg;
  750. bluecard_info_t *info = link->priv;
  751. if (link->state & DEV_PRESENT)
  752. bluecard_close(info);
  753. MOD_DEC_USE_COUNT;
  754. link->dev = NULL;
  755. CardServices(ReleaseConfiguration, link->handle);
  756. CardServices(ReleaseIO, link->handle, &link->io);
  757. CardServices(ReleaseIRQ, link->handle, &link->irq);
  758. link->state &= ~DEV_CONFIG;
  759. }
  760. int bluecard_event(event_t event, int priority, event_callback_args_t *args)
  761. {
  762. dev_link_t *link = args->client_data;
  763. bluecard_info_t *info = link->priv;
  764. switch (event) {
  765. case CS_EVENT_CARD_REMOVAL:
  766. link->state &= ~DEV_PRESENT;
  767. if (link->state & DEV_CONFIG) {
  768. bluecard_close(info);
  769. mod_timer(&link->release, jiffies + HZ / 20);
  770. }
  771. break;
  772. case CS_EVENT_CARD_INSERTION:
  773. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  774. bluecard_config(link);
  775. break;
  776. case CS_EVENT_PM_SUSPEND:
  777. link->state |= DEV_SUSPEND;
  778. /* Fall through... */
  779. case CS_EVENT_RESET_PHYSICAL:
  780. if (link->state & DEV_CONFIG)
  781. CardServices(ReleaseConfiguration, link->handle);
  782. break;
  783. case CS_EVENT_PM_RESUME:
  784. link->state &= ~DEV_SUSPEND;
  785. /* Fall through... */
  786. case CS_EVENT_CARD_RESET:
  787. if (DEV_OK(link))
  788. CardServices(RequestConfiguration, link->handle, &link->conf);
  789. break;
  790. }
  791. return 0;
  792. }
  793. /* ======================== Module initialization ======================== */
  794. int __init init_bluecard_cs(void)
  795. {
  796. servinfo_t serv;
  797. int err;
  798. CardServices(GetCardServicesInfo, &serv);
  799. if (serv.Revision != CS_RELEASE_CODE) {
  800. printk(KERN_NOTICE "bluecard_cs: Card Services release does not match!n");
  801. return -1;
  802. }
  803. err = register_pccard_driver(&dev_info, &bluecard_attach, &bluecard_detach);
  804. return err;
  805. }
  806. void __exit exit_bluecard_cs(void)
  807. {
  808. unregister_pccard_driver(&dev_info);
  809. while (dev_list != NULL)
  810. bluecard_detach(dev_list);
  811. }
  812. module_init(init_bluecard_cs);
  813. module_exit(exit_bluecard_cs);
  814. EXPORT_NO_SYMBOLS;