quatech2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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 <linux/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_getregister(struct usb_device *dev,
  149. u8 uart,
  150. u8 reg,
  151. u8 *data)
  152. {
  153. int ret;
  154. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  155. QT_SET_GET_REGISTER, 0xc0, reg,
  156. uart, data, sizeof(*data), QT2_USB_TIMEOUT);
  157. if (ret < (int)sizeof(*data)) {
  158. if (ret >= 0)
  159. ret = -EIO;
  160. }
  161. return ret;
  162. }
  163. static inline int qt2_setregister(struct usb_device *dev,
  164. u8 uart, u8 reg, u16 data)
  165. {
  166. u16 value = (data << 8) | reg;
  167. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  168. QT_SET_GET_REGISTER, 0x40, value, uart,
  169. NULL, 0, QT2_USB_TIMEOUT);
  170. }
  171. static inline int update_mctrl(struct qt2_port_private *port_priv,
  172. unsigned int set, unsigned int clear)
  173. {
  174. struct usb_serial_port *port = port_priv->port;
  175. struct usb_device *dev = port->serial->dev;
  176. unsigned urb_value;
  177. int status;
  178. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
  179. dev_dbg(&port->dev,
  180. "update_mctrl - DTR|RTS not being set|cleared\n");
  181. return 0; /* no change */
  182. }
  183. clear &= ~set; /* 'set' takes precedence over 'clear' */
  184. urb_value = 0;
  185. if (set & TIOCM_DTR)
  186. urb_value |= UART_MCR_DTR;
  187. if (set & TIOCM_RTS)
  188. urb_value |= UART_MCR_RTS;
  189. status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
  190. urb_value);
  191. if (status < 0)
  192. dev_err(&port->dev,
  193. "update_mctrl - Error from MODEM_CTRL urb: %i\n",
  194. status);
  195. return status;
  196. }
  197. static int qt2_calc_num_ports(struct usb_serial *serial,
  198. struct usb_serial_endpoints *epds)
  199. {
  200. struct qt2_device_detail d;
  201. int i;
  202. for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
  203. if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
  204. return d.num_ports;
  205. }
  206. /* we didn't recognize the device */
  207. dev_err(&serial->dev->dev,
  208. "don't know the number of ports, assuming 1\n");
  209. return 1;
  210. }
  211. static void qt2_set_termios(struct tty_struct *tty,
  212. struct usb_serial_port *port,
  213. const struct ktermios *old_termios)
  214. {
  215. struct usb_device *dev = port->serial->dev;
  216. struct qt2_port_private *port_priv;
  217. struct ktermios *termios = &tty->termios;
  218. u16 baud;
  219. unsigned int cflag = termios->c_cflag;
  220. u16 new_lcr = 0;
  221. int status;
  222. port_priv = usb_get_serial_port_data(port);
  223. if (cflag & PARENB) {
  224. if (cflag & PARODD)
  225. new_lcr |= UART_LCR_PARITY;
  226. else
  227. new_lcr |= SERIAL_EVEN_PARITY;
  228. }
  229. new_lcr |= UART_LCR_WLEN(tty_get_char_size(cflag));
  230. baud = tty_get_baud_rate(tty);
  231. if (!baud)
  232. baud = 9600;
  233. status = qt2_set_port_config(dev, port_priv->device_port, baud,
  234. new_lcr);
  235. if (status < 0)
  236. dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
  237. __func__, status);
  238. if (cflag & CRTSCTS)
  239. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  240. SERIAL_CRTSCTS,
  241. port_priv->device_port);
  242. else
  243. status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  244. 0, port_priv->device_port);
  245. if (status < 0)
  246. dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
  247. __func__, status);
  248. if (I_IXOFF(tty) || I_IXON(tty)) {
  249. u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
  250. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  251. x, port_priv->device_port);
  252. } else
  253. status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  254. 0, port_priv->device_port);
  255. if (status < 0)
  256. dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
  257. __func__, status);
  258. }
  259. static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
  260. {
  261. struct usb_serial *serial;
  262. struct qt2_port_private *port_priv;
  263. u8 *data;
  264. u16 device_port;
  265. int status;
  266. unsigned long flags;
  267. device_port = port->port_number;
  268. serial = port->serial;
  269. port_priv = usb_get_serial_port_data(port);
  270. /* set the port to RS232 mode */
  271. status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
  272. QT2_QMCR_RS232, device_port);
  273. if (status < 0) {
  274. dev_err(&port->dev,
  275. "%s failed to set RS232 mode for port %i error %i\n",
  276. __func__, device_port, status);
  277. return status;
  278. }
  279. data = kzalloc(2, GFP_KERNEL);
  280. if (!data)
  281. return -ENOMEM;
  282. /* open the port */
  283. status = usb_control_msg(serial->dev,
  284. usb_rcvctrlpipe(serial->dev, 0),
  285. QT_OPEN_CLOSE_CHANNEL,
  286. 0xc0, 0,
  287. device_port, data, 2, QT2_USB_TIMEOUT);
  288. if (status < 2) {
  289. dev_err(&port->dev, "%s - open port failed %i\n", __func__,
  290. status);
  291. if (status >= 0)
  292. status = -EIO;
  293. kfree(data);
  294. return status;
  295. }
  296. spin_lock_irqsave(&port_priv->lock, flags);
  297. port_priv->shadowLSR = data[0];
  298. port_priv->shadowMSR = data[1];
  299. spin_unlock_irqrestore(&port_priv->lock, flags);
  300. kfree(data);
  301. /* set to default speed and 8bit word size */
  302. status = qt2_set_port_config(serial->dev, device_port,
  303. DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
  304. if (status < 0) {
  305. dev_err(&port->dev, "%s - initial setup failed (%i)\n",
  306. __func__, device_port);
  307. return status;
  308. }
  309. port_priv->device_port = (u8) device_port;
  310. if (tty)
  311. qt2_set_termios(tty, port, &tty->termios);
  312. return 0;
  313. }
  314. static void qt2_close(struct usb_serial_port *port)
  315. {
  316. struct usb_serial *serial;
  317. struct qt2_port_private *port_priv;
  318. int i;
  319. serial = port->serial;
  320. port_priv = usb_get_serial_port_data(port);
  321. usb_kill_urb(port_priv->write_urb);
  322. /* flush the port transmit buffer */
  323. i = usb_control_msg(serial->dev,
  324. usb_sndctrlpipe(serial->dev, 0),
  325. QT2_FLUSH_DEVICE, 0x40, 1,
  326. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  327. if (i < 0)
  328. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  329. __func__, i);
  330. /* flush the port receive buffer */
  331. i = usb_control_msg(serial->dev,
  332. usb_sndctrlpipe(serial->dev, 0),
  333. QT2_FLUSH_DEVICE, 0x40, 0,
  334. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  335. if (i < 0)
  336. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  337. __func__, i);
  338. /* close the port */
  339. i = usb_control_msg(serial->dev,
  340. usb_sndctrlpipe(serial->dev, 0),
  341. QT_OPEN_CLOSE_CHANNEL,
  342. 0x40, 0,
  343. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  344. if (i < 0)
  345. dev_err(&port->dev, "%s - close port failed %i\n",
  346. __func__, i);
  347. }
  348. static void qt2_disconnect(struct usb_serial *serial)
  349. {
  350. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  351. usb_kill_urb(serial_priv->read_urb);
  352. }
  353. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  354. {
  355. switch (*ch) {
  356. case QT2_LINE_STATUS:
  357. qt2_update_lsr(port, ch + 1);
  358. break;
  359. case QT2_MODEM_STATUS:
  360. qt2_update_msr(port, ch + 1);
  361. break;
  362. }
  363. }
  364. static void qt2_process_read_urb(struct urb *urb)
  365. {
  366. struct usb_serial *serial;
  367. struct qt2_serial_private *serial_priv;
  368. struct usb_serial_port *port;
  369. bool escapeflag;
  370. unsigned char *ch;
  371. int i;
  372. unsigned char newport;
  373. int len = urb->actual_length;
  374. if (!len)
  375. return;
  376. ch = urb->transfer_buffer;
  377. serial = urb->context;
  378. serial_priv = usb_get_serial_data(serial);
  379. port = serial->port[serial_priv->current_port];
  380. for (i = 0; i < urb->actual_length; i++) {
  381. ch = (unsigned char *)urb->transfer_buffer + i;
  382. if ((i <= (len - 3)) &&
  383. (*ch == QT2_CONTROL_BYTE) &&
  384. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  385. escapeflag = false;
  386. switch (*(ch + 2)) {
  387. case QT2_LINE_STATUS:
  388. case QT2_MODEM_STATUS:
  389. if (i > (len - 4)) {
  390. dev_warn(&port->dev,
  391. "%s - status message too short\n",
  392. __func__);
  393. break;
  394. }
  395. qt2_process_status(port, ch + 2);
  396. i += 3;
  397. escapeflag = true;
  398. break;
  399. case QT2_XMIT_HOLD:
  400. if (i > (len - 5)) {
  401. dev_warn(&port->dev,
  402. "%s - xmit_empty message too short\n",
  403. __func__);
  404. break;
  405. }
  406. /* bytes_written = (ch[1] << 4) + ch[0]; */
  407. i += 4;
  408. escapeflag = true;
  409. break;
  410. case QT2_CHANGE_PORT:
  411. if (i > (len - 4)) {
  412. dev_warn(&port->dev,
  413. "%s - change_port message too short\n",
  414. __func__);
  415. break;
  416. }
  417. tty_flip_buffer_push(&port->port);
  418. newport = *(ch + 3);
  419. if (newport >= serial->num_ports) {
  420. dev_err(&port->dev,
  421. "%s - port change to invalid port: %i\n",
  422. __func__, newport);
  423. break;
  424. }
  425. serial_priv->current_port = newport;
  426. port = serial->port[serial_priv->current_port];
  427. i += 3;
  428. escapeflag = true;
  429. break;
  430. case QT2_REC_FLUSH:
  431. case QT2_XMIT_FLUSH:
  432. i += 2;
  433. escapeflag = true;
  434. break;
  435. case QT2_CONTROL_ESCAPE:
  436. tty_insert_flip_string(&port->port, ch, 2);
  437. i += 2;
  438. escapeflag = true;
  439. break;
  440. default:
  441. dev_warn(&port->dev,
  442. "%s - unsupported command %i\n",
  443. __func__, *(ch + 2));
  444. break;
  445. }
  446. if (escapeflag)
  447. continue;
  448. }
  449. tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
  450. }
  451. tty_flip_buffer_push(&port->port);
  452. }
  453. static void qt2_write_bulk_callback(struct urb *urb)
  454. {
  455. struct usb_serial_port *port;
  456. struct qt2_port_private *port_priv;
  457. unsigned long flags;
  458. port = urb->context;
  459. port_priv = usb_get_serial_port_data(port);
  460. spin_lock_irqsave(&port_priv->urb_lock, flags);
  461. port_priv->urb_in_use = false;
  462. usb_serial_port_softint(port);
  463. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  464. }
  465. static void qt2_read_bulk_callback(struct urb *urb)
  466. {
  467. struct usb_serial *serial = urb->context;
  468. int status;
  469. if (urb->status) {
  470. dev_warn(&serial->dev->dev,
  471. "%s - non-zero urb status: %i\n", __func__,
  472. urb->status);
  473. return;
  474. }
  475. qt2_process_read_urb(urb);
  476. status = usb_submit_urb(urb, GFP_ATOMIC);
  477. if (status != 0)
  478. dev_err(&serial->dev->dev,
  479. "%s - resubmit read urb failed: %i\n",
  480. __func__, status);
  481. }
  482. static int qt2_setup_urbs(struct usb_serial *serial)
  483. {
  484. struct usb_serial_port *port0;
  485. struct qt2_serial_private *serial_priv;
  486. int status;
  487. port0 = serial->port[0];
  488. serial_priv = usb_get_serial_data(serial);
  489. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  490. if (!serial_priv->read_urb)
  491. return -ENOMEM;
  492. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  493. usb_rcvbulkpipe(serial->dev,
  494. port0->bulk_in_endpointAddress),
  495. serial_priv->read_buffer,
  496. QT2_READ_BUFFER_SIZE,
  497. qt2_read_bulk_callback, serial);
  498. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  499. if (status != 0) {
  500. dev_err(&serial->dev->dev,
  501. "%s - submit read urb failed %i\n", __func__, status);
  502. usb_free_urb(serial_priv->read_urb);
  503. return status;
  504. }
  505. return 0;
  506. }
  507. static int qt2_attach(struct usb_serial *serial)
  508. {
  509. struct qt2_serial_private *serial_priv;
  510. int status;
  511. /* power on unit */
  512. status = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  513. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  514. QT2_USB_TIMEOUT);
  515. if (status < 0) {
  516. dev_err(&serial->dev->dev,
  517. "%s - failed to power on unit: %i\n", __func__, status);
  518. return status;
  519. }
  520. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  521. if (!serial_priv)
  522. return -ENOMEM;
  523. serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
  524. if (!serial_priv->read_buffer) {
  525. status = -ENOMEM;
  526. goto err_buf;
  527. }
  528. usb_set_serial_data(serial, serial_priv);
  529. status = qt2_setup_urbs(serial);
  530. if (status != 0)
  531. goto attach_failed;
  532. return 0;
  533. attach_failed:
  534. kfree(serial_priv->read_buffer);
  535. err_buf:
  536. kfree(serial_priv);
  537. return status;
  538. }
  539. static int qt2_port_probe(struct usb_serial_port *port)
  540. {
  541. struct usb_serial *serial = port->serial;
  542. struct qt2_port_private *port_priv;
  543. u8 bEndpointAddress;
  544. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  545. if (!port_priv)
  546. return -ENOMEM;
  547. spin_lock_init(&port_priv->lock);
  548. spin_lock_init(&port_priv->urb_lock);
  549. port_priv->port = port;
  550. port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
  551. if (!port_priv->write_buffer)
  552. goto err_buf;
  553. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  554. if (!port_priv->write_urb)
  555. goto err_urb;
  556. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  557. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  558. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  559. port_priv->write_buffer,
  560. QT2_WRITE_BUFFER_SIZE,
  561. qt2_write_bulk_callback, port);
  562. usb_set_serial_port_data(port, port_priv);
  563. return 0;
  564. err_urb:
  565. kfree(port_priv->write_buffer);
  566. err_buf:
  567. kfree(port_priv);
  568. return -ENOMEM;
  569. }
  570. static void qt2_port_remove(struct usb_serial_port *port)
  571. {
  572. struct qt2_port_private *port_priv;
  573. port_priv = usb_get_serial_port_data(port);
  574. usb_free_urb(port_priv->write_urb);
  575. kfree(port_priv->write_buffer);
  576. kfree(port_priv);
  577. }
  578. static int qt2_tiocmget(struct tty_struct *tty)
  579. {
  580. struct usb_serial_port *port = tty->driver_data;
  581. struct usb_device *dev = port->serial->dev;
  582. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  583. u8 *d;
  584. int r;
  585. d = kzalloc(2, GFP_KERNEL);
  586. if (!d)
  587. return -ENOMEM;
  588. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  589. if (r < 0)
  590. goto mget_out;
  591. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  592. if (r < 0)
  593. goto mget_out;
  594. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  595. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  596. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  597. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  598. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  599. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  600. mget_out:
  601. kfree(d);
  602. return r;
  603. }
  604. static int qt2_tiocmset(struct tty_struct *tty,
  605. unsigned int set, unsigned int clear)
  606. {
  607. struct qt2_port_private *port_priv;
  608. port_priv = usb_get_serial_port_data(tty->driver_data);
  609. return update_mctrl(port_priv, set, clear);
  610. }
  611. static int qt2_break_ctl(struct tty_struct *tty, int break_state)
  612. {
  613. struct usb_serial_port *port = tty->driver_data;
  614. struct qt2_port_private *port_priv;
  615. int status;
  616. u16 val;
  617. port_priv = usb_get_serial_port_data(port);
  618. val = (break_state == -1) ? 1 : 0;
  619. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  620. val, port_priv->device_port);
  621. if (status < 0) {
  622. dev_warn(&port->dev,
  623. "%s - failed to send control message: %i\n", __func__,
  624. status);
  625. return status;
  626. }
  627. return 0;
  628. }
  629. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  630. {
  631. struct usb_device *dev = port->serial->dev;
  632. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  633. /* Disable flow control */
  634. if (!on) {
  635. if (qt2_setregister(dev, port_priv->device_port,
  636. UART_MCR, 0) < 0)
  637. dev_warn(&port->dev, "error from flowcontrol urb\n");
  638. }
  639. /* drop RTS and DTR */
  640. if (on)
  641. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  642. else
  643. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  644. }
  645. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  646. {
  647. struct qt2_port_private *port_priv;
  648. u8 newMSR = (u8) *ch;
  649. unsigned long flags;
  650. /* May be called from qt2_process_read_urb() for an unbound port. */
  651. port_priv = usb_get_serial_port_data(port);
  652. if (!port_priv)
  653. return;
  654. spin_lock_irqsave(&port_priv->lock, flags);
  655. port_priv->shadowMSR = newMSR;
  656. spin_unlock_irqrestore(&port_priv->lock, flags);
  657. if (newMSR & UART_MSR_ANY_DELTA) {
  658. /* update input line counters */
  659. if (newMSR & UART_MSR_DCTS)
  660. port->icount.cts++;
  661. if (newMSR & UART_MSR_DDSR)
  662. port->icount.dsr++;
  663. if (newMSR & UART_MSR_DDCD)
  664. port->icount.dcd++;
  665. if (newMSR & UART_MSR_TERI)
  666. port->icount.rng++;
  667. wake_up_interruptible(&port->port.delta_msr_wait);
  668. }
  669. }
  670. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  671. {
  672. struct qt2_port_private *port_priv;
  673. struct async_icount *icount;
  674. unsigned long flags;
  675. u8 newLSR = (u8) *ch;
  676. /* May be called from qt2_process_read_urb() for an unbound port. */
  677. port_priv = usb_get_serial_port_data(port);
  678. if (!port_priv)
  679. return;
  680. if (newLSR & UART_LSR_BI)
  681. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  682. spin_lock_irqsave(&port_priv->lock, flags);
  683. port_priv->shadowLSR = newLSR;
  684. spin_unlock_irqrestore(&port_priv->lock, flags);
  685. icount = &port->icount;
  686. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  687. if (newLSR & UART_LSR_BI)
  688. icount->brk++;
  689. if (newLSR & UART_LSR_OE)
  690. icount->overrun++;
  691. if (newLSR & UART_LSR_PE)
  692. icount->parity++;
  693. if (newLSR & UART_LSR_FE)
  694. icount->frame++;
  695. }
  696. }
  697. static unsigned int qt2_write_room(struct tty_struct *tty)
  698. {
  699. struct usb_serial_port *port = tty->driver_data;
  700. struct qt2_port_private *port_priv;
  701. unsigned long flags;
  702. unsigned int r;
  703. port_priv = usb_get_serial_port_data(port);
  704. spin_lock_irqsave(&port_priv->urb_lock, flags);
  705. if (port_priv->urb_in_use)
  706. r = 0;
  707. else
  708. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  709. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  710. return r;
  711. }
  712. static int qt2_write(struct tty_struct *tty,
  713. struct usb_serial_port *port,
  714. const unsigned char *buf, int count)
  715. {
  716. struct qt2_port_private *port_priv;
  717. struct urb *write_urb;
  718. unsigned char *data;
  719. unsigned long flags;
  720. int status;
  721. int bytes_out = 0;
  722. port_priv = usb_get_serial_port_data(port);
  723. if (port_priv->write_urb == NULL) {
  724. dev_err(&port->dev, "%s - no output urb\n", __func__);
  725. return 0;
  726. }
  727. write_urb = port_priv->write_urb;
  728. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  729. data = write_urb->transfer_buffer;
  730. spin_lock_irqsave(&port_priv->urb_lock, flags);
  731. if (port_priv->urb_in_use) {
  732. dev_err(&port->dev, "qt2_write - urb is in use\n");
  733. goto write_out;
  734. }
  735. *data++ = QT2_CONTROL_BYTE;
  736. *data++ = QT2_CONTROL_BYTE;
  737. *data++ = port_priv->device_port;
  738. put_unaligned_le16(count, data);
  739. data += 2;
  740. memcpy(data, buf, count);
  741. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  742. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  743. if (status == 0) {
  744. port_priv->urb_in_use = true;
  745. bytes_out += count;
  746. }
  747. write_out:
  748. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  749. return bytes_out;
  750. }
  751. static struct usb_serial_driver qt2_device = {
  752. .driver = {
  753. .name = "quatech-serial",
  754. },
  755. .description = DRIVER_DESC,
  756. .id_table = id_table,
  757. .open = qt2_open,
  758. .close = qt2_close,
  759. .write = qt2_write,
  760. .write_room = qt2_write_room,
  761. .calc_num_ports = qt2_calc_num_ports,
  762. .attach = qt2_attach,
  763. .release = qt2_release,
  764. .disconnect = qt2_disconnect,
  765. .port_probe = qt2_port_probe,
  766. .port_remove = qt2_port_remove,
  767. .dtr_rts = qt2_dtr_rts,
  768. .break_ctl = qt2_break_ctl,
  769. .tiocmget = qt2_tiocmget,
  770. .tiocmset = qt2_tiocmset,
  771. .tiocmiwait = usb_serial_generic_tiocmiwait,
  772. .get_icount = usb_serial_generic_get_icount,
  773. .set_termios = qt2_set_termios,
  774. };
  775. static struct usb_serial_driver *const serial_drivers[] = {
  776. &qt2_device, NULL
  777. };
  778. module_usb_serial_driver(serial_drivers, id_table);
  779. MODULE_DESCRIPTION(DRIVER_DESC);
  780. MODULE_LICENSE("GPL v2");