ti_usb_3410_5052.c 49 KB

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