bluecard_cs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 = kmalloc(sizeof(*info), GFP_KERNEL);
  638. if (!info)
  639. return NULL;
  640. memset(info, 0, sizeof(*info));
  641. link = &info->link;
  642. link->priv = info;
  643. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  644. link->io.NumPorts1 = 8;
  645. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  646. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  647. link->irq.Handler = bluecard_interrupt;
  648. link->irq.Instance = info;
  649. link->conf.Attributes = CONF_ENABLE_IRQ;
  650. link->conf.Vcc = 50;
  651. link->conf.IntType = INT_MEMORY_AND_IO;
  652. /* Register with Card Services */
  653. link->next = dev_list;
  654. dev_list = link;
  655. client_reg.dev_info = &dev_info;
  656. client_reg.Version = 0x0210;
  657. client_reg.event_callback_args.client_data = link;
  658. ret = pcmcia_register_client(&link->handle, &client_reg);
  659. if (ret != CS_SUCCESS) {
  660. cs_error(link->handle, RegisterClient, ret);
  661. bluecard_detach(link);
  662. return NULL;
  663. }
  664. return link;
  665. }
  666. static void bluecard_detach(dev_link_t *link)
  667. {
  668. bluecard_info_t *info = link->priv;
  669. dev_link_t **linkp;
  670. int ret;
  671. /* Locate device structure */
  672. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  673. if (*linkp == link)
  674. break;
  675. if (*linkp == NULL)
  676. return;
  677. if (link->state & DEV_CONFIG)
  678. bluecard_release(link);
  679. if (link->handle) {
  680. ret = pcmcia_deregister_client(link->handle);
  681. if (ret != CS_SUCCESS)
  682. cs_error(link->handle, DeregisterClient, ret);
  683. }
  684. /* Unlink device structure, free bits */
  685. *linkp = link->next;
  686. kfree(info);
  687. }
  688. static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
  689. {
  690. int i;
  691. i = pcmcia_get_first_tuple(handle, tuple);
  692. if (i != CS_SUCCESS)
  693. return CS_NO_MORE_ITEMS;
  694. i = pcmcia_get_tuple_data(handle, tuple);
  695. if (i != CS_SUCCESS)
  696. return i;
  697. return pcmcia_parse_tuple(handle, tuple, parse);
  698. }
  699. static void bluecard_config(dev_link_t *link)
  700. {
  701. client_handle_t handle = link->handle;
  702. bluecard_info_t *info = link->priv;
  703. tuple_t tuple;
  704. u_short buf[256];
  705. cisparse_t parse;
  706. config_info_t config;
  707. int i, n, last_ret, last_fn;
  708. tuple.TupleData = (cisdata_t *)buf;
  709. tuple.TupleOffset = 0;
  710. tuple.TupleDataMax = 255;
  711. tuple.Attributes = 0;
  712. /* Get configuration register information */
  713. tuple.DesiredTuple = CISTPL_CONFIG;
  714. last_ret = first_tuple(handle, &tuple, &parse);
  715. if (last_ret != CS_SUCCESS) {
  716. last_fn = ParseTuple;
  717. goto cs_failed;
  718. }
  719. link->conf.ConfigBase = parse.config.base;
  720. link->conf.Present = parse.config.rmask[0];
  721. /* Configure card */
  722. link->state |= DEV_CONFIG;
  723. i = pcmcia_get_configuration_info(handle, &config);
  724. link->conf.Vcc = config.Vcc;
  725. link->conf.ConfigIndex = 0x20;
  726. link->io.NumPorts1 = 64;
  727. link->io.IOAddrLines = 6;
  728. for (n = 0; n < 0x400; n += 0x40) {
  729. link->io.BasePort1 = n ^ 0x300;
  730. i = pcmcia_request_io(link->handle, &link->io);
  731. if (i == CS_SUCCESS)
  732. break;
  733. }
  734. if (i != CS_SUCCESS) {
  735. cs_error(link->handle, RequestIO, i);
  736. goto failed;
  737. }
  738. i = pcmcia_request_irq(link->handle, &link->irq);
  739. if (i != CS_SUCCESS) {
  740. cs_error(link->handle, RequestIRQ, i);
  741. link->irq.AssignedIRQ = 0;
  742. }
  743. i = pcmcia_request_configuration(link->handle, &link->conf);
  744. if (i != CS_SUCCESS) {
  745. cs_error(link->handle, RequestConfiguration, i);
  746. goto failed;
  747. }
  748. if (bluecard_open(info) != 0)
  749. goto failed;
  750. strcpy(info->node.dev_name, info->hdev->name);
  751. link->dev = &info->node;
  752. link->state &= ~DEV_CONFIG_PENDING;
  753. return;
  754. cs_failed:
  755. cs_error(link->handle, last_fn, last_ret);
  756. failed:
  757. bluecard_release(link);
  758. }
  759. static void bluecard_release(dev_link_t *link)
  760. {
  761. bluecard_info_t *info = link->priv;
  762. if (link->state & DEV_PRESENT)
  763. bluecard_close(info);
  764. del_timer(&(info->timer));
  765. link->dev = NULL;
  766. pcmcia_release_configuration(link->handle);
  767. pcmcia_release_io(link->handle, &link->io);
  768. pcmcia_release_irq(link->handle, &link->irq);
  769. link->state &= ~DEV_CONFIG;
  770. }
  771. static int bluecard_event(event_t event, int priority, event_callback_args_t *args)
  772. {
  773. dev_link_t *link = args->client_data;
  774. bluecard_info_t *info = link->priv;
  775. switch (event) {
  776. case CS_EVENT_CARD_REMOVAL:
  777. link->state &= ~DEV_PRESENT;
  778. if (link->state & DEV_CONFIG) {
  779. bluecard_close(info);
  780. bluecard_release(link);
  781. }
  782. break;
  783. case CS_EVENT_CARD_INSERTION:
  784. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  785. bluecard_config(link);
  786. break;
  787. case CS_EVENT_PM_SUSPEND:
  788. link->state |= DEV_SUSPEND;
  789. /* Fall through... */
  790. case CS_EVENT_RESET_PHYSICAL:
  791. if (link->state & DEV_CONFIG)
  792. pcmcia_release_configuration(link->handle);
  793. break;
  794. case CS_EVENT_PM_RESUME:
  795. link->state &= ~DEV_SUSPEND;
  796. /* Fall through... */
  797. case CS_EVENT_CARD_RESET:
  798. if (DEV_OK(link))
  799. pcmcia_request_configuration(link->handle, &link->conf);
  800. break;
  801. }
  802. return 0;
  803. }
  804. static struct pcmcia_device_id bluecard_ids[] = {
  805. PCMCIA_DEVICE_PROD_ID12("BlueCard", "LSE041", 0xbaf16fbf, 0x657cc15e),
  806. PCMCIA_DEVICE_PROD_ID12("BTCFCARD", "LSE139", 0xe3987764, 0x2524b59c),
  807. PCMCIA_DEVICE_PROD_ID12("WSS", "LSE039", 0x0a0736ec, 0x24e6dfab),
  808. PCMCIA_DEVICE_NULL
  809. };
  810. MODULE_DEVICE_TABLE(pcmcia, bluecard_ids);
  811. static struct pcmcia_driver bluecard_driver = {
  812. .owner = THIS_MODULE,
  813. .drv = {
  814. .name = "bluecard_cs",
  815. },
  816. .attach = bluecard_attach,
  817. .event = bluecard_event,
  818. .detach = bluecard_detach,
  819. .id_table = bluecard_ids,
  820. };
  821. static int __init init_bluecard_cs(void)
  822. {
  823. return pcmcia_register_driver(&bluecard_driver);
  824. }
  825. static void __exit exit_bluecard_cs(void)
  826. {
  827. pcmcia_unregister_driver(&bluecard_driver);
  828. BUG_ON(dev_list != NULL);
  829. }
  830. module_init(init_bluecard_cs);
  831. module_exit(exit_bluecard_cs);