bluecard_cs.c 25 KB

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