ti_usb_3410_5052.c 47 KB

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