zte_ev.c 8.7 KB

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