dtl1_cs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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/system.h>
  39. #include <asm/io.h>
  40. #include <pcmcia/cistpl.h>
  41. #include <pcmcia/ciscode.h>
  42. #include <pcmcia/ds.h>
  43. #include <pcmcia/cisreg.h>
  44. #include <net/bluetooth/bluetooth.h>
  45. #include <net/bluetooth/hci_core.h>
  46. /* ======================== Module parameters ======================== */
  47. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  48. MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
  49. MODULE_LICENSE("GPL");
  50. /* ======================== Local structures ======================== */
  51. typedef struct dtl1_info_t {
  52. struct pcmcia_device *p_dev;
  53. struct hci_dev *hdev;
  54. spinlock_t lock; /* For serializing operations */
  55. unsigned long flowmask; /* HCI flow mask */
  56. int ri_latch;
  57. struct sk_buff_head txq;
  58. unsigned long tx_state;
  59. unsigned long rx_state;
  60. unsigned long rx_count;
  61. struct sk_buff *rx_skb;
  62. } dtl1_info_t;
  63. static int dtl1_config(struct pcmcia_device *link);
  64. /* Transmit states */
  65. #define XMIT_SENDING 1
  66. #define XMIT_WAKEUP 2
  67. #define XMIT_WAITING 8
  68. /* Receiver States */
  69. #define RECV_WAIT_NSH 0
  70. #define RECV_WAIT_DATA 1
  71. typedef struct {
  72. u8 type;
  73. u8 zero;
  74. u16 len;
  75. } __packed nsh_t; /* Nokia Specific Header */
  76. #define NSHL 4 /* Nokia Specific Header Length */
  77. /* ======================== Interrupt handling ======================== */
  78. static int dtl1_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 dtl1_write_wakeup(dtl1_info_t *info)
  93. {
  94. if (!info) {
  95. BT_ERR("Unknown device");
  96. return;
  97. }
  98. if (test_bit(XMIT_WAITING, &(info->tx_state))) {
  99. set_bit(XMIT_WAKEUP, &(info->tx_state));
  100. return;
  101. }
  102. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  103. set_bit(XMIT_WAKEUP, &(info->tx_state));
  104. return;
  105. }
  106. do {
  107. register unsigned int iobase = info->p_dev->resource[0]->start;
  108. register struct sk_buff *skb;
  109. register int len;
  110. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  111. if (!pcmcia_dev_present(info->p_dev))
  112. return;
  113. if (!(skb = skb_dequeue(&(info->txq))))
  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(dtl1_info_t *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(dtl1_info_t *info)
  146. {
  147. unsigned int iobase;
  148. nsh_t *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. if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  160. BT_ERR("Can't allocate mem for new packet");
  161. info->rx_state = RECV_WAIT_NSH;
  162. info->rx_count = NSHL;
  163. return;
  164. }
  165. *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
  166. nsh = (nsh_t *)info->rx_skb->data;
  167. info->rx_count--;
  168. if (info->rx_count == 0) {
  169. switch (info->rx_state) {
  170. case RECV_WAIT_NSH:
  171. info->rx_state = RECV_WAIT_DATA;
  172. info->rx_count = nsh->len + (nsh->len & 0x0001);
  173. break;
  174. case RECV_WAIT_DATA:
  175. bt_cb(info->rx_skb)->pkt_type = nsh->type;
  176. /* remove PAD byte if it exists */
  177. if (nsh->len & 0x0001) {
  178. info->rx_skb->tail--;
  179. info->rx_skb->len--;
  180. }
  181. /* remove NSH */
  182. skb_pull(info->rx_skb, NSHL);
  183. switch (bt_cb(info->rx_skb)->pkt_type) {
  184. case 0x80:
  185. /* control data for the Nokia Card */
  186. dtl1_control(info, info->rx_skb);
  187. break;
  188. case 0x82:
  189. case 0x83:
  190. case 0x84:
  191. /* send frame to the HCI layer */
  192. info->rx_skb->dev = (void *) info->hdev;
  193. bt_cb(info->rx_skb)->pkt_type &= 0x0f;
  194. hci_recv_frame(info->rx_skb);
  195. break;
  196. default:
  197. /* unknown packet */
  198. BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
  199. kfree_skb(info->rx_skb);
  200. break;
  201. }
  202. info->rx_state = RECV_WAIT_NSH;
  203. info->rx_count = NSHL;
  204. info->rx_skb = NULL;
  205. break;
  206. }
  207. }
  208. /* Make sure we don't stay here too long */
  209. if (boguscount++ > 32)
  210. break;
  211. } while (inb(iobase + UART_LSR) & UART_LSR_DR);
  212. }
  213. static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
  214. {
  215. dtl1_info_t *info = dev_inst;
  216. unsigned int iobase;
  217. unsigned char msr;
  218. int boguscount = 0;
  219. int iir, lsr;
  220. irqreturn_t r = IRQ_NONE;
  221. if (!info || !info->hdev)
  222. /* our irq handler is shared */
  223. return IRQ_NONE;
  224. iobase = info->p_dev->resource[0]->start;
  225. spin_lock(&(info->lock));
  226. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  227. while (iir) {
  228. r = IRQ_HANDLED;
  229. /* Clear interrupt */
  230. lsr = inb(iobase + UART_LSR);
  231. switch (iir) {
  232. case UART_IIR_RLSI:
  233. BT_ERR("RLSI");
  234. break;
  235. case UART_IIR_RDI:
  236. /* Receive interrupt */
  237. dtl1_receive(info);
  238. break;
  239. case UART_IIR_THRI:
  240. if (lsr & UART_LSR_THRE) {
  241. /* Transmitter ready for data */
  242. dtl1_write_wakeup(info);
  243. }
  244. break;
  245. default:
  246. BT_ERR("Unhandled IIR=%#x", iir);
  247. break;
  248. }
  249. /* Make sure we don't stay here too long */
  250. if (boguscount++ > 100)
  251. break;
  252. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  253. }
  254. msr = inb(iobase + UART_MSR);
  255. if (info->ri_latch ^ (msr & UART_MSR_RI)) {
  256. info->ri_latch = msr & UART_MSR_RI;
  257. clear_bit(XMIT_WAITING, &(info->tx_state));
  258. dtl1_write_wakeup(info);
  259. r = IRQ_HANDLED;
  260. }
  261. spin_unlock(&(info->lock));
  262. return r;
  263. }
  264. /* ======================== HCI interface ======================== */
  265. static int dtl1_hci_open(struct hci_dev *hdev)
  266. {
  267. set_bit(HCI_RUNNING, &(hdev->flags));
  268. return 0;
  269. }
  270. static int dtl1_hci_flush(struct hci_dev *hdev)
  271. {
  272. dtl1_info_t *info = hci_get_drvdata(hdev);
  273. /* Drop TX queue */
  274. skb_queue_purge(&(info->txq));
  275. return 0;
  276. }
  277. static int dtl1_hci_close(struct hci_dev *hdev)
  278. {
  279. if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
  280. return 0;
  281. dtl1_hci_flush(hdev);
  282. return 0;
  283. }
  284. static int dtl1_hci_send_frame(struct sk_buff *skb)
  285. {
  286. dtl1_info_t *info;
  287. struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
  288. struct sk_buff *s;
  289. nsh_t nsh;
  290. if (!hdev) {
  291. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  292. return -ENODEV;
  293. }
  294. info = hci_get_drvdata(hdev);
  295. switch (bt_cb(skb)->pkt_type) {
  296. case HCI_COMMAND_PKT:
  297. hdev->stat.cmd_tx++;
  298. nsh.type = 0x81;
  299. break;
  300. case HCI_ACLDATA_PKT:
  301. hdev->stat.acl_tx++;
  302. nsh.type = 0x82;
  303. break;
  304. case HCI_SCODATA_PKT:
  305. hdev->stat.sco_tx++;
  306. nsh.type = 0x83;
  307. break;
  308. default:
  309. return -EILSEQ;
  310. };
  311. nsh.zero = 0;
  312. nsh.len = skb->len;
  313. s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
  314. if (!s)
  315. return -ENOMEM;
  316. skb_reserve(s, NSHL);
  317. skb_copy_from_linear_data(skb, skb_put(s, skb->len), skb->len);
  318. if (skb->len & 0x0001)
  319. *skb_put(s, 1) = 0; /* PAD */
  320. /* Prepend skb with Nokia frame header and queue */
  321. memcpy(skb_push(s, NSHL), &nsh, NSHL);
  322. skb_queue_tail(&(info->txq), s);
  323. dtl1_write_wakeup(info);
  324. kfree_skb(skb);
  325. return 0;
  326. }
  327. static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
  328. {
  329. return -ENOIOCTLCMD;
  330. }
  331. /* ======================== Card services HCI interaction ======================== */
  332. static int dtl1_open(dtl1_info_t *info)
  333. {
  334. unsigned long flags;
  335. unsigned int iobase = info->p_dev->resource[0]->start;
  336. struct hci_dev *hdev;
  337. spin_lock_init(&(info->lock));
  338. skb_queue_head_init(&(info->txq));
  339. info->rx_state = RECV_WAIT_NSH;
  340. info->rx_count = NSHL;
  341. info->rx_skb = NULL;
  342. set_bit(XMIT_WAITING, &(info->tx_state));
  343. /* Initialize HCI device */
  344. hdev = hci_alloc_dev();
  345. if (!hdev) {
  346. BT_ERR("Can't allocate HCI device");
  347. return -ENOMEM;
  348. }
  349. info->hdev = hdev;
  350. hdev->bus = HCI_PCCARD;
  351. hci_set_drvdata(hdev, info);
  352. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  353. hdev->open = dtl1_hci_open;
  354. hdev->close = dtl1_hci_close;
  355. hdev->flush = dtl1_hci_flush;
  356. hdev->send = dtl1_hci_send_frame;
  357. hdev->ioctl = dtl1_hci_ioctl;
  358. spin_lock_irqsave(&(info->lock), flags);
  359. /* Reset UART */
  360. outb(0, iobase + UART_MCR);
  361. /* Turn off interrupts */
  362. outb(0, iobase + UART_IER);
  363. /* Initialize UART */
  364. outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
  365. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
  366. info->ri_latch = inb(info->p_dev->resource[0]->start + UART_MSR)
  367. & UART_MSR_RI;
  368. /* Turn on interrupts */
  369. outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  370. spin_unlock_irqrestore(&(info->lock), flags);
  371. /* Timeout before it is safe to send the first HCI packet */
  372. msleep(2000);
  373. /* Register HCI device */
  374. if (hci_register_dev(hdev) < 0) {
  375. BT_ERR("Can't register HCI device");
  376. info->hdev = NULL;
  377. hci_free_dev(hdev);
  378. return -ENODEV;
  379. }
  380. return 0;
  381. }
  382. static int dtl1_close(dtl1_info_t *info)
  383. {
  384. unsigned long flags;
  385. unsigned int iobase = info->p_dev->resource[0]->start;
  386. struct hci_dev *hdev = info->hdev;
  387. if (!hdev)
  388. return -ENODEV;
  389. dtl1_hci_close(hdev);
  390. spin_lock_irqsave(&(info->lock), flags);
  391. /* Reset UART */
  392. outb(0, iobase + UART_MCR);
  393. /* Turn off interrupts */
  394. outb(0, iobase + UART_IER);
  395. spin_unlock_irqrestore(&(info->lock), flags);
  396. hci_unregister_dev(hdev);
  397. hci_free_dev(hdev);
  398. return 0;
  399. }
  400. static int dtl1_probe(struct pcmcia_device *link)
  401. {
  402. dtl1_info_t *info;
  403. /* Create new info device */
  404. info = kzalloc(sizeof(*info), GFP_KERNEL);
  405. if (!info)
  406. return -ENOMEM;
  407. info->p_dev = link;
  408. link->priv = info;
  409. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
  410. return dtl1_config(link);
  411. }
  412. static void dtl1_detach(struct pcmcia_device *link)
  413. {
  414. dtl1_info_t *info = link->priv;
  415. dtl1_close(info);
  416. pcmcia_disable_device(link);
  417. kfree(info);
  418. }
  419. static int dtl1_confcheck(struct pcmcia_device *p_dev, void *priv_data)
  420. {
  421. if ((p_dev->resource[1]->end) || (p_dev->resource[1]->end < 8))
  422. return -ENODEV;
  423. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  424. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  425. return pcmcia_request_io(p_dev);
  426. }
  427. static int dtl1_config(struct pcmcia_device *link)
  428. {
  429. dtl1_info_t *info = link->priv;
  430. int i;
  431. /* Look for a generic full-sized window */
  432. link->resource[0]->end = 8;
  433. if (pcmcia_loop_config(link, dtl1_confcheck, NULL) < 0)
  434. goto failed;
  435. i = pcmcia_request_irq(link, dtl1_interrupt);
  436. if (i != 0)
  437. goto failed;
  438. i = pcmcia_enable_device(link);
  439. if (i != 0)
  440. goto failed;
  441. if (dtl1_open(info) != 0)
  442. goto failed;
  443. return 0;
  444. failed:
  445. dtl1_detach(link);
  446. return -ENODEV;
  447. }
  448. static const struct pcmcia_device_id dtl1_ids[] = {
  449. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
  450. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
  451. PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
  452. PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
  453. PCMCIA_DEVICE_NULL
  454. };
  455. MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
  456. static struct pcmcia_driver dtl1_driver = {
  457. .owner = THIS_MODULE,
  458. .name = "dtl1_cs",
  459. .probe = dtl1_probe,
  460. .remove = dtl1_detach,
  461. .id_table = dtl1_ids,
  462. };
  463. static int __init init_dtl1_cs(void)
  464. {
  465. return pcmcia_register_driver(&dtl1_driver);
  466. }
  467. static void __exit exit_dtl1_cs(void)
  468. {
  469. pcmcia_unregister_driver(&dtl1_driver);
  470. }
  471. module_init(init_dtl1_cs);
  472. module_exit(exit_dtl1_cs);