bluecard_cs.c 23 KB

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