btuart_cs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. *
  3. * Driver for Bluetooth PCMCIA cards with HCI UART interface
  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/delay.h>
  28. #include <linux/errno.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/ioport.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/string.h>
  35. #include <linux/serial.h>
  36. #include <linux/serial_reg.h>
  37. #include <linux/bitops.h>
  38. #include <linux/io.h>
  39. #include <pcmcia/cistpl.h>
  40. #include <pcmcia/ciscode.h>
  41. #include <pcmcia/ds.h>
  42. #include <pcmcia/cisreg.h>
  43. #include <net/bluetooth/bluetooth.h>
  44. #include <net/bluetooth/hci_core.h>
  45. /* ======================== Module parameters ======================== */
  46. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  47. MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
  48. MODULE_LICENSE("GPL");
  49. /* ======================== Local structures ======================== */
  50. struct btuart_info {
  51. struct pcmcia_device *p_dev;
  52. struct hci_dev *hdev;
  53. spinlock_t lock; /* For serializing operations */
  54. struct sk_buff_head txq;
  55. unsigned long tx_state;
  56. unsigned long rx_state;
  57. unsigned long rx_count;
  58. struct sk_buff *rx_skb;
  59. };
  60. static int btuart_config(struct pcmcia_device *link);
  61. static void btuart_release(struct pcmcia_device *link);
  62. static void btuart_detach(struct pcmcia_device *p_dev);
  63. /* Maximum baud rate */
  64. #define SPEED_MAX 115200
  65. /* Default baud rate: 57600, 115200, 230400 or 460800 */
  66. #define DEFAULT_BAUD_RATE 115200
  67. /* Transmit states */
  68. #define XMIT_SENDING 1
  69. #define XMIT_WAKEUP 2
  70. #define XMIT_WAITING 8
  71. /* Receiver states */
  72. #define RECV_WAIT_PACKET_TYPE 0
  73. #define RECV_WAIT_EVENT_HEADER 1
  74. #define RECV_WAIT_ACL_HEADER 2
  75. #define RECV_WAIT_SCO_HEADER 3
  76. #define RECV_WAIT_DATA 4
  77. /* ======================== Interrupt handling ======================== */
  78. static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
  79. {
  80. int actual = 0;
  81. /* Tx FIFO should be empty */
  82. if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
  83. return 0;
  84. /* Fill FIFO with current frame */
  85. while ((fifo_size-- > 0) && (actual < len)) {
  86. /* Transmit next byte */
  87. outb(buf[actual], iobase + UART_TX);
  88. actual++;
  89. }
  90. return actual;
  91. }
  92. static void btuart_write_wakeup(struct btuart_info *info)
  93. {
  94. if (!info) {
  95. BT_ERR("Unknown device");
  96. return;
  97. }
  98. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  99. set_bit(XMIT_WAKEUP, &(info->tx_state));
  100. return;
  101. }
  102. do {
  103. unsigned int iobase = info->p_dev->resource[0]->start;
  104. register struct sk_buff *skb;
  105. int len;
  106. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  107. if (!pcmcia_dev_present(info->p_dev))
  108. return;
  109. skb = skb_dequeue(&(info->txq));
  110. if (!skb)
  111. break;
  112. /* Send frame */
  113. len = btuart_write(iobase, 16, skb->data, skb->len);
  114. set_bit(XMIT_WAKEUP, &(info->tx_state));
  115. if (len == skb->len) {
  116. kfree_skb(skb);
  117. } else {
  118. skb_pull(skb, len);
  119. skb_queue_head(&(info->txq), skb);
  120. }
  121. info->hdev->stat.byte_tx += len;
  122. } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
  123. clear_bit(XMIT_SENDING, &(info->tx_state));
  124. }
  125. static void btuart_receive(struct btuart_info *info)
  126. {
  127. unsigned int iobase;
  128. int boguscount = 0;
  129. if (!info) {
  130. BT_ERR("Unknown device");
  131. return;
  132. }
  133. iobase = info->p_dev->resource[0]->start;
  134. do {
  135. info->hdev->stat.byte_rx++;
  136. /* Allocate packet */
  137. if (!info->rx_skb) {
  138. info->rx_state = RECV_WAIT_PACKET_TYPE;
  139. info->rx_count = 0;
  140. info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  141. if (!info->rx_skb) {
  142. BT_ERR("Can't allocate mem for new packet");
  143. return;
  144. }
  145. }
  146. if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
  147. bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
  148. switch (bt_cb(info->rx_skb)->pkt_type) {
  149. case HCI_EVENT_PKT:
  150. info->rx_state = RECV_WAIT_EVENT_HEADER;
  151. info->rx_count = HCI_EVENT_HDR_SIZE;
  152. break;
  153. case HCI_ACLDATA_PKT:
  154. info->rx_state = RECV_WAIT_ACL_HEADER;
  155. info->rx_count = HCI_ACL_HDR_SIZE;
  156. break;
  157. case HCI_SCODATA_PKT:
  158. info->rx_state = RECV_WAIT_SCO_HEADER;
  159. info->rx_count = HCI_SCO_HDR_SIZE;
  160. break;
  161. default:
  162. /* Unknown packet */
  163. BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
  164. info->hdev->stat.err_rx++;
  165. kfree_skb(info->rx_skb);
  166. info->rx_skb = NULL;
  167. break;
  168. }
  169. } else {
  170. *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
  171. info->rx_count--;
  172. if (info->rx_count == 0) {
  173. int dlen;
  174. struct hci_event_hdr *eh;
  175. struct hci_acl_hdr *ah;
  176. struct hci_sco_hdr *sh;
  177. switch (info->rx_state) {
  178. case RECV_WAIT_EVENT_HEADER:
  179. eh = hci_event_hdr(info->rx_skb);
  180. info->rx_state = RECV_WAIT_DATA;
  181. info->rx_count = eh->plen;
  182. break;
  183. case RECV_WAIT_ACL_HEADER:
  184. ah = hci_acl_hdr(info->rx_skb);
  185. dlen = __le16_to_cpu(ah->dlen);
  186. info->rx_state = RECV_WAIT_DATA;
  187. info->rx_count = dlen;
  188. break;
  189. case RECV_WAIT_SCO_HEADER:
  190. sh = hci_sco_hdr(info->rx_skb);
  191. info->rx_state = RECV_WAIT_DATA;
  192. info->rx_count = sh->dlen;
  193. break;
  194. case RECV_WAIT_DATA:
  195. hci_recv_frame(info->hdev, info->rx_skb);
  196. info->rx_skb = NULL;
  197. break;
  198. }
  199. }
  200. }
  201. /* Make sure we don't stay here too long */
  202. if (boguscount++ > 16)
  203. break;
  204. } while (inb(iobase + UART_LSR) & UART_LSR_DR);
  205. }
  206. static irqreturn_t btuart_interrupt(int irq, void *dev_inst)
  207. {
  208. struct btuart_info *info = dev_inst;
  209. unsigned int iobase;
  210. int boguscount = 0;
  211. int iir, lsr;
  212. irqreturn_t r = IRQ_NONE;
  213. if (!info || !info->hdev)
  214. /* our irq handler is shared */
  215. return IRQ_NONE;
  216. iobase = info->p_dev->resource[0]->start;
  217. spin_lock(&(info->lock));
  218. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  219. while (iir) {
  220. r = IRQ_HANDLED;
  221. /* Clear interrupt */
  222. lsr = inb(iobase + UART_LSR);
  223. switch (iir) {
  224. case UART_IIR_RLSI:
  225. BT_ERR("RLSI");
  226. break;
  227. case UART_IIR_RDI:
  228. /* Receive interrupt */
  229. btuart_receive(info);
  230. break;
  231. case UART_IIR_THRI:
  232. if (lsr & UART_LSR_THRE) {
  233. /* Transmitter ready for data */
  234. btuart_write_wakeup(info);
  235. }
  236. break;
  237. default:
  238. BT_ERR("Unhandled IIR=%#x", iir);
  239. break;
  240. }
  241. /* Make sure we don't stay here too long */
  242. if (boguscount++ > 100)
  243. break;
  244. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  245. }
  246. spin_unlock(&(info->lock));
  247. return r;
  248. }
  249. static void btuart_change_speed(struct btuart_info *info,
  250. unsigned int speed)
  251. {
  252. unsigned long flags;
  253. unsigned int iobase;
  254. int fcr; /* FIFO control reg */
  255. int lcr; /* Line control reg */
  256. int divisor;
  257. if (!info) {
  258. BT_ERR("Unknown device");
  259. return;
  260. }
  261. iobase = info->p_dev->resource[0]->start;
  262. spin_lock_irqsave(&(info->lock), flags);
  263. /* Turn off interrupts */
  264. outb(0, iobase + UART_IER);
  265. divisor = SPEED_MAX / speed;
  266. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
  267. /*
  268. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  269. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  270. * about this timeout since it will always be fast enough.
  271. */
  272. if (speed < 38400)
  273. fcr |= UART_FCR_TRIGGER_1;
  274. else
  275. fcr |= UART_FCR_TRIGGER_14;
  276. /* Bluetooth cards use 8N1 */
  277. lcr = UART_LCR_WLEN8;
  278. outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
  279. outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
  280. outb(divisor >> 8, iobase + UART_DLM);
  281. outb(lcr, iobase + UART_LCR); /* Set 8N1 */
  282. outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
  283. /* Turn on interrupts */
  284. outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  285. spin_unlock_irqrestore(&(info->lock), flags);
  286. }
  287. /* ======================== HCI interface ======================== */
  288. static int btuart_hci_flush(struct hci_dev *hdev)
  289. {
  290. struct btuart_info *info = hci_get_drvdata(hdev);
  291. /* Drop TX queue */
  292. skb_queue_purge(&(info->txq));
  293. return 0;
  294. }
  295. static int btuart_hci_open(struct hci_dev *hdev)
  296. {
  297. return 0;
  298. }
  299. static int btuart_hci_close(struct hci_dev *hdev)
  300. {
  301. btuart_hci_flush(hdev);
  302. return 0;
  303. }
  304. static int btuart_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  305. {
  306. struct btuart_info *info = hci_get_drvdata(hdev);
  307. switch (bt_cb(skb)->pkt_type) {
  308. case HCI_COMMAND_PKT:
  309. hdev->stat.cmd_tx++;
  310. break;
  311. case HCI_ACLDATA_PKT:
  312. hdev->stat.acl_tx++;
  313. break;
  314. case HCI_SCODATA_PKT:
  315. hdev->stat.sco_tx++;
  316. break;
  317. }
  318. /* Prepend skb with frame type */
  319. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  320. skb_queue_tail(&(info->txq), skb);
  321. btuart_write_wakeup(info);
  322. return 0;
  323. }
  324. /* ======================== Card services HCI interaction ======================== */
  325. static int btuart_open(struct btuart_info *info)
  326. {
  327. unsigned long flags;
  328. unsigned int iobase = info->p_dev->resource[0]->start;
  329. struct hci_dev *hdev;
  330. spin_lock_init(&(info->lock));
  331. skb_queue_head_init(&(info->txq));
  332. info->rx_state = RECV_WAIT_PACKET_TYPE;
  333. info->rx_count = 0;
  334. info->rx_skb = NULL;
  335. /* Initialize HCI device */
  336. hdev = hci_alloc_dev();
  337. if (!hdev) {
  338. BT_ERR("Can't allocate HCI device");
  339. return -ENOMEM;
  340. }
  341. info->hdev = hdev;
  342. hdev->bus = HCI_PCCARD;
  343. hci_set_drvdata(hdev, info);
  344. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  345. hdev->open = btuart_hci_open;
  346. hdev->close = btuart_hci_close;
  347. hdev->flush = btuart_hci_flush;
  348. hdev->send = btuart_hci_send_frame;
  349. spin_lock_irqsave(&(info->lock), flags);
  350. /* Reset UART */
  351. outb(0, iobase + UART_MCR);
  352. /* Turn off interrupts */
  353. outb(0, iobase + UART_IER);
  354. /* Initialize UART */
  355. outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
  356. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
  357. /* Turn on interrupts */
  358. // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  359. spin_unlock_irqrestore(&(info->lock), flags);
  360. btuart_change_speed(info, DEFAULT_BAUD_RATE);
  361. /* Timeout before it is safe to send the first HCI packet */
  362. msleep(1000);
  363. /* Register HCI device */
  364. if (hci_register_dev(hdev) < 0) {
  365. BT_ERR("Can't register HCI device");
  366. info->hdev = NULL;
  367. hci_free_dev(hdev);
  368. return -ENODEV;
  369. }
  370. return 0;
  371. }
  372. static int btuart_close(struct btuart_info *info)
  373. {
  374. unsigned long flags;
  375. unsigned int iobase = info->p_dev->resource[0]->start;
  376. struct hci_dev *hdev = info->hdev;
  377. if (!hdev)
  378. return -ENODEV;
  379. btuart_hci_close(hdev);
  380. spin_lock_irqsave(&(info->lock), flags);
  381. /* Reset UART */
  382. outb(0, iobase + UART_MCR);
  383. /* Turn off interrupts */
  384. outb(0, iobase + UART_IER);
  385. spin_unlock_irqrestore(&(info->lock), flags);
  386. hci_unregister_dev(hdev);
  387. hci_free_dev(hdev);
  388. return 0;
  389. }
  390. static int btuart_probe(struct pcmcia_device *link)
  391. {
  392. struct btuart_info *info;
  393. /* Create new info device */
  394. info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
  395. if (!info)
  396. return -ENOMEM;
  397. info->p_dev = link;
  398. link->priv = info;
  399. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
  400. CONF_AUTO_SET_IO;
  401. return btuart_config(link);
  402. }
  403. static void btuart_detach(struct pcmcia_device *link)
  404. {
  405. btuart_release(link);
  406. }
  407. static int btuart_check_config(struct pcmcia_device *p_dev, void *priv_data)
  408. {
  409. int *try = priv_data;
  410. if (!try)
  411. p_dev->io_lines = 16;
  412. if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
  413. return -EINVAL;
  414. p_dev->resource[0]->end = 8;
  415. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  416. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  417. return pcmcia_request_io(p_dev);
  418. }
  419. static int btuart_check_config_notpicky(struct pcmcia_device *p_dev,
  420. void *priv_data)
  421. {
  422. static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
  423. int j;
  424. if (p_dev->io_lines > 3)
  425. return -ENODEV;
  426. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  427. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  428. p_dev->resource[0]->end = 8;
  429. for (j = 0; j < 5; j++) {
  430. p_dev->resource[0]->start = base[j];
  431. p_dev->io_lines = base[j] ? 16 : 3;
  432. if (!pcmcia_request_io(p_dev))
  433. return 0;
  434. }
  435. return -ENODEV;
  436. }
  437. static int btuart_config(struct pcmcia_device *link)
  438. {
  439. struct btuart_info *info = link->priv;
  440. int i;
  441. int try;
  442. /* First pass: look for a config entry that looks normal.
  443. Two tries: without IO aliases, then with aliases */
  444. for (try = 0; try < 2; try++)
  445. if (!pcmcia_loop_config(link, btuart_check_config, &try))
  446. goto found_port;
  447. /* Second pass: try to find an entry that isn't picky about
  448. its base address, then try to grab any standard serial port
  449. address, and finally try to get any free port. */
  450. if (!pcmcia_loop_config(link, btuart_check_config_notpicky, NULL))
  451. goto found_port;
  452. BT_ERR("No usable port range found");
  453. goto failed;
  454. found_port:
  455. i = pcmcia_request_irq(link, btuart_interrupt);
  456. if (i != 0)
  457. goto failed;
  458. i = pcmcia_enable_device(link);
  459. if (i != 0)
  460. goto failed;
  461. if (btuart_open(info) != 0)
  462. goto failed;
  463. return 0;
  464. failed:
  465. btuart_release(link);
  466. return -ENODEV;
  467. }
  468. static void btuart_release(struct pcmcia_device *link)
  469. {
  470. struct btuart_info *info = link->priv;
  471. btuart_close(info);
  472. pcmcia_disable_device(link);
  473. }
  474. static const struct pcmcia_device_id btuart_ids[] = {
  475. /* don't use this driver. Use serial_cs + hci_uart instead */
  476. PCMCIA_DEVICE_NULL
  477. };
  478. MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
  479. static struct pcmcia_driver btuart_driver = {
  480. .owner = THIS_MODULE,
  481. .name = "btuart_cs",
  482. .probe = btuart_probe,
  483. .remove = btuart_detach,
  484. .id_table = btuart_ids,
  485. };
  486. module_pcmcia_driver(btuart_driver);