bluecard_cs.c 21 KB

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