bluecard_cs.c 25 KB

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