quatech2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. if (port_priv->write_urb->status == -EINPROGRESS)
  350. usb_kill_urb(port_priv->write_urb);
  351. port_priv->urb_in_use = false;
  352. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  353. /* flush the port transmit buffer */
  354. i = usb_control_msg(serial->dev,
  355. usb_rcvctrlpipe(serial->dev, 0),
  356. QT2_FLUSH_DEVICE, 0x40, 1,
  357. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  358. if (i < 0)
  359. dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
  360. __func__, i);
  361. /* flush the port receive buffer */
  362. i = usb_control_msg(serial->dev,
  363. usb_rcvctrlpipe(serial->dev, 0),
  364. QT2_FLUSH_DEVICE, 0x40, 0,
  365. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  366. if (i < 0)
  367. dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
  368. __func__, i);
  369. /* close the port */
  370. i = usb_control_msg(serial->dev,
  371. usb_sndctrlpipe(serial->dev, 0),
  372. QT_OPEN_CLOSE_CHANNEL,
  373. 0x40, 0,
  374. port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
  375. if (i < 0)
  376. dev_err(&port->dev, "%s - close port failed %i\n",
  377. __func__, i);
  378. }
  379. static void qt2_disconnect(struct usb_serial *serial)
  380. {
  381. struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
  382. if (serial_priv->read_urb->status == -EINPROGRESS)
  383. usb_kill_urb(serial_priv->read_urb);
  384. }
  385. static int get_serial_info(struct usb_serial_port *port,
  386. struct serial_struct __user *retinfo)
  387. {
  388. struct serial_struct tmp;
  389. if (!retinfo)
  390. return -EFAULT;
  391. memset(&tmp, 0, sizeof(tmp));
  392. tmp.line = port->serial->minor;
  393. tmp.port = 0;
  394. tmp.irq = 0;
  395. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  396. tmp.xmit_fifo_size = port->bulk_out_size;
  397. tmp.baud_base = 9600;
  398. tmp.close_delay = 5*HZ;
  399. tmp.closing_wait = 30*HZ;
  400. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  401. return -EFAULT;
  402. return 0;
  403. }
  404. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  405. {
  406. struct qt2_port_private *priv = usb_get_serial_port_data(port);
  407. struct async_icount prev, cur;
  408. unsigned long flags;
  409. spin_lock_irqsave(&priv->lock, flags);
  410. prev = priv->icount;
  411. spin_unlock_irqrestore(&priv->lock, flags);
  412. while (1) {
  413. wait_event_interruptible(priv->delta_msr_wait,
  414. ((priv->icount.rng != prev.rng) ||
  415. (priv->icount.dsr != prev.dsr) ||
  416. (priv->icount.dcd != prev.dcd) ||
  417. (priv->icount.cts != prev.cts)));
  418. if (signal_pending(current))
  419. return -ERESTARTSYS;
  420. spin_lock_irqsave(&priv->lock, flags);
  421. cur = priv->icount;
  422. spin_unlock_irqrestore(&priv->lock, flags);
  423. if ((prev.rng == cur.rng) &&
  424. (prev.dsr == cur.dsr) &&
  425. (prev.dcd == cur.dcd) &&
  426. (prev.cts == cur.cts))
  427. return -EIO;
  428. if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
  429. (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
  430. (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
  431. (arg & TIOCM_CTS && (prev.cts != cur.cts)))
  432. return 0;
  433. }
  434. return 0;
  435. }
  436. static int qt2_get_icount(struct tty_struct *tty,
  437. struct serial_icounter_struct *icount)
  438. {
  439. struct usb_serial_port *port = tty->driver_data;
  440. struct qt2_port_private *priv = usb_get_serial_port_data(port);
  441. struct async_icount cnow = priv->icount;
  442. icount->cts = cnow.cts;
  443. icount->dsr = cnow.dsr;
  444. icount->rng = cnow.rng;
  445. icount->dcd = cnow.dcd;
  446. icount->rx = cnow.rx;
  447. icount->tx = cnow.tx;
  448. icount->frame = cnow.frame;
  449. icount->overrun = cnow.overrun;
  450. icount->parity = cnow.parity;
  451. icount->brk = cnow.brk;
  452. icount->buf_overrun = cnow.buf_overrun;
  453. return 0;
  454. }
  455. static int qt2_ioctl(struct tty_struct *tty,
  456. unsigned int cmd, unsigned long arg)
  457. {
  458. struct usb_serial_port *port = tty->driver_data;
  459. switch (cmd) {
  460. case TIOCGSERIAL:
  461. return get_serial_info(port,
  462. (struct serial_struct __user *)arg);
  463. case TIOCMIWAIT:
  464. return wait_modem_info(port, arg);
  465. default:
  466. break;
  467. }
  468. return -ENOIOCTLCMD;
  469. }
  470. static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
  471. {
  472. switch (*ch) {
  473. case QT2_LINE_STATUS:
  474. qt2_update_lsr(port, ch + 1);
  475. break;
  476. case QT2_MODEM_STATUS:
  477. qt2_update_msr(port, ch + 1);
  478. break;
  479. }
  480. }
  481. /* not needed, kept to document functionality */
  482. static void qt2_process_xmit_empty(struct usb_serial_port *port,
  483. unsigned char *ch)
  484. {
  485. int bytes_written;
  486. bytes_written = (int)(*ch) + (int)(*(ch + 1) << 4);
  487. }
  488. /* not needed, kept to document functionality */
  489. static void qt2_process_flush(struct usb_serial_port *port, unsigned char *ch)
  490. {
  491. return;
  492. }
  493. void qt2_process_read_urb(struct urb *urb)
  494. {
  495. struct usb_serial *serial;
  496. struct qt2_serial_private *serial_priv;
  497. struct usb_serial_port *port;
  498. struct qt2_port_private *port_priv;
  499. struct tty_struct *tty;
  500. bool escapeflag;
  501. unsigned char *ch;
  502. int i;
  503. unsigned char newport;
  504. int len = urb->actual_length;
  505. if (!len)
  506. return;
  507. ch = urb->transfer_buffer;
  508. tty = NULL;
  509. serial = urb->context;
  510. serial_priv = usb_get_serial_data(serial);
  511. port = serial->port[serial_priv->current_port];
  512. port_priv = usb_get_serial_port_data(port);
  513. if (port_priv->is_open)
  514. tty = tty_port_tty_get(&port->port);
  515. for (i = 0; i < urb->actual_length; i++) {
  516. ch = (unsigned char *)urb->transfer_buffer + i;
  517. if ((i <= (len - 3)) &&
  518. (*ch == QT2_CONTROL_BYTE) &&
  519. (*(ch + 1) == QT2_CONTROL_BYTE)) {
  520. escapeflag = false;
  521. switch (*(ch + 2)) {
  522. case QT2_LINE_STATUS:
  523. case QT2_MODEM_STATUS:
  524. if (i > (len - 4)) {
  525. dev_warn(&port->dev,
  526. "%s - status message too short\n",
  527. __func__);
  528. break;
  529. }
  530. qt2_process_status(port, ch + 2);
  531. i += 3;
  532. escapeflag = true;
  533. break;
  534. case QT2_XMIT_HOLD:
  535. if (i > (len - 5)) {
  536. dev_warn(&port->dev,
  537. "%s - xmit_empty message too short\n",
  538. __func__);
  539. break;
  540. }
  541. qt2_process_xmit_empty(port, ch + 3);
  542. i += 4;
  543. escapeflag = true;
  544. break;
  545. case QT2_CHANGE_PORT:
  546. if (i > (len - 4)) {
  547. dev_warn(&port->dev,
  548. "%s - change_port message too short\n",
  549. __func__);
  550. break;
  551. }
  552. if (tty) {
  553. tty_flip_buffer_push(tty);
  554. tty_kref_put(tty);
  555. }
  556. newport = *(ch + 3);
  557. if (newport > serial->num_ports) {
  558. dev_err(&port->dev,
  559. "%s - port change to invalid port: %i\n",
  560. __func__, newport);
  561. break;
  562. }
  563. serial_priv->current_port = newport;
  564. port = serial->port[serial_priv->current_port];
  565. port_priv = usb_get_serial_port_data(port);
  566. if (port_priv->is_open)
  567. tty = tty_port_tty_get(&port->port);
  568. else
  569. tty = NULL;
  570. i += 3;
  571. escapeflag = true;
  572. break;
  573. case QT2_REC_FLUSH:
  574. case QT2_XMIT_FLUSH:
  575. qt2_process_flush(port, ch + 2);
  576. i += 2;
  577. escapeflag = true;
  578. break;
  579. case QT2_CONTROL_ESCAPE:
  580. tty_buffer_request_room(tty, 2);
  581. tty_insert_flip_string(tty, ch, 2);
  582. i += 2;
  583. escapeflag = true;
  584. break;
  585. default:
  586. dev_warn(&port->dev,
  587. "%s - unsupported command %i\n",
  588. __func__, *(ch + 2));
  589. break;
  590. }
  591. if (escapeflag)
  592. continue;
  593. }
  594. if (tty) {
  595. tty_buffer_request_room(tty, 1);
  596. tty_insert_flip_string(tty, ch, 1);
  597. }
  598. }
  599. if (tty) {
  600. tty_flip_buffer_push(tty);
  601. tty_kref_put(tty);
  602. }
  603. }
  604. static void qt2_write_bulk_callback(struct urb *urb)
  605. {
  606. struct usb_serial_port *port;
  607. struct qt2_port_private *port_priv;
  608. port = urb->context;
  609. port_priv = usb_get_serial_port_data(port);
  610. spin_lock(&port_priv->urb_lock);
  611. port_priv->urb_in_use = false;
  612. usb_serial_port_softint(port);
  613. spin_unlock(&port_priv->urb_lock);
  614. }
  615. static void qt2_read_bulk_callback(struct urb *urb)
  616. {
  617. struct usb_serial *serial = urb->context;
  618. int status;
  619. if (urb->status) {
  620. dev_warn(&serial->dev->dev,
  621. "%s - non-zero urb status: %i\n", __func__,
  622. urb->status);
  623. return;
  624. }
  625. qt2_process_read_urb(urb);
  626. status = usb_submit_urb(urb, GFP_ATOMIC);
  627. if (status != 0)
  628. dev_err(&serial->dev->dev,
  629. "%s - resubmit read urb failed: %i\n",
  630. __func__, status);
  631. }
  632. static int qt2_setup_urbs(struct usb_serial *serial)
  633. {
  634. struct usb_serial_port *port0;
  635. struct qt2_serial_private *serial_priv;
  636. int status;
  637. port0 = serial->port[0];
  638. serial_priv = usb_get_serial_data(serial);
  639. serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  640. if (!serial_priv->read_urb) {
  641. dev_err(&serial->dev->dev, "No free urbs available\n");
  642. return -ENOMEM;
  643. }
  644. usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
  645. usb_rcvbulkpipe(serial->dev,
  646. port0->bulk_in_endpointAddress),
  647. serial_priv->read_buffer,
  648. sizeof(serial_priv->read_buffer),
  649. qt2_read_bulk_callback, serial);
  650. status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
  651. if (status != 0) {
  652. dev_err(&serial->dev->dev,
  653. "%s - submit read urb failed %i\n", __func__, status);
  654. usb_free_urb(serial_priv->read_urb);
  655. return status;
  656. }
  657. return 0;
  658. }
  659. static int qt2_attach(struct usb_serial *serial)
  660. {
  661. struct qt2_serial_private *serial_priv;
  662. int status;
  663. /* power on unit */
  664. status = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  665. 0xc2, 0x40, 0x8000, 0, NULL, 0,
  666. QT2_USB_TIMEOUT);
  667. if (status < 0) {
  668. dev_err(&serial->dev->dev,
  669. "%s - failed to power on unit: %i\n", __func__, status);
  670. return status;
  671. }
  672. serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
  673. if (!serial_priv) {
  674. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  675. return -ENOMEM;
  676. }
  677. usb_set_serial_data(serial, serial_priv);
  678. status = qt2_setup_urbs(serial);
  679. if (status != 0)
  680. goto attach_failed;
  681. return 0;
  682. attach_failed:
  683. kfree(serial_priv);
  684. return status;
  685. }
  686. static int qt2_port_probe(struct usb_serial_port *port)
  687. {
  688. struct usb_serial *serial = port->serial;
  689. struct qt2_port_private *port_priv;
  690. u8 bEndpointAddress;
  691. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  692. if (!port_priv)
  693. return -ENOMEM;
  694. spin_lock_init(&port_priv->lock);
  695. spin_lock_init(&port_priv->urb_lock);
  696. init_waitqueue_head(&port_priv->delta_msr_wait);
  697. port_priv->port = port;
  698. port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  699. if (!port_priv->write_urb) {
  700. kfree(port_priv);
  701. return -ENOMEM;
  702. }
  703. bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
  704. usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
  705. usb_sndbulkpipe(serial->dev, bEndpointAddress),
  706. port_priv->write_buffer,
  707. sizeof(port_priv->write_buffer),
  708. qt2_write_bulk_callback, port);
  709. usb_set_serial_port_data(port, port_priv);
  710. return 0;
  711. }
  712. static int qt2_port_remove(struct usb_serial_port *port)
  713. {
  714. struct qt2_port_private *port_priv;
  715. port_priv = usb_get_serial_port_data(port);
  716. usb_free_urb(port_priv->write_urb);
  717. kfree(port_priv);
  718. return 0;
  719. }
  720. static int qt2_tiocmget(struct tty_struct *tty)
  721. {
  722. struct usb_serial_port *port = tty->driver_data;
  723. struct usb_device *dev = port->serial->dev;
  724. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  725. u8 *d;
  726. int r;
  727. d = kzalloc(2, GFP_KERNEL);
  728. if (!d)
  729. return -ENOMEM;
  730. r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
  731. if (r < 0)
  732. goto mget_out;
  733. r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
  734. if (r < 0)
  735. goto mget_out;
  736. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  737. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  738. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  739. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  740. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  741. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  742. mget_out:
  743. kfree(d);
  744. return r;
  745. }
  746. static int qt2_tiocmset(struct tty_struct *tty,
  747. unsigned int set, unsigned int clear)
  748. {
  749. struct qt2_port_private *port_priv;
  750. port_priv = usb_get_serial_port_data(tty->driver_data);
  751. return update_mctrl(port_priv, set, clear);
  752. }
  753. static void qt2_break_ctl(struct tty_struct *tty, int break_state)
  754. {
  755. struct usb_serial_port *port = tty->driver_data;
  756. struct qt2_port_private *port_priv;
  757. int status;
  758. u16 val;
  759. port_priv = usb_get_serial_port_data(port);
  760. if (!port_priv->is_open) {
  761. dev_err(&port->dev,
  762. "%s - port is not open\n", __func__);
  763. return;
  764. }
  765. val = (break_state == -1) ? 1 : 0;
  766. status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
  767. val, port_priv->device_port);
  768. if (status < 0)
  769. dev_warn(&port->dev,
  770. "%s - failed to send control message: %i\n", __func__,
  771. status);
  772. }
  773. static void qt2_dtr_rts(struct usb_serial_port *port, int on)
  774. {
  775. struct usb_device *dev = port->serial->dev;
  776. struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
  777. mutex_lock(&port->serial->disc_mutex);
  778. if (!port->serial->disconnected) {
  779. /* Disable flow control */
  780. if (!on && qt2_setregister(dev, port_priv->device_port,
  781. UART_MCR, 0) < 0)
  782. dev_warn(&port->dev, "error from flowcontrol urb\n");
  783. /* drop RTS and DTR */
  784. if (on)
  785. update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
  786. else
  787. update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
  788. }
  789. mutex_unlock(&port->serial->disc_mutex);
  790. }
  791. static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
  792. {
  793. struct qt2_port_private *port_priv;
  794. u8 newMSR = (u8) *ch;
  795. unsigned long flags;
  796. port_priv = usb_get_serial_port_data(port);
  797. spin_lock_irqsave(&port_priv->lock, flags);
  798. port_priv->shadowMSR = newMSR;
  799. spin_unlock_irqrestore(&port_priv->lock, flags);
  800. if (newMSR & UART_MSR_ANY_DELTA) {
  801. /* update input line counters */
  802. if (newMSR & UART_MSR_DCTS)
  803. port_priv->icount.cts++;
  804. if (newMSR & UART_MSR_DDSR)
  805. port_priv->icount.dsr++;
  806. if (newMSR & UART_MSR_DDCD)
  807. port_priv->icount.dcd++;
  808. if (newMSR & UART_MSR_TERI)
  809. port_priv->icount.rng++;
  810. wake_up_interruptible(&port_priv->delta_msr_wait);
  811. }
  812. }
  813. static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
  814. {
  815. struct qt2_port_private *port_priv;
  816. struct async_icount *icount;
  817. unsigned long flags;
  818. u8 newLSR = (u8) *ch;
  819. port_priv = usb_get_serial_port_data(port);
  820. if (newLSR & UART_LSR_BI)
  821. newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
  822. spin_lock_irqsave(&port_priv->lock, flags);
  823. port_priv->shadowLSR = newLSR;
  824. spin_unlock_irqrestore(&port_priv->lock, flags);
  825. icount = &port_priv->icount;
  826. if (newLSR & UART_LSR_BRK_ERROR_BITS) {
  827. if (newLSR & UART_LSR_BI)
  828. icount->brk++;
  829. if (newLSR & UART_LSR_OE)
  830. icount->overrun++;
  831. if (newLSR & UART_LSR_PE)
  832. icount->parity++;
  833. if (newLSR & UART_LSR_FE)
  834. icount->frame++;
  835. }
  836. }
  837. static int qt2_write_room(struct tty_struct *tty)
  838. {
  839. struct usb_serial_port *port = tty->driver_data;
  840. struct qt2_port_private *port_priv;
  841. unsigned long flags = 0;
  842. int r;
  843. port_priv = usb_get_serial_port_data(port);
  844. spin_lock_irqsave(&port_priv->urb_lock, flags);
  845. if (port_priv->urb_in_use)
  846. r = 0;
  847. else
  848. r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
  849. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  850. return r;
  851. }
  852. static int qt2_write(struct tty_struct *tty,
  853. struct usb_serial_port *port,
  854. const unsigned char *buf, int count)
  855. {
  856. struct qt2_port_private *port_priv;
  857. struct urb *write_urb;
  858. unsigned char *data;
  859. unsigned long flags;
  860. int status;
  861. int bytes_out = 0;
  862. port_priv = usb_get_serial_port_data(port);
  863. if (port_priv->write_urb == NULL) {
  864. dev_err(&port->dev, "%s - no output urb\n", __func__);
  865. return 0;
  866. }
  867. write_urb = port_priv->write_urb;
  868. count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
  869. data = write_urb->transfer_buffer;
  870. spin_lock_irqsave(&port_priv->urb_lock, flags);
  871. if (port_priv->urb_in_use == true) {
  872. dev_err(&port->dev, "qt2_write - urb is in use\n");
  873. goto write_out;
  874. }
  875. *data++ = QT2_CONTROL_BYTE;
  876. *data++ = QT2_CONTROL_BYTE;
  877. *data++ = port_priv->device_port;
  878. put_unaligned_le16(count, data);
  879. data += 2;
  880. memcpy(data, buf, count);
  881. write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
  882. status = usb_submit_urb(write_urb, GFP_ATOMIC);
  883. if (status == 0) {
  884. port_priv->urb_in_use = true;
  885. bytes_out += count;
  886. }
  887. write_out:
  888. spin_unlock_irqrestore(&port_priv->urb_lock, flags);
  889. return bytes_out;
  890. }
  891. static struct usb_serial_driver qt2_device = {
  892. .driver = {
  893. .owner = THIS_MODULE,
  894. .name = "quatech-serial",
  895. },
  896. .description = DRIVER_DESC,
  897. .id_table = id_table,
  898. .open = qt2_open,
  899. .close = qt2_close,
  900. .write = qt2_write,
  901. .write_room = qt2_write_room,
  902. .calc_num_ports = qt2_calc_num_ports,
  903. .attach = qt2_attach,
  904. .release = qt2_release,
  905. .disconnect = qt2_disconnect,
  906. .port_probe = qt2_port_probe,
  907. .port_remove = qt2_port_remove,
  908. .dtr_rts = qt2_dtr_rts,
  909. .break_ctl = qt2_break_ctl,
  910. .tiocmget = qt2_tiocmget,
  911. .tiocmset = qt2_tiocmset,
  912. .get_icount = qt2_get_icount,
  913. .ioctl = qt2_ioctl,
  914. .set_termios = qt2_set_termios,
  915. };
  916. static struct usb_serial_driver *const serial_drivers[] = {
  917. &qt2_device, NULL
  918. };
  919. module_usb_serial_driver(serial_drivers, id_table);
  920. MODULE_DESCRIPTION(DRIVER_DESC);
  921. MODULE_LICENSE("GPL");