quatech2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * usb-serial driver for Quatech USB 2 devices
  4. *
  5. * Copyright (C) 2012 Bill Pemberton (wfp5p@virginia.edu)
  6. *
  7. * These devices all have only 1 bulk in and 1 bulk out that is shared
  8. * for all serial ports.
  9. *
  10. */
  11. #include <asm/unaligned.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/serial.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. #include <linux/serial_reg.h>
  22. #include <linux/uaccess.h>
  23. /* default urb timeout for usb operations */
  24. #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
  25. #define QT_OPEN_CLOSE_CHANNEL 0xca
  26. #define QT_SET_GET_DEVICE 0xc2
  27. #define QT_SET_GET_REGISTER 0xc0
  28. #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
  29. #define QT_SET_ATF 0xcd
  30. #define QT_TRANSFER_IN 0xc0
  31. #define QT_HW_FLOW_CONTROL_MASK 0xc5
  32. #define QT_SW_FLOW_CONTROL_MASK 0xc6
  33. #define QT2_BREAK_CONTROL 0xc8
  34. #define QT2_GET_SET_UART 0xc1
  35. #define QT2_FLUSH_DEVICE 0xc4
  36. #define QT2_GET_SET_QMCR 0xe1
  37. #define QT2_QMCR_RS232 0x40
  38. #define QT2_QMCR_RS422 0x10
  39. #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
  40. #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
  41. /* status bytes for the device */
  42. #define QT2_CONTROL_BYTE 0x1b
  43. #define QT2_LINE_STATUS 0x00 /* following 1 byte is line status */
  44. #define QT2_MODEM_STATUS 0x01 /* following 1 byte is modem status */
  45. #define QT2_XMIT_HOLD 0x02 /* following 2 bytes are ?? */
  46. #define QT2_CHANGE_PORT 0x03 /* following 1 byte is port to change to */
  47. #define QT2_REC_FLUSH 0x04 /* no following info */
  48. #define QT2_XMIT_FLUSH 0x05 /* no following info */
  49. #define QT2_CONTROL_ESCAPE 0xff /* pass through previous 2 control bytes */
  50. #define MAX_BAUD_RATE 921600
  51. #define DEFAULT_BAUD_RATE 9600
  52. #define QT2_READ_BUFFER_SIZE 512 /* size of read buffer */
  53. #define QT2_WRITE_BUFFER_SIZE 512 /* size of write buffer */
  54. #define QT2_WRITE_CONTROL_SIZE 5 /* control bytes used for a write */
  55. #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
  56. #define USB_VENDOR_ID_QUATECH 0x061d
  57. #define QUATECH_SSU2_100 0xC120 /* RS232 single port */
  58. #define QUATECH_DSU2_100 0xC140 /* RS232 dual port */
  59. #define QUATECH_DSU2_400 0xC150 /* RS232/422/485 dual port */
  60. #define QUATECH_QSU2_100 0xC160 /* RS232 four port */
  61. #define QUATECH_QSU2_400 0xC170 /* RS232/422/485 four port */
  62. #define QUATECH_ESU2_100 0xC1A0 /* RS232 eight port */
  63. #define QUATECH_ESU2_400 0xC180 /* RS232/422/485 eight port */
  64. struct qt2_device_detail {
  65. int product_id;
  66. int num_ports;
  67. };
  68. #define QT_DETAILS(prod, ports) \
  69. .product_id = (prod), \
  70. .num_ports = (ports)
  71. static const struct qt2_device_detail qt2_device_details[] = {
  72. {QT_DETAILS(QUATECH_SSU2_100, 1)},
  73. {QT_DETAILS(QUATECH_DSU2_400, 2)},
  74. {QT_DETAILS(QUATECH_DSU2_100, 2)},
  75. {QT_DETAILS(QUATECH_QSU2_400, 4)},
  76. {QT_DETAILS(QUATECH_QSU2_100, 4)},
  77. {QT_DETAILS(QUATECH_ESU2_400, 8)},
  78. {QT_DETAILS(QUATECH_ESU2_100, 8)},
  79. {QT_DETAILS(0, 0)} /* Terminating entry */
  80. };
  81. static const struct usb_device_id id_table[] = {
  82. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
  83. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
  84. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
  85. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
  86. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
  87. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
  88. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
  89. {} /* Terminating entry */
  90. };
  91. MODULE_DEVICE_TABLE(usb, id_table);
  92. struct qt2_serial_private {
  93. unsigned char current_port; /* current port for incoming data */
  94. struct urb *read_urb; /* shared among all ports */
  95. char *read_buffer;
  96. };
  97. struct qt2_port_private {
  98. u8 device_port;
  99. spinlock_t urb_lock;
  100. bool urb_in_use;
  101. struct urb *write_urb;
  102. char *write_buffer;
  103. spinlock_t lock;
  104. u8 shadowLSR;
  105. u8 shadowMSR;
  106. struct usb_serial_port *port;
  107. };
  108. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch);
  109. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch);
  110. static void qt2_write_bulk_callback(struct urb *urb);
  111. static void qt2_read_bulk_callback(struct urb *urb);
  112. static void qt2_release(struct usb_serial *serial)
  113. {
  114. struct qt2_serial_private *serial_priv;
  115. serial_priv = usb_get_serial_data(serial);
  116. usb_kill_urb(serial_priv->read_urb);
  117. usb_free_urb(serial_priv->read_urb);
  118. kfree(serial_priv->read_buffer);
  119. kfree(serial_priv);
  120. }
  121. static inline int calc_baud_divisor(int baudrate)
  122. {
  123. int divisor, rem;
  124. divisor = MAX_BAUD_RATE / baudrate;
  125. rem = MAX_BAUD_RATE % baudrate;
  126. /* Round to nearest divisor */
  127. if (((rem * 2) >= baudrate) && (baudrate != 110))
  128. divisor++;
  129. return divisor;
  130. }
  131. static inline int qt2_set_port_config(struct usb_device *dev,
  132. unsigned char port_number,
  133. u16 baudrate, u16 lcr)
  134. {
  135. int divisor = calc_baud_divisor(baudrate);
  136. u16 index = ((u16) (lcr << 8) | (u16) (port_number));
  137. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  138. QT2_GET_SET_UART, 0x40,
  139. divisor, index, NULL, 0, QT2_USB_TIMEOUT);
  140. }
  141. static inline int qt2_control_msg(struct usb_device *dev,
  142. u8 request, u16 data, u16 index)
  143. {
  144. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  145. request, 0x40, data, index,
  146. NULL, 0, QT2_USB_TIMEOUT);
  147. }
  148. static inline int qt2_setdevice(struct usb_device *dev, u8 *data)
  149. {
  150. u16 x = ((u16) (data[1] << 8) | (u16) (data[0]));
  151. return qt2_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
  152. }
  153. static inline int qt2_getregister(struct usb_device *dev,
  154. u8 uart,
  155. u8 reg,
  156. u8 *data)
  157. {
  158. int ret;
  159. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  160. QT_SET_GET_REGISTER, 0xc0, reg,
  161. uart, data, sizeof(*data), QT2_USB_TIMEOUT);
  162. if (ret < (int)sizeof(*data)) {
  163. if (ret >= 0)
  164. ret = -EIO;
  165. }
  166. return ret;
  167. }
  168. static inline int qt2_setregister(struct usb_device *dev,
  169. u8 uart, u8 reg, u16 data)
  170. {
  171. u16 value = (data << 8) | reg;
  172. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  173. QT_SET_GET_REGISTER, 0x40, value, uart,
  174. NULL, 0, QT2_USB_TIMEOUT);
  175. }
  176. static inline int update_mctrl(struct qt2_port_private *port_priv,
  177. unsigned int set, unsigned int clear)
  178. {
  179. struct usb_serial_port *port = port_priv->port;
  180. struct usb_device *dev = port->serial->dev;
  181. unsigned urb_value;
  182. int status;
  183. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
  184. dev_dbg(&port->dev,
  185. "update_mctrl - DTR|RTS not being set|cleared\n");
  186. return 0; /* no change */
  187. }
  188. clear &= ~set; /* 'set' takes precedence over 'clear' */
  189. urb_value = 0;
  190. if (set & TIOCM_DTR)
  191. urb_value |= UART_MCR_DTR;
  192. if (set & TIOCM_RTS)
  193. urb_value |= UART_MCR_RTS;
  194. status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
  195. urb_value);
  196. if (status < 0)
  197. dev_err(&port->dev,
  198. "update_mctrl - Error from MODEM_CTRL urb: %i\n",
  199. status);
  200. return status;
  201. }
  202. static int qt2_calc_num_ports(struct usb_serial *serial,
  203. struct usb_serial_endpoints *epds)
  204. {
  205. struct qt2_device_detail d;
  206. int i;
  207. for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
  208. if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
  209. return d.num_ports;
  210. }
  211. /* we didn't recognize the device */
  212. dev_err(&serial->dev->dev,
  213. "don't know the number of ports, assuming 1\n");
  214. return 1;
  215. }
  216. static void qt2_set_termios(struct tty_struct *tty,
  217. struct usb_serial_port *port,
  218. struct ktermios *old_termios)
  219. {
  220. struct usb_device *dev = port->serial->dev;
  221. struct qt2_port_private *port_priv;
  222. struct ktermios *termios = &tty->termios;
  223. u16 baud;
  224. unsigned int cflag = termios->c_cflag;
  225. u16 new_lcr = 0;
  226. int status;
  227. port_priv = usb_get_serial_port_data(port);
  228. if (cflag & PARENB) {
  229. if (cflag & PARODD)
  230. new_lcr |= UART_LCR_PARITY;
  231. else
  232. new_lcr |= SERIAL_EVEN_PARITY;
  233. }
  234. switch (cflag & CSIZE) {
  235. case CS5:
  236. new_lcr |= UART_LCR_WLEN5;
  237. break;
  238. case CS6:
  239. new_lcr |= UART_LCR_WLEN6;
  240. break;
  241. case CS7:
  242. new_lcr |= UART_LCR_WLEN7;
  243. break;
  244. default:
  245. case CS8:
  246. new_lcr |= UART_LCR_WLEN8;
  247. break;
  248. }
  249. baud = tty_get_baud_rate(tty);
  250. if (!baud)
  251. baud = 9600;
  252. status = qt2_set_port_config(dev, port_priv->device_port, baud,
  253. new_lcr);
  254. if (status < 0)
  255. dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
  256. __func__, status);
  257. if (cflag & CRTSCTS)
  258. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  259. SERIAL_CRTSCTS,
  260. port_priv->device_port);
  261. else
  262. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  263. 0, port_priv->device_port);
  264. if (status < 0)
  265. dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
  266. __func__, status);
  267. if (I_IXOFF(tty) || I_IXON(tty)) {
  268. u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
  269. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  270. x, port_priv->device_port);
  271. } else
  272. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  273. 0, port_priv->device_port);
  274. if (status < 0)
  275. dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
  276. __func__, status);
  277. }
  278. static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
  279. {
  280. struct usb_serial *serial;
  281. struct qt2_port_private *port_priv;
  282. u8 *data;
  283. u16 device_port;
  284. int status;
  285. unsigned long flags;
  286. device_port = port->port_number;
  287. serial = port->serial;
  288. port_priv = usb_get_serial_port_data(port);
  289. /* set the port to RS232 mode */
  290. status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
  291. QT2_QMCR_RS232, device_port);
  292. if (status < 0) {
  293. dev_err(&port->dev,
  294. "%s failed to set RS232 mode for port %i error %i\n",
  295. __func__, device_port, status);
  296. return status;
  297. }
  298. data = kzalloc(2, GFP_KERNEL);
  299. if (!data)
  300. return -ENOMEM;
  301. /* open the port */
  302. status = usb_control_msg(serial->dev,
  303. usb_rcvctrlpipe(serial->dev, 0),
  304. QT_OPEN_CLOSE_CHANNEL,
  305. 0xc0, 0,
  306. device_port, data, 2, QT2_USB_TIMEOUT);
  307. if (status < 2) {
  308. dev_err(&port->dev, "%s - open port failed %i\n", __func__,
  309. status);
  310. if (status >= 0)
  311. status = -EIO;
  312. kfree(data);
  313. return status;
  314. }
  315. spin_lock_irqsave(&port_priv->lock, flags);
  316. port_priv->shadowLSR = data[0];
  317. port_priv->shadowMSR = data[1];
  318. spin_unlock_irqrestore(&port_priv->lock, flags);
  319. kfree(data);
  320. /* set to default speed and 8bit word size */
  321. status = qt2_set_port_config(serial->dev, device_port,
  322. DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
  323. if (status < 0) {
  324. dev_err(&port->dev, "%s - initial setup failed (%i)\n",
  325. __func__, device_port);
  326. return status;
  327. }
  328. port_priv->device_port = (u8) device_port;
  329. if (tty)
  330. qt2_set_termios(tty, port, &tty->termios);
  331. return 0;
  332. }
  333. static void qt2_close(struct usb_serial_port *port)
  334. {
  335. struct usb_serial *serial;
  336. struct qt2_port_private *port_priv;
  337. int i;
  338. serial = port->serial;
  339. port_priv = usb_get_serial_port_data(port);
  340. usb_kill_urb(port_priv->write_urb);
  341. /* flush the port transmit buffer */
  342. i = usb_control_msg(serial->dev,
  343. usb_sndctrlpipe(serial->dev, 0),
  344. QT2_FLUSH_DEVICE, 0x40, 1,
  345. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  346. if (i < 0)
  347. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  348. __func__, i);
  349. /* flush the port receive buffer */
  350. i = usb_control_msg(serial->dev,
  351. usb_sndctrlpipe(serial->dev, 0),
  352. QT2_FLUSH_DEVICE, 0x40, 0,
  353. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  354. if (i < 0)
  355. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  356. __func__, i);
  357. /* close the port */
  358. i = usb_control_msg(serial->dev,
  359. usb_sndctrlpipe(serial->dev, 0),
  360. QT_OPEN_CLOSE_CHANNEL,
  361. 0x40, 0,
  362. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  363. if (i < 0)
  364. dev_err(&port->dev, "%s - close port failed %i\n",
  365. __func__, i);
  366. }
  367. static void qt2_disconnect(struct usb_serial *serial)
  368. {
  369. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  370. usb_kill_urb(serial_priv->read_urb);
  371. }
  372. static int get_serial_info(struct tty_struct *tty,
  373. struct serial_struct *ss)
  374. {
  375. struct usb_serial_port *port = tty->driver_data;
  376. ss->line = port->minor;
  377. ss->port = 0;
  378. ss->irq = 0;
  379. ss->xmit_fifo_size = port->bulk_out_size;
  380. ss->baud_base = 9600;
  381. ss->close_delay = 5*HZ;
  382. ss->closing_wait = 30*HZ;
  383. return 0;
  384. }
  385. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  386. {
  387. switch (*ch) {
  388. case QT2_LINE_STATUS:
  389. qt2_update_lsr(port, ch + 1);
  390. break;
  391. case QT2_MODEM_STATUS:
  392. qt2_update_msr(port, ch + 1);
  393. break;
  394. }
  395. }
  396. /* not needed, kept to document functionality */
  397. static void qt2_process_xmit_empty(struct usb_serial_port *port,
  398. unsigned char *ch)
  399. {
  400. int bytes_written;
  401. bytes_written = (int)(*ch) + (int)(*(ch + 1) << 4);
  402. }
  403. /* not needed, kept to document functionality */
  404. static void qt2_process_flush(struct usb_serial_port *port, unsigned char *ch)
  405. {
  406. return;
  407. }
  408. static void qt2_process_read_urb(struct urb *urb)
  409. {
  410. struct usb_serial *serial;
  411. struct qt2_serial_private *serial_priv;
  412. struct usb_serial_port *port;
  413. bool escapeflag;
  414. unsigned char *ch;
  415. int i;
  416. unsigned char newport;
  417. int len = urb->actual_length;
  418. if (!len)
  419. return;
  420. ch = urb->transfer_buffer;
  421. serial = urb->context;
  422. serial_priv = usb_get_serial_data(serial);
  423. port = serial->port[serial_priv->current_port];
  424. for (i = 0; i < urb->actual_length; i++) {
  425. ch = (unsigned char *)urb->transfer_buffer + i;
  426. if ((i <= (len - 3)) &&
  427. (*ch == QT2_CONTROL_BYTE) &&
  428. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  429. escapeflag = false;
  430. switch (*(ch + 2)) {
  431. case QT2_LINE_STATUS:
  432. case QT2_MODEM_STATUS:
  433. if (i > (len - 4)) {
  434. dev_warn(&port->dev,
  435. "%s - status message too short\n",
  436. __func__);
  437. break;
  438. }
  439. qt2_process_status(port, ch + 2);
  440. i += 3;
  441. escapeflag = true;
  442. break;
  443. case QT2_XMIT_HOLD:
  444. if (i > (len - 5)) {
  445. dev_warn(&port->dev,
  446. "%s - xmit_empty message too short\n",
  447. __func__);
  448. break;
  449. }
  450. qt2_process_xmit_empty(port, ch + 3);
  451. i += 4;
  452. escapeflag = true;
  453. break;
  454. case QT2_CHANGE_PORT:
  455. if (i > (len - 4)) {
  456. dev_warn(&port->dev,
  457. "%s - change_port message too short\n",
  458. __func__);
  459. break;
  460. }
  461. tty_flip_buffer_push(&port->port);
  462. newport = *(ch + 3);
  463. if (newport > serial->num_ports) {
  464. dev_err(&port->dev,
  465. "%s - port change to invalid port: %i\n",
  466. __func__, newport);
  467. break;
  468. }
  469. serial_priv->current_port = newport;
  470. port = serial->port[serial_priv->current_port];
  471. i += 3;
  472. escapeflag = true;
  473. break;
  474. case QT2_REC_FLUSH:
  475. case QT2_XMIT_FLUSH:
  476. qt2_process_flush(port, ch + 2);
  477. i += 2;
  478. escapeflag = true;
  479. break;
  480. case QT2_CONTROL_ESCAPE:
  481. tty_insert_flip_string(&port->port, ch, 2);
  482. i += 2;
  483. escapeflag = true;
  484. break;
  485. default:
  486. dev_warn(&port->dev,
  487. "%s - unsupported command %i\n",
  488. __func__, *(ch + 2));
  489. break;
  490. }
  491. if (escapeflag)
  492. continue;
  493. }
  494. tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
  495. }
  496. tty_flip_buffer_push(&port->port);
  497. }
  498. static void qt2_write_bulk_callback(struct urb *urb)
  499. {
  500. struct usb_serial_port *port;
  501. struct qt2_port_private *port_priv;
  502. unsigned long flags;
  503. port = urb->context;
  504. port_priv = usb_get_serial_port_data(port);
  505. spin_lock_irqsave(&port_priv->urb_lock, flags);
  506. port_priv->urb_in_use = false;
  507. usb_serial_port_softint(port);
  508. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  509. }
  510. static void qt2_read_bulk_callback(struct urb *urb)
  511. {
  512. struct usb_serial *serial = urb->context;
  513. int status;
  514. if (urb->status) {
  515. dev_warn(&serial->dev->dev,
  516. "%s - non-zero urb status: %i\n", __func__,
  517. urb->status);
  518. return;
  519. }
  520. qt2_process_read_urb(urb);
  521. status = usb_submit_urb(urb, GFP_ATOMIC);
  522. if (status != 0)
  523. dev_err(&serial->dev->dev,
  524. "%s - resubmit read urb failed: %i\n",
  525. __func__, status);
  526. }
  527. static int qt2_setup_urbs(struct usb_serial *serial)
  528. {
  529. struct usb_serial_port *port0;
  530. struct qt2_serial_private *serial_priv;
  531. int status;
  532. port0 = serial->port[0];
  533. serial_priv = usb_get_serial_data(serial);
  534. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  535. if (!serial_priv->read_urb)
  536. return -ENOMEM;
  537. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  538. usb_rcvbulkpipe(serial->dev,
  539. port0->bulk_in_endpointAddress),
  540. serial_priv->read_buffer,
  541. QT2_READ_BUFFER_SIZE,
  542. qt2_read_bulk_callback, serial);
  543. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  544. if (status != 0) {
  545. dev_err(&serial->dev->dev,
  546. "%s - submit read urb failed %i\n", __func__, status);
  547. usb_free_urb(serial_priv->read_urb);
  548. return status;
  549. }
  550. return 0;
  551. }
  552. static int qt2_attach(struct usb_serial *serial)
  553. {
  554. struct qt2_serial_private *serial_priv;
  555. int status;
  556. /* power on unit */
  557. status = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  558. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  559. QT2_USB_TIMEOUT);
  560. if (status < 0) {
  561. dev_err(&serial->dev->dev,
  562. "%s - failed to power on unit: %i\n", __func__, status);
  563. return status;
  564. }
  565. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  566. if (!serial_priv)
  567. return -ENOMEM;
  568. serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
  569. if (!serial_priv->read_buffer) {
  570. status = -ENOMEM;
  571. goto err_buf;
  572. }
  573. usb_set_serial_data(serial, serial_priv);
  574. status = qt2_setup_urbs(serial);
  575. if (status != 0)
  576. goto attach_failed;
  577. return 0;
  578. attach_failed:
  579. kfree(serial_priv->read_buffer);
  580. err_buf:
  581. kfree(serial_priv);
  582. return status;
  583. }
  584. static int qt2_port_probe(struct usb_serial_port *port)
  585. {
  586. struct usb_serial *serial = port->serial;
  587. struct qt2_port_private *port_priv;
  588. u8 bEndpointAddress;
  589. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  590. if (!port_priv)
  591. return -ENOMEM;
  592. spin_lock_init(&port_priv->lock);
  593. spin_lock_init(&port_priv->urb_lock);
  594. port_priv->port = port;
  595. port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
  596. if (!port_priv->write_buffer)
  597. goto err_buf;
  598. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  599. if (!port_priv->write_urb)
  600. goto err_urb;
  601. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  602. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  603. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  604. port_priv->write_buffer,
  605. QT2_WRITE_BUFFER_SIZE,
  606. qt2_write_bulk_callback, port);
  607. usb_set_serial_port_data(port, port_priv);
  608. return 0;
  609. err_urb:
  610. kfree(port_priv->write_buffer);
  611. err_buf:
  612. kfree(port_priv);
  613. return -ENOMEM;
  614. }
  615. static int qt2_port_remove(struct usb_serial_port *port)
  616. {
  617. struct qt2_port_private *port_priv;
  618. port_priv = usb_get_serial_port_data(port);
  619. usb_free_urb(port_priv->write_urb);
  620. kfree(port_priv->write_buffer);
  621. kfree(port_priv);
  622. return 0;
  623. }
  624. static int qt2_tiocmget(struct tty_struct *tty)
  625. {
  626. struct usb_serial_port *port = tty->driver_data;
  627. struct usb_device *dev = port->serial->dev;
  628. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  629. u8 *d;
  630. int r;
  631. d = kzalloc(2, GFP_KERNEL);
  632. if (!d)
  633. return -ENOMEM;
  634. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  635. if (r < 0)
  636. goto mget_out;
  637. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  638. if (r < 0)
  639. goto mget_out;
  640. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  641. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  642. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  643. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  644. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  645. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  646. mget_out:
  647. kfree(d);
  648. return r;
  649. }
  650. static int qt2_tiocmset(struct tty_struct *tty,
  651. unsigned int set, unsigned int clear)
  652. {
  653. struct qt2_port_private *port_priv;
  654. port_priv = usb_get_serial_port_data(tty->driver_data);
  655. return update_mctrl(port_priv, set, clear);
  656. }
  657. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  658. {
  659. struct usb_serial_port *port = tty->driver_data;
  660. struct qt2_port_private *port_priv;
  661. int status;
  662. u16 val;
  663. port_priv = usb_get_serial_port_data(port);
  664. val = (break_state == -1) ? 1 : 0;
  665. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  666. val, port_priv->device_port);
  667. if (status < 0)
  668. dev_warn(&port->dev,
  669. "%s - failed to send control message: %i\n", __func__,
  670. status);
  671. }
  672. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  673. {
  674. struct usb_device *dev = port->serial->dev;
  675. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  676. /* Disable flow control */
  677. if (!on) {
  678. if (qt2_setregister(dev, port_priv->device_port,
  679. UART_MCR, 0) < 0)
  680. dev_warn(&port->dev, "error from flowcontrol urb\n");
  681. }
  682. /* drop RTS and DTR */
  683. if (on)
  684. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  685. else
  686. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  687. }
  688. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  689. {
  690. struct qt2_port_private *port_priv;
  691. u8 newMSR = (u8) *ch;
  692. unsigned long flags;
  693. /* May be called from qt2_process_read_urb() for an unbound port. */
  694. port_priv = usb_get_serial_port_data(port);
  695. if (!port_priv)
  696. return;
  697. spin_lock_irqsave(&port_priv->lock, flags);
  698. port_priv->shadowMSR = newMSR;
  699. spin_unlock_irqrestore(&port_priv->lock, flags);
  700. if (newMSR & UART_MSR_ANY_DELTA) {
  701. /* update input line counters */
  702. if (newMSR & UART_MSR_DCTS)
  703. port->icount.cts++;
  704. if (newMSR & UART_MSR_DDSR)
  705. port->icount.dsr++;
  706. if (newMSR & UART_MSR_DDCD)
  707. port->icount.dcd++;
  708. if (newMSR & UART_MSR_TERI)
  709. port->icount.rng++;
  710. wake_up_interruptible(&port->port.delta_msr_wait);
  711. }
  712. }
  713. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  714. {
  715. struct qt2_port_private *port_priv;
  716. struct async_icount *icount;
  717. unsigned long flags;
  718. u8 newLSR = (u8) *ch;
  719. /* May be called from qt2_process_read_urb() for an unbound port. */
  720. port_priv = usb_get_serial_port_data(port);
  721. if (!port_priv)
  722. return;
  723. if (newLSR & UART_LSR_BI)
  724. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  725. spin_lock_irqsave(&port_priv->lock, flags);
  726. port_priv->shadowLSR = newLSR;
  727. spin_unlock_irqrestore(&port_priv->lock, flags);
  728. icount = &port->icount;
  729. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  730. if (newLSR & UART_LSR_BI)
  731. icount->brk++;
  732. if (newLSR & UART_LSR_OE)
  733. icount->overrun++;
  734. if (newLSR & UART_LSR_PE)
  735. icount->parity++;
  736. if (newLSR & UART_LSR_FE)
  737. icount->frame++;
  738. }
  739. }
  740. static int qt2_write_room(struct tty_struct *tty)
  741. {
  742. struct usb_serial_port *port = tty->driver_data;
  743. struct qt2_port_private *port_priv;
  744. unsigned long flags = 0;
  745. int r;
  746. port_priv = usb_get_serial_port_data(port);
  747. spin_lock_irqsave(&port_priv->urb_lock, flags);
  748. if (port_priv->urb_in_use)
  749. r = 0;
  750. else
  751. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  752. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  753. return r;
  754. }
  755. static int qt2_write(struct tty_struct *tty,
  756. struct usb_serial_port *port,
  757. const unsigned char *buf, int count)
  758. {
  759. struct qt2_port_private *port_priv;
  760. struct urb *write_urb;
  761. unsigned char *data;
  762. unsigned long flags;
  763. int status;
  764. int bytes_out = 0;
  765. port_priv = usb_get_serial_port_data(port);
  766. if (port_priv->write_urb == NULL) {
  767. dev_err(&port->dev, "%s - no output urb\n", __func__);
  768. return 0;
  769. }
  770. write_urb = port_priv->write_urb;
  771. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  772. data = write_urb->transfer_buffer;
  773. spin_lock_irqsave(&port_priv->urb_lock, flags);
  774. if (port_priv->urb_in_use) {
  775. dev_err(&port->dev, "qt2_write - urb is in use\n");
  776. goto write_out;
  777. }
  778. *data++ = QT2_CONTROL_BYTE;
  779. *data++ = QT2_CONTROL_BYTE;
  780. *data++ = port_priv->device_port;
  781. put_unaligned_le16(count, data);
  782. data += 2;
  783. memcpy(data, buf, count);
  784. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  785. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  786. if (status == 0) {
  787. port_priv->urb_in_use = true;
  788. bytes_out += count;
  789. }
  790. write_out:
  791. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  792. return bytes_out;
  793. }
  794. static struct usb_serial_driver qt2_device = {
  795. .driver = {
  796. .owner = THIS_MODULE,
  797. .name = "quatech-serial",
  798. },
  799. .description = DRIVER_DESC,
  800. .id_table = id_table,
  801. .open = qt2_open,
  802. .close = qt2_close,
  803. .write = qt2_write,
  804. .write_room = qt2_write_room,
  805. .calc_num_ports = qt2_calc_num_ports,
  806. .attach = qt2_attach,
  807. .release = qt2_release,
  808. .disconnect = qt2_disconnect,
  809. .port_probe = qt2_port_probe,
  810. .port_remove = qt2_port_remove,
  811. .dtr_rts = qt2_dtr_rts,
  812. .break_ctl = qt2_break_ctl,
  813. .tiocmget = qt2_tiocmget,
  814. .tiocmset = qt2_tiocmset,
  815. .tiocmiwait = usb_serial_generic_tiocmiwait,
  816. .get_icount = usb_serial_generic_get_icount,
  817. .get_serial = get_serial_info,
  818. .set_termios = qt2_set_termios,
  819. };
  820. static struct usb_serial_driver *const serial_drivers[] = {
  821. &qt2_device, NULL
  822. };
  823. module_usb_serial_driver(serial_drivers, id_table);
  824. MODULE_DESCRIPTION(DRIVER_DESC);
  825. MODULE_LICENSE("GPL v2");