dtl1_cs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. static void dtl1_release(struct pcmcia_device *link);
  65. static void dtl1_detach(struct pcmcia_device *p_dev);
  66. /* Transmit states */
  67. #define XMIT_SENDING 1
  68. #define XMIT_WAKEUP 2
  69. #define XMIT_WAITING 8
  70. /* Receiver States */
  71. #define RECV_WAIT_NSH 0
  72. #define RECV_WAIT_DATA 1
  73. typedef struct {
  74. u8 type;
  75. u8 zero;
  76. u16 len;
  77. } __packed nsh_t; /* Nokia Specific Header */
  78. #define NSHL 4 /* Nokia Specific Header Length */
  79. /* ======================== Interrupt handling ======================== */
  80. static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
  81. {
  82. int actual = 0;
  83. /* Tx FIFO should be empty */
  84. if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
  85. return 0;
  86. /* Fill FIFO with current frame */
  87. while ((fifo_size-- > 0) && (actual < len)) {
  88. /* Transmit next byte */
  89. outb(buf[actual], iobase + UART_TX);
  90. actual++;
  91. }
  92. return actual;
  93. }
  94. static void dtl1_write_wakeup(dtl1_info_t *info)
  95. {
  96. if (!info) {
  97. BT_ERR("Unknown device");
  98. return;
  99. }
  100. if (test_bit(XMIT_WAITING, &(info->tx_state))) {
  101. set_bit(XMIT_WAKEUP, &(info->tx_state));
  102. return;
  103. }
  104. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  105. set_bit(XMIT_WAKEUP, &(info->tx_state));
  106. return;
  107. }
  108. do {
  109. register unsigned int iobase = info->p_dev->resource[0]->start;
  110. register struct sk_buff *skb;
  111. register int len;
  112. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  113. if (!pcmcia_dev_present(info->p_dev))
  114. return;
  115. if (!(skb = skb_dequeue(&(info->txq))))
  116. break;
  117. /* Send frame */
  118. len = dtl1_write(iobase, 32, skb->data, skb->len);
  119. if (len == skb->len) {
  120. set_bit(XMIT_WAITING, &(info->tx_state));
  121. kfree_skb(skb);
  122. } else {
  123. skb_pull(skb, len);
  124. skb_queue_head(&(info->txq), skb);
  125. }
  126. info->hdev->stat.byte_tx += len;
  127. } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
  128. clear_bit(XMIT_SENDING, &(info->tx_state));
  129. }
  130. static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
  131. {
  132. u8 flowmask = *(u8 *)skb->data;
  133. int i;
  134. printk(KERN_INFO "Bluetooth: Nokia control data =");
  135. for (i = 0; i < skb->len; i++) {
  136. printk(" %02x", skb->data[i]);
  137. }
  138. printk("\n");
  139. /* transition to active state */
  140. if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
  141. clear_bit(XMIT_WAITING, &(info->tx_state));
  142. dtl1_write_wakeup(info);
  143. }
  144. info->flowmask = flowmask;
  145. kfree_skb(skb);
  146. }
  147. static void dtl1_receive(dtl1_info_t *info)
  148. {
  149. unsigned int iobase;
  150. nsh_t *nsh;
  151. int boguscount = 0;
  152. if (!info) {
  153. BT_ERR("Unknown device");
  154. return;
  155. }
  156. iobase = info->p_dev->resource[0]->start;
  157. do {
  158. info->hdev->stat.byte_rx++;
  159. /* Allocate packet */
  160. if (info->rx_skb == NULL)
  161. if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  162. BT_ERR("Can't allocate mem for new packet");
  163. info->rx_state = RECV_WAIT_NSH;
  164. info->rx_count = NSHL;
  165. return;
  166. }
  167. *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
  168. nsh = (nsh_t *)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. info->rx_skb->dev = (void *) info->hdev;
  195. bt_cb(info->rx_skb)->pkt_type &= 0x0f;
  196. hci_recv_frame(info->rx_skb);
  197. break;
  198. default:
  199. /* unknown packet */
  200. BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
  201. kfree_skb(info->rx_skb);
  202. break;
  203. }
  204. info->rx_state = RECV_WAIT_NSH;
  205. info->rx_count = NSHL;
  206. info->rx_skb = NULL;
  207. break;
  208. }
  209. }
  210. /* Make sure we don't stay here too long */
  211. if (boguscount++ > 32)
  212. break;
  213. } while (inb(iobase + UART_LSR) & UART_LSR_DR);
  214. }
  215. static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
  216. {
  217. dtl1_info_t *info = dev_inst;
  218. unsigned int iobase;
  219. unsigned char msr;
  220. int boguscount = 0;
  221. int iir, lsr;
  222. irqreturn_t r = IRQ_NONE;
  223. if (!info || !info->hdev)
  224. /* our irq handler is shared */
  225. return IRQ_NONE;
  226. iobase = info->p_dev->resource[0]->start;
  227. spin_lock(&(info->lock));
  228. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  229. while (iir) {
  230. r = IRQ_HANDLED;
  231. /* Clear interrupt */
  232. lsr = inb(iobase + UART_LSR);
  233. switch (iir) {
  234. case UART_IIR_RLSI:
  235. BT_ERR("RLSI");
  236. break;
  237. case UART_IIR_RDI:
  238. /* Receive interrupt */
  239. dtl1_receive(info);
  240. break;
  241. case UART_IIR_THRI:
  242. if (lsr & UART_LSR_THRE) {
  243. /* Transmitter ready for data */
  244. dtl1_write_wakeup(info);
  245. }
  246. break;
  247. default:
  248. BT_ERR("Unhandled IIR=%#x", iir);
  249. break;
  250. }
  251. /* Make sure we don't stay here too long */
  252. if (boguscount++ > 100)
  253. break;
  254. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  255. }
  256. msr = inb(iobase + UART_MSR);
  257. if (info->ri_latch ^ (msr & UART_MSR_RI)) {
  258. info->ri_latch = msr & UART_MSR_RI;
  259. clear_bit(XMIT_WAITING, &(info->tx_state));
  260. dtl1_write_wakeup(info);
  261. r = IRQ_HANDLED;
  262. }
  263. spin_unlock(&(info->lock));
  264. return r;
  265. }
  266. /* ======================== HCI interface ======================== */
  267. static int dtl1_hci_open(struct hci_dev *hdev)
  268. {
  269. set_bit(HCI_RUNNING, &(hdev->flags));
  270. return 0;
  271. }
  272. static int dtl1_hci_flush(struct hci_dev *hdev)
  273. {
  274. dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
  275. /* Drop TX queue */
  276. skb_queue_purge(&(info->txq));
  277. return 0;
  278. }
  279. static int dtl1_hci_close(struct hci_dev *hdev)
  280. {
  281. if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
  282. return 0;
  283. dtl1_hci_flush(hdev);
  284. return 0;
  285. }
  286. static int dtl1_hci_send_frame(struct sk_buff *skb)
  287. {
  288. dtl1_info_t *info;
  289. struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
  290. struct sk_buff *s;
  291. nsh_t nsh;
  292. if (!hdev) {
  293. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  294. return -ENODEV;
  295. }
  296. info = (dtl1_info_t *)(hdev->driver_data);
  297. switch (bt_cb(skb)->pkt_type) {
  298. case HCI_COMMAND_PKT:
  299. hdev->stat.cmd_tx++;
  300. nsh.type = 0x81;
  301. break;
  302. case HCI_ACLDATA_PKT:
  303. hdev->stat.acl_tx++;
  304. nsh.type = 0x82;
  305. break;
  306. case HCI_SCODATA_PKT:
  307. hdev->stat.sco_tx++;
  308. nsh.type = 0x83;
  309. break;
  310. default:
  311. return -EILSEQ;
  312. };
  313. nsh.zero = 0;
  314. nsh.len = skb->len;
  315. s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
  316. if (!s)
  317. return -ENOMEM;
  318. skb_reserve(s, NSHL);
  319. skb_copy_from_linear_data(skb, skb_put(s, skb->len), skb->len);
  320. if (skb->len & 0x0001)
  321. *skb_put(s, 1) = 0; /* PAD */
  322. /* Prepend skb with Nokia frame header and queue */
  323. memcpy(skb_push(s, NSHL), &nsh, NSHL);
  324. skb_queue_tail(&(info->txq), s);
  325. dtl1_write_wakeup(info);
  326. kfree_skb(skb);
  327. return 0;
  328. }
  329. static void dtl1_hci_destruct(struct hci_dev *hdev)
  330. {
  331. }
  332. static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
  333. {
  334. return -ENOIOCTLCMD;
  335. }
  336. /* ======================== Card services HCI interaction ======================== */
  337. static int dtl1_open(dtl1_info_t *info)
  338. {
  339. unsigned long flags;
  340. unsigned int iobase = info->p_dev->resource[0]->start;
  341. struct hci_dev *hdev;
  342. spin_lock_init(&(info->lock));
  343. skb_queue_head_init(&(info->txq));
  344. info->rx_state = RECV_WAIT_NSH;
  345. info->rx_count = NSHL;
  346. info->rx_skb = NULL;
  347. set_bit(XMIT_WAITING, &(info->tx_state));
  348. /* Initialize HCI device */
  349. hdev = hci_alloc_dev();
  350. if (!hdev) {
  351. BT_ERR("Can't allocate HCI device");
  352. return -ENOMEM;
  353. }
  354. info->hdev = hdev;
  355. hdev->bus = HCI_PCCARD;
  356. hdev->driver_data = info;
  357. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  358. hdev->open = dtl1_hci_open;
  359. hdev->close = dtl1_hci_close;
  360. hdev->flush = dtl1_hci_flush;
  361. hdev->send = dtl1_hci_send_frame;
  362. hdev->destruct = dtl1_hci_destruct;
  363. hdev->ioctl = dtl1_hci_ioctl;
  364. hdev->owner = THIS_MODULE;
  365. spin_lock_irqsave(&(info->lock), flags);
  366. /* Reset UART */
  367. outb(0, iobase + UART_MCR);
  368. /* Turn off interrupts */
  369. outb(0, iobase + UART_IER);
  370. /* Initialize UART */
  371. outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
  372. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
  373. info->ri_latch = inb(info->p_dev->resource[0]->start + UART_MSR)
  374. & UART_MSR_RI;
  375. /* Turn on interrupts */
  376. outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  377. spin_unlock_irqrestore(&(info->lock), flags);
  378. /* Timeout before it is safe to send the first HCI packet */
  379. msleep(2000);
  380. /* Register HCI device */
  381. if (hci_register_dev(hdev) < 0) {
  382. BT_ERR("Can't register HCI device");
  383. info->hdev = NULL;
  384. hci_free_dev(hdev);
  385. return -ENODEV;
  386. }
  387. return 0;
  388. }
  389. static int dtl1_close(dtl1_info_t *info)
  390. {
  391. unsigned long flags;
  392. unsigned int iobase = info->p_dev->resource[0]->start;
  393. struct hci_dev *hdev = info->hdev;
  394. if (!hdev)
  395. return -ENODEV;
  396. dtl1_hci_close(hdev);
  397. spin_lock_irqsave(&(info->lock), flags);
  398. /* Reset UART */
  399. outb(0, iobase + UART_MCR);
  400. /* Turn off interrupts */
  401. outb(0, iobase + UART_IER);
  402. spin_unlock_irqrestore(&(info->lock), flags);
  403. if (hci_unregister_dev(hdev) < 0)
  404. BT_ERR("Can't unregister HCI device %s", hdev->name);
  405. hci_free_dev(hdev);
  406. return 0;
  407. }
  408. static int dtl1_probe(struct pcmcia_device *link)
  409. {
  410. dtl1_info_t *info;
  411. /* Create new info device */
  412. info = kzalloc(sizeof(*info), GFP_KERNEL);
  413. if (!info)
  414. return -ENOMEM;
  415. info->p_dev = link;
  416. link->priv = info;
  417. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
  418. return dtl1_config(link);
  419. }
  420. static void dtl1_detach(struct pcmcia_device *link)
  421. {
  422. dtl1_info_t *info = link->priv;
  423. dtl1_release(link);
  424. kfree(info);
  425. }
  426. static int dtl1_confcheck(struct pcmcia_device *p_dev, void *priv_data)
  427. {
  428. if ((p_dev->resource[1]->end) || (p_dev->resource[1]->end < 8))
  429. return -ENODEV;
  430. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  431. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  432. return pcmcia_request_io(p_dev);
  433. }
  434. static int dtl1_config(struct pcmcia_device *link)
  435. {
  436. dtl1_info_t *info = link->priv;
  437. int i;
  438. /* Look for a generic full-sized window */
  439. link->resource[0]->end = 8;
  440. if (pcmcia_loop_config(link, dtl1_confcheck, NULL) < 0)
  441. goto failed;
  442. i = pcmcia_request_irq(link, dtl1_interrupt);
  443. if (i != 0)
  444. goto failed;
  445. i = pcmcia_enable_device(link);
  446. if (i != 0)
  447. goto failed;
  448. if (dtl1_open(info) != 0)
  449. goto failed;
  450. return 0;
  451. failed:
  452. dtl1_release(link);
  453. return -ENODEV;
  454. }
  455. static void dtl1_release(struct pcmcia_device *link)
  456. {
  457. dtl1_info_t *info = link->priv;
  458. dtl1_close(info);
  459. pcmcia_disable_device(link);
  460. }
  461. static struct pcmcia_device_id dtl1_ids[] = {
  462. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
  463. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
  464. PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
  465. PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
  466. PCMCIA_DEVICE_NULL
  467. };
  468. MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
  469. static struct pcmcia_driver dtl1_driver = {
  470. .owner = THIS_MODULE,
  471. .name = "dtl1_cs",
  472. .probe = dtl1_probe,
  473. .remove = dtl1_detach,
  474. .id_table = dtl1_ids,
  475. };
  476. static int __init init_dtl1_cs(void)
  477. {
  478. return pcmcia_register_driver(&dtl1_driver);
  479. }
  480. static void __exit exit_dtl1_cs(void)
  481. {
  482. pcmcia_unregister_driver(&dtl1_driver);
  483. }
  484. module_init(init_dtl1_cs);
  485. module_exit(exit_dtl1_cs);