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