quatech2.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_driver.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/module.h>
  23. #include <linux/serial.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/serial.h>
  26. #include <linux/serial_reg.h>
  27. #include <linux/uaccess.h>
  28. /* default urb timeout for usb operations */
  29. #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
  30. #define QT_OPEN_CLOSE_CHANNEL 0xca
  31. #define QT_SET_GET_DEVICE 0xc2
  32. #define QT_SET_GET_REGISTER 0xc0
  33. #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
  34. #define QT_SET_ATF 0xcd
  35. #define QT_TRANSFER_IN 0xc0
  36. #define QT_HW_FLOW_CONTROL_MASK 0xc5
  37. #define QT_SW_FLOW_CONTROL_MASK 0xc6
  38. #define QT2_BREAK_CONTROL 0xc8
  39. #define QT2_GET_SET_UART 0xc1
  40. #define QT2_FLUSH_DEVICE 0xc4
  41. #define QT2_GET_SET_QMCR 0xe1
  42. #define QT2_QMCR_RS232 0x40
  43. #define QT2_QMCR_RS422 0x10
  44. #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
  45. #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
  46. /* status bytes for the device */
  47. #define QT2_CONTROL_BYTE 0x1b
  48. #define QT2_LINE_STATUS 0x00 /* following 1 byte is line status */
  49. #define QT2_MODEM_STATUS 0x01 /* following 1 byte is modem status */
  50. #define QT2_XMIT_HOLD 0x02 /* following 2 bytes are ?? */
  51. #define QT2_CHANGE_PORT 0x03 /* following 1 byte is port to change to */
  52. #define QT2_REC_FLUSH 0x04 /* no following info */
  53. #define QT2_XMIT_FLUSH 0x05 /* no following info */
  54. #define QT2_CONTROL_ESCAPE 0xff /* pass through previous 2 control bytes */
  55. #define MAX_BAUD_RATE 921600
  56. #define DEFAULT_BAUD_RATE 9600
  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[512];
  100. };
  101. struct qt2_port_private {
  102. bool is_open;
  103. u8 device_port;
  104. spinlock_t urb_lock;
  105. bool urb_in_use;
  106. struct urb *write_urb;
  107. char write_buffer[QT2_WRITE_BUFFER_SIZE];
  108. spinlock_t lock;
  109. u8 shadowLSR;
  110. u8 shadowMSR;
  111. struct async_icount icount;
  112. struct usb_serial_port *port;
  113. };
  114. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch);
  115. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch);
  116. static void qt2_write_bulk_callback(struct urb *urb);
  117. static void qt2_read_bulk_callback(struct urb *urb);
  118. static void qt2_release(struct usb_serial *serial)
  119. {
  120. struct qt2_serial_private *serial_priv;
  121. serial_priv = usb_get_serial_data(serial);
  122. usb_free_urb(serial_priv->read_urb);
  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_getdevice(struct usb_device *dev, u8 *data)
  158. {
  159. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  160. QT_SET_GET_DEVICE, 0xc0, 0, 0,
  161. data, 3, QT2_USB_TIMEOUT);
  162. }
  163. static inline int qt2_getregister(struct usb_device *dev,
  164. u8 uart,
  165. u8 reg,
  166. u8 *data)
  167. {
  168. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  169. QT_SET_GET_REGISTER, 0xc0, reg,
  170. uart, data, sizeof(*data), QT2_USB_TIMEOUT);
  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 = (u16) (port->number - port->serial->minor);
  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 < 0) {
  311. dev_err(&port->dev, "%s - open port failed %i", __func__,
  312. status);
  313. kfree(data);
  314. return status;
  315. }
  316. spin_lock_irqsave(&port_priv->lock, flags);
  317. port_priv->shadowLSR = data[0];
  318. port_priv->shadowMSR = data[1];
  319. spin_unlock_irqrestore(&port_priv->lock, flags);
  320. kfree(data);
  321. /* set to default speed and 8bit word size */
  322. status = qt2_set_port_config(serial->dev, device_port,
  323. DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
  324. if (status < 0) {
  325. dev_err(&port->dev,
  326. "%s - initial setup failed for port %i (%i)\n",
  327. __func__, port->number, device_port);
  328. return status;
  329. }
  330. port_priv->is_open = true;
  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. unsigned long flags;
  341. int i;
  342. serial = port->serial;
  343. port_priv = usb_get_serial_port_data(port);
  344. port_priv->is_open = false;
  345. spin_lock_irqsave(&port_priv->urb_lock, flags);
  346. usb_kill_urb(port_priv->write_urb);
  347. port_priv->urb_in_use = false;
  348. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  349. mutex_lock(&port->serial->disc_mutex);
  350. if (port->serial->disconnected) {
  351. mutex_unlock(&port->serial->disc_mutex);
  352. return;
  353. }
  354. /* flush the port transmit buffer */
  355. i = usb_control_msg(serial->dev,
  356. usb_rcvctrlpipe(serial->dev, 0),
  357. QT2_FLUSH_DEVICE, 0x40, 1,
  358. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  359. if (i < 0)
  360. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  361. __func__, i);
  362. /* flush the port receive buffer */
  363. i = usb_control_msg(serial->dev,
  364. usb_rcvctrlpipe(serial->dev, 0),
  365. QT2_FLUSH_DEVICE, 0x40, 0,
  366. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  367. if (i < 0)
  368. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  369. __func__, i);
  370. /* close the port */
  371. i = usb_control_msg(serial->dev,
  372. usb_sndctrlpipe(serial->dev, 0),
  373. QT_OPEN_CLOSE_CHANNEL,
  374. 0x40, 0,
  375. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  376. if (i < 0)
  377. dev_err(&port->dev, "%s - close port failed %i\n",
  378. __func__, i);
  379. mutex_unlock(&port->serial->disc_mutex);
  380. }
  381. static void qt2_disconnect(struct usb_serial *serial)
  382. {
  383. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  384. usb_kill_urb(serial_priv->read_urb);
  385. }
  386. static int get_serial_info(struct usb_serial_port *port,
  387. struct serial_struct __user *retinfo)
  388. {
  389. struct serial_struct tmp;
  390. if (!retinfo)
  391. return -EFAULT;
  392. memset(&tmp, 0, sizeof(tmp));
  393. tmp.line = port->serial->minor;
  394. tmp.port = 0;
  395. tmp.irq = 0;
  396. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  397. tmp.xmit_fifo_size = port->bulk_out_size;
  398. tmp.baud_base = 9600;
  399. tmp.close_delay = 5*HZ;
  400. tmp.closing_wait = 30*HZ;
  401. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  402. return -EFAULT;
  403. return 0;
  404. }
  405. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  406. {
  407. struct qt2_port_private *priv = usb_get_serial_port_data(port);
  408. struct async_icount prev, cur;
  409. unsigned long flags;
  410. spin_lock_irqsave(&priv->lock, flags);
  411. prev = priv->icount;
  412. spin_unlock_irqrestore(&priv->lock, flags);
  413. while (1) {
  414. wait_event_interruptible(port->delta_msr_wait,
  415. (port->serial->disconnected ||
  416. (priv->icount.rng != prev.rng) ||
  417. (priv->icount.dsr != prev.dsr) ||
  418. (priv->icount.dcd != prev.dcd) ||
  419. (priv->icount.cts != prev.cts)));
  420. if (signal_pending(current))
  421. return -ERESTARTSYS;
  422. if (port->serial->disconnected)
  423. return -EIO;
  424. spin_lock_irqsave(&priv->lock, flags);
  425. cur = priv->icount;
  426. spin_unlock_irqrestore(&priv->lock, flags);
  427. if ((prev.rng == cur.rng) &&
  428. (prev.dsr == cur.dsr) &&
  429. (prev.dcd == cur.dcd) &&
  430. (prev.cts == cur.cts))
  431. return -EIO;
  432. if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
  433. (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
  434. (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
  435. (arg & TIOCM_CTS && (prev.cts != cur.cts)))
  436. return 0;
  437. }
  438. return 0;
  439. }
  440. static int qt2_get_icount(struct tty_struct *tty,
  441. struct serial_icounter_struct *icount)
  442. {
  443. struct usb_serial_port *port = tty->driver_data;
  444. struct qt2_port_private *priv = usb_get_serial_port_data(port);
  445. struct async_icount cnow = priv->icount;
  446. icount->cts = cnow.cts;
  447. icount->dsr = cnow.dsr;
  448. icount->rng = cnow.rng;
  449. icount->dcd = cnow.dcd;
  450. icount->rx = cnow.rx;
  451. icount->tx = cnow.tx;
  452. icount->frame = cnow.frame;
  453. icount->overrun = cnow.overrun;
  454. icount->parity = cnow.parity;
  455. icount->brk = cnow.brk;
  456. icount->buf_overrun = cnow.buf_overrun;
  457. return 0;
  458. }
  459. static int qt2_ioctl(struct tty_struct *tty,
  460. unsigned int cmd, unsigned long arg)
  461. {
  462. struct usb_serial_port *port = tty->driver_data;
  463. switch (cmd) {
  464. case TIOCGSERIAL:
  465. return get_serial_info(port,
  466. (struct serial_struct __user *)arg);
  467. case TIOCMIWAIT:
  468. return wait_modem_info(port, arg);
  469. default:
  470. break;
  471. }
  472. return -ENOIOCTLCMD;
  473. }
  474. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  475. {
  476. switch (*ch) {
  477. case QT2_LINE_STATUS:
  478. qt2_update_lsr(port, ch + 1);
  479. break;
  480. case QT2_MODEM_STATUS:
  481. qt2_update_msr(port, ch + 1);
  482. break;
  483. }
  484. }
  485. /* not needed, kept to document functionality */
  486. static void qt2_process_xmit_empty(struct usb_serial_port *port,
  487. unsigned char *ch)
  488. {
  489. int bytes_written;
  490. bytes_written = (int)(*ch) + (int)(*(ch + 1) << 4);
  491. }
  492. /* not needed, kept to document functionality */
  493. static void qt2_process_flush(struct usb_serial_port *port, unsigned char *ch)
  494. {
  495. return;
  496. }
  497. void qt2_process_read_urb(struct urb *urb)
  498. {
  499. struct usb_serial *serial;
  500. struct qt2_serial_private *serial_priv;
  501. struct usb_serial_port *port;
  502. struct qt2_port_private *port_priv;
  503. bool escapeflag;
  504. unsigned char *ch;
  505. int i;
  506. unsigned char newport;
  507. int len = urb->actual_length;
  508. if (!len)
  509. return;
  510. ch = urb->transfer_buffer;
  511. serial = urb->context;
  512. serial_priv = usb_get_serial_data(serial);
  513. port = serial->port[serial_priv->current_port];
  514. port_priv = usb_get_serial_port_data(port);
  515. for (i = 0; i < urb->actual_length; i++) {
  516. ch = (unsigned char *)urb->transfer_buffer + i;
  517. if ((i <= (len - 3)) &&
  518. (*ch == QT2_CONTROL_BYTE) &&
  519. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  520. escapeflag = false;
  521. switch (*(ch + 2)) {
  522. case QT2_LINE_STATUS:
  523. case QT2_MODEM_STATUS:
  524. if (i > (len - 4)) {
  525. dev_warn(&port->dev,
  526. "%s - status message too short\n",
  527. __func__);
  528. break;
  529. }
  530. qt2_process_status(port, ch + 2);
  531. i += 3;
  532. escapeflag = true;
  533. break;
  534. case QT2_XMIT_HOLD:
  535. if (i > (len - 5)) {
  536. dev_warn(&port->dev,
  537. "%s - xmit_empty message too short\n",
  538. __func__);
  539. break;
  540. }
  541. qt2_process_xmit_empty(port, ch + 3);
  542. i += 4;
  543. escapeflag = true;
  544. break;
  545. case QT2_CHANGE_PORT:
  546. if (i > (len - 4)) {
  547. dev_warn(&port->dev,
  548. "%s - change_port message too short\n",
  549. __func__);
  550. break;
  551. }
  552. if (port_priv->is_open)
  553. tty_flip_buffer_push(&port->port);
  554. newport = *(ch + 3);
  555. if (newport > serial->num_ports) {
  556. dev_err(&port->dev,
  557. "%s - port change to invalid port: %i\n",
  558. __func__, newport);
  559. break;
  560. }
  561. serial_priv->current_port = newport;
  562. port = serial->port[serial_priv->current_port];
  563. port_priv = usb_get_serial_port_data(port);
  564. i += 3;
  565. escapeflag = true;
  566. break;
  567. case QT2_REC_FLUSH:
  568. case QT2_XMIT_FLUSH:
  569. qt2_process_flush(port, ch + 2);
  570. i += 2;
  571. escapeflag = true;
  572. break;
  573. case QT2_CONTROL_ESCAPE:
  574. tty_buffer_request_room(&port->port, 2);
  575. tty_insert_flip_string(&port->port, ch, 2);
  576. i += 2;
  577. escapeflag = true;
  578. break;
  579. default:
  580. dev_warn(&port->dev,
  581. "%s - unsupported command %i\n",
  582. __func__, *(ch + 2));
  583. break;
  584. }
  585. if (escapeflag)
  586. continue;
  587. }
  588. tty_buffer_request_room(&port->port, 1);
  589. tty_insert_flip_string(&port->port, ch, 1);
  590. }
  591. if (port_priv->is_open)
  592. tty_flip_buffer_push(&port->port);
  593. }
  594. static void qt2_write_bulk_callback(struct urb *urb)
  595. {
  596. struct usb_serial_port *port;
  597. struct qt2_port_private *port_priv;
  598. port = urb->context;
  599. port_priv = usb_get_serial_port_data(port);
  600. spin_lock(&port_priv->urb_lock);
  601. port_priv->urb_in_use = false;
  602. usb_serial_port_softint(port);
  603. spin_unlock(&port_priv->urb_lock);
  604. }
  605. static void qt2_read_bulk_callback(struct urb *urb)
  606. {
  607. struct usb_serial *serial = urb->context;
  608. int status;
  609. if (urb->status) {
  610. dev_warn(&serial->dev->dev,
  611. "%s - non-zero urb status: %i\n", __func__,
  612. urb->status);
  613. return;
  614. }
  615. qt2_process_read_urb(urb);
  616. status = usb_submit_urb(urb, GFP_ATOMIC);
  617. if (status != 0)
  618. dev_err(&serial->dev->dev,
  619. "%s - resubmit read urb failed: %i\n",
  620. __func__, status);
  621. }
  622. static int qt2_setup_urbs(struct usb_serial *serial)
  623. {
  624. struct usb_serial_port *port0;
  625. struct qt2_serial_private *serial_priv;
  626. int status;
  627. port0 = serial->port[0];
  628. serial_priv = usb_get_serial_data(serial);
  629. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  630. if (!serial_priv->read_urb) {
  631. dev_err(&serial->dev->dev, "No free urbs available\n");
  632. return -ENOMEM;
  633. }
  634. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  635. usb_rcvbulkpipe(serial->dev,
  636. port0->bulk_in_endpointAddress),
  637. serial_priv->read_buffer,
  638. sizeof(serial_priv->read_buffer),
  639. qt2_read_bulk_callback, serial);
  640. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  641. if (status != 0) {
  642. dev_err(&serial->dev->dev,
  643. "%s - submit read urb failed %i\n", __func__, status);
  644. usb_free_urb(serial_priv->read_urb);
  645. return status;
  646. }
  647. return 0;
  648. }
  649. static int qt2_attach(struct usb_serial *serial)
  650. {
  651. struct qt2_serial_private *serial_priv;
  652. int status;
  653. /* power on unit */
  654. status = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  655. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  656. QT2_USB_TIMEOUT);
  657. if (status < 0) {
  658. dev_err(&serial->dev->dev,
  659. "%s - failed to power on unit: %i\n", __func__, status);
  660. return status;
  661. }
  662. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  663. if (!serial_priv) {
  664. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  665. return -ENOMEM;
  666. }
  667. usb_set_serial_data(serial, serial_priv);
  668. status = qt2_setup_urbs(serial);
  669. if (status != 0)
  670. goto attach_failed;
  671. return 0;
  672. attach_failed:
  673. kfree(serial_priv);
  674. return status;
  675. }
  676. static int qt2_port_probe(struct usb_serial_port *port)
  677. {
  678. struct usb_serial *serial = port->serial;
  679. struct qt2_port_private *port_priv;
  680. u8 bEndpointAddress;
  681. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  682. if (!port_priv)
  683. return -ENOMEM;
  684. spin_lock_init(&port_priv->lock);
  685. spin_lock_init(&port_priv->urb_lock);
  686. port_priv->port = port;
  687. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  688. if (!port_priv->write_urb) {
  689. kfree(port_priv);
  690. return -ENOMEM;
  691. }
  692. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  693. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  694. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  695. port_priv->write_buffer,
  696. sizeof(port_priv->write_buffer),
  697. qt2_write_bulk_callback, port);
  698. usb_set_serial_port_data(port, port_priv);
  699. return 0;
  700. }
  701. static int qt2_port_remove(struct usb_serial_port *port)
  702. {
  703. struct qt2_port_private *port_priv;
  704. port_priv = usb_get_serial_port_data(port);
  705. usb_free_urb(port_priv->write_urb);
  706. kfree(port_priv);
  707. return 0;
  708. }
  709. static int qt2_tiocmget(struct tty_struct *tty)
  710. {
  711. struct usb_serial_port *port = tty->driver_data;
  712. struct usb_device *dev = port->serial->dev;
  713. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  714. u8 *d;
  715. int r;
  716. d = kzalloc(2, GFP_KERNEL);
  717. if (!d)
  718. return -ENOMEM;
  719. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  720. if (r < 0)
  721. goto mget_out;
  722. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  723. if (r < 0)
  724. goto mget_out;
  725. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  726. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  727. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  728. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  729. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  730. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  731. mget_out:
  732. kfree(d);
  733. return r;
  734. }
  735. static int qt2_tiocmset(struct tty_struct *tty,
  736. unsigned int set, unsigned int clear)
  737. {
  738. struct qt2_port_private *port_priv;
  739. port_priv = usb_get_serial_port_data(tty->driver_data);
  740. return update_mctrl(port_priv, set, clear);
  741. }
  742. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  743. {
  744. struct usb_serial_port *port = tty->driver_data;
  745. struct qt2_port_private *port_priv;
  746. int status;
  747. u16 val;
  748. port_priv = usb_get_serial_port_data(port);
  749. if (!port_priv->is_open) {
  750. dev_err(&port->dev,
  751. "%s - port is not open\n", __func__);
  752. return;
  753. }
  754. val = (break_state == -1) ? 1 : 0;
  755. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  756. val, port_priv->device_port);
  757. if (status < 0)
  758. dev_warn(&port->dev,
  759. "%s - failed to send control message: %i\n", __func__,
  760. status);
  761. }
  762. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  763. {
  764. struct usb_device *dev = port->serial->dev;
  765. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  766. /* Disable flow control */
  767. if (!on) {
  768. if (qt2_setregister(dev, port_priv->device_port,
  769. UART_MCR, 0) < 0)
  770. dev_warn(&port->dev, "error from flowcontrol urb\n");
  771. }
  772. /* drop RTS and DTR */
  773. if (on)
  774. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  775. else
  776. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  777. }
  778. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  779. {
  780. struct qt2_port_private *port_priv;
  781. u8 newMSR = (u8) *ch;
  782. unsigned long flags;
  783. port_priv = usb_get_serial_port_data(port);
  784. spin_lock_irqsave(&port_priv->lock, flags);
  785. port_priv->shadowMSR = newMSR;
  786. spin_unlock_irqrestore(&port_priv->lock, flags);
  787. if (newMSR & UART_MSR_ANY_DELTA) {
  788. /* update input line counters */
  789. if (newMSR & UART_MSR_DCTS)
  790. port_priv->icount.cts++;
  791. if (newMSR & UART_MSR_DDSR)
  792. port_priv->icount.dsr++;
  793. if (newMSR & UART_MSR_DDCD)
  794. port_priv->icount.dcd++;
  795. if (newMSR & UART_MSR_TERI)
  796. port_priv->icount.rng++;
  797. wake_up_interruptible(&port->delta_msr_wait);
  798. }
  799. }
  800. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  801. {
  802. struct qt2_port_private *port_priv;
  803. struct async_icount *icount;
  804. unsigned long flags;
  805. u8 newLSR = (u8) *ch;
  806. port_priv = usb_get_serial_port_data(port);
  807. if (newLSR & UART_LSR_BI)
  808. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  809. spin_lock_irqsave(&port_priv->lock, flags);
  810. port_priv->shadowLSR = newLSR;
  811. spin_unlock_irqrestore(&port_priv->lock, flags);
  812. icount = &port_priv->icount;
  813. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  814. if (newLSR & UART_LSR_BI)
  815. icount->brk++;
  816. if (newLSR & UART_LSR_OE)
  817. icount->overrun++;
  818. if (newLSR & UART_LSR_PE)
  819. icount->parity++;
  820. if (newLSR & UART_LSR_FE)
  821. icount->frame++;
  822. }
  823. }
  824. static int qt2_write_room(struct tty_struct *tty)
  825. {
  826. struct usb_serial_port *port = tty->driver_data;
  827. struct qt2_port_private *port_priv;
  828. unsigned long flags = 0;
  829. int r;
  830. port_priv = usb_get_serial_port_data(port);
  831. spin_lock_irqsave(&port_priv->urb_lock, flags);
  832. if (port_priv->urb_in_use)
  833. r = 0;
  834. else
  835. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  836. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  837. return r;
  838. }
  839. static int qt2_write(struct tty_struct *tty,
  840. struct usb_serial_port *port,
  841. const unsigned char *buf, int count)
  842. {
  843. struct qt2_port_private *port_priv;
  844. struct urb *write_urb;
  845. unsigned char *data;
  846. unsigned long flags;
  847. int status;
  848. int bytes_out = 0;
  849. port_priv = usb_get_serial_port_data(port);
  850. if (port_priv->write_urb == NULL) {
  851. dev_err(&port->dev, "%s - no output urb\n", __func__);
  852. return 0;
  853. }
  854. write_urb = port_priv->write_urb;
  855. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  856. data = write_urb->transfer_buffer;
  857. spin_lock_irqsave(&port_priv->urb_lock, flags);
  858. if (port_priv->urb_in_use == true) {
  859. dev_err(&port->dev, "qt2_write - urb is in use\n");
  860. goto write_out;
  861. }
  862. *data++ = QT2_CONTROL_BYTE;
  863. *data++ = QT2_CONTROL_BYTE;
  864. *data++ = port_priv->device_port;
  865. put_unaligned_le16(count, data);
  866. data += 2;
  867. memcpy(data, buf, count);
  868. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  869. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  870. if (status == 0) {
  871. port_priv->urb_in_use = true;
  872. bytes_out += count;
  873. }
  874. write_out:
  875. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  876. return bytes_out;
  877. }
  878. static struct usb_serial_driver qt2_device = {
  879. .driver = {
  880. .owner = THIS_MODULE,
  881. .name = "quatech-serial",
  882. },
  883. .description = DRIVER_DESC,
  884. .id_table = id_table,
  885. .open = qt2_open,
  886. .close = qt2_close,
  887. .write = qt2_write,
  888. .write_room = qt2_write_room,
  889. .calc_num_ports = qt2_calc_num_ports,
  890. .attach = qt2_attach,
  891. .release = qt2_release,
  892. .disconnect = qt2_disconnect,
  893. .port_probe = qt2_port_probe,
  894. .port_remove = qt2_port_remove,
  895. .dtr_rts = qt2_dtr_rts,
  896. .break_ctl = qt2_break_ctl,
  897. .tiocmget = qt2_tiocmget,
  898. .tiocmset = qt2_tiocmset,
  899. .get_icount = qt2_get_icount,
  900. .ioctl = qt2_ioctl,
  901. .set_termios = qt2_set_termios,
  902. };
  903. static struct usb_serial_driver *const serial_drivers[] = {
  904. &qt2_device, NULL
  905. };
  906. module_usb_serial_driver(serial_drivers, id_table);
  907. MODULE_DESCRIPTION(DRIVER_DESC);
  908. MODULE_LICENSE("GPL");