bluecard_cs.c 22 KB

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