ti_usb_3410_5052.c 45 KB

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