ti_usb_3410_5052.c 45 KB

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