quatech2.c 27 KB

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