nokia.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/device.h>
  19. #include "u_serial.h"
  20. #include "u_ether.h"
  21. #include "u_phonet.h"
  22. #include "gadget_chips.h"
  23. /* Defines */
  24. #define NOKIA_VERSION_NUM 0x0211
  25. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  26. /*-------------------------------------------------------------------------*/
  27. /*
  28. * Kbuild is not very cooperative with respect to linking separately
  29. * compiled library objects into one module. So for now we won't use
  30. * separate compilation ... ensuring init/exit sections work to shrink
  31. * the runtime footprint, and giving us at least some parts of what
  32. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  33. */
  34. #include "u_serial.c"
  35. #include "f_acm.c"
  36. #include "f_ecm.c"
  37. #include "f_obex.c"
  38. #include "f_serial.c"
  39. #include "f_phonet.c"
  40. #include "u_ether.c"
  41. /*-------------------------------------------------------------------------*/
  42. USB_GADGET_COMPOSITE_OPTIONS();
  43. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  44. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  45. /* string IDs are assigned dynamically */
  46. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  47. static char manufacturer_nokia[] = "Nokia";
  48. static const char product_nokia[] = NOKIA_LONG_NAME;
  49. static const char description_nokia[] = "PC-Suite Configuration";
  50. static struct usb_string strings_dev[] = {
  51. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
  52. [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  53. [USB_GADGET_SERIAL_IDX].s = "",
  54. [STRING_DESCRIPTION_IDX].s = description_nokia,
  55. { } /* end of list */
  56. };
  57. static struct usb_gadget_strings stringtab_dev = {
  58. .language = 0x0409, /* en-us */
  59. .strings = strings_dev,
  60. };
  61. static struct usb_gadget_strings *dev_strings[] = {
  62. &stringtab_dev,
  63. NULL,
  64. };
  65. static struct usb_device_descriptor device_desc = {
  66. .bLength = USB_DT_DEVICE_SIZE,
  67. .bDescriptorType = USB_DT_DEVICE,
  68. .bcdUSB = __constant_cpu_to_le16(0x0200),
  69. .bDeviceClass = USB_CLASS_COMM,
  70. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  71. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  72. .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
  73. /* .iManufacturer = DYNAMIC */
  74. /* .iProduct = DYNAMIC */
  75. .bNumConfigurations = 1,
  76. };
  77. /*-------------------------------------------------------------------------*/
  78. /* Module */
  79. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  80. MODULE_AUTHOR("Felipe Balbi");
  81. MODULE_LICENSE("GPL");
  82. /*-------------------------------------------------------------------------*/
  83. static u8 hostaddr[ETH_ALEN];
  84. static int __init nokia_bind_config(struct usb_configuration *c)
  85. {
  86. int status = 0;
  87. status = phonet_bind_config(c);
  88. if (status)
  89. printk(KERN_DEBUG "could not bind phonet config\n");
  90. status = obex_bind_config(c, 0);
  91. if (status)
  92. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  93. status = obex_bind_config(c, 1);
  94. if (status)
  95. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  96. status = acm_bind_config(c, 2);
  97. if (status)
  98. printk(KERN_DEBUG "could not bind acm config\n");
  99. status = ecm_bind_config(c, hostaddr);
  100. if (status)
  101. printk(KERN_DEBUG "could not bind ecm config\n");
  102. return status;
  103. }
  104. static struct usb_configuration nokia_config_500ma_driver = {
  105. .label = "Bus Powered",
  106. .bConfigurationValue = 1,
  107. /* .iConfiguration = DYNAMIC */
  108. .bmAttributes = USB_CONFIG_ATT_ONE,
  109. .bMaxPower = 250, /* 500mA */
  110. };
  111. static struct usb_configuration nokia_config_100ma_driver = {
  112. .label = "Self Powered",
  113. .bConfigurationValue = 2,
  114. /* .iConfiguration = DYNAMIC */
  115. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  116. .bMaxPower = 50, /* 100 mA */
  117. };
  118. static int __init nokia_bind(struct usb_composite_dev *cdev)
  119. {
  120. struct usb_gadget *gadget = cdev->gadget;
  121. int status;
  122. status = gphonet_setup(cdev->gadget);
  123. if (status < 0)
  124. goto err_phonet;
  125. status = gserial_setup(cdev->gadget, 3);
  126. if (status < 0)
  127. goto err_serial;
  128. status = gether_setup(cdev->gadget, hostaddr);
  129. if (status < 0)
  130. goto err_ether;
  131. status = usb_string_ids_tab(cdev, strings_dev);
  132. if (status < 0)
  133. goto err_usb;
  134. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  135. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  136. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  137. nokia_config_500ma_driver.iConfiguration = status;
  138. nokia_config_100ma_driver.iConfiguration = status;
  139. if (!gadget_supports_altsettings(gadget))
  140. goto err_usb;
  141. /* finally register the configuration */
  142. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  143. nokia_bind_config);
  144. if (status < 0)
  145. goto err_usb;
  146. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  147. nokia_bind_config);
  148. if (status < 0)
  149. goto err_usb;
  150. usb_composite_overwrite_options(cdev, &coverwrite);
  151. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  152. return 0;
  153. err_usb:
  154. gether_cleanup();
  155. err_ether:
  156. gserial_cleanup();
  157. err_serial:
  158. gphonet_cleanup();
  159. err_phonet:
  160. return status;
  161. }
  162. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  163. {
  164. gphonet_cleanup();
  165. gserial_cleanup();
  166. gether_cleanup();
  167. return 0;
  168. }
  169. static __refdata struct usb_composite_driver nokia_driver = {
  170. .name = "g_nokia",
  171. .dev = &device_desc,
  172. .strings = dev_strings,
  173. .max_speed = USB_SPEED_HIGH,
  174. .bind = nokia_bind,
  175. .unbind = __exit_p(nokia_unbind),
  176. };
  177. static int __init nokia_init(void)
  178. {
  179. return usb_composite_probe(&nokia_driver);
  180. }
  181. module_init(nokia_init);
  182. static void __exit nokia_cleanup(void)
  183. {
  184. usb_composite_unregister(&nokia_driver);
  185. }
  186. module_exit(nokia_cleanup);