oti6858.c 33 KB

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