bluecard_cs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 <linux/io.h>
  38. #include <pcmcia/cistpl.h>
  39. #include <pcmcia/ciscode.h>
  40. #include <pcmcia/ds.h>
  41. #include <pcmcia/cisreg.h>
  42. #include <net/bluetooth/bluetooth.h>
  43. #include <net/bluetooth/hci_core.h>
  44. /* ======================== Module parameters ======================== */
  45. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  46. MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
  47. MODULE_LICENSE("GPL");
  48. /* ======================== Local structures ======================== */
  49. struct bluecard_info {
  50. struct pcmcia_device *p_dev;
  51. struct hci_dev *hdev;
  52. spinlock_t lock; /* For serializing operations */
  53. struct timer_list timer; /* For LED control */
  54. struct sk_buff_head txq;
  55. unsigned long tx_state;
  56. unsigned long rx_state;
  57. unsigned long rx_count;
  58. struct sk_buff *rx_skb;
  59. unsigned char ctrl_reg;
  60. unsigned long hw_state; /* Status of the hardware and LED control */
  61. };
  62. static int bluecard_config(struct pcmcia_device *link);
  63. static void bluecard_release(struct pcmcia_device *link);
  64. static void bluecard_detach(struct pcmcia_device *p_dev);
  65. /* Default baud rate: 57600, 115200, 230400 or 460800 */
  66. #define DEFAULT_BAUD_RATE 230400
  67. /* Hardware states */
  68. #define CARD_READY 1
  69. #define CARD_ACTIVITY 2
  70. #define CARD_HAS_PCCARD_ID 4
  71. #define CARD_HAS_POWER_LED 5
  72. #define CARD_HAS_ACTIVITY_LED 6
  73. /* Transmit states */
  74. #define XMIT_SENDING 1
  75. #define XMIT_WAKEUP 2
  76. #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
  77. #define XMIT_BUF_ONE_READY 6
  78. #define XMIT_BUF_TWO_READY 7
  79. #define XMIT_SENDING_READY 8
  80. /* Receiver states */
  81. #define RECV_WAIT_PACKET_TYPE 0
  82. #define RECV_WAIT_EVENT_HEADER 1
  83. #define RECV_WAIT_ACL_HEADER 2
  84. #define RECV_WAIT_SCO_HEADER 3
  85. #define RECV_WAIT_DATA 4
  86. /* Special packet types */
  87. #define PKT_BAUD_RATE_57600 0x80
  88. #define PKT_BAUD_RATE_115200 0x81
  89. #define PKT_BAUD_RATE_230400 0x82
  90. #define PKT_BAUD_RATE_460800 0x83
  91. /* These are the register offsets */
  92. #define REG_COMMAND 0x20
  93. #define REG_INTERRUPT 0x21
  94. #define REG_CONTROL 0x22
  95. #define REG_RX_CONTROL 0x24
  96. #define REG_CARD_RESET 0x30
  97. #define REG_LED_CTRL 0x30
  98. /* REG_COMMAND */
  99. #define REG_COMMAND_TX_BUF_ONE 0x01
  100. #define REG_COMMAND_TX_BUF_TWO 0x02
  101. #define REG_COMMAND_RX_BUF_ONE 0x04
  102. #define REG_COMMAND_RX_BUF_TWO 0x08
  103. #define REG_COMMAND_RX_WIN_ONE 0x00
  104. #define REG_COMMAND_RX_WIN_TWO 0x10
  105. /* REG_CONTROL */
  106. #define REG_CONTROL_BAUD_RATE_57600 0x00
  107. #define REG_CONTROL_BAUD_RATE_115200 0x01
  108. #define REG_CONTROL_BAUD_RATE_230400 0x02
  109. #define REG_CONTROL_BAUD_RATE_460800 0x03
  110. #define REG_CONTROL_RTS 0x04
  111. #define REG_CONTROL_BT_ON 0x08
  112. #define REG_CONTROL_BT_RESET 0x10
  113. #define REG_CONTROL_BT_RES_PU 0x20
  114. #define REG_CONTROL_INTERRUPT 0x40
  115. #define REG_CONTROL_CARD_RESET 0x80
  116. /* REG_RX_CONTROL */
  117. #define RTS_LEVEL_SHIFT_BITS 0x02
  118. /* ======================== LED handling routines ======================== */
  119. static void bluecard_activity_led_timeout(struct timer_list *t)
  120. {
  121. struct bluecard_info *info = from_timer(info, t, timer);
  122. unsigned int iobase = info->p_dev->resource[0]->start;
  123. if (test_bit(CARD_ACTIVITY, &(info->hw_state))) {
  124. /* leave LED in inactive state for HZ/10 for blink effect */
  125. clear_bit(CARD_ACTIVITY, &(info->hw_state));
  126. mod_timer(&(info->timer), jiffies + HZ / 10);
  127. }
  128. /* Disable activity LED, enable power LED */
  129. outb(0x08 | 0x20, iobase + 0x30);
  130. }
  131. static void bluecard_enable_activity_led(struct bluecard_info *info)
  132. {
  133. unsigned int iobase = info->p_dev->resource[0]->start;
  134. /* don't disturb running blink timer */
  135. if (timer_pending(&(info->timer)))
  136. return;
  137. set_bit(CARD_ACTIVITY, &(info->hw_state));
  138. if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
  139. /* Enable activity LED, keep power LED enabled */
  140. outb(0x18 | 0x60, iobase + 0x30);
  141. } else {
  142. /* Disable power LED */
  143. outb(0x00, iobase + 0x30);
  144. }
  145. /* Stop the LED after HZ/10 */
  146. mod_timer(&(info->timer), jiffies + HZ / 10);
  147. }
  148. /* ======================== Interrupt handling ======================== */
  149. static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
  150. {
  151. int i, actual;
  152. actual = (len > 15) ? 15 : len;
  153. outb_p(actual, iobase + offset);
  154. for (i = 0; i < actual; i++)
  155. outb_p(buf[i], iobase + offset + i + 1);
  156. return actual;
  157. }
  158. static void bluecard_write_wakeup(struct bluecard_info *info)
  159. {
  160. if (!info) {
  161. BT_ERR("Unknown device");
  162. return;
  163. }
  164. if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
  165. return;
  166. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  167. set_bit(XMIT_WAKEUP, &(info->tx_state));
  168. return;
  169. }
  170. do {
  171. unsigned int iobase = info->p_dev->resource[0]->start;
  172. unsigned int offset;
  173. unsigned char command;
  174. unsigned long ready_bit;
  175. register struct sk_buff *skb;
  176. int len;
  177. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  178. if (!pcmcia_dev_present(info->p_dev))
  179. return;
  180. if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
  181. if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
  182. break;
  183. offset = 0x10;
  184. command = REG_COMMAND_TX_BUF_TWO;
  185. ready_bit = XMIT_BUF_TWO_READY;
  186. } else {
  187. if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
  188. break;
  189. offset = 0x00;
  190. command = REG_COMMAND_TX_BUF_ONE;
  191. ready_bit = XMIT_BUF_ONE_READY;
  192. }
  193. skb = skb_dequeue(&(info->txq));
  194. if (!skb)
  195. break;
  196. if (hci_skb_pkt_type(skb) & 0x80) {
  197. /* Disable RTS */
  198. info->ctrl_reg |= REG_CONTROL_RTS;
  199. outb(info->ctrl_reg, iobase + REG_CONTROL);
  200. }
  201. /* Activate LED */
  202. bluecard_enable_activity_led(info);
  203. /* Send frame */
  204. len = bluecard_write(iobase, offset, skb->data, skb->len);
  205. /* Tell the FPGA to send the data */
  206. outb_p(command, iobase + REG_COMMAND);
  207. /* Mark the buffer as dirty */
  208. clear_bit(ready_bit, &(info->tx_state));
  209. if (hci_skb_pkt_type(skb) & 0x80) {
  210. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  211. DEFINE_WAIT(wait);
  212. unsigned char baud_reg;
  213. switch (hci_skb_pkt_type(skb)) {
  214. case PKT_BAUD_RATE_460800:
  215. baud_reg = REG_CONTROL_BAUD_RATE_460800;
  216. break;
  217. case PKT_BAUD_RATE_230400:
  218. baud_reg = REG_CONTROL_BAUD_RATE_230400;
  219. break;
  220. case PKT_BAUD_RATE_115200:
  221. baud_reg = REG_CONTROL_BAUD_RATE_115200;
  222. break;
  223. case PKT_BAUD_RATE_57600:
  224. /* Fall through... */
  225. default:
  226. baud_reg = REG_CONTROL_BAUD_RATE_57600;
  227. break;
  228. }
  229. /* Wait until the command reaches the baseband */
  230. mdelay(100);
  231. /* Set baud on baseband */
  232. info->ctrl_reg &= ~0x03;
  233. info->ctrl_reg |= baud_reg;
  234. outb(info->ctrl_reg, iobase + REG_CONTROL);
  235. /* Enable RTS */
  236. info->ctrl_reg &= ~REG_CONTROL_RTS;
  237. outb(info->ctrl_reg, iobase + REG_CONTROL);
  238. /* Wait before the next HCI packet can be send */
  239. mdelay(1000);
  240. }
  241. if (len == skb->len) {
  242. kfree_skb(skb);
  243. } else {
  244. skb_pull(skb, len);
  245. skb_queue_head(&(info->txq), skb);
  246. }
  247. info->hdev->stat.byte_tx += len;
  248. /* Change buffer */
  249. change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
  250. } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
  251. clear_bit(XMIT_SENDING, &(info->tx_state));
  252. }
  253. static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
  254. {
  255. int i, n, len;
  256. outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
  257. len = inb(iobase + offset);
  258. n = 0;
  259. i = 1;
  260. while (n < len) {
  261. if (i == 16) {
  262. outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
  263. i = 0;
  264. }
  265. buf[n] = inb(iobase + offset + i);
  266. n++;
  267. i++;
  268. }
  269. return len;
  270. }
  271. static void bluecard_receive(struct bluecard_info *info,
  272. unsigned int offset)
  273. {
  274. unsigned int iobase;
  275. unsigned char buf[31];
  276. int i, len;
  277. if (!info) {
  278. BT_ERR("Unknown device");
  279. return;
  280. }
  281. iobase = info->p_dev->resource[0]->start;
  282. if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
  283. bluecard_enable_activity_led(info);
  284. len = bluecard_read(iobase, offset, buf, sizeof(buf));
  285. for (i = 0; i < len; i++) {
  286. /* Allocate packet */
  287. if (!info->rx_skb) {
  288. info->rx_state = RECV_WAIT_PACKET_TYPE;
  289. info->rx_count = 0;
  290. info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  291. if (!info->rx_skb) {
  292. BT_ERR("Can't allocate mem for new packet");
  293. return;
  294. }
  295. }
  296. if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
  297. hci_skb_pkt_type(info->rx_skb) = buf[i];
  298. switch (hci_skb_pkt_type(info->rx_skb)) {
  299. case 0x00:
  300. /* init packet */
  301. if (offset != 0x00) {
  302. set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
  303. set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
  304. set_bit(XMIT_SENDING_READY, &(info->tx_state));
  305. bluecard_write_wakeup(info);
  306. }
  307. kfree_skb(info->rx_skb);
  308. info->rx_skb = NULL;
  309. break;
  310. case HCI_EVENT_PKT:
  311. info->rx_state = RECV_WAIT_EVENT_HEADER;
  312. info->rx_count = HCI_EVENT_HDR_SIZE;
  313. break;
  314. case HCI_ACLDATA_PKT:
  315. info->rx_state = RECV_WAIT_ACL_HEADER;
  316. info->rx_count = HCI_ACL_HDR_SIZE;
  317. break;
  318. case HCI_SCODATA_PKT:
  319. info->rx_state = RECV_WAIT_SCO_HEADER;
  320. info->rx_count = HCI_SCO_HDR_SIZE;
  321. break;
  322. default:
  323. /* unknown packet */
  324. BT_ERR("Unknown HCI packet with type 0x%02x received",
  325. hci_skb_pkt_type(info->rx_skb));
  326. info->hdev->stat.err_rx++;
  327. kfree_skb(info->rx_skb);
  328. info->rx_skb = NULL;
  329. break;
  330. }
  331. } else {
  332. skb_put_u8(info->rx_skb, buf[i]);
  333. info->rx_count--;
  334. if (info->rx_count == 0) {
  335. int dlen;
  336. struct hci_event_hdr *eh;
  337. struct hci_acl_hdr *ah;
  338. struct hci_sco_hdr *sh;
  339. switch (info->rx_state) {
  340. case RECV_WAIT_EVENT_HEADER:
  341. eh = hci_event_hdr(info->rx_skb);
  342. info->rx_state = RECV_WAIT_DATA;
  343. info->rx_count = eh->plen;
  344. break;
  345. case RECV_WAIT_ACL_HEADER:
  346. ah = hci_acl_hdr(info->rx_skb);
  347. dlen = __le16_to_cpu(ah->dlen);
  348. info->rx_state = RECV_WAIT_DATA;
  349. info->rx_count = dlen;
  350. break;
  351. case RECV_WAIT_SCO_HEADER:
  352. sh = hci_sco_hdr(info->rx_skb);
  353. info->rx_state = RECV_WAIT_DATA;
  354. info->rx_count = sh->dlen;
  355. break;
  356. case RECV_WAIT_DATA:
  357. hci_recv_frame(info->hdev, info->rx_skb);
  358. info->rx_skb = NULL;
  359. break;
  360. }
  361. }
  362. }
  363. }
  364. info->hdev->stat.byte_rx += len;
  365. }
  366. static irqreturn_t bluecard_interrupt(int irq, void *dev_inst)
  367. {
  368. struct bluecard_info *info = dev_inst;
  369. unsigned int iobase;
  370. unsigned char reg;
  371. if (!info || !info->hdev)
  372. /* our irq handler is shared */
  373. return IRQ_NONE;
  374. if (!test_bit(CARD_READY, &(info->hw_state)))
  375. return IRQ_HANDLED;
  376. iobase = info->p_dev->resource[0]->start;
  377. spin_lock(&(info->lock));
  378. /* Disable interrupt */
  379. info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
  380. outb(info->ctrl_reg, iobase + REG_CONTROL);
  381. reg = inb(iobase + REG_INTERRUPT);
  382. if ((reg != 0x00) && (reg != 0xff)) {
  383. if (reg & 0x04) {
  384. bluecard_receive(info, 0x00);
  385. outb(0x04, iobase + REG_INTERRUPT);
  386. outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
  387. }
  388. if (reg & 0x08) {
  389. bluecard_receive(info, 0x10);
  390. outb(0x08, iobase + REG_INTERRUPT);
  391. outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
  392. }
  393. if (reg & 0x01) {
  394. set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
  395. outb(0x01, iobase + REG_INTERRUPT);
  396. bluecard_write_wakeup(info);
  397. }
  398. if (reg & 0x02) {
  399. set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
  400. outb(0x02, iobase + REG_INTERRUPT);
  401. bluecard_write_wakeup(info);
  402. }
  403. }
  404. /* Enable interrupt */
  405. info->ctrl_reg |= REG_CONTROL_INTERRUPT;
  406. outb(info->ctrl_reg, iobase + REG_CONTROL);
  407. spin_unlock(&(info->lock));
  408. return IRQ_HANDLED;
  409. }
  410. /* ======================== Device specific HCI commands ======================== */
  411. static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
  412. {
  413. struct bluecard_info *info = hci_get_drvdata(hdev);
  414. struct sk_buff *skb;
  415. /* Ericsson baud rate command */
  416. unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
  417. skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_KERNEL);
  418. if (!skb) {
  419. BT_ERR("Can't allocate mem for new packet");
  420. return -1;
  421. }
  422. switch (baud) {
  423. case 460800:
  424. cmd[4] = 0x00;
  425. hci_skb_pkt_type(skb) = PKT_BAUD_RATE_460800;
  426. break;
  427. case 230400:
  428. cmd[4] = 0x01;
  429. hci_skb_pkt_type(skb) = PKT_BAUD_RATE_230400;
  430. break;
  431. case 115200:
  432. cmd[4] = 0x02;
  433. hci_skb_pkt_type(skb) = PKT_BAUD_RATE_115200;
  434. break;
  435. case 57600:
  436. /* Fall through... */
  437. default:
  438. cmd[4] = 0x03;
  439. hci_skb_pkt_type(skb) = PKT_BAUD_RATE_57600;
  440. break;
  441. }
  442. skb_put_data(skb, cmd, sizeof(cmd));
  443. skb_queue_tail(&(info->txq), skb);
  444. bluecard_write_wakeup(info);
  445. return 0;
  446. }
  447. /* ======================== HCI interface ======================== */
  448. static int bluecard_hci_flush(struct hci_dev *hdev)
  449. {
  450. struct bluecard_info *info = hci_get_drvdata(hdev);
  451. /* Drop TX queue */
  452. skb_queue_purge(&(info->txq));
  453. return 0;
  454. }
  455. static int bluecard_hci_open(struct hci_dev *hdev)
  456. {
  457. struct bluecard_info *info = hci_get_drvdata(hdev);
  458. unsigned int iobase = info->p_dev->resource[0]->start;
  459. if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
  460. bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
  461. /* Enable power LED */
  462. outb(0x08 | 0x20, iobase + 0x30);
  463. return 0;
  464. }
  465. static int bluecard_hci_close(struct hci_dev *hdev)
  466. {
  467. struct bluecard_info *info = hci_get_drvdata(hdev);
  468. unsigned int iobase = info->p_dev->resource[0]->start;
  469. bluecard_hci_flush(hdev);
  470. /* Stop LED timer */
  471. del_timer_sync(&(info->timer));
  472. /* Disable power LED */
  473. outb(0x00, iobase + 0x30);
  474. return 0;
  475. }
  476. static int bluecard_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  477. {
  478. struct bluecard_info *info = hci_get_drvdata(hdev);
  479. switch (hci_skb_pkt_type(skb)) {
  480. case HCI_COMMAND_PKT:
  481. hdev->stat.cmd_tx++;
  482. break;
  483. case HCI_ACLDATA_PKT:
  484. hdev->stat.acl_tx++;
  485. break;
  486. case HCI_SCODATA_PKT:
  487. hdev->stat.sco_tx++;
  488. break;
  489. }
  490. /* Prepend skb with frame type */
  491. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  492. skb_queue_tail(&(info->txq), skb);
  493. bluecard_write_wakeup(info);
  494. return 0;
  495. }
  496. /* ======================== Card services HCI interaction ======================== */
  497. static int bluecard_open(struct bluecard_info *info)
  498. {
  499. unsigned int iobase = info->p_dev->resource[0]->start;
  500. struct hci_dev *hdev;
  501. unsigned char id;
  502. spin_lock_init(&(info->lock));
  503. timer_setup(&info->timer, bluecard_activity_led_timeout, 0);
  504. skb_queue_head_init(&(info->txq));
  505. info->rx_state = RECV_WAIT_PACKET_TYPE;
  506. info->rx_count = 0;
  507. info->rx_skb = NULL;
  508. /* Initialize HCI device */
  509. hdev = hci_alloc_dev();
  510. if (!hdev) {
  511. BT_ERR("Can't allocate HCI device");
  512. return -ENOMEM;
  513. }
  514. info->hdev = hdev;
  515. hdev->bus = HCI_PCCARD;
  516. hci_set_drvdata(hdev, info);
  517. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  518. hdev->open = bluecard_hci_open;
  519. hdev->close = bluecard_hci_close;
  520. hdev->flush = bluecard_hci_flush;
  521. hdev->send = bluecard_hci_send_frame;
  522. id = inb(iobase + 0x30);
  523. if ((id & 0x0f) == 0x02)
  524. set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
  525. if (id & 0x10)
  526. set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
  527. if (id & 0x20)
  528. set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
  529. /* Reset card */
  530. info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
  531. outb(info->ctrl_reg, iobase + REG_CONTROL);
  532. /* Turn FPGA off */
  533. outb(0x80, iobase + 0x30);
  534. /* Wait some time */
  535. msleep(10);
  536. /* Turn FPGA on */
  537. outb(0x00, iobase + 0x30);
  538. /* Activate card */
  539. info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
  540. outb(info->ctrl_reg, iobase + REG_CONTROL);
  541. /* Enable interrupt */
  542. outb(0xff, iobase + REG_INTERRUPT);
  543. info->ctrl_reg |= REG_CONTROL_INTERRUPT;
  544. outb(info->ctrl_reg, iobase + REG_CONTROL);
  545. if ((id & 0x0f) == 0x03) {
  546. /* Disable RTS */
  547. info->ctrl_reg |= REG_CONTROL_RTS;
  548. outb(info->ctrl_reg, iobase + REG_CONTROL);
  549. /* Set baud rate */
  550. info->ctrl_reg |= 0x03;
  551. outb(info->ctrl_reg, iobase + REG_CONTROL);
  552. /* Enable RTS */
  553. info->ctrl_reg &= ~REG_CONTROL_RTS;
  554. outb(info->ctrl_reg, iobase + REG_CONTROL);
  555. set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
  556. set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
  557. set_bit(XMIT_SENDING_READY, &(info->tx_state));
  558. }
  559. /* Start the RX buffers */
  560. outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
  561. outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
  562. /* Signal that the hardware is ready */
  563. set_bit(CARD_READY, &(info->hw_state));
  564. /* Drop TX queue */
  565. skb_queue_purge(&(info->txq));
  566. /* Control the point at which RTS is enabled */
  567. outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
  568. /* Timeout before it is safe to send the first HCI packet */
  569. msleep(1250);
  570. /* Register HCI device */
  571. if (hci_register_dev(hdev) < 0) {
  572. BT_ERR("Can't register HCI device");
  573. info->hdev = NULL;
  574. hci_free_dev(hdev);
  575. return -ENODEV;
  576. }
  577. return 0;
  578. }
  579. static int bluecard_close(struct bluecard_info *info)
  580. {
  581. unsigned int iobase = info->p_dev->resource[0]->start;
  582. struct hci_dev *hdev = info->hdev;
  583. if (!hdev)
  584. return -ENODEV;
  585. bluecard_hci_close(hdev);
  586. clear_bit(CARD_READY, &(info->hw_state));
  587. /* Reset card */
  588. info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
  589. outb(info->ctrl_reg, iobase + REG_CONTROL);
  590. /* Turn FPGA off */
  591. outb(0x80, iobase + 0x30);
  592. hci_unregister_dev(hdev);
  593. hci_free_dev(hdev);
  594. return 0;
  595. }
  596. static int bluecard_probe(struct pcmcia_device *link)
  597. {
  598. struct bluecard_info *info;
  599. /* Create new info device */
  600. info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
  601. if (!info)
  602. return -ENOMEM;
  603. info->p_dev = link;
  604. link->priv = info;
  605. link->config_flags |= CONF_ENABLE_IRQ;
  606. return bluecard_config(link);
  607. }
  608. static void bluecard_detach(struct pcmcia_device *link)
  609. {
  610. bluecard_release(link);
  611. }
  612. static int bluecard_config(struct pcmcia_device *link)
  613. {
  614. struct bluecard_info *info = link->priv;
  615. int i, n;
  616. link->config_index = 0x20;
  617. link->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  618. link->resource[0]->end = 64;
  619. link->io_lines = 6;
  620. for (n = 0; n < 0x400; n += 0x40) {
  621. link->resource[0]->start = n ^ 0x300;
  622. i = pcmcia_request_io(link);
  623. if (i == 0)
  624. break;
  625. }
  626. if (i != 0)
  627. goto failed;
  628. i = pcmcia_request_irq(link, bluecard_interrupt);
  629. if (i != 0)
  630. goto failed;
  631. i = pcmcia_enable_device(link);
  632. if (i != 0)
  633. goto failed;
  634. if (bluecard_open(info) != 0)
  635. goto failed;
  636. return 0;
  637. failed:
  638. bluecard_release(link);
  639. return -ENODEV;
  640. }
  641. static void bluecard_release(struct pcmcia_device *link)
  642. {
  643. struct bluecard_info *info = link->priv;
  644. bluecard_close(info);
  645. del_timer_sync(&(info->timer));
  646. pcmcia_disable_device(link);
  647. }
  648. static const struct pcmcia_device_id bluecard_ids[] = {
  649. PCMCIA_DEVICE_PROD_ID12("BlueCard", "LSE041", 0xbaf16fbf, 0x657cc15e),
  650. PCMCIA_DEVICE_PROD_ID12("BTCFCARD", "LSE139", 0xe3987764, 0x2524b59c),
  651. PCMCIA_DEVICE_PROD_ID12("WSS", "LSE039", 0x0a0736ec, 0x24e6dfab),
  652. PCMCIA_DEVICE_NULL
  653. };
  654. MODULE_DEVICE_TABLE(pcmcia, bluecard_ids);
  655. static struct pcmcia_driver bluecard_driver = {
  656. .owner = THIS_MODULE,
  657. .name = "bluecard_cs",
  658. .probe = bluecard_probe,
  659. .remove = bluecard_detach,
  660. .id_table = bluecard_ids,
  661. };
  662. module_pcmcia_driver(bluecard_driver);