oti6858.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
  3. *
  4. * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
  5. * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
  6. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2003 IBM Corp.
  8. *
  9. * Many thanks to the authors of pl2303 driver: all functions in this file
  10. * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
  11. *
  12. * Warning! You use this driver on your own risk! The only official
  13. * description of this device I have is datasheet from manufacturer,
  14. * and it doesn't contain almost any information needed to write a driver.
  15. * Almost all knowlegde used while writing this driver was gathered by:
  16. * - analyzing traffic between device and the M$ Windows 2000 driver,
  17. * - trying different bit combinations and checking pin states
  18. * with a voltmeter,
  19. * - receiving malformed frames and producing buffer overflows
  20. * to learn how errors are reported,
  21. * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
  22. * CONNECTED TO IT!
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License.
  27. *
  28. * See Documentation/usb/usb-serial.txt for more information on using this driver
  29. *
  30. * TODO:
  31. * - implement correct flushing for ioctls and oti6858_close()
  32. * - check how errors (rx overflow, parity error, framing error) are reported
  33. * - implement oti6858_break_ctl()
  34. * - implement more ioctls
  35. * - test/implement flow control
  36. * - allow setting custom baud rates
  37. */
  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 <linux/usb.h>
  50. #include <linux/usb/serial.h>
  51. #include <asm/uaccess.h>
  52. #include "oti6858.h"
  53. #define OTI6858_DESCRIPTION \
  54. "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
  55. #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
  56. #define OTI6858_VERSION "0.1"
  57. static struct usb_device_id id_table [] = {
  58. { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
  59. { }
  60. };
  61. MODULE_DEVICE_TABLE(usb, id_table);
  62. static struct usb_driver oti6858_driver = {
  63. .name = "oti6858",
  64. .probe = usb_serial_probe,
  65. .disconnect = usb_serial_disconnect,
  66. .id_table = id_table,
  67. .no_dynamic_id = 1,
  68. };
  69. static int debug;
  70. /* buffering code, copied from pl2303 driver */
  71. #define PL2303_BUF_SIZE 1024
  72. #define PL2303_TMP_BUF_SIZE 1024
  73. struct oti6858_buf {
  74. unsigned int buf_size;
  75. char *buf_buf;
  76. char *buf_get;
  77. char *buf_put;
  78. };
  79. /* requests */
  80. #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
  81. #define OTI6858_REQ_T_GET_STATUS 0x01
  82. #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
  83. #define OTI6858_REQ_T_SET_LINE 0x00
  84. #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
  85. #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
  86. /* format of the control packet */
  87. struct oti6858_control_pkt {
  88. u16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
  89. #define OTI6858_MAX_BAUD_RATE 3000000
  90. u8 frame_fmt;
  91. #define FMT_STOP_BITS_MASK 0xc0
  92. #define FMT_STOP_BITS_1 0x00
  93. #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
  94. #define FMT_PARITY_MASK 0x38
  95. #define FMT_PARITY_NONE 0x00
  96. #define FMT_PARITY_ODD 0x08
  97. #define FMT_PARITY_EVEN 0x18
  98. #define FMT_PARITY_MARK 0x28
  99. #define FMT_PARITY_SPACE 0x38
  100. #define FMT_DATA_BITS_MASK 0x03
  101. #define FMT_DATA_BITS_5 0x00
  102. #define FMT_DATA_BITS_6 0x01
  103. #define FMT_DATA_BITS_7 0x02
  104. #define FMT_DATA_BITS_8 0x03
  105. u8 something; /* always equals 0x43 */
  106. u8 control; /* settings of flow control lines */
  107. #define CONTROL_MASK 0x0c
  108. #define CONTROL_DTR_HIGH 0x08
  109. #define CONTROL_RTS_HIGH 0x04
  110. u8 tx_status;
  111. #define TX_BUFFER_EMPTIED 0x09
  112. u8 pin_state;
  113. #define PIN_MASK 0x3f
  114. #define PIN_RTS 0x20 /* output pin */
  115. #define PIN_CTS 0x10 /* input pin, active low */
  116. #define PIN_DSR 0x08 /* input pin, active low */
  117. #define PIN_DTR 0x04 /* output pin */
  118. #define PIN_RI 0x02 /* input pin, active low */
  119. #define PIN_DCD 0x01 /* input pin, active low */
  120. u8 rx_bytes_avail; /* number of bytes in rx buffer */;
  121. };
  122. #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
  123. #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
  124. ( ((a)->divisor == (priv)->pending_setup.divisor) \
  125. && ((a)->control == (priv)->pending_setup.control) \
  126. && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt) )
  127. /* function prototypes */
  128. static int oti6858_open(struct usb_serial_port *port, struct file *filp);
  129. static void oti6858_close(struct usb_serial_port *port, struct file *filp);
  130. static void oti6858_set_termios(struct usb_serial_port *port,
  131. struct ktermios *old);
  132. static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
  133. unsigned int cmd, unsigned long arg);
  134. static void oti6858_read_int_callback(struct urb *urb);
  135. static void oti6858_read_bulk_callback(struct urb *urb);
  136. static void oti6858_write_bulk_callback(struct urb *urb);
  137. static int oti6858_write(struct usb_serial_port *port,
  138. const unsigned char *buf, int count);
  139. static int oti6858_write_room(struct usb_serial_port *port);
  140. static void oti6858_break_ctl(struct usb_serial_port *port, int break_state);
  141. static int oti6858_chars_in_buffer(struct usb_serial_port *port);
  142. static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file);
  143. static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
  144. unsigned int set, unsigned int clear);
  145. static int oti6858_startup(struct usb_serial *serial);
  146. static void oti6858_shutdown(struct usb_serial *serial);
  147. /* functions operating on buffers */
  148. static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
  149. static void oti6858_buf_free(struct oti6858_buf *pb);
  150. static void oti6858_buf_clear(struct oti6858_buf *pb);
  151. static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
  152. static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
  153. static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
  154. unsigned int count);
  155. static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
  156. unsigned int count);
  157. /* device info */
  158. static struct usb_serial_driver oti6858_device = {
  159. .driver = {
  160. .owner = THIS_MODULE,
  161. .name = "oti6858",
  162. },
  163. .id_table = id_table,
  164. .num_ports = 1,
  165. .open = oti6858_open,
  166. .close = oti6858_close,
  167. .write = oti6858_write,
  168. .ioctl = oti6858_ioctl,
  169. .break_ctl = oti6858_break_ctl,
  170. .set_termios = oti6858_set_termios,
  171. .tiocmget = oti6858_tiocmget,
  172. .tiocmset = oti6858_tiocmset,
  173. .read_bulk_callback = oti6858_read_bulk_callback,
  174. .read_int_callback = oti6858_read_int_callback,
  175. .write_bulk_callback = oti6858_write_bulk_callback,
  176. .write_room = oti6858_write_room,
  177. .chars_in_buffer = oti6858_chars_in_buffer,
  178. .attach = oti6858_startup,
  179. .shutdown = oti6858_shutdown,
  180. };
  181. struct oti6858_private {
  182. spinlock_t lock;
  183. struct oti6858_buf *buf;
  184. struct oti6858_control_pkt status;
  185. struct {
  186. u8 read_urb_in_use;
  187. u8 write_urb_in_use;
  188. u8 termios_initialized;
  189. } flags;
  190. struct delayed_work delayed_write_work;
  191. struct {
  192. u16 divisor;
  193. u8 frame_fmt;
  194. u8 control;
  195. } pending_setup;
  196. u8 transient;
  197. u8 setup_done;
  198. struct delayed_work delayed_setup_work;
  199. wait_queue_head_t intr_wait;
  200. struct usb_serial_port *port; /* USB port with which associated */
  201. };
  202. #undef dbg
  203. /* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
  204. #define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
  205. static void setup_line(struct work_struct *work)
  206. {
  207. struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_setup_work.work);
  208. struct usb_serial_port *port = priv->port;
  209. struct oti6858_control_pkt *new_setup;
  210. unsigned long flags;
  211. int result;
  212. dbg("%s(port = %d)", __func__, port->number);
  213. if ((new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
  214. dev_err(&port->dev, "%s(): out of memory!\n", __func__);
  215. /* we will try again */
  216. schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
  217. return;
  218. }
  219. result = usb_control_msg(port->serial->dev,
  220. usb_rcvctrlpipe(port->serial->dev, 0),
  221. OTI6858_REQ_T_GET_STATUS,
  222. OTI6858_REQ_GET_STATUS,
  223. 0, 0,
  224. new_setup, OTI6858_CTRL_PKT_SIZE,
  225. 100);
  226. if (result != OTI6858_CTRL_PKT_SIZE) {
  227. dev_err(&port->dev, "%s(): error reading status\n", __func__);
  228. kfree(new_setup);
  229. /* we will try again */
  230. schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
  231. return;
  232. }
  233. spin_lock_irqsave(&priv->lock, flags);
  234. if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
  235. new_setup->divisor = priv->pending_setup.divisor;
  236. new_setup->control = priv->pending_setup.control;
  237. new_setup->frame_fmt = priv->pending_setup.frame_fmt;
  238. spin_unlock_irqrestore(&priv->lock, flags);
  239. result = usb_control_msg(port->serial->dev,
  240. usb_sndctrlpipe(port->serial->dev, 0),
  241. OTI6858_REQ_T_SET_LINE,
  242. OTI6858_REQ_SET_LINE,
  243. 0, 0,
  244. new_setup, OTI6858_CTRL_PKT_SIZE,
  245. 100);
  246. } else {
  247. spin_unlock_irqrestore(&priv->lock, flags);
  248. result = 0;
  249. }
  250. kfree(new_setup);
  251. spin_lock_irqsave(&priv->lock, flags);
  252. if (result != OTI6858_CTRL_PKT_SIZE)
  253. priv->transient = 0;
  254. priv->setup_done = 1;
  255. spin_unlock_irqrestore(&priv->lock, flags);
  256. dbg("%s(): submitting interrupt urb", __func__);
  257. port->interrupt_in_urb->dev = port->serial->dev;
  258. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  259. if (result != 0) {
  260. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  261. " with error %d\n", __func__, result);
  262. }
  263. }
  264. void send_data(struct work_struct *work)
  265. {
  266. struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_write_work.work);
  267. struct usb_serial_port *port = priv->port;
  268. int count = 0, result;
  269. unsigned long flags;
  270. unsigned char allow;
  271. dbg("%s(port = %d)", __func__, port->number);
  272. spin_lock_irqsave(&priv->lock, flags);
  273. if (priv->flags.write_urb_in_use) {
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. schedule_delayed_work(&priv->delayed_write_work, msecs_to_jiffies(2));
  276. return;
  277. }
  278. priv->flags.write_urb_in_use = 1;
  279. count = oti6858_buf_data_avail(priv->buf);
  280. spin_unlock_irqrestore(&priv->lock, flags);
  281. if (count > port->bulk_out_size)
  282. count = port->bulk_out_size;
  283. if (count != 0) {
  284. result = usb_control_msg(port->serial->dev,
  285. usb_rcvctrlpipe(port->serial->dev, 0),
  286. OTI6858_REQ_T_CHECK_TXBUFF,
  287. OTI6858_REQ_CHECK_TXBUFF,
  288. count, 0, &allow, 1, 100);
  289. if (result != 1 || allow != 0)
  290. count = 0;
  291. }
  292. if (count == 0) {
  293. priv->flags.write_urb_in_use = 0;
  294. dbg("%s(): submitting interrupt urb", __func__);
  295. port->interrupt_in_urb->dev = port->serial->dev;
  296. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  297. if (result != 0) {
  298. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  299. " with error %d\n", __func__, result);
  300. }
  301. return;
  302. }
  303. spin_lock_irqsave(&priv->lock, flags);
  304. oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
  305. spin_unlock_irqrestore(&priv->lock, flags);
  306. port->write_urb->transfer_buffer_length = count;
  307. port->write_urb->dev = port->serial->dev;
  308. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  309. if (result != 0) {
  310. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  311. " with error %d\n", __func__, result);
  312. priv->flags.write_urb_in_use = 0;
  313. }
  314. usb_serial_port_softint(port);
  315. }
  316. static int oti6858_startup(struct usb_serial *serial)
  317. {
  318. struct usb_serial_port *port = serial->port[0];
  319. struct oti6858_private *priv;
  320. int i;
  321. for (i = 0; i < serial->num_ports; ++i) {
  322. priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
  323. if (!priv)
  324. break;
  325. priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
  326. if (priv->buf == NULL) {
  327. kfree(priv);
  328. break;
  329. }
  330. spin_lock_init(&priv->lock);
  331. init_waitqueue_head(&priv->intr_wait);
  332. // INIT_WORK(&priv->setup_work, setup_line, serial->port[i]);
  333. // INIT_WORK(&priv->write_work, send_data, serial->port[i]);
  334. priv->port = port;
  335. INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
  336. INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
  337. usb_set_serial_port_data(serial->port[i], priv);
  338. }
  339. if (i == serial->num_ports)
  340. return 0;
  341. for (--i; i >= 0; --i) {
  342. priv = usb_get_serial_port_data(serial->port[i]);
  343. oti6858_buf_free(priv->buf);
  344. kfree(priv);
  345. usb_set_serial_port_data(serial->port[i], NULL);
  346. }
  347. return -ENOMEM;
  348. }
  349. static int oti6858_write(struct usb_serial_port *port,
  350. const unsigned char *buf, int count)
  351. {
  352. struct oti6858_private *priv = usb_get_serial_port_data(port);
  353. unsigned long flags;
  354. dbg("%s(port = %d, count = %d)", __func__, port->number, count);
  355. if (!count)
  356. return count;
  357. spin_lock_irqsave(&priv->lock, flags);
  358. count = oti6858_buf_put(priv->buf, buf, count);
  359. spin_unlock_irqrestore(&priv->lock, flags);
  360. return count;
  361. }
  362. static int oti6858_write_room(struct usb_serial_port *port)
  363. {
  364. struct oti6858_private *priv = usb_get_serial_port_data(port);
  365. int room = 0;
  366. unsigned long flags;
  367. dbg("%s(port = %d)", __func__, port->number);
  368. spin_lock_irqsave(&priv->lock, flags);
  369. room = oti6858_buf_space_avail(priv->buf);
  370. spin_unlock_irqrestore(&priv->lock, flags);
  371. return room;
  372. }
  373. static int oti6858_chars_in_buffer(struct usb_serial_port *port)
  374. {
  375. struct oti6858_private *priv = usb_get_serial_port_data(port);
  376. int chars = 0;
  377. unsigned long flags;
  378. dbg("%s(port = %d)", __func__, port->number);
  379. spin_lock_irqsave(&priv->lock, flags);
  380. chars = oti6858_buf_data_avail(priv->buf);
  381. spin_unlock_irqrestore(&priv->lock, flags);
  382. return chars;
  383. }
  384. static void oti6858_set_termios(struct usb_serial_port *port,
  385. struct ktermios *old_termios)
  386. {
  387. struct oti6858_private *priv = usb_get_serial_port_data(port);
  388. unsigned long flags;
  389. unsigned int cflag;
  390. u8 frame_fmt, control;
  391. u16 divisor;
  392. int br;
  393. dbg("%s(port = %d)", __func__, port->number);
  394. if (!port->tty || !port->tty->termios) {
  395. dbg("%s(): no tty structures", __func__);
  396. return;
  397. }
  398. spin_lock_irqsave(&priv->lock, flags);
  399. if (!priv->flags.termios_initialized) {
  400. *(port->tty->termios) = tty_std_termios;
  401. port->tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
  402. priv->flags.termios_initialized = 1;
  403. port->tty->termios->c_ispeed = 38400;
  404. port->tty->termios->c_ospeed = 38400;
  405. }
  406. spin_unlock_irqrestore(&priv->lock, flags);
  407. cflag = port->tty->termios->c_cflag;
  408. spin_lock_irqsave(&priv->lock, flags);
  409. divisor = priv->pending_setup.divisor;
  410. frame_fmt = priv->pending_setup.frame_fmt;
  411. control = priv->pending_setup.control;
  412. spin_unlock_irqrestore(&priv->lock, flags);
  413. frame_fmt &= ~FMT_DATA_BITS_MASK;
  414. switch (cflag & CSIZE) {
  415. case CS5:
  416. frame_fmt |= FMT_DATA_BITS_5;
  417. break;
  418. case CS6:
  419. frame_fmt |= FMT_DATA_BITS_6;
  420. break;
  421. case CS7:
  422. frame_fmt |= FMT_DATA_BITS_7;
  423. break;
  424. default:
  425. case CS8:
  426. frame_fmt |= FMT_DATA_BITS_8;
  427. break;
  428. }
  429. /* manufacturer claims that this device can work with baud rates
  430. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  431. * guarantee that any other baud rate will work (especially
  432. * the higher ones)
  433. */
  434. br = tty_get_baud_rate(port->tty);
  435. if (br == 0) {
  436. divisor = 0;
  437. } else {
  438. int real_br;
  439. br = min(br, OTI6858_MAX_BAUD_RATE);
  440. divisor = (96000000 + 8 * br) / (16 * br);
  441. real_br = 96000000 / (16 * divisor);
  442. divisor = cpu_to_le16(divisor);
  443. tty_encode_baud_rate(port->tty, real_br, real_br);
  444. }
  445. frame_fmt &= ~FMT_STOP_BITS_MASK;
  446. if ((cflag & CSTOPB) != 0) {
  447. frame_fmt |= FMT_STOP_BITS_2;
  448. } else {
  449. frame_fmt |= FMT_STOP_BITS_1;
  450. }
  451. frame_fmt &= ~FMT_PARITY_MASK;
  452. if ((cflag & PARENB) != 0) {
  453. if ((cflag & PARODD) != 0) {
  454. frame_fmt |= FMT_PARITY_ODD;
  455. } else {
  456. frame_fmt |= FMT_PARITY_EVEN;
  457. }
  458. } else {
  459. frame_fmt |= FMT_PARITY_NONE;
  460. }
  461. control &= ~CONTROL_MASK;
  462. if ((cflag & CRTSCTS) != 0)
  463. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  464. /* change control lines if we are switching to or from B0 */
  465. /* FIXME:
  466. spin_lock_irqsave(&priv->lock, flags);
  467. control = priv->line_control;
  468. if ((cflag & CBAUD) == B0)
  469. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  470. else
  471. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  472. if (control != priv->line_control) {
  473. control = priv->line_control;
  474. spin_unlock_irqrestore(&priv->lock, flags);
  475. set_control_lines(serial->dev, control);
  476. } else {
  477. spin_unlock_irqrestore(&priv->lock, flags);
  478. }
  479. */
  480. spin_lock_irqsave(&priv->lock, flags);
  481. if (divisor != priv->pending_setup.divisor
  482. || control != priv->pending_setup.control
  483. || frame_fmt != priv->pending_setup.frame_fmt) {
  484. priv->pending_setup.divisor = divisor;
  485. priv->pending_setup.control = control;
  486. priv->pending_setup.frame_fmt = frame_fmt;
  487. }
  488. spin_unlock_irqrestore(&priv->lock, flags);
  489. }
  490. static int oti6858_open(struct usb_serial_port *port, struct file *filp)
  491. {
  492. struct oti6858_private *priv = usb_get_serial_port_data(port);
  493. struct ktermios tmp_termios;
  494. struct usb_serial *serial = port->serial;
  495. struct oti6858_control_pkt *buf;
  496. unsigned long flags;
  497. int result;
  498. dbg("%s(port = %d)", __func__, port->number);
  499. usb_clear_halt(serial->dev, port->write_urb->pipe);
  500. usb_clear_halt(serial->dev, port->read_urb->pipe);
  501. if (port->open_count != 1)
  502. return 0;
  503. if ((buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
  504. dev_err(&port->dev, "%s(): out of memory!\n", __func__);
  505. return -ENOMEM;
  506. }
  507. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  508. OTI6858_REQ_T_GET_STATUS,
  509. OTI6858_REQ_GET_STATUS,
  510. 0, 0,
  511. buf, OTI6858_CTRL_PKT_SIZE,
  512. 100);
  513. if (result != OTI6858_CTRL_PKT_SIZE) {
  514. /* assume default (after power-on reset) values */
  515. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  516. buf->frame_fmt = 0x03; /* 8N1 */
  517. buf->something = 0x43;
  518. buf->control = 0x4c; /* DTR, RTS */
  519. buf->tx_status = 0x00;
  520. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  521. buf->rx_bytes_avail = 0x00;
  522. }
  523. spin_lock_irqsave(&priv->lock, flags);
  524. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  525. priv->pending_setup.divisor = buf->divisor;
  526. priv->pending_setup.frame_fmt = buf->frame_fmt;
  527. priv->pending_setup.control = buf->control;
  528. spin_unlock_irqrestore(&priv->lock, flags);
  529. kfree(buf);
  530. dbg("%s(): submitting interrupt urb", __func__);
  531. port->interrupt_in_urb->dev = serial->dev;
  532. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  533. if (result != 0) {
  534. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  535. " with error %d\n", __func__, result);
  536. oti6858_close(port, NULL);
  537. return -EPROTO;
  538. }
  539. /* setup termios */
  540. if (port->tty)
  541. oti6858_set_termios(port, &tmp_termios);
  542. return 0;
  543. }
  544. static void oti6858_close(struct usb_serial_port *port, struct file *filp)
  545. {
  546. struct oti6858_private *priv = usb_get_serial_port_data(port);
  547. unsigned long flags;
  548. long timeout;
  549. wait_queue_t wait;
  550. dbg("%s(port = %d)", __func__, port->number);
  551. /* wait for data to drain from the buffer */
  552. spin_lock_irqsave(&priv->lock, flags);
  553. timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
  554. init_waitqueue_entry(&wait, current);
  555. add_wait_queue(&port->tty->write_wait, &wait);
  556. dbg("%s(): entering wait loop", __func__);
  557. for (;;) {
  558. set_current_state(TASK_INTERRUPTIBLE);
  559. if (oti6858_buf_data_avail(priv->buf) == 0
  560. || timeout == 0 || signal_pending(current)
  561. || port->serial->disconnected)
  562. break;
  563. spin_unlock_irqrestore(&priv->lock, flags);
  564. timeout = schedule_timeout(timeout);
  565. spin_lock_irqsave(&priv->lock, flags);
  566. }
  567. set_current_state(TASK_RUNNING);
  568. remove_wait_queue(&port->tty->write_wait, &wait);
  569. dbg("%s(): after wait loop", __func__);
  570. /* clear out any remaining data in the buffer */
  571. oti6858_buf_clear(priv->buf);
  572. spin_unlock_irqrestore(&priv->lock, flags);
  573. /* wait for characters to drain from the device */
  574. /* (this is long enough for the entire 256 byte */
  575. /* pl2303 hardware buffer to drain with no flow */
  576. /* control for data rates of 1200 bps or more, */
  577. /* for lower rates we should really know how much */
  578. /* data is in the buffer to compute a delay */
  579. /* that is not unnecessarily long) */
  580. /* FIXME
  581. bps = tty_get_baud_rate(port->tty);
  582. if (bps > 1200)
  583. timeout = max((HZ*2560)/bps,HZ/10);
  584. else
  585. */
  586. timeout = 2*HZ;
  587. schedule_timeout_interruptible(timeout);
  588. dbg("%s(): after schedule_timeout_interruptible()", __func__);
  589. /* cancel scheduled setup */
  590. cancel_delayed_work(&priv->delayed_setup_work);
  591. cancel_delayed_work(&priv->delayed_write_work);
  592. flush_scheduled_work();
  593. /* shutdown our urbs */
  594. dbg("%s(): shutting down urbs", __func__);
  595. usb_kill_urb(port->write_urb);
  596. usb_kill_urb(port->read_urb);
  597. usb_kill_urb(port->interrupt_in_urb);
  598. /*
  599. if (port->tty && (port->tty->termios->c_cflag) & HUPCL) {
  600. // drop DTR and RTS
  601. spin_lock_irqsave(&priv->lock, flags);
  602. priv->pending_setup.control &= ~CONTROL_MASK;
  603. spin_unlock_irqrestore(&priv->lock, flags);
  604. }
  605. */
  606. }
  607. static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
  608. unsigned int set, unsigned int clear)
  609. {
  610. struct oti6858_private *priv = usb_get_serial_port_data(port);
  611. unsigned long flags;
  612. u8 control;
  613. dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
  614. __func__, port->number, set, clear);
  615. if (!usb_get_intfdata(port->serial->interface))
  616. return -ENODEV;
  617. /* FIXME: check if this is correct (active high/low) */
  618. spin_lock_irqsave(&priv->lock, flags);
  619. control = priv->pending_setup.control;
  620. if ((set & TIOCM_RTS) != 0)
  621. control |= CONTROL_RTS_HIGH;
  622. if ((set & TIOCM_DTR) != 0)
  623. control |= CONTROL_DTR_HIGH;
  624. if ((clear & TIOCM_RTS) != 0)
  625. control &= ~CONTROL_RTS_HIGH;
  626. if ((clear & TIOCM_DTR) != 0)
  627. control &= ~CONTROL_DTR_HIGH;
  628. if (control != priv->pending_setup.control) {
  629. priv->pending_setup.control = control;
  630. }
  631. spin_unlock_irqrestore(&priv->lock, flags);
  632. return 0;
  633. }
  634. static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file)
  635. {
  636. struct oti6858_private *priv = usb_get_serial_port_data(port);
  637. unsigned long flags;
  638. unsigned pin_state;
  639. unsigned result = 0;
  640. dbg("%s(port = %d)", __func__, port->number);
  641. if (!usb_get_intfdata(port->serial->interface))
  642. return -ENODEV;
  643. spin_lock_irqsave(&priv->lock, flags);
  644. pin_state = priv->status.pin_state & PIN_MASK;
  645. spin_unlock_irqrestore(&priv->lock, flags);
  646. /* FIXME: check if this is correct (active high/low) */
  647. if ((pin_state & PIN_RTS) != 0)
  648. result |= TIOCM_RTS;
  649. if ((pin_state & PIN_CTS) != 0)
  650. result |= TIOCM_CTS;
  651. if ((pin_state & PIN_DSR) != 0)
  652. result |= TIOCM_DSR;
  653. if ((pin_state & PIN_DTR) != 0)
  654. result |= TIOCM_DTR;
  655. if ((pin_state & PIN_RI) != 0)
  656. result |= TIOCM_RI;
  657. if ((pin_state & PIN_DCD) != 0)
  658. result |= TIOCM_CD;
  659. dbg("%s() = 0x%08x", __func__, result);
  660. return result;
  661. }
  662. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  663. {
  664. struct oti6858_private *priv = usb_get_serial_port_data(port);
  665. unsigned long flags;
  666. unsigned int prev, status;
  667. unsigned int changed;
  668. spin_lock_irqsave(&priv->lock, flags);
  669. prev = priv->status.pin_state;
  670. spin_unlock_irqrestore(&priv->lock, flags);
  671. while (1) {
  672. wait_event_interruptible(priv->intr_wait, priv->status.pin_state != prev);
  673. if (signal_pending(current))
  674. return -ERESTARTSYS;
  675. spin_lock_irqsave(&priv->lock, flags);
  676. status = priv->status.pin_state & PIN_MASK;
  677. spin_unlock_irqrestore(&priv->lock, flags);
  678. changed = prev ^ status;
  679. /* FIXME: check if this is correct (active high/low) */
  680. if ( ((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
  681. ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
  682. ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
  683. ((arg & TIOCM_CTS) && (changed & PIN_CTS))) {
  684. return 0;
  685. }
  686. prev = status;
  687. }
  688. /* NOTREACHED */
  689. return 0;
  690. }
  691. static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
  692. unsigned int cmd, unsigned long arg)
  693. {
  694. void __user *user_arg = (void __user *) arg;
  695. unsigned int x;
  696. dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
  697. __func__, port->number, cmd, arg);
  698. switch (cmd) {
  699. case TIOCMBIS:
  700. if (copy_from_user(&x, user_arg, sizeof(x)))
  701. return -EFAULT;
  702. return oti6858_tiocmset(port, NULL, x, 0);
  703. case TIOCMBIC:
  704. if (copy_from_user(&x, user_arg, sizeof(x)))
  705. return -EFAULT;
  706. return oti6858_tiocmset(port, NULL, 0, x);
  707. case TIOCMIWAIT:
  708. dbg("%s(): TIOCMIWAIT", __func__);
  709. return wait_modem_info(port, arg);
  710. default:
  711. dbg("%s(): 0x%04x not supported", __func__, cmd);
  712. break;
  713. }
  714. return -ENOIOCTLCMD;
  715. }
  716. static void oti6858_break_ctl(struct usb_serial_port *port, int break_state)
  717. {
  718. int state;
  719. dbg("%s(port = %d)", __func__, port->number);
  720. state = (break_state == 0) ? 0 : 1;
  721. dbg("%s(): turning break %s", __func__, state ? "on" : "off");
  722. /* FIXME */
  723. /*
  724. result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
  725. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  726. 0, NULL, 0, 100);
  727. if (result != 0)
  728. dbg("%s(): error sending break", __func__);
  729. */
  730. }
  731. static void oti6858_shutdown(struct usb_serial *serial)
  732. {
  733. struct oti6858_private *priv;
  734. int i;
  735. dbg("%s()", __func__);
  736. for (i = 0; i < serial->num_ports; ++i) {
  737. priv = usb_get_serial_port_data(serial->port[i]);
  738. if (priv) {
  739. oti6858_buf_free(priv->buf);
  740. kfree(priv);
  741. usb_set_serial_port_data(serial->port[i], NULL);
  742. }
  743. }
  744. }
  745. static void oti6858_read_int_callback(struct urb *urb)
  746. {
  747. struct usb_serial_port *port = urb->context;
  748. struct oti6858_private *priv = usb_get_serial_port_data(port);
  749. int transient = 0, can_recv = 0, resubmit = 1;
  750. int status = urb->status;
  751. dbg("%s(port = %d, status = %d)",
  752. __func__, port->number, status);
  753. switch (status) {
  754. case 0:
  755. /* success */
  756. break;
  757. case -ECONNRESET:
  758. case -ENOENT:
  759. case -ESHUTDOWN:
  760. /* this urb is terminated, clean up */
  761. dbg("%s(): urb shutting down with status: %d",
  762. __func__, status);
  763. return;
  764. default:
  765. dbg("%s(): nonzero urb status received: %d",
  766. __func__, status);
  767. break;
  768. }
  769. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  770. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  771. unsigned long flags;
  772. spin_lock_irqsave(&priv->lock, flags);
  773. if (!priv->transient) {
  774. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  775. if (xs->rx_bytes_avail == 0) {
  776. priv->transient = 4;
  777. priv->setup_done = 0;
  778. resubmit = 0;
  779. dbg("%s(): scheduling setup_line()",
  780. __func__);
  781. schedule_delayed_work(&priv->delayed_setup_work, 0);
  782. }
  783. }
  784. } else {
  785. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  786. priv->transient = 0;
  787. } else if (!priv->setup_done) {
  788. resubmit = 0;
  789. } else if (--priv->transient == 0) {
  790. if (xs->rx_bytes_avail == 0) {
  791. priv->transient = 4;
  792. priv->setup_done = 0;
  793. resubmit = 0;
  794. dbg("%s(): scheduling setup_line()",
  795. __func__);
  796. schedule_delayed_work(&priv->delayed_setup_work, 0);
  797. }
  798. }
  799. }
  800. if (!priv->transient) {
  801. if (xs->pin_state != priv->status.pin_state)
  802. wake_up_interruptible(&priv->intr_wait);
  803. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  804. }
  805. if (!priv->transient && xs->rx_bytes_avail != 0) {
  806. can_recv = xs->rx_bytes_avail;
  807. priv->flags.read_urb_in_use = 1;
  808. }
  809. transient = priv->transient;
  810. spin_unlock_irqrestore(&priv->lock, flags);
  811. }
  812. if (can_recv) {
  813. int result;
  814. port->read_urb->dev = port->serial->dev;
  815. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  816. if (result != 0) {
  817. priv->flags.read_urb_in_use = 0;
  818. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  819. " error %d\n", __func__, result);
  820. } else {
  821. resubmit = 0;
  822. }
  823. } else if (!transient) {
  824. unsigned long flags;
  825. spin_lock_irqsave(&priv->lock, flags);
  826. if (priv->flags.write_urb_in_use == 0
  827. && oti6858_buf_data_avail(priv->buf) != 0) {
  828. schedule_delayed_work(&priv->delayed_write_work,0);
  829. resubmit = 0;
  830. }
  831. spin_unlock_irqrestore(&priv->lock, flags);
  832. }
  833. if (resubmit) {
  834. int result;
  835. // dbg("%s(): submitting interrupt urb", __func__);
  836. urb->dev = port->serial->dev;
  837. result = usb_submit_urb(urb, GFP_ATOMIC);
  838. if (result != 0) {
  839. dev_err(&urb->dev->dev,
  840. "%s(): usb_submit_urb() failed with"
  841. " error %d\n", __func__, result);
  842. }
  843. }
  844. }
  845. static void oti6858_read_bulk_callback(struct urb *urb)
  846. {
  847. struct usb_serial_port *port = urb->context;
  848. struct oti6858_private *priv = usb_get_serial_port_data(port);
  849. struct tty_struct *tty;
  850. unsigned char *data = urb->transfer_buffer;
  851. unsigned long flags;
  852. int status = urb->status;
  853. int result;
  854. dbg("%s(port = %d, status = %d)",
  855. __func__, port->number, status);
  856. spin_lock_irqsave(&priv->lock, flags);
  857. priv->flags.read_urb_in_use = 0;
  858. spin_unlock_irqrestore(&priv->lock, flags);
  859. if (status != 0) {
  860. if (!port->open_count) {
  861. dbg("%s(): port is closed, exiting", __func__);
  862. return;
  863. }
  864. /*
  865. if (status == -EPROTO) {
  866. // PL2303 mysteriously fails with -EPROTO reschedule the read
  867. dbg("%s - caught -EPROTO, resubmitting the urb", __func__);
  868. result = usb_submit_urb(urb, GFP_ATOMIC);
  869. if (result)
  870. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
  871. return;
  872. }
  873. */
  874. dbg("%s(): unable to handle the error, exiting", __func__);
  875. return;
  876. }
  877. tty = port->tty;
  878. if (tty != NULL && urb->actual_length > 0) {
  879. tty_insert_flip_string(tty, data, urb->actual_length);
  880. tty_flip_buffer_push(tty);
  881. }
  882. // schedule the interrupt urb if we are still open */
  883. if (port->open_count != 0) {
  884. port->interrupt_in_urb->dev = port->serial->dev;
  885. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  886. if (result != 0) {
  887. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  888. " error %d\n", __func__, result);
  889. }
  890. }
  891. }
  892. static void oti6858_write_bulk_callback(struct urb *urb)
  893. {
  894. struct usb_serial_port *port = urb->context;
  895. struct oti6858_private *priv = usb_get_serial_port_data(port);
  896. int status = urb->status;
  897. int result;
  898. dbg("%s(port = %d, status = %d)",
  899. __func__, port->number, status);
  900. switch (status) {
  901. case 0:
  902. /* success */
  903. break;
  904. case -ECONNRESET:
  905. case -ENOENT:
  906. case -ESHUTDOWN:
  907. /* this urb is terminated, clean up */
  908. dbg("%s(): urb shutting down with status: %d",
  909. __func__, status);
  910. priv->flags.write_urb_in_use = 0;
  911. return;
  912. default:
  913. /* error in the urb, so we have to resubmit it */
  914. dbg("%s(): nonzero write bulk status received: %d",
  915. __func__, status);
  916. dbg("%s(): overflow in write", __func__);
  917. port->write_urb->transfer_buffer_length = 1;
  918. port->write_urb->dev = port->serial->dev;
  919. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  920. if (result) {
  921. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  922. " error %d\n", __func__, result);
  923. } else {
  924. return;
  925. }
  926. }
  927. priv->flags.write_urb_in_use = 0;
  928. // schedule the interrupt urb if we are still open */
  929. port->interrupt_in_urb->dev = port->serial->dev;
  930. dbg("%s(): submitting interrupt urb", __func__);
  931. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  932. if (result != 0) {
  933. dev_err(&port->dev, "%s(): failed submitting int urb,"
  934. " error %d\n", __func__, result);
  935. }
  936. }
  937. /*
  938. * oti6858_buf_alloc
  939. *
  940. * Allocate a circular buffer and all associated memory.
  941. */
  942. static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
  943. {
  944. struct oti6858_buf *pb;
  945. if (size == 0)
  946. return NULL;
  947. pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
  948. if (pb == NULL)
  949. return NULL;
  950. pb->buf_buf = kmalloc(size, GFP_KERNEL);
  951. if (pb->buf_buf == NULL) {
  952. kfree(pb);
  953. return NULL;
  954. }
  955. pb->buf_size = size;
  956. pb->buf_get = pb->buf_put = pb->buf_buf;
  957. return pb;
  958. }
  959. /*
  960. * oti6858_buf_free
  961. *
  962. * Free the buffer and all associated memory.
  963. */
  964. static void oti6858_buf_free(struct oti6858_buf *pb)
  965. {
  966. if (pb) {
  967. kfree(pb->buf_buf);
  968. kfree(pb);
  969. }
  970. }
  971. /*
  972. * oti6858_buf_clear
  973. *
  974. * Clear out all data in the circular buffer.
  975. */
  976. static void oti6858_buf_clear(struct oti6858_buf *pb)
  977. {
  978. if (pb != NULL) {
  979. /* equivalent to a get of all data available */
  980. pb->buf_get = pb->buf_put;
  981. }
  982. }
  983. /*
  984. * oti6858_buf_data_avail
  985. *
  986. * Return the number of bytes of data available in the circular
  987. * buffer.
  988. */
  989. static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
  990. {
  991. if (pb == NULL)
  992. return 0;
  993. return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
  994. }
  995. /*
  996. * oti6858_buf_space_avail
  997. *
  998. * Return the number of bytes of space available in the circular
  999. * buffer.
  1000. */
  1001. static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
  1002. {
  1003. if (pb == NULL)
  1004. return 0;
  1005. return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
  1006. }
  1007. /*
  1008. * oti6858_buf_put
  1009. *
  1010. * Copy data data from a user buffer and put it into the circular buffer.
  1011. * Restrict to the amount of space available.
  1012. *
  1013. * Return the number of bytes copied.
  1014. */
  1015. static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
  1016. unsigned int count)
  1017. {
  1018. unsigned int len;
  1019. if (pb == NULL)
  1020. return 0;
  1021. len = oti6858_buf_space_avail(pb);
  1022. if (count > len)
  1023. count = len;
  1024. if (count == 0)
  1025. return 0;
  1026. len = pb->buf_buf + pb->buf_size - pb->buf_put;
  1027. if (count > len) {
  1028. memcpy(pb->buf_put, buf, len);
  1029. memcpy(pb->buf_buf, buf+len, count - len);
  1030. pb->buf_put = pb->buf_buf + count - len;
  1031. } else {
  1032. memcpy(pb->buf_put, buf, count);
  1033. if (count < len)
  1034. pb->buf_put += count;
  1035. else /* count == len */
  1036. pb->buf_put = pb->buf_buf;
  1037. }
  1038. return count;
  1039. }
  1040. /*
  1041. * oti6858_buf_get
  1042. *
  1043. * Get data from the circular buffer and copy to the given buffer.
  1044. * Restrict to the amount of data available.
  1045. *
  1046. * Return the number of bytes copied.
  1047. */
  1048. static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
  1049. unsigned int count)
  1050. {
  1051. unsigned int len;
  1052. if (pb == NULL)
  1053. return 0;
  1054. len = oti6858_buf_data_avail(pb);
  1055. if (count > len)
  1056. count = len;
  1057. if (count == 0)
  1058. return 0;
  1059. len = pb->buf_buf + pb->buf_size - pb->buf_get;
  1060. if (count > len) {
  1061. memcpy(buf, pb->buf_get, len);
  1062. memcpy(buf+len, pb->buf_buf, count - len);
  1063. pb->buf_get = pb->buf_buf + count - len;
  1064. } else {
  1065. memcpy(buf, pb->buf_get, count);
  1066. if (count < len)
  1067. pb->buf_get += count;
  1068. else /* count == len */
  1069. pb->buf_get = pb->buf_buf;
  1070. }
  1071. return count;
  1072. }
  1073. /* module description and (de)initialization */
  1074. static int __init oti6858_init(void)
  1075. {
  1076. int retval;
  1077. if ((retval = usb_serial_register(&oti6858_device)) == 0) {
  1078. if ((retval = usb_register(&oti6858_driver)) != 0)
  1079. usb_serial_deregister(&oti6858_device);
  1080. else
  1081. return 0;
  1082. }
  1083. return retval;
  1084. }
  1085. static void __exit oti6858_exit(void)
  1086. {
  1087. usb_deregister(&oti6858_driver);
  1088. usb_serial_deregister(&oti6858_device);
  1089. }
  1090. module_init(oti6858_init);
  1091. module_exit(oti6858_exit);
  1092. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  1093. MODULE_AUTHOR(OTI6858_AUTHOR);
  1094. MODULE_VERSION(OTI6858_VERSION);
  1095. MODULE_LICENSE("GPL");
  1096. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1097. MODULE_PARM_DESC(debug, "enable debug output");