osfuncs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /***************************************************************************
  2. sys/osfuncs.c
  3. -------------------
  4. copyright : (C) 2001, 2002 by Frank Mori Hess
  5. email : fmhess@users.sourceforge.net
  6. ***************************************************************************/
  7. /***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "ibsys.h"
  16. #include "autopoll.h"
  17. #include <linux/fcntl.h>
  18. #include <linux/kmod.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/vmalloc.h>
  21. static int board_type_ioctl(gpib_file_private_t *file_priv, gpib_board_t *board, unsigned long arg);
  22. static int read_ioctl( gpib_file_private_t *file_priv, gpib_board_t *board,
  23. unsigned long arg);
  24. static int write_ioctl( gpib_file_private_t *file_priv, gpib_board_t *board,
  25. unsigned long arg);
  26. static int command_ioctl( gpib_file_private_t *file_priv, gpib_board_t *board,
  27. unsigned long arg);
  28. static int open_dev_ioctl( struct file *filep, gpib_board_t *board, unsigned long arg );
  29. static int close_dev_ioctl( struct file *filep, gpib_board_t *board, unsigned long arg );
  30. static int serial_poll_ioctl( gpib_board_t *board, unsigned long arg );
  31. static int wait_ioctl( gpib_file_private_t *file_priv, gpib_board_t *board, unsigned long arg );
  32. static int parallel_poll_ioctl( gpib_board_t *board, unsigned long arg );
  33. static int online_ioctl( gpib_board_t *board, unsigned long arg );
  34. static int remote_enable_ioctl( gpib_board_t *board, unsigned long arg );
  35. static int take_control_ioctl( gpib_board_t *board, unsigned long arg );
  36. static int line_status_ioctl( gpib_board_t *board, unsigned long arg );
  37. static int pad_ioctl( gpib_board_t *board, gpib_file_private_t *file_priv,
  38. unsigned long arg );
  39. static int sad_ioctl( gpib_board_t *board, gpib_file_private_t *file_priv,
  40. unsigned long arg );
  41. static int eos_ioctl( gpib_board_t *board, unsigned long arg );
  42. static int request_service_ioctl( gpib_board_t *board, unsigned long arg );
  43. static int iobase_ioctl( gpib_board_t *board, unsigned long arg );
  44. static int irq_ioctl( gpib_board_t *board, unsigned long arg );
  45. static int dma_ioctl( gpib_board_t *board, unsigned long arg );
  46. static int autospoll_ioctl(gpib_board_t *board, unsigned long arg);
  47. static int mutex_ioctl( gpib_board_t *board, gpib_file_private_t *file_priv,
  48. unsigned long arg );
  49. static int timeout_ioctl( gpib_board_t *board, unsigned long arg );
  50. static int status_bytes_ioctl( gpib_board_t *board, unsigned long arg );
  51. static int board_info_ioctl( const gpib_board_t *board, unsigned long arg );
  52. static int ppc_ioctl( gpib_board_t *board, unsigned long arg );
  53. static int query_board_rsv_ioctl( gpib_board_t *board, unsigned long arg );
  54. static int interface_clear_ioctl( gpib_board_t *board, unsigned long arg );
  55. static int select_pci_ioctl( gpib_board_t *board, unsigned long arg );
  56. static int event_ioctl( gpib_board_t *board, unsigned long arg );
  57. static int request_system_control_ioctl( gpib_board_t *board, unsigned long arg );
  58. static int t1_delay_ioctl( gpib_board_t *board, unsigned long arg );
  59. static int cleanup_open_devices( gpib_file_private_t *file_priv, gpib_board_t *board );
  60. static gpib_descriptor_t* handle_to_descriptor( const gpib_file_private_t *file_priv,
  61. int handle )
  62. {
  63. if( handle < 0 || handle >= GPIB_MAX_NUM_DESCRIPTORS )
  64. {
  65. printk( "gpib: invalid handle %i\n", handle );
  66. return NULL;
  67. }
  68. return file_priv->descriptors[ handle ];
  69. }
  70. static int init_gpib_file_private( gpib_file_private_t *priv )
  71. {
  72. memset(priv, 0, sizeof(*priv));
  73. priv->descriptors[ 0 ] = kmalloc( sizeof( gpib_descriptor_t ), GFP_KERNEL );
  74. if( priv->descriptors[ 0 ] == NULL )
  75. {
  76. printk( "gpib: failed to allocate default board descriptor\n" );
  77. return -ENOMEM;
  78. }
  79. init_gpib_descriptor( priv->descriptors[ 0 ] );
  80. priv->descriptors[ 0 ]->is_board = 1;
  81. mutex_init(&priv->descriptors_mutex);
  82. return 0;
  83. }
  84. int ibopen(struct inode *inode, struct file *filep)
  85. {
  86. unsigned int minor = iminor(inode);
  87. gpib_board_t *board;
  88. gpib_file_private_t *priv;
  89. if(minor >= GPIB_MAX_NUM_BOARDS)
  90. {
  91. printk("gpib: invalid minor number of device file\n");
  92. return -ENXIO;
  93. }
  94. board = &board_array[minor];
  95. filep->private_data = kmalloc( sizeof( gpib_file_private_t ), GFP_KERNEL );
  96. if( filep->private_data == NULL )
  97. {
  98. return -ENOMEM;
  99. }
  100. priv = filep->private_data;
  101. init_gpib_file_private( ( gpib_file_private_t * ) filep->private_data );
  102. GPIB_DPRINTK( "gpib: opening minor %d\n", minor );
  103. if(board->use_count == 0)
  104. {
  105. char module_string[ 32 ];
  106. int retval;
  107. snprintf(module_string, sizeof(module_string), "gpib%i", minor);
  108. retval = request_module( module_string );
  109. if( retval )
  110. {
  111. printk( "gpib: (debug) request module returned %i\n", retval );
  112. }
  113. }
  114. if(board->interface)
  115. {
  116. if(!try_module_get(board->provider_module))
  117. {
  118. printk("gpib: try_module_get() failed\n");
  119. return -ENOSYS;
  120. }
  121. board->use_count++;
  122. priv->got_module = 1;
  123. }
  124. return 0;
  125. }
  126. int ibclose(struct inode *inode, struct file *filep)
  127. {
  128. unsigned int minor = iminor(inode);
  129. gpib_board_t *board;
  130. gpib_file_private_t *priv = filep->private_data;
  131. if(minor >= GPIB_MAX_NUM_BOARDS)
  132. {
  133. printk("gpib: invalid minor number of device file\n");
  134. return -ENODEV;
  135. }
  136. GPIB_DPRINTK( "gpib: closing minor %d\n", minor );
  137. board = &board_array[ minor ];
  138. if( priv )
  139. {
  140. cleanup_open_devices( priv, board );
  141. if( priv->holding_mutex )
  142. mutex_unlock( &board->mutex );
  143. if(priv->got_module && board->use_count)
  144. {
  145. module_put(board->provider_module);
  146. --board->use_count;
  147. }
  148. kfree( filep->private_data );
  149. filep->private_data = NULL;
  150. }
  151. return 0;
  152. }
  153. long ibioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  154. {
  155. unsigned int minor = iminor(filep->f_dentry->d_inode);
  156. gpib_board_t *board;
  157. gpib_file_private_t *file_priv = filep->private_data;
  158. long retval = -ENOTTY;
  159. if( minor >= GPIB_MAX_NUM_BOARDS )
  160. {
  161. printk("gpib: invalid minor number of device file\n");
  162. return -ENODEV;
  163. }
  164. board = &board_array[ minor ];
  165. lock_kernel();
  166. GPIB_DPRINTK( "pid %i, minor %i, ioctl %d, interface=%s, use=%d, onl=%d\n",
  167. current->pid, minor, cmd & 0xff,
  168. board->interface ? board->interface->name : "",
  169. board->use_count,
  170. board->online );
  171. switch( cmd )
  172. {
  173. case CFCBOARDTYPE:
  174. retval = board_type_ioctl(file_priv, board, arg);
  175. goto done;
  176. break;
  177. case IBONL:
  178. retval = online_ioctl( board, arg );
  179. goto done;
  180. break;
  181. default:
  182. break;
  183. }
  184. if( board->interface == NULL )
  185. {
  186. printk("gpib: no gpib board configured on /dev/gpib%i\n", minor);
  187. retval = -ENODEV;
  188. goto done;
  189. }
  190. if(file_priv->got_module == 0)
  191. {
  192. if(!try_module_get(board->provider_module))
  193. {
  194. printk("gpib: try_module_get() failed\n");
  195. retval = -ENOSYS;
  196. goto done;
  197. }
  198. file_priv->got_module = 1;
  199. board->use_count++;
  200. }
  201. switch( cmd )
  202. {
  203. case CFCBASE:
  204. retval = iobase_ioctl( board, arg );
  205. goto done;
  206. break;
  207. case CFCIRQ:
  208. retval = irq_ioctl( board, arg );
  209. goto done;
  210. break;
  211. case CFCDMA:
  212. retval = dma_ioctl( board, arg );
  213. goto done;
  214. break;
  215. case IBAUTOSPOLL:
  216. retval = autospoll_ioctl(board, arg);
  217. goto done;
  218. break;
  219. case IBBOARD_INFO:
  220. retval = board_info_ioctl( board, arg );
  221. goto done;
  222. break;
  223. case IBMUTEX:
  224. retval = mutex_ioctl( board, file_priv, arg );
  225. goto done;
  226. break;
  227. case IBPAD:
  228. retval = pad_ioctl( board, file_priv, arg );
  229. goto done;
  230. break;
  231. case IBSAD:
  232. retval = sad_ioctl( board, file_priv, arg );
  233. goto done;
  234. break;
  235. case IBSELECT_PCI:
  236. retval = select_pci_ioctl( board, arg );
  237. goto done;
  238. break;
  239. default:
  240. break;
  241. }
  242. if( !board->online )
  243. {
  244. printk( "gpib: ioctl %i invalid for offline board\n",
  245. cmd & 0xff );
  246. retval = -EINVAL;
  247. goto done;
  248. }
  249. switch( cmd )
  250. {
  251. case IBEVENT:
  252. retval = event_ioctl( board, arg );
  253. goto done;
  254. break;
  255. case IBCLOSEDEV:
  256. retval = close_dev_ioctl( filep, board, arg );
  257. goto done;
  258. break;
  259. case IBOPENDEV:
  260. retval = open_dev_ioctl( filep, board, arg );
  261. goto done;
  262. break;
  263. case IBSPOLL_BYTES:
  264. retval = status_bytes_ioctl( board, arg );
  265. goto done;
  266. break;
  267. case IBWAIT:
  268. retval = wait_ioctl( file_priv, board, arg );
  269. goto done;
  270. break;
  271. case IBLINES:
  272. retval = line_status_ioctl( board, arg );
  273. goto done;
  274. break;
  275. case IBLOC:
  276. board->interface->return_to_local( board );
  277. retval = 0;
  278. goto done;
  279. break;
  280. default:
  281. break;
  282. }
  283. if( current->pid != board->locking_pid )
  284. {
  285. printk( "gpib: need to hold board lock to perform ioctl %i\n",
  286. cmd & 0xff );
  287. retval = -EPERM;
  288. goto done;
  289. }
  290. switch( cmd )
  291. {
  292. case IB_T1_DELAY:
  293. retval = t1_delay_ioctl( board, arg );
  294. goto done;
  295. break;
  296. case IBCAC:
  297. retval = take_control_ioctl( board, arg );
  298. goto done;
  299. break;
  300. case IBCMD:
  301. retval = command_ioctl( file_priv, board, arg );
  302. goto done;
  303. break;
  304. case IBEOS:
  305. retval = eos_ioctl( board, arg );
  306. goto done;
  307. break;
  308. case IBGTS:
  309. retval = ibgts( board );
  310. goto done;
  311. break;
  312. case IBPPC:
  313. retval = ppc_ioctl( board, arg );
  314. goto done;
  315. break;
  316. case IBQUERY_BOARD_RSV:
  317. retval = query_board_rsv_ioctl( board, arg );
  318. goto done;
  319. break;
  320. case IBRD:
  321. retval = read_ioctl( file_priv, board, arg );
  322. goto done;
  323. break;
  324. case IBRPP:
  325. retval = parallel_poll_ioctl( board, arg );
  326. goto done;
  327. break;
  328. case IBRSC:
  329. retval = request_system_control_ioctl( board, arg );
  330. goto done;
  331. break;
  332. case IBRSP:
  333. retval = serial_poll_ioctl( board, arg );
  334. goto done;
  335. break;
  336. case IBRSV:
  337. retval = request_service_ioctl( board, arg );
  338. goto done;
  339. break;
  340. case IBSIC:
  341. retval = interface_clear_ioctl( board, arg );
  342. goto done;
  343. break;
  344. case IBSRE:
  345. retval = remote_enable_ioctl( board, arg );
  346. goto done;
  347. break;
  348. case IBTMO:
  349. retval = timeout_ioctl( board, arg );
  350. goto done;
  351. break;
  352. case IBWRT:
  353. retval = write_ioctl( file_priv, board, arg );
  354. goto done;
  355. break;
  356. default:
  357. retval = -ENOTTY;
  358. goto done;
  359. break;
  360. }
  361. done:
  362. unlock_kernel();
  363. return retval;
  364. }
  365. static int board_type_ioctl(gpib_file_private_t *file_priv, gpib_board_t *board, unsigned long arg)
  366. {
  367. struct list_head *list_ptr;
  368. board_type_ioctl_t cmd;
  369. int retval;
  370. if(!capable(CAP_SYS_ADMIN))
  371. return -EPERM;
  372. if(board->online)
  373. {
  374. printk("gpib: can't change board type while board is online.\n");
  375. return -EBUSY;
  376. }
  377. retval = copy_from_user(&cmd, (void*)arg, sizeof(board_type_ioctl_t));
  378. if(retval)
  379. {
  380. return retval;
  381. }
  382. for(list_ptr = registered_drivers.next; list_ptr != &registered_drivers; list_ptr = list_ptr->next)
  383. {
  384. gpib_interface_list_t *entry;
  385. entry = list_entry(list_ptr, gpib_interface_list_t, list);
  386. if(strcmp(entry->interface->name, cmd.name) == 0)
  387. {
  388. int i;
  389. int had_module = file_priv->got_module;
  390. if(board->use_count)
  391. {
  392. for(i = 0; i < board->use_count; ++i)
  393. {
  394. module_put(board->provider_module);
  395. }
  396. board->interface = NULL;
  397. file_priv->got_module = 0;
  398. }
  399. board->interface = entry->interface;
  400. board->provider_module = entry->module;
  401. for(i = 0; i < board->use_count; ++i)
  402. {
  403. if(!try_module_get(entry->module))
  404. {
  405. board->use_count = i;
  406. return -ENOSYS;
  407. }
  408. }
  409. if(had_module == 0)
  410. {
  411. if(!try_module_get(entry->module))
  412. {
  413. return -ENOSYS;
  414. }
  415. ++board->use_count;
  416. }
  417. file_priv->got_module = 1;
  418. return 0;
  419. }
  420. }
  421. return -EINVAL;
  422. }
  423. static int read_ioctl( gpib_file_private_t *file_priv, gpib_board_t *board,
  424. unsigned long arg)
  425. {
  426. read_write_ioctl_t read_cmd;
  427. uint8_t *userbuf;
  428. unsigned long remain;
  429. int end_flag = 0;
  430. int retval;
  431. ssize_t read_ret = 0;
  432. gpib_descriptor_t *desc;
  433. size_t nbytes;
  434. retval = copy_from_user(&read_cmd, (void*) arg, sizeof(read_cmd));
  435. if (retval)
  436. return -EFAULT;
  437. desc = handle_to_descriptor( file_priv, read_cmd.handle );
  438. if( desc == NULL ) return -EINVAL;
  439. BUG_ON(sizeof(userbuf) > sizeof(read_cmd.buffer_ptr));
  440. userbuf = (uint8_t*)(unsigned long)read_cmd.buffer_ptr;
  441. /* Check write access to buffer */
  442. if(!access_ok(VERIFY_WRITE, userbuf, read_cmd.count))
  443. return -EFAULT;
  444. desc->io_in_progress = 1;
  445. /* Read buffer loads till we fill the user supplied buffer */
  446. remain = read_cmd.count;
  447. while(remain > 0 && end_flag == 0)
  448. {
  449. nbytes = 0;
  450. read_ret = ibrd(board, board->buffer, (board->buffer_length < remain) ? board->buffer_length :
  451. remain, &end_flag, &nbytes);
  452. if(nbytes == 0) break;
  453. retval = copy_to_user(userbuf, board->buffer, nbytes);
  454. if(retval)
  455. {
  456. retval = -EFAULT;
  457. break;
  458. }
  459. remain -= nbytes;
  460. userbuf += nbytes;
  461. if(read_ret < 0) break;
  462. }
  463. read_cmd.count -= remain;
  464. read_cmd.end = end_flag;
  465. /* suppress errors (for example due to timeout or interruption by device clear)
  466. if all bytes got sent. This prevents races that can occur in the various drivers
  467. if a device receives a device clear immediately after a transfer completes and
  468. the driver code wasn't careful enough to handle that case.
  469. */
  470. if(remain == 0 || end_flag)
  471. {
  472. read_ret = 0;
  473. }
  474. if(retval == 0)
  475. retval = copy_to_user((void*) arg, &read_cmd, sizeof(read_cmd));
  476. desc->io_in_progress = 0;
  477. wake_up_interruptible( &board->wait );
  478. if(retval) return -EFAULT;
  479. return read_ret;
  480. }
  481. static int command_ioctl( gpib_file_private_t *file_priv,
  482. gpib_board_t *board, unsigned long arg)
  483. {
  484. read_write_ioctl_t cmd;
  485. uint8_t *userbuf;
  486. unsigned long remain;
  487. int retval;
  488. gpib_descriptor_t *desc;
  489. retval = copy_from_user(&cmd, (void*) arg, sizeof(cmd));
  490. if( retval )
  491. return -EFAULT;
  492. desc = handle_to_descriptor( file_priv, cmd.handle );
  493. if( desc == NULL ) return -EINVAL;
  494. userbuf = (uint8_t*)(unsigned long)cmd.buffer_ptr;
  495. /* Check read access to buffer */
  496. if(!access_ok(VERIFY_READ, userbuf, cmd.count))
  497. return -EFAULT;
  498. /* Write buffer loads till we empty the user supplied buffer */
  499. remain = cmd.count;
  500. desc->io_in_progress = 1;
  501. while( remain > 0 )
  502. {
  503. retval = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
  504. board->buffer_length : remain );
  505. if(retval) retval = -EFAULT;
  506. else
  507. retval = ibcmd(board, board->buffer, (board->buffer_length < remain) ?
  508. board->buffer_length : remain );
  509. if(retval < 0)
  510. {
  511. desc->io_in_progress = 0;
  512. wake_up_interruptible( &board->wait );
  513. return retval;
  514. break;
  515. }
  516. if( retval == 0 ) break;
  517. remain -= retval;
  518. userbuf += retval;
  519. }
  520. cmd.count -= remain;
  521. retval = copy_to_user((void*) arg, &cmd, sizeof(cmd));
  522. desc->io_in_progress = 0;
  523. wake_up_interruptible( &board->wait );
  524. if( retval ) return -EFAULT;
  525. return 0;
  526. }
  527. static int write_ioctl(gpib_file_private_t *file_priv, gpib_board_t *board,
  528. unsigned long arg)
  529. {
  530. read_write_ioctl_t write_cmd;
  531. uint8_t *userbuf;
  532. unsigned long remain;
  533. int retval = 0;
  534. int fault;
  535. gpib_descriptor_t *desc;
  536. fault = copy_from_user(&write_cmd, (void*) arg, sizeof(write_cmd));
  537. if(fault)
  538. return -EFAULT;
  539. desc = handle_to_descriptor( file_priv, write_cmd.handle );
  540. if( desc == NULL ) return -EINVAL;
  541. userbuf = (uint8_t*)(unsigned long)write_cmd.buffer_ptr;
  542. /* Check read access to buffer */
  543. if(!access_ok(VERIFY_READ, userbuf, write_cmd.count))
  544. return -EFAULT;
  545. desc->io_in_progress = 1;
  546. /* Write buffer loads till we empty the user supplied buffer */
  547. remain = write_cmd.count;
  548. while(remain > 0)
  549. {
  550. int send_eoi;
  551. size_t bytes_written = 0;
  552. send_eoi = remain <= board->buffer_length && write_cmd.end;
  553. fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
  554. board->buffer_length : remain );
  555. if(fault)
  556. {
  557. retval = -EFAULT;
  558. break;
  559. }
  560. retval = ibwrt(board, board->buffer, (board->buffer_length < remain) ?
  561. board->buffer_length : remain, send_eoi, &bytes_written);
  562. remain -= bytes_written;
  563. userbuf += bytes_written;
  564. if(retval < 0)
  565. {
  566. break;
  567. }
  568. }
  569. write_cmd.count -= remain;
  570. /* suppress errors (for example due to timeout or interruption by device clear)
  571. if all bytes got sent. This prevents races that can occur in the various drivers
  572. if a device receives a device clear immediately after a transfer completes and
  573. the driver code wasn't careful enough to handle that case.
  574. */
  575. if(remain == 0)
  576. {
  577. retval = 0;
  578. }
  579. fault = copy_to_user((void*) arg, &write_cmd, sizeof(write_cmd));
  580. desc->io_in_progress = 0;
  581. wake_up_interruptible( &board->wait );
  582. if(fault) return -EFAULT;
  583. return retval;
  584. }
  585. static int status_bytes_ioctl( gpib_board_t *board, unsigned long arg )
  586. {
  587. gpib_status_queue_t *device;
  588. spoll_bytes_ioctl_t cmd;
  589. int retval;
  590. retval = copy_from_user( &cmd, (void *) arg, sizeof( cmd ) );
  591. if( retval )
  592. return -EFAULT;
  593. device = get_gpib_status_queue( board, cmd.pad, cmd.sad );
  594. if( device == NULL )
  595. cmd.num_bytes = 0;
  596. else
  597. cmd.num_bytes = num_status_bytes( device );
  598. retval = copy_to_user( (void *) arg, &cmd, sizeof( cmd ) );
  599. if( retval )
  600. return -EFAULT;
  601. return 0;
  602. }
  603. static int increment_open_device_count( struct list_head *head, unsigned int pad, int sad )
  604. {
  605. struct list_head *list_ptr;
  606. gpib_status_queue_t *device;
  607. /* first see if address has already been opened, then increment
  608. * open count */
  609. for( list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next )
  610. {
  611. device = list_entry( list_ptr, gpib_status_queue_t, list );
  612. if( gpib_address_equal( device->pad, device->sad, pad, sad ) )
  613. {
  614. GPIB_DPRINTK( "incrementing open count for pad %i, sad %i\n",
  615. device->pad, device->sad );
  616. device->reference_count++;
  617. return 0;
  618. }
  619. }
  620. /* otherwise we need to allocate a new gpib_status_queue_t */
  621. device = kmalloc( sizeof( gpib_status_queue_t ), GFP_ATOMIC );
  622. if( device == NULL )
  623. return -ENOMEM;
  624. init_gpib_status_queue( device );
  625. device->pad = pad;
  626. device->sad = sad;
  627. device->reference_count = 1;
  628. list_add( &device->list, head );
  629. GPIB_DPRINTK( "opened pad %i, sad %i\n",
  630. device->pad, device->sad );
  631. return 0;
  632. }
  633. static int subtract_open_device_count( struct list_head *head, unsigned int pad, int sad, unsigned int count )
  634. {
  635. gpib_status_queue_t *device;
  636. struct list_head *list_ptr;
  637. for( list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next )
  638. {
  639. device = list_entry( list_ptr, gpib_status_queue_t, list );
  640. if( gpib_address_equal( device->pad, device->sad, pad, sad ) )
  641. {
  642. GPIB_DPRINTK( "decrementing open count for pad %i, sad %i\n",
  643. device->pad, device->sad );
  644. if( count > device->reference_count )
  645. {
  646. printk( "gpib: bug! in subtract_open_device_count()\n" );
  647. return -EINVAL;
  648. }
  649. device->reference_count -= count;
  650. if( device->reference_count == 0 )
  651. {
  652. GPIB_DPRINTK( "closing pad %i, sad %i\n",
  653. device->pad, device->sad );
  654. list_del( list_ptr );
  655. kfree( device );
  656. }
  657. return 0;
  658. }
  659. }
  660. printk( "gpib: bug! tried to close address that was never opened!\n" );
  661. return -EINVAL;
  662. }
  663. static inline int decrement_open_device_count( struct list_head *head, unsigned int pad, int sad )
  664. {
  665. return subtract_open_device_count( head, pad, sad, 1 );
  666. }
  667. static int cleanup_open_devices( gpib_file_private_t *file_priv, gpib_board_t *board )
  668. {
  669. int retval = 0;
  670. int i;
  671. for( i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++ )
  672. {
  673. gpib_descriptor_t *desc;
  674. desc = file_priv->descriptors[ i ];
  675. if( desc == NULL ) continue;
  676. if( desc->is_board == 0 )
  677. {
  678. retval = decrement_open_device_count( &board->device_list, desc->pad,
  679. desc->sad );
  680. if( retval < 0 ) return retval;
  681. }
  682. kfree( desc );
  683. file_priv->descriptors[ i ] = NULL;
  684. }
  685. return 0;
  686. }
  687. static int open_dev_ioctl( struct file *filep, gpib_board_t *board, unsigned long arg )
  688. {
  689. open_dev_ioctl_t open_dev_cmd;
  690. int retval;
  691. gpib_file_private_t *file_priv = filep->private_data;
  692. int i;
  693. retval = copy_from_user( &open_dev_cmd, ( void* ) arg, sizeof( open_dev_cmd ) );
  694. if (retval)
  695. return -EFAULT;
  696. if(mutex_lock_interruptible(&file_priv->descriptors_mutex))
  697. {
  698. return -ERESTARTSYS;
  699. }
  700. for( i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++ )
  701. if( file_priv->descriptors[ i ] == NULL ) break;
  702. if( i == GPIB_MAX_NUM_DESCRIPTORS )
  703. return -ERANGE;
  704. file_priv->descriptors[ i ] = kmalloc( sizeof( gpib_descriptor_t ), GFP_KERNEL );
  705. if( file_priv->descriptors[ i ] == NULL )
  706. {
  707. mutex_unlock(&file_priv->descriptors_mutex);
  708. return -ENOMEM;
  709. }
  710. init_gpib_descriptor( file_priv->descriptors[ i ] );
  711. file_priv->descriptors[ i ]->pad = open_dev_cmd.pad;
  712. file_priv->descriptors[ i ]->sad = open_dev_cmd.sad;
  713. file_priv->descriptors[ i ]->is_board = open_dev_cmd.is_board;
  714. mutex_unlock(&file_priv->descriptors_mutex);
  715. retval = increment_open_device_count( &board->device_list, open_dev_cmd.pad, open_dev_cmd.sad );
  716. if( retval < 0 )
  717. return retval;
  718. /* clear stuck srq state, since we may be able to find service request on
  719. * the new device */
  720. board->stuck_srq = 0;
  721. open_dev_cmd.handle = i;
  722. retval = copy_to_user( ( void* ) arg, &open_dev_cmd, sizeof( open_dev_cmd ) );
  723. if (retval)
  724. return -EFAULT;
  725. return 0;
  726. }
  727. static int close_dev_ioctl( struct file *filep, gpib_board_t *board, unsigned long arg )
  728. {
  729. close_dev_ioctl_t cmd;
  730. gpib_file_private_t *file_priv = filep->private_data;
  731. int retval;
  732. retval = copy_from_user( &cmd, ( void* ) arg, sizeof( cmd ) );
  733. if (retval)
  734. return -EFAULT;
  735. if( cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS ) return -EINVAL;
  736. if( file_priv->descriptors[ cmd.handle ] == NULL ) return -EINVAL;
  737. retval = decrement_open_device_count( &board->device_list, file_priv->descriptors[ cmd.handle ]->pad,
  738. file_priv->descriptors[ cmd.handle ]->sad );
  739. if( retval < 0 ) return retval;
  740. kfree( file_priv->descriptors[ cmd.handle ] );
  741. file_priv->descriptors[ cmd.handle ] = NULL;
  742. return 0;
  743. }
  744. static int serial_poll_ioctl( gpib_board_t *board, unsigned long arg )
  745. {
  746. serial_poll_ioctl_t serial_cmd;
  747. int retval;
  748. retval = copy_from_user( &serial_cmd, ( void* ) arg, sizeof( serial_cmd ) );
  749. if( retval )
  750. return -EFAULT;
  751. retval = get_serial_poll_byte( board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout,
  752. &serial_cmd.status_byte );
  753. if( retval < 0 )
  754. return retval;
  755. retval = copy_to_user( ( void * ) arg, &serial_cmd, sizeof( serial_cmd ) );
  756. if( retval )
  757. return -EFAULT;
  758. return 0;
  759. }
  760. static int wait_ioctl( gpib_file_private_t *file_priv, gpib_board_t *board,
  761. unsigned long arg )
  762. {
  763. wait_ioctl_t wait_cmd;
  764. int retval;
  765. gpib_descriptor_t *desc;
  766. retval = copy_from_user( &wait_cmd, ( void * ) arg, sizeof( wait_cmd ) );
  767. if( retval )
  768. return -EFAULT;
  769. desc = handle_to_descriptor( file_priv, wait_cmd.handle );
  770. if( desc == NULL ) return -EINVAL;
  771. retval = ibwait( board, wait_cmd.wait_mask, wait_cmd.clear_mask,
  772. wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc );
  773. if( retval < 0 ) return retval;
  774. retval = copy_to_user( ( void * ) arg, &wait_cmd, sizeof( wait_cmd ) );
  775. if( retval )
  776. return -EFAULT;
  777. return 0;
  778. }
  779. static int parallel_poll_ioctl( gpib_board_t *board, unsigned long arg )
  780. {
  781. uint8_t poll_byte;
  782. int retval;
  783. retval = ibrpp( board, &poll_byte );
  784. if( retval < 0 )
  785. return retval;
  786. retval = copy_to_user( ( void * ) arg, &poll_byte, sizeof( poll_byte ) );
  787. if( retval )
  788. return -EFAULT;
  789. return 0;
  790. }
  791. static int online_ioctl( gpib_board_t *board, unsigned long arg )
  792. {
  793. online_ioctl_t online_cmd;
  794. gpib_board_config_t config;
  795. int retval;
  796. void *init_data = NULL;
  797. config.init_data = NULL;
  798. if(!capable(CAP_SYS_ADMIN))
  799. return -EPERM;
  800. retval = copy_from_user( &online_cmd, ( void * ) arg, sizeof( online_cmd ) );
  801. if(retval)
  802. return -EFAULT;
  803. if(online_cmd.init_data_length > 0)
  804. {
  805. config.init_data = vmalloc(online_cmd.init_data_length);
  806. if(config.init_data == NULL)
  807. return -ENOMEM;
  808. BUG_ON(sizeof(init_data) > sizeof(online_cmd.init_data_ptr));
  809. init_data = (void*)(unsigned long)(online_cmd.init_data_ptr);
  810. retval = copy_from_user(config.init_data, init_data, online_cmd.init_data_length);
  811. if(retval)
  812. {
  813. vfree(config.init_data);
  814. return -EFAULT;
  815. }
  816. config.init_data_length = online_cmd.init_data_length;
  817. }else
  818. {
  819. config.init_data = NULL;
  820. config.init_data_length = 0;
  821. }
  822. if(online_cmd.online)
  823. retval = ibonline(board, config);
  824. else
  825. retval = iboffline(board);
  826. if(config.init_data)
  827. vfree(config.init_data);
  828. return retval;
  829. }
  830. static int remote_enable_ioctl( gpib_board_t *board, unsigned long arg )
  831. {
  832. int enable;
  833. int retval;
  834. retval = copy_from_user( &enable, ( void * ) arg, sizeof( enable ) );
  835. if( retval )
  836. return -EFAULT;
  837. return ibsre( board, enable );
  838. }
  839. static int take_control_ioctl( gpib_board_t *board, unsigned long arg )
  840. {
  841. int synchronous;
  842. int retval;
  843. retval = copy_from_user( &synchronous, ( void * ) arg, sizeof( synchronous ) );
  844. if( retval )
  845. return -EFAULT;
  846. return ibcac( board, synchronous );
  847. }
  848. static int line_status_ioctl( gpib_board_t *board, unsigned long arg )
  849. {
  850. short lines;
  851. int retval;
  852. retval = iblines( board, &lines );
  853. if( retval < 0 )
  854. return retval;
  855. retval = copy_to_user( ( void * ) arg, &lines, sizeof( lines ) );
  856. if( retval )
  857. return -EFAULT;
  858. return 0;
  859. }
  860. static int pad_ioctl( gpib_board_t *board, gpib_file_private_t *file_priv,
  861. unsigned long arg )
  862. {
  863. pad_ioctl_t cmd;
  864. int retval;
  865. gpib_descriptor_t *desc;
  866. retval = copy_from_user( &cmd, ( void * ) arg, sizeof( cmd ) );
  867. if( retval )
  868. return -EFAULT;
  869. desc = handle_to_descriptor( file_priv, cmd.handle );
  870. if( desc == NULL )
  871. return -EINVAL;
  872. if( desc->is_board )
  873. {
  874. retval = ibpad( board, cmd.pad );
  875. if( retval < 0 ) return retval;
  876. }else
  877. {
  878. retval = decrement_open_device_count( &board->device_list, desc->pad, desc->sad );
  879. if( retval < 0 )
  880. return retval;
  881. desc->pad = cmd.pad;
  882. retval = increment_open_device_count( &board->device_list, desc->pad, desc->sad );
  883. if( retval < 0 )
  884. return retval;
  885. }
  886. return 0;
  887. }
  888. static int sad_ioctl( gpib_board_t *board, gpib_file_private_t *file_priv,
  889. unsigned long arg )
  890. {
  891. sad_ioctl_t cmd;
  892. int retval;
  893. gpib_descriptor_t *desc;
  894. retval = copy_from_user( &cmd, ( void * ) arg, sizeof( cmd ) );
  895. if( retval )
  896. return -EFAULT;
  897. desc = handle_to_descriptor( file_priv, cmd.handle );
  898. if( desc == NULL )
  899. return -EINVAL;
  900. if( desc->is_board )
  901. {
  902. retval = ibsad( board, cmd.sad );
  903. if( retval < 0 ) return retval;
  904. }else
  905. {
  906. retval = decrement_open_device_count( &board->device_list, desc->pad, desc->sad );
  907. if( retval < 0 )
  908. return retval;
  909. desc->sad = cmd.sad;
  910. retval = increment_open_device_count( &board->device_list, desc->pad, desc->sad );
  911. if( retval < 0 )
  912. return retval;
  913. }
  914. return 0;
  915. }
  916. static int eos_ioctl( gpib_board_t *board, unsigned long arg )
  917. {
  918. eos_ioctl_t eos_cmd;
  919. int retval;
  920. retval = copy_from_user( &eos_cmd, ( void * ) arg, sizeof( eos_cmd ) );
  921. if( retval )
  922. return -EFAULT;
  923. return ibeos( board, eos_cmd.eos, eos_cmd.eos_flags );
  924. }
  925. static int request_service_ioctl( gpib_board_t *board, unsigned long arg )
  926. {
  927. uint8_t status_byte;
  928. int retval;
  929. retval = copy_from_user( &status_byte, ( void * ) arg, sizeof( status_byte ) );
  930. if( retval )
  931. return -EFAULT;
  932. return ibrsv( board, status_byte );
  933. }
  934. static int iobase_ioctl( gpib_board_t *board, unsigned long arg )
  935. {
  936. uint64_t base_addr;
  937. int retval;
  938. if(!capable(CAP_SYS_ADMIN))
  939. return -EPERM;
  940. retval = copy_from_user( &base_addr, ( void * ) arg, sizeof( base_addr ) );
  941. if( retval )
  942. return -EFAULT;
  943. BUG_ON(sizeof(void*) > sizeof(base_addr));
  944. board->ibbase = (void*)(unsigned long)(base_addr);
  945. return 0;
  946. }
  947. static int irq_ioctl( gpib_board_t *board, unsigned long arg )
  948. {
  949. unsigned int irq;
  950. int retval;
  951. if(!capable(CAP_SYS_ADMIN))
  952. return -EPERM;
  953. retval = copy_from_user( &irq, ( void * ) arg, sizeof( irq ) );
  954. if( retval )
  955. return -EFAULT;
  956. board->ibirq = irq;
  957. return 0;
  958. }
  959. static int dma_ioctl( gpib_board_t *board, unsigned long arg )
  960. {
  961. unsigned int dma_channel;
  962. int retval;
  963. if(!capable(CAP_SYS_ADMIN))
  964. return -EPERM;
  965. retval = copy_from_user( &dma_channel, ( void * ) arg, sizeof( dma_channel ) );
  966. if( retval )
  967. return -EFAULT;
  968. board->ibdma = dma_channel;
  969. return 0;
  970. }
  971. static int autospoll_ioctl(gpib_board_t *board, unsigned long arg)
  972. {
  973. autospoll_ioctl_t enable;
  974. int retval;
  975. retval = copy_from_user( &enable, ( void * ) arg, sizeof( enable ) );
  976. if(retval)
  977. return -EFAULT;
  978. /*FIXME: should keep track of whether autospolling is on or off
  979. * by descriptor. That would also allow automatic decrement
  980. * of autospollers when descriptors are closed. */
  981. if(enable)
  982. board->autospollers++;
  983. else
  984. {
  985. if(board->autospollers <= 0)
  986. {
  987. printk("gpib: tried to set number of autospollers negative\n");
  988. retval = -EINVAL;
  989. }else
  990. {
  991. board->autospollers--;
  992. retval = 0;
  993. }
  994. }
  995. return retval;
  996. }
  997. static int mutex_ioctl( gpib_board_t *board, gpib_file_private_t *file_priv,
  998. unsigned long arg )
  999. {
  1000. int retval, lock_mutex;
  1001. retval = copy_from_user( &lock_mutex, ( void * ) arg, sizeof( lock_mutex ) );
  1002. if( retval )
  1003. return -EFAULT;
  1004. if( lock_mutex )
  1005. {
  1006. retval = mutex_lock_interruptible(&board->mutex);
  1007. if(retval)
  1008. {
  1009. printk("gpib: ioctl interrupted while waiting on lock\n");
  1010. return -ERESTARTSYS;
  1011. }
  1012. board->locking_pid = current->pid;
  1013. file_priv->holding_mutex = 1;
  1014. GPIB_DPRINTK("locked board %d mutex\n", board->minor);
  1015. }else
  1016. {
  1017. if( current->pid != board->locking_pid )
  1018. {
  1019. printk( "gpib: bug! pid %i tried to release mutex held by pid %i\n",
  1020. current->pid, board->locking_pid );
  1021. return -EPERM;
  1022. }
  1023. file_priv->holding_mutex = 0;
  1024. board->locking_pid = 0;
  1025. mutex_unlock( &board->mutex );
  1026. GPIB_DPRINTK("unlocked board %i mutex\n", board->minor);
  1027. }
  1028. return 0;
  1029. }
  1030. static int timeout_ioctl( gpib_board_t *board, unsigned long arg )
  1031. {
  1032. unsigned int timeout;
  1033. int retval;
  1034. retval = copy_from_user( &timeout, ( void * ) arg, sizeof( timeout ) );
  1035. if( retval )
  1036. return -EFAULT;
  1037. board->usec_timeout = timeout;
  1038. GPIB_DPRINTK( "timeout set to %i usec\n", timeout );
  1039. return 0;
  1040. }
  1041. static int ppc_ioctl( gpib_board_t *board, unsigned long arg)
  1042. {
  1043. ppoll_config_ioctl_t cmd;
  1044. int retval;
  1045. retval = copy_from_user( &cmd, ( void * ) arg, sizeof( cmd ) );
  1046. if( retval )
  1047. return -EFAULT;
  1048. if( cmd.set_ist )
  1049. {
  1050. board->ist = 1;
  1051. board->interface->parallel_poll_response( board, board->ist );
  1052. }else if( cmd.clear_ist )
  1053. {
  1054. board->ist = 0;
  1055. board->interface->parallel_poll_response( board, board->ist );
  1056. }
  1057. if( cmd.config )
  1058. {
  1059. retval = ibppc( board, cmd.config );
  1060. if( retval < 0 ) return retval;
  1061. }
  1062. return 0;
  1063. }
  1064. static int query_board_rsv_ioctl( gpib_board_t *board, unsigned long arg )
  1065. {
  1066. int status;
  1067. int retval;
  1068. status = board->interface->serial_poll_status( board );
  1069. retval = copy_to_user( ( void * ) arg, &status, sizeof( status ) );
  1070. if( retval )
  1071. return -EFAULT;
  1072. return 0;
  1073. }
  1074. static int board_info_ioctl( const gpib_board_t *board, unsigned long arg)
  1075. {
  1076. board_info_ioctl_t info;
  1077. int retval;
  1078. info.pad = board->pad;
  1079. info.sad = board->sad;
  1080. info.parallel_poll_configuration = board->parallel_poll_configuration;
  1081. info.is_system_controller = board->master;
  1082. if( board->autospollers )
  1083. info.autopolling = 1;
  1084. else
  1085. info.autopolling = 0;
  1086. info.t1_delay = board->t1_nano_sec;
  1087. info.ist = board->ist;
  1088. info.no_7_bit_eos = board->interface->no_7_bit_eos;
  1089. retval = copy_to_user( ( void * ) arg, &info, sizeof( info ) );
  1090. if( retval )
  1091. return -EFAULT;
  1092. return 0;
  1093. }
  1094. static int interface_clear_ioctl( gpib_board_t *board, unsigned long arg )
  1095. {
  1096. unsigned int usec_duration;
  1097. int retval;
  1098. retval = copy_from_user( &usec_duration, ( void * ) arg, sizeof( usec_duration ) );
  1099. if( retval )
  1100. return -EFAULT;
  1101. return ibsic( board, usec_duration );
  1102. }
  1103. static int select_pci_ioctl( gpib_board_t *board, unsigned long arg )
  1104. {
  1105. select_pci_ioctl_t selection;
  1106. int retval;
  1107. if(!capable(CAP_SYS_ADMIN))
  1108. return -EPERM;
  1109. retval = copy_from_user( &selection, ( void * ) arg, sizeof( selection ) );
  1110. if( retval ) return -EFAULT;
  1111. board->pci_bus = selection.pci_bus;
  1112. board->pci_slot = selection.pci_slot;
  1113. return 0;
  1114. }
  1115. static int event_ioctl( gpib_board_t *board, unsigned long arg )
  1116. {
  1117. event_ioctl_t user_event;
  1118. int retval;
  1119. short event;
  1120. retval = pop_gpib_event( &board->event_queue, &event );
  1121. if( retval < 0 ) return retval;
  1122. user_event = event;
  1123. retval = copy_to_user( (void*) arg, &user_event, sizeof( user_event ) );
  1124. if( retval ) return -EFAULT;
  1125. return 0;
  1126. }
  1127. static int request_system_control_ioctl( gpib_board_t *board, unsigned long arg )
  1128. {
  1129. rsc_ioctl_t request_control;
  1130. int retval;
  1131. retval = copy_from_user( &request_control, ( void * ) arg, sizeof( request_control ) );
  1132. if( retval ) return -EFAULT;
  1133. ibrsc( board, request_control );
  1134. return 0;
  1135. }
  1136. static int t1_delay_ioctl( gpib_board_t *board, unsigned long arg )
  1137. {
  1138. t1_delay_ioctl_t cmd;
  1139. unsigned int delay;
  1140. int retval;
  1141. if( board->interface->t1_delay == NULL )
  1142. {
  1143. printk("gpib: t1 delay not implemented in driver!\n" );
  1144. return -EIO;
  1145. }
  1146. retval = copy_from_user( &cmd, ( void * ) arg, sizeof( cmd ) );
  1147. if( retval ) return -EFAULT;
  1148. delay = cmd;
  1149. board->t1_nano_sec = board->interface->t1_delay( board, delay );
  1150. return 0;
  1151. }