quatech2.c 28 KB

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