ath3k.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /* Atheros AR3011 with sflash firmware*/
  34. { USB_DEVICE(0x0CF3, 0x3002) },
  35. { } /* Terminating entry */
  36. };
  37. MODULE_DEVICE_TABLE(usb, ath3k_table);
  38. #define USB_REQ_DFU_DNLOAD 1
  39. #define BULK_SIZE 4096
  40. struct ath3k_data {
  41. struct usb_device *udev;
  42. u8 *fw_data;
  43. u32 fw_size;
  44. u32 fw_sent;
  45. };
  46. static int ath3k_load_firmware(struct ath3k_data *data,
  47. unsigned char *firmware,
  48. int count)
  49. {
  50. u8 *send_buf;
  51. int err, pipe, len, size, sent = 0;
  52. BT_DBG("ath3k %p udev %p", data, data->udev);
  53. pipe = usb_sndctrlpipe(data->udev, 0);
  54. if ((usb_control_msg(data->udev, pipe,
  55. USB_REQ_DFU_DNLOAD,
  56. USB_TYPE_VENDOR, 0, 0,
  57. firmware, 20, USB_CTRL_SET_TIMEOUT)) < 0) {
  58. BT_ERR("Can't change to loading configuration err");
  59. return -EBUSY;
  60. }
  61. sent += 20;
  62. count -= 20;
  63. send_buf = kmalloc(BULK_SIZE, GFP_ATOMIC);
  64. if (!send_buf) {
  65. BT_ERR("Can't allocate memory chunk for firmware");
  66. return -ENOMEM;
  67. }
  68. while (count) {
  69. size = min_t(uint, count, BULK_SIZE);
  70. pipe = usb_sndbulkpipe(data->udev, 0x02);
  71. memcpy(send_buf, firmware + sent, size);
  72. err = usb_bulk_msg(data->udev, pipe, send_buf, size,
  73. &len, 3000);
  74. if (err || (len != size)) {
  75. BT_ERR("Error in firmware loading err = %d,"
  76. "len = %d, size = %d", err, len, size);
  77. goto error;
  78. }
  79. sent += size;
  80. count -= size;
  81. }
  82. kfree(send_buf);
  83. return 0;
  84. error:
  85. kfree(send_buf);
  86. return err;
  87. }
  88. static int ath3k_probe(struct usb_interface *intf,
  89. const struct usb_device_id *id)
  90. {
  91. const struct firmware *firmware;
  92. struct usb_device *udev = interface_to_usbdev(intf);
  93. struct ath3k_data *data;
  94. int size;
  95. BT_DBG("intf %p id %p", intf, id);
  96. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  97. return -ENODEV;
  98. data = kzalloc(sizeof(*data), GFP_KERNEL);
  99. if (!data)
  100. return -ENOMEM;
  101. data->udev = udev;
  102. if (request_firmware(&firmware, "ath3k-1.fw", &udev->dev) < 0) {
  103. kfree(data);
  104. return -EIO;
  105. }
  106. size = max_t(uint, firmware->size, 4096);
  107. data->fw_data = kmalloc(size, GFP_KERNEL);
  108. if (!data->fw_data) {
  109. release_firmware(firmware);
  110. kfree(data);
  111. return -ENOMEM;
  112. }
  113. memcpy(data->fw_data, firmware->data, firmware->size);
  114. data->fw_size = firmware->size;
  115. data->fw_sent = 0;
  116. release_firmware(firmware);
  117. usb_set_intfdata(intf, data);
  118. if (ath3k_load_firmware(data, data->fw_data, data->fw_size)) {
  119. usb_set_intfdata(intf, NULL);
  120. kfree(data->fw_data);
  121. kfree(data);
  122. return -EIO;
  123. }
  124. return 0;
  125. }
  126. static void ath3k_disconnect(struct usb_interface *intf)
  127. {
  128. struct ath3k_data *data = usb_get_intfdata(intf);
  129. BT_DBG("ath3k_disconnect intf %p", intf);
  130. kfree(data->fw_data);
  131. kfree(data);
  132. }
  133. static struct usb_driver ath3k_driver = {
  134. .name = "ath3k",
  135. .probe = ath3k_probe,
  136. .disconnect = ath3k_disconnect,
  137. .id_table = ath3k_table,
  138. };
  139. static int __init ath3k_init(void)
  140. {
  141. BT_INFO("Atheros AR30xx firmware driver ver %s", VERSION);
  142. return usb_register(&ath3k_driver);
  143. }
  144. static void __exit ath3k_exit(void)
  145. {
  146. usb_deregister(&ath3k_driver);
  147. }
  148. module_init(ath3k_init);
  149. module_exit(ath3k_exit);
  150. MODULE_AUTHOR("Atheros Communications");
  151. MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
  152. MODULE_VERSION(VERSION);
  153. MODULE_LICENSE("GPL");
  154. MODULE_FIRMWARE("ath3k-1.fw");