quatech2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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_READ_BUFFER_SIZE 512 /* size of read buffer */
  58. #define QT2_WRITE_BUFFER_SIZE 512 /* size of write buffer */
  59. #define QT2_WRITE_CONTROL_SIZE 5 /* control bytes used for a write */
  60. #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
  61. #define USB_VENDOR_ID_QUATECH 0x061d
  62. #define QUATECH_SSU2_100 0xC120 /* RS232 single port */
  63. #define QUATECH_DSU2_100 0xC140 /* RS232 dual port */
  64. #define QUATECH_DSU2_400 0xC150 /* RS232/422/485 dual port */
  65. #define QUATECH_QSU2_100 0xC160 /* RS232 four port */
  66. #define QUATECH_QSU2_400 0xC170 /* RS232/422/485 four port */
  67. #define QUATECH_ESU2_100 0xC1A0 /* RS232 eight port */
  68. #define QUATECH_ESU2_400 0xC180 /* RS232/422/485 eight port */
  69. struct qt2_device_detail {
  70. int product_id;
  71. int num_ports;
  72. };
  73. #define QT_DETAILS(prod, ports) \
  74. .product_id = (prod), \
  75. .num_ports = (ports)
  76. static const struct qt2_device_detail qt2_device_details[] = {
  77. {QT_DETAILS(QUATECH_SSU2_100, 1)},
  78. {QT_DETAILS(QUATECH_DSU2_400, 2)},
  79. {QT_DETAILS(QUATECH_DSU2_100, 2)},
  80. {QT_DETAILS(QUATECH_QSU2_400, 4)},
  81. {QT_DETAILS(QUATECH_QSU2_100, 4)},
  82. {QT_DETAILS(QUATECH_ESU2_400, 8)},
  83. {QT_DETAILS(QUATECH_ESU2_100, 8)},
  84. {QT_DETAILS(0, 0)} /* Terminating entry */
  85. };
  86. static const struct usb_device_id id_table[] = {
  87. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
  88. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
  89. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
  90. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
  91. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
  92. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
  93. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
  94. {} /* Terminating entry */
  95. };
  96. MODULE_DEVICE_TABLE(usb, id_table);
  97. struct qt2_serial_private {
  98. unsigned char current_port; /* current port for incoming data */
  99. struct urb *read_urb; /* shared among all ports */
  100. char *read_buffer;
  101. };
  102. struct qt2_port_private {
  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 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->read_buffer);
  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 = port->port_number;
  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, "%s - initial setup failed (%i)\n",
  326. __func__, 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. /* flush the port transmit buffer */
  347. i = usb_control_msg(serial->dev,
  348. usb_rcvctrlpipe(serial->dev, 0),
  349. QT2_FLUSH_DEVICE, 0x40, 1,
  350. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  351. if (i < 0)
  352. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  353. __func__, i);
  354. /* flush the port receive buffer */
  355. i = usb_control_msg(serial->dev,
  356. usb_rcvctrlpipe(serial->dev, 0),
  357. QT2_FLUSH_DEVICE, 0x40, 0,
  358. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  359. if (i < 0)
  360. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  361. __func__, i);
  362. /* close the port */
  363. i = usb_control_msg(serial->dev,
  364. usb_sndctrlpipe(serial->dev, 0),
  365. QT_OPEN_CLOSE_CHANNEL,
  366. 0x40, 0,
  367. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  368. if (i < 0)
  369. dev_err(&port->dev, "%s - close port failed %i\n",
  370. __func__, i);
  371. }
  372. static void qt2_disconnect(struct usb_serial *serial)
  373. {
  374. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  375. usb_kill_urb(serial_priv->read_urb);
  376. }
  377. static int get_serial_info(struct usb_serial_port *port,
  378. struct serial_struct __user *retinfo)
  379. {
  380. struct serial_struct tmp;
  381. if (!retinfo)
  382. return -EFAULT;
  383. memset(&tmp, 0, sizeof(tmp));
  384. tmp.line = port->minor;
  385. tmp.port = 0;
  386. tmp.irq = 0;
  387. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  388. tmp.xmit_fifo_size = port->bulk_out_size;
  389. tmp.baud_base = 9600;
  390. tmp.close_delay = 5*HZ;
  391. tmp.closing_wait = 30*HZ;
  392. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  393. return -EFAULT;
  394. return 0;
  395. }
  396. static int qt2_ioctl(struct tty_struct *tty,
  397. unsigned int cmd, unsigned long arg)
  398. {
  399. struct usb_serial_port *port = tty->driver_data;
  400. switch (cmd) {
  401. case TIOCGSERIAL:
  402. return get_serial_info(port,
  403. (struct serial_struct __user *)arg);
  404. default:
  405. break;
  406. }
  407. return -ENOIOCTLCMD;
  408. }
  409. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  410. {
  411. switch (*ch) {
  412. case QT2_LINE_STATUS:
  413. qt2_update_lsr(port, ch + 1);
  414. break;
  415. case QT2_MODEM_STATUS:
  416. qt2_update_msr(port, ch + 1);
  417. break;
  418. }
  419. }
  420. /* not needed, kept to document functionality */
  421. static void qt2_process_xmit_empty(struct usb_serial_port *port,
  422. unsigned char *ch)
  423. {
  424. int bytes_written;
  425. bytes_written = (int)(*ch) + (int)(*(ch + 1) << 4);
  426. }
  427. /* not needed, kept to document functionality */
  428. static void qt2_process_flush(struct usb_serial_port *port, unsigned char *ch)
  429. {
  430. return;
  431. }
  432. static void qt2_process_read_urb(struct urb *urb)
  433. {
  434. struct usb_serial *serial;
  435. struct qt2_serial_private *serial_priv;
  436. struct usb_serial_port *port;
  437. struct qt2_port_private *port_priv;
  438. bool escapeflag;
  439. unsigned char *ch;
  440. int i;
  441. unsigned char newport;
  442. int len = urb->actual_length;
  443. if (!len)
  444. return;
  445. ch = urb->transfer_buffer;
  446. serial = urb->context;
  447. serial_priv = usb_get_serial_data(serial);
  448. port = serial->port[serial_priv->current_port];
  449. port_priv = usb_get_serial_port_data(port);
  450. for (i = 0; i < urb->actual_length; i++) {
  451. ch = (unsigned char *)urb->transfer_buffer + i;
  452. if ((i <= (len - 3)) &&
  453. (*ch == QT2_CONTROL_BYTE) &&
  454. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  455. escapeflag = false;
  456. switch (*(ch + 2)) {
  457. case QT2_LINE_STATUS:
  458. case QT2_MODEM_STATUS:
  459. if (i > (len - 4)) {
  460. dev_warn(&port->dev,
  461. "%s - status message too short\n",
  462. __func__);
  463. break;
  464. }
  465. qt2_process_status(port, ch + 2);
  466. i += 3;
  467. escapeflag = true;
  468. break;
  469. case QT2_XMIT_HOLD:
  470. if (i > (len - 5)) {
  471. dev_warn(&port->dev,
  472. "%s - xmit_empty message too short\n",
  473. __func__);
  474. break;
  475. }
  476. qt2_process_xmit_empty(port, ch + 3);
  477. i += 4;
  478. escapeflag = true;
  479. break;
  480. case QT2_CHANGE_PORT:
  481. if (i > (len - 4)) {
  482. dev_warn(&port->dev,
  483. "%s - change_port message too short\n",
  484. __func__);
  485. break;
  486. }
  487. tty_flip_buffer_push(&port->port);
  488. newport = *(ch + 3);
  489. if (newport > serial->num_ports) {
  490. dev_err(&port->dev,
  491. "%s - port change to invalid port: %i\n",
  492. __func__, newport);
  493. break;
  494. }
  495. serial_priv->current_port = newport;
  496. port = serial->port[serial_priv->current_port];
  497. port_priv = usb_get_serial_port_data(port);
  498. i += 3;
  499. escapeflag = true;
  500. break;
  501. case QT2_REC_FLUSH:
  502. case QT2_XMIT_FLUSH:
  503. qt2_process_flush(port, ch + 2);
  504. i += 2;
  505. escapeflag = true;
  506. break;
  507. case QT2_CONTROL_ESCAPE:
  508. tty_buffer_request_room(&port->port, 2);
  509. tty_insert_flip_string(&port->port, ch, 2);
  510. i += 2;
  511. escapeflag = true;
  512. break;
  513. default:
  514. dev_warn(&port->dev,
  515. "%s - unsupported command %i\n",
  516. __func__, *(ch + 2));
  517. break;
  518. }
  519. if (escapeflag)
  520. continue;
  521. }
  522. tty_buffer_request_room(&port->port, 1);
  523. tty_insert_flip_string(&port->port, ch, 1);
  524. }
  525. tty_flip_buffer_push(&port->port);
  526. }
  527. static void qt2_write_bulk_callback(struct urb *urb)
  528. {
  529. struct usb_serial_port *port;
  530. struct qt2_port_private *port_priv;
  531. port = urb->context;
  532. port_priv = usb_get_serial_port_data(port);
  533. spin_lock(&port_priv->urb_lock);
  534. port_priv->urb_in_use = false;
  535. usb_serial_port_softint(port);
  536. spin_unlock(&port_priv->urb_lock);
  537. }
  538. static void qt2_read_bulk_callback(struct urb *urb)
  539. {
  540. struct usb_serial *serial = urb->context;
  541. int status;
  542. if (urb->status) {
  543. dev_warn(&serial->dev->dev,
  544. "%s - non-zero urb status: %i\n", __func__,
  545. urb->status);
  546. return;
  547. }
  548. qt2_process_read_urb(urb);
  549. status = usb_submit_urb(urb, GFP_ATOMIC);
  550. if (status != 0)
  551. dev_err(&serial->dev->dev,
  552. "%s - resubmit read urb failed: %i\n",
  553. __func__, status);
  554. }
  555. static int qt2_setup_urbs(struct usb_serial *serial)
  556. {
  557. struct usb_serial_port *port0;
  558. struct qt2_serial_private *serial_priv;
  559. int status;
  560. port0 = serial->port[0];
  561. serial_priv = usb_get_serial_data(serial);
  562. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  563. if (!serial_priv->read_urb) {
  564. dev_err(&serial->dev->dev, "No free urbs available\n");
  565. return -ENOMEM;
  566. }
  567. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  568. usb_rcvbulkpipe(serial->dev,
  569. port0->bulk_in_endpointAddress),
  570. serial_priv->read_buffer,
  571. QT2_READ_BUFFER_SIZE,
  572. qt2_read_bulk_callback, serial);
  573. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  574. if (status != 0) {
  575. dev_err(&serial->dev->dev,
  576. "%s - submit read urb failed %i\n", __func__, status);
  577. usb_free_urb(serial_priv->read_urb);
  578. return status;
  579. }
  580. return 0;
  581. }
  582. static int qt2_attach(struct usb_serial *serial)
  583. {
  584. struct qt2_serial_private *serial_priv;
  585. int status;
  586. /* power on unit */
  587. status = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  588. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  589. QT2_USB_TIMEOUT);
  590. if (status < 0) {
  591. dev_err(&serial->dev->dev,
  592. "%s - failed to power on unit: %i\n", __func__, status);
  593. return status;
  594. }
  595. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  596. if (!serial_priv) {
  597. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  598. return -ENOMEM;
  599. }
  600. serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
  601. if (!serial_priv->read_buffer) {
  602. status = -ENOMEM;
  603. goto err_buf;
  604. }
  605. usb_set_serial_data(serial, serial_priv);
  606. status = qt2_setup_urbs(serial);
  607. if (status != 0)
  608. goto attach_failed;
  609. return 0;
  610. attach_failed:
  611. kfree(serial_priv->read_buffer);
  612. err_buf:
  613. kfree(serial_priv);
  614. return status;
  615. }
  616. static int qt2_port_probe(struct usb_serial_port *port)
  617. {
  618. struct usb_serial *serial = port->serial;
  619. struct qt2_port_private *port_priv;
  620. u8 bEndpointAddress;
  621. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  622. if (!port_priv)
  623. return -ENOMEM;
  624. spin_lock_init(&port_priv->lock);
  625. spin_lock_init(&port_priv->urb_lock);
  626. port_priv->port = port;
  627. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  628. if (!port_priv->write_urb) {
  629. kfree(port_priv);
  630. return -ENOMEM;
  631. }
  632. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  633. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  634. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  635. port_priv->write_buffer,
  636. sizeof(port_priv->write_buffer),
  637. qt2_write_bulk_callback, port);
  638. usb_set_serial_port_data(port, port_priv);
  639. return 0;
  640. }
  641. static int qt2_port_remove(struct usb_serial_port *port)
  642. {
  643. struct qt2_port_private *port_priv;
  644. port_priv = usb_get_serial_port_data(port);
  645. usb_free_urb(port_priv->write_urb);
  646. kfree(port_priv);
  647. return 0;
  648. }
  649. static int qt2_tiocmget(struct tty_struct *tty)
  650. {
  651. struct usb_serial_port *port = tty->driver_data;
  652. struct usb_device *dev = port->serial->dev;
  653. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  654. u8 *d;
  655. int r;
  656. d = kzalloc(2, GFP_KERNEL);
  657. if (!d)
  658. return -ENOMEM;
  659. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  660. if (r < 0)
  661. goto mget_out;
  662. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  663. if (r < 0)
  664. goto mget_out;
  665. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  666. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  667. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  668. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  669. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  670. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  671. mget_out:
  672. kfree(d);
  673. return r;
  674. }
  675. static int qt2_tiocmset(struct tty_struct *tty,
  676. unsigned int set, unsigned int clear)
  677. {
  678. struct qt2_port_private *port_priv;
  679. port_priv = usb_get_serial_port_data(tty->driver_data);
  680. return update_mctrl(port_priv, set, clear);
  681. }
  682. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  683. {
  684. struct usb_serial_port *port = tty->driver_data;
  685. struct qt2_port_private *port_priv;
  686. int status;
  687. u16 val;
  688. port_priv = usb_get_serial_port_data(port);
  689. val = (break_state == -1) ? 1 : 0;
  690. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  691. val, port_priv->device_port);
  692. if (status < 0)
  693. dev_warn(&port->dev,
  694. "%s - failed to send control message: %i\n", __func__,
  695. status);
  696. }
  697. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  698. {
  699. struct usb_device *dev = port->serial->dev;
  700. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  701. /* Disable flow control */
  702. if (!on) {
  703. if (qt2_setregister(dev, port_priv->device_port,
  704. UART_MCR, 0) < 0)
  705. dev_warn(&port->dev, "error from flowcontrol urb\n");
  706. }
  707. /* drop RTS and DTR */
  708. if (on)
  709. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  710. else
  711. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  712. }
  713. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  714. {
  715. struct qt2_port_private *port_priv;
  716. u8 newMSR = (u8) *ch;
  717. unsigned long flags;
  718. port_priv = usb_get_serial_port_data(port);
  719. spin_lock_irqsave(&port_priv->lock, flags);
  720. port_priv->shadowMSR = newMSR;
  721. spin_unlock_irqrestore(&port_priv->lock, flags);
  722. if (newMSR & UART_MSR_ANY_DELTA) {
  723. /* update input line counters */
  724. if (newMSR & UART_MSR_DCTS)
  725. port->icount.cts++;
  726. if (newMSR & UART_MSR_DDSR)
  727. port->icount.dsr++;
  728. if (newMSR & UART_MSR_DDCD)
  729. port->icount.dcd++;
  730. if (newMSR & UART_MSR_TERI)
  731. port->icount.rng++;
  732. wake_up_interruptible(&port->port.delta_msr_wait);
  733. }
  734. }
  735. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  736. {
  737. struct qt2_port_private *port_priv;
  738. struct async_icount *icount;
  739. unsigned long flags;
  740. u8 newLSR = (u8) *ch;
  741. port_priv = usb_get_serial_port_data(port);
  742. if (newLSR & UART_LSR_BI)
  743. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  744. spin_lock_irqsave(&port_priv->lock, flags);
  745. port_priv->shadowLSR = newLSR;
  746. spin_unlock_irqrestore(&port_priv->lock, flags);
  747. icount = &port->icount;
  748. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  749. if (newLSR & UART_LSR_BI)
  750. icount->brk++;
  751. if (newLSR & UART_LSR_OE)
  752. icount->overrun++;
  753. if (newLSR & UART_LSR_PE)
  754. icount->parity++;
  755. if (newLSR & UART_LSR_FE)
  756. icount->frame++;
  757. }
  758. }
  759. static int qt2_write_room(struct tty_struct *tty)
  760. {
  761. struct usb_serial_port *port = tty->driver_data;
  762. struct qt2_port_private *port_priv;
  763. unsigned long flags = 0;
  764. int r;
  765. port_priv = usb_get_serial_port_data(port);
  766. spin_lock_irqsave(&port_priv->urb_lock, flags);
  767. if (port_priv->urb_in_use)
  768. r = 0;
  769. else
  770. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  771. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  772. return r;
  773. }
  774. static int qt2_write(struct tty_struct *tty,
  775. struct usb_serial_port *port,
  776. const unsigned char *buf, int count)
  777. {
  778. struct qt2_port_private *port_priv;
  779. struct urb *write_urb;
  780. unsigned char *data;
  781. unsigned long flags;
  782. int status;
  783. int bytes_out = 0;
  784. port_priv = usb_get_serial_port_data(port);
  785. if (port_priv->write_urb == NULL) {
  786. dev_err(&port->dev, "%s - no output urb\n", __func__);
  787. return 0;
  788. }
  789. write_urb = port_priv->write_urb;
  790. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  791. data = write_urb->transfer_buffer;
  792. spin_lock_irqsave(&port_priv->urb_lock, flags);
  793. if (port_priv->urb_in_use == true) {
  794. dev_err(&port->dev, "qt2_write - urb is in use\n");
  795. goto write_out;
  796. }
  797. *data++ = QT2_CONTROL_BYTE;
  798. *data++ = QT2_CONTROL_BYTE;
  799. *data++ = port_priv->device_port;
  800. put_unaligned_le16(count, data);
  801. data += 2;
  802. memcpy(data, buf, count);
  803. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  804. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  805. if (status == 0) {
  806. port_priv->urb_in_use = true;
  807. bytes_out += count;
  808. }
  809. write_out:
  810. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  811. return bytes_out;
  812. }
  813. static struct usb_serial_driver qt2_device = {
  814. .driver = {
  815. .owner = THIS_MODULE,
  816. .name = "quatech-serial",
  817. },
  818. .description = DRIVER_DESC,
  819. .id_table = id_table,
  820. .open = qt2_open,
  821. .close = qt2_close,
  822. .write = qt2_write,
  823. .write_room = qt2_write_room,
  824. .calc_num_ports = qt2_calc_num_ports,
  825. .attach = qt2_attach,
  826. .release = qt2_release,
  827. .disconnect = qt2_disconnect,
  828. .port_probe = qt2_port_probe,
  829. .port_remove = qt2_port_remove,
  830. .dtr_rts = qt2_dtr_rts,
  831. .break_ctl = qt2_break_ctl,
  832. .tiocmget = qt2_tiocmget,
  833. .tiocmset = qt2_tiocmset,
  834. .tiocmiwait = usb_serial_generic_tiocmiwait,
  835. .get_icount = usb_serial_generic_get_icount,
  836. .ioctl = qt2_ioctl,
  837. .set_termios = qt2_set_termios,
  838. };
  839. static struct usb_serial_driver *const serial_drivers[] = {
  840. &qt2_device, NULL
  841. };
  842. module_usb_serial_driver(serial_drivers, id_table);
  843. MODULE_DESCRIPTION(DRIVER_DESC);
  844. MODULE_LICENSE("GPL");