pl2303.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. /*
  2. * Prolific PL2303 USB to serial adaptor driver
  3. *
  4. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2003 IBM Corp.
  6. *
  7. * Original driver for 2.2.x by anonymous
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this driver
  15. *
  16. * 2002_Mar_26 gkh
  17. * allowed driver to work properly if there is no tty assigned to a port
  18. * (this happens for serial console devices.)
  19. *
  20. * 2001_Oct_06 gkh
  21. * Added RTS and DTR line control. Thanks to joe@bndlg.de for parts of it.
  22. *
  23. * 2001_Sep_19 gkh
  24. * Added break support.
  25. *
  26. * 2001_Aug_30 gkh
  27. * fixed oops in write_bulk_callback.
  28. *
  29. * 2001_Aug_28 gkh
  30. * reworked buffer logic to be like other usb-serial drivers. Hopefully
  31. * removing some reported problems.
  32. *
  33. * 2001_Jun_06 gkh
  34. * finished porting to 2.4 format.
  35. *
  36. */
  37. #include <linux/config.h>
  38. #include <linux/kernel.h>
  39. #include <linux/errno.h>
  40. #include <linux/init.h>
  41. #include <linux/slab.h>
  42. #include <linux/tty.h>
  43. #include <linux/tty_driver.h>
  44. #include <linux/tty_flip.h>
  45. #include <linux/serial.h>
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/spinlock.h>
  49. #include <asm/uaccess.h>
  50. #include <linux/usb.h>
  51. #include "usb-serial.h"
  52. #include "pl2303.h"
  53. /*
  54. * Version Information
  55. */
  56. #define DRIVER_VERSION "v0.12"
  57. #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
  58. static int debug;
  59. #define PL2303_CLOSING_WAIT (30*HZ)
  60. #define PL2303_BUF_SIZE 1024
  61. #define PL2303_TMP_BUF_SIZE 1024
  62. static DECLARE_MUTEX(pl2303_tmp_buf_sem);
  63. struct pl2303_buf {
  64. unsigned int buf_size;
  65. char *buf_buf;
  66. char *buf_get;
  67. char *buf_put;
  68. };
  69. static struct usb_device_id id_table [] = {
  70. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
  71. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
  72. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
  73. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
  74. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
  75. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
  76. { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
  77. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
  78. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
  79. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
  80. { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
  81. { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
  82. { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
  83. { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
  84. { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
  85. { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
  86. { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
  87. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
  88. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
  89. { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
  90. { } /* Terminating entry */
  91. };
  92. MODULE_DEVICE_TABLE (usb, id_table);
  93. static struct usb_driver pl2303_driver = {
  94. .owner = THIS_MODULE,
  95. .name = "pl2303",
  96. .probe = usb_serial_probe,
  97. .disconnect = usb_serial_disconnect,
  98. .id_table = id_table,
  99. };
  100. #define SET_LINE_REQUEST_TYPE 0x21
  101. #define SET_LINE_REQUEST 0x20
  102. #define SET_CONTROL_REQUEST_TYPE 0x21
  103. #define SET_CONTROL_REQUEST 0x22
  104. #define CONTROL_DTR 0x01
  105. #define CONTROL_RTS 0x02
  106. #define BREAK_REQUEST_TYPE 0x21
  107. #define BREAK_REQUEST 0x23
  108. #define BREAK_ON 0xffff
  109. #define BREAK_OFF 0x0000
  110. #define GET_LINE_REQUEST_TYPE 0xa1
  111. #define GET_LINE_REQUEST 0x21
  112. #define VENDOR_WRITE_REQUEST_TYPE 0x40
  113. #define VENDOR_WRITE_REQUEST 0x01
  114. #define VENDOR_READ_REQUEST_TYPE 0xc0
  115. #define VENDOR_READ_REQUEST 0x01
  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. /* function prototypes for a PL2303 serial converter */
  127. static int pl2303_open (struct usb_serial_port *port, struct file *filp);
  128. static void pl2303_close (struct usb_serial_port *port, struct file *filp);
  129. static void pl2303_set_termios (struct usb_serial_port *port,
  130. struct termios *old);
  131. static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
  132. unsigned int cmd, unsigned long arg);
  133. static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
  134. static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  135. static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  136. static int pl2303_write (struct usb_serial_port *port,
  137. const unsigned char *buf, int count);
  138. static void pl2303_send (struct usb_serial_port *port);
  139. static int pl2303_write_room(struct usb_serial_port *port);
  140. static int pl2303_chars_in_buffer(struct usb_serial_port *port);
  141. static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
  142. static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
  143. static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
  144. unsigned int set, unsigned int clear);
  145. static int pl2303_startup (struct usb_serial *serial);
  146. static void pl2303_shutdown (struct usb_serial *serial);
  147. static struct pl2303_buf *pl2303_buf_alloc(unsigned int size);
  148. static void pl2303_buf_free(struct pl2303_buf *pb);
  149. static void pl2303_buf_clear(struct pl2303_buf *pb);
  150. static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb);
  151. static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb);
  152. static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
  153. unsigned int count);
  154. static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
  155. unsigned int count);
  156. /* All of the device info needed for the PL2303 SIO serial converter */
  157. static struct usb_serial_device_type pl2303_device = {
  158. .owner = THIS_MODULE,
  159. .name = "PL-2303",
  160. .id_table = id_table,
  161. .num_interrupt_in = NUM_DONT_CARE,
  162. .num_bulk_in = 1,
  163. .num_bulk_out = 1,
  164. .num_ports = 1,
  165. .open = pl2303_open,
  166. .close = pl2303_close,
  167. .write = pl2303_write,
  168. .ioctl = pl2303_ioctl,
  169. .break_ctl = pl2303_break_ctl,
  170. .set_termios = pl2303_set_termios,
  171. .tiocmget = pl2303_tiocmget,
  172. .tiocmset = pl2303_tiocmset,
  173. .read_bulk_callback = pl2303_read_bulk_callback,
  174. .read_int_callback = pl2303_read_int_callback,
  175. .write_bulk_callback = pl2303_write_bulk_callback,
  176. .write_room = pl2303_write_room,
  177. .chars_in_buffer = pl2303_chars_in_buffer,
  178. .attach = pl2303_startup,
  179. .shutdown = pl2303_shutdown,
  180. };
  181. enum pl2303_type {
  182. type_0, /* don't know the difference between type 0 and */
  183. type_1, /* type 1, until someone from prolific tells us... */
  184. HX, /* HX version of the pl2303 chip */
  185. };
  186. struct pl2303_private {
  187. spinlock_t lock;
  188. struct pl2303_buf *buf;
  189. int write_urb_in_use;
  190. wait_queue_head_t delta_msr_wait;
  191. u8 line_control;
  192. u8 line_status;
  193. u8 termios_initialized;
  194. enum pl2303_type type;
  195. };
  196. static int pl2303_startup (struct usb_serial *serial)
  197. {
  198. struct pl2303_private *priv;
  199. enum pl2303_type type = type_0;
  200. int i;
  201. if (serial->dev->descriptor.bDeviceClass == 0x02)
  202. type = type_0;
  203. else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
  204. type = HX;
  205. else if (serial->dev->descriptor.bDeviceClass == 0x00)
  206. type = type_1;
  207. else if (serial->dev->descriptor.bDeviceClass == 0xFF)
  208. type = type_1;
  209. dbg("device type: %d", type);
  210. for (i = 0; i < serial->num_ports; ++i) {
  211. priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL);
  212. if (!priv)
  213. goto cleanup;
  214. memset (priv, 0x00, sizeof (struct pl2303_private));
  215. spin_lock_init(&priv->lock);
  216. priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
  217. if (priv->buf == NULL) {
  218. kfree(priv);
  219. goto cleanup;
  220. }
  221. init_waitqueue_head(&priv->delta_msr_wait);
  222. priv->type = type;
  223. usb_set_serial_port_data(serial->port[i], priv);
  224. }
  225. return 0;
  226. cleanup:
  227. for (--i; i>=0; --i) {
  228. priv = usb_get_serial_port_data(serial->port[i]);
  229. pl2303_buf_free(priv->buf);
  230. kfree(priv);
  231. usb_set_serial_port_data(serial->port[i], NULL);
  232. }
  233. return -ENOMEM;
  234. }
  235. static int set_control_lines (struct usb_device *dev, u8 value)
  236. {
  237. int retval;
  238. retval = usb_control_msg (dev, usb_sndctrlpipe (dev, 0),
  239. SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
  240. value, 0, NULL, 0, 100);
  241. dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
  242. return retval;
  243. }
  244. static int pl2303_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  245. {
  246. struct pl2303_private *priv = usb_get_serial_port_data(port);
  247. unsigned long flags;
  248. dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
  249. if (!count)
  250. return count;
  251. spin_lock_irqsave(&priv->lock, flags);
  252. count = pl2303_buf_put(priv->buf, buf, count);
  253. spin_unlock_irqrestore(&priv->lock, flags);
  254. pl2303_send(port);
  255. return count;
  256. }
  257. static void pl2303_send(struct usb_serial_port *port)
  258. {
  259. int count, result;
  260. struct pl2303_private *priv = usb_get_serial_port_data(port);
  261. unsigned long flags;
  262. dbg("%s - port %d", __FUNCTION__, port->number);
  263. spin_lock_irqsave(&priv->lock, flags);
  264. if (priv->write_urb_in_use) {
  265. spin_unlock_irqrestore(&priv->lock, flags);
  266. return;
  267. }
  268. count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer,
  269. port->bulk_out_size);
  270. if (count == 0) {
  271. spin_unlock_irqrestore(&priv->lock, flags);
  272. return;
  273. }
  274. priv->write_urb_in_use = 1;
  275. spin_unlock_irqrestore(&priv->lock, flags);
  276. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
  277. port->write_urb->transfer_buffer_length = count;
  278. port->write_urb->dev = port->serial->dev;
  279. result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
  280. if (result) {
  281. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  282. priv->write_urb_in_use = 0;
  283. // TODO: reschedule pl2303_send
  284. }
  285. schedule_work(&port->work);
  286. }
  287. static int pl2303_write_room(struct usb_serial_port *port)
  288. {
  289. struct pl2303_private *priv = usb_get_serial_port_data(port);
  290. int room = 0;
  291. unsigned long flags;
  292. dbg("%s - port %d", __FUNCTION__, port->number);
  293. spin_lock_irqsave(&priv->lock, flags);
  294. room = pl2303_buf_space_avail(priv->buf);
  295. spin_unlock_irqrestore(&priv->lock, flags);
  296. dbg("%s - returns %d", __FUNCTION__, room);
  297. return room;
  298. }
  299. static int pl2303_chars_in_buffer(struct usb_serial_port *port)
  300. {
  301. struct pl2303_private *priv = usb_get_serial_port_data(port);
  302. int chars = 0;
  303. unsigned long flags;
  304. dbg("%s - port %d", __FUNCTION__, port->number);
  305. spin_lock_irqsave(&priv->lock, flags);
  306. chars = pl2303_buf_data_avail(priv->buf);
  307. spin_unlock_irqrestore(&priv->lock, flags);
  308. dbg("%s - returns %d", __FUNCTION__, chars);
  309. return chars;
  310. }
  311. static void pl2303_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  312. {
  313. struct usb_serial *serial = port->serial;
  314. struct pl2303_private *priv = usb_get_serial_port_data(port);
  315. unsigned long flags;
  316. unsigned int cflag;
  317. unsigned char *buf;
  318. int baud;
  319. int i;
  320. u8 control;
  321. dbg("%s - port %d", __FUNCTION__, port->number);
  322. if ((!port->tty) || (!port->tty->termios)) {
  323. dbg("%s - no tty structures", __FUNCTION__);
  324. return;
  325. }
  326. spin_lock_irqsave(&priv->lock, flags);
  327. if (!priv->termios_initialized) {
  328. *(port->tty->termios) = tty_std_termios;
  329. port->tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  330. priv->termios_initialized = 1;
  331. }
  332. spin_unlock_irqrestore(&priv->lock, flags);
  333. cflag = port->tty->termios->c_cflag;
  334. /* check that they really want us to change something */
  335. if (old_termios) {
  336. if ((cflag == old_termios->c_cflag) &&
  337. (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
  338. dbg("%s - nothing to change...", __FUNCTION__);
  339. return;
  340. }
  341. }
  342. buf = kmalloc (7, GFP_KERNEL);
  343. if (!buf) {
  344. dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
  345. return;
  346. }
  347. memset (buf, 0x00, 0x07);
  348. i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
  349. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  350. 0, 0, buf, 7, 100);
  351. dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
  352. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
  353. if (cflag & CSIZE) {
  354. switch (cflag & CSIZE) {
  355. case CS5: buf[6] = 5; break;
  356. case CS6: buf[6] = 6; break;
  357. case CS7: buf[6] = 7; break;
  358. default:
  359. case CS8: buf[6] = 8; break;
  360. }
  361. dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
  362. }
  363. baud = 0;
  364. switch (cflag & CBAUD) {
  365. case B0: baud = 0; break;
  366. case B75: baud = 75; break;
  367. case B150: baud = 150; break;
  368. case B300: baud = 300; break;
  369. case B600: baud = 600; break;
  370. case B1200: baud = 1200; break;
  371. case B1800: baud = 1800; break;
  372. case B2400: baud = 2400; break;
  373. case B4800: baud = 4800; break;
  374. case B9600: baud = 9600; break;
  375. case B19200: baud = 19200; break;
  376. case B38400: baud = 38400; break;
  377. case B57600: baud = 57600; break;
  378. case B115200: baud = 115200; break;
  379. case B230400: baud = 230400; break;
  380. case B460800: baud = 460800; break;
  381. default:
  382. dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
  383. break;
  384. }
  385. dbg("%s - baud = %d", __FUNCTION__, baud);
  386. if (baud) {
  387. buf[0] = baud & 0xff;
  388. buf[1] = (baud >> 8) & 0xff;
  389. buf[2] = (baud >> 16) & 0xff;
  390. buf[3] = (baud >> 24) & 0xff;
  391. }
  392. /* For reference buf[4]=0 is 1 stop bits */
  393. /* For reference buf[4]=1 is 1.5 stop bits */
  394. /* For reference buf[4]=2 is 2 stop bits */
  395. if (cflag & CSTOPB) {
  396. buf[4] = 2;
  397. dbg("%s - stop bits = 2", __FUNCTION__);
  398. } else {
  399. buf[4] = 0;
  400. dbg("%s - stop bits = 1", __FUNCTION__);
  401. }
  402. if (cflag & PARENB) {
  403. /* For reference buf[5]=0 is none parity */
  404. /* For reference buf[5]=1 is odd parity */
  405. /* For reference buf[5]=2 is even parity */
  406. /* For reference buf[5]=3 is mark parity */
  407. /* For reference buf[5]=4 is space parity */
  408. if (cflag & PARODD) {
  409. buf[5] = 1;
  410. dbg("%s - parity = odd", __FUNCTION__);
  411. } else {
  412. buf[5] = 2;
  413. dbg("%s - parity = even", __FUNCTION__);
  414. }
  415. } else {
  416. buf[5] = 0;
  417. dbg("%s - parity = none", __FUNCTION__);
  418. }
  419. i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
  420. SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
  421. 0, 0, buf, 7, 100);
  422. dbg ("0x21:0x20:0:0 %d", i);
  423. /* change control lines if we are switching to or from B0 */
  424. spin_lock_irqsave(&priv->lock, flags);
  425. control = priv->line_control;
  426. if ((cflag & CBAUD) == B0)
  427. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  428. else
  429. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  430. if (control != priv->line_control) {
  431. control = priv->line_control;
  432. spin_unlock_irqrestore(&priv->lock, flags);
  433. set_control_lines(serial->dev, control);
  434. } else {
  435. spin_unlock_irqrestore(&priv->lock, flags);
  436. }
  437. buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
  438. i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
  439. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  440. 0, 0, buf, 7, 100);
  441. dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
  442. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
  443. if (cflag & CRTSCTS) {
  444. __u16 index;
  445. if (priv->type == HX)
  446. index = 0x61;
  447. else
  448. index = 0x41;
  449. i = usb_control_msg(serial->dev,
  450. usb_sndctrlpipe(serial->dev, 0),
  451. VENDOR_WRITE_REQUEST,
  452. VENDOR_WRITE_REQUEST_TYPE,
  453. 0x0, index, NULL, 0, 100);
  454. dbg ("0x40:0x1:0x0:0x%x %d", index, i);
  455. }
  456. kfree (buf);
  457. }
  458. static int pl2303_open (struct usb_serial_port *port, struct file *filp)
  459. {
  460. struct termios tmp_termios;
  461. struct usb_serial *serial = port->serial;
  462. struct pl2303_private *priv = usb_get_serial_port_data(port);
  463. unsigned char *buf;
  464. int result;
  465. dbg("%s - port %d", __FUNCTION__, port->number);
  466. if (priv->type != HX) {
  467. usb_clear_halt(serial->dev, port->write_urb->pipe);
  468. usb_clear_halt(serial->dev, port->read_urb->pipe);
  469. }
  470. buf = kmalloc(10, GFP_KERNEL);
  471. if (buf==NULL)
  472. return -ENOMEM;
  473. #define FISH(a,b,c,d) \
  474. result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
  475. b, a, c, d, buf, 1, 100); \
  476. dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
  477. #define SOUP(a,b,c,d) \
  478. result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
  479. b, a, c, d, NULL, 0, 100); \
  480. dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
  481. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  482. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
  483. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  484. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
  485. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  486. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
  487. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  488. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
  489. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
  490. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0);
  491. if (priv->type == HX) {
  492. /* HX chip */
  493. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44);
  494. /* reset upstream data pipes */
  495. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0);
  496. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0);
  497. } else {
  498. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
  499. }
  500. kfree(buf);
  501. /* Setup termios */
  502. if (port->tty) {
  503. pl2303_set_termios (port, &tmp_termios);
  504. }
  505. //FIXME: need to assert RTS and DTR if CRTSCTS off
  506. dbg("%s - submitting read urb", __FUNCTION__);
  507. port->read_urb->dev = serial->dev;
  508. result = usb_submit_urb (port->read_urb, GFP_KERNEL);
  509. if (result) {
  510. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  511. pl2303_close (port, NULL);
  512. return -EPROTO;
  513. }
  514. dbg("%s - submitting interrupt urb", __FUNCTION__);
  515. port->interrupt_in_urb->dev = serial->dev;
  516. result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
  517. if (result) {
  518. dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
  519. pl2303_close (port, NULL);
  520. return -EPROTO;
  521. }
  522. return 0;
  523. }
  524. static void pl2303_close (struct usb_serial_port *port, struct file *filp)
  525. {
  526. struct pl2303_private *priv = usb_get_serial_port_data(port);
  527. unsigned long flags;
  528. unsigned int c_cflag;
  529. int bps;
  530. long timeout;
  531. wait_queue_t wait; \
  532. dbg("%s - port %d", __FUNCTION__, port->number);
  533. /* wait for data to drain from the buffer */
  534. spin_lock_irqsave(&priv->lock, flags);
  535. timeout = PL2303_CLOSING_WAIT;
  536. init_waitqueue_entry(&wait, current);
  537. add_wait_queue(&port->tty->write_wait, &wait);
  538. for (;;) {
  539. set_current_state(TASK_INTERRUPTIBLE);
  540. if (pl2303_buf_data_avail(priv->buf) == 0
  541. || timeout == 0 || signal_pending(current)
  542. || !usb_get_intfdata(port->serial->interface)) /* disconnect */
  543. break;
  544. spin_unlock_irqrestore(&priv->lock, flags);
  545. timeout = schedule_timeout(timeout);
  546. spin_lock_irqsave(&priv->lock, flags);
  547. }
  548. set_current_state(TASK_RUNNING);
  549. remove_wait_queue(&port->tty->write_wait, &wait);
  550. /* clear out any remaining data in the buffer */
  551. pl2303_buf_clear(priv->buf);
  552. spin_unlock_irqrestore(&priv->lock, flags);
  553. /* wait for characters to drain from the device */
  554. /* (this is long enough for the entire 256 byte */
  555. /* pl2303 hardware buffer to drain with no flow */
  556. /* control for data rates of 1200 bps or more, */
  557. /* for lower rates we should really know how much */
  558. /* data is in the buffer to compute a delay */
  559. /* that is not unnecessarily long) */
  560. bps = tty_get_baud_rate(port->tty);
  561. if (bps > 1200)
  562. timeout = max((HZ*2560)/bps,HZ/10);
  563. else
  564. timeout = 2*HZ;
  565. set_current_state(TASK_INTERRUPTIBLE);
  566. schedule_timeout(timeout);
  567. /* shutdown our urbs */
  568. dbg("%s - shutting down urbs", __FUNCTION__);
  569. usb_kill_urb(port->write_urb);
  570. usb_kill_urb(port->read_urb);
  571. usb_kill_urb(port->interrupt_in_urb);
  572. if (port->tty) {
  573. c_cflag = port->tty->termios->c_cflag;
  574. if (c_cflag & HUPCL) {
  575. /* drop DTR and RTS */
  576. spin_lock_irqsave(&priv->lock, flags);
  577. priv->line_control = 0;
  578. spin_unlock_irqrestore (&priv->lock, flags);
  579. set_control_lines (port->serial->dev, 0);
  580. }
  581. }
  582. }
  583. static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
  584. unsigned int set, unsigned int clear)
  585. {
  586. struct pl2303_private *priv = usb_get_serial_port_data(port);
  587. unsigned long flags;
  588. u8 control;
  589. if (!usb_get_intfdata(port->serial->interface))
  590. return -ENODEV;
  591. spin_lock_irqsave (&priv->lock, flags);
  592. if (set & TIOCM_RTS)
  593. priv->line_control |= CONTROL_RTS;
  594. if (set & TIOCM_DTR)
  595. priv->line_control |= CONTROL_DTR;
  596. if (clear & TIOCM_RTS)
  597. priv->line_control &= ~CONTROL_RTS;
  598. if (clear & TIOCM_DTR)
  599. priv->line_control &= ~CONTROL_DTR;
  600. control = priv->line_control;
  601. spin_unlock_irqrestore (&priv->lock, flags);
  602. return set_control_lines (port->serial->dev, control);
  603. }
  604. static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
  605. {
  606. struct pl2303_private *priv = usb_get_serial_port_data(port);
  607. unsigned long flags;
  608. unsigned int mcr;
  609. unsigned int status;
  610. unsigned int result;
  611. dbg("%s (%d)", __FUNCTION__, port->number);
  612. if (!usb_get_intfdata(port->serial->interface))
  613. return -ENODEV;
  614. spin_lock_irqsave (&priv->lock, flags);
  615. mcr = priv->line_control;
  616. status = priv->line_status;
  617. spin_unlock_irqrestore (&priv->lock, flags);
  618. result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
  619. | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
  620. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  621. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  622. | ((status & UART_RING) ? TIOCM_RI : 0)
  623. | ((status & UART_DCD) ? TIOCM_CD : 0);
  624. dbg("%s - result = %x", __FUNCTION__, result);
  625. return result;
  626. }
  627. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  628. {
  629. struct pl2303_private *priv = usb_get_serial_port_data(port);
  630. unsigned long flags;
  631. unsigned int prevstatus;
  632. unsigned int status;
  633. unsigned int changed;
  634. spin_lock_irqsave (&priv->lock, flags);
  635. prevstatus = priv->line_status;
  636. spin_unlock_irqrestore (&priv->lock, flags);
  637. while (1) {
  638. interruptible_sleep_on(&priv->delta_msr_wait);
  639. /* see if a signal did it */
  640. if (signal_pending(current))
  641. return -ERESTARTSYS;
  642. spin_lock_irqsave (&priv->lock, flags);
  643. status = priv->line_status;
  644. spin_unlock_irqrestore (&priv->lock, flags);
  645. changed=prevstatus^status;
  646. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  647. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  648. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  649. ((arg & TIOCM_CTS) && (changed & UART_CTS)) ) {
  650. return 0;
  651. }
  652. prevstatus = status;
  653. }
  654. /* NOTREACHED */
  655. return 0;
  656. }
  657. static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
  658. {
  659. dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
  660. switch (cmd) {
  661. case TIOCMIWAIT:
  662. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  663. return wait_modem_info(port, arg);
  664. default:
  665. dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
  666. break;
  667. }
  668. return -ENOIOCTLCMD;
  669. }
  670. static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
  671. {
  672. struct usb_serial *serial = port->serial;
  673. u16 state;
  674. int result;
  675. dbg("%s - port %d", __FUNCTION__, port->number);
  676. if (break_state == 0)
  677. state = BREAK_OFF;
  678. else
  679. state = BREAK_ON;
  680. dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on");
  681. result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
  682. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  683. 0, NULL, 0, 100);
  684. if (result)
  685. dbg("%s - error sending break = %d", __FUNCTION__, result);
  686. }
  687. static void pl2303_shutdown (struct usb_serial *serial)
  688. {
  689. int i;
  690. struct pl2303_private *priv;
  691. dbg("%s", __FUNCTION__);
  692. for (i = 0; i < serial->num_ports; ++i) {
  693. priv = usb_get_serial_port_data(serial->port[i]);
  694. if (priv) {
  695. pl2303_buf_free(priv->buf);
  696. kfree(priv);
  697. usb_set_serial_port_data(serial->port[i], NULL);
  698. }
  699. }
  700. }
  701. static void pl2303_update_line_status(struct usb_serial_port *port,
  702. unsigned char *data,
  703. unsigned int actual_length)
  704. {
  705. struct pl2303_private *priv = usb_get_serial_port_data(port);
  706. unsigned long flags;
  707. u8 status_idx = UART_STATE;
  708. u8 length = UART_STATE;
  709. if ((le16_to_cpu(port->serial->dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
  710. (le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X65)) {
  711. length = 1;
  712. status_idx = 0;
  713. }
  714. if (actual_length < length)
  715. goto exit;
  716. /* Save off the uart status for others to look at */
  717. spin_lock_irqsave(&priv->lock, flags);
  718. priv->line_status = data[status_idx];
  719. spin_unlock_irqrestore(&priv->lock, flags);
  720. exit:
  721. return;
  722. }
  723. static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
  724. {
  725. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  726. unsigned char *data = urb->transfer_buffer;
  727. unsigned int actual_length = urb->actual_length;
  728. int status;
  729. dbg("%s (%d)", __FUNCTION__, port->number);
  730. switch (urb->status) {
  731. case 0:
  732. /* success */
  733. break;
  734. case -ECONNRESET:
  735. case -ENOENT:
  736. case -ESHUTDOWN:
  737. /* this urb is terminated, clean up */
  738. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  739. return;
  740. default:
  741. dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
  742. goto exit;
  743. }
  744. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
  745. pl2303_update_line_status(port, data, actual_length);
  746. exit:
  747. status = usb_submit_urb (urb, GFP_ATOMIC);
  748. if (status)
  749. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
  750. __FUNCTION__, status);
  751. }
  752. static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  753. {
  754. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  755. struct pl2303_private *priv = usb_get_serial_port_data(port);
  756. struct tty_struct *tty;
  757. unsigned char *data = urb->transfer_buffer;
  758. unsigned long flags;
  759. int i;
  760. int result;
  761. u8 status;
  762. char tty_flag;
  763. dbg("%s - port %d", __FUNCTION__, port->number);
  764. if (urb->status) {
  765. dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
  766. if (!port->open_count) {
  767. dbg("%s - port is closed, exiting.", __FUNCTION__);
  768. return;
  769. }
  770. if (urb->status == -EPROTO) {
  771. /* PL2303 mysteriously fails with -EPROTO reschedule the read */
  772. dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
  773. urb->status = 0;
  774. urb->dev = port->serial->dev;
  775. result = usb_submit_urb(urb, GFP_ATOMIC);
  776. if (result)
  777. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  778. return;
  779. }
  780. dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
  781. return;
  782. }
  783. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  784. /* get tty_flag from status */
  785. tty_flag = TTY_NORMAL;
  786. spin_lock_irqsave(&priv->lock, flags);
  787. status = priv->line_status;
  788. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  789. spin_unlock_irqrestore(&priv->lock, flags);
  790. wake_up_interruptible (&priv->delta_msr_wait);
  791. /* break takes precedence over parity, */
  792. /* which takes precedence over framing errors */
  793. if (status & UART_BREAK_ERROR )
  794. tty_flag = TTY_BREAK;
  795. else if (status & UART_PARITY_ERROR)
  796. tty_flag = TTY_PARITY;
  797. else if (status & UART_FRAME_ERROR)
  798. tty_flag = TTY_FRAME;
  799. dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
  800. tty = port->tty;
  801. if (tty && urb->actual_length) {
  802. /* overrun is special, not associated with a char */
  803. if (status & UART_OVERRUN_ERROR)
  804. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  805. for (i = 0; i < urb->actual_length; ++i) {
  806. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  807. tty_flip_buffer_push(tty);
  808. }
  809. tty_insert_flip_char (tty, data[i], tty_flag);
  810. }
  811. tty_flip_buffer_push (tty);
  812. }
  813. /* Schedule the next read _if_ we are still open */
  814. if (port->open_count) {
  815. urb->dev = port->serial->dev;
  816. result = usb_submit_urb(urb, GFP_ATOMIC);
  817. if (result)
  818. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  819. }
  820. return;
  821. }
  822. static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  823. {
  824. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  825. struct pl2303_private *priv = usb_get_serial_port_data(port);
  826. int result;
  827. dbg("%s - port %d", __FUNCTION__, port->number);
  828. switch (urb->status) {
  829. case 0:
  830. /* success */
  831. break;
  832. case -ECONNRESET:
  833. case -ENOENT:
  834. case -ESHUTDOWN:
  835. /* this urb is terminated, clean up */
  836. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  837. priv->write_urb_in_use = 0;
  838. return;
  839. default:
  840. /* error in the urb, so we have to resubmit it */
  841. dbg("%s - Overflow in write", __FUNCTION__);
  842. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  843. port->write_urb->transfer_buffer_length = 1;
  844. port->write_urb->dev = port->serial->dev;
  845. result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
  846. if (result)
  847. dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
  848. else
  849. return;
  850. }
  851. priv->write_urb_in_use = 0;
  852. /* send any buffered data */
  853. pl2303_send(port);
  854. }
  855. /*
  856. * pl2303_buf_alloc
  857. *
  858. * Allocate a circular buffer and all associated memory.
  859. */
  860. static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
  861. {
  862. struct pl2303_buf *pb;
  863. if (size == 0)
  864. return NULL;
  865. pb = (struct pl2303_buf *)kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
  866. if (pb == NULL)
  867. return NULL;
  868. pb->buf_buf = kmalloc(size, GFP_KERNEL);
  869. if (pb->buf_buf == NULL) {
  870. kfree(pb);
  871. return NULL;
  872. }
  873. pb->buf_size = size;
  874. pb->buf_get = pb->buf_put = pb->buf_buf;
  875. return pb;
  876. }
  877. /*
  878. * pl2303_buf_free
  879. *
  880. * Free the buffer and all associated memory.
  881. */
  882. static void pl2303_buf_free(struct pl2303_buf *pb)
  883. {
  884. if (pb) {
  885. kfree(pb->buf_buf);
  886. kfree(pb);
  887. }
  888. }
  889. /*
  890. * pl2303_buf_clear
  891. *
  892. * Clear out all data in the circular buffer.
  893. */
  894. static void pl2303_buf_clear(struct pl2303_buf *pb)
  895. {
  896. if (pb != NULL)
  897. pb->buf_get = pb->buf_put;
  898. /* equivalent to a get of all data available */
  899. }
  900. /*
  901. * pl2303_buf_data_avail
  902. *
  903. * Return the number of bytes of data available in the circular
  904. * buffer.
  905. */
  906. static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
  907. {
  908. if (pb != NULL)
  909. return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
  910. else
  911. return 0;
  912. }
  913. /*
  914. * pl2303_buf_space_avail
  915. *
  916. * Return the number of bytes of space available in the circular
  917. * buffer.
  918. */
  919. static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
  920. {
  921. if (pb != NULL)
  922. return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
  923. else
  924. return 0;
  925. }
  926. /*
  927. * pl2303_buf_put
  928. *
  929. * Copy data data from a user buffer and put it into the circular buffer.
  930. * Restrict to the amount of space available.
  931. *
  932. * Return the number of bytes copied.
  933. */
  934. static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
  935. unsigned int count)
  936. {
  937. unsigned int len;
  938. if (pb == NULL)
  939. return 0;
  940. len = pl2303_buf_space_avail(pb);
  941. if (count > len)
  942. count = len;
  943. if (count == 0)
  944. return 0;
  945. len = pb->buf_buf + pb->buf_size - pb->buf_put;
  946. if (count > len) {
  947. memcpy(pb->buf_put, buf, len);
  948. memcpy(pb->buf_buf, buf+len, count - len);
  949. pb->buf_put = pb->buf_buf + count - len;
  950. } else {
  951. memcpy(pb->buf_put, buf, count);
  952. if (count < len)
  953. pb->buf_put += count;
  954. else /* count == len */
  955. pb->buf_put = pb->buf_buf;
  956. }
  957. return count;
  958. }
  959. /*
  960. * pl2303_buf_get
  961. *
  962. * Get data from the circular buffer and copy to the given buffer.
  963. * Restrict to the amount of data available.
  964. *
  965. * Return the number of bytes copied.
  966. */
  967. static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
  968. unsigned int count)
  969. {
  970. unsigned int len;
  971. if (pb == NULL)
  972. return 0;
  973. len = pl2303_buf_data_avail(pb);
  974. if (count > len)
  975. count = len;
  976. if (count == 0)
  977. return 0;
  978. len = pb->buf_buf + pb->buf_size - pb->buf_get;
  979. if (count > len) {
  980. memcpy(buf, pb->buf_get, len);
  981. memcpy(buf+len, pb->buf_buf, count - len);
  982. pb->buf_get = pb->buf_buf + count - len;
  983. } else {
  984. memcpy(buf, pb->buf_get, count);
  985. if (count < len)
  986. pb->buf_get += count;
  987. else /* count == len */
  988. pb->buf_get = pb->buf_buf;
  989. }
  990. return count;
  991. }
  992. static int __init pl2303_init (void)
  993. {
  994. int retval;
  995. retval = usb_serial_register(&pl2303_device);
  996. if (retval)
  997. goto failed_usb_serial_register;
  998. retval = usb_register(&pl2303_driver);
  999. if (retval)
  1000. goto failed_usb_register;
  1001. info(DRIVER_DESC " " DRIVER_VERSION);
  1002. return 0;
  1003. failed_usb_register:
  1004. usb_serial_deregister(&pl2303_device);
  1005. failed_usb_serial_register:
  1006. return retval;
  1007. }
  1008. static void __exit pl2303_exit (void)
  1009. {
  1010. usb_deregister (&pl2303_driver);
  1011. usb_serial_deregister (&pl2303_device);
  1012. }
  1013. module_init(pl2303_init);
  1014. module_exit(pl2303_exit);
  1015. MODULE_DESCRIPTION(DRIVER_DESC);
  1016. MODULE_VERSION(DRIVER_VERSION);
  1017. MODULE_LICENSE("GPL");
  1018. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1019. MODULE_PARM_DESC(debug, "Debug enabled or not");