quatech2.c 25 KB

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