ti_usb_3410_5052.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  1. /* vi: ts=8 sw=8
  2. *
  3. * TI 3410/5052 USB Serial Driver
  4. *
  5. * Copyright (C) 2004 Texas Instruments
  6. *
  7. * This driver is based on the Linux io_ti driver, which is
  8. * Copyright (C) 2000-2002 Inside Out Networks
  9. * Copyright (C) 2001-2002 Greg Kroah-Hartman
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * For questions or problems with this driver, contact Texas Instruments
  17. * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
  18. * Peter Berger <pberger@brimson.com>.
  19. *
  20. * This driver needs this hotplug script in /etc/hotplug/usb/ti_usb_3410_5052
  21. * or in /etc/hotplug.d/usb/ti_usb_3410_5052.hotplug to set the device
  22. * configuration.
  23. *
  24. * #!/bin/bash
  25. *
  26. * BOOT_CONFIG=1
  27. * ACTIVE_CONFIG=2
  28. *
  29. * if [[ "$ACTION" != "add" ]]
  30. * then
  31. * exit
  32. * fi
  33. *
  34. * CONFIG_PATH=/sys${DEVPATH%/?*}/bConfigurationValue
  35. *
  36. * if [[ 0`cat $CONFIG_PATH` -ne $BOOT_CONFIG ]]
  37. * then
  38. * exit
  39. * fi
  40. *
  41. * PRODUCT=${PRODUCT%/?*} # delete version
  42. * VENDOR_ID=`printf "%d" 0x${PRODUCT%/?*}`
  43. * PRODUCT_ID=`printf "%d" 0x${PRODUCT#*?/}`
  44. *
  45. * PARAM_PATH=/sys/module/ti_usb_3410_5052/parameters
  46. *
  47. * function scan() {
  48. * s=$1
  49. * shift
  50. * for i
  51. * do
  52. * if [[ $s -eq $i ]]
  53. * then
  54. * return 0
  55. * fi
  56. * done
  57. * return 1
  58. * }
  59. *
  60. * IFS=$IFS,
  61. *
  62. * if (scan $VENDOR_ID 1105 `cat $PARAM_PATH/vendor_3410` &&
  63. * scan $PRODUCT_ID 13328 `cat $PARAM_PATH/product_3410`) ||
  64. * (scan $VENDOR_ID 1105 `cat $PARAM_PATH/vendor_5052` &&
  65. * scan $PRODUCT_ID 20562 20818 20570 20575 `cat $PARAM_PATH/product_5052`)
  66. * then
  67. * echo $ACTIVE_CONFIG > $CONFIG_PATH
  68. * fi
  69. */
  70. #include <linux/config.h>
  71. #include <linux/kernel.h>
  72. #include <linux/errno.h>
  73. #include <linux/init.h>
  74. #include <linux/slab.h>
  75. #include <linux/tty.h>
  76. #include <linux/tty_driver.h>
  77. #include <linux/tty_flip.h>
  78. #include <linux/module.h>
  79. #include <linux/spinlock.h>
  80. #include <linux/ioctl.h>
  81. #include <linux/serial.h>
  82. #include <linux/circ_buf.h>
  83. #include <asm/uaccess.h>
  84. #include <asm/semaphore.h>
  85. #include <linux/usb.h>
  86. #include "usb-serial.h"
  87. #include "ti_usb_3410_5052.h"
  88. #include "ti_fw_3410.h" /* firmware image for 3410 */
  89. #include "ti_fw_5052.h" /* firmware image for 5052 */
  90. /* Defines */
  91. #define TI_DRIVER_VERSION "v0.9"
  92. #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
  93. #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
  94. #define TI_FIRMWARE_BUF_SIZE 16284
  95. #define TI_WRITE_BUF_SIZE 1024
  96. #define TI_TRANSFER_TIMEOUT 2
  97. #define TI_DEFAULT_LOW_LATENCY 0
  98. #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
  99. /* supported setserial flags */
  100. #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
  101. /* read urb states */
  102. #define TI_READ_URB_RUNNING 0
  103. #define TI_READ_URB_STOPPING 1
  104. #define TI_READ_URB_STOPPED 2
  105. #define TI_EXTRA_VID_PID_COUNT 5
  106. /* Structures */
  107. struct ti_port {
  108. int tp_is_open;
  109. __u8 tp_msr;
  110. __u8 tp_lsr;
  111. __u8 tp_shadow_mcr;
  112. __u8 tp_uart_mode; /* 232 or 485 modes */
  113. unsigned int tp_uart_base_addr;
  114. int tp_flags;
  115. int tp_closing_wait;/* in .01 secs */
  116. struct async_icount tp_icount;
  117. wait_queue_head_t tp_msr_wait; /* wait for msr change */
  118. wait_queue_head_t tp_write_wait;
  119. struct ti_device *tp_tdev;
  120. struct usb_serial_port *tp_port;
  121. spinlock_t tp_lock;
  122. int tp_read_urb_state;
  123. int tp_write_urb_in_use;
  124. struct circ_buf *tp_write_buf;
  125. };
  126. struct ti_device {
  127. struct semaphore td_open_close_sem;
  128. int td_open_port_count;
  129. struct usb_serial *td_serial;
  130. int td_is_3410;
  131. int td_urb_error;
  132. };
  133. /* Function Declarations */
  134. static int ti_startup(struct usb_serial *serial);
  135. static void ti_shutdown(struct usb_serial *serial);
  136. static int ti_open(struct usb_serial_port *port, struct file *file);
  137. static void ti_close(struct usb_serial_port *port, struct file *file);
  138. static int ti_write(struct usb_serial_port *port, const unsigned char *data,
  139. int count);
  140. static int ti_write_room(struct usb_serial_port *port);
  141. static int ti_chars_in_buffer(struct usb_serial_port *port);
  142. static void ti_throttle(struct usb_serial_port *port);
  143. static void ti_unthrottle(struct usb_serial_port *port);
  144. static int ti_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
  145. static void ti_set_termios(struct usb_serial_port *port,
  146. struct termios *old_termios);
  147. static int ti_tiocmget(struct usb_serial_port *port, struct file *file);
  148. static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
  149. unsigned int set, unsigned int clear);
  150. static void ti_break(struct usb_serial_port *port, int break_state);
  151. static void ti_interrupt_callback(struct urb *urb, struct pt_regs *regs);
  152. static void ti_bulk_in_callback(struct urb *urb, struct pt_regs *regs);
  153. static void ti_bulk_out_callback(struct urb *urb, struct pt_regs *regs);
  154. static void ti_recv(struct device *dev, struct tty_struct *tty,
  155. unsigned char *data, int length);
  156. static void ti_send(struct ti_port *tport);
  157. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
  158. static int ti_get_lsr(struct ti_port *tport);
  159. static int ti_get_serial_info(struct ti_port *tport,
  160. struct serial_struct __user *ret_arg);
  161. static int ti_set_serial_info(struct ti_port *tport,
  162. struct serial_struct __user *new_arg);
  163. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
  164. static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
  165. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
  166. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
  167. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  168. __u16 moduleid, __u16 value, __u8 *data, int size);
  169. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  170. __u16 moduleid, __u16 value, __u8 *data, int size);
  171. static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
  172. __u8 mask, __u8 byte);
  173. static int ti_download_firmware(struct ti_device *tdev,
  174. unsigned char *firmware, unsigned int firmware_size);
  175. /* circular buffer */
  176. static struct circ_buf *ti_buf_alloc(void);
  177. static void ti_buf_free(struct circ_buf *cb);
  178. static void ti_buf_clear(struct circ_buf *cb);
  179. static int ti_buf_data_avail(struct circ_buf *cb);
  180. static int ti_buf_space_avail(struct circ_buf *cb);
  181. static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
  182. static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
  183. /* Data */
  184. /* module parameters */
  185. static int debug;
  186. static int low_latency = TI_DEFAULT_LOW_LATENCY;
  187. static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
  188. static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
  189. static int vendor_3410_count;
  190. static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
  191. static int product_3410_count;
  192. static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
  193. static int vendor_5052_count;
  194. static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
  195. static int product_5052_count;
  196. /* supported devices */
  197. /* the array dimension is the number of default entries plus */
  198. /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
  199. /* null entry */
  200. static struct usb_device_id ti_id_table_3410[1+TI_EXTRA_VID_PID_COUNT+1] = {
  201. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  202. };
  203. static struct usb_device_id ti_id_table_5052[4+TI_EXTRA_VID_PID_COUNT+1] = {
  204. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  205. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  206. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  207. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  208. };
  209. static struct usb_device_id ti_id_table_combined[] = {
  210. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  211. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  212. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  213. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  214. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  215. { }
  216. };
  217. static struct usb_driver ti_usb_driver = {
  218. .owner = THIS_MODULE,
  219. .name = "ti_usb_3410_5052",
  220. .probe = usb_serial_probe,
  221. .disconnect = usb_serial_disconnect,
  222. .id_table = ti_id_table_combined,
  223. .no_dynamic_id = 1,
  224. };
  225. static struct usb_serial_driver ti_1port_device = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "ti_usb_3410_5052_1",
  229. },
  230. .description = "TI USB 3410 1 port adapter",
  231. .id_table = ti_id_table_3410,
  232. .num_interrupt_in = 1,
  233. .num_bulk_in = 1,
  234. .num_bulk_out = 1,
  235. .num_ports = 1,
  236. .attach = ti_startup,
  237. .shutdown = ti_shutdown,
  238. .open = ti_open,
  239. .close = ti_close,
  240. .write = ti_write,
  241. .write_room = ti_write_room,
  242. .chars_in_buffer = ti_chars_in_buffer,
  243. .throttle = ti_throttle,
  244. .unthrottle = ti_unthrottle,
  245. .ioctl = ti_ioctl,
  246. .set_termios = ti_set_termios,
  247. .tiocmget = ti_tiocmget,
  248. .tiocmset = ti_tiocmset,
  249. .break_ctl = ti_break,
  250. .read_int_callback = ti_interrupt_callback,
  251. .read_bulk_callback = ti_bulk_in_callback,
  252. .write_bulk_callback = ti_bulk_out_callback,
  253. };
  254. static struct usb_serial_driver ti_2port_device = {
  255. .driver = {
  256. .owner = THIS_MODULE,
  257. .name = "ti_usb_3410_5052_2",
  258. },
  259. .description = "TI USB 5052 2 port adapter",
  260. .id_table = ti_id_table_5052,
  261. .num_interrupt_in = 1,
  262. .num_bulk_in = 2,
  263. .num_bulk_out = 2,
  264. .num_ports = 2,
  265. .attach = ti_startup,
  266. .shutdown = ti_shutdown,
  267. .open = ti_open,
  268. .close = ti_close,
  269. .write = ti_write,
  270. .write_room = ti_write_room,
  271. .chars_in_buffer = ti_chars_in_buffer,
  272. .throttle = ti_throttle,
  273. .unthrottle = ti_unthrottle,
  274. .ioctl = ti_ioctl,
  275. .set_termios = ti_set_termios,
  276. .tiocmget = ti_tiocmget,
  277. .tiocmset = ti_tiocmset,
  278. .break_ctl = ti_break,
  279. .read_int_callback = ti_interrupt_callback,
  280. .read_bulk_callback = ti_bulk_in_callback,
  281. .write_bulk_callback = ti_bulk_out_callback,
  282. };
  283. /* Module */
  284. MODULE_AUTHOR(TI_DRIVER_AUTHOR);
  285. MODULE_DESCRIPTION(TI_DRIVER_DESC);
  286. MODULE_VERSION(TI_DRIVER_VERSION);
  287. MODULE_LICENSE("GPL");
  288. module_param(debug, bool, S_IRUGO | S_IWUSR);
  289. MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
  290. module_param(low_latency, bool, S_IRUGO | S_IWUSR);
  291. MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");
  292. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  293. MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");
  294. module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
  295. MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
  296. module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
  297. MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
  298. module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
  299. MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
  300. module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
  301. MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");
  302. MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
  303. /* Functions */
  304. static int __init ti_init(void)
  305. {
  306. int i,j;
  307. int ret;
  308. /* insert extra vendor and product ids */
  309. j = sizeof(ti_id_table_3410)/sizeof(struct usb_device_id)
  310. - TI_EXTRA_VID_PID_COUNT - 1;
  311. for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
  312. ti_id_table_3410[j].idVendor = vendor_3410[i];
  313. ti_id_table_3410[j].idProduct = product_3410[i];
  314. ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  315. }
  316. j = sizeof(ti_id_table_5052)/sizeof(struct usb_device_id)
  317. - TI_EXTRA_VID_PID_COUNT - 1;
  318. for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
  319. ti_id_table_5052[j].idVendor = vendor_5052[i];
  320. ti_id_table_5052[j].idProduct = product_5052[i];
  321. ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  322. }
  323. ret = usb_serial_register(&ti_1port_device);
  324. if (ret)
  325. goto failed_1port;
  326. ret = usb_serial_register(&ti_2port_device);
  327. if (ret)
  328. goto failed_2port;
  329. ret = usb_register(&ti_usb_driver);
  330. if (ret)
  331. goto failed_usb;
  332. info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
  333. return 0;
  334. failed_usb:
  335. usb_serial_deregister(&ti_2port_device);
  336. failed_2port:
  337. usb_serial_deregister(&ti_1port_device);
  338. failed_1port:
  339. return ret;
  340. }
  341. static void __exit ti_exit(void)
  342. {
  343. usb_serial_deregister(&ti_1port_device);
  344. usb_serial_deregister(&ti_2port_device);
  345. usb_deregister(&ti_usb_driver);
  346. }
  347. module_init(ti_init);
  348. module_exit(ti_exit);
  349. static int ti_startup(struct usb_serial *serial)
  350. {
  351. struct ti_device *tdev;
  352. struct ti_port *tport;
  353. struct usb_device *dev = serial->dev;
  354. int status;
  355. int i;
  356. dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
  357. __FUNCTION__, le16_to_cpu(dev->descriptor.idProduct),
  358. dev->descriptor.bNumConfigurations,
  359. dev->actconfig->desc.bConfigurationValue);
  360. /* create device structure */
  361. tdev = kmalloc(sizeof(struct ti_device), GFP_KERNEL);
  362. if (tdev == NULL) {
  363. dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
  364. return -ENOMEM;
  365. }
  366. memset(tdev, 0, sizeof(struct ti_device));
  367. sema_init(&tdev->td_open_close_sem, 1);
  368. tdev->td_serial = serial;
  369. usb_set_serial_data(serial, tdev);
  370. /* determine device type */
  371. if (usb_match_id(serial->interface, ti_id_table_3410))
  372. tdev->td_is_3410 = 1;
  373. dbg("%s - device type is %s", __FUNCTION__, tdev->td_is_3410 ? "3410" : "5052");
  374. /* if we have only 1 configuration, download firmware */
  375. if (dev->descriptor.bNumConfigurations == 1) {
  376. if (tdev->td_is_3410)
  377. status = ti_download_firmware(tdev, ti_fw_3410,
  378. sizeof(ti_fw_3410));
  379. else
  380. status = ti_download_firmware(tdev, ti_fw_5052,
  381. sizeof(ti_fw_5052));
  382. if (status)
  383. goto free_tdev;
  384. /* 3410 must be reset, 5052 resets itself */
  385. if (tdev->td_is_3410) {
  386. msleep_interruptible(100);
  387. usb_reset_device(dev);
  388. }
  389. status = -ENODEV;
  390. goto free_tdev;
  391. }
  392. /* the second configuration must be set (in sysfs by hotplug script) */
  393. if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
  394. status = -ENODEV;
  395. goto free_tdev;
  396. }
  397. /* set up port structures */
  398. for (i = 0; i < serial->num_ports; ++i) {
  399. tport = kmalloc(sizeof(struct ti_port), GFP_KERNEL);
  400. if (tport == NULL) {
  401. dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
  402. status = -ENOMEM;
  403. goto free_tports;
  404. }
  405. memset(tport, 0, sizeof(struct ti_port));
  406. spin_lock_init(&tport->tp_lock);
  407. tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
  408. tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
  409. tport->tp_closing_wait = closing_wait;
  410. init_waitqueue_head(&tport->tp_msr_wait);
  411. init_waitqueue_head(&tport->tp_write_wait);
  412. tport->tp_write_buf = ti_buf_alloc();
  413. if (tport->tp_write_buf == NULL) {
  414. dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
  415. kfree(tport);
  416. status = -ENOMEM;
  417. goto free_tports;
  418. }
  419. tport->tp_port = serial->port[i];
  420. tport->tp_tdev = tdev;
  421. usb_set_serial_port_data(serial->port[i], tport);
  422. tport->tp_uart_mode = 0; /* default is RS232 */
  423. }
  424. return 0;
  425. free_tports:
  426. for (--i; i>=0; --i) {
  427. tport = usb_get_serial_port_data(serial->port[i]);
  428. ti_buf_free(tport->tp_write_buf);
  429. kfree(tport);
  430. usb_set_serial_port_data(serial->port[i], NULL);
  431. }
  432. free_tdev:
  433. kfree(tdev);
  434. usb_set_serial_data(serial, NULL);
  435. return status;
  436. }
  437. static void ti_shutdown(struct usb_serial *serial)
  438. {
  439. int i;
  440. struct ti_device *tdev = usb_get_serial_data(serial);
  441. struct ti_port *tport;
  442. dbg("%s", __FUNCTION__);
  443. for (i=0; i < serial->num_ports; ++i) {
  444. tport = usb_get_serial_port_data(serial->port[i]);
  445. if (tport) {
  446. ti_buf_free(tport->tp_write_buf);
  447. kfree(tport);
  448. usb_set_serial_port_data(serial->port[i], NULL);
  449. }
  450. }
  451. kfree(tdev);
  452. usb_set_serial_data(serial, NULL);
  453. }
  454. static int ti_open(struct usb_serial_port *port, struct file *file)
  455. {
  456. struct ti_port *tport = usb_get_serial_port_data(port);
  457. struct ti_device *tdev;
  458. struct usb_device *dev;
  459. struct urb *urb;
  460. int port_number;
  461. int status;
  462. __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
  463. TI_PIPE_TIMEOUT_ENABLE |
  464. (TI_TRANSFER_TIMEOUT << 2));
  465. dbg("%s - port %d", __FUNCTION__, port->number);
  466. if (tport == NULL)
  467. return -ENODEV;
  468. dev = port->serial->dev;
  469. tdev = tport->tp_tdev;
  470. /* only one open on any port on a device at a time */
  471. if (down_interruptible(&tdev->td_open_close_sem))
  472. return -ERESTARTSYS;
  473. if (port->tty)
  474. port->tty->low_latency =
  475. (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  476. port_number = port->number - port->serial->minor;
  477. memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
  478. tport->tp_msr = 0;
  479. tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
  480. /* start interrupt urb the first time a port is opened on this device */
  481. if (tdev->td_open_port_count == 0) {
  482. dbg("%s - start interrupt in urb", __FUNCTION__);
  483. urb = tdev->td_serial->port[0]->interrupt_in_urb;
  484. if (!urb) {
  485. dev_err(&port->dev, "%s - no interrupt urb\n", __FUNCTION__);
  486. status = -EINVAL;
  487. goto up_sem;
  488. }
  489. urb->complete = ti_interrupt_callback;
  490. urb->context = tdev;
  491. urb->dev = dev;
  492. status = usb_submit_urb(urb, GFP_KERNEL);
  493. if (status) {
  494. dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __FUNCTION__, status);
  495. goto up_sem;
  496. }
  497. }
  498. ti_set_termios(port, NULL);
  499. dbg("%s - sending TI_OPEN_PORT", __FUNCTION__);
  500. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  501. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  502. if (status) {
  503. dev_err(&port->dev, "%s - cannot send open command, %d\n", __FUNCTION__, status);
  504. goto unlink_int_urb;
  505. }
  506. dbg("%s - sending TI_START_PORT", __FUNCTION__);
  507. status = ti_command_out_sync(tdev, TI_START_PORT,
  508. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  509. if (status) {
  510. dev_err(&port->dev, "%s - cannot send start command, %d\n", __FUNCTION__, status);
  511. goto unlink_int_urb;
  512. }
  513. dbg("%s - sending TI_PURGE_PORT", __FUNCTION__);
  514. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  515. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
  516. if (status) {
  517. dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __FUNCTION__, status);
  518. goto unlink_int_urb;
  519. }
  520. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  521. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
  522. if (status) {
  523. dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __FUNCTION__, status);
  524. goto unlink_int_urb;
  525. }
  526. /* reset the data toggle on the bulk endpoints to work around bug in
  527. * host controllers where things get out of sync some times */
  528. usb_clear_halt(dev, port->write_urb->pipe);
  529. usb_clear_halt(dev, port->read_urb->pipe);
  530. ti_set_termios(port, NULL);
  531. dbg("%s - sending TI_OPEN_PORT (2)", __FUNCTION__);
  532. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  533. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  534. if (status) {
  535. dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __FUNCTION__, status);
  536. goto unlink_int_urb;
  537. }
  538. dbg("%s - sending TI_START_PORT (2)", __FUNCTION__);
  539. status = ti_command_out_sync(tdev, TI_START_PORT,
  540. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  541. if (status) {
  542. dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __FUNCTION__, status);
  543. goto unlink_int_urb;
  544. }
  545. /* start read urb */
  546. dbg("%s - start read urb", __FUNCTION__);
  547. urb = port->read_urb;
  548. if (!urb) {
  549. dev_err(&port->dev, "%s - no read urb\n", __FUNCTION__);
  550. status = -EINVAL;
  551. goto unlink_int_urb;
  552. }
  553. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  554. urb->complete = ti_bulk_in_callback;
  555. urb->context = tport;
  556. urb->dev = dev;
  557. status = usb_submit_urb(urb, GFP_KERNEL);
  558. if (status) {
  559. dev_err(&port->dev, "%s - submit read urb failed, %d\n", __FUNCTION__, status);
  560. goto unlink_int_urb;
  561. }
  562. tport->tp_is_open = 1;
  563. ++tdev->td_open_port_count;
  564. goto up_sem;
  565. unlink_int_urb:
  566. if (tdev->td_open_port_count == 0)
  567. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  568. up_sem:
  569. up(&tdev->td_open_close_sem);
  570. dbg("%s - exit %d", __FUNCTION__, status);
  571. return status;
  572. }
  573. static void ti_close(struct usb_serial_port *port, struct file *file)
  574. {
  575. struct ti_device *tdev;
  576. struct ti_port *tport;
  577. int port_number;
  578. int status;
  579. int do_up;
  580. dbg("%s - port %d", __FUNCTION__, port->number);
  581. tdev = usb_get_serial_data(port->serial);
  582. tport = usb_get_serial_port_data(port);
  583. if (tdev == NULL || tport == NULL)
  584. return;
  585. tport->tp_is_open = 0;
  586. ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
  587. usb_kill_urb(port->read_urb);
  588. usb_kill_urb(port->write_urb);
  589. tport->tp_write_urb_in_use = 0;
  590. port_number = port->number - port->serial->minor;
  591. dbg("%s - sending TI_CLOSE_PORT", __FUNCTION__);
  592. status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
  593. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  594. if (status)
  595. dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __FUNCTION__, status);
  596. /* if down is interrupted, continue anyway */
  597. do_up = !down_interruptible(&tdev->td_open_close_sem);
  598. --tport->tp_tdev->td_open_port_count;
  599. if (tport->tp_tdev->td_open_port_count <= 0) {
  600. /* last port is closed, shut down interrupt urb */
  601. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  602. tport->tp_tdev->td_open_port_count = 0;
  603. }
  604. if (do_up)
  605. up(&tdev->td_open_close_sem);
  606. dbg("%s - exit", __FUNCTION__);
  607. }
  608. static int ti_write(struct usb_serial_port *port, const unsigned char *data,
  609. int count)
  610. {
  611. struct ti_port *tport = usb_get_serial_port_data(port);
  612. unsigned long flags;
  613. dbg("%s - port %d", __FUNCTION__, port->number);
  614. if (count == 0) {
  615. dbg("%s - write request of 0 bytes", __FUNCTION__);
  616. return 0;
  617. }
  618. if (tport == NULL || !tport->tp_is_open)
  619. return -ENODEV;
  620. spin_lock_irqsave(&tport->tp_lock, flags);
  621. count = ti_buf_put(tport->tp_write_buf, data, count);
  622. spin_unlock_irqrestore(&tport->tp_lock, flags);
  623. ti_send(tport);
  624. return count;
  625. }
  626. static int ti_write_room(struct usb_serial_port *port)
  627. {
  628. struct ti_port *tport = usb_get_serial_port_data(port);
  629. int room = 0;
  630. unsigned long flags;
  631. dbg("%s - port %d", __FUNCTION__, port->number);
  632. if (tport == NULL)
  633. return -ENODEV;
  634. spin_lock_irqsave(&tport->tp_lock, flags);
  635. room = ti_buf_space_avail(tport->tp_write_buf);
  636. spin_unlock_irqrestore(&tport->tp_lock, flags);
  637. dbg("%s - returns %d", __FUNCTION__, room);
  638. return room;
  639. }
  640. static int ti_chars_in_buffer(struct usb_serial_port *port)
  641. {
  642. struct ti_port *tport = usb_get_serial_port_data(port);
  643. int chars = 0;
  644. unsigned long flags;
  645. dbg("%s - port %d", __FUNCTION__, port->number);
  646. if (tport == NULL)
  647. return -ENODEV;
  648. spin_lock_irqsave(&tport->tp_lock, flags);
  649. chars = ti_buf_data_avail(tport->tp_write_buf);
  650. spin_unlock_irqrestore(&tport->tp_lock, flags);
  651. dbg("%s - returns %d", __FUNCTION__, chars);
  652. return chars;
  653. }
  654. static void ti_throttle(struct usb_serial_port *port)
  655. {
  656. struct ti_port *tport = usb_get_serial_port_data(port);
  657. struct tty_struct *tty;
  658. dbg("%s - port %d", __FUNCTION__, port->number);
  659. if (tport == NULL)
  660. return;
  661. tty = port->tty;
  662. if (!tty) {
  663. dbg("%s - no tty", __FUNCTION__);
  664. return;
  665. }
  666. if (I_IXOFF(tty) || C_CRTSCTS(tty))
  667. ti_stop_read(tport, tty);
  668. }
  669. static void ti_unthrottle(struct usb_serial_port *port)
  670. {
  671. struct ti_port *tport = usb_get_serial_port_data(port);
  672. struct tty_struct *tty;
  673. int status;
  674. dbg("%s - port %d", __FUNCTION__, port->number);
  675. if (tport == NULL)
  676. return;
  677. tty = port->tty;
  678. if (!tty) {
  679. dbg("%s - no tty", __FUNCTION__);
  680. return;
  681. }
  682. if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
  683. status = ti_restart_read(tport, tty);
  684. if (status)
  685. dev_err(&port->dev, "%s - cannot restart read, %d\n", __FUNCTION__, status);
  686. }
  687. }
  688. static int ti_ioctl(struct usb_serial_port *port, struct file *file,
  689. unsigned int cmd, unsigned long arg)
  690. {
  691. struct ti_port *tport = usb_get_serial_port_data(port);
  692. struct async_icount cnow;
  693. struct async_icount cprev;
  694. dbg("%s - port %d, cmd = 0x%04X", __FUNCTION__, port->number, cmd);
  695. if (tport == NULL)
  696. return -ENODEV;
  697. switch (cmd) {
  698. case TIOCGSERIAL:
  699. dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  700. return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
  701. break;
  702. case TIOCSSERIAL:
  703. dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  704. return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
  705. break;
  706. case TIOCMIWAIT:
  707. dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  708. cprev = tport->tp_icount;
  709. while (1) {
  710. interruptible_sleep_on(&tport->tp_msr_wait);
  711. if (signal_pending(current))
  712. return -ERESTARTSYS;
  713. cnow = tport->tp_icount;
  714. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  715. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  716. return -EIO; /* no change => error */
  717. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  718. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  719. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  720. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  721. return 0;
  722. }
  723. cprev = cnow;
  724. }
  725. break;
  726. case TIOCGICOUNT:
  727. dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
  728. if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
  729. return -EFAULT;
  730. return 0;
  731. }
  732. return -ENOIOCTLCMD;
  733. }
  734. static void ti_set_termios(struct usb_serial_port *port,
  735. struct termios *old_termios)
  736. {
  737. struct ti_port *tport = usb_get_serial_port_data(port);
  738. struct tty_struct *tty = port->tty;
  739. struct ti_uart_config *config;
  740. tcflag_t cflag,iflag;
  741. int baud;
  742. int status;
  743. int port_number = port->number - port->serial->minor;
  744. unsigned int mcr;
  745. dbg("%s - port %d", __FUNCTION__, port->number);
  746. if (!tty || !tty->termios) {
  747. dbg("%s - no tty or termios", __FUNCTION__);
  748. return;
  749. }
  750. cflag = tty->termios->c_cflag;
  751. iflag = tty->termios->c_iflag;
  752. if (old_termios && cflag == old_termios->c_cflag
  753. && iflag == old_termios->c_iflag) {
  754. dbg("%s - nothing to change", __FUNCTION__);
  755. return;
  756. }
  757. dbg("%s - clfag %08x, iflag %08x", __FUNCTION__, cflag, iflag);
  758. if (old_termios)
  759. dbg("%s - old clfag %08x, old iflag %08x", __FUNCTION__, old_termios->c_cflag, old_termios->c_iflag);
  760. if (tport == NULL)
  761. return;
  762. config = kmalloc(sizeof(*config), GFP_KERNEL);
  763. if (!config) {
  764. dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
  765. return;
  766. }
  767. config->wFlags = 0;
  768. /* these flags must be set */
  769. config->wFlags |= TI_UART_ENABLE_MS_INTS;
  770. config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
  771. config->bUartMode = (__u8)(tport->tp_uart_mode);
  772. switch (cflag & CSIZE) {
  773. case CS5:
  774. config->bDataBits = TI_UART_5_DATA_BITS;
  775. break;
  776. case CS6:
  777. config->bDataBits = TI_UART_6_DATA_BITS;
  778. break;
  779. case CS7:
  780. config->bDataBits = TI_UART_7_DATA_BITS;
  781. break;
  782. default:
  783. case CS8:
  784. config->bDataBits = TI_UART_8_DATA_BITS;
  785. break;
  786. }
  787. if (cflag & PARENB) {
  788. if (cflag & PARODD) {
  789. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  790. config->bParity = TI_UART_ODD_PARITY;
  791. } else {
  792. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  793. config->bParity = TI_UART_EVEN_PARITY;
  794. }
  795. } else {
  796. config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
  797. config->bParity = TI_UART_NO_PARITY;
  798. }
  799. if (cflag & CSTOPB)
  800. config->bStopBits = TI_UART_2_STOP_BITS;
  801. else
  802. config->bStopBits = TI_UART_1_STOP_BITS;
  803. if (cflag & CRTSCTS) {
  804. /* RTS flow control must be off to drop RTS for baud rate B0 */
  805. if ((cflag & CBAUD) != B0)
  806. config->wFlags |= TI_UART_ENABLE_RTS_IN;
  807. config->wFlags |= TI_UART_ENABLE_CTS_OUT;
  808. } else {
  809. tty->hw_stopped = 0;
  810. ti_restart_read(tport, tty);
  811. }
  812. if (I_IXOFF(tty) || I_IXON(tty)) {
  813. config->cXon = START_CHAR(tty);
  814. config->cXoff = STOP_CHAR(tty);
  815. if (I_IXOFF(tty))
  816. config->wFlags |= TI_UART_ENABLE_X_IN;
  817. else
  818. ti_restart_read(tport, tty);
  819. if (I_IXON(tty))
  820. config->wFlags |= TI_UART_ENABLE_X_OUT;
  821. }
  822. baud = tty_get_baud_rate(tty);
  823. if (!baud) baud = 9600;
  824. if (tport->tp_tdev->td_is_3410)
  825. config->wBaudRate = (__u16)((923077 + baud/2) / baud);
  826. else
  827. config->wBaudRate = (__u16)((461538 + baud/2) / baud);
  828. dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
  829. __FUNCTION__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
  830. cpu_to_be16s(&config->wBaudRate);
  831. cpu_to_be16s(&config->wFlags);
  832. status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
  833. (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
  834. sizeof(*config));
  835. if (status)
  836. dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __FUNCTION__, port_number, status);
  837. /* SET_CONFIG asserts RTS and DTR, reset them correctly */
  838. mcr = tport->tp_shadow_mcr;
  839. /* if baud rate is B0, clear RTS and DTR */
  840. if ((cflag & CBAUD) == B0)
  841. mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
  842. status = ti_set_mcr(tport, mcr);
  843. if (status)
  844. dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __FUNCTION__, port_number, status);
  845. kfree(config);
  846. }
  847. static int ti_tiocmget(struct usb_serial_port *port, struct file *file)
  848. {
  849. struct ti_port *tport = usb_get_serial_port_data(port);
  850. unsigned int result;
  851. unsigned int msr;
  852. unsigned int mcr;
  853. dbg("%s - port %d", __FUNCTION__, port->number);
  854. if (tport == NULL)
  855. return -ENODEV;
  856. msr = tport->tp_msr;
  857. mcr = tport->tp_shadow_mcr;
  858. result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
  859. | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
  860. | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
  861. | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
  862. | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
  863. | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
  864. | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
  865. dbg("%s - 0x%04X", __FUNCTION__, result);
  866. return result;
  867. }
  868. static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
  869. unsigned int set, unsigned int clear)
  870. {
  871. struct ti_port *tport = usb_get_serial_port_data(port);
  872. unsigned int mcr;
  873. dbg("%s - port %d", __FUNCTION__, port->number);
  874. if (tport == NULL)
  875. return -ENODEV;
  876. mcr = tport->tp_shadow_mcr;
  877. if (set & TIOCM_RTS)
  878. mcr |= TI_MCR_RTS;
  879. if (set & TIOCM_DTR)
  880. mcr |= TI_MCR_DTR;
  881. if (set & TIOCM_LOOP)
  882. mcr |= TI_MCR_LOOP;
  883. if (clear & TIOCM_RTS)
  884. mcr &= ~TI_MCR_RTS;
  885. if (clear & TIOCM_DTR)
  886. mcr &= ~TI_MCR_DTR;
  887. if (clear & TIOCM_LOOP)
  888. mcr &= ~TI_MCR_LOOP;
  889. return ti_set_mcr(tport, mcr);
  890. }
  891. static void ti_break(struct usb_serial_port *port, int break_state)
  892. {
  893. struct ti_port *tport = usb_get_serial_port_data(port);
  894. int status;
  895. dbg("%s - state = %d", __FUNCTION__, break_state);
  896. if (tport == NULL)
  897. return;
  898. ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
  899. status = ti_write_byte(tport->tp_tdev,
  900. tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
  901. TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
  902. if (status)
  903. dbg("%s - error setting break, %d", __FUNCTION__, status);
  904. }
  905. static void ti_interrupt_callback(struct urb *urb, struct pt_regs *regs)
  906. {
  907. struct ti_device *tdev = (struct ti_device *)urb->context;
  908. struct usb_serial_port *port;
  909. struct usb_serial *serial = tdev->td_serial;
  910. struct ti_port *tport;
  911. struct device *dev = &urb->dev->dev;
  912. unsigned char *data = urb->transfer_buffer;
  913. int length = urb->actual_length;
  914. int port_number;
  915. int function;
  916. int status;
  917. __u8 msr;
  918. dbg("%s", __FUNCTION__);
  919. switch (urb->status) {
  920. case 0:
  921. break;
  922. case -ECONNRESET:
  923. case -ENOENT:
  924. case -ESHUTDOWN:
  925. dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
  926. tdev->td_urb_error = 1;
  927. return;
  928. default:
  929. dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status);
  930. tdev->td_urb_error = 1;
  931. goto exit;
  932. }
  933. if (length != 2) {
  934. dbg("%s - bad packet size, %d", __FUNCTION__, length);
  935. goto exit;
  936. }
  937. if (data[0] == TI_CODE_HARDWARE_ERROR) {
  938. dev_err(dev, "%s - hardware error, %d\n", __FUNCTION__, data[1]);
  939. goto exit;
  940. }
  941. port_number = TI_GET_PORT_FROM_CODE(data[0]);
  942. function = TI_GET_FUNC_FROM_CODE(data[0]);
  943. dbg("%s - port_number %d, function %d, data 0x%02X", __FUNCTION__, port_number, function, data[1]);
  944. if (port_number >= serial->num_ports) {
  945. dev_err(dev, "%s - bad port number, %d\n", __FUNCTION__, port_number);
  946. goto exit;
  947. }
  948. port = serial->port[port_number];
  949. tport = usb_get_serial_port_data(port);
  950. if (!tport)
  951. goto exit;
  952. switch (function) {
  953. case TI_CODE_DATA_ERROR:
  954. dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __FUNCTION__, port_number, data[1]);
  955. break;
  956. case TI_CODE_MODEM_STATUS:
  957. msr = data[1];
  958. dbg("%s - port %d, msr 0x%02X", __FUNCTION__, port_number, msr);
  959. ti_handle_new_msr(tport, msr);
  960. break;
  961. default:
  962. dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __FUNCTION__, data[1]);
  963. break;
  964. }
  965. exit:
  966. status = usb_submit_urb(urb, GFP_ATOMIC);
  967. if (status)
  968. dev_err(dev, "%s - resubmit interrupt urb failed, %d\n", __FUNCTION__, status);
  969. }
  970. static void ti_bulk_in_callback(struct urb *urb, struct pt_regs *regs)
  971. {
  972. struct ti_port *tport = (struct ti_port *)urb->context;
  973. struct usb_serial_port *port = tport->tp_port;
  974. struct device *dev = &urb->dev->dev;
  975. int status = 0;
  976. dbg("%s", __FUNCTION__);
  977. switch (urb->status) {
  978. case 0:
  979. break;
  980. case -ECONNRESET:
  981. case -ENOENT:
  982. case -ESHUTDOWN:
  983. dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
  984. tport->tp_tdev->td_urb_error = 1;
  985. wake_up_interruptible(&tport->tp_write_wait);
  986. return;
  987. default:
  988. dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status );
  989. tport->tp_tdev->td_urb_error = 1;
  990. wake_up_interruptible(&tport->tp_write_wait);
  991. }
  992. if (urb->status == -EPIPE)
  993. goto exit;
  994. if (urb->status) {
  995. dev_err(dev, "%s - stopping read!\n", __FUNCTION__);
  996. return;
  997. }
  998. if (port->tty && urb->actual_length) {
  999. usb_serial_debug_data(debug, dev, __FUNCTION__,
  1000. urb->actual_length, urb->transfer_buffer);
  1001. if (!tport->tp_is_open)
  1002. dbg("%s - port closed, dropping data", __FUNCTION__);
  1003. else
  1004. ti_recv(&urb->dev->dev, port->tty, urb->transfer_buffer,
  1005. urb->actual_length);
  1006. spin_lock(&tport->tp_lock);
  1007. tport->tp_icount.rx += urb->actual_length;
  1008. spin_unlock(&tport->tp_lock);
  1009. }
  1010. exit:
  1011. /* continue to read unless stopping */
  1012. spin_lock(&tport->tp_lock);
  1013. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
  1014. urb->dev = port->serial->dev;
  1015. status = usb_submit_urb(urb, GFP_ATOMIC);
  1016. } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
  1017. tport->tp_read_urb_state = TI_READ_URB_STOPPED;
  1018. }
  1019. spin_unlock(&tport->tp_lock);
  1020. if (status)
  1021. dev_err(dev, "%s - resubmit read urb failed, %d\n", __FUNCTION__, status);
  1022. }
  1023. static void ti_bulk_out_callback(struct urb *urb, struct pt_regs *regs)
  1024. {
  1025. struct ti_port *tport = (struct ti_port *)urb->context;
  1026. struct usb_serial_port *port = tport->tp_port;
  1027. struct device *dev = &urb->dev->dev;
  1028. dbg("%s - port %d", __FUNCTION__, port->number);
  1029. tport->tp_write_urb_in_use = 0;
  1030. switch (urb->status) {
  1031. case 0:
  1032. break;
  1033. case -ECONNRESET:
  1034. case -ENOENT:
  1035. case -ESHUTDOWN:
  1036. dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
  1037. tport->tp_tdev->td_urb_error = 1;
  1038. wake_up_interruptible(&tport->tp_write_wait);
  1039. return;
  1040. default:
  1041. dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status);
  1042. tport->tp_tdev->td_urb_error = 1;
  1043. wake_up_interruptible(&tport->tp_write_wait);
  1044. }
  1045. /* send any buffered data */
  1046. ti_send(tport);
  1047. }
  1048. static void ti_recv(struct device *dev, struct tty_struct *tty,
  1049. unsigned char *data, int length)
  1050. {
  1051. int cnt;
  1052. do {
  1053. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  1054. tty_flip_buffer_push(tty);
  1055. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  1056. dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length);
  1057. return;
  1058. }
  1059. }
  1060. cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
  1061. memcpy(tty->flip.char_buf_ptr, data, cnt);
  1062. memset(tty->flip.flag_buf_ptr, 0, cnt);
  1063. tty->flip.char_buf_ptr += cnt;
  1064. tty->flip.flag_buf_ptr += cnt;
  1065. tty->flip.count += cnt;
  1066. data += cnt;
  1067. length -= cnt;
  1068. } while (length > 0);
  1069. tty_flip_buffer_push(tty);
  1070. }
  1071. static void ti_send(struct ti_port *tport)
  1072. {
  1073. int count, result;
  1074. struct usb_serial_port *port = tport->tp_port;
  1075. struct tty_struct *tty = port->tty;
  1076. unsigned long flags;
  1077. dbg("%s - port %d", __FUNCTION__, port->number);
  1078. spin_lock_irqsave(&tport->tp_lock, flags);
  1079. if (tport->tp_write_urb_in_use) {
  1080. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1081. return;
  1082. }
  1083. count = ti_buf_get(tport->tp_write_buf,
  1084. port->write_urb->transfer_buffer,
  1085. port->bulk_out_size);
  1086. if (count == 0) {
  1087. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1088. return;
  1089. }
  1090. tport->tp_write_urb_in_use = 1;
  1091. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1092. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
  1093. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  1094. usb_sndbulkpipe(port->serial->dev,
  1095. port->bulk_out_endpointAddress),
  1096. port->write_urb->transfer_buffer, count,
  1097. ti_bulk_out_callback, tport);
  1098. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1099. if (result) {
  1100. dev_err(&port->dev, "%s - submit write urb failed, %d\n", __FUNCTION__, result);
  1101. tport->tp_write_urb_in_use = 0;
  1102. /* TODO: reschedule ti_send */
  1103. } else {
  1104. spin_lock_irqsave(&tport->tp_lock, flags);
  1105. tport->tp_icount.tx += count;
  1106. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1107. }
  1108. /* more room in the buffer for new writes, wakeup */
  1109. if (tty)
  1110. tty_wakeup(tty);
  1111. wake_up_interruptible(&tport->tp_write_wait);
  1112. }
  1113. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
  1114. {
  1115. int status;
  1116. status = ti_write_byte(tport->tp_tdev,
  1117. tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
  1118. TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
  1119. if (!status)
  1120. tport->tp_shadow_mcr = mcr;
  1121. return status;
  1122. }
  1123. static int ti_get_lsr(struct ti_port *tport)
  1124. {
  1125. int size,status;
  1126. struct ti_device *tdev = tport->tp_tdev;
  1127. struct usb_serial_port *port = tport->tp_port;
  1128. int port_number = port->number - port->serial->minor;
  1129. struct ti_port_status *data;
  1130. dbg("%s - port %d", __FUNCTION__, port->number);
  1131. size = sizeof(struct ti_port_status);
  1132. data = kmalloc(size, GFP_KERNEL);
  1133. if (!data) {
  1134. dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
  1135. return -ENOMEM;
  1136. }
  1137. status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
  1138. (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
  1139. if (status) {
  1140. dev_err(&port->dev, "%s - get port status command failed, %d\n", __FUNCTION__, status);
  1141. goto free_data;
  1142. }
  1143. dbg("%s - lsr 0x%02X", __FUNCTION__, data->bLSR);
  1144. tport->tp_lsr = data->bLSR;
  1145. free_data:
  1146. kfree(data);
  1147. return status;
  1148. }
  1149. static int ti_get_serial_info(struct ti_port *tport,
  1150. struct serial_struct __user *ret_arg)
  1151. {
  1152. struct usb_serial_port *port = tport->tp_port;
  1153. struct serial_struct ret_serial;
  1154. if (!ret_arg)
  1155. return -EFAULT;
  1156. memset(&ret_serial, 0, sizeof(ret_serial));
  1157. ret_serial.type = PORT_16550A;
  1158. ret_serial.line = port->serial->minor;
  1159. ret_serial.port = port->number - port->serial->minor;
  1160. ret_serial.flags = tport->tp_flags;
  1161. ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
  1162. ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
  1163. ret_serial.closing_wait = tport->tp_closing_wait;
  1164. if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
  1165. return -EFAULT;
  1166. return 0;
  1167. }
  1168. static int ti_set_serial_info(struct ti_port *tport,
  1169. struct serial_struct __user *new_arg)
  1170. {
  1171. struct usb_serial_port *port = tport->tp_port;
  1172. struct serial_struct new_serial;
  1173. if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
  1174. return -EFAULT;
  1175. tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
  1176. if (port->tty)
  1177. port->tty->low_latency =
  1178. (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1179. tport->tp_closing_wait = new_serial.closing_wait;
  1180. return 0;
  1181. }
  1182. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
  1183. {
  1184. struct async_icount *icount;
  1185. struct tty_struct *tty;
  1186. unsigned long flags;
  1187. dbg("%s - msr 0x%02X", __FUNCTION__, msr);
  1188. if (msr & TI_MSR_DELTA_MASK) {
  1189. spin_lock_irqsave(&tport->tp_lock, flags);
  1190. icount = &tport->tp_icount;
  1191. if (msr & TI_MSR_DELTA_CTS)
  1192. icount->cts++;
  1193. if (msr & TI_MSR_DELTA_DSR)
  1194. icount->dsr++;
  1195. if (msr & TI_MSR_DELTA_CD)
  1196. icount->dcd++;
  1197. if (msr & TI_MSR_DELTA_RI)
  1198. icount->rng++;
  1199. wake_up_interruptible(&tport->tp_msr_wait);
  1200. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1201. }
  1202. tport->tp_msr = msr & TI_MSR_MASK;
  1203. /* handle CTS flow control */
  1204. tty = tport->tp_port->tty;
  1205. if (tty && C_CRTSCTS(tty)) {
  1206. if (msr & TI_MSR_CTS) {
  1207. tty->hw_stopped = 0;
  1208. tty_wakeup(tty);
  1209. } else {
  1210. tty->hw_stopped = 1;
  1211. }
  1212. }
  1213. }
  1214. static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
  1215. {
  1216. struct ti_device *tdev = tport->tp_tdev;
  1217. struct usb_serial_port *port = tport->tp_port;
  1218. wait_queue_t wait;
  1219. unsigned long flags;
  1220. dbg("%s - port %d", __FUNCTION__, port->number);
  1221. spin_lock_irqsave(&tport->tp_lock, flags);
  1222. /* wait for data to drain from the buffer */
  1223. tdev->td_urb_error = 0;
  1224. init_waitqueue_entry(&wait, current);
  1225. add_wait_queue(&tport->tp_write_wait, &wait);
  1226. for (;;) {
  1227. set_current_state(TASK_INTERRUPTIBLE);
  1228. if (ti_buf_data_avail(tport->tp_write_buf) == 0
  1229. || timeout == 0 || signal_pending(current)
  1230. || tdev->td_urb_error
  1231. || !usb_get_intfdata(port->serial->interface)) /* disconnect */
  1232. break;
  1233. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1234. timeout = schedule_timeout(timeout);
  1235. spin_lock_irqsave(&tport->tp_lock, flags);
  1236. }
  1237. set_current_state(TASK_RUNNING);
  1238. remove_wait_queue(&tport->tp_write_wait, &wait);
  1239. /* flush any remaining data in the buffer */
  1240. if (flush)
  1241. ti_buf_clear(tport->tp_write_buf);
  1242. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1243. /* wait for data to drain from the device */
  1244. /* wait for empty tx register, plus 20 ms */
  1245. timeout += jiffies;
  1246. tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
  1247. while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
  1248. && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
  1249. && usb_get_intfdata(port->serial->interface)) { /* not disconnected */
  1250. if (ti_get_lsr(tport))
  1251. break;
  1252. msleep_interruptible(20);
  1253. }
  1254. }
  1255. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
  1256. {
  1257. unsigned long flags;
  1258. spin_lock_irqsave(&tport->tp_lock, flags);
  1259. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1260. tport->tp_read_urb_state = TI_READ_URB_STOPPING;
  1261. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1262. }
  1263. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
  1264. {
  1265. struct urb *urb;
  1266. int status = 0;
  1267. unsigned long flags;
  1268. spin_lock_irqsave(&tport->tp_lock, flags);
  1269. if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
  1270. urb = tport->tp_port->read_urb;
  1271. urb->complete = ti_bulk_in_callback;
  1272. urb->context = tport;
  1273. urb->dev = tport->tp_port->serial->dev;
  1274. status = usb_submit_urb(urb, GFP_KERNEL);
  1275. }
  1276. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1277. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1278. return status;
  1279. }
  1280. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  1281. __u16 moduleid, __u16 value, __u8 *data, int size)
  1282. {
  1283. int status;
  1284. status = usb_control_msg(tdev->td_serial->dev,
  1285. usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
  1286. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  1287. value, moduleid, data, size, 1000);
  1288. if (status == size)
  1289. status = 0;
  1290. if (status > 0)
  1291. status = -ECOMM;
  1292. return status;
  1293. }
  1294. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  1295. __u16 moduleid, __u16 value, __u8 *data, int size)
  1296. {
  1297. int status;
  1298. status = usb_control_msg(tdev->td_serial->dev,
  1299. usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
  1300. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  1301. value, moduleid, data, size, 1000);
  1302. if (status == size)
  1303. status = 0;
  1304. if (status > 0)
  1305. status = -ECOMM;
  1306. return status;
  1307. }
  1308. static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
  1309. __u8 mask, __u8 byte)
  1310. {
  1311. int status;
  1312. unsigned int size;
  1313. struct ti_write_data_bytes *data;
  1314. struct device *dev = &tdev->td_serial->dev->dev;
  1315. dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", __FUNCTION__, addr, mask, byte);
  1316. size = sizeof(struct ti_write_data_bytes) + 2;
  1317. data = kmalloc(size, GFP_KERNEL);
  1318. if (!data) {
  1319. dev_err(dev, "%s - out of memory\n", __FUNCTION__);
  1320. return -ENOMEM;
  1321. }
  1322. data->bAddrType = TI_RW_DATA_ADDR_XDATA;
  1323. data->bDataType = TI_RW_DATA_BYTE;
  1324. data->bDataCounter = 1;
  1325. data->wBaseAddrHi = cpu_to_be16(addr>>16);
  1326. data->wBaseAddrLo = cpu_to_be16(addr);
  1327. data->bData[0] = mask;
  1328. data->bData[1] = byte;
  1329. status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
  1330. (__u8 *)data, size);
  1331. if (status < 0)
  1332. dev_err(dev, "%s - failed, %d\n", __FUNCTION__, status);
  1333. kfree(data);
  1334. return status;
  1335. }
  1336. static int ti_download_firmware(struct ti_device *tdev,
  1337. unsigned char *firmware, unsigned int firmware_size)
  1338. {
  1339. int status = 0;
  1340. int buffer_size;
  1341. int pos;
  1342. int len;
  1343. int done;
  1344. __u8 cs = 0;
  1345. __u8 *buffer;
  1346. struct usb_device *dev = tdev->td_serial->dev;
  1347. struct ti_firmware_header *header;
  1348. unsigned int pipe = usb_sndbulkpipe(dev,
  1349. tdev->td_serial->port[0]->bulk_out_endpointAddress);
  1350. buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
  1351. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1352. if (!buffer) {
  1353. dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
  1354. return -ENOMEM;
  1355. }
  1356. memcpy(buffer, firmware, firmware_size);
  1357. memset(buffer+firmware_size, 0xff, buffer_size-firmware_size);
  1358. for(pos = sizeof(struct ti_firmware_header); pos < buffer_size; pos++)
  1359. cs = (__u8)(cs + buffer[pos]);
  1360. header = (struct ti_firmware_header *)buffer;
  1361. header->wLength = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_firmware_header)));
  1362. header->bCheckSum = cs;
  1363. dbg("%s - downloading firmware", __FUNCTION__);
  1364. for (pos = 0; pos < buffer_size; pos += done) {
  1365. len = min(buffer_size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
  1366. status = usb_bulk_msg(dev, pipe, buffer+pos, len, &done, 1000);
  1367. if (status)
  1368. break;
  1369. }
  1370. kfree(buffer);
  1371. if (status) {
  1372. dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __FUNCTION__, status);
  1373. return status;
  1374. }
  1375. dbg("%s - download successful", __FUNCTION__);
  1376. return 0;
  1377. }
  1378. /* Circular Buffer Functions */
  1379. /*
  1380. * ti_buf_alloc
  1381. *
  1382. * Allocate a circular buffer and all associated memory.
  1383. */
  1384. static struct circ_buf *ti_buf_alloc(void)
  1385. {
  1386. struct circ_buf *cb;
  1387. cb = (struct circ_buf *)kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
  1388. if (cb == NULL)
  1389. return NULL;
  1390. cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
  1391. if (cb->buf == NULL) {
  1392. kfree(cb);
  1393. return NULL;
  1394. }
  1395. ti_buf_clear(cb);
  1396. return cb;
  1397. }
  1398. /*
  1399. * ti_buf_free
  1400. *
  1401. * Free the buffer and all associated memory.
  1402. */
  1403. static void ti_buf_free(struct circ_buf *cb)
  1404. {
  1405. kfree(cb->buf);
  1406. kfree(cb);
  1407. }
  1408. /*
  1409. * ti_buf_clear
  1410. *
  1411. * Clear out all data in the circular buffer.
  1412. */
  1413. static void ti_buf_clear(struct circ_buf *cb)
  1414. {
  1415. cb->head = cb->tail = 0;
  1416. }
  1417. /*
  1418. * ti_buf_data_avail
  1419. *
  1420. * Return the number of bytes of data available in the circular
  1421. * buffer.
  1422. */
  1423. static int ti_buf_data_avail(struct circ_buf *cb)
  1424. {
  1425. return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
  1426. }
  1427. /*
  1428. * ti_buf_space_avail
  1429. *
  1430. * Return the number of bytes of space available in the circular
  1431. * buffer.
  1432. */
  1433. static int ti_buf_space_avail(struct circ_buf *cb)
  1434. {
  1435. return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
  1436. }
  1437. /*
  1438. * ti_buf_put
  1439. *
  1440. * Copy data data from a user buffer and put it into the circular buffer.
  1441. * Restrict to the amount of space available.
  1442. *
  1443. * Return the number of bytes copied.
  1444. */
  1445. static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
  1446. {
  1447. int c, ret = 0;
  1448. while (1) {
  1449. c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
  1450. if (count < c)
  1451. c = count;
  1452. if (c <= 0)
  1453. break;
  1454. memcpy(cb->buf + cb->head, buf, c);
  1455. cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
  1456. buf += c;
  1457. count -= c;
  1458. ret += c;
  1459. }
  1460. return ret;
  1461. }
  1462. /*
  1463. * ti_buf_get
  1464. *
  1465. * Get data from the circular buffer and copy to the given buffer.
  1466. * Restrict to the amount of data available.
  1467. *
  1468. * Return the number of bytes copied.
  1469. */
  1470. static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
  1471. {
  1472. int c, ret = 0;
  1473. while (1) {
  1474. c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
  1475. if (count < c)
  1476. c = count;
  1477. if (c <= 0)
  1478. break;
  1479. memcpy(buf, cb->buf + cb->tail, c);
  1480. cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
  1481. buf += c;
  1482. count -= c;
  1483. ret += c;
  1484. }
  1485. return ret;
  1486. }