ti_usb_3410_5052.c 48 KB

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