ti_usb_3410_5052.c 47 KB

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