bt3c_cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. *
  3. * Driver for the 3Com Bluetooth PCMCIA card
  4. *
  5. * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org>
  6. * Jose Orlando Pereira <jop@di.uminho.pt>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation;
  12. *
  13. * Software distributed under the License is distributed on an "AS
  14. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  15. * implied. See the License for the specific language governing
  16. * rights and limitations under the License.
  17. *
  18. * The initial developer of the original code is David A. Hinds
  19. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  20. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.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/io.h>
  40. #include <linux/device.h>
  41. #include <linux/firmware.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 the 3Com Bluetooth PCMCIA card");
  51. MODULE_LICENSE("GPL");
  52. MODULE_FIRMWARE("BT3CPCC.bin");
  53. /* ======================== Local structures ======================== */
  54. struct bt3c_info {
  55. struct pcmcia_device *p_dev;
  56. struct hci_dev *hdev;
  57. spinlock_t lock; /* For serializing operations */
  58. struct sk_buff_head txq;
  59. unsigned long tx_state;
  60. unsigned long rx_state;
  61. unsigned long rx_count;
  62. struct sk_buff *rx_skb;
  63. };
  64. static int bt3c_config(struct pcmcia_device *link);
  65. static void bt3c_release(struct pcmcia_device *link);
  66. static void bt3c_detach(struct pcmcia_device *p_dev);
  67. /* Transmit states */
  68. #define XMIT_SENDING 1
  69. #define XMIT_WAKEUP 2
  70. #define XMIT_WAITING 8
  71. /* Receiver states */
  72. #define RECV_WAIT_PACKET_TYPE 0
  73. #define RECV_WAIT_EVENT_HEADER 1
  74. #define RECV_WAIT_ACL_HEADER 2
  75. #define RECV_WAIT_SCO_HEADER 3
  76. #define RECV_WAIT_DATA 4
  77. /* ======================== Special I/O functions ======================== */
  78. #define DATA_L 0
  79. #define DATA_H 1
  80. #define ADDR_L 2
  81. #define ADDR_H 3
  82. #define CONTROL 4
  83. static inline void bt3c_address(unsigned int iobase, unsigned short addr)
  84. {
  85. outb(addr & 0xff, iobase + ADDR_L);
  86. outb((addr >> 8) & 0xff, iobase + ADDR_H);
  87. }
  88. static inline void bt3c_put(unsigned int iobase, unsigned short value)
  89. {
  90. outb(value & 0xff, iobase + DATA_L);
  91. outb((value >> 8) & 0xff, iobase + DATA_H);
  92. }
  93. static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
  94. {
  95. bt3c_address(iobase, addr);
  96. bt3c_put(iobase, value);
  97. }
  98. static inline unsigned short bt3c_get(unsigned int iobase)
  99. {
  100. unsigned short value = inb(iobase + DATA_L);
  101. value |= inb(iobase + DATA_H) << 8;
  102. return value;
  103. }
  104. static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
  105. {
  106. bt3c_address(iobase, addr);
  107. return bt3c_get(iobase);
  108. }
  109. /* ======================== Interrupt handling ======================== */
  110. static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
  111. {
  112. int actual = 0;
  113. bt3c_address(iobase, 0x7080);
  114. /* Fill FIFO with current frame */
  115. while (actual < len) {
  116. /* Transmit next byte */
  117. bt3c_put(iobase, buf[actual]);
  118. actual++;
  119. }
  120. bt3c_io_write(iobase, 0x7005, actual);
  121. return actual;
  122. }
  123. static void bt3c_write_wakeup(struct bt3c_info *info)
  124. {
  125. if (!info) {
  126. BT_ERR("Unknown device");
  127. return;
  128. }
  129. if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
  130. return;
  131. do {
  132. unsigned int iobase = info->p_dev->resource[0]->start;
  133. register struct sk_buff *skb;
  134. int len;
  135. if (!pcmcia_dev_present(info->p_dev))
  136. break;
  137. skb = skb_dequeue(&(info->txq));
  138. if (!skb) {
  139. clear_bit(XMIT_SENDING, &(info->tx_state));
  140. break;
  141. }
  142. /* Send frame */
  143. len = bt3c_write(iobase, 256, skb->data, skb->len);
  144. if (len != skb->len)
  145. BT_ERR("Very strange");
  146. kfree_skb(skb);
  147. info->hdev->stat.byte_tx += len;
  148. } while (0);
  149. }
  150. static void bt3c_receive(struct bt3c_info *info)
  151. {
  152. unsigned int iobase;
  153. int size = 0, avail;
  154. if (!info) {
  155. BT_ERR("Unknown device");
  156. return;
  157. }
  158. iobase = info->p_dev->resource[0]->start;
  159. avail = bt3c_read(iobase, 0x7006);
  160. bt3c_address(iobase, 0x7480);
  161. while (size < avail) {
  162. size++;
  163. info->hdev->stat.byte_rx++;
  164. /* Allocate packet */
  165. if (!info->rx_skb) {
  166. info->rx_state = RECV_WAIT_PACKET_TYPE;
  167. info->rx_count = 0;
  168. info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  169. if (!info->rx_skb) {
  170. BT_ERR("Can't allocate mem for new packet");
  171. return;
  172. }
  173. }
  174. if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
  175. hci_skb_pkt_type(info->rx_skb) = inb(iobase + DATA_L);
  176. inb(iobase + DATA_H);
  177. switch (hci_skb_pkt_type(info->rx_skb)) {
  178. case HCI_EVENT_PKT:
  179. info->rx_state = RECV_WAIT_EVENT_HEADER;
  180. info->rx_count = HCI_EVENT_HDR_SIZE;
  181. break;
  182. case HCI_ACLDATA_PKT:
  183. info->rx_state = RECV_WAIT_ACL_HEADER;
  184. info->rx_count = HCI_ACL_HDR_SIZE;
  185. break;
  186. case HCI_SCODATA_PKT:
  187. info->rx_state = RECV_WAIT_SCO_HEADER;
  188. info->rx_count = HCI_SCO_HDR_SIZE;
  189. break;
  190. default:
  191. /* Unknown packet */
  192. BT_ERR("Unknown HCI packet with type 0x%02x received",
  193. hci_skb_pkt_type(info->rx_skb));
  194. info->hdev->stat.err_rx++;
  195. kfree_skb(info->rx_skb);
  196. info->rx_skb = NULL;
  197. break;
  198. }
  199. } else {
  200. __u8 x = inb(iobase + DATA_L);
  201. skb_put_u8(info->rx_skb, x);
  202. inb(iobase + DATA_H);
  203. info->rx_count--;
  204. if (info->rx_count == 0) {
  205. int dlen;
  206. struct hci_event_hdr *eh;
  207. struct hci_acl_hdr *ah;
  208. struct hci_sco_hdr *sh;
  209. switch (info->rx_state) {
  210. case RECV_WAIT_EVENT_HEADER:
  211. eh = hci_event_hdr(info->rx_skb);
  212. info->rx_state = RECV_WAIT_DATA;
  213. info->rx_count = eh->plen;
  214. break;
  215. case RECV_WAIT_ACL_HEADER:
  216. ah = hci_acl_hdr(info->rx_skb);
  217. dlen = __le16_to_cpu(ah->dlen);
  218. info->rx_state = RECV_WAIT_DATA;
  219. info->rx_count = dlen;
  220. break;
  221. case RECV_WAIT_SCO_HEADER:
  222. sh = hci_sco_hdr(info->rx_skb);
  223. info->rx_state = RECV_WAIT_DATA;
  224. info->rx_count = sh->dlen;
  225. break;
  226. case RECV_WAIT_DATA:
  227. hci_recv_frame(info->hdev, info->rx_skb);
  228. info->rx_skb = NULL;
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. bt3c_io_write(iobase, 0x7006, 0x0000);
  235. }
  236. static irqreturn_t bt3c_interrupt(int irq, void *dev_inst)
  237. {
  238. struct bt3c_info *info = dev_inst;
  239. unsigned int iobase;
  240. int iir;
  241. irqreturn_t r = IRQ_NONE;
  242. if (!info || !info->hdev)
  243. /* our irq handler is shared */
  244. return IRQ_NONE;
  245. iobase = info->p_dev->resource[0]->start;
  246. spin_lock(&(info->lock));
  247. iir = inb(iobase + CONTROL);
  248. if (iir & 0x80) {
  249. int stat = bt3c_read(iobase, 0x7001);
  250. if ((stat & 0xff) == 0x7f) {
  251. BT_ERR("Very strange (stat=0x%04x)", stat);
  252. } else if ((stat & 0xff) != 0xff) {
  253. if (stat & 0x0020) {
  254. int status = bt3c_read(iobase, 0x7002) & 0x10;
  255. bt_dev_info(info->hdev, "Antenna %s",
  256. status ? "out" : "in");
  257. }
  258. if (stat & 0x0001)
  259. bt3c_receive(info);
  260. if (stat & 0x0002) {
  261. clear_bit(XMIT_SENDING, &(info->tx_state));
  262. bt3c_write_wakeup(info);
  263. }
  264. bt3c_io_write(iobase, 0x7001, 0x0000);
  265. outb(iir, iobase + CONTROL);
  266. }
  267. r = IRQ_HANDLED;
  268. }
  269. spin_unlock(&(info->lock));
  270. return r;
  271. }
  272. /* ======================== HCI interface ======================== */
  273. static int bt3c_hci_flush(struct hci_dev *hdev)
  274. {
  275. struct bt3c_info *info = hci_get_drvdata(hdev);
  276. /* Drop TX queue */
  277. skb_queue_purge(&(info->txq));
  278. return 0;
  279. }
  280. static int bt3c_hci_open(struct hci_dev *hdev)
  281. {
  282. return 0;
  283. }
  284. static int bt3c_hci_close(struct hci_dev *hdev)
  285. {
  286. bt3c_hci_flush(hdev);
  287. return 0;
  288. }
  289. static int bt3c_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  290. {
  291. struct bt3c_info *info = hci_get_drvdata(hdev);
  292. unsigned long flags;
  293. switch (hci_skb_pkt_type(skb)) {
  294. case HCI_COMMAND_PKT:
  295. hdev->stat.cmd_tx++;
  296. break;
  297. case HCI_ACLDATA_PKT:
  298. hdev->stat.acl_tx++;
  299. break;
  300. case HCI_SCODATA_PKT:
  301. hdev->stat.sco_tx++;
  302. break;
  303. }
  304. /* Prepend skb with frame type */
  305. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  306. skb_queue_tail(&(info->txq), skb);
  307. spin_lock_irqsave(&(info->lock), flags);
  308. bt3c_write_wakeup(info);
  309. spin_unlock_irqrestore(&(info->lock), flags);
  310. return 0;
  311. }
  312. /* ======================== Card services HCI interaction ======================== */
  313. static int bt3c_load_firmware(struct bt3c_info *info,
  314. const unsigned char *firmware,
  315. int count)
  316. {
  317. char *ptr = (char *) firmware;
  318. char b[9];
  319. unsigned int iobase, tmp, tn;
  320. unsigned long size, addr, fcs;
  321. int i, err = 0;
  322. iobase = info->p_dev->resource[0]->start;
  323. /* Reset */
  324. bt3c_io_write(iobase, 0x8040, 0x0404);
  325. bt3c_io_write(iobase, 0x8040, 0x0400);
  326. udelay(1);
  327. bt3c_io_write(iobase, 0x8040, 0x0404);
  328. udelay(17);
  329. /* Load */
  330. while (count) {
  331. if (ptr[0] != 'S') {
  332. BT_ERR("Bad address in firmware");
  333. err = -EFAULT;
  334. goto error;
  335. }
  336. memset(b, 0, sizeof(b));
  337. memcpy(b, ptr + 2, 2);
  338. if (kstrtoul(b, 16, &size) < 0)
  339. return -EINVAL;
  340. memset(b, 0, sizeof(b));
  341. memcpy(b, ptr + 4, 8);
  342. if (kstrtoul(b, 16, &addr) < 0)
  343. return -EINVAL;
  344. memset(b, 0, sizeof(b));
  345. memcpy(b, ptr + (size * 2) + 2, 2);
  346. if (kstrtoul(b, 16, &fcs) < 0)
  347. return -EINVAL;
  348. memset(b, 0, sizeof(b));
  349. for (tmp = 0, i = 0; i < size; i++) {
  350. memcpy(b, ptr + (i * 2) + 2, 2);
  351. if (kstrtouint(b, 16, &tn))
  352. return -EINVAL;
  353. tmp += tn;
  354. }
  355. if (((tmp + fcs) & 0xff) != 0xff) {
  356. BT_ERR("Checksum error in firmware");
  357. err = -EILSEQ;
  358. goto error;
  359. }
  360. if (ptr[1] == '3') {
  361. bt3c_address(iobase, addr);
  362. memset(b, 0, sizeof(b));
  363. for (i = 0; i < (size - 4) / 2; i++) {
  364. memcpy(b, ptr + (i * 4) + 12, 4);
  365. if (kstrtouint(b, 16, &tmp))
  366. return -EINVAL;
  367. bt3c_put(iobase, tmp);
  368. }
  369. }
  370. ptr += (size * 2) + 6;
  371. count -= (size * 2) + 6;
  372. }
  373. udelay(17);
  374. /* Boot */
  375. bt3c_address(iobase, 0x3000);
  376. outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
  377. error:
  378. udelay(17);
  379. /* Clear */
  380. bt3c_io_write(iobase, 0x7006, 0x0000);
  381. bt3c_io_write(iobase, 0x7005, 0x0000);
  382. bt3c_io_write(iobase, 0x7001, 0x0000);
  383. return err;
  384. }
  385. static int bt3c_open(struct bt3c_info *info)
  386. {
  387. const struct firmware *firmware;
  388. struct hci_dev *hdev;
  389. int err;
  390. spin_lock_init(&(info->lock));
  391. skb_queue_head_init(&(info->txq));
  392. info->rx_state = RECV_WAIT_PACKET_TYPE;
  393. info->rx_count = 0;
  394. info->rx_skb = NULL;
  395. /* Initialize HCI device */
  396. hdev = hci_alloc_dev();
  397. if (!hdev) {
  398. BT_ERR("Can't allocate HCI device");
  399. return -ENOMEM;
  400. }
  401. info->hdev = hdev;
  402. hdev->bus = HCI_PCCARD;
  403. hci_set_drvdata(hdev, info);
  404. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  405. hdev->open = bt3c_hci_open;
  406. hdev->close = bt3c_hci_close;
  407. hdev->flush = bt3c_hci_flush;
  408. hdev->send = bt3c_hci_send_frame;
  409. /* Load firmware */
  410. err = request_firmware(&firmware, "BT3CPCC.bin", &info->p_dev->dev);
  411. if (err < 0) {
  412. BT_ERR("Firmware request failed");
  413. goto error;
  414. }
  415. err = bt3c_load_firmware(info, firmware->data, firmware->size);
  416. release_firmware(firmware);
  417. if (err < 0) {
  418. BT_ERR("Firmware loading failed");
  419. goto error;
  420. }
  421. /* Timeout before it is safe to send the first HCI packet */
  422. msleep(1000);
  423. /* Register HCI device */
  424. err = hci_register_dev(hdev);
  425. if (err < 0) {
  426. BT_ERR("Can't register HCI device");
  427. goto error;
  428. }
  429. return 0;
  430. error:
  431. info->hdev = NULL;
  432. hci_free_dev(hdev);
  433. return err;
  434. }
  435. static int bt3c_close(struct bt3c_info *info)
  436. {
  437. struct hci_dev *hdev = info->hdev;
  438. if (!hdev)
  439. return -ENODEV;
  440. bt3c_hci_close(hdev);
  441. hci_unregister_dev(hdev);
  442. hci_free_dev(hdev);
  443. return 0;
  444. }
  445. static int bt3c_probe(struct pcmcia_device *link)
  446. {
  447. struct bt3c_info *info;
  448. /* Create new info device */
  449. info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
  450. if (!info)
  451. return -ENOMEM;
  452. info->p_dev = link;
  453. link->priv = info;
  454. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
  455. CONF_AUTO_SET_IO;
  456. return bt3c_config(link);
  457. }
  458. static void bt3c_detach(struct pcmcia_device *link)
  459. {
  460. bt3c_release(link);
  461. }
  462. static int bt3c_check_config(struct pcmcia_device *p_dev, void *priv_data)
  463. {
  464. int *try = priv_data;
  465. if (!try)
  466. p_dev->io_lines = 16;
  467. if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
  468. return -EINVAL;
  469. p_dev->resource[0]->end = 8;
  470. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  471. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  472. return pcmcia_request_io(p_dev);
  473. }
  474. static int bt3c_check_config_notpicky(struct pcmcia_device *p_dev,
  475. void *priv_data)
  476. {
  477. static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
  478. int j;
  479. if (p_dev->io_lines > 3)
  480. return -ENODEV;
  481. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  482. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  483. p_dev->resource[0]->end = 8;
  484. for (j = 0; j < 5; j++) {
  485. p_dev->resource[0]->start = base[j];
  486. p_dev->io_lines = base[j] ? 16 : 3;
  487. if (!pcmcia_request_io(p_dev))
  488. return 0;
  489. }
  490. return -ENODEV;
  491. }
  492. static int bt3c_config(struct pcmcia_device *link)
  493. {
  494. struct bt3c_info *info = link->priv;
  495. int i;
  496. unsigned long try;
  497. /* First pass: look for a config entry that looks normal.
  498. * Two tries: without IO aliases, then with aliases
  499. */
  500. for (try = 0; try < 2; try++)
  501. if (!pcmcia_loop_config(link, bt3c_check_config, (void *) try))
  502. goto found_port;
  503. /* Second pass: try to find an entry that isn't picky about
  504. * its base address, then try to grab any standard serial port
  505. * address, and finally try to get any free port.
  506. */
  507. if (!pcmcia_loop_config(link, bt3c_check_config_notpicky, NULL))
  508. goto found_port;
  509. BT_ERR("No usable port range found");
  510. goto failed;
  511. found_port:
  512. i = pcmcia_request_irq(link, &bt3c_interrupt);
  513. if (i != 0)
  514. goto failed;
  515. i = pcmcia_enable_device(link);
  516. if (i != 0)
  517. goto failed;
  518. if (bt3c_open(info) != 0)
  519. goto failed;
  520. return 0;
  521. failed:
  522. bt3c_release(link);
  523. return -ENODEV;
  524. }
  525. static void bt3c_release(struct pcmcia_device *link)
  526. {
  527. struct bt3c_info *info = link->priv;
  528. bt3c_close(info);
  529. pcmcia_disable_device(link);
  530. }
  531. static const struct pcmcia_device_id bt3c_ids[] = {
  532. PCMCIA_DEVICE_PROD_ID13("3COM", "Bluetooth PC Card", 0xefce0a31, 0xd4ce9b02),
  533. PCMCIA_DEVICE_NULL
  534. };
  535. MODULE_DEVICE_TABLE(pcmcia, bt3c_ids);
  536. static struct pcmcia_driver bt3c_driver = {
  537. .owner = THIS_MODULE,
  538. .name = "bt3c_cs",
  539. .probe = bt3c_probe,
  540. .remove = bt3c_detach,
  541. .id_table = bt3c_ids,
  542. };
  543. module_pcmcia_driver(bt3c_driver);