btuart_cs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. hci_skb_pkt_type(info->rx_skb) = inb(iobase + UART_RX);
  148. switch (hci_skb_pkt_type(info->rx_skb)) {
  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",
  164. hci_skb_pkt_type(info->rx_skb));
  165. info->hdev->stat.err_rx++;
  166. kfree_skb(info->rx_skb);
  167. info->rx_skb = NULL;
  168. break;
  169. }
  170. } else {
  171. skb_put_u8(info->rx_skb, inb(iobase + UART_RX));
  172. info->rx_count--;
  173. if (info->rx_count == 0) {
  174. int dlen;
  175. struct hci_event_hdr *eh;
  176. struct hci_acl_hdr *ah;
  177. struct hci_sco_hdr *sh;
  178. switch (info->rx_state) {
  179. case RECV_WAIT_EVENT_HEADER:
  180. eh = hci_event_hdr(info->rx_skb);
  181. info->rx_state = RECV_WAIT_DATA;
  182. info->rx_count = eh->plen;
  183. break;
  184. case RECV_WAIT_ACL_HEADER:
  185. ah = hci_acl_hdr(info->rx_skb);
  186. dlen = __le16_to_cpu(ah->dlen);
  187. info->rx_state = RECV_WAIT_DATA;
  188. info->rx_count = dlen;
  189. break;
  190. case RECV_WAIT_SCO_HEADER:
  191. sh = hci_sco_hdr(info->rx_skb);
  192. info->rx_state = RECV_WAIT_DATA;
  193. info->rx_count = sh->dlen;
  194. break;
  195. case RECV_WAIT_DATA:
  196. hci_recv_frame(info->hdev, info->rx_skb);
  197. info->rx_skb = NULL;
  198. break;
  199. }
  200. }
  201. }
  202. /* Make sure we don't stay here too long */
  203. if (boguscount++ > 16)
  204. break;
  205. } while (inb(iobase + UART_LSR) & UART_LSR_DR);
  206. }
  207. static irqreturn_t btuart_interrupt(int irq, void *dev_inst)
  208. {
  209. struct btuart_info *info = dev_inst;
  210. unsigned int iobase;
  211. int boguscount = 0;
  212. int iir, lsr;
  213. irqreturn_t r = IRQ_NONE;
  214. if (!info || !info->hdev)
  215. /* our irq handler is shared */
  216. return IRQ_NONE;
  217. iobase = info->p_dev->resource[0]->start;
  218. spin_lock(&(info->lock));
  219. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  220. while (iir) {
  221. r = IRQ_HANDLED;
  222. /* Clear interrupt */
  223. lsr = inb(iobase + UART_LSR);
  224. switch (iir) {
  225. case UART_IIR_RLSI:
  226. BT_ERR("RLSI");
  227. break;
  228. case UART_IIR_RDI:
  229. /* Receive interrupt */
  230. btuart_receive(info);
  231. break;
  232. case UART_IIR_THRI:
  233. if (lsr & UART_LSR_THRE) {
  234. /* Transmitter ready for data */
  235. btuart_write_wakeup(info);
  236. }
  237. break;
  238. default:
  239. BT_ERR("Unhandled IIR=%#x", iir);
  240. break;
  241. }
  242. /* Make sure we don't stay here too long */
  243. if (boguscount++ > 100)
  244. break;
  245. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  246. }
  247. spin_unlock(&(info->lock));
  248. return r;
  249. }
  250. static void btuart_change_speed(struct btuart_info *info,
  251. unsigned int speed)
  252. {
  253. unsigned long flags;
  254. unsigned int iobase;
  255. int fcr; /* FIFO control reg */
  256. int lcr; /* Line control reg */
  257. int divisor;
  258. if (!info) {
  259. BT_ERR("Unknown device");
  260. return;
  261. }
  262. iobase = info->p_dev->resource[0]->start;
  263. spin_lock_irqsave(&(info->lock), flags);
  264. /* Turn off interrupts */
  265. outb(0, iobase + UART_IER);
  266. divisor = SPEED_MAX / speed;
  267. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
  268. /*
  269. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  270. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  271. * about this timeout since it will always be fast enough.
  272. */
  273. if (speed < 38400)
  274. fcr |= UART_FCR_TRIGGER_1;
  275. else
  276. fcr |= UART_FCR_TRIGGER_14;
  277. /* Bluetooth cards use 8N1 */
  278. lcr = UART_LCR_WLEN8;
  279. outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
  280. outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
  281. outb(divisor >> 8, iobase + UART_DLM);
  282. outb(lcr, iobase + UART_LCR); /* Set 8N1 */
  283. outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
  284. /* Turn on interrupts */
  285. outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  286. spin_unlock_irqrestore(&(info->lock), flags);
  287. }
  288. /* ======================== HCI interface ======================== */
  289. static int btuart_hci_flush(struct hci_dev *hdev)
  290. {
  291. struct btuart_info *info = hci_get_drvdata(hdev);
  292. /* Drop TX queue */
  293. skb_queue_purge(&(info->txq));
  294. return 0;
  295. }
  296. static int btuart_hci_open(struct hci_dev *hdev)
  297. {
  298. return 0;
  299. }
  300. static int btuart_hci_close(struct hci_dev *hdev)
  301. {
  302. btuart_hci_flush(hdev);
  303. return 0;
  304. }
  305. static int btuart_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  306. {
  307. struct btuart_info *info = hci_get_drvdata(hdev);
  308. switch (hci_skb_pkt_type(skb)) {
  309. case HCI_COMMAND_PKT:
  310. hdev->stat.cmd_tx++;
  311. break;
  312. case HCI_ACLDATA_PKT:
  313. hdev->stat.acl_tx++;
  314. break;
  315. case HCI_SCODATA_PKT:
  316. hdev->stat.sco_tx++;
  317. break;
  318. }
  319. /* Prepend skb with frame type */
  320. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  321. skb_queue_tail(&(info->txq), skb);
  322. btuart_write_wakeup(info);
  323. return 0;
  324. }
  325. /* ======================== Card services HCI interaction ======================== */
  326. static int btuart_open(struct btuart_info *info)
  327. {
  328. unsigned long flags;
  329. unsigned int iobase = info->p_dev->resource[0]->start;
  330. struct hci_dev *hdev;
  331. spin_lock_init(&(info->lock));
  332. skb_queue_head_init(&(info->txq));
  333. info->rx_state = RECV_WAIT_PACKET_TYPE;
  334. info->rx_count = 0;
  335. info->rx_skb = NULL;
  336. /* Initialize HCI device */
  337. hdev = hci_alloc_dev();
  338. if (!hdev) {
  339. BT_ERR("Can't allocate HCI device");
  340. return -ENOMEM;
  341. }
  342. info->hdev = hdev;
  343. hdev->bus = HCI_PCCARD;
  344. hci_set_drvdata(hdev, info);
  345. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  346. hdev->open = btuart_hci_open;
  347. hdev->close = btuart_hci_close;
  348. hdev->flush = btuart_hci_flush;
  349. hdev->send = btuart_hci_send_frame;
  350. spin_lock_irqsave(&(info->lock), flags);
  351. /* Reset UART */
  352. outb(0, iobase + UART_MCR);
  353. /* Turn off interrupts */
  354. outb(0, iobase + UART_IER);
  355. /* Initialize UART */
  356. outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
  357. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
  358. /* Turn on interrupts */
  359. // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  360. spin_unlock_irqrestore(&(info->lock), flags);
  361. btuart_change_speed(info, DEFAULT_BAUD_RATE);
  362. /* Timeout before it is safe to send the first HCI packet */
  363. msleep(1000);
  364. /* Register HCI device */
  365. if (hci_register_dev(hdev) < 0) {
  366. BT_ERR("Can't register HCI device");
  367. info->hdev = NULL;
  368. hci_free_dev(hdev);
  369. return -ENODEV;
  370. }
  371. return 0;
  372. }
  373. static int btuart_close(struct btuart_info *info)
  374. {
  375. unsigned long flags;
  376. unsigned int iobase = info->p_dev->resource[0]->start;
  377. struct hci_dev *hdev = info->hdev;
  378. if (!hdev)
  379. return -ENODEV;
  380. btuart_hci_close(hdev);
  381. spin_lock_irqsave(&(info->lock), flags);
  382. /* Reset UART */
  383. outb(0, iobase + UART_MCR);
  384. /* Turn off interrupts */
  385. outb(0, iobase + UART_IER);
  386. spin_unlock_irqrestore(&(info->lock), flags);
  387. hci_unregister_dev(hdev);
  388. hci_free_dev(hdev);
  389. return 0;
  390. }
  391. static int btuart_probe(struct pcmcia_device *link)
  392. {
  393. struct btuart_info *info;
  394. /* Create new info device */
  395. info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
  396. if (!info)
  397. return -ENOMEM;
  398. info->p_dev = link;
  399. link->priv = info;
  400. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
  401. CONF_AUTO_SET_IO;
  402. return btuart_config(link);
  403. }
  404. static void btuart_detach(struct pcmcia_device *link)
  405. {
  406. btuart_release(link);
  407. }
  408. static int btuart_check_config(struct pcmcia_device *p_dev, void *priv_data)
  409. {
  410. int *try = priv_data;
  411. if (!try)
  412. p_dev->io_lines = 16;
  413. if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
  414. return -EINVAL;
  415. p_dev->resource[0]->end = 8;
  416. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  417. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  418. return pcmcia_request_io(p_dev);
  419. }
  420. static int btuart_check_config_notpicky(struct pcmcia_device *p_dev,
  421. void *priv_data)
  422. {
  423. static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
  424. int j;
  425. if (p_dev->io_lines > 3)
  426. return -ENODEV;
  427. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  428. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  429. p_dev->resource[0]->end = 8;
  430. for (j = 0; j < 5; j++) {
  431. p_dev->resource[0]->start = base[j];
  432. p_dev->io_lines = base[j] ? 16 : 3;
  433. if (!pcmcia_request_io(p_dev))
  434. return 0;
  435. }
  436. return -ENODEV;
  437. }
  438. static int btuart_config(struct pcmcia_device *link)
  439. {
  440. struct btuart_info *info = link->priv;
  441. int i;
  442. int try;
  443. /* First pass: look for a config entry that looks normal.
  444. * Two tries: without IO aliases, then with aliases
  445. */
  446. for (try = 0; try < 2; try++)
  447. if (!pcmcia_loop_config(link, btuart_check_config, &try))
  448. goto found_port;
  449. /* Second pass: try to find an entry that isn't picky about
  450. * its base address, then try to grab any standard serial port
  451. * address, and finally try to get any free port.
  452. */
  453. if (!pcmcia_loop_config(link, btuart_check_config_notpicky, NULL))
  454. goto found_port;
  455. BT_ERR("No usable port range found");
  456. goto failed;
  457. found_port:
  458. i = pcmcia_request_irq(link, btuart_interrupt);
  459. if (i != 0)
  460. goto failed;
  461. i = pcmcia_enable_device(link);
  462. if (i != 0)
  463. goto failed;
  464. if (btuart_open(info) != 0)
  465. goto failed;
  466. return 0;
  467. failed:
  468. btuart_release(link);
  469. return -ENODEV;
  470. }
  471. static void btuart_release(struct pcmcia_device *link)
  472. {
  473. struct btuart_info *info = link->priv;
  474. btuart_close(info);
  475. pcmcia_disable_device(link);
  476. }
  477. static const struct pcmcia_device_id btuart_ids[] = {
  478. /* don't use this driver. Use serial_cs + hci_uart instead */
  479. PCMCIA_DEVICE_NULL
  480. };
  481. MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
  482. static struct pcmcia_driver btuart_driver = {
  483. .owner = THIS_MODULE,
  484. .name = "btuart_cs",
  485. .probe = btuart_probe,
  486. .remove = btuart_detach,
  487. .id_table = btuart_ids,
  488. };
  489. module_pcmcia_driver(btuart_driver);