spcp8x5.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * spcp8x5 USB to serial adaptor driver
  3. *
  4. * Copyright (C) 2006 Linxb (xubin.lin@worldplus.com.cn)
  5. * Copyright (C) 2006 S1 Corp.
  6. *
  7. * Original driver for 2.6.10 pl2303 driver by
  8. * Greg Kroah-Hartman (greg@kroah.com)
  9. * Changes for 2.6.20 by Harald Klein <hari@vt100.at>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_driver.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/module.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. /* Version Information */
  30. #define DRIVER_VERSION "v0.04"
  31. #define DRIVER_DESC "SPCP8x5 USB to serial adaptor driver"
  32. static int debug;
  33. #define SPCP8x5_007_VID 0x04FC
  34. #define SPCP8x5_007_PID 0x0201
  35. #define SPCP8x5_008_VID 0x04fc
  36. #define SPCP8x5_008_PID 0x0235
  37. #define SPCP8x5_PHILIPS_VID 0x0471
  38. #define SPCP8x5_PHILIPS_PID 0x081e
  39. #define SPCP8x5_INTERMATIC_VID 0x04FC
  40. #define SPCP8x5_INTERMATIC_PID 0x0204
  41. #define SPCP8x5_835_VID 0x04fc
  42. #define SPCP8x5_835_PID 0x0231
  43. static struct usb_device_id id_table [] = {
  44. { USB_DEVICE(SPCP8x5_PHILIPS_VID , SPCP8x5_PHILIPS_PID)},
  45. { USB_DEVICE(SPCP8x5_INTERMATIC_VID, SPCP8x5_INTERMATIC_PID)},
  46. { USB_DEVICE(SPCP8x5_835_VID, SPCP8x5_835_PID)},
  47. { USB_DEVICE(SPCP8x5_008_VID, SPCP8x5_008_PID)},
  48. { USB_DEVICE(SPCP8x5_007_VID, SPCP8x5_007_PID)},
  49. { } /* Terminating entry */
  50. };
  51. MODULE_DEVICE_TABLE(usb, id_table);
  52. struct spcp8x5_usb_ctrl_arg {
  53. u8 type;
  54. u8 cmd;
  55. u8 cmd_type;
  56. u16 value;
  57. u16 index;
  58. u16 length;
  59. };
  60. /* wait 30s before close */
  61. #define SPCP8x5_CLOSING_WAIT (30*HZ)
  62. #define SPCP8x5_BUF_SIZE 1024
  63. /* spcp8x5 spec register define */
  64. #define MCR_CONTROL_LINE_RTS 0x02
  65. #define MCR_CONTROL_LINE_DTR 0x01
  66. #define MCR_DTR 0x01
  67. #define MCR_RTS 0x02
  68. #define MSR_STATUS_LINE_DCD 0x80
  69. #define MSR_STATUS_LINE_RI 0x40
  70. #define MSR_STATUS_LINE_DSR 0x20
  71. #define MSR_STATUS_LINE_CTS 0x10
  72. /* verdor command here , we should define myself */
  73. #define SET_DEFAULT 0x40
  74. #define SET_DEFAULT_TYPE 0x20
  75. #define SET_UART_FORMAT 0x40
  76. #define SET_UART_FORMAT_TYPE 0x21
  77. #define SET_UART_FORMAT_SIZE_5 0x00
  78. #define SET_UART_FORMAT_SIZE_6 0x01
  79. #define SET_UART_FORMAT_SIZE_7 0x02
  80. #define SET_UART_FORMAT_SIZE_8 0x03
  81. #define SET_UART_FORMAT_STOP_1 0x00
  82. #define SET_UART_FORMAT_STOP_2 0x04
  83. #define SET_UART_FORMAT_PAR_NONE 0x00
  84. #define SET_UART_FORMAT_PAR_ODD 0x10
  85. #define SET_UART_FORMAT_PAR_EVEN 0x30
  86. #define SET_UART_FORMAT_PAR_MASK 0xD0
  87. #define SET_UART_FORMAT_PAR_SPACE 0x90
  88. #define GET_UART_STATUS_TYPE 0xc0
  89. #define GET_UART_STATUS 0x22
  90. #define GET_UART_STATUS_MSR 0x06
  91. #define SET_UART_STATUS 0x40
  92. #define SET_UART_STATUS_TYPE 0x23
  93. #define SET_UART_STATUS_MCR 0x0004
  94. #define SET_UART_STATUS_MCR_DTR 0x01
  95. #define SET_UART_STATUS_MCR_RTS 0x02
  96. #define SET_UART_STATUS_MCR_LOOP 0x10
  97. #define SET_WORKING_MODE 0x40
  98. #define SET_WORKING_MODE_TYPE 0x24
  99. #define SET_WORKING_MODE_U2C 0x00
  100. #define SET_WORKING_MODE_RS485 0x01
  101. #define SET_WORKING_MODE_PDMA 0x02
  102. #define SET_WORKING_MODE_SPP 0x03
  103. #define SET_FLOWCTL_CHAR 0x40
  104. #define SET_FLOWCTL_CHAR_TYPE 0x25
  105. #define GET_VERSION 0xc0
  106. #define GET_VERSION_TYPE 0x26
  107. #define SET_REGISTER 0x40
  108. #define SET_REGISTER_TYPE 0x27
  109. #define GET_REGISTER 0xc0
  110. #define GET_REGISTER_TYPE 0x28
  111. #define SET_RAM 0x40
  112. #define SET_RAM_TYPE 0x31
  113. #define GET_RAM 0xc0
  114. #define GET_RAM_TYPE 0x32
  115. /* how come ??? */
  116. #define UART_STATE 0x08
  117. #define UART_STATE_TRANSIENT_MASK 0x74
  118. #define UART_DCD 0x01
  119. #define UART_DSR 0x02
  120. #define UART_BREAK_ERROR 0x04
  121. #define UART_RING 0x08
  122. #define UART_FRAME_ERROR 0x10
  123. #define UART_PARITY_ERROR 0x20
  124. #define UART_OVERRUN_ERROR 0x40
  125. #define UART_CTS 0x80
  126. enum spcp8x5_type {
  127. SPCP825_007_TYPE,
  128. SPCP825_008_TYPE,
  129. SPCP825_PHILIP_TYPE,
  130. SPCP825_INTERMATIC_TYPE,
  131. SPCP835_TYPE,
  132. };
  133. /* 1st in 1st out buffer 4 driver */
  134. struct ringbuf {
  135. unsigned int buf_size;
  136. char *buf_buf;
  137. char *buf_get;
  138. char *buf_put;
  139. };
  140. /* alloc the ring buf and alloc the buffer itself */
  141. static inline struct ringbuf *alloc_ringbuf(unsigned int size)
  142. {
  143. struct ringbuf *pb;
  144. if (size == 0)
  145. return NULL;
  146. pb = kmalloc(sizeof(*pb), GFP_KERNEL);
  147. if (pb == NULL)
  148. return NULL;
  149. pb->buf_buf = kmalloc(size, GFP_KERNEL);
  150. if (pb->buf_buf == NULL) {
  151. kfree(pb);
  152. return NULL;
  153. }
  154. pb->buf_size = size;
  155. pb->buf_get = pb->buf_put = pb->buf_buf;
  156. return pb;
  157. }
  158. /* free the ring buf and the buffer itself */
  159. static inline void free_ringbuf(struct ringbuf *pb)
  160. {
  161. if (pb != NULL) {
  162. kfree(pb->buf_buf);
  163. kfree(pb);
  164. }
  165. }
  166. /* clear pipo , juest repoint the pointer here */
  167. static inline void clear_ringbuf(struct ringbuf *pb)
  168. {
  169. if (pb != NULL)
  170. pb->buf_get = pb->buf_put;
  171. }
  172. /* get the number of data in the pipo */
  173. static inline unsigned int ringbuf_avail_data(struct ringbuf *pb)
  174. {
  175. if (pb == NULL)
  176. return 0;
  177. return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
  178. }
  179. /* get the number of space in the pipo */
  180. static inline unsigned int ringbuf_avail_space(struct ringbuf *pb)
  181. {
  182. if (pb == NULL)
  183. return 0;
  184. return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
  185. }
  186. /* put count data into pipo */
  187. static unsigned int put_ringbuf(struct ringbuf *pb, const char *buf,
  188. unsigned int count)
  189. {
  190. unsigned int len;
  191. if (pb == NULL)
  192. return 0;
  193. len = ringbuf_avail_space(pb);
  194. if (count > len)
  195. count = len;
  196. if (count == 0)
  197. return 0;
  198. len = pb->buf_buf + pb->buf_size - pb->buf_put;
  199. if (count > len) {
  200. memcpy(pb->buf_put, buf, len);
  201. memcpy(pb->buf_buf, buf+len, count - len);
  202. pb->buf_put = pb->buf_buf + count - len;
  203. } else {
  204. memcpy(pb->buf_put, buf, count);
  205. if (count < len)
  206. pb->buf_put += count;
  207. else /* count == len */
  208. pb->buf_put = pb->buf_buf;
  209. }
  210. return count;
  211. }
  212. /* get count data from pipo */
  213. static unsigned int get_ringbuf(struct ringbuf *pb, char *buf,
  214. unsigned int count)
  215. {
  216. unsigned int len;
  217. if (pb == NULL || buf == NULL)
  218. return 0;
  219. len = ringbuf_avail_data(pb);
  220. if (count > len)
  221. count = len;
  222. if (count == 0)
  223. return 0;
  224. len = pb->buf_buf + pb->buf_size - pb->buf_get;
  225. if (count > len) {
  226. memcpy(buf, pb->buf_get, len);
  227. memcpy(buf+len, pb->buf_buf, count - len);
  228. pb->buf_get = pb->buf_buf + count - len;
  229. } else {
  230. memcpy(buf, pb->buf_get, count);
  231. if (count < len)
  232. pb->buf_get += count;
  233. else /* count == len */
  234. pb->buf_get = pb->buf_buf;
  235. }
  236. return count;
  237. }
  238. static struct usb_driver spcp8x5_driver = {
  239. .name = "spcp8x5",
  240. .probe = usb_serial_probe,
  241. .disconnect = usb_serial_disconnect,
  242. .id_table = id_table,
  243. .no_dynamic_id = 1,
  244. };
  245. struct spcp8x5_private {
  246. spinlock_t lock;
  247. struct ringbuf *buf;
  248. int write_urb_in_use;
  249. enum spcp8x5_type type;
  250. wait_queue_head_t delta_msr_wait;
  251. u8 line_control;
  252. u8 line_status;
  253. };
  254. /* desc : when device plug in,this function would be called.
  255. * thanks to usb_serial subsystem,then do almost every things for us. And what
  256. * we should do just alloc the buffer */
  257. static int spcp8x5_startup(struct usb_serial *serial)
  258. {
  259. struct spcp8x5_private *priv;
  260. int i;
  261. enum spcp8x5_type type = SPCP825_007_TYPE;
  262. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  263. if (product == 0x0201)
  264. type = SPCP825_007_TYPE;
  265. else if (product == 0x0231)
  266. type = SPCP835_TYPE;
  267. else if (product == 0x0235)
  268. type = SPCP825_008_TYPE;
  269. else if (product == 0x0204)
  270. type = SPCP825_INTERMATIC_TYPE;
  271. else if (product == 0x0471 &&
  272. serial->dev->descriptor.idVendor == cpu_to_le16(0x081e))
  273. type = SPCP825_PHILIP_TYPE;
  274. dev_dbg(&serial->dev->dev, "device type = %d\n", (int)type);
  275. for (i = 0; i < serial->num_ports; ++i) {
  276. priv = kzalloc(sizeof(struct spcp8x5_private), GFP_KERNEL);
  277. if (!priv)
  278. goto cleanup;
  279. spin_lock_init(&priv->lock);
  280. priv->buf = alloc_ringbuf(SPCP8x5_BUF_SIZE);
  281. if (priv->buf == NULL)
  282. goto cleanup2;
  283. init_waitqueue_head(&priv->delta_msr_wait);
  284. priv->type = type;
  285. usb_set_serial_port_data(serial->port[i] , priv);
  286. }
  287. return 0;
  288. cleanup2:
  289. kfree(priv);
  290. cleanup:
  291. for (--i; i >= 0; --i) {
  292. priv = usb_get_serial_port_data(serial->port[i]);
  293. free_ringbuf(priv->buf);
  294. kfree(priv);
  295. usb_set_serial_port_data(serial->port[i] , NULL);
  296. }
  297. return -ENOMEM;
  298. }
  299. /* call when the device plug out. free all the memory alloced by probe */
  300. static void spcp8x5_release(struct usb_serial *serial)
  301. {
  302. int i;
  303. struct spcp8x5_private *priv;
  304. for (i = 0; i < serial->num_ports; i++) {
  305. priv = usb_get_serial_port_data(serial->port[i]);
  306. if (priv) {
  307. free_ringbuf(priv->buf);
  308. kfree(priv);
  309. }
  310. }
  311. }
  312. /* set the modem control line of the device.
  313. * NOTE spcp825-007 not supported this */
  314. static int spcp8x5_set_ctrlLine(struct usb_device *dev, u8 value,
  315. enum spcp8x5_type type)
  316. {
  317. int retval;
  318. u8 mcr = 0 ;
  319. if (type == SPCP825_007_TYPE)
  320. return -EPERM;
  321. mcr = (unsigned short)value;
  322. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  323. SET_UART_STATUS_TYPE, SET_UART_STATUS,
  324. mcr, 0x04, NULL, 0, 100);
  325. if (retval != 0)
  326. dev_dbg(&dev->dev, "usb_control_msg return %#x\n", retval);
  327. return retval;
  328. }
  329. /* get the modem status register of the device
  330. * NOTE spcp825-007 not supported this */
  331. static int spcp8x5_get_msr(struct usb_device *dev, u8 *status,
  332. enum spcp8x5_type type)
  333. {
  334. u8 *status_buffer;
  335. int ret;
  336. /* I return Permited not support here but seem inval device
  337. * is more fix */
  338. if (type == SPCP825_007_TYPE)
  339. return -EPERM;
  340. if (status == NULL)
  341. return -EINVAL;
  342. status_buffer = kmalloc(1, GFP_KERNEL);
  343. if (!status_buffer)
  344. return -ENOMEM;
  345. status_buffer[0] = status[0];
  346. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  347. GET_UART_STATUS, GET_UART_STATUS_TYPE,
  348. 0, GET_UART_STATUS_MSR, status_buffer, 1, 100);
  349. if (ret < 0)
  350. dev_dbg(&dev->dev, "Get MSR = 0x%p failed (error = %d)",
  351. status_buffer, ret);
  352. dev_dbg(&dev->dev, "0xc0:0x22:0:6 %d - 0x%p ", ret, status_buffer);
  353. status[0] = status_buffer[0];
  354. kfree(status_buffer);
  355. return ret;
  356. }
  357. /* select the work mode.
  358. * NOTE this function not supported by spcp825-007 */
  359. static void spcp8x5_set_workMode(struct usb_device *dev, u16 value,
  360. u16 index, enum spcp8x5_type type)
  361. {
  362. int ret;
  363. /* I return Permited not support here but seem inval device
  364. * is more fix */
  365. if (type == SPCP825_007_TYPE)
  366. return;
  367. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  368. SET_WORKING_MODE_TYPE, SET_WORKING_MODE,
  369. value, index, NULL, 0, 100);
  370. dev_dbg(&dev->dev, "value = %#x , index = %#x\n", value, index);
  371. if (ret < 0)
  372. dev_dbg(&dev->dev,
  373. "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret);
  374. }
  375. static int spcp8x5_carrier_raised(struct usb_serial_port *port)
  376. {
  377. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  378. if (priv->line_status & MSR_STATUS_LINE_DCD)
  379. return 1;
  380. return 0;
  381. }
  382. static void spcp8x5_dtr_rts(struct usb_serial_port *port, int on)
  383. {
  384. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  385. unsigned long flags;
  386. u8 control;
  387. spin_lock_irqsave(&priv->lock, flags);
  388. if (on)
  389. priv->line_control = MCR_CONTROL_LINE_DTR
  390. | MCR_CONTROL_LINE_RTS;
  391. else
  392. priv->line_control &= ~ (MCR_CONTROL_LINE_DTR
  393. | MCR_CONTROL_LINE_RTS);
  394. control = priv->line_control;
  395. spin_unlock_irqrestore(&priv->lock, flags);
  396. spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type);
  397. }
  398. /* close the serial port. We should wait for data sending to device 1st and
  399. * then kill all urb. */
  400. static void spcp8x5_close(struct usb_serial_port *port)
  401. {
  402. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  403. unsigned long flags;
  404. int result;
  405. dbg("%s - port %d", __func__, port->number);
  406. spin_lock_irqsave(&priv->lock, flags);
  407. /* clear out any remaining data in the buffer */
  408. clear_ringbuf(priv->buf);
  409. spin_unlock_irqrestore(&priv->lock, flags);
  410. /* kill urb */
  411. if (port->write_urb != NULL) {
  412. result = usb_unlink_urb(port->write_urb);
  413. if (result)
  414. dev_dbg(&port->dev,
  415. "usb_unlink_urb(write_urb) = %d\n", result);
  416. }
  417. result = usb_unlink_urb(port->read_urb);
  418. if (result)
  419. dev_dbg(&port->dev, "usb_unlink_urb(read_urb) = %d\n", result);
  420. }
  421. static void spcp8x5_init_termios(struct tty_struct *tty)
  422. {
  423. /* for the 1st time call this function */
  424. *(tty->termios) = tty_std_termios;
  425. tty->termios->c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
  426. tty->termios->c_ispeed = 115200;
  427. tty->termios->c_ospeed = 115200;
  428. }
  429. /* set the serial param for transfer. we should check if we really need to
  430. * transfer. if we set flow control we should do this too. */
  431. static void spcp8x5_set_termios(struct tty_struct *tty,
  432. struct usb_serial_port *port, struct ktermios *old_termios)
  433. {
  434. struct usb_serial *serial = port->serial;
  435. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  436. unsigned long flags;
  437. unsigned int cflag = tty->termios->c_cflag;
  438. unsigned int old_cflag = old_termios->c_cflag;
  439. unsigned short uartdata;
  440. unsigned char buf[2] = {0, 0};
  441. int baud;
  442. int i;
  443. u8 control;
  444. /* check that they really want us to change something */
  445. if (!tty_termios_hw_change(tty->termios, old_termios))
  446. return;
  447. /* set DTR/RTS active */
  448. spin_lock_irqsave(&priv->lock, flags);
  449. control = priv->line_control;
  450. if ((old_cflag & CBAUD) == B0) {
  451. priv->line_control |= MCR_DTR;
  452. if (!(old_cflag & CRTSCTS))
  453. priv->line_control |= MCR_RTS;
  454. }
  455. if (control != priv->line_control) {
  456. control = priv->line_control;
  457. spin_unlock_irqrestore(&priv->lock, flags);
  458. spcp8x5_set_ctrlLine(serial->dev, control , priv->type);
  459. } else {
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. }
  462. /* Set Baud Rate */
  463. baud = tty_get_baud_rate(tty);
  464. switch (baud) {
  465. case 300: buf[0] = 0x00; break;
  466. case 600: buf[0] = 0x01; break;
  467. case 1200: buf[0] = 0x02; break;
  468. case 2400: buf[0] = 0x03; break;
  469. case 4800: buf[0] = 0x04; break;
  470. case 9600: buf[0] = 0x05; break;
  471. case 19200: buf[0] = 0x07; break;
  472. case 38400: buf[0] = 0x09; break;
  473. case 57600: buf[0] = 0x0a; break;
  474. case 115200: buf[0] = 0x0b; break;
  475. case 230400: buf[0] = 0x0c; break;
  476. case 460800: buf[0] = 0x0d; break;
  477. case 921600: buf[0] = 0x0e; break;
  478. /* case 1200000: buf[0] = 0x0f; break; */
  479. /* case 2400000: buf[0] = 0x10; break; */
  480. case 3000000: buf[0] = 0x11; break;
  481. /* case 6000000: buf[0] = 0x12; break; */
  482. case 0:
  483. case 1000000:
  484. buf[0] = 0x0b; break;
  485. default:
  486. dev_err(&port->dev, "spcp825 driver does not support the "
  487. "baudrate requested, using default of 9600.\n");
  488. }
  489. /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
  490. if (cflag & CSIZE) {
  491. switch (cflag & CSIZE) {
  492. case CS5:
  493. buf[1] |= SET_UART_FORMAT_SIZE_5;
  494. break;
  495. case CS6:
  496. buf[1] |= SET_UART_FORMAT_SIZE_6;
  497. break;
  498. case CS7:
  499. buf[1] |= SET_UART_FORMAT_SIZE_7;
  500. break;
  501. default:
  502. case CS8:
  503. buf[1] |= SET_UART_FORMAT_SIZE_8;
  504. break;
  505. }
  506. }
  507. /* Set Stop bit2 : 0:1bit 1:2bit */
  508. buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
  509. SET_UART_FORMAT_STOP_1;
  510. /* Set Parity bit3-4 01:Odd 11:Even */
  511. if (cflag & PARENB) {
  512. buf[1] |= (cflag & PARODD) ?
  513. SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
  514. } else
  515. buf[1] |= SET_UART_FORMAT_PAR_NONE;
  516. uartdata = buf[0] | buf[1]<<8;
  517. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  518. SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
  519. uartdata, 0, NULL, 0, 100);
  520. if (i < 0)
  521. dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n",
  522. uartdata, i);
  523. dbg("0x21:0x40:0:0 %d\n", i);
  524. if (cflag & CRTSCTS) {
  525. /* enable hardware flow control */
  526. spcp8x5_set_workMode(serial->dev, 0x000a,
  527. SET_WORKING_MODE_U2C, priv->type);
  528. }
  529. return;
  530. }
  531. /* open the serial port. do some usb system call. set termios and get the line
  532. * status of the device. then submit the read urb */
  533. static int spcp8x5_open(struct tty_struct *tty, struct usb_serial_port *port)
  534. {
  535. struct ktermios tmp_termios;
  536. struct usb_serial *serial = port->serial;
  537. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  538. int ret;
  539. unsigned long flags;
  540. u8 status = 0x30;
  541. /* status 0x30 means DSR and CTS = 1 other CDC RI and delta = 0 */
  542. dbg("%s - port %d", __func__, port->number);
  543. usb_clear_halt(serial->dev, port->write_urb->pipe);
  544. usb_clear_halt(serial->dev, port->read_urb->pipe);
  545. ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  546. 0x09, 0x00,
  547. 0x01, 0x00, NULL, 0x00, 100);
  548. if (ret)
  549. return ret;
  550. spcp8x5_set_ctrlLine(serial->dev, priv->line_control , priv->type);
  551. /* Setup termios */
  552. if (tty)
  553. spcp8x5_set_termios(tty, port, &tmp_termios);
  554. spcp8x5_get_msr(serial->dev, &status, priv->type);
  555. /* may be we should update uart status here but now we did not do */
  556. spin_lock_irqsave(&priv->lock, flags);
  557. priv->line_status = status & 0xf0 ;
  558. spin_unlock_irqrestore(&priv->lock, flags);
  559. dbg("%s - submitting read urb", __func__);
  560. port->read_urb->dev = serial->dev;
  561. ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
  562. if (ret) {
  563. spcp8x5_close(port);
  564. return -EPROTO;
  565. }
  566. port->port.drain_delay = 256;
  567. return 0;
  568. }
  569. /* bulk read call back function. check the status of the urb. if transfer
  570. * failed return. then update the status and the tty send data to tty subsys.
  571. * submit urb again.
  572. */
  573. static void spcp8x5_read_bulk_callback(struct urb *urb)
  574. {
  575. struct usb_serial_port *port = urb->context;
  576. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  577. struct tty_struct *tty;
  578. unsigned char *data = urb->transfer_buffer;
  579. unsigned long flags;
  580. int i;
  581. int result = urb->status;
  582. u8 status;
  583. char tty_flag;
  584. dev_dbg(&port->dev, "start, result = %d, urb->actual_length = %d\n,",
  585. result, urb->actual_length);
  586. /* check the urb status */
  587. if (result) {
  588. if (!port->port.count)
  589. return;
  590. if (result == -EPROTO) {
  591. /* spcp8x5 mysteriously fails with -EPROTO */
  592. /* reschedule the read */
  593. urb->dev = port->serial->dev;
  594. result = usb_submit_urb(urb , GFP_ATOMIC);
  595. if (result)
  596. dev_dbg(&port->dev,
  597. "failed submitting read urb %d\n",
  598. result);
  599. return;
  600. }
  601. dev_dbg(&port->dev, "unable to handle the error, exiting.\n");
  602. return;
  603. }
  604. /* get tty_flag from status */
  605. tty_flag = TTY_NORMAL;
  606. spin_lock_irqsave(&priv->lock, flags);
  607. status = priv->line_status;
  608. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  609. spin_unlock_irqrestore(&priv->lock, flags);
  610. /* wake up the wait for termios */
  611. wake_up_interruptible(&priv->delta_msr_wait);
  612. /* break takes precedence over parity, which takes precedence over
  613. * framing errors */
  614. if (status & UART_BREAK_ERROR)
  615. tty_flag = TTY_BREAK;
  616. else if (status & UART_PARITY_ERROR)
  617. tty_flag = TTY_PARITY;
  618. else if (status & UART_FRAME_ERROR)
  619. tty_flag = TTY_FRAME;
  620. dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
  621. tty = tty_port_tty_get(&port->port);
  622. if (tty && urb->actual_length) {
  623. tty_buffer_request_room(tty, urb->actual_length + 1);
  624. /* overrun is special, not associated with a char */
  625. if (status & UART_OVERRUN_ERROR)
  626. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  627. for (i = 0; i < urb->actual_length; ++i)
  628. tty_insert_flip_char(tty, data[i], tty_flag);
  629. tty_flip_buffer_push(tty);
  630. }
  631. tty_kref_put(tty);
  632. /* Schedule the next read _if_ we are still open */
  633. if (port->port.count) {
  634. urb->dev = port->serial->dev;
  635. result = usb_submit_urb(urb , GFP_ATOMIC);
  636. if (result)
  637. dev_dbg(&port->dev, "failed submitting read urb %d\n",
  638. result);
  639. }
  640. return;
  641. }
  642. /* get data from ring buffer and then write to usb bus */
  643. static void spcp8x5_send(struct usb_serial_port *port)
  644. {
  645. int count, result;
  646. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  647. unsigned long flags;
  648. spin_lock_irqsave(&priv->lock, flags);
  649. if (priv->write_urb_in_use) {
  650. dev_dbg(&port->dev, "write urb still used\n");
  651. spin_unlock_irqrestore(&priv->lock, flags);
  652. return;
  653. }
  654. /* send the 1st urb for writting */
  655. memset(port->write_urb->transfer_buffer , 0x00 , port->bulk_out_size);
  656. count = get_ringbuf(priv->buf, port->write_urb->transfer_buffer,
  657. port->bulk_out_size);
  658. if (count == 0) {
  659. spin_unlock_irqrestore(&priv->lock, flags);
  660. return;
  661. }
  662. /* update the urb status */
  663. priv->write_urb_in_use = 1;
  664. spin_unlock_irqrestore(&priv->lock, flags);
  665. port->write_urb->transfer_buffer_length = count;
  666. port->write_urb->dev = port->serial->dev;
  667. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  668. if (result) {
  669. dev_dbg(&port->dev, "failed submitting write urb, error %d\n",
  670. result);
  671. priv->write_urb_in_use = 0;
  672. /* TODO: reschedule spcp8x5_send */
  673. }
  674. schedule_work(&port->work);
  675. }
  676. /* this is the call back function for write urb. NOTE we should not sleep in
  677. * this routine. check the urb return code and then submit the write urb again
  678. * to hold the write loop */
  679. static void spcp8x5_write_bulk_callback(struct urb *urb)
  680. {
  681. struct usb_serial_port *port = urb->context;
  682. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  683. int result;
  684. int status = urb->status;
  685. switch (status) {
  686. case 0:
  687. /* success */
  688. break;
  689. case -ECONNRESET:
  690. case -ENOENT:
  691. case -ESHUTDOWN:
  692. /* this urb is terminated, clean up */
  693. dev_dbg(&port->dev, "urb shutting down with status: %d\n",
  694. status);
  695. priv->write_urb_in_use = 0;
  696. return;
  697. default:
  698. /* error in the urb, so we have to resubmit it */
  699. dbg("%s - Overflow in write", __func__);
  700. dbg("%s - nonzero write bulk status received: %d",
  701. __func__, status);
  702. port->write_urb->transfer_buffer_length = 1;
  703. port->write_urb->dev = port->serial->dev;
  704. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  705. if (result)
  706. dev_dbg(&port->dev,
  707. "failed resubmitting write urb %d\n", result);
  708. else
  709. return;
  710. }
  711. priv->write_urb_in_use = 0;
  712. /* send any buffered data */
  713. spcp8x5_send(port);
  714. }
  715. /* write data to ring buffer. and then start the write transfer */
  716. static int spcp8x5_write(struct tty_struct *tty, struct usb_serial_port *port,
  717. const unsigned char *buf, int count)
  718. {
  719. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  720. unsigned long flags;
  721. dev_dbg(&port->dev, "%d bytes\n", count);
  722. if (!count)
  723. return count;
  724. spin_lock_irqsave(&priv->lock, flags);
  725. count = put_ringbuf(priv->buf, buf, count);
  726. spin_unlock_irqrestore(&priv->lock, flags);
  727. spcp8x5_send(port);
  728. return count;
  729. }
  730. static int spcp8x5_wait_modem_info(struct usb_serial_port *port,
  731. unsigned int arg)
  732. {
  733. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  734. unsigned long flags;
  735. unsigned int prevstatus;
  736. unsigned int status;
  737. unsigned int changed;
  738. spin_lock_irqsave(&priv->lock, flags);
  739. prevstatus = priv->line_status;
  740. spin_unlock_irqrestore(&priv->lock, flags);
  741. while (1) {
  742. /* wake up in bulk read */
  743. interruptible_sleep_on(&priv->delta_msr_wait);
  744. /* see if a signal did it */
  745. if (signal_pending(current))
  746. return -ERESTARTSYS;
  747. spin_lock_irqsave(&priv->lock, flags);
  748. status = priv->line_status;
  749. spin_unlock_irqrestore(&priv->lock, flags);
  750. changed = prevstatus^status;
  751. if (((arg & TIOCM_RNG) && (changed & MSR_STATUS_LINE_RI)) ||
  752. ((arg & TIOCM_DSR) && (changed & MSR_STATUS_LINE_DSR)) ||
  753. ((arg & TIOCM_CD) && (changed & MSR_STATUS_LINE_DCD)) ||
  754. ((arg & TIOCM_CTS) && (changed & MSR_STATUS_LINE_CTS)))
  755. return 0;
  756. prevstatus = status;
  757. }
  758. /* NOTREACHED */
  759. return 0;
  760. }
  761. static int spcp8x5_ioctl(struct tty_struct *tty, struct file *file,
  762. unsigned int cmd, unsigned long arg)
  763. {
  764. struct usb_serial_port *port = tty->driver_data;
  765. dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
  766. switch (cmd) {
  767. case TIOCMIWAIT:
  768. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  769. return spcp8x5_wait_modem_info(port, arg);
  770. default:
  771. dbg("%s not supported = 0x%04x", __func__, cmd);
  772. break;
  773. }
  774. return -ENOIOCTLCMD;
  775. }
  776. static int spcp8x5_tiocmset(struct tty_struct *tty, struct file *file,
  777. unsigned int set, unsigned int clear)
  778. {
  779. struct usb_serial_port *port = tty->driver_data;
  780. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  781. unsigned long flags;
  782. u8 control;
  783. spin_lock_irqsave(&priv->lock, flags);
  784. if (set & TIOCM_RTS)
  785. priv->line_control |= MCR_RTS;
  786. if (set & TIOCM_DTR)
  787. priv->line_control |= MCR_DTR;
  788. if (clear & TIOCM_RTS)
  789. priv->line_control &= ~MCR_RTS;
  790. if (clear & TIOCM_DTR)
  791. priv->line_control &= ~MCR_DTR;
  792. control = priv->line_control;
  793. spin_unlock_irqrestore(&priv->lock, flags);
  794. return spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type);
  795. }
  796. static int spcp8x5_tiocmget(struct tty_struct *tty, struct file *file)
  797. {
  798. struct usb_serial_port *port = tty->driver_data;
  799. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  800. unsigned long flags;
  801. unsigned int mcr;
  802. unsigned int status;
  803. unsigned int result;
  804. spin_lock_irqsave(&priv->lock, flags);
  805. mcr = priv->line_control;
  806. status = priv->line_status;
  807. spin_unlock_irqrestore(&priv->lock, flags);
  808. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  809. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  810. | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0)
  811. | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0)
  812. | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0)
  813. | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0);
  814. return result;
  815. }
  816. /* get the avail space room in ring buffer */
  817. static int spcp8x5_write_room(struct tty_struct *tty)
  818. {
  819. struct usb_serial_port *port = tty->driver_data;
  820. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  821. int room = 0;
  822. unsigned long flags;
  823. spin_lock_irqsave(&priv->lock, flags);
  824. room = ringbuf_avail_space(priv->buf);
  825. spin_unlock_irqrestore(&priv->lock, flags);
  826. return room;
  827. }
  828. /* get the number of avail data in write ring buffer */
  829. static int spcp8x5_chars_in_buffer(struct tty_struct *tty)
  830. {
  831. struct usb_serial_port *port = tty->driver_data;
  832. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  833. int chars = 0;
  834. unsigned long flags;
  835. spin_lock_irqsave(&priv->lock, flags);
  836. chars = ringbuf_avail_data(priv->buf);
  837. spin_unlock_irqrestore(&priv->lock, flags);
  838. return chars;
  839. }
  840. /* All of the device info needed for the spcp8x5 SIO serial converter */
  841. static struct usb_serial_driver spcp8x5_device = {
  842. .driver = {
  843. .owner = THIS_MODULE,
  844. .name = "SPCP8x5",
  845. },
  846. .id_table = id_table,
  847. .num_ports = 1,
  848. .open = spcp8x5_open,
  849. .close = spcp8x5_close,
  850. .dtr_rts = spcp8x5_dtr_rts,
  851. .carrier_raised = spcp8x5_carrier_raised,
  852. .write = spcp8x5_write,
  853. .set_termios = spcp8x5_set_termios,
  854. .init_termios = spcp8x5_init_termios,
  855. .ioctl = spcp8x5_ioctl,
  856. .tiocmget = spcp8x5_tiocmget,
  857. .tiocmset = spcp8x5_tiocmset,
  858. .write_room = spcp8x5_write_room,
  859. .read_bulk_callback = spcp8x5_read_bulk_callback,
  860. .write_bulk_callback = spcp8x5_write_bulk_callback,
  861. .chars_in_buffer = spcp8x5_chars_in_buffer,
  862. .attach = spcp8x5_startup,
  863. .release = spcp8x5_release,
  864. };
  865. static int __init spcp8x5_init(void)
  866. {
  867. int retval;
  868. retval = usb_serial_register(&spcp8x5_device);
  869. if (retval)
  870. goto failed_usb_serial_register;
  871. retval = usb_register(&spcp8x5_driver);
  872. if (retval)
  873. goto failed_usb_register;
  874. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  875. DRIVER_DESC "\n");
  876. return 0;
  877. failed_usb_register:
  878. usb_serial_deregister(&spcp8x5_device);
  879. failed_usb_serial_register:
  880. return retval;
  881. }
  882. static void __exit spcp8x5_exit(void)
  883. {
  884. usb_deregister(&spcp8x5_driver);
  885. usb_serial_deregister(&spcp8x5_device);
  886. }
  887. module_init(spcp8x5_init);
  888. module_exit(spcp8x5_exit);
  889. MODULE_DESCRIPTION(DRIVER_DESC);
  890. MODULE_VERSION(DRIVER_VERSION);
  891. MODULE_LICENSE("GPL");
  892. module_param(debug, bool, S_IRUGO | S_IWUSR);
  893. MODULE_PARM_DESC(debug, "Debug enabled or not");