nokia.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * nokia.c -- Nokia Composite Gadget Driver
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  6. *
  7. * This gadget driver borrows from serial.c which is:
  8. *
  9. * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  10. * Copyright (C) 2008 by David Brownell
  11. * Copyright (C) 2008 by Nokia Corporation
  12. *
  13. * This software is distributed under the terms of the GNU General
  14. * Public License ("GPL") as published by the Free Software Foundation,
  15. * version 2 of that License.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/utsname.h>
  19. #include <linux/device.h>
  20. #include "u_serial.h"
  21. #include "u_ether.h"
  22. #include "u_phonet.h"
  23. #include "gadget_chips.h"
  24. /* Defines */
  25. #define NOKIA_VERSION_NUM 0x0211
  26. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  27. /*-------------------------------------------------------------------------*/
  28. /*
  29. * Kbuild is not very cooperative with respect to linking separately
  30. * compiled library objects into one module. So for now we won't use
  31. * separate compilation ... ensuring init/exit sections work to shrink
  32. * the runtime footprint, and giving us at least some parts of what
  33. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  34. */
  35. #include "composite.c"
  36. #include "config.c"
  37. #include "epautoconf.c"
  38. #include "u_serial.c"
  39. #include "f_acm.c"
  40. #include "f_ecm.c"
  41. #include "f_obex.c"
  42. #include "f_serial.c"
  43. #include "f_phonet.c"
  44. #include "u_ether.c"
  45. /*-------------------------------------------------------------------------*/
  46. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  47. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  48. /* string IDs are assigned dynamically */
  49. #define STRING_MANUFACTURER_IDX 0
  50. #define STRING_PRODUCT_IDX 1
  51. #define STRING_DESCRIPTION_IDX 2
  52. static char manufacturer_nokia[] = "Nokia";
  53. static const char product_nokia[] = NOKIA_LONG_NAME;
  54. static const char description_nokia[] = "PC-Suite Configuration";
  55. static struct usb_string strings_dev[] = {
  56. [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
  57. [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  58. [STRING_DESCRIPTION_IDX].s = description_nokia,
  59. { } /* end of list */
  60. };
  61. static struct usb_gadget_strings stringtab_dev = {
  62. .language = 0x0409, /* en-us */
  63. .strings = strings_dev,
  64. };
  65. static struct usb_gadget_strings *dev_strings[] = {
  66. &stringtab_dev,
  67. NULL,
  68. };
  69. static struct usb_device_descriptor device_desc = {
  70. .bLength = USB_DT_DEVICE_SIZE,
  71. .bDescriptorType = USB_DT_DEVICE,
  72. .bcdUSB = __constant_cpu_to_le16(0x0200),
  73. .bDeviceClass = USB_CLASS_COMM,
  74. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  75. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  76. /* .iManufacturer = DYNAMIC */
  77. /* .iProduct = DYNAMIC */
  78. .bNumConfigurations = 1,
  79. };
  80. /*-------------------------------------------------------------------------*/
  81. /* Module */
  82. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  83. MODULE_AUTHOR("Felipe Balbi");
  84. MODULE_LICENSE("GPL");
  85. /*-------------------------------------------------------------------------*/
  86. static u8 hostaddr[ETH_ALEN];
  87. static int __init nokia_bind_config(struct usb_configuration *c)
  88. {
  89. int status = 0;
  90. status = phonet_bind_config(c);
  91. if (status)
  92. printk(KERN_DEBUG "could not bind phonet config\n");
  93. status = obex_bind_config(c, 0);
  94. if (status)
  95. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  96. status = obex_bind_config(c, 1);
  97. if (status)
  98. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  99. status = acm_bind_config(c, 2);
  100. if (status)
  101. printk(KERN_DEBUG "could not bind acm config\n");
  102. status = ecm_bind_config(c, hostaddr);
  103. if (status)
  104. printk(KERN_DEBUG "could not bind ecm config\n");
  105. return status;
  106. }
  107. static struct usb_configuration nokia_config_500ma_driver = {
  108. .label = "Bus Powered",
  109. .bConfigurationValue = 1,
  110. /* .iConfiguration = DYNAMIC */
  111. .bmAttributes = USB_CONFIG_ATT_ONE,
  112. .bMaxPower = 250, /* 500mA */
  113. };
  114. static struct usb_configuration nokia_config_100ma_driver = {
  115. .label = "Self Powered",
  116. .bConfigurationValue = 2,
  117. /* .iConfiguration = DYNAMIC */
  118. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  119. .bMaxPower = 50, /* 100 mA */
  120. };
  121. static int __init nokia_bind(struct usb_composite_dev *cdev)
  122. {
  123. int gcnum;
  124. struct usb_gadget *gadget = cdev->gadget;
  125. int status;
  126. status = gphonet_setup(cdev->gadget);
  127. if (status < 0)
  128. goto err_phonet;
  129. status = gserial_setup(cdev->gadget, 3);
  130. if (status < 0)
  131. goto err_serial;
  132. status = gether_setup(cdev->gadget, hostaddr);
  133. if (status < 0)
  134. goto err_ether;
  135. status = usb_string_id(cdev);
  136. if (status < 0)
  137. goto err_usb;
  138. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  139. device_desc.iManufacturer = status;
  140. status = usb_string_id(cdev);
  141. if (status < 0)
  142. goto err_usb;
  143. strings_dev[STRING_PRODUCT_IDX].id = status;
  144. device_desc.iProduct = status;
  145. /* config description */
  146. status = usb_string_id(cdev);
  147. if (status < 0)
  148. goto err_usb;
  149. strings_dev[STRING_DESCRIPTION_IDX].id = status;
  150. nokia_config_500ma_driver.iConfiguration = status;
  151. nokia_config_100ma_driver.iConfiguration = status;
  152. /* set up other descriptors */
  153. gcnum = usb_gadget_controller_number(gadget);
  154. if (gcnum >= 0)
  155. device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
  156. else {
  157. /* this should only work with hw that supports altsettings
  158. * and several endpoints, anything else, panic.
  159. */
  160. pr_err("nokia_bind: controller '%s' not recognized\n",
  161. gadget->name);
  162. goto err_usb;
  163. }
  164. /* finally register the configuration */
  165. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  166. nokia_bind_config);
  167. if (status < 0)
  168. goto err_usb;
  169. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  170. nokia_bind_config);
  171. if (status < 0)
  172. goto err_usb;
  173. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  174. return 0;
  175. err_usb:
  176. gether_cleanup();
  177. err_ether:
  178. gserial_cleanup();
  179. err_serial:
  180. gphonet_cleanup();
  181. err_phonet:
  182. return status;
  183. }
  184. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  185. {
  186. gphonet_cleanup();
  187. gserial_cleanup();
  188. gether_cleanup();
  189. return 0;
  190. }
  191. static __refdata struct usb_composite_driver nokia_driver = {
  192. .name = "g_nokia",
  193. .dev = &device_desc,
  194. .strings = dev_strings,
  195. .max_speed = USB_SPEED_HIGH,
  196. .bind = nokia_bind,
  197. .unbind = __exit_p(nokia_unbind),
  198. };
  199. static int __init nokia_init(void)
  200. {
  201. return usb_composite_probe(&nokia_driver);
  202. }
  203. module_init(nokia_init);
  204. static void __exit nokia_cleanup(void)
  205. {
  206. usb_composite_unregister(&nokia_driver);
  207. }
  208. module_exit(nokia_cleanup);