bcm203x.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include <linux/errno.h>
  30. #include <linux/device.h>
  31. #include <linux/firmware.h>
  32. #include <linux/usb.h>
  33. #include <net/bluetooth/bluetooth.h>
  34. #ifndef CONFIG_BT_HCIBCM203X_DEBUG
  35. #undef BT_DBG
  36. #define BT_DBG(D...)
  37. #endif
  38. #define VERSION "1.2"
  39. static struct usb_device_id bcm203x_table[] = {
  40. /* Broadcom Blutonium (BCM2033) */
  41. { USB_DEVICE(0x0a5c, 0x2033) },
  42. { } /* Terminating entry */
  43. };
  44. MODULE_DEVICE_TABLE(usb, bcm203x_table);
  45. #define BCM203X_ERROR 0
  46. #define BCM203X_RESET 1
  47. #define BCM203X_LOAD_MINIDRV 2
  48. #define BCM203X_SELECT_MEMORY 3
  49. #define BCM203X_CHECK_MEMORY 4
  50. #define BCM203X_LOAD_FIRMWARE 5
  51. #define BCM203X_CHECK_FIRMWARE 6
  52. #define BCM203X_IN_EP 0x81
  53. #define BCM203X_OUT_EP 0x02
  54. struct bcm203x_data {
  55. struct usb_device *udev;
  56. unsigned long state;
  57. struct work_struct work;
  58. struct urb *urb;
  59. unsigned char *buffer;
  60. unsigned char *fw_data;
  61. unsigned int fw_size;
  62. unsigned int fw_sent;
  63. };
  64. static void bcm203x_complete(struct urb *urb)
  65. {
  66. struct bcm203x_data *data = urb->context;
  67. struct usb_device *udev = urb->dev;
  68. int len;
  69. BT_DBG("udev %p urb %p", udev, urb);
  70. if (urb->status) {
  71. BT_ERR("URB failed with status %d", urb->status);
  72. data->state = BCM203X_ERROR;
  73. return;
  74. }
  75. switch (data->state) {
  76. case BCM203X_LOAD_MINIDRV:
  77. memcpy(data->buffer, "#", 1);
  78. usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
  79. data->buffer, 1, bcm203x_complete, data);
  80. data->state = BCM203X_SELECT_MEMORY;
  81. schedule_work(&data->work);
  82. break;
  83. case BCM203X_SELECT_MEMORY:
  84. usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, BCM203X_IN_EP),
  85. data->buffer, 32, bcm203x_complete, data, 1);
  86. data->state = BCM203X_CHECK_MEMORY;
  87. if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
  88. BT_ERR("Can't submit URB");
  89. break;
  90. case BCM203X_CHECK_MEMORY:
  91. if (data->buffer[0] != '#') {
  92. BT_ERR("Memory select failed");
  93. data->state = BCM203X_ERROR;
  94. break;
  95. }
  96. data->state = BCM203X_LOAD_FIRMWARE;
  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 (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
  126. BT_ERR("Can't submit URB");
  127. }
  128. static int bcm203x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  129. {
  130. const struct firmware *firmware;
  131. struct usb_device *udev = interface_to_usbdev(intf);
  132. struct bcm203x_data *data;
  133. int size;
  134. BT_DBG("intf %p id %p", intf, id);
  135. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  136. return -ENODEV;
  137. data = kzalloc(sizeof(*data), GFP_KERNEL);
  138. if (!data) {
  139. BT_ERR("Can't allocate memory for data structure");
  140. return -ENOMEM;
  141. }
  142. data->udev = udev;
  143. data->state = BCM203X_LOAD_MINIDRV;
  144. data->urb = usb_alloc_urb(0, GFP_KERNEL);
  145. if (!data->urb) {
  146. BT_ERR("Can't allocate URB");
  147. kfree(data);
  148. return -ENOMEM;
  149. }
  150. if (request_firmware(&firmware, "BCM2033-MD.hex", &udev->dev) < 0) {
  151. BT_ERR("Mini driver request failed");
  152. usb_free_urb(data->urb);
  153. kfree(data);
  154. return -EIO;
  155. }
  156. BT_DBG("minidrv data %p size %d", firmware->data, firmware->size);
  157. size = max_t(uint, firmware->size, 4096);
  158. data->buffer = kmalloc(size, GFP_KERNEL);
  159. if (!data->buffer) {
  160. BT_ERR("Can't allocate memory for mini driver");
  161. release_firmware(firmware);
  162. usb_free_urb(data->urb);
  163. kfree(data);
  164. return -ENOMEM;
  165. }
  166. memcpy(data->buffer, firmware->data, firmware->size);
  167. usb_fill_bulk_urb(data->urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
  168. data->buffer, firmware->size, bcm203x_complete, data);
  169. release_firmware(firmware);
  170. if (request_firmware(&firmware, "BCM2033-FW.bin", &udev->dev) < 0) {
  171. BT_ERR("Firmware request failed");
  172. usb_free_urb(data->urb);
  173. kfree(data->buffer);
  174. kfree(data);
  175. return -EIO;
  176. }
  177. BT_DBG("firmware data %p size %d", firmware->data, firmware->size);
  178. data->fw_data = kmalloc(firmware->size, GFP_KERNEL);
  179. if (!data->fw_data) {
  180. BT_ERR("Can't allocate memory for firmware image");
  181. release_firmware(firmware);
  182. usb_free_urb(data->urb);
  183. kfree(data->buffer);
  184. kfree(data);
  185. return -ENOMEM;
  186. }
  187. memcpy(data->fw_data, firmware->data, firmware->size);
  188. data->fw_size = firmware->size;
  189. data->fw_sent = 0;
  190. release_firmware(firmware);
  191. INIT_WORK(&data->work, bcm203x_work);
  192. usb_set_intfdata(intf, data);
  193. schedule_work(&data->work);
  194. return 0;
  195. }
  196. static void bcm203x_disconnect(struct usb_interface *intf)
  197. {
  198. struct bcm203x_data *data = usb_get_intfdata(intf);
  199. BT_DBG("intf %p", intf);
  200. usb_kill_urb(data->urb);
  201. usb_set_intfdata(intf, NULL);
  202. usb_free_urb(data->urb);
  203. kfree(data->fw_data);
  204. kfree(data->buffer);
  205. kfree(data);
  206. }
  207. static struct usb_driver bcm203x_driver = {
  208. .name = "bcm203x",
  209. .probe = bcm203x_probe,
  210. .disconnect = bcm203x_disconnect,
  211. .id_table = bcm203x_table,
  212. };
  213. static int __init bcm203x_init(void)
  214. {
  215. int err;
  216. BT_INFO("Broadcom Blutonium firmware driver ver %s", VERSION);
  217. err = usb_register(&bcm203x_driver);
  218. if (err < 0)
  219. BT_ERR("Failed to register USB driver");
  220. return err;
  221. }
  222. static void __exit bcm203x_exit(void)
  223. {
  224. usb_deregister(&bcm203x_driver);
  225. }
  226. module_init(bcm203x_init);
  227. module_exit(bcm203x_exit);
  228. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  229. MODULE_DESCRIPTION("Broadcom Blutonium firmware driver ver " VERSION);
  230. MODULE_VERSION(VERSION);
  231. MODULE_LICENSE("GPL");
  232. MODULE_FIRMWARE("BCM2033-MD.hex");
  233. MODULE_FIRMWARE("BCM2033-FW.bin");