dtl1_cs.c 15 KB

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