mos7720.c 42 KB

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