dtl1_cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/errno.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/ioport.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/string.h>
  36. #include <linux/serial.h>
  37. #include <linux/serial_reg.h>
  38. #include <linux/bitops.h>
  39. #include <asm/system.h>
  40. #include <asm/io.h>
  41. #include <pcmcia/cs_types.h>
  42. #include <pcmcia/cs.h>
  43. #include <pcmcia/cistpl.h>
  44. #include <pcmcia/ciscode.h>
  45. #include <pcmcia/ds.h>
  46. #include <pcmcia/cisreg.h>
  47. #include <net/bluetooth/bluetooth.h>
  48. #include <net/bluetooth/hci_core.h>
  49. /* ======================== Module parameters ======================== */
  50. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  51. MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
  52. MODULE_LICENSE("GPL");
  53. /* ======================== Local structures ======================== */
  54. typedef struct dtl1_info_t {
  55. struct pcmcia_device *p_dev;
  56. dev_node_t node;
  57. struct hci_dev *hdev;
  58. spinlock_t lock; /* For serializing operations */
  59. unsigned long flowmask; /* HCI flow mask */
  60. int ri_latch;
  61. struct sk_buff_head txq;
  62. unsigned long tx_state;
  63. unsigned long rx_state;
  64. unsigned long rx_count;
  65. struct sk_buff *rx_skb;
  66. } dtl1_info_t;
  67. static int dtl1_config(struct pcmcia_device *link);
  68. static void dtl1_release(struct pcmcia_device *link);
  69. static void dtl1_detach(struct pcmcia_device *p_dev);
  70. /* Transmit states */
  71. #define XMIT_SENDING 1
  72. #define XMIT_WAKEUP 2
  73. #define XMIT_WAITING 8
  74. /* Receiver States */
  75. #define RECV_WAIT_NSH 0
  76. #define RECV_WAIT_DATA 1
  77. typedef struct {
  78. u8 type;
  79. u8 zero;
  80. u16 len;
  81. } __attribute__ ((packed)) nsh_t; /* Nokia Specific Header */
  82. #define NSHL 4 /* Nokia Specific Header Length */
  83. /* ======================== Interrupt handling ======================== */
  84. static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
  85. {
  86. int actual = 0;
  87. /* Tx FIFO should be empty */
  88. if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
  89. return 0;
  90. /* Fill FIFO with current frame */
  91. while ((fifo_size-- > 0) && (actual < len)) {
  92. /* Transmit next byte */
  93. outb(buf[actual], iobase + UART_TX);
  94. actual++;
  95. }
  96. return actual;
  97. }
  98. static void dtl1_write_wakeup(dtl1_info_t *info)
  99. {
  100. if (!info) {
  101. BT_ERR("Unknown device");
  102. return;
  103. }
  104. if (test_bit(XMIT_WAITING, &(info->tx_state))) {
  105. set_bit(XMIT_WAKEUP, &(info->tx_state));
  106. return;
  107. }
  108. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
  109. set_bit(XMIT_WAKEUP, &(info->tx_state));
  110. return;
  111. }
  112. do {
  113. register unsigned int iobase = info->p_dev->io.BasePort1;
  114. register struct sk_buff *skb;
  115. register int len;
  116. clear_bit(XMIT_WAKEUP, &(info->tx_state));
  117. if (!pcmcia_dev_present(info->p_dev))
  118. return;
  119. if (!(skb = skb_dequeue(&(info->txq))))
  120. break;
  121. /* Send frame */
  122. len = dtl1_write(iobase, 32, skb->data, skb->len);
  123. if (len == skb->len) {
  124. set_bit(XMIT_WAITING, &(info->tx_state));
  125. kfree_skb(skb);
  126. } else {
  127. skb_pull(skb, len);
  128. skb_queue_head(&(info->txq), skb);
  129. }
  130. info->hdev->stat.byte_tx += len;
  131. } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
  132. clear_bit(XMIT_SENDING, &(info->tx_state));
  133. }
  134. static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
  135. {
  136. u8 flowmask = *(u8 *)skb->data;
  137. int i;
  138. printk(KERN_INFO "Bluetooth: Nokia control data =");
  139. for (i = 0; i < skb->len; i++) {
  140. printk(" %02x", skb->data[i]);
  141. }
  142. printk("\n");
  143. /* transition to active state */
  144. if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
  145. clear_bit(XMIT_WAITING, &(info->tx_state));
  146. dtl1_write_wakeup(info);
  147. }
  148. info->flowmask = flowmask;
  149. kfree_skb(skb);
  150. }
  151. static void dtl1_receive(dtl1_info_t *info)
  152. {
  153. unsigned int iobase;
  154. nsh_t *nsh;
  155. int boguscount = 0;
  156. if (!info) {
  157. BT_ERR("Unknown device");
  158. return;
  159. }
  160. iobase = info->p_dev->io.BasePort1;
  161. do {
  162. info->hdev->stat.byte_rx++;
  163. /* Allocate packet */
  164. if (info->rx_skb == NULL)
  165. if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
  166. BT_ERR("Can't allocate mem for new packet");
  167. info->rx_state = RECV_WAIT_NSH;
  168. info->rx_count = NSHL;
  169. return;
  170. }
  171. *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
  172. nsh = (nsh_t *)info->rx_skb->data;
  173. info->rx_count--;
  174. if (info->rx_count == 0) {
  175. switch (info->rx_state) {
  176. case RECV_WAIT_NSH:
  177. info->rx_state = RECV_WAIT_DATA;
  178. info->rx_count = nsh->len + (nsh->len & 0x0001);
  179. break;
  180. case RECV_WAIT_DATA:
  181. bt_cb(info->rx_skb)->pkt_type = nsh->type;
  182. /* remove PAD byte if it exists */
  183. if (nsh->len & 0x0001) {
  184. info->rx_skb->tail--;
  185. info->rx_skb->len--;
  186. }
  187. /* remove NSH */
  188. skb_pull(info->rx_skb, NSHL);
  189. switch (bt_cb(info->rx_skb)->pkt_type) {
  190. case 0x80:
  191. /* control data for the Nokia Card */
  192. dtl1_control(info, info->rx_skb);
  193. break;
  194. case 0x82:
  195. case 0x83:
  196. case 0x84:
  197. /* send frame to the HCI layer */
  198. info->rx_skb->dev = (void *) info->hdev;
  199. bt_cb(info->rx_skb)->pkt_type &= 0x0f;
  200. hci_recv_frame(info->rx_skb);
  201. break;
  202. default:
  203. /* unknown packet */
  204. BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
  205. kfree_skb(info->rx_skb);
  206. break;
  207. }
  208. info->rx_state = RECV_WAIT_NSH;
  209. info->rx_count = NSHL;
  210. info->rx_skb = NULL;
  211. break;
  212. }
  213. }
  214. /* Make sure we don't stay here too long */
  215. if (boguscount++ > 32)
  216. break;
  217. } while (inb(iobase + UART_LSR) & UART_LSR_DR);
  218. }
  219. static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
  220. {
  221. dtl1_info_t *info = dev_inst;
  222. unsigned int iobase;
  223. unsigned char msr;
  224. int boguscount = 0;
  225. int iir, lsr;
  226. if (!info || !info->hdev) {
  227. BT_ERR("Call of irq %d for unknown device", irq);
  228. return IRQ_NONE;
  229. }
  230. iobase = info->p_dev->io.BasePort1;
  231. spin_lock(&(info->lock));
  232. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  233. while (iir) {
  234. /* Clear interrupt */
  235. lsr = inb(iobase + UART_LSR);
  236. switch (iir) {
  237. case UART_IIR_RLSI:
  238. BT_ERR("RLSI");
  239. break;
  240. case UART_IIR_RDI:
  241. /* Receive interrupt */
  242. dtl1_receive(info);
  243. break;
  244. case UART_IIR_THRI:
  245. if (lsr & UART_LSR_THRE) {
  246. /* Transmitter ready for data */
  247. dtl1_write_wakeup(info);
  248. }
  249. break;
  250. default:
  251. BT_ERR("Unhandled IIR=%#x", iir);
  252. break;
  253. }
  254. /* Make sure we don't stay here too long */
  255. if (boguscount++ > 100)
  256. break;
  257. iir = inb(iobase + UART_IIR) & UART_IIR_ID;
  258. }
  259. msr = inb(iobase + UART_MSR);
  260. if (info->ri_latch ^ (msr & UART_MSR_RI)) {
  261. info->ri_latch = msr & UART_MSR_RI;
  262. clear_bit(XMIT_WAITING, &(info->tx_state));
  263. dtl1_write_wakeup(info);
  264. }
  265. spin_unlock(&(info->lock));
  266. return IRQ_HANDLED;
  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. };
  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. memcpy(skb_put(s, skb->len), skb->data, 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->io.BasePort1;
  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->type = 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->io.BasePort1 + UART_MSR) & UART_MSR_RI;
  374. /* Turn on interrupts */
  375. outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
  376. spin_unlock_irqrestore(&(info->lock), flags);
  377. /* Timeout before it is safe to send the first HCI packet */
  378. msleep(2000);
  379. /* Register HCI device */
  380. if (hci_register_dev(hdev) < 0) {
  381. BT_ERR("Can't register HCI device");
  382. info->hdev = NULL;
  383. hci_free_dev(hdev);
  384. return -ENODEV;
  385. }
  386. return 0;
  387. }
  388. static int dtl1_close(dtl1_info_t *info)
  389. {
  390. unsigned long flags;
  391. unsigned int iobase = info->p_dev->io.BasePort1;
  392. struct hci_dev *hdev = info->hdev;
  393. if (!hdev)
  394. return -ENODEV;
  395. dtl1_hci_close(hdev);
  396. spin_lock_irqsave(&(info->lock), flags);
  397. /* Reset UART */
  398. outb(0, iobase + UART_MCR);
  399. /* Turn off interrupts */
  400. outb(0, iobase + UART_IER);
  401. spin_unlock_irqrestore(&(info->lock), flags);
  402. if (hci_unregister_dev(hdev) < 0)
  403. BT_ERR("Can't unregister HCI device %s", hdev->name);
  404. hci_free_dev(hdev);
  405. return 0;
  406. }
  407. static int dtl1_probe(struct pcmcia_device *link)
  408. {
  409. dtl1_info_t *info;
  410. /* Create new info device */
  411. info = kzalloc(sizeof(*info), GFP_KERNEL);
  412. if (!info)
  413. return -ENOMEM;
  414. info->p_dev = link;
  415. link->priv = info;
  416. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  417. link->io.NumPorts1 = 8;
  418. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  419. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  420. link->irq.Handler = dtl1_interrupt;
  421. link->irq.Instance = info;
  422. link->conf.Attributes = CONF_ENABLE_IRQ;
  423. link->conf.IntType = INT_MEMORY_AND_IO;
  424. return dtl1_config(link);
  425. }
  426. static void dtl1_detach(struct pcmcia_device *link)
  427. {
  428. dtl1_info_t *info = link->priv;
  429. dtl1_release(link);
  430. kfree(info);
  431. }
  432. static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
  433. {
  434. int i;
  435. i = pcmcia_get_tuple_data(handle, tuple);
  436. if (i != CS_SUCCESS)
  437. return i;
  438. return pcmcia_parse_tuple(handle, tuple, parse);
  439. }
  440. static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
  441. {
  442. if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
  443. return CS_NO_MORE_ITEMS;
  444. return get_tuple(handle, tuple, parse);
  445. }
  446. static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
  447. {
  448. if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
  449. return CS_NO_MORE_ITEMS;
  450. return get_tuple(handle, tuple, parse);
  451. }
  452. static int dtl1_config(struct pcmcia_device *link)
  453. {
  454. dtl1_info_t *info = link->priv;
  455. tuple_t tuple;
  456. u_short buf[256];
  457. cisparse_t parse;
  458. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  459. int i;
  460. tuple.TupleData = (cisdata_t *)buf;
  461. tuple.TupleOffset = 0;
  462. tuple.TupleDataMax = 255;
  463. tuple.Attributes = 0;
  464. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  465. /* Look for a generic full-sized window */
  466. link->io.NumPorts1 = 8;
  467. i = first_tuple(link, &tuple, &parse);
  468. while (i != CS_NO_MORE_ITEMS) {
  469. if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
  470. link->conf.ConfigIndex = cf->index;
  471. link->io.BasePort1 = cf->io.win[0].base;
  472. link->io.NumPorts1 = cf->io.win[0].len; /*yo */
  473. link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
  474. i = pcmcia_request_io(link, &link->io);
  475. if (i == CS_SUCCESS)
  476. break;
  477. }
  478. i = next_tuple(link, &tuple, &parse);
  479. }
  480. if (i != CS_SUCCESS) {
  481. cs_error(link, RequestIO, i);
  482. goto failed;
  483. }
  484. i = pcmcia_request_irq(link, &link->irq);
  485. if (i != CS_SUCCESS) {
  486. cs_error(link, RequestIRQ, i);
  487. link->irq.AssignedIRQ = 0;
  488. }
  489. i = pcmcia_request_configuration(link, &link->conf);
  490. if (i != CS_SUCCESS) {
  491. cs_error(link, RequestConfiguration, i);
  492. goto failed;
  493. }
  494. if (dtl1_open(info) != 0)
  495. goto failed;
  496. strcpy(info->node.dev_name, info->hdev->name);
  497. link->dev_node = &info->node;
  498. return 0;
  499. failed:
  500. dtl1_release(link);
  501. return -ENODEV;
  502. }
  503. static void dtl1_release(struct pcmcia_device *link)
  504. {
  505. dtl1_info_t *info = link->priv;
  506. dtl1_close(info);
  507. pcmcia_disable_device(link);
  508. }
  509. static struct pcmcia_device_id dtl1_ids[] = {
  510. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
  511. PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
  512. PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
  513. PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
  514. PCMCIA_DEVICE_NULL
  515. };
  516. MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
  517. static struct pcmcia_driver dtl1_driver = {
  518. .owner = THIS_MODULE,
  519. .drv = {
  520. .name = "dtl1_cs",
  521. },
  522. .probe = dtl1_probe,
  523. .remove = dtl1_detach,
  524. .id_table = dtl1_ids,
  525. };
  526. static int __init init_dtl1_cs(void)
  527. {
  528. return pcmcia_register_driver(&dtl1_driver);
  529. }
  530. static void __exit exit_dtl1_cs(void)
  531. {
  532. pcmcia_unregister_driver(&dtl1_driver);
  533. }
  534. module_init(init_dtl1_cs);
  535. module_exit(exit_dtl1_cs);