ti_usb_3410_5052.c 45 KB

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