nokia.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "u_serial.c"
  37. #include "f_acm.c"
  38. #include "f_ecm.c"
  39. #include "f_obex.c"
  40. #include "f_serial.c"
  41. #include "f_phonet.c"
  42. #include "u_ether.c"
  43. /*-------------------------------------------------------------------------*/
  44. USB_GADGET_COMPOSITE_OPTIONS();
  45. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  46. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  47. /* string IDs are assigned dynamically */
  48. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  49. static char manufacturer_nokia[] = "Nokia";
  50. static const char product_nokia[] = NOKIA_LONG_NAME;
  51. static const char description_nokia[] = "PC-Suite Configuration";
  52. static struct usb_string strings_dev[] = {
  53. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
  54. [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  55. [USB_GADGET_SERIAL_IDX].s = "",
  56. [STRING_DESCRIPTION_IDX].s = description_nokia,
  57. { } /* end of list */
  58. };
  59. static struct usb_gadget_strings stringtab_dev = {
  60. .language = 0x0409, /* en-us */
  61. .strings = strings_dev,
  62. };
  63. static struct usb_gadget_strings *dev_strings[] = {
  64. &stringtab_dev,
  65. NULL,
  66. };
  67. static struct usb_device_descriptor device_desc = {
  68. .bLength = USB_DT_DEVICE_SIZE,
  69. .bDescriptorType = USB_DT_DEVICE,
  70. .bcdUSB = __constant_cpu_to_le16(0x0200),
  71. .bDeviceClass = USB_CLASS_COMM,
  72. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  73. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  74. /* .iManufacturer = DYNAMIC */
  75. /* .iProduct = DYNAMIC */
  76. .bNumConfigurations = 1,
  77. };
  78. /*-------------------------------------------------------------------------*/
  79. /* Module */
  80. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  81. MODULE_AUTHOR("Felipe Balbi");
  82. MODULE_LICENSE("GPL");
  83. /*-------------------------------------------------------------------------*/
  84. static u8 hostaddr[ETH_ALEN];
  85. static int __init nokia_bind_config(struct usb_configuration *c)
  86. {
  87. int status = 0;
  88. status = phonet_bind_config(c);
  89. if (status)
  90. printk(KERN_DEBUG "could not bind phonet config\n");
  91. status = obex_bind_config(c, 0);
  92. if (status)
  93. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  94. status = obex_bind_config(c, 1);
  95. if (status)
  96. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  97. status = acm_bind_config(c, 2);
  98. if (status)
  99. printk(KERN_DEBUG "could not bind acm config\n");
  100. status = ecm_bind_config(c, hostaddr);
  101. if (status)
  102. printk(KERN_DEBUG "could not bind ecm config\n");
  103. return status;
  104. }
  105. static struct usb_configuration nokia_config_500ma_driver = {
  106. .label = "Bus Powered",
  107. .bConfigurationValue = 1,
  108. /* .iConfiguration = DYNAMIC */
  109. .bmAttributes = USB_CONFIG_ATT_ONE,
  110. .bMaxPower = 250, /* 500mA */
  111. };
  112. static struct usb_configuration nokia_config_100ma_driver = {
  113. .label = "Self Powered",
  114. .bConfigurationValue = 2,
  115. /* .iConfiguration = DYNAMIC */
  116. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  117. .bMaxPower = 50, /* 100 mA */
  118. };
  119. static int __init nokia_bind(struct usb_composite_dev *cdev)
  120. {
  121. int gcnum;
  122. struct usb_gadget *gadget = cdev->gadget;
  123. int status;
  124. status = gphonet_setup(cdev->gadget);
  125. if (status < 0)
  126. goto err_phonet;
  127. status = gserial_setup(cdev->gadget, 3);
  128. if (status < 0)
  129. goto err_serial;
  130. status = gether_setup(cdev->gadget, hostaddr);
  131. if (status < 0)
  132. goto err_ether;
  133. status = usb_string_ids_tab(cdev, strings_dev);
  134. if (status < 0)
  135. goto err_usb;
  136. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  137. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  138. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  139. nokia_config_500ma_driver.iConfiguration = status;
  140. nokia_config_100ma_driver.iConfiguration = status;
  141. /* set up other descriptors */
  142. gcnum = usb_gadget_controller_number(gadget);
  143. if (gcnum >= 0)
  144. device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
  145. else {
  146. /* this should only work with hw that supports altsettings
  147. * and several endpoints, anything else, panic.
  148. */
  149. pr_err("nokia_bind: controller '%s' not recognized\n",
  150. gadget->name);
  151. goto err_usb;
  152. }
  153. /* finally register the configuration */
  154. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  155. nokia_bind_config);
  156. if (status < 0)
  157. goto err_usb;
  158. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  159. nokia_bind_config);
  160. if (status < 0)
  161. goto err_usb;
  162. usb_composite_overwrite_options(cdev, &coverwrite);
  163. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  164. return 0;
  165. err_usb:
  166. gether_cleanup();
  167. err_ether:
  168. gserial_cleanup();
  169. err_serial:
  170. gphonet_cleanup();
  171. err_phonet:
  172. return status;
  173. }
  174. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  175. {
  176. gphonet_cleanup();
  177. gserial_cleanup();
  178. gether_cleanup();
  179. return 0;
  180. }
  181. static __refdata struct usb_composite_driver nokia_driver = {
  182. .name = "g_nokia",
  183. .dev = &device_desc,
  184. .strings = dev_strings,
  185. .max_speed = USB_SPEED_HIGH,
  186. .bind = nokia_bind,
  187. .unbind = __exit_p(nokia_unbind),
  188. };
  189. static int __init nokia_init(void)
  190. {
  191. return usb_composite_probe(&nokia_driver);
  192. }
  193. module_init(nokia_init);
  194. static void __exit nokia_cleanup(void)
  195. {
  196. usb_composite_unregister(&nokia_driver);
  197. }
  198. module_exit(nokia_cleanup);