quatech2.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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_free_urb(serial_priv->read_urb);
  121. kfree(serial_priv->read_buffer);
  122. kfree(serial_priv);
  123. }
  124. static inline int calc_baud_divisor(int baudrate)
  125. {
  126. int divisor, rem;
  127. divisor = MAX_BAUD_RATE / baudrate;
  128. rem = MAX_BAUD_RATE % baudrate;
  129. /* Round to nearest divisor */
  130. if (((rem * 2) >= baudrate) && (baudrate != 110))
  131. divisor++;
  132. return divisor;
  133. }
  134. static inline int qt2_set_port_config(struct usb_device *dev,
  135. unsigned char port_number,
  136. u16 baudrate, u16 lcr)
  137. {
  138. int divisor = calc_baud_divisor(baudrate);
  139. u16 index = ((u16) (lcr << 8) | (u16) (port_number));
  140. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  141. QT2_GET_SET_UART, 0x40,
  142. divisor, index, NULL, 0, QT2_USB_TIMEOUT);
  143. }
  144. static inline int qt2_control_msg(struct usb_device *dev,
  145. u8 request, u16 data, u16 index)
  146. {
  147. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  148. request, 0x40, data, index,
  149. NULL, 0, QT2_USB_TIMEOUT);
  150. }
  151. static inline int qt2_setdevice(struct usb_device *dev, u8 *data)
  152. {
  153. u16 x = ((u16) (data[1] << 8) | (u16) (data[0]));
  154. return qt2_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
  155. }
  156. static inline int qt2_getdevice(struct usb_device *dev, u8 *data)
  157. {
  158. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  159. QT_SET_GET_DEVICE, 0xc0, 0, 0,
  160. data, 3, QT2_USB_TIMEOUT);
  161. }
  162. static inline int qt2_getregister(struct usb_device *dev,
  163. u8 uart,
  164. u8 reg,
  165. u8 *data)
  166. {
  167. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  168. QT_SET_GET_REGISTER, 0xc0, reg,
  169. uart, data, sizeof(*data), QT2_USB_TIMEOUT);
  170. }
  171. static inline int qt2_setregister(struct usb_device *dev,
  172. u8 uart, u8 reg, u16 data)
  173. {
  174. u16 value = (data << 8) | reg;
  175. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  176. QT_SET_GET_REGISTER, 0x40, value, uart,
  177. NULL, 0, QT2_USB_TIMEOUT);
  178. }
  179. static inline int update_mctrl(struct qt2_port_private *port_priv,
  180. unsigned int set, unsigned int clear)
  181. {
  182. struct usb_serial_port *port = port_priv->port;
  183. struct usb_device *dev = port->serial->dev;
  184. unsigned urb_value;
  185. int status;
  186. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
  187. dev_dbg(&port->dev,
  188. "update_mctrl - DTR|RTS not being set|cleared\n");
  189. return 0; /* no change */
  190. }
  191. clear &= ~set; /* 'set' takes precedence over 'clear' */
  192. urb_value = 0;
  193. if (set & TIOCM_DTR)
  194. urb_value |= UART_MCR_DTR;
  195. if (set & TIOCM_RTS)
  196. urb_value |= UART_MCR_RTS;
  197. status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
  198. urb_value);
  199. if (status < 0)
  200. dev_err(&port->dev,
  201. "update_mctrl - Error from MODEM_CTRL urb: %i\n",
  202. status);
  203. return status;
  204. }
  205. static int qt2_calc_num_ports(struct usb_serial *serial)
  206. {
  207. struct qt2_device_detail d;
  208. int i;
  209. for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
  210. if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
  211. return d.num_ports;
  212. }
  213. /* we didn't recognize the device */
  214. dev_err(&serial->dev->dev,
  215. "don't know the number of ports, assuming 1\n");
  216. return 1;
  217. }
  218. static void qt2_set_termios(struct tty_struct *tty,
  219. struct usb_serial_port *port,
  220. struct ktermios *old_termios)
  221. {
  222. struct usb_device *dev = port->serial->dev;
  223. struct qt2_port_private *port_priv;
  224. struct ktermios *termios = &tty->termios;
  225. u16 baud;
  226. unsigned int cflag = termios->c_cflag;
  227. u16 new_lcr = 0;
  228. int status;
  229. port_priv = usb_get_serial_port_data(port);
  230. if (cflag & PARENB) {
  231. if (cflag & PARODD)
  232. new_lcr |= UART_LCR_PARITY;
  233. else
  234. new_lcr |= SERIAL_EVEN_PARITY;
  235. }
  236. switch (cflag & CSIZE) {
  237. case CS5:
  238. new_lcr |= UART_LCR_WLEN5;
  239. break;
  240. case CS6:
  241. new_lcr |= UART_LCR_WLEN6;
  242. break;
  243. case CS7:
  244. new_lcr |= UART_LCR_WLEN7;
  245. break;
  246. default:
  247. case CS8:
  248. new_lcr |= UART_LCR_WLEN8;
  249. break;
  250. }
  251. baud = tty_get_baud_rate(tty);
  252. if (!baud)
  253. baud = 9600;
  254. status = qt2_set_port_config(dev, port_priv->device_port, baud,
  255. new_lcr);
  256. if (status < 0)
  257. dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
  258. __func__, status);
  259. if (cflag & CRTSCTS)
  260. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  261. SERIAL_CRTSCTS,
  262. port_priv->device_port);
  263. else
  264. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  265. 0, port_priv->device_port);
  266. if (status < 0)
  267. dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
  268. __func__, status);
  269. if (I_IXOFF(tty) || I_IXON(tty)) {
  270. u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
  271. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  272. x, port_priv->device_port);
  273. } else
  274. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  275. 0, port_priv->device_port);
  276. if (status < 0)
  277. dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
  278. __func__, status);
  279. }
  280. static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
  281. {
  282. struct usb_serial *serial;
  283. struct qt2_port_private *port_priv;
  284. u8 *data;
  285. u16 device_port;
  286. int status;
  287. unsigned long flags;
  288. device_port = port->port_number;
  289. serial = port->serial;
  290. port_priv = usb_get_serial_port_data(port);
  291. /* set the port to RS232 mode */
  292. status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
  293. QT2_QMCR_RS232, device_port);
  294. if (status < 0) {
  295. dev_err(&port->dev,
  296. "%s failed to set RS232 mode for port %i error %i\n",
  297. __func__, device_port, status);
  298. return status;
  299. }
  300. data = kzalloc(2, GFP_KERNEL);
  301. if (!data)
  302. return -ENOMEM;
  303. /* open the port */
  304. status = usb_control_msg(serial->dev,
  305. usb_rcvctrlpipe(serial->dev, 0),
  306. QT_OPEN_CLOSE_CHANNEL,
  307. 0xc0, 0,
  308. device_port, data, 2, QT2_USB_TIMEOUT);
  309. if (status < 0) {
  310. dev_err(&port->dev, "%s - open port failed %i\n", __func__,
  311. status);
  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. unsigned long flags;
  338. int i;
  339. serial = port->serial;
  340. port_priv = usb_get_serial_port_data(port);
  341. spin_lock_irqsave(&port_priv->urb_lock, flags);
  342. usb_kill_urb(port_priv->write_urb);
  343. port_priv->urb_in_use = false;
  344. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  345. /* flush the port transmit buffer */
  346. i = usb_control_msg(serial->dev,
  347. usb_rcvctrlpipe(serial->dev, 0),
  348. QT2_FLUSH_DEVICE, 0x40, 1,
  349. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  350. if (i < 0)
  351. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  352. __func__, i);
  353. /* flush the port receive buffer */
  354. i = usb_control_msg(serial->dev,
  355. usb_rcvctrlpipe(serial->dev, 0),
  356. QT2_FLUSH_DEVICE, 0x40, 0,
  357. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  358. if (i < 0)
  359. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  360. __func__, i);
  361. /* close the port */
  362. i = usb_control_msg(serial->dev,
  363. usb_sndctrlpipe(serial->dev, 0),
  364. QT_OPEN_CLOSE_CHANNEL,
  365. 0x40, 0,
  366. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  367. if (i < 0)
  368. dev_err(&port->dev, "%s - close port failed %i\n",
  369. __func__, i);
  370. }
  371. static void qt2_disconnect(struct usb_serial *serial)
  372. {
  373. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  374. usb_kill_urb(serial_priv->read_urb);
  375. }
  376. static int get_serial_info(struct usb_serial_port *port,
  377. struct serial_struct __user *retinfo)
  378. {
  379. struct serial_struct tmp;
  380. if (!retinfo)
  381. return -EFAULT;
  382. memset(&tmp, 0, sizeof(tmp));
  383. tmp.line = port->minor;
  384. tmp.port = 0;
  385. tmp.irq = 0;
  386. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  387. tmp.xmit_fifo_size = port->bulk_out_size;
  388. tmp.baud_base = 9600;
  389. tmp.close_delay = 5*HZ;
  390. tmp.closing_wait = 30*HZ;
  391. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  392. return -EFAULT;
  393. return 0;
  394. }
  395. static int qt2_ioctl(struct tty_struct *tty,
  396. unsigned int cmd, unsigned long arg)
  397. {
  398. struct usb_serial_port *port = tty->driver_data;
  399. switch (cmd) {
  400. case TIOCGSERIAL:
  401. return get_serial_info(port,
  402. (struct serial_struct __user *)arg);
  403. default:
  404. break;
  405. }
  406. return -ENOIOCTLCMD;
  407. }
  408. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  409. {
  410. switch (*ch) {
  411. case QT2_LINE_STATUS:
  412. qt2_update_lsr(port, ch + 1);
  413. break;
  414. case QT2_MODEM_STATUS:
  415. qt2_update_msr(port, ch + 1);
  416. break;
  417. }
  418. }
  419. /* not needed, kept to document functionality */
  420. static void qt2_process_xmit_empty(struct usb_serial_port *port,
  421. unsigned char *ch)
  422. {
  423. int bytes_written;
  424. bytes_written = (int)(*ch) + (int)(*(ch + 1) << 4);
  425. }
  426. /* not needed, kept to document functionality */
  427. static void qt2_process_flush(struct usb_serial_port *port, unsigned char *ch)
  428. {
  429. return;
  430. }
  431. static void qt2_process_read_urb(struct urb *urb)
  432. {
  433. struct usb_serial *serial;
  434. struct qt2_serial_private *serial_priv;
  435. struct usb_serial_port *port;
  436. struct qt2_port_private *port_priv;
  437. bool escapeflag;
  438. unsigned char *ch;
  439. int i;
  440. unsigned char newport;
  441. int len = urb->actual_length;
  442. if (!len)
  443. return;
  444. ch = urb->transfer_buffer;
  445. serial = urb->context;
  446. serial_priv = usb_get_serial_data(serial);
  447. port = serial->port[serial_priv->current_port];
  448. port_priv = usb_get_serial_port_data(port);
  449. for (i = 0; i < urb->actual_length; i++) {
  450. ch = (unsigned char *)urb->transfer_buffer + i;
  451. if ((i <= (len - 3)) &&
  452. (*ch == QT2_CONTROL_BYTE) &&
  453. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  454. escapeflag = false;
  455. switch (*(ch + 2)) {
  456. case QT2_LINE_STATUS:
  457. case QT2_MODEM_STATUS:
  458. if (i > (len - 4)) {
  459. dev_warn(&port->dev,
  460. "%s - status message too short\n",
  461. __func__);
  462. break;
  463. }
  464. qt2_process_status(port, ch + 2);
  465. i += 3;
  466. escapeflag = true;
  467. break;
  468. case QT2_XMIT_HOLD:
  469. if (i > (len - 5)) {
  470. dev_warn(&port->dev,
  471. "%s - xmit_empty message too short\n",
  472. __func__);
  473. break;
  474. }
  475. qt2_process_xmit_empty(port, ch + 3);
  476. i += 4;
  477. escapeflag = true;
  478. break;
  479. case QT2_CHANGE_PORT:
  480. if (i > (len - 4)) {
  481. dev_warn(&port->dev,
  482. "%s - change_port message too short\n",
  483. __func__);
  484. break;
  485. }
  486. tty_flip_buffer_push(&port->port);
  487. newport = *(ch + 3);
  488. if (newport > serial->num_ports) {
  489. dev_err(&port->dev,
  490. "%s - port change to invalid port: %i\n",
  491. __func__, newport);
  492. break;
  493. }
  494. serial_priv->current_port = newport;
  495. port = serial->port[serial_priv->current_port];
  496. port_priv = usb_get_serial_port_data(port);
  497. i += 3;
  498. escapeflag = true;
  499. break;
  500. case QT2_REC_FLUSH:
  501. case QT2_XMIT_FLUSH:
  502. qt2_process_flush(port, ch + 2);
  503. i += 2;
  504. escapeflag = true;
  505. break;
  506. case QT2_CONTROL_ESCAPE:
  507. tty_buffer_request_room(&port->port, 2);
  508. tty_insert_flip_string(&port->port, ch, 2);
  509. i += 2;
  510. escapeflag = true;
  511. break;
  512. default:
  513. dev_warn(&port->dev,
  514. "%s - unsupported command %i\n",
  515. __func__, *(ch + 2));
  516. break;
  517. }
  518. if (escapeflag)
  519. continue;
  520. }
  521. tty_buffer_request_room(&port->port, 1);
  522. tty_insert_flip_string(&port->port, ch, 1);
  523. }
  524. tty_flip_buffer_push(&port->port);
  525. }
  526. static void qt2_write_bulk_callback(struct urb *urb)
  527. {
  528. struct usb_serial_port *port;
  529. struct qt2_port_private *port_priv;
  530. port = urb->context;
  531. port_priv = usb_get_serial_port_data(port);
  532. spin_lock(&port_priv->urb_lock);
  533. port_priv->urb_in_use = false;
  534. usb_serial_port_softint(port);
  535. spin_unlock(&port_priv->urb_lock);
  536. }
  537. static void qt2_read_bulk_callback(struct urb *urb)
  538. {
  539. struct usb_serial *serial = urb->context;
  540. int status;
  541. if (urb->status) {
  542. dev_warn(&serial->dev->dev,
  543. "%s - non-zero urb status: %i\n", __func__,
  544. urb->status);
  545. return;
  546. }
  547. qt2_process_read_urb(urb);
  548. status = usb_submit_urb(urb, GFP_ATOMIC);
  549. if (status != 0)
  550. dev_err(&serial->dev->dev,
  551. "%s - resubmit read urb failed: %i\n",
  552. __func__, status);
  553. }
  554. static int qt2_setup_urbs(struct usb_serial *serial)
  555. {
  556. struct usb_serial_port *port0;
  557. struct qt2_serial_private *serial_priv;
  558. int status;
  559. port0 = serial->port[0];
  560. serial_priv = usb_get_serial_data(serial);
  561. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  562. if (!serial_priv->read_urb)
  563. return -ENOMEM;
  564. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  565. usb_rcvbulkpipe(serial->dev,
  566. port0->bulk_in_endpointAddress),
  567. serial_priv->read_buffer,
  568. QT2_READ_BUFFER_SIZE,
  569. qt2_read_bulk_callback, serial);
  570. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  571. if (status != 0) {
  572. dev_err(&serial->dev->dev,
  573. "%s - submit read urb failed %i\n", __func__, status);
  574. usb_free_urb(serial_priv->read_urb);
  575. return status;
  576. }
  577. return 0;
  578. }
  579. static int qt2_attach(struct usb_serial *serial)
  580. {
  581. struct qt2_serial_private *serial_priv;
  582. int status;
  583. /* power on unit */
  584. status = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  585. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  586. QT2_USB_TIMEOUT);
  587. if (status < 0) {
  588. dev_err(&serial->dev->dev,
  589. "%s - failed to power on unit: %i\n", __func__, status);
  590. return status;
  591. }
  592. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  593. if (!serial_priv)
  594. return -ENOMEM;
  595. serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
  596. if (!serial_priv->read_buffer) {
  597. status = -ENOMEM;
  598. goto err_buf;
  599. }
  600. usb_set_serial_data(serial, serial_priv);
  601. status = qt2_setup_urbs(serial);
  602. if (status != 0)
  603. goto attach_failed;
  604. return 0;
  605. attach_failed:
  606. kfree(serial_priv->read_buffer);
  607. err_buf:
  608. kfree(serial_priv);
  609. return status;
  610. }
  611. static int qt2_port_probe(struct usb_serial_port *port)
  612. {
  613. struct usb_serial *serial = port->serial;
  614. struct qt2_port_private *port_priv;
  615. u8 bEndpointAddress;
  616. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  617. if (!port_priv)
  618. return -ENOMEM;
  619. spin_lock_init(&port_priv->lock);
  620. spin_lock_init(&port_priv->urb_lock);
  621. port_priv->port = port;
  622. port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
  623. if (!port_priv->write_buffer)
  624. goto err_buf;
  625. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  626. if (!port_priv->write_urb)
  627. goto err_urb;
  628. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  629. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  630. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  631. port_priv->write_buffer,
  632. QT2_WRITE_BUFFER_SIZE,
  633. qt2_write_bulk_callback, port);
  634. usb_set_serial_port_data(port, port_priv);
  635. return 0;
  636. err_urb:
  637. kfree(port_priv->write_buffer);
  638. err_buf:
  639. kfree(port_priv);
  640. return -ENOMEM;
  641. }
  642. static int qt2_port_remove(struct usb_serial_port *port)
  643. {
  644. struct qt2_port_private *port_priv;
  645. port_priv = usb_get_serial_port_data(port);
  646. usb_free_urb(port_priv->write_urb);
  647. kfree(port_priv->write_buffer);
  648. kfree(port_priv);
  649. return 0;
  650. }
  651. static int qt2_tiocmget(struct tty_struct *tty)
  652. {
  653. struct usb_serial_port *port = tty->driver_data;
  654. struct usb_device *dev = port->serial->dev;
  655. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  656. u8 *d;
  657. int r;
  658. d = kzalloc(2, GFP_KERNEL);
  659. if (!d)
  660. return -ENOMEM;
  661. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  662. if (r < 0)
  663. goto mget_out;
  664. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  665. if (r < 0)
  666. goto mget_out;
  667. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  668. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  669. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  670. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  671. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  672. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  673. mget_out:
  674. kfree(d);
  675. return r;
  676. }
  677. static int qt2_tiocmset(struct tty_struct *tty,
  678. unsigned int set, unsigned int clear)
  679. {
  680. struct qt2_port_private *port_priv;
  681. port_priv = usb_get_serial_port_data(tty->driver_data);
  682. return update_mctrl(port_priv, set, clear);
  683. }
  684. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  685. {
  686. struct usb_serial_port *port = tty->driver_data;
  687. struct qt2_port_private *port_priv;
  688. int status;
  689. u16 val;
  690. port_priv = usb_get_serial_port_data(port);
  691. val = (break_state == -1) ? 1 : 0;
  692. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  693. val, port_priv->device_port);
  694. if (status < 0)
  695. dev_warn(&port->dev,
  696. "%s - failed to send control message: %i\n", __func__,
  697. status);
  698. }
  699. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  700. {
  701. struct usb_device *dev = port->serial->dev;
  702. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  703. /* Disable flow control */
  704. if (!on) {
  705. if (qt2_setregister(dev, port_priv->device_port,
  706. UART_MCR, 0) < 0)
  707. dev_warn(&port->dev, "error from flowcontrol urb\n");
  708. }
  709. /* drop RTS and DTR */
  710. if (on)
  711. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  712. else
  713. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  714. }
  715. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  716. {
  717. struct qt2_port_private *port_priv;
  718. u8 newMSR = (u8) *ch;
  719. unsigned long flags;
  720. port_priv = usb_get_serial_port_data(port);
  721. spin_lock_irqsave(&port_priv->lock, flags);
  722. port_priv->shadowMSR = newMSR;
  723. spin_unlock_irqrestore(&port_priv->lock, flags);
  724. if (newMSR & UART_MSR_ANY_DELTA) {
  725. /* update input line counters */
  726. if (newMSR & UART_MSR_DCTS)
  727. port->icount.cts++;
  728. if (newMSR & UART_MSR_DDSR)
  729. port->icount.dsr++;
  730. if (newMSR & UART_MSR_DDCD)
  731. port->icount.dcd++;
  732. if (newMSR & UART_MSR_TERI)
  733. port->icount.rng++;
  734. wake_up_interruptible(&port->port.delta_msr_wait);
  735. }
  736. }
  737. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  738. {
  739. struct qt2_port_private *port_priv;
  740. struct async_icount *icount;
  741. unsigned long flags;
  742. u8 newLSR = (u8) *ch;
  743. port_priv = usb_get_serial_port_data(port);
  744. if (newLSR & UART_LSR_BI)
  745. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  746. spin_lock_irqsave(&port_priv->lock, flags);
  747. port_priv->shadowLSR = newLSR;
  748. spin_unlock_irqrestore(&port_priv->lock, flags);
  749. icount = &port->icount;
  750. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  751. if (newLSR & UART_LSR_BI)
  752. icount->brk++;
  753. if (newLSR & UART_LSR_OE)
  754. icount->overrun++;
  755. if (newLSR & UART_LSR_PE)
  756. icount->parity++;
  757. if (newLSR & UART_LSR_FE)
  758. icount->frame++;
  759. }
  760. }
  761. static int qt2_write_room(struct tty_struct *tty)
  762. {
  763. struct usb_serial_port *port = tty->driver_data;
  764. struct qt2_port_private *port_priv;
  765. unsigned long flags = 0;
  766. int r;
  767. port_priv = usb_get_serial_port_data(port);
  768. spin_lock_irqsave(&port_priv->urb_lock, flags);
  769. if (port_priv->urb_in_use)
  770. r = 0;
  771. else
  772. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  773. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  774. return r;
  775. }
  776. static int qt2_write(struct tty_struct *tty,
  777. struct usb_serial_port *port,
  778. const unsigned char *buf, int count)
  779. {
  780. struct qt2_port_private *port_priv;
  781. struct urb *write_urb;
  782. unsigned char *data;
  783. unsigned long flags;
  784. int status;
  785. int bytes_out = 0;
  786. port_priv = usb_get_serial_port_data(port);
  787. if (port_priv->write_urb == NULL) {
  788. dev_err(&port->dev, "%s - no output urb\n", __func__);
  789. return 0;
  790. }
  791. write_urb = port_priv->write_urb;
  792. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  793. data = write_urb->transfer_buffer;
  794. spin_lock_irqsave(&port_priv->urb_lock, flags);
  795. if (port_priv->urb_in_use == true) {
  796. dev_err(&port->dev, "qt2_write - urb is in use\n");
  797. goto write_out;
  798. }
  799. *data++ = QT2_CONTROL_BYTE;
  800. *data++ = QT2_CONTROL_BYTE;
  801. *data++ = port_priv->device_port;
  802. put_unaligned_le16(count, data);
  803. data += 2;
  804. memcpy(data, buf, count);
  805. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  806. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  807. if (status == 0) {
  808. port_priv->urb_in_use = true;
  809. bytes_out += count;
  810. }
  811. write_out:
  812. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  813. return bytes_out;
  814. }
  815. static struct usb_serial_driver qt2_device = {
  816. .driver = {
  817. .owner = THIS_MODULE,
  818. .name = "quatech-serial",
  819. },
  820. .description = DRIVER_DESC,
  821. .id_table = id_table,
  822. .open = qt2_open,
  823. .close = qt2_close,
  824. .write = qt2_write,
  825. .write_room = qt2_write_room,
  826. .calc_num_ports = qt2_calc_num_ports,
  827. .attach = qt2_attach,
  828. .release = qt2_release,
  829. .disconnect = qt2_disconnect,
  830. .port_probe = qt2_port_probe,
  831. .port_remove = qt2_port_remove,
  832. .dtr_rts = qt2_dtr_rts,
  833. .break_ctl = qt2_break_ctl,
  834. .tiocmget = qt2_tiocmget,
  835. .tiocmset = qt2_tiocmset,
  836. .tiocmiwait = usb_serial_generic_tiocmiwait,
  837. .get_icount = usb_serial_generic_get_icount,
  838. .ioctl = qt2_ioctl,
  839. .set_termios = qt2_set_termios,
  840. };
  841. static struct usb_serial_driver *const serial_drivers[] = {
  842. &qt2_device, NULL
  843. };
  844. module_usb_serial_driver(serial_drivers, id_table);
  845. MODULE_DESCRIPTION(DRIVER_DESC);
  846. MODULE_LICENSE("GPL");