bcm203x.c 7.1 KB

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