bluecard_cs.c 23 KB

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