ti_usb_3410_5052.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /* vi: ts=8 sw=8
  2. *
  3. * TI 3410/5052 USB Serial Driver
  4. *
  5. * Copyright (C) 2004 Texas Instruments
  6. *
  7. * This driver is based on the Linux io_ti driver, which is
  8. * Copyright (C) 2000-2002 Inside Out Networks
  9. * Copyright (C) 2001-2002 Greg Kroah-Hartman
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * For questions or problems with this driver, contact Texas Instruments
  17. * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
  18. * Peter Berger <pberger@brimson.com>.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/firmware.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_driver.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/module.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/serial.h>
  31. #include <linux/kfifo.h>
  32. #include <linux/mutex.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include "ti_usb_3410_5052.h"
  37. /* Defines */
  38. #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
  39. #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
  40. #define TI_FIRMWARE_BUF_SIZE 16284
  41. #define TI_TRANSFER_TIMEOUT 2
  42. #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
  43. /* supported setserial flags */
  44. #define TI_SET_SERIAL_FLAGS 0
  45. /* read urb states */
  46. #define TI_READ_URB_RUNNING 0
  47. #define TI_READ_URB_STOPPING 1
  48. #define TI_READ_URB_STOPPED 2
  49. #define TI_EXTRA_VID_PID_COUNT 5
  50. /* Structures */
  51. struct ti_port {
  52. int tp_is_open;
  53. __u8 tp_msr;
  54. __u8 tp_shadow_mcr;
  55. __u8 tp_uart_mode; /* 232 or 485 modes */
  56. unsigned int tp_uart_base_addr;
  57. int tp_flags;
  58. struct ti_device *tp_tdev;
  59. struct usb_serial_port *tp_port;
  60. spinlock_t tp_lock;
  61. int tp_read_urb_state;
  62. int tp_write_urb_in_use;
  63. };
  64. struct ti_device {
  65. struct mutex td_open_close_lock;
  66. int td_open_port_count;
  67. struct usb_serial *td_serial;
  68. int td_is_3410;
  69. int td_urb_error;
  70. };
  71. /* Function Declarations */
  72. static int ti_startup(struct usb_serial *serial);
  73. static void ti_release(struct usb_serial *serial);
  74. static int ti_port_probe(struct usb_serial_port *port);
  75. static int ti_port_remove(struct usb_serial_port *port);
  76. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
  77. static void ti_close(struct usb_serial_port *port);
  78. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  79. const unsigned char *data, int count);
  80. static int ti_write_room(struct tty_struct *tty);
  81. static int ti_chars_in_buffer(struct tty_struct *tty);
  82. static bool ti_tx_empty(struct usb_serial_port *port);
  83. static void ti_throttle(struct tty_struct *tty);
  84. static void ti_unthrottle(struct tty_struct *tty);
  85. static int ti_ioctl(struct tty_struct *tty,
  86. unsigned int cmd, unsigned long arg);
  87. static void ti_set_termios(struct tty_struct *tty,
  88. struct usb_serial_port *port, struct ktermios *old_termios);
  89. static int ti_tiocmget(struct tty_struct *tty);
  90. static int ti_tiocmset(struct tty_struct *tty,
  91. unsigned int set, unsigned int clear);
  92. static void ti_break(struct tty_struct *tty, int break_state);
  93. static void ti_interrupt_callback(struct urb *urb);
  94. static void ti_bulk_in_callback(struct urb *urb);
  95. static void ti_bulk_out_callback(struct urb *urb);
  96. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  97. int length);
  98. static void ti_send(struct ti_port *tport);
  99. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
  100. static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
  101. static int ti_get_serial_info(struct ti_port *tport,
  102. struct serial_struct __user *ret_arg);
  103. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  104. struct serial_struct __user *new_arg);
  105. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
  106. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
  107. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
  108. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  109. __u16 moduleid, __u16 value, __u8 *data, int size);
  110. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  111. __u16 moduleid, __u16 value, __u8 *data, int size);
  112. static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
  113. unsigned long addr, __u8 mask, __u8 byte);
  114. static int ti_download_firmware(struct ti_device *tdev);
  115. /* Data */
  116. /* module parameters */
  117. static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
  118. /* supported devices */
  119. static const struct usb_device_id ti_id_table_3410[] = {
  120. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  121. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  122. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  123. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  124. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  125. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  126. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  127. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  128. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  129. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  130. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  131. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  132. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  133. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
  134. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
  135. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  136. { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
  137. { } /* terminator */
  138. };
  139. static const struct usb_device_id ti_id_table_5052[] = {
  140. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  141. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  142. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  143. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  144. { } /* terminator */
  145. };
  146. static const struct usb_device_id ti_id_table_combined[] = {
  147. { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
  148. { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
  149. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
  150. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
  151. { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
  152. { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
  153. { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
  154. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
  155. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
  156. { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
  157. { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
  158. { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
  159. { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
  160. { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
  161. { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
  162. { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
  163. { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
  164. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
  165. { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
  166. { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
  167. { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
  168. { } /* terminator */
  169. };
  170. static struct usb_serial_driver ti_1port_device = {
  171. .driver = {
  172. .owner = THIS_MODULE,
  173. .name = "ti_usb_3410_5052_1",
  174. },
  175. .description = "TI USB 3410 1 port adapter",
  176. .id_table = ti_id_table_3410,
  177. .num_ports = 1,
  178. .attach = ti_startup,
  179. .release = ti_release,
  180. .port_probe = ti_port_probe,
  181. .port_remove = ti_port_remove,
  182. .open = ti_open,
  183. .close = ti_close,
  184. .write = ti_write,
  185. .write_room = ti_write_room,
  186. .chars_in_buffer = ti_chars_in_buffer,
  187. .tx_empty = ti_tx_empty,
  188. .throttle = ti_throttle,
  189. .unthrottle = ti_unthrottle,
  190. .ioctl = ti_ioctl,
  191. .set_termios = ti_set_termios,
  192. .tiocmget = ti_tiocmget,
  193. .tiocmset = ti_tiocmset,
  194. .tiocmiwait = usb_serial_generic_tiocmiwait,
  195. .get_icount = usb_serial_generic_get_icount,
  196. .break_ctl = ti_break,
  197. .read_int_callback = ti_interrupt_callback,
  198. .read_bulk_callback = ti_bulk_in_callback,
  199. .write_bulk_callback = ti_bulk_out_callback,
  200. };
  201. static struct usb_serial_driver ti_2port_device = {
  202. .driver = {
  203. .owner = THIS_MODULE,
  204. .name = "ti_usb_3410_5052_2",
  205. },
  206. .description = "TI USB 5052 2 port adapter",
  207. .id_table = ti_id_table_5052,
  208. .num_ports = 2,
  209. .attach = ti_startup,
  210. .release = ti_release,
  211. .port_probe = ti_port_probe,
  212. .port_remove = ti_port_remove,
  213. .open = ti_open,
  214. .close = ti_close,
  215. .write = ti_write,
  216. .write_room = ti_write_room,
  217. .chars_in_buffer = ti_chars_in_buffer,
  218. .tx_empty = ti_tx_empty,
  219. .throttle = ti_throttle,
  220. .unthrottle = ti_unthrottle,
  221. .ioctl = ti_ioctl,
  222. .set_termios = ti_set_termios,
  223. .tiocmget = ti_tiocmget,
  224. .tiocmset = ti_tiocmset,
  225. .tiocmiwait = usb_serial_generic_tiocmiwait,
  226. .get_icount = usb_serial_generic_get_icount,
  227. .break_ctl = ti_break,
  228. .read_int_callback = ti_interrupt_callback,
  229. .read_bulk_callback = ti_bulk_in_callback,
  230. .write_bulk_callback = ti_bulk_out_callback,
  231. };
  232. static struct usb_serial_driver * const serial_drivers[] = {
  233. &ti_1port_device, &ti_2port_device, NULL
  234. };
  235. /* Module */
  236. MODULE_AUTHOR(TI_DRIVER_AUTHOR);
  237. MODULE_DESCRIPTION(TI_DRIVER_DESC);
  238. MODULE_LICENSE("GPL");
  239. MODULE_FIRMWARE("ti_3410.fw");
  240. MODULE_FIRMWARE("ti_5052.fw");
  241. MODULE_FIRMWARE("mts_cdma.fw");
  242. MODULE_FIRMWARE("mts_gsm.fw");
  243. MODULE_FIRMWARE("mts_edge.fw");
  244. MODULE_FIRMWARE("mts_mt9234mu.fw");
  245. MODULE_FIRMWARE("mts_mt9234zba.fw");
  246. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  247. MODULE_PARM_DESC(closing_wait,
  248. "Maximum wait for data to drain in close, in .01 secs, default is 4000");
  249. MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
  250. module_usb_serial_driver(serial_drivers, ti_id_table_combined);
  251. /* Functions */
  252. static int ti_startup(struct usb_serial *serial)
  253. {
  254. struct ti_device *tdev;
  255. struct usb_device *dev = serial->dev;
  256. int status;
  257. dev_dbg(&dev->dev,
  258. "%s - product 0x%4X, num configurations %d, configuration value %d\n",
  259. __func__, le16_to_cpu(dev->descriptor.idProduct),
  260. dev->descriptor.bNumConfigurations,
  261. dev->actconfig->desc.bConfigurationValue);
  262. /* create device structure */
  263. tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
  264. if (!tdev)
  265. return -ENOMEM;
  266. mutex_init(&tdev->td_open_close_lock);
  267. tdev->td_serial = serial;
  268. usb_set_serial_data(serial, tdev);
  269. /* determine device type */
  270. if (serial->type == &ti_1port_device)
  271. tdev->td_is_3410 = 1;
  272. dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
  273. tdev->td_is_3410 ? "3410" : "5052");
  274. /* if we have only 1 configuration, download firmware */
  275. if (dev->descriptor.bNumConfigurations == 1) {
  276. status = ti_download_firmware(tdev);
  277. if (status != 0)
  278. goto free_tdev;
  279. /* 3410 must be reset, 5052 resets itself */
  280. if (tdev->td_is_3410) {
  281. msleep_interruptible(100);
  282. usb_reset_device(dev);
  283. }
  284. status = -ENODEV;
  285. goto free_tdev;
  286. }
  287. /* the second configuration must be set */
  288. if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
  289. status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
  290. status = status ? status : -ENODEV;
  291. goto free_tdev;
  292. }
  293. if (serial->num_bulk_in < serial->num_ports ||
  294. serial->num_bulk_out < serial->num_ports) {
  295. dev_err(&serial->interface->dev, "missing endpoints\n");
  296. status = -ENODEV;
  297. goto free_tdev;
  298. }
  299. return 0;
  300. free_tdev:
  301. kfree(tdev);
  302. usb_set_serial_data(serial, NULL);
  303. return status;
  304. }
  305. static void ti_release(struct usb_serial *serial)
  306. {
  307. struct ti_device *tdev = usb_get_serial_data(serial);
  308. kfree(tdev);
  309. }
  310. static int ti_port_probe(struct usb_serial_port *port)
  311. {
  312. struct ti_port *tport;
  313. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  314. if (!tport)
  315. return -ENOMEM;
  316. spin_lock_init(&tport->tp_lock);
  317. if (port == port->serial->port[0])
  318. tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
  319. else
  320. tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
  321. port->port.closing_wait = msecs_to_jiffies(10 * closing_wait);
  322. tport->tp_port = port;
  323. tport->tp_tdev = usb_get_serial_data(port->serial);
  324. tport->tp_uart_mode = 0; /* default is RS232 */
  325. usb_set_serial_port_data(port, tport);
  326. port->port.drain_delay = 3;
  327. return 0;
  328. }
  329. static int ti_port_remove(struct usb_serial_port *port)
  330. {
  331. struct ti_port *tport;
  332. tport = usb_get_serial_port_data(port);
  333. kfree(tport);
  334. return 0;
  335. }
  336. static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
  337. {
  338. struct ti_port *tport = usb_get_serial_port_data(port);
  339. struct ti_device *tdev;
  340. struct usb_device *dev;
  341. struct urb *urb;
  342. int port_number;
  343. int status;
  344. __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
  345. TI_PIPE_TIMEOUT_ENABLE |
  346. (TI_TRANSFER_TIMEOUT << 2));
  347. if (tport == NULL)
  348. return -ENODEV;
  349. dev = port->serial->dev;
  350. tdev = tport->tp_tdev;
  351. /* only one open on any port on a device at a time */
  352. if (mutex_lock_interruptible(&tdev->td_open_close_lock))
  353. return -ERESTARTSYS;
  354. port_number = port->port_number;
  355. tport->tp_msr = 0;
  356. tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
  357. /* start interrupt urb the first time a port is opened on this device */
  358. if (tdev->td_open_port_count == 0) {
  359. dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
  360. urb = tdev->td_serial->port[0]->interrupt_in_urb;
  361. if (!urb) {
  362. dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
  363. status = -EINVAL;
  364. goto release_lock;
  365. }
  366. urb->context = tdev;
  367. status = usb_submit_urb(urb, GFP_KERNEL);
  368. if (status) {
  369. dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
  370. goto release_lock;
  371. }
  372. }
  373. if (tty)
  374. ti_set_termios(tty, port, &tty->termios);
  375. dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
  376. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  377. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  378. if (status) {
  379. dev_err(&port->dev, "%s - cannot send open command, %d\n",
  380. __func__, status);
  381. goto unlink_int_urb;
  382. }
  383. dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
  384. status = ti_command_out_sync(tdev, TI_START_PORT,
  385. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  386. if (status) {
  387. dev_err(&port->dev, "%s - cannot send start command, %d\n",
  388. __func__, status);
  389. goto unlink_int_urb;
  390. }
  391. dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
  392. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  393. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
  394. if (status) {
  395. dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
  396. __func__, status);
  397. goto unlink_int_urb;
  398. }
  399. status = ti_command_out_sync(tdev, TI_PURGE_PORT,
  400. (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
  401. if (status) {
  402. dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
  403. __func__, status);
  404. goto unlink_int_urb;
  405. }
  406. /* reset the data toggle on the bulk endpoints to work around bug in
  407. * host controllers where things get out of sync some times */
  408. usb_clear_halt(dev, port->write_urb->pipe);
  409. usb_clear_halt(dev, port->read_urb->pipe);
  410. if (tty)
  411. ti_set_termios(tty, port, &tty->termios);
  412. dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
  413. status = ti_command_out_sync(tdev, TI_OPEN_PORT,
  414. (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
  415. if (status) {
  416. dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
  417. __func__, status);
  418. goto unlink_int_urb;
  419. }
  420. dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
  421. status = ti_command_out_sync(tdev, TI_START_PORT,
  422. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  423. if (status) {
  424. dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
  425. __func__, status);
  426. goto unlink_int_urb;
  427. }
  428. /* start read urb */
  429. dev_dbg(&port->dev, "%s - start read urb\n", __func__);
  430. urb = port->read_urb;
  431. if (!urb) {
  432. dev_err(&port->dev, "%s - no read urb\n", __func__);
  433. status = -EINVAL;
  434. goto unlink_int_urb;
  435. }
  436. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  437. urb->context = tport;
  438. status = usb_submit_urb(urb, GFP_KERNEL);
  439. if (status) {
  440. dev_err(&port->dev, "%s - submit read urb failed, %d\n",
  441. __func__, status);
  442. goto unlink_int_urb;
  443. }
  444. tport->tp_is_open = 1;
  445. ++tdev->td_open_port_count;
  446. goto release_lock;
  447. unlink_int_urb:
  448. if (tdev->td_open_port_count == 0)
  449. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  450. release_lock:
  451. mutex_unlock(&tdev->td_open_close_lock);
  452. dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
  453. return status;
  454. }
  455. static void ti_close(struct usb_serial_port *port)
  456. {
  457. struct ti_device *tdev;
  458. struct ti_port *tport;
  459. int port_number;
  460. int status;
  461. int do_unlock;
  462. unsigned long flags;
  463. tdev = usb_get_serial_data(port->serial);
  464. tport = usb_get_serial_port_data(port);
  465. if (tdev == NULL || tport == NULL)
  466. return;
  467. tport->tp_is_open = 0;
  468. usb_kill_urb(port->read_urb);
  469. usb_kill_urb(port->write_urb);
  470. tport->tp_write_urb_in_use = 0;
  471. spin_lock_irqsave(&tport->tp_lock, flags);
  472. kfifo_reset_out(&port->write_fifo);
  473. spin_unlock_irqrestore(&tport->tp_lock, flags);
  474. port_number = port->port_number;
  475. dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
  476. status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
  477. (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
  478. if (status)
  479. dev_err(&port->dev,
  480. "%s - cannot send close port command, %d\n"
  481. , __func__, status);
  482. /* if mutex_lock is interrupted, continue anyway */
  483. do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
  484. --tport->tp_tdev->td_open_port_count;
  485. if (tport->tp_tdev->td_open_port_count <= 0) {
  486. /* last port is closed, shut down interrupt urb */
  487. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  488. tport->tp_tdev->td_open_port_count = 0;
  489. }
  490. if (do_unlock)
  491. mutex_unlock(&tdev->td_open_close_lock);
  492. }
  493. static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
  494. const unsigned char *data, int count)
  495. {
  496. struct ti_port *tport = usb_get_serial_port_data(port);
  497. if (count == 0) {
  498. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  499. return 0;
  500. }
  501. if (tport == NULL || !tport->tp_is_open)
  502. return -ENODEV;
  503. count = kfifo_in_locked(&port->write_fifo, data, count,
  504. &tport->tp_lock);
  505. ti_send(tport);
  506. return count;
  507. }
  508. static int ti_write_room(struct tty_struct *tty)
  509. {
  510. struct usb_serial_port *port = tty->driver_data;
  511. struct ti_port *tport = usb_get_serial_port_data(port);
  512. int room = 0;
  513. unsigned long flags;
  514. if (tport == NULL)
  515. return 0;
  516. spin_lock_irqsave(&tport->tp_lock, flags);
  517. room = kfifo_avail(&port->write_fifo);
  518. spin_unlock_irqrestore(&tport->tp_lock, flags);
  519. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  520. return room;
  521. }
  522. static int ti_chars_in_buffer(struct tty_struct *tty)
  523. {
  524. struct usb_serial_port *port = tty->driver_data;
  525. struct ti_port *tport = usb_get_serial_port_data(port);
  526. int chars = 0;
  527. unsigned long flags;
  528. if (tport == NULL)
  529. return 0;
  530. spin_lock_irqsave(&tport->tp_lock, flags);
  531. chars = kfifo_len(&port->write_fifo);
  532. spin_unlock_irqrestore(&tport->tp_lock, flags);
  533. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  534. return chars;
  535. }
  536. static bool ti_tx_empty(struct usb_serial_port *port)
  537. {
  538. struct ti_port *tport = usb_get_serial_port_data(port);
  539. int ret;
  540. u8 lsr;
  541. ret = ti_get_lsr(tport, &lsr);
  542. if (!ret && !(lsr & TI_LSR_TX_EMPTY))
  543. return false;
  544. return true;
  545. }
  546. static void ti_throttle(struct tty_struct *tty)
  547. {
  548. struct usb_serial_port *port = tty->driver_data;
  549. struct ti_port *tport = usb_get_serial_port_data(port);
  550. if (tport == NULL)
  551. return;
  552. if (I_IXOFF(tty) || C_CRTSCTS(tty))
  553. ti_stop_read(tport, tty);
  554. }
  555. static void ti_unthrottle(struct tty_struct *tty)
  556. {
  557. struct usb_serial_port *port = tty->driver_data;
  558. struct ti_port *tport = usb_get_serial_port_data(port);
  559. int status;
  560. if (tport == NULL)
  561. return;
  562. if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
  563. status = ti_restart_read(tport, tty);
  564. if (status)
  565. dev_err(&port->dev, "%s - cannot restart read, %d\n",
  566. __func__, status);
  567. }
  568. }
  569. static int ti_ioctl(struct tty_struct *tty,
  570. unsigned int cmd, unsigned long arg)
  571. {
  572. struct usb_serial_port *port = tty->driver_data;
  573. struct ti_port *tport = usb_get_serial_port_data(port);
  574. if (tport == NULL)
  575. return -ENODEV;
  576. switch (cmd) {
  577. case TIOCGSERIAL:
  578. dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
  579. return ti_get_serial_info(tport,
  580. (struct serial_struct __user *)arg);
  581. case TIOCSSERIAL:
  582. dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
  583. return ti_set_serial_info(tty, tport,
  584. (struct serial_struct __user *)arg);
  585. }
  586. return -ENOIOCTLCMD;
  587. }
  588. static void ti_set_termios(struct tty_struct *tty,
  589. struct usb_serial_port *port, struct ktermios *old_termios)
  590. {
  591. struct ti_port *tport = usb_get_serial_port_data(port);
  592. struct ti_uart_config *config;
  593. tcflag_t cflag, iflag;
  594. int baud;
  595. int status;
  596. int port_number = port->port_number;
  597. unsigned int mcr;
  598. cflag = tty->termios.c_cflag;
  599. iflag = tty->termios.c_iflag;
  600. dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
  601. dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
  602. old_termios->c_cflag, old_termios->c_iflag);
  603. if (tport == NULL)
  604. return;
  605. config = kmalloc(sizeof(*config), GFP_KERNEL);
  606. if (!config)
  607. return;
  608. config->wFlags = 0;
  609. /* these flags must be set */
  610. config->wFlags |= TI_UART_ENABLE_MS_INTS;
  611. config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
  612. config->bUartMode = (__u8)(tport->tp_uart_mode);
  613. switch (cflag & CSIZE) {
  614. case CS5:
  615. config->bDataBits = TI_UART_5_DATA_BITS;
  616. break;
  617. case CS6:
  618. config->bDataBits = TI_UART_6_DATA_BITS;
  619. break;
  620. case CS7:
  621. config->bDataBits = TI_UART_7_DATA_BITS;
  622. break;
  623. default:
  624. case CS8:
  625. config->bDataBits = TI_UART_8_DATA_BITS;
  626. break;
  627. }
  628. /* CMSPAR isn't supported by this driver */
  629. tty->termios.c_cflag &= ~CMSPAR;
  630. if (cflag & PARENB) {
  631. if (cflag & PARODD) {
  632. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  633. config->bParity = TI_UART_ODD_PARITY;
  634. } else {
  635. config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
  636. config->bParity = TI_UART_EVEN_PARITY;
  637. }
  638. } else {
  639. config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
  640. config->bParity = TI_UART_NO_PARITY;
  641. }
  642. if (cflag & CSTOPB)
  643. config->bStopBits = TI_UART_2_STOP_BITS;
  644. else
  645. config->bStopBits = TI_UART_1_STOP_BITS;
  646. if (cflag & CRTSCTS) {
  647. /* RTS flow control must be off to drop RTS for baud rate B0 */
  648. if ((cflag & CBAUD) != B0)
  649. config->wFlags |= TI_UART_ENABLE_RTS_IN;
  650. config->wFlags |= TI_UART_ENABLE_CTS_OUT;
  651. } else {
  652. ti_restart_read(tport, tty);
  653. }
  654. if (I_IXOFF(tty) || I_IXON(tty)) {
  655. config->cXon = START_CHAR(tty);
  656. config->cXoff = STOP_CHAR(tty);
  657. if (I_IXOFF(tty))
  658. config->wFlags |= TI_UART_ENABLE_X_IN;
  659. else
  660. ti_restart_read(tport, tty);
  661. if (I_IXON(tty))
  662. config->wFlags |= TI_UART_ENABLE_X_OUT;
  663. }
  664. baud = tty_get_baud_rate(tty);
  665. if (!baud)
  666. baud = 9600;
  667. if (tport->tp_tdev->td_is_3410)
  668. config->wBaudRate = (__u16)((923077 + baud/2) / baud);
  669. else
  670. config->wBaudRate = (__u16)((461538 + baud/2) / baud);
  671. /* FIXME: Should calculate resulting baud here and report it back */
  672. if ((cflag & CBAUD) != B0)
  673. tty_encode_baud_rate(tty, baud, baud);
  674. dev_dbg(&port->dev,
  675. "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
  676. __func__, baud, config->wBaudRate, config->wFlags,
  677. config->bDataBits, config->bParity, config->bStopBits,
  678. config->cXon, config->cXoff, config->bUartMode);
  679. cpu_to_be16s(&config->wBaudRate);
  680. cpu_to_be16s(&config->wFlags);
  681. status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
  682. (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
  683. sizeof(*config));
  684. if (status)
  685. dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
  686. __func__, port_number, status);
  687. /* SET_CONFIG asserts RTS and DTR, reset them correctly */
  688. mcr = tport->tp_shadow_mcr;
  689. /* if baud rate is B0, clear RTS and DTR */
  690. if ((cflag & CBAUD) == B0)
  691. mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
  692. status = ti_set_mcr(tport, mcr);
  693. if (status)
  694. dev_err(&port->dev,
  695. "%s - cannot set modem control on port %d, %d\n",
  696. __func__, port_number, status);
  697. kfree(config);
  698. }
  699. static int ti_tiocmget(struct tty_struct *tty)
  700. {
  701. struct usb_serial_port *port = tty->driver_data;
  702. struct ti_port *tport = usb_get_serial_port_data(port);
  703. unsigned int result;
  704. unsigned int msr;
  705. unsigned int mcr;
  706. unsigned long flags;
  707. if (tport == NULL)
  708. return -ENODEV;
  709. spin_lock_irqsave(&tport->tp_lock, flags);
  710. msr = tport->tp_msr;
  711. mcr = tport->tp_shadow_mcr;
  712. spin_unlock_irqrestore(&tport->tp_lock, flags);
  713. result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
  714. | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
  715. | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
  716. | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
  717. | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
  718. | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
  719. | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
  720. dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
  721. return result;
  722. }
  723. static int ti_tiocmset(struct tty_struct *tty,
  724. unsigned int set, unsigned int clear)
  725. {
  726. struct usb_serial_port *port = tty->driver_data;
  727. struct ti_port *tport = usb_get_serial_port_data(port);
  728. unsigned int mcr;
  729. unsigned long flags;
  730. if (tport == NULL)
  731. return -ENODEV;
  732. spin_lock_irqsave(&tport->tp_lock, flags);
  733. mcr = tport->tp_shadow_mcr;
  734. if (set & TIOCM_RTS)
  735. mcr |= TI_MCR_RTS;
  736. if (set & TIOCM_DTR)
  737. mcr |= TI_MCR_DTR;
  738. if (set & TIOCM_LOOP)
  739. mcr |= TI_MCR_LOOP;
  740. if (clear & TIOCM_RTS)
  741. mcr &= ~TI_MCR_RTS;
  742. if (clear & TIOCM_DTR)
  743. mcr &= ~TI_MCR_DTR;
  744. if (clear & TIOCM_LOOP)
  745. mcr &= ~TI_MCR_LOOP;
  746. spin_unlock_irqrestore(&tport->tp_lock, flags);
  747. return ti_set_mcr(tport, mcr);
  748. }
  749. static void ti_break(struct tty_struct *tty, int break_state)
  750. {
  751. struct usb_serial_port *port = tty->driver_data;
  752. struct ti_port *tport = usb_get_serial_port_data(port);
  753. int status;
  754. dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
  755. if (tport == NULL)
  756. return;
  757. status = ti_write_byte(port, tport->tp_tdev,
  758. tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
  759. TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
  760. if (status)
  761. dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
  762. }
  763. static void ti_interrupt_callback(struct urb *urb)
  764. {
  765. struct ti_device *tdev = urb->context;
  766. struct usb_serial_port *port;
  767. struct usb_serial *serial = tdev->td_serial;
  768. struct ti_port *tport;
  769. struct device *dev = &urb->dev->dev;
  770. unsigned char *data = urb->transfer_buffer;
  771. int length = urb->actual_length;
  772. int port_number;
  773. int function;
  774. int status = urb->status;
  775. int retval;
  776. __u8 msr;
  777. switch (status) {
  778. case 0:
  779. break;
  780. case -ECONNRESET:
  781. case -ENOENT:
  782. case -ESHUTDOWN:
  783. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  784. tdev->td_urb_error = 1;
  785. return;
  786. default:
  787. dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
  788. tdev->td_urb_error = 1;
  789. goto exit;
  790. }
  791. if (length != 2) {
  792. dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
  793. goto exit;
  794. }
  795. if (data[0] == TI_CODE_HARDWARE_ERROR) {
  796. dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
  797. goto exit;
  798. }
  799. port_number = TI_GET_PORT_FROM_CODE(data[0]);
  800. function = TI_GET_FUNC_FROM_CODE(data[0]);
  801. dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
  802. __func__, port_number, function, data[1]);
  803. if (port_number >= serial->num_ports) {
  804. dev_err(dev, "%s - bad port number, %d\n",
  805. __func__, port_number);
  806. goto exit;
  807. }
  808. port = serial->port[port_number];
  809. tport = usb_get_serial_port_data(port);
  810. if (!tport)
  811. goto exit;
  812. switch (function) {
  813. case TI_CODE_DATA_ERROR:
  814. dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
  815. __func__, port_number, data[1]);
  816. break;
  817. case TI_CODE_MODEM_STATUS:
  818. msr = data[1];
  819. dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
  820. ti_handle_new_msr(tport, msr);
  821. break;
  822. default:
  823. dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
  824. __func__, data[1]);
  825. break;
  826. }
  827. exit:
  828. retval = usb_submit_urb(urb, GFP_ATOMIC);
  829. if (retval)
  830. dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
  831. __func__, retval);
  832. }
  833. static void ti_bulk_in_callback(struct urb *urb)
  834. {
  835. struct ti_port *tport = urb->context;
  836. struct usb_serial_port *port = tport->tp_port;
  837. struct device *dev = &urb->dev->dev;
  838. int status = urb->status;
  839. int retval = 0;
  840. switch (status) {
  841. case 0:
  842. break;
  843. case -ECONNRESET:
  844. case -ENOENT:
  845. case -ESHUTDOWN:
  846. dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
  847. tport->tp_tdev->td_urb_error = 1;
  848. return;
  849. default:
  850. dev_err(dev, "%s - nonzero urb status, %d\n",
  851. __func__, status);
  852. tport->tp_tdev->td_urb_error = 1;
  853. }
  854. if (status == -EPIPE)
  855. goto exit;
  856. if (status) {
  857. dev_err(dev, "%s - stopping read!\n", __func__);
  858. return;
  859. }
  860. if (urb->actual_length) {
  861. usb_serial_debug_data(dev, __func__, urb->actual_length,
  862. urb->transfer_buffer);
  863. if (!tport->tp_is_open)
  864. dev_dbg(dev, "%s - port closed, dropping data\n",
  865. __func__);
  866. else
  867. ti_recv(port, urb->transfer_buffer, urb->actual_length);
  868. spin_lock(&tport->tp_lock);
  869. port->icount.rx += urb->actual_length;
  870. spin_unlock(&tport->tp_lock);
  871. }
  872. exit:
  873. /* continue to read unless stopping */
  874. spin_lock(&tport->tp_lock);
  875. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  876. retval = usb_submit_urb(urb, GFP_ATOMIC);
  877. else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
  878. tport->tp_read_urb_state = TI_READ_URB_STOPPED;
  879. spin_unlock(&tport->tp_lock);
  880. if (retval)
  881. dev_err(dev, "%s - resubmit read urb failed, %d\n",
  882. __func__, retval);
  883. }
  884. static void ti_bulk_out_callback(struct urb *urb)
  885. {
  886. struct ti_port *tport = urb->context;
  887. struct usb_serial_port *port = tport->tp_port;
  888. int status = urb->status;
  889. tport->tp_write_urb_in_use = 0;
  890. switch (status) {
  891. case 0:
  892. break;
  893. case -ECONNRESET:
  894. case -ENOENT:
  895. case -ESHUTDOWN:
  896. dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
  897. tport->tp_tdev->td_urb_error = 1;
  898. return;
  899. default:
  900. dev_err_console(port, "%s - nonzero urb status, %d\n",
  901. __func__, status);
  902. tport->tp_tdev->td_urb_error = 1;
  903. }
  904. /* send any buffered data */
  905. ti_send(tport);
  906. }
  907. static void ti_recv(struct usb_serial_port *port, unsigned char *data,
  908. int length)
  909. {
  910. int cnt;
  911. do {
  912. cnt = tty_insert_flip_string(&port->port, data, length);
  913. if (cnt < length) {
  914. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  915. __func__, length - cnt);
  916. if (cnt == 0)
  917. break;
  918. }
  919. tty_flip_buffer_push(&port->port);
  920. data += cnt;
  921. length -= cnt;
  922. } while (length > 0);
  923. }
  924. static void ti_send(struct ti_port *tport)
  925. {
  926. int count, result;
  927. struct usb_serial_port *port = tport->tp_port;
  928. unsigned long flags;
  929. spin_lock_irqsave(&tport->tp_lock, flags);
  930. if (tport->tp_write_urb_in_use)
  931. goto unlock;
  932. count = kfifo_out(&port->write_fifo,
  933. port->write_urb->transfer_buffer,
  934. port->bulk_out_size);
  935. if (count == 0)
  936. goto unlock;
  937. tport->tp_write_urb_in_use = 1;
  938. spin_unlock_irqrestore(&tport->tp_lock, flags);
  939. usb_serial_debug_data(&port->dev, __func__, count,
  940. port->write_urb->transfer_buffer);
  941. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  942. usb_sndbulkpipe(port->serial->dev,
  943. port->bulk_out_endpointAddress),
  944. port->write_urb->transfer_buffer, count,
  945. ti_bulk_out_callback, tport);
  946. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  947. if (result) {
  948. dev_err_console(port, "%s - submit write urb failed, %d\n",
  949. __func__, result);
  950. tport->tp_write_urb_in_use = 0;
  951. /* TODO: reschedule ti_send */
  952. } else {
  953. spin_lock_irqsave(&tport->tp_lock, flags);
  954. port->icount.tx += count;
  955. spin_unlock_irqrestore(&tport->tp_lock, flags);
  956. }
  957. /* more room in the buffer for new writes, wakeup */
  958. tty_port_tty_wakeup(&port->port);
  959. return;
  960. unlock:
  961. spin_unlock_irqrestore(&tport->tp_lock, flags);
  962. return;
  963. }
  964. static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
  965. {
  966. unsigned long flags;
  967. int status;
  968. status = ti_write_byte(tport->tp_port, tport->tp_tdev,
  969. tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
  970. TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
  971. spin_lock_irqsave(&tport->tp_lock, flags);
  972. if (!status)
  973. tport->tp_shadow_mcr = mcr;
  974. spin_unlock_irqrestore(&tport->tp_lock, flags);
  975. return status;
  976. }
  977. static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
  978. {
  979. int size, status;
  980. struct ti_device *tdev = tport->tp_tdev;
  981. struct usb_serial_port *port = tport->tp_port;
  982. int port_number = port->port_number;
  983. struct ti_port_status *data;
  984. size = sizeof(struct ti_port_status);
  985. data = kmalloc(size, GFP_KERNEL);
  986. if (!data)
  987. return -ENOMEM;
  988. status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
  989. (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
  990. if (status) {
  991. dev_err(&port->dev,
  992. "%s - get port status command failed, %d\n",
  993. __func__, status);
  994. goto free_data;
  995. }
  996. dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
  997. *lsr = data->bLSR;
  998. free_data:
  999. kfree(data);
  1000. return status;
  1001. }
  1002. static int ti_get_serial_info(struct ti_port *tport,
  1003. struct serial_struct __user *ret_arg)
  1004. {
  1005. struct usb_serial_port *port = tport->tp_port;
  1006. struct serial_struct ret_serial;
  1007. unsigned cwait;
  1008. if (!ret_arg)
  1009. return -EFAULT;
  1010. cwait = port->port.closing_wait;
  1011. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1012. cwait = jiffies_to_msecs(cwait) / 10;
  1013. memset(&ret_serial, 0, sizeof(ret_serial));
  1014. ret_serial.type = PORT_16550A;
  1015. ret_serial.line = port->minor;
  1016. ret_serial.port = port->port_number;
  1017. ret_serial.flags = tport->tp_flags;
  1018. ret_serial.xmit_fifo_size = kfifo_size(&port->write_fifo);
  1019. ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
  1020. ret_serial.closing_wait = cwait;
  1021. if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
  1022. return -EFAULT;
  1023. return 0;
  1024. }
  1025. static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
  1026. struct serial_struct __user *new_arg)
  1027. {
  1028. struct serial_struct new_serial;
  1029. unsigned cwait;
  1030. if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
  1031. return -EFAULT;
  1032. cwait = new_serial.closing_wait;
  1033. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1034. cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
  1035. tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
  1036. tport->tp_port->port.closing_wait = cwait;
  1037. return 0;
  1038. }
  1039. static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
  1040. {
  1041. struct async_icount *icount;
  1042. struct tty_struct *tty;
  1043. unsigned long flags;
  1044. dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
  1045. if (msr & TI_MSR_DELTA_MASK) {
  1046. spin_lock_irqsave(&tport->tp_lock, flags);
  1047. icount = &tport->tp_port->icount;
  1048. if (msr & TI_MSR_DELTA_CTS)
  1049. icount->cts++;
  1050. if (msr & TI_MSR_DELTA_DSR)
  1051. icount->dsr++;
  1052. if (msr & TI_MSR_DELTA_CD)
  1053. icount->dcd++;
  1054. if (msr & TI_MSR_DELTA_RI)
  1055. icount->rng++;
  1056. wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
  1057. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1058. }
  1059. tport->tp_msr = msr & TI_MSR_MASK;
  1060. /* handle CTS flow control */
  1061. tty = tty_port_tty_get(&tport->tp_port->port);
  1062. if (tty && C_CRTSCTS(tty)) {
  1063. if (msr & TI_MSR_CTS)
  1064. tty_wakeup(tty);
  1065. }
  1066. tty_kref_put(tty);
  1067. }
  1068. static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
  1069. {
  1070. unsigned long flags;
  1071. spin_lock_irqsave(&tport->tp_lock, flags);
  1072. if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
  1073. tport->tp_read_urb_state = TI_READ_URB_STOPPING;
  1074. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1075. }
  1076. static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
  1077. {
  1078. struct urb *urb;
  1079. int status = 0;
  1080. unsigned long flags;
  1081. spin_lock_irqsave(&tport->tp_lock, flags);
  1082. if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
  1083. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1084. urb = tport->tp_port->read_urb;
  1085. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1086. urb->context = tport;
  1087. status = usb_submit_urb(urb, GFP_KERNEL);
  1088. } else {
  1089. tport->tp_read_urb_state = TI_READ_URB_RUNNING;
  1090. spin_unlock_irqrestore(&tport->tp_lock, flags);
  1091. }
  1092. return status;
  1093. }
  1094. static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
  1095. __u16 moduleid, __u16 value, __u8 *data, int size)
  1096. {
  1097. int status;
  1098. status = usb_control_msg(tdev->td_serial->dev,
  1099. usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
  1100. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  1101. value, moduleid, data, size, 1000);
  1102. if (status < 0)
  1103. return status;
  1104. return 0;
  1105. }
  1106. static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
  1107. __u16 moduleid, __u16 value, __u8 *data, int size)
  1108. {
  1109. int status;
  1110. status = usb_control_msg(tdev->td_serial->dev,
  1111. usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
  1112. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  1113. value, moduleid, data, size, 1000);
  1114. if (status == size)
  1115. status = 0;
  1116. else if (status >= 0)
  1117. status = -ECOMM;
  1118. return status;
  1119. }
  1120. static int ti_write_byte(struct usb_serial_port *port,
  1121. struct ti_device *tdev, unsigned long addr,
  1122. __u8 mask, __u8 byte)
  1123. {
  1124. int status;
  1125. unsigned int size;
  1126. struct ti_write_data_bytes *data;
  1127. dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
  1128. addr, mask, byte);
  1129. size = sizeof(struct ti_write_data_bytes) + 2;
  1130. data = kmalloc(size, GFP_KERNEL);
  1131. if (!data)
  1132. return -ENOMEM;
  1133. data->bAddrType = TI_RW_DATA_ADDR_XDATA;
  1134. data->bDataType = TI_RW_DATA_BYTE;
  1135. data->bDataCounter = 1;
  1136. data->wBaseAddrHi = cpu_to_be16(addr>>16);
  1137. data->wBaseAddrLo = cpu_to_be16(addr);
  1138. data->bData[0] = mask;
  1139. data->bData[1] = byte;
  1140. status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
  1141. (__u8 *)data, size);
  1142. if (status < 0)
  1143. dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
  1144. kfree(data);
  1145. return status;
  1146. }
  1147. static int ti_do_download(struct usb_device *dev, int pipe,
  1148. u8 *buffer, int size)
  1149. {
  1150. int pos;
  1151. u8 cs = 0;
  1152. int done;
  1153. struct ti_firmware_header *header;
  1154. int status = 0;
  1155. int len;
  1156. for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
  1157. cs = (__u8)(cs + buffer[pos]);
  1158. header = (struct ti_firmware_header *)buffer;
  1159. header->wLength = cpu_to_le16((__u16)(size
  1160. - sizeof(struct ti_firmware_header)));
  1161. header->bCheckSum = cs;
  1162. dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
  1163. for (pos = 0; pos < size; pos += done) {
  1164. len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
  1165. status = usb_bulk_msg(dev, pipe, buffer + pos, len,
  1166. &done, 1000);
  1167. if (status)
  1168. break;
  1169. }
  1170. return status;
  1171. }
  1172. static int ti_download_firmware(struct ti_device *tdev)
  1173. {
  1174. int status;
  1175. int buffer_size;
  1176. __u8 *buffer;
  1177. struct usb_device *dev = tdev->td_serial->dev;
  1178. unsigned int pipe = usb_sndbulkpipe(dev,
  1179. tdev->td_serial->port[0]->bulk_out_endpointAddress);
  1180. const struct firmware *fw_p;
  1181. char buf[32];
  1182. /* try ID specific firmware first, then try generic firmware */
  1183. sprintf(buf, "ti_usb-v%04x-p%04x.fw",
  1184. le16_to_cpu(dev->descriptor.idVendor),
  1185. le16_to_cpu(dev->descriptor.idProduct));
  1186. status = request_firmware(&fw_p, buf, &dev->dev);
  1187. if (status != 0) {
  1188. buf[0] = '\0';
  1189. if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
  1190. switch (le16_to_cpu(dev->descriptor.idProduct)) {
  1191. case MTS_CDMA_PRODUCT_ID:
  1192. strcpy(buf, "mts_cdma.fw");
  1193. break;
  1194. case MTS_GSM_PRODUCT_ID:
  1195. strcpy(buf, "mts_gsm.fw");
  1196. break;
  1197. case MTS_EDGE_PRODUCT_ID:
  1198. strcpy(buf, "mts_edge.fw");
  1199. break;
  1200. case MTS_MT9234MU_PRODUCT_ID:
  1201. strcpy(buf, "mts_mt9234mu.fw");
  1202. break;
  1203. case MTS_MT9234ZBA_PRODUCT_ID:
  1204. strcpy(buf, "mts_mt9234zba.fw");
  1205. break;
  1206. case MTS_MT9234ZBAOLD_PRODUCT_ID:
  1207. strcpy(buf, "mts_mt9234zba.fw");
  1208. break; }
  1209. }
  1210. if (buf[0] == '\0') {
  1211. if (tdev->td_is_3410)
  1212. strcpy(buf, "ti_3410.fw");
  1213. else
  1214. strcpy(buf, "ti_5052.fw");
  1215. }
  1216. status = request_firmware(&fw_p, buf, &dev->dev);
  1217. }
  1218. if (status) {
  1219. dev_err(&dev->dev, "%s - firmware not found\n", __func__);
  1220. return -ENOENT;
  1221. }
  1222. if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
  1223. dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
  1224. release_firmware(fw_p);
  1225. return -ENOENT;
  1226. }
  1227. buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
  1228. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1229. if (buffer) {
  1230. memcpy(buffer, fw_p->data, fw_p->size);
  1231. memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
  1232. status = ti_do_download(dev, pipe, buffer, fw_p->size);
  1233. kfree(buffer);
  1234. } else {
  1235. status = -ENOMEM;
  1236. }
  1237. release_firmware(fw_p);
  1238. if (status) {
  1239. dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
  1240. __func__, status);
  1241. return status;
  1242. }
  1243. dev_dbg(&dev->dev, "%s - download successful\n", __func__);
  1244. return 0;
  1245. }