ti_usb_3410_5052.c 42 KB

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