quatech2.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 < (int)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. unsigned long flags;
  522. port = urb->context;
  523. port_priv = usb_get_serial_port_data(port);
  524. spin_lock_irqsave(&port_priv->urb_lock, flags);
  525. port_priv->urb_in_use = false;
  526. usb_serial_port_softint(port);
  527. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  528. }
  529. static void qt2_read_bulk_callback(struct urb *urb)
  530. {
  531. struct usb_serial *serial = urb->context;
  532. int status;
  533. if (urb->status) {
  534. dev_warn(&serial->dev->dev,
  535. "%s - non-zero urb status: %i\n", __func__,
  536. urb->status);
  537. return;
  538. }
  539. qt2_process_read_urb(urb);
  540. status = usb_submit_urb(urb, GFP_ATOMIC);
  541. if (status != 0)
  542. dev_err(&serial->dev->dev,
  543. "%s - resubmit read urb failed: %i\n",
  544. __func__, status);
  545. }
  546. static int qt2_setup_urbs(struct usb_serial *serial)
  547. {
  548. struct usb_serial_port *port0;
  549. struct qt2_serial_private *serial_priv;
  550. int status;
  551. port0 = serial->port[0];
  552. serial_priv = usb_get_serial_data(serial);
  553. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  554. if (!serial_priv->read_urb)
  555. return -ENOMEM;
  556. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  557. usb_rcvbulkpipe(serial->dev,
  558. port0->bulk_in_endpointAddress),
  559. serial_priv->read_buffer,
  560. QT2_READ_BUFFER_SIZE,
  561. qt2_read_bulk_callback, serial);
  562. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  563. if (status != 0) {
  564. dev_err(&serial->dev->dev,
  565. "%s - submit read urb failed %i\n", __func__, status);
  566. usb_free_urb(serial_priv->read_urb);
  567. return status;
  568. }
  569. return 0;
  570. }
  571. static int qt2_attach(struct usb_serial *serial)
  572. {
  573. struct qt2_serial_private *serial_priv;
  574. int status;
  575. /* power on unit */
  576. status = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  577. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  578. QT2_USB_TIMEOUT);
  579. if (status < 0) {
  580. dev_err(&serial->dev->dev,
  581. "%s - failed to power on unit: %i\n", __func__, status);
  582. return status;
  583. }
  584. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  585. if (!serial_priv)
  586. return -ENOMEM;
  587. serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
  588. if (!serial_priv->read_buffer) {
  589. status = -ENOMEM;
  590. goto err_buf;
  591. }
  592. usb_set_serial_data(serial, serial_priv);
  593. status = qt2_setup_urbs(serial);
  594. if (status != 0)
  595. goto attach_failed;
  596. return 0;
  597. attach_failed:
  598. kfree(serial_priv->read_buffer);
  599. err_buf:
  600. kfree(serial_priv);
  601. return status;
  602. }
  603. static int qt2_port_probe(struct usb_serial_port *port)
  604. {
  605. struct usb_serial *serial = port->serial;
  606. struct qt2_port_private *port_priv;
  607. u8 bEndpointAddress;
  608. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  609. if (!port_priv)
  610. return -ENOMEM;
  611. spin_lock_init(&port_priv->lock);
  612. spin_lock_init(&port_priv->urb_lock);
  613. port_priv->port = port;
  614. port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
  615. if (!port_priv->write_buffer)
  616. goto err_buf;
  617. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  618. if (!port_priv->write_urb)
  619. goto err_urb;
  620. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  621. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  622. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  623. port_priv->write_buffer,
  624. QT2_WRITE_BUFFER_SIZE,
  625. qt2_write_bulk_callback, port);
  626. usb_set_serial_port_data(port, port_priv);
  627. return 0;
  628. err_urb:
  629. kfree(port_priv->write_buffer);
  630. err_buf:
  631. kfree(port_priv);
  632. return -ENOMEM;
  633. }
  634. static int qt2_port_remove(struct usb_serial_port *port)
  635. {
  636. struct qt2_port_private *port_priv;
  637. port_priv = usb_get_serial_port_data(port);
  638. usb_free_urb(port_priv->write_urb);
  639. kfree(port_priv->write_buffer);
  640. kfree(port_priv);
  641. return 0;
  642. }
  643. static int qt2_tiocmget(struct tty_struct *tty)
  644. {
  645. struct usb_serial_port *port = tty->driver_data;
  646. struct usb_device *dev = port->serial->dev;
  647. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  648. u8 *d;
  649. int r;
  650. d = kzalloc(2, GFP_KERNEL);
  651. if (!d)
  652. return -ENOMEM;
  653. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  654. if (r < 0)
  655. goto mget_out;
  656. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  657. if (r < 0)
  658. goto mget_out;
  659. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  660. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  661. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  662. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  663. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  664. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  665. mget_out:
  666. kfree(d);
  667. return r;
  668. }
  669. static int qt2_tiocmset(struct tty_struct *tty,
  670. unsigned int set, unsigned int clear)
  671. {
  672. struct qt2_port_private *port_priv;
  673. port_priv = usb_get_serial_port_data(tty->driver_data);
  674. return update_mctrl(port_priv, set, clear);
  675. }
  676. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  677. {
  678. struct usb_serial_port *port = tty->driver_data;
  679. struct qt2_port_private *port_priv;
  680. int status;
  681. u16 val;
  682. port_priv = usb_get_serial_port_data(port);
  683. val = (break_state == -1) ? 1 : 0;
  684. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  685. val, port_priv->device_port);
  686. if (status < 0)
  687. dev_warn(&port->dev,
  688. "%s - failed to send control message: %i\n", __func__,
  689. status);
  690. }
  691. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  692. {
  693. struct usb_device *dev = port->serial->dev;
  694. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  695. /* Disable flow control */
  696. if (!on) {
  697. if (qt2_setregister(dev, port_priv->device_port,
  698. UART_MCR, 0) < 0)
  699. dev_warn(&port->dev, "error from flowcontrol urb\n");
  700. }
  701. /* drop RTS and DTR */
  702. if (on)
  703. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  704. else
  705. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  706. }
  707. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  708. {
  709. struct qt2_port_private *port_priv;
  710. u8 newMSR = (u8) *ch;
  711. unsigned long flags;
  712. /* May be called from qt2_process_read_urb() for an unbound port. */
  713. port_priv = usb_get_serial_port_data(port);
  714. if (!port_priv)
  715. return;
  716. spin_lock_irqsave(&port_priv->lock, flags);
  717. port_priv->shadowMSR = newMSR;
  718. spin_unlock_irqrestore(&port_priv->lock, flags);
  719. if (newMSR & UART_MSR_ANY_DELTA) {
  720. /* update input line counters */
  721. if (newMSR & UART_MSR_DCTS)
  722. port->icount.cts++;
  723. if (newMSR & UART_MSR_DDSR)
  724. port->icount.dsr++;
  725. if (newMSR & UART_MSR_DDCD)
  726. port->icount.dcd++;
  727. if (newMSR & UART_MSR_TERI)
  728. port->icount.rng++;
  729. wake_up_interruptible(&port->port.delta_msr_wait);
  730. }
  731. }
  732. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  733. {
  734. struct qt2_port_private *port_priv;
  735. struct async_icount *icount;
  736. unsigned long flags;
  737. u8 newLSR = (u8) *ch;
  738. /* May be called from qt2_process_read_urb() for an unbound port. */
  739. port_priv = usb_get_serial_port_data(port);
  740. if (!port_priv)
  741. return;
  742. if (newLSR & UART_LSR_BI)
  743. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  744. spin_lock_irqsave(&port_priv->lock, flags);
  745. port_priv->shadowLSR = newLSR;
  746. spin_unlock_irqrestore(&port_priv->lock, flags);
  747. icount = &port->icount;
  748. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  749. if (newLSR & UART_LSR_BI)
  750. icount->brk++;
  751. if (newLSR & UART_LSR_OE)
  752. icount->overrun++;
  753. if (newLSR & UART_LSR_PE)
  754. icount->parity++;
  755. if (newLSR & UART_LSR_FE)
  756. icount->frame++;
  757. }
  758. }
  759. static int qt2_write_room(struct tty_struct *tty)
  760. {
  761. struct usb_serial_port *port = tty->driver_data;
  762. struct qt2_port_private *port_priv;
  763. unsigned long flags = 0;
  764. int r;
  765. port_priv = usb_get_serial_port_data(port);
  766. spin_lock_irqsave(&port_priv->urb_lock, flags);
  767. if (port_priv->urb_in_use)
  768. r = 0;
  769. else
  770. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  771. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  772. return r;
  773. }
  774. static int qt2_write(struct tty_struct *tty,
  775. struct usb_serial_port *port,
  776. const unsigned char *buf, int count)
  777. {
  778. struct qt2_port_private *port_priv;
  779. struct urb *write_urb;
  780. unsigned char *data;
  781. unsigned long flags;
  782. int status;
  783. int bytes_out = 0;
  784. port_priv = usb_get_serial_port_data(port);
  785. if (port_priv->write_urb == NULL) {
  786. dev_err(&port->dev, "%s - no output urb\n", __func__);
  787. return 0;
  788. }
  789. write_urb = port_priv->write_urb;
  790. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  791. data = write_urb->transfer_buffer;
  792. spin_lock_irqsave(&port_priv->urb_lock, flags);
  793. if (port_priv->urb_in_use) {
  794. dev_err(&port->dev, "qt2_write - urb is in use\n");
  795. goto write_out;
  796. }
  797. *data++ = QT2_CONTROL_BYTE;
  798. *data++ = QT2_CONTROL_BYTE;
  799. *data++ = port_priv->device_port;
  800. put_unaligned_le16(count, data);
  801. data += 2;
  802. memcpy(data, buf, count);
  803. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  804. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  805. if (status == 0) {
  806. port_priv->urb_in_use = true;
  807. bytes_out += count;
  808. }
  809. write_out:
  810. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  811. return bytes_out;
  812. }
  813. static struct usb_serial_driver qt2_device = {
  814. .driver = {
  815. .owner = THIS_MODULE,
  816. .name = "quatech-serial",
  817. },
  818. .description = DRIVER_DESC,
  819. .id_table = id_table,
  820. .open = qt2_open,
  821. .close = qt2_close,
  822. .write = qt2_write,
  823. .write_room = qt2_write_room,
  824. .calc_num_ports = qt2_calc_num_ports,
  825. .attach = qt2_attach,
  826. .release = qt2_release,
  827. .disconnect = qt2_disconnect,
  828. .port_probe = qt2_port_probe,
  829. .port_remove = qt2_port_remove,
  830. .dtr_rts = qt2_dtr_rts,
  831. .break_ctl = qt2_break_ctl,
  832. .tiocmget = qt2_tiocmget,
  833. .tiocmset = qt2_tiocmset,
  834. .tiocmiwait = usb_serial_generic_tiocmiwait,
  835. .get_icount = usb_serial_generic_get_icount,
  836. .ioctl = qt2_ioctl,
  837. .set_termios = qt2_set_termios,
  838. };
  839. static struct usb_serial_driver *const serial_drivers[] = {
  840. &qt2_device, NULL
  841. };
  842. module_usb_serial_driver(serial_drivers, id_table);
  843. MODULE_DESCRIPTION(DRIVER_DESC);
  844. MODULE_LICENSE("GPL v2");