quatech2.c 28 KB

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