ssu100.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * usb-serial driver for Quatech SSU-100
  3. *
  4. * based on ftdi_sio.c and the original serqt_usb.c from Quatech
  5. *
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/slab.h>
  9. #include <linux/tty.h>
  10. #include <linux/tty_driver.h>
  11. #include <linux/tty_flip.h>
  12. #include <linux/module.h>
  13. #include <linux/serial.h>
  14. #include <linux/usb.h>
  15. #include <linux/usb/serial.h>
  16. #include <linux/serial_reg.h>
  17. #include <linux/uaccess.h>
  18. #define QT_OPEN_CLOSE_CHANNEL 0xca
  19. #define QT_SET_GET_DEVICE 0xc2
  20. #define QT_SET_GET_REGISTER 0xc0
  21. #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
  22. #define QT_SET_ATF 0xcd
  23. #define QT_GET_SET_UART 0xc1
  24. #define QT_TRANSFER_IN 0xc0
  25. #define QT_HW_FLOW_CONTROL_MASK 0xc5
  26. #define QT_SW_FLOW_CONTROL_MASK 0xc6
  27. #define SERIAL_MSR_MASK 0xf0
  28. #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
  29. #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
  30. #define MAX_BAUD_RATE 460800
  31. #define ATC_DISABLED 0x00
  32. #define DUPMODE_BITS 0xc0
  33. #define RR_BITS 0x03
  34. #define LOOPMODE_BITS 0x41
  35. #define RS232_MODE 0x00
  36. #define RTSCTS_TO_CONNECTOR 0x40
  37. #define CLKS_X4 0x02
  38. #define FULLPWRBIT 0x00000080
  39. #define NEXT_BOARD_POWER_BIT 0x00000004
  40. #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
  41. #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
  42. #define QUATECH_SSU100 0xC020 /* SSU100 */
  43. static const struct usb_device_id id_table[] = {
  44. {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
  45. {} /* Terminating entry */
  46. };
  47. MODULE_DEVICE_TABLE(usb, id_table);
  48. struct ssu100_port_private {
  49. spinlock_t status_lock;
  50. u8 shadowLSR;
  51. u8 shadowMSR;
  52. };
  53. static inline int ssu100_control_msg(struct usb_device *dev,
  54. u8 request, u16 data, u16 index)
  55. {
  56. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  57. request, 0x40, data, index,
  58. NULL, 0, 300);
  59. }
  60. static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
  61. {
  62. u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
  63. return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
  64. }
  65. static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
  66. {
  67. int ret;
  68. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  69. QT_SET_GET_DEVICE, 0xc0, 0, 0,
  70. data, 3, 300);
  71. if (ret < 3) {
  72. if (ret >= 0)
  73. ret = -EIO;
  74. }
  75. return ret;
  76. }
  77. static inline int ssu100_getregister(struct usb_device *dev,
  78. unsigned short uart,
  79. unsigned short reg,
  80. u8 *data)
  81. {
  82. int ret;
  83. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  84. QT_SET_GET_REGISTER, 0xc0, reg,
  85. uart, data, sizeof(*data), 300);
  86. if (ret < sizeof(*data)) {
  87. if (ret >= 0)
  88. ret = -EIO;
  89. }
  90. return ret;
  91. }
  92. static inline int ssu100_setregister(struct usb_device *dev,
  93. unsigned short uart,
  94. unsigned short reg,
  95. u16 data)
  96. {
  97. u16 value = (data << 8) | reg;
  98. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  99. QT_SET_GET_REGISTER, 0x40, value, uart,
  100. NULL, 0, 300);
  101. }
  102. #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
  103. #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
  104. /* these do not deal with device that have more than 1 port */
  105. static inline int update_mctrl(struct usb_device *dev, unsigned int set,
  106. unsigned int clear)
  107. {
  108. unsigned urb_value;
  109. int result;
  110. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
  111. dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
  112. return 0; /* no change */
  113. }
  114. clear &= ~set; /* 'set' takes precedence over 'clear' */
  115. urb_value = 0;
  116. if (set & TIOCM_DTR)
  117. urb_value |= UART_MCR_DTR;
  118. if (set & TIOCM_RTS)
  119. urb_value |= UART_MCR_RTS;
  120. result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
  121. if (result < 0)
  122. dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
  123. return result;
  124. }
  125. static int ssu100_initdevice(struct usb_device *dev)
  126. {
  127. u8 *data;
  128. int result = 0;
  129. data = kzalloc(3, GFP_KERNEL);
  130. if (!data)
  131. return -ENOMEM;
  132. result = ssu100_getdevice(dev, data);
  133. if (result < 0) {
  134. dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
  135. goto out;
  136. }
  137. data[1] &= ~FULLPWRBIT;
  138. result = ssu100_setdevice(dev, data);
  139. if (result < 0) {
  140. dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
  141. goto out;
  142. }
  143. result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
  144. if (result < 0) {
  145. dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
  146. goto out;
  147. }
  148. result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
  149. if (result < 0) {
  150. dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
  151. goto out;
  152. }
  153. result = ssu100_getdevice(dev, data);
  154. if (result < 0) {
  155. dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
  156. goto out;
  157. }
  158. data[0] &= ~(RR_BITS | DUPMODE_BITS);
  159. data[0] |= CLKS_X4;
  160. data[1] &= ~(LOOPMODE_BITS);
  161. data[1] |= RS232_MODE;
  162. result = ssu100_setdevice(dev, data);
  163. if (result < 0) {
  164. dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
  165. goto out;
  166. }
  167. out: kfree(data);
  168. return result;
  169. }
  170. static void ssu100_set_termios(struct tty_struct *tty,
  171. struct usb_serial_port *port,
  172. struct ktermios *old_termios)
  173. {
  174. struct usb_device *dev = port->serial->dev;
  175. struct ktermios *termios = &tty->termios;
  176. u16 baud, divisor, remainder;
  177. unsigned int cflag = termios->c_cflag;
  178. u16 urb_value = 0; /* will hold the new flags */
  179. int result;
  180. if (cflag & PARENB) {
  181. if (cflag & PARODD)
  182. urb_value |= UART_LCR_PARITY;
  183. else
  184. urb_value |= SERIAL_EVEN_PARITY;
  185. }
  186. switch (cflag & CSIZE) {
  187. case CS5:
  188. urb_value |= UART_LCR_WLEN5;
  189. break;
  190. case CS6:
  191. urb_value |= UART_LCR_WLEN6;
  192. break;
  193. case CS7:
  194. urb_value |= UART_LCR_WLEN7;
  195. break;
  196. default:
  197. case CS8:
  198. urb_value |= UART_LCR_WLEN8;
  199. break;
  200. }
  201. baud = tty_get_baud_rate(tty);
  202. if (!baud)
  203. baud = 9600;
  204. dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
  205. divisor = MAX_BAUD_RATE / baud;
  206. remainder = MAX_BAUD_RATE % baud;
  207. if (((remainder * 2) >= baud) && (baud != 110))
  208. divisor++;
  209. urb_value = urb_value << 8;
  210. result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
  211. if (result < 0)
  212. dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
  213. if (cflag & CRTSCTS)
  214. result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  215. SERIAL_CRTSCTS, 0);
  216. else
  217. result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
  218. 0, 0);
  219. if (result < 0)
  220. dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
  221. if (I_IXOFF(tty) || I_IXON(tty)) {
  222. u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
  223. result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  224. x, 0);
  225. } else
  226. result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
  227. 0, 0);
  228. if (result < 0)
  229. dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
  230. }
  231. static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
  232. {
  233. struct usb_device *dev = port->serial->dev;
  234. struct ssu100_port_private *priv = usb_get_serial_port_data(port);
  235. u8 *data;
  236. int result;
  237. unsigned long flags;
  238. data = kzalloc(2, GFP_KERNEL);
  239. if (!data)
  240. return -ENOMEM;
  241. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  242. QT_OPEN_CLOSE_CHANNEL,
  243. QT_TRANSFER_IN, 0x01,
  244. 0, data, 2, 300);
  245. if (result < 2) {
  246. dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
  247. if (result >= 0)
  248. result = -EIO;
  249. kfree(data);
  250. return result;
  251. }
  252. spin_lock_irqsave(&priv->status_lock, flags);
  253. priv->shadowLSR = data[0];
  254. priv->shadowMSR = data[1];
  255. spin_unlock_irqrestore(&priv->status_lock, flags);
  256. kfree(data);
  257. /* set to 9600 */
  258. result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
  259. if (result < 0)
  260. dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
  261. if (tty)
  262. ssu100_set_termios(tty, port, &tty->termios);
  263. return usb_serial_generic_open(tty, port);
  264. }
  265. static int get_serial_info(struct usb_serial_port *port,
  266. struct serial_struct __user *retinfo)
  267. {
  268. struct serial_struct tmp;
  269. if (!retinfo)
  270. return -EFAULT;
  271. memset(&tmp, 0, sizeof(tmp));
  272. tmp.line = port->minor;
  273. tmp.port = 0;
  274. tmp.irq = 0;
  275. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  276. tmp.xmit_fifo_size = port->bulk_out_size;
  277. tmp.baud_base = 9600;
  278. tmp.close_delay = 5*HZ;
  279. tmp.closing_wait = 30*HZ;
  280. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  281. return -EFAULT;
  282. return 0;
  283. }
  284. static int ssu100_ioctl(struct tty_struct *tty,
  285. unsigned int cmd, unsigned long arg)
  286. {
  287. struct usb_serial_port *port = tty->driver_data;
  288. switch (cmd) {
  289. case TIOCGSERIAL:
  290. return get_serial_info(port,
  291. (struct serial_struct __user *) arg);
  292. default:
  293. break;
  294. }
  295. return -ENOIOCTLCMD;
  296. }
  297. static int ssu100_attach(struct usb_serial *serial)
  298. {
  299. return ssu100_initdevice(serial->dev);
  300. }
  301. static int ssu100_port_probe(struct usb_serial_port *port)
  302. {
  303. struct ssu100_port_private *priv;
  304. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  305. if (!priv)
  306. return -ENOMEM;
  307. spin_lock_init(&priv->status_lock);
  308. usb_set_serial_port_data(port, priv);
  309. return 0;
  310. }
  311. static int ssu100_port_remove(struct usb_serial_port *port)
  312. {
  313. struct ssu100_port_private *priv;
  314. priv = usb_get_serial_port_data(port);
  315. kfree(priv);
  316. return 0;
  317. }
  318. static int ssu100_tiocmget(struct tty_struct *tty)
  319. {
  320. struct usb_serial_port *port = tty->driver_data;
  321. struct usb_device *dev = port->serial->dev;
  322. u8 *d;
  323. int r;
  324. d = kzalloc(2, GFP_KERNEL);
  325. if (!d)
  326. return -ENOMEM;
  327. r = ssu100_getregister(dev, 0, UART_MCR, d);
  328. if (r < 0)
  329. goto mget_out;
  330. r = ssu100_getregister(dev, 0, UART_MSR, d+1);
  331. if (r < 0)
  332. goto mget_out;
  333. r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
  334. (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
  335. (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
  336. (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
  337. (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
  338. (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
  339. mget_out:
  340. kfree(d);
  341. return r;
  342. }
  343. static int ssu100_tiocmset(struct tty_struct *tty,
  344. unsigned int set, unsigned int clear)
  345. {
  346. struct usb_serial_port *port = tty->driver_data;
  347. struct usb_device *dev = port->serial->dev;
  348. return update_mctrl(dev, set, clear);
  349. }
  350. static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
  351. {
  352. struct usb_device *dev = port->serial->dev;
  353. /* Disable flow control */
  354. if (!on) {
  355. if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
  356. dev_err(&port->dev, "error from flowcontrol urb\n");
  357. }
  358. /* drop RTS and DTR */
  359. if (on)
  360. set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
  361. else
  362. clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
  363. }
  364. static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
  365. {
  366. struct ssu100_port_private *priv = usb_get_serial_port_data(port);
  367. unsigned long flags;
  368. spin_lock_irqsave(&priv->status_lock, flags);
  369. priv->shadowMSR = msr;
  370. spin_unlock_irqrestore(&priv->status_lock, flags);
  371. if (msr & UART_MSR_ANY_DELTA) {
  372. /* update input line counters */
  373. if (msr & UART_MSR_DCTS)
  374. port->icount.cts++;
  375. if (msr & UART_MSR_DDSR)
  376. port->icount.dsr++;
  377. if (msr & UART_MSR_DDCD)
  378. port->icount.dcd++;
  379. if (msr & UART_MSR_TERI)
  380. port->icount.rng++;
  381. wake_up_interruptible(&port->port.delta_msr_wait);
  382. }
  383. }
  384. static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
  385. char *tty_flag)
  386. {
  387. struct ssu100_port_private *priv = usb_get_serial_port_data(port);
  388. unsigned long flags;
  389. spin_lock_irqsave(&priv->status_lock, flags);
  390. priv->shadowLSR = lsr;
  391. spin_unlock_irqrestore(&priv->status_lock, flags);
  392. *tty_flag = TTY_NORMAL;
  393. if (lsr & UART_LSR_BRK_ERROR_BITS) {
  394. /* we always want to update icount, but we only want to
  395. * update tty_flag for one case */
  396. if (lsr & UART_LSR_BI) {
  397. port->icount.brk++;
  398. *tty_flag = TTY_BREAK;
  399. usb_serial_handle_break(port);
  400. }
  401. if (lsr & UART_LSR_PE) {
  402. port->icount.parity++;
  403. if (*tty_flag == TTY_NORMAL)
  404. *tty_flag = TTY_PARITY;
  405. }
  406. if (lsr & UART_LSR_FE) {
  407. port->icount.frame++;
  408. if (*tty_flag == TTY_NORMAL)
  409. *tty_flag = TTY_FRAME;
  410. }
  411. if (lsr & UART_LSR_OE) {
  412. port->icount.overrun++;
  413. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  414. }
  415. }
  416. }
  417. static void ssu100_process_read_urb(struct urb *urb)
  418. {
  419. struct usb_serial_port *port = urb->context;
  420. char *packet = (char *)urb->transfer_buffer;
  421. char flag = TTY_NORMAL;
  422. u32 len = urb->actual_length;
  423. int i;
  424. char *ch;
  425. if ((len >= 4) &&
  426. (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
  427. ((packet[2] == 0x00) || (packet[2] == 0x01))) {
  428. if (packet[2] == 0x00)
  429. ssu100_update_lsr(port, packet[3], &flag);
  430. if (packet[2] == 0x01)
  431. ssu100_update_msr(port, packet[3]);
  432. len -= 4;
  433. ch = packet + 4;
  434. } else
  435. ch = packet;
  436. if (!len)
  437. return; /* status only */
  438. if (port->port.console && port->sysrq) {
  439. for (i = 0; i < len; i++, ch++) {
  440. if (!usb_serial_handle_sysrq_char(port, *ch))
  441. tty_insert_flip_char(&port->port, *ch, flag);
  442. }
  443. } else
  444. tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
  445. tty_flip_buffer_push(&port->port);
  446. }
  447. static struct usb_serial_driver ssu100_device = {
  448. .driver = {
  449. .owner = THIS_MODULE,
  450. .name = "ssu100",
  451. },
  452. .description = DRIVER_DESC,
  453. .id_table = id_table,
  454. .num_ports = 1,
  455. .open = ssu100_open,
  456. .attach = ssu100_attach,
  457. .port_probe = ssu100_port_probe,
  458. .port_remove = ssu100_port_remove,
  459. .dtr_rts = ssu100_dtr_rts,
  460. .process_read_urb = ssu100_process_read_urb,
  461. .tiocmget = ssu100_tiocmget,
  462. .tiocmset = ssu100_tiocmset,
  463. .tiocmiwait = usb_serial_generic_tiocmiwait,
  464. .get_icount = usb_serial_generic_get_icount,
  465. .ioctl = ssu100_ioctl,
  466. .set_termios = ssu100_set_termios,
  467. };
  468. static struct usb_serial_driver * const serial_drivers[] = {
  469. &ssu100_device, NULL
  470. };
  471. module_usb_serial_driver(serial_drivers, id_table);
  472. MODULE_DESCRIPTION(DRIVER_DESC);
  473. MODULE_LICENSE("GPL");