bt3c_cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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;
  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. tmp += simple_strtol(b, NULL, 16);
  352. }
  353. if (((tmp + fcs) & 0xff) != 0xff) {
  354. BT_ERR("Checksum error in firmware");
  355. err = -EILSEQ;
  356. goto error;
  357. }
  358. if (ptr[1] == '3') {
  359. bt3c_address(iobase, addr);
  360. memset(b, 0, sizeof(b));
  361. for (i = 0; i < (size - 4) / 2; i++) {
  362. memcpy(b, ptr + (i * 4) + 12, 4);
  363. tmp = simple_strtoul(b, NULL, 16);
  364. bt3c_put(iobase, tmp);
  365. }
  366. }
  367. ptr += (size * 2) + 6;
  368. count -= (size * 2) + 6;
  369. }
  370. udelay(17);
  371. /* Boot */
  372. bt3c_address(iobase, 0x3000);
  373. outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
  374. error:
  375. udelay(17);
  376. /* Clear */
  377. bt3c_io_write(iobase, 0x7006, 0x0000);
  378. bt3c_io_write(iobase, 0x7005, 0x0000);
  379. bt3c_io_write(iobase, 0x7001, 0x0000);
  380. return err;
  381. }
  382. static int bt3c_open(struct bt3c_info *info)
  383. {
  384. const struct firmware *firmware;
  385. struct hci_dev *hdev;
  386. int err;
  387. spin_lock_init(&(info->lock));
  388. skb_queue_head_init(&(info->txq));
  389. info->rx_state = RECV_WAIT_PACKET_TYPE;
  390. info->rx_count = 0;
  391. info->rx_skb = NULL;
  392. /* Initialize HCI device */
  393. hdev = hci_alloc_dev();
  394. if (!hdev) {
  395. BT_ERR("Can't allocate HCI device");
  396. return -ENOMEM;
  397. }
  398. info->hdev = hdev;
  399. hdev->bus = HCI_PCCARD;
  400. hci_set_drvdata(hdev, info);
  401. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  402. hdev->open = bt3c_hci_open;
  403. hdev->close = bt3c_hci_close;
  404. hdev->flush = bt3c_hci_flush;
  405. hdev->send = bt3c_hci_send_frame;
  406. /* Load firmware */
  407. err = request_firmware(&firmware, "BT3CPCC.bin", &info->p_dev->dev);
  408. if (err)
  409. goto error;
  410. err = bt3c_load_firmware(info, firmware->data, firmware->size);
  411. release_firmware(firmware);
  412. if (err < 0) {
  413. BT_ERR("Firmware loading failed");
  414. goto error;
  415. }
  416. /* Timeout before it is safe to send the first HCI packet */
  417. msleep(1000);
  418. /* Register HCI device */
  419. err = hci_register_dev(hdev);
  420. if (err < 0) {
  421. BT_ERR("Can't register HCI device");
  422. goto error;
  423. }
  424. return 0;
  425. error:
  426. info->hdev = NULL;
  427. hci_free_dev(hdev);
  428. return err;
  429. }
  430. static int bt3c_close(struct bt3c_info *info)
  431. {
  432. struct hci_dev *hdev = info->hdev;
  433. if (!hdev)
  434. return -ENODEV;
  435. bt3c_hci_close(hdev);
  436. hci_unregister_dev(hdev);
  437. hci_free_dev(hdev);
  438. return 0;
  439. }
  440. static int bt3c_probe(struct pcmcia_device *link)
  441. {
  442. struct bt3c_info *info;
  443. /* Create new info device */
  444. info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
  445. if (!info)
  446. return -ENOMEM;
  447. info->p_dev = link;
  448. link->priv = info;
  449. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
  450. CONF_AUTO_SET_IO;
  451. return bt3c_config(link);
  452. }
  453. static void bt3c_detach(struct pcmcia_device *link)
  454. {
  455. bt3c_release(link);
  456. }
  457. static int bt3c_check_config(struct pcmcia_device *p_dev, void *priv_data)
  458. {
  459. int *try = priv_data;
  460. if (!try)
  461. p_dev->io_lines = 16;
  462. if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
  463. return -EINVAL;
  464. p_dev->resource[0]->end = 8;
  465. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  466. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  467. return pcmcia_request_io(p_dev);
  468. }
  469. static int bt3c_check_config_notpicky(struct pcmcia_device *p_dev,
  470. void *priv_data)
  471. {
  472. static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
  473. int j;
  474. if (p_dev->io_lines > 3)
  475. return -ENODEV;
  476. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  477. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  478. p_dev->resource[0]->end = 8;
  479. for (j = 0; j < 5; j++) {
  480. p_dev->resource[0]->start = base[j];
  481. p_dev->io_lines = base[j] ? 16 : 3;
  482. if (!pcmcia_request_io(p_dev))
  483. return 0;
  484. }
  485. return -ENODEV;
  486. }
  487. static int bt3c_config(struct pcmcia_device *link)
  488. {
  489. struct bt3c_info *info = link->priv;
  490. int i;
  491. unsigned long try;
  492. /* First pass: look for a config entry that looks normal.
  493. * Two tries: without IO aliases, then with aliases
  494. */
  495. for (try = 0; try < 2; try++)
  496. if (!pcmcia_loop_config(link, bt3c_check_config, (void *) try))
  497. goto found_port;
  498. /* Second pass: try to find an entry that isn't picky about
  499. * its base address, then try to grab any standard serial port
  500. * address, and finally try to get any free port.
  501. */
  502. if (!pcmcia_loop_config(link, bt3c_check_config_notpicky, NULL))
  503. goto found_port;
  504. BT_ERR("No usable port range found");
  505. goto failed;
  506. found_port:
  507. i = pcmcia_request_irq(link, &bt3c_interrupt);
  508. if (i != 0)
  509. goto failed;
  510. i = pcmcia_enable_device(link);
  511. if (i != 0)
  512. goto failed;
  513. if (bt3c_open(info) != 0)
  514. goto failed;
  515. return 0;
  516. failed:
  517. bt3c_release(link);
  518. return -ENODEV;
  519. }
  520. static void bt3c_release(struct pcmcia_device *link)
  521. {
  522. struct bt3c_info *info = link->priv;
  523. bt3c_close(info);
  524. pcmcia_disable_device(link);
  525. }
  526. static const struct pcmcia_device_id bt3c_ids[] = {
  527. PCMCIA_DEVICE_PROD_ID13("3COM", "Bluetooth PC Card", 0xefce0a31, 0xd4ce9b02),
  528. PCMCIA_DEVICE_NULL
  529. };
  530. MODULE_DEVICE_TABLE(pcmcia, bt3c_ids);
  531. static struct pcmcia_driver bt3c_driver = {
  532. .owner = THIS_MODULE,
  533. .name = "bt3c_cs",
  534. .probe = bt3c_probe,
  535. .remove = bt3c_detach,
  536. .id_table = bt3c_ids,
  537. };
  538. module_pcmcia_driver(bt3c_driver);