ti_usb_3410_5052.c 49 KB

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