pl2303.c 32 KB

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