quatech2.c 28 KB

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