quatech2.c 24 KB

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