quatech2.c 25 KB

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