oti6858.c 35 KB

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