quatech2.c 28 KB

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