ti_usb_3410_5052.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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 bool 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->context = tdev;
  459. status = usb_submit_urb(urb, GFP_KERNEL);
  460. if (status) {
  461. dev_err(&port->dev,
  462. "%s - submit interrupt urb failed, %d\n",
  463. __func__, status);
  464. goto release_lock;
  465. }
  466. }
  467. if (tty)
  468. ti_set_termios(tty, port, tty->termios);
  469. dbg("%s - sending TI_OPEN_PORT", __func__);
  470. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  471. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  472. if (status) {
  473. dev_err(&port->dev, "%s - cannot send open command, %d\n",
  474. __func__, status);
  475. goto unlink_int_urb;
  476. }
  477. dbg("%s - sending TI_START_PORT", __func__);
  478. status = ti_command_out_sync(tdev, TI_START_PORT,
  479. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  480. if (status) {
  481. dev_err(&port->dev, "%s - cannot send start command, %d\n",
  482. __func__, status);
  483. goto unlink_int_urb;
  484. }
  485. dbg("%s - sending TI_PURGE_PORT", __func__);
  486. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  487. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
  488. if (status) {
  489. dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
  490. __func__, status);
  491. goto unlink_int_urb;
  492. }
  493. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  494. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
  495. if (status) {
  496. dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
  497. __func__, status);
  498. goto unlink_int_urb;
  499. }
  500. /* reset the data toggle on the bulk endpoints to work around bug in
  501. * host controllers where things get out of sync some times */
  502. usb_clear_halt(dev, port->write_urb->pipe);
  503. usb_clear_halt(dev, port->read_urb->pipe);
  504. if (tty)
  505. ti_set_termios(tty, port, tty->termios);
  506. dbg("%s - sending TI_OPEN_PORT (2)", __func__);
  507. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  508. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  509. if (status) {
  510. dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
  511. __func__, status);
  512. goto unlink_int_urb;
  513. }
  514. dbg("%s - sending TI_START_PORT (2)", __func__);
  515. status = ti_command_out_sync(tdev, TI_START_PORT,
  516. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  517. if (status) {
  518. dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
  519. __func__, status);
  520. goto unlink_int_urb;
  521. }
  522. /* start read urb */
  523. dbg("%s - start read urb", __func__);
  524. urb = port->read_urb;
  525. if (!urb) {
  526. dev_err(&port->dev, "%s - no read urb\n", __func__);
  527. status = -EINVAL;
  528. goto unlink_int_urb;
  529. }
  530. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  531. urb->context = tport;
  532. status = usb_submit_urb(urb, GFP_KERNEL);
  533. if (status) {
  534. dev_err(&port->dev, "%s - submit read urb failed, %d\n",
  535. __func__, status);
  536. goto unlink_int_urb;
  537. }
  538. tport->tp_is_open = 1;
  539. ++tdev->td_open_port_count;
  540. goto release_lock;
  541. unlink_int_urb:
  542. if (tdev->td_open_port_count == 0)
  543. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  544. release_lock:
  545. mutex_unlock(&tdev->td_open_close_lock);
  546. dbg("%s - exit %d", __func__, status);
  547. return status;
  548. }
  549. static void ti_close(struct usb_serial_port *port)
  550. {
  551. struct ti_device *tdev;
  552. struct ti_port *tport;
  553. int port_number;
  554. int status;
  555. int do_unlock;
  556. dbg("%s - port %d", __func__, port->number);
  557. tdev = usb_get_serial_data(port->serial);
  558. tport = usb_get_serial_port_data(port);
  559. if (tdev == NULL || tport == NULL)
  560. return;
  561. tport->tp_is_open = 0;
  562. ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
  563. usb_kill_urb(port->read_urb);
  564. usb_kill_urb(port->write_urb);
  565. tport->tp_write_urb_in_use = 0;
  566. port_number = port->number - port->serial->minor;
  567. dbg("%s - sending TI_CLOSE_PORT", __func__);
  568. status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
  569. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  570. if (status)
  571. dev_err(&port->dev,
  572. "%s - cannot send close port command, %d\n"
  573. , __func__, status);
  574. /* if mutex_lock is interrupted, continue anyway */
  575. do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
  576. --tport->tp_tdev->td_open_port_count;
  577. if (tport->tp_tdev->td_open_port_count <= 0) {
  578. /* last port is closed, shut down interrupt urb */
  579. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  580. tport->tp_tdev->td_open_port_count = 0;
  581. }
  582. if (do_unlock)
  583. mutex_unlock(&tdev->td_open_close_lock);
  584. dbg("%s - exit", __func__);
  585. }
  586. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  587. const unsigned char *data, int count)
  588. {
  589. struct ti_port *tport = usb_get_serial_port_data(port);
  590. dbg("%s - port %d", __func__, port->number);
  591. if (count == 0) {
  592. dbg("%s - write request of 0 bytes", __func__);
  593. return 0;
  594. }
  595. if (tport == NULL || !tport->tp_is_open)
  596. return -ENODEV;
  597. count = kfifo_in_locked(&tport->write_fifo, data, count,
  598. &tport->tp_lock);
  599. ti_send(tport);
  600. return count;
  601. }
  602. static int ti_write_room(struct tty_struct *tty)
  603. {
  604. struct usb_serial_port *port = tty->driver_data;
  605. struct ti_port *tport = usb_get_serial_port_data(port);
  606. int room = 0;
  607. unsigned long flags;
  608. dbg("%s - port %d", __func__, port->number);
  609. if (tport == NULL)
  610. return 0;
  611. spin_lock_irqsave(&tport->tp_lock, flags);
  612. room = kfifo_avail(&tport->write_fifo);
  613. spin_unlock_irqrestore(&tport->tp_lock, flags);
  614. dbg("%s - returns %d", __func__, room);
  615. return room;
  616. }
  617. static int ti_chars_in_buffer(struct tty_struct *tty)
  618. {
  619. struct usb_serial_port *port = tty->driver_data;
  620. struct ti_port *tport = usb_get_serial_port_data(port);
  621. int chars = 0;
  622. unsigned long flags;
  623. dbg("%s - port %d", __func__, port->number);
  624. if (tport == NULL)
  625. return 0;
  626. spin_lock_irqsave(&tport->tp_lock, flags);
  627. chars = kfifo_len(&tport->write_fifo);
  628. spin_unlock_irqrestore(&tport->tp_lock, flags);
  629. dbg("%s - returns %d", __func__, chars);
  630. return chars;
  631. }
  632. static void ti_throttle(struct tty_struct *tty)
  633. {
  634. struct usb_serial_port *port = tty->driver_data;
  635. struct ti_port *tport = usb_get_serial_port_data(port);
  636. dbg("%s - port %d", __func__, port->number);
  637. if (tport == NULL)
  638. return;
  639. if (I_IXOFF(tty) || C_CRTSCTS(tty))
  640. ti_stop_read(tport, tty);
  641. }
  642. static void ti_unthrottle(struct tty_struct *tty)
  643. {
  644. struct usb_serial_port *port = tty->driver_data;
  645. struct ti_port *tport = usb_get_serial_port_data(port);
  646. int status;
  647. dbg("%s - port %d", __func__, port->number);
  648. if (tport == NULL)
  649. return;
  650. if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
  651. status = ti_restart_read(tport, tty);
  652. if (status)
  653. dev_err(&port->dev, "%s - cannot restart read, %d\n",
  654. __func__, status);
  655. }
  656. }
  657. static int ti_get_icount(struct tty_struct *tty,
  658. struct serial_icounter_struct *icount)
  659. {
  660. struct usb_serial_port *port = tty->driver_data;
  661. struct ti_port *tport = usb_get_serial_port_data(port);
  662. struct async_icount cnow = tport->tp_icount;
  663. dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
  664. __func__, port->number,
  665. cnow.rx, cnow.tx);
  666. icount->cts = cnow.cts;
  667. icount->dsr = cnow.dsr;
  668. icount->rng = cnow.rng;
  669. icount->dcd = cnow.dcd;
  670. icount->rx = cnow.rx;
  671. icount->tx = cnow.tx;
  672. icount->frame = cnow.frame;
  673. icount->overrun = cnow.overrun;
  674. icount->parity = cnow.parity;
  675. icount->brk = cnow.brk;
  676. icount->buf_overrun = cnow.buf_overrun;
  677. return 0;
  678. }
  679. static int ti_ioctl(struct tty_struct *tty,
  680. unsigned int cmd, unsigned long arg)
  681. {
  682. struct usb_serial_port *port = tty->driver_data;
  683. struct ti_port *tport = usb_get_serial_port_data(port);
  684. struct async_icount cnow;
  685. struct async_icount cprev;
  686. dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
  687. if (tport == NULL)
  688. return -ENODEV;
  689. switch (cmd) {
  690. case TIOCGSERIAL:
  691. dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
  692. return ti_get_serial_info(tport,
  693. (struct serial_struct __user *)arg);
  694. case TIOCSSERIAL:
  695. dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
  696. return ti_set_serial_info(tty, tport,
  697. (struct serial_struct __user *)arg);
  698. case TIOCMIWAIT:
  699. dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
  700. cprev = tport->tp_icount;
  701. while (1) {
  702. interruptible_sleep_on(&tport->tp_msr_wait);
  703. if (signal_pending(current))
  704. return -ERESTARTSYS;
  705. cnow = tport->tp_icount;
  706. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  707. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  708. return -EIO; /* no change => error */
  709. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  710. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  711. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  712. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
  713. return 0;
  714. cprev = cnow;
  715. }
  716. break;
  717. }
  718. return -ENOIOCTLCMD;
  719. }
  720. static void ti_set_termios(struct tty_struct *tty,
  721. struct usb_serial_port *port, struct ktermios *old_termios)
  722. {
  723. struct ti_port *tport = usb_get_serial_port_data(port);
  724. struct ti_uart_config *config;
  725. tcflag_t cflag, iflag;
  726. int baud;
  727. int status;
  728. int port_number = port->number - port->serial->minor;
  729. unsigned int mcr;
  730. dbg("%s - port %d", __func__, port->number);
  731. cflag = tty->termios->c_cflag;
  732. iflag = tty->termios->c_iflag;
  733. dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
  734. dbg("%s - old clfag %08x, old iflag %08x", __func__,
  735. old_termios->c_cflag, old_termios->c_iflag);
  736. if (tport == NULL)
  737. return;
  738. config = kmalloc(sizeof(*config), GFP_KERNEL);
  739. if (!config) {
  740. dev_err(&port->dev, "%s - out of memory\n", __func__);
  741. return;
  742. }
  743. config->wFlags = 0;
  744. /* these flags must be set */
  745. config->wFlags |= TI_UART_ENABLE_MS_INTS;
  746. config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
  747. config->bUartMode = (__u8)(tport->tp_uart_mode);
  748. switch (cflag & CSIZE) {
  749. case CS5:
  750. config->bDataBits = TI_UART_5_DATA_BITS;
  751. break;
  752. case CS6:
  753. config->bDataBits = TI_UART_6_DATA_BITS;
  754. break;
  755. case CS7:
  756. config->bDataBits = TI_UART_7_DATA_BITS;
  757. break;
  758. default:
  759. case CS8:
  760. config->bDataBits = TI_UART_8_DATA_BITS;
  761. break;
  762. }
  763. /* CMSPAR isn't supported by this driver */
  764. tty->termios->c_cflag &= ~CMSPAR;
  765. if (cflag & PARENB) {
  766. if (cflag & PARODD) {
  767. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  768. config->bParity = TI_UART_ODD_PARITY;
  769. } else {
  770. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  771. config->bParity = TI_UART_EVEN_PARITY;
  772. }
  773. } else {
  774. config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
  775. config->bParity = TI_UART_NO_PARITY;
  776. }
  777. if (cflag & CSTOPB)
  778. config->bStopBits = TI_UART_2_STOP_BITS;
  779. else
  780. config->bStopBits = TI_UART_1_STOP_BITS;
  781. if (cflag & CRTSCTS) {
  782. /* RTS flow control must be off to drop RTS for baud rate B0 */
  783. if ((cflag & CBAUD) != B0)
  784. config->wFlags |= TI_UART_ENABLE_RTS_IN;
  785. config->wFlags |= TI_UART_ENABLE_CTS_OUT;
  786. } else {
  787. tty->hw_stopped = 0;
  788. ti_restart_read(tport, tty);
  789. }
  790. if (I_IXOFF(tty) || I_IXON(tty)) {
  791. config->cXon = START_CHAR(tty);
  792. config->cXoff = STOP_CHAR(tty);
  793. if (I_IXOFF(tty))
  794. config->wFlags |= TI_UART_ENABLE_X_IN;
  795. else
  796. ti_restart_read(tport, tty);
  797. if (I_IXON(tty))
  798. config->wFlags |= TI_UART_ENABLE_X_OUT;
  799. }
  800. baud = tty_get_baud_rate(tty);
  801. if (!baud)
  802. baud = 9600;
  803. if (tport->tp_tdev->td_is_3410)
  804. config->wBaudRate = (__u16)((923077 + baud/2) / baud);
  805. else
  806. config->wBaudRate = (__u16)((461538 + baud/2) / baud);
  807. /* FIXME: Should calculate resulting baud here and report it back */
  808. if ((cflag & CBAUD) != B0)
  809. tty_encode_baud_rate(tty, baud, baud);
  810. dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
  811. __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
  812. cpu_to_be16s(&config->wBaudRate);
  813. cpu_to_be16s(&config->wFlags);
  814. status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
  815. (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
  816. sizeof(*config));
  817. if (status)
  818. dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
  819. __func__, port_number, status);
  820. /* SET_CONFIG asserts RTS and DTR, reset them correctly */
  821. mcr = tport->tp_shadow_mcr;
  822. /* if baud rate is B0, clear RTS and DTR */
  823. if ((cflag & CBAUD) == B0)
  824. mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
  825. status = ti_set_mcr(tport, mcr);
  826. if (status)
  827. dev_err(&port->dev,
  828. "%s - cannot set modem control on port %d, %d\n",
  829. __func__, port_number, status);
  830. kfree(config);
  831. }
  832. static int ti_tiocmget(struct tty_struct *tty)
  833. {
  834. struct usb_serial_port *port = tty->driver_data;
  835. struct ti_port *tport = usb_get_serial_port_data(port);
  836. unsigned int result;
  837. unsigned int msr;
  838. unsigned int mcr;
  839. unsigned long flags;
  840. dbg("%s - port %d", __func__, port->number);
  841. if (tport == NULL)
  842. return -ENODEV;
  843. spin_lock_irqsave(&tport->tp_lock, flags);
  844. msr = tport->tp_msr;
  845. mcr = tport->tp_shadow_mcr;
  846. spin_unlock_irqrestore(&tport->tp_lock, flags);
  847. result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
  848. | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
  849. | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
  850. | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
  851. | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
  852. | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
  853. | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
  854. dbg("%s - 0x%04X", __func__, result);
  855. return result;
  856. }
  857. static int ti_tiocmset(struct tty_struct *tty,
  858. unsigned int set, unsigned int clear)
  859. {
  860. struct usb_serial_port *port = tty->driver_data;
  861. struct ti_port *tport = usb_get_serial_port_data(port);
  862. unsigned int mcr;
  863. unsigned long flags;
  864. dbg("%s - port %d", __func__, port->number);
  865. if (tport == NULL)
  866. return -ENODEV;
  867. spin_lock_irqsave(&tport->tp_lock, flags);
  868. mcr = tport->tp_shadow_mcr;
  869. if (set & TIOCM_RTS)
  870. mcr |= TI_MCR_RTS;
  871. if (set & TIOCM_DTR)
  872. mcr |= TI_MCR_DTR;
  873. if (set & TIOCM_LOOP)
  874. mcr |= TI_MCR_LOOP;
  875. if (clear & TIOCM_RTS)
  876. mcr &= ~TI_MCR_RTS;
  877. if (clear & TIOCM_DTR)
  878. mcr &= ~TI_MCR_DTR;
  879. if (clear & TIOCM_LOOP)
  880. mcr &= ~TI_MCR_LOOP;
  881. spin_unlock_irqrestore(&tport->tp_lock, flags);
  882. return ti_set_mcr(tport, mcr);
  883. }
  884. static void ti_break(struct tty_struct *tty, int break_state)
  885. {
  886. struct usb_serial_port *port = tty->driver_data;
  887. struct ti_port *tport = usb_get_serial_port_data(port);
  888. int status;
  889. dbg("%s - state = %d", __func__, break_state);
  890. if (tport == NULL)
  891. return;
  892. ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
  893. status = ti_write_byte(tport->tp_tdev,
  894. tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
  895. TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
  896. if (status)
  897. dbg("%s - error setting break, %d", __func__, status);
  898. }
  899. static void ti_interrupt_callback(struct urb *urb)
  900. {
  901. struct ti_device *tdev = urb->context;
  902. struct usb_serial_port *port;
  903. struct usb_serial *serial = tdev->td_serial;
  904. struct ti_port *tport;
  905. struct device *dev = &urb->dev->dev;
  906. unsigned char *data = urb->transfer_buffer;
  907. int length = urb->actual_length;
  908. int port_number;
  909. int function;
  910. int status = urb->status;
  911. int retval;
  912. __u8 msr;
  913. dbg("%s", __func__);
  914. switch (status) {
  915. case 0:
  916. break;
  917. case -ECONNRESET:
  918. case -ENOENT:
  919. case -ESHUTDOWN:
  920. dbg("%s - urb shutting down, %d", __func__, status);
  921. tdev->td_urb_error = 1;
  922. return;
  923. default:
  924. dev_err(dev, "%s - nonzero urb status, %d\n",
  925. __func__, status);
  926. tdev->td_urb_error = 1;
  927. goto exit;
  928. }
  929. if (length != 2) {
  930. dbg("%s - bad packet size, %d", __func__, length);
  931. goto exit;
  932. }
  933. if (data[0] == TI_CODE_HARDWARE_ERROR) {
  934. dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
  935. goto exit;
  936. }
  937. port_number = TI_GET_PORT_FROM_CODE(data[0]);
  938. function = TI_GET_FUNC_FROM_CODE(data[0]);
  939. dbg("%s - port_number %d, function %d, data 0x%02X",
  940. __func__, port_number, function, data[1]);
  941. if (port_number >= serial->num_ports) {
  942. dev_err(dev, "%s - bad port number, %d\n",
  943. __func__, port_number);
  944. goto exit;
  945. }
  946. port = serial->port[port_number];
  947. tport = usb_get_serial_port_data(port);
  948. if (!tport)
  949. goto exit;
  950. switch (function) {
  951. case TI_CODE_DATA_ERROR:
  952. dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
  953. __func__, port_number, data[1]);
  954. break;
  955. case TI_CODE_MODEM_STATUS:
  956. msr = data[1];
  957. dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
  958. ti_handle_new_msr(tport, msr);
  959. break;
  960. default:
  961. dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
  962. __func__, data[1]);
  963. break;
  964. }
  965. exit:
  966. retval = usb_submit_urb(urb, GFP_ATOMIC);
  967. if (retval)
  968. dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
  969. __func__, retval);
  970. }
  971. static void ti_bulk_in_callback(struct urb *urb)
  972. {
  973. struct ti_port *tport = urb->context;
  974. struct usb_serial_port *port = tport->tp_port;
  975. struct device *dev = &urb->dev->dev;
  976. int status = urb->status;
  977. int retval = 0;
  978. struct tty_struct *tty;
  979. dbg("%s", __func__);
  980. switch (status) {
  981. case 0:
  982. break;
  983. case -ECONNRESET:
  984. case -ENOENT:
  985. case -ESHUTDOWN:
  986. dbg("%s - urb shutting down, %d", __func__, status);
  987. tport->tp_tdev->td_urb_error = 1;
  988. wake_up_interruptible(&tport->tp_write_wait);
  989. return;
  990. default:
  991. dev_err(dev, "%s - nonzero urb status, %d\n",
  992. __func__, status);
  993. tport->tp_tdev->td_urb_error = 1;
  994. wake_up_interruptible(&tport->tp_write_wait);
  995. }
  996. if (status == -EPIPE)
  997. goto exit;
  998. if (status) {
  999. dev_err(dev, "%s - stopping read!\n", __func__);
  1000. return;
  1001. }
  1002. tty = tty_port_tty_get(&port->port);
  1003. if (tty) {
  1004. if (urb->actual_length) {
  1005. usb_serial_debug_data(debug, dev, __func__,
  1006. urb->actual_length, urb->transfer_buffer);
  1007. if (!tport->tp_is_open)
  1008. dbg("%s - port closed, dropping data",
  1009. __func__);
  1010. else
  1011. ti_recv(&urb->dev->dev, tty,
  1012. urb->transfer_buffer,
  1013. urb->actual_length);
  1014. spin_lock(&tport->tp_lock);
  1015. tport->tp_icount.rx += urb->actual_length;
  1016. spin_unlock(&tport->tp_lock);
  1017. }
  1018. tty_kref_put(tty);
  1019. }
  1020. exit:
  1021. /* continue to read unless stopping */
  1022. spin_lock(&tport->tp_lock);
  1023. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1024. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1025. else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
  1026. tport->tp_read_urb_state = TI_READ_URB_STOPPED;
  1027. spin_unlock(&tport->tp_lock);
  1028. if (retval)
  1029. dev_err(dev, "%s - resubmit read urb failed, %d\n",
  1030. __func__, retval);
  1031. }
  1032. static void ti_bulk_out_callback(struct urb *urb)
  1033. {
  1034. struct ti_port *tport = urb->context;
  1035. struct usb_serial_port *port = tport->tp_port;
  1036. struct device *dev = &urb->dev->dev;
  1037. int status = urb->status;
  1038. dbg("%s - port %d", __func__, port->number);
  1039. tport->tp_write_urb_in_use = 0;
  1040. switch (status) {
  1041. case 0:
  1042. break;
  1043. case -ECONNRESET:
  1044. case -ENOENT:
  1045. case -ESHUTDOWN:
  1046. dbg("%s - urb shutting down, %d", __func__, status);
  1047. tport->tp_tdev->td_urb_error = 1;
  1048. wake_up_interruptible(&tport->tp_write_wait);
  1049. return;
  1050. default:
  1051. dev_err(dev, "%s - nonzero urb status, %d\n",
  1052. __func__, status);
  1053. tport->tp_tdev->td_urb_error = 1;
  1054. wake_up_interruptible(&tport->tp_write_wait);
  1055. }
  1056. /* send any buffered data */
  1057. ti_send(tport);
  1058. }
  1059. static void ti_recv(struct device *dev, struct tty_struct *tty,
  1060. unsigned char *data, int length)
  1061. {
  1062. int cnt;
  1063. do {
  1064. cnt = tty_insert_flip_string(tty, data, length);
  1065. if (cnt < length) {
  1066. dev_err(dev, "%s - dropping data, %d bytes lost\n",
  1067. __func__, length - cnt);
  1068. if (cnt == 0)
  1069. break;
  1070. }
  1071. tty_flip_buffer_push(tty);
  1072. data += cnt;
  1073. length -= cnt;
  1074. } while (length > 0);
  1075. }
  1076. static void ti_send(struct ti_port *tport)
  1077. {
  1078. int count, result;
  1079. struct usb_serial_port *port = tport->tp_port;
  1080. struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
  1081. unsigned long flags;
  1082. dbg("%s - port %d", __func__, port->number);
  1083. spin_lock_irqsave(&tport->tp_lock, flags);
  1084. if (tport->tp_write_urb_in_use)
  1085. goto unlock;
  1086. count = kfifo_out(&tport->write_fifo,
  1087. port->write_urb->transfer_buffer,
  1088. port->bulk_out_size);
  1089. if (count == 0)
  1090. goto unlock;
  1091. tport->tp_write_urb_in_use = 1;
  1092. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1093. usb_serial_debug_data(debug, &port->dev, __func__, count,
  1094. port->write_urb->transfer_buffer);
  1095. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  1096. usb_sndbulkpipe(port->serial->dev,
  1097. port->bulk_out_endpointAddress),
  1098. port->write_urb->transfer_buffer, count,
  1099. ti_bulk_out_callback, tport);
  1100. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1101. if (result) {
  1102. dev_err(&port->dev, "%s - submit write urb failed, %d\n",
  1103. __func__, result);
  1104. tport->tp_write_urb_in_use = 0;
  1105. /* TODO: reschedule ti_send */
  1106. } else {
  1107. spin_lock_irqsave(&tport->tp_lock, flags);
  1108. tport->tp_icount.tx += count;
  1109. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1110. }
  1111. /* more room in the buffer for new writes, wakeup */
  1112. if (tty)
  1113. tty_wakeup(tty);
  1114. tty_kref_put(tty);
  1115. wake_up_interruptible(&tport->tp_write_wait);
  1116. return;
  1117. unlock:
  1118. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1119. tty_kref_put(tty);
  1120. return;
  1121. }
  1122. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
  1123. {
  1124. unsigned long flags;
  1125. int status;
  1126. status = ti_write_byte(tport->tp_tdev,
  1127. tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
  1128. TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
  1129. spin_lock_irqsave(&tport->tp_lock, flags);
  1130. if (!status)
  1131. tport->tp_shadow_mcr = mcr;
  1132. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1133. return status;
  1134. }
  1135. static int ti_get_lsr(struct ti_port *tport)
  1136. {
  1137. int size, status;
  1138. struct ti_device *tdev = tport->tp_tdev;
  1139. struct usb_serial_port *port = tport->tp_port;
  1140. int port_number = port->number - port->serial->minor;
  1141. struct ti_port_status *data;
  1142. dbg("%s - port %d", __func__, port->number);
  1143. size = sizeof(struct ti_port_status);
  1144. data = kmalloc(size, GFP_KERNEL);
  1145. if (!data) {
  1146. dev_err(&port->dev, "%s - out of memory\n", __func__);
  1147. return -ENOMEM;
  1148. }
  1149. status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
  1150. (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
  1151. if (status) {
  1152. dev_err(&port->dev,
  1153. "%s - get port status command failed, %d\n",
  1154. __func__, status);
  1155. goto free_data;
  1156. }
  1157. dbg("%s - lsr 0x%02X", __func__, data->bLSR);
  1158. tport->tp_lsr = data->bLSR;
  1159. free_data:
  1160. kfree(data);
  1161. return status;
  1162. }
  1163. static int ti_get_serial_info(struct ti_port *tport,
  1164. struct serial_struct __user *ret_arg)
  1165. {
  1166. struct usb_serial_port *port = tport->tp_port;
  1167. struct serial_struct ret_serial;
  1168. if (!ret_arg)
  1169. return -EFAULT;
  1170. memset(&ret_serial, 0, sizeof(ret_serial));
  1171. ret_serial.type = PORT_16550A;
  1172. ret_serial.line = port->serial->minor;
  1173. ret_serial.port = port->number - port->serial->minor;
  1174. ret_serial.flags = tport->tp_flags;
  1175. ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
  1176. ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
  1177. ret_serial.closing_wait = tport->tp_closing_wait;
  1178. if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
  1179. return -EFAULT;
  1180. return 0;
  1181. }
  1182. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  1183. struct serial_struct __user *new_arg)
  1184. {
  1185. struct serial_struct new_serial;
  1186. if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
  1187. return -EFAULT;
  1188. tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
  1189. tport->tp_closing_wait = new_serial.closing_wait;
  1190. return 0;
  1191. }
  1192. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
  1193. {
  1194. struct async_icount *icount;
  1195. struct tty_struct *tty;
  1196. unsigned long flags;
  1197. dbg("%s - msr 0x%02X", __func__, msr);
  1198. if (msr & TI_MSR_DELTA_MASK) {
  1199. spin_lock_irqsave(&tport->tp_lock, flags);
  1200. icount = &tport->tp_icount;
  1201. if (msr & TI_MSR_DELTA_CTS)
  1202. icount->cts++;
  1203. if (msr & TI_MSR_DELTA_DSR)
  1204. icount->dsr++;
  1205. if (msr & TI_MSR_DELTA_CD)
  1206. icount->dcd++;
  1207. if (msr & TI_MSR_DELTA_RI)
  1208. icount->rng++;
  1209. wake_up_interruptible(&tport->tp_msr_wait);
  1210. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1211. }
  1212. tport->tp_msr = msr & TI_MSR_MASK;
  1213. /* handle CTS flow control */
  1214. tty = tty_port_tty_get(&tport->tp_port->port);
  1215. if (tty && C_CRTSCTS(tty)) {
  1216. if (msr & TI_MSR_CTS) {
  1217. tty->hw_stopped = 0;
  1218. tty_wakeup(tty);
  1219. } else {
  1220. tty->hw_stopped = 1;
  1221. }
  1222. }
  1223. tty_kref_put(tty);
  1224. }
  1225. static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
  1226. {
  1227. struct ti_device *tdev = tport->tp_tdev;
  1228. struct usb_serial_port *port = tport->tp_port;
  1229. wait_queue_t wait;
  1230. dbg("%s - port %d", __func__, port->number);
  1231. spin_lock_irq(&tport->tp_lock);
  1232. /* wait for data to drain from the buffer */
  1233. tdev->td_urb_error = 0;
  1234. init_waitqueue_entry(&wait, current);
  1235. add_wait_queue(&tport->tp_write_wait, &wait);
  1236. for (;;) {
  1237. set_current_state(TASK_INTERRUPTIBLE);
  1238. if (kfifo_len(&tport->write_fifo) == 0
  1239. || timeout == 0 || signal_pending(current)
  1240. || tdev->td_urb_error
  1241. || port->serial->disconnected) /* disconnect */
  1242. break;
  1243. spin_unlock_irq(&tport->tp_lock);
  1244. timeout = schedule_timeout(timeout);
  1245. spin_lock_irq(&tport->tp_lock);
  1246. }
  1247. set_current_state(TASK_RUNNING);
  1248. remove_wait_queue(&tport->tp_write_wait, &wait);
  1249. /* flush any remaining data in the buffer */
  1250. if (flush)
  1251. kfifo_reset_out(&tport->write_fifo);
  1252. spin_unlock_irq(&tport->tp_lock);
  1253. mutex_lock(&port->serial->disc_mutex);
  1254. /* wait for data to drain from the device */
  1255. /* wait for empty tx register, plus 20 ms */
  1256. timeout += jiffies;
  1257. tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
  1258. while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
  1259. && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
  1260. && !port->serial->disconnected) {
  1261. if (ti_get_lsr(tport))
  1262. break;
  1263. mutex_unlock(&port->serial->disc_mutex);
  1264. msleep_interruptible(20);
  1265. mutex_lock(&port->serial->disc_mutex);
  1266. }
  1267. mutex_unlock(&port->serial->disc_mutex);
  1268. }
  1269. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
  1270. {
  1271. unsigned long flags;
  1272. spin_lock_irqsave(&tport->tp_lock, flags);
  1273. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1274. tport->tp_read_urb_state = TI_READ_URB_STOPPING;
  1275. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1276. }
  1277. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
  1278. {
  1279. struct urb *urb;
  1280. int status = 0;
  1281. unsigned long flags;
  1282. spin_lock_irqsave(&tport->tp_lock, flags);
  1283. if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
  1284. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1285. urb = tport->tp_port->read_urb;
  1286. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1287. urb->context = tport;
  1288. status = usb_submit_urb(urb, GFP_KERNEL);
  1289. } else {
  1290. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1291. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1292. }
  1293. return status;
  1294. }
  1295. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  1296. __u16 moduleid, __u16 value, __u8 *data, int size)
  1297. {
  1298. int status;
  1299. status = usb_control_msg(tdev->td_serial->dev,
  1300. usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
  1301. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  1302. value, moduleid, data, size, 1000);
  1303. if (status == size)
  1304. status = 0;
  1305. if (status > 0)
  1306. status = -ECOMM;
  1307. return status;
  1308. }
  1309. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  1310. __u16 moduleid, __u16 value, __u8 *data, int size)
  1311. {
  1312. int status;
  1313. status = usb_control_msg(tdev->td_serial->dev,
  1314. usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
  1315. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  1316. value, moduleid, data, size, 1000);
  1317. if (status == size)
  1318. status = 0;
  1319. if (status > 0)
  1320. status = -ECOMM;
  1321. return status;
  1322. }
  1323. static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
  1324. __u8 mask, __u8 byte)
  1325. {
  1326. int status;
  1327. unsigned int size;
  1328. struct ti_write_data_bytes *data;
  1329. struct device *dev = &tdev->td_serial->dev->dev;
  1330. dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
  1331. __func__, addr, mask, byte);
  1332. size = sizeof(struct ti_write_data_bytes) + 2;
  1333. data = kmalloc(size, GFP_KERNEL);
  1334. if (!data) {
  1335. dev_err(dev, "%s - out of memory\n", __func__);
  1336. return -ENOMEM;
  1337. }
  1338. data->bAddrType = TI_RW_DATA_ADDR_XDATA;
  1339. data->bDataType = TI_RW_DATA_BYTE;
  1340. data->bDataCounter = 1;
  1341. data->wBaseAddrHi = cpu_to_be16(addr>>16);
  1342. data->wBaseAddrLo = cpu_to_be16(addr);
  1343. data->bData[0] = mask;
  1344. data->bData[1] = byte;
  1345. status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
  1346. (__u8 *)data, size);
  1347. if (status < 0)
  1348. dev_err(dev, "%s - failed, %d\n", __func__, status);
  1349. kfree(data);
  1350. return status;
  1351. }
  1352. static int ti_do_download(struct usb_device *dev, int pipe,
  1353. u8 *buffer, int size)
  1354. {
  1355. int pos;
  1356. u8 cs = 0;
  1357. int done;
  1358. struct ti_firmware_header *header;
  1359. int status = 0;
  1360. int len;
  1361. for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
  1362. cs = (__u8)(cs + buffer[pos]);
  1363. header = (struct ti_firmware_header *)buffer;
  1364. header->wLength = cpu_to_le16((__u16)(size
  1365. - sizeof(struct ti_firmware_header)));
  1366. header->bCheckSum = cs;
  1367. dbg("%s - downloading firmware", __func__);
  1368. for (pos = 0; pos < size; pos += done) {
  1369. len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
  1370. status = usb_bulk_msg(dev, pipe, buffer + pos, len,
  1371. &done, 1000);
  1372. if (status)
  1373. break;
  1374. }
  1375. return status;
  1376. }
  1377. static int ti_download_firmware(struct ti_device *tdev)
  1378. {
  1379. int status;
  1380. int buffer_size;
  1381. __u8 *buffer;
  1382. struct usb_device *dev = tdev->td_serial->dev;
  1383. unsigned int pipe = usb_sndbulkpipe(dev,
  1384. tdev->td_serial->port[0]->bulk_out_endpointAddress);
  1385. const struct firmware *fw_p;
  1386. char buf[32];
  1387. dbg("%s\n", __func__);
  1388. /* try ID specific firmware first, then try generic firmware */
  1389. sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
  1390. dev->descriptor.idProduct);
  1391. if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
  1392. buf[0] = '\0';
  1393. if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
  1394. switch (dev->descriptor.idProduct) {
  1395. case MTS_CDMA_PRODUCT_ID:
  1396. strcpy(buf, "mts_cdma.fw");
  1397. break;
  1398. case MTS_GSM_PRODUCT_ID:
  1399. strcpy(buf, "mts_gsm.fw");
  1400. break;
  1401. case MTS_EDGE_PRODUCT_ID:
  1402. strcpy(buf, "mts_edge.fw");
  1403. break;
  1404. case MTS_MT9234MU_PRODUCT_ID:
  1405. strcpy(buf, "mts_mt9234mu.fw");
  1406. break;
  1407. case MTS_MT9234ZBA_PRODUCT_ID:
  1408. strcpy(buf, "mts_mt9234zba.fw");
  1409. break;
  1410. case MTS_MT9234ZBAOLD_PRODUCT_ID:
  1411. strcpy(buf, "mts_mt9234zba.fw");
  1412. break; }
  1413. }
  1414. if (buf[0] == '\0') {
  1415. if (tdev->td_is_3410)
  1416. strcpy(buf, "ti_3410.fw");
  1417. else
  1418. strcpy(buf, "ti_5052.fw");
  1419. }
  1420. status = request_firmware(&fw_p, buf, &dev->dev);
  1421. }
  1422. if (status) {
  1423. dev_err(&dev->dev, "%s - firmware not found\n", __func__);
  1424. return -ENOENT;
  1425. }
  1426. if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
  1427. dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
  1428. release_firmware(fw_p);
  1429. return -ENOENT;
  1430. }
  1431. buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
  1432. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1433. if (buffer) {
  1434. memcpy(buffer, fw_p->data, fw_p->size);
  1435. memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
  1436. status = ti_do_download(dev, pipe, buffer, fw_p->size);
  1437. kfree(buffer);
  1438. } else {
  1439. dbg("%s ENOMEM\n", __func__);
  1440. status = -ENOMEM;
  1441. }
  1442. release_firmware(fw_p);
  1443. if (status) {
  1444. dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
  1445. __func__, status);
  1446. return status;
  1447. }
  1448. dbg("%s - download successful", __func__);
  1449. return 0;
  1450. }