quatech2.c 25 KB

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