ti_usb_3410_5052.c 40 KB

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