mos7720.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port {
  61. __u8 shadowLCR; /* last LCR value received */
  62. __u8 shadowMCR; /* last MCR value received */
  63. __u8 shadowMSR; /* last MSR value received */
  64. char open;
  65. struct async_icount icount;
  66. struct usb_serial_port *port; /* loop back to the owner */
  67. struct urb *write_urb_pool[NUM_URBS];
  68. };
  69. /* This structure holds all of the individual serial device information */
  70. struct moschip_serial {
  71. int interrupt_started;
  72. };
  73. static int debug;
  74. #define USB_VENDOR_ID_MOSCHIP 0x9710
  75. #define MOSCHIP_DEVICE_ID_7720 0x7720
  76. #define MOSCHIP_DEVICE_ID_7715 0x7715
  77. static struct usb_device_id moschip_port_id_table[] = {
  78. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  79. { } /* terminating entry */
  80. };
  81. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  82. /*
  83. * mos7720_interrupt_callback
  84. * this is the callback function for when we have received data on the
  85. * interrupt endpoint.
  86. */
  87. static void mos7720_interrupt_callback(struct urb *urb)
  88. {
  89. int result;
  90. int length;
  91. int status = urb->status;
  92. __u8 *data;
  93. __u8 sp1;
  94. __u8 sp2;
  95. dbg("%s", " : Entering\n");
  96. switch (status) {
  97. case 0:
  98. /* success */
  99. break;
  100. case -ECONNRESET:
  101. case -ENOENT:
  102. case -ESHUTDOWN:
  103. /* this urb is terminated, clean up */
  104. dbg("%s - urb shutting down with status: %d", __func__,
  105. status);
  106. return;
  107. default:
  108. dbg("%s - nonzero urb status received: %d", __func__,
  109. status);
  110. goto exit;
  111. }
  112. length = urb->actual_length;
  113. data = urb->transfer_buffer;
  114. /* Moschip get 4 bytes
  115. * Byte 1 IIR Port 1 (port.number is 0)
  116. * Byte 2 IIR Port 2 (port.number is 1)
  117. * Byte 3 --------------
  118. * Byte 4 FIFO status for both */
  119. /* the above description is inverted
  120. * oneukum 2007-03-14 */
  121. if (unlikely(length != 4)) {
  122. dbg("Wrong data !!!");
  123. return;
  124. }
  125. sp1 = data[3];
  126. sp2 = data[2];
  127. if ((sp1 | sp2) & 0x01) {
  128. /* No Interrupt Pending in both the ports */
  129. dbg("No Interrupt !!!");
  130. } else {
  131. switch (sp1 & 0x0f) {
  132. case SERIAL_IIR_RLS:
  133. dbg("Serial Port 1: Receiver status error or address "
  134. "bit detected in 9-bit mode\n");
  135. break;
  136. case SERIAL_IIR_CTI:
  137. dbg("Serial Port 1: Receiver time out");
  138. break;
  139. case SERIAL_IIR_MS:
  140. dbg("Serial Port 1: Modem status change");
  141. break;
  142. }
  143. switch (sp2 & 0x0f) {
  144. case SERIAL_IIR_RLS:
  145. dbg("Serial Port 2: Receiver status error or address "
  146. "bit detected in 9-bit mode");
  147. break;
  148. case SERIAL_IIR_CTI:
  149. dbg("Serial Port 2: Receiver time out");
  150. break;
  151. case SERIAL_IIR_MS:
  152. dbg("Serial Port 2: Modem status change");
  153. break;
  154. }
  155. }
  156. exit:
  157. result = usb_submit_urb(urb, GFP_ATOMIC);
  158. if (result)
  159. dev_err(&urb->dev->dev,
  160. "%s - Error %d submitting control urb\n",
  161. __func__, result);
  162. return;
  163. }
  164. /*
  165. * mos7720_bulk_in_callback
  166. * this is the callback function for when we have received data on the
  167. * bulk in endpoint.
  168. */
  169. static void mos7720_bulk_in_callback(struct urb *urb)
  170. {
  171. int retval;
  172. unsigned char *data ;
  173. struct usb_serial_port *port;
  174. struct moschip_port *mos7720_port;
  175. struct tty_struct *tty;
  176. int status = urb->status;
  177. if (status) {
  178. dbg("nonzero read bulk status received: %d", status);
  179. return;
  180. }
  181. mos7720_port = urb->context;
  182. if (!mos7720_port) {
  183. dbg("%s", "NULL mos7720_port pointer \n");
  184. return ;
  185. }
  186. port = mos7720_port->port;
  187. dbg("Entering...%s", __func__);
  188. data = urb->transfer_buffer;
  189. tty = tty_port_tty_get(&port->port);
  190. if (tty && urb->actual_length) {
  191. tty_buffer_request_room(tty, urb->actual_length);
  192. tty_insert_flip_string(tty, data, urb->actual_length);
  193. tty_flip_buffer_push(tty);
  194. }
  195. tty_kref_put(tty);
  196. if (!port->read_urb) {
  197. dbg("URB KILLED !!!");
  198. return;
  199. }
  200. if (port->read_urb->status != -EINPROGRESS) {
  201. port->read_urb->dev = port->serial->dev;
  202. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  203. if (retval)
  204. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  205. retval);
  206. }
  207. }
  208. /*
  209. * mos7720_bulk_out_data_callback
  210. * this is the callback function for when we have finished sending serial
  211. * data on the bulk out endpoint.
  212. */
  213. static void mos7720_bulk_out_data_callback(struct urb *urb)
  214. {
  215. struct moschip_port *mos7720_port;
  216. struct tty_struct *tty;
  217. int status = urb->status;
  218. if (status) {
  219. dbg("nonzero write bulk status received:%d", status);
  220. return;
  221. }
  222. mos7720_port = urb->context;
  223. if (!mos7720_port) {
  224. dbg("NULL mos7720_port pointer");
  225. return ;
  226. }
  227. dbg("Entering .........");
  228. tty = tty_port_tty_get(&mos7720_port->port->port);
  229. if (tty && mos7720_port->open)
  230. tty_wakeup(tty);
  231. tty_kref_put(tty);
  232. }
  233. /*
  234. * send_mos_cmd
  235. * this function will be used for sending command to device
  236. */
  237. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  238. __u16 index, void *data)
  239. {
  240. int status;
  241. unsigned int pipe;
  242. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  243. __u8 requesttype;
  244. __u16 size = 0x0000;
  245. if (value < MOS_MAX_PORT) {
  246. if (product == MOSCHIP_DEVICE_ID_7715)
  247. value = value*0x100+0x100;
  248. else
  249. value = value*0x100+0x200;
  250. } else {
  251. value = 0x0000;
  252. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  253. (index != 0x08)) {
  254. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  255. /* index = 0x01 ; */
  256. }
  257. }
  258. if (request == MOS_WRITE) {
  259. request = (__u8)MOS_WRITE;
  260. requesttype = (__u8)0x40;
  261. value = value + (__u16)*((unsigned char *)data);
  262. data = NULL;
  263. pipe = usb_sndctrlpipe(serial->dev, 0);
  264. } else {
  265. request = (__u8)MOS_READ;
  266. requesttype = (__u8)0xC0;
  267. size = 0x01;
  268. pipe = usb_rcvctrlpipe(serial->dev, 0);
  269. }
  270. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  271. value, index, data, size, MOS_WDR_TIMEOUT);
  272. if (status < 0)
  273. dbg("Command Write failed Value %x index %x\n", value, index);
  274. return status;
  275. }
  276. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  277. {
  278. struct usb_serial *serial;
  279. struct usb_serial_port *port0;
  280. struct urb *urb;
  281. struct moschip_serial *mos7720_serial;
  282. struct moschip_port *mos7720_port;
  283. int response;
  284. int port_number;
  285. char data;
  286. int allocated_urbs = 0;
  287. int j;
  288. serial = port->serial;
  289. mos7720_port = usb_get_serial_port_data(port);
  290. if (mos7720_port == NULL)
  291. return -ENODEV;
  292. port0 = serial->port[0];
  293. mos7720_serial = usb_get_serial_data(serial);
  294. if (mos7720_serial == NULL || port0 == NULL)
  295. return -ENODEV;
  296. usb_clear_halt(serial->dev, port->write_urb->pipe);
  297. usb_clear_halt(serial->dev, port->read_urb->pipe);
  298. /* Initialising the write urb pool */
  299. for (j = 0; j < NUM_URBS; ++j) {
  300. urb = usb_alloc_urb(0, GFP_KERNEL);
  301. mos7720_port->write_urb_pool[j] = urb;
  302. if (urb == NULL) {
  303. dev_err(&port->dev, "No more urbs???\n");
  304. continue;
  305. }
  306. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  307. GFP_KERNEL);
  308. if (!urb->transfer_buffer) {
  309. dev_err(&port->dev,
  310. "%s-out of memory for urb buffers.\n",
  311. __func__);
  312. usb_free_urb(mos7720_port->write_urb_pool[j]);
  313. mos7720_port->write_urb_pool[j] = NULL;
  314. continue;
  315. }
  316. allocated_urbs++;
  317. }
  318. if (!allocated_urbs)
  319. return -ENOMEM;
  320. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  321. *
  322. * Register Index
  323. * 0 : THR/RHR
  324. * 1 : IER
  325. * 2 : FCR
  326. * 3 : LCR
  327. * 4 : MCR
  328. * 5 : LSR
  329. * 6 : MSR
  330. * 7 : SPR
  331. *
  332. * 0x08 : SP1/2 Control Reg
  333. */
  334. port_number = port->number - port->serial->minor;
  335. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  336. dbg("SS::%p LSR:%x\n", mos7720_port, data);
  337. dbg("Check:Sending Command ..........");
  338. data = 0x02;
  339. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  340. data = 0x02;
  341. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  342. data = 0x00;
  343. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  344. data = 0x00;
  345. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  346. data = 0xCF;
  347. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  348. data = 0x03;
  349. mos7720_port->shadowLCR = data;
  350. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  351. data = 0x0b;
  352. mos7720_port->shadowMCR = data;
  353. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  354. data = 0x0b;
  355. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  356. data = 0x00;
  357. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  358. data = 0x00;
  359. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  360. /* data = 0x00;
  361. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  362. data = 0x03;
  363. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  364. data = 0x00;
  365. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
  366. port_number + 1, &data);
  367. */
  368. data = 0x00;
  369. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  370. data = data | (port->number - port->serial->minor + 1);
  371. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  372. data = 0x83;
  373. mos7720_port->shadowLCR = data;
  374. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  375. data = 0x0c;
  376. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  377. data = 0x00;
  378. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  379. data = 0x03;
  380. mos7720_port->shadowLCR = data;
  381. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  382. data = 0x0c;
  383. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  384. data = 0x0c;
  385. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  386. /* see if we've set up our endpoint info yet *
  387. * (can't set it up in mos7720_startup as the *
  388. * structures were not set up at that time.) */
  389. if (!mos7720_serial->interrupt_started) {
  390. dbg("Interrupt buffer NULL !!!");
  391. /* not set up yet, so do it now */
  392. mos7720_serial->interrupt_started = 1;
  393. dbg("To Submit URB !!!");
  394. /* set up our interrupt urb */
  395. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  396. usb_rcvintpipe(serial->dev,
  397. port->interrupt_in_endpointAddress),
  398. port0->interrupt_in_buffer,
  399. port0->interrupt_in_urb->transfer_buffer_length,
  400. mos7720_interrupt_callback, mos7720_port,
  401. port0->interrupt_in_urb->interval);
  402. /* start interrupt read for this mos7720 this interrupt *
  403. * will continue as long as the mos7720 is connected */
  404. dbg("Submit URB over !!!");
  405. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  406. if (response)
  407. dev_err(&port->dev,
  408. "%s - Error %d submitting control urb\n",
  409. __func__, response);
  410. }
  411. /* set up our bulk in urb */
  412. usb_fill_bulk_urb(port->read_urb, serial->dev,
  413. usb_rcvbulkpipe(serial->dev,
  414. port->bulk_in_endpointAddress),
  415. port->bulk_in_buffer,
  416. port->read_urb->transfer_buffer_length,
  417. mos7720_bulk_in_callback, mos7720_port);
  418. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  419. if (response)
  420. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  421. __func__, response);
  422. /* initialize our icount structure */
  423. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  424. /* initialize our port settings */
  425. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  426. /* send a open port command */
  427. mos7720_port->open = 1;
  428. return 0;
  429. }
  430. /*
  431. * mos7720_chars_in_buffer
  432. * this function is called by the tty driver when it wants to know how many
  433. * bytes of data we currently have outstanding in the port (data that has
  434. * been written, but hasn't made it out the port yet)
  435. * If successful, we return the number of bytes left to be written in the
  436. * system,
  437. * Otherwise we return a negative error number.
  438. */
  439. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  440. {
  441. struct usb_serial_port *port = tty->driver_data;
  442. int i;
  443. int chars = 0;
  444. struct moschip_port *mos7720_port;
  445. dbg("%s:entering ...........", __func__);
  446. mos7720_port = usb_get_serial_port_data(port);
  447. if (mos7720_port == NULL) {
  448. dbg("%s:leaving ...........", __func__);
  449. return 0;
  450. }
  451. for (i = 0; i < NUM_URBS; ++i) {
  452. if (mos7720_port->write_urb_pool[i] &&
  453. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  454. chars += URB_TRANSFER_BUFFER_SIZE;
  455. }
  456. dbg("%s - returns %d", __func__, chars);
  457. return chars;
  458. }
  459. static void mos7720_close(struct usb_serial_port *port)
  460. {
  461. struct usb_serial *serial;
  462. struct moschip_port *mos7720_port;
  463. char data;
  464. int j;
  465. dbg("mos7720_close:entering...");
  466. serial = port->serial;
  467. mos7720_port = usb_get_serial_port_data(port);
  468. if (mos7720_port == NULL)
  469. return;
  470. for (j = 0; j < NUM_URBS; ++j)
  471. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  472. /* Freeing Write URBs */
  473. for (j = 0; j < NUM_URBS; ++j) {
  474. if (mos7720_port->write_urb_pool[j]) {
  475. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  476. usb_free_urb(mos7720_port->write_urb_pool[j]);
  477. }
  478. }
  479. /* While closing port, shutdown all bulk read, write *
  480. * and interrupt read if they exists, otherwise nop */
  481. dbg("Shutdown bulk write");
  482. usb_kill_urb(port->write_urb);
  483. dbg("Shutdown bulk read");
  484. usb_kill_urb(port->read_urb);
  485. mutex_lock(&serial->disc_mutex);
  486. /* these commands must not be issued if the device has
  487. * been disconnected */
  488. if (!serial->disconnected) {
  489. data = 0x00;
  490. send_mos_cmd(serial, MOS_WRITE,
  491. port->number - port->serial->minor, 0x04, &data);
  492. data = 0x00;
  493. send_mos_cmd(serial, MOS_WRITE,
  494. port->number - port->serial->minor, 0x01, &data);
  495. }
  496. mutex_unlock(&serial->disc_mutex);
  497. mos7720_port->open = 0;
  498. dbg("Leaving %s", __func__);
  499. }
  500. static void mos7720_break(struct tty_struct *tty, int break_state)
  501. {
  502. struct usb_serial_port *port = tty->driver_data;
  503. unsigned char data;
  504. struct usb_serial *serial;
  505. struct moschip_port *mos7720_port;
  506. dbg("Entering %s", __func__);
  507. serial = port->serial;
  508. mos7720_port = usb_get_serial_port_data(port);
  509. if (mos7720_port == NULL)
  510. return;
  511. if (break_state == -1)
  512. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  513. else
  514. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  515. mos7720_port->shadowLCR = data;
  516. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  517. 0x03, &data);
  518. return;
  519. }
  520. /*
  521. * mos7720_write_room
  522. * this function is called by the tty driver when it wants to know how many
  523. * bytes of data we can accept for a specific port.
  524. * If successful, we return the amount of room that we have for this port
  525. * Otherwise we return a negative error number.
  526. */
  527. static int mos7720_write_room(struct tty_struct *tty)
  528. {
  529. struct usb_serial_port *port = tty->driver_data;
  530. struct moschip_port *mos7720_port;
  531. int room = 0;
  532. int i;
  533. dbg("%s:entering ...........", __func__);
  534. mos7720_port = usb_get_serial_port_data(port);
  535. if (mos7720_port == NULL) {
  536. dbg("%s:leaving ...........", __func__);
  537. return -ENODEV;
  538. }
  539. /* FIXME: Locking */
  540. for (i = 0; i < NUM_URBS; ++i) {
  541. if (mos7720_port->write_urb_pool[i] &&
  542. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  543. room += URB_TRANSFER_BUFFER_SIZE;
  544. }
  545. dbg("%s - returns %d", __func__, room);
  546. return room;
  547. }
  548. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  549. const unsigned char *data, int count)
  550. {
  551. int status;
  552. int i;
  553. int bytes_sent = 0;
  554. int transfer_size;
  555. struct moschip_port *mos7720_port;
  556. struct usb_serial *serial;
  557. struct urb *urb;
  558. const unsigned char *current_position = data;
  559. dbg("%s:entering ...........", __func__);
  560. serial = port->serial;
  561. mos7720_port = usb_get_serial_port_data(port);
  562. if (mos7720_port == NULL) {
  563. dbg("mos7720_port is NULL");
  564. return -ENODEV;
  565. }
  566. /* try to find a free urb in the list */
  567. urb = NULL;
  568. for (i = 0; i < NUM_URBS; ++i) {
  569. if (mos7720_port->write_urb_pool[i] &&
  570. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  571. urb = mos7720_port->write_urb_pool[i];
  572. dbg("URB:%d", i);
  573. break;
  574. }
  575. }
  576. if (urb == NULL) {
  577. dbg("%s - no more free urbs", __func__);
  578. goto exit;
  579. }
  580. if (urb->transfer_buffer == NULL) {
  581. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  582. GFP_KERNEL);
  583. if (urb->transfer_buffer == NULL) {
  584. dev_err(&port->dev, "%s no more kernel memory...\n",
  585. __func__);
  586. goto exit;
  587. }
  588. }
  589. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  590. memcpy(urb->transfer_buffer, current_position, transfer_size);
  591. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  592. urb->transfer_buffer);
  593. /* fill urb with data and submit */
  594. usb_fill_bulk_urb(urb, serial->dev,
  595. usb_sndbulkpipe(serial->dev,
  596. port->bulk_out_endpointAddress),
  597. urb->transfer_buffer, transfer_size,
  598. mos7720_bulk_out_data_callback, mos7720_port);
  599. /* send it down the pipe */
  600. status = usb_submit_urb(urb, GFP_ATOMIC);
  601. if (status) {
  602. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  603. "with status = %d\n", __func__, status);
  604. bytes_sent = status;
  605. goto exit;
  606. }
  607. bytes_sent = transfer_size;
  608. exit:
  609. return bytes_sent;
  610. }
  611. static void mos7720_throttle(struct tty_struct *tty)
  612. {
  613. struct usb_serial_port *port = tty->driver_data;
  614. struct moschip_port *mos7720_port;
  615. int status;
  616. dbg("%s- port %d\n", __func__, port->number);
  617. mos7720_port = usb_get_serial_port_data(port);
  618. if (mos7720_port == NULL)
  619. return;
  620. if (!mos7720_port->open) {
  621. dbg("port not opened");
  622. return;
  623. }
  624. dbg("%s: Entering ..........", __func__);
  625. /* if we are implementing XON/XOFF, send the stop character */
  626. if (I_IXOFF(tty)) {
  627. unsigned char stop_char = STOP_CHAR(tty);
  628. status = mos7720_write(tty, port, &stop_char, 1);
  629. if (status <= 0)
  630. return;
  631. }
  632. /* if we are implementing RTS/CTS, toggle that line */
  633. if (tty->termios->c_cflag & CRTSCTS) {
  634. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  635. status = send_mos_cmd(port->serial, MOS_WRITE,
  636. port->number - port->serial->minor,
  637. UART_MCR, &mos7720_port->shadowMCR);
  638. if (status != 0)
  639. return;
  640. }
  641. }
  642. static void mos7720_unthrottle(struct tty_struct *tty)
  643. {
  644. struct usb_serial_port *port = tty->driver_data;
  645. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  646. int status;
  647. if (mos7720_port == NULL)
  648. return;
  649. if (!mos7720_port->open) {
  650. dbg("%s - port not opened", __func__);
  651. return;
  652. }
  653. dbg("%s: Entering ..........", __func__);
  654. /* if we are implementing XON/XOFF, send the start character */
  655. if (I_IXOFF(tty)) {
  656. unsigned char start_char = START_CHAR(tty);
  657. status = mos7720_write(tty, port, &start_char, 1);
  658. if (status <= 0)
  659. return;
  660. }
  661. /* if we are implementing RTS/CTS, toggle that line */
  662. if (tty->termios->c_cflag & CRTSCTS) {
  663. mos7720_port->shadowMCR |= UART_MCR_RTS;
  664. status = send_mos_cmd(port->serial, MOS_WRITE,
  665. port->number - port->serial->minor,
  666. UART_MCR, &mos7720_port->shadowMCR);
  667. if (status != 0)
  668. return;
  669. }
  670. }
  671. static int set_higher_rates(struct moschip_port *mos7720_port,
  672. unsigned int baud)
  673. {
  674. unsigned char data;
  675. struct usb_serial_port *port;
  676. struct usb_serial *serial;
  677. int port_number;
  678. if (mos7720_port == NULL)
  679. return -EINVAL;
  680. port = mos7720_port->port;
  681. serial = port->serial;
  682. /***********************************************
  683. * Init Sequence for higher rates
  684. ***********************************************/
  685. dbg("Sending Setting Commands ..........");
  686. port_number = port->number - port->serial->minor;
  687. data = 0x000;
  688. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  689. data = 0x000;
  690. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  691. data = 0x0CF;
  692. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  693. data = 0x00b;
  694. mos7720_port->shadowMCR = data;
  695. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  696. data = 0x00b;
  697. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  698. data = 0x000;
  699. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  700. data = 0x000;
  701. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  702. /***********************************************
  703. * Set for higher rates *
  704. ***********************************************/
  705. data = baud * 0x10;
  706. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  707. data = 0x003;
  708. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  709. data = 0x003;
  710. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  711. data = 0x02b;
  712. mos7720_port->shadowMCR = data;
  713. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  714. data = 0x02b;
  715. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  716. /***********************************************
  717. * Set DLL/DLM
  718. ***********************************************/
  719. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  720. mos7720_port->shadowLCR = data;
  721. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  722. data = 0x001; /* DLL */
  723. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  724. data = 0x000; /* DLM */
  725. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  726. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  727. mos7720_port->shadowLCR = data;
  728. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  729. return 0;
  730. }
  731. /* baud rate information */
  732. struct divisor_table_entry {
  733. __u32 baudrate;
  734. __u16 divisor;
  735. };
  736. /* Define table of divisors for moschip 7720 hardware *
  737. * These assume a 3.6864MHz crystal, the standard /16, and *
  738. * MCR.7 = 0. */
  739. static struct divisor_table_entry divisor_table[] = {
  740. { 50, 2304},
  741. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  742. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  743. { 150, 768},
  744. { 300, 384},
  745. { 600, 192},
  746. { 1200, 96},
  747. { 1800, 64},
  748. { 2400, 48},
  749. { 4800, 24},
  750. { 7200, 16},
  751. { 9600, 12},
  752. { 19200, 6},
  753. { 38400, 3},
  754. { 57600, 2},
  755. { 115200, 1},
  756. };
  757. /*****************************************************************************
  758. * calc_baud_rate_divisor
  759. * this function calculates the proper baud rate divisor for the specified
  760. * baud rate.
  761. *****************************************************************************/
  762. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  763. {
  764. int i;
  765. __u16 custom;
  766. __u16 round1;
  767. __u16 round;
  768. dbg("%s - %d", __func__, baudrate);
  769. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  770. if (divisor_table[i].baudrate == baudrate) {
  771. *divisor = divisor_table[i].divisor;
  772. return 0;
  773. }
  774. }
  775. /* After trying for all the standard baud rates *
  776. * Try calculating the divisor for this baud rate */
  777. if (baudrate > 75 && baudrate < 230400) {
  778. /* get the divisor */
  779. custom = (__u16)(230400L / baudrate);
  780. /* Check for round off */
  781. round1 = (__u16)(2304000L / baudrate);
  782. round = (__u16)(round1 - (custom * 10));
  783. if (round > 4)
  784. custom++;
  785. *divisor = custom;
  786. dbg("Baud %d = %d", baudrate, custom);
  787. return 0;
  788. }
  789. dbg("Baud calculation Failed...");
  790. return -EINVAL;
  791. }
  792. /*
  793. * send_cmd_write_baud_rate
  794. * this function sends the proper command to change the baud rate of the
  795. * specified port.
  796. */
  797. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  798. int baudrate)
  799. {
  800. struct usb_serial_port *port;
  801. struct usb_serial *serial;
  802. int divisor;
  803. int status;
  804. unsigned char data;
  805. unsigned char number;
  806. if (mos7720_port == NULL)
  807. return -1;
  808. port = mos7720_port->port;
  809. serial = port->serial;
  810. dbg("%s: Entering ..........", __func__);
  811. number = port->number - port->serial->minor;
  812. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  813. /* Calculate the Divisor */
  814. status = calc_baud_rate_divisor(baudrate, &divisor);
  815. if (status) {
  816. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  817. return status;
  818. }
  819. /* Enable access to divisor latch */
  820. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  821. mos7720_port->shadowLCR = data;
  822. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  823. /* Write the divisor */
  824. data = ((unsigned char)(divisor & 0xff));
  825. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  826. data = ((unsigned char)((divisor & 0xff00) >> 8));
  827. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  828. /* Disable access to divisor latch */
  829. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  830. mos7720_port->shadowLCR = data;
  831. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  832. return status;
  833. }
  834. /*
  835. * change_port_settings
  836. * This routine is called to set the UART on the device to match
  837. * the specified new settings.
  838. */
  839. static void change_port_settings(struct tty_struct *tty,
  840. struct moschip_port *mos7720_port,
  841. struct ktermios *old_termios)
  842. {
  843. struct usb_serial_port *port;
  844. struct usb_serial *serial;
  845. int baud;
  846. unsigned cflag;
  847. unsigned iflag;
  848. __u8 mask = 0xff;
  849. __u8 lData;
  850. __u8 lParity;
  851. __u8 lStop;
  852. int status;
  853. int port_number;
  854. char data;
  855. if (mos7720_port == NULL)
  856. return ;
  857. port = mos7720_port->port;
  858. serial = port->serial;
  859. port_number = port->number - port->serial->minor;
  860. dbg("%s - port %d", __func__, port->number);
  861. if (!mos7720_port->open) {
  862. dbg("%s - port not opened", __func__);
  863. return;
  864. }
  865. dbg("%s: Entering ..........", __func__);
  866. lData = UART_LCR_WLEN8;
  867. lStop = 0x00; /* 1 stop bit */
  868. lParity = 0x00; /* No parity */
  869. cflag = tty->termios->c_cflag;
  870. iflag = tty->termios->c_iflag;
  871. /* Change the number of bits */
  872. switch (cflag & CSIZE) {
  873. case CS5:
  874. lData = UART_LCR_WLEN5;
  875. mask = 0x1f;
  876. break;
  877. case CS6:
  878. lData = UART_LCR_WLEN6;
  879. mask = 0x3f;
  880. break;
  881. case CS7:
  882. lData = UART_LCR_WLEN7;
  883. mask = 0x7f;
  884. break;
  885. default:
  886. case CS8:
  887. lData = UART_LCR_WLEN8;
  888. break;
  889. }
  890. /* Change the Parity bit */
  891. if (cflag & PARENB) {
  892. if (cflag & PARODD) {
  893. lParity = UART_LCR_PARITY;
  894. dbg("%s - parity = odd", __func__);
  895. } else {
  896. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  897. dbg("%s - parity = even", __func__);
  898. }
  899. } else {
  900. dbg("%s - parity = none", __func__);
  901. }
  902. if (cflag & CMSPAR)
  903. lParity = lParity | 0x20;
  904. /* Change the Stop bit */
  905. if (cflag & CSTOPB) {
  906. lStop = UART_LCR_STOP;
  907. dbg("%s - stop bits = 2", __func__);
  908. } else {
  909. lStop = 0x00;
  910. dbg("%s - stop bits = 1", __func__);
  911. }
  912. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  913. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  914. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  915. /* Update the LCR with the correct value */
  916. mos7720_port->shadowLCR &=
  917. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  918. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  919. /* Disable Interrupts */
  920. data = 0x00;
  921. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  922. UART_IER, &data);
  923. data = 0x00;
  924. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  925. data = 0xcf;
  926. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  927. /* Send the updated LCR value to the mos7720 */
  928. data = mos7720_port->shadowLCR;
  929. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  930. data = 0x00b;
  931. mos7720_port->shadowMCR = data;
  932. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  933. data = 0x00b;
  934. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  935. /* set up the MCR register and send it to the mos7720 */
  936. mos7720_port->shadowMCR = UART_MCR_OUT2;
  937. if (cflag & CBAUD)
  938. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  939. if (cflag & CRTSCTS) {
  940. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  941. /* To set hardware flow control to the specified *
  942. * serial port, in SP1/2_CONTROL_REG */
  943. if (port->number) {
  944. data = 0x001;
  945. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  946. 0x08, &data);
  947. } else {
  948. data = 0x002;
  949. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  950. 0x08, &data);
  951. }
  952. } else {
  953. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  954. }
  955. data = mos7720_port->shadowMCR;
  956. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  957. /* Determine divisor based on baud rate */
  958. baud = tty_get_baud_rate(tty);
  959. if (!baud) {
  960. /* pick a default, any default... */
  961. dbg("Picked default baud...");
  962. baud = 9600;
  963. }
  964. if (baud >= 230400) {
  965. set_higher_rates(mos7720_port, baud);
  966. /* Enable Interrupts */
  967. data = 0x0c;
  968. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  969. return;
  970. }
  971. dbg("%s - baud rate = %d", __func__, baud);
  972. status = send_cmd_write_baud_rate(mos7720_port, baud);
  973. /* FIXME: needs to write actual resulting baud back not just
  974. blindly do so */
  975. if (cflag & CBAUD)
  976. tty_encode_baud_rate(tty, baud, baud);
  977. /* Enable Interrupts */
  978. data = 0x0c;
  979. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  980. if (port->read_urb->status != -EINPROGRESS) {
  981. port->read_urb->dev = serial->dev;
  982. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  983. if (status)
  984. dbg("usb_submit_urb(read bulk) failed, status = %d",
  985. status);
  986. }
  987. return;
  988. }
  989. /*
  990. * mos7720_set_termios
  991. * this function is called by the tty driver when it wants to change the
  992. * termios structure.
  993. */
  994. static void mos7720_set_termios(struct tty_struct *tty,
  995. struct usb_serial_port *port, struct ktermios *old_termios)
  996. {
  997. int status;
  998. unsigned int cflag;
  999. struct usb_serial *serial;
  1000. struct moschip_port *mos7720_port;
  1001. serial = port->serial;
  1002. mos7720_port = usb_get_serial_port_data(port);
  1003. if (mos7720_port == NULL)
  1004. return;
  1005. if (!mos7720_port->open) {
  1006. dbg("%s - port not opened", __func__);
  1007. return;
  1008. }
  1009. dbg("%s\n", "setting termios - ASPIRE");
  1010. cflag = tty->termios->c_cflag;
  1011. dbg("%s - cflag %08x iflag %08x", __func__,
  1012. tty->termios->c_cflag,
  1013. RELEVANT_IFLAG(tty->termios->c_iflag));
  1014. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1015. old_termios->c_cflag,
  1016. RELEVANT_IFLAG(old_termios->c_iflag));
  1017. dbg("%s - port %d", __func__, port->number);
  1018. /* change the port settings to the new ones specified */
  1019. change_port_settings(tty, mos7720_port, old_termios);
  1020. if (!port->read_urb) {
  1021. dbg("%s", "URB KILLED !!!!!\n");
  1022. return;
  1023. }
  1024. if (port->read_urb->status != -EINPROGRESS) {
  1025. port->read_urb->dev = serial->dev;
  1026. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1027. if (status)
  1028. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1029. status);
  1030. }
  1031. return;
  1032. }
  1033. /*
  1034. * get_lsr_info - get line status register info
  1035. *
  1036. * Purpose: Let user call ioctl() to get info when the UART physically
  1037. * is emptied. On bus types like RS485, the transmitter must
  1038. * release the bus after transmitting. This must be done when
  1039. * the transmit shift register is empty, not be done when the
  1040. * transmit holding register is empty. This functionality
  1041. * allows an RS485 driver to be written in user space.
  1042. */
  1043. static int get_lsr_info(struct tty_struct *tty,
  1044. struct moschip_port *mos7720_port, unsigned int __user *value)
  1045. {
  1046. struct usb_serial_port *port = tty->driver_data;
  1047. unsigned int result = 0;
  1048. unsigned char data = 0;
  1049. int port_number = port->number - port->serial->minor;
  1050. int count;
  1051. count = mos7720_chars_in_buffer(tty);
  1052. if (count == 0) {
  1053. send_mos_cmd(port->serial, MOS_READ, port_number,
  1054. UART_LSR, &data);
  1055. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1056. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1057. dbg("%s -- Empty", __func__);
  1058. result = TIOCSER_TEMT;
  1059. }
  1060. }
  1061. if (copy_to_user(value, &result, sizeof(int)))
  1062. return -EFAULT;
  1063. return 0;
  1064. }
  1065. static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
  1066. {
  1067. struct usb_serial_port *port = tty->driver_data;
  1068. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1069. unsigned int result = 0;
  1070. unsigned int mcr ;
  1071. unsigned int msr ;
  1072. dbg("%s - port %d", __func__, port->number);
  1073. mcr = mos7720_port->shadowMCR;
  1074. msr = mos7720_port->shadowMSR;
  1075. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1076. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1077. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1078. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1079. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1080. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1081. dbg("%s -- %x", __func__, result);
  1082. return result;
  1083. }
  1084. static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
  1085. unsigned int set, unsigned int clear)
  1086. {
  1087. struct usb_serial_port *port = tty->driver_data;
  1088. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1089. unsigned int mcr ;
  1090. unsigned char lmcr;
  1091. dbg("%s - port %d", __func__, port->number);
  1092. dbg("he was at tiocmget");
  1093. mcr = mos7720_port->shadowMCR;
  1094. if (set & TIOCM_RTS)
  1095. mcr |= UART_MCR_RTS;
  1096. if (set & TIOCM_DTR)
  1097. mcr |= UART_MCR_DTR;
  1098. if (set & TIOCM_LOOP)
  1099. mcr |= UART_MCR_LOOP;
  1100. if (clear & TIOCM_RTS)
  1101. mcr &= ~UART_MCR_RTS;
  1102. if (clear & TIOCM_DTR)
  1103. mcr &= ~UART_MCR_DTR;
  1104. if (clear & TIOCM_LOOP)
  1105. mcr &= ~UART_MCR_LOOP;
  1106. mos7720_port->shadowMCR = mcr;
  1107. lmcr = mos7720_port->shadowMCR;
  1108. send_mos_cmd(port->serial, MOS_WRITE,
  1109. port->number - port->serial->minor, UART_MCR, &lmcr);
  1110. return 0;
  1111. }
  1112. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1113. unsigned int __user *value)
  1114. {
  1115. unsigned int mcr ;
  1116. unsigned int arg;
  1117. unsigned char data;
  1118. struct usb_serial_port *port;
  1119. if (mos7720_port == NULL)
  1120. return -1;
  1121. port = (struct usb_serial_port *)mos7720_port->port;
  1122. mcr = mos7720_port->shadowMCR;
  1123. if (copy_from_user(&arg, value, sizeof(int)))
  1124. return -EFAULT;
  1125. switch (cmd) {
  1126. case TIOCMBIS:
  1127. if (arg & TIOCM_RTS)
  1128. mcr |= UART_MCR_RTS;
  1129. if (arg & TIOCM_DTR)
  1130. mcr |= UART_MCR_RTS;
  1131. if (arg & TIOCM_LOOP)
  1132. mcr |= UART_MCR_LOOP;
  1133. break;
  1134. case TIOCMBIC:
  1135. if (arg & TIOCM_RTS)
  1136. mcr &= ~UART_MCR_RTS;
  1137. if (arg & TIOCM_DTR)
  1138. mcr &= ~UART_MCR_RTS;
  1139. if (arg & TIOCM_LOOP)
  1140. mcr &= ~UART_MCR_LOOP;
  1141. break;
  1142. }
  1143. mos7720_port->shadowMCR = mcr;
  1144. data = mos7720_port->shadowMCR;
  1145. send_mos_cmd(port->serial, MOS_WRITE,
  1146. port->number - port->serial->minor, UART_MCR, &data);
  1147. return 0;
  1148. }
  1149. static int get_serial_info(struct moschip_port *mos7720_port,
  1150. struct serial_struct __user *retinfo)
  1151. {
  1152. struct serial_struct tmp;
  1153. if (!retinfo)
  1154. return -EFAULT;
  1155. memset(&tmp, 0, sizeof(tmp));
  1156. tmp.type = PORT_16550A;
  1157. tmp.line = mos7720_port->port->serial->minor;
  1158. tmp.port = mos7720_port->port->number;
  1159. tmp.irq = 0;
  1160. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1161. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1162. tmp.baud_base = 9600;
  1163. tmp.close_delay = 5*HZ;
  1164. tmp.closing_wait = 30*HZ;
  1165. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1166. return -EFAULT;
  1167. return 0;
  1168. }
  1169. static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
  1170. unsigned int cmd, unsigned long arg)
  1171. {
  1172. struct usb_serial_port *port = tty->driver_data;
  1173. struct moschip_port *mos7720_port;
  1174. struct async_icount cnow;
  1175. struct async_icount cprev;
  1176. struct serial_icounter_struct icount;
  1177. mos7720_port = usb_get_serial_port_data(port);
  1178. if (mos7720_port == NULL)
  1179. return -ENODEV;
  1180. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1181. switch (cmd) {
  1182. case TIOCSERGETLSR:
  1183. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1184. return get_lsr_info(tty, mos7720_port,
  1185. (unsigned int __user *)arg);
  1186. return 0;
  1187. /* FIXME: These should be using the mode methods */
  1188. case TIOCMBIS:
  1189. case TIOCMBIC:
  1190. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1191. __func__, port->number);
  1192. return set_modem_info(mos7720_port, cmd,
  1193. (unsigned int __user *)arg);
  1194. case TIOCGSERIAL:
  1195. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1196. return get_serial_info(mos7720_port,
  1197. (struct serial_struct __user *)arg);
  1198. case TIOCMIWAIT:
  1199. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1200. cprev = mos7720_port->icount;
  1201. while (1) {
  1202. if (signal_pending(current))
  1203. return -ERESTARTSYS;
  1204. cnow = mos7720_port->icount;
  1205. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1206. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1207. return -EIO; /* no change => error */
  1208. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1209. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1210. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1211. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1212. return 0;
  1213. }
  1214. cprev = cnow;
  1215. }
  1216. /* NOTREACHED */
  1217. break;
  1218. case TIOCGICOUNT:
  1219. cnow = mos7720_port->icount;
  1220. icount.cts = cnow.cts;
  1221. icount.dsr = cnow.dsr;
  1222. icount.rng = cnow.rng;
  1223. icount.dcd = cnow.dcd;
  1224. icount.rx = cnow.rx;
  1225. icount.tx = cnow.tx;
  1226. icount.frame = cnow.frame;
  1227. icount.overrun = cnow.overrun;
  1228. icount.parity = cnow.parity;
  1229. icount.brk = cnow.brk;
  1230. icount.buf_overrun = cnow.buf_overrun;
  1231. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1232. port->number, icount.rx, icount.tx);
  1233. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1234. return -EFAULT;
  1235. return 0;
  1236. }
  1237. return -ENOIOCTLCMD;
  1238. }
  1239. static int mos7720_startup(struct usb_serial *serial)
  1240. {
  1241. struct moschip_serial *mos7720_serial;
  1242. struct moschip_port *mos7720_port;
  1243. struct usb_device *dev;
  1244. int i;
  1245. char data;
  1246. dbg("%s: Entering ..........", __func__);
  1247. if (!serial) {
  1248. dbg("Invalid Handler");
  1249. return -ENODEV;
  1250. }
  1251. dev = serial->dev;
  1252. /* create our private serial structure */
  1253. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1254. if (mos7720_serial == NULL) {
  1255. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1256. return -ENOMEM;
  1257. }
  1258. usb_set_serial_data(serial, mos7720_serial);
  1259. /* we set up the pointers to the endpoints in the mos7720_open *
  1260. * function, as the structures aren't created yet. */
  1261. /* set up port private structures */
  1262. for (i = 0; i < serial->num_ports; ++i) {
  1263. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1264. if (mos7720_port == NULL) {
  1265. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1266. usb_set_serial_data(serial, NULL);
  1267. kfree(mos7720_serial);
  1268. return -ENOMEM;
  1269. }
  1270. /* Initialize all port interrupt end point to port 0 int
  1271. * endpoint. Our device has only one interrupt endpoint
  1272. * comman to all ports */
  1273. serial->port[i]->interrupt_in_endpointAddress =
  1274. serial->port[0]->interrupt_in_endpointAddress;
  1275. mos7720_port->port = serial->port[i];
  1276. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1277. dbg("port number is %d", serial->port[i]->number);
  1278. dbg("serial number is %d", serial->minor);
  1279. }
  1280. /* setting configuration feature to one */
  1281. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1282. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1283. /* LSR For Port 1 */
  1284. send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
  1285. dbg("LSR:%x", data);
  1286. /* LSR For Port 2 */
  1287. send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
  1288. dbg("LSR:%x", data);
  1289. return 0;
  1290. }
  1291. static void mos7720_release(struct usb_serial *serial)
  1292. {
  1293. int i;
  1294. /* free private structure allocated for serial port */
  1295. for (i = 0; i < serial->num_ports; ++i)
  1296. kfree(usb_get_serial_port_data(serial->port[i]));
  1297. /* free private structure allocated for serial device */
  1298. kfree(usb_get_serial_data(serial));
  1299. }
  1300. static struct usb_driver usb_driver = {
  1301. .name = "moschip7720",
  1302. .probe = usb_serial_probe,
  1303. .disconnect = usb_serial_disconnect,
  1304. .id_table = moschip_port_id_table,
  1305. .no_dynamic_id = 1,
  1306. };
  1307. static struct usb_serial_driver moschip7720_2port_driver = {
  1308. .driver = {
  1309. .owner = THIS_MODULE,
  1310. .name = "moschip7720",
  1311. },
  1312. .description = "Moschip 2 port adapter",
  1313. .usb_driver = &usb_driver,
  1314. .id_table = moschip_port_id_table,
  1315. .num_ports = 2,
  1316. .open = mos7720_open,
  1317. .close = mos7720_close,
  1318. .throttle = mos7720_throttle,
  1319. .unthrottle = mos7720_unthrottle,
  1320. .attach = mos7720_startup,
  1321. .release = mos7720_release,
  1322. .ioctl = mos7720_ioctl,
  1323. .tiocmget = mos7720_tiocmget,
  1324. .tiocmset = mos7720_tiocmset,
  1325. .set_termios = mos7720_set_termios,
  1326. .write = mos7720_write,
  1327. .write_room = mos7720_write_room,
  1328. .chars_in_buffer = mos7720_chars_in_buffer,
  1329. .break_ctl = mos7720_break,
  1330. .read_bulk_callback = mos7720_bulk_in_callback,
  1331. .read_int_callback = mos7720_interrupt_callback,
  1332. };
  1333. static int __init moschip7720_init(void)
  1334. {
  1335. int retval;
  1336. dbg("%s: Entering ..........", __func__);
  1337. /* Register with the usb serial */
  1338. retval = usb_serial_register(&moschip7720_2port_driver);
  1339. if (retval)
  1340. goto failed_port_device_register;
  1341. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1342. DRIVER_DESC "\n");
  1343. /* Register with the usb */
  1344. retval = usb_register(&usb_driver);
  1345. if (retval)
  1346. goto failed_usb_register;
  1347. return 0;
  1348. failed_usb_register:
  1349. usb_serial_deregister(&moschip7720_2port_driver);
  1350. failed_port_device_register:
  1351. return retval;
  1352. }
  1353. static void __exit moschip7720_exit(void)
  1354. {
  1355. usb_deregister(&usb_driver);
  1356. usb_serial_deregister(&moschip7720_2port_driver);
  1357. }
  1358. module_init(moschip7720_init);
  1359. module_exit(moschip7720_exit);
  1360. /* Module information */
  1361. MODULE_AUTHOR(DRIVER_AUTHOR);
  1362. MODULE_DESCRIPTION(DRIVER_DESC);
  1363. MODULE_LICENSE("GPL");
  1364. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1365. MODULE_PARM_DESC(debug, "Debug enabled or not");