spcp8x5.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. u8 termios_initialized;
  254. };
  255. /* desc : when device plug in,this function would be called.
  256. * thanks to usb_serial subsystem,then do almost every things for us. And what
  257. * we should do just alloc the buffer */
  258. static int spcp8x5_startup(struct usb_serial *serial)
  259. {
  260. struct spcp8x5_private *priv;
  261. int i;
  262. enum spcp8x5_type type = SPCP825_007_TYPE;
  263. if (serial->dev->descriptor.idProduct == 0x0201)
  264. type = SPCP825_007_TYPE;
  265. else if (serial->dev->descriptor.idProduct == 0x0231)
  266. type = SPCP835_TYPE;
  267. else if (serial->dev->descriptor.idProduct == 0x0235)
  268. type = SPCP825_008_TYPE;
  269. else if (serial->dev->descriptor.idProduct == 0x0204)
  270. type = SPCP825_INTERMATIC_TYPE;
  271. else if (serial->dev->descriptor.idProduct == 0x0471 &&
  272. serial->dev->descriptor.idVendor == 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_shutdown(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. usb_set_serial_port_data(serial->port[i] , NULL);
  310. }
  311. }
  312. }
  313. /* set the modem control line of the device.
  314. * NOTE spcp825-007 not supported this */
  315. static int spcp8x5_set_ctrlLine(struct usb_device *dev, u8 value,
  316. enum spcp8x5_type type)
  317. {
  318. int retval;
  319. u8 mcr = 0 ;
  320. if (type == SPCP825_007_TYPE)
  321. return -EPERM;
  322. mcr = (unsigned short)value;
  323. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  324. SET_UART_STATUS_TYPE, SET_UART_STATUS,
  325. mcr, 0x04, NULL, 0, 100);
  326. if (retval != 0)
  327. dev_dbg(&dev->dev, "usb_control_msg return %#x\n", retval);
  328. return retval;
  329. }
  330. /* get the modem status register of the device
  331. * NOTE spcp825-007 not supported this */
  332. static int spcp8x5_get_msr(struct usb_device *dev, u8 *status,
  333. enum spcp8x5_type type)
  334. {
  335. u8 *status_buffer;
  336. int ret;
  337. /* I return Permited not support here but seem inval device
  338. * is more fix */
  339. if (type == SPCP825_007_TYPE)
  340. return -EPERM;
  341. if (status == NULL)
  342. return -EINVAL;
  343. status_buffer = kmalloc(1, GFP_KERNEL);
  344. if (!status_buffer)
  345. return -ENOMEM;
  346. status_buffer[0] = status[0];
  347. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  348. GET_UART_STATUS, GET_UART_STATUS_TYPE,
  349. 0, GET_UART_STATUS_MSR, status_buffer, 1, 100);
  350. if (ret < 0)
  351. dev_dbg(&dev->dev, "Get MSR = 0x%p failed (error = %d)",
  352. status_buffer, ret);
  353. dev_dbg(&dev->dev, "0xc0:0x22:0:6 %d - 0x%p ", ret, status_buffer);
  354. status[0] = status_buffer[0];
  355. kfree(status_buffer);
  356. return ret;
  357. }
  358. /* select the work mode.
  359. * NOTE this function not supported by spcp825-007 */
  360. static void spcp8x5_set_workMode(struct usb_device *dev, u16 value,
  361. u16 index, enum spcp8x5_type type)
  362. {
  363. int ret;
  364. /* I return Permited not support here but seem inval device
  365. * is more fix */
  366. if (type == SPCP825_007_TYPE)
  367. return;
  368. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  369. SET_WORKING_MODE_TYPE, SET_WORKING_MODE,
  370. value, index, NULL, 0, 100);
  371. dev_dbg(&dev->dev, "value = %#x , index = %#x\n", value, index);
  372. if (ret < 0)
  373. dev_dbg(&dev->dev,
  374. "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret);
  375. }
  376. /* close the serial port. We should wait for data sending to device 1st and
  377. * then kill all urb. */
  378. static void spcp8x5_close(struct usb_serial_port *port, struct file *filp)
  379. {
  380. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  381. unsigned long flags;
  382. unsigned int c_cflag;
  383. int bps;
  384. long timeout;
  385. wait_queue_t wait;
  386. int result;
  387. dbg("%s - port %d", __func__, port->number);
  388. /* wait for data to drain from the buffer */
  389. spin_lock_irqsave(&priv->lock, flags);
  390. timeout = SPCP8x5_CLOSING_WAIT;
  391. init_waitqueue_entry(&wait, current);
  392. add_wait_queue(&port->tty->write_wait, &wait);
  393. for (;;) {
  394. set_current_state(TASK_INTERRUPTIBLE);
  395. if (ringbuf_avail_data(priv->buf) == 0 ||
  396. timeout == 0 || signal_pending(current))
  397. break;
  398. spin_unlock_irqrestore(&priv->lock, flags);
  399. timeout = schedule_timeout(timeout);
  400. spin_lock_irqsave(&priv->lock, flags);
  401. }
  402. set_current_state(TASK_RUNNING);
  403. remove_wait_queue(&port->tty->write_wait, &wait);
  404. /* clear out any remaining data in the buffer */
  405. clear_ringbuf(priv->buf);
  406. spin_unlock_irqrestore(&priv->lock, flags);
  407. /* wait for characters to drain from the device (this is long enough
  408. * for the entire all byte spcp8x5 hardware buffer to drain with no
  409. * flow control for data rates of 1200 bps or more, for lower rates we
  410. * should really know how much data is in the buffer to compute a delay
  411. * that is not unnecessarily long) */
  412. bps = tty_get_baud_rate(port->tty);
  413. if (bps > 1200)
  414. timeout = max((HZ*2560) / bps, HZ/10);
  415. else
  416. timeout = 2*HZ;
  417. set_current_state(TASK_INTERRUPTIBLE);
  418. schedule_timeout(timeout);
  419. /* clear control lines */
  420. if (port->tty) {
  421. c_cflag = port->tty->termios->c_cflag;
  422. if (c_cflag & HUPCL) {
  423. spin_lock_irqsave(&priv->lock, flags);
  424. priv->line_control = 0;
  425. spin_unlock_irqrestore(&priv->lock, flags);
  426. spcp8x5_set_ctrlLine(port->serial->dev, 0 , priv->type);
  427. }
  428. }
  429. /* kill urb */
  430. if (port->write_urb != NULL) {
  431. result = usb_unlink_urb(port->write_urb);
  432. if (result)
  433. dev_dbg(&port->dev,
  434. "usb_unlink_urb(write_urb) = %d\n", result);
  435. }
  436. result = usb_unlink_urb(port->read_urb);
  437. if (result)
  438. dev_dbg(&port->dev, "usb_unlink_urb(read_urb) = %d\n", result);
  439. }
  440. /* set the serial param for transfer. we should check if we really need to
  441. * transfer. then if be set flow contorl we should do this too. */
  442. static void spcp8x5_set_termios(struct usb_serial_port *port,
  443. struct ktermios *old_termios)
  444. {
  445. struct usb_serial *serial = port->serial;
  446. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  447. unsigned long flags;
  448. unsigned int cflag = port->tty->termios->c_cflag;
  449. unsigned int old_cflag = old_termios->c_cflag;
  450. unsigned short uartdata;
  451. unsigned char buf[2] = {0, 0};
  452. int baud;
  453. int i;
  454. u8 control;
  455. if ((!port->tty) || (!port->tty->termios))
  456. return;
  457. /* for the 1st time call this function */
  458. spin_lock_irqsave(&priv->lock, flags);
  459. if (!priv->termios_initialized) {
  460. *(port->tty->termios) = tty_std_termios;
  461. port->tty->termios->c_cflag = B115200 | CS8 | CREAD |
  462. HUPCL | CLOCAL;
  463. priv->termios_initialized = 1;
  464. }
  465. spin_unlock_irqrestore(&priv->lock, flags);
  466. /* check that they really want us to change something */
  467. if (!tty_termios_hw_change(port->tty->termios, old_termios))
  468. return;
  469. /* set DTR/RTS active */
  470. spin_lock_irqsave(&priv->lock, flags);
  471. control = priv->line_control;
  472. if ((old_cflag & CBAUD) == B0) {
  473. priv->line_control |= MCR_DTR;
  474. if (!(old_cflag & CRTSCTS))
  475. priv->line_control |= MCR_RTS;
  476. }
  477. if (control != priv->line_control) {
  478. control = priv->line_control;
  479. spin_unlock_irqrestore(&priv->lock, flags);
  480. spcp8x5_set_ctrlLine(serial->dev, control , priv->type);
  481. } else {
  482. spin_unlock_irqrestore(&priv->lock, flags);
  483. }
  484. /* Set Baud Rate */
  485. baud = tty_get_baud_rate(port->tty);;
  486. switch (baud) {
  487. case 300: buf[0] = 0x00; break;
  488. case 600: buf[0] = 0x01; break;
  489. case 1200: buf[0] = 0x02; break;
  490. case 2400: buf[0] = 0x03; break;
  491. case 4800: buf[0] = 0x04; break;
  492. case 9600: buf[0] = 0x05; break;
  493. case 19200: buf[0] = 0x07; break;
  494. case 38400: buf[0] = 0x09; break;
  495. case 57600: buf[0] = 0x0a; break;
  496. case 115200: buf[0] = 0x0b; break;
  497. case 230400: buf[0] = 0x0c; break;
  498. case 460800: buf[0] = 0x0d; break;
  499. case 921600: buf[0] = 0x0e; break;
  500. /* case 1200000: buf[0] = 0x0f; break; */
  501. /* case 2400000: buf[0] = 0x10; break; */
  502. case 3000000: buf[0] = 0x11; break;
  503. /* case 6000000: buf[0] = 0x12; break; */
  504. case 0:
  505. case 1000000:
  506. buf[0] = 0x0b; break;
  507. default:
  508. err("spcp825 driver does not support the baudrate "
  509. "requested, using default of 9600.");
  510. }
  511. /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
  512. if (cflag & CSIZE) {
  513. switch (cflag & CSIZE) {
  514. case CS5:
  515. buf[1] |= SET_UART_FORMAT_SIZE_5;
  516. break;
  517. case CS6:
  518. buf[1] |= SET_UART_FORMAT_SIZE_6;
  519. break;
  520. case CS7:
  521. buf[1] |= SET_UART_FORMAT_SIZE_7;
  522. break;
  523. default:
  524. case CS8:
  525. buf[1] |= SET_UART_FORMAT_SIZE_8;
  526. break;
  527. }
  528. }
  529. /* Set Stop bit2 : 0:1bit 1:2bit */
  530. buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
  531. SET_UART_FORMAT_STOP_1;
  532. /* Set Parity bit3-4 01:Odd 11:Even */
  533. if (cflag & PARENB) {
  534. buf[1] |= (cflag & PARODD) ?
  535. SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
  536. } else
  537. buf[1] |= SET_UART_FORMAT_PAR_NONE;
  538. uartdata = buf[0] | buf[1]<<8;
  539. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  540. SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
  541. uartdata, 0, NULL, 0, 100);
  542. if (i < 0)
  543. err("Set UART format %#x failed (error = %d)", uartdata, i);
  544. dbg("0x21:0x40:0:0 %d\n", i);
  545. if (cflag & CRTSCTS) {
  546. /* enable hardware flow control */
  547. spcp8x5_set_workMode(serial->dev, 0x000a,
  548. SET_WORKING_MODE_U2C, priv->type);
  549. }
  550. return;
  551. }
  552. /* open the serial port. do some usb system call. set termios and get the line
  553. * status of the device. then submit the read urb */
  554. static int spcp8x5_open(struct usb_serial_port *port, struct file *filp)
  555. {
  556. struct ktermios tmp_termios;
  557. struct usb_serial *serial = port->serial;
  558. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  559. int ret;
  560. unsigned long flags;
  561. u8 status = 0x30;
  562. /* status 0x30 means DSR and CTS = 1 other CDC RI and delta = 0 */
  563. dbg("%s - port %d", __func__, port->number);
  564. usb_clear_halt(serial->dev, port->write_urb->pipe);
  565. usb_clear_halt(serial->dev, port->read_urb->pipe);
  566. ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  567. 0x09, 0x00,
  568. 0x01, 0x00, NULL, 0x00, 100);
  569. if (ret)
  570. return ret;
  571. spin_lock_irqsave(&priv->lock, flags);
  572. if (port->tty->termios->c_cflag & CBAUD)
  573. priv->line_control = MCR_DTR | MCR_RTS;
  574. else
  575. priv->line_control = 0;
  576. spin_unlock_irqrestore(&priv->lock, flags);
  577. spcp8x5_set_ctrlLine(serial->dev, priv->line_control , priv->type);
  578. /* Setup termios */
  579. if (port->tty)
  580. spcp8x5_set_termios(port, &tmp_termios);
  581. spcp8x5_get_msr(serial->dev, &status, priv->type);
  582. /* may be we should update uart status here but now we did not do */
  583. spin_lock_irqsave(&priv->lock, flags);
  584. priv->line_status = status & 0xf0 ;
  585. spin_unlock_irqrestore(&priv->lock, flags);
  586. /* FIXME: need to assert RTS and DTR if CRTSCTS off */
  587. dbg("%s - submitting read urb", __func__);
  588. port->read_urb->dev = serial->dev;
  589. ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
  590. if (ret) {
  591. spcp8x5_close(port, NULL);
  592. return -EPROTO;
  593. }
  594. return 0;
  595. }
  596. /* bulk read call back function. check the status of the urb. if transfer
  597. * failed return. then update the status and the tty send data to tty subsys.
  598. * submit urb again.
  599. */
  600. static void spcp8x5_read_bulk_callback(struct urb *urb)
  601. {
  602. struct usb_serial_port *port = urb->context;
  603. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  604. struct tty_struct *tty;
  605. unsigned char *data = urb->transfer_buffer;
  606. unsigned long flags;
  607. int i;
  608. int result;
  609. u8 status = 0;
  610. char tty_flag;
  611. dev_dbg(&port->dev, "start, urb->status = %d, "
  612. "urb->actual_length = %d\n,", urb->status, urb->actual_length);
  613. /* check the urb status */
  614. if (urb->status) {
  615. if (!port->open_count)
  616. return;
  617. if (urb->status == -EPROTO) {
  618. /* spcp8x5 mysteriously fails with -EPROTO */
  619. /* reschedule the read */
  620. urb->status = 0;
  621. urb->dev = port->serial->dev;
  622. result = usb_submit_urb(urb , GFP_ATOMIC);
  623. if (result)
  624. dev_dbg(&port->dev,
  625. "failed submitting read urb %d\n",
  626. result);
  627. return;
  628. }
  629. dev_dbg(&port->dev, "unable to handle the error, exiting.\n");
  630. return;
  631. }
  632. /* get tty_flag from status */
  633. tty_flag = TTY_NORMAL;
  634. spin_lock_irqsave(&priv->lock, flags);
  635. status = priv->line_status;
  636. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  637. spin_unlock_irqrestore(&priv->lock, flags);
  638. /* wake up the wait for termios */
  639. wake_up_interruptible(&priv->delta_msr_wait);
  640. /* break takes precedence over parity, which takes precedence over
  641. * framing errors */
  642. if (status & UART_BREAK_ERROR)
  643. tty_flag = TTY_BREAK;
  644. else if (status & UART_PARITY_ERROR)
  645. tty_flag = TTY_PARITY;
  646. else if (status & UART_FRAME_ERROR)
  647. tty_flag = TTY_FRAME;
  648. dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
  649. tty = port->tty;
  650. if (tty && urb->actual_length) {
  651. tty_buffer_request_room(tty, urb->actual_length + 1);
  652. /* overrun is special, not associated with a char */
  653. if (status & UART_OVERRUN_ERROR)
  654. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  655. for (i = 0; i < urb->actual_length; ++i)
  656. tty_insert_flip_char(tty, data[i], tty_flag);
  657. tty_flip_buffer_push(tty);
  658. }
  659. /* Schedule the next read _if_ we are still open */
  660. if (port->open_count) {
  661. urb->dev = port->serial->dev;
  662. result = usb_submit_urb(urb , GFP_ATOMIC);
  663. if (result)
  664. dev_dbg(&port->dev, "failed submitting read urb %d\n",
  665. result);
  666. }
  667. return;
  668. }
  669. /* get data from ring buffer and then write to usb bus */
  670. static void spcp8x5_send(struct usb_serial_port *port)
  671. {
  672. int count, result;
  673. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  674. unsigned long flags;
  675. spin_lock_irqsave(&priv->lock, flags);
  676. if (priv->write_urb_in_use) {
  677. dev_dbg(&port->dev, "write urb still used\n");
  678. spin_unlock_irqrestore(&priv->lock, flags);
  679. return;
  680. }
  681. /* send the 1st urb for writting */
  682. memset(port->write_urb->transfer_buffer , 0x00 , port->bulk_out_size);
  683. count = get_ringbuf(priv->buf, port->write_urb->transfer_buffer,
  684. port->bulk_out_size);
  685. if (count == 0) {
  686. spin_unlock_irqrestore(&priv->lock, flags);
  687. return;
  688. }
  689. /* update the urb status */
  690. priv->write_urb_in_use = 1;
  691. spin_unlock_irqrestore(&priv->lock, flags);
  692. port->write_urb->transfer_buffer_length = count;
  693. port->write_urb->dev = port->serial->dev;
  694. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  695. if (result) {
  696. dev_dbg(&port->dev, "failed submitting write urb, error %d\n",
  697. result);
  698. priv->write_urb_in_use = 0;
  699. /* TODO: reschedule spcp8x5_send */
  700. }
  701. schedule_work(&port->work);
  702. }
  703. /* this is the call back function for write urb. NOTE we should not sleep in
  704. * this routine. check the urb return code and then submit the write urb again
  705. * to hold the write loop */
  706. static void spcp8x5_write_bulk_callback(struct urb *urb)
  707. {
  708. struct usb_serial_port *port = urb->context;
  709. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  710. int result;
  711. switch (urb->status) {
  712. case 0:
  713. /* success */
  714. break;
  715. case -ECONNRESET:
  716. case -ENOENT:
  717. case -ESHUTDOWN:
  718. /* this urb is terminated, clean up */
  719. dev_dbg(&port->dev, "urb shutting down with status: %d\n",
  720. urb->status);
  721. priv->write_urb_in_use = 0;
  722. return;
  723. default:
  724. /* error in the urb, so we have to resubmit it */
  725. dbg("%s - Overflow in write", __func__);
  726. dbg("%s - nonzero write bulk status received: %d",
  727. __func__, urb->status);
  728. port->write_urb->transfer_buffer_length = 1;
  729. port->write_urb->dev = port->serial->dev;
  730. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  731. if (result)
  732. dev_dbg(&port->dev,
  733. "failed resubmitting write urb %d\n", result);
  734. else
  735. return;
  736. }
  737. priv->write_urb_in_use = 0;
  738. /* send any buffered data */
  739. spcp8x5_send(port);
  740. }
  741. /* write data to ring buffer. and then start the write transfer */
  742. static int spcp8x5_write(struct usb_serial_port *port,
  743. const unsigned char *buf, int count)
  744. {
  745. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  746. unsigned long flags;
  747. dev_dbg(&port->dev, "%d bytes\n", count);
  748. if (!count)
  749. return count;
  750. spin_lock_irqsave(&priv->lock, flags);
  751. count = put_ringbuf(priv->buf, buf, count);
  752. spin_unlock_irqrestore(&priv->lock, flags);
  753. spcp8x5_send(port);
  754. return count;
  755. }
  756. static int spcp8x5_wait_modem_info(struct usb_serial_port *port,
  757. unsigned int arg)
  758. {
  759. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  760. unsigned long flags;
  761. unsigned int prevstatus;
  762. unsigned int status;
  763. unsigned int changed;
  764. spin_lock_irqsave(&priv->lock, flags);
  765. prevstatus = priv->line_status;
  766. spin_unlock_irqrestore(&priv->lock, flags);
  767. while (1) {
  768. /* wake up in bulk read */
  769. interruptible_sleep_on(&priv->delta_msr_wait);
  770. /* see if a signal did it */
  771. if (signal_pending(current))
  772. return -ERESTARTSYS;
  773. spin_lock_irqsave(&priv->lock, flags);
  774. status = priv->line_status;
  775. spin_unlock_irqrestore(&priv->lock, flags);
  776. changed = prevstatus^status;
  777. if (((arg & TIOCM_RNG) && (changed & MSR_STATUS_LINE_RI)) ||
  778. ((arg & TIOCM_DSR) && (changed & MSR_STATUS_LINE_DSR)) ||
  779. ((arg & TIOCM_CD) && (changed & MSR_STATUS_LINE_DCD)) ||
  780. ((arg & TIOCM_CTS) && (changed & MSR_STATUS_LINE_CTS)))
  781. return 0;
  782. prevstatus = status;
  783. }
  784. /* NOTREACHED */
  785. return 0;
  786. }
  787. static int spcp8x5_ioctl(struct usb_serial_port *port, struct file *file,
  788. unsigned int cmd, unsigned long arg)
  789. {
  790. dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
  791. switch (cmd) {
  792. case TIOCMIWAIT:
  793. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  794. return spcp8x5_wait_modem_info(port, arg);
  795. default:
  796. dbg("%s not supported = 0x%04x", __func__, cmd);
  797. break;
  798. }
  799. return -ENOIOCTLCMD;
  800. }
  801. static int spcp8x5_tiocmset(struct usb_serial_port *port, struct file *file,
  802. unsigned int set, unsigned int clear)
  803. {
  804. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  805. unsigned long flags;
  806. u8 control;
  807. spin_lock_irqsave(&priv->lock, flags);
  808. if (set & TIOCM_RTS)
  809. priv->line_control |= MCR_RTS;
  810. if (set & TIOCM_DTR)
  811. priv->line_control |= MCR_DTR;
  812. if (clear & TIOCM_RTS)
  813. priv->line_control &= ~MCR_RTS;
  814. if (clear & TIOCM_DTR)
  815. priv->line_control &= ~MCR_DTR;
  816. control = priv->line_control;
  817. spin_unlock_irqrestore(&priv->lock, flags);
  818. return spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type);
  819. }
  820. static int spcp8x5_tiocmget(struct usb_serial_port *port, struct file *file)
  821. {
  822. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  823. unsigned long flags;
  824. unsigned int mcr;
  825. unsigned int status;
  826. unsigned int result;
  827. spin_lock_irqsave(&priv->lock, flags);
  828. mcr = priv->line_control;
  829. status = priv->line_status;
  830. spin_unlock_irqrestore(&priv->lock, flags);
  831. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  832. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  833. | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0)
  834. | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0)
  835. | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0)
  836. | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0);
  837. return result;
  838. }
  839. /* get the avail space room in ring buffer */
  840. static int spcp8x5_write_room(struct usb_serial_port *port)
  841. {
  842. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  843. int room = 0;
  844. unsigned long flags;
  845. spin_lock_irqsave(&priv->lock, flags);
  846. room = ringbuf_avail_space(priv->buf);
  847. spin_unlock_irqrestore(&priv->lock, flags);
  848. return room;
  849. }
  850. /* get the number of avail data in write ring buffer */
  851. static int spcp8x5_chars_in_buffer(struct usb_serial_port *port)
  852. {
  853. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  854. int chars = 0;
  855. unsigned long flags;
  856. spin_lock_irqsave(&priv->lock, flags);
  857. chars = ringbuf_avail_data(priv->buf);
  858. spin_unlock_irqrestore(&priv->lock, flags);
  859. return chars;
  860. }
  861. /* All of the device info needed for the spcp8x5 SIO serial converter */
  862. static struct usb_serial_driver spcp8x5_device = {
  863. .driver = {
  864. .owner = THIS_MODULE,
  865. .name = "SPCP8x5",
  866. },
  867. .id_table = id_table,
  868. .num_ports = 1,
  869. .open = spcp8x5_open,
  870. .close = spcp8x5_close,
  871. .write = spcp8x5_write,
  872. .set_termios = spcp8x5_set_termios,
  873. .ioctl = spcp8x5_ioctl,
  874. .tiocmget = spcp8x5_tiocmget,
  875. .tiocmset = spcp8x5_tiocmset,
  876. .write_room = spcp8x5_write_room,
  877. .read_bulk_callback = spcp8x5_read_bulk_callback,
  878. .write_bulk_callback = spcp8x5_write_bulk_callback,
  879. .chars_in_buffer = spcp8x5_chars_in_buffer,
  880. .attach = spcp8x5_startup,
  881. .shutdown = spcp8x5_shutdown,
  882. };
  883. static int __init spcp8x5_init(void)
  884. {
  885. int retval;
  886. retval = usb_serial_register(&spcp8x5_device);
  887. if (retval)
  888. goto failed_usb_serial_register;
  889. retval = usb_register(&spcp8x5_driver);
  890. if (retval)
  891. goto failed_usb_register;
  892. info(DRIVER_DESC " " DRIVER_VERSION);
  893. return 0;
  894. failed_usb_register:
  895. usb_serial_deregister(&spcp8x5_device);
  896. failed_usb_serial_register:
  897. return retval;
  898. }
  899. static void __exit spcp8x5_exit(void)
  900. {
  901. usb_deregister(&spcp8x5_driver);
  902. usb_serial_deregister(&spcp8x5_device);
  903. }
  904. module_init(spcp8x5_init);
  905. module_exit(spcp8x5_exit);
  906. MODULE_DESCRIPTION(DRIVER_DESC);
  907. MODULE_VERSION(DRIVER_VERSION);
  908. MODULE_LICENSE("GPL");
  909. module_param(debug, bool, S_IRUGO | S_IWUSR);
  910. MODULE_PARM_DESC(debug, "Debug enabled or not");