quatech2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * usb-serial driver for Quatech USB 2 devices
  4. *
  5. * Copyright (C) 2012 Bill Pemberton (wfp5p@virginia.edu)
  6. *
  7. * These devices all have only 1 bulk in and 1 bulk out that is shared
  8. * for all serial ports.
  9. *
  10. */
  11. #include <asm/unaligned.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/serial.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. #include <linux/serial_reg.h>
  22. #include <linux/uaccess.h>
  23. /* default urb timeout for usb operations */
  24. #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
  25. #define QT_OPEN_CLOSE_CHANNEL 0xca
  26. #define QT_SET_GET_DEVICE 0xc2
  27. #define QT_SET_GET_REGISTER 0xc0
  28. #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
  29. #define QT_SET_ATF 0xcd
  30. #define QT_TRANSFER_IN 0xc0
  31. #define QT_HW_FLOW_CONTROL_MASK 0xc5
  32. #define QT_SW_FLOW_CONTROL_MASK 0xc6
  33. #define QT2_BREAK_CONTROL 0xc8
  34. #define QT2_GET_SET_UART 0xc1
  35. #define QT2_FLUSH_DEVICE 0xc4
  36. #define QT2_GET_SET_QMCR 0xe1
  37. #define QT2_QMCR_RS232 0x40
  38. #define QT2_QMCR_RS422 0x10
  39. #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
  40. #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
  41. /* status bytes for the device */
  42. #define QT2_CONTROL_BYTE 0x1b
  43. #define QT2_LINE_STATUS 0x00 /* following 1 byte is line status */
  44. #define QT2_MODEM_STATUS 0x01 /* following 1 byte is modem status */
  45. #define QT2_XMIT_HOLD 0x02 /* following 2 bytes are ?? */
  46. #define QT2_CHANGE_PORT 0x03 /* following 1 byte is port to change to */
  47. #define QT2_REC_FLUSH 0x04 /* no following info */
  48. #define QT2_XMIT_FLUSH 0x05 /* no following info */
  49. #define QT2_CONTROL_ESCAPE 0xff /* pass through previous 2 control bytes */
  50. #define MAX_BAUD_RATE 921600
  51. #define DEFAULT_BAUD_RATE 9600
  52. #define QT2_READ_BUFFER_SIZE 512 /* size of read buffer */
  53. #define QT2_WRITE_BUFFER_SIZE 512 /* size of write buffer */
  54. #define QT2_WRITE_CONTROL_SIZE 5 /* control bytes used for a write */
  55. #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
  56. #define USB_VENDOR_ID_QUATECH 0x061d
  57. #define QUATECH_SSU2_100 0xC120 /* RS232 single port */
  58. #define QUATECH_DSU2_100 0xC140 /* RS232 dual port */
  59. #define QUATECH_DSU2_400 0xC150 /* RS232/422/485 dual port */
  60. #define QUATECH_QSU2_100 0xC160 /* RS232 four port */
  61. #define QUATECH_QSU2_400 0xC170 /* RS232/422/485 four port */
  62. #define QUATECH_ESU2_100 0xC1A0 /* RS232 eight port */
  63. #define QUATECH_ESU2_400 0xC180 /* RS232/422/485 eight port */
  64. struct qt2_device_detail {
  65. int product_id;
  66. int num_ports;
  67. };
  68. #define QT_DETAILS(prod, ports) \
  69. .product_id = (prod), \
  70. .num_ports = (ports)
  71. static const struct qt2_device_detail qt2_device_details[] = {
  72. {QT_DETAILS(QUATECH_SSU2_100, 1)},
  73. {QT_DETAILS(QUATECH_DSU2_400, 2)},
  74. {QT_DETAILS(QUATECH_DSU2_100, 2)},
  75. {QT_DETAILS(QUATECH_QSU2_400, 4)},
  76. {QT_DETAILS(QUATECH_QSU2_100, 4)},
  77. {QT_DETAILS(QUATECH_ESU2_400, 8)},
  78. {QT_DETAILS(QUATECH_ESU2_100, 8)},
  79. {QT_DETAILS(0, 0)} /* Terminating entry */
  80. };
  81. static const struct usb_device_id id_table[] = {
  82. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
  83. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
  84. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
  85. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
  86. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
  87. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
  88. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
  89. {} /* Terminating entry */
  90. };
  91. MODULE_DEVICE_TABLE(usb, id_table);
  92. struct qt2_serial_private {
  93. unsigned char current_port; /* current port for incoming data */
  94. struct urb *read_urb; /* shared among all ports */
  95. char *read_buffer;
  96. };
  97. struct qt2_port_private {
  98. u8 device_port;
  99. spinlock_t urb_lock;
  100. bool urb_in_use;
  101. struct urb *write_urb;
  102. char *write_buffer;
  103. spinlock_t lock;
  104. u8 shadowLSR;
  105. u8 shadowMSR;
  106. struct usb_serial_port *port;
  107. };
  108. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch);
  109. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch);
  110. static void qt2_write_bulk_callback(struct urb *urb);
  111. static void qt2_read_bulk_callback(struct urb *urb);
  112. static void qt2_release(struct usb_serial *serial)
  113. {
  114. struct qt2_serial_private *serial_priv;
  115. serial_priv = usb_get_serial_data(serial);
  116. usb_kill_urb(serial_priv->read_urb);
  117. usb_free_urb(serial_priv->read_urb);
  118. kfree(serial_priv->read_buffer);
  119. kfree(serial_priv);
  120. }
  121. static inline int calc_baud_divisor(int baudrate)
  122. {
  123. int divisor, rem;
  124. divisor = MAX_BAUD_RATE / baudrate;
  125. rem = MAX_BAUD_RATE % baudrate;
  126. /* Round to nearest divisor */
  127. if (((rem * 2) >= baudrate) && (baudrate != 110))
  128. divisor++;
  129. return divisor;
  130. }
  131. static inline int qt2_set_port_config(struct usb_device *dev,
  132. unsigned char port_number,
  133. u16 baudrate, u16 lcr)
  134. {
  135. int divisor = calc_baud_divisor(baudrate);
  136. u16 index = ((u16) (lcr << 8) | (u16) (port_number));
  137. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  138. QT2_GET_SET_UART, 0x40,
  139. divisor, index, NULL, 0, QT2_USB_TIMEOUT);
  140. }
  141. static inline int qt2_control_msg(struct usb_device *dev,
  142. u8 request, u16 data, u16 index)
  143. {
  144. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  145. request, 0x40, data, index,
  146. NULL, 0, QT2_USB_TIMEOUT);
  147. }
  148. static inline int qt2_setdevice(struct usb_device *dev, u8 *data)
  149. {
  150. u16 x = ((u16) (data[1] << 8) | (u16) (data[0]));
  151. return qt2_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
  152. }
  153. static inline int qt2_getregister(struct usb_device *dev,
  154. u8 uart,
  155. u8 reg,
  156. u8 *data)
  157. {
  158. int ret;
  159. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  160. QT_SET_GET_REGISTER, 0xc0, reg,
  161. uart, data, sizeof(*data), QT2_USB_TIMEOUT);
  162. if (ret < sizeof(*data)) {
  163. if (ret >= 0)
  164. ret = -EIO;
  165. }
  166. return ret;
  167. }
  168. static inline int qt2_setregister(struct usb_device *dev,
  169. u8 uart, u8 reg, u16 data)
  170. {
  171. u16 value = (data << 8) | reg;
  172. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  173. QT_SET_GET_REGISTER, 0x40, value, uart,
  174. NULL, 0, QT2_USB_TIMEOUT);
  175. }
  176. static inline int update_mctrl(struct qt2_port_private *port_priv,
  177. unsigned int set, unsigned int clear)
  178. {
  179. struct usb_serial_port *port = port_priv->port;
  180. struct usb_device *dev = port->serial->dev;
  181. unsigned urb_value;
  182. int status;
  183. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
  184. dev_dbg(&port->dev,
  185. "update_mctrl - DTR|RTS not being set|cleared\n");
  186. return 0; /* no change */
  187. }
  188. clear &= ~set; /* 'set' takes precedence over 'clear' */
  189. urb_value = 0;
  190. if (set & TIOCM_DTR)
  191. urb_value |= UART_MCR_DTR;
  192. if (set & TIOCM_RTS)
  193. urb_value |= UART_MCR_RTS;
  194. status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
  195. urb_value);
  196. if (status < 0)
  197. dev_err(&port->dev,
  198. "update_mctrl - Error from MODEM_CTRL urb: %i\n",
  199. status);
  200. return status;
  201. }
  202. static int qt2_calc_num_ports(struct usb_serial *serial,
  203. struct usb_serial_endpoints *epds)
  204. {
  205. struct qt2_device_detail d;
  206. int i;
  207. for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
  208. if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
  209. return d.num_ports;
  210. }
  211. /* we didn't recognize the device */
  212. dev_err(&serial->dev->dev,
  213. "don't know the number of ports, assuming 1\n");
  214. return 1;
  215. }
  216. static void qt2_set_termios(struct tty_struct *tty,
  217. struct usb_serial_port *port,
  218. struct ktermios *old_termios)
  219. {
  220. struct usb_device *dev = port->serial->dev;
  221. struct qt2_port_private *port_priv;
  222. struct ktermios *termios = &tty->termios;
  223. u16 baud;
  224. unsigned int cflag = termios->c_cflag;
  225. u16 new_lcr = 0;
  226. int status;
  227. port_priv = usb_get_serial_port_data(port);
  228. if (cflag & PARENB) {
  229. if (cflag & PARODD)
  230. new_lcr |= UART_LCR_PARITY;
  231. else
  232. new_lcr |= SERIAL_EVEN_PARITY;
  233. }
  234. switch (cflag & CSIZE) {
  235. case CS5:
  236. new_lcr |= UART_LCR_WLEN5;
  237. break;
  238. case CS6:
  239. new_lcr |= UART_LCR_WLEN6;
  240. break;
  241. case CS7:
  242. new_lcr |= UART_LCR_WLEN7;
  243. break;
  244. default:
  245. case CS8:
  246. new_lcr |= UART_LCR_WLEN8;
  247. break;
  248. }
  249. baud = tty_get_baud_rate(tty);
  250. if (!baud)
  251. baud = 9600;
  252. status = qt2_set_port_config(dev, port_priv->device_port, baud,
  253. new_lcr);
  254. if (status < 0)
  255. dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
  256. __func__, status);
  257. if (cflag & CRTSCTS)
  258. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  259. SERIAL_CRTSCTS,
  260. port_priv->device_port);
  261. else
  262. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  263. 0, port_priv->device_port);
  264. if (status < 0)
  265. dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
  266. __func__, status);
  267. if (I_IXOFF(tty) || I_IXON(tty)) {
  268. u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
  269. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  270. x, port_priv->device_port);
  271. } else
  272. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  273. 0, port_priv->device_port);
  274. if (status < 0)
  275. dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
  276. __func__, status);
  277. }
  278. static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
  279. {
  280. struct usb_serial *serial;
  281. struct qt2_port_private *port_priv;
  282. u8 *data;
  283. u16 device_port;
  284. int status;
  285. unsigned long flags;
  286. device_port = port->port_number;
  287. serial = port->serial;
  288. port_priv = usb_get_serial_port_data(port);
  289. /* set the port to RS232 mode */
  290. status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
  291. QT2_QMCR_RS232, device_port);
  292. if (status < 0) {
  293. dev_err(&port->dev,
  294. "%s failed to set RS232 mode for port %i error %i\n",
  295. __func__, device_port, status);
  296. return status;
  297. }
  298. data = kzalloc(2, GFP_KERNEL);
  299. if (!data)
  300. return -ENOMEM;
  301. /* open the port */
  302. status = usb_control_msg(serial->dev,
  303. usb_rcvctrlpipe(serial->dev, 0),
  304. QT_OPEN_CLOSE_CHANNEL,
  305. 0xc0, 0,
  306. device_port, data, 2, QT2_USB_TIMEOUT);
  307. if (status < 2) {
  308. dev_err(&port->dev, "%s - open port failed %i\n", __func__,
  309. status);
  310. if (status >= 0)
  311. status = -EIO;
  312. kfree(data);
  313. return status;
  314. }
  315. spin_lock_irqsave(&port_priv->lock, flags);
  316. port_priv->shadowLSR = data[0];
  317. port_priv->shadowMSR = data[1];
  318. spin_unlock_irqrestore(&port_priv->lock, flags);
  319. kfree(data);
  320. /* set to default speed and 8bit word size */
  321. status = qt2_set_port_config(serial->dev, device_port,
  322. DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
  323. if (status < 0) {
  324. dev_err(&port->dev, "%s - initial setup failed (%i)\n",
  325. __func__, device_port);
  326. return status;
  327. }
  328. port_priv->device_port = (u8) device_port;
  329. if (tty)
  330. qt2_set_termios(tty, port, &tty->termios);
  331. return 0;
  332. }
  333. static void qt2_close(struct usb_serial_port *port)
  334. {
  335. struct usb_serial *serial;
  336. struct qt2_port_private *port_priv;
  337. int i;
  338. serial = port->serial;
  339. port_priv = usb_get_serial_port_data(port);
  340. usb_kill_urb(port_priv->write_urb);
  341. /* flush the port transmit buffer */
  342. i = usb_control_msg(serial->dev,
  343. usb_rcvctrlpipe(serial->dev, 0),
  344. QT2_FLUSH_DEVICE, 0x40, 1,
  345. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  346. if (i < 0)
  347. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  348. __func__, i);
  349. /* flush the port receive buffer */
  350. i = usb_control_msg(serial->dev,
  351. usb_rcvctrlpipe(serial->dev, 0),
  352. QT2_FLUSH_DEVICE, 0x40, 0,
  353. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  354. if (i < 0)
  355. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  356. __func__, i);
  357. /* close the port */
  358. i = usb_control_msg(serial->dev,
  359. usb_sndctrlpipe(serial->dev, 0),
  360. QT_OPEN_CLOSE_CHANNEL,
  361. 0x40, 0,
  362. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  363. if (i < 0)
  364. dev_err(&port->dev, "%s - close port failed %i\n",
  365. __func__, i);
  366. }
  367. static void qt2_disconnect(struct usb_serial *serial)
  368. {
  369. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  370. usb_kill_urb(serial_priv->read_urb);
  371. }
  372. static int get_serial_info(struct usb_serial_port *port,
  373. struct serial_struct __user *retinfo)
  374. {
  375. struct serial_struct tmp;
  376. memset(&tmp, 0, sizeof(tmp));
  377. tmp.line = port->minor;
  378. tmp.port = 0;
  379. tmp.irq = 0;
  380. tmp.xmit_fifo_size = port->bulk_out_size;
  381. tmp.baud_base = 9600;
  382. tmp.close_delay = 5*HZ;
  383. tmp.closing_wait = 30*HZ;
  384. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  385. return -EFAULT;
  386. return 0;
  387. }
  388. static int qt2_ioctl(struct tty_struct *tty,
  389. unsigned int cmd, unsigned long arg)
  390. {
  391. struct usb_serial_port *port = tty->driver_data;
  392. switch (cmd) {
  393. case TIOCGSERIAL:
  394. return get_serial_info(port,
  395. (struct serial_struct __user *)arg);
  396. default:
  397. break;
  398. }
  399. return -ENOIOCTLCMD;
  400. }
  401. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  402. {
  403. switch (*ch) {
  404. case QT2_LINE_STATUS:
  405. qt2_update_lsr(port, ch + 1);
  406. break;
  407. case QT2_MODEM_STATUS:
  408. qt2_update_msr(port, ch + 1);
  409. break;
  410. }
  411. }
  412. /* not needed, kept to document functionality */
  413. static void qt2_process_xmit_empty(struct usb_serial_port *port,
  414. unsigned char *ch)
  415. {
  416. int bytes_written;
  417. bytes_written = (int)(*ch) + (int)(*(ch + 1) << 4);
  418. }
  419. /* not needed, kept to document functionality */
  420. static void qt2_process_flush(struct usb_serial_port *port, unsigned char *ch)
  421. {
  422. return;
  423. }
  424. static void qt2_process_read_urb(struct urb *urb)
  425. {
  426. struct usb_serial *serial;
  427. struct qt2_serial_private *serial_priv;
  428. struct usb_serial_port *port;
  429. struct qt2_port_private *port_priv;
  430. bool escapeflag;
  431. unsigned char *ch;
  432. int i;
  433. unsigned char newport;
  434. int len = urb->actual_length;
  435. if (!len)
  436. return;
  437. ch = urb->transfer_buffer;
  438. serial = urb->context;
  439. serial_priv = usb_get_serial_data(serial);
  440. port = serial->port[serial_priv->current_port];
  441. port_priv = usb_get_serial_port_data(port);
  442. for (i = 0; i < urb->actual_length; i++) {
  443. ch = (unsigned char *)urb->transfer_buffer + i;
  444. if ((i <= (len - 3)) &&
  445. (*ch == QT2_CONTROL_BYTE) &&
  446. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  447. escapeflag = false;
  448. switch (*(ch + 2)) {
  449. case QT2_LINE_STATUS:
  450. case QT2_MODEM_STATUS:
  451. if (i > (len - 4)) {
  452. dev_warn(&port->dev,
  453. "%s - status message too short\n",
  454. __func__);
  455. break;
  456. }
  457. qt2_process_status(port, ch + 2);
  458. i += 3;
  459. escapeflag = true;
  460. break;
  461. case QT2_XMIT_HOLD:
  462. if (i > (len - 5)) {
  463. dev_warn(&port->dev,
  464. "%s - xmit_empty message too short\n",
  465. __func__);
  466. break;
  467. }
  468. qt2_process_xmit_empty(port, ch + 3);
  469. i += 4;
  470. escapeflag = true;
  471. break;
  472. case QT2_CHANGE_PORT:
  473. if (i > (len - 4)) {
  474. dev_warn(&port->dev,
  475. "%s - change_port message too short\n",
  476. __func__);
  477. break;
  478. }
  479. tty_flip_buffer_push(&port->port);
  480. newport = *(ch + 3);
  481. if (newport > serial->num_ports) {
  482. dev_err(&port->dev,
  483. "%s - port change to invalid port: %i\n",
  484. __func__, newport);
  485. break;
  486. }
  487. serial_priv->current_port = newport;
  488. port = serial->port[serial_priv->current_port];
  489. port_priv = usb_get_serial_port_data(port);
  490. i += 3;
  491. escapeflag = true;
  492. break;
  493. case QT2_REC_FLUSH:
  494. case QT2_XMIT_FLUSH:
  495. qt2_process_flush(port, ch + 2);
  496. i += 2;
  497. escapeflag = true;
  498. break;
  499. case QT2_CONTROL_ESCAPE:
  500. tty_insert_flip_string(&port->port, ch, 2);
  501. i += 2;
  502. escapeflag = true;
  503. break;
  504. default:
  505. dev_warn(&port->dev,
  506. "%s - unsupported command %i\n",
  507. __func__, *(ch + 2));
  508. break;
  509. }
  510. if (escapeflag)
  511. continue;
  512. }
  513. tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
  514. }
  515. tty_flip_buffer_push(&port->port);
  516. }
  517. static void qt2_write_bulk_callback(struct urb *urb)
  518. {
  519. struct usb_serial_port *port;
  520. struct qt2_port_private *port_priv;
  521. port = urb->context;
  522. port_priv = usb_get_serial_port_data(port);
  523. spin_lock(&port_priv->urb_lock);
  524. port_priv->urb_in_use = false;
  525. usb_serial_port_softint(port);
  526. spin_unlock(&port_priv->urb_lock);
  527. }
  528. static void qt2_read_bulk_callback(struct urb *urb)
  529. {
  530. struct usb_serial *serial = urb->context;
  531. int status;
  532. if (urb->status) {
  533. dev_warn(&serial->dev->dev,
  534. "%s - non-zero urb status: %i\n", __func__,
  535. urb->status);
  536. return;
  537. }
  538. qt2_process_read_urb(urb);
  539. status = usb_submit_urb(urb, GFP_ATOMIC);
  540. if (status != 0)
  541. dev_err(&serial->dev->dev,
  542. "%s - resubmit read urb failed: %i\n",
  543. __func__, status);
  544. }
  545. static int qt2_setup_urbs(struct usb_serial *serial)
  546. {
  547. struct usb_serial_port *port0;
  548. struct qt2_serial_private *serial_priv;
  549. int status;
  550. port0 = serial->port[0];
  551. serial_priv = usb_get_serial_data(serial);
  552. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  553. if (!serial_priv->read_urb)
  554. return -ENOMEM;
  555. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  556. usb_rcvbulkpipe(serial->dev,
  557. port0->bulk_in_endpointAddress),
  558. serial_priv->read_buffer,
  559. QT2_READ_BUFFER_SIZE,
  560. qt2_read_bulk_callback, serial);
  561. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  562. if (status != 0) {
  563. dev_err(&serial->dev->dev,
  564. "%s - submit read urb failed %i\n", __func__, status);
  565. usb_free_urb(serial_priv->read_urb);
  566. return status;
  567. }
  568. return 0;
  569. }
  570. static int qt2_attach(struct usb_serial *serial)
  571. {
  572. struct qt2_serial_private *serial_priv;
  573. int status;
  574. /* power on unit */
  575. status = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  576. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  577. QT2_USB_TIMEOUT);
  578. if (status < 0) {
  579. dev_err(&serial->dev->dev,
  580. "%s - failed to power on unit: %i\n", __func__, status);
  581. return status;
  582. }
  583. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  584. if (!serial_priv)
  585. return -ENOMEM;
  586. serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
  587. if (!serial_priv->read_buffer) {
  588. status = -ENOMEM;
  589. goto err_buf;
  590. }
  591. usb_set_serial_data(serial, serial_priv);
  592. status = qt2_setup_urbs(serial);
  593. if (status != 0)
  594. goto attach_failed;
  595. return 0;
  596. attach_failed:
  597. kfree(serial_priv->read_buffer);
  598. err_buf:
  599. kfree(serial_priv);
  600. return status;
  601. }
  602. static int qt2_port_probe(struct usb_serial_port *port)
  603. {
  604. struct usb_serial *serial = port->serial;
  605. struct qt2_port_private *port_priv;
  606. u8 bEndpointAddress;
  607. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  608. if (!port_priv)
  609. return -ENOMEM;
  610. spin_lock_init(&port_priv->lock);
  611. spin_lock_init(&port_priv->urb_lock);
  612. port_priv->port = port;
  613. port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
  614. if (!port_priv->write_buffer)
  615. goto err_buf;
  616. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  617. if (!port_priv->write_urb)
  618. goto err_urb;
  619. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  620. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  621. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  622. port_priv->write_buffer,
  623. QT2_WRITE_BUFFER_SIZE,
  624. qt2_write_bulk_callback, port);
  625. usb_set_serial_port_data(port, port_priv);
  626. return 0;
  627. err_urb:
  628. kfree(port_priv->write_buffer);
  629. err_buf:
  630. kfree(port_priv);
  631. return -ENOMEM;
  632. }
  633. static int qt2_port_remove(struct usb_serial_port *port)
  634. {
  635. struct qt2_port_private *port_priv;
  636. port_priv = usb_get_serial_port_data(port);
  637. usb_free_urb(port_priv->write_urb);
  638. kfree(port_priv->write_buffer);
  639. kfree(port_priv);
  640. return 0;
  641. }
  642. static int qt2_tiocmget(struct tty_struct *tty)
  643. {
  644. struct usb_serial_port *port = tty->driver_data;
  645. struct usb_device *dev = port->serial->dev;
  646. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  647. u8 *d;
  648. int r;
  649. d = kzalloc(2, GFP_KERNEL);
  650. if (!d)
  651. return -ENOMEM;
  652. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  653. if (r < 0)
  654. goto mget_out;
  655. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  656. if (r < 0)
  657. goto mget_out;
  658. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  659. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  660. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  661. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  662. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  663. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  664. mget_out:
  665. kfree(d);
  666. return r;
  667. }
  668. static int qt2_tiocmset(struct tty_struct *tty,
  669. unsigned int set, unsigned int clear)
  670. {
  671. struct qt2_port_private *port_priv;
  672. port_priv = usb_get_serial_port_data(tty->driver_data);
  673. return update_mctrl(port_priv, set, clear);
  674. }
  675. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  676. {
  677. struct usb_serial_port *port = tty->driver_data;
  678. struct qt2_port_private *port_priv;
  679. int status;
  680. u16 val;
  681. port_priv = usb_get_serial_port_data(port);
  682. val = (break_state == -1) ? 1 : 0;
  683. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  684. val, port_priv->device_port);
  685. if (status < 0)
  686. dev_warn(&port->dev,
  687. "%s - failed to send control message: %i\n", __func__,
  688. status);
  689. }
  690. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  691. {
  692. struct usb_device *dev = port->serial->dev;
  693. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  694. /* Disable flow control */
  695. if (!on) {
  696. if (qt2_setregister(dev, port_priv->device_port,
  697. UART_MCR, 0) < 0)
  698. dev_warn(&port->dev, "error from flowcontrol urb\n");
  699. }
  700. /* drop RTS and DTR */
  701. if (on)
  702. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  703. else
  704. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  705. }
  706. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  707. {
  708. struct qt2_port_private *port_priv;
  709. u8 newMSR = (u8) *ch;
  710. unsigned long flags;
  711. port_priv = usb_get_serial_port_data(port);
  712. spin_lock_irqsave(&port_priv->lock, flags);
  713. port_priv->shadowMSR = newMSR;
  714. spin_unlock_irqrestore(&port_priv->lock, flags);
  715. if (newMSR & UART_MSR_ANY_DELTA) {
  716. /* update input line counters */
  717. if (newMSR & UART_MSR_DCTS)
  718. port->icount.cts++;
  719. if (newMSR & UART_MSR_DDSR)
  720. port->icount.dsr++;
  721. if (newMSR & UART_MSR_DDCD)
  722. port->icount.dcd++;
  723. if (newMSR & UART_MSR_TERI)
  724. port->icount.rng++;
  725. wake_up_interruptible(&port->port.delta_msr_wait);
  726. }
  727. }
  728. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  729. {
  730. struct qt2_port_private *port_priv;
  731. struct async_icount *icount;
  732. unsigned long flags;
  733. u8 newLSR = (u8) *ch;
  734. port_priv = usb_get_serial_port_data(port);
  735. if (newLSR & UART_LSR_BI)
  736. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  737. spin_lock_irqsave(&port_priv->lock, flags);
  738. port_priv->shadowLSR = newLSR;
  739. spin_unlock_irqrestore(&port_priv->lock, flags);
  740. icount = &port->icount;
  741. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  742. if (newLSR & UART_LSR_BI)
  743. icount->brk++;
  744. if (newLSR & UART_LSR_OE)
  745. icount->overrun++;
  746. if (newLSR & UART_LSR_PE)
  747. icount->parity++;
  748. if (newLSR & UART_LSR_FE)
  749. icount->frame++;
  750. }
  751. }
  752. static int qt2_write_room(struct tty_struct *tty)
  753. {
  754. struct usb_serial_port *port = tty->driver_data;
  755. struct qt2_port_private *port_priv;
  756. unsigned long flags = 0;
  757. int r;
  758. port_priv = usb_get_serial_port_data(port);
  759. spin_lock_irqsave(&port_priv->urb_lock, flags);
  760. if (port_priv->urb_in_use)
  761. r = 0;
  762. else
  763. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  764. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  765. return r;
  766. }
  767. static int qt2_write(struct tty_struct *tty,
  768. struct usb_serial_port *port,
  769. const unsigned char *buf, int count)
  770. {
  771. struct qt2_port_private *port_priv;
  772. struct urb *write_urb;
  773. unsigned char *data;
  774. unsigned long flags;
  775. int status;
  776. int bytes_out = 0;
  777. port_priv = usb_get_serial_port_data(port);
  778. if (port_priv->write_urb == NULL) {
  779. dev_err(&port->dev, "%s - no output urb\n", __func__);
  780. return 0;
  781. }
  782. write_urb = port_priv->write_urb;
  783. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  784. data = write_urb->transfer_buffer;
  785. spin_lock_irqsave(&port_priv->urb_lock, flags);
  786. if (port_priv->urb_in_use) {
  787. dev_err(&port->dev, "qt2_write - urb is in use\n");
  788. goto write_out;
  789. }
  790. *data++ = QT2_CONTROL_BYTE;
  791. *data++ = QT2_CONTROL_BYTE;
  792. *data++ = port_priv->device_port;
  793. put_unaligned_le16(count, data);
  794. data += 2;
  795. memcpy(data, buf, count);
  796. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  797. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  798. if (status == 0) {
  799. port_priv->urb_in_use = true;
  800. bytes_out += count;
  801. }
  802. write_out:
  803. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  804. return bytes_out;
  805. }
  806. static struct usb_serial_driver qt2_device = {
  807. .driver = {
  808. .owner = THIS_MODULE,
  809. .name = "quatech-serial",
  810. },
  811. .description = DRIVER_DESC,
  812. .id_table = id_table,
  813. .open = qt2_open,
  814. .close = qt2_close,
  815. .write = qt2_write,
  816. .write_room = qt2_write_room,
  817. .calc_num_ports = qt2_calc_num_ports,
  818. .attach = qt2_attach,
  819. .release = qt2_release,
  820. .disconnect = qt2_disconnect,
  821. .port_probe = qt2_port_probe,
  822. .port_remove = qt2_port_remove,
  823. .dtr_rts = qt2_dtr_rts,
  824. .break_ctl = qt2_break_ctl,
  825. .tiocmget = qt2_tiocmget,
  826. .tiocmset = qt2_tiocmset,
  827. .tiocmiwait = usb_serial_generic_tiocmiwait,
  828. .get_icount = usb_serial_generic_get_icount,
  829. .ioctl = qt2_ioctl,
  830. .set_termios = qt2_set_termios,
  831. };
  832. static struct usb_serial_driver *const serial_drivers[] = {
  833. &qt2_device, NULL
  834. };
  835. module_usb_serial_driver(serial_drivers, id_table);
  836. MODULE_DESCRIPTION(DRIVER_DESC);
  837. MODULE_LICENSE("GPL v2");