bcm203x.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. *
  3. * Broadcom Blutonium firmware driver
  4. *
  5. * Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
  6. * Copyright (C) 2003 Marcel Holtmann <marcel@holtmann.org>
  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 as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/atomic.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/errno.h>
  31. #include <linux/device.h>
  32. #include <linux/firmware.h>
  33. #include <linux/usb.h>
  34. #include <net/bluetooth/bluetooth.h>
  35. #define VERSION "1.2"
  36. static const struct usb_device_id bcm203x_table[] = {
  37. /* Broadcom Blutonium (BCM2033) */
  38. { USB_DEVICE(0x0a5c, 0x2033) },
  39. { } /* Terminating entry */
  40. };
  41. MODULE_DEVICE_TABLE(usb, bcm203x_table);
  42. #define BCM203X_ERROR 0
  43. #define BCM203X_RESET 1
  44. #define BCM203X_LOAD_MINIDRV 2
  45. #define BCM203X_SELECT_MEMORY 3
  46. #define BCM203X_CHECK_MEMORY 4
  47. #define BCM203X_LOAD_FIRMWARE 5
  48. #define BCM203X_CHECK_FIRMWARE 6
  49. #define BCM203X_IN_EP 0x81
  50. #define BCM203X_OUT_EP 0x02
  51. struct bcm203x_data {
  52. struct usb_device *udev;
  53. unsigned long state;
  54. struct work_struct work;
  55. atomic_t shutdown;
  56. struct urb *urb;
  57. unsigned char *buffer;
  58. unsigned char *fw_data;
  59. unsigned int fw_size;
  60. unsigned int fw_sent;
  61. };
  62. static void bcm203x_complete(struct urb *urb)
  63. {
  64. struct bcm203x_data *data = urb->context;
  65. struct usb_device *udev = urb->dev;
  66. int len;
  67. BT_DBG("udev %p urb %p", udev, urb);
  68. if (urb->status) {
  69. BT_ERR("URB failed with status %d", urb->status);
  70. data->state = BCM203X_ERROR;
  71. return;
  72. }
  73. switch (data->state) {
  74. case BCM203X_LOAD_MINIDRV:
  75. memcpy(data->buffer, "#", 1);
  76. usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
  77. data->buffer, 1, bcm203x_complete, data);
  78. data->state = BCM203X_SELECT_MEMORY;
  79. /* use workqueue to have a small delay */
  80. schedule_work(&data->work);
  81. break;
  82. case BCM203X_SELECT_MEMORY:
  83. usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, BCM203X_IN_EP),
  84. data->buffer, 32, bcm203x_complete, data, 1);
  85. data->state = BCM203X_CHECK_MEMORY;
  86. if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
  87. BT_ERR("Can't submit URB");
  88. break;
  89. case BCM203X_CHECK_MEMORY:
  90. if (data->buffer[0] != '#') {
  91. BT_ERR("Memory select failed");
  92. data->state = BCM203X_ERROR;
  93. break;
  94. }
  95. data->state = BCM203X_LOAD_FIRMWARE;
  96. /* fall through */
  97. case BCM203X_LOAD_FIRMWARE:
  98. if (data->fw_sent == data->fw_size) {
  99. usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, BCM203X_IN_EP),
  100. data->buffer, 32, bcm203x_complete, data, 1);
  101. data->state = BCM203X_CHECK_FIRMWARE;
  102. } else {
  103. len = min_t(uint, data->fw_size - data->fw_sent, 4096);
  104. usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
  105. data->fw_data + data->fw_sent, len, bcm203x_complete, data);
  106. data->fw_sent += len;
  107. }
  108. if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
  109. BT_ERR("Can't submit URB");
  110. break;
  111. case BCM203X_CHECK_FIRMWARE:
  112. if (data->buffer[0] != '.') {
  113. BT_ERR("Firmware loading failed");
  114. data->state = BCM203X_ERROR;
  115. break;
  116. }
  117. data->state = BCM203X_RESET;
  118. break;
  119. }
  120. }
  121. static void bcm203x_work(struct work_struct *work)
  122. {
  123. struct bcm203x_data *data =
  124. container_of(work, struct bcm203x_data, work);
  125. if (atomic_read(&data->shutdown))
  126. return;
  127. if (usb_submit_urb(data->urb, GFP_KERNEL) < 0)
  128. BT_ERR("Can't submit URB");
  129. }
  130. static int bcm203x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  131. {
  132. const struct firmware *firmware;
  133. struct usb_device *udev = interface_to_usbdev(intf);
  134. struct bcm203x_data *data;
  135. int size;
  136. BT_DBG("intf %p id %p", intf, id);
  137. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  138. return -ENODEV;
  139. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  140. if (!data)
  141. return -ENOMEM;
  142. data->udev = udev;
  143. data->state = BCM203X_LOAD_MINIDRV;
  144. data->urb = usb_alloc_urb(0, GFP_KERNEL);
  145. if (!data->urb)
  146. return -ENOMEM;
  147. if (request_firmware(&firmware, "BCM2033-MD.hex", &udev->dev) < 0) {
  148. usb_free_urb(data->urb);
  149. return -EIO;
  150. }
  151. BT_DBG("minidrv data %p size %zu", firmware->data, firmware->size);
  152. size = max_t(uint, firmware->size, 4096);
  153. data->buffer = kmalloc(size, GFP_KERNEL);
  154. if (!data->buffer) {
  155. BT_ERR("Can't allocate memory for mini driver");
  156. release_firmware(firmware);
  157. usb_free_urb(data->urb);
  158. return -ENOMEM;
  159. }
  160. memcpy(data->buffer, firmware->data, firmware->size);
  161. usb_fill_bulk_urb(data->urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
  162. data->buffer, firmware->size, bcm203x_complete, data);
  163. release_firmware(firmware);
  164. if (request_firmware(&firmware, "BCM2033-FW.bin", &udev->dev) < 0) {
  165. usb_free_urb(data->urb);
  166. kfree(data->buffer);
  167. return -EIO;
  168. }
  169. BT_DBG("firmware data %p size %zu", firmware->data, firmware->size);
  170. data->fw_data = kmemdup(firmware->data, firmware->size, GFP_KERNEL);
  171. if (!data->fw_data) {
  172. BT_ERR("Can't allocate memory for firmware image");
  173. release_firmware(firmware);
  174. usb_free_urb(data->urb);
  175. kfree(data->buffer);
  176. return -ENOMEM;
  177. }
  178. data->fw_size = firmware->size;
  179. data->fw_sent = 0;
  180. release_firmware(firmware);
  181. INIT_WORK(&data->work, bcm203x_work);
  182. usb_set_intfdata(intf, data);
  183. /* use workqueue to have a small delay */
  184. schedule_work(&data->work);
  185. return 0;
  186. }
  187. static void bcm203x_disconnect(struct usb_interface *intf)
  188. {
  189. struct bcm203x_data *data = usb_get_intfdata(intf);
  190. BT_DBG("intf %p", intf);
  191. atomic_inc(&data->shutdown);
  192. cancel_work_sync(&data->work);
  193. usb_kill_urb(data->urb);
  194. usb_set_intfdata(intf, NULL);
  195. usb_free_urb(data->urb);
  196. kfree(data->fw_data);
  197. kfree(data->buffer);
  198. }
  199. static struct usb_driver bcm203x_driver = {
  200. .name = "bcm203x",
  201. .probe = bcm203x_probe,
  202. .disconnect = bcm203x_disconnect,
  203. .id_table = bcm203x_table,
  204. .disable_hub_initiated_lpm = 1,
  205. };
  206. module_usb_driver(bcm203x_driver);
  207. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  208. MODULE_DESCRIPTION("Broadcom Blutonium firmware driver ver " VERSION);
  209. MODULE_VERSION(VERSION);
  210. MODULE_LICENSE("GPL");
  211. MODULE_FIRMWARE("BCM2033-MD.hex");
  212. MODULE_FIRMWARE("BCM2033-FW.bin");