spcp8x5.c 27 KB

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