dtl1_cs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. *
  3. * A driver for Nokia Connectivity Card DTL-1 devices
  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 <asm/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 Nokia Connectivity Card DTL-1");
  48. MODULE_LICENSE("GPL");
  49. /* ======================== Local structures ======================== */
  50. struct dtl1_info {
  51. struct pcmcia_device *p_dev;
  52. struct hci_dev *hdev;
  53. spinlock_t lock; /* For serializing operations */
  54. unsigned long flowmask; /* HCI flow mask */
  55. int ri_latch;
  56. struct sk_buff_head txq;
  57. unsigned long tx_state;
  58. unsigned long rx_state;
  59. unsigned long rx_count;
  60. struct sk_buff *rx_skb;
  61. };
  62. static int dtl1_config(struct pcmcia_device *link);
  63. /* Transmit states */
  64. #define XMIT_SENDING 1
  65. #define XMIT_WAKEUP 2
  66. #define XMIT_WAITING 8
  67. /* Receiver States */
  68. #define RECV_WAIT_NSH 0
  69. #define RECV_WAIT_DATA 1
  70. struct nsh {
  71. u8 type;
  72. u8 zero;
  73. u16 len;
  74. } __packed; /* Nokia Specific Header */
  75. #define NSHL 4 /* Nokia Specific Header Length */
  76. /* ======================== Interrupt handling ======================== */
  77. static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
  78. {
  79. int actual = 0;
  80. /* Tx FIFO should be empty */
  81. if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
  82. return 0;
  83. /* Fill FIFO with current frame */
  84. while ((fifo_size-- > 0) && (actual < len)) {
  85. /* Transmit next byte */
  86. outb(buf[actual], iobase + UART_TX);
  87. actual++;
  88. }
  89. return actual;
  90. }
  91. static void dtl1_write_wakeup(struct dtl1_info *info)
  92. {
  93. if (!info) {
  94. BT_ERR("Unknown device");
  95. return;
  96. }
  97. if (test_bit(XMIT_WAITING, &(info->tx_state))) {
  98. set_bit(XMIT_WAKEUP, &(info->tx_state));
  99. return;
  100. }
  101. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  102. set_bit(XMIT_WAKEUP, &(info->tx_state));
  103. return;
  104. }
  105. do {
  106. unsigned int iobase = info->p_dev->resource[0]->start;
  107. register struct sk_buff *skb;
  108. int len;
  109. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  110. if (!pcmcia_dev_present(info->p_dev))
  111. return;
  112. skb = skb_dequeue(&(info->txq));
  113. if (!skb)
  114. break;
  115. /* Send frame */
  116. len = dtl1_write(iobase, 32, skb->data, skb->len);
  117. if (len == skb->len) {
  118. set_bit(XMIT_WAITING, &(info->tx_state));
  119. kfree_skb(skb);
  120. } else {
  121. skb_pull(skb, len);
  122. skb_queue_head(&(info->txq), skb);
  123. }
  124. info->hdev->stat.byte_tx += len;
  125. } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
  126. clear_bit(XMIT_SENDING, &(info->tx_state));
  127. }
  128. static void dtl1_control(struct dtl1_info *info, struct sk_buff *skb)
  129. {
  130. u8 flowmask = *(u8 *)skb->data;
  131. int i;
  132. printk(KERN_INFO "Bluetooth: Nokia control data =");
  133. for (i = 0; i < skb->len; i++) {
  134. printk(" %02x", skb->data[i]);
  135. }
  136. printk("\n");
  137. /* transition to active state */
  138. if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
  139. clear_bit(XMIT_WAITING, &(info->tx_state));
  140. dtl1_write_wakeup(info);
  141. }
  142. info->flowmask = flowmask;
  143. kfree_skb(skb);
  144. }
  145. static void dtl1_receive(struct dtl1_info *info)
  146. {
  147. unsigned int iobase;
  148. struct nsh *nsh;
  149. int boguscount = 0;
  150. if (!info) {
  151. BT_ERR("Unknown device");
  152. return;
  153. }
  154. iobase = info->p_dev->resource[0]->start;
  155. do {
  156. info->hdev->stat.byte_rx++;
  157. /* Allocate packet */
  158. if (info->rx_skb == NULL) {
  159. info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  160. if (!info->rx_skb) {
  161. BT_ERR("Can't allocate mem for new packet");
  162. info->rx_state = RECV_WAIT_NSH;
  163. info->rx_count = NSHL;
  164. return;
  165. }
  166. }
  167. *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
  168. nsh = (struct nsh *)info->rx_skb->data;
  169. info->rx_count--;
  170. if (info->rx_count == 0) {
  171. switch (info->rx_state) {
  172. case RECV_WAIT_NSH:
  173. info->rx_state = RECV_WAIT_DATA;
  174. info->rx_count = nsh->len + (nsh->len & 0x0001);
  175. break;
  176. case RECV_WAIT_DATA:
  177. bt_cb(info->rx_skb)->pkt_type = nsh->type;
  178. /* remove PAD byte if it exists */
  179. if (nsh->len & 0x0001) {
  180. info->rx_skb->tail--;
  181. info->rx_skb->len--;
  182. }
  183. /* remove NSH */
  184. skb_pull(info->rx_skb, NSHL);
  185. switch (bt_cb(info->rx_skb)->pkt_type) {
  186. case 0x80:
  187. /* control data for the Nokia Card */
  188. dtl1_control(info, info->rx_skb);
  189. break;
  190. case 0x82:
  191. case 0x83:
  192. case 0x84:
  193. /* send frame to the HCI layer */
  194. bt_cb(info->rx_skb)->pkt_type &= 0x0f;
  195. hci_recv_frame(info->hdev, info->rx_skb);
  196. break;
  197. default:
  198. /* unknown packet */
  199. BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
  200. kfree_skb(info->rx_skb);
  201. break;
  202. }
  203. info->rx_state = RECV_WAIT_NSH;
  204. info->rx_count = NSHL;
  205. info->rx_skb = NULL;
  206. break;
  207. }
  208. }
  209. /* Make sure we don't stay here too long */
  210. if (boguscount++ > 32)
  211. break;
  212. } while (inb(iobase + UART_LSR) & UART_LSR_DR);
  213. }
  214. static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
  215. {
  216. struct dtl1_info *info = dev_inst;
  217. unsigned int iobase;
  218. unsigned char msr;
  219. int boguscount = 0;
  220. int iir, lsr;
  221. irqreturn_t r = IRQ_NONE;
  222. if (!info || !info->hdev)
  223. /* our irq handler is shared */
  224. return IRQ_NONE;
  225. iobase = info->p_dev->resource[0]->start;
  226. spin_lock(&(info->lock));
  227. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  228. while (iir) {
  229. r = IRQ_HANDLED;
  230. /* Clear interrupt */
  231. lsr = inb(iobase + UART_LSR);
  232. switch (iir) {
  233. case UART_IIR_RLSI:
  234. BT_ERR("RLSI");
  235. break;
  236. case UART_IIR_RDI:
  237. /* Receive interrupt */
  238. dtl1_receive(info);
  239. break;
  240. case UART_IIR_THRI:
  241. if (lsr & UART_LSR_THRE) {
  242. /* Transmitter ready for data */
  243. dtl1_write_wakeup(info);
  244. }
  245. break;
  246. default:
  247. BT_ERR("Unhandled IIR=%#x", iir);
  248. break;
  249. }
  250. /* Make sure we don't stay here too long */
  251. if (boguscount++ > 100)
  252. break;
  253. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  254. }
  255. msr = inb(iobase + UART_MSR);
  256. if (info->ri_latch ^ (msr & UART_MSR_RI)) {
  257. info->ri_latch = msr & UART_MSR_RI;
  258. clear_bit(XMIT_WAITING, &(info->tx_state));
  259. dtl1_write_wakeup(info);
  260. r = IRQ_HANDLED;
  261. }
  262. spin_unlock(&(info->lock));
  263. return r;
  264. }
  265. /* ======================== HCI interface ======================== */
  266. static int dtl1_hci_open(struct hci_dev *hdev)
  267. {
  268. set_bit(HCI_RUNNING, &(hdev->flags));
  269. return 0;
  270. }
  271. static int dtl1_hci_flush(struct hci_dev *hdev)
  272. {
  273. struct dtl1_info *info = hci_get_drvdata(hdev);
  274. /* Drop TX queue */
  275. skb_queue_purge(&(info->txq));
  276. return 0;
  277. }
  278. static int dtl1_hci_close(struct hci_dev *hdev)
  279. {
  280. if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
  281. return 0;
  282. dtl1_hci_flush(hdev);
  283. return 0;
  284. }
  285. static int dtl1_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  286. {
  287. struct dtl1_info *info = hci_get_drvdata(hdev);
  288. struct sk_buff *s;
  289. struct nsh nsh;
  290. switch (bt_cb(skb)->pkt_type) {
  291. case HCI_COMMAND_PKT:
  292. hdev->stat.cmd_tx++;
  293. nsh.type = 0x81;
  294. break;
  295. case HCI_ACLDATA_PKT:
  296. hdev->stat.acl_tx++;
  297. nsh.type = 0x82;
  298. break;
  299. case HCI_SCODATA_PKT:
  300. hdev->stat.sco_tx++;
  301. nsh.type = 0x83;
  302. break;
  303. default:
  304. return -EILSEQ;
  305. };
  306. nsh.zero = 0;
  307. nsh.len = skb->len;
  308. s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
  309. if (!s)
  310. return -ENOMEM;
  311. skb_reserve(s, NSHL);
  312. skb_copy_from_linear_data(skb, skb_put(s, skb->len), skb->len);
  313. if (skb->len & 0x0001)
  314. *skb_put(s, 1) = 0; /* PAD */
  315. /* Prepend skb with Nokia frame header and queue */
  316. memcpy(skb_push(s, NSHL), &nsh, NSHL);
  317. skb_queue_tail(&(info->txq), s);
  318. dtl1_write_wakeup(info);
  319. kfree_skb(skb);
  320. return 0;
  321. }
  322. /* ======================== Card services HCI interaction ======================== */
  323. static int dtl1_open(struct dtl1_info *info)
  324. {
  325. unsigned long flags;
  326. unsigned int iobase = info->p_dev->resource[0]->start;
  327. struct hci_dev *hdev;
  328. spin_lock_init(&(info->lock));
  329. skb_queue_head_init(&(info->txq));
  330. info->rx_state = RECV_WAIT_NSH;
  331. info->rx_count = NSHL;
  332. info->rx_skb = NULL;
  333. set_bit(XMIT_WAITING, &(info->tx_state));
  334. /* Initialize HCI device */
  335. hdev = hci_alloc_dev();
  336. if (!hdev) {
  337. BT_ERR("Can't allocate HCI device");
  338. return -ENOMEM;
  339. }
  340. info->hdev = hdev;
  341. hdev->bus = HCI_PCCARD;
  342. hci_set_drvdata(hdev, info);
  343. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  344. hdev->open = dtl1_hci_open;
  345. hdev->close = dtl1_hci_close;
  346. hdev->flush = dtl1_hci_flush;
  347. hdev->send = dtl1_hci_send_frame;
  348. spin_lock_irqsave(&(info->lock), flags);
  349. /* Reset UART */
  350. outb(0, iobase + UART_MCR);
  351. /* Turn off interrupts */
  352. outb(0, iobase + UART_IER);
  353. /* Initialize UART */
  354. outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
  355. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
  356. info->ri_latch = inb(info->p_dev->resource[0]->start + UART_MSR)
  357. & UART_MSR_RI;
  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. /* Timeout before it is safe to send the first HCI packet */
  362. msleep(2000);
  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 dtl1_close(struct dtl1_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. dtl1_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 dtl1_probe(struct pcmcia_device *link)
  391. {
  392. struct dtl1_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_IO;
  400. return dtl1_config(link);
  401. }
  402. static void dtl1_detach(struct pcmcia_device *link)
  403. {
  404. struct dtl1_info *info = link->priv;
  405. dtl1_close(info);
  406. pcmcia_disable_device(link);
  407. }
  408. static int dtl1_confcheck(struct pcmcia_device *p_dev, void *priv_data)
  409. {
  410. if ((p_dev->resource[1]->end) || (p_dev->resource[1]->end < 8))
  411. return -ENODEV;
  412. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  413. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  414. return pcmcia_request_io(p_dev);
  415. }
  416. static int dtl1_config(struct pcmcia_device *link)
  417. {
  418. struct dtl1_info *info = link->priv;
  419. int ret;
  420. /* Look for a generic full-sized window */
  421. link->resource[0]->end = 8;
  422. ret = pcmcia_loop_config(link, dtl1_confcheck, NULL);
  423. if (ret)
  424. goto failed;
  425. ret = pcmcia_request_irq(link, dtl1_interrupt);
  426. if (ret)
  427. goto failed;
  428. ret = pcmcia_enable_device(link);
  429. if (ret)
  430. goto failed;
  431. ret = dtl1_open(info);
  432. if (ret)
  433. goto failed;
  434. return 0;
  435. failed:
  436. dtl1_detach(link);
  437. return ret;
  438. }
  439. static const struct pcmcia_device_id dtl1_ids[] = {
  440. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
  441. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
  442. PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
  443. PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
  444. PCMCIA_DEVICE_NULL
  445. };
  446. MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
  447. static struct pcmcia_driver dtl1_driver = {
  448. .owner = THIS_MODULE,
  449. .name = "dtl1_cs",
  450. .probe = dtl1_probe,
  451. .remove = dtl1_detach,
  452. .id_table = dtl1_ids,
  453. };
  454. module_pcmcia_driver(dtl1_driver);