ti_usb_3410_5052.c 48 KB

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