zte_ev.c 8.2 KB

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