ti_usb_3410_5052.c 44 KB

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