ti_usb_3410_5052.c 47 KB

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