bt3c_cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. bt_cb(info->rx_skb)->pkt_type = inb(iobase + DATA_L);
  176. inb(iobase + DATA_H);
  177. switch (bt_cb(info->rx_skb)->pkt_type) {
  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", bt_cb(info->rx_skb)->pkt_type);
  193. info->hdev->stat.err_rx++;
  194. kfree_skb(info->rx_skb);
  195. info->rx_skb = NULL;
  196. break;
  197. }
  198. } else {
  199. __u8 x = inb(iobase + DATA_L);
  200. *skb_put(info->rx_skb, 1) = x;
  201. inb(iobase + DATA_H);
  202. info->rx_count--;
  203. if (info->rx_count == 0) {
  204. int dlen;
  205. struct hci_event_hdr *eh;
  206. struct hci_acl_hdr *ah;
  207. struct hci_sco_hdr *sh;
  208. switch (info->rx_state) {
  209. case RECV_WAIT_EVENT_HEADER:
  210. eh = hci_event_hdr(info->rx_skb);
  211. info->rx_state = RECV_WAIT_DATA;
  212. info->rx_count = eh->plen;
  213. break;
  214. case RECV_WAIT_ACL_HEADER:
  215. ah = hci_acl_hdr(info->rx_skb);
  216. dlen = __le16_to_cpu(ah->dlen);
  217. info->rx_state = RECV_WAIT_DATA;
  218. info->rx_count = dlen;
  219. break;
  220. case RECV_WAIT_SCO_HEADER:
  221. sh = hci_sco_hdr(info->rx_skb);
  222. info->rx_state = RECV_WAIT_DATA;
  223. info->rx_count = sh->dlen;
  224. break;
  225. case RECV_WAIT_DATA:
  226. hci_recv_frame(info->hdev, info->rx_skb);
  227. info->rx_skb = NULL;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. bt3c_io_write(iobase, 0x7006, 0x0000);
  234. }
  235. static irqreturn_t bt3c_interrupt(int irq, void *dev_inst)
  236. {
  237. struct bt3c_info *info = dev_inst;
  238. unsigned int iobase;
  239. int iir;
  240. irqreturn_t r = IRQ_NONE;
  241. if (!info || !info->hdev)
  242. /* our irq handler is shared */
  243. return IRQ_NONE;
  244. iobase = info->p_dev->resource[0]->start;
  245. spin_lock(&(info->lock));
  246. iir = inb(iobase + CONTROL);
  247. if (iir & 0x80) {
  248. int stat = bt3c_read(iobase, 0x7001);
  249. if ((stat & 0xff) == 0x7f) {
  250. BT_ERR("Very strange (stat=0x%04x)", stat);
  251. } else if ((stat & 0xff) != 0xff) {
  252. if (stat & 0x0020) {
  253. int status = bt3c_read(iobase, 0x7002) & 0x10;
  254. BT_INFO("%s: Antenna %s", info->hdev->name,
  255. status ? "out" : "in");
  256. }
  257. if (stat & 0x0001)
  258. bt3c_receive(info);
  259. if (stat & 0x0002) {
  260. clear_bit(XMIT_SENDING, &(info->tx_state));
  261. bt3c_write_wakeup(info);
  262. }
  263. bt3c_io_write(iobase, 0x7001, 0x0000);
  264. outb(iir, iobase + CONTROL);
  265. }
  266. r = IRQ_HANDLED;
  267. }
  268. spin_unlock(&(info->lock));
  269. return r;
  270. }
  271. /* ======================== HCI interface ======================== */
  272. static int bt3c_hci_flush(struct hci_dev *hdev)
  273. {
  274. struct bt3c_info *info = hci_get_drvdata(hdev);
  275. /* Drop TX queue */
  276. skb_queue_purge(&(info->txq));
  277. return 0;
  278. }
  279. static int bt3c_hci_open(struct hci_dev *hdev)
  280. {
  281. return 0;
  282. }
  283. static int bt3c_hci_close(struct hci_dev *hdev)
  284. {
  285. bt3c_hci_flush(hdev);
  286. return 0;
  287. }
  288. static int bt3c_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  289. {
  290. struct bt3c_info *info = hci_get_drvdata(hdev);
  291. unsigned long flags;
  292. switch (bt_cb(skb)->pkt_type) {
  293. case HCI_COMMAND_PKT:
  294. hdev->stat.cmd_tx++;
  295. break;
  296. case HCI_ACLDATA_PKT:
  297. hdev->stat.acl_tx++;
  298. break;
  299. case HCI_SCODATA_PKT:
  300. hdev->stat.sco_tx++;
  301. break;
  302. }
  303. /* Prepend skb with frame type */
  304. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  305. skb_queue_tail(&(info->txq), skb);
  306. spin_lock_irqsave(&(info->lock), flags);
  307. bt3c_write_wakeup(info);
  308. spin_unlock_irqrestore(&(info->lock), flags);
  309. return 0;
  310. }
  311. /* ======================== Card services HCI interaction ======================== */
  312. static int bt3c_load_firmware(struct bt3c_info *info,
  313. const unsigned char *firmware,
  314. int count)
  315. {
  316. char *ptr = (char *) firmware;
  317. char b[9];
  318. unsigned int iobase, tmp;
  319. unsigned long size, addr, fcs;
  320. int i, err = 0;
  321. iobase = info->p_dev->resource[0]->start;
  322. /* Reset */
  323. bt3c_io_write(iobase, 0x8040, 0x0404);
  324. bt3c_io_write(iobase, 0x8040, 0x0400);
  325. udelay(1);
  326. bt3c_io_write(iobase, 0x8040, 0x0404);
  327. udelay(17);
  328. /* Load */
  329. while (count) {
  330. if (ptr[0] != 'S') {
  331. BT_ERR("Bad address in firmware");
  332. err = -EFAULT;
  333. goto error;
  334. }
  335. memset(b, 0, sizeof(b));
  336. memcpy(b, ptr + 2, 2);
  337. if (kstrtoul(b, 16, &size) < 0)
  338. return -EINVAL;
  339. memset(b, 0, sizeof(b));
  340. memcpy(b, ptr + 4, 8);
  341. if (kstrtoul(b, 16, &addr) < 0)
  342. return -EINVAL;
  343. memset(b, 0, sizeof(b));
  344. memcpy(b, ptr + (size * 2) + 2, 2);
  345. if (kstrtoul(b, 16, &fcs) < 0)
  346. return -EINVAL;
  347. memset(b, 0, sizeof(b));
  348. for (tmp = 0, i = 0; i < size; i++) {
  349. memcpy(b, ptr + (i * 2) + 2, 2);
  350. tmp += simple_strtol(b, NULL, 16);
  351. }
  352. if (((tmp + fcs) & 0xff) != 0xff) {
  353. BT_ERR("Checksum error in firmware");
  354. err = -EILSEQ;
  355. goto error;
  356. }
  357. if (ptr[1] == '3') {
  358. bt3c_address(iobase, addr);
  359. memset(b, 0, sizeof(b));
  360. for (i = 0; i < (size - 4) / 2; i++) {
  361. memcpy(b, ptr + (i * 4) + 12, 4);
  362. tmp = simple_strtoul(b, NULL, 16);
  363. bt3c_put(iobase, tmp);
  364. }
  365. }
  366. ptr += (size * 2) + 6;
  367. count -= (size * 2) + 6;
  368. }
  369. udelay(17);
  370. /* Boot */
  371. bt3c_address(iobase, 0x3000);
  372. outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
  373. error:
  374. udelay(17);
  375. /* Clear */
  376. bt3c_io_write(iobase, 0x7006, 0x0000);
  377. bt3c_io_write(iobase, 0x7005, 0x0000);
  378. bt3c_io_write(iobase, 0x7001, 0x0000);
  379. return err;
  380. }
  381. static int bt3c_open(struct bt3c_info *info)
  382. {
  383. const struct firmware *firmware;
  384. struct hci_dev *hdev;
  385. int err;
  386. spin_lock_init(&(info->lock));
  387. skb_queue_head_init(&(info->txq));
  388. info->rx_state = RECV_WAIT_PACKET_TYPE;
  389. info->rx_count = 0;
  390. info->rx_skb = NULL;
  391. /* Initialize HCI device */
  392. hdev = hci_alloc_dev();
  393. if (!hdev) {
  394. BT_ERR("Can't allocate HCI device");
  395. return -ENOMEM;
  396. }
  397. info->hdev = hdev;
  398. hdev->bus = HCI_PCCARD;
  399. hci_set_drvdata(hdev, info);
  400. SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
  401. hdev->open = bt3c_hci_open;
  402. hdev->close = bt3c_hci_close;
  403. hdev->flush = bt3c_hci_flush;
  404. hdev->send = bt3c_hci_send_frame;
  405. /* Load firmware */
  406. err = request_firmware(&firmware, "BT3CPCC.bin", &info->p_dev->dev);
  407. if (err < 0) {
  408. BT_ERR("Firmware request failed");
  409. goto error;
  410. }
  411. err = bt3c_load_firmware(info, firmware->data, firmware->size);
  412. release_firmware(firmware);
  413. if (err < 0) {
  414. BT_ERR("Firmware loading failed");
  415. goto error;
  416. }
  417. /* Timeout before it is safe to send the first HCI packet */
  418. msleep(1000);
  419. /* Register HCI device */
  420. err = hci_register_dev(hdev);
  421. if (err < 0) {
  422. BT_ERR("Can't register HCI device");
  423. goto error;
  424. }
  425. return 0;
  426. error:
  427. info->hdev = NULL;
  428. hci_free_dev(hdev);
  429. return err;
  430. }
  431. static int bt3c_close(struct bt3c_info *info)
  432. {
  433. struct hci_dev *hdev = info->hdev;
  434. if (!hdev)
  435. return -ENODEV;
  436. bt3c_hci_close(hdev);
  437. hci_unregister_dev(hdev);
  438. hci_free_dev(hdev);
  439. return 0;
  440. }
  441. static int bt3c_probe(struct pcmcia_device *link)
  442. {
  443. struct bt3c_info *info;
  444. /* Create new info device */
  445. info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
  446. if (!info)
  447. return -ENOMEM;
  448. info->p_dev = link;
  449. link->priv = info;
  450. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
  451. CONF_AUTO_SET_IO;
  452. return bt3c_config(link);
  453. }
  454. static void bt3c_detach(struct pcmcia_device *link)
  455. {
  456. bt3c_release(link);
  457. }
  458. static int bt3c_check_config(struct pcmcia_device *p_dev, void *priv_data)
  459. {
  460. int *try = priv_data;
  461. if (!try)
  462. p_dev->io_lines = 16;
  463. if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
  464. return -EINVAL;
  465. p_dev->resource[0]->end = 8;
  466. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  467. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  468. return pcmcia_request_io(p_dev);
  469. }
  470. static int bt3c_check_config_notpicky(struct pcmcia_device *p_dev,
  471. void *priv_data)
  472. {
  473. static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
  474. int j;
  475. if (p_dev->io_lines > 3)
  476. return -ENODEV;
  477. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  478. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  479. p_dev->resource[0]->end = 8;
  480. for (j = 0; j < 5; j++) {
  481. p_dev->resource[0]->start = base[j];
  482. p_dev->io_lines = base[j] ? 16 : 3;
  483. if (!pcmcia_request_io(p_dev))
  484. return 0;
  485. }
  486. return -ENODEV;
  487. }
  488. static int bt3c_config(struct pcmcia_device *link)
  489. {
  490. struct bt3c_info *info = link->priv;
  491. int i;
  492. unsigned long try;
  493. /* First pass: look for a config entry that looks normal.
  494. Two tries: without IO aliases, then with aliases */
  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. if (!pcmcia_loop_config(link, bt3c_check_config_notpicky, NULL))
  502. goto found_port;
  503. BT_ERR("No usable port range found");
  504. goto failed;
  505. found_port:
  506. i = pcmcia_request_irq(link, &bt3c_interrupt);
  507. if (i != 0)
  508. goto failed;
  509. i = pcmcia_enable_device(link);
  510. if (i != 0)
  511. goto failed;
  512. if (bt3c_open(info) != 0)
  513. goto failed;
  514. return 0;
  515. failed:
  516. bt3c_release(link);
  517. return -ENODEV;
  518. }
  519. static void bt3c_release(struct pcmcia_device *link)
  520. {
  521. struct bt3c_info *info = link->priv;
  522. bt3c_close(info);
  523. pcmcia_disable_device(link);
  524. }
  525. static const struct pcmcia_device_id bt3c_ids[] = {
  526. PCMCIA_DEVICE_PROD_ID13("3COM", "Bluetooth PC Card", 0xefce0a31, 0xd4ce9b02),
  527. PCMCIA_DEVICE_NULL
  528. };
  529. MODULE_DEVICE_TABLE(pcmcia, bt3c_ids);
  530. static struct pcmcia_driver bt3c_driver = {
  531. .owner = THIS_MODULE,
  532. .name = "bt3c_cs",
  533. .probe = bt3c_probe,
  534. .remove = bt3c_detach,
  535. .id_table = bt3c_ids,
  536. };
  537. module_pcmcia_driver(bt3c_driver);