option_ms.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Driver for Option High Speed Mobile Devices.
  3. *
  4. * (c) 2008 Dan Williams <dcbw@redhat.com>
  5. *
  6. * Inspiration taken from sierra_ms.c by Kevin Lloyd <klloyd@sierrawireless.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/usb.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include "usb.h"
  26. #include "transport.h"
  27. #include "option_ms.h"
  28. #include "debug.h"
  29. #define ZCD_FORCE_MODEM 0x01
  30. #define ZCD_ALLOW_MS 0x02
  31. static unsigned int option_zero_cd = ZCD_FORCE_MODEM;
  32. module_param(option_zero_cd, uint, S_IRUGO | S_IWUSR);
  33. MODULE_PARM_DESC(option_zero_cd, "ZeroCD mode (1=Force Modem (default),"
  34. " 2=Allow CD-Rom");
  35. #define RESPONSE_LEN 1024
  36. static int option_rezero(struct us_data *us)
  37. {
  38. const unsigned char rezero_msg[] = {
  39. 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12,
  40. 0x01, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x01,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  43. };
  44. char *buffer;
  45. int result;
  46. usb_stor_dbg(us, "Option MS: %s\n", "DEVICE MODE SWITCH");
  47. buffer = kzalloc(RESPONSE_LEN, GFP_KERNEL);
  48. if (buffer == NULL)
  49. return USB_STOR_TRANSPORT_ERROR;
  50. memcpy(buffer, rezero_msg, sizeof(rezero_msg));
  51. result = usb_stor_bulk_transfer_buf(us,
  52. us->send_bulk_pipe,
  53. buffer, sizeof(rezero_msg), NULL);
  54. if (result != USB_STOR_XFER_GOOD) {
  55. result = USB_STOR_XFER_ERROR;
  56. goto out;
  57. }
  58. /* Some of the devices need to be asked for a response, but we don't
  59. * care what that response is.
  60. */
  61. usb_stor_bulk_transfer_buf(us,
  62. us->recv_bulk_pipe,
  63. buffer, RESPONSE_LEN, NULL);
  64. /* Read the CSW */
  65. usb_stor_bulk_transfer_buf(us,
  66. us->recv_bulk_pipe,
  67. buffer, 13, NULL);
  68. result = USB_STOR_XFER_GOOD;
  69. out:
  70. kfree(buffer);
  71. return result;
  72. }
  73. static int option_inquiry(struct us_data *us)
  74. {
  75. const unsigned char inquiry_msg[] = {
  76. 0x55, 0x53, 0x42, 0x43, 0x12, 0x34, 0x56, 0x78,
  77. 0x24, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x12,
  78. 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  80. };
  81. char *buffer;
  82. int result;
  83. usb_stor_dbg(us, "Option MS: %s\n", "device inquiry for vendor name");
  84. buffer = kzalloc(0x24, GFP_KERNEL);
  85. if (buffer == NULL)
  86. return USB_STOR_TRANSPORT_ERROR;
  87. memcpy(buffer, inquiry_msg, sizeof(inquiry_msg));
  88. result = usb_stor_bulk_transfer_buf(us,
  89. us->send_bulk_pipe,
  90. buffer, sizeof(inquiry_msg), NULL);
  91. if (result != USB_STOR_XFER_GOOD) {
  92. result = USB_STOR_XFER_ERROR;
  93. goto out;
  94. }
  95. result = usb_stor_bulk_transfer_buf(us,
  96. us->recv_bulk_pipe,
  97. buffer, 0x24, NULL);
  98. if (result != USB_STOR_XFER_GOOD) {
  99. result = USB_STOR_XFER_ERROR;
  100. goto out;
  101. }
  102. result = memcmp(buffer+8, "Option", 6);
  103. if (result != 0)
  104. result = memcmp(buffer+8, "ZCOPTION", 8);
  105. /* Read the CSW */
  106. usb_stor_bulk_transfer_buf(us,
  107. us->recv_bulk_pipe,
  108. buffer, 13, NULL);
  109. out:
  110. kfree(buffer);
  111. return result;
  112. }
  113. int option_ms_init(struct us_data *us)
  114. {
  115. int result;
  116. usb_stor_dbg(us, "Option MS: %s\n", "option_ms_init called");
  117. /* Additional test for vendor information via INQUIRY,
  118. * because some vendor/product IDs are ambiguous
  119. */
  120. result = option_inquiry(us);
  121. if (result != 0) {
  122. usb_stor_dbg(us, "Option MS: %s\n",
  123. "vendor is not Option or not determinable, no action taken");
  124. return 0;
  125. } else
  126. usb_stor_dbg(us, "Option MS: %s\n",
  127. "this is a genuine Option device, proceeding");
  128. /* Force Modem mode */
  129. if (option_zero_cd == ZCD_FORCE_MODEM) {
  130. usb_stor_dbg(us, "Option MS: %s\n", "Forcing Modem Mode");
  131. result = option_rezero(us);
  132. if (result != USB_STOR_XFER_GOOD)
  133. usb_stor_dbg(us, "Option MS: %s\n",
  134. "Failed to switch to modem mode");
  135. return -EIO;
  136. } else if (option_zero_cd == ZCD_ALLOW_MS) {
  137. /* Allow Mass Storage mode (keep CD-Rom) */
  138. usb_stor_dbg(us, "Option MS: %s\n",
  139. "Allowing Mass Storage Mode if device requests it");
  140. }
  141. return 0;
  142. }