ti_usb_3410_5052.c 48 KB

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