bluecard_cs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.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/moduleparam.h>
  35. #include <linux/wait.h>
  36. #include <linux/skbuff.h>
  37. #include <asm/io.h>
  38. #include <pcmcia/cs_types.h>
  39. #include <pcmcia/cs.h>
  40. #include <pcmcia/cistpl.h>
  41. #include <pcmcia/ciscode.h>
  42. #include <pcmcia/ds.h>
  43. #include <pcmcia/cisreg.h>
  44. #include <net/bluetooth/bluetooth.h>
  45. #include <net/bluetooth/hci_core.h>
  46. /* ======================== Module parameters ======================== */
  47. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  48. MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
  49. MODULE_LICENSE("GPL");
  50. /* ======================== Local structures ======================== */
  51. typedef struct bluecard_info_t {
  52. struct pcmcia_device *p_dev;
  53. dev_node_t node;
  54. struct hci_dev *hdev;
  55. spinlock_t lock; /* For serializing operations */
  56. struct timer_list timer; /* For LED control */
  57. struct sk_buff_head txq;
  58. unsigned long tx_state;
  59. unsigned long rx_state;
  60. unsigned long rx_count;
  61. struct sk_buff *rx_skb;
  62. unsigned char ctrl_reg;
  63. unsigned long hw_state; /* Status of the hardware and LED control */
  64. } bluecard_info_t;
  65. static int bluecard_config(struct pcmcia_device *link);
  66. static void bluecard_release(struct pcmcia_device *link);
  67. static void bluecard_detach(struct pcmcia_device *p_dev);
  68. /* Default baud rate: 57600, 115200, 230400 or 460800 */
  69. #define DEFAULT_BAUD_RATE 230400
  70. /* Hardware states */
  71. #define CARD_READY 1
  72. #define CARD_HAS_PCCARD_ID 4
  73. #define CARD_HAS_POWER_LED 5
  74. #define CARD_HAS_ACTIVITY_LED 6
  75. /* Transmit states */
  76. #define XMIT_SENDING 1
  77. #define XMIT_WAKEUP 2
  78. #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
  79. #define XMIT_BUF_ONE_READY 6
  80. #define XMIT_BUF_TWO_READY 7
  81. #define XMIT_SENDING_READY 8
  82. /* Receiver states */
  83. #define RECV_WAIT_PACKET_TYPE 0
  84. #define RECV_WAIT_EVENT_HEADER 1
  85. #define RECV_WAIT_ACL_HEADER 2
  86. #define RECV_WAIT_SCO_HEADER 3
  87. #define RECV_WAIT_DATA 4
  88. /* Special packet types */
  89. #define PKT_BAUD_RATE_57600 0x80
  90. #define PKT_BAUD_RATE_115200 0x81
  91. #define PKT_BAUD_RATE_230400 0x82
  92. #define PKT_BAUD_RATE_460800 0x83
  93. /* These are the register offsets */
  94. #define REG_COMMAND 0x20
  95. #define REG_INTERRUPT 0x21
  96. #define REG_CONTROL 0x22
  97. #define REG_RX_CONTROL 0x24
  98. #define REG_CARD_RESET 0x30
  99. #define REG_LED_CTRL 0x30
  100. /* REG_COMMAND */
  101. #define REG_COMMAND_TX_BUF_ONE 0x01
  102. #define REG_COMMAND_TX_BUF_TWO 0x02
  103. #define REG_COMMAND_RX_BUF_ONE 0x04
  104. #define REG_COMMAND_RX_BUF_TWO 0x08
  105. #define REG_COMMAND_RX_WIN_ONE 0x00
  106. #define REG_COMMAND_RX_WIN_TWO 0x10
  107. /* REG_CONTROL */
  108. #define REG_CONTROL_BAUD_RATE_57600 0x00
  109. #define REG_CONTROL_BAUD_RATE_115200 0x01
  110. #define REG_CONTROL_BAUD_RATE_230400 0x02
  111. #define REG_CONTROL_BAUD_RATE_460800 0x03
  112. #define REG_CONTROL_RTS 0x04
  113. #define REG_CONTROL_BT_ON 0x08
  114. #define REG_CONTROL_BT_RESET 0x10
  115. #define REG_CONTROL_BT_RES_PU 0x20
  116. #define REG_CONTROL_INTERRUPT 0x40
  117. #define REG_CONTROL_CARD_RESET 0x80
  118. /* REG_RX_CONTROL */
  119. #define RTS_LEVEL_SHIFT_BITS 0x02
  120. /* ======================== LED handling routines ======================== */
  121. static void bluecard_activity_led_timeout(u_long arg)
  122. {
  123. bluecard_info_t *info = (bluecard_info_t *)arg;
  124. unsigned int iobase = info->p_dev->io.BasePort1;
  125. if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
  126. return;
  127. if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
  128. /* Disable activity LED */
  129. outb(0x08 | 0x20, iobase + 0x30);
  130. } else {
  131. /* Disable power LED */
  132. outb(0x00, iobase + 0x30);
  133. }
  134. }
  135. static void bluecard_enable_activity_led(bluecard_info_t *info)
  136. {
  137. unsigned int iobase = info->p_dev->io.BasePort1;
  138. if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
  139. return;
  140. if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
  141. /* Enable activity LED */
  142. outb(0x10 | 0x40, iobase + 0x30);
  143. /* Stop the LED after HZ/4 */
  144. mod_timer(&(info->timer), jiffies + HZ / 4);
  145. } else {
  146. /* Enable power LED */
  147. outb(0x08 | 0x20, iobase + 0x30);
  148. /* Stop the LED after HZ/2 */
  149. mod_timer(&(info->timer), jiffies + HZ / 2);
  150. }
  151. }
  152. /* ======================== Interrupt handling ======================== */
  153. static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
  154. {
  155. int i, actual;
  156. actual = (len > 15) ? 15 : len;
  157. outb_p(actual, iobase + offset);
  158. for (i = 0; i < actual; i++)
  159. outb_p(buf[i], iobase + offset + i + 1);
  160. return actual;
  161. }
  162. static void bluecard_write_wakeup(bluecard_info_t *info)
  163. {
  164. if (!info) {
  165. BT_ERR("Unknown device");
  166. return;
  167. }
  168. if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
  169. return;
  170. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  171. set_bit(XMIT_WAKEUP, &(info->tx_state));
  172. return;
  173. }
  174. do {
  175. register unsigned int iobase = info->p_dev->io.BasePort1;
  176. register unsigned int offset;
  177. register unsigned char command;
  178. register unsigned long ready_bit;
  179. register struct sk_buff *skb;
  180. register int len;
  181. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  182. if (!pcmcia_dev_present(info->p_dev))
  183. return;
  184. if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
  185. if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
  186. break;
  187. offset = 0x10;
  188. command = REG_COMMAND_TX_BUF_TWO;
  189. ready_bit = XMIT_BUF_TWO_READY;
  190. } else {
  191. if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
  192. break;
  193. offset = 0x00;
  194. command = REG_COMMAND_TX_BUF_ONE;
  195. ready_bit = XMIT_BUF_ONE_READY;
  196. }
  197. if (!(skb = skb_dequeue(&(info->txq))))
  198. break;
  199. if (bt_cb(skb)->pkt_type & 0x80) {
  200. /* Disable RTS */
  201. info->ctrl_reg |= REG_CONTROL_RTS;
  202. outb(info->ctrl_reg, iobase + REG_CONTROL);
  203. }
  204. /* Activate LED */
  205. bluecard_enable_activity_led(info);
  206. /* Send frame */
  207. len = bluecard_write(iobase, offset, skb->data, skb->len);
  208. /* Tell the FPGA to send the data */
  209. outb_p(command, iobase + REG_COMMAND);
  210. /* Mark the buffer as dirty */
  211. clear_bit(ready_bit, &(info->tx_state));
  212. if (bt_cb(skb)->pkt_type & 0x80) {
  213. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  214. DEFINE_WAIT(wait);
  215. unsigned char baud_reg;
  216. switch (bt_cb(skb)->pkt_type) {
  217. case PKT_BAUD_RATE_460800:
  218. baud_reg = REG_CONTROL_BAUD_RATE_460800;
  219. break;
  220. case PKT_BAUD_RATE_230400:
  221. baud_reg = REG_CONTROL_BAUD_RATE_230400;
  222. break;
  223. case PKT_BAUD_RATE_115200:
  224. baud_reg = REG_CONTROL_BAUD_RATE_115200;
  225. break;
  226. case PKT_BAUD_RATE_57600:
  227. /* Fall through... */
  228. default:
  229. baud_reg = REG_CONTROL_BAUD_RATE_57600;
  230. break;
  231. }
  232. /* Wait until the command reaches the baseband */
  233. prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
  234. schedule_timeout(HZ/10);
  235. finish_wait(&wq, &wait);
  236. /* Set baud on baseband */
  237. info->ctrl_reg &= ~0x03;
  238. info->ctrl_reg |= baud_reg;
  239. outb(info->ctrl_reg, iobase + REG_CONTROL);
  240. /* Enable RTS */
  241. info->ctrl_reg &= ~REG_CONTROL_RTS;
  242. outb(info->ctrl_reg, iobase + REG_CONTROL);
  243. /* Wait before the next HCI packet can be send */
  244. prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
  245. schedule_timeout(HZ);
  246. finish_wait(&wq, &wait);
  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. BT_ERR("Unknown device");
  285. return;
  286. }
  287. iobase = info->p_dev->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 = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  297. BT_ERR("Can't allocate mem for new packet");
  298. return;
  299. }
  300. }
  301. if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
  302. info->rx_skb->dev = (void *) info->hdev;
  303. bt_cb(info->rx_skb)->pkt_type = buf[i];
  304. switch (bt_cb(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. BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(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. struct hci_event_hdr *eh;
  342. struct hci_acl_hdr *ah;
  343. struct hci_sco_hdr *sh;
  344. switch (info->rx_state) {
  345. case RECV_WAIT_EVENT_HEADER:
  346. eh = (struct 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 = (struct 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 = (struct 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. static irqreturn_t bluecard_interrupt(int irq, void *dev_inst)
  372. {
  373. bluecard_info_t *info = dev_inst;
  374. unsigned int iobase;
  375. unsigned char reg;
  376. if (!info || !info->hdev) {
  377. BT_ERR("Call of irq %d for unknown device", irq);
  378. return IRQ_NONE;
  379. }
  380. if (!test_bit(CARD_READY, &(info->hw_state)))
  381. return IRQ_HANDLED;
  382. iobase = info->p_dev->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. return IRQ_HANDLED;
  415. }
  416. /* ======================== Device specific HCI commands ======================== */
  417. static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
  418. {
  419. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  420. struct sk_buff *skb;
  421. /* Ericsson baud rate command */
  422. unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
  423. if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  424. BT_ERR("Can't allocate mem for new packet");
  425. return -1;
  426. }
  427. switch (baud) {
  428. case 460800:
  429. cmd[4] = 0x00;
  430. bt_cb(skb)->pkt_type = PKT_BAUD_RATE_460800;
  431. break;
  432. case 230400:
  433. cmd[4] = 0x01;
  434. bt_cb(skb)->pkt_type = PKT_BAUD_RATE_230400;
  435. break;
  436. case 115200:
  437. cmd[4] = 0x02;
  438. bt_cb(skb)->pkt_type = PKT_BAUD_RATE_115200;
  439. break;
  440. case 57600:
  441. /* Fall through... */
  442. default:
  443. cmd[4] = 0x03;
  444. bt_cb(skb)->pkt_type = PKT_BAUD_RATE_57600;
  445. break;
  446. }
  447. memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
  448. skb_queue_tail(&(info->txq), skb);
  449. bluecard_write_wakeup(info);
  450. return 0;
  451. }
  452. /* ======================== HCI interface ======================== */
  453. static int bluecard_hci_flush(struct hci_dev *hdev)
  454. {
  455. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  456. /* Drop TX queue */
  457. skb_queue_purge(&(info->txq));
  458. return 0;
  459. }
  460. static int bluecard_hci_open(struct hci_dev *hdev)
  461. {
  462. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  463. unsigned int iobase = info->p_dev->io.BasePort1;
  464. if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
  465. bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
  466. if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
  467. return 0;
  468. if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
  469. /* Enable LED */
  470. outb(0x08 | 0x20, iobase + 0x30);
  471. }
  472. return 0;
  473. }
  474. static int bluecard_hci_close(struct hci_dev *hdev)
  475. {
  476. bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
  477. unsigned int iobase = info->p_dev->io.BasePort1;
  478. if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
  479. return 0;
  480. bluecard_hci_flush(hdev);
  481. if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
  482. /* Disable LED */
  483. outb(0x00, iobase + 0x30);
  484. }
  485. return 0;
  486. }
  487. static int bluecard_hci_send_frame(struct sk_buff *skb)
  488. {
  489. bluecard_info_t *info;
  490. struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
  491. if (!hdev) {
  492. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  493. return -ENODEV;
  494. }
  495. info = (bluecard_info_t *)(hdev->driver_data);
  496. switch (bt_cb(skb)->pkt_type) {
  497. case HCI_COMMAND_PKT:
  498. hdev->stat.cmd_tx++;
  499. break;
  500. case HCI_ACLDATA_PKT:
  501. hdev->stat.acl_tx++;
  502. break;
  503. case HCI_SCODATA_PKT:
  504. hdev->stat.sco_tx++;
  505. break;
  506. };
  507. /* Prepend skb with frame type */
  508. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  509. skb_queue_tail(&(info->txq), skb);
  510. bluecard_write_wakeup(info);
  511. return 0;
  512. }
  513. static void bluecard_hci_destruct(struct hci_dev *hdev)
  514. {
  515. }
  516. static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
  517. {
  518. return -ENOIOCTLCMD;
  519. }
  520. /* ======================== Card services HCI interaction ======================== */
  521. static int bluecard_open(bluecard_info_t *info)
  522. {
  523. unsigned int iobase = info->p_dev->io.BasePort1;
  524. struct hci_dev *hdev;
  525. unsigned char id;
  526. spin_lock_init(&(info->lock));
  527. init_timer(&(info->timer));
  528. info->timer.function = &bluecard_activity_led_timeout;
  529. info->timer.data = (u_long)info;
  530. skb_queue_head_init(&(info->txq));
  531. info->rx_state = RECV_WAIT_PACKET_TYPE;
  532. info->rx_count = 0;
  533. info->rx_skb = NULL;
  534. /* Initialize HCI device */
  535. hdev = hci_alloc_dev();
  536. if (!hdev) {
  537. BT_ERR("Can't allocate HCI device");
  538. return -ENOMEM;
  539. }
  540. info->hdev = hdev;
  541. hdev->type = HCI_PCCARD;
  542. hdev->driver_data = info;
  543. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  544. hdev->open = bluecard_hci_open;
  545. hdev->close = bluecard_hci_close;
  546. hdev->flush = bluecard_hci_flush;
  547. hdev->send = bluecard_hci_send_frame;
  548. hdev->destruct = bluecard_hci_destruct;
  549. hdev->ioctl = bluecard_hci_ioctl;
  550. hdev->owner = THIS_MODULE;
  551. id = inb(iobase + 0x30);
  552. if ((id & 0x0f) == 0x02)
  553. set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
  554. if (id & 0x10)
  555. set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
  556. if (id & 0x20)
  557. set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
  558. /* Reset card */
  559. info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
  560. outb(info->ctrl_reg, iobase + REG_CONTROL);
  561. /* Turn FPGA off */
  562. outb(0x80, iobase + 0x30);
  563. /* Wait some time */
  564. msleep(10);
  565. /* Turn FPGA on */
  566. outb(0x00, iobase + 0x30);
  567. /* Activate card */
  568. info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
  569. outb(info->ctrl_reg, iobase + REG_CONTROL);
  570. /* Enable interrupt */
  571. outb(0xff, iobase + REG_INTERRUPT);
  572. info->ctrl_reg |= REG_CONTROL_INTERRUPT;
  573. outb(info->ctrl_reg, iobase + REG_CONTROL);
  574. if ((id & 0x0f) == 0x03) {
  575. /* Disable RTS */
  576. info->ctrl_reg |= REG_CONTROL_RTS;
  577. outb(info->ctrl_reg, iobase + REG_CONTROL);
  578. /* Set baud rate */
  579. info->ctrl_reg |= 0x03;
  580. outb(info->ctrl_reg, iobase + REG_CONTROL);
  581. /* Enable RTS */
  582. info->ctrl_reg &= ~REG_CONTROL_RTS;
  583. outb(info->ctrl_reg, iobase + REG_CONTROL);
  584. set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
  585. set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
  586. set_bit(XMIT_SENDING_READY, &(info->tx_state));
  587. }
  588. /* Start the RX buffers */
  589. outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
  590. outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
  591. /* Signal that the hardware is ready */
  592. set_bit(CARD_READY, &(info->hw_state));
  593. /* Drop TX queue */
  594. skb_queue_purge(&(info->txq));
  595. /* Control the point at which RTS is enabled */
  596. outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
  597. /* Timeout before it is safe to send the first HCI packet */
  598. msleep(1250);
  599. /* Register HCI device */
  600. if (hci_register_dev(hdev) < 0) {
  601. BT_ERR("Can't register HCI device");
  602. info->hdev = NULL;
  603. hci_free_dev(hdev);
  604. return -ENODEV;
  605. }
  606. return 0;
  607. }
  608. static int bluecard_close(bluecard_info_t *info)
  609. {
  610. unsigned int iobase = info->p_dev->io.BasePort1;
  611. struct hci_dev *hdev = info->hdev;
  612. if (!hdev)
  613. return -ENODEV;
  614. bluecard_hci_close(hdev);
  615. clear_bit(CARD_READY, &(info->hw_state));
  616. /* Reset card */
  617. info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
  618. outb(info->ctrl_reg, iobase + REG_CONTROL);
  619. /* Turn FPGA off */
  620. outb(0x80, iobase + 0x30);
  621. if (hci_unregister_dev(hdev) < 0)
  622. BT_ERR("Can't unregister HCI device %s", hdev->name);
  623. hci_free_dev(hdev);
  624. return 0;
  625. }
  626. static int bluecard_probe(struct pcmcia_device *link)
  627. {
  628. bluecard_info_t *info;
  629. /* Create new info device */
  630. info = kzalloc(sizeof(*info), GFP_KERNEL);
  631. if (!info)
  632. return -ENOMEM;
  633. info->p_dev = link;
  634. link->priv = info;
  635. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  636. link->io.NumPorts1 = 8;
  637. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  638. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  639. link->irq.Handler = bluecard_interrupt;
  640. link->irq.Instance = info;
  641. link->conf.Attributes = CONF_ENABLE_IRQ;
  642. link->conf.IntType = INT_MEMORY_AND_IO;
  643. return bluecard_config(link);
  644. }
  645. static void bluecard_detach(struct pcmcia_device *link)
  646. {
  647. bluecard_info_t *info = link->priv;
  648. bluecard_release(link);
  649. kfree(info);
  650. }
  651. static int bluecard_config(struct pcmcia_device *link)
  652. {
  653. bluecard_info_t *info = link->priv;
  654. int i, n;
  655. link->conf.ConfigIndex = 0x20;
  656. link->io.NumPorts1 = 64;
  657. link->io.IOAddrLines = 6;
  658. for (n = 0; n < 0x400; n += 0x40) {
  659. link->io.BasePort1 = n ^ 0x300;
  660. i = pcmcia_request_io(link, &link->io);
  661. if (i == CS_SUCCESS)
  662. break;
  663. }
  664. if (i != CS_SUCCESS) {
  665. cs_error(link, RequestIO, i);
  666. goto failed;
  667. }
  668. i = pcmcia_request_irq(link, &link->irq);
  669. if (i != CS_SUCCESS) {
  670. cs_error(link, RequestIRQ, i);
  671. link->irq.AssignedIRQ = 0;
  672. }
  673. i = pcmcia_request_configuration(link, &link->conf);
  674. if (i != CS_SUCCESS) {
  675. cs_error(link, RequestConfiguration, i);
  676. goto failed;
  677. }
  678. if (bluecard_open(info) != 0)
  679. goto failed;
  680. strcpy(info->node.dev_name, info->hdev->name);
  681. link->dev_node = &info->node;
  682. return 0;
  683. failed:
  684. bluecard_release(link);
  685. return -ENODEV;
  686. }
  687. static void bluecard_release(struct pcmcia_device *link)
  688. {
  689. bluecard_info_t *info = link->priv;
  690. bluecard_close(info);
  691. del_timer(&(info->timer));
  692. pcmcia_disable_device(link);
  693. }
  694. static struct pcmcia_device_id bluecard_ids[] = {
  695. PCMCIA_DEVICE_PROD_ID12("BlueCard", "LSE041", 0xbaf16fbf, 0x657cc15e),
  696. PCMCIA_DEVICE_PROD_ID12("BTCFCARD", "LSE139", 0xe3987764, 0x2524b59c),
  697. PCMCIA_DEVICE_PROD_ID12("WSS", "LSE039", 0x0a0736ec, 0x24e6dfab),
  698. PCMCIA_DEVICE_NULL
  699. };
  700. MODULE_DEVICE_TABLE(pcmcia, bluecard_ids);
  701. static struct pcmcia_driver bluecard_driver = {
  702. .owner = THIS_MODULE,
  703. .drv = {
  704. .name = "bluecard_cs",
  705. },
  706. .probe = bluecard_probe,
  707. .remove = bluecard_detach,
  708. .id_table = bluecard_ids,
  709. };
  710. static int __init init_bluecard_cs(void)
  711. {
  712. return pcmcia_register_driver(&bluecard_driver);
  713. }
  714. static void __exit exit_bluecard_cs(void)
  715. {
  716. pcmcia_unregister_driver(&bluecard_driver);
  717. }
  718. module_init(init_bluecard_cs);
  719. module_exit(exit_bluecard_cs);