ti_usb_3410_5052.c 47 KB

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