zte_ev.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * ZTE_EV USB serial driver
  3. *
  4. * Copyright (C) 2012 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  5. * Copyright (C) 2012 Linux Foundation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This driver is based on code found in a ZTE_ENV patch that modified
  12. * the usb-serial generic driver. Comments were left in that I think
  13. * show the commands used to talk to the device, but I am not sure.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/tty.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. #include <linux/uaccess.h>
  22. #define MAX_SETUP_DATA_SIZE 32
  23. static void debug_data(struct device *dev, const char *function, int len,
  24. const unsigned char *data, int result)
  25. {
  26. dev_dbg(dev, "result = %d\n", result);
  27. if (result == len)
  28. dev_dbg(dev, "%s - length = %d, data = %*ph\n", function,
  29. len, len, data);
  30. }
  31. static int zte_ev_usb_serial_open(struct tty_struct *tty,
  32. struct usb_serial_port *port)
  33. {
  34. struct usb_device *udev = port->serial->dev;
  35. struct device *dev = &port->dev;
  36. int result = 0;
  37. int len;
  38. unsigned char *buf;
  39. buf = kmalloc(MAX_SETUP_DATA_SIZE, GFP_KERNEL);
  40. if (!buf)
  41. return -ENOMEM;
  42. /* send 1st ctl cmd(CTL 21 22 01 00 00 00 00 00) */
  43. len = 0;
  44. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  45. 0x22, 0x21,
  46. 0x0001, 0x0000, NULL, len,
  47. USB_CTRL_GET_TIMEOUT);
  48. dev_dbg(dev, "result = %d\n", result);
  49. /* send 2st cmd and receive data */
  50. /*
  51. * 16.0 CTL a1 21 00 00 00 00 07 00 CLASS 25.1.0(5)
  52. * 16.0 DI 00 96 00 00 00 00 08
  53. */
  54. len = 0x0007;
  55. result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  56. 0x21, 0xa1,
  57. 0x0000, 0x0000, buf, len,
  58. USB_CTRL_GET_TIMEOUT);
  59. debug_data(dev, __func__, len, buf, result);
  60. /* send 3rd cmd */
  61. /*
  62. * 16.0 CTL 21 20 00 00 00 00 07 00 CLASS 30.1.0
  63. * 16.0 DO 80 25 00 00 00 00 08 .%..... 30.2.0
  64. */
  65. len = 0x0007;
  66. buf[0] = 0x80;
  67. buf[1] = 0x25;
  68. buf[2] = 0x00;
  69. buf[3] = 0x00;
  70. buf[4] = 0x00;
  71. buf[5] = 0x00;
  72. buf[6] = 0x08;
  73. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  74. 0x20, 0x21,
  75. 0x0000, 0x0000, buf, len,
  76. USB_CTRL_GET_TIMEOUT);
  77. debug_data(dev, __func__, len, buf, result);
  78. /* send 4th cmd */
  79. /*
  80. * 16.0 CTL 21 22 03 00 00 00 00 00
  81. */
  82. len = 0;
  83. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  84. 0x22, 0x21,
  85. 0x0003, 0x0000, NULL, len,
  86. USB_CTRL_GET_TIMEOUT);
  87. dev_dbg(dev, "result = %d\n", result);
  88. /* send 5th cmd */
  89. /*
  90. * 16.0 CTL a1 21 00 00 00 00 07 00 CLASS 33.1.0
  91. * 16.0 DI 80 25 00 00 00 00 08
  92. */
  93. len = 0x0007;
  94. result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  95. 0x21, 0xa1,
  96. 0x0000, 0x0000, buf, len,
  97. USB_CTRL_GET_TIMEOUT);
  98. debug_data(dev, __func__, len, buf, result);
  99. /* send 6th cmd */
  100. /*
  101. * 16.0 CTL 21 20 00 00 00 00 07 00 CLASS 34.1.0
  102. * 16.0 DO 80 25 00 00 00 00 08
  103. */
  104. len = 0x0007;
  105. buf[0] = 0x80;
  106. buf[1] = 0x25;
  107. buf[2] = 0x00;
  108. buf[3] = 0x00;
  109. buf[4] = 0x00;
  110. buf[5] = 0x00;
  111. buf[6] = 0x08;
  112. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  113. 0x20, 0x21,
  114. 0x0000, 0x0000, buf, len,
  115. USB_CTRL_GET_TIMEOUT);
  116. debug_data(dev, __func__, len, buf, result);
  117. kfree(buf);
  118. return usb_serial_generic_open(tty, port);
  119. }
  120. /*
  121. * CTL 21 22 02 00 00 00 00 00 CLASS 338.1.0
  122. *
  123. * 16.1 DI a1 20 00 00 00 00 02 00 02 00 . ........ 340.1.0
  124. * 16.0 CTL 21 22 03 00 00 00 00 00 CLASS 341.1.0
  125. *
  126. * 16.0 CTL a1 21 00 00 00 00 07 00 CLASS 346.1.0(3)
  127. * 16.0 DI 00 08 07 00 00 00 08 ....... 346.2.0
  128. *
  129. * 16.0 CTL 21 20 00 00 00 00 07 00 CLASS 349.1.0
  130. * 16.0 DO 00 c2 01 00 00 00 08 ....... 349.2.0
  131. *
  132. * 16.0 CTL 21 22 03 00 00 00 00 00 CLASS 350.1.0(2)
  133. *
  134. * 16.0 CTL a1 21 00 00 00 00 07 00 CLASS 352.1.0
  135. * 16.0 DI 00 c2 01 00 00 00 08 ....... 352.2.0
  136. *
  137. * 16.1 DI a1 20 00 00 00 00 02 00 02 00 . ........ 353.1.0
  138. *
  139. * 16.0 CTL 21 20 00 00 00 00 07 00 CLASS 354.1.0
  140. * 16.0 DO 00 c2 01 00 00 00 08 ....... 354.2.0
  141. *
  142. * 16.0 CTL 21 22 03 00 00 00 00 00
  143. */
  144. static void zte_ev_usb_serial_close(struct usb_serial_port *port)
  145. {
  146. struct usb_device *udev = port->serial->dev;
  147. struct device *dev = &port->dev;
  148. int result = 0;
  149. int len;
  150. unsigned char *buf;
  151. buf = kmalloc(MAX_SETUP_DATA_SIZE, GFP_KERNEL);
  152. if (!buf)
  153. return;
  154. /* send 1st ctl cmd(CTL 21 22 02 00 00 00 00 00) */
  155. len = 0;
  156. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  157. 0x22, 0x21,
  158. 0x0002, 0x0000, NULL, len,
  159. USB_CTRL_GET_TIMEOUT);
  160. dev_dbg(dev, "result = %d\n", result);
  161. /* send 2st ctl cmd(CTL 21 22 03 00 00 00 00 00 ) */
  162. len = 0;
  163. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  164. 0x22, 0x21,
  165. 0x0003, 0x0000, NULL, len,
  166. USB_CTRL_GET_TIMEOUT);
  167. dev_dbg(dev, "result = %d\n", result);
  168. /* send 3st cmd and recieve data */
  169. /*
  170. * 16.0 CTL a1 21 00 00 00 00 07 00 CLASS 25.1.0(5)
  171. * 16.0 DI 00 08 07 00 00 00 08
  172. */
  173. len = 0x0007;
  174. result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  175. 0x21, 0xa1,
  176. 0x0000, 0x0000, buf, len,
  177. USB_CTRL_GET_TIMEOUT);
  178. debug_data(dev, __func__, len, buf, result);
  179. /* send 4th cmd */
  180. /*
  181. * 16.0 CTL 21 20 00 00 00 00 07 00 CLASS 30.1.0
  182. * 16.0 DO 00 c2 01 00 00 00 08 .%..... 30.2.0
  183. */
  184. len = 0x0007;
  185. buf[0] = 0x00;
  186. buf[1] = 0xc2;
  187. buf[2] = 0x01;
  188. buf[3] = 0x00;
  189. buf[4] = 0x00;
  190. buf[5] = 0x00;
  191. buf[6] = 0x08;
  192. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  193. 0x20, 0x21,
  194. 0x0000, 0x0000, buf, len,
  195. USB_CTRL_GET_TIMEOUT);
  196. debug_data(dev, __func__, len, buf, result);
  197. /* send 5th cmd */
  198. /*
  199. * 16.0 CTL 21 22 03 00 00 00 00 00
  200. */
  201. len = 0;
  202. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  203. 0x22, 0x21,
  204. 0x0003, 0x0000, NULL, len,
  205. USB_CTRL_GET_TIMEOUT);
  206. dev_dbg(dev, "result = %d\n", result);
  207. /* send 6th cmd */
  208. /*
  209. * 16.0 CTL a1 21 00 00 00 00 07 00 CLASS 33.1.0
  210. * 16.0 DI 00 c2 01 00 00 00 08
  211. */
  212. len = 0x0007;
  213. result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  214. 0x21, 0xa1,
  215. 0x0000, 0x0000, buf, len,
  216. USB_CTRL_GET_TIMEOUT);
  217. debug_data(dev, __func__, len, buf, result);
  218. /* send 7th cmd */
  219. /*
  220. * 16.0 CTL 21 20 00 00 00 00 07 00 CLASS 354.1.0
  221. * 16.0 DO 00 c2 01 00 00 00 08 ....... 354.2.0
  222. */
  223. len = 0x0007;
  224. buf[0] = 0x00;
  225. buf[1] = 0xc2;
  226. buf[2] = 0x01;
  227. buf[3] = 0x00;
  228. buf[4] = 0x00;
  229. buf[5] = 0x00;
  230. buf[6] = 0x08;
  231. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  232. 0x20, 0x21,
  233. 0x0000, 0x0000, buf, len,
  234. USB_CTRL_GET_TIMEOUT);
  235. debug_data(dev, __func__, len, buf, result);
  236. /* send 8th cmd */
  237. /*
  238. * 16.0 CTL 21 22 03 00 00 00 00 00
  239. */
  240. len = 0;
  241. result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  242. 0x22, 0x21,
  243. 0x0003, 0x0000, NULL, len,
  244. USB_CTRL_GET_TIMEOUT);
  245. dev_dbg(dev, "result = %d\n", result);
  246. kfree(buf);
  247. usb_serial_generic_close(port);
  248. }
  249. static const struct usb_device_id id_table[] = {
  250. /* AC8710, AC8710T */
  251. { USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xffff, 0xff, 0xff, 0xff) },
  252. /* AC8700 */
  253. { USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xfffe, 0xff, 0xff, 0xff) },
  254. /* MG880 */
  255. { USB_DEVICE(0x19d2, 0xfffd) },
  256. { USB_DEVICE(0x19d2, 0xfffc) },
  257. { USB_DEVICE(0x19d2, 0xfffb) },
  258. /* AC8710_V3 */
  259. { USB_DEVICE(0x19d2, 0xfff6) },
  260. { USB_DEVICE(0x19d2, 0xfff7) },
  261. { USB_DEVICE(0x19d2, 0xfff8) },
  262. { USB_DEVICE(0x19d2, 0xfff9) },
  263. { USB_DEVICE(0x19d2, 0xffee) },
  264. /* AC2716, MC2716 */
  265. { USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xffed, 0xff, 0xff, 0xff) },
  266. /* AD3812 */
  267. { USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xffeb, 0xff, 0xff, 0xff) },
  268. { USB_DEVICE(0x19d2, 0xffec) },
  269. { USB_DEVICE(0x05C6, 0x3197) },
  270. { USB_DEVICE(0x05C6, 0x6000) },
  271. { USB_DEVICE(0x05C6, 0x9008) },
  272. { },
  273. };
  274. MODULE_DEVICE_TABLE(usb, id_table);
  275. static struct usb_serial_driver zio_device = {
  276. .driver = {
  277. .owner = THIS_MODULE,
  278. .name = "zte_ev",
  279. },
  280. .id_table = id_table,
  281. .num_ports = 1,
  282. .open = zte_ev_usb_serial_open,
  283. .close = zte_ev_usb_serial_close,
  284. };
  285. static struct usb_serial_driver * const serial_drivers[] = {
  286. &zio_device, NULL
  287. };
  288. module_usb_serial_driver(serial_drivers, id_table);
  289. MODULE_LICENSE("GPL v2");