ti_usb_3410_5052.c 43 KB

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