bcm203x.c 7.2 KB

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