ti_usb_3410_5052.c 47 KB

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