bluecard_cs.c 21 KB

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