ath3k.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/device.h>
  26. #include <linux/firmware.h>
  27. #include <linux/usb.h>
  28. #include <net/bluetooth/bluetooth.h>
  29. #define VERSION "1.0"
  30. static struct usb_device_id ath3k_table[] = {
  31. /* Atheros AR3011 */
  32. { USB_DEVICE(0x0CF3, 0x3000) },
  33. { } /* Terminating entry */
  34. };
  35. MODULE_DEVICE_TABLE(usb, ath3k_table);
  36. #define USB_REQ_DFU_DNLOAD 1
  37. #define BULK_SIZE 4096
  38. struct ath3k_data {
  39. struct usb_device *udev;
  40. u8 *fw_data;
  41. u32 fw_size;
  42. u32 fw_sent;
  43. };
  44. static int ath3k_load_firmware(struct ath3k_data *data,
  45. unsigned char *firmware,
  46. int count)
  47. {
  48. u8 *send_buf;
  49. int err, pipe, len, size, sent = 0;
  50. BT_DBG("ath3k %p udev %p", data, data->udev);
  51. pipe = usb_sndctrlpipe(data->udev, 0);
  52. if ((usb_control_msg(data->udev, pipe,
  53. USB_REQ_DFU_DNLOAD,
  54. USB_TYPE_VENDOR, 0, 0,
  55. firmware, 20, USB_CTRL_SET_TIMEOUT)) < 0) {
  56. BT_ERR("Can't change to loading configuration err");
  57. return -EBUSY;
  58. }
  59. sent += 20;
  60. count -= 20;
  61. send_buf = kmalloc(BULK_SIZE, GFP_ATOMIC);
  62. if (!send_buf) {
  63. BT_ERR("Can't allocate memory chunk for firmware");
  64. return -ENOMEM;
  65. }
  66. while (count) {
  67. size = min_t(uint, count, BULK_SIZE);
  68. pipe = usb_sndbulkpipe(data->udev, 0x02);
  69. memcpy(send_buf, firmware + sent, size);
  70. err = usb_bulk_msg(data->udev, pipe, send_buf, size,
  71. &len, 3000);
  72. if (err || (len != size)) {
  73. BT_ERR("Error in firmware loading err = %d,"
  74. "len = %d, size = %d", err, len, size);
  75. goto error;
  76. }
  77. sent += size;
  78. count -= size;
  79. }
  80. kfree(send_buf);
  81. return 0;
  82. error:
  83. kfree(send_buf);
  84. return err;
  85. }
  86. static int ath3k_probe(struct usb_interface *intf,
  87. const struct usb_device_id *id)
  88. {
  89. const struct firmware *firmware;
  90. struct usb_device *udev = interface_to_usbdev(intf);
  91. struct ath3k_data *data;
  92. int size;
  93. BT_DBG("intf %p id %p", intf, id);
  94. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  95. return -ENODEV;
  96. data = kzalloc(sizeof(*data), GFP_KERNEL);
  97. if (!data)
  98. return -ENOMEM;
  99. data->udev = udev;
  100. if (request_firmware(&firmware, "ath3k-1.fw", &udev->dev) < 0) {
  101. kfree(data);
  102. return -EIO;
  103. }
  104. size = max_t(uint, firmware->size, 4096);
  105. data->fw_data = kmalloc(size, GFP_KERNEL);
  106. if (!data->fw_data) {
  107. release_firmware(firmware);
  108. kfree(data);
  109. return -ENOMEM;
  110. }
  111. memcpy(data->fw_data, firmware->data, firmware->size);
  112. data->fw_size = firmware->size;
  113. data->fw_sent = 0;
  114. release_firmware(firmware);
  115. usb_set_intfdata(intf, data);
  116. if (ath3k_load_firmware(data, data->fw_data, data->fw_size)) {
  117. usb_set_intfdata(intf, NULL);
  118. kfree(data->fw_data);
  119. kfree(data);
  120. return -EIO;
  121. }
  122. return 0;
  123. }
  124. static void ath3k_disconnect(struct usb_interface *intf)
  125. {
  126. struct ath3k_data *data = usb_get_intfdata(intf);
  127. BT_DBG("ath3k_disconnect intf %p", intf);
  128. kfree(data->fw_data);
  129. kfree(data);
  130. }
  131. static struct usb_driver ath3k_driver = {
  132. .name = "ath3k",
  133. .probe = ath3k_probe,
  134. .disconnect = ath3k_disconnect,
  135. .id_table = ath3k_table,
  136. };
  137. static int __init ath3k_init(void)
  138. {
  139. BT_INFO("Atheros AR30xx firmware driver ver %s", VERSION);
  140. return usb_register(&ath3k_driver);
  141. }
  142. static void __exit ath3k_exit(void)
  143. {
  144. usb_deregister(&ath3k_driver);
  145. }
  146. module_init(ath3k_init);
  147. module_exit(ath3k_exit);
  148. MODULE_AUTHOR("Atheros Communications");
  149. MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
  150. MODULE_VERSION(VERSION);
  151. MODULE_LICENSE("GPL");
  152. MODULE_FIRMWARE("ath3k-1.fw");