dtl1_cs.c 14 KB

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