quatech2.c 28 KB

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