ti_usb_3410_5052.c 46 KB

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