ti_usb_3410_5052.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. // SPDX-License-Identifier: GPL-2.0+
  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. * For questions or problems with this driver, contact Texas Instruments
  12. * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
  13. * Peter Berger <pberger@brimson.com>.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/firmware.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_driver.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/module.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/serial.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/mutex.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/usb.h>
  30. #include <linux/usb/serial.h>
  31. /* Configuration ids */
  32. #define TI_BOOT_CONFIG 1
  33. #define TI_ACTIVE_CONFIG 2
  34. /* Vendor and product ids */
  35. #define TI_VENDOR_ID 0x0451
  36. #define IBM_VENDOR_ID 0x04b3
  37. #define STARTECH_VENDOR_ID 0x14b0
  38. #define TI_3410_PRODUCT_ID 0x3410
  39. #define IBM_4543_PRODUCT_ID 0x4543
  40. #define IBM_454B_PRODUCT_ID 0x454b
  41. #define IBM_454C_PRODUCT_ID 0x454c
  42. #define TI_3410_EZ430_ID 0xF430 /* TI ez430 development tool */
  43. #define TI_5052_BOOT_PRODUCT_ID 0x5052 /* no EEPROM, no firmware */
  44. #define TI_5152_BOOT_PRODUCT_ID 0x5152 /* no EEPROM, no firmware */
  45. #define TI_5052_EEPROM_PRODUCT_ID 0x505A /* EEPROM, no firmware */
  46. #define TI_5052_FIRMWARE_PRODUCT_ID 0x505F /* firmware is running */
  47. #define FRI2_PRODUCT_ID 0x5053 /* Fish River Island II */
  48. /* Multi-Tech vendor and product ids */
  49. #define MTS_VENDOR_ID 0x06E0
  50. #define MTS_GSM_NO_FW_PRODUCT_ID 0xF108
  51. #define MTS_CDMA_NO_FW_PRODUCT_ID 0xF109
  52. #define MTS_CDMA_PRODUCT_ID 0xF110
  53. #define MTS_GSM_PRODUCT_ID 0xF111
  54. #define MTS_EDGE_PRODUCT_ID 0xF112
  55. #define MTS_MT9234MU_PRODUCT_ID 0xF114
  56. #define MTS_MT9234ZBA_PRODUCT_ID 0xF115
  57. #define MTS_MT9234ZBAOLD_PRODUCT_ID 0x0319
  58. /* Abbott Diabetics vendor and product ids */
  59. #define ABBOTT_VENDOR_ID 0x1a61
  60. #define ABBOTT_STEREO_PLUG_ID 0x3410
  61. #define ABBOTT_PRODUCT_ID ABBOTT_STEREO_PLUG_ID
  62. #define ABBOTT_STRIP_PORT_ID 0x3420
  63. /* Honeywell vendor and product IDs */
  64. #define HONEYWELL_VENDOR_ID 0x10ac
  65. #define HONEYWELL_HGI80_PRODUCT_ID 0x0102 /* Honeywell HGI80 */
  66. /* Moxa UPORT 11x0 vendor and product IDs */
  67. #define MXU1_VENDOR_ID 0x110a
  68. #define MXU1_1110_PRODUCT_ID 0x1110
  69. #define MXU1_1130_PRODUCT_ID 0x1130
  70. #define MXU1_1150_PRODUCT_ID 0x1150
  71. #define MXU1_1151_PRODUCT_ID 0x1151
  72. #define MXU1_1131_PRODUCT_ID 0x1131
  73. /* Commands */
  74. #define TI_GET_VERSION 0x01
  75. #define TI_GET_PORT_STATUS 0x02
  76. #define TI_GET_PORT_DEV_INFO 0x03
  77. #define TI_GET_CONFIG 0x04
  78. #define TI_SET_CONFIG 0x05
  79. #define TI_OPEN_PORT 0x06
  80. #define TI_CLOSE_PORT 0x07
  81. #define TI_START_PORT 0x08
  82. #define TI_STOP_PORT 0x09
  83. #define TI_TEST_PORT 0x0A
  84. #define TI_PURGE_PORT 0x0B
  85. #define TI_RESET_EXT_DEVICE 0x0C
  86. #define TI_WRITE_DATA 0x80
  87. #define TI_READ_DATA 0x81
  88. #define TI_REQ_TYPE_CLASS 0x82
  89. /* Module identifiers */
  90. #define TI_I2C_PORT 0x01
  91. #define TI_IEEE1284_PORT 0x02
  92. #define TI_UART1_PORT 0x03
  93. #define TI_UART2_PORT 0x04
  94. #define TI_RAM_PORT 0x05
  95. /* Modem status */
  96. #define TI_MSR_DELTA_CTS 0x01
  97. #define TI_MSR_DELTA_DSR 0x02
  98. #define TI_MSR_DELTA_RI 0x04
  99. #define TI_MSR_DELTA_CD 0x08
  100. #define TI_MSR_CTS 0x10
  101. #define TI_MSR_DSR 0x20
  102. #define TI_MSR_RI 0x40
  103. #define TI_MSR_CD 0x80
  104. #define TI_MSR_DELTA_MASK 0x0F
  105. #define TI_MSR_MASK 0xF0
  106. /* Line status */
  107. #define TI_LSR_OVERRUN_ERROR 0x01
  108. #define TI_LSR_PARITY_ERROR 0x02
  109. #define TI_LSR_FRAMING_ERROR 0x04
  110. #define TI_LSR_BREAK 0x08
  111. #define TI_LSR_ERROR 0x0F
  112. #define TI_LSR_RX_FULL 0x10
  113. #define TI_LSR_TX_EMPTY 0x20
  114. /* Line control */
  115. #define TI_LCR_BREAK 0x40
  116. /* Modem control */
  117. #define TI_MCR_LOOP 0x04
  118. #define TI_MCR_DTR 0x10
  119. #define TI_MCR_RTS 0x20
  120. /* Mask settings */
  121. #define TI_UART_ENABLE_RTS_IN 0x0001
  122. #define TI_UART_DISABLE_RTS 0x0002
  123. #define TI_UART_ENABLE_PARITY_CHECKING 0x0008
  124. #define TI_UART_ENABLE_DSR_OUT 0x0010
  125. #define TI_UART_ENABLE_CTS_OUT 0x0020
  126. #define TI_UART_ENABLE_X_OUT 0x0040
  127. #define TI_UART_ENABLE_XA_OUT 0x0080
  128. #define TI_UART_ENABLE_X_IN 0x0100
  129. #define TI_UART_ENABLE_DTR_IN 0x0800
  130. #define TI_UART_DISABLE_DTR 0x1000
  131. #define TI_UART_ENABLE_MS_INTS 0x2000
  132. #define TI_UART_ENABLE_AUTO_START_DMA 0x4000
  133. /* Parity */
  134. #define TI_UART_NO_PARITY 0x00
  135. #define TI_UART_ODD_PARITY 0x01
  136. #define TI_UART_EVEN_PARITY 0x02
  137. #define TI_UART_MARK_PARITY 0x03
  138. #define TI_UART_SPACE_PARITY 0x04
  139. /* Stop bits */
  140. #define TI_UART_1_STOP_BITS 0x00
  141. #define TI_UART_1_5_STOP_BITS 0x01
  142. #define TI_UART_2_STOP_BITS 0x02
  143. /* Bits per character */
  144. #define TI_UART_5_DATA_BITS 0x00
  145. #define TI_UART_6_DATA_BITS 0x01
  146. #define TI_UART_7_DATA_BITS 0x02
  147. #define TI_UART_8_DATA_BITS 0x03
  148. /* 232/485 modes */
  149. #define TI_UART_232 0x00
  150. #define TI_UART_485_RECEIVER_DISABLED 0x01
  151. #define TI_UART_485_RECEIVER_ENABLED 0x02
  152. /* Pipe transfer mode and timeout */
  153. #define TI_PIPE_MODE_CONTINUOUS 0x01
  154. #define TI_PIPE_MODE_MASK 0x03
  155. #define TI_PIPE_TIMEOUT_MASK 0x7C
  156. #define TI_PIPE_TIMEOUT_ENABLE 0x80
  157. /* Config struct */
  158. struct ti_uart_config {
  159. __be16 wBaudRate;
  160. __be16 wFlags;
  161. u8 bDataBits;
  162. u8 bParity;
  163. u8 bStopBits;
  164. char cXon;
  165. char cXoff;
  166. u8 bUartMode;
  167. } __packed;
  168. /* Get port status */
  169. struct ti_port_status {
  170. u8 bCmdCode;
  171. u8 bModuleId;
  172. u8 bErrorCode;
  173. u8 bMSR;
  174. u8 bLSR;
  175. } __packed;
  176. /* Purge modes */
  177. #define TI_PURGE_OUTPUT 0x00
  178. #define TI_PURGE_INPUT 0x80
  179. /* Read/Write data */
  180. #define TI_RW_DATA_ADDR_SFR 0x10
  181. #define TI_RW_DATA_ADDR_IDATA 0x20
  182. #define TI_RW_DATA_ADDR_XDATA 0x30
  183. #define TI_RW_DATA_ADDR_CODE 0x40
  184. #define TI_RW_DATA_ADDR_GPIO 0x50
  185. #define TI_RW_DATA_ADDR_I2C 0x60
  186. #define TI_RW_DATA_ADDR_FLASH 0x70
  187. #define TI_RW_DATA_ADDR_DSP 0x80
  188. #define TI_RW_DATA_UNSPECIFIED 0x00
  189. #define TI_RW_DATA_BYTE 0x01
  190. #define TI_RW_DATA_WORD 0x02
  191. #define TI_RW_DATA_DOUBLE_WORD 0x04
  192. struct ti_write_data_bytes {
  193. u8 bAddrType;
  194. u8 bDataType;
  195. u8 bDataCounter;
  196. __be16 wBaseAddrHi;
  197. __be16 wBaseAddrLo;
  198. u8 bData[0];
  199. } __packed;
  200. struct ti_read_data_request {
  201. __u8 bAddrType;
  202. __u8 bDataType;
  203. __u8 bDataCounter;
  204. __be16 wBaseAddrHi;
  205. __be16 wBaseAddrLo;
  206. } __packed;
  207. struct ti_read_data_bytes {
  208. __u8 bCmdCode;
  209. __u8 bModuleId;
  210. __u8 bErrorCode;
  211. __u8 bData[0];
  212. } __packed;
  213. /* Interrupt struct */
  214. struct ti_interrupt {
  215. __u8 bICode;
  216. __u8 bIInfo;
  217. } __packed;
  218. /* Interrupt codes */
  219. #define TI_CODE_HARDWARE_ERROR 0xFF
  220. #define TI_CODE_DATA_ERROR 0x03
  221. #define TI_CODE_MODEM_STATUS 0x04
  222. /* Download firmware max packet size */
  223. #define TI_DOWNLOAD_MAX_PACKET_SIZE 64
  224. /* Firmware image header */
  225. struct ti_firmware_header {
  226. __le16 wLength;
  227. u8 bCheckSum;
  228. } __packed;
  229. /* UART addresses */
  230. #define TI_UART1_BASE_ADDR 0xFFA0 /* UART 1 base address */
  231. #define TI_UART2_BASE_ADDR 0xFFB0 /* UART 2 base address */
  232. #define TI_UART_OFFSET_LCR 0x0002 /* UART MCR register offset */
  233. #define TI_UART_OFFSET_MCR 0x0004 /* UART MCR register offset */
  234. #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
  235. #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
  236. #define TI_FIRMWARE_BUF_SIZE 16284
  237. #define TI_TRANSFER_TIMEOUT 2
  238. #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
  239. /* read urb states */
  240. #define TI_READ_URB_RUNNING 0
  241. #define TI_READ_URB_STOPPING 1
  242. #define TI_READ_URB_STOPPED 2
  243. #define TI_EXTRA_VID_PID_COUNT 5
  244. struct ti_port {
  245. int tp_is_open;
  246. u8 tp_msr;
  247. u8 tp_shadow_mcr;
  248. u8 tp_uart_mode; /* 232 or 485 modes */
  249. unsigned int tp_uart_base_addr;
  250. struct ti_device *tp_tdev;
  251. struct usb_serial_port *tp_port;
  252. spinlock_t tp_lock;
  253. int tp_read_urb_state;
  254. int tp_write_urb_in_use;
  255. };
  256. struct ti_device {
  257. struct mutex td_open_close_lock;
  258. int td_open_port_count;
  259. struct usb_serial *td_serial;
  260. int td_is_3410;
  261. bool td_rs485_only;
  262. };
  263. static int ti_startup(struct usb_serial *serial);
  264. static void ti_release(struct usb_serial *serial);
  265. static int ti_port_probe(struct usb_serial_port *port);
  266. static int ti_port_remove(struct usb_serial_port *port);
  267. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
  268. static void ti_close(struct usb_serial_port *port);
  269. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  270. const unsigned char *data, int count);
  271. static int ti_write_room(struct tty_struct *tty);
  272. static int ti_chars_in_buffer(struct tty_struct *tty);
  273. static bool ti_tx_empty(struct usb_serial_port *port);
  274. static void ti_throttle(struct tty_struct *tty);
  275. static void ti_unthrottle(struct tty_struct *tty);
  276. static void ti_set_termios(struct tty_struct *tty,
  277. struct usb_serial_port *port, struct ktermios *old_termios);
  278. static int ti_tiocmget(struct tty_struct *tty);
  279. static int ti_tiocmset(struct tty_struct *tty,
  280. unsigned int set, unsigned int clear);
  281. static void ti_break(struct tty_struct *tty, int break_state);
  282. static void ti_interrupt_callback(struct urb *urb);
  283. static void ti_bulk_in_callback(struct urb *urb);
  284. static void ti_bulk_out_callback(struct urb *urb);
  285. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  286. int length);
  287. static void ti_send(struct ti_port *tport);
  288. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
  289. static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
  290. static int ti_get_serial_info(struct tty_struct *tty,
  291. struct serial_struct *ss);
  292. static int ti_set_serial_info(struct tty_struct *tty,
  293. struct serial_struct *ss);
  294. static void ti_handle_new_msr(struct ti_port *tport, u8 msr);
  295. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
  296. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
  297. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  298. __u16 moduleid, __u16 value, __u8 *data, int size);
  299. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  300. __u16 moduleid, __u16 value, __u8 *data, int size);
  301. static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
  302. unsigned long addr, u8 mask, u8 byte);
  303. static int ti_download_firmware(struct ti_device *tdev);
  304. static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
  305. static const struct usb_device_id ti_id_table_3410[] = {
  306. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  307. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  308. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  309. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  310. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  311. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  312. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  313. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  314. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  315. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  316. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  317. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  318. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  319. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
  320. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
  321. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  322. { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
  323. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
  324. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
  325. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
  326. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
  327. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
  328. { USB_DEVICE(STARTECH_VENDOR_ID, TI_3410_PRODUCT_ID) },
  329. { } /* terminator */
  330. };
  331. static const struct usb_device_id ti_id_table_5052[] = {
  332. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  333. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  334. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  335. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  336. { }
  337. };
  338. static const struct usb_device_id ti_id_table_combined[] = {
  339. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  340. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  341. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  342. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  343. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  344. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  345. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  346. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  347. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  348. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  349. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  350. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  351. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  352. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  353. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  354. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  355. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  356. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
  357. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
  358. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  359. { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
  360. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
  361. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
  362. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
  363. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
  364. { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
  365. { USB_DEVICE(STARTECH_VENDOR_ID, TI_3410_PRODUCT_ID) },
  366. { } /* terminator */
  367. };
  368. static struct usb_serial_driver ti_1port_device = {
  369. .driver = {
  370. .owner = THIS_MODULE,
  371. .name = "ti_usb_3410_5052_1",
  372. },
  373. .description = "TI USB 3410 1 port adapter",
  374. .id_table = ti_id_table_3410,
  375. .num_ports = 1,
  376. .num_bulk_out = 1,
  377. .attach = ti_startup,
  378. .release = ti_release,
  379. .port_probe = ti_port_probe,
  380. .port_remove = ti_port_remove,
  381. .open = ti_open,
  382. .close = ti_close,
  383. .write = ti_write,
  384. .write_room = ti_write_room,
  385. .chars_in_buffer = ti_chars_in_buffer,
  386. .tx_empty = ti_tx_empty,
  387. .throttle = ti_throttle,
  388. .unthrottle = ti_unthrottle,
  389. .get_serial = ti_get_serial_info,
  390. .set_serial = ti_set_serial_info,
  391. .set_termios = ti_set_termios,
  392. .tiocmget = ti_tiocmget,
  393. .tiocmset = ti_tiocmset,
  394. .tiocmiwait = usb_serial_generic_tiocmiwait,
  395. .get_icount = usb_serial_generic_get_icount,
  396. .break_ctl = ti_break,
  397. .read_int_callback = ti_interrupt_callback,
  398. .read_bulk_callback = ti_bulk_in_callback,
  399. .write_bulk_callback = ti_bulk_out_callback,
  400. };
  401. static struct usb_serial_driver ti_2port_device = {
  402. .driver = {
  403. .owner = THIS_MODULE,
  404. .name = "ti_usb_3410_5052_2",
  405. },
  406. .description = "TI USB 5052 2 port adapter",
  407. .id_table = ti_id_table_5052,
  408. .num_ports = 2,
  409. .num_bulk_out = 1,
  410. .attach = ti_startup,
  411. .release = ti_release,
  412. .port_probe = ti_port_probe,
  413. .port_remove = ti_port_remove,
  414. .open = ti_open,
  415. .close = ti_close,
  416. .write = ti_write,
  417. .write_room = ti_write_room,
  418. .chars_in_buffer = ti_chars_in_buffer,
  419. .tx_empty = ti_tx_empty,
  420. .throttle = ti_throttle,
  421. .unthrottle = ti_unthrottle,
  422. .get_serial = ti_get_serial_info,
  423. .set_serial = ti_set_serial_info,
  424. .set_termios = ti_set_termios,
  425. .tiocmget = ti_tiocmget,
  426. .tiocmset = ti_tiocmset,
  427. .tiocmiwait = usb_serial_generic_tiocmiwait,
  428. .get_icount = usb_serial_generic_get_icount,
  429. .break_ctl = ti_break,
  430. .read_int_callback = ti_interrupt_callback,
  431. .read_bulk_callback = ti_bulk_in_callback,
  432. .write_bulk_callback = ti_bulk_out_callback,
  433. };
  434. static struct usb_serial_driver * const serial_drivers[] = {
  435. &ti_1port_device, &ti_2port_device, NULL
  436. };
  437. MODULE_AUTHOR(TI_DRIVER_AUTHOR);
  438. MODULE_DESCRIPTION(TI_DRIVER_DESC);
  439. MODULE_LICENSE("GPL");
  440. MODULE_FIRMWARE("ti_3410.fw");
  441. MODULE_FIRMWARE("ti_5052.fw");
  442. MODULE_FIRMWARE("mts_cdma.fw");
  443. MODULE_FIRMWARE("mts_gsm.fw");
  444. MODULE_FIRMWARE("mts_edge.fw");
  445. MODULE_FIRMWARE("mts_mt9234mu.fw");
  446. MODULE_FIRMWARE("mts_mt9234zba.fw");
  447. MODULE_FIRMWARE("moxa/moxa-1110.fw");
  448. MODULE_FIRMWARE("moxa/moxa-1130.fw");
  449. MODULE_FIRMWARE("moxa/moxa-1131.fw");
  450. MODULE_FIRMWARE("moxa/moxa-1150.fw");
  451. MODULE_FIRMWARE("moxa/moxa-1151.fw");
  452. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  453. MODULE_PARM_DESC(closing_wait,
  454. "Maximum wait for data to drain in close, in .01 secs, default is 4000");
  455. MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
  456. module_usb_serial_driver(serial_drivers, ti_id_table_combined);
  457. static int ti_startup(struct usb_serial *serial)
  458. {
  459. struct ti_device *tdev;
  460. struct usb_device *dev = serial->dev;
  461. struct usb_host_interface *cur_altsetting;
  462. int num_endpoints;
  463. u16 vid, pid;
  464. int status;
  465. dev_dbg(&dev->dev,
  466. "%s - product 0x%4X, num configurations %d, configuration value %d\n",
  467. __func__, le16_to_cpu(dev->descriptor.idProduct),
  468. dev->descriptor.bNumConfigurations,
  469. dev->actconfig->desc.bConfigurationValue);
  470. tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
  471. if (!tdev)
  472. return -ENOMEM;
  473. mutex_init(&tdev->td_open_close_lock);
  474. tdev->td_serial = serial;
  475. usb_set_serial_data(serial, tdev);
  476. /* determine device type */
  477. if (serial->type == &ti_1port_device)
  478. tdev->td_is_3410 = 1;
  479. dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
  480. tdev->td_is_3410 ? "3410" : "5052");
  481. vid = le16_to_cpu(dev->descriptor.idVendor);
  482. pid = le16_to_cpu(dev->descriptor.idProduct);
  483. if (vid == MXU1_VENDOR_ID) {
  484. switch (pid) {
  485. case MXU1_1130_PRODUCT_ID:
  486. case MXU1_1131_PRODUCT_ID:
  487. tdev->td_rs485_only = true;
  488. break;
  489. }
  490. }
  491. cur_altsetting = serial->interface->cur_altsetting;
  492. num_endpoints = cur_altsetting->desc.bNumEndpoints;
  493. /* if we have only 1 configuration and 1 endpoint, download firmware */
  494. if (dev->descriptor.bNumConfigurations == 1 && num_endpoints == 1) {
  495. status = ti_download_firmware(tdev);
  496. if (status != 0)
  497. goto free_tdev;
  498. /* 3410 must be reset, 5052 resets itself */
  499. if (tdev->td_is_3410) {
  500. msleep_interruptible(100);
  501. usb_reset_device(dev);
  502. }
  503. status = -ENODEV;
  504. goto free_tdev;
  505. }
  506. /* the second configuration must be set */
  507. if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
  508. status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
  509. status = status ? status : -ENODEV;
  510. goto free_tdev;
  511. }
  512. if (serial->num_bulk_in < serial->num_ports ||
  513. serial->num_bulk_out < serial->num_ports) {
  514. dev_err(&serial->interface->dev, "missing endpoints\n");
  515. status = -ENODEV;
  516. goto free_tdev;
  517. }
  518. return 0;
  519. free_tdev:
  520. kfree(tdev);
  521. usb_set_serial_data(serial, NULL);
  522. return status;
  523. }
  524. static void ti_release(struct usb_serial *serial)
  525. {
  526. struct ti_device *tdev = usb_get_serial_data(serial);
  527. kfree(tdev);
  528. }
  529. static int ti_port_probe(struct usb_serial_port *port)
  530. {
  531. struct ti_port *tport;
  532. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  533. if (!tport)
  534. return -ENOMEM;
  535. spin_lock_init(&tport->tp_lock);
  536. if (port == port->serial->port[0])
  537. tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
  538. else
  539. tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
  540. port->port.closing_wait = msecs_to_jiffies(10 * closing_wait);
  541. tport->tp_port = port;
  542. tport->tp_tdev = usb_get_serial_data(port->serial);
  543. if (tport->tp_tdev->td_rs485_only)
  544. tport->tp_uart_mode = TI_UART_485_RECEIVER_DISABLED;
  545. else
  546. tport->tp_uart_mode = TI_UART_232;
  547. usb_set_serial_port_data(port, tport);
  548. port->port.drain_delay = 3;
  549. return 0;
  550. }
  551. static int ti_port_remove(struct usb_serial_port *port)
  552. {
  553. struct ti_port *tport;
  554. tport = usb_get_serial_port_data(port);
  555. kfree(tport);
  556. return 0;
  557. }
  558. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
  559. {
  560. struct ti_port *tport = usb_get_serial_port_data(port);
  561. struct ti_device *tdev;
  562. struct usb_device *dev;
  563. struct urb *urb;
  564. int port_number;
  565. int status;
  566. u16 open_settings;
  567. open_settings = (TI_PIPE_MODE_CONTINUOUS |
  568. TI_PIPE_TIMEOUT_ENABLE |
  569. (TI_TRANSFER_TIMEOUT << 2));
  570. dev = port->serial->dev;
  571. tdev = tport->tp_tdev;
  572. /* only one open on any port on a device at a time */
  573. if (mutex_lock_interruptible(&tdev->td_open_close_lock))
  574. return -ERESTARTSYS;
  575. port_number = port->port_number;
  576. tport->tp_msr = 0;
  577. tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
  578. /* start interrupt urb the first time a port is opened on this device */
  579. if (tdev->td_open_port_count == 0) {
  580. dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
  581. urb = tdev->td_serial->port[0]->interrupt_in_urb;
  582. if (!urb) {
  583. dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
  584. status = -EINVAL;
  585. goto release_lock;
  586. }
  587. urb->context = tdev;
  588. status = usb_submit_urb(urb, GFP_KERNEL);
  589. if (status) {
  590. dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
  591. goto release_lock;
  592. }
  593. }
  594. if (tty)
  595. ti_set_termios(tty, port, &tty->termios);
  596. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  597. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  598. if (status) {
  599. dev_err(&port->dev, "%s - cannot send open command, %d\n",
  600. __func__, status);
  601. goto unlink_int_urb;
  602. }
  603. status = ti_command_out_sync(tdev, TI_START_PORT,
  604. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  605. if (status) {
  606. dev_err(&port->dev, "%s - cannot send start command, %d\n",
  607. __func__, status);
  608. goto unlink_int_urb;
  609. }
  610. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  611. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
  612. if (status) {
  613. dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
  614. __func__, status);
  615. goto unlink_int_urb;
  616. }
  617. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  618. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
  619. if (status) {
  620. dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
  621. __func__, status);
  622. goto unlink_int_urb;
  623. }
  624. /* reset the data toggle on the bulk endpoints to work around bug in
  625. * host controllers where things get out of sync some times */
  626. usb_clear_halt(dev, port->write_urb->pipe);
  627. usb_clear_halt(dev, port->read_urb->pipe);
  628. if (tty)
  629. ti_set_termios(tty, port, &tty->termios);
  630. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  631. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  632. if (status) {
  633. dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
  634. __func__, status);
  635. goto unlink_int_urb;
  636. }
  637. status = ti_command_out_sync(tdev, TI_START_PORT,
  638. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  639. if (status) {
  640. dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
  641. __func__, status);
  642. goto unlink_int_urb;
  643. }
  644. /* start read urb */
  645. urb = port->read_urb;
  646. if (!urb) {
  647. dev_err(&port->dev, "%s - no read urb\n", __func__);
  648. status = -EINVAL;
  649. goto unlink_int_urb;
  650. }
  651. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  652. urb->context = tport;
  653. status = usb_submit_urb(urb, GFP_KERNEL);
  654. if (status) {
  655. dev_err(&port->dev, "%s - submit read urb failed, %d\n",
  656. __func__, status);
  657. goto unlink_int_urb;
  658. }
  659. tport->tp_is_open = 1;
  660. ++tdev->td_open_port_count;
  661. goto release_lock;
  662. unlink_int_urb:
  663. if (tdev->td_open_port_count == 0)
  664. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  665. release_lock:
  666. mutex_unlock(&tdev->td_open_close_lock);
  667. return status;
  668. }
  669. static void ti_close(struct usb_serial_port *port)
  670. {
  671. struct ti_device *tdev;
  672. struct ti_port *tport;
  673. int port_number;
  674. int status;
  675. unsigned long flags;
  676. tdev = usb_get_serial_data(port->serial);
  677. tport = usb_get_serial_port_data(port);
  678. tport->tp_is_open = 0;
  679. usb_kill_urb(port->read_urb);
  680. usb_kill_urb(port->write_urb);
  681. tport->tp_write_urb_in_use = 0;
  682. spin_lock_irqsave(&tport->tp_lock, flags);
  683. kfifo_reset_out(&port->write_fifo);
  684. spin_unlock_irqrestore(&tport->tp_lock, flags);
  685. port_number = port->port_number;
  686. status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
  687. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  688. if (status)
  689. dev_err(&port->dev,
  690. "%s - cannot send close port command, %d\n"
  691. , __func__, status);
  692. mutex_lock(&tdev->td_open_close_lock);
  693. --tdev->td_open_port_count;
  694. if (tdev->td_open_port_count == 0) {
  695. /* last port is closed, shut down interrupt urb */
  696. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  697. }
  698. mutex_unlock(&tdev->td_open_close_lock);
  699. }
  700. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  701. const unsigned char *data, int count)
  702. {
  703. struct ti_port *tport = usb_get_serial_port_data(port);
  704. if (count == 0) {
  705. return 0;
  706. }
  707. if (!tport->tp_is_open)
  708. return -ENODEV;
  709. count = kfifo_in_locked(&port->write_fifo, data, count,
  710. &tport->tp_lock);
  711. ti_send(tport);
  712. return count;
  713. }
  714. static int ti_write_room(struct tty_struct *tty)
  715. {
  716. struct usb_serial_port *port = tty->driver_data;
  717. struct ti_port *tport = usb_get_serial_port_data(port);
  718. int room = 0;
  719. unsigned long flags;
  720. spin_lock_irqsave(&tport->tp_lock, flags);
  721. room = kfifo_avail(&port->write_fifo);
  722. spin_unlock_irqrestore(&tport->tp_lock, flags);
  723. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  724. return room;
  725. }
  726. static int ti_chars_in_buffer(struct tty_struct *tty)
  727. {
  728. struct usb_serial_port *port = tty->driver_data;
  729. struct ti_port *tport = usb_get_serial_port_data(port);
  730. int chars = 0;
  731. unsigned long flags;
  732. spin_lock_irqsave(&tport->tp_lock, flags);
  733. chars = kfifo_len(&port->write_fifo);
  734. spin_unlock_irqrestore(&tport->tp_lock, flags);
  735. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  736. return chars;
  737. }
  738. static bool ti_tx_empty(struct usb_serial_port *port)
  739. {
  740. struct ti_port *tport = usb_get_serial_port_data(port);
  741. int ret;
  742. u8 lsr;
  743. ret = ti_get_lsr(tport, &lsr);
  744. if (!ret && !(lsr & TI_LSR_TX_EMPTY))
  745. return false;
  746. return true;
  747. }
  748. static void ti_throttle(struct tty_struct *tty)
  749. {
  750. struct usb_serial_port *port = tty->driver_data;
  751. struct ti_port *tport = usb_get_serial_port_data(port);
  752. if (I_IXOFF(tty) || C_CRTSCTS(tty))
  753. ti_stop_read(tport, tty);
  754. }
  755. static void ti_unthrottle(struct tty_struct *tty)
  756. {
  757. struct usb_serial_port *port = tty->driver_data;
  758. struct ti_port *tport = usb_get_serial_port_data(port);
  759. int status;
  760. if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
  761. status = ti_restart_read(tport, tty);
  762. if (status)
  763. dev_err(&port->dev, "%s - cannot restart read, %d\n",
  764. __func__, status);
  765. }
  766. }
  767. static void ti_set_termios(struct tty_struct *tty,
  768. struct usb_serial_port *port, struct ktermios *old_termios)
  769. {
  770. struct ti_port *tport = usb_get_serial_port_data(port);
  771. struct ti_uart_config *config;
  772. int baud;
  773. int status;
  774. int port_number = port->port_number;
  775. unsigned int mcr;
  776. u16 wbaudrate;
  777. u16 wflags = 0;
  778. config = kmalloc(sizeof(*config), GFP_KERNEL);
  779. if (!config)
  780. return;
  781. /* these flags must be set */
  782. wflags |= TI_UART_ENABLE_MS_INTS;
  783. wflags |= TI_UART_ENABLE_AUTO_START_DMA;
  784. config->bUartMode = tport->tp_uart_mode;
  785. switch (C_CSIZE(tty)) {
  786. case CS5:
  787. config->bDataBits = TI_UART_5_DATA_BITS;
  788. break;
  789. case CS6:
  790. config->bDataBits = TI_UART_6_DATA_BITS;
  791. break;
  792. case CS7:
  793. config->bDataBits = TI_UART_7_DATA_BITS;
  794. break;
  795. default:
  796. case CS8:
  797. config->bDataBits = TI_UART_8_DATA_BITS;
  798. break;
  799. }
  800. /* CMSPAR isn't supported by this driver */
  801. tty->termios.c_cflag &= ~CMSPAR;
  802. if (C_PARENB(tty)) {
  803. if (C_PARODD(tty)) {
  804. wflags |= TI_UART_ENABLE_PARITY_CHECKING;
  805. config->bParity = TI_UART_ODD_PARITY;
  806. } else {
  807. wflags |= TI_UART_ENABLE_PARITY_CHECKING;
  808. config->bParity = TI_UART_EVEN_PARITY;
  809. }
  810. } else {
  811. wflags &= ~TI_UART_ENABLE_PARITY_CHECKING;
  812. config->bParity = TI_UART_NO_PARITY;
  813. }
  814. if (C_CSTOPB(tty))
  815. config->bStopBits = TI_UART_2_STOP_BITS;
  816. else
  817. config->bStopBits = TI_UART_1_STOP_BITS;
  818. if (C_CRTSCTS(tty)) {
  819. /* RTS flow control must be off to drop RTS for baud rate B0 */
  820. if ((C_BAUD(tty)) != B0)
  821. wflags |= TI_UART_ENABLE_RTS_IN;
  822. wflags |= TI_UART_ENABLE_CTS_OUT;
  823. } else {
  824. ti_restart_read(tport, tty);
  825. }
  826. if (I_IXOFF(tty) || I_IXON(tty)) {
  827. config->cXon = START_CHAR(tty);
  828. config->cXoff = STOP_CHAR(tty);
  829. if (I_IXOFF(tty))
  830. wflags |= TI_UART_ENABLE_X_IN;
  831. else
  832. ti_restart_read(tport, tty);
  833. if (I_IXON(tty))
  834. wflags |= TI_UART_ENABLE_X_OUT;
  835. }
  836. baud = tty_get_baud_rate(tty);
  837. if (!baud)
  838. baud = 9600;
  839. if (tport->tp_tdev->td_is_3410)
  840. wbaudrate = (923077 + baud/2) / baud;
  841. else
  842. wbaudrate = (461538 + baud/2) / baud;
  843. /* FIXME: Should calculate resulting baud here and report it back */
  844. if ((C_BAUD(tty)) != B0)
  845. tty_encode_baud_rate(tty, baud, baud);
  846. dev_dbg(&port->dev,
  847. "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
  848. __func__, baud, wbaudrate, wflags,
  849. config->bDataBits, config->bParity, config->bStopBits,
  850. config->cXon, config->cXoff, config->bUartMode);
  851. config->wBaudRate = cpu_to_be16(wbaudrate);
  852. config->wFlags = cpu_to_be16(wflags);
  853. status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
  854. (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
  855. sizeof(*config));
  856. if (status)
  857. dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
  858. __func__, port_number, status);
  859. /* SET_CONFIG asserts RTS and DTR, reset them correctly */
  860. mcr = tport->tp_shadow_mcr;
  861. /* if baud rate is B0, clear RTS and DTR */
  862. if (C_BAUD(tty) == B0)
  863. mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
  864. status = ti_set_mcr(tport, mcr);
  865. if (status)
  866. dev_err(&port->dev,
  867. "%s - cannot set modem control on port %d, %d\n",
  868. __func__, port_number, status);
  869. kfree(config);
  870. }
  871. static int ti_tiocmget(struct tty_struct *tty)
  872. {
  873. struct usb_serial_port *port = tty->driver_data;
  874. struct ti_port *tport = usb_get_serial_port_data(port);
  875. unsigned int result;
  876. unsigned int msr;
  877. unsigned int mcr;
  878. unsigned long flags;
  879. spin_lock_irqsave(&tport->tp_lock, flags);
  880. msr = tport->tp_msr;
  881. mcr = tport->tp_shadow_mcr;
  882. spin_unlock_irqrestore(&tport->tp_lock, flags);
  883. result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
  884. | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
  885. | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
  886. | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
  887. | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
  888. | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
  889. | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
  890. dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
  891. return result;
  892. }
  893. static int ti_tiocmset(struct tty_struct *tty,
  894. unsigned int set, unsigned int clear)
  895. {
  896. struct usb_serial_port *port = tty->driver_data;
  897. struct ti_port *tport = usb_get_serial_port_data(port);
  898. unsigned int mcr;
  899. unsigned long flags;
  900. spin_lock_irqsave(&tport->tp_lock, flags);
  901. mcr = tport->tp_shadow_mcr;
  902. if (set & TIOCM_RTS)
  903. mcr |= TI_MCR_RTS;
  904. if (set & TIOCM_DTR)
  905. mcr |= TI_MCR_DTR;
  906. if (set & TIOCM_LOOP)
  907. mcr |= TI_MCR_LOOP;
  908. if (clear & TIOCM_RTS)
  909. mcr &= ~TI_MCR_RTS;
  910. if (clear & TIOCM_DTR)
  911. mcr &= ~TI_MCR_DTR;
  912. if (clear & TIOCM_LOOP)
  913. mcr &= ~TI_MCR_LOOP;
  914. spin_unlock_irqrestore(&tport->tp_lock, flags);
  915. return ti_set_mcr(tport, mcr);
  916. }
  917. static void ti_break(struct tty_struct *tty, int break_state)
  918. {
  919. struct usb_serial_port *port = tty->driver_data;
  920. struct ti_port *tport = usb_get_serial_port_data(port);
  921. int status;
  922. dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
  923. status = ti_write_byte(port, tport->tp_tdev,
  924. tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
  925. TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
  926. if (status)
  927. dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
  928. }
  929. static int ti_get_port_from_code(unsigned char code)
  930. {
  931. return (code >> 6) & 0x01;
  932. }
  933. static int ti_get_func_from_code(unsigned char code)
  934. {
  935. return code & 0x0f;
  936. }
  937. static void ti_interrupt_callback(struct urb *urb)
  938. {
  939. struct ti_device *tdev = urb->context;
  940. struct usb_serial_port *port;
  941. struct usb_serial *serial = tdev->td_serial;
  942. struct ti_port *tport;
  943. struct device *dev = &urb->dev->dev;
  944. unsigned char *data = urb->transfer_buffer;
  945. int length = urb->actual_length;
  946. int port_number;
  947. int function;
  948. int status = urb->status;
  949. int retval;
  950. u8 msr;
  951. switch (status) {
  952. case 0:
  953. break;
  954. case -ECONNRESET:
  955. case -ENOENT:
  956. case -ESHUTDOWN:
  957. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  958. return;
  959. default:
  960. dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
  961. goto exit;
  962. }
  963. if (length != 2) {
  964. dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
  965. goto exit;
  966. }
  967. if (data[0] == TI_CODE_HARDWARE_ERROR) {
  968. dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
  969. goto exit;
  970. }
  971. port_number = ti_get_port_from_code(data[0]);
  972. function = ti_get_func_from_code(data[0]);
  973. dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
  974. __func__, port_number, function, data[1]);
  975. if (port_number >= serial->num_ports) {
  976. dev_err(dev, "%s - bad port number, %d\n",
  977. __func__, port_number);
  978. goto exit;
  979. }
  980. port = serial->port[port_number];
  981. tport = usb_get_serial_port_data(port);
  982. if (!tport)
  983. goto exit;
  984. switch (function) {
  985. case TI_CODE_DATA_ERROR:
  986. dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
  987. __func__, port_number, data[1]);
  988. break;
  989. case TI_CODE_MODEM_STATUS:
  990. msr = data[1];
  991. dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
  992. ti_handle_new_msr(tport, msr);
  993. break;
  994. default:
  995. dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
  996. __func__, data[1]);
  997. break;
  998. }
  999. exit:
  1000. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1001. if (retval)
  1002. dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
  1003. __func__, retval);
  1004. }
  1005. static void ti_bulk_in_callback(struct urb *urb)
  1006. {
  1007. struct ti_port *tport = urb->context;
  1008. struct usb_serial_port *port = tport->tp_port;
  1009. struct device *dev = &urb->dev->dev;
  1010. int status = urb->status;
  1011. unsigned long flags;
  1012. int retval = 0;
  1013. switch (status) {
  1014. case 0:
  1015. break;
  1016. case -ECONNRESET:
  1017. case -ENOENT:
  1018. case -ESHUTDOWN:
  1019. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  1020. return;
  1021. default:
  1022. dev_err(dev, "%s - nonzero urb status, %d\n",
  1023. __func__, status);
  1024. }
  1025. if (status == -EPIPE)
  1026. goto exit;
  1027. if (status) {
  1028. dev_err(dev, "%s - stopping read!\n", __func__);
  1029. return;
  1030. }
  1031. if (urb->actual_length) {
  1032. usb_serial_debug_data(dev, __func__, urb->actual_length,
  1033. urb->transfer_buffer);
  1034. if (!tport->tp_is_open)
  1035. dev_dbg(dev, "%s - port closed, dropping data\n",
  1036. __func__);
  1037. else
  1038. ti_recv(port, urb->transfer_buffer, urb->actual_length);
  1039. spin_lock_irqsave(&tport->tp_lock, flags);
  1040. port->icount.rx += urb->actual_length;
  1041. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1042. }
  1043. exit:
  1044. /* continue to read unless stopping */
  1045. spin_lock_irqsave(&tport->tp_lock, flags);
  1046. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1047. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1048. else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
  1049. tport->tp_read_urb_state = TI_READ_URB_STOPPED;
  1050. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1051. if (retval)
  1052. dev_err(dev, "%s - resubmit read urb failed, %d\n",
  1053. __func__, retval);
  1054. }
  1055. static void ti_bulk_out_callback(struct urb *urb)
  1056. {
  1057. struct ti_port *tport = urb->context;
  1058. struct usb_serial_port *port = tport->tp_port;
  1059. int status = urb->status;
  1060. tport->tp_write_urb_in_use = 0;
  1061. switch (status) {
  1062. case 0:
  1063. break;
  1064. case -ECONNRESET:
  1065. case -ENOENT:
  1066. case -ESHUTDOWN:
  1067. dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
  1068. return;
  1069. default:
  1070. dev_err_console(port, "%s - nonzero urb status, %d\n",
  1071. __func__, status);
  1072. }
  1073. /* send any buffered data */
  1074. ti_send(tport);
  1075. }
  1076. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  1077. int length)
  1078. {
  1079. int cnt;
  1080. do {
  1081. cnt = tty_insert_flip_string(&port->port, data, length);
  1082. if (cnt < length) {
  1083. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  1084. __func__, length - cnt);
  1085. if (cnt == 0)
  1086. break;
  1087. }
  1088. tty_flip_buffer_push(&port->port);
  1089. data += cnt;
  1090. length -= cnt;
  1091. } while (length > 0);
  1092. }
  1093. static void ti_send(struct ti_port *tport)
  1094. {
  1095. int count, result;
  1096. struct usb_serial_port *port = tport->tp_port;
  1097. unsigned long flags;
  1098. spin_lock_irqsave(&tport->tp_lock, flags);
  1099. if (tport->tp_write_urb_in_use)
  1100. goto unlock;
  1101. count = kfifo_out(&port->write_fifo,
  1102. port->write_urb->transfer_buffer,
  1103. port->bulk_out_size);
  1104. if (count == 0)
  1105. goto unlock;
  1106. tport->tp_write_urb_in_use = 1;
  1107. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1108. usb_serial_debug_data(&port->dev, __func__, count,
  1109. port->write_urb->transfer_buffer);
  1110. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  1111. usb_sndbulkpipe(port->serial->dev,
  1112. port->bulk_out_endpointAddress),
  1113. port->write_urb->transfer_buffer, count,
  1114. ti_bulk_out_callback, tport);
  1115. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1116. if (result) {
  1117. dev_err_console(port, "%s - submit write urb failed, %d\n",
  1118. __func__, result);
  1119. tport->tp_write_urb_in_use = 0;
  1120. /* TODO: reschedule ti_send */
  1121. } else {
  1122. spin_lock_irqsave(&tport->tp_lock, flags);
  1123. port->icount.tx += count;
  1124. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1125. }
  1126. /* more room in the buffer for new writes, wakeup */
  1127. tty_port_tty_wakeup(&port->port);
  1128. return;
  1129. unlock:
  1130. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1131. return;
  1132. }
  1133. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
  1134. {
  1135. unsigned long flags;
  1136. int status;
  1137. status = ti_write_byte(tport->tp_port, tport->tp_tdev,
  1138. tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
  1139. TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
  1140. spin_lock_irqsave(&tport->tp_lock, flags);
  1141. if (!status)
  1142. tport->tp_shadow_mcr = mcr;
  1143. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1144. return status;
  1145. }
  1146. static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
  1147. {
  1148. int size, status;
  1149. struct ti_device *tdev = tport->tp_tdev;
  1150. struct usb_serial_port *port = tport->tp_port;
  1151. int port_number = port->port_number;
  1152. struct ti_port_status *data;
  1153. size = sizeof(struct ti_port_status);
  1154. data = kmalloc(size, GFP_KERNEL);
  1155. if (!data)
  1156. return -ENOMEM;
  1157. status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
  1158. (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
  1159. if (status) {
  1160. dev_err(&port->dev,
  1161. "%s - get port status command failed, %d\n",
  1162. __func__, status);
  1163. goto free_data;
  1164. }
  1165. dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
  1166. *lsr = data->bLSR;
  1167. free_data:
  1168. kfree(data);
  1169. return status;
  1170. }
  1171. static int ti_get_serial_info(struct tty_struct *tty,
  1172. struct serial_struct *ss)
  1173. {
  1174. struct usb_serial_port *port = tty->driver_data;
  1175. struct ti_port *tport = usb_get_serial_port_data(port);
  1176. unsigned cwait;
  1177. cwait = port->port.closing_wait;
  1178. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1179. cwait = jiffies_to_msecs(cwait) / 10;
  1180. ss->type = PORT_16550A;
  1181. ss->line = port->minor;
  1182. ss->port = port->port_number;
  1183. ss->xmit_fifo_size = kfifo_size(&port->write_fifo);
  1184. ss->baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
  1185. ss->closing_wait = cwait;
  1186. return 0;
  1187. }
  1188. static int ti_set_serial_info(struct tty_struct *tty,
  1189. struct serial_struct *ss)
  1190. {
  1191. struct usb_serial_port *port = tty->driver_data;
  1192. struct tty_port *tport = &port->port;
  1193. unsigned cwait;
  1194. cwait = ss->closing_wait;
  1195. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1196. cwait = msecs_to_jiffies(10 * ss->closing_wait);
  1197. if (!capable(CAP_SYS_ADMIN)) {
  1198. if (cwait != tport->closing_wait)
  1199. return -EPERM;
  1200. }
  1201. tport->closing_wait = cwait;
  1202. return 0;
  1203. }
  1204. static void ti_handle_new_msr(struct ti_port *tport, u8 msr)
  1205. {
  1206. struct async_icount *icount;
  1207. struct tty_struct *tty;
  1208. unsigned long flags;
  1209. dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
  1210. if (msr & TI_MSR_DELTA_MASK) {
  1211. spin_lock_irqsave(&tport->tp_lock, flags);
  1212. icount = &tport->tp_port->icount;
  1213. if (msr & TI_MSR_DELTA_CTS)
  1214. icount->cts++;
  1215. if (msr & TI_MSR_DELTA_DSR)
  1216. icount->dsr++;
  1217. if (msr & TI_MSR_DELTA_CD)
  1218. icount->dcd++;
  1219. if (msr & TI_MSR_DELTA_RI)
  1220. icount->rng++;
  1221. wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
  1222. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1223. }
  1224. tport->tp_msr = msr & TI_MSR_MASK;
  1225. /* handle CTS flow control */
  1226. tty = tty_port_tty_get(&tport->tp_port->port);
  1227. if (tty && C_CRTSCTS(tty)) {
  1228. if (msr & TI_MSR_CTS)
  1229. tty_wakeup(tty);
  1230. }
  1231. tty_kref_put(tty);
  1232. }
  1233. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
  1234. {
  1235. unsigned long flags;
  1236. spin_lock_irqsave(&tport->tp_lock, flags);
  1237. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1238. tport->tp_read_urb_state = TI_READ_URB_STOPPING;
  1239. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1240. }
  1241. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
  1242. {
  1243. struct urb *urb;
  1244. int status = 0;
  1245. unsigned long flags;
  1246. spin_lock_irqsave(&tport->tp_lock, flags);
  1247. if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
  1248. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1249. urb = tport->tp_port->read_urb;
  1250. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1251. urb->context = tport;
  1252. status = usb_submit_urb(urb, GFP_KERNEL);
  1253. } else {
  1254. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1255. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1256. }
  1257. return status;
  1258. }
  1259. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  1260. __u16 moduleid, __u16 value, __u8 *data, int size)
  1261. {
  1262. int status;
  1263. status = usb_control_msg(tdev->td_serial->dev,
  1264. usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
  1265. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  1266. value, moduleid, data, size, 1000);
  1267. if (status < 0)
  1268. return status;
  1269. return 0;
  1270. }
  1271. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  1272. __u16 moduleid, __u16 value, __u8 *data, int size)
  1273. {
  1274. int status;
  1275. status = usb_control_msg(tdev->td_serial->dev,
  1276. usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
  1277. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  1278. value, moduleid, data, size, 1000);
  1279. if (status == size)
  1280. status = 0;
  1281. else if (status >= 0)
  1282. status = -ECOMM;
  1283. return status;
  1284. }
  1285. static int ti_write_byte(struct usb_serial_port *port,
  1286. struct ti_device *tdev, unsigned long addr,
  1287. u8 mask, u8 byte)
  1288. {
  1289. int status;
  1290. unsigned int size;
  1291. struct ti_write_data_bytes *data;
  1292. dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
  1293. addr, mask, byte);
  1294. size = sizeof(struct ti_write_data_bytes) + 2;
  1295. data = kmalloc(size, GFP_KERNEL);
  1296. if (!data)
  1297. return -ENOMEM;
  1298. data->bAddrType = TI_RW_DATA_ADDR_XDATA;
  1299. data->bDataType = TI_RW_DATA_BYTE;
  1300. data->bDataCounter = 1;
  1301. data->wBaseAddrHi = cpu_to_be16(addr>>16);
  1302. data->wBaseAddrLo = cpu_to_be16(addr);
  1303. data->bData[0] = mask;
  1304. data->bData[1] = byte;
  1305. status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
  1306. (__u8 *)data, size);
  1307. if (status < 0)
  1308. dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
  1309. kfree(data);
  1310. return status;
  1311. }
  1312. static int ti_do_download(struct usb_device *dev, int pipe,
  1313. u8 *buffer, int size)
  1314. {
  1315. int pos;
  1316. u8 cs = 0;
  1317. int done;
  1318. struct ti_firmware_header *header;
  1319. int status = 0;
  1320. int len;
  1321. for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
  1322. cs = (u8)(cs + buffer[pos]);
  1323. header = (struct ti_firmware_header *)buffer;
  1324. header->wLength = cpu_to_le16(size - sizeof(*header));
  1325. header->bCheckSum = cs;
  1326. dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
  1327. for (pos = 0; pos < size; pos += done) {
  1328. len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
  1329. status = usb_bulk_msg(dev, pipe, buffer + pos, len,
  1330. &done, 1000);
  1331. if (status)
  1332. break;
  1333. }
  1334. return status;
  1335. }
  1336. static int ti_download_firmware(struct ti_device *tdev)
  1337. {
  1338. int status;
  1339. int buffer_size;
  1340. u8 *buffer;
  1341. struct usb_device *dev = tdev->td_serial->dev;
  1342. unsigned int pipe = usb_sndbulkpipe(dev,
  1343. tdev->td_serial->port[0]->bulk_out_endpointAddress);
  1344. const struct firmware *fw_p;
  1345. char buf[32];
  1346. if (le16_to_cpu(dev->descriptor.idVendor) == MXU1_VENDOR_ID) {
  1347. snprintf(buf,
  1348. sizeof(buf),
  1349. "moxa/moxa-%04x.fw",
  1350. le16_to_cpu(dev->descriptor.idProduct));
  1351. status = request_firmware(&fw_p, buf, &dev->dev);
  1352. goto check_firmware;
  1353. }
  1354. /* try ID specific firmware first, then try generic firmware */
  1355. sprintf(buf, "ti_usb-v%04x-p%04x.fw",
  1356. le16_to_cpu(dev->descriptor.idVendor),
  1357. le16_to_cpu(dev->descriptor.idProduct));
  1358. status = request_firmware(&fw_p, buf, &dev->dev);
  1359. if (status != 0) {
  1360. buf[0] = '\0';
  1361. if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
  1362. switch (le16_to_cpu(dev->descriptor.idProduct)) {
  1363. case MTS_CDMA_PRODUCT_ID:
  1364. strcpy(buf, "mts_cdma.fw");
  1365. break;
  1366. case MTS_GSM_PRODUCT_ID:
  1367. strcpy(buf, "mts_gsm.fw");
  1368. break;
  1369. case MTS_EDGE_PRODUCT_ID:
  1370. strcpy(buf, "mts_edge.fw");
  1371. break;
  1372. case MTS_MT9234MU_PRODUCT_ID:
  1373. strcpy(buf, "mts_mt9234mu.fw");
  1374. break;
  1375. case MTS_MT9234ZBA_PRODUCT_ID:
  1376. strcpy(buf, "mts_mt9234zba.fw");
  1377. break;
  1378. case MTS_MT9234ZBAOLD_PRODUCT_ID:
  1379. strcpy(buf, "mts_mt9234zba.fw");
  1380. break; }
  1381. }
  1382. if (buf[0] == '\0') {
  1383. if (tdev->td_is_3410)
  1384. strcpy(buf, "ti_3410.fw");
  1385. else
  1386. strcpy(buf, "ti_5052.fw");
  1387. }
  1388. status = request_firmware(&fw_p, buf, &dev->dev);
  1389. }
  1390. check_firmware:
  1391. if (status) {
  1392. dev_err(&dev->dev, "%s - firmware not found\n", __func__);
  1393. return -ENOENT;
  1394. }
  1395. if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
  1396. dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
  1397. release_firmware(fw_p);
  1398. return -ENOENT;
  1399. }
  1400. buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
  1401. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1402. if (buffer) {
  1403. memcpy(buffer, fw_p->data, fw_p->size);
  1404. memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
  1405. status = ti_do_download(dev, pipe, buffer, fw_p->size);
  1406. kfree(buffer);
  1407. } else {
  1408. status = -ENOMEM;
  1409. }
  1410. release_firmware(fw_p);
  1411. if (status) {
  1412. dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
  1413. __func__, status);
  1414. return status;
  1415. }
  1416. dev_dbg(&dev->dev, "%s - download successful\n", __func__);
  1417. return 0;
  1418. }